Path: blob/master/drivers/gpu/drm/bridge/analogix/analogix-anx78xx.c
26516 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright(c) 2016, Analogix Semiconductor.3*4* Based on anx7808 driver obtained from chromeos with copyright:5* Copyright(c) 2013, Google Inc.6*/7#include <linux/delay.h>8#include <linux/err.h>9#include <linux/gpio/consumer.h>10#include <linux/i2c.h>11#include <linux/interrupt.h>12#include <linux/kernel.h>13#include <linux/module.h>14#include <linux/of_irq.h>15#include <linux/of_platform.h>16#include <linux/regmap.h>17#include <linux/regulator/consumer.h>18#include <linux/types.h>1920#include <drm/display/drm_dp_helper.h>21#include <drm/drm_atomic_helper.h>22#include <drm/drm_bridge.h>23#include <drm/drm_crtc.h>24#include <drm/drm_edid.h>25#include <drm/drm_print.h>26#include <drm/drm_probe_helper.h>2728#include "analogix-anx78xx.h"2930#define I2C_NUM_ADDRESSES 531#define I2C_IDX_TX_P0 032#define I2C_IDX_TX_P1 133#define I2C_IDX_TX_P2 234#define I2C_IDX_RX_P0 335#define I2C_IDX_RX_P1 43637#define XTAL_CLK 270 /* 27M */3839static const u8 anx7808_i2c_addresses[] = {40[I2C_IDX_TX_P0] = 0x78,41[I2C_IDX_TX_P1] = 0x7a,42[I2C_IDX_TX_P2] = 0x72,43[I2C_IDX_RX_P0] = 0x7e,44[I2C_IDX_RX_P1] = 0x80,45};4647static const u8 anx781x_i2c_addresses[] = {48[I2C_IDX_TX_P0] = 0x70,49[I2C_IDX_TX_P1] = 0x7a,50[I2C_IDX_TX_P2] = 0x72,51[I2C_IDX_RX_P0] = 0x7e,52[I2C_IDX_RX_P1] = 0x80,53};5455struct anx78xx_platform_data {56struct regulator *dvdd10;57struct gpio_desc *gpiod_hpd;58struct gpio_desc *gpiod_pd;59struct gpio_desc *gpiod_reset;6061int hpd_irq;62int intp_irq;63};6465struct anx78xx {66struct drm_dp_aux aux;67struct drm_bridge bridge;68struct i2c_client *client;69const struct drm_edid *drm_edid;70struct drm_connector connector;71struct anx78xx_platform_data pdata;72struct mutex lock;7374/*75* I2C Slave addresses of ANX7814 are mapped as TX_P0, TX_P1, TX_P2,76* RX_P0 and RX_P1.77*/78struct i2c_client *i2c_dummy[I2C_NUM_ADDRESSES];79struct regmap *map[I2C_NUM_ADDRESSES];8081u16 chipid;82u8 dpcd[DP_RECEIVER_CAP_SIZE];8384bool powered;85};8687static inline struct anx78xx *connector_to_anx78xx(struct drm_connector *c)88{89return container_of(c, struct anx78xx, connector);90}9192static inline struct anx78xx *bridge_to_anx78xx(struct drm_bridge *bridge)93{94return container_of(bridge, struct anx78xx, bridge);95}9697static int anx78xx_set_bits(struct regmap *map, u8 reg, u8 mask)98{99return regmap_update_bits(map, reg, mask, mask);100}101102static int anx78xx_clear_bits(struct regmap *map, u8 reg, u8 mask)103{104return regmap_update_bits(map, reg, mask, 0);105}106107static ssize_t anx78xx_aux_transfer(struct drm_dp_aux *aux,108struct drm_dp_aux_msg *msg)109{110struct anx78xx *anx78xx = container_of(aux, struct anx78xx, aux);111return anx_dp_aux_transfer(anx78xx->map[I2C_IDX_TX_P0], msg);112}113114static int anx78xx_set_hpd(struct anx78xx *anx78xx)115{116int err;117118err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_RX_P0],119SP_TMDS_CTRL_BASE + 7, SP_PD_RT);120if (err)121return err;122123err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL3_REG,124SP_HPD_OUT);125if (err)126return err;127128return 0;129}130131static int anx78xx_clear_hpd(struct anx78xx *anx78xx)132{133int err;134135err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL3_REG,136SP_HPD_OUT);137if (err)138return err;139140err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],141SP_TMDS_CTRL_BASE + 7, SP_PD_RT);142if (err)143return err;144145return 0;146}147148static const struct reg_sequence tmds_phy_initialization[] = {149{ SP_TMDS_CTRL_BASE + 1, 0x90 },150{ SP_TMDS_CTRL_BASE + 2, 0xa9 },151{ SP_TMDS_CTRL_BASE + 6, 0x92 },152{ SP_TMDS_CTRL_BASE + 7, 0x80 },153{ SP_TMDS_CTRL_BASE + 20, 0xf2 },154{ SP_TMDS_CTRL_BASE + 22, 0xc4 },155{ SP_TMDS_CTRL_BASE + 23, 0x18 },156};157158static int anx78xx_rx_initialization(struct anx78xx *anx78xx)159{160int err;161162err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_HDMI_MUTE_CTRL_REG,163SP_AUD_MUTE | SP_VID_MUTE);164if (err)165return err;166167err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0], SP_CHIP_CTRL_REG,168SP_MAN_HDMI5V_DET | SP_PLLLOCK_CKDT_EN |169SP_DIGITAL_CKDT_EN);170if (err)171return err;172173err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],174SP_SOFTWARE_RESET1_REG, SP_HDCP_MAN_RST |175SP_SW_MAN_RST | SP_TMDS_RST | SP_VIDEO_RST);176if (err)177return err;178179err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_RX_P0],180SP_SOFTWARE_RESET1_REG, SP_HDCP_MAN_RST |181SP_SW_MAN_RST | SP_TMDS_RST | SP_VIDEO_RST);182if (err)183return err;184185/* Sync detect change, GP set mute */186err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],187SP_AUD_EXCEPTION_ENABLE_BASE + 1, BIT(5) |188BIT(6));189if (err)190return err;191192err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],193SP_AUD_EXCEPTION_ENABLE_BASE + 3,194SP_AEC_EN21);195if (err)196return err;197198err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0], SP_AUDVID_CTRL_REG,199SP_AVC_EN | SP_AAC_OE | SP_AAC_EN);200if (err)201return err;202203err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_RX_P0],204SP_SYSTEM_POWER_DOWN1_REG, SP_PWDN_CTRL);205if (err)206return err;207208err = anx78xx_set_bits(anx78xx->map[I2C_IDX_RX_P0],209SP_VID_DATA_RANGE_CTRL_REG, SP_R2Y_INPUT_LIMIT);210if (err)211return err;212213/* Enable DDC stretch */214err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],215SP_DP_EXTRA_I2C_DEV_ADDR_REG, SP_I2C_EXTRA_ADDR);216if (err)217return err;218219/* TMDS phy initialization */220err = regmap_multi_reg_write(anx78xx->map[I2C_IDX_RX_P0],221tmds_phy_initialization,222ARRAY_SIZE(tmds_phy_initialization));223if (err)224return err;225226err = anx78xx_clear_hpd(anx78xx);227if (err)228return err;229230return 0;231}232233static const u8 dp_tx_output_precise_tune_bits[20] = {2340x01, 0x03, 0x07, 0x7f, 0x71, 0x6b, 0x7f,2350x73, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x00,2360x0c, 0x42, 0x1e, 0x3e, 0x72, 0x7e,237};238239static int anx78xx_link_phy_initialization(struct anx78xx *anx78xx)240{241int err;242243/*244* REVISIT : It is writing to a RESERVED bits in Analog Control 0245* register.246*/247err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_ANALOG_CTRL0_REG,2480x02);249if (err)250return err;251252/*253* Write DP TX output emphasis precise tune bits.254*/255err = regmap_bulk_write(anx78xx->map[I2C_IDX_TX_P1],256SP_DP_TX_LT_CTRL0_REG,257dp_tx_output_precise_tune_bits,258ARRAY_SIZE(dp_tx_output_precise_tune_bits));259260if (err)261return err;262263return 0;264}265266static int anx78xx_xtal_clk_sel(struct anx78xx *anx78xx)267{268unsigned int value;269int err;270271err = regmap_update_bits(anx78xx->map[I2C_IDX_TX_P2],272SP_ANALOG_DEBUG2_REG,273SP_XTAL_FRQ | SP_FORCE_SW_OFF_BYPASS,274SP_XTAL_FRQ_27M);275if (err)276return err;277278err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL3_REG,279XTAL_CLK & SP_WAIT_COUNTER_7_0_MASK);280if (err)281return err;282283err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL4_REG,284((XTAL_CLK & 0xff00) >> 2) | (XTAL_CLK / 10));285if (err)286return err;287288err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],289SP_I2C_GEN_10US_TIMER0_REG, XTAL_CLK & 0xff);290if (err)291return err;292293err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],294SP_I2C_GEN_10US_TIMER1_REG,295(XTAL_CLK & 0xff00) >> 8);296if (err)297return err;298299err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_MISC_CTRL_REG,300XTAL_CLK / 10 - 1);301if (err)302return err;303304err = regmap_read(anx78xx->map[I2C_IDX_RX_P0],305SP_HDMI_US_TIMER_CTRL_REG,306&value);307if (err)308return err;309310err = regmap_write(anx78xx->map[I2C_IDX_RX_P0],311SP_HDMI_US_TIMER_CTRL_REG,312(value & SP_MS_TIMER_MARGIN_10_8_MASK) |313((((XTAL_CLK / 10) >> 1) - 2) << 3));314if (err)315return err;316317return 0;318}319320static const struct reg_sequence otp_key_protect[] = {321{ SP_OTP_KEY_PROTECT1_REG, SP_OTP_PSW1 },322{ SP_OTP_KEY_PROTECT2_REG, SP_OTP_PSW2 },323{ SP_OTP_KEY_PROTECT3_REG, SP_OTP_PSW3 },324};325326static int anx78xx_tx_initialization(struct anx78xx *anx78xx)327{328int err;329330/* Set terminal resistor to 50 ohm */331err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_AUX_CH_CTRL2_REG,3320x30);333if (err)334return err;335336/* Enable aux double diff output */337err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],338SP_DP_AUX_CH_CTRL2_REG, 0x08);339if (err)340return err;341342err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],343SP_DP_HDCP_CTRL_REG, SP_AUTO_EN |344SP_AUTO_START);345if (err)346return err;347348err = regmap_multi_reg_write(anx78xx->map[I2C_IDX_TX_P0],349otp_key_protect,350ARRAY_SIZE(otp_key_protect));351if (err)352return err;353354err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],355SP_HDCP_KEY_COMMAND_REG, SP_DISABLE_SYNC_HDCP);356if (err)357return err;358359err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL8_REG,360SP_VID_VRES_TH);361if (err)362return err;363364/*365* DP HDCP auto authentication wait timer (when downstream starts to366* auth, DP side will wait for this period then do auth automatically)367*/368err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_HDCP_AUTO_TIMER_REG,3690x00);370if (err)371return err;372373err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],374SP_DP_HDCP_CTRL_REG, SP_LINK_POLLING);375if (err)376return err;377378err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],379SP_DP_LINK_DEBUG_CTRL_REG, SP_M_VID_DEBUG);380if (err)381return err;382383err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2],384SP_ANALOG_DEBUG2_REG, SP_POWERON_TIME_1P5MS);385if (err)386return err;387388err = anx78xx_xtal_clk_sel(anx78xx);389if (err)390return err;391392err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_AUX_DEFER_CTRL_REG,393SP_DEFER_CTRL_EN | 0x0c);394if (err)395return err;396397err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],398SP_DP_POLLING_CTRL_REG,399SP_AUTO_POLLING_DISABLE);400if (err)401return err;402403/*404* Short the link integrity check timer to speed up bstatus405* polling for HDCP CTS item 1A-07406*/407err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],408SP_HDCP_LINK_CHECK_TIMER_REG, 0x1d);409if (err)410return err;411412err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],413SP_DP_MISC_CTRL_REG, SP_EQ_TRAINING_LOOP);414if (err)415return err;416417/* Power down the main link by default */418err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],419SP_DP_ANALOG_POWER_DOWN_REG, SP_CH0_PD);420if (err)421return err;422423err = anx78xx_link_phy_initialization(anx78xx);424if (err)425return err;426427/* Gen m_clk with downspreading */428err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],429SP_DP_M_CALCULATION_CTRL_REG, SP_M_GEN_CLK_SEL);430if (err)431return err;432433return 0;434}435436static int anx78xx_enable_interrupts(struct anx78xx *anx78xx)437{438int err;439440/*441* BIT0: INT pin assertion polarity: 1 = assert high442* BIT1: INT pin output type: 0 = push/pull443*/444err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_INT_CTRL_REG, 0x01);445if (err)446return err;447448err = regmap_write(anx78xx->map[I2C_IDX_TX_P2],449SP_COMMON_INT_MASK4_REG, SP_HPD_LOST | SP_HPD_PLUG);450if (err)451return err;452453err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_MASK1_REG,454SP_TRAINING_FINISH);455if (err)456return err;457458err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_INT_MASK1_REG,459SP_CKDT_CHG | SP_SCDT_CHG);460if (err)461return err;462463return 0;464}465466static void anx78xx_poweron(struct anx78xx *anx78xx)467{468struct anx78xx_platform_data *pdata = &anx78xx->pdata;469int err;470471if (WARN_ON(anx78xx->powered))472return;473474if (pdata->dvdd10) {475err = regulator_enable(pdata->dvdd10);476if (err) {477DRM_ERROR("Failed to enable DVDD10 regulator: %d\n",478err);479return;480}481482usleep_range(1000, 2000);483}484485gpiod_set_value_cansleep(pdata->gpiod_reset, 1);486usleep_range(1000, 2000);487488gpiod_set_value_cansleep(pdata->gpiod_pd, 0);489usleep_range(1000, 2000);490491gpiod_set_value_cansleep(pdata->gpiod_reset, 0);492493/* Power on registers module */494anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,495SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);496anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,497SP_REGISTER_PD | SP_TOTAL_PD);498499anx78xx->powered = true;500}501502static void anx78xx_poweroff(struct anx78xx *anx78xx)503{504struct anx78xx_platform_data *pdata = &anx78xx->pdata;505int err;506507if (WARN_ON(!anx78xx->powered))508return;509510gpiod_set_value_cansleep(pdata->gpiod_reset, 1);511usleep_range(1000, 2000);512513gpiod_set_value_cansleep(pdata->gpiod_pd, 1);514usleep_range(1000, 2000);515516if (pdata->dvdd10) {517err = regulator_disable(pdata->dvdd10);518if (err) {519DRM_ERROR("Failed to disable DVDD10 regulator: %d\n",520err);521return;522}523524usleep_range(1000, 2000);525}526527anx78xx->powered = false;528}529530static int anx78xx_start(struct anx78xx *anx78xx)531{532int err;533534/* Power on all modules */535err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2],536SP_POWERDOWN_CTRL_REG,537SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD |538SP_LINK_PD);539540err = anx78xx_enable_interrupts(anx78xx);541if (err) {542DRM_ERROR("Failed to enable interrupts: %d\n", err);543goto err_poweroff;544}545546err = anx78xx_rx_initialization(anx78xx);547if (err) {548DRM_ERROR("Failed receiver initialization: %d\n", err);549goto err_poweroff;550}551552err = anx78xx_tx_initialization(anx78xx);553if (err) {554DRM_ERROR("Failed transmitter initialization: %d\n", err);555goto err_poweroff;556}557558/*559* This delay seems to help keep the hardware in a good state. Without560* it, there are times where it fails silently.561*/562usleep_range(10000, 15000);563564return 0;565566err_poweroff:567DRM_ERROR("Failed SlimPort transmitter initialization: %d\n", err);568anx78xx_poweroff(anx78xx);569570return err;571}572573static int anx78xx_init_pdata(struct anx78xx *anx78xx)574{575struct anx78xx_platform_data *pdata = &anx78xx->pdata;576struct device *dev = &anx78xx->client->dev;577578/* 1.0V digital core power regulator */579pdata->dvdd10 = devm_regulator_get(dev, "dvdd10");580if (IS_ERR(pdata->dvdd10)) {581if (PTR_ERR(pdata->dvdd10) != -EPROBE_DEFER)582DRM_ERROR("DVDD10 regulator not found\n");583584return PTR_ERR(pdata->dvdd10);585}586587/* GPIO for HPD */588pdata->gpiod_hpd = devm_gpiod_get(dev, "hpd", GPIOD_IN);589if (IS_ERR(pdata->gpiod_hpd))590return PTR_ERR(pdata->gpiod_hpd);591592/* GPIO for chip power down */593pdata->gpiod_pd = devm_gpiod_get(dev, "pd", GPIOD_OUT_HIGH);594if (IS_ERR(pdata->gpiod_pd))595return PTR_ERR(pdata->gpiod_pd);596597/* GPIO for chip reset */598pdata->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);599600return PTR_ERR_OR_ZERO(pdata->gpiod_reset);601}602603static int anx78xx_dp_link_training(struct anx78xx *anx78xx)604{605u8 dp_bw, dpcd[2];606int err;607608err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_HDMI_MUTE_CTRL_REG,6090x0);610if (err)611return err;612613err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2],614SP_POWERDOWN_CTRL_REG,615SP_TOTAL_PD);616if (err)617return err;618619err = drm_dp_dpcd_readb(&anx78xx->aux, DP_MAX_LINK_RATE, &dp_bw);620if (err < 0)621return err;622623switch (dp_bw) {624case DP_LINK_BW_1_62:625case DP_LINK_BW_2_7:626case DP_LINK_BW_5_4:627break;628629default:630DRM_DEBUG_KMS("DP bandwidth (%#02x) not supported\n", dp_bw);631return -EINVAL;632}633634err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL1_REG,635SP_VIDEO_MUTE);636if (err)637return err;638639err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2],640SP_VID_CTRL1_REG, SP_VIDEO_EN);641if (err)642return err;643644/* Get DPCD info */645err = drm_dp_dpcd_read(&anx78xx->aux, DP_DPCD_REV,646&anx78xx->dpcd, DP_RECEIVER_CAP_SIZE);647if (err < 0) {648DRM_ERROR("Failed to read DPCD: %d\n", err);649return err;650}651652/* Clear channel x SERDES power down */653err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],654SP_DP_ANALOG_POWER_DOWN_REG, SP_CH0_PD);655if (err)656return err;657658drm_dp_link_power_up(&anx78xx->aux, anx78xx->dpcd[DP_DPCD_REV]);659660/* Possibly enable downspread on the sink */661err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],662SP_DP_DOWNSPREAD_CTRL1_REG, 0);663if (err)664return err;665666if (anx78xx->dpcd[DP_MAX_DOWNSPREAD] & DP_MAX_DOWNSPREAD_0_5) {667DRM_DEBUG("Enable downspread on the sink\n");668/* 4000PPM */669err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],670SP_DP_DOWNSPREAD_CTRL1_REG, 8);671if (err)672return err;673674err = drm_dp_dpcd_writeb(&anx78xx->aux, DP_DOWNSPREAD_CTRL,675DP_SPREAD_AMP_0_5);676if (err < 0)677return err;678} else {679err = drm_dp_dpcd_writeb(&anx78xx->aux, DP_DOWNSPREAD_CTRL, 0);680if (err < 0)681return err;682}683684/* Set the lane count and the link rate on the sink */685if (drm_dp_enhanced_frame_cap(anx78xx->dpcd))686err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],687SP_DP_SYSTEM_CTRL_BASE + 4,688SP_ENHANCED_MODE);689else690err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],691SP_DP_SYSTEM_CTRL_BASE + 4,692SP_ENHANCED_MODE);693if (err)694return err;695696err = regmap_write(anx78xx->map[I2C_IDX_TX_P0],697SP_DP_MAIN_LINK_BW_SET_REG,698anx78xx->dpcd[DP_MAX_LINK_RATE]);699if (err)700return err;701702dpcd[1] = drm_dp_max_lane_count(anx78xx->dpcd);703704if (drm_dp_enhanced_frame_cap(anx78xx->dpcd))705dpcd[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;706707err = drm_dp_dpcd_write(&anx78xx->aux, DP_LINK_BW_SET, dpcd,708sizeof(dpcd));709if (err < 0) {710DRM_ERROR("Failed to configure link: %d\n", err);711return err;712}713714/* Start training on the source */715err = regmap_write(anx78xx->map[I2C_IDX_TX_P0], SP_DP_LT_CTRL_REG,716SP_LT_EN);717if (err)718return err;719720return 0;721}722723static int anx78xx_config_dp_output(struct anx78xx *anx78xx)724{725int err;726727err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL1_REG,728SP_VIDEO_MUTE);729if (err)730return err;731732/* Enable DP output */733err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_VID_CTRL1_REG,734SP_VIDEO_EN);735if (err)736return err;737738return 0;739}740741static int anx78xx_send_video_infoframe(struct anx78xx *anx78xx,742struct hdmi_avi_infoframe *frame)743{744u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];745int err;746747err = hdmi_avi_infoframe_pack(frame, buffer, sizeof(buffer));748if (err < 0) {749DRM_ERROR("Failed to pack AVI infoframe: %d\n", err);750return err;751}752753err = anx78xx_clear_bits(anx78xx->map[I2C_IDX_TX_P0],754SP_PACKET_SEND_CTRL_REG, SP_AVI_IF_EN);755if (err)756return err;757758err = regmap_bulk_write(anx78xx->map[I2C_IDX_TX_P2],759SP_INFOFRAME_AVI_DB1_REG, buffer,760frame->length);761if (err)762return err;763764err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],765SP_PACKET_SEND_CTRL_REG, SP_AVI_IF_UD);766if (err)767return err;768769err = anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P0],770SP_PACKET_SEND_CTRL_REG, SP_AVI_IF_EN);771if (err)772return err;773774return 0;775}776777static int anx78xx_get_downstream_info(struct anx78xx *anx78xx)778{779u8 value;780int err;781782err = drm_dp_dpcd_readb(&anx78xx->aux, DP_SINK_COUNT, &value);783if (err < 0) {784DRM_ERROR("Get sink count failed %d\n", err);785return err;786}787788if (!DP_GET_SINK_COUNT(value)) {789DRM_ERROR("Downstream disconnected\n");790return -EIO;791}792793return 0;794}795796static int anx78xx_get_modes(struct drm_connector *connector)797{798struct anx78xx *anx78xx = connector_to_anx78xx(connector);799int err, num_modes = 0;800801if (WARN_ON(!anx78xx->powered))802return 0;803804if (anx78xx->drm_edid)805return drm_edid_connector_add_modes(connector);806807mutex_lock(&anx78xx->lock);808809err = anx78xx_get_downstream_info(anx78xx);810if (err) {811DRM_ERROR("Failed to get downstream info: %d\n", err);812goto unlock;813}814815anx78xx->drm_edid = drm_edid_read_ddc(connector, &anx78xx->aux.ddc);816817err = drm_edid_connector_update(connector, anx78xx->drm_edid);818819if (!anx78xx->drm_edid) {820DRM_ERROR("Failed to read EDID\n");821goto unlock;822}823824if (err) {825DRM_ERROR("Failed to update EDID property: %d\n", err);826goto unlock;827}828829num_modes = drm_edid_connector_add_modes(connector);830831unlock:832mutex_unlock(&anx78xx->lock);833834return num_modes;835}836837static const struct drm_connector_helper_funcs anx78xx_connector_helper_funcs = {838.get_modes = anx78xx_get_modes,839};840841static enum drm_connector_status anx78xx_detect(struct drm_connector *connector,842bool force)843{844struct anx78xx *anx78xx = connector_to_anx78xx(connector);845846if (!gpiod_get_value(anx78xx->pdata.gpiod_hpd))847return connector_status_disconnected;848849return connector_status_connected;850}851852static const struct drm_connector_funcs anx78xx_connector_funcs = {853.fill_modes = drm_helper_probe_single_connector_modes,854.detect = anx78xx_detect,855.destroy = drm_connector_cleanup,856.reset = drm_atomic_helper_connector_reset,857.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,858.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,859};860861static int anx78xx_bridge_attach(struct drm_bridge *bridge,862struct drm_encoder *encoder,863enum drm_bridge_attach_flags flags)864{865struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);866int err;867868if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) {869DRM_ERROR("Fix bridge driver to make connector optional!");870return -EINVAL;871}872873/* Register aux channel */874anx78xx->aux.name = "DP-AUX";875anx78xx->aux.dev = &anx78xx->client->dev;876anx78xx->aux.drm_dev = bridge->dev;877anx78xx->aux.transfer = anx78xx_aux_transfer;878879err = drm_dp_aux_register(&anx78xx->aux);880if (err < 0) {881DRM_ERROR("Failed to register aux channel: %d\n", err);882return err;883}884885err = drm_connector_init(bridge->dev, &anx78xx->connector,886&anx78xx_connector_funcs,887DRM_MODE_CONNECTOR_DisplayPort);888if (err) {889DRM_ERROR("Failed to initialize connector: %d\n", err);890goto aux_unregister;891}892893drm_connector_helper_add(&anx78xx->connector,894&anx78xx_connector_helper_funcs);895896anx78xx->connector.polled = DRM_CONNECTOR_POLL_HPD;897898err = drm_connector_attach_encoder(&anx78xx->connector,899encoder);900if (err) {901DRM_ERROR("Failed to link up connector to encoder: %d\n", err);902goto connector_cleanup;903}904905err = drm_connector_register(&anx78xx->connector);906if (err) {907DRM_ERROR("Failed to register connector: %d\n", err);908goto connector_cleanup;909}910911return 0;912connector_cleanup:913drm_connector_cleanup(&anx78xx->connector);914aux_unregister:915drm_dp_aux_unregister(&anx78xx->aux);916return err;917}918919static void anx78xx_bridge_detach(struct drm_bridge *bridge)920{921drm_dp_aux_unregister(&bridge_to_anx78xx(bridge)->aux);922}923924static enum drm_mode_status925anx78xx_bridge_mode_valid(struct drm_bridge *bridge,926const struct drm_display_info *info,927const struct drm_display_mode *mode)928{929if (mode->flags & DRM_MODE_FLAG_INTERLACE)930return MODE_NO_INTERLACE;931932/* Max 1200p at 5.4 Ghz, one lane */933if (mode->clock > 154000)934return MODE_CLOCK_HIGH;935936return MODE_OK;937}938939static void anx78xx_bridge_disable(struct drm_bridge *bridge)940{941struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);942943/* Power off all modules except configuration registers access */944anx78xx_set_bits(anx78xx->map[I2C_IDX_TX_P2], SP_POWERDOWN_CTRL_REG,945SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);946}947948static void anx78xx_bridge_mode_set(struct drm_bridge *bridge,949const struct drm_display_mode *mode,950const struct drm_display_mode *adjusted_mode)951{952struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);953struct hdmi_avi_infoframe frame;954int err;955956if (WARN_ON(!anx78xx->powered))957return;958959mutex_lock(&anx78xx->lock);960961err = drm_hdmi_avi_infoframe_from_display_mode(&frame,962&anx78xx->connector,963adjusted_mode);964if (err) {965DRM_ERROR("Failed to setup AVI infoframe: %d\n", err);966goto unlock;967}968969err = anx78xx_send_video_infoframe(anx78xx, &frame);970if (err)971DRM_ERROR("Failed to send AVI infoframe: %d\n", err);972973unlock:974mutex_unlock(&anx78xx->lock);975}976977static void anx78xx_bridge_enable(struct drm_bridge *bridge)978{979struct anx78xx *anx78xx = bridge_to_anx78xx(bridge);980int err;981982err = anx78xx_start(anx78xx);983if (err) {984DRM_ERROR("Failed to initialize: %d\n", err);985return;986}987988err = anx78xx_set_hpd(anx78xx);989if (err)990DRM_ERROR("Failed to set HPD: %d\n", err);991}992993static const struct drm_bridge_funcs anx78xx_bridge_funcs = {994.attach = anx78xx_bridge_attach,995.detach = anx78xx_bridge_detach,996.mode_valid = anx78xx_bridge_mode_valid,997.disable = anx78xx_bridge_disable,998.mode_set = anx78xx_bridge_mode_set,999.enable = anx78xx_bridge_enable,1000};10011002static irqreturn_t anx78xx_hpd_threaded_handler(int irq, void *data)1003{1004struct anx78xx *anx78xx = data;1005int err;10061007if (anx78xx->powered)1008return IRQ_HANDLED;10091010mutex_lock(&anx78xx->lock);10111012/* Cable is pulled, power on the chip */1013anx78xx_poweron(anx78xx);10141015err = anx78xx_enable_interrupts(anx78xx);1016if (err)1017DRM_ERROR("Failed to enable interrupts: %d\n", err);10181019mutex_unlock(&anx78xx->lock);10201021return IRQ_HANDLED;1022}10231024static int anx78xx_handle_dp_int_1(struct anx78xx *anx78xx, u8 irq)1025{1026int err;10271028DRM_DEBUG_KMS("Handle DP interrupt 1: %02x\n", irq);10291030err = regmap_write(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_STATUS1_REG,1031irq);1032if (err)1033return err;10341035if (irq & SP_TRAINING_FINISH) {1036DRM_DEBUG_KMS("IRQ: hardware link training finished\n");1037err = anx78xx_config_dp_output(anx78xx);1038}10391040return err;1041}10421043static bool anx78xx_handle_common_int_4(struct anx78xx *anx78xx, u8 irq)1044{1045bool event = false;1046int err;10471048DRM_DEBUG_KMS("Handle common interrupt 4: %02x\n", irq);10491050err = regmap_write(anx78xx->map[I2C_IDX_TX_P2],1051SP_COMMON_INT_STATUS4_REG, irq);1052if (err) {1053DRM_ERROR("Failed to write SP_COMMON_INT_STATUS4 %d\n", err);1054return event;1055}10561057if (irq & SP_HPD_LOST) {1058DRM_DEBUG_KMS("IRQ: Hot plug detect - cable is pulled out\n");1059event = true;1060anx78xx_poweroff(anx78xx);1061/* Free cached EDID */1062drm_edid_free(anx78xx->drm_edid);1063anx78xx->drm_edid = NULL;1064} else if (irq & SP_HPD_PLUG) {1065DRM_DEBUG_KMS("IRQ: Hot plug detect - cable plug\n");1066event = true;1067}10681069return event;1070}10711072static void anx78xx_handle_hdmi_int_1(struct anx78xx *anx78xx, u8 irq)1073{1074unsigned int value;1075int err;10761077DRM_DEBUG_KMS("Handle HDMI interrupt 1: %02x\n", irq);10781079err = regmap_write(anx78xx->map[I2C_IDX_RX_P0], SP_INT_STATUS1_REG,1080irq);1081if (err) {1082DRM_ERROR("Write HDMI int 1 failed: %d\n", err);1083return;1084}10851086if ((irq & SP_CKDT_CHG) || (irq & SP_SCDT_CHG)) {1087DRM_DEBUG_KMS("IRQ: HDMI input detected\n");10881089err = regmap_read(anx78xx->map[I2C_IDX_RX_P0],1090SP_SYSTEM_STATUS_REG, &value);1091if (err) {1092DRM_ERROR("Read system status reg failed: %d\n", err);1093return;1094}10951096if (!(value & SP_TMDS_CLOCK_DET)) {1097DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI clock ***\n");1098return;1099}11001101if (!(value & SP_TMDS_DE_DET)) {1102DRM_DEBUG_KMS("IRQ: *** Waiting for HDMI signal ***\n");1103return;1104}11051106err = anx78xx_dp_link_training(anx78xx);1107if (err)1108DRM_ERROR("Failed to start link training: %d\n", err);1109}1110}11111112static irqreturn_t anx78xx_intp_threaded_handler(int unused, void *data)1113{1114struct anx78xx *anx78xx = data;1115bool event = false;1116unsigned int irq;1117int err;11181119mutex_lock(&anx78xx->lock);11201121err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DP_INT_STATUS1_REG,1122&irq);1123if (err) {1124DRM_ERROR("Failed to read DP interrupt 1 status: %d\n", err);1125goto unlock;1126}11271128if (irq)1129anx78xx_handle_dp_int_1(anx78xx, irq);11301131err = regmap_read(anx78xx->map[I2C_IDX_TX_P2],1132SP_COMMON_INT_STATUS4_REG, &irq);1133if (err) {1134DRM_ERROR("Failed to read common interrupt 4 status: %d\n",1135err);1136goto unlock;1137}11381139if (irq)1140event = anx78xx_handle_common_int_4(anx78xx, irq);11411142/* Make sure we are still powered after handle HPD events */1143if (!anx78xx->powered)1144goto unlock;11451146err = regmap_read(anx78xx->map[I2C_IDX_RX_P0], SP_INT_STATUS1_REG,1147&irq);1148if (err) {1149DRM_ERROR("Failed to read HDMI int 1 status: %d\n", err);1150goto unlock;1151}11521153if (irq)1154anx78xx_handle_hdmi_int_1(anx78xx, irq);11551156unlock:1157mutex_unlock(&anx78xx->lock);11581159if (event)1160drm_helper_hpd_irq_event(anx78xx->connector.dev);11611162return IRQ_HANDLED;1163}11641165static void unregister_i2c_dummy_clients(struct anx78xx *anx78xx)1166{1167unsigned int i;11681169for (i = 0; i < ARRAY_SIZE(anx78xx->i2c_dummy); i++)1170i2c_unregister_device(anx78xx->i2c_dummy[i]);1171}11721173static const struct regmap_config anx78xx_regmap_config = {1174.reg_bits = 8,1175.val_bits = 8,1176};11771178static const u16 anx78xx_chipid_list[] = {11790x7808,11800x7812,11810x7814,11820x7816,11830x7818,1184};11851186static int anx78xx_i2c_probe(struct i2c_client *client)1187{1188struct anx78xx *anx78xx;1189struct anx78xx_platform_data *pdata;1190unsigned int i, idl, idh, version;1191const u8 *i2c_addresses;1192bool found = false;1193int err;11941195anx78xx = devm_drm_bridge_alloc(&client->dev, struct anx78xx, bridge,1196&anx78xx_bridge_funcs);1197if (IS_ERR(anx78xx))1198return PTR_ERR(anx78xx);11991200pdata = &anx78xx->pdata;12011202mutex_init(&anx78xx->lock);12031204anx78xx->bridge.of_node = client->dev.of_node;12051206anx78xx->client = client;1207i2c_set_clientdata(client, anx78xx);12081209err = anx78xx_init_pdata(anx78xx);1210if (err) {1211if (err != -EPROBE_DEFER)1212DRM_ERROR("Failed to initialize pdata: %d\n", err);12131214return err;1215}12161217pdata->hpd_irq = gpiod_to_irq(pdata->gpiod_hpd);1218if (pdata->hpd_irq < 0) {1219DRM_ERROR("Failed to get HPD IRQ: %d\n", pdata->hpd_irq);1220return -ENODEV;1221}12221223pdata->intp_irq = client->irq;1224if (!pdata->intp_irq) {1225DRM_ERROR("Failed to get CABLE_DET and INTP IRQ\n");1226return -ENODEV;1227}12281229/* Map slave addresses of ANX7814 */1230i2c_addresses = device_get_match_data(&client->dev);1231for (i = 0; i < I2C_NUM_ADDRESSES; i++) {1232struct i2c_client *i2c_dummy;12331234i2c_dummy = i2c_new_dummy_device(client->adapter,1235i2c_addresses[i] >> 1);1236if (IS_ERR(i2c_dummy)) {1237err = PTR_ERR(i2c_dummy);1238DRM_ERROR("Failed to reserve I2C bus %02x: %d\n",1239i2c_addresses[i], err);1240goto err_unregister_i2c;1241}12421243anx78xx->i2c_dummy[i] = i2c_dummy;1244anx78xx->map[i] = devm_regmap_init_i2c(anx78xx->i2c_dummy[i],1245&anx78xx_regmap_config);1246if (IS_ERR(anx78xx->map[i])) {1247err = PTR_ERR(anx78xx->map[i]);1248DRM_ERROR("Failed regmap initialization %02x\n",1249i2c_addresses[i]);1250goto err_unregister_i2c;1251}1252}12531254/* Look for supported chip ID */1255anx78xx_poweron(anx78xx);12561257err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_IDL_REG,1258&idl);1259if (err)1260goto err_poweroff;12611262err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_IDH_REG,1263&idh);1264if (err)1265goto err_poweroff;12661267anx78xx->chipid = (u8)idl | ((u8)idh << 8);12681269err = regmap_read(anx78xx->map[I2C_IDX_TX_P2], SP_DEVICE_VERSION_REG,1270&version);1271if (err)1272goto err_poweroff;12731274for (i = 0; i < ARRAY_SIZE(anx78xx_chipid_list); i++) {1275if (anx78xx->chipid == anx78xx_chipid_list[i]) {1276DRM_INFO("Found ANX%x (ver. %d) SlimPort Transmitter\n",1277anx78xx->chipid, version);1278found = true;1279break;1280}1281}12821283if (!found) {1284DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",1285anx78xx->chipid, version);1286err = -ENODEV;1287goto err_poweroff;1288}12891290err = devm_request_threaded_irq(&client->dev, pdata->hpd_irq, NULL,1291anx78xx_hpd_threaded_handler,1292IRQF_TRIGGER_RISING | IRQF_ONESHOT,1293"anx78xx-hpd", anx78xx);1294if (err) {1295DRM_ERROR("Failed to request CABLE_DET threaded IRQ: %d\n",1296err);1297goto err_poweroff;1298}12991300err = devm_request_threaded_irq(&client->dev, pdata->intp_irq, NULL,1301anx78xx_intp_threaded_handler,1302IRQF_TRIGGER_RISING | IRQF_ONESHOT,1303"anx78xx-intp", anx78xx);1304if (err) {1305DRM_ERROR("Failed to request INTP threaded IRQ: %d\n", err);1306goto err_poweroff;1307}13081309drm_bridge_add(&anx78xx->bridge);13101311/* If cable is pulled out, just poweroff and wait for HPD event */1312if (!gpiod_get_value(anx78xx->pdata.gpiod_hpd))1313anx78xx_poweroff(anx78xx);13141315return 0;13161317err_poweroff:1318anx78xx_poweroff(anx78xx);13191320err_unregister_i2c:1321unregister_i2c_dummy_clients(anx78xx);1322return err;1323}13241325static void anx78xx_i2c_remove(struct i2c_client *client)1326{1327struct anx78xx *anx78xx = i2c_get_clientdata(client);13281329drm_bridge_remove(&anx78xx->bridge);13301331unregister_i2c_dummy_clients(anx78xx);13321333drm_edid_free(anx78xx->drm_edid);1334}13351336static const struct of_device_id anx78xx_match_table[] = {1337{ .compatible = "analogix,anx7808", .data = anx7808_i2c_addresses },1338{ .compatible = "analogix,anx7812", .data = anx781x_i2c_addresses },1339{ .compatible = "analogix,anx7814", .data = anx781x_i2c_addresses },1340{ .compatible = "analogix,anx7816", .data = anx781x_i2c_addresses },1341{ .compatible = "analogix,anx7818", .data = anx781x_i2c_addresses },1342{ /* sentinel */ },1343};1344MODULE_DEVICE_TABLE(of, anx78xx_match_table);13451346static struct i2c_driver anx78xx_driver = {1347.driver = {1348.name = "anx7814",1349.of_match_table = anx78xx_match_table,1350},1351.probe = anx78xx_i2c_probe,1352.remove = anx78xx_i2c_remove,1353};1354module_i2c_driver(anx78xx_driver);13551356MODULE_DESCRIPTION("ANX78xx SlimPort Transmitter driver");1357MODULE_AUTHOR("Enric Balletbo i Serra <[email protected]>");1358MODULE_LICENSE("GPL v2");135913601361