panel-simple.c revision 6d54e3d275de861c0290c85fec8c0ed6deaf6ad5
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
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
319static 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
332static 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
341static const struct drm_display_mode samsung_ltn101nt05_mode = {
342	.clock = 54030,
343	.hdisplay = 1024,
344	.hsync_start = 1024 + 24,
345	.hsync_end = 1024 + 24 + 136,
346	.htotal = 1024 + 24 + 136 + 160,
347	.vdisplay = 600,
348	.vsync_start = 600 + 3,
349	.vsync_end = 600 + 3 + 6,
350	.vtotal = 600 + 3 + 6 + 61,
351	.vrefresh = 60,
352};
353
354static const struct panel_desc samsung_ltn101nt05 = {
355	.modes = &samsung_ltn101nt05_mode,
356	.num_modes = 1,
357	.size = {
358		.width = 1024,
359		.height = 600,
360	},
361};
362
363static const struct of_device_id platform_of_match[] = {
364	{
365		.compatible = "auo,b101aw03",
366		.data = &auo_b101aw03,
367	}, {
368		.compatible = "chunghwa,claa101wb01",
369		.data = &chunghwa_claa101wb01
370	}, {
371		.compatible = "samsung,ltn101nt05",
372		.data = &samsung_ltn101nt05,
373	}, {
374		.compatible = "simple-panel",
375	}, {
376		/* sentinel */
377	}
378};
379MODULE_DEVICE_TABLE(of, platform_of_match);
380
381static int panel_simple_platform_probe(struct platform_device *pdev)
382{
383	const struct of_device_id *id;
384
385	id = of_match_node(platform_of_match, pdev->dev.of_node);
386	if (!id)
387		return -ENODEV;
388
389	return panel_simple_probe(&pdev->dev, id->data);
390}
391
392static int panel_simple_platform_remove(struct platform_device *pdev)
393{
394	return panel_simple_remove(&pdev->dev);
395}
396
397static struct platform_driver panel_simple_platform_driver = {
398	.driver = {
399		.name = "panel-simple",
400		.owner = THIS_MODULE,
401		.of_match_table = platform_of_match,
402	},
403	.probe = panel_simple_platform_probe,
404	.remove = panel_simple_platform_remove,
405};
406
407struct panel_desc_dsi {
408	struct panel_desc desc;
409
410	enum mipi_dsi_pixel_format format;
411	unsigned int lanes;
412};
413
414static const struct drm_display_mode panasonic_vvx10f004b00_mode = {
415	.clock = 157200,
416	.hdisplay = 1920,
417	.hsync_start = 1920 + 154,
418	.hsync_end = 1920 + 154 + 16,
419	.htotal = 1920 + 154 + 16 + 32,
420	.vdisplay = 1200,
421	.vsync_start = 1200 + 17,
422	.vsync_end = 1200 + 17 + 2,
423	.vtotal = 1200 + 17 + 2 + 16,
424	.vrefresh = 60,
425};
426
427static const struct panel_desc_dsi panasonic_vvx10f004b00 = {
428	.desc = {
429		.modes = &panasonic_vvx10f004b00_mode,
430		.num_modes = 1,
431		.size = {
432			.width = 217,
433			.height = 136,
434		},
435	},
436	.format = MIPI_DSI_FMT_RGB888,
437	.lanes = 4,
438};
439
440static const struct of_device_id dsi_of_match[] = {
441	{
442		.compatible = "panasonic,vvx10f004b00",
443		.data = &panasonic_vvx10f004b00
444	}, {
445		/* sentinel */
446	}
447};
448MODULE_DEVICE_TABLE(of, dsi_of_match);
449
450static int panel_simple_dsi_probe(struct mipi_dsi_device *dsi)
451{
452	const struct panel_desc_dsi *desc;
453	const struct of_device_id *id;
454	int err;
455
456	id = of_match_node(dsi_of_match, dsi->dev.of_node);
457	if (!id)
458		return -ENODEV;
459
460	desc = id->data;
461
462	err = panel_simple_probe(&dsi->dev, &desc->desc);
463	if (err < 0)
464		return err;
465
466	dsi->format = desc->format;
467	dsi->lanes = desc->lanes;
468
469	return mipi_dsi_attach(dsi);
470}
471
472static int panel_simple_dsi_remove(struct mipi_dsi_device *dsi)
473{
474	int err;
475
476	err = mipi_dsi_detach(dsi);
477	if (err < 0)
478		dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
479
480	return panel_simple_remove(&dsi->dev);
481}
482
483static struct mipi_dsi_driver panel_simple_dsi_driver = {
484	.driver = {
485		.name = "panel-simple-dsi",
486		.owner = THIS_MODULE,
487		.of_match_table = dsi_of_match,
488	},
489	.probe = panel_simple_dsi_probe,
490	.remove = panel_simple_dsi_remove,
491};
492
493static int __init panel_simple_init(void)
494{
495	int err;
496
497	err = platform_driver_register(&panel_simple_platform_driver);
498	if (err < 0)
499		return err;
500
501	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
502		err = mipi_dsi_driver_register(&panel_simple_dsi_driver);
503		if (err < 0)
504			return err;
505	}
506
507	return 0;
508}
509module_init(panel_simple_init);
510
511static void __exit panel_simple_exit(void)
512{
513	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
514		mipi_dsi_driver_unregister(&panel_simple_dsi_driver);
515
516	platform_driver_unregister(&panel_simple_platform_driver);
517}
518module_exit(panel_simple_exit);
519
520MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
521MODULE_DESCRIPTION("DRM Driver for Simple Panels");
522MODULE_LICENSE("GPL and additional rights");
523