1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * NEC NL8048HL11 Panel Driver
4  *
5  * Copyright (C) 2019 Texas Instruments Incorporated
6  *
7  * Based on the omapdrm-specific panel-nec-nl8048hl11 driver
8  *
9  * Copyright (C) 2010 Texas Instruments Incorporated
10  * Author: Erik Gilling <konkers@android.com>
11  */
12 
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/pm.h>
17 #include <linux/spi/spi.h>
18 
19 #include <drm/drm_connector.h>
20 #include <drm/drm_modes.h>
21 #include <drm/drm_panel.h>
22 
23 struct nl8048_panel {
24 	struct drm_panel panel;
25 
26 	struct spi_device *spi;
27 	struct gpio_desc *reset_gpio;
28 };
29 
30 #define to_nl8048_device(p) container_of(p, struct nl8048_panel, panel)
31 
32 static int nl8048_write(struct nl8048_panel *lcd, unsigned char addr,
33 			unsigned char value)
34 {
35 	u8 data[4] = { value, 0x01, addr, 0x00 };
36 	int ret;
37 
38 	ret = spi_write(lcd->spi, data, sizeof(data));
39 	if (ret)
40 		dev_err(&lcd->spi->dev, "SPI write to %u failed: %d\n",
41 			addr, ret);
42 
43 	return ret;
44 }
45 
46 static int nl8048_init(struct nl8048_panel *lcd)
47 {
48 	static const struct {
49 		unsigned char addr;
50 		unsigned char data;
51 	} nl8048_init_seq[] = {
52 		{   3, 0x01 }, {   0, 0x00 }, {   1, 0x01 }, {   4, 0x00 },
53 		{   5, 0x14 }, {   6, 0x24 }, {  16, 0xd7 }, {  17, 0x00 },
54 		{  18, 0x00 }, {  19, 0x55 }, {  20, 0x01 }, {  21, 0x70 },
55 		{  22, 0x1e }, {  23, 0x25 }, {  24, 0x25 }, {  25, 0x02 },
56 		{  26, 0x02 }, {  27, 0xa0 }, {  32, 0x2f }, {  33, 0x0f },
57 		{  34, 0x0f }, {  35, 0x0f }, {  36, 0x0f }, {  37, 0x0f },
58 		{  38, 0x0f }, {  39, 0x00 }, {  40, 0x02 }, {  41, 0x02 },
59 		{  42, 0x02 }, {  43, 0x0f }, {  44, 0x0f }, {  45, 0x0f },
60 		{  46, 0x0f }, {  47, 0x0f }, {  48, 0x0f }, {  49, 0x0f },
61 		{  50, 0x00 }, {  51, 0x02 }, {  52, 0x02 }, {  53, 0x02 },
62 		{  80, 0x0c }, {  83, 0x42 }, {  84, 0x42 }, {  85, 0x41 },
63 		{  86, 0x14 }, {  89, 0x88 }, {  90, 0x01 }, {  91, 0x00 },
64 		{  92, 0x02 }, {  93, 0x0c }, {  94, 0x1c }, {  95, 0x27 },
65 		{  98, 0x49 }, {  99, 0x27 }, { 102, 0x76 }, { 103, 0x27 },
66 		{ 112, 0x01 }, { 113, 0x0e }, { 114, 0x02 }, { 115, 0x0c },
67 		{ 118, 0x0c }, { 121, 0x30 }, { 130, 0x00 }, { 131, 0x00 },
68 		{ 132, 0xfc }, { 134, 0x00 }, { 136, 0x00 }, { 138, 0x00 },
69 		{ 139, 0x00 }, { 140, 0x00 }, { 141, 0xfc }, { 143, 0x00 },
70 		{ 145, 0x00 }, { 147, 0x00 }, { 148, 0x00 }, { 149, 0x00 },
71 		{ 150, 0xfc }, { 152, 0x00 }, { 154, 0x00 }, { 156, 0x00 },
72 		{ 157, 0x00 },
73 	};
74 
75 	unsigned int i;
76 	int ret;
77 
78 	for (i = 0; i < ARRAY_SIZE(nl8048_init_seq); ++i) {
79 		ret = nl8048_write(lcd, nl8048_init_seq[i].addr,
80 				   nl8048_init_seq[i].data);
81 		if (ret < 0)
82 			return ret;
83 	}
84 
85 	udelay(20);
86 
87 	return nl8048_write(lcd, 2, 0x00);
88 }
89 
90 static int nl8048_disable(struct drm_panel *panel)
91 {
92 	struct nl8048_panel *lcd = to_nl8048_device(panel);
93 
94 	gpiod_set_value_cansleep(lcd->reset_gpio, 0);
95 
96 	return 0;
97 }
98 
99 static int nl8048_enable(struct drm_panel *panel)
100 {
101 	struct nl8048_panel *lcd = to_nl8048_device(panel);
102 
103 	gpiod_set_value_cansleep(lcd->reset_gpio, 1);
104 
105 	return 0;
106 }
107 
108 static const struct drm_display_mode nl8048_mode = {
109 	/*  NEC PIX Clock Ratings MIN:21.8MHz TYP:23.8MHz MAX:25.7MHz */
110 	.clock	= 23800,
111 	.hdisplay = 800,
112 	.hsync_start = 800 + 6,
113 	.hsync_end = 800 + 6 + 1,
114 	.htotal = 800 + 6 + 1 + 4,
115 	.vdisplay = 480,
116 	.vsync_start = 480 + 3,
117 	.vsync_end = 480 + 3 + 1,
118 	.vtotal = 480 + 3 + 1 + 4,
119 	.vrefresh = 60,
120 	.type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
121 	.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
122 	.width_mm = 89,
123 	.height_mm = 53,
124 };
125 
126 static int nl8048_get_modes(struct drm_panel *panel)
127 {
128 	struct drm_connector *connector = panel->connector;
129 	struct drm_display_mode *mode;
130 
131 	mode = drm_mode_duplicate(panel->drm, &nl8048_mode);
132 	if (!mode)
133 		return -ENOMEM;
134 
135 	drm_mode_set_name(mode);
136 	drm_mode_probed_add(connector, mode);
137 
138 	connector->display_info.width_mm = nl8048_mode.width_mm;
139 	connector->display_info.height_mm = nl8048_mode.height_mm;
140 	connector->display_info.bus_flags = DRM_BUS_FLAG_DE_HIGH
141 					  | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE
142 					  | DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE;
143 
144 	return 1;
145 }
146 
147 static const struct drm_panel_funcs nl8048_funcs = {
148 	.disable = nl8048_disable,
149 	.enable = nl8048_enable,
150 	.get_modes = nl8048_get_modes,
151 };
152 
153 static int __maybe_unused nl8048_suspend(struct device *dev)
154 {
155 	struct nl8048_panel *lcd = dev_get_drvdata(dev);
156 
157 	nl8048_write(lcd, 2, 0x01);
158 	msleep(40);
159 
160 	return 0;
161 }
162 
163 static int __maybe_unused nl8048_resume(struct device *dev)
164 {
165 	struct nl8048_panel *lcd = dev_get_drvdata(dev);
166 
167 	/* Reinitialize the panel. */
168 	spi_setup(lcd->spi);
169 	nl8048_write(lcd, 2, 0x00);
170 	nl8048_init(lcd);
171 
172 	return 0;
173 }
174 
175 static SIMPLE_DEV_PM_OPS(nl8048_pm_ops, nl8048_suspend, nl8048_resume);
176 
177 static int nl8048_probe(struct spi_device *spi)
178 {
179 	struct nl8048_panel *lcd;
180 	int ret;
181 
182 	lcd = devm_kzalloc(&spi->dev, sizeof(*lcd), GFP_KERNEL);
183 	if (!lcd)
184 		return -ENOMEM;
185 
186 	spi_set_drvdata(spi, lcd);
187 	lcd->spi = spi;
188 
189 	lcd->reset_gpio = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_LOW);
190 	if (IS_ERR(lcd->reset_gpio)) {
191 		dev_err(&spi->dev, "failed to parse reset gpio\n");
192 		return PTR_ERR(lcd->reset_gpio);
193 	}
194 
195 	spi->mode = SPI_MODE_0;
196 	spi->bits_per_word = 32;
197 
198 	ret = spi_setup(spi);
199 	if (ret < 0) {
200 		dev_err(&spi->dev, "failed to setup SPI: %d\n", ret);
201 		return ret;
202 	}
203 
204 	ret = nl8048_init(lcd);
205 	if (ret < 0)
206 		return ret;
207 
208 	drm_panel_init(&lcd->panel);
209 	lcd->panel.dev = &lcd->spi->dev;
210 	lcd->panel.funcs = &nl8048_funcs;
211 
212 	return drm_panel_add(&lcd->panel);
213 }
214 
215 static int nl8048_remove(struct spi_device *spi)
216 {
217 	struct nl8048_panel *lcd = spi_get_drvdata(spi);
218 
219 	drm_panel_remove(&lcd->panel);
220 	drm_panel_disable(&lcd->panel);
221 	drm_panel_unprepare(&lcd->panel);
222 
223 	return 0;
224 }
225 
226 static const struct of_device_id nl8048_of_match[] = {
227 	{ .compatible = "nec,nl8048hl11", },
228 	{ /* sentinel */ },
229 };
230 
231 MODULE_DEVICE_TABLE(of, nl8048_of_match);
232 
233 static struct spi_driver nl8048_driver = {
234 	.probe		= nl8048_probe,
235 	.remove		= nl8048_remove,
236 	.driver		= {
237 		.name	= "panel-nec-nl8048hl11",
238 		.pm	= &nl8048_pm_ops,
239 		.of_match_table = nl8048_of_match,
240 	},
241 };
242 
243 module_spi_driver(nl8048_driver);
244 
245 MODULE_ALIAS("spi:nec,nl8048hl11");
246 MODULE_AUTHOR("Erik Gilling <konkers@android.com>");
247 MODULE_DESCRIPTION("NEC-NL8048HL11 Driver");
248 MODULE_LICENSE("GPL");
249