Linux 4.16.11
[linux/fpc-iii.git] / drivers / video / fbdev / omap2 / omapfb / displays / panel-lgphilips-lb035q02.c
blob6cd759c0103754b3378915eecf4f6ce7da5314d3
1 /*
2 * LG.Philips LB035Q02 LCD Panel driver
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>
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.
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>
19 #include <video/omapfb_dss.h>
21 static const struct omap_video_timings lb035q02_timings = {
22 .x_res = 320,
23 .y_res = 240,
25 .pixelclock = 6500000,
27 .hsw = 2,
28 .hfp = 20,
29 .hbp = 68,
31 .vsw = 2,
32 .vfp = 4,
33 .vbp = 18,
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,
42 struct panel_drv_data {
43 struct omap_dss_device dssdev;
44 struct omap_dss_device *in;
46 struct spi_device *spi;
48 int data_lines;
50 struct omap_video_timings videomode;
52 /* used for non-DT boot, to be removed */
53 int backlight_gpio;
55 struct gpio_desc *enable_gpio;
58 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
60 static int lb035q02_write_reg(struct spi_device *spi, u8 reg, u16 val)
62 struct spi_message msg;
63 struct spi_transfer index_xfer = {
64 .len = 3,
65 .cs_change = 1,
67 struct spi_transfer value_xfer = {
68 .len = 3,
70 u8 buffer[16];
72 spi_message_init(&msg);
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);
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);
88 return spi_sync(spi, &msg);
91 static void init_lb035q02_panel(struct spi_device *spi)
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);
120 static int lb035q02_connect(struct omap_dss_device *dssdev)
122 struct panel_drv_data *ddata = to_panel_data(dssdev);
123 struct omap_dss_device *in = ddata->in;
124 int r;
126 if (omapdss_device_is_connected(dssdev))
127 return 0;
129 r = in->ops.dpi->connect(in, dssdev);
130 if (r)
131 return r;
133 init_lb035q02_panel(ddata->spi);
135 return 0;
138 static void lb035q02_disconnect(struct omap_dss_device *dssdev)
140 struct panel_drv_data *ddata = to_panel_data(dssdev);
141 struct omap_dss_device *in = ddata->in;
143 if (!omapdss_device_is_connected(dssdev))
144 return;
146 in->ops.dpi->disconnect(in, dssdev);
149 static int lb035q02_enable(struct omap_dss_device *dssdev)
151 struct panel_drv_data *ddata = to_panel_data(dssdev);
152 struct omap_dss_device *in = ddata->in;
153 int r;
155 if (!omapdss_device_is_connected(dssdev))
156 return -ENODEV;
158 if (omapdss_device_is_enabled(dssdev))
159 return 0;
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);
165 r = in->ops.dpi->enable(in);
166 if (r)
167 return r;
169 if (ddata->enable_gpio)
170 gpiod_set_value_cansleep(ddata->enable_gpio, 1);
172 if (gpio_is_valid(ddata->backlight_gpio))
173 gpio_set_value_cansleep(ddata->backlight_gpio, 1);
175 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
177 return 0;
180 static void lb035q02_disable(struct omap_dss_device *dssdev)
182 struct panel_drv_data *ddata = to_panel_data(dssdev);
183 struct omap_dss_device *in = ddata->in;
185 if (!omapdss_device_is_enabled(dssdev))
186 return;
188 if (ddata->enable_gpio)
189 gpiod_set_value_cansleep(ddata->enable_gpio, 0);
191 if (gpio_is_valid(ddata->backlight_gpio))
192 gpio_set_value_cansleep(ddata->backlight_gpio, 0);
194 in->ops.dpi->disable(in);
196 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
199 static void lb035q02_set_timings(struct omap_dss_device *dssdev,
200 struct omap_video_timings *timings)
202 struct panel_drv_data *ddata = to_panel_data(dssdev);
203 struct omap_dss_device *in = ddata->in;
205 ddata->videomode = *timings;
206 dssdev->panel.timings = *timings;
208 in->ops.dpi->set_timings(in, timings);
211 static void lb035q02_get_timings(struct omap_dss_device *dssdev,
212 struct omap_video_timings *timings)
214 struct panel_drv_data *ddata = to_panel_data(dssdev);
216 *timings = ddata->videomode;
219 static int lb035q02_check_timings(struct omap_dss_device *dssdev,
220 struct omap_video_timings *timings)
222 struct panel_drv_data *ddata = to_panel_data(dssdev);
223 struct omap_dss_device *in = ddata->in;
225 return in->ops.dpi->check_timings(in, timings);
228 static struct omap_dss_driver lb035q02_ops = {
229 .connect = lb035q02_connect,
230 .disconnect = lb035q02_disconnect,
232 .enable = lb035q02_enable,
233 .disable = lb035q02_disable,
235 .set_timings = lb035q02_set_timings,
236 .get_timings = lb035q02_get_timings,
237 .check_timings = lb035q02_check_timings,
239 .get_resolution = omapdss_default_get_resolution,
242 static int lb035q02_probe_of(struct spi_device *spi)
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;
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);
255 ddata->enable_gpio = gpio;
257 ddata->backlight_gpio = -ENOENT;
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);
265 ddata->in = in;
267 return 0;
270 static int lb035q02_panel_spi_probe(struct spi_device *spi)
272 struct panel_drv_data *ddata;
273 struct omap_dss_device *dssdev;
274 int r;
276 if (!spi->dev.of_node)
277 return -ENODEV;
279 ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
280 if (ddata == NULL)
281 return -ENOMEM;
283 dev_set_drvdata(&spi->dev, ddata);
285 ddata->spi = spi;
287 r = lb035q02_probe_of(spi);
288 if (r)
289 return r;
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;
298 ddata->videomode = lb035q02_timings;
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;
308 r = omapdss_register_display(dssdev);
309 if (r) {
310 dev_err(&spi->dev, "Failed to register panel\n");
311 goto err_reg;
314 return 0;
316 err_reg:
317 err_gpio:
318 omap_dss_put_device(ddata->in);
319 return r;
322 static int lb035q02_panel_spi_remove(struct spi_device *spi)
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;
328 omapdss_unregister_display(dssdev);
330 lb035q02_disable(dssdev);
331 lb035q02_disconnect(dssdev);
333 omap_dss_put_device(in);
335 return 0;
338 static const struct of_device_id lb035q02_of_match[] = {
339 { .compatible = "omapdss,lgphilips,lb035q02", },
343 MODULE_DEVICE_TABLE(of, lb035q02_of_match);
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,
355 module_spi_driver(lb035q02_spi_driver);
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");