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