1 // SPDX-License-Identifier: GPL-2.0
3 * Innolux/Chimei EJ030NA TFT LCD panel driver
5 * Copyright (C) 2020, Paul Cercueil <paul@crapouillou.net>
6 * Copyright (C) 2020, Christophe Branchereau <cbranchereau@gmail.com>
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_device.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/spi/spi.h>
19 #include <drm/drm_modes.h>
20 #include <drm/drm_panel.h>
23 const struct drm_display_mode *display_modes;
24 unsigned int num_modes;
25 u16 width_mm, height_mm;
26 u32 bus_format, bus_flags;
30 struct drm_panel panel;
31 struct spi_device *spi;
34 const struct ej030na_info *panel_info;
36 struct regulator *supply;
37 struct gpio_desc *reset_gpio;
40 static inline struct ej030na *to_ej030na(struct drm_panel *panel)
42 return container_of(panel, struct ej030na, panel);
45 static const struct reg_sequence ej030na_init_sequence[] = {
85 static int ej030na_prepare(struct drm_panel *panel)
87 struct ej030na *priv = to_ej030na(panel);
88 struct device *dev = &priv->spi->dev;
91 err = regulator_enable(priv->supply);
93 dev_err(dev, "Failed to enable power supply: %d\n", err);
98 gpiod_set_value_cansleep(priv->reset_gpio, 1);
99 usleep_range(50, 150);
100 gpiod_set_value_cansleep(priv->reset_gpio, 0);
101 usleep_range(50, 150);
103 err = regmap_multi_reg_write(priv->map, ej030na_init_sequence,
104 ARRAY_SIZE(ej030na_init_sequence));
106 dev_err(dev, "Failed to init registers: %d\n", err);
107 goto err_disable_regulator;
112 err_disable_regulator:
113 regulator_disable(priv->supply);
117 static int ej030na_unprepare(struct drm_panel *panel)
119 struct ej030na *priv = to_ej030na(panel);
121 gpiod_set_value_cansleep(priv->reset_gpio, 1);
122 regulator_disable(priv->supply);
127 static int ej030na_enable(struct drm_panel *panel)
129 struct ej030na *priv = to_ej030na(panel);
132 regmap_write(priv->map, 0x2b, 0x01);
134 if (panel->backlight) {
135 /* Wait for the picture to be ready before enabling backlight */
142 static int ej030na_disable(struct drm_panel *panel)
144 struct ej030na *priv = to_ej030na(panel);
147 regmap_write(priv->map, 0x2b, 0x00);
152 static int ej030na_get_modes(struct drm_panel *panel,
153 struct drm_connector *connector)
155 struct ej030na *priv = to_ej030na(panel);
156 const struct ej030na_info *panel_info = priv->panel_info;
157 struct drm_display_mode *mode;
160 for (i = 0; i < panel_info->num_modes; i++) {
161 mode = drm_mode_duplicate(connector->dev,
162 &panel_info->display_modes[i]);
166 drm_mode_set_name(mode);
168 mode->type = DRM_MODE_TYPE_DRIVER;
169 if (panel_info->num_modes == 1)
170 mode->type |= DRM_MODE_TYPE_PREFERRED;
172 drm_mode_probed_add(connector, mode);
175 connector->display_info.bpc = 8;
176 connector->display_info.width_mm = panel_info->width_mm;
177 connector->display_info.height_mm = panel_info->height_mm;
179 drm_display_info_set_bus_formats(&connector->display_info,
180 &panel_info->bus_format, 1);
181 connector->display_info.bus_flags = panel_info->bus_flags;
183 return panel_info->num_modes;
186 static const struct drm_panel_funcs ej030na_funcs = {
187 .prepare = ej030na_prepare,
188 .unprepare = ej030na_unprepare,
189 .enable = ej030na_enable,
190 .disable = ej030na_disable,
191 .get_modes = ej030na_get_modes,
194 static const struct regmap_config ej030na_regmap_config = {
197 .max_register = 0x5a,
200 static int ej030na_probe(struct spi_device *spi)
202 struct device *dev = &spi->dev;
203 struct ej030na *priv;
206 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
211 spi_set_drvdata(spi, priv);
213 priv->map = devm_regmap_init_spi(spi, &ej030na_regmap_config);
214 if (IS_ERR(priv->map)) {
215 dev_err(dev, "Unable to init regmap\n");
216 return PTR_ERR(priv->map);
219 priv->panel_info = of_device_get_match_data(dev);
220 if (!priv->panel_info)
223 priv->supply = devm_regulator_get(dev, "power");
224 if (IS_ERR(priv->supply))
225 return dev_err_probe(dev, PTR_ERR(priv->supply),
226 "Failed to get power supply\n");
228 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
229 if (IS_ERR(priv->reset_gpio))
230 return dev_err_probe(dev, PTR_ERR(priv->reset_gpio),
231 "Failed to get reset GPIO\n");
233 drm_panel_init(&priv->panel, dev, &ej030na_funcs,
234 DRM_MODE_CONNECTOR_DPI);
236 err = drm_panel_of_backlight(&priv->panel);
240 drm_panel_add(&priv->panel);
245 static void ej030na_remove(struct spi_device *spi)
247 struct ej030na *priv = spi_get_drvdata(spi);
249 drm_panel_remove(&priv->panel);
250 drm_panel_disable(&priv->panel);
251 drm_panel_unprepare(&priv->panel);
254 static const struct drm_display_mode ej030na_modes[] = {
258 .hsync_start = 320 + 10,
259 .hsync_end = 320 + 10 + 37,
260 .htotal = 320 + 10 + 37 + 33,
262 .vsync_start = 480 + 102,
263 .vsync_end = 480 + 102 + 9 + 9,
264 .vtotal = 480 + 102 + 9 + 9,
265 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
270 .hsync_start = 320 + 10,
271 .hsync_end = 320 + 10 + 37,
272 .htotal = 320 + 10 + 37 + 33,
274 .vsync_start = 480 + 102,
275 .vsync_end = 480 + 102 + 9,
276 .vtotal = 480 + 102 + 9 + 9,
277 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
281 static const struct ej030na_info ej030na_info = {
282 .display_modes = ej030na_modes,
283 .num_modes = ARRAY_SIZE(ej030na_modes),
286 .bus_format = MEDIA_BUS_FMT_RGB888_3X8_DELTA,
287 .bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE | DRM_BUS_FLAG_DE_LOW,
290 static const struct of_device_id ej030na_of_match[] = {
291 { .compatible = "innolux,ej030na", .data = &ej030na_info },
294 MODULE_DEVICE_TABLE(of, ej030na_of_match);
296 static struct spi_driver ej030na_driver = {
298 .name = "panel-innolux-ej030na",
299 .of_match_table = ej030na_of_match,
301 .probe = ej030na_probe,
302 .remove = ej030na_remove,
304 module_spi_driver(ej030na_driver);
306 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
307 MODULE_AUTHOR("Christophe Branchereau <cbranchereau@gmail.com>");
308 MODULE_LICENSE("GPL v2");