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[] = {
87 static int ej030na_prepare(struct drm_panel *panel)
89 struct ej030na *priv = to_ej030na(panel);
90 struct device *dev = &priv->spi->dev;
93 err = regulator_enable(priv->supply);
95 dev_err(dev, "Failed to enable power supply: %d\n", err);
100 gpiod_set_value_cansleep(priv->reset_gpio, 1);
101 usleep_range(50, 150);
102 gpiod_set_value_cansleep(priv->reset_gpio, 0);
103 usleep_range(50, 150);
105 err = regmap_multi_reg_write(priv->map, ej030na_init_sequence,
106 ARRAY_SIZE(ej030na_init_sequence));
108 dev_err(dev, "Failed to init registers: %d\n", err);
109 goto err_disable_regulator;
116 err_disable_regulator:
117 regulator_disable(priv->supply);
121 static int ej030na_unprepare(struct drm_panel *panel)
123 struct ej030na *priv = to_ej030na(panel);
125 gpiod_set_value_cansleep(priv->reset_gpio, 1);
126 regulator_disable(priv->supply);
131 static int ej030na_get_modes(struct drm_panel *panel,
132 struct drm_connector *connector)
134 struct ej030na *priv = to_ej030na(panel);
135 const struct ej030na_info *panel_info = priv->panel_info;
136 struct drm_display_mode *mode;
139 for (i = 0; i < panel_info->num_modes; i++) {
140 mode = drm_mode_duplicate(connector->dev,
141 &panel_info->display_modes[i]);
145 drm_mode_set_name(mode);
147 mode->type = DRM_MODE_TYPE_DRIVER;
148 if (panel_info->num_modes == 1)
149 mode->type |= DRM_MODE_TYPE_PREFERRED;
151 drm_mode_probed_add(connector, mode);
154 connector->display_info.bpc = 8;
155 connector->display_info.width_mm = panel_info->width_mm;
156 connector->display_info.height_mm = panel_info->height_mm;
158 drm_display_info_set_bus_formats(&connector->display_info,
159 &panel_info->bus_format, 1);
160 connector->display_info.bus_flags = panel_info->bus_flags;
162 return panel_info->num_modes;
165 static const struct drm_panel_funcs ej030na_funcs = {
166 .prepare = ej030na_prepare,
167 .unprepare = ej030na_unprepare,
168 .get_modes = ej030na_get_modes,
171 static const struct regmap_config ej030na_regmap_config = {
174 .max_register = 0x5a,
177 static int ej030na_probe(struct spi_device *spi)
179 struct device *dev = &spi->dev;
180 struct ej030na *priv;
183 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
188 spi_set_drvdata(spi, priv);
190 priv->map = devm_regmap_init_spi(spi, &ej030na_regmap_config);
191 if (IS_ERR(priv->map)) {
192 dev_err(dev, "Unable to init regmap\n");
193 return PTR_ERR(priv->map);
196 priv->panel_info = of_device_get_match_data(dev);
197 if (!priv->panel_info)
200 priv->supply = devm_regulator_get(dev, "power");
201 if (IS_ERR(priv->supply)) {
202 dev_err(dev, "Failed to get power supply\n");
203 return PTR_ERR(priv->supply);
206 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
207 if (IS_ERR(priv->reset_gpio)) {
208 dev_err(dev, "Failed to get reset GPIO\n");
209 return PTR_ERR(priv->reset_gpio);
212 drm_panel_init(&priv->panel, dev, &ej030na_funcs,
213 DRM_MODE_CONNECTOR_DPI);
215 err = drm_panel_of_backlight(&priv->panel);
219 drm_panel_add(&priv->panel);
224 static int ej030na_remove(struct spi_device *spi)
226 struct ej030na *priv = spi_get_drvdata(spi);
228 drm_panel_remove(&priv->panel);
229 drm_panel_disable(&priv->panel);
230 drm_panel_unprepare(&priv->panel);
235 static const struct drm_display_mode ej030na_modes[] = {
239 .hsync_start = 320 + 10,
240 .hsync_end = 320 + 10 + 37,
241 .htotal = 320 + 10 + 37 + 33,
243 .vsync_start = 480 + 102,
244 .vsync_end = 480 + 102 + 9 + 9,
245 .vtotal = 480 + 102 + 9 + 9,
246 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
251 .hsync_start = 320 + 10,
252 .hsync_end = 320 + 10 + 37,
253 .htotal = 320 + 10 + 37 + 33,
255 .vsync_start = 480 + 102,
256 .vsync_end = 480 + 102 + 9,
257 .vtotal = 480 + 102 + 9 + 9,
258 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
262 static const struct ej030na_info ej030na_info = {
263 .display_modes = ej030na_modes,
264 .num_modes = ARRAY_SIZE(ej030na_modes),
267 .bus_format = MEDIA_BUS_FMT_RGB888_3X8_DELTA,
268 .bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE | DRM_BUS_FLAG_DE_LOW,
271 static const struct of_device_id ej030na_of_match[] = {
272 { .compatible = "innolux,ej030na", .data = &ej030na_info },
275 MODULE_DEVICE_TABLE(of, ej030na_of_match);
277 static struct spi_driver ej030na_driver = {
279 .name = "panel-innolux-ej030na",
280 .of_match_table = ej030na_of_match,
282 .probe = ej030na_probe,
283 .remove = ej030na_remove,
285 module_spi_driver(ej030na_driver);
287 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
288 MODULE_AUTHOR("Christophe Branchereau <cbranchereau@gmail.com>");
289 MODULE_LICENSE("GPL v2");