xref: /openbmc/linux/drivers/video/fbdev/simplefb.c (revision 8c0b9ee8)
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>
27 #include <linux/platform_data/simplefb.h>
28 #include <linux/platform_device.h>
29 #include <linux/clk-provider.h>
30 #include <linux/of_platform.h>
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 
46 #define PSEUDO_PALETTE_SIZE 16
47 
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 
57 	if (regno >= PSEUDO_PALETTE_SIZE)
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 
73 static void simplefb_destroy(struct fb_info *info)
74 {
75 	if (info->screen_base)
76 		iounmap(info->screen_base);
77 }
78 
79 static struct fb_ops simplefb_ops = {
80 	.owner		= THIS_MODULE,
81 	.fb_destroy	= simplefb_destroy,
82 	.fb_setcolreg	= simplefb_setcolreg,
83 	.fb_fillrect	= cfb_fillrect,
84 	.fb_copyarea	= cfb_copyarea,
85 	.fb_imageblit	= cfb_imageblit,
86 };
87 
88 static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
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", &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 
143 static int simplefb_parse_pd(struct platform_device *pdev,
144 			     struct simplefb_params *params)
145 {
146 	struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
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 
170 struct simplefb_par {
171 	u32 palette[PSEUDO_PALETTE_SIZE];
172 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
173 	int clk_count;
174 	struct clk **clks;
175 #endif
176 };
177 
178 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
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 
271 static int simplefb_probe(struct platform_device *pdev)
272 {
273 	int ret;
274 	struct simplefb_params params;
275 	struct fb_info *info;
276 	struct simplefb_par *par;
277 	struct resource *mem;
278 
279 	if (fb_get_options("simplefb", NULL))
280 		return -ENODEV;
281 
282 	ret = -ENODEV;
283 	if (dev_get_platdata(&pdev->dev))
284 		ret = simplefb_parse_pd(pdev, &params);
285 	else if (pdev->dev.of_node)
286 		ret = simplefb_parse_dt(pdev, &params);
287 
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 
297 	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
298 	if (!info)
299 		return -ENOMEM;
300 	platform_set_drvdata(pdev, info);
301 
302 	par = info->par;
303 
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 
320 	info->apertures = alloc_apertures(1);
321 	if (!info->apertures) {
322 		ret = -ENOMEM;
323 		goto error_fb_release;
324 	}
325 	info->apertures->ranges[0].base = info->fix.smem_start;
326 	info->apertures->ranges[0].size = info->fix.smem_len;
327 
328 	info->fbops = &simplefb_ops;
329 	info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
330 	info->screen_base = ioremap_wc(info->fix.smem_start,
331 				       info->fix.smem_len);
332 	if (!info->screen_base) {
333 		ret = -ENOMEM;
334 		goto error_fb_release;
335 	}
336 	info->pseudo_palette = par->palette;
337 
338 	ret = simplefb_clocks_init(par, pdev);
339 	if (ret < 0)
340 		goto error_unmap;
341 
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 
350 	ret = register_framebuffer(info);
351 	if (ret < 0) {
352 		dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
353 		goto error_clocks;
354 	}
355 
356 	dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
357 
358 	return 0;
359 
360 error_clocks:
361 	simplefb_clocks_destroy(par);
362 error_unmap:
363 	iounmap(info->screen_base);
364 error_fb_release:
365 	framebuffer_release(info);
366 	return ret;
367 }
368 
369 static int simplefb_remove(struct platform_device *pdev)
370 {
371 	struct fb_info *info = platform_get_drvdata(pdev);
372 	struct simplefb_par *par = info->par;
373 
374 	unregister_framebuffer(info);
375 	simplefb_clocks_destroy(par);
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",
390 		.of_match_table = simplefb_of_match,
391 	},
392 	.probe = simplefb_probe,
393 	.remove = simplefb_remove,
394 };
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 
405 	if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
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 
415 fs_initcall(simplefb_init);
416 
417 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
418 MODULE_DESCRIPTION("Simple framebuffer driver");
419 MODULE_LICENSE("GPL v2");
420