1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * NEC NL8048HL11 Panel driver
4  *
5  * Copyright (C) 2010 Texas Instruments Inc.
6  * Author: Erik Gilling <konkers@android.com>
7  * Converted to new DSS device model: Tomi Valkeinen <tomi.valkeinen@ti.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/spi/spi.h>
13 #include <linux/fb.h>
14 #include <linux/gpio.h>
15 #include <linux/of_gpio.h>
16 
17 #include <video/omapfb_dss.h>
18 
19 struct panel_drv_data {
20 	struct omap_dss_device	dssdev;
21 	struct omap_dss_device *in;
22 
23 	struct omap_video_timings videomode;
24 
25 	int data_lines;
26 
27 	int res_gpio;
28 	int qvga_gpio;
29 
30 	struct spi_device *spi;
31 };
32 
33 #define LCD_XRES		800
34 #define LCD_YRES		480
35 /*
36  * NEC PIX Clock Ratings
37  * MIN:21.8MHz TYP:23.8MHz MAX:25.7MHz
38  */
39 #define LCD_PIXEL_CLOCK		23800000
40 
41 static const struct {
42 	unsigned char addr;
43 	unsigned char dat;
44 } nec_8048_init_seq[] = {
45 	{ 3, 0x01 }, { 0, 0x00 }, { 1, 0x01 }, { 4, 0x00 }, { 5, 0x14 },
46 	{ 6, 0x24 }, { 16, 0xD7 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x55 },
47 	{ 20, 0x01 }, { 21, 0x70 }, { 22, 0x1E }, { 23, 0x25 },	{ 24, 0x25 },
48 	{ 25, 0x02 }, { 26, 0x02 }, { 27, 0xA0 }, { 32, 0x2F }, { 33, 0x0F },
49 	{ 34, 0x0F }, { 35, 0x0F }, { 36, 0x0F }, { 37, 0x0F },	{ 38, 0x0F },
50 	{ 39, 0x00 }, { 40, 0x02 }, { 41, 0x02 }, { 42, 0x02 },	{ 43, 0x0F },
51 	{ 44, 0x0F }, { 45, 0x0F }, { 46, 0x0F }, { 47, 0x0F },	{ 48, 0x0F },
52 	{ 49, 0x0F }, { 50, 0x00 }, { 51, 0x02 }, { 52, 0x02 }, { 53, 0x02 },
53 	{ 80, 0x0C }, { 83, 0x42 }, { 84, 0x42 }, { 85, 0x41 },	{ 86, 0x14 },
54 	{ 89, 0x88 }, { 90, 0x01 }, { 91, 0x00 }, { 92, 0x02 },	{ 93, 0x0C },
55 	{ 94, 0x1C }, { 95, 0x27 }, { 98, 0x49 }, { 99, 0x27 }, { 102, 0x76 },
56 	{ 103, 0x27 }, { 112, 0x01 }, { 113, 0x0E }, { 114, 0x02 },
57 	{ 115, 0x0C }, { 118, 0x0C }, { 121, 0x30 }, { 130, 0x00 },
58 	{ 131, 0x00 }, { 132, 0xFC }, { 134, 0x00 }, { 136, 0x00 },
59 	{ 138, 0x00 }, { 139, 0x00 }, { 140, 0x00 }, { 141, 0xFC },
60 	{ 143, 0x00 }, { 145, 0x00 }, { 147, 0x00 }, { 148, 0x00 },
61 	{ 149, 0x00 }, { 150, 0xFC }, { 152, 0x00 }, { 154, 0x00 },
62 	{ 156, 0x00 }, { 157, 0x00 }, { 2, 0x00 },
63 };
64 
65 static const struct omap_video_timings nec_8048_panel_timings = {
66 	.x_res		= LCD_XRES,
67 	.y_res		= LCD_YRES,
68 	.pixelclock	= LCD_PIXEL_CLOCK,
69 	.hfp		= 6,
70 	.hsw		= 1,
71 	.hbp		= 4,
72 	.vfp		= 3,
73 	.vsw		= 1,
74 	.vbp		= 4,
75 
76 	.vsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
77 	.hsync_level	= OMAPDSS_SIG_ACTIVE_LOW,
78 	.data_pclk_edge	= OMAPDSS_DRIVE_SIG_RISING_EDGE,
79 	.de_level	= OMAPDSS_SIG_ACTIVE_HIGH,
80 	.sync_pclk_edge	= OMAPDSS_DRIVE_SIG_RISING_EDGE,
81 };
82 
83 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev)
84 
85 static int nec_8048_spi_send(struct spi_device *spi, unsigned char reg_addr,
86 			unsigned char reg_data)
87 {
88 	int ret = 0;
89 	unsigned int cmd = 0, data = 0;
90 
91 	cmd = 0x0000 | reg_addr; /* register address write */
92 	data = 0x0100 | reg_data; /* register data write */
93 	data = (cmd << 16) | data;
94 
95 	ret = spi_write(spi, (unsigned char *)&data, 4);
96 	if (ret)
97 		pr_err("error in spi_write %x\n", data);
98 
99 	return ret;
100 }
101 
102 static int init_nec_8048_wvga_lcd(struct spi_device *spi)
103 {
104 	unsigned int i;
105 	/* Initialization Sequence */
106 	/* nec_8048_spi_send(spi, REG, VAL) */
107 	for (i = 0; i < (ARRAY_SIZE(nec_8048_init_seq) - 1); i++)
108 		nec_8048_spi_send(spi, nec_8048_init_seq[i].addr,
109 				nec_8048_init_seq[i].dat);
110 	udelay(20);
111 	nec_8048_spi_send(spi, nec_8048_init_seq[i].addr,
112 				nec_8048_init_seq[i].dat);
113 	return 0;
114 }
115 
116 static int nec_8048_connect(struct omap_dss_device *dssdev)
117 {
118 	struct panel_drv_data *ddata = to_panel_data(dssdev);
119 	struct omap_dss_device *in = ddata->in;
120 
121 	if (omapdss_device_is_connected(dssdev))
122 		return 0;
123 
124 	return in->ops.dpi->connect(in, dssdev);
125 }
126 
127 static void nec_8048_disconnect(struct omap_dss_device *dssdev)
128 {
129 	struct panel_drv_data *ddata = to_panel_data(dssdev);
130 	struct omap_dss_device *in = ddata->in;
131 
132 	if (!omapdss_device_is_connected(dssdev))
133 		return;
134 
135 	in->ops.dpi->disconnect(in, dssdev);
136 }
137 
138 static int nec_8048_enable(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 	int r;
143 
144 	if (!omapdss_device_is_connected(dssdev))
145 		return -ENODEV;
146 
147 	if (omapdss_device_is_enabled(dssdev))
148 		return 0;
149 
150 	if (ddata->data_lines)
151 		in->ops.dpi->set_data_lines(in, ddata->data_lines);
152 	in->ops.dpi->set_timings(in, &ddata->videomode);
153 
154 	r = in->ops.dpi->enable(in);
155 	if (r)
156 		return r;
157 
158 	if (gpio_is_valid(ddata->res_gpio))
159 		gpio_set_value_cansleep(ddata->res_gpio, 1);
160 
161 	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
162 
163 	return 0;
164 }
165 
166 static void nec_8048_disable(struct omap_dss_device *dssdev)
167 {
168 	struct panel_drv_data *ddata = to_panel_data(dssdev);
169 	struct omap_dss_device *in = ddata->in;
170 
171 	if (!omapdss_device_is_enabled(dssdev))
172 		return;
173 
174 	if (gpio_is_valid(ddata->res_gpio))
175 		gpio_set_value_cansleep(ddata->res_gpio, 0);
176 
177 	in->ops.dpi->disable(in);
178 
179 	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
180 }
181 
182 static void nec_8048_set_timings(struct omap_dss_device *dssdev,
183 		struct omap_video_timings *timings)
184 {
185 	struct panel_drv_data *ddata = to_panel_data(dssdev);
186 	struct omap_dss_device *in = ddata->in;
187 
188 	ddata->videomode = *timings;
189 	dssdev->panel.timings = *timings;
190 
191 	in->ops.dpi->set_timings(in, timings);
192 }
193 
194 static void nec_8048_get_timings(struct omap_dss_device *dssdev,
195 		struct omap_video_timings *timings)
196 {
197 	struct panel_drv_data *ddata = to_panel_data(dssdev);
198 
199 	*timings = ddata->videomode;
200 }
201 
202 static int nec_8048_check_timings(struct omap_dss_device *dssdev,
203 		struct omap_video_timings *timings)
204 {
205 	struct panel_drv_data *ddata = to_panel_data(dssdev);
206 	struct omap_dss_device *in = ddata->in;
207 
208 	return in->ops.dpi->check_timings(in, timings);
209 }
210 
211 static struct omap_dss_driver nec_8048_ops = {
212 	.connect	= nec_8048_connect,
213 	.disconnect	= nec_8048_disconnect,
214 
215 	.enable		= nec_8048_enable,
216 	.disable	= nec_8048_disable,
217 
218 	.set_timings	= nec_8048_set_timings,
219 	.get_timings	= nec_8048_get_timings,
220 	.check_timings	= nec_8048_check_timings,
221 
222 	.get_resolution	= omapdss_default_get_resolution,
223 };
224 
225 
226 static int nec_8048_probe_of(struct spi_device *spi)
227 {
228 	struct device_node *node = spi->dev.of_node;
229 	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
230 	struct omap_dss_device *in;
231 	int gpio;
232 
233 	gpio = of_get_named_gpio(node, "reset-gpios", 0);
234 	if (!gpio_is_valid(gpio)) {
235 		dev_err(&spi->dev, "failed to parse enable gpio\n");
236 		return gpio;
237 	}
238 	ddata->res_gpio = gpio;
239 
240 	/* XXX the panel spec doesn't mention any QVGA pin?? */
241 	ddata->qvga_gpio = -ENOENT;
242 
243 	in = omapdss_of_find_source_for_first_ep(node);
244 	if (IS_ERR(in)) {
245 		dev_err(&spi->dev, "failed to find video source\n");
246 		return PTR_ERR(in);
247 	}
248 
249 	ddata->in = in;
250 
251 	return 0;
252 }
253 
254 static int nec_8048_probe(struct spi_device *spi)
255 {
256 	struct panel_drv_data *ddata;
257 	struct omap_dss_device *dssdev;
258 	int r;
259 
260 	dev_dbg(&spi->dev, "%s\n", __func__);
261 
262 	if (!spi->dev.of_node)
263 		return -ENODEV;
264 
265 	spi->mode = SPI_MODE_0;
266 	spi->bits_per_word = 32;
267 
268 	r = spi_setup(spi);
269 	if (r < 0) {
270 		dev_err(&spi->dev, "spi_setup failed: %d\n", r);
271 		return r;
272 	}
273 
274 	init_nec_8048_wvga_lcd(spi);
275 
276 	ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
277 	if (ddata == NULL)
278 		return -ENOMEM;
279 
280 	dev_set_drvdata(&spi->dev, ddata);
281 
282 	ddata->spi = spi;
283 
284 	r = nec_8048_probe_of(spi);
285 	if (r)
286 		return r;
287 
288 	if (gpio_is_valid(ddata->qvga_gpio)) {
289 		r = devm_gpio_request_one(&spi->dev, ddata->qvga_gpio,
290 				GPIOF_OUT_INIT_HIGH, "lcd QVGA");
291 		if (r)
292 			goto err_gpio;
293 	}
294 
295 	if (gpio_is_valid(ddata->res_gpio)) {
296 		r = devm_gpio_request_one(&spi->dev, ddata->res_gpio,
297 				GPIOF_OUT_INIT_LOW, "lcd RES");
298 		if (r)
299 			goto err_gpio;
300 	}
301 
302 	ddata->videomode = nec_8048_panel_timings;
303 
304 	dssdev = &ddata->dssdev;
305 	dssdev->dev = &spi->dev;
306 	dssdev->driver = &nec_8048_ops;
307 	dssdev->type = OMAP_DISPLAY_TYPE_DPI;
308 	dssdev->owner = THIS_MODULE;
309 	dssdev->panel.timings = ddata->videomode;
310 
311 	r = omapdss_register_display(dssdev);
312 	if (r) {
313 		dev_err(&spi->dev, "Failed to register panel\n");
314 		goto err_reg;
315 	}
316 
317 	return 0;
318 
319 err_reg:
320 err_gpio:
321 	omap_dss_put_device(ddata->in);
322 	return r;
323 }
324 
325 static void nec_8048_remove(struct spi_device *spi)
326 {
327 	struct panel_drv_data *ddata = dev_get_drvdata(&spi->dev);
328 	struct omap_dss_device *dssdev = &ddata->dssdev;
329 	struct omap_dss_device *in = ddata->in;
330 
331 	dev_dbg(&ddata->spi->dev, "%s\n", __func__);
332 
333 	omapdss_unregister_display(dssdev);
334 
335 	nec_8048_disable(dssdev);
336 	nec_8048_disconnect(dssdev);
337 
338 	omap_dss_put_device(in);
339 }
340 
341 #ifdef CONFIG_PM_SLEEP
342 static int nec_8048_suspend(struct device *dev)
343 {
344 	struct spi_device *spi = to_spi_device(dev);
345 
346 	nec_8048_spi_send(spi, 2, 0x01);
347 	mdelay(40);
348 
349 	return 0;
350 }
351 
352 static int nec_8048_resume(struct device *dev)
353 {
354 	struct spi_device *spi = to_spi_device(dev);
355 
356 	/* reinitialize the panel */
357 	spi_setup(spi);
358 	nec_8048_spi_send(spi, 2, 0x00);
359 	init_nec_8048_wvga_lcd(spi);
360 
361 	return 0;
362 }
363 static SIMPLE_DEV_PM_OPS(nec_8048_pm_ops, nec_8048_suspend,
364 		nec_8048_resume);
365 #define NEC_8048_PM_OPS (&nec_8048_pm_ops)
366 #else
367 #define NEC_8048_PM_OPS NULL
368 #endif
369 
370 static const struct of_device_id nec_8048_of_match[] = {
371 	{ .compatible = "omapdss,nec,nl8048hl11", },
372 	{},
373 };
374 
375 MODULE_DEVICE_TABLE(of, nec_8048_of_match);
376 
377 static struct spi_driver nec_8048_driver = {
378 	.driver = {
379 		.name	= "panel-nec-nl8048hl11",
380 		.pm	= NEC_8048_PM_OPS,
381 		.of_match_table = nec_8048_of_match,
382 		.suppress_bind_attrs = true,
383 	},
384 	.probe	= nec_8048_probe,
385 	.remove	= nec_8048_remove,
386 };
387 
388 module_spi_driver(nec_8048_driver);
389 
390 MODULE_ALIAS("spi:nec,nl8048hl11");
391 MODULE_AUTHOR("Erik Gilling <konkers@android.com>");
392 MODULE_DESCRIPTION("NEC-NL8048HL11 Driver");
393 MODULE_LICENSE("GPL");
394