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