blob: 27a1c9923b09ef3b6189e0a965e743b39d97f2a8 [file] [log] [blame]
Kuninori Morimotod7cfd252018-08-06 03:18:22 +00001// SPDX-License-Identifier: GPL-2.0+
Laurent Pinchart7c9dff52016-11-18 02:58:18 +02002/*
Baruch Siach6abd061d2018-01-22 18:01:42 +02003 * Generic LVDS panel driver
Laurent Pinchart7c9dff52016-11-18 02:58:18 +02004 *
5 * Copyright (C) 2016 Laurent Pinchart
6 * Copyright (C) 2016 Renesas Electronics Corporation
7 *
8 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
Laurent Pinchart7c9dff52016-11-18 02:58:18 +02009 */
10
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020011#include <linux/gpio/consumer.h>
12#include <linux/module.h>
13#include <linux/of_platform.h>
14#include <linux/platform_device.h>
Maxime Riparda1c55bc2017-12-21 12:02:28 +010015#include <linux/regulator/consumer.h>
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020016#include <linux/slab.h>
17
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020018#include <video/display_timing.h>
19#include <video/of_display_timing.h>
20#include <video/videomode.h>
21
Sam Ravnborgcb23eae2019-05-26 20:05:32 +020022#include <drm/drm_crtc.h>
Marek Vasut7c4dd0a2021-10-13 00:42:52 +020023#include <drm/drm_of.h>
Sam Ravnborgcb23eae2019-05-26 20:05:32 +020024#include <drm/drm_panel.h>
25
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020026struct 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
Maxime Riparda1c55bc2017-12-21 12:02:28 +010037 struct regulator *supply;
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020038
39 struct gpio_desc *enable_gpio;
40 struct gpio_desc *reset_gpio;
Dmitry Osipenko58310c2e2020-08-14 00:56:08 +030041
42 enum drm_panel_orientation orientation;
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020043};
44
45static inline struct panel_lvds *to_panel_lvds(struct drm_panel *panel)
46{
47 return container_of(panel, struct panel_lvds, panel);
48}
49
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020050static 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
Maxime Riparda1c55bc2017-12-21 12:02:28 +010057 if (lvds->supply)
58 regulator_disable(lvds->supply);
59
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020060 return 0;
61}
62
63static int panel_lvds_prepare(struct drm_panel *panel)
64{
65 struct panel_lvds *lvds = to_panel_lvds(panel);
66
Maxime Riparda1c55bc2017-12-21 12:02:28 +010067 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
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020078 if (lvds->enable_gpio)
79 gpiod_set_value_cansleep(lvds->enable_gpio, 1);
80
81 return 0;
82}
83
Sam Ravnborg0ce8ddd2019-12-07 15:03:33 +010084static int panel_lvds_get_modes(struct drm_panel *panel,
85 struct drm_connector *connector)
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020086{
87 struct panel_lvds *lvds = to_panel_lvds(panel);
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020088 struct drm_display_mode *mode;
89
Sam Ravnborgaa6c4362019-12-07 15:03:35 +010090 mode = drm_mode_create(connector->dev);
Laurent Pinchart7c9dff52016-11-18 02:58:18 +020091 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;
Dmitry Osipenko58310c2e2020-08-14 00:56:08 +0300105 drm_connector_set_panel_orientation(connector, lvds->orientation);
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200106
107 return 1;
108}
109
110static const struct drm_panel_funcs panel_lvds_funcs = {
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200111 .unprepare = panel_lvds_unprepare,
112 .prepare = panel_lvds_prepare,
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200113 .get_modes = panel_lvds_get_modes,
114};
115
116static int panel_lvds_parse_dt(struct panel_lvds *lvds)
117{
118 struct device_node *np = lvds->dev->of_node;
119 struct display_timing timing;
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200120 int ret;
121
Dmitry Osipenko58310c2e2020-08-14 00:56:08 +0300122 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
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200128 ret = of_get_display_timing(np, "panel-timing", &timing);
Douglas Andersonc2d7e472019-07-22 11:24:38 -0700129 if (ret < 0) {
130 dev_err(lvds->dev, "%pOF: problems parsing panel-timing (%d)\n",
131 np, ret);
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200132 return ret;
Douglas Andersonc2d7e472019-07-22 11:24:38 -0700133 }
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200134
135 videomode_from_timing(&timing, &lvds->video_mode);
136
137 ret = of_property_read_u32(np, "width-mm", &lvds->width);
138 if (ret < 0) {
Rob Herring4bf99142017-07-18 16:43:04 -0500139 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
140 np, "width-mm");
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200141 return -ENODEV;
142 }
143 ret = of_property_read_u32(np, "height-mm", &lvds->height);
144 if (ret < 0) {
Rob Herring4bf99142017-07-18 16:43:04 -0500145 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
146 np, "height-mm");
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200147 return -ENODEV;
148 }
149
150 of_property_read_string(np, "label", &lvds->label);
151
Marek Vasut7c4dd0a2021-10-13 00:42:52 +0200152 ret = drm_of_lvds_get_data_mapping(np);
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200153 if (ret < 0) {
Rob Herring4bf99142017-07-18 16:43:04 -0500154 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n",
155 np, "data-mapping");
Marek Vasut7c4dd0a2021-10-13 00:42:52 +0200156 return ret;
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200157 }
158
Marek Vasut7c4dd0a2021-10-13 00:42:52 +0200159 lvds->bus_format = ret;
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200160
161 lvds->data_mirror = of_property_read_bool(np, "data-mirror");
162
163 return 0;
164}
165
166static int panel_lvds_probe(struct platform_device *pdev)
167{
168 struct panel_lvds *lvds;
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200169 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
Maxime Riparda1c55bc2017-12-21 12:02:28 +0100181 lvds->supply = devm_regulator_get_optional(lvds->dev, "power");
182 if (IS_ERR(lvds->supply)) {
183 ret = PTR_ERR(lvds->supply);
Maxime Riparda0d60532018-01-10 16:59:41 +0100184
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;
Maxime Riparda1c55bc2017-12-21 12:02:28 +0100193 }
194
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200195 /* 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
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200214 /*
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. */
Laurent Pinchart9a2654c2019-09-04 16:28:03 +0300222 drm_panel_init(&lvds->panel, lvds->dev, &panel_lvds_funcs,
223 DRM_MODE_CONNECTOR_LVDS);
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200224
Sam Ravnborg17d51832019-12-07 15:03:40 +0100225 ret = drm_panel_of_backlight(&lvds->panel);
226 if (ret)
227 return ret;
228
Bernard Zhaoc3ee8c62020-08-01 20:02:13 +0800229 drm_panel_add(&lvds->panel);
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200230
231 dev_set_drvdata(lvds->dev, lvds);
232 return 0;
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200233}
234
235static int panel_lvds_remove(struct platform_device *pdev)
236{
Julia Lawall5dd331d2021-02-09 22:13:04 +0100237 struct panel_lvds *lvds = platform_get_drvdata(pdev);
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200238
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200239 drm_panel_remove(&lvds->panel);
240
Sam Ravnborg17d51832019-12-07 15:03:40 +0100241 drm_panel_disable(&lvds->panel);
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200242
Laurent Pinchart7c9dff52016-11-18 02:58:18 +0200243 return 0;
244}
245
246static const struct of_device_id panel_lvds_of_table[] = {
247 { .compatible = "panel-lvds", },
248 { /* Sentinel */ },
249};
250
251MODULE_DEVICE_TABLE(of, panel_lvds_of_table);
252
253static 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
262module_platform_driver(panel_lvds_driver);
263
264MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
265MODULE_DESCRIPTION("LVDS Panel Driver");
266MODULE_LICENSE("GPL");