1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2017 NXP Semiconductors.
4 * Author: Marco Franchi <marco.franchi@nxp.com>
5 *
6 * Based on Panel Simple driver by Thierry Reding <treding@nvidia.com>
7 */
8
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/media-bus-format.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/consumer.h>
16
17 #include <video/display_timing.h>
18 #include <video/videomode.h>
19
20 #include <drm/drm_crtc.h>
21 #include <drm/drm_device.h>
22 #include <drm/drm_panel.h>
23
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;
29
30 unsigned int bpc;
31
32 /**
33 * @width: width (in millimeters) of the panel's active display area
34 * @height: height (in millimeters) of the panel's active display area
35 */
36 struct {
37 unsigned int width;
38 unsigned int height;
39 } size;
40
41 u32 bus_format;
42 u32 bus_flags;
43 };
44
45 struct seiko_panel {
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;
51 };
52
to_seiko_panel(struct drm_panel * panel)53 static inline struct seiko_panel *to_seiko_panel(struct drm_panel *panel)
54 {
55 return container_of(panel, struct seiko_panel, base);
56 }
57
seiko_panel_get_fixed_modes(struct seiko_panel * panel,struct drm_connector * connector)58 static int seiko_panel_get_fixed_modes(struct seiko_panel *panel,
59 struct drm_connector *connector)
60 {
61 struct drm_display_mode *mode;
62 unsigned int i, num = 0;
63
64 if (!panel->desc)
65 return 0;
66
67 for (i = 0; i < panel->desc->num_timings; i++) {
68 const struct display_timing *dt = &panel->desc->timings[i];
69 struct videomode vm;
70
71 videomode_from_timing(dt, &vm);
72 mode = drm_mode_create(connector->dev);
73 if (!mode) {
74 dev_err(panel->base.dev, "failed to add mode %ux%u\n",
75 dt->hactive.typ, dt->vactive.typ);
76 continue;
77 }
78
79 drm_display_mode_from_videomode(&vm, mode);
80
81 mode->type |= DRM_MODE_TYPE_DRIVER;
82
83 if (panel->desc->num_timings == 1)
84 mode->type |= DRM_MODE_TYPE_PREFERRED;
85
86 drm_mode_probed_add(connector, mode);
87 num++;
88 }
89
90 for (i = 0; i < panel->desc->num_modes; i++) {
91 const struct drm_display_mode *m = &panel->desc->modes[i];
92
93 mode = drm_mode_duplicate(connector->dev, m);
94 if (!mode) {
95 dev_err(panel->base.dev, "failed to add mode %ux%u@%u\n",
96 m->hdisplay, m->vdisplay,
97 drm_mode_vrefresh(m));
98 continue;
99 }
100
101 mode->type |= DRM_MODE_TYPE_DRIVER;
102
103 if (panel->desc->num_modes == 1)
104 mode->type |= DRM_MODE_TYPE_PREFERRED;
105
106 drm_mode_set_name(mode);
107
108 drm_mode_probed_add(connector, mode);
109 num++;
110 }
111
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;
119
120 return num;
121 }
122
seiko_panel_unprepare(struct drm_panel * panel)123 static int seiko_panel_unprepare(struct drm_panel *panel)
124 {
125 struct seiko_panel *p = to_seiko_panel(panel);
126
127 gpiod_set_value_cansleep(p->enable_gpio, 0);
128
129 regulator_disable(p->avdd);
130
131 /* Add a 100ms delay as per the panel datasheet */
132 msleep(100);
133
134 regulator_disable(p->dvdd);
135
136 return 0;
137 }
138
seiko_panel_prepare(struct drm_panel * panel)139 static int seiko_panel_prepare(struct drm_panel *panel)
140 {
141 struct seiko_panel *p = to_seiko_panel(panel);
142 int err;
143
144 err = regulator_enable(p->dvdd);
145 if (err < 0) {
146 dev_err(panel->dev, "failed to enable dvdd: %d\n", err);
147 return err;
148 }
149
150 /* Add a 100ms delay as per the panel datasheet */
151 msleep(100);
152
153 err = regulator_enable(p->avdd);
154 if (err < 0) {
155 dev_err(panel->dev, "failed to enable avdd: %d\n", err);
156 goto disable_dvdd;
157 }
158
159 gpiod_set_value_cansleep(p->enable_gpio, 1);
160
161 return 0;
162
163 disable_dvdd:
164 regulator_disable(p->dvdd);
165 return err;
166 }
167
seiko_panel_get_modes(struct drm_panel * panel,struct drm_connector * connector)168 static int seiko_panel_get_modes(struct drm_panel *panel,
169 struct drm_connector *connector)
170 {
171 struct seiko_panel *p = to_seiko_panel(panel);
172
173 /* add hard-coded panel modes */
174 return seiko_panel_get_fixed_modes(p, connector);
175 }
176
seiko_panel_get_timings(struct drm_panel * panel,unsigned int num_timings,struct display_timing * timings)177 static int seiko_panel_get_timings(struct drm_panel *panel,
178 unsigned int num_timings,
179 struct display_timing *timings)
180 {
181 struct seiko_panel *p = to_seiko_panel(panel);
182 unsigned int i;
183
184 if (p->desc->num_timings < num_timings)
185 num_timings = p->desc->num_timings;
186
187 if (timings)
188 for (i = 0; i < num_timings; i++)
189 timings[i] = p->desc->timings[i];
190
191 return p->desc->num_timings;
192 }
193
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,
199 };
200
seiko_panel_probe(struct device * dev,const struct seiko_panel_desc * desc)201 static int seiko_panel_probe(struct device *dev,
202 const struct seiko_panel_desc *desc)
203 {
204 struct seiko_panel *panel;
205 int err;
206
207 panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
208 if (!panel)
209 return -ENOMEM;
210
211 panel->desc = desc;
212
213 panel->dvdd = devm_regulator_get(dev, "dvdd");
214 if (IS_ERR(panel->dvdd))
215 return PTR_ERR(panel->dvdd);
216
217 panel->avdd = devm_regulator_get(dev, "avdd");
218 if (IS_ERR(panel->avdd))
219 return PTR_ERR(panel->avdd);
220
221 panel->enable_gpio = devm_gpiod_get_optional(dev, "enable",
222 GPIOD_OUT_LOW);
223 if (IS_ERR(panel->enable_gpio))
224 return dev_err_probe(dev, PTR_ERR(panel->enable_gpio),
225 "failed to request GPIO\n");
226
227 drm_panel_init(&panel->base, dev, &seiko_panel_funcs,
228 DRM_MODE_CONNECTOR_DPI);
229
230 err = drm_panel_of_backlight(&panel->base);
231 if (err)
232 return err;
233
234 drm_panel_add(&panel->base);
235
236 dev_set_drvdata(dev, panel);
237
238 return 0;
239 }
240
seiko_panel_remove(struct platform_device * pdev)241 static void seiko_panel_remove(struct platform_device *pdev)
242 {
243 struct seiko_panel *panel = platform_get_drvdata(pdev);
244
245 drm_panel_remove(&panel->base);
246 }
247
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,
259 };
260
261 static const struct seiko_panel_desc seiko_43wvf1g = {
262 .timings = &seiko_43wvf1g_timing,
263 .num_timings = 1,
264 .bpc = 8,
265 .size = {
266 .width = 93,
267 .height = 57,
268 },
269 .bus_format = MEDIA_BUS_FMT_RGB888_1X24,
270 .bus_flags = DRM_BUS_FLAG_DE_HIGH | DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE,
271 };
272
273 static const struct of_device_id platform_of_match[] = {
274 {
275 .compatible = "sii,43wvf1g",
276 .data = &seiko_43wvf1g,
277 }, {
278 /* sentinel */
279 }
280 };
281 MODULE_DEVICE_TABLE(of, platform_of_match);
282
seiko_panel_platform_probe(struct platform_device * pdev)283 static int seiko_panel_platform_probe(struct platform_device *pdev)
284 {
285 const struct of_device_id *id;
286
287 id = of_match_node(platform_of_match, pdev->dev.of_node);
288 if (!id)
289 return -ENODEV;
290
291 return seiko_panel_probe(&pdev->dev, id->data);
292 }
293
294 static struct platform_driver seiko_panel_platform_driver = {
295 .driver = {
296 .name = "seiko_panel",
297 .of_match_table = platform_of_match,
298 },
299 .probe = seiko_panel_platform_probe,
300 .remove = seiko_panel_remove,
301 };
302 module_platform_driver(seiko_panel_platform_driver);
303
304 MODULE_AUTHOR("Marco Franchi <marco.franchi@nxp.com>");
305 MODULE_DESCRIPTION("Seiko 43WVF1G panel driver");
306 MODULE_LICENSE("GPL v2");
307