Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[linux-2.6-block.git] / drivers / video / fbdev / simplefb.c
CommitLineData
26549c8d
SW
1/*
2 * Simplest possible simple frame-buffer driver, as a platform device
3 *
4 * Copyright (c) 2013, Stephen Warren
5 *
6 * Based on q40fb.c, which was:
7 * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org>
8 *
9 * Also based on offb.c, which was:
10 * Copyright (C) 1997 Geert Uytterhoeven
11 * Copyright (C) 1996 Paul Mackerras
12 *
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.
16 *
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.
21 */
22
23#include <linux/errno.h>
24#include <linux/fb.h>
25#include <linux/io.h>
26#include <linux/module.h>
5ef76da6 27#include <linux/platform_data/simplefb.h>
26549c8d 28#include <linux/platform_device.h>
d49c6993 29#include <linux/clk.h>
fc219bfd 30#include <linux/clk-provider.h>
89c95001 31#include <linux/of_platform.h>
26549c8d
SW
32
33static 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,
38};
39
40static struct fb_var_screeninfo simplefb_var = {
41 .height = -1,
42 .width = -1,
43 .activate = FB_ACTIVATE_NOW,
44 .vmode = FB_VMODE_NONINTERLACED,
45};
46
1270be4a
LV
47#define PSEUDO_PALETTE_SIZE 16
48
26549c8d
SW
49static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
50 u_int transp, struct fb_info *info)
51{
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;
57
1270be4a 58 if (regno >= PSEUDO_PALETTE_SIZE)
26549c8d
SW
59 return -EINVAL;
60
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;
68 }
69 pal[regno] = value;
70
71 return 0;
72}
73
498f6d36
DH
74static void simplefb_destroy(struct fb_info *info)
75{
76 if (info->screen_base)
77 iounmap(info->screen_base);
78}
79
26549c8d
SW
80static struct fb_ops simplefb_ops = {
81 .owner = THIS_MODULE,
498f6d36 82 .fb_destroy = simplefb_destroy,
26549c8d
SW
83 .fb_setcolreg = simplefb_setcolreg,
84 .fb_fillrect = cfb_fillrect,
85 .fb_copyarea = cfb_copyarea,
86 .fb_imageblit = cfb_imageblit,
87};
88
5ef76da6 89static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
26549c8d
SW
90
91struct simplefb_params {
92 u32 width;
93 u32 height;
94 u32 stride;
95 struct simplefb_format *format;
96};
97
98static int simplefb_parse_dt(struct platform_device *pdev,
99 struct simplefb_params *params)
100{
101 struct device_node *np = pdev->dev.of_node;
102 int ret;
103 const char *format;
104 int i;
105
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;
110 }
111
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;
116 }
117
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;
122 }
123
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;
128 }
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;
135 }
136 if (!params->format) {
137 dev_err(&pdev->dev, "Invalid format value\n");
138 return -EINVAL;
139 }
140
141 return 0;
142}
143
5ef76da6
DH
144static int simplefb_parse_pd(struct platform_device *pdev,
145 struct simplefb_params *params)
146{
129f1be4 147 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
5ef76da6
DH
148 int i;
149
150 params->width = pd->width;
151 params->height = pd->height;
152 params->stride = pd->stride;
153
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;
158
159 params->format = &simplefb_formats[i];
160 break;
161 }
162
163 if (!params->format) {
164 dev_err(&pdev->dev, "Invalid format value\n");
165 return -EINVAL;
166 }
167
168 return 0;
169}
170
1270be4a
LV
171struct simplefb_par {
172 u32 palette[PSEUDO_PALETTE_SIZE];
8284731e 173#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
fc219bfd
LV
174 int clk_count;
175 struct clk **clks;
176#endif
1270be4a
LV
177};
178
8284731e 179#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
fc219bfd
LV
180/*
181 * Clock handling code.
182 *
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.
188 *
189 * When the driver unloads, we cleanly disable, and then release the clocks.
190 *
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.
197 */
198static int simplefb_clocks_init(struct simplefb_par *par,
199 struct platform_device *pdev)
200{
201 struct device_node *np = pdev->dev.of_node;
202 struct clk *clock;
203 int i, ret;
204
205 if (dev_get_platdata(&pdev->dev) || !np)
206 return 0;
207
208 par->clk_count = of_clk_get_parent_count(np);
209 if (par->clk_count <= 0)
210 return 0;
211
212 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
213 if (!par->clks)
214 return -ENOMEM;
215
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]);
223 }
224 kfree(par->clks);
225 return -EPROBE_DEFER;
226 }
227 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
228 __func__, i, PTR_ERR(clock));
229 continue;
230 }
231 par->clks[i] = clock;
232 }
233
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;
243 }
244 }
245 }
246
247 return 0;
248}
249
250static void simplefb_clocks_destroy(struct simplefb_par *par)
251{
252 int i;
253
254 if (!par->clks)
255 return;
256
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]);
261 }
262 }
263
264 kfree(par->clks);
265}
266#else
267static int simplefb_clocks_init(struct simplefb_par *par,
268 struct platform_device *pdev) { return 0; }
269static void simplefb_clocks_destroy(struct simplefb_par *par) { }
270#endif
271
26549c8d
SW
272static int simplefb_probe(struct platform_device *pdev)
273{
274 int ret;
275 struct simplefb_params params;
276 struct fb_info *info;
1270be4a 277 struct simplefb_par *par;
26549c8d
SW
278 struct resource *mem;
279
280 if (fb_get_options("simplefb", NULL))
281 return -ENODEV;
282
5ef76da6 283 ret = -ENODEV;
129f1be4 284 if (dev_get_platdata(&pdev->dev))
5ef76da6
DH
285 ret = simplefb_parse_pd(pdev, &params);
286 else if (pdev->dev.of_node)
287 ret = simplefb_parse_dt(pdev, &params);
288
26549c8d
SW
289 if (ret)
290 return ret;
291
292 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
293 if (!mem) {
294 dev_err(&pdev->dev, "No memory resource\n");
295 return -EINVAL;
296 }
297
1270be4a 298 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
26549c8d
SW
299 if (!info)
300 return -ENOMEM;
301 platform_set_drvdata(pdev, info);
302
1270be4a
LV
303 par = info->par;
304
26549c8d
SW
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;
309
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;
320
df0960ab
DH
321 info->apertures = alloc_apertures(1);
322 if (!info->apertures) {
bf2fda15
LV
323 ret = -ENOMEM;
324 goto error_fb_release;
df0960ab
DH
325 }
326 info->apertures->ranges[0].base = info->fix.smem_start;
327 info->apertures->ranges[0].size = info->fix.smem_len;
328
26549c8d 329 info->fbops = &simplefb_ops;
df0960ab 330 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
9e210be6
DH
331 info->screen_base = ioremap_wc(info->fix.smem_start,
332 info->fix.smem_len);
26549c8d 333 if (!info->screen_base) {
bf2fda15
LV
334 ret = -ENOMEM;
335 goto error_fb_release;
26549c8d 336 }
1270be4a 337 info->pseudo_palette = par->palette;
26549c8d 338
fc219bfd
LV
339 ret = simplefb_clocks_init(par, pdev);
340 if (ret < 0)
341 goto error_unmap;
342
9f192a92
TG
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);
350
26549c8d
SW
351 ret = register_framebuffer(info);
352 if (ret < 0) {
353 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
fc219bfd 354 goto error_clocks;
26549c8d
SW
355 }
356
357 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
358
359 return 0;
bf2fda15 360
fc219bfd
LV
361error_clocks:
362 simplefb_clocks_destroy(par);
bf2fda15
LV
363error_unmap:
364 iounmap(info->screen_base);
365error_fb_release:
366 framebuffer_release(info);
367 return ret;
26549c8d
SW
368}
369
370static int simplefb_remove(struct platform_device *pdev)
371{
372 struct fb_info *info = platform_get_drvdata(pdev);
fc219bfd 373 struct simplefb_par *par = info->par;
26549c8d
SW
374
375 unregister_framebuffer(info);
fc219bfd 376 simplefb_clocks_destroy(par);
26549c8d
SW
377 framebuffer_release(info);
378
379 return 0;
380}
381
382static const struct of_device_id simplefb_of_match[] = {
383 { .compatible = "simple-framebuffer", },
384 { },
385};
386MODULE_DEVICE_TABLE(of, simplefb_of_match);
387
388static struct platform_driver simplefb_driver = {
389 .driver = {
390 .name = "simple-framebuffer",
26549c8d
SW
391 .of_match_table = simplefb_of_match,
392 },
393 .probe = simplefb_probe,
394 .remove = simplefb_remove,
395};
89c95001
HG
396
397static int __init simplefb_init(void)
398{
399 int ret;
400 struct device_node *np;
401
402 ret = platform_driver_register(&simplefb_driver);
403 if (ret)
404 return ret;
405
d9e02019 406 if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
89c95001
HG
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);
410 }
411 }
412
413 return 0;
414}
415
0c5b240c 416fs_initcall(simplefb_init);
26549c8d
SW
417
418MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
419MODULE_DESCRIPTION("Simple framebuffer driver");
420MODULE_LICENSE("GPL v2");