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