xref: /openbmc/linux/drivers/video/fbdev/simplefb.c (revision 4e1a33b1)
1 /*
2  * Simplest possible simple frame-buffer driver, as a platform device
3  *
4  * Copyright (c) 2013, Stephen Warren
5  *
6  * Based on q40fb.c, which was:
7  * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org>
8  *
9  * Also based on offb.c, which was:
10  * Copyright (C) 1997 Geert Uytterhoeven
11  * Copyright (C) 1996 Paul Mackerras
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms and conditions of the GNU General Public License,
15  * version 2, as published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
20  * more details.
21  */
22 
23 #include <linux/errno.h>
24 #include <linux/fb.h>
25 #include <linux/io.h>
26 #include <linux/module.h>
27 #include <linux/platform_data/simplefb.h>
28 #include <linux/platform_device.h>
29 #include <linux/clk.h>
30 #include <linux/clk-provider.h>
31 #include <linux/of.h>
32 #include <linux/of_platform.h>
33 #include <linux/parser.h>
34 #include <linux/regulator/consumer.h>
35 
36 static const struct fb_fix_screeninfo simplefb_fix = {
37 	.id		= "simple",
38 	.type		= FB_TYPE_PACKED_PIXELS,
39 	.visual		= FB_VISUAL_TRUECOLOR,
40 	.accel		= FB_ACCEL_NONE,
41 };
42 
43 static const struct fb_var_screeninfo simplefb_var = {
44 	.height		= -1,
45 	.width		= -1,
46 	.activate	= FB_ACTIVATE_NOW,
47 	.vmode		= FB_VMODE_NONINTERLACED,
48 };
49 
50 #define PSEUDO_PALETTE_SIZE 16
51 
52 static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
53 			      u_int transp, struct fb_info *info)
54 {
55 	u32 *pal = info->pseudo_palette;
56 	u32 cr = red >> (16 - info->var.red.length);
57 	u32 cg = green >> (16 - info->var.green.length);
58 	u32 cb = blue >> (16 - info->var.blue.length);
59 	u32 value;
60 
61 	if (regno >= PSEUDO_PALETTE_SIZE)
62 		return -EINVAL;
63 
64 	value = (cr << info->var.red.offset) |
65 		(cg << info->var.green.offset) |
66 		(cb << info->var.blue.offset);
67 	if (info->var.transp.length > 0) {
68 		u32 mask = (1 << info->var.transp.length) - 1;
69 		mask <<= info->var.transp.offset;
70 		value |= mask;
71 	}
72 	pal[regno] = value;
73 
74 	return 0;
75 }
76 
77 struct simplefb_par;
78 static void simplefb_clocks_destroy(struct simplefb_par *par);
79 static void simplefb_regulators_destroy(struct simplefb_par *par);
80 
81 static void simplefb_destroy(struct fb_info *info)
82 {
83 	simplefb_regulators_destroy(info->par);
84 	simplefb_clocks_destroy(info->par);
85 	if (info->screen_base)
86 		iounmap(info->screen_base);
87 }
88 
89 static struct fb_ops simplefb_ops = {
90 	.owner		= THIS_MODULE,
91 	.fb_destroy	= simplefb_destroy,
92 	.fb_setcolreg	= simplefb_setcolreg,
93 	.fb_fillrect	= cfb_fillrect,
94 	.fb_copyarea	= cfb_copyarea,
95 	.fb_imageblit	= cfb_imageblit,
96 };
97 
98 static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
99 
100 struct simplefb_params {
101 	u32 width;
102 	u32 height;
103 	u32 stride;
104 	struct simplefb_format *format;
105 };
106 
107 static int simplefb_parse_dt(struct platform_device *pdev,
108 			   struct simplefb_params *params)
109 {
110 	struct device_node *np = pdev->dev.of_node;
111 	int ret;
112 	const char *format;
113 	int i;
114 
115 	ret = of_property_read_u32(np, "width", &params->width);
116 	if (ret) {
117 		dev_err(&pdev->dev, "Can't parse width property\n");
118 		return ret;
119 	}
120 
121 	ret = of_property_read_u32(np, "height", &params->height);
122 	if (ret) {
123 		dev_err(&pdev->dev, "Can't parse height property\n");
124 		return ret;
125 	}
126 
127 	ret = of_property_read_u32(np, "stride", &params->stride);
128 	if (ret) {
129 		dev_err(&pdev->dev, "Can't parse stride property\n");
130 		return ret;
131 	}
132 
133 	ret = of_property_read_string(np, "format", &format);
134 	if (ret) {
135 		dev_err(&pdev->dev, "Can't parse format property\n");
136 		return ret;
137 	}
138 	params->format = NULL;
139 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
140 		if (strcmp(format, simplefb_formats[i].name))
141 			continue;
142 		params->format = &simplefb_formats[i];
143 		break;
144 	}
145 	if (!params->format) {
146 		dev_err(&pdev->dev, "Invalid format value\n");
147 		return -EINVAL;
148 	}
149 
150 	return 0;
151 }
152 
153 static int simplefb_parse_pd(struct platform_device *pdev,
154 			     struct simplefb_params *params)
155 {
156 	struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
157 	int i;
158 
159 	params->width = pd->width;
160 	params->height = pd->height;
161 	params->stride = pd->stride;
162 
163 	params->format = NULL;
164 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
165 		if (strcmp(pd->format, simplefb_formats[i].name))
166 			continue;
167 
168 		params->format = &simplefb_formats[i];
169 		break;
170 	}
171 
172 	if (!params->format) {
173 		dev_err(&pdev->dev, "Invalid format value\n");
174 		return -EINVAL;
175 	}
176 
177 	return 0;
178 }
179 
180 struct simplefb_par {
181 	u32 palette[PSEUDO_PALETTE_SIZE];
182 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
183 	unsigned int clk_count;
184 	struct clk **clks;
185 #endif
186 #if defined CONFIG_OF && defined CONFIG_REGULATOR
187 	u32 regulator_count;
188 	struct regulator **regulators;
189 #endif
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_init(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, ret;
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 	for (i = 0; i < par->clk_count; i++) {
248 		if (par->clks[i]) {
249 			ret = clk_prepare_enable(par->clks[i]);
250 			if (ret) {
251 				dev_err(&pdev->dev,
252 					"%s: failed to enable clock %d: %d\n",
253 					__func__, i, ret);
254 				clk_put(par->clks[i]);
255 				par->clks[i] = NULL;
256 			}
257 		}
258 	}
259 
260 	return 0;
261 }
262 
263 static void simplefb_clocks_destroy(struct simplefb_par *par)
264 {
265 	int i;
266 
267 	if (!par->clks)
268 		return;
269 
270 	for (i = 0; i < par->clk_count; i++) {
271 		if (par->clks[i]) {
272 			clk_disable_unprepare(par->clks[i]);
273 			clk_put(par->clks[i]);
274 		}
275 	}
276 
277 	kfree(par->clks);
278 }
279 #else
280 static int simplefb_clocks_init(struct simplefb_par *par,
281 	struct platform_device *pdev) { return 0; }
282 static void simplefb_clocks_destroy(struct simplefb_par *par) { }
283 #endif
284 
285 #if defined CONFIG_OF && defined CONFIG_REGULATOR
286 
287 #define SUPPLY_SUFFIX "-supply"
288 
289 /*
290  * Regulator handling code.
291  *
292  * Here we handle the num-supplies and vin*-supply properties of our
293  * "simple-framebuffer" dt node. This is necessary so that we can make sure
294  * that any regulators needed by the display hardware that the bootloader
295  * set up for us (and for which it provided a simplefb dt node), stay up,
296  * for the life of the simplefb driver.
297  *
298  * When the driver unloads, we cleanly disable, and then release the
299  * regulators.
300  *
301  * We only complain about errors here, no action is taken as the most likely
302  * error can only happen due to a mismatch between the bootloader which set
303  * up simplefb, and the regulator definitions in the device tree. Chances are
304  * that there are no adverse effects, and if there are, a clean teardown of
305  * the fb probe will not help us much either. So just complain and carry on,
306  * and hope that the user actually gets a working fb at the end of things.
307  */
308 static int simplefb_regulators_init(struct simplefb_par *par,
309 	struct platform_device *pdev)
310 {
311 	struct device_node *np = pdev->dev.of_node;
312 	struct property *prop;
313 	struct regulator *regulator;
314 	const char *p;
315 	int count = 0, i = 0, ret;
316 
317 	if (dev_get_platdata(&pdev->dev) || !np)
318 		return 0;
319 
320 	/* Count the number of regulator supplies */
321 	for_each_property_of_node(np, prop) {
322 		p = strstr(prop->name, SUPPLY_SUFFIX);
323 		if (p && p != prop->name)
324 			count++;
325 	}
326 
327 	if (!count)
328 		return 0;
329 
330 	par->regulators = devm_kcalloc(&pdev->dev, count,
331 				       sizeof(struct regulator *), GFP_KERNEL);
332 	if (!par->regulators)
333 		return -ENOMEM;
334 
335 	/* Get all the regulators */
336 	for_each_property_of_node(np, prop) {
337 		char name[32]; /* 32 is max size of property name */
338 
339 		p = strstr(prop->name, SUPPLY_SUFFIX);
340 		if (!p || p == prop->name)
341 			continue;
342 
343 		strlcpy(name, prop->name,
344 			strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
345 		regulator = devm_regulator_get_optional(&pdev->dev, name);
346 		if (IS_ERR(regulator)) {
347 			if (PTR_ERR(regulator) == -EPROBE_DEFER)
348 				return -EPROBE_DEFER;
349 			dev_err(&pdev->dev, "regulator %s not found: %ld\n",
350 				name, PTR_ERR(regulator));
351 			continue;
352 		}
353 		par->regulators[i++] = regulator;
354 	}
355 	par->regulator_count = i;
356 
357 	/* Enable all the regulators */
358 	for (i = 0; i < par->regulator_count; i++) {
359 		ret = regulator_enable(par->regulators[i]);
360 		if (ret) {
361 			dev_err(&pdev->dev,
362 				"failed to enable regulator %d: %d\n",
363 				i, ret);
364 			devm_regulator_put(par->regulators[i]);
365 			par->regulators[i] = NULL;
366 		}
367 	}
368 
369 	return 0;
370 }
371 
372 static void simplefb_regulators_destroy(struct simplefb_par *par)
373 {
374 	int i;
375 
376 	if (!par->regulators)
377 		return;
378 
379 	for (i = 0; i < par->regulator_count; i++)
380 		if (par->regulators[i])
381 			regulator_disable(par->regulators[i]);
382 }
383 #else
384 static int simplefb_regulators_init(struct simplefb_par *par,
385 	struct platform_device *pdev) { return 0; }
386 static void simplefb_regulators_destroy(struct simplefb_par *par) { }
387 #endif
388 
389 static int simplefb_probe(struct platform_device *pdev)
390 {
391 	int ret;
392 	struct simplefb_params params;
393 	struct fb_info *info;
394 	struct simplefb_par *par;
395 	struct resource *mem;
396 
397 	if (fb_get_options("simplefb", NULL))
398 		return -ENODEV;
399 
400 	ret = -ENODEV;
401 	if (dev_get_platdata(&pdev->dev))
402 		ret = simplefb_parse_pd(pdev, &params);
403 	else if (pdev->dev.of_node)
404 		ret = simplefb_parse_dt(pdev, &params);
405 
406 	if (ret)
407 		return ret;
408 
409 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
410 	if (!mem) {
411 		dev_err(&pdev->dev, "No memory resource\n");
412 		return -EINVAL;
413 	}
414 
415 	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
416 	if (!info)
417 		return -ENOMEM;
418 	platform_set_drvdata(pdev, info);
419 
420 	par = info->par;
421 
422 	info->fix = simplefb_fix;
423 	info->fix.smem_start = mem->start;
424 	info->fix.smem_len = resource_size(mem);
425 	info->fix.line_length = params.stride;
426 
427 	info->var = simplefb_var;
428 	info->var.xres = params.width;
429 	info->var.yres = params.height;
430 	info->var.xres_virtual = params.width;
431 	info->var.yres_virtual = params.height;
432 	info->var.bits_per_pixel = params.format->bits_per_pixel;
433 	info->var.red = params.format->red;
434 	info->var.green = params.format->green;
435 	info->var.blue = params.format->blue;
436 	info->var.transp = params.format->transp;
437 
438 	info->apertures = alloc_apertures(1);
439 	if (!info->apertures) {
440 		ret = -ENOMEM;
441 		goto error_fb_release;
442 	}
443 	info->apertures->ranges[0].base = info->fix.smem_start;
444 	info->apertures->ranges[0].size = info->fix.smem_len;
445 
446 	info->fbops = &simplefb_ops;
447 	info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
448 	info->screen_base = ioremap_wc(info->fix.smem_start,
449 				       info->fix.smem_len);
450 	if (!info->screen_base) {
451 		ret = -ENOMEM;
452 		goto error_fb_release;
453 	}
454 	info->pseudo_palette = par->palette;
455 
456 	ret = simplefb_clocks_init(par, pdev);
457 	if (ret < 0)
458 		goto error_unmap;
459 
460 	ret = simplefb_regulators_init(par, pdev);
461 	if (ret < 0)
462 		goto error_clocks;
463 
464 	dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
465 			     info->fix.smem_start, info->fix.smem_len,
466 			     info->screen_base);
467 	dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
468 			     params.format->name,
469 			     info->var.xres, info->var.yres,
470 			     info->var.bits_per_pixel, info->fix.line_length);
471 
472 	ret = register_framebuffer(info);
473 	if (ret < 0) {
474 		dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
475 		goto error_regulators;
476 	}
477 
478 	dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
479 
480 	return 0;
481 
482 error_regulators:
483 	simplefb_regulators_destroy(par);
484 error_clocks:
485 	simplefb_clocks_destroy(par);
486 error_unmap:
487 	iounmap(info->screen_base);
488 error_fb_release:
489 	framebuffer_release(info);
490 	return ret;
491 }
492 
493 static int simplefb_remove(struct platform_device *pdev)
494 {
495 	struct fb_info *info = platform_get_drvdata(pdev);
496 
497 	unregister_framebuffer(info);
498 	framebuffer_release(info);
499 
500 	return 0;
501 }
502 
503 static const struct of_device_id simplefb_of_match[] = {
504 	{ .compatible = "simple-framebuffer", },
505 	{ },
506 };
507 MODULE_DEVICE_TABLE(of, simplefb_of_match);
508 
509 static struct platform_driver simplefb_driver = {
510 	.driver = {
511 		.name = "simple-framebuffer",
512 		.of_match_table = simplefb_of_match,
513 	},
514 	.probe = simplefb_probe,
515 	.remove = simplefb_remove,
516 };
517 
518 static int __init simplefb_init(void)
519 {
520 	int ret;
521 	struct device_node *np;
522 
523 	ret = platform_driver_register(&simplefb_driver);
524 	if (ret)
525 		return ret;
526 
527 	if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
528 		for_each_child_of_node(of_chosen, np) {
529 			if (of_device_is_compatible(np, "simple-framebuffer"))
530 				of_platform_device_create(np, NULL, NULL);
531 		}
532 	}
533 
534 	return 0;
535 }
536 
537 fs_initcall(simplefb_init);
538 
539 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
540 MODULE_DESCRIPTION("Simple framebuffer driver");
541 MODULE_LICENSE("GPL v2");
542