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>
31 #include <linux/of_platform.h>
33 static struct fb_fix_screeninfo simplefb_fix
= {
35 .type
= FB_TYPE_PACKED_PIXELS
,
36 .visual
= FB_VISUAL_TRUECOLOR
,
37 .accel
= FB_ACCEL_NONE
,
40 static struct fb_var_screeninfo simplefb_var
= {
43 .activate
= FB_ACTIVATE_NOW
,
44 .vmode
= FB_VMODE_NONINTERLACED
,
47 #define PSEUDO_PALETTE_SIZE 16
49 static int simplefb_setcolreg(u_int regno
, u_int red
, u_int green
, u_int blue
,
50 u_int transp
, struct fb_info
*info
)
52 u32
*pal
= info
->pseudo_palette
;
53 u32 cr
= red
>> (16 - info
->var
.red
.length
);
54 u32 cg
= green
>> (16 - info
->var
.green
.length
);
55 u32 cb
= blue
>> (16 - info
->var
.blue
.length
);
58 if (regno
>= PSEUDO_PALETTE_SIZE
)
61 value
= (cr
<< info
->var
.red
.offset
) |
62 (cg
<< info
->var
.green
.offset
) |
63 (cb
<< info
->var
.blue
.offset
);
64 if (info
->var
.transp
.length
> 0) {
65 u32 mask
= (1 << info
->var
.transp
.length
) - 1;
66 mask
<<= info
->var
.transp
.offset
;
74 static void simplefb_destroy(struct fb_info
*info
)
76 if (info
->screen_base
)
77 iounmap(info
->screen_base
);
80 static struct fb_ops simplefb_ops
= {
82 .fb_destroy
= simplefb_destroy
,
83 .fb_setcolreg
= simplefb_setcolreg
,
84 .fb_fillrect
= cfb_fillrect
,
85 .fb_copyarea
= cfb_copyarea
,
86 .fb_imageblit
= cfb_imageblit
,
89 static struct simplefb_format simplefb_formats
[] = SIMPLEFB_FORMATS
;
91 struct simplefb_params
{
95 struct simplefb_format
*format
;
98 static int simplefb_parse_dt(struct platform_device
*pdev
,
99 struct simplefb_params
*params
)
101 struct device_node
*np
= pdev
->dev
.of_node
;
106 ret
= of_property_read_u32(np
, "width", ¶ms
->width
);
108 dev_err(&pdev
->dev
, "Can't parse width property\n");
112 ret
= of_property_read_u32(np
, "height", ¶ms
->height
);
114 dev_err(&pdev
->dev
, "Can't parse height property\n");
118 ret
= of_property_read_u32(np
, "stride", ¶ms
->stride
);
120 dev_err(&pdev
->dev
, "Can't parse stride property\n");
124 ret
= of_property_read_string(np
, "format", &format
);
126 dev_err(&pdev
->dev
, "Can't parse format property\n");
129 params
->format
= NULL
;
130 for (i
= 0; i
< ARRAY_SIZE(simplefb_formats
); i
++) {
131 if (strcmp(format
, simplefb_formats
[i
].name
))
133 params
->format
= &simplefb_formats
[i
];
136 if (!params
->format
) {
137 dev_err(&pdev
->dev
, "Invalid format value\n");
144 static int simplefb_parse_pd(struct platform_device
*pdev
,
145 struct simplefb_params
*params
)
147 struct simplefb_platform_data
*pd
= dev_get_platdata(&pdev
->dev
);
150 params
->width
= pd
->width
;
151 params
->height
= pd
->height
;
152 params
->stride
= pd
->stride
;
154 params
->format
= NULL
;
155 for (i
= 0; i
< ARRAY_SIZE(simplefb_formats
); i
++) {
156 if (strcmp(pd
->format
, simplefb_formats
[i
].name
))
159 params
->format
= &simplefb_formats
[i
];
163 if (!params
->format
) {
164 dev_err(&pdev
->dev
, "Invalid format value\n");
171 struct simplefb_par
{
172 u32 palette
[PSEUDO_PALETTE_SIZE
];
173 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
179 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
181 * Clock handling code.
183 * Here we handle the clocks property of our "simple-framebuffer" dt node.
184 * This is necessary so that we can make sure that any clocks needed by
185 * the display engine that the bootloader set up for us (and for which it
186 * provided a simplefb dt node), stay up, for the life of the simplefb
189 * When the driver unloads, we cleanly disable, and then release the clocks.
191 * We only complain about errors here, no action is taken as the most likely
192 * error can only happen due to a mismatch between the bootloader which set
193 * up simplefb, and the clock definitions in the device tree. Chances are
194 * that there are no adverse effects, and if there are, a clean teardown of
195 * the fb probe will not help us much either. So just complain and carry on,
196 * and hope that the user actually gets a working fb at the end of things.
198 static int simplefb_clocks_init(struct simplefb_par
*par
,
199 struct platform_device
*pdev
)
201 struct device_node
*np
= pdev
->dev
.of_node
;
205 if (dev_get_platdata(&pdev
->dev
) || !np
)
208 par
->clk_count
= of_clk_get_parent_count(np
);
209 if (par
->clk_count
<= 0)
212 par
->clks
= kcalloc(par
->clk_count
, sizeof(struct clk
*), GFP_KERNEL
);
216 for (i
= 0; i
< par
->clk_count
; i
++) {
217 clock
= of_clk_get(np
, i
);
219 if (PTR_ERR(clock
) == -EPROBE_DEFER
) {
222 clk_put(par
->clks
[i
]);
225 return -EPROBE_DEFER
;
227 dev_err(&pdev
->dev
, "%s: clock %d not found: %ld\n",
228 __func__
, i
, PTR_ERR(clock
));
231 par
->clks
[i
] = clock
;
234 for (i
= 0; i
< par
->clk_count
; i
++) {
236 ret
= clk_prepare_enable(par
->clks
[i
]);
239 "%s: failed to enable clock %d: %d\n",
241 clk_put(par
->clks
[i
]);
250 static void simplefb_clocks_destroy(struct simplefb_par
*par
)
257 for (i
= 0; i
< par
->clk_count
; i
++) {
259 clk_disable_unprepare(par
->clks
[i
]);
260 clk_put(par
->clks
[i
]);
267 static int simplefb_clocks_init(struct simplefb_par
*par
,
268 struct platform_device
*pdev
) { return 0; }
269 static void simplefb_clocks_destroy(struct simplefb_par
*par
) { }
272 static int simplefb_probe(struct platform_device
*pdev
)
275 struct simplefb_params params
;
276 struct fb_info
*info
;
277 struct simplefb_par
*par
;
278 struct resource
*mem
;
280 if (fb_get_options("simplefb", NULL
))
284 if (dev_get_platdata(&pdev
->dev
))
285 ret
= simplefb_parse_pd(pdev
, ¶ms
);
286 else if (pdev
->dev
.of_node
)
287 ret
= simplefb_parse_dt(pdev
, ¶ms
);
292 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
294 dev_err(&pdev
->dev
, "No memory resource\n");
298 info
= framebuffer_alloc(sizeof(struct simplefb_par
), &pdev
->dev
);
301 platform_set_drvdata(pdev
, info
);
305 info
->fix
= simplefb_fix
;
306 info
->fix
.smem_start
= mem
->start
;
307 info
->fix
.smem_len
= resource_size(mem
);
308 info
->fix
.line_length
= params
.stride
;
310 info
->var
= simplefb_var
;
311 info
->var
.xres
= params
.width
;
312 info
->var
.yres
= params
.height
;
313 info
->var
.xres_virtual
= params
.width
;
314 info
->var
.yres_virtual
= params
.height
;
315 info
->var
.bits_per_pixel
= params
.format
->bits_per_pixel
;
316 info
->var
.red
= params
.format
->red
;
317 info
->var
.green
= params
.format
->green
;
318 info
->var
.blue
= params
.format
->blue
;
319 info
->var
.transp
= params
.format
->transp
;
321 info
->apertures
= alloc_apertures(1);
322 if (!info
->apertures
) {
324 goto error_fb_release
;
326 info
->apertures
->ranges
[0].base
= info
->fix
.smem_start
;
327 info
->apertures
->ranges
[0].size
= info
->fix
.smem_len
;
329 info
->fbops
= &simplefb_ops
;
330 info
->flags
= FBINFO_DEFAULT
| FBINFO_MISC_FIRMWARE
;
331 info
->screen_base
= ioremap_wc(info
->fix
.smem_start
,
333 if (!info
->screen_base
) {
335 goto error_fb_release
;
337 info
->pseudo_palette
= par
->palette
;
339 ret
= simplefb_clocks_init(par
, pdev
);
343 dev_info(&pdev
->dev
, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
344 info
->fix
.smem_start
, info
->fix
.smem_len
,
346 dev_info(&pdev
->dev
, "format=%s, mode=%dx%dx%d, linelength=%d\n",
348 info
->var
.xres
, info
->var
.yres
,
349 info
->var
.bits_per_pixel
, info
->fix
.line_length
);
351 ret
= register_framebuffer(info
);
353 dev_err(&pdev
->dev
, "Unable to register simplefb: %d\n", ret
);
357 dev_info(&pdev
->dev
, "fb%d: simplefb registered!\n", info
->node
);
362 simplefb_clocks_destroy(par
);
364 iounmap(info
->screen_base
);
366 framebuffer_release(info
);
370 static int simplefb_remove(struct platform_device
*pdev
)
372 struct fb_info
*info
= platform_get_drvdata(pdev
);
373 struct simplefb_par
*par
= info
->par
;
375 unregister_framebuffer(info
);
376 simplefb_clocks_destroy(par
);
377 framebuffer_release(info
);
382 static const struct of_device_id simplefb_of_match
[] = {
383 { .compatible
= "simple-framebuffer", },
386 MODULE_DEVICE_TABLE(of
, simplefb_of_match
);
388 static struct platform_driver simplefb_driver
= {
390 .name
= "simple-framebuffer",
391 .of_match_table
= simplefb_of_match
,
393 .probe
= simplefb_probe
,
394 .remove
= simplefb_remove
,
397 static int __init
simplefb_init(void)
400 struct device_node
*np
;
402 ret
= platform_driver_register(&simplefb_driver
);
406 if (IS_ENABLED(CONFIG_OF_ADDRESS
) && of_chosen
) {
407 for_each_child_of_node(of_chosen
, np
) {
408 if (of_device_is_compatible(np
, "simple-framebuffer"))
409 of_platform_device_create(np
, NULL
, NULL
);
416 fs_initcall(simplefb_init
);
418 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
419 MODULE_DESCRIPTION("Simple framebuffer driver");
420 MODULE_LICENSE("GPL v2");