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/aperture.h>
16 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/platform_data/simplefb.h>
21 #include <linux/platform_device.h>
22 #include <linux/clk.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>
31 static const struct fb_fix_screeninfo simplefb_fix = {
33 .type = FB_TYPE_PACKED_PIXELS,
34 .visual = FB_VISUAL_TRUECOLOR,
35 .accel = FB_ACCEL_NONE,
38 static const struct fb_var_screeninfo simplefb_var = {
41 .activate = FB_ACTIVATE_NOW,
42 .vmode = FB_VMODE_NONINTERLACED,
45 #define PSEUDO_PALETTE_SIZE 16
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)
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);
56 if (regno >= PSEUDO_PALETTE_SIZE)
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;
73 u32 palette[PSEUDO_PALETTE_SIZE];
77 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
79 unsigned int clk_count;
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;
87 #if defined CONFIG_OF && defined CONFIG_REGULATOR
88 bool regulators_enabled;
90 struct regulator **regulators;
94 static void simplefb_clocks_destroy(struct simplefb_par *par);
95 static void simplefb_regulators_destroy(struct simplefb_par *par);
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.
101 static void simplefb_destroy(struct fb_info *info)
103 struct simplefb_par *par = info->par;
104 struct resource *mem = par->mem;
106 simplefb_regulators_destroy(info->par);
107 simplefb_clocks_destroy(info->par);
108 if (info->screen_base)
109 iounmap(info->screen_base);
111 framebuffer_release(info);
114 release_mem_region(mem->start, resource_size(mem));
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,
124 static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
126 struct simplefb_params {
130 struct simplefb_format *format;
131 struct resource memory;
134 static int simplefb_parse_dt(struct platform_device *pdev,
135 struct simplefb_params *params)
137 struct device_node *np = pdev->dev.of_node, *mem;
142 ret = of_property_read_u32(np, "width", ¶ms->width);
144 dev_err(&pdev->dev, "Can't parse width property\n");
148 ret = of_property_read_u32(np, "height", ¶ms->height);
150 dev_err(&pdev->dev, "Can't parse height property\n");
154 ret = of_property_read_u32(np, "stride", ¶ms->stride);
156 dev_err(&pdev->dev, "Can't parse stride property\n");
160 ret = of_property_read_string(np, "format", &format);
162 dev_err(&pdev->dev, "Can't parse format property\n");
165 params->format = NULL;
166 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
167 if (strcmp(format, simplefb_formats[i].name))
169 params->format = &simplefb_formats[i];
172 if (!params->format) {
173 dev_err(&pdev->dev, "Invalid format value\n");
177 mem = of_parse_phandle(np, "memory-region", 0);
179 ret = of_address_to_resource(mem, 0, ¶ms->memory);
181 dev_err(&pdev->dev, "failed to parse memory-region\n");
186 if (of_property_present(np, "reg"))
187 dev_warn(&pdev->dev, "preferring \"memory-region\" over \"reg\" property\n");
191 memset(¶ms->memory, 0, sizeof(params->memory));
197 static int simplefb_parse_pd(struct platform_device *pdev,
198 struct simplefb_params *params)
200 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
203 params->width = pd->width;
204 params->height = pd->height;
205 params->stride = pd->stride;
207 params->format = NULL;
208 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
209 if (strcmp(pd->format, simplefb_formats[i].name))
212 params->format = &simplefb_formats[i];
216 if (!params->format) {
217 dev_err(&pdev->dev, "Invalid format value\n");
221 memset(¶ms->memory, 0, sizeof(params->memory));
226 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
228 * Clock handling code.
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
236 * When the driver unloads, we cleanly disable, and then release the clocks.
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.
245 static int simplefb_clocks_get(struct simplefb_par *par,
246 struct platform_device *pdev)
248 struct device_node *np = pdev->dev.of_node;
252 if (dev_get_platdata(&pdev->dev) || !np)
255 par->clk_count = of_clk_get_parent_count(np);
259 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
263 for (i = 0; i < par->clk_count; i++) {
264 clock = of_clk_get(np, i);
266 if (PTR_ERR(clock) == -EPROBE_DEFER) {
268 clk_put(par->clks[i]);
271 return -EPROBE_DEFER;
273 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
274 __func__, i, PTR_ERR(clock));
277 par->clks[i] = clock;
283 static void simplefb_clocks_enable(struct simplefb_par *par,
284 struct platform_device *pdev)
288 for (i = 0; i < par->clk_count; i++) {
290 ret = clk_prepare_enable(par->clks[i]);
293 "%s: failed to enable clock %d: %d\n",
295 clk_put(par->clks[i]);
300 par->clks_enabled = true;
303 static void simplefb_clocks_destroy(struct simplefb_par *par)
310 for (i = 0; i < par->clk_count; i++) {
312 if (par->clks_enabled)
313 clk_disable_unprepare(par->clks[i]);
314 clk_put(par->clks[i]);
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) { }
328 #if defined CONFIG_OF && defined CONFIG_REGULATOR
330 #define SUPPLY_SUFFIX "-supply"
333 * Regulator handling code.
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.
341 * When the driver unloads, we cleanly disable, and then release the
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.
351 static int simplefb_regulators_get(struct simplefb_par *par,
352 struct platform_device *pdev)
354 struct device_node *np = pdev->dev.of_node;
355 struct property *prop;
356 struct regulator *regulator;
358 int count = 0, i = 0;
360 if (dev_get_platdata(&pdev->dev) || !np)
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)
373 par->regulators = devm_kcalloc(&pdev->dev, count,
374 sizeof(struct regulator *), GFP_KERNEL);
375 if (!par->regulators)
378 /* Get all the regulators */
379 for_each_property_of_node(np, prop) {
380 char name[32]; /* 32 is max size of property name */
382 p = strstr(prop->name, SUPPLY_SUFFIX);
383 if (!p || p == prop->name)
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));
396 par->regulators[i++] = regulator;
398 par->regulator_count = i;
403 static void simplefb_regulators_enable(struct simplefb_par *par,
404 struct platform_device *pdev)
408 /* Enable all the regulators */
409 for (i = 0; i < par->regulator_count; i++) {
410 ret = regulator_enable(par->regulators[i]);
413 "failed to enable regulator %d: %d\n",
415 devm_regulator_put(par->regulators[i]);
416 par->regulators[i] = NULL;
419 par->regulators_enabled = true;
422 static void simplefb_regulators_destroy(struct simplefb_par *par)
426 if (!par->regulators || !par->regulators_enabled)
429 for (i = 0; i < par->regulator_count; i++)
430 if (par->regulators[i])
431 regulator_disable(par->regulators[i]);
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) { }
441 #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
442 static void simplefb_detach_genpds(void *res)
444 struct simplefb_par *par = res;
445 unsigned int i = par->num_genpds;
447 if (par->num_genpds <= 1)
451 if (par->genpd_links[i])
452 device_link_del(par->genpd_links[i]);
454 if (!IS_ERR_OR_NULL(par->genpds[i]))
455 dev_pm_domain_detach(par->genpds[i], true);
459 static int simplefb_attach_genpds(struct simplefb_par *par,
460 struct platform_device *pdev)
462 struct device *dev = &pdev->dev;
466 err = of_count_phandle_with_args(dev->of_node, "power-domains",
467 "#power-domain-cells");
469 /* Nothing wrong if optional PDs are missing */
473 dev_err(dev, "failed to parse power-domains: %d\n", err);
477 par->num_genpds = err;
480 * Single power-domain devices are handled by the driver core, so
481 * nothing to do here.
483 if (par->num_genpds <= 1)
486 par->genpds = devm_kcalloc(dev, par->num_genpds, sizeof(*par->genpds),
491 par->genpd_links = devm_kcalloc(dev, par->num_genpds,
492 sizeof(*par->genpd_links),
494 if (!par->genpd_links)
497 for (i = 0; i < par->num_genpds; i++) {
498 par->genpds[i] = dev_pm_domain_attach_by_id(dev, i);
499 if (IS_ERR(par->genpds[i])) {
500 err = PTR_ERR(par->genpds[i]);
501 if (err == -EPROBE_DEFER) {
502 simplefb_detach_genpds(par);
506 dev_warn(dev, "failed to attach domain %u: %d\n", i, err);
510 par->genpd_links[i] = device_link_add(dev, par->genpds[i],
514 if (!par->genpd_links[i])
515 dev_warn(dev, "failed to link power-domain %u\n", i);
518 return devm_add_action_or_reset(dev, simplefb_detach_genpds, par);
521 static int simplefb_attach_genpds(struct simplefb_par *par,
522 struct platform_device *pdev)
528 static int simplefb_probe(struct platform_device *pdev)
531 struct simplefb_params params;
532 struct fb_info *info;
533 struct simplefb_par *par;
534 struct resource *res, *mem;
536 if (fb_get_options("simplefb", NULL))
540 if (dev_get_platdata(&pdev->dev))
541 ret = simplefb_parse_pd(pdev, ¶ms);
542 else if (pdev->dev.of_node)
543 ret = simplefb_parse_dt(pdev, ¶ms);
548 if (params.memory.start == 0 && params.memory.end == 0) {
549 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
551 dev_err(&pdev->dev, "No memory resource\n");
555 res = ¶ms.memory;
558 mem = request_mem_region(res->start, resource_size(res), "simplefb");
561 * We cannot make this fatal. Sometimes this comes from magic
562 * spaces our resource handlers simply don't know about. Use
563 * the I/O-memory resource as-is and try to map that instead.
565 dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res);
569 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
572 goto error_release_mem_region;
574 platform_set_drvdata(pdev, info);
578 info->fix = simplefb_fix;
579 info->fix.smem_start = mem->start;
580 info->fix.smem_len = resource_size(mem);
581 info->fix.line_length = params.stride;
583 info->var = simplefb_var;
584 info->var.xres = params.width;
585 info->var.yres = params.height;
586 info->var.xres_virtual = params.width;
587 info->var.yres_virtual = params.height;
588 info->var.bits_per_pixel = params.format->bits_per_pixel;
589 info->var.red = params.format->red;
590 info->var.green = params.format->green;
591 info->var.blue = params.format->blue;
592 info->var.transp = params.format->transp;
594 par->base = info->fix.smem_start;
595 par->size = info->fix.smem_len;
597 info->fbops = &simplefb_ops;
598 info->screen_base = ioremap_wc(info->fix.smem_start,
600 if (!info->screen_base) {
602 goto error_fb_release;
604 info->pseudo_palette = par->palette;
606 ret = simplefb_clocks_get(par, pdev);
610 ret = simplefb_regulators_get(par, pdev);
614 ret = simplefb_attach_genpds(par, pdev);
616 goto error_regulators;
618 simplefb_clocks_enable(par, pdev);
619 simplefb_regulators_enable(par, pdev);
621 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n",
622 info->fix.smem_start, info->fix.smem_len);
623 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
625 info->var.xres, info->var.yres,
626 info->var.bits_per_pixel, info->fix.line_length);
629 par->mem = mem; /* release in clean-up handler */
631 ret = devm_aperture_acquire_for_platform_device(pdev, par->base, par->size);
633 dev_err(&pdev->dev, "Unable to acquire aperture: %d\n", ret);
634 goto error_regulators;
636 ret = register_framebuffer(info);
638 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
639 goto error_regulators;
642 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
647 simplefb_regulators_destroy(par);
649 simplefb_clocks_destroy(par);
651 iounmap(info->screen_base);
653 framebuffer_release(info);
654 error_release_mem_region:
656 release_mem_region(mem->start, resource_size(mem));
660 static void simplefb_remove(struct platform_device *pdev)
662 struct fb_info *info = platform_get_drvdata(pdev);
664 /* simplefb_destroy takes care of info cleanup */
665 unregister_framebuffer(info);
668 static const struct of_device_id simplefb_of_match[] = {
669 { .compatible = "simple-framebuffer", },
672 MODULE_DEVICE_TABLE(of, simplefb_of_match);
674 static struct platform_driver simplefb_driver = {
676 .name = "simple-framebuffer",
677 .of_match_table = simplefb_of_match,
679 .probe = simplefb_probe,
680 .remove_new = simplefb_remove,
683 module_platform_driver(simplefb_driver);
685 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
686 MODULE_DESCRIPTION("Simple framebuffer driver");
687 MODULE_LICENSE("GPL v2");