xref: /linux/drivers/video/fbdev/simplefb.c (revision 4cd24d4b1a9548f42cdb7f449edc6f869a8ae730)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Simplest possible simple frame-buffer driver, as a platform device
4  *
5  * Copyright (c) 2013, Stephen Warren
6  *
7  * Based on q40fb.c, which was:
8  * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org>
9  *
10  * Also based on offb.c, which was:
11  * Copyright (C) 1997 Geert Uytterhoeven
12  * Copyright (C) 1996 Paul Mackerras
13  */
14 
15 #include <linux/aperture.h>
16 #include <linux/errno.h>
17 #include <linux/fb.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/platform_data/simplefb.h>
21 #include <linux/platform_device.h>
22 #include <linux/clk.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/of_clk.h>
26 #include <linux/of_platform.h>
27 #include <linux/parser.h>
28 #include <linux/pm_domain.h>
29 #include <linux/regulator/consumer.h>
30 
31 static const struct fb_fix_screeninfo simplefb_fix = {
32 	.id		= "simple",
33 	.type		= FB_TYPE_PACKED_PIXELS,
34 	.visual		= FB_VISUAL_TRUECOLOR,
35 	.accel		= FB_ACCEL_NONE,
36 };
37 
38 static const struct fb_var_screeninfo simplefb_var = {
39 	.height		= -1,
40 	.width		= -1,
41 	.activate	= FB_ACTIVATE_NOW,
42 	.vmode		= FB_VMODE_NONINTERLACED,
43 };
44 
45 #define PSEUDO_PALETTE_SIZE 16
46 
47 static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
48 			      u_int transp, struct fb_info *info)
49 {
50 	u32 *pal = info->pseudo_palette;
51 	u32 cr = red >> (16 - info->var.red.length);
52 	u32 cg = green >> (16 - info->var.green.length);
53 	u32 cb = blue >> (16 - info->var.blue.length);
54 	u32 value;
55 
56 	if (regno >= PSEUDO_PALETTE_SIZE)
57 		return -EINVAL;
58 
59 	value = (cr << info->var.red.offset) |
60 		(cg << info->var.green.offset) |
61 		(cb << info->var.blue.offset);
62 	if (info->var.transp.length > 0) {
63 		u32 mask = (1 << info->var.transp.length) - 1;
64 		mask <<= info->var.transp.offset;
65 		value |= mask;
66 	}
67 	pal[regno] = value;
68 
69 	return 0;
70 }
71 
72 struct simplefb_par {
73 	u32 palette[PSEUDO_PALETTE_SIZE];
74 	resource_size_t base;
75 	resource_size_t size;
76 	struct resource *mem;
77 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
78 	bool clks_enabled;
79 	unsigned int clk_count;
80 	struct clk **clks;
81 #endif
82 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
83 	unsigned int num_genpds;
84 	struct device **genpds;
85 	struct device_link **genpd_links;
86 #endif
87 #if defined CONFIG_OF && defined CONFIG_REGULATOR
88 	bool regulators_enabled;
89 	u32 regulator_count;
90 	struct regulator **regulators;
91 #endif
92 };
93 
94 static void simplefb_clocks_destroy(struct simplefb_par *par);
95 static void simplefb_regulators_destroy(struct simplefb_par *par);
96 
97 /*
98  * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
99  * of unregister_framebuffer() or fb_release(). Do any cleanup here.
100  */
101 static void simplefb_destroy(struct fb_info *info)
102 {
103 	struct simplefb_par *par = info->par;
104 	struct resource *mem = par->mem;
105 
106 	simplefb_regulators_destroy(info->par);
107 	simplefb_clocks_destroy(info->par);
108 	if (info->screen_base)
109 		iounmap(info->screen_base);
110 
111 	framebuffer_release(info);
112 
113 	if (mem)
114 		release_mem_region(mem->start, resource_size(mem));
115 }
116 
117 static const struct fb_ops simplefb_ops = {
118 	.owner		= THIS_MODULE,
119 	FB_DEFAULT_IOMEM_OPS,
120 	.fb_destroy	= simplefb_destroy,
121 	.fb_setcolreg	= simplefb_setcolreg,
122 };
123 
124 static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
125 
126 struct simplefb_params {
127 	u32 width;
128 	u32 height;
129 	u32 stride;
130 	struct simplefb_format *format;
131 	struct resource memory;
132 };
133 
134 static int simplefb_parse_dt(struct platform_device *pdev,
135 			   struct simplefb_params *params)
136 {
137 	struct device_node *np = pdev->dev.of_node, *mem;
138 	int ret;
139 	const char *format;
140 	int i;
141 
142 	ret = of_property_read_u32(np, "width", &params->width);
143 	if (ret) {
144 		dev_err(&pdev->dev, "Can't parse width property\n");
145 		return ret;
146 	}
147 
148 	ret = of_property_read_u32(np, "height", &params->height);
149 	if (ret) {
150 		dev_err(&pdev->dev, "Can't parse height property\n");
151 		return ret;
152 	}
153 
154 	ret = of_property_read_u32(np, "stride", &params->stride);
155 	if (ret) {
156 		dev_err(&pdev->dev, "Can't parse stride property\n");
157 		return ret;
158 	}
159 
160 	ret = of_property_read_string(np, "format", &format);
161 	if (ret) {
162 		dev_err(&pdev->dev, "Can't parse format property\n");
163 		return ret;
164 	}
165 	params->format = NULL;
166 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
167 		if (strcmp(format, simplefb_formats[i].name))
168 			continue;
169 		params->format = &simplefb_formats[i];
170 		break;
171 	}
172 	if (!params->format) {
173 		dev_err(&pdev->dev, "Invalid format value\n");
174 		return -EINVAL;
175 	}
176 
177 	mem = of_parse_phandle(np, "memory-region", 0);
178 	if (mem) {
179 		ret = of_address_to_resource(mem, 0, &params->memory);
180 		if (ret < 0) {
181 			dev_err(&pdev->dev, "failed to parse memory-region\n");
182 			of_node_put(mem);
183 			return ret;
184 		}
185 
186 		if (of_property_present(np, "reg"))
187 			dev_warn(&pdev->dev, "preferring \"memory-region\" over \"reg\" property\n");
188 
189 		of_node_put(mem);
190 	} else {
191 		memset(&params->memory, 0, sizeof(params->memory));
192 	}
193 
194 	return 0;
195 }
196 
197 static int simplefb_parse_pd(struct platform_device *pdev,
198 			     struct simplefb_params *params)
199 {
200 	struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
201 	int i;
202 
203 	params->width = pd->width;
204 	params->height = pd->height;
205 	params->stride = pd->stride;
206 
207 	params->format = NULL;
208 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
209 		if (strcmp(pd->format, simplefb_formats[i].name))
210 			continue;
211 
212 		params->format = &simplefb_formats[i];
213 		break;
214 	}
215 
216 	if (!params->format) {
217 		dev_err(&pdev->dev, "Invalid format value\n");
218 		return -EINVAL;
219 	}
220 
221 	memset(&params->memory, 0, sizeof(params->memory));
222 
223 	return 0;
224 }
225 
226 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
227 /*
228  * Clock handling code.
229  *
230  * Here we handle the clocks property of our "simple-framebuffer" dt node.
231  * This is necessary so that we can make sure that any clocks needed by
232  * the display engine that the bootloader set up for us (and for which it
233  * provided a simplefb dt node), stay up, for the life of the simplefb
234  * driver.
235  *
236  * When the driver unloads, we cleanly disable, and then release the clocks.
237  *
238  * We only complain about errors here, no action is taken as the most likely
239  * error can only happen due to a mismatch between the bootloader which set
240  * up simplefb, and the clock definitions in the device tree. Chances are
241  * that there are no adverse effects, and if there are, a clean teardown of
242  * the fb probe will not help us much either. So just complain and carry on,
243  * and hope that the user actually gets a working fb at the end of things.
244  */
245 static int simplefb_clocks_get(struct simplefb_par *par,
246 			       struct platform_device *pdev)
247 {
248 	struct device_node *np = pdev->dev.of_node;
249 	struct clk *clock;
250 	int i;
251 
252 	if (dev_get_platdata(&pdev->dev) || !np)
253 		return 0;
254 
255 	par->clk_count = of_clk_get_parent_count(np);
256 	if (!par->clk_count)
257 		return 0;
258 
259 	par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
260 	if (!par->clks)
261 		return -ENOMEM;
262 
263 	for (i = 0; i < par->clk_count; i++) {
264 		clock = of_clk_get(np, i);
265 		if (IS_ERR(clock)) {
266 			if (PTR_ERR(clock) == -EPROBE_DEFER) {
267 				while (--i >= 0) {
268 					clk_put(par->clks[i]);
269 				}
270 				kfree(par->clks);
271 				return -EPROBE_DEFER;
272 			}
273 			dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
274 				__func__, i, PTR_ERR(clock));
275 			continue;
276 		}
277 		par->clks[i] = clock;
278 	}
279 
280 	return 0;
281 }
282 
283 static void simplefb_clocks_enable(struct simplefb_par *par,
284 				   struct platform_device *pdev)
285 {
286 	int i, ret;
287 
288 	for (i = 0; i < par->clk_count; i++) {
289 		if (par->clks[i]) {
290 			ret = clk_prepare_enable(par->clks[i]);
291 			if (ret) {
292 				dev_err(&pdev->dev,
293 					"%s: failed to enable clock %d: %d\n",
294 					__func__, i, ret);
295 				clk_put(par->clks[i]);
296 				par->clks[i] = NULL;
297 			}
298 		}
299 	}
300 	par->clks_enabled = true;
301 }
302 
303 static void simplefb_clocks_destroy(struct simplefb_par *par)
304 {
305 	int i;
306 
307 	if (!par->clks)
308 		return;
309 
310 	for (i = 0; i < par->clk_count; i++) {
311 		if (par->clks[i]) {
312 			if (par->clks_enabled)
313 				clk_disable_unprepare(par->clks[i]);
314 			clk_put(par->clks[i]);
315 		}
316 	}
317 
318 	kfree(par->clks);
319 }
320 #else
321 static int simplefb_clocks_get(struct simplefb_par *par,
322 	struct platform_device *pdev) { return 0; }
323 static void simplefb_clocks_enable(struct simplefb_par *par,
324 	struct platform_device *pdev) { }
325 static void simplefb_clocks_destroy(struct simplefb_par *par) { }
326 #endif
327 
328 #if defined CONFIG_OF && defined CONFIG_REGULATOR
329 
330 #define SUPPLY_SUFFIX "-supply"
331 
332 /*
333  * Regulator handling code.
334  *
335  * Here we handle the num-supplies and vin*-supply properties of our
336  * "simple-framebuffer" dt node. This is necessary so that we can make sure
337  * that any regulators needed by the display hardware that the bootloader
338  * set up for us (and for which it provided a simplefb dt node), stay up,
339  * for the life of the simplefb driver.
340  *
341  * When the driver unloads, we cleanly disable, and then release the
342  * regulators.
343  *
344  * We only complain about errors here, no action is taken as the most likely
345  * error can only happen due to a mismatch between the bootloader which set
346  * up simplefb, and the regulator definitions in the device tree. Chances are
347  * that there are no adverse effects, and if there are, a clean teardown of
348  * the fb probe will not help us much either. So just complain and carry on,
349  * and hope that the user actually gets a working fb at the end of things.
350  */
351 static int simplefb_regulators_get(struct simplefb_par *par,
352 				   struct platform_device *pdev)
353 {
354 	struct device_node *np = pdev->dev.of_node;
355 	struct property *prop;
356 	struct regulator *regulator;
357 	const char *p;
358 	int count = 0, i = 0;
359 
360 	if (dev_get_platdata(&pdev->dev) || !np)
361 		return 0;
362 
363 	/* Count the number of regulator supplies */
364 	for_each_property_of_node(np, prop) {
365 		p = strstr(prop->name, SUPPLY_SUFFIX);
366 		if (p && p != prop->name)
367 			count++;
368 	}
369 
370 	if (!count)
371 		return 0;
372 
373 	par->regulators = devm_kcalloc(&pdev->dev, count,
374 				       sizeof(struct regulator *), GFP_KERNEL);
375 	if (!par->regulators)
376 		return -ENOMEM;
377 
378 	/* Get all the regulators */
379 	for_each_property_of_node(np, prop) {
380 		char name[32]; /* 32 is max size of property name */
381 
382 		p = strstr(prop->name, SUPPLY_SUFFIX);
383 		if (!p || p == prop->name)
384 			continue;
385 
386 		strscpy(name, prop->name,
387 			strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
388 		regulator = devm_regulator_get_optional(&pdev->dev, name);
389 		if (IS_ERR(regulator)) {
390 			if (PTR_ERR(regulator) == -EPROBE_DEFER)
391 				return -EPROBE_DEFER;
392 			dev_err(&pdev->dev, "regulator %s not found: %ld\n",
393 				name, PTR_ERR(regulator));
394 			continue;
395 		}
396 		par->regulators[i++] = regulator;
397 	}
398 	par->regulator_count = i;
399 
400 	return 0;
401 }
402 
403 static void simplefb_regulators_enable(struct simplefb_par *par,
404 				       struct platform_device *pdev)
405 {
406 	int i, ret;
407 
408 	/* Enable all the regulators */
409 	for (i = 0; i < par->regulator_count; i++) {
410 		ret = regulator_enable(par->regulators[i]);
411 		if (ret) {
412 			dev_err(&pdev->dev,
413 				"failed to enable regulator %d: %d\n",
414 				i, ret);
415 			devm_regulator_put(par->regulators[i]);
416 			par->regulators[i] = NULL;
417 		}
418 	}
419 	par->regulators_enabled = true;
420 }
421 
422 static void simplefb_regulators_destroy(struct simplefb_par *par)
423 {
424 	int i;
425 
426 	if (!par->regulators || !par->regulators_enabled)
427 		return;
428 
429 	for (i = 0; i < par->regulator_count; i++)
430 		if (par->regulators[i])
431 			regulator_disable(par->regulators[i]);
432 }
433 #else
434 static int simplefb_regulators_get(struct simplefb_par *par,
435 	struct platform_device *pdev) { return 0; }
436 static void simplefb_regulators_enable(struct simplefb_par *par,
437 	struct platform_device *pdev) { }
438 static void simplefb_regulators_destroy(struct simplefb_par *par) { }
439 #endif
440 
441 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
442 static void simplefb_detach_genpds(void *res)
443 {
444 	struct simplefb_par *par = res;
445 	unsigned int i = par->num_genpds;
446 
447 	if (par->num_genpds <= 1)
448 		return;
449 
450 	while (i--) {
451 		if (par->genpd_links[i])
452 			device_link_del(par->genpd_links[i]);
453 
454 		if (!IS_ERR_OR_NULL(par->genpds[i]))
455 			dev_pm_domain_detach(par->genpds[i], true);
456 	}
457 }
458 
459 static int simplefb_attach_genpds(struct simplefb_par *par,
460 				  struct platform_device *pdev)
461 {
462 	struct device *dev = &pdev->dev;
463 	unsigned int i;
464 	int err;
465 
466 	err = of_count_phandle_with_args(dev->of_node, "power-domains",
467 					 "#power-domain-cells");
468 	if (err < 0) {
469 		dev_info(dev, "failed to parse power-domains: %d\n", err);
470 		return err;
471 	}
472 
473 	par->num_genpds = err;
474 
475 	/*
476 	 * Single power-domain devices are handled by the driver core, so
477 	 * nothing to do here.
478 	 */
479 	if (par->num_genpds <= 1)
480 		return 0;
481 
482 	par->genpds = devm_kcalloc(dev, par->num_genpds, sizeof(*par->genpds),
483 				   GFP_KERNEL);
484 	if (!par->genpds)
485 		return -ENOMEM;
486 
487 	par->genpd_links = devm_kcalloc(dev, par->num_genpds,
488 					sizeof(*par->genpd_links),
489 					GFP_KERNEL);
490 	if (!par->genpd_links)
491 		return -ENOMEM;
492 
493 	for (i = 0; i < par->num_genpds; i++) {
494 		par->genpds[i] = dev_pm_domain_attach_by_id(dev, i);
495 		if (IS_ERR(par->genpds[i])) {
496 			err = PTR_ERR(par->genpds[i]);
497 			if (err == -EPROBE_DEFER) {
498 				simplefb_detach_genpds(par);
499 				return err;
500 			}
501 
502 			dev_warn(dev, "failed to attach domain %u: %d\n", i, err);
503 			continue;
504 		}
505 
506 		par->genpd_links[i] = device_link_add(dev, par->genpds[i],
507 						      DL_FLAG_STATELESS |
508 						      DL_FLAG_PM_RUNTIME |
509 						      DL_FLAG_RPM_ACTIVE);
510 		if (!par->genpd_links[i])
511 			dev_warn(dev, "failed to link power-domain %u\n", i);
512 	}
513 
514 	return devm_add_action_or_reset(dev, simplefb_detach_genpds, par);
515 }
516 #else
517 static int simplefb_attach_genpds(struct simplefb_par *par,
518 				  struct platform_device *pdev)
519 {
520 	return 0;
521 }
522 #endif
523 
524 static int simplefb_probe(struct platform_device *pdev)
525 {
526 	int ret;
527 	struct simplefb_params params;
528 	struct fb_info *info;
529 	struct simplefb_par *par;
530 	struct resource *res, *mem;
531 
532 	if (fb_get_options("simplefb", NULL))
533 		return -ENODEV;
534 
535 	ret = -ENODEV;
536 	if (dev_get_platdata(&pdev->dev))
537 		ret = simplefb_parse_pd(pdev, &params);
538 	else if (pdev->dev.of_node)
539 		ret = simplefb_parse_dt(pdev, &params);
540 
541 	if (ret)
542 		return ret;
543 
544 	if (params.memory.start == 0 && params.memory.end == 0) {
545 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
546 		if (!res) {
547 			dev_err(&pdev->dev, "No memory resource\n");
548 			return -EINVAL;
549 		}
550 	} else {
551 		res = &params.memory;
552 	}
553 
554 	mem = request_mem_region(res->start, resource_size(res), "simplefb");
555 	if (!mem) {
556 		/*
557 		 * We cannot make this fatal. Sometimes this comes from magic
558 		 * spaces our resource handlers simply don't know about. Use
559 		 * the I/O-memory resource as-is and try to map that instead.
560 		 */
561 		dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res);
562 		mem = res;
563 	}
564 
565 	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
566 	if (!info) {
567 		ret = -ENOMEM;
568 		goto error_release_mem_region;
569 	}
570 	platform_set_drvdata(pdev, info);
571 
572 	par = info->par;
573 
574 	info->fix = simplefb_fix;
575 	info->fix.smem_start = mem->start;
576 	info->fix.smem_len = resource_size(mem);
577 	info->fix.line_length = params.stride;
578 
579 	info->var = simplefb_var;
580 	info->var.xres = params.width;
581 	info->var.yres = params.height;
582 	info->var.xres_virtual = params.width;
583 	info->var.yres_virtual = params.height;
584 	info->var.bits_per_pixel = params.format->bits_per_pixel;
585 	info->var.red = params.format->red;
586 	info->var.green = params.format->green;
587 	info->var.blue = params.format->blue;
588 	info->var.transp = params.format->transp;
589 
590 	par->base = info->fix.smem_start;
591 	par->size = info->fix.smem_len;
592 
593 	info->fbops = &simplefb_ops;
594 	info->screen_base = ioremap_wc(info->fix.smem_start,
595 				       info->fix.smem_len);
596 	if (!info->screen_base) {
597 		ret = -ENOMEM;
598 		goto error_fb_release;
599 	}
600 	info->pseudo_palette = par->palette;
601 
602 	ret = simplefb_clocks_get(par, pdev);
603 	if (ret < 0)
604 		goto error_unmap;
605 
606 	ret = simplefb_regulators_get(par, pdev);
607 	if (ret < 0)
608 		goto error_clocks;
609 
610 	ret = simplefb_attach_genpds(par, pdev);
611 	if (ret < 0)
612 		goto error_regulators;
613 
614 	simplefb_clocks_enable(par, pdev);
615 	simplefb_regulators_enable(par, pdev);
616 
617 	dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n",
618 			     info->fix.smem_start, info->fix.smem_len);
619 	dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
620 			     params.format->name,
621 			     info->var.xres, info->var.yres,
622 			     info->var.bits_per_pixel, info->fix.line_length);
623 
624 	if (mem != res)
625 		par->mem = mem; /* release in clean-up handler */
626 
627 	ret = devm_aperture_acquire_for_platform_device(pdev, par->base, par->size);
628 	if (ret) {
629 		dev_err(&pdev->dev, "Unable to acquire aperture: %d\n", ret);
630 		goto error_regulators;
631 	}
632 	ret = register_framebuffer(info);
633 	if (ret < 0) {
634 		dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
635 		goto error_regulators;
636 	}
637 
638 	dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
639 
640 	return 0;
641 
642 error_regulators:
643 	simplefb_regulators_destroy(par);
644 error_clocks:
645 	simplefb_clocks_destroy(par);
646 error_unmap:
647 	iounmap(info->screen_base);
648 error_fb_release:
649 	framebuffer_release(info);
650 error_release_mem_region:
651 	if (mem != res)
652 		release_mem_region(mem->start, resource_size(mem));
653 	return ret;
654 }
655 
656 static void simplefb_remove(struct platform_device *pdev)
657 {
658 	struct fb_info *info = platform_get_drvdata(pdev);
659 
660 	/* simplefb_destroy takes care of info cleanup */
661 	unregister_framebuffer(info);
662 }
663 
664 static const struct of_device_id simplefb_of_match[] = {
665 	{ .compatible = "simple-framebuffer", },
666 	{ },
667 };
668 MODULE_DEVICE_TABLE(of, simplefb_of_match);
669 
670 static struct platform_driver simplefb_driver = {
671 	.driver = {
672 		.name = "simple-framebuffer",
673 		.of_match_table = simplefb_of_match,
674 	},
675 	.probe = simplefb_probe,
676 	.remove_new = simplefb_remove,
677 };
678 
679 module_platform_driver(simplefb_driver);
680 
681 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
682 MODULE_DESCRIPTION("Simple framebuffer driver");
683 MODULE_LICENSE("GPL v2");
684