blob: e79fbc1b770441d7280a1efe537a47c1efbad630 [file] [log] [blame]
Andrey Gusakov7caff0f2016-07-13 13:19:59 +05301/*
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 *
Andrey Gusakov2f51be02017-11-07 19:56:25 +03009 * Copyright (C) 2016 Zodiac Inflight Innovations
10 *
Andrey Gusakov7caff0f2016-07-13 13:19:59 +053011 * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c
12 *
13 * Copyright (C) 2012 Texas Instruments
14 * Author: Rob Clark <robdclark@gmail.com>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 */
26
27#include <linux/clk.h>
28#include <linux/device.h>
29#include <linux/gpio/consumer.h>
30#include <linux/i2c.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/regmap.h>
34#include <linux/slab.h>
35
36#include <drm/drm_atomic_helper.h>
37#include <drm/drm_crtc_helper.h>
38#include <drm/drm_dp_helper.h>
39#include <drm/drm_edid.h>
40#include <drm/drm_of.h>
41#include <drm/drm_panel.h>
42
43/* Registers */
44
45/* Display Parallel Interface */
46#define DPIPXLFMT 0x0440
47#define VS_POL_ACTIVE_LOW (1 << 10)
48#define HS_POL_ACTIVE_LOW (1 << 9)
49#define DE_POL_ACTIVE_HIGH (0 << 8)
50#define SUB_CFG_TYPE_CONFIG1 (0 << 2) /* LSB aligned */
51#define SUB_CFG_TYPE_CONFIG2 (1 << 2) /* Loosely Packed */
52#define SUB_CFG_TYPE_CONFIG3 (2 << 2) /* LSB aligned 8-bit */
53#define DPI_BPP_RGB888 (0 << 0)
54#define DPI_BPP_RGB666 (1 << 0)
55#define DPI_BPP_RGB565 (2 << 0)
56
57/* Video Path */
58#define VPCTRL0 0x0450
59#define OPXLFMT_RGB666 (0 << 8)
60#define OPXLFMT_RGB888 (1 << 8)
61#define FRMSYNC_DISABLED (0 << 4) /* Video Timing Gen Disabled */
62#define FRMSYNC_ENABLED (1 << 4) /* Video Timing Gen Enabled */
63#define MSF_DISABLED (0 << 0) /* Magic Square FRC disabled */
64#define MSF_ENABLED (1 << 0) /* Magic Square FRC enabled */
65#define HTIM01 0x0454
66#define HTIM02 0x0458
67#define VTIM01 0x045c
68#define VTIM02 0x0460
69#define VFUEN0 0x0464
70#define VFUEN BIT(0) /* Video Frame Timing Upload */
71
72/* System */
73#define TC_IDREG 0x0500
74#define SYSCTRL 0x0510
75#define DP0_AUDSRC_NO_INPUT (0 << 3)
76#define DP0_AUDSRC_I2S_RX (1 << 3)
77#define DP0_VIDSRC_NO_INPUT (0 << 0)
78#define DP0_VIDSRC_DSI_RX (1 << 0)
79#define DP0_VIDSRC_DPI_RX (2 << 0)
80#define DP0_VIDSRC_COLOR_BAR (3 << 0)
81
82/* Control */
83#define DP0CTL 0x0600
84#define VID_MN_GEN BIT(6) /* Auto-generate M/N values */
85#define EF_EN BIT(5) /* Enable Enhanced Framing */
86#define VID_EN BIT(1) /* Video transmission enable */
87#define DP_EN BIT(0) /* Enable DPTX function */
88
89/* Clocks */
90#define DP0_VIDMNGEN0 0x0610
91#define DP0_VIDMNGEN1 0x0614
92#define DP0_VMNGENSTATUS 0x0618
93
94/* Main Channel */
95#define DP0_SECSAMPLE 0x0640
96#define DP0_VIDSYNCDELAY 0x0644
97#define DP0_TOTALVAL 0x0648
98#define DP0_STARTVAL 0x064c
99#define DP0_ACTIVEVAL 0x0650
100#define DP0_SYNCVAL 0x0654
101#define DP0_MISC 0x0658
102#define TU_SIZE_RECOMMENDED (0x3f << 16) /* LSCLK cycles per TU */
103#define BPC_6 (0 << 5)
104#define BPC_8 (1 << 5)
105
106/* AUX channel */
107#define DP0_AUXCFG0 0x0660
108#define DP0_AUXCFG1 0x0664
109#define AUX_RX_FILTER_EN BIT(16)
110
111#define DP0_AUXADDR 0x0668
112#define DP0_AUXWDATA(i) (0x066c + (i) * 4)
113#define DP0_AUXRDATA(i) (0x067c + (i) * 4)
114#define DP0_AUXSTATUS 0x068c
115#define AUX_STATUS_MASK 0xf0
116#define AUX_STATUS_SHIFT 4
117#define AUX_TIMEOUT BIT(1)
118#define AUX_BUSY BIT(0)
119#define DP0_AUXI2CADR 0x0698
120
121/* Link Training */
122#define DP0_SRCCTRL 0x06a0
123#define DP0_SRCCTRL_SCRMBLDIS BIT(13)
124#define DP0_SRCCTRL_EN810B BIT(12)
125#define DP0_SRCCTRL_NOTP (0 << 8)
126#define DP0_SRCCTRL_TP1 (1 << 8)
127#define DP0_SRCCTRL_TP2 (2 << 8)
128#define DP0_SRCCTRL_LANESKEW BIT(7)
129#define DP0_SRCCTRL_SSCG BIT(3)
130#define DP0_SRCCTRL_LANES_1 (0 << 2)
131#define DP0_SRCCTRL_LANES_2 (1 << 2)
132#define DP0_SRCCTRL_BW27 (1 << 1)
133#define DP0_SRCCTRL_BW162 (0 << 1)
134#define DP0_SRCCTRL_AUTOCORRECT BIT(0)
135#define DP0_LTSTAT 0x06d0
136#define LT_LOOPDONE BIT(13)
137#define LT_STATUS_MASK (0x1f << 8)
138#define LT_CHANNEL1_EQ_BITS (DP_CHANNEL_EQ_BITS << 4)
139#define LT_INTERLANE_ALIGN_DONE BIT(3)
140#define LT_CHANNEL0_EQ_BITS (DP_CHANNEL_EQ_BITS)
141#define DP0_SNKLTCHGREQ 0x06d4
142#define DP0_LTLOOPCTRL 0x06d8
143#define DP0_SNKLTCTRL 0x06e4
144
145/* PHY */
146#define DP_PHY_CTRL 0x0800
147#define DP_PHY_RST BIT(28) /* DP PHY Global Soft Reset */
148#define BGREN BIT(25) /* AUX PHY BGR Enable */
149#define PWR_SW_EN BIT(24) /* PHY Power Switch Enable */
150#define PHY_M1_RST BIT(12) /* Reset PHY1 Main Channel */
151#define PHY_RDY BIT(16) /* PHY Main Channels Ready */
152#define PHY_M0_RST BIT(8) /* Reset PHY0 Main Channel */
153#define PHY_A0_EN BIT(1) /* PHY Aux Channel0 Enable */
154#define PHY_M0_EN BIT(0) /* PHY Main Channel0 Enable */
155
156/* PLL */
157#define DP0_PLLCTRL 0x0900
158#define DP1_PLLCTRL 0x0904 /* not defined in DS */
159#define PXL_PLLCTRL 0x0908
160#define PLLUPDATE BIT(2)
161#define PLLBYP BIT(1)
162#define PLLEN BIT(0)
163#define PXL_PLLPARAM 0x0914
164#define IN_SEL_REFCLK (0 << 14)
165#define SYS_PLLPARAM 0x0918
166#define REF_FREQ_38M4 (0 << 8) /* 38.4 MHz */
167#define REF_FREQ_19M2 (1 << 8) /* 19.2 MHz */
168#define REF_FREQ_26M (2 << 8) /* 26 MHz */
169#define REF_FREQ_13M (3 << 8) /* 13 MHz */
170#define SYSCLK_SEL_LSCLK (0 << 4)
171#define LSCLK_DIV_1 (0 << 0)
172#define LSCLK_DIV_2 (1 << 0)
173
174/* Test & Debug */
175#define TSTCTL 0x0a00
176#define PLL_DBG 0x0a04
177
178static bool tc_test_pattern;
179module_param_named(test, tc_test_pattern, bool, 0644);
180
181struct tc_edp_link {
182 struct drm_dp_link base;
183 u8 assr;
184 int scrambler_dis;
185 int spread;
186 int coding8b10b;
187 u8 swing;
188 u8 preemp;
189};
190
191struct tc_data {
192 struct device *dev;
193 struct regmap *regmap;
194 struct drm_dp_aux aux;
195
196 struct drm_bridge bridge;
197 struct drm_connector connector;
198 struct drm_panel *panel;
199
200 /* link settings */
201 struct tc_edp_link link;
202
203 /* display edid */
204 struct edid *edid;
205 /* current mode */
206 struct drm_display_mode *mode;
207
208 u32 rev;
209 u8 assr;
210
211 struct gpio_desc *sd_gpio;
212 struct gpio_desc *reset_gpio;
213 struct clk *refclk;
214};
215
216static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
217{
218 return container_of(a, struct tc_data, aux);
219}
220
221static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
222{
223 return container_of(b, struct tc_data, bridge);
224}
225
226static inline struct tc_data *connector_to_tc(struct drm_connector *c)
227{
228 return container_of(c, struct tc_data, connector);
229}
230
231/* Simple macros to avoid repeated error checks */
232#define tc_write(reg, var) \
233 do { \
234 ret = regmap_write(tc->regmap, reg, var); \
235 if (ret) \
236 goto err; \
237 } while (0)
238#define tc_read(reg, var) \
239 do { \
240 ret = regmap_read(tc->regmap, reg, var); \
241 if (ret) \
242 goto err; \
243 } while (0)
244
245static inline int tc_poll_timeout(struct regmap *map, unsigned int addr,
246 unsigned int cond_mask,
247 unsigned int cond_value,
248 unsigned long sleep_us, u64 timeout_us)
249{
250 ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
251 unsigned int val;
252 int ret;
253
254 for (;;) {
255 ret = regmap_read(map, addr, &val);
256 if (ret)
257 break;
258 if ((val & cond_mask) == cond_value)
259 break;
260 if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) {
261 ret = regmap_read(map, addr, &val);
262 break;
263 }
264 if (sleep_us)
265 usleep_range((sleep_us >> 2) + 1, sleep_us);
266 }
267 return ret ?: (((val & cond_mask) == cond_value) ? 0 : -ETIMEDOUT);
268}
269
270static int tc_aux_wait_busy(struct tc_data *tc, unsigned int timeout_ms)
271{
272 return tc_poll_timeout(tc->regmap, DP0_AUXSTATUS, AUX_BUSY, 0,
273 1000, 1000 * timeout_ms);
274}
275
276static int tc_aux_get_status(struct tc_data *tc, u8 *reply)
277{
278 int ret;
279 u32 value;
280
281 ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &value);
282 if (ret < 0)
283 return ret;
284 if (value & AUX_BUSY) {
285 if (value & AUX_TIMEOUT) {
286 dev_err(tc->dev, "i2c access timeout!\n");
287 return -ETIMEDOUT;
288 }
289 return -EBUSY;
290 }
291
292 *reply = (value & AUX_STATUS_MASK) >> AUX_STATUS_SHIFT;
293 return 0;
294}
295
296static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
297 struct drm_dp_aux_msg *msg)
298{
299 struct tc_data *tc = aux_to_tc(aux);
300 size_t size = min_t(size_t, 8, msg->size);
301 u8 request = msg->request & ~DP_AUX_I2C_MOT;
302 u8 *buf = msg->buffer;
303 u32 tmp = 0;
304 int i = 0;
305 int ret;
306
307 if (size == 0)
308 return 0;
309
310 ret = tc_aux_wait_busy(tc, 100);
311 if (ret)
312 goto err;
313
314 if (request == DP_AUX_I2C_WRITE || request == DP_AUX_NATIVE_WRITE) {
315 /* Store data */
316 while (i < size) {
317 if (request == DP_AUX_NATIVE_WRITE)
318 tmp = tmp | (buf[i] << (8 * (i & 0x3)));
319 else
320 tmp = (tmp << 8) | buf[i];
321 i++;
322 if (((i % 4) == 0) || (i == size)) {
323 tc_write(DP0_AUXWDATA(i >> 2), tmp);
324 tmp = 0;
325 }
326 }
327 } else if (request != DP_AUX_I2C_READ &&
328 request != DP_AUX_NATIVE_READ) {
329 return -EINVAL;
330 }
331
332 /* Store address */
333 tc_write(DP0_AUXADDR, msg->address);
334 /* Start transfer */
335 tc_write(DP0_AUXCFG0, ((size - 1) << 8) | request);
336
337 ret = tc_aux_wait_busy(tc, 100);
338 if (ret)
339 goto err;
340
341 ret = tc_aux_get_status(tc, &msg->reply);
342 if (ret)
343 goto err;
344
345 if (request == DP_AUX_I2C_READ || request == DP_AUX_NATIVE_READ) {
346 /* Read data */
347 while (i < size) {
348 if ((i % 4) == 0)
349 tc_read(DP0_AUXRDATA(i >> 2), &tmp);
350 buf[i] = tmp & 0xff;
351 tmp = tmp >> 8;
352 i++;
353 }
354 }
355
356 return size;
357err:
358 return ret;
359}
360
361static const char * const training_pattern1_errors[] = {
362 "No errors",
363 "Aux write error",
364 "Aux read error",
365 "Max voltage reached error",
366 "Loop counter expired error",
367 "res", "res", "res"
368};
369
370static const char * const training_pattern2_errors[] = {
371 "No errors",
372 "Aux write error",
373 "Aux read error",
374 "Clock recovery failed error",
375 "Loop counter expired error",
376 "res", "res", "res"
377};
378
379static u32 tc_srcctrl(struct tc_data *tc)
380{
381 /*
382 * No training pattern, skew lane 1 data by two LSCLK cycles with
383 * respect to lane 0 data, AutoCorrect Mode = 0
384 */
385 u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW;
386
387 if (tc->link.scrambler_dis)
388 reg |= DP0_SRCCTRL_SCRMBLDIS; /* Scrambler Disabled */
389 if (tc->link.coding8b10b)
390 /* Enable 8/10B Encoder (TxData[19:16] not used) */
391 reg |= DP0_SRCCTRL_EN810B;
392 if (tc->link.spread)
393 reg |= DP0_SRCCTRL_SSCG; /* Spread Spectrum Enable */
394 if (tc->link.base.num_lanes == 2)
395 reg |= DP0_SRCCTRL_LANES_2; /* Two Main Channel Lanes */
396 if (tc->link.base.rate != 162000)
397 reg |= DP0_SRCCTRL_BW27; /* 2.7 Gbps link */
398 return reg;
399}
400
401static void tc_wait_pll_lock(struct tc_data *tc)
402{
403 /* Wait for PLL to lock: up to 2.09 ms, depending on refclk */
404 usleep_range(3000, 6000);
405}
406
407static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
408{
409 int ret;
410 int i_pre, best_pre = 1;
411 int i_post, best_post = 1;
412 int div, best_div = 1;
413 int mul, best_mul = 1;
414 int delta, best_delta;
415 int ext_div[] = {1, 2, 3, 5, 7};
416 int best_pixelclock = 0;
417 int vco_hi = 0;
418
419 dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
420 refclk);
421 best_delta = pixelclock;
422 /* Loop over all possible ext_divs, skipping invalid configurations */
423 for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
424 /*
425 * refclk / ext_pre_div should be in the 1 to 200 MHz range.
426 * We don't allow any refclk > 200 MHz, only check lower bounds.
427 */
428 if (refclk / ext_div[i_pre] < 1000000)
429 continue;
430 for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
431 for (div = 1; div <= 16; div++) {
432 u32 clk;
433 u64 tmp;
434
435 tmp = pixelclock * ext_div[i_pre] *
436 ext_div[i_post] * div;
437 do_div(tmp, refclk);
438 mul = tmp;
439
440 /* Check limits */
441 if ((mul < 1) || (mul > 128))
442 continue;
443
444 clk = (refclk / ext_div[i_pre] / div) * mul;
445 /*
446 * refclk * mul / (ext_pre_div * pre_div)
447 * should be in the 150 to 650 MHz range
448 */
449 if ((clk > 650000000) || (clk < 150000000))
450 continue;
451
452 clk = clk / ext_div[i_post];
453 delta = clk - pixelclock;
454
455 if (abs(delta) < abs(best_delta)) {
456 best_pre = i_pre;
457 best_post = i_post;
458 best_div = div;
459 best_mul = mul;
460 best_delta = delta;
461 best_pixelclock = clk;
462 }
463 }
464 }
465 }
466 if (best_pixelclock == 0) {
467 dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
468 pixelclock);
469 return -EINVAL;
470 }
471
472 dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
473 best_delta);
474 dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
475 ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
476
477 /* if VCO >= 300 MHz */
478 if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
479 vco_hi = 1;
480 /* see DS */
481 if (best_div == 16)
482 best_div = 0;
483 if (best_mul == 128)
484 best_mul = 0;
485
486 /* Power up PLL and switch to bypass */
487 tc_write(PXL_PLLCTRL, PLLBYP | PLLEN);
488
489 tc_write(PXL_PLLPARAM,
490 (vco_hi << 24) | /* For PLL VCO >= 300 MHz = 1 */
491 (ext_div[best_pre] << 20) | /* External Pre-divider */
492 (ext_div[best_post] << 16) | /* External Post-divider */
493 IN_SEL_REFCLK | /* Use RefClk as PLL input */
494 (best_div << 8) | /* Divider for PLL RefClk */
495 (best_mul << 0)); /* Multiplier for PLL */
496
497 /* Force PLL parameter update and disable bypass */
498 tc_write(PXL_PLLCTRL, PLLUPDATE | PLLEN);
499
500 tc_wait_pll_lock(tc);
501
502 return 0;
503err:
504 return ret;
505}
506
507static int tc_pxl_pll_dis(struct tc_data *tc)
508{
509 /* Enable PLL bypass, power down PLL */
510 return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
511}
512
513static int tc_stream_clock_calc(struct tc_data *tc)
514{
515 int ret;
516 /*
517 * If the Stream clock and Link Symbol clock are
518 * asynchronous with each other, the value of M changes over
519 * time. This way of generating link clock and stream
520 * clock is called Asynchronous Clock mode. The value M
521 * must change while the value N stays constant. The
522 * value of N in this Asynchronous Clock mode must be set
523 * to 2^15 or 32,768.
524 *
525 * LSCLK = 1/10 of high speed link clock
526 *
527 * f_STRMCLK = M/N * f_LSCLK
528 * M/N = f_STRMCLK / f_LSCLK
529 *
530 */
531 tc_write(DP0_VIDMNGEN1, 32768);
532
533 return 0;
534err:
535 return ret;
536}
537
538static int tc_aux_link_setup(struct tc_data *tc)
539{
540 unsigned long rate;
541 u32 value;
542 int ret;
543
544 rate = clk_get_rate(tc->refclk);
545 switch (rate) {
546 case 38400000:
547 value = REF_FREQ_38M4;
548 break;
549 case 26000000:
550 value = REF_FREQ_26M;
551 break;
552 case 19200000:
553 value = REF_FREQ_19M2;
554 break;
555 case 13000000:
556 value = REF_FREQ_13M;
557 break;
558 default:
559 dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
560 return -EINVAL;
561 }
562
563 /* Setup DP-PHY / PLL */
564 value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
565 tc_write(SYS_PLLPARAM, value);
566
567 tc_write(DP_PHY_CTRL, BGREN | PWR_SW_EN | BIT(2) | PHY_A0_EN);
568
569 /*
570 * Initially PLLs are in bypass. Force PLL parameter update,
571 * disable PLL bypass, enable PLL
572 */
573 tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
574 tc_wait_pll_lock(tc);
575
576 tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
577 tc_wait_pll_lock(tc);
578
579 ret = tc_poll_timeout(tc->regmap, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 1,
580 1000);
581 if (ret == -ETIMEDOUT) {
582 dev_err(tc->dev, "Timeout waiting for PHY to become ready");
583 return ret;
584 } else if (ret)
585 goto err;
586
587 /* Setup AUX link */
588 tc_write(DP0_AUXCFG1, AUX_RX_FILTER_EN |
589 (0x06 << 8) | /* Aux Bit Period Calculator Threshold */
590 (0x3f << 0)); /* Aux Response Timeout Timer */
591
592 return 0;
593err:
594 dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
595 return ret;
596}
597
598static int tc_get_display_props(struct tc_data *tc)
599{
600 int ret;
601 /* temp buffer */
602 u8 tmp[8];
603
604 /* Read DP Rx Link Capability */
605 ret = drm_dp_link_probe(&tc->aux, &tc->link.base);
606 if (ret < 0)
607 goto err_dpcd_read;
608 if ((tc->link.base.rate != 162000) && (tc->link.base.rate != 270000))
609 goto err_dpcd_inval;
610
611 ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, tmp);
612 if (ret < 0)
613 goto err_dpcd_read;
614 tc->link.spread = tmp[0] & BIT(0); /* 0.5% down spread */
615
616 ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, tmp);
617 if (ret < 0)
618 goto err_dpcd_read;
619 tc->link.coding8b10b = tmp[0] & BIT(0);
620 tc->link.scrambler_dis = 0;
621 /* read assr */
622 ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, tmp);
623 if (ret < 0)
624 goto err_dpcd_read;
625 tc->link.assr = tmp[0] & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
626
627 dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
628 tc->link.base.revision >> 4, tc->link.base.revision & 0x0f,
629 (tc->link.base.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
630 tc->link.base.num_lanes,
631 (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING) ?
632 "enhanced" : "non-enhanced");
633 dev_dbg(tc->dev, "ANSI 8B/10B: %d\n", tc->link.coding8b10b);
634 dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
635 tc->link.assr, tc->assr);
636
637 return 0;
638
639err_dpcd_read:
640 dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
641 return ret;
642err_dpcd_inval:
643 dev_err(tc->dev, "invalid DPCD\n");
644 return -EINVAL;
645}
646
647static int tc_set_video_mode(struct tc_data *tc, struct drm_display_mode *mode)
648{
649 int ret;
650 int vid_sync_dly;
651 int max_tu_symbol;
652
653 int left_margin = mode->htotal - mode->hsync_end;
654 int right_margin = mode->hsync_start - mode->hdisplay;
655 int hsync_len = mode->hsync_end - mode->hsync_start;
656 int upper_margin = mode->vtotal - mode->vsync_end;
657 int lower_margin = mode->vsync_start - mode->vdisplay;
658 int vsync_len = mode->vsync_end - mode->vsync_start;
659
660 dev_dbg(tc->dev, "set mode %dx%d\n",
661 mode->hdisplay, mode->vdisplay);
662 dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
663 left_margin, right_margin, hsync_len);
664 dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
665 upper_margin, lower_margin, vsync_len);
666 dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
667
668
669 /* LCD Ctl Frame Size */
670 tc_write(VPCTRL0, (0x40 << 20) /* VSDELAY */ |
671 OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
672 tc_write(HTIM01, (left_margin << 16) | /* H back porch */
673 (hsync_len << 0)); /* Hsync */
674 tc_write(HTIM02, (right_margin << 16) | /* H front porch */
675 (mode->hdisplay << 0)); /* width */
676 tc_write(VTIM01, (upper_margin << 16) | /* V back porch */
677 (vsync_len << 0)); /* Vsync */
678 tc_write(VTIM02, (lower_margin << 16) | /* V front porch */
679 (mode->vdisplay << 0)); /* height */
680 tc_write(VFUEN0, VFUEN); /* update settings */
681
682 /* Test pattern settings */
683 tc_write(TSTCTL,
684 (120 << 24) | /* Red Color component value */
685 (20 << 16) | /* Green Color component value */
686 (99 << 8) | /* Blue Color component value */
687 (1 << 4) | /* Enable I2C Filter */
688 (2 << 0) | /* Color bar Mode */
689 0);
690
691 /* DP Main Stream Attributes */
692 vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
693 tc_write(DP0_VIDSYNCDELAY,
694 (0x003e << 16) | /* thresh_dly */
695 (vid_sync_dly << 0));
696
697 tc_write(DP0_TOTALVAL, (mode->vtotal << 16) | (mode->htotal));
698
699 tc_write(DP0_STARTVAL,
700 ((upper_margin + vsync_len) << 16) |
701 ((left_margin + hsync_len) << 0));
702
703 tc_write(DP0_ACTIVEVAL, (mode->vdisplay << 16) | (mode->hdisplay));
704
705 tc_write(DP0_SYNCVAL, (vsync_len << 16) | (hsync_len << 0));
706
707 tc_write(DPIPXLFMT, VS_POL_ACTIVE_LOW | HS_POL_ACTIVE_LOW |
708 DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 | DPI_BPP_RGB888);
709
710 /*
711 * Recommended maximum number of symbols transferred in a transfer unit:
712 * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
713 * (output active video bandwidth in bytes))
714 * Must be less than tu_size.
715 */
716 max_tu_symbol = TU_SIZE_RECOMMENDED - 1;
717 tc_write(DP0_MISC, (max_tu_symbol << 23) | TU_SIZE_RECOMMENDED | BPC_8);
718
719 return 0;
720err:
721 return ret;
722}
723
724static int tc_link_training(struct tc_data *tc, int pattern)
725{
726 const char * const *errors;
727 u32 srcctrl = tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
728 DP0_SRCCTRL_AUTOCORRECT;
729 int timeout;
730 int retry;
731 u32 value;
732 int ret;
733
734 if (pattern == DP_TRAINING_PATTERN_1) {
735 srcctrl |= DP0_SRCCTRL_TP1;
736 errors = training_pattern1_errors;
737 } else {
738 srcctrl |= DP0_SRCCTRL_TP2;
739 errors = training_pattern2_errors;
740 }
741
742 /* Set DPCD 0x102 for Training Part 1 or 2 */
743 tc_write(DP0_SNKLTCTRL, DP_LINK_SCRAMBLING_DISABLE | pattern);
744
745 tc_write(DP0_LTLOOPCTRL,
746 (0x0f << 28) | /* Defer Iteration Count */
747 (0x0f << 24) | /* Loop Iteration Count */
748 (0x0d << 0)); /* Loop Timer Delay */
749
750 retry = 5;
751 do {
752 /* Set DP0 Training Pattern */
753 tc_write(DP0_SRCCTRL, srcctrl);
754
755 /* Enable DP0 to start Link Training */
756 tc_write(DP0CTL, DP_EN);
757
758 /* wait */
759 timeout = 1000;
760 do {
761 tc_read(DP0_LTSTAT, &value);
762 udelay(1);
763 } while ((!(value & LT_LOOPDONE)) && (--timeout));
764 if (timeout == 0) {
765 dev_err(tc->dev, "Link training timeout!\n");
766 } else {
767 int pattern = (value >> 11) & 0x3;
768 int error = (value >> 8) & 0x7;
769
770 dev_dbg(tc->dev,
771 "Link training phase %d done after %d uS: %s\n",
772 pattern, 1000 - timeout, errors[error]);
773 if (pattern == DP_TRAINING_PATTERN_1 && error == 0)
774 break;
775 if (pattern == DP_TRAINING_PATTERN_2) {
776 value &= LT_CHANNEL1_EQ_BITS |
777 LT_INTERLANE_ALIGN_DONE |
778 LT_CHANNEL0_EQ_BITS;
779 /* in case of two lanes */
780 if ((tc->link.base.num_lanes == 2) &&
781 (value == (LT_CHANNEL1_EQ_BITS |
782 LT_INTERLANE_ALIGN_DONE |
783 LT_CHANNEL0_EQ_BITS)))
784 break;
785 /* in case of one line */
786 if ((tc->link.base.num_lanes == 1) &&
787 (value == (LT_INTERLANE_ALIGN_DONE |
788 LT_CHANNEL0_EQ_BITS)))
789 break;
790 }
791 }
792 /* restart */
793 tc_write(DP0CTL, 0);
794 usleep_range(10, 20);
795 } while (--retry);
796 if (retry == 0) {
797 dev_err(tc->dev, "Failed to finish training phase %d\n",
798 pattern);
799 }
800
801 return 0;
802err:
803 return ret;
804}
805
806static int tc_main_link_setup(struct tc_data *tc)
807{
808 struct drm_dp_aux *aux = &tc->aux;
809 struct device *dev = tc->dev;
810 unsigned int rate;
811 u32 dp_phy_ctrl;
812 int timeout;
813 bool aligned;
814 bool ready;
815 u32 value;
816 int ret;
817 u8 tmp[8];
818
819 /* display mode should be set at this point */
820 if (!tc->mode)
821 return -EINVAL;
822
823 /* from excel file - DP0_SrcCtrl */
824 tc_write(DP0_SRCCTRL, DP0_SRCCTRL_SCRMBLDIS | DP0_SRCCTRL_EN810B |
825 DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_LANES_2 |
826 DP0_SRCCTRL_BW27 | DP0_SRCCTRL_AUTOCORRECT);
827 /* from excel file - DP1_SrcCtrl */
828 tc_write(0x07a0, 0x00003083);
829
830 rate = clk_get_rate(tc->refclk);
831 switch (rate) {
832 case 38400000:
833 value = REF_FREQ_38M4;
834 break;
835 case 26000000:
836 value = REF_FREQ_26M;
837 break;
838 case 19200000:
839 value = REF_FREQ_19M2;
840 break;
841 case 13000000:
842 value = REF_FREQ_13M;
843 break;
844 default:
845 return -EINVAL;
846 }
847 value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
848 tc_write(SYS_PLLPARAM, value);
849 /* Setup Main Link */
850 dp_phy_ctrl = BGREN | PWR_SW_EN | BIT(2) | PHY_A0_EN | PHY_M0_EN;
851 tc_write(DP_PHY_CTRL, dp_phy_ctrl);
852 msleep(100);
853
854 /* PLL setup */
855 tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
856 tc_wait_pll_lock(tc);
857
858 tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
859 tc_wait_pll_lock(tc);
860
861 /* PXL PLL setup */
862 if (tc_test_pattern) {
863 ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
864 1000 * tc->mode->clock);
865 if (ret)
866 goto err;
867 }
868
869 /* Reset/Enable Main Links */
870 dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST;
871 tc_write(DP_PHY_CTRL, dp_phy_ctrl);
872 usleep_range(100, 200);
873 dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
874 tc_write(DP_PHY_CTRL, dp_phy_ctrl);
875
876 timeout = 1000;
877 do {
878 tc_read(DP_PHY_CTRL, &value);
879 udelay(1);
880 } while ((!(value & PHY_RDY)) && (--timeout));
881
882 if (timeout == 0) {
883 dev_err(dev, "timeout waiting for phy become ready");
884 return -ETIMEDOUT;
885 }
886
887 /* Set misc: 8 bits per color */
888 ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
889 if (ret)
890 goto err;
891
892 /*
893 * ASSR mode
894 * on TC358767 side ASSR configured through strap pin
895 * seems there is no way to change this setting from SW
896 *
897 * check is tc configured for same mode
898 */
899 if (tc->assr != tc->link.assr) {
900 dev_dbg(dev, "Trying to set display to ASSR: %d\n",
901 tc->assr);
902 /* try to set ASSR on display side */
903 tmp[0] = tc->assr;
904 ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
905 if (ret < 0)
906 goto err_dpcd_read;
907 /* read back */
908 ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
909 if (ret < 0)
910 goto err_dpcd_read;
911
912 if (tmp[0] != tc->assr) {
Lucas Stach87291e52016-11-30 12:48:10 +0100913 dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
Andrey Gusakov7caff0f2016-07-13 13:19:59 +0530914 tc->assr);
915 /* trying with disabled scrambler */
916 tc->link.scrambler_dis = 1;
917 }
918 }
919
920 /* Setup Link & DPRx Config for Training */
921 ret = drm_dp_link_configure(aux, &tc->link.base);
922 if (ret < 0)
923 goto err_dpcd_write;
924
925 /* DOWNSPREAD_CTRL */
926 tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
927 /* MAIN_LINK_CHANNEL_CODING_SET */
928 tmp[1] = tc->link.coding8b10b ? DP_SET_ANSI_8B10B : 0x00;
929 ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
930 if (ret < 0)
931 goto err_dpcd_write;
932
933 ret = tc_link_training(tc, DP_TRAINING_PATTERN_1);
934 if (ret)
935 goto err;
936
937 ret = tc_link_training(tc, DP_TRAINING_PATTERN_2);
938 if (ret)
939 goto err;
940
941 /* Clear DPCD 0x102 */
942 /* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
943 tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
944 ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
945 if (ret < 0)
946 goto err_dpcd_write;
947
948 /* Clear Training Pattern, set AutoCorrect Mode = 1 */
949 tc_write(DP0_SRCCTRL, tc_srcctrl(tc) | DP0_SRCCTRL_AUTOCORRECT);
950
951 /* Wait */
952 timeout = 100;
953 do {
954 udelay(1);
955 /* Read DPCD 0x202-0x207 */
956 ret = drm_dp_dpcd_read_link_status(aux, tmp + 2);
957 if (ret < 0)
958 goto err_dpcd_read;
959 ready = (tmp[2] == ((DP_CHANNEL_EQ_BITS << 4) | /* Lane1 */
960 DP_CHANNEL_EQ_BITS)); /* Lane0 */
961 aligned = tmp[4] & DP_INTERLANE_ALIGN_DONE;
962 } while ((--timeout) && !(ready && aligned));
963
964 if (timeout == 0) {
965 /* Read DPCD 0x200-0x201 */
966 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT, tmp, 2);
967 if (ret < 0)
968 goto err_dpcd_read;
969 dev_info(dev, "0x0200 SINK_COUNT: 0x%02x\n", tmp[0]);
970 dev_info(dev, "0x0201 DEVICE_SERVICE_IRQ_VECTOR: 0x%02x\n",
971 tmp[1]);
972 dev_info(dev, "0x0202 LANE0_1_STATUS: 0x%02x\n", tmp[2]);
973 dev_info(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n",
974 tmp[4]);
975 dev_info(dev, "0x0205 SINK_STATUS: 0x%02x\n", tmp[5]);
976 dev_info(dev, "0x0206 ADJUST_REQUEST_LANE0_1: 0x%02x\n",
977 tmp[6]);
978
979 if (!ready)
980 dev_err(dev, "Lane0/1 not ready\n");
981 if (!aligned)
982 dev_err(dev, "Lane0/1 not aligned\n");
983 return -EAGAIN;
984 }
985
986 ret = tc_set_video_mode(tc, tc->mode);
987 if (ret)
988 goto err;
989
990 /* Set M/N */
991 ret = tc_stream_clock_calc(tc);
992 if (ret)
993 goto err;
994
995 return 0;
996err_dpcd_read:
997 dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
998 return ret;
999err_dpcd_write:
1000 dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
1001err:
1002 return ret;
1003}
1004
1005static int tc_main_link_stream(struct tc_data *tc, int state)
1006{
1007 int ret;
1008 u32 value;
1009
1010 dev_dbg(tc->dev, "stream: %d\n", state);
1011
1012 if (state) {
1013 value = VID_MN_GEN | DP_EN;
1014 if (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
1015 value |= EF_EN;
1016 tc_write(DP0CTL, value);
1017 /*
1018 * VID_EN assertion should be delayed by at least N * LSCLK
1019 * cycles from the time VID_MN_GEN is enabled in order to
1020 * generate stable values for VID_M. LSCLK is 270 MHz or
1021 * 162 MHz, VID_N is set to 32768 in tc_stream_clock_calc(),
1022 * so a delay of at least 203 us should suffice.
1023 */
1024 usleep_range(500, 1000);
1025 value |= VID_EN;
1026 tc_write(DP0CTL, value);
1027 /* Set input interface */
1028 value = DP0_AUDSRC_NO_INPUT;
1029 if (tc_test_pattern)
1030 value |= DP0_VIDSRC_COLOR_BAR;
1031 else
1032 value |= DP0_VIDSRC_DPI_RX;
1033 tc_write(SYSCTRL, value);
1034 } else {
1035 tc_write(DP0CTL, 0);
1036 }
1037
1038 return 0;
1039err:
1040 return ret;
1041}
1042
Andrey Gusakov7caff0f2016-07-13 13:19:59 +05301043static void tc_bridge_pre_enable(struct drm_bridge *bridge)
1044{
1045 struct tc_data *tc = bridge_to_tc(bridge);
1046
1047 drm_panel_prepare(tc->panel);
1048}
1049
1050static void tc_bridge_enable(struct drm_bridge *bridge)
1051{
1052 struct tc_data *tc = bridge_to_tc(bridge);
1053 int ret;
1054
1055 ret = tc_main_link_setup(tc);
1056 if (ret < 0) {
1057 dev_err(tc->dev, "main link setup error: %d\n", ret);
1058 return;
1059 }
1060
1061 ret = tc_main_link_stream(tc, 1);
1062 if (ret < 0) {
1063 dev_err(tc->dev, "main link stream start error: %d\n", ret);
1064 return;
1065 }
1066
1067 drm_panel_enable(tc->panel);
1068}
1069
1070static void tc_bridge_disable(struct drm_bridge *bridge)
1071{
1072 struct tc_data *tc = bridge_to_tc(bridge);
1073 int ret;
1074
1075 drm_panel_disable(tc->panel);
1076
1077 ret = tc_main_link_stream(tc, 0);
1078 if (ret < 0)
1079 dev_err(tc->dev, "main link stream stop error: %d\n", ret);
1080}
1081
1082static void tc_bridge_post_disable(struct drm_bridge *bridge)
1083{
1084 struct tc_data *tc = bridge_to_tc(bridge);
1085
1086 drm_panel_unprepare(tc->panel);
1087}
1088
1089static bool tc_bridge_mode_fixup(struct drm_bridge *bridge,
1090 const struct drm_display_mode *mode,
1091 struct drm_display_mode *adj)
1092{
1093 /* Fixup sync polarities, both hsync and vsync are active low */
1094 adj->flags = mode->flags;
1095 adj->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
1096 adj->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
1097
1098 return true;
1099}
1100
1101static int tc_connector_mode_valid(struct drm_connector *connector,
1102 struct drm_display_mode *mode)
1103{
1104 /* Accept any mode */
1105 return MODE_OK;
1106}
1107
1108static void tc_bridge_mode_set(struct drm_bridge *bridge,
1109 struct drm_display_mode *mode,
1110 struct drm_display_mode *adj)
1111{
1112 struct tc_data *tc = bridge_to_tc(bridge);
1113
1114 tc->mode = mode;
1115}
1116
1117static int tc_connector_get_modes(struct drm_connector *connector)
1118{
1119 struct tc_data *tc = connector_to_tc(connector);
1120 struct edid *edid;
1121 unsigned int count;
1122
1123 if (tc->panel && tc->panel->funcs && tc->panel->funcs->get_modes) {
1124 count = tc->panel->funcs->get_modes(tc->panel);
1125 if (count > 0)
1126 return count;
1127 }
1128
1129 edid = drm_get_edid(connector, &tc->aux.ddc);
1130
1131 kfree(tc->edid);
1132 tc->edid = edid;
1133 if (!edid)
1134 return 0;
1135
1136 drm_mode_connector_update_edid_property(connector, edid);
1137 count = drm_add_edid_modes(connector, edid);
1138
1139 return count;
1140}
1141
1142static void tc_connector_set_polling(struct tc_data *tc,
1143 struct drm_connector *connector)
1144{
1145 /* TODO: add support for HPD */
1146 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1147 DRM_CONNECTOR_POLL_DISCONNECT;
1148}
1149
1150static struct drm_encoder *
1151tc_connector_best_encoder(struct drm_connector *connector)
1152{
1153 struct tc_data *tc = connector_to_tc(connector);
1154
1155 return tc->bridge.encoder;
1156}
1157
1158static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
1159 .get_modes = tc_connector_get_modes,
1160 .mode_valid = tc_connector_mode_valid,
1161 .best_encoder = tc_connector_best_encoder,
1162};
1163
Andrey Gusakov7caff0f2016-07-13 13:19:59 +05301164static const struct drm_connector_funcs tc_connector_funcs = {
Andrey Gusakov7caff0f2016-07-13 13:19:59 +05301165 .fill_modes = drm_helper_probe_single_connector_modes,
Marek Vasutfdd83262016-10-05 16:31:33 +02001166 .destroy = drm_connector_cleanup,
Andrey Gusakov7caff0f2016-07-13 13:19:59 +05301167 .reset = drm_atomic_helper_connector_reset,
1168 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1169 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1170};
1171
1172static int tc_bridge_attach(struct drm_bridge *bridge)
1173{
1174 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1175 struct tc_data *tc = bridge_to_tc(bridge);
1176 struct drm_device *drm = bridge->dev;
1177 int ret;
1178
1179 /* Create eDP connector */
1180 drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
1181 ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs,
1182 DRM_MODE_CONNECTOR_eDP);
1183 if (ret)
1184 return ret;
1185
1186 if (tc->panel)
1187 drm_panel_attach(tc->panel, &tc->connector);
1188
1189 drm_display_info_set_bus_formats(&tc->connector.display_info,
1190 &bus_format, 1);
1191 drm_mode_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
1192
1193 return 0;
1194}
1195
1196static const struct drm_bridge_funcs tc_bridge_funcs = {
1197 .attach = tc_bridge_attach,
1198 .mode_set = tc_bridge_mode_set,
1199 .pre_enable = tc_bridge_pre_enable,
1200 .enable = tc_bridge_enable,
1201 .disable = tc_bridge_disable,
1202 .post_disable = tc_bridge_post_disable,
1203 .mode_fixup = tc_bridge_mode_fixup,
1204};
1205
1206static bool tc_readable_reg(struct device *dev, unsigned int reg)
1207{
1208 return reg != SYSCTRL;
1209}
1210
1211static const struct regmap_range tc_volatile_ranges[] = {
1212 regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
1213 regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
1214 regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
1215 regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
1216 regmap_reg_range(VFUEN0, VFUEN0),
1217};
1218
1219static const struct regmap_access_table tc_volatile_table = {
1220 .yes_ranges = tc_volatile_ranges,
1221 .n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
1222};
1223
1224static bool tc_writeable_reg(struct device *dev, unsigned int reg)
1225{
1226 return (reg != TC_IDREG) &&
1227 (reg != DP0_LTSTAT) &&
1228 (reg != DP0_SNKLTCHGREQ);
1229}
1230
1231static const struct regmap_config tc_regmap_config = {
1232 .name = "tc358767",
1233 .reg_bits = 16,
1234 .val_bits = 32,
1235 .reg_stride = 4,
1236 .max_register = PLL_DBG,
1237 .cache_type = REGCACHE_RBTREE,
1238 .readable_reg = tc_readable_reg,
1239 .volatile_table = &tc_volatile_table,
1240 .writeable_reg = tc_writeable_reg,
1241 .reg_format_endian = REGMAP_ENDIAN_BIG,
1242 .val_format_endian = REGMAP_ENDIAN_LITTLE,
1243};
1244
1245static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
1246{
1247 struct device *dev = &client->dev;
Andrey Gusakov7caff0f2016-07-13 13:19:59 +05301248 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 */
Rob Herringebc94462017-03-29 13:55:46 -05001258 ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &tc->panel, NULL);
Lucas Stachd6302132017-07-10 14:41:25 +02001259 if (ret && ret != -ENODEV)
Rob Herringebc94462017-03-29 13:55:46 -05001260 return ret;
Andrey Gusakov7caff0f2016-07-13 13:19:59 +05301261
1262 /* Shut down GPIO is optional */
1263 tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
1264 if (IS_ERR(tc->sd_gpio))
1265 return PTR_ERR(tc->sd_gpio);
1266
1267 if (tc->sd_gpio) {
1268 gpiod_set_value_cansleep(tc->sd_gpio, 0);
1269 usleep_range(5000, 10000);
1270 }
1271
1272 /* Reset GPIO is optional */
1273 tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
1274 if (IS_ERR(tc->reset_gpio))
1275 return PTR_ERR(tc->reset_gpio);
1276
1277 if (tc->reset_gpio) {
1278 gpiod_set_value_cansleep(tc->reset_gpio, 1);
1279 usleep_range(5000, 10000);
1280 }
1281
1282 tc->refclk = devm_clk_get(dev, "ref");
1283 if (IS_ERR(tc->refclk)) {
1284 ret = PTR_ERR(tc->refclk);
1285 dev_err(dev, "Failed to get refclk: %d\n", ret);
1286 return ret;
1287 }
1288
1289 tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
1290 if (IS_ERR(tc->regmap)) {
1291 ret = PTR_ERR(tc->regmap);
1292 dev_err(dev, "Failed to initialize regmap: %d\n", ret);
1293 return ret;
1294 }
1295
1296 ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
1297 if (ret) {
1298 dev_err(tc->dev, "can not read device ID: %d\n", ret);
1299 return ret;
1300 }
1301
1302 if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
1303 dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
1304 return -EINVAL;
1305 }
1306
1307 tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
1308
1309 ret = tc_aux_link_setup(tc);
1310 if (ret)
1311 return ret;
1312
1313 /* Register DP AUX channel */
1314 tc->aux.name = "TC358767 AUX i2c adapter";
1315 tc->aux.dev = tc->dev;
1316 tc->aux.transfer = tc_aux_transfer;
1317 ret = drm_dp_aux_register(&tc->aux);
1318 if (ret)
1319 return ret;
1320
1321 ret = tc_get_display_props(tc);
1322 if (ret)
1323 goto err_unregister_aux;
1324
1325 tc_connector_set_polling(tc, &tc->connector);
1326
1327 tc->bridge.funcs = &tc_bridge_funcs;
1328 tc->bridge.of_node = dev->of_node;
Inki Daedc017322017-07-03 17:42:26 +09001329 drm_bridge_add(&tc->bridge);
Andrey Gusakov7caff0f2016-07-13 13:19:59 +05301330
1331 i2c_set_clientdata(client, tc);
1332
1333 return 0;
1334err_unregister_aux:
1335 drm_dp_aux_unregister(&tc->aux);
1336 return ret;
1337}
1338
1339static int tc_remove(struct i2c_client *client)
1340{
1341 struct tc_data *tc = i2c_get_clientdata(client);
1342
1343 drm_bridge_remove(&tc->bridge);
1344 drm_dp_aux_unregister(&tc->aux);
1345
1346 tc_pxl_pll_dis(tc);
1347
1348 return 0;
1349}
1350
1351static const struct i2c_device_id tc358767_i2c_ids[] = {
1352 { "tc358767", 0 },
1353 { }
1354};
1355MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
1356
1357static const struct of_device_id tc358767_of_ids[] = {
1358 { .compatible = "toshiba,tc358767", },
1359 { }
1360};
1361MODULE_DEVICE_TABLE(of, tc358767_of_ids);
1362
1363static struct i2c_driver tc358767_driver = {
1364 .driver = {
1365 .name = "tc358767",
1366 .of_match_table = tc358767_of_ids,
1367 },
1368 .id_table = tc358767_i2c_ids,
1369 .probe = tc_probe,
1370 .remove = tc_remove,
1371};
1372module_i2c_driver(tc358767_driver);
1373
1374MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
1375MODULE_DESCRIPTION("tc358767 eDP encoder driver");
1376MODULE_LICENSE("GPL");