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