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