panel-simple.c revision 102932b0e474bb33061e88bcf5d83e66f10c1da2
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/consumer.h>
26#include <linux/module.h>
27#include <linux/of_platform.h>
28#include <linux/platform_device.h>
29#include <linux/regulator/consumer.h>
30
31#include <drm/drmP.h>
32#include <drm/drm_crtc.h>
33#include <drm/drm_mipi_dsi.h>
34#include <drm/drm_panel.h>
35
36struct panel_desc {
37	const struct drm_display_mode *modes;
38	unsigned int num_modes;
39
40	struct {
41		unsigned int width;
42		unsigned int height;
43	} size;
44};
45
46struct panel_simple {
47	struct drm_panel base;
48	bool enabled;
49
50	const struct panel_desc *desc;
51
52	struct backlight_device *backlight;
53	struct regulator *supply;
54	struct i2c_adapter *ddc;
55
56	struct gpio_desc *enable_gpio;
57};
58
59static inline struct panel_simple *to_panel_simple(struct drm_panel *panel)
60{
61	return container_of(panel, struct panel_simple, base);
62}
63
64static int panel_simple_get_fixed_modes(struct panel_simple *panel)
65{
66	struct drm_connector *connector = panel->base.connector;
67	struct drm_device *drm = panel->base.drm;
68	struct drm_display_mode *mode;
69	unsigned int i, num = 0;
70
71	if (!panel->desc)
72		return 0;
73
74	for (i = 0; i < panel->desc->num_modes; i++) {
75		const struct drm_display_mode *m = &panel->desc->modes[i];
76
77		mode = drm_mode_duplicate(drm, m);
78		if (!mode) {
79			dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
80				m->hdisplay, m->vdisplay, m->vrefresh);
81			continue;
82		}
83
84		drm_mode_set_name(mode);
85
86		drm_mode_probed_add(connector, mode);
87		num++;
88	}
89
90	connector->display_info.width_mm = panel->desc->size.width;
91	connector->display_info.height_mm = panel->desc->size.height;
92
93	return num;
94}
95
96static int panel_simple_disable(struct drm_panel *panel)
97{
98	struct panel_simple *p = to_panel_simple(panel);
99
100	if (!p->enabled)
101		return 0;
102
103	if (p->backlight) {
104		p->backlight->props.power = FB_BLANK_POWERDOWN;
105		backlight_update_status(p->backlight);
106	}
107
108	if (p->enable_gpio)
109		gpiod_set_value_cansleep(p->enable_gpio, 0);
110
111	regulator_disable(p->supply);
112	p->enabled = false;
113
114	return 0;
115}
116
117static int panel_simple_enable(struct drm_panel *panel)
118{
119	struct panel_simple *p = to_panel_simple(panel);
120	int err;
121
122	if (p->enabled)
123		return 0;
124
125	err = regulator_enable(p->supply);
126	if (err < 0) {
127		dev_err(panel->dev, "failed to enable supply: %d\n", err);
128		return err;
129	}
130
131	if (p->enable_gpio)
132		gpiod_set_value_cansleep(p->enable_gpio, 1);
133
134	if (p->backlight) {
135		p->backlight->props.power = FB_BLANK_UNBLANK;
136		backlight_update_status(p->backlight);
137	}
138
139	p->enabled = true;
140
141	return 0;
142}
143
144static int panel_simple_get_modes(struct drm_panel *panel)
145{
146	struct panel_simple *p = to_panel_simple(panel);
147	int num = 0;
148
149	/* probe EDID if a DDC bus is available */
150	if (p->ddc) {
151		struct edid *edid = drm_get_edid(panel->connector, p->ddc);
152		drm_mode_connector_update_edid_property(panel->connector, edid);
153		if (edid) {
154			num += drm_add_edid_modes(panel->connector, edid);
155			kfree(edid);
156		}
157	}
158
159	/* add hard-coded panel modes */
160	num += panel_simple_get_fixed_modes(p);
161
162	return num;
163}
164
165static const struct drm_panel_funcs panel_simple_funcs = {
166	.disable = panel_simple_disable,
167	.enable = panel_simple_enable,
168	.get_modes = panel_simple_get_modes,
169};
170
171static int panel_simple_probe(struct device *dev, const struct panel_desc *desc)
172{
173	struct device_node *backlight, *ddc;
174	struct panel_simple *panel;
175	int err;
176
177	panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
178	if (!panel)
179		return -ENOMEM;
180
181	panel->enabled = false;
182	panel->desc = desc;
183
184	panel->supply = devm_regulator_get(dev, "power");
185	if (IS_ERR(panel->supply))
186		return PTR_ERR(panel->supply);
187
188	panel->enable_gpio = devm_gpiod_get(dev, "enable");
189	if (IS_ERR(panel->enable_gpio)) {
190		err = PTR_ERR(panel->enable_gpio);
191		if (err != -ENOENT) {
192			dev_err(dev, "failed to request GPIO: %d\n", err);
193			return err;
194		}
195
196		panel->enable_gpio = NULL;
197	} else {
198		err = gpiod_direction_output(panel->enable_gpio, 0);
199		if (err < 0) {
200			dev_err(dev, "failed to setup GPIO: %d\n", err);
201			return err;
202		}
203	}
204
205	backlight = of_parse_phandle(dev->of_node, "backlight", 0);
206	if (backlight) {
207		panel->backlight = of_find_backlight_by_node(backlight);
208		of_node_put(backlight);
209
210		if (!panel->backlight)
211			return -EPROBE_DEFER;
212	}
213
214	ddc = of_parse_phandle(dev->of_node, "ddc-i2c-bus", 0);
215	if (ddc) {
216		panel->ddc = of_find_i2c_adapter_by_node(ddc);
217		of_node_put(ddc);
218
219		if (!panel->ddc) {
220			err = -EPROBE_DEFER;
221			goto free_backlight;
222		}
223	}
224
225	drm_panel_init(&panel->base);
226	panel->base.dev = dev;
227	panel->base.funcs = &panel_simple_funcs;
228
229	err = drm_panel_add(&panel->base);
230	if (err < 0)
231		goto free_ddc;
232
233	dev_set_drvdata(dev, panel);
234
235	return 0;
236
237free_ddc:
238	if (panel->ddc)
239		put_device(&panel->ddc->dev);
240free_backlight:
241	if (panel->backlight)
242		put_device(&panel->backlight->dev);
243
244	return err;
245}
246
247static int panel_simple_remove(struct device *dev)
248{
249	struct panel_simple *panel = dev_get_drvdata(dev);
250
251	drm_panel_detach(&panel->base);
252	drm_panel_remove(&panel->base);
253
254	panel_simple_disable(&panel->base);
255
256	if (panel->ddc)
257		put_device(&panel->ddc->dev);
258
259	if (panel->backlight)
260		put_device(&panel->backlight->dev);
261
262	return 0;
263}
264
265static void panel_simple_shutdown(struct device *dev)
266{
267	struct panel_simple *panel = dev_get_drvdata(dev);
268
269	panel_simple_disable(&panel->base);
270}
271
272static const struct drm_display_mode auo_b101aw03_mode = {
273	.clock = 51450,
274	.hdisplay = 1024,
275	.hsync_start = 1024 + 156,
276	.hsync_end = 1024 + 156 + 8,
277	.htotal = 1024 + 156 + 8 + 156,
278	.vdisplay = 600,
279	.vsync_start = 600 + 16,
280	.vsync_end = 600 + 16 + 6,
281	.vtotal = 600 + 16 + 6 + 16,
282	.vrefresh = 60,
283};
284
285static const struct panel_desc auo_b101aw03 = {
286	.modes = &auo_b101aw03_mode,
287	.num_modes = 1,
288	.size = {
289		.width = 223,
290		.height = 125,
291	},
292};
293
294static const struct drm_display_mode auo_b133xtn01_mode = {
295	.clock = 69500,
296	.hdisplay = 1366,
297	.hsync_start = 1366 + 48,
298	.hsync_end = 1366 + 48 + 32,
299	.htotal = 1366 + 48 + 32 + 20,
300	.vdisplay = 768,
301	.vsync_start = 768 + 3,
302	.vsync_end = 768 + 3 + 6,
303	.vtotal = 768 + 3 + 6 + 13,
304	.vrefresh = 60,
305};
306
307static const struct panel_desc auo_b133xtn01 = {
308	.modes = &auo_b133xtn01_mode,
309	.num_modes = 1,
310	.size = {
311		.width = 293,
312		.height = 165,
313	},
314};
315
316static const struct drm_display_mode chunghwa_claa101wa01a_mode = {
317	.clock = 72070,
318	.hdisplay = 1366,
319	.hsync_start = 1366 + 58,
320	.hsync_end = 1366 + 58 + 58,
321	.htotal = 1366 + 58 + 58 + 58,
322	.vdisplay = 768,
323	.vsync_start = 768 + 4,
324	.vsync_end = 768 + 4 + 4,
325	.vtotal = 768 + 4 + 4 + 4,
326	.vrefresh = 60,
327};
328
329static const struct panel_desc chunghwa_claa101wa01a = {
330	.modes = &chunghwa_claa101wa01a_mode,
331	.num_modes = 1,
332	.size = {
333		.width = 220,
334		.height = 120,
335	},
336};
337
338static const struct drm_display_mode chunghwa_claa101wb01_mode = {
339	.clock = 69300,
340	.hdisplay = 1366,
341	.hsync_start = 1366 + 48,
342	.hsync_end = 1366 + 48 + 32,
343	.htotal = 1366 + 48 + 32 + 20,
344	.vdisplay = 768,
345	.vsync_start = 768 + 16,
346	.vsync_end = 768 + 16 + 8,
347	.vtotal = 768 + 16 + 8 + 16,
348	.vrefresh = 60,
349};
350
351static const struct panel_desc chunghwa_claa101wb01 = {
352	.modes = &chunghwa_claa101wb01_mode,
353	.num_modes = 1,
354	.size = {
355		.width = 223,
356		.height = 125,
357	},
358};
359
360static const struct drm_display_mode edt_et057090dhu_mode = {
361	.clock = 25175,
362	.hdisplay = 640,
363	.hsync_start = 640 + 16,
364	.hsync_end = 640 + 16 + 30,
365	.htotal = 640 + 16 + 30 + 114,
366	.vdisplay = 480,
367	.vsync_start = 480 + 10,
368	.vsync_end = 480 + 10 + 3,
369	.vtotal = 480 + 10 + 3 + 32,
370	.vrefresh = 60,
371	.flags = DRM_MODE_FLAG_NVSYNC | DRM_MODE_FLAG_NHSYNC,
372};
373
374static const struct panel_desc edt_et057090dhu = {
375	.modes = &edt_et057090dhu_mode,
376	.num_modes = 1,
377	.size = {
378		.width = 115,
379		.height = 86,
380	},
381};
382
383static const struct drm_display_mode edt_etm0700g0dh6_mode = {
384	.clock = 33260,
385	.hdisplay = 800,
386	.hsync_start = 800 + 40,
387	.hsync_end = 800 + 40 + 128,
388	.htotal = 800 + 40 + 128 + 88,
389	.vdisplay = 480,
390	.vsync_start = 480 + 10,
391	.vsync_end = 480 + 10 + 2,
392	.vtotal = 480 + 10 + 2 + 33,
393	.vrefresh = 60,
394	.flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
395};
396
397static const struct panel_desc edt_etm0700g0dh6 = {
398	.modes = &edt_etm0700g0dh6_mode,
399	.num_modes = 1,
400	.size = {
401		.width = 152,
402		.height = 91,
403	},
404};
405
406static const struct drm_display_mode foxlink_fl500wvr00_a0t_mode = {
407	.clock = 32260,
408	.hdisplay = 800,
409	.hsync_start = 800 + 168,
410	.hsync_end = 800 + 168 + 64,
411	.htotal = 800 + 168 + 64 + 88,
412	.vdisplay = 480,
413	.vsync_start = 480 + 37,
414	.vsync_end = 480 + 37 + 2,
415	.vtotal = 480 + 37 + 2 + 8,
416	.vrefresh = 60,
417};
418
419static const struct panel_desc foxlink_fl500wvr00_a0t = {
420	.modes = &foxlink_fl500wvr00_a0t_mode,
421	.num_modes = 1,
422	.size = {
423		.width = 108,
424		.height = 65,
425	},
426};
427
428static const struct drm_display_mode lg_lp129qe_mode = {
429	.clock = 285250,
430	.hdisplay = 2560,
431	.hsync_start = 2560 + 48,
432	.hsync_end = 2560 + 48 + 32,
433	.htotal = 2560 + 48 + 32 + 80,
434	.vdisplay = 1700,
435	.vsync_start = 1700 + 3,
436	.vsync_end = 1700 + 3 + 10,
437	.vtotal = 1700 + 3 + 10 + 36,
438	.vrefresh = 60,
439};
440
441static const struct panel_desc lg_lp129qe = {
442	.modes = &lg_lp129qe_mode,
443	.num_modes = 1,
444	.size = {
445		.width = 272,
446		.height = 181,
447	},
448};
449
450static const struct drm_display_mode samsung_ltn101nt05_mode = {
451	.clock = 54030,
452	.hdisplay = 1024,
453	.hsync_start = 1024 + 24,
454	.hsync_end = 1024 + 24 + 136,
455	.htotal = 1024 + 24 + 136 + 160,
456	.vdisplay = 600,
457	.vsync_start = 600 + 3,
458	.vsync_end = 600 + 3 + 6,
459	.vtotal = 600 + 3 + 6 + 61,
460	.vrefresh = 60,
461};
462
463static const struct panel_desc samsung_ltn101nt05 = {
464	.modes = &samsung_ltn101nt05_mode,
465	.num_modes = 1,
466	.size = {
467		.width = 1024,
468		.height = 600,
469	},
470};
471
472static const struct of_device_id platform_of_match[] = {
473	{
474		.compatible = "auo,b101aw03",
475		.data = &auo_b101aw03,
476	}, {
477		.compatible = "auo,b133xtn01",
478		.data = &auo_b133xtn01,
479	}, {
480		.compatible = "chunghwa,claa101wa01a",
481		.data = &chunghwa_claa101wa01a
482	}, {
483		.compatible = "chunghwa,claa101wb01",
484		.data = &chunghwa_claa101wb01
485	}, {
486		.compatible = "edt,et057090dhu",
487		.data = &edt_et057090dhu,
488	}, {
489		.compatible = "edt,et070080dh6",
490		.data = &edt_etm0700g0dh6,
491	}, {
492		.compatible = "edt,etm0700g0dh6",
493		.data = &edt_etm0700g0dh6,
494	}, {
495		.compatible = "foxlink,fl500wvr00-a0t",
496		.data = &foxlink_fl500wvr00_a0t,
497	}, {
498		.compatible = "lg,lp129qe",
499		.data = &lg_lp129qe,
500	}, {
501		.compatible = "samsung,ltn101nt05",
502		.data = &samsung_ltn101nt05,
503	}, {
504		.compatible = "simple-panel",
505	}, {
506		/* sentinel */
507	}
508};
509MODULE_DEVICE_TABLE(of, platform_of_match);
510
511static int panel_simple_platform_probe(struct platform_device *pdev)
512{
513	const struct of_device_id *id;
514
515	id = of_match_node(platform_of_match, pdev->dev.of_node);
516	if (!id)
517		return -ENODEV;
518
519	return panel_simple_probe(&pdev->dev, id->data);
520}
521
522static int panel_simple_platform_remove(struct platform_device *pdev)
523{
524	return panel_simple_remove(&pdev->dev);
525}
526
527static void panel_simple_platform_shutdown(struct platform_device *pdev)
528{
529	panel_simple_shutdown(&pdev->dev);
530}
531
532static struct platform_driver panel_simple_platform_driver = {
533	.driver = {
534		.name = "panel-simple",
535		.owner = THIS_MODULE,
536		.of_match_table = platform_of_match,
537	},
538	.probe = panel_simple_platform_probe,
539	.remove = panel_simple_platform_remove,
540	.shutdown = panel_simple_platform_shutdown,
541};
542
543struct panel_desc_dsi {
544	struct panel_desc desc;
545
546	unsigned long flags;
547	enum mipi_dsi_pixel_format format;
548	unsigned int lanes;
549};
550
551static const struct drm_display_mode lg_ld070wx3_sl01_mode = {
552	.clock = 71000,
553	.hdisplay = 800,
554	.hsync_start = 800 + 32,
555	.hsync_end = 800 + 32 + 1,
556	.htotal = 800 + 32 + 1 + 57,
557	.vdisplay = 1280,
558	.vsync_start = 1280 + 28,
559	.vsync_end = 1280 + 28 + 1,
560	.vtotal = 1280 + 28 + 1 + 14,
561	.vrefresh = 60,
562};
563
564static const struct panel_desc_dsi lg_ld070wx3_sl01 = {
565	.desc = {
566		.modes = &lg_ld070wx3_sl01_mode,
567		.num_modes = 1,
568		.size = {
569			.width = 94,
570			.height = 151,
571		},
572	},
573	.flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_CLOCK_NON_CONTINUOUS,
574	.format = MIPI_DSI_FMT_RGB888,
575	.lanes = 4,
576};
577
578static const struct drm_display_mode lg_lh500wx1_sd03_mode = {
579	.clock = 67000,
580	.hdisplay = 720,
581	.hsync_start = 720 + 12,
582	.hsync_end = 720 + 12 + 4,
583	.htotal = 720 + 12 + 4 + 112,
584	.vdisplay = 1280,
585	.vsync_start = 1280 + 8,
586	.vsync_end = 1280 + 8 + 4,
587	.vtotal = 1280 + 8 + 4 + 12,
588	.vrefresh = 60,
589};
590
591static const struct panel_desc_dsi lg_lh500wx1_sd03 = {
592	.desc = {
593		.modes = &lg_lh500wx1_sd03_mode,
594		.num_modes = 1,
595		.size = {
596			.width = 62,
597			.height = 110,
598		},
599	},
600	.flags = MIPI_DSI_MODE_VIDEO,
601	.format = MIPI_DSI_FMT_RGB888,
602	.lanes = 4,
603};
604
605static const struct drm_display_mode panasonic_vvx10f004b00_mode = {
606	.clock = 157200,
607	.hdisplay = 1920,
608	.hsync_start = 1920 + 154,
609	.hsync_end = 1920 + 154 + 16,
610	.htotal = 1920 + 154 + 16 + 32,
611	.vdisplay = 1200,
612	.vsync_start = 1200 + 17,
613	.vsync_end = 1200 + 17 + 2,
614	.vtotal = 1200 + 17 + 2 + 16,
615	.vrefresh = 60,
616};
617
618static const struct panel_desc_dsi panasonic_vvx10f004b00 = {
619	.desc = {
620		.modes = &panasonic_vvx10f004b00_mode,
621		.num_modes = 1,
622		.size = {
623			.width = 217,
624			.height = 136,
625		},
626	},
627	.flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
628		 MIPI_DSI_CLOCK_NON_CONTINUOUS,
629	.format = MIPI_DSI_FMT_RGB888,
630	.lanes = 4,
631};
632
633static const struct of_device_id dsi_of_match[] = {
634	{
635		.compatible = "lg,ld070wx3-sl01",
636		.data = &lg_ld070wx3_sl01
637	}, {
638		.compatible = "lg,lh500wx1-sd03",
639		.data = &lg_lh500wx1_sd03
640	}, {
641		.compatible = "panasonic,vvx10f004b00",
642		.data = &panasonic_vvx10f004b00
643	}, {
644		/* sentinel */
645	}
646};
647MODULE_DEVICE_TABLE(of, dsi_of_match);
648
649static int panel_simple_dsi_probe(struct mipi_dsi_device *dsi)
650{
651	const struct panel_desc_dsi *desc;
652	const struct of_device_id *id;
653	int err;
654
655	id = of_match_node(dsi_of_match, dsi->dev.of_node);
656	if (!id)
657		return -ENODEV;
658
659	desc = id->data;
660
661	err = panel_simple_probe(&dsi->dev, &desc->desc);
662	if (err < 0)
663		return err;
664
665	dsi->mode_flags = desc->flags;
666	dsi->format = desc->format;
667	dsi->lanes = desc->lanes;
668
669	return mipi_dsi_attach(dsi);
670}
671
672static int panel_simple_dsi_remove(struct mipi_dsi_device *dsi)
673{
674	int err;
675
676	err = mipi_dsi_detach(dsi);
677	if (err < 0)
678		dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
679
680	return panel_simple_remove(&dsi->dev);
681}
682
683static void panel_simple_dsi_shutdown(struct mipi_dsi_device *dsi)
684{
685	panel_simple_shutdown(&dsi->dev);
686}
687
688static struct mipi_dsi_driver panel_simple_dsi_driver = {
689	.driver = {
690		.name = "panel-simple-dsi",
691		.owner = THIS_MODULE,
692		.of_match_table = dsi_of_match,
693	},
694	.probe = panel_simple_dsi_probe,
695	.remove = panel_simple_dsi_remove,
696	.shutdown = panel_simple_dsi_shutdown,
697};
698
699static int __init panel_simple_init(void)
700{
701	int err;
702
703	err = platform_driver_register(&panel_simple_platform_driver);
704	if (err < 0)
705		return err;
706
707	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
708		err = mipi_dsi_driver_register(&panel_simple_dsi_driver);
709		if (err < 0)
710			return err;
711	}
712
713	return 0;
714}
715module_init(panel_simple_init);
716
717static void __exit panel_simple_exit(void)
718{
719	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
720		mipi_dsi_driver_unregister(&panel_simple_dsi_driver);
721
722	platform_driver_unregister(&panel_simple_platform_driver);
723}
724module_exit(panel_simple_exit);
725
726MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
727MODULE_DESCRIPTION("DRM Driver for Simple Panels");
728MODULE_LICENSE("GPL and additional rights");
729