drm/panel: drop drm_device from drm_panel
[platform/kernel/linux-starfive.git] / drivers / gpu / drm / panel / panel-seiko-43wvf1g.c
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/backlight.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/consumer.h>
15
16 #include <video/display_timing.h>
17 #include <video/videomode.h>
18
19 #include <drm/drm_crtc.h>
20 #include <drm/drm_device.h>
21 #include <drm/drm_panel.h>
22
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;
28
29         unsigned int bpc;
30
31         /**
32          * @width: width (in millimeters) of the panel's active display area
33          * @height: height (in millimeters) of the panel's active display area
34          */
35         struct {
36                 unsigned int width;
37                 unsigned int height;
38         } size;
39
40         u32 bus_format;
41         u32 bus_flags;
42 };
43
44 struct seiko_panel {
45         struct drm_panel base;
46         bool prepared;
47         bool enabled;
48         const struct seiko_panel_desc *desc;
49         struct backlight_device *backlight;
50         struct regulator *dvdd;
51         struct regulator *avdd;
52 };
53
54 static inline struct seiko_panel *to_seiko_panel(struct drm_panel *panel)
55 {
56         return container_of(panel, struct seiko_panel, base);
57 }
58
59 static int seiko_panel_get_fixed_modes(struct seiko_panel *panel,
60                                        struct drm_connector *connector)
61 {
62         struct drm_display_mode *mode;
63         unsigned int i, num = 0;
64
65         if (!panel->desc)
66                 return 0;
67
68         for (i = 0; i < panel->desc->num_timings; i++) {
69                 const struct display_timing *dt = &panel->desc->timings[i];
70                 struct videomode vm;
71
72                 videomode_from_timing(dt, &vm);
73                 mode = drm_mode_create(connector->dev);
74                 if (!mode) {
75                         dev_err(panel->base.dev, "failed to add mode %ux%u\n",
76                                 dt->hactive.typ, dt->vactive.typ);
77                         continue;
78                 }
79
80                 drm_display_mode_from_videomode(&vm, mode);
81
82                 mode->type |= DRM_MODE_TYPE_DRIVER;
83
84                 if (panel->desc->num_timings == 1)
85                         mode->type |= DRM_MODE_TYPE_PREFERRED;
86
87                 drm_mode_probed_add(connector, mode);
88                 num++;
89         }
90
91         for (i = 0; i < panel->desc->num_modes; i++) {
92                 const struct drm_display_mode *m = &panel->desc->modes[i];
93
94                 mode = drm_mode_duplicate(connector->dev, m);
95                 if (!mode) {
96                         dev_err(panel->base.dev, "failed to add mode %ux%u@%u\n",
97                                 m->hdisplay, m->vdisplay, m->vrefresh);
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
123 static int seiko_panel_disable(struct drm_panel *panel)
124 {
125         struct seiko_panel *p = to_seiko_panel(panel);
126
127         if (!p->enabled)
128                 return 0;
129
130         if (p->backlight) {
131                 p->backlight->props.power = FB_BLANK_POWERDOWN;
132                 p->backlight->props.state |= BL_CORE_FBBLANK;
133                 backlight_update_status(p->backlight);
134         }
135
136         p->enabled = false;
137
138         return 0;
139 }
140
141 static int seiko_panel_unprepare(struct drm_panel *panel)
142 {
143         struct seiko_panel *p = to_seiko_panel(panel);
144
145         if (!p->prepared)
146                 return 0;
147
148         regulator_disable(p->avdd);
149
150         /* Add a 100ms delay as per the panel datasheet */
151         msleep(100);
152
153         regulator_disable(p->dvdd);
154
155         p->prepared = false;
156
157         return 0;
158 }
159
160 static int seiko_panel_prepare(struct drm_panel *panel)
161 {
162         struct seiko_panel *p = to_seiko_panel(panel);
163         int err;
164
165         if (p->prepared)
166                 return 0;
167
168         err = regulator_enable(p->dvdd);
169         if (err < 0) {
170                 dev_err(panel->dev, "failed to enable dvdd: %d\n", err);
171                 return err;
172         }
173
174         /* Add a 100ms delay as per the panel datasheet */
175         msleep(100);
176
177         err = regulator_enable(p->avdd);
178         if (err < 0) {
179                 dev_err(panel->dev, "failed to enable avdd: %d\n", err);
180                 goto disable_dvdd;
181         }
182
183         p->prepared = true;
184
185         return 0;
186
187 disable_dvdd:
188         regulator_disable(p->dvdd);
189         return err;
190 }
191
192 static int seiko_panel_enable(struct drm_panel *panel)
193 {
194         struct seiko_panel *p = to_seiko_panel(panel);
195
196         if (p->enabled)
197                 return 0;
198
199         if (p->backlight) {
200                 p->backlight->props.state &= ~BL_CORE_FBBLANK;
201                 p->backlight->props.power = FB_BLANK_UNBLANK;
202                 backlight_update_status(p->backlight);
203         }
204
205         p->enabled = true;
206
207         return 0;
208 }
209
210 static int seiko_panel_get_modes(struct drm_panel *panel,
211                                  struct drm_connector *connector)
212 {
213         struct seiko_panel *p = to_seiko_panel(panel);
214
215         /* add hard-coded panel modes */
216         return seiko_panel_get_fixed_modes(p, connector);
217 }
218
219 static int seiko_panel_get_timings(struct drm_panel *panel,
220                                     unsigned int num_timings,
221                                     struct display_timing *timings)
222 {
223         struct seiko_panel *p = to_seiko_panel(panel);
224         unsigned int i;
225
226         if (p->desc->num_timings < num_timings)
227                 num_timings = p->desc->num_timings;
228
229         if (timings)
230                 for (i = 0; i < num_timings; i++)
231                         timings[i] = p->desc->timings[i];
232
233         return p->desc->num_timings;
234 }
235
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,
243 };
244
245 static int seiko_panel_probe(struct device *dev,
246                                         const struct seiko_panel_desc *desc)
247 {
248         struct device_node *backlight;
249         struct seiko_panel *panel;
250         int err;
251
252         panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
253         if (!panel)
254                 return -ENOMEM;
255
256         panel->enabled = false;
257         panel->prepared = false;
258         panel->desc = desc;
259
260         panel->dvdd = devm_regulator_get(dev, "dvdd");
261         if (IS_ERR(panel->dvdd))
262                 return PTR_ERR(panel->dvdd);
263
264         panel->avdd = devm_regulator_get(dev, "avdd");
265         if (IS_ERR(panel->avdd))
266                 return PTR_ERR(panel->avdd);
267
268         backlight = of_parse_phandle(dev->of_node, "backlight", 0);
269         if (backlight) {
270                 panel->backlight = of_find_backlight_by_node(backlight);
271                 of_node_put(backlight);
272
273                 if (!panel->backlight)
274                         return -EPROBE_DEFER;
275         }
276
277         drm_panel_init(&panel->base, dev, &seiko_panel_funcs,
278                        DRM_MODE_CONNECTOR_DPI);
279
280         err = drm_panel_add(&panel->base);
281         if (err < 0)
282                 return err;
283
284         dev_set_drvdata(dev, panel);
285
286         return 0;
287 }
288
289 static int seiko_panel_remove(struct platform_device *pdev)
290 {
291         struct seiko_panel *panel = dev_get_drvdata(&pdev->dev);
292
293         drm_panel_remove(&panel->base);
294
295         seiko_panel_disable(&panel->base);
296
297         if (panel->backlight)
298                 put_device(&panel->backlight->dev);
299
300         return 0;
301 }
302
303 static void seiko_panel_shutdown(struct platform_device *pdev)
304 {
305         struct seiko_panel *panel = dev_get_drvdata(&pdev->dev);
306
307         seiko_panel_disable(&panel->base);
308 }
309
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,
321 };
322
323 static const struct seiko_panel_desc seiko_43wvf1g = {
324         .timings = &seiko_43wvf1g_timing,
325         .num_timings = 1,
326         .bpc = 8,
327         .size = {
328                 .width = 93,
329                 .height = 57,
330         },
331         .bus_format = MEDIA_BUS_FMT_RGB888_1X24,
332         .bus_flags = DRM_BUS_FLAG_DE_HIGH | DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE,
333 };
334
335 static const struct of_device_id platform_of_match[] = {
336         {
337                 .compatible = "sii,43wvf1g",
338                 .data = &seiko_43wvf1g,
339         }, {
340                 /* sentinel */
341         }
342 };
343 MODULE_DEVICE_TABLE(of, platform_of_match);
344
345 static int seiko_panel_platform_probe(struct platform_device *pdev)
346 {
347         const struct of_device_id *id;
348
349         id = of_match_node(platform_of_match, pdev->dev.of_node);
350         if (!id)
351                 return -ENODEV;
352
353         return seiko_panel_probe(&pdev->dev, id->data);
354 }
355
356 static struct platform_driver seiko_panel_platform_driver = {
357         .driver = {
358                 .name = "seiko_panel",
359                 .of_match_table = platform_of_match,
360         },
361         .probe = seiko_panel_platform_probe,
362         .remove = seiko_panel_remove,
363         .shutdown = seiko_panel_shutdown,
364 };
365 module_platform_driver(seiko_panel_platform_driver);
366
367 MODULE_AUTHOR("Marco Franchi <marco.franchi@nxp.com>");
368 MODULE_DESCRIPTION("Seiko 43WVF1G panel driver");
369 MODULE_LICENSE("GPL v2");