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