1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Sony ACX565AKM LCD Panel driver
4  *
5  * Copyright (C) 2010 Nokia Corporation
6  *
7  * Original Driver Author: Imre Deak <imre.deak@nokia.com>
8  * Based on panel-generic.c by Tomi Valkeinen <tomi.valkeinen@nokia.com>
9  * Adapted to new DSS2 framework: Roger Quadros <roger.quadros@nokia.com>
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/delay.h>
16 #include <linux/spi/spi.h>
17 #include <linux/jiffies.h>
18 #include <linux/sched.h>
19 #include <linux/backlight.h>
20 #include <linux/fb.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/of.h>
23 
24 #include <video/omapfb_dss.h>
25 
26 #define MIPID_CMD_READ_DISP_ID		0x04
27 #define MIPID_CMD_READ_RED		0x06
28 #define MIPID_CMD_READ_GREEN		0x07
29 #define MIPID_CMD_READ_BLUE		0x08
30 #define MIPID_CMD_READ_DISP_STATUS	0x09
31 #define MIPID_CMD_RDDSDR		0x0F
32 #define MIPID_CMD_SLEEP_IN		0x10
33 #define MIPID_CMD_SLEEP_OUT		0x11
34 #define MIPID_CMD_DISP_OFF		0x28
35 #define MIPID_CMD_DISP_ON		0x29
36 #define MIPID_CMD_WRITE_DISP_BRIGHTNESS	0x51
37 #define MIPID_CMD_READ_DISP_BRIGHTNESS	0x52
38 #define MIPID_CMD_WRITE_CTRL_DISP	0x53
39 
40 #define CTRL_DISP_BRIGHTNESS_CTRL_ON	(1 << 5)
41 #define CTRL_DISP_AMBIENT_LIGHT_CTRL_ON	(1 << 4)
42 #define CTRL_DISP_BACKLIGHT_ON		(1 << 2)
43 #define CTRL_DISP_AUTO_BRIGHTNESS_ON	(1 << 1)
44 
45 #define MIPID_CMD_READ_CTRL_DISP	0x54
46 #define MIPID_CMD_WRITE_CABC		0x55
47 #define MIPID_CMD_READ_CABC		0x56
48 
49 #define MIPID_VER_LPH8923		3
50 #define MIPID_VER_LS041Y3		4
51 #define MIPID_VER_L4F00311		8
52 #define MIPID_VER_ACX565AKM		9
53 
54 struct panel_drv_data {
55 	struct omap_dss_device	dssdev;
56 	struct omap_dss_device *in;
57 
58 	struct gpio_desc *reset_gpio;
59 
60 	int datapairs;
61 
62 	struct omap_video_timings videomode;
63 
64 	char		*name;
65 	int		enabled;
66 	int		model;
67 	int		revision;
68 	u8		display_id[3];
69 	unsigned	has_bc:1;
70 	unsigned	has_cabc:1;
71 	unsigned	cabc_mode;
72 	unsigned long	hw_guard_end;		/* next value of jiffies
73 						   when we can issue the
74 						   next sleep in/out command */
75 	unsigned long	hw_guard_wait;		/* max guard time in jiffies */
76 
77 	struct spi_device	*spi;
78 	struct mutex		mutex;
79 
80 	struct backlight_device *bl_dev;
81 };
82 
83 static const struct omap_video_timings acx565akm_panel_timings = {
84 	.x_res		= 800,
85 	.y_res		= 480,
86 	.pixelclock	= 24000000,
87 	.hfp		= 28,
88 	.hsw		= 4,
89 	.hbp		= 24,
90 	.vfp		= 3,
91 	.vsw		= 3,
92 	.vbp		= 4,
93 
94 	.vsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
95 	.hsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
96 
97 	.data_pclk_edge	= OMAPDSS_DRIVE_SIG_RISING_EDGE,
98 	.de_level	= OMAPDSS_SIG_ACTIVE_HIGH,
99 	.sync_pclk_edge	= OMAPDSS_DRIVE_SIG_FALLING_EDGE,
100 };
101 
102 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
103 
104 static void acx565akm_transfer(struct panel_drv_data *ddata, int cmd,
105 			      const u8 *wbuf, int wlen, u8 *rbuf, int rlen)
106 {
107 	struct spi_message	m;
108 	struct spi_transfer	*x, xfer[5];
109 	int			r;
110 
111 	BUG_ON(ddata->spi == NULL);
112 
113 	spi_message_init(&m);
114 
115 	memset(xfer, 0, sizeof(xfer));
116 	x = &xfer[0];
117 
118 	cmd &=  0xff;
119 	x->tx_buf = &cmd;
120 	x->bits_per_word = 9;
121 	x->len = 2;
122 
123 	if (rlen > 1 && wlen == 0) {
124 		/*
125 		 * Between the command and the response data there is a
126 		 * dummy clock cycle. Add an extra bit after the command
127 		 * word to account for this.
128 		 */
129 		x->bits_per_word = 10;
130 		cmd <<= 1;
131 	}
132 	spi_message_add_tail(x, &m);
133 
134 	if (wlen) {
135 		x++;
136 		x->tx_buf = wbuf;
137 		x->len = wlen;
138 		x->bits_per_word = 9;
139 		spi_message_add_tail(x, &m);
140 	}
141 
142 	if (rlen) {
143 		x++;
144 		x->rx_buf	= rbuf;
145 		x->len		= rlen;
146 		spi_message_add_tail(x, &m);
147 	}
148 
149 	r = spi_sync(ddata->spi, &m);
150 	if (r < 0)
151 		dev_dbg(&ddata->spi->dev, "spi_sync %d\n", r);
152 }
153 
154 static inline void acx565akm_cmd(struct panel_drv_data *ddata, int cmd)
155 {
156 	acx565akm_transfer(ddata, cmd, NULL, 0, NULL, 0);
157 }
158 
159 static inline void acx565akm_write(struct panel_drv_data *ddata,
160 			       int reg, const u8 *buf, int len)
161 {
162 	acx565akm_transfer(ddata, reg, buf, len, NULL, 0);
163 }
164 
165 static inline void acx565akm_read(struct panel_drv_data *ddata,
166 			      int reg, u8 *buf, int len)
167 {
168 	acx565akm_transfer(ddata, reg, NULL, 0, buf, len);
169 }
170 
171 static void hw_guard_start(struct panel_drv_data *ddata, int guard_msec)
172 {
173 	ddata->hw_guard_wait = msecs_to_jiffies(guard_msec);
174 	ddata->hw_guard_end = jiffies + ddata->hw_guard_wait;
175 }
176 
177 static void hw_guard_wait(struct panel_drv_data *ddata)
178 {
179 	unsigned long wait = ddata->hw_guard_end - jiffies;
180 
181 	if ((long)wait > 0 && wait <= ddata->hw_guard_wait) {
182 		set_current_state(TASK_UNINTERRUPTIBLE);
183 		schedule_timeout(wait);
184 	}
185 }
186 
187 static void set_sleep_mode(struct panel_drv_data *ddata, int on)
188 {
189 	int cmd;
190 
191 	if (on)
192 		cmd = MIPID_CMD_SLEEP_IN;
193 	else
194 		cmd = MIPID_CMD_SLEEP_OUT;
195 	/*
196 	 * We have to keep 120msec between sleep in/out commands.
197 	 * (8.2.15, 8.2.16).
198 	 */
199 	hw_guard_wait(ddata);
200 	acx565akm_cmd(ddata, cmd);
201 	hw_guard_start(ddata, 120);
202 }
203 
204 static void set_display_state(struct panel_drv_data *ddata, int enabled)
205 {
206 	int cmd = enabled ? MIPID_CMD_DISP_ON : MIPID_CMD_DISP_OFF;
207 
208 	acx565akm_cmd(ddata, cmd);
209 }
210 
211 static int panel_enabled(struct panel_drv_data *ddata)
212 {
213 	u32 disp_status;
214 	int enabled;
215 
216 	acx565akm_read(ddata, MIPID_CMD_READ_DISP_STATUS,
217 			(u8 *)&disp_status, 4);
218 	disp_status = __be32_to_cpu(disp_status);
219 	enabled = (disp_status & (1 << 17)) && (disp_status & (1 << 10));
220 	dev_dbg(&ddata->spi->dev,
221 		"LCD panel %senabled by bootloader (status 0x%04x)\n",
222 		enabled ? "" : "not ", disp_status);
223 	return enabled;
224 }
225 
226 static int panel_detect(struct panel_drv_data *ddata)
227 {
228 	acx565akm_read(ddata, MIPID_CMD_READ_DISP_ID, ddata->display_id, 3);
229 	dev_dbg(&ddata->spi->dev, "MIPI display ID: %02x%02x%02x\n",
230 		ddata->display_id[0],
231 		ddata->display_id[1],
232 		ddata->display_id[2]);
233 
234 	switch (ddata->display_id[0]) {
235 	case 0x10:
236 		ddata->model = MIPID_VER_ACX565AKM;
237 		ddata->name = "acx565akm";
238 		ddata->has_bc = 1;
239 		ddata->has_cabc = 1;
240 		break;
241 	case 0x29:
242 		ddata->model = MIPID_VER_L4F00311;
243 		ddata->name = "l4f00311";
244 		break;
245 	case 0x45:
246 		ddata->model = MIPID_VER_LPH8923;
247 		ddata->name = "lph8923";
248 		break;
249 	case 0x83:
250 		ddata->model = MIPID_VER_LS041Y3;
251 		ddata->name = "ls041y3";
252 		break;
253 	default:
254 		ddata->name = "unknown";
255 		dev_err(&ddata->spi->dev, "invalid display ID\n");
256 		return -ENODEV;
257 	}
258 
259 	ddata->revision = ddata->display_id[1];
260 
261 	dev_info(&ddata->spi->dev, "omapfb: %s rev %02x LCD detected\n",
262 			ddata->name, ddata->revision);
263 
264 	return 0;
265 }
266 
267 /*----------------------Backlight Control-------------------------*/
268 
269 static void enable_backlight_ctrl(struct panel_drv_data *ddata, int enable)
270 {
271 	u16 ctrl;
272 
273 	acx565akm_read(ddata, MIPID_CMD_READ_CTRL_DISP, (u8 *)&ctrl, 1);
274 	if (enable) {
275 		ctrl |= CTRL_DISP_BRIGHTNESS_CTRL_ON |
276 			CTRL_DISP_BACKLIGHT_ON;
277 	} else {
278 		ctrl &= ~(CTRL_DISP_BRIGHTNESS_CTRL_ON |
279 			  CTRL_DISP_BACKLIGHT_ON);
280 	}
281 
282 	ctrl |= 1 << 8;
283 	acx565akm_write(ddata, MIPID_CMD_WRITE_CTRL_DISP, (u8 *)&ctrl, 2);
284 }
285 
286 static void set_cabc_mode(struct panel_drv_data *ddata, unsigned mode)
287 {
288 	u16 cabc_ctrl;
289 
290 	ddata->cabc_mode = mode;
291 	if (!ddata->enabled)
292 		return;
293 	cabc_ctrl = 0;
294 	acx565akm_read(ddata, MIPID_CMD_READ_CABC, (u8 *)&cabc_ctrl, 1);
295 	cabc_ctrl &= ~3;
296 	cabc_ctrl |= (1 << 8) | (mode & 3);
297 	acx565akm_write(ddata, MIPID_CMD_WRITE_CABC, (u8 *)&cabc_ctrl, 2);
298 }
299 
300 static unsigned get_cabc_mode(struct panel_drv_data *ddata)
301 {
302 	return ddata->cabc_mode;
303 }
304 
305 static unsigned get_hw_cabc_mode(struct panel_drv_data *ddata)
306 {
307 	u8 cabc_ctrl;
308 
309 	acx565akm_read(ddata, MIPID_CMD_READ_CABC, &cabc_ctrl, 1);
310 	return cabc_ctrl & 3;
311 }
312 
313 static void acx565akm_set_brightness(struct panel_drv_data *ddata, int level)
314 {
315 	int bv;
316 
317 	bv = level | (1 << 8);
318 	acx565akm_write(ddata, MIPID_CMD_WRITE_DISP_BRIGHTNESS, (u8 *)&bv, 2);
319 
320 	if (level)
321 		enable_backlight_ctrl(ddata, 1);
322 	else
323 		enable_backlight_ctrl(ddata, 0);
324 }
325 
326 static int acx565akm_get_actual_brightness(struct panel_drv_data *ddata)
327 {
328 	u8 bv;
329 
330 	acx565akm_read(ddata, MIPID_CMD_READ_DISP_BRIGHTNESS, &bv, 1);
331 
332 	return bv;
333 }
334 
335 
336 static int acx565akm_bl_update_status(struct backlight_device *dev)
337 {
338 	struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
339 	int level;
340 
341 	dev_dbg(&ddata->spi->dev, "%s\n", __func__);
342 
343 	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
344 			dev->props.power == FB_BLANK_UNBLANK)
345 		level = dev->props.brightness;
346 	else
347 		level = 0;
348 
349 	if (ddata->has_bc)
350 		acx565akm_set_brightness(ddata, level);
351 	else
352 		return -ENODEV;
353 
354 	return 0;
355 }
356 
357 static int acx565akm_bl_get_intensity(struct backlight_device *dev)
358 {
359 	struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
360 
361 	dev_dbg(&dev->dev, "%s\n", __func__);
362 
363 	if (!ddata->has_bc)
364 		return -ENODEV;
365 
366 	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
367 			dev->props.power == FB_BLANK_UNBLANK) {
368 		if (ddata->has_bc)
369 			return acx565akm_get_actual_brightness(ddata);
370 		else
371 			return dev->props.brightness;
372 	}
373 
374 	return 0;
375 }
376 
377 static int acx565akm_bl_update_status_locked(struct backlight_device *dev)
378 {
379 	struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
380 	int r;
381 
382 	mutex_lock(&ddata->mutex);
383 	r = acx565akm_bl_update_status(dev);
384 	mutex_unlock(&ddata->mutex);
385 
386 	return r;
387 }
388 
389 static int acx565akm_bl_get_intensity_locked(struct backlight_device *dev)
390 {
391 	struct panel_drv_data *ddata = dev_get_drvdata(&dev->dev);
392 	int r;
393 
394 	mutex_lock(&ddata->mutex);
395 	r = acx565akm_bl_get_intensity(dev);
396 	mutex_unlock(&ddata->mutex);
397 
398 	return r;
399 }
400 
401 static const struct backlight_ops acx565akm_bl_ops = {
402 	.get_brightness = acx565akm_bl_get_intensity_locked,
403 	.update_status  = acx565akm_bl_update_status_locked,
404 };
405 
406 /*--------------------Auto Brightness control via Sysfs---------------------*/
407 
408 static const char * const cabc_modes[] = {
409 	"off",		/* always used when CABC is not supported */
410 	"ui",
411 	"still-image",
412 	"moving-image",
413 };
414 
415 static ssize_t show_cabc_mode(struct device *dev,
416 		struct device_attribute *attr,
417 		char *buf)
418 {
419 	struct panel_drv_data *ddata = dev_get_drvdata(dev);
420 	const char *mode_str;
421 	int mode;
422 	int len;
423 
424 	if (!ddata->has_cabc)
425 		mode = 0;
426 	else
427 		mode = get_cabc_mode(ddata);
428 	mode_str = "unknown";
429 	if (mode >= 0 && mode < ARRAY_SIZE(cabc_modes))
430 		mode_str = cabc_modes[mode];
431 	len = snprintf(buf, PAGE_SIZE, "%s\n", mode_str);
432 
433 	return len < PAGE_SIZE - 1 ? len : PAGE_SIZE - 1;
434 }
435 
436 static ssize_t store_cabc_mode(struct device *dev,
437 		struct device_attribute *attr,
438 		const char *buf, size_t count)
439 {
440 	struct panel_drv_data *ddata = dev_get_drvdata(dev);
441 	int i;
442 
443 	for (i = 0; i < ARRAY_SIZE(cabc_modes); i++) {
444 		const char *mode_str = cabc_modes[i];
445 		int cmp_len = strlen(mode_str);
446 
447 		if (count > 0 && buf[count - 1] == '\n')
448 			count--;
449 		if (count != cmp_len)
450 			continue;
451 
452 		if (strncmp(buf, mode_str, cmp_len) == 0)
453 			break;
454 	}
455 
456 	if (i == ARRAY_SIZE(cabc_modes))
457 		return -EINVAL;
458 
459 	if (!ddata->has_cabc && i != 0)
460 		return -EINVAL;
461 
462 	mutex_lock(&ddata->mutex);
463 	set_cabc_mode(ddata, i);
464 	mutex_unlock(&ddata->mutex);
465 
466 	return count;
467 }
468 
469 static ssize_t show_cabc_available_modes(struct device *dev,
470 		struct device_attribute *attr,
471 		char *buf)
472 {
473 	struct panel_drv_data *ddata = dev_get_drvdata(dev);
474 	int len;
475 	int i;
476 
477 	if (!ddata->has_cabc)
478 		return sysfs_emit(buf, "%s\n", cabc_modes[0]);
479 
480 	for (i = 0, len = 0;
481 	     len < PAGE_SIZE && i < ARRAY_SIZE(cabc_modes); i++)
482 		len += snprintf(&buf[len], PAGE_SIZE - len, "%s%s%s",
483 			i ? " " : "", cabc_modes[i],
484 			i == ARRAY_SIZE(cabc_modes) - 1 ? "\n" : "");
485 
486 	return len < PAGE_SIZE ? len : PAGE_SIZE - 1;
487 }
488 
489 static DEVICE_ATTR(cabc_mode, S_IRUGO | S_IWUSR,
490 		show_cabc_mode, store_cabc_mode);
491 static DEVICE_ATTR(cabc_available_modes, S_IRUGO,
492 		show_cabc_available_modes, NULL);
493 
494 static struct attribute *bldev_attrs[] = {
495 	&dev_attr_cabc_mode.attr,
496 	&dev_attr_cabc_available_modes.attr,
497 	NULL,
498 };
499 
500 static const struct attribute_group bldev_attr_group = {
501 	.attrs = bldev_attrs,
502 };
503 
504 static int acx565akm_connect(struct omap_dss_device *dssdev)
505 {
506 	struct panel_drv_data *ddata = to_panel_data(dssdev);
507 	struct omap_dss_device *in = ddata->in;
508 
509 	if (omapdss_device_is_connected(dssdev))
510 		return 0;
511 
512 	return in->ops.sdi->connect(in, dssdev);
513 }
514 
515 static void acx565akm_disconnect(struct omap_dss_device *dssdev)
516 {
517 	struct panel_drv_data *ddata = to_panel_data(dssdev);
518 	struct omap_dss_device *in = ddata->in;
519 
520 	if (!omapdss_device_is_connected(dssdev))
521 		return;
522 
523 	in->ops.sdi->disconnect(in, dssdev);
524 }
525 
526 static int acx565akm_panel_power_on(struct omap_dss_device *dssdev)
527 {
528 	struct panel_drv_data *ddata = to_panel_data(dssdev);
529 	struct omap_dss_device *in = ddata->in;
530 	int r;
531 
532 	dev_dbg(&ddata->spi->dev, "%s\n", __func__);
533 
534 	in->ops.sdi->set_timings(in, &ddata->videomode);
535 
536 	if (ddata->datapairs > 0)
537 		in->ops.sdi->set_datapairs(in, ddata->datapairs);
538 
539 	r = in->ops.sdi->enable(in);
540 	if (r) {
541 		pr_err("%s sdi enable failed\n", __func__);
542 		return r;
543 	}
544 
545 	/*FIXME tweak me */
546 	msleep(50);
547 
548 	/*
549 	 * Note that we appear to activate the reset line here. However
550 	 * existing DTSes specified incorrect polarity for it (active high),
551 	 * so in fact this deasserts the reset line.
552 	 */
553 	if (ddata->reset_gpio)
554 		gpiod_set_value_cansleep(ddata->reset_gpio, 1);
555 
556 	if (ddata->enabled) {
557 		dev_dbg(&ddata->spi->dev, "panel already enabled\n");
558 		return 0;
559 	}
560 
561 	/*
562 	 * We have to meet all the following delay requirements:
563 	 * 1. tRW: reset pulse width 10usec (7.12.1)
564 	 * 2. tRT: reset cancel time 5msec (7.12.1)
565 	 * 3. Providing PCLK,HS,VS signals for 2 frames = ~50msec worst
566 	 *    case (7.6.2)
567 	 * 4. 120msec before the sleep out command (7.12.1)
568 	 */
569 	msleep(120);
570 
571 	set_sleep_mode(ddata, 0);
572 	ddata->enabled = 1;
573 
574 	/* 5msec between sleep out and the next command. (8.2.16) */
575 	usleep_range(5000, 10000);
576 	set_display_state(ddata, 1);
577 	set_cabc_mode(ddata, ddata->cabc_mode);
578 
579 	return acx565akm_bl_update_status(ddata->bl_dev);
580 }
581 
582 static void acx565akm_panel_power_off(struct omap_dss_device *dssdev)
583 {
584 	struct panel_drv_data *ddata = to_panel_data(dssdev);
585 	struct omap_dss_device *in = ddata->in;
586 
587 	dev_dbg(dssdev->dev, "%s\n", __func__);
588 
589 	if (!ddata->enabled)
590 		return;
591 
592 	set_display_state(ddata, 0);
593 	set_sleep_mode(ddata, 1);
594 	ddata->enabled = 0;
595 	/*
596 	 * We have to provide PCLK,HS,VS signals for 2 frames (worst case
597 	 * ~50msec) after sending the sleep in command and asserting the
598 	 * reset signal. We probably could assert the reset w/o the delay
599 	 * but we still delay to avoid possible artifacts. (7.6.1)
600 	 */
601 	msleep(50);
602 
603 	/* see comment in acx565akm_panel_power_on() */
604 	if (ddata->reset_gpio)
605 		gpiod_set_value_cansleep(ddata->reset_gpio, 0);
606 
607 	/* FIXME need to tweak this delay */
608 	msleep(100);
609 
610 	in->ops.sdi->disable(in);
611 }
612 
613 static int acx565akm_enable(struct omap_dss_device *dssdev)
614 {
615 	struct panel_drv_data *ddata = to_panel_data(dssdev);
616 	int r;
617 
618 	dev_dbg(dssdev->dev, "%s\n", __func__);
619 
620 	if (!omapdss_device_is_connected(dssdev))
621 		return -ENODEV;
622 
623 	if (omapdss_device_is_enabled(dssdev))
624 		return 0;
625 
626 	mutex_lock(&ddata->mutex);
627 	r = acx565akm_panel_power_on(dssdev);
628 	mutex_unlock(&ddata->mutex);
629 	if (r)
630 		return r;
631 
632 	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
633 
634 	return 0;
635 }
636 
637 static void acx565akm_disable(struct omap_dss_device *dssdev)
638 {
639 	struct panel_drv_data *ddata = to_panel_data(dssdev);
640 
641 	dev_dbg(dssdev->dev, "%s\n", __func__);
642 
643 	if (!omapdss_device_is_enabled(dssdev))
644 		return;
645 
646 	mutex_lock(&ddata->mutex);
647 	acx565akm_panel_power_off(dssdev);
648 	mutex_unlock(&ddata->mutex);
649 
650 	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
651 }
652 
653 static void acx565akm_set_timings(struct omap_dss_device *dssdev,
654 		struct omap_video_timings *timings)
655 {
656 	struct panel_drv_data *ddata = to_panel_data(dssdev);
657 	struct omap_dss_device *in = ddata->in;
658 
659 	ddata->videomode = *timings;
660 	dssdev->panel.timings = *timings;
661 
662 	in->ops.sdi->set_timings(in, timings);
663 }
664 
665 static void acx565akm_get_timings(struct omap_dss_device *dssdev,
666 		struct omap_video_timings *timings)
667 {
668 	struct panel_drv_data *ddata = to_panel_data(dssdev);
669 
670 	*timings = ddata->videomode;
671 }
672 
673 static int acx565akm_check_timings(struct omap_dss_device *dssdev,
674 		struct omap_video_timings *timings)
675 {
676 	struct panel_drv_data *ddata = to_panel_data(dssdev);
677 	struct omap_dss_device *in = ddata->in;
678 
679 	return in->ops.sdi->check_timings(in, timings);
680 }
681 
682 static struct omap_dss_driver acx565akm_ops = {
683 	.connect	= acx565akm_connect,
684 	.disconnect	= acx565akm_disconnect,
685 
686 	.enable		= acx565akm_enable,
687 	.disable	= acx565akm_disable,
688 
689 	.set_timings	= acx565akm_set_timings,
690 	.get_timings	= acx565akm_get_timings,
691 	.check_timings	= acx565akm_check_timings,
692 
693 	.get_resolution	= omapdss_default_get_resolution,
694 };
695 
696 static int acx565akm_probe(struct spi_device *spi)
697 {
698 	struct panel_drv_data *ddata;
699 	struct omap_dss_device *dssdev;
700 	struct backlight_device *bldev;
701 	int max_brightness, brightness;
702 	struct backlight_properties props;
703 	int r;
704 
705 	dev_dbg(&spi->dev, "%s\n", __func__);
706 
707 	if (!spi->dev.of_node)
708 		return -ENODEV;
709 
710 	spi->mode = SPI_MODE_3;
711 
712 	ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
713 	if (ddata == NULL)
714 		return -ENOMEM;
715 
716 	dev_set_drvdata(&spi->dev, ddata);
717 
718 	ddata->spi = spi;
719 
720 	mutex_init(&ddata->mutex);
721 
722 	ddata->in = omapdss_of_find_source_for_first_ep(spi->dev.of_node);
723 	r = PTR_ERR_OR_ZERO(ddata->in);
724 	if (r) {
725 		dev_err(&spi->dev, "failed to find video source\n");
726 		return r;
727 	}
728 
729 	ddata->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset",
730 						    GPIOD_OUT_LOW);
731 	r = PTR_ERR_OR_ZERO(ddata->reset_gpio);
732 	if (r)
733 		goto err_gpio;
734 
735 	if (ddata->reset_gpio) {
736 		gpiod_set_consumer_name(ddata->reset_gpio, "lcd reset");
737 
738 		/* release the reset line */
739 		gpiod_set_value_cansleep(ddata->reset_gpio, 1);
740 	}
741 
742 	/*
743 	 * After reset we have to wait 5 msec before the first
744 	 * command can be sent.
745 	 */
746 	usleep_range(5000, 10000);
747 
748 	ddata->enabled = panel_enabled(ddata);
749 
750 	r = panel_detect(ddata);
751 
752 	if (!ddata->enabled && ddata->reset_gpio)
753 		gpiod_set_value_cansleep(ddata->reset_gpio, 0);
754 
755 	if (r) {
756 		dev_err(&spi->dev, "%s panel detect error\n", __func__);
757 		goto err_detect;
758 	}
759 
760 	memset(&props, 0, sizeof(props));
761 	props.fb_blank = FB_BLANK_UNBLANK;
762 	props.power = FB_BLANK_UNBLANK;
763 	props.type = BACKLIGHT_RAW;
764 
765 	bldev = backlight_device_register("acx565akm", &ddata->spi->dev,
766 			ddata, &acx565akm_bl_ops, &props);
767 	if (IS_ERR(bldev)) {
768 		r = PTR_ERR(bldev);
769 		goto err_reg_bl;
770 	}
771 	ddata->bl_dev = bldev;
772 	if (ddata->has_cabc) {
773 		r = sysfs_create_group(&bldev->dev.kobj, &bldev_attr_group);
774 		if (r) {
775 			dev_err(&bldev->dev,
776 				"%s failed to create sysfs files\n", __func__);
777 			goto err_sysfs;
778 		}
779 		ddata->cabc_mode = get_hw_cabc_mode(ddata);
780 	}
781 
782 	max_brightness = 255;
783 
784 	if (ddata->has_bc)
785 		brightness = acx565akm_get_actual_brightness(ddata);
786 	else
787 		brightness = 0;
788 
789 	bldev->props.max_brightness = max_brightness;
790 	bldev->props.brightness = brightness;
791 
792 	acx565akm_bl_update_status(bldev);
793 
794 
795 	ddata->videomode = acx565akm_panel_timings;
796 
797 	dssdev = &ddata->dssdev;
798 	dssdev->dev = &spi->dev;
799 	dssdev->driver = &acx565akm_ops;
800 	dssdev->type = OMAP_DISPLAY_TYPE_SDI;
801 	dssdev->owner = THIS_MODULE;
802 	dssdev->panel.timings = ddata->videomode;
803 
804 	r = omapdss_register_display(dssdev);
805 	if (r) {
806 		dev_err(&spi->dev, "Failed to register panel\n");
807 		goto err_reg;
808 	}
809 
810 	return 0;
811 
812 err_reg:
813 	sysfs_remove_group(&bldev->dev.kobj, &bldev_attr_group);
814 err_sysfs:
815 	backlight_device_unregister(bldev);
816 err_reg_bl:
817 err_detect:
818 err_gpio:
819 	omap_dss_put_device(ddata->in);
820 	return r;
821 }
822 
823 static void acx565akm_remove(struct spi_device *spi)
824 {
825 	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
826 	struct omap_dss_device *dssdev = &ddata->dssdev;
827 	struct omap_dss_device *in = ddata->in;
828 
829 	dev_dbg(&ddata->spi->dev, "%s\n", __func__);
830 
831 	sysfs_remove_group(&ddata->bl_dev->dev.kobj, &bldev_attr_group);
832 	backlight_device_unregister(ddata->bl_dev);
833 
834 	omapdss_unregister_display(dssdev);
835 
836 	acx565akm_disable(dssdev);
837 	acx565akm_disconnect(dssdev);
838 
839 	omap_dss_put_device(in);
840 }
841 
842 static const struct of_device_id acx565akm_of_match[] = {
843 	{ .compatible = "omapdss,sony,acx565akm", },
844 	{},
845 };
846 MODULE_DEVICE_TABLE(of, acx565akm_of_match);
847 
848 static struct spi_driver acx565akm_driver = {
849 	.driver = {
850 		.name	= "acx565akm",
851 		.of_match_table = acx565akm_of_match,
852 		.suppress_bind_attrs = true,
853 	},
854 	.probe	= acx565akm_probe,
855 	.remove	= acx565akm_remove,
856 };
857 
858 module_spi_driver(acx565akm_driver);
859 
860 MODULE_AUTHOR("Nokia Corporation");
861 MODULE_DESCRIPTION("acx565akm LCD Driver");
862 MODULE_LICENSE("GPL");
863