1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Orisetech OTA5601A TFT LCD panel driver
4 *
5 * Copyright (C) 2021, Christophe Branchereau <cbranchereau@gmail.com>
6 */
7
8 #include <linux/bits.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/media-bus-format.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
19
20 #include <drm/drm_modes.h>
21 #include <drm/drm_panel.h>
22
23 #define OTA5601A_CTL 0x01
24 #define OTA5601A_CTL_OFF 0x00
25 #define OTA5601A_CTL_ON BIT(0)
26
27 struct ota5601a_panel_info {
28 const struct drm_display_mode *display_modes;
29 unsigned int num_modes;
30 u16 width_mm, height_mm;
31 u32 bus_format, bus_flags;
32 };
33
34 struct ota5601a {
35 struct drm_panel drm_panel;
36 struct regmap *map;
37 struct regulator *supply;
38 const struct ota5601a_panel_info *panel_info;
39
40 struct gpio_desc *reset_gpio;
41 };
42
to_ota5601a(struct drm_panel * panel)43 static inline struct ota5601a *to_ota5601a(struct drm_panel *panel)
44 {
45 return container_of(panel, struct ota5601a, drm_panel);
46 }
47
48 static const struct reg_sequence ota5601a_panel_regs[] = {
49 { 0xfd, 0x00 }, /* Page Shift */
50 { 0x02, 0x00 }, /* Reset */
51
52 { 0x18, 0x00 }, /* Interface Sel: RGB 24 Bits */
53 { 0x34, 0x20 }, /* Undocumented */
54
55 { 0x0c, 0x01 }, /* Contrast set by CMD1 == within page 0x00 */
56 { 0x0d, 0x48 }, /* R Brightness */
57 { 0x0e, 0x48 }, /* G Brightness */
58 { 0x0f, 0x48 }, /* B Brightness */
59 { 0x07, 0x40 }, /* R Contrast */
60 { 0x08, 0x33 }, /* G Contrast */
61 { 0x09, 0x3a }, /* B Contrast */
62
63 { 0x16, 0x01 }, /* NTSC Sel */
64 { 0x19, 0x8d }, /* VBLK */
65 { 0x1a, 0x28 }, /* HBLK */
66 { 0x1c, 0x00 }, /* Scan Shift Dir. */
67
68 { 0xfd, 0xc5 }, /* Page Shift */
69 { 0x82, 0x0c }, /* PWR_CTRL Pump */
70 { 0xa2, 0xb4 }, /* PWR_CTRL VGH/VGL */
71
72 { 0xfd, 0xc4 }, /* Page Shift - What follows is listed as "RGB 24bit Timing Set" */
73 { 0x82, 0x45 },
74
75 { 0xfd, 0xc1 },
76 { 0x91, 0x02 },
77
78 { 0xfd, 0xc0 },
79 { 0xa1, 0x01 },
80 { 0xa2, 0x1f },
81 { 0xa3, 0x0b },
82 { 0xa4, 0x38 },
83 { 0xa5, 0x00 },
84 { 0xa6, 0x0a },
85 { 0xa7, 0x38 },
86 { 0xa8, 0x00 },
87 { 0xa9, 0x0a },
88 { 0xaa, 0x37 },
89
90 { 0xfd, 0xce },
91 { 0x81, 0x18 },
92 { 0x82, 0x43 },
93 { 0x83, 0x43 },
94 { 0x91, 0x06 },
95 { 0x93, 0x38 },
96 { 0x94, 0x02 },
97 { 0x95, 0x06 },
98 { 0x97, 0x38 },
99 { 0x98, 0x02 },
100 { 0x99, 0x06 },
101 { 0x9b, 0x38 },
102 { 0x9c, 0x02 },
103
104 { 0xfd, 0x00 }, /* Page Shift */
105 };
106
107 static const struct regmap_config ota5601a_regmap_config = {
108 .reg_bits = 8,
109 .val_bits = 8,
110 };
111
ota5601a_prepare(struct drm_panel * drm_panel)112 static int ota5601a_prepare(struct drm_panel *drm_panel)
113 {
114 struct ota5601a *panel = to_ota5601a(drm_panel);
115 int err;
116
117 err = regulator_enable(panel->supply);
118 if (err) {
119 dev_err(drm_panel->dev, "Failed to enable power supply: %d\n", err);
120 return err;
121 }
122
123 /* Reset to be held low for 10us min according to the doc, 10ms before sending commands */
124 gpiod_set_value_cansleep(panel->reset_gpio, 1);
125 usleep_range(10, 30);
126 gpiod_set_value_cansleep(panel->reset_gpio, 0);
127 usleep_range(10000, 20000);
128
129 /* Init all registers. */
130 err = regmap_multi_reg_write(panel->map, ota5601a_panel_regs,
131 ARRAY_SIZE(ota5601a_panel_regs));
132 if (err) {
133 dev_err(drm_panel->dev, "Failed to init registers: %d\n", err);
134 goto err_disable_regulator;
135 }
136
137 msleep(120);
138
139 return 0;
140
141 err_disable_regulator:
142 regulator_disable(panel->supply);
143 return err;
144 }
145
ota5601a_unprepare(struct drm_panel * drm_panel)146 static int ota5601a_unprepare(struct drm_panel *drm_panel)
147 {
148 struct ota5601a *panel = to_ota5601a(drm_panel);
149
150 gpiod_set_value_cansleep(panel->reset_gpio, 1);
151
152 regulator_disable(panel->supply);
153
154 return 0;
155 }
156
ota5601a_enable(struct drm_panel * drm_panel)157 static int ota5601a_enable(struct drm_panel *drm_panel)
158 {
159 struct ota5601a *panel = to_ota5601a(drm_panel);
160 int err;
161
162 err = regmap_write(panel->map, OTA5601A_CTL, OTA5601A_CTL_ON);
163
164 if (err) {
165 dev_err(drm_panel->dev, "Unable to enable panel: %d\n", err);
166 return err;
167 }
168
169 if (drm_panel->backlight) {
170 /* Wait for the picture to be ready before enabling backlight */
171 msleep(120);
172 }
173
174 return 0;
175 }
176
ota5601a_disable(struct drm_panel * drm_panel)177 static int ota5601a_disable(struct drm_panel *drm_panel)
178 {
179 struct ota5601a *panel = to_ota5601a(drm_panel);
180 int err;
181
182 err = regmap_write(panel->map, OTA5601A_CTL, OTA5601A_CTL_OFF);
183
184 if (err) {
185 dev_err(drm_panel->dev, "Unable to disable panel: %d\n", err);
186 return err;
187 }
188
189 return 0;
190 }
191
ota5601a_get_modes(struct drm_panel * drm_panel,struct drm_connector * connector)192 static int ota5601a_get_modes(struct drm_panel *drm_panel,
193 struct drm_connector *connector)
194 {
195 struct ota5601a *panel = to_ota5601a(drm_panel);
196 const struct ota5601a_panel_info *panel_info = panel->panel_info;
197 struct drm_display_mode *mode;
198 unsigned int i;
199
200 for (i = 0; i < panel_info->num_modes; i++) {
201 mode = drm_mode_duplicate(connector->dev,
202 &panel_info->display_modes[i]);
203 if (!mode)
204 return -ENOMEM;
205
206 drm_mode_set_name(mode);
207
208 mode->type = DRM_MODE_TYPE_DRIVER;
209 if (panel_info->num_modes == 1)
210 mode->type |= DRM_MODE_TYPE_PREFERRED;
211
212 drm_mode_probed_add(connector, mode);
213 }
214
215 connector->display_info.bpc = 8;
216 connector->display_info.width_mm = panel_info->width_mm;
217 connector->display_info.height_mm = panel_info->height_mm;
218
219 drm_display_info_set_bus_formats(&connector->display_info,
220 &panel_info->bus_format, 1);
221 connector->display_info.bus_flags = panel_info->bus_flags;
222
223 return panel_info->num_modes;
224 }
225
226 static const struct drm_panel_funcs ota5601a_funcs = {
227 .prepare = ota5601a_prepare,
228 .unprepare = ota5601a_unprepare,
229 .enable = ota5601a_enable,
230 .disable = ota5601a_disable,
231 .get_modes = ota5601a_get_modes,
232 };
233
ota5601a_probe(struct spi_device * spi)234 static int ota5601a_probe(struct spi_device *spi)
235 {
236 const struct spi_device_id *id = spi_get_device_id(spi);
237 struct device *dev = &spi->dev;
238 struct ota5601a *panel;
239 int err;
240
241 panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
242 if (!panel)
243 return -ENOMEM;
244
245 spi_set_drvdata(spi, panel);
246
247 panel->panel_info = (const struct ota5601a_panel_info *)id->driver_data;
248 if (!panel->panel_info)
249 return -EINVAL;
250
251 panel->supply = devm_regulator_get(dev, "power");
252 if (IS_ERR(panel->supply)) {
253 dev_err(dev, "Failed to get power supply\n");
254 return PTR_ERR(panel->supply);
255 }
256
257 panel->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
258 if (IS_ERR(panel->reset_gpio)) {
259 dev_err(dev, "Failed to get reset GPIO\n");
260 return PTR_ERR(panel->reset_gpio);
261 }
262
263 spi->bits_per_word = 8;
264 spi->mode = SPI_MODE_3 | SPI_3WIRE;
265 err = spi_setup(spi);
266 if (err) {
267 dev_err(dev, "Failed to setup SPI\n");
268 return err;
269 }
270
271 panel->map = devm_regmap_init_spi(spi, &ota5601a_regmap_config);
272 if (IS_ERR(panel->map)) {
273 dev_err(dev, "Failed to init regmap\n");
274 return PTR_ERR(panel->map);
275 }
276
277 drm_panel_init(&panel->drm_panel, dev, &ota5601a_funcs,
278 DRM_MODE_CONNECTOR_DPI);
279
280 err = drm_panel_of_backlight(&panel->drm_panel);
281 if (err) {
282 if (err != -EPROBE_DEFER)
283 dev_err(dev, "Failed to get backlight handle\n");
284 return err;
285 }
286
287 drm_panel_add(&panel->drm_panel);
288
289 return 0;
290 }
291
ota5601a_remove(struct spi_device * spi)292 static void ota5601a_remove(struct spi_device *spi)
293 {
294 struct ota5601a *panel = spi_get_drvdata(spi);
295
296 drm_panel_remove(&panel->drm_panel);
297
298 ota5601a_disable(&panel->drm_panel);
299 ota5601a_unprepare(&panel->drm_panel);
300 }
301
302 static const struct drm_display_mode gpt3_display_modes[] = {
303 { /* 60 Hz */
304 .clock = 27000,
305 .hdisplay = 640,
306 .hsync_start = 640 + 220,
307 .hsync_end = 640 + 220 + 20,
308 .htotal = 640 + 220 + 20 + 20,
309 .vdisplay = 480,
310 .vsync_start = 480 + 7,
311 .vsync_end = 480 + 7 + 6,
312 .vtotal = 480 + 7 + 6 + 7,
313 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
314 },
315
316 { /* 50 Hz */
317 .clock = 24000,
318 .hdisplay = 640,
319 .hsync_start = 640 + 280,
320 .hsync_end = 640 + 280 + 20,
321 .htotal = 640 + 280 + 20 + 20,
322 .vdisplay = 480,
323 .vsync_start = 480 + 7,
324 .vsync_end = 480 + 7 + 6,
325 .vtotal = 480 + 7 + 6 + 7,
326 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
327 },
328 };
329
330 static const struct ota5601a_panel_info gpt3_info = {
331 .display_modes = gpt3_display_modes,
332 .num_modes = ARRAY_SIZE(gpt3_display_modes),
333 .width_mm = 71,
334 .height_mm = 51,
335 .bus_format = MEDIA_BUS_FMT_RGB888_1X24,
336 .bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
337 };
338
339 static const struct spi_device_id gpt3_id[] = {
340 { "gpt3", (kernel_ulong_t)&gpt3_info },
341 { /* sentinel */ }
342 };
343 MODULE_DEVICE_TABLE(spi, gpt3_id);
344
345 static const struct of_device_id ota5601a_of_match[] = {
346 { .compatible = "focaltech,gpt3" },
347 { /* sentinel */ }
348 };
349 MODULE_DEVICE_TABLE(of, ota5601a_of_match);
350
351 static struct spi_driver ota5601a_driver = {
352 .driver = {
353 .name = "ota5601a",
354 .of_match_table = ota5601a_of_match,
355 },
356 .id_table = gpt3_id,
357 .probe = ota5601a_probe,
358 .remove = ota5601a_remove,
359 };
360
361 module_spi_driver(ota5601a_driver);
362
363 MODULE_AUTHOR("Christophe Branchereau <cbranchereau@gmail.com>");
364 MODULE_LICENSE("GPL");
365