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