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-provider.h>
30 #include <linux/of_platform.h>
32 static struct fb_fix_screeninfo simplefb_fix
= {
34 .type
= FB_TYPE_PACKED_PIXELS
,
35 .visual
= FB_VISUAL_TRUECOLOR
,
36 .accel
= FB_ACCEL_NONE
,
39 static struct fb_var_screeninfo simplefb_var
= {
42 .activate
= FB_ACTIVATE_NOW
,
43 .vmode
= FB_VMODE_NONINTERLACED
,
46 #define PSEUDO_PALETTE_SIZE 16
48 static int simplefb_setcolreg(u_int regno
, u_int red
, u_int green
, u_int blue
,
49 u_int transp
, struct fb_info
*info
)
51 u32
*pal
= info
->pseudo_palette
;
52 u32 cr
= red
>> (16 - info
->var
.red
.length
);
53 u32 cg
= green
>> (16 - info
->var
.green
.length
);
54 u32 cb
= blue
>> (16 - info
->var
.blue
.length
);
57 if (regno
>= PSEUDO_PALETTE_SIZE
)
60 value
= (cr
<< info
->var
.red
.offset
) |
61 (cg
<< info
->var
.green
.offset
) |
62 (cb
<< info
->var
.blue
.offset
);
63 if (info
->var
.transp
.length
> 0) {
64 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
65 mask
<<= info
->var
.transp
.offset
;
73 static void simplefb_destroy(struct fb_info
*info
)
75 if (info
->screen_base
)
76 iounmap(info
->screen_base
);
79 static struct fb_ops simplefb_ops
= {
81 .fb_destroy
= simplefb_destroy
,
82 .fb_setcolreg
= simplefb_setcolreg
,
83 .fb_fillrect
= cfb_fillrect
,
84 .fb_copyarea
= cfb_copyarea
,
85 .fb_imageblit
= cfb_imageblit
,
88 static struct simplefb_format simplefb_formats
[] = SIMPLEFB_FORMATS
;
90 struct simplefb_params
{
94 struct simplefb_format
*format
;
97 static int simplefb_parse_dt(struct platform_device
*pdev
,
98 struct simplefb_params
*params
)
100 struct device_node
*np
= pdev
->dev
.of_node
;
105 ret
= of_property_read_u32(np
, "width", ¶ms
->width
);
107 dev_err(&pdev
->dev
, "Can't parse width property\n");
111 ret
= of_property_read_u32(np
, "height", ¶ms
->height
);
113 dev_err(&pdev
->dev
, "Can't parse height property\n");
117 ret
= of_property_read_u32(np
, "stride", ¶ms
->stride
);
119 dev_err(&pdev
->dev
, "Can't parse stride property\n");
123 ret
= of_property_read_string(np
, "format", &format
);
125 dev_err(&pdev
->dev
, "Can't parse format property\n");
128 params
->format
= NULL
;
129 for (i
= 0; i
< ARRAY_SIZE(simplefb_formats
); i
++) {
130 if (strcmp(format
, simplefb_formats
[i
].name
))
132 params
->format
= &simplefb_formats
[i
];
135 if (!params
->format
) {
136 dev_err(&pdev
->dev
, "Invalid format value\n");
143 static int simplefb_parse_pd(struct platform_device
*pdev
,
144 struct simplefb_params
*params
)
146 struct simplefb_platform_data
*pd
= dev_get_platdata(&pdev
->dev
);
149 params
->width
= pd
->width
;
150 params
->height
= pd
->height
;
151 params
->stride
= pd
->stride
;
153 params
->format
= NULL
;
154 for (i
= 0; i
< ARRAY_SIZE(simplefb_formats
); i
++) {
155 if (strcmp(pd
->format
, simplefb_formats
[i
].name
))
158 params
->format
= &simplefb_formats
[i
];
162 if (!params
->format
) {
163 dev_err(&pdev
->dev
, "Invalid format value\n");
170 struct simplefb_par
{
171 u32 palette
[PSEUDO_PALETTE_SIZE
];
172 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
178 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
180 * Clock handling code.
182 * Here we handle the clocks property of our "simple-framebuffer" dt node.
183 * This is necessary so that we can make sure that any clocks needed by
184 * the display engine that the bootloader set up for us (and for which it
185 * provided a simplefb dt node), stay up, for the life of the simplefb
188 * When the driver unloads, we cleanly disable, and then release the clocks.
190 * We only complain about errors here, no action is taken as the most likely
191 * error can only happen due to a mismatch between the bootloader which set
192 * up simplefb, and the clock definitions in the device tree. Chances are
193 * that there are no adverse effects, and if there are, a clean teardown of
194 * the fb probe will not help us much either. So just complain and carry on,
195 * and hope that the user actually gets a working fb at the end of things.
197 static int simplefb_clocks_init(struct simplefb_par
*par
,
198 struct platform_device
*pdev
)
200 struct device_node
*np
= pdev
->dev
.of_node
;
204 if (dev_get_platdata(&pdev
->dev
) || !np
)
207 par
->clk_count
= of_clk_get_parent_count(np
);
208 if (par
->clk_count
<= 0)
211 par
->clks
= kcalloc(par
->clk_count
, sizeof(struct clk
*), GFP_KERNEL
);
215 for (i
= 0; i
< par
->clk_count
; i
++) {
216 clock
= of_clk_get(np
, i
);
218 if (PTR_ERR(clock
) == -EPROBE_DEFER
) {
221 clk_put(par
->clks
[i
]);
224 return -EPROBE_DEFER
;
226 dev_err(&pdev
->dev
, "%s: clock %d not found: %ld\n",
227 __func__
, i
, PTR_ERR(clock
));
230 par
->clks
[i
] = clock
;
233 for (i
= 0; i
< par
->clk_count
; i
++) {
235 ret
= clk_prepare_enable(par
->clks
[i
]);
238 "%s: failed to enable clock %d: %d\n",
240 clk_put(par
->clks
[i
]);
249 static void simplefb_clocks_destroy(struct simplefb_par
*par
)
256 for (i
= 0; i
< par
->clk_count
; i
++) {
258 clk_disable_unprepare(par
->clks
[i
]);
259 clk_put(par
->clks
[i
]);
266 static int simplefb_clocks_init(struct simplefb_par
*par
,
267 struct platform_device
*pdev
) { return 0; }
268 static void simplefb_clocks_destroy(struct simplefb_par
*par
) { }
271 static int simplefb_probe(struct platform_device
*pdev
)
274 struct simplefb_params params
;
275 struct fb_info
*info
;
276 struct simplefb_par
*par
;
277 struct resource
*mem
;
279 if (fb_get_options("simplefb", NULL
))
283 if (dev_get_platdata(&pdev
->dev
))
284 ret
= simplefb_parse_pd(pdev
, ¶ms
);
285 else if (pdev
->dev
.of_node
)
286 ret
= simplefb_parse_dt(pdev
, ¶ms
);
291 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
293 dev_err(&pdev
->dev
, "No memory resource\n");
297 info
= framebuffer_alloc(sizeof(struct simplefb_par
), &pdev
->dev
);
300 platform_set_drvdata(pdev
, info
);
304 info
->fix
= simplefb_fix
;
305 info
->fix
.smem_start
= mem
->start
;
306 info
->fix
.smem_len
= resource_size(mem
);
307 info
->fix
.line_length
= params
.stride
;
309 info
->var
= simplefb_var
;
310 info
->var
.xres
= params
.width
;
311 info
->var
.yres
= params
.height
;
312 info
->var
.xres_virtual
= params
.width
;
313 info
->var
.yres_virtual
= params
.height
;
314 info
->var
.bits_per_pixel
= params
.format
->bits_per_pixel
;
315 info
->var
.red
= params
.format
->red
;
316 info
->var
.green
= params
.format
->green
;
317 info
->var
.blue
= params
.format
->blue
;
318 info
->var
.transp
= params
.format
->transp
;
320 info
->apertures
= alloc_apertures(1);
321 if (!info
->apertures
) {
323 goto error_fb_release
;
325 info
->apertures
->ranges
[0].base
= info
->fix
.smem_start
;
326 info
->apertures
->ranges
[0].size
= info
->fix
.smem_len
;
328 info
->fbops
= &simplefb_ops
;
329 info
->flags
= FBINFO_DEFAULT
| FBINFO_MISC_FIRMWARE
;
330 info
->screen_base
= ioremap_wc(info
->fix
.smem_start
,
332 if (!info
->screen_base
) {
334 goto error_fb_release
;
336 info
->pseudo_palette
= par
->palette
;
338 ret
= simplefb_clocks_init(par
, pdev
);
342 dev_info(&pdev
->dev
, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
343 info
->fix
.smem_start
, info
->fix
.smem_len
,
345 dev_info(&pdev
->dev
, "format=%s, mode=%dx%dx%d, linelength=%d\n",
347 info
->var
.xres
, info
->var
.yres
,
348 info
->var
.bits_per_pixel
, info
->fix
.line_length
);
350 ret
= register_framebuffer(info
);
352 dev_err(&pdev
->dev
, "Unable to register simplefb: %d\n", ret
);
356 dev_info(&pdev
->dev
, "fb%d: simplefb registered!\n", info
->node
);
361 simplefb_clocks_destroy(par
);
363 iounmap(info
->screen_base
);
365 framebuffer_release(info
);
369 static int simplefb_remove(struct platform_device
*pdev
)
371 struct fb_info
*info
= platform_get_drvdata(pdev
);
372 struct simplefb_par
*par
= info
->par
;
374 unregister_framebuffer(info
);
375 simplefb_clocks_destroy(par
);
376 framebuffer_release(info
);
381 static const struct of_device_id simplefb_of_match
[] = {
382 { .compatible
= "simple-framebuffer", },
385 MODULE_DEVICE_TABLE(of
, simplefb_of_match
);
387 static struct platform_driver simplefb_driver
= {
389 .name
= "simple-framebuffer",
390 .of_match_table
= simplefb_of_match
,
392 .probe
= simplefb_probe
,
393 .remove
= simplefb_remove
,
396 static int __init
simplefb_init(void)
399 struct device_node
*np
;
401 ret
= platform_driver_register(&simplefb_driver
);
405 if (IS_ENABLED(CONFIG_OF
) && of_chosen
) {
406 for_each_child_of_node(of_chosen
, np
) {
407 if (of_device_is_compatible(np
, "simple-framebuffer"))
408 of_platform_device_create(np
, NULL
, NULL
);
415 fs_initcall(simplefb_init
);
417 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
418 MODULE_DESCRIPTION("Simple framebuffer driver");
419 MODULE_LICENSE("GPL v2");