2 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
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.
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.
14 #include <linux/clk.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/regulator/consumer.h>
19 #include "drm_dp_helper.h"
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 */
29 #define DPCD_LINK_VOLTAGE_MAX 4
30 #define DPCD_LINK_PRE_EMPHASIS_MAX 4
32 #define EDP_LINK_BW_MAX DP_LINK_BW_2_7
34 /* Link training return value */
35 #define EDP_TRAIN_FAIL -1
36 #define EDP_TRAIN_SUCCESS 0
37 #define EDP_TRAIN_RECONFIG 1
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)
49 #define EDP_BACKLIGHT_MAX 255
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)
65 struct platform_device
*pdev
;
70 struct regulator
*vdda_vreg
;
71 struct regulator
*lvl_vreg
;
75 struct clk
*pixel_clk
;
78 struct clk
*mdp_core_clk
;
81 struct gpio_desc
*panel_en_gpio
;
82 struct gpio_desc
*panel_hpd_gpio
;
84 /* completion and mutex */
85 struct completion idle_comp
;
86 struct mutex dev_mutex
; /* To protect device power status */
89 struct work_struct on_work
;
90 struct work_struct off_work
;
91 struct workqueue_struct
*workqueue
;
93 /* Interrupt register lock */
102 struct drm_dp_link dp_link
;
103 struct drm_dp_aux
*drm_aux
;
106 u8 dpcd
[DP_RECEIVER_CAP_SIZE
];
116 u32 pixel_rate
; /* in kHz */
123 struct edp_pixel_clk_div
{
124 u32 rate
; /* in kHz */
129 #define EDP_PIXEL_CLK_NUM 8
130 static 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 */
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 */
153 static int edp_clk_init(struct edp_ctrl
*ctrl
)
155 struct device
*dev
= &ctrl
->pdev
->dev
;
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
;
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
;
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
;
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
;
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
;
202 static int edp_clk_enable(struct edp_ctrl
*ctrl
, u32 clk_mask
)
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
);
211 pr_err("%s: Failed to enable ahb clk\n", __func__
);
215 if (clk_mask
& EDP_CLK_MASK_AUX
) {
216 ret
= clk_set_rate(ctrl
->aux_clk
, 19200000);
218 pr_err("%s: Failed to set rate aux clk\n", __func__
);
221 ret
= clk_prepare_enable(ctrl
->aux_clk
);
223 pr_err("%s: Failed to enable aux clk\n", __func__
);
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);
234 pr_err("%s: Failed to set rate to link clk\n",
239 ret
= clk_prepare_enable(ctrl
->link_clk
);
241 pr_err("%s: Failed to enable link clk\n", __func__
);
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);
251 pr_err("%s: Failed to set rate to pixel clk\n",
256 ret
= clk_prepare_enable(ctrl
->pixel_clk
);
258 pr_err("%s: Failed to enable pixel clk\n", __func__
);
262 if (clk_mask
& EDP_CLK_MASK_MDP_CORE
) {
263 ret
= clk_prepare_enable(ctrl
->mdp_core_clk
);
265 pr_err("%s: Failed to enable mdp core clk\n", __func__
);
273 if (clk_mask
& EDP_CLK_MASK_PIXEL
)
274 clk_disable_unprepare(ctrl
->pixel_clk
);
276 if (clk_mask
& EDP_CLK_MASK_LINK
)
277 clk_disable_unprepare(ctrl
->link_clk
);
279 if (clk_mask
& EDP_CLK_MASK_AUX
)
280 clk_disable_unprepare(ctrl
->aux_clk
);
282 if (clk_mask
& EDP_CLK_MASK_AHB
)
283 clk_disable_unprepare(ctrl
->ahb_clk
);
288 static void edp_clk_disable(struct edp_ctrl
*ctrl
, u32 clk_mask
)
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
);
302 static int edp_regulator_init(struct edp_ctrl
*ctrl
)
304 struct device
*dev
= &ctrl
->pdev
->dev
;
307 ctrl
->vdda_vreg
= devm_regulator_get(dev
, "vdda");
308 if (IS_ERR(ctrl
->vdda_vreg
)) {
309 pr_err("%s: Could not get vdda reg, ret = %ld\n", __func__
,
310 PTR_ERR(ctrl
->vdda_vreg
));
311 ctrl
->vdda_vreg
= NULL
;
312 return PTR_ERR(ctrl
->vdda_vreg
);
314 ctrl
->lvl_vreg
= devm_regulator_get(dev
, "lvl-vdd");
315 if (IS_ERR(ctrl
->lvl_vreg
)) {
316 pr_err("Could not get lvl-vdd reg, %ld",
317 PTR_ERR(ctrl
->lvl_vreg
));
318 ctrl
->lvl_vreg
= NULL
;
319 return PTR_ERR(ctrl
->lvl_vreg
);
325 static int edp_regulator_enable(struct edp_ctrl
*ctrl
)
329 ret
= regulator_set_voltage(ctrl
->vdda_vreg
, VDDA_MIN_UV
, VDDA_MAX_UV
);
331 pr_err("%s:vdda_vreg set_voltage failed, %d\n", __func__
, ret
);
335 ret
= regulator_set_load(ctrl
->vdda_vreg
, VDDA_UA_ON_LOAD
);
337 pr_err("%s: vdda_vreg set regulator mode failed.\n", __func__
);
341 ret
= regulator_enable(ctrl
->vdda_vreg
);
343 pr_err("%s: Failed to enable vdda_vreg regulator.\n", __func__
);
344 goto vdda_enable_fail
;
347 ret
= regulator_enable(ctrl
->lvl_vreg
);
349 pr_err("Failed to enable lvl-vdd reg regulator, %d", ret
);
350 goto lvl_enable_fail
;
357 regulator_disable(ctrl
->vdda_vreg
);
359 regulator_set_load(ctrl
->vdda_vreg
, VDDA_UA_OFF_LOAD
);
364 static void edp_regulator_disable(struct edp_ctrl
*ctrl
)
366 regulator_disable(ctrl
->lvl_vreg
);
367 regulator_disable(ctrl
->vdda_vreg
);
368 regulator_set_load(ctrl
->vdda_vreg
, VDDA_UA_OFF_LOAD
);
371 static int edp_gpio_config(struct edp_ctrl
*ctrl
)
373 struct device
*dev
= &ctrl
->pdev
->dev
;
376 ctrl
->panel_hpd_gpio
= devm_gpiod_get(dev
, "panel-hpd", GPIOD_IN
);
377 if (IS_ERR(ctrl
->panel_hpd_gpio
)) {
378 ret
= PTR_ERR(ctrl
->panel_hpd_gpio
);
379 ctrl
->panel_hpd_gpio
= NULL
;
380 pr_err("%s: cannot get panel-hpd-gpios, %d\n", __func__
, ret
);
384 ctrl
->panel_en_gpio
= devm_gpiod_get(dev
, "panel-en", GPIOD_OUT_LOW
);
385 if (IS_ERR(ctrl
->panel_en_gpio
)) {
386 ret
= PTR_ERR(ctrl
->panel_en_gpio
);
387 ctrl
->panel_en_gpio
= NULL
;
388 pr_err("%s: cannot get panel-en-gpios, %d\n", __func__
, ret
);
397 static void edp_ctrl_irq_enable(struct edp_ctrl
*ctrl
, int enable
)
402 spin_lock_irqsave(&ctrl
->irq_lock
, flags
);
404 edp_write(ctrl
->base
+ REG_EDP_INTERRUPT_REG_1
, EDP_INTR_MASK1
);
405 edp_write(ctrl
->base
+ REG_EDP_INTERRUPT_REG_2
, EDP_INTR_MASK2
);
407 edp_write(ctrl
->base
+ REG_EDP_INTERRUPT_REG_1
, 0x0);
408 edp_write(ctrl
->base
+ REG_EDP_INTERRUPT_REG_2
, 0x0);
410 spin_unlock_irqrestore(&ctrl
->irq_lock
, flags
);
414 static void edp_fill_link_cfg(struct edp_ctrl
*ctrl
)
419 u8 max_lane
= ctrl
->dp_link
.num_lanes
;
422 prate
= ctrl
->pixel_rate
;
423 bpp
= ctrl
->color_depth
* 3;
426 * By default, use the maximum link rate and minimum lane count,
427 * so that we can do rate down shift during link training.
429 ctrl
->link_rate
= drm_dp_link_rate_to_bw_code(ctrl
->dp_link
.rate
);
432 prate
/= 8; /* in kByte */
434 lrate
= 270000; /* in kHz */
435 lrate
*= ctrl
->link_rate
;
436 lrate
/= 10; /* in kByte, 10 bits --> 8 bits */
438 for (lane
= 1; lane
<= max_lane
; lane
<<= 1) {
444 ctrl
->lane_cnt
= lane
;
445 DBG("rate=%d lane=%d", ctrl
->link_rate
, ctrl
->lane_cnt
);
448 static void edp_config_ctrl(struct edp_ctrl
*ctrl
)
451 enum edp_color_depth depth
;
453 data
= EDP_CONFIGURATION_CTRL_LANES(ctrl
->lane_cnt
- 1);
455 if (ctrl
->dp_link
.capabilities
& DP_LINK_CAP_ENHANCED_FRAMING
)
456 data
|= EDP_CONFIGURATION_CTRL_ENHANCED_FRAMING
;
459 if (ctrl
->color_depth
== 8)
462 data
|= EDP_CONFIGURATION_CTRL_COLOR(depth
);
464 if (!ctrl
->interlaced
) /* progressive */
465 data
|= EDP_CONFIGURATION_CTRL_PROGRESSIVE
;
467 data
|= (EDP_CONFIGURATION_CTRL_SYNC_CLK
|
468 EDP_CONFIGURATION_CTRL_STATIC_MVID
);
470 edp_write(ctrl
->base
+ REG_EDP_CONFIGURATION_CTRL
, data
);
473 static void edp_state_ctrl(struct edp_ctrl
*ctrl
, u32 state
)
475 edp_write(ctrl
->base
+ REG_EDP_STATE_CTRL
, state
);
476 /* Make sure H/W status is set */
480 static int edp_lane_set_write(struct edp_ctrl
*ctrl
,
481 u8 voltage_level
, u8 pre_emphasis_level
)
486 if (voltage_level
>= DPCD_LINK_VOLTAGE_MAX
)
487 voltage_level
|= 0x04;
489 if (pre_emphasis_level
>= DPCD_LINK_PRE_EMPHASIS_MAX
)
490 pre_emphasis_level
|= 0x04;
492 pre_emphasis_level
<<= 3;
494 for (i
= 0; i
< 4; i
++)
495 buf
[i
] = voltage_level
| pre_emphasis_level
;
497 DBG("%s: p|v=0x%x", __func__
, voltage_level
| pre_emphasis_level
);
498 if (drm_dp_dpcd_write(ctrl
->drm_aux
, 0x103, buf
, 4) < 4) {
499 pr_err("%s: Set sw/pe to panel failed\n", __func__
);
506 static int edp_train_pattern_set_write(struct edp_ctrl
*ctrl
, u8 pattern
)
510 DBG("pattern=%x", p
);
511 if (drm_dp_dpcd_write(ctrl
->drm_aux
,
512 DP_TRAINING_PATTERN_SET
, &p
, 1) < 1) {
513 pr_err("%s: Set training pattern to panel failed\n", __func__
);
520 static void edp_sink_train_set_adjust(struct edp_ctrl
*ctrl
,
521 const u8
*link_status
)
527 /* use the max level across lanes */
528 for (i
= 0; i
< ctrl
->lane_cnt
; i
++) {
529 data
= drm_dp_get_adjust_request_voltage(link_status
, i
);
530 DBG("lane=%d req_voltage_swing=0x%x", i
, data
);
535 ctrl
->v_level
= max
>> DP_TRAIN_VOLTAGE_SWING_SHIFT
;
537 /* use the max level across lanes */
539 for (i
= 0; i
< ctrl
->lane_cnt
; i
++) {
540 data
= drm_dp_get_adjust_request_pre_emphasis(link_status
, i
);
541 DBG("lane=%d req_pre_emphasis=0x%x", i
, data
);
546 ctrl
->p_level
= max
>> DP_TRAIN_PRE_EMPHASIS_SHIFT
;
547 DBG("v_level=%d, p_level=%d", ctrl
->v_level
, ctrl
->p_level
);
550 static void edp_host_train_set(struct edp_ctrl
*ctrl
, u32 train
)
554 u32 shift
= train
- 1;
556 DBG("train=%d", train
);
558 edp_state_ctrl(ctrl
, EDP_STATE_CTRL_TRAIN_PATTERN_1
<< shift
);
560 data
= edp_read(ctrl
->base
+ REG_EDP_MAINLINK_READY
);
561 if (data
& (EDP_MAINLINK_READY_TRAIN_PATTERN_1_READY
<< shift
))
566 pr_err("%s: set link_train=%d failed\n", __func__
, train
);
569 static const u8 vm_pre_emphasis
[4][4] = {
570 {0x03, 0x06, 0x09, 0x0C}, /* pe0, 0 db */
571 {0x03, 0x06, 0x09, 0xFF}, /* pe1, 3.5 db */
572 {0x03, 0x06, 0xFF, 0xFF}, /* pe2, 6.0 db */
573 {0x03, 0xFF, 0xFF, 0xFF} /* pe3, 9.5 db */
576 /* voltage swing, 0.2v and 1.0v are not support */
577 static const u8 vm_voltage_swing
[4][4] = {
578 {0x14, 0x18, 0x1A, 0x1E}, /* sw0, 0.4v */
579 {0x18, 0x1A, 0x1E, 0xFF}, /* sw1, 0.6 v */
580 {0x1A, 0x1E, 0xFF, 0xFF}, /* sw1, 0.8 v */
581 {0x1E, 0xFF, 0xFF, 0xFF} /* sw1, 1.2 v, optional */
584 static int edp_voltage_pre_emphasise_set(struct edp_ctrl
*ctrl
)
589 DBG("v=%d p=%d", ctrl
->v_level
, ctrl
->p_level
);
591 value0
= vm_pre_emphasis
[(int)(ctrl
->v_level
)][(int)(ctrl
->p_level
)];
592 value1
= vm_voltage_swing
[(int)(ctrl
->v_level
)][(int)(ctrl
->p_level
)];
594 /* Configure host and panel only if both values are allowed */
595 if (value0
!= 0xFF && value1
!= 0xFF) {
596 msm_edp_phy_vm_pe_cfg(ctrl
->phy
, value0
, value1
);
597 return edp_lane_set_write(ctrl
, ctrl
->v_level
, ctrl
->p_level
);
603 static int edp_start_link_train_1(struct edp_ctrl
*ctrl
)
605 u8 link_status
[DP_LINK_STATUS_SIZE
];
613 edp_host_train_set(ctrl
, DP_TRAINING_PATTERN_1
);
614 ret
= edp_voltage_pre_emphasise_set(ctrl
);
617 ret
= edp_train_pattern_set_write(ctrl
,
618 DP_TRAINING_PATTERN_1
| DP_RECOVERED_CLOCK_OUT_EN
);
623 old_v_level
= ctrl
->v_level
;
625 drm_dp_link_train_clock_recovery_delay(ctrl
->dpcd
);
627 rlen
= drm_dp_dpcd_read_link_status(ctrl
->drm_aux
, link_status
);
628 if (rlen
< DP_LINK_STATUS_SIZE
) {
629 pr_err("%s: read link status failed\n", __func__
);
632 if (drm_dp_clock_recovery_ok(link_status
, ctrl
->lane_cnt
)) {
637 if (ctrl
->v_level
== DPCD_LINK_VOLTAGE_MAX
) {
642 if (old_v_level
== ctrl
->v_level
) {
650 old_v_level
= ctrl
->v_level
;
653 edp_sink_train_set_adjust(ctrl
, link_status
);
654 ret
= edp_voltage_pre_emphasise_set(ctrl
);
662 static int edp_start_link_train_2(struct edp_ctrl
*ctrl
)
664 u8 link_status
[DP_LINK_STATUS_SIZE
];
671 edp_host_train_set(ctrl
, DP_TRAINING_PATTERN_2
);
672 ret
= edp_voltage_pre_emphasise_set(ctrl
);
676 ret
= edp_train_pattern_set_write(ctrl
,
677 DP_TRAINING_PATTERN_2
| DP_RECOVERED_CLOCK_OUT_EN
);
682 drm_dp_link_train_channel_eq_delay(ctrl
->dpcd
);
684 rlen
= drm_dp_dpcd_read_link_status(ctrl
->drm_aux
, link_status
);
685 if (rlen
< DP_LINK_STATUS_SIZE
) {
686 pr_err("%s: read link status failed\n", __func__
);
689 if (drm_dp_channel_eq_ok(link_status
, ctrl
->lane_cnt
)) {
700 edp_sink_train_set_adjust(ctrl
, link_status
);
701 ret
= edp_voltage_pre_emphasise_set(ctrl
);
709 static int edp_link_rate_down_shift(struct edp_ctrl
*ctrl
)
711 u32 prate
, lrate
, bpp
;
712 u8 rate
, lane
, max_lane
;
715 rate
= ctrl
->link_rate
;
716 lane
= ctrl
->lane_cnt
;
717 max_lane
= ctrl
->dp_link
.num_lanes
;
719 bpp
= ctrl
->color_depth
* 3;
720 prate
= ctrl
->pixel_rate
;
722 prate
/= 8; /* in kByte */
724 if (rate
> DP_LINK_BW_1_62
&& rate
<= EDP_LINK_BW_MAX
) {
725 rate
-= 4; /* reduce rate */
730 if (lane
>= 1 && lane
< max_lane
)
731 lane
<<= 1; /* increase lane */
733 lrate
= 270000; /* in kHz */
735 lrate
/= 10; /* kByte, 10 bits --> 8 bits */
738 DBG("new lrate=%u prate=%u(kHz) rate=%d lane=%d p=%u b=%d",
739 lrate
, prate
, rate
, lane
,
744 ctrl
->link_rate
= rate
;
745 ctrl
->lane_cnt
= lane
;
746 DBG("new rate=%d %d", rate
, lane
);
754 static int edp_clear_training_pattern(struct edp_ctrl
*ctrl
)
758 ret
= edp_train_pattern_set_write(ctrl
, 0);
760 drm_dp_link_train_channel_eq_delay(ctrl
->dpcd
);
765 static int edp_do_link_train(struct edp_ctrl
*ctrl
)
768 struct drm_dp_link dp_link
;
772 * Set the current link rate and lane cnt to panel. They may have been
773 * adjusted and the values are different from them in DPCD CAP
775 dp_link
.num_lanes
= ctrl
->lane_cnt
;
776 dp_link
.rate
= drm_dp_bw_code_to_link_rate(ctrl
->link_rate
);
777 dp_link
.capabilities
= ctrl
->dp_link
.capabilities
;
778 if (drm_dp_link_configure(ctrl
->drm_aux
, &dp_link
) < 0)
779 return EDP_TRAIN_FAIL
;
781 ctrl
->v_level
= 0; /* start from default level */
784 edp_state_ctrl(ctrl
, 0);
785 if (edp_clear_training_pattern(ctrl
))
786 return EDP_TRAIN_FAIL
;
788 ret
= edp_start_link_train_1(ctrl
);
790 if (edp_link_rate_down_shift(ctrl
) == 0) {
791 DBG("link reconfig");
792 ret
= EDP_TRAIN_RECONFIG
;
795 pr_err("%s: Training 1 failed", __func__
);
796 ret
= EDP_TRAIN_FAIL
;
800 DBG("Training 1 completed successfully");
802 edp_state_ctrl(ctrl
, 0);
803 if (edp_clear_training_pattern(ctrl
))
804 return EDP_TRAIN_FAIL
;
806 ret
= edp_start_link_train_2(ctrl
);
808 if (edp_link_rate_down_shift(ctrl
) == 0) {
809 DBG("link reconfig");
810 ret
= EDP_TRAIN_RECONFIG
;
813 pr_err("%s: Training 2 failed", __func__
);
814 ret
= EDP_TRAIN_FAIL
;
818 DBG("Training 2 completed successfully");
820 edp_state_ctrl(ctrl
, EDP_STATE_CTRL_SEND_VIDEO
);
822 edp_clear_training_pattern(ctrl
);
827 static void edp_clock_synchrous(struct edp_ctrl
*ctrl
, int sync
)
830 enum edp_color_depth depth
;
832 data
= edp_read(ctrl
->base
+ REG_EDP_MISC1_MISC0
);
835 data
|= EDP_MISC1_MISC0_SYNC
;
837 data
&= ~EDP_MISC1_MISC0_SYNC
;
839 /* only legacy rgb mode supported */
840 depth
= EDP_6BIT
; /* Default */
841 if (ctrl
->color_depth
== 8)
843 else if (ctrl
->color_depth
== 10)
845 else if (ctrl
->color_depth
== 12)
847 else if (ctrl
->color_depth
== 16)
850 data
|= EDP_MISC1_MISC0_COLOR(depth
);
852 edp_write(ctrl
->base
+ REG_EDP_MISC1_MISC0
, data
);
855 static int edp_sw_mvid_nvid(struct edp_ctrl
*ctrl
, u32 m
, u32 n
)
857 u32 n_multi
, m_multi
= 5;
859 if (ctrl
->link_rate
== DP_LINK_BW_1_62
) {
861 } else if (ctrl
->link_rate
== DP_LINK_BW_2_7
) {
864 pr_err("%s: Invalid link rate, %d\n", __func__
,
869 edp_write(ctrl
->base
+ REG_EDP_SOFTWARE_MVID
, m
* m_multi
);
870 edp_write(ctrl
->base
+ REG_EDP_SOFTWARE_NVID
, n
* n_multi
);
875 static void edp_mainlink_ctrl(struct edp_ctrl
*ctrl
, int enable
)
879 edp_write(ctrl
->base
+ REG_EDP_MAINLINK_CTRL
, EDP_MAINLINK_CTRL_RESET
);
880 /* Make sure fully reset */
882 usleep_range(500, 1000);
885 data
|= EDP_MAINLINK_CTRL_ENABLE
;
887 edp_write(ctrl
->base
+ REG_EDP_MAINLINK_CTRL
, data
);
890 static void edp_ctrl_phy_aux_enable(struct edp_ctrl
*ctrl
, int enable
)
893 edp_regulator_enable(ctrl
);
894 edp_clk_enable(ctrl
, EDP_CLK_MASK_AUX_CHAN
);
895 msm_edp_phy_ctrl(ctrl
->phy
, 1);
896 msm_edp_aux_ctrl(ctrl
->aux
, 1);
897 gpiod_set_value(ctrl
->panel_en_gpio
, 1);
899 gpiod_set_value(ctrl
->panel_en_gpio
, 0);
900 msm_edp_aux_ctrl(ctrl
->aux
, 0);
901 msm_edp_phy_ctrl(ctrl
->phy
, 0);
902 edp_clk_disable(ctrl
, EDP_CLK_MASK_AUX_CHAN
);
903 edp_regulator_disable(ctrl
);
907 static void edp_ctrl_link_enable(struct edp_ctrl
*ctrl
, int enable
)
912 /* Enable link channel clocks */
913 edp_clk_enable(ctrl
, EDP_CLK_MASK_LINK_CHAN
);
915 msm_edp_phy_lane_power_ctrl(ctrl
->phy
, true, ctrl
->lane_cnt
);
917 msm_edp_phy_vm_pe_init(ctrl
->phy
);
919 /* Make sure phy is programed */
921 msm_edp_phy_ready(ctrl
->phy
);
923 edp_config_ctrl(ctrl
);
924 msm_edp_ctrl_pixel_clock_valid(ctrl
, ctrl
->pixel_rate
, &m
, &n
);
925 edp_sw_mvid_nvid(ctrl
, m
, n
);
926 edp_mainlink_ctrl(ctrl
, 1);
928 edp_mainlink_ctrl(ctrl
, 0);
930 msm_edp_phy_lane_power_ctrl(ctrl
->phy
, false, 0);
931 edp_clk_disable(ctrl
, EDP_CLK_MASK_LINK_CHAN
);
935 static int edp_ctrl_training(struct edp_ctrl
*ctrl
)
939 /* Do link training only when power is on */
944 ret
= edp_do_link_train(ctrl
);
945 if (ret
== EDP_TRAIN_RECONFIG
) {
946 /* Re-configure main link */
947 edp_ctrl_irq_enable(ctrl
, 0);
948 edp_ctrl_link_enable(ctrl
, 0);
949 msm_edp_phy_ctrl(ctrl
->phy
, 0);
951 /* Make sure link is fully disabled */
953 usleep_range(500, 1000);
955 msm_edp_phy_ctrl(ctrl
->phy
, 1);
956 edp_ctrl_link_enable(ctrl
, 1);
957 edp_ctrl_irq_enable(ctrl
, 1);
964 static void edp_ctrl_on_worker(struct work_struct
*work
)
966 struct edp_ctrl
*ctrl
= container_of(
967 work
, struct edp_ctrl
, on_work
);
970 mutex_lock(&ctrl
->dev_mutex
);
972 if (ctrl
->power_on
) {
977 edp_ctrl_phy_aux_enable(ctrl
, 1);
978 edp_ctrl_link_enable(ctrl
, 1);
980 edp_ctrl_irq_enable(ctrl
, 1);
981 ret
= drm_dp_link_power_up(ctrl
->drm_aux
, &ctrl
->dp_link
);
985 ctrl
->power_on
= true;
987 /* Start link training */
988 ret
= edp_ctrl_training(ctrl
);
989 if (ret
!= EDP_TRAIN_SUCCESS
)
996 edp_ctrl_irq_enable(ctrl
, 0);
997 edp_ctrl_link_enable(ctrl
, 0);
998 edp_ctrl_phy_aux_enable(ctrl
, 0);
999 ctrl
->power_on
= false;
1001 mutex_unlock(&ctrl
->dev_mutex
);
1004 static void edp_ctrl_off_worker(struct work_struct
*work
)
1006 struct edp_ctrl
*ctrl
= container_of(
1007 work
, struct edp_ctrl
, off_work
);
1008 unsigned long time_left
;
1010 mutex_lock(&ctrl
->dev_mutex
);
1012 if (!ctrl
->power_on
) {
1017 reinit_completion(&ctrl
->idle_comp
);
1018 edp_state_ctrl(ctrl
, EDP_STATE_CTRL_PUSH_IDLE
);
1020 time_left
= wait_for_completion_timeout(&ctrl
->idle_comp
,
1021 msecs_to_jiffies(500));
1023 DBG("%s: idle pattern timedout\n", __func__
);
1025 edp_state_ctrl(ctrl
, 0);
1027 drm_dp_link_power_down(ctrl
->drm_aux
, &ctrl
->dp_link
);
1029 edp_ctrl_irq_enable(ctrl
, 0);
1031 edp_ctrl_link_enable(ctrl
, 0);
1033 edp_ctrl_phy_aux_enable(ctrl
, 0);
1035 ctrl
->power_on
= false;
1038 mutex_unlock(&ctrl
->dev_mutex
);
1041 irqreturn_t
msm_edp_ctrl_irq(struct edp_ctrl
*ctrl
)
1043 u32 isr1
, isr2
, mask1
, mask2
;
1047 spin_lock(&ctrl
->irq_lock
);
1048 isr1
= edp_read(ctrl
->base
+ REG_EDP_INTERRUPT_REG_1
);
1049 isr2
= edp_read(ctrl
->base
+ REG_EDP_INTERRUPT_REG_2
);
1051 mask1
= isr1
& EDP_INTR_MASK1
;
1052 mask2
= isr2
& EDP_INTR_MASK2
;
1054 isr1
&= ~mask1
; /* remove masks bit */
1057 DBG("isr=%x mask=%x isr2=%x mask2=%x",
1058 isr1
, mask1
, isr2
, mask2
);
1060 ack
= isr1
& EDP_INTR_STATUS1
;
1061 ack
<<= 1; /* ack bits */
1063 edp_write(ctrl
->base
+ REG_EDP_INTERRUPT_REG_1
, ack
);
1065 ack
= isr2
& EDP_INTR_STATUS2
;
1066 ack
<<= 1; /* ack bits */
1068 edp_write(ctrl
->base
+ REG_EDP_INTERRUPT_REG_2
, ack
);
1069 spin_unlock(&ctrl
->irq_lock
);
1071 if (isr1
& EDP_INTERRUPT_REG_1_HPD
)
1074 if (isr2
& EDP_INTERRUPT_REG_2_READY_FOR_VIDEO
)
1075 DBG("edp_video_ready");
1077 if (isr2
& EDP_INTERRUPT_REG_2_IDLE_PATTERNs_SENT
) {
1078 DBG("idle_patterns_sent");
1079 complete(&ctrl
->idle_comp
);
1082 msm_edp_aux_irq(ctrl
->aux
, isr1
);
1087 void msm_edp_ctrl_power(struct edp_ctrl
*ctrl
, bool on
)
1090 queue_work(ctrl
->workqueue
, &ctrl
->on_work
);
1092 queue_work(ctrl
->workqueue
, &ctrl
->off_work
);
1095 int msm_edp_ctrl_init(struct msm_edp
*edp
)
1097 struct edp_ctrl
*ctrl
= NULL
;
1098 struct device
*dev
= &edp
->pdev
->dev
;
1102 pr_err("%s: edp is NULL!\n", __func__
);
1106 ctrl
= devm_kzalloc(dev
, sizeof(*ctrl
), GFP_KERNEL
);
1111 ctrl
->pdev
= edp
->pdev
;
1113 ctrl
->base
= msm_ioremap(ctrl
->pdev
, "edp", "eDP");
1114 if (IS_ERR(ctrl
->base
))
1115 return PTR_ERR(ctrl
->base
);
1117 /* Get regulator, clock, gpio, pwm */
1118 ret
= edp_regulator_init(ctrl
);
1120 pr_err("%s:regulator init fail\n", __func__
);
1123 ret
= edp_clk_init(ctrl
);
1125 pr_err("%s:clk init fail\n", __func__
);
1128 ret
= edp_gpio_config(ctrl
);
1130 pr_err("%s:failed to configure GPIOs: %d", __func__
, ret
);
1134 /* Init aux and phy */
1135 ctrl
->aux
= msm_edp_aux_init(dev
, ctrl
->base
, &ctrl
->drm_aux
);
1136 if (!ctrl
->aux
|| !ctrl
->drm_aux
) {
1137 pr_err("%s:failed to init aux\n", __func__
);
1141 ctrl
->phy
= msm_edp_phy_init(dev
, ctrl
->base
);
1143 pr_err("%s:failed to init phy\n", __func__
);
1145 goto err_destory_aux
;
1148 spin_lock_init(&ctrl
->irq_lock
);
1149 mutex_init(&ctrl
->dev_mutex
);
1150 init_completion(&ctrl
->idle_comp
);
1152 /* setup workqueue */
1153 ctrl
->workqueue
= alloc_ordered_workqueue("edp_drm_work", 0);
1154 INIT_WORK(&ctrl
->on_work
, edp_ctrl_on_worker
);
1155 INIT_WORK(&ctrl
->off_work
, edp_ctrl_off_worker
);
1160 msm_edp_aux_destroy(dev
, ctrl
->aux
);
1165 void msm_edp_ctrl_destroy(struct edp_ctrl
*ctrl
)
1170 if (ctrl
->workqueue
) {
1171 flush_workqueue(ctrl
->workqueue
);
1172 destroy_workqueue(ctrl
->workqueue
);
1173 ctrl
->workqueue
= NULL
;
1177 msm_edp_aux_destroy(&ctrl
->pdev
->dev
, ctrl
->aux
);
1184 mutex_destroy(&ctrl
->dev_mutex
);
1187 bool msm_edp_ctrl_panel_connected(struct edp_ctrl
*ctrl
)
1189 mutex_lock(&ctrl
->dev_mutex
);
1190 DBG("connect status = %d", ctrl
->edp_connected
);
1191 if (ctrl
->edp_connected
) {
1192 mutex_unlock(&ctrl
->dev_mutex
);
1196 if (!ctrl
->power_on
) {
1197 edp_ctrl_phy_aux_enable(ctrl
, 1);
1198 edp_ctrl_irq_enable(ctrl
, 1);
1201 if (drm_dp_dpcd_read(ctrl
->drm_aux
, DP_DPCD_REV
, ctrl
->dpcd
,
1202 DP_RECEIVER_CAP_SIZE
) < DP_RECEIVER_CAP_SIZE
) {
1203 pr_err("%s: AUX channel is NOT ready\n", __func__
);
1204 memset(ctrl
->dpcd
, 0, DP_RECEIVER_CAP_SIZE
);
1206 ctrl
->edp_connected
= true;
1209 if (!ctrl
->power_on
) {
1210 edp_ctrl_irq_enable(ctrl
, 0);
1211 edp_ctrl_phy_aux_enable(ctrl
, 0);
1214 DBG("exit: connect status=%d", ctrl
->edp_connected
);
1216 mutex_unlock(&ctrl
->dev_mutex
);
1218 return ctrl
->edp_connected
;
1221 int msm_edp_ctrl_get_panel_info(struct edp_ctrl
*ctrl
,
1222 struct drm_connector
*connector
, struct edid
**edid
)
1226 mutex_lock(&ctrl
->dev_mutex
);
1230 DBG("Just return edid buffer");
1236 if (!ctrl
->power_on
) {
1237 edp_ctrl_phy_aux_enable(ctrl
, 1);
1238 edp_ctrl_irq_enable(ctrl
, 1);
1241 ret
= drm_dp_link_probe(ctrl
->drm_aux
, &ctrl
->dp_link
);
1243 pr_err("%s: read dpcd cap failed, %d\n", __func__
, ret
);
1247 /* Initialize link rate as panel max link rate */
1248 ctrl
->link_rate
= drm_dp_link_rate_to_bw_code(ctrl
->dp_link
.rate
);
1250 ctrl
->edid
= drm_get_edid(connector
, &ctrl
->drm_aux
->ddc
);
1252 pr_err("%s: edid read fail\n", __func__
);
1260 if (!ctrl
->power_on
) {
1261 edp_ctrl_irq_enable(ctrl
, 0);
1262 edp_ctrl_phy_aux_enable(ctrl
, 0);
1265 mutex_unlock(&ctrl
->dev_mutex
);
1269 int msm_edp_ctrl_timing_cfg(struct edp_ctrl
*ctrl
,
1270 const struct drm_display_mode
*mode
,
1271 const struct drm_display_info
*info
)
1273 u32 hstart_from_sync
, vstart_from_sync
;
1277 mutex_lock(&ctrl
->dev_mutex
);
1279 * Need to keep color depth, pixel rate and
1280 * interlaced information in ctrl context
1282 ctrl
->color_depth
= info
->bpc
;
1283 ctrl
->pixel_rate
= mode
->clock
;
1284 ctrl
->interlaced
= !!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
);
1286 /* Fill initial link config based on passed in timing */
1287 edp_fill_link_cfg(ctrl
);
1289 if (edp_clk_enable(ctrl
, EDP_CLK_MASK_AHB
)) {
1290 pr_err("%s, fail to prepare enable ahb clk\n", __func__
);
1294 edp_clock_synchrous(ctrl
, 1);
1296 /* Configure eDP timing to HW */
1297 edp_write(ctrl
->base
+ REG_EDP_TOTAL_HOR_VER
,
1298 EDP_TOTAL_HOR_VER_HORIZ(mode
->htotal
) |
1299 EDP_TOTAL_HOR_VER_VERT(mode
->vtotal
));
1301 vstart_from_sync
= mode
->vtotal
- mode
->vsync_start
;
1302 hstart_from_sync
= mode
->htotal
- mode
->hsync_start
;
1303 edp_write(ctrl
->base
+ REG_EDP_START_HOR_VER_FROM_SYNC
,
1304 EDP_START_HOR_VER_FROM_SYNC_HORIZ(hstart_from_sync
) |
1305 EDP_START_HOR_VER_FROM_SYNC_VERT(vstart_from_sync
));
1307 data
= EDP_HSYNC_VSYNC_WIDTH_POLARITY_VERT(
1308 mode
->vsync_end
- mode
->vsync_start
);
1309 data
|= EDP_HSYNC_VSYNC_WIDTH_POLARITY_HORIZ(
1310 mode
->hsync_end
- mode
->hsync_start
);
1311 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
1312 data
|= EDP_HSYNC_VSYNC_WIDTH_POLARITY_NVSYNC
;
1313 if (mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
1314 data
|= EDP_HSYNC_VSYNC_WIDTH_POLARITY_NHSYNC
;
1315 edp_write(ctrl
->base
+ REG_EDP_HSYNC_VSYNC_WIDTH_POLARITY
, data
);
1317 edp_write(ctrl
->base
+ REG_EDP_ACTIVE_HOR_VER
,
1318 EDP_ACTIVE_HOR_VER_HORIZ(mode
->hdisplay
) |
1319 EDP_ACTIVE_HOR_VER_VERT(mode
->vdisplay
));
1321 edp_clk_disable(ctrl
, EDP_CLK_MASK_AHB
);
1324 mutex_unlock(&ctrl
->dev_mutex
);
1328 bool msm_edp_ctrl_pixel_clock_valid(struct edp_ctrl
*ctrl
,
1329 u32 pixel_rate
, u32
*pm
, u32
*pn
)
1331 const struct edp_pixel_clk_div
*divs
;
1332 u32 err
= 1; /* 1% error tolerance */
1336 if (ctrl
->link_rate
== DP_LINK_BW_1_62
) {
1338 } else if (ctrl
->link_rate
== DP_LINK_BW_2_7
) {
1341 pr_err("%s: Invalid link rate,%d\n", __func__
, ctrl
->link_rate
);
1345 for (i
= 0; i
< EDP_PIXEL_CLK_NUM
; i
++) {
1346 clk_err
= abs(divs
[i
].rate
- pixel_rate
);
1347 if ((divs
[i
].rate
* err
/ 100) >= clk_err
) {
1356 DBG("pixel clock %d(kHz) not supported", pixel_rate
);