1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017 NXP Semiconductors.
4 * Author: Marco Franchi <marco.franchi@nxp.com>
6 * Based on Panel Simple driver by Thierry Reding <treding@nvidia.com>
9 #include <linux/backlight.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/consumer.h>
16 #include <video/display_timing.h>
17 #include <video/videomode.h>
19 #include <drm/drm_crtc.h>
20 #include <drm/drm_device.h>
21 #include <drm/drm_panel.h>
23 struct seiko_panel_desc {
24 const struct drm_display_mode *modes;
25 unsigned int num_modes;
26 const struct display_timing *timings;
27 unsigned int num_timings;
32 * @width: width (in millimeters) of the panel's active display area
33 * @height: height (in millimeters) of the panel's active display area
45 struct drm_panel base;
48 const struct seiko_panel_desc *desc;
49 struct backlight_device *backlight;
50 struct regulator *dvdd;
51 struct regulator *avdd;
54 static inline struct seiko_panel *to_seiko_panel(struct drm_panel *panel)
56 return container_of(panel, struct seiko_panel, base);
59 static int seiko_panel_get_fixed_modes(struct seiko_panel *panel,
60 struct drm_connector *connector)
62 struct drm_display_mode *mode;
63 unsigned int i, num = 0;
68 for (i = 0; i < panel->desc->num_timings; i++) {
69 const struct display_timing *dt = &panel->desc->timings[i];
72 videomode_from_timing(dt, &vm);
73 mode = drm_mode_create(connector->dev);
75 dev_err(panel->base.dev, "failed to add mode %ux%u\n",
76 dt->hactive.typ, dt->vactive.typ);
80 drm_display_mode_from_videomode(&vm, mode);
82 mode->type |= DRM_MODE_TYPE_DRIVER;
84 if (panel->desc->num_timings == 1)
85 mode->type |= DRM_MODE_TYPE_PREFERRED;
87 drm_mode_probed_add(connector, mode);
91 for (i = 0; i < panel->desc->num_modes; i++) {
92 const struct drm_display_mode *m = &panel->desc->modes[i];
94 mode = drm_mode_duplicate(connector->dev, m);
96 dev_err(panel->base.dev, "failed to add mode %ux%u@%u\n",
97 m->hdisplay, m->vdisplay, m->vrefresh);
101 mode->type |= DRM_MODE_TYPE_DRIVER;
103 if (panel->desc->num_modes == 1)
104 mode->type |= DRM_MODE_TYPE_PREFERRED;
106 drm_mode_set_name(mode);
108 drm_mode_probed_add(connector, mode);
112 connector->display_info.bpc = panel->desc->bpc;
113 connector->display_info.width_mm = panel->desc->size.width;
114 connector->display_info.height_mm = panel->desc->size.height;
115 if (panel->desc->bus_format)
116 drm_display_info_set_bus_formats(&connector->display_info,
117 &panel->desc->bus_format, 1);
118 connector->display_info.bus_flags = panel->desc->bus_flags;
123 static int seiko_panel_disable(struct drm_panel *panel)
125 struct seiko_panel *p = to_seiko_panel(panel);
131 p->backlight->props.power = FB_BLANK_POWERDOWN;
132 p->backlight->props.state |= BL_CORE_FBBLANK;
133 backlight_update_status(p->backlight);
141 static int seiko_panel_unprepare(struct drm_panel *panel)
143 struct seiko_panel *p = to_seiko_panel(panel);
148 regulator_disable(p->avdd);
150 /* Add a 100ms delay as per the panel datasheet */
153 regulator_disable(p->dvdd);
160 static int seiko_panel_prepare(struct drm_panel *panel)
162 struct seiko_panel *p = to_seiko_panel(panel);
168 err = regulator_enable(p->dvdd);
170 dev_err(panel->dev, "failed to enable dvdd: %d\n", err);
174 /* Add a 100ms delay as per the panel datasheet */
177 err = regulator_enable(p->avdd);
179 dev_err(panel->dev, "failed to enable avdd: %d\n", err);
188 regulator_disable(p->dvdd);
192 static int seiko_panel_enable(struct drm_panel *panel)
194 struct seiko_panel *p = to_seiko_panel(panel);
200 p->backlight->props.state &= ~BL_CORE_FBBLANK;
201 p->backlight->props.power = FB_BLANK_UNBLANK;
202 backlight_update_status(p->backlight);
210 static int seiko_panel_get_modes(struct drm_panel *panel,
211 struct drm_connector *connector)
213 struct seiko_panel *p = to_seiko_panel(panel);
215 /* add hard-coded panel modes */
216 return seiko_panel_get_fixed_modes(p, connector);
219 static int seiko_panel_get_timings(struct drm_panel *panel,
220 unsigned int num_timings,
221 struct display_timing *timings)
223 struct seiko_panel *p = to_seiko_panel(panel);
226 if (p->desc->num_timings < num_timings)
227 num_timings = p->desc->num_timings;
230 for (i = 0; i < num_timings; i++)
231 timings[i] = p->desc->timings[i];
233 return p->desc->num_timings;
236 static const struct drm_panel_funcs seiko_panel_funcs = {
237 .disable = seiko_panel_disable,
238 .unprepare = seiko_panel_unprepare,
239 .prepare = seiko_panel_prepare,
240 .enable = seiko_panel_enable,
241 .get_modes = seiko_panel_get_modes,
242 .get_timings = seiko_panel_get_timings,
245 static int seiko_panel_probe(struct device *dev,
246 const struct seiko_panel_desc *desc)
248 struct device_node *backlight;
249 struct seiko_panel *panel;
252 panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
256 panel->enabled = false;
257 panel->prepared = false;
260 panel->dvdd = devm_regulator_get(dev, "dvdd");
261 if (IS_ERR(panel->dvdd))
262 return PTR_ERR(panel->dvdd);
264 panel->avdd = devm_regulator_get(dev, "avdd");
265 if (IS_ERR(panel->avdd))
266 return PTR_ERR(panel->avdd);
268 backlight = of_parse_phandle(dev->of_node, "backlight", 0);
270 panel->backlight = of_find_backlight_by_node(backlight);
271 of_node_put(backlight);
273 if (!panel->backlight)
274 return -EPROBE_DEFER;
277 drm_panel_init(&panel->base, dev, &seiko_panel_funcs,
278 DRM_MODE_CONNECTOR_DPI);
280 err = drm_panel_add(&panel->base);
284 dev_set_drvdata(dev, panel);
289 static int seiko_panel_remove(struct platform_device *pdev)
291 struct seiko_panel *panel = dev_get_drvdata(&pdev->dev);
293 drm_panel_remove(&panel->base);
295 seiko_panel_disable(&panel->base);
297 if (panel->backlight)
298 put_device(&panel->backlight->dev);
303 static void seiko_panel_shutdown(struct platform_device *pdev)
305 struct seiko_panel *panel = dev_get_drvdata(&pdev->dev);
307 seiko_panel_disable(&panel->base);
310 static const struct display_timing seiko_43wvf1g_timing = {
311 .pixelclock = { 33500000, 33500000, 33500000 },
312 .hactive = { 800, 800, 800 },
313 .hfront_porch = { 164, 164, 164 },
314 .hback_porch = { 89, 89, 89 },
315 .hsync_len = { 10, 10, 10 },
316 .vactive = { 480, 480, 480 },
317 .vfront_porch = { 10, 10, 10 },
318 .vback_porch = { 23, 23, 23 },
319 .vsync_len = { 10, 10, 10 },
320 .flags = DISPLAY_FLAGS_DE_LOW,
323 static const struct seiko_panel_desc seiko_43wvf1g = {
324 .timings = &seiko_43wvf1g_timing,
331 .bus_format = MEDIA_BUS_FMT_RGB888_1X24,
332 .bus_flags = DRM_BUS_FLAG_DE_HIGH | DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE,
335 static const struct of_device_id platform_of_match[] = {
337 .compatible = "sii,43wvf1g",
338 .data = &seiko_43wvf1g,
343 MODULE_DEVICE_TABLE(of, platform_of_match);
345 static int seiko_panel_platform_probe(struct platform_device *pdev)
347 const struct of_device_id *id;
349 id = of_match_node(platform_of_match, pdev->dev.of_node);
353 return seiko_panel_probe(&pdev->dev, id->data);
356 static struct platform_driver seiko_panel_platform_driver = {
358 .name = "seiko_panel",
359 .of_match_table = platform_of_match,
361 .probe = seiko_panel_platform_probe,
362 .remove = seiko_panel_remove,
363 .shutdown = seiko_panel_shutdown,
365 module_platform_driver(seiko_panel_platform_driver);
367 MODULE_AUTHOR("Marco Franchi <marco.franchi@nxp.com>");
368 MODULE_DESCRIPTION("Seiko 43WVF1G panel driver");
369 MODULE_LICENSE("GPL v2");