1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2019-2020. Linaro Limited.
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>
19 #include <sound/hdmi-codec.h>
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>
27 #define EDID_BLOCK_SIZE 128
28 #define EDID_NUM_BLOCKS 2
32 struct drm_bridge bridge;
33 struct drm_connector connector;
35 struct regmap *regmap;
36 /* Protects all accesses to registers by stopping the on-chip MCU */
37 struct mutex ocm_lock;
39 struct wait_queue_head wq;
40 struct work_struct work;
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;
48 struct gpio_desc *reset_gpio;
49 struct gpio_desc *enable_gpio;
51 struct regulator_bulk_data supplies[2];
53 struct i2c_client *client;
57 /* can be accessed from different threads, so protect this with ocm_lock */
62 #define LT9611_PAGE_CONTROL 0xff
64 static const struct regmap_range_cfg lt9611uxc_ranges[] = {
66 .name = "register_range",
69 .selector_reg = LT9611_PAGE_CONTROL,
70 .selector_mask = 0xff,
77 static const struct regmap_config lt9611uxc_regmap_config = {
80 .max_register = 0xffff,
81 .ranges = lt9611uxc_ranges,
82 .num_ranges = ARRAY_SIZE(lt9611uxc_ranges),
85 struct lt9611uxc_mode {
92 * This chip supports only a fixed set of modes.
93 * Enumerate them here to check whether the mode is supported.
95 static struct lt9611uxc_mode lt9611uxc_modes[] = {
114 static struct lt9611uxc *bridge_to_lt9611uxc(struct drm_bridge *bridge)
116 return container_of(bridge, struct lt9611uxc, bridge);
119 static struct lt9611uxc *connector_to_lt9611uxc(struct drm_connector *connector)
121 return container_of(connector, struct lt9611uxc, connector);
124 static void lt9611uxc_lock(struct lt9611uxc *lt9611uxc)
126 mutex_lock(<9611uxc->ocm_lock);
127 regmap_write(lt9611uxc->regmap, 0x80ee, 0x01);
130 static void lt9611uxc_unlock(struct lt9611uxc *lt9611uxc)
132 regmap_write(lt9611uxc->regmap, 0x80ee, 0x00);
134 mutex_unlock(<9611uxc->ocm_lock);
137 static irqreturn_t lt9611uxc_irq_thread_handler(int irq, void *dev_id)
139 struct lt9611uxc *lt9611uxc = dev_id;
140 unsigned int irq_status = 0;
141 unsigned int hpd_status = 0;
143 lt9611uxc_lock(lt9611uxc);
145 regmap_read(lt9611uxc->regmap, 0xb022, &irq_status);
146 regmap_read(lt9611uxc->regmap, 0xb023, &hpd_status);
148 regmap_write(lt9611uxc->regmap, 0xb022, 0);
150 if (irq_status & BIT(0)) {
151 lt9611uxc->edid_read = !!(hpd_status & BIT(0));
152 wake_up_all(<9611uxc->wq);
155 if (irq_status & BIT(1)) {
156 lt9611uxc->hdmi_connected = hpd_status & BIT(1);
157 schedule_work(<9611uxc->work);
160 lt9611uxc_unlock(lt9611uxc);
165 static void lt9611uxc_hpd_work(struct work_struct *work)
167 struct lt9611uxc *lt9611uxc = container_of(work, struct lt9611uxc, work);
170 if (lt9611uxc->connector.dev)
171 drm_kms_helper_hotplug_event(lt9611uxc->connector.dev);
174 mutex_lock(<9611uxc->ocm_lock);
175 connected = lt9611uxc->hdmi_connected;
176 mutex_unlock(<9611uxc->ocm_lock);
178 drm_bridge_hpd_notify(<9611uxc->bridge,
180 connector_status_connected :
181 connector_status_disconnected);
185 static void lt9611uxc_reset(struct lt9611uxc *lt9611uxc)
187 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
190 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 0);
193 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
197 static void lt9611uxc_assert_5v(struct lt9611uxc *lt9611uxc)
199 if (!lt9611uxc->enable_gpio)
202 gpiod_set_value_cansleep(lt9611uxc->enable_gpio, 1);
206 static int lt9611uxc_regulator_init(struct lt9611uxc *lt9611uxc)
210 lt9611uxc->supplies[0].supply = "vdd";
211 lt9611uxc->supplies[1].supply = "vcc";
213 ret = devm_regulator_bulk_get(lt9611uxc->dev, 2, lt9611uxc->supplies);
217 return regulator_set_load(lt9611uxc->supplies[0].consumer, 200000);
220 static int lt9611uxc_regulator_enable(struct lt9611uxc *lt9611uxc)
224 ret = regulator_enable(lt9611uxc->supplies[0].consumer);
228 usleep_range(1000, 10000); /* 50000 according to dtsi */
230 ret = regulator_enable(lt9611uxc->supplies[1].consumer);
232 regulator_disable(lt9611uxc->supplies[0].consumer);
239 static struct lt9611uxc_mode *lt9611uxc_find_mode(const struct drm_display_mode *mode)
243 for (i = 0; i < ARRAY_SIZE(lt9611uxc_modes); i++) {
244 if (lt9611uxc_modes[i].hdisplay == mode->hdisplay &&
245 lt9611uxc_modes[i].vdisplay == mode->vdisplay &&
246 lt9611uxc_modes[i].vrefresh == drm_mode_vrefresh(mode)) {
247 return <9611uxc_modes[i];
254 static struct mipi_dsi_device *lt9611uxc_attach_dsi(struct lt9611uxc *lt9611uxc,
255 struct device_node *dsi_node)
257 const struct mipi_dsi_device_info info = { "lt9611uxc", 0, NULL };
258 struct mipi_dsi_device *dsi;
259 struct mipi_dsi_host *host;
262 host = of_find_mipi_dsi_host_by_node(dsi_node);
264 dev_err(lt9611uxc->dev, "failed to find dsi host\n");
265 return ERR_PTR(-EPROBE_DEFER);
268 dsi = mipi_dsi_device_register_full(host, &info);
270 dev_err(lt9611uxc->dev, "failed to create dsi device\n");
275 dsi->format = MIPI_DSI_FMT_RGB888;
276 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
277 MIPI_DSI_MODE_VIDEO_HSE;
279 ret = mipi_dsi_attach(dsi);
281 dev_err(lt9611uxc->dev, "failed to attach dsi to host\n");
282 mipi_dsi_device_unregister(dsi);
289 static int lt9611uxc_connector_get_modes(struct drm_connector *connector)
291 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
295 edid = lt9611uxc->bridge.funcs->get_edid(<9611uxc->bridge, connector);
296 drm_connector_update_edid_property(connector, edid);
297 count = drm_add_edid_modes(connector, edid);
303 static enum drm_connector_status lt9611uxc_connector_detect(struct drm_connector *connector,
306 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
308 return lt9611uxc->bridge.funcs->detect(<9611uxc->bridge);
311 static enum drm_mode_status lt9611uxc_connector_mode_valid(struct drm_connector *connector,
312 struct drm_display_mode *mode)
314 struct lt9611uxc_mode *lt9611uxc_mode = lt9611uxc_find_mode(mode);
316 return lt9611uxc_mode ? MODE_OK : MODE_BAD;
319 static const struct drm_connector_helper_funcs lt9611uxc_bridge_connector_helper_funcs = {
320 .get_modes = lt9611uxc_connector_get_modes,
321 .mode_valid = lt9611uxc_connector_mode_valid,
324 static const struct drm_connector_funcs lt9611uxc_bridge_connector_funcs = {
325 .fill_modes = drm_helper_probe_single_connector_modes,
326 .detect = lt9611uxc_connector_detect,
327 .destroy = drm_connector_cleanup,
328 .reset = drm_atomic_helper_connector_reset,
329 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
330 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
333 static int lt9611uxc_connector_init(struct drm_bridge *bridge, struct lt9611uxc *lt9611uxc)
337 if (!bridge->encoder) {
338 DRM_ERROR("Parent encoder object not found");
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);
348 DRM_ERROR("Failed to initialize connector with drm\n");
352 return drm_connector_attach_encoder(<9611uxc->connector, bridge->encoder);
355 static void lt9611uxc_bridge_detach(struct drm_bridge *bridge)
357 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
359 if (lt9611uxc->dsi1) {
360 mipi_dsi_detach(lt9611uxc->dsi1);
361 mipi_dsi_device_unregister(lt9611uxc->dsi1);
364 mipi_dsi_detach(lt9611uxc->dsi0);
365 mipi_dsi_device_unregister(lt9611uxc->dsi0);
368 static int lt9611uxc_bridge_attach(struct drm_bridge *bridge,
369 enum drm_bridge_attach_flags flags)
371 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
374 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
375 ret = lt9611uxc_connector_init(bridge, lt9611uxc);
380 /* Attach primary DSI */
381 lt9611uxc->dsi0 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi0_node);
382 if (IS_ERR(lt9611uxc->dsi0))
383 return PTR_ERR(lt9611uxc->dsi0);
385 /* Attach secondary DSI, if specified */
386 if (lt9611uxc->dsi1_node) {
387 lt9611uxc->dsi1 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi1_node);
388 if (IS_ERR(lt9611uxc->dsi1)) {
389 ret = PTR_ERR(lt9611uxc->dsi1);
390 goto err_unregister_dsi0;
397 mipi_dsi_detach(lt9611uxc->dsi0);
398 mipi_dsi_device_unregister(lt9611uxc->dsi0);
403 static enum drm_mode_status
404 lt9611uxc_bridge_mode_valid(struct drm_bridge *bridge,
405 const struct drm_display_info *info,
406 const struct drm_display_mode *mode)
408 struct lt9611uxc_mode *lt9611uxc_mode;
410 lt9611uxc_mode = lt9611uxc_find_mode(mode);
412 return lt9611uxc_mode ? MODE_OK : MODE_BAD;
415 static void lt9611uxc_video_setup(struct lt9611uxc *lt9611uxc,
416 const struct drm_display_mode *mode)
418 u32 h_total, hactive, hsync_len, hfront_porch;
419 u32 v_total, vactive, vsync_len, vfront_porch;
421 h_total = mode->htotal;
422 v_total = mode->vtotal;
424 hactive = mode->hdisplay;
425 hsync_len = mode->hsync_end - mode->hsync_start;
426 hfront_porch = mode->hsync_start - mode->hdisplay;
428 vactive = mode->vdisplay;
429 vsync_len = mode->vsync_end - mode->vsync_start;
430 vfront_porch = mode->vsync_start - mode->vdisplay;
432 regmap_write(lt9611uxc->regmap, 0xd00d, (u8)(v_total / 256));
433 regmap_write(lt9611uxc->regmap, 0xd00e, (u8)(v_total % 256));
435 regmap_write(lt9611uxc->regmap, 0xd00f, (u8)(vactive / 256));
436 regmap_write(lt9611uxc->regmap, 0xd010, (u8)(vactive % 256));
438 regmap_write(lt9611uxc->regmap, 0xd011, (u8)(h_total / 256));
439 regmap_write(lt9611uxc->regmap, 0xd012, (u8)(h_total % 256));
441 regmap_write(lt9611uxc->regmap, 0xd013, (u8)(hactive / 256));
442 regmap_write(lt9611uxc->regmap, 0xd014, (u8)(hactive % 256));
444 regmap_write(lt9611uxc->regmap, 0xd015, (u8)(vsync_len % 256));
446 regmap_update_bits(lt9611uxc->regmap, 0xd016, 0xf, (u8)(hsync_len / 256));
447 regmap_write(lt9611uxc->regmap, 0xd017, (u8)(hsync_len % 256));
449 regmap_update_bits(lt9611uxc->regmap, 0xd018, 0xf, (u8)(vfront_porch / 256));
450 regmap_write(lt9611uxc->regmap, 0xd019, (u8)(vfront_porch % 256));
452 regmap_update_bits(lt9611uxc->regmap, 0xd01a, 0xf, (u8)(hfront_porch / 256));
453 regmap_write(lt9611uxc->regmap, 0xd01b, (u8)(hfront_porch % 256));
456 static void lt9611uxc_bridge_mode_set(struct drm_bridge *bridge,
457 const struct drm_display_mode *mode,
458 const struct drm_display_mode *adj_mode)
460 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
462 lt9611uxc_lock(lt9611uxc);
463 lt9611uxc_video_setup(lt9611uxc, mode);
464 lt9611uxc_unlock(lt9611uxc);
467 static enum drm_connector_status lt9611uxc_bridge_detect(struct drm_bridge *bridge)
469 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
470 unsigned int reg_val = 0;
472 bool connected = true;
474 lt9611uxc_lock(lt9611uxc);
476 if (lt9611uxc->hpd_supported) {
477 ret = regmap_read(lt9611uxc->regmap, 0xb023, ®_val);
480 dev_err(lt9611uxc->dev, "failed to read hpd status: %d\n", ret);
482 connected = reg_val & BIT(1);
484 lt9611uxc->hdmi_connected = connected;
486 lt9611uxc_unlock(lt9611uxc);
488 return connected ? connector_status_connected :
489 connector_status_disconnected;
492 static int lt9611uxc_wait_for_edid(struct lt9611uxc *lt9611uxc)
494 return wait_event_interruptible_timeout(lt9611uxc->wq, lt9611uxc->edid_read,
495 msecs_to_jiffies(500));
498 static int lt9611uxc_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
500 struct lt9611uxc *lt9611uxc = data;
503 if (len > EDID_BLOCK_SIZE)
506 if (block >= EDID_NUM_BLOCKS)
509 lt9611uxc_lock(lt9611uxc);
511 regmap_write(lt9611uxc->regmap, 0xb00b, 0x10);
513 regmap_write(lt9611uxc->regmap, 0xb00a, block * EDID_BLOCK_SIZE);
515 ret = regmap_noinc_read(lt9611uxc->regmap, 0xb0b0, buf, len);
517 dev_err(lt9611uxc->dev, "edid read failed: %d\n", ret);
519 lt9611uxc_unlock(lt9611uxc);
524 static struct edid *lt9611uxc_bridge_get_edid(struct drm_bridge *bridge,
525 struct drm_connector *connector)
527 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
530 ret = lt9611uxc_wait_for_edid(lt9611uxc);
532 dev_err(lt9611uxc->dev, "wait for EDID failed: %d\n", ret);
534 } else if (ret == 0) {
535 dev_err(lt9611uxc->dev, "wait for EDID timeout\n");
539 return drm_do_get_edid(connector, lt9611uxc_get_edid_block, lt9611uxc);
542 static const struct drm_bridge_funcs lt9611uxc_bridge_funcs = {
543 .attach = lt9611uxc_bridge_attach,
544 .detach = lt9611uxc_bridge_detach,
545 .mode_valid = lt9611uxc_bridge_mode_valid,
546 .mode_set = lt9611uxc_bridge_mode_set,
547 .detect = lt9611uxc_bridge_detect,
548 .get_edid = lt9611uxc_bridge_get_edid,
551 static int lt9611uxc_parse_dt(struct device *dev,
552 struct lt9611uxc *lt9611uxc)
554 lt9611uxc->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
555 if (!lt9611uxc->dsi0_node) {
556 dev_err(lt9611uxc->dev, "failed to get remote node for primary dsi\n");
560 lt9611uxc->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
565 static int lt9611uxc_gpio_init(struct lt9611uxc *lt9611uxc)
567 struct device *dev = lt9611uxc->dev;
569 lt9611uxc->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
570 if (IS_ERR(lt9611uxc->reset_gpio)) {
571 dev_err(dev, "failed to acquire reset gpio\n");
572 return PTR_ERR(lt9611uxc->reset_gpio);
575 lt9611uxc->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
576 if (IS_ERR(lt9611uxc->enable_gpio)) {
577 dev_err(dev, "failed to acquire enable gpio\n");
578 return PTR_ERR(lt9611uxc->enable_gpio);
584 static int lt9611uxc_read_device_rev(struct lt9611uxc *lt9611uxc)
586 unsigned int rev0, rev1, rev2;
589 lt9611uxc_lock(lt9611uxc);
591 ret = regmap_read(lt9611uxc->regmap, 0x8100, &rev0);
592 ret |= regmap_read(lt9611uxc->regmap, 0x8101, &rev1);
593 ret |= regmap_read(lt9611uxc->regmap, 0x8102, &rev2);
595 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
597 dev_info(lt9611uxc->dev, "LT9611 revision: 0x%02x.%02x.%02x\n", rev0, rev1, rev2);
599 lt9611uxc_unlock(lt9611uxc);
604 static int lt9611uxc_read_version(struct lt9611uxc *lt9611uxc)
609 lt9611uxc_lock(lt9611uxc);
611 ret = regmap_read(lt9611uxc->regmap, 0xb021, &rev);
613 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
615 dev_info(lt9611uxc->dev, "LT9611 version: 0x%02x\n", rev);
617 lt9611uxc_unlock(lt9611uxc);
619 return ret < 0 ? ret : rev;
622 static int lt9611uxc_hdmi_hw_params(struct device *dev, void *data,
623 struct hdmi_codec_daifmt *fmt,
624 struct hdmi_codec_params *hparms)
627 * LT9611UXC will automatically detect rate and sample size, so no need
628 * to setup anything here.
633 static void lt9611uxc_audio_shutdown(struct device *dev, void *data)
637 static int lt9611uxc_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
638 struct device_node *endpoint)
640 struct of_endpoint of_ep;
643 ret = of_graph_parse_endpoint(endpoint, &of_ep);
648 * HDMI sound should be located as reg = <2>
649 * Then, it is sound port 0
657 static const struct hdmi_codec_ops lt9611uxc_codec_ops = {
658 .hw_params = lt9611uxc_hdmi_hw_params,
659 .audio_shutdown = lt9611uxc_audio_shutdown,
660 .get_dai_id = lt9611uxc_hdmi_i2s_get_dai_id,
663 static int lt9611uxc_audio_init(struct device *dev, struct lt9611uxc *lt9611uxc)
665 struct hdmi_codec_pdata codec_data = {
666 .ops = <9611uxc_codec_ops,
667 .max_i2s_channels = 2,
672 lt9611uxc->audio_pdev =
673 platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
675 &codec_data, sizeof(codec_data));
677 return PTR_ERR_OR_ZERO(lt9611uxc->audio_pdev);
680 static void lt9611uxc_audio_exit(struct lt9611uxc *lt9611uxc)
682 if (lt9611uxc->audio_pdev) {
683 platform_device_unregister(lt9611uxc->audio_pdev);
684 lt9611uxc->audio_pdev = NULL;
688 #define LT9611UXC_FW_PAGE_SIZE 32
689 static void lt9611uxc_firmware_write_page(struct lt9611uxc *lt9611uxc, u16 addr, const u8 *buf)
691 struct reg_sequence seq_write_prepare[] = {
692 REG_SEQ0(0x805a, 0x04),
693 REG_SEQ0(0x805a, 0x00),
695 REG_SEQ0(0x805e, 0xdf),
696 REG_SEQ0(0x805a, 0x20),
697 REG_SEQ0(0x805a, 0x00),
698 REG_SEQ0(0x8058, 0x21),
701 struct reg_sequence seq_write_addr[] = {
702 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
703 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
704 REG_SEQ0(0x805d, addr & 0xff),
705 REG_SEQ0(0x805a, 0x10),
706 REG_SEQ0(0x805a, 0x00),
709 regmap_write(lt9611uxc->regmap, 0x8108, 0xbf);
711 regmap_write(lt9611uxc->regmap, 0x8108, 0xff);
713 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_prepare, ARRAY_SIZE(seq_write_prepare));
714 regmap_noinc_write(lt9611uxc->regmap, 0x8059, buf, LT9611UXC_FW_PAGE_SIZE);
715 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_addr, ARRAY_SIZE(seq_write_addr));
719 static void lt9611uxc_firmware_read_page(struct lt9611uxc *lt9611uxc, u16 addr, char *buf)
721 struct reg_sequence seq_read_page[] = {
722 REG_SEQ0(0x805a, 0xa0),
723 REG_SEQ0(0x805a, 0x80),
724 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
725 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
726 REG_SEQ0(0x805d, addr & 0xff),
727 REG_SEQ0(0x805a, 0x90),
728 REG_SEQ0(0x805a, 0x80),
729 REG_SEQ0(0x8058, 0x21),
732 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_page, ARRAY_SIZE(seq_read_page));
733 regmap_noinc_read(lt9611uxc->regmap, 0x805f, buf, LT9611UXC_FW_PAGE_SIZE);
736 static char *lt9611uxc_firmware_read(struct lt9611uxc *lt9611uxc, size_t size)
738 struct reg_sequence seq_read_setup[] = {
739 REG_SEQ0(0x805a, 0x84),
740 REG_SEQ0(0x805a, 0x80),
746 readbuf = kzalloc(ALIGN(size, 32), GFP_KERNEL);
750 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_setup, ARRAY_SIZE(seq_read_setup));
754 offset += LT9611UXC_FW_PAGE_SIZE)
755 lt9611uxc_firmware_read_page(lt9611uxc, offset, &readbuf[offset]);
760 static int lt9611uxc_firmware_update(struct lt9611uxc *lt9611uxc)
766 const struct firmware *fw;
768 struct reg_sequence seq_setup[] = {
769 REG_SEQ0(0x805e, 0xdf),
770 REG_SEQ0(0x8058, 0x00),
771 REG_SEQ0(0x8059, 0x50),
772 REG_SEQ0(0x805a, 0x10),
773 REG_SEQ0(0x805a, 0x00),
777 struct reg_sequence seq_block_erase[] = {
778 REG_SEQ0(0x805a, 0x04),
779 REG_SEQ0(0x805a, 0x00),
780 REG_SEQ0(0x805b, 0x00),
781 REG_SEQ0(0x805c, 0x00),
782 REG_SEQ0(0x805d, 0x00),
783 REG_SEQ0(0x805a, 0x01),
784 REG_SEQ0(0x805a, 0x00),
787 ret = request_firmware(&fw, "lt9611uxc_fw.bin", lt9611uxc->dev);
791 dev_info(lt9611uxc->dev, "Updating firmware\n");
792 lt9611uxc_lock(lt9611uxc);
794 regmap_multi_reg_write(lt9611uxc->regmap, seq_setup, ARRAY_SIZE(seq_setup));
797 * Need erase block 2 timess here. Sometimes, block erase can fail.
798 * This is a workaroud.
800 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
802 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
805 for (offset = 0, remain = fw->size;
806 remain >= LT9611UXC_FW_PAGE_SIZE;
807 offset += LT9611UXC_FW_PAGE_SIZE, remain -= LT9611UXC_FW_PAGE_SIZE)
808 lt9611uxc_firmware_write_page(lt9611uxc, offset, fw->data + offset);
811 char buf[LT9611UXC_FW_PAGE_SIZE];
813 memset(buf, 0xff, LT9611UXC_FW_PAGE_SIZE);
814 memcpy(buf, fw->data + offset, remain);
815 lt9611uxc_firmware_write_page(lt9611uxc, offset, buf);
819 readbuf = lt9611uxc_firmware_read(lt9611uxc, fw->size);
825 if (!memcmp(readbuf, fw->data, fw->size)) {
826 dev_err(lt9611uxc->dev, "Firmware update failed\n");
827 print_hex_dump(KERN_ERR, "fw: ", DUMP_PREFIX_OFFSET, 16, 1, readbuf, fw->size, false);
830 dev_info(lt9611uxc->dev, "Firmware updates successfully\n");
836 lt9611uxc_unlock(lt9611uxc);
837 lt9611uxc_reset(lt9611uxc);
838 release_firmware(fw);
843 static ssize_t lt9611uxc_firmware_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
845 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
848 ret = lt9611uxc_firmware_update(lt9611uxc);
854 static ssize_t lt9611uxc_firmware_show(struct device *dev, struct device_attribute *attr, char *buf)
856 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
858 return snprintf(buf, PAGE_SIZE, "%02x\n", lt9611uxc->fw_version);
861 static DEVICE_ATTR_RW(lt9611uxc_firmware);
863 static struct attribute *lt9611uxc_attrs[] = {
864 &dev_attr_lt9611uxc_firmware.attr,
868 static const struct attribute_group lt9611uxc_attr_group = {
869 .attrs = lt9611uxc_attrs,
872 static const struct attribute_group *lt9611uxc_attr_groups[] = {
873 <9611uxc_attr_group,
877 static int lt9611uxc_probe(struct i2c_client *client,
878 const struct i2c_device_id *id)
880 struct lt9611uxc *lt9611uxc;
881 struct device *dev = &client->dev;
883 bool fw_updated = false;
885 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
886 dev_err(dev, "device doesn't support I2C\n");
890 lt9611uxc = devm_kzalloc(dev, sizeof(*lt9611uxc), GFP_KERNEL);
894 lt9611uxc->dev = &client->dev;
895 lt9611uxc->client = client;
896 mutex_init(<9611uxc->ocm_lock);
898 lt9611uxc->regmap = devm_regmap_init_i2c(client, <9611uxc_regmap_config);
899 if (IS_ERR(lt9611uxc->regmap)) {
900 dev_err(lt9611uxc->dev, "regmap i2c init failed\n");
901 return PTR_ERR(lt9611uxc->regmap);
904 ret = lt9611uxc_parse_dt(&client->dev, lt9611uxc);
906 dev_err(dev, "failed to parse device tree\n");
910 ret = lt9611uxc_gpio_init(lt9611uxc);
914 ret = lt9611uxc_regulator_init(lt9611uxc);
918 lt9611uxc_assert_5v(lt9611uxc);
920 ret = lt9611uxc_regulator_enable(lt9611uxc);
924 lt9611uxc_reset(lt9611uxc);
926 ret = lt9611uxc_read_device_rev(lt9611uxc);
928 dev_err(dev, "failed to read chip rev\n");
929 goto err_disable_regulators;
933 ret = lt9611uxc_read_version(lt9611uxc);
935 dev_err(dev, "failed to read FW version\n");
936 goto err_disable_regulators;
937 } else if (ret == 0) {
940 dev_err(dev, "FW version 0, enforcing firmware update\n");
941 ret = lt9611uxc_firmware_update(lt9611uxc);
943 goto err_disable_regulators;
947 dev_err(dev, "FW version 0, update failed\n");
949 goto err_disable_regulators;
951 } else if (ret < 0x40) {
952 dev_info(dev, "FW version 0x%x, HPD not supported\n", ret);
954 lt9611uxc->hpd_supported = true;
956 lt9611uxc->fw_version = ret;
958 init_waitqueue_head(<9611uxc->wq);
959 INIT_WORK(<9611uxc->work, lt9611uxc_hpd_work);
961 ret = devm_request_threaded_irq(dev, client->irq, NULL,
962 lt9611uxc_irq_thread_handler,
963 IRQF_ONESHOT, "lt9611uxc", lt9611uxc);
965 dev_err(dev, "failed to request irq\n");
966 goto err_disable_regulators;
969 i2c_set_clientdata(client, lt9611uxc);
971 lt9611uxc->bridge.funcs = <9611uxc_bridge_funcs;
972 lt9611uxc->bridge.of_node = client->dev.of_node;
973 lt9611uxc->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID;
974 if (lt9611uxc->hpd_supported)
975 lt9611uxc->bridge.ops |= DRM_BRIDGE_OP_HPD;
976 lt9611uxc->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
978 drm_bridge_add(<9611uxc->bridge);
980 return lt9611uxc_audio_init(dev, lt9611uxc);
982 err_disable_regulators:
983 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
986 of_node_put(lt9611uxc->dsi1_node);
987 of_node_put(lt9611uxc->dsi0_node);
992 static int lt9611uxc_remove(struct i2c_client *client)
994 struct lt9611uxc *lt9611uxc = i2c_get_clientdata(client);
996 disable_irq(client->irq);
997 flush_scheduled_work();
998 lt9611uxc_audio_exit(lt9611uxc);
999 drm_bridge_remove(<9611uxc->bridge);
1001 mutex_destroy(<9611uxc->ocm_lock);
1003 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
1005 of_node_put(lt9611uxc->dsi1_node);
1006 of_node_put(lt9611uxc->dsi0_node);
1011 static struct i2c_device_id lt9611uxc_id[] = {
1012 { "lontium,lt9611uxc", 0 },
1016 static const struct of_device_id lt9611uxc_match_table[] = {
1017 { .compatible = "lontium,lt9611uxc" },
1020 MODULE_DEVICE_TABLE(of, lt9611uxc_match_table);
1022 static struct i2c_driver lt9611uxc_driver = {
1024 .name = "lt9611uxc",
1025 .of_match_table = lt9611uxc_match_table,
1026 .dev_groups = lt9611uxc_attr_groups,
1028 .probe = lt9611uxc_probe,
1029 .remove = lt9611uxc_remove,
1030 .id_table = lt9611uxc_id,
1032 module_i2c_driver(lt9611uxc_driver);
1034 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");
1035 MODULE_LICENSE("GPL v2");