Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Analog Devices ADV7511 HDMI transmitter driver
0004  *
0005  * Copyright 2012 Analog Devices Inc.
0006  */
0007 
0008 #include <linux/clk.h>
0009 #include <linux/device.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/module.h>
0012 #include <linux/of_device.h>
0013 #include <linux/slab.h>
0014 
0015 #include <media/cec.h>
0016 
0017 #include <drm/drm_atomic.h>
0018 #include <drm/drm_atomic_helper.h>
0019 #include <drm/drm_edid.h>
0020 #include <drm/drm_print.h>
0021 #include <drm/drm_probe_helper.h>
0022 
0023 #include "adv7511.h"
0024 
0025 /* ADI recommended values for proper operation. */
0026 static const struct reg_sequence adv7511_fixed_registers[] = {
0027     { 0x98, 0x03 },
0028     { 0x9a, 0xe0 },
0029     { 0x9c, 0x30 },
0030     { 0x9d, 0x61 },
0031     { 0xa2, 0xa4 },
0032     { 0xa3, 0xa4 },
0033     { 0xe0, 0xd0 },
0034     { 0xf9, 0x00 },
0035     { 0x55, 0x02 },
0036 };
0037 
0038 /* -----------------------------------------------------------------------------
0039  * Register access
0040  */
0041 
0042 static const uint8_t adv7511_register_defaults[] = {
0043     0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
0044     0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
0045     0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
0046     0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
0047     0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
0048     0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
0049     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
0050     0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
0051     0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
0052     0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0053     0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
0054     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0055     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
0056     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0057     0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
0058     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0059     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
0060     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0061     0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
0062     0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
0063     0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
0064     0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
0065     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
0066     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0067     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
0068     0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
0069     0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
0070     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
0071     0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
0072     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0073     0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
0074     0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0075 };
0076 
0077 static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
0078 {
0079     switch (reg) {
0080     case ADV7511_REG_CHIP_REVISION:
0081     case ADV7511_REG_SPDIF_FREQ:
0082     case ADV7511_REG_CTS_AUTOMATIC1:
0083     case ADV7511_REG_CTS_AUTOMATIC2:
0084     case ADV7511_REG_VIC_DETECTED:
0085     case ADV7511_REG_VIC_SEND:
0086     case ADV7511_REG_AUX_VIC_DETECTED:
0087     case ADV7511_REG_STATUS:
0088     case ADV7511_REG_GC(1):
0089     case ADV7511_REG_INT(0):
0090     case ADV7511_REG_INT(1):
0091     case ADV7511_REG_PLL_STATUS:
0092     case ADV7511_REG_AN(0):
0093     case ADV7511_REG_AN(1):
0094     case ADV7511_REG_AN(2):
0095     case ADV7511_REG_AN(3):
0096     case ADV7511_REG_AN(4):
0097     case ADV7511_REG_AN(5):
0098     case ADV7511_REG_AN(6):
0099     case ADV7511_REG_AN(7):
0100     case ADV7511_REG_HDCP_STATUS:
0101     case ADV7511_REG_BCAPS:
0102     case ADV7511_REG_BKSV(0):
0103     case ADV7511_REG_BKSV(1):
0104     case ADV7511_REG_BKSV(2):
0105     case ADV7511_REG_BKSV(3):
0106     case ADV7511_REG_BKSV(4):
0107     case ADV7511_REG_DDC_STATUS:
0108     case ADV7511_REG_EDID_READ_CTRL:
0109     case ADV7511_REG_BSTATUS(0):
0110     case ADV7511_REG_BSTATUS(1):
0111     case ADV7511_REG_CHIP_ID_HIGH:
0112     case ADV7511_REG_CHIP_ID_LOW:
0113         return true;
0114     }
0115 
0116     return false;
0117 }
0118 
0119 static const struct regmap_config adv7511_regmap_config = {
0120     .reg_bits = 8,
0121     .val_bits = 8,
0122 
0123     .max_register = 0xff,
0124     .cache_type = REGCACHE_RBTREE,
0125     .reg_defaults_raw = adv7511_register_defaults,
0126     .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
0127 
0128     .volatile_reg = adv7511_register_volatile,
0129 };
0130 
0131 /* -----------------------------------------------------------------------------
0132  * Hardware configuration
0133  */
0134 
0135 static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
0136                  const uint16_t *coeff,
0137                  unsigned int scaling_factor)
0138 {
0139     unsigned int i;
0140 
0141     regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
0142                ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
0143 
0144     if (enable) {
0145         for (i = 0; i < 12; ++i) {
0146             regmap_update_bits(adv7511->regmap,
0147                        ADV7511_REG_CSC_UPPER(i),
0148                        0x1f, coeff[i] >> 8);
0149             regmap_write(adv7511->regmap,
0150                      ADV7511_REG_CSC_LOWER(i),
0151                      coeff[i] & 0xff);
0152         }
0153     }
0154 
0155     if (enable)
0156         regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
0157                    0xe0, 0x80 | (scaling_factor << 5));
0158     else
0159         regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
0160                    0x80, 0x00);
0161 
0162     regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
0163                ADV7511_CSC_UPDATE_MODE, 0);
0164 }
0165 
0166 static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
0167 {
0168     if (packet & 0xff)
0169         regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
0170                    packet, 0xff);
0171 
0172     if (packet & 0xff00) {
0173         packet >>= 8;
0174         regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
0175                    packet, 0xff);
0176     }
0177 
0178     return 0;
0179 }
0180 
0181 static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
0182 {
0183     if (packet & 0xff)
0184         regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
0185                    packet, 0x00);
0186 
0187     if (packet & 0xff00) {
0188         packet >>= 8;
0189         regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
0190                    packet, 0x00);
0191     }
0192 
0193     return 0;
0194 }
0195 
0196 /* Coefficients for adv7511 color space conversion */
0197 static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
0198     0x0734, 0x04ad, 0x0000, 0x1c1b,
0199     0x1ddc, 0x04ad, 0x1f24, 0x0135,
0200     0x0000, 0x04ad, 0x087c, 0x1b77,
0201 };
0202 
0203 static void adv7511_set_config_csc(struct adv7511 *adv7511,
0204                    struct drm_connector *connector,
0205                    bool rgb, bool hdmi_mode)
0206 {
0207     struct adv7511_video_config config;
0208     bool output_format_422, output_format_ycbcr;
0209     unsigned int mode;
0210     uint8_t infoframe[17];
0211 
0212     config.hdmi_mode = hdmi_mode;
0213 
0214     hdmi_avi_infoframe_init(&config.avi_infoframe);
0215 
0216     config.avi_infoframe.scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
0217 
0218     if (rgb) {
0219         config.csc_enable = false;
0220         config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
0221     } else {
0222         config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
0223         config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
0224 
0225         if ((connector->display_info.color_formats &
0226              DRM_COLOR_FORMAT_YCBCR422) &&
0227             config.hdmi_mode) {
0228             config.csc_enable = false;
0229             config.avi_infoframe.colorspace =
0230                 HDMI_COLORSPACE_YUV422;
0231         } else {
0232             config.csc_enable = true;
0233             config.avi_infoframe.colorspace = HDMI_COLORSPACE_RGB;
0234         }
0235     }
0236 
0237     if (config.hdmi_mode) {
0238         mode = ADV7511_HDMI_CFG_MODE_HDMI;
0239 
0240         switch (config.avi_infoframe.colorspace) {
0241         case HDMI_COLORSPACE_YUV444:
0242             output_format_422 = false;
0243             output_format_ycbcr = true;
0244             break;
0245         case HDMI_COLORSPACE_YUV422:
0246             output_format_422 = true;
0247             output_format_ycbcr = true;
0248             break;
0249         default:
0250             output_format_422 = false;
0251             output_format_ycbcr = false;
0252             break;
0253         }
0254     } else {
0255         mode = ADV7511_HDMI_CFG_MODE_DVI;
0256         output_format_422 = false;
0257         output_format_ycbcr = false;
0258     }
0259 
0260     adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
0261 
0262     adv7511_set_colormap(adv7511, config.csc_enable,
0263                  config.csc_coefficents,
0264                  config.csc_scaling_factor);
0265 
0266     regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81,
0267                (output_format_422 << 7) | output_format_ycbcr);
0268 
0269     regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
0270                ADV7511_HDMI_CFG_MODE_MASK, mode);
0271 
0272     hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe,
0273                 sizeof(infoframe));
0274 
0275     /* The AVI infoframe id is not configurable */
0276     regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
0277               infoframe + 1, sizeof(infoframe) - 1);
0278 
0279     adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
0280 }
0281 
0282 static void adv7511_set_link_config(struct adv7511 *adv7511,
0283                     const struct adv7511_link_config *config)
0284 {
0285     /*
0286      * The input style values documented in the datasheet don't match the
0287      * hardware register field values :-(
0288      */
0289     static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
0290 
0291     unsigned int clock_delay;
0292     unsigned int color_depth;
0293     unsigned int input_id;
0294 
0295     clock_delay = (config->clock_delay + 1200) / 400;
0296     color_depth = config->input_color_depth == 8 ? 3
0297             : (config->input_color_depth == 10 ? 1 : 2);
0298 
0299     /* TODO Support input ID 6 */
0300     if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
0301         input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
0302              ? 5 : 0;
0303     else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
0304         input_id = config->embedded_sync ? 8 : 7;
0305     else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
0306         input_id = config->embedded_sync ? 4 : 3;
0307     else
0308         input_id = config->embedded_sync ? 2 : 1;
0309 
0310     regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf,
0311                input_id);
0312     regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e,
0313                (color_depth << 4) |
0314                (input_styles[config->input_style] << 2));
0315     regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
0316              config->input_justification << 3);
0317     regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
0318              config->sync_pulse << 2);
0319 
0320     regmap_write(adv7511->regmap, 0xba, clock_delay << 5);
0321 
0322     adv7511->embedded_sync = config->embedded_sync;
0323     adv7511->hsync_polarity = config->hsync_polarity;
0324     adv7511->vsync_polarity = config->vsync_polarity;
0325     adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
0326 }
0327 
0328 static void __adv7511_power_on(struct adv7511 *adv7511)
0329 {
0330     adv7511->current_edid_segment = -1;
0331 
0332     regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
0333                ADV7511_POWER_POWER_DOWN, 0);
0334     if (adv7511->i2c_main->irq) {
0335         /*
0336          * Documentation says the INT_ENABLE registers are reset in
0337          * POWER_DOWN mode. My 7511w preserved the bits, however.
0338          * Still, let's be safe and stick to the documentation.
0339          */
0340         regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
0341                  ADV7511_INT0_EDID_READY | ADV7511_INT0_HPD);
0342         regmap_update_bits(adv7511->regmap,
0343                    ADV7511_REG_INT_ENABLE(1),
0344                    ADV7511_INT1_DDC_ERROR,
0345                    ADV7511_INT1_DDC_ERROR);
0346     }
0347 
0348     /*
0349      * Per spec it is allowed to pulse the HPD signal to indicate that the
0350      * EDID information has changed. Some monitors do this when they wakeup
0351      * from standby or are enabled. When the HPD goes low the adv7511 is
0352      * reset and the outputs are disabled which might cause the monitor to
0353      * go to standby again. To avoid this we ignore the HPD pin for the
0354      * first few seconds after enabling the output. On the other hand
0355      * adv7535 require to enable HPD Override bit for proper HPD.
0356      */
0357     if (adv7511->type == ADV7535)
0358         regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
0359                    ADV7535_REG_POWER2_HPD_OVERRIDE,
0360                    ADV7535_REG_POWER2_HPD_OVERRIDE);
0361     else
0362         regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
0363                    ADV7511_REG_POWER2_HPD_SRC_MASK,
0364                    ADV7511_REG_POWER2_HPD_SRC_NONE);
0365 }
0366 
0367 static void adv7511_power_on(struct adv7511 *adv7511)
0368 {
0369     __adv7511_power_on(adv7511);
0370 
0371     /*
0372      * Most of the registers are reset during power down or when HPD is low.
0373      */
0374     regcache_sync(adv7511->regmap);
0375 
0376     if (adv7511->type == ADV7533 || adv7511->type == ADV7535)
0377         adv7533_dsi_power_on(adv7511);
0378     adv7511->powered = true;
0379 }
0380 
0381 static void __adv7511_power_off(struct adv7511 *adv7511)
0382 {
0383     /* TODO: setup additional power down modes */
0384     if (adv7511->type == ADV7535)
0385         regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
0386                    ADV7535_REG_POWER2_HPD_OVERRIDE, 0);
0387 
0388     regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
0389                ADV7511_POWER_POWER_DOWN,
0390                ADV7511_POWER_POWER_DOWN);
0391     regmap_update_bits(adv7511->regmap,
0392                ADV7511_REG_INT_ENABLE(1),
0393                ADV7511_INT1_DDC_ERROR, 0);
0394     regcache_mark_dirty(adv7511->regmap);
0395 }
0396 
0397 static void adv7511_power_off(struct adv7511 *adv7511)
0398 {
0399     __adv7511_power_off(adv7511);
0400     if (adv7511->type == ADV7533 || adv7511->type == ADV7535)
0401         adv7533_dsi_power_off(adv7511);
0402     adv7511->powered = false;
0403 }
0404 
0405 /* -----------------------------------------------------------------------------
0406  * Interrupt and hotplug detection
0407  */
0408 
0409 static bool adv7511_hpd(struct adv7511 *adv7511)
0410 {
0411     unsigned int irq0;
0412     int ret;
0413 
0414     ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
0415     if (ret < 0)
0416         return false;
0417 
0418     if (irq0 & ADV7511_INT0_HPD) {
0419         regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
0420                  ADV7511_INT0_HPD);
0421         return true;
0422     }
0423 
0424     return false;
0425 }
0426 
0427 static void adv7511_hpd_work(struct work_struct *work)
0428 {
0429     struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
0430     enum drm_connector_status status;
0431     unsigned int val;
0432     int ret;
0433 
0434     ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
0435     if (ret < 0)
0436         status = connector_status_disconnected;
0437     else if (val & ADV7511_STATUS_HPD)
0438         status = connector_status_connected;
0439     else
0440         status = connector_status_disconnected;
0441 
0442     /*
0443      * The bridge resets its registers on unplug. So when we get a plug
0444      * event and we're already supposed to be powered, cycle the bridge to
0445      * restore its state.
0446      */
0447     if (status == connector_status_connected &&
0448         adv7511->connector.status == connector_status_disconnected &&
0449         adv7511->powered) {
0450         regcache_mark_dirty(adv7511->regmap);
0451         adv7511_power_on(adv7511);
0452     }
0453 
0454     if (adv7511->connector.status != status) {
0455         adv7511->connector.status = status;
0456 
0457         if (adv7511->connector.dev) {
0458             if (status == connector_status_disconnected)
0459                 cec_phys_addr_invalidate(adv7511->cec_adap);
0460             drm_kms_helper_hotplug_event(adv7511->connector.dev);
0461         } else {
0462             drm_bridge_hpd_notify(&adv7511->bridge, status);
0463         }
0464     }
0465 }
0466 
0467 static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
0468 {
0469     unsigned int irq0, irq1;
0470     int ret;
0471 
0472     ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
0473     if (ret < 0)
0474         return ret;
0475 
0476     ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
0477     if (ret < 0)
0478         return ret;
0479 
0480     regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
0481     regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
0482 
0483     if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder)
0484         schedule_work(&adv7511->hpd_work);
0485 
0486     if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
0487         adv7511->edid_read = true;
0488 
0489         if (adv7511->i2c_main->irq)
0490             wake_up_all(&adv7511->wq);
0491     }
0492 
0493 #ifdef CONFIG_DRM_I2C_ADV7511_CEC
0494     adv7511_cec_irq_process(adv7511, irq1);
0495 #endif
0496 
0497     return 0;
0498 }
0499 
0500 static irqreturn_t adv7511_irq_handler(int irq, void *devid)
0501 {
0502     struct adv7511 *adv7511 = devid;
0503     int ret;
0504 
0505     ret = adv7511_irq_process(adv7511, true);
0506     return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
0507 }
0508 
0509 /* -----------------------------------------------------------------------------
0510  * EDID retrieval
0511  */
0512 
0513 static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
0514 {
0515     int ret;
0516 
0517     if (adv7511->i2c_main->irq) {
0518         ret = wait_event_interruptible_timeout(adv7511->wq,
0519                 adv7511->edid_read, msecs_to_jiffies(timeout));
0520     } else {
0521         for (; timeout > 0; timeout -= 25) {
0522             ret = adv7511_irq_process(adv7511, false);
0523             if (ret < 0)
0524                 break;
0525 
0526             if (adv7511->edid_read)
0527                 break;
0528 
0529             msleep(25);
0530         }
0531     }
0532 
0533     return adv7511->edid_read ? 0 : -EIO;
0534 }
0535 
0536 static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
0537                   size_t len)
0538 {
0539     struct adv7511 *adv7511 = data;
0540     struct i2c_msg xfer[2];
0541     uint8_t offset;
0542     unsigned int i;
0543     int ret;
0544 
0545     if (len > 128)
0546         return -EINVAL;
0547 
0548     if (adv7511->current_edid_segment != block / 2) {
0549         unsigned int status;
0550 
0551         ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
0552                   &status);
0553         if (ret < 0)
0554             return ret;
0555 
0556         if (status != 2) {
0557             adv7511->edid_read = false;
0558             regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
0559                      block);
0560             ret = adv7511_wait_for_edid(adv7511, 200);
0561             if (ret < 0)
0562                 return ret;
0563         }
0564 
0565         /* Break this apart, hopefully more I2C controllers will
0566          * support 64 byte transfers than 256 byte transfers
0567          */
0568 
0569         xfer[0].addr = adv7511->i2c_edid->addr;
0570         xfer[0].flags = 0;
0571         xfer[0].len = 1;
0572         xfer[0].buf = &offset;
0573         xfer[1].addr = adv7511->i2c_edid->addr;
0574         xfer[1].flags = I2C_M_RD;
0575         xfer[1].len = 64;
0576         xfer[1].buf = adv7511->edid_buf;
0577 
0578         offset = 0;
0579 
0580         for (i = 0; i < 4; ++i) {
0581             ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
0582                        ARRAY_SIZE(xfer));
0583             if (ret < 0)
0584                 return ret;
0585             else if (ret != 2)
0586                 return -EIO;
0587 
0588             xfer[1].buf += 64;
0589             offset += 64;
0590         }
0591 
0592         adv7511->current_edid_segment = block / 2;
0593     }
0594 
0595     if (block % 2 == 0)
0596         memcpy(buf, adv7511->edid_buf, len);
0597     else
0598         memcpy(buf, adv7511->edid_buf + 128, len);
0599 
0600     return 0;
0601 }
0602 
0603 /* -----------------------------------------------------------------------------
0604  * ADV75xx helpers
0605  */
0606 
0607 static struct edid *adv7511_get_edid(struct adv7511 *adv7511,
0608                      struct drm_connector *connector)
0609 {
0610     struct edid *edid;
0611 
0612     /* Reading the EDID only works if the device is powered */
0613     if (!adv7511->powered) {
0614         unsigned int edid_i2c_addr =
0615                     (adv7511->i2c_edid->addr << 1);
0616 
0617         __adv7511_power_on(adv7511);
0618 
0619         /* Reset the EDID_I2C_ADDR register as it might be cleared */
0620         regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
0621                  edid_i2c_addr);
0622     }
0623 
0624     edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
0625 
0626     if (!adv7511->powered)
0627         __adv7511_power_off(adv7511);
0628 
0629     adv7511_set_config_csc(adv7511, connector, adv7511->rgb,
0630                    drm_detect_hdmi_monitor(edid));
0631 
0632     cec_s_phys_addr_from_edid(adv7511->cec_adap, edid);
0633 
0634     return edid;
0635 }
0636 
0637 static int adv7511_get_modes(struct adv7511 *adv7511,
0638                  struct drm_connector *connector)
0639 {
0640     struct edid *edid;
0641     unsigned int count;
0642 
0643     edid = adv7511_get_edid(adv7511, connector);
0644 
0645     drm_connector_update_edid_property(connector, edid);
0646     count = drm_add_edid_modes(connector, edid);
0647 
0648     kfree(edid);
0649 
0650     return count;
0651 }
0652 
0653 static enum drm_connector_status
0654 adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector)
0655 {
0656     enum drm_connector_status status;
0657     unsigned int val;
0658     bool hpd;
0659     int ret;
0660 
0661     ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
0662     if (ret < 0)
0663         return connector_status_disconnected;
0664 
0665     if (val & ADV7511_STATUS_HPD)
0666         status = connector_status_connected;
0667     else
0668         status = connector_status_disconnected;
0669 
0670     hpd = adv7511_hpd(adv7511);
0671 
0672     /* The chip resets itself when the cable is disconnected, so in case
0673      * there is a pending HPD interrupt and the cable is connected there was
0674      * at least one transition from disconnected to connected and the chip
0675      * has to be reinitialized. */
0676     if (status == connector_status_connected && hpd && adv7511->powered) {
0677         regcache_mark_dirty(adv7511->regmap);
0678         adv7511_power_on(adv7511);
0679         if (connector)
0680             adv7511_get_modes(adv7511, connector);
0681         if (adv7511->status == connector_status_connected)
0682             status = connector_status_disconnected;
0683     } else {
0684         /* Renable HPD sensing */
0685         if (adv7511->type == ADV7535)
0686             regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
0687                        ADV7535_REG_POWER2_HPD_OVERRIDE,
0688                        ADV7535_REG_POWER2_HPD_OVERRIDE);
0689         else
0690             regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
0691                        ADV7511_REG_POWER2_HPD_SRC_MASK,
0692                        ADV7511_REG_POWER2_HPD_SRC_BOTH);
0693     }
0694 
0695     adv7511->status = status;
0696     return status;
0697 }
0698 
0699 static enum drm_mode_status adv7511_mode_valid(struct adv7511 *adv7511,
0700                   struct drm_display_mode *mode)
0701 {
0702     if (mode->clock > 165000)
0703         return MODE_CLOCK_HIGH;
0704 
0705     return MODE_OK;
0706 }
0707 
0708 static void adv7511_mode_set(struct adv7511 *adv7511,
0709                  const struct drm_display_mode *mode,
0710                  const struct drm_display_mode *adj_mode)
0711 {
0712     unsigned int low_refresh_rate;
0713     unsigned int hsync_polarity = 0;
0714     unsigned int vsync_polarity = 0;
0715 
0716     if (adv7511->embedded_sync) {
0717         unsigned int hsync_offset, hsync_len;
0718         unsigned int vsync_offset, vsync_len;
0719 
0720         hsync_offset = adj_mode->crtc_hsync_start -
0721                    adj_mode->crtc_hdisplay;
0722         vsync_offset = adj_mode->crtc_vsync_start -
0723                    adj_mode->crtc_vdisplay;
0724         hsync_len = adj_mode->crtc_hsync_end -
0725                 adj_mode->crtc_hsync_start;
0726         vsync_len = adj_mode->crtc_vsync_end -
0727                 adj_mode->crtc_vsync_start;
0728 
0729         /* The hardware vsync generator has a off-by-one bug */
0730         vsync_offset += 1;
0731 
0732         regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
0733                  ((hsync_offset >> 10) & 0x7) << 5);
0734         regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
0735                  (hsync_offset >> 2) & 0xff);
0736         regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
0737                  ((hsync_offset & 0x3) << 6) |
0738                  ((hsync_len >> 4) & 0x3f));
0739         regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
0740                  ((hsync_len & 0xf) << 4) |
0741                  ((vsync_offset >> 6) & 0xf));
0742         regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
0743                  ((vsync_offset & 0x3f) << 2) |
0744                  ((vsync_len >> 8) & 0x3));
0745         regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
0746                  vsync_len & 0xff);
0747 
0748         hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
0749         vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
0750     } else {
0751         enum adv7511_sync_polarity mode_hsync_polarity;
0752         enum adv7511_sync_polarity mode_vsync_polarity;
0753 
0754         /**
0755          * If the input signal is always low or always high we want to
0756          * invert or let it passthrough depending on the polarity of the
0757          * current mode.
0758          **/
0759         if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
0760             mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
0761         else
0762             mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
0763 
0764         if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
0765             mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
0766         else
0767             mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
0768 
0769         if (adv7511->hsync_polarity != mode_hsync_polarity &&
0770             adv7511->hsync_polarity !=
0771             ADV7511_SYNC_POLARITY_PASSTHROUGH)
0772             hsync_polarity = 1;
0773 
0774         if (adv7511->vsync_polarity != mode_vsync_polarity &&
0775             adv7511->vsync_polarity !=
0776             ADV7511_SYNC_POLARITY_PASSTHROUGH)
0777             vsync_polarity = 1;
0778     }
0779 
0780     if (drm_mode_vrefresh(mode) <= 24)
0781         low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
0782     else if (drm_mode_vrefresh(mode) <= 25)
0783         low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
0784     else if (drm_mode_vrefresh(mode) <= 30)
0785         low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
0786     else
0787         low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
0788 
0789     regmap_update_bits(adv7511->regmap, 0xfb,
0790         0x6, low_refresh_rate << 1);
0791     regmap_update_bits(adv7511->regmap, 0x17,
0792         0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
0793 
0794     if (adv7511->type == ADV7533 || adv7511->type == ADV7535)
0795         adv7533_mode_set(adv7511, adj_mode);
0796 
0797     drm_mode_copy(&adv7511->curr_mode, adj_mode);
0798 
0799     /*
0800      * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
0801      * supposed to give better results.
0802      */
0803 
0804     adv7511->f_tmds = mode->clock;
0805 }
0806 
0807 /* -----------------------------------------------------------------------------
0808  * DRM Connector Operations
0809  */
0810 
0811 static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
0812 {
0813     return container_of(connector, struct adv7511, connector);
0814 }
0815 
0816 static int adv7511_connector_get_modes(struct drm_connector *connector)
0817 {
0818     struct adv7511 *adv = connector_to_adv7511(connector);
0819 
0820     return adv7511_get_modes(adv, connector);
0821 }
0822 
0823 static enum drm_mode_status
0824 adv7511_connector_mode_valid(struct drm_connector *connector,
0825                  struct drm_display_mode *mode)
0826 {
0827     struct adv7511 *adv = connector_to_adv7511(connector);
0828 
0829     return adv7511_mode_valid(adv, mode);
0830 }
0831 
0832 static struct drm_connector_helper_funcs adv7511_connector_helper_funcs = {
0833     .get_modes = adv7511_connector_get_modes,
0834     .mode_valid = adv7511_connector_mode_valid,
0835 };
0836 
0837 static enum drm_connector_status
0838 adv7511_connector_detect(struct drm_connector *connector, bool force)
0839 {
0840     struct adv7511 *adv = connector_to_adv7511(connector);
0841 
0842     return adv7511_detect(adv, connector);
0843 }
0844 
0845 static const struct drm_connector_funcs adv7511_connector_funcs = {
0846     .fill_modes = drm_helper_probe_single_connector_modes,
0847     .detect = adv7511_connector_detect,
0848     .destroy = drm_connector_cleanup,
0849     .reset = drm_atomic_helper_connector_reset,
0850     .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
0851     .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
0852 };
0853 
0854 static int adv7511_connector_init(struct adv7511 *adv)
0855 {
0856     struct drm_bridge *bridge = &adv->bridge;
0857     int ret;
0858 
0859     if (!bridge->encoder) {
0860         DRM_ERROR("Parent encoder object not found");
0861         return -ENODEV;
0862     }
0863 
0864     if (adv->i2c_main->irq)
0865         adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
0866     else
0867         adv->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
0868                 DRM_CONNECTOR_POLL_DISCONNECT;
0869 
0870     ret = drm_connector_init(bridge->dev, &adv->connector,
0871                  &adv7511_connector_funcs,
0872                  DRM_MODE_CONNECTOR_HDMIA);
0873     if (ret < 0) {
0874         DRM_ERROR("Failed to initialize connector with drm\n");
0875         return ret;
0876     }
0877     drm_connector_helper_add(&adv->connector,
0878                  &adv7511_connector_helper_funcs);
0879     drm_connector_attach_encoder(&adv->connector, bridge->encoder);
0880 
0881     return 0;
0882 }
0883 
0884 /* -----------------------------------------------------------------------------
0885  * DRM Bridge Operations
0886  */
0887 
0888 static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
0889 {
0890     return container_of(bridge, struct adv7511, bridge);
0891 }
0892 
0893 static void adv7511_bridge_enable(struct drm_bridge *bridge)
0894 {
0895     struct adv7511 *adv = bridge_to_adv7511(bridge);
0896 
0897     adv7511_power_on(adv);
0898 }
0899 
0900 static void adv7511_bridge_disable(struct drm_bridge *bridge)
0901 {
0902     struct adv7511 *adv = bridge_to_adv7511(bridge);
0903 
0904     adv7511_power_off(adv);
0905 }
0906 
0907 static void adv7511_bridge_mode_set(struct drm_bridge *bridge,
0908                     const struct drm_display_mode *mode,
0909                     const struct drm_display_mode *adj_mode)
0910 {
0911     struct adv7511 *adv = bridge_to_adv7511(bridge);
0912 
0913     adv7511_mode_set(adv, mode, adj_mode);
0914 }
0915 
0916 static int adv7511_bridge_attach(struct drm_bridge *bridge,
0917                  enum drm_bridge_attach_flags flags)
0918 {
0919     struct adv7511 *adv = bridge_to_adv7511(bridge);
0920     int ret = 0;
0921 
0922     if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
0923         ret = adv7511_connector_init(adv);
0924         if (ret < 0)
0925             return ret;
0926     }
0927 
0928     if (adv->i2c_main->irq)
0929         regmap_write(adv->regmap, ADV7511_REG_INT_ENABLE(0),
0930                  ADV7511_INT0_HPD);
0931 
0932     return ret;
0933 }
0934 
0935 static enum drm_connector_status adv7511_bridge_detect(struct drm_bridge *bridge)
0936 {
0937     struct adv7511 *adv = bridge_to_adv7511(bridge);
0938 
0939     return adv7511_detect(adv, NULL);
0940 }
0941 
0942 static struct edid *adv7511_bridge_get_edid(struct drm_bridge *bridge,
0943                         struct drm_connector *connector)
0944 {
0945     struct adv7511 *adv = bridge_to_adv7511(bridge);
0946 
0947     return adv7511_get_edid(adv, connector);
0948 }
0949 
0950 static void adv7511_bridge_hpd_notify(struct drm_bridge *bridge,
0951                       enum drm_connector_status status)
0952 {
0953     struct adv7511 *adv = bridge_to_adv7511(bridge);
0954 
0955     if (status == connector_status_disconnected)
0956         cec_phys_addr_invalidate(adv->cec_adap);
0957 }
0958 
0959 static const struct drm_bridge_funcs adv7511_bridge_funcs = {
0960     .enable = adv7511_bridge_enable,
0961     .disable = adv7511_bridge_disable,
0962     .mode_set = adv7511_bridge_mode_set,
0963     .attach = adv7511_bridge_attach,
0964     .detect = adv7511_bridge_detect,
0965     .get_edid = adv7511_bridge_get_edid,
0966     .hpd_notify = adv7511_bridge_hpd_notify,
0967 };
0968 
0969 /* -----------------------------------------------------------------------------
0970  * Probe & remove
0971  */
0972 
0973 static const char * const adv7511_supply_names[] = {
0974     "avdd",
0975     "dvdd",
0976     "pvdd",
0977     "bgvdd",
0978     "dvdd-3v",
0979 };
0980 
0981 static const char * const adv7533_supply_names[] = {
0982     "avdd",
0983     "dvdd",
0984     "pvdd",
0985     "a2vdd",
0986     "v3p3",
0987     "v1p2",
0988 };
0989 
0990 static int adv7511_init_regulators(struct adv7511 *adv)
0991 {
0992     struct device *dev = &adv->i2c_main->dev;
0993     const char * const *supply_names;
0994     unsigned int i;
0995     int ret;
0996 
0997     if (adv->type == ADV7511) {
0998         supply_names = adv7511_supply_names;
0999         adv->num_supplies = ARRAY_SIZE(adv7511_supply_names);
1000     } else {
1001         supply_names = adv7533_supply_names;
1002         adv->num_supplies = ARRAY_SIZE(adv7533_supply_names);
1003     }
1004 
1005     adv->supplies = devm_kcalloc(dev, adv->num_supplies,
1006                      sizeof(*adv->supplies), GFP_KERNEL);
1007     if (!adv->supplies)
1008         return -ENOMEM;
1009 
1010     for (i = 0; i < adv->num_supplies; i++)
1011         adv->supplies[i].supply = supply_names[i];
1012 
1013     ret = devm_regulator_bulk_get(dev, adv->num_supplies, adv->supplies);
1014     if (ret)
1015         return ret;
1016 
1017     return regulator_bulk_enable(adv->num_supplies, adv->supplies);
1018 }
1019 
1020 static void adv7511_uninit_regulators(struct adv7511 *adv)
1021 {
1022     regulator_bulk_disable(adv->num_supplies, adv->supplies);
1023 }
1024 
1025 static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg)
1026 {
1027     struct i2c_client *i2c = to_i2c_client(dev);
1028     struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1029 
1030     reg -= adv7511->reg_cec_offset;
1031 
1032     switch (reg) {
1033     case ADV7511_REG_CEC_RX1_FRAME_HDR:
1034     case ADV7511_REG_CEC_RX1_FRAME_DATA0 ... ADV7511_REG_CEC_RX1_FRAME_DATA0 + 14:
1035     case ADV7511_REG_CEC_RX1_FRAME_LEN:
1036     case ADV7511_REG_CEC_RX2_FRAME_HDR:
1037     case ADV7511_REG_CEC_RX2_FRAME_DATA0 ... ADV7511_REG_CEC_RX2_FRAME_DATA0 + 14:
1038     case ADV7511_REG_CEC_RX2_FRAME_LEN:
1039     case ADV7511_REG_CEC_RX3_FRAME_HDR:
1040     case ADV7511_REG_CEC_RX3_FRAME_DATA0 ... ADV7511_REG_CEC_RX3_FRAME_DATA0 + 14:
1041     case ADV7511_REG_CEC_RX3_FRAME_LEN:
1042     case ADV7511_REG_CEC_RX_STATUS:
1043     case ADV7511_REG_CEC_RX_BUFFERS:
1044     case ADV7511_REG_CEC_TX_LOW_DRV_CNT:
1045         return true;
1046     }
1047 
1048     return false;
1049 }
1050 
1051 static const struct regmap_config adv7511_cec_regmap_config = {
1052     .reg_bits = 8,
1053     .val_bits = 8,
1054 
1055     .max_register = 0xff,
1056     .cache_type = REGCACHE_RBTREE,
1057     .volatile_reg = adv7511_cec_register_volatile,
1058 };
1059 
1060 static int adv7511_init_cec_regmap(struct adv7511 *adv)
1061 {
1062     int ret;
1063 
1064     adv->i2c_cec = i2c_new_ancillary_device(adv->i2c_main, "cec",
1065                         ADV7511_CEC_I2C_ADDR_DEFAULT);
1066     if (IS_ERR(adv->i2c_cec))
1067         return PTR_ERR(adv->i2c_cec);
1068 
1069     regmap_write(adv->regmap, ADV7511_REG_CEC_I2C_ADDR,
1070              adv->i2c_cec->addr << 1);
1071 
1072     i2c_set_clientdata(adv->i2c_cec, adv);
1073 
1074     adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec,
1075                     &adv7511_cec_regmap_config);
1076     if (IS_ERR(adv->regmap_cec)) {
1077         ret = PTR_ERR(adv->regmap_cec);
1078         goto err;
1079     }
1080 
1081     if (adv->type == ADV7533 || adv->type == ADV7535) {
1082         ret = adv7533_patch_cec_registers(adv);
1083         if (ret)
1084             goto err;
1085 
1086         adv->reg_cec_offset = ADV7533_REG_CEC_OFFSET;
1087     }
1088 
1089     return 0;
1090 err:
1091     i2c_unregister_device(adv->i2c_cec);
1092     return ret;
1093 }
1094 
1095 static int adv7511_parse_dt(struct device_node *np,
1096                 struct adv7511_link_config *config)
1097 {
1098     const char *str;
1099     int ret;
1100 
1101     of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
1102     if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
1103         config->input_color_depth != 12)
1104         return -EINVAL;
1105 
1106     ret = of_property_read_string(np, "adi,input-colorspace", &str);
1107     if (ret < 0)
1108         return ret;
1109 
1110     if (!strcmp(str, "rgb"))
1111         config->input_colorspace = HDMI_COLORSPACE_RGB;
1112     else if (!strcmp(str, "yuv422"))
1113         config->input_colorspace = HDMI_COLORSPACE_YUV422;
1114     else if (!strcmp(str, "yuv444"))
1115         config->input_colorspace = HDMI_COLORSPACE_YUV444;
1116     else
1117         return -EINVAL;
1118 
1119     ret = of_property_read_string(np, "adi,input-clock", &str);
1120     if (ret < 0)
1121         return ret;
1122 
1123     if (!strcmp(str, "1x"))
1124         config->input_clock = ADV7511_INPUT_CLOCK_1X;
1125     else if (!strcmp(str, "2x"))
1126         config->input_clock = ADV7511_INPUT_CLOCK_2X;
1127     else if (!strcmp(str, "ddr"))
1128         config->input_clock = ADV7511_INPUT_CLOCK_DDR;
1129     else
1130         return -EINVAL;
1131 
1132     if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
1133         config->input_clock != ADV7511_INPUT_CLOCK_1X) {
1134         ret = of_property_read_u32(np, "adi,input-style",
1135                        &config->input_style);
1136         if (ret)
1137             return ret;
1138 
1139         if (config->input_style < 1 || config->input_style > 3)
1140             return -EINVAL;
1141 
1142         ret = of_property_read_string(np, "adi,input-justification",
1143                           &str);
1144         if (ret < 0)
1145             return ret;
1146 
1147         if (!strcmp(str, "left"))
1148             config->input_justification =
1149                 ADV7511_INPUT_JUSTIFICATION_LEFT;
1150         else if (!strcmp(str, "evenly"))
1151             config->input_justification =
1152                 ADV7511_INPUT_JUSTIFICATION_EVENLY;
1153         else if (!strcmp(str, "right"))
1154             config->input_justification =
1155                 ADV7511_INPUT_JUSTIFICATION_RIGHT;
1156         else
1157             return -EINVAL;
1158 
1159     } else {
1160         config->input_style = 1;
1161         config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
1162     }
1163 
1164     of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
1165     if (config->clock_delay < -1200 || config->clock_delay > 1600)
1166         return -EINVAL;
1167 
1168     config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
1169 
1170     /* Hardcode the sync pulse configurations for now. */
1171     config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1172     config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1173     config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1174 
1175     return 0;
1176 }
1177 
1178 static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1179 {
1180     struct adv7511_link_config link_config;
1181     struct adv7511 *adv7511;
1182     struct device *dev = &i2c->dev;
1183     unsigned int val;
1184     int ret;
1185 
1186     if (!dev->of_node)
1187         return -EINVAL;
1188 
1189     adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
1190     if (!adv7511)
1191         return -ENOMEM;
1192 
1193     adv7511->i2c_main = i2c;
1194     adv7511->powered = false;
1195     adv7511->status = connector_status_disconnected;
1196 
1197     if (dev->of_node)
1198         adv7511->type = (enum adv7511_type)of_device_get_match_data(dev);
1199     else
1200         adv7511->type = id->driver_data;
1201 
1202     memset(&link_config, 0, sizeof(link_config));
1203 
1204     if (adv7511->type == ADV7511)
1205         ret = adv7511_parse_dt(dev->of_node, &link_config);
1206     else
1207         ret = adv7533_parse_dt(dev->of_node, adv7511);
1208     if (ret)
1209         return ret;
1210 
1211     ret = adv7511_init_regulators(adv7511);
1212     if (ret) {
1213         dev_err(dev, "failed to init regulators\n");
1214         return ret;
1215     }
1216 
1217     /*
1218      * The power down GPIO is optional. If present, toggle it from active to
1219      * inactive to wake up the encoder.
1220      */
1221     adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
1222     if (IS_ERR(adv7511->gpio_pd)) {
1223         ret = PTR_ERR(adv7511->gpio_pd);
1224         goto uninit_regulators;
1225     }
1226 
1227     if (adv7511->gpio_pd) {
1228         usleep_range(5000, 6000);
1229         gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
1230     }
1231 
1232     adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
1233     if (IS_ERR(adv7511->regmap)) {
1234         ret = PTR_ERR(adv7511->regmap);
1235         goto uninit_regulators;
1236     }
1237 
1238     ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
1239     if (ret)
1240         goto uninit_regulators;
1241     dev_dbg(dev, "Rev. %d\n", val);
1242 
1243     if (adv7511->type == ADV7511)
1244         ret = regmap_register_patch(adv7511->regmap,
1245                         adv7511_fixed_registers,
1246                         ARRAY_SIZE(adv7511_fixed_registers));
1247     else
1248         ret = adv7533_patch_registers(adv7511);
1249     if (ret)
1250         goto uninit_regulators;
1251 
1252     adv7511_packet_disable(adv7511, 0xffff);
1253 
1254     adv7511->i2c_edid = i2c_new_ancillary_device(i2c, "edid",
1255                     ADV7511_EDID_I2C_ADDR_DEFAULT);
1256     if (IS_ERR(adv7511->i2c_edid)) {
1257         ret = PTR_ERR(adv7511->i2c_edid);
1258         goto uninit_regulators;
1259     }
1260 
1261     regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
1262              adv7511->i2c_edid->addr << 1);
1263 
1264     adv7511->i2c_packet = i2c_new_ancillary_device(i2c, "packet",
1265                     ADV7511_PACKET_I2C_ADDR_DEFAULT);
1266     if (IS_ERR(adv7511->i2c_packet)) {
1267         ret = PTR_ERR(adv7511->i2c_packet);
1268         goto err_i2c_unregister_edid;
1269     }
1270 
1271     regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
1272              adv7511->i2c_packet->addr << 1);
1273 
1274     ret = adv7511_init_cec_regmap(adv7511);
1275     if (ret)
1276         goto err_i2c_unregister_packet;
1277 
1278     INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1279 
1280     if (i2c->irq) {
1281         init_waitqueue_head(&adv7511->wq);
1282 
1283         ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1284                         adv7511_irq_handler,
1285                         IRQF_ONESHOT, dev_name(dev),
1286                         adv7511);
1287         if (ret)
1288             goto err_unregister_cec;
1289     }
1290 
1291     adv7511_power_off(adv7511);
1292 
1293     i2c_set_clientdata(i2c, adv7511);
1294 
1295     if (adv7511->type == ADV7511)
1296         adv7511_set_link_config(adv7511, &link_config);
1297 
1298     ret = adv7511_cec_init(dev, adv7511);
1299     if (ret)
1300         goto err_unregister_cec;
1301 
1302     adv7511->bridge.funcs = &adv7511_bridge_funcs;
1303     adv7511->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID;
1304     if (adv7511->i2c_main->irq)
1305         adv7511->bridge.ops |= DRM_BRIDGE_OP_HPD;
1306 
1307     adv7511->bridge.of_node = dev->of_node;
1308     adv7511->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1309 
1310     drm_bridge_add(&adv7511->bridge);
1311 
1312     adv7511_audio_init(dev, adv7511);
1313 
1314     if (adv7511->type == ADV7533 || adv7511->type == ADV7535) {
1315         ret = adv7533_attach_dsi(adv7511);
1316         if (ret)
1317             goto err_unregister_audio;
1318     }
1319 
1320     return 0;
1321 
1322 err_unregister_audio:
1323     adv7511_audio_exit(adv7511);
1324     drm_bridge_remove(&adv7511->bridge);
1325 err_unregister_cec:
1326     cec_unregister_adapter(adv7511->cec_adap);
1327     i2c_unregister_device(adv7511->i2c_cec);
1328     clk_disable_unprepare(adv7511->cec_clk);
1329 err_i2c_unregister_packet:
1330     i2c_unregister_device(adv7511->i2c_packet);
1331 err_i2c_unregister_edid:
1332     i2c_unregister_device(adv7511->i2c_edid);
1333 uninit_regulators:
1334     adv7511_uninit_regulators(adv7511);
1335 
1336     return ret;
1337 }
1338 
1339 static int adv7511_remove(struct i2c_client *i2c)
1340 {
1341     struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1342 
1343     i2c_unregister_device(adv7511->i2c_cec);
1344     clk_disable_unprepare(adv7511->cec_clk);
1345 
1346     adv7511_uninit_regulators(adv7511);
1347 
1348     drm_bridge_remove(&adv7511->bridge);
1349 
1350     adv7511_audio_exit(adv7511);
1351 
1352     cec_unregister_adapter(adv7511->cec_adap);
1353 
1354     i2c_unregister_device(adv7511->i2c_packet);
1355     i2c_unregister_device(adv7511->i2c_edid);
1356 
1357     return 0;
1358 }
1359 
1360 static const struct i2c_device_id adv7511_i2c_ids[] = {
1361     { "adv7511", ADV7511 },
1362     { "adv7511w", ADV7511 },
1363     { "adv7513", ADV7511 },
1364     { "adv7533", ADV7533 },
1365     { "adv7535", ADV7535 },
1366     { }
1367 };
1368 MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1369 
1370 static const struct of_device_id adv7511_of_ids[] = {
1371     { .compatible = "adi,adv7511", .data = (void *)ADV7511 },
1372     { .compatible = "adi,adv7511w", .data = (void *)ADV7511 },
1373     { .compatible = "adi,adv7513", .data = (void *)ADV7511 },
1374     { .compatible = "adi,adv7533", .data = (void *)ADV7533 },
1375     { .compatible = "adi,adv7535", .data = (void *)ADV7535 },
1376     { }
1377 };
1378 MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1379 
1380 static struct mipi_dsi_driver adv7533_dsi_driver = {
1381     .driver.name = "adv7533",
1382 };
1383 
1384 static struct i2c_driver adv7511_driver = {
1385     .driver = {
1386         .name = "adv7511",
1387         .of_match_table = adv7511_of_ids,
1388     },
1389     .id_table = adv7511_i2c_ids,
1390     .probe = adv7511_probe,
1391     .remove = adv7511_remove,
1392 };
1393 
1394 static int __init adv7511_init(void)
1395 {
1396     int ret;
1397 
1398     if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
1399         ret = mipi_dsi_driver_register(&adv7533_dsi_driver);
1400         if (ret)
1401             return ret;
1402     }
1403 
1404     ret = i2c_add_driver(&adv7511_driver);
1405     if (ret) {
1406         if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1407             mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1408     }
1409 
1410     return ret;
1411 }
1412 module_init(adv7511_init);
1413 
1414 static void __exit adv7511_exit(void)
1415 {
1416     i2c_del_driver(&adv7511_driver);
1417 
1418     if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1419         mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1420 }
1421 module_exit(adv7511_exit);
1422 
1423 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1424 MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1425 MODULE_LICENSE("GPL");