Commit | Line | Data |
---|---|---|
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 | ||
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); | |
70bf6878 | 165 | drm_mode_connector_update_edid_property(panel->connector, edid); |
280921de TR |
166 | if (edid) { |
167 | num += drm_add_edid_modes(panel->connector, edid); | |
168 | kfree(edid); | |
169 | } | |
170 | } | |
171 | ||
172 | /* add hard-coded panel modes */ | |
173 | num += panel_simple_get_fixed_modes(p); | |
174 | ||
175 | return num; | |
176 | } | |
177 | ||
178 | static const struct drm_panel_funcs panel_simple_funcs = { | |
179 | .disable = panel_simple_disable, | |
180 | .enable = panel_simple_enable, | |
181 | .get_modes = panel_simple_get_modes, | |
182 | }; | |
183 | ||
184 | static int panel_simple_probe(struct device *dev, const struct panel_desc *desc) | |
185 | { | |
186 | struct device_node *backlight, *ddc; | |
187 | struct panel_simple *panel; | |
188 | enum of_gpio_flags flags; | |
189 | int err; | |
190 | ||
191 | panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL); | |
192 | if (!panel) | |
193 | return -ENOMEM; | |
194 | ||
195 | panel->enabled = false; | |
196 | panel->desc = desc; | |
197 | ||
198 | panel->supply = devm_regulator_get(dev, "power"); | |
199 | if (IS_ERR(panel->supply)) | |
200 | return PTR_ERR(panel->supply); | |
201 | ||
202 | panel->enable_gpio = of_get_named_gpio_flags(dev->of_node, | |
203 | "enable-gpios", 0, | |
204 | &flags); | |
205 | if (gpio_is_valid(panel->enable_gpio)) { | |
206 | unsigned int value; | |
207 | ||
208 | if (flags & OF_GPIO_ACTIVE_LOW) | |
209 | panel->enable_gpio_flags |= GPIO_ACTIVE_LOW; | |
210 | ||
211 | err = gpio_request(panel->enable_gpio, "enable"); | |
212 | if (err < 0) { | |
213 | dev_err(dev, "failed to request GPIO#%u: %d\n", | |
214 | panel->enable_gpio, err); | |
215 | return err; | |
216 | } | |
217 | ||
218 | value = (panel->enable_gpio_flags & GPIO_ACTIVE_LOW) != 0; | |
219 | ||
220 | err = gpio_direction_output(panel->enable_gpio, value); | |
221 | if (err < 0) { | |
222 | dev_err(dev, "failed to setup GPIO%u: %d\n", | |
223 | panel->enable_gpio, err); | |
224 | goto free_gpio; | |
225 | } | |
226 | } | |
227 | ||
228 | backlight = of_parse_phandle(dev->of_node, "backlight", 0); | |
229 | if (backlight) { | |
230 | panel->backlight = of_find_backlight_by_node(backlight); | |
231 | of_node_put(backlight); | |
232 | ||
233 | if (!panel->backlight) { | |
234 | err = -EPROBE_DEFER; | |
235 | goto free_gpio; | |
236 | } | |
237 | } | |
238 | ||
239 | ddc = of_parse_phandle(dev->of_node, "ddc-i2c-bus", 0); | |
240 | if (ddc) { | |
241 | panel->ddc = of_find_i2c_adapter_by_node(ddc); | |
242 | of_node_put(ddc); | |
243 | ||
244 | if (!panel->ddc) { | |
245 | err = -EPROBE_DEFER; | |
246 | goto free_backlight; | |
247 | } | |
248 | } | |
249 | ||
250 | drm_panel_init(&panel->base); | |
251 | panel->base.dev = dev; | |
252 | panel->base.funcs = &panel_simple_funcs; | |
253 | ||
254 | err = drm_panel_add(&panel->base); | |
255 | if (err < 0) | |
256 | goto free_ddc; | |
257 | ||
258 | dev_set_drvdata(dev, panel); | |
259 | ||
260 | return 0; | |
261 | ||
262 | free_ddc: | |
263 | if (panel->ddc) | |
264 | put_device(&panel->ddc->dev); | |
265 | free_backlight: | |
266 | if (panel->backlight) | |
267 | put_device(&panel->backlight->dev); | |
268 | free_gpio: | |
269 | if (gpio_is_valid(panel->enable_gpio)) | |
270 | gpio_free(panel->enable_gpio); | |
271 | ||
272 | return err; | |
273 | } | |
274 | ||
275 | static int panel_simple_remove(struct device *dev) | |
276 | { | |
277 | struct panel_simple *panel = dev_get_drvdata(dev); | |
278 | ||
279 | drm_panel_detach(&panel->base); | |
280 | drm_panel_remove(&panel->base); | |
281 | ||
282 | panel_simple_disable(&panel->base); | |
283 | ||
284 | if (panel->ddc) | |
285 | put_device(&panel->ddc->dev); | |
286 | ||
287 | if (panel->backlight) | |
288 | put_device(&panel->backlight->dev); | |
289 | ||
290 | if (gpio_is_valid(panel->enable_gpio)) | |
291 | gpio_free(panel->enable_gpio); | |
292 | ||
293 | regulator_disable(panel->supply); | |
294 | ||
295 | return 0; | |
296 | } | |
297 | ||
298 | static const struct drm_display_mode auo_b101aw03_mode = { | |
299 | .clock = 51450, | |
300 | .hdisplay = 1024, | |
301 | .hsync_start = 1024 + 156, | |
302 | .hsync_end = 1024 + 156 + 8, | |
303 | .htotal = 1024 + 156 + 8 + 156, | |
304 | .vdisplay = 600, | |
305 | .vsync_start = 600 + 16, | |
306 | .vsync_end = 600 + 16 + 6, | |
307 | .vtotal = 600 + 16 + 6 + 16, | |
308 | .vrefresh = 60, | |
309 | }; | |
310 | ||
311 | static const struct panel_desc auo_b101aw03 = { | |
312 | .modes = &auo_b101aw03_mode, | |
313 | .num_modes = 1, | |
314 | .size = { | |
315 | .width = 223, | |
316 | .height = 125, | |
317 | }, | |
318 | }; | |
319 | ||
4c930757 SW |
320 | static const struct drm_display_mode chunghwa_claa101wa01a_mode = { |
321 | .clock = 72070, | |
322 | .hdisplay = 1366, | |
323 | .hsync_start = 1366 + 58, | |
324 | .hsync_end = 1366 + 58 + 58, | |
325 | .htotal = 1366 + 58 + 58 + 58, | |
326 | .vdisplay = 768, | |
327 | .vsync_start = 768 + 4, | |
328 | .vsync_end = 768 + 4 + 4, | |
329 | .vtotal = 768 + 4 + 4 + 4, | |
330 | .vrefresh = 60, | |
331 | }; | |
332 | ||
333 | static const struct panel_desc chunghwa_claa101wa01a = { | |
334 | .modes = &chunghwa_claa101wa01a_mode, | |
335 | .num_modes = 1, | |
336 | .size = { | |
337 | .width = 220, | |
338 | .height = 120, | |
339 | }, | |
340 | }; | |
341 | ||
280921de TR |
342 | static const struct drm_display_mode chunghwa_claa101wb01_mode = { |
343 | .clock = 69300, | |
344 | .hdisplay = 1366, | |
345 | .hsync_start = 1366 + 48, | |
346 | .hsync_end = 1366 + 48 + 32, | |
347 | .htotal = 1366 + 48 + 32 + 20, | |
348 | .vdisplay = 768, | |
349 | .vsync_start = 768 + 16, | |
350 | .vsync_end = 768 + 16 + 8, | |
351 | .vtotal = 768 + 16 + 8 + 16, | |
352 | .vrefresh = 60, | |
353 | }; | |
354 | ||
355 | static const struct panel_desc chunghwa_claa101wb01 = { | |
356 | .modes = &chunghwa_claa101wb01_mode, | |
357 | .num_modes = 1, | |
358 | .size = { | |
359 | .width = 223, | |
360 | .height = 125, | |
361 | }, | |
362 | }; | |
363 | ||
ec7c5653 TR |
364 | static const struct drm_display_mode lg_lp129qe_mode = { |
365 | .clock = 285250, | |
366 | .hdisplay = 2560, | |
367 | .hsync_start = 2560 + 48, | |
368 | .hsync_end = 2560 + 48 + 32, | |
369 | .htotal = 2560 + 48 + 32 + 80, | |
370 | .vdisplay = 1700, | |
371 | .vsync_start = 1700 + 3, | |
372 | .vsync_end = 1700 + 3 + 10, | |
373 | .vtotal = 1700 + 3 + 10 + 36, | |
374 | .vrefresh = 60, | |
375 | }; | |
376 | ||
377 | static const struct panel_desc lg_lp129qe = { | |
378 | .modes = &lg_lp129qe_mode, | |
379 | .num_modes = 1, | |
380 | .size = { | |
381 | .width = 272, | |
382 | .height = 181, | |
383 | }, | |
384 | }; | |
385 | ||
6d54e3d2 MD |
386 | static const struct drm_display_mode samsung_ltn101nt05_mode = { |
387 | .clock = 54030, | |
388 | .hdisplay = 1024, | |
389 | .hsync_start = 1024 + 24, | |
390 | .hsync_end = 1024 + 24 + 136, | |
391 | .htotal = 1024 + 24 + 136 + 160, | |
392 | .vdisplay = 600, | |
393 | .vsync_start = 600 + 3, | |
394 | .vsync_end = 600 + 3 + 6, | |
395 | .vtotal = 600 + 3 + 6 + 61, | |
396 | .vrefresh = 60, | |
397 | }; | |
398 | ||
399 | static const struct panel_desc samsung_ltn101nt05 = { | |
400 | .modes = &samsung_ltn101nt05_mode, | |
401 | .num_modes = 1, | |
402 | .size = { | |
403 | .width = 1024, | |
404 | .height = 600, | |
405 | }, | |
406 | }; | |
407 | ||
280921de TR |
408 | static const struct of_device_id platform_of_match[] = { |
409 | { | |
410 | .compatible = "auo,b101aw03", | |
411 | .data = &auo_b101aw03, | |
4c930757 SW |
412 | }, { |
413 | .compatible = "chunghwa,claa101wa01a", | |
414 | .data = &chunghwa_claa101wa01a | |
280921de TR |
415 | }, { |
416 | .compatible = "chunghwa,claa101wb01", | |
417 | .data = &chunghwa_claa101wb01 | |
ec7c5653 TR |
418 | }, { |
419 | .compatible = "lg,lp129qe", | |
420 | .data = &lg_lp129qe, | |
6d54e3d2 MD |
421 | }, { |
422 | .compatible = "samsung,ltn101nt05", | |
423 | .data = &samsung_ltn101nt05, | |
280921de TR |
424 | }, { |
425 | .compatible = "simple-panel", | |
426 | }, { | |
427 | /* sentinel */ | |
428 | } | |
429 | }; | |
430 | MODULE_DEVICE_TABLE(of, platform_of_match); | |
431 | ||
432 | static int panel_simple_platform_probe(struct platform_device *pdev) | |
433 | { | |
434 | const struct of_device_id *id; | |
435 | ||
436 | id = of_match_node(platform_of_match, pdev->dev.of_node); | |
437 | if (!id) | |
438 | return -ENODEV; | |
439 | ||
440 | return panel_simple_probe(&pdev->dev, id->data); | |
441 | } | |
442 | ||
443 | static int panel_simple_platform_remove(struct platform_device *pdev) | |
444 | { | |
445 | return panel_simple_remove(&pdev->dev); | |
446 | } | |
447 | ||
448 | static struct platform_driver panel_simple_platform_driver = { | |
449 | .driver = { | |
450 | .name = "panel-simple", | |
451 | .owner = THIS_MODULE, | |
452 | .of_match_table = platform_of_match, | |
453 | }, | |
454 | .probe = panel_simple_platform_probe, | |
455 | .remove = panel_simple_platform_remove, | |
456 | }; | |
457 | ||
210fcd9d TR |
458 | struct panel_desc_dsi { |
459 | struct panel_desc desc; | |
460 | ||
461 | enum mipi_dsi_pixel_format format; | |
462 | unsigned int lanes; | |
463 | }; | |
464 | ||
280921de TR |
465 | static const struct drm_display_mode panasonic_vvx10f004b00_mode = { |
466 | .clock = 157200, | |
467 | .hdisplay = 1920, | |
468 | .hsync_start = 1920 + 154, | |
469 | .hsync_end = 1920 + 154 + 16, | |
470 | .htotal = 1920 + 154 + 16 + 32, | |
471 | .vdisplay = 1200, | |
472 | .vsync_start = 1200 + 17, | |
473 | .vsync_end = 1200 + 17 + 2, | |
474 | .vtotal = 1200 + 17 + 2 + 16, | |
475 | .vrefresh = 60, | |
476 | }; | |
477 | ||
210fcd9d TR |
478 | static const struct panel_desc_dsi panasonic_vvx10f004b00 = { |
479 | .desc = { | |
480 | .modes = &panasonic_vvx10f004b00_mode, | |
481 | .num_modes = 1, | |
482 | .size = { | |
483 | .width = 217, | |
484 | .height = 136, | |
485 | }, | |
280921de | 486 | }, |
210fcd9d TR |
487 | .format = MIPI_DSI_FMT_RGB888, |
488 | .lanes = 4, | |
489 | }; | |
490 | ||
491 | static const struct of_device_id dsi_of_match[] = { | |
492 | { | |
493 | .compatible = "panasonic,vvx10f004b00", | |
494 | .data = &panasonic_vvx10f004b00 | |
495 | }, { | |
496 | /* sentinel */ | |
497 | } | |
498 | }; | |
499 | MODULE_DEVICE_TABLE(of, dsi_of_match); | |
500 | ||
501 | static int panel_simple_dsi_probe(struct mipi_dsi_device *dsi) | |
502 | { | |
503 | const struct panel_desc_dsi *desc; | |
504 | const struct of_device_id *id; | |
505 | int err; | |
506 | ||
507 | id = of_match_node(dsi_of_match, dsi->dev.of_node); | |
508 | if (!id) | |
509 | return -ENODEV; | |
510 | ||
511 | desc = id->data; | |
512 | ||
513 | err = panel_simple_probe(&dsi->dev, &desc->desc); | |
514 | if (err < 0) | |
515 | return err; | |
516 | ||
517 | dsi->format = desc->format; | |
518 | dsi->lanes = desc->lanes; | |
519 | ||
520 | return mipi_dsi_attach(dsi); | |
521 | } | |
522 | ||
523 | static int panel_simple_dsi_remove(struct mipi_dsi_device *dsi) | |
524 | { | |
525 | int err; | |
526 | ||
527 | err = mipi_dsi_detach(dsi); | |
528 | if (err < 0) | |
529 | dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err); | |
530 | ||
531 | return panel_simple_remove(&dsi->dev); | |
532 | } | |
533 | ||
534 | static struct mipi_dsi_driver panel_simple_dsi_driver = { | |
535 | .driver = { | |
536 | .name = "panel-simple-dsi", | |
537 | .owner = THIS_MODULE, | |
538 | .of_match_table = dsi_of_match, | |
539 | }, | |
540 | .probe = panel_simple_dsi_probe, | |
541 | .remove = panel_simple_dsi_remove, | |
280921de TR |
542 | }; |
543 | ||
544 | static int __init panel_simple_init(void) | |
545 | { | |
210fcd9d TR |
546 | int err; |
547 | ||
548 | err = platform_driver_register(&panel_simple_platform_driver); | |
549 | if (err < 0) | |
550 | return err; | |
551 | ||
552 | if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) { | |
553 | err = mipi_dsi_driver_register(&panel_simple_dsi_driver); | |
554 | if (err < 0) | |
555 | return err; | |
556 | } | |
557 | ||
558 | return 0; | |
280921de TR |
559 | } |
560 | module_init(panel_simple_init); | |
561 | ||
562 | static void __exit panel_simple_exit(void) | |
563 | { | |
210fcd9d TR |
564 | if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) |
565 | mipi_dsi_driver_unregister(&panel_simple_dsi_driver); | |
566 | ||
280921de TR |
567 | platform_driver_unregister(&panel_simple_platform_driver); |
568 | } | |
569 | module_exit(panel_simple_exit); | |
570 | ||
571 | MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>"); | |
572 | MODULE_DESCRIPTION("DRM Driver for Simple Panels"); | |
573 | MODULE_LICENSE("GPL and additional rights"); |