edp_ctrl.c 31 KB

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