Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * tc358767 eDP bridge driver
4 *
5 * Copyright (C) 2016 CogentEmbedded Inc
6 * Author: Andrey Gusakov <andrey.gusakov@cogentembedded.com>
7 *
8 * Copyright (C) 2016 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
9 *
10 * Copyright (C) 2016 Zodiac Inflight Innovations
11 *
12 * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c
13 *
14 * Copyright (C) 2012 Texas Instruments
15 * Author: Rob Clark <robdclark@gmail.com>
16 */
17
18#include <linux/bitfield.h>
19#include <linux/clk.h>
20#include <linux/device.h>
21#include <linux/gpio/consumer.h>
22#include <linux/i2c.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/regmap.h>
26#include <linux/slab.h>
27
28#include <drm/drm_atomic_helper.h>
29#include <drm/drm_bridge.h>
30#include <drm/drm_dp_helper.h>
31#include <drm/drm_edid.h>
32#include <drm/drm_of.h>
33#include <drm/drm_panel.h>
34#include <drm/drm_print.h>
35#include <drm/drm_probe_helper.h>
36
37/* Registers */
38
39/* Display Parallel Interface */
40#define DPIPXLFMT 0x0440
41#define VS_POL_ACTIVE_LOW (1 << 10)
42#define HS_POL_ACTIVE_LOW (1 << 9)
43#define DE_POL_ACTIVE_HIGH (0 << 8)
44#define SUB_CFG_TYPE_CONFIG1 (0 << 2) /* LSB aligned */
45#define SUB_CFG_TYPE_CONFIG2 (1 << 2) /* Loosely Packed */
46#define SUB_CFG_TYPE_CONFIG3 (2 << 2) /* LSB aligned 8-bit */
47#define DPI_BPP_RGB888 (0 << 0)
48#define DPI_BPP_RGB666 (1 << 0)
49#define DPI_BPP_RGB565 (2 << 0)
50
51/* Video Path */
52#define VPCTRL0 0x0450
53#define VSDELAY GENMASK(31, 20)
54#define OPXLFMT_RGB666 (0 << 8)
55#define OPXLFMT_RGB888 (1 << 8)
56#define FRMSYNC_DISABLED (0 << 4) /* Video Timing Gen Disabled */
57#define FRMSYNC_ENABLED (1 << 4) /* Video Timing Gen Enabled */
58#define MSF_DISABLED (0 << 0) /* Magic Square FRC disabled */
59#define MSF_ENABLED (1 << 0) /* Magic Square FRC enabled */
60#define HTIM01 0x0454
61#define HPW GENMASK(8, 0)
62#define HBPR GENMASK(24, 16)
63#define HTIM02 0x0458
64#define HDISPR GENMASK(10, 0)
65#define HFPR GENMASK(24, 16)
66#define VTIM01 0x045c
67#define VSPR GENMASK(7, 0)
68#define VBPR GENMASK(23, 16)
69#define VTIM02 0x0460
70#define VFPR GENMASK(23, 16)
71#define VDISPR GENMASK(10, 0)
72#define VFUEN0 0x0464
73#define VFUEN BIT(0) /* Video Frame Timing Upload */
74
75/* System */
76#define TC_IDREG 0x0500
77#define SYSSTAT 0x0508
78#define SYSCTRL 0x0510
79#define DP0_AUDSRC_NO_INPUT (0 << 3)
80#define DP0_AUDSRC_I2S_RX (1 << 3)
81#define DP0_VIDSRC_NO_INPUT (0 << 0)
82#define DP0_VIDSRC_DSI_RX (1 << 0)
83#define DP0_VIDSRC_DPI_RX (2 << 0)
84#define DP0_VIDSRC_COLOR_BAR (3 << 0)
85#define SYSRSTENB 0x050c
86#define ENBI2C (1 << 0)
87#define ENBLCD0 (1 << 2)
88#define ENBBM (1 << 3)
89#define ENBDSIRX (1 << 4)
90#define ENBREG (1 << 5)
91#define ENBHDCP (1 << 8)
92#define GPIOM 0x0540
93#define GPIOC 0x0544
94#define GPIOO 0x0548
95#define GPIOI 0x054c
96#define INTCTL_G 0x0560
97#define INTSTS_G 0x0564
98
99#define INT_SYSERR BIT(16)
100#define INT_GPIO_H(x) (1 << (x == 0 ? 2 : 10))
101#define INT_GPIO_LC(x) (1 << (x == 0 ? 3 : 11))
102
103#define INT_GP0_LCNT 0x0584
104#define INT_GP1_LCNT 0x0588
105
106/* Control */
107#define DP0CTL 0x0600
108#define VID_MN_GEN BIT(6) /* Auto-generate M/N values */
109#define EF_EN BIT(5) /* Enable Enhanced Framing */
110#define VID_EN BIT(1) /* Video transmission enable */
111#define DP_EN BIT(0) /* Enable DPTX function */
112
113/* Clocks */
114#define DP0_VIDMNGEN0 0x0610
115#define DP0_VIDMNGEN1 0x0614
116#define DP0_VMNGENSTATUS 0x0618
117
118/* Main Channel */
119#define DP0_SECSAMPLE 0x0640
120#define DP0_VIDSYNCDELAY 0x0644
121#define VID_SYNC_DLY GENMASK(15, 0)
122#define THRESH_DLY GENMASK(31, 16)
123
124#define DP0_TOTALVAL 0x0648
125#define H_TOTAL GENMASK(15, 0)
126#define V_TOTAL GENMASK(31, 16)
127#define DP0_STARTVAL 0x064c
128#define H_START GENMASK(15, 0)
129#define V_START GENMASK(31, 16)
130#define DP0_ACTIVEVAL 0x0650
131#define H_ACT GENMASK(15, 0)
132#define V_ACT GENMASK(31, 16)
133
134#define DP0_SYNCVAL 0x0654
135#define VS_WIDTH GENMASK(30, 16)
136#define HS_WIDTH GENMASK(14, 0)
137#define SYNCVAL_HS_POL_ACTIVE_LOW (1 << 15)
138#define SYNCVAL_VS_POL_ACTIVE_LOW (1 << 31)
139#define DP0_MISC 0x0658
140#define TU_SIZE_RECOMMENDED (63) /* LSCLK cycles per TU */
141#define MAX_TU_SYMBOL GENMASK(28, 23)
142#define TU_SIZE GENMASK(21, 16)
143#define BPC_6 (0 << 5)
144#define BPC_8 (1 << 5)
145
146/* AUX channel */
147#define DP0_AUXCFG0 0x0660
148#define DP0_AUXCFG0_BSIZE GENMASK(11, 8)
149#define DP0_AUXCFG0_ADDR_ONLY BIT(4)
150#define DP0_AUXCFG1 0x0664
151#define AUX_RX_FILTER_EN BIT(16)
152
153#define DP0_AUXADDR 0x0668
154#define DP0_AUXWDATA(i) (0x066c + (i) * 4)
155#define DP0_AUXRDATA(i) (0x067c + (i) * 4)
156#define DP0_AUXSTATUS 0x068c
157#define AUX_BYTES GENMASK(15, 8)
158#define AUX_STATUS GENMASK(7, 4)
159#define AUX_TIMEOUT BIT(1)
160#define AUX_BUSY BIT(0)
161#define DP0_AUXI2CADR 0x0698
162
163/* Link Training */
164#define DP0_SRCCTRL 0x06a0
165#define DP0_SRCCTRL_SCRMBLDIS BIT(13)
166#define DP0_SRCCTRL_EN810B BIT(12)
167#define DP0_SRCCTRL_NOTP (0 << 8)
168#define DP0_SRCCTRL_TP1 (1 << 8)
169#define DP0_SRCCTRL_TP2 (2 << 8)
170#define DP0_SRCCTRL_LANESKEW BIT(7)
171#define DP0_SRCCTRL_SSCG BIT(3)
172#define DP0_SRCCTRL_LANES_1 (0 << 2)
173#define DP0_SRCCTRL_LANES_2 (1 << 2)
174#define DP0_SRCCTRL_BW27 (1 << 1)
175#define DP0_SRCCTRL_BW162 (0 << 1)
176#define DP0_SRCCTRL_AUTOCORRECT BIT(0)
177#define DP0_LTSTAT 0x06d0
178#define LT_LOOPDONE BIT(13)
179#define LT_STATUS_MASK (0x1f << 8)
180#define LT_CHANNEL1_EQ_BITS (DP_CHANNEL_EQ_BITS << 4)
181#define LT_INTERLANE_ALIGN_DONE BIT(3)
182#define LT_CHANNEL0_EQ_BITS (DP_CHANNEL_EQ_BITS)
183#define DP0_SNKLTCHGREQ 0x06d4
184#define DP0_LTLOOPCTRL 0x06d8
185#define DP0_SNKLTCTRL 0x06e4
186
187#define DP1_SRCCTRL 0x07a0
188
189/* PHY */
190#define DP_PHY_CTRL 0x0800
191#define DP_PHY_RST BIT(28) /* DP PHY Global Soft Reset */
192#define BGREN BIT(25) /* AUX PHY BGR Enable */
193#define PWR_SW_EN BIT(24) /* PHY Power Switch Enable */
194#define PHY_M1_RST BIT(12) /* Reset PHY1 Main Channel */
195#define PHY_RDY BIT(16) /* PHY Main Channels Ready */
196#define PHY_M0_RST BIT(8) /* Reset PHY0 Main Channel */
197#define PHY_2LANE BIT(2) /* PHY Enable 2 lanes */
198#define PHY_A0_EN BIT(1) /* PHY Aux Channel0 Enable */
199#define PHY_M0_EN BIT(0) /* PHY Main Channel0 Enable */
200
201/* PLL */
202#define DP0_PLLCTRL 0x0900
203#define DP1_PLLCTRL 0x0904 /* not defined in DS */
204#define PXL_PLLCTRL 0x0908
205#define PLLUPDATE BIT(2)
206#define PLLBYP BIT(1)
207#define PLLEN BIT(0)
208#define PXL_PLLPARAM 0x0914
209#define IN_SEL_REFCLK (0 << 14)
210#define SYS_PLLPARAM 0x0918
211#define REF_FREQ_38M4 (0 << 8) /* 38.4 MHz */
212#define REF_FREQ_19M2 (1 << 8) /* 19.2 MHz */
213#define REF_FREQ_26M (2 << 8) /* 26 MHz */
214#define REF_FREQ_13M (3 << 8) /* 13 MHz */
215#define SYSCLK_SEL_LSCLK (0 << 4)
216#define LSCLK_DIV_1 (0 << 0)
217#define LSCLK_DIV_2 (1 << 0)
218
219/* Test & Debug */
220#define TSTCTL 0x0a00
221#define COLOR_R GENMASK(31, 24)
222#define COLOR_G GENMASK(23, 16)
223#define COLOR_B GENMASK(15, 8)
224#define ENI2CFILTER BIT(4)
225#define COLOR_BAR_MODE GENMASK(1, 0)
226#define COLOR_BAR_MODE_BARS 2
227#define PLL_DBG 0x0a04
228
229static bool tc_test_pattern;
230module_param_named(test, tc_test_pattern, bool, 0644);
231
232struct tc_edp_link {
233 u8 dpcd[DP_RECEIVER_CAP_SIZE];
234 unsigned int rate;
235 u8 num_lanes;
236 u8 assr;
237 bool scrambler_dis;
238 bool spread;
239};
240
241struct tc_data {
242 struct device *dev;
243 struct regmap *regmap;
244 struct drm_dp_aux aux;
245
246 struct drm_bridge bridge;
247 struct drm_connector connector;
248 struct drm_panel *panel;
249
250 /* link settings */
251 struct tc_edp_link link;
252
253 /* display edid */
254 struct edid *edid;
255 /* current mode */
256 struct drm_display_mode mode;
257
258 u32 rev;
259 u8 assr;
260
261 struct gpio_desc *sd_gpio;
262 struct gpio_desc *reset_gpio;
263 struct clk *refclk;
264
265 /* do we have IRQ */
266 bool have_irq;
267
268 /* HPD pin number (0 or 1) or -ENODEV */
269 int hpd_pin;
270};
271
272static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
273{
274 return container_of(a, struct tc_data, aux);
275}
276
277static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
278{
279 return container_of(b, struct tc_data, bridge);
280}
281
282static inline struct tc_data *connector_to_tc(struct drm_connector *c)
283{
284 return container_of(c, struct tc_data, connector);
285}
286
287static inline int tc_poll_timeout(struct tc_data *tc, unsigned int addr,
288 unsigned int cond_mask,
289 unsigned int cond_value,
290 unsigned long sleep_us, u64 timeout_us)
291{
292 unsigned int val;
293
294 return regmap_read_poll_timeout(tc->regmap, addr, val,
295 (val & cond_mask) == cond_value,
296 sleep_us, timeout_us);
297}
298
299static int tc_aux_wait_busy(struct tc_data *tc)
300{
301 return tc_poll_timeout(tc, DP0_AUXSTATUS, AUX_BUSY, 0, 100, 100000);
302}
303
304static int tc_aux_write_data(struct tc_data *tc, const void *data,
305 size_t size)
306{
307 u32 auxwdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)] = { 0 };
308 int ret, count = ALIGN(size, sizeof(u32));
309
310 memcpy(auxwdata, data, size);
311
312 ret = regmap_raw_write(tc->regmap, DP0_AUXWDATA(0), auxwdata, count);
313 if (ret)
314 return ret;
315
316 return size;
317}
318
319static int tc_aux_read_data(struct tc_data *tc, void *data, size_t size)
320{
321 u32 auxrdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)];
322 int ret, count = ALIGN(size, sizeof(u32));
323
324 ret = regmap_raw_read(tc->regmap, DP0_AUXRDATA(0), auxrdata, count);
325 if (ret)
326 return ret;
327
328 memcpy(data, auxrdata, size);
329
330 return size;
331}
332
333static u32 tc_auxcfg0(struct drm_dp_aux_msg *msg, size_t size)
334{
335 u32 auxcfg0 = msg->request;
336
337 if (size)
338 auxcfg0 |= FIELD_PREP(DP0_AUXCFG0_BSIZE, size - 1);
339 else
340 auxcfg0 |= DP0_AUXCFG0_ADDR_ONLY;
341
342 return auxcfg0;
343}
344
345static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
346 struct drm_dp_aux_msg *msg)
347{
348 struct tc_data *tc = aux_to_tc(aux);
349 size_t size = min_t(size_t, DP_AUX_MAX_PAYLOAD_BYTES - 1, msg->size);
350 u8 request = msg->request & ~DP_AUX_I2C_MOT;
351 u32 auxstatus;
352 int ret;
353
354 ret = tc_aux_wait_busy(tc);
355 if (ret)
356 return ret;
357
358 switch (request) {
359 case DP_AUX_NATIVE_READ:
360 case DP_AUX_I2C_READ:
361 break;
362 case DP_AUX_NATIVE_WRITE:
363 case DP_AUX_I2C_WRITE:
364 if (size) {
365 ret = tc_aux_write_data(tc, msg->buffer, size);
366 if (ret < 0)
367 return ret;
368 }
369 break;
370 default:
371 return -EINVAL;
372 }
373
374 /* Store address */
375 ret = regmap_write(tc->regmap, DP0_AUXADDR, msg->address);
376 if (ret)
377 return ret;
378 /* Start transfer */
379 ret = regmap_write(tc->regmap, DP0_AUXCFG0, tc_auxcfg0(msg, size));
380 if (ret)
381 return ret;
382
383 ret = tc_aux_wait_busy(tc);
384 if (ret)
385 return ret;
386
387 ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &auxstatus);
388 if (ret)
389 return ret;
390
391 if (auxstatus & AUX_TIMEOUT)
392 return -ETIMEDOUT;
393 /*
394 * For some reason address-only DP_AUX_I2C_WRITE (MOT), still
395 * reports 1 byte transferred in its status. To deal we that
396 * we ignore aux_bytes field if we know that this was an
397 * address-only transfer
398 */
399 if (size)
400 size = FIELD_GET(AUX_BYTES, auxstatus);
401 msg->reply = FIELD_GET(AUX_STATUS, auxstatus);
402
403 switch (request) {
404 case DP_AUX_NATIVE_READ:
405 case DP_AUX_I2C_READ:
406 if (size)
407 return tc_aux_read_data(tc, msg->buffer, size);
408 break;
409 }
410
411 return size;
412}
413
414static const char * const training_pattern1_errors[] = {
415 "No errors",
416 "Aux write error",
417 "Aux read error",
418 "Max voltage reached error",
419 "Loop counter expired error",
420 "res", "res", "res"
421};
422
423static const char * const training_pattern2_errors[] = {
424 "No errors",
425 "Aux write error",
426 "Aux read error",
427 "Clock recovery failed error",
428 "Loop counter expired error",
429 "res", "res", "res"
430};
431
432static u32 tc_srcctrl(struct tc_data *tc)
433{
434 /*
435 * No training pattern, skew lane 1 data by two LSCLK cycles with
436 * respect to lane 0 data, AutoCorrect Mode = 0
437 */
438 u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_EN810B;
439
440 if (tc->link.scrambler_dis)
441 reg |= DP0_SRCCTRL_SCRMBLDIS; /* Scrambler Disabled */
442 if (tc->link.spread)
443 reg |= DP0_SRCCTRL_SSCG; /* Spread Spectrum Enable */
444 if (tc->link.num_lanes == 2)
445 reg |= DP0_SRCCTRL_LANES_2; /* Two Main Channel Lanes */
446 if (tc->link.rate != 162000)
447 reg |= DP0_SRCCTRL_BW27; /* 2.7 Gbps link */
448 return reg;
449}
450
451static int tc_pllupdate(struct tc_data *tc, unsigned int pllctrl)
452{
453 int ret;
454
455 ret = regmap_write(tc->regmap, pllctrl, PLLUPDATE | PLLEN);
456 if (ret)
457 return ret;
458
459 /* Wait for PLL to lock: up to 2.09 ms, depending on refclk */
460 usleep_range(3000, 6000);
461
462 return 0;
463}
464
465static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
466{
467 int ret;
468 int i_pre, best_pre = 1;
469 int i_post, best_post = 1;
470 int div, best_div = 1;
471 int mul, best_mul = 1;
472 int delta, best_delta;
473 int ext_div[] = {1, 2, 3, 5, 7};
474 int best_pixelclock = 0;
475 int vco_hi = 0;
476 u32 pxl_pllparam;
477
478 dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
479 refclk);
480 best_delta = pixelclock;
481 /* Loop over all possible ext_divs, skipping invalid configurations */
482 for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
483 /*
484 * refclk / ext_pre_div should be in the 1 to 200 MHz range.
485 * We don't allow any refclk > 200 MHz, only check lower bounds.
486 */
487 if (refclk / ext_div[i_pre] < 1000000)
488 continue;
489 for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
490 for (div = 1; div <= 16; div++) {
491 u32 clk;
492 u64 tmp;
493
494 tmp = pixelclock * ext_div[i_pre] *
495 ext_div[i_post] * div;
496 do_div(tmp, refclk);
497 mul = tmp;
498
499 /* Check limits */
500 if ((mul < 1) || (mul > 128))
501 continue;
502
503 clk = (refclk / ext_div[i_pre] / div) * mul;
504 /*
505 * refclk * mul / (ext_pre_div * pre_div)
506 * should be in the 150 to 650 MHz range
507 */
508 if ((clk > 650000000) || (clk < 150000000))
509 continue;
510
511 clk = clk / ext_div[i_post];
512 delta = clk - pixelclock;
513
514 if (abs(delta) < abs(best_delta)) {
515 best_pre = i_pre;
516 best_post = i_post;
517 best_div = div;
518 best_mul = mul;
519 best_delta = delta;
520 best_pixelclock = clk;
521 }
522 }
523 }
524 }
525 if (best_pixelclock == 0) {
526 dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
527 pixelclock);
528 return -EINVAL;
529 }
530
531 dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
532 best_delta);
533 dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
534 ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
535
536 /* if VCO >= 300 MHz */
537 if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
538 vco_hi = 1;
539 /* see DS */
540 if (best_div == 16)
541 best_div = 0;
542 if (best_mul == 128)
543 best_mul = 0;
544
545 /* Power up PLL and switch to bypass */
546 ret = regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP | PLLEN);
547 if (ret)
548 return ret;
549
550 pxl_pllparam = vco_hi << 24; /* For PLL VCO >= 300 MHz = 1 */
551 pxl_pllparam |= ext_div[best_pre] << 20; /* External Pre-divider */
552 pxl_pllparam |= ext_div[best_post] << 16; /* External Post-divider */
553 pxl_pllparam |= IN_SEL_REFCLK; /* Use RefClk as PLL input */
554 pxl_pllparam |= best_div << 8; /* Divider for PLL RefClk */
555 pxl_pllparam |= best_mul; /* Multiplier for PLL */
556
557 ret = regmap_write(tc->regmap, PXL_PLLPARAM, pxl_pllparam);
558 if (ret)
559 return ret;
560
561 /* Force PLL parameter update and disable bypass */
562 return tc_pllupdate(tc, PXL_PLLCTRL);
563}
564
565static int tc_pxl_pll_dis(struct tc_data *tc)
566{
567 /* Enable PLL bypass, power down PLL */
568 return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
569}
570
571static int tc_stream_clock_calc(struct tc_data *tc)
572{
573 /*
574 * If the Stream clock and Link Symbol clock are
575 * asynchronous with each other, the value of M changes over
576 * time. This way of generating link clock and stream
577 * clock is called Asynchronous Clock mode. The value M
578 * must change while the value N stays constant. The
579 * value of N in this Asynchronous Clock mode must be set
580 * to 2^15 or 32,768.
581 *
582 * LSCLK = 1/10 of high speed link clock
583 *
584 * f_STRMCLK = M/N * f_LSCLK
585 * M/N = f_STRMCLK / f_LSCLK
586 *
587 */
588 return regmap_write(tc->regmap, DP0_VIDMNGEN1, 32768);
589}
590
591static int tc_set_syspllparam(struct tc_data *tc)
592{
593 unsigned long rate;
594 u32 pllparam = SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
595
596 rate = clk_get_rate(tc->refclk);
597 switch (rate) {
598 case 38400000:
599 pllparam |= REF_FREQ_38M4;
600 break;
601 case 26000000:
602 pllparam |= REF_FREQ_26M;
603 break;
604 case 19200000:
605 pllparam |= REF_FREQ_19M2;
606 break;
607 case 13000000:
608 pllparam |= REF_FREQ_13M;
609 break;
610 default:
611 dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
612 return -EINVAL;
613 }
614
615 return regmap_write(tc->regmap, SYS_PLLPARAM, pllparam);
616}
617
618static int tc_aux_link_setup(struct tc_data *tc)
619{
620 int ret;
621 u32 dp0_auxcfg1;
622
623 /* Setup DP-PHY / PLL */
624 ret = tc_set_syspllparam(tc);
625 if (ret)
626 goto err;
627
628 ret = regmap_write(tc->regmap, DP_PHY_CTRL,
629 BGREN | PWR_SW_EN | PHY_A0_EN);
630 if (ret)
631 goto err;
632 /*
633 * Initially PLLs are in bypass. Force PLL parameter update,
634 * disable PLL bypass, enable PLL
635 */
636 ret = tc_pllupdate(tc, DP0_PLLCTRL);
637 if (ret)
638 goto err;
639
640 ret = tc_pllupdate(tc, DP1_PLLCTRL);
641 if (ret)
642 goto err;
643
644 ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 100, 100000);
645 if (ret == -ETIMEDOUT) {
646 dev_err(tc->dev, "Timeout waiting for PHY to become ready");
647 return ret;
648 } else if (ret) {
649 goto err;
650 }
651
652 /* Setup AUX link */
653 dp0_auxcfg1 = AUX_RX_FILTER_EN;
654 dp0_auxcfg1 |= 0x06 << 8; /* Aux Bit Period Calculator Threshold */
655 dp0_auxcfg1 |= 0x3f << 0; /* Aux Response Timeout Timer */
656
657 ret = regmap_write(tc->regmap, DP0_AUXCFG1, dp0_auxcfg1);
658 if (ret)
659 goto err;
660
661 return 0;
662err:
663 dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
664 return ret;
665}
666
667static int tc_get_display_props(struct tc_data *tc)
668{
669 u8 revision, num_lanes;
670 unsigned int rate;
671 int ret;
672 u8 reg;
673
674 /* Read DP Rx Link Capability */
675 ret = drm_dp_dpcd_read(&tc->aux, DP_DPCD_REV, tc->link.dpcd,
676 DP_RECEIVER_CAP_SIZE);
677 if (ret < 0)
678 goto err_dpcd_read;
679
680 revision = tc->link.dpcd[DP_DPCD_REV];
681 rate = drm_dp_max_link_rate(tc->link.dpcd);
682 num_lanes = drm_dp_max_lane_count(tc->link.dpcd);
683
684 if (rate != 162000 && rate != 270000) {
685 dev_dbg(tc->dev, "Falling to 2.7 Gbps rate\n");
686 rate = 270000;
687 }
688
689 tc->link.rate = rate;
690
691 if (num_lanes > 2) {
692 dev_dbg(tc->dev, "Falling to 2 lanes\n");
693 num_lanes = 2;
694 }
695
696 tc->link.num_lanes = num_lanes;
697
698 ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, ®);
699 if (ret < 0)
700 goto err_dpcd_read;
701 tc->link.spread = reg & DP_MAX_DOWNSPREAD_0_5;
702
703 ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, ®);
704 if (ret < 0)
705 goto err_dpcd_read;
706
707 tc->link.scrambler_dis = false;
708 /* read assr */
709 ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, ®);
710 if (ret < 0)
711 goto err_dpcd_read;
712 tc->link.assr = reg & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
713
714 dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
715 revision >> 4, revision & 0x0f,
716 (tc->link.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
717 tc->link.num_lanes,
718 drm_dp_enhanced_frame_cap(tc->link.dpcd) ?
719 "enhanced" : "default");
720 dev_dbg(tc->dev, "Downspread: %s, scrambler: %s\n",
721 tc->link.spread ? "0.5%" : "0.0%",
722 tc->link.scrambler_dis ? "disabled" : "enabled");
723 dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
724 tc->link.assr, tc->assr);
725
726 return 0;
727
728err_dpcd_read:
729 dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
730 return ret;
731}
732
733static int tc_set_video_mode(struct tc_data *tc,
734 const struct drm_display_mode *mode)
735{
736 int ret;
737 int vid_sync_dly;
738 int max_tu_symbol;
739
740 int left_margin = mode->htotal - mode->hsync_end;
741 int right_margin = mode->hsync_start - mode->hdisplay;
742 int hsync_len = mode->hsync_end - mode->hsync_start;
743 int upper_margin = mode->vtotal - mode->vsync_end;
744 int lower_margin = mode->vsync_start - mode->vdisplay;
745 int vsync_len = mode->vsync_end - mode->vsync_start;
746 u32 dp0_syncval;
747 u32 bits_per_pixel = 24;
748 u32 in_bw, out_bw;
749
750 /*
751 * Recommended maximum number of symbols transferred in a transfer unit:
752 * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
753 * (output active video bandwidth in bytes))
754 * Must be less than tu_size.
755 */
756
757 in_bw = mode->clock * bits_per_pixel / 8;
758 out_bw = tc->link.num_lanes * tc->link.rate;
759 max_tu_symbol = DIV_ROUND_UP(in_bw * TU_SIZE_RECOMMENDED, out_bw);
760
761 dev_dbg(tc->dev, "set mode %dx%d\n",
762 mode->hdisplay, mode->vdisplay);
763 dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
764 left_margin, right_margin, hsync_len);
765 dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
766 upper_margin, lower_margin, vsync_len);
767 dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
768
769
770 /*
771 * LCD Ctl Frame Size
772 * datasheet is not clear of vsdelay in case of DPI
773 * assume we do not need any delay when DPI is a source of
774 * sync signals
775 */
776 ret = regmap_write(tc->regmap, VPCTRL0,
777 FIELD_PREP(VSDELAY, 0) |
778 OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
779 if (ret)
780 return ret;
781
782 ret = regmap_write(tc->regmap, HTIM01,
783 FIELD_PREP(HBPR, ALIGN(left_margin, 2)) |
784 FIELD_PREP(HPW, ALIGN(hsync_len, 2)));
785 if (ret)
786 return ret;
787
788 ret = regmap_write(tc->regmap, HTIM02,
789 FIELD_PREP(HDISPR, ALIGN(mode->hdisplay, 2)) |
790 FIELD_PREP(HFPR, ALIGN(right_margin, 2)));
791 if (ret)
792 return ret;
793
794 ret = regmap_write(tc->regmap, VTIM01,
795 FIELD_PREP(VBPR, upper_margin) |
796 FIELD_PREP(VSPR, vsync_len));
797 if (ret)
798 return ret;
799
800 ret = regmap_write(tc->regmap, VTIM02,
801 FIELD_PREP(VFPR, lower_margin) |
802 FIELD_PREP(VDISPR, mode->vdisplay));
803 if (ret)
804 return ret;
805
806 ret = regmap_write(tc->regmap, VFUEN0, VFUEN); /* update settings */
807 if (ret)
808 return ret;
809
810 /* Test pattern settings */
811 ret = regmap_write(tc->regmap, TSTCTL,
812 FIELD_PREP(COLOR_R, 120) |
813 FIELD_PREP(COLOR_G, 20) |
814 FIELD_PREP(COLOR_B, 99) |
815 ENI2CFILTER |
816 FIELD_PREP(COLOR_BAR_MODE, COLOR_BAR_MODE_BARS));
817 if (ret)
818 return ret;
819
820 /* DP Main Stream Attributes */
821 vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
822 ret = regmap_write(tc->regmap, DP0_VIDSYNCDELAY,
823 FIELD_PREP(THRESH_DLY, max_tu_symbol) |
824 FIELD_PREP(VID_SYNC_DLY, vid_sync_dly));
825
826 ret = regmap_write(tc->regmap, DP0_TOTALVAL,
827 FIELD_PREP(H_TOTAL, mode->htotal) |
828 FIELD_PREP(V_TOTAL, mode->vtotal));
829 if (ret)
830 return ret;
831
832 ret = regmap_write(tc->regmap, DP0_STARTVAL,
833 FIELD_PREP(H_START, left_margin + hsync_len) |
834 FIELD_PREP(V_START, upper_margin + vsync_len));
835 if (ret)
836 return ret;
837
838 ret = regmap_write(tc->regmap, DP0_ACTIVEVAL,
839 FIELD_PREP(V_ACT, mode->vdisplay) |
840 FIELD_PREP(H_ACT, mode->hdisplay));
841 if (ret)
842 return ret;
843
844 dp0_syncval = FIELD_PREP(VS_WIDTH, vsync_len) |
845 FIELD_PREP(HS_WIDTH, hsync_len);
846
847 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
848 dp0_syncval |= SYNCVAL_VS_POL_ACTIVE_LOW;
849
850 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
851 dp0_syncval |= SYNCVAL_HS_POL_ACTIVE_LOW;
852
853 ret = regmap_write(tc->regmap, DP0_SYNCVAL, dp0_syncval);
854 if (ret)
855 return ret;
856
857 ret = regmap_write(tc->regmap, DPIPXLFMT,
858 VS_POL_ACTIVE_LOW | HS_POL_ACTIVE_LOW |
859 DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 |
860 DPI_BPP_RGB888);
861 if (ret)
862 return ret;
863
864 ret = regmap_write(tc->regmap, DP0_MISC,
865 FIELD_PREP(MAX_TU_SYMBOL, max_tu_symbol) |
866 FIELD_PREP(TU_SIZE, TU_SIZE_RECOMMENDED) |
867 BPC_8);
868 if (ret)
869 return ret;
870
871 return 0;
872}
873
874static int tc_wait_link_training(struct tc_data *tc)
875{
876 u32 value;
877 int ret;
878
879 ret = tc_poll_timeout(tc, DP0_LTSTAT, LT_LOOPDONE,
880 LT_LOOPDONE, 500, 100000);
881 if (ret) {
882 dev_err(tc->dev, "Link training timeout waiting for LT_LOOPDONE!\n");
883 return ret;
884 }
885
886 ret = regmap_read(tc->regmap, DP0_LTSTAT, &value);
887 if (ret)
888 return ret;
889
890 return (value >> 8) & 0x7;
891}
892
893static int tc_main_link_enable(struct tc_data *tc)
894{
895 struct drm_dp_aux *aux = &tc->aux;
896 struct device *dev = tc->dev;
897 u32 dp_phy_ctrl;
898 u32 value;
899 int ret;
900 u8 tmp[DP_LINK_STATUS_SIZE];
901
902 dev_dbg(tc->dev, "link enable\n");
903
904 ret = regmap_read(tc->regmap, DP0CTL, &value);
905 if (ret)
906 return ret;
907
908 if (WARN_ON(value & DP_EN)) {
909 ret = regmap_write(tc->regmap, DP0CTL, 0);
910 if (ret)
911 return ret;
912 }
913
914 ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc));
915 if (ret)
916 return ret;
917 /* SSCG and BW27 on DP1 must be set to the same as on DP0 */
918 ret = regmap_write(tc->regmap, DP1_SRCCTRL,
919 (tc->link.spread ? DP0_SRCCTRL_SSCG : 0) |
920 ((tc->link.rate != 162000) ? DP0_SRCCTRL_BW27 : 0));
921 if (ret)
922 return ret;
923
924 ret = tc_set_syspllparam(tc);
925 if (ret)
926 return ret;
927
928 /* Setup Main Link */
929 dp_phy_ctrl = BGREN | PWR_SW_EN | PHY_A0_EN | PHY_M0_EN;
930 if (tc->link.num_lanes == 2)
931 dp_phy_ctrl |= PHY_2LANE;
932
933 ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
934 if (ret)
935 return ret;
936
937 /* PLL setup */
938 ret = tc_pllupdate(tc, DP0_PLLCTRL);
939 if (ret)
940 return ret;
941
942 ret = tc_pllupdate(tc, DP1_PLLCTRL);
943 if (ret)
944 return ret;
945
946 /* Reset/Enable Main Links */
947 dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST;
948 ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
949 usleep_range(100, 200);
950 dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
951 ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl);
952
953 ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 500, 100000);
954 if (ret) {
955 dev_err(dev, "timeout waiting for phy become ready");
956 return ret;
957 }
958
959 /* Set misc: 8 bits per color */
960 ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
961 if (ret)
962 return ret;
963
964 /*
965 * ASSR mode
966 * on TC358767 side ASSR configured through strap pin
967 * seems there is no way to change this setting from SW
968 *
969 * check is tc configured for same mode
970 */
971 if (tc->assr != tc->link.assr) {
972 dev_dbg(dev, "Trying to set display to ASSR: %d\n",
973 tc->assr);
974 /* try to set ASSR on display side */
975 tmp[0] = tc->assr;
976 ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
977 if (ret < 0)
978 goto err_dpcd_read;
979 /* read back */
980 ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
981 if (ret < 0)
982 goto err_dpcd_read;
983
984 if (tmp[0] != tc->assr) {
985 dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
986 tc->assr);
987 /* trying with disabled scrambler */
988 tc->link.scrambler_dis = true;
989 }
990 }
991
992 /* Setup Link & DPRx Config for Training */
993 tmp[0] = drm_dp_link_rate_to_bw_code(tc->link.rate);
994 tmp[1] = tc->link.num_lanes;
995
996 if (drm_dp_enhanced_frame_cap(tc->link.dpcd))
997 tmp[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
998
999 ret = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, tmp, 2);
1000 if (ret < 0)
1001 goto err_dpcd_write;
1002
1003 /* DOWNSPREAD_CTRL */
1004 tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
1005 /* MAIN_LINK_CHANNEL_CODING_SET */
1006 tmp[1] = DP_SET_ANSI_8B10B;
1007 ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
1008 if (ret < 0)
1009 goto err_dpcd_write;
1010
1011 /* Reset voltage-swing & pre-emphasis */
1012 tmp[0] = tmp[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 |
1013 DP_TRAIN_PRE_EMPH_LEVEL_0;
1014 ret = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, tmp, 2);
1015 if (ret < 0)
1016 goto err_dpcd_write;
1017
1018 /* Clock-Recovery */
1019
1020 /* Set DPCD 0x102 for Training Pattern 1 */
1021 ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
1022 DP_LINK_SCRAMBLING_DISABLE |
1023 DP_TRAINING_PATTERN_1);
1024 if (ret)
1025 return ret;
1026
1027 ret = regmap_write(tc->regmap, DP0_LTLOOPCTRL,
1028 (15 << 28) | /* Defer Iteration Count */
1029 (15 << 24) | /* Loop Iteration Count */
1030 (0xd << 0)); /* Loop Timer Delay */
1031 if (ret)
1032 return ret;
1033
1034 ret = regmap_write(tc->regmap, DP0_SRCCTRL,
1035 tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
1036 DP0_SRCCTRL_AUTOCORRECT |
1037 DP0_SRCCTRL_TP1);
1038 if (ret)
1039 return ret;
1040
1041 /* Enable DP0 to start Link Training */
1042 ret = regmap_write(tc->regmap, DP0CTL,
1043 (drm_dp_enhanced_frame_cap(tc->link.dpcd) ?
1044 EF_EN : 0) | DP_EN);
1045 if (ret)
1046 return ret;
1047
1048 /* wait */
1049
1050 ret = tc_wait_link_training(tc);
1051 if (ret < 0)
1052 return ret;
1053
1054 if (ret) {
1055 dev_err(tc->dev, "Link training phase 1 failed: %s\n",
1056 training_pattern1_errors[ret]);
1057 return -ENODEV;
1058 }
1059
1060 /* Channel Equalization */
1061
1062 /* Set DPCD 0x102 for Training Pattern 2 */
1063 ret = regmap_write(tc->regmap, DP0_SNKLTCTRL,
1064 DP_LINK_SCRAMBLING_DISABLE |
1065 DP_TRAINING_PATTERN_2);
1066 if (ret)
1067 return ret;
1068
1069 ret = regmap_write(tc->regmap, DP0_SRCCTRL,
1070 tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
1071 DP0_SRCCTRL_AUTOCORRECT |
1072 DP0_SRCCTRL_TP2);
1073 if (ret)
1074 return ret;
1075
1076 /* wait */
1077 ret = tc_wait_link_training(tc);
1078 if (ret < 0)
1079 return ret;
1080
1081 if (ret) {
1082 dev_err(tc->dev, "Link training phase 2 failed: %s\n",
1083 training_pattern2_errors[ret]);
1084 return -ENODEV;
1085 }
1086
1087 /*
1088 * Toshiba's documentation suggests to first clear DPCD 0x102, then
1089 * clear the training pattern bit in DP0_SRCCTRL. Testing shows
1090 * that the link sometimes drops if those steps are done in that order,
1091 * but if the steps are done in reverse order, the link stays up.
1092 *
1093 * So we do the steps differently than documented here.
1094 */
1095
1096 /* Clear Training Pattern, set AutoCorrect Mode = 1 */
1097 ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc) |
1098 DP0_SRCCTRL_AUTOCORRECT);
1099 if (ret)
1100 return ret;
1101
1102 /* Clear DPCD 0x102 */
1103 /* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
1104 tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
1105 ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
1106 if (ret < 0)
1107 goto err_dpcd_write;
1108
1109 /* Check link status */
1110 ret = drm_dp_dpcd_read_link_status(aux, tmp);
1111 if (ret < 0)
1112 goto err_dpcd_read;
1113
1114 ret = 0;
1115
1116 value = tmp[0] & DP_CHANNEL_EQ_BITS;
1117
1118 if (value != DP_CHANNEL_EQ_BITS) {
1119 dev_err(tc->dev, "Lane 0 failed: %x\n", value);
1120 ret = -ENODEV;
1121 }
1122
1123 if (tc->link.num_lanes == 2) {
1124 value = (tmp[0] >> 4) & DP_CHANNEL_EQ_BITS;
1125
1126 if (value != DP_CHANNEL_EQ_BITS) {
1127 dev_err(tc->dev, "Lane 1 failed: %x\n", value);
1128 ret = -ENODEV;
1129 }
1130
1131 if (!(tmp[2] & DP_INTERLANE_ALIGN_DONE)) {
1132 dev_err(tc->dev, "Interlane align failed\n");
1133 ret = -ENODEV;
1134 }
1135 }
1136
1137 if (ret) {
1138 dev_err(dev, "0x0202 LANE0_1_STATUS: 0x%02x\n", tmp[0]);
1139 dev_err(dev, "0x0203 LANE2_3_STATUS 0x%02x\n", tmp[1]);
1140 dev_err(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n", tmp[2]);
1141 dev_err(dev, "0x0205 SINK_STATUS: 0x%02x\n", tmp[3]);
1142 dev_err(dev, "0x0206 ADJUST_REQUEST_LANE0_1: 0x%02x\n", tmp[4]);
1143 dev_err(dev, "0x0207 ADJUST_REQUEST_LANE2_3: 0x%02x\n", tmp[5]);
1144 return ret;
1145 }
1146
1147 return 0;
1148err_dpcd_read:
1149 dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
1150 return ret;
1151err_dpcd_write:
1152 dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
1153 return ret;
1154}
1155
1156static int tc_main_link_disable(struct tc_data *tc)
1157{
1158 int ret;
1159
1160 dev_dbg(tc->dev, "link disable\n");
1161
1162 ret = regmap_write(tc->regmap, DP0_SRCCTRL, 0);
1163 if (ret)
1164 return ret;
1165
1166 return regmap_write(tc->regmap, DP0CTL, 0);
1167}
1168
1169static int tc_stream_enable(struct tc_data *tc)
1170{
1171 int ret;
1172 u32 value;
1173
1174 dev_dbg(tc->dev, "enable video stream\n");
1175
1176 /* PXL PLL setup */
1177 if (tc_test_pattern) {
1178 ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
1179 1000 * tc->mode.clock);
1180 if (ret)
1181 return ret;
1182 }
1183
1184 ret = tc_set_video_mode(tc, &tc->mode);
1185 if (ret)
1186 return ret;
1187
1188 /* Set M/N */
1189 ret = tc_stream_clock_calc(tc);
1190 if (ret)
1191 return ret;
1192
1193 value = VID_MN_GEN | DP_EN;
1194 if (drm_dp_enhanced_frame_cap(tc->link.dpcd))
1195 value |= EF_EN;
1196 ret = regmap_write(tc->regmap, DP0CTL, value);
1197 if (ret)
1198 return ret;
1199 /*
1200 * VID_EN assertion should be delayed by at least N * LSCLK
1201 * cycles from the time VID_MN_GEN is enabled in order to
1202 * generate stable values for VID_M. LSCLK is 270 MHz or
1203 * 162 MHz, VID_N is set to 32768 in tc_stream_clock_calc(),
1204 * so a delay of at least 203 us should suffice.
1205 */
1206 usleep_range(500, 1000);
1207 value |= VID_EN;
1208 ret = regmap_write(tc->regmap, DP0CTL, value);
1209 if (ret)
1210 return ret;
1211 /* Set input interface */
1212 value = DP0_AUDSRC_NO_INPUT;
1213 if (tc_test_pattern)
1214 value |= DP0_VIDSRC_COLOR_BAR;
1215 else
1216 value |= DP0_VIDSRC_DPI_RX;
1217 ret = regmap_write(tc->regmap, SYSCTRL, value);
1218 if (ret)
1219 return ret;
1220
1221 return 0;
1222}
1223
1224static int tc_stream_disable(struct tc_data *tc)
1225{
1226 int ret;
1227
1228 dev_dbg(tc->dev, "disable video stream\n");
1229
1230 ret = regmap_update_bits(tc->regmap, DP0CTL, VID_EN, 0);
1231 if (ret)
1232 return ret;
1233
1234 tc_pxl_pll_dis(tc);
1235
1236 return 0;
1237}
1238
1239static void tc_bridge_pre_enable(struct drm_bridge *bridge)
1240{
1241 struct tc_data *tc = bridge_to_tc(bridge);
1242
1243 drm_panel_prepare(tc->panel);
1244}
1245
1246static void tc_bridge_enable(struct drm_bridge *bridge)
1247{
1248 struct tc_data *tc = bridge_to_tc(bridge);
1249 int ret;
1250
1251 ret = tc_get_display_props(tc);
1252 if (ret < 0) {
1253 dev_err(tc->dev, "failed to read display props: %d\n", ret);
1254 return;
1255 }
1256
1257 ret = tc_main_link_enable(tc);
1258 if (ret < 0) {
1259 dev_err(tc->dev, "main link enable error: %d\n", ret);
1260 return;
1261 }
1262
1263 ret = tc_stream_enable(tc);
1264 if (ret < 0) {
1265 dev_err(tc->dev, "main link stream start error: %d\n", ret);
1266 tc_main_link_disable(tc);
1267 return;
1268 }
1269
1270 drm_panel_enable(tc->panel);
1271}
1272
1273static void tc_bridge_disable(struct drm_bridge *bridge)
1274{
1275 struct tc_data *tc = bridge_to_tc(bridge);
1276 int ret;
1277
1278 drm_panel_disable(tc->panel);
1279
1280 ret = tc_stream_disable(tc);
1281 if (ret < 0)
1282 dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1283
1284 ret = tc_main_link_disable(tc);
1285 if (ret < 0)
1286 dev_err(tc->dev, "main link disable error: %d\n", ret);
1287}
1288
1289static void tc_bridge_post_disable(struct drm_bridge *bridge)
1290{
1291 struct tc_data *tc = bridge_to_tc(bridge);
1292
1293 drm_panel_unprepare(tc->panel);
1294}
1295
1296static bool tc_bridge_mode_fixup(struct drm_bridge *bridge,
1297 const struct drm_display_mode *mode,
1298 struct drm_display_mode *adj)
1299{
1300 /* Fixup sync polarities, both hsync and vsync are active low */
1301 adj->flags = mode->flags;
1302 adj->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
1303 adj->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
1304
1305 return true;
1306}
1307
1308static enum drm_mode_status tc_mode_valid(struct drm_bridge *bridge,
1309 const struct drm_display_info *info,
1310 const struct drm_display_mode *mode)
1311{
1312 struct tc_data *tc = bridge_to_tc(bridge);
1313 u32 req, avail;
1314 u32 bits_per_pixel = 24;
1315
1316 /* DPI interface clock limitation: upto 154 MHz */
1317 if (mode->clock > 154000)
1318 return MODE_CLOCK_HIGH;
1319
1320 req = mode->clock * bits_per_pixel / 8;
1321 avail = tc->link.num_lanes * tc->link.rate;
1322
1323 if (req > avail)
1324 return MODE_BAD;
1325
1326 return MODE_OK;
1327}
1328
1329static void tc_bridge_mode_set(struct drm_bridge *bridge,
1330 const struct drm_display_mode *mode,
1331 const struct drm_display_mode *adj)
1332{
1333 struct tc_data *tc = bridge_to_tc(bridge);
1334
1335 tc->mode = *mode;
1336}
1337
1338static int tc_connector_get_modes(struct drm_connector *connector)
1339{
1340 struct tc_data *tc = connector_to_tc(connector);
1341 struct edid *edid;
1342 int count;
1343 int ret;
1344
1345 ret = tc_get_display_props(tc);
1346 if (ret < 0) {
1347 dev_err(tc->dev, "failed to read display props: %d\n", ret);
1348 return 0;
1349 }
1350
1351 count = drm_panel_get_modes(tc->panel, connector);
1352 if (count > 0)
1353 return count;
1354
1355 edid = drm_get_edid(connector, &tc->aux.ddc);
1356
1357 kfree(tc->edid);
1358 tc->edid = edid;
1359 if (!edid)
1360 return 0;
1361
1362 drm_connector_update_edid_property(connector, edid);
1363 count = drm_add_edid_modes(connector, edid);
1364
1365 return count;
1366}
1367
1368static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
1369 .get_modes = tc_connector_get_modes,
1370};
1371
1372static enum drm_connector_status tc_connector_detect(struct drm_connector *connector,
1373 bool force)
1374{
1375 struct tc_data *tc = connector_to_tc(connector);
1376 bool conn;
1377 u32 val;
1378 int ret;
1379
1380 if (tc->hpd_pin < 0) {
1381 if (tc->panel)
1382 return connector_status_connected;
1383 else
1384 return connector_status_unknown;
1385 }
1386
1387 ret = regmap_read(tc->regmap, GPIOI, &val);
1388 if (ret)
1389 return connector_status_unknown;
1390
1391 conn = val & BIT(tc->hpd_pin);
1392
1393 if (conn)
1394 return connector_status_connected;
1395 else
1396 return connector_status_disconnected;
1397}
1398
1399static const struct drm_connector_funcs tc_connector_funcs = {
1400 .detect = tc_connector_detect,
1401 .fill_modes = drm_helper_probe_single_connector_modes,
1402 .destroy = drm_connector_cleanup,
1403 .reset = drm_atomic_helper_connector_reset,
1404 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1405 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1406};
1407
1408static int tc_bridge_attach(struct drm_bridge *bridge,
1409 enum drm_bridge_attach_flags flags)
1410{
1411 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1412 struct tc_data *tc = bridge_to_tc(bridge);
1413 struct drm_device *drm = bridge->dev;
1414 int ret;
1415
1416 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) {
1417 DRM_ERROR("Fix bridge driver to make connector optional!");
1418 return -EINVAL;
1419 }
1420
1421 /* Create DP/eDP connector */
1422 drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
1423 ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs,
1424 tc->panel ? DRM_MODE_CONNECTOR_eDP :
1425 DRM_MODE_CONNECTOR_DisplayPort);
1426 if (ret)
1427 return ret;
1428
1429 /* Don't poll if don't have HPD connected */
1430 if (tc->hpd_pin >= 0) {
1431 if (tc->have_irq)
1432 tc->connector.polled = DRM_CONNECTOR_POLL_HPD;
1433 else
1434 tc->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
1435 DRM_CONNECTOR_POLL_DISCONNECT;
1436 }
1437
1438 if (tc->panel)
1439 drm_panel_attach(tc->panel, &tc->connector);
1440
1441 drm_display_info_set_bus_formats(&tc->connector.display_info,
1442 &bus_format, 1);
1443 tc->connector.display_info.bus_flags =
1444 DRM_BUS_FLAG_DE_HIGH |
1445 DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE |
1446 DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
1447 drm_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
1448
1449 return 0;
1450}
1451
1452static const struct drm_bridge_funcs tc_bridge_funcs = {
1453 .attach = tc_bridge_attach,
1454 .mode_valid = tc_mode_valid,
1455 .mode_set = tc_bridge_mode_set,
1456 .pre_enable = tc_bridge_pre_enable,
1457 .enable = tc_bridge_enable,
1458 .disable = tc_bridge_disable,
1459 .post_disable = tc_bridge_post_disable,
1460 .mode_fixup = tc_bridge_mode_fixup,
1461};
1462
1463static bool tc_readable_reg(struct device *dev, unsigned int reg)
1464{
1465 return reg != SYSCTRL;
1466}
1467
1468static const struct regmap_range tc_volatile_ranges[] = {
1469 regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
1470 regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
1471 regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
1472 regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
1473 regmap_reg_range(VFUEN0, VFUEN0),
1474 regmap_reg_range(INTSTS_G, INTSTS_G),
1475 regmap_reg_range(GPIOI, GPIOI),
1476};
1477
1478static const struct regmap_access_table tc_volatile_table = {
1479 .yes_ranges = tc_volatile_ranges,
1480 .n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
1481};
1482
1483static bool tc_writeable_reg(struct device *dev, unsigned int reg)
1484{
1485 return (reg != TC_IDREG) &&
1486 (reg != DP0_LTSTAT) &&
1487 (reg != DP0_SNKLTCHGREQ);
1488}
1489
1490static const struct regmap_config tc_regmap_config = {
1491 .name = "tc358767",
1492 .reg_bits = 16,
1493 .val_bits = 32,
1494 .reg_stride = 4,
1495 .max_register = PLL_DBG,
1496 .cache_type = REGCACHE_RBTREE,
1497 .readable_reg = tc_readable_reg,
1498 .volatile_table = &tc_volatile_table,
1499 .writeable_reg = tc_writeable_reg,
1500 .reg_format_endian = REGMAP_ENDIAN_BIG,
1501 .val_format_endian = REGMAP_ENDIAN_LITTLE,
1502};
1503
1504static irqreturn_t tc_irq_handler(int irq, void *arg)
1505{
1506 struct tc_data *tc = arg;
1507 u32 val;
1508 int r;
1509
1510 r = regmap_read(tc->regmap, INTSTS_G, &val);
1511 if (r)
1512 return IRQ_NONE;
1513
1514 if (!val)
1515 return IRQ_NONE;
1516
1517 if (val & INT_SYSERR) {
1518 u32 stat = 0;
1519
1520 regmap_read(tc->regmap, SYSSTAT, &stat);
1521
1522 dev_err(tc->dev, "syserr %x\n", stat);
1523 }
1524
1525 if (tc->hpd_pin >= 0 && tc->bridge.dev) {
1526 /*
1527 * H is triggered when the GPIO goes high.
1528 *
1529 * LC is triggered when the GPIO goes low and stays low for
1530 * the duration of LCNT
1531 */
1532 bool h = val & INT_GPIO_H(tc->hpd_pin);
1533 bool lc = val & INT_GPIO_LC(tc->hpd_pin);
1534
1535 dev_dbg(tc->dev, "GPIO%d: %s %s\n", tc->hpd_pin,
1536 h ? "H" : "", lc ? "LC" : "");
1537
1538 if (h || lc)
1539 drm_kms_helper_hotplug_event(tc->bridge.dev);
1540 }
1541
1542 regmap_write(tc->regmap, INTSTS_G, val);
1543
1544 return IRQ_HANDLED;
1545}
1546
1547static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
1548{
1549 struct device *dev = &client->dev;
1550 struct tc_data *tc;
1551 int ret;
1552
1553 tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
1554 if (!tc)
1555 return -ENOMEM;
1556
1557 tc->dev = dev;
1558
1559 /* port@2 is the output port */
1560 ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &tc->panel, NULL);
1561 if (ret && ret != -ENODEV)
1562 return ret;
1563
1564 /* Shut down GPIO is optional */
1565 tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
1566 if (IS_ERR(tc->sd_gpio))
1567 return PTR_ERR(tc->sd_gpio);
1568
1569 if (tc->sd_gpio) {
1570 gpiod_set_value_cansleep(tc->sd_gpio, 0);
1571 usleep_range(5000, 10000);
1572 }
1573
1574 /* Reset GPIO is optional */
1575 tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
1576 if (IS_ERR(tc->reset_gpio))
1577 return PTR_ERR(tc->reset_gpio);
1578
1579 if (tc->reset_gpio) {
1580 gpiod_set_value_cansleep(tc->reset_gpio, 1);
1581 usleep_range(5000, 10000);
1582 }
1583
1584 tc->refclk = devm_clk_get(dev, "ref");
1585 if (IS_ERR(tc->refclk)) {
1586 ret = PTR_ERR(tc->refclk);
1587 dev_err(dev, "Failed to get refclk: %d\n", ret);
1588 return ret;
1589 }
1590
1591 tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
1592 if (IS_ERR(tc->regmap)) {
1593 ret = PTR_ERR(tc->regmap);
1594 dev_err(dev, "Failed to initialize regmap: %d\n", ret);
1595 return ret;
1596 }
1597
1598 ret = of_property_read_u32(dev->of_node, "toshiba,hpd-pin",
1599 &tc->hpd_pin);
1600 if (ret) {
1601 tc->hpd_pin = -ENODEV;
1602 } else {
1603 if (tc->hpd_pin < 0 || tc->hpd_pin > 1) {
1604 dev_err(dev, "failed to parse HPD number\n");
1605 return ret;
1606 }
1607 }
1608
1609 if (client->irq > 0) {
1610 /* enable SysErr */
1611 regmap_write(tc->regmap, INTCTL_G, INT_SYSERR);
1612
1613 ret = devm_request_threaded_irq(dev, client->irq,
1614 NULL, tc_irq_handler,
1615 IRQF_ONESHOT,
1616 "tc358767-irq", tc);
1617 if (ret) {
1618 dev_err(dev, "failed to register dp interrupt\n");
1619 return ret;
1620 }
1621
1622 tc->have_irq = true;
1623 }
1624
1625 ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
1626 if (ret) {
1627 dev_err(tc->dev, "can not read device ID: %d\n", ret);
1628 return ret;
1629 }
1630
1631 if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
1632 dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
1633 return -EINVAL;
1634 }
1635
1636 tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
1637
1638 if (!tc->reset_gpio) {
1639 /*
1640 * If the reset pin isn't present, do a software reset. It isn't
1641 * as thorough as the hardware reset, as we can't reset the I2C
1642 * communication block for obvious reasons, but it's getting the
1643 * chip into a defined state.
1644 */
1645 regmap_update_bits(tc->regmap, SYSRSTENB,
1646 ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP,
1647 0);
1648 regmap_update_bits(tc->regmap, SYSRSTENB,
1649 ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP,
1650 ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP);
1651 usleep_range(5000, 10000);
1652 }
1653
1654 if (tc->hpd_pin >= 0) {
1655 u32 lcnt_reg = tc->hpd_pin == 0 ? INT_GP0_LCNT : INT_GP1_LCNT;
1656 u32 h_lc = INT_GPIO_H(tc->hpd_pin) | INT_GPIO_LC(tc->hpd_pin);
1657
1658 /* Set LCNT to 2ms */
1659 regmap_write(tc->regmap, lcnt_reg,
1660 clk_get_rate(tc->refclk) * 2 / 1000);
1661 /* We need the "alternate" mode for HPD */
1662 regmap_write(tc->regmap, GPIOM, BIT(tc->hpd_pin));
1663
1664 if (tc->have_irq) {
1665 /* enable H & LC */
1666 regmap_update_bits(tc->regmap, INTCTL_G, h_lc, h_lc);
1667 }
1668 }
1669
1670 ret = tc_aux_link_setup(tc);
1671 if (ret)
1672 return ret;
1673
1674 /* Register DP AUX channel */
1675 tc->aux.name = "TC358767 AUX i2c adapter";
1676 tc->aux.dev = tc->dev;
1677 tc->aux.transfer = tc_aux_transfer;
1678 ret = drm_dp_aux_register(&tc->aux);
1679 if (ret)
1680 return ret;
1681
1682 tc->bridge.funcs = &tc_bridge_funcs;
1683 tc->bridge.of_node = dev->of_node;
1684 drm_bridge_add(&tc->bridge);
1685
1686 i2c_set_clientdata(client, tc);
1687
1688 return 0;
1689}
1690
1691static int tc_remove(struct i2c_client *client)
1692{
1693 struct tc_data *tc = i2c_get_clientdata(client);
1694
1695 drm_bridge_remove(&tc->bridge);
1696 drm_dp_aux_unregister(&tc->aux);
1697
1698 return 0;
1699}
1700
1701static const struct i2c_device_id tc358767_i2c_ids[] = {
1702 { "tc358767", 0 },
1703 { }
1704};
1705MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
1706
1707static const struct of_device_id tc358767_of_ids[] = {
1708 { .compatible = "toshiba,tc358767", },
1709 { }
1710};
1711MODULE_DEVICE_TABLE(of, tc358767_of_ids);
1712
1713static struct i2c_driver tc358767_driver = {
1714 .driver = {
1715 .name = "tc358767",
1716 .of_match_table = tc358767_of_ids,
1717 },
1718 .id_table = tc358767_i2c_ids,
1719 .probe = tc_probe,
1720 .remove = tc_remove,
1721};
1722module_i2c_driver(tc358767_driver);
1723
1724MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
1725MODULE_DESCRIPTION("tc358767 eDP encoder driver");
1726MODULE_LICENSE("GPL");
1/*
2 * tc358767 eDP bridge driver
3 *
4 * Copyright (C) 2016 CogentEmbedded Inc
5 * Author: Andrey Gusakov <andrey.gusakov@cogentembedded.com>
6 *
7 * Copyright (C) 2016 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
8 *
9 * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c
10 *
11 * Copyright (C) 2012 Texas Instruments
12 * Author: Rob Clark <robdclark@gmail.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 */
24
25#include <linux/clk.h>
26#include <linux/device.h>
27#include <linux/gpio/consumer.h>
28#include <linux/i2c.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/regmap.h>
32#include <linux/slab.h>
33
34#include <drm/drm_atomic_helper.h>
35#include <drm/drm_crtc_helper.h>
36#include <drm/drm_dp_helper.h>
37#include <drm/drm_edid.h>
38#include <drm/drm_of.h>
39#include <drm/drm_panel.h>
40
41/* Registers */
42
43/* Display Parallel Interface */
44#define DPIPXLFMT 0x0440
45#define VS_POL_ACTIVE_LOW (1 << 10)
46#define HS_POL_ACTIVE_LOW (1 << 9)
47#define DE_POL_ACTIVE_HIGH (0 << 8)
48#define SUB_CFG_TYPE_CONFIG1 (0 << 2) /* LSB aligned */
49#define SUB_CFG_TYPE_CONFIG2 (1 << 2) /* Loosely Packed */
50#define SUB_CFG_TYPE_CONFIG3 (2 << 2) /* LSB aligned 8-bit */
51#define DPI_BPP_RGB888 (0 << 0)
52#define DPI_BPP_RGB666 (1 << 0)
53#define DPI_BPP_RGB565 (2 << 0)
54
55/* Video Path */
56#define VPCTRL0 0x0450
57#define OPXLFMT_RGB666 (0 << 8)
58#define OPXLFMT_RGB888 (1 << 8)
59#define FRMSYNC_DISABLED (0 << 4) /* Video Timing Gen Disabled */
60#define FRMSYNC_ENABLED (1 << 4) /* Video Timing Gen Enabled */
61#define MSF_DISABLED (0 << 0) /* Magic Square FRC disabled */
62#define MSF_ENABLED (1 << 0) /* Magic Square FRC enabled */
63#define HTIM01 0x0454
64#define HTIM02 0x0458
65#define VTIM01 0x045c
66#define VTIM02 0x0460
67#define VFUEN0 0x0464
68#define VFUEN BIT(0) /* Video Frame Timing Upload */
69
70/* System */
71#define TC_IDREG 0x0500
72#define SYSCTRL 0x0510
73#define DP0_AUDSRC_NO_INPUT (0 << 3)
74#define DP0_AUDSRC_I2S_RX (1 << 3)
75#define DP0_VIDSRC_NO_INPUT (0 << 0)
76#define DP0_VIDSRC_DSI_RX (1 << 0)
77#define DP0_VIDSRC_DPI_RX (2 << 0)
78#define DP0_VIDSRC_COLOR_BAR (3 << 0)
79
80/* Control */
81#define DP0CTL 0x0600
82#define VID_MN_GEN BIT(6) /* Auto-generate M/N values */
83#define EF_EN BIT(5) /* Enable Enhanced Framing */
84#define VID_EN BIT(1) /* Video transmission enable */
85#define DP_EN BIT(0) /* Enable DPTX function */
86
87/* Clocks */
88#define DP0_VIDMNGEN0 0x0610
89#define DP0_VIDMNGEN1 0x0614
90#define DP0_VMNGENSTATUS 0x0618
91
92/* Main Channel */
93#define DP0_SECSAMPLE 0x0640
94#define DP0_VIDSYNCDELAY 0x0644
95#define DP0_TOTALVAL 0x0648
96#define DP0_STARTVAL 0x064c
97#define DP0_ACTIVEVAL 0x0650
98#define DP0_SYNCVAL 0x0654
99#define DP0_MISC 0x0658
100#define TU_SIZE_RECOMMENDED (0x3f << 16) /* LSCLK cycles per TU */
101#define BPC_6 (0 << 5)
102#define BPC_8 (1 << 5)
103
104/* AUX channel */
105#define DP0_AUXCFG0 0x0660
106#define DP0_AUXCFG1 0x0664
107#define AUX_RX_FILTER_EN BIT(16)
108
109#define DP0_AUXADDR 0x0668
110#define DP0_AUXWDATA(i) (0x066c + (i) * 4)
111#define DP0_AUXRDATA(i) (0x067c + (i) * 4)
112#define DP0_AUXSTATUS 0x068c
113#define AUX_STATUS_MASK 0xf0
114#define AUX_STATUS_SHIFT 4
115#define AUX_TIMEOUT BIT(1)
116#define AUX_BUSY BIT(0)
117#define DP0_AUXI2CADR 0x0698
118
119/* Link Training */
120#define DP0_SRCCTRL 0x06a0
121#define DP0_SRCCTRL_SCRMBLDIS BIT(13)
122#define DP0_SRCCTRL_EN810B BIT(12)
123#define DP0_SRCCTRL_NOTP (0 << 8)
124#define DP0_SRCCTRL_TP1 (1 << 8)
125#define DP0_SRCCTRL_TP2 (2 << 8)
126#define DP0_SRCCTRL_LANESKEW BIT(7)
127#define DP0_SRCCTRL_SSCG BIT(3)
128#define DP0_SRCCTRL_LANES_1 (0 << 2)
129#define DP0_SRCCTRL_LANES_2 (1 << 2)
130#define DP0_SRCCTRL_BW27 (1 << 1)
131#define DP0_SRCCTRL_BW162 (0 << 1)
132#define DP0_SRCCTRL_AUTOCORRECT BIT(0)
133#define DP0_LTSTAT 0x06d0
134#define LT_LOOPDONE BIT(13)
135#define LT_STATUS_MASK (0x1f << 8)
136#define LT_CHANNEL1_EQ_BITS (DP_CHANNEL_EQ_BITS << 4)
137#define LT_INTERLANE_ALIGN_DONE BIT(3)
138#define LT_CHANNEL0_EQ_BITS (DP_CHANNEL_EQ_BITS)
139#define DP0_SNKLTCHGREQ 0x06d4
140#define DP0_LTLOOPCTRL 0x06d8
141#define DP0_SNKLTCTRL 0x06e4
142
143/* PHY */
144#define DP_PHY_CTRL 0x0800
145#define DP_PHY_RST BIT(28) /* DP PHY Global Soft Reset */
146#define BGREN BIT(25) /* AUX PHY BGR Enable */
147#define PWR_SW_EN BIT(24) /* PHY Power Switch Enable */
148#define PHY_M1_RST BIT(12) /* Reset PHY1 Main Channel */
149#define PHY_RDY BIT(16) /* PHY Main Channels Ready */
150#define PHY_M0_RST BIT(8) /* Reset PHY0 Main Channel */
151#define PHY_A0_EN BIT(1) /* PHY Aux Channel0 Enable */
152#define PHY_M0_EN BIT(0) /* PHY Main Channel0 Enable */
153
154/* PLL */
155#define DP0_PLLCTRL 0x0900
156#define DP1_PLLCTRL 0x0904 /* not defined in DS */
157#define PXL_PLLCTRL 0x0908
158#define PLLUPDATE BIT(2)
159#define PLLBYP BIT(1)
160#define PLLEN BIT(0)
161#define PXL_PLLPARAM 0x0914
162#define IN_SEL_REFCLK (0 << 14)
163#define SYS_PLLPARAM 0x0918
164#define REF_FREQ_38M4 (0 << 8) /* 38.4 MHz */
165#define REF_FREQ_19M2 (1 << 8) /* 19.2 MHz */
166#define REF_FREQ_26M (2 << 8) /* 26 MHz */
167#define REF_FREQ_13M (3 << 8) /* 13 MHz */
168#define SYSCLK_SEL_LSCLK (0 << 4)
169#define LSCLK_DIV_1 (0 << 0)
170#define LSCLK_DIV_2 (1 << 0)
171
172/* Test & Debug */
173#define TSTCTL 0x0a00
174#define PLL_DBG 0x0a04
175
176static bool tc_test_pattern;
177module_param_named(test, tc_test_pattern, bool, 0644);
178
179struct tc_edp_link {
180 struct drm_dp_link base;
181 u8 assr;
182 int scrambler_dis;
183 int spread;
184 int coding8b10b;
185 u8 swing;
186 u8 preemp;
187};
188
189struct tc_data {
190 struct device *dev;
191 struct regmap *regmap;
192 struct drm_dp_aux aux;
193
194 struct drm_bridge bridge;
195 struct drm_connector connector;
196 struct drm_panel *panel;
197
198 /* link settings */
199 struct tc_edp_link link;
200
201 /* display edid */
202 struct edid *edid;
203 /* current mode */
204 struct drm_display_mode *mode;
205
206 u32 rev;
207 u8 assr;
208
209 struct gpio_desc *sd_gpio;
210 struct gpio_desc *reset_gpio;
211 struct clk *refclk;
212};
213
214static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
215{
216 return container_of(a, struct tc_data, aux);
217}
218
219static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
220{
221 return container_of(b, struct tc_data, bridge);
222}
223
224static inline struct tc_data *connector_to_tc(struct drm_connector *c)
225{
226 return container_of(c, struct tc_data, connector);
227}
228
229/* Simple macros to avoid repeated error checks */
230#define tc_write(reg, var) \
231 do { \
232 ret = regmap_write(tc->regmap, reg, var); \
233 if (ret) \
234 goto err; \
235 } while (0)
236#define tc_read(reg, var) \
237 do { \
238 ret = regmap_read(tc->regmap, reg, var); \
239 if (ret) \
240 goto err; \
241 } while (0)
242
243static inline int tc_poll_timeout(struct regmap *map, unsigned int addr,
244 unsigned int cond_mask,
245 unsigned int cond_value,
246 unsigned long sleep_us, u64 timeout_us)
247{
248 ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
249 unsigned int val;
250 int ret;
251
252 for (;;) {
253 ret = regmap_read(map, addr, &val);
254 if (ret)
255 break;
256 if ((val & cond_mask) == cond_value)
257 break;
258 if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) {
259 ret = regmap_read(map, addr, &val);
260 break;
261 }
262 if (sleep_us)
263 usleep_range((sleep_us >> 2) + 1, sleep_us);
264 }
265 return ret ?: (((val & cond_mask) == cond_value) ? 0 : -ETIMEDOUT);
266}
267
268static int tc_aux_wait_busy(struct tc_data *tc, unsigned int timeout_ms)
269{
270 return tc_poll_timeout(tc->regmap, DP0_AUXSTATUS, AUX_BUSY, 0,
271 1000, 1000 * timeout_ms);
272}
273
274static int tc_aux_get_status(struct tc_data *tc, u8 *reply)
275{
276 int ret;
277 u32 value;
278
279 ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &value);
280 if (ret < 0)
281 return ret;
282 if (value & AUX_BUSY) {
283 if (value & AUX_TIMEOUT) {
284 dev_err(tc->dev, "i2c access timeout!\n");
285 return -ETIMEDOUT;
286 }
287 return -EBUSY;
288 }
289
290 *reply = (value & AUX_STATUS_MASK) >> AUX_STATUS_SHIFT;
291 return 0;
292}
293
294static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
295 struct drm_dp_aux_msg *msg)
296{
297 struct tc_data *tc = aux_to_tc(aux);
298 size_t size = min_t(size_t, 8, msg->size);
299 u8 request = msg->request & ~DP_AUX_I2C_MOT;
300 u8 *buf = msg->buffer;
301 u32 tmp = 0;
302 int i = 0;
303 int ret;
304
305 if (size == 0)
306 return 0;
307
308 ret = tc_aux_wait_busy(tc, 100);
309 if (ret)
310 goto err;
311
312 if (request == DP_AUX_I2C_WRITE || request == DP_AUX_NATIVE_WRITE) {
313 /* Store data */
314 while (i < size) {
315 if (request == DP_AUX_NATIVE_WRITE)
316 tmp = tmp | (buf[i] << (8 * (i & 0x3)));
317 else
318 tmp = (tmp << 8) | buf[i];
319 i++;
320 if (((i % 4) == 0) || (i == size)) {
321 tc_write(DP0_AUXWDATA(i >> 2), tmp);
322 tmp = 0;
323 }
324 }
325 } else if (request != DP_AUX_I2C_READ &&
326 request != DP_AUX_NATIVE_READ) {
327 return -EINVAL;
328 }
329
330 /* Store address */
331 tc_write(DP0_AUXADDR, msg->address);
332 /* Start transfer */
333 tc_write(DP0_AUXCFG0, ((size - 1) << 8) | request);
334
335 ret = tc_aux_wait_busy(tc, 100);
336 if (ret)
337 goto err;
338
339 ret = tc_aux_get_status(tc, &msg->reply);
340 if (ret)
341 goto err;
342
343 if (request == DP_AUX_I2C_READ || request == DP_AUX_NATIVE_READ) {
344 /* Read data */
345 while (i < size) {
346 if ((i % 4) == 0)
347 tc_read(DP0_AUXRDATA(i >> 2), &tmp);
348 buf[i] = tmp & 0xff;
349 tmp = tmp >> 8;
350 i++;
351 }
352 }
353
354 return size;
355err:
356 return ret;
357}
358
359static const char * const training_pattern1_errors[] = {
360 "No errors",
361 "Aux write error",
362 "Aux read error",
363 "Max voltage reached error",
364 "Loop counter expired error",
365 "res", "res", "res"
366};
367
368static const char * const training_pattern2_errors[] = {
369 "No errors",
370 "Aux write error",
371 "Aux read error",
372 "Clock recovery failed error",
373 "Loop counter expired error",
374 "res", "res", "res"
375};
376
377static u32 tc_srcctrl(struct tc_data *tc)
378{
379 /*
380 * No training pattern, skew lane 1 data by two LSCLK cycles with
381 * respect to lane 0 data, AutoCorrect Mode = 0
382 */
383 u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW;
384
385 if (tc->link.scrambler_dis)
386 reg |= DP0_SRCCTRL_SCRMBLDIS; /* Scrambler Disabled */
387 if (tc->link.coding8b10b)
388 /* Enable 8/10B Encoder (TxData[19:16] not used) */
389 reg |= DP0_SRCCTRL_EN810B;
390 if (tc->link.spread)
391 reg |= DP0_SRCCTRL_SSCG; /* Spread Spectrum Enable */
392 if (tc->link.base.num_lanes == 2)
393 reg |= DP0_SRCCTRL_LANES_2; /* Two Main Channel Lanes */
394 if (tc->link.base.rate != 162000)
395 reg |= DP0_SRCCTRL_BW27; /* 2.7 Gbps link */
396 return reg;
397}
398
399static void tc_wait_pll_lock(struct tc_data *tc)
400{
401 /* Wait for PLL to lock: up to 2.09 ms, depending on refclk */
402 usleep_range(3000, 6000);
403}
404
405static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
406{
407 int ret;
408 int i_pre, best_pre = 1;
409 int i_post, best_post = 1;
410 int div, best_div = 1;
411 int mul, best_mul = 1;
412 int delta, best_delta;
413 int ext_div[] = {1, 2, 3, 5, 7};
414 int best_pixelclock = 0;
415 int vco_hi = 0;
416
417 dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
418 refclk);
419 best_delta = pixelclock;
420 /* Loop over all possible ext_divs, skipping invalid configurations */
421 for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
422 /*
423 * refclk / ext_pre_div should be in the 1 to 200 MHz range.
424 * We don't allow any refclk > 200 MHz, only check lower bounds.
425 */
426 if (refclk / ext_div[i_pre] < 1000000)
427 continue;
428 for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
429 for (div = 1; div <= 16; div++) {
430 u32 clk;
431 u64 tmp;
432
433 tmp = pixelclock * ext_div[i_pre] *
434 ext_div[i_post] * div;
435 do_div(tmp, refclk);
436 mul = tmp;
437
438 /* Check limits */
439 if ((mul < 1) || (mul > 128))
440 continue;
441
442 clk = (refclk / ext_div[i_pre] / div) * mul;
443 /*
444 * refclk * mul / (ext_pre_div * pre_div)
445 * should be in the 150 to 650 MHz range
446 */
447 if ((clk > 650000000) || (clk < 150000000))
448 continue;
449
450 clk = clk / ext_div[i_post];
451 delta = clk - pixelclock;
452
453 if (abs(delta) < abs(best_delta)) {
454 best_pre = i_pre;
455 best_post = i_post;
456 best_div = div;
457 best_mul = mul;
458 best_delta = delta;
459 best_pixelclock = clk;
460 }
461 }
462 }
463 }
464 if (best_pixelclock == 0) {
465 dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
466 pixelclock);
467 return -EINVAL;
468 }
469
470 dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
471 best_delta);
472 dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
473 ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
474
475 /* if VCO >= 300 MHz */
476 if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
477 vco_hi = 1;
478 /* see DS */
479 if (best_div == 16)
480 best_div = 0;
481 if (best_mul == 128)
482 best_mul = 0;
483
484 /* Power up PLL and switch to bypass */
485 tc_write(PXL_PLLCTRL, PLLBYP | PLLEN);
486
487 tc_write(PXL_PLLPARAM,
488 (vco_hi << 24) | /* For PLL VCO >= 300 MHz = 1 */
489 (ext_div[best_pre] << 20) | /* External Pre-divider */
490 (ext_div[best_post] << 16) | /* External Post-divider */
491 IN_SEL_REFCLK | /* Use RefClk as PLL input */
492 (best_div << 8) | /* Divider for PLL RefClk */
493 (best_mul << 0)); /* Multiplier for PLL */
494
495 /* Force PLL parameter update and disable bypass */
496 tc_write(PXL_PLLCTRL, PLLUPDATE | PLLEN);
497
498 tc_wait_pll_lock(tc);
499
500 return 0;
501err:
502 return ret;
503}
504
505static int tc_pxl_pll_dis(struct tc_data *tc)
506{
507 /* Enable PLL bypass, power down PLL */
508 return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
509}
510
511static int tc_stream_clock_calc(struct tc_data *tc)
512{
513 int ret;
514 /*
515 * If the Stream clock and Link Symbol clock are
516 * asynchronous with each other, the value of M changes over
517 * time. This way of generating link clock and stream
518 * clock is called Asynchronous Clock mode. The value M
519 * must change while the value N stays constant. The
520 * value of N in this Asynchronous Clock mode must be set
521 * to 2^15 or 32,768.
522 *
523 * LSCLK = 1/10 of high speed link clock
524 *
525 * f_STRMCLK = M/N * f_LSCLK
526 * M/N = f_STRMCLK / f_LSCLK
527 *
528 */
529 tc_write(DP0_VIDMNGEN1, 32768);
530
531 return 0;
532err:
533 return ret;
534}
535
536static int tc_aux_link_setup(struct tc_data *tc)
537{
538 unsigned long rate;
539 u32 value;
540 int ret;
541
542 rate = clk_get_rate(tc->refclk);
543 switch (rate) {
544 case 38400000:
545 value = REF_FREQ_38M4;
546 break;
547 case 26000000:
548 value = REF_FREQ_26M;
549 break;
550 case 19200000:
551 value = REF_FREQ_19M2;
552 break;
553 case 13000000:
554 value = REF_FREQ_13M;
555 break;
556 default:
557 dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
558 return -EINVAL;
559 }
560
561 /* Setup DP-PHY / PLL */
562 value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
563 tc_write(SYS_PLLPARAM, value);
564
565 tc_write(DP_PHY_CTRL, BGREN | PWR_SW_EN | BIT(2) | PHY_A0_EN);
566
567 /*
568 * Initially PLLs are in bypass. Force PLL parameter update,
569 * disable PLL bypass, enable PLL
570 */
571 tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
572 tc_wait_pll_lock(tc);
573
574 tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
575 tc_wait_pll_lock(tc);
576
577 ret = tc_poll_timeout(tc->regmap, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 1,
578 1000);
579 if (ret == -ETIMEDOUT) {
580 dev_err(tc->dev, "Timeout waiting for PHY to become ready");
581 return ret;
582 } else if (ret)
583 goto err;
584
585 /* Setup AUX link */
586 tc_write(DP0_AUXCFG1, AUX_RX_FILTER_EN |
587 (0x06 << 8) | /* Aux Bit Period Calculator Threshold */
588 (0x3f << 0)); /* Aux Response Timeout Timer */
589
590 return 0;
591err:
592 dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
593 return ret;
594}
595
596static int tc_get_display_props(struct tc_data *tc)
597{
598 int ret;
599 /* temp buffer */
600 u8 tmp[8];
601
602 /* Read DP Rx Link Capability */
603 ret = drm_dp_link_probe(&tc->aux, &tc->link.base);
604 if (ret < 0)
605 goto err_dpcd_read;
606 if ((tc->link.base.rate != 162000) && (tc->link.base.rate != 270000))
607 goto err_dpcd_inval;
608
609 ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, tmp);
610 if (ret < 0)
611 goto err_dpcd_read;
612 tc->link.spread = tmp[0] & BIT(0); /* 0.5% down spread */
613
614 ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, tmp);
615 if (ret < 0)
616 goto err_dpcd_read;
617 tc->link.coding8b10b = tmp[0] & BIT(0);
618 tc->link.scrambler_dis = 0;
619 /* read assr */
620 ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, tmp);
621 if (ret < 0)
622 goto err_dpcd_read;
623 tc->link.assr = tmp[0] & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
624
625 dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
626 tc->link.base.revision >> 4, tc->link.base.revision & 0x0f,
627 (tc->link.base.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
628 tc->link.base.num_lanes,
629 (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING) ?
630 "enhanced" : "non-enhanced");
631 dev_dbg(tc->dev, "ANSI 8B/10B: %d\n", tc->link.coding8b10b);
632 dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
633 tc->link.assr, tc->assr);
634
635 return 0;
636
637err_dpcd_read:
638 dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
639 return ret;
640err_dpcd_inval:
641 dev_err(tc->dev, "invalid DPCD\n");
642 return -EINVAL;
643}
644
645static int tc_set_video_mode(struct tc_data *tc, struct drm_display_mode *mode)
646{
647 int ret;
648 int vid_sync_dly;
649 int max_tu_symbol;
650
651 int left_margin = mode->htotal - mode->hsync_end;
652 int right_margin = mode->hsync_start - mode->hdisplay;
653 int hsync_len = mode->hsync_end - mode->hsync_start;
654 int upper_margin = mode->vtotal - mode->vsync_end;
655 int lower_margin = mode->vsync_start - mode->vdisplay;
656 int vsync_len = mode->vsync_end - mode->vsync_start;
657
658 dev_dbg(tc->dev, "set mode %dx%d\n",
659 mode->hdisplay, mode->vdisplay);
660 dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
661 left_margin, right_margin, hsync_len);
662 dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
663 upper_margin, lower_margin, vsync_len);
664 dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
665
666
667 /* LCD Ctl Frame Size */
668 tc_write(VPCTRL0, (0x40 << 20) /* VSDELAY */ |
669 OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
670 tc_write(HTIM01, (left_margin << 16) | /* H back porch */
671 (hsync_len << 0)); /* Hsync */
672 tc_write(HTIM02, (right_margin << 16) | /* H front porch */
673 (mode->hdisplay << 0)); /* width */
674 tc_write(VTIM01, (upper_margin << 16) | /* V back porch */
675 (vsync_len << 0)); /* Vsync */
676 tc_write(VTIM02, (lower_margin << 16) | /* V front porch */
677 (mode->vdisplay << 0)); /* height */
678 tc_write(VFUEN0, VFUEN); /* update settings */
679
680 /* Test pattern settings */
681 tc_write(TSTCTL,
682 (120 << 24) | /* Red Color component value */
683 (20 << 16) | /* Green Color component value */
684 (99 << 8) | /* Blue Color component value */
685 (1 << 4) | /* Enable I2C Filter */
686 (2 << 0) | /* Color bar Mode */
687 0);
688
689 /* DP Main Stream Attributes */
690 vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
691 tc_write(DP0_VIDSYNCDELAY,
692 (0x003e << 16) | /* thresh_dly */
693 (vid_sync_dly << 0));
694
695 tc_write(DP0_TOTALVAL, (mode->vtotal << 16) | (mode->htotal));
696
697 tc_write(DP0_STARTVAL,
698 ((upper_margin + vsync_len) << 16) |
699 ((left_margin + hsync_len) << 0));
700
701 tc_write(DP0_ACTIVEVAL, (mode->vdisplay << 16) | (mode->hdisplay));
702
703 tc_write(DP0_SYNCVAL, (vsync_len << 16) | (hsync_len << 0));
704
705 tc_write(DPIPXLFMT, VS_POL_ACTIVE_LOW | HS_POL_ACTIVE_LOW |
706 DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 | DPI_BPP_RGB888);
707
708 /*
709 * Recommended maximum number of symbols transferred in a transfer unit:
710 * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
711 * (output active video bandwidth in bytes))
712 * Must be less than tu_size.
713 */
714 max_tu_symbol = TU_SIZE_RECOMMENDED - 1;
715 tc_write(DP0_MISC, (max_tu_symbol << 23) | TU_SIZE_RECOMMENDED | BPC_8);
716
717 return 0;
718err:
719 return ret;
720}
721
722static int tc_link_training(struct tc_data *tc, int pattern)
723{
724 const char * const *errors;
725 u32 srcctrl = tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
726 DP0_SRCCTRL_AUTOCORRECT;
727 int timeout;
728 int retry;
729 u32 value;
730 int ret;
731
732 if (pattern == DP_TRAINING_PATTERN_1) {
733 srcctrl |= DP0_SRCCTRL_TP1;
734 errors = training_pattern1_errors;
735 } else {
736 srcctrl |= DP0_SRCCTRL_TP2;
737 errors = training_pattern2_errors;
738 }
739
740 /* Set DPCD 0x102 for Training Part 1 or 2 */
741 tc_write(DP0_SNKLTCTRL, DP_LINK_SCRAMBLING_DISABLE | pattern);
742
743 tc_write(DP0_LTLOOPCTRL,
744 (0x0f << 28) | /* Defer Iteration Count */
745 (0x0f << 24) | /* Loop Iteration Count */
746 (0x0d << 0)); /* Loop Timer Delay */
747
748 retry = 5;
749 do {
750 /* Set DP0 Training Pattern */
751 tc_write(DP0_SRCCTRL, srcctrl);
752
753 /* Enable DP0 to start Link Training */
754 tc_write(DP0CTL, DP_EN);
755
756 /* wait */
757 timeout = 1000;
758 do {
759 tc_read(DP0_LTSTAT, &value);
760 udelay(1);
761 } while ((!(value & LT_LOOPDONE)) && (--timeout));
762 if (timeout == 0) {
763 dev_err(tc->dev, "Link training timeout!\n");
764 } else {
765 int pattern = (value >> 11) & 0x3;
766 int error = (value >> 8) & 0x7;
767
768 dev_dbg(tc->dev,
769 "Link training phase %d done after %d uS: %s\n",
770 pattern, 1000 - timeout, errors[error]);
771 if (pattern == DP_TRAINING_PATTERN_1 && error == 0)
772 break;
773 if (pattern == DP_TRAINING_PATTERN_2) {
774 value &= LT_CHANNEL1_EQ_BITS |
775 LT_INTERLANE_ALIGN_DONE |
776 LT_CHANNEL0_EQ_BITS;
777 /* in case of two lanes */
778 if ((tc->link.base.num_lanes == 2) &&
779 (value == (LT_CHANNEL1_EQ_BITS |
780 LT_INTERLANE_ALIGN_DONE |
781 LT_CHANNEL0_EQ_BITS)))
782 break;
783 /* in case of one line */
784 if ((tc->link.base.num_lanes == 1) &&
785 (value == (LT_INTERLANE_ALIGN_DONE |
786 LT_CHANNEL0_EQ_BITS)))
787 break;
788 }
789 }
790 /* restart */
791 tc_write(DP0CTL, 0);
792 usleep_range(10, 20);
793 } while (--retry);
794 if (retry == 0) {
795 dev_err(tc->dev, "Failed to finish training phase %d\n",
796 pattern);
797 }
798
799 return 0;
800err:
801 return ret;
802}
803
804static int tc_main_link_setup(struct tc_data *tc)
805{
806 struct drm_dp_aux *aux = &tc->aux;
807 struct device *dev = tc->dev;
808 unsigned int rate;
809 u32 dp_phy_ctrl;
810 int timeout;
811 bool aligned;
812 bool ready;
813 u32 value;
814 int ret;
815 u8 tmp[8];
816
817 /* display mode should be set at this point */
818 if (!tc->mode)
819 return -EINVAL;
820
821 /* from excel file - DP0_SrcCtrl */
822 tc_write(DP0_SRCCTRL, DP0_SRCCTRL_SCRMBLDIS | DP0_SRCCTRL_EN810B |
823 DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_LANES_2 |
824 DP0_SRCCTRL_BW27 | DP0_SRCCTRL_AUTOCORRECT);
825 /* from excel file - DP1_SrcCtrl */
826 tc_write(0x07a0, 0x00003083);
827
828 rate = clk_get_rate(tc->refclk);
829 switch (rate) {
830 case 38400000:
831 value = REF_FREQ_38M4;
832 break;
833 case 26000000:
834 value = REF_FREQ_26M;
835 break;
836 case 19200000:
837 value = REF_FREQ_19M2;
838 break;
839 case 13000000:
840 value = REF_FREQ_13M;
841 break;
842 default:
843 return -EINVAL;
844 }
845 value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
846 tc_write(SYS_PLLPARAM, value);
847 /* Setup Main Link */
848 dp_phy_ctrl = BGREN | PWR_SW_EN | BIT(2) | PHY_A0_EN | PHY_M0_EN;
849 tc_write(DP_PHY_CTRL, dp_phy_ctrl);
850 msleep(100);
851
852 /* PLL setup */
853 tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
854 tc_wait_pll_lock(tc);
855
856 tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
857 tc_wait_pll_lock(tc);
858
859 /* PXL PLL setup */
860 if (tc_test_pattern) {
861 ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
862 1000 * tc->mode->clock);
863 if (ret)
864 goto err;
865 }
866
867 /* Reset/Enable Main Links */
868 dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST;
869 tc_write(DP_PHY_CTRL, dp_phy_ctrl);
870 usleep_range(100, 200);
871 dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
872 tc_write(DP_PHY_CTRL, dp_phy_ctrl);
873
874 timeout = 1000;
875 do {
876 tc_read(DP_PHY_CTRL, &value);
877 udelay(1);
878 } while ((!(value & PHY_RDY)) && (--timeout));
879
880 if (timeout == 0) {
881 dev_err(dev, "timeout waiting for phy become ready");
882 return -ETIMEDOUT;
883 }
884
885 /* Set misc: 8 bits per color */
886 ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
887 if (ret)
888 goto err;
889
890 /*
891 * ASSR mode
892 * on TC358767 side ASSR configured through strap pin
893 * seems there is no way to change this setting from SW
894 *
895 * check is tc configured for same mode
896 */
897 if (tc->assr != tc->link.assr) {
898 dev_dbg(dev, "Trying to set display to ASSR: %d\n",
899 tc->assr);
900 /* try to set ASSR on display side */
901 tmp[0] = tc->assr;
902 ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
903 if (ret < 0)
904 goto err_dpcd_read;
905 /* read back */
906 ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
907 if (ret < 0)
908 goto err_dpcd_read;
909
910 if (tmp[0] != tc->assr) {
911 dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
912 tc->assr);
913 /* trying with disabled scrambler */
914 tc->link.scrambler_dis = 1;
915 }
916 }
917
918 /* Setup Link & DPRx Config for Training */
919 ret = drm_dp_link_configure(aux, &tc->link.base);
920 if (ret < 0)
921 goto err_dpcd_write;
922
923 /* DOWNSPREAD_CTRL */
924 tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
925 /* MAIN_LINK_CHANNEL_CODING_SET */
926 tmp[1] = tc->link.coding8b10b ? DP_SET_ANSI_8B10B : 0x00;
927 ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
928 if (ret < 0)
929 goto err_dpcd_write;
930
931 ret = tc_link_training(tc, DP_TRAINING_PATTERN_1);
932 if (ret)
933 goto err;
934
935 ret = tc_link_training(tc, DP_TRAINING_PATTERN_2);
936 if (ret)
937 goto err;
938
939 /* Clear DPCD 0x102 */
940 /* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
941 tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
942 ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
943 if (ret < 0)
944 goto err_dpcd_write;
945
946 /* Clear Training Pattern, set AutoCorrect Mode = 1 */
947 tc_write(DP0_SRCCTRL, tc_srcctrl(tc) | DP0_SRCCTRL_AUTOCORRECT);
948
949 /* Wait */
950 timeout = 100;
951 do {
952 udelay(1);
953 /* Read DPCD 0x202-0x207 */
954 ret = drm_dp_dpcd_read_link_status(aux, tmp + 2);
955 if (ret < 0)
956 goto err_dpcd_read;
957 ready = (tmp[2] == ((DP_CHANNEL_EQ_BITS << 4) | /* Lane1 */
958 DP_CHANNEL_EQ_BITS)); /* Lane0 */
959 aligned = tmp[4] & DP_INTERLANE_ALIGN_DONE;
960 } while ((--timeout) && !(ready && aligned));
961
962 if (timeout == 0) {
963 /* Read DPCD 0x200-0x201 */
964 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT, tmp, 2);
965 if (ret < 0)
966 goto err_dpcd_read;
967 dev_info(dev, "0x0200 SINK_COUNT: 0x%02x\n", tmp[0]);
968 dev_info(dev, "0x0201 DEVICE_SERVICE_IRQ_VECTOR: 0x%02x\n",
969 tmp[1]);
970 dev_info(dev, "0x0202 LANE0_1_STATUS: 0x%02x\n", tmp[2]);
971 dev_info(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n",
972 tmp[4]);
973 dev_info(dev, "0x0205 SINK_STATUS: 0x%02x\n", tmp[5]);
974 dev_info(dev, "0x0206 ADJUST_REQUEST_LANE0_1: 0x%02x\n",
975 tmp[6]);
976
977 if (!ready)
978 dev_err(dev, "Lane0/1 not ready\n");
979 if (!aligned)
980 dev_err(dev, "Lane0/1 not aligned\n");
981 return -EAGAIN;
982 }
983
984 ret = tc_set_video_mode(tc, tc->mode);
985 if (ret)
986 goto err;
987
988 /* Set M/N */
989 ret = tc_stream_clock_calc(tc);
990 if (ret)
991 goto err;
992
993 return 0;
994err_dpcd_read:
995 dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
996 return ret;
997err_dpcd_write:
998 dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
999err:
1000 return ret;
1001}
1002
1003static int tc_main_link_stream(struct tc_data *tc, int state)
1004{
1005 int ret;
1006 u32 value;
1007
1008 dev_dbg(tc->dev, "stream: %d\n", state);
1009
1010 if (state) {
1011 value = VID_MN_GEN | DP_EN;
1012 if (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1013 value |= EF_EN;
1014 tc_write(DP0CTL, value);
1015 /*
1016 * VID_EN assertion should be delayed by at least N * LSCLK
1017 * cycles from the time VID_MN_GEN is enabled in order to
1018 * generate stable values for VID_M. LSCLK is 270 MHz or
1019 * 162 MHz, VID_N is set to 32768 in tc_stream_clock_calc(),
1020 * so a delay of at least 203 us should suffice.
1021 */
1022 usleep_range(500, 1000);
1023 value |= VID_EN;
1024 tc_write(DP0CTL, value);
1025 /* Set input interface */
1026 value = DP0_AUDSRC_NO_INPUT;
1027 if (tc_test_pattern)
1028 value |= DP0_VIDSRC_COLOR_BAR;
1029 else
1030 value |= DP0_VIDSRC_DPI_RX;
1031 tc_write(SYSCTRL, value);
1032 } else {
1033 tc_write(DP0CTL, 0);
1034 }
1035
1036 return 0;
1037err:
1038 return ret;
1039}
1040
1041static void tc_bridge_pre_enable(struct drm_bridge *bridge)
1042{
1043 struct tc_data *tc = bridge_to_tc(bridge);
1044
1045 drm_panel_prepare(tc->panel);
1046}
1047
1048static void tc_bridge_enable(struct drm_bridge *bridge)
1049{
1050 struct tc_data *tc = bridge_to_tc(bridge);
1051 int ret;
1052
1053 ret = tc_main_link_setup(tc);
1054 if (ret < 0) {
1055 dev_err(tc->dev, "main link setup error: %d\n", ret);
1056 return;
1057 }
1058
1059 ret = tc_main_link_stream(tc, 1);
1060 if (ret < 0) {
1061 dev_err(tc->dev, "main link stream start error: %d\n", ret);
1062 return;
1063 }
1064
1065 drm_panel_enable(tc->panel);
1066}
1067
1068static void tc_bridge_disable(struct drm_bridge *bridge)
1069{
1070 struct tc_data *tc = bridge_to_tc(bridge);
1071 int ret;
1072
1073 drm_panel_disable(tc->panel);
1074
1075 ret = tc_main_link_stream(tc, 0);
1076 if (ret < 0)
1077 dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1078}
1079
1080static void tc_bridge_post_disable(struct drm_bridge *bridge)
1081{
1082 struct tc_data *tc = bridge_to_tc(bridge);
1083
1084 drm_panel_unprepare(tc->panel);
1085}
1086
1087static bool tc_bridge_mode_fixup(struct drm_bridge *bridge,
1088 const struct drm_display_mode *mode,
1089 struct drm_display_mode *adj)
1090{
1091 /* Fixup sync polarities, both hsync and vsync are active low */
1092 adj->flags = mode->flags;
1093 adj->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
1094 adj->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
1095
1096 return true;
1097}
1098
1099static int tc_connector_mode_valid(struct drm_connector *connector,
1100 struct drm_display_mode *mode)
1101{
1102 /* Accept any mode */
1103 return MODE_OK;
1104}
1105
1106static void tc_bridge_mode_set(struct drm_bridge *bridge,
1107 struct drm_display_mode *mode,
1108 struct drm_display_mode *adj)
1109{
1110 struct tc_data *tc = bridge_to_tc(bridge);
1111
1112 tc->mode = mode;
1113}
1114
1115static int tc_connector_get_modes(struct drm_connector *connector)
1116{
1117 struct tc_data *tc = connector_to_tc(connector);
1118 struct edid *edid;
1119 unsigned int count;
1120
1121 if (tc->panel && tc->panel->funcs && tc->panel->funcs->get_modes) {
1122 count = tc->panel->funcs->get_modes(tc->panel);
1123 if (count > 0)
1124 return count;
1125 }
1126
1127 edid = drm_get_edid(connector, &tc->aux.ddc);
1128
1129 kfree(tc->edid);
1130 tc->edid = edid;
1131 if (!edid)
1132 return 0;
1133
1134 drm_mode_connector_update_edid_property(connector, edid);
1135 count = drm_add_edid_modes(connector, edid);
1136
1137 return count;
1138}
1139
1140static void tc_connector_set_polling(struct tc_data *tc,
1141 struct drm_connector *connector)
1142{
1143 /* TODO: add support for HPD */
1144 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1145 DRM_CONNECTOR_POLL_DISCONNECT;
1146}
1147
1148static struct drm_encoder *
1149tc_connector_best_encoder(struct drm_connector *connector)
1150{
1151 struct tc_data *tc = connector_to_tc(connector);
1152
1153 return tc->bridge.encoder;
1154}
1155
1156static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
1157 .get_modes = tc_connector_get_modes,
1158 .mode_valid = tc_connector_mode_valid,
1159 .best_encoder = tc_connector_best_encoder,
1160};
1161
1162static const struct drm_connector_funcs tc_connector_funcs = {
1163 .dpms = drm_atomic_helper_connector_dpms,
1164 .fill_modes = drm_helper_probe_single_connector_modes,
1165 .destroy = drm_connector_cleanup,
1166 .reset = drm_atomic_helper_connector_reset,
1167 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1168 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1169};
1170
1171static int tc_bridge_attach(struct drm_bridge *bridge)
1172{
1173 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1174 struct tc_data *tc = bridge_to_tc(bridge);
1175 struct drm_device *drm = bridge->dev;
1176 int ret;
1177
1178 /* Create eDP connector */
1179 drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
1180 ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs,
1181 DRM_MODE_CONNECTOR_eDP);
1182 if (ret)
1183 return ret;
1184
1185 if (tc->panel)
1186 drm_panel_attach(tc->panel, &tc->connector);
1187
1188 drm_display_info_set_bus_formats(&tc->connector.display_info,
1189 &bus_format, 1);
1190 drm_mode_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
1191
1192 return 0;
1193}
1194
1195static const struct drm_bridge_funcs tc_bridge_funcs = {
1196 .attach = tc_bridge_attach,
1197 .mode_set = tc_bridge_mode_set,
1198 .pre_enable = tc_bridge_pre_enable,
1199 .enable = tc_bridge_enable,
1200 .disable = tc_bridge_disable,
1201 .post_disable = tc_bridge_post_disable,
1202 .mode_fixup = tc_bridge_mode_fixup,
1203};
1204
1205static bool tc_readable_reg(struct device *dev, unsigned int reg)
1206{
1207 return reg != SYSCTRL;
1208}
1209
1210static const struct regmap_range tc_volatile_ranges[] = {
1211 regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
1212 regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
1213 regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
1214 regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
1215 regmap_reg_range(VFUEN0, VFUEN0),
1216};
1217
1218static const struct regmap_access_table tc_volatile_table = {
1219 .yes_ranges = tc_volatile_ranges,
1220 .n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
1221};
1222
1223static bool tc_writeable_reg(struct device *dev, unsigned int reg)
1224{
1225 return (reg != TC_IDREG) &&
1226 (reg != DP0_LTSTAT) &&
1227 (reg != DP0_SNKLTCHGREQ);
1228}
1229
1230static const struct regmap_config tc_regmap_config = {
1231 .name = "tc358767",
1232 .reg_bits = 16,
1233 .val_bits = 32,
1234 .reg_stride = 4,
1235 .max_register = PLL_DBG,
1236 .cache_type = REGCACHE_RBTREE,
1237 .readable_reg = tc_readable_reg,
1238 .volatile_table = &tc_volatile_table,
1239 .writeable_reg = tc_writeable_reg,
1240 .reg_format_endian = REGMAP_ENDIAN_BIG,
1241 .val_format_endian = REGMAP_ENDIAN_LITTLE,
1242};
1243
1244static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
1245{
1246 struct device *dev = &client->dev;
1247 struct device_node *ep;
1248 struct tc_data *tc;
1249 int ret;
1250
1251 tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
1252 if (!tc)
1253 return -ENOMEM;
1254
1255 tc->dev = dev;
1256
1257 /* port@2 is the output port */
1258 ep = of_graph_get_endpoint_by_regs(dev->of_node, 2, -1);
1259 if (ep) {
1260 struct device_node *remote;
1261
1262 remote = of_graph_get_remote_port_parent(ep);
1263 if (!remote) {
1264 dev_warn(dev, "endpoint %s not connected\n",
1265 ep->full_name);
1266 of_node_put(ep);
1267 return -ENODEV;
1268 }
1269 of_node_put(ep);
1270 tc->panel = of_drm_find_panel(remote);
1271 if (tc->panel) {
1272 dev_dbg(dev, "found panel %s\n", remote->full_name);
1273 } else {
1274 dev_dbg(dev, "waiting for panel %s\n",
1275 remote->full_name);
1276 of_node_put(remote);
1277 return -EPROBE_DEFER;
1278 }
1279 of_node_put(remote);
1280 }
1281
1282 /* Shut down GPIO is optional */
1283 tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
1284 if (IS_ERR(tc->sd_gpio))
1285 return PTR_ERR(tc->sd_gpio);
1286
1287 if (tc->sd_gpio) {
1288 gpiod_set_value_cansleep(tc->sd_gpio, 0);
1289 usleep_range(5000, 10000);
1290 }
1291
1292 /* Reset GPIO is optional */
1293 tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
1294 if (IS_ERR(tc->reset_gpio))
1295 return PTR_ERR(tc->reset_gpio);
1296
1297 if (tc->reset_gpio) {
1298 gpiod_set_value_cansleep(tc->reset_gpio, 1);
1299 usleep_range(5000, 10000);
1300 }
1301
1302 tc->refclk = devm_clk_get(dev, "ref");
1303 if (IS_ERR(tc->refclk)) {
1304 ret = PTR_ERR(tc->refclk);
1305 dev_err(dev, "Failed to get refclk: %d\n", ret);
1306 return ret;
1307 }
1308
1309 tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
1310 if (IS_ERR(tc->regmap)) {
1311 ret = PTR_ERR(tc->regmap);
1312 dev_err(dev, "Failed to initialize regmap: %d\n", ret);
1313 return ret;
1314 }
1315
1316 ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
1317 if (ret) {
1318 dev_err(tc->dev, "can not read device ID: %d\n", ret);
1319 return ret;
1320 }
1321
1322 if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
1323 dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
1324 return -EINVAL;
1325 }
1326
1327 tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
1328
1329 ret = tc_aux_link_setup(tc);
1330 if (ret)
1331 return ret;
1332
1333 /* Register DP AUX channel */
1334 tc->aux.name = "TC358767 AUX i2c adapter";
1335 tc->aux.dev = tc->dev;
1336 tc->aux.transfer = tc_aux_transfer;
1337 ret = drm_dp_aux_register(&tc->aux);
1338 if (ret)
1339 return ret;
1340
1341 ret = tc_get_display_props(tc);
1342 if (ret)
1343 goto err_unregister_aux;
1344
1345 tc_connector_set_polling(tc, &tc->connector);
1346
1347 tc->bridge.funcs = &tc_bridge_funcs;
1348 tc->bridge.of_node = dev->of_node;
1349 ret = drm_bridge_add(&tc->bridge);
1350 if (ret) {
1351 dev_err(dev, "Failed to add drm_bridge: %d\n", ret);
1352 goto err_unregister_aux;
1353 }
1354
1355 i2c_set_clientdata(client, tc);
1356
1357 return 0;
1358err_unregister_aux:
1359 drm_dp_aux_unregister(&tc->aux);
1360 return ret;
1361}
1362
1363static int tc_remove(struct i2c_client *client)
1364{
1365 struct tc_data *tc = i2c_get_clientdata(client);
1366
1367 drm_bridge_remove(&tc->bridge);
1368 drm_dp_aux_unregister(&tc->aux);
1369
1370 tc_pxl_pll_dis(tc);
1371
1372 return 0;
1373}
1374
1375static const struct i2c_device_id tc358767_i2c_ids[] = {
1376 { "tc358767", 0 },
1377 { }
1378};
1379MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
1380
1381static const struct of_device_id tc358767_of_ids[] = {
1382 { .compatible = "toshiba,tc358767", },
1383 { }
1384};
1385MODULE_DEVICE_TABLE(of, tc358767_of_ids);
1386
1387static struct i2c_driver tc358767_driver = {
1388 .driver = {
1389 .name = "tc358767",
1390 .of_match_table = tc358767_of_ids,
1391 },
1392 .id_table = tc358767_i2c_ids,
1393 .probe = tc_probe,
1394 .remove = tc_remove,
1395};
1396module_i2c_driver(tc358767_driver);
1397
1398MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
1399MODULE_DESCRIPTION("tc358767 eDP encoder driver");
1400MODULE_LICENSE("GPL");