1/*
2 * LCD panel driver for Sharp LS037V7DW01
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#include <linux/delay.h>
13#include <linux/gpio.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_gpio.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include <linux/regulator/consumer.h>
20#include <video/omapdss.h>
21#include <video/omap-panel-data.h>
22
23struct panel_drv_data {
24	struct omap_dss_device dssdev;
25	struct omap_dss_device *in;
26	struct regulator *vcc;
27
28	int data_lines;
29
30	struct omap_video_timings videomode;
31
32	struct gpio_desc *resb_gpio;	/* low = reset active min 20 us */
33	struct gpio_desc *ini_gpio;	/* high = power on */
34	struct gpio_desc *mo_gpio;	/* low = 480x640, high = 240x320 */
35	struct gpio_desc *lr_gpio;	/* high = conventional horizontal scanning */
36	struct gpio_desc *ud_gpio;	/* high = conventional vertical scanning */
37};
38
39static const struct omap_video_timings sharp_ls_timings = {
40	.x_res = 480,
41	.y_res = 640,
42
43	.pixelclock	= 19200000,
44
45	.hsw		= 2,
46	.hfp		= 1,
47	.hbp		= 28,
48
49	.vsw		= 1,
50	.vfp		= 1,
51	.vbp		= 1,
52
53	.vsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
54	.hsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
55	.data_pclk_edge	= OMAPDSS_DRIVE_SIG_RISING_EDGE,
56	.de_level	= OMAPDSS_SIG_ACTIVE_HIGH,
57	.sync_pclk_edge	= OMAPDSS_DRIVE_SIG_FALLING_EDGE,
58};
59
60#define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
61
62static int sharp_ls_connect(struct omap_dss_device *dssdev)
63{
64	struct panel_drv_data *ddata = to_panel_data(dssdev);
65	struct omap_dss_device *in = ddata->in;
66	int r;
67
68	if (omapdss_device_is_connected(dssdev))
69		return 0;
70
71	r = in->ops.dpi->connect(in, dssdev);
72	if (r)
73		return r;
74
75	return 0;
76}
77
78static void sharp_ls_disconnect(struct omap_dss_device *dssdev)
79{
80	struct panel_drv_data *ddata = to_panel_data(dssdev);
81	struct omap_dss_device *in = ddata->in;
82
83	if (!omapdss_device_is_connected(dssdev))
84		return;
85
86	in->ops.dpi->disconnect(in, dssdev);
87}
88
89static int sharp_ls_enable(struct omap_dss_device *dssdev)
90{
91	struct panel_drv_data *ddata = to_panel_data(dssdev);
92	struct omap_dss_device *in = ddata->in;
93	int r;
94
95	if (!omapdss_device_is_connected(dssdev))
96		return -ENODEV;
97
98	if (omapdss_device_is_enabled(dssdev))
99		return 0;
100
101	if (ddata->data_lines)
102		in->ops.dpi->set_data_lines(in, ddata->data_lines);
103	in->ops.dpi->set_timings(in, &ddata->videomode);
104
105	if (ddata->vcc) {
106		r = regulator_enable(ddata->vcc);
107		if (r != 0)
108			return r;
109	}
110
111	r = in->ops.dpi->enable(in);
112	if (r) {
113		regulator_disable(ddata->vcc);
114		return r;
115	}
116
117	/* wait couple of vsyncs until enabling the LCD */
118	msleep(50);
119
120	if (ddata->resb_gpio)
121		gpiod_set_value_cansleep(ddata->resb_gpio, 1);
122
123	if (ddata->ini_gpio)
124		gpiod_set_value_cansleep(ddata->ini_gpio, 1);
125
126	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
127
128	return 0;
129}
130
131static void sharp_ls_disable(struct omap_dss_device *dssdev)
132{
133	struct panel_drv_data *ddata = to_panel_data(dssdev);
134	struct omap_dss_device *in = ddata->in;
135
136	if (!omapdss_device_is_enabled(dssdev))
137		return;
138
139	if (ddata->ini_gpio)
140		gpiod_set_value_cansleep(ddata->ini_gpio, 0);
141
142	if (ddata->resb_gpio)
143		gpiod_set_value_cansleep(ddata->resb_gpio, 0);
144
145	/* wait at least 5 vsyncs after disabling the LCD */
146
147	msleep(100);
148
149	in->ops.dpi->disable(in);
150
151	if (ddata->vcc)
152		regulator_disable(ddata->vcc);
153
154	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
155}
156
157static void sharp_ls_set_timings(struct omap_dss_device *dssdev,
158		struct omap_video_timings *timings)
159{
160	struct panel_drv_data *ddata = to_panel_data(dssdev);
161	struct omap_dss_device *in = ddata->in;
162
163	ddata->videomode = *timings;
164	dssdev->panel.timings = *timings;
165
166	in->ops.dpi->set_timings(in, timings);
167}
168
169static void sharp_ls_get_timings(struct omap_dss_device *dssdev,
170		struct omap_video_timings *timings)
171{
172	struct panel_drv_data *ddata = to_panel_data(dssdev);
173
174	*timings = ddata->videomode;
175}
176
177static int sharp_ls_check_timings(struct omap_dss_device *dssdev,
178		struct omap_video_timings *timings)
179{
180	struct panel_drv_data *ddata = to_panel_data(dssdev);
181	struct omap_dss_device *in = ddata->in;
182
183	return in->ops.dpi->check_timings(in, timings);
184}
185
186static struct omap_dss_driver sharp_ls_ops = {
187	.connect	= sharp_ls_connect,
188	.disconnect	= sharp_ls_disconnect,
189
190	.enable		= sharp_ls_enable,
191	.disable	= sharp_ls_disable,
192
193	.set_timings	= sharp_ls_set_timings,
194	.get_timings	= sharp_ls_get_timings,
195	.check_timings	= sharp_ls_check_timings,
196
197	.get_resolution	= omapdss_default_get_resolution,
198};
199
200static int sharp_ls_get_gpio(struct device *dev, int gpio, unsigned long flags,
201		  char *desc, struct gpio_desc **gpiod)
202{
203	struct gpio_desc *gd;
204	int r;
205
206	*gpiod = NULL;
207
208	r = devm_gpio_request_one(dev, gpio, flags, desc);
209	if (r)
210		return r == -ENOENT ? 0 : r;
211
212	gd = gpio_to_desc(gpio);
213	if (IS_ERR(gd))
214		return PTR_ERR(gd) == -ENOENT ? 0 : PTR_ERR(gd);
215
216	*gpiod = gd;
217	return 0;
218}
219
220static int sharp_ls_probe_pdata(struct platform_device *pdev)
221{
222	const struct panel_sharp_ls037v7dw01_platform_data *pdata;
223	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
224	struct omap_dss_device *dssdev, *in;
225	int r;
226
227	pdata = dev_get_platdata(&pdev->dev);
228
229	in = omap_dss_find_output(pdata->source);
230	if (in == NULL) {
231		dev_err(&pdev->dev, "failed to find video source '%s'\n",
232				pdata->source);
233		return -EPROBE_DEFER;
234	}
235
236	ddata->in = in;
237
238	ddata->data_lines = pdata->data_lines;
239
240	dssdev = &ddata->dssdev;
241	dssdev->name = pdata->name;
242
243	r = sharp_ls_get_gpio(&pdev->dev, pdata->mo_gpio, GPIOF_OUT_INIT_LOW,
244		"lcd MO", &ddata->mo_gpio);
245	if (r)
246		return r;
247	r = sharp_ls_get_gpio(&pdev->dev, pdata->lr_gpio, GPIOF_OUT_INIT_HIGH,
248		"lcd LR", &ddata->lr_gpio);
249	if (r)
250		return r;
251	r = sharp_ls_get_gpio(&pdev->dev, pdata->ud_gpio, GPIOF_OUT_INIT_HIGH,
252		"lcd UD", &ddata->ud_gpio);
253	if (r)
254		return r;
255	r = sharp_ls_get_gpio(&pdev->dev, pdata->resb_gpio, GPIOF_OUT_INIT_LOW,
256		"lcd RESB", &ddata->resb_gpio);
257	if (r)
258		return r;
259	r = sharp_ls_get_gpio(&pdev->dev, pdata->ini_gpio, GPIOF_OUT_INIT_LOW,
260		"lcd INI", &ddata->ini_gpio);
261	if (r)
262		return r;
263
264	return 0;
265}
266
267static  int sharp_ls_get_gpio_of(struct device *dev, int index, int val,
268	const char *desc, struct gpio_desc **gpiod)
269{
270	struct gpio_desc *gd;
271
272	*gpiod = NULL;
273
274	gd = devm_gpiod_get_index(dev, desc, index, GPIOD_OUT_LOW);
275	if (IS_ERR(gd))
276		return PTR_ERR(gd);
277
278	*gpiod = gd;
279	return 0;
280}
281
282static int sharp_ls_probe_of(struct platform_device *pdev)
283{
284	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
285	struct device_node *node = pdev->dev.of_node;
286	struct omap_dss_device *in;
287	int r;
288
289	ddata->vcc = devm_regulator_get(&pdev->dev, "envdd");
290	if (IS_ERR(ddata->vcc)) {
291		dev_err(&pdev->dev, "failed to get regulator\n");
292		return PTR_ERR(ddata->vcc);
293	}
294
295	/* lcd INI */
296	r = sharp_ls_get_gpio_of(&pdev->dev, 0, 0, "enable", &ddata->ini_gpio);
297	if (r)
298		return r;
299
300	/* lcd RESB */
301	r = sharp_ls_get_gpio_of(&pdev->dev, 0, 0, "reset", &ddata->resb_gpio);
302	if (r)
303		return r;
304
305	/* lcd MO */
306	r = sharp_ls_get_gpio_of(&pdev->dev, 0, 0, "mode", &ddata->mo_gpio);
307	if (r)
308		return r;
309
310	/* lcd LR */
311	r = sharp_ls_get_gpio_of(&pdev->dev, 1, 1, "mode", &ddata->lr_gpio);
312	if (r)
313		return r;
314
315	/* lcd UD */
316	r = sharp_ls_get_gpio_of(&pdev->dev, 2, 1, "mode", &ddata->ud_gpio);
317	if (r)
318		return r;
319
320	in = omapdss_of_find_source_for_first_ep(node);
321	if (IS_ERR(in)) {
322		dev_err(&pdev->dev, "failed to find video source\n");
323		return PTR_ERR(in);
324	}
325
326	ddata->in = in;
327
328	return 0;
329}
330
331static int sharp_ls_probe(struct platform_device *pdev)
332{
333	struct panel_drv_data *ddata;
334	struct omap_dss_device *dssdev;
335	int r;
336
337	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
338	if (ddata == NULL)
339		return -ENOMEM;
340
341	platform_set_drvdata(pdev, ddata);
342
343	if (dev_get_platdata(&pdev->dev)) {
344		r = sharp_ls_probe_pdata(pdev);
345		if (r)
346			return r;
347	} else if (pdev->dev.of_node) {
348		r = sharp_ls_probe_of(pdev);
349		if (r)
350			return r;
351	} else {
352		return -ENODEV;
353	}
354
355	ddata->videomode = sharp_ls_timings;
356
357	dssdev = &ddata->dssdev;
358	dssdev->dev = &pdev->dev;
359	dssdev->driver = &sharp_ls_ops;
360	dssdev->type = OMAP_DISPLAY_TYPE_DPI;
361	dssdev->owner = THIS_MODULE;
362	dssdev->panel.timings = ddata->videomode;
363	dssdev->phy.dpi.data_lines = ddata->data_lines;
364
365	r = omapdss_register_display(dssdev);
366	if (r) {
367		dev_err(&pdev->dev, "Failed to register panel\n");
368		goto err_reg;
369	}
370
371	return 0;
372
373err_reg:
374	omap_dss_put_device(ddata->in);
375	return r;
376}
377
378static int __exit sharp_ls_remove(struct platform_device *pdev)
379{
380	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
381	struct omap_dss_device *dssdev = &ddata->dssdev;
382	struct omap_dss_device *in = ddata->in;
383
384	omapdss_unregister_display(dssdev);
385
386	sharp_ls_disable(dssdev);
387	sharp_ls_disconnect(dssdev);
388
389	omap_dss_put_device(in);
390
391	return 0;
392}
393
394static const struct of_device_id sharp_ls_of_match[] = {
395	{ .compatible = "omapdss,sharp,ls037v7dw01", },
396	{},
397};
398
399MODULE_DEVICE_TABLE(of, sharp_ls_of_match);
400
401static struct platform_driver sharp_ls_driver = {
402	.probe = sharp_ls_probe,
403	.remove = __exit_p(sharp_ls_remove),
404	.driver = {
405		.name = "panel-sharp-ls037v7dw01",
406		.of_match_table = sharp_ls_of_match,
407		.suppress_bind_attrs = true,
408	},
409};
410
411module_platform_driver(sharp_ls_driver);
412
413MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
414MODULE_DESCRIPTION("Sharp LS037V7DW01 Panel Driver");
415MODULE_LICENSE("GPL");
416