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:
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_clk.h>
25 #include <linux/of_platform.h>
26 #include <linux/parser.h>
27 #include <linux/regulator/consumer.h>
29 static const struct fb_fix_screeninfo simplefb_fix = {
31 .type = FB_TYPE_PACKED_PIXELS,
32 .visual = FB_VISUAL_TRUECOLOR,
33 .accel = FB_ACCEL_NONE,
36 static const struct fb_var_screeninfo simplefb_var = {
39 .activate = FB_ACTIVATE_NOW,
40 .vmode = FB_VMODE_NONINTERLACED,
43 #define PSEUDO_PALETTE_SIZE 16
45 static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
46 u_int transp, struct fb_info *info)
48 u32 *pal = info->pseudo_palette;
49 u32 cr = red >> (16 - info->var.red.length);
50 u32 cg = green >> (16 - info->var.green.length);
51 u32 cb = blue >> (16 - info->var.blue.length);
54 if (regno >= PSEUDO_PALETTE_SIZE)
57 value = (cr << info->var.red.offset) |
58 (cg << info->var.green.offset) |
59 (cb << info->var.blue.offset);
60 if (info->var.transp.length > 0) {
61 u32 mask = (1 << info->var.transp.length) - 1;
62 mask <<= info->var.transp.offset;
71 u32 palette[PSEUDO_PALETTE_SIZE];
75 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
77 unsigned int clk_count;
80 #if defined CONFIG_OF && defined CONFIG_REGULATOR
81 bool regulators_enabled;
83 struct regulator **regulators;
87 static void simplefb_clocks_destroy(struct simplefb_par *par);
88 static void simplefb_regulators_destroy(struct simplefb_par *par);
91 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
92 * of unregister_framebuffer() or fb_release(). Do any cleanup here.
94 static void simplefb_destroy(struct fb_info *info)
96 struct simplefb_par *par = info->par;
97 struct resource *mem = par->mem;
99 simplefb_regulators_destroy(info->par);
100 simplefb_clocks_destroy(info->par);
101 if (info->screen_base)
102 iounmap(info->screen_base);
104 framebuffer_release(info);
107 release_mem_region(mem->start, resource_size(mem));
110 static const struct fb_ops simplefb_ops = {
111 .owner = THIS_MODULE,
112 .fb_destroy = simplefb_destroy,
113 .fb_setcolreg = simplefb_setcolreg,
114 .fb_fillrect = cfb_fillrect,
115 .fb_copyarea = cfb_copyarea,
116 .fb_imageblit = cfb_imageblit,
119 static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
121 struct simplefb_params {
125 struct simplefb_format *format;
128 static int simplefb_parse_dt(struct platform_device *pdev,
129 struct simplefb_params *params)
131 struct device_node *np = pdev->dev.of_node;
136 ret = of_property_read_u32(np, "width", ¶ms->width);
138 dev_err(&pdev->dev, "Can't parse width property\n");
142 ret = of_property_read_u32(np, "height", ¶ms->height);
144 dev_err(&pdev->dev, "Can't parse height property\n");
148 ret = of_property_read_u32(np, "stride", ¶ms->stride);
150 dev_err(&pdev->dev, "Can't parse stride property\n");
154 ret = of_property_read_string(np, "format", &format);
156 dev_err(&pdev->dev, "Can't parse format property\n");
159 params->format = NULL;
160 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
161 if (strcmp(format, simplefb_formats[i].name))
163 params->format = &simplefb_formats[i];
166 if (!params->format) {
167 dev_err(&pdev->dev, "Invalid format value\n");
174 static int simplefb_parse_pd(struct platform_device *pdev,
175 struct simplefb_params *params)
177 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
180 params->width = pd->width;
181 params->height = pd->height;
182 params->stride = pd->stride;
184 params->format = NULL;
185 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
186 if (strcmp(pd->format, simplefb_formats[i].name))
189 params->format = &simplefb_formats[i];
193 if (!params->format) {
194 dev_err(&pdev->dev, "Invalid format value\n");
201 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
203 * Clock handling code.
205 * Here we handle the clocks property of our "simple-framebuffer" dt node.
206 * This is necessary so that we can make sure that any clocks needed by
207 * the display engine that the bootloader set up for us (and for which it
208 * provided a simplefb dt node), stay up, for the life of the simplefb
211 * When the driver unloads, we cleanly disable, and then release the clocks.
213 * We only complain about errors here, no action is taken as the most likely
214 * error can only happen due to a mismatch between the bootloader which set
215 * up simplefb, and the clock definitions in the device tree. Chances are
216 * that there are no adverse effects, and if there are, a clean teardown of
217 * the fb probe will not help us much either. So just complain and carry on,
218 * and hope that the user actually gets a working fb at the end of things.
220 static int simplefb_clocks_get(struct simplefb_par *par,
221 struct platform_device *pdev)
223 struct device_node *np = pdev->dev.of_node;
227 if (dev_get_platdata(&pdev->dev) || !np)
230 par->clk_count = of_clk_get_parent_count(np);
234 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
238 for (i = 0; i < par->clk_count; i++) {
239 clock = of_clk_get(np, i);
241 if (PTR_ERR(clock) == -EPROBE_DEFER) {
243 clk_put(par->clks[i]);
246 return -EPROBE_DEFER;
248 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
249 __func__, i, PTR_ERR(clock));
252 par->clks[i] = clock;
258 static void simplefb_clocks_enable(struct simplefb_par *par,
259 struct platform_device *pdev)
263 for (i = 0; i < par->clk_count; i++) {
265 ret = clk_prepare_enable(par->clks[i]);
268 "%s: failed to enable clock %d: %d\n",
270 clk_put(par->clks[i]);
275 par->clks_enabled = true;
278 static void simplefb_clocks_destroy(struct simplefb_par *par)
285 for (i = 0; i < par->clk_count; i++) {
287 if (par->clks_enabled)
288 clk_disable_unprepare(par->clks[i]);
289 clk_put(par->clks[i]);
296 static int simplefb_clocks_get(struct simplefb_par *par,
297 struct platform_device *pdev) { return 0; }
298 static void simplefb_clocks_enable(struct simplefb_par *par,
299 struct platform_device *pdev) { }
300 static void simplefb_clocks_destroy(struct simplefb_par *par) { }
303 #if defined CONFIG_OF && defined CONFIG_REGULATOR
305 #define SUPPLY_SUFFIX "-supply"
308 * Regulator handling code.
310 * Here we handle the num-supplies and vin*-supply properties of our
311 * "simple-framebuffer" dt node. This is necessary so that we can make sure
312 * that any regulators needed by the display hardware that the bootloader
313 * set up for us (and for which it provided a simplefb dt node), stay up,
314 * for the life of the simplefb driver.
316 * When the driver unloads, we cleanly disable, and then release the
319 * We only complain about errors here, no action is taken as the most likely
320 * error can only happen due to a mismatch between the bootloader which set
321 * up simplefb, and the regulator definitions in the device tree. Chances are
322 * that there are no adverse effects, and if there are, a clean teardown of
323 * the fb probe will not help us much either. So just complain and carry on,
324 * and hope that the user actually gets a working fb at the end of things.
326 static int simplefb_regulators_get(struct simplefb_par *par,
327 struct platform_device *pdev)
329 struct device_node *np = pdev->dev.of_node;
330 struct property *prop;
331 struct regulator *regulator;
333 int count = 0, i = 0;
335 if (dev_get_platdata(&pdev->dev) || !np)
338 /* Count the number of regulator supplies */
339 for_each_property_of_node(np, prop) {
340 p = strstr(prop->name, SUPPLY_SUFFIX);
341 if (p && p != prop->name)
348 par->regulators = devm_kcalloc(&pdev->dev, count,
349 sizeof(struct regulator *), GFP_KERNEL);
350 if (!par->regulators)
353 /* Get all the regulators */
354 for_each_property_of_node(np, prop) {
355 char name[32]; /* 32 is max size of property name */
357 p = strstr(prop->name, SUPPLY_SUFFIX);
358 if (!p || p == prop->name)
361 strscpy(name, prop->name,
362 strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
363 regulator = devm_regulator_get_optional(&pdev->dev, name);
364 if (IS_ERR(regulator)) {
365 if (PTR_ERR(regulator) == -EPROBE_DEFER)
366 return -EPROBE_DEFER;
367 dev_err(&pdev->dev, "regulator %s not found: %ld\n",
368 name, PTR_ERR(regulator));
371 par->regulators[i++] = regulator;
373 par->regulator_count = i;
378 static void simplefb_regulators_enable(struct simplefb_par *par,
379 struct platform_device *pdev)
383 /* Enable all the regulators */
384 for (i = 0; i < par->regulator_count; i++) {
385 ret = regulator_enable(par->regulators[i]);
388 "failed to enable regulator %d: %d\n",
390 devm_regulator_put(par->regulators[i]);
391 par->regulators[i] = NULL;
394 par->regulators_enabled = true;
397 static void simplefb_regulators_destroy(struct simplefb_par *par)
401 if (!par->regulators || !par->regulators_enabled)
404 for (i = 0; i < par->regulator_count; i++)
405 if (par->regulators[i])
406 regulator_disable(par->regulators[i]);
409 static int simplefb_regulators_get(struct simplefb_par *par,
410 struct platform_device *pdev) { return 0; }
411 static void simplefb_regulators_enable(struct simplefb_par *par,
412 struct platform_device *pdev) { }
413 static void simplefb_regulators_destroy(struct simplefb_par *par) { }
416 static int simplefb_probe(struct platform_device *pdev)
419 struct simplefb_params params;
420 struct fb_info *info;
421 struct simplefb_par *par;
422 struct resource *res, *mem;
424 if (fb_get_options("simplefb", NULL))
428 if (dev_get_platdata(&pdev->dev))
429 ret = simplefb_parse_pd(pdev, ¶ms);
430 else if (pdev->dev.of_node)
431 ret = simplefb_parse_dt(pdev, ¶ms);
436 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
438 dev_err(&pdev->dev, "No memory resource\n");
442 mem = request_mem_region(res->start, resource_size(res), "simplefb");
445 * We cannot make this fatal. Sometimes this comes from magic
446 * spaces our resource handlers simply don't know about. Use
447 * the I/O-memory resource as-is and try to map that instead.
449 dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res);
453 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
456 goto error_release_mem_region;
458 platform_set_drvdata(pdev, info);
462 info->fix = simplefb_fix;
463 info->fix.smem_start = mem->start;
464 info->fix.smem_len = resource_size(mem);
465 info->fix.line_length = params.stride;
467 info->var = simplefb_var;
468 info->var.xres = params.width;
469 info->var.yres = params.height;
470 info->var.xres_virtual = params.width;
471 info->var.yres_virtual = params.height;
472 info->var.bits_per_pixel = params.format->bits_per_pixel;
473 info->var.red = params.format->red;
474 info->var.green = params.format->green;
475 info->var.blue = params.format->blue;
476 info->var.transp = params.format->transp;
478 par->base = info->fix.smem_start;
479 par->size = info->fix.smem_len;
481 info->fbops = &simplefb_ops;
482 info->flags = FBINFO_DEFAULT;
483 info->screen_base = ioremap_wc(info->fix.smem_start,
485 if (!info->screen_base) {
487 goto error_fb_release;
489 info->pseudo_palette = par->palette;
491 ret = simplefb_clocks_get(par, pdev);
495 ret = simplefb_regulators_get(par, pdev);
499 simplefb_clocks_enable(par, pdev);
500 simplefb_regulators_enable(par, pdev);
502 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n",
503 info->fix.smem_start, info->fix.smem_len);
504 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
506 info->var.xres, info->var.yres,
507 info->var.bits_per_pixel, info->fix.line_length);
510 par->mem = mem; /* release in clean-up handler */
512 ret = devm_aperture_acquire_for_platform_device(pdev, par->base, par->size);
514 dev_err(&pdev->dev, "Unable to acquire aperture: %d\n", ret);
515 goto error_regulators;
517 ret = register_framebuffer(info);
519 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
520 goto error_regulators;
523 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
528 simplefb_regulators_destroy(par);
530 simplefb_clocks_destroy(par);
532 iounmap(info->screen_base);
534 framebuffer_release(info);
535 error_release_mem_region:
537 release_mem_region(mem->start, resource_size(mem));
541 static void simplefb_remove(struct platform_device *pdev)
543 struct fb_info *info = platform_get_drvdata(pdev);
545 /* simplefb_destroy takes care of info cleanup */
546 unregister_framebuffer(info);
549 static const struct of_device_id simplefb_of_match[] = {
550 { .compatible = "simple-framebuffer", },
553 MODULE_DEVICE_TABLE(of, simplefb_of_match);
555 static struct platform_driver simplefb_driver = {
557 .name = "simple-framebuffer",
558 .of_match_table = simplefb_of_match,
560 .probe = simplefb_probe,
561 .remove_new = simplefb_remove,
564 module_platform_driver(simplefb_driver);
567 MODULE_DESCRIPTION("Simple framebuffer driver");
568 MODULE_LICENSE("GPL v2");