xref: /openbmc/linux/drivers/gpu/drm/panel/panel-lvds.c (revision 2cf1c348)
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_of.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 regulator *supply;
38 
39 	struct gpio_desc *enable_gpio;
40 	struct gpio_desc *reset_gpio;
41 
42 	enum drm_panel_orientation orientation;
43 };
44 
45 static inline struct panel_lvds *to_panel_lvds(struct drm_panel *panel)
46 {
47 	return container_of(panel, struct panel_lvds, panel);
48 }
49 
50 static int panel_lvds_unprepare(struct drm_panel *panel)
51 {
52 	struct panel_lvds *lvds = to_panel_lvds(panel);
53 
54 	if (lvds->enable_gpio)
55 		gpiod_set_value_cansleep(lvds->enable_gpio, 0);
56 
57 	if (lvds->supply)
58 		regulator_disable(lvds->supply);
59 
60 	return 0;
61 }
62 
63 static int panel_lvds_prepare(struct drm_panel *panel)
64 {
65 	struct panel_lvds *lvds = to_panel_lvds(panel);
66 
67 	if (lvds->supply) {
68 		int err;
69 
70 		err = regulator_enable(lvds->supply);
71 		if (err < 0) {
72 			dev_err(lvds->dev, "failed to enable supply: %d\n",
73 				err);
74 			return err;
75 		}
76 	}
77 
78 	if (lvds->enable_gpio)
79 		gpiod_set_value_cansleep(lvds->enable_gpio, 1);
80 
81 	return 0;
82 }
83 
84 static int panel_lvds_get_modes(struct drm_panel *panel,
85 				struct drm_connector *connector)
86 {
87 	struct panel_lvds *lvds = to_panel_lvds(panel);
88 	struct drm_display_mode *mode;
89 
90 	mode = drm_mode_create(connector->dev);
91 	if (!mode)
92 		return 0;
93 
94 	drm_display_mode_from_videomode(&lvds->video_mode, mode);
95 	mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
96 	drm_mode_probed_add(connector, mode);
97 
98 	connector->display_info.width_mm = lvds->width;
99 	connector->display_info.height_mm = lvds->height;
100 	drm_display_info_set_bus_formats(&connector->display_info,
101 					 &lvds->bus_format, 1);
102 	connector->display_info.bus_flags = lvds->data_mirror
103 					  ? DRM_BUS_FLAG_DATA_LSB_TO_MSB
104 					  : DRM_BUS_FLAG_DATA_MSB_TO_LSB;
105 	drm_connector_set_panel_orientation(connector, lvds->orientation);
106 
107 	return 1;
108 }
109 
110 static const struct drm_panel_funcs panel_lvds_funcs = {
111 	.unprepare = panel_lvds_unprepare,
112 	.prepare = panel_lvds_prepare,
113 	.get_modes = panel_lvds_get_modes,
114 };
115 
116 static int panel_lvds_parse_dt(struct panel_lvds *lvds)
117 {
118 	struct device_node *np = lvds->dev->of_node;
119 	struct display_timing timing;
120 	int ret;
121 
122 	ret = of_drm_get_panel_orientation(np, &lvds->orientation);
123 	if (ret < 0) {
124 		dev_err(lvds->dev, "%pOF: failed to get orientation %d\n", np, ret);
125 		return ret;
126 	}
127 
128 	ret = of_get_display_timing(np, "panel-timing", &timing);
129 	if (ret < 0) {
130 		dev_err(lvds->dev, "%pOF: problems parsing panel-timing (%d)\n",
131 			np, ret);
132 		return ret;
133 	}
134 
135 	videomode_from_timing(&timing, &lvds->video_mode);
136 
137 	ret = of_property_read_u32(np, "width-mm", &lvds->width);
138 	if (ret < 0) {
139 		dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
140 			np, "width-mm");
141 		return -ENODEV;
142 	}
143 	ret = of_property_read_u32(np, "height-mm", &lvds->height);
144 	if (ret < 0) {
145 		dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
146 			np, "height-mm");
147 		return -ENODEV;
148 	}
149 
150 	of_property_read_string(np, "label", &lvds->label);
151 
152 	ret = drm_of_lvds_get_data_mapping(np);
153 	if (ret < 0) {
154 		dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
155 			np, "data-mapping");
156 		return ret;
157 	}
158 
159 	lvds->bus_format = ret;
160 
161 	lvds->data_mirror = of_property_read_bool(np, "data-mirror");
162 
163 	return 0;
164 }
165 
166 static int panel_lvds_probe(struct platform_device *pdev)
167 {
168 	struct panel_lvds *lvds;
169 	int ret;
170 
171 	lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL);
172 	if (!lvds)
173 		return -ENOMEM;
174 
175 	lvds->dev = &pdev->dev;
176 
177 	ret = panel_lvds_parse_dt(lvds);
178 	if (ret < 0)
179 		return ret;
180 
181 	lvds->supply = devm_regulator_get_optional(lvds->dev, "power");
182 	if (IS_ERR(lvds->supply)) {
183 		ret = PTR_ERR(lvds->supply);
184 
185 		if (ret != -ENODEV) {
186 			if (ret != -EPROBE_DEFER)
187 				dev_err(lvds->dev, "failed to request regulator: %d\n",
188 					ret);
189 			return ret;
190 		}
191 
192 		lvds->supply = NULL;
193 	}
194 
195 	/* Get GPIOs and backlight controller. */
196 	lvds->enable_gpio = devm_gpiod_get_optional(lvds->dev, "enable",
197 						     GPIOD_OUT_LOW);
198 	if (IS_ERR(lvds->enable_gpio)) {
199 		ret = PTR_ERR(lvds->enable_gpio);
200 		dev_err(lvds->dev, "failed to request %s GPIO: %d\n",
201 			"enable", ret);
202 		return ret;
203 	}
204 
205 	lvds->reset_gpio = devm_gpiod_get_optional(lvds->dev, "reset",
206 						     GPIOD_OUT_HIGH);
207 	if (IS_ERR(lvds->reset_gpio)) {
208 		ret = PTR_ERR(lvds->reset_gpio);
209 		dev_err(lvds->dev, "failed to request %s GPIO: %d\n",
210 			"reset", ret);
211 		return ret;
212 	}
213 
214 	/*
215 	 * TODO: Handle all power supplies specified in the DT node in a generic
216 	 * way for panels that don't care about power supply ordering. LVDS
217 	 * panels that require a specific power sequence will need a dedicated
218 	 * driver.
219 	 */
220 
221 	/* Register the panel. */
222 	drm_panel_init(&lvds->panel, lvds->dev, &panel_lvds_funcs,
223 		       DRM_MODE_CONNECTOR_LVDS);
224 
225 	ret = drm_panel_of_backlight(&lvds->panel);
226 	if (ret)
227 		return ret;
228 
229 	drm_panel_add(&lvds->panel);
230 
231 	dev_set_drvdata(lvds->dev, lvds);
232 	return 0;
233 }
234 
235 static int panel_lvds_remove(struct platform_device *pdev)
236 {
237 	struct panel_lvds *lvds = platform_get_drvdata(pdev);
238 
239 	drm_panel_remove(&lvds->panel);
240 
241 	drm_panel_disable(&lvds->panel);
242 
243 	return 0;
244 }
245 
246 static const struct of_device_id panel_lvds_of_table[] = {
247 	{ .compatible = "panel-lvds", },
248 	{ /* Sentinel */ },
249 };
250 
251 MODULE_DEVICE_TABLE(of, panel_lvds_of_table);
252 
253 static struct platform_driver panel_lvds_driver = {
254 	.probe		= panel_lvds_probe,
255 	.remove		= panel_lvds_remove,
256 	.driver		= {
257 		.name	= "panel-lvds",
258 		.of_match_table = panel_lvds_of_table,
259 	},
260 };
261 
262 module_platform_driver(panel_lvds_driver);
263 
264 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
265 MODULE_DESCRIPTION("LVDS Panel Driver");
266 MODULE_LICENSE("GPL");
267