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 
22 static 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 
43 struct 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 
61 static 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 
92 static 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 
121 static 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 
139 static 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 
150 static 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 
181 static 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 
200 static 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 
212 static 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 
220 static 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 
229 static 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 
243 static 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;
276 err_gpio:
277 	omap_dss_put_device(ddata->in);
278 	return r;
279 }
280 
281 static 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", GPIOD_OUT_LOW);
289 	if (IS_ERR(gpio)) {
290 		dev_err(&spi->dev, "failed to parse enable gpio\n");
291 		return PTR_ERR(gpio);
292 	}
293 
294 	ddata->enable_gpio = gpio;
295 
296 	ddata->backlight_gpio = -ENOENT;
297 
298 	in = omapdss_of_find_source_for_first_ep(node);
299 	if (IS_ERR(in)) {
300 		dev_err(&spi->dev, "failed to find video source\n");
301 		return PTR_ERR(in);
302 	}
303 
304 	ddata->in = in;
305 
306 	return 0;
307 }
308 
309 static int lb035q02_panel_spi_probe(struct spi_device *spi)
310 {
311 	struct panel_drv_data *ddata;
312 	struct omap_dss_device *dssdev;
313 	int r;
314 
315 	ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
316 	if (ddata == NULL)
317 		return -ENOMEM;
318 
319 	dev_set_drvdata(&spi->dev, ddata);
320 
321 	ddata->spi = spi;
322 
323 	if (dev_get_platdata(&spi->dev)) {
324 		r = lb035q02_probe_pdata(spi);
325 		if (r)
326 			return r;
327 	} else if (spi->dev.of_node) {
328 		r = lb035q02_probe_of(spi);
329 		if (r)
330 			return r;
331 	} else {
332 		return -ENODEV;
333 	}
334 
335 	if (gpio_is_valid(ddata->backlight_gpio)) {
336 		r = devm_gpio_request_one(&spi->dev, ddata->backlight_gpio,
337 				GPIOF_OUT_INIT_LOW, "panel backlight");
338 		if (r)
339 			goto err_gpio;
340 	}
341 
342 	ddata->videomode = lb035q02_timings;
343 
344 	dssdev = &ddata->dssdev;
345 	dssdev->dev = &spi->dev;
346 	dssdev->driver = &lb035q02_ops;
347 	dssdev->type = OMAP_DISPLAY_TYPE_DPI;
348 	dssdev->owner = THIS_MODULE;
349 	dssdev->panel.timings = ddata->videomode;
350 	dssdev->phy.dpi.data_lines = ddata->data_lines;
351 
352 	r = omapdss_register_display(dssdev);
353 	if (r) {
354 		dev_err(&spi->dev, "Failed to register panel\n");
355 		goto err_reg;
356 	}
357 
358 	return 0;
359 
360 err_reg:
361 err_gpio:
362 	omap_dss_put_device(ddata->in);
363 	return r;
364 }
365 
366 static int lb035q02_panel_spi_remove(struct spi_device *spi)
367 {
368 	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
369 	struct omap_dss_device *dssdev = &ddata->dssdev;
370 	struct omap_dss_device *in = ddata->in;
371 
372 	omapdss_unregister_display(dssdev);
373 
374 	lb035q02_disable(dssdev);
375 	lb035q02_disconnect(dssdev);
376 
377 	omap_dss_put_device(in);
378 
379 	return 0;
380 }
381 
382 static const struct of_device_id lb035q02_of_match[] = {
383 	{ .compatible = "omapdss,lgphilips,lb035q02", },
384 	{},
385 };
386 
387 MODULE_DEVICE_TABLE(of, lb035q02_of_match);
388 
389 static struct spi_driver lb035q02_spi_driver = {
390 	.probe		= lb035q02_panel_spi_probe,
391 	.remove		= lb035q02_panel_spi_remove,
392 	.driver		= {
393 		.name	= "panel_lgphilips_lb035q02",
394 		.of_match_table = lb035q02_of_match,
395 		.suppress_bind_attrs = true,
396 	},
397 };
398 
399 module_spi_driver(lb035q02_spi_driver);
400 
401 MODULE_ALIAS("spi:lgphilips,lb035q02");
402 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
403 MODULE_DESCRIPTION("LG.Philips LB035Q02 LCD Panel driver");
404 MODULE_LICENSE("GPL");
405