edp_ctrl.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. /*
  2. * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
  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 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/regulator/consumer.h>
  16. #include "drm_crtc.h"
  17. #include "drm_dp_helper.h"
  18. #include "drm_edid.h"
  19. #include "edp.h"
  20. #include "edp.xml.h"
  21. #define VDDA_MIN_UV 1800000 /* uV units */
  22. #define VDDA_MAX_UV 1800000 /* uV units */
  23. #define VDDA_UA_ON_LOAD 100000 /* uA units */
  24. #define VDDA_UA_OFF_LOAD 100 /* uA units */
  25. #define DPCD_LINK_VOLTAGE_MAX 4
  26. #define DPCD_LINK_PRE_EMPHASIS_MAX 4
  27. #define EDP_LINK_BW_MAX DP_LINK_BW_2_7
  28. /* Link training return value */
  29. #define EDP_TRAIN_FAIL -1
  30. #define EDP_TRAIN_SUCCESS 0
  31. #define EDP_TRAIN_RECONFIG 1
  32. #define EDP_CLK_MASK_AHB BIT(0)
  33. #define EDP_CLK_MASK_AUX BIT(1)
  34. #define EDP_CLK_MASK_LINK BIT(2)
  35. #define EDP_CLK_MASK_PIXEL BIT(3)
  36. #define EDP_CLK_MASK_MDP_CORE BIT(4)
  37. #define EDP_CLK_MASK_LINK_CHAN (EDP_CLK_MASK_LINK | EDP_CLK_MASK_PIXEL)
  38. #define EDP_CLK_MASK_AUX_CHAN \
  39. (EDP_CLK_MASK_AHB | EDP_CLK_MASK_AUX | EDP_CLK_MASK_MDP_CORE)
  40. #define EDP_CLK_MASK_ALL (EDP_CLK_MASK_AUX_CHAN | EDP_CLK_MASK_LINK_CHAN)
  41. #define EDP_BACKLIGHT_MAX 255
  42. #define EDP_INTR_STATUS1 \
  43. (EDP_INTERRUPT_REG_1_HPD | EDP_INTERRUPT_REG_1_AUX_I2C_DONE | \
  44. EDP_INTERRUPT_REG_1_WRONG_ADDR | EDP_INTERRUPT_REG_1_TIMEOUT | \
  45. EDP_INTERRUPT_REG_1_NACK_DEFER | EDP_INTERRUPT_REG_1_WRONG_DATA_CNT | \
  46. EDP_INTERRUPT_REG_1_I2C_NACK | EDP_INTERRUPT_REG_1_I2C_DEFER | \
  47. EDP_INTERRUPT_REG_1_PLL_UNLOCK | EDP_INTERRUPT_REG_1_AUX_ERROR)
  48. #define EDP_INTR_MASK1 (EDP_INTR_STATUS1 << 2)
  49. #define EDP_INTR_STATUS2 \
  50. (EDP_INTERRUPT_REG_2_READY_FOR_VIDEO | \
  51. EDP_INTERRUPT_REG_2_IDLE_PATTERNs_SENT | \
  52. EDP_INTERRUPT_REG_2_FRAME_END | EDP_INTERRUPT_REG_2_CRC_UPDATED)
  53. #define EDP_INTR_MASK2 (EDP_INTR_STATUS2 << 2)
  54. struct edp_ctrl {
  55. struct platform_device *pdev;
  56. void __iomem *base;
  57. /* regulators */
  58. struct regulator *vdda_vreg;
  59. struct regulator *lvl_vreg;
  60. /* clocks */
  61. struct clk *aux_clk;
  62. struct clk *pixel_clk;
  63. struct clk *ahb_clk;
  64. struct clk *link_clk;
  65. struct clk *mdp_core_clk;
  66. /* gpios */
  67. struct gpio_desc *panel_en_gpio;
  68. struct gpio_desc *panel_hpd_gpio;
  69. /* completion and mutex */
  70. struct completion idle_comp;
  71. struct mutex dev_mutex; /* To protect device power status */
  72. /* work queue */
  73. struct work_struct on_work;
  74. struct work_struct off_work;
  75. struct workqueue_struct *workqueue;
  76. /* Interrupt register lock */
  77. spinlock_t irq_lock;
  78. bool edp_connected;
  79. bool power_on;
  80. /* edid raw data */
  81. struct edid *edid;
  82. struct drm_dp_link dp_link;
  83. struct drm_dp_aux *drm_aux;
  84. /* dpcd raw data */
  85. u8 dpcd[DP_RECEIVER_CAP_SIZE];
  86. /* Link status */
  87. u8 link_rate;
  88. u8 lane_cnt;
  89. u8 v_level;
  90. u8 p_level;
  91. /* Timing status */
  92. u8 interlaced;
  93. u32 pixel_rate; /* in kHz */
  94. u32 color_depth;
  95. struct edp_aux *aux;
  96. struct edp_phy *phy;
  97. };
  98. struct edp_pixel_clk_div {
  99. u32 rate; /* in kHz */
  100. u32 m;
  101. u32 n;
  102. };
  103. #define EDP_PIXEL_CLK_NUM 8
  104. static const struct edp_pixel_clk_div clk_divs[2][EDP_PIXEL_CLK_NUM] = {
  105. { /* Link clock = 162MHz, source clock = 810MHz */
  106. {119000, 31, 211}, /* WSXGA+ 1680x1050@60Hz CVT */
  107. {130250, 32, 199}, /* UXGA 1600x1200@60Hz CVT */
  108. {148500, 11, 60}, /* FHD 1920x1080@60Hz */
  109. {154000, 50, 263}, /* WUXGA 1920x1200@60Hz CVT */
  110. {209250, 31, 120}, /* QXGA 2048x1536@60Hz CVT */
  111. {268500, 119, 359}, /* WQXGA 2560x1600@60Hz CVT */
  112. {138530, 33, 193}, /* AUO B116HAN03.0 Panel */
  113. {141400, 48, 275}, /* AUO B133HTN01.2 Panel */
  114. },
  115. { /* Link clock = 270MHz, source clock = 675MHz */
  116. {119000, 52, 295}, /* WSXGA+ 1680x1050@60Hz CVT */
  117. {130250, 11, 57}, /* UXGA 1600x1200@60Hz CVT */
  118. {148500, 11, 50}, /* FHD 1920x1080@60Hz */
  119. {154000, 47, 206}, /* WUXGA 1920x1200@60Hz CVT */
  120. {209250, 31, 100}, /* QXGA 2048x1536@60Hz CVT */
  121. {268500, 107, 269}, /* WQXGA 2560x1600@60Hz CVT */
  122. {138530, 63, 307}, /* AUO B116HAN03.0 Panel */
  123. {141400, 53, 253}, /* AUO B133HTN01.2 Panel */
  124. },
  125. };
  126. static int edp_clk_init(struct edp_ctrl *ctrl)
  127. {
  128. struct device *dev = &ctrl->pdev->dev;
  129. int ret;
  130. ctrl->aux_clk = devm_clk_get(dev, "core_clk");
  131. if (IS_ERR(ctrl->aux_clk)) {
  132. ret = PTR_ERR(ctrl->aux_clk);
  133. pr_err("%s: Can't find aux_clk, %d\n", __func__, ret);
  134. ctrl->aux_clk = NULL;
  135. return ret;
  136. }
  137. ctrl->pixel_clk = devm_clk_get(dev, "pixel_clk");
  138. if (IS_ERR(ctrl->pixel_clk)) {
  139. ret = PTR_ERR(ctrl->pixel_clk);
  140. pr_err("%s: Can't find pixel_clk, %d\n", __func__, ret);
  141. ctrl->pixel_clk = NULL;
  142. return ret;
  143. }
  144. ctrl->ahb_clk = devm_clk_get(dev, "iface_clk");
  145. if (IS_ERR(ctrl->ahb_clk)) {
  146. ret = PTR_ERR(ctrl->ahb_clk);
  147. pr_err("%s: Can't find ahb_clk, %d\n", __func__, ret);
  148. ctrl->ahb_clk = NULL;
  149. return ret;
  150. }
  151. ctrl->link_clk = devm_clk_get(dev, "link_clk");
  152. if (IS_ERR(ctrl->link_clk)) {
  153. ret = PTR_ERR(ctrl->link_clk);
  154. pr_err("%s: Can't find link_clk, %d\n", __func__, ret);
  155. ctrl->link_clk = NULL;
  156. return ret;
  157. }
  158. /* need mdp core clock to receive irq */
  159. ctrl->mdp_core_clk = devm_clk_get(dev, "mdp_core_clk");
  160. if (IS_ERR(ctrl->mdp_core_clk)) {
  161. ret = PTR_ERR(ctrl->mdp_core_clk);
  162. pr_err("%s: Can't find mdp_core_clk, %d\n", __func__, ret);
  163. ctrl->mdp_core_clk = NULL;
  164. return ret;
  165. }
  166. return 0;
  167. }
  168. static int edp_clk_enable(struct edp_ctrl *ctrl, u32 clk_mask)
  169. {
  170. int ret;
  171. DBG("mask=%x", clk_mask);
  172. /* ahb_clk should be enabled first */
  173. if (clk_mask & EDP_CLK_MASK_AHB) {
  174. ret = clk_prepare_enable(ctrl->ahb_clk);
  175. if (ret) {
  176. pr_err("%s: Failed to enable ahb clk\n", __func__);
  177. goto f0;
  178. }
  179. }
  180. if (clk_mask & EDP_CLK_MASK_AUX) {
  181. ret = clk_set_rate(ctrl->aux_clk, 19200000);
  182. if (ret) {
  183. pr_err("%s: Failed to set rate aux clk\n", __func__);
  184. goto f1;
  185. }
  186. ret = clk_prepare_enable(ctrl->aux_clk);
  187. if (ret) {
  188. pr_err("%s: Failed to enable aux clk\n", __func__);
  189. goto f1;
  190. }
  191. }
  192. /* Need to set rate and enable link_clk prior to pixel_clk */
  193. if (clk_mask & EDP_CLK_MASK_LINK) {
  194. DBG("edp->link_clk, set_rate %ld",
  195. (unsigned long)ctrl->link_rate * 27000000);
  196. ret = clk_set_rate(ctrl->link_clk,
  197. (unsigned long)ctrl->link_rate * 27000000);
  198. if (ret) {
  199. pr_err("%s: Failed to set rate to link clk\n",
  200. __func__);
  201. goto f2;
  202. }
  203. ret = clk_prepare_enable(ctrl->link_clk);
  204. if (ret) {
  205. pr_err("%s: Failed to enable link clk\n", __func__);
  206. goto f2;
  207. }
  208. }
  209. if (clk_mask & EDP_CLK_MASK_PIXEL) {
  210. DBG("edp->pixel_clk, set_rate %ld",
  211. (unsigned long)ctrl->pixel_rate * 1000);
  212. ret = clk_set_rate(ctrl->pixel_clk,
  213. (unsigned long)ctrl->pixel_rate * 1000);
  214. if (ret) {
  215. pr_err("%s: Failed to set rate to pixel clk\n",
  216. __func__);
  217. goto f3;
  218. }
  219. ret = clk_prepare_enable(ctrl->pixel_clk);
  220. if (ret) {
  221. pr_err("%s: Failed to enable pixel clk\n", __func__);
  222. goto f3;
  223. }
  224. }
  225. if (clk_mask & EDP_CLK_MASK_MDP_CORE) {
  226. ret = clk_prepare_enable(ctrl->mdp_core_clk);
  227. if (ret) {
  228. pr_err("%s: Failed to enable mdp core clk\n", __func__);
  229. goto f4;
  230. }
  231. }
  232. return 0;
  233. f4:
  234. if (clk_mask & EDP_CLK_MASK_PIXEL)
  235. clk_disable_unprepare(ctrl->pixel_clk);
  236. f3:
  237. if (clk_mask & EDP_CLK_MASK_LINK)
  238. clk_disable_unprepare(ctrl->link_clk);
  239. f2:
  240. if (clk_mask & EDP_CLK_MASK_AUX)
  241. clk_disable_unprepare(ctrl->aux_clk);
  242. f1:
  243. if (clk_mask & EDP_CLK_MASK_AHB)
  244. clk_disable_unprepare(ctrl->ahb_clk);
  245. f0:
  246. return ret;
  247. }
  248. static void edp_clk_disable(struct edp_ctrl *ctrl, u32 clk_mask)
  249. {
  250. if (clk_mask & EDP_CLK_MASK_MDP_CORE)
  251. clk_disable_unprepare(ctrl->mdp_core_clk);
  252. if (clk_mask & EDP_CLK_MASK_PIXEL)
  253. clk_disable_unprepare(ctrl->pixel_clk);
  254. if (clk_mask & EDP_CLK_MASK_LINK)
  255. clk_disable_unprepare(ctrl->link_clk);
  256. if (clk_mask & EDP_CLK_MASK_AUX)
  257. clk_disable_unprepare(ctrl->aux_clk);
  258. if (clk_mask & EDP_CLK_MASK_AHB)
  259. clk_disable_unprepare(ctrl->ahb_clk);
  260. }
  261. static int edp_regulator_init(struct edp_ctrl *ctrl)
  262. {
  263. struct device *dev = &ctrl->pdev->dev;
  264. DBG("");
  265. ctrl->vdda_vreg = devm_regulator_get(dev, "vdda");
  266. if (IS_ERR(ctrl->vdda_vreg)) {
  267. pr_err("%s: Could not get vdda reg, ret = %ld\n", __func__,
  268. PTR_ERR(ctrl->vdda_vreg));
  269. ctrl->vdda_vreg = NULL;
  270. return PTR_ERR(ctrl->vdda_vreg);
  271. }
  272. ctrl->lvl_vreg = devm_regulator_get(dev, "lvl-vdd");
  273. if (IS_ERR(ctrl->lvl_vreg)) {
  274. pr_err("Could not get lvl-vdd reg, %ld",
  275. PTR_ERR(ctrl->lvl_vreg));
  276. ctrl->lvl_vreg = NULL;
  277. return PTR_ERR(ctrl->lvl_vreg);
  278. }
  279. return 0;
  280. }
  281. static int edp_regulator_enable(struct edp_ctrl *ctrl)
  282. {
  283. int ret;
  284. ret = regulator_set_voltage(ctrl->vdda_vreg, VDDA_MIN_UV, VDDA_MAX_UV);
  285. if (ret) {
  286. pr_err("%s:vdda_vreg set_voltage failed, %d\n", __func__, ret);
  287. goto vdda_set_fail;
  288. }
  289. ret = regulator_set_load(ctrl->vdda_vreg, VDDA_UA_ON_LOAD);
  290. if (ret < 0) {
  291. pr_err("%s: vdda_vreg set regulator mode failed.\n", __func__);
  292. goto vdda_set_fail;
  293. }
  294. ret = regulator_enable(ctrl->vdda_vreg);
  295. if (ret) {
  296. pr_err("%s: Failed to enable vdda_vreg regulator.\n", __func__);
  297. goto vdda_enable_fail;
  298. }
  299. ret = regulator_enable(ctrl->lvl_vreg);
  300. if (ret) {
  301. pr_err("Failed to enable lvl-vdd reg regulator, %d", ret);
  302. goto lvl_enable_fail;
  303. }
  304. DBG("exit");
  305. return 0;
  306. lvl_enable_fail:
  307. regulator_disable(ctrl->vdda_vreg);
  308. vdda_enable_fail:
  309. regulator_set_load(ctrl->vdda_vreg, VDDA_UA_OFF_LOAD);
  310. vdda_set_fail:
  311. return ret;
  312. }
  313. static void edp_regulator_disable(struct edp_ctrl *ctrl)
  314. {
  315. regulator_disable(ctrl->lvl_vreg);
  316. regulator_disable(ctrl->vdda_vreg);
  317. regulator_set_load(ctrl->vdda_vreg, VDDA_UA_OFF_LOAD);
  318. }
  319. static int edp_gpio_config(struct edp_ctrl *ctrl)
  320. {
  321. struct device *dev = &ctrl->pdev->dev;
  322. int ret;
  323. ctrl->panel_hpd_gpio = devm_gpiod_get(dev, "panel-hpd", GPIOD_IN);
  324. if (IS_ERR(ctrl->panel_hpd_gpio)) {
  325. ret = PTR_ERR(ctrl->panel_hpd_gpio);
  326. ctrl->panel_hpd_gpio = NULL;
  327. pr_err("%s: cannot get panel-hpd-gpios, %d\n", __func__, ret);
  328. return ret;
  329. }
  330. ctrl->panel_en_gpio = devm_gpiod_get(dev, "panel-en", GPIOD_OUT_LOW);
  331. if (IS_ERR(ctrl->panel_en_gpio)) {
  332. ret = PTR_ERR(ctrl->panel_en_gpio);
  333. ctrl->panel_en_gpio = NULL;
  334. pr_err("%s: cannot get panel-en-gpios, %d\n", __func__, ret);
  335. return ret;
  336. }
  337. DBG("gpio on");
  338. return 0;
  339. }
  340. static void edp_ctrl_irq_enable(struct edp_ctrl *ctrl, int enable)
  341. {
  342. unsigned long flags;
  343. DBG("%d", enable);
  344. spin_lock_irqsave(&ctrl->irq_lock, flags);
  345. if (enable) {
  346. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_1, EDP_INTR_MASK1);
  347. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_2, EDP_INTR_MASK2);
  348. } else {
  349. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_1, 0x0);
  350. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_2, 0x0);
  351. }
  352. spin_unlock_irqrestore(&ctrl->irq_lock, flags);
  353. DBG("exit");
  354. }
  355. static void edp_fill_link_cfg(struct edp_ctrl *ctrl)
  356. {
  357. u32 prate;
  358. u32 lrate;
  359. u32 bpp;
  360. u8 max_lane = ctrl->dp_link.num_lanes;
  361. u8 lane;
  362. prate = ctrl->pixel_rate;
  363. bpp = ctrl->color_depth * 3;
  364. /*
  365. * By default, use the maximum link rate and minimum lane count,
  366. * so that we can do rate down shift during link training.
  367. */
  368. ctrl->link_rate = drm_dp_link_rate_to_bw_code(ctrl->dp_link.rate);
  369. prate *= bpp;
  370. prate /= 8; /* in kByte */
  371. lrate = 270000; /* in kHz */
  372. lrate *= ctrl->link_rate;
  373. lrate /= 10; /* in kByte, 10 bits --> 8 bits */
  374. for (lane = 1; lane <= max_lane; lane <<= 1) {
  375. if (lrate >= prate)
  376. break;
  377. lrate <<= 1;
  378. }
  379. ctrl->lane_cnt = lane;
  380. DBG("rate=%d lane=%d", ctrl->link_rate, ctrl->lane_cnt);
  381. }
  382. static void edp_config_ctrl(struct edp_ctrl *ctrl)
  383. {
  384. u32 data;
  385. enum edp_color_depth depth;
  386. data = EDP_CONFIGURATION_CTRL_LANES(ctrl->lane_cnt - 1);
  387. if (ctrl->dp_link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  388. data |= EDP_CONFIGURATION_CTRL_ENHANCED_FRAMING;
  389. depth = EDP_6BIT;
  390. if (ctrl->color_depth == 8)
  391. depth = EDP_8BIT;
  392. data |= EDP_CONFIGURATION_CTRL_COLOR(depth);
  393. if (!ctrl->interlaced) /* progressive */
  394. data |= EDP_CONFIGURATION_CTRL_PROGRESSIVE;
  395. data |= (EDP_CONFIGURATION_CTRL_SYNC_CLK |
  396. EDP_CONFIGURATION_CTRL_STATIC_MVID);
  397. edp_write(ctrl->base + REG_EDP_CONFIGURATION_CTRL, data);
  398. }
  399. static void edp_state_ctrl(struct edp_ctrl *ctrl, u32 state)
  400. {
  401. edp_write(ctrl->base + REG_EDP_STATE_CTRL, state);
  402. /* Make sure H/W status is set */
  403. wmb();
  404. }
  405. static int edp_lane_set_write(struct edp_ctrl *ctrl,
  406. u8 voltage_level, u8 pre_emphasis_level)
  407. {
  408. int i;
  409. u8 buf[4];
  410. if (voltage_level >= DPCD_LINK_VOLTAGE_MAX)
  411. voltage_level |= 0x04;
  412. if (pre_emphasis_level >= DPCD_LINK_PRE_EMPHASIS_MAX)
  413. pre_emphasis_level |= 0x04;
  414. pre_emphasis_level <<= 3;
  415. for (i = 0; i < 4; i++)
  416. buf[i] = voltage_level | pre_emphasis_level;
  417. DBG("%s: p|v=0x%x", __func__, voltage_level | pre_emphasis_level);
  418. if (drm_dp_dpcd_write(ctrl->drm_aux, 0x103, buf, 4) < 4) {
  419. pr_err("%s: Set sw/pe to panel failed\n", __func__);
  420. return -ENOLINK;
  421. }
  422. return 0;
  423. }
  424. static int edp_train_pattern_set_write(struct edp_ctrl *ctrl, u8 pattern)
  425. {
  426. u8 p = pattern;
  427. DBG("pattern=%x", p);
  428. if (drm_dp_dpcd_write(ctrl->drm_aux,
  429. DP_TRAINING_PATTERN_SET, &p, 1) < 1) {
  430. pr_err("%s: Set training pattern to panel failed\n", __func__);
  431. return -ENOLINK;
  432. }
  433. return 0;
  434. }
  435. static void edp_sink_train_set_adjust(struct edp_ctrl *ctrl,
  436. const u8 *link_status)
  437. {
  438. int i;
  439. u8 max = 0;
  440. u8 data;
  441. /* use the max level across lanes */
  442. for (i = 0; i < ctrl->lane_cnt; i++) {
  443. data = drm_dp_get_adjust_request_voltage(link_status, i);
  444. DBG("lane=%d req_voltage_swing=0x%x", i, data);
  445. if (max < data)
  446. max = data;
  447. }
  448. ctrl->v_level = max >> DP_TRAIN_VOLTAGE_SWING_SHIFT;
  449. /* use the max level across lanes */
  450. max = 0;
  451. for (i = 0; i < ctrl->lane_cnt; i++) {
  452. data = drm_dp_get_adjust_request_pre_emphasis(link_status, i);
  453. DBG("lane=%d req_pre_emphasis=0x%x", i, data);
  454. if (max < data)
  455. max = data;
  456. }
  457. ctrl->p_level = max >> DP_TRAIN_PRE_EMPHASIS_SHIFT;
  458. DBG("v_level=%d, p_level=%d", ctrl->v_level, ctrl->p_level);
  459. }
  460. static void edp_host_train_set(struct edp_ctrl *ctrl, u32 train)
  461. {
  462. int cnt = 10;
  463. u32 data;
  464. u32 shift = train - 1;
  465. DBG("train=%d", train);
  466. edp_state_ctrl(ctrl, EDP_STATE_CTRL_TRAIN_PATTERN_1 << shift);
  467. while (--cnt) {
  468. data = edp_read(ctrl->base + REG_EDP_MAINLINK_READY);
  469. if (data & (EDP_MAINLINK_READY_TRAIN_PATTERN_1_READY << shift))
  470. break;
  471. }
  472. if (cnt == 0)
  473. pr_err("%s: set link_train=%d failed\n", __func__, train);
  474. }
  475. static const u8 vm_pre_emphasis[4][4] = {
  476. {0x03, 0x06, 0x09, 0x0C}, /* pe0, 0 db */
  477. {0x03, 0x06, 0x09, 0xFF}, /* pe1, 3.5 db */
  478. {0x03, 0x06, 0xFF, 0xFF}, /* pe2, 6.0 db */
  479. {0x03, 0xFF, 0xFF, 0xFF} /* pe3, 9.5 db */
  480. };
  481. /* voltage swing, 0.2v and 1.0v are not support */
  482. static const u8 vm_voltage_swing[4][4] = {
  483. {0x14, 0x18, 0x1A, 0x1E}, /* sw0, 0.4v */
  484. {0x18, 0x1A, 0x1E, 0xFF}, /* sw1, 0.6 v */
  485. {0x1A, 0x1E, 0xFF, 0xFF}, /* sw1, 0.8 v */
  486. {0x1E, 0xFF, 0xFF, 0xFF} /* sw1, 1.2 v, optional */
  487. };
  488. static int edp_voltage_pre_emphasise_set(struct edp_ctrl *ctrl)
  489. {
  490. u32 value0;
  491. u32 value1;
  492. DBG("v=%d p=%d", ctrl->v_level, ctrl->p_level);
  493. value0 = vm_pre_emphasis[(int)(ctrl->v_level)][(int)(ctrl->p_level)];
  494. value1 = vm_voltage_swing[(int)(ctrl->v_level)][(int)(ctrl->p_level)];
  495. /* Configure host and panel only if both values are allowed */
  496. if (value0 != 0xFF && value1 != 0xFF) {
  497. msm_edp_phy_vm_pe_cfg(ctrl->phy, value0, value1);
  498. return edp_lane_set_write(ctrl, ctrl->v_level, ctrl->p_level);
  499. }
  500. return -EINVAL;
  501. }
  502. static int edp_start_link_train_1(struct edp_ctrl *ctrl)
  503. {
  504. u8 link_status[DP_LINK_STATUS_SIZE];
  505. u8 old_v_level;
  506. int tries;
  507. int ret;
  508. int rlen;
  509. DBG("");
  510. edp_host_train_set(ctrl, DP_TRAINING_PATTERN_1);
  511. ret = edp_voltage_pre_emphasise_set(ctrl);
  512. if (ret)
  513. return ret;
  514. ret = edp_train_pattern_set_write(ctrl,
  515. DP_TRAINING_PATTERN_1 | DP_RECOVERED_CLOCK_OUT_EN);
  516. if (ret)
  517. return ret;
  518. tries = 0;
  519. old_v_level = ctrl->v_level;
  520. while (1) {
  521. drm_dp_link_train_clock_recovery_delay(ctrl->dpcd);
  522. rlen = drm_dp_dpcd_read_link_status(ctrl->drm_aux, link_status);
  523. if (rlen < DP_LINK_STATUS_SIZE) {
  524. pr_err("%s: read link status failed\n", __func__);
  525. return -ENOLINK;
  526. }
  527. if (drm_dp_clock_recovery_ok(link_status, ctrl->lane_cnt)) {
  528. ret = 0;
  529. break;
  530. }
  531. if (ctrl->v_level == DPCD_LINK_VOLTAGE_MAX) {
  532. ret = -1;
  533. break;
  534. }
  535. if (old_v_level == ctrl->v_level) {
  536. tries++;
  537. if (tries >= 5) {
  538. ret = -1;
  539. break;
  540. }
  541. } else {
  542. tries = 0;
  543. old_v_level = ctrl->v_level;
  544. }
  545. edp_sink_train_set_adjust(ctrl, link_status);
  546. ret = edp_voltage_pre_emphasise_set(ctrl);
  547. if (ret)
  548. return ret;
  549. }
  550. return ret;
  551. }
  552. static int edp_start_link_train_2(struct edp_ctrl *ctrl)
  553. {
  554. u8 link_status[DP_LINK_STATUS_SIZE];
  555. int tries = 0;
  556. int ret;
  557. int rlen;
  558. DBG("");
  559. edp_host_train_set(ctrl, DP_TRAINING_PATTERN_2);
  560. ret = edp_voltage_pre_emphasise_set(ctrl);
  561. if (ret)
  562. return ret;
  563. ret = edp_train_pattern_set_write(ctrl,
  564. DP_TRAINING_PATTERN_2 | DP_RECOVERED_CLOCK_OUT_EN);
  565. if (ret)
  566. return ret;
  567. while (1) {
  568. drm_dp_link_train_channel_eq_delay(ctrl->dpcd);
  569. rlen = drm_dp_dpcd_read_link_status(ctrl->drm_aux, link_status);
  570. if (rlen < DP_LINK_STATUS_SIZE) {
  571. pr_err("%s: read link status failed\n", __func__);
  572. return -ENOLINK;
  573. }
  574. if (drm_dp_channel_eq_ok(link_status, ctrl->lane_cnt)) {
  575. ret = 0;
  576. break;
  577. }
  578. tries++;
  579. if (tries > 10) {
  580. ret = -1;
  581. break;
  582. }
  583. edp_sink_train_set_adjust(ctrl, link_status);
  584. ret = edp_voltage_pre_emphasise_set(ctrl);
  585. if (ret)
  586. return ret;
  587. }
  588. return ret;
  589. }
  590. static int edp_link_rate_down_shift(struct edp_ctrl *ctrl)
  591. {
  592. u32 prate, lrate, bpp;
  593. u8 rate, lane, max_lane;
  594. int changed = 0;
  595. rate = ctrl->link_rate;
  596. lane = ctrl->lane_cnt;
  597. max_lane = ctrl->dp_link.num_lanes;
  598. bpp = ctrl->color_depth * 3;
  599. prate = ctrl->pixel_rate;
  600. prate *= bpp;
  601. prate /= 8; /* in kByte */
  602. if (rate > DP_LINK_BW_1_62 && rate <= EDP_LINK_BW_MAX) {
  603. rate -= 4; /* reduce rate */
  604. changed++;
  605. }
  606. if (changed) {
  607. if (lane >= 1 && lane < max_lane)
  608. lane <<= 1; /* increase lane */
  609. lrate = 270000; /* in kHz */
  610. lrate *= rate;
  611. lrate /= 10; /* kByte, 10 bits --> 8 bits */
  612. lrate *= lane;
  613. DBG("new lrate=%u prate=%u(kHz) rate=%d lane=%d p=%u b=%d",
  614. lrate, prate, rate, lane,
  615. ctrl->pixel_rate,
  616. bpp);
  617. if (lrate > prate) {
  618. ctrl->link_rate = rate;
  619. ctrl->lane_cnt = lane;
  620. DBG("new rate=%d %d", rate, lane);
  621. return 0;
  622. }
  623. }
  624. return -EINVAL;
  625. }
  626. static int edp_clear_training_pattern(struct edp_ctrl *ctrl)
  627. {
  628. int ret;
  629. ret = edp_train_pattern_set_write(ctrl, 0);
  630. drm_dp_link_train_channel_eq_delay(ctrl->dpcd);
  631. return ret;
  632. }
  633. static int edp_do_link_train(struct edp_ctrl *ctrl)
  634. {
  635. int ret;
  636. struct drm_dp_link dp_link;
  637. DBG("");
  638. /*
  639. * Set the current link rate and lane cnt to panel. They may have been
  640. * adjusted and the values are different from them in DPCD CAP
  641. */
  642. dp_link.num_lanes = ctrl->lane_cnt;
  643. dp_link.rate = drm_dp_bw_code_to_link_rate(ctrl->link_rate);
  644. dp_link.capabilities = ctrl->dp_link.capabilities;
  645. if (drm_dp_link_configure(ctrl->drm_aux, &dp_link) < 0)
  646. return EDP_TRAIN_FAIL;
  647. ctrl->v_level = 0; /* start from default level */
  648. ctrl->p_level = 0;
  649. edp_state_ctrl(ctrl, 0);
  650. if (edp_clear_training_pattern(ctrl))
  651. return EDP_TRAIN_FAIL;
  652. ret = edp_start_link_train_1(ctrl);
  653. if (ret < 0) {
  654. if (edp_link_rate_down_shift(ctrl) == 0) {
  655. DBG("link reconfig");
  656. ret = EDP_TRAIN_RECONFIG;
  657. goto clear;
  658. } else {
  659. pr_err("%s: Training 1 failed", __func__);
  660. ret = EDP_TRAIN_FAIL;
  661. goto clear;
  662. }
  663. }
  664. DBG("Training 1 completed successfully");
  665. edp_state_ctrl(ctrl, 0);
  666. if (edp_clear_training_pattern(ctrl))
  667. return EDP_TRAIN_FAIL;
  668. ret = edp_start_link_train_2(ctrl);
  669. if (ret < 0) {
  670. if (edp_link_rate_down_shift(ctrl) == 0) {
  671. DBG("link reconfig");
  672. ret = EDP_TRAIN_RECONFIG;
  673. goto clear;
  674. } else {
  675. pr_err("%s: Training 2 failed", __func__);
  676. ret = EDP_TRAIN_FAIL;
  677. goto clear;
  678. }
  679. }
  680. DBG("Training 2 completed successfully");
  681. edp_state_ctrl(ctrl, EDP_STATE_CTRL_SEND_VIDEO);
  682. clear:
  683. edp_clear_training_pattern(ctrl);
  684. return ret;
  685. }
  686. static void edp_clock_synchrous(struct edp_ctrl *ctrl, int sync)
  687. {
  688. u32 data;
  689. enum edp_color_depth depth;
  690. data = edp_read(ctrl->base + REG_EDP_MISC1_MISC0);
  691. if (sync)
  692. data |= EDP_MISC1_MISC0_SYNC;
  693. else
  694. data &= ~EDP_MISC1_MISC0_SYNC;
  695. /* only legacy rgb mode supported */
  696. depth = EDP_6BIT; /* Default */
  697. if (ctrl->color_depth == 8)
  698. depth = EDP_8BIT;
  699. else if (ctrl->color_depth == 10)
  700. depth = EDP_10BIT;
  701. else if (ctrl->color_depth == 12)
  702. depth = EDP_12BIT;
  703. else if (ctrl->color_depth == 16)
  704. depth = EDP_16BIT;
  705. data |= EDP_MISC1_MISC0_COLOR(depth);
  706. edp_write(ctrl->base + REG_EDP_MISC1_MISC0, data);
  707. }
  708. static int edp_sw_mvid_nvid(struct edp_ctrl *ctrl, u32 m, u32 n)
  709. {
  710. u32 n_multi, m_multi = 5;
  711. if (ctrl->link_rate == DP_LINK_BW_1_62) {
  712. n_multi = 1;
  713. } else if (ctrl->link_rate == DP_LINK_BW_2_7) {
  714. n_multi = 2;
  715. } else {
  716. pr_err("%s: Invalid link rate, %d\n", __func__,
  717. ctrl->link_rate);
  718. return -EINVAL;
  719. }
  720. edp_write(ctrl->base + REG_EDP_SOFTWARE_MVID, m * m_multi);
  721. edp_write(ctrl->base + REG_EDP_SOFTWARE_NVID, n * n_multi);
  722. return 0;
  723. }
  724. static void edp_mainlink_ctrl(struct edp_ctrl *ctrl, int enable)
  725. {
  726. u32 data = 0;
  727. edp_write(ctrl->base + REG_EDP_MAINLINK_CTRL, EDP_MAINLINK_CTRL_RESET);
  728. /* Make sure fully reset */
  729. wmb();
  730. usleep_range(500, 1000);
  731. if (enable)
  732. data |= EDP_MAINLINK_CTRL_ENABLE;
  733. edp_write(ctrl->base + REG_EDP_MAINLINK_CTRL, data);
  734. }
  735. static void edp_ctrl_phy_aux_enable(struct edp_ctrl *ctrl, int enable)
  736. {
  737. if (enable) {
  738. edp_regulator_enable(ctrl);
  739. edp_clk_enable(ctrl, EDP_CLK_MASK_AUX_CHAN);
  740. msm_edp_phy_ctrl(ctrl->phy, 1);
  741. msm_edp_aux_ctrl(ctrl->aux, 1);
  742. gpiod_set_value(ctrl->panel_en_gpio, 1);
  743. } else {
  744. gpiod_set_value(ctrl->panel_en_gpio, 0);
  745. msm_edp_aux_ctrl(ctrl->aux, 0);
  746. msm_edp_phy_ctrl(ctrl->phy, 0);
  747. edp_clk_disable(ctrl, EDP_CLK_MASK_AUX_CHAN);
  748. edp_regulator_disable(ctrl);
  749. }
  750. }
  751. static void edp_ctrl_link_enable(struct edp_ctrl *ctrl, int enable)
  752. {
  753. u32 m, n;
  754. if (enable) {
  755. /* Enable link channel clocks */
  756. edp_clk_enable(ctrl, EDP_CLK_MASK_LINK_CHAN);
  757. msm_edp_phy_lane_power_ctrl(ctrl->phy, true, ctrl->lane_cnt);
  758. msm_edp_phy_vm_pe_init(ctrl->phy);
  759. /* Make sure phy is programed */
  760. wmb();
  761. msm_edp_phy_ready(ctrl->phy);
  762. edp_config_ctrl(ctrl);
  763. msm_edp_ctrl_pixel_clock_valid(ctrl, ctrl->pixel_rate, &m, &n);
  764. edp_sw_mvid_nvid(ctrl, m, n);
  765. edp_mainlink_ctrl(ctrl, 1);
  766. } else {
  767. edp_mainlink_ctrl(ctrl, 0);
  768. msm_edp_phy_lane_power_ctrl(ctrl->phy, false, 0);
  769. edp_clk_disable(ctrl, EDP_CLK_MASK_LINK_CHAN);
  770. }
  771. }
  772. static int edp_ctrl_training(struct edp_ctrl *ctrl)
  773. {
  774. int ret;
  775. /* Do link training only when power is on */
  776. if (!ctrl->power_on)
  777. return -EINVAL;
  778. train_start:
  779. ret = edp_do_link_train(ctrl);
  780. if (ret == EDP_TRAIN_RECONFIG) {
  781. /* Re-configure main link */
  782. edp_ctrl_irq_enable(ctrl, 0);
  783. edp_ctrl_link_enable(ctrl, 0);
  784. msm_edp_phy_ctrl(ctrl->phy, 0);
  785. /* Make sure link is fully disabled */
  786. wmb();
  787. usleep_range(500, 1000);
  788. msm_edp_phy_ctrl(ctrl->phy, 1);
  789. edp_ctrl_link_enable(ctrl, 1);
  790. edp_ctrl_irq_enable(ctrl, 1);
  791. goto train_start;
  792. }
  793. return ret;
  794. }
  795. static void edp_ctrl_on_worker(struct work_struct *work)
  796. {
  797. struct edp_ctrl *ctrl = container_of(
  798. work, struct edp_ctrl, on_work);
  799. int ret;
  800. mutex_lock(&ctrl->dev_mutex);
  801. if (ctrl->power_on) {
  802. DBG("already on");
  803. goto unlock_ret;
  804. }
  805. edp_ctrl_phy_aux_enable(ctrl, 1);
  806. edp_ctrl_link_enable(ctrl, 1);
  807. edp_ctrl_irq_enable(ctrl, 1);
  808. ret = drm_dp_link_power_up(ctrl->drm_aux, &ctrl->dp_link);
  809. if (ret)
  810. goto fail;
  811. ctrl->power_on = true;
  812. /* Start link training */
  813. ret = edp_ctrl_training(ctrl);
  814. if (ret != EDP_TRAIN_SUCCESS)
  815. goto fail;
  816. DBG("DONE");
  817. goto unlock_ret;
  818. fail:
  819. edp_ctrl_irq_enable(ctrl, 0);
  820. edp_ctrl_link_enable(ctrl, 0);
  821. edp_ctrl_phy_aux_enable(ctrl, 0);
  822. ctrl->power_on = false;
  823. unlock_ret:
  824. mutex_unlock(&ctrl->dev_mutex);
  825. }
  826. static void edp_ctrl_off_worker(struct work_struct *work)
  827. {
  828. struct edp_ctrl *ctrl = container_of(
  829. work, struct edp_ctrl, off_work);
  830. unsigned long time_left;
  831. mutex_lock(&ctrl->dev_mutex);
  832. if (!ctrl->power_on) {
  833. DBG("already off");
  834. goto unlock_ret;
  835. }
  836. reinit_completion(&ctrl->idle_comp);
  837. edp_state_ctrl(ctrl, EDP_STATE_CTRL_PUSH_IDLE);
  838. time_left = wait_for_completion_timeout(&ctrl->idle_comp,
  839. msecs_to_jiffies(500));
  840. if (!time_left)
  841. DBG("%s: idle pattern timedout\n", __func__);
  842. edp_state_ctrl(ctrl, 0);
  843. drm_dp_link_power_down(ctrl->drm_aux, &ctrl->dp_link);
  844. edp_ctrl_irq_enable(ctrl, 0);
  845. edp_ctrl_link_enable(ctrl, 0);
  846. edp_ctrl_phy_aux_enable(ctrl, 0);
  847. ctrl->power_on = false;
  848. unlock_ret:
  849. mutex_unlock(&ctrl->dev_mutex);
  850. }
  851. irqreturn_t msm_edp_ctrl_irq(struct edp_ctrl *ctrl)
  852. {
  853. u32 isr1, isr2, mask1, mask2;
  854. u32 ack;
  855. DBG("");
  856. spin_lock(&ctrl->irq_lock);
  857. isr1 = edp_read(ctrl->base + REG_EDP_INTERRUPT_REG_1);
  858. isr2 = edp_read(ctrl->base + REG_EDP_INTERRUPT_REG_2);
  859. mask1 = isr1 & EDP_INTR_MASK1;
  860. mask2 = isr2 & EDP_INTR_MASK2;
  861. isr1 &= ~mask1; /* remove masks bit */
  862. isr2 &= ~mask2;
  863. DBG("isr=%x mask=%x isr2=%x mask2=%x",
  864. isr1, mask1, isr2, mask2);
  865. ack = isr1 & EDP_INTR_STATUS1;
  866. ack <<= 1; /* ack bits */
  867. ack |= mask1;
  868. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_1, ack);
  869. ack = isr2 & EDP_INTR_STATUS2;
  870. ack <<= 1; /* ack bits */
  871. ack |= mask2;
  872. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_2, ack);
  873. spin_unlock(&ctrl->irq_lock);
  874. if (isr1 & EDP_INTERRUPT_REG_1_HPD)
  875. DBG("edp_hpd");
  876. if (isr2 & EDP_INTERRUPT_REG_2_READY_FOR_VIDEO)
  877. DBG("edp_video_ready");
  878. if (isr2 & EDP_INTERRUPT_REG_2_IDLE_PATTERNs_SENT) {
  879. DBG("idle_patterns_sent");
  880. complete(&ctrl->idle_comp);
  881. }
  882. msm_edp_aux_irq(ctrl->aux, isr1);
  883. return IRQ_HANDLED;
  884. }
  885. void msm_edp_ctrl_power(struct edp_ctrl *ctrl, bool on)
  886. {
  887. if (on)
  888. queue_work(ctrl->workqueue, &ctrl->on_work);
  889. else
  890. queue_work(ctrl->workqueue, &ctrl->off_work);
  891. }
  892. int msm_edp_ctrl_init(struct msm_edp *edp)
  893. {
  894. struct edp_ctrl *ctrl = NULL;
  895. struct device *dev = &edp->pdev->dev;
  896. int ret;
  897. if (!edp) {
  898. pr_err("%s: edp is NULL!\n", __func__);
  899. return -EINVAL;
  900. }
  901. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  902. if (!ctrl)
  903. return -ENOMEM;
  904. edp->ctrl = ctrl;
  905. ctrl->pdev = edp->pdev;
  906. ctrl->base = msm_ioremap(ctrl->pdev, "edp", "eDP");
  907. if (IS_ERR(ctrl->base))
  908. return PTR_ERR(ctrl->base);
  909. /* Get regulator, clock, gpio, pwm */
  910. ret = edp_regulator_init(ctrl);
  911. if (ret) {
  912. pr_err("%s:regulator init fail\n", __func__);
  913. return ret;
  914. }
  915. ret = edp_clk_init(ctrl);
  916. if (ret) {
  917. pr_err("%s:clk init fail\n", __func__);
  918. return ret;
  919. }
  920. ret = edp_gpio_config(ctrl);
  921. if (ret) {
  922. pr_err("%s:failed to configure GPIOs: %d", __func__, ret);
  923. return ret;
  924. }
  925. /* Init aux and phy */
  926. ctrl->aux = msm_edp_aux_init(dev, ctrl->base, &ctrl->drm_aux);
  927. if (!ctrl->aux || !ctrl->drm_aux) {
  928. pr_err("%s:failed to init aux\n", __func__);
  929. return -ENOMEM;
  930. }
  931. ctrl->phy = msm_edp_phy_init(dev, ctrl->base);
  932. if (!ctrl->phy) {
  933. pr_err("%s:failed to init phy\n", __func__);
  934. ret = -ENOMEM;
  935. goto err_destory_aux;
  936. }
  937. spin_lock_init(&ctrl->irq_lock);
  938. mutex_init(&ctrl->dev_mutex);
  939. init_completion(&ctrl->idle_comp);
  940. /* setup workqueue */
  941. ctrl->workqueue = alloc_ordered_workqueue("edp_drm_work", 0);
  942. INIT_WORK(&ctrl->on_work, edp_ctrl_on_worker);
  943. INIT_WORK(&ctrl->off_work, edp_ctrl_off_worker);
  944. return 0;
  945. err_destory_aux:
  946. msm_edp_aux_destroy(dev, ctrl->aux);
  947. ctrl->aux = NULL;
  948. return ret;
  949. }
  950. void msm_edp_ctrl_destroy(struct edp_ctrl *ctrl)
  951. {
  952. if (!ctrl)
  953. return;
  954. if (ctrl->workqueue) {
  955. flush_workqueue(ctrl->workqueue);
  956. destroy_workqueue(ctrl->workqueue);
  957. ctrl->workqueue = NULL;
  958. }
  959. if (ctrl->aux) {
  960. msm_edp_aux_destroy(&ctrl->pdev->dev, ctrl->aux);
  961. ctrl->aux = NULL;
  962. }
  963. kfree(ctrl->edid);
  964. ctrl->edid = NULL;
  965. mutex_destroy(&ctrl->dev_mutex);
  966. }
  967. bool msm_edp_ctrl_panel_connected(struct edp_ctrl *ctrl)
  968. {
  969. mutex_lock(&ctrl->dev_mutex);
  970. DBG("connect status = %d", ctrl->edp_connected);
  971. if (ctrl->edp_connected) {
  972. mutex_unlock(&ctrl->dev_mutex);
  973. return true;
  974. }
  975. if (!ctrl->power_on) {
  976. edp_ctrl_phy_aux_enable(ctrl, 1);
  977. edp_ctrl_irq_enable(ctrl, 1);
  978. }
  979. if (drm_dp_dpcd_read(ctrl->drm_aux, DP_DPCD_REV, ctrl->dpcd,
  980. DP_RECEIVER_CAP_SIZE) < DP_RECEIVER_CAP_SIZE) {
  981. pr_err("%s: AUX channel is NOT ready\n", __func__);
  982. memset(ctrl->dpcd, 0, DP_RECEIVER_CAP_SIZE);
  983. } else {
  984. ctrl->edp_connected = true;
  985. }
  986. if (!ctrl->power_on) {
  987. edp_ctrl_irq_enable(ctrl, 0);
  988. edp_ctrl_phy_aux_enable(ctrl, 0);
  989. }
  990. DBG("exit: connect status=%d", ctrl->edp_connected);
  991. mutex_unlock(&ctrl->dev_mutex);
  992. return ctrl->edp_connected;
  993. }
  994. int msm_edp_ctrl_get_panel_info(struct edp_ctrl *ctrl,
  995. struct drm_connector *connector, struct edid **edid)
  996. {
  997. int ret = 0;
  998. mutex_lock(&ctrl->dev_mutex);
  999. if (ctrl->edid) {
  1000. if (edid) {
  1001. DBG("Just return edid buffer");
  1002. *edid = ctrl->edid;
  1003. }
  1004. goto unlock_ret;
  1005. }
  1006. if (!ctrl->power_on) {
  1007. edp_ctrl_phy_aux_enable(ctrl, 1);
  1008. edp_ctrl_irq_enable(ctrl, 1);
  1009. }
  1010. ret = drm_dp_link_probe(ctrl->drm_aux, &ctrl->dp_link);
  1011. if (ret) {
  1012. pr_err("%s: read dpcd cap failed, %d\n", __func__, ret);
  1013. goto disable_ret;
  1014. }
  1015. /* Initialize link rate as panel max link rate */
  1016. ctrl->link_rate = drm_dp_link_rate_to_bw_code(ctrl->dp_link.rate);
  1017. ctrl->edid = drm_get_edid(connector, &ctrl->drm_aux->ddc);
  1018. if (!ctrl->edid) {
  1019. pr_err("%s: edid read fail\n", __func__);
  1020. goto disable_ret;
  1021. }
  1022. if (edid)
  1023. *edid = ctrl->edid;
  1024. disable_ret:
  1025. if (!ctrl->power_on) {
  1026. edp_ctrl_irq_enable(ctrl, 0);
  1027. edp_ctrl_phy_aux_enable(ctrl, 0);
  1028. }
  1029. unlock_ret:
  1030. mutex_unlock(&ctrl->dev_mutex);
  1031. return ret;
  1032. }
  1033. int msm_edp_ctrl_timing_cfg(struct edp_ctrl *ctrl,
  1034. const struct drm_display_mode *mode,
  1035. const struct drm_display_info *info)
  1036. {
  1037. u32 hstart_from_sync, vstart_from_sync;
  1038. u32 data;
  1039. int ret = 0;
  1040. mutex_lock(&ctrl->dev_mutex);
  1041. /*
  1042. * Need to keep color depth, pixel rate and
  1043. * interlaced information in ctrl context
  1044. */
  1045. ctrl->color_depth = info->bpc;
  1046. ctrl->pixel_rate = mode->clock;
  1047. ctrl->interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  1048. /* Fill initial link config based on passed in timing */
  1049. edp_fill_link_cfg(ctrl);
  1050. if (edp_clk_enable(ctrl, EDP_CLK_MASK_AHB)) {
  1051. pr_err("%s, fail to prepare enable ahb clk\n", __func__);
  1052. ret = -EINVAL;
  1053. goto unlock_ret;
  1054. }
  1055. edp_clock_synchrous(ctrl, 1);
  1056. /* Configure eDP timing to HW */
  1057. edp_write(ctrl->base + REG_EDP_TOTAL_HOR_VER,
  1058. EDP_TOTAL_HOR_VER_HORIZ(mode->htotal) |
  1059. EDP_TOTAL_HOR_VER_VERT(mode->vtotal));
  1060. vstart_from_sync = mode->vtotal - mode->vsync_start;
  1061. hstart_from_sync = mode->htotal - mode->hsync_start;
  1062. edp_write(ctrl->base + REG_EDP_START_HOR_VER_FROM_SYNC,
  1063. EDP_START_HOR_VER_FROM_SYNC_HORIZ(hstart_from_sync) |
  1064. EDP_START_HOR_VER_FROM_SYNC_VERT(vstart_from_sync));
  1065. data = EDP_HSYNC_VSYNC_WIDTH_POLARITY_VERT(
  1066. mode->vsync_end - mode->vsync_start);
  1067. data |= EDP_HSYNC_VSYNC_WIDTH_POLARITY_HORIZ(
  1068. mode->hsync_end - mode->hsync_start);
  1069. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1070. data |= EDP_HSYNC_VSYNC_WIDTH_POLARITY_NVSYNC;
  1071. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1072. data |= EDP_HSYNC_VSYNC_WIDTH_POLARITY_NHSYNC;
  1073. edp_write(ctrl->base + REG_EDP_HSYNC_VSYNC_WIDTH_POLARITY, data);
  1074. edp_write(ctrl->base + REG_EDP_ACTIVE_HOR_VER,
  1075. EDP_ACTIVE_HOR_VER_HORIZ(mode->hdisplay) |
  1076. EDP_ACTIVE_HOR_VER_VERT(mode->vdisplay));
  1077. edp_clk_disable(ctrl, EDP_CLK_MASK_AHB);
  1078. unlock_ret:
  1079. mutex_unlock(&ctrl->dev_mutex);
  1080. return ret;
  1081. }
  1082. bool msm_edp_ctrl_pixel_clock_valid(struct edp_ctrl *ctrl,
  1083. u32 pixel_rate, u32 *pm, u32 *pn)
  1084. {
  1085. const struct edp_pixel_clk_div *divs;
  1086. u32 err = 1; /* 1% error tolerance */
  1087. u32 clk_err;
  1088. int i;
  1089. if (ctrl->link_rate == DP_LINK_BW_1_62) {
  1090. divs = clk_divs[0];
  1091. } else if (ctrl->link_rate == DP_LINK_BW_2_7) {
  1092. divs = clk_divs[1];
  1093. } else {
  1094. pr_err("%s: Invalid link rate,%d\n", __func__, ctrl->link_rate);
  1095. return false;
  1096. }
  1097. for (i = 0; i < EDP_PIXEL_CLK_NUM; i++) {
  1098. clk_err = abs(divs[i].rate - pixel_rate);
  1099. if ((divs[i].rate * err / 100) >= clk_err) {
  1100. if (pm)
  1101. *pm = divs[i].m;
  1102. if (pn)
  1103. *pn = divs[i].n;
  1104. return true;
  1105. }
  1106. }
  1107. DBG("pixel clock %d(kHz) not supported", pixel_rate);
  1108. return false;
  1109. }