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