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