1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic DSI Command Mode panel driver
4  *
5  * Copyright (C) 2013 Texas Instruments
6  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
7  */
8 
9 /* #define DEBUG */
10 
11 #include <linux/backlight.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/fb.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/interrupt.h>
17 #include <linux/jiffies.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/sched/signal.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
23 #include <linux/of_device.h>
24 
25 #include <video/omapfb_dss.h>
26 #include <video/mipi_display.h>
27 
28 /* DSI Virtual channel. Hardcoded for now. */
29 #define TCH 0
30 
31 #define DCS_READ_NUM_ERRORS	0x05
32 #define DCS_BRIGHTNESS		0x51
33 #define DCS_CTRL_DISPLAY	0x53
34 #define DCS_GET_ID1		0xda
35 #define DCS_GET_ID2		0xdb
36 #define DCS_GET_ID3		0xdc
37 
38 struct panel_drv_data {
39 	struct omap_dss_device dssdev;
40 	struct omap_dss_device *in;
41 
42 	struct omap_video_timings timings;
43 
44 	struct platform_device *pdev;
45 
46 	struct mutex lock;
47 
48 	struct backlight_device *bldev;
49 
50 	unsigned long	hw_guard_end;	/* next value of jiffies when we can
51 					 * issue the next sleep in/out command
52 					 */
53 	unsigned long	hw_guard_wait;	/* max guard time in jiffies */
54 
55 	/* panel HW configuration from DT or platform data */
56 	struct gpio_desc *reset_gpio;
57 	struct gpio_desc *ext_te_gpio;
58 
59 	bool use_dsi_backlight;
60 
61 	struct omap_dsi_pin_config pin_config;
62 
63 	/* runtime variables */
64 	bool enabled;
65 
66 	bool te_enabled;
67 
68 	atomic_t do_update;
69 	int channel;
70 
71 	struct delayed_work te_timeout_work;
72 
73 	bool intro_printed;
74 
75 	bool ulps_enabled;
76 	unsigned ulps_timeout;
77 	struct delayed_work ulps_work;
78 };
79 
80 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
81 
82 static irqreturn_t dsicm_te_isr(int irq, void *data);
83 static void dsicm_te_timeout_work_callback(struct work_struct *work);
84 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable);
85 
86 static int dsicm_panel_reset(struct panel_drv_data *ddata);
87 
88 static void dsicm_ulps_work(struct work_struct *work);
89 
90 static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
91 {
92 	ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
93 	ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
94 }
95 
96 static void hw_guard_wait(struct panel_drv_data *ddata)
97 {
98 	unsigned long wait = ddata->hw_guard_end - jiffies;
99 
100 	if ((long)wait > 0 && time_before_eq(wait, ddata->hw_guard_wait)) {
101 		set_current_state(TASK_UNINTERRUPTIBLE);
102 		schedule_timeout(wait);
103 	}
104 }
105 
106 static int dsicm_dcs_read_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 *data)
107 {
108 	struct omap_dss_device *in = ddata->in;
109 	int r;
110 	u8 buf[1];
111 
112 	r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd, buf, 1);
113 
114 	if (r < 0)
115 		return r;
116 
117 	*data = buf[0];
118 
119 	return 0;
120 }
121 
122 static int dsicm_dcs_write_0(struct panel_drv_data *ddata, u8 dcs_cmd)
123 {
124 	struct omap_dss_device *in = ddata->in;
125 	return in->ops.dsi->dcs_write(in, ddata->channel, &dcs_cmd, 1);
126 }
127 
128 static int dsicm_dcs_write_1(struct panel_drv_data *ddata, u8 dcs_cmd, u8 param)
129 {
130 	struct omap_dss_device *in = ddata->in;
131 	u8 buf[2] = { dcs_cmd, param };
132 
133 	return in->ops.dsi->dcs_write(in, ddata->channel, buf, 2);
134 }
135 
136 static int dsicm_sleep_in(struct panel_drv_data *ddata)
137 
138 {
139 	struct omap_dss_device *in = ddata->in;
140 	u8 cmd;
141 	int r;
142 
143 	hw_guard_wait(ddata);
144 
145 	cmd = MIPI_DCS_ENTER_SLEEP_MODE;
146 	r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, &cmd, 1);
147 	if (r)
148 		return r;
149 
150 	hw_guard_start(ddata, 120);
151 
152 	usleep_range(5000, 10000);
153 
154 	return 0;
155 }
156 
157 static int dsicm_sleep_out(struct panel_drv_data *ddata)
158 {
159 	int r;
160 
161 	hw_guard_wait(ddata);
162 
163 	r = dsicm_dcs_write_0(ddata, MIPI_DCS_EXIT_SLEEP_MODE);
164 	if (r)
165 		return r;
166 
167 	hw_guard_start(ddata, 120);
168 
169 	usleep_range(5000, 10000);
170 
171 	return 0;
172 }
173 
174 static int dsicm_get_id(struct panel_drv_data *ddata, u8 *id1, u8 *id2, u8 *id3)
175 {
176 	int r;
177 
178 	r = dsicm_dcs_read_1(ddata, DCS_GET_ID1, id1);
179 	if (r)
180 		return r;
181 	r = dsicm_dcs_read_1(ddata, DCS_GET_ID2, id2);
182 	if (r)
183 		return r;
184 	r = dsicm_dcs_read_1(ddata, DCS_GET_ID3, id3);
185 	if (r)
186 		return r;
187 
188 	return 0;
189 }
190 
191 static int dsicm_set_update_window(struct panel_drv_data *ddata,
192 		u16 x, u16 y, u16 w, u16 h)
193 {
194 	struct omap_dss_device *in = ddata->in;
195 	int r;
196 	u16 x1 = x;
197 	u16 x2 = x + w - 1;
198 	u16 y1 = y;
199 	u16 y2 = y + h - 1;
200 
201 	u8 buf[5];
202 	buf[0] = MIPI_DCS_SET_COLUMN_ADDRESS;
203 	buf[1] = (x1 >> 8) & 0xff;
204 	buf[2] = (x1 >> 0) & 0xff;
205 	buf[3] = (x2 >> 8) & 0xff;
206 	buf[4] = (x2 >> 0) & 0xff;
207 
208 	r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
209 	if (r)
210 		return r;
211 
212 	buf[0] = MIPI_DCS_SET_PAGE_ADDRESS;
213 	buf[1] = (y1 >> 8) & 0xff;
214 	buf[2] = (y1 >> 0) & 0xff;
215 	buf[3] = (y2 >> 8) & 0xff;
216 	buf[4] = (y2 >> 0) & 0xff;
217 
218 	r = in->ops.dsi->dcs_write_nosync(in, ddata->channel, buf, sizeof(buf));
219 	if (r)
220 		return r;
221 
222 	in->ops.dsi->bta_sync(in, ddata->channel);
223 
224 	return r;
225 }
226 
227 static void dsicm_queue_ulps_work(struct panel_drv_data *ddata)
228 {
229 	if (ddata->ulps_timeout > 0)
230 		schedule_delayed_work(&ddata->ulps_work,
231 				msecs_to_jiffies(ddata->ulps_timeout));
232 }
233 
234 static void dsicm_cancel_ulps_work(struct panel_drv_data *ddata)
235 {
236 	cancel_delayed_work(&ddata->ulps_work);
237 }
238 
239 static int dsicm_enter_ulps(struct panel_drv_data *ddata)
240 {
241 	struct omap_dss_device *in = ddata->in;
242 	int r;
243 
244 	if (ddata->ulps_enabled)
245 		return 0;
246 
247 	dsicm_cancel_ulps_work(ddata);
248 
249 	r = _dsicm_enable_te(ddata, false);
250 	if (r)
251 		goto err;
252 
253 	if (ddata->ext_te_gpio)
254 		disable_irq(gpiod_to_irq(ddata->ext_te_gpio));
255 
256 	in->ops.dsi->disable(in, false, true);
257 
258 	ddata->ulps_enabled = true;
259 
260 	return 0;
261 
262 err:
263 	dev_err(&ddata->pdev->dev, "enter ULPS failed");
264 	dsicm_panel_reset(ddata);
265 
266 	ddata->ulps_enabled = false;
267 
268 	dsicm_queue_ulps_work(ddata);
269 
270 	return r;
271 }
272 
273 static int dsicm_exit_ulps(struct panel_drv_data *ddata)
274 {
275 	struct omap_dss_device *in = ddata->in;
276 	int r;
277 
278 	if (!ddata->ulps_enabled)
279 		return 0;
280 
281 	r = in->ops.dsi->enable(in);
282 	if (r) {
283 		dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
284 		goto err1;
285 	}
286 
287 	in->ops.dsi->enable_hs(in, ddata->channel, true);
288 
289 	r = _dsicm_enable_te(ddata, true);
290 	if (r) {
291 		dev_err(&ddata->pdev->dev, "failed to re-enable TE");
292 		goto err2;
293 	}
294 
295 	if (ddata->ext_te_gpio)
296 		enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
297 
298 	dsicm_queue_ulps_work(ddata);
299 
300 	ddata->ulps_enabled = false;
301 
302 	return 0;
303 
304 err2:
305 	dev_err(&ddata->pdev->dev, "failed to exit ULPS");
306 
307 	r = dsicm_panel_reset(ddata);
308 	if (!r) {
309 		if (ddata->ext_te_gpio)
310 			enable_irq(gpiod_to_irq(ddata->ext_te_gpio));
311 		ddata->ulps_enabled = false;
312 	}
313 err1:
314 	dsicm_queue_ulps_work(ddata);
315 
316 	return r;
317 }
318 
319 static int dsicm_wake_up(struct panel_drv_data *ddata)
320 {
321 	if (ddata->ulps_enabled)
322 		return dsicm_exit_ulps(ddata);
323 
324 	dsicm_cancel_ulps_work(ddata);
325 	dsicm_queue_ulps_work(ddata);
326 	return 0;
327 }
328 
329 static int dsicm_bl_update_status(struct backlight_device *dev)
330 {
331 	struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
332 	struct omap_dss_device *in = ddata->in;
333 	int r;
334 	int level;
335 
336 	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
337 			dev->props.power == FB_BLANK_UNBLANK)
338 		level = dev->props.brightness;
339 	else
340 		level = 0;
341 
342 	dev_dbg(&ddata->pdev->dev, "update brightness to %d\n", level);
343 
344 	mutex_lock(&ddata->lock);
345 
346 	if (ddata->enabled) {
347 		in->ops.dsi->bus_lock(in);
348 
349 		r = dsicm_wake_up(ddata);
350 		if (!r)
351 			r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, level);
352 
353 		in->ops.dsi->bus_unlock(in);
354 	} else {
355 		r = 0;
356 	}
357 
358 	mutex_unlock(&ddata->lock);
359 
360 	return r;
361 }
362 
363 static int dsicm_bl_get_intensity(struct backlight_device *dev)
364 {
365 	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
366 			dev->props.power == FB_BLANK_UNBLANK)
367 		return dev->props.brightness;
368 
369 	return 0;
370 }
371 
372 static const struct backlight_ops dsicm_bl_ops = {
373 	.get_brightness = dsicm_bl_get_intensity,
374 	.update_status  = dsicm_bl_update_status,
375 };
376 
377 static void dsicm_get_resolution(struct omap_dss_device *dssdev,
378 		u16 *xres, u16 *yres)
379 {
380 	*xres = dssdev->panel.timings.x_res;
381 	*yres = dssdev->panel.timings.y_res;
382 }
383 
384 static ssize_t dsicm_num_errors_show(struct device *dev,
385 		struct device_attribute *attr, char *buf)
386 {
387 	struct panel_drv_data *ddata = dev_get_drvdata(dev);
388 	struct omap_dss_device *in = ddata->in;
389 	u8 errors = 0;
390 	int r;
391 
392 	mutex_lock(&ddata->lock);
393 
394 	if (ddata->enabled) {
395 		in->ops.dsi->bus_lock(in);
396 
397 		r = dsicm_wake_up(ddata);
398 		if (!r)
399 			r = dsicm_dcs_read_1(ddata, DCS_READ_NUM_ERRORS,
400 					&errors);
401 
402 		in->ops.dsi->bus_unlock(in);
403 	} else {
404 		r = -ENODEV;
405 	}
406 
407 	mutex_unlock(&ddata->lock);
408 
409 	if (r)
410 		return r;
411 
412 	return sysfs_emit(buf, "%d\n", errors);
413 }
414 
415 static ssize_t dsicm_hw_revision_show(struct device *dev,
416 		struct device_attribute *attr, char *buf)
417 {
418 	struct panel_drv_data *ddata = dev_get_drvdata(dev);
419 	struct omap_dss_device *in = ddata->in;
420 	u8 id1, id2, id3;
421 	int r;
422 
423 	mutex_lock(&ddata->lock);
424 
425 	if (ddata->enabled) {
426 		in->ops.dsi->bus_lock(in);
427 
428 		r = dsicm_wake_up(ddata);
429 		if (!r)
430 			r = dsicm_get_id(ddata, &id1, &id2, &id3);
431 
432 		in->ops.dsi->bus_unlock(in);
433 	} else {
434 		r = -ENODEV;
435 	}
436 
437 	mutex_unlock(&ddata->lock);
438 
439 	if (r)
440 		return r;
441 
442 	return sysfs_emit(buf, "%02x.%02x.%02x\n", id1, id2, id3);
443 }
444 
445 static ssize_t dsicm_store_ulps(struct device *dev,
446 		struct device_attribute *attr,
447 		const char *buf, size_t count)
448 {
449 	struct panel_drv_data *ddata = dev_get_drvdata(dev);
450 	struct omap_dss_device *in = ddata->in;
451 	unsigned long t;
452 	int r;
453 
454 	r = kstrtoul(buf, 0, &t);
455 	if (r)
456 		return r;
457 
458 	mutex_lock(&ddata->lock);
459 
460 	if (ddata->enabled) {
461 		in->ops.dsi->bus_lock(in);
462 
463 		if (t)
464 			r = dsicm_enter_ulps(ddata);
465 		else
466 			r = dsicm_wake_up(ddata);
467 
468 		in->ops.dsi->bus_unlock(in);
469 	}
470 
471 	mutex_unlock(&ddata->lock);
472 
473 	if (r)
474 		return r;
475 
476 	return count;
477 }
478 
479 static ssize_t dsicm_show_ulps(struct device *dev,
480 		struct device_attribute *attr,
481 		char *buf)
482 {
483 	struct panel_drv_data *ddata = dev_get_drvdata(dev);
484 	unsigned t;
485 
486 	mutex_lock(&ddata->lock);
487 	t = ddata->ulps_enabled;
488 	mutex_unlock(&ddata->lock);
489 
490 	return sysfs_emit(buf, "%u\n", t);
491 }
492 
493 static ssize_t dsicm_store_ulps_timeout(struct device *dev,
494 		struct device_attribute *attr,
495 		const char *buf, size_t count)
496 {
497 	struct panel_drv_data *ddata = dev_get_drvdata(dev);
498 	struct omap_dss_device *in = ddata->in;
499 	unsigned long t;
500 	int r;
501 
502 	r = kstrtoul(buf, 0, &t);
503 	if (r)
504 		return r;
505 
506 	mutex_lock(&ddata->lock);
507 	ddata->ulps_timeout = t;
508 
509 	if (ddata->enabled) {
510 		/* dsicm_wake_up will restart the timer */
511 		in->ops.dsi->bus_lock(in);
512 		r = dsicm_wake_up(ddata);
513 		in->ops.dsi->bus_unlock(in);
514 	}
515 
516 	mutex_unlock(&ddata->lock);
517 
518 	if (r)
519 		return r;
520 
521 	return count;
522 }
523 
524 static ssize_t dsicm_show_ulps_timeout(struct device *dev,
525 		struct device_attribute *attr,
526 		char *buf)
527 {
528 	struct panel_drv_data *ddata = dev_get_drvdata(dev);
529 	unsigned t;
530 
531 	mutex_lock(&ddata->lock);
532 	t = ddata->ulps_timeout;
533 	mutex_unlock(&ddata->lock);
534 
535 	return sysfs_emit(buf, "%u\n", t);
536 }
537 
538 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, dsicm_num_errors_show, NULL);
539 static DEVICE_ATTR(hw_revision, S_IRUGO, dsicm_hw_revision_show, NULL);
540 static DEVICE_ATTR(ulps, S_IRUGO | S_IWUSR,
541 		dsicm_show_ulps, dsicm_store_ulps);
542 static DEVICE_ATTR(ulps_timeout, S_IRUGO | S_IWUSR,
543 		dsicm_show_ulps_timeout, dsicm_store_ulps_timeout);
544 
545 static struct attribute *dsicm_attrs[] = {
546 	&dev_attr_num_dsi_errors.attr,
547 	&dev_attr_hw_revision.attr,
548 	&dev_attr_ulps.attr,
549 	&dev_attr_ulps_timeout.attr,
550 	NULL,
551 };
552 
553 static const struct attribute_group dsicm_attr_group = {
554 	.attrs = dsicm_attrs,
555 };
556 
557 static void dsicm_hw_reset(struct panel_drv_data *ddata)
558 {
559 	/*
560 	 * Note that we appear to activate the reset line here. However
561 	 * existing DTSes specified incorrect polarity for it (active high),
562 	 * so in fact this deasserts the reset line.
563 	 */
564 	gpiod_set_value_cansleep(ddata->reset_gpio, 1);
565 	udelay(10);
566 	/* reset the panel */
567 	gpiod_set_value_cansleep(ddata->reset_gpio, 0);
568 	/* keep reset asserted */
569 	udelay(10);
570 	/* release reset line */
571 	gpiod_set_value_cansleep(ddata->reset_gpio, 1);
572 	/* wait after releasing reset */
573 	usleep_range(5000, 10000);
574 }
575 
576 static int dsicm_power_on(struct panel_drv_data *ddata)
577 {
578 	struct omap_dss_device *in = ddata->in;
579 	u8 id1, id2, id3;
580 	int r;
581 	struct omap_dss_dsi_config dsi_config = {
582 		.mode = OMAP_DSS_DSI_CMD_MODE,
583 		.pixel_format = OMAP_DSS_DSI_FMT_RGB888,
584 		.timings = &ddata->timings,
585 		.hs_clk_min = 150000000,
586 		.hs_clk_max = 300000000,
587 		.lp_clk_min = 7000000,
588 		.lp_clk_max = 10000000,
589 	};
590 
591 	if (ddata->pin_config.num_pins > 0) {
592 		r = in->ops.dsi->configure_pins(in, &ddata->pin_config);
593 		if (r) {
594 			dev_err(&ddata->pdev->dev,
595 				"failed to configure DSI pins\n");
596 			goto err0;
597 		}
598 	}
599 
600 	r = in->ops.dsi->set_config(in, &dsi_config);
601 	if (r) {
602 		dev_err(&ddata->pdev->dev, "failed to configure DSI\n");
603 		goto err0;
604 	}
605 
606 	r = in->ops.dsi->enable(in);
607 	if (r) {
608 		dev_err(&ddata->pdev->dev, "failed to enable DSI\n");
609 		goto err0;
610 	}
611 
612 	dsicm_hw_reset(ddata);
613 
614 	in->ops.dsi->enable_hs(in, ddata->channel, false);
615 
616 	r = dsicm_sleep_out(ddata);
617 	if (r)
618 		goto err;
619 
620 	r = dsicm_get_id(ddata, &id1, &id2, &id3);
621 	if (r)
622 		goto err;
623 
624 	r = dsicm_dcs_write_1(ddata, DCS_BRIGHTNESS, 0xff);
625 	if (r)
626 		goto err;
627 
628 	r = dsicm_dcs_write_1(ddata, DCS_CTRL_DISPLAY,
629 			(1<<2) | (1<<5));	/* BL | BCTRL */
630 	if (r)
631 		goto err;
632 
633 	r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_PIXEL_FORMAT,
634 		MIPI_DCS_PIXEL_FMT_24BIT);
635 	if (r)
636 		goto err;
637 
638 	r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_ON);
639 	if (r)
640 		goto err;
641 
642 	r = _dsicm_enable_te(ddata, ddata->te_enabled);
643 	if (r)
644 		goto err;
645 
646 	r = in->ops.dsi->enable_video_output(in, ddata->channel);
647 	if (r)
648 		goto err;
649 
650 	ddata->enabled = 1;
651 
652 	if (!ddata->intro_printed) {
653 		dev_info(&ddata->pdev->dev, "panel revision %02x.%02x.%02x\n",
654 			id1, id2, id3);
655 		ddata->intro_printed = true;
656 	}
657 
658 	in->ops.dsi->enable_hs(in, ddata->channel, true);
659 
660 	return 0;
661 err:
662 	dev_err(&ddata->pdev->dev, "error while enabling panel, issuing HW reset\n");
663 
664 	dsicm_hw_reset(ddata);
665 
666 	in->ops.dsi->disable(in, true, false);
667 err0:
668 	return r;
669 }
670 
671 static void dsicm_power_off(struct panel_drv_data *ddata)
672 {
673 	struct omap_dss_device *in = ddata->in;
674 	int r;
675 
676 	in->ops.dsi->disable_video_output(in, ddata->channel);
677 
678 	r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_DISPLAY_OFF);
679 	if (!r)
680 		r = dsicm_sleep_in(ddata);
681 
682 	if (r) {
683 		dev_err(&ddata->pdev->dev,
684 				"error disabling panel, issuing HW reset\n");
685 		dsicm_hw_reset(ddata);
686 	}
687 
688 	in->ops.dsi->disable(in, true, false);
689 
690 	ddata->enabled = 0;
691 }
692 
693 static int dsicm_panel_reset(struct panel_drv_data *ddata)
694 {
695 	dev_err(&ddata->pdev->dev, "performing LCD reset\n");
696 
697 	dsicm_power_off(ddata);
698 	dsicm_hw_reset(ddata);
699 	return dsicm_power_on(ddata);
700 }
701 
702 static int dsicm_connect(struct omap_dss_device *dssdev)
703 {
704 	struct panel_drv_data *ddata = to_panel_data(dssdev);
705 	struct omap_dss_device *in = ddata->in;
706 	struct device *dev = &ddata->pdev->dev;
707 	int r;
708 
709 	if (omapdss_device_is_connected(dssdev))
710 		return 0;
711 
712 	r = in->ops.dsi->connect(in, dssdev);
713 	if (r) {
714 		dev_err(dev, "Failed to connect to video source\n");
715 		return r;
716 	}
717 
718 	r = in->ops.dsi->request_vc(ddata->in, &ddata->channel);
719 	if (r) {
720 		dev_err(dev, "failed to get virtual channel\n");
721 		goto err_req_vc;
722 	}
723 
724 	r = in->ops.dsi->set_vc_id(ddata->in, ddata->channel, TCH);
725 	if (r) {
726 		dev_err(dev, "failed to set VC_ID\n");
727 		goto err_vc_id;
728 	}
729 
730 	return 0;
731 
732 err_vc_id:
733 	in->ops.dsi->release_vc(ddata->in, ddata->channel);
734 err_req_vc:
735 	in->ops.dsi->disconnect(in, dssdev);
736 	return r;
737 }
738 
739 static void dsicm_disconnect(struct omap_dss_device *dssdev)
740 {
741 	struct panel_drv_data *ddata = to_panel_data(dssdev);
742 	struct omap_dss_device *in = ddata->in;
743 
744 	if (!omapdss_device_is_connected(dssdev))
745 		return;
746 
747 	in->ops.dsi->release_vc(in, ddata->channel);
748 	in->ops.dsi->disconnect(in, dssdev);
749 }
750 
751 static int dsicm_enable(struct omap_dss_device *dssdev)
752 {
753 	struct panel_drv_data *ddata = to_panel_data(dssdev);
754 	struct omap_dss_device *in = ddata->in;
755 	int r;
756 
757 	dev_dbg(&ddata->pdev->dev, "enable\n");
758 
759 	mutex_lock(&ddata->lock);
760 
761 	if (!omapdss_device_is_connected(dssdev)) {
762 		r = -ENODEV;
763 		goto err;
764 	}
765 
766 	if (omapdss_device_is_enabled(dssdev)) {
767 		r = 0;
768 		goto err;
769 	}
770 
771 	in->ops.dsi->bus_lock(in);
772 
773 	r = dsicm_power_on(ddata);
774 
775 	in->ops.dsi->bus_unlock(in);
776 
777 	if (r)
778 		goto err;
779 
780 	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
781 
782 	mutex_unlock(&ddata->lock);
783 
784 	return 0;
785 err:
786 	dev_dbg(&ddata->pdev->dev, "enable failed\n");
787 	mutex_unlock(&ddata->lock);
788 	return r;
789 }
790 
791 static void dsicm_disable(struct omap_dss_device *dssdev)
792 {
793 	struct panel_drv_data *ddata = to_panel_data(dssdev);
794 	struct omap_dss_device *in = ddata->in;
795 	int r;
796 
797 	dev_dbg(&ddata->pdev->dev, "disable\n");
798 
799 	mutex_lock(&ddata->lock);
800 
801 	dsicm_cancel_ulps_work(ddata);
802 
803 	in->ops.dsi->bus_lock(in);
804 
805 	if (omapdss_device_is_enabled(dssdev)) {
806 		r = dsicm_wake_up(ddata);
807 		if (!r)
808 			dsicm_power_off(ddata);
809 	}
810 
811 	in->ops.dsi->bus_unlock(in);
812 
813 	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
814 
815 	mutex_unlock(&ddata->lock);
816 }
817 
818 static void dsicm_framedone_cb(int err, void *data)
819 {
820 	struct panel_drv_data *ddata = data;
821 	struct omap_dss_device *in = ddata->in;
822 
823 	dev_dbg(&ddata->pdev->dev, "framedone, err %d\n", err);
824 	in->ops.dsi->bus_unlock(ddata->in);
825 }
826 
827 static irqreturn_t dsicm_te_isr(int irq, void *data)
828 {
829 	struct panel_drv_data *ddata = data;
830 	struct omap_dss_device *in = ddata->in;
831 	int old;
832 	int r;
833 
834 	old = atomic_cmpxchg(&ddata->do_update, 1, 0);
835 
836 	if (old) {
837 		cancel_delayed_work(&ddata->te_timeout_work);
838 
839 		r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
840 				ddata);
841 		if (r)
842 			goto err;
843 	}
844 
845 	return IRQ_HANDLED;
846 err:
847 	dev_err(&ddata->pdev->dev, "start update failed\n");
848 	in->ops.dsi->bus_unlock(in);
849 	return IRQ_HANDLED;
850 }
851 
852 static void dsicm_te_timeout_work_callback(struct work_struct *work)
853 {
854 	struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
855 					te_timeout_work.work);
856 	struct omap_dss_device *in = ddata->in;
857 
858 	dev_err(&ddata->pdev->dev, "TE not received for 250ms!\n");
859 
860 	atomic_set(&ddata->do_update, 0);
861 	in->ops.dsi->bus_unlock(in);
862 }
863 
864 static int dsicm_update(struct omap_dss_device *dssdev,
865 				    u16 x, u16 y, u16 w, u16 h)
866 {
867 	struct panel_drv_data *ddata = to_panel_data(dssdev);
868 	struct omap_dss_device *in = ddata->in;
869 	int r;
870 
871 	dev_dbg(&ddata->pdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
872 
873 	mutex_lock(&ddata->lock);
874 	in->ops.dsi->bus_lock(in);
875 
876 	r = dsicm_wake_up(ddata);
877 	if (r)
878 		goto err;
879 
880 	if (!ddata->enabled) {
881 		r = 0;
882 		goto err;
883 	}
884 
885 	/* XXX no need to send this every frame, but dsi break if not done */
886 	r = dsicm_set_update_window(ddata, 0, 0,
887 			dssdev->panel.timings.x_res,
888 			dssdev->panel.timings.y_res);
889 	if (r)
890 		goto err;
891 
892 	if (ddata->te_enabled && ddata->ext_te_gpio) {
893 		schedule_delayed_work(&ddata->te_timeout_work,
894 				msecs_to_jiffies(250));
895 		atomic_set(&ddata->do_update, 1);
896 	} else {
897 		r = in->ops.dsi->update(in, ddata->channel, dsicm_framedone_cb,
898 				ddata);
899 		if (r)
900 			goto err;
901 	}
902 
903 	/* note: no bus_unlock here. unlock is in framedone_cb */
904 	mutex_unlock(&ddata->lock);
905 	return 0;
906 err:
907 	in->ops.dsi->bus_unlock(in);
908 	mutex_unlock(&ddata->lock);
909 	return r;
910 }
911 
912 static int dsicm_sync(struct omap_dss_device *dssdev)
913 {
914 	struct panel_drv_data *ddata = to_panel_data(dssdev);
915 	struct omap_dss_device *in = ddata->in;
916 
917 	dev_dbg(&ddata->pdev->dev, "sync\n");
918 
919 	mutex_lock(&ddata->lock);
920 	in->ops.dsi->bus_lock(in);
921 	in->ops.dsi->bus_unlock(in);
922 	mutex_unlock(&ddata->lock);
923 
924 	dev_dbg(&ddata->pdev->dev, "sync done\n");
925 
926 	return 0;
927 }
928 
929 static int _dsicm_enable_te(struct panel_drv_data *ddata, bool enable)
930 {
931 	struct omap_dss_device *in = ddata->in;
932 	int r;
933 
934 	if (enable)
935 		r = dsicm_dcs_write_1(ddata, MIPI_DCS_SET_TEAR_ON, 0);
936 	else
937 		r = dsicm_dcs_write_0(ddata, MIPI_DCS_SET_TEAR_OFF);
938 
939 	if (!ddata->ext_te_gpio)
940 		in->ops.dsi->enable_te(in, enable);
941 
942 	/* possible panel bug */
943 	msleep(100);
944 
945 	return r;
946 }
947 
948 static int dsicm_enable_te(struct omap_dss_device *dssdev, bool enable)
949 {
950 	struct panel_drv_data *ddata = to_panel_data(dssdev);
951 	struct omap_dss_device *in = ddata->in;
952 	int r;
953 
954 	mutex_lock(&ddata->lock);
955 
956 	if (ddata->te_enabled == enable)
957 		goto end;
958 
959 	in->ops.dsi->bus_lock(in);
960 
961 	if (ddata->enabled) {
962 		r = dsicm_wake_up(ddata);
963 		if (r)
964 			goto err;
965 
966 		r = _dsicm_enable_te(ddata, enable);
967 		if (r)
968 			goto err;
969 	}
970 
971 	ddata->te_enabled = enable;
972 
973 	in->ops.dsi->bus_unlock(in);
974 end:
975 	mutex_unlock(&ddata->lock);
976 
977 	return 0;
978 err:
979 	in->ops.dsi->bus_unlock(in);
980 	mutex_unlock(&ddata->lock);
981 
982 	return r;
983 }
984 
985 static int dsicm_get_te(struct omap_dss_device *dssdev)
986 {
987 	struct panel_drv_data *ddata = to_panel_data(dssdev);
988 	int r;
989 
990 	mutex_lock(&ddata->lock);
991 	r = ddata->te_enabled;
992 	mutex_unlock(&ddata->lock);
993 
994 	return r;
995 }
996 
997 static int dsicm_memory_read(struct omap_dss_device *dssdev,
998 		void *buf, size_t size,
999 		u16 x, u16 y, u16 w, u16 h)
1000 {
1001 	struct panel_drv_data *ddata = to_panel_data(dssdev);
1002 	struct omap_dss_device *in = ddata->in;
1003 	int r;
1004 	int first = 1;
1005 	int plen;
1006 	unsigned buf_used = 0;
1007 
1008 	if (size < w * h * 3)
1009 		return -ENOMEM;
1010 
1011 	mutex_lock(&ddata->lock);
1012 
1013 	if (!ddata->enabled) {
1014 		r = -ENODEV;
1015 		goto err1;
1016 	}
1017 
1018 	size = min(w * h * 3,
1019 			dssdev->panel.timings.x_res *
1020 			dssdev->panel.timings.y_res * 3);
1021 
1022 	in->ops.dsi->bus_lock(in);
1023 
1024 	r = dsicm_wake_up(ddata);
1025 	if (r)
1026 		goto err2;
1027 
1028 	/* plen 1 or 2 goes into short packet. until checksum error is fixed,
1029 	 * use short packets. plen 32 works, but bigger packets seem to cause
1030 	 * an error. */
1031 	if (size % 2)
1032 		plen = 1;
1033 	else
1034 		plen = 2;
1035 
1036 	dsicm_set_update_window(ddata, x, y, w, h);
1037 
1038 	r = in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, plen);
1039 	if (r)
1040 		goto err2;
1041 
1042 	while (buf_used < size) {
1043 		u8 dcs_cmd = first ? 0x2e : 0x3e;
1044 		first = 0;
1045 
1046 		r = in->ops.dsi->dcs_read(in, ddata->channel, dcs_cmd,
1047 				buf + buf_used, size - buf_used);
1048 
1049 		if (r < 0) {
1050 			dev_err(dssdev->dev, "read error\n");
1051 			goto err3;
1052 		}
1053 
1054 		buf_used += r;
1055 
1056 		if (r < plen) {
1057 			dev_err(&ddata->pdev->dev, "short read\n");
1058 			break;
1059 		}
1060 
1061 		if (signal_pending(current)) {
1062 			dev_err(&ddata->pdev->dev, "signal pending, "
1063 					"aborting memory read\n");
1064 			r = -ERESTARTSYS;
1065 			goto err3;
1066 		}
1067 	}
1068 
1069 	r = buf_used;
1070 
1071 err3:
1072 	in->ops.dsi->set_max_rx_packet_size(in, ddata->channel, 1);
1073 err2:
1074 	in->ops.dsi->bus_unlock(in);
1075 err1:
1076 	mutex_unlock(&ddata->lock);
1077 	return r;
1078 }
1079 
1080 static void dsicm_ulps_work(struct work_struct *work)
1081 {
1082 	struct panel_drv_data *ddata = container_of(work, struct panel_drv_data,
1083 			ulps_work.work);
1084 	struct omap_dss_device *dssdev = &ddata->dssdev;
1085 	struct omap_dss_device *in = ddata->in;
1086 
1087 	mutex_lock(&ddata->lock);
1088 
1089 	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE || !ddata->enabled) {
1090 		mutex_unlock(&ddata->lock);
1091 		return;
1092 	}
1093 
1094 	in->ops.dsi->bus_lock(in);
1095 
1096 	dsicm_enter_ulps(ddata);
1097 
1098 	in->ops.dsi->bus_unlock(in);
1099 	mutex_unlock(&ddata->lock);
1100 }
1101 
1102 static struct omap_dss_driver dsicm_ops = {
1103 	.connect	= dsicm_connect,
1104 	.disconnect	= dsicm_disconnect,
1105 
1106 	.enable		= dsicm_enable,
1107 	.disable	= dsicm_disable,
1108 
1109 	.update		= dsicm_update,
1110 	.sync		= dsicm_sync,
1111 
1112 	.get_resolution	= dsicm_get_resolution,
1113 	.get_recommended_bpp = omapdss_default_get_recommended_bpp,
1114 
1115 	.enable_te	= dsicm_enable_te,
1116 	.get_te		= dsicm_get_te,
1117 
1118 	.memory_read	= dsicm_memory_read,
1119 };
1120 
1121 static int dsicm_probe(struct platform_device *pdev)
1122 {
1123 	struct backlight_properties props;
1124 	struct panel_drv_data *ddata;
1125 	struct backlight_device *bldev = NULL;
1126 	struct device *dev = &pdev->dev;
1127 	struct omap_dss_device *dssdev;
1128 	int r;
1129 
1130 	dev_dbg(dev, "probe\n");
1131 
1132 	if (!pdev->dev.of_node)
1133 		return -ENODEV;
1134 
1135 	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
1136 	if (!ddata)
1137 		return -ENOMEM;
1138 
1139 	platform_set_drvdata(pdev, ddata);
1140 	ddata->pdev = pdev;
1141 
1142 	ddata->in = omapdss_of_find_source_for_first_ep(pdev->dev.of_node);
1143 	r = PTR_ERR_OR_ZERO(ddata->in);
1144 	if (r) {
1145 		dev_err(&pdev->dev, "failed to find video source: %d\n", r);
1146 		return r;
1147 	}
1148 
1149 	ddata->timings.x_res = 864;
1150 	ddata->timings.y_res = 480;
1151 	ddata->timings.pixelclock = 864 * 480 * 60;
1152 
1153 	dssdev = &ddata->dssdev;
1154 	dssdev->dev = dev;
1155 	dssdev->driver = &dsicm_ops;
1156 	dssdev->panel.timings = ddata->timings;
1157 	dssdev->type = OMAP_DISPLAY_TYPE_DSI;
1158 	dssdev->owner = THIS_MODULE;
1159 
1160 	dssdev->panel.dsi_pix_fmt = OMAP_DSS_DSI_FMT_RGB888;
1161 	dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
1162 		OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
1163 
1164 	r = omapdss_register_display(dssdev);
1165 	if (r) {
1166 		dev_err(dev, "Failed to register panel\n");
1167 		goto err_reg;
1168 	}
1169 
1170 	mutex_init(&ddata->lock);
1171 
1172 	atomic_set(&ddata->do_update, 0);
1173 
1174 	ddata->reset_gpio = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
1175 	r = PTR_ERR_OR_ZERO(ddata->reset_gpio);
1176 	if (r) {
1177 		dev_err(&pdev->dev, "Failed to request reset gpio: %d\n", r);
1178 		return r;
1179 	}
1180 
1181 	gpiod_set_consumer_name(ddata->reset_gpio, "taal rst");
1182 
1183 	ddata->ext_te_gpio = devm_gpiod_get_optional(&pdev->dev, "te",
1184 						     GPIOD_IN);
1185 	r = PTR_ERR_OR_ZERO(ddata->ext_te_gpio);
1186 	if (r) {
1187 		dev_err(&pdev->dev, "Failed to request TE gpio: %d\n", r);
1188 		return r;
1189 	}
1190 
1191 	if (ddata->ext_te_gpio) {
1192 		gpiod_set_consumer_name(ddata->ext_te_gpio, "taal irq");
1193 
1194 		r = devm_request_irq(dev, gpiod_to_irq(ddata->ext_te_gpio),
1195 				dsicm_te_isr,
1196 				IRQF_TRIGGER_RISING,
1197 				"taal vsync", ddata);
1198 
1199 		if (r) {
1200 			dev_err(dev, "IRQ request failed\n");
1201 			return r;
1202 		}
1203 
1204 		INIT_DEFERRABLE_WORK(&ddata->te_timeout_work,
1205 					dsicm_te_timeout_work_callback);
1206 
1207 		dev_dbg(dev, "Using GPIO TE\n");
1208 	}
1209 
1210 	INIT_DELAYED_WORK(&ddata->ulps_work, dsicm_ulps_work);
1211 
1212 	dsicm_hw_reset(ddata);
1213 
1214 	if (ddata->use_dsi_backlight) {
1215 		memset(&props, 0, sizeof(struct backlight_properties));
1216 		props.max_brightness = 255;
1217 
1218 		props.type = BACKLIGHT_RAW;
1219 		bldev = backlight_device_register(dev_name(dev),
1220 				dev, ddata, &dsicm_bl_ops, &props);
1221 		if (IS_ERR(bldev)) {
1222 			r = PTR_ERR(bldev);
1223 			goto err_reg;
1224 		}
1225 
1226 		ddata->bldev = bldev;
1227 
1228 		bldev->props.fb_blank = FB_BLANK_UNBLANK;
1229 		bldev->props.power = FB_BLANK_UNBLANK;
1230 		bldev->props.brightness = 255;
1231 
1232 		dsicm_bl_update_status(bldev);
1233 	}
1234 
1235 	r = sysfs_create_group(&dev->kobj, &dsicm_attr_group);
1236 	if (r) {
1237 		dev_err(dev, "failed to create sysfs files\n");
1238 		goto err_sysfs_create;
1239 	}
1240 
1241 	return 0;
1242 
1243 err_sysfs_create:
1244 	if (bldev != NULL)
1245 		backlight_device_unregister(bldev);
1246 err_reg:
1247 	return r;
1248 }
1249 
1250 static int __exit dsicm_remove(struct platform_device *pdev)
1251 {
1252 	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
1253 	struct omap_dss_device *dssdev = &ddata->dssdev;
1254 	struct backlight_device *bldev;
1255 
1256 	dev_dbg(&pdev->dev, "remove\n");
1257 
1258 	omapdss_unregister_display(dssdev);
1259 
1260 	dsicm_disable(dssdev);
1261 	dsicm_disconnect(dssdev);
1262 
1263 	sysfs_remove_group(&pdev->dev.kobj, &dsicm_attr_group);
1264 
1265 	bldev = ddata->bldev;
1266 	if (bldev != NULL) {
1267 		bldev->props.power = FB_BLANK_POWERDOWN;
1268 		dsicm_bl_update_status(bldev);
1269 		backlight_device_unregister(bldev);
1270 	}
1271 
1272 	omap_dss_put_device(ddata->in);
1273 
1274 	dsicm_cancel_ulps_work(ddata);
1275 
1276 	/* reset, to be sure that the panel is in a valid state */
1277 	dsicm_hw_reset(ddata);
1278 
1279 	return 0;
1280 }
1281 
1282 static const struct of_device_id dsicm_of_match[] = {
1283 	{ .compatible = "omapdss,panel-dsi-cm", },
1284 	{},
1285 };
1286 
1287 MODULE_DEVICE_TABLE(of, dsicm_of_match);
1288 
1289 static struct platform_driver dsicm_driver = {
1290 	.probe = dsicm_probe,
1291 	.remove = __exit_p(dsicm_remove),
1292 	.driver = {
1293 		.name = "panel-dsi-cm",
1294 		.of_match_table = dsicm_of_match,
1295 		.suppress_bind_attrs = true,
1296 	},
1297 };
1298 
1299 module_platform_driver(dsicm_driver);
1300 
1301 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
1302 MODULE_DESCRIPTION("Generic DSI Command Mode Panel Driver");
1303 MODULE_LICENSE("GPL");
1304