1/*
2 * LG.Philips LB035Q02 LCD Panel driver
3 *
4 * Copyright (C) 2013 Texas Instruments
5 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
6 * Based on a driver by: Steve Sakoman <steve@sakoman.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/delay.h>
15#include <linux/spi/spi.h>
16#include <linux/mutex.h>
17#include <linux/gpio.h>
18
19#include <video/omapdss.h>
20#include <video/omap-panel-data.h>
21
22static struct omap_video_timings lb035q02_timings = {
23	.x_res = 320,
24	.y_res = 240,
25
26	.pixelclock	= 6500000,
27
28	.hsw		= 2,
29	.hfp		= 20,
30	.hbp		= 68,
31
32	.vsw		= 2,
33	.vfp		= 4,
34	.vbp		= 18,
35
36	.vsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
37	.hsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
38	.data_pclk_edge	= OMAPDSS_DRIVE_SIG_RISING_EDGE,
39	.de_level	= OMAPDSS_SIG_ACTIVE_HIGH,
40	.sync_pclk_edge	= OMAPDSS_DRIVE_SIG_FALLING_EDGE,
41};
42
43struct panel_drv_data {
44	struct omap_dss_device dssdev;
45	struct omap_dss_device *in;
46
47	struct spi_device *spi;
48
49	int data_lines;
50
51	struct omap_video_timings videomode;
52
53	/* used for non-DT boot, to be removed */
54	int backlight_gpio;
55
56	struct gpio_desc *enable_gpio;
57};
58
59#define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
60
61static int lb035q02_write_reg(struct spi_device *spi, u8 reg, u16 val)
62{
63	struct spi_message msg;
64	struct spi_transfer index_xfer = {
65		.len		= 3,
66		.cs_change	= 1,
67	};
68	struct spi_transfer value_xfer = {
69		.len		= 3,
70	};
71	u8	buffer[16];
72
73	spi_message_init(&msg);
74
75	/* register index */
76	buffer[0] = 0x70;
77	buffer[1] = 0x00;
78	buffer[2] = reg & 0x7f;
79	index_xfer.tx_buf = buffer;
80	spi_message_add_tail(&index_xfer, &msg);
81
82	/* register value */
83	buffer[4] = 0x72;
84	buffer[5] = val >> 8;
85	buffer[6] = val;
86	value_xfer.tx_buf = buffer + 4;
87	spi_message_add_tail(&value_xfer, &msg);
88
89	return spi_sync(spi, &msg);
90}
91
92static void init_lb035q02_panel(struct spi_device *spi)
93{
94	/* Init sequence from page 28 of the lb035q02 spec */
95	lb035q02_write_reg(spi, 0x01, 0x6300);
96	lb035q02_write_reg(spi, 0x02, 0x0200);
97	lb035q02_write_reg(spi, 0x03, 0x0177);
98	lb035q02_write_reg(spi, 0x04, 0x04c7);
99	lb035q02_write_reg(spi, 0x05, 0xffc0);
100	lb035q02_write_reg(spi, 0x06, 0xe806);
101	lb035q02_write_reg(spi, 0x0a, 0x4008);
102	lb035q02_write_reg(spi, 0x0b, 0x0000);
103	lb035q02_write_reg(spi, 0x0d, 0x0030);
104	lb035q02_write_reg(spi, 0x0e, 0x2800);
105	lb035q02_write_reg(spi, 0x0f, 0x0000);
106	lb035q02_write_reg(spi, 0x16, 0x9f80);
107	lb035q02_write_reg(spi, 0x17, 0x0a0f);
108	lb035q02_write_reg(spi, 0x1e, 0x00c1);
109	lb035q02_write_reg(spi, 0x30, 0x0300);
110	lb035q02_write_reg(spi, 0x31, 0x0007);
111	lb035q02_write_reg(spi, 0x32, 0x0000);
112	lb035q02_write_reg(spi, 0x33, 0x0000);
113	lb035q02_write_reg(spi, 0x34, 0x0707);
114	lb035q02_write_reg(spi, 0x35, 0x0004);
115	lb035q02_write_reg(spi, 0x36, 0x0302);
116	lb035q02_write_reg(spi, 0x37, 0x0202);
117	lb035q02_write_reg(spi, 0x3a, 0x0a0d);
118	lb035q02_write_reg(spi, 0x3b, 0x0806);
119}
120
121static int lb035q02_connect(struct omap_dss_device *dssdev)
122{
123	struct panel_drv_data *ddata = to_panel_data(dssdev);
124	struct omap_dss_device *in = ddata->in;
125	int r;
126
127	if (omapdss_device_is_connected(dssdev))
128		return 0;
129
130	r = in->ops.dpi->connect(in, dssdev);
131	if (r)
132		return r;
133
134	init_lb035q02_panel(ddata->spi);
135
136	return 0;
137}
138
139static void lb035q02_disconnect(struct omap_dss_device *dssdev)
140{
141	struct panel_drv_data *ddata = to_panel_data(dssdev);
142	struct omap_dss_device *in = ddata->in;
143
144	if (!omapdss_device_is_connected(dssdev))
145		return;
146
147	in->ops.dpi->disconnect(in, dssdev);
148}
149
150static int lb035q02_enable(struct omap_dss_device *dssdev)
151{
152	struct panel_drv_data *ddata = to_panel_data(dssdev);
153	struct omap_dss_device *in = ddata->in;
154	int r;
155
156	if (!omapdss_device_is_connected(dssdev))
157		return -ENODEV;
158
159	if (omapdss_device_is_enabled(dssdev))
160		return 0;
161
162	if (ddata->data_lines)
163		in->ops.dpi->set_data_lines(in, ddata->data_lines);
164	in->ops.dpi->set_timings(in, &ddata->videomode);
165
166	r = in->ops.dpi->enable(in);
167	if (r)
168		return r;
169
170	if (ddata->enable_gpio)
171		gpiod_set_value_cansleep(ddata->enable_gpio, 1);
172
173	if (gpio_is_valid(ddata->backlight_gpio))
174		gpio_set_value_cansleep(ddata->backlight_gpio, 1);
175
176	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
177
178	return 0;
179}
180
181static void lb035q02_disable(struct omap_dss_device *dssdev)
182{
183	struct panel_drv_data *ddata = to_panel_data(dssdev);
184	struct omap_dss_device *in = ddata->in;
185
186	if (!omapdss_device_is_enabled(dssdev))
187		return;
188
189	if (ddata->enable_gpio)
190		gpiod_set_value_cansleep(ddata->enable_gpio, 0);
191
192	if (gpio_is_valid(ddata->backlight_gpio))
193		gpio_set_value_cansleep(ddata->backlight_gpio, 0);
194
195	in->ops.dpi->disable(in);
196
197	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
198}
199
200static void lb035q02_set_timings(struct omap_dss_device *dssdev,
201		struct omap_video_timings *timings)
202{
203	struct panel_drv_data *ddata = to_panel_data(dssdev);
204	struct omap_dss_device *in = ddata->in;
205
206	ddata->videomode = *timings;
207	dssdev->panel.timings = *timings;
208
209	in->ops.dpi->set_timings(in, timings);
210}
211
212static void lb035q02_get_timings(struct omap_dss_device *dssdev,
213		struct omap_video_timings *timings)
214{
215	struct panel_drv_data *ddata = to_panel_data(dssdev);
216
217	*timings = ddata->videomode;
218}
219
220static int lb035q02_check_timings(struct omap_dss_device *dssdev,
221		struct omap_video_timings *timings)
222{
223	struct panel_drv_data *ddata = to_panel_data(dssdev);
224	struct omap_dss_device *in = ddata->in;
225
226	return in->ops.dpi->check_timings(in, timings);
227}
228
229static struct omap_dss_driver lb035q02_ops = {
230	.connect	= lb035q02_connect,
231	.disconnect	= lb035q02_disconnect,
232
233	.enable		= lb035q02_enable,
234	.disable	= lb035q02_disable,
235
236	.set_timings	= lb035q02_set_timings,
237	.get_timings	= lb035q02_get_timings,
238	.check_timings	= lb035q02_check_timings,
239
240	.get_resolution	= omapdss_default_get_resolution,
241};
242
243static int lb035q02_probe_pdata(struct spi_device *spi)
244{
245	const struct panel_lb035q02_platform_data *pdata;
246	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
247	struct omap_dss_device *dssdev, *in;
248	int r;
249
250	pdata = dev_get_platdata(&spi->dev);
251
252	in = omap_dss_find_output(pdata->source);
253	if (in == NULL) {
254		dev_err(&spi->dev, "failed to find video source '%s'\n",
255				pdata->source);
256		return -EPROBE_DEFER;
257	}
258
259	ddata->in = in;
260
261	ddata->data_lines = pdata->data_lines;
262
263	dssdev = &ddata->dssdev;
264	dssdev->name = pdata->name;
265
266	r = devm_gpio_request_one(&spi->dev, pdata->enable_gpio,
267					GPIOF_OUT_INIT_LOW, "panel enable");
268	if (r)
269		goto err_gpio;
270
271	ddata->enable_gpio = gpio_to_desc(pdata->enable_gpio);
272
273	ddata->backlight_gpio = pdata->backlight_gpio;
274
275	return 0;
276err_gpio:
277	omap_dss_put_device(ddata->in);
278	return r;
279}
280
281static int lb035q02_probe_of(struct spi_device *spi)
282{
283	struct device_node *node = spi->dev.of_node;
284	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
285	struct omap_dss_device *in;
286	struct gpio_desc *gpio;
287
288	gpio = devm_gpiod_get(&spi->dev, "enable");
289	if (IS_ERR(gpio)) {
290		dev_err(&spi->dev, "failed to parse enable gpio\n");
291		return PTR_ERR(gpio);
292	} else {
293		gpiod_direction_output(gpio, 0);
294		ddata->enable_gpio = gpio;
295	}
296
297	ddata->backlight_gpio = -ENOENT;
298
299	in = omapdss_of_find_source_for_first_ep(node);
300	if (IS_ERR(in)) {
301		dev_err(&spi->dev, "failed to find video source\n");
302		return PTR_ERR(in);
303	}
304
305	ddata->in = in;
306
307	return 0;
308}
309
310static int lb035q02_panel_spi_probe(struct spi_device *spi)
311{
312	struct panel_drv_data *ddata;
313	struct omap_dss_device *dssdev;
314	int r;
315
316	ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
317	if (ddata == NULL)
318		return -ENOMEM;
319
320	dev_set_drvdata(&spi->dev, ddata);
321
322	ddata->spi = spi;
323
324	if (dev_get_platdata(&spi->dev)) {
325		r = lb035q02_probe_pdata(spi);
326		if (r)
327			return r;
328	} else if (spi->dev.of_node) {
329		r = lb035q02_probe_of(spi);
330		if (r)
331			return r;
332	} else {
333		return -ENODEV;
334	}
335
336	if (gpio_is_valid(ddata->backlight_gpio)) {
337		r = devm_gpio_request_one(&spi->dev, ddata->backlight_gpio,
338				GPIOF_OUT_INIT_LOW, "panel backlight");
339		if (r)
340			goto err_gpio;
341	}
342
343	ddata->videomode = lb035q02_timings;
344
345	dssdev = &ddata->dssdev;
346	dssdev->dev = &spi->dev;
347	dssdev->driver = &lb035q02_ops;
348	dssdev->type = OMAP_DISPLAY_TYPE_DPI;
349	dssdev->owner = THIS_MODULE;
350	dssdev->panel.timings = ddata->videomode;
351	dssdev->phy.dpi.data_lines = ddata->data_lines;
352
353	r = omapdss_register_display(dssdev);
354	if (r) {
355		dev_err(&spi->dev, "Failed to register panel\n");
356		goto err_reg;
357	}
358
359	return 0;
360
361err_reg:
362err_gpio:
363	omap_dss_put_device(ddata->in);
364	return r;
365}
366
367static int lb035q02_panel_spi_remove(struct spi_device *spi)
368{
369	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
370	struct omap_dss_device *dssdev = &ddata->dssdev;
371	struct omap_dss_device *in = ddata->in;
372
373	omapdss_unregister_display(dssdev);
374
375	lb035q02_disable(dssdev);
376	lb035q02_disconnect(dssdev);
377
378	omap_dss_put_device(in);
379
380	return 0;
381}
382
383static const struct of_device_id lb035q02_of_match[] = {
384	{ .compatible = "omapdss,lgphilips,lb035q02", },
385	{},
386};
387
388MODULE_DEVICE_TABLE(of, lb035q02_of_match);
389
390static struct spi_driver lb035q02_spi_driver = {
391	.probe		= lb035q02_panel_spi_probe,
392	.remove		= lb035q02_panel_spi_remove,
393	.driver		= {
394		.name	= "panel_lgphilips_lb035q02",
395		.owner	= THIS_MODULE,
396		.of_match_table = lb035q02_of_match,
397		.suppress_bind_attrs = true,
398	},
399};
400
401module_spi_driver(lb035q02_spi_driver);
402
403MODULE_ALIAS("spi:lgphilips,lb035q02");
404MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
405MODULE_DESCRIPTION("LG.Philips LB035Q02 LCD Panel driver");
406MODULE_LICENSE("GPL");
407