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