1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2016, Analogix Semiconductor.
5 * Based on anx7808 driver obtained from chromeos with copyright:
6 * Copyright(c) 2013, Google Inc.
8 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of_irq.h>
16 #include <linux/of_platform.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/types.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_bridge.h>
23 #include <drm/drm_crtc.h>
24 #include <drm/drm_dp_helper.h>
25 #include <drm/drm_edid.h>
26 #include <drm/drm_print.h>
27 #include <drm/drm_probe_helper.h>
29 #include "analogix-anx78xx.h"
31 #define I2C_NUM_ADDRESSES 5
32 #define I2C_IDX_TX_P0 0
33 #define I2C_IDX_TX_P1 1
34 #define I2C_IDX_TX_P2 2
35 #define I2C_IDX_RX_P0 3
36 #define I2C_IDX_RX_P1 4
38 #define XTAL_CLK 270 /* 27M */
40 static const u8 anx7808_i2c_addresses
[] = {
41 [I2C_IDX_TX_P0
] = 0x78,
42 [I2C_IDX_TX_P1
] = 0x7a,
43 [I2C_IDX_TX_P2
] = 0x72,
44 [I2C_IDX_RX_P0
] = 0x7e,
45 [I2C_IDX_RX_P1
] = 0x80,
48 static const u8 anx781x_i2c_addresses
[] = {
49 [I2C_IDX_TX_P0
] = 0x70,
50 [I2C_IDX_TX_P1
] = 0x7a,
51 [I2C_IDX_TX_P2
] = 0x72,
52 [I2C_IDX_RX_P0
] = 0x7e,
53 [I2C_IDX_RX_P1
] = 0x80,
56 struct anx78xx_platform_data
{
57 struct regulator
*dvdd10
;
58 struct gpio_desc
*gpiod_hpd
;
59 struct gpio_desc
*gpiod_pd
;
60 struct gpio_desc
*gpiod_reset
;
67 struct drm_dp_aux aux
;
68 struct drm_bridge bridge
;
69 struct i2c_client
*client
;
71 struct drm_connector connector
;
72 struct anx78xx_platform_data pdata
;
76 * I2C Slave addresses of ANX7814 are mapped as TX_P0, TX_P1, TX_P2,
79 struct i2c_client
*i2c_dummy
[I2C_NUM_ADDRESSES
];
80 struct regmap
*map
[I2C_NUM_ADDRESSES
];
83 u8 dpcd
[DP_RECEIVER_CAP_SIZE
];
88 static inline struct anx78xx
*connector_to_anx78xx(struct drm_connector
*c
)
90 return container_of(c
, struct anx78xx
, connector
);
93 static inline struct anx78xx
*bridge_to_anx78xx(struct drm_bridge
*bridge
)
95 return container_of(bridge
, struct anx78xx
, bridge
);
98 static int anx78xx_set_bits(struct regmap
*map
, u8 reg
, u8 mask
)
100 return regmap_update_bits(map
, reg
, mask
, mask
);
103 static int anx78xx_clear_bits(struct regmap
*map
, u8 reg
, u8 mask
)
105 return regmap_update_bits(map
, reg
, mask
, 0);
108 static ssize_t
anx78xx_aux_transfer(struct drm_dp_aux
*aux
,
109 struct drm_dp_aux_msg
*msg
)
111 struct anx78xx
*anx78xx
= container_of(aux
, struct anx78xx
, aux
);
112 return anx_dp_aux_transfer(anx78xx
->map
[I2C_IDX_TX_P0
], msg
);
115 static int anx78xx_set_hpd(struct anx78xx
*anx78xx
)
119 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
120 SP_TMDS_CTRL_BASE
+ 7, SP_PD_RT
);
124 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL3_REG
,
132 static int anx78xx_clear_hpd(struct anx78xx
*anx78xx
)
136 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL3_REG
,
141 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
142 SP_TMDS_CTRL_BASE
+ 7, SP_PD_RT
);
149 static const struct reg_sequence tmds_phy_initialization
[] = {
150 { SP_TMDS_CTRL_BASE
+ 1, 0x90 },
151 { SP_TMDS_CTRL_BASE
+ 2, 0xa9 },
152 { SP_TMDS_CTRL_BASE
+ 6, 0x92 },
153 { SP_TMDS_CTRL_BASE
+ 7, 0x80 },
154 { SP_TMDS_CTRL_BASE
+ 20, 0xf2 },
155 { SP_TMDS_CTRL_BASE
+ 22, 0xc4 },
156 { SP_TMDS_CTRL_BASE
+ 23, 0x18 },
159 static int anx78xx_rx_initialization(struct anx78xx
*anx78xx
)
163 err
= regmap_write(anx78xx
->map
[I2C_IDX_RX_P0
], SP_HDMI_MUTE_CTRL_REG
,
164 SP_AUD_MUTE
| SP_VID_MUTE
);
168 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
], SP_CHIP_CTRL_REG
,
169 SP_MAN_HDMI5V_DET
| SP_PLLLOCK_CKDT_EN
|
174 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
175 SP_SOFTWARE_RESET1_REG
, SP_HDCP_MAN_RST
|
176 SP_SW_MAN_RST
| SP_TMDS_RST
| SP_VIDEO_RST
);
180 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
181 SP_SOFTWARE_RESET1_REG
, SP_HDCP_MAN_RST
|
182 SP_SW_MAN_RST
| SP_TMDS_RST
| SP_VIDEO_RST
);
186 /* Sync detect change, GP set mute */
187 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
188 SP_AUD_EXCEPTION_ENABLE_BASE
+ 1, BIT(5) |
193 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
194 SP_AUD_EXCEPTION_ENABLE_BASE
+ 3,
199 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
], SP_AUDVID_CTRL_REG
,
200 SP_AVC_EN
| SP_AAC_OE
| SP_AAC_EN
);
204 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
205 SP_SYSTEM_POWER_DOWN1_REG
, SP_PWDN_CTRL
);
209 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
210 SP_VID_DATA_RANGE_CTRL_REG
, SP_R2Y_INPUT_LIMIT
);
214 /* Enable DDC stretch */
215 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
216 SP_DP_EXTRA_I2C_DEV_ADDR_REG
, SP_I2C_EXTRA_ADDR
);
220 /* TMDS phy initialization */
221 err
= regmap_multi_reg_write(anx78xx
->map
[I2C_IDX_RX_P0
],
222 tmds_phy_initialization
,
223 ARRAY_SIZE(tmds_phy_initialization
));
227 err
= anx78xx_clear_hpd(anx78xx
);
234 static const u8 dp_tx_output_precise_tune_bits
[20] = {
235 0x01, 0x03, 0x07, 0x7f, 0x71, 0x6b, 0x7f,
236 0x73, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x00,
237 0x0c, 0x42, 0x1e, 0x3e, 0x72, 0x7e,
240 static int anx78xx_link_phy_initialization(struct anx78xx
*anx78xx
)
245 * REVISIT : It is writing to a RESERVED bits in Analog Control 0
248 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
], SP_ANALOG_CTRL0_REG
,
254 * Write DP TX output emphasis precise tune bits.
256 err
= regmap_bulk_write(anx78xx
->map
[I2C_IDX_TX_P1
],
257 SP_DP_TX_LT_CTRL0_REG
,
258 dp_tx_output_precise_tune_bits
,
259 ARRAY_SIZE(dp_tx_output_precise_tune_bits
));
267 static int anx78xx_xtal_clk_sel(struct anx78xx
*anx78xx
)
272 err
= regmap_update_bits(anx78xx
->map
[I2C_IDX_TX_P2
],
273 SP_ANALOG_DEBUG2_REG
,
274 SP_XTAL_FRQ
| SP_FORCE_SW_OFF_BYPASS
,
279 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_DP_AUX_CH_CTRL3_REG
,
280 XTAL_CLK
& SP_WAIT_COUNTER_7_0_MASK
);
284 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_DP_AUX_CH_CTRL4_REG
,
285 ((XTAL_CLK
& 0xff00) >> 2) | (XTAL_CLK
/ 10));
289 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
290 SP_I2C_GEN_10US_TIMER0_REG
, XTAL_CLK
& 0xff);
294 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
295 SP_I2C_GEN_10US_TIMER1_REG
,
296 (XTAL_CLK
& 0xff00) >> 8);
300 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_AUX_MISC_CTRL_REG
,
305 err
= regmap_read(anx78xx
->map
[I2C_IDX_RX_P0
],
306 SP_HDMI_US_TIMER_CTRL_REG
,
311 err
= regmap_write(anx78xx
->map
[I2C_IDX_RX_P0
],
312 SP_HDMI_US_TIMER_CTRL_REG
,
313 (value
& SP_MS_TIMER_MARGIN_10_8_MASK
) |
314 ((((XTAL_CLK
/ 10) >> 1) - 2) << 3));
321 static const struct reg_sequence otp_key_protect
[] = {
322 { SP_OTP_KEY_PROTECT1_REG
, SP_OTP_PSW1
},
323 { SP_OTP_KEY_PROTECT2_REG
, SP_OTP_PSW2
},
324 { SP_OTP_KEY_PROTECT3_REG
, SP_OTP_PSW3
},
327 static int anx78xx_tx_initialization(struct anx78xx
*anx78xx
)
331 /* Set terminal resistor to 50 ohm */
332 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_DP_AUX_CH_CTRL2_REG
,
337 /* Enable aux double diff output */
338 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
339 SP_DP_AUX_CH_CTRL2_REG
, 0x08);
343 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
344 SP_DP_HDCP_CTRL_REG
, SP_AUTO_EN
|
349 err
= regmap_multi_reg_write(anx78xx
->map
[I2C_IDX_TX_P0
],
351 ARRAY_SIZE(otp_key_protect
));
355 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
356 SP_HDCP_KEY_COMMAND_REG
, SP_DISABLE_SYNC_HDCP
);
360 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL8_REG
,
366 * DP HDCP auto authentication wait timer (when downstream starts to
367 * auth, DP side will wait for this period then do auth automatically)
369 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_HDCP_AUTO_TIMER_REG
,
374 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
375 SP_DP_HDCP_CTRL_REG
, SP_LINK_POLLING
);
379 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
380 SP_DP_LINK_DEBUG_CTRL_REG
, SP_M_VID_DEBUG
);
384 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
],
385 SP_ANALOG_DEBUG2_REG
, SP_POWERON_TIME_1P5MS
);
389 err
= anx78xx_xtal_clk_sel(anx78xx
);
393 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_AUX_DEFER_CTRL_REG
,
394 SP_DEFER_CTRL_EN
| 0x0c);
398 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
399 SP_DP_POLLING_CTRL_REG
,
400 SP_AUTO_POLLING_DISABLE
);
405 * Short the link integrity check timer to speed up bstatus
406 * polling for HDCP CTS item 1A-07
408 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
409 SP_HDCP_LINK_CHECK_TIMER_REG
, 0x1d);
413 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
414 SP_DP_MISC_CTRL_REG
, SP_EQ_TRAINING_LOOP
);
418 /* Power down the main link by default */
419 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
420 SP_DP_ANALOG_POWER_DOWN_REG
, SP_CH0_PD
);
424 err
= anx78xx_link_phy_initialization(anx78xx
);
428 /* Gen m_clk with downspreading */
429 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
430 SP_DP_M_CALCULATION_CTRL_REG
, SP_M_GEN_CLK_SEL
);
437 static int anx78xx_enable_interrupts(struct anx78xx
*anx78xx
)
442 * BIT0: INT pin assertion polarity: 1 = assert high
443 * BIT1: INT pin output type: 0 = push/pull
445 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
], SP_INT_CTRL_REG
, 0x01);
449 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
],
450 SP_COMMON_INT_MASK4_REG
, SP_HPD_LOST
| SP_HPD_PLUG
);
454 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DP_INT_MASK1_REG
,
459 err
= regmap_write(anx78xx
->map
[I2C_IDX_RX_P0
], SP_INT_MASK1_REG
,
460 SP_CKDT_CHG
| SP_SCDT_CHG
);
467 static void anx78xx_poweron(struct anx78xx
*anx78xx
)
469 struct anx78xx_platform_data
*pdata
= &anx78xx
->pdata
;
472 if (WARN_ON(anx78xx
->powered
))
476 err
= regulator_enable(pdata
->dvdd10
);
478 DRM_ERROR("Failed to enable DVDD10 regulator: %d\n",
483 usleep_range(1000, 2000);
486 gpiod_set_value_cansleep(pdata
->gpiod_reset
, 1);
487 usleep_range(1000, 2000);
489 gpiod_set_value_cansleep(pdata
->gpiod_pd
, 0);
490 usleep_range(1000, 2000);
492 gpiod_set_value_cansleep(pdata
->gpiod_reset
, 0);
494 /* Power on registers module */
495 anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_POWERDOWN_CTRL_REG
,
496 SP_HDCP_PD
| SP_AUDIO_PD
| SP_VIDEO_PD
| SP_LINK_PD
);
497 anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_POWERDOWN_CTRL_REG
,
498 SP_REGISTER_PD
| SP_TOTAL_PD
);
500 anx78xx
->powered
= true;
503 static void anx78xx_poweroff(struct anx78xx
*anx78xx
)
505 struct anx78xx_platform_data
*pdata
= &anx78xx
->pdata
;
508 if (WARN_ON(!anx78xx
->powered
))
511 gpiod_set_value_cansleep(pdata
->gpiod_reset
, 1);
512 usleep_range(1000, 2000);
514 gpiod_set_value_cansleep(pdata
->gpiod_pd
, 1);
515 usleep_range(1000, 2000);
518 err
= regulator_disable(pdata
->dvdd10
);
520 DRM_ERROR("Failed to disable DVDD10 regulator: %d\n",
525 usleep_range(1000, 2000);
528 anx78xx
->powered
= false;
531 static int anx78xx_start(struct anx78xx
*anx78xx
)
535 /* Power on all modules */
536 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
],
537 SP_POWERDOWN_CTRL_REG
,
538 SP_HDCP_PD
| SP_AUDIO_PD
| SP_VIDEO_PD
|
541 err
= anx78xx_enable_interrupts(anx78xx
);
543 DRM_ERROR("Failed to enable interrupts: %d\n", err
);
547 err
= anx78xx_rx_initialization(anx78xx
);
549 DRM_ERROR("Failed receiver initialization: %d\n", err
);
553 err
= anx78xx_tx_initialization(anx78xx
);
555 DRM_ERROR("Failed transmitter initialization: %d\n", err
);
560 * This delay seems to help keep the hardware in a good state. Without
561 * it, there are times where it fails silently.
563 usleep_range(10000, 15000);
568 DRM_ERROR("Failed SlimPort transmitter initialization: %d\n", err
);
569 anx78xx_poweroff(anx78xx
);
574 static int anx78xx_init_pdata(struct anx78xx
*anx78xx
)
576 struct anx78xx_platform_data
*pdata
= &anx78xx
->pdata
;
577 struct device
*dev
= &anx78xx
->client
->dev
;
579 /* 1.0V digital core power regulator */
580 pdata
->dvdd10
= devm_regulator_get(dev
, "dvdd10");
581 if (IS_ERR(pdata
->dvdd10
)) {
582 if (PTR_ERR(pdata
->dvdd10
) != -EPROBE_DEFER
)
583 DRM_ERROR("DVDD10 regulator not found\n");
585 return PTR_ERR(pdata
->dvdd10
);
589 pdata
->gpiod_hpd
= devm_gpiod_get(dev
, "hpd", GPIOD_IN
);
590 if (IS_ERR(pdata
->gpiod_hpd
))
591 return PTR_ERR(pdata
->gpiod_hpd
);
593 /* GPIO for chip power down */
594 pdata
->gpiod_pd
= devm_gpiod_get(dev
, "pd", GPIOD_OUT_HIGH
);
595 if (IS_ERR(pdata
->gpiod_pd
))
596 return PTR_ERR(pdata
->gpiod_pd
);
598 /* GPIO for chip reset */
599 pdata
->gpiod_reset
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
601 return PTR_ERR_OR_ZERO(pdata
->gpiod_reset
);
604 static int anx78xx_dp_link_training(struct anx78xx
*anx78xx
)
609 err
= regmap_write(anx78xx
->map
[I2C_IDX_RX_P0
], SP_HDMI_MUTE_CTRL_REG
,
614 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
],
615 SP_POWERDOWN_CTRL_REG
,
620 err
= drm_dp_dpcd_readb(&anx78xx
->aux
, DP_MAX_LINK_RATE
, &dp_bw
);
625 case DP_LINK_BW_1_62
:
631 DRM_DEBUG_KMS("DP bandwidth (%#02x) not supported\n", dp_bw
);
635 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL1_REG
,
640 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
],
641 SP_VID_CTRL1_REG
, SP_VIDEO_EN
);
646 err
= drm_dp_dpcd_read(&anx78xx
->aux
, DP_DPCD_REV
,
647 &anx78xx
->dpcd
, DP_RECEIVER_CAP_SIZE
);
649 DRM_ERROR("Failed to read DPCD: %d\n", err
);
653 /* Clear channel x SERDES power down */
654 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
655 SP_DP_ANALOG_POWER_DOWN_REG
, SP_CH0_PD
);
660 * Power up the sink (DP_SET_POWER register is only available on DPCD
663 if (anx78xx
->dpcd
[DP_DPCD_REV
] >= 0x11) {
664 err
= drm_dp_dpcd_readb(&anx78xx
->aux
, DP_SET_POWER
, &dpcd
[0]);
666 DRM_ERROR("Failed to read DP_SET_POWER register: %d\n",
671 dpcd
[0] &= ~DP_SET_POWER_MASK
;
672 dpcd
[0] |= DP_SET_POWER_D0
;
674 err
= drm_dp_dpcd_writeb(&anx78xx
->aux
, DP_SET_POWER
, dpcd
[0]);
676 DRM_ERROR("Failed to power up DisplayPort link: %d\n",
682 * According to the DP 1.1 specification, a "Sink Device must
683 * exit the power saving state within 1 ms" (Section 2.5.3.1,
684 * Table 5-52, "Sink Control Field" (register 0x600).
686 usleep_range(1000, 2000);
689 /* Possibly enable downspread on the sink */
690 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
691 SP_DP_DOWNSPREAD_CTRL1_REG
, 0);
695 if (anx78xx
->dpcd
[DP_MAX_DOWNSPREAD
] & DP_MAX_DOWNSPREAD_0_5
) {
696 DRM_DEBUG("Enable downspread on the sink\n");
698 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
699 SP_DP_DOWNSPREAD_CTRL1_REG
, 8);
703 err
= drm_dp_dpcd_writeb(&anx78xx
->aux
, DP_DOWNSPREAD_CTRL
,
708 err
= drm_dp_dpcd_writeb(&anx78xx
->aux
, DP_DOWNSPREAD_CTRL
, 0);
713 /* Set the lane count and the link rate on the sink */
714 if (drm_dp_enhanced_frame_cap(anx78xx
->dpcd
))
715 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
716 SP_DP_SYSTEM_CTRL_BASE
+ 4,
719 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
720 SP_DP_SYSTEM_CTRL_BASE
+ 4,
725 dpcd
[0] = drm_dp_max_link_rate(anx78xx
->dpcd
);
726 dpcd
[0] = drm_dp_link_rate_to_bw_code(dpcd
[0]);
727 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
728 SP_DP_MAIN_LINK_BW_SET_REG
, dpcd
[0]);
732 dpcd
[1] = drm_dp_max_lane_count(anx78xx
->dpcd
);
734 if (drm_dp_enhanced_frame_cap(anx78xx
->dpcd
))
735 dpcd
[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN
;
737 err
= drm_dp_dpcd_write(&anx78xx
->aux
, DP_LINK_BW_SET
, dpcd
,
740 DRM_ERROR("Failed to configure link: %d\n", err
);
744 /* Start training on the source */
745 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_DP_LT_CTRL_REG
,
753 static int anx78xx_config_dp_output(struct anx78xx
*anx78xx
)
757 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL1_REG
,
762 /* Enable DP output */
763 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL1_REG
,
771 static int anx78xx_send_video_infoframe(struct anx78xx
*anx78xx
,
772 struct hdmi_avi_infoframe
*frame
)
774 u8 buffer
[HDMI_INFOFRAME_HEADER_SIZE
+ HDMI_AVI_INFOFRAME_SIZE
];
777 err
= hdmi_avi_infoframe_pack(frame
, buffer
, sizeof(buffer
));
779 DRM_ERROR("Failed to pack AVI infoframe: %d\n", err
);
783 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
784 SP_PACKET_SEND_CTRL_REG
, SP_AVI_IF_EN
);
788 err
= regmap_bulk_write(anx78xx
->map
[I2C_IDX_TX_P2
],
789 SP_INFOFRAME_AVI_DB1_REG
, buffer
,
794 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
795 SP_PACKET_SEND_CTRL_REG
, SP_AVI_IF_UD
);
799 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
800 SP_PACKET_SEND_CTRL_REG
, SP_AVI_IF_EN
);
807 static int anx78xx_get_downstream_info(struct anx78xx
*anx78xx
)
812 err
= drm_dp_dpcd_readb(&anx78xx
->aux
, DP_SINK_COUNT
, &value
);
814 DRM_ERROR("Get sink count failed %d\n", err
);
818 if (!DP_GET_SINK_COUNT(value
)) {
819 DRM_ERROR("Downstream disconnected\n");
826 static int anx78xx_get_modes(struct drm_connector
*connector
)
828 struct anx78xx
*anx78xx
= connector_to_anx78xx(connector
);
829 int err
, num_modes
= 0;
831 if (WARN_ON(!anx78xx
->powered
))
835 return drm_add_edid_modes(connector
, anx78xx
->edid
);
837 mutex_lock(&anx78xx
->lock
);
839 err
= anx78xx_get_downstream_info(anx78xx
);
841 DRM_ERROR("Failed to get downstream info: %d\n", err
);
845 anx78xx
->edid
= drm_get_edid(connector
, &anx78xx
->aux
.ddc
);
846 if (!anx78xx
->edid
) {
847 DRM_ERROR("Failed to read EDID\n");
851 err
= drm_connector_update_edid_property(connector
,
854 DRM_ERROR("Failed to update EDID property: %d\n", err
);
858 num_modes
= drm_add_edid_modes(connector
, anx78xx
->edid
);
861 mutex_unlock(&anx78xx
->lock
);
866 static const struct drm_connector_helper_funcs anx78xx_connector_helper_funcs
= {
867 .get_modes
= anx78xx_get_modes
,
870 static enum drm_connector_status
anx78xx_detect(struct drm_connector
*connector
,
873 struct anx78xx
*anx78xx
= connector_to_anx78xx(connector
);
875 if (!gpiod_get_value(anx78xx
->pdata
.gpiod_hpd
))
876 return connector_status_disconnected
;
878 return connector_status_connected
;
881 static const struct drm_connector_funcs anx78xx_connector_funcs
= {
882 .fill_modes
= drm_helper_probe_single_connector_modes
,
883 .detect
= anx78xx_detect
,
884 .destroy
= drm_connector_cleanup
,
885 .reset
= drm_atomic_helper_connector_reset
,
886 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
887 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
890 static int anx78xx_bridge_attach(struct drm_bridge
*bridge
)
892 struct anx78xx
*anx78xx
= bridge_to_anx78xx(bridge
);
895 if (!bridge
->encoder
) {
896 DRM_ERROR("Parent encoder object not found");
900 /* Register aux channel */
901 anx78xx
->aux
.name
= "DP-AUX";
902 anx78xx
->aux
.dev
= &anx78xx
->client
->dev
;
903 anx78xx
->aux
.transfer
= anx78xx_aux_transfer
;
905 err
= drm_dp_aux_register(&anx78xx
->aux
);
907 DRM_ERROR("Failed to register aux channel: %d\n", err
);
911 err
= drm_connector_init(bridge
->dev
, &anx78xx
->connector
,
912 &anx78xx_connector_funcs
,
913 DRM_MODE_CONNECTOR_DisplayPort
);
915 DRM_ERROR("Failed to initialize connector: %d\n", err
);
919 drm_connector_helper_add(&anx78xx
->connector
,
920 &anx78xx_connector_helper_funcs
);
922 err
= drm_connector_register(&anx78xx
->connector
);
924 DRM_ERROR("Failed to register connector: %d\n", err
);
928 anx78xx
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
930 err
= drm_connector_attach_encoder(&anx78xx
->connector
,
933 DRM_ERROR("Failed to link up connector to encoder: %d\n", err
);
940 static enum drm_mode_status
941 anx78xx_bridge_mode_valid(struct drm_bridge
*bridge
,
942 const struct drm_display_mode
*mode
)
944 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
945 return MODE_NO_INTERLACE
;
947 /* Max 1200p at 5.4 Ghz, one lane */
948 if (mode
->clock
> 154000)
949 return MODE_CLOCK_HIGH
;
954 static void anx78xx_bridge_disable(struct drm_bridge
*bridge
)
956 struct anx78xx
*anx78xx
= bridge_to_anx78xx(bridge
);
958 /* Power off all modules except configuration registers access */
959 anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_POWERDOWN_CTRL_REG
,
960 SP_HDCP_PD
| SP_AUDIO_PD
| SP_VIDEO_PD
| SP_LINK_PD
);
963 static void anx78xx_bridge_mode_set(struct drm_bridge
*bridge
,
964 const struct drm_display_mode
*mode
,
965 const struct drm_display_mode
*adjusted_mode
)
967 struct anx78xx
*anx78xx
= bridge_to_anx78xx(bridge
);
968 struct hdmi_avi_infoframe frame
;
971 if (WARN_ON(!anx78xx
->powered
))
974 mutex_lock(&anx78xx
->lock
);
976 err
= drm_hdmi_avi_infoframe_from_display_mode(&frame
,
980 DRM_ERROR("Failed to setup AVI infoframe: %d\n", err
);
984 err
= anx78xx_send_video_infoframe(anx78xx
, &frame
);
986 DRM_ERROR("Failed to send AVI infoframe: %d\n", err
);
989 mutex_unlock(&anx78xx
->lock
);
992 static void anx78xx_bridge_enable(struct drm_bridge
*bridge
)
994 struct anx78xx
*anx78xx
= bridge_to_anx78xx(bridge
);
997 err
= anx78xx_start(anx78xx
);
999 DRM_ERROR("Failed to initialize: %d\n", err
);
1003 err
= anx78xx_set_hpd(anx78xx
);
1005 DRM_ERROR("Failed to set HPD: %d\n", err
);
1008 static const struct drm_bridge_funcs anx78xx_bridge_funcs
= {
1009 .attach
= anx78xx_bridge_attach
,
1010 .mode_valid
= anx78xx_bridge_mode_valid
,
1011 .disable
= anx78xx_bridge_disable
,
1012 .mode_set
= anx78xx_bridge_mode_set
,
1013 .enable
= anx78xx_bridge_enable
,
1016 static irqreturn_t
anx78xx_hpd_threaded_handler(int irq
, void *data
)
1018 struct anx78xx
*anx78xx
= data
;
1021 if (anx78xx
->powered
)
1024 mutex_lock(&anx78xx
->lock
);
1026 /* Cable is pulled, power on the chip */
1027 anx78xx_poweron(anx78xx
);
1029 err
= anx78xx_enable_interrupts(anx78xx
);
1031 DRM_ERROR("Failed to enable interrupts: %d\n", err
);
1033 mutex_unlock(&anx78xx
->lock
);
1038 static int anx78xx_handle_dp_int_1(struct anx78xx
*anx78xx
, u8 irq
)
1042 DRM_DEBUG_KMS("Handle DP interrupt 1: %02x\n", irq
);
1044 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DP_INT_STATUS1_REG
,
1049 if (irq
& SP_TRAINING_FINISH
) {
1050 DRM_DEBUG_KMS("IRQ: hardware link training finished\n");
1051 err
= anx78xx_config_dp_output(anx78xx
);
1057 static bool anx78xx_handle_common_int_4(struct anx78xx
*anx78xx
, u8 irq
)
1062 DRM_DEBUG_KMS("Handle common interrupt 4: %02x\n", irq
);
1064 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
],
1065 SP_COMMON_INT_STATUS4_REG
, irq
);
1067 DRM_ERROR("Failed to write SP_COMMON_INT_STATUS4 %d\n", err
);
1071 if (irq
& SP_HPD_LOST
) {
1072 DRM_DEBUG_KMS("IRQ: Hot plug detect - cable is pulled out\n");
1074 anx78xx_poweroff(anx78xx
);
1075 /* Free cached EDID */
1076 kfree(anx78xx
->edid
);
1077 anx78xx
->edid
= NULL
;
1078 } else if (irq
& SP_HPD_PLUG
) {
1079 DRM_DEBUG_KMS("IRQ: Hot plug detect - cable plug\n");
1086 static void anx78xx_handle_hdmi_int_1(struct anx78xx
*anx78xx
, u8 irq
)
1091 DRM_DEBUG_KMS("Handle HDMI interrupt 1: %02x\n", irq
);
1093 err
= regmap_write(anx78xx
->map
[I2C_IDX_RX_P0
], SP_INT_STATUS1_REG
,
1096 DRM_ERROR("Write HDMI int 1 failed: %d\n", err
);
1100 if ((irq
& SP_CKDT_CHG
) || (irq
& SP_SCDT_CHG
)) {
1101 DRM_DEBUG_KMS("IRQ: HDMI input detected\n");
1103 err
= regmap_read(anx78xx
->map
[I2C_IDX_RX_P0
],
1104 SP_SYSTEM_STATUS_REG
, &value
);
1106 DRM_ERROR("Read system status reg failed: %d\n", err
);
1110 if (!(value
& SP_TMDS_CLOCK_DET
)) {
1111 DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI clock ***\n");
1115 if (!(value
& SP_TMDS_DE_DET
)) {
1116 DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI signal ***\n");
1120 err
= anx78xx_dp_link_training(anx78xx
);
1122 DRM_ERROR("Failed to start link training: %d\n", err
);
1126 static irqreturn_t
anx78xx_intp_threaded_handler(int unused
, void *data
)
1128 struct anx78xx
*anx78xx
= data
;
1133 mutex_lock(&anx78xx
->lock
);
1135 err
= regmap_read(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DP_INT_STATUS1_REG
,
1138 DRM_ERROR("Failed to read DP interrupt 1 status: %d\n", err
);
1143 anx78xx_handle_dp_int_1(anx78xx
, irq
);
1145 err
= regmap_read(anx78xx
->map
[I2C_IDX_TX_P2
],
1146 SP_COMMON_INT_STATUS4_REG
, &irq
);
1148 DRM_ERROR("Failed to read common interrupt 4 status: %d\n",
1154 event
= anx78xx_handle_common_int_4(anx78xx
, irq
);
1156 /* Make sure we are still powered after handle HPD events */
1157 if (!anx78xx
->powered
)
1160 err
= regmap_read(anx78xx
->map
[I2C_IDX_RX_P0
], SP_INT_STATUS1_REG
,
1163 DRM_ERROR("Failed to read HDMI int 1 status: %d\n", err
);
1168 anx78xx_handle_hdmi_int_1(anx78xx
, irq
);
1171 mutex_unlock(&anx78xx
->lock
);
1174 drm_helper_hpd_irq_event(anx78xx
->connector
.dev
);
1179 static void unregister_i2c_dummy_clients(struct anx78xx
*anx78xx
)
1183 for (i
= 0; i
< ARRAY_SIZE(anx78xx
->i2c_dummy
); i
++)
1184 i2c_unregister_device(anx78xx
->i2c_dummy
[i
]);
1187 static const struct regmap_config anx78xx_regmap_config
= {
1192 static const u16 anx78xx_chipid_list
[] = {
1199 static int anx78xx_i2c_probe(struct i2c_client
*client
,
1200 const struct i2c_device_id
*id
)
1202 struct anx78xx
*anx78xx
;
1203 struct anx78xx_platform_data
*pdata
;
1204 unsigned int i
, idl
, idh
, version
;
1205 const u8
*i2c_addresses
;
1209 anx78xx
= devm_kzalloc(&client
->dev
, sizeof(*anx78xx
), GFP_KERNEL
);
1213 pdata
= &anx78xx
->pdata
;
1215 mutex_init(&anx78xx
->lock
);
1217 #if IS_ENABLED(CONFIG_OF)
1218 anx78xx
->bridge
.of_node
= client
->dev
.of_node
;
1221 anx78xx
->client
= client
;
1222 i2c_set_clientdata(client
, anx78xx
);
1224 err
= anx78xx_init_pdata(anx78xx
);
1226 if (err
!= -EPROBE_DEFER
)
1227 DRM_ERROR("Failed to initialize pdata: %d\n", err
);
1232 pdata
->hpd_irq
= gpiod_to_irq(pdata
->gpiod_hpd
);
1233 if (pdata
->hpd_irq
< 0) {
1234 DRM_ERROR("Failed to get HPD IRQ: %d\n", pdata
->hpd_irq
);
1238 pdata
->intp_irq
= client
->irq
;
1239 if (!pdata
->intp_irq
) {
1240 DRM_ERROR("Failed to get CABLE_DET and INTP IRQ\n");
1244 /* Map slave addresses of ANX7814 */
1245 i2c_addresses
= device_get_match_data(&client
->dev
);
1246 for (i
= 0; i
< I2C_NUM_ADDRESSES
; i
++) {
1247 struct i2c_client
*i2c_dummy
;
1249 i2c_dummy
= i2c_new_dummy_device(client
->adapter
,
1250 i2c_addresses
[i
] >> 1);
1251 if (IS_ERR(i2c_dummy
)) {
1252 err
= PTR_ERR(i2c_dummy
);
1253 DRM_ERROR("Failed to reserve I2C bus %02x: %d\n",
1254 i2c_addresses
[i
], err
);
1255 goto err_unregister_i2c
;
1258 anx78xx
->i2c_dummy
[i
] = i2c_dummy
;
1259 anx78xx
->map
[i
] = devm_regmap_init_i2c(anx78xx
->i2c_dummy
[i
],
1260 &anx78xx_regmap_config
);
1261 if (IS_ERR(anx78xx
->map
[i
])) {
1262 err
= PTR_ERR(anx78xx
->map
[i
]);
1263 DRM_ERROR("Failed regmap initialization %02x\n",
1265 goto err_unregister_i2c
;
1269 /* Look for supported chip ID */
1270 anx78xx_poweron(anx78xx
);
1272 err
= regmap_read(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DEVICE_IDL_REG
,
1277 err
= regmap_read(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DEVICE_IDH_REG
,
1282 anx78xx
->chipid
= (u8
)idl
| ((u8
)idh
<< 8);
1284 err
= regmap_read(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DEVICE_VERSION_REG
,
1289 for (i
= 0; i
< ARRAY_SIZE(anx78xx_chipid_list
); i
++) {
1290 if (anx78xx
->chipid
== anx78xx_chipid_list
[i
]) {
1291 DRM_INFO("Found ANX%x (ver. %d) SlimPort Transmitter\n",
1292 anx78xx
->chipid
, version
);
1299 DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",
1300 anx78xx
->chipid
, version
);
1305 err
= devm_request_threaded_irq(&client
->dev
, pdata
->hpd_irq
, NULL
,
1306 anx78xx_hpd_threaded_handler
,
1307 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1308 "anx78xx-hpd", anx78xx
);
1310 DRM_ERROR("Failed to request CABLE_DET threaded IRQ: %d\n",
1315 err
= devm_request_threaded_irq(&client
->dev
, pdata
->intp_irq
, NULL
,
1316 anx78xx_intp_threaded_handler
,
1317 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1318 "anx78xx-intp", anx78xx
);
1320 DRM_ERROR("Failed to request INTP threaded IRQ: %d\n", err
);
1324 anx78xx
->bridge
.funcs
= &anx78xx_bridge_funcs
;
1326 drm_bridge_add(&anx78xx
->bridge
);
1328 /* If cable is pulled out, just poweroff and wait for HPD event */
1329 if (!gpiod_get_value(anx78xx
->pdata
.gpiod_hpd
))
1330 anx78xx_poweroff(anx78xx
);
1335 anx78xx_poweroff(anx78xx
);
1338 unregister_i2c_dummy_clients(anx78xx
);
1342 static int anx78xx_i2c_remove(struct i2c_client
*client
)
1344 struct anx78xx
*anx78xx
= i2c_get_clientdata(client
);
1346 drm_bridge_remove(&anx78xx
->bridge
);
1348 unregister_i2c_dummy_clients(anx78xx
);
1350 kfree(anx78xx
->edid
);
1355 static const struct i2c_device_id anx78xx_id
[] = {
1359 MODULE_DEVICE_TABLE(i2c
, anx78xx_id
);
1361 #if IS_ENABLED(CONFIG_OF)
1362 static const struct of_device_id anx78xx_match_table
[] = {
1363 { .compatible
= "analogix,anx7808", .data
= anx7808_i2c_addresses
},
1364 { .compatible
= "analogix,anx7812", .data
= anx781x_i2c_addresses
},
1365 { .compatible
= "analogix,anx7814", .data
= anx781x_i2c_addresses
},
1366 { .compatible
= "analogix,anx7818", .data
= anx781x_i2c_addresses
},
1369 MODULE_DEVICE_TABLE(of
, anx78xx_match_table
);
1372 static struct i2c_driver anx78xx_driver
= {
1375 .of_match_table
= of_match_ptr(anx78xx_match_table
),
1377 .probe
= anx78xx_i2c_probe
,
1378 .remove
= anx78xx_i2c_remove
,
1379 .id_table
= anx78xx_id
,
1381 module_i2c_driver(anx78xx_driver
);
1383 MODULE_DESCRIPTION("ANX78xx SlimPort Transmitter driver");
1384 MODULE_AUTHOR("Enric Balletbo i Serra <enric.balletbo@collabora.com>");
1385 MODULE_LICENSE("GPL v2");