]> git.ipfire.org Git - thirdparty/linux.git/blob - drivers/video/fbdev/simplefb.c
18025f34fde71e2bb6e0a3be29477e3b806ab623
[thirdparty/linux.git] / drivers / video / fbdev / simplefb.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Simplest possible simple frame-buffer driver, as a platform device
4 *
5 * Copyright (c) 2013, Stephen Warren
6 *
7 * Based on q40fb.c, which was:
8 * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org>
9 *
10 * Also based on offb.c, which was:
11 * Copyright (C) 1997 Geert Uytterhoeven
12 * Copyright (C) 1996 Paul Mackerras
13 */
14
15 #include <linux/aperture.h>
16 #include <linux/errno.h>
17 #include <linux/fb.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/platform_data/simplefb.h>
21 #include <linux/platform_device.h>
22 #include <linux/clk.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/of_clk.h>
26 #include <linux/of_platform.h>
27 #include <linux/parser.h>
28 #include <linux/regulator/consumer.h>
29
30 static const struct fb_fix_screeninfo simplefb_fix = {
31 .id = "simple",
32 .type = FB_TYPE_PACKED_PIXELS,
33 .visual = FB_VISUAL_TRUECOLOR,
34 .accel = FB_ACCEL_NONE,
35 };
36
37 static const struct fb_var_screeninfo simplefb_var = {
38 .height = -1,
39 .width = -1,
40 .activate = FB_ACTIVATE_NOW,
41 .vmode = FB_VMODE_NONINTERLACED,
42 };
43
44 #define PSEUDO_PALETTE_SIZE 16
45
46 static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
47 u_int transp, struct fb_info *info)
48 {
49 u32 *pal = info->pseudo_palette;
50 u32 cr = red >> (16 - info->var.red.length);
51 u32 cg = green >> (16 - info->var.green.length);
52 u32 cb = blue >> (16 - info->var.blue.length);
53 u32 value;
54
55 if (regno >= PSEUDO_PALETTE_SIZE)
56 return -EINVAL;
57
58 value = (cr << info->var.red.offset) |
59 (cg << info->var.green.offset) |
60 (cb << info->var.blue.offset);
61 if (info->var.transp.length > 0) {
62 u32 mask = (1 << info->var.transp.length) - 1;
63 mask <<= info->var.transp.offset;
64 value |= mask;
65 }
66 pal[regno] = value;
67
68 return 0;
69 }
70
71 struct simplefb_par {
72 u32 palette[PSEUDO_PALETTE_SIZE];
73 resource_size_t base;
74 resource_size_t size;
75 struct resource *mem;
76 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
77 bool clks_enabled;
78 unsigned int clk_count;
79 struct clk **clks;
80 #endif
81 #if defined CONFIG_OF && defined CONFIG_REGULATOR
82 bool regulators_enabled;
83 u32 regulator_count;
84 struct regulator **regulators;
85 #endif
86 };
87
88 static void simplefb_clocks_destroy(struct simplefb_par *par);
89 static void simplefb_regulators_destroy(struct simplefb_par *par);
90
91 /*
92 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
93 * of unregister_framebuffer() or fb_release(). Do any cleanup here.
94 */
95 static void simplefb_destroy(struct fb_info *info)
96 {
97 struct simplefb_par *par = info->par;
98 struct resource *mem = par->mem;
99
100 simplefb_regulators_destroy(info->par);
101 simplefb_clocks_destroy(info->par);
102 if (info->screen_base)
103 iounmap(info->screen_base);
104
105 framebuffer_release(info);
106
107 if (mem)
108 release_mem_region(mem->start, resource_size(mem));
109 }
110
111 static const struct fb_ops simplefb_ops = {
112 .owner = THIS_MODULE,
113 FB_DEFAULT_IOMEM_OPS,
114 .fb_destroy = simplefb_destroy,
115 .fb_setcolreg = simplefb_setcolreg,
116 };
117
118 static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
119
120 struct simplefb_params {
121 u32 width;
122 u32 height;
123 u32 stride;
124 struct simplefb_format *format;
125 struct resource memory;
126 };
127
128 static int simplefb_parse_dt(struct platform_device *pdev,
129 struct simplefb_params *params)
130 {
131 struct device_node *np = pdev->dev.of_node, *mem;
132 int ret;
133 const char *format;
134 int i;
135
136 ret = of_property_read_u32(np, "width", &params->width);
137 if (ret) {
138 dev_err(&pdev->dev, "Can't parse width property\n");
139 return ret;
140 }
141
142 ret = of_property_read_u32(np, "height", &params->height);
143 if (ret) {
144 dev_err(&pdev->dev, "Can't parse height property\n");
145 return ret;
146 }
147
148 ret = of_property_read_u32(np, "stride", &params->stride);
149 if (ret) {
150 dev_err(&pdev->dev, "Can't parse stride property\n");
151 return ret;
152 }
153
154 ret = of_property_read_string(np, "format", &format);
155 if (ret) {
156 dev_err(&pdev->dev, "Can't parse format property\n");
157 return ret;
158 }
159 params->format = NULL;
160 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
161 if (strcmp(format, simplefb_formats[i].name))
162 continue;
163 params->format = &simplefb_formats[i];
164 break;
165 }
166 if (!params->format) {
167 dev_err(&pdev->dev, "Invalid format value\n");
168 return -EINVAL;
169 }
170
171 mem = of_parse_phandle(np, "memory-region", 0);
172 if (mem) {
173 ret = of_address_to_resource(mem, 0, &params->memory);
174 if (ret < 0) {
175 dev_err(&pdev->dev, "failed to parse memory-region\n");
176 of_node_put(mem);
177 return ret;
178 }
179
180 if (of_property_present(np, "reg"))
181 dev_warn(&pdev->dev, "preferring \"memory-region\" over \"reg\" property\n");
182
183 of_node_put(mem);
184 } else {
185 memset(&params->memory, 0, sizeof(params->memory));
186 }
187
188 return 0;
189 }
190
191 static int simplefb_parse_pd(struct platform_device *pdev,
192 struct simplefb_params *params)
193 {
194 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
195 int i;
196
197 params->width = pd->width;
198 params->height = pd->height;
199 params->stride = pd->stride;
200
201 params->format = NULL;
202 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
203 if (strcmp(pd->format, simplefb_formats[i].name))
204 continue;
205
206 params->format = &simplefb_formats[i];
207 break;
208 }
209
210 if (!params->format) {
211 dev_err(&pdev->dev, "Invalid format value\n");
212 return -EINVAL;
213 }
214
215 memset(&params->memory, 0, sizeof(params->memory));
216
217 return 0;
218 }
219
220 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
221 /*
222 * Clock handling code.
223 *
224 * Here we handle the clocks property of our "simple-framebuffer" dt node.
225 * This is necessary so that we can make sure that any clocks needed by
226 * the display engine that the bootloader set up for us (and for which it
227 * provided a simplefb dt node), stay up, for the life of the simplefb
228 * driver.
229 *
230 * When the driver unloads, we cleanly disable, and then release the clocks.
231 *
232 * We only complain about errors here, no action is taken as the most likely
233 * error can only happen due to a mismatch between the bootloader which set
234 * up simplefb, and the clock definitions in the device tree. Chances are
235 * that there are no adverse effects, and if there are, a clean teardown of
236 * the fb probe will not help us much either. So just complain and carry on,
237 * and hope that the user actually gets a working fb at the end of things.
238 */
239 static int simplefb_clocks_get(struct simplefb_par *par,
240 struct platform_device *pdev)
241 {
242 struct device_node *np = pdev->dev.of_node;
243 struct clk *clock;
244 int i;
245
246 if (dev_get_platdata(&pdev->dev) || !np)
247 return 0;
248
249 par->clk_count = of_clk_get_parent_count(np);
250 if (!par->clk_count)
251 return 0;
252
253 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
254 if (!par->clks)
255 return -ENOMEM;
256
257 for (i = 0; i < par->clk_count; i++) {
258 clock = of_clk_get(np, i);
259 if (IS_ERR(clock)) {
260 if (PTR_ERR(clock) == -EPROBE_DEFER) {
261 while (--i >= 0) {
262 clk_put(par->clks[i]);
263 }
264 kfree(par->clks);
265 return -EPROBE_DEFER;
266 }
267 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
268 __func__, i, PTR_ERR(clock));
269 continue;
270 }
271 par->clks[i] = clock;
272 }
273
274 return 0;
275 }
276
277 static void simplefb_clocks_enable(struct simplefb_par *par,
278 struct platform_device *pdev)
279 {
280 int i, ret;
281
282 for (i = 0; i < par->clk_count; i++) {
283 if (par->clks[i]) {
284 ret = clk_prepare_enable(par->clks[i]);
285 if (ret) {
286 dev_err(&pdev->dev,
287 "%s: failed to enable clock %d: %d\n",
288 __func__, i, ret);
289 clk_put(par->clks[i]);
290 par->clks[i] = NULL;
291 }
292 }
293 }
294 par->clks_enabled = true;
295 }
296
297 static void simplefb_clocks_destroy(struct simplefb_par *par)
298 {
299 int i;
300
301 if (!par->clks)
302 return;
303
304 for (i = 0; i < par->clk_count; i++) {
305 if (par->clks[i]) {
306 if (par->clks_enabled)
307 clk_disable_unprepare(par->clks[i]);
308 clk_put(par->clks[i]);
309 }
310 }
311
312 kfree(par->clks);
313 }
314 #else
315 static int simplefb_clocks_get(struct simplefb_par *par,
316 struct platform_device *pdev) { return 0; }
317 static void simplefb_clocks_enable(struct simplefb_par *par,
318 struct platform_device *pdev) { }
319 static void simplefb_clocks_destroy(struct simplefb_par *par) { }
320 #endif
321
322 #if defined CONFIG_OF && defined CONFIG_REGULATOR
323
324 #define SUPPLY_SUFFIX "-supply"
325
326 /*
327 * Regulator handling code.
328 *
329 * Here we handle the num-supplies and vin*-supply properties of our
330 * "simple-framebuffer" dt node. This is necessary so that we can make sure
331 * that any regulators needed by the display hardware that the bootloader
332 * set up for us (and for which it provided a simplefb dt node), stay up,
333 * for the life of the simplefb driver.
334 *
335 * When the driver unloads, we cleanly disable, and then release the
336 * regulators.
337 *
338 * We only complain about errors here, no action is taken as the most likely
339 * error can only happen due to a mismatch between the bootloader which set
340 * up simplefb, and the regulator definitions in the device tree. Chances are
341 * that there are no adverse effects, and if there are, a clean teardown of
342 * the fb probe will not help us much either. So just complain and carry on,
343 * and hope that the user actually gets a working fb at the end of things.
344 */
345 static int simplefb_regulators_get(struct simplefb_par *par,
346 struct platform_device *pdev)
347 {
348 struct device_node *np = pdev->dev.of_node;
349 struct property *prop;
350 struct regulator *regulator;
351 const char *p;
352 int count = 0, i = 0;
353
354 if (dev_get_platdata(&pdev->dev) || !np)
355 return 0;
356
357 /* Count the number of regulator supplies */
358 for_each_property_of_node(np, prop) {
359 p = strstr(prop->name, SUPPLY_SUFFIX);
360 if (p && p != prop->name)
361 count++;
362 }
363
364 if (!count)
365 return 0;
366
367 par->regulators = devm_kcalloc(&pdev->dev, count,
368 sizeof(struct regulator *), GFP_KERNEL);
369 if (!par->regulators)
370 return -ENOMEM;
371
372 /* Get all the regulators */
373 for_each_property_of_node(np, prop) {
374 char name[32]; /* 32 is max size of property name */
375
376 p = strstr(prop->name, SUPPLY_SUFFIX);
377 if (!p || p == prop->name)
378 continue;
379
380 strscpy(name, prop->name,
381 strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
382 regulator = devm_regulator_get_optional(&pdev->dev, name);
383 if (IS_ERR(regulator)) {
384 if (PTR_ERR(regulator) == -EPROBE_DEFER)
385 return -EPROBE_DEFER;
386 dev_err(&pdev->dev, "regulator %s not found: %ld\n",
387 name, PTR_ERR(regulator));
388 continue;
389 }
390 par->regulators[i++] = regulator;
391 }
392 par->regulator_count = i;
393
394 return 0;
395 }
396
397 static void simplefb_regulators_enable(struct simplefb_par *par,
398 struct platform_device *pdev)
399 {
400 int i, ret;
401
402 /* Enable all the regulators */
403 for (i = 0; i < par->regulator_count; i++) {
404 ret = regulator_enable(par->regulators[i]);
405 if (ret) {
406 dev_err(&pdev->dev,
407 "failed to enable regulator %d: %d\n",
408 i, ret);
409 devm_regulator_put(par->regulators[i]);
410 par->regulators[i] = NULL;
411 }
412 }
413 par->regulators_enabled = true;
414 }
415
416 static void simplefb_regulators_destroy(struct simplefb_par *par)
417 {
418 int i;
419
420 if (!par->regulators || !par->regulators_enabled)
421 return;
422
423 for (i = 0; i < par->regulator_count; i++)
424 if (par->regulators[i])
425 regulator_disable(par->regulators[i]);
426 }
427 #else
428 static int simplefb_regulators_get(struct simplefb_par *par,
429 struct platform_device *pdev) { return 0; }
430 static void simplefb_regulators_enable(struct simplefb_par *par,
431 struct platform_device *pdev) { }
432 static void simplefb_regulators_destroy(struct simplefb_par *par) { }
433 #endif
434
435 static int simplefb_probe(struct platform_device *pdev)
436 {
437 int ret;
438 struct simplefb_params params;
439 struct fb_info *info;
440 struct simplefb_par *par;
441 struct resource *res, *mem;
442
443 if (fb_get_options("simplefb", NULL))
444 return -ENODEV;
445
446 ret = -ENODEV;
447 if (dev_get_platdata(&pdev->dev))
448 ret = simplefb_parse_pd(pdev, &params);
449 else if (pdev->dev.of_node)
450 ret = simplefb_parse_dt(pdev, &params);
451
452 if (ret)
453 return ret;
454
455 if (params.memory.start == 0 && params.memory.end == 0) {
456 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
457 if (!res) {
458 dev_err(&pdev->dev, "No memory resource\n");
459 return -EINVAL;
460 }
461 } else {
462 res = &params.memory;
463 }
464
465 mem = request_mem_region(res->start, resource_size(res), "simplefb");
466 if (!mem) {
467 /*
468 * We cannot make this fatal. Sometimes this comes from magic
469 * spaces our resource handlers simply don't know about. Use
470 * the I/O-memory resource as-is and try to map that instead.
471 */
472 dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res);
473 mem = res;
474 }
475
476 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
477 if (!info) {
478 ret = -ENOMEM;
479 goto error_release_mem_region;
480 }
481 platform_set_drvdata(pdev, info);
482
483 par = info->par;
484
485 info->fix = simplefb_fix;
486 info->fix.smem_start = mem->start;
487 info->fix.smem_len = resource_size(mem);
488 info->fix.line_length = params.stride;
489
490 info->var = simplefb_var;
491 info->var.xres = params.width;
492 info->var.yres = params.height;
493 info->var.xres_virtual = params.width;
494 info->var.yres_virtual = params.height;
495 info->var.bits_per_pixel = params.format->bits_per_pixel;
496 info->var.red = params.format->red;
497 info->var.green = params.format->green;
498 info->var.blue = params.format->blue;
499 info->var.transp = params.format->transp;
500
501 par->base = info->fix.smem_start;
502 par->size = info->fix.smem_len;
503
504 info->fbops = &simplefb_ops;
505 info->screen_base = ioremap_wc(info->fix.smem_start,
506 info->fix.smem_len);
507 if (!info->screen_base) {
508 ret = -ENOMEM;
509 goto error_fb_release;
510 }
511 info->pseudo_palette = par->palette;
512
513 ret = simplefb_clocks_get(par, pdev);
514 if (ret < 0)
515 goto error_unmap;
516
517 ret = simplefb_regulators_get(par, pdev);
518 if (ret < 0)
519 goto error_clocks;
520
521 simplefb_clocks_enable(par, pdev);
522 simplefb_regulators_enable(par, pdev);
523
524 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n",
525 info->fix.smem_start, info->fix.smem_len);
526 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
527 params.format->name,
528 info->var.xres, info->var.yres,
529 info->var.bits_per_pixel, info->fix.line_length);
530
531 if (mem != res)
532 par->mem = mem; /* release in clean-up handler */
533
534 ret = devm_aperture_acquire_for_platform_device(pdev, par->base, par->size);
535 if (ret) {
536 dev_err(&pdev->dev, "Unable to acquire aperture: %d\n", ret);
537 goto error_regulators;
538 }
539 ret = register_framebuffer(info);
540 if (ret < 0) {
541 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
542 goto error_regulators;
543 }
544
545 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
546
547 return 0;
548
549 error_regulators:
550 simplefb_regulators_destroy(par);
551 error_clocks:
552 simplefb_clocks_destroy(par);
553 error_unmap:
554 iounmap(info->screen_base);
555 error_fb_release:
556 framebuffer_release(info);
557 error_release_mem_region:
558 if (mem != res)
559 release_mem_region(mem->start, resource_size(mem));
560 return ret;
561 }
562
563 static void simplefb_remove(struct platform_device *pdev)
564 {
565 struct fb_info *info = platform_get_drvdata(pdev);
566
567 /* simplefb_destroy takes care of info cleanup */
568 unregister_framebuffer(info);
569 }
570
571 static const struct of_device_id simplefb_of_match[] = {
572 { .compatible = "simple-framebuffer", },
573 { },
574 };
575 MODULE_DEVICE_TABLE(of, simplefb_of_match);
576
577 static struct platform_driver simplefb_driver = {
578 .driver = {
579 .name = "simple-framebuffer",
580 .of_match_table = simplefb_of_match,
581 },
582 .probe = simplefb_probe,
583 .remove_new = simplefb_remove,
584 };
585
586 module_platform_driver(simplefb_driver);
587
588 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
589 MODULE_DESCRIPTION("Simple framebuffer driver");
590 MODULE_LICENSE("GPL v2");