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