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/module.h>
12 #include <linux/regulator/consumer.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_panel.h>
18 #include <video/display_timing.h>
19 #include <video/videomode.h>
21 struct seiko_panel_desc
{
22 const struct drm_display_mode
*modes
;
23 unsigned int num_modes
;
24 const struct display_timing
*timings
;
25 unsigned int num_timings
;
30 * @width: width (in millimeters) of the panel's active display area
31 * @height: height (in millimeters) of the panel's active display area
43 struct drm_panel base
;
46 const struct seiko_panel_desc
*desc
;
47 struct backlight_device
*backlight
;
48 struct regulator
*dvdd
;
49 struct regulator
*avdd
;
52 static inline struct seiko_panel
*to_seiko_panel(struct drm_panel
*panel
)
54 return container_of(panel
, struct seiko_panel
, base
);
57 static int seiko_panel_get_fixed_modes(struct seiko_panel
*panel
)
59 struct drm_connector
*connector
= panel
->base
.connector
;
60 struct drm_device
*drm
= panel
->base
.drm
;
61 struct drm_display_mode
*mode
;
62 unsigned int i
, num
= 0;
67 for (i
= 0; i
< panel
->desc
->num_timings
; i
++) {
68 const struct display_timing
*dt
= &panel
->desc
->timings
[i
];
71 videomode_from_timing(dt
, &vm
);
72 mode
= drm_mode_create(drm
);
74 dev_err(drm
->dev
, "failed to add mode %ux%u\n",
75 dt
->hactive
.typ
, dt
->vactive
.typ
);
79 drm_display_mode_from_videomode(&vm
, mode
);
81 mode
->type
|= DRM_MODE_TYPE_DRIVER
;
83 if (panel
->desc
->num_timings
== 1)
84 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
86 drm_mode_probed_add(connector
, mode
);
90 for (i
= 0; i
< panel
->desc
->num_modes
; i
++) {
91 const struct drm_display_mode
*m
= &panel
->desc
->modes
[i
];
93 mode
= drm_mode_duplicate(drm
, m
);
95 dev_err(drm
->dev
, "failed to add mode %ux%u@%u\n",
96 m
->hdisplay
, m
->vdisplay
, m
->vrefresh
);
100 mode
->type
|= DRM_MODE_TYPE_DRIVER
;
102 if (panel
->desc
->num_modes
== 1)
103 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
105 drm_mode_set_name(mode
);
107 drm_mode_probed_add(connector
, mode
);
111 connector
->display_info
.bpc
= panel
->desc
->bpc
;
112 connector
->display_info
.width_mm
= panel
->desc
->size
.width
;
113 connector
->display_info
.height_mm
= panel
->desc
->size
.height
;
114 if (panel
->desc
->bus_format
)
115 drm_display_info_set_bus_formats(&connector
->display_info
,
116 &panel
->desc
->bus_format
, 1);
117 connector
->display_info
.bus_flags
= panel
->desc
->bus_flags
;
122 static int seiko_panel_disable(struct drm_panel
*panel
)
124 struct seiko_panel
*p
= to_seiko_panel(panel
);
130 p
->backlight
->props
.power
= FB_BLANK_POWERDOWN
;
131 p
->backlight
->props
.state
|= BL_CORE_FBBLANK
;
132 backlight_update_status(p
->backlight
);
140 static int seiko_panel_unprepare(struct drm_panel
*panel
)
142 struct seiko_panel
*p
= to_seiko_panel(panel
);
147 regulator_disable(p
->avdd
);
149 /* Add a 100ms delay as per the panel datasheet */
152 regulator_disable(p
->dvdd
);
159 static int seiko_panel_prepare(struct drm_panel
*panel
)
161 struct seiko_panel
*p
= to_seiko_panel(panel
);
167 err
= regulator_enable(p
->dvdd
);
169 dev_err(panel
->dev
, "failed to enable dvdd: %d\n", err
);
173 /* Add a 100ms delay as per the panel datasheet */
176 err
= regulator_enable(p
->avdd
);
178 dev_err(panel
->dev
, "failed to enable avdd: %d\n", err
);
187 regulator_disable(p
->dvdd
);
191 static int seiko_panel_enable(struct drm_panel
*panel
)
193 struct seiko_panel
*p
= to_seiko_panel(panel
);
199 p
->backlight
->props
.state
&= ~BL_CORE_FBBLANK
;
200 p
->backlight
->props
.power
= FB_BLANK_UNBLANK
;
201 backlight_update_status(p
->backlight
);
209 static int seiko_panel_get_modes(struct drm_panel
*panel
)
211 struct seiko_panel
*p
= to_seiko_panel(panel
);
213 /* add hard-coded panel modes */
214 return seiko_panel_get_fixed_modes(p
);
217 static int seiko_panel_get_timings(struct drm_panel
*panel
,
218 unsigned int num_timings
,
219 struct display_timing
*timings
)
221 struct seiko_panel
*p
= to_seiko_panel(panel
);
224 if (p
->desc
->num_timings
< num_timings
)
225 num_timings
= p
->desc
->num_timings
;
228 for (i
= 0; i
< num_timings
; i
++)
229 timings
[i
] = p
->desc
->timings
[i
];
231 return p
->desc
->num_timings
;
234 static const struct drm_panel_funcs seiko_panel_funcs
= {
235 .disable
= seiko_panel_disable
,
236 .unprepare
= seiko_panel_unprepare
,
237 .prepare
= seiko_panel_prepare
,
238 .enable
= seiko_panel_enable
,
239 .get_modes
= seiko_panel_get_modes
,
240 .get_timings
= seiko_panel_get_timings
,
243 static int seiko_panel_probe(struct device
*dev
,
244 const struct seiko_panel_desc
*desc
)
246 struct device_node
*backlight
;
247 struct seiko_panel
*panel
;
250 panel
= devm_kzalloc(dev
, sizeof(*panel
), GFP_KERNEL
);
254 panel
->enabled
= false;
255 panel
->prepared
= false;
258 panel
->dvdd
= devm_regulator_get(dev
, "dvdd");
259 if (IS_ERR(panel
->dvdd
))
260 return PTR_ERR(panel
->dvdd
);
262 panel
->avdd
= devm_regulator_get(dev
, "avdd");
263 if (IS_ERR(panel
->avdd
))
264 return PTR_ERR(panel
->avdd
);
266 backlight
= of_parse_phandle(dev
->of_node
, "backlight", 0);
268 panel
->backlight
= of_find_backlight_by_node(backlight
);
269 of_node_put(backlight
);
271 if (!panel
->backlight
)
272 return -EPROBE_DEFER
;
275 drm_panel_init(&panel
->base
);
276 panel
->base
.dev
= dev
;
277 panel
->base
.funcs
= &seiko_panel_funcs
;
279 err
= drm_panel_add(&panel
->base
);
283 dev_set_drvdata(dev
, panel
);
288 static int seiko_panel_remove(struct platform_device
*pdev
)
290 struct seiko_panel
*panel
= dev_get_drvdata(&pdev
->dev
);
292 drm_panel_remove(&panel
->base
);
294 seiko_panel_disable(&panel
->base
);
296 if (panel
->backlight
)
297 put_device(&panel
->backlight
->dev
);
302 static void seiko_panel_shutdown(struct platform_device
*pdev
)
304 struct seiko_panel
*panel
= dev_get_drvdata(&pdev
->dev
);
306 seiko_panel_disable(&panel
->base
);
309 static const struct display_timing seiko_43wvf1g_timing
= {
310 .pixelclock
= { 33500000, 33500000, 33500000 },
311 .hactive
= { 800, 800, 800 },
312 .hfront_porch
= { 164, 164, 164 },
313 .hback_porch
= { 89, 89, 89 },
314 .hsync_len
= { 10, 10, 10 },
315 .vactive
= { 480, 480, 480 },
316 .vfront_porch
= { 10, 10, 10 },
317 .vback_porch
= { 23, 23, 23 },
318 .vsync_len
= { 10, 10, 10 },
319 .flags
= DISPLAY_FLAGS_DE_LOW
,
322 static const struct seiko_panel_desc seiko_43wvf1g
= {
323 .timings
= &seiko_43wvf1g_timing
,
330 .bus_format
= MEDIA_BUS_FMT_RGB888_1X24
,
331 .bus_flags
= DRM_BUS_FLAG_DE_HIGH
| DRM_BUS_FLAG_PIXDATA_NEGEDGE
,
334 static const struct of_device_id platform_of_match
[] = {
336 .compatible
= "sii,43wvf1g",
337 .data
= &seiko_43wvf1g
,
342 MODULE_DEVICE_TABLE(of
, platform_of_match
);
344 static int seiko_panel_platform_probe(struct platform_device
*pdev
)
346 const struct of_device_id
*id
;
348 id
= of_match_node(platform_of_match
, pdev
->dev
.of_node
);
352 return seiko_panel_probe(&pdev
->dev
, id
->data
);
355 static struct platform_driver seiko_panel_platform_driver
= {
357 .name
= "seiko_panel",
358 .of_match_table
= platform_of_match
,
360 .probe
= seiko_panel_platform_probe
,
361 .remove
= seiko_panel_remove
,
362 .shutdown
= seiko_panel_shutdown
,
364 module_platform_driver(seiko_panel_platform_driver
);
366 MODULE_AUTHOR("Marco Franchi <marco.franchi@nxp.com>");
367 MODULE_DESCRIPTION("Seiko 43WVF1G panel driver");
368 MODULE_LICENSE("GPL v2");