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>
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>
20 #include <sound/hdmi-codec.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_bridge.h>
24 #include <drm/drm_mipi_dsi.h>
25 #include <drm/drm_print.h>
26 #include <drm/drm_probe_helper.h>
28 #define EDID_BLOCK_SIZE 128
29 #define EDID_NUM_BLOCKS 2
31 #define FW_FILE "lt9611uxc_fw.bin"
35 struct drm_bridge bridge;
36 struct drm_connector connector;
38 struct regmap *regmap;
39 /* Protects all accesses to registers by stopping the on-chip MCU */
40 struct mutex ocm_lock;
42 struct wait_queue_head wq;
43 struct work_struct work;
45 struct device_node *dsi0_node;
46 struct device_node *dsi1_node;
47 struct mipi_dsi_device *dsi0;
48 struct mipi_dsi_device *dsi1;
49 struct platform_device *audio_pdev;
51 struct gpio_desc *reset_gpio;
52 struct gpio_desc *enable_gpio;
54 struct regulator_bulk_data supplies[2];
56 struct i2c_client *client;
60 /* can be accessed from different threads, so protect this with ocm_lock */
65 #define LT9611_PAGE_CONTROL 0xff
67 static const struct regmap_range_cfg lt9611uxc_ranges[] = {
69 .name = "register_range",
72 .selector_reg = LT9611_PAGE_CONTROL,
73 .selector_mask = 0xff,
80 static const struct regmap_config lt9611uxc_regmap_config = {
83 .max_register = 0xffff,
84 .ranges = lt9611uxc_ranges,
85 .num_ranges = ARRAY_SIZE(lt9611uxc_ranges),
88 struct lt9611uxc_mode {
95 * This chip supports only a fixed set of modes.
96 * Enumerate them here to check whether the mode is supported.
98 static struct lt9611uxc_mode lt9611uxc_modes[] = {
117 static struct lt9611uxc *bridge_to_lt9611uxc(struct drm_bridge *bridge)
119 return container_of(bridge, struct lt9611uxc, bridge);
122 static struct lt9611uxc *connector_to_lt9611uxc(struct drm_connector *connector)
124 return container_of(connector, struct lt9611uxc, connector);
127 static void lt9611uxc_lock(struct lt9611uxc *lt9611uxc)
129 mutex_lock(<9611uxc->ocm_lock);
130 regmap_write(lt9611uxc->regmap, 0x80ee, 0x01);
133 static void lt9611uxc_unlock(struct lt9611uxc *lt9611uxc)
135 regmap_write(lt9611uxc->regmap, 0x80ee, 0x00);
137 mutex_unlock(<9611uxc->ocm_lock);
140 static irqreturn_t lt9611uxc_irq_thread_handler(int irq, void *dev_id)
142 struct lt9611uxc *lt9611uxc = dev_id;
143 unsigned int irq_status = 0;
144 unsigned int hpd_status = 0;
146 lt9611uxc_lock(lt9611uxc);
148 regmap_read(lt9611uxc->regmap, 0xb022, &irq_status);
149 regmap_read(lt9611uxc->regmap, 0xb023, &hpd_status);
151 regmap_write(lt9611uxc->regmap, 0xb022, 0);
153 if (irq_status & BIT(0)) {
154 lt9611uxc->edid_read = !!(hpd_status & BIT(0));
155 wake_up_all(<9611uxc->wq);
158 if (irq_status & BIT(1)) {
159 lt9611uxc->hdmi_connected = hpd_status & BIT(1);
160 schedule_work(<9611uxc->work);
163 lt9611uxc_unlock(lt9611uxc);
168 static void lt9611uxc_hpd_work(struct work_struct *work)
170 struct lt9611uxc *lt9611uxc = container_of(work, struct lt9611uxc, work);
173 if (lt9611uxc->connector.dev) {
174 if (lt9611uxc->connector.dev->mode_config.funcs)
175 drm_kms_helper_hotplug_event(lt9611uxc->connector.dev);
178 mutex_lock(<9611uxc->ocm_lock);
179 connected = lt9611uxc->hdmi_connected;
180 mutex_unlock(<9611uxc->ocm_lock);
182 drm_bridge_hpd_notify(<9611uxc->bridge,
184 connector_status_connected :
185 connector_status_disconnected);
189 static void lt9611uxc_reset(struct lt9611uxc *lt9611uxc)
191 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
194 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 0);
197 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
201 static void lt9611uxc_assert_5v(struct lt9611uxc *lt9611uxc)
203 if (!lt9611uxc->enable_gpio)
206 gpiod_set_value_cansleep(lt9611uxc->enable_gpio, 1);
210 static int lt9611uxc_regulator_init(struct lt9611uxc *lt9611uxc)
214 lt9611uxc->supplies[0].supply = "vdd";
215 lt9611uxc->supplies[1].supply = "vcc";
217 ret = devm_regulator_bulk_get(lt9611uxc->dev, 2, lt9611uxc->supplies);
221 return regulator_set_load(lt9611uxc->supplies[0].consumer, 200000);
224 static int lt9611uxc_regulator_enable(struct lt9611uxc *lt9611uxc)
228 ret = regulator_enable(lt9611uxc->supplies[0].consumer);
232 usleep_range(1000, 10000); /* 50000 according to dtsi */
234 ret = regulator_enable(lt9611uxc->supplies[1].consumer);
236 regulator_disable(lt9611uxc->supplies[0].consumer);
243 static struct lt9611uxc_mode *lt9611uxc_find_mode(const struct drm_display_mode *mode)
247 for (i = 0; i < ARRAY_SIZE(lt9611uxc_modes); i++) {
248 if (lt9611uxc_modes[i].hdisplay == mode->hdisplay &&
249 lt9611uxc_modes[i].vdisplay == mode->vdisplay &&
250 lt9611uxc_modes[i].vrefresh == drm_mode_vrefresh(mode)) {
251 return <9611uxc_modes[i];
258 static struct mipi_dsi_device *lt9611uxc_attach_dsi(struct lt9611uxc *lt9611uxc,
259 struct device_node *dsi_node)
261 const struct mipi_dsi_device_info info = { "lt9611uxc", 0, NULL };
262 struct mipi_dsi_device *dsi;
263 struct mipi_dsi_host *host;
264 struct device *dev = lt9611uxc->dev;
267 host = of_find_mipi_dsi_host_by_node(dsi_node);
269 dev_err(dev, "failed to find dsi host\n");
270 return ERR_PTR(-EPROBE_DEFER);
273 dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
275 dev_err(dev, "failed to create dsi device\n");
280 dsi->format = MIPI_DSI_FMT_RGB888;
281 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
282 MIPI_DSI_MODE_VIDEO_HSE;
284 ret = devm_mipi_dsi_attach(dev, dsi);
286 dev_err(dev, "failed to attach dsi to host\n");
293 static int lt9611uxc_connector_get_modes(struct drm_connector *connector)
295 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
299 edid = drm_bridge_get_edid(<9611uxc->bridge, connector);
300 drm_connector_update_edid_property(connector, edid);
301 count = drm_add_edid_modes(connector, edid);
307 static enum drm_connector_status lt9611uxc_connector_detect(struct drm_connector *connector,
310 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
312 return lt9611uxc->bridge.funcs->detect(<9611uxc->bridge);
315 static enum drm_mode_status lt9611uxc_connector_mode_valid(struct drm_connector *connector,
316 struct drm_display_mode *mode)
318 struct lt9611uxc_mode *lt9611uxc_mode = lt9611uxc_find_mode(mode);
320 return lt9611uxc_mode ? MODE_OK : MODE_BAD;
323 static const struct drm_connector_helper_funcs lt9611uxc_bridge_connector_helper_funcs = {
324 .get_modes = lt9611uxc_connector_get_modes,
325 .mode_valid = lt9611uxc_connector_mode_valid,
328 static const struct drm_connector_funcs lt9611uxc_bridge_connector_funcs = {
329 .fill_modes = drm_helper_probe_single_connector_modes,
330 .detect = lt9611uxc_connector_detect,
331 .destroy = drm_connector_cleanup,
332 .reset = drm_atomic_helper_connector_reset,
333 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
334 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
337 static int lt9611uxc_connector_init(struct drm_bridge *bridge, struct lt9611uxc *lt9611uxc)
341 if (!bridge->encoder) {
342 DRM_ERROR("Parent encoder object not found");
346 lt9611uxc->connector.polled = DRM_CONNECTOR_POLL_HPD;
348 drm_connector_helper_add(<9611uxc->connector,
349 <9611uxc_bridge_connector_helper_funcs);
350 ret = drm_connector_init(bridge->dev, <9611uxc->connector,
351 <9611uxc_bridge_connector_funcs,
352 DRM_MODE_CONNECTOR_HDMIA);
354 DRM_ERROR("Failed to initialize connector with drm\n");
358 return drm_connector_attach_encoder(<9611uxc->connector, bridge->encoder);
361 static int lt9611uxc_bridge_attach(struct drm_bridge *bridge,
362 enum drm_bridge_attach_flags flags)
364 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
367 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
368 ret = lt9611uxc_connector_init(bridge, lt9611uxc);
376 static enum drm_mode_status
377 lt9611uxc_bridge_mode_valid(struct drm_bridge *bridge,
378 const struct drm_display_info *info,
379 const struct drm_display_mode *mode)
381 struct lt9611uxc_mode *lt9611uxc_mode;
383 lt9611uxc_mode = lt9611uxc_find_mode(mode);
385 return lt9611uxc_mode ? MODE_OK : MODE_BAD;
388 static void lt9611uxc_video_setup(struct lt9611uxc *lt9611uxc,
389 const struct drm_display_mode *mode)
391 u32 h_total, hactive, hsync_len, hfront_porch;
392 u32 v_total, vactive, vsync_len, vfront_porch;
394 h_total = mode->htotal;
395 v_total = mode->vtotal;
397 hactive = mode->hdisplay;
398 hsync_len = mode->hsync_end - mode->hsync_start;
399 hfront_porch = mode->hsync_start - mode->hdisplay;
401 vactive = mode->vdisplay;
402 vsync_len = mode->vsync_end - mode->vsync_start;
403 vfront_porch = mode->vsync_start - mode->vdisplay;
405 regmap_write(lt9611uxc->regmap, 0xd00d, (u8)(v_total / 256));
406 regmap_write(lt9611uxc->regmap, 0xd00e, (u8)(v_total % 256));
408 regmap_write(lt9611uxc->regmap, 0xd00f, (u8)(vactive / 256));
409 regmap_write(lt9611uxc->regmap, 0xd010, (u8)(vactive % 256));
411 regmap_write(lt9611uxc->regmap, 0xd011, (u8)(h_total / 256));
412 regmap_write(lt9611uxc->regmap, 0xd012, (u8)(h_total % 256));
414 regmap_write(lt9611uxc->regmap, 0xd013, (u8)(hactive / 256));
415 regmap_write(lt9611uxc->regmap, 0xd014, (u8)(hactive % 256));
417 regmap_write(lt9611uxc->regmap, 0xd015, (u8)(vsync_len % 256));
419 regmap_update_bits(lt9611uxc->regmap, 0xd016, 0xf, (u8)(hsync_len / 256));
420 regmap_write(lt9611uxc->regmap, 0xd017, (u8)(hsync_len % 256));
422 regmap_update_bits(lt9611uxc->regmap, 0xd018, 0xf, (u8)(vfront_porch / 256));
423 regmap_write(lt9611uxc->regmap, 0xd019, (u8)(vfront_porch % 256));
425 regmap_update_bits(lt9611uxc->regmap, 0xd01a, 0xf, (u8)(hfront_porch / 256));
426 regmap_write(lt9611uxc->regmap, 0xd01b, (u8)(hfront_porch % 256));
429 static void lt9611uxc_bridge_mode_set(struct drm_bridge *bridge,
430 const struct drm_display_mode *mode,
431 const struct drm_display_mode *adj_mode)
433 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
435 lt9611uxc_lock(lt9611uxc);
436 lt9611uxc_video_setup(lt9611uxc, mode);
437 lt9611uxc_unlock(lt9611uxc);
440 static enum drm_connector_status lt9611uxc_bridge_detect(struct drm_bridge *bridge)
442 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
443 unsigned int reg_val = 0;
445 bool connected = true;
447 lt9611uxc_lock(lt9611uxc);
449 if (lt9611uxc->hpd_supported) {
450 ret = regmap_read(lt9611uxc->regmap, 0xb023, ®_val);
453 dev_err(lt9611uxc->dev, "failed to read hpd status: %d\n", ret);
455 connected = reg_val & BIT(1);
457 lt9611uxc->hdmi_connected = connected;
459 lt9611uxc_unlock(lt9611uxc);
461 return connected ? connector_status_connected :
462 connector_status_disconnected;
465 static int lt9611uxc_wait_for_edid(struct lt9611uxc *lt9611uxc)
467 return wait_event_interruptible_timeout(lt9611uxc->wq, lt9611uxc->edid_read,
468 msecs_to_jiffies(500));
471 static int lt9611uxc_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
473 struct lt9611uxc *lt9611uxc = data;
476 if (len > EDID_BLOCK_SIZE)
479 if (block >= EDID_NUM_BLOCKS)
482 lt9611uxc_lock(lt9611uxc);
484 regmap_write(lt9611uxc->regmap, 0xb00b, 0x10);
486 regmap_write(lt9611uxc->regmap, 0xb00a, block * EDID_BLOCK_SIZE);
488 ret = regmap_noinc_read(lt9611uxc->regmap, 0xb0b0, buf, len);
490 dev_err(lt9611uxc->dev, "edid read failed: %d\n", ret);
492 lt9611uxc_unlock(lt9611uxc);
497 static struct edid *lt9611uxc_bridge_get_edid(struct drm_bridge *bridge,
498 struct drm_connector *connector)
500 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
503 ret = lt9611uxc_wait_for_edid(lt9611uxc);
505 dev_err(lt9611uxc->dev, "wait for EDID failed: %d\n", ret);
507 } else if (ret == 0) {
508 dev_err(lt9611uxc->dev, "wait for EDID timeout\n");
512 return drm_do_get_edid(connector, lt9611uxc_get_edid_block, lt9611uxc);
515 static const struct drm_bridge_funcs lt9611uxc_bridge_funcs = {
516 .attach = lt9611uxc_bridge_attach,
517 .mode_valid = lt9611uxc_bridge_mode_valid,
518 .mode_set = lt9611uxc_bridge_mode_set,
519 .detect = lt9611uxc_bridge_detect,
520 .get_edid = lt9611uxc_bridge_get_edid,
523 static int lt9611uxc_parse_dt(struct device *dev,
524 struct lt9611uxc *lt9611uxc)
526 lt9611uxc->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
527 if (!lt9611uxc->dsi0_node) {
528 dev_err(lt9611uxc->dev, "failed to get remote node for primary dsi\n");
532 lt9611uxc->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
537 static int lt9611uxc_gpio_init(struct lt9611uxc *lt9611uxc)
539 struct device *dev = lt9611uxc->dev;
541 lt9611uxc->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
542 if (IS_ERR(lt9611uxc->reset_gpio)) {
543 dev_err(dev, "failed to acquire reset gpio\n");
544 return PTR_ERR(lt9611uxc->reset_gpio);
547 lt9611uxc->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
548 if (IS_ERR(lt9611uxc->enable_gpio)) {
549 dev_err(dev, "failed to acquire enable gpio\n");
550 return PTR_ERR(lt9611uxc->enable_gpio);
556 static int lt9611uxc_read_device_rev(struct lt9611uxc *lt9611uxc)
558 unsigned int rev0, rev1, rev2;
561 lt9611uxc_lock(lt9611uxc);
563 ret = regmap_read(lt9611uxc->regmap, 0x8100, &rev0);
564 ret |= regmap_read(lt9611uxc->regmap, 0x8101, &rev1);
565 ret |= regmap_read(lt9611uxc->regmap, 0x8102, &rev2);
567 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
569 dev_info(lt9611uxc->dev, "LT9611 revision: 0x%02x.%02x.%02x\n", rev0, rev1, rev2);
571 lt9611uxc_unlock(lt9611uxc);
576 static int lt9611uxc_read_version(struct lt9611uxc *lt9611uxc)
581 lt9611uxc_lock(lt9611uxc);
583 ret = regmap_read(lt9611uxc->regmap, 0xb021, &rev);
585 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
587 dev_info(lt9611uxc->dev, "LT9611 version: 0x%02x\n", rev);
589 lt9611uxc_unlock(lt9611uxc);
591 return ret < 0 ? ret : rev;
594 static int lt9611uxc_hdmi_hw_params(struct device *dev, void *data,
595 struct hdmi_codec_daifmt *fmt,
596 struct hdmi_codec_params *hparms)
599 * LT9611UXC will automatically detect rate and sample size, so no need
600 * to setup anything here.
605 static void lt9611uxc_audio_shutdown(struct device *dev, void *data)
609 static int lt9611uxc_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
610 struct device_node *endpoint)
612 struct of_endpoint of_ep;
615 ret = of_graph_parse_endpoint(endpoint, &of_ep);
620 * HDMI sound should be located as reg = <2>
621 * Then, it is sound port 0
629 static const struct hdmi_codec_ops lt9611uxc_codec_ops = {
630 .hw_params = lt9611uxc_hdmi_hw_params,
631 .audio_shutdown = lt9611uxc_audio_shutdown,
632 .get_dai_id = lt9611uxc_hdmi_i2s_get_dai_id,
635 static int lt9611uxc_audio_init(struct device *dev, struct lt9611uxc *lt9611uxc)
637 struct hdmi_codec_pdata codec_data = {
638 .ops = <9611uxc_codec_ops,
639 .max_i2s_channels = 2,
644 lt9611uxc->audio_pdev =
645 platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
647 &codec_data, sizeof(codec_data));
649 return PTR_ERR_OR_ZERO(lt9611uxc->audio_pdev);
652 static void lt9611uxc_audio_exit(struct lt9611uxc *lt9611uxc)
654 if (lt9611uxc->audio_pdev) {
655 platform_device_unregister(lt9611uxc->audio_pdev);
656 lt9611uxc->audio_pdev = NULL;
660 #define LT9611UXC_FW_PAGE_SIZE 32
661 static void lt9611uxc_firmware_write_page(struct lt9611uxc *lt9611uxc, u16 addr, const u8 *buf)
663 struct reg_sequence seq_write_prepare[] = {
664 REG_SEQ0(0x805a, 0x04),
665 REG_SEQ0(0x805a, 0x00),
667 REG_SEQ0(0x805e, 0xdf),
668 REG_SEQ0(0x805a, 0x20),
669 REG_SEQ0(0x805a, 0x00),
670 REG_SEQ0(0x8058, 0x21),
673 struct reg_sequence seq_write_addr[] = {
674 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
675 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
676 REG_SEQ0(0x805d, addr & 0xff),
677 REG_SEQ0(0x805a, 0x10),
678 REG_SEQ0(0x805a, 0x00),
681 regmap_write(lt9611uxc->regmap, 0x8108, 0xbf);
683 regmap_write(lt9611uxc->regmap, 0x8108, 0xff);
685 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_prepare, ARRAY_SIZE(seq_write_prepare));
686 regmap_noinc_write(lt9611uxc->regmap, 0x8059, buf, LT9611UXC_FW_PAGE_SIZE);
687 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_addr, ARRAY_SIZE(seq_write_addr));
691 static void lt9611uxc_firmware_read_page(struct lt9611uxc *lt9611uxc, u16 addr, char *buf)
693 struct reg_sequence seq_read_page[] = {
694 REG_SEQ0(0x805a, 0xa0),
695 REG_SEQ0(0x805a, 0x80),
696 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
697 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
698 REG_SEQ0(0x805d, addr & 0xff),
699 REG_SEQ0(0x805a, 0x90),
700 REG_SEQ0(0x805a, 0x80),
701 REG_SEQ0(0x8058, 0x21),
704 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_page, ARRAY_SIZE(seq_read_page));
705 regmap_noinc_read(lt9611uxc->regmap, 0x805f, buf, LT9611UXC_FW_PAGE_SIZE);
708 static char *lt9611uxc_firmware_read(struct lt9611uxc *lt9611uxc, size_t size)
710 struct reg_sequence seq_read_setup[] = {
711 REG_SEQ0(0x805a, 0x84),
712 REG_SEQ0(0x805a, 0x80),
718 readbuf = kzalloc(ALIGN(size, 32), GFP_KERNEL);
722 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_setup, ARRAY_SIZE(seq_read_setup));
726 offset += LT9611UXC_FW_PAGE_SIZE)
727 lt9611uxc_firmware_read_page(lt9611uxc, offset, &readbuf[offset]);
732 static int lt9611uxc_firmware_update(struct lt9611uxc *lt9611uxc)
738 const struct firmware *fw;
740 struct reg_sequence seq_setup[] = {
741 REG_SEQ0(0x805e, 0xdf),
742 REG_SEQ0(0x8058, 0x00),
743 REG_SEQ0(0x8059, 0x50),
744 REG_SEQ0(0x805a, 0x10),
745 REG_SEQ0(0x805a, 0x00),
749 struct reg_sequence seq_block_erase[] = {
750 REG_SEQ0(0x805a, 0x04),
751 REG_SEQ0(0x805a, 0x00),
752 REG_SEQ0(0x805b, 0x00),
753 REG_SEQ0(0x805c, 0x00),
754 REG_SEQ0(0x805d, 0x00),
755 REG_SEQ0(0x805a, 0x01),
756 REG_SEQ0(0x805a, 0x00),
759 ret = request_firmware(&fw, FW_FILE, lt9611uxc->dev);
763 dev_info(lt9611uxc->dev, "Updating firmware\n");
764 lt9611uxc_lock(lt9611uxc);
766 regmap_multi_reg_write(lt9611uxc->regmap, seq_setup, ARRAY_SIZE(seq_setup));
769 * Need erase block 2 timess here. Sometimes, block erase can fail.
770 * This is a workaroud.
772 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
774 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
777 for (offset = 0, remain = fw->size;
778 remain >= LT9611UXC_FW_PAGE_SIZE;
779 offset += LT9611UXC_FW_PAGE_SIZE, remain -= LT9611UXC_FW_PAGE_SIZE)
780 lt9611uxc_firmware_write_page(lt9611uxc, offset, fw->data + offset);
783 char buf[LT9611UXC_FW_PAGE_SIZE];
785 memset(buf, 0xff, LT9611UXC_FW_PAGE_SIZE);
786 memcpy(buf, fw->data + offset, remain);
787 lt9611uxc_firmware_write_page(lt9611uxc, offset, buf);
791 readbuf = lt9611uxc_firmware_read(lt9611uxc, fw->size);
797 if (!memcmp(readbuf, fw->data, fw->size)) {
798 dev_err(lt9611uxc->dev, "Firmware update failed\n");
799 print_hex_dump(KERN_ERR, "fw: ", DUMP_PREFIX_OFFSET, 16, 1, readbuf, fw->size, false);
802 dev_info(lt9611uxc->dev, "Firmware updates successfully\n");
808 lt9611uxc_unlock(lt9611uxc);
809 lt9611uxc_reset(lt9611uxc);
810 release_firmware(fw);
815 static ssize_t lt9611uxc_firmware_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
817 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
820 ret = lt9611uxc_firmware_update(lt9611uxc);
826 static ssize_t lt9611uxc_firmware_show(struct device *dev, struct device_attribute *attr, char *buf)
828 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
830 return sysfs_emit(buf, "%02x\n", lt9611uxc->fw_version);
833 static DEVICE_ATTR_RW(lt9611uxc_firmware);
835 static struct attribute *lt9611uxc_attrs[] = {
836 &dev_attr_lt9611uxc_firmware.attr,
840 static const struct attribute_group lt9611uxc_attr_group = {
841 .attrs = lt9611uxc_attrs,
844 static const struct attribute_group *lt9611uxc_attr_groups[] = {
845 <9611uxc_attr_group,
849 static int lt9611uxc_probe(struct i2c_client *client)
851 struct lt9611uxc *lt9611uxc;
852 struct device *dev = &client->dev;
854 bool fw_updated = false;
856 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
857 dev_err(dev, "device doesn't support I2C\n");
861 lt9611uxc = devm_kzalloc(dev, sizeof(*lt9611uxc), GFP_KERNEL);
865 lt9611uxc->dev = dev;
866 lt9611uxc->client = client;
867 mutex_init(<9611uxc->ocm_lock);
869 lt9611uxc->regmap = devm_regmap_init_i2c(client, <9611uxc_regmap_config);
870 if (IS_ERR(lt9611uxc->regmap)) {
871 dev_err(lt9611uxc->dev, "regmap i2c init failed\n");
872 return PTR_ERR(lt9611uxc->regmap);
875 ret = lt9611uxc_parse_dt(dev, lt9611uxc);
877 dev_err(dev, "failed to parse device tree\n");
881 ret = lt9611uxc_gpio_init(lt9611uxc);
885 ret = lt9611uxc_regulator_init(lt9611uxc);
889 lt9611uxc_assert_5v(lt9611uxc);
891 ret = lt9611uxc_regulator_enable(lt9611uxc);
895 lt9611uxc_reset(lt9611uxc);
897 ret = lt9611uxc_read_device_rev(lt9611uxc);
899 dev_err(dev, "failed to read chip rev\n");
900 goto err_disable_regulators;
904 ret = lt9611uxc_read_version(lt9611uxc);
906 dev_err(dev, "failed to read FW version\n");
907 goto err_disable_regulators;
908 } else if (ret == 0) {
911 dev_err(dev, "FW version 0, enforcing firmware update\n");
912 ret = lt9611uxc_firmware_update(lt9611uxc);
914 goto err_disable_regulators;
918 dev_err(dev, "FW version 0, update failed\n");
920 goto err_disable_regulators;
922 } else if (ret < 0x40) {
923 dev_info(dev, "FW version 0x%x, HPD not supported\n", ret);
925 lt9611uxc->hpd_supported = true;
927 lt9611uxc->fw_version = ret;
929 init_waitqueue_head(<9611uxc->wq);
930 INIT_WORK(<9611uxc->work, lt9611uxc_hpd_work);
932 ret = request_threaded_irq(client->irq, NULL,
933 lt9611uxc_irq_thread_handler,
934 IRQF_ONESHOT, "lt9611uxc", lt9611uxc);
936 dev_err(dev, "failed to request irq\n");
937 goto err_disable_regulators;
940 i2c_set_clientdata(client, lt9611uxc);
942 lt9611uxc->bridge.funcs = <9611uxc_bridge_funcs;
943 lt9611uxc->bridge.of_node = client->dev.of_node;
944 lt9611uxc->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID;
945 if (lt9611uxc->hpd_supported)
946 lt9611uxc->bridge.ops |= DRM_BRIDGE_OP_HPD;
947 lt9611uxc->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
949 drm_bridge_add(<9611uxc->bridge);
951 /* Attach primary DSI */
952 lt9611uxc->dsi0 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi0_node);
953 if (IS_ERR(lt9611uxc->dsi0)) {
954 ret = PTR_ERR(lt9611uxc->dsi0);
955 goto err_remove_bridge;
958 /* Attach secondary DSI, if specified */
959 if (lt9611uxc->dsi1_node) {
960 lt9611uxc->dsi1 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi1_node);
961 if (IS_ERR(lt9611uxc->dsi1)) {
962 ret = PTR_ERR(lt9611uxc->dsi1);
963 goto err_remove_bridge;
967 return lt9611uxc_audio_init(dev, lt9611uxc);
970 free_irq(client->irq, lt9611uxc);
971 cancel_work_sync(<9611uxc->work);
972 drm_bridge_remove(<9611uxc->bridge);
974 err_disable_regulators:
975 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
978 of_node_put(lt9611uxc->dsi1_node);
979 of_node_put(lt9611uxc->dsi0_node);
984 static void lt9611uxc_remove(struct i2c_client *client)
986 struct lt9611uxc *lt9611uxc = i2c_get_clientdata(client);
988 free_irq(client->irq, lt9611uxc);
989 cancel_work_sync(<9611uxc->work);
990 lt9611uxc_audio_exit(lt9611uxc);
991 drm_bridge_remove(<9611uxc->bridge);
993 mutex_destroy(<9611uxc->ocm_lock);
995 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
997 of_node_put(lt9611uxc->dsi1_node);
998 of_node_put(lt9611uxc->dsi0_node);
1001 static struct i2c_device_id lt9611uxc_id[] = {
1002 { "lontium,lt9611uxc", 0 },
1006 static const struct of_device_id lt9611uxc_match_table[] = {
1007 { .compatible = "lontium,lt9611uxc" },
1010 MODULE_DEVICE_TABLE(of, lt9611uxc_match_table);
1012 static struct i2c_driver lt9611uxc_driver = {
1014 .name = "lt9611uxc",
1015 .of_match_table = lt9611uxc_match_table,
1016 .dev_groups = lt9611uxc_attr_groups,
1018 .probe = lt9611uxc_probe,
1019 .remove = lt9611uxc_remove,
1020 .id_table = lt9611uxc_id,
1022 module_i2c_driver(lt9611uxc_driver);
1024 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");
1025 MODULE_LICENSE("GPL v2");
1027 MODULE_FIRMWARE(FW_FILE);