of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / video / fbdev / simplefb.c
blob52c5c7e63b525786754689ce0e2751d9790ae0ff
1 /*
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
20 * more details.
23 #include <linux/errno.h>
24 #include <linux/fb.h>
25 #include <linux/io.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 = {
34 .id = "simple",
35 .type = FB_TYPE_PACKED_PIXELS,
36 .visual = FB_VISUAL_TRUECOLOR,
37 .accel = FB_ACCEL_NONE,
40 static struct fb_var_screeninfo simplefb_var = {
41 .height = -1,
42 .width = -1,
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);
56 u32 value;
58 if (regno >= PSEUDO_PALETTE_SIZE)
59 return -EINVAL;
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;
67 value |= mask;
69 pal[regno] = value;
71 return 0;
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 = {
81 .owner = THIS_MODULE,
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 {
92 u32 width;
93 u32 height;
94 u32 stride;
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;
102 int ret;
103 const char *format;
104 int i;
106 ret = of_property_read_u32(np, "width", &params->width);
107 if (ret) {
108 dev_err(&pdev->dev, "Can't parse width property\n");
109 return ret;
112 ret = of_property_read_u32(np, "height", &params->height);
113 if (ret) {
114 dev_err(&pdev->dev, "Can't parse height property\n");
115 return ret;
118 ret = of_property_read_u32(np, "stride", &params->stride);
119 if (ret) {
120 dev_err(&pdev->dev, "Can't parse stride property\n");
121 return ret;
124 ret = of_property_read_string(np, "format", &format);
125 if (ret) {
126 dev_err(&pdev->dev, "Can't parse format property\n");
127 return ret;
129 params->format = NULL;
130 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
131 if (strcmp(format, simplefb_formats[i].name))
132 continue;
133 params->format = &simplefb_formats[i];
134 break;
136 if (!params->format) {
137 dev_err(&pdev->dev, "Invalid format value\n");
138 return -EINVAL;
141 return 0;
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);
148 int i;
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))
157 continue;
159 params->format = &simplefb_formats[i];
160 break;
163 if (!params->format) {
164 dev_err(&pdev->dev, "Invalid format value\n");
165 return -EINVAL;
168 return 0;
171 struct simplefb_par {
172 u32 palette[PSEUDO_PALETTE_SIZE];
173 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
174 int clk_count;
175 struct clk **clks;
176 #endif
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
187 * driver.
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;
202 struct clk *clock;
203 int i, ret;
205 if (dev_get_platdata(&pdev->dev) || !np)
206 return 0;
208 par->clk_count = of_clk_get_parent_count(np);
209 if (par->clk_count <= 0)
210 return 0;
212 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
213 if (!par->clks)
214 return -ENOMEM;
216 for (i = 0; i < par->clk_count; i++) {
217 clock = of_clk_get(np, i);
218 if (IS_ERR(clock)) {
219 if (PTR_ERR(clock) == -EPROBE_DEFER) {
220 while (--i >= 0) {
221 if (par->clks[i])
222 clk_put(par->clks[i]);
224 kfree(par->clks);
225 return -EPROBE_DEFER;
227 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
228 __func__, i, PTR_ERR(clock));
229 continue;
231 par->clks[i] = clock;
234 for (i = 0; i < par->clk_count; i++) {
235 if (par->clks[i]) {
236 ret = clk_prepare_enable(par->clks[i]);
237 if (ret) {
238 dev_err(&pdev->dev,
239 "%s: failed to enable clock %d: %d\n",
240 __func__, i, ret);
241 clk_put(par->clks[i]);
242 par->clks[i] = NULL;
247 return 0;
250 static void simplefb_clocks_destroy(struct simplefb_par *par)
252 int i;
254 if (!par->clks)
255 return;
257 for (i = 0; i < par->clk_count; i++) {
258 if (par->clks[i]) {
259 clk_disable_unprepare(par->clks[i]);
260 clk_put(par->clks[i]);
264 kfree(par->clks);
266 #else
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) { }
270 #endif
272 static int simplefb_probe(struct platform_device *pdev)
274 int ret;
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))
281 return -ENODEV;
283 ret = -ENODEV;
284 if (dev_get_platdata(&pdev->dev))
285 ret = simplefb_parse_pd(pdev, &params);
286 else if (pdev->dev.of_node)
287 ret = simplefb_parse_dt(pdev, &params);
289 if (ret)
290 return ret;
292 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
293 if (!mem) {
294 dev_err(&pdev->dev, "No memory resource\n");
295 return -EINVAL;
298 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
299 if (!info)
300 return -ENOMEM;
301 platform_set_drvdata(pdev, info);
303 par = info->par;
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) {
323 ret = -ENOMEM;
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,
332 info->fix.smem_len);
333 if (!info->screen_base) {
334 ret = -ENOMEM;
335 goto error_fb_release;
337 info->pseudo_palette = par->palette;
339 ret = simplefb_clocks_init(par, pdev);
340 if (ret < 0)
341 goto error_unmap;
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,
345 info->screen_base);
346 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
347 params.format->name,
348 info->var.xres, info->var.yres,
349 info->var.bits_per_pixel, info->fix.line_length);
351 ret = register_framebuffer(info);
352 if (ret < 0) {
353 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
354 goto error_clocks;
357 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
359 return 0;
361 error_clocks:
362 simplefb_clocks_destroy(par);
363 error_unmap:
364 iounmap(info->screen_base);
365 error_fb_release:
366 framebuffer_release(info);
367 return ret;
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);
379 return 0;
382 static const struct of_device_id simplefb_of_match[] = {
383 { .compatible = "simple-framebuffer", },
384 { },
386 MODULE_DEVICE_TABLE(of, simplefb_of_match);
388 static struct platform_driver simplefb_driver = {
389 .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)
399 int ret;
400 struct device_node *np;
402 ret = platform_driver_register(&simplefb_driver);
403 if (ret)
404 return ret;
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);
413 return 0;
416 fs_initcall(simplefb_init);
418 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
419 MODULE_DESCRIPTION("Simple framebuffer driver");
420 MODULE_LICENSE("GPL v2");