xref: /openbmc/linux/drivers/gpu/drm/panel/panel-lvds.c (revision 6abd061d)
1 /*
2  * Generic LVDS panel driver
3  *
4  * Copyright (C) 2016 Laurent Pinchart
5  * Copyright (C) 2016 Renesas Electronics Corporation
6  *
7  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  */
14 
15 #include <linux/backlight.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/module.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
22 
23 #include <drm/drmP.h>
24 #include <drm/drm_crtc.h>
25 #include <drm/drm_panel.h>
26 
27 #include <video/display_timing.h>
28 #include <video/of_display_timing.h>
29 #include <video/videomode.h>
30 
31 struct panel_lvds {
32 	struct drm_panel panel;
33 	struct device *dev;
34 
35 	const char *label;
36 	unsigned int width;
37 	unsigned int height;
38 	struct videomode video_mode;
39 	unsigned int bus_format;
40 	bool data_mirror;
41 
42 	struct backlight_device *backlight;
43 	struct regulator *supply;
44 
45 	struct gpio_desc *enable_gpio;
46 	struct gpio_desc *reset_gpio;
47 };
48 
49 static inline struct panel_lvds *to_panel_lvds(struct drm_panel *panel)
50 {
51 	return container_of(panel, struct panel_lvds, panel);
52 }
53 
54 static int panel_lvds_disable(struct drm_panel *panel)
55 {
56 	struct panel_lvds *lvds = to_panel_lvds(panel);
57 
58 	if (lvds->backlight) {
59 		lvds->backlight->props.power = FB_BLANK_POWERDOWN;
60 		lvds->backlight->props.state |= BL_CORE_FBBLANK;
61 		backlight_update_status(lvds->backlight);
62 	}
63 
64 	return 0;
65 }
66 
67 static int panel_lvds_unprepare(struct drm_panel *panel)
68 {
69 	struct panel_lvds *lvds = to_panel_lvds(panel);
70 
71 	if (lvds->enable_gpio)
72 		gpiod_set_value_cansleep(lvds->enable_gpio, 0);
73 
74 	if (lvds->supply)
75 		regulator_disable(lvds->supply);
76 
77 	return 0;
78 }
79 
80 static int panel_lvds_prepare(struct drm_panel *panel)
81 {
82 	struct panel_lvds *lvds = to_panel_lvds(panel);
83 
84 	if (lvds->supply) {
85 		int err;
86 
87 		err = regulator_enable(lvds->supply);
88 		if (err < 0) {
89 			dev_err(lvds->dev, "failed to enable supply: %d\n",
90 				err);
91 			return err;
92 		}
93 	}
94 
95 	if (lvds->enable_gpio)
96 		gpiod_set_value_cansleep(lvds->enable_gpio, 1);
97 
98 	return 0;
99 }
100 
101 static int panel_lvds_enable(struct drm_panel *panel)
102 {
103 	struct panel_lvds *lvds = to_panel_lvds(panel);
104 
105 	if (lvds->backlight) {
106 		lvds->backlight->props.state &= ~BL_CORE_FBBLANK;
107 		lvds->backlight->props.power = FB_BLANK_UNBLANK;
108 		backlight_update_status(lvds->backlight);
109 	}
110 
111 	return 0;
112 }
113 
114 static int panel_lvds_get_modes(struct drm_panel *panel)
115 {
116 	struct panel_lvds *lvds = to_panel_lvds(panel);
117 	struct drm_connector *connector = lvds->panel.connector;
118 	struct drm_display_mode *mode;
119 
120 	mode = drm_mode_create(lvds->panel.drm);
121 	if (!mode)
122 		return 0;
123 
124 	drm_display_mode_from_videomode(&lvds->video_mode, mode);
125 	mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
126 	drm_mode_probed_add(connector, mode);
127 
128 	connector->display_info.width_mm = lvds->width;
129 	connector->display_info.height_mm = lvds->height;
130 	drm_display_info_set_bus_formats(&connector->display_info,
131 					 &lvds->bus_format, 1);
132 	connector->display_info.bus_flags = lvds->data_mirror
133 					  ? DRM_BUS_FLAG_DATA_LSB_TO_MSB
134 					  : DRM_BUS_FLAG_DATA_MSB_TO_LSB;
135 
136 	return 1;
137 }
138 
139 static const struct drm_panel_funcs panel_lvds_funcs = {
140 	.disable = panel_lvds_disable,
141 	.unprepare = panel_lvds_unprepare,
142 	.prepare = panel_lvds_prepare,
143 	.enable = panel_lvds_enable,
144 	.get_modes = panel_lvds_get_modes,
145 };
146 
147 static int panel_lvds_parse_dt(struct panel_lvds *lvds)
148 {
149 	struct device_node *np = lvds->dev->of_node;
150 	struct display_timing timing;
151 	const char *mapping;
152 	int ret;
153 
154 	ret = of_get_display_timing(np, "panel-timing", &timing);
155 	if (ret < 0)
156 		return ret;
157 
158 	videomode_from_timing(&timing, &lvds->video_mode);
159 
160 	ret = of_property_read_u32(np, "width-mm", &lvds->width);
161 	if (ret < 0) {
162 		dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
163 			np, "width-mm");
164 		return -ENODEV;
165 	}
166 	ret = of_property_read_u32(np, "height-mm", &lvds->height);
167 	if (ret < 0) {
168 		dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
169 			np, "height-mm");
170 		return -ENODEV;
171 	}
172 
173 	of_property_read_string(np, "label", &lvds->label);
174 
175 	ret = of_property_read_string(np, "data-mapping", &mapping);
176 	if (ret < 0) {
177 		dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
178 			np, "data-mapping");
179 		return -ENODEV;
180 	}
181 
182 	if (!strcmp(mapping, "jeida-18")) {
183 		lvds->bus_format = MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
184 	} else if (!strcmp(mapping, "jeida-24")) {
185 		lvds->bus_format = MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
186 	} else if (!strcmp(mapping, "vesa-24")) {
187 		lvds->bus_format = MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
188 	} else {
189 		dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
190 			np, "data-mapping");
191 		return -EINVAL;
192 	}
193 
194 	lvds->data_mirror = of_property_read_bool(np, "data-mirror");
195 
196 	return 0;
197 }
198 
199 static int panel_lvds_probe(struct platform_device *pdev)
200 {
201 	struct panel_lvds *lvds;
202 	struct device_node *np;
203 	int ret;
204 
205 	lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL);
206 	if (!lvds)
207 		return -ENOMEM;
208 
209 	lvds->dev = &pdev->dev;
210 
211 	ret = panel_lvds_parse_dt(lvds);
212 	if (ret < 0)
213 		return ret;
214 
215 	lvds->supply = devm_regulator_get_optional(lvds->dev, "power");
216 	if (IS_ERR(lvds->supply)) {
217 		ret = PTR_ERR(lvds->supply);
218 
219 		if (ret != -ENODEV) {
220 			if (ret != -EPROBE_DEFER)
221 				dev_err(lvds->dev, "failed to request regulator: %d\n",
222 					ret);
223 			return ret;
224 		}
225 
226 		lvds->supply = NULL;
227 	}
228 
229 	/* Get GPIOs and backlight controller. */
230 	lvds->enable_gpio = devm_gpiod_get_optional(lvds->dev, "enable",
231 						     GPIOD_OUT_LOW);
232 	if (IS_ERR(lvds->enable_gpio)) {
233 		ret = PTR_ERR(lvds->enable_gpio);
234 		dev_err(lvds->dev, "failed to request %s GPIO: %d\n",
235 			"enable", ret);
236 		return ret;
237 	}
238 
239 	lvds->reset_gpio = devm_gpiod_get_optional(lvds->dev, "reset",
240 						     GPIOD_OUT_HIGH);
241 	if (IS_ERR(lvds->reset_gpio)) {
242 		ret = PTR_ERR(lvds->reset_gpio);
243 		dev_err(lvds->dev, "failed to request %s GPIO: %d\n",
244 			"reset", ret);
245 		return ret;
246 	}
247 
248 	np = of_parse_phandle(lvds->dev->of_node, "backlight", 0);
249 	if (np) {
250 		lvds->backlight = of_find_backlight_by_node(np);
251 		of_node_put(np);
252 
253 		if (!lvds->backlight)
254 			return -EPROBE_DEFER;
255 	}
256 
257 	/*
258 	 * TODO: Handle all power supplies specified in the DT node in a generic
259 	 * way for panels that don't care about power supply ordering. LVDS
260 	 * panels that require a specific power sequence will need a dedicated
261 	 * driver.
262 	 */
263 
264 	/* Register the panel. */
265 	drm_panel_init(&lvds->panel);
266 	lvds->panel.dev = lvds->dev;
267 	lvds->panel.funcs = &panel_lvds_funcs;
268 
269 	ret = drm_panel_add(&lvds->panel);
270 	if (ret < 0)
271 		goto error;
272 
273 	dev_set_drvdata(lvds->dev, lvds);
274 	return 0;
275 
276 error:
277 	put_device(&lvds->backlight->dev);
278 	return ret;
279 }
280 
281 static int panel_lvds_remove(struct platform_device *pdev)
282 {
283 	struct panel_lvds *lvds = dev_get_drvdata(&pdev->dev);
284 
285 	drm_panel_detach(&lvds->panel);
286 	drm_panel_remove(&lvds->panel);
287 
288 	panel_lvds_disable(&lvds->panel);
289 
290 	if (lvds->backlight)
291 		put_device(&lvds->backlight->dev);
292 
293 	return 0;
294 }
295 
296 static const struct of_device_id panel_lvds_of_table[] = {
297 	{ .compatible = "panel-lvds", },
298 	{ /* Sentinel */ },
299 };
300 
301 MODULE_DEVICE_TABLE(of, panel_lvds_of_table);
302 
303 static struct platform_driver panel_lvds_driver = {
304 	.probe		= panel_lvds_probe,
305 	.remove		= panel_lvds_remove,
306 	.driver		= {
307 		.name	= "panel-lvds",
308 		.of_match_table = panel_lvds_of_table,
309 	},
310 };
311 
312 module_platform_driver(panel_lvds_driver);
313 
314 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
315 MODULE_DESCRIPTION("LVDS Panel Driver");
316 MODULE_LICENSE("GPL");
317