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