1/*
2 * TPO TD043MTEA1 Panel driver
3 *
4 * Author: Gra��vydas Ignotas <notasas@gmail.com>
5 * Converted to new DSS device model: Tomi Valkeinen <tomi.valkeinen@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/module.h>
14#include <linux/delay.h>
15#include <linux/spi/spi.h>
16#include <linux/regulator/consumer.h>
17#include <linux/gpio.h>
18#include <linux/err.h>
19#include <linux/slab.h>
20#include <linux/of_gpio.h>
21
22#include <video/omapdss.h>
23#include <video/omap-panel-data.h>
24
25#define TPO_R02_MODE(x)		((x) & 7)
26#define TPO_R02_MODE_800x480	7
27#define TPO_R02_NCLK_RISING	BIT(3)
28#define TPO_R02_HSYNC_HIGH	BIT(4)
29#define TPO_R02_VSYNC_HIGH	BIT(5)
30
31#define TPO_R03_NSTANDBY	BIT(0)
32#define TPO_R03_EN_CP_CLK	BIT(1)
33#define TPO_R03_EN_VGL_PUMP	BIT(2)
34#define TPO_R03_EN_PWM		BIT(3)
35#define TPO_R03_DRIVING_CAP_100	BIT(4)
36#define TPO_R03_EN_PRE_CHARGE	BIT(6)
37#define TPO_R03_SOFTWARE_CTL	BIT(7)
38
39#define TPO_R04_NFLIP_H		BIT(0)
40#define TPO_R04_NFLIP_V		BIT(1)
41#define TPO_R04_CP_CLK_FREQ_1H	BIT(2)
42#define TPO_R04_VGL_FREQ_1H	BIT(4)
43
44#define TPO_R03_VAL_NORMAL (TPO_R03_NSTANDBY | TPO_R03_EN_CP_CLK | \
45			TPO_R03_EN_VGL_PUMP |  TPO_R03_EN_PWM | \
46			TPO_R03_DRIVING_CAP_100 | TPO_R03_EN_PRE_CHARGE | \
47			TPO_R03_SOFTWARE_CTL)
48
49#define TPO_R03_VAL_STANDBY (TPO_R03_DRIVING_CAP_100 | \
50			TPO_R03_EN_PRE_CHARGE | TPO_R03_SOFTWARE_CTL)
51
52static const u16 tpo_td043_def_gamma[12] = {
53	105, 315, 381, 431, 490, 537, 579, 686, 780, 837, 880, 1023
54};
55
56struct panel_drv_data {
57	struct omap_dss_device	dssdev;
58	struct omap_dss_device *in;
59
60	struct omap_video_timings videomode;
61
62	int data_lines;
63
64	struct spi_device *spi;
65	struct regulator *vcc_reg;
66	int nreset_gpio;
67	u16 gamma[12];
68	u32 mode;
69	u32 hmirror:1;
70	u32 vmirror:1;
71	u32 powered_on:1;
72	u32 spi_suspended:1;
73	u32 power_on_resume:1;
74};
75
76static const struct omap_video_timings tpo_td043_timings = {
77	.x_res		= 800,
78	.y_res		= 480,
79
80	.pixelclock	= 36000000,
81
82	.hsw		= 1,
83	.hfp		= 68,
84	.hbp		= 214,
85
86	.vsw		= 1,
87	.vfp		= 39,
88	.vbp		= 34,
89
90	.vsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
91	.hsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
92	.data_pclk_edge	= OMAPDSS_DRIVE_SIG_FALLING_EDGE,
93	.de_level	= OMAPDSS_SIG_ACTIVE_HIGH,
94	.sync_pclk_edge	= OMAPDSS_DRIVE_SIG_RISING_EDGE,
95};
96
97#define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
98
99static int tpo_td043_write(struct spi_device *spi, u8 addr, u8 data)
100{
101	struct spi_message	m;
102	struct spi_transfer	xfer;
103	u16			w;
104	int			r;
105
106	spi_message_init(&m);
107
108	memset(&xfer, 0, sizeof(xfer));
109
110	w = ((u16)addr << 10) | (1 << 8) | data;
111	xfer.tx_buf = &w;
112	xfer.bits_per_word = 16;
113	xfer.len = 2;
114	spi_message_add_tail(&xfer, &m);
115
116	r = spi_sync(spi, &m);
117	if (r < 0)
118		dev_warn(&spi->dev, "failed to write to LCD reg (%d)\n", r);
119	return r;
120}
121
122static void tpo_td043_write_gamma(struct spi_device *spi, u16 gamma[12])
123{
124	u8 i, val;
125
126	/* gamma bits [9:8] */
127	for (val = i = 0; i < 4; i++)
128		val |= (gamma[i] & 0x300) >> ((i + 1) * 2);
129	tpo_td043_write(spi, 0x11, val);
130
131	for (val = i = 0; i < 4; i++)
132		val |= (gamma[i+4] & 0x300) >> ((i + 1) * 2);
133	tpo_td043_write(spi, 0x12, val);
134
135	for (val = i = 0; i < 4; i++)
136		val |= (gamma[i+8] & 0x300) >> ((i + 1) * 2);
137	tpo_td043_write(spi, 0x13, val);
138
139	/* gamma bits [7:0] */
140	for (val = i = 0; i < 12; i++)
141		tpo_td043_write(spi, 0x14 + i, gamma[i] & 0xff);
142}
143
144static int tpo_td043_write_mirror(struct spi_device *spi, bool h, bool v)
145{
146	u8 reg4 = TPO_R04_NFLIP_H | TPO_R04_NFLIP_V |
147		TPO_R04_CP_CLK_FREQ_1H | TPO_R04_VGL_FREQ_1H;
148	if (h)
149		reg4 &= ~TPO_R04_NFLIP_H;
150	if (v)
151		reg4 &= ~TPO_R04_NFLIP_V;
152
153	return tpo_td043_write(spi, 4, reg4);
154}
155
156static int tpo_td043_set_hmirror(struct omap_dss_device *dssdev, bool enable)
157{
158	struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
159
160	ddata->hmirror = enable;
161	return tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
162			ddata->vmirror);
163}
164
165static bool tpo_td043_get_hmirror(struct omap_dss_device *dssdev)
166{
167	struct panel_drv_data *ddata = dev_get_drvdata(dssdev->dev);
168
169	return ddata->hmirror;
170}
171
172static ssize_t tpo_td043_vmirror_show(struct device *dev,
173	struct device_attribute *attr, char *buf)
174{
175	struct panel_drv_data *ddata = dev_get_drvdata(dev);
176
177	return snprintf(buf, PAGE_SIZE, "%d\n", ddata->vmirror);
178}
179
180static ssize_t tpo_td043_vmirror_store(struct device *dev,
181	struct device_attribute *attr, const char *buf, size_t count)
182{
183	struct panel_drv_data *ddata = dev_get_drvdata(dev);
184	int val;
185	int ret;
186
187	ret = kstrtoint(buf, 0, &val);
188	if (ret < 0)
189		return ret;
190
191	val = !!val;
192
193	ret = tpo_td043_write_mirror(ddata->spi, ddata->hmirror, val);
194	if (ret < 0)
195		return ret;
196
197	ddata->vmirror = val;
198
199	return count;
200}
201
202static ssize_t tpo_td043_mode_show(struct device *dev,
203	struct device_attribute *attr, char *buf)
204{
205	struct panel_drv_data *ddata = dev_get_drvdata(dev);
206
207	return snprintf(buf, PAGE_SIZE, "%d\n", ddata->mode);
208}
209
210static ssize_t tpo_td043_mode_store(struct device *dev,
211	struct device_attribute *attr, const char *buf, size_t count)
212{
213	struct panel_drv_data *ddata = dev_get_drvdata(dev);
214	long val;
215	int ret;
216
217	ret = kstrtol(buf, 0, &val);
218	if (ret != 0 || val & ~7)
219		return -EINVAL;
220
221	ddata->mode = val;
222
223	val |= TPO_R02_NCLK_RISING;
224	tpo_td043_write(ddata->spi, 2, val);
225
226	return count;
227}
228
229static ssize_t tpo_td043_gamma_show(struct device *dev,
230	struct device_attribute *attr, char *buf)
231{
232	struct panel_drv_data *ddata = dev_get_drvdata(dev);
233	ssize_t len = 0;
234	int ret;
235	int i;
236
237	for (i = 0; i < ARRAY_SIZE(ddata->gamma); i++) {
238		ret = snprintf(buf + len, PAGE_SIZE - len, "%u ",
239				ddata->gamma[i]);
240		if (ret < 0)
241			return ret;
242		len += ret;
243	}
244	buf[len - 1] = '\n';
245
246	return len;
247}
248
249static ssize_t tpo_td043_gamma_store(struct device *dev,
250	struct device_attribute *attr, const char *buf, size_t count)
251{
252	struct panel_drv_data *ddata = dev_get_drvdata(dev);
253	unsigned int g[12];
254	int ret;
255	int i;
256
257	ret = sscanf(buf, "%u %u %u %u %u %u %u %u %u %u %u %u",
258			&g[0], &g[1], &g[2], &g[3], &g[4], &g[5],
259			&g[6], &g[7], &g[8], &g[9], &g[10], &g[11]);
260
261	if (ret != 12)
262		return -EINVAL;
263
264	for (i = 0; i < 12; i++)
265		ddata->gamma[i] = g[i];
266
267	tpo_td043_write_gamma(ddata->spi, ddata->gamma);
268
269	return count;
270}
271
272static DEVICE_ATTR(vmirror, S_IRUGO | S_IWUSR,
273		tpo_td043_vmirror_show, tpo_td043_vmirror_store);
274static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
275		tpo_td043_mode_show, tpo_td043_mode_store);
276static DEVICE_ATTR(gamma, S_IRUGO | S_IWUSR,
277		tpo_td043_gamma_show, tpo_td043_gamma_store);
278
279static struct attribute *tpo_td043_attrs[] = {
280	&dev_attr_vmirror.attr,
281	&dev_attr_mode.attr,
282	&dev_attr_gamma.attr,
283	NULL,
284};
285
286static struct attribute_group tpo_td043_attr_group = {
287	.attrs = tpo_td043_attrs,
288};
289
290static int tpo_td043_power_on(struct panel_drv_data *ddata)
291{
292	int r;
293
294	if (ddata->powered_on)
295		return 0;
296
297	r = regulator_enable(ddata->vcc_reg);
298	if (r != 0)
299		return r;
300
301	/* wait for panel to stabilize */
302	msleep(160);
303
304	if (gpio_is_valid(ddata->nreset_gpio))
305		gpio_set_value(ddata->nreset_gpio, 1);
306
307	tpo_td043_write(ddata->spi, 2,
308			TPO_R02_MODE(ddata->mode) | TPO_R02_NCLK_RISING);
309	tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_NORMAL);
310	tpo_td043_write(ddata->spi, 0x20, 0xf0);
311	tpo_td043_write(ddata->spi, 0x21, 0xf0);
312	tpo_td043_write_mirror(ddata->spi, ddata->hmirror,
313			ddata->vmirror);
314	tpo_td043_write_gamma(ddata->spi, ddata->gamma);
315
316	ddata->powered_on = 1;
317	return 0;
318}
319
320static void tpo_td043_power_off(struct panel_drv_data *ddata)
321{
322	if (!ddata->powered_on)
323		return;
324
325	tpo_td043_write(ddata->spi, 3,
326			TPO_R03_VAL_STANDBY | TPO_R03_EN_PWM);
327
328	if (gpio_is_valid(ddata->nreset_gpio))
329		gpio_set_value(ddata->nreset_gpio, 0);
330
331	/* wait for at least 2 vsyncs before cutting off power */
332	msleep(50);
333
334	tpo_td043_write(ddata->spi, 3, TPO_R03_VAL_STANDBY);
335
336	regulator_disable(ddata->vcc_reg);
337
338	ddata->powered_on = 0;
339}
340
341static int tpo_td043_connect(struct omap_dss_device *dssdev)
342{
343	struct panel_drv_data *ddata = to_panel_data(dssdev);
344	struct omap_dss_device *in = ddata->in;
345	int r;
346
347	if (omapdss_device_is_connected(dssdev))
348		return 0;
349
350	r = in->ops.dpi->connect(in, dssdev);
351	if (r)
352		return r;
353
354	return 0;
355}
356
357static void tpo_td043_disconnect(struct omap_dss_device *dssdev)
358{
359	struct panel_drv_data *ddata = to_panel_data(dssdev);
360	struct omap_dss_device *in = ddata->in;
361
362	if (!omapdss_device_is_connected(dssdev))
363		return;
364
365	in->ops.dpi->disconnect(in, dssdev);
366}
367
368static int tpo_td043_enable(struct omap_dss_device *dssdev)
369{
370	struct panel_drv_data *ddata = to_panel_data(dssdev);
371	struct omap_dss_device *in = ddata->in;
372	int r;
373
374	if (!omapdss_device_is_connected(dssdev))
375		return -ENODEV;
376
377	if (omapdss_device_is_enabled(dssdev))
378		return 0;
379
380	if (ddata->data_lines)
381		in->ops.dpi->set_data_lines(in, ddata->data_lines);
382	in->ops.dpi->set_timings(in, &ddata->videomode);
383
384	r = in->ops.dpi->enable(in);
385	if (r)
386		return r;
387
388	/*
389	 * If we are resuming from system suspend, SPI clocks might not be
390	 * enabled yet, so we'll program the LCD from SPI PM resume callback.
391	 */
392	if (!ddata->spi_suspended) {
393		r = tpo_td043_power_on(ddata);
394		if (r) {
395			in->ops.dpi->disable(in);
396			return r;
397		}
398	}
399
400	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
401
402	return 0;
403}
404
405static void tpo_td043_disable(struct omap_dss_device *dssdev)
406{
407	struct panel_drv_data *ddata = to_panel_data(dssdev);
408	struct omap_dss_device *in = ddata->in;
409
410	if (!omapdss_device_is_enabled(dssdev))
411		return;
412
413	in->ops.dpi->disable(in);
414
415	if (!ddata->spi_suspended)
416		tpo_td043_power_off(ddata);
417
418	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
419}
420
421static void tpo_td043_set_timings(struct omap_dss_device *dssdev,
422		struct omap_video_timings *timings)
423{
424	struct panel_drv_data *ddata = to_panel_data(dssdev);
425	struct omap_dss_device *in = ddata->in;
426
427	ddata->videomode = *timings;
428	dssdev->panel.timings = *timings;
429
430	in->ops.dpi->set_timings(in, timings);
431}
432
433static void tpo_td043_get_timings(struct omap_dss_device *dssdev,
434		struct omap_video_timings *timings)
435{
436	struct panel_drv_data *ddata = to_panel_data(dssdev);
437
438	*timings = ddata->videomode;
439}
440
441static int tpo_td043_check_timings(struct omap_dss_device *dssdev,
442		struct omap_video_timings *timings)
443{
444	struct panel_drv_data *ddata = to_panel_data(dssdev);
445	struct omap_dss_device *in = ddata->in;
446
447	return in->ops.dpi->check_timings(in, timings);
448}
449
450static struct omap_dss_driver tpo_td043_ops = {
451	.connect	= tpo_td043_connect,
452	.disconnect	= tpo_td043_disconnect,
453
454	.enable		= tpo_td043_enable,
455	.disable	= tpo_td043_disable,
456
457	.set_timings	= tpo_td043_set_timings,
458	.get_timings	= tpo_td043_get_timings,
459	.check_timings	= tpo_td043_check_timings,
460
461	.set_mirror	= tpo_td043_set_hmirror,
462	.get_mirror	= tpo_td043_get_hmirror,
463
464	.get_resolution	= omapdss_default_get_resolution,
465};
466
467
468static int tpo_td043_probe_pdata(struct spi_device *spi)
469{
470	const struct panel_tpo_td043mtea1_platform_data *pdata;
471	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
472	struct omap_dss_device *dssdev, *in;
473
474	pdata = dev_get_platdata(&spi->dev);
475
476	ddata->nreset_gpio = pdata->nreset_gpio;
477
478	in = omap_dss_find_output(pdata->source);
479	if (in == NULL) {
480		dev_err(&spi->dev, "failed to find video source '%s'\n",
481				pdata->source);
482		return -EPROBE_DEFER;
483	}
484	ddata->in = in;
485
486	ddata->data_lines = pdata->data_lines;
487
488	dssdev = &ddata->dssdev;
489	dssdev->name = pdata->name;
490
491	return 0;
492}
493
494static int tpo_td043_probe_of(struct spi_device *spi)
495{
496	struct device_node *node = spi->dev.of_node;
497	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
498	struct omap_dss_device *in;
499	int gpio;
500
501	gpio = of_get_named_gpio(node, "reset-gpios", 0);
502	if (!gpio_is_valid(gpio)) {
503		dev_err(&spi->dev, "failed to parse enable gpio\n");
504		return gpio;
505	}
506	ddata->nreset_gpio = gpio;
507
508	in = omapdss_of_find_source_for_first_ep(node);
509	if (IS_ERR(in)) {
510		dev_err(&spi->dev, "failed to find video source\n");
511		return PTR_ERR(in);
512	}
513
514	ddata->in = in;
515
516	return 0;
517}
518
519static int tpo_td043_probe(struct spi_device *spi)
520{
521	struct panel_drv_data *ddata;
522	struct omap_dss_device *dssdev;
523	int r;
524
525	dev_dbg(&spi->dev, "%s\n", __func__);
526
527	spi->bits_per_word = 16;
528	spi->mode = SPI_MODE_0;
529
530	r = spi_setup(spi);
531	if (r < 0) {
532		dev_err(&spi->dev, "spi_setup failed: %d\n", r);
533		return r;
534	}
535
536	ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
537	if (ddata == NULL)
538		return -ENOMEM;
539
540	dev_set_drvdata(&spi->dev, ddata);
541
542	ddata->spi = spi;
543
544	if (dev_get_platdata(&spi->dev)) {
545		r = tpo_td043_probe_pdata(spi);
546		if (r)
547			return r;
548	} else if (spi->dev.of_node) {
549		r = tpo_td043_probe_of(spi);
550		if (r)
551			return r;
552	} else {
553		return -ENODEV;
554	}
555
556	ddata->mode = TPO_R02_MODE_800x480;
557	memcpy(ddata->gamma, tpo_td043_def_gamma, sizeof(ddata->gamma));
558
559	ddata->vcc_reg = devm_regulator_get(&spi->dev, "vcc");
560	if (IS_ERR(ddata->vcc_reg)) {
561		dev_err(&spi->dev, "failed to get LCD VCC regulator\n");
562		r = PTR_ERR(ddata->vcc_reg);
563		goto err_regulator;
564	}
565
566	if (gpio_is_valid(ddata->nreset_gpio)) {
567		r = devm_gpio_request_one(&spi->dev,
568				ddata->nreset_gpio, GPIOF_OUT_INIT_LOW,
569				"lcd reset");
570		if (r < 0) {
571			dev_err(&spi->dev, "couldn't request reset GPIO\n");
572			goto err_gpio_req;
573		}
574	}
575
576	r = sysfs_create_group(&spi->dev.kobj, &tpo_td043_attr_group);
577	if (r) {
578		dev_err(&spi->dev, "failed to create sysfs files\n");
579		goto err_sysfs;
580	}
581
582	ddata->videomode = tpo_td043_timings;
583
584	dssdev = &ddata->dssdev;
585	dssdev->dev = &spi->dev;
586	dssdev->driver = &tpo_td043_ops;
587	dssdev->type = OMAP_DISPLAY_TYPE_DPI;
588	dssdev->owner = THIS_MODULE;
589	dssdev->panel.timings = ddata->videomode;
590
591	r = omapdss_register_display(dssdev);
592	if (r) {
593		dev_err(&spi->dev, "Failed to register panel\n");
594		goto err_reg;
595	}
596
597	return 0;
598
599err_reg:
600	sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
601err_sysfs:
602err_gpio_req:
603err_regulator:
604	omap_dss_put_device(ddata->in);
605	return r;
606}
607
608static int tpo_td043_remove(struct spi_device *spi)
609{
610	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
611	struct omap_dss_device *dssdev = &ddata->dssdev;
612	struct omap_dss_device *in = ddata->in;
613
614	dev_dbg(&ddata->spi->dev, "%s\n", __func__);
615
616	omapdss_unregister_display(dssdev);
617
618	tpo_td043_disable(dssdev);
619	tpo_td043_disconnect(dssdev);
620
621	omap_dss_put_device(in);
622
623	sysfs_remove_group(&spi->dev.kobj, &tpo_td043_attr_group);
624
625	return 0;
626}
627
628#ifdef CONFIG_PM_SLEEP
629static int tpo_td043_spi_suspend(struct device *dev)
630{
631	struct panel_drv_data *ddata = dev_get_drvdata(dev);
632
633	dev_dbg(dev, "tpo_td043_spi_suspend, tpo %p\n", ddata);
634
635	ddata->power_on_resume = ddata->powered_on;
636	tpo_td043_power_off(ddata);
637	ddata->spi_suspended = 1;
638
639	return 0;
640}
641
642static int tpo_td043_spi_resume(struct device *dev)
643{
644	struct panel_drv_data *ddata = dev_get_drvdata(dev);
645	int ret;
646
647	dev_dbg(dev, "tpo_td043_spi_resume\n");
648
649	if (ddata->power_on_resume) {
650		ret = tpo_td043_power_on(ddata);
651		if (ret)
652			return ret;
653	}
654	ddata->spi_suspended = 0;
655
656	return 0;
657}
658#endif
659
660static SIMPLE_DEV_PM_OPS(tpo_td043_spi_pm,
661	tpo_td043_spi_suspend, tpo_td043_spi_resume);
662
663static const struct of_device_id tpo_td043_of_match[] = {
664	{ .compatible = "omapdss,tpo,td043mtea1", },
665	{},
666};
667
668MODULE_DEVICE_TABLE(of, tpo_td043_of_match);
669
670static struct spi_driver tpo_td043_spi_driver = {
671	.driver = {
672		.name	= "panel-tpo-td043mtea1",
673		.pm	= &tpo_td043_spi_pm,
674		.of_match_table = tpo_td043_of_match,
675		.suppress_bind_attrs = true,
676	},
677	.probe	= tpo_td043_probe,
678	.remove	= tpo_td043_remove,
679};
680
681module_spi_driver(tpo_td043_spi_driver);
682
683MODULE_ALIAS("spi:tpo,td043mtea1");
684MODULE_AUTHOR("Gra��vydas Ignotas <notasas@gmail.com>");
685MODULE_DESCRIPTION("TPO TD043MTEA1 LCD Driver");
686MODULE_LICENSE("GPL");
687