2 * Copyright (C) 2017 NXP Semiconductors.
3 * Author: Marco Franchi <marco.franchi@nxp.com>
5 * Based on Panel Simple driver by Thierry Reding <treding@nvidia.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
12 #include <linux/backlight.h>
13 #include <linux/module.h>
15 #include <linux/regulator/consumer.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_panel.h>
21 #include <video/display_timing.h>
22 #include <video/videomode.h>
24 struct seiko_panel_desc
{
25 const struct drm_display_mode
*modes
;
26 unsigned int num_modes
;
27 const struct display_timing
*timings
;
28 unsigned int num_timings
;
33 * @width: width (in millimeters) of the panel's active display area
34 * @height: height (in millimeters) of the panel's active display area
46 struct drm_panel base
;
49 const struct seiko_panel_desc
*desc
;
50 struct backlight_device
*backlight
;
51 struct regulator
*dvdd
;
52 struct regulator
*avdd
;
55 static inline struct seiko_panel
*to_seiko_panel(struct drm_panel
*panel
)
57 return container_of(panel
, struct seiko_panel
, base
);
60 static int seiko_panel_get_fixed_modes(struct seiko_panel
*panel
)
62 struct drm_connector
*connector
= panel
->base
.connector
;
63 struct drm_device
*drm
= panel
->base
.drm
;
64 struct drm_display_mode
*mode
;
65 unsigned int i
, num
= 0;
70 for (i
= 0; i
< panel
->desc
->num_timings
; i
++) {
71 const struct display_timing
*dt
= &panel
->desc
->timings
[i
];
74 videomode_from_timing(dt
, &vm
);
75 mode
= drm_mode_create(drm
);
77 dev_err(drm
->dev
, "failed to add mode %ux%u\n",
78 dt
->hactive
.typ
, dt
->vactive
.typ
);
82 drm_display_mode_from_videomode(&vm
, mode
);
84 mode
->type
|= DRM_MODE_TYPE_DRIVER
;
86 if (panel
->desc
->num_timings
== 1)
87 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
89 drm_mode_probed_add(connector
, mode
);
93 for (i
= 0; i
< panel
->desc
->num_modes
; i
++) {
94 const struct drm_display_mode
*m
= &panel
->desc
->modes
[i
];
96 mode
= drm_mode_duplicate(drm
, m
);
98 dev_err(drm
->dev
, "failed to add mode %ux%u@%u\n",
99 m
->hdisplay
, m
->vdisplay
, m
->vrefresh
);
103 mode
->type
|= DRM_MODE_TYPE_DRIVER
;
105 if (panel
->desc
->num_modes
== 1)
106 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
108 drm_mode_set_name(mode
);
110 drm_mode_probed_add(connector
, mode
);
114 connector
->display_info
.bpc
= panel
->desc
->bpc
;
115 connector
->display_info
.width_mm
= panel
->desc
->size
.width
;
116 connector
->display_info
.height_mm
= panel
->desc
->size
.height
;
117 if (panel
->desc
->bus_format
)
118 drm_display_info_set_bus_formats(&connector
->display_info
,
119 &panel
->desc
->bus_format
, 1);
120 connector
->display_info
.bus_flags
= panel
->desc
->bus_flags
;
125 static int seiko_panel_disable(struct drm_panel
*panel
)
127 struct seiko_panel
*p
= to_seiko_panel(panel
);
133 p
->backlight
->props
.power
= FB_BLANK_POWERDOWN
;
134 p
->backlight
->props
.state
|= BL_CORE_FBBLANK
;
135 backlight_update_status(p
->backlight
);
143 static int seiko_panel_unprepare(struct drm_panel
*panel
)
145 struct seiko_panel
*p
= to_seiko_panel(panel
);
150 regulator_disable(p
->avdd
);
152 /* Add a 100ms delay as per the panel datasheet */
155 regulator_disable(p
->dvdd
);
162 static int seiko_panel_prepare(struct drm_panel
*panel
)
164 struct seiko_panel
*p
= to_seiko_panel(panel
);
170 err
= regulator_enable(p
->dvdd
);
172 dev_err(panel
->dev
, "failed to enable dvdd: %d\n", err
);
176 /* Add a 100ms delay as per the panel datasheet */
179 err
= regulator_enable(p
->avdd
);
181 dev_err(panel
->dev
, "failed to enable avdd: %d\n", err
);
190 regulator_disable(p
->dvdd
);
194 static int seiko_panel_enable(struct drm_panel
*panel
)
196 struct seiko_panel
*p
= to_seiko_panel(panel
);
202 p
->backlight
->props
.state
&= ~BL_CORE_FBBLANK
;
203 p
->backlight
->props
.power
= FB_BLANK_UNBLANK
;
204 backlight_update_status(p
->backlight
);
212 static int seiko_panel_get_modes(struct drm_panel
*panel
)
214 struct seiko_panel
*p
= to_seiko_panel(panel
);
216 /* add hard-coded panel modes */
217 return seiko_panel_get_fixed_modes(p
);
220 static int seiko_panel_get_timings(struct drm_panel
*panel
,
221 unsigned int num_timings
,
222 struct display_timing
*timings
)
224 struct seiko_panel
*p
= to_seiko_panel(panel
);
227 if (p
->desc
->num_timings
< num_timings
)
228 num_timings
= p
->desc
->num_timings
;
231 for (i
= 0; i
< num_timings
; i
++)
232 timings
[i
] = p
->desc
->timings
[i
];
234 return p
->desc
->num_timings
;
237 static const struct drm_panel_funcs seiko_panel_funcs
= {
238 .disable
= seiko_panel_disable
,
239 .unprepare
= seiko_panel_unprepare
,
240 .prepare
= seiko_panel_prepare
,
241 .enable
= seiko_panel_enable
,
242 .get_modes
= seiko_panel_get_modes
,
243 .get_timings
= seiko_panel_get_timings
,
246 static int seiko_panel_probe(struct device
*dev
,
247 const struct seiko_panel_desc
*desc
)
249 struct device_node
*backlight
;
250 struct seiko_panel
*panel
;
253 panel
= devm_kzalloc(dev
, sizeof(*panel
), GFP_KERNEL
);
257 panel
->enabled
= false;
258 panel
->prepared
= false;
261 panel
->dvdd
= devm_regulator_get(dev
, "dvdd");
262 if (IS_ERR(panel
->dvdd
))
263 return PTR_ERR(panel
->dvdd
);
265 panel
->avdd
= devm_regulator_get(dev
, "avdd");
266 if (IS_ERR(panel
->avdd
))
267 return PTR_ERR(panel
->avdd
);
269 backlight
= of_parse_phandle(dev
->of_node
, "backlight", 0);
271 panel
->backlight
= of_find_backlight_by_node(backlight
);
272 of_node_put(backlight
);
274 if (!panel
->backlight
)
275 return -EPROBE_DEFER
;
278 drm_panel_init(&panel
->base
);
279 panel
->base
.dev
= dev
;
280 panel
->base
.funcs
= &seiko_panel_funcs
;
282 err
= drm_panel_add(&panel
->base
);
286 dev_set_drvdata(dev
, panel
);
291 static int seiko_panel_remove(struct platform_device
*pdev
)
293 struct seiko_panel
*panel
= dev_get_drvdata(&pdev
->dev
);
295 drm_panel_remove(&panel
->base
);
297 seiko_panel_disable(&panel
->base
);
299 if (panel
->backlight
)
300 put_device(&panel
->backlight
->dev
);
305 static void seiko_panel_shutdown(struct platform_device
*pdev
)
307 struct seiko_panel
*panel
= dev_get_drvdata(&pdev
->dev
);
309 seiko_panel_disable(&panel
->base
);
312 static const struct display_timing seiko_43wvf1g_timing
= {
313 .pixelclock
= { 33500000, 33500000, 33500000 },
314 .hactive
= { 800, 800, 800 },
315 .hfront_porch
= { 164, 164, 164 },
316 .hback_porch
= { 89, 89, 89 },
317 .hsync_len
= { 10, 10, 10 },
318 .vactive
= { 480, 480, 480 },
319 .vfront_porch
= { 10, 10, 10 },
320 .vback_porch
= { 23, 23, 23 },
321 .vsync_len
= { 10, 10, 10 },
322 .flags
= DISPLAY_FLAGS_DE_LOW
,
325 static const struct seiko_panel_desc seiko_43wvf1g
= {
326 .timings
= &seiko_43wvf1g_timing
,
333 .bus_format
= MEDIA_BUS_FMT_RGB888_1X24
,
334 .bus_flags
= DRM_BUS_FLAG_DE_HIGH
| DRM_BUS_FLAG_PIXDATA_NEGEDGE
,
337 static const struct of_device_id platform_of_match
[] = {
339 .compatible
= "sii,43wvf1g",
340 .data
= &seiko_43wvf1g
,
345 MODULE_DEVICE_TABLE(of
, platform_of_match
);
347 static int seiko_panel_platform_probe(struct platform_device
*pdev
)
349 const struct of_device_id
*id
;
351 id
= of_match_node(platform_of_match
, pdev
->dev
.of_node
);
355 return seiko_panel_probe(&pdev
->dev
, id
->data
);
358 static struct platform_driver seiko_panel_platform_driver
= {
360 .name
= "seiko_panel",
361 .of_match_table
= platform_of_match
,
363 .probe
= seiko_panel_platform_probe
,
364 .remove
= seiko_panel_remove
,
365 .shutdown
= seiko_panel_shutdown
,
367 module_platform_driver(seiko_panel_platform_driver
);
369 MODULE_AUTHOR("Marco Franchi <marco.franchi@nxp.com");
370 MODULE_DESCRIPTION("Seiko 43WVF1G panel driver");
371 MODULE_LICENSE("GPL v2");