xref: /openbmc/linux/drivers/gpu/drm/panel/panel-lvds.c (revision f8e17c17)
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/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>
17 
18 #include <video/display_timing.h>
19 #include <video/of_display_timing.h>
20 #include <video/videomode.h>
21 
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_panel.h>
24 
25 struct panel_lvds {
26 	struct drm_panel panel;
27 	struct device *dev;
28 
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;
35 
36 	struct regulator *supply;
37 
38 	struct gpio_desc *enable_gpio;
39 	struct gpio_desc *reset_gpio;
40 };
41 
42 static inline struct panel_lvds *to_panel_lvds(struct drm_panel *panel)
43 {
44 	return container_of(panel, struct panel_lvds, panel);
45 }
46 
47 static int panel_lvds_unprepare(struct drm_panel *panel)
48 {
49 	struct panel_lvds *lvds = to_panel_lvds(panel);
50 
51 	if (lvds->enable_gpio)
52 		gpiod_set_value_cansleep(lvds->enable_gpio, 0);
53 
54 	if (lvds->supply)
55 		regulator_disable(lvds->supply);
56 
57 	return 0;
58 }
59 
60 static int panel_lvds_prepare(struct drm_panel *panel)
61 {
62 	struct panel_lvds *lvds = to_panel_lvds(panel);
63 
64 	if (lvds->supply) {
65 		int err;
66 
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;
72 		}
73 	}
74 
75 	if (lvds->enable_gpio)
76 		gpiod_set_value_cansleep(lvds->enable_gpio, 1);
77 
78 	return 0;
79 }
80 
81 static int panel_lvds_get_modes(struct drm_panel *panel,
82 				struct drm_connector *connector)
83 {
84 	struct panel_lvds *lvds = to_panel_lvds(panel);
85 	struct drm_display_mode *mode;
86 
87 	mode = drm_mode_create(connector->dev);
88 	if (!mode)
89 		return 0;
90 
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);
94 
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;
102 
103 	return 1;
104 }
105 
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,
110 };
111 
112 static int panel_lvds_parse_dt(struct panel_lvds *lvds)
113 {
114 	struct device_node *np = lvds->dev->of_node;
115 	struct display_timing timing;
116 	const char *mapping;
117 	int ret;
118 
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;
124 	}
125 
126 	videomode_from_timing(&timing, &lvds->video_mode);
127 
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;
133 	}
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;
139 	}
140 
141 	of_property_read_string(np, "label", &lvds->label);
142 
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;
148 	}
149 
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;
160 	}
161 
162 	lvds->data_mirror = of_property_read_bool(np, "data-mirror");
163 
164 	return 0;
165 }
166 
167 static int panel_lvds_probe(struct platform_device *pdev)
168 {
169 	struct panel_lvds *lvds;
170 	int ret;
171 
172 	lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL);
173 	if (!lvds)
174 		return -ENOMEM;
175 
176 	lvds->dev = &pdev->dev;
177 
178 	ret = panel_lvds_parse_dt(lvds);
179 	if (ret < 0)
180 		return ret;
181 
182 	lvds->supply = devm_regulator_get_optional(lvds->dev, "power");
183 	if (IS_ERR(lvds->supply)) {
184 		ret = PTR_ERR(lvds->supply);
185 
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;
191 		}
192 
193 		lvds->supply = NULL;
194 	}
195 
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;
204 	}
205 
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;
213 	}
214 
215 	/*
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.
220 	 */
221 
222 	/* Register the panel. */
223 	drm_panel_init(&lvds->panel, lvds->dev, &panel_lvds_funcs,
224 		       DRM_MODE_CONNECTOR_LVDS);
225 
226 	ret = drm_panel_of_backlight(&lvds->panel);
227 	if (ret)
228 		return ret;
229 
230 	ret = drm_panel_add(&lvds->panel);
231 	if (ret < 0)
232 		return ret;
233 
234 	dev_set_drvdata(lvds->dev, lvds);
235 	return 0;
236 }
237 
238 static int panel_lvds_remove(struct platform_device *pdev)
239 {
240 	struct panel_lvds *lvds = dev_get_drvdata(&pdev->dev);
241 
242 	drm_panel_remove(&lvds->panel);
243 
244 	drm_panel_disable(&lvds->panel);
245 
246 	return 0;
247 }
248 
249 static const struct of_device_id panel_lvds_of_table[] = {
250 	{ .compatible = "panel-lvds", },
251 	{ /* Sentinel */ },
252 };
253 
254 MODULE_DEVICE_TABLE(of, panel_lvds_of_table);
255 
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,
262 	},
263 };
264 
265 module_platform_driver(panel_lvds_driver);
266 
267 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
268 MODULE_DESCRIPTION("LVDS Panel Driver");
269 MODULE_LICENSE("GPL");
270