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