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