Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / bridge / lontium-lt9611uxc.c
blob0c98d27f84ac7da2b4b23b557e7e28c24377e70a
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 */
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>
18 #include <sound/hdmi-codec.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_bridge.h>
22 #include <drm/drm_mipi_dsi.h>
23 #include <drm/drm_print.h>
24 #include <drm/drm_probe_helper.h>
26 #define EDID_BLOCK_SIZE 128
27 #define EDID_NUM_BLOCKS 2
29 struct lt9611uxc {
30 struct device *dev;
31 struct drm_bridge bridge;
32 struct drm_connector connector;
34 struct regmap *regmap;
35 /* Protects all accesses to registers by stopping the on-chip MCU */
36 struct mutex ocm_lock;
38 struct wait_queue_head wq;
40 struct device_node *dsi0_node;
41 struct device_node *dsi1_node;
42 struct mipi_dsi_device *dsi0;
43 struct mipi_dsi_device *dsi1;
44 struct platform_device *audio_pdev;
46 struct gpio_desc *reset_gpio;
47 struct gpio_desc *enable_gpio;
49 struct regulator_bulk_data supplies[2];
51 struct i2c_client *client;
53 bool hpd_supported;
54 bool edid_read;
55 uint8_t fw_version;
58 #define LT9611_PAGE_CONTROL 0xff
60 static const struct regmap_range_cfg lt9611uxc_ranges[] = {
62 .name = "register_range",
63 .range_min = 0,
64 .range_max = 0xd0ff,
65 .selector_reg = LT9611_PAGE_CONTROL,
66 .selector_mask = 0xff,
67 .selector_shift = 0,
68 .window_start = 0,
69 .window_len = 0x100,
73 static const struct regmap_config lt9611uxc_regmap_config = {
74 .reg_bits = 8,
75 .val_bits = 8,
76 .max_register = 0xffff,
77 .ranges = lt9611uxc_ranges,
78 .num_ranges = ARRAY_SIZE(lt9611uxc_ranges),
81 struct lt9611uxc_mode {
82 u16 hdisplay;
83 u16 vdisplay;
84 u8 vrefresh;
88 * This chip supports only a fixed set of modes.
89 * Enumerate them here to check whether the mode is supported.
91 static struct lt9611uxc_mode lt9611uxc_modes[] = {
92 { 1920, 1080, 60 },
93 { 1920, 1080, 30 },
94 { 1920, 1080, 25 },
95 { 1366, 768, 60 },
96 { 1360, 768, 60 },
97 { 1280, 1024, 60 },
98 { 1280, 800, 60 },
99 { 1280, 720, 60 },
100 { 1280, 720, 50 },
101 { 1280, 720, 30 },
102 { 1152, 864, 60 },
103 { 1024, 768, 60 },
104 { 800, 600, 60 },
105 { 720, 576, 50 },
106 { 720, 480, 60 },
107 { 640, 480, 60 },
110 static struct lt9611uxc *bridge_to_lt9611uxc(struct drm_bridge *bridge)
112 return container_of(bridge, struct lt9611uxc, bridge);
115 static struct lt9611uxc *connector_to_lt9611uxc(struct drm_connector *connector)
117 return container_of(connector, struct lt9611uxc, connector);
120 static void lt9611uxc_lock(struct lt9611uxc *lt9611uxc)
122 mutex_lock(&lt9611uxc->ocm_lock);
123 regmap_write(lt9611uxc->regmap, 0x80ee, 0x01);
126 static void lt9611uxc_unlock(struct lt9611uxc *lt9611uxc)
128 regmap_write(lt9611uxc->regmap, 0x80ee, 0x00);
129 msleep(50);
130 mutex_unlock(&lt9611uxc->ocm_lock);
133 static irqreturn_t lt9611uxc_irq_thread_handler(int irq, void *dev_id)
135 struct lt9611uxc *lt9611uxc = dev_id;
136 unsigned int irq_status = 0;
137 unsigned int hpd_status = 0;
139 lt9611uxc_lock(lt9611uxc);
141 regmap_read(lt9611uxc->regmap, 0xb022, &irq_status);
142 regmap_read(lt9611uxc->regmap, 0xb023, &hpd_status);
143 if (irq_status)
144 regmap_write(lt9611uxc->regmap, 0xb022, 0);
146 lt9611uxc_unlock(lt9611uxc);
148 if (irq_status & BIT(0))
149 lt9611uxc->edid_read = !!(hpd_status & BIT(0));
151 if (irq_status & BIT(1)) {
152 if (lt9611uxc->connector.dev)
153 drm_kms_helper_hotplug_event(lt9611uxc->connector.dev);
154 else
155 drm_bridge_hpd_notify(&lt9611uxc->bridge, !!(hpd_status & BIT(1)));
158 return IRQ_HANDLED;
161 static void lt9611uxc_reset(struct lt9611uxc *lt9611uxc)
163 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
164 msleep(20);
166 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 0);
167 msleep(20);
169 gpiod_set_value_cansleep(lt9611uxc->reset_gpio, 1);
170 msleep(300);
173 static void lt9611uxc_assert_5v(struct lt9611uxc *lt9611uxc)
175 if (!lt9611uxc->enable_gpio)
176 return;
178 gpiod_set_value_cansleep(lt9611uxc->enable_gpio, 1);
179 msleep(20);
182 static int lt9611uxc_regulator_init(struct lt9611uxc *lt9611uxc)
184 int ret;
186 lt9611uxc->supplies[0].supply = "vdd";
187 lt9611uxc->supplies[1].supply = "vcc";
189 ret = devm_regulator_bulk_get(lt9611uxc->dev, 2, lt9611uxc->supplies);
190 if (ret < 0)
191 return ret;
193 return regulator_set_load(lt9611uxc->supplies[0].consumer, 200000);
196 static int lt9611uxc_regulator_enable(struct lt9611uxc *lt9611uxc)
198 int ret;
200 ret = regulator_enable(lt9611uxc->supplies[0].consumer);
201 if (ret < 0)
202 return ret;
204 usleep_range(1000, 10000); /* 50000 according to dtsi */
206 ret = regulator_enable(lt9611uxc->supplies[1].consumer);
207 if (ret < 0) {
208 regulator_disable(lt9611uxc->supplies[0].consumer);
209 return ret;
212 return 0;
215 static struct lt9611uxc_mode *lt9611uxc_find_mode(const struct drm_display_mode *mode)
217 int i;
219 for (i = 0; i < ARRAY_SIZE(lt9611uxc_modes); i++) {
220 if (lt9611uxc_modes[i].hdisplay == mode->hdisplay &&
221 lt9611uxc_modes[i].vdisplay == mode->vdisplay &&
222 lt9611uxc_modes[i].vrefresh == drm_mode_vrefresh(mode)) {
223 return &lt9611uxc_modes[i];
227 return NULL;
230 static struct mipi_dsi_device *lt9611uxc_attach_dsi(struct lt9611uxc *lt9611uxc,
231 struct device_node *dsi_node)
233 const struct mipi_dsi_device_info info = { "lt9611uxc", 0, NULL };
234 struct mipi_dsi_device *dsi;
235 struct mipi_dsi_host *host;
236 int ret;
238 host = of_find_mipi_dsi_host_by_node(dsi_node);
239 if (!host) {
240 dev_err(lt9611uxc->dev, "failed to find dsi host\n");
241 return ERR_PTR(-EPROBE_DEFER);
244 dsi = mipi_dsi_device_register_full(host, &info);
245 if (IS_ERR(dsi)) {
246 dev_err(lt9611uxc->dev, "failed to create dsi device\n");
247 return dsi;
250 dsi->lanes = 4;
251 dsi->format = MIPI_DSI_FMT_RGB888;
252 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
253 MIPI_DSI_MODE_VIDEO_HSE;
255 ret = mipi_dsi_attach(dsi);
256 if (ret < 0) {
257 dev_err(lt9611uxc->dev, "failed to attach dsi to host\n");
258 mipi_dsi_device_unregister(dsi);
259 return ERR_PTR(ret);
262 return dsi;
265 static int lt9611uxc_connector_get_modes(struct drm_connector *connector)
267 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
268 unsigned int count;
269 struct edid *edid;
271 edid = lt9611uxc->bridge.funcs->get_edid(&lt9611uxc->bridge, connector);
272 drm_connector_update_edid_property(connector, edid);
273 count = drm_add_edid_modes(connector, edid);
274 kfree(edid);
276 return count;
279 static enum drm_connector_status lt9611uxc_connector_detect(struct drm_connector *connector,
280 bool force)
282 struct lt9611uxc *lt9611uxc = connector_to_lt9611uxc(connector);
284 return lt9611uxc->bridge.funcs->detect(&lt9611uxc->bridge);
287 static enum drm_mode_status lt9611uxc_connector_mode_valid(struct drm_connector *connector,
288 struct drm_display_mode *mode)
290 struct lt9611uxc_mode *lt9611uxc_mode = lt9611uxc_find_mode(mode);
292 return lt9611uxc_mode ? MODE_OK : MODE_BAD;
295 static const struct drm_connector_helper_funcs lt9611uxc_bridge_connector_helper_funcs = {
296 .get_modes = lt9611uxc_connector_get_modes,
297 .mode_valid = lt9611uxc_connector_mode_valid,
300 static const struct drm_connector_funcs lt9611uxc_bridge_connector_funcs = {
301 .fill_modes = drm_helper_probe_single_connector_modes,
302 .detect = lt9611uxc_connector_detect,
303 .destroy = drm_connector_cleanup,
304 .reset = drm_atomic_helper_connector_reset,
305 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
306 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
309 static int lt9611uxc_connector_init(struct drm_bridge *bridge, struct lt9611uxc *lt9611uxc)
311 int ret;
313 if (!bridge->encoder) {
314 DRM_ERROR("Parent encoder object not found");
315 return -ENODEV;
318 drm_connector_helper_add(&lt9611uxc->connector,
319 &lt9611uxc_bridge_connector_helper_funcs);
320 ret = drm_connector_init(bridge->dev, &lt9611uxc->connector,
321 &lt9611uxc_bridge_connector_funcs,
322 DRM_MODE_CONNECTOR_HDMIA);
323 if (ret) {
324 DRM_ERROR("Failed to initialize connector with drm\n");
325 return ret;
328 return drm_connector_attach_encoder(&lt9611uxc->connector, bridge->encoder);
331 static void lt9611uxc_bridge_detach(struct drm_bridge *bridge)
333 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
335 if (lt9611uxc->dsi1) {
336 mipi_dsi_detach(lt9611uxc->dsi1);
337 mipi_dsi_device_unregister(lt9611uxc->dsi1);
340 mipi_dsi_detach(lt9611uxc->dsi0);
341 mipi_dsi_device_unregister(lt9611uxc->dsi0);
344 static int lt9611uxc_bridge_attach(struct drm_bridge *bridge,
345 enum drm_bridge_attach_flags flags)
347 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
348 int ret;
350 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
351 ret = lt9611uxc_connector_init(bridge, lt9611uxc);
352 if (ret < 0)
353 return ret;
356 /* Attach primary DSI */
357 lt9611uxc->dsi0 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi0_node);
358 if (IS_ERR(lt9611uxc->dsi0))
359 return PTR_ERR(lt9611uxc->dsi0);
361 /* Attach secondary DSI, if specified */
362 if (lt9611uxc->dsi1_node) {
363 lt9611uxc->dsi1 = lt9611uxc_attach_dsi(lt9611uxc, lt9611uxc->dsi1_node);
364 if (IS_ERR(lt9611uxc->dsi1)) {
365 ret = PTR_ERR(lt9611uxc->dsi1);
366 goto err_unregister_dsi0;
370 return 0;
372 err_unregister_dsi0:
373 mipi_dsi_detach(lt9611uxc->dsi0);
374 mipi_dsi_device_unregister(lt9611uxc->dsi0);
376 return ret;
379 static enum drm_mode_status
380 lt9611uxc_bridge_mode_valid(struct drm_bridge *bridge,
381 const struct drm_display_info *info,
382 const struct drm_display_mode *mode)
384 struct lt9611uxc_mode *lt9611uxc_mode;
386 lt9611uxc_mode = lt9611uxc_find_mode(mode);
388 return lt9611uxc_mode ? MODE_OK : MODE_BAD;
391 static void lt9611uxc_video_setup(struct lt9611uxc *lt9611uxc,
392 const struct drm_display_mode *mode)
394 u32 h_total, hactive, hsync_len, hfront_porch;
395 u32 v_total, vactive, vsync_len, vfront_porch;
397 h_total = mode->htotal;
398 v_total = mode->vtotal;
400 hactive = mode->hdisplay;
401 hsync_len = mode->hsync_end - mode->hsync_start;
402 hfront_porch = mode->hsync_start - mode->hdisplay;
404 vactive = mode->vdisplay;
405 vsync_len = mode->vsync_end - mode->vsync_start;
406 vfront_porch = mode->vsync_start - mode->vdisplay;
408 regmap_write(lt9611uxc->regmap, 0xd00d, (u8)(v_total / 256));
409 regmap_write(lt9611uxc->regmap, 0xd00e, (u8)(v_total % 256));
411 regmap_write(lt9611uxc->regmap, 0xd00f, (u8)(vactive / 256));
412 regmap_write(lt9611uxc->regmap, 0xd010, (u8)(vactive % 256));
414 regmap_write(lt9611uxc->regmap, 0xd011, (u8)(h_total / 256));
415 regmap_write(lt9611uxc->regmap, 0xd012, (u8)(h_total % 256));
417 regmap_write(lt9611uxc->regmap, 0xd013, (u8)(hactive / 256));
418 regmap_write(lt9611uxc->regmap, 0xd014, (u8)(hactive % 256));
420 regmap_write(lt9611uxc->regmap, 0xd015, (u8)(vsync_len % 256));
422 regmap_update_bits(lt9611uxc->regmap, 0xd016, 0xf, (u8)(hsync_len / 256));
423 regmap_write(lt9611uxc->regmap, 0xd017, (u8)(hsync_len % 256));
425 regmap_update_bits(lt9611uxc->regmap, 0xd018, 0xf, (u8)(vfront_porch / 256));
426 regmap_write(lt9611uxc->regmap, 0xd019, (u8)(vfront_porch % 256));
428 regmap_update_bits(lt9611uxc->regmap, 0xd01a, 0xf, (u8)(hfront_porch / 256));
429 regmap_write(lt9611uxc->regmap, 0xd01b, (u8)(hfront_porch % 256));
432 static void lt9611uxc_bridge_mode_set(struct drm_bridge *bridge,
433 const struct drm_display_mode *mode,
434 const struct drm_display_mode *adj_mode)
436 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
438 lt9611uxc_lock(lt9611uxc);
439 lt9611uxc_video_setup(lt9611uxc, mode);
440 lt9611uxc_unlock(lt9611uxc);
443 static enum drm_connector_status lt9611uxc_bridge_detect(struct drm_bridge *bridge)
445 struct lt9611uxc *lt9611uxc = bridge_to_lt9611uxc(bridge);
446 unsigned int reg_val = 0;
447 int ret;
448 int connected = 1;
450 if (lt9611uxc->hpd_supported) {
451 lt9611uxc_lock(lt9611uxc);
452 ret = regmap_read(lt9611uxc->regmap, 0xb023, &reg_val);
453 lt9611uxc_unlock(lt9611uxc);
455 if (ret)
456 dev_err(lt9611uxc->dev, "failed to read hpd status: %d\n", ret);
457 else
458 connected = reg_val & BIT(1);
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(100));
471 static int lt9611uxc_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
473 struct lt9611uxc *lt9611uxc = data;
474 int ret;
476 if (len > EDID_BLOCK_SIZE)
477 return -EINVAL;
479 if (block >= EDID_NUM_BLOCKS)
480 return -EINVAL;
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);
489 if (ret)
490 dev_err(lt9611uxc->dev, "edid read failed: %d\n", ret);
492 lt9611uxc_unlock(lt9611uxc);
494 return 0;
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);
501 int ret;
503 ret = lt9611uxc_wait_for_edid(lt9611uxc);
504 if (ret < 0) {
505 dev_err(lt9611uxc->dev, "wait for EDID failed: %d\n", ret);
506 return ERR_PTR(ret);
509 return drm_do_get_edid(connector, lt9611uxc_get_edid_block, lt9611uxc);
512 static const struct drm_bridge_funcs lt9611uxc_bridge_funcs = {
513 .attach = lt9611uxc_bridge_attach,
514 .detach = lt9611uxc_bridge_detach,
515 .mode_valid = lt9611uxc_bridge_mode_valid,
516 .mode_set = lt9611uxc_bridge_mode_set,
517 .detect = lt9611uxc_bridge_detect,
518 .get_edid = lt9611uxc_bridge_get_edid,
521 static int lt9611uxc_parse_dt(struct device *dev,
522 struct lt9611uxc *lt9611uxc)
524 lt9611uxc->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
525 if (!lt9611uxc->dsi0_node) {
526 dev_err(lt9611uxc->dev, "failed to get remote node for primary dsi\n");
527 return -ENODEV;
530 lt9611uxc->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
532 return 0;
535 static int lt9611uxc_gpio_init(struct lt9611uxc *lt9611uxc)
537 struct device *dev = lt9611uxc->dev;
539 lt9611uxc->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
540 if (IS_ERR(lt9611uxc->reset_gpio)) {
541 dev_err(dev, "failed to acquire reset gpio\n");
542 return PTR_ERR(lt9611uxc->reset_gpio);
545 lt9611uxc->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
546 if (IS_ERR(lt9611uxc->enable_gpio)) {
547 dev_err(dev, "failed to acquire enable gpio\n");
548 return PTR_ERR(lt9611uxc->enable_gpio);
551 return 0;
554 static int lt9611uxc_read_device_rev(struct lt9611uxc *lt9611uxc)
556 unsigned int rev0, rev1, rev2;
557 int ret;
559 lt9611uxc_lock(lt9611uxc);
561 ret = regmap_read(lt9611uxc->regmap, 0x8100, &rev0);
562 ret |= regmap_read(lt9611uxc->regmap, 0x8101, &rev1);
563 ret |= regmap_read(lt9611uxc->regmap, 0x8102, &rev2);
564 if (ret)
565 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
566 else
567 dev_info(lt9611uxc->dev, "LT9611 revision: 0x%02x.%02x.%02x\n", rev0, rev1, rev2);
569 lt9611uxc_unlock(lt9611uxc);
571 return ret;
574 static int lt9611uxc_read_version(struct lt9611uxc *lt9611uxc)
576 unsigned int rev;
577 int ret;
579 lt9611uxc_lock(lt9611uxc);
581 ret = regmap_read(lt9611uxc->regmap, 0xb021, &rev);
582 if (ret)
583 dev_err(lt9611uxc->dev, "failed to read revision: %d\n", ret);
584 else
585 dev_info(lt9611uxc->dev, "LT9611 version: 0x%02x\n", rev);
587 lt9611uxc_unlock(lt9611uxc);
589 return ret < 0 ? ret : rev;
592 static int lt9611uxc_hdmi_hw_params(struct device *dev, void *data,
593 struct hdmi_codec_daifmt *fmt,
594 struct hdmi_codec_params *hparms)
597 * LT9611UXC will automatically detect rate and sample size, so no need
598 * to setup anything here.
600 return 0;
603 static void lt9611uxc_audio_shutdown(struct device *dev, void *data)
607 static int lt9611uxc_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
608 struct device_node *endpoint)
610 struct of_endpoint of_ep;
611 int ret;
613 ret = of_graph_parse_endpoint(endpoint, &of_ep);
614 if (ret < 0)
615 return ret;
618 * HDMI sound should be located as reg = <2>
619 * Then, it is sound port 0
621 if (of_ep.port == 2)
622 return 0;
624 return -EINVAL;
627 static const struct hdmi_codec_ops lt9611uxc_codec_ops = {
628 .hw_params = lt9611uxc_hdmi_hw_params,
629 .audio_shutdown = lt9611uxc_audio_shutdown,
630 .get_dai_id = lt9611uxc_hdmi_i2s_get_dai_id,
633 static int lt9611uxc_audio_init(struct device *dev, struct lt9611uxc *lt9611uxc)
635 struct hdmi_codec_pdata codec_data = {
636 .ops = &lt9611uxc_codec_ops,
637 .max_i2s_channels = 2,
638 .i2s = 1,
639 .data = lt9611uxc,
642 lt9611uxc->audio_pdev =
643 platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
644 PLATFORM_DEVID_AUTO,
645 &codec_data, sizeof(codec_data));
647 return PTR_ERR_OR_ZERO(lt9611uxc->audio_pdev);
650 static void lt9611uxc_audio_exit(struct lt9611uxc *lt9611uxc)
652 if (lt9611uxc->audio_pdev) {
653 platform_device_unregister(lt9611uxc->audio_pdev);
654 lt9611uxc->audio_pdev = NULL;
658 #define LT9611UXC_FW_PAGE_SIZE 32
659 static void lt9611uxc_firmware_write_page(struct lt9611uxc *lt9611uxc, u16 addr, const u8 *buf)
661 struct reg_sequence seq_write_prepare[] = {
662 REG_SEQ0(0x805a, 0x04),
663 REG_SEQ0(0x805a, 0x00),
665 REG_SEQ0(0x805e, 0xdf),
666 REG_SEQ0(0x805a, 0x20),
667 REG_SEQ0(0x805a, 0x00),
668 REG_SEQ0(0x8058, 0x21),
671 struct reg_sequence seq_write_addr[] = {
672 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
673 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
674 REG_SEQ0(0x805d, addr & 0xff),
675 REG_SEQ0(0x805a, 0x10),
676 REG_SEQ0(0x805a, 0x00),
679 regmap_write(lt9611uxc->regmap, 0x8108, 0xbf);
680 msleep(20);
681 regmap_write(lt9611uxc->regmap, 0x8108, 0xff);
682 msleep(20);
683 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_prepare, ARRAY_SIZE(seq_write_prepare));
684 regmap_noinc_write(lt9611uxc->regmap, 0x8059, buf, LT9611UXC_FW_PAGE_SIZE);
685 regmap_multi_reg_write(lt9611uxc->regmap, seq_write_addr, ARRAY_SIZE(seq_write_addr));
686 msleep(20);
689 static void lt9611uxc_firmware_read_page(struct lt9611uxc *lt9611uxc, u16 addr, char *buf)
691 struct reg_sequence seq_read_page[] = {
692 REG_SEQ0(0x805a, 0xa0),
693 REG_SEQ0(0x805a, 0x80),
694 REG_SEQ0(0x805b, (addr >> 16) & 0xff),
695 REG_SEQ0(0x805c, (addr >> 8) & 0xff),
696 REG_SEQ0(0x805d, addr & 0xff),
697 REG_SEQ0(0x805a, 0x90),
698 REG_SEQ0(0x805a, 0x80),
699 REG_SEQ0(0x8058, 0x21),
702 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_page, ARRAY_SIZE(seq_read_page));
703 regmap_noinc_read(lt9611uxc->regmap, 0x805f, buf, LT9611UXC_FW_PAGE_SIZE);
706 static char *lt9611uxc_firmware_read(struct lt9611uxc *lt9611uxc, size_t size)
708 struct reg_sequence seq_read_setup[] = {
709 REG_SEQ0(0x805a, 0x84),
710 REG_SEQ0(0x805a, 0x80),
713 char *readbuf;
714 u16 offset;
716 readbuf = kzalloc(ALIGN(size, 32), GFP_KERNEL);
717 if (!readbuf)
718 return NULL;
720 regmap_multi_reg_write(lt9611uxc->regmap, seq_read_setup, ARRAY_SIZE(seq_read_setup));
722 for (offset = 0;
723 offset < size;
724 offset += LT9611UXC_FW_PAGE_SIZE)
725 lt9611uxc_firmware_read_page(lt9611uxc, offset, &readbuf[offset]);
727 return readbuf;
730 static int lt9611uxc_firmware_update(struct lt9611uxc *lt9611uxc)
732 int ret;
733 u16 offset;
734 size_t remain;
735 char *readbuf;
736 const struct firmware *fw;
738 struct reg_sequence seq_setup[] = {
739 REG_SEQ0(0x805e, 0xdf),
740 REG_SEQ0(0x8058, 0x00),
741 REG_SEQ0(0x8059, 0x50),
742 REG_SEQ0(0x805a, 0x10),
743 REG_SEQ0(0x805a, 0x00),
747 struct reg_sequence seq_block_erase[] = {
748 REG_SEQ0(0x805a, 0x04),
749 REG_SEQ0(0x805a, 0x00),
750 REG_SEQ0(0x805b, 0x00),
751 REG_SEQ0(0x805c, 0x00),
752 REG_SEQ0(0x805d, 0x00),
753 REG_SEQ0(0x805a, 0x01),
754 REG_SEQ0(0x805a, 0x00),
757 ret = request_firmware(&fw, "lt9611uxc_fw.bin", lt9611uxc->dev);
758 if (ret < 0)
759 return ret;
761 dev_info(lt9611uxc->dev, "Updating firmware\n");
762 lt9611uxc_lock(lt9611uxc);
764 regmap_multi_reg_write(lt9611uxc->regmap, seq_setup, ARRAY_SIZE(seq_setup));
767 * Need erase block 2 timess here. Sometimes, block erase can fail.
768 * This is a workaroud.
770 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
771 msleep(3000);
772 regmap_multi_reg_write(lt9611uxc->regmap, seq_block_erase, ARRAY_SIZE(seq_block_erase));
773 msleep(3000);
775 for (offset = 0, remain = fw->size;
776 remain >= LT9611UXC_FW_PAGE_SIZE;
777 offset += LT9611UXC_FW_PAGE_SIZE, remain -= LT9611UXC_FW_PAGE_SIZE)
778 lt9611uxc_firmware_write_page(lt9611uxc, offset, fw->data + offset);
780 if (remain > 0) {
781 char buf[LT9611UXC_FW_PAGE_SIZE];
783 memset(buf, 0xff, LT9611UXC_FW_PAGE_SIZE);
784 memcpy(buf, fw->data + offset, remain);
785 lt9611uxc_firmware_write_page(lt9611uxc, offset, buf);
787 msleep(20);
789 readbuf = lt9611uxc_firmware_read(lt9611uxc, fw->size);
790 if (!readbuf) {
791 ret = -ENOMEM;
792 goto out;
795 if (!memcmp(readbuf, fw->data, fw->size)) {
796 dev_err(lt9611uxc->dev, "Firmware update failed\n");
797 print_hex_dump(KERN_ERR, "fw: ", DUMP_PREFIX_OFFSET, 16, 1, readbuf, fw->size, false);
798 ret = -EINVAL;
799 } else {
800 dev_info(lt9611uxc->dev, "Firmware updates successfully\n");
801 ret = 0;
803 kfree(readbuf);
805 out:
806 lt9611uxc_unlock(lt9611uxc);
807 lt9611uxc_reset(lt9611uxc);
808 release_firmware(fw);
810 return ret;
813 static ssize_t lt9611uxc_firmware_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
815 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
816 int ret;
818 ret = lt9611uxc_firmware_update(lt9611uxc);
819 if (ret < 0)
820 return ret;
821 return len;
824 static ssize_t lt9611uxc_firmware_show(struct device *dev, struct device_attribute *attr, char *buf)
826 struct lt9611uxc *lt9611uxc = dev_get_drvdata(dev);
828 return snprintf(buf, PAGE_SIZE, "%02x\n", lt9611uxc->fw_version);
831 static DEVICE_ATTR_RW(lt9611uxc_firmware);
833 static struct attribute *lt9611uxc_attrs[] = {
834 &dev_attr_lt9611uxc_firmware.attr,
835 NULL,
838 static const struct attribute_group lt9611uxc_attr_group = {
839 .attrs = lt9611uxc_attrs,
842 static const struct attribute_group *lt9611uxc_attr_groups[] = {
843 &lt9611uxc_attr_group,
844 NULL,
847 static int lt9611uxc_probe(struct i2c_client *client,
848 const struct i2c_device_id *id)
850 struct lt9611uxc *lt9611uxc;
851 struct device *dev = &client->dev;
852 int ret;
853 bool fw_updated = false;
855 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
856 dev_err(dev, "device doesn't support I2C\n");
857 return -ENODEV;
860 lt9611uxc = devm_kzalloc(dev, sizeof(*lt9611uxc), GFP_KERNEL);
861 if (!lt9611uxc)
862 return -ENOMEM;
864 lt9611uxc->dev = &client->dev;
865 lt9611uxc->client = client;
866 mutex_init(&lt9611uxc->ocm_lock);
868 lt9611uxc->regmap = devm_regmap_init_i2c(client, &lt9611uxc_regmap_config);
869 if (IS_ERR(lt9611uxc->regmap)) {
870 dev_err(lt9611uxc->dev, "regmap i2c init failed\n");
871 return PTR_ERR(lt9611uxc->regmap);
874 ret = lt9611uxc_parse_dt(&client->dev, lt9611uxc);
875 if (ret) {
876 dev_err(dev, "failed to parse device tree\n");
877 return ret;
880 ret = lt9611uxc_gpio_init(lt9611uxc);
881 if (ret < 0)
882 goto err_of_put;
884 ret = lt9611uxc_regulator_init(lt9611uxc);
885 if (ret < 0)
886 goto err_of_put;
888 lt9611uxc_assert_5v(lt9611uxc);
890 ret = lt9611uxc_regulator_enable(lt9611uxc);
891 if (ret)
892 goto err_of_put;
894 lt9611uxc_reset(lt9611uxc);
896 ret = lt9611uxc_read_device_rev(lt9611uxc);
897 if (ret) {
898 dev_err(dev, "failed to read chip rev\n");
899 goto err_disable_regulators;
902 retry:
903 ret = lt9611uxc_read_version(lt9611uxc);
904 if (ret < 0) {
905 dev_err(dev, "failed to read FW version\n");
906 goto err_disable_regulators;
907 } else if (ret == 0) {
908 if (!fw_updated) {
909 fw_updated = true;
910 dev_err(dev, "FW version 0, enforcing firmware update\n");
911 ret = lt9611uxc_firmware_update(lt9611uxc);
912 if (ret < 0)
913 goto err_disable_regulators;
914 else
915 goto retry;
916 } else {
917 dev_err(dev, "FW version 0, update failed\n");
918 ret = -EOPNOTSUPP;
919 goto err_disable_regulators;
921 } else if (ret < 0x40) {
922 dev_info(dev, "FW version 0x%x, HPD not supported\n", ret);
923 } else {
924 lt9611uxc->hpd_supported = true;
926 lt9611uxc->fw_version = ret;
928 init_waitqueue_head(&lt9611uxc->wq);
929 ret = devm_request_threaded_irq(dev, client->irq, NULL,
930 lt9611uxc_irq_thread_handler,
931 IRQF_ONESHOT, "lt9611uxc", lt9611uxc);
932 if (ret) {
933 dev_err(dev, "failed to request irq\n");
934 goto err_disable_regulators;
937 i2c_set_clientdata(client, lt9611uxc);
939 lt9611uxc->bridge.funcs = &lt9611uxc_bridge_funcs;
940 lt9611uxc->bridge.of_node = client->dev.of_node;
941 lt9611uxc->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID;
942 if (lt9611uxc->hpd_supported)
943 lt9611uxc->bridge.ops |= DRM_BRIDGE_OP_HPD;
944 lt9611uxc->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
946 drm_bridge_add(&lt9611uxc->bridge);
948 return lt9611uxc_audio_init(dev, lt9611uxc);
950 err_disable_regulators:
951 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
953 err_of_put:
954 of_node_put(lt9611uxc->dsi1_node);
955 of_node_put(lt9611uxc->dsi0_node);
957 return ret;
960 static int lt9611uxc_remove(struct i2c_client *client)
962 struct lt9611uxc *lt9611uxc = i2c_get_clientdata(client);
964 disable_irq(client->irq);
965 lt9611uxc_audio_exit(lt9611uxc);
966 drm_bridge_remove(&lt9611uxc->bridge);
968 mutex_destroy(&lt9611uxc->ocm_lock);
970 regulator_bulk_disable(ARRAY_SIZE(lt9611uxc->supplies), lt9611uxc->supplies);
972 of_node_put(lt9611uxc->dsi1_node);
973 of_node_put(lt9611uxc->dsi0_node);
975 return 0;
978 static struct i2c_device_id lt9611uxc_id[] = {
979 { "lontium,lt9611uxc", 0 },
980 { /* sentinel */ }
983 static const struct of_device_id lt9611uxc_match_table[] = {
984 { .compatible = "lontium,lt9611uxc" },
985 { /* sentinel */ }
987 MODULE_DEVICE_TABLE(of, lt9611uxc_match_table);
989 static struct i2c_driver lt9611uxc_driver = {
990 .driver = {
991 .name = "lt9611uxc",
992 .of_match_table = lt9611uxc_match_table,
993 .dev_groups = lt9611uxc_attr_groups,
995 .probe = lt9611uxc_probe,
996 .remove = lt9611uxc_remove,
997 .id_table = lt9611uxc_id,
999 module_i2c_driver(lt9611uxc_driver);
1001 MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");
1002 MODULE_LICENSE("GPL v2");