edp_ctrl.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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");
  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. ret = gpiod_direction_input(ctrl->panel_hpd_gpio);
  331. if (ret) {
  332. pr_err("%s: Set direction for hpd failed, %d\n", __func__, ret);
  333. return ret;
  334. }
  335. ctrl->panel_en_gpio = devm_gpiod_get(dev, "panel-en");
  336. if (IS_ERR(ctrl->panel_en_gpio)) {
  337. ret = PTR_ERR(ctrl->panel_en_gpio);
  338. ctrl->panel_en_gpio = NULL;
  339. pr_err("%s: cannot get panel-en-gpios, %d\n", __func__, ret);
  340. return ret;
  341. }
  342. ret = gpiod_direction_output(ctrl->panel_en_gpio, 0);
  343. if (ret) {
  344. pr_err("%s: Set direction for panel_en failed, %d\n",
  345. __func__, ret);
  346. return ret;
  347. }
  348. DBG("gpio on");
  349. return 0;
  350. }
  351. static void edp_ctrl_irq_enable(struct edp_ctrl *ctrl, int enable)
  352. {
  353. unsigned long flags;
  354. DBG("%d", enable);
  355. spin_lock_irqsave(&ctrl->irq_lock, flags);
  356. if (enable) {
  357. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_1, EDP_INTR_MASK1);
  358. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_2, EDP_INTR_MASK2);
  359. } else {
  360. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_1, 0x0);
  361. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_2, 0x0);
  362. }
  363. spin_unlock_irqrestore(&ctrl->irq_lock, flags);
  364. DBG("exit");
  365. }
  366. static void edp_fill_link_cfg(struct edp_ctrl *ctrl)
  367. {
  368. u32 prate;
  369. u32 lrate;
  370. u32 bpp;
  371. u8 max_lane = ctrl->dp_link.num_lanes;
  372. u8 lane;
  373. prate = ctrl->pixel_rate;
  374. bpp = ctrl->color_depth * 3;
  375. /*
  376. * By default, use the maximum link rate and minimum lane count,
  377. * so that we can do rate down shift during link training.
  378. */
  379. ctrl->link_rate = drm_dp_link_rate_to_bw_code(ctrl->dp_link.rate);
  380. prate *= bpp;
  381. prate /= 8; /* in kByte */
  382. lrate = 270000; /* in kHz */
  383. lrate *= ctrl->link_rate;
  384. lrate /= 10; /* in kByte, 10 bits --> 8 bits */
  385. for (lane = 1; lane <= max_lane; lane <<= 1) {
  386. if (lrate >= prate)
  387. break;
  388. lrate <<= 1;
  389. }
  390. ctrl->lane_cnt = lane;
  391. DBG("rate=%d lane=%d", ctrl->link_rate, ctrl->lane_cnt);
  392. }
  393. static void edp_config_ctrl(struct edp_ctrl *ctrl)
  394. {
  395. u32 data;
  396. enum edp_color_depth depth;
  397. data = EDP_CONFIGURATION_CTRL_LANES(ctrl->lane_cnt - 1);
  398. if (ctrl->dp_link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  399. data |= EDP_CONFIGURATION_CTRL_ENHANCED_FRAMING;
  400. depth = EDP_6BIT;
  401. if (ctrl->color_depth == 8)
  402. depth = EDP_8BIT;
  403. data |= EDP_CONFIGURATION_CTRL_COLOR(depth);
  404. if (!ctrl->interlaced) /* progressive */
  405. data |= EDP_CONFIGURATION_CTRL_PROGRESSIVE;
  406. data |= (EDP_CONFIGURATION_CTRL_SYNC_CLK |
  407. EDP_CONFIGURATION_CTRL_STATIC_MVID);
  408. edp_write(ctrl->base + REG_EDP_CONFIGURATION_CTRL, data);
  409. }
  410. static void edp_state_ctrl(struct edp_ctrl *ctrl, u32 state)
  411. {
  412. edp_write(ctrl->base + REG_EDP_STATE_CTRL, state);
  413. /* Make sure H/W status is set */
  414. wmb();
  415. }
  416. static int edp_lane_set_write(struct edp_ctrl *ctrl,
  417. u8 voltage_level, u8 pre_emphasis_level)
  418. {
  419. int i;
  420. u8 buf[4];
  421. if (voltage_level >= DPCD_LINK_VOLTAGE_MAX)
  422. voltage_level |= 0x04;
  423. if (pre_emphasis_level >= DPCD_LINK_PRE_EMPHASIS_MAX)
  424. pre_emphasis_level |= 0x04;
  425. pre_emphasis_level <<= 3;
  426. for (i = 0; i < 4; i++)
  427. buf[i] = voltage_level | pre_emphasis_level;
  428. DBG("%s: p|v=0x%x", __func__, voltage_level | pre_emphasis_level);
  429. if (drm_dp_dpcd_write(ctrl->drm_aux, 0x103, buf, 4) < 4) {
  430. pr_err("%s: Set sw/pe to panel failed\n", __func__);
  431. return -ENOLINK;
  432. }
  433. return 0;
  434. }
  435. static int edp_train_pattern_set_write(struct edp_ctrl *ctrl, u8 pattern)
  436. {
  437. u8 p = pattern;
  438. DBG("pattern=%x", p);
  439. if (drm_dp_dpcd_write(ctrl->drm_aux,
  440. DP_TRAINING_PATTERN_SET, &p, 1) < 1) {
  441. pr_err("%s: Set training pattern to panel failed\n", __func__);
  442. return -ENOLINK;
  443. }
  444. return 0;
  445. }
  446. static void edp_sink_train_set_adjust(struct edp_ctrl *ctrl,
  447. const u8 *link_status)
  448. {
  449. int i;
  450. u8 max = 0;
  451. u8 data;
  452. /* use the max level across lanes */
  453. for (i = 0; i < ctrl->lane_cnt; i++) {
  454. data = drm_dp_get_adjust_request_voltage(link_status, i);
  455. DBG("lane=%d req_voltage_swing=0x%x", i, data);
  456. if (max < data)
  457. max = data;
  458. }
  459. ctrl->v_level = max >> DP_TRAIN_VOLTAGE_SWING_SHIFT;
  460. /* use the max level across lanes */
  461. max = 0;
  462. for (i = 0; i < ctrl->lane_cnt; i++) {
  463. data = drm_dp_get_adjust_request_pre_emphasis(link_status, i);
  464. DBG("lane=%d req_pre_emphasis=0x%x", i, data);
  465. if (max < data)
  466. max = data;
  467. }
  468. ctrl->p_level = max >> DP_TRAIN_PRE_EMPHASIS_SHIFT;
  469. DBG("v_level=%d, p_level=%d", ctrl->v_level, ctrl->p_level);
  470. }
  471. static void edp_host_train_set(struct edp_ctrl *ctrl, u32 train)
  472. {
  473. int cnt = 10;
  474. u32 data;
  475. u32 shift = train - 1;
  476. DBG("train=%d", train);
  477. edp_state_ctrl(ctrl, EDP_STATE_CTRL_TRAIN_PATTERN_1 << shift);
  478. while (--cnt) {
  479. data = edp_read(ctrl->base + REG_EDP_MAINLINK_READY);
  480. if (data & (EDP_MAINLINK_READY_TRAIN_PATTERN_1_READY << shift))
  481. break;
  482. }
  483. if (cnt == 0)
  484. pr_err("%s: set link_train=%d failed\n", __func__, train);
  485. }
  486. static const u8 vm_pre_emphasis[4][4] = {
  487. {0x03, 0x06, 0x09, 0x0C}, /* pe0, 0 db */
  488. {0x03, 0x06, 0x09, 0xFF}, /* pe1, 3.5 db */
  489. {0x03, 0x06, 0xFF, 0xFF}, /* pe2, 6.0 db */
  490. {0x03, 0xFF, 0xFF, 0xFF} /* pe3, 9.5 db */
  491. };
  492. /* voltage swing, 0.2v and 1.0v are not support */
  493. static const u8 vm_voltage_swing[4][4] = {
  494. {0x14, 0x18, 0x1A, 0x1E}, /* sw0, 0.4v */
  495. {0x18, 0x1A, 0x1E, 0xFF}, /* sw1, 0.6 v */
  496. {0x1A, 0x1E, 0xFF, 0xFF}, /* sw1, 0.8 v */
  497. {0x1E, 0xFF, 0xFF, 0xFF} /* sw1, 1.2 v, optional */
  498. };
  499. static int edp_voltage_pre_emphasise_set(struct edp_ctrl *ctrl)
  500. {
  501. u32 value0;
  502. u32 value1;
  503. DBG("v=%d p=%d", ctrl->v_level, ctrl->p_level);
  504. value0 = vm_pre_emphasis[(int)(ctrl->v_level)][(int)(ctrl->p_level)];
  505. value1 = vm_voltage_swing[(int)(ctrl->v_level)][(int)(ctrl->p_level)];
  506. /* Configure host and panel only if both values are allowed */
  507. if (value0 != 0xFF && value1 != 0xFF) {
  508. msm_edp_phy_vm_pe_cfg(ctrl->phy, value0, value1);
  509. return edp_lane_set_write(ctrl, ctrl->v_level, ctrl->p_level);
  510. }
  511. return -EINVAL;
  512. }
  513. static int edp_start_link_train_1(struct edp_ctrl *ctrl)
  514. {
  515. u8 link_status[DP_LINK_STATUS_SIZE];
  516. u8 old_v_level;
  517. int tries;
  518. int ret;
  519. int rlen;
  520. DBG("");
  521. edp_host_train_set(ctrl, DP_TRAINING_PATTERN_1);
  522. ret = edp_voltage_pre_emphasise_set(ctrl);
  523. if (ret)
  524. return ret;
  525. ret = edp_train_pattern_set_write(ctrl,
  526. DP_TRAINING_PATTERN_1 | DP_RECOVERED_CLOCK_OUT_EN);
  527. if (ret)
  528. return ret;
  529. tries = 0;
  530. old_v_level = ctrl->v_level;
  531. while (1) {
  532. drm_dp_link_train_clock_recovery_delay(ctrl->dpcd);
  533. rlen = drm_dp_dpcd_read_link_status(ctrl->drm_aux, link_status);
  534. if (rlen < DP_LINK_STATUS_SIZE) {
  535. pr_err("%s: read link status failed\n", __func__);
  536. return -ENOLINK;
  537. }
  538. if (drm_dp_clock_recovery_ok(link_status, ctrl->lane_cnt)) {
  539. ret = 0;
  540. break;
  541. }
  542. if (ctrl->v_level == DPCD_LINK_VOLTAGE_MAX) {
  543. ret = -1;
  544. break;
  545. }
  546. if (old_v_level == ctrl->v_level) {
  547. tries++;
  548. if (tries >= 5) {
  549. ret = -1;
  550. break;
  551. }
  552. } else {
  553. tries = 0;
  554. old_v_level = ctrl->v_level;
  555. }
  556. edp_sink_train_set_adjust(ctrl, link_status);
  557. ret = edp_voltage_pre_emphasise_set(ctrl);
  558. if (ret)
  559. return ret;
  560. }
  561. return ret;
  562. }
  563. static int edp_start_link_train_2(struct edp_ctrl *ctrl)
  564. {
  565. u8 link_status[DP_LINK_STATUS_SIZE];
  566. int tries = 0;
  567. int ret;
  568. int rlen;
  569. DBG("");
  570. edp_host_train_set(ctrl, DP_TRAINING_PATTERN_2);
  571. ret = edp_voltage_pre_emphasise_set(ctrl);
  572. if (ret)
  573. return ret;
  574. ret = edp_train_pattern_set_write(ctrl,
  575. DP_TRAINING_PATTERN_2 | DP_RECOVERED_CLOCK_OUT_EN);
  576. if (ret)
  577. return ret;
  578. while (1) {
  579. drm_dp_link_train_channel_eq_delay(ctrl->dpcd);
  580. rlen = drm_dp_dpcd_read_link_status(ctrl->drm_aux, link_status);
  581. if (rlen < DP_LINK_STATUS_SIZE) {
  582. pr_err("%s: read link status failed\n", __func__);
  583. return -ENOLINK;
  584. }
  585. if (drm_dp_channel_eq_ok(link_status, ctrl->lane_cnt)) {
  586. ret = 0;
  587. break;
  588. }
  589. tries++;
  590. if (tries > 10) {
  591. ret = -1;
  592. break;
  593. }
  594. edp_sink_train_set_adjust(ctrl, link_status);
  595. ret = edp_voltage_pre_emphasise_set(ctrl);
  596. if (ret)
  597. return ret;
  598. }
  599. return ret;
  600. }
  601. static int edp_link_rate_down_shift(struct edp_ctrl *ctrl)
  602. {
  603. u32 prate, lrate, bpp;
  604. u8 rate, lane, max_lane;
  605. int changed = 0;
  606. rate = ctrl->link_rate;
  607. lane = ctrl->lane_cnt;
  608. max_lane = ctrl->dp_link.num_lanes;
  609. bpp = ctrl->color_depth * 3;
  610. prate = ctrl->pixel_rate;
  611. prate *= bpp;
  612. prate /= 8; /* in kByte */
  613. if (rate > DP_LINK_BW_1_62 && rate <= EDP_LINK_BW_MAX) {
  614. rate -= 4; /* reduce rate */
  615. changed++;
  616. }
  617. if (changed) {
  618. if (lane >= 1 && lane < max_lane)
  619. lane <<= 1; /* increase lane */
  620. lrate = 270000; /* in kHz */
  621. lrate *= rate;
  622. lrate /= 10; /* kByte, 10 bits --> 8 bits */
  623. lrate *= lane;
  624. DBG("new lrate=%u prate=%u(kHz) rate=%d lane=%d p=%u b=%d",
  625. lrate, prate, rate, lane,
  626. ctrl->pixel_rate,
  627. bpp);
  628. if (lrate > prate) {
  629. ctrl->link_rate = rate;
  630. ctrl->lane_cnt = lane;
  631. DBG("new rate=%d %d", rate, lane);
  632. return 0;
  633. }
  634. }
  635. return -EINVAL;
  636. }
  637. static int edp_clear_training_pattern(struct edp_ctrl *ctrl)
  638. {
  639. int ret;
  640. ret = edp_train_pattern_set_write(ctrl, 0);
  641. drm_dp_link_train_channel_eq_delay(ctrl->dpcd);
  642. return ret;
  643. }
  644. static int edp_do_link_train(struct edp_ctrl *ctrl)
  645. {
  646. int ret;
  647. struct drm_dp_link dp_link;
  648. DBG("");
  649. /*
  650. * Set the current link rate and lane cnt to panel. They may have been
  651. * adjusted and the values are different from them in DPCD CAP
  652. */
  653. dp_link.num_lanes = ctrl->lane_cnt;
  654. dp_link.rate = drm_dp_bw_code_to_link_rate(ctrl->link_rate);
  655. dp_link.capabilities = ctrl->dp_link.capabilities;
  656. if (drm_dp_link_configure(ctrl->drm_aux, &dp_link) < 0)
  657. return EDP_TRAIN_FAIL;
  658. ctrl->v_level = 0; /* start from default level */
  659. ctrl->p_level = 0;
  660. edp_state_ctrl(ctrl, 0);
  661. if (edp_clear_training_pattern(ctrl))
  662. return EDP_TRAIN_FAIL;
  663. ret = edp_start_link_train_1(ctrl);
  664. if (ret < 0) {
  665. if (edp_link_rate_down_shift(ctrl) == 0) {
  666. DBG("link reconfig");
  667. ret = EDP_TRAIN_RECONFIG;
  668. goto clear;
  669. } else {
  670. pr_err("%s: Training 1 failed", __func__);
  671. ret = EDP_TRAIN_FAIL;
  672. goto clear;
  673. }
  674. }
  675. DBG("Training 1 completed successfully");
  676. edp_state_ctrl(ctrl, 0);
  677. if (edp_clear_training_pattern(ctrl))
  678. return EDP_TRAIN_FAIL;
  679. ret = edp_start_link_train_2(ctrl);
  680. if (ret < 0) {
  681. if (edp_link_rate_down_shift(ctrl) == 0) {
  682. DBG("link reconfig");
  683. ret = EDP_TRAIN_RECONFIG;
  684. goto clear;
  685. } else {
  686. pr_err("%s: Training 2 failed", __func__);
  687. ret = EDP_TRAIN_FAIL;
  688. goto clear;
  689. }
  690. }
  691. DBG("Training 2 completed successfully");
  692. edp_state_ctrl(ctrl, EDP_STATE_CTRL_SEND_VIDEO);
  693. clear:
  694. edp_clear_training_pattern(ctrl);
  695. return ret;
  696. }
  697. static void edp_clock_synchrous(struct edp_ctrl *ctrl, int sync)
  698. {
  699. u32 data;
  700. enum edp_color_depth depth;
  701. data = edp_read(ctrl->base + REG_EDP_MISC1_MISC0);
  702. if (sync)
  703. data |= EDP_MISC1_MISC0_SYNC;
  704. else
  705. data &= ~EDP_MISC1_MISC0_SYNC;
  706. /* only legacy rgb mode supported */
  707. depth = EDP_6BIT; /* Default */
  708. if (ctrl->color_depth == 8)
  709. depth = EDP_8BIT;
  710. else if (ctrl->color_depth == 10)
  711. depth = EDP_10BIT;
  712. else if (ctrl->color_depth == 12)
  713. depth = EDP_12BIT;
  714. else if (ctrl->color_depth == 16)
  715. depth = EDP_16BIT;
  716. data |= EDP_MISC1_MISC0_COLOR(depth);
  717. edp_write(ctrl->base + REG_EDP_MISC1_MISC0, data);
  718. }
  719. static int edp_sw_mvid_nvid(struct edp_ctrl *ctrl, u32 m, u32 n)
  720. {
  721. u32 n_multi, m_multi = 5;
  722. if (ctrl->link_rate == DP_LINK_BW_1_62) {
  723. n_multi = 1;
  724. } else if (ctrl->link_rate == DP_LINK_BW_2_7) {
  725. n_multi = 2;
  726. } else {
  727. pr_err("%s: Invalid link rate, %d\n", __func__,
  728. ctrl->link_rate);
  729. return -EINVAL;
  730. }
  731. edp_write(ctrl->base + REG_EDP_SOFTWARE_MVID, m * m_multi);
  732. edp_write(ctrl->base + REG_EDP_SOFTWARE_NVID, n * n_multi);
  733. return 0;
  734. }
  735. static void edp_mainlink_ctrl(struct edp_ctrl *ctrl, int enable)
  736. {
  737. u32 data = 0;
  738. edp_write(ctrl->base + REG_EDP_MAINLINK_CTRL, EDP_MAINLINK_CTRL_RESET);
  739. /* Make sure fully reset */
  740. wmb();
  741. usleep_range(500, 1000);
  742. if (enable)
  743. data |= EDP_MAINLINK_CTRL_ENABLE;
  744. edp_write(ctrl->base + REG_EDP_MAINLINK_CTRL, data);
  745. }
  746. static void edp_ctrl_phy_aux_enable(struct edp_ctrl *ctrl, int enable)
  747. {
  748. if (enable) {
  749. edp_regulator_enable(ctrl);
  750. edp_clk_enable(ctrl, EDP_CLK_MASK_AUX_CHAN);
  751. msm_edp_phy_ctrl(ctrl->phy, 1);
  752. msm_edp_aux_ctrl(ctrl->aux, 1);
  753. gpiod_set_value(ctrl->panel_en_gpio, 1);
  754. } else {
  755. gpiod_set_value(ctrl->panel_en_gpio, 0);
  756. msm_edp_aux_ctrl(ctrl->aux, 0);
  757. msm_edp_phy_ctrl(ctrl->phy, 0);
  758. edp_clk_disable(ctrl, EDP_CLK_MASK_AUX_CHAN);
  759. edp_regulator_disable(ctrl);
  760. }
  761. }
  762. static void edp_ctrl_link_enable(struct edp_ctrl *ctrl, int enable)
  763. {
  764. u32 m, n;
  765. if (enable) {
  766. /* Enable link channel clocks */
  767. edp_clk_enable(ctrl, EDP_CLK_MASK_LINK_CHAN);
  768. msm_edp_phy_lane_power_ctrl(ctrl->phy, true, ctrl->lane_cnt);
  769. msm_edp_phy_vm_pe_init(ctrl->phy);
  770. /* Make sure phy is programed */
  771. wmb();
  772. msm_edp_phy_ready(ctrl->phy);
  773. edp_config_ctrl(ctrl);
  774. msm_edp_ctrl_pixel_clock_valid(ctrl, ctrl->pixel_rate, &m, &n);
  775. edp_sw_mvid_nvid(ctrl, m, n);
  776. edp_mainlink_ctrl(ctrl, 1);
  777. } else {
  778. edp_mainlink_ctrl(ctrl, 0);
  779. msm_edp_phy_lane_power_ctrl(ctrl->phy, false, 0);
  780. edp_clk_disable(ctrl, EDP_CLK_MASK_LINK_CHAN);
  781. }
  782. }
  783. static int edp_ctrl_training(struct edp_ctrl *ctrl)
  784. {
  785. int ret;
  786. /* Do link training only when power is on */
  787. if (!ctrl->power_on)
  788. return -EINVAL;
  789. train_start:
  790. ret = edp_do_link_train(ctrl);
  791. if (ret == EDP_TRAIN_RECONFIG) {
  792. /* Re-configure main link */
  793. edp_ctrl_irq_enable(ctrl, 0);
  794. edp_ctrl_link_enable(ctrl, 0);
  795. msm_edp_phy_ctrl(ctrl->phy, 0);
  796. /* Make sure link is fully disabled */
  797. wmb();
  798. usleep_range(500, 1000);
  799. msm_edp_phy_ctrl(ctrl->phy, 1);
  800. edp_ctrl_link_enable(ctrl, 1);
  801. edp_ctrl_irq_enable(ctrl, 1);
  802. goto train_start;
  803. }
  804. return ret;
  805. }
  806. static void edp_ctrl_on_worker(struct work_struct *work)
  807. {
  808. struct edp_ctrl *ctrl = container_of(
  809. work, struct edp_ctrl, on_work);
  810. int ret;
  811. mutex_lock(&ctrl->dev_mutex);
  812. if (ctrl->power_on) {
  813. DBG("already on");
  814. goto unlock_ret;
  815. }
  816. edp_ctrl_phy_aux_enable(ctrl, 1);
  817. edp_ctrl_link_enable(ctrl, 1);
  818. edp_ctrl_irq_enable(ctrl, 1);
  819. ret = drm_dp_link_power_up(ctrl->drm_aux, &ctrl->dp_link);
  820. if (ret)
  821. goto fail;
  822. ctrl->power_on = true;
  823. /* Start link training */
  824. ret = edp_ctrl_training(ctrl);
  825. if (ret != EDP_TRAIN_SUCCESS)
  826. goto fail;
  827. DBG("DONE");
  828. goto unlock_ret;
  829. fail:
  830. edp_ctrl_irq_enable(ctrl, 0);
  831. edp_ctrl_link_enable(ctrl, 0);
  832. edp_ctrl_phy_aux_enable(ctrl, 0);
  833. ctrl->power_on = false;
  834. unlock_ret:
  835. mutex_unlock(&ctrl->dev_mutex);
  836. }
  837. static void edp_ctrl_off_worker(struct work_struct *work)
  838. {
  839. struct edp_ctrl *ctrl = container_of(
  840. work, struct edp_ctrl, off_work);
  841. unsigned long time_left;
  842. mutex_lock(&ctrl->dev_mutex);
  843. if (!ctrl->power_on) {
  844. DBG("already off");
  845. goto unlock_ret;
  846. }
  847. reinit_completion(&ctrl->idle_comp);
  848. edp_state_ctrl(ctrl, EDP_STATE_CTRL_PUSH_IDLE);
  849. time_left = wait_for_completion_timeout(&ctrl->idle_comp,
  850. msecs_to_jiffies(500));
  851. if (!time_left)
  852. DBG("%s: idle pattern timedout\n", __func__);
  853. edp_state_ctrl(ctrl, 0);
  854. drm_dp_link_power_down(ctrl->drm_aux, &ctrl->dp_link);
  855. edp_ctrl_irq_enable(ctrl, 0);
  856. edp_ctrl_link_enable(ctrl, 0);
  857. edp_ctrl_phy_aux_enable(ctrl, 0);
  858. ctrl->power_on = false;
  859. unlock_ret:
  860. mutex_unlock(&ctrl->dev_mutex);
  861. }
  862. irqreturn_t msm_edp_ctrl_irq(struct edp_ctrl *ctrl)
  863. {
  864. u32 isr1, isr2, mask1, mask2;
  865. u32 ack;
  866. DBG("");
  867. spin_lock(&ctrl->irq_lock);
  868. isr1 = edp_read(ctrl->base + REG_EDP_INTERRUPT_REG_1);
  869. isr2 = edp_read(ctrl->base + REG_EDP_INTERRUPT_REG_2);
  870. mask1 = isr1 & EDP_INTR_MASK1;
  871. mask2 = isr2 & EDP_INTR_MASK2;
  872. isr1 &= ~mask1; /* remove masks bit */
  873. isr2 &= ~mask2;
  874. DBG("isr=%x mask=%x isr2=%x mask2=%x",
  875. isr1, mask1, isr2, mask2);
  876. ack = isr1 & EDP_INTR_STATUS1;
  877. ack <<= 1; /* ack bits */
  878. ack |= mask1;
  879. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_1, ack);
  880. ack = isr2 & EDP_INTR_STATUS2;
  881. ack <<= 1; /* ack bits */
  882. ack |= mask2;
  883. edp_write(ctrl->base + REG_EDP_INTERRUPT_REG_2, ack);
  884. spin_unlock(&ctrl->irq_lock);
  885. if (isr1 & EDP_INTERRUPT_REG_1_HPD)
  886. DBG("edp_hpd");
  887. if (isr2 & EDP_INTERRUPT_REG_2_READY_FOR_VIDEO)
  888. DBG("edp_video_ready");
  889. if (isr2 & EDP_INTERRUPT_REG_2_IDLE_PATTERNs_SENT) {
  890. DBG("idle_patterns_sent");
  891. complete(&ctrl->idle_comp);
  892. }
  893. msm_edp_aux_irq(ctrl->aux, isr1);
  894. return IRQ_HANDLED;
  895. }
  896. void msm_edp_ctrl_power(struct edp_ctrl *ctrl, bool on)
  897. {
  898. if (on)
  899. queue_work(ctrl->workqueue, &ctrl->on_work);
  900. else
  901. queue_work(ctrl->workqueue, &ctrl->off_work);
  902. }
  903. int msm_edp_ctrl_init(struct msm_edp *edp)
  904. {
  905. struct edp_ctrl *ctrl = NULL;
  906. struct device *dev = &edp->pdev->dev;
  907. int ret;
  908. if (!edp) {
  909. pr_err("%s: edp is NULL!\n", __func__);
  910. return -EINVAL;
  911. }
  912. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  913. if (!ctrl)
  914. return -ENOMEM;
  915. edp->ctrl = ctrl;
  916. ctrl->pdev = edp->pdev;
  917. ctrl->base = msm_ioremap(ctrl->pdev, "edp", "eDP");
  918. if (IS_ERR(ctrl->base))
  919. return PTR_ERR(ctrl->base);
  920. /* Get regulator, clock, gpio, pwm */
  921. ret = edp_regulator_init(ctrl);
  922. if (ret) {
  923. pr_err("%s:regulator init fail\n", __func__);
  924. return ret;
  925. }
  926. ret = edp_clk_init(ctrl);
  927. if (ret) {
  928. pr_err("%s:clk init fail\n", __func__);
  929. return ret;
  930. }
  931. ret = edp_gpio_config(ctrl);
  932. if (ret) {
  933. pr_err("%s:failed to configure GPIOs: %d", __func__, ret);
  934. return ret;
  935. }
  936. /* Init aux and phy */
  937. ctrl->aux = msm_edp_aux_init(dev, ctrl->base, &ctrl->drm_aux);
  938. if (!ctrl->aux || !ctrl->drm_aux) {
  939. pr_err("%s:failed to init aux\n", __func__);
  940. return -ENOMEM;
  941. }
  942. ctrl->phy = msm_edp_phy_init(dev, ctrl->base);
  943. if (!ctrl->phy) {
  944. pr_err("%s:failed to init phy\n", __func__);
  945. ret = -ENOMEM;
  946. goto err_destory_aux;
  947. }
  948. spin_lock_init(&ctrl->irq_lock);
  949. mutex_init(&ctrl->dev_mutex);
  950. init_completion(&ctrl->idle_comp);
  951. /* setup workqueue */
  952. ctrl->workqueue = alloc_ordered_workqueue("edp_drm_work", 0);
  953. INIT_WORK(&ctrl->on_work, edp_ctrl_on_worker);
  954. INIT_WORK(&ctrl->off_work, edp_ctrl_off_worker);
  955. return 0;
  956. err_destory_aux:
  957. msm_edp_aux_destroy(dev, ctrl->aux);
  958. ctrl->aux = NULL;
  959. return ret;
  960. }
  961. void msm_edp_ctrl_destroy(struct edp_ctrl *ctrl)
  962. {
  963. if (!ctrl)
  964. return;
  965. if (ctrl->workqueue) {
  966. flush_workqueue(ctrl->workqueue);
  967. destroy_workqueue(ctrl->workqueue);
  968. ctrl->workqueue = NULL;
  969. }
  970. if (ctrl->aux) {
  971. msm_edp_aux_destroy(&ctrl->pdev->dev, ctrl->aux);
  972. ctrl->aux = NULL;
  973. }
  974. kfree(ctrl->edid);
  975. ctrl->edid = NULL;
  976. mutex_destroy(&ctrl->dev_mutex);
  977. }
  978. bool msm_edp_ctrl_panel_connected(struct edp_ctrl *ctrl)
  979. {
  980. mutex_lock(&ctrl->dev_mutex);
  981. DBG("connect status = %d", ctrl->edp_connected);
  982. if (ctrl->edp_connected) {
  983. mutex_unlock(&ctrl->dev_mutex);
  984. return true;
  985. }
  986. if (!ctrl->power_on) {
  987. edp_ctrl_phy_aux_enable(ctrl, 1);
  988. edp_ctrl_irq_enable(ctrl, 1);
  989. }
  990. if (drm_dp_dpcd_read(ctrl->drm_aux, DP_DPCD_REV, ctrl->dpcd,
  991. DP_RECEIVER_CAP_SIZE) < DP_RECEIVER_CAP_SIZE) {
  992. pr_err("%s: AUX channel is NOT ready\n", __func__);
  993. memset(ctrl->dpcd, 0, DP_RECEIVER_CAP_SIZE);
  994. } else {
  995. ctrl->edp_connected = true;
  996. }
  997. if (!ctrl->power_on) {
  998. edp_ctrl_irq_enable(ctrl, 0);
  999. edp_ctrl_phy_aux_enable(ctrl, 0);
  1000. }
  1001. DBG("exit: connect status=%d", ctrl->edp_connected);
  1002. mutex_unlock(&ctrl->dev_mutex);
  1003. return ctrl->edp_connected;
  1004. }
  1005. int msm_edp_ctrl_get_panel_info(struct edp_ctrl *ctrl,
  1006. struct drm_connector *connector, struct edid **edid)
  1007. {
  1008. int ret = 0;
  1009. mutex_lock(&ctrl->dev_mutex);
  1010. if (ctrl->edid) {
  1011. if (edid) {
  1012. DBG("Just return edid buffer");
  1013. *edid = ctrl->edid;
  1014. }
  1015. goto unlock_ret;
  1016. }
  1017. if (!ctrl->power_on) {
  1018. edp_ctrl_phy_aux_enable(ctrl, 1);
  1019. edp_ctrl_irq_enable(ctrl, 1);
  1020. }
  1021. ret = drm_dp_link_probe(ctrl->drm_aux, &ctrl->dp_link);
  1022. if (ret) {
  1023. pr_err("%s: read dpcd cap failed, %d\n", __func__, ret);
  1024. goto disable_ret;
  1025. }
  1026. /* Initialize link rate as panel max link rate */
  1027. ctrl->link_rate = drm_dp_link_rate_to_bw_code(ctrl->dp_link.rate);
  1028. ctrl->edid = drm_get_edid(connector, &ctrl->drm_aux->ddc);
  1029. if (!ctrl->edid) {
  1030. pr_err("%s: edid read fail\n", __func__);
  1031. goto disable_ret;
  1032. }
  1033. if (edid)
  1034. *edid = ctrl->edid;
  1035. disable_ret:
  1036. if (!ctrl->power_on) {
  1037. edp_ctrl_irq_enable(ctrl, 0);
  1038. edp_ctrl_phy_aux_enable(ctrl, 0);
  1039. }
  1040. unlock_ret:
  1041. mutex_unlock(&ctrl->dev_mutex);
  1042. return ret;
  1043. }
  1044. int msm_edp_ctrl_timing_cfg(struct edp_ctrl *ctrl,
  1045. const struct drm_display_mode *mode,
  1046. const struct drm_display_info *info)
  1047. {
  1048. u32 hstart_from_sync, vstart_from_sync;
  1049. u32 data;
  1050. int ret = 0;
  1051. mutex_lock(&ctrl->dev_mutex);
  1052. /*
  1053. * Need to keep color depth, pixel rate and
  1054. * interlaced information in ctrl context
  1055. */
  1056. ctrl->color_depth = info->bpc;
  1057. ctrl->pixel_rate = mode->clock;
  1058. ctrl->interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  1059. /* Fill initial link config based on passed in timing */
  1060. edp_fill_link_cfg(ctrl);
  1061. if (edp_clk_enable(ctrl, EDP_CLK_MASK_AHB)) {
  1062. pr_err("%s, fail to prepare enable ahb clk\n", __func__);
  1063. ret = -EINVAL;
  1064. goto unlock_ret;
  1065. }
  1066. edp_clock_synchrous(ctrl, 1);
  1067. /* Configure eDP timing to HW */
  1068. edp_write(ctrl->base + REG_EDP_TOTAL_HOR_VER,
  1069. EDP_TOTAL_HOR_VER_HORIZ(mode->htotal) |
  1070. EDP_TOTAL_HOR_VER_VERT(mode->vtotal));
  1071. vstart_from_sync = mode->vtotal - mode->vsync_start;
  1072. hstart_from_sync = mode->htotal - mode->hsync_start;
  1073. edp_write(ctrl->base + REG_EDP_START_HOR_VER_FROM_SYNC,
  1074. EDP_START_HOR_VER_FROM_SYNC_HORIZ(hstart_from_sync) |
  1075. EDP_START_HOR_VER_FROM_SYNC_VERT(vstart_from_sync));
  1076. data = EDP_HSYNC_VSYNC_WIDTH_POLARITY_VERT(
  1077. mode->vsync_end - mode->vsync_start);
  1078. data |= EDP_HSYNC_VSYNC_WIDTH_POLARITY_HORIZ(
  1079. mode->hsync_end - mode->hsync_start);
  1080. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1081. data |= EDP_HSYNC_VSYNC_WIDTH_POLARITY_NVSYNC;
  1082. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1083. data |= EDP_HSYNC_VSYNC_WIDTH_POLARITY_NHSYNC;
  1084. edp_write(ctrl->base + REG_EDP_HSYNC_VSYNC_WIDTH_POLARITY, data);
  1085. edp_write(ctrl->base + REG_EDP_ACTIVE_HOR_VER,
  1086. EDP_ACTIVE_HOR_VER_HORIZ(mode->hdisplay) |
  1087. EDP_ACTIVE_HOR_VER_VERT(mode->vdisplay));
  1088. edp_clk_disable(ctrl, EDP_CLK_MASK_AHB);
  1089. unlock_ret:
  1090. mutex_unlock(&ctrl->dev_mutex);
  1091. return ret;
  1092. }
  1093. bool msm_edp_ctrl_pixel_clock_valid(struct edp_ctrl *ctrl,
  1094. u32 pixel_rate, u32 *pm, u32 *pn)
  1095. {
  1096. const struct edp_pixel_clk_div *divs;
  1097. u32 err = 1; /* 1% error tolerance */
  1098. u32 clk_err;
  1099. int i;
  1100. if (ctrl->link_rate == DP_LINK_BW_1_62) {
  1101. divs = clk_divs[0];
  1102. } else if (ctrl->link_rate == DP_LINK_BW_2_7) {
  1103. divs = clk_divs[1];
  1104. } else {
  1105. pr_err("%s: Invalid link rate,%d\n", __func__, ctrl->link_rate);
  1106. return false;
  1107. }
  1108. for (i = 0; i < EDP_PIXEL_CLK_NUM; i++) {
  1109. clk_err = abs(divs[i].rate - pixel_rate);
  1110. if ((divs[i].rate * err / 100) >= clk_err) {
  1111. if (pm)
  1112. *pm = divs[i].m;
  1113. if (pn)
  1114. *pn = divs[i].n;
  1115. return true;
  1116. }
  1117. }
  1118. DBG("pixel clock %d(kHz) not supported", pixel_rate);
  1119. return false;
  1120. }