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