Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright(c) 2016, Analogix Semiconductor.
0004  * Copyright(c) 2017, Icenowy Zheng <icenowy@aosc.io>
0005  *
0006  * Based on anx7808 driver obtained from chromeos with copyright:
0007  * Copyright(c) 2013, Google Inc.
0008  */
0009 #include <linux/delay.h>
0010 #include <linux/err.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/i2c.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/of_platform.h>
0017 #include <linux/regmap.h>
0018 #include <linux/regulator/consumer.h>
0019 #include <linux/types.h>
0020 
0021 #include <drm/display/drm_dp_helper.h>
0022 #include <drm/drm_atomic_helper.h>
0023 #include <drm/drm_bridge.h>
0024 #include <drm/drm_crtc.h>
0025 #include <drm/drm_crtc_helper.h>
0026 #include <drm/drm_edid.h>
0027 #include <drm/drm_of.h>
0028 #include <drm/drm_panel.h>
0029 #include <drm/drm_print.h>
0030 #include <drm/drm_probe_helper.h>
0031 
0032 #include "analogix-i2c-dptx.h"
0033 #include "analogix-i2c-txcommon.h"
0034 
0035 #define POLL_DELAY      50000 /* us */
0036 #define POLL_TIMEOUT        5000000 /* us */
0037 
0038 #define I2C_IDX_DPTX        0
0039 #define I2C_IDX_TXCOM       1
0040 
0041 static const u8 anx6345_i2c_addresses[] = {
0042     [I2C_IDX_DPTX]  = 0x70,
0043     [I2C_IDX_TXCOM] = 0x72,
0044 };
0045 #define I2C_NUM_ADDRESSES   ARRAY_SIZE(anx6345_i2c_addresses)
0046 
0047 struct anx6345 {
0048     struct drm_dp_aux aux;
0049     struct drm_bridge bridge;
0050     struct i2c_client *client;
0051     struct edid *edid;
0052     struct drm_connector connector;
0053     struct drm_panel *panel;
0054     struct regulator *dvdd12;
0055     struct regulator *dvdd25;
0056     struct gpio_desc *gpiod_reset;
0057     struct mutex lock;  /* protect EDID access */
0058 
0059     /* I2C Slave addresses of ANX6345 are mapped as DPTX and SYS */
0060     struct i2c_client *i2c_clients[I2C_NUM_ADDRESSES];
0061     struct regmap *map[I2C_NUM_ADDRESSES];
0062 
0063     u16 chipid;
0064     u8 dpcd[DP_RECEIVER_CAP_SIZE];
0065 
0066     bool powered;
0067 };
0068 
0069 static inline struct anx6345 *connector_to_anx6345(struct drm_connector *c)
0070 {
0071     return container_of(c, struct anx6345, connector);
0072 }
0073 
0074 static inline struct anx6345 *bridge_to_anx6345(struct drm_bridge *bridge)
0075 {
0076     return container_of(bridge, struct anx6345, bridge);
0077 }
0078 
0079 static int anx6345_set_bits(struct regmap *map, u8 reg, u8 mask)
0080 {
0081     return regmap_update_bits(map, reg, mask, mask);
0082 }
0083 
0084 static int anx6345_clear_bits(struct regmap *map, u8 reg, u8 mask)
0085 {
0086     return regmap_update_bits(map, reg, mask, 0);
0087 }
0088 
0089 static ssize_t anx6345_aux_transfer(struct drm_dp_aux *aux,
0090                     struct drm_dp_aux_msg *msg)
0091 {
0092     struct anx6345 *anx6345 = container_of(aux, struct anx6345, aux);
0093 
0094     return anx_dp_aux_transfer(anx6345->map[I2C_IDX_DPTX], msg);
0095 }
0096 
0097 static int anx6345_dp_link_training(struct anx6345 *anx6345)
0098 {
0099     unsigned int value;
0100     u8 dp_bw, dpcd[2];
0101     int err;
0102 
0103     err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
0104                  SP_POWERDOWN_CTRL_REG,
0105                  SP_TOTAL_PD);
0106     if (err)
0107         return err;
0108 
0109     err = drm_dp_dpcd_readb(&anx6345->aux, DP_MAX_LINK_RATE, &dp_bw);
0110     if (err < 0)
0111         return err;
0112 
0113     switch (dp_bw) {
0114     case DP_LINK_BW_1_62:
0115     case DP_LINK_BW_2_7:
0116         break;
0117 
0118     default:
0119         DRM_DEBUG_KMS("DP bandwidth (%#02x) not supported\n", dp_bw);
0120         return -EINVAL;
0121     }
0122 
0123     err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
0124                    SP_VIDEO_MUTE);
0125     if (err)
0126         return err;
0127 
0128     err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
0129                  SP_VID_CTRL1_REG, SP_VIDEO_EN);
0130     if (err)
0131         return err;
0132 
0133     /* Get DPCD info */
0134     err = drm_dp_dpcd_read(&anx6345->aux, DP_DPCD_REV,
0135                    &anx6345->dpcd, DP_RECEIVER_CAP_SIZE);
0136     if (err < 0) {
0137         DRM_ERROR("Failed to read DPCD: %d\n", err);
0138         return err;
0139     }
0140 
0141     /* Clear channel x SERDES power down */
0142     err = anx6345_clear_bits(anx6345->map[I2C_IDX_DPTX],
0143                  SP_DP_ANALOG_POWER_DOWN_REG, SP_CH0_PD);
0144     if (err)
0145         return err;
0146 
0147     /*
0148      * Power up the sink (DP_SET_POWER register is only available on DPCD
0149      * v1.1 and later).
0150      */
0151     if (anx6345->dpcd[DP_DPCD_REV] >= 0x11) {
0152         err = drm_dp_dpcd_readb(&anx6345->aux, DP_SET_POWER, &dpcd[0]);
0153         if (err < 0) {
0154             DRM_ERROR("Failed to read DP_SET_POWER register: %d\n",
0155                   err);
0156             return err;
0157         }
0158 
0159         dpcd[0] &= ~DP_SET_POWER_MASK;
0160         dpcd[0] |= DP_SET_POWER_D0;
0161 
0162         err = drm_dp_dpcd_writeb(&anx6345->aux, DP_SET_POWER, dpcd[0]);
0163         if (err < 0) {
0164             DRM_ERROR("Failed to power up DisplayPort link: %d\n",
0165                   err);
0166             return err;
0167         }
0168 
0169         /*
0170          * According to the DP 1.1 specification, a "Sink Device must
0171          * exit the power saving state within 1 ms" (Section 2.5.3.1,
0172          * Table 5-52, "Sink Control Field" (register 0x600).
0173          */
0174         usleep_range(1000, 2000);
0175     }
0176 
0177     /* Possibly enable downspread on the sink */
0178     err = regmap_write(anx6345->map[I2C_IDX_DPTX],
0179                SP_DP_DOWNSPREAD_CTRL1_REG, 0);
0180     if (err)
0181         return err;
0182 
0183     if (anx6345->dpcd[DP_MAX_DOWNSPREAD] & DP_MAX_DOWNSPREAD_0_5) {
0184         DRM_DEBUG("Enable downspread on the sink\n");
0185         /* 4000PPM */
0186         err = regmap_write(anx6345->map[I2C_IDX_DPTX],
0187                    SP_DP_DOWNSPREAD_CTRL1_REG, 8);
0188         if (err)
0189             return err;
0190 
0191         err = drm_dp_dpcd_writeb(&anx6345->aux, DP_DOWNSPREAD_CTRL,
0192                      DP_SPREAD_AMP_0_5);
0193         if (err < 0)
0194             return err;
0195     } else {
0196         err = drm_dp_dpcd_writeb(&anx6345->aux, DP_DOWNSPREAD_CTRL, 0);
0197         if (err < 0)
0198             return err;
0199     }
0200 
0201     /* Set the lane count and the link rate on the sink */
0202     if (drm_dp_enhanced_frame_cap(anx6345->dpcd))
0203         err = anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
0204                        SP_DP_SYSTEM_CTRL_BASE + 4,
0205                        SP_ENHANCED_MODE);
0206     else
0207         err = anx6345_clear_bits(anx6345->map[I2C_IDX_DPTX],
0208                      SP_DP_SYSTEM_CTRL_BASE + 4,
0209                      SP_ENHANCED_MODE);
0210     if (err)
0211         return err;
0212 
0213     dpcd[0] = dp_bw;
0214     err = regmap_write(anx6345->map[I2C_IDX_DPTX],
0215                SP_DP_MAIN_LINK_BW_SET_REG, dpcd[0]);
0216     if (err)
0217         return err;
0218 
0219     dpcd[1] = drm_dp_max_lane_count(anx6345->dpcd);
0220 
0221     err = regmap_write(anx6345->map[I2C_IDX_DPTX],
0222                SP_DP_LANE_COUNT_SET_REG, dpcd[1]);
0223     if (err)
0224         return err;
0225 
0226     if (drm_dp_enhanced_frame_cap(anx6345->dpcd))
0227         dpcd[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
0228 
0229     err = drm_dp_dpcd_write(&anx6345->aux, DP_LINK_BW_SET, dpcd,
0230                 sizeof(dpcd));
0231 
0232     if (err < 0) {
0233         DRM_ERROR("Failed to configure link: %d\n", err);
0234         return err;
0235     }
0236 
0237     /* Start training on the source */
0238     err = regmap_write(anx6345->map[I2C_IDX_DPTX], SP_DP_LT_CTRL_REG,
0239                SP_LT_EN);
0240     if (err)
0241         return err;
0242 
0243     return regmap_read_poll_timeout(anx6345->map[I2C_IDX_DPTX],
0244                        SP_DP_LT_CTRL_REG,
0245                        value, !(value & SP_DP_LT_INPROGRESS),
0246                        POLL_DELAY, POLL_TIMEOUT);
0247 }
0248 
0249 static int anx6345_tx_initialization(struct anx6345 *anx6345)
0250 {
0251     int err, i;
0252 
0253     /* FIXME: colordepth is hardcoded for now */
0254     err = regmap_write(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL2_REG,
0255                SP_IN_BPC_6BIT << SP_IN_BPC_SHIFT);
0256     if (err)
0257         return err;
0258 
0259     err = regmap_write(anx6345->map[I2C_IDX_DPTX], SP_DP_PLL_CTRL_REG, 0);
0260     if (err)
0261         return err;
0262 
0263     err = regmap_write(anx6345->map[I2C_IDX_TXCOM],
0264                SP_ANALOG_DEBUG1_REG, 0);
0265     if (err)
0266         return err;
0267 
0268     err = regmap_write(anx6345->map[I2C_IDX_DPTX],
0269                SP_DP_LINK_DEBUG_CTRL_REG,
0270                SP_NEW_PRBS7 | SP_M_VID_DEBUG);
0271     if (err)
0272         return err;
0273 
0274     err = regmap_write(anx6345->map[I2C_IDX_DPTX],
0275                SP_DP_ANALOG_POWER_DOWN_REG, 0);
0276     if (err)
0277         return err;
0278 
0279     /* Force HPD */
0280     err = anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
0281                    SP_DP_SYSTEM_CTRL_BASE + 3,
0282                    SP_HPD_FORCE | SP_HPD_CTRL);
0283     if (err)
0284         return err;
0285 
0286     for (i = 0; i < 4; i++) {
0287         /* 4 lanes */
0288         err = regmap_write(anx6345->map[I2C_IDX_DPTX],
0289                    SP_DP_LANE0_LT_CTRL_REG + i, 0);
0290         if (err)
0291             return err;
0292     }
0293 
0294     /* Reset AUX */
0295     err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM],
0296                    SP_RESET_CTRL2_REG, SP_AUX_RST);
0297     if (err)
0298         return err;
0299 
0300     return anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
0301                  SP_RESET_CTRL2_REG, SP_AUX_RST);
0302 }
0303 
0304 static void anx6345_poweron(struct anx6345 *anx6345)
0305 {
0306     int err;
0307 
0308     /* Ensure reset is asserted before starting power on sequence */
0309     gpiod_set_value_cansleep(anx6345->gpiod_reset, 1);
0310     usleep_range(1000, 2000);
0311 
0312     err = regulator_enable(anx6345->dvdd12);
0313     if (err) {
0314         DRM_ERROR("Failed to enable dvdd12 regulator: %d\n",
0315               err);
0316         return;
0317     }
0318 
0319     /* T1 - delay between VDD12 and VDD25 should be 0-2ms */
0320     usleep_range(1000, 2000);
0321 
0322     err = regulator_enable(anx6345->dvdd25);
0323     if (err) {
0324         DRM_ERROR("Failed to enable dvdd25 regulator: %d\n",
0325               err);
0326         return;
0327     }
0328 
0329     /* T2 - delay between RESETN and all power rail stable,
0330      * should be 2-5ms
0331      */
0332     usleep_range(2000, 5000);
0333 
0334     gpiod_set_value_cansleep(anx6345->gpiod_reset, 0);
0335 
0336     /* Power on registers module */
0337     anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
0338              SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
0339     anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
0340                SP_REGISTER_PD | SP_TOTAL_PD);
0341 
0342     if (anx6345->panel)
0343         drm_panel_prepare(anx6345->panel);
0344 
0345     anx6345->powered = true;
0346 }
0347 
0348 static void anx6345_poweroff(struct anx6345 *anx6345)
0349 {
0350     int err;
0351 
0352     gpiod_set_value_cansleep(anx6345->gpiod_reset, 1);
0353     usleep_range(1000, 2000);
0354 
0355     if (anx6345->panel)
0356         drm_panel_unprepare(anx6345->panel);
0357 
0358     err = regulator_disable(anx6345->dvdd25);
0359     if (err) {
0360         DRM_ERROR("Failed to disable dvdd25 regulator: %d\n",
0361               err);
0362         return;
0363     }
0364 
0365     usleep_range(5000, 10000);
0366 
0367     err = regulator_disable(anx6345->dvdd12);
0368     if (err) {
0369         DRM_ERROR("Failed to disable dvdd12 regulator: %d\n",
0370               err);
0371         return;
0372     }
0373 
0374     usleep_range(1000, 2000);
0375 
0376     anx6345->powered = false;
0377 }
0378 
0379 static int anx6345_start(struct anx6345 *anx6345)
0380 {
0381     int err;
0382 
0383     if (!anx6345->powered)
0384         anx6345_poweron(anx6345);
0385 
0386     /* Power on needed modules */
0387     err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
0388                 SP_POWERDOWN_CTRL_REG,
0389                 SP_VIDEO_PD | SP_LINK_PD);
0390 
0391     err = anx6345_tx_initialization(anx6345);
0392     if (err) {
0393         DRM_ERROR("Failed eDP transmitter initialization: %d\n", err);
0394         anx6345_poweroff(anx6345);
0395         return err;
0396     }
0397 
0398     err = anx6345_dp_link_training(anx6345);
0399     if (err) {
0400         DRM_ERROR("Failed link training: %d\n", err);
0401         anx6345_poweroff(anx6345);
0402         return err;
0403     }
0404 
0405     /*
0406      * This delay seems to help keep the hardware in a good state. Without
0407      * it, there are times where it fails silently.
0408      */
0409     usleep_range(10000, 15000);
0410 
0411     return 0;
0412 }
0413 
0414 static int anx6345_config_dp_output(struct anx6345 *anx6345)
0415 {
0416     int err;
0417 
0418     err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
0419                  SP_VIDEO_MUTE);
0420     if (err)
0421         return err;
0422 
0423     /* Enable DP output */
0424     err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
0425                    SP_VIDEO_EN);
0426     if (err)
0427         return err;
0428 
0429     /* Force stream valid */
0430     return anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
0431                    SP_DP_SYSTEM_CTRL_BASE + 3,
0432                    SP_STRM_FORCE | SP_STRM_CTRL);
0433 }
0434 
0435 static int anx6345_get_downstream_info(struct anx6345 *anx6345)
0436 {
0437     u8 value;
0438     int err;
0439 
0440     err = drm_dp_dpcd_readb(&anx6345->aux, DP_SINK_COUNT, &value);
0441     if (err < 0) {
0442         DRM_ERROR("Get sink count failed %d\n", err);
0443         return err;
0444     }
0445 
0446     if (!DP_GET_SINK_COUNT(value)) {
0447         DRM_ERROR("Downstream disconnected\n");
0448         return -EIO;
0449     }
0450 
0451     return 0;
0452 }
0453 
0454 static int anx6345_get_modes(struct drm_connector *connector)
0455 {
0456     struct anx6345 *anx6345 = connector_to_anx6345(connector);
0457     int err, num_modes = 0;
0458     bool power_off = false;
0459 
0460     mutex_lock(&anx6345->lock);
0461 
0462     if (!anx6345->edid) {
0463         if (!anx6345->powered) {
0464             anx6345_poweron(anx6345);
0465             power_off = true;
0466         }
0467 
0468         err = anx6345_get_downstream_info(anx6345);
0469         if (err) {
0470             DRM_ERROR("Failed to get downstream info: %d\n", err);
0471             goto unlock;
0472         }
0473 
0474         anx6345->edid = drm_get_edid(connector, &anx6345->aux.ddc);
0475         if (!anx6345->edid)
0476             DRM_ERROR("Failed to read EDID from panel\n");
0477 
0478         err = drm_connector_update_edid_property(connector,
0479                              anx6345->edid);
0480         if (err) {
0481             DRM_ERROR("Failed to update EDID property: %d\n", err);
0482             goto unlock;
0483         }
0484     }
0485 
0486     num_modes += drm_add_edid_modes(connector, anx6345->edid);
0487 
0488     /* Driver currently supports only 6bpc */
0489     connector->display_info.bpc = 6;
0490 
0491 unlock:
0492     if (power_off)
0493         anx6345_poweroff(anx6345);
0494 
0495     mutex_unlock(&anx6345->lock);
0496 
0497     if (!num_modes && anx6345->panel)
0498         num_modes += drm_panel_get_modes(anx6345->panel, connector);
0499 
0500     return num_modes;
0501 }
0502 
0503 static const struct drm_connector_helper_funcs anx6345_connector_helper_funcs = {
0504     .get_modes = anx6345_get_modes,
0505 };
0506 
0507 static void
0508 anx6345_connector_destroy(struct drm_connector *connector)
0509 {
0510     drm_connector_cleanup(connector);
0511 }
0512 
0513 static const struct drm_connector_funcs anx6345_connector_funcs = {
0514     .fill_modes = drm_helper_probe_single_connector_modes,
0515     .destroy = anx6345_connector_destroy,
0516     .reset = drm_atomic_helper_connector_reset,
0517     .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
0518     .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
0519 };
0520 
0521 static int anx6345_bridge_attach(struct drm_bridge *bridge,
0522                  enum drm_bridge_attach_flags flags)
0523 {
0524     struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
0525     int err;
0526 
0527     if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) {
0528         DRM_ERROR("Fix bridge driver to make connector optional!");
0529         return -EINVAL;
0530     }
0531 
0532     if (!bridge->encoder) {
0533         DRM_ERROR("Parent encoder object not found");
0534         return -ENODEV;
0535     }
0536 
0537     /* Register aux channel */
0538     anx6345->aux.name = "DP-AUX";
0539     anx6345->aux.dev = &anx6345->client->dev;
0540     anx6345->aux.drm_dev = bridge->dev;
0541     anx6345->aux.transfer = anx6345_aux_transfer;
0542 
0543     err = drm_dp_aux_register(&anx6345->aux);
0544     if (err < 0) {
0545         DRM_ERROR("Failed to register aux channel: %d\n", err);
0546         return err;
0547     }
0548 
0549     err = drm_connector_init(bridge->dev, &anx6345->connector,
0550                  &anx6345_connector_funcs,
0551                  DRM_MODE_CONNECTOR_eDP);
0552     if (err) {
0553         DRM_ERROR("Failed to initialize connector: %d\n", err);
0554         goto aux_unregister;
0555     }
0556 
0557     drm_connector_helper_add(&anx6345->connector,
0558                  &anx6345_connector_helper_funcs);
0559 
0560     anx6345->connector.polled = DRM_CONNECTOR_POLL_HPD;
0561 
0562     err = drm_connector_attach_encoder(&anx6345->connector,
0563                        bridge->encoder);
0564     if (err) {
0565         DRM_ERROR("Failed to link up connector to encoder: %d\n", err);
0566         goto connector_cleanup;
0567     }
0568 
0569     err = drm_connector_register(&anx6345->connector);
0570     if (err) {
0571         DRM_ERROR("Failed to register connector: %d\n", err);
0572         goto connector_cleanup;
0573     }
0574 
0575     return 0;
0576 connector_cleanup:
0577     drm_connector_cleanup(&anx6345->connector);
0578 aux_unregister:
0579     drm_dp_aux_unregister(&anx6345->aux);
0580     return err;
0581 }
0582 
0583 static void anx6345_bridge_detach(struct drm_bridge *bridge)
0584 {
0585     drm_dp_aux_unregister(&bridge_to_anx6345(bridge)->aux);
0586 }
0587 
0588 static enum drm_mode_status
0589 anx6345_bridge_mode_valid(struct drm_bridge *bridge,
0590               const struct drm_display_info *info,
0591               const struct drm_display_mode *mode)
0592 {
0593     if (mode->flags & DRM_MODE_FLAG_INTERLACE)
0594         return MODE_NO_INTERLACE;
0595 
0596     /* Max 1200p at 5.4 Ghz, one lane */
0597     if (mode->clock > 154000)
0598         return MODE_CLOCK_HIGH;
0599 
0600     return MODE_OK;
0601 }
0602 
0603 static void anx6345_bridge_disable(struct drm_bridge *bridge)
0604 {
0605     struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
0606 
0607     /* Power off all modules except configuration registers access */
0608     anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
0609              SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
0610     if (anx6345->panel)
0611         drm_panel_disable(anx6345->panel);
0612 
0613     if (anx6345->powered)
0614         anx6345_poweroff(anx6345);
0615 }
0616 
0617 static void anx6345_bridge_enable(struct drm_bridge *bridge)
0618 {
0619     struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
0620     int err;
0621 
0622     if (anx6345->panel)
0623         drm_panel_enable(anx6345->panel);
0624 
0625     err = anx6345_start(anx6345);
0626     if (err) {
0627         DRM_ERROR("Failed to initialize: %d\n", err);
0628         return;
0629     }
0630 
0631     err = anx6345_config_dp_output(anx6345);
0632     if (err)
0633         DRM_ERROR("Failed to enable DP output: %d\n", err);
0634 }
0635 
0636 static const struct drm_bridge_funcs anx6345_bridge_funcs = {
0637     .attach = anx6345_bridge_attach,
0638     .detach = anx6345_bridge_detach,
0639     .mode_valid = anx6345_bridge_mode_valid,
0640     .disable = anx6345_bridge_disable,
0641     .enable = anx6345_bridge_enable,
0642 };
0643 
0644 static void unregister_i2c_dummy_clients(struct anx6345 *anx6345)
0645 {
0646     unsigned int i;
0647 
0648     for (i = 1; i < ARRAY_SIZE(anx6345->i2c_clients); i++)
0649         if (anx6345->i2c_clients[i] &&
0650             anx6345->i2c_clients[i]->addr != anx6345->client->addr)
0651             i2c_unregister_device(anx6345->i2c_clients[i]);
0652 }
0653 
0654 static const struct regmap_config anx6345_regmap_config = {
0655     .reg_bits = 8,
0656     .val_bits = 8,
0657     .max_register = 0xff,
0658     .cache_type = REGCACHE_NONE,
0659 };
0660 
0661 static const u16 anx6345_chipid_list[] = {
0662     0x6345,
0663 };
0664 
0665 static bool anx6345_get_chip_id(struct anx6345 *anx6345)
0666 {
0667     unsigned int i, idl, idh, version;
0668 
0669     if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_IDL_REG, &idl))
0670         return false;
0671 
0672     if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_IDH_REG, &idh))
0673         return false;
0674 
0675     anx6345->chipid = (u8)idl | ((u8)idh << 8);
0676 
0677     if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_VERSION_REG,
0678             &version))
0679         return false;
0680 
0681     for (i = 0; i < ARRAY_SIZE(anx6345_chipid_list); i++) {
0682         if (anx6345->chipid == anx6345_chipid_list[i]) {
0683             DRM_INFO("Found ANX%x (ver. %d) eDP Transmitter\n",
0684                  anx6345->chipid, version);
0685             return true;
0686         }
0687     }
0688 
0689     DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",
0690           anx6345->chipid, version);
0691 
0692     return false;
0693 }
0694 
0695 static int anx6345_i2c_probe(struct i2c_client *client,
0696                  const struct i2c_device_id *id)
0697 {
0698     struct anx6345 *anx6345;
0699     struct device *dev;
0700     int i, err;
0701 
0702     anx6345 = devm_kzalloc(&client->dev, sizeof(*anx6345), GFP_KERNEL);
0703     if (!anx6345)
0704         return -ENOMEM;
0705 
0706     mutex_init(&anx6345->lock);
0707 
0708     anx6345->bridge.of_node = client->dev.of_node;
0709 
0710     anx6345->client = client;
0711     i2c_set_clientdata(client, anx6345);
0712 
0713     dev = &anx6345->client->dev;
0714 
0715     err = drm_of_find_panel_or_bridge(client->dev.of_node, 1, 0,
0716                       &anx6345->panel, NULL);
0717     if (err == -EPROBE_DEFER)
0718         return err;
0719 
0720     if (err)
0721         DRM_DEBUG("No panel found\n");
0722 
0723     /* 1.2V digital core power regulator  */
0724     anx6345->dvdd12 = devm_regulator_get(dev, "dvdd12");
0725     if (IS_ERR(anx6345->dvdd12)) {
0726         if (PTR_ERR(anx6345->dvdd12) != -EPROBE_DEFER)
0727             DRM_ERROR("Failed to get dvdd12 supply (%ld)\n",
0728                   PTR_ERR(anx6345->dvdd12));
0729         return PTR_ERR(anx6345->dvdd12);
0730     }
0731 
0732     /* 2.5V digital core power regulator  */
0733     anx6345->dvdd25 = devm_regulator_get(dev, "dvdd25");
0734     if (IS_ERR(anx6345->dvdd25)) {
0735         if (PTR_ERR(anx6345->dvdd25) != -EPROBE_DEFER)
0736             DRM_ERROR("Failed to get dvdd25 supply (%ld)\n",
0737                   PTR_ERR(anx6345->dvdd25));
0738         return PTR_ERR(anx6345->dvdd25);
0739     }
0740 
0741     /* GPIO for chip reset */
0742     anx6345->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
0743     if (IS_ERR(anx6345->gpiod_reset)) {
0744         DRM_ERROR("Reset gpio not found\n");
0745         return PTR_ERR(anx6345->gpiod_reset);
0746     }
0747 
0748     /* Map slave addresses of ANX6345 */
0749     for (i = 0; i < I2C_NUM_ADDRESSES; i++) {
0750         if (anx6345_i2c_addresses[i] >> 1 != client->addr)
0751             anx6345->i2c_clients[i] = i2c_new_dummy_device(client->adapter,
0752                         anx6345_i2c_addresses[i] >> 1);
0753         else
0754             anx6345->i2c_clients[i] = client;
0755 
0756         if (IS_ERR(anx6345->i2c_clients[i])) {
0757             err = PTR_ERR(anx6345->i2c_clients[i]);
0758             DRM_ERROR("Failed to reserve I2C bus %02x\n",
0759                   anx6345_i2c_addresses[i]);
0760             goto err_unregister_i2c;
0761         }
0762 
0763         anx6345->map[i] = devm_regmap_init_i2c(anx6345->i2c_clients[i],
0764                                &anx6345_regmap_config);
0765         if (IS_ERR(anx6345->map[i])) {
0766             err = PTR_ERR(anx6345->map[i]);
0767             DRM_ERROR("Failed regmap initialization %02x\n",
0768                   anx6345_i2c_addresses[i]);
0769             goto err_unregister_i2c;
0770         }
0771     }
0772 
0773     /* Look for supported chip ID */
0774     anx6345_poweron(anx6345);
0775     if (anx6345_get_chip_id(anx6345)) {
0776         anx6345->bridge.funcs = &anx6345_bridge_funcs;
0777         drm_bridge_add(&anx6345->bridge);
0778 
0779         return 0;
0780     } else {
0781         anx6345_poweroff(anx6345);
0782         err = -ENODEV;
0783     }
0784 
0785 err_unregister_i2c:
0786     unregister_i2c_dummy_clients(anx6345);
0787     return err;
0788 }
0789 
0790 static int anx6345_i2c_remove(struct i2c_client *client)
0791 {
0792     struct anx6345 *anx6345 = i2c_get_clientdata(client);
0793 
0794     drm_bridge_remove(&anx6345->bridge);
0795 
0796     unregister_i2c_dummy_clients(anx6345);
0797 
0798     kfree(anx6345->edid);
0799 
0800     mutex_destroy(&anx6345->lock);
0801 
0802     return 0;
0803 }
0804 
0805 static const struct i2c_device_id anx6345_id[] = {
0806     { "anx6345", 0 },
0807     { /* sentinel */ }
0808 };
0809 MODULE_DEVICE_TABLE(i2c, anx6345_id);
0810 
0811 static const struct of_device_id anx6345_match_table[] = {
0812     { .compatible = "analogix,anx6345", },
0813     { /* sentinel */ },
0814 };
0815 MODULE_DEVICE_TABLE(of, anx6345_match_table);
0816 
0817 static struct i2c_driver anx6345_driver = {
0818     .driver = {
0819            .name = "anx6345",
0820            .of_match_table = of_match_ptr(anx6345_match_table),
0821           },
0822     .probe = anx6345_i2c_probe,
0823     .remove = anx6345_i2c_remove,
0824     .id_table = anx6345_id,
0825 };
0826 module_i2c_driver(anx6345_driver);
0827 
0828 MODULE_DESCRIPTION("ANX6345 eDP Transmitter driver");
0829 MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
0830 MODULE_LICENSE("GPL v2");