1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * tc358767 eDP bridge driver
5 * Copyright (C) 2016 CogentEmbedded Inc
10 * Copyright (C) 2016 Zodiac Inflight Innovations
12 * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c
14 * Copyright (C) 2012 Texas Instruments
18 #include <linux/clk.h>
19 #include <linux/device.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/i2c.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
27 #include <drm/drm_atomic_helper.h>
28 #include <drm/drm_dp_helper.h>
29 #include <drm/drm_edid.h>
30 #include <drm/drm_of.h>
31 #include <drm/drm_panel.h>
32 #include <drm/drm_probe_helper.h>
36 /* Display Parallel Interface */
37 #define DPIPXLFMT 0x0440
38 #define VS_POL_ACTIVE_LOW (1 << 10)
39 #define HS_POL_ACTIVE_LOW (1 << 9)
40 #define DE_POL_ACTIVE_HIGH (0 << 8)
41 #define SUB_CFG_TYPE_CONFIG1 (0 << 2) /* LSB aligned */
42 #define SUB_CFG_TYPE_CONFIG2 (1 << 2) /* Loosely Packed */
43 #define SUB_CFG_TYPE_CONFIG3 (2 << 2) /* LSB aligned 8-bit */
44 #define DPI_BPP_RGB888 (0 << 0)
45 #define DPI_BPP_RGB666 (1 << 0)
46 #define DPI_BPP_RGB565 (2 << 0)
49 #define VPCTRL0 0x0450
50 #define OPXLFMT_RGB666 (0 << 8)
51 #define OPXLFMT_RGB888 (1 << 8)
52 #define FRMSYNC_DISABLED (0 << 4) /* Video Timing Gen Disabled */
53 #define FRMSYNC_ENABLED (1 << 4) /* Video Timing Gen Enabled */
54 #define MSF_DISABLED (0 << 0) /* Magic Square FRC disabled */
55 #define MSF_ENABLED (1 << 0) /* Magic Square FRC enabled */
61 #define VFUEN BIT(0) /* Video Frame Timing Upload */
64 #define TC_IDREG 0x0500
65 #define SYSSTAT 0x0508
66 #define SYSCTRL 0x0510
67 #define DP0_AUDSRC_NO_INPUT (0 << 3)
68 #define DP0_AUDSRC_I2S_RX (1 << 3)
69 #define DP0_VIDSRC_NO_INPUT (0 << 0)
70 #define DP0_VIDSRC_DSI_RX (1 << 0)
71 #define DP0_VIDSRC_DPI_RX (2 << 0)
72 #define DP0_VIDSRC_COLOR_BAR (3 << 0)
77 #define INTCTL_G 0x0560
78 #define INTSTS_G 0x0564
80 #define INT_SYSERR BIT(16)
81 #define INT_GPIO_H(x) (1 << (x == 0 ? 2 : 10))
82 #define INT_GPIO_LC(x) (1 << (x == 0 ? 3 : 11))
84 #define INT_GP0_LCNT 0x0584
85 #define INT_GP1_LCNT 0x0588
89 #define VID_MN_GEN BIT(6) /* Auto-generate M/N values */
90 #define EF_EN BIT(5) /* Enable Enhanced Framing */
91 #define VID_EN BIT(1) /* Video transmission enable */
92 #define DP_EN BIT(0) /* Enable DPTX function */
95 #define DP0_VIDMNGEN0 0x0610
96 #define DP0_VIDMNGEN1 0x0614
97 #define DP0_VMNGENSTATUS 0x0618
100 #define DP0_SECSAMPLE 0x0640
101 #define DP0_VIDSYNCDELAY 0x0644
102 #define DP0_TOTALVAL 0x0648
103 #define DP0_STARTVAL 0x064c
104 #define DP0_ACTIVEVAL 0x0650
105 #define DP0_SYNCVAL 0x0654
106 #define SYNCVAL_HS_POL_ACTIVE_LOW (1 << 15)
107 #define SYNCVAL_VS_POL_ACTIVE_LOW (1 << 31)
108 #define DP0_MISC 0x0658
109 #define TU_SIZE_RECOMMENDED (63) /* LSCLK cycles per TU */
110 #define BPC_6 (0 << 5)
111 #define BPC_8 (1 << 5)
114 #define DP0_AUXCFG0 0x0660
115 #define DP0_AUXCFG1 0x0664
116 #define AUX_RX_FILTER_EN BIT(16)
118 #define DP0_AUXADDR 0x0668
119 #define DP0_AUXWDATA(i) (0x066c + (i) * 4)
120 #define DP0_AUXRDATA(i) (0x067c + (i) * 4)
121 #define DP0_AUXSTATUS 0x068c
122 #define AUX_STATUS_MASK 0xf0
123 #define AUX_STATUS_SHIFT 4
124 #define AUX_TIMEOUT BIT(1)
125 #define AUX_BUSY BIT(0)
126 #define DP0_AUXI2CADR 0x0698
129 #define DP0_SRCCTRL 0x06a0
130 #define DP0_SRCCTRL_SCRMBLDIS BIT(13)
131 #define DP0_SRCCTRL_EN810B BIT(12)
132 #define DP0_SRCCTRL_NOTP (0 << 8)
133 #define DP0_SRCCTRL_TP1 (1 << 8)
134 #define DP0_SRCCTRL_TP2 (2 << 8)
135 #define DP0_SRCCTRL_LANESKEW BIT(7)
136 #define DP0_SRCCTRL_SSCG BIT(3)
137 #define DP0_SRCCTRL_LANES_1 (0 << 2)
138 #define DP0_SRCCTRL_LANES_2 (1 << 2)
139 #define DP0_SRCCTRL_BW27 (1 << 1)
140 #define DP0_SRCCTRL_BW162 (0 << 1)
141 #define DP0_SRCCTRL_AUTOCORRECT BIT(0)
142 #define DP0_LTSTAT 0x06d0
143 #define LT_LOOPDONE BIT(13)
144 #define LT_STATUS_MASK (0x1f << 8)
145 #define LT_CHANNEL1_EQ_BITS (DP_CHANNEL_EQ_BITS << 4)
146 #define LT_INTERLANE_ALIGN_DONE BIT(3)
147 #define LT_CHANNEL0_EQ_BITS (DP_CHANNEL_EQ_BITS)
148 #define DP0_SNKLTCHGREQ 0x06d4
149 #define DP0_LTLOOPCTRL 0x06d8
150 #define DP0_SNKLTCTRL 0x06e4
152 #define DP1_SRCCTRL 0x07a0
155 #define DP_PHY_CTRL 0x0800
156 #define DP_PHY_RST BIT(28) /* DP PHY Global Soft Reset */
157 #define BGREN BIT(25) /* AUX PHY BGR Enable */
158 #define PWR_SW_EN BIT(24) /* PHY Power Switch Enable */
159 #define PHY_M1_RST BIT(12) /* Reset PHY1 Main Channel */
160 #define PHY_RDY BIT(16) /* PHY Main Channels Ready */
161 #define PHY_M0_RST BIT(8) /* Reset PHY0 Main Channel */
162 #define PHY_2LANE BIT(2) /* PHY Enable 2 lanes */
163 #define PHY_A0_EN BIT(1) /* PHY Aux Channel0 Enable */
164 #define PHY_M0_EN BIT(0) /* PHY Main Channel0 Enable */
167 #define DP0_PLLCTRL 0x0900
168 #define DP1_PLLCTRL 0x0904 /* not defined in DS */
169 #define PXL_PLLCTRL 0x0908
170 #define PLLUPDATE BIT(2)
171 #define PLLBYP BIT(1)
173 #define PXL_PLLPARAM 0x0914
174 #define IN_SEL_REFCLK (0 << 14)
175 #define SYS_PLLPARAM 0x0918
176 #define REF_FREQ_38M4 (0 << 8) /* 38.4 MHz */
177 #define REF_FREQ_19M2 (1 << 8) /* 19.2 MHz */
178 #define REF_FREQ_26M (2 << 8) /* 26 MHz */
179 #define REF_FREQ_13M (3 << 8) /* 13 MHz */
180 #define SYSCLK_SEL_LSCLK (0 << 4)
181 #define LSCLK_DIV_1 (0 << 0)
182 #define LSCLK_DIV_2 (1 << 0)
185 #define TSTCTL 0x0a00
186 #define PLL_DBG 0x0a04
188 static bool tc_test_pattern;
189 module_param_named(test, tc_test_pattern, bool, 0644);
192 struct drm_dp_link base;
200 struct regmap *regmap;
201 struct drm_dp_aux aux;
203 struct drm_bridge bridge;
204 struct drm_connector connector;
205 struct drm_panel *panel;
208 struct tc_edp_link link;
213 struct drm_display_mode mode;
218 struct gpio_desc *sd_gpio;
219 struct gpio_desc *reset_gpio;
225 /* HPD pin number (0 or 1) or -ENODEV */
229 static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
231 return container_of(a, struct tc_data, aux);
234 static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
236 return container_of(b, struct tc_data, bridge);
239 static inline struct tc_data *connector_to_tc(struct drm_connector *c)
241 return container_of(c, struct tc_data, connector);
244 /* Simple macros to avoid repeated error checks */
245 #define tc_write(reg, var) \
247 ret = regmap_write(tc->regmap, reg, var); \
251 #define tc_read(reg, var) \
253 ret = regmap_read(tc->regmap, reg, var); \
258 static inline int tc_poll_timeout(struct regmap *map, unsigned int addr,
259 unsigned int cond_mask,
260 unsigned int cond_value,
261 unsigned long sleep_us, u64 timeout_us)
263 ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
268 ret = regmap_read(map, addr, &val);
271 if ((val & cond_mask) == cond_value)
273 if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) {
274 ret = regmap_read(map, addr, &val);
278 usleep_range((sleep_us >> 2) + 1, sleep_us);
280 return ret ?: (((val & cond_mask) == cond_value) ? 0 : -ETIMEDOUT);
283 static int tc_aux_wait_busy(struct tc_data *tc, unsigned int timeout_ms)
285 return tc_poll_timeout(tc->regmap, DP0_AUXSTATUS, AUX_BUSY, 0,
286 1000, 1000 * timeout_ms);
289 static int tc_aux_get_status(struct tc_data *tc, u8 *reply)
294 ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &value);
298 if (value & AUX_BUSY) {
299 dev_err(tc->dev, "aux busy!\n");
303 if (value & AUX_TIMEOUT) {
304 dev_err(tc->dev, "aux access timeout!\n");
308 *reply = (value & AUX_STATUS_MASK) >> AUX_STATUS_SHIFT;
312 static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
313 struct drm_dp_aux_msg *msg)
315 struct tc_data *tc = aux_to_tc(aux);
316 size_t size = min_t(size_t, 8, msg->size);
317 u8 request = msg->request & ~DP_AUX_I2C_MOT;
318 u8 *buf = msg->buffer;
326 ret = tc_aux_wait_busy(tc, 100);
330 if (request == DP_AUX_I2C_WRITE || request == DP_AUX_NATIVE_WRITE) {
333 if (request == DP_AUX_NATIVE_WRITE)
334 tmp = tmp | (buf[i] << (8 * (i & 0x3)));
336 tmp = (tmp << 8) | buf[i];
338 if (((i % 4) == 0) || (i == size)) {
339 tc_write(DP0_AUXWDATA((i - 1) >> 2), tmp);
343 } else if (request != DP_AUX_I2C_READ &&
344 request != DP_AUX_NATIVE_READ) {
349 tc_write(DP0_AUXADDR, msg->address);
351 tc_write(DP0_AUXCFG0, ((size - 1) << 8) | request);
353 ret = tc_aux_wait_busy(tc, 100);
357 ret = tc_aux_get_status(tc, &msg->reply);
361 if (request == DP_AUX_I2C_READ || request == DP_AUX_NATIVE_READ) {
365 tc_read(DP0_AUXRDATA(i >> 2), &tmp);
377 static const char * const training_pattern1_errors[] = {
381 "Max voltage reached error",
382 "Loop counter expired error",
386 static const char * const training_pattern2_errors[] = {
390 "Clock recovery failed error",
391 "Loop counter expired error",
395 static u32 tc_srcctrl(struct tc_data *tc)
398 * No training pattern, skew lane 1 data by two LSCLK cycles with
399 * respect to lane 0 data, AutoCorrect Mode = 0
401 u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_EN810B;
403 if (tc->link.scrambler_dis)
404 reg |= DP0_SRCCTRL_SCRMBLDIS; /* Scrambler Disabled */
406 reg |= DP0_SRCCTRL_SSCG; /* Spread Spectrum Enable */
407 if (tc->link.base.num_lanes == 2)
408 reg |= DP0_SRCCTRL_LANES_2; /* Two Main Channel Lanes */
409 if (tc->link.base.rate != 162000)
410 reg |= DP0_SRCCTRL_BW27; /* 2.7 Gbps link */
414 static void tc_wait_pll_lock(struct tc_data *tc)
416 /* Wait for PLL to lock: up to 2.09 ms, depending on refclk */
417 usleep_range(3000, 6000);
420 static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
423 int i_pre, best_pre = 1;
424 int i_post, best_post = 1;
425 int div, best_div = 1;
426 int mul, best_mul = 1;
427 int delta, best_delta;
428 int ext_div[] = {1, 2, 3, 5, 7};
429 int best_pixelclock = 0;
432 dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
434 best_delta = pixelclock;
435 /* Loop over all possible ext_divs, skipping invalid configurations */
436 for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
438 * refclk / ext_pre_div should be in the 1 to 200 MHz range.
439 * We don't allow any refclk > 200 MHz, only check lower bounds.
441 if (refclk / ext_div[i_pre] < 1000000)
443 for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
444 for (div = 1; div <= 16; div++) {
448 tmp = pixelclock * ext_div[i_pre] *
449 ext_div[i_post] * div;
454 if ((mul < 1) || (mul > 128))
457 clk = (refclk / ext_div[i_pre] / div) * mul;
459 * refclk * mul / (ext_pre_div * pre_div)
460 * should be in the 150 to 650 MHz range
462 if ((clk > 650000000) || (clk < 150000000))
465 clk = clk / ext_div[i_post];
466 delta = clk - pixelclock;
468 if (abs(delta) < abs(best_delta)) {
474 best_pixelclock = clk;
479 if (best_pixelclock == 0) {
480 dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
485 dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
487 dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
488 ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
490 /* if VCO >= 300 MHz */
491 if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
499 /* Power up PLL and switch to bypass */
500 tc_write(PXL_PLLCTRL, PLLBYP | PLLEN);
502 tc_write(PXL_PLLPARAM,
503 (vco_hi << 24) | /* For PLL VCO >= 300 MHz = 1 */
504 (ext_div[best_pre] << 20) | /* External Pre-divider */
505 (ext_div[best_post] << 16) | /* External Post-divider */
506 IN_SEL_REFCLK | /* Use RefClk as PLL input */
507 (best_div << 8) | /* Divider for PLL RefClk */
508 (best_mul << 0)); /* Multiplier for PLL */
510 /* Force PLL parameter update and disable bypass */
511 tc_write(PXL_PLLCTRL, PLLUPDATE | PLLEN);
513 tc_wait_pll_lock(tc);
520 static int tc_pxl_pll_dis(struct tc_data *tc)
522 /* Enable PLL bypass, power down PLL */
523 return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
526 static int tc_stream_clock_calc(struct tc_data *tc)
530 * If the Stream clock and Link Symbol clock are
531 * asynchronous with each other, the value of M changes over
532 * time. This way of generating link clock and stream
533 * clock is called Asynchronous Clock mode. The value M
534 * must change while the value N stays constant. The
535 * value of N in this Asynchronous Clock mode must be set
538 * LSCLK = 1/10 of high speed link clock
540 * f_STRMCLK = M/N * f_LSCLK
541 * M/N = f_STRMCLK / f_LSCLK
544 tc_write(DP0_VIDMNGEN1, 32768);
551 static int tc_aux_link_setup(struct tc_data *tc)
557 rate = clk_get_rate(tc->refclk);
560 value = REF_FREQ_38M4;
563 value = REF_FREQ_26M;
566 value = REF_FREQ_19M2;
569 value = REF_FREQ_13M;
572 dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
576 /* Setup DP-PHY / PLL */
577 value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
578 tc_write(SYS_PLLPARAM, value);
580 tc_write(DP_PHY_CTRL, BGREN | PWR_SW_EN | PHY_A0_EN);
583 * Initially PLLs are in bypass. Force PLL parameter update,
584 * disable PLL bypass, enable PLL
586 tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
587 tc_wait_pll_lock(tc);
589 tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
590 tc_wait_pll_lock(tc);
592 ret = tc_poll_timeout(tc->regmap, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 1,
594 if (ret == -ETIMEDOUT) {
595 dev_err(tc->dev, "Timeout waiting for PHY to become ready");
602 tc_write(DP0_AUXCFG1, AUX_RX_FILTER_EN |
603 (0x06 << 8) | /* Aux Bit Period Calculator Threshold */
604 (0x3f << 0)); /* Aux Response Timeout Timer */
608 dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
612 static int tc_get_display_props(struct tc_data *tc)
618 /* Read DP Rx Link Capability */
619 ret = drm_dp_link_probe(&tc->aux, &tc->link.base);
622 if (tc->link.base.rate != 162000 && tc->link.base.rate != 270000) {
623 dev_dbg(tc->dev, "Falling to 2.7 Gbps rate\n");
624 tc->link.base.rate = 270000;
627 if (tc->link.base.num_lanes > 2) {
628 dev_dbg(tc->dev, "Falling to 2 lanes\n");
629 tc->link.base.num_lanes = 2;
632 ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, tmp);
635 tc->link.spread = tmp[0] & DP_MAX_DOWNSPREAD_0_5;
637 ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, tmp);
641 tc->link.scrambler_dis = false;
643 ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, tmp);
646 tc->link.assr = tmp[0] & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
648 dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
649 tc->link.base.revision >> 4, tc->link.base.revision & 0x0f,
650 (tc->link.base.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
651 tc->link.base.num_lanes,
652 (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING) ?
653 "enhanced" : "non-enhanced");
654 dev_dbg(tc->dev, "Downspread: %s, scrambler: %s\n",
655 tc->link.spread ? "0.5%" : "0.0%",
656 tc->link.scrambler_dis ? "disabled" : "enabled");
657 dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
658 tc->link.assr, tc->assr);
663 dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
667 static int tc_set_video_mode(struct tc_data *tc,
668 const struct drm_display_mode *mode)
674 int left_margin = mode->htotal - mode->hsync_end;
675 int right_margin = mode->hsync_start - mode->hdisplay;
676 int hsync_len = mode->hsync_end - mode->hsync_start;
677 int upper_margin = mode->vtotal - mode->vsync_end;
678 int lower_margin = mode->vsync_start - mode->vdisplay;
679 int vsync_len = mode->vsync_end - mode->vsync_start;
682 * Recommended maximum number of symbols transferred in a transfer unit:
683 * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
684 * (output active video bandwidth in bytes))
685 * Must be less than tu_size.
687 max_tu_symbol = TU_SIZE_RECOMMENDED - 1;
689 dev_dbg(tc->dev, "set mode %dx%d\n",
690 mode->hdisplay, mode->vdisplay);
691 dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
692 left_margin, right_margin, hsync_len);
693 dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
694 upper_margin, lower_margin, vsync_len);
695 dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
700 * datasheet is not clear of vsdelay in case of DPI
701 * assume we do not need any delay when DPI is a source of
704 tc_write(VPCTRL0, (0 << 20) /* VSDELAY */ |
705 OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
706 tc_write(HTIM01, (ALIGN(left_margin, 2) << 16) | /* H back porch */
707 (ALIGN(hsync_len, 2) << 0)); /* Hsync */
708 tc_write(HTIM02, (ALIGN(right_margin, 2) << 16) | /* H front porch */
709 (ALIGN(mode->hdisplay, 2) << 0)); /* width */
710 tc_write(VTIM01, (upper_margin << 16) | /* V back porch */
711 (vsync_len << 0)); /* Vsync */
712 tc_write(VTIM02, (lower_margin << 16) | /* V front porch */
713 (mode->vdisplay << 0)); /* height */
714 tc_write(VFUEN0, VFUEN); /* update settings */
716 /* Test pattern settings */
718 (120 << 24) | /* Red Color component value */
719 (20 << 16) | /* Green Color component value */
720 (99 << 8) | /* Blue Color component value */
721 (1 << 4) | /* Enable I2C Filter */
722 (2 << 0) | /* Color bar Mode */
725 /* DP Main Stream Attributes */
726 vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
727 tc_write(DP0_VIDSYNCDELAY,
728 (max_tu_symbol << 16) | /* thresh_dly */
729 (vid_sync_dly << 0));
731 tc_write(DP0_TOTALVAL, (mode->vtotal << 16) | (mode->htotal));
733 tc_write(DP0_STARTVAL,
734 ((upper_margin + vsync_len) << 16) |
735 ((left_margin + hsync_len) << 0));
737 tc_write(DP0_ACTIVEVAL, (mode->vdisplay << 16) | (mode->hdisplay));
739 tc_write(DP0_SYNCVAL, (vsync_len << 16) | (hsync_len << 0) |
740 ((mode->flags & DRM_MODE_FLAG_NHSYNC) ? SYNCVAL_HS_POL_ACTIVE_LOW : 0) |
741 ((mode->flags & DRM_MODE_FLAG_NVSYNC) ? SYNCVAL_VS_POL_ACTIVE_LOW : 0));
743 tc_write(DPIPXLFMT, VS_POL_ACTIVE_LOW | HS_POL_ACTIVE_LOW |
744 DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 | DPI_BPP_RGB888);
746 tc_write(DP0_MISC, (max_tu_symbol << 23) | (TU_SIZE_RECOMMENDED << 16) |
754 static int tc_wait_link_training(struct tc_data *tc)
762 tc_read(DP0_LTSTAT, &value);
763 } while ((!(value & LT_LOOPDONE)) && (--timeout));
766 dev_err(tc->dev, "Link training timeout waiting for LT_LOOPDONE!\n");
770 return (value >> 8) & 0x7;
776 static int tc_main_link_enable(struct tc_data *tc)
778 struct drm_dp_aux *aux = &tc->aux;
779 struct device *dev = tc->dev;
787 dev_dbg(tc->dev, "link enable\n");
789 tc_read(DP0CTL, &value);
790 if (WARN_ON(value & DP_EN))
793 tc_write(DP0_SRCCTRL, tc_srcctrl(tc));
794 /* SSCG and BW27 on DP1 must be set to the same as on DP0 */
795 tc_write(DP1_SRCCTRL,
796 (tc->link.spread ? DP0_SRCCTRL_SSCG : 0) |
797 ((tc->link.base.rate != 162000) ? DP0_SRCCTRL_BW27 : 0));
799 rate = clk_get_rate(tc->refclk);
802 value = REF_FREQ_38M4;
805 value = REF_FREQ_26M;
808 value = REF_FREQ_19M2;
811 value = REF_FREQ_13M;
816 value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
817 tc_write(SYS_PLLPARAM, value);
819 /* Setup Main Link */
820 dp_phy_ctrl = BGREN | PWR_SW_EN | PHY_A0_EN | PHY_M0_EN;
821 if (tc->link.base.num_lanes == 2)
822 dp_phy_ctrl |= PHY_2LANE;
823 tc_write(DP_PHY_CTRL, dp_phy_ctrl);
826 tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
827 tc_wait_pll_lock(tc);
829 tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
830 tc_wait_pll_lock(tc);
832 /* Reset/Enable Main Links */
833 dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST;
834 tc_write(DP_PHY_CTRL, dp_phy_ctrl);
835 usleep_range(100, 200);
836 dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
837 tc_write(DP_PHY_CTRL, dp_phy_ctrl);
841 tc_read(DP_PHY_CTRL, &value);
843 } while ((!(value & PHY_RDY)) && (--timeout));
846 dev_err(dev, "timeout waiting for phy become ready");
850 /* Set misc: 8 bits per color */
851 ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
857 * on TC358767 side ASSR configured through strap pin
858 * seems there is no way to change this setting from SW
860 * check is tc configured for same mode
862 if (tc->assr != tc->link.assr) {
863 dev_dbg(dev, "Trying to set display to ASSR: %d\n",
865 /* try to set ASSR on display side */
867 ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
871 ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
875 if (tmp[0] != tc->assr) {
876 dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
878 /* trying with disabled scrambler */
879 tc->link.scrambler_dis = true;
883 /* Setup Link & DPRx Config for Training */
884 ret = drm_dp_link_configure(aux, &tc->link.base);
888 /* DOWNSPREAD_CTRL */
889 tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
890 /* MAIN_LINK_CHANNEL_CODING_SET */
891 tmp[1] = DP_SET_ANSI_8B10B;
892 ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
896 /* Reset voltage-swing & pre-emphasis */
897 tmp[0] = tmp[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 |
898 DP_TRAIN_PRE_EMPH_LEVEL_0;
899 ret = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, tmp, 2);
905 /* Set DPCD 0x102 for Training Pattern 1 */
906 tc_write(DP0_SNKLTCTRL, DP_LINK_SCRAMBLING_DISABLE |
907 DP_TRAINING_PATTERN_1);
909 tc_write(DP0_LTLOOPCTRL,
910 (15 << 28) | /* Defer Iteration Count */
911 (15 << 24) | /* Loop Iteration Count */
912 (0xd << 0)); /* Loop Timer Delay */
914 tc_write(DP0_SRCCTRL, tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
915 DP0_SRCCTRL_AUTOCORRECT | DP0_SRCCTRL_TP1);
917 /* Enable DP0 to start Link Training */
919 ((tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING) ? EF_EN : 0) |
923 ret = tc_wait_link_training(tc);
928 dev_err(tc->dev, "Link training phase 1 failed: %s\n",
929 training_pattern1_errors[ret]);
934 /* Channel Equalization */
936 /* Set DPCD 0x102 for Training Pattern 2 */
937 tc_write(DP0_SNKLTCTRL, DP_LINK_SCRAMBLING_DISABLE |
938 DP_TRAINING_PATTERN_2);
940 tc_write(DP0_SRCCTRL, tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
941 DP0_SRCCTRL_AUTOCORRECT | DP0_SRCCTRL_TP2);
944 ret = tc_wait_link_training(tc);
949 dev_err(tc->dev, "Link training phase 2 failed: %s\n",
950 training_pattern2_errors[ret]);
956 * Toshiba's documentation suggests to first clear DPCD 0x102, then
957 * clear the training pattern bit in DP0_SRCCTRL. Testing shows
958 * that the link sometimes drops if those steps are done in that order,
959 * but if the steps are done in reverse order, the link stays up.
961 * So we do the steps differently than documented here.
964 /* Clear Training Pattern, set AutoCorrect Mode = 1 */
965 tc_write(DP0_SRCCTRL, tc_srcctrl(tc) | DP0_SRCCTRL_AUTOCORRECT);
967 /* Clear DPCD 0x102 */
968 /* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
969 tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
970 ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
974 /* Check link status */
975 ret = drm_dp_dpcd_read_link_status(aux, tmp);
981 value = tmp[0] & DP_CHANNEL_EQ_BITS;
983 if (value != DP_CHANNEL_EQ_BITS) {
984 dev_err(tc->dev, "Lane 0 failed: %x\n", value);
988 if (tc->link.base.num_lanes == 2) {
989 value = (tmp[0] >> 4) & DP_CHANNEL_EQ_BITS;
991 if (value != DP_CHANNEL_EQ_BITS) {
992 dev_err(tc->dev, "Lane 1 failed: %x\n", value);
996 if (!(tmp[2] & DP_INTERLANE_ALIGN_DONE)) {
997 dev_err(tc->dev, "Interlane align failed\n");
1003 dev_err(dev, "0x0202 LANE0_1_STATUS: 0x%02x\n", tmp[0]);
1004 dev_err(dev, "0x0203 LANE2_3_STATUS 0x%02x\n", tmp[1]);
1005 dev_err(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n", tmp[2]);
1006 dev_err(dev, "0x0205 SINK_STATUS: 0x%02x\n", tmp[3]);
1007 dev_err(dev, "0x0206 ADJUST_REQUEST_LANE0_1: 0x%02x\n", tmp[4]);
1008 dev_err(dev, "0x0207 ADJUST_REQUEST_LANE2_3: 0x%02x\n", tmp[5]);
1014 dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
1017 dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
1022 static int tc_main_link_disable(struct tc_data *tc)
1026 dev_dbg(tc->dev, "link disable\n");
1028 tc_write(DP0_SRCCTRL, 0);
1029 tc_write(DP0CTL, 0);
1036 static int tc_stream_enable(struct tc_data *tc)
1041 dev_dbg(tc->dev, "enable video stream\n");
1044 if (tc_test_pattern) {
1045 ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
1046 1000 * tc->mode.clock);
1051 ret = tc_set_video_mode(tc, &tc->mode);
1056 ret = tc_stream_clock_calc(tc);
1060 value = VID_MN_GEN | DP_EN;
1061 if (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1063 tc_write(DP0CTL, value);
1065 * VID_EN assertion should be delayed by at least N * LSCLK
1066 * cycles from the time VID_MN_GEN is enabled in order to
1067 * generate stable values for VID_M. LSCLK is 270 MHz or
1068 * 162 MHz, VID_N is set to 32768 in tc_stream_clock_calc(),
1069 * so a delay of at least 203 us should suffice.
1071 usleep_range(500, 1000);
1073 tc_write(DP0CTL, value);
1074 /* Set input interface */
1075 value = DP0_AUDSRC_NO_INPUT;
1076 if (tc_test_pattern)
1077 value |= DP0_VIDSRC_COLOR_BAR;
1079 value |= DP0_VIDSRC_DPI_RX;
1080 tc_write(SYSCTRL, value);
1087 static int tc_stream_disable(struct tc_data *tc)
1092 dev_dbg(tc->dev, "disable video stream\n");
1094 tc_read(DP0CTL, &val);
1096 tc_write(DP0CTL, val);
1105 static void tc_bridge_pre_enable(struct drm_bridge *bridge)
1107 struct tc_data *tc = bridge_to_tc(bridge);
1109 drm_panel_prepare(tc->panel);
1112 static void tc_bridge_enable(struct drm_bridge *bridge)
1114 struct tc_data *tc = bridge_to_tc(bridge);
1117 ret = tc_get_display_props(tc);
1119 dev_err(tc->dev, "failed to read display props: %d\n", ret);
1123 ret = tc_main_link_enable(tc);
1125 dev_err(tc->dev, "main link enable error: %d\n", ret);
1129 ret = tc_stream_enable(tc);
1131 dev_err(tc->dev, "main link stream start error: %d\n", ret);
1132 tc_main_link_disable(tc);
1136 drm_panel_enable(tc->panel);
1139 static void tc_bridge_disable(struct drm_bridge *bridge)
1141 struct tc_data *tc = bridge_to_tc(bridge);
1144 drm_panel_disable(tc->panel);
1146 ret = tc_stream_disable(tc);
1148 dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1150 ret = tc_main_link_disable(tc);
1152 dev_err(tc->dev, "main link disable error: %d\n", ret);
1155 static void tc_bridge_post_disable(struct drm_bridge *bridge)
1157 struct tc_data *tc = bridge_to_tc(bridge);
1159 drm_panel_unprepare(tc->panel);
1162 static bool tc_bridge_mode_fixup(struct drm_bridge *bridge,
1163 const struct drm_display_mode *mode,
1164 struct drm_display_mode *adj)
1166 /* Fixup sync polarities, both hsync and vsync are active low */
1167 adj->flags = mode->flags;
1168 adj->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
1169 adj->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
1174 static enum drm_mode_status tc_mode_valid(struct drm_bridge *bridge,
1175 const struct drm_display_mode *mode)
1177 struct tc_data *tc = bridge_to_tc(bridge);
1179 u32 bits_per_pixel = 24;
1181 /* DPI interface clock limitation: upto 154 MHz */
1182 if (mode->clock > 154000)
1183 return MODE_CLOCK_HIGH;
1185 req = mode->clock * bits_per_pixel / 8;
1186 avail = tc->link.base.num_lanes * tc->link.base.rate;
1194 static void tc_bridge_mode_set(struct drm_bridge *bridge,
1195 const struct drm_display_mode *mode,
1196 const struct drm_display_mode *adj)
1198 struct tc_data *tc = bridge_to_tc(bridge);
1203 static int tc_connector_get_modes(struct drm_connector *connector)
1205 struct tc_data *tc = connector_to_tc(connector);
1210 ret = tc_get_display_props(tc);
1212 dev_err(tc->dev, "failed to read display props: %d\n", ret);
1216 if (tc->panel && tc->panel->funcs && tc->panel->funcs->get_modes) {
1217 count = tc->panel->funcs->get_modes(tc->panel);
1222 edid = drm_get_edid(connector, &tc->aux.ddc);
1229 drm_connector_update_edid_property(connector, edid);
1230 count = drm_add_edid_modes(connector, edid);
1235 static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
1236 .get_modes = tc_connector_get_modes,
1239 static enum drm_connector_status tc_connector_detect(struct drm_connector *connector,
1242 struct tc_data *tc = connector_to_tc(connector);
1247 if (tc->hpd_pin < 0) {
1249 return connector_status_connected;
1251 return connector_status_unknown;
1254 tc_read(GPIOI, &val);
1256 conn = val & BIT(tc->hpd_pin);
1259 return connector_status_connected;
1261 return connector_status_disconnected;
1264 return connector_status_unknown;
1267 static const struct drm_connector_funcs tc_connector_funcs = {
1268 .detect = tc_connector_detect,
1269 .fill_modes = drm_helper_probe_single_connector_modes,
1270 .destroy = drm_connector_cleanup,
1271 .reset = drm_atomic_helper_connector_reset,
1272 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1273 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1276 static int tc_bridge_attach(struct drm_bridge *bridge)
1278 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1279 struct tc_data *tc = bridge_to_tc(bridge);
1280 struct drm_device *drm = bridge->dev;
1283 /* Create DP/eDP connector */
1284 drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
1285 ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs,
1286 tc->panel ? DRM_MODE_CONNECTOR_eDP :
1287 DRM_MODE_CONNECTOR_DisplayPort);
1291 /* Don't poll if don't have HPD connected */
1292 if (tc->hpd_pin >= 0) {
1294 tc->connector.polled = DRM_CONNECTOR_POLL_HPD;
1296 tc->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
1297 DRM_CONNECTOR_POLL_DISCONNECT;
1301 drm_panel_attach(tc->panel, &tc->connector);
1303 drm_display_info_set_bus_formats(&tc->connector.display_info,
1305 tc->connector.display_info.bus_flags =
1306 DRM_BUS_FLAG_DE_HIGH |
1307 DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE |
1308 DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
1309 drm_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
1314 static const struct drm_bridge_funcs tc_bridge_funcs = {
1315 .attach = tc_bridge_attach,
1316 .mode_valid = tc_mode_valid,
1317 .mode_set = tc_bridge_mode_set,
1318 .pre_enable = tc_bridge_pre_enable,
1319 .enable = tc_bridge_enable,
1320 .disable = tc_bridge_disable,
1321 .post_disable = tc_bridge_post_disable,
1322 .mode_fixup = tc_bridge_mode_fixup,
1325 static bool tc_readable_reg(struct device *dev, unsigned int reg)
1327 return reg != SYSCTRL;
1330 static const struct regmap_range tc_volatile_ranges[] = {
1331 regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
1332 regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
1333 regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
1334 regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
1335 regmap_reg_range(VFUEN0, VFUEN0),
1336 regmap_reg_range(INTSTS_G, INTSTS_G),
1337 regmap_reg_range(GPIOI, GPIOI),
1340 static const struct regmap_access_table tc_volatile_table = {
1341 .yes_ranges = tc_volatile_ranges,
1342 .n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
1345 static bool tc_writeable_reg(struct device *dev, unsigned int reg)
1347 return (reg != TC_IDREG) &&
1348 (reg != DP0_LTSTAT) &&
1349 (reg != DP0_SNKLTCHGREQ);
1352 static const struct regmap_config tc_regmap_config = {
1357 .max_register = PLL_DBG,
1358 .cache_type = REGCACHE_RBTREE,
1359 .readable_reg = tc_readable_reg,
1360 .volatile_table = &tc_volatile_table,
1361 .writeable_reg = tc_writeable_reg,
1362 .reg_format_endian = REGMAP_ENDIAN_BIG,
1363 .val_format_endian = REGMAP_ENDIAN_LITTLE,
1366 static irqreturn_t tc_irq_handler(int irq, void *arg)
1368 struct tc_data *tc = arg;
1372 r = regmap_read(tc->regmap, INTSTS_G, &val);
1379 if (val & INT_SYSERR) {
1382 regmap_read(tc->regmap, SYSSTAT, &stat);
1384 dev_err(tc->dev, "syserr %x\n", stat);
1387 if (tc->hpd_pin >= 0 && tc->bridge.dev) {
1389 * H is triggered when the GPIO goes high.
1391 * LC is triggered when the GPIO goes low and stays low for
1392 * the duration of LCNT
1394 bool h = val & INT_GPIO_H(tc->hpd_pin);
1395 bool lc = val & INT_GPIO_LC(tc->hpd_pin);
1397 dev_dbg(tc->dev, "GPIO%d: %s %s\n", tc->hpd_pin,
1398 h ? "H" : "", lc ? "LC" : "");
1401 drm_kms_helper_hotplug_event(tc->bridge.dev);
1404 regmap_write(tc->regmap, INTSTS_G, val);
1409 static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
1411 struct device *dev = &client->dev;
1415 tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
1421 /* port@2 is the output port */
1422 ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &tc->panel, NULL);
1423 if (ret && ret != -ENODEV)
1426 /* Shut down GPIO is optional */
1427 tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
1428 if (IS_ERR(tc->sd_gpio))
1429 return PTR_ERR(tc->sd_gpio);
1432 gpiod_set_value_cansleep(tc->sd_gpio, 0);
1433 usleep_range(5000, 10000);
1436 /* Reset GPIO is optional */
1437 tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
1438 if (IS_ERR(tc->reset_gpio))
1439 return PTR_ERR(tc->reset_gpio);
1441 if (tc->reset_gpio) {
1442 gpiod_set_value_cansleep(tc->reset_gpio, 1);
1443 usleep_range(5000, 10000);
1446 tc->refclk = devm_clk_get(dev, "ref");
1447 if (IS_ERR(tc->refclk)) {
1448 ret = PTR_ERR(tc->refclk);
1449 dev_err(dev, "Failed to get refclk: %d\n", ret);
1453 tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
1454 if (IS_ERR(tc->regmap)) {
1455 ret = PTR_ERR(tc->regmap);
1456 dev_err(dev, "Failed to initialize regmap: %d\n", ret);
1460 ret = of_property_read_u32(dev->of_node, "toshiba,hpd-pin",
1463 tc->hpd_pin = -ENODEV;
1465 if (tc->hpd_pin < 0 || tc->hpd_pin > 1) {
1466 dev_err(dev, "failed to parse HPD number\n");
1471 if (client->irq > 0) {
1473 regmap_write(tc->regmap, INTCTL_G, INT_SYSERR);
1475 ret = devm_request_threaded_irq(dev, client->irq,
1476 NULL, tc_irq_handler,
1478 "tc358767-irq", tc);
1480 dev_err(dev, "failed to register dp interrupt\n");
1484 tc->have_irq = true;
1487 ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
1489 dev_err(tc->dev, "can not read device ID: %d\n", ret);
1493 if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
1494 dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
1498 tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
1500 if (tc->hpd_pin >= 0) {
1501 u32 lcnt_reg = tc->hpd_pin == 0 ? INT_GP0_LCNT : INT_GP1_LCNT;
1502 u32 h_lc = INT_GPIO_H(tc->hpd_pin) | INT_GPIO_LC(tc->hpd_pin);
1504 /* Set LCNT to 2ms */
1505 regmap_write(tc->regmap, lcnt_reg,
1506 clk_get_rate(tc->refclk) * 2 / 1000);
1507 /* We need the "alternate" mode for HPD */
1508 regmap_write(tc->regmap, GPIOM, BIT(tc->hpd_pin));
1512 regmap_update_bits(tc->regmap, INTCTL_G, h_lc, h_lc);
1516 ret = tc_aux_link_setup(tc);
1520 /* Register DP AUX channel */
1521 tc->aux.name = "TC358767 AUX i2c adapter";
1522 tc->aux.dev = tc->dev;
1523 tc->aux.transfer = tc_aux_transfer;
1524 ret = drm_dp_aux_register(&tc->aux);
1528 tc->bridge.funcs = &tc_bridge_funcs;
1529 tc->bridge.of_node = dev->of_node;
1530 drm_bridge_add(&tc->bridge);
1532 i2c_set_clientdata(client, tc);
1537 static int tc_remove(struct i2c_client *client)
1539 struct tc_data *tc = i2c_get_clientdata(client);
1541 drm_bridge_remove(&tc->bridge);
1542 drm_dp_aux_unregister(&tc->aux);
1547 static const struct i2c_device_id tc358767_i2c_ids[] = {
1551 MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
1553 static const struct of_device_id tc358767_of_ids[] = {
1554 { .compatible = "toshiba,tc358767", },
1557 MODULE_DEVICE_TABLE(of, tc358767_of_ids);
1559 static struct i2c_driver tc358767_driver = {
1562 .of_match_table = tc358767_of_ids,
1564 .id_table = tc358767_i2c_ids,
1566 .remove = tc_remove,
1568 module_i2c_driver(tc358767_driver);
1571 MODULE_DESCRIPTION("tc358767 eDP encoder driver");
1572 MODULE_LICENSE("GPL");