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	int r;
272
273	*gpiod = NULL;
274
275	gd = devm_gpiod_get_index(dev, desc, index);
276	if (IS_ERR(gd))
277		return PTR_ERR(gd) == -ENOENT ? 0 : PTR_ERR(gd);
278
279	r = gpiod_direction_output(gd, val);
280	if (r)
281		return r;
282
283	*gpiod = gd;
284	return 0;
285}
286
287static int sharp_ls_probe_of(struct platform_device *pdev)
288{
289	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
290	struct device_node *node = pdev->dev.of_node;
291	struct omap_dss_device *in;
292	int r;
293
294	ddata->vcc = devm_regulator_get(&pdev->dev, "envdd");
295	if (IS_ERR(ddata->vcc)) {
296		dev_err(&pdev->dev, "failed to get regulator\n");
297		return PTR_ERR(ddata->vcc);
298	}
299
300	/* lcd INI */
301	r = sharp_ls_get_gpio_of(&pdev->dev, 0, 0, "enable", &ddata->ini_gpio);
302	if (r)
303		return r;
304
305	/* lcd RESB */
306	r = sharp_ls_get_gpio_of(&pdev->dev, 0, 0, "reset", &ddata->resb_gpio);
307	if (r)
308		return r;
309
310	/* lcd MO */
311	r = sharp_ls_get_gpio_of(&pdev->dev, 0, 0, "mode", &ddata->mo_gpio);
312	if (r)
313		return r;
314
315	/* lcd LR */
316	r = sharp_ls_get_gpio_of(&pdev->dev, 1, 1, "mode", &ddata->lr_gpio);
317	if (r)
318		return r;
319
320	/* lcd UD */
321	r = sharp_ls_get_gpio_of(&pdev->dev, 2, 1, "mode", &ddata->ud_gpio);
322	if (r)
323		return r;
324
325	in = omapdss_of_find_source_for_first_ep(node);
326	if (IS_ERR(in)) {
327		dev_err(&pdev->dev, "failed to find video source\n");
328		return PTR_ERR(in);
329	}
330
331	ddata->in = in;
332
333	return 0;
334}
335
336static int sharp_ls_probe(struct platform_device *pdev)
337{
338	struct panel_drv_data *ddata;
339	struct omap_dss_device *dssdev;
340	int r;
341
342	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
343	if (ddata == NULL)
344		return -ENOMEM;
345
346	platform_set_drvdata(pdev, ddata);
347
348	if (dev_get_platdata(&pdev->dev)) {
349		r = sharp_ls_probe_pdata(pdev);
350		if (r)
351			return r;
352	} else if (pdev->dev.of_node) {
353		r = sharp_ls_probe_of(pdev);
354		if (r)
355			return r;
356	} else {
357		return -ENODEV;
358	}
359
360	ddata->videomode = sharp_ls_timings;
361
362	dssdev = &ddata->dssdev;
363	dssdev->dev = &pdev->dev;
364	dssdev->driver = &sharp_ls_ops;
365	dssdev->type = OMAP_DISPLAY_TYPE_DPI;
366	dssdev->owner = THIS_MODULE;
367	dssdev->panel.timings = ddata->videomode;
368	dssdev->phy.dpi.data_lines = ddata->data_lines;
369
370	r = omapdss_register_display(dssdev);
371	if (r) {
372		dev_err(&pdev->dev, "Failed to register panel\n");
373		goto err_reg;
374	}
375
376	return 0;
377
378err_reg:
379	omap_dss_put_device(ddata->in);
380	return r;
381}
382
383static int __exit sharp_ls_remove(struct platform_device *pdev)
384{
385	struct panel_drv_data *ddata = platform_get_drvdata(pdev);
386	struct omap_dss_device *dssdev = &ddata->dssdev;
387	struct omap_dss_device *in = ddata->in;
388
389	omapdss_unregister_display(dssdev);
390
391	sharp_ls_disable(dssdev);
392	sharp_ls_disconnect(dssdev);
393
394	omap_dss_put_device(in);
395
396	return 0;
397}
398
399static const struct of_device_id sharp_ls_of_match[] = {
400	{ .compatible = "omapdss,sharp,ls037v7dw01", },
401	{},
402};
403
404MODULE_DEVICE_TABLE(of, sharp_ls_of_match);
405
406static struct platform_driver sharp_ls_driver = {
407	.probe = sharp_ls_probe,
408	.remove = __exit_p(sharp_ls_remove),
409	.driver = {
410		.name = "panel-sharp-ls037v7dw01",
411		.of_match_table = sharp_ls_of_match,
412		.suppress_bind_attrs = true,
413	},
414};
415
416module_platform_driver(sharp_ls_driver);
417
418MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
419MODULE_DESCRIPTION("Sharp LS037V7DW01 Panel Driver");
420MODULE_LICENSE("GPL");
421