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/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/media-bus-format.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/consumer.h>
17 #include <video/display_timing.h>
18 #include <video/videomode.h>
20 #include <drm/drm_crtc.h>
21 #include <drm/drm_device.h>
22 #include <drm/drm_panel.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
;
47 const struct seiko_panel_desc
*desc
;
48 struct regulator
*dvdd
;
49 struct regulator
*avdd
;
50 struct gpio_desc
*enable_gpio
;
53 static inline struct seiko_panel
*to_seiko_panel(struct drm_panel
*panel
)
55 return container_of(panel
, struct seiko_panel
, base
);
58 static int seiko_panel_get_fixed_modes(struct seiko_panel
*panel
,
59 struct drm_connector
*connector
)
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(connector
->dev
);
74 dev_err(panel
->base
.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(connector
->dev
, m
);
95 dev_err(panel
->base
.dev
, "failed to add mode %ux%u@%u\n",
96 m
->hdisplay
, m
->vdisplay
,
97 drm_mode_vrefresh(m
));
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_unprepare(struct drm_panel
*panel
)
125 struct seiko_panel
*p
= to_seiko_panel(panel
);
127 gpiod_set_value_cansleep(p
->enable_gpio
, 0);
129 regulator_disable(p
->avdd
);
131 /* Add a 100ms delay as per the panel datasheet */
134 regulator_disable(p
->dvdd
);
139 static int seiko_panel_prepare(struct drm_panel
*panel
)
141 struct seiko_panel
*p
= to_seiko_panel(panel
);
144 err
= regulator_enable(p
->dvdd
);
146 dev_err(panel
->dev
, "failed to enable dvdd: %d\n", err
);
150 /* Add a 100ms delay as per the panel datasheet */
153 err
= regulator_enable(p
->avdd
);
155 dev_err(panel
->dev
, "failed to enable avdd: %d\n", err
);
159 gpiod_set_value_cansleep(p
->enable_gpio
, 1);
164 regulator_disable(p
->dvdd
);
168 static int seiko_panel_get_modes(struct drm_panel
*panel
,
169 struct drm_connector
*connector
)
171 struct seiko_panel
*p
= to_seiko_panel(panel
);
173 /* add hard-coded panel modes */
174 return seiko_panel_get_fixed_modes(p
, connector
);
177 static int seiko_panel_get_timings(struct drm_panel
*panel
,
178 unsigned int num_timings
,
179 struct display_timing
*timings
)
181 struct seiko_panel
*p
= to_seiko_panel(panel
);
184 if (p
->desc
->num_timings
< num_timings
)
185 num_timings
= p
->desc
->num_timings
;
188 for (i
= 0; i
< num_timings
; i
++)
189 timings
[i
] = p
->desc
->timings
[i
];
191 return p
->desc
->num_timings
;
194 static const struct drm_panel_funcs seiko_panel_funcs
= {
195 .unprepare
= seiko_panel_unprepare
,
196 .prepare
= seiko_panel_prepare
,
197 .get_modes
= seiko_panel_get_modes
,
198 .get_timings
= seiko_panel_get_timings
,
201 static int seiko_panel_probe(struct device
*dev
,
202 const struct seiko_panel_desc
*desc
)
204 struct seiko_panel
*panel
;
207 panel
= devm_kzalloc(dev
, sizeof(*panel
), GFP_KERNEL
);
213 panel
->dvdd
= devm_regulator_get(dev
, "dvdd");
214 if (IS_ERR(panel
->dvdd
))
215 return PTR_ERR(panel
->dvdd
);
217 panel
->avdd
= devm_regulator_get(dev
, "avdd");
218 if (IS_ERR(panel
->avdd
))
219 return PTR_ERR(panel
->avdd
);
221 panel
->enable_gpio
= devm_gpiod_get_optional(dev
, "enable",
223 if (IS_ERR(panel
->enable_gpio
))
224 return dev_err_probe(dev
, PTR_ERR(panel
->enable_gpio
),
225 "failed to request GPIO\n");
227 drm_panel_init(&panel
->base
, dev
, &seiko_panel_funcs
,
228 DRM_MODE_CONNECTOR_DPI
);
230 err
= drm_panel_of_backlight(&panel
->base
);
234 drm_panel_add(&panel
->base
);
236 dev_set_drvdata(dev
, panel
);
241 static void seiko_panel_remove(struct platform_device
*pdev
)
243 struct seiko_panel
*panel
= platform_get_drvdata(pdev
);
245 drm_panel_remove(&panel
->base
);
248 static const struct display_timing seiko_43wvf1g_timing
= {
249 .pixelclock
= { 33500000, 33500000, 33500000 },
250 .hactive
= { 800, 800, 800 },
251 .hfront_porch
= { 164, 164, 164 },
252 .hback_porch
= { 89, 89, 89 },
253 .hsync_len
= { 10, 10, 10 },
254 .vactive
= { 480, 480, 480 },
255 .vfront_porch
= { 10, 10, 10 },
256 .vback_porch
= { 23, 23, 23 },
257 .vsync_len
= { 10, 10, 10 },
258 .flags
= DISPLAY_FLAGS_DE_LOW
,
261 static const struct seiko_panel_desc seiko_43wvf1g
= {
262 .timings
= &seiko_43wvf1g_timing
,
269 .bus_format
= MEDIA_BUS_FMT_RGB888_1X24
,
270 .bus_flags
= DRM_BUS_FLAG_DE_HIGH
| DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE
,
273 static const struct of_device_id platform_of_match
[] = {
275 .compatible
= "sii,43wvf1g",
276 .data
= &seiko_43wvf1g
,
281 MODULE_DEVICE_TABLE(of
, platform_of_match
);
283 static int seiko_panel_platform_probe(struct platform_device
*pdev
)
285 const struct of_device_id
*id
;
287 id
= of_match_node(platform_of_match
, pdev
->dev
.of_node
);
291 return seiko_panel_probe(&pdev
->dev
, id
->data
);
294 static struct platform_driver seiko_panel_platform_driver
= {
296 .name
= "seiko_panel",
297 .of_match_table
= platform_of_match
,
299 .probe
= seiko_panel_platform_probe
,
300 .remove
= seiko_panel_remove
,
302 module_platform_driver(seiko_panel_platform_driver
);
304 MODULE_AUTHOR("Marco Franchi <marco.franchi@nxp.com>");
305 MODULE_DESCRIPTION("Seiko 43WVF1G panel driver");
306 MODULE_LICENSE("GPL v2");