]>
Commit | Line | Data |
---|---|---|
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> |
fc219bfd | 29 | #include <linux/clk-provider.h> |
89c95001 | 30 | #include <linux/of_platform.h> |
26549c8d SW |
31 | |
32 | static 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 | ||
39 | static struct fb_var_screeninfo simplefb_var = { | |
40 | .height = -1, | |
41 | .width = -1, | |
42 | .activate = FB_ACTIVATE_NOW, | |
43 | .vmode = FB_VMODE_NONINTERLACED, | |
44 | }; | |
45 | ||
1270be4a LV |
46 | #define PSEUDO_PALETTE_SIZE 16 |
47 | ||
26549c8d SW |
48 | static 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 | ||
1270be4a | 57 | if (regno >= PSEUDO_PALETTE_SIZE) |
26549c8d SW |
58 | 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 | ||
498f6d36 DH |
73 | static void simplefb_destroy(struct fb_info *info) |
74 | { | |
75 | if (info->screen_base) | |
76 | iounmap(info->screen_base); | |
77 | } | |
78 | ||
26549c8d SW |
79 | static struct fb_ops simplefb_ops = { |
80 | .owner = THIS_MODULE, | |
498f6d36 | 81 | .fb_destroy = simplefb_destroy, |
26549c8d SW |
82 | .fb_setcolreg = simplefb_setcolreg, |
83 | .fb_fillrect = cfb_fillrect, | |
84 | .fb_copyarea = cfb_copyarea, | |
85 | .fb_imageblit = cfb_imageblit, | |
86 | }; | |
87 | ||
5ef76da6 | 88 | static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS; |
26549c8d SW |
89 | |
90 | struct simplefb_params { | |
91 | u32 width; | |
92 | u32 height; | |
93 | u32 stride; | |
94 | struct simplefb_format *format; | |
95 | }; | |
96 | ||
97 | static 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", ¶ms->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", ¶ms->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", ¶ms->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 | ||
5ef76da6 DH |
143 | static int simplefb_parse_pd(struct platform_device *pdev, |
144 | struct simplefb_params *params) | |
145 | { | |
129f1be4 | 146 | struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev); |
5ef76da6 DH |
147 | 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 | ||
1270be4a LV |
170 | struct simplefb_par { |
171 | u32 palette[PSEUDO_PALETTE_SIZE]; | |
8284731e | 172 | #if defined CONFIG_OF && defined CONFIG_COMMON_CLK |
fc219bfd LV |
173 | int clk_count; |
174 | struct clk **clks; | |
175 | #endif | |
1270be4a LV |
176 | }; |
177 | ||
8284731e | 178 | #if defined CONFIG_OF && defined CONFIG_COMMON_CLK |
fc219bfd LV |
179 | /* |
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 | */ | |
197 | static 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 | ||
249 | static 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 | |
266 | static int simplefb_clocks_init(struct simplefb_par *par, | |
267 | struct platform_device *pdev) { return 0; } | |
268 | static void simplefb_clocks_destroy(struct simplefb_par *par) { } | |
269 | #endif | |
270 | ||
26549c8d SW |
271 | static int simplefb_probe(struct platform_device *pdev) |
272 | { | |
273 | int ret; | |
274 | struct simplefb_params params; | |
275 | struct fb_info *info; | |
1270be4a | 276 | struct simplefb_par *par; |
26549c8d SW |
277 | struct resource *mem; |
278 | ||
279 | if (fb_get_options("simplefb", NULL)) | |
280 | return -ENODEV; | |
281 | ||
5ef76da6 | 282 | ret = -ENODEV; |
129f1be4 | 283 | if (dev_get_platdata(&pdev->dev)) |
5ef76da6 DH |
284 | ret = simplefb_parse_pd(pdev, ¶ms); |
285 | else if (pdev->dev.of_node) | |
286 | ret = simplefb_parse_dt(pdev, ¶ms); | |
287 | ||
26549c8d SW |
288 | 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 | ||
1270be4a | 297 | info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev); |
26549c8d SW |
298 | if (!info) |
299 | return -ENOMEM; | |
300 | platform_set_drvdata(pdev, info); | |
301 | ||
1270be4a LV |
302 | par = info->par; |
303 | ||
26549c8d SW |
304 | 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 | ||
df0960ab DH |
320 | info->apertures = alloc_apertures(1); |
321 | if (!info->apertures) { | |
bf2fda15 LV |
322 | ret = -ENOMEM; |
323 | goto error_fb_release; | |
df0960ab DH |
324 | } |
325 | info->apertures->ranges[0].base = info->fix.smem_start; | |
326 | info->apertures->ranges[0].size = info->fix.smem_len; | |
327 | ||
26549c8d | 328 | info->fbops = &simplefb_ops; |
df0960ab | 329 | info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE; |
9e210be6 DH |
330 | info->screen_base = ioremap_wc(info->fix.smem_start, |
331 | info->fix.smem_len); | |
26549c8d | 332 | if (!info->screen_base) { |
bf2fda15 LV |
333 | ret = -ENOMEM; |
334 | goto error_fb_release; | |
26549c8d | 335 | } |
1270be4a | 336 | info->pseudo_palette = par->palette; |
26549c8d | 337 | |
fc219bfd LV |
338 | ret = simplefb_clocks_init(par, pdev); |
339 | if (ret < 0) | |
340 | goto error_unmap; | |
341 | ||
9f192a92 TG |
342 | 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 | ||
26549c8d SW |
350 | ret = register_framebuffer(info); |
351 | if (ret < 0) { | |
352 | dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret); | |
fc219bfd | 353 | goto error_clocks; |
26549c8d SW |
354 | } |
355 | ||
356 | dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node); | |
357 | ||
358 | return 0; | |
bf2fda15 | 359 | |
fc219bfd LV |
360 | error_clocks: |
361 | simplefb_clocks_destroy(par); | |
bf2fda15 LV |
362 | error_unmap: |
363 | iounmap(info->screen_base); | |
364 | error_fb_release: | |
365 | framebuffer_release(info); | |
366 | return ret; | |
26549c8d SW |
367 | } |
368 | ||
369 | static int simplefb_remove(struct platform_device *pdev) | |
370 | { | |
371 | struct fb_info *info = platform_get_drvdata(pdev); | |
fc219bfd | 372 | struct simplefb_par *par = info->par; |
26549c8d SW |
373 | |
374 | unregister_framebuffer(info); | |
fc219bfd | 375 | simplefb_clocks_destroy(par); |
26549c8d SW |
376 | framebuffer_release(info); |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
381 | static const struct of_device_id simplefb_of_match[] = { | |
382 | { .compatible = "simple-framebuffer", }, | |
383 | { }, | |
384 | }; | |
385 | MODULE_DEVICE_TABLE(of, simplefb_of_match); | |
386 | ||
387 | static struct platform_driver simplefb_driver = { | |
388 | .driver = { | |
389 | .name = "simple-framebuffer", | |
26549c8d SW |
390 | .of_match_table = simplefb_of_match, |
391 | }, | |
392 | .probe = simplefb_probe, | |
393 | .remove = simplefb_remove, | |
394 | }; | |
89c95001 HG |
395 | |
396 | static int __init simplefb_init(void) | |
397 | { | |
398 | int ret; | |
399 | struct device_node *np; | |
400 | ||
401 | ret = platform_driver_register(&simplefb_driver); | |
402 | if (ret) | |
403 | return ret; | |
404 | ||
d9e02019 | 405 | if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) { |
89c95001 HG |
406 | for_each_child_of_node(of_chosen, np) { |
407 | if (of_device_is_compatible(np, "simple-framebuffer")) | |
408 | of_platform_device_create(np, NULL, NULL); | |
409 | } | |
410 | } | |
411 | ||
412 | return 0; | |
413 | } | |
414 | ||
0c5b240c | 415 | fs_initcall(simplefb_init); |
26549c8d SW |
416 | |
417 | MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>"); | |
418 | MODULE_DESCRIPTION("Simple framebuffer driver"); | |
419 | MODULE_LICENSE("GPL v2"); |