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