xref: /linux-6.15/drivers/video/fbdev/simplefb.c (revision 748bd587)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f7018c21STomi Valkeinen /*
3f7018c21STomi Valkeinen  * Simplest possible simple frame-buffer driver, as a platform device
4f7018c21STomi Valkeinen  *
5f7018c21STomi Valkeinen  * Copyright (c) 2013, Stephen Warren
6f7018c21STomi Valkeinen  *
7f7018c21STomi Valkeinen  * Based on q40fb.c, which was:
8f7018c21STomi Valkeinen  * Copyright (C) 2001 Richard Zidlicky <[email protected]>
9f7018c21STomi Valkeinen  *
10f7018c21STomi Valkeinen  * Also based on offb.c, which was:
11f7018c21STomi Valkeinen  * Copyright (C) 1997 Geert Uytterhoeven
12f7018c21STomi Valkeinen  * Copyright (C) 1996 Paul Mackerras
13f7018c21STomi Valkeinen  */
14f7018c21STomi Valkeinen 
15f7018c21STomi Valkeinen #include <linux/errno.h>
16f7018c21STomi Valkeinen #include <linux/fb.h>
17f7018c21STomi Valkeinen #include <linux/io.h>
18f7018c21STomi Valkeinen #include <linux/module.h>
19f7018c21STomi Valkeinen #include <linux/platform_data/simplefb.h>
20f7018c21STomi Valkeinen #include <linux/platform_device.h>
21d49c6993SStephen Boyd #include <linux/clk.h>
22814740e7SChen-Yu Tsai #include <linux/of.h>
23db3b4fd1SGeert Uytterhoeven #include <linux/of_clk.h>
2489c95001SHans de Goede #include <linux/of_platform.h>
25814740e7SChen-Yu Tsai #include <linux/parser.h>
26814740e7SChen-Yu Tsai #include <linux/regulator/consumer.h>
27f7018c21STomi Valkeinen 
28ca9384c5SJulia Lawall static const struct fb_fix_screeninfo simplefb_fix = {
29f7018c21STomi Valkeinen 	.id		= "simple",
30f7018c21STomi Valkeinen 	.type		= FB_TYPE_PACKED_PIXELS,
31f7018c21STomi Valkeinen 	.visual		= FB_VISUAL_TRUECOLOR,
32f7018c21STomi Valkeinen 	.accel		= FB_ACCEL_NONE,
33f7018c21STomi Valkeinen };
34f7018c21STomi Valkeinen 
35ca9384c5SJulia Lawall static const struct fb_var_screeninfo simplefb_var = {
36f7018c21STomi Valkeinen 	.height		= -1,
37f7018c21STomi Valkeinen 	.width		= -1,
38f7018c21STomi Valkeinen 	.activate	= FB_ACTIVATE_NOW,
39f7018c21STomi Valkeinen 	.vmode		= FB_VMODE_NONINTERLACED,
40f7018c21STomi Valkeinen };
41f7018c21STomi Valkeinen 
421270be4aSLuc Verhaegen #define PSEUDO_PALETTE_SIZE 16
431270be4aSLuc Verhaegen 
44f7018c21STomi Valkeinen static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
45f7018c21STomi Valkeinen 			      u_int transp, struct fb_info *info)
46f7018c21STomi Valkeinen {
47f7018c21STomi Valkeinen 	u32 *pal = info->pseudo_palette;
48f7018c21STomi Valkeinen 	u32 cr = red >> (16 - info->var.red.length);
49f7018c21STomi Valkeinen 	u32 cg = green >> (16 - info->var.green.length);
50f7018c21STomi Valkeinen 	u32 cb = blue >> (16 - info->var.blue.length);
51f7018c21STomi Valkeinen 	u32 value;
52f7018c21STomi Valkeinen 
531270be4aSLuc Verhaegen 	if (regno >= PSEUDO_PALETTE_SIZE)
54f7018c21STomi Valkeinen 		return -EINVAL;
55f7018c21STomi Valkeinen 
56f7018c21STomi Valkeinen 	value = (cr << info->var.red.offset) |
57f7018c21STomi Valkeinen 		(cg << info->var.green.offset) |
58f7018c21STomi Valkeinen 		(cb << info->var.blue.offset);
59f7018c21STomi Valkeinen 	if (info->var.transp.length > 0) {
60f7018c21STomi Valkeinen 		u32 mask = (1 << info->var.transp.length) - 1;
61f7018c21STomi Valkeinen 		mask <<= info->var.transp.offset;
62f7018c21STomi Valkeinen 		value |= mask;
63f7018c21STomi Valkeinen 	}
64f7018c21STomi Valkeinen 	pal[regno] = value;
65f7018c21STomi Valkeinen 
66f7018c21STomi Valkeinen 	return 0;
67f7018c21STomi Valkeinen }
68f7018c21STomi Valkeinen 
69*748bd587SThomas Zimmermann struct simplefb_par {
70*748bd587SThomas Zimmermann 	u32 palette[PSEUDO_PALETTE_SIZE];
71*748bd587SThomas Zimmermann 	struct resource *mem;
72*748bd587SThomas Zimmermann #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
73*748bd587SThomas Zimmermann 	bool clks_enabled;
74*748bd587SThomas Zimmermann 	unsigned int clk_count;
75*748bd587SThomas Zimmermann 	struct clk **clks;
76*748bd587SThomas Zimmermann #endif
77*748bd587SThomas Zimmermann #if defined CONFIG_OF && defined CONFIG_REGULATOR
78*748bd587SThomas Zimmermann 	bool regulators_enabled;
79*748bd587SThomas Zimmermann 	u32 regulator_count;
80*748bd587SThomas Zimmermann 	struct regulator **regulators;
81*748bd587SThomas Zimmermann #endif
82*748bd587SThomas Zimmermann };
83*748bd587SThomas Zimmermann 
847c9806e8SChen-Yu Tsai static void simplefb_clocks_destroy(struct simplefb_par *par);
857c9806e8SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par);
867c9806e8SChen-Yu Tsai 
87f7018c21STomi Valkeinen static void simplefb_destroy(struct fb_info *info)
88f7018c21STomi Valkeinen {
89*748bd587SThomas Zimmermann 	struct simplefb_par *par = info->par;
90*748bd587SThomas Zimmermann 	struct resource *mem = par->mem;
91*748bd587SThomas Zimmermann 
927c9806e8SChen-Yu Tsai 	simplefb_regulators_destroy(info->par);
937c9806e8SChen-Yu Tsai 	simplefb_clocks_destroy(info->par);
94f7018c21STomi Valkeinen 	if (info->screen_base)
95f7018c21STomi Valkeinen 		iounmap(info->screen_base);
96*748bd587SThomas Zimmermann 
97*748bd587SThomas Zimmermann 	if (mem)
98*748bd587SThomas Zimmermann 		release_mem_region(mem->start, resource_size(mem));
99f7018c21STomi Valkeinen }
100f7018c21STomi Valkeinen 
1018a48ac33SJani Nikula static const struct fb_ops simplefb_ops = {
102f7018c21STomi Valkeinen 	.owner		= THIS_MODULE,
103f7018c21STomi Valkeinen 	.fb_destroy	= simplefb_destroy,
104f7018c21STomi Valkeinen 	.fb_setcolreg	= simplefb_setcolreg,
105f7018c21STomi Valkeinen 	.fb_fillrect	= cfb_fillrect,
106f7018c21STomi Valkeinen 	.fb_copyarea	= cfb_copyarea,
107f7018c21STomi Valkeinen 	.fb_imageblit	= cfb_imageblit,
108f7018c21STomi Valkeinen };
109f7018c21STomi Valkeinen 
110f7018c21STomi Valkeinen static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
111f7018c21STomi Valkeinen 
112f7018c21STomi Valkeinen struct simplefb_params {
113f7018c21STomi Valkeinen 	u32 width;
114f7018c21STomi Valkeinen 	u32 height;
115f7018c21STomi Valkeinen 	u32 stride;
116f7018c21STomi Valkeinen 	struct simplefb_format *format;
117f7018c21STomi Valkeinen };
118f7018c21STomi Valkeinen 
119f7018c21STomi Valkeinen static int simplefb_parse_dt(struct platform_device *pdev,
120f7018c21STomi Valkeinen 			   struct simplefb_params *params)
121f7018c21STomi Valkeinen {
122f7018c21STomi Valkeinen 	struct device_node *np = pdev->dev.of_node;
123f7018c21STomi Valkeinen 	int ret;
124f7018c21STomi Valkeinen 	const char *format;
125f7018c21STomi Valkeinen 	int i;
126f7018c21STomi Valkeinen 
127f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "width", &params->width);
128f7018c21STomi Valkeinen 	if (ret) {
129f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse width property\n");
130f7018c21STomi Valkeinen 		return ret;
131f7018c21STomi Valkeinen 	}
132f7018c21STomi Valkeinen 
133f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "height", &params->height);
134f7018c21STomi Valkeinen 	if (ret) {
135f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse height property\n");
136f7018c21STomi Valkeinen 		return ret;
137f7018c21STomi Valkeinen 	}
138f7018c21STomi Valkeinen 
139f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "stride", &params->stride);
140f7018c21STomi Valkeinen 	if (ret) {
141f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse stride property\n");
142f7018c21STomi Valkeinen 		return ret;
143f7018c21STomi Valkeinen 	}
144f7018c21STomi Valkeinen 
145f7018c21STomi Valkeinen 	ret = of_property_read_string(np, "format", &format);
146f7018c21STomi Valkeinen 	if (ret) {
147f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse format property\n");
148f7018c21STomi Valkeinen 		return ret;
149f7018c21STomi Valkeinen 	}
150f7018c21STomi Valkeinen 	params->format = NULL;
151f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
152f7018c21STomi Valkeinen 		if (strcmp(format, simplefb_formats[i].name))
153f7018c21STomi Valkeinen 			continue;
154f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
155f7018c21STomi Valkeinen 		break;
156f7018c21STomi Valkeinen 	}
157f7018c21STomi Valkeinen 	if (!params->format) {
158f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
159f7018c21STomi Valkeinen 		return -EINVAL;
160f7018c21STomi Valkeinen 	}
161f7018c21STomi Valkeinen 
162f7018c21STomi Valkeinen 	return 0;
163f7018c21STomi Valkeinen }
164f7018c21STomi Valkeinen 
165f7018c21STomi Valkeinen static int simplefb_parse_pd(struct platform_device *pdev,
166f7018c21STomi Valkeinen 			     struct simplefb_params *params)
167f7018c21STomi Valkeinen {
168f7018c21STomi Valkeinen 	struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
169f7018c21STomi Valkeinen 	int i;
170f7018c21STomi Valkeinen 
171f7018c21STomi Valkeinen 	params->width = pd->width;
172f7018c21STomi Valkeinen 	params->height = pd->height;
173f7018c21STomi Valkeinen 	params->stride = pd->stride;
174f7018c21STomi Valkeinen 
175f7018c21STomi Valkeinen 	params->format = NULL;
176f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
177f7018c21STomi Valkeinen 		if (strcmp(pd->format, simplefb_formats[i].name))
178f7018c21STomi Valkeinen 			continue;
179f7018c21STomi Valkeinen 
180f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
181f7018c21STomi Valkeinen 		break;
182f7018c21STomi Valkeinen 	}
183f7018c21STomi Valkeinen 
184f7018c21STomi Valkeinen 	if (!params->format) {
185f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
186f7018c21STomi Valkeinen 		return -EINVAL;
187f7018c21STomi Valkeinen 	}
188f7018c21STomi Valkeinen 
189f7018c21STomi Valkeinen 	return 0;
190f7018c21STomi Valkeinen }
191f7018c21STomi Valkeinen 
1928284731eSHans de Goede #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
193fc219bfdSLuc Verhaegen /*
194fc219bfdSLuc Verhaegen  * Clock handling code.
195fc219bfdSLuc Verhaegen  *
196fc219bfdSLuc Verhaegen  * Here we handle the clocks property of our "simple-framebuffer" dt node.
197fc219bfdSLuc Verhaegen  * This is necessary so that we can make sure that any clocks needed by
198fc219bfdSLuc Verhaegen  * the display engine that the bootloader set up for us (and for which it
199fc219bfdSLuc Verhaegen  * provided a simplefb dt node), stay up, for the life of the simplefb
200fc219bfdSLuc Verhaegen  * driver.
201fc219bfdSLuc Verhaegen  *
202fc219bfdSLuc Verhaegen  * When the driver unloads, we cleanly disable, and then release the clocks.
203fc219bfdSLuc Verhaegen  *
204fc219bfdSLuc Verhaegen  * We only complain about errors here, no action is taken as the most likely
205fc219bfdSLuc Verhaegen  * error can only happen due to a mismatch between the bootloader which set
206fc219bfdSLuc Verhaegen  * up simplefb, and the clock definitions in the device tree. Chances are
207fc219bfdSLuc Verhaegen  * that there are no adverse effects, and if there are, a clean teardown of
208fc219bfdSLuc Verhaegen  * the fb probe will not help us much either. So just complain and carry on,
209fc219bfdSLuc Verhaegen  * and hope that the user actually gets a working fb at the end of things.
210fc219bfdSLuc Verhaegen  */
211a3accfd7SHans de Goede static int simplefb_clocks_get(struct simplefb_par *par,
212fc219bfdSLuc Verhaegen 			       struct platform_device *pdev)
213fc219bfdSLuc Verhaegen {
214fc219bfdSLuc Verhaegen 	struct device_node *np = pdev->dev.of_node;
215fc219bfdSLuc Verhaegen 	struct clk *clock;
216a3accfd7SHans de Goede 	int i;
217fc219bfdSLuc Verhaegen 
218fc219bfdSLuc Verhaegen 	if (dev_get_platdata(&pdev->dev) || !np)
219fc219bfdSLuc Verhaegen 		return 0;
220fc219bfdSLuc Verhaegen 
221fc219bfdSLuc Verhaegen 	par->clk_count = of_clk_get_parent_count(np);
2222cd82d06SStephen Boyd 	if (!par->clk_count)
223fc219bfdSLuc Verhaegen 		return 0;
224fc219bfdSLuc Verhaegen 
225fc219bfdSLuc Verhaegen 	par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
226fc219bfdSLuc Verhaegen 	if (!par->clks)
227fc219bfdSLuc Verhaegen 		return -ENOMEM;
228fc219bfdSLuc Verhaegen 
229fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
230fc219bfdSLuc Verhaegen 		clock = of_clk_get(np, i);
231fc219bfdSLuc Verhaegen 		if (IS_ERR(clock)) {
232fc219bfdSLuc Verhaegen 			if (PTR_ERR(clock) == -EPROBE_DEFER) {
233fc219bfdSLuc Verhaegen 				while (--i >= 0) {
234fc219bfdSLuc Verhaegen 					if (par->clks[i])
235fc219bfdSLuc Verhaegen 						clk_put(par->clks[i]);
236fc219bfdSLuc Verhaegen 				}
237fc219bfdSLuc Verhaegen 				kfree(par->clks);
238fc219bfdSLuc Verhaegen 				return -EPROBE_DEFER;
239fc219bfdSLuc Verhaegen 			}
240fc219bfdSLuc Verhaegen 			dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
241fc219bfdSLuc Verhaegen 				__func__, i, PTR_ERR(clock));
242fc219bfdSLuc Verhaegen 			continue;
243fc219bfdSLuc Verhaegen 		}
244fc219bfdSLuc Verhaegen 		par->clks[i] = clock;
245fc219bfdSLuc Verhaegen 	}
246fc219bfdSLuc Verhaegen 
247a3accfd7SHans de Goede 	return 0;
248a3accfd7SHans de Goede }
249a3accfd7SHans de Goede 
250a3accfd7SHans de Goede static void simplefb_clocks_enable(struct simplefb_par *par,
251a3accfd7SHans de Goede 				   struct platform_device *pdev)
252a3accfd7SHans de Goede {
253a3accfd7SHans de Goede 	int i, ret;
254a3accfd7SHans de Goede 
255fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
256fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
257fc219bfdSLuc Verhaegen 			ret = clk_prepare_enable(par->clks[i]);
258fc219bfdSLuc Verhaegen 			if (ret) {
259fc219bfdSLuc Verhaegen 				dev_err(&pdev->dev,
260fc219bfdSLuc Verhaegen 					"%s: failed to enable clock %d: %d\n",
261fc219bfdSLuc Verhaegen 					__func__, i, ret);
262fc219bfdSLuc Verhaegen 				clk_put(par->clks[i]);
263fc219bfdSLuc Verhaegen 				par->clks[i] = NULL;
264fc219bfdSLuc Verhaegen 			}
265fc219bfdSLuc Verhaegen 		}
266fc219bfdSLuc Verhaegen 	}
267a3accfd7SHans de Goede 	par->clks_enabled = true;
268fc219bfdSLuc Verhaegen }
269fc219bfdSLuc Verhaegen 
270fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par)
271fc219bfdSLuc Verhaegen {
272fc219bfdSLuc Verhaegen 	int i;
273fc219bfdSLuc Verhaegen 
274fc219bfdSLuc Verhaegen 	if (!par->clks)
275fc219bfdSLuc Verhaegen 		return;
276fc219bfdSLuc Verhaegen 
277fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
278fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
279a3accfd7SHans de Goede 			if (par->clks_enabled)
280fc219bfdSLuc Verhaegen 				clk_disable_unprepare(par->clks[i]);
281fc219bfdSLuc Verhaegen 			clk_put(par->clks[i]);
282fc219bfdSLuc Verhaegen 		}
283fc219bfdSLuc Verhaegen 	}
284fc219bfdSLuc Verhaegen 
285fc219bfdSLuc Verhaegen 	kfree(par->clks);
286fc219bfdSLuc Verhaegen }
287fc219bfdSLuc Verhaegen #else
288a3accfd7SHans de Goede static int simplefb_clocks_get(struct simplefb_par *par,
289fc219bfdSLuc Verhaegen 	struct platform_device *pdev) { return 0; }
290a3accfd7SHans de Goede static void simplefb_clocks_enable(struct simplefb_par *par,
291a3accfd7SHans de Goede 	struct platform_device *pdev) { }
292fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par) { }
293fc219bfdSLuc Verhaegen #endif
294fc219bfdSLuc Verhaegen 
295814740e7SChen-Yu Tsai #if defined CONFIG_OF && defined CONFIG_REGULATOR
296814740e7SChen-Yu Tsai 
297814740e7SChen-Yu Tsai #define SUPPLY_SUFFIX "-supply"
298814740e7SChen-Yu Tsai 
299814740e7SChen-Yu Tsai /*
300814740e7SChen-Yu Tsai  * Regulator handling code.
301814740e7SChen-Yu Tsai  *
302814740e7SChen-Yu Tsai  * Here we handle the num-supplies and vin*-supply properties of our
303814740e7SChen-Yu Tsai  * "simple-framebuffer" dt node. This is necessary so that we can make sure
304814740e7SChen-Yu Tsai  * that any regulators needed by the display hardware that the bootloader
305814740e7SChen-Yu Tsai  * set up for us (and for which it provided a simplefb dt node), stay up,
306814740e7SChen-Yu Tsai  * for the life of the simplefb driver.
307814740e7SChen-Yu Tsai  *
308814740e7SChen-Yu Tsai  * When the driver unloads, we cleanly disable, and then release the
309814740e7SChen-Yu Tsai  * regulators.
310814740e7SChen-Yu Tsai  *
311814740e7SChen-Yu Tsai  * We only complain about errors here, no action is taken as the most likely
312814740e7SChen-Yu Tsai  * error can only happen due to a mismatch between the bootloader which set
313814740e7SChen-Yu Tsai  * up simplefb, and the regulator definitions in the device tree. Chances are
314814740e7SChen-Yu Tsai  * that there are no adverse effects, and if there are, a clean teardown of
315814740e7SChen-Yu Tsai  * the fb probe will not help us much either. So just complain and carry on,
316814740e7SChen-Yu Tsai  * and hope that the user actually gets a working fb at the end of things.
317814740e7SChen-Yu Tsai  */
318a3accfd7SHans de Goede static int simplefb_regulators_get(struct simplefb_par *par,
319814740e7SChen-Yu Tsai 				   struct platform_device *pdev)
320814740e7SChen-Yu Tsai {
321814740e7SChen-Yu Tsai 	struct device_node *np = pdev->dev.of_node;
322814740e7SChen-Yu Tsai 	struct property *prop;
323814740e7SChen-Yu Tsai 	struct regulator *regulator;
324814740e7SChen-Yu Tsai 	const char *p;
325a3accfd7SHans de Goede 	int count = 0, i = 0;
326814740e7SChen-Yu Tsai 
327814740e7SChen-Yu Tsai 	if (dev_get_platdata(&pdev->dev) || !np)
328814740e7SChen-Yu Tsai 		return 0;
329814740e7SChen-Yu Tsai 
330814740e7SChen-Yu Tsai 	/* Count the number of regulator supplies */
331814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
332814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
333814740e7SChen-Yu Tsai 		if (p && p != prop->name)
334814740e7SChen-Yu Tsai 			count++;
335814740e7SChen-Yu Tsai 	}
336814740e7SChen-Yu Tsai 
337814740e7SChen-Yu Tsai 	if (!count)
338814740e7SChen-Yu Tsai 		return 0;
339814740e7SChen-Yu Tsai 
340814740e7SChen-Yu Tsai 	par->regulators = devm_kcalloc(&pdev->dev, count,
341814740e7SChen-Yu Tsai 				       sizeof(struct regulator *), GFP_KERNEL);
342814740e7SChen-Yu Tsai 	if (!par->regulators)
343814740e7SChen-Yu Tsai 		return -ENOMEM;
344814740e7SChen-Yu Tsai 
345814740e7SChen-Yu Tsai 	/* Get all the regulators */
346814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
347814740e7SChen-Yu Tsai 		char name[32]; /* 32 is max size of property name */
348814740e7SChen-Yu Tsai 
349814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
350814740e7SChen-Yu Tsai 		if (!p || p == prop->name)
351814740e7SChen-Yu Tsai 			continue;
352814740e7SChen-Yu Tsai 
353814740e7SChen-Yu Tsai 		strlcpy(name, prop->name,
354814740e7SChen-Yu Tsai 			strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
355814740e7SChen-Yu Tsai 		regulator = devm_regulator_get_optional(&pdev->dev, name);
356814740e7SChen-Yu Tsai 		if (IS_ERR(regulator)) {
357814740e7SChen-Yu Tsai 			if (PTR_ERR(regulator) == -EPROBE_DEFER)
358814740e7SChen-Yu Tsai 				return -EPROBE_DEFER;
359814740e7SChen-Yu Tsai 			dev_err(&pdev->dev, "regulator %s not found: %ld\n",
360814740e7SChen-Yu Tsai 				name, PTR_ERR(regulator));
361814740e7SChen-Yu Tsai 			continue;
362814740e7SChen-Yu Tsai 		}
363814740e7SChen-Yu Tsai 		par->regulators[i++] = regulator;
364814740e7SChen-Yu Tsai 	}
365814740e7SChen-Yu Tsai 	par->regulator_count = i;
366814740e7SChen-Yu Tsai 
367a3accfd7SHans de Goede 	return 0;
368a3accfd7SHans de Goede }
369a3accfd7SHans de Goede 
370a3accfd7SHans de Goede static void simplefb_regulators_enable(struct simplefb_par *par,
371a3accfd7SHans de Goede 				       struct platform_device *pdev)
372a3accfd7SHans de Goede {
373a3accfd7SHans de Goede 	int i, ret;
374a3accfd7SHans de Goede 
375814740e7SChen-Yu Tsai 	/* Enable all the regulators */
376814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++) {
377814740e7SChen-Yu Tsai 		ret = regulator_enable(par->regulators[i]);
378814740e7SChen-Yu Tsai 		if (ret) {
379814740e7SChen-Yu Tsai 			dev_err(&pdev->dev,
380814740e7SChen-Yu Tsai 				"failed to enable regulator %d: %d\n",
381814740e7SChen-Yu Tsai 				i, ret);
382814740e7SChen-Yu Tsai 			devm_regulator_put(par->regulators[i]);
383814740e7SChen-Yu Tsai 			par->regulators[i] = NULL;
384814740e7SChen-Yu Tsai 		}
385814740e7SChen-Yu Tsai 	}
386a3accfd7SHans de Goede 	par->regulators_enabled = true;
387814740e7SChen-Yu Tsai }
388814740e7SChen-Yu Tsai 
389814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par)
390814740e7SChen-Yu Tsai {
391814740e7SChen-Yu Tsai 	int i;
392814740e7SChen-Yu Tsai 
393a3accfd7SHans de Goede 	if (!par->regulators || !par->regulators_enabled)
394814740e7SChen-Yu Tsai 		return;
395814740e7SChen-Yu Tsai 
396814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++)
397814740e7SChen-Yu Tsai 		if (par->regulators[i])
398814740e7SChen-Yu Tsai 			regulator_disable(par->regulators[i]);
399814740e7SChen-Yu Tsai }
400814740e7SChen-Yu Tsai #else
401a3accfd7SHans de Goede static int simplefb_regulators_get(struct simplefb_par *par,
402814740e7SChen-Yu Tsai 	struct platform_device *pdev) { return 0; }
403a3accfd7SHans de Goede static void simplefb_regulators_enable(struct simplefb_par *par,
404a3accfd7SHans de Goede 	struct platform_device *pdev) { }
405814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par) { }
406814740e7SChen-Yu Tsai #endif
407814740e7SChen-Yu Tsai 
408f7018c21STomi Valkeinen static int simplefb_probe(struct platform_device *pdev)
409f7018c21STomi Valkeinen {
410f7018c21STomi Valkeinen 	int ret;
411f7018c21STomi Valkeinen 	struct simplefb_params params;
412f7018c21STomi Valkeinen 	struct fb_info *info;
4131270be4aSLuc Verhaegen 	struct simplefb_par *par;
414*748bd587SThomas Zimmermann 	struct resource *res, *mem;
415f7018c21STomi Valkeinen 
416fb561bf9SJavier Martinez Canillas 	/*
417fb561bf9SJavier Martinez Canillas 	 * Generic drivers must not be registered if a framebuffer exists.
418fb561bf9SJavier Martinez Canillas 	 * If a native driver was probed, the display hardware was already
419fb561bf9SJavier Martinez Canillas 	 * taken and attempting to use the system framebuffer is dangerous.
420fb561bf9SJavier Martinez Canillas 	 */
421fb561bf9SJavier Martinez Canillas 	if (num_registered_fb > 0) {
422fb561bf9SJavier Martinez Canillas 		dev_err(&pdev->dev,
423fb561bf9SJavier Martinez Canillas 			"simplefb: a framebuffer is already registered\n");
424fb561bf9SJavier Martinez Canillas 		return -EINVAL;
425fb561bf9SJavier Martinez Canillas 	}
426fb561bf9SJavier Martinez Canillas 
427f7018c21STomi Valkeinen 	if (fb_get_options("simplefb", NULL))
428f7018c21STomi Valkeinen 		return -ENODEV;
429f7018c21STomi Valkeinen 
430f7018c21STomi Valkeinen 	ret = -ENODEV;
431f7018c21STomi Valkeinen 	if (dev_get_platdata(&pdev->dev))
432f7018c21STomi Valkeinen 		ret = simplefb_parse_pd(pdev, &params);
433f7018c21STomi Valkeinen 	else if (pdev->dev.of_node)
434f7018c21STomi Valkeinen 		ret = simplefb_parse_dt(pdev, &params);
435f7018c21STomi Valkeinen 
436f7018c21STomi Valkeinen 	if (ret)
437f7018c21STomi Valkeinen 		return ret;
438f7018c21STomi Valkeinen 
439*748bd587SThomas Zimmermann 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
440*748bd587SThomas Zimmermann 	if (!res) {
441f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "No memory resource\n");
442f7018c21STomi Valkeinen 		return -EINVAL;
443f7018c21STomi Valkeinen 	}
444f7018c21STomi Valkeinen 
445*748bd587SThomas Zimmermann 	mem = request_mem_region(res->start, resource_size(res), "simplefb");
446*748bd587SThomas Zimmermann 	if (!mem) {
447*748bd587SThomas Zimmermann 		/*
448*748bd587SThomas Zimmermann 		 * We cannot make this fatal. Sometimes this comes from magic
449*748bd587SThomas Zimmermann 		 * spaces our resource handlers simply don't know about. Use
450*748bd587SThomas Zimmermann 		 * the I/O-memory resource as-is and try to map that instead.
451*748bd587SThomas Zimmermann 		 */
452*748bd587SThomas Zimmermann 		dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res);
453*748bd587SThomas Zimmermann 		mem = res;
454*748bd587SThomas Zimmermann 	}
455*748bd587SThomas Zimmermann 
4561270be4aSLuc Verhaegen 	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
457*748bd587SThomas Zimmermann 	if (!info) {
458*748bd587SThomas Zimmermann 		ret = -ENOMEM;
459*748bd587SThomas Zimmermann 		goto error_release_mem_region;
460*748bd587SThomas Zimmermann 	}
461f7018c21STomi Valkeinen 	platform_set_drvdata(pdev, info);
462f7018c21STomi Valkeinen 
4631270be4aSLuc Verhaegen 	par = info->par;
4641270be4aSLuc Verhaegen 
465f7018c21STomi Valkeinen 	info->fix = simplefb_fix;
466f7018c21STomi Valkeinen 	info->fix.smem_start = mem->start;
467f7018c21STomi Valkeinen 	info->fix.smem_len = resource_size(mem);
468f7018c21STomi Valkeinen 	info->fix.line_length = params.stride;
469f7018c21STomi Valkeinen 
470f7018c21STomi Valkeinen 	info->var = simplefb_var;
471f7018c21STomi Valkeinen 	info->var.xres = params.width;
472f7018c21STomi Valkeinen 	info->var.yres = params.height;
473f7018c21STomi Valkeinen 	info->var.xres_virtual = params.width;
474f7018c21STomi Valkeinen 	info->var.yres_virtual = params.height;
475f7018c21STomi Valkeinen 	info->var.bits_per_pixel = params.format->bits_per_pixel;
476f7018c21STomi Valkeinen 	info->var.red = params.format->red;
477f7018c21STomi Valkeinen 	info->var.green = params.format->green;
478f7018c21STomi Valkeinen 	info->var.blue = params.format->blue;
479f7018c21STomi Valkeinen 	info->var.transp = params.format->transp;
480f7018c21STomi Valkeinen 
481f7018c21STomi Valkeinen 	info->apertures = alloc_apertures(1);
482f7018c21STomi Valkeinen 	if (!info->apertures) {
483bf2fda15SLuc Verhaegen 		ret = -ENOMEM;
484bf2fda15SLuc Verhaegen 		goto error_fb_release;
485f7018c21STomi Valkeinen 	}
486f7018c21STomi Valkeinen 	info->apertures->ranges[0].base = info->fix.smem_start;
487f7018c21STomi Valkeinen 	info->apertures->ranges[0].size = info->fix.smem_len;
488f7018c21STomi Valkeinen 
489f7018c21STomi Valkeinen 	info->fbops = &simplefb_ops;
490f7018c21STomi Valkeinen 	info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
491f7018c21STomi Valkeinen 	info->screen_base = ioremap_wc(info->fix.smem_start,
492f7018c21STomi Valkeinen 				       info->fix.smem_len);
493f7018c21STomi Valkeinen 	if (!info->screen_base) {
494bf2fda15SLuc Verhaegen 		ret = -ENOMEM;
495bf2fda15SLuc Verhaegen 		goto error_fb_release;
496f7018c21STomi Valkeinen 	}
4971270be4aSLuc Verhaegen 	info->pseudo_palette = par->palette;
498f7018c21STomi Valkeinen 
499a3accfd7SHans de Goede 	ret = simplefb_clocks_get(par, pdev);
500fc219bfdSLuc Verhaegen 	if (ret < 0)
501fc219bfdSLuc Verhaegen 		goto error_unmap;
502fc219bfdSLuc Verhaegen 
503a3accfd7SHans de Goede 	ret = simplefb_regulators_get(par, pdev);
504814740e7SChen-Yu Tsai 	if (ret < 0)
505814740e7SChen-Yu Tsai 		goto error_clocks;
506814740e7SChen-Yu Tsai 
507a3accfd7SHans de Goede 	simplefb_clocks_enable(par, pdev);
508a3accfd7SHans de Goede 	simplefb_regulators_enable(par, pdev);
509a3accfd7SHans de Goede 
5105269a618SPeter Robinson 	dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n",
5115269a618SPeter Robinson 			     info->fix.smem_start, info->fix.smem_len);
512f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
513f7018c21STomi Valkeinen 			     params.format->name,
514f7018c21STomi Valkeinen 			     info->var.xres, info->var.yres,
515f7018c21STomi Valkeinen 			     info->var.bits_per_pixel, info->fix.line_length);
516f7018c21STomi Valkeinen 
517*748bd587SThomas Zimmermann 	if (mem != res)
518*748bd587SThomas Zimmermann 		par->mem = mem; /* release in clean-up handler */
519*748bd587SThomas Zimmermann 
520f7018c21STomi Valkeinen 	ret = register_framebuffer(info);
521f7018c21STomi Valkeinen 	if (ret < 0) {
522f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
523814740e7SChen-Yu Tsai 		goto error_regulators;
524f7018c21STomi Valkeinen 	}
525f7018c21STomi Valkeinen 
526f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
527f7018c21STomi Valkeinen 
528f7018c21STomi Valkeinen 	return 0;
529bf2fda15SLuc Verhaegen 
530814740e7SChen-Yu Tsai error_regulators:
531814740e7SChen-Yu Tsai 	simplefb_regulators_destroy(par);
532fc219bfdSLuc Verhaegen error_clocks:
533fc219bfdSLuc Verhaegen 	simplefb_clocks_destroy(par);
534bf2fda15SLuc Verhaegen error_unmap:
535bf2fda15SLuc Verhaegen 	iounmap(info->screen_base);
536bf2fda15SLuc Verhaegen error_fb_release:
537bf2fda15SLuc Verhaegen 	framebuffer_release(info);
538*748bd587SThomas Zimmermann error_release_mem_region:
539*748bd587SThomas Zimmermann 	if (mem != res)
540*748bd587SThomas Zimmermann 		release_mem_region(mem->start, resource_size(mem));
541bf2fda15SLuc Verhaegen 	return ret;
542f7018c21STomi Valkeinen }
543f7018c21STomi Valkeinen 
544f7018c21STomi Valkeinen static int simplefb_remove(struct platform_device *pdev)
545f7018c21STomi Valkeinen {
546f7018c21STomi Valkeinen 	struct fb_info *info = platform_get_drvdata(pdev);
547f7018c21STomi Valkeinen 
548f7018c21STomi Valkeinen 	unregister_framebuffer(info);
549f7018c21STomi Valkeinen 	framebuffer_release(info);
550f7018c21STomi Valkeinen 
551f7018c21STomi Valkeinen 	return 0;
552f7018c21STomi Valkeinen }
553f7018c21STomi Valkeinen 
554f7018c21STomi Valkeinen static const struct of_device_id simplefb_of_match[] = {
555f7018c21STomi Valkeinen 	{ .compatible = "simple-framebuffer", },
556f7018c21STomi Valkeinen 	{ },
557f7018c21STomi Valkeinen };
558f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(of, simplefb_of_match);
559f7018c21STomi Valkeinen 
560f7018c21STomi Valkeinen static struct platform_driver simplefb_driver = {
561f7018c21STomi Valkeinen 	.driver = {
562f7018c21STomi Valkeinen 		.name = "simple-framebuffer",
563f7018c21STomi Valkeinen 		.of_match_table = simplefb_of_match,
564f7018c21STomi Valkeinen 	},
565f7018c21STomi Valkeinen 	.probe = simplefb_probe,
566f7018c21STomi Valkeinen 	.remove = simplefb_remove,
567f7018c21STomi Valkeinen };
56889c95001SHans de Goede 
5692f92ea21SHector Martin module_platform_driver(simplefb_driver);
570f7018c21STomi Valkeinen 
571f7018c21STomi Valkeinen MODULE_AUTHOR("Stephen Warren <[email protected]>");
572f7018c21STomi Valkeinen MODULE_DESCRIPTION("Simple framebuffer driver");
573f7018c21STomi Valkeinen MODULE_LICENSE("GPL v2");
574