treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / panel / panel-seiko-43wvf1g.c
blob40fcbbbacb2c5f45fe8b8e957d9b6f38da59f81b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
7 */
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/regulator/consumer.h>
15 #include <video/display_timing.h>
16 #include <video/videomode.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_panel.h>
22 struct seiko_panel_desc {
23 const struct drm_display_mode *modes;
24 unsigned int num_modes;
25 const struct display_timing *timings;
26 unsigned int num_timings;
28 unsigned int bpc;
30 /**
31 * @width: width (in millimeters) of the panel's active display area
32 * @height: height (in millimeters) of the panel's active display area
34 struct {
35 unsigned int width;
36 unsigned int height;
37 } size;
39 u32 bus_format;
40 u32 bus_flags;
43 struct seiko_panel {
44 struct drm_panel base;
45 bool prepared;
46 bool enabled;
47 const struct seiko_panel_desc *desc;
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,
58 struct drm_connector *connector)
60 struct drm_display_mode *mode;
61 unsigned int i, num = 0;
63 if (!panel->desc)
64 return 0;
66 for (i = 0; i < panel->desc->num_timings; i++) {
67 const struct display_timing *dt = &panel->desc->timings[i];
68 struct videomode vm;
70 videomode_from_timing(dt, &vm);
71 mode = drm_mode_create(connector->dev);
72 if (!mode) {
73 dev_err(panel->base.dev, "failed to add mode %ux%u\n",
74 dt->hactive.typ, dt->vactive.typ);
75 continue;
78 drm_display_mode_from_videomode(&vm, mode);
80 mode->type |= DRM_MODE_TYPE_DRIVER;
82 if (panel->desc->num_timings == 1)
83 mode->type |= DRM_MODE_TYPE_PREFERRED;
85 drm_mode_probed_add(connector, mode);
86 num++;
89 for (i = 0; i < panel->desc->num_modes; i++) {
90 const struct drm_display_mode *m = &panel->desc->modes[i];
92 mode = drm_mode_duplicate(connector->dev, m);
93 if (!mode) {
94 dev_err(panel->base.dev, "failed to add mode %ux%u@%u\n",
95 m->hdisplay, m->vdisplay, m->vrefresh);
96 continue;
99 mode->type |= DRM_MODE_TYPE_DRIVER;
101 if (panel->desc->num_modes == 1)
102 mode->type |= DRM_MODE_TYPE_PREFERRED;
104 drm_mode_set_name(mode);
106 drm_mode_probed_add(connector, mode);
107 num++;
110 connector->display_info.bpc = panel->desc->bpc;
111 connector->display_info.width_mm = panel->desc->size.width;
112 connector->display_info.height_mm = panel->desc->size.height;
113 if (panel->desc->bus_format)
114 drm_display_info_set_bus_formats(&connector->display_info,
115 &panel->desc->bus_format, 1);
116 connector->display_info.bus_flags = panel->desc->bus_flags;
118 return num;
121 static int seiko_panel_disable(struct drm_panel *panel)
123 struct seiko_panel *p = to_seiko_panel(panel);
125 if (!p->enabled)
126 return 0;
128 p->enabled = false;
130 return 0;
133 static int seiko_panel_unprepare(struct drm_panel *panel)
135 struct seiko_panel *p = to_seiko_panel(panel);
137 if (!p->prepared)
138 return 0;
140 regulator_disable(p->avdd);
142 /* Add a 100ms delay as per the panel datasheet */
143 msleep(100);
145 regulator_disable(p->dvdd);
147 p->prepared = false;
149 return 0;
152 static int seiko_panel_prepare(struct drm_panel *panel)
154 struct seiko_panel *p = to_seiko_panel(panel);
155 int err;
157 if (p->prepared)
158 return 0;
160 err = regulator_enable(p->dvdd);
161 if (err < 0) {
162 dev_err(panel->dev, "failed to enable dvdd: %d\n", err);
163 return err;
166 /* Add a 100ms delay as per the panel datasheet */
167 msleep(100);
169 err = regulator_enable(p->avdd);
170 if (err < 0) {
171 dev_err(panel->dev, "failed to enable avdd: %d\n", err);
172 goto disable_dvdd;
175 p->prepared = true;
177 return 0;
179 disable_dvdd:
180 regulator_disable(p->dvdd);
181 return err;
184 static int seiko_panel_enable(struct drm_panel *panel)
186 struct seiko_panel *p = to_seiko_panel(panel);
188 if (p->enabled)
189 return 0;
191 p->enabled = true;
193 return 0;
196 static int seiko_panel_get_modes(struct drm_panel *panel,
197 struct drm_connector *connector)
199 struct seiko_panel *p = to_seiko_panel(panel);
201 /* add hard-coded panel modes */
202 return seiko_panel_get_fixed_modes(p, connector);
205 static int seiko_panel_get_timings(struct drm_panel *panel,
206 unsigned int num_timings,
207 struct display_timing *timings)
209 struct seiko_panel *p = to_seiko_panel(panel);
210 unsigned int i;
212 if (p->desc->num_timings < num_timings)
213 num_timings = p->desc->num_timings;
215 if (timings)
216 for (i = 0; i < num_timings; i++)
217 timings[i] = p->desc->timings[i];
219 return p->desc->num_timings;
222 static const struct drm_panel_funcs seiko_panel_funcs = {
223 .disable = seiko_panel_disable,
224 .unprepare = seiko_panel_unprepare,
225 .prepare = seiko_panel_prepare,
226 .enable = seiko_panel_enable,
227 .get_modes = seiko_panel_get_modes,
228 .get_timings = seiko_panel_get_timings,
231 static int seiko_panel_probe(struct device *dev,
232 const struct seiko_panel_desc *desc)
234 struct seiko_panel *panel;
235 int err;
237 panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
238 if (!panel)
239 return -ENOMEM;
241 panel->enabled = false;
242 panel->prepared = false;
243 panel->desc = desc;
245 panel->dvdd = devm_regulator_get(dev, "dvdd");
246 if (IS_ERR(panel->dvdd))
247 return PTR_ERR(panel->dvdd);
249 panel->avdd = devm_regulator_get(dev, "avdd");
250 if (IS_ERR(panel->avdd))
251 return PTR_ERR(panel->avdd);
253 drm_panel_init(&panel->base, dev, &seiko_panel_funcs,
254 DRM_MODE_CONNECTOR_DPI);
256 err = drm_panel_of_backlight(&panel->base);
257 if (err)
258 return err;
260 err = drm_panel_add(&panel->base);
261 if (err < 0)
262 return err;
264 dev_set_drvdata(dev, panel);
266 return 0;
269 static int seiko_panel_remove(struct platform_device *pdev)
271 struct seiko_panel *panel = dev_get_drvdata(&pdev->dev);
273 drm_panel_remove(&panel->base);
274 drm_panel_disable(&panel->base);
276 return 0;
279 static void seiko_panel_shutdown(struct platform_device *pdev)
281 struct seiko_panel *panel = dev_get_drvdata(&pdev->dev);
283 drm_panel_disable(&panel->base);
286 static const struct display_timing seiko_43wvf1g_timing = {
287 .pixelclock = { 33500000, 33500000, 33500000 },
288 .hactive = { 800, 800, 800 },
289 .hfront_porch = { 164, 164, 164 },
290 .hback_porch = { 89, 89, 89 },
291 .hsync_len = { 10, 10, 10 },
292 .vactive = { 480, 480, 480 },
293 .vfront_porch = { 10, 10, 10 },
294 .vback_porch = { 23, 23, 23 },
295 .vsync_len = { 10, 10, 10 },
296 .flags = DISPLAY_FLAGS_DE_LOW,
299 static const struct seiko_panel_desc seiko_43wvf1g = {
300 .timings = &seiko_43wvf1g_timing,
301 .num_timings = 1,
302 .bpc = 8,
303 .size = {
304 .width = 93,
305 .height = 57,
307 .bus_format = MEDIA_BUS_FMT_RGB888_1X24,
308 .bus_flags = DRM_BUS_FLAG_DE_HIGH | DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE,
311 static const struct of_device_id platform_of_match[] = {
313 .compatible = "sii,43wvf1g",
314 .data = &seiko_43wvf1g,
315 }, {
316 /* sentinel */
319 MODULE_DEVICE_TABLE(of, platform_of_match);
321 static int seiko_panel_platform_probe(struct platform_device *pdev)
323 const struct of_device_id *id;
325 id = of_match_node(platform_of_match, pdev->dev.of_node);
326 if (!id)
327 return -ENODEV;
329 return seiko_panel_probe(&pdev->dev, id->data);
332 static struct platform_driver seiko_panel_platform_driver = {
333 .driver = {
334 .name = "seiko_panel",
335 .of_match_table = platform_of_match,
337 .probe = seiko_panel_platform_probe,
338 .remove = seiko_panel_remove,
339 .shutdown = seiko_panel_shutdown,
341 module_platform_driver(seiko_panel_platform_driver);
343 MODULE_AUTHOR("Marco Franchi <marco.franchi@nxp.com>");
344 MODULE_DESCRIPTION("Seiko 43WVF1G panel driver");
345 MODULE_LICENSE("GPL v2");