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