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_edid.h>
25 #include <drm/drm_mipi_dsi.h>
26 #include <drm/drm_print.h>
27 #include <drm/drm_probe_helper.h>
29 #define EDID_BLOCK_SIZE 128
30 #define EDID_NUM_BLOCKS 2
32 #define FW_FILE "lt9611uxc_fw.bin"
36 struct drm_bridge bridge;
37 struct drm_connector connector;
39 struct regmap *regmap;
40 /* Protects all accesses to registers by stopping the on-chip MCU */
41 struct mutex ocm_lock;
43 struct wait_queue_head wq;
44 struct work_struct work;
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;
52 struct gpio_desc *reset_gpio;
53 struct gpio_desc *enable_gpio;
55 struct regulator_bulk_data supplies[2];
57 struct i2c_client *client;
61 /* can be accessed from different threads, so protect this with ocm_lock */
66 #define LT9611_PAGE_CONTROL 0xff
68 static const struct regmap_range_cfg lt9611uxc_ranges[] = {
70 .name = "register_range",
73 .selector_reg = LT9611_PAGE_CONTROL,
74 .selector_mask = 0xff,
81 static const struct regmap_config lt9611uxc_regmap_config = {
84 .max_register = 0xffff,
85 .ranges = lt9611uxc_ranges,
86 .num_ranges = ARRAY_SIZE(lt9611uxc_ranges),
89 struct lt9611uxc_mode {
96 * This chip supports only a fixed set of modes.
97 * Enumerate them here to check whether the mode is supported.
99 static struct lt9611uxc_mode lt9611uxc_modes[] = {
118 static struct lt9611uxc *bridge_to_lt9611uxc(struct drm_bridge *bridge)
120 return container_of(bridge, struct lt9611uxc, bridge);
123 static struct lt9611uxc *connector_to_lt9611uxc(struct drm_connector *connector)
125 return container_of(connector, struct lt9611uxc, connector);
128 static void lt9611uxc_lock(struct lt9611uxc *lt9611uxc)
130 mutex_lock(<9611uxc->ocm_lock);
131 regmap_write(lt9611uxc->regmap, 0x80ee, 0x01);
134 static void lt9611uxc_unlock(struct lt9611uxc *lt9611uxc)
136 regmap_write(lt9611uxc->regmap, 0x80ee, 0x00);
138 mutex_unlock(<9611uxc->ocm_lock);
141 static irqreturn_t lt9611uxc_irq_thread_handler(int irq, void *dev_id)
143 struct lt9611uxc *lt9611uxc = dev_id;
144 unsigned int irq_status = 0;
145 unsigned int hpd_status = 0;
147 lt9611uxc_lock(lt9611uxc);
149 regmap_read(lt9611uxc->regmap, 0xb022, &irq_status);
150 regmap_read(lt9611uxc->regmap, 0xb023, &hpd_status);
152 regmap_write(lt9611uxc->regmap, 0xb022, 0);
154 if (irq_status & BIT(0)) {
155 lt9611uxc->edid_read = !!(hpd_status & BIT(0));
156 wake_up_all(<9611uxc->wq);
159 if (irq_status & BIT(1)) {
160 lt9611uxc->hdmi_connected = hpd_status & BIT(1);
161 schedule_work(<9611uxc->work);
164 lt9611uxc_unlock(lt9611uxc);
169 static void lt9611uxc_hpd_work(struct work_struct *work)
171 struct lt9611uxc *lt9611uxc = container_of(work, struct lt9611uxc, work);
174 if (lt9611uxc->connector.dev) {
175 if (lt9611uxc->connector.dev->mode_config.funcs)
176 drm_kms_helper_hotplug_event(lt9611uxc->connector.dev);
179 mutex_lock(<9611uxc->ocm_lock);
180 connected = lt9611uxc->hdmi_connected;
181 mutex_unlock(<9611uxc->ocm_lock);
183 drm_bridge_hpd_notify(<9611uxc->bridge,
185 connector_status_connected :
186 connector_status_disconnected);
190 static void lt9611uxc_reset(struct lt9611uxc *lt9611uxc)
192 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
195 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 0);
198 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
202 static void lt9611uxc_assert_5v(struct lt9611uxc *lt9611uxc)
204 if (!lt9611uxc->enable_gpio)
207 gpiod_set_value_cansleep(lt9611uxc->enable_gpio, 1);
211 static int lt9611uxc_regulator_init(struct lt9611uxc *lt9611uxc)
215 lt9611uxc->supplies[0].supply = "vdd";
216 lt9611uxc->supplies[1].supply = "vcc";
218 ret = devm_regulator_bulk_get(lt9611uxc->dev, 2, lt9611uxc->supplies);
222 return regulator_set_load(lt9611uxc->supplies[0].consumer, 200000);
225 static int lt9611uxc_regulator_enable(struct lt9611uxc *lt9611uxc)
229 ret = regulator_enable(lt9611uxc->supplies[0].consumer);
233 usleep_range(1000, 10000); /* 50000 according to dtsi */
235 ret = regulator_enable(lt9611uxc->supplies[1].consumer);
237 regulator_disable(lt9611uxc->supplies[0].consumer);
244 static struct lt9611uxc_mode *lt9611uxc_find_mode(const struct drm_display_mode *mode)
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];
259 static struct mipi_dsi_device *lt9611uxc_attach_dsi(struct lt9611uxc *lt9611uxc,
260 struct device_node *dsi_node)
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;
268 host = of_find_mipi_dsi_host_by_node(dsi_node);
270 dev_err(dev, "failed to find dsi host\n");
271 return ERR_PTR(-EPROBE_DEFER);
274 dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
276 dev_err(dev, "failed to create dsi device\n");
281 dsi->format = MIPI_DSI_FMT_RGB888;
282 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
283 MIPI_DSI_MODE_VIDEO_HSE;
285 ret = devm_mipi_dsi_attach(dev, dsi);
287 dev_err(dev, "failed to attach dsi to host\n");
294 static int lt9611uxc_connector_get_modes(struct drm_connector *connector)
296 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
297 const struct drm_edid *drm_edid;
300 drm_edid = drm_bridge_edid_read(<9611uxc->bridge, connector);
301 drm_edid_connector_update(connector, drm_edid);
302 count = drm_edid_connector_add_modes(connector);
303 drm_edid_free(drm_edid);
308 static enum drm_connector_status lt9611uxc_connector_detect(struct drm_connector *connector,
311 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
313 return lt9611uxc->bridge.funcs->detect(<9611uxc->bridge);
316 static enum drm_mode_status lt9611uxc_connector_mode_valid(struct drm_connector *connector,
317 struct drm_display_mode *mode)
319 struct lt9611uxc_mode *lt9611uxc_mode = lt9611uxc_find_mode(mode);
321 return lt9611uxc_mode ? MODE_OK : MODE_BAD;
324 static const struct drm_connector_helper_funcs lt9611uxc_bridge_connector_helper_funcs = {
325 .get_modes = lt9611uxc_connector_get_modes,
326 .mode_valid = lt9611uxc_connector_mode_valid,
329 static const struct drm_connector_funcs lt9611uxc_bridge_connector_funcs = {
330 .fill_modes = drm_helper_probe_single_connector_modes,
331 .detect = lt9611uxc_connector_detect,
332 .destroy = drm_connector_cleanup,
333 .reset = drm_atomic_helper_connector_reset,
334 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
335 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
338 static int lt9611uxc_connector_init(struct drm_bridge *bridge, struct lt9611uxc *lt9611uxc)
342 if (!bridge->encoder) {
343 DRM_ERROR("Parent encoder object not found");
347 lt9611uxc->connector.polled = DRM_CONNECTOR_POLL_HPD;
349 drm_connector_helper_add(<9611uxc->connector,
350 <9611uxc_bridge_connector_helper_funcs);
351 ret = drm_connector_init(bridge->dev, <9611uxc->connector,
352 <9611uxc_bridge_connector_funcs,
353 DRM_MODE_CONNECTOR_HDMIA);
355 DRM_ERROR("Failed to initialize connector with drm\n");
359 return drm_connector_attach_encoder(<9611uxc->connector, bridge->encoder);
362 static int lt9611uxc_bridge_attach(struct drm_bridge *bridge,
363 enum drm_bridge_attach_flags flags)
365 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
368 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
369 ret = lt9611uxc_connector_init(bridge, lt9611uxc);
377 static enum drm_mode_status
378 lt9611uxc_bridge_mode_valid(struct drm_bridge *bridge,
379 const struct drm_display_info *info,
380 const struct drm_display_mode *mode)
382 struct lt9611uxc_mode *lt9611uxc_mode;
384 lt9611uxc_mode = lt9611uxc_find_mode(mode);
386 return lt9611uxc_mode ? MODE_OK : MODE_BAD;
389 static void lt9611uxc_video_setup(struct lt9611uxc *lt9611uxc,
390 const struct drm_display_mode *mode)
392 u32 h_total, hactive, hsync_len, hfront_porch;
393 u32 v_total, vactive, vsync_len, vfront_porch;
395 h_total = mode->htotal;
396 v_total = mode->vtotal;
398 hactive = mode->hdisplay;
399 hsync_len = mode->hsync_end - mode->hsync_start;
400 hfront_porch = mode->hsync_start - mode->hdisplay;
402 vactive = mode->vdisplay;
403 vsync_len = mode->vsync_end - mode->vsync_start;
404 vfront_porch = mode->vsync_start - mode->vdisplay;
406 regmap_write(lt9611uxc->regmap, 0xd00d, (u8)(v_total / 256));
407 regmap_write(lt9611uxc->regmap, 0xd00e, (u8)(v_total % 256));
409 regmap_write(lt9611uxc->regmap, 0xd00f, (u8)(vactive / 256));
410 regmap_write(lt9611uxc->regmap, 0xd010, (u8)(vactive % 256));
412 regmap_write(lt9611uxc->regmap, 0xd011, (u8)(h_total / 256));
413 regmap_write(lt9611uxc->regmap, 0xd012, (u8)(h_total % 256));
415 regmap_write(lt9611uxc->regmap, 0xd013, (u8)(hactive / 256));
416 regmap_write(lt9611uxc->regmap, 0xd014, (u8)(hactive % 256));
418 regmap_write(lt9611uxc->regmap, 0xd015, (u8)(vsync_len % 256));
420 regmap_update_bits(lt9611uxc->regmap, 0xd016, 0xf, (u8)(hsync_len / 256));
421 regmap_write(lt9611uxc->regmap, 0xd017, (u8)(hsync_len % 256));
423 regmap_update_bits(lt9611uxc->regmap, 0xd018, 0xf, (u8)(vfront_porch / 256));
424 regmap_write(lt9611uxc->regmap, 0xd019, (u8)(vfront_porch % 256));
426 regmap_update_bits(lt9611uxc->regmap, 0xd01a, 0xf, (u8)(hfront_porch / 256));
427 regmap_write(lt9611uxc->regmap, 0xd01b, (u8)(hfront_porch % 256));
430 static void lt9611uxc_bridge_mode_set(struct drm_bridge *bridge,
431 const struct drm_display_mode *mode,
432 const struct drm_display_mode *adj_mode)
434 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
436 lt9611uxc_lock(lt9611uxc);
437 lt9611uxc_video_setup(lt9611uxc, mode);
438 lt9611uxc_unlock(lt9611uxc);
441 static enum drm_connector_status lt9611uxc_bridge_detect(struct drm_bridge *bridge)
443 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
444 unsigned int reg_val = 0;
446 bool connected = true;
448 lt9611uxc_lock(lt9611uxc);
450 if (lt9611uxc->hpd_supported) {
451 ret = regmap_read(lt9611uxc->regmap, 0xb023, ®_val);
454 dev_err(lt9611uxc->dev, "failed to read hpd status: %d\n", ret);
456 connected = reg_val & BIT(1);
458 lt9611uxc->hdmi_connected = connected;
460 lt9611uxc_unlock(lt9611uxc);
462 return connected ? connector_status_connected :
463 connector_status_disconnected;
466 static int lt9611uxc_wait_for_edid(struct lt9611uxc *lt9611uxc)
468 return wait_event_interruptible_timeout(lt9611uxc->wq, lt9611uxc->edid_read,
469 msecs_to_jiffies(500));
472 static int lt9611uxc_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
474 struct lt9611uxc *lt9611uxc = data;
477 if (len > EDID_BLOCK_SIZE)
480 if (block >= EDID_NUM_BLOCKS)
483 lt9611uxc_lock(lt9611uxc);
485 regmap_write(lt9611uxc->regmap, 0xb00b, 0x10);
487 regmap_write(lt9611uxc->regmap, 0xb00a, block * EDID_BLOCK_SIZE);
489 ret = regmap_noinc_read(lt9611uxc->regmap, 0xb0b0, buf, len);
491 dev_err(lt9611uxc->dev, "edid read failed: %d\n", ret);
493 lt9611uxc_unlock(lt9611uxc);
498 static const struct drm_edid *lt9611uxc_bridge_edid_read(struct drm_bridge *bridge,
499 struct drm_connector *connector)
501 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
504 ret = lt9611uxc_wait_for_edid(lt9611uxc);
506 dev_err(lt9611uxc->dev, "wait for EDID failed: %d\n", ret);
508 } else if (ret == 0) {
509 dev_err(lt9611uxc->dev, "wait for EDID timeout\n");
513 return drm_edid_read_custom(connector, lt9611uxc_get_edid_block, lt9611uxc);
516 static const struct drm_bridge_funcs lt9611uxc_bridge_funcs = {
517 .attach = lt9611uxc_bridge_attach,
518 .mode_valid = lt9611uxc_bridge_mode_valid,
519 .mode_set = lt9611uxc_bridge_mode_set,
520 .detect = lt9611uxc_bridge_detect,
521 .edid_read = lt9611uxc_bridge_edid_read,
524 static int lt9611uxc_parse_dt(struct device *dev,
525 struct lt9611uxc *lt9611uxc)
527 lt9611uxc->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
528 if (!lt9611uxc->dsi0_node) {
529 dev_err(lt9611uxc->dev, "failed to get remote node for primary dsi\n");
533 lt9611uxc->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
538 static int lt9611uxc_gpio_init(struct lt9611uxc *lt9611uxc)
540 struct device *dev = lt9611uxc->dev;
542 lt9611uxc->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
543 if (IS_ERR(lt9611uxc->reset_gpio)) {
544 dev_err(dev, "failed to acquire reset gpio\n");
545 return PTR_ERR(lt9611uxc->reset_gpio);
548 lt9611uxc->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
549 if (IS_ERR(lt9611uxc->enable_gpio)) {
550 dev_err(dev, "failed to acquire enable gpio\n");
551 return PTR_ERR(lt9611uxc->enable_gpio);
557 static int lt9611uxc_read_device_rev(struct lt9611uxc *lt9611uxc)
559 unsigned int rev0, rev1, rev2;
562 lt9611uxc_lock(lt9611uxc);
564 ret = regmap_read(lt9611uxc->regmap, 0x8100, &rev0);
565 ret |= regmap_read(lt9611uxc->regmap, 0x8101, &rev1);
566 ret |= regmap_read(lt9611uxc->regmap, 0x8102, &rev2);
568 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
570 dev_info(lt9611uxc->dev, "LT9611 revision: 0x%02x.%02x.%02x\n", rev0, rev1, rev2);
572 lt9611uxc_unlock(lt9611uxc);
577 static int lt9611uxc_read_version(struct lt9611uxc *lt9611uxc)
582 lt9611uxc_lock(lt9611uxc);
584 ret = regmap_read(lt9611uxc->regmap, 0xb021, &rev);
586 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
588 dev_info(lt9611uxc->dev, "LT9611 version: 0x%02x\n", rev);
590 lt9611uxc_unlock(lt9611uxc);
592 return ret < 0 ? ret : rev;
595 static int lt9611uxc_hdmi_hw_params(struct device *dev, void *data,
596 struct hdmi_codec_daifmt *fmt,
597 struct hdmi_codec_params *hparms)
600 * LT9611UXC will automatically detect rate and sample size, so no need
601 * to setup anything here.
606 static void lt9611uxc_audio_shutdown(struct device *dev, void *data)
610 static int lt9611uxc_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
611 struct device_node *endpoint)
613 struct of_endpoint of_ep;
616 ret = of_graph_parse_endpoint(endpoint, &of_ep);
621 * HDMI sound should be located as reg = <2>
622 * Then, it is sound port 0
630 static const struct hdmi_codec_ops lt9611uxc_codec_ops = {
631 .hw_params = lt9611uxc_hdmi_hw_params,
632 .audio_shutdown = lt9611uxc_audio_shutdown,
633 .get_dai_id = lt9611uxc_hdmi_i2s_get_dai_id,
636 static int lt9611uxc_audio_init(struct device *dev, struct lt9611uxc *lt9611uxc)
638 struct hdmi_codec_pdata codec_data = {
639 .ops = <9611uxc_codec_ops,
640 .max_i2s_channels = 2,
645 lt9611uxc->audio_pdev =
646 platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
648 &codec_data, sizeof(codec_data));
650 return PTR_ERR_OR_ZERO(lt9611uxc->audio_pdev);
653 static void lt9611uxc_audio_exit(struct lt9611uxc *lt9611uxc)
655 if (lt9611uxc->audio_pdev) {
656 platform_device_unregister(lt9611uxc->audio_pdev);
657 lt9611uxc->audio_pdev = NULL;
661 #define LT9611UXC_FW_PAGE_SIZE 32
662 static void lt9611uxc_firmware_write_page(struct lt9611uxc *lt9611uxc, u16 addr, const u8 *buf)
664 struct reg_sequence seq_write_prepare[] = {
665 REG_SEQ0(0x805a, 0x04),
666 REG_SEQ0(0x805a, 0x00),
668 REG_SEQ0(0x805e, 0xdf),
669 REG_SEQ0(0x805a, 0x20),
670 REG_SEQ0(0x805a, 0x00),
671 REG_SEQ0(0x8058, 0x21),
674 struct reg_sequence seq_write_addr[] = {
675 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
676 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
677 REG_SEQ0(0x805d, addr & 0xff),
678 REG_SEQ0(0x805a, 0x10),
679 REG_SEQ0(0x805a, 0x00),
682 regmap_write(lt9611uxc->regmap, 0x8108, 0xbf);
684 regmap_write(lt9611uxc->regmap, 0x8108, 0xff);
686 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_prepare, ARRAY_SIZE(seq_write_prepare));
687 regmap_noinc_write(lt9611uxc->regmap, 0x8059, buf, LT9611UXC_FW_PAGE_SIZE);
688 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_addr, ARRAY_SIZE(seq_write_addr));
692 static void lt9611uxc_firmware_read_page(struct lt9611uxc *lt9611uxc, u16 addr, char *buf)
694 struct reg_sequence seq_read_page[] = {
695 REG_SEQ0(0x805a, 0xa0),
696 REG_SEQ0(0x805a, 0x80),
697 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
698 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
699 REG_SEQ0(0x805d, addr & 0xff),
700 REG_SEQ0(0x805a, 0x90),
701 REG_SEQ0(0x805a, 0x80),
702 REG_SEQ0(0x8058, 0x21),
705 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_page, ARRAY_SIZE(seq_read_page));
706 regmap_noinc_read(lt9611uxc->regmap, 0x805f, buf, LT9611UXC_FW_PAGE_SIZE);
709 static char *lt9611uxc_firmware_read(struct lt9611uxc *lt9611uxc, size_t size)
711 struct reg_sequence seq_read_setup[] = {
712 REG_SEQ0(0x805a, 0x84),
713 REG_SEQ0(0x805a, 0x80),
719 readbuf = kzalloc(ALIGN(size, 32), GFP_KERNEL);
723 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_setup, ARRAY_SIZE(seq_read_setup));
727 offset += LT9611UXC_FW_PAGE_SIZE)
728 lt9611uxc_firmware_read_page(lt9611uxc, offset, &readbuf[offset]);
733 static int lt9611uxc_firmware_update(struct lt9611uxc *lt9611uxc)
739 const struct firmware *fw;
741 struct reg_sequence seq_setup[] = {
742 REG_SEQ0(0x805e, 0xdf),
743 REG_SEQ0(0x8058, 0x00),
744 REG_SEQ0(0x8059, 0x50),
745 REG_SEQ0(0x805a, 0x10),
746 REG_SEQ0(0x805a, 0x00),
750 struct reg_sequence seq_block_erase[] = {
751 REG_SEQ0(0x805a, 0x04),
752 REG_SEQ0(0x805a, 0x00),
753 REG_SEQ0(0x805b, 0x00),
754 REG_SEQ0(0x805c, 0x00),
755 REG_SEQ0(0x805d, 0x00),
756 REG_SEQ0(0x805a, 0x01),
757 REG_SEQ0(0x805a, 0x00),
760 ret = request_firmware(&fw, FW_FILE, lt9611uxc->dev);
764 dev_info(lt9611uxc->dev, "Updating firmware\n");
765 lt9611uxc_lock(lt9611uxc);
767 regmap_multi_reg_write(lt9611uxc->regmap, seq_setup, ARRAY_SIZE(seq_setup));
770 * Need erase block 2 timess here. Sometimes, block erase can fail.
771 * This is a workaroud.
773 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
775 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
778 for (offset = 0, remain = fw->size;
779 remain >= LT9611UXC_FW_PAGE_SIZE;
780 offset += LT9611UXC_FW_PAGE_SIZE, remain -= LT9611UXC_FW_PAGE_SIZE)
781 lt9611uxc_firmware_write_page(lt9611uxc, offset, fw->data + offset);
784 char buf[LT9611UXC_FW_PAGE_SIZE];
786 memset(buf, 0xff, LT9611UXC_FW_PAGE_SIZE);
787 memcpy(buf, fw->data + offset, remain);
788 lt9611uxc_firmware_write_page(lt9611uxc, offset, buf);
792 readbuf = lt9611uxc_firmware_read(lt9611uxc, fw->size);
798 if (!memcmp(readbuf, fw->data, fw->size)) {
799 dev_err(lt9611uxc->dev, "Firmware update failed\n");
800 print_hex_dump(KERN_ERR, "fw: ", DUMP_PREFIX_OFFSET, 16, 1, readbuf, fw->size, false);
803 dev_info(lt9611uxc->dev, "Firmware updates successfully\n");
809 lt9611uxc_unlock(lt9611uxc);
810 lt9611uxc_reset(lt9611uxc);
811 release_firmware(fw);
816 static ssize_t lt9611uxc_firmware_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
818 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
821 ret = lt9611uxc_firmware_update(lt9611uxc);
827 static ssize_t lt9611uxc_firmware_show(struct device *dev, struct device_attribute *attr, char *buf)
829 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
831 return sysfs_emit(buf, "%02x\n", lt9611uxc->fw_version);
834 static DEVICE_ATTR_RW(lt9611uxc_firmware);
836 static struct attribute *lt9611uxc_attrs[] = {
837 &dev_attr_lt9611uxc_firmware.attr,
841 static const struct attribute_group lt9611uxc_attr_group = {
842 .attrs = lt9611uxc_attrs,
845 static const struct attribute_group *lt9611uxc_attr_groups[] = {
846 <9611uxc_attr_group,
850 static int lt9611uxc_probe(struct i2c_client *client)
852 struct lt9611uxc *lt9611uxc;
853 struct device *dev = &client->dev;
855 bool fw_updated = false;
857 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
858 dev_err(dev, "device doesn't support I2C\n");
862 lt9611uxc = devm_kzalloc(dev, sizeof(*lt9611uxc), GFP_KERNEL);
866 lt9611uxc->dev = dev;
867 lt9611uxc->client = client;
868 mutex_init(<9611uxc->ocm_lock);
870 lt9611uxc->regmap = devm_regmap_init_i2c(client, <9611uxc_regmap_config);
871 if (IS_ERR(lt9611uxc->regmap)) {
872 dev_err(lt9611uxc->dev, "regmap i2c init failed\n");
873 return PTR_ERR(lt9611uxc->regmap);
876 ret = lt9611uxc_parse_dt(dev, lt9611uxc);
878 dev_err(dev, "failed to parse device tree\n");
882 ret = lt9611uxc_gpio_init(lt9611uxc);
886 ret = lt9611uxc_regulator_init(lt9611uxc);
890 lt9611uxc_assert_5v(lt9611uxc);
892 ret = lt9611uxc_regulator_enable(lt9611uxc);
896 lt9611uxc_reset(lt9611uxc);
898 ret = lt9611uxc_read_device_rev(lt9611uxc);
900 dev_err(dev, "failed to read chip rev\n");
901 goto err_disable_regulators;
905 ret = lt9611uxc_read_version(lt9611uxc);
907 dev_err(dev, "failed to read FW version\n");
908 goto err_disable_regulators;
909 } else if (ret == 0) {
912 dev_err(dev, "FW version 0, enforcing firmware update\n");
913 ret = lt9611uxc_firmware_update(lt9611uxc);
915 goto err_disable_regulators;
919 dev_err(dev, "FW version 0, update failed\n");
921 goto err_disable_regulators;
923 } else if (ret < 0x40) {
924 dev_info(dev, "FW version 0x%x, HPD not supported\n", ret);
926 lt9611uxc->hpd_supported = true;
928 lt9611uxc->fw_version = ret;
930 init_waitqueue_head(<9611uxc->wq);
931 INIT_WORK(<9611uxc->work, lt9611uxc_hpd_work);
933 ret = request_threaded_irq(client->irq, NULL,
934 lt9611uxc_irq_thread_handler,
935 IRQF_ONESHOT, "lt9611uxc", lt9611uxc);
937 dev_err(dev, "failed to request irq\n");
938 goto err_disable_regulators;
941 i2c_set_clientdata(client, lt9611uxc);
943 lt9611uxc->bridge.funcs = <9611uxc_bridge_funcs;
944 lt9611uxc->bridge.of_node = client->dev.of_node;
945 lt9611uxc->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID;
946 if (lt9611uxc->hpd_supported)
947 lt9611uxc->bridge.ops |= DRM_BRIDGE_OP_HPD;
948 lt9611uxc->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
950 drm_bridge_add(<9611uxc->bridge);
952 /* Attach primary DSI */
953 lt9611uxc->dsi0 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi0_node);
954 if (IS_ERR(lt9611uxc->dsi0)) {
955 ret = PTR_ERR(lt9611uxc->dsi0);
956 goto err_remove_bridge;
959 /* Attach secondary DSI, if specified */
960 if (lt9611uxc->dsi1_node) {
961 lt9611uxc->dsi1 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi1_node);
962 if (IS_ERR(lt9611uxc->dsi1)) {
963 ret = PTR_ERR(lt9611uxc->dsi1);
964 goto err_remove_bridge;
968 return lt9611uxc_audio_init(dev, lt9611uxc);
971 free_irq(client->irq, lt9611uxc);
972 cancel_work_sync(<9611uxc->work);
973 drm_bridge_remove(<9611uxc->bridge);
975 err_disable_regulators:
976 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
979 of_node_put(lt9611uxc->dsi1_node);
980 of_node_put(lt9611uxc->dsi0_node);
985 static void lt9611uxc_remove(struct i2c_client *client)
987 struct lt9611uxc *lt9611uxc = i2c_get_clientdata(client);
989 free_irq(client->irq, lt9611uxc);
990 cancel_work_sync(<9611uxc->work);
991 lt9611uxc_audio_exit(lt9611uxc);
992 drm_bridge_remove(<9611uxc->bridge);
994 mutex_destroy(<9611uxc->ocm_lock);
996 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
998 of_node_put(lt9611uxc->dsi1_node);
999 of_node_put(lt9611uxc->dsi0_node);
1002 static struct i2c_device_id lt9611uxc_id[] = {
1003 { "lontium,lt9611uxc", 0 },
1007 static const struct of_device_id lt9611uxc_match_table[] = {
1008 { .compatible = "lontium,lt9611uxc" },
1011 MODULE_DEVICE_TABLE(of, lt9611uxc_match_table);
1013 static struct i2c_driver lt9611uxc_driver = {
1015 .name = "lt9611uxc",
1016 .of_match_table = lt9611uxc_match_table,
1017 .dev_groups = lt9611uxc_attr_groups,
1019 .probe = lt9611uxc_probe,
1020 .remove = lt9611uxc_remove,
1021 .id_table = lt9611uxc_id,
1023 module_i2c_driver(lt9611uxc_driver);
1025 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");
1026 MODULE_LICENSE("GPL v2");
1028 MODULE_FIRMWARE(FW_FILE);