blob: b2ae9254fd75563ebe4f24686f96b1b3e2e74020 [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>
Luc Verhaegenfc219bf2014-11-14 13:26:50 +010029#include <linux/clk-provider.h>
Hans de Goede89c95002014-11-14 13:26:53 +010030#include <linux/of_platform.h>
Stephen Warren26549c82013-05-24 15:55:13 -070031
32static struct fb_fix_screeninfo simplefb_fix = {
33 .id = "simple",
34 .type = FB_TYPE_PACKED_PIXELS,
35 .visual = FB_VISUAL_TRUECOLOR,
36 .accel = FB_ACCEL_NONE,
37};
38
39static struct fb_var_screeninfo simplefb_var = {
40 .height = -1,
41 .width = -1,
42 .activate = FB_ACTIVATE_NOW,
43 .vmode = FB_VMODE_NONINTERLACED,
44};
45
Luc Verhaegen1270be42014-11-14 13:26:48 +010046#define PSEUDO_PALETTE_SIZE 16
47
Stephen Warren26549c82013-05-24 15:55:13 -070048static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
49 u_int transp, struct fb_info *info)
50{
51 u32 *pal = info->pseudo_palette;
52 u32 cr = red >> (16 - info->var.red.length);
53 u32 cg = green >> (16 - info->var.green.length);
54 u32 cb = blue >> (16 - info->var.blue.length);
55 u32 value;
56
Luc Verhaegen1270be42014-11-14 13:26:48 +010057 if (regno >= PSEUDO_PALETTE_SIZE)
Stephen Warren26549c82013-05-24 15:55:13 -070058 return -EINVAL;
59
60 value = (cr << info->var.red.offset) |
61 (cg << info->var.green.offset) |
62 (cb << info->var.blue.offset);
63 if (info->var.transp.length > 0) {
64 u32 mask = (1 << info->var.transp.length) - 1;
65 mask <<= info->var.transp.offset;
66 value |= mask;
67 }
68 pal[regno] = value;
69
70 return 0;
71}
72
David Herrmann498f6d32013-10-02 16:58:38 +020073static void simplefb_destroy(struct fb_info *info)
74{
75 if (info->screen_base)
76 iounmap(info->screen_base);
77}
78
Stephen Warren26549c82013-05-24 15:55:13 -070079static struct fb_ops simplefb_ops = {
80 .owner = THIS_MODULE,
David Herrmann498f6d32013-10-02 16:58:38 +020081 .fb_destroy = simplefb_destroy,
Stephen Warren26549c82013-05-24 15:55:13 -070082 .fb_setcolreg = simplefb_setcolreg,
83 .fb_fillrect = cfb_fillrect,
84 .fb_copyarea = cfb_copyarea,
85 .fb_imageblit = cfb_imageblit,
86};
87
David Herrmann5ef76da2013-08-02 14:05:20 +020088static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
Stephen Warren26549c82013-05-24 15:55:13 -070089
90struct simplefb_params {
91 u32 width;
92 u32 height;
93 u32 stride;
94 struct simplefb_format *format;
95};
96
97static int simplefb_parse_dt(struct platform_device *pdev,
98 struct simplefb_params *params)
99{
100 struct device_node *np = pdev->dev.of_node;
101 int ret;
102 const char *format;
103 int i;
104
105 ret = of_property_read_u32(np, "width", &params->width);
106 if (ret) {
107 dev_err(&pdev->dev, "Can't parse width property\n");
108 return ret;
109 }
110
111 ret = of_property_read_u32(np, "height", &params->height);
112 if (ret) {
113 dev_err(&pdev->dev, "Can't parse height property\n");
114 return ret;
115 }
116
117 ret = of_property_read_u32(np, "stride", &params->stride);
118 if (ret) {
119 dev_err(&pdev->dev, "Can't parse stride property\n");
120 return ret;
121 }
122
123 ret = of_property_read_string(np, "format", &format);
124 if (ret) {
125 dev_err(&pdev->dev, "Can't parse format property\n");
126 return ret;
127 }
128 params->format = NULL;
129 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
130 if (strcmp(format, simplefb_formats[i].name))
131 continue;
132 params->format = &simplefb_formats[i];
133 break;
134 }
135 if (!params->format) {
136 dev_err(&pdev->dev, "Invalid format value\n");
137 return -EINVAL;
138 }
139
140 return 0;
141}
142
David Herrmann5ef76da2013-08-02 14:05:20 +0200143static int simplefb_parse_pd(struct platform_device *pdev,
144 struct simplefb_params *params)
145{
Jingoo Han129f1be2013-09-17 14:11:04 +0900146 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
David Herrmann5ef76da2013-08-02 14:05:20 +0200147 int i;
148
149 params->width = pd->width;
150 params->height = pd->height;
151 params->stride = pd->stride;
152
153 params->format = NULL;
154 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
155 if (strcmp(pd->format, simplefb_formats[i].name))
156 continue;
157
158 params->format = &simplefb_formats[i];
159 break;
160 }
161
162 if (!params->format) {
163 dev_err(&pdev->dev, "Invalid format value\n");
164 return -EINVAL;
165 }
166
167 return 0;
168}
169
Luc Verhaegen1270be42014-11-14 13:26:48 +0100170struct simplefb_par {
171 u32 palette[PSEUDO_PALETTE_SIZE];
Hans de Goede82847312014-11-25 12:13:30 +0100172#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100173 int clk_count;
174 struct clk **clks;
175#endif
Luc Verhaegen1270be42014-11-14 13:26:48 +0100176};
177
Hans de Goede82847312014-11-25 12:13:30 +0100178#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100179/*
180 * Clock handling code.
181 *
182 * Here we handle the clocks property of our "simple-framebuffer" dt node.
183 * This is necessary so that we can make sure that any clocks needed by
184 * the display engine that the bootloader set up for us (and for which it
185 * provided a simplefb dt node), stay up, for the life of the simplefb
186 * driver.
187 *
188 * When the driver unloads, we cleanly disable, and then release the clocks.
189 *
190 * We only complain about errors here, no action is taken as the most likely
191 * error can only happen due to a mismatch between the bootloader which set
192 * up simplefb, and the clock definitions in the device tree. Chances are
193 * that there are no adverse effects, and if there are, a clean teardown of
194 * the fb probe will not help us much either. So just complain and carry on,
195 * and hope that the user actually gets a working fb at the end of things.
196 */
197static int simplefb_clocks_init(struct simplefb_par *par,
198 struct platform_device *pdev)
199{
200 struct device_node *np = pdev->dev.of_node;
201 struct clk *clock;
202 int i, ret;
203
204 if (dev_get_platdata(&pdev->dev) || !np)
205 return 0;
206
207 par->clk_count = of_clk_get_parent_count(np);
208 if (par->clk_count <= 0)
209 return 0;
210
211 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
212 if (!par->clks)
213 return -ENOMEM;
214
215 for (i = 0; i < par->clk_count; i++) {
216 clock = of_clk_get(np, i);
217 if (IS_ERR(clock)) {
218 if (PTR_ERR(clock) == -EPROBE_DEFER) {
219 while (--i >= 0) {
220 if (par->clks[i])
221 clk_put(par->clks[i]);
222 }
223 kfree(par->clks);
224 return -EPROBE_DEFER;
225 }
226 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
227 __func__, i, PTR_ERR(clock));
228 continue;
229 }
230 par->clks[i] = clock;
231 }
232
233 for (i = 0; i < par->clk_count; i++) {
234 if (par->clks[i]) {
235 ret = clk_prepare_enable(par->clks[i]);
236 if (ret) {
237 dev_err(&pdev->dev,
238 "%s: failed to enable clock %d: %d\n",
239 __func__, i, ret);
240 clk_put(par->clks[i]);
241 par->clks[i] = NULL;
242 }
243 }
244 }
245
246 return 0;
247}
248
249static void simplefb_clocks_destroy(struct simplefb_par *par)
250{
251 int i;
252
253 if (!par->clks)
254 return;
255
256 for (i = 0; i < par->clk_count; i++) {
257 if (par->clks[i]) {
258 clk_disable_unprepare(par->clks[i]);
259 clk_put(par->clks[i]);
260 }
261 }
262
263 kfree(par->clks);
264}
265#else
266static int simplefb_clocks_init(struct simplefb_par *par,
267 struct platform_device *pdev) { return 0; }
268static void simplefb_clocks_destroy(struct simplefb_par *par) { }
269#endif
270
Stephen Warren26549c82013-05-24 15:55:13 -0700271static int simplefb_probe(struct platform_device *pdev)
272{
273 int ret;
274 struct simplefb_params params;
275 struct fb_info *info;
Luc Verhaegen1270be42014-11-14 13:26:48 +0100276 struct simplefb_par *par;
Stephen Warren26549c82013-05-24 15:55:13 -0700277 struct resource *mem;
278
279 if (fb_get_options("simplefb", NULL))
280 return -ENODEV;
281
David Herrmann5ef76da2013-08-02 14:05:20 +0200282 ret = -ENODEV;
Jingoo Han129f1be2013-09-17 14:11:04 +0900283 if (dev_get_platdata(&pdev->dev))
David Herrmann5ef76da2013-08-02 14:05:20 +0200284 ret = simplefb_parse_pd(pdev, &params);
285 else if (pdev->dev.of_node)
286 ret = simplefb_parse_dt(pdev, &params);
287
Stephen Warren26549c82013-05-24 15:55:13 -0700288 if (ret)
289 return ret;
290
291 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
292 if (!mem) {
293 dev_err(&pdev->dev, "No memory resource\n");
294 return -EINVAL;
295 }
296
Luc Verhaegen1270be42014-11-14 13:26:48 +0100297 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
Stephen Warren26549c82013-05-24 15:55:13 -0700298 if (!info)
299 return -ENOMEM;
300 platform_set_drvdata(pdev, info);
301
Luc Verhaegen1270be42014-11-14 13:26:48 +0100302 par = info->par;
303
Stephen Warren26549c82013-05-24 15:55:13 -0700304 info->fix = simplefb_fix;
305 info->fix.smem_start = mem->start;
306 info->fix.smem_len = resource_size(mem);
307 info->fix.line_length = params.stride;
308
309 info->var = simplefb_var;
310 info->var.xres = params.width;
311 info->var.yres = params.height;
312 info->var.xres_virtual = params.width;
313 info->var.yres_virtual = params.height;
314 info->var.bits_per_pixel = params.format->bits_per_pixel;
315 info->var.red = params.format->red;
316 info->var.green = params.format->green;
317 info->var.blue = params.format->blue;
318 info->var.transp = params.format->transp;
319
David Herrmanndf0960a2013-08-02 14:05:21 +0200320 info->apertures = alloc_apertures(1);
321 if (!info->apertures) {
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100322 ret = -ENOMEM;
323 goto error_fb_release;
David Herrmanndf0960a2013-08-02 14:05:21 +0200324 }
325 info->apertures->ranges[0].base = info->fix.smem_start;
326 info->apertures->ranges[0].size = info->fix.smem_len;
327
Stephen Warren26549c82013-05-24 15:55:13 -0700328 info->fbops = &simplefb_ops;
David Herrmanndf0960a2013-08-02 14:05:21 +0200329 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
David Herrmann9e210be2013-10-02 16:58:39 +0200330 info->screen_base = ioremap_wc(info->fix.smem_start,
331 info->fix.smem_len);
Stephen Warren26549c82013-05-24 15:55:13 -0700332 if (!info->screen_base) {
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100333 ret = -ENOMEM;
334 goto error_fb_release;
Stephen Warren26549c82013-05-24 15:55:13 -0700335 }
Luc Verhaegen1270be42014-11-14 13:26:48 +0100336 info->pseudo_palette = par->palette;
Stephen Warren26549c82013-05-24 15:55:13 -0700337
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100338 ret = simplefb_clocks_init(par, pdev);
339 if (ret < 0)
340 goto error_unmap;
341
Tom Gundersen9f192a92013-09-07 16:08:35 +0200342 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
343 info->fix.smem_start, info->fix.smem_len,
344 info->screen_base);
345 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
346 params.format->name,
347 info->var.xres, info->var.yres,
348 info->var.bits_per_pixel, info->fix.line_length);
349
Stephen Warren26549c82013-05-24 15:55:13 -0700350 ret = register_framebuffer(info);
351 if (ret < 0) {
352 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100353 goto error_clocks;
Stephen Warren26549c82013-05-24 15:55:13 -0700354 }
355
356 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
357
358 return 0;
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100359
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100360error_clocks:
361 simplefb_clocks_destroy(par);
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100362error_unmap:
363 iounmap(info->screen_base);
364error_fb_release:
365 framebuffer_release(info);
366 return ret;
Stephen Warren26549c82013-05-24 15:55:13 -0700367}
368
369static int simplefb_remove(struct platform_device *pdev)
370{
371 struct fb_info *info = platform_get_drvdata(pdev);
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100372 struct simplefb_par *par = info->par;
Stephen Warren26549c82013-05-24 15:55:13 -0700373
374 unregister_framebuffer(info);
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100375 simplefb_clocks_destroy(par);
Stephen Warren26549c82013-05-24 15:55:13 -0700376 framebuffer_release(info);
377
378 return 0;
379}
380
381static const struct of_device_id simplefb_of_match[] = {
382 { .compatible = "simple-framebuffer", },
383 { },
384};
385MODULE_DEVICE_TABLE(of, simplefb_of_match);
386
387static struct platform_driver simplefb_driver = {
388 .driver = {
389 .name = "simple-framebuffer",
390 .owner = THIS_MODULE,
391 .of_match_table = simplefb_of_match,
392 },
393 .probe = simplefb_probe,
394 .remove = simplefb_remove,
395};
Hans de Goede89c95002014-11-14 13:26:53 +0100396
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
406 if (IS_ENABLED(CONFIG_OF) && 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);
410 }
411 }
412
413 return 0;
414}
415
Hans de Goede0c5b2402014-11-14 13:26:55 +0100416fs_initcall(simplefb_init);
Stephen Warren26549c82013-05-24 15:55:13 -0700417
418MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
419MODULE_DESCRIPTION("Simple framebuffer driver");
420MODULE_LICENSE("GPL v2");