gpu: host1x: Remove unnecessary include
[deliverable/linux.git] / drivers / gpu / drm / panel / panel-simple.c
CommitLineData
280921de
TR
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>
210fcd9d 34#include <drm/drm_mipi_dsi.h>
280921de
TR
35#include <drm/drm_panel.h>
36
37struct 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
50struct 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
64static inline struct panel_simple *to_panel_simple(struct drm_panel *panel)
65{
66 return container_of(panel, struct panel_simple, base);
67}
68
69static 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
101static 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
126static 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
157static 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
177static 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
183static 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
261free_ddc:
262 if (panel->ddc)
263 put_device(&panel->ddc->dev);
264free_backlight:
265 if (panel->backlight)
266 put_device(&panel->backlight->dev);
267free_gpio:
268 if (gpio_is_valid(panel->enable_gpio))
269 gpio_free(panel->enable_gpio);
270
271 return err;
272}
273
274static 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
297static 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
310static 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
4c930757
SW
319static const struct drm_display_mode chunghwa_claa101wa01a_mode = {
320 .clock = 72070,
321 .hdisplay = 1366,
322 .hsync_start = 1366 + 58,
323 .hsync_end = 1366 + 58 + 58,
324 .htotal = 1366 + 58 + 58 + 58,
325 .vdisplay = 768,
326 .vsync_start = 768 + 4,
327 .vsync_end = 768 + 4 + 4,
328 .vtotal = 768 + 4 + 4 + 4,
329 .vrefresh = 60,
330};
331
332static const struct panel_desc chunghwa_claa101wa01a = {
333 .modes = &chunghwa_claa101wa01a_mode,
334 .num_modes = 1,
335 .size = {
336 .width = 220,
337 .height = 120,
338 },
339};
340
280921de
TR
341static const struct drm_display_mode chunghwa_claa101wb01_mode = {
342 .clock = 69300,
343 .hdisplay = 1366,
344 .hsync_start = 1366 + 48,
345 .hsync_end = 1366 + 48 + 32,
346 .htotal = 1366 + 48 + 32 + 20,
347 .vdisplay = 768,
348 .vsync_start = 768 + 16,
349 .vsync_end = 768 + 16 + 8,
350 .vtotal = 768 + 16 + 8 + 16,
351 .vrefresh = 60,
352};
353
354static const struct panel_desc chunghwa_claa101wb01 = {
355 .modes = &chunghwa_claa101wb01_mode,
356 .num_modes = 1,
357 .size = {
358 .width = 223,
359 .height = 125,
360 },
361};
362
6d54e3d2
MD
363static const struct drm_display_mode samsung_ltn101nt05_mode = {
364 .clock = 54030,
365 .hdisplay = 1024,
366 .hsync_start = 1024 + 24,
367 .hsync_end = 1024 + 24 + 136,
368 .htotal = 1024 + 24 + 136 + 160,
369 .vdisplay = 600,
370 .vsync_start = 600 + 3,
371 .vsync_end = 600 + 3 + 6,
372 .vtotal = 600 + 3 + 6 + 61,
373 .vrefresh = 60,
374};
375
376static const struct panel_desc samsung_ltn101nt05 = {
377 .modes = &samsung_ltn101nt05_mode,
378 .num_modes = 1,
379 .size = {
380 .width = 1024,
381 .height = 600,
382 },
383};
384
280921de
TR
385static const struct of_device_id platform_of_match[] = {
386 {
387 .compatible = "auo,b101aw03",
388 .data = &auo_b101aw03,
4c930757
SW
389 }, {
390 .compatible = "chunghwa,claa101wa01a",
391 .data = &chunghwa_claa101wa01a
280921de
TR
392 }, {
393 .compatible = "chunghwa,claa101wb01",
394 .data = &chunghwa_claa101wb01
6d54e3d2
MD
395 }, {
396 .compatible = "samsung,ltn101nt05",
397 .data = &samsung_ltn101nt05,
280921de
TR
398 }, {
399 .compatible = "simple-panel",
400 }, {
401 /* sentinel */
402 }
403};
404MODULE_DEVICE_TABLE(of, platform_of_match);
405
406static int panel_simple_platform_probe(struct platform_device *pdev)
407{
408 const struct of_device_id *id;
409
410 id = of_match_node(platform_of_match, pdev->dev.of_node);
411 if (!id)
412 return -ENODEV;
413
414 return panel_simple_probe(&pdev->dev, id->data);
415}
416
417static int panel_simple_platform_remove(struct platform_device *pdev)
418{
419 return panel_simple_remove(&pdev->dev);
420}
421
422static struct platform_driver panel_simple_platform_driver = {
423 .driver = {
424 .name = "panel-simple",
425 .owner = THIS_MODULE,
426 .of_match_table = platform_of_match,
427 },
428 .probe = panel_simple_platform_probe,
429 .remove = panel_simple_platform_remove,
430};
431
210fcd9d
TR
432struct panel_desc_dsi {
433 struct panel_desc desc;
434
435 enum mipi_dsi_pixel_format format;
436 unsigned int lanes;
437};
438
280921de
TR
439static const struct drm_display_mode panasonic_vvx10f004b00_mode = {
440 .clock = 157200,
441 .hdisplay = 1920,
442 .hsync_start = 1920 + 154,
443 .hsync_end = 1920 + 154 + 16,
444 .htotal = 1920 + 154 + 16 + 32,
445 .vdisplay = 1200,
446 .vsync_start = 1200 + 17,
447 .vsync_end = 1200 + 17 + 2,
448 .vtotal = 1200 + 17 + 2 + 16,
449 .vrefresh = 60,
450};
451
210fcd9d
TR
452static const struct panel_desc_dsi panasonic_vvx10f004b00 = {
453 .desc = {
454 .modes = &panasonic_vvx10f004b00_mode,
455 .num_modes = 1,
456 .size = {
457 .width = 217,
458 .height = 136,
459 },
280921de 460 },
210fcd9d
TR
461 .format = MIPI_DSI_FMT_RGB888,
462 .lanes = 4,
463};
464
465static const struct of_device_id dsi_of_match[] = {
466 {
467 .compatible = "panasonic,vvx10f004b00",
468 .data = &panasonic_vvx10f004b00
469 }, {
470 /* sentinel */
471 }
472};
473MODULE_DEVICE_TABLE(of, dsi_of_match);
474
475static int panel_simple_dsi_probe(struct mipi_dsi_device *dsi)
476{
477 const struct panel_desc_dsi *desc;
478 const struct of_device_id *id;
479 int err;
480
481 id = of_match_node(dsi_of_match, dsi->dev.of_node);
482 if (!id)
483 return -ENODEV;
484
485 desc = id->data;
486
487 err = panel_simple_probe(&dsi->dev, &desc->desc);
488 if (err < 0)
489 return err;
490
491 dsi->format = desc->format;
492 dsi->lanes = desc->lanes;
493
494 return mipi_dsi_attach(dsi);
495}
496
497static int panel_simple_dsi_remove(struct mipi_dsi_device *dsi)
498{
499 int err;
500
501 err = mipi_dsi_detach(dsi);
502 if (err < 0)
503 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
504
505 return panel_simple_remove(&dsi->dev);
506}
507
508static struct mipi_dsi_driver panel_simple_dsi_driver = {
509 .driver = {
510 .name = "panel-simple-dsi",
511 .owner = THIS_MODULE,
512 .of_match_table = dsi_of_match,
513 },
514 .probe = panel_simple_dsi_probe,
515 .remove = panel_simple_dsi_remove,
280921de
TR
516};
517
518static int __init panel_simple_init(void)
519{
210fcd9d
TR
520 int err;
521
522 err = platform_driver_register(&panel_simple_platform_driver);
523 if (err < 0)
524 return err;
525
526 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
527 err = mipi_dsi_driver_register(&panel_simple_dsi_driver);
528 if (err < 0)
529 return err;
530 }
531
532 return 0;
280921de
TR
533}
534module_init(panel_simple_init);
535
536static void __exit panel_simple_exit(void)
537{
210fcd9d
TR
538 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
539 mipi_dsi_driver_unregister(&panel_simple_dsi_driver);
540
280921de
TR
541 platform_driver_unregister(&panel_simple_platform_driver);
542}
543module_exit(panel_simple_exit);
544
545MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
546MODULE_DESCRIPTION("DRM Driver for Simple Panels");
547MODULE_LICENSE("GPL and additional rights");
This page took 0.046593 seconds and 5 git commands to generate.