sor.c 35 KB

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