1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simplest possible simple frame-buffer driver, as a platform device
5 * Copyright (c) 2013, Stephen Warren
7 * Based on q40fb.c, which was:
8 * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org>
10 * Also based on offb.c, which was:
11 * Copyright (C) 1997 Geert Uytterhoeven
12 * Copyright (C) 1996 Paul Mackerras
15 #include <linux/errno.h>
18 #include <linux/module.h>
19 #include <linux/platform_data/simplefb.h>
20 #include <linux/platform_device.h>
21 #include <linux/clk.h>
23 #include <linux/of_clk.h>
24 #include <linux/of_platform.h>
25 #include <linux/parser.h>
26 #include <linux/regulator/consumer.h>
28 static const struct fb_fix_screeninfo simplefb_fix = {
30 .type = FB_TYPE_PACKED_PIXELS,
31 .visual = FB_VISUAL_TRUECOLOR,
32 .accel = FB_ACCEL_NONE,
35 static const struct fb_var_screeninfo simplefb_var = {
38 .activate = FB_ACTIVATE_NOW,
39 .vmode = FB_VMODE_NONINTERLACED,
42 #define PSEUDO_PALETTE_SIZE 16
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)
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);
53 if (regno >= PSEUDO_PALETTE_SIZE)
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;
70 u32 palette[PSEUDO_PALETTE_SIZE];
72 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
74 unsigned int clk_count;
77 #if defined CONFIG_OF && defined CONFIG_REGULATOR
78 bool regulators_enabled;
80 struct regulator **regulators;
84 static void simplefb_clocks_destroy(struct simplefb_par *par);
85 static void simplefb_regulators_destroy(struct simplefb_par *par);
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.
91 static void simplefb_destroy(struct fb_info *info)
93 struct simplefb_par *par = info->par;
94 struct resource *mem = par->mem;
96 simplefb_regulators_destroy(info->par);
97 simplefb_clocks_destroy(info->par);
98 if (info->screen_base)
99 iounmap(info->screen_base);
101 framebuffer_release(info);
104 release_mem_region(mem->start, resource_size(mem));
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,
116 static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
118 struct simplefb_params {
122 struct simplefb_format *format;
125 static int simplefb_parse_dt(struct platform_device *pdev,
126 struct simplefb_params *params)
128 struct device_node *np = pdev->dev.of_node;
133 ret = of_property_read_u32(np, "width", ¶ms->width);
135 dev_err(&pdev->dev, "Can't parse width property\n");
139 ret = of_property_read_u32(np, "height", ¶ms->height);
141 dev_err(&pdev->dev, "Can't parse height property\n");
145 ret = of_property_read_u32(np, "stride", ¶ms->stride);
147 dev_err(&pdev->dev, "Can't parse stride property\n");
151 ret = of_property_read_string(np, "format", &format);
153 dev_err(&pdev->dev, "Can't parse format property\n");
156 params->format = NULL;
157 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
158 if (strcmp(format, simplefb_formats[i].name))
160 params->format = &simplefb_formats[i];
163 if (!params->format) {
164 dev_err(&pdev->dev, "Invalid format value\n");
171 static int simplefb_parse_pd(struct platform_device *pdev,
172 struct simplefb_params *params)
174 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
177 params->width = pd->width;
178 params->height = pd->height;
179 params->stride = pd->stride;
181 params->format = NULL;
182 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
183 if (strcmp(pd->format, simplefb_formats[i].name))
186 params->format = &simplefb_formats[i];
190 if (!params->format) {
191 dev_err(&pdev->dev, "Invalid format value\n");
198 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
200 * Clock handling code.
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
208 * When the driver unloads, we cleanly disable, and then release the clocks.
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.
217 static int simplefb_clocks_get(struct simplefb_par *par,
218 struct platform_device *pdev)
220 struct device_node *np = pdev->dev.of_node;
224 if (dev_get_platdata(&pdev->dev) || !np)
227 par->clk_count = of_clk_get_parent_count(np);
231 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
235 for (i = 0; i < par->clk_count; i++) {
236 clock = of_clk_get(np, i);
238 if (PTR_ERR(clock) == -EPROBE_DEFER) {
240 clk_put(par->clks[i]);
243 return -EPROBE_DEFER;
245 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
246 __func__, i, PTR_ERR(clock));
249 par->clks[i] = clock;
255 static void simplefb_clocks_enable(struct simplefb_par *par,
256 struct platform_device *pdev)
260 for (i = 0; i < par->clk_count; i++) {
262 ret = clk_prepare_enable(par->clks[i]);
265 "%s: failed to enable clock %d: %d\n",
267 clk_put(par->clks[i]);
272 par->clks_enabled = true;
275 static void simplefb_clocks_destroy(struct simplefb_par *par)
282 for (i = 0; i < par->clk_count; i++) {
284 if (par->clks_enabled)
285 clk_disable_unprepare(par->clks[i]);
286 clk_put(par->clks[i]);
293 static int simplefb_clocks_get(struct simplefb_par *par,
294 struct platform_device *pdev) { return 0; }
295 static void simplefb_clocks_enable(struct simplefb_par *par,
296 struct platform_device *pdev) { }
297 static void simplefb_clocks_destroy(struct simplefb_par *par) { }
300 #if defined CONFIG_OF && defined CONFIG_REGULATOR
302 #define SUPPLY_SUFFIX "-supply"
305 * Regulator handling code.
307 * Here we handle the num-supplies and vin*-supply properties of our
308 * "simple-framebuffer" dt node. This is necessary so that we can make sure
309 * that any regulators needed by the display hardware that the bootloader
310 * set up for us (and for which it provided a simplefb dt node), stay up,
311 * for the life of the simplefb driver.
313 * When the driver unloads, we cleanly disable, and then release the
316 * We only complain about errors here, no action is taken as the most likely
317 * error can only happen due to a mismatch between the bootloader which set
318 * up simplefb, and the regulator definitions in the device tree. Chances are
319 * that there are no adverse effects, and if there are, a clean teardown of
320 * the fb probe will not help us much either. So just complain and carry on,
321 * and hope that the user actually gets a working fb at the end of things.
323 static int simplefb_regulators_get(struct simplefb_par *par,
324 struct platform_device *pdev)
326 struct device_node *np = pdev->dev.of_node;
327 struct property *prop;
328 struct regulator *regulator;
330 int count = 0, i = 0;
332 if (dev_get_platdata(&pdev->dev) || !np)
335 /* Count the number of regulator supplies */
336 for_each_property_of_node(np, prop) {
337 p = strstr(prop->name, SUPPLY_SUFFIX);
338 if (p && p != prop->name)
345 par->regulators = devm_kcalloc(&pdev->dev, count,
346 sizeof(struct regulator *), GFP_KERNEL);
347 if (!par->regulators)
350 /* Get all the regulators */
351 for_each_property_of_node(np, prop) {
352 char name[32]; /* 32 is max size of property name */
354 p = strstr(prop->name, SUPPLY_SUFFIX);
355 if (!p || p == prop->name)
358 strscpy(name, prop->name,
359 strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
360 regulator = devm_regulator_get_optional(&pdev->dev, name);
361 if (IS_ERR(regulator)) {
362 if (PTR_ERR(regulator) == -EPROBE_DEFER)
363 return -EPROBE_DEFER;
364 dev_err(&pdev->dev, "regulator %s not found: %ld\n",
365 name, PTR_ERR(regulator));
368 par->regulators[i++] = regulator;
370 par->regulator_count = i;
375 static void simplefb_regulators_enable(struct simplefb_par *par,
376 struct platform_device *pdev)
380 /* Enable all the regulators */
381 for (i = 0; i < par->regulator_count; i++) {
382 ret = regulator_enable(par->regulators[i]);
385 "failed to enable regulator %d: %d\n",
387 devm_regulator_put(par->regulators[i]);
388 par->regulators[i] = NULL;
391 par->regulators_enabled = true;
394 static void simplefb_regulators_destroy(struct simplefb_par *par)
398 if (!par->regulators || !par->regulators_enabled)
401 for (i = 0; i < par->regulator_count; i++)
402 if (par->regulators[i])
403 regulator_disable(par->regulators[i]);
406 static int simplefb_regulators_get(struct simplefb_par *par,
407 struct platform_device *pdev) { return 0; }
408 static void simplefb_regulators_enable(struct simplefb_par *par,
409 struct platform_device *pdev) { }
410 static void simplefb_regulators_destroy(struct simplefb_par *par) { }
413 static int simplefb_probe(struct platform_device *pdev)
416 struct simplefb_params params;
417 struct fb_info *info;
418 struct simplefb_par *par;
419 struct resource *res, *mem;
421 if (fb_get_options("simplefb", NULL))
425 if (dev_get_platdata(&pdev->dev))
426 ret = simplefb_parse_pd(pdev, ¶ms);
427 else if (pdev->dev.of_node)
428 ret = simplefb_parse_dt(pdev, ¶ms);
433 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
435 dev_err(&pdev->dev, "No memory resource\n");
439 mem = request_mem_region(res->start, resource_size(res), "simplefb");
442 * We cannot make this fatal. Sometimes this comes from magic
443 * spaces our resource handlers simply don't know about. Use
444 * the I/O-memory resource as-is and try to map that instead.
446 dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res);
450 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
453 goto error_release_mem_region;
455 platform_set_drvdata(pdev, info);
459 info->fix = simplefb_fix;
460 info->fix.smem_start = mem->start;
461 info->fix.smem_len = resource_size(mem);
462 info->fix.line_length = params.stride;
464 info->var = simplefb_var;
465 info->var.xres = params.width;
466 info->var.yres = params.height;
467 info->var.xres_virtual = params.width;
468 info->var.yres_virtual = params.height;
469 info->var.bits_per_pixel = params.format->bits_per_pixel;
470 info->var.red = params.format->red;
471 info->var.green = params.format->green;
472 info->var.blue = params.format->blue;
473 info->var.transp = params.format->transp;
475 info->apertures = alloc_apertures(1);
476 if (!info->apertures) {
478 goto error_fb_release;
480 info->apertures->ranges[0].base = info->fix.smem_start;
481 info->apertures->ranges[0].size = info->fix.smem_len;
483 info->fbops = &simplefb_ops;
484 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
485 info->screen_base = ioremap_wc(info->fix.smem_start,
487 if (!info->screen_base) {
489 goto error_fb_release;
491 info->pseudo_palette = par->palette;
493 ret = simplefb_clocks_get(par, pdev);
497 ret = simplefb_regulators_get(par, pdev);
501 simplefb_clocks_enable(par, pdev);
502 simplefb_regulators_enable(par, pdev);
504 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n",
505 info->fix.smem_start, info->fix.smem_len);
506 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
508 info->var.xres, info->var.yres,
509 info->var.bits_per_pixel, info->fix.line_length);
512 par->mem = mem; /* release in clean-up handler */
514 ret = register_framebuffer(info);
516 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
517 goto error_regulators;
520 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
525 simplefb_regulators_destroy(par);
527 simplefb_clocks_destroy(par);
529 iounmap(info->screen_base);
531 framebuffer_release(info);
532 error_release_mem_region:
534 release_mem_region(mem->start, resource_size(mem));
538 static int simplefb_remove(struct platform_device *pdev)
540 struct fb_info *info = platform_get_drvdata(pdev);
542 /* simplefb_destroy takes care of info cleanup */
543 unregister_framebuffer(info);
548 static const struct of_device_id simplefb_of_match[] = {
549 { .compatible = "simple-framebuffer", },
552 MODULE_DEVICE_TABLE(of, simplefb_of_match);
554 static struct platform_driver simplefb_driver = {
556 .name = "simple-framebuffer",
557 .of_match_table = simplefb_of_match,
559 .probe = simplefb_probe,
560 .remove = simplefb_remove,
563 module_platform_driver(simplefb_driver);
565 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
566 MODULE_DESCRIPTION("Simple framebuffer driver");
567 MODULE_LICENSE("GPL v2");