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