1 // SPDX-License-Identifier: GPL-2.0
3 * Orisetech OTA5601A TFT LCD panel driver
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>
15 #include <linux/of_device.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
20 #include <drm/drm_modes.h>
21 #include <drm/drm_panel.h>
23 #define OTA5601A_CTL 0x01
24 #define OTA5601A_CTL_OFF 0x00
25 #define OTA5601A_CTL_ON BIT(0)
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;
35 struct drm_panel drm_panel;
37 struct regulator *supply;
38 const struct ota5601a_panel_info *panel_info;
40 struct gpio_desc *reset_gpio;
43 static inline struct ota5601a *to_ota5601a(struct drm_panel *panel)
45 return container_of(panel, struct ota5601a, drm_panel);
48 static const struct reg_sequence ota5601a_panel_regs[] = {
49 { 0xfd, 0x00 }, /* Page Shift */
50 { 0x02, 0x00 }, /* Reset */
52 { 0x18, 0x00 }, /* Interface Sel: RGB 24 Bits */
53 { 0x34, 0x20 }, /* Undocumented */
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 */
63 { 0x16, 0x01 }, /* NTSC Sel */
64 { 0x19, 0x8d }, /* VBLK */
65 { 0x1a, 0x28 }, /* HBLK */
66 { 0x1c, 0x00 }, /* Scan Shift Dir. */
68 { 0xfd, 0xc5 }, /* Page Shift */
69 { 0x82, 0x0c }, /* PWR_CTRL Pump */
70 { 0xa2, 0xb4 }, /* PWR_CTRL VGH/VGL */
72 { 0xfd, 0xc4 }, /* Page Shift - What follows is listed as "RGB 24bit Timing Set" */
104 { 0xfd, 0x00 }, /* Page Shift */
107 static const struct regmap_config ota5601a_regmap_config = {
112 static int ota5601a_prepare(struct drm_panel *drm_panel)
114 struct ota5601a *panel = to_ota5601a(drm_panel);
117 err = regulator_enable(panel->supply);
119 dev_err(drm_panel->dev, "Failed to enable power supply: %d\n", err);
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);
129 /* Init all registers. */
130 err = regmap_multi_reg_write(panel->map, ota5601a_panel_regs,
131 ARRAY_SIZE(ota5601a_panel_regs));
133 dev_err(drm_panel->dev, "Failed to init registers: %d\n", err);
134 goto err_disable_regulator;
141 err_disable_regulator:
142 regulator_disable(panel->supply);
146 static int ota5601a_unprepare(struct drm_panel *drm_panel)
148 struct ota5601a *panel = to_ota5601a(drm_panel);
150 gpiod_set_value_cansleep(panel->reset_gpio, 1);
152 regulator_disable(panel->supply);
157 static int ota5601a_enable(struct drm_panel *drm_panel)
159 struct ota5601a *panel = to_ota5601a(drm_panel);
162 err = regmap_write(panel->map, OTA5601A_CTL, OTA5601A_CTL_ON);
165 dev_err(drm_panel->dev, "Unable to enable panel: %d\n", err);
169 if (drm_panel->backlight) {
170 /* Wait for the picture to be ready before enabling backlight */
177 static int ota5601a_disable(struct drm_panel *drm_panel)
179 struct ota5601a *panel = to_ota5601a(drm_panel);
182 err = regmap_write(panel->map, OTA5601A_CTL, OTA5601A_CTL_OFF);
185 dev_err(drm_panel->dev, "Unable to disable panel: %d\n", err);
192 static int ota5601a_get_modes(struct drm_panel *drm_panel,
193 struct drm_connector *connector)
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;
200 for (i = 0; i < panel_info->num_modes; i++) {
201 mode = drm_mode_duplicate(connector->dev,
202 &panel_info->display_modes[i]);
206 drm_mode_set_name(mode);
208 mode->type = DRM_MODE_TYPE_DRIVER;
209 if (panel_info->num_modes == 1)
210 mode->type |= DRM_MODE_TYPE_PREFERRED;
212 drm_mode_probed_add(connector, mode);
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;
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;
223 return panel_info->num_modes;
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,
234 static int ota5601a_probe(struct spi_device *spi)
236 const struct spi_device_id *id = spi_get_device_id(spi);
237 struct device *dev = &spi->dev;
238 struct ota5601a *panel;
241 panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
245 spi_set_drvdata(spi, panel);
247 panel->panel_info = (const struct ota5601a_panel_info *)id->driver_data;
248 if (!panel->panel_info)
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);
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);
263 spi->bits_per_word = 8;
264 spi->mode = SPI_MODE_3 | SPI_3WIRE;
265 err = spi_setup(spi);
267 dev_err(dev, "Failed to setup SPI\n");
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);
277 drm_panel_init(&panel->drm_panel, dev, &ota5601a_funcs,
278 DRM_MODE_CONNECTOR_DPI);
280 err = drm_panel_of_backlight(&panel->drm_panel);
282 if (err != -EPROBE_DEFER)
283 dev_err(dev, "Failed to get backlight handle\n");
287 drm_panel_add(&panel->drm_panel);
292 static void ota5601a_remove(struct spi_device *spi)
294 struct ota5601a *panel = spi_get_drvdata(spi);
296 drm_panel_remove(&panel->drm_panel);
298 ota5601a_disable(&panel->drm_panel);
299 ota5601a_unprepare(&panel->drm_panel);
302 static const struct drm_display_mode gpt3_display_modes[] = {
306 .hsync_start = 640 + 220,
307 .hsync_end = 640 + 220 + 20,
308 .htotal = 640 + 220 + 20 + 20,
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,
319 .hsync_start = 640 + 280,
320 .hsync_end = 640 + 280 + 20,
321 .htotal = 640 + 280 + 20 + 20,
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,
330 static const struct ota5601a_panel_info gpt3_info = {
331 .display_modes = gpt3_display_modes,
332 .num_modes = ARRAY_SIZE(gpt3_display_modes),
335 .bus_format = MEDIA_BUS_FMT_RGB888_1X24,
336 .bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE,
339 static const struct spi_device_id gpt3_id[] = {
340 { "gpt3", (kernel_ulong_t)&gpt3_info },
343 MODULE_DEVICE_TABLE(spi, gpt3_id);
345 static const struct of_device_id ota5601a_of_match[] = {
346 { .compatible = "focaltech,gpt3" },
349 MODULE_DEVICE_TABLE(of, ota5601a_of_match);
351 static struct spi_driver ota5601a_driver = {
354 .of_match_table = ota5601a_of_match,
357 .probe = ota5601a_probe,
358 .remove = ota5601a_remove,
361 module_spi_driver(ota5601a_driver);
364 MODULE_LICENSE("GPL");