15d97408eSIcenowy Zheng // SPDX-License-Identifier: GPL-2.0-only
25d97408eSIcenowy Zheng /*
35d97408eSIcenowy Zheng  * Copyright(c) 2016, Analogix Semiconductor.
45d97408eSIcenowy Zheng  *
55d97408eSIcenowy Zheng  * Based on anx7808 driver obtained from chromeos with copyright:
65d97408eSIcenowy Zheng  * Copyright(c) 2013, Google Inc.
75d97408eSIcenowy Zheng  */
85d97408eSIcenowy Zheng #include <linux/delay.h>
95d97408eSIcenowy Zheng #include <linux/err.h>
105d97408eSIcenowy Zheng #include <linux/gpio/consumer.h>
115d97408eSIcenowy Zheng #include <linux/i2c.h>
125d97408eSIcenowy Zheng #include <linux/interrupt.h>
135d97408eSIcenowy Zheng #include <linux/kernel.h>
145d97408eSIcenowy Zheng #include <linux/module.h>
155d97408eSIcenowy Zheng #include <linux/of_irq.h>
165d97408eSIcenowy Zheng #include <linux/of_platform.h>
175d97408eSIcenowy Zheng #include <linux/regmap.h>
185d97408eSIcenowy Zheng #include <linux/regulator/consumer.h>
195d97408eSIcenowy Zheng #include <linux/types.h>
205d97408eSIcenowy Zheng 
215d97408eSIcenowy Zheng #include <drm/drm_atomic_helper.h>
225d97408eSIcenowy Zheng #include <drm/drm_bridge.h>
235d97408eSIcenowy Zheng #include <drm/drm_crtc.h>
245d97408eSIcenowy Zheng #include <drm/drm_dp_helper.h>
255d97408eSIcenowy Zheng #include <drm/drm_edid.h>
265d97408eSIcenowy Zheng #include <drm/drm_print.h>
275d97408eSIcenowy Zheng #include <drm/drm_probe_helper.h>
285d97408eSIcenowy Zheng 
295d97408eSIcenowy Zheng #include "analogix-anx78xx.h"
305d97408eSIcenowy Zheng 
315d97408eSIcenowy Zheng #define I2C_NUM_ADDRESSES	5
325d97408eSIcenowy Zheng #define I2C_IDX_TX_P0		0
335d97408eSIcenowy Zheng #define I2C_IDX_TX_P1		1
345d97408eSIcenowy Zheng #define I2C_IDX_TX_P2		2
355d97408eSIcenowy Zheng #define I2C_IDX_RX_P0		3
365d97408eSIcenowy Zheng #define I2C_IDX_RX_P1		4
375d97408eSIcenowy Zheng 
385d97408eSIcenowy Zheng #define XTAL_CLK		270 /* 27M */
395d97408eSIcenowy Zheng #define AUX_CH_BUFFER_SIZE	16
405d97408eSIcenowy Zheng #define AUX_WAIT_TIMEOUT_MS	15
415d97408eSIcenowy Zheng 
425d97408eSIcenowy Zheng static const u8 anx7808_i2c_addresses[] = {
435d97408eSIcenowy Zheng 	[I2C_IDX_TX_P0] = 0x78,
445d97408eSIcenowy Zheng 	[I2C_IDX_TX_P1] = 0x7a,
455d97408eSIcenowy Zheng 	[I2C_IDX_TX_P2] = 0x72,
465d97408eSIcenowy Zheng 	[I2C_IDX_RX_P0] = 0x7e,
475d97408eSIcenowy Zheng 	[I2C_IDX_RX_P1] = 0x80,
485d97408eSIcenowy Zheng };
495d97408eSIcenowy Zheng 
505d97408eSIcenowy Zheng static const u8 anx781x_i2c_addresses[] = {
515d97408eSIcenowy Zheng 	[I2C_IDX_TX_P0] = 0x70,
525d97408eSIcenowy Zheng 	[I2C_IDX_TX_P1] = 0x7a,
535d97408eSIcenowy Zheng 	[I2C_IDX_TX_P2] = 0x72,
545d97408eSIcenowy Zheng 	[I2C_IDX_RX_P0] = 0x7e,
555d97408eSIcenowy Zheng 	[I2C_IDX_RX_P1] = 0x80,
565d97408eSIcenowy Zheng };
575d97408eSIcenowy Zheng 
585d97408eSIcenowy Zheng struct anx78xx_platform_data {
595d97408eSIcenowy Zheng 	struct regulator *dvdd10;
605d97408eSIcenowy Zheng 	struct gpio_desc *gpiod_hpd;
615d97408eSIcenowy Zheng 	struct gpio_desc *gpiod_pd;
625d97408eSIcenowy Zheng 	struct gpio_desc *gpiod_reset;
635d97408eSIcenowy Zheng 
645d97408eSIcenowy Zheng 	int hpd_irq;
655d97408eSIcenowy Zheng 	int intp_irq;
665d97408eSIcenowy Zheng };
675d97408eSIcenowy Zheng 
685d97408eSIcenowy Zheng struct anx78xx {
695d97408eSIcenowy Zheng 	struct drm_dp_aux aux;
705d97408eSIcenowy Zheng 	struct drm_bridge bridge;
715d97408eSIcenowy Zheng 	struct i2c_client *client;
725d97408eSIcenowy Zheng 	struct edid *edid;
735d97408eSIcenowy Zheng 	struct drm_connector connector;
745d97408eSIcenowy Zheng 	struct anx78xx_platform_data pdata;
755d97408eSIcenowy Zheng 	struct mutex lock;
765d97408eSIcenowy Zheng 
775d97408eSIcenowy Zheng 	/*
785d97408eSIcenowy Zheng 	 * I2C Slave addresses of ANX7814 are mapped as TX_P0, TX_P1, TX_P2,
795d97408eSIcenowy Zheng 	 * RX_P0 and RX_P1.
805d97408eSIcenowy Zheng 	 */
815d97408eSIcenowy Zheng 	struct i2c_client *i2c_dummy[I2C_NUM_ADDRESSES];
825d97408eSIcenowy Zheng 	struct regmap *map[I2C_NUM_ADDRESSES];
835d97408eSIcenowy Zheng 
845d97408eSIcenowy Zheng 	u16 chipid;
855d97408eSIcenowy Zheng 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
865d97408eSIcenowy Zheng 
875d97408eSIcenowy Zheng 	bool powered;
885d97408eSIcenowy Zheng };
895d97408eSIcenowy Zheng 
905d97408eSIcenowy Zheng static inline struct anx78xx *connector_to_anx78xx(struct drm_connector *c)
915d97408eSIcenowy Zheng {
925d97408eSIcenowy Zheng 	return container_of(c, struct anx78xx, connector);
935d97408eSIcenowy Zheng }
945d97408eSIcenowy Zheng 
955d97408eSIcenowy Zheng static inline struct anx78xx *bridge_to_anx78xx(struct drm_bridge *bridge)
965d97408eSIcenowy Zheng {
975d97408eSIcenowy Zheng 	return container_of(bridge, struct anx78xx, bridge);
985d97408eSIcenowy Zheng }
995d97408eSIcenowy Zheng 
1005d97408eSIcenowy Zheng static int anx78xx_set_bits(struct regmap *map, u8 reg, u8 mask)
1015d97408eSIcenowy Zheng {
1025d97408eSIcenowy Zheng 	return regmap_update_bits(map, reg, mask, mask);
1035d97408eSIcenowy Zheng }
1045d97408eSIcenowy Zheng 
1055d97408eSIcenowy Zheng static int anx78xx_clear_bits(struct regmap *map, u8 reg, u8 mask)
1065d97408eSIcenowy Zheng {
1075d97408eSIcenowy Zheng 	return regmap_update_bits(map, reg, mask, 0);
1085d97408eSIcenowy Zheng }
1095d97408eSIcenowy Zheng 
1105d97408eSIcenowy Zheng static bool anx78xx_aux_op_finished(struct anx78xx *anx78xx)
1115d97408eSIcenowy Zheng {
1125d97408eSIcenowy Zheng 	unsigned int value;
1135d97408eSIcenowy Zheng 	int err;
1145d97408eSIcenowy Zheng 
1155d97408eSIcenowy Zheng 	err = regmap_read(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL2_REG,
1165d97408eSIcenowy Zheng 			  &value);
1175d97408eSIcenowy Zheng 	if (err < 0)
1185d97408eSIcenowy Zheng 		return false;
1195d97408eSIcenowy Zheng 
1205d97408eSIcenowy Zheng 	return (value & SP_AUX_EN) == 0;
1215d97408eSIcenowy Zheng }
1225d97408eSIcenowy Zheng 
1235d97408eSIcenowy Zheng static int anx78xx_aux_wait(struct anx78xx *anx78xx)
1245d97408eSIcenowy Zheng {
1255d97408eSIcenowy Zheng 	unsigned long timeout;
1265d97408eSIcenowy Zheng 	unsigned int status;
1275d97408eSIcenowy Zheng 	int err;
1285d97408eSIcenowy Zheng 
1295d97408eSIcenowy Zheng 	timeout = jiffies + msecs_to_jiffies(AUX_WAIT_TIMEOUT_MS) + 1;
1305d97408eSIcenowy Zheng 
1315d97408eSIcenowy Zheng 	while (!anx78xx_aux_op_finished(anx78xx)) {
1325d97408eSIcenowy Zheng 		if (time_after(jiffies, timeout)) {
1335d97408eSIcenowy Zheng 			if (!anx78xx_aux_op_finished(anx78xx)) {
1345d97408eSIcenowy Zheng 				DRM_ERROR("Timed out waiting AUX to finish\n");
1355d97408eSIcenowy Zheng 				return -ETIMEDOUT;
1365d97408eSIcenowy Zheng 			}
1375d97408eSIcenowy Zheng 
1385d97408eSIcenowy Zheng 			break;
1395d97408eSIcenowy Zheng 		}
1405d97408eSIcenowy Zheng 
1415d97408eSIcenowy Zheng 		usleep_range(1000, 2000);
1425d97408eSIcenowy Zheng 	}
1435d97408eSIcenowy Zheng 
1445d97408eSIcenowy Zheng 	/* Read the AUX channel access status */
1455d97408eSIcenowy Zheng 	err = regmap_read(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_CH_STATUS_REG,
1465d97408eSIcenowy Zheng 			  &status);
1475d97408eSIcenowy Zheng 	if (err < 0) {
1485d97408eSIcenowy Zheng 		DRM_ERROR("Failed to read from AUX channel: %d\n", err);
1495d97408eSIcenowy Zheng 		return err;
1505d97408eSIcenowy Zheng 	}
1515d97408eSIcenowy Zheng 
1525d97408eSIcenowy Zheng 	if (status & SP_AUX_STATUS) {
1535d97408eSIcenowy Zheng 		DRM_ERROR("Failed to wait for AUX channel (status: %02x)\n",
1545d97408eSIcenowy Zheng 			  status);
1555d97408eSIcenowy Zheng 		return -ETIMEDOUT;
1565d97408eSIcenowy Zheng 	}
1575d97408eSIcenowy Zheng 
1585d97408eSIcenowy Zheng 	return 0;
1595d97408eSIcenowy Zheng }
1605d97408eSIcenowy Zheng 
1615d97408eSIcenowy Zheng static int anx78xx_aux_address(struct anx78xx *anx78xx, unsigned int addr)
1625d97408eSIcenowy Zheng {
1635d97408eSIcenowy Zheng 	int err;
1645d97408eSIcenowy Zheng 
1655d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_ADDR_7_0_REG,
1665d97408eSIcenowy Zheng 			   addr & 0xff);
1675d97408eSIcenowy Zheng 	if (err)
1685d97408eSIcenowy Zheng 		return err;
1695d97408eSIcenowy Zheng 
1705d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_ADDR_15_8_REG,
1715d97408eSIcenowy Zheng 			   (addr & 0xff00) >> 8);
1725d97408eSIcenowy Zheng 	if (err)
1735d97408eSIcenowy Zheng 		return err;
1745d97408eSIcenowy Zheng 
1755d97408eSIcenowy Zheng 	/*
1765d97408eSIcenowy Zheng 	 * DP AUX CH Address Register #2, only update bits[3:0]
1775d97408eSIcenowy Zheng 	 * [7:4] RESERVED
1785d97408eSIcenowy Zheng 	 * [3:0] AUX_ADDR[19:16], Register control AUX CH address.
1795d97408eSIcenowy Zheng 	 */
1805d97408eSIcenowy Zheng 	err = regmap_update_bits(anx78xx->map[I2C_IDX_TX_P0],
1815d97408eSIcenowy Zheng 				 SP_AUX_ADDR_19_16_REG,
1825d97408eSIcenowy Zheng 				 SP_AUX_ADDR_19_16_MASK,
1835d97408eSIcenowy Zheng 				 (addr & 0xf0000) >> 16);
1845d97408eSIcenowy Zheng 
1855d97408eSIcenowy Zheng 	if (err)
1865d97408eSIcenowy Zheng 		return err;
1875d97408eSIcenowy Zheng 
1885d97408eSIcenowy Zheng 	return 0;
1895d97408eSIcenowy Zheng }
1905d97408eSIcenowy Zheng 
1915d97408eSIcenowy Zheng static ssize_t anx78xx_aux_transfer(struct drm_dp_aux *aux,
1925d97408eSIcenowy Zheng 				    struct drm_dp_aux_msg *msg)
1935d97408eSIcenowy Zheng {
1945d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = container_of(aux, struct anx78xx, aux);
1955d97408eSIcenowy Zheng 	u8 ctrl1 = msg->request;
1965d97408eSIcenowy Zheng 	u8 ctrl2 = SP_AUX_EN;
1975d97408eSIcenowy Zheng 	u8 *buffer = msg->buffer;
1985d97408eSIcenowy Zheng 	int err;
1995d97408eSIcenowy Zheng 
2005d97408eSIcenowy Zheng 	/* The DP AUX transmit and receive buffer has 16 bytes. */
2015d97408eSIcenowy Zheng 	if (WARN_ON(msg->size > AUX_CH_BUFFER_SIZE))
2025d97408eSIcenowy Zheng 		return -E2BIG;
2035d97408eSIcenowy Zheng 
2045d97408eSIcenowy Zheng 	/* Zero-sized messages specify address-only transactions. */
2055d97408eSIcenowy Zheng 	if (msg->size < 1)
2065d97408eSIcenowy Zheng 		ctrl2 |= SP_ADDR_ONLY;
2075d97408eSIcenowy Zheng 	else	/* For non-zero-sized set the length field. */
2085d97408eSIcenowy Zheng 		ctrl1 |= (msg->size - 1) << SP_AUX_LENGTH_SHIFT;
2095d97408eSIcenowy Zheng 
2105d97408eSIcenowy Zheng 	if ((msg->request & DP_AUX_I2C_READ) == 0) {
2115d97408eSIcenowy Zheng 		/* When WRITE | MOT write values to data buffer */
2125d97408eSIcenowy Zheng 		err = regmap_bulk_write(anx78xx->map[I2C_IDX_TX_P0],
2135d97408eSIcenowy Zheng 					SP_DP_BUF_DATA0_REG, buffer,
2145d97408eSIcenowy Zheng 					msg->size);
2155d97408eSIcenowy Zheng 		if (err)
2165d97408eSIcenowy Zheng 			return err;
2175d97408eSIcenowy Zheng 	}
2185d97408eSIcenowy Zheng 
2195d97408eSIcenowy Zheng 	/* Write address and request */
2205d97408eSIcenowy Zheng 	err = anx78xx_aux_address(anx78xx, msg->address);
2215d97408eSIcenowy Zheng 	if (err)
2225d97408eSIcenowy Zheng 		return err;
2235d97408eSIcenowy Zheng 
2245d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL1_REG,
2255d97408eSIcenowy Zheng 			   ctrl1);
2265d97408eSIcenowy Zheng 	if (err)
2275d97408eSIcenowy Zheng 		return err;
2285d97408eSIcenowy Zheng 
2295d97408eSIcenowy Zheng 	/* Start transaction */
2305d97408eSIcenowy Zheng 	err = regmap_update_bits(anx78xx->map[I2C_IDX_TX_P0],
2315d97408eSIcenowy Zheng 				 SP_DP_AUX_CH_CTRL2_REG, SP_ADDR_ONLY |
2325d97408eSIcenowy Zheng 				 SP_AUX_EN, ctrl2);
2335d97408eSIcenowy Zheng 	if (err)
2345d97408eSIcenowy Zheng 		return err;
2355d97408eSIcenowy Zheng 
2365d97408eSIcenowy Zheng 	err = anx78xx_aux_wait(anx78xx);
2375d97408eSIcenowy Zheng 	if (err)
2385d97408eSIcenowy Zheng 		return err;
2395d97408eSIcenowy Zheng 
2405d97408eSIcenowy Zheng 	msg->reply = DP_AUX_I2C_REPLY_ACK;
2415d97408eSIcenowy Zheng 
2425d97408eSIcenowy Zheng 	if ((msg->size > 0) && (msg->request & DP_AUX_I2C_READ)) {
2435d97408eSIcenowy Zheng 		/* Read values from data buffer */
2445d97408eSIcenowy Zheng 		err = regmap_bulk_read(anx78xx->map[I2C_IDX_TX_P0],
2455d97408eSIcenowy Zheng 				       SP_DP_BUF_DATA0_REG, buffer,
2465d97408eSIcenowy Zheng 				       msg->size);
2475d97408eSIcenowy Zheng 		if (err)
2485d97408eSIcenowy Zheng 			return err;
2495d97408eSIcenowy Zheng 	}
2505d97408eSIcenowy Zheng 
2515d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
2525d97408eSIcenowy Zheng 				 SP_DP_AUX_CH_CTRL2_REG, SP_ADDR_ONLY);
2535d97408eSIcenowy Zheng 	if (err)
2545d97408eSIcenowy Zheng 		return err;
2555d97408eSIcenowy Zheng 
2565d97408eSIcenowy Zheng 	return msg->size;
2575d97408eSIcenowy Zheng }
2585d97408eSIcenowy Zheng 
2595d97408eSIcenowy Zheng static int anx78xx_set_hpd(struct anx78xx *anx78xx)
2605d97408eSIcenowy Zheng {
2615d97408eSIcenowy Zheng 	int err;
2625d97408eSIcenowy Zheng 
2635d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_RX_P0],
2645d97408eSIcenowy Zheng 				 SP_TMDS_CTRL_BASE + 7, SP_PD_RT);
2655d97408eSIcenowy Zheng 	if (err)
2665d97408eSIcenowy Zheng 		return err;
2675d97408eSIcenowy Zheng 
2685d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL3_REG,
2695d97408eSIcenowy Zheng 			       SP_HPD_OUT);
2705d97408eSIcenowy Zheng 	if (err)
2715d97408eSIcenowy Zheng 		return err;
2725d97408eSIcenowy Zheng 
2735d97408eSIcenowy Zheng 	return 0;
2745d97408eSIcenowy Zheng }
2755d97408eSIcenowy Zheng 
2765d97408eSIcenowy Zheng static int anx78xx_clear_hpd(struct anx78xx *anx78xx)
2775d97408eSIcenowy Zheng {
2785d97408eSIcenowy Zheng 	int err;
2795d97408eSIcenowy Zheng 
2805d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL3_REG,
2815d97408eSIcenowy Zheng 				 SP_HPD_OUT);
2825d97408eSIcenowy Zheng 	if (err)
2835d97408eSIcenowy Zheng 		return err;
2845d97408eSIcenowy Zheng 
2855d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
2865d97408eSIcenowy Zheng 			       SP_TMDS_CTRL_BASE + 7, SP_PD_RT);
2875d97408eSIcenowy Zheng 	if (err)
2885d97408eSIcenowy Zheng 		return err;
2895d97408eSIcenowy Zheng 
2905d97408eSIcenowy Zheng 	return 0;
2915d97408eSIcenowy Zheng }
2925d97408eSIcenowy Zheng 
2935d97408eSIcenowy Zheng static const struct reg_sequence tmds_phy_initialization[] = {
2945d97408eSIcenowy Zheng 	{ SP_TMDS_CTRL_BASE +  1, 0x90 },
2955d97408eSIcenowy Zheng 	{ SP_TMDS_CTRL_BASE +  2, 0xa9 },
2965d97408eSIcenowy Zheng 	{ SP_TMDS_CTRL_BASE +  6, 0x92 },
2975d97408eSIcenowy Zheng 	{ SP_TMDS_CTRL_BASE +  7, 0x80 },
2985d97408eSIcenowy Zheng 	{ SP_TMDS_CTRL_BASE + 20, 0xf2 },
2995d97408eSIcenowy Zheng 	{ SP_TMDS_CTRL_BASE + 22, 0xc4 },
3005d97408eSIcenowy Zheng 	{ SP_TMDS_CTRL_BASE + 23, 0x18 },
3015d97408eSIcenowy Zheng };
3025d97408eSIcenowy Zheng 
3035d97408eSIcenowy Zheng static int anx78xx_rx_initialization(struct anx78xx *anx78xx)
3045d97408eSIcenowy Zheng {
3055d97408eSIcenowy Zheng 	int err;
3065d97408eSIcenowy Zheng 
3075d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_HDMI_MUTE_CTRL_REG,
3085d97408eSIcenowy Zheng 			   SP_AUD_MUTE | SP_VID_MUTE);
3095d97408eSIcenowy Zheng 	if (err)
3105d97408eSIcenowy Zheng 		return err;
3115d97408eSIcenowy Zheng 
3125d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0], SP_CHIP_CTRL_REG,
3135d97408eSIcenowy Zheng 			       SP_MAN_HDMI5V_DET | SP_PLLLOCK_CKDT_EN |
3145d97408eSIcenowy Zheng 			       SP_DIGITAL_CKDT_EN);
3155d97408eSIcenowy Zheng 	if (err)
3165d97408eSIcenowy Zheng 		return err;
3175d97408eSIcenowy Zheng 
3185d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
3195d97408eSIcenowy Zheng 			       SP_SOFTWARE_RESET1_REG, SP_HDCP_MAN_RST |
3205d97408eSIcenowy Zheng 			       SP_SW_MAN_RST | SP_TMDS_RST | SP_VIDEO_RST);
3215d97408eSIcenowy Zheng 	if (err)
3225d97408eSIcenowy Zheng 		return err;
3235d97408eSIcenowy Zheng 
3245d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_RX_P0],
3255d97408eSIcenowy Zheng 				 SP_SOFTWARE_RESET1_REG, SP_HDCP_MAN_RST |
3265d97408eSIcenowy Zheng 				 SP_SW_MAN_RST | SP_TMDS_RST | SP_VIDEO_RST);
3275d97408eSIcenowy Zheng 	if (err)
3285d97408eSIcenowy Zheng 		return err;
3295d97408eSIcenowy Zheng 
3305d97408eSIcenowy Zheng 	/* Sync detect change, GP set mute */
3315d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
3325d97408eSIcenowy Zheng 			       SP_AUD_EXCEPTION_ENABLE_BASE + 1, BIT(5) |
3335d97408eSIcenowy Zheng 			       BIT(6));
3345d97408eSIcenowy Zheng 	if (err)
3355d97408eSIcenowy Zheng 		return err;
3365d97408eSIcenowy Zheng 
3375d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
3385d97408eSIcenowy Zheng 			       SP_AUD_EXCEPTION_ENABLE_BASE + 3,
3395d97408eSIcenowy Zheng 			       SP_AEC_EN21);
3405d97408eSIcenowy Zheng 	if (err)
3415d97408eSIcenowy Zheng 		return err;
3425d97408eSIcenowy Zheng 
3435d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0], SP_AUDVID_CTRL_REG,
3445d97408eSIcenowy Zheng 			       SP_AVC_EN | SP_AAC_OE | SP_AAC_EN);
3455d97408eSIcenowy Zheng 	if (err)
3465d97408eSIcenowy Zheng 		return err;
3475d97408eSIcenowy Zheng 
3485d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_RX_P0],
3495d97408eSIcenowy Zheng 				 SP_SYSTEM_POWER_DOWN1_REG, SP_PWDN_CTRL);
3505d97408eSIcenowy Zheng 	if (err)
3515d97408eSIcenowy Zheng 		return err;
3525d97408eSIcenowy Zheng 
3535d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],
3545d97408eSIcenowy Zheng 			       SP_VID_DATA_RANGE_CTRL_REG, SP_R2Y_INPUT_LIMIT);
3555d97408eSIcenowy Zheng 	if (err)
3565d97408eSIcenowy Zheng 		return err;
3575d97408eSIcenowy Zheng 
3585d97408eSIcenowy Zheng 	/* Enable DDC stretch */
3595d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
3605d97408eSIcenowy Zheng 			   SP_DP_EXTRA_I2C_DEV_ADDR_REG, SP_I2C_EXTRA_ADDR);
3615d97408eSIcenowy Zheng 	if (err)
3625d97408eSIcenowy Zheng 		return err;
3635d97408eSIcenowy Zheng 
3645d97408eSIcenowy Zheng 	/* TMDS phy initialization */
3655d97408eSIcenowy Zheng 	err = regmap_multi_reg_write(anx78xx->map[I2C_IDX_RX_P0],
3665d97408eSIcenowy Zheng 				     tmds_phy_initialization,
3675d97408eSIcenowy Zheng 				     ARRAY_SIZE(tmds_phy_initialization));
3685d97408eSIcenowy Zheng 	if (err)
3695d97408eSIcenowy Zheng 		return err;
3705d97408eSIcenowy Zheng 
3715d97408eSIcenowy Zheng 	err = anx78xx_clear_hpd(anx78xx);
3725d97408eSIcenowy Zheng 	if (err)
3735d97408eSIcenowy Zheng 		return err;
3745d97408eSIcenowy Zheng 
3755d97408eSIcenowy Zheng 	return 0;
3765d97408eSIcenowy Zheng }
3775d97408eSIcenowy Zheng 
3785d97408eSIcenowy Zheng static const u8 dp_tx_output_precise_tune_bits[20] = {
3795d97408eSIcenowy Zheng 	0x01, 0x03, 0x07, 0x7f, 0x71, 0x6b, 0x7f,
3805d97408eSIcenowy Zheng 	0x73, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x00,
3815d97408eSIcenowy Zheng 	0x0c, 0x42, 0x1e, 0x3e, 0x72, 0x7e,
3825d97408eSIcenowy Zheng };
3835d97408eSIcenowy Zheng 
3845d97408eSIcenowy Zheng static int anx78xx_link_phy_initialization(struct anx78xx *anx78xx)
3855d97408eSIcenowy Zheng {
3865d97408eSIcenowy Zheng 	int err;
3875d97408eSIcenowy Zheng 
3885d97408eSIcenowy Zheng 	/*
3895d97408eSIcenowy Zheng 	 * REVISIT : It is writing to a RESERVED bits in Analog Control 0
3905d97408eSIcenowy Zheng 	 * register.
3915d97408eSIcenowy Zheng 	 */
3925d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_ANALOG_CTRL0_REG,
3935d97408eSIcenowy Zheng 			   0x02);
3945d97408eSIcenowy Zheng 	if (err)
3955d97408eSIcenowy Zheng 		return err;
3965d97408eSIcenowy Zheng 
3975d97408eSIcenowy Zheng 	/*
3985d97408eSIcenowy Zheng 	 * Write DP TX output emphasis precise tune bits.
3995d97408eSIcenowy Zheng 	 */
4005d97408eSIcenowy Zheng 	err = regmap_bulk_write(anx78xx->map[I2C_IDX_TX_P1],
4015d97408eSIcenowy Zheng 				SP_DP_TX_LT_CTRL0_REG,
4025d97408eSIcenowy Zheng 				dp_tx_output_precise_tune_bits,
4035d97408eSIcenowy Zheng 				ARRAY_SIZE(dp_tx_output_precise_tune_bits));
4045d97408eSIcenowy Zheng 
4055d97408eSIcenowy Zheng 	if (err)
4065d97408eSIcenowy Zheng 		return err;
4075d97408eSIcenowy Zheng 
4085d97408eSIcenowy Zheng 	return 0;
4095d97408eSIcenowy Zheng }
4105d97408eSIcenowy Zheng 
4115d97408eSIcenowy Zheng static int anx78xx_xtal_clk_sel(struct anx78xx *anx78xx)
4125d97408eSIcenowy Zheng {
4135d97408eSIcenowy Zheng 	unsigned int value;
4145d97408eSIcenowy Zheng 	int err;
4155d97408eSIcenowy Zheng 
4165d97408eSIcenowy Zheng 	err = regmap_update_bits(anx78xx->map[I2C_IDX_TX_P2],
4175d97408eSIcenowy Zheng 				 SP_ANALOG_DEBUG2_REG,
4185d97408eSIcenowy Zheng 				 SP_XTAL_FRQ | SP_FORCE_SW_OFF_BYPASS,
4195d97408eSIcenowy Zheng 				 SP_XTAL_FRQ_27M);
4205d97408eSIcenowy Zheng 	if (err)
4215d97408eSIcenowy Zheng 		return err;
4225d97408eSIcenowy Zheng 
4235d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL3_REG,
4245d97408eSIcenowy Zheng 			   XTAL_CLK & SP_WAIT_COUNTER_7_0_MASK);
4255d97408eSIcenowy Zheng 	if (err)
4265d97408eSIcenowy Zheng 		return err;
4275d97408eSIcenowy Zheng 
4285d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL4_REG,
4295d97408eSIcenowy Zheng 			   ((XTAL_CLK & 0xff00) >> 2) | (XTAL_CLK / 10));
4305d97408eSIcenowy Zheng 	if (err)
4315d97408eSIcenowy Zheng 		return err;
4325d97408eSIcenowy Zheng 
4335d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
4345d97408eSIcenowy Zheng 			   SP_I2C_GEN_10US_TIMER0_REG, XTAL_CLK & 0xff);
4355d97408eSIcenowy Zheng 	if (err)
4365d97408eSIcenowy Zheng 		return err;
4375d97408eSIcenowy Zheng 
4385d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
4395d97408eSIcenowy Zheng 			   SP_I2C_GEN_10US_TIMER1_REG,
4405d97408eSIcenowy Zheng 			   (XTAL_CLK & 0xff00) >> 8);
4415d97408eSIcenowy Zheng 	if (err)
4425d97408eSIcenowy Zheng 		return err;
4435d97408eSIcenowy Zheng 
4445d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_MISC_CTRL_REG,
4455d97408eSIcenowy Zheng 			   XTAL_CLK / 10 - 1);
4465d97408eSIcenowy Zheng 	if (err)
4475d97408eSIcenowy Zheng 		return err;
4485d97408eSIcenowy Zheng 
4495d97408eSIcenowy Zheng 	err = regmap_read(anx78xx->map[I2C_IDX_RX_P0],
4505d97408eSIcenowy Zheng 			  SP_HDMI_US_TIMER_CTRL_REG,
4515d97408eSIcenowy Zheng 			  &value);
4525d97408eSIcenowy Zheng 	if (err)
4535d97408eSIcenowy Zheng 		return err;
4545d97408eSIcenowy Zheng 
4555d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_RX_P0],
4565d97408eSIcenowy Zheng 			   SP_HDMI_US_TIMER_CTRL_REG,
4575d97408eSIcenowy Zheng 			   (value & SP_MS_TIMER_MARGIN_10_8_MASK) |
4585d97408eSIcenowy Zheng 			   ((((XTAL_CLK / 10) >> 1) - 2) << 3));
4595d97408eSIcenowy Zheng 	if (err)
4605d97408eSIcenowy Zheng 		return err;
4615d97408eSIcenowy Zheng 
4625d97408eSIcenowy Zheng 	return 0;
4635d97408eSIcenowy Zheng }
4645d97408eSIcenowy Zheng 
4655d97408eSIcenowy Zheng static const struct reg_sequence otp_key_protect[] = {
4665d97408eSIcenowy Zheng 	{ SP_OTP_KEY_PROTECT1_REG, SP_OTP_PSW1 },
4675d97408eSIcenowy Zheng 	{ SP_OTP_KEY_PROTECT2_REG, SP_OTP_PSW2 },
4685d97408eSIcenowy Zheng 	{ SP_OTP_KEY_PROTECT3_REG, SP_OTP_PSW3 },
4695d97408eSIcenowy Zheng };
4705d97408eSIcenowy Zheng 
4715d97408eSIcenowy Zheng static int anx78xx_tx_initialization(struct anx78xx *anx78xx)
4725d97408eSIcenowy Zheng {
4735d97408eSIcenowy Zheng 	int err;
4745d97408eSIcenowy Zheng 
4755d97408eSIcenowy Zheng 	/* Set terminal resistor to 50 ohm */
4765d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL2_REG,
4775d97408eSIcenowy Zheng 			   0x30);
4785d97408eSIcenowy Zheng 	if (err)
4795d97408eSIcenowy Zheng 		return err;
4805d97408eSIcenowy Zheng 
4815d97408eSIcenowy Zheng 	/* Enable aux double diff output */
4825d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
4835d97408eSIcenowy Zheng 			       SP_DP_AUX_CH_CTRL2_REG, 0x08);
4845d97408eSIcenowy Zheng 	if (err)
4855d97408eSIcenowy Zheng 		return err;
4865d97408eSIcenowy Zheng 
4875d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
4885d97408eSIcenowy Zheng 				 SP_DP_HDCP_CTRL_REG, SP_AUTO_EN |
4895d97408eSIcenowy Zheng 				 SP_AUTO_START);
4905d97408eSIcenowy Zheng 	if (err)
4915d97408eSIcenowy Zheng 		return err;
4925d97408eSIcenowy Zheng 
4935d97408eSIcenowy Zheng 	err = regmap_multi_reg_write(anx78xx->map[I2C_IDX_TX_P0],
4945d97408eSIcenowy Zheng 				     otp_key_protect,
4955d97408eSIcenowy Zheng 				     ARRAY_SIZE(otp_key_protect));
4965d97408eSIcenowy Zheng 	if (err)
4975d97408eSIcenowy Zheng 		return err;
4985d97408eSIcenowy Zheng 
4995d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
5005d97408eSIcenowy Zheng 			       SP_HDCP_KEY_COMMAND_REG, SP_DISABLE_SYNC_HDCP);
5015d97408eSIcenowy Zheng 	if (err)
5025d97408eSIcenowy Zheng 		return err;
5035d97408eSIcenowy Zheng 
5045d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL8_REG,
5055d97408eSIcenowy Zheng 			   SP_VID_VRES_TH);
5065d97408eSIcenowy Zheng 	if (err)
5075d97408eSIcenowy Zheng 		return err;
5085d97408eSIcenowy Zheng 
5095d97408eSIcenowy Zheng 	/*
5105d97408eSIcenowy Zheng 	 * DP HDCP auto authentication wait timer (when downstream starts to
5115d97408eSIcenowy Zheng 	 * auth, DP side will wait for this period then do auth automatically)
5125d97408eSIcenowy Zheng 	 */
5135d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_HDCP_AUTO_TIMER_REG,
5145d97408eSIcenowy Zheng 			   0x00);
5155d97408eSIcenowy Zheng 	if (err)
5165d97408eSIcenowy Zheng 		return err;
5175d97408eSIcenowy Zheng 
5185d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
5195d97408eSIcenowy Zheng 			       SP_DP_HDCP_CTRL_REG, SP_LINK_POLLING);
5205d97408eSIcenowy Zheng 	if (err)
5215d97408eSIcenowy Zheng 		return err;
5225d97408eSIcenowy Zheng 
5235d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
5245d97408eSIcenowy Zheng 			       SP_DP_LINK_DEBUG_CTRL_REG, SP_M_VID_DEBUG);
5255d97408eSIcenowy Zheng 	if (err)
5265d97408eSIcenowy Zheng 		return err;
5275d97408eSIcenowy Zheng 
5285d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2],
5295d97408eSIcenowy Zheng 			       SP_ANALOG_DEBUG2_REG, SP_POWERON_TIME_1P5MS);
5305d97408eSIcenowy Zheng 	if (err)
5315d97408eSIcenowy Zheng 		return err;
5325d97408eSIcenowy Zheng 
5335d97408eSIcenowy Zheng 	err = anx78xx_xtal_clk_sel(anx78xx);
5345d97408eSIcenowy Zheng 	if (err)
5355d97408eSIcenowy Zheng 		return err;
5365d97408eSIcenowy Zheng 
5375d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_DEFER_CTRL_REG,
5385d97408eSIcenowy Zheng 			   SP_DEFER_CTRL_EN | 0x0c);
5395d97408eSIcenowy Zheng 	if (err)
5405d97408eSIcenowy Zheng 		return err;
5415d97408eSIcenowy Zheng 
5425d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
5435d97408eSIcenowy Zheng 			       SP_DP_POLLING_CTRL_REG,
5445d97408eSIcenowy Zheng 			       SP_AUTO_POLLING_DISABLE);
5455d97408eSIcenowy Zheng 	if (err)
5465d97408eSIcenowy Zheng 		return err;
5475d97408eSIcenowy Zheng 
5485d97408eSIcenowy Zheng 	/*
5495d97408eSIcenowy Zheng 	 * Short the link integrity check timer to speed up bstatus
5505d97408eSIcenowy Zheng 	 * polling for HDCP CTS item 1A-07
5515d97408eSIcenowy Zheng 	 */
5525d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
5535d97408eSIcenowy Zheng 			   SP_HDCP_LINK_CHECK_TIMER_REG, 0x1d);
5545d97408eSIcenowy Zheng 	if (err)
5555d97408eSIcenowy Zheng 		return err;
5565d97408eSIcenowy Zheng 
5575d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
5585d97408eSIcenowy Zheng 			       SP_DP_MISC_CTRL_REG, SP_EQ_TRAINING_LOOP);
5595d97408eSIcenowy Zheng 	if (err)
5605d97408eSIcenowy Zheng 		return err;
5615d97408eSIcenowy Zheng 
5625d97408eSIcenowy Zheng 	/* Power down the main link by default */
5635d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
5645d97408eSIcenowy Zheng 			       SP_DP_ANALOG_POWER_DOWN_REG, SP_CH0_PD);
5655d97408eSIcenowy Zheng 	if (err)
5665d97408eSIcenowy Zheng 		return err;
5675d97408eSIcenowy Zheng 
5685d97408eSIcenowy Zheng 	err = anx78xx_link_phy_initialization(anx78xx);
5695d97408eSIcenowy Zheng 	if (err)
5705d97408eSIcenowy Zheng 		return err;
5715d97408eSIcenowy Zheng 
5725d97408eSIcenowy Zheng 	/* Gen m_clk with downspreading */
5735d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
5745d97408eSIcenowy Zheng 			       SP_DP_M_CALCULATION_CTRL_REG, SP_M_GEN_CLK_SEL);
5755d97408eSIcenowy Zheng 	if (err)
5765d97408eSIcenowy Zheng 		return err;
5775d97408eSIcenowy Zheng 
5785d97408eSIcenowy Zheng 	return 0;
5795d97408eSIcenowy Zheng }
5805d97408eSIcenowy Zheng 
5815d97408eSIcenowy Zheng static int anx78xx_enable_interrupts(struct anx78xx *anx78xx)
5825d97408eSIcenowy Zheng {
5835d97408eSIcenowy Zheng 	int err;
5845d97408eSIcenowy Zheng 
5855d97408eSIcenowy Zheng 	/*
5865d97408eSIcenowy Zheng 	 * BIT0: INT pin assertion polarity: 1 = assert high
5875d97408eSIcenowy Zheng 	 * BIT1: INT pin output type: 0 = push/pull
5885d97408eSIcenowy Zheng 	 */
5895d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_INT_CTRL_REG, 0x01);
5905d97408eSIcenowy Zheng 	if (err)
5915d97408eSIcenowy Zheng 		return err;
5925d97408eSIcenowy Zheng 
5935d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P2],
5945d97408eSIcenowy Zheng 			   SP_COMMON_INT_MASK4_REG, SP_HPD_LOST | SP_HPD_PLUG);
5955d97408eSIcenowy Zheng 	if (err)
5965d97408eSIcenowy Zheng 		return err;
5975d97408eSIcenowy Zheng 
5985d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_MASK1_REG,
5995d97408eSIcenowy Zheng 			   SP_TRAINING_FINISH);
6005d97408eSIcenowy Zheng 	if (err)
6015d97408eSIcenowy Zheng 		return err;
6025d97408eSIcenowy Zheng 
6035d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_INT_MASK1_REG,
6045d97408eSIcenowy Zheng 			   SP_CKDT_CHG | SP_SCDT_CHG);
6055d97408eSIcenowy Zheng 	if (err)
6065d97408eSIcenowy Zheng 		return err;
6075d97408eSIcenowy Zheng 
6085d97408eSIcenowy Zheng 	return 0;
6095d97408eSIcenowy Zheng }
6105d97408eSIcenowy Zheng 
6115d97408eSIcenowy Zheng static void anx78xx_poweron(struct anx78xx *anx78xx)
6125d97408eSIcenowy Zheng {
6135d97408eSIcenowy Zheng 	struct anx78xx_platform_data *pdata = &anx78xx->pdata;
6145d97408eSIcenowy Zheng 	int err;
6155d97408eSIcenowy Zheng 
6165d97408eSIcenowy Zheng 	if (WARN_ON(anx78xx->powered))
6175d97408eSIcenowy Zheng 		return;
6185d97408eSIcenowy Zheng 
6195d97408eSIcenowy Zheng 	if (pdata->dvdd10) {
6205d97408eSIcenowy Zheng 		err = regulator_enable(pdata->dvdd10);
6215d97408eSIcenowy Zheng 		if (err) {
6225d97408eSIcenowy Zheng 			DRM_ERROR("Failed to enable DVDD10 regulator: %d\n",
6235d97408eSIcenowy Zheng 				  err);
6245d97408eSIcenowy Zheng 			return;
6255d97408eSIcenowy Zheng 		}
6265d97408eSIcenowy Zheng 
6275d97408eSIcenowy Zheng 		usleep_range(1000, 2000);
6285d97408eSIcenowy Zheng 	}
6295d97408eSIcenowy Zheng 
6305d97408eSIcenowy Zheng 	gpiod_set_value_cansleep(pdata->gpiod_reset, 1);
6315d97408eSIcenowy Zheng 	usleep_range(1000, 2000);
6325d97408eSIcenowy Zheng 
6335d97408eSIcenowy Zheng 	gpiod_set_value_cansleep(pdata->gpiod_pd, 0);
6345d97408eSIcenowy Zheng 	usleep_range(1000, 2000);
6355d97408eSIcenowy Zheng 
6365d97408eSIcenowy Zheng 	gpiod_set_value_cansleep(pdata->gpiod_reset, 0);
6375d97408eSIcenowy Zheng 
6385d97408eSIcenowy Zheng 	/* Power on registers module */
6395d97408eSIcenowy Zheng 	anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,
6405d97408eSIcenowy Zheng 			 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
6415d97408eSIcenowy Zheng 	anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,
6425d97408eSIcenowy Zheng 			   SP_REGISTER_PD | SP_TOTAL_PD);
6435d97408eSIcenowy Zheng 
6445d97408eSIcenowy Zheng 	anx78xx->powered = true;
6455d97408eSIcenowy Zheng }
6465d97408eSIcenowy Zheng 
6475d97408eSIcenowy Zheng static void anx78xx_poweroff(struct anx78xx *anx78xx)
6485d97408eSIcenowy Zheng {
6495d97408eSIcenowy Zheng 	struct anx78xx_platform_data *pdata = &anx78xx->pdata;
6505d97408eSIcenowy Zheng 	int err;
6515d97408eSIcenowy Zheng 
6525d97408eSIcenowy Zheng 	if (WARN_ON(!anx78xx->powered))
6535d97408eSIcenowy Zheng 		return;
6545d97408eSIcenowy Zheng 
6555d97408eSIcenowy Zheng 	gpiod_set_value_cansleep(pdata->gpiod_reset, 1);
6565d97408eSIcenowy Zheng 	usleep_range(1000, 2000);
6575d97408eSIcenowy Zheng 
6585d97408eSIcenowy Zheng 	gpiod_set_value_cansleep(pdata->gpiod_pd, 1);
6595d97408eSIcenowy Zheng 	usleep_range(1000, 2000);
6605d97408eSIcenowy Zheng 
6615d97408eSIcenowy Zheng 	if (pdata->dvdd10) {
6625d97408eSIcenowy Zheng 		err = regulator_disable(pdata->dvdd10);
6635d97408eSIcenowy Zheng 		if (err) {
6645d97408eSIcenowy Zheng 			DRM_ERROR("Failed to disable DVDD10 regulator: %d\n",
6655d97408eSIcenowy Zheng 				  err);
6665d97408eSIcenowy Zheng 			return;
6675d97408eSIcenowy Zheng 		}
6685d97408eSIcenowy Zheng 
6695d97408eSIcenowy Zheng 		usleep_range(1000, 2000);
6705d97408eSIcenowy Zheng 	}
6715d97408eSIcenowy Zheng 
6725d97408eSIcenowy Zheng 	anx78xx->powered = false;
6735d97408eSIcenowy Zheng }
6745d97408eSIcenowy Zheng 
6755d97408eSIcenowy Zheng static int anx78xx_start(struct anx78xx *anx78xx)
6765d97408eSIcenowy Zheng {
6775d97408eSIcenowy Zheng 	int err;
6785d97408eSIcenowy Zheng 
6795d97408eSIcenowy Zheng 	/* Power on all modules */
6805d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2],
6815d97408eSIcenowy Zheng 				 SP_POWERDOWN_CTRL_REG,
6825d97408eSIcenowy Zheng 				 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD |
6835d97408eSIcenowy Zheng 				 SP_LINK_PD);
6845d97408eSIcenowy Zheng 
6855d97408eSIcenowy Zheng 	err = anx78xx_enable_interrupts(anx78xx);
6865d97408eSIcenowy Zheng 	if (err) {
6875d97408eSIcenowy Zheng 		DRM_ERROR("Failed to enable interrupts: %d\n", err);
6885d97408eSIcenowy Zheng 		goto err_poweroff;
6895d97408eSIcenowy Zheng 	}
6905d97408eSIcenowy Zheng 
6915d97408eSIcenowy Zheng 	err = anx78xx_rx_initialization(anx78xx);
6925d97408eSIcenowy Zheng 	if (err) {
6935d97408eSIcenowy Zheng 		DRM_ERROR("Failed receiver initialization: %d\n", err);
6945d97408eSIcenowy Zheng 		goto err_poweroff;
6955d97408eSIcenowy Zheng 	}
6965d97408eSIcenowy Zheng 
6975d97408eSIcenowy Zheng 	err = anx78xx_tx_initialization(anx78xx);
6985d97408eSIcenowy Zheng 	if (err) {
6995d97408eSIcenowy Zheng 		DRM_ERROR("Failed transmitter initialization: %d\n", err);
7005d97408eSIcenowy Zheng 		goto err_poweroff;
7015d97408eSIcenowy Zheng 	}
7025d97408eSIcenowy Zheng 
7035d97408eSIcenowy Zheng 	/*
7045d97408eSIcenowy Zheng 	 * This delay seems to help keep the hardware in a good state. Without
7055d97408eSIcenowy Zheng 	 * it, there are times where it fails silently.
7065d97408eSIcenowy Zheng 	 */
7075d97408eSIcenowy Zheng 	usleep_range(10000, 15000);
7085d97408eSIcenowy Zheng 
7095d97408eSIcenowy Zheng 	return 0;
7105d97408eSIcenowy Zheng 
7115d97408eSIcenowy Zheng err_poweroff:
7125d97408eSIcenowy Zheng 	DRM_ERROR("Failed SlimPort transmitter initialization: %d\n", err);
7135d97408eSIcenowy Zheng 	anx78xx_poweroff(anx78xx);
7145d97408eSIcenowy Zheng 
7155d97408eSIcenowy Zheng 	return err;
7165d97408eSIcenowy Zheng }
7175d97408eSIcenowy Zheng 
7185d97408eSIcenowy Zheng static int anx78xx_init_pdata(struct anx78xx *anx78xx)
7195d97408eSIcenowy Zheng {
7205d97408eSIcenowy Zheng 	struct anx78xx_platform_data *pdata = &anx78xx->pdata;
7215d97408eSIcenowy Zheng 	struct device *dev = &anx78xx->client->dev;
7225d97408eSIcenowy Zheng 
7235d97408eSIcenowy Zheng 	/* 1.0V digital core power regulator  */
7245d97408eSIcenowy Zheng 	pdata->dvdd10 = devm_regulator_get(dev, "dvdd10");
7255d97408eSIcenowy Zheng 	if (IS_ERR(pdata->dvdd10)) {
7265d97408eSIcenowy Zheng 		if (PTR_ERR(pdata->dvdd10) != -EPROBE_DEFER)
7275d97408eSIcenowy Zheng 			DRM_ERROR("DVDD10 regulator not found\n");
7285d97408eSIcenowy Zheng 
7295d97408eSIcenowy Zheng 		return PTR_ERR(pdata->dvdd10);
7305d97408eSIcenowy Zheng 	}
7315d97408eSIcenowy Zheng 
7325d97408eSIcenowy Zheng 	/* GPIO for HPD */
7335d97408eSIcenowy Zheng 	pdata->gpiod_hpd = devm_gpiod_get(dev, "hpd", GPIOD_IN);
7345d97408eSIcenowy Zheng 	if (IS_ERR(pdata->gpiod_hpd))
7355d97408eSIcenowy Zheng 		return PTR_ERR(pdata->gpiod_hpd);
7365d97408eSIcenowy Zheng 
7375d97408eSIcenowy Zheng 	/* GPIO for chip power down */
7385d97408eSIcenowy Zheng 	pdata->gpiod_pd = devm_gpiod_get(dev, "pd", GPIOD_OUT_HIGH);
7395d97408eSIcenowy Zheng 	if (IS_ERR(pdata->gpiod_pd))
7405d97408eSIcenowy Zheng 		return PTR_ERR(pdata->gpiod_pd);
7415d97408eSIcenowy Zheng 
7425d97408eSIcenowy Zheng 	/* GPIO for chip reset */
7435d97408eSIcenowy Zheng 	pdata->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
7445d97408eSIcenowy Zheng 
7455d97408eSIcenowy Zheng 	return PTR_ERR_OR_ZERO(pdata->gpiod_reset);
7465d97408eSIcenowy Zheng }
7475d97408eSIcenowy Zheng 
7485d97408eSIcenowy Zheng static int anx78xx_dp_link_training(struct anx78xx *anx78xx)
7495d97408eSIcenowy Zheng {
7505d97408eSIcenowy Zheng 	u8 dp_bw, dpcd[2];
7515d97408eSIcenowy Zheng 	int err;
7525d97408eSIcenowy Zheng 
7535d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_HDMI_MUTE_CTRL_REG,
7545d97408eSIcenowy Zheng 			   0x0);
7555d97408eSIcenowy Zheng 	if (err)
7565d97408eSIcenowy Zheng 		return err;
7575d97408eSIcenowy Zheng 
7585d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2],
7595d97408eSIcenowy Zheng 				 SP_POWERDOWN_CTRL_REG,
7605d97408eSIcenowy Zheng 				 SP_TOTAL_PD);
7615d97408eSIcenowy Zheng 	if (err)
7625d97408eSIcenowy Zheng 		return err;
7635d97408eSIcenowy Zheng 
7645d97408eSIcenowy Zheng 	err = drm_dp_dpcd_readb(&anx78xx->aux, DP_MAX_LINK_RATE, &dp_bw);
7655d97408eSIcenowy Zheng 	if (err < 0)
7665d97408eSIcenowy Zheng 		return err;
7675d97408eSIcenowy Zheng 
7685d97408eSIcenowy Zheng 	switch (dp_bw) {
7695d97408eSIcenowy Zheng 	case DP_LINK_BW_1_62:
7705d97408eSIcenowy Zheng 	case DP_LINK_BW_2_7:
7715d97408eSIcenowy Zheng 	case DP_LINK_BW_5_4:
7725d97408eSIcenowy Zheng 		break;
7735d97408eSIcenowy Zheng 
7745d97408eSIcenowy Zheng 	default:
7755d97408eSIcenowy Zheng 		DRM_DEBUG_KMS("DP bandwidth (%#02x) not supported\n", dp_bw);
7765d97408eSIcenowy Zheng 		return -EINVAL;
7775d97408eSIcenowy Zheng 	}
7785d97408eSIcenowy Zheng 
7795d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL1_REG,
7805d97408eSIcenowy Zheng 			       SP_VIDEO_MUTE);
7815d97408eSIcenowy Zheng 	if (err)
7825d97408eSIcenowy Zheng 		return err;
7835d97408eSIcenowy Zheng 
7845d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2],
7855d97408eSIcenowy Zheng 				 SP_VID_CTRL1_REG, SP_VIDEO_EN);
7865d97408eSIcenowy Zheng 	if (err)
7875d97408eSIcenowy Zheng 		return err;
7885d97408eSIcenowy Zheng 
7895d97408eSIcenowy Zheng 	/* Get DPCD info */
7905d97408eSIcenowy Zheng 	err = drm_dp_dpcd_read(&anx78xx->aux, DP_DPCD_REV,
7915d97408eSIcenowy Zheng 			       &anx78xx->dpcd, DP_RECEIVER_CAP_SIZE);
7925d97408eSIcenowy Zheng 	if (err < 0) {
7935d97408eSIcenowy Zheng 		DRM_ERROR("Failed to read DPCD: %d\n", err);
7945d97408eSIcenowy Zheng 		return err;
7955d97408eSIcenowy Zheng 	}
7965d97408eSIcenowy Zheng 
7975d97408eSIcenowy Zheng 	/* Clear channel x SERDES power down */
7985d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
7995d97408eSIcenowy Zheng 				 SP_DP_ANALOG_POWER_DOWN_REG, SP_CH0_PD);
8005d97408eSIcenowy Zheng 	if (err)
8015d97408eSIcenowy Zheng 		return err;
8025d97408eSIcenowy Zheng 
8035d97408eSIcenowy Zheng 	/*
8045d97408eSIcenowy Zheng 	 * Power up the sink (DP_SET_POWER register is only available on DPCD
8055d97408eSIcenowy Zheng 	 * v1.1 and later).
8065d97408eSIcenowy Zheng 	 */
8075d97408eSIcenowy Zheng 	if (anx78xx->dpcd[DP_DPCD_REV] >= 0x11) {
8085d97408eSIcenowy Zheng 		err = drm_dp_dpcd_readb(&anx78xx->aux, DP_SET_POWER, &dpcd[0]);
8095d97408eSIcenowy Zheng 		if (err < 0) {
8105d97408eSIcenowy Zheng 			DRM_ERROR("Failed to read DP_SET_POWER register: %d\n",
8115d97408eSIcenowy Zheng 				  err);
8125d97408eSIcenowy Zheng 			return err;
8135d97408eSIcenowy Zheng 		}
8145d97408eSIcenowy Zheng 
8155d97408eSIcenowy Zheng 		dpcd[0] &= ~DP_SET_POWER_MASK;
8165d97408eSIcenowy Zheng 		dpcd[0] |= DP_SET_POWER_D0;
8175d97408eSIcenowy Zheng 
8185d97408eSIcenowy Zheng 		err = drm_dp_dpcd_writeb(&anx78xx->aux, DP_SET_POWER, dpcd[0]);
8195d97408eSIcenowy Zheng 		if (err < 0) {
8205d97408eSIcenowy Zheng 			DRM_ERROR("Failed to power up DisplayPort link: %d\n",
8215d97408eSIcenowy Zheng 				  err);
8225d97408eSIcenowy Zheng 			return err;
8235d97408eSIcenowy Zheng 		}
8245d97408eSIcenowy Zheng 
8255d97408eSIcenowy Zheng 		/*
8265d97408eSIcenowy Zheng 		 * According to the DP 1.1 specification, a "Sink Device must
8275d97408eSIcenowy Zheng 		 * exit the power saving state within 1 ms" (Section 2.5.3.1,
8285d97408eSIcenowy Zheng 		 * Table 5-52, "Sink Control Field" (register 0x600).
8295d97408eSIcenowy Zheng 		 */
8305d97408eSIcenowy Zheng 		usleep_range(1000, 2000);
8315d97408eSIcenowy Zheng 	}
8325d97408eSIcenowy Zheng 
8335d97408eSIcenowy Zheng 	/* Possibly enable downspread on the sink */
8345d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
8355d97408eSIcenowy Zheng 			   SP_DP_DOWNSPREAD_CTRL1_REG, 0);
8365d97408eSIcenowy Zheng 	if (err)
8375d97408eSIcenowy Zheng 		return err;
8385d97408eSIcenowy Zheng 
8395d97408eSIcenowy Zheng 	if (anx78xx->dpcd[DP_MAX_DOWNSPREAD] & DP_MAX_DOWNSPREAD_0_5) {
8405d97408eSIcenowy Zheng 		DRM_DEBUG("Enable downspread on the sink\n");
8415d97408eSIcenowy Zheng 		/* 4000PPM */
8425d97408eSIcenowy Zheng 		err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
8435d97408eSIcenowy Zheng 				   SP_DP_DOWNSPREAD_CTRL1_REG, 8);
8445d97408eSIcenowy Zheng 		if (err)
8455d97408eSIcenowy Zheng 			return err;
8465d97408eSIcenowy Zheng 
8475d97408eSIcenowy Zheng 		err = drm_dp_dpcd_writeb(&anx78xx->aux, DP_DOWNSPREAD_CTRL,
8485d97408eSIcenowy Zheng 					 DP_SPREAD_AMP_0_5);
8495d97408eSIcenowy Zheng 		if (err < 0)
8505d97408eSIcenowy Zheng 			return err;
8515d97408eSIcenowy Zheng 	} else {
8525d97408eSIcenowy Zheng 		err = drm_dp_dpcd_writeb(&anx78xx->aux, DP_DOWNSPREAD_CTRL, 0);
8535d97408eSIcenowy Zheng 		if (err < 0)
8545d97408eSIcenowy Zheng 			return err;
8555d97408eSIcenowy Zheng 	}
8565d97408eSIcenowy Zheng 
8575d97408eSIcenowy Zheng 	/* Set the lane count and the link rate on the sink */
8585d97408eSIcenowy Zheng 	if (drm_dp_enhanced_frame_cap(anx78xx->dpcd))
8595d97408eSIcenowy Zheng 		err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
8605d97408eSIcenowy Zheng 				       SP_DP_SYSTEM_CTRL_BASE + 4,
8615d97408eSIcenowy Zheng 				       SP_ENHANCED_MODE);
8625d97408eSIcenowy Zheng 	else
8635d97408eSIcenowy Zheng 		err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
8645d97408eSIcenowy Zheng 					 SP_DP_SYSTEM_CTRL_BASE + 4,
8655d97408eSIcenowy Zheng 					 SP_ENHANCED_MODE);
8665d97408eSIcenowy Zheng 	if (err)
8675d97408eSIcenowy Zheng 		return err;
8685d97408eSIcenowy Zheng 
8695d97408eSIcenowy Zheng 	dpcd[0] = drm_dp_max_link_rate(anx78xx->dpcd);
8705d97408eSIcenowy Zheng 	dpcd[0] = drm_dp_link_rate_to_bw_code(dpcd[0]);
8715d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],
8725d97408eSIcenowy Zheng 			   SP_DP_MAIN_LINK_BW_SET_REG, dpcd[0]);
8735d97408eSIcenowy Zheng 	if (err)
8745d97408eSIcenowy Zheng 		return err;
8755d97408eSIcenowy Zheng 
8765d97408eSIcenowy Zheng 	dpcd[1] = drm_dp_max_lane_count(anx78xx->dpcd);
8775d97408eSIcenowy Zheng 
8785d97408eSIcenowy Zheng 	if (drm_dp_enhanced_frame_cap(anx78xx->dpcd))
8795d97408eSIcenowy Zheng 		dpcd[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
8805d97408eSIcenowy Zheng 
8815d97408eSIcenowy Zheng 	err = drm_dp_dpcd_write(&anx78xx->aux, DP_LINK_BW_SET, dpcd,
8825d97408eSIcenowy Zheng 				sizeof(dpcd));
8835d97408eSIcenowy Zheng 	if (err < 0) {
8845d97408eSIcenowy Zheng 		DRM_ERROR("Failed to configure link: %d\n", err);
8855d97408eSIcenowy Zheng 		return err;
8865d97408eSIcenowy Zheng 	}
8875d97408eSIcenowy Zheng 
8885d97408eSIcenowy Zheng 	/* Start training on the source */
8895d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_LT_CTRL_REG,
8905d97408eSIcenowy Zheng 			   SP_LT_EN);
8915d97408eSIcenowy Zheng 	if (err)
8925d97408eSIcenowy Zheng 		return err;
8935d97408eSIcenowy Zheng 
8945d97408eSIcenowy Zheng 	return 0;
8955d97408eSIcenowy Zheng }
8965d97408eSIcenowy Zheng 
8975d97408eSIcenowy Zheng static int anx78xx_config_dp_output(struct anx78xx *anx78xx)
8985d97408eSIcenowy Zheng {
8995d97408eSIcenowy Zheng 	int err;
9005d97408eSIcenowy Zheng 
9015d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL1_REG,
9025d97408eSIcenowy Zheng 				 SP_VIDEO_MUTE);
9035d97408eSIcenowy Zheng 	if (err)
9045d97408eSIcenowy Zheng 		return err;
9055d97408eSIcenowy Zheng 
9065d97408eSIcenowy Zheng 	/* Enable DP output */
9075d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL1_REG,
9085d97408eSIcenowy Zheng 			       SP_VIDEO_EN);
9095d97408eSIcenowy Zheng 	if (err)
9105d97408eSIcenowy Zheng 		return err;
9115d97408eSIcenowy Zheng 
9125d97408eSIcenowy Zheng 	return 0;
9135d97408eSIcenowy Zheng }
9145d97408eSIcenowy Zheng 
9155d97408eSIcenowy Zheng static int anx78xx_send_video_infoframe(struct anx78xx *anx78xx,
9165d97408eSIcenowy Zheng 					struct hdmi_avi_infoframe *frame)
9175d97408eSIcenowy Zheng {
9185d97408eSIcenowy Zheng 	u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
9195d97408eSIcenowy Zheng 	int err;
9205d97408eSIcenowy Zheng 
9215d97408eSIcenowy Zheng 	err = hdmi_avi_infoframe_pack(frame, buffer, sizeof(buffer));
9225d97408eSIcenowy Zheng 	if (err < 0) {
9235d97408eSIcenowy Zheng 		DRM_ERROR("Failed to pack AVI infoframe: %d\n", err);
9245d97408eSIcenowy Zheng 		return err;
9255d97408eSIcenowy Zheng 	}
9265d97408eSIcenowy Zheng 
9275d97408eSIcenowy Zheng 	err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],
9285d97408eSIcenowy Zheng 				 SP_PACKET_SEND_CTRL_REG, SP_AVI_IF_EN);
9295d97408eSIcenowy Zheng 	if (err)
9305d97408eSIcenowy Zheng 		return err;
9315d97408eSIcenowy Zheng 
9325d97408eSIcenowy Zheng 	err = regmap_bulk_write(anx78xx->map[I2C_IDX_TX_P2],
9335d97408eSIcenowy Zheng 				SP_INFOFRAME_AVI_DB1_REG, buffer,
9345d97408eSIcenowy Zheng 				frame->length);
9355d97408eSIcenowy Zheng 	if (err)
9365d97408eSIcenowy Zheng 		return err;
9375d97408eSIcenowy Zheng 
9385d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
9395d97408eSIcenowy Zheng 			       SP_PACKET_SEND_CTRL_REG, SP_AVI_IF_UD);
9405d97408eSIcenowy Zheng 	if (err)
9415d97408eSIcenowy Zheng 		return err;
9425d97408eSIcenowy Zheng 
9435d97408eSIcenowy Zheng 	err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],
9445d97408eSIcenowy Zheng 			       SP_PACKET_SEND_CTRL_REG, SP_AVI_IF_EN);
9455d97408eSIcenowy Zheng 	if (err)
9465d97408eSIcenowy Zheng 		return err;
9475d97408eSIcenowy Zheng 
9485d97408eSIcenowy Zheng 	return 0;
9495d97408eSIcenowy Zheng }
9505d97408eSIcenowy Zheng 
9515d97408eSIcenowy Zheng static int anx78xx_get_downstream_info(struct anx78xx *anx78xx)
9525d97408eSIcenowy Zheng {
9535d97408eSIcenowy Zheng 	u8 value;
9545d97408eSIcenowy Zheng 	int err;
9555d97408eSIcenowy Zheng 
9565d97408eSIcenowy Zheng 	err = drm_dp_dpcd_readb(&anx78xx->aux, DP_SINK_COUNT, &value);
9575d97408eSIcenowy Zheng 	if (err < 0) {
9585d97408eSIcenowy Zheng 		DRM_ERROR("Get sink count failed %d\n", err);
9595d97408eSIcenowy Zheng 		return err;
9605d97408eSIcenowy Zheng 	}
9615d97408eSIcenowy Zheng 
9625d97408eSIcenowy Zheng 	if (!DP_GET_SINK_COUNT(value)) {
9635d97408eSIcenowy Zheng 		DRM_ERROR("Downstream disconnected\n");
9645d97408eSIcenowy Zheng 		return -EIO;
9655d97408eSIcenowy Zheng 	}
9665d97408eSIcenowy Zheng 
9675d97408eSIcenowy Zheng 	return 0;
9685d97408eSIcenowy Zheng }
9695d97408eSIcenowy Zheng 
9705d97408eSIcenowy Zheng static int anx78xx_get_modes(struct drm_connector *connector)
9715d97408eSIcenowy Zheng {
9725d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = connector_to_anx78xx(connector);
9735d97408eSIcenowy Zheng 	int err, num_modes = 0;
9745d97408eSIcenowy Zheng 
9755d97408eSIcenowy Zheng 	if (WARN_ON(!anx78xx->powered))
9765d97408eSIcenowy Zheng 		return 0;
9775d97408eSIcenowy Zheng 
9785d97408eSIcenowy Zheng 	if (anx78xx->edid)
9795d97408eSIcenowy Zheng 		return drm_add_edid_modes(connector, anx78xx->edid);
9805d97408eSIcenowy Zheng 
9815d97408eSIcenowy Zheng 	mutex_lock(&anx78xx->lock);
9825d97408eSIcenowy Zheng 
9835d97408eSIcenowy Zheng 	err = anx78xx_get_downstream_info(anx78xx);
9845d97408eSIcenowy Zheng 	if (err) {
9855d97408eSIcenowy Zheng 		DRM_ERROR("Failed to get downstream info: %d\n", err);
9865d97408eSIcenowy Zheng 		goto unlock;
9875d97408eSIcenowy Zheng 	}
9885d97408eSIcenowy Zheng 
9895d97408eSIcenowy Zheng 	anx78xx->edid = drm_get_edid(connector, &anx78xx->aux.ddc);
9905d97408eSIcenowy Zheng 	if (!anx78xx->edid) {
9915d97408eSIcenowy Zheng 		DRM_ERROR("Failed to read EDID\n");
9925d97408eSIcenowy Zheng 		goto unlock;
9935d97408eSIcenowy Zheng 	}
9945d97408eSIcenowy Zheng 
9955d97408eSIcenowy Zheng 	err = drm_connector_update_edid_property(connector,
9965d97408eSIcenowy Zheng 						 anx78xx->edid);
9975d97408eSIcenowy Zheng 	if (err) {
9985d97408eSIcenowy Zheng 		DRM_ERROR("Failed to update EDID property: %d\n", err);
9995d97408eSIcenowy Zheng 		goto unlock;
10005d97408eSIcenowy Zheng 	}
10015d97408eSIcenowy Zheng 
10025d97408eSIcenowy Zheng 	num_modes = drm_add_edid_modes(connector, anx78xx->edid);
10035d97408eSIcenowy Zheng 
10045d97408eSIcenowy Zheng unlock:
10055d97408eSIcenowy Zheng 	mutex_unlock(&anx78xx->lock);
10065d97408eSIcenowy Zheng 
10075d97408eSIcenowy Zheng 	return num_modes;
10085d97408eSIcenowy Zheng }
10095d97408eSIcenowy Zheng 
10105d97408eSIcenowy Zheng static const struct drm_connector_helper_funcs anx78xx_connector_helper_funcs = {
10115d97408eSIcenowy Zheng 	.get_modes = anx78xx_get_modes,
10125d97408eSIcenowy Zheng };
10135d97408eSIcenowy Zheng 
10145d97408eSIcenowy Zheng static enum drm_connector_status anx78xx_detect(struct drm_connector *connector,
10155d97408eSIcenowy Zheng 						bool force)
10165d97408eSIcenowy Zheng {
10175d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = connector_to_anx78xx(connector);
10185d97408eSIcenowy Zheng 
10195d97408eSIcenowy Zheng 	if (!gpiod_get_value(anx78xx->pdata.gpiod_hpd))
10205d97408eSIcenowy Zheng 		return connector_status_disconnected;
10215d97408eSIcenowy Zheng 
10225d97408eSIcenowy Zheng 	return connector_status_connected;
10235d97408eSIcenowy Zheng }
10245d97408eSIcenowy Zheng 
10255d97408eSIcenowy Zheng static const struct drm_connector_funcs anx78xx_connector_funcs = {
10265d97408eSIcenowy Zheng 	.fill_modes = drm_helper_probe_single_connector_modes,
10275d97408eSIcenowy Zheng 	.detect = anx78xx_detect,
10285d97408eSIcenowy Zheng 	.destroy = drm_connector_cleanup,
10295d97408eSIcenowy Zheng 	.reset = drm_atomic_helper_connector_reset,
10305d97408eSIcenowy Zheng 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
10315d97408eSIcenowy Zheng 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
10325d97408eSIcenowy Zheng };
10335d97408eSIcenowy Zheng 
10345d97408eSIcenowy Zheng static int anx78xx_bridge_attach(struct drm_bridge *bridge)
10355d97408eSIcenowy Zheng {
10365d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
10375d97408eSIcenowy Zheng 	int err;
10385d97408eSIcenowy Zheng 
10395d97408eSIcenowy Zheng 	if (!bridge->encoder) {
10405d97408eSIcenowy Zheng 		DRM_ERROR("Parent encoder object not found");
10415d97408eSIcenowy Zheng 		return -ENODEV;
10425d97408eSIcenowy Zheng 	}
10435d97408eSIcenowy Zheng 
10445d97408eSIcenowy Zheng 	/* Register aux channel */
10455d97408eSIcenowy Zheng 	anx78xx->aux.name = "DP-AUX";
10465d97408eSIcenowy Zheng 	anx78xx->aux.dev = &anx78xx->client->dev;
10475d97408eSIcenowy Zheng 	anx78xx->aux.transfer = anx78xx_aux_transfer;
10485d97408eSIcenowy Zheng 
10495d97408eSIcenowy Zheng 	err = drm_dp_aux_register(&anx78xx->aux);
10505d97408eSIcenowy Zheng 	if (err < 0) {
10515d97408eSIcenowy Zheng 		DRM_ERROR("Failed to register aux channel: %d\n", err);
10525d97408eSIcenowy Zheng 		return err;
10535d97408eSIcenowy Zheng 	}
10545d97408eSIcenowy Zheng 
10555d97408eSIcenowy Zheng 	err = drm_connector_init(bridge->dev, &anx78xx->connector,
10565d97408eSIcenowy Zheng 				 &anx78xx_connector_funcs,
10575d97408eSIcenowy Zheng 				 DRM_MODE_CONNECTOR_DisplayPort);
10585d97408eSIcenowy Zheng 	if (err) {
10595d97408eSIcenowy Zheng 		DRM_ERROR("Failed to initialize connector: %d\n", err);
10605d97408eSIcenowy Zheng 		return err;
10615d97408eSIcenowy Zheng 	}
10625d97408eSIcenowy Zheng 
10635d97408eSIcenowy Zheng 	drm_connector_helper_add(&anx78xx->connector,
10645d97408eSIcenowy Zheng 				 &anx78xx_connector_helper_funcs);
10655d97408eSIcenowy Zheng 
10665d97408eSIcenowy Zheng 	err = drm_connector_register(&anx78xx->connector);
10675d97408eSIcenowy Zheng 	if (err) {
10685d97408eSIcenowy Zheng 		DRM_ERROR("Failed to register connector: %d\n", err);
10695d97408eSIcenowy Zheng 		return err;
10705d97408eSIcenowy Zheng 	}
10715d97408eSIcenowy Zheng 
10725d97408eSIcenowy Zheng 	anx78xx->connector.polled = DRM_CONNECTOR_POLL_HPD;
10735d97408eSIcenowy Zheng 
10745d97408eSIcenowy Zheng 	err = drm_connector_attach_encoder(&anx78xx->connector,
10755d97408eSIcenowy Zheng 					   bridge->encoder);
10765d97408eSIcenowy Zheng 	if (err) {
10775d97408eSIcenowy Zheng 		DRM_ERROR("Failed to link up connector to encoder: %d\n", err);
10785d97408eSIcenowy Zheng 		return err;
10795d97408eSIcenowy Zheng 	}
10805d97408eSIcenowy Zheng 
10815d97408eSIcenowy Zheng 	return 0;
10825d97408eSIcenowy Zheng }
10835d97408eSIcenowy Zheng 
10845d97408eSIcenowy Zheng static enum drm_mode_status
10855d97408eSIcenowy Zheng anx78xx_bridge_mode_valid(struct drm_bridge *bridge,
10865d97408eSIcenowy Zheng 			  const struct drm_display_mode *mode)
10875d97408eSIcenowy Zheng {
10885d97408eSIcenowy Zheng 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10895d97408eSIcenowy Zheng 		return MODE_NO_INTERLACE;
10905d97408eSIcenowy Zheng 
10915d97408eSIcenowy Zheng 	/* Max 1200p at 5.4 Ghz, one lane */
10925d97408eSIcenowy Zheng 	if (mode->clock > 154000)
10935d97408eSIcenowy Zheng 		return MODE_CLOCK_HIGH;
10945d97408eSIcenowy Zheng 
10955d97408eSIcenowy Zheng 	return MODE_OK;
10965d97408eSIcenowy Zheng }
10975d97408eSIcenowy Zheng 
10985d97408eSIcenowy Zheng static void anx78xx_bridge_disable(struct drm_bridge *bridge)
10995d97408eSIcenowy Zheng {
11005d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
11015d97408eSIcenowy Zheng 
11025d97408eSIcenowy Zheng 	/* Power off all modules except configuration registers access */
11035d97408eSIcenowy Zheng 	anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,
11045d97408eSIcenowy Zheng 			 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
11055d97408eSIcenowy Zheng }
11065d97408eSIcenowy Zheng 
11075d97408eSIcenowy Zheng static void anx78xx_bridge_mode_set(struct drm_bridge *bridge,
11085d97408eSIcenowy Zheng 				const struct drm_display_mode *mode,
11095d97408eSIcenowy Zheng 				const struct drm_display_mode *adjusted_mode)
11105d97408eSIcenowy Zheng {
11115d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
11125d97408eSIcenowy Zheng 	struct hdmi_avi_infoframe frame;
11135d97408eSIcenowy Zheng 	int err;
11145d97408eSIcenowy Zheng 
11155d97408eSIcenowy Zheng 	if (WARN_ON(!anx78xx->powered))
11165d97408eSIcenowy Zheng 		return;
11175d97408eSIcenowy Zheng 
11185d97408eSIcenowy Zheng 	mutex_lock(&anx78xx->lock);
11195d97408eSIcenowy Zheng 
11205d97408eSIcenowy Zheng 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame,
11215d97408eSIcenowy Zheng 						       &anx78xx->connector,
11225d97408eSIcenowy Zheng 						       adjusted_mode);
11235d97408eSIcenowy Zheng 	if (err) {
11245d97408eSIcenowy Zheng 		DRM_ERROR("Failed to setup AVI infoframe: %d\n", err);
11255d97408eSIcenowy Zheng 		goto unlock;
11265d97408eSIcenowy Zheng 	}
11275d97408eSIcenowy Zheng 
11285d97408eSIcenowy Zheng 	err = anx78xx_send_video_infoframe(anx78xx, &frame);
11295d97408eSIcenowy Zheng 	if (err)
11305d97408eSIcenowy Zheng 		DRM_ERROR("Failed to send AVI infoframe: %d\n", err);
11315d97408eSIcenowy Zheng 
11325d97408eSIcenowy Zheng unlock:
11335d97408eSIcenowy Zheng 	mutex_unlock(&anx78xx->lock);
11345d97408eSIcenowy Zheng }
11355d97408eSIcenowy Zheng 
11365d97408eSIcenowy Zheng static void anx78xx_bridge_enable(struct drm_bridge *bridge)
11375d97408eSIcenowy Zheng {
11385d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);
11395d97408eSIcenowy Zheng 	int err;
11405d97408eSIcenowy Zheng 
11415d97408eSIcenowy Zheng 	err = anx78xx_start(anx78xx);
11425d97408eSIcenowy Zheng 	if (err) {
11435d97408eSIcenowy Zheng 		DRM_ERROR("Failed to initialize: %d\n", err);
11445d97408eSIcenowy Zheng 		return;
11455d97408eSIcenowy Zheng 	}
11465d97408eSIcenowy Zheng 
11475d97408eSIcenowy Zheng 	err = anx78xx_set_hpd(anx78xx);
11485d97408eSIcenowy Zheng 	if (err)
11495d97408eSIcenowy Zheng 		DRM_ERROR("Failed to set HPD: %d\n", err);
11505d97408eSIcenowy Zheng }
11515d97408eSIcenowy Zheng 
11525d97408eSIcenowy Zheng static const struct drm_bridge_funcs anx78xx_bridge_funcs = {
11535d97408eSIcenowy Zheng 	.attach = anx78xx_bridge_attach,
11545d97408eSIcenowy Zheng 	.mode_valid = anx78xx_bridge_mode_valid,
11555d97408eSIcenowy Zheng 	.disable = anx78xx_bridge_disable,
11565d97408eSIcenowy Zheng 	.mode_set = anx78xx_bridge_mode_set,
11575d97408eSIcenowy Zheng 	.enable = anx78xx_bridge_enable,
11585d97408eSIcenowy Zheng };
11595d97408eSIcenowy Zheng 
11605d97408eSIcenowy Zheng static irqreturn_t anx78xx_hpd_threaded_handler(int irq, void *data)
11615d97408eSIcenowy Zheng {
11625d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = data;
11635d97408eSIcenowy Zheng 	int err;
11645d97408eSIcenowy Zheng 
11655d97408eSIcenowy Zheng 	if (anx78xx->powered)
11665d97408eSIcenowy Zheng 		return IRQ_HANDLED;
11675d97408eSIcenowy Zheng 
11685d97408eSIcenowy Zheng 	mutex_lock(&anx78xx->lock);
11695d97408eSIcenowy Zheng 
11705d97408eSIcenowy Zheng 	/* Cable is pulled, power on the chip */
11715d97408eSIcenowy Zheng 	anx78xx_poweron(anx78xx);
11725d97408eSIcenowy Zheng 
11735d97408eSIcenowy Zheng 	err = anx78xx_enable_interrupts(anx78xx);
11745d97408eSIcenowy Zheng 	if (err)
11755d97408eSIcenowy Zheng 		DRM_ERROR("Failed to enable interrupts: %d\n", err);
11765d97408eSIcenowy Zheng 
11775d97408eSIcenowy Zheng 	mutex_unlock(&anx78xx->lock);
11785d97408eSIcenowy Zheng 
11795d97408eSIcenowy Zheng 	return IRQ_HANDLED;
11805d97408eSIcenowy Zheng }
11815d97408eSIcenowy Zheng 
11825d97408eSIcenowy Zheng static int anx78xx_handle_dp_int_1(struct anx78xx *anx78xx, u8 irq)
11835d97408eSIcenowy Zheng {
11845d97408eSIcenowy Zheng 	int err;
11855d97408eSIcenowy Zheng 
11865d97408eSIcenowy Zheng 	DRM_DEBUG_KMS("Handle DP interrupt 1: %02x\n", irq);
11875d97408eSIcenowy Zheng 
11885d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_STATUS1_REG,
11895d97408eSIcenowy Zheng 			   irq);
11905d97408eSIcenowy Zheng 	if (err)
11915d97408eSIcenowy Zheng 		return err;
11925d97408eSIcenowy Zheng 
11935d97408eSIcenowy Zheng 	if (irq & SP_TRAINING_FINISH) {
11945d97408eSIcenowy Zheng 		DRM_DEBUG_KMS("IRQ: hardware link training finished\n");
11955d97408eSIcenowy Zheng 		err = anx78xx_config_dp_output(anx78xx);
11965d97408eSIcenowy Zheng 	}
11975d97408eSIcenowy Zheng 
11985d97408eSIcenowy Zheng 	return err;
11995d97408eSIcenowy Zheng }
12005d97408eSIcenowy Zheng 
12015d97408eSIcenowy Zheng static bool anx78xx_handle_common_int_4(struct anx78xx *anx78xx, u8 irq)
12025d97408eSIcenowy Zheng {
12035d97408eSIcenowy Zheng 	bool event = false;
12045d97408eSIcenowy Zheng 	int err;
12055d97408eSIcenowy Zheng 
12065d97408eSIcenowy Zheng 	DRM_DEBUG_KMS("Handle common interrupt 4: %02x\n", irq);
12075d97408eSIcenowy Zheng 
12085d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_TX_P2],
12095d97408eSIcenowy Zheng 			   SP_COMMON_INT_STATUS4_REG, irq);
12105d97408eSIcenowy Zheng 	if (err) {
12115d97408eSIcenowy Zheng 		DRM_ERROR("Failed to write SP_COMMON_INT_STATUS4 %d\n", err);
12125d97408eSIcenowy Zheng 		return event;
12135d97408eSIcenowy Zheng 	}
12145d97408eSIcenowy Zheng 
12155d97408eSIcenowy Zheng 	if (irq & SP_HPD_LOST) {
12165d97408eSIcenowy Zheng 		DRM_DEBUG_KMS("IRQ: Hot plug detect - cable is pulled out\n");
12175d97408eSIcenowy Zheng 		event = true;
12185d97408eSIcenowy Zheng 		anx78xx_poweroff(anx78xx);
12195d97408eSIcenowy Zheng 		/* Free cached EDID */
12205d97408eSIcenowy Zheng 		kfree(anx78xx->edid);
12215d97408eSIcenowy Zheng 		anx78xx->edid = NULL;
12225d97408eSIcenowy Zheng 	} else if (irq & SP_HPD_PLUG) {
12235d97408eSIcenowy Zheng 		DRM_DEBUG_KMS("IRQ: Hot plug detect - cable plug\n");
12245d97408eSIcenowy Zheng 		event = true;
12255d97408eSIcenowy Zheng 	}
12265d97408eSIcenowy Zheng 
12275d97408eSIcenowy Zheng 	return event;
12285d97408eSIcenowy Zheng }
12295d97408eSIcenowy Zheng 
12305d97408eSIcenowy Zheng static void anx78xx_handle_hdmi_int_1(struct anx78xx *anx78xx, u8 irq)
12315d97408eSIcenowy Zheng {
12325d97408eSIcenowy Zheng 	unsigned int value;
12335d97408eSIcenowy Zheng 	int err;
12345d97408eSIcenowy Zheng 
12355d97408eSIcenowy Zheng 	DRM_DEBUG_KMS("Handle HDMI interrupt 1: %02x\n", irq);
12365d97408eSIcenowy Zheng 
12375d97408eSIcenowy Zheng 	err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_INT_STATUS1_REG,
12385d97408eSIcenowy Zheng 			   irq);
12395d97408eSIcenowy Zheng 	if (err) {
12405d97408eSIcenowy Zheng 		DRM_ERROR("Write HDMI int 1 failed: %d\n", err);
12415d97408eSIcenowy Zheng 		return;
12425d97408eSIcenowy Zheng 	}
12435d97408eSIcenowy Zheng 
12445d97408eSIcenowy Zheng 	if ((irq & SP_CKDT_CHG) || (irq & SP_SCDT_CHG)) {
12455d97408eSIcenowy Zheng 		DRM_DEBUG_KMS("IRQ: HDMI input detected\n");
12465d97408eSIcenowy Zheng 
12475d97408eSIcenowy Zheng 		err = regmap_read(anx78xx->map[I2C_IDX_RX_P0],
12485d97408eSIcenowy Zheng 				  SP_SYSTEM_STATUS_REG, &value);
12495d97408eSIcenowy Zheng 		if (err) {
12505d97408eSIcenowy Zheng 			DRM_ERROR("Read system status reg failed: %d\n", err);
12515d97408eSIcenowy Zheng 			return;
12525d97408eSIcenowy Zheng 		}
12535d97408eSIcenowy Zheng 
12545d97408eSIcenowy Zheng 		if (!(value & SP_TMDS_CLOCK_DET)) {
12555d97408eSIcenowy Zheng 			DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI clock ***\n");
12565d97408eSIcenowy Zheng 			return;
12575d97408eSIcenowy Zheng 		}
12585d97408eSIcenowy Zheng 
12595d97408eSIcenowy Zheng 		if (!(value & SP_TMDS_DE_DET)) {
12605d97408eSIcenowy Zheng 			DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI signal ***\n");
12615d97408eSIcenowy Zheng 			return;
12625d97408eSIcenowy Zheng 		}
12635d97408eSIcenowy Zheng 
12645d97408eSIcenowy Zheng 		err = anx78xx_dp_link_training(anx78xx);
12655d97408eSIcenowy Zheng 		if (err)
12665d97408eSIcenowy Zheng 			DRM_ERROR("Failed to start link training: %d\n", err);
12675d97408eSIcenowy Zheng 	}
12685d97408eSIcenowy Zheng }
12695d97408eSIcenowy Zheng 
12705d97408eSIcenowy Zheng static irqreturn_t anx78xx_intp_threaded_handler(int unused, void *data)
12715d97408eSIcenowy Zheng {
12725d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = data;
12735d97408eSIcenowy Zheng 	bool event = false;
12745d97408eSIcenowy Zheng 	unsigned int irq;
12755d97408eSIcenowy Zheng 	int err;
12765d97408eSIcenowy Zheng 
12775d97408eSIcenowy Zheng 	mutex_lock(&anx78xx->lock);
12785d97408eSIcenowy Zheng 
12795d97408eSIcenowy Zheng 	err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_STATUS1_REG,
12805d97408eSIcenowy Zheng 			  &irq);
12815d97408eSIcenowy Zheng 	if (err) {
12825d97408eSIcenowy Zheng 		DRM_ERROR("Failed to read DP interrupt 1 status: %d\n", err);
12835d97408eSIcenowy Zheng 		goto unlock;
12845d97408eSIcenowy Zheng 	}
12855d97408eSIcenowy Zheng 
12865d97408eSIcenowy Zheng 	if (irq)
12875d97408eSIcenowy Zheng 		anx78xx_handle_dp_int_1(anx78xx, irq);
12885d97408eSIcenowy Zheng 
12895d97408eSIcenowy Zheng 	err = regmap_read(anx78xx->map[I2C_IDX_TX_P2],
12905d97408eSIcenowy Zheng 			  SP_COMMON_INT_STATUS4_REG, &irq);
12915d97408eSIcenowy Zheng 	if (err) {
12925d97408eSIcenowy Zheng 		DRM_ERROR("Failed to read common interrupt 4 status: %d\n",
12935d97408eSIcenowy Zheng 			  err);
12945d97408eSIcenowy Zheng 		goto unlock;
12955d97408eSIcenowy Zheng 	}
12965d97408eSIcenowy Zheng 
12975d97408eSIcenowy Zheng 	if (irq)
12985d97408eSIcenowy Zheng 		event = anx78xx_handle_common_int_4(anx78xx, irq);
12995d97408eSIcenowy Zheng 
13005d97408eSIcenowy Zheng 	/* Make sure we are still powered after handle HPD events */
13015d97408eSIcenowy Zheng 	if (!anx78xx->powered)
13025d97408eSIcenowy Zheng 		goto unlock;
13035d97408eSIcenowy Zheng 
13045d97408eSIcenowy Zheng 	err = regmap_read(anx78xx->map[I2C_IDX_RX_P0], SP_INT_STATUS1_REG,
13055d97408eSIcenowy Zheng 			  &irq);
13065d97408eSIcenowy Zheng 	if (err) {
13075d97408eSIcenowy Zheng 		DRM_ERROR("Failed to read HDMI int 1 status: %d\n", err);
13085d97408eSIcenowy Zheng 		goto unlock;
13095d97408eSIcenowy Zheng 	}
13105d97408eSIcenowy Zheng 
13115d97408eSIcenowy Zheng 	if (irq)
13125d97408eSIcenowy Zheng 		anx78xx_handle_hdmi_int_1(anx78xx, irq);
13135d97408eSIcenowy Zheng 
13145d97408eSIcenowy Zheng unlock:
13155d97408eSIcenowy Zheng 	mutex_unlock(&anx78xx->lock);
13165d97408eSIcenowy Zheng 
13175d97408eSIcenowy Zheng 	if (event)
13185d97408eSIcenowy Zheng 		drm_helper_hpd_irq_event(anx78xx->connector.dev);
13195d97408eSIcenowy Zheng 
13205d97408eSIcenowy Zheng 	return IRQ_HANDLED;
13215d97408eSIcenowy Zheng }
13225d97408eSIcenowy Zheng 
13235d97408eSIcenowy Zheng static void unregister_i2c_dummy_clients(struct anx78xx *anx78xx)
13245d97408eSIcenowy Zheng {
13255d97408eSIcenowy Zheng 	unsigned int i;
13265d97408eSIcenowy Zheng 
13275d97408eSIcenowy Zheng 	for (i = 0; i < ARRAY_SIZE(anx78xx->i2c_dummy); i++)
13285d97408eSIcenowy Zheng 		i2c_unregister_device(anx78xx->i2c_dummy[i]);
13295d97408eSIcenowy Zheng }
13305d97408eSIcenowy Zheng 
13315d97408eSIcenowy Zheng static const struct regmap_config anx78xx_regmap_config = {
13325d97408eSIcenowy Zheng 	.reg_bits = 8,
13335d97408eSIcenowy Zheng 	.val_bits = 8,
13345d97408eSIcenowy Zheng };
13355d97408eSIcenowy Zheng 
13365d97408eSIcenowy Zheng static const u16 anx78xx_chipid_list[] = {
13375d97408eSIcenowy Zheng 	0x7808,
13385d97408eSIcenowy Zheng 	0x7812,
13395d97408eSIcenowy Zheng 	0x7814,
13405d97408eSIcenowy Zheng 	0x7818,
13415d97408eSIcenowy Zheng };
13425d97408eSIcenowy Zheng 
13435d97408eSIcenowy Zheng static int anx78xx_i2c_probe(struct i2c_client *client,
13445d97408eSIcenowy Zheng 			     const struct i2c_device_id *id)
13455d97408eSIcenowy Zheng {
13465d97408eSIcenowy Zheng 	struct anx78xx *anx78xx;
13475d97408eSIcenowy Zheng 	struct anx78xx_platform_data *pdata;
13485d97408eSIcenowy Zheng 	unsigned int i, idl, idh, version;
13495d97408eSIcenowy Zheng 	const u8 *i2c_addresses;
13505d97408eSIcenowy Zheng 	bool found = false;
13515d97408eSIcenowy Zheng 	int err;
13525d97408eSIcenowy Zheng 
13535d97408eSIcenowy Zheng 	anx78xx = devm_kzalloc(&client->dev, sizeof(*anx78xx), GFP_KERNEL);
13545d97408eSIcenowy Zheng 	if (!anx78xx)
13555d97408eSIcenowy Zheng 		return -ENOMEM;
13565d97408eSIcenowy Zheng 
13575d97408eSIcenowy Zheng 	pdata = &anx78xx->pdata;
13585d97408eSIcenowy Zheng 
13595d97408eSIcenowy Zheng 	mutex_init(&anx78xx->lock);
13605d97408eSIcenowy Zheng 
13615d97408eSIcenowy Zheng #if IS_ENABLED(CONFIG_OF)
13625d97408eSIcenowy Zheng 	anx78xx->bridge.of_node = client->dev.of_node;
13635d97408eSIcenowy Zheng #endif
13645d97408eSIcenowy Zheng 
13655d97408eSIcenowy Zheng 	anx78xx->client = client;
13665d97408eSIcenowy Zheng 	i2c_set_clientdata(client, anx78xx);
13675d97408eSIcenowy Zheng 
13685d97408eSIcenowy Zheng 	err = anx78xx_init_pdata(anx78xx);
13695d97408eSIcenowy Zheng 	if (err) {
13705d97408eSIcenowy Zheng 		if (err != -EPROBE_DEFER)
13715d97408eSIcenowy Zheng 			DRM_ERROR("Failed to initialize pdata: %d\n", err);
13725d97408eSIcenowy Zheng 
13735d97408eSIcenowy Zheng 		return err;
13745d97408eSIcenowy Zheng 	}
13755d97408eSIcenowy Zheng 
13765d97408eSIcenowy Zheng 	pdata->hpd_irq = gpiod_to_irq(pdata->gpiod_hpd);
13775d97408eSIcenowy Zheng 	if (pdata->hpd_irq < 0) {
13785d97408eSIcenowy Zheng 		DRM_ERROR("Failed to get HPD IRQ: %d\n", pdata->hpd_irq);
13795d97408eSIcenowy Zheng 		return -ENODEV;
13805d97408eSIcenowy Zheng 	}
13815d97408eSIcenowy Zheng 
13825d97408eSIcenowy Zheng 	pdata->intp_irq = client->irq;
13835d97408eSIcenowy Zheng 	if (!pdata->intp_irq) {
13845d97408eSIcenowy Zheng 		DRM_ERROR("Failed to get CABLE_DET and INTP IRQ\n");
13855d97408eSIcenowy Zheng 		return -ENODEV;
13865d97408eSIcenowy Zheng 	}
13875d97408eSIcenowy Zheng 
13885d97408eSIcenowy Zheng 	/* Map slave addresses of ANX7814 */
13895d97408eSIcenowy Zheng 	i2c_addresses = device_get_match_data(&client->dev);
13905d97408eSIcenowy Zheng 	for (i = 0; i < I2C_NUM_ADDRESSES; i++) {
13915d97408eSIcenowy Zheng 		struct i2c_client *i2c_dummy;
13925d97408eSIcenowy Zheng 
13935d97408eSIcenowy Zheng 		i2c_dummy = i2c_new_dummy_device(client->adapter,
13945d97408eSIcenowy Zheng 						 i2c_addresses[i] >> 1);
13955d97408eSIcenowy Zheng 		if (IS_ERR(i2c_dummy)) {
13965d97408eSIcenowy Zheng 			err = PTR_ERR(i2c_dummy);
13975d97408eSIcenowy Zheng 			DRM_ERROR("Failed to reserve I2C bus %02x: %d\n",
13985d97408eSIcenowy Zheng 				  i2c_addresses[i], err);
13995d97408eSIcenowy Zheng 			goto err_unregister_i2c;
14005d97408eSIcenowy Zheng 		}
14015d97408eSIcenowy Zheng 
14025d97408eSIcenowy Zheng 		anx78xx->i2c_dummy[i] = i2c_dummy;
14035d97408eSIcenowy Zheng 		anx78xx->map[i] = devm_regmap_init_i2c(anx78xx->i2c_dummy[i],
14045d97408eSIcenowy Zheng 						       &anx78xx_regmap_config);
14055d97408eSIcenowy Zheng 		if (IS_ERR(anx78xx->map[i])) {
14065d97408eSIcenowy Zheng 			err = PTR_ERR(anx78xx->map[i]);
14075d97408eSIcenowy Zheng 			DRM_ERROR("Failed regmap initialization %02x\n",
14085d97408eSIcenowy Zheng 				  i2c_addresses[i]);
14095d97408eSIcenowy Zheng 			goto err_unregister_i2c;
14105d97408eSIcenowy Zheng 		}
14115d97408eSIcenowy Zheng 	}
14125d97408eSIcenowy Zheng 
14135d97408eSIcenowy Zheng 	/* Look for supported chip ID */
14145d97408eSIcenowy Zheng 	anx78xx_poweron(anx78xx);
14155d97408eSIcenowy Zheng 
14165d97408eSIcenowy Zheng 	err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_IDL_REG,
14175d97408eSIcenowy Zheng 			  &idl);
14185d97408eSIcenowy Zheng 	if (err)
14195d97408eSIcenowy Zheng 		goto err_poweroff;
14205d97408eSIcenowy Zheng 
14215d97408eSIcenowy Zheng 	err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_IDH_REG,
14225d97408eSIcenowy Zheng 			  &idh);
14235d97408eSIcenowy Zheng 	if (err)
14245d97408eSIcenowy Zheng 		goto err_poweroff;
14255d97408eSIcenowy Zheng 
14265d97408eSIcenowy Zheng 	anx78xx->chipid = (u8)idl | ((u8)idh << 8);
14275d97408eSIcenowy Zheng 
14285d97408eSIcenowy Zheng 	err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_VERSION_REG,
14295d97408eSIcenowy Zheng 			  &version);
14305d97408eSIcenowy Zheng 	if (err)
14315d97408eSIcenowy Zheng 		goto err_poweroff;
14325d97408eSIcenowy Zheng 
14335d97408eSIcenowy Zheng 	for (i = 0; i < ARRAY_SIZE(anx78xx_chipid_list); i++) {
14345d97408eSIcenowy Zheng 		if (anx78xx->chipid == anx78xx_chipid_list[i]) {
14355d97408eSIcenowy Zheng 			DRM_INFO("Found ANX%x (ver. %d) SlimPort Transmitter\n",
14365d97408eSIcenowy Zheng 				 anx78xx->chipid, version);
14375d97408eSIcenowy Zheng 			found = true;
14385d97408eSIcenowy Zheng 			break;
14395d97408eSIcenowy Zheng 		}
14405d97408eSIcenowy Zheng 	}
14415d97408eSIcenowy Zheng 
14425d97408eSIcenowy Zheng 	if (!found) {
14435d97408eSIcenowy Zheng 		DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",
14445d97408eSIcenowy Zheng 			  anx78xx->chipid, version);
14455d97408eSIcenowy Zheng 		err = -ENODEV;
14465d97408eSIcenowy Zheng 		goto err_poweroff;
14475d97408eSIcenowy Zheng 	}
14485d97408eSIcenowy Zheng 
14495d97408eSIcenowy Zheng 	err = devm_request_threaded_irq(&client->dev, pdata->hpd_irq, NULL,
14505d97408eSIcenowy Zheng 					anx78xx_hpd_threaded_handler,
14515d97408eSIcenowy Zheng 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
14525d97408eSIcenowy Zheng 					"anx78xx-hpd", anx78xx);
14535d97408eSIcenowy Zheng 	if (err) {
14545d97408eSIcenowy Zheng 		DRM_ERROR("Failed to request CABLE_DET threaded IRQ: %d\n",
14555d97408eSIcenowy Zheng 			  err);
14565d97408eSIcenowy Zheng 		goto err_poweroff;
14575d97408eSIcenowy Zheng 	}
14585d97408eSIcenowy Zheng 
14595d97408eSIcenowy Zheng 	err = devm_request_threaded_irq(&client->dev, pdata->intp_irq, NULL,
14605d97408eSIcenowy Zheng 					anx78xx_intp_threaded_handler,
14615d97408eSIcenowy Zheng 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
14625d97408eSIcenowy Zheng 					"anx78xx-intp", anx78xx);
14635d97408eSIcenowy Zheng 	if (err) {
14645d97408eSIcenowy Zheng 		DRM_ERROR("Failed to request INTP threaded IRQ: %d\n", err);
14655d97408eSIcenowy Zheng 		goto err_poweroff;
14665d97408eSIcenowy Zheng 	}
14675d97408eSIcenowy Zheng 
14685d97408eSIcenowy Zheng 	anx78xx->bridge.funcs = &anx78xx_bridge_funcs;
14695d97408eSIcenowy Zheng 
14705d97408eSIcenowy Zheng 	drm_bridge_add(&anx78xx->bridge);
14715d97408eSIcenowy Zheng 
14725d97408eSIcenowy Zheng 	/* If cable is pulled out, just poweroff and wait for HPD event */
14735d97408eSIcenowy Zheng 	if (!gpiod_get_value(anx78xx->pdata.gpiod_hpd))
14745d97408eSIcenowy Zheng 		anx78xx_poweroff(anx78xx);
14755d97408eSIcenowy Zheng 
14765d97408eSIcenowy Zheng 	return 0;
14775d97408eSIcenowy Zheng 
14785d97408eSIcenowy Zheng err_poweroff:
14795d97408eSIcenowy Zheng 	anx78xx_poweroff(anx78xx);
14805d97408eSIcenowy Zheng 
14815d97408eSIcenowy Zheng err_unregister_i2c:
14825d97408eSIcenowy Zheng 	unregister_i2c_dummy_clients(anx78xx);
14835d97408eSIcenowy Zheng 	return err;
14845d97408eSIcenowy Zheng }
14855d97408eSIcenowy Zheng 
14865d97408eSIcenowy Zheng static int anx78xx_i2c_remove(struct i2c_client *client)
14875d97408eSIcenowy Zheng {
14885d97408eSIcenowy Zheng 	struct anx78xx *anx78xx = i2c_get_clientdata(client);
14895d97408eSIcenowy Zheng 
14905d97408eSIcenowy Zheng 	drm_bridge_remove(&anx78xx->bridge);
14915d97408eSIcenowy Zheng 
14925d97408eSIcenowy Zheng 	unregister_i2c_dummy_clients(anx78xx);
14935d97408eSIcenowy Zheng 
14945d97408eSIcenowy Zheng 	kfree(anx78xx->edid);
14955d97408eSIcenowy Zheng 
14965d97408eSIcenowy Zheng 	return 0;
14975d97408eSIcenowy Zheng }
14985d97408eSIcenowy Zheng 
14995d97408eSIcenowy Zheng static const struct i2c_device_id anx78xx_id[] = {
15005d97408eSIcenowy Zheng 	{ "anx7814", 0 },
15015d97408eSIcenowy Zheng 	{ /* sentinel */ }
15025d97408eSIcenowy Zheng };
15035d97408eSIcenowy Zheng MODULE_DEVICE_TABLE(i2c, anx78xx_id);
15045d97408eSIcenowy Zheng 
15055d97408eSIcenowy Zheng #if IS_ENABLED(CONFIG_OF)
15065d97408eSIcenowy Zheng static const struct of_device_id anx78xx_match_table[] = {
15075d97408eSIcenowy Zheng 	{ .compatible = "analogix,anx7808", .data = anx7808_i2c_addresses },
15085d97408eSIcenowy Zheng 	{ .compatible = "analogix,anx7812", .data = anx781x_i2c_addresses },
15095d97408eSIcenowy Zheng 	{ .compatible = "analogix,anx7814", .data = anx781x_i2c_addresses },
15105d97408eSIcenowy Zheng 	{ .compatible = "analogix,anx7818", .data = anx781x_i2c_addresses },
15115d97408eSIcenowy Zheng 	{ /* sentinel */ },
15125d97408eSIcenowy Zheng };
15135d97408eSIcenowy Zheng MODULE_DEVICE_TABLE(of, anx78xx_match_table);
15145d97408eSIcenowy Zheng #endif
15155d97408eSIcenowy Zheng 
15165d97408eSIcenowy Zheng static struct i2c_driver anx78xx_driver = {
15175d97408eSIcenowy Zheng 	.driver = {
15185d97408eSIcenowy Zheng 		   .name = "anx7814",
15195d97408eSIcenowy Zheng 		   .of_match_table = of_match_ptr(anx78xx_match_table),
15205d97408eSIcenowy Zheng 		  },
15215d97408eSIcenowy Zheng 	.probe = anx78xx_i2c_probe,
15225d97408eSIcenowy Zheng 	.remove = anx78xx_i2c_remove,
15235d97408eSIcenowy Zheng 	.id_table = anx78xx_id,
15245d97408eSIcenowy Zheng };
15255d97408eSIcenowy Zheng module_i2c_driver(anx78xx_driver);
15265d97408eSIcenowy Zheng 
15275d97408eSIcenowy Zheng MODULE_DESCRIPTION("ANX78xx SlimPort Transmitter driver");
15285d97408eSIcenowy Zheng MODULE_AUTHOR("Enric Balletbo i Serra <enric.balletbo@collabora.com>");
15295d97408eSIcenowy Zheng MODULE_LICENSE("GPL v2");
1530