xref: /openbmc/linux/drivers/gpu/drm/panel/panel-lvds.c (revision 234489ac)
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 drm_display_mode dmode;
34 	u32 bus_flags;
35 	unsigned int bus_format;
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_duplicate(connector->dev, &lvds->dmode);
91 	if (!mode)
92 		return 0;
93 
94 	mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
95 	drm_mode_probed_add(connector, mode);
96 
97 	connector->display_info.width_mm = lvds->dmode.width_mm;
98 	connector->display_info.height_mm = lvds->dmode.height_mm;
99 	drm_display_info_set_bus_formats(&connector->display_info,
100 					 &lvds->bus_format, 1);
101 	connector->display_info.bus_flags = lvds->bus_flags;
102 
103 	/*
104 	 * TODO: Remove once all drm drivers call
105 	 * drm_connector_set_orientation_from_panel()
106 	 */
107 	drm_connector_set_panel_orientation(connector, lvds->orientation);
108 
109 	return 1;
110 }
111 
112 static enum drm_panel_orientation panel_lvds_get_orientation(struct drm_panel *panel)
113 {
114 	struct panel_lvds *lvds = to_panel_lvds(panel);
115 
116 	return lvds->orientation;
117 }
118 
119 static const struct drm_panel_funcs panel_lvds_funcs = {
120 	.unprepare = panel_lvds_unprepare,
121 	.prepare = panel_lvds_prepare,
122 	.get_modes = panel_lvds_get_modes,
123 	.get_orientation = panel_lvds_get_orientation,
124 };
125 
126 static int panel_lvds_parse_dt(struct panel_lvds *lvds)
127 {
128 	struct device_node *np = lvds->dev->of_node;
129 	int ret;
130 
131 	ret = of_drm_get_panel_orientation(np, &lvds->orientation);
132 	if (ret < 0) {
133 		dev_err(lvds->dev, "%pOF: failed to get orientation %d\n", np, ret);
134 		return ret;
135 	}
136 
137 	ret = of_get_drm_panel_display_mode(np, &lvds->dmode, &lvds->bus_flags);
138 	if (ret < 0) {
139 		dev_err(lvds->dev, "%pOF: problems parsing panel-timing (%d)\n",
140 			np, ret);
141 		return ret;
142 	}
143 
144 	of_property_read_string(np, "label", &lvds->label);
145 
146 	ret = drm_of_lvds_get_data_mapping(np);
147 	if (ret < 0) {
148 		dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
149 			np, "data-mapping");
150 		return ret;
151 	}
152 
153 	lvds->bus_format = ret;
154 
155 	lvds->bus_flags |= of_property_read_bool(np, "data-mirror") ?
156 			   DRM_BUS_FLAG_DATA_LSB_TO_MSB :
157 			   DRM_BUS_FLAG_DATA_MSB_TO_LSB;
158 
159 	return 0;
160 }
161 
162 static int panel_lvds_probe(struct platform_device *pdev)
163 {
164 	struct panel_lvds *lvds;
165 	int ret;
166 
167 	lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL);
168 	if (!lvds)
169 		return -ENOMEM;
170 
171 	lvds->dev = &pdev->dev;
172 
173 	ret = panel_lvds_parse_dt(lvds);
174 	if (ret < 0)
175 		return ret;
176 
177 	lvds->supply = devm_regulator_get_optional(lvds->dev, "power");
178 	if (IS_ERR(lvds->supply)) {
179 		ret = PTR_ERR(lvds->supply);
180 
181 		if (ret != -ENODEV) {
182 			if (ret != -EPROBE_DEFER)
183 				dev_err(lvds->dev, "failed to request regulator: %d\n",
184 					ret);
185 			return ret;
186 		}
187 
188 		lvds->supply = NULL;
189 	}
190 
191 	/* Get GPIOs and backlight controller. */
192 	lvds->enable_gpio = devm_gpiod_get_optional(lvds->dev, "enable",
193 						     GPIOD_OUT_LOW);
194 	if (IS_ERR(lvds->enable_gpio)) {
195 		ret = PTR_ERR(lvds->enable_gpio);
196 		dev_err(lvds->dev, "failed to request %s GPIO: %d\n",
197 			"enable", ret);
198 		return ret;
199 	}
200 
201 	lvds->reset_gpio = devm_gpiod_get_optional(lvds->dev, "reset",
202 						     GPIOD_OUT_HIGH);
203 	if (IS_ERR(lvds->reset_gpio)) {
204 		ret = PTR_ERR(lvds->reset_gpio);
205 		dev_err(lvds->dev, "failed to request %s GPIO: %d\n",
206 			"reset", ret);
207 		return ret;
208 	}
209 
210 	/*
211 	 * TODO: Handle all power supplies specified in the DT node in a generic
212 	 * way for panels that don't care about power supply ordering. LVDS
213 	 * panels that require a specific power sequence will need a dedicated
214 	 * driver.
215 	 */
216 
217 	/* Register the panel. */
218 	drm_panel_init(&lvds->panel, lvds->dev, &panel_lvds_funcs,
219 		       DRM_MODE_CONNECTOR_LVDS);
220 
221 	ret = drm_panel_of_backlight(&lvds->panel);
222 	if (ret)
223 		return ret;
224 
225 	drm_panel_add(&lvds->panel);
226 
227 	dev_set_drvdata(lvds->dev, lvds);
228 	return 0;
229 }
230 
231 static int panel_lvds_remove(struct platform_device *pdev)
232 {
233 	struct panel_lvds *lvds = platform_get_drvdata(pdev);
234 
235 	drm_panel_remove(&lvds->panel);
236 
237 	drm_panel_disable(&lvds->panel);
238 
239 	return 0;
240 }
241 
242 static const struct of_device_id panel_lvds_of_table[] = {
243 	{ .compatible = "panel-lvds", },
244 	{ /* Sentinel */ },
245 };
246 
247 MODULE_DEVICE_TABLE(of, panel_lvds_of_table);
248 
249 static struct platform_driver panel_lvds_driver = {
250 	.probe		= panel_lvds_probe,
251 	.remove		= panel_lvds_remove,
252 	.driver		= {
253 		.name	= "panel-lvds",
254 		.of_match_table = panel_lvds_of_table,
255 	},
256 };
257 
258 module_platform_driver(panel_lvds_driver);
259 
260 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
261 MODULE_DESCRIPTION("LVDS Panel Driver");
262 MODULE_LICENSE("GPL");
263