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>
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
;
70 struct drm_dp_aux aux
;
71 struct drm_bridge bridge
;
72 struct i2c_client
*client
;
74 struct drm_connector connector
;
75 struct drm_dp_link link
;
76 struct anx78xx_platform_data pdata
;
80 * I2C Slave addresses of ANX7814 are mapped as TX_P0, TX_P1, TX_P2,
83 struct i2c_client
*i2c_dummy
[I2C_NUM_ADDRESSES
];
84 struct regmap
*map
[I2C_NUM_ADDRESSES
];
87 u8 dpcd
[DP_RECEIVER_CAP_SIZE
];
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
)
117 err
= regmap_read(anx78xx
->map
[I2C_IDX_TX_P0
], SP_DP_AUX_CH_CTRL2_REG
,
122 return (value
& SP_AUX_EN
) == 0;
125 static int anx78xx_aux_wait(struct anx78xx
*anx78xx
)
127 unsigned long timeout
;
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");
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
,
150 DRM_ERROR("Failed to read from AUX channel: %d\n", err
);
154 if (status
& SP_AUX_STATUS
) {
155 DRM_ERROR("Failed to wait for AUX channel (status: %02x)\n",
163 static int anx78xx_aux_address(struct anx78xx
*anx78xx
, unsigned int addr
)
167 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_AUX_ADDR_7_0_REG
,
172 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_AUX_ADDR_15_8_REG
,
173 (addr
& 0xff00) >> 8);
178 * DP AUX CH Address Register #2, only update bits[3:0]
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);
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
;
202 /* The DP AUX transmit and receive buffer has 16 bytes. */
203 if (WARN_ON(msg
->size
> AUX_CH_BUFFER_SIZE
))
206 /* Zero-sized messages specify address-only transactions. */
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
,
221 /* Write address and request */
222 err
= anx78xx_aux_address(anx78xx
, msg
->address
);
226 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_DP_AUX_CH_CTRL1_REG
,
231 /* Start transaction */
232 err
= regmap_update_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
233 SP_DP_AUX_CH_CTRL2_REG
, SP_ADDR_ONLY
|
238 err
= anx78xx_aux_wait(anx78xx
);
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
,
253 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
254 SP_DP_AUX_CH_CTRL2_REG
, SP_ADDR_ONLY
);
261 static int anx78xx_set_hpd(struct anx78xx
*anx78xx
)
265 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
266 SP_TMDS_CTRL_BASE
+ 7, SP_PD_RT
);
270 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL3_REG
,
278 static int anx78xx_clear_hpd(struct anx78xx
*anx78xx
)
282 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL3_REG
,
287 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
288 SP_TMDS_CTRL_BASE
+ 7, SP_PD_RT
);
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
)
309 err
= regmap_write(anx78xx
->map
[I2C_IDX_RX_P0
], SP_HDMI_MUTE_CTRL_REG
,
310 SP_AUD_MUTE
| SP_VID_MUTE
);
314 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
], SP_CHIP_CTRL_REG
,
315 SP_MAN_HDMI5V_DET
| SP_PLLLOCK_CKDT_EN
|
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
);
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
);
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) |
339 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
340 SP_AUD_EXCEPTION_ENABLE_BASE
+ 3,
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
);
350 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
351 SP_SYSTEM_POWER_DOWN1_REG
, SP_PWDN_CTRL
);
355 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_RX_P0
],
356 SP_VID_DATA_RANGE_CTRL_REG
, SP_R2Y_INPUT_LIMIT
);
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
);
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
));
373 err
= anx78xx_clear_hpd(anx78xx
);
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
)
391 * REVISIT : It is writing to a RESERVED bits in Analog Control 0
394 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
], SP_ANALOG_CTRL0_REG
,
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
));
413 static int anx78xx_xtal_clk_sel(struct anx78xx
*anx78xx
)
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
,
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
);
430 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_DP_AUX_CH_CTRL4_REG
,
431 ((XTAL_CLK
& 0xff00) >> 2) | (XTAL_CLK
/ 10));
435 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
436 SP_I2C_GEN_10US_TIMER0_REG
, XTAL_CLK
& 0xff);
440 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
441 SP_I2C_GEN_10US_TIMER1_REG
,
442 (XTAL_CLK
& 0xff00) >> 8);
446 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_AUX_MISC_CTRL_REG
,
451 err
= regmap_read(anx78xx
->map
[I2C_IDX_RX_P0
],
452 SP_HDMI_US_TIMER_CTRL_REG
,
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));
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
)
477 /* Set terminal resistor to 50 ohm */
478 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_DP_AUX_CH_CTRL2_REG
,
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);
489 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
490 SP_DP_HDCP_CTRL_REG
, SP_AUTO_EN
|
495 err
= regmap_multi_reg_write(anx78xx
->map
[I2C_IDX_TX_P0
],
497 ARRAY_SIZE(otp_key_protect
));
501 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
502 SP_HDCP_KEY_COMMAND_REG
, SP_DISABLE_SYNC_HDCP
);
506 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL8_REG
,
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
,
520 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
521 SP_DP_HDCP_CTRL_REG
, SP_LINK_POLLING
);
525 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
526 SP_DP_LINK_DEBUG_CTRL_REG
, SP_M_VID_DEBUG
);
530 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
],
531 SP_ANALOG_DEBUG2_REG
, SP_POWERON_TIME_1P5MS
);
535 err
= anx78xx_xtal_clk_sel(anx78xx
);
539 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_AUX_DEFER_CTRL_REG
,
540 SP_DEFER_CTRL_EN
| 0x0c);
544 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
545 SP_DP_POLLING_CTRL_REG
,
546 SP_AUTO_POLLING_DISABLE
);
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);
559 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
560 SP_DP_MISC_CTRL_REG
, SP_EQ_TRAINING_LOOP
);
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
);
570 err
= anx78xx_link_phy_initialization(anx78xx
);
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
);
583 static int anx78xx_enable_interrupts(struct anx78xx
*anx78xx
)
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);
595 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
],
596 SP_COMMON_INT_MASK4_REG
, SP_HPD_LOST
| SP_HPD_PLUG
);
600 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DP_INT_MASK1_REG
,
605 err
= regmap_write(anx78xx
->map
[I2C_IDX_RX_P0
], SP_INT_MASK1_REG
,
606 SP_CKDT_CHG
| SP_SCDT_CHG
);
613 static void anx78xx_poweron(struct anx78xx
*anx78xx
)
615 struct anx78xx_platform_data
*pdata
= &anx78xx
->pdata
;
618 if (WARN_ON(anx78xx
->powered
))
622 err
= regulator_enable(pdata
->dvdd10
);
624 DRM_ERROR("Failed to enable DVDD10 regulator: %d\n",
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
;
654 if (WARN_ON(!anx78xx
->powered
))
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);
664 err
= regulator_disable(pdata
->dvdd10
);
666 DRM_ERROR("Failed to disable DVDD10 regulator: %d\n",
671 usleep_range(1000, 2000);
674 anx78xx
->powered
= false;
677 static int anx78xx_start(struct anx78xx
*anx78xx
)
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
|
687 err
= anx78xx_enable_interrupts(anx78xx
);
689 DRM_ERROR("Failed to enable interrupts: %d\n", err
);
693 err
= anx78xx_rx_initialization(anx78xx
);
695 DRM_ERROR("Failed receiver initialization: %d\n", err
);
699 err
= anx78xx_tx_initialization(anx78xx
);
701 DRM_ERROR("Failed transmitter initialization: %d\n", err
);
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);
714 DRM_ERROR("Failed SlimPort transmitter initialization: %d\n", err
);
715 anx78xx_poweroff(anx78xx
);
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
);
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
)
753 err
= regmap_write(anx78xx
->map
[I2C_IDX_RX_P0
], SP_HDMI_MUTE_CTRL_REG
,
758 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
],
759 SP_POWERDOWN_CTRL_REG
,
764 err
= drm_dp_dpcd_readb(&anx78xx
->aux
, DP_MAX_LINK_RATE
, &dp_bw
);
769 case DP_LINK_BW_1_62
:
775 DRM_DEBUG_KMS("DP bandwidth (%#02x) not supported\n", dp_bw
);
779 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL1_REG
,
784 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
],
785 SP_VID_CTRL1_REG
, SP_VIDEO_EN
);
790 err
= drm_dp_dpcd_read(&anx78xx
->aux
, DP_DPCD_REV
,
791 &anx78xx
->dpcd
, DP_RECEIVER_CAP_SIZE
);
793 DRM_ERROR("Failed to read DPCD: %d\n", 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
);
803 /* Check link capabilities */
804 err
= drm_dp_link_probe(&anx78xx
->aux
, &anx78xx
->link
);
806 DRM_ERROR("Failed to probe link capabilities: %d\n", err
);
810 /* Power up the sink */
811 err
= drm_dp_link_power_up(&anx78xx
->aux
, &anx78xx
->link
);
813 DRM_ERROR("Failed to power up DisplayPort link: %d\n", 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);
823 if (anx78xx
->dpcd
[DP_MAX_DOWNSPREAD
] & DP_MAX_DOWNSPREAD_0_5
) {
824 DRM_DEBUG("Enable downspread on the sink\n");
826 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
],
827 SP_DP_DOWNSPREAD_CTRL1_REG
, 8);
831 err
= drm_dp_dpcd_writeb(&anx78xx
->aux
, DP_DOWNSPREAD_CTRL
,
836 err
= drm_dp_dpcd_writeb(&anx78xx
->aux
, DP_DOWNSPREAD_CTRL
, 0);
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,
847 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
848 SP_DP_SYSTEM_CTRL_BASE
+ 4,
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
);
859 err
= drm_dp_link_configure(&anx78xx
->aux
, &anx78xx
->link
);
861 DRM_ERROR("Failed to configure DisplayPort link: %d\n", err
);
865 /* Start training on the source */
866 err
= regmap_write(anx78xx
->map
[I2C_IDX_TX_P0
], SP_DP_LT_CTRL_REG
,
874 static int anx78xx_config_dp_output(struct anx78xx
*anx78xx
)
878 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL1_REG
,
883 /* Enable DP output */
884 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P2
], SP_VID_CTRL1_REG
,
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
];
898 err
= hdmi_avi_infoframe_pack(frame
, buffer
, sizeof(buffer
));
900 DRM_ERROR("Failed to pack AVI infoframe: %d\n", err
);
904 err
= anx78xx_clear_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
905 SP_PACKET_SEND_CTRL_REG
, SP_AVI_IF_EN
);
909 err
= regmap_bulk_write(anx78xx
->map
[I2C_IDX_TX_P2
],
910 SP_INFOFRAME_AVI_DB1_REG
, buffer
,
915 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
916 SP_PACKET_SEND_CTRL_REG
, SP_AVI_IF_UD
);
920 err
= anx78xx_set_bits(anx78xx
->map
[I2C_IDX_TX_P0
],
921 SP_PACKET_SEND_CTRL_REG
, SP_AVI_IF_EN
);
928 static int anx78xx_get_downstream_info(struct anx78xx
*anx78xx
)
933 err
= drm_dp_dpcd_readb(&anx78xx
->aux
, DP_SINK_COUNT
, &value
);
935 DRM_ERROR("Get sink count failed %d\n", err
);
939 if (!DP_GET_SINK_COUNT(value
)) {
940 DRM_ERROR("Downstream disconnected\n");
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
))
956 return drm_add_edid_modes(connector
, anx78xx
->edid
);
958 mutex_lock(&anx78xx
->lock
);
960 err
= anx78xx_get_downstream_info(anx78xx
);
962 DRM_ERROR("Failed to get downstream info: %d\n", err
);
966 anx78xx
->edid
= drm_get_edid(connector
, &anx78xx
->aux
.ddc
);
967 if (!anx78xx
->edid
) {
968 DRM_ERROR("Failed to read EDID\n");
972 err
= drm_mode_connector_update_edid_property(connector
,
975 DRM_ERROR("Failed to update EDID property: %d\n", err
);
979 num_modes
= drm_add_edid_modes(connector
, anx78xx
->edid
);
981 drm_edid_to_eld(connector
, anx78xx
->edid
);
984 mutex_unlock(&anx78xx
->lock
);
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
,
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
);
1019 if (!bridge
->encoder
) {
1020 DRM_ERROR("Parent encoder object not found");
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
);
1031 DRM_ERROR("Failed to register aux channel: %d\n", err
);
1035 err
= drm_connector_init(bridge
->dev
, &anx78xx
->connector
,
1036 &anx78xx_connector_funcs
,
1037 DRM_MODE_CONNECTOR_DisplayPort
);
1039 DRM_ERROR("Failed to initialize connector: %d\n", err
);
1043 drm_connector_helper_add(&anx78xx
->connector
,
1044 &anx78xx_connector_helper_funcs
);
1046 err
= drm_connector_register(&anx78xx
->connector
);
1048 DRM_ERROR("Failed to register connector: %d\n", err
);
1052 anx78xx
->connector
.polled
= DRM_CONNECTOR_POLL_HPD
;
1054 err
= drm_mode_connector_attach_encoder(&anx78xx
->connector
,
1057 DRM_ERROR("Failed to link up connector to encoder: %d\n", err
);
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
)
1071 /* Max 1200p at 5.4 Ghz, one lane */
1072 if (mode
->clock
> 154000)
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
;
1095 if (WARN_ON(!anx78xx
->powered
))
1098 mutex_lock(&anx78xx
->lock
);
1100 err
= drm_hdmi_avi_infoframe_from_display_mode(&frame
, adjusted_mode
);
1102 DRM_ERROR("Failed to setup AVI infoframe: %d\n", err
);
1106 err
= anx78xx_send_video_infoframe(anx78xx
, &frame
);
1108 DRM_ERROR("Failed to send AVI infoframe: %d\n", err
);
1111 mutex_unlock(&anx78xx
->lock
);
1114 static void anx78xx_bridge_enable(struct drm_bridge
*bridge
)
1116 struct anx78xx
*anx78xx
= bridge_to_anx78xx(bridge
);
1119 err
= anx78xx_start(anx78xx
);
1121 DRM_ERROR("Failed to initialize: %d\n", err
);
1125 err
= anx78xx_set_hpd(anx78xx
);
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
;
1143 if (anx78xx
->powered
)
1146 mutex_lock(&anx78xx
->lock
);
1148 /* Cable is pulled, power on the chip */
1149 anx78xx_poweron(anx78xx
);
1151 err
= anx78xx_enable_interrupts(anx78xx
);
1153 DRM_ERROR("Failed to enable interrupts: %d\n", err
);
1155 mutex_unlock(&anx78xx
->lock
);
1160 static int anx78xx_handle_dp_int_1(struct anx78xx
*anx78xx
, u8 irq
)
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
,
1171 if (irq
& SP_TRAINING_FINISH
) {
1172 DRM_DEBUG_KMS("IRQ: hardware link training finished\n");
1173 err
= anx78xx_config_dp_output(anx78xx
);
1179 static bool anx78xx_handle_common_int_4(struct anx78xx
*anx78xx
, u8 irq
)
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
);
1189 DRM_ERROR("Failed to write SP_COMMON_INT_STATUS4 %d\n", err
);
1193 if (irq
& SP_HPD_LOST
) {
1194 DRM_DEBUG_KMS("IRQ: Hot plug detect - cable is pulled out\n");
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");
1208 static void anx78xx_handle_hdmi_int_1(struct anx78xx
*anx78xx
, u8 irq
)
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
,
1218 DRM_ERROR("Write HDMI int 1 failed: %d\n", err
);
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
);
1228 DRM_ERROR("Read system status reg failed: %d\n", err
);
1232 if (!(value
& SP_TMDS_CLOCK_DET
)) {
1233 DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI clock ***\n");
1237 if (!(value
& SP_TMDS_DE_DET
)) {
1238 DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI signal ***\n");
1242 err
= anx78xx_dp_link_training(anx78xx
);
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
;
1255 mutex_lock(&anx78xx
->lock
);
1257 err
= regmap_read(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DP_INT_STATUS1_REG
,
1260 DRM_ERROR("Failed to read DP interrupt 1 status: %d\n", err
);
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
);
1270 DRM_ERROR("Failed to read common interrupt 4 status: %d\n",
1276 event
= anx78xx_handle_common_int_4(anx78xx
, irq
);
1278 /* Make sure we are still powered after handle HPD events */
1279 if (!anx78xx
->powered
)
1282 err
= regmap_read(anx78xx
->map
[I2C_IDX_RX_P0
], SP_INT_STATUS1_REG
,
1285 DRM_ERROR("Failed to read HDMI int 1 status: %d\n", err
);
1290 anx78xx_handle_hdmi_int_1(anx78xx
, irq
);
1293 mutex_unlock(&anx78xx
->lock
);
1296 drm_helper_hpd_irq_event(anx78xx
->connector
.dev
);
1301 static void unregister_i2c_dummy_clients(struct anx78xx
*anx78xx
)
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
= {
1315 static const u16 anx78xx_chipid_list
[] = {
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
;
1330 anx78xx
= devm_kzalloc(&client
->dev
, sizeof(*anx78xx
), GFP_KERNEL
);
1334 pdata
= &anx78xx
->pdata
;
1336 mutex_init(&anx78xx
->lock
);
1338 #if IS_ENABLED(CONFIG_OF)
1339 anx78xx
->bridge
.of_node
= client
->dev
.of_node
;
1342 anx78xx
->client
= client
;
1343 i2c_set_clientdata(client
, anx78xx
);
1345 err
= anx78xx_init_pdata(anx78xx
);
1347 DRM_ERROR("Failed to initialize pdata: %d\n", 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
);
1357 pdata
->intp_irq
= client
->irq
;
1358 if (!pdata
->intp_irq
) {
1359 DRM_ERROR("Failed to get CABLE_DET and INTP IRQ\n");
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
]) {
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
,
1392 err
= regmap_read(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DEVICE_IDH_REG
,
1397 anx78xx
->chipid
= (u8
)idl
| ((u8
)idh
<< 8);
1399 err
= regmap_read(anx78xx
->map
[I2C_IDX_TX_P2
], SP_DEVICE_VERSION_REG
,
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
);
1414 DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",
1415 anx78xx
->chipid
, version
);
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
);
1425 DRM_ERROR("Failed to request CABLE_DET threaded IRQ: %d\n",
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
);
1435 DRM_ERROR("Failed to request INTP threaded IRQ: %d\n", err
);
1439 anx78xx
->bridge
.funcs
= &anx78xx_bridge_funcs
;
1441 err
= drm_bridge_add(&anx78xx
->bridge
);
1443 DRM_ERROR("Failed to add drm bridge: %d\n", err
);
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
);
1454 anx78xx_poweroff(anx78xx
);
1457 unregister_i2c_dummy_clients(anx78xx
);
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
);
1474 static const struct i2c_device_id anx78xx_id
[] = {
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", },
1485 MODULE_DEVICE_TABLE(of
, anx78xx_match_table
);
1488 static struct i2c_driver anx78xx_driver
= {
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");