blob: 48ccf6db62a2476cac273628605ab0a4a124f709 [file] [log] [blame]
Stephen Warren26549c82013-05-24 15:55:13 -07001/*
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>
David Herrmann5ef76da2013-08-02 14:05:20 +020027#include <linux/platform_data/simplefb.h>
Stephen Warren26549c82013-05-24 15:55:13 -070028#include <linux/platform_device.h>
Stephen Boydd49c6992015-06-19 15:00:46 -070029#include <linux/clk.h>
Luc Verhaegenfc219bf2014-11-14 13:26:50 +010030#include <linux/clk-provider.h>
Chen-Yu Tsai814740e2015-11-17 12:31:03 +080031#include <linux/of.h>
Hans de Goede89c95002014-11-14 13:26:53 +010032#include <linux/of_platform.h>
Chen-Yu Tsai814740e2015-11-17 12:31:03 +080033#include <linux/parser.h>
34#include <linux/regulator/consumer.h>
Stephen Warren26549c82013-05-24 15:55:13 -070035
36static struct fb_fix_screeninfo simplefb_fix = {
37 .id = "simple",
38 .type = FB_TYPE_PACKED_PIXELS,
39 .visual = FB_VISUAL_TRUECOLOR,
40 .accel = FB_ACCEL_NONE,
41};
42
43static struct fb_var_screeninfo simplefb_var = {
44 .height = -1,
45 .width = -1,
46 .activate = FB_ACTIVATE_NOW,
47 .vmode = FB_VMODE_NONINTERLACED,
48};
49
Luc Verhaegen1270be42014-11-14 13:26:48 +010050#define PSEUDO_PALETTE_SIZE 16
51
Stephen Warren26549c82013-05-24 15:55:13 -070052static 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
Luc Verhaegen1270be42014-11-14 13:26:48 +010061 if (regno >= PSEUDO_PALETTE_SIZE)
Stephen Warren26549c82013-05-24 15:55:13 -070062 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
David Herrmann498f6d32013-10-02 16:58:38 +020077static void simplefb_destroy(struct fb_info *info)
78{
79 if (info->screen_base)
80 iounmap(info->screen_base);
81}
82
Stephen Warren26549c82013-05-24 15:55:13 -070083static struct fb_ops simplefb_ops = {
84 .owner = THIS_MODULE,
David Herrmann498f6d32013-10-02 16:58:38 +020085 .fb_destroy = simplefb_destroy,
Stephen Warren26549c82013-05-24 15:55:13 -070086 .fb_setcolreg = simplefb_setcolreg,
87 .fb_fillrect = cfb_fillrect,
88 .fb_copyarea = cfb_copyarea,
89 .fb_imageblit = cfb_imageblit,
90};
91
David Herrmann5ef76da2013-08-02 14:05:20 +020092static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
Stephen Warren26549c82013-05-24 15:55:13 -070093
94struct simplefb_params {
95 u32 width;
96 u32 height;
97 u32 stride;
98 struct simplefb_format *format;
99};
100
101static int simplefb_parse_dt(struct platform_device *pdev,
102 struct simplefb_params *params)
103{
104 struct device_node *np = pdev->dev.of_node;
105 int ret;
106 const char *format;
107 int i;
108
109 ret = of_property_read_u32(np, "width", &params->width);
110 if (ret) {
111 dev_err(&pdev->dev, "Can't parse width property\n");
112 return ret;
113 }
114
115 ret = of_property_read_u32(np, "height", &params->height);
116 if (ret) {
117 dev_err(&pdev->dev, "Can't parse height property\n");
118 return ret;
119 }
120
121 ret = of_property_read_u32(np, "stride", &params->stride);
122 if (ret) {
123 dev_err(&pdev->dev, "Can't parse stride property\n");
124 return ret;
125 }
126
127 ret = of_property_read_string(np, "format", &format);
128 if (ret) {
129 dev_err(&pdev->dev, "Can't parse format property\n");
130 return ret;
131 }
132 params->format = NULL;
133 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
134 if (strcmp(format, simplefb_formats[i].name))
135 continue;
136 params->format = &simplefb_formats[i];
137 break;
138 }
139 if (!params->format) {
140 dev_err(&pdev->dev, "Invalid format value\n");
141 return -EINVAL;
142 }
143
144 return 0;
145}
146
David Herrmann5ef76da2013-08-02 14:05:20 +0200147static int simplefb_parse_pd(struct platform_device *pdev,
148 struct simplefb_params *params)
149{
Jingoo Han129f1be2013-09-17 14:11:04 +0900150 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
David Herrmann5ef76da2013-08-02 14:05:20 +0200151 int i;
152
153 params->width = pd->width;
154 params->height = pd->height;
155 params->stride = pd->stride;
156
157 params->format = NULL;
158 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
159 if (strcmp(pd->format, simplefb_formats[i].name))
160 continue;
161
162 params->format = &simplefb_formats[i];
163 break;
164 }
165
166 if (!params->format) {
167 dev_err(&pdev->dev, "Invalid format value\n");
168 return -EINVAL;
169 }
170
171 return 0;
172}
173
Luc Verhaegen1270be42014-11-14 13:26:48 +0100174struct simplefb_par {
175 u32 palette[PSEUDO_PALETTE_SIZE];
Hans de Goede82847312014-11-25 12:13:30 +0100176#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100177 int clk_count;
178 struct clk **clks;
179#endif
Chen-Yu Tsai814740e2015-11-17 12:31:03 +0800180#if defined CONFIG_OF && defined CONFIG_REGULATOR
181 u32 regulator_count;
182 struct regulator **regulators;
183#endif
Luc Verhaegen1270be42014-11-14 13:26:48 +0100184};
185
Hans de Goede82847312014-11-25 12:13:30 +0100186#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100187/*
188 * Clock handling code.
189 *
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
194 * driver.
195 *
196 * When the driver unloads, we cleanly disable, and then release the clocks.
197 *
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.
204 */
205static int simplefb_clocks_init(struct simplefb_par *par,
206 struct platform_device *pdev)
207{
208 struct device_node *np = pdev->dev.of_node;
209 struct clk *clock;
210 int i, ret;
211
212 if (dev_get_platdata(&pdev->dev) || !np)
213 return 0;
214
215 par->clk_count = of_clk_get_parent_count(np);
216 if (par->clk_count <= 0)
217 return 0;
218
219 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
220 if (!par->clks)
221 return -ENOMEM;
222
223 for (i = 0; i < par->clk_count; i++) {
224 clock = of_clk_get(np, i);
225 if (IS_ERR(clock)) {
226 if (PTR_ERR(clock) == -EPROBE_DEFER) {
227 while (--i >= 0) {
228 if (par->clks[i])
229 clk_put(par->clks[i]);
230 }
231 kfree(par->clks);
232 return -EPROBE_DEFER;
233 }
234 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
235 __func__, i, PTR_ERR(clock));
236 continue;
237 }
238 par->clks[i] = clock;
239 }
240
241 for (i = 0; i < par->clk_count; i++) {
242 if (par->clks[i]) {
243 ret = clk_prepare_enable(par->clks[i]);
244 if (ret) {
245 dev_err(&pdev->dev,
246 "%s: failed to enable clock %d: %d\n",
247 __func__, i, ret);
248 clk_put(par->clks[i]);
249 par->clks[i] = NULL;
250 }
251 }
252 }
253
254 return 0;
255}
256
257static void simplefb_clocks_destroy(struct simplefb_par *par)
258{
259 int i;
260
261 if (!par->clks)
262 return;
263
264 for (i = 0; i < par->clk_count; i++) {
265 if (par->clks[i]) {
266 clk_disable_unprepare(par->clks[i]);
267 clk_put(par->clks[i]);
268 }
269 }
270
271 kfree(par->clks);
272}
273#else
274static int simplefb_clocks_init(struct simplefb_par *par,
275 struct platform_device *pdev) { return 0; }
276static void simplefb_clocks_destroy(struct simplefb_par *par) { }
277#endif
278
Chen-Yu Tsai814740e2015-11-17 12:31:03 +0800279#if defined CONFIG_OF && defined CONFIG_REGULATOR
280
281#define SUPPLY_SUFFIX "-supply"
282
283/*
284 * Regulator handling code.
285 *
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.
291 *
292 * When the driver unloads, we cleanly disable, and then release the
293 * regulators.
294 *
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.
301 */
302static int simplefb_regulators_init(struct simplefb_par *par,
303 struct platform_device *pdev)
304{
305 struct device_node *np = pdev->dev.of_node;
306 struct property *prop;
307 struct regulator *regulator;
308 const char *p;
309 int count = 0, i = 0, ret;
310
311 if (dev_get_platdata(&pdev->dev) || !np)
312 return 0;
313
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)
318 count++;
319 }
320
321 if (!count)
322 return 0;
323
324 par->regulators = devm_kcalloc(&pdev->dev, count,
325 sizeof(struct regulator *), GFP_KERNEL);
326 if (!par->regulators)
327 return -ENOMEM;
328
329 /* Get all the regulators */
330 for_each_property_of_node(np, prop) {
331 char name[32]; /* 32 is max size of property name */
332
333 p = strstr(prop->name, SUPPLY_SUFFIX);
334 if (!p || p == prop->name)
335 continue;
336
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));
345 continue;
346 }
347 par->regulators[i++] = regulator;
348 }
349 par->regulator_count = i;
350
351 /* Enable all the regulators */
352 for (i = 0; i < par->regulator_count; i++) {
353 ret = regulator_enable(par->regulators[i]);
354 if (ret) {
355 dev_err(&pdev->dev,
356 "failed to enable regulator %d: %d\n",
357 i, ret);
358 devm_regulator_put(par->regulators[i]);
359 par->regulators[i] = NULL;
360 }
361 }
362
363 return 0;
364}
365
366static void simplefb_regulators_destroy(struct simplefb_par *par)
367{
368 int i;
369
370 if (!par->regulators)
371 return;
372
373 for (i = 0; i < par->regulator_count; i++)
374 if (par->regulators[i])
375 regulator_disable(par->regulators[i]);
376}
377#else
378static int simplefb_regulators_init(struct simplefb_par *par,
379 struct platform_device *pdev) { return 0; }
380static void simplefb_regulators_destroy(struct simplefb_par *par) { }
381#endif
382
Stephen Warren26549c82013-05-24 15:55:13 -0700383static int simplefb_probe(struct platform_device *pdev)
384{
385 int ret;
386 struct simplefb_params params;
387 struct fb_info *info;
Luc Verhaegen1270be42014-11-14 13:26:48 +0100388 struct simplefb_par *par;
Stephen Warren26549c82013-05-24 15:55:13 -0700389 struct resource *mem;
390
391 if (fb_get_options("simplefb", NULL))
392 return -ENODEV;
393
David Herrmann5ef76da2013-08-02 14:05:20 +0200394 ret = -ENODEV;
Jingoo Han129f1be2013-09-17 14:11:04 +0900395 if (dev_get_platdata(&pdev->dev))
David Herrmann5ef76da2013-08-02 14:05:20 +0200396 ret = simplefb_parse_pd(pdev, &params);
397 else if (pdev->dev.of_node)
398 ret = simplefb_parse_dt(pdev, &params);
399
Stephen Warren26549c82013-05-24 15:55:13 -0700400 if (ret)
401 return ret;
402
403 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
404 if (!mem) {
405 dev_err(&pdev->dev, "No memory resource\n");
406 return -EINVAL;
407 }
408
Luc Verhaegen1270be42014-11-14 13:26:48 +0100409 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
Stephen Warren26549c82013-05-24 15:55:13 -0700410 if (!info)
411 return -ENOMEM;
412 platform_set_drvdata(pdev, info);
413
Luc Verhaegen1270be42014-11-14 13:26:48 +0100414 par = info->par;
415
Stephen Warren26549c82013-05-24 15:55:13 -0700416 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;
420
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;
431
David Herrmanndf0960a2013-08-02 14:05:21 +0200432 info->apertures = alloc_apertures(1);
433 if (!info->apertures) {
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100434 ret = -ENOMEM;
435 goto error_fb_release;
David Herrmanndf0960a2013-08-02 14:05:21 +0200436 }
437 info->apertures->ranges[0].base = info->fix.smem_start;
438 info->apertures->ranges[0].size = info->fix.smem_len;
439
Stephen Warren26549c82013-05-24 15:55:13 -0700440 info->fbops = &simplefb_ops;
David Herrmanndf0960a2013-08-02 14:05:21 +0200441 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
David Herrmann9e210be2013-10-02 16:58:39 +0200442 info->screen_base = ioremap_wc(info->fix.smem_start,
443 info->fix.smem_len);
Stephen Warren26549c82013-05-24 15:55:13 -0700444 if (!info->screen_base) {
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100445 ret = -ENOMEM;
446 goto error_fb_release;
Stephen Warren26549c82013-05-24 15:55:13 -0700447 }
Luc Verhaegen1270be42014-11-14 13:26:48 +0100448 info->pseudo_palette = par->palette;
Stephen Warren26549c82013-05-24 15:55:13 -0700449
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100450 ret = simplefb_clocks_init(par, pdev);
451 if (ret < 0)
452 goto error_unmap;
453
Chen-Yu Tsai814740e2015-11-17 12:31:03 +0800454 ret = simplefb_regulators_init(par, pdev);
455 if (ret < 0)
456 goto error_clocks;
457
Tom Gundersen9f192a92013-09-07 16:08:35 +0200458 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,
460 info->screen_base);
461 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
462 params.format->name,
463 info->var.xres, info->var.yres,
464 info->var.bits_per_pixel, info->fix.line_length);
465
Stephen Warren26549c82013-05-24 15:55:13 -0700466 ret = register_framebuffer(info);
467 if (ret < 0) {
468 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
Chen-Yu Tsai814740e2015-11-17 12:31:03 +0800469 goto error_regulators;
Stephen Warren26549c82013-05-24 15:55:13 -0700470 }
471
472 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
473
474 return 0;
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100475
Chen-Yu Tsai814740e2015-11-17 12:31:03 +0800476error_regulators:
477 simplefb_regulators_destroy(par);
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100478error_clocks:
479 simplefb_clocks_destroy(par);
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100480error_unmap:
481 iounmap(info->screen_base);
482error_fb_release:
483 framebuffer_release(info);
484 return ret;
Stephen Warren26549c82013-05-24 15:55:13 -0700485}
486
487static int simplefb_remove(struct platform_device *pdev)
488{
489 struct fb_info *info = platform_get_drvdata(pdev);
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100490 struct simplefb_par *par = info->par;
Stephen Warren26549c82013-05-24 15:55:13 -0700491
492 unregister_framebuffer(info);
Chen-Yu Tsai814740e2015-11-17 12:31:03 +0800493 simplefb_regulators_destroy(par);
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100494 simplefb_clocks_destroy(par);
Stephen Warren26549c82013-05-24 15:55:13 -0700495 framebuffer_release(info);
496
497 return 0;
498}
499
500static const struct of_device_id simplefb_of_match[] = {
501 { .compatible = "simple-framebuffer", },
502 { },
503};
504MODULE_DEVICE_TABLE(of, simplefb_of_match);
505
506static struct platform_driver simplefb_driver = {
507 .driver = {
508 .name = "simple-framebuffer",
Stephen Warren26549c82013-05-24 15:55:13 -0700509 .of_match_table = simplefb_of_match,
510 },
511 .probe = simplefb_probe,
512 .remove = simplefb_remove,
513};
Hans de Goede89c95002014-11-14 13:26:53 +0100514
515static int __init simplefb_init(void)
516{
517 int ret;
518 struct device_node *np;
519
520 ret = platform_driver_register(&simplefb_driver);
521 if (ret)
522 return ret;
523
Hans de Goeded9e02012015-01-05 09:15:16 +0100524 if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
Hans de Goede89c95002014-11-14 13:26:53 +0100525 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);
528 }
529 }
530
531 return 0;
532}
533
Hans de Goede0c5b2402014-11-14 13:26:55 +0100534fs_initcall(simplefb_init);
Stephen Warren26549c82013-05-24 15:55:13 -0700535
536MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
537MODULE_DESCRIPTION("Simple framebuffer driver");
538MODULE_LICENSE("GPL v2");