1 /*
2  * Taal DSI command mode panel
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Author: Tomi Valkeinen <tomi.valkeinen@nokia.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  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /*#define DEBUG*/
21 
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/jiffies.h>
26 #include <linux/sched.h>
27 #include <linux/backlight.h>
28 #include <linux/fb.h>
29 #include <linux/interrupt.h>
30 #include <linux/gpio.h>
31 #include <linux/workqueue.h>
32 #include <linux/slab.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/mutex.h>
35 
36 #include <plat/display.h>
37 #include <plat/nokia-dsi-panel.h>
38 
39 /* DSI Virtual channel. Hardcoded for now. */
40 #define TCH 0
41 
42 #define DCS_READ_NUM_ERRORS	0x05
43 #define DCS_READ_POWER_MODE	0x0a
44 #define DCS_READ_MADCTL		0x0b
45 #define DCS_READ_PIXEL_FORMAT	0x0c
46 #define DCS_RDDSDR		0x0f
47 #define DCS_SLEEP_IN		0x10
48 #define DCS_SLEEP_OUT		0x11
49 #define DCS_DISPLAY_OFF		0x28
50 #define DCS_DISPLAY_ON		0x29
51 #define DCS_COLUMN_ADDR		0x2a
52 #define DCS_PAGE_ADDR		0x2b
53 #define DCS_MEMORY_WRITE	0x2c
54 #define DCS_TEAR_OFF		0x34
55 #define DCS_TEAR_ON		0x35
56 #define DCS_MEM_ACC_CTRL	0x36
57 #define DCS_PIXEL_FORMAT	0x3a
58 #define DCS_BRIGHTNESS		0x51
59 #define DCS_CTRL_DISPLAY	0x53
60 #define DCS_WRITE_CABC		0x55
61 #define DCS_READ_CABC		0x56
62 #define DCS_GET_ID1		0xda
63 #define DCS_GET_ID2		0xdb
64 #define DCS_GET_ID3		0xdc
65 
66 #define TAAL_ESD_CHECK_PERIOD	msecs_to_jiffies(5000)
67 
68 static irqreturn_t taal_te_isr(int irq, void *data);
69 static void taal_te_timeout_work_callback(struct work_struct *work);
70 static int _taal_enable_te(struct omap_dss_device *dssdev, bool enable);
71 
72 struct panel_regulator {
73 	struct regulator *regulator;
74 	const char *name;
75 	int min_uV;
76 	int max_uV;
77 };
78 
free_regulators(struct panel_regulator * regulators,int n)79 static void free_regulators(struct panel_regulator *regulators, int n)
80 {
81 	int i;
82 
83 	for (i = 0; i < n; i++) {
84 		/* disable/put in reverse order */
85 		regulator_disable(regulators[n - i - 1].regulator);
86 		regulator_put(regulators[n - i - 1].regulator);
87 	}
88 }
89 
init_regulators(struct omap_dss_device * dssdev,struct panel_regulator * regulators,int n)90 static int init_regulators(struct omap_dss_device *dssdev,
91 			struct panel_regulator *regulators, int n)
92 {
93 	int r, i, v;
94 
95 	for (i = 0; i < n; i++) {
96 		struct regulator *reg;
97 
98 		reg = regulator_get(&dssdev->dev, regulators[i].name);
99 		if (IS_ERR(reg)) {
100 			dev_err(&dssdev->dev, "failed to get regulator %s\n",
101 				regulators[i].name);
102 			r = PTR_ERR(reg);
103 			goto err;
104 		}
105 
106 		/* FIXME: better handling of fixed vs. variable regulators */
107 		v = regulator_get_voltage(reg);
108 		if (v < regulators[i].min_uV || v > regulators[i].max_uV) {
109 			r = regulator_set_voltage(reg, regulators[i].min_uV,
110 						regulators[i].max_uV);
111 			if (r) {
112 				dev_err(&dssdev->dev,
113 					"failed to set regulator %s voltage\n",
114 					regulators[i].name);
115 				regulator_put(reg);
116 				goto err;
117 			}
118 		}
119 
120 		r = regulator_enable(reg);
121 		if (r) {
122 			dev_err(&dssdev->dev, "failed to enable regulator %s\n",
123 				regulators[i].name);
124 			regulator_put(reg);
125 			goto err;
126 		}
127 
128 		regulators[i].regulator = reg;
129 	}
130 
131 	return 0;
132 
133 err:
134 	free_regulators(regulators, i);
135 
136 	return r;
137 }
138 
139 /**
140  * struct panel_config - panel configuration
141  * @name: panel name
142  * @type: panel type
143  * @timings: panel resolution
144  * @sleep: various panel specific delays, passed to msleep() if non-zero
145  * @reset_sequence: reset sequence timings, passed to udelay() if non-zero
146  * @regulators: array of panel regulators
147  * @num_regulators: number of regulators in the array
148  */
149 struct panel_config {
150 	const char *name;
151 	int type;
152 
153 	struct omap_video_timings timings;
154 
155 	struct {
156 		unsigned int sleep_in;
157 		unsigned int sleep_out;
158 		unsigned int hw_reset;
159 		unsigned int enable_te;
160 	} sleep;
161 
162 	struct {
163 		unsigned int high;
164 		unsigned int low;
165 	} reset_sequence;
166 
167 	struct panel_regulator *regulators;
168 	int num_regulators;
169 };
170 
171 enum {
172 	PANEL_TAAL,
173 };
174 
175 static struct panel_config panel_configs[] = {
176 	{
177 		.name		= "taal",
178 		.type		= PANEL_TAAL,
179 		.timings	= {
180 			.x_res		= 864,
181 			.y_res		= 480,
182 		},
183 		.sleep		= {
184 			.sleep_in	= 5,
185 			.sleep_out	= 5,
186 			.hw_reset	= 5,
187 			.enable_te	= 100, /* possible panel bug */
188 		},
189 		.reset_sequence	= {
190 			.high		= 10,
191 			.low		= 10,
192 		},
193 	},
194 };
195 
196 struct taal_data {
197 	struct mutex lock;
198 
199 	struct backlight_device *bldev;
200 
201 	unsigned long	hw_guard_end;	/* next value of jiffies when we can
202 					 * issue the next sleep in/out command
203 					 */
204 	unsigned long	hw_guard_wait;	/* max guard time in jiffies */
205 
206 	struct omap_dss_device *dssdev;
207 
208 	bool enabled;
209 	u8 rotate;
210 	bool mirror;
211 
212 	bool te_enabled;
213 
214 	atomic_t do_update;
215 	struct {
216 		u16 x;
217 		u16 y;
218 		u16 w;
219 		u16 h;
220 	} update_region;
221 	int channel;
222 
223 	struct delayed_work te_timeout_work;
224 
225 	bool use_dsi_bl;
226 
227 	bool cabc_broken;
228 	unsigned cabc_mode;
229 
230 	bool intro_printed;
231 
232 	struct workqueue_struct *esd_wq;
233 	struct delayed_work esd_work;
234 
235 	struct panel_config *panel_config;
236 };
237 
238 static inline struct nokia_dsi_panel_data
get_panel_data(const struct omap_dss_device * dssdev)239 *get_panel_data(const struct omap_dss_device *dssdev)
240 {
241 	return (struct nokia_dsi_panel_data *) dssdev->data;
242 }
243 
244 static void taal_esd_work(struct work_struct *work);
245 
hw_guard_start(struct taal_data * td,int guard_msec)246 static void hw_guard_start(struct taal_data *td, int guard_msec)
247 {
248 	td->hw_guard_wait = msecs_to_jiffies(guard_msec);
249 	td->hw_guard_end = jiffies + td->hw_guard_wait;
250 }
251 
hw_guard_wait(struct taal_data * td)252 static void hw_guard_wait(struct taal_data *td)
253 {
254 	unsigned long wait = td->hw_guard_end - jiffies;
255 
256 	if ((long)wait > 0 && wait <= td->hw_guard_wait) {
257 		set_current_state(TASK_UNINTERRUPTIBLE);
258 		schedule_timeout(wait);
259 	}
260 }
261 
taal_dcs_read_1(struct taal_data * td,u8 dcs_cmd,u8 * data)262 static int taal_dcs_read_1(struct taal_data *td, u8 dcs_cmd, u8 *data)
263 {
264 	int r;
265 	u8 buf[1];
266 
267 	r = dsi_vc_dcs_read(td->channel, dcs_cmd, buf, 1);
268 
269 	if (r < 0)
270 		return r;
271 
272 	*data = buf[0];
273 
274 	return 0;
275 }
276 
taal_dcs_write_0(struct taal_data * td,u8 dcs_cmd)277 static int taal_dcs_write_0(struct taal_data *td, u8 dcs_cmd)
278 {
279 	return dsi_vc_dcs_write(td->channel, &dcs_cmd, 1);
280 }
281 
taal_dcs_write_1(struct taal_data * td,u8 dcs_cmd,u8 param)282 static int taal_dcs_write_1(struct taal_data *td, u8 dcs_cmd, u8 param)
283 {
284 	u8 buf[2];
285 	buf[0] = dcs_cmd;
286 	buf[1] = param;
287 	return dsi_vc_dcs_write(td->channel, buf, 2);
288 }
289 
taal_sleep_in(struct taal_data * td)290 static int taal_sleep_in(struct taal_data *td)
291 
292 {
293 	u8 cmd;
294 	int r;
295 
296 	hw_guard_wait(td);
297 
298 	cmd = DCS_SLEEP_IN;
299 	r = dsi_vc_dcs_write_nosync(td->channel, &cmd, 1);
300 	if (r)
301 		return r;
302 
303 	hw_guard_start(td, 120);
304 
305 	if (td->panel_config->sleep.sleep_in)
306 		msleep(td->panel_config->sleep.sleep_in);
307 
308 	return 0;
309 }
310 
taal_sleep_out(struct taal_data * td)311 static int taal_sleep_out(struct taal_data *td)
312 {
313 	int r;
314 
315 	hw_guard_wait(td);
316 
317 	r = taal_dcs_write_0(td, DCS_SLEEP_OUT);
318 	if (r)
319 		return r;
320 
321 	hw_guard_start(td, 120);
322 
323 	if (td->panel_config->sleep.sleep_out)
324 		msleep(td->panel_config->sleep.sleep_out);
325 
326 	return 0;
327 }
328 
taal_get_id(struct taal_data * td,u8 * id1,u8 * id2,u8 * id3)329 static int taal_get_id(struct taal_data *td, u8 *id1, u8 *id2, u8 *id3)
330 {
331 	int r;
332 
333 	r = taal_dcs_read_1(td, DCS_GET_ID1, id1);
334 	if (r)
335 		return r;
336 	r = taal_dcs_read_1(td, DCS_GET_ID2, id2);
337 	if (r)
338 		return r;
339 	r = taal_dcs_read_1(td, DCS_GET_ID3, id3);
340 	if (r)
341 		return r;
342 
343 	return 0;
344 }
345 
taal_set_addr_mode(struct taal_data * td,u8 rotate,bool mirror)346 static int taal_set_addr_mode(struct taal_data *td, u8 rotate, bool mirror)
347 {
348 	int r;
349 	u8 mode;
350 	int b5, b6, b7;
351 
352 	r = taal_dcs_read_1(td, DCS_READ_MADCTL, &mode);
353 	if (r)
354 		return r;
355 
356 	switch (rotate) {
357 	default:
358 	case 0:
359 		b7 = 0;
360 		b6 = 0;
361 		b5 = 0;
362 		break;
363 	case 1:
364 		b7 = 0;
365 		b6 = 1;
366 		b5 = 1;
367 		break;
368 	case 2:
369 		b7 = 1;
370 		b6 = 1;
371 		b5 = 0;
372 		break;
373 	case 3:
374 		b7 = 1;
375 		b6 = 0;
376 		b5 = 1;
377 		break;
378 	}
379 
380 	if (mirror)
381 		b6 = !b6;
382 
383 	mode &= ~((1<<7) | (1<<6) | (1<<5));
384 	mode |= (b7 << 7) | (b6 << 6) | (b5 << 5);
385 
386 	return taal_dcs_write_1(td, DCS_MEM_ACC_CTRL, mode);
387 }
388 
taal_set_update_window(struct taal_data * td,u16 x,u16 y,u16 w,u16 h)389 static int taal_set_update_window(struct taal_data *td,
390 		u16 x, u16 y, u16 w, u16 h)
391 {
392 	int r;
393 	u16 x1 = x;
394 	u16 x2 = x + w - 1;
395 	u16 y1 = y;
396 	u16 y2 = y + h - 1;
397 
398 	u8 buf[5];
399 	buf[0] = DCS_COLUMN_ADDR;
400 	buf[1] = (x1 >> 8) & 0xff;
401 	buf[2] = (x1 >> 0) & 0xff;
402 	buf[3] = (x2 >> 8) & 0xff;
403 	buf[4] = (x2 >> 0) & 0xff;
404 
405 	r = dsi_vc_dcs_write_nosync(td->channel, buf, sizeof(buf));
406 	if (r)
407 		return r;
408 
409 	buf[0] = DCS_PAGE_ADDR;
410 	buf[1] = (y1 >> 8) & 0xff;
411 	buf[2] = (y1 >> 0) & 0xff;
412 	buf[3] = (y2 >> 8) & 0xff;
413 	buf[4] = (y2 >> 0) & 0xff;
414 
415 	r = dsi_vc_dcs_write_nosync(td->channel, buf, sizeof(buf));
416 	if (r)
417 		return r;
418 
419 	dsi_vc_send_bta_sync(td->channel);
420 
421 	return r;
422 }
423 
taal_bl_update_status(struct backlight_device * dev)424 static int taal_bl_update_status(struct backlight_device *dev)
425 {
426 	struct omap_dss_device *dssdev = dev_get_drvdata(&dev->dev);
427 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
428 	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
429 	int r;
430 	int level;
431 
432 	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
433 			dev->props.power == FB_BLANK_UNBLANK)
434 		level = dev->props.brightness;
435 	else
436 		level = 0;
437 
438 	dev_dbg(&dssdev->dev, "update brightness to %d\n", level);
439 
440 	mutex_lock(&td->lock);
441 
442 	if (td->use_dsi_bl) {
443 		if (td->enabled) {
444 			dsi_bus_lock();
445 			r = taal_dcs_write_1(td, DCS_BRIGHTNESS, level);
446 			dsi_bus_unlock();
447 		} else {
448 			r = 0;
449 		}
450 	} else {
451 		if (!panel_data->set_backlight)
452 			r = -EINVAL;
453 		else
454 			r = panel_data->set_backlight(dssdev, level);
455 	}
456 
457 	mutex_unlock(&td->lock);
458 
459 	return r;
460 }
461 
taal_bl_get_intensity(struct backlight_device * dev)462 static int taal_bl_get_intensity(struct backlight_device *dev)
463 {
464 	if (dev->props.fb_blank == FB_BLANK_UNBLANK &&
465 			dev->props.power == FB_BLANK_UNBLANK)
466 		return dev->props.brightness;
467 
468 	return 0;
469 }
470 
471 static const struct backlight_ops taal_bl_ops = {
472 	.get_brightness = taal_bl_get_intensity,
473 	.update_status  = taal_bl_update_status,
474 };
475 
taal_get_timings(struct omap_dss_device * dssdev,struct omap_video_timings * timings)476 static void taal_get_timings(struct omap_dss_device *dssdev,
477 			struct omap_video_timings *timings)
478 {
479 	*timings = dssdev->panel.timings;
480 }
481 
taal_get_resolution(struct omap_dss_device * dssdev,u16 * xres,u16 * yres)482 static void taal_get_resolution(struct omap_dss_device *dssdev,
483 		u16 *xres, u16 *yres)
484 {
485 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
486 
487 	if (td->rotate == 0 || td->rotate == 2) {
488 		*xres = dssdev->panel.timings.x_res;
489 		*yres = dssdev->panel.timings.y_res;
490 	} else {
491 		*yres = dssdev->panel.timings.x_res;
492 		*xres = dssdev->panel.timings.y_res;
493 	}
494 }
495 
taal_num_errors_show(struct device * dev,struct device_attribute * attr,char * buf)496 static ssize_t taal_num_errors_show(struct device *dev,
497 		struct device_attribute *attr, char *buf)
498 {
499 	struct omap_dss_device *dssdev = to_dss_device(dev);
500 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
501 	u8 errors;
502 	int r;
503 
504 	mutex_lock(&td->lock);
505 
506 	if (td->enabled) {
507 		dsi_bus_lock();
508 		r = taal_dcs_read_1(td, DCS_READ_NUM_ERRORS, &errors);
509 		dsi_bus_unlock();
510 	} else {
511 		r = -ENODEV;
512 	}
513 
514 	mutex_unlock(&td->lock);
515 
516 	if (r)
517 		return r;
518 
519 	return snprintf(buf, PAGE_SIZE, "%d\n", errors);
520 }
521 
taal_hw_revision_show(struct device * dev,struct device_attribute * attr,char * buf)522 static ssize_t taal_hw_revision_show(struct device *dev,
523 		struct device_attribute *attr, char *buf)
524 {
525 	struct omap_dss_device *dssdev = to_dss_device(dev);
526 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
527 	u8 id1, id2, id3;
528 	int r;
529 
530 	mutex_lock(&td->lock);
531 
532 	if (td->enabled) {
533 		dsi_bus_lock();
534 		r = taal_get_id(td, &id1, &id2, &id3);
535 		dsi_bus_unlock();
536 	} else {
537 		r = -ENODEV;
538 	}
539 
540 	mutex_unlock(&td->lock);
541 
542 	if (r)
543 		return r;
544 
545 	return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x\n", id1, id2, id3);
546 }
547 
548 static const char *cabc_modes[] = {
549 	"off",		/* used also always when CABC is not supported */
550 	"ui",
551 	"still-image",
552 	"moving-image",
553 };
554 
show_cabc_mode(struct device * dev,struct device_attribute * attr,char * buf)555 static ssize_t show_cabc_mode(struct device *dev,
556 		struct device_attribute *attr,
557 		char *buf)
558 {
559 	struct omap_dss_device *dssdev = to_dss_device(dev);
560 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
561 	const char *mode_str;
562 	int mode;
563 	int len;
564 
565 	mode = td->cabc_mode;
566 
567 	mode_str = "unknown";
568 	if (mode >= 0 && mode < ARRAY_SIZE(cabc_modes))
569 		mode_str = cabc_modes[mode];
570 	len = snprintf(buf, PAGE_SIZE, "%s\n", mode_str);
571 
572 	return len < PAGE_SIZE - 1 ? len : PAGE_SIZE - 1;
573 }
574 
store_cabc_mode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)575 static ssize_t store_cabc_mode(struct device *dev,
576 		struct device_attribute *attr,
577 		const char *buf, size_t count)
578 {
579 	struct omap_dss_device *dssdev = to_dss_device(dev);
580 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
581 	int i;
582 
583 	for (i = 0; i < ARRAY_SIZE(cabc_modes); i++) {
584 		if (sysfs_streq(cabc_modes[i], buf))
585 			break;
586 	}
587 
588 	if (i == ARRAY_SIZE(cabc_modes))
589 		return -EINVAL;
590 
591 	mutex_lock(&td->lock);
592 
593 	if (td->enabled) {
594 		dsi_bus_lock();
595 		if (!td->cabc_broken)
596 			taal_dcs_write_1(td, DCS_WRITE_CABC, i);
597 		dsi_bus_unlock();
598 	}
599 
600 	td->cabc_mode = i;
601 
602 	mutex_unlock(&td->lock);
603 
604 	return count;
605 }
606 
show_cabc_available_modes(struct device * dev,struct device_attribute * attr,char * buf)607 static ssize_t show_cabc_available_modes(struct device *dev,
608 		struct device_attribute *attr,
609 		char *buf)
610 {
611 	int len;
612 	int i;
613 
614 	for (i = 0, len = 0;
615 	     len < PAGE_SIZE && i < ARRAY_SIZE(cabc_modes); i++)
616 		len += snprintf(&buf[len], PAGE_SIZE - len, "%s%s%s",
617 			i ? " " : "", cabc_modes[i],
618 			i == ARRAY_SIZE(cabc_modes) - 1 ? "\n" : "");
619 
620 	return len < PAGE_SIZE ? len : PAGE_SIZE - 1;
621 }
622 
623 static DEVICE_ATTR(num_dsi_errors, S_IRUGO, taal_num_errors_show, NULL);
624 static DEVICE_ATTR(hw_revision, S_IRUGO, taal_hw_revision_show, NULL);
625 static DEVICE_ATTR(cabc_mode, S_IRUGO | S_IWUSR,
626 		show_cabc_mode, store_cabc_mode);
627 static DEVICE_ATTR(cabc_available_modes, S_IRUGO,
628 		show_cabc_available_modes, NULL);
629 
630 static struct attribute *taal_attrs[] = {
631 	&dev_attr_num_dsi_errors.attr,
632 	&dev_attr_hw_revision.attr,
633 	&dev_attr_cabc_mode.attr,
634 	&dev_attr_cabc_available_modes.attr,
635 	NULL,
636 };
637 
638 static struct attribute_group taal_attr_group = {
639 	.attrs = taal_attrs,
640 };
641 
taal_hw_reset(struct omap_dss_device * dssdev)642 static void taal_hw_reset(struct omap_dss_device *dssdev)
643 {
644 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
645 	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
646 
647 	if (panel_data->reset_gpio == -1)
648 		return;
649 
650 	gpio_set_value(panel_data->reset_gpio, 1);
651 	if (td->panel_config->reset_sequence.high)
652 		udelay(td->panel_config->reset_sequence.high);
653 	/* reset the panel */
654 	gpio_set_value(panel_data->reset_gpio, 0);
655 	/* assert reset */
656 	if (td->panel_config->reset_sequence.low)
657 		udelay(td->panel_config->reset_sequence.low);
658 	gpio_set_value(panel_data->reset_gpio, 1);
659 	/* wait after releasing reset */
660 	if (td->panel_config->sleep.hw_reset)
661 		msleep(td->panel_config->sleep.hw_reset);
662 }
663 
taal_probe(struct omap_dss_device * dssdev)664 static int taal_probe(struct omap_dss_device *dssdev)
665 {
666 	struct backlight_properties props;
667 	struct taal_data *td;
668 	struct backlight_device *bldev;
669 	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
670 	struct panel_config *panel_config = NULL;
671 	int r, i;
672 
673 	dev_dbg(&dssdev->dev, "probe\n");
674 
675 	if (!panel_data || !panel_data->name) {
676 		r = -EINVAL;
677 		goto err;
678 	}
679 
680 	for (i = 0; i < ARRAY_SIZE(panel_configs); i++) {
681 		if (strcmp(panel_data->name, panel_configs[i].name) == 0) {
682 			panel_config = &panel_configs[i];
683 			break;
684 		}
685 	}
686 
687 	if (!panel_config) {
688 		r = -EINVAL;
689 		goto err;
690 	}
691 
692 	dssdev->panel.config = OMAP_DSS_LCD_TFT;
693 	dssdev->panel.timings = panel_config->timings;
694 	dssdev->ctrl.pixel_size = 24;
695 
696 	td = kzalloc(sizeof(*td), GFP_KERNEL);
697 	if (!td) {
698 		r = -ENOMEM;
699 		goto err;
700 	}
701 	td->dssdev = dssdev;
702 	td->panel_config = panel_config;
703 
704 	mutex_init(&td->lock);
705 
706 	atomic_set(&td->do_update, 0);
707 
708 	r = init_regulators(dssdev, panel_config->regulators,
709 			panel_config->num_regulators);
710 	if (r)
711 		goto err_reg;
712 
713 	td->esd_wq = create_singlethread_workqueue("taal_esd");
714 	if (td->esd_wq == NULL) {
715 		dev_err(&dssdev->dev, "can't create ESD workqueue\n");
716 		r = -ENOMEM;
717 		goto err_wq;
718 	}
719 	INIT_DELAYED_WORK_DEFERRABLE(&td->esd_work, taal_esd_work);
720 
721 	dev_set_drvdata(&dssdev->dev, td);
722 
723 	taal_hw_reset(dssdev);
724 
725 	/* if no platform set_backlight() defined, presume DSI backlight
726 	 * control */
727 	memset(&props, 0, sizeof(struct backlight_properties));
728 	if (!panel_data->set_backlight)
729 		td->use_dsi_bl = true;
730 
731 	if (td->use_dsi_bl)
732 		props.max_brightness = 255;
733 	else
734 		props.max_brightness = 127;
735 
736 	props.type = BACKLIGHT_RAW;
737 	bldev = backlight_device_register("taal", &dssdev->dev, dssdev,
738 					  &taal_bl_ops, &props);
739 	if (IS_ERR(bldev)) {
740 		r = PTR_ERR(bldev);
741 		goto err_bl;
742 	}
743 
744 	td->bldev = bldev;
745 
746 	bldev->props.fb_blank = FB_BLANK_UNBLANK;
747 	bldev->props.power = FB_BLANK_UNBLANK;
748 	if (td->use_dsi_bl)
749 		bldev->props.brightness = 255;
750 	else
751 		bldev->props.brightness = 127;
752 
753 	taal_bl_update_status(bldev);
754 
755 	if (panel_data->use_ext_te) {
756 		int gpio = panel_data->ext_te_gpio;
757 
758 		r = gpio_request(gpio, "taal irq");
759 		if (r) {
760 			dev_err(&dssdev->dev, "GPIO request failed\n");
761 			goto err_gpio;
762 		}
763 
764 		gpio_direction_input(gpio);
765 
766 		r = request_irq(gpio_to_irq(gpio), taal_te_isr,
767 				IRQF_DISABLED | IRQF_TRIGGER_RISING,
768 				"taal vsync", dssdev);
769 
770 		if (r) {
771 			dev_err(&dssdev->dev, "IRQ request failed\n");
772 			gpio_free(gpio);
773 			goto err_irq;
774 		}
775 
776 		INIT_DELAYED_WORK_DEFERRABLE(&td->te_timeout_work,
777 					taal_te_timeout_work_callback);
778 
779 		dev_dbg(&dssdev->dev, "Using GPIO TE\n");
780 	}
781 
782 	r = omap_dsi_request_vc(dssdev, &td->channel);
783 	if (r) {
784 		dev_err(&dssdev->dev, "failed to get virtual channel\n");
785 		goto err_req_vc;
786 	}
787 
788 	r = omap_dsi_set_vc_id(dssdev, td->channel, TCH);
789 	if (r) {
790 		dev_err(&dssdev->dev, "failed to set VC_ID\n");
791 		goto err_vc_id;
792 	}
793 
794 	r = sysfs_create_group(&dssdev->dev.kobj, &taal_attr_group);
795 	if (r) {
796 		dev_err(&dssdev->dev, "failed to create sysfs files\n");
797 		goto err_vc_id;
798 	}
799 
800 	return 0;
801 
802 err_vc_id:
803 	omap_dsi_release_vc(dssdev, td->channel);
804 err_req_vc:
805 	if (panel_data->use_ext_te)
806 		free_irq(gpio_to_irq(panel_data->ext_te_gpio), dssdev);
807 err_irq:
808 	if (panel_data->use_ext_te)
809 		gpio_free(panel_data->ext_te_gpio);
810 err_gpio:
811 	backlight_device_unregister(bldev);
812 err_bl:
813 	destroy_workqueue(td->esd_wq);
814 err_wq:
815 	free_regulators(panel_config->regulators, panel_config->num_regulators);
816 err_reg:
817 	kfree(td);
818 err:
819 	return r;
820 }
821 
taal_remove(struct omap_dss_device * dssdev)822 static void taal_remove(struct omap_dss_device *dssdev)
823 {
824 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
825 	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
826 	struct backlight_device *bldev;
827 
828 	dev_dbg(&dssdev->dev, "remove\n");
829 
830 	sysfs_remove_group(&dssdev->dev.kobj, &taal_attr_group);
831 	omap_dsi_release_vc(dssdev, td->channel);
832 
833 	if (panel_data->use_ext_te) {
834 		int gpio = panel_data->ext_te_gpio;
835 		free_irq(gpio_to_irq(gpio), dssdev);
836 		gpio_free(gpio);
837 	}
838 
839 	bldev = td->bldev;
840 	bldev->props.power = FB_BLANK_POWERDOWN;
841 	taal_bl_update_status(bldev);
842 	backlight_device_unregister(bldev);
843 
844 	cancel_delayed_work(&td->esd_work);
845 	destroy_workqueue(td->esd_wq);
846 
847 	/* reset, to be sure that the panel is in a valid state */
848 	taal_hw_reset(dssdev);
849 
850 	free_regulators(td->panel_config->regulators,
851 			td->panel_config->num_regulators);
852 
853 	kfree(td);
854 }
855 
taal_power_on(struct omap_dss_device * dssdev)856 static int taal_power_on(struct omap_dss_device *dssdev)
857 {
858 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
859 	u8 id1, id2, id3;
860 	int r;
861 
862 	r = omapdss_dsi_display_enable(dssdev);
863 	if (r) {
864 		dev_err(&dssdev->dev, "failed to enable DSI\n");
865 		goto err0;
866 	}
867 
868 	taal_hw_reset(dssdev);
869 
870 	omapdss_dsi_vc_enable_hs(td->channel, false);
871 
872 	r = taal_sleep_out(td);
873 	if (r)
874 		goto err;
875 
876 	r = taal_get_id(td, &id1, &id2, &id3);
877 	if (r)
878 		goto err;
879 
880 	/* on early Taal revisions CABC is broken */
881 	if (td->panel_config->type == PANEL_TAAL &&
882 		(id2 == 0x00 || id2 == 0xff || id2 == 0x81))
883 		td->cabc_broken = true;
884 
885 	r = taal_dcs_write_1(td, DCS_BRIGHTNESS, 0xff);
886 	if (r)
887 		goto err;
888 
889 	r = taal_dcs_write_1(td, DCS_CTRL_DISPLAY,
890 			(1<<2) | (1<<5));	/* BL | BCTRL */
891 	if (r)
892 		goto err;
893 
894 	r = taal_dcs_write_1(td, DCS_PIXEL_FORMAT, 0x7); /* 24bit/pixel */
895 	if (r)
896 		goto err;
897 
898 	r = taal_set_addr_mode(td, td->rotate, td->mirror);
899 	if (r)
900 		goto err;
901 
902 	if (!td->cabc_broken) {
903 		r = taal_dcs_write_1(td, DCS_WRITE_CABC, td->cabc_mode);
904 		if (r)
905 			goto err;
906 	}
907 
908 	r = taal_dcs_write_0(td, DCS_DISPLAY_ON);
909 	if (r)
910 		goto err;
911 
912 	r = _taal_enable_te(dssdev, td->te_enabled);
913 	if (r)
914 		goto err;
915 
916 	td->enabled = 1;
917 
918 	if (!td->intro_printed) {
919 		dev_info(&dssdev->dev, "%s panel revision %02x.%02x.%02x\n",
920 			td->panel_config->name, id1, id2, id3);
921 		if (td->cabc_broken)
922 			dev_info(&dssdev->dev,
923 					"old Taal version, CABC disabled\n");
924 		td->intro_printed = true;
925 	}
926 
927 	omapdss_dsi_vc_enable_hs(td->channel, true);
928 
929 	return 0;
930 err:
931 	dev_err(&dssdev->dev, "error while enabling panel, issuing HW reset\n");
932 
933 	taal_hw_reset(dssdev);
934 
935 	omapdss_dsi_display_disable(dssdev);
936 err0:
937 	return r;
938 }
939 
taal_power_off(struct omap_dss_device * dssdev)940 static void taal_power_off(struct omap_dss_device *dssdev)
941 {
942 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
943 	int r;
944 
945 	r = taal_dcs_write_0(td, DCS_DISPLAY_OFF);
946 	if (!r) {
947 		r = taal_sleep_in(td);
948 		/* HACK: wait a bit so that the message goes through */
949 		msleep(10);
950 	}
951 
952 	if (r) {
953 		dev_err(&dssdev->dev,
954 				"error disabling panel, issuing HW reset\n");
955 		taal_hw_reset(dssdev);
956 	}
957 
958 	omapdss_dsi_display_disable(dssdev);
959 
960 	td->enabled = 0;
961 }
962 
taal_enable(struct omap_dss_device * dssdev)963 static int taal_enable(struct omap_dss_device *dssdev)
964 {
965 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
966 	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
967 	int r;
968 
969 	dev_dbg(&dssdev->dev, "enable\n");
970 
971 	mutex_lock(&td->lock);
972 
973 	if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED) {
974 		r = -EINVAL;
975 		goto err;
976 	}
977 
978 	dsi_bus_lock();
979 
980 	r = taal_power_on(dssdev);
981 
982 	dsi_bus_unlock();
983 
984 	if (r)
985 		goto err;
986 
987 	if (panel_data->use_esd_check)
988 		queue_delayed_work(td->esd_wq, &td->esd_work,
989 				TAAL_ESD_CHECK_PERIOD);
990 
991 	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
992 
993 	mutex_unlock(&td->lock);
994 
995 	return 0;
996 err:
997 	dev_dbg(&dssdev->dev, "enable failed\n");
998 	mutex_unlock(&td->lock);
999 	return r;
1000 }
1001 
taal_disable(struct omap_dss_device * dssdev)1002 static void taal_disable(struct omap_dss_device *dssdev)
1003 {
1004 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1005 
1006 	dev_dbg(&dssdev->dev, "disable\n");
1007 
1008 	mutex_lock(&td->lock);
1009 
1010 	cancel_delayed_work(&td->esd_work);
1011 
1012 	dsi_bus_lock();
1013 
1014 	if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
1015 		taal_power_off(dssdev);
1016 
1017 	dsi_bus_unlock();
1018 
1019 	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
1020 
1021 	mutex_unlock(&td->lock);
1022 }
1023 
taal_suspend(struct omap_dss_device * dssdev)1024 static int taal_suspend(struct omap_dss_device *dssdev)
1025 {
1026 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1027 	int r;
1028 
1029 	dev_dbg(&dssdev->dev, "suspend\n");
1030 
1031 	mutex_lock(&td->lock);
1032 
1033 	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
1034 		r = -EINVAL;
1035 		goto err;
1036 	}
1037 
1038 	cancel_delayed_work(&td->esd_work);
1039 
1040 	dsi_bus_lock();
1041 
1042 	taal_power_off(dssdev);
1043 
1044 	dsi_bus_unlock();
1045 
1046 	dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
1047 
1048 	mutex_unlock(&td->lock);
1049 
1050 	return 0;
1051 err:
1052 	mutex_unlock(&td->lock);
1053 	return r;
1054 }
1055 
taal_resume(struct omap_dss_device * dssdev)1056 static int taal_resume(struct omap_dss_device *dssdev)
1057 {
1058 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1059 	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1060 	int r;
1061 
1062 	dev_dbg(&dssdev->dev, "resume\n");
1063 
1064 	mutex_lock(&td->lock);
1065 
1066 	if (dssdev->state != OMAP_DSS_DISPLAY_SUSPENDED) {
1067 		r = -EINVAL;
1068 		goto err;
1069 	}
1070 
1071 	dsi_bus_lock();
1072 
1073 	r = taal_power_on(dssdev);
1074 
1075 	dsi_bus_unlock();
1076 
1077 	if (r) {
1078 		dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
1079 	} else {
1080 		dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
1081 		if (panel_data->use_esd_check)
1082 			queue_delayed_work(td->esd_wq, &td->esd_work,
1083 					TAAL_ESD_CHECK_PERIOD);
1084 	}
1085 
1086 	mutex_unlock(&td->lock);
1087 
1088 	return r;
1089 err:
1090 	mutex_unlock(&td->lock);
1091 	return r;
1092 }
1093 
taal_framedone_cb(int err,void * data)1094 static void taal_framedone_cb(int err, void *data)
1095 {
1096 	struct omap_dss_device *dssdev = data;
1097 	dev_dbg(&dssdev->dev, "framedone, err %d\n", err);
1098 	dsi_bus_unlock();
1099 }
1100 
taal_te_isr(int irq,void * data)1101 static irqreturn_t taal_te_isr(int irq, void *data)
1102 {
1103 	struct omap_dss_device *dssdev = data;
1104 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1105 	int old;
1106 	int r;
1107 
1108 	old = atomic_cmpxchg(&td->do_update, 1, 0);
1109 
1110 	if (old) {
1111 		cancel_delayed_work(&td->te_timeout_work);
1112 
1113 		r = omap_dsi_update(dssdev, td->channel,
1114 				td->update_region.x,
1115 				td->update_region.y,
1116 				td->update_region.w,
1117 				td->update_region.h,
1118 				taal_framedone_cb, dssdev);
1119 		if (r)
1120 			goto err;
1121 	}
1122 
1123 	return IRQ_HANDLED;
1124 err:
1125 	dev_err(&dssdev->dev, "start update failed\n");
1126 	dsi_bus_unlock();
1127 	return IRQ_HANDLED;
1128 }
1129 
taal_te_timeout_work_callback(struct work_struct * work)1130 static void taal_te_timeout_work_callback(struct work_struct *work)
1131 {
1132 	struct taal_data *td = container_of(work, struct taal_data,
1133 					te_timeout_work.work);
1134 	struct omap_dss_device *dssdev = td->dssdev;
1135 
1136 	dev_err(&dssdev->dev, "TE not received for 250ms!\n");
1137 
1138 	atomic_set(&td->do_update, 0);
1139 	dsi_bus_unlock();
1140 }
1141 
taal_update(struct omap_dss_device * dssdev,u16 x,u16 y,u16 w,u16 h)1142 static int taal_update(struct omap_dss_device *dssdev,
1143 				    u16 x, u16 y, u16 w, u16 h)
1144 {
1145 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1146 	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1147 	int r;
1148 
1149 	dev_dbg(&dssdev->dev, "update %d, %d, %d x %d\n", x, y, w, h);
1150 
1151 	mutex_lock(&td->lock);
1152 	dsi_bus_lock();
1153 
1154 	if (!td->enabled) {
1155 		r = 0;
1156 		goto err;
1157 	}
1158 
1159 	r = omap_dsi_prepare_update(dssdev, &x, &y, &w, &h, true);
1160 	if (r)
1161 		goto err;
1162 
1163 	r = taal_set_update_window(td, x, y, w, h);
1164 	if (r)
1165 		goto err;
1166 
1167 	if (td->te_enabled && panel_data->use_ext_te) {
1168 		td->update_region.x = x;
1169 		td->update_region.y = y;
1170 		td->update_region.w = w;
1171 		td->update_region.h = h;
1172 		barrier();
1173 		schedule_delayed_work(&td->te_timeout_work,
1174 				msecs_to_jiffies(250));
1175 		atomic_set(&td->do_update, 1);
1176 	} else {
1177 		r = omap_dsi_update(dssdev, td->channel, x, y, w, h,
1178 				taal_framedone_cb, dssdev);
1179 		if (r)
1180 			goto err;
1181 	}
1182 
1183 	/* note: no bus_unlock here. unlock is in framedone_cb */
1184 	mutex_unlock(&td->lock);
1185 	return 0;
1186 err:
1187 	dsi_bus_unlock();
1188 	mutex_unlock(&td->lock);
1189 	return r;
1190 }
1191 
taal_sync(struct omap_dss_device * dssdev)1192 static int taal_sync(struct omap_dss_device *dssdev)
1193 {
1194 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1195 
1196 	dev_dbg(&dssdev->dev, "sync\n");
1197 
1198 	mutex_lock(&td->lock);
1199 	dsi_bus_lock();
1200 	dsi_bus_unlock();
1201 	mutex_unlock(&td->lock);
1202 
1203 	dev_dbg(&dssdev->dev, "sync done\n");
1204 
1205 	return 0;
1206 }
1207 
_taal_enable_te(struct omap_dss_device * dssdev,bool enable)1208 static int _taal_enable_te(struct omap_dss_device *dssdev, bool enable)
1209 {
1210 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1211 	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1212 	int r;
1213 
1214 	if (enable)
1215 		r = taal_dcs_write_1(td, DCS_TEAR_ON, 0);
1216 	else
1217 		r = taal_dcs_write_0(td, DCS_TEAR_OFF);
1218 
1219 	if (!panel_data->use_ext_te)
1220 		omapdss_dsi_enable_te(dssdev, enable);
1221 
1222 	if (td->panel_config->sleep.enable_te)
1223 		msleep(td->panel_config->sleep.enable_te);
1224 
1225 	return r;
1226 }
1227 
taal_enable_te(struct omap_dss_device * dssdev,bool enable)1228 static int taal_enable_te(struct omap_dss_device *dssdev, bool enable)
1229 {
1230 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1231 	int r;
1232 
1233 	mutex_lock(&td->lock);
1234 
1235 	if (td->te_enabled == enable)
1236 		goto end;
1237 
1238 	dsi_bus_lock();
1239 
1240 	if (td->enabled) {
1241 		r = _taal_enable_te(dssdev, enable);
1242 		if (r)
1243 			goto err;
1244 	}
1245 
1246 	td->te_enabled = enable;
1247 
1248 	dsi_bus_unlock();
1249 end:
1250 	mutex_unlock(&td->lock);
1251 
1252 	return 0;
1253 err:
1254 	dsi_bus_unlock();
1255 	mutex_unlock(&td->lock);
1256 
1257 	return r;
1258 }
1259 
taal_get_te(struct omap_dss_device * dssdev)1260 static int taal_get_te(struct omap_dss_device *dssdev)
1261 {
1262 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1263 	int r;
1264 
1265 	mutex_lock(&td->lock);
1266 	r = td->te_enabled;
1267 	mutex_unlock(&td->lock);
1268 
1269 	return r;
1270 }
1271 
taal_rotate(struct omap_dss_device * dssdev,u8 rotate)1272 static int taal_rotate(struct omap_dss_device *dssdev, u8 rotate)
1273 {
1274 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1275 	int r;
1276 
1277 	dev_dbg(&dssdev->dev, "rotate %d\n", rotate);
1278 
1279 	mutex_lock(&td->lock);
1280 
1281 	if (td->rotate == rotate)
1282 		goto end;
1283 
1284 	dsi_bus_lock();
1285 
1286 	if (td->enabled) {
1287 		r = taal_set_addr_mode(td, rotate, td->mirror);
1288 		if (r)
1289 			goto err;
1290 	}
1291 
1292 	td->rotate = rotate;
1293 
1294 	dsi_bus_unlock();
1295 end:
1296 	mutex_unlock(&td->lock);
1297 	return 0;
1298 err:
1299 	dsi_bus_unlock();
1300 	mutex_unlock(&td->lock);
1301 	return r;
1302 }
1303 
taal_get_rotate(struct omap_dss_device * dssdev)1304 static u8 taal_get_rotate(struct omap_dss_device *dssdev)
1305 {
1306 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1307 	int r;
1308 
1309 	mutex_lock(&td->lock);
1310 	r = td->rotate;
1311 	mutex_unlock(&td->lock);
1312 
1313 	return r;
1314 }
1315 
taal_mirror(struct omap_dss_device * dssdev,bool enable)1316 static int taal_mirror(struct omap_dss_device *dssdev, bool enable)
1317 {
1318 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1319 	int r;
1320 
1321 	dev_dbg(&dssdev->dev, "mirror %d\n", enable);
1322 
1323 	mutex_lock(&td->lock);
1324 
1325 	if (td->mirror == enable)
1326 		goto end;
1327 
1328 	dsi_bus_lock();
1329 	if (td->enabled) {
1330 		r = taal_set_addr_mode(td, td->rotate, enable);
1331 		if (r)
1332 			goto err;
1333 	}
1334 
1335 	td->mirror = enable;
1336 
1337 	dsi_bus_unlock();
1338 end:
1339 	mutex_unlock(&td->lock);
1340 	return 0;
1341 err:
1342 	dsi_bus_unlock();
1343 	mutex_unlock(&td->lock);
1344 	return r;
1345 }
1346 
taal_get_mirror(struct omap_dss_device * dssdev)1347 static bool taal_get_mirror(struct omap_dss_device *dssdev)
1348 {
1349 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1350 	int r;
1351 
1352 	mutex_lock(&td->lock);
1353 	r = td->mirror;
1354 	mutex_unlock(&td->lock);
1355 
1356 	return r;
1357 }
1358 
taal_run_test(struct omap_dss_device * dssdev,int test_num)1359 static int taal_run_test(struct omap_dss_device *dssdev, int test_num)
1360 {
1361 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1362 	u8 id1, id2, id3;
1363 	int r;
1364 
1365 	mutex_lock(&td->lock);
1366 
1367 	if (!td->enabled) {
1368 		r = -ENODEV;
1369 		goto err1;
1370 	}
1371 
1372 	dsi_bus_lock();
1373 
1374 	r = taal_dcs_read_1(td, DCS_GET_ID1, &id1);
1375 	if (r)
1376 		goto err2;
1377 	r = taal_dcs_read_1(td, DCS_GET_ID2, &id2);
1378 	if (r)
1379 		goto err2;
1380 	r = taal_dcs_read_1(td, DCS_GET_ID3, &id3);
1381 	if (r)
1382 		goto err2;
1383 
1384 	dsi_bus_unlock();
1385 	mutex_unlock(&td->lock);
1386 	return 0;
1387 err2:
1388 	dsi_bus_unlock();
1389 err1:
1390 	mutex_unlock(&td->lock);
1391 	return r;
1392 }
1393 
taal_memory_read(struct omap_dss_device * dssdev,void * buf,size_t size,u16 x,u16 y,u16 w,u16 h)1394 static int taal_memory_read(struct omap_dss_device *dssdev,
1395 		void *buf, size_t size,
1396 		u16 x, u16 y, u16 w, u16 h)
1397 {
1398 	int r;
1399 	int first = 1;
1400 	int plen;
1401 	unsigned buf_used = 0;
1402 	struct taal_data *td = dev_get_drvdata(&dssdev->dev);
1403 
1404 	if (size < w * h * 3)
1405 		return -ENOMEM;
1406 
1407 	mutex_lock(&td->lock);
1408 
1409 	if (!td->enabled) {
1410 		r = -ENODEV;
1411 		goto err1;
1412 	}
1413 
1414 	size = min(w * h * 3,
1415 			dssdev->panel.timings.x_res *
1416 			dssdev->panel.timings.y_res * 3);
1417 
1418 	dsi_bus_lock();
1419 
1420 	/* plen 1 or 2 goes into short packet. until checksum error is fixed,
1421 	 * use short packets. plen 32 works, but bigger packets seem to cause
1422 	 * an error. */
1423 	if (size % 2)
1424 		plen = 1;
1425 	else
1426 		plen = 2;
1427 
1428 	taal_set_update_window(td, x, y, w, h);
1429 
1430 	r = dsi_vc_set_max_rx_packet_size(td->channel, plen);
1431 	if (r)
1432 		goto err2;
1433 
1434 	while (buf_used < size) {
1435 		u8 dcs_cmd = first ? 0x2e : 0x3e;
1436 		first = 0;
1437 
1438 		r = dsi_vc_dcs_read(td->channel, dcs_cmd,
1439 				buf + buf_used, size - buf_used);
1440 
1441 		if (r < 0) {
1442 			dev_err(&dssdev->dev, "read error\n");
1443 			goto err3;
1444 		}
1445 
1446 		buf_used += r;
1447 
1448 		if (r < plen) {
1449 			dev_err(&dssdev->dev, "short read\n");
1450 			break;
1451 		}
1452 
1453 		if (signal_pending(current)) {
1454 			dev_err(&dssdev->dev, "signal pending, "
1455 					"aborting memory read\n");
1456 			r = -ERESTARTSYS;
1457 			goto err3;
1458 		}
1459 	}
1460 
1461 	r = buf_used;
1462 
1463 err3:
1464 	dsi_vc_set_max_rx_packet_size(td->channel, 1);
1465 err2:
1466 	dsi_bus_unlock();
1467 err1:
1468 	mutex_unlock(&td->lock);
1469 	return r;
1470 }
1471 
taal_esd_work(struct work_struct * work)1472 static void taal_esd_work(struct work_struct *work)
1473 {
1474 	struct taal_data *td = container_of(work, struct taal_data,
1475 			esd_work.work);
1476 	struct omap_dss_device *dssdev = td->dssdev;
1477 	struct nokia_dsi_panel_data *panel_data = get_panel_data(dssdev);
1478 	u8 state1, state2;
1479 	int r;
1480 
1481 	mutex_lock(&td->lock);
1482 
1483 	if (!td->enabled) {
1484 		mutex_unlock(&td->lock);
1485 		return;
1486 	}
1487 
1488 	dsi_bus_lock();
1489 
1490 	r = taal_dcs_read_1(td, DCS_RDDSDR, &state1);
1491 	if (r) {
1492 		dev_err(&dssdev->dev, "failed to read Taal status\n");
1493 		goto err;
1494 	}
1495 
1496 	/* Run self diagnostics */
1497 	r = taal_sleep_out(td);
1498 	if (r) {
1499 		dev_err(&dssdev->dev, "failed to run Taal self-diagnostics\n");
1500 		goto err;
1501 	}
1502 
1503 	r = taal_dcs_read_1(td, DCS_RDDSDR, &state2);
1504 	if (r) {
1505 		dev_err(&dssdev->dev, "failed to read Taal status\n");
1506 		goto err;
1507 	}
1508 
1509 	/* Each sleep out command will trigger a self diagnostic and flip
1510 	 * Bit6 if the test passes.
1511 	 */
1512 	if (!((state1 ^ state2) & (1 << 6))) {
1513 		dev_err(&dssdev->dev, "LCD self diagnostics failed\n");
1514 		goto err;
1515 	}
1516 	/* Self-diagnostics result is also shown on TE GPIO line. We need
1517 	 * to re-enable TE after self diagnostics */
1518 	if (td->te_enabled && panel_data->use_ext_te) {
1519 		r = taal_dcs_write_1(td, DCS_TEAR_ON, 0);
1520 		if (r)
1521 			goto err;
1522 	}
1523 
1524 	dsi_bus_unlock();
1525 
1526 	queue_delayed_work(td->esd_wq, &td->esd_work, TAAL_ESD_CHECK_PERIOD);
1527 
1528 	mutex_unlock(&td->lock);
1529 	return;
1530 err:
1531 	dev_err(&dssdev->dev, "performing LCD reset\n");
1532 
1533 	taal_power_off(dssdev);
1534 	taal_hw_reset(dssdev);
1535 	taal_power_on(dssdev);
1536 
1537 	dsi_bus_unlock();
1538 
1539 	queue_delayed_work(td->esd_wq, &td->esd_work, TAAL_ESD_CHECK_PERIOD);
1540 
1541 	mutex_unlock(&td->lock);
1542 }
1543 
taal_set_update_mode(struct omap_dss_device * dssdev,enum omap_dss_update_mode mode)1544 static int taal_set_update_mode(struct omap_dss_device *dssdev,
1545 		enum omap_dss_update_mode mode)
1546 {
1547 	if (mode != OMAP_DSS_UPDATE_MANUAL)
1548 		return -EINVAL;
1549 	return 0;
1550 }
1551 
taal_get_update_mode(struct omap_dss_device * dssdev)1552 static enum omap_dss_update_mode taal_get_update_mode(
1553 		struct omap_dss_device *dssdev)
1554 {
1555 	return OMAP_DSS_UPDATE_MANUAL;
1556 }
1557 
1558 static struct omap_dss_driver taal_driver = {
1559 	.probe		= taal_probe,
1560 	.remove		= taal_remove,
1561 
1562 	.enable		= taal_enable,
1563 	.disable	= taal_disable,
1564 	.suspend	= taal_suspend,
1565 	.resume		= taal_resume,
1566 
1567 	.set_update_mode = taal_set_update_mode,
1568 	.get_update_mode = taal_get_update_mode,
1569 
1570 	.update		= taal_update,
1571 	.sync		= taal_sync,
1572 
1573 	.get_resolution	= taal_get_resolution,
1574 	.get_recommended_bpp = omapdss_default_get_recommended_bpp,
1575 
1576 	.enable_te	= taal_enable_te,
1577 	.get_te		= taal_get_te,
1578 
1579 	.set_rotate	= taal_rotate,
1580 	.get_rotate	= taal_get_rotate,
1581 	.set_mirror	= taal_mirror,
1582 	.get_mirror	= taal_get_mirror,
1583 	.run_test	= taal_run_test,
1584 	.memory_read	= taal_memory_read,
1585 
1586 	.get_timings	= taal_get_timings,
1587 
1588 	.driver         = {
1589 		.name   = "taal",
1590 		.owner  = THIS_MODULE,
1591 	},
1592 };
1593 
taal_init(void)1594 static int __init taal_init(void)
1595 {
1596 	omap_dss_register_driver(&taal_driver);
1597 
1598 	return 0;
1599 }
1600 
taal_exit(void)1601 static void __exit taal_exit(void)
1602 {
1603 	omap_dss_unregister_driver(&taal_driver);
1604 }
1605 
1606 module_init(taal_init);
1607 module_exit(taal_exit);
1608 
1609 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
1610 MODULE_DESCRIPTION("Taal Driver");
1611 MODULE_LICENSE("GPL");
1612