treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / panel / panel-lvds.c
blob5ce3f4a2b7a19da6849b41fdb697c8d71025a3db
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Generic LVDS panel driver
5 * Copyright (C) 2016 Laurent Pinchart
6 * Copyright (C) 2016 Renesas Electronics Corporation
8 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
9 */
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
18 #include <video/display_timing.h>
19 #include <video/of_display_timing.h>
20 #include <video/videomode.h>
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_panel.h>
25 struct panel_lvds {
26 struct drm_panel panel;
27 struct device *dev;
29 const char *label;
30 unsigned int width;
31 unsigned int height;
32 struct videomode video_mode;
33 unsigned int bus_format;
34 bool data_mirror;
36 struct regulator *supply;
38 struct gpio_desc *enable_gpio;
39 struct gpio_desc *reset_gpio;
42 static inline struct panel_lvds *to_panel_lvds(struct drm_panel *panel)
44 return container_of(panel, struct panel_lvds, panel);
47 static int panel_lvds_unprepare(struct drm_panel *panel)
49 struct panel_lvds *lvds = to_panel_lvds(panel);
51 if (lvds->enable_gpio)
52 gpiod_set_value_cansleep(lvds->enable_gpio, 0);
54 if (lvds->supply)
55 regulator_disable(lvds->supply);
57 return 0;
60 static int panel_lvds_prepare(struct drm_panel *panel)
62 struct panel_lvds *lvds = to_panel_lvds(panel);
64 if (lvds->supply) {
65 int err;
67 err = regulator_enable(lvds->supply);
68 if (err < 0) {
69 dev_err(lvds->dev, "failed to enable supply: %d\n",
70 err);
71 return err;
75 if (lvds->enable_gpio)
76 gpiod_set_value_cansleep(lvds->enable_gpio, 1);
78 return 0;
81 static int panel_lvds_get_modes(struct drm_panel *panel,
82 struct drm_connector *connector)
84 struct panel_lvds *lvds = to_panel_lvds(panel);
85 struct drm_display_mode *mode;
87 mode = drm_mode_create(connector->dev);
88 if (!mode)
89 return 0;
91 drm_display_mode_from_videomode(&lvds->video_mode, mode);
92 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
93 drm_mode_probed_add(connector, mode);
95 connector->display_info.width_mm = lvds->width;
96 connector->display_info.height_mm = lvds->height;
97 drm_display_info_set_bus_formats(&connector->display_info,
98 &lvds->bus_format, 1);
99 connector->display_info.bus_flags = lvds->data_mirror
100 ? DRM_BUS_FLAG_DATA_LSB_TO_MSB
101 : DRM_BUS_FLAG_DATA_MSB_TO_LSB;
103 return 1;
106 static const struct drm_panel_funcs panel_lvds_funcs = {
107 .unprepare = panel_lvds_unprepare,
108 .prepare = panel_lvds_prepare,
109 .get_modes = panel_lvds_get_modes,
112 static int panel_lvds_parse_dt(struct panel_lvds *lvds)
114 struct device_node *np = lvds->dev->of_node;
115 struct display_timing timing;
116 const char *mapping;
117 int ret;
119 ret = of_get_display_timing(np, "panel-timing", &timing);
120 if (ret < 0) {
121 dev_err(lvds->dev, "%pOF: problems parsing panel-timing (%d)\n",
122 np, ret);
123 return ret;
126 videomode_from_timing(&timing, &lvds->video_mode);
128 ret = of_property_read_u32(np, "width-mm", &lvds->width);
129 if (ret < 0) {
130 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
131 np, "width-mm");
132 return -ENODEV;
134 ret = of_property_read_u32(np, "height-mm", &lvds->height);
135 if (ret < 0) {
136 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
137 np, "height-mm");
138 return -ENODEV;
141 of_property_read_string(np, "label", &lvds->label);
143 ret = of_property_read_string(np, "data-mapping", &mapping);
144 if (ret < 0) {
145 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
146 np, "data-mapping");
147 return -ENODEV;
150 if (!strcmp(mapping, "jeida-18")) {
151 lvds->bus_format = MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
152 } else if (!strcmp(mapping, "jeida-24")) {
153 lvds->bus_format = MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
154 } else if (!strcmp(mapping, "vesa-24")) {
155 lvds->bus_format = MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
156 } else {
157 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
158 np, "data-mapping");
159 return -EINVAL;
162 lvds->data_mirror = of_property_read_bool(np, "data-mirror");
164 return 0;
167 static int panel_lvds_probe(struct platform_device *pdev)
169 struct panel_lvds *lvds;
170 int ret;
172 lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL);
173 if (!lvds)
174 return -ENOMEM;
176 lvds->dev = &pdev->dev;
178 ret = panel_lvds_parse_dt(lvds);
179 if (ret < 0)
180 return ret;
182 lvds->supply = devm_regulator_get_optional(lvds->dev, "power");
183 if (IS_ERR(lvds->supply)) {
184 ret = PTR_ERR(lvds->supply);
186 if (ret != -ENODEV) {
187 if (ret != -EPROBE_DEFER)
188 dev_err(lvds->dev, "failed to request regulator: %d\n",
189 ret);
190 return ret;
193 lvds->supply = NULL;
196 /* Get GPIOs and backlight controller. */
197 lvds->enable_gpio = devm_gpiod_get_optional(lvds->dev, "enable",
198 GPIOD_OUT_LOW);
199 if (IS_ERR(lvds->enable_gpio)) {
200 ret = PTR_ERR(lvds->enable_gpio);
201 dev_err(lvds->dev, "failed to request %s GPIO: %d\n",
202 "enable", ret);
203 return ret;
206 lvds->reset_gpio = devm_gpiod_get_optional(lvds->dev, "reset",
207 GPIOD_OUT_HIGH);
208 if (IS_ERR(lvds->reset_gpio)) {
209 ret = PTR_ERR(lvds->reset_gpio);
210 dev_err(lvds->dev, "failed to request %s GPIO: %d\n",
211 "reset", ret);
212 return ret;
216 * TODO: Handle all power supplies specified in the DT node in a generic
217 * way for panels that don't care about power supply ordering. LVDS
218 * panels that require a specific power sequence will need a dedicated
219 * driver.
222 /* Register the panel. */
223 drm_panel_init(&lvds->panel, lvds->dev, &panel_lvds_funcs,
224 DRM_MODE_CONNECTOR_LVDS);
226 ret = drm_panel_of_backlight(&lvds->panel);
227 if (ret)
228 return ret;
230 ret = drm_panel_add(&lvds->panel);
231 if (ret < 0)
232 return ret;
234 dev_set_drvdata(lvds->dev, lvds);
235 return 0;
238 static int panel_lvds_remove(struct platform_device *pdev)
240 struct panel_lvds *lvds = dev_get_drvdata(&pdev->dev);
242 drm_panel_remove(&lvds->panel);
244 drm_panel_disable(&lvds->panel);
246 return 0;
249 static const struct of_device_id panel_lvds_of_table[] = {
250 { .compatible = "panel-lvds", },
251 { /* Sentinel */ },
254 MODULE_DEVICE_TABLE(of, panel_lvds_of_table);
256 static struct platform_driver panel_lvds_driver = {
257 .probe = panel_lvds_probe,
258 .remove = panel_lvds_remove,
259 .driver = {
260 .name = "panel-lvds",
261 .of_match_table = panel_lvds_of_table,
265 module_platform_driver(panel_lvds_driver);
267 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
268 MODULE_DESCRIPTION("LVDS Panel Driver");
269 MODULE_LICENSE("GPL");