2 * Simplest possible simple frame-buffer driver, as a platform device
4 * Copyright (c) 2013, Stephen Warren
6 * Based on q40fb.c, which was:
7 * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org>
9 * Also based on offb.c, which was:
10 * Copyright (C) 1997 Geert Uytterhoeven
11 * Copyright (C) 1996 Paul Mackerras
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.
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
23 #include <linux/errno.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>
32 #include <linux/of_platform.h>
33 #include <linux/parser.h>
34 #include <linux/regulator/consumer.h>
36 static const struct fb_fix_screeninfo simplefb_fix
= {
38 .type
= FB_TYPE_PACKED_PIXELS
,
39 .visual
= FB_VISUAL_TRUECOLOR
,
40 .accel
= FB_ACCEL_NONE
,
43 static const struct fb_var_screeninfo simplefb_var
= {
46 .activate
= FB_ACTIVATE_NOW
,
47 .vmode
= FB_VMODE_NONINTERLACED
,
50 #define PSEUDO_PALETTE_SIZE 16
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
)
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
);
61 if (regno
>= PSEUDO_PALETTE_SIZE
)
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
;
78 static void simplefb_clocks_destroy(struct simplefb_par
*par
);
79 static void simplefb_regulators_destroy(struct simplefb_par
*par
);
81 static void simplefb_destroy(struct fb_info
*info
)
83 simplefb_regulators_destroy(info
->par
);
84 simplefb_clocks_destroy(info
->par
);
85 if (info
->screen_base
)
86 iounmap(info
->screen_base
);
89 static struct fb_ops simplefb_ops
= {
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
,
98 static struct simplefb_format simplefb_formats
[] = SIMPLEFB_FORMATS
;
100 struct simplefb_params
{
104 struct simplefb_format
*format
;
107 static int simplefb_parse_dt(struct platform_device
*pdev
,
108 struct simplefb_params
*params
)
110 struct device_node
*np
= pdev
->dev
.of_node
;
115 ret
= of_property_read_u32(np
, "width", ¶ms
->width
);
117 dev_err(&pdev
->dev
, "Can't parse width property\n");
121 ret
= of_property_read_u32(np
, "height", ¶ms
->height
);
123 dev_err(&pdev
->dev
, "Can't parse height property\n");
127 ret
= of_property_read_u32(np
, "stride", ¶ms
->stride
);
129 dev_err(&pdev
->dev
, "Can't parse stride property\n");
133 ret
= of_property_read_string(np
, "format", &format
);
135 dev_err(&pdev
->dev
, "Can't parse format property\n");
138 params
->format
= NULL
;
139 for (i
= 0; i
< ARRAY_SIZE(simplefb_formats
); i
++) {
140 if (strcmp(format
, simplefb_formats
[i
].name
))
142 params
->format
= &simplefb_formats
[i
];
145 if (!params
->format
) {
146 dev_err(&pdev
->dev
, "Invalid format value\n");
153 static int simplefb_parse_pd(struct platform_device
*pdev
,
154 struct simplefb_params
*params
)
156 struct simplefb_platform_data
*pd
= dev_get_platdata(&pdev
->dev
);
159 params
->width
= pd
->width
;
160 params
->height
= pd
->height
;
161 params
->stride
= pd
->stride
;
163 params
->format
= NULL
;
164 for (i
= 0; i
< ARRAY_SIZE(simplefb_formats
); i
++) {
165 if (strcmp(pd
->format
, simplefb_formats
[i
].name
))
168 params
->format
= &simplefb_formats
[i
];
172 if (!params
->format
) {
173 dev_err(&pdev
->dev
, "Invalid format value\n");
180 struct simplefb_par
{
181 u32 palette
[PSEUDO_PALETTE_SIZE
];
182 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
183 unsigned int clk_count
;
186 #if defined CONFIG_OF && defined CONFIG_REGULATOR
188 struct regulator
**regulators
;
192 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
194 * Clock handling code.
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
202 * When the driver unloads, we cleanly disable, and then release the clocks.
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.
211 static int simplefb_clocks_init(struct simplefb_par
*par
,
212 struct platform_device
*pdev
)
214 struct device_node
*np
= pdev
->dev
.of_node
;
218 if (dev_get_platdata(&pdev
->dev
) || !np
)
221 par
->clk_count
= of_clk_get_parent_count(np
);
225 par
->clks
= kcalloc(par
->clk_count
, sizeof(struct clk
*), GFP_KERNEL
);
229 for (i
= 0; i
< par
->clk_count
; i
++) {
230 clock
= of_clk_get(np
, i
);
232 if (PTR_ERR(clock
) == -EPROBE_DEFER
) {
235 clk_put(par
->clks
[i
]);
238 return -EPROBE_DEFER
;
240 dev_err(&pdev
->dev
, "%s: clock %d not found: %ld\n",
241 __func__
, i
, PTR_ERR(clock
));
244 par
->clks
[i
] = clock
;
247 for (i
= 0; i
< par
->clk_count
; i
++) {
249 ret
= clk_prepare_enable(par
->clks
[i
]);
252 "%s: failed to enable clock %d: %d\n",
254 clk_put(par
->clks
[i
]);
263 static void simplefb_clocks_destroy(struct simplefb_par
*par
)
270 for (i
= 0; i
< par
->clk_count
; i
++) {
272 clk_disable_unprepare(par
->clks
[i
]);
273 clk_put(par
->clks
[i
]);
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
) { }
285 #if defined CONFIG_OF && defined CONFIG_REGULATOR
287 #define SUPPLY_SUFFIX "-supply"
290 * Regulator handling code.
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.
298 * When the driver unloads, we cleanly disable, and then release the
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.
308 static int simplefb_regulators_init(struct simplefb_par
*par
,
309 struct platform_device
*pdev
)
311 struct device_node
*np
= pdev
->dev
.of_node
;
312 struct property
*prop
;
313 struct regulator
*regulator
;
315 int count
= 0, i
= 0, ret
;
317 if (dev_get_platdata(&pdev
->dev
) || !np
)
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
)
330 par
->regulators
= devm_kcalloc(&pdev
->dev
, count
,
331 sizeof(struct regulator
*), GFP_KERNEL
);
332 if (!par
->regulators
)
335 /* Get all the regulators */
336 for_each_property_of_node(np
, prop
) {
337 char name
[32]; /* 32 is max size of property name */
339 p
= strstr(prop
->name
, SUPPLY_SUFFIX
);
340 if (!p
|| p
== prop
->name
)
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
));
353 par
->regulators
[i
++] = regulator
;
355 par
->regulator_count
= i
;
357 /* Enable all the regulators */
358 for (i
= 0; i
< par
->regulator_count
; i
++) {
359 ret
= regulator_enable(par
->regulators
[i
]);
362 "failed to enable regulator %d: %d\n",
364 devm_regulator_put(par
->regulators
[i
]);
365 par
->regulators
[i
] = NULL
;
372 static void simplefb_regulators_destroy(struct simplefb_par
*par
)
376 if (!par
->regulators
)
379 for (i
= 0; i
< par
->regulator_count
; i
++)
380 if (par
->regulators
[i
])
381 regulator_disable(par
->regulators
[i
]);
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
) { }
389 static int simplefb_probe(struct platform_device
*pdev
)
392 struct simplefb_params params
;
393 struct fb_info
*info
;
394 struct simplefb_par
*par
;
395 struct resource
*mem
;
397 if (fb_get_options("simplefb", NULL
))
401 if (dev_get_platdata(&pdev
->dev
))
402 ret
= simplefb_parse_pd(pdev
, ¶ms
);
403 else if (pdev
->dev
.of_node
)
404 ret
= simplefb_parse_dt(pdev
, ¶ms
);
409 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
411 dev_err(&pdev
->dev
, "No memory resource\n");
415 info
= framebuffer_alloc(sizeof(struct simplefb_par
), &pdev
->dev
);
418 platform_set_drvdata(pdev
, info
);
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
;
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
;
438 info
->apertures
= alloc_apertures(1);
439 if (!info
->apertures
) {
441 goto error_fb_release
;
443 info
->apertures
->ranges
[0].base
= info
->fix
.smem_start
;
444 info
->apertures
->ranges
[0].size
= info
->fix
.smem_len
;
446 info
->fbops
= &simplefb_ops
;
447 info
->flags
= FBINFO_DEFAULT
| FBINFO_MISC_FIRMWARE
;
448 info
->screen_base
= ioremap_wc(info
->fix
.smem_start
,
450 if (!info
->screen_base
) {
452 goto error_fb_release
;
454 info
->pseudo_palette
= par
->palette
;
456 ret
= simplefb_clocks_init(par
, pdev
);
460 ret
= simplefb_regulators_init(par
, pdev
);
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
,
467 dev_info(&pdev
->dev
, "format=%s, mode=%dx%dx%d, linelength=%d\n",
469 info
->var
.xres
, info
->var
.yres
,
470 info
->var
.bits_per_pixel
, info
->fix
.line_length
);
472 ret
= register_framebuffer(info
);
474 dev_err(&pdev
->dev
, "Unable to register simplefb: %d\n", ret
);
475 goto error_regulators
;
478 dev_info(&pdev
->dev
, "fb%d: simplefb registered!\n", info
->node
);
483 simplefb_regulators_destroy(par
);
485 simplefb_clocks_destroy(par
);
487 iounmap(info
->screen_base
);
489 framebuffer_release(info
);
493 static int simplefb_remove(struct platform_device
*pdev
)
495 struct fb_info
*info
= platform_get_drvdata(pdev
);
497 unregister_framebuffer(info
);
498 framebuffer_release(info
);
503 static const struct of_device_id simplefb_of_match
[] = {
504 { .compatible
= "simple-framebuffer", },
507 MODULE_DEVICE_TABLE(of
, simplefb_of_match
);
509 static struct platform_driver simplefb_driver
= {
511 .name
= "simple-framebuffer",
512 .of_match_table
= simplefb_of_match
,
514 .probe
= simplefb_probe
,
515 .remove
= simplefb_remove
,
518 static int __init
simplefb_init(void)
521 struct device_node
*np
;
523 ret
= platform_driver_register(&simplefb_driver
);
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
);
537 fs_initcall(simplefb_init
);
539 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
540 MODULE_DESCRIPTION("Simple framebuffer driver");
541 MODULE_LICENSE("GPL v2");