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