]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/panel/panel-simple.c
arm: imx6: defconfig: update tx6 defconfigs
[karo-tx-linux.git] / drivers / gpu / drm / panel / panel-simple.c
1 /*
2  * Copyright (C) 2013, NVIDIA Corporation.  All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sub license,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include <linux/backlight.h>
25 #include <linux/gpio.h>
26 #include <linux/module.h>
27 #include <linux/of_gpio.h>
28 #include <linux/of_platform.h>
29 #include <linux/platform_device.h>
30 #include <linux/regulator/consumer.h>
31
32 #include <drm/drm_crtc.h>
33 #include <drm/drm_panel.h>
34
35 struct panel_desc {
36         const struct drm_display_mode *modes;
37         unsigned int num_modes;
38
39         struct {
40                 unsigned int width;
41                 unsigned int height;
42         } size;
43 };
44
45 /* TODO: convert to gpiod_*() API once it's been merged */
46 #define GPIO_ACTIVE_LOW (1 << 0)
47
48 struct panel_simple {
49         struct drm_panel base;
50         bool enabled;
51
52         const struct panel_desc *desc;
53
54         struct backlight_device *backlight;
55         struct regulator *supply;
56
57         unsigned long enable_gpio_flags;
58         int enable_gpio;
59 };
60
61 static inline struct panel_simple *to_panel_simple(struct drm_panel *panel)
62 {
63         return container_of(panel, struct panel_simple, base);
64 }
65
66 static void panel_simple_disable(struct drm_panel *panel)
67 {
68         struct panel_simple *p = to_panel_simple(panel);
69
70         if (!p->enabled)
71                 return;
72
73         if (p->backlight) {
74                 p->backlight->props.power = FB_BLANK_POWERDOWN;
75                 backlight_update_status(p->backlight);
76         }
77
78         if (gpio_is_valid(p->enable_gpio)) {
79                 if (p->enable_gpio_flags & GPIO_ACTIVE_LOW)
80                         gpio_set_value(p->enable_gpio, 1);
81                 else
82                         gpio_set_value(p->enable_gpio, 0);
83         }
84
85         regulator_disable(p->supply);
86         p->enabled = false;
87 }
88
89 static void panel_simple_enable(struct drm_panel *panel)
90 {
91         struct panel_simple *p = to_panel_simple(panel);
92         int err;
93
94         if (p->enabled)
95                 return;
96
97         err = regulator_enable(p->supply);
98         if (err < 0)
99                 dev_err(panel->dev, "failed to enable supply: %d\n", err);
100
101         if (gpio_is_valid(p->enable_gpio)) {
102                 if (p->enable_gpio_flags & GPIO_ACTIVE_LOW)
103                         gpio_set_value(p->enable_gpio, 0);
104                 else
105                         gpio_set_value(p->enable_gpio, 1);
106         }
107
108         if (p->backlight) {
109                 p->backlight->props.power = FB_BLANK_UNBLANK;
110                 backlight_update_status(p->backlight);
111         }
112
113         p->enabled = true;
114 }
115
116 static int panel_simple_get_modes(struct drm_panel *panel)
117 {
118         struct panel_simple *p = to_panel_simple(panel);
119         struct drm_display_mode *mode;
120         unsigned int i;
121
122         for (i = 0; i < p->desc->num_modes; i++) {
123                 mode = drm_mode_duplicate(panel->drm, &p->desc->modes[i]);
124                 if (!mode)
125                         return -ENOMEM;
126
127                 drm_mode_set_name(mode);
128
129                 drm_mode_probed_add(panel->connector, mode);
130         }
131
132         return p->desc->num_modes;
133 }
134
135 static const struct drm_panel_funcs panel_simple_funcs = {
136         .disable = panel_simple_disable,
137         .enable = panel_simple_enable,
138         .get_modes = panel_simple_get_modes,
139 };
140
141 static const struct drm_display_mode auo_b101aw03_mode = {
142         .clock = 51450,
143         .hdisplay = 1024,
144         .hsync_start = 1024 + 156,
145         .hsync_end = 1024 + 156 + 8,
146         .htotal = 1024 + 156 + 8 + 156,
147         .vdisplay = 600,
148         .vsync_start = 600 + 16,
149         .vsync_end = 600 + 16 + 6,
150         .vtotal = 600 + 16 + 6 + 16,
151         .vrefresh = 60,
152 };
153
154 static const struct panel_desc auo_b101aw03 = {
155         .modes = &auo_b101aw03_mode,
156         .num_modes = 1,
157         .size = {
158                 .width = 223,
159                 .height = 125,
160         },
161 };
162
163 static const struct drm_display_mode chunghwa_claa101wb01_mode = {
164         .clock = 69300,
165         .hdisplay = 1366,
166         .hsync_start = 1366 + 48,
167         .hsync_end = 1366 + 48 + 32,
168         .htotal = 1366 + 48 + 32 + 20,
169         .vdisplay = 768,
170         .vsync_start = 768 + 16,
171         .vsync_end = 768 + 16 + 8,
172         .vtotal = 768 + 16 + 8 + 16,
173         .vrefresh = 60,
174 };
175
176 static const struct panel_desc chunghwa_claa101wb01 = {
177         .modes = &chunghwa_claa101wb01_mode,
178         .num_modes = 1,
179         .size = {
180                 .width = 223,
181                 .height = 125,
182         },
183 };
184
185 static const struct drm_display_mode panasonic_vvx10f004b00_mode = {
186         .clock = 154700,
187         .hdisplay = 1920,
188         .hsync_start = 1920 + 154,
189         .hsync_end = 1920 + 154 + 16,
190         .htotal = 1920 + 154 + 16 + 32,
191         .vdisplay = 1200,
192         .vsync_start = 1200 + 17,
193         .vsync_end = 1200 + 17 + 2,
194         .vtotal = 1200 + 17 + 2 + 16,
195         .vrefresh = 60,
196 };
197
198 static const struct panel_desc panasonic_vvx10f004b00 = {
199         .modes = &panasonic_vvx10f004b00_mode,
200         .num_modes = 1,
201         .size = {
202                 .width = 217,
203                 .height = 136,
204         },
205 };
206
207 static const struct of_device_id panel_simple_of_match[] = {
208         {
209                 .compatible = "auo,b101aw03",
210                 .data = &auo_b101aw03,
211         }, {
212                 .compatible = "chunghwa,claa101wb01",
213                 .data = &chunghwa_claa101wb01
214         }, {
215                 .compatible = "panasonic,vvx10f004b00",
216                 .data = &panasonic_vvx10f004b00
217         }, {
218                 /* sentinel */
219         }
220 };
221 MODULE_DEVICE_TABLE(of, panel_simple_of_match);
222
223 static int panel_simple_probe(struct platform_device *pdev)
224 {
225         const struct of_device_id *id;
226         struct device_node *backlight;
227         struct panel_simple *panel;
228         enum of_gpio_flags flags;
229         int err;
230
231         panel = devm_kzalloc(&pdev->dev, sizeof(*panel), GFP_KERNEL);
232         if (!panel)
233                 return -ENOMEM;
234
235         id = of_match_node(panel_simple_of_match, pdev->dev.of_node);
236         if (!id)
237                 return -ENODEV;
238
239         panel->desc = id->data;
240         panel->enabled = false;
241
242         panel->supply = devm_regulator_get(&pdev->dev, "power");
243         if (IS_ERR(panel->supply))
244                 return PTR_ERR(panel->supply);
245
246         panel->enable_gpio = of_get_named_gpio_flags(pdev->dev.of_node,
247                                                      "enable-gpios", 0,
248                                                      &flags);
249         if (gpio_is_valid(panel->enable_gpio)) {
250                 unsigned int value;
251
252                 if (flags & OF_GPIO_ACTIVE_LOW)
253                         panel->enable_gpio_flags |= GPIO_ACTIVE_LOW;
254
255                 err = gpio_request(panel->enable_gpio, "enable");
256                 if (err < 0) {
257                         dev_err(&pdev->dev, "failed to request GPIO#%u: %d\n",
258                                 panel->enable_gpio, err);
259                         return err;
260                 }
261
262                 value = (panel->enable_gpio_flags & GPIO_ACTIVE_LOW) != 0;
263
264                 err = gpio_direction_output(panel->enable_gpio, value);
265                 if (err < 0) {
266                         dev_err(&pdev->dev, "failed to setup GPIO%u: %d\n",
267                                 panel->enable_gpio, err);
268                         goto free_gpio;
269                 }
270         }
271
272         backlight = of_parse_phandle(pdev->dev.of_node, "backlight", 0);
273         if (backlight) {
274                 panel->backlight = of_find_backlight_by_node(backlight);
275                 if (!panel->backlight) {
276                         err = -EPROBE_DEFER;
277                         goto free_gpio;
278                 }
279
280                 of_node_put(backlight);
281         }
282
283         drm_panel_init(&panel->base);
284         panel->base.dev = &pdev->dev;
285         panel->base.funcs = &panel_simple_funcs;
286
287         err = drm_panel_add(&panel->base);
288         if (err < 0)
289                 goto free_gpio;
290
291         platform_set_drvdata(pdev, panel);
292
293         return 0;
294
295 free_gpio:
296         if (gpio_is_valid(panel->enable_gpio))
297                 gpio_free(panel->enable_gpio);
298
299         return err;
300 }
301
302 static int panel_simple_remove(struct platform_device *pdev)
303 {
304         struct panel_simple *panel = platform_get_drvdata(pdev);
305
306         drm_panel_detach(&panel->base);
307         drm_panel_remove(&panel->base);
308
309         panel_simple_disable(&panel->base);
310
311         if (panel->backlight)
312                 put_device(&panel->backlight->dev);
313
314         if (gpio_is_valid(panel->enable_gpio))
315                 gpio_free(panel->enable_gpio);
316
317         regulator_disable(panel->supply);
318
319         return 0;
320 }
321
322 static struct platform_driver panel_simple_driver = {
323         .driver = {
324                 .name = "panel-simple",
325                 .owner = THIS_MODULE,
326                 .of_match_table = panel_simple_of_match,
327         },
328         .probe = panel_simple_probe,
329         .remove = panel_simple_remove,
330 };
331 module_platform_driver(panel_simple_driver);
332
333 MODULE_DESCRIPTION("DRM Driver for Simple Panels");
334 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
335 MODULE_LICENSE("GPL v2");