1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2018, The Linux Foundation. All rights reserved. 4 * Copyright (c) 2019-2020. Linaro Limited. 5 */ 6 7 #include <linux/firmware.h> 8 #include <linux/gpio/consumer.h> 9 #include <linux/i2c.h> 10 #include <linux/interrupt.h> 11 #include <linux/module.h> 12 #include <linux/mutex.h> 13 #include <linux/of_graph.h> 14 #include <linux/platform_device.h> 15 #include <linux/regmap.h> 16 #include <linux/regulator/consumer.h> 17 #include <linux/wait.h> 18 #include <linux/workqueue.h> 19 20 #include <sound/hdmi-codec.h> 21 22 #include <drm/drm_atomic_helper.h> 23 #include <drm/drm_bridge.h> 24 #include <drm/drm_edid.h> 25 #include <drm/drm_mipi_dsi.h> 26 #include <drm/drm_print.h> 27 #include <drm/drm_probe_helper.h> 28 29 #define EDID_BLOCK_SIZE 128 30 #define EDID_NUM_BLOCKS 2 31 32 #define FW_FILE "lt9611uxc_fw.bin" 33 34 struct lt9611uxc { 35 struct device *dev; 36 struct drm_bridge bridge; 37 struct drm_connector connector; 38 39 struct regmap *regmap; 40 /* Protects all accesses to registers by stopping the on-chip MCU */ 41 struct mutex ocm_lock; 42 43 struct wait_queue_head wq; 44 struct work_struct work; 45 46 struct device_node *dsi0_node; 47 struct device_node *dsi1_node; 48 struct mipi_dsi_device *dsi0; 49 struct mipi_dsi_device *dsi1; 50 struct platform_device *audio_pdev; 51 52 struct gpio_desc *reset_gpio; 53 struct gpio_desc *enable_gpio; 54 55 struct regulator_bulk_data supplies[2]; 56 57 struct i2c_client *client; 58 59 bool hpd_supported; 60 bool edid_read; 61 /* can be accessed from different threads, so protect this with ocm_lock */ 62 bool hdmi_connected; 63 uint8_t fw_version; 64 }; 65 66 #define LT9611_PAGE_CONTROL 0xff 67 68 static const struct regmap_range_cfg lt9611uxc_ranges[] = { 69 { 70 .name = "register_range", 71 .range_min = 0, 72 .range_max = 0xd0ff, 73 .selector_reg = LT9611_PAGE_CONTROL, 74 .selector_mask = 0xff, 75 .selector_shift = 0, 76 .window_start = 0, 77 .window_len = 0x100, 78 }, 79 }; 80 81 static const struct regmap_config lt9611uxc_regmap_config = { 82 .reg_bits = 8, 83 .val_bits = 8, 84 .max_register = 0xffff, 85 .ranges = lt9611uxc_ranges, 86 .num_ranges = ARRAY_SIZE(lt9611uxc_ranges), 87 }; 88 89 struct lt9611uxc_mode { 90 u16 hdisplay; 91 u16 vdisplay; 92 u8 vrefresh; 93 }; 94 95 /* 96 * This chip supports only a fixed set of modes. 97 * Enumerate them here to check whether the mode is supported. 98 */ 99 static struct lt9611uxc_mode lt9611uxc_modes[] = { 100 { 1920, 1080, 60 }, 101 { 1920, 1080, 30 }, 102 { 1920, 1080, 25 }, 103 { 1366, 768, 60 }, 104 { 1360, 768, 60 }, 105 { 1280, 1024, 60 }, 106 { 1280, 800, 60 }, 107 { 1280, 720, 60 }, 108 { 1280, 720, 50 }, 109 { 1280, 720, 30 }, 110 { 1152, 864, 60 }, 111 { 1024, 768, 60 }, 112 { 800, 600, 60 }, 113 { 720, 576, 50 }, 114 { 720, 480, 60 }, 115 { 640, 480, 60 }, 116 }; 117 118 static struct lt9611uxc *bridge_to_lt9611uxc(struct drm_bridge *bridge) 119 { 120 return container_of(bridge, struct lt9611uxc, bridge); 121 } 122 123 static struct lt9611uxc *connector_to_lt9611uxc(struct drm_connector *connector) 124 { 125 return container_of(connector, struct lt9611uxc, connector); 126 } 127 128 static void lt9611uxc_lock(struct lt9611uxc *lt9611uxc) 129 { 130 mutex_lock(<9611uxc->ocm_lock); 131 regmap_write(lt9611uxc->regmap, 0x80ee, 0x01); 132 } 133 134 static void lt9611uxc_unlock(struct lt9611uxc *lt9611uxc) 135 { 136 regmap_write(lt9611uxc->regmap, 0x80ee, 0x00); 137 msleep(50); 138 mutex_unlock(<9611uxc->ocm_lock); 139 } 140 141 static irqreturn_t lt9611uxc_irq_thread_handler(int irq, void *dev_id) 142 { 143 struct lt9611uxc *lt9611uxc = dev_id; 144 unsigned int irq_status = 0; 145 unsigned int hpd_status = 0; 146 147 lt9611uxc_lock(lt9611uxc); 148 149 regmap_read(lt9611uxc->regmap, 0xb022, &irq_status); 150 regmap_read(lt9611uxc->regmap, 0xb023, &hpd_status); 151 if (irq_status) 152 regmap_write(lt9611uxc->regmap, 0xb022, 0); 153 154 if (irq_status & BIT(0)) { 155 lt9611uxc->edid_read = !!(hpd_status & BIT(0)); 156 wake_up_all(<9611uxc->wq); 157 } 158 159 if (irq_status & BIT(1)) { 160 lt9611uxc->hdmi_connected = hpd_status & BIT(1); 161 schedule_work(<9611uxc->work); 162 } 163 164 lt9611uxc_unlock(lt9611uxc); 165 166 return IRQ_HANDLED; 167 } 168 169 static void lt9611uxc_hpd_work(struct work_struct *work) 170 { 171 struct lt9611uxc *lt9611uxc = container_of(work, struct lt9611uxc, work); 172 bool connected; 173 174 if (lt9611uxc->connector.dev) { 175 if (lt9611uxc->connector.dev->mode_config.funcs) 176 drm_kms_helper_hotplug_event(lt9611uxc->connector.dev); 177 } else { 178 179 mutex_lock(<9611uxc->ocm_lock); 180 connected = lt9611uxc->hdmi_connected; 181 mutex_unlock(<9611uxc->ocm_lock); 182 183 drm_bridge_hpd_notify(<9611uxc->bridge, 184 connected ? 185 connector_status_connected : 186 connector_status_disconnected); 187 } 188 } 189 190 static void lt9611uxc_reset(struct lt9611uxc *lt9611uxc) 191 { 192 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1); 193 msleep(20); 194 195 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 0); 196 msleep(20); 197 198 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1); 199 msleep(300); 200 } 201 202 static void lt9611uxc_assert_5v(struct lt9611uxc *lt9611uxc) 203 { 204 if (!lt9611uxc->enable_gpio) 205 return; 206 207 gpiod_set_value_cansleep(lt9611uxc->enable_gpio, 1); 208 msleep(20); 209 } 210 211 static int lt9611uxc_regulator_init(struct lt9611uxc *lt9611uxc) 212 { 213 int ret; 214 215 lt9611uxc->supplies[0].supply = "vdd"; 216 lt9611uxc->supplies[1].supply = "vcc"; 217 218 ret = devm_regulator_bulk_get(lt9611uxc->dev, 2, lt9611uxc->supplies); 219 if (ret < 0) 220 return ret; 221 222 return regulator_set_load(lt9611uxc->supplies[0].consumer, 200000); 223 } 224 225 static int lt9611uxc_regulator_enable(struct lt9611uxc *lt9611uxc) 226 { 227 int ret; 228 229 ret = regulator_enable(lt9611uxc->supplies[0].consumer); 230 if (ret < 0) 231 return ret; 232 233 usleep_range(1000, 10000); /* 50000 according to dtsi */ 234 235 ret = regulator_enable(lt9611uxc->supplies[1].consumer); 236 if (ret < 0) { 237 regulator_disable(lt9611uxc->supplies[0].consumer); 238 return ret; 239 } 240 241 return 0; 242 } 243 244 static struct lt9611uxc_mode *lt9611uxc_find_mode(const struct drm_display_mode *mode) 245 { 246 int i; 247 248 for (i = 0; i < ARRAY_SIZE(lt9611uxc_modes); i++) { 249 if (lt9611uxc_modes[i].hdisplay == mode->hdisplay && 250 lt9611uxc_modes[i].vdisplay == mode->vdisplay && 251 lt9611uxc_modes[i].vrefresh == drm_mode_vrefresh(mode)) { 252 return <9611uxc_modes[i]; 253 } 254 } 255 256 return NULL; 257 } 258 259 static struct mipi_dsi_device *lt9611uxc_attach_dsi(struct lt9611uxc *lt9611uxc, 260 struct device_node *dsi_node) 261 { 262 const struct mipi_dsi_device_info info = { "lt9611uxc", 0, NULL }; 263 struct mipi_dsi_device *dsi; 264 struct mipi_dsi_host *host; 265 struct device *dev = lt9611uxc->dev; 266 int ret; 267 268 host = of_find_mipi_dsi_host_by_node(dsi_node); 269 if (!host) 270 return ERR_PTR(dev_err_probe(dev, -EPROBE_DEFER, "failed to find dsi host\n")); 271 272 dsi = devm_mipi_dsi_device_register_full(dev, host, &info); 273 if (IS_ERR(dsi)) { 274 dev_err(dev, "failed to create dsi device\n"); 275 return dsi; 276 } 277 278 dsi->lanes = 4; 279 dsi->format = MIPI_DSI_FMT_RGB888; 280 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE | 281 MIPI_DSI_MODE_VIDEO_HSE; 282 283 ret = devm_mipi_dsi_attach(dev, dsi); 284 if (ret < 0) { 285 dev_err(dev, "failed to attach dsi to host\n"); 286 return ERR_PTR(ret); 287 } 288 289 return dsi; 290 } 291 292 static int lt9611uxc_connector_get_modes(struct drm_connector *connector) 293 { 294 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector); 295 const struct drm_edid *drm_edid; 296 int count; 297 298 drm_edid = drm_bridge_edid_read(<9611uxc->bridge, connector); 299 drm_edid_connector_update(connector, drm_edid); 300 count = drm_edid_connector_add_modes(connector); 301 drm_edid_free(drm_edid); 302 303 return count; 304 } 305 306 static enum drm_connector_status lt9611uxc_connector_detect(struct drm_connector *connector, 307 bool force) 308 { 309 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector); 310 311 return lt9611uxc->bridge.funcs->detect(<9611uxc->bridge); 312 } 313 314 static enum drm_mode_status lt9611uxc_connector_mode_valid(struct drm_connector *connector, 315 struct drm_display_mode *mode) 316 { 317 struct lt9611uxc_mode *lt9611uxc_mode = lt9611uxc_find_mode(mode); 318 319 return lt9611uxc_mode ? MODE_OK : MODE_BAD; 320 } 321 322 static const struct drm_connector_helper_funcs lt9611uxc_bridge_connector_helper_funcs = { 323 .get_modes = lt9611uxc_connector_get_modes, 324 .mode_valid = lt9611uxc_connector_mode_valid, 325 }; 326 327 static const struct drm_connector_funcs lt9611uxc_bridge_connector_funcs = { 328 .fill_modes = drm_helper_probe_single_connector_modes, 329 .detect = lt9611uxc_connector_detect, 330 .destroy = drm_connector_cleanup, 331 .reset = drm_atomic_helper_connector_reset, 332 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 333 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 334 }; 335 336 static int lt9611uxc_connector_init(struct drm_bridge *bridge, struct lt9611uxc *lt9611uxc) 337 { 338 int ret; 339 340 lt9611uxc->connector.polled = DRM_CONNECTOR_POLL_HPD; 341 342 drm_connector_helper_add(<9611uxc->connector, 343 <9611uxc_bridge_connector_helper_funcs); 344 ret = drm_connector_init(bridge->dev, <9611uxc->connector, 345 <9611uxc_bridge_connector_funcs, 346 DRM_MODE_CONNECTOR_HDMIA); 347 if (ret) { 348 DRM_ERROR("Failed to initialize connector with drm\n"); 349 return ret; 350 } 351 352 return drm_connector_attach_encoder(<9611uxc->connector, bridge->encoder); 353 } 354 355 static int lt9611uxc_bridge_attach(struct drm_bridge *bridge, 356 enum drm_bridge_attach_flags flags) 357 { 358 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge); 359 int ret; 360 361 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) { 362 ret = lt9611uxc_connector_init(bridge, lt9611uxc); 363 if (ret < 0) 364 return ret; 365 } 366 367 return 0; 368 } 369 370 static enum drm_mode_status 371 lt9611uxc_bridge_mode_valid(struct drm_bridge *bridge, 372 const struct drm_display_info *info, 373 const struct drm_display_mode *mode) 374 { 375 struct lt9611uxc_mode *lt9611uxc_mode; 376 377 lt9611uxc_mode = lt9611uxc_find_mode(mode); 378 379 return lt9611uxc_mode ? MODE_OK : MODE_BAD; 380 } 381 382 static void lt9611uxc_video_setup(struct lt9611uxc *lt9611uxc, 383 const struct drm_display_mode *mode) 384 { 385 u32 h_total, hactive, hsync_len, hfront_porch; 386 u32 v_total, vactive, vsync_len, vfront_porch; 387 388 h_total = mode->htotal; 389 v_total = mode->vtotal; 390 391 hactive = mode->hdisplay; 392 hsync_len = mode->hsync_end - mode->hsync_start; 393 hfront_porch = mode->hsync_start - mode->hdisplay; 394 395 vactive = mode->vdisplay; 396 vsync_len = mode->vsync_end - mode->vsync_start; 397 vfront_porch = mode->vsync_start - mode->vdisplay; 398 399 regmap_write(lt9611uxc->regmap, 0xd00d, (u8)(v_total / 256)); 400 regmap_write(lt9611uxc->regmap, 0xd00e, (u8)(v_total % 256)); 401 402 regmap_write(lt9611uxc->regmap, 0xd00f, (u8)(vactive / 256)); 403 regmap_write(lt9611uxc->regmap, 0xd010, (u8)(vactive % 256)); 404 405 regmap_write(lt9611uxc->regmap, 0xd011, (u8)(h_total / 256)); 406 regmap_write(lt9611uxc->regmap, 0xd012, (u8)(h_total % 256)); 407 408 regmap_write(lt9611uxc->regmap, 0xd013, (u8)(hactive / 256)); 409 regmap_write(lt9611uxc->regmap, 0xd014, (u8)(hactive % 256)); 410 411 regmap_write(lt9611uxc->regmap, 0xd015, (u8)(vsync_len % 256)); 412 413 regmap_update_bits(lt9611uxc->regmap, 0xd016, 0xf, (u8)(hsync_len / 256)); 414 regmap_write(lt9611uxc->regmap, 0xd017, (u8)(hsync_len % 256)); 415 416 regmap_update_bits(lt9611uxc->regmap, 0xd018, 0xf, (u8)(vfront_porch / 256)); 417 regmap_write(lt9611uxc->regmap, 0xd019, (u8)(vfront_porch % 256)); 418 419 regmap_update_bits(lt9611uxc->regmap, 0xd01a, 0xf, (u8)(hfront_porch / 256)); 420 regmap_write(lt9611uxc->regmap, 0xd01b, (u8)(hfront_porch % 256)); 421 } 422 423 static void lt9611uxc_bridge_mode_set(struct drm_bridge *bridge, 424 const struct drm_display_mode *mode, 425 const struct drm_display_mode *adj_mode) 426 { 427 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge); 428 429 lt9611uxc_lock(lt9611uxc); 430 lt9611uxc_video_setup(lt9611uxc, mode); 431 lt9611uxc_unlock(lt9611uxc); 432 } 433 434 static enum drm_connector_status lt9611uxc_bridge_detect(struct drm_bridge *bridge) 435 { 436 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge); 437 unsigned int reg_val = 0; 438 int ret; 439 bool connected = true; 440 441 lt9611uxc_lock(lt9611uxc); 442 443 if (lt9611uxc->hpd_supported) { 444 ret = regmap_read(lt9611uxc->regmap, 0xb023, ®_val); 445 446 if (ret) 447 dev_err(lt9611uxc->dev, "failed to read hpd status: %d\n", ret); 448 else 449 connected = reg_val & BIT(1); 450 } 451 lt9611uxc->hdmi_connected = connected; 452 453 lt9611uxc_unlock(lt9611uxc); 454 455 return connected ? connector_status_connected : 456 connector_status_disconnected; 457 } 458 459 static int lt9611uxc_wait_for_edid(struct lt9611uxc *lt9611uxc) 460 { 461 return wait_event_interruptible_timeout(lt9611uxc->wq, lt9611uxc->edid_read, 462 msecs_to_jiffies(500)); 463 } 464 465 static int lt9611uxc_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len) 466 { 467 struct lt9611uxc *lt9611uxc = data; 468 int ret; 469 470 if (len > EDID_BLOCK_SIZE) 471 return -EINVAL; 472 473 if (block >= EDID_NUM_BLOCKS) 474 return -EINVAL; 475 476 lt9611uxc_lock(lt9611uxc); 477 478 regmap_write(lt9611uxc->regmap, 0xb00b, 0x10); 479 480 regmap_write(lt9611uxc->regmap, 0xb00a, block * EDID_BLOCK_SIZE); 481 482 ret = regmap_noinc_read(lt9611uxc->regmap, 0xb0b0, buf, len); 483 if (ret) 484 dev_err(lt9611uxc->dev, "edid read failed: %d\n", ret); 485 486 lt9611uxc_unlock(lt9611uxc); 487 488 return 0; 489 }; 490 491 static const struct drm_edid *lt9611uxc_bridge_edid_read(struct drm_bridge *bridge, 492 struct drm_connector *connector) 493 { 494 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge); 495 int ret; 496 497 ret = lt9611uxc_wait_for_edid(lt9611uxc); 498 if (ret < 0) { 499 dev_err(lt9611uxc->dev, "wait for EDID failed: %d\n", ret); 500 return NULL; 501 } else if (ret == 0) { 502 dev_err(lt9611uxc->dev, "wait for EDID timeout\n"); 503 return NULL; 504 } 505 506 return drm_edid_read_custom(connector, lt9611uxc_get_edid_block, lt9611uxc); 507 } 508 509 static const struct drm_bridge_funcs lt9611uxc_bridge_funcs = { 510 .attach = lt9611uxc_bridge_attach, 511 .mode_valid = lt9611uxc_bridge_mode_valid, 512 .mode_set = lt9611uxc_bridge_mode_set, 513 .detect = lt9611uxc_bridge_detect, 514 .edid_read = lt9611uxc_bridge_edid_read, 515 }; 516 517 static int lt9611uxc_parse_dt(struct device *dev, 518 struct lt9611uxc *lt9611uxc) 519 { 520 lt9611uxc->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1); 521 if (!lt9611uxc->dsi0_node) { 522 dev_err(lt9611uxc->dev, "failed to get remote node for primary dsi\n"); 523 return -ENODEV; 524 } 525 526 lt9611uxc->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1); 527 528 return 0; 529 } 530 531 static int lt9611uxc_gpio_init(struct lt9611uxc *lt9611uxc) 532 { 533 struct device *dev = lt9611uxc->dev; 534 535 lt9611uxc->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 536 if (IS_ERR(lt9611uxc->reset_gpio)) { 537 dev_err(dev, "failed to acquire reset gpio\n"); 538 return PTR_ERR(lt9611uxc->reset_gpio); 539 } 540 541 lt9611uxc->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW); 542 if (IS_ERR(lt9611uxc->enable_gpio)) { 543 dev_err(dev, "failed to acquire enable gpio\n"); 544 return PTR_ERR(lt9611uxc->enable_gpio); 545 } 546 547 return 0; 548 } 549 550 static int lt9611uxc_read_device_rev(struct lt9611uxc *lt9611uxc) 551 { 552 unsigned int rev0, rev1, rev2; 553 int ret; 554 555 lt9611uxc_lock(lt9611uxc); 556 557 ret = regmap_read(lt9611uxc->regmap, 0x8100, &rev0); 558 ret |= regmap_read(lt9611uxc->regmap, 0x8101, &rev1); 559 ret |= regmap_read(lt9611uxc->regmap, 0x8102, &rev2); 560 if (ret) 561 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret); 562 else 563 dev_info(lt9611uxc->dev, "LT9611 revision: 0x%02x.%02x.%02x\n", rev0, rev1, rev2); 564 565 lt9611uxc_unlock(lt9611uxc); 566 567 return ret; 568 } 569 570 static int lt9611uxc_read_version(struct lt9611uxc *lt9611uxc) 571 { 572 unsigned int rev; 573 int ret; 574 575 lt9611uxc_lock(lt9611uxc); 576 577 ret = regmap_read(lt9611uxc->regmap, 0xb021, &rev); 578 if (ret) 579 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret); 580 else 581 dev_info(lt9611uxc->dev, "LT9611 version: 0x%02x\n", rev); 582 583 lt9611uxc_unlock(lt9611uxc); 584 585 return ret < 0 ? ret : rev; 586 } 587 588 static int lt9611uxc_hdmi_hw_params(struct device *dev, void *data, 589 struct hdmi_codec_daifmt *fmt, 590 struct hdmi_codec_params *hparms) 591 { 592 /* 593 * LT9611UXC will automatically detect rate and sample size, so no need 594 * to setup anything here. 595 */ 596 return 0; 597 } 598 599 static void lt9611uxc_audio_shutdown(struct device *dev, void *data) 600 { 601 } 602 603 static int lt9611uxc_hdmi_i2s_get_dai_id(struct snd_soc_component *component, 604 struct device_node *endpoint) 605 { 606 struct of_endpoint of_ep; 607 int ret; 608 609 ret = of_graph_parse_endpoint(endpoint, &of_ep); 610 if (ret < 0) 611 return ret; 612 613 /* 614 * HDMI sound should be located as reg = <2> 615 * Then, it is sound port 0 616 */ 617 if (of_ep.port == 2) 618 return 0; 619 620 return -EINVAL; 621 } 622 623 static const struct hdmi_codec_ops lt9611uxc_codec_ops = { 624 .hw_params = lt9611uxc_hdmi_hw_params, 625 .audio_shutdown = lt9611uxc_audio_shutdown, 626 .get_dai_id = lt9611uxc_hdmi_i2s_get_dai_id, 627 }; 628 629 static int lt9611uxc_audio_init(struct device *dev, struct lt9611uxc *lt9611uxc) 630 { 631 struct hdmi_codec_pdata codec_data = { 632 .ops = <9611uxc_codec_ops, 633 .max_i2s_channels = 2, 634 .i2s = 1, 635 .data = lt9611uxc, 636 }; 637 638 lt9611uxc->audio_pdev = 639 platform_device_register_data(dev, HDMI_CODEC_DRV_NAME, 640 PLATFORM_DEVID_AUTO, 641 &codec_data, sizeof(codec_data)); 642 643 return PTR_ERR_OR_ZERO(lt9611uxc->audio_pdev); 644 } 645 646 static void lt9611uxc_audio_exit(struct lt9611uxc *lt9611uxc) 647 { 648 if (lt9611uxc->audio_pdev) { 649 platform_device_unregister(lt9611uxc->audio_pdev); 650 lt9611uxc->audio_pdev = NULL; 651 } 652 } 653 654 #define LT9611UXC_FW_PAGE_SIZE 32 655 static void lt9611uxc_firmware_write_page(struct lt9611uxc *lt9611uxc, u16 addr, const u8 *buf) 656 { 657 struct reg_sequence seq_write_prepare[] = { 658 REG_SEQ0(0x805a, 0x04), 659 REG_SEQ0(0x805a, 0x00), 660 661 REG_SEQ0(0x805e, 0xdf), 662 REG_SEQ0(0x805a, 0x20), 663 REG_SEQ0(0x805a, 0x00), 664 REG_SEQ0(0x8058, 0x21), 665 }; 666 667 struct reg_sequence seq_write_addr[] = { 668 REG_SEQ0(0x805b, (addr >> 16) & 0xff), 669 REG_SEQ0(0x805c, (addr >> 8) & 0xff), 670 REG_SEQ0(0x805d, addr & 0xff), 671 REG_SEQ0(0x805a, 0x10), 672 REG_SEQ0(0x805a, 0x00), 673 }; 674 675 regmap_write(lt9611uxc->regmap, 0x8108, 0xbf); 676 msleep(20); 677 regmap_write(lt9611uxc->regmap, 0x8108, 0xff); 678 msleep(20); 679 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_prepare, ARRAY_SIZE(seq_write_prepare)); 680 regmap_noinc_write(lt9611uxc->regmap, 0x8059, buf, LT9611UXC_FW_PAGE_SIZE); 681 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_addr, ARRAY_SIZE(seq_write_addr)); 682 msleep(20); 683 } 684 685 static void lt9611uxc_firmware_read_page(struct lt9611uxc *lt9611uxc, u16 addr, char *buf) 686 { 687 struct reg_sequence seq_read_page[] = { 688 REG_SEQ0(0x805a, 0xa0), 689 REG_SEQ0(0x805a, 0x80), 690 REG_SEQ0(0x805b, (addr >> 16) & 0xff), 691 REG_SEQ0(0x805c, (addr >> 8) & 0xff), 692 REG_SEQ0(0x805d, addr & 0xff), 693 REG_SEQ0(0x805a, 0x90), 694 REG_SEQ0(0x805a, 0x80), 695 REG_SEQ0(0x8058, 0x21), 696 }; 697 698 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_page, ARRAY_SIZE(seq_read_page)); 699 regmap_noinc_read(lt9611uxc->regmap, 0x805f, buf, LT9611UXC_FW_PAGE_SIZE); 700 } 701 702 static char *lt9611uxc_firmware_read(struct lt9611uxc *lt9611uxc, size_t size) 703 { 704 struct reg_sequence seq_read_setup[] = { 705 REG_SEQ0(0x805a, 0x84), 706 REG_SEQ0(0x805a, 0x80), 707 }; 708 709 char *readbuf; 710 u16 offset; 711 712 readbuf = kzalloc(ALIGN(size, 32), GFP_KERNEL); 713 if (!readbuf) 714 return NULL; 715 716 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_setup, ARRAY_SIZE(seq_read_setup)); 717 718 for (offset = 0; 719 offset < size; 720 offset += LT9611UXC_FW_PAGE_SIZE) 721 lt9611uxc_firmware_read_page(lt9611uxc, offset, &readbuf[offset]); 722 723 return readbuf; 724 } 725 726 static int lt9611uxc_firmware_update(struct lt9611uxc *lt9611uxc) 727 { 728 int ret; 729 u16 offset; 730 size_t remain; 731 char *readbuf; 732 const struct firmware *fw; 733 734 struct reg_sequence seq_setup[] = { 735 REG_SEQ0(0x805e, 0xdf), 736 REG_SEQ0(0x8058, 0x00), 737 REG_SEQ0(0x8059, 0x50), 738 REG_SEQ0(0x805a, 0x10), 739 REG_SEQ0(0x805a, 0x00), 740 }; 741 742 743 struct reg_sequence seq_block_erase[] = { 744 REG_SEQ0(0x805a, 0x04), 745 REG_SEQ0(0x805a, 0x00), 746 REG_SEQ0(0x805b, 0x00), 747 REG_SEQ0(0x805c, 0x00), 748 REG_SEQ0(0x805d, 0x00), 749 REG_SEQ0(0x805a, 0x01), 750 REG_SEQ0(0x805a, 0x00), 751 }; 752 753 ret = request_firmware(&fw, FW_FILE, lt9611uxc->dev); 754 if (ret < 0) 755 return ret; 756 757 dev_info(lt9611uxc->dev, "Updating firmware\n"); 758 lt9611uxc_lock(lt9611uxc); 759 760 regmap_multi_reg_write(lt9611uxc->regmap, seq_setup, ARRAY_SIZE(seq_setup)); 761 762 /* 763 * Need erase block 2 timess here. Sometimes, block erase can fail. 764 * This is a workaroud. 765 */ 766 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase)); 767 msleep(3000); 768 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase)); 769 msleep(3000); 770 771 for (offset = 0, remain = fw->size; 772 remain >= LT9611UXC_FW_PAGE_SIZE; 773 offset += LT9611UXC_FW_PAGE_SIZE, remain -= LT9611UXC_FW_PAGE_SIZE) 774 lt9611uxc_firmware_write_page(lt9611uxc, offset, fw->data + offset); 775 776 if (remain > 0) { 777 char buf[LT9611UXC_FW_PAGE_SIZE]; 778 779 memset(buf, 0xff, LT9611UXC_FW_PAGE_SIZE); 780 memcpy(buf, fw->data + offset, remain); 781 lt9611uxc_firmware_write_page(lt9611uxc, offset, buf); 782 } 783 msleep(20); 784 785 readbuf = lt9611uxc_firmware_read(lt9611uxc, fw->size); 786 if (!readbuf) { 787 ret = -ENOMEM; 788 goto out; 789 } 790 791 if (!memcmp(readbuf, fw->data, fw->size)) { 792 dev_err(lt9611uxc->dev, "Firmware update failed\n"); 793 print_hex_dump(KERN_ERR, "fw: ", DUMP_PREFIX_OFFSET, 16, 1, readbuf, fw->size, false); 794 ret = -EINVAL; 795 } else { 796 dev_info(lt9611uxc->dev, "Firmware updates successfully\n"); 797 ret = 0; 798 } 799 kfree(readbuf); 800 801 out: 802 lt9611uxc_unlock(lt9611uxc); 803 lt9611uxc_reset(lt9611uxc); 804 release_firmware(fw); 805 806 return ret; 807 } 808 809 static ssize_t lt9611uxc_firmware_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) 810 { 811 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev); 812 int ret; 813 814 ret = lt9611uxc_firmware_update(lt9611uxc); 815 if (ret < 0) 816 return ret; 817 return len; 818 } 819 820 static ssize_t lt9611uxc_firmware_show(struct device *dev, struct device_attribute *attr, char *buf) 821 { 822 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev); 823 824 return sysfs_emit(buf, "%02x\n", lt9611uxc->fw_version); 825 } 826 827 static DEVICE_ATTR_RW(lt9611uxc_firmware); 828 829 static struct attribute *lt9611uxc_attrs[] = { 830 &dev_attr_lt9611uxc_firmware.attr, 831 NULL, 832 }; 833 834 static const struct attribute_group lt9611uxc_attr_group = { 835 .attrs = lt9611uxc_attrs, 836 }; 837 838 static const struct attribute_group *lt9611uxc_attr_groups[] = { 839 <9611uxc_attr_group, 840 NULL, 841 }; 842 843 static int lt9611uxc_probe(struct i2c_client *client) 844 { 845 struct lt9611uxc *lt9611uxc; 846 struct device *dev = &client->dev; 847 int ret; 848 bool fw_updated = false; 849 850 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 851 dev_err(dev, "device doesn't support I2C\n"); 852 return -ENODEV; 853 } 854 855 lt9611uxc = devm_kzalloc(dev, sizeof(*lt9611uxc), GFP_KERNEL); 856 if (!lt9611uxc) 857 return -ENOMEM; 858 859 lt9611uxc->dev = dev; 860 lt9611uxc->client = client; 861 mutex_init(<9611uxc->ocm_lock); 862 863 lt9611uxc->regmap = devm_regmap_init_i2c(client, <9611uxc_regmap_config); 864 if (IS_ERR(lt9611uxc->regmap)) { 865 dev_err(lt9611uxc->dev, "regmap i2c init failed\n"); 866 return PTR_ERR(lt9611uxc->regmap); 867 } 868 869 ret = lt9611uxc_parse_dt(dev, lt9611uxc); 870 if (ret) { 871 dev_err(dev, "failed to parse device tree\n"); 872 return ret; 873 } 874 875 ret = lt9611uxc_gpio_init(lt9611uxc); 876 if (ret < 0) 877 goto err_of_put; 878 879 ret = lt9611uxc_regulator_init(lt9611uxc); 880 if (ret < 0) 881 goto err_of_put; 882 883 lt9611uxc_assert_5v(lt9611uxc); 884 885 ret = lt9611uxc_regulator_enable(lt9611uxc); 886 if (ret) 887 goto err_of_put; 888 889 lt9611uxc_reset(lt9611uxc); 890 891 ret = lt9611uxc_read_device_rev(lt9611uxc); 892 if (ret) { 893 dev_err(dev, "failed to read chip rev\n"); 894 goto err_disable_regulators; 895 } 896 897 retry: 898 ret = lt9611uxc_read_version(lt9611uxc); 899 if (ret < 0) { 900 dev_err(dev, "failed to read FW version\n"); 901 goto err_disable_regulators; 902 } else if (ret == 0) { 903 if (!fw_updated) { 904 fw_updated = true; 905 dev_err(dev, "FW version 0, enforcing firmware update\n"); 906 ret = lt9611uxc_firmware_update(lt9611uxc); 907 if (ret < 0) 908 goto err_disable_regulators; 909 else 910 goto retry; 911 } else { 912 dev_err(dev, "FW version 0, update failed\n"); 913 ret = -EOPNOTSUPP; 914 goto err_disable_regulators; 915 } 916 } else if (ret < 0x40) { 917 dev_info(dev, "FW version 0x%x, HPD not supported\n", ret); 918 } else { 919 lt9611uxc->hpd_supported = true; 920 } 921 lt9611uxc->fw_version = ret; 922 923 init_waitqueue_head(<9611uxc->wq); 924 INIT_WORK(<9611uxc->work, lt9611uxc_hpd_work); 925 926 ret = request_threaded_irq(client->irq, NULL, 927 lt9611uxc_irq_thread_handler, 928 IRQF_ONESHOT, "lt9611uxc", lt9611uxc); 929 if (ret) { 930 dev_err(dev, "failed to request irq\n"); 931 goto err_disable_regulators; 932 } 933 934 i2c_set_clientdata(client, lt9611uxc); 935 936 lt9611uxc->bridge.funcs = <9611uxc_bridge_funcs; 937 lt9611uxc->bridge.of_node = client->dev.of_node; 938 lt9611uxc->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID; 939 if (lt9611uxc->hpd_supported) 940 lt9611uxc->bridge.ops |= DRM_BRIDGE_OP_HPD; 941 lt9611uxc->bridge.type = DRM_MODE_CONNECTOR_HDMIA; 942 943 drm_bridge_add(<9611uxc->bridge); 944 945 /* Attach primary DSI */ 946 lt9611uxc->dsi0 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi0_node); 947 if (IS_ERR(lt9611uxc->dsi0)) { 948 ret = PTR_ERR(lt9611uxc->dsi0); 949 goto err_remove_bridge; 950 } 951 952 /* Attach secondary DSI, if specified */ 953 if (lt9611uxc->dsi1_node) { 954 lt9611uxc->dsi1 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi1_node); 955 if (IS_ERR(lt9611uxc->dsi1)) { 956 ret = PTR_ERR(lt9611uxc->dsi1); 957 goto err_remove_bridge; 958 } 959 } 960 961 return lt9611uxc_audio_init(dev, lt9611uxc); 962 963 err_remove_bridge: 964 free_irq(client->irq, lt9611uxc); 965 cancel_work_sync(<9611uxc->work); 966 drm_bridge_remove(<9611uxc->bridge); 967 968 err_disable_regulators: 969 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies); 970 971 err_of_put: 972 of_node_put(lt9611uxc->dsi1_node); 973 of_node_put(lt9611uxc->dsi0_node); 974 975 return ret; 976 } 977 978 static void lt9611uxc_remove(struct i2c_client *client) 979 { 980 struct lt9611uxc *lt9611uxc = i2c_get_clientdata(client); 981 982 free_irq(client->irq, lt9611uxc); 983 cancel_work_sync(<9611uxc->work); 984 lt9611uxc_audio_exit(lt9611uxc); 985 drm_bridge_remove(<9611uxc->bridge); 986 987 mutex_destroy(<9611uxc->ocm_lock); 988 989 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies); 990 991 of_node_put(lt9611uxc->dsi1_node); 992 of_node_put(lt9611uxc->dsi0_node); 993 } 994 995 static struct i2c_device_id lt9611uxc_id[] = { 996 { "lontium,lt9611uxc", 0 }, 997 { /* sentinel */ } 998 }; 999 1000 static const struct of_device_id lt9611uxc_match_table[] = { 1001 { .compatible = "lontium,lt9611uxc" }, 1002 { /* sentinel */ } 1003 }; 1004 MODULE_DEVICE_TABLE(of, lt9611uxc_match_table); 1005 1006 static struct i2c_driver lt9611uxc_driver = { 1007 .driver = { 1008 .name = "lt9611uxc", 1009 .of_match_table = lt9611uxc_match_table, 1010 .dev_groups = lt9611uxc_attr_groups, 1011 }, 1012 .probe = lt9611uxc_probe, 1013 .remove = lt9611uxc_remove, 1014 .id_table = lt9611uxc_id, 1015 }; 1016 module_i2c_driver(lt9611uxc_driver); 1017 1018 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>"); 1019 MODULE_DESCRIPTION("Lontium LT9611UXC DSI/HDMI bridge driver"); 1020 MODULE_LICENSE("GPL v2"); 1021 1022 MODULE_FIRMWARE(FW_FILE); 1023