1 // SPDX-License-Identifier: GPL-2.0
3 * Innolux/Chimei EJ030NA TFT LCD panel driver
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/platform_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>
24 const struct drm_display_mode *display_modes;
25 unsigned int num_modes;
26 u16 width_mm, height_mm;
27 u32 bus_format, bus_flags;
31 struct drm_panel panel;
32 struct spi_device *spi;
35 const struct ej030na_info *panel_info;
37 struct regulator *supply;
38 struct gpio_desc *reset_gpio;
41 static inline struct ej030na *to_ej030na(struct drm_panel *panel)
43 return container_of(panel, struct ej030na, panel);
46 static const struct reg_sequence ej030na_init_sequence[] = {
86 static int ej030na_prepare(struct drm_panel *panel)
88 struct ej030na *priv = to_ej030na(panel);
89 struct device *dev = &priv->spi->dev;
92 err = regulator_enable(priv->supply);
94 dev_err(dev, "Failed to enable power supply: %d\n", err);
99 gpiod_set_value_cansleep(priv->reset_gpio, 1);
100 usleep_range(50, 150);
101 gpiod_set_value_cansleep(priv->reset_gpio, 0);
102 usleep_range(50, 150);
104 err = regmap_multi_reg_write(priv->map, ej030na_init_sequence,
105 ARRAY_SIZE(ej030na_init_sequence));
107 dev_err(dev, "Failed to init registers: %d\n", err);
108 goto err_disable_regulator;
113 err_disable_regulator:
114 regulator_disable(priv->supply);
118 static int ej030na_unprepare(struct drm_panel *panel)
120 struct ej030na *priv = to_ej030na(panel);
122 gpiod_set_value_cansleep(priv->reset_gpio, 1);
123 regulator_disable(priv->supply);
128 static int ej030na_enable(struct drm_panel *panel)
130 struct ej030na *priv = to_ej030na(panel);
133 regmap_write(priv->map, 0x2b, 0x01);
135 if (panel->backlight) {
136 /* Wait for the picture to be ready before enabling backlight */
143 static int ej030na_disable(struct drm_panel *panel)
145 struct ej030na *priv = to_ej030na(panel);
148 regmap_write(priv->map, 0x2b, 0x00);
153 static int ej030na_get_modes(struct drm_panel *panel,
154 struct drm_connector *connector)
156 struct ej030na *priv = to_ej030na(panel);
157 const struct ej030na_info *panel_info = priv->panel_info;
158 struct drm_display_mode *mode;
161 for (i = 0; i < panel_info->num_modes; i++) {
162 mode = drm_mode_duplicate(connector->dev,
163 &panel_info->display_modes[i]);
167 drm_mode_set_name(mode);
169 mode->type = DRM_MODE_TYPE_DRIVER;
170 if (panel_info->num_modes == 1)
171 mode->type |= DRM_MODE_TYPE_PREFERRED;
173 drm_mode_probed_add(connector, mode);
176 connector->display_info.bpc = 8;
177 connector->display_info.width_mm = panel_info->width_mm;
178 connector->display_info.height_mm = panel_info->height_mm;
180 drm_display_info_set_bus_formats(&connector->display_info,
181 &panel_info->bus_format, 1);
182 connector->display_info.bus_flags = panel_info->bus_flags;
184 return panel_info->num_modes;
187 static const struct drm_panel_funcs ej030na_funcs = {
188 .prepare = ej030na_prepare,
189 .unprepare = ej030na_unprepare,
190 .enable = ej030na_enable,
191 .disable = ej030na_disable,
192 .get_modes = ej030na_get_modes,
195 static const struct regmap_config ej030na_regmap_config = {
198 .max_register = 0x5a,
201 static int ej030na_probe(struct spi_device *spi)
203 struct device *dev = &spi->dev;
204 struct ej030na *priv;
207 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
212 spi_set_drvdata(spi, priv);
214 priv->map = devm_regmap_init_spi(spi, &ej030na_regmap_config);
215 if (IS_ERR(priv->map)) {
216 dev_err(dev, "Unable to init regmap\n");
217 return PTR_ERR(priv->map);
220 priv->panel_info = of_device_get_match_data(dev);
221 if (!priv->panel_info)
224 priv->supply = devm_regulator_get(dev, "power");
225 if (IS_ERR(priv->supply))
226 return dev_err_probe(dev, PTR_ERR(priv->supply),
227 "Failed to get power supply\n");
229 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
230 if (IS_ERR(priv->reset_gpio))
231 return dev_err_probe(dev, PTR_ERR(priv->reset_gpio),
232 "Failed to get reset GPIO\n");
234 drm_panel_init(&priv->panel, dev, &ej030na_funcs,
235 DRM_MODE_CONNECTOR_DPI);
237 err = drm_panel_of_backlight(&priv->panel);
241 drm_panel_add(&priv->panel);
246 static void ej030na_remove(struct spi_device *spi)
248 struct ej030na *priv = spi_get_drvdata(spi);
250 drm_panel_remove(&priv->panel);
251 drm_panel_disable(&priv->panel);
252 drm_panel_unprepare(&priv->panel);
255 static const struct drm_display_mode ej030na_modes[] = {
259 .hsync_start = 320 + 10,
260 .hsync_end = 320 + 10 + 37,
261 .htotal = 320 + 10 + 37 + 33,
263 .vsync_start = 480 + 102,
264 .vsync_end = 480 + 102 + 9 + 9,
265 .vtotal = 480 + 102 + 9 + 9,
266 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
271 .hsync_start = 320 + 10,
272 .hsync_end = 320 + 10 + 37,
273 .htotal = 320 + 10 + 37 + 33,
275 .vsync_start = 480 + 102,
276 .vsync_end = 480 + 102 + 9,
277 .vtotal = 480 + 102 + 9 + 9,
278 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
282 static const struct ej030na_info ej030na_info = {
283 .display_modes = ej030na_modes,
284 .num_modes = ARRAY_SIZE(ej030na_modes),
287 .bus_format = MEDIA_BUS_FMT_RGB888_3X8_DELTA,
288 .bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE | DRM_BUS_FLAG_DE_LOW,
291 static const struct of_device_id ej030na_of_match[] = {
292 { .compatible = "innolux,ej030na", .data = &ej030na_info },
295 MODULE_DEVICE_TABLE(of, ej030na_of_match);
297 static struct spi_driver ej030na_driver = {
299 .name = "panel-innolux-ej030na",
300 .of_match_table = ej030na_of_match,
302 .probe = ej030na_probe,
303 .remove = ej030na_remove,
305 module_spi_driver(ej030na_driver);
309 MODULE_DESCRIPTION("Innolux/Chimei EJ030NA TFT LCD panel driver");
310 MODULE_LICENSE("GPL v2");