1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2018 Renesas Electronics 4 * 5 * Copyright (C) 2016 Atmel 6 * Bo Shen <voice.shen@atmel.com> 7 * 8 * Authors: Bo Shen <voice.shen@atmel.com> 9 * Boris Brezillon <boris.brezillon@free-electrons.com> 10 * Wu, Songjun <Songjun.Wu@atmel.com> 11 * 12 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved. 13 */ 14 15 #include <linux/gpio/consumer.h> 16 #include <linux/i2c-mux.h> 17 #include <linux/i2c.h> 18 #include <linux/media-bus-format.h> 19 #include <linux/module.h> 20 #include <linux/regmap.h> 21 #include <linux/regulator/consumer.h> 22 #include <linux/clk.h> 23 24 #include <drm/drm_atomic_helper.h> 25 #include <drm/drm_bridge.h> 26 #include <drm/drm_drv.h> 27 #include <drm/drm_edid.h> 28 #include <drm/drm_print.h> 29 #include <drm/drm_probe_helper.h> 30 31 #include <sound/hdmi-codec.h> 32 33 #define SII902X_TPI_VIDEO_DATA 0x0 34 35 #define SII902X_TPI_PIXEL_REPETITION 0x8 36 #define SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT BIT(5) 37 #define SII902X_TPI_AVI_PIXEL_REP_RISING_EDGE BIT(4) 38 #define SII902X_TPI_AVI_PIXEL_REP_4X 3 39 #define SII902X_TPI_AVI_PIXEL_REP_2X 1 40 #define SII902X_TPI_AVI_PIXEL_REP_NONE 0 41 #define SII902X_TPI_CLK_RATIO_HALF (0 << 6) 42 #define SII902X_TPI_CLK_RATIO_1X (1 << 6) 43 #define SII902X_TPI_CLK_RATIO_2X (2 << 6) 44 #define SII902X_TPI_CLK_RATIO_4X (3 << 6) 45 46 #define SII902X_TPI_AVI_IN_FORMAT 0x9 47 #define SII902X_TPI_AVI_INPUT_BITMODE_12BIT BIT(7) 48 #define SII902X_TPI_AVI_INPUT_DITHER BIT(6) 49 #define SII902X_TPI_AVI_INPUT_RANGE_LIMITED (2 << 2) 50 #define SII902X_TPI_AVI_INPUT_RANGE_FULL (1 << 2) 51 #define SII902X_TPI_AVI_INPUT_RANGE_AUTO (0 << 2) 52 #define SII902X_TPI_AVI_INPUT_COLORSPACE_BLACK (3 << 0) 53 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV422 (2 << 0) 54 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV444 (1 << 0) 55 #define SII902X_TPI_AVI_INPUT_COLORSPACE_RGB (0 << 0) 56 57 #define SII902X_TPI_AVI_INFOFRAME 0x0c 58 59 #define SII902X_SYS_CTRL_DATA 0x1a 60 #define SII902X_SYS_CTRL_PWR_DWN BIT(4) 61 #define SII902X_SYS_CTRL_AV_MUTE BIT(3) 62 #define SII902X_SYS_CTRL_DDC_BUS_REQ BIT(2) 63 #define SII902X_SYS_CTRL_DDC_BUS_GRTD BIT(1) 64 #define SII902X_SYS_CTRL_OUTPUT_MODE BIT(0) 65 #define SII902X_SYS_CTRL_OUTPUT_HDMI 1 66 #define SII902X_SYS_CTRL_OUTPUT_DVI 0 67 68 #define SII902X_REG_CHIPID(n) (0x1b + (n)) 69 70 #define SII902X_PWR_STATE_CTRL 0x1e 71 #define SII902X_AVI_POWER_STATE_MSK GENMASK(1, 0) 72 #define SII902X_AVI_POWER_STATE_D(l) ((l) & SII902X_AVI_POWER_STATE_MSK) 73 74 /* Audio */ 75 #define SII902X_TPI_I2S_ENABLE_MAPPING_REG 0x1f 76 #define SII902X_TPI_I2S_CONFIG_FIFO0 (0 << 0) 77 #define SII902X_TPI_I2S_CONFIG_FIFO1 (1 << 0) 78 #define SII902X_TPI_I2S_CONFIG_FIFO2 (2 << 0) 79 #define SII902X_TPI_I2S_CONFIG_FIFO3 (3 << 0) 80 #define SII902X_TPI_I2S_LEFT_RIGHT_SWAP (1 << 2) 81 #define SII902X_TPI_I2S_AUTO_DOWNSAMPLE (1 << 3) 82 #define SII902X_TPI_I2S_SELECT_SD0 (0 << 4) 83 #define SII902X_TPI_I2S_SELECT_SD1 (1 << 4) 84 #define SII902X_TPI_I2S_SELECT_SD2 (2 << 4) 85 #define SII902X_TPI_I2S_SELECT_SD3 (3 << 4) 86 #define SII902X_TPI_I2S_FIFO_ENABLE (1 << 7) 87 88 #define SII902X_TPI_I2S_INPUT_CONFIG_REG 0x20 89 #define SII902X_TPI_I2S_FIRST_BIT_SHIFT_YES (0 << 0) 90 #define SII902X_TPI_I2S_FIRST_BIT_SHIFT_NO (1 << 0) 91 #define SII902X_TPI_I2S_SD_DIRECTION_MSB_FIRST (0 << 1) 92 #define SII902X_TPI_I2S_SD_DIRECTION_LSB_FIRST (1 << 1) 93 #define SII902X_TPI_I2S_SD_JUSTIFY_LEFT (0 << 2) 94 #define SII902X_TPI_I2S_SD_JUSTIFY_RIGHT (1 << 2) 95 #define SII902X_TPI_I2S_WS_POLARITY_LOW (0 << 3) 96 #define SII902X_TPI_I2S_WS_POLARITY_HIGH (1 << 3) 97 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_128 (0 << 4) 98 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_256 (1 << 4) 99 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_384 (2 << 4) 100 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_512 (3 << 4) 101 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_768 (4 << 4) 102 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_1024 (5 << 4) 103 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_1152 (6 << 4) 104 #define SII902X_TPI_I2S_MCLK_MULTIPLIER_192 (7 << 4) 105 #define SII902X_TPI_I2S_SCK_EDGE_FALLING (0 << 7) 106 #define SII902X_TPI_I2S_SCK_EDGE_RISING (1 << 7) 107 108 #define SII902X_TPI_I2S_STRM_HDR_BASE 0x21 109 #define SII902X_TPI_I2S_STRM_HDR_SIZE 5 110 111 #define SII902X_TPI_AUDIO_CONFIG_BYTE2_REG 0x26 112 #define SII902X_TPI_AUDIO_CODING_STREAM_HEADER (0 << 0) 113 #define SII902X_TPI_AUDIO_CODING_PCM (1 << 0) 114 #define SII902X_TPI_AUDIO_CODING_AC3 (2 << 0) 115 #define SII902X_TPI_AUDIO_CODING_MPEG1 (3 << 0) 116 #define SII902X_TPI_AUDIO_CODING_MP3 (4 << 0) 117 #define SII902X_TPI_AUDIO_CODING_MPEG2 (5 << 0) 118 #define SII902X_TPI_AUDIO_CODING_AAC (6 << 0) 119 #define SII902X_TPI_AUDIO_CODING_DTS (7 << 0) 120 #define SII902X_TPI_AUDIO_CODING_ATRAC (8 << 0) 121 #define SII902X_TPI_AUDIO_MUTE_DISABLE (0 << 4) 122 #define SII902X_TPI_AUDIO_MUTE_ENABLE (1 << 4) 123 #define SII902X_TPI_AUDIO_LAYOUT_2_CHANNELS (0 << 5) 124 #define SII902X_TPI_AUDIO_LAYOUT_8_CHANNELS (1 << 5) 125 #define SII902X_TPI_AUDIO_INTERFACE_DISABLE (0 << 6) 126 #define SII902X_TPI_AUDIO_INTERFACE_SPDIF (1 << 6) 127 #define SII902X_TPI_AUDIO_INTERFACE_I2S (2 << 6) 128 129 #define SII902X_TPI_AUDIO_CONFIG_BYTE3_REG 0x27 130 #define SII902X_TPI_AUDIO_FREQ_STREAM (0 << 3) 131 #define SII902X_TPI_AUDIO_FREQ_32KHZ (1 << 3) 132 #define SII902X_TPI_AUDIO_FREQ_44KHZ (2 << 3) 133 #define SII902X_TPI_AUDIO_FREQ_48KHZ (3 << 3) 134 #define SII902X_TPI_AUDIO_FREQ_88KHZ (4 << 3) 135 #define SII902X_TPI_AUDIO_FREQ_96KHZ (5 << 3) 136 #define SII902X_TPI_AUDIO_FREQ_176KHZ (6 << 3) 137 #define SII902X_TPI_AUDIO_FREQ_192KHZ (7 << 3) 138 #define SII902X_TPI_AUDIO_SAMPLE_SIZE_STREAM (0 << 6) 139 #define SII902X_TPI_AUDIO_SAMPLE_SIZE_16 (1 << 6) 140 #define SII902X_TPI_AUDIO_SAMPLE_SIZE_20 (2 << 6) 141 #define SII902X_TPI_AUDIO_SAMPLE_SIZE_24 (3 << 6) 142 143 #define SII902X_TPI_AUDIO_CONFIG_BYTE4_REG 0x28 144 145 #define SII902X_INT_ENABLE 0x3c 146 #define SII902X_INT_STATUS 0x3d 147 #define SII902X_HOTPLUG_EVENT BIT(0) 148 #define SII902X_PLUGGED_STATUS BIT(2) 149 150 #define SII902X_REG_TPI_RQB 0xc7 151 152 /* Indirect internal register access */ 153 #define SII902X_IND_SET_PAGE 0xbc 154 #define SII902X_IND_OFFSET 0xbd 155 #define SII902X_IND_VALUE 0xbe 156 157 #define SII902X_TPI_MISC_INFOFRAME_BASE 0xbf 158 #define SII902X_TPI_MISC_INFOFRAME_END 0xde 159 #define SII902X_TPI_MISC_INFOFRAME_SIZE \ 160 (SII902X_TPI_MISC_INFOFRAME_END - SII902X_TPI_MISC_INFOFRAME_BASE) 161 162 #define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS 500 163 164 #define SII902X_AUDIO_PORT_INDEX 3 165 166 /* 167 * The maximum resolution supported by the HDMI bridge is 1080p@60Hz 168 * and 1920x1200 requiring a pixel clock of 165MHz and the minimum 169 * resolution supported is 480p@60Hz requiring a pixel clock of 25MHz 170 */ 171 #define SII902X_MIN_PIXEL_CLOCK_KHZ 25000 172 #define SII902X_MAX_PIXEL_CLOCK_KHZ 165000 173 174 struct sii902x { 175 struct i2c_client *i2c; 176 struct regmap *regmap; 177 struct drm_bridge bridge; 178 struct drm_connector connector; 179 struct gpio_desc *reset_gpio; 180 struct i2c_mux_core *i2cmux; 181 u32 bus_width; 182 183 /* 184 * Mutex protects audio and video functions from interfering 185 * each other, by keeping their i2c command sequences atomic. 186 */ 187 struct mutex mutex; 188 struct sii902x_audio { 189 struct platform_device *pdev; 190 struct clk *mclk; 191 u32 i2s_fifo_sequence[4]; 192 } audio; 193 }; 194 195 static int sii902x_read_unlocked(struct i2c_client *i2c, u8 reg, u8 *val) 196 { 197 union i2c_smbus_data data; 198 int ret; 199 200 ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags, 201 I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data); 202 203 if (ret < 0) 204 return ret; 205 206 *val = data.byte; 207 return 0; 208 } 209 210 static int sii902x_write_unlocked(struct i2c_client *i2c, u8 reg, u8 val) 211 { 212 union i2c_smbus_data data; 213 214 data.byte = val; 215 216 return __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags, 217 I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, 218 &data); 219 } 220 221 static int sii902x_update_bits_unlocked(struct i2c_client *i2c, u8 reg, u8 mask, 222 u8 val) 223 { 224 int ret; 225 u8 status; 226 227 ret = sii902x_read_unlocked(i2c, reg, &status); 228 if (ret) 229 return ret; 230 status &= ~mask; 231 status |= val & mask; 232 return sii902x_write_unlocked(i2c, reg, status); 233 } 234 235 static inline struct sii902x *bridge_to_sii902x(struct drm_bridge *bridge) 236 { 237 return container_of(bridge, struct sii902x, bridge); 238 } 239 240 static inline struct sii902x *connector_to_sii902x(struct drm_connector *con) 241 { 242 return container_of(con, struct sii902x, connector); 243 } 244 245 static void sii902x_reset(struct sii902x *sii902x) 246 { 247 if (!sii902x->reset_gpio) 248 return; 249 250 gpiod_set_value_cansleep(sii902x->reset_gpio, 1); 251 252 /* The datasheet says treset-min = 100us. Make it 150us to be sure. */ 253 usleep_range(150, 200); 254 255 gpiod_set_value_cansleep(sii902x->reset_gpio, 0); 256 } 257 258 static enum drm_connector_status sii902x_detect(struct sii902x *sii902x) 259 { 260 unsigned int status; 261 262 mutex_lock(&sii902x->mutex); 263 264 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status); 265 266 mutex_unlock(&sii902x->mutex); 267 268 return (status & SII902X_PLUGGED_STATUS) ? 269 connector_status_connected : connector_status_disconnected; 270 } 271 272 static enum drm_connector_status 273 sii902x_connector_detect(struct drm_connector *connector, bool force) 274 { 275 struct sii902x *sii902x = connector_to_sii902x(connector); 276 277 return sii902x_detect(sii902x); 278 } 279 280 static const struct drm_connector_funcs sii902x_connector_funcs = { 281 .detect = sii902x_connector_detect, 282 .fill_modes = drm_helper_probe_single_connector_modes, 283 .destroy = drm_connector_cleanup, 284 .reset = drm_atomic_helper_connector_reset, 285 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 286 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 287 }; 288 289 static const struct drm_edid *sii902x_edid_read(struct sii902x *sii902x, 290 struct drm_connector *connector) 291 { 292 const struct drm_edid *drm_edid; 293 294 mutex_lock(&sii902x->mutex); 295 296 drm_edid = drm_edid_read_ddc(connector, sii902x->i2cmux->adapter[0]); 297 298 mutex_unlock(&sii902x->mutex); 299 300 return drm_edid; 301 } 302 303 static int sii902x_get_modes(struct drm_connector *connector) 304 { 305 struct sii902x *sii902x = connector_to_sii902x(connector); 306 const struct drm_edid *drm_edid; 307 int num = 0; 308 309 drm_edid = sii902x_edid_read(sii902x, connector); 310 drm_edid_connector_update(connector, drm_edid); 311 if (drm_edid) { 312 num = drm_edid_connector_add_modes(connector); 313 drm_edid_free(drm_edid); 314 } 315 316 return num; 317 } 318 319 static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs = { 320 .get_modes = sii902x_get_modes, 321 }; 322 323 static void sii902x_bridge_atomic_disable(struct drm_bridge *bridge, 324 struct drm_atomic_state *state) 325 { 326 struct sii902x *sii902x = bridge_to_sii902x(bridge); 327 328 mutex_lock(&sii902x->mutex); 329 330 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, 331 SII902X_SYS_CTRL_PWR_DWN, 332 SII902X_SYS_CTRL_PWR_DWN); 333 334 mutex_unlock(&sii902x->mutex); 335 } 336 337 static void sii902x_bridge_atomic_enable(struct drm_bridge *bridge, 338 struct drm_atomic_state *state) 339 { 340 struct sii902x *sii902x = bridge_to_sii902x(bridge); 341 struct drm_connector *connector; 342 u8 output_mode = SII902X_SYS_CTRL_OUTPUT_DVI; 343 344 connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder); 345 if (connector && connector->display_info.is_hdmi) 346 output_mode = SII902X_SYS_CTRL_OUTPUT_HDMI; 347 348 mutex_lock(&sii902x->mutex); 349 350 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, 351 SII902X_SYS_CTRL_OUTPUT_MODE, output_mode); 352 regmap_update_bits(sii902x->regmap, SII902X_PWR_STATE_CTRL, 353 SII902X_AVI_POWER_STATE_MSK, 354 SII902X_AVI_POWER_STATE_D(0)); 355 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, 356 SII902X_SYS_CTRL_PWR_DWN, 0); 357 358 mutex_unlock(&sii902x->mutex); 359 } 360 361 static void sii902x_bridge_mode_set(struct drm_bridge *bridge, 362 const struct drm_display_mode *mode, 363 const struct drm_display_mode *adj) 364 { 365 struct sii902x *sii902x = bridge_to_sii902x(bridge); 366 struct regmap *regmap = sii902x->regmap; 367 u8 buf[HDMI_INFOFRAME_SIZE(AVI)]; 368 struct hdmi_avi_infoframe frame; 369 u16 pixel_clock_10kHz = adj->clock / 10; 370 int ret; 371 372 buf[0] = pixel_clock_10kHz & 0xff; 373 buf[1] = pixel_clock_10kHz >> 8; 374 buf[2] = drm_mode_vrefresh(adj); 375 buf[3] = 0x00; 376 buf[4] = adj->hdisplay; 377 buf[5] = adj->hdisplay >> 8; 378 buf[6] = adj->vdisplay; 379 buf[7] = adj->vdisplay >> 8; 380 buf[8] = SII902X_TPI_CLK_RATIO_1X | SII902X_TPI_AVI_PIXEL_REP_NONE | 381 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT; 382 buf[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO | 383 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB; 384 385 mutex_lock(&sii902x->mutex); 386 387 ret = regmap_bulk_write(regmap, SII902X_TPI_VIDEO_DATA, buf, 10); 388 if (ret) 389 goto out; 390 391 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, 392 &sii902x->connector, adj); 393 if (ret < 0) { 394 DRM_ERROR("couldn't fill AVI infoframe\n"); 395 goto out; 396 } 397 398 ret = hdmi_avi_infoframe_pack(&frame, buf, sizeof(buf)); 399 if (ret < 0) { 400 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret); 401 goto out; 402 } 403 404 /* Do not send the infoframe header, but keep the CRC field. */ 405 regmap_bulk_write(regmap, SII902X_TPI_AVI_INFOFRAME, 406 buf + HDMI_INFOFRAME_HEADER_SIZE - 1, 407 HDMI_AVI_INFOFRAME_SIZE + 1); 408 409 out: 410 mutex_unlock(&sii902x->mutex); 411 } 412 413 static int sii902x_bridge_attach(struct drm_bridge *bridge, 414 struct drm_encoder *encoder, 415 enum drm_bridge_attach_flags flags) 416 { 417 struct sii902x *sii902x = bridge_to_sii902x(bridge); 418 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24; 419 struct drm_device *drm = bridge->dev; 420 int ret; 421 422 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) 423 return drm_bridge_attach(encoder, sii902x->bridge.next_bridge, 424 bridge, flags); 425 426 drm_connector_helper_add(&sii902x->connector, 427 &sii902x_connector_helper_funcs); 428 429 if (!drm_core_check_feature(drm, DRIVER_ATOMIC)) { 430 dev_err(&sii902x->i2c->dev, 431 "sii902x driver is only compatible with DRM devices supporting atomic updates\n"); 432 return -ENOTSUPP; 433 } 434 435 ret = drm_connector_init(drm, &sii902x->connector, 436 &sii902x_connector_funcs, 437 DRM_MODE_CONNECTOR_HDMIA); 438 if (ret) 439 return ret; 440 441 if (sii902x->i2c->irq > 0) 442 sii902x->connector.polled = DRM_CONNECTOR_POLL_HPD; 443 else 444 sii902x->connector.polled = DRM_CONNECTOR_POLL_CONNECT; 445 446 ret = drm_display_info_set_bus_formats(&sii902x->connector.display_info, 447 &bus_format, 1); 448 if (ret) 449 return ret; 450 451 drm_connector_attach_encoder(&sii902x->connector, encoder); 452 453 return 0; 454 } 455 456 static enum drm_connector_status 457 sii902x_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector) 458 { 459 struct sii902x *sii902x = bridge_to_sii902x(bridge); 460 461 return sii902x_detect(sii902x); 462 } 463 464 static const struct drm_edid *sii902x_bridge_edid_read(struct drm_bridge *bridge, 465 struct drm_connector *connector) 466 { 467 struct sii902x *sii902x = bridge_to_sii902x(bridge); 468 469 return sii902x_edid_read(sii902x, connector); 470 } 471 472 static u32 *sii902x_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge, 473 struct drm_bridge_state *bridge_state, 474 struct drm_crtc_state *crtc_state, 475 struct drm_connector_state *conn_state, 476 u32 output_fmt, 477 unsigned int *num_input_fmts) 478 { 479 480 struct sii902x *sii902x = bridge_to_sii902x(bridge); 481 u32 *input_fmts; 482 483 *num_input_fmts = 0; 484 485 input_fmts = kcalloc(1, sizeof(*input_fmts), GFP_KERNEL); 486 if (!input_fmts) 487 return NULL; 488 489 switch (sii902x->bus_width) { 490 case 16: 491 input_fmts[0] = MEDIA_BUS_FMT_RGB565_1X16; 492 break; 493 case 18: 494 input_fmts[0] = MEDIA_BUS_FMT_RGB666_1X18; 495 break; 496 case 24: 497 input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24; 498 break; 499 default: 500 return NULL; 501 } 502 503 *num_input_fmts = 1; 504 505 return input_fmts; 506 } 507 508 static int sii902x_bridge_atomic_check(struct drm_bridge *bridge, 509 struct drm_bridge_state *bridge_state, 510 struct drm_crtc_state *crtc_state, 511 struct drm_connector_state *conn_state) 512 { 513 if (crtc_state->mode.clock < SII902X_MIN_PIXEL_CLOCK_KHZ || 514 crtc_state->mode.clock > SII902X_MAX_PIXEL_CLOCK_KHZ) 515 return -EINVAL; 516 517 /* 518 * There might be flags negotiation supported in future but 519 * set the bus flags in atomic_check statically for now. 520 */ 521 bridge_state->input_bus_cfg.flags = bridge->timings->input_bus_flags; 522 523 return 0; 524 } 525 526 static enum drm_mode_status 527 sii902x_bridge_mode_valid(struct drm_bridge *bridge, 528 const struct drm_display_info *info, 529 const struct drm_display_mode *mode) 530 { 531 if (mode->clock < SII902X_MIN_PIXEL_CLOCK_KHZ) 532 return MODE_CLOCK_LOW; 533 534 if (mode->clock > SII902X_MAX_PIXEL_CLOCK_KHZ) 535 return MODE_CLOCK_HIGH; 536 537 return MODE_OK; 538 } 539 540 static const struct drm_bridge_funcs sii902x_bridge_funcs = { 541 .attach = sii902x_bridge_attach, 542 .mode_set = sii902x_bridge_mode_set, 543 .atomic_disable = sii902x_bridge_atomic_disable, 544 .atomic_enable = sii902x_bridge_atomic_enable, 545 .detect = sii902x_bridge_detect, 546 .edid_read = sii902x_bridge_edid_read, 547 .atomic_reset = drm_atomic_helper_bridge_reset, 548 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 549 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 550 .atomic_get_input_bus_fmts = sii902x_bridge_atomic_get_input_bus_fmts, 551 .atomic_check = sii902x_bridge_atomic_check, 552 .mode_valid = sii902x_bridge_mode_valid, 553 }; 554 555 static int sii902x_mute(struct sii902x *sii902x, bool mute) 556 { 557 struct device *dev = &sii902x->i2c->dev; 558 unsigned int val = mute ? SII902X_TPI_AUDIO_MUTE_ENABLE : 559 SII902X_TPI_AUDIO_MUTE_DISABLE; 560 561 dev_dbg(dev, "%s: %s\n", __func__, mute ? "Muted" : "Unmuted"); 562 563 return regmap_update_bits(sii902x->regmap, 564 SII902X_TPI_AUDIO_CONFIG_BYTE2_REG, 565 SII902X_TPI_AUDIO_MUTE_ENABLE, val); 566 } 567 568 static const int sii902x_mclk_div_table[] = { 569 128, 256, 384, 512, 768, 1024, 1152, 192 }; 570 571 static int sii902x_select_mclk_div(u8 *i2s_config_reg, unsigned int rate, 572 unsigned int mclk) 573 { 574 int div = mclk / rate; 575 int distance = 100000; 576 u8 i, nearest = 0; 577 578 for (i = 0; i < ARRAY_SIZE(sii902x_mclk_div_table); i++) { 579 unsigned int d = abs(div - sii902x_mclk_div_table[i]); 580 581 if (d >= distance) 582 continue; 583 584 nearest = i; 585 distance = d; 586 if (d == 0) 587 break; 588 } 589 590 *i2s_config_reg |= nearest << 4; 591 592 return sii902x_mclk_div_table[nearest]; 593 } 594 595 static const struct sii902x_sample_freq { 596 u32 freq; 597 u8 val; 598 } sii902x_sample_freq[] = { 599 { .freq = 32000, .val = SII902X_TPI_AUDIO_FREQ_32KHZ }, 600 { .freq = 44000, .val = SII902X_TPI_AUDIO_FREQ_44KHZ }, 601 { .freq = 48000, .val = SII902X_TPI_AUDIO_FREQ_48KHZ }, 602 { .freq = 88000, .val = SII902X_TPI_AUDIO_FREQ_88KHZ }, 603 { .freq = 96000, .val = SII902X_TPI_AUDIO_FREQ_96KHZ }, 604 { .freq = 176000, .val = SII902X_TPI_AUDIO_FREQ_176KHZ }, 605 { .freq = 192000, .val = SII902X_TPI_AUDIO_FREQ_192KHZ }, 606 }; 607 608 static int sii902x_audio_hw_params(struct device *dev, void *data, 609 struct hdmi_codec_daifmt *daifmt, 610 struct hdmi_codec_params *params) 611 { 612 struct sii902x *sii902x = dev_get_drvdata(dev); 613 u8 i2s_config_reg = SII902X_TPI_I2S_SD_DIRECTION_MSB_FIRST; 614 u8 config_byte2_reg = (SII902X_TPI_AUDIO_INTERFACE_I2S | 615 SII902X_TPI_AUDIO_MUTE_ENABLE | 616 SII902X_TPI_AUDIO_CODING_PCM); 617 u8 config_byte3_reg = 0; 618 u8 infoframe_buf[HDMI_INFOFRAME_SIZE(AUDIO)]; 619 unsigned long mclk_rate; 620 int i, ret; 621 622 if (daifmt->bit_clk_provider || daifmt->frame_clk_provider) { 623 dev_dbg(dev, "%s: I2S clock provider mode not supported\n", 624 __func__); 625 return -EINVAL; 626 } 627 628 switch (daifmt->fmt) { 629 case HDMI_I2S: 630 i2s_config_reg |= SII902X_TPI_I2S_FIRST_BIT_SHIFT_YES | 631 SII902X_TPI_I2S_SD_JUSTIFY_LEFT; 632 break; 633 case HDMI_RIGHT_J: 634 i2s_config_reg |= SII902X_TPI_I2S_SD_JUSTIFY_RIGHT; 635 break; 636 case HDMI_LEFT_J: 637 i2s_config_reg |= SII902X_TPI_I2S_SD_JUSTIFY_LEFT; 638 break; 639 default: 640 dev_dbg(dev, "%s: Unsupported i2s format %u\n", __func__, 641 daifmt->fmt); 642 return -EINVAL; 643 } 644 645 if (daifmt->bit_clk_inv) 646 i2s_config_reg |= SII902X_TPI_I2S_SCK_EDGE_FALLING; 647 else 648 i2s_config_reg |= SII902X_TPI_I2S_SCK_EDGE_RISING; 649 650 if (daifmt->frame_clk_inv) 651 i2s_config_reg |= SII902X_TPI_I2S_WS_POLARITY_LOW; 652 else 653 i2s_config_reg |= SII902X_TPI_I2S_WS_POLARITY_HIGH; 654 655 if (params->channels > 2) 656 config_byte2_reg |= SII902X_TPI_AUDIO_LAYOUT_8_CHANNELS; 657 else 658 config_byte2_reg |= SII902X_TPI_AUDIO_LAYOUT_2_CHANNELS; 659 660 switch (params->sample_width) { 661 case 16: 662 config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_16; 663 break; 664 case 20: 665 config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_20; 666 break; 667 case 24: 668 case 32: 669 config_byte3_reg |= SII902X_TPI_AUDIO_SAMPLE_SIZE_24; 670 break; 671 default: 672 dev_err(dev, "%s: Unsupported sample width %u\n", __func__, 673 params->sample_width); 674 return -EINVAL; 675 } 676 677 for (i = 0; i < ARRAY_SIZE(sii902x_sample_freq); i++) { 678 if (params->sample_rate == sii902x_sample_freq[i].freq) { 679 config_byte3_reg |= sii902x_sample_freq[i].val; 680 break; 681 } 682 } 683 684 ret = clk_prepare_enable(sii902x->audio.mclk); 685 if (ret) { 686 dev_err(dev, "Enabling mclk failed: %d\n", ret); 687 return ret; 688 } 689 690 if (sii902x->audio.mclk) { 691 mclk_rate = clk_get_rate(sii902x->audio.mclk); 692 ret = sii902x_select_mclk_div(&i2s_config_reg, 693 params->sample_rate, mclk_rate); 694 if (mclk_rate != ret * params->sample_rate) 695 dev_dbg(dev, "Inaccurate reference clock (%ld/%d != %u)\n", 696 mclk_rate, ret, params->sample_rate); 697 } 698 699 mutex_lock(&sii902x->mutex); 700 701 ret = regmap_write(sii902x->regmap, 702 SII902X_TPI_AUDIO_CONFIG_BYTE2_REG, 703 config_byte2_reg); 704 if (ret < 0) 705 goto out; 706 707 ret = regmap_write(sii902x->regmap, SII902X_TPI_I2S_INPUT_CONFIG_REG, 708 i2s_config_reg); 709 if (ret) 710 goto out; 711 712 for (i = 0; i < ARRAY_SIZE(sii902x->audio.i2s_fifo_sequence) && 713 sii902x->audio.i2s_fifo_sequence[i]; i++) 714 regmap_write(sii902x->regmap, 715 SII902X_TPI_I2S_ENABLE_MAPPING_REG, 716 sii902x->audio.i2s_fifo_sequence[i]); 717 718 ret = regmap_write(sii902x->regmap, SII902X_TPI_AUDIO_CONFIG_BYTE3_REG, 719 config_byte3_reg); 720 if (ret) 721 goto out; 722 723 ret = regmap_bulk_write(sii902x->regmap, SII902X_TPI_I2S_STRM_HDR_BASE, 724 params->iec.status, 725 min((size_t) SII902X_TPI_I2S_STRM_HDR_SIZE, 726 sizeof(params->iec.status))); 727 if (ret) 728 goto out; 729 730 ret = hdmi_audio_infoframe_pack(¶ms->cea, infoframe_buf, 731 sizeof(infoframe_buf)); 732 if (ret < 0) { 733 dev_err(dev, "%s: Failed to pack audio infoframe: %d\n", 734 __func__, ret); 735 goto out; 736 } 737 738 ret = regmap_bulk_write(sii902x->regmap, 739 SII902X_TPI_MISC_INFOFRAME_BASE, 740 infoframe_buf, 741 min(ret, SII902X_TPI_MISC_INFOFRAME_SIZE)); 742 if (ret) 743 goto out; 744 745 /* Decode Level 0 Packets */ 746 ret = regmap_write(sii902x->regmap, SII902X_IND_SET_PAGE, 0x02); 747 if (ret) 748 goto out; 749 750 ret = regmap_write(sii902x->regmap, SII902X_IND_OFFSET, 0x24); 751 if (ret) 752 goto out; 753 754 ret = regmap_write(sii902x->regmap, SII902X_IND_VALUE, 0x02); 755 if (ret) 756 goto out; 757 758 dev_dbg(dev, "%s: hdmi audio enabled\n", __func__); 759 out: 760 mutex_unlock(&sii902x->mutex); 761 762 if (ret) { 763 clk_disable_unprepare(sii902x->audio.mclk); 764 dev_err(dev, "%s: hdmi audio enable failed: %d\n", __func__, 765 ret); 766 } 767 768 return ret; 769 } 770 771 static void sii902x_audio_shutdown(struct device *dev, void *data) 772 { 773 struct sii902x *sii902x = dev_get_drvdata(dev); 774 775 mutex_lock(&sii902x->mutex); 776 777 regmap_write(sii902x->regmap, SII902X_TPI_AUDIO_CONFIG_BYTE2_REG, 778 SII902X_TPI_AUDIO_INTERFACE_DISABLE); 779 780 mutex_unlock(&sii902x->mutex); 781 782 clk_disable_unprepare(sii902x->audio.mclk); 783 } 784 785 static int sii902x_audio_mute(struct device *dev, void *data, 786 bool enable, int direction) 787 { 788 struct sii902x *sii902x = dev_get_drvdata(dev); 789 790 mutex_lock(&sii902x->mutex); 791 792 sii902x_mute(sii902x, enable); 793 794 mutex_unlock(&sii902x->mutex); 795 796 return 0; 797 } 798 799 static int sii902x_audio_get_eld(struct device *dev, void *data, 800 uint8_t *buf, size_t len) 801 { 802 struct sii902x *sii902x = dev_get_drvdata(dev); 803 804 mutex_lock(&sii902x->mutex); 805 806 memcpy(buf, sii902x->connector.eld, 807 min(sizeof(sii902x->connector.eld), len)); 808 809 mutex_unlock(&sii902x->mutex); 810 811 return 0; 812 } 813 814 static int sii902x_audio_get_dai_id(struct snd_soc_component *component, 815 struct device_node *endpoint, 816 void *data) 817 { 818 struct of_endpoint of_ep; 819 int ret; 820 821 ret = of_graph_parse_endpoint(endpoint, &of_ep); 822 if (ret < 0) 823 return ret; 824 825 /* 826 * HDMI sound should be located at reg = <3> 827 * Return expected DAI index 0. 828 */ 829 if (of_ep.port == SII902X_AUDIO_PORT_INDEX) 830 return 0; 831 832 return -EINVAL; 833 } 834 835 static const struct hdmi_codec_ops sii902x_audio_codec_ops = { 836 .hw_params = sii902x_audio_hw_params, 837 .audio_shutdown = sii902x_audio_shutdown, 838 .mute_stream = sii902x_audio_mute, 839 .get_eld = sii902x_audio_get_eld, 840 .get_dai_id = sii902x_audio_get_dai_id, 841 }; 842 843 static int sii902x_audio_codec_init(struct sii902x *sii902x, 844 struct device *dev) 845 { 846 static const u8 audio_fifo_id[] = { 847 SII902X_TPI_I2S_CONFIG_FIFO0, 848 SII902X_TPI_I2S_CONFIG_FIFO1, 849 SII902X_TPI_I2S_CONFIG_FIFO2, 850 SII902X_TPI_I2S_CONFIG_FIFO3, 851 }; 852 static const u8 i2s_lane_id[] = { 853 SII902X_TPI_I2S_SELECT_SD0, 854 SII902X_TPI_I2S_SELECT_SD1, 855 SII902X_TPI_I2S_SELECT_SD2, 856 SII902X_TPI_I2S_SELECT_SD3, 857 }; 858 struct hdmi_codec_pdata codec_data = { 859 .ops = &sii902x_audio_codec_ops, 860 .i2s = 1, /* Only i2s support for now. */ 861 .spdif = 0, 862 .max_i2s_channels = 0, 863 .no_capture_mute = 1, 864 }; 865 u8 lanes[4]; 866 int num_lanes, i; 867 868 if (!of_property_present(dev->of_node, "#sound-dai-cells")) { 869 dev_dbg(dev, "%s: No \"#sound-dai-cells\", no audio\n", 870 __func__); 871 return 0; 872 } 873 874 num_lanes = of_property_read_variable_u8_array(dev->of_node, 875 "sil,i2s-data-lanes", 876 lanes, 1, 877 ARRAY_SIZE(lanes)); 878 879 if (num_lanes == -EINVAL) { 880 dev_dbg(dev, 881 "%s: No \"sil,i2s-data-lanes\", use default <0>\n", 882 __func__); 883 num_lanes = 1; 884 lanes[0] = 0; 885 } else if (num_lanes < 0) { 886 dev_err(dev, 887 "%s: Error getting \"sil,i2s-data-lanes\": %d\n", 888 __func__, num_lanes); 889 return num_lanes; 890 } 891 codec_data.max_i2s_channels = 2 * num_lanes; 892 893 for (i = 0; i < num_lanes; i++) 894 sii902x->audio.i2s_fifo_sequence[i] |= audio_fifo_id[i] | 895 i2s_lane_id[lanes[i]] | SII902X_TPI_I2S_FIFO_ENABLE; 896 897 sii902x->audio.mclk = devm_clk_get_optional(dev, "mclk"); 898 if (IS_ERR(sii902x->audio.mclk)) { 899 dev_err(dev, "%s: No clock (audio mclk) found: %ld\n", 900 __func__, PTR_ERR(sii902x->audio.mclk)); 901 return PTR_ERR(sii902x->audio.mclk); 902 } 903 904 sii902x->audio.pdev = platform_device_register_data( 905 dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO, 906 &codec_data, sizeof(codec_data)); 907 908 return PTR_ERR_OR_ZERO(sii902x->audio.pdev); 909 } 910 911 static const struct regmap_range sii902x_volatile_ranges[] = { 912 { .range_min = 0, .range_max = 0xff }, 913 }; 914 915 static const struct regmap_access_table sii902x_volatile_table = { 916 .yes_ranges = sii902x_volatile_ranges, 917 .n_yes_ranges = ARRAY_SIZE(sii902x_volatile_ranges), 918 }; 919 920 static const struct regmap_config sii902x_regmap_config = { 921 .reg_bits = 8, 922 .val_bits = 8, 923 .disable_locking = true, /* struct sii902x mutex should be enough */ 924 .max_register = SII902X_TPI_MISC_INFOFRAME_END, 925 .volatile_table = &sii902x_volatile_table, 926 .cache_type = REGCACHE_NONE, 927 }; 928 929 static irqreturn_t sii902x_interrupt(int irq, void *data) 930 { 931 struct sii902x *sii902x = data; 932 unsigned int status = 0; 933 934 mutex_lock(&sii902x->mutex); 935 936 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status); 937 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status); 938 939 mutex_unlock(&sii902x->mutex); 940 941 if ((status & SII902X_HOTPLUG_EVENT) && sii902x->bridge.dev) { 942 drm_helper_hpd_irq_event(sii902x->bridge.dev); 943 drm_bridge_hpd_notify(&sii902x->bridge, (status & SII902X_PLUGGED_STATUS) 944 ? connector_status_connected 945 : connector_status_disconnected); 946 } 947 948 return IRQ_HANDLED; 949 } 950 951 /* 952 * The purpose of sii902x_i2c_bypass_select is to enable the pass through 953 * mode of the HDMI transmitter. Do not use regmap from within this function, 954 * only use sii902x_*_unlocked functions to read/modify/write registers. 955 * We are holding the parent adapter lock here, keep this in mind before 956 * adding more i2c transactions. 957 * 958 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere 959 * in this driver, we need to make sure that we only touch 0x1A[2:1] from 960 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that 961 * we leave the remaining bits as we have found them. 962 */ 963 static int sii902x_i2c_bypass_select(struct i2c_mux_core *mux, u32 chan_id) 964 { 965 struct sii902x *sii902x = i2c_mux_priv(mux); 966 struct device *dev = &sii902x->i2c->dev; 967 unsigned long timeout; 968 u8 status; 969 int ret; 970 971 ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 972 SII902X_SYS_CTRL_DDC_BUS_REQ, 973 SII902X_SYS_CTRL_DDC_BUS_REQ); 974 if (ret) 975 return ret; 976 977 timeout = jiffies + 978 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS); 979 do { 980 ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 981 &status); 982 if (ret) 983 return ret; 984 } while (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD) && 985 time_before(jiffies, timeout)); 986 987 if (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD)) { 988 dev_err(dev, "Failed to acquire the i2c bus\n"); 989 return -ETIMEDOUT; 990 } 991 992 return sii902x_write_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 993 status); 994 } 995 996 /* 997 * The purpose of sii902x_i2c_bypass_deselect is to disable the pass through 998 * mode of the HDMI transmitter. Do not use regmap from within this function, 999 * only use sii902x_*_unlocked functions to read/modify/write registers. 1000 * We are holding the parent adapter lock here, keep this in mind before 1001 * adding more i2c transactions. 1002 * 1003 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere 1004 * in this driver, we need to make sure that we only touch 0x1A[2:1] from 1005 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that 1006 * we leave the remaining bits as we have found them. 1007 */ 1008 static int sii902x_i2c_bypass_deselect(struct i2c_mux_core *mux, u32 chan_id) 1009 { 1010 struct sii902x *sii902x = i2c_mux_priv(mux); 1011 struct device *dev = &sii902x->i2c->dev; 1012 unsigned long timeout; 1013 unsigned int retries; 1014 u8 status; 1015 int ret; 1016 1017 /* 1018 * When the HDMI transmitter is in pass through mode, we need an 1019 * (undocumented) additional delay between STOP and START conditions 1020 * to guarantee the bus won't get stuck. 1021 */ 1022 udelay(30); 1023 1024 /* 1025 * Sometimes the I2C bus can stall after failure to use the 1026 * EDID channel. Retry a few times to see if things clear 1027 * up, else continue anyway. 1028 */ 1029 retries = 5; 1030 do { 1031 ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 1032 &status); 1033 retries--; 1034 } while (ret && retries); 1035 if (ret) { 1036 dev_err(dev, "failed to read status (%d)\n", ret); 1037 return ret; 1038 } 1039 1040 ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 1041 SII902X_SYS_CTRL_DDC_BUS_REQ | 1042 SII902X_SYS_CTRL_DDC_BUS_GRTD, 0); 1043 if (ret) 1044 return ret; 1045 1046 timeout = jiffies + 1047 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS); 1048 do { 1049 ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 1050 &status); 1051 if (ret) 1052 return ret; 1053 } while (status & (SII902X_SYS_CTRL_DDC_BUS_REQ | 1054 SII902X_SYS_CTRL_DDC_BUS_GRTD) && 1055 time_before(jiffies, timeout)); 1056 1057 if (status & (SII902X_SYS_CTRL_DDC_BUS_REQ | 1058 SII902X_SYS_CTRL_DDC_BUS_GRTD)) { 1059 dev_err(dev, "failed to release the i2c bus\n"); 1060 return -ETIMEDOUT; 1061 } 1062 1063 return 0; 1064 } 1065 1066 static const struct drm_bridge_timings default_sii902x_timings = { 1067 .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE 1068 | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE 1069 | DRM_BUS_FLAG_DE_HIGH, 1070 }; 1071 1072 static int sii902x_init(struct sii902x *sii902x) 1073 { 1074 struct device *dev = &sii902x->i2c->dev; 1075 unsigned int status = 0; 1076 u8 chipid[4]; 1077 int ret; 1078 1079 sii902x_reset(sii902x); 1080 1081 ret = regmap_write(sii902x->regmap, SII902X_REG_TPI_RQB, 0x0); 1082 if (ret) 1083 return ret; 1084 1085 ret = regmap_bulk_read(sii902x->regmap, SII902X_REG_CHIPID(0), 1086 &chipid, 4); 1087 if (ret) { 1088 dev_err(dev, "regmap_read failed %d\n", ret); 1089 return ret; 1090 } 1091 1092 if (chipid[0] != 0xb0) { 1093 dev_err(dev, "Invalid chipid: %02x (expecting 0xb0)\n", 1094 chipid[0]); 1095 return -EINVAL; 1096 } 1097 1098 /* Clear all pending interrupts */ 1099 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status); 1100 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status); 1101 1102 if (sii902x->i2c->irq > 0) { 1103 regmap_write(sii902x->regmap, SII902X_INT_ENABLE, 1104 SII902X_HOTPLUG_EVENT); 1105 1106 ret = devm_request_threaded_irq(dev, sii902x->i2c->irq, NULL, 1107 sii902x_interrupt, 1108 IRQF_ONESHOT, dev_name(dev), 1109 sii902x); 1110 if (ret) 1111 return ret; 1112 } 1113 1114 ret = sii902x_audio_codec_init(sii902x, dev); 1115 if (ret) 1116 return ret; 1117 1118 i2c_set_clientdata(sii902x->i2c, sii902x); 1119 1120 sii902x->i2cmux = i2c_mux_alloc(sii902x->i2c->adapter, dev, 1121 1, 0, I2C_MUX_GATE, 1122 sii902x_i2c_bypass_select, 1123 sii902x_i2c_bypass_deselect); 1124 if (!sii902x->i2cmux) { 1125 ret = -ENOMEM; 1126 goto err_unreg_audio; 1127 } 1128 1129 sii902x->i2cmux->priv = sii902x; 1130 ret = i2c_mux_add_adapter(sii902x->i2cmux, 0, 0); 1131 if (ret) 1132 goto err_unreg_audio; 1133 1134 sii902x->bridge.of_node = dev->of_node; 1135 sii902x->bridge.timings = &default_sii902x_timings; 1136 sii902x->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID; 1137 sii902x->bridge.type = DRM_MODE_CONNECTOR_HDMIA; 1138 1139 if (sii902x->i2c->irq > 0) 1140 sii902x->bridge.ops |= DRM_BRIDGE_OP_HPD; 1141 1142 drm_bridge_add(&sii902x->bridge); 1143 1144 return 0; 1145 1146 err_unreg_audio: 1147 if (!PTR_ERR_OR_ZERO(sii902x->audio.pdev)) 1148 platform_device_unregister(sii902x->audio.pdev); 1149 1150 return ret; 1151 } 1152 1153 static int sii902x_probe(struct i2c_client *client) 1154 { 1155 struct device *dev = &client->dev; 1156 struct device_node *endpoint; 1157 struct sii902x *sii902x; 1158 static const char * const supplies[] = {"iovcc", "cvcc12"}; 1159 int ret; 1160 1161 ret = i2c_check_functionality(client->adapter, 1162 I2C_FUNC_SMBUS_BYTE_DATA); 1163 if (!ret) { 1164 dev_err(dev, "I2C adapter not suitable\n"); 1165 return -EIO; 1166 } 1167 1168 sii902x = devm_drm_bridge_alloc(dev, struct sii902x, bridge, &sii902x_bridge_funcs); 1169 if (IS_ERR(sii902x)) 1170 return PTR_ERR(sii902x); 1171 1172 sii902x->i2c = client; 1173 sii902x->regmap = devm_regmap_init_i2c(client, &sii902x_regmap_config); 1174 if (IS_ERR(sii902x->regmap)) 1175 return PTR_ERR(sii902x->regmap); 1176 1177 sii902x->reset_gpio = devm_gpiod_get_optional(dev, "reset", 1178 GPIOD_OUT_LOW); 1179 if (IS_ERR(sii902x->reset_gpio)) { 1180 dev_err(dev, "Failed to retrieve/request reset gpio: %ld\n", 1181 PTR_ERR(sii902x->reset_gpio)); 1182 return PTR_ERR(sii902x->reset_gpio); 1183 } 1184 1185 sii902x->bus_width = 24; 1186 endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1); 1187 if (endpoint) 1188 of_property_read_u32(endpoint, "bus-width", &sii902x->bus_width); 1189 1190 endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 1, -1); 1191 if (endpoint) { 1192 struct device_node *remote = of_graph_get_remote_port_parent(endpoint); 1193 1194 of_node_put(endpoint); 1195 if (!remote) { 1196 dev_err(dev, "Endpoint in port@1 unconnected\n"); 1197 return -ENODEV; 1198 } 1199 1200 if (!of_device_is_available(remote)) { 1201 dev_err(dev, "port@1 remote device is disabled\n"); 1202 of_node_put(remote); 1203 return -ENODEV; 1204 } 1205 1206 sii902x->bridge.next_bridge = of_drm_find_and_get_bridge(remote); 1207 of_node_put(remote); 1208 if (!sii902x->bridge.next_bridge) 1209 return dev_err_probe(dev, -EPROBE_DEFER, 1210 "Failed to find remote bridge\n"); 1211 } 1212 1213 mutex_init(&sii902x->mutex); 1214 1215 ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(supplies), supplies); 1216 if (ret < 0) 1217 return dev_err_probe(dev, ret, "Failed to enable supplies"); 1218 1219 return sii902x_init(sii902x); 1220 } 1221 1222 static void sii902x_remove(struct i2c_client *client) 1223 { 1224 struct sii902x *sii902x = i2c_get_clientdata(client); 1225 1226 drm_bridge_remove(&sii902x->bridge); 1227 i2c_mux_del_adapters(sii902x->i2cmux); 1228 1229 if (!PTR_ERR_OR_ZERO(sii902x->audio.pdev)) 1230 platform_device_unregister(sii902x->audio.pdev); 1231 } 1232 1233 static const struct of_device_id sii902x_dt_ids[] = { 1234 { .compatible = "sil,sii9022", }, 1235 { } 1236 }; 1237 MODULE_DEVICE_TABLE(of, sii902x_dt_ids); 1238 1239 static const struct i2c_device_id sii902x_i2c_ids[] = { 1240 { "sii9022" }, 1241 { } 1242 }; 1243 MODULE_DEVICE_TABLE(i2c, sii902x_i2c_ids); 1244 1245 static struct i2c_driver sii902x_driver = { 1246 .probe = sii902x_probe, 1247 .remove = sii902x_remove, 1248 .driver = { 1249 .name = "sii902x", 1250 .of_match_table = sii902x_dt_ids, 1251 }, 1252 .id_table = sii902x_i2c_ids, 1253 }; 1254 module_i2c_driver(sii902x_driver); 1255 1256 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>"); 1257 MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges"); 1258 MODULE_LICENSE("GPL"); 1259