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 31 #include <drm/drm_atomic_helper.h> 32 #include <drm/drm_drv.h> 33 #include <drm/drm_edid.h> 34 #include <drm/drm_print.h> 35 #include <drm/drm_probe_helper.h> 36 37 #define SII902X_TPI_VIDEO_DATA 0x0 38 39 #define SII902X_TPI_PIXEL_REPETITION 0x8 40 #define SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT BIT(5) 41 #define SII902X_TPI_AVI_PIXEL_REP_RISING_EDGE BIT(4) 42 #define SII902X_TPI_AVI_PIXEL_REP_4X 3 43 #define SII902X_TPI_AVI_PIXEL_REP_2X 1 44 #define SII902X_TPI_AVI_PIXEL_REP_NONE 0 45 #define SII902X_TPI_CLK_RATIO_HALF (0 << 6) 46 #define SII902X_TPI_CLK_RATIO_1X (1 << 6) 47 #define SII902X_TPI_CLK_RATIO_2X (2 << 6) 48 #define SII902X_TPI_CLK_RATIO_4X (3 << 6) 49 50 #define SII902X_TPI_AVI_IN_FORMAT 0x9 51 #define SII902X_TPI_AVI_INPUT_BITMODE_12BIT BIT(7) 52 #define SII902X_TPI_AVI_INPUT_DITHER BIT(6) 53 #define SII902X_TPI_AVI_INPUT_RANGE_LIMITED (2 << 2) 54 #define SII902X_TPI_AVI_INPUT_RANGE_FULL (1 << 2) 55 #define SII902X_TPI_AVI_INPUT_RANGE_AUTO (0 << 2) 56 #define SII902X_TPI_AVI_INPUT_COLORSPACE_BLACK (3 << 0) 57 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV422 (2 << 0) 58 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV444 (1 << 0) 59 #define SII902X_TPI_AVI_INPUT_COLORSPACE_RGB (0 << 0) 60 61 #define SII902X_TPI_AVI_INFOFRAME 0x0c 62 63 #define SII902X_SYS_CTRL_DATA 0x1a 64 #define SII902X_SYS_CTRL_PWR_DWN BIT(4) 65 #define SII902X_SYS_CTRL_AV_MUTE BIT(3) 66 #define SII902X_SYS_CTRL_DDC_BUS_REQ BIT(2) 67 #define SII902X_SYS_CTRL_DDC_BUS_GRTD BIT(1) 68 #define SII902X_SYS_CTRL_OUTPUT_MODE BIT(0) 69 #define SII902X_SYS_CTRL_OUTPUT_HDMI 1 70 #define SII902X_SYS_CTRL_OUTPUT_DVI 0 71 72 #define SII902X_REG_CHIPID(n) (0x1b + (n)) 73 74 #define SII902X_PWR_STATE_CTRL 0x1e 75 #define SII902X_AVI_POWER_STATE_MSK GENMASK(1, 0) 76 #define SII902X_AVI_POWER_STATE_D(l) ((l) & SII902X_AVI_POWER_STATE_MSK) 77 78 #define SII902X_INT_ENABLE 0x3c 79 #define SII902X_INT_STATUS 0x3d 80 #define SII902X_HOTPLUG_EVENT BIT(0) 81 #define SII902X_PLUGGED_STATUS BIT(2) 82 83 #define SII902X_REG_TPI_RQB 0xc7 84 85 #define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS 500 86 87 struct sii902x { 88 struct i2c_client *i2c; 89 struct regmap *regmap; 90 struct drm_bridge bridge; 91 struct drm_connector connector; 92 struct gpio_desc *reset_gpio; 93 struct i2c_mux_core *i2cmux; 94 }; 95 96 static int sii902x_read_unlocked(struct i2c_client *i2c, u8 reg, u8 *val) 97 { 98 union i2c_smbus_data data; 99 int ret; 100 101 ret = __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags, 102 I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data); 103 104 if (ret < 0) 105 return ret; 106 107 *val = data.byte; 108 return 0; 109 } 110 111 static int sii902x_write_unlocked(struct i2c_client *i2c, u8 reg, u8 val) 112 { 113 union i2c_smbus_data data; 114 115 data.byte = val; 116 117 return __i2c_smbus_xfer(i2c->adapter, i2c->addr, i2c->flags, 118 I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, 119 &data); 120 } 121 122 static int sii902x_update_bits_unlocked(struct i2c_client *i2c, u8 reg, u8 mask, 123 u8 val) 124 { 125 int ret; 126 u8 status; 127 128 ret = sii902x_read_unlocked(i2c, reg, &status); 129 if (ret) 130 return ret; 131 status &= ~mask; 132 status |= val & mask; 133 return sii902x_write_unlocked(i2c, reg, status); 134 } 135 136 static inline struct sii902x *bridge_to_sii902x(struct drm_bridge *bridge) 137 { 138 return container_of(bridge, struct sii902x, bridge); 139 } 140 141 static inline struct sii902x *connector_to_sii902x(struct drm_connector *con) 142 { 143 return container_of(con, struct sii902x, connector); 144 } 145 146 static void sii902x_reset(struct sii902x *sii902x) 147 { 148 if (!sii902x->reset_gpio) 149 return; 150 151 gpiod_set_value(sii902x->reset_gpio, 1); 152 153 /* The datasheet says treset-min = 100us. Make it 150us to be sure. */ 154 usleep_range(150, 200); 155 156 gpiod_set_value(sii902x->reset_gpio, 0); 157 } 158 159 static enum drm_connector_status 160 sii902x_connector_detect(struct drm_connector *connector, bool force) 161 { 162 struct sii902x *sii902x = connector_to_sii902x(connector); 163 unsigned int status; 164 165 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status); 166 167 return (status & SII902X_PLUGGED_STATUS) ? 168 connector_status_connected : connector_status_disconnected; 169 } 170 171 static const struct drm_connector_funcs sii902x_connector_funcs = { 172 .detect = sii902x_connector_detect, 173 .fill_modes = drm_helper_probe_single_connector_modes, 174 .destroy = drm_connector_cleanup, 175 .reset = drm_atomic_helper_connector_reset, 176 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 177 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 178 }; 179 180 static int sii902x_get_modes(struct drm_connector *connector) 181 { 182 struct sii902x *sii902x = connector_to_sii902x(connector); 183 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24; 184 struct edid *edid; 185 int num = 0, ret; 186 187 edid = drm_get_edid(connector, sii902x->i2cmux->adapter[0]); 188 drm_connector_update_edid_property(connector, edid); 189 if (edid) { 190 num = drm_add_edid_modes(connector, edid); 191 kfree(edid); 192 } 193 194 ret = drm_display_info_set_bus_formats(&connector->display_info, 195 &bus_format, 1); 196 if (ret) 197 return ret; 198 199 return num; 200 } 201 202 static enum drm_mode_status sii902x_mode_valid(struct drm_connector *connector, 203 struct drm_display_mode *mode) 204 { 205 /* TODO: check mode */ 206 207 return MODE_OK; 208 } 209 210 static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs = { 211 .get_modes = sii902x_get_modes, 212 .mode_valid = sii902x_mode_valid, 213 }; 214 215 static void sii902x_bridge_disable(struct drm_bridge *bridge) 216 { 217 struct sii902x *sii902x = bridge_to_sii902x(bridge); 218 219 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, 220 SII902X_SYS_CTRL_PWR_DWN, 221 SII902X_SYS_CTRL_PWR_DWN); 222 } 223 224 static void sii902x_bridge_enable(struct drm_bridge *bridge) 225 { 226 struct sii902x *sii902x = bridge_to_sii902x(bridge); 227 228 regmap_update_bits(sii902x->regmap, SII902X_PWR_STATE_CTRL, 229 SII902X_AVI_POWER_STATE_MSK, 230 SII902X_AVI_POWER_STATE_D(0)); 231 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, 232 SII902X_SYS_CTRL_PWR_DWN, 0); 233 } 234 235 static void sii902x_bridge_mode_set(struct drm_bridge *bridge, 236 const struct drm_display_mode *mode, 237 const struct drm_display_mode *adj) 238 { 239 struct sii902x *sii902x = bridge_to_sii902x(bridge); 240 struct regmap *regmap = sii902x->regmap; 241 u8 buf[HDMI_INFOFRAME_SIZE(AVI)]; 242 struct hdmi_avi_infoframe frame; 243 int ret; 244 245 buf[0] = adj->clock; 246 buf[1] = adj->clock >> 8; 247 buf[2] = adj->vrefresh; 248 buf[3] = 0x00; 249 buf[4] = adj->hdisplay; 250 buf[5] = adj->hdisplay >> 8; 251 buf[6] = adj->vdisplay; 252 buf[7] = adj->vdisplay >> 8; 253 buf[8] = SII902X_TPI_CLK_RATIO_1X | SII902X_TPI_AVI_PIXEL_REP_NONE | 254 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT; 255 buf[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO | 256 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB; 257 258 ret = regmap_bulk_write(regmap, SII902X_TPI_VIDEO_DATA, buf, 10); 259 if (ret) 260 return; 261 262 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, 263 &sii902x->connector, adj); 264 if (ret < 0) { 265 DRM_ERROR("couldn't fill AVI infoframe\n"); 266 return; 267 } 268 269 ret = hdmi_avi_infoframe_pack(&frame, buf, sizeof(buf)); 270 if (ret < 0) { 271 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret); 272 return; 273 } 274 275 /* Do not send the infoframe header, but keep the CRC field. */ 276 regmap_bulk_write(regmap, SII902X_TPI_AVI_INFOFRAME, 277 buf + HDMI_INFOFRAME_HEADER_SIZE - 1, 278 HDMI_AVI_INFOFRAME_SIZE + 1); 279 } 280 281 static int sii902x_bridge_attach(struct drm_bridge *bridge) 282 { 283 struct sii902x *sii902x = bridge_to_sii902x(bridge); 284 struct drm_device *drm = bridge->dev; 285 int ret; 286 287 drm_connector_helper_add(&sii902x->connector, 288 &sii902x_connector_helper_funcs); 289 290 if (!drm_core_check_feature(drm, DRIVER_ATOMIC)) { 291 dev_err(&sii902x->i2c->dev, 292 "sii902x driver is only compatible with DRM devices supporting atomic updates\n"); 293 return -ENOTSUPP; 294 } 295 296 ret = drm_connector_init(drm, &sii902x->connector, 297 &sii902x_connector_funcs, 298 DRM_MODE_CONNECTOR_HDMIA); 299 if (ret) 300 return ret; 301 302 if (sii902x->i2c->irq > 0) 303 sii902x->connector.polled = DRM_CONNECTOR_POLL_HPD; 304 else 305 sii902x->connector.polled = DRM_CONNECTOR_POLL_CONNECT; 306 307 drm_connector_attach_encoder(&sii902x->connector, bridge->encoder); 308 309 return 0; 310 } 311 312 static const struct drm_bridge_funcs sii902x_bridge_funcs = { 313 .attach = sii902x_bridge_attach, 314 .mode_set = sii902x_bridge_mode_set, 315 .disable = sii902x_bridge_disable, 316 .enable = sii902x_bridge_enable, 317 }; 318 319 static const struct regmap_range sii902x_volatile_ranges[] = { 320 { .range_min = 0, .range_max = 0xff }, 321 }; 322 323 static const struct regmap_access_table sii902x_volatile_table = { 324 .yes_ranges = sii902x_volatile_ranges, 325 .n_yes_ranges = ARRAY_SIZE(sii902x_volatile_ranges), 326 }; 327 328 static const struct regmap_config sii902x_regmap_config = { 329 .reg_bits = 8, 330 .val_bits = 8, 331 .volatile_table = &sii902x_volatile_table, 332 .cache_type = REGCACHE_NONE, 333 }; 334 335 static irqreturn_t sii902x_interrupt(int irq, void *data) 336 { 337 struct sii902x *sii902x = data; 338 unsigned int status = 0; 339 340 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status); 341 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status); 342 343 if ((status & SII902X_HOTPLUG_EVENT) && sii902x->bridge.dev) 344 drm_helper_hpd_irq_event(sii902x->bridge.dev); 345 346 return IRQ_HANDLED; 347 } 348 349 /* 350 * The purpose of sii902x_i2c_bypass_select is to enable the pass through 351 * mode of the HDMI transmitter. Do not use regmap from within this function, 352 * only use sii902x_*_unlocked functions to read/modify/write registers. 353 * We are holding the parent adapter lock here, keep this in mind before 354 * adding more i2c transactions. 355 * 356 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere 357 * in this driver, we need to make sure that we only touch 0x1A[2:1] from 358 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that 359 * we leave the remaining bits as we have found them. 360 */ 361 static int sii902x_i2c_bypass_select(struct i2c_mux_core *mux, u32 chan_id) 362 { 363 struct sii902x *sii902x = i2c_mux_priv(mux); 364 struct device *dev = &sii902x->i2c->dev; 365 unsigned long timeout; 366 u8 status; 367 int ret; 368 369 ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 370 SII902X_SYS_CTRL_DDC_BUS_REQ, 371 SII902X_SYS_CTRL_DDC_BUS_REQ); 372 if (ret) 373 return ret; 374 375 timeout = jiffies + 376 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS); 377 do { 378 ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 379 &status); 380 if (ret) 381 return ret; 382 } while (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD) && 383 time_before(jiffies, timeout)); 384 385 if (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD)) { 386 dev_err(dev, "Failed to acquire the i2c bus\n"); 387 return -ETIMEDOUT; 388 } 389 390 return sii902x_write_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 391 status); 392 } 393 394 /* 395 * The purpose of sii902x_i2c_bypass_deselect is to disable the pass through 396 * mode of the HDMI transmitter. Do not use regmap from within this function, 397 * only use sii902x_*_unlocked functions to read/modify/write registers. 398 * We are holding the parent adapter lock here, keep this in mind before 399 * adding more i2c transactions. 400 * 401 * Also, since SII902X_SYS_CTRL_DATA is used with regmap_update_bits elsewhere 402 * in this driver, we need to make sure that we only touch 0x1A[2:1] from 403 * within sii902x_i2c_bypass_select and sii902x_i2c_bypass_deselect, and that 404 * we leave the remaining bits as we have found them. 405 */ 406 static int sii902x_i2c_bypass_deselect(struct i2c_mux_core *mux, u32 chan_id) 407 { 408 struct sii902x *sii902x = i2c_mux_priv(mux); 409 struct device *dev = &sii902x->i2c->dev; 410 unsigned long timeout; 411 unsigned int retries; 412 u8 status; 413 int ret; 414 415 /* 416 * When the HDMI transmitter is in pass through mode, we need an 417 * (undocumented) additional delay between STOP and START conditions 418 * to guarantee the bus won't get stuck. 419 */ 420 udelay(30); 421 422 /* 423 * Sometimes the I2C bus can stall after failure to use the 424 * EDID channel. Retry a few times to see if things clear 425 * up, else continue anyway. 426 */ 427 retries = 5; 428 do { 429 ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 430 &status); 431 retries--; 432 } while (ret && retries); 433 if (ret) { 434 dev_err(dev, "failed to read status (%d)\n", ret); 435 return ret; 436 } 437 438 ret = sii902x_update_bits_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 439 SII902X_SYS_CTRL_DDC_BUS_REQ | 440 SII902X_SYS_CTRL_DDC_BUS_GRTD, 0); 441 if (ret) 442 return ret; 443 444 timeout = jiffies + 445 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS); 446 do { 447 ret = sii902x_read_unlocked(sii902x->i2c, SII902X_SYS_CTRL_DATA, 448 &status); 449 if (ret) 450 return ret; 451 } while (status & (SII902X_SYS_CTRL_DDC_BUS_REQ | 452 SII902X_SYS_CTRL_DDC_BUS_GRTD) && 453 time_before(jiffies, timeout)); 454 455 if (status & (SII902X_SYS_CTRL_DDC_BUS_REQ | 456 SII902X_SYS_CTRL_DDC_BUS_GRTD)) { 457 dev_err(dev, "failed to release the i2c bus\n"); 458 return -ETIMEDOUT; 459 } 460 461 return 0; 462 } 463 464 static int sii902x_probe(struct i2c_client *client, 465 const struct i2c_device_id *id) 466 { 467 struct device *dev = &client->dev; 468 unsigned int status = 0; 469 struct sii902x *sii902x; 470 u8 chipid[4]; 471 int ret; 472 473 ret = i2c_check_functionality(client->adapter, 474 I2C_FUNC_SMBUS_BYTE_DATA); 475 if (!ret) { 476 dev_err(dev, "I2C adapter not suitable\n"); 477 return -EIO; 478 } 479 480 sii902x = devm_kzalloc(dev, sizeof(*sii902x), GFP_KERNEL); 481 if (!sii902x) 482 return -ENOMEM; 483 484 sii902x->i2c = client; 485 sii902x->regmap = devm_regmap_init_i2c(client, &sii902x_regmap_config); 486 if (IS_ERR(sii902x->regmap)) 487 return PTR_ERR(sii902x->regmap); 488 489 sii902x->reset_gpio = devm_gpiod_get_optional(dev, "reset", 490 GPIOD_OUT_LOW); 491 if (IS_ERR(sii902x->reset_gpio)) { 492 dev_err(dev, "Failed to retrieve/request reset gpio: %ld\n", 493 PTR_ERR(sii902x->reset_gpio)); 494 return PTR_ERR(sii902x->reset_gpio); 495 } 496 497 sii902x_reset(sii902x); 498 499 ret = regmap_write(sii902x->regmap, SII902X_REG_TPI_RQB, 0x0); 500 if (ret) 501 return ret; 502 503 ret = regmap_bulk_read(sii902x->regmap, SII902X_REG_CHIPID(0), 504 &chipid, 4); 505 if (ret) { 506 dev_err(dev, "regmap_read failed %d\n", ret); 507 return ret; 508 } 509 510 if (chipid[0] != 0xb0) { 511 dev_err(dev, "Invalid chipid: %02x (expecting 0xb0)\n", 512 chipid[0]); 513 return -EINVAL; 514 } 515 516 /* Clear all pending interrupts */ 517 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status); 518 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status); 519 520 if (client->irq > 0) { 521 regmap_write(sii902x->regmap, SII902X_INT_ENABLE, 522 SII902X_HOTPLUG_EVENT); 523 524 ret = devm_request_threaded_irq(dev, client->irq, NULL, 525 sii902x_interrupt, 526 IRQF_ONESHOT, dev_name(dev), 527 sii902x); 528 if (ret) 529 return ret; 530 } 531 532 sii902x->bridge.funcs = &sii902x_bridge_funcs; 533 sii902x->bridge.of_node = dev->of_node; 534 drm_bridge_add(&sii902x->bridge); 535 536 i2c_set_clientdata(client, sii902x); 537 538 sii902x->i2cmux = i2c_mux_alloc(client->adapter, dev, 539 1, 0, I2C_MUX_GATE, 540 sii902x_i2c_bypass_select, 541 sii902x_i2c_bypass_deselect); 542 if (!sii902x->i2cmux) 543 return -ENOMEM; 544 545 sii902x->i2cmux->priv = sii902x; 546 return i2c_mux_add_adapter(sii902x->i2cmux, 0, 0, 0); 547 } 548 549 static int sii902x_remove(struct i2c_client *client) 550 551 { 552 struct sii902x *sii902x = i2c_get_clientdata(client); 553 554 i2c_mux_del_adapters(sii902x->i2cmux); 555 drm_bridge_remove(&sii902x->bridge); 556 557 return 0; 558 } 559 560 static const struct of_device_id sii902x_dt_ids[] = { 561 { .compatible = "sil,sii9022", }, 562 { } 563 }; 564 MODULE_DEVICE_TABLE(of, sii902x_dt_ids); 565 566 static const struct i2c_device_id sii902x_i2c_ids[] = { 567 { "sii9022", 0 }, 568 { }, 569 }; 570 MODULE_DEVICE_TABLE(i2c, sii902x_i2c_ids); 571 572 static struct i2c_driver sii902x_driver = { 573 .probe = sii902x_probe, 574 .remove = sii902x_remove, 575 .driver = { 576 .name = "sii902x", 577 .of_match_table = sii902x_dt_ids, 578 }, 579 .id_table = sii902x_i2c_ids, 580 }; 581 module_i2c_driver(sii902x_driver); 582 583 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>"); 584 MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges"); 585 MODULE_LICENSE("GPL"); 586