blob: d19294635a4296fda9fd0694cc5c98ca0b80e72c [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>
Stephen Warren26549c82013-05-24 15:55:13 -070030
31static struct fb_fix_screeninfo simplefb_fix = {
32 .id = "simple",
33 .type = FB_TYPE_PACKED_PIXELS,
34 .visual = FB_VISUAL_TRUECOLOR,
35 .accel = FB_ACCEL_NONE,
36};
37
38static struct fb_var_screeninfo simplefb_var = {
39 .height = -1,
40 .width = -1,
41 .activate = FB_ACTIVATE_NOW,
42 .vmode = FB_VMODE_NONINTERLACED,
43};
44
Luc Verhaegen1270be42014-11-14 13:26:48 +010045#define PSEUDO_PALETTE_SIZE 16
46
Stephen Warren26549c82013-05-24 15:55:13 -070047static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
48 u_int transp, struct fb_info *info)
49{
50 u32 *pal = info->pseudo_palette;
51 u32 cr = red >> (16 - info->var.red.length);
52 u32 cg = green >> (16 - info->var.green.length);
53 u32 cb = blue >> (16 - info->var.blue.length);
54 u32 value;
55
Luc Verhaegen1270be42014-11-14 13:26:48 +010056 if (regno >= PSEUDO_PALETTE_SIZE)
Stephen Warren26549c82013-05-24 15:55:13 -070057 return -EINVAL;
58
59 value = (cr << info->var.red.offset) |
60 (cg << info->var.green.offset) |
61 (cb << info->var.blue.offset);
62 if (info->var.transp.length > 0) {
63 u32 mask = (1 << info->var.transp.length) - 1;
64 mask <<= info->var.transp.offset;
65 value |= mask;
66 }
67 pal[regno] = value;
68
69 return 0;
70}
71
David Herrmann498f6d32013-10-02 16:58:38 +020072static void simplefb_destroy(struct fb_info *info)
73{
74 if (info->screen_base)
75 iounmap(info->screen_base);
76}
77
Stephen Warren26549c82013-05-24 15:55:13 -070078static struct fb_ops simplefb_ops = {
79 .owner = THIS_MODULE,
David Herrmann498f6d32013-10-02 16:58:38 +020080 .fb_destroy = simplefb_destroy,
Stephen Warren26549c82013-05-24 15:55:13 -070081 .fb_setcolreg = simplefb_setcolreg,
82 .fb_fillrect = cfb_fillrect,
83 .fb_copyarea = cfb_copyarea,
84 .fb_imageblit = cfb_imageblit,
85};
86
David Herrmann5ef76da2013-08-02 14:05:20 +020087static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
Stephen Warren26549c82013-05-24 15:55:13 -070088
89struct simplefb_params {
90 u32 width;
91 u32 height;
92 u32 stride;
93 struct simplefb_format *format;
94};
95
96static int simplefb_parse_dt(struct platform_device *pdev,
97 struct simplefb_params *params)
98{
99 struct device_node *np = pdev->dev.of_node;
100 int ret;
101 const char *format;
102 int i;
103
104 ret = of_property_read_u32(np, "width", &params->width);
105 if (ret) {
106 dev_err(&pdev->dev, "Can't parse width property\n");
107 return ret;
108 }
109
110 ret = of_property_read_u32(np, "height", &params->height);
111 if (ret) {
112 dev_err(&pdev->dev, "Can't parse height property\n");
113 return ret;
114 }
115
116 ret = of_property_read_u32(np, "stride", &params->stride);
117 if (ret) {
118 dev_err(&pdev->dev, "Can't parse stride property\n");
119 return ret;
120 }
121
122 ret = of_property_read_string(np, "format", &format);
123 if (ret) {
124 dev_err(&pdev->dev, "Can't parse format property\n");
125 return ret;
126 }
127 params->format = NULL;
128 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
129 if (strcmp(format, simplefb_formats[i].name))
130 continue;
131 params->format = &simplefb_formats[i];
132 break;
133 }
134 if (!params->format) {
135 dev_err(&pdev->dev, "Invalid format value\n");
136 return -EINVAL;
137 }
138
139 return 0;
140}
141
David Herrmann5ef76da2013-08-02 14:05:20 +0200142static int simplefb_parse_pd(struct platform_device *pdev,
143 struct simplefb_params *params)
144{
Jingoo Han129f1be2013-09-17 14:11:04 +0900145 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
David Herrmann5ef76da2013-08-02 14:05:20 +0200146 int i;
147
148 params->width = pd->width;
149 params->height = pd->height;
150 params->stride = pd->stride;
151
152 params->format = NULL;
153 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
154 if (strcmp(pd->format, simplefb_formats[i].name))
155 continue;
156
157 params->format = &simplefb_formats[i];
158 break;
159 }
160
161 if (!params->format) {
162 dev_err(&pdev->dev, "Invalid format value\n");
163 return -EINVAL;
164 }
165
166 return 0;
167}
168
Luc Verhaegen1270be42014-11-14 13:26:48 +0100169struct simplefb_par {
170 u32 palette[PSEUDO_PALETTE_SIZE];
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100171#ifdef CONFIG_OF
172 int clk_count;
173 struct clk **clks;
174#endif
Luc Verhaegen1270be42014-11-14 13:26:48 +0100175};
176
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100177#ifdef CONFIG_OF
178/*
179 * Clock handling code.
180 *
181 * Here we handle the clocks property of our "simple-framebuffer" dt node.
182 * This is necessary so that we can make sure that any clocks needed by
183 * the display engine that the bootloader set up for us (and for which it
184 * provided a simplefb dt node), stay up, for the life of the simplefb
185 * driver.
186 *
187 * When the driver unloads, we cleanly disable, and then release the clocks.
188 *
189 * We only complain about errors here, no action is taken as the most likely
190 * error can only happen due to a mismatch between the bootloader which set
191 * up simplefb, and the clock definitions in the device tree. Chances are
192 * that there are no adverse effects, and if there are, a clean teardown of
193 * the fb probe will not help us much either. So just complain and carry on,
194 * and hope that the user actually gets a working fb at the end of things.
195 */
196static int simplefb_clocks_init(struct simplefb_par *par,
197 struct platform_device *pdev)
198{
199 struct device_node *np = pdev->dev.of_node;
200 struct clk *clock;
201 int i, ret;
202
203 if (dev_get_platdata(&pdev->dev) || !np)
204 return 0;
205
206 par->clk_count = of_clk_get_parent_count(np);
207 if (par->clk_count <= 0)
208 return 0;
209
210 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
211 if (!par->clks)
212 return -ENOMEM;
213
214 for (i = 0; i < par->clk_count; i++) {
215 clock = of_clk_get(np, i);
216 if (IS_ERR(clock)) {
217 if (PTR_ERR(clock) == -EPROBE_DEFER) {
218 while (--i >= 0) {
219 if (par->clks[i])
220 clk_put(par->clks[i]);
221 }
222 kfree(par->clks);
223 return -EPROBE_DEFER;
224 }
225 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
226 __func__, i, PTR_ERR(clock));
227 continue;
228 }
229 par->clks[i] = clock;
230 }
231
232 for (i = 0; i < par->clk_count; i++) {
233 if (par->clks[i]) {
234 ret = clk_prepare_enable(par->clks[i]);
235 if (ret) {
236 dev_err(&pdev->dev,
237 "%s: failed to enable clock %d: %d\n",
238 __func__, i, ret);
239 clk_put(par->clks[i]);
240 par->clks[i] = NULL;
241 }
242 }
243 }
244
245 return 0;
246}
247
248static void simplefb_clocks_destroy(struct simplefb_par *par)
249{
250 int i;
251
252 if (!par->clks)
253 return;
254
255 for (i = 0; i < par->clk_count; i++) {
256 if (par->clks[i]) {
257 clk_disable_unprepare(par->clks[i]);
258 clk_put(par->clks[i]);
259 }
260 }
261
262 kfree(par->clks);
263}
264#else
265static int simplefb_clocks_init(struct simplefb_par *par,
266 struct platform_device *pdev) { return 0; }
267static void simplefb_clocks_destroy(struct simplefb_par *par) { }
268#endif
269
Stephen Warren26549c82013-05-24 15:55:13 -0700270static int simplefb_probe(struct platform_device *pdev)
271{
272 int ret;
273 struct simplefb_params params;
274 struct fb_info *info;
Luc Verhaegen1270be42014-11-14 13:26:48 +0100275 struct simplefb_par *par;
Stephen Warren26549c82013-05-24 15:55:13 -0700276 struct resource *mem;
277
278 if (fb_get_options("simplefb", NULL))
279 return -ENODEV;
280
David Herrmann5ef76da2013-08-02 14:05:20 +0200281 ret = -ENODEV;
Jingoo Han129f1be2013-09-17 14:11:04 +0900282 if (dev_get_platdata(&pdev->dev))
David Herrmann5ef76da2013-08-02 14:05:20 +0200283 ret = simplefb_parse_pd(pdev, &params);
284 else if (pdev->dev.of_node)
285 ret = simplefb_parse_dt(pdev, &params);
286
Stephen Warren26549c82013-05-24 15:55:13 -0700287 if (ret)
288 return ret;
289
290 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
291 if (!mem) {
292 dev_err(&pdev->dev, "No memory resource\n");
293 return -EINVAL;
294 }
295
Luc Verhaegen1270be42014-11-14 13:26:48 +0100296 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
Stephen Warren26549c82013-05-24 15:55:13 -0700297 if (!info)
298 return -ENOMEM;
299 platform_set_drvdata(pdev, info);
300
Luc Verhaegen1270be42014-11-14 13:26:48 +0100301 par = info->par;
302
Stephen Warren26549c82013-05-24 15:55:13 -0700303 info->fix = simplefb_fix;
304 info->fix.smem_start = mem->start;
305 info->fix.smem_len = resource_size(mem);
306 info->fix.line_length = params.stride;
307
308 info->var = simplefb_var;
309 info->var.xres = params.width;
310 info->var.yres = params.height;
311 info->var.xres_virtual = params.width;
312 info->var.yres_virtual = params.height;
313 info->var.bits_per_pixel = params.format->bits_per_pixel;
314 info->var.red = params.format->red;
315 info->var.green = params.format->green;
316 info->var.blue = params.format->blue;
317 info->var.transp = params.format->transp;
318
David Herrmanndf0960a2013-08-02 14:05:21 +0200319 info->apertures = alloc_apertures(1);
320 if (!info->apertures) {
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100321 ret = -ENOMEM;
322 goto error_fb_release;
David Herrmanndf0960a2013-08-02 14:05:21 +0200323 }
324 info->apertures->ranges[0].base = info->fix.smem_start;
325 info->apertures->ranges[0].size = info->fix.smem_len;
326
Stephen Warren26549c82013-05-24 15:55:13 -0700327 info->fbops = &simplefb_ops;
David Herrmanndf0960a2013-08-02 14:05:21 +0200328 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
David Herrmann9e210be2013-10-02 16:58:39 +0200329 info->screen_base = ioremap_wc(info->fix.smem_start,
330 info->fix.smem_len);
Stephen Warren26549c82013-05-24 15:55:13 -0700331 if (!info->screen_base) {
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100332 ret = -ENOMEM;
333 goto error_fb_release;
Stephen Warren26549c82013-05-24 15:55:13 -0700334 }
Luc Verhaegen1270be42014-11-14 13:26:48 +0100335 info->pseudo_palette = par->palette;
Stephen Warren26549c82013-05-24 15:55:13 -0700336
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100337 ret = simplefb_clocks_init(par, pdev);
338 if (ret < 0)
339 goto error_unmap;
340
Tom Gundersen9f192a92013-09-07 16:08:35 +0200341 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
342 info->fix.smem_start, info->fix.smem_len,
343 info->screen_base);
344 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
345 params.format->name,
346 info->var.xres, info->var.yres,
347 info->var.bits_per_pixel, info->fix.line_length);
348
Stephen Warren26549c82013-05-24 15:55:13 -0700349 ret = register_framebuffer(info);
350 if (ret < 0) {
351 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100352 goto error_clocks;
Stephen Warren26549c82013-05-24 15:55:13 -0700353 }
354
355 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
356
357 return 0;
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100358
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100359error_clocks:
360 simplefb_clocks_destroy(par);
Luc Verhaegenbf2fda12014-11-14 13:26:49 +0100361error_unmap:
362 iounmap(info->screen_base);
363error_fb_release:
364 framebuffer_release(info);
365 return ret;
Stephen Warren26549c82013-05-24 15:55:13 -0700366}
367
368static int simplefb_remove(struct platform_device *pdev)
369{
370 struct fb_info *info = platform_get_drvdata(pdev);
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100371 struct simplefb_par *par = info->par;
Stephen Warren26549c82013-05-24 15:55:13 -0700372
373 unregister_framebuffer(info);
Luc Verhaegenfc219bf2014-11-14 13:26:50 +0100374 simplefb_clocks_destroy(par);
Stephen Warren26549c82013-05-24 15:55:13 -0700375 framebuffer_release(info);
376
377 return 0;
378}
379
380static const struct of_device_id simplefb_of_match[] = {
381 { .compatible = "simple-framebuffer", },
382 { },
383};
384MODULE_DEVICE_TABLE(of, simplefb_of_match);
385
386static struct platform_driver simplefb_driver = {
387 .driver = {
388 .name = "simple-framebuffer",
389 .owner = THIS_MODULE,
390 .of_match_table = simplefb_of_match,
391 },
392 .probe = simplefb_probe,
393 .remove = simplefb_remove,
394};
395module_platform_driver(simplefb_driver);
396
397MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
398MODULE_DESCRIPTION("Simple framebuffer driver");
399MODULE_LICENSE("GPL v2");