drm/nouveau/fb: add gm20b device
[linux/fpc-iii.git] / drivers / gpu / drm / bridge / analogix-anx78xx.c
bloba2a82366a7714493488257c357dbc614215712b1
1 /*
2 * Copyright(c) 2016, Analogix Semiconductor.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * Based on anx7808 driver obtained from chromeos with copyright:
14 * Copyright(c) 2013, Google Inc.
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/interrupt.h>
20 #include <linux/i2c.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_gpio.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_platform.h>
26 #include <linux/regmap.h>
27 #include <linux/types.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/regulator/consumer.h>
31 #include <drm/drmP.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_crtc.h>
34 #include <drm/drm_crtc_helper.h>
35 #include <drm/drm_dp_helper.h>
36 #include <drm/drm_edid.h>
38 #include "analogix-anx78xx.h"
40 #define I2C_NUM_ADDRESSES 5
41 #define I2C_IDX_TX_P0 0
42 #define I2C_IDX_TX_P1 1
43 #define I2C_IDX_TX_P2 2
44 #define I2C_IDX_RX_P0 3
45 #define I2C_IDX_RX_P1 4
47 #define XTAL_CLK 270 /* 27M */
48 #define AUX_CH_BUFFER_SIZE 16
49 #define AUX_WAIT_TIMEOUT_MS 15
51 static const u8 anx78xx_i2c_addresses[] = {
52 [I2C_IDX_TX_P0] = TX_P0,
53 [I2C_IDX_TX_P1] = TX_P1,
54 [I2C_IDX_TX_P2] = TX_P2,
55 [I2C_IDX_RX_P0] = RX_P0,
56 [I2C_IDX_RX_P1] = RX_P1,
59 struct anx78xx_platform_data {
60 struct regulator *dvdd10;
61 struct gpio_desc *gpiod_hpd;
62 struct gpio_desc *gpiod_pd;
63 struct gpio_desc *gpiod_reset;
65 int hpd_irq;
66 int intp_irq;
69 struct anx78xx {
70 struct drm_dp_aux aux;
71 struct drm_bridge bridge;
72 struct i2c_client *client;
73 struct edid *edid;
74 struct drm_connector connector;
75 struct drm_dp_link link;
76 struct anx78xx_platform_data pdata;
77 struct mutex lock;
80 * I2C Slave addresses of ANX7814 are mapped as TX_P0, TX_P1, TX_P2,
81 * RX_P0 and RX_P1.
83 struct i2c_client *i2c_dummy[I2C_NUM_ADDRESSES];
84 struct regmap *map[I2C_NUM_ADDRESSES];
86 u16 chipid;
87 u8 dpcd[DP_RECEIVER_CAP_SIZE];
89 bool powered;
92 static inline struct anx78xx *connector_to_anx78xx(struct drm_connector *c)
94 return container_of(c, struct anx78xx, connector);
97 static inline struct anx78xx *bridge_to_anx78xx(struct drm_bridge *bridge)
99 return container_of(bridge, struct anx78xx, bridge);
102 static int anx78xx_set_bits(struct regmap *map, u8 reg, u8 mask)
104 return regmap_update_bits(map, reg, mask, mask);
107 static int anx78xx_clear_bits(struct regmap *map, u8 reg, u8 mask)
109 return regmap_update_bits(map, reg, mask, 0);
112 static bool anx78xx_aux_op_finished(struct anx78xx *anx78xx)
114 unsigned int value;
115 int err;
117 err = regmap_read(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL2_REG,
118 &value);
119 if (err < 0)
120 return false;
122 return (value & SP_AUX_EN) == 0;
125 static int anx78xx_aux_wait(struct anx78xx *anx78xx)
127 unsigned long timeout;
128 unsigned int status;
129 int err;
131 timeout = jiffies + msecs_to_jiffies(AUX_WAIT_TIMEOUT_MS) + 1;
133 while (!anx78xx_aux_op_finished(anx78xx)) {
134 if (time_after(jiffies, timeout)) {
135 if (!anx78xx_aux_op_finished(anx78xx)) {
136 DRM_ERROR("Timed out waiting AUX to finish\n");
137 return -ETIMEDOUT;
140 break;
143 usleep_range(1000, 2000);
146 /* Read the AUX channel access status */
147 err = regmap_read(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_CH_STATUS_REG,
148 &status);
149 if (err < 0) {
150 DRM_ERROR("Failed to read from AUX channel: %d\n", err);
151 return err;
154 if (status & SP_AUX_STATUS) {
155 DRM_ERROR("Failed to wait for AUX channel (status: %02x)\n",
156 status);
157 return -ETIMEDOUT;
160 return 0;
163 static int anx78xx_aux_address(struct anx78xx *anx78xx, unsigned int addr)
165 int err;
167 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_ADDR_7_0_REG,
168 addr & 0xff);
169 if (err)
170 return err;
172 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_ADDR_15_8_REG,
173 (addr & 0xff00) >> 8);
174 if (err)
175 return err;
178 * DP AUX CH Address Register #2, only update bits[3:0]
179 * [7:4] RESERVED
180 * [3:0] AUX_ADDR[19:16], Register control AUX CH address.
182 err = regmap_update_bits(anx78xx->map[I2C_IDX_TX_P0],
183 SP_AUX_ADDR_19_16_REG,
184 SP_AUX_ADDR_19_16_MASK,
185 (addr & 0xf0000) >> 16);
187 if (err)
188 return err;
190 return 0;
193 static ssize_t anx78xx_aux_transfer(struct drm_dp_aux *aux,
194 struct drm_dp_aux_msg *msg)
196 struct anx78xx *anx78xx = container_of(aux, struct anx78xx, aux);
197 u8 ctrl1 = msg->request;
198 u8 ctrl2 = SP_AUX_EN;
199 u8 *buffer = msg->buffer;
200 int err;
202 /* The DP AUX transmit and receive buffer has 16 bytes. */
203 if (WARN_ON(msg->size > AUX_CH_BUFFER_SIZE))
204 return -E2BIG;
206 /* Zero-sized messages specify address-only transactions. */
207 if (msg->size < 1)
208 ctrl2 |= SP_ADDR_ONLY;
209 else /* For non-zero-sized set the length field. */
210 ctrl1 |= (msg->size - 1) << SP_AUX_LENGTH_SHIFT;
212 if ((msg->request & DP_AUX_I2C_READ) == 0) {
213 /* When WRITE | MOT write values to data buffer */
214 err = regmap_bulk_write(anx78xx->map[I2C_IDX_TX_P0],
215 SP_DP_BUF_DATA0_REG, buffer,
216 msg->size);
217 if (err)
218 return err;
221 /* Write address and request */
222 err = anx78xx_aux_address(anx78xx, msg->address);
223 if (err)
224 return err;
226 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL1_REG,
227 ctrl1);
228 if (err)
229 return err;
231 /* Start transaction */
232 err = regmap_update_bits(anx78xx->map[I2C_IDX_TX_P0],
233 SP_DP_AUX_CH_CTRL2_REG, SP_ADDR_ONLY |
234 SP_AUX_EN, ctrl2);
235 if (err)
236 return err;
238 err = anx78xx_aux_wait(anx78xx);
239 if (err)
240 return err;
242 msg->reply = DP_AUX_I2C_REPLY_ACK;
244 if ((msg->size > 0) && (msg->request & DP_AUX_I2C_READ)) {
245 /* Read values from data buffer */
246 err = regmap_bulk_read(anx78xx->map[I2C_IDX_TX_P0],
247 SP_DP_BUF_DATA0_REG, buffer,
248 msg->size);
249 if (err)
250 return err;
253 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
254 SP_DP_AUX_CH_CTRL2_REG, SP_ADDR_ONLY);
255 if (err)
256 return err;
258 return msg->size;
261 static int anx78xx_set_hpd(struct anx78xx *anx78xx)
263 int err;
265 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_RX_P0],
266 SP_TMDS_CTRL_BASE + 7, SP_PD_RT);
267 if (err)
268 return err;
270 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL3_REG,
271 SP_HPD_OUT);
272 if (err)
273 return err;
275 return 0;
278 static int anx78xx_clear_hpd(struct anx78xx *anx78xx)
280 int err;
282 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL3_REG,
283 SP_HPD_OUT);
284 if (err)
285 return err;
287 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
288 SP_TMDS_CTRL_BASE + 7, SP_PD_RT);
289 if (err)
290 return err;
292 return 0;
295 static const struct reg_sequence tmds_phy_initialization[] = {
296 { SP_TMDS_CTRL_BASE + 1, 0x90 },
297 { SP_TMDS_CTRL_BASE + 2, 0xa9 },
298 { SP_TMDS_CTRL_BASE + 6, 0x92 },
299 { SP_TMDS_CTRL_BASE + 7, 0x80 },
300 { SP_TMDS_CTRL_BASE + 20, 0xf2 },
301 { SP_TMDS_CTRL_BASE + 22, 0xc4 },
302 { SP_TMDS_CTRL_BASE + 23, 0x18 },
305 static int anx78xx_rx_initialization(struct anx78xx *anx78xx)
307 int err;
309 err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_HDMI_MUTE_CTRL_REG,
310 SP_AUD_MUTE | SP_VID_MUTE);
311 if (err)
312 return err;
314 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0], SP_CHIP_CTRL_REG,
315 SP_MAN_HDMI5V_DET | SP_PLLLOCK_CKDT_EN |
316 SP_DIGITAL_CKDT_EN);
317 if (err)
318 return err;
320 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
321 SP_SOFTWARE_RESET1_REG, SP_HDCP_MAN_RST |
322 SP_SW_MAN_RST | SP_TMDS_RST | SP_VIDEO_RST);
323 if (err)
324 return err;
326 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_RX_P0],
327 SP_SOFTWARE_RESET1_REG, SP_HDCP_MAN_RST |
328 SP_SW_MAN_RST | SP_TMDS_RST | SP_VIDEO_RST);
329 if (err)
330 return err;
332 /* Sync detect change, GP set mute */
333 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
334 SP_AUD_EXCEPTION_ENABLE_BASE + 1, BIT(5) |
335 BIT(6));
336 if (err)
337 return err;
339 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
340 SP_AUD_EXCEPTION_ENABLE_BASE + 3,
341 SP_AEC_EN21);
342 if (err)
343 return err;
345 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0], SP_AUDVID_CTRL_REG,
346 SP_AVC_EN | SP_AAC_OE | SP_AAC_EN);
347 if (err)
348 return err;
350 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_RX_P0],
351 SP_SYSTEM_POWER_DOWN1_REG, SP_PWDN_CTRL);
352 if (err)
353 return err;
355 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
356 SP_VID_DATA_RANGE_CTRL_REG, SP_R2Y_INPUT_LIMIT);
357 if (err)
358 return err;
360 /* Enable DDC stretch */
361 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
362 SP_DP_EXTRA_I2C_DEV_ADDR_REG, SP_I2C_EXTRA_ADDR);
363 if (err)
364 return err;
366 /* TMDS phy initialization */
367 err = regmap_multi_reg_write(anx78xx->map[I2C_IDX_RX_P0],
368 tmds_phy_initialization,
369 ARRAY_SIZE(tmds_phy_initialization));
370 if (err)
371 return err;
373 err = anx78xx_clear_hpd(anx78xx);
374 if (err)
375 return err;
377 return 0;
380 static const u8 dp_tx_output_precise_tune_bits[20] = {
381 0x01, 0x03, 0x07, 0x7f, 0x71, 0x6b, 0x7f,
382 0x73, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x00,
383 0x0c, 0x42, 0x1e, 0x3e, 0x72, 0x7e,
386 static int anx78xx_link_phy_initialization(struct anx78xx *anx78xx)
388 int err;
391 * REVISIT : It is writing to a RESERVED bits in Analog Control 0
392 * register.
394 err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_ANALOG_CTRL0_REG,
395 0x02);
396 if (err)
397 return err;
400 * Write DP TX output emphasis precise tune bits.
402 err = regmap_bulk_write(anx78xx->map[I2C_IDX_TX_P1],
403 SP_DP_TX_LT_CTRL0_REG,
404 dp_tx_output_precise_tune_bits,
405 ARRAY_SIZE(dp_tx_output_precise_tune_bits));
407 if (err)
408 return err;
410 return 0;
413 static int anx78xx_xtal_clk_sel(struct anx78xx *anx78xx)
415 unsigned int value;
416 int err;
418 err = regmap_update_bits(anx78xx->map[I2C_IDX_TX_P2],
419 SP_ANALOG_DEBUG2_REG,
420 SP_XTAL_FRQ | SP_FORCE_SW_OFF_BYPASS,
421 SP_XTAL_FRQ_27M);
422 if (err)
423 return err;
425 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL3_REG,
426 XTAL_CLK & SP_WAIT_COUNTER_7_0_MASK);
427 if (err)
428 return err;
430 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL4_REG,
431 ((XTAL_CLK & 0xff00) >> 2) | (XTAL_CLK / 10));
432 if (err)
433 return err;
435 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
436 SP_I2C_GEN_10US_TIMER0_REG, XTAL_CLK & 0xff);
437 if (err)
438 return err;
440 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
441 SP_I2C_GEN_10US_TIMER1_REG,
442 (XTAL_CLK & 0xff00) >> 8);
443 if (err)
444 return err;
446 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_MISC_CTRL_REG,
447 XTAL_CLK / 10 - 1);
448 if (err)
449 return err;
451 err = regmap_read(anx78xx->map[I2C_IDX_RX_P0],
452 SP_HDMI_US_TIMER_CTRL_REG,
453 &value);
454 if (err)
455 return err;
457 err = regmap_write(anx78xx->map[I2C_IDX_RX_P0],
458 SP_HDMI_US_TIMER_CTRL_REG,
459 (value & SP_MS_TIMER_MARGIN_10_8_MASK) |
460 ((((XTAL_CLK / 10) >> 1) - 2) << 3));
461 if (err)
462 return err;
464 return 0;
467 static const struct reg_sequence otp_key_protect[] = {
468 { SP_OTP_KEY_PROTECT1_REG, SP_OTP_PSW1 },
469 { SP_OTP_KEY_PROTECT2_REG, SP_OTP_PSW2 },
470 { SP_OTP_KEY_PROTECT3_REG, SP_OTP_PSW3 },
473 static int anx78xx_tx_initialization(struct anx78xx *anx78xx)
475 int err;
477 /* Set terminal resistor to 50 ohm */
478 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL2_REG,
479 0x30);
480 if (err)
481 return err;
483 /* Enable aux double diff output */
484 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
485 SP_DP_AUX_CH_CTRL2_REG, 0x08);
486 if (err)
487 return err;
489 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
490 SP_DP_HDCP_CTRL_REG, SP_AUTO_EN |
491 SP_AUTO_START);
492 if (err)
493 return err;
495 err = regmap_multi_reg_write(anx78xx->map[I2C_IDX_TX_P0],
496 otp_key_protect,
497 ARRAY_SIZE(otp_key_protect));
498 if (err)
499 return err;
501 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
502 SP_HDCP_KEY_COMMAND_REG, SP_DISABLE_SYNC_HDCP);
503 if (err)
504 return err;
506 err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL8_REG,
507 SP_VID_VRES_TH);
508 if (err)
509 return err;
512 * DP HDCP auto authentication wait timer (when downstream starts to
513 * auth, DP side will wait for this period then do auth automatically)
515 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_HDCP_AUTO_TIMER_REG,
516 0x00);
517 if (err)
518 return err;
520 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
521 SP_DP_HDCP_CTRL_REG, SP_LINK_POLLING);
522 if (err)
523 return err;
525 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
526 SP_DP_LINK_DEBUG_CTRL_REG, SP_M_VID_DEBUG);
527 if (err)
528 return err;
530 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2],
531 SP_ANALOG_DEBUG2_REG, SP_POWERON_TIME_1P5MS);
532 if (err)
533 return err;
535 err = anx78xx_xtal_clk_sel(anx78xx);
536 if (err)
537 return err;
539 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_DEFER_CTRL_REG,
540 SP_DEFER_CTRL_EN | 0x0c);
541 if (err)
542 return err;
544 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
545 SP_DP_POLLING_CTRL_REG,
546 SP_AUTO_POLLING_DISABLE);
547 if (err)
548 return err;
551 * Short the link integrity check timer to speed up bstatus
552 * polling for HDCP CTS item 1A-07
554 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
555 SP_HDCP_LINK_CHECK_TIMER_REG, 0x1d);
556 if (err)
557 return err;
559 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
560 SP_DP_MISC_CTRL_REG, SP_EQ_TRAINING_LOOP);
561 if (err)
562 return err;
564 /* Power down the main link by default */
565 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
566 SP_DP_ANALOG_POWER_DOWN_REG, SP_CH0_PD);
567 if (err)
568 return err;
570 err = anx78xx_link_phy_initialization(anx78xx);
571 if (err)
572 return err;
574 /* Gen m_clk with downspreading */
575 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
576 SP_DP_M_CALCULATION_CTRL_REG, SP_M_GEN_CLK_SEL);
577 if (err)
578 return err;
580 return 0;
583 static int anx78xx_enable_interrupts(struct anx78xx *anx78xx)
585 int err;
588 * BIT0: INT pin assertion polarity: 1 = assert high
589 * BIT1: INT pin output type: 0 = push/pull
591 err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_INT_CTRL_REG, 0x01);
592 if (err)
593 return err;
595 err = regmap_write(anx78xx->map[I2C_IDX_TX_P2],
596 SP_COMMON_INT_MASK4_REG, SP_HPD_LOST | SP_HPD_PLUG);
597 if (err)
598 return err;
600 err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_MASK1_REG,
601 SP_TRAINING_FINISH);
602 if (err)
603 return err;
605 err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_INT_MASK1_REG,
606 SP_CKDT_CHG | SP_SCDT_CHG);
607 if (err)
608 return err;
610 return 0;
613 static void anx78xx_poweron(struct anx78xx *anx78xx)
615 struct anx78xx_platform_data *pdata = &anx78xx->pdata;
616 int err;
618 if (WARN_ON(anx78xx->powered))
619 return;
621 if (pdata->dvdd10) {
622 err = regulator_enable(pdata->dvdd10);
623 if (err) {
624 DRM_ERROR("Failed to enable DVDD10 regulator: %d\n",
625 err);
626 return;
629 usleep_range(1000, 2000);
632 gpiod_set_value_cansleep(pdata->gpiod_reset, 1);
633 usleep_range(1000, 2000);
635 gpiod_set_value_cansleep(pdata->gpiod_pd, 0);
636 usleep_range(1000, 2000);
638 gpiod_set_value_cansleep(pdata->gpiod_reset, 0);
640 /* Power on registers module */
641 anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,
642 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
643 anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,
644 SP_REGISTER_PD | SP_TOTAL_PD);
646 anx78xx->powered = true;
649 static void anx78xx_poweroff(struct anx78xx *anx78xx)
651 struct anx78xx_platform_data *pdata = &anx78xx->pdata;
652 int err;
654 if (WARN_ON(!anx78xx->powered))
655 return;
657 gpiod_set_value_cansleep(pdata->gpiod_reset, 1);
658 usleep_range(1000, 2000);
660 gpiod_set_value_cansleep(pdata->gpiod_pd, 1);
661 usleep_range(1000, 2000);
663 if (pdata->dvdd10) {
664 err = regulator_disable(pdata->dvdd10);
665 if (err) {
666 DRM_ERROR("Failed to disable DVDD10 regulator: %d\n",
667 err);
668 return;
671 usleep_range(1000, 2000);
674 anx78xx->powered = false;
677 static int anx78xx_start(struct anx78xx *anx78xx)
679 int err;
681 /* Power on all modules */
682 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2],
683 SP_POWERDOWN_CTRL_REG,
684 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD |
685 SP_LINK_PD);
687 err = anx78xx_enable_interrupts(anx78xx);
688 if (err) {
689 DRM_ERROR("Failed to enable interrupts: %d\n", err);
690 goto err_poweroff;
693 err = anx78xx_rx_initialization(anx78xx);
694 if (err) {
695 DRM_ERROR("Failed receiver initialization: %d\n", err);
696 goto err_poweroff;
699 err = anx78xx_tx_initialization(anx78xx);
700 if (err) {
701 DRM_ERROR("Failed transmitter initialization: %d\n", err);
702 goto err_poweroff;
706 * This delay seems to help keep the hardware in a good state. Without
707 * it, there are times where it fails silently.
709 usleep_range(10000, 15000);
711 return 0;
713 err_poweroff:
714 DRM_ERROR("Failed SlimPort transmitter initialization: %d\n", err);
715 anx78xx_poweroff(anx78xx);
717 return err;
720 static int anx78xx_init_pdata(struct anx78xx *anx78xx)
722 struct anx78xx_platform_data *pdata = &anx78xx->pdata;
723 struct device *dev = &anx78xx->client->dev;
725 /* 1.0V digital core power regulator */
726 pdata->dvdd10 = devm_regulator_get(dev, "dvdd10");
727 if (IS_ERR(pdata->dvdd10)) {
728 DRM_ERROR("DVDD10 regulator not found\n");
729 return PTR_ERR(pdata->dvdd10);
732 /* GPIO for HPD */
733 pdata->gpiod_hpd = devm_gpiod_get(dev, "hpd", GPIOD_IN);
734 if (IS_ERR(pdata->gpiod_hpd))
735 return PTR_ERR(pdata->gpiod_hpd);
737 /* GPIO for chip power down */
738 pdata->gpiod_pd = devm_gpiod_get(dev, "pd", GPIOD_OUT_HIGH);
739 if (IS_ERR(pdata->gpiod_pd))
740 return PTR_ERR(pdata->gpiod_pd);
742 /* GPIO for chip reset */
743 pdata->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
745 return PTR_ERR_OR_ZERO(pdata->gpiod_reset);
748 static int anx78xx_dp_link_training(struct anx78xx *anx78xx)
750 u8 dp_bw, value;
751 int err;
753 err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_HDMI_MUTE_CTRL_REG,
754 0x0);
755 if (err)
756 return err;
758 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2],
759 SP_POWERDOWN_CTRL_REG,
760 SP_TOTAL_PD);
761 if (err)
762 return err;
764 err = drm_dp_dpcd_readb(&anx78xx->aux, DP_MAX_LINK_RATE, &dp_bw);
765 if (err < 0)
766 return err;
768 switch (dp_bw) {
769 case DP_LINK_BW_1_62:
770 case DP_LINK_BW_2_7:
771 case DP_LINK_BW_5_4:
772 break;
774 default:
775 DRM_DEBUG_KMS("DP bandwidth (%#02x) not supported\n", dp_bw);
776 return -EINVAL;
779 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL1_REG,
780 SP_VIDEO_MUTE);
781 if (err)
782 return err;
784 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2],
785 SP_VID_CTRL1_REG, SP_VIDEO_EN);
786 if (err)
787 return err;
789 /* Get DPCD info */
790 err = drm_dp_dpcd_read(&anx78xx->aux, DP_DPCD_REV,
791 &anx78xx->dpcd, DP_RECEIVER_CAP_SIZE);
792 if (err < 0) {
793 DRM_ERROR("Failed to read DPCD: %d\n", err);
794 return err;
797 /* Clear channel x SERDES power down */
798 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
799 SP_DP_ANALOG_POWER_DOWN_REG, SP_CH0_PD);
800 if (err)
801 return err;
803 /* Check link capabilities */
804 err = drm_dp_link_probe(&anx78xx->aux, &anx78xx->link);
805 if (err < 0) {
806 DRM_ERROR("Failed to probe link capabilities: %d\n", err);
807 return err;
810 /* Power up the sink */
811 err = drm_dp_link_power_up(&anx78xx->aux, &anx78xx->link);
812 if (err < 0) {
813 DRM_ERROR("Failed to power up DisplayPort link: %d\n", err);
814 return err;
817 /* Possibly enable downspread on the sink */
818 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
819 SP_DP_DOWNSPREAD_CTRL1_REG, 0);
820 if (err)
821 return err;
823 if (anx78xx->dpcd[DP_MAX_DOWNSPREAD] & DP_MAX_DOWNSPREAD_0_5) {
824 DRM_DEBUG("Enable downspread on the sink\n");
825 /* 4000PPM */
826 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
827 SP_DP_DOWNSPREAD_CTRL1_REG, 8);
828 if (err)
829 return err;
831 err = drm_dp_dpcd_writeb(&anx78xx->aux, DP_DOWNSPREAD_CTRL,
832 DP_SPREAD_AMP_0_5);
833 if (err < 0)
834 return err;
835 } else {
836 err = drm_dp_dpcd_writeb(&anx78xx->aux, DP_DOWNSPREAD_CTRL, 0);
837 if (err < 0)
838 return err;
841 /* Set the lane count and the link rate on the sink */
842 if (drm_dp_enhanced_frame_cap(anx78xx->dpcd))
843 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
844 SP_DP_SYSTEM_CTRL_BASE + 4,
845 SP_ENHANCED_MODE);
846 else
847 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
848 SP_DP_SYSTEM_CTRL_BASE + 4,
849 SP_ENHANCED_MODE);
850 if (err)
851 return err;
853 value = drm_dp_link_rate_to_bw_code(anx78xx->link.rate);
854 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
855 SP_DP_MAIN_LINK_BW_SET_REG, value);
856 if (err)
857 return err;
859 err = drm_dp_link_configure(&anx78xx->aux, &anx78xx->link);
860 if (err < 0) {
861 DRM_ERROR("Failed to configure DisplayPort link: %d\n", err);
862 return err;
865 /* Start training on the source */
866 err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_LT_CTRL_REG,
867 SP_LT_EN);
868 if (err)
869 return err;
871 return 0;
874 static int anx78xx_config_dp_output(struct anx78xx *anx78xx)
876 int err;
878 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL1_REG,
879 SP_VIDEO_MUTE);
880 if (err)
881 return err;
883 /* Enable DP output */
884 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL1_REG,
885 SP_VIDEO_EN);
886 if (err)
887 return err;
889 return 0;
892 static int anx78xx_send_video_infoframe(struct anx78xx *anx78xx,
893 struct hdmi_avi_infoframe *frame)
895 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
896 int err;
898 err = hdmi_avi_infoframe_pack(frame, buffer, sizeof(buffer));
899 if (err < 0) {
900 DRM_ERROR("Failed to pack AVI infoframe: %d\n", err);
901 return err;
904 err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
905 SP_PACKET_SEND_CTRL_REG, SP_AVI_IF_EN);
906 if (err)
907 return err;
909 err = regmap_bulk_write(anx78xx->map[I2C_IDX_TX_P2],
910 SP_INFOFRAME_AVI_DB1_REG, buffer,
911 frame->length);
912 if (err)
913 return err;
915 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
916 SP_PACKET_SEND_CTRL_REG, SP_AVI_IF_UD);
917 if (err)
918 return err;
920 err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
921 SP_PACKET_SEND_CTRL_REG, SP_AVI_IF_EN);
922 if (err)
923 return err;
925 return 0;
928 static int anx78xx_get_downstream_info(struct anx78xx *anx78xx)
930 u8 value;
931 int err;
933 err = drm_dp_dpcd_readb(&anx78xx->aux, DP_SINK_COUNT, &value);
934 if (err < 0) {
935 DRM_ERROR("Get sink count failed %d\n", err);
936 return err;
939 if (!DP_GET_SINK_COUNT(value)) {
940 DRM_ERROR("Downstream disconnected\n");
941 return -EIO;
944 return 0;
947 static int anx78xx_get_modes(struct drm_connector *connector)
949 struct anx78xx *anx78xx = connector_to_anx78xx(connector);
950 int err, num_modes = 0;
952 if (WARN_ON(!anx78xx->powered))
953 return 0;
955 if (anx78xx->edid)
956 return drm_add_edid_modes(connector, anx78xx->edid);
958 mutex_lock(&anx78xx->lock);
960 err = anx78xx_get_downstream_info(anx78xx);
961 if (err) {
962 DRM_ERROR("Failed to get downstream info: %d\n", err);
963 goto unlock;
966 anx78xx->edid = drm_get_edid(connector, &anx78xx->aux.ddc);
967 if (!anx78xx->edid) {
968 DRM_ERROR("Failed to read EDID\n");
969 goto unlock;
972 err = drm_mode_connector_update_edid_property(connector,
973 anx78xx->edid);
974 if (err) {
975 DRM_ERROR("Failed to update EDID property: %d\n", err);
976 goto unlock;
979 num_modes = drm_add_edid_modes(connector, anx78xx->edid);
980 /* Store the ELD */
981 drm_edid_to_eld(connector, anx78xx->edid);
983 unlock:
984 mutex_unlock(&anx78xx->lock);
986 return num_modes;
989 static const struct drm_connector_helper_funcs anx78xx_connector_helper_funcs = {
990 .get_modes = anx78xx_get_modes,
993 static enum drm_connector_status anx78xx_detect(struct drm_connector *connector,
994 bool force)
996 struct anx78xx *anx78xx = connector_to_anx78xx(connector);
998 if (!gpiod_get_value(anx78xx->pdata.gpiod_hpd))
999 return connector_status_disconnected;
1001 return connector_status_connected;
1004 static const struct drm_connector_funcs anx78xx_connector_funcs = {
1005 .dpms = drm_atomic_helper_connector_dpms,
1006 .fill_modes = drm_helper_probe_single_connector_modes,
1007 .detect = anx78xx_detect,
1008 .destroy = drm_connector_cleanup,
1009 .reset = drm_atomic_helper_connector_reset,
1010 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1011 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1014 static int anx78xx_bridge_attach(struct drm_bridge *bridge)
1016 struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
1017 int err;
1019 if (!bridge->encoder) {
1020 DRM_ERROR("Parent encoder object not found");
1021 return -ENODEV;
1024 /* Register aux channel */
1025 anx78xx->aux.name = "DP-AUX";
1026 anx78xx->aux.dev = &anx78xx->client->dev;
1027 anx78xx->aux.transfer = anx78xx_aux_transfer;
1029 err = drm_dp_aux_register(&anx78xx->aux);
1030 if (err < 0) {
1031 DRM_ERROR("Failed to register aux channel: %d\n", err);
1032 return err;
1035 err = drm_connector_init(bridge->dev, &anx78xx->connector,
1036 &anx78xx_connector_funcs,
1037 DRM_MODE_CONNECTOR_DisplayPort);
1038 if (err) {
1039 DRM_ERROR("Failed to initialize connector: %d\n", err);
1040 return err;
1043 drm_connector_helper_add(&anx78xx->connector,
1044 &anx78xx_connector_helper_funcs);
1046 err = drm_connector_register(&anx78xx->connector);
1047 if (err) {
1048 DRM_ERROR("Failed to register connector: %d\n", err);
1049 return err;
1052 anx78xx->connector.polled = DRM_CONNECTOR_POLL_HPD;
1054 err = drm_mode_connector_attach_encoder(&anx78xx->connector,
1055 bridge->encoder);
1056 if (err) {
1057 DRM_ERROR("Failed to link up connector to encoder: %d\n", err);
1058 return err;
1061 return 0;
1064 static bool anx78xx_bridge_mode_fixup(struct drm_bridge *bridge,
1065 const struct drm_display_mode *mode,
1066 struct drm_display_mode *adjusted_mode)
1068 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1069 return false;
1071 /* Max 1200p at 5.4 Ghz, one lane */
1072 if (mode->clock > 154000)
1073 return false;
1075 return true;
1078 static void anx78xx_bridge_disable(struct drm_bridge *bridge)
1080 struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
1082 /* Power off all modules except configuration registers access */
1083 anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,
1084 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
1087 static void anx78xx_bridge_mode_set(struct drm_bridge *bridge,
1088 struct drm_display_mode *mode,
1089 struct drm_display_mode *adjusted_mode)
1091 struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
1092 struct hdmi_avi_infoframe frame;
1093 int err;
1095 if (WARN_ON(!anx78xx->powered))
1096 return;
1098 mutex_lock(&anx78xx->lock);
1100 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, adjusted_mode);
1101 if (err) {
1102 DRM_ERROR("Failed to setup AVI infoframe: %d\n", err);
1103 goto unlock;
1106 err = anx78xx_send_video_infoframe(anx78xx, &frame);
1107 if (err)
1108 DRM_ERROR("Failed to send AVI infoframe: %d\n", err);
1110 unlock:
1111 mutex_unlock(&anx78xx->lock);
1114 static void anx78xx_bridge_enable(struct drm_bridge *bridge)
1116 struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
1117 int err;
1119 err = anx78xx_start(anx78xx);
1120 if (err) {
1121 DRM_ERROR("Failed to initialize: %d\n", err);
1122 return;
1125 err = anx78xx_set_hpd(anx78xx);
1126 if (err)
1127 DRM_ERROR("Failed to set HPD: %d\n", err);
1130 static const struct drm_bridge_funcs anx78xx_bridge_funcs = {
1131 .attach = anx78xx_bridge_attach,
1132 .mode_fixup = anx78xx_bridge_mode_fixup,
1133 .disable = anx78xx_bridge_disable,
1134 .mode_set = anx78xx_bridge_mode_set,
1135 .enable = anx78xx_bridge_enable,
1138 static irqreturn_t anx78xx_hpd_threaded_handler(int irq, void *data)
1140 struct anx78xx *anx78xx = data;
1141 int err;
1143 if (anx78xx->powered)
1144 return IRQ_HANDLED;
1146 mutex_lock(&anx78xx->lock);
1148 /* Cable is pulled, power on the chip */
1149 anx78xx_poweron(anx78xx);
1151 err = anx78xx_enable_interrupts(anx78xx);
1152 if (err)
1153 DRM_ERROR("Failed to enable interrupts: %d\n", err);
1155 mutex_unlock(&anx78xx->lock);
1157 return IRQ_HANDLED;
1160 static int anx78xx_handle_dp_int_1(struct anx78xx *anx78xx, u8 irq)
1162 int err;
1164 DRM_DEBUG_KMS("Handle DP interrupt 1: %02x\n", irq);
1166 err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_STATUS1_REG,
1167 irq);
1168 if (err)
1169 return err;
1171 if (irq & SP_TRAINING_FINISH) {
1172 DRM_DEBUG_KMS("IRQ: hardware link training finished\n");
1173 err = anx78xx_config_dp_output(anx78xx);
1176 return err;
1179 static bool anx78xx_handle_common_int_4(struct anx78xx *anx78xx, u8 irq)
1181 bool event = false;
1182 int err;
1184 DRM_DEBUG_KMS("Handle common interrupt 4: %02x\n", irq);
1186 err = regmap_write(anx78xx->map[I2C_IDX_TX_P2],
1187 SP_COMMON_INT_STATUS4_REG, irq);
1188 if (err) {
1189 DRM_ERROR("Failed to write SP_COMMON_INT_STATUS4 %d\n", err);
1190 return event;
1193 if (irq & SP_HPD_LOST) {
1194 DRM_DEBUG_KMS("IRQ: Hot plug detect - cable is pulled out\n");
1195 event = true;
1196 anx78xx_poweroff(anx78xx);
1197 /* Free cached EDID */
1198 kfree(anx78xx->edid);
1199 anx78xx->edid = NULL;
1200 } else if (irq & SP_HPD_PLUG) {
1201 DRM_DEBUG_KMS("IRQ: Hot plug detect - cable plug\n");
1202 event = true;
1205 return event;
1208 static void anx78xx_handle_hdmi_int_1(struct anx78xx *anx78xx, u8 irq)
1210 unsigned int value;
1211 int err;
1213 DRM_DEBUG_KMS("Handle HDMI interrupt 1: %02x\n", irq);
1215 err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_INT_STATUS1_REG,
1216 irq);
1217 if (err) {
1218 DRM_ERROR("Write HDMI int 1 failed: %d\n", err);
1219 return;
1222 if ((irq & SP_CKDT_CHG) || (irq & SP_SCDT_CHG)) {
1223 DRM_DEBUG_KMS("IRQ: HDMI input detected\n");
1225 err = regmap_read(anx78xx->map[I2C_IDX_RX_P0],
1226 SP_SYSTEM_STATUS_REG, &value);
1227 if (err) {
1228 DRM_ERROR("Read system status reg failed: %d\n", err);
1229 return;
1232 if (!(value & SP_TMDS_CLOCK_DET)) {
1233 DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI clock ***\n");
1234 return;
1237 if (!(value & SP_TMDS_DE_DET)) {
1238 DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI signal ***\n");
1239 return;
1242 err = anx78xx_dp_link_training(anx78xx);
1243 if (err)
1244 DRM_ERROR("Failed to start link training: %d\n", err);
1248 static irqreturn_t anx78xx_intp_threaded_handler(int unused, void *data)
1250 struct anx78xx *anx78xx = data;
1251 bool event = false;
1252 unsigned int irq;
1253 int err;
1255 mutex_lock(&anx78xx->lock);
1257 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_STATUS1_REG,
1258 &irq);
1259 if (err) {
1260 DRM_ERROR("Failed to read DP interrupt 1 status: %d\n", err);
1261 goto unlock;
1264 if (irq)
1265 anx78xx_handle_dp_int_1(anx78xx, irq);
1267 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2],
1268 SP_COMMON_INT_STATUS4_REG, &irq);
1269 if (err) {
1270 DRM_ERROR("Failed to read common interrupt 4 status: %d\n",
1271 err);
1272 goto unlock;
1275 if (irq)
1276 event = anx78xx_handle_common_int_4(anx78xx, irq);
1278 /* Make sure we are still powered after handle HPD events */
1279 if (!anx78xx->powered)
1280 goto unlock;
1282 err = regmap_read(anx78xx->map[I2C_IDX_RX_P0], SP_INT_STATUS1_REG,
1283 &irq);
1284 if (err) {
1285 DRM_ERROR("Failed to read HDMI int 1 status: %d\n", err);
1286 goto unlock;
1289 if (irq)
1290 anx78xx_handle_hdmi_int_1(anx78xx, irq);
1292 unlock:
1293 mutex_unlock(&anx78xx->lock);
1295 if (event)
1296 drm_helper_hpd_irq_event(anx78xx->connector.dev);
1298 return IRQ_HANDLED;
1301 static void unregister_i2c_dummy_clients(struct anx78xx *anx78xx)
1303 unsigned int i;
1305 for (i = 0; i < ARRAY_SIZE(anx78xx->i2c_dummy); i++)
1306 if (anx78xx->i2c_dummy[i])
1307 i2c_unregister_device(anx78xx->i2c_dummy[i]);
1310 static const struct regmap_config anx78xx_regmap_config = {
1311 .reg_bits = 8,
1312 .val_bits = 8,
1315 static const u16 anx78xx_chipid_list[] = {
1316 0x7812,
1317 0x7814,
1318 0x7818,
1321 static int anx78xx_i2c_probe(struct i2c_client *client,
1322 const struct i2c_device_id *id)
1324 struct anx78xx *anx78xx;
1325 struct anx78xx_platform_data *pdata;
1326 unsigned int i, idl, idh, version;
1327 bool found = false;
1328 int err;
1330 anx78xx = devm_kzalloc(&client->dev, sizeof(*anx78xx), GFP_KERNEL);
1331 if (!anx78xx)
1332 return -ENOMEM;
1334 pdata = &anx78xx->pdata;
1336 mutex_init(&anx78xx->lock);
1338 #if IS_ENABLED(CONFIG_OF)
1339 anx78xx->bridge.of_node = client->dev.of_node;
1340 #endif
1342 anx78xx->client = client;
1343 i2c_set_clientdata(client, anx78xx);
1345 err = anx78xx_init_pdata(anx78xx);
1346 if (err) {
1347 DRM_ERROR("Failed to initialize pdata: %d\n", err);
1348 return err;
1351 pdata->hpd_irq = gpiod_to_irq(pdata->gpiod_hpd);
1352 if (pdata->hpd_irq < 0) {
1353 DRM_ERROR("Failed to get HPD IRQ: %d\n", pdata->hpd_irq);
1354 return -ENODEV;
1357 pdata->intp_irq = client->irq;
1358 if (!pdata->intp_irq) {
1359 DRM_ERROR("Failed to get CABLE_DET and INTP IRQ\n");
1360 return -ENODEV;
1363 /* Map slave addresses of ANX7814 */
1364 for (i = 0; i < I2C_NUM_ADDRESSES; i++) {
1365 anx78xx->i2c_dummy[i] = i2c_new_dummy(client->adapter,
1366 anx78xx_i2c_addresses[i] >> 1);
1367 if (!anx78xx->i2c_dummy[i]) {
1368 err = -ENOMEM;
1369 DRM_ERROR("Failed to reserve I2C bus %02x\n",
1370 anx78xx_i2c_addresses[i]);
1371 goto err_unregister_i2c;
1374 anx78xx->map[i] = devm_regmap_init_i2c(anx78xx->i2c_dummy[i],
1375 &anx78xx_regmap_config);
1376 if (IS_ERR(anx78xx->map[i])) {
1377 err = PTR_ERR(anx78xx->map[i]);
1378 DRM_ERROR("Failed regmap initialization %02x\n",
1379 anx78xx_i2c_addresses[i]);
1380 goto err_unregister_i2c;
1384 /* Look for supported chip ID */
1385 anx78xx_poweron(anx78xx);
1387 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_IDL_REG,
1388 &idl);
1389 if (err)
1390 goto err_poweroff;
1392 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_IDH_REG,
1393 &idh);
1394 if (err)
1395 goto err_poweroff;
1397 anx78xx->chipid = (u8)idl | ((u8)idh << 8);
1399 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_VERSION_REG,
1400 &version);
1401 if (err)
1402 goto err_poweroff;
1404 for (i = 0; i < ARRAY_SIZE(anx78xx_chipid_list); i++) {
1405 if (anx78xx->chipid == anx78xx_chipid_list[i]) {
1406 DRM_INFO("Found ANX%x (ver. %d) SlimPort Transmitter\n",
1407 anx78xx->chipid, version);
1408 found = true;
1409 break;
1413 if (!found) {
1414 DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",
1415 anx78xx->chipid, version);
1416 err = -ENODEV;
1417 goto err_poweroff;
1420 err = devm_request_threaded_irq(&client->dev, pdata->hpd_irq, NULL,
1421 anx78xx_hpd_threaded_handler,
1422 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1423 "anx78xx-hpd", anx78xx);
1424 if (err) {
1425 DRM_ERROR("Failed to request CABLE_DET threaded IRQ: %d\n",
1426 err);
1427 goto err_poweroff;
1430 err = devm_request_threaded_irq(&client->dev, pdata->intp_irq, NULL,
1431 anx78xx_intp_threaded_handler,
1432 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1433 "anx78xx-intp", anx78xx);
1434 if (err) {
1435 DRM_ERROR("Failed to request INTP threaded IRQ: %d\n", err);
1436 goto err_poweroff;
1439 anx78xx->bridge.funcs = &anx78xx_bridge_funcs;
1441 err = drm_bridge_add(&anx78xx->bridge);
1442 if (err < 0) {
1443 DRM_ERROR("Failed to add drm bridge: %d\n", err);
1444 goto err_poweroff;
1447 /* If cable is pulled out, just poweroff and wait for HPD event */
1448 if (!gpiod_get_value(anx78xx->pdata.gpiod_hpd))
1449 anx78xx_poweroff(anx78xx);
1451 return 0;
1453 err_poweroff:
1454 anx78xx_poweroff(anx78xx);
1456 err_unregister_i2c:
1457 unregister_i2c_dummy_clients(anx78xx);
1458 return err;
1461 static int anx78xx_i2c_remove(struct i2c_client *client)
1463 struct anx78xx *anx78xx = i2c_get_clientdata(client);
1465 drm_bridge_remove(&anx78xx->bridge);
1467 unregister_i2c_dummy_clients(anx78xx);
1469 kfree(anx78xx->edid);
1471 return 0;
1474 static const struct i2c_device_id anx78xx_id[] = {
1475 { "anx7814", 0 },
1476 { /* sentinel */ }
1478 MODULE_DEVICE_TABLE(i2c, anx78xx_id);
1480 #if IS_ENABLED(CONFIG_OF)
1481 static const struct of_device_id anx78xx_match_table[] = {
1482 { .compatible = "analogix,anx7814", },
1483 { /* sentinel */ },
1485 MODULE_DEVICE_TABLE(of, anx78xx_match_table);
1486 #endif
1488 static struct i2c_driver anx78xx_driver = {
1489 .driver = {
1490 .name = "anx7814",
1491 .of_match_table = of_match_ptr(anx78xx_match_table),
1493 .probe = anx78xx_i2c_probe,
1494 .remove = anx78xx_i2c_remove,
1495 .id_table = anx78xx_id,
1497 module_i2c_driver(anx78xx_driver);
1499 MODULE_DESCRIPTION("ANX78xx SlimPort Transmitter driver");
1500 MODULE_AUTHOR("Enric Balletbo i Serra <enric.balletbo@collabora.com>");
1501 MODULE_LICENSE("GPL v2");