1 /* 2 * Copyright (C) 2016 Atmel 3 * Bo Shen <voice.shen@atmel.com> 4 * 5 * Authors: Bo Shen <voice.shen@atmel.com> 6 * Boris Brezillon <boris.brezillon@free-electrons.com> 7 * Wu, Songjun <Songjun.Wu@atmel.com> 8 * 9 * 10 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved. 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 */ 22 23 #include <linux/gpio/consumer.h> 24 #include <linux/i2c.h> 25 #include <linux/module.h> 26 #include <linux/regmap.h> 27 28 #include <drm/drmP.h> 29 #include <drm/drm_atomic_helper.h> 30 #include <drm/drm_crtc_helper.h> 31 #include <drm/drm_edid.h> 32 33 #define SII902X_TPI_VIDEO_DATA 0x0 34 35 #define SII902X_TPI_PIXEL_REPETITION 0x8 36 #define SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT BIT(5) 37 #define SII902X_TPI_AVI_PIXEL_REP_RISING_EDGE BIT(4) 38 #define SII902X_TPI_AVI_PIXEL_REP_4X 3 39 #define SII902X_TPI_AVI_PIXEL_REP_2X 1 40 #define SII902X_TPI_AVI_PIXEL_REP_NONE 0 41 #define SII902X_TPI_CLK_RATIO_HALF (0 << 6) 42 #define SII902X_TPI_CLK_RATIO_1X (1 << 6) 43 #define SII902X_TPI_CLK_RATIO_2X (2 << 6) 44 #define SII902X_TPI_CLK_RATIO_4X (3 << 6) 45 46 #define SII902X_TPI_AVI_IN_FORMAT 0x9 47 #define SII902X_TPI_AVI_INPUT_BITMODE_12BIT BIT(7) 48 #define SII902X_TPI_AVI_INPUT_DITHER BIT(6) 49 #define SII902X_TPI_AVI_INPUT_RANGE_LIMITED (2 << 2) 50 #define SII902X_TPI_AVI_INPUT_RANGE_FULL (1 << 2) 51 #define SII902X_TPI_AVI_INPUT_RANGE_AUTO (0 << 2) 52 #define SII902X_TPI_AVI_INPUT_COLORSPACE_BLACK (3 << 0) 53 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV422 (2 << 0) 54 #define SII902X_TPI_AVI_INPUT_COLORSPACE_YUV444 (1 << 0) 55 #define SII902X_TPI_AVI_INPUT_COLORSPACE_RGB (0 << 0) 56 57 #define SII902X_TPI_AVI_INFOFRAME 0x0c 58 59 #define SII902X_SYS_CTRL_DATA 0x1a 60 #define SII902X_SYS_CTRL_PWR_DWN BIT(4) 61 #define SII902X_SYS_CTRL_AV_MUTE BIT(3) 62 #define SII902X_SYS_CTRL_DDC_BUS_REQ BIT(2) 63 #define SII902X_SYS_CTRL_DDC_BUS_GRTD BIT(1) 64 #define SII902X_SYS_CTRL_OUTPUT_MODE BIT(0) 65 #define SII902X_SYS_CTRL_OUTPUT_HDMI 1 66 #define SII902X_SYS_CTRL_OUTPUT_DVI 0 67 68 #define SII902X_REG_CHIPID(n) (0x1b + (n)) 69 70 #define SII902X_PWR_STATE_CTRL 0x1e 71 #define SII902X_AVI_POWER_STATE_MSK GENMASK(1, 0) 72 #define SII902X_AVI_POWER_STATE_D(l) ((l) & SII902X_AVI_POWER_STATE_MSK) 73 74 #define SII902X_INT_ENABLE 0x3c 75 #define SII902X_INT_STATUS 0x3d 76 #define SII902X_HOTPLUG_EVENT BIT(0) 77 #define SII902X_PLUGGED_STATUS BIT(2) 78 79 #define SII902X_REG_TPI_RQB 0xc7 80 81 #define SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS 500 82 83 struct sii902x { 84 struct i2c_client *i2c; 85 struct regmap *regmap; 86 struct drm_bridge bridge; 87 struct drm_connector connector; 88 struct gpio_desc *reset_gpio; 89 }; 90 91 static inline struct sii902x *bridge_to_sii902x(struct drm_bridge *bridge) 92 { 93 return container_of(bridge, struct sii902x, bridge); 94 } 95 96 static inline struct sii902x *connector_to_sii902x(struct drm_connector *con) 97 { 98 return container_of(con, struct sii902x, connector); 99 } 100 101 static void sii902x_reset(struct sii902x *sii902x) 102 { 103 if (!sii902x->reset_gpio) 104 return; 105 106 gpiod_set_value(sii902x->reset_gpio, 1); 107 108 /* The datasheet says treset-min = 100us. Make it 150us to be sure. */ 109 usleep_range(150, 200); 110 111 gpiod_set_value(sii902x->reset_gpio, 0); 112 } 113 114 static enum drm_connector_status 115 sii902x_connector_detect(struct drm_connector *connector, bool force) 116 { 117 struct sii902x *sii902x = connector_to_sii902x(connector); 118 unsigned int status; 119 120 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status); 121 122 return (status & SII902X_PLUGGED_STATUS) ? 123 connector_status_connected : connector_status_disconnected; 124 } 125 126 static const struct drm_connector_funcs sii902x_connector_funcs = { 127 .detect = sii902x_connector_detect, 128 .fill_modes = drm_helper_probe_single_connector_modes, 129 .destroy = drm_connector_cleanup, 130 .reset = drm_atomic_helper_connector_reset, 131 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 132 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 133 }; 134 135 static int sii902x_get_modes(struct drm_connector *connector) 136 { 137 struct sii902x *sii902x = connector_to_sii902x(connector); 138 struct regmap *regmap = sii902x->regmap; 139 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24; 140 struct device *dev = &sii902x->i2c->dev; 141 unsigned long timeout; 142 unsigned int retries; 143 unsigned int status; 144 struct edid *edid; 145 int num = 0; 146 int ret; 147 148 ret = regmap_update_bits(regmap, SII902X_SYS_CTRL_DATA, 149 SII902X_SYS_CTRL_DDC_BUS_REQ, 150 SII902X_SYS_CTRL_DDC_BUS_REQ); 151 if (ret) 152 return ret; 153 154 timeout = jiffies + 155 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS); 156 do { 157 ret = regmap_read(regmap, SII902X_SYS_CTRL_DATA, &status); 158 if (ret) 159 return ret; 160 } while (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD) && 161 time_before(jiffies, timeout)); 162 163 if (!(status & SII902X_SYS_CTRL_DDC_BUS_GRTD)) { 164 dev_err(dev, "failed to acquire the i2c bus\n"); 165 return -ETIMEDOUT; 166 } 167 168 ret = regmap_write(regmap, SII902X_SYS_CTRL_DATA, status); 169 if (ret) 170 return ret; 171 172 edid = drm_get_edid(connector, sii902x->i2c->adapter); 173 drm_connector_update_edid_property(connector, edid); 174 if (edid) { 175 num = drm_add_edid_modes(connector, edid); 176 kfree(edid); 177 } 178 179 ret = drm_display_info_set_bus_formats(&connector->display_info, 180 &bus_format, 1); 181 if (ret) 182 return ret; 183 184 /* 185 * Sometimes the I2C bus can stall after failure to use the 186 * EDID channel. Retry a few times to see if things clear 187 * up, else continue anyway. 188 */ 189 retries = 5; 190 do { 191 ret = regmap_read(regmap, SII902X_SYS_CTRL_DATA, 192 &status); 193 retries--; 194 } while (ret && retries); 195 if (ret) 196 dev_err(dev, "failed to read status (%d)\n", ret); 197 198 ret = regmap_update_bits(regmap, SII902X_SYS_CTRL_DATA, 199 SII902X_SYS_CTRL_DDC_BUS_REQ | 200 SII902X_SYS_CTRL_DDC_BUS_GRTD, 0); 201 if (ret) 202 return ret; 203 204 timeout = jiffies + 205 msecs_to_jiffies(SII902X_I2C_BUS_ACQUISITION_TIMEOUT_MS); 206 do { 207 ret = regmap_read(regmap, SII902X_SYS_CTRL_DATA, &status); 208 if (ret) 209 return ret; 210 } while (status & (SII902X_SYS_CTRL_DDC_BUS_REQ | 211 SII902X_SYS_CTRL_DDC_BUS_GRTD) && 212 time_before(jiffies, timeout)); 213 214 if (status & (SII902X_SYS_CTRL_DDC_BUS_REQ | 215 SII902X_SYS_CTRL_DDC_BUS_GRTD)) { 216 dev_err(dev, "failed to release the i2c bus\n"); 217 return -ETIMEDOUT; 218 } 219 220 return num; 221 } 222 223 static enum drm_mode_status sii902x_mode_valid(struct drm_connector *connector, 224 struct drm_display_mode *mode) 225 { 226 /* TODO: check mode */ 227 228 return MODE_OK; 229 } 230 231 static const struct drm_connector_helper_funcs sii902x_connector_helper_funcs = { 232 .get_modes = sii902x_get_modes, 233 .mode_valid = sii902x_mode_valid, 234 }; 235 236 static void sii902x_bridge_disable(struct drm_bridge *bridge) 237 { 238 struct sii902x *sii902x = bridge_to_sii902x(bridge); 239 240 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, 241 SII902X_SYS_CTRL_PWR_DWN, 242 SII902X_SYS_CTRL_PWR_DWN); 243 } 244 245 static void sii902x_bridge_enable(struct drm_bridge *bridge) 246 { 247 struct sii902x *sii902x = bridge_to_sii902x(bridge); 248 249 regmap_update_bits(sii902x->regmap, SII902X_PWR_STATE_CTRL, 250 SII902X_AVI_POWER_STATE_MSK, 251 SII902X_AVI_POWER_STATE_D(0)); 252 regmap_update_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, 253 SII902X_SYS_CTRL_PWR_DWN, 0); 254 } 255 256 static void sii902x_bridge_mode_set(struct drm_bridge *bridge, 257 struct drm_display_mode *mode, 258 struct drm_display_mode *adj) 259 { 260 struct sii902x *sii902x = bridge_to_sii902x(bridge); 261 struct regmap *regmap = sii902x->regmap; 262 u8 buf[HDMI_INFOFRAME_SIZE(AVI)]; 263 struct hdmi_avi_infoframe frame; 264 int ret; 265 266 buf[0] = adj->clock; 267 buf[1] = adj->clock >> 8; 268 buf[2] = adj->vrefresh; 269 buf[3] = 0x00; 270 buf[4] = adj->hdisplay; 271 buf[5] = adj->hdisplay >> 8; 272 buf[6] = adj->vdisplay; 273 buf[7] = adj->vdisplay >> 8; 274 buf[8] = SII902X_TPI_CLK_RATIO_1X | SII902X_TPI_AVI_PIXEL_REP_NONE | 275 SII902X_TPI_AVI_PIXEL_REP_BUS_24BIT; 276 buf[9] = SII902X_TPI_AVI_INPUT_RANGE_AUTO | 277 SII902X_TPI_AVI_INPUT_COLORSPACE_RGB; 278 279 ret = regmap_bulk_write(regmap, SII902X_TPI_VIDEO_DATA, buf, 10); 280 if (ret) 281 return; 282 283 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, adj, false); 284 if (ret < 0) { 285 DRM_ERROR("couldn't fill AVI infoframe\n"); 286 return; 287 } 288 289 ret = hdmi_avi_infoframe_pack(&frame, buf, sizeof(buf)); 290 if (ret < 0) { 291 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret); 292 return; 293 } 294 295 /* Do not send the infoframe header, but keep the CRC field. */ 296 regmap_bulk_write(regmap, SII902X_TPI_AVI_INFOFRAME, 297 buf + HDMI_INFOFRAME_HEADER_SIZE - 1, 298 HDMI_AVI_INFOFRAME_SIZE + 1); 299 } 300 301 static int sii902x_bridge_attach(struct drm_bridge *bridge) 302 { 303 struct sii902x *sii902x = bridge_to_sii902x(bridge); 304 struct drm_device *drm = bridge->dev; 305 int ret; 306 307 drm_connector_helper_add(&sii902x->connector, 308 &sii902x_connector_helper_funcs); 309 310 if (!drm_core_check_feature(drm, DRIVER_ATOMIC)) { 311 dev_err(&sii902x->i2c->dev, 312 "sii902x driver is only compatible with DRM devices supporting atomic updates\n"); 313 return -ENOTSUPP; 314 } 315 316 ret = drm_connector_init(drm, &sii902x->connector, 317 &sii902x_connector_funcs, 318 DRM_MODE_CONNECTOR_HDMIA); 319 if (ret) 320 return ret; 321 322 if (sii902x->i2c->irq > 0) 323 sii902x->connector.polled = DRM_CONNECTOR_POLL_HPD; 324 else 325 sii902x->connector.polled = DRM_CONNECTOR_POLL_CONNECT; 326 327 drm_connector_attach_encoder(&sii902x->connector, bridge->encoder); 328 329 return 0; 330 } 331 332 static const struct drm_bridge_funcs sii902x_bridge_funcs = { 333 .attach = sii902x_bridge_attach, 334 .mode_set = sii902x_bridge_mode_set, 335 .disable = sii902x_bridge_disable, 336 .enable = sii902x_bridge_enable, 337 }; 338 339 static const struct regmap_range sii902x_volatile_ranges[] = { 340 { .range_min = 0, .range_max = 0xff }, 341 }; 342 343 static const struct regmap_access_table sii902x_volatile_table = { 344 .yes_ranges = sii902x_volatile_ranges, 345 .n_yes_ranges = ARRAY_SIZE(sii902x_volatile_ranges), 346 }; 347 348 static const struct regmap_config sii902x_regmap_config = { 349 .reg_bits = 8, 350 .val_bits = 8, 351 .volatile_table = &sii902x_volatile_table, 352 .cache_type = REGCACHE_NONE, 353 }; 354 355 static irqreturn_t sii902x_interrupt(int irq, void *data) 356 { 357 struct sii902x *sii902x = data; 358 unsigned int status = 0; 359 360 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status); 361 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status); 362 363 if ((status & SII902X_HOTPLUG_EVENT) && sii902x->bridge.dev) 364 drm_helper_hpd_irq_event(sii902x->bridge.dev); 365 366 return IRQ_HANDLED; 367 } 368 369 static int sii902x_probe(struct i2c_client *client, 370 const struct i2c_device_id *id) 371 { 372 struct device *dev = &client->dev; 373 unsigned int status = 0; 374 struct sii902x *sii902x; 375 u8 chipid[4]; 376 int ret; 377 378 sii902x = devm_kzalloc(dev, sizeof(*sii902x), GFP_KERNEL); 379 if (!sii902x) 380 return -ENOMEM; 381 382 sii902x->i2c = client; 383 sii902x->regmap = devm_regmap_init_i2c(client, &sii902x_regmap_config); 384 if (IS_ERR(sii902x->regmap)) 385 return PTR_ERR(sii902x->regmap); 386 387 sii902x->reset_gpio = devm_gpiod_get_optional(dev, "reset", 388 GPIOD_OUT_LOW); 389 if (IS_ERR(sii902x->reset_gpio)) { 390 dev_err(dev, "Failed to retrieve/request reset gpio: %ld\n", 391 PTR_ERR(sii902x->reset_gpio)); 392 return PTR_ERR(sii902x->reset_gpio); 393 } 394 395 sii902x_reset(sii902x); 396 397 ret = regmap_write(sii902x->regmap, SII902X_REG_TPI_RQB, 0x0); 398 if (ret) 399 return ret; 400 401 ret = regmap_bulk_read(sii902x->regmap, SII902X_REG_CHIPID(0), 402 &chipid, 4); 403 if (ret) { 404 dev_err(dev, "regmap_read failed %d\n", ret); 405 return ret; 406 } 407 408 if (chipid[0] != 0xb0) { 409 dev_err(dev, "Invalid chipid: %02x (expecting 0xb0)\n", 410 chipid[0]); 411 return -EINVAL; 412 } 413 414 /* Clear all pending interrupts */ 415 regmap_read(sii902x->regmap, SII902X_INT_STATUS, &status); 416 regmap_write(sii902x->regmap, SII902X_INT_STATUS, status); 417 418 if (client->irq > 0) { 419 regmap_write(sii902x->regmap, SII902X_INT_ENABLE, 420 SII902X_HOTPLUG_EVENT); 421 422 ret = devm_request_threaded_irq(dev, client->irq, NULL, 423 sii902x_interrupt, 424 IRQF_ONESHOT, dev_name(dev), 425 sii902x); 426 if (ret) 427 return ret; 428 } 429 430 sii902x->bridge.funcs = &sii902x_bridge_funcs; 431 sii902x->bridge.of_node = dev->of_node; 432 drm_bridge_add(&sii902x->bridge); 433 434 i2c_set_clientdata(client, sii902x); 435 436 return 0; 437 } 438 439 static int sii902x_remove(struct i2c_client *client) 440 441 { 442 struct sii902x *sii902x = i2c_get_clientdata(client); 443 444 drm_bridge_remove(&sii902x->bridge); 445 446 return 0; 447 } 448 449 static const struct of_device_id sii902x_dt_ids[] = { 450 { .compatible = "sil,sii9022", }, 451 { } 452 }; 453 MODULE_DEVICE_TABLE(of, sii902x_dt_ids); 454 455 static const struct i2c_device_id sii902x_i2c_ids[] = { 456 { "sii9022", 0 }, 457 { }, 458 }; 459 MODULE_DEVICE_TABLE(i2c, sii902x_i2c_ids); 460 461 static struct i2c_driver sii902x_driver = { 462 .probe = sii902x_probe, 463 .remove = sii902x_remove, 464 .driver = { 465 .name = "sii902x", 466 .of_match_table = sii902x_dt_ids, 467 }, 468 .id_table = sii902x_i2c_ids, 469 }; 470 module_i2c_driver(sii902x_driver); 471 472 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>"); 473 MODULE_DESCRIPTION("SII902x RGB -> HDMI bridges"); 474 MODULE_LICENSE("GPL"); 475