Merge branch 'fixes-for-v4.9-rc2' of http://git.agner.ch/git/linux-drm-fsl-dcu into...
[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>
814740e7 31#include <linux/of.h>
89c95001 32#include <linux/of_platform.h>
814740e7
CYT
33#include <linux/parser.h>
34#include <linux/regulator/consumer.h>
26549c8d 35
ca9384c5 36static const struct fb_fix_screeninfo simplefb_fix = {
26549c8d
SW
37 .id = "simple",
38 .type = FB_TYPE_PACKED_PIXELS,
39 .visual = FB_VISUAL_TRUECOLOR,
40 .accel = FB_ACCEL_NONE,
41};
42
ca9384c5 43static const struct fb_var_screeninfo simplefb_var = {
26549c8d
SW
44 .height = -1,
45 .width = -1,
46 .activate = FB_ACTIVATE_NOW,
47 .vmode = FB_VMODE_NONINTERLACED,
48};
49
1270be4a
LV
50#define PSEUDO_PALETTE_SIZE 16
51
26549c8d
SW
52static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
53 u_int transp, struct fb_info *info)
54{
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);
59 u32 value;
60
1270be4a 61 if (regno >= PSEUDO_PALETTE_SIZE)
26549c8d
SW
62 return -EINVAL;
63
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;
70 value |= mask;
71 }
72 pal[regno] = value;
73
74 return 0;
75}
76
7c9806e8
CYT
77struct simplefb_par;
78static void simplefb_clocks_destroy(struct simplefb_par *par);
79static void simplefb_regulators_destroy(struct simplefb_par *par);
80
498f6d36
DH
81static void simplefb_destroy(struct fb_info *info)
82{
7c9806e8
CYT
83 simplefb_regulators_destroy(info->par);
84 simplefb_clocks_destroy(info->par);
498f6d36
DH
85 if (info->screen_base)
86 iounmap(info->screen_base);
87}
88
26549c8d
SW
89static struct fb_ops simplefb_ops = {
90 .owner = THIS_MODULE,
498f6d36 91 .fb_destroy = simplefb_destroy,
26549c8d
SW
92 .fb_setcolreg = simplefb_setcolreg,
93 .fb_fillrect = cfb_fillrect,
94 .fb_copyarea = cfb_copyarea,
95 .fb_imageblit = cfb_imageblit,
96};
97
5ef76da6 98static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
26549c8d
SW
99
100struct simplefb_params {
101 u32 width;
102 u32 height;
103 u32 stride;
104 struct simplefb_format *format;
105};
106
107static int simplefb_parse_dt(struct platform_device *pdev,
108 struct simplefb_params *params)
109{
110 struct device_node *np = pdev->dev.of_node;
111 int ret;
112 const char *format;
113 int i;
114
115 ret = of_property_read_u32(np, "width", &params->width);
116 if (ret) {
117 dev_err(&pdev->dev, "Can't parse width property\n");
118 return ret;
119 }
120
121 ret = of_property_read_u32(np, "height", &params->height);
122 if (ret) {
123 dev_err(&pdev->dev, "Can't parse height property\n");
124 return ret;
125 }
126
127 ret = of_property_read_u32(np, "stride", &params->stride);
128 if (ret) {
129 dev_err(&pdev->dev, "Can't parse stride property\n");
130 return ret;
131 }
132
133 ret = of_property_read_string(np, "format", &format);
134 if (ret) {
135 dev_err(&pdev->dev, "Can't parse format property\n");
136 return ret;
137 }
138 params->format = NULL;
139 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
140 if (strcmp(format, simplefb_formats[i].name))
141 continue;
142 params->format = &simplefb_formats[i];
143 break;
144 }
145 if (!params->format) {
146 dev_err(&pdev->dev, "Invalid format value\n");
147 return -EINVAL;
148 }
149
150 return 0;
151}
152
5ef76da6
DH
153static int simplefb_parse_pd(struct platform_device *pdev,
154 struct simplefb_params *params)
155{
129f1be4 156 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
5ef76da6
DH
157 int i;
158
159 params->width = pd->width;
160 params->height = pd->height;
161 params->stride = pd->stride;
162
163 params->format = NULL;
164 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
165 if (strcmp(pd->format, simplefb_formats[i].name))
166 continue;
167
168 params->format = &simplefb_formats[i];
169 break;
170 }
171
172 if (!params->format) {
173 dev_err(&pdev->dev, "Invalid format value\n");
174 return -EINVAL;
175 }
176
177 return 0;
178}
179
1270be4a
LV
180struct simplefb_par {
181 u32 palette[PSEUDO_PALETTE_SIZE];
8284731e 182#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
2cd82d06 183 unsigned int clk_count;
fc219bfd
LV
184 struct clk **clks;
185#endif
814740e7
CYT
186#if defined CONFIG_OF && defined CONFIG_REGULATOR
187 u32 regulator_count;
188 struct regulator **regulators;
189#endif
1270be4a
LV
190};
191
8284731e 192#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
fc219bfd
LV
193/*
194 * Clock handling code.
195 *
196 * Here we handle the clocks property of our "simple-framebuffer" dt node.
197 * This is necessary so that we can make sure that any clocks needed by
198 * the display engine that the bootloader set up for us (and for which it
199 * provided a simplefb dt node), stay up, for the life of the simplefb
200 * driver.
201 *
202 * When the driver unloads, we cleanly disable, and then release the clocks.
203 *
204 * We only complain about errors here, no action is taken as the most likely
205 * error can only happen due to a mismatch between the bootloader which set
206 * up simplefb, and the clock definitions in the device tree. Chances are
207 * that there are no adverse effects, and if there are, a clean teardown of
208 * the fb probe will not help us much either. So just complain and carry on,
209 * and hope that the user actually gets a working fb at the end of things.
210 */
211static int simplefb_clocks_init(struct simplefb_par *par,
212 struct platform_device *pdev)
213{
214 struct device_node *np = pdev->dev.of_node;
215 struct clk *clock;
216 int i, ret;
217
218 if (dev_get_platdata(&pdev->dev) || !np)
219 return 0;
220
221 par->clk_count = of_clk_get_parent_count(np);
2cd82d06 222 if (!par->clk_count)
fc219bfd
LV
223 return 0;
224
225 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
226 if (!par->clks)
227 return -ENOMEM;
228
229 for (i = 0; i < par->clk_count; i++) {
230 clock = of_clk_get(np, i);
231 if (IS_ERR(clock)) {
232 if (PTR_ERR(clock) == -EPROBE_DEFER) {
233 while (--i >= 0) {
234 if (par->clks[i])
235 clk_put(par->clks[i]);
236 }
237 kfree(par->clks);
238 return -EPROBE_DEFER;
239 }
240 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
241 __func__, i, PTR_ERR(clock));
242 continue;
243 }
244 par->clks[i] = clock;
245 }
246
247 for (i = 0; i < par->clk_count; i++) {
248 if (par->clks[i]) {
249 ret = clk_prepare_enable(par->clks[i]);
250 if (ret) {
251 dev_err(&pdev->dev,
252 "%s: failed to enable clock %d: %d\n",
253 __func__, i, ret);
254 clk_put(par->clks[i]);
255 par->clks[i] = NULL;
256 }
257 }
258 }
259
260 return 0;
261}
262
263static void simplefb_clocks_destroy(struct simplefb_par *par)
264{
265 int i;
266
267 if (!par->clks)
268 return;
269
270 for (i = 0; i < par->clk_count; i++) {
271 if (par->clks[i]) {
272 clk_disable_unprepare(par->clks[i]);
273 clk_put(par->clks[i]);
274 }
275 }
276
277 kfree(par->clks);
278}
279#else
280static int simplefb_clocks_init(struct simplefb_par *par,
281 struct platform_device *pdev) { return 0; }
282static void simplefb_clocks_destroy(struct simplefb_par *par) { }
283#endif
284
814740e7
CYT
285#if defined CONFIG_OF && defined CONFIG_REGULATOR
286
287#define SUPPLY_SUFFIX "-supply"
288
289/*
290 * Regulator handling code.
291 *
292 * Here we handle the num-supplies and vin*-supply properties of our
293 * "simple-framebuffer" dt node. This is necessary so that we can make sure
294 * that any regulators needed by the display hardware that the bootloader
295 * set up for us (and for which it provided a simplefb dt node), stay up,
296 * for the life of the simplefb driver.
297 *
298 * When the driver unloads, we cleanly disable, and then release the
299 * regulators.
300 *
301 * We only complain about errors here, no action is taken as the most likely
302 * error can only happen due to a mismatch between the bootloader which set
303 * up simplefb, and the regulator definitions in the device tree. Chances are
304 * that there are no adverse effects, and if there are, a clean teardown of
305 * the fb probe will not help us much either. So just complain and carry on,
306 * and hope that the user actually gets a working fb at the end of things.
307 */
308static int simplefb_regulators_init(struct simplefb_par *par,
309 struct platform_device *pdev)
310{
311 struct device_node *np = pdev->dev.of_node;
312 struct property *prop;
313 struct regulator *regulator;
314 const char *p;
315 int count = 0, i = 0, ret;
316
317 if (dev_get_platdata(&pdev->dev) || !np)
318 return 0;
319
320 /* Count the number of regulator supplies */
321 for_each_property_of_node(np, prop) {
322 p = strstr(prop->name, SUPPLY_SUFFIX);
323 if (p && p != prop->name)
324 count++;
325 }
326
327 if (!count)
328 return 0;
329
330 par->regulators = devm_kcalloc(&pdev->dev, count,
331 sizeof(struct regulator *), GFP_KERNEL);
332 if (!par->regulators)
333 return -ENOMEM;
334
335 /* Get all the regulators */
336 for_each_property_of_node(np, prop) {
337 char name[32]; /* 32 is max size of property name */
338
339 p = strstr(prop->name, SUPPLY_SUFFIX);
340 if (!p || p == prop->name)
341 continue;
342
343 strlcpy(name, prop->name,
344 strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
345 regulator = devm_regulator_get_optional(&pdev->dev, name);
346 if (IS_ERR(regulator)) {
347 if (PTR_ERR(regulator) == -EPROBE_DEFER)
348 return -EPROBE_DEFER;
349 dev_err(&pdev->dev, "regulator %s not found: %ld\n",
350 name, PTR_ERR(regulator));
351 continue;
352 }
353 par->regulators[i++] = regulator;
354 }
355 par->regulator_count = i;
356
357 /* Enable all the regulators */
358 for (i = 0; i < par->regulator_count; i++) {
359 ret = regulator_enable(par->regulators[i]);
360 if (ret) {
361 dev_err(&pdev->dev,
362 "failed to enable regulator %d: %d\n",
363 i, ret);
364 devm_regulator_put(par->regulators[i]);
365 par->regulators[i] = NULL;
366 }
367 }
368
369 return 0;
370}
371
372static void simplefb_regulators_destroy(struct simplefb_par *par)
373{
374 int i;
375
376 if (!par->regulators)
377 return;
378
379 for (i = 0; i < par->regulator_count; i++)
380 if (par->regulators[i])
381 regulator_disable(par->regulators[i]);
382}
383#else
384static int simplefb_regulators_init(struct simplefb_par *par,
385 struct platform_device *pdev) { return 0; }
386static void simplefb_regulators_destroy(struct simplefb_par *par) { }
387#endif
388
26549c8d
SW
389static int simplefb_probe(struct platform_device *pdev)
390{
391 int ret;
392 struct simplefb_params params;
393 struct fb_info *info;
1270be4a 394 struct simplefb_par *par;
26549c8d
SW
395 struct resource *mem;
396
397 if (fb_get_options("simplefb", NULL))
398 return -ENODEV;
399
5ef76da6 400 ret = -ENODEV;
129f1be4 401 if (dev_get_platdata(&pdev->dev))
5ef76da6
DH
402 ret = simplefb_parse_pd(pdev, &params);
403 else if (pdev->dev.of_node)
404 ret = simplefb_parse_dt(pdev, &params);
405
26549c8d
SW
406 if (ret)
407 return ret;
408
409 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
410 if (!mem) {
411 dev_err(&pdev->dev, "No memory resource\n");
412 return -EINVAL;
413 }
414
1270be4a 415 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
26549c8d
SW
416 if (!info)
417 return -ENOMEM;
418 platform_set_drvdata(pdev, info);
419
1270be4a
LV
420 par = info->par;
421
26549c8d
SW
422 info->fix = simplefb_fix;
423 info->fix.smem_start = mem->start;
424 info->fix.smem_len = resource_size(mem);
425 info->fix.line_length = params.stride;
426
427 info->var = simplefb_var;
428 info->var.xres = params.width;
429 info->var.yres = params.height;
430 info->var.xres_virtual = params.width;
431 info->var.yres_virtual = params.height;
432 info->var.bits_per_pixel = params.format->bits_per_pixel;
433 info->var.red = params.format->red;
434 info->var.green = params.format->green;
435 info->var.blue = params.format->blue;
436 info->var.transp = params.format->transp;
437
df0960ab
DH
438 info->apertures = alloc_apertures(1);
439 if (!info->apertures) {
bf2fda15
LV
440 ret = -ENOMEM;
441 goto error_fb_release;
df0960ab
DH
442 }
443 info->apertures->ranges[0].base = info->fix.smem_start;
444 info->apertures->ranges[0].size = info->fix.smem_len;
445
26549c8d 446 info->fbops = &simplefb_ops;
df0960ab 447 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
9e210be6
DH
448 info->screen_base = ioremap_wc(info->fix.smem_start,
449 info->fix.smem_len);
26549c8d 450 if (!info->screen_base) {
bf2fda15
LV
451 ret = -ENOMEM;
452 goto error_fb_release;
26549c8d 453 }
1270be4a 454 info->pseudo_palette = par->palette;
26549c8d 455
fc219bfd
LV
456 ret = simplefb_clocks_init(par, pdev);
457 if (ret < 0)
458 goto error_unmap;
459
814740e7
CYT
460 ret = simplefb_regulators_init(par, pdev);
461 if (ret < 0)
462 goto error_clocks;
463
9f192a92
TG
464 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
465 info->fix.smem_start, info->fix.smem_len,
466 info->screen_base);
467 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
468 params.format->name,
469 info->var.xres, info->var.yres,
470 info->var.bits_per_pixel, info->fix.line_length);
471
26549c8d
SW
472 ret = register_framebuffer(info);
473 if (ret < 0) {
474 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
814740e7 475 goto error_regulators;
26549c8d
SW
476 }
477
478 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
479
480 return 0;
bf2fda15 481
814740e7
CYT
482error_regulators:
483 simplefb_regulators_destroy(par);
fc219bfd
LV
484error_clocks:
485 simplefb_clocks_destroy(par);
bf2fda15
LV
486error_unmap:
487 iounmap(info->screen_base);
488error_fb_release:
489 framebuffer_release(info);
490 return ret;
26549c8d
SW
491}
492
493static int simplefb_remove(struct platform_device *pdev)
494{
495 struct fb_info *info = platform_get_drvdata(pdev);
496
497 unregister_framebuffer(info);
498 framebuffer_release(info);
499
500 return 0;
501}
502
503static const struct of_device_id simplefb_of_match[] = {
504 { .compatible = "simple-framebuffer", },
505 { },
506};
507MODULE_DEVICE_TABLE(of, simplefb_of_match);
508
509static struct platform_driver simplefb_driver = {
510 .driver = {
511 .name = "simple-framebuffer",
26549c8d
SW
512 .of_match_table = simplefb_of_match,
513 },
514 .probe = simplefb_probe,
515 .remove = simplefb_remove,
516};
89c95001
HG
517
518static int __init simplefb_init(void)
519{
520 int ret;
521 struct device_node *np;
522
523 ret = platform_driver_register(&simplefb_driver);
524 if (ret)
525 return ret;
526
d9e02019 527 if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
89c95001
HG
528 for_each_child_of_node(of_chosen, np) {
529 if (of_device_is_compatible(np, "simple-framebuffer"))
530 of_platform_device_create(np, NULL, NULL);
531 }
532 }
533
534 return 0;
535}
536
0c5b240c 537fs_initcall(simplefb_init);
26549c8d
SW
538
539MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
540MODULE_DESCRIPTION("Simple framebuffer driver");
541MODULE_LICENSE("GPL v2");