sor.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. /*
  2. * Copyright (C) 2013 NVIDIA Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/gpio.h>
  11. #include <linux/io.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/reset.h>
  14. #include <soc/tegra/pmc.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_dp_helper.h>
  17. #include <drm/drm_panel.h>
  18. #include "dc.h"
  19. #include "drm.h"
  20. #include "sor.h"
  21. struct tegra_sor {
  22. struct host1x_client client;
  23. struct tegra_output output;
  24. struct device *dev;
  25. void __iomem *regs;
  26. struct reset_control *rst;
  27. struct clk *clk_parent;
  28. struct clk *clk_safe;
  29. struct clk *clk_dp;
  30. struct clk *clk;
  31. struct tegra_dpaux *dpaux;
  32. struct mutex lock;
  33. bool enabled;
  34. struct dentry *debugfs;
  35. };
  36. struct tegra_sor_config {
  37. u32 bits_per_pixel;
  38. u32 active_polarity;
  39. u32 active_count;
  40. u32 tu_size;
  41. u32 active_frac;
  42. u32 watermark;
  43. u32 hblank_symbols;
  44. u32 vblank_symbols;
  45. };
  46. static inline struct tegra_sor *
  47. host1x_client_to_sor(struct host1x_client *client)
  48. {
  49. return container_of(client, struct tegra_sor, client);
  50. }
  51. static inline struct tegra_sor *to_sor(struct tegra_output *output)
  52. {
  53. return container_of(output, struct tegra_sor, output);
  54. }
  55. static inline unsigned long tegra_sor_readl(struct tegra_sor *sor,
  56. unsigned long offset)
  57. {
  58. return readl(sor->regs + (offset << 2));
  59. }
  60. static inline void tegra_sor_writel(struct tegra_sor *sor, unsigned long value,
  61. unsigned long offset)
  62. {
  63. writel(value, sor->regs + (offset << 2));
  64. }
  65. static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
  66. struct drm_dp_link *link)
  67. {
  68. unsigned long value;
  69. unsigned int i;
  70. u8 pattern;
  71. int err;
  72. /* setup lane parameters */
  73. value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
  74. SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
  75. SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
  76. SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
  77. tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT_0);
  78. value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
  79. SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
  80. SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
  81. SOR_LANE_PREEMPHASIS_LANE0(0x0f);
  82. tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS_0);
  83. value = SOR_LANE_POST_CURSOR_LANE3(0x00) |
  84. SOR_LANE_POST_CURSOR_LANE2(0x00) |
  85. SOR_LANE_POST_CURSOR_LANE1(0x00) |
  86. SOR_LANE_POST_CURSOR_LANE0(0x00);
  87. tegra_sor_writel(sor, value, SOR_LANE_POST_CURSOR_0);
  88. /* disable LVDS mode */
  89. tegra_sor_writel(sor, 0, SOR_LVDS);
  90. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  91. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  92. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  93. value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
  94. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  95. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  96. value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  97. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
  98. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  99. usleep_range(10, 100);
  100. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  101. value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  102. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
  103. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  104. err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
  105. if (err < 0)
  106. return err;
  107. for (i = 0, value = 0; i < link->num_lanes; i++) {
  108. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  109. SOR_DP_TPG_SCRAMBLER_NONE |
  110. SOR_DP_TPG_PATTERN_TRAIN1;
  111. value = (value << 8) | lane;
  112. }
  113. tegra_sor_writel(sor, value, SOR_DP_TPG);
  114. pattern = DP_TRAINING_PATTERN_1;
  115. err = tegra_dpaux_train(sor->dpaux, link, pattern);
  116. if (err < 0)
  117. return err;
  118. value = tegra_sor_readl(sor, SOR_DP_SPARE_0);
  119. value |= SOR_DP_SPARE_SEQ_ENABLE;
  120. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  121. value |= SOR_DP_SPARE_MACRO_SOR_CLK;
  122. tegra_sor_writel(sor, value, SOR_DP_SPARE_0);
  123. for (i = 0, value = 0; i < link->num_lanes; i++) {
  124. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  125. SOR_DP_TPG_SCRAMBLER_NONE |
  126. SOR_DP_TPG_PATTERN_TRAIN2;
  127. value = (value << 8) | lane;
  128. }
  129. tegra_sor_writel(sor, value, SOR_DP_TPG);
  130. pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
  131. err = tegra_dpaux_train(sor->dpaux, link, pattern);
  132. if (err < 0)
  133. return err;
  134. for (i = 0, value = 0; i < link->num_lanes; i++) {
  135. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  136. SOR_DP_TPG_SCRAMBLER_GALIOS |
  137. SOR_DP_TPG_PATTERN_NONE;
  138. value = (value << 8) | lane;
  139. }
  140. tegra_sor_writel(sor, value, SOR_DP_TPG);
  141. pattern = DP_TRAINING_PATTERN_DISABLE;
  142. err = tegra_dpaux_train(sor->dpaux, link, pattern);
  143. if (err < 0)
  144. return err;
  145. return 0;
  146. }
  147. static void tegra_sor_super_update(struct tegra_sor *sor)
  148. {
  149. tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
  150. tegra_sor_writel(sor, 1, SOR_SUPER_STATE_0);
  151. tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
  152. }
  153. static void tegra_sor_update(struct tegra_sor *sor)
  154. {
  155. tegra_sor_writel(sor, 0, SOR_STATE_0);
  156. tegra_sor_writel(sor, 1, SOR_STATE_0);
  157. tegra_sor_writel(sor, 0, SOR_STATE_0);
  158. }
  159. static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
  160. {
  161. unsigned long value;
  162. value = tegra_sor_readl(sor, SOR_PWM_DIV);
  163. value &= ~SOR_PWM_DIV_MASK;
  164. value |= 0x400; /* period */
  165. tegra_sor_writel(sor, value, SOR_PWM_DIV);
  166. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  167. value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
  168. value |= 0x400; /* duty cycle */
  169. value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
  170. value |= SOR_PWM_CTL_TRIGGER;
  171. tegra_sor_writel(sor, value, SOR_PWM_CTL);
  172. timeout = jiffies + msecs_to_jiffies(timeout);
  173. while (time_before(jiffies, timeout)) {
  174. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  175. if ((value & SOR_PWM_CTL_TRIGGER) == 0)
  176. return 0;
  177. usleep_range(25, 100);
  178. }
  179. return -ETIMEDOUT;
  180. }
  181. static int tegra_sor_attach(struct tegra_sor *sor)
  182. {
  183. unsigned long value, timeout;
  184. /* wake up in normal mode */
  185. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  186. value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
  187. value |= SOR_SUPER_STATE_MODE_NORMAL;
  188. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  189. tegra_sor_super_update(sor);
  190. /* attach */
  191. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  192. value |= SOR_SUPER_STATE_ATTACHED;
  193. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  194. tegra_sor_super_update(sor);
  195. timeout = jiffies + msecs_to_jiffies(250);
  196. while (time_before(jiffies, timeout)) {
  197. value = tegra_sor_readl(sor, SOR_TEST);
  198. if ((value & SOR_TEST_ATTACHED) != 0)
  199. return 0;
  200. usleep_range(25, 100);
  201. }
  202. return -ETIMEDOUT;
  203. }
  204. static int tegra_sor_wakeup(struct tegra_sor *sor)
  205. {
  206. struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
  207. unsigned long value, timeout;
  208. /* enable display controller outputs */
  209. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  210. value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  211. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
  212. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  213. tegra_dc_commit(dc);
  214. timeout = jiffies + msecs_to_jiffies(250);
  215. /* wait for head to wake up */
  216. while (time_before(jiffies, timeout)) {
  217. value = tegra_sor_readl(sor, SOR_TEST);
  218. value &= SOR_TEST_HEAD_MODE_MASK;
  219. if (value == SOR_TEST_HEAD_MODE_AWAKE)
  220. return 0;
  221. usleep_range(25, 100);
  222. }
  223. return -ETIMEDOUT;
  224. }
  225. static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
  226. {
  227. unsigned long value;
  228. value = tegra_sor_readl(sor, SOR_PWR);
  229. value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
  230. tegra_sor_writel(sor, value, SOR_PWR);
  231. timeout = jiffies + msecs_to_jiffies(timeout);
  232. while (time_before(jiffies, timeout)) {
  233. value = tegra_sor_readl(sor, SOR_PWR);
  234. if ((value & SOR_PWR_TRIGGER) == 0)
  235. return 0;
  236. usleep_range(25, 100);
  237. }
  238. return -ETIMEDOUT;
  239. }
  240. struct tegra_sor_params {
  241. /* number of link clocks per line */
  242. unsigned int num_clocks;
  243. /* ratio between input and output */
  244. u64 ratio;
  245. /* precision factor */
  246. u64 precision;
  247. unsigned int active_polarity;
  248. unsigned int active_count;
  249. unsigned int active_frac;
  250. unsigned int tu_size;
  251. unsigned int error;
  252. };
  253. static int tegra_sor_compute_params(struct tegra_sor *sor,
  254. struct tegra_sor_params *params,
  255. unsigned int tu_size)
  256. {
  257. u64 active_sym, active_count, frac, approx;
  258. u32 active_polarity, active_frac = 0;
  259. const u64 f = params->precision;
  260. s64 error;
  261. active_sym = params->ratio * tu_size;
  262. active_count = div_u64(active_sym, f) * f;
  263. frac = active_sym - active_count;
  264. /* fraction < 0.5 */
  265. if (frac >= (f / 2)) {
  266. active_polarity = 1;
  267. frac = f - frac;
  268. } else {
  269. active_polarity = 0;
  270. }
  271. if (frac != 0) {
  272. frac = div_u64(f * f, frac); /* 1/fraction */
  273. if (frac <= (15 * f)) {
  274. active_frac = div_u64(frac, f);
  275. /* round up */
  276. if (active_polarity)
  277. active_frac++;
  278. } else {
  279. active_frac = active_polarity ? 1 : 15;
  280. }
  281. }
  282. if (active_frac == 1)
  283. active_polarity = 0;
  284. if (active_polarity == 1) {
  285. if (active_frac) {
  286. approx = active_count + (active_frac * (f - 1)) * f;
  287. approx = div_u64(approx, active_frac * f);
  288. } else {
  289. approx = active_count + f;
  290. }
  291. } else {
  292. if (active_frac)
  293. approx = active_count + div_u64(f, active_frac);
  294. else
  295. approx = active_count;
  296. }
  297. error = div_s64(active_sym - approx, tu_size);
  298. error *= params->num_clocks;
  299. if (error <= 0 && abs64(error) < params->error) {
  300. params->active_count = div_u64(active_count, f);
  301. params->active_polarity = active_polarity;
  302. params->active_frac = active_frac;
  303. params->error = abs64(error);
  304. params->tu_size = tu_size;
  305. if (error == 0)
  306. return true;
  307. }
  308. return false;
  309. }
  310. static int tegra_sor_calc_config(struct tegra_sor *sor,
  311. struct drm_display_mode *mode,
  312. struct tegra_sor_config *config,
  313. struct drm_dp_link *link)
  314. {
  315. const u64 f = 100000, link_rate = link->rate * 1000;
  316. const u64 pclk = mode->clock * 1000;
  317. u64 input, output, watermark, num;
  318. struct tegra_sor_params params;
  319. u32 num_syms_per_line;
  320. unsigned int i;
  321. if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
  322. return -EINVAL;
  323. output = link_rate * 8 * link->num_lanes;
  324. input = pclk * config->bits_per_pixel;
  325. if (input >= output)
  326. return -ERANGE;
  327. memset(&params, 0, sizeof(params));
  328. params.ratio = div64_u64(input * f, output);
  329. params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
  330. params.precision = f;
  331. params.error = 64 * f;
  332. params.tu_size = 64;
  333. for (i = params.tu_size; i >= 32; i--)
  334. if (tegra_sor_compute_params(sor, &params, i))
  335. break;
  336. if (params.active_frac == 0) {
  337. config->active_polarity = 0;
  338. config->active_count = params.active_count;
  339. if (!params.active_polarity)
  340. config->active_count--;
  341. config->tu_size = params.tu_size;
  342. config->active_frac = 1;
  343. } else {
  344. config->active_polarity = params.active_polarity;
  345. config->active_count = params.active_count;
  346. config->active_frac = params.active_frac;
  347. config->tu_size = params.tu_size;
  348. }
  349. dev_dbg(sor->dev,
  350. "polarity: %d active count: %d tu size: %d active frac: %d\n",
  351. config->active_polarity, config->active_count,
  352. config->tu_size, config->active_frac);
  353. watermark = params.ratio * config->tu_size * (f - params.ratio);
  354. watermark = div_u64(watermark, f);
  355. watermark = div_u64(watermark + params.error, f);
  356. config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
  357. num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
  358. (link->num_lanes * 8);
  359. if (config->watermark > 30) {
  360. config->watermark = 30;
  361. dev_err(sor->dev,
  362. "unable to compute TU size, forcing watermark to %u\n",
  363. config->watermark);
  364. } else if (config->watermark > num_syms_per_line) {
  365. config->watermark = num_syms_per_line;
  366. dev_err(sor->dev, "watermark too high, forcing to %u\n",
  367. config->watermark);
  368. }
  369. /* compute the number of symbols per horizontal blanking interval */
  370. num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
  371. config->hblank_symbols = div_u64(num, pclk);
  372. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  373. config->hblank_symbols -= 3;
  374. config->hblank_symbols -= 12 / link->num_lanes;
  375. /* compute the number of symbols per vertical blanking interval */
  376. num = (mode->hdisplay - 25) * link_rate;
  377. config->vblank_symbols = div_u64(num, pclk);
  378. config->vblank_symbols -= 36 / link->num_lanes + 4;
  379. dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
  380. config->vblank_symbols);
  381. return 0;
  382. }
  383. static int tegra_sor_detach(struct tegra_sor *sor)
  384. {
  385. unsigned long value, timeout;
  386. /* switch to safe mode */
  387. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  388. value &= ~SOR_SUPER_STATE_MODE_NORMAL;
  389. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  390. tegra_sor_super_update(sor);
  391. timeout = jiffies + msecs_to_jiffies(250);
  392. while (time_before(jiffies, timeout)) {
  393. value = tegra_sor_readl(sor, SOR_PWR);
  394. if (value & SOR_PWR_MODE_SAFE)
  395. break;
  396. }
  397. if ((value & SOR_PWR_MODE_SAFE) == 0)
  398. return -ETIMEDOUT;
  399. /* go to sleep */
  400. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  401. value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
  402. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  403. tegra_sor_super_update(sor);
  404. /* detach */
  405. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  406. value &= ~SOR_SUPER_STATE_ATTACHED;
  407. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  408. tegra_sor_super_update(sor);
  409. timeout = jiffies + msecs_to_jiffies(250);
  410. while (time_before(jiffies, timeout)) {
  411. value = tegra_sor_readl(sor, SOR_TEST);
  412. if ((value & SOR_TEST_ATTACHED) == 0)
  413. break;
  414. usleep_range(25, 100);
  415. }
  416. if ((value & SOR_TEST_ATTACHED) != 0)
  417. return -ETIMEDOUT;
  418. return 0;
  419. }
  420. static int tegra_sor_power_down(struct tegra_sor *sor)
  421. {
  422. unsigned long value, timeout;
  423. int err;
  424. value = tegra_sor_readl(sor, SOR_PWR);
  425. value &= ~SOR_PWR_NORMAL_STATE_PU;
  426. value |= SOR_PWR_TRIGGER;
  427. tegra_sor_writel(sor, value, SOR_PWR);
  428. timeout = jiffies + msecs_to_jiffies(250);
  429. while (time_before(jiffies, timeout)) {
  430. value = tegra_sor_readl(sor, SOR_PWR);
  431. if ((value & SOR_PWR_TRIGGER) == 0)
  432. return 0;
  433. usleep_range(25, 100);
  434. }
  435. if ((value & SOR_PWR_TRIGGER) != 0)
  436. return -ETIMEDOUT;
  437. err = clk_set_parent(sor->clk, sor->clk_safe);
  438. if (err < 0)
  439. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  440. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  441. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  442. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
  443. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  444. /* stop lane sequencer */
  445. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
  446. SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
  447. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  448. timeout = jiffies + msecs_to_jiffies(250);
  449. while (time_before(jiffies, timeout)) {
  450. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  451. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  452. break;
  453. usleep_range(25, 100);
  454. }
  455. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
  456. return -ETIMEDOUT;
  457. value = tegra_sor_readl(sor, SOR_PLL_2);
  458. value |= SOR_PLL_2_PORT_POWERDOWN;
  459. tegra_sor_writel(sor, value, SOR_PLL_2);
  460. usleep_range(20, 100);
  461. value = tegra_sor_readl(sor, SOR_PLL_0);
  462. value |= SOR_PLL_0_POWER_OFF;
  463. value |= SOR_PLL_0_VCOPD;
  464. tegra_sor_writel(sor, value, SOR_PLL_0);
  465. value = tegra_sor_readl(sor, SOR_PLL_2);
  466. value |= SOR_PLL_2_SEQ_PLLCAPPD;
  467. value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  468. tegra_sor_writel(sor, value, SOR_PLL_2);
  469. usleep_range(20, 100);
  470. return 0;
  471. }
  472. static int tegra_sor_crc_open(struct inode *inode, struct file *file)
  473. {
  474. file->private_data = inode->i_private;
  475. return 0;
  476. }
  477. static int tegra_sor_crc_release(struct inode *inode, struct file *file)
  478. {
  479. return 0;
  480. }
  481. static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
  482. {
  483. u32 value;
  484. timeout = jiffies + msecs_to_jiffies(timeout);
  485. while (time_before(jiffies, timeout)) {
  486. value = tegra_sor_readl(sor, SOR_CRC_A);
  487. if (value & SOR_CRC_A_VALID)
  488. return 0;
  489. usleep_range(100, 200);
  490. }
  491. return -ETIMEDOUT;
  492. }
  493. static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
  494. size_t size, loff_t *ppos)
  495. {
  496. struct tegra_sor *sor = file->private_data;
  497. ssize_t num, err;
  498. char buf[10];
  499. u32 value;
  500. mutex_lock(&sor->lock);
  501. if (!sor->enabled) {
  502. err = -EAGAIN;
  503. goto unlock;
  504. }
  505. value = tegra_sor_readl(sor, SOR_STATE_1);
  506. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  507. tegra_sor_writel(sor, value, SOR_STATE_1);
  508. value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
  509. value |= SOR_CRC_CNTRL_ENABLE;
  510. tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
  511. value = tegra_sor_readl(sor, SOR_TEST);
  512. value &= ~SOR_TEST_CRC_POST_SERIALIZE;
  513. tegra_sor_writel(sor, value, SOR_TEST);
  514. err = tegra_sor_crc_wait(sor, 100);
  515. if (err < 0)
  516. goto unlock;
  517. tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A);
  518. value = tegra_sor_readl(sor, SOR_CRC_B);
  519. num = scnprintf(buf, sizeof(buf), "%08x\n", value);
  520. err = simple_read_from_buffer(buffer, size, ppos, buf, num);
  521. unlock:
  522. mutex_unlock(&sor->lock);
  523. return err;
  524. }
  525. static const struct file_operations tegra_sor_crc_fops = {
  526. .owner = THIS_MODULE,
  527. .open = tegra_sor_crc_open,
  528. .read = tegra_sor_crc_read,
  529. .release = tegra_sor_crc_release,
  530. };
  531. static int tegra_sor_debugfs_init(struct tegra_sor *sor,
  532. struct drm_minor *minor)
  533. {
  534. struct dentry *entry;
  535. int err = 0;
  536. sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
  537. if (!sor->debugfs)
  538. return -ENOMEM;
  539. entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
  540. &tegra_sor_crc_fops);
  541. if (!entry) {
  542. dev_err(sor->dev,
  543. "cannot create /sys/kernel/debug/dri/%s/sor/crc\n",
  544. minor->debugfs_root->d_name.name);
  545. err = -ENOMEM;
  546. goto remove;
  547. }
  548. return err;
  549. remove:
  550. debugfs_remove(sor->debugfs);
  551. sor->debugfs = NULL;
  552. return err;
  553. }
  554. static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
  555. {
  556. debugfs_remove_recursive(sor->debugfs);
  557. sor->debugfs = NULL;
  558. }
  559. static void tegra_sor_connector_dpms(struct drm_connector *connector, int mode)
  560. {
  561. }
  562. static enum drm_connector_status
  563. tegra_sor_connector_detect(struct drm_connector *connector, bool force)
  564. {
  565. struct tegra_output *output = connector_to_output(connector);
  566. struct tegra_sor *sor = to_sor(output);
  567. if (sor->dpaux)
  568. return tegra_dpaux_detect(sor->dpaux);
  569. return connector_status_unknown;
  570. }
  571. static const struct drm_connector_funcs tegra_sor_connector_funcs = {
  572. .dpms = tegra_sor_connector_dpms,
  573. .detect = tegra_sor_connector_detect,
  574. .fill_modes = drm_helper_probe_single_connector_modes,
  575. .destroy = tegra_output_connector_destroy,
  576. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  577. };
  578. static int tegra_sor_connector_get_modes(struct drm_connector *connector)
  579. {
  580. struct tegra_output *output = connector_to_output(connector);
  581. struct tegra_sor *sor = to_sor(output);
  582. int err;
  583. if (sor->dpaux)
  584. tegra_dpaux_enable(sor->dpaux);
  585. err = tegra_output_connector_get_modes(connector);
  586. if (sor->dpaux)
  587. tegra_dpaux_disable(sor->dpaux);
  588. return err;
  589. }
  590. static enum drm_mode_status
  591. tegra_sor_connector_mode_valid(struct drm_connector *connector,
  592. struct drm_display_mode *mode)
  593. {
  594. return MODE_OK;
  595. }
  596. static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
  597. .get_modes = tegra_sor_connector_get_modes,
  598. .mode_valid = tegra_sor_connector_mode_valid,
  599. .best_encoder = tegra_output_connector_best_encoder,
  600. };
  601. static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
  602. .destroy = tegra_output_encoder_destroy,
  603. };
  604. static void tegra_sor_encoder_dpms(struct drm_encoder *encoder, int mode)
  605. {
  606. }
  607. static bool tegra_sor_encoder_mode_fixup(struct drm_encoder *encoder,
  608. const struct drm_display_mode *mode,
  609. struct drm_display_mode *adjusted)
  610. {
  611. struct tegra_output *output = encoder_to_output(encoder);
  612. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  613. unsigned long pclk = mode->clock * 1000;
  614. struct tegra_sor *sor = to_sor(output);
  615. int err;
  616. err = tegra_dc_setup_clock(dc, sor->clk_parent, pclk, 0);
  617. if (err < 0) {
  618. dev_err(output->dev, "failed to setup DC clock: %d\n", err);
  619. return false;
  620. }
  621. err = clk_set_rate(sor->clk_parent, pclk);
  622. if (err < 0) {
  623. dev_err(output->dev, "failed to set clock rate to %lu Hz\n",
  624. pclk);
  625. return false;
  626. }
  627. return true;
  628. }
  629. static void tegra_sor_encoder_prepare(struct drm_encoder *encoder)
  630. {
  631. }
  632. static void tegra_sor_encoder_commit(struct drm_encoder *encoder)
  633. {
  634. }
  635. static void tegra_sor_encoder_mode_set(struct drm_encoder *encoder,
  636. struct drm_display_mode *mode,
  637. struct drm_display_mode *adjusted)
  638. {
  639. struct tegra_output *output = encoder_to_output(encoder);
  640. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  641. unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
  642. struct tegra_sor *sor = to_sor(output);
  643. struct tegra_sor_config config;
  644. struct drm_dp_link link;
  645. struct drm_dp_aux *aux;
  646. unsigned long value;
  647. int err = 0;
  648. mutex_lock(&sor->lock);
  649. if (sor->enabled)
  650. goto unlock;
  651. err = clk_prepare_enable(sor->clk);
  652. if (err < 0)
  653. goto unlock;
  654. reset_control_deassert(sor->rst);
  655. if (output->panel)
  656. drm_panel_prepare(output->panel);
  657. /* FIXME: properly convert to struct drm_dp_aux */
  658. aux = (struct drm_dp_aux *)sor->dpaux;
  659. if (sor->dpaux) {
  660. err = tegra_dpaux_enable(sor->dpaux);
  661. if (err < 0)
  662. dev_err(sor->dev, "failed to enable DP: %d\n", err);
  663. err = drm_dp_link_probe(aux, &link);
  664. if (err < 0) {
  665. dev_err(sor->dev, "failed to probe eDP link: %d\n",
  666. err);
  667. goto unlock;
  668. }
  669. }
  670. err = clk_set_parent(sor->clk, sor->clk_safe);
  671. if (err < 0)
  672. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  673. memset(&config, 0, sizeof(config));
  674. config.bits_per_pixel = output->connector.display_info.bpc * 3;
  675. err = tegra_sor_calc_config(sor, mode, &config, &link);
  676. if (err < 0)
  677. dev_err(sor->dev, "failed to compute link configuration: %d\n",
  678. err);
  679. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  680. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  681. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
  682. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  683. value = tegra_sor_readl(sor, SOR_PLL_2);
  684. value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
  685. tegra_sor_writel(sor, value, SOR_PLL_2);
  686. usleep_range(20, 100);
  687. value = tegra_sor_readl(sor, SOR_PLL_3);
  688. value |= SOR_PLL_3_PLL_VDD_MODE_V3_3;
  689. tegra_sor_writel(sor, value, SOR_PLL_3);
  690. value = SOR_PLL_0_ICHPMP(0xf) | SOR_PLL_0_VCOCAP_RST |
  691. SOR_PLL_0_PLLREG_LEVEL_V45 | SOR_PLL_0_RESISTOR_EXT;
  692. tegra_sor_writel(sor, value, SOR_PLL_0);
  693. value = tegra_sor_readl(sor, SOR_PLL_2);
  694. value |= SOR_PLL_2_SEQ_PLLCAPPD;
  695. value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  696. value |= SOR_PLL_2_LVDS_ENABLE;
  697. tegra_sor_writel(sor, value, SOR_PLL_2);
  698. value = SOR_PLL_1_TERM_COMPOUT | SOR_PLL_1_TMDS_TERM;
  699. tegra_sor_writel(sor, value, SOR_PLL_1);
  700. while (true) {
  701. value = tegra_sor_readl(sor, SOR_PLL_2);
  702. if ((value & SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE) == 0)
  703. break;
  704. usleep_range(250, 1000);
  705. }
  706. value = tegra_sor_readl(sor, SOR_PLL_2);
  707. value &= ~SOR_PLL_2_POWERDOWN_OVERRIDE;
  708. value &= ~SOR_PLL_2_PORT_POWERDOWN;
  709. tegra_sor_writel(sor, value, SOR_PLL_2);
  710. /*
  711. * power up
  712. */
  713. /* set safe link bandwidth (1.62 Gbps) */
  714. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  715. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  716. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
  717. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  718. /* step 1 */
  719. value = tegra_sor_readl(sor, SOR_PLL_2);
  720. value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL_2_PORT_POWERDOWN |
  721. SOR_PLL_2_BANDGAP_POWERDOWN;
  722. tegra_sor_writel(sor, value, SOR_PLL_2);
  723. value = tegra_sor_readl(sor, SOR_PLL_0);
  724. value |= SOR_PLL_0_VCOPD | SOR_PLL_0_POWER_OFF;
  725. tegra_sor_writel(sor, value, SOR_PLL_0);
  726. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  727. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  728. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  729. /* step 2 */
  730. err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
  731. if (err < 0) {
  732. dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
  733. goto unlock;
  734. }
  735. usleep_range(5, 100);
  736. /* step 3 */
  737. value = tegra_sor_readl(sor, SOR_PLL_2);
  738. value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
  739. tegra_sor_writel(sor, value, SOR_PLL_2);
  740. usleep_range(20, 100);
  741. /* step 4 */
  742. value = tegra_sor_readl(sor, SOR_PLL_0);
  743. value &= ~SOR_PLL_0_POWER_OFF;
  744. value &= ~SOR_PLL_0_VCOPD;
  745. tegra_sor_writel(sor, value, SOR_PLL_0);
  746. value = tegra_sor_readl(sor, SOR_PLL_2);
  747. value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  748. tegra_sor_writel(sor, value, SOR_PLL_2);
  749. usleep_range(200, 1000);
  750. /* step 5 */
  751. value = tegra_sor_readl(sor, SOR_PLL_2);
  752. value &= ~SOR_PLL_2_PORT_POWERDOWN;
  753. tegra_sor_writel(sor, value, SOR_PLL_2);
  754. /* switch to DP clock */
  755. err = clk_set_parent(sor->clk, sor->clk_dp);
  756. if (err < 0)
  757. dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
  758. /* power DP lanes */
  759. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  760. if (link.num_lanes <= 2)
  761. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
  762. else
  763. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
  764. if (link.num_lanes <= 1)
  765. value &= ~SOR_DP_PADCTL_PD_TXD_1;
  766. else
  767. value |= SOR_DP_PADCTL_PD_TXD_1;
  768. if (link.num_lanes == 0)
  769. value &= ~SOR_DP_PADCTL_PD_TXD_0;
  770. else
  771. value |= SOR_DP_PADCTL_PD_TXD_0;
  772. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  773. value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
  774. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  775. value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
  776. tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
  777. /* start lane sequencer */
  778. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  779. SOR_LANE_SEQ_CTL_POWER_STATE_UP;
  780. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  781. while (true) {
  782. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  783. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  784. break;
  785. usleep_range(250, 1000);
  786. }
  787. /* set link bandwidth */
  788. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  789. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  790. value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
  791. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  792. /* set linkctl */
  793. value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
  794. value |= SOR_DP_LINKCTL_ENABLE;
  795. value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
  796. value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
  797. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  798. tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
  799. for (i = 0, value = 0; i < 4; i++) {
  800. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  801. SOR_DP_TPG_SCRAMBLER_GALIOS |
  802. SOR_DP_TPG_PATTERN_NONE;
  803. value = (value << 8) | lane;
  804. }
  805. tegra_sor_writel(sor, value, SOR_DP_TPG);
  806. value = tegra_sor_readl(sor, SOR_DP_CONFIG_0);
  807. value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
  808. value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
  809. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
  810. value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
  811. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
  812. value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
  813. if (config.active_polarity)
  814. value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  815. else
  816. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  817. value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
  818. value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
  819. tegra_sor_writel(sor, value, SOR_DP_CONFIG_0);
  820. value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  821. value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
  822. value |= config.hblank_symbols & 0xffff;
  823. tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  824. value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  825. value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
  826. value |= config.vblank_symbols & 0xffff;
  827. tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  828. /* enable pad calibration logic */
  829. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  830. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  831. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  832. if (sor->dpaux) {
  833. u8 rate, lanes;
  834. err = drm_dp_link_probe(aux, &link);
  835. if (err < 0) {
  836. dev_err(sor->dev, "failed to probe eDP link: %d\n",
  837. err);
  838. goto unlock;
  839. }
  840. err = drm_dp_link_power_up(aux, &link);
  841. if (err < 0) {
  842. dev_err(sor->dev, "failed to power up eDP link: %d\n",
  843. err);
  844. goto unlock;
  845. }
  846. err = drm_dp_link_configure(aux, &link);
  847. if (err < 0) {
  848. dev_err(sor->dev, "failed to configure eDP link: %d\n",
  849. err);
  850. goto unlock;
  851. }
  852. rate = drm_dp_link_rate_to_bw_code(link.rate);
  853. lanes = link.num_lanes;
  854. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  855. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  856. value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
  857. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  858. value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
  859. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  860. value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
  861. if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  862. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  863. tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
  864. /* disable training pattern generator */
  865. for (i = 0; i < link.num_lanes; i++) {
  866. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  867. SOR_DP_TPG_SCRAMBLER_GALIOS |
  868. SOR_DP_TPG_PATTERN_NONE;
  869. value = (value << 8) | lane;
  870. }
  871. tegra_sor_writel(sor, value, SOR_DP_TPG);
  872. err = tegra_sor_dp_train_fast(sor, &link);
  873. if (err < 0) {
  874. dev_err(sor->dev, "DP fast link training failed: %d\n",
  875. err);
  876. goto unlock;
  877. }
  878. dev_dbg(sor->dev, "fast link training succeeded\n");
  879. }
  880. err = tegra_sor_power_up(sor, 250);
  881. if (err < 0) {
  882. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  883. goto unlock;
  884. }
  885. /* start display controller in continuous mode */
  886. value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
  887. value |= WRITE_MUX;
  888. tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
  889. tegra_dc_writel(dc, VSYNC_H_POSITION(1), DC_DISP_DISP_TIMING_OPTIONS);
  890. tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY, DC_CMD_DISPLAY_COMMAND);
  891. value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
  892. value &= ~WRITE_MUX;
  893. tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
  894. /*
  895. * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
  896. * raster, associate with display controller)
  897. */
  898. value = SOR_STATE_ASY_PROTOCOL_DP_A |
  899. SOR_STATE_ASY_CRC_MODE_COMPLETE |
  900. SOR_STATE_ASY_OWNER(dc->pipe + 1);
  901. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  902. value &= ~SOR_STATE_ASY_HSYNCPOL;
  903. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  904. value |= SOR_STATE_ASY_HSYNCPOL;
  905. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  906. value &= ~SOR_STATE_ASY_VSYNCPOL;
  907. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  908. value |= SOR_STATE_ASY_VSYNCPOL;
  909. switch (config.bits_per_pixel) {
  910. case 24:
  911. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  912. break;
  913. case 18:
  914. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
  915. break;
  916. default:
  917. BUG();
  918. break;
  919. }
  920. tegra_sor_writel(sor, value, SOR_STATE_1);
  921. /*
  922. * TODO: The video timing programming below doesn't seem to match the
  923. * register definitions.
  924. */
  925. value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
  926. tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0));
  927. vse = mode->vsync_end - mode->vsync_start - 1;
  928. hse = mode->hsync_end - mode->hsync_start - 1;
  929. value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
  930. tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0));
  931. vbe = vse + (mode->vsync_start - mode->vdisplay);
  932. hbe = hse + (mode->hsync_start - mode->hdisplay);
  933. value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
  934. tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0));
  935. vbs = vbe + mode->vdisplay;
  936. hbs = hbe + mode->hdisplay;
  937. value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
  938. tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0));
  939. /* CSTM (LVDS, link A/B, upper) */
  940. value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
  941. SOR_CSTM_UPPER;
  942. tegra_sor_writel(sor, value, SOR_CSTM);
  943. /* PWM setup */
  944. err = tegra_sor_setup_pwm(sor, 250);
  945. if (err < 0) {
  946. dev_err(sor->dev, "failed to setup PWM: %d\n", err);
  947. goto unlock;
  948. }
  949. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  950. value |= SOR_ENABLE;
  951. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  952. tegra_sor_update(sor);
  953. err = tegra_sor_attach(sor);
  954. if (err < 0) {
  955. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  956. goto unlock;
  957. }
  958. err = tegra_sor_wakeup(sor);
  959. if (err < 0) {
  960. dev_err(sor->dev, "failed to enable DC: %d\n", err);
  961. goto unlock;
  962. }
  963. if (output->panel)
  964. drm_panel_enable(output->panel);
  965. sor->enabled = true;
  966. unlock:
  967. mutex_unlock(&sor->lock);
  968. }
  969. static void tegra_sor_encoder_disable(struct drm_encoder *encoder)
  970. {
  971. struct tegra_output *output = encoder_to_output(encoder);
  972. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  973. struct tegra_sor *sor = to_sor(output);
  974. u32 value;
  975. int err;
  976. mutex_lock(&sor->lock);
  977. if (!sor->enabled)
  978. goto unlock;
  979. if (output->panel)
  980. drm_panel_disable(output->panel);
  981. err = tegra_sor_detach(sor);
  982. if (err < 0) {
  983. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  984. goto unlock;
  985. }
  986. tegra_sor_writel(sor, 0, SOR_STATE_1);
  987. tegra_sor_update(sor);
  988. /*
  989. * The following accesses registers of the display controller, so make
  990. * sure it's only executed when the output is attached to one.
  991. */
  992. if (dc) {
  993. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  994. value &= ~SOR_ENABLE;
  995. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  996. tegra_dc_commit(dc);
  997. }
  998. err = tegra_sor_power_down(sor);
  999. if (err < 0) {
  1000. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  1001. goto unlock;
  1002. }
  1003. if (sor->dpaux) {
  1004. err = tegra_dpaux_disable(sor->dpaux);
  1005. if (err < 0) {
  1006. dev_err(sor->dev, "failed to disable DP: %d\n", err);
  1007. goto unlock;
  1008. }
  1009. }
  1010. err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
  1011. if (err < 0) {
  1012. dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
  1013. goto unlock;
  1014. }
  1015. if (output->panel)
  1016. drm_panel_unprepare(output->panel);
  1017. clk_disable_unprepare(sor->clk);
  1018. reset_control_assert(sor->rst);
  1019. sor->enabled = false;
  1020. unlock:
  1021. mutex_unlock(&sor->lock);
  1022. }
  1023. static const struct drm_encoder_helper_funcs tegra_sor_encoder_helper_funcs = {
  1024. .dpms = tegra_sor_encoder_dpms,
  1025. .mode_fixup = tegra_sor_encoder_mode_fixup,
  1026. .prepare = tegra_sor_encoder_prepare,
  1027. .commit = tegra_sor_encoder_commit,
  1028. .mode_set = tegra_sor_encoder_mode_set,
  1029. .disable = tegra_sor_encoder_disable,
  1030. };
  1031. static int tegra_sor_init(struct host1x_client *client)
  1032. {
  1033. struct drm_device *drm = dev_get_drvdata(client->parent);
  1034. struct tegra_sor *sor = host1x_client_to_sor(client);
  1035. int err;
  1036. if (!sor->dpaux)
  1037. return -ENODEV;
  1038. sor->output.dev = sor->dev;
  1039. drm_connector_init(drm, &sor->output.connector,
  1040. &tegra_sor_connector_funcs,
  1041. DRM_MODE_CONNECTOR_eDP);
  1042. drm_connector_helper_add(&sor->output.connector,
  1043. &tegra_sor_connector_helper_funcs);
  1044. sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
  1045. drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
  1046. DRM_MODE_ENCODER_TMDS);
  1047. drm_encoder_helper_add(&sor->output.encoder,
  1048. &tegra_sor_encoder_helper_funcs);
  1049. drm_mode_connector_attach_encoder(&sor->output.connector,
  1050. &sor->output.encoder);
  1051. drm_connector_register(&sor->output.connector);
  1052. err = tegra_output_init(drm, &sor->output);
  1053. if (err < 0) {
  1054. dev_err(client->dev, "failed to initialize output: %d\n", err);
  1055. return err;
  1056. }
  1057. sor->output.encoder.possible_crtcs = 0x3;
  1058. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1059. err = tegra_sor_debugfs_init(sor, drm->primary);
  1060. if (err < 0)
  1061. dev_err(sor->dev, "debugfs setup failed: %d\n", err);
  1062. }
  1063. if (sor->dpaux) {
  1064. err = tegra_dpaux_attach(sor->dpaux, &sor->output);
  1065. if (err < 0) {
  1066. dev_err(sor->dev, "failed to attach DP: %d\n", err);
  1067. return err;
  1068. }
  1069. }
  1070. err = clk_prepare_enable(sor->clk);
  1071. if (err < 0) {
  1072. dev_err(sor->dev, "failed to enable clock: %d\n", err);
  1073. return err;
  1074. }
  1075. err = clk_prepare_enable(sor->clk_safe);
  1076. if (err < 0)
  1077. return err;
  1078. err = clk_prepare_enable(sor->clk_dp);
  1079. if (err < 0)
  1080. return err;
  1081. return 0;
  1082. }
  1083. static int tegra_sor_exit(struct host1x_client *client)
  1084. {
  1085. struct tegra_sor *sor = host1x_client_to_sor(client);
  1086. int err;
  1087. tegra_output_exit(&sor->output);
  1088. if (sor->dpaux) {
  1089. err = tegra_dpaux_detach(sor->dpaux);
  1090. if (err < 0) {
  1091. dev_err(sor->dev, "failed to detach DP: %d\n", err);
  1092. return err;
  1093. }
  1094. }
  1095. clk_disable_unprepare(sor->clk_safe);
  1096. clk_disable_unprepare(sor->clk_dp);
  1097. clk_disable_unprepare(sor->clk);
  1098. if (IS_ENABLED(CONFIG_DEBUG_FS))
  1099. tegra_sor_debugfs_exit(sor);
  1100. return 0;
  1101. }
  1102. static const struct host1x_client_ops sor_client_ops = {
  1103. .init = tegra_sor_init,
  1104. .exit = tegra_sor_exit,
  1105. };
  1106. static int tegra_sor_probe(struct platform_device *pdev)
  1107. {
  1108. struct device_node *np;
  1109. struct tegra_sor *sor;
  1110. struct resource *regs;
  1111. int err;
  1112. sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
  1113. if (!sor)
  1114. return -ENOMEM;
  1115. sor->output.dev = sor->dev = &pdev->dev;
  1116. np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
  1117. if (np) {
  1118. sor->dpaux = tegra_dpaux_find_by_of_node(np);
  1119. of_node_put(np);
  1120. if (!sor->dpaux)
  1121. return -EPROBE_DEFER;
  1122. }
  1123. err = tegra_output_probe(&sor->output);
  1124. if (err < 0)
  1125. return err;
  1126. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1127. sor->regs = devm_ioremap_resource(&pdev->dev, regs);
  1128. if (IS_ERR(sor->regs))
  1129. return PTR_ERR(sor->regs);
  1130. sor->rst = devm_reset_control_get(&pdev->dev, "sor");
  1131. if (IS_ERR(sor->rst))
  1132. return PTR_ERR(sor->rst);
  1133. sor->clk = devm_clk_get(&pdev->dev, NULL);
  1134. if (IS_ERR(sor->clk))
  1135. return PTR_ERR(sor->clk);
  1136. sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
  1137. if (IS_ERR(sor->clk_parent))
  1138. return PTR_ERR(sor->clk_parent);
  1139. sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
  1140. if (IS_ERR(sor->clk_safe))
  1141. return PTR_ERR(sor->clk_safe);
  1142. sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
  1143. if (IS_ERR(sor->clk_dp))
  1144. return PTR_ERR(sor->clk_dp);
  1145. INIT_LIST_HEAD(&sor->client.list);
  1146. sor->client.ops = &sor_client_ops;
  1147. sor->client.dev = &pdev->dev;
  1148. mutex_init(&sor->lock);
  1149. err = host1x_client_register(&sor->client);
  1150. if (err < 0) {
  1151. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1152. err);
  1153. return err;
  1154. }
  1155. platform_set_drvdata(pdev, sor);
  1156. return 0;
  1157. }
  1158. static int tegra_sor_remove(struct platform_device *pdev)
  1159. {
  1160. struct tegra_sor *sor = platform_get_drvdata(pdev);
  1161. int err;
  1162. err = host1x_client_unregister(&sor->client);
  1163. if (err < 0) {
  1164. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1165. err);
  1166. return err;
  1167. }
  1168. tegra_output_remove(&sor->output);
  1169. return 0;
  1170. }
  1171. static const struct of_device_id tegra_sor_of_match[] = {
  1172. { .compatible = "nvidia,tegra124-sor", },
  1173. { },
  1174. };
  1175. MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
  1176. struct platform_driver tegra_sor_driver = {
  1177. .driver = {
  1178. .name = "tegra-sor",
  1179. .of_match_table = tegra_sor_of_match,
  1180. },
  1181. .probe = tegra_sor_probe,
  1182. .remove = tegra_sor_remove,
  1183. };