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/regulator/consumer.h>
30 static const struct fb_fix_screeninfo simplefb_fix
= {
32 .type
= FB_TYPE_PACKED_PIXELS
,
33 .visual
= FB_VISUAL_TRUECOLOR
,
34 .accel
= FB_ACCEL_NONE
,
37 static const struct fb_var_screeninfo simplefb_var
= {
40 .activate
= FB_ACTIVATE_NOW
,
41 .vmode
= FB_VMODE_NONINTERLACED
,
44 #define PSEUDO_PALETTE_SIZE 16
46 static int simplefb_setcolreg(u_int regno
, u_int red
, u_int green
, u_int blue
,
47 u_int transp
, struct fb_info
*info
)
49 u32
*pal
= info
->pseudo_palette
;
50 u32 cr
= red
>> (16 - info
->var
.red
.length
);
51 u32 cg
= green
>> (16 - info
->var
.green
.length
);
52 u32 cb
= blue
>> (16 - info
->var
.blue
.length
);
55 if (regno
>= PSEUDO_PALETTE_SIZE
)
58 value
= (cr
<< info
->var
.red
.offset
) |
59 (cg
<< info
->var
.green
.offset
) |
60 (cb
<< info
->var
.blue
.offset
);
61 if (info
->var
.transp
.length
> 0) {
62 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
63 mask
<<= info
->var
.transp
.offset
;
72 u32 palette
[PSEUDO_PALETTE_SIZE
];
76 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
78 unsigned int clk_count
;
81 #if defined CONFIG_OF && defined CONFIG_REGULATOR
82 bool regulators_enabled
;
84 struct regulator
**regulators
;
88 static void simplefb_clocks_destroy(struct simplefb_par
*par
);
89 static void simplefb_regulators_destroy(struct simplefb_par
*par
);
92 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
93 * of unregister_framebuffer() or fb_release(). Do any cleanup here.
95 static void simplefb_destroy(struct fb_info
*info
)
97 struct simplefb_par
*par
= info
->par
;
98 struct resource
*mem
= par
->mem
;
100 simplefb_regulators_destroy(info
->par
);
101 simplefb_clocks_destroy(info
->par
);
102 if (info
->screen_base
)
103 iounmap(info
->screen_base
);
105 framebuffer_release(info
);
108 release_mem_region(mem
->start
, resource_size(mem
));
111 static const struct fb_ops simplefb_ops
= {
112 .owner
= THIS_MODULE
,
113 FB_DEFAULT_IOMEM_OPS
,
114 .fb_destroy
= simplefb_destroy
,
115 .fb_setcolreg
= simplefb_setcolreg
,
118 static struct simplefb_format simplefb_formats
[] = SIMPLEFB_FORMATS
;
120 struct simplefb_params
{
124 struct simplefb_format
*format
;
125 struct resource memory
;
128 static int simplefb_parse_dt(struct platform_device
*pdev
,
129 struct simplefb_params
*params
)
131 struct device_node
*np
= pdev
->dev
.of_node
, *mem
;
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");
171 mem
= of_parse_phandle(np
, "memory-region", 0);
173 ret
= of_address_to_resource(mem
, 0, ¶ms
->memory
);
175 dev_err(&pdev
->dev
, "failed to parse memory-region\n");
180 if (of_property_present(np
, "reg"))
181 dev_warn(&pdev
->dev
, "preferring \"memory-region\" over \"reg\" property\n");
185 memset(¶ms
->memory
, 0, sizeof(params
->memory
));
191 static int simplefb_parse_pd(struct platform_device
*pdev
,
192 struct simplefb_params
*params
)
194 struct simplefb_platform_data
*pd
= dev_get_platdata(&pdev
->dev
);
197 params
->width
= pd
->width
;
198 params
->height
= pd
->height
;
199 params
->stride
= pd
->stride
;
201 params
->format
= NULL
;
202 for (i
= 0; i
< ARRAY_SIZE(simplefb_formats
); i
++) {
203 if (strcmp(pd
->format
, simplefb_formats
[i
].name
))
206 params
->format
= &simplefb_formats
[i
];
210 if (!params
->format
) {
211 dev_err(&pdev
->dev
, "Invalid format value\n");
215 memset(¶ms
->memory
, 0, sizeof(params
->memory
));
220 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
222 * Clock handling code.
224 * Here we handle the clocks property of our "simple-framebuffer" dt node.
225 * This is necessary so that we can make sure that any clocks needed by
226 * the display engine that the bootloader set up for us (and for which it
227 * provided a simplefb dt node), stay up, for the life of the simplefb
230 * When the driver unloads, we cleanly disable, and then release the clocks.
232 * We only complain about errors here, no action is taken as the most likely
233 * error can only happen due to a mismatch between the bootloader which set
234 * up simplefb, and the clock definitions in the device tree. Chances are
235 * that there are no adverse effects, and if there are, a clean teardown of
236 * the fb probe will not help us much either. So just complain and carry on,
237 * and hope that the user actually gets a working fb at the end of things.
239 static int simplefb_clocks_get(struct simplefb_par
*par
,
240 struct platform_device
*pdev
)
242 struct device_node
*np
= pdev
->dev
.of_node
;
246 if (dev_get_platdata(&pdev
->dev
) || !np
)
249 par
->clk_count
= of_clk_get_parent_count(np
);
253 par
->clks
= kcalloc(par
->clk_count
, sizeof(struct clk
*), GFP_KERNEL
);
257 for (i
= 0; i
< par
->clk_count
; i
++) {
258 clock
= of_clk_get(np
, i
);
260 if (PTR_ERR(clock
) == -EPROBE_DEFER
) {
262 clk_put(par
->clks
[i
]);
265 return -EPROBE_DEFER
;
267 dev_err(&pdev
->dev
, "%s: clock %d not found: %ld\n",
268 __func__
, i
, PTR_ERR(clock
));
271 par
->clks
[i
] = clock
;
277 static void simplefb_clocks_enable(struct simplefb_par
*par
,
278 struct platform_device
*pdev
)
282 for (i
= 0; i
< par
->clk_count
; i
++) {
284 ret
= clk_prepare_enable(par
->clks
[i
]);
287 "%s: failed to enable clock %d: %d\n",
289 clk_put(par
->clks
[i
]);
294 par
->clks_enabled
= true;
297 static void simplefb_clocks_destroy(struct simplefb_par
*par
)
304 for (i
= 0; i
< par
->clk_count
; i
++) {
306 if (par
->clks_enabled
)
307 clk_disable_unprepare(par
->clks
[i
]);
308 clk_put(par
->clks
[i
]);
315 static int simplefb_clocks_get(struct simplefb_par
*par
,
316 struct platform_device
*pdev
) { return 0; }
317 static void simplefb_clocks_enable(struct simplefb_par
*par
,
318 struct platform_device
*pdev
) { }
319 static void simplefb_clocks_destroy(struct simplefb_par
*par
) { }
322 #if defined CONFIG_OF && defined CONFIG_REGULATOR
324 #define SUPPLY_SUFFIX "-supply"
327 * Regulator handling code.
329 * Here we handle the num-supplies and vin*-supply properties of our
330 * "simple-framebuffer" dt node. This is necessary so that we can make sure
331 * that any regulators needed by the display hardware that the bootloader
332 * set up for us (and for which it provided a simplefb dt node), stay up,
333 * for the life of the simplefb driver.
335 * When the driver unloads, we cleanly disable, and then release the
338 * We only complain about errors here, no action is taken as the most likely
339 * error can only happen due to a mismatch between the bootloader which set
340 * up simplefb, and the regulator definitions in the device tree. Chances are
341 * that there are no adverse effects, and if there are, a clean teardown of
342 * the fb probe will not help us much either. So just complain and carry on,
343 * and hope that the user actually gets a working fb at the end of things.
345 static int simplefb_regulators_get(struct simplefb_par
*par
,
346 struct platform_device
*pdev
)
348 struct device_node
*np
= pdev
->dev
.of_node
;
349 struct property
*prop
;
350 struct regulator
*regulator
;
352 int count
= 0, i
= 0;
354 if (dev_get_platdata(&pdev
->dev
) || !np
)
357 /* Count the number of regulator supplies */
358 for_each_property_of_node(np
, prop
) {
359 p
= strstr(prop
->name
, SUPPLY_SUFFIX
);
360 if (p
&& p
!= prop
->name
)
367 par
->regulators
= devm_kcalloc(&pdev
->dev
, count
,
368 sizeof(struct regulator
*), GFP_KERNEL
);
369 if (!par
->regulators
)
372 /* Get all the regulators */
373 for_each_property_of_node(np
, prop
) {
374 char name
[32]; /* 32 is max size of property name */
376 p
= strstr(prop
->name
, SUPPLY_SUFFIX
);
377 if (!p
|| p
== prop
->name
)
380 strscpy(name
, prop
->name
,
381 strlen(prop
->name
) - strlen(SUPPLY_SUFFIX
) + 1);
382 regulator
= devm_regulator_get_optional(&pdev
->dev
, name
);
383 if (IS_ERR(regulator
)) {
384 if (PTR_ERR(regulator
) == -EPROBE_DEFER
)
385 return -EPROBE_DEFER
;
386 dev_err(&pdev
->dev
, "regulator %s not found: %ld\n",
387 name
, PTR_ERR(regulator
));
390 par
->regulators
[i
++] = regulator
;
392 par
->regulator_count
= i
;
397 static void simplefb_regulators_enable(struct simplefb_par
*par
,
398 struct platform_device
*pdev
)
402 /* Enable all the regulators */
403 for (i
= 0; i
< par
->regulator_count
; i
++) {
404 ret
= regulator_enable(par
->regulators
[i
]);
407 "failed to enable regulator %d: %d\n",
409 devm_regulator_put(par
->regulators
[i
]);
410 par
->regulators
[i
] = NULL
;
413 par
->regulators_enabled
= true;
416 static void simplefb_regulators_destroy(struct simplefb_par
*par
)
420 if (!par
->regulators
|| !par
->regulators_enabled
)
423 for (i
= 0; i
< par
->regulator_count
; i
++)
424 if (par
->regulators
[i
])
425 regulator_disable(par
->regulators
[i
]);
428 static int simplefb_regulators_get(struct simplefb_par
*par
,
429 struct platform_device
*pdev
) { return 0; }
430 static void simplefb_regulators_enable(struct simplefb_par
*par
,
431 struct platform_device
*pdev
) { }
432 static void simplefb_regulators_destroy(struct simplefb_par
*par
) { }
435 static int simplefb_probe(struct platform_device
*pdev
)
438 struct simplefb_params params
;
439 struct fb_info
*info
;
440 struct simplefb_par
*par
;
441 struct resource
*res
, *mem
;
443 if (fb_get_options("simplefb", NULL
))
447 if (dev_get_platdata(&pdev
->dev
))
448 ret
= simplefb_parse_pd(pdev
, ¶ms
);
449 else if (pdev
->dev
.of_node
)
450 ret
= simplefb_parse_dt(pdev
, ¶ms
);
455 if (params
.memory
.start
== 0 && params
.memory
.end
== 0) {
456 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
458 dev_err(&pdev
->dev
, "No memory resource\n");
462 res
= ¶ms
.memory
;
465 mem
= request_mem_region(res
->start
, resource_size(res
), "simplefb");
468 * We cannot make this fatal. Sometimes this comes from magic
469 * spaces our resource handlers simply don't know about. Use
470 * the I/O-memory resource as-is and try to map that instead.
472 dev_warn(&pdev
->dev
, "simplefb: cannot reserve video memory at %pR\n", res
);
476 info
= framebuffer_alloc(sizeof(struct simplefb_par
), &pdev
->dev
);
479 goto error_release_mem_region
;
481 platform_set_drvdata(pdev
, info
);
485 info
->fix
= simplefb_fix
;
486 info
->fix
.smem_start
= mem
->start
;
487 info
->fix
.smem_len
= resource_size(mem
);
488 info
->fix
.line_length
= params
.stride
;
490 info
->var
= simplefb_var
;
491 info
->var
.xres
= params
.width
;
492 info
->var
.yres
= params
.height
;
493 info
->var
.xres_virtual
= params
.width
;
494 info
->var
.yres_virtual
= params
.height
;
495 info
->var
.bits_per_pixel
= params
.format
->bits_per_pixel
;
496 info
->var
.red
= params
.format
->red
;
497 info
->var
.green
= params
.format
->green
;
498 info
->var
.blue
= params
.format
->blue
;
499 info
->var
.transp
= params
.format
->transp
;
501 par
->base
= info
->fix
.smem_start
;
502 par
->size
= info
->fix
.smem_len
;
504 info
->fbops
= &simplefb_ops
;
505 info
->screen_base
= ioremap_wc(info
->fix
.smem_start
,
507 if (!info
->screen_base
) {
509 goto error_fb_release
;
511 info
->pseudo_palette
= par
->palette
;
513 ret
= simplefb_clocks_get(par
, pdev
);
517 ret
= simplefb_regulators_get(par
, pdev
);
521 simplefb_clocks_enable(par
, pdev
);
522 simplefb_regulators_enable(par
, pdev
);
524 dev_info(&pdev
->dev
, "framebuffer at 0x%lx, 0x%x bytes\n",
525 info
->fix
.smem_start
, info
->fix
.smem_len
);
526 dev_info(&pdev
->dev
, "format=%s, mode=%dx%dx%d, linelength=%d\n",
528 info
->var
.xres
, info
->var
.yres
,
529 info
->var
.bits_per_pixel
, info
->fix
.line_length
);
532 par
->mem
= mem
; /* release in clean-up handler */
534 ret
= devm_aperture_acquire_for_platform_device(pdev
, par
->base
, par
->size
);
536 dev_err(&pdev
->dev
, "Unable to acquire aperture: %d\n", ret
);
537 goto error_regulators
;
539 ret
= register_framebuffer(info
);
541 dev_err(&pdev
->dev
, "Unable to register simplefb: %d\n", ret
);
542 goto error_regulators
;
545 dev_info(&pdev
->dev
, "fb%d: simplefb registered!\n", info
->node
);
550 simplefb_regulators_destroy(par
);
552 simplefb_clocks_destroy(par
);
554 iounmap(info
->screen_base
);
556 framebuffer_release(info
);
557 error_release_mem_region
:
559 release_mem_region(mem
->start
, resource_size(mem
));
563 static void simplefb_remove(struct platform_device
*pdev
)
565 struct fb_info
*info
= platform_get_drvdata(pdev
);
567 /* simplefb_destroy takes care of info cleanup */
568 unregister_framebuffer(info
);
571 static const struct of_device_id simplefb_of_match
[] = {
572 { .compatible
= "simple-framebuffer", },
575 MODULE_DEVICE_TABLE(of
, simplefb_of_match
);
577 static struct platform_driver simplefb_driver
= {
579 .name
= "simple-framebuffer",
580 .of_match_table
= simplefb_of_match
,
582 .probe
= simplefb_probe
,
583 .remove_new
= simplefb_remove
,
586 module_platform_driver(simplefb_driver
);
588 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
589 MODULE_DESCRIPTION("Simple framebuffer driver");
590 MODULE_LICENSE("GPL v2");