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