drm/panel: Add support for Panasonic VVX10F004B0
[firefly-linux-kernel-4.4.55.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/drmP.h>
33 #include <drm/drm_crtc.h>
34 #include <drm/drm_mipi_dsi.h>
35 #include <drm/drm_panel.h>
36
37 struct panel_desc {
38         const struct drm_display_mode *modes;
39         unsigned int num_modes;
40
41         struct {
42                 unsigned int width;
43                 unsigned int height;
44         } size;
45 };
46
47 /* TODO: convert to gpiod_*() API once it's been merged */
48 #define GPIO_ACTIVE_LOW (1 << 0)
49
50 struct panel_simple {
51         struct drm_panel base;
52         bool enabled;
53
54         const struct panel_desc *desc;
55
56         struct backlight_device *backlight;
57         struct regulator *supply;
58         struct i2c_adapter *ddc;
59
60         unsigned long enable_gpio_flags;
61         int enable_gpio;
62 };
63
64 static inline struct panel_simple *to_panel_simple(struct drm_panel *panel)
65 {
66         return container_of(panel, struct panel_simple, base);
67 }
68
69 static int panel_simple_get_fixed_modes(struct panel_simple *panel)
70 {
71         struct drm_connector *connector = panel->base.connector;
72         struct drm_device *drm = panel->base.drm;
73         struct drm_display_mode *mode;
74         unsigned int i, num = 0;
75
76         if (!panel->desc)
77                 return 0;
78
79         for (i = 0; i < panel->desc->num_modes; i++) {
80                 const struct drm_display_mode *m = &panel->desc->modes[i];
81
82                 mode = drm_mode_duplicate(drm, m);
83                 if (!mode) {
84                         dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
85                                 m->hdisplay, m->vdisplay, m->vrefresh);
86                         continue;
87                 }
88
89                 drm_mode_set_name(mode);
90
91                 drm_mode_probed_add(connector, mode);
92                 num++;
93         }
94
95         connector->display_info.width_mm = panel->desc->size.width;
96         connector->display_info.height_mm = panel->desc->size.height;
97
98         return num;
99 }
100
101 static int panel_simple_disable(struct drm_panel *panel)
102 {
103         struct panel_simple *p = to_panel_simple(panel);
104
105         if (!p->enabled)
106                 return 0;
107
108         if (p->backlight) {
109                 p->backlight->props.power = FB_BLANK_POWERDOWN;
110                 backlight_update_status(p->backlight);
111         }
112
113         if (gpio_is_valid(p->enable_gpio)) {
114                 if (p->enable_gpio_flags & GPIO_ACTIVE_LOW)
115                         gpio_set_value(p->enable_gpio, 1);
116                 else
117                         gpio_set_value(p->enable_gpio, 0);
118         }
119
120         regulator_disable(p->supply);
121         p->enabled = false;
122
123         return 0;
124 }
125
126 static int panel_simple_enable(struct drm_panel *panel)
127 {
128         struct panel_simple *p = to_panel_simple(panel);
129         int err;
130
131         if (p->enabled)
132                 return 0;
133
134         err = regulator_enable(p->supply);
135         if (err < 0) {
136                 dev_err(panel->dev, "failed to enable supply: %d\n", err);
137                 return err;
138         }
139
140         if (gpio_is_valid(p->enable_gpio)) {
141                 if (p->enable_gpio_flags & GPIO_ACTIVE_LOW)
142                         gpio_set_value(p->enable_gpio, 0);
143                 else
144                         gpio_set_value(p->enable_gpio, 1);
145         }
146
147         if (p->backlight) {
148                 p->backlight->props.power = FB_BLANK_UNBLANK;
149                 backlight_update_status(p->backlight);
150         }
151
152         p->enabled = true;
153
154         return 0;
155 }
156
157 static int panel_simple_get_modes(struct drm_panel *panel)
158 {
159         struct panel_simple *p = to_panel_simple(panel);
160         int num = 0;
161
162         /* probe EDID if a DDC bus is available */
163         if (p->ddc) {
164                 struct edid *edid = drm_get_edid(panel->connector, p->ddc);
165                 if (edid) {
166                         num += drm_add_edid_modes(panel->connector, edid);
167                         kfree(edid);
168                 }
169         }
170
171         /* add hard-coded panel modes */
172         num += panel_simple_get_fixed_modes(p);
173
174         return num;
175 }
176
177 static const struct drm_panel_funcs panel_simple_funcs = {
178         .disable = panel_simple_disable,
179         .enable = panel_simple_enable,
180         .get_modes = panel_simple_get_modes,
181 };
182
183 static int panel_simple_probe(struct device *dev, const struct panel_desc *desc)
184 {
185         struct device_node *backlight, *ddc;
186         struct panel_simple *panel;
187         enum of_gpio_flags flags;
188         int err;
189
190         panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
191         if (!panel)
192                 return -ENOMEM;
193
194         panel->enabled = false;
195         panel->desc = desc;
196
197         panel->supply = devm_regulator_get(dev, "power");
198         if (IS_ERR(panel->supply))
199                 return PTR_ERR(panel->supply);
200
201         panel->enable_gpio = of_get_named_gpio_flags(dev->of_node,
202                                                      "enable-gpios", 0,
203                                                      &flags);
204         if (gpio_is_valid(panel->enable_gpio)) {
205                 unsigned int value;
206
207                 if (flags & OF_GPIO_ACTIVE_LOW)
208                         panel->enable_gpio_flags |= GPIO_ACTIVE_LOW;
209
210                 err = gpio_request(panel->enable_gpio, "enable");
211                 if (err < 0) {
212                         dev_err(dev, "failed to request GPIO#%u: %d\n",
213                                 panel->enable_gpio, err);
214                         return err;
215                 }
216
217                 value = (panel->enable_gpio_flags & GPIO_ACTIVE_LOW) != 0;
218
219                 err = gpio_direction_output(panel->enable_gpio, value);
220                 if (err < 0) {
221                         dev_err(dev, "failed to setup GPIO%u: %d\n",
222                                 panel->enable_gpio, err);
223                         goto free_gpio;
224                 }
225         }
226
227         backlight = of_parse_phandle(dev->of_node, "backlight", 0);
228         if (backlight) {
229                 panel->backlight = of_find_backlight_by_node(backlight);
230                 of_node_put(backlight);
231
232                 if (!panel->backlight) {
233                         err = -EPROBE_DEFER;
234                         goto free_gpio;
235                 }
236         }
237
238         ddc = of_parse_phandle(dev->of_node, "ddc-i2c-bus", 0);
239         if (ddc) {
240                 panel->ddc = of_find_i2c_adapter_by_node(ddc);
241                 of_node_put(ddc);
242
243                 if (!panel->ddc) {
244                         err = -EPROBE_DEFER;
245                         goto free_backlight;
246                 }
247         }
248
249         drm_panel_init(&panel->base);
250         panel->base.dev = dev;
251         panel->base.funcs = &panel_simple_funcs;
252
253         err = drm_panel_add(&panel->base);
254         if (err < 0)
255                 goto free_ddc;
256
257         dev_set_drvdata(dev, panel);
258
259         return 0;
260
261 free_ddc:
262         if (panel->ddc)
263                 put_device(&panel->ddc->dev);
264 free_backlight:
265         if (panel->backlight)
266                 put_device(&panel->backlight->dev);
267 free_gpio:
268         if (gpio_is_valid(panel->enable_gpio))
269                 gpio_free(panel->enable_gpio);
270
271         return err;
272 }
273
274 static int panel_simple_remove(struct device *dev)
275 {
276         struct panel_simple *panel = dev_get_drvdata(dev);
277
278         drm_panel_detach(&panel->base);
279         drm_panel_remove(&panel->base);
280
281         panel_simple_disable(&panel->base);
282
283         if (panel->ddc)
284                 put_device(&panel->ddc->dev);
285
286         if (panel->backlight)
287                 put_device(&panel->backlight->dev);
288
289         if (gpio_is_valid(panel->enable_gpio))
290                 gpio_free(panel->enable_gpio);
291
292         regulator_disable(panel->supply);
293
294         return 0;
295 }
296
297 static const struct drm_display_mode auo_b101aw03_mode = {
298         .clock = 51450,
299         .hdisplay = 1024,
300         .hsync_start = 1024 + 156,
301         .hsync_end = 1024 + 156 + 8,
302         .htotal = 1024 + 156 + 8 + 156,
303         .vdisplay = 600,
304         .vsync_start = 600 + 16,
305         .vsync_end = 600 + 16 + 6,
306         .vtotal = 600 + 16 + 6 + 16,
307         .vrefresh = 60,
308 };
309
310 static const struct panel_desc auo_b101aw03 = {
311         .modes = &auo_b101aw03_mode,
312         .num_modes = 1,
313         .size = {
314                 .width = 223,
315                 .height = 125,
316         },
317 };
318
319 static const struct drm_display_mode chunghwa_claa101wb01_mode = {
320         .clock = 69300,
321         .hdisplay = 1366,
322         .hsync_start = 1366 + 48,
323         .hsync_end = 1366 + 48 + 32,
324         .htotal = 1366 + 48 + 32 + 20,
325         .vdisplay = 768,
326         .vsync_start = 768 + 16,
327         .vsync_end = 768 + 16 + 8,
328         .vtotal = 768 + 16 + 8 + 16,
329         .vrefresh = 60,
330 };
331
332 static const struct panel_desc chunghwa_claa101wb01 = {
333         .modes = &chunghwa_claa101wb01_mode,
334         .num_modes = 1,
335         .size = {
336                 .width = 223,
337                 .height = 125,
338         },
339 };
340
341 static const struct of_device_id platform_of_match[] = {
342         {
343                 .compatible = "auo,b101aw03",
344                 .data = &auo_b101aw03,
345         }, {
346                 .compatible = "chunghwa,claa101wb01",
347                 .data = &chunghwa_claa101wb01
348         }, {
349                 .compatible = "simple-panel",
350         }, {
351                 /* sentinel */
352         }
353 };
354 MODULE_DEVICE_TABLE(of, platform_of_match);
355
356 static int panel_simple_platform_probe(struct platform_device *pdev)
357 {
358         const struct of_device_id *id;
359
360         id = of_match_node(platform_of_match, pdev->dev.of_node);
361         if (!id)
362                 return -ENODEV;
363
364         return panel_simple_probe(&pdev->dev, id->data);
365 }
366
367 static int panel_simple_platform_remove(struct platform_device *pdev)
368 {
369         return panel_simple_remove(&pdev->dev);
370 }
371
372 static struct platform_driver panel_simple_platform_driver = {
373         .driver = {
374                 .name = "panel-simple",
375                 .owner = THIS_MODULE,
376                 .of_match_table = platform_of_match,
377         },
378         .probe = panel_simple_platform_probe,
379         .remove = panel_simple_platform_remove,
380 };
381
382 struct panel_desc_dsi {
383         struct panel_desc desc;
384
385         enum mipi_dsi_pixel_format format;
386         unsigned int lanes;
387 };
388
389 static const struct drm_display_mode panasonic_vvx10f004b00_mode = {
390         .clock = 157200,
391         .hdisplay = 1920,
392         .hsync_start = 1920 + 154,
393         .hsync_end = 1920 + 154 + 16,
394         .htotal = 1920 + 154 + 16 + 32,
395         .vdisplay = 1200,
396         .vsync_start = 1200 + 17,
397         .vsync_end = 1200 + 17 + 2,
398         .vtotal = 1200 + 17 + 2 + 16,
399         .vrefresh = 60,
400 };
401
402 static const struct panel_desc_dsi panasonic_vvx10f004b00 = {
403         .desc = {
404                 .modes = &panasonic_vvx10f004b00_mode,
405                 .num_modes = 1,
406                 .size = {
407                         .width = 217,
408                         .height = 136,
409                 },
410         },
411         .format = MIPI_DSI_FMT_RGB888,
412         .lanes = 4,
413 };
414
415 static const struct of_device_id dsi_of_match[] = {
416         {
417                 .compatible = "panasonic,vvx10f004b00",
418                 .data = &panasonic_vvx10f004b00
419         }, {
420                 /* sentinel */
421         }
422 };
423 MODULE_DEVICE_TABLE(of, dsi_of_match);
424
425 static int panel_simple_dsi_probe(struct mipi_dsi_device *dsi)
426 {
427         const struct panel_desc_dsi *desc;
428         const struct of_device_id *id;
429         int err;
430
431         id = of_match_node(dsi_of_match, dsi->dev.of_node);
432         if (!id)
433                 return -ENODEV;
434
435         desc = id->data;
436
437         err = panel_simple_probe(&dsi->dev, &desc->desc);
438         if (err < 0)
439                 return err;
440
441         dsi->format = desc->format;
442         dsi->lanes = desc->lanes;
443
444         return mipi_dsi_attach(dsi);
445 }
446
447 static int panel_simple_dsi_remove(struct mipi_dsi_device *dsi)
448 {
449         int err;
450
451         err = mipi_dsi_detach(dsi);
452         if (err < 0)
453                 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
454
455         return panel_simple_remove(&dsi->dev);
456 }
457
458 static struct mipi_dsi_driver panel_simple_dsi_driver = {
459         .driver = {
460                 .name = "panel-simple-dsi",
461                 .owner = THIS_MODULE,
462                 .of_match_table = dsi_of_match,
463         },
464         .probe = panel_simple_dsi_probe,
465         .remove = panel_simple_dsi_remove,
466 };
467
468 static int __init panel_simple_init(void)
469 {
470         int err;
471
472         err = platform_driver_register(&panel_simple_platform_driver);
473         if (err < 0)
474                 return err;
475
476         if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
477                 err = mipi_dsi_driver_register(&panel_simple_dsi_driver);
478                 if (err < 0)
479                         return err;
480         }
481
482         return 0;
483 }
484 module_init(panel_simple_init);
485
486 static void __exit panel_simple_exit(void)
487 {
488         if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
489                 mipi_dsi_driver_unregister(&panel_simple_dsi_driver);
490
491         platform_driver_unregister(&panel_simple_platform_driver);
492 }
493 module_exit(panel_simple_exit);
494
495 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
496 MODULE_DESCRIPTION("DRM Driver for Simple Panels");
497 MODULE_LICENSE("GPL and additional rights");