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