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);
982 mutex_unlock(&anx78xx->lock);
987 static const struct drm_connector_helper_funcs anx78xx_connector_helper_funcs = {
988 .get_modes = anx78xx_get_modes,
991 static enum drm_connector_status anx78xx_detect(struct drm_connector *connector,
994 struct anx78xx *anx78xx = connector_to_anx78xx(connector);
996 if (!gpiod_get_value(anx78xx->pdata.gpiod_hpd))
997 return connector_status_disconnected;
999 return connector_status_connected;
1002 static const struct drm_connector_funcs anx78xx_connector_funcs = {
1003 .fill_modes = drm_helper_probe_single_connector_modes,
1004 .detect = anx78xx_detect,
1005 .destroy = drm_connector_cleanup,
1006 .reset = drm_atomic_helper_connector_reset,
1007 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1008 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1011 static int anx78xx_bridge_attach(struct drm_bridge *bridge)
1013 struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
1016 if (!bridge->encoder) {
1017 DRM_ERROR("Parent encoder object not found");
1021 /* Register aux channel */
1022 anx78xx->aux.name = "DP-AUX";
1023 anx78xx->aux.dev = &anx78xx->client->dev;
1024 anx78xx->aux.transfer = anx78xx_aux_transfer;
1026 err = drm_dp_aux_register(&anx78xx->aux);
1028 DRM_ERROR("Failed to register aux channel: %d\n", err);
1032 err = drm_connector_init(bridge->dev, &anx78xx->connector,
1033 &anx78xx_connector_funcs,
1034 DRM_MODE_CONNECTOR_DisplayPort);
1036 DRM_ERROR("Failed to initialize connector: %d\n", err);
1040 drm_connector_helper_add(&anx78xx->connector,
1041 &anx78xx_connector_helper_funcs);
1043 err = drm_connector_register(&anx78xx->connector);
1045 DRM_ERROR("Failed to register connector: %d\n", err);
1049 anx78xx->connector.polled = DRM_CONNECTOR_POLL_HPD;
1051 err = drm_mode_connector_attach_encoder(&anx78xx->connector,
1054 DRM_ERROR("Failed to link up connector to encoder: %d\n", err);
1061 static enum drm_mode_status
1062 anx78xx_bridge_mode_valid(struct drm_bridge *bridge,
1063 const struct drm_display_mode *mode)
1065 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1066 return MODE_NO_INTERLACE;
1068 /* Max 1200p at 5.4 Ghz, one lane */
1069 if (mode->clock > 154000)
1070 return MODE_CLOCK_HIGH;
1075 static void anx78xx_bridge_disable(struct drm_bridge *bridge)
1077 struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
1079 /* Power off all modules except configuration registers access */
1080 anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,
1081 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
1084 static void anx78xx_bridge_mode_set(struct drm_bridge *bridge,
1085 struct drm_display_mode *mode,
1086 struct drm_display_mode *adjusted_mode)
1088 struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
1089 struct hdmi_avi_infoframe frame;
1092 if (WARN_ON(!anx78xx->powered))
1095 mutex_lock(&anx78xx->lock);
1097 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, adjusted_mode,
1100 DRM_ERROR("Failed to setup AVI infoframe: %d\n", err);
1104 err = anx78xx_send_video_infoframe(anx78xx, &frame);
1106 DRM_ERROR("Failed to send AVI infoframe: %d\n", err);
1109 mutex_unlock(&anx78xx->lock);
1112 static void anx78xx_bridge_enable(struct drm_bridge *bridge)
1114 struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
1117 err = anx78xx_start(anx78xx);
1119 DRM_ERROR("Failed to initialize: %d\n", err);
1123 err = anx78xx_set_hpd(anx78xx);
1125 DRM_ERROR("Failed to set HPD: %d\n", err);
1128 static const struct drm_bridge_funcs anx78xx_bridge_funcs = {
1129 .attach = anx78xx_bridge_attach,
1130 .mode_valid = anx78xx_bridge_mode_valid,
1131 .disable = anx78xx_bridge_disable,
1132 .mode_set = anx78xx_bridge_mode_set,
1133 .enable = anx78xx_bridge_enable,
1136 static irqreturn_t anx78xx_hpd_threaded_handler(int irq, void *data)
1138 struct anx78xx *anx78xx = data;
1141 if (anx78xx->powered)
1144 mutex_lock(&anx78xx->lock);
1146 /* Cable is pulled, power on the chip */
1147 anx78xx_poweron(anx78xx);
1149 err = anx78xx_enable_interrupts(anx78xx);
1151 DRM_ERROR("Failed to enable interrupts: %d\n", err);
1153 mutex_unlock(&anx78xx->lock);
1158 static int anx78xx_handle_dp_int_1(struct anx78xx *anx78xx, u8 irq)
1162 DRM_DEBUG_KMS("Handle DP interrupt 1: %02x\n", irq);
1164 err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_STATUS1_REG,
1169 if (irq & SP_TRAINING_FINISH) {
1170 DRM_DEBUG_KMS("IRQ: hardware link training finished\n");
1171 err = anx78xx_config_dp_output(anx78xx);
1177 static bool anx78xx_handle_common_int_4(struct anx78xx *anx78xx, u8 irq)
1182 DRM_DEBUG_KMS("Handle common interrupt 4: %02x\n", irq);
1184 err = regmap_write(anx78xx->map[I2C_IDX_TX_P2],
1185 SP_COMMON_INT_STATUS4_REG, irq);
1187 DRM_ERROR("Failed to write SP_COMMON_INT_STATUS4 %d\n", err);
1191 if (irq & SP_HPD_LOST) {
1192 DRM_DEBUG_KMS("IRQ: Hot plug detect - cable is pulled out\n");
1194 anx78xx_poweroff(anx78xx);
1195 /* Free cached EDID */
1196 kfree(anx78xx->edid);
1197 anx78xx->edid = NULL;
1198 } else if (irq & SP_HPD_PLUG) {
1199 DRM_DEBUG_KMS("IRQ: Hot plug detect - cable plug\n");
1206 static void anx78xx_handle_hdmi_int_1(struct anx78xx *anx78xx, u8 irq)
1211 DRM_DEBUG_KMS("Handle HDMI interrupt 1: %02x\n", irq);
1213 err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_INT_STATUS1_REG,
1216 DRM_ERROR("Write HDMI int 1 failed: %d\n", err);
1220 if ((irq & SP_CKDT_CHG) || (irq & SP_SCDT_CHG)) {
1221 DRM_DEBUG_KMS("IRQ: HDMI input detected\n");
1223 err = regmap_read(anx78xx->map[I2C_IDX_RX_P0],
1224 SP_SYSTEM_STATUS_REG, &value);
1226 DRM_ERROR("Read system status reg failed: %d\n", err);
1230 if (!(value & SP_TMDS_CLOCK_DET)) {
1231 DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI clock ***\n");
1235 if (!(value & SP_TMDS_DE_DET)) {
1236 DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI signal ***\n");
1240 err = anx78xx_dp_link_training(anx78xx);
1242 DRM_ERROR("Failed to start link training: %d\n", err);
1246 static irqreturn_t anx78xx_intp_threaded_handler(int unused, void *data)
1248 struct anx78xx *anx78xx = data;
1253 mutex_lock(&anx78xx->lock);
1255 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_STATUS1_REG,
1258 DRM_ERROR("Failed to read DP interrupt 1 status: %d\n", err);
1263 anx78xx_handle_dp_int_1(anx78xx, irq);
1265 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2],
1266 SP_COMMON_INT_STATUS4_REG, &irq);
1268 DRM_ERROR("Failed to read common interrupt 4 status: %d\n",
1274 event = anx78xx_handle_common_int_4(anx78xx, irq);
1276 /* Make sure we are still powered after handle HPD events */
1277 if (!anx78xx->powered)
1280 err = regmap_read(anx78xx->map[I2C_IDX_RX_P0], SP_INT_STATUS1_REG,
1283 DRM_ERROR("Failed to read HDMI int 1 status: %d\n", err);
1288 anx78xx_handle_hdmi_int_1(anx78xx, irq);
1291 mutex_unlock(&anx78xx->lock);
1294 drm_helper_hpd_irq_event(anx78xx->connector.dev);
1299 static void unregister_i2c_dummy_clients(struct anx78xx *anx78xx)
1303 for (i = 0; i < ARRAY_SIZE(anx78xx->i2c_dummy); i++)
1304 i2c_unregister_device(anx78xx->i2c_dummy[i]);
1307 static const struct regmap_config anx78xx_regmap_config = {
1312 static const u16 anx78xx_chipid_list[] = {
1318 static int anx78xx_i2c_probe(struct i2c_client *client,
1319 const struct i2c_device_id *id)
1321 struct anx78xx *anx78xx;
1322 struct anx78xx_platform_data *pdata;
1323 unsigned int i, idl, idh, version;
1327 anx78xx = devm_kzalloc(&client->dev, sizeof(*anx78xx), GFP_KERNEL);
1331 pdata = &anx78xx->pdata;
1333 mutex_init(&anx78xx->lock);
1335 #if IS_ENABLED(CONFIG_OF)
1336 anx78xx->bridge.of_node = client->dev.of_node;
1339 anx78xx->client = client;
1340 i2c_set_clientdata(client, anx78xx);
1342 err = anx78xx_init_pdata(anx78xx);
1344 DRM_ERROR("Failed to initialize pdata: %d\n", err);
1348 pdata->hpd_irq = gpiod_to_irq(pdata->gpiod_hpd);
1349 if (pdata->hpd_irq < 0) {
1350 DRM_ERROR("Failed to get HPD IRQ: %d\n", pdata->hpd_irq);
1354 pdata->intp_irq = client->irq;
1355 if (!pdata->intp_irq) {
1356 DRM_ERROR("Failed to get CABLE_DET and INTP IRQ\n");
1360 /* Map slave addresses of ANX7814 */
1361 for (i = 0; i < I2C_NUM_ADDRESSES; i++) {
1362 anx78xx->i2c_dummy[i] = i2c_new_dummy(client->adapter,
1363 anx78xx_i2c_addresses[i] >> 1);
1364 if (!anx78xx->i2c_dummy[i]) {
1366 DRM_ERROR("Failed to reserve I2C bus %02x\n",
1367 anx78xx_i2c_addresses[i]);
1368 goto err_unregister_i2c;
1371 anx78xx->map[i] = devm_regmap_init_i2c(anx78xx->i2c_dummy[i],
1372 &anx78xx_regmap_config);
1373 if (IS_ERR(anx78xx->map[i])) {
1374 err = PTR_ERR(anx78xx->map[i]);
1375 DRM_ERROR("Failed regmap initialization %02x\n",
1376 anx78xx_i2c_addresses[i]);
1377 goto err_unregister_i2c;
1381 /* Look for supported chip ID */
1382 anx78xx_poweron(anx78xx);
1384 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_IDL_REG,
1389 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_IDH_REG,
1394 anx78xx->chipid = (u8)idl | ((u8)idh << 8);
1396 err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_VERSION_REG,
1401 for (i = 0; i < ARRAY_SIZE(anx78xx_chipid_list); i++) {
1402 if (anx78xx->chipid == anx78xx_chipid_list[i]) {
1403 DRM_INFO("Found ANX%x (ver. %d) SlimPort Transmitter\n",
1404 anx78xx->chipid, version);
1411 DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",
1412 anx78xx->chipid, version);
1417 err = devm_request_threaded_irq(&client->dev, pdata->hpd_irq, NULL,
1418 anx78xx_hpd_threaded_handler,
1419 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1420 "anx78xx-hpd", anx78xx);
1422 DRM_ERROR("Failed to request CABLE_DET threaded IRQ: %d\n",
1427 err = devm_request_threaded_irq(&client->dev, pdata->intp_irq, NULL,
1428 anx78xx_intp_threaded_handler,
1429 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1430 "anx78xx-intp", anx78xx);
1432 DRM_ERROR("Failed to request INTP threaded IRQ: %d\n", err);
1436 anx78xx->bridge.funcs = &anx78xx_bridge_funcs;
1438 drm_bridge_add(&anx78xx->bridge);
1440 /* If cable is pulled out, just poweroff and wait for HPD event */
1441 if (!gpiod_get_value(anx78xx->pdata.gpiod_hpd))
1442 anx78xx_poweroff(anx78xx);
1447 anx78xx_poweroff(anx78xx);
1450 unregister_i2c_dummy_clients(anx78xx);
1454 static int anx78xx_i2c_remove(struct i2c_client *client)
1456 struct anx78xx *anx78xx = i2c_get_clientdata(client);
1458 drm_bridge_remove(&anx78xx->bridge);
1460 unregister_i2c_dummy_clients(anx78xx);
1462 kfree(anx78xx->edid);
1467 static const struct i2c_device_id anx78xx_id[] = {
1471 MODULE_DEVICE_TABLE(i2c, anx78xx_id);
1473 #if IS_ENABLED(CONFIG_OF)
1474 static const struct of_device_id anx78xx_match_table[] = {
1475 { .compatible = "analogix,anx7814", },
1478 MODULE_DEVICE_TABLE(of, anx78xx_match_table);
1481 static struct i2c_driver anx78xx_driver = {
1484 .of_match_table = of_match_ptr(anx78xx_match_table),
1486 .probe = anx78xx_i2c_probe,
1487 .remove = anx78xx_i2c_remove,
1488 .id_table = anx78xx_id,
1490 module_i2c_driver(anx78xx_driver);
1492 MODULE_DESCRIPTION("ANX78xx SlimPort Transmitter driver");
1494 MODULE_LICENSE("GPL v2");