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 struct fb_fix_screeninfo simplefb_fix = {
38 .type = FB_TYPE_PACKED_PIXELS,
39 .visual = FB_VISUAL_TRUECOLOR,
40 .accel = FB_ACCEL_NONE,
43 static 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;
77 static void simplefb_destroy(struct fb_info *info)
79 if (info->screen_base)
80 iounmap(info->screen_base);
83 static struct fb_ops simplefb_ops = {
85 .fb_destroy = simplefb_destroy,
86 .fb_setcolreg = simplefb_setcolreg,
87 .fb_fillrect = cfb_fillrect,
88 .fb_copyarea = cfb_copyarea,
89 .fb_imageblit = cfb_imageblit,
92 static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
94 struct simplefb_params {
98 struct simplefb_format *format;
101 static int simplefb_parse_dt(struct platform_device *pdev,
102 struct simplefb_params *params)
104 struct device_node *np = pdev->dev.of_node;
109 ret = of_property_read_u32(np, "width", ¶ms->width);
111 dev_err(&pdev->dev, "Can't parse width property\n");
115 ret = of_property_read_u32(np, "height", ¶ms->height);
117 dev_err(&pdev->dev, "Can't parse height property\n");
121 ret = of_property_read_u32(np, "stride", ¶ms->stride);
123 dev_err(&pdev->dev, "Can't parse stride property\n");
127 ret = of_property_read_string(np, "format", &format);
129 dev_err(&pdev->dev, "Can't parse format property\n");
132 params->format = NULL;
133 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
134 if (strcmp(format, simplefb_formats[i].name))
136 params->format = &simplefb_formats[i];
139 if (!params->format) {
140 dev_err(&pdev->dev, "Invalid format value\n");
147 static int simplefb_parse_pd(struct platform_device *pdev,
148 struct simplefb_params *params)
150 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
153 params->width = pd->width;
154 params->height = pd->height;
155 params->stride = pd->stride;
157 params->format = NULL;
158 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
159 if (strcmp(pd->format, simplefb_formats[i].name))
162 params->format = &simplefb_formats[i];
166 if (!params->format) {
167 dev_err(&pdev->dev, "Invalid format value\n");
174 struct simplefb_par {
175 u32 palette[PSEUDO_PALETTE_SIZE];
176 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
177 unsigned int clk_count;
180 #if defined CONFIG_OF && defined CONFIG_REGULATOR
182 struct regulator **regulators;
186 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
188 * Clock handling code.
190 * Here we handle the clocks property of our "simple-framebuffer" dt node.
191 * This is necessary so that we can make sure that any clocks needed by
192 * the display engine that the bootloader set up for us (and for which it
193 * provided a simplefb dt node), stay up, for the life of the simplefb
196 * When the driver unloads, we cleanly disable, and then release the clocks.
198 * We only complain about errors here, no action is taken as the most likely
199 * error can only happen due to a mismatch between the bootloader which set
200 * up simplefb, and the clock definitions in the device tree. Chances are
201 * that there are no adverse effects, and if there are, a clean teardown of
202 * the fb probe will not help us much either. So just complain and carry on,
203 * and hope that the user actually gets a working fb at the end of things.
205 static int simplefb_clocks_init(struct simplefb_par *par,
206 struct platform_device *pdev)
208 struct device_node *np = pdev->dev.of_node;
212 if (dev_get_platdata(&pdev->dev) || !np)
215 par->clk_count = of_clk_get_parent_count(np);
219 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
223 for (i = 0; i < par->clk_count; i++) {
224 clock = of_clk_get(np, i);
226 if (PTR_ERR(clock) == -EPROBE_DEFER) {
229 clk_put(par->clks[i]);
232 return -EPROBE_DEFER;
234 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
235 __func__, i, PTR_ERR(clock));
238 par->clks[i] = clock;
241 for (i = 0; i < par->clk_count; i++) {
243 ret = clk_prepare_enable(par->clks[i]);
246 "%s: failed to enable clock %d: %d\n",
248 clk_put(par->clks[i]);
257 static void simplefb_clocks_destroy(struct simplefb_par *par)
264 for (i = 0; i < par->clk_count; i++) {
266 clk_disable_unprepare(par->clks[i]);
267 clk_put(par->clks[i]);
274 static int simplefb_clocks_init(struct simplefb_par *par,
275 struct platform_device *pdev) { return 0; }
276 static void simplefb_clocks_destroy(struct simplefb_par *par) { }
279 #if defined CONFIG_OF && defined CONFIG_REGULATOR
281 #define SUPPLY_SUFFIX "-supply"
284 * Regulator handling code.
286 * Here we handle the num-supplies and vin*-supply properties of our
287 * "simple-framebuffer" dt node. This is necessary so that we can make sure
288 * that any regulators needed by the display hardware that the bootloader
289 * set up for us (and for which it provided a simplefb dt node), stay up,
290 * for the life of the simplefb driver.
292 * When the driver unloads, we cleanly disable, and then release the
295 * We only complain about errors here, no action is taken as the most likely
296 * error can only happen due to a mismatch between the bootloader which set
297 * up simplefb, and the regulator definitions in the device tree. Chances are
298 * that there are no adverse effects, and if there are, a clean teardown of
299 * the fb probe will not help us much either. So just complain and carry on,
300 * and hope that the user actually gets a working fb at the end of things.
302 static int simplefb_regulators_init(struct simplefb_par *par,
303 struct platform_device *pdev)
305 struct device_node *np = pdev->dev.of_node;
306 struct property *prop;
307 struct regulator *regulator;
309 int count = 0, i = 0, ret;
311 if (dev_get_platdata(&pdev->dev) || !np)
314 /* Count the number of regulator supplies */
315 for_each_property_of_node(np, prop) {
316 p = strstr(prop->name, SUPPLY_SUFFIX);
317 if (p && p != prop->name)
324 par->regulators = devm_kcalloc(&pdev->dev, count,
325 sizeof(struct regulator *), GFP_KERNEL);
326 if (!par->regulators)
329 /* Get all the regulators */
330 for_each_property_of_node(np, prop) {
331 char name[32]; /* 32 is max size of property name */
333 p = strstr(prop->name, SUPPLY_SUFFIX);
334 if (!p || p == prop->name)
337 strlcpy(name, prop->name,
338 strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
339 regulator = devm_regulator_get_optional(&pdev->dev, name);
340 if (IS_ERR(regulator)) {
341 if (PTR_ERR(regulator) == -EPROBE_DEFER)
342 return -EPROBE_DEFER;
343 dev_err(&pdev->dev, "regulator %s not found: %ld\n",
344 name, PTR_ERR(regulator));
347 par->regulators[i++] = regulator;
349 par->regulator_count = i;
351 /* Enable all the regulators */
352 for (i = 0; i < par->regulator_count; i++) {
353 ret = regulator_enable(par->regulators[i]);
356 "failed to enable regulator %d: %d\n",
358 devm_regulator_put(par->regulators[i]);
359 par->regulators[i] = NULL;
366 static void simplefb_regulators_destroy(struct simplefb_par *par)
370 if (!par->regulators)
373 for (i = 0; i < par->regulator_count; i++)
374 if (par->regulators[i])
375 regulator_disable(par->regulators[i]);
378 static int simplefb_regulators_init(struct simplefb_par *par,
379 struct platform_device *pdev) { return 0; }
380 static void simplefb_regulators_destroy(struct simplefb_par *par) { }
383 static int simplefb_probe(struct platform_device *pdev)
386 struct simplefb_params params;
387 struct fb_info *info;
388 struct simplefb_par *par;
389 struct resource *mem;
391 if (fb_get_options("simplefb", NULL))
395 if (dev_get_platdata(&pdev->dev))
396 ret = simplefb_parse_pd(pdev, ¶ms);
397 else if (pdev->dev.of_node)
398 ret = simplefb_parse_dt(pdev, ¶ms);
403 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
405 dev_err(&pdev->dev, "No memory resource\n");
409 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
412 platform_set_drvdata(pdev, info);
416 info->fix = simplefb_fix;
417 info->fix.smem_start = mem->start;
418 info->fix.smem_len = resource_size(mem);
419 info->fix.line_length = params.stride;
421 info->var = simplefb_var;
422 info->var.xres = params.width;
423 info->var.yres = params.height;
424 info->var.xres_virtual = params.width;
425 info->var.yres_virtual = params.height;
426 info->var.bits_per_pixel = params.format->bits_per_pixel;
427 info->var.red = params.format->red;
428 info->var.green = params.format->green;
429 info->var.blue = params.format->blue;
430 info->var.transp = params.format->transp;
432 info->apertures = alloc_apertures(1);
433 if (!info->apertures) {
435 goto error_fb_release;
437 info->apertures->ranges[0].base = info->fix.smem_start;
438 info->apertures->ranges[0].size = info->fix.smem_len;
440 info->fbops = &simplefb_ops;
441 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
442 info->screen_base = ioremap_wc(info->fix.smem_start,
444 if (!info->screen_base) {
446 goto error_fb_release;
448 info->pseudo_palette = par->palette;
450 ret = simplefb_clocks_init(par, pdev);
454 ret = simplefb_regulators_init(par, pdev);
458 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
459 info->fix.smem_start, info->fix.smem_len,
461 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
463 info->var.xres, info->var.yres,
464 info->var.bits_per_pixel, info->fix.line_length);
466 ret = register_framebuffer(info);
468 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
469 goto error_regulators;
472 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
477 simplefb_regulators_destroy(par);
479 simplefb_clocks_destroy(par);
481 iounmap(info->screen_base);
483 framebuffer_release(info);
487 static int simplefb_remove(struct platform_device *pdev)
489 struct fb_info *info = platform_get_drvdata(pdev);
490 struct simplefb_par *par = info->par;
492 unregister_framebuffer(info);
493 simplefb_regulators_destroy(par);
494 simplefb_clocks_destroy(par);
495 framebuffer_release(info);
500 static const struct of_device_id simplefb_of_match[] = {
501 { .compatible = "simple-framebuffer", },
504 MODULE_DEVICE_TABLE(of, simplefb_of_match);
506 static struct platform_driver simplefb_driver = {
508 .name = "simple-framebuffer",
509 .of_match_table = simplefb_of_match,
511 .probe = simplefb_probe,
512 .remove = simplefb_remove,
515 static int __init simplefb_init(void)
518 struct device_node *np;
520 ret = platform_driver_register(&simplefb_driver);
524 if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
525 for_each_child_of_node(of_chosen, np) {
526 if (of_device_is_compatible(np, "simple-framebuffer"))
527 of_platform_device_create(np, NULL, NULL);
534 fs_initcall(simplefb_init);
536 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
537 MODULE_DESCRIPTION("Simple framebuffer driver");
538 MODULE_LICENSE("GPL v2");