1 // SPDX-License-Identifier: GPL-2.0
3 * AU Optronics A030JTN01.0 TFT LCD panel driver
5 * Copyright (C) 2023, Paul Cercueil <paul@crapouillou.net>
6 * Copyright (C) 2023, Christophe Branchereau <cbranchereau@gmail.com>
9 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/media-bus-format.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.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>
27 #define REG05_STDBY BIT(0)
28 #define REG06_VBLK GENMASK(4, 0)
29 #define REG07_HBLK GENMASK(7, 0)
32 struct a030jtn01_info {
33 const struct drm_display_mode *display_modes;
34 unsigned int num_modes;
35 u16 width_mm, height_mm;
36 u32 bus_format, bus_flags;
40 struct drm_panel panel;
41 struct spi_device *spi;
44 const struct a030jtn01_info *panel_info;
46 struct regulator *supply;
47 struct gpio_desc *reset_gpio;
50 static inline struct a030jtn01 *to_a030jtn01(struct drm_panel *panel)
52 return container_of(panel, struct a030jtn01, panel);
55 static int a030jtn01_prepare(struct drm_panel *panel)
57 struct a030jtn01 *priv = to_a030jtn01(panel);
58 struct device *dev = &priv->spi->dev;
62 err = regulator_enable(priv->supply);
64 dev_err(dev, "Failed to enable power supply: %d\n", err);
68 usleep_range(1000, 8000);
71 gpiod_set_value_cansleep(priv->reset_gpio, 1);
72 usleep_range(100, 8000);
73 gpiod_set_value_cansleep(priv->reset_gpio, 0);
74 usleep_range(2000, 8000);
77 * No idea why, but a register read (doesn't matter which) is needed to
78 * properly initialize the chip after a reset; otherwise, the colors
79 * will be wrong. It doesn't seem to be timing-related as a msleep(200)
82 err = regmap_read(priv->map, REG05, &dummy);
84 goto err_disable_regulator;
86 /* Use (24 + 6) == 0x1e as the vertical back porch */
87 err = regmap_write(priv->map, REG06, FIELD_PREP(REG06_VBLK, 0x1e));
89 goto err_disable_regulator;
91 /* Use (42 + 30) * 3 == 0xd8 as the horizontal back porch */
92 err = regmap_write(priv->map, REG07, FIELD_PREP(REG07_HBLK, 0xd8));
94 goto err_disable_regulator;
98 err_disable_regulator:
99 gpiod_set_value_cansleep(priv->reset_gpio, 1);
100 regulator_disable(priv->supply);
104 static int a030jtn01_unprepare(struct drm_panel *panel)
106 struct a030jtn01 *priv = to_a030jtn01(panel);
108 gpiod_set_value_cansleep(priv->reset_gpio, 1);
109 regulator_disable(priv->supply);
114 static int a030jtn01_enable(struct drm_panel *panel)
116 struct a030jtn01 *priv = to_a030jtn01(panel);
119 ret = regmap_set_bits(priv->map, REG05, REG05_STDBY);
123 /* Wait for the picture to be stable */
124 if (panel->backlight)
130 static int a030jtn01_disable(struct drm_panel *panel)
132 struct a030jtn01 *priv = to_a030jtn01(panel);
134 return regmap_clear_bits(priv->map, REG05, REG05_STDBY);
137 static int a030jtn01_get_modes(struct drm_panel *panel,
138 struct drm_connector *connector)
140 struct a030jtn01 *priv = to_a030jtn01(panel);
141 const struct a030jtn01_info *panel_info = priv->panel_info;
142 struct drm_display_mode *mode;
145 for (i = 0; i < panel_info->num_modes; i++) {
146 mode = drm_mode_duplicate(connector->dev,
147 &panel_info->display_modes[i]);
151 drm_mode_set_name(mode);
153 mode->type = DRM_MODE_TYPE_DRIVER;
154 if (panel_info->num_modes == 1)
155 mode->type |= DRM_MODE_TYPE_PREFERRED;
157 drm_mode_probed_add(connector, mode);
160 connector->display_info.bpc = 8;
161 connector->display_info.width_mm = panel_info->width_mm;
162 connector->display_info.height_mm = panel_info->height_mm;
164 drm_display_info_set_bus_formats(&connector->display_info,
165 &panel_info->bus_format, 1);
166 connector->display_info.bus_flags = panel_info->bus_flags;
168 return panel_info->num_modes;
171 static const struct drm_panel_funcs a030jtn01_funcs = {
172 .prepare = a030jtn01_prepare,
173 .unprepare = a030jtn01_unprepare,
174 .enable = a030jtn01_enable,
175 .disable = a030jtn01_disable,
176 .get_modes = a030jtn01_get_modes,
179 static bool a030jtn01_has_reg(struct device *dev, unsigned int reg)
181 static const u32 a030jtn01_regs_mask = 0x001823f1fb;
183 return a030jtn01_regs_mask & BIT(reg);
186 static const struct regmap_config a030jtn01_regmap_config = {
189 .read_flag_mask = 0x40,
190 .max_register = 0x1c,
191 .readable_reg = a030jtn01_has_reg,
192 .writeable_reg = a030jtn01_has_reg,
195 static int a030jtn01_probe(struct spi_device *spi)
197 struct device *dev = &spi->dev;
198 struct a030jtn01 *priv;
201 spi->mode |= SPI_MODE_3 | SPI_3WIRE;
203 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
208 spi_set_drvdata(spi, priv);
210 priv->map = devm_regmap_init_spi(spi, &a030jtn01_regmap_config);
211 if (IS_ERR(priv->map))
212 return dev_err_probe(dev, PTR_ERR(priv->map), "Unable to init regmap");
214 priv->panel_info = spi_get_device_match_data(spi);
215 if (!priv->panel_info)
218 priv->supply = devm_regulator_get(dev, "power");
219 if (IS_ERR(priv->supply))
220 return dev_err_probe(dev, PTR_ERR(priv->supply), "Failed to get power supply");
222 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
223 if (IS_ERR(priv->reset_gpio))
224 return dev_err_probe(dev, PTR_ERR(priv->reset_gpio), "Failed to get reset GPIO");
226 drm_panel_init(&priv->panel, dev, &a030jtn01_funcs,
227 DRM_MODE_CONNECTOR_DPI);
229 err = drm_panel_of_backlight(&priv->panel);
233 drm_panel_add(&priv->panel);
238 static void a030jtn01_remove(struct spi_device *spi)
240 struct a030jtn01 *priv = spi_get_drvdata(spi);
242 drm_panel_remove(&priv->panel);
243 drm_panel_disable(&priv->panel);
244 drm_panel_unprepare(&priv->panel);
247 static const struct drm_display_mode a030jtn01_modes[] = {
251 .hsync_start = 320 + 8,
252 .hsync_end = 320 + 8 + 42,
253 .htotal = 320 + 8 + 42 + 30,
255 .vsync_start = 480 + 90,
256 .vsync_end = 480 + 90 + 24,
257 .vtotal = 480 + 90 + 24 + 6,
258 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
263 .hsync_start = 320 + 8,
264 .hsync_end = 320 + 8 + 42,
265 .htotal = 320 + 8 + 42 + 30,
267 .vsync_start = 480 + 90,
268 .vsync_end = 480 + 90 + 24,
269 .vtotal = 480 + 90 + 24 + 6,
270 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
274 static const struct a030jtn01_info a030jtn01_info = {
275 .display_modes = a030jtn01_modes,
276 .num_modes = ARRAY_SIZE(a030jtn01_modes),
279 .bus_format = MEDIA_BUS_FMT_RGB888_3X8_DELTA,
280 .bus_flags = DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE,
283 static const struct spi_device_id a030jtn01_id[] = {
284 { "a030jtn01", (kernel_ulong_t) &a030jtn01_info },
287 MODULE_DEVICE_TABLE(spi, a030jtn01_id);
289 static const struct of_device_id a030jtn01_of_match[] = {
290 { .compatible = "auo,a030jtn01" },
293 MODULE_DEVICE_TABLE(of, a030jtn01_of_match);
295 static struct spi_driver a030jtn01_driver = {
297 .name = "auo-a030jtn01",
298 .of_match_table = a030jtn01_of_match,
300 .id_table = a030jtn01_id,
301 .probe = a030jtn01_probe,
302 .remove = a030jtn01_remove,
304 module_spi_driver(a030jtn01_driver);
306 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
307 MODULE_AUTHOR("Christophe Branchereau <cbranchereau@gmail.com>");
308 MODULE_LICENSE("GPL");