xref: /linux-6.15/drivers/video/fbdev/simplefb.c (revision 8d026858)
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 
69748bd587SThomas Zimmermann struct simplefb_par {
70748bd587SThomas Zimmermann 	u32 palette[PSEUDO_PALETTE_SIZE];
71748bd587SThomas Zimmermann 	struct resource *mem;
72748bd587SThomas Zimmermann #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
73748bd587SThomas Zimmermann 	bool clks_enabled;
74748bd587SThomas Zimmermann 	unsigned int clk_count;
75748bd587SThomas Zimmermann 	struct clk **clks;
76748bd587SThomas Zimmermann #endif
77748bd587SThomas Zimmermann #if defined CONFIG_OF && defined CONFIG_REGULATOR
78748bd587SThomas Zimmermann 	bool regulators_enabled;
79748bd587SThomas Zimmermann 	u32 regulator_count;
80748bd587SThomas Zimmermann 	struct regulator **regulators;
81748bd587SThomas Zimmermann #endif
82748bd587SThomas Zimmermann };
83748bd587SThomas 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 
87666b90b3SJavier Martinez Canillas /*
88666b90b3SJavier Martinez Canillas  * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
89666b90b3SJavier Martinez Canillas  * of unregister_framebuffer() or fb_release(). Do any cleanup here.
90666b90b3SJavier Martinez Canillas  */
91f7018c21STomi Valkeinen static void simplefb_destroy(struct fb_info *info)
92f7018c21STomi Valkeinen {
93748bd587SThomas Zimmermann 	struct simplefb_par *par = info->par;
94748bd587SThomas Zimmermann 	struct resource *mem = par->mem;
95748bd587SThomas Zimmermann 
967c9806e8SChen-Yu Tsai 	simplefb_regulators_destroy(info->par);
977c9806e8SChen-Yu Tsai 	simplefb_clocks_destroy(info->par);
98f7018c21STomi Valkeinen 	if (info->screen_base)
99f7018c21STomi Valkeinen 		iounmap(info->screen_base);
100748bd587SThomas Zimmermann 
101666b90b3SJavier Martinez Canillas 	framebuffer_release(info);
102666b90b3SJavier Martinez Canillas 
103748bd587SThomas Zimmermann 	if (mem)
104748bd587SThomas Zimmermann 		release_mem_region(mem->start, resource_size(mem));
105f7018c21STomi Valkeinen }
106f7018c21STomi Valkeinen 
1078a48ac33SJani Nikula static const struct fb_ops simplefb_ops = {
108f7018c21STomi Valkeinen 	.owner		= THIS_MODULE,
109f7018c21STomi Valkeinen 	.fb_destroy	= simplefb_destroy,
110f7018c21STomi Valkeinen 	.fb_setcolreg	= simplefb_setcolreg,
111f7018c21STomi Valkeinen 	.fb_fillrect	= cfb_fillrect,
112f7018c21STomi Valkeinen 	.fb_copyarea	= cfb_copyarea,
113f7018c21STomi Valkeinen 	.fb_imageblit	= cfb_imageblit,
114f7018c21STomi Valkeinen };
115f7018c21STomi Valkeinen 
116f7018c21STomi Valkeinen static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
117f7018c21STomi Valkeinen 
118f7018c21STomi Valkeinen struct simplefb_params {
119f7018c21STomi Valkeinen 	u32 width;
120f7018c21STomi Valkeinen 	u32 height;
121f7018c21STomi Valkeinen 	u32 stride;
122f7018c21STomi Valkeinen 	struct simplefb_format *format;
123f7018c21STomi Valkeinen };
124f7018c21STomi Valkeinen 
125f7018c21STomi Valkeinen static int simplefb_parse_dt(struct platform_device *pdev,
126f7018c21STomi Valkeinen 			   struct simplefb_params *params)
127f7018c21STomi Valkeinen {
128f7018c21STomi Valkeinen 	struct device_node *np = pdev->dev.of_node;
129f7018c21STomi Valkeinen 	int ret;
130f7018c21STomi Valkeinen 	const char *format;
131f7018c21STomi Valkeinen 	int i;
132f7018c21STomi Valkeinen 
133f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "width", &params->width);
134f7018c21STomi Valkeinen 	if (ret) {
135f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse width property\n");
136f7018c21STomi Valkeinen 		return ret;
137f7018c21STomi Valkeinen 	}
138f7018c21STomi Valkeinen 
139f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "height", &params->height);
140f7018c21STomi Valkeinen 	if (ret) {
141f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse height property\n");
142f7018c21STomi Valkeinen 		return ret;
143f7018c21STomi Valkeinen 	}
144f7018c21STomi Valkeinen 
145f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "stride", &params->stride);
146f7018c21STomi Valkeinen 	if (ret) {
147f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse stride property\n");
148f7018c21STomi Valkeinen 		return ret;
149f7018c21STomi Valkeinen 	}
150f7018c21STomi Valkeinen 
151f7018c21STomi Valkeinen 	ret = of_property_read_string(np, "format", &format);
152f7018c21STomi Valkeinen 	if (ret) {
153f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse format property\n");
154f7018c21STomi Valkeinen 		return ret;
155f7018c21STomi Valkeinen 	}
156f7018c21STomi Valkeinen 	params->format = NULL;
157f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
158f7018c21STomi Valkeinen 		if (strcmp(format, simplefb_formats[i].name))
159f7018c21STomi Valkeinen 			continue;
160f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
161f7018c21STomi Valkeinen 		break;
162f7018c21STomi Valkeinen 	}
163f7018c21STomi Valkeinen 	if (!params->format) {
164f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
165f7018c21STomi Valkeinen 		return -EINVAL;
166f7018c21STomi Valkeinen 	}
167f7018c21STomi Valkeinen 
168f7018c21STomi Valkeinen 	return 0;
169f7018c21STomi Valkeinen }
170f7018c21STomi Valkeinen 
171f7018c21STomi Valkeinen static int simplefb_parse_pd(struct platform_device *pdev,
172f7018c21STomi Valkeinen 			     struct simplefb_params *params)
173f7018c21STomi Valkeinen {
174f7018c21STomi Valkeinen 	struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
175f7018c21STomi Valkeinen 	int i;
176f7018c21STomi Valkeinen 
177f7018c21STomi Valkeinen 	params->width = pd->width;
178f7018c21STomi Valkeinen 	params->height = pd->height;
179f7018c21STomi Valkeinen 	params->stride = pd->stride;
180f7018c21STomi Valkeinen 
181f7018c21STomi Valkeinen 	params->format = NULL;
182f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
183f7018c21STomi Valkeinen 		if (strcmp(pd->format, simplefb_formats[i].name))
184f7018c21STomi Valkeinen 			continue;
185f7018c21STomi Valkeinen 
186f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
187f7018c21STomi Valkeinen 		break;
188f7018c21STomi Valkeinen 	}
189f7018c21STomi Valkeinen 
190f7018c21STomi Valkeinen 	if (!params->format) {
191f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
192f7018c21STomi Valkeinen 		return -EINVAL;
193f7018c21STomi Valkeinen 	}
194f7018c21STomi Valkeinen 
195f7018c21STomi Valkeinen 	return 0;
196f7018c21STomi Valkeinen }
197f7018c21STomi Valkeinen 
1988284731eSHans de Goede #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
199fc219bfdSLuc Verhaegen /*
200fc219bfdSLuc Verhaegen  * Clock handling code.
201fc219bfdSLuc Verhaegen  *
202fc219bfdSLuc Verhaegen  * Here we handle the clocks property of our "simple-framebuffer" dt node.
203fc219bfdSLuc Verhaegen  * This is necessary so that we can make sure that any clocks needed by
204fc219bfdSLuc Verhaegen  * the display engine that the bootloader set up for us (and for which it
205fc219bfdSLuc Verhaegen  * provided a simplefb dt node), stay up, for the life of the simplefb
206fc219bfdSLuc Verhaegen  * driver.
207fc219bfdSLuc Verhaegen  *
208fc219bfdSLuc Verhaegen  * When the driver unloads, we cleanly disable, and then release the clocks.
209fc219bfdSLuc Verhaegen  *
210fc219bfdSLuc Verhaegen  * We only complain about errors here, no action is taken as the most likely
211fc219bfdSLuc Verhaegen  * error can only happen due to a mismatch between the bootloader which set
212fc219bfdSLuc Verhaegen  * up simplefb, and the clock definitions in the device tree. Chances are
213fc219bfdSLuc Verhaegen  * that there are no adverse effects, and if there are, a clean teardown of
214fc219bfdSLuc Verhaegen  * the fb probe will not help us much either. So just complain and carry on,
215fc219bfdSLuc Verhaegen  * and hope that the user actually gets a working fb at the end of things.
216fc219bfdSLuc Verhaegen  */
217a3accfd7SHans de Goede static int simplefb_clocks_get(struct simplefb_par *par,
218fc219bfdSLuc Verhaegen 			       struct platform_device *pdev)
219fc219bfdSLuc Verhaegen {
220fc219bfdSLuc Verhaegen 	struct device_node *np = pdev->dev.of_node;
221fc219bfdSLuc Verhaegen 	struct clk *clock;
222a3accfd7SHans de Goede 	int i;
223fc219bfdSLuc Verhaegen 
224fc219bfdSLuc Verhaegen 	if (dev_get_platdata(&pdev->dev) || !np)
225fc219bfdSLuc Verhaegen 		return 0;
226fc219bfdSLuc Verhaegen 
227fc219bfdSLuc Verhaegen 	par->clk_count = of_clk_get_parent_count(np);
2282cd82d06SStephen Boyd 	if (!par->clk_count)
229fc219bfdSLuc Verhaegen 		return 0;
230fc219bfdSLuc Verhaegen 
231fc219bfdSLuc Verhaegen 	par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
232fc219bfdSLuc Verhaegen 	if (!par->clks)
233fc219bfdSLuc Verhaegen 		return -ENOMEM;
234fc219bfdSLuc Verhaegen 
235fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
236fc219bfdSLuc Verhaegen 		clock = of_clk_get(np, i);
237fc219bfdSLuc Verhaegen 		if (IS_ERR(clock)) {
238fc219bfdSLuc Verhaegen 			if (PTR_ERR(clock) == -EPROBE_DEFER) {
239fc219bfdSLuc Verhaegen 				while (--i >= 0) {
240fc219bfdSLuc Verhaegen 					clk_put(par->clks[i]);
241fc219bfdSLuc Verhaegen 				}
242fc219bfdSLuc Verhaegen 				kfree(par->clks);
243fc219bfdSLuc Verhaegen 				return -EPROBE_DEFER;
244fc219bfdSLuc Verhaegen 			}
245fc219bfdSLuc Verhaegen 			dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
246fc219bfdSLuc Verhaegen 				__func__, i, PTR_ERR(clock));
247fc219bfdSLuc Verhaegen 			continue;
248fc219bfdSLuc Verhaegen 		}
249fc219bfdSLuc Verhaegen 		par->clks[i] = clock;
250fc219bfdSLuc Verhaegen 	}
251fc219bfdSLuc Verhaegen 
252a3accfd7SHans de Goede 	return 0;
253a3accfd7SHans de Goede }
254a3accfd7SHans de Goede 
255a3accfd7SHans de Goede static void simplefb_clocks_enable(struct simplefb_par *par,
256a3accfd7SHans de Goede 				   struct platform_device *pdev)
257a3accfd7SHans de Goede {
258a3accfd7SHans de Goede 	int i, ret;
259a3accfd7SHans de Goede 
260fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
261fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
262fc219bfdSLuc Verhaegen 			ret = clk_prepare_enable(par->clks[i]);
263fc219bfdSLuc Verhaegen 			if (ret) {
264fc219bfdSLuc Verhaegen 				dev_err(&pdev->dev,
265fc219bfdSLuc Verhaegen 					"%s: failed to enable clock %d: %d\n",
266fc219bfdSLuc Verhaegen 					__func__, i, ret);
267fc219bfdSLuc Verhaegen 				clk_put(par->clks[i]);
268fc219bfdSLuc Verhaegen 				par->clks[i] = NULL;
269fc219bfdSLuc Verhaegen 			}
270fc219bfdSLuc Verhaegen 		}
271fc219bfdSLuc Verhaegen 	}
272a3accfd7SHans de Goede 	par->clks_enabled = true;
273fc219bfdSLuc Verhaegen }
274fc219bfdSLuc Verhaegen 
275fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par)
276fc219bfdSLuc Verhaegen {
277fc219bfdSLuc Verhaegen 	int i;
278fc219bfdSLuc Verhaegen 
279fc219bfdSLuc Verhaegen 	if (!par->clks)
280fc219bfdSLuc Verhaegen 		return;
281fc219bfdSLuc Verhaegen 
282fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
283fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
284a3accfd7SHans de Goede 			if (par->clks_enabled)
285fc219bfdSLuc Verhaegen 				clk_disable_unprepare(par->clks[i]);
286fc219bfdSLuc Verhaegen 			clk_put(par->clks[i]);
287fc219bfdSLuc Verhaegen 		}
288fc219bfdSLuc Verhaegen 	}
289fc219bfdSLuc Verhaegen 
290fc219bfdSLuc Verhaegen 	kfree(par->clks);
291fc219bfdSLuc Verhaegen }
292fc219bfdSLuc Verhaegen #else
293a3accfd7SHans de Goede static int simplefb_clocks_get(struct simplefb_par *par,
294fc219bfdSLuc Verhaegen 	struct platform_device *pdev) { return 0; }
295a3accfd7SHans de Goede static void simplefb_clocks_enable(struct simplefb_par *par,
296a3accfd7SHans de Goede 	struct platform_device *pdev) { }
297fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par) { }
298fc219bfdSLuc Verhaegen #endif
299fc219bfdSLuc Verhaegen 
300814740e7SChen-Yu Tsai #if defined CONFIG_OF && defined CONFIG_REGULATOR
301814740e7SChen-Yu Tsai 
302814740e7SChen-Yu Tsai #define SUPPLY_SUFFIX "-supply"
303814740e7SChen-Yu Tsai 
304814740e7SChen-Yu Tsai /*
305814740e7SChen-Yu Tsai  * Regulator handling code.
306814740e7SChen-Yu Tsai  *
307814740e7SChen-Yu Tsai  * Here we handle the num-supplies and vin*-supply properties of our
308814740e7SChen-Yu Tsai  * "simple-framebuffer" dt node. This is necessary so that we can make sure
309814740e7SChen-Yu Tsai  * that any regulators needed by the display hardware that the bootloader
310814740e7SChen-Yu Tsai  * set up for us (and for which it provided a simplefb dt node), stay up,
311814740e7SChen-Yu Tsai  * for the life of the simplefb driver.
312814740e7SChen-Yu Tsai  *
313814740e7SChen-Yu Tsai  * When the driver unloads, we cleanly disable, and then release the
314814740e7SChen-Yu Tsai  * regulators.
315814740e7SChen-Yu Tsai  *
316814740e7SChen-Yu Tsai  * We only complain about errors here, no action is taken as the most likely
317814740e7SChen-Yu Tsai  * error can only happen due to a mismatch between the bootloader which set
318814740e7SChen-Yu Tsai  * up simplefb, and the regulator definitions in the device tree. Chances are
319814740e7SChen-Yu Tsai  * that there are no adverse effects, and if there are, a clean teardown of
320814740e7SChen-Yu Tsai  * the fb probe will not help us much either. So just complain and carry on,
321814740e7SChen-Yu Tsai  * and hope that the user actually gets a working fb at the end of things.
322814740e7SChen-Yu Tsai  */
323a3accfd7SHans de Goede static int simplefb_regulators_get(struct simplefb_par *par,
324814740e7SChen-Yu Tsai 				   struct platform_device *pdev)
325814740e7SChen-Yu Tsai {
326814740e7SChen-Yu Tsai 	struct device_node *np = pdev->dev.of_node;
327814740e7SChen-Yu Tsai 	struct property *prop;
328814740e7SChen-Yu Tsai 	struct regulator *regulator;
329814740e7SChen-Yu Tsai 	const char *p;
330a3accfd7SHans de Goede 	int count = 0, i = 0;
331814740e7SChen-Yu Tsai 
332814740e7SChen-Yu Tsai 	if (dev_get_platdata(&pdev->dev) || !np)
333814740e7SChen-Yu Tsai 		return 0;
334814740e7SChen-Yu Tsai 
335814740e7SChen-Yu Tsai 	/* Count the number of regulator supplies */
336814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
337814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
338814740e7SChen-Yu Tsai 		if (p && p != prop->name)
339814740e7SChen-Yu Tsai 			count++;
340814740e7SChen-Yu Tsai 	}
341814740e7SChen-Yu Tsai 
342814740e7SChen-Yu Tsai 	if (!count)
343814740e7SChen-Yu Tsai 		return 0;
344814740e7SChen-Yu Tsai 
345814740e7SChen-Yu Tsai 	par->regulators = devm_kcalloc(&pdev->dev, count,
346814740e7SChen-Yu Tsai 				       sizeof(struct regulator *), GFP_KERNEL);
347814740e7SChen-Yu Tsai 	if (!par->regulators)
348814740e7SChen-Yu Tsai 		return -ENOMEM;
349814740e7SChen-Yu Tsai 
350814740e7SChen-Yu Tsai 	/* Get all the regulators */
351814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
352814740e7SChen-Yu Tsai 		char name[32]; /* 32 is max size of property name */
353814740e7SChen-Yu Tsai 
354814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
355814740e7SChen-Yu Tsai 		if (!p || p == prop->name)
356814740e7SChen-Yu Tsai 			continue;
357814740e7SChen-Yu Tsai 
358*8d026858SWolfram Sang 		strscpy(name, prop->name,
359814740e7SChen-Yu Tsai 			strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
360814740e7SChen-Yu Tsai 		regulator = devm_regulator_get_optional(&pdev->dev, name);
361814740e7SChen-Yu Tsai 		if (IS_ERR(regulator)) {
362814740e7SChen-Yu Tsai 			if (PTR_ERR(regulator) == -EPROBE_DEFER)
363814740e7SChen-Yu Tsai 				return -EPROBE_DEFER;
364814740e7SChen-Yu Tsai 			dev_err(&pdev->dev, "regulator %s not found: %ld\n",
365814740e7SChen-Yu Tsai 				name, PTR_ERR(regulator));
366814740e7SChen-Yu Tsai 			continue;
367814740e7SChen-Yu Tsai 		}
368814740e7SChen-Yu Tsai 		par->regulators[i++] = regulator;
369814740e7SChen-Yu Tsai 	}
370814740e7SChen-Yu Tsai 	par->regulator_count = i;
371814740e7SChen-Yu Tsai 
372a3accfd7SHans de Goede 	return 0;
373a3accfd7SHans de Goede }
374a3accfd7SHans de Goede 
375a3accfd7SHans de Goede static void simplefb_regulators_enable(struct simplefb_par *par,
376a3accfd7SHans de Goede 				       struct platform_device *pdev)
377a3accfd7SHans de Goede {
378a3accfd7SHans de Goede 	int i, ret;
379a3accfd7SHans de Goede 
380814740e7SChen-Yu Tsai 	/* Enable all the regulators */
381814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++) {
382814740e7SChen-Yu Tsai 		ret = regulator_enable(par->regulators[i]);
383814740e7SChen-Yu Tsai 		if (ret) {
384814740e7SChen-Yu Tsai 			dev_err(&pdev->dev,
385814740e7SChen-Yu Tsai 				"failed to enable regulator %d: %d\n",
386814740e7SChen-Yu Tsai 				i, ret);
387814740e7SChen-Yu Tsai 			devm_regulator_put(par->regulators[i]);
388814740e7SChen-Yu Tsai 			par->regulators[i] = NULL;
389814740e7SChen-Yu Tsai 		}
390814740e7SChen-Yu Tsai 	}
391a3accfd7SHans de Goede 	par->regulators_enabled = true;
392814740e7SChen-Yu Tsai }
393814740e7SChen-Yu Tsai 
394814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par)
395814740e7SChen-Yu Tsai {
396814740e7SChen-Yu Tsai 	int i;
397814740e7SChen-Yu Tsai 
398a3accfd7SHans de Goede 	if (!par->regulators || !par->regulators_enabled)
399814740e7SChen-Yu Tsai 		return;
400814740e7SChen-Yu Tsai 
401814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++)
402814740e7SChen-Yu Tsai 		if (par->regulators[i])
403814740e7SChen-Yu Tsai 			regulator_disable(par->regulators[i]);
404814740e7SChen-Yu Tsai }
405814740e7SChen-Yu Tsai #else
406a3accfd7SHans de Goede static int simplefb_regulators_get(struct simplefb_par *par,
407814740e7SChen-Yu Tsai 	struct platform_device *pdev) { return 0; }
408a3accfd7SHans de Goede static void simplefb_regulators_enable(struct simplefb_par *par,
409a3accfd7SHans de Goede 	struct platform_device *pdev) { }
410814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par) { }
411814740e7SChen-Yu Tsai #endif
412814740e7SChen-Yu Tsai 
413f7018c21STomi Valkeinen static int simplefb_probe(struct platform_device *pdev)
414f7018c21STomi Valkeinen {
415f7018c21STomi Valkeinen 	int ret;
416f7018c21STomi Valkeinen 	struct simplefb_params params;
417f7018c21STomi Valkeinen 	struct fb_info *info;
4181270be4aSLuc Verhaegen 	struct simplefb_par *par;
419748bd587SThomas Zimmermann 	struct resource *res, *mem;
420f7018c21STomi Valkeinen 
421f7018c21STomi Valkeinen 	if (fb_get_options("simplefb", NULL))
422f7018c21STomi Valkeinen 		return -ENODEV;
423f7018c21STomi Valkeinen 
424f7018c21STomi Valkeinen 	ret = -ENODEV;
425f7018c21STomi Valkeinen 	if (dev_get_platdata(&pdev->dev))
426f7018c21STomi Valkeinen 		ret = simplefb_parse_pd(pdev, &params);
427f7018c21STomi Valkeinen 	else if (pdev->dev.of_node)
428f7018c21STomi Valkeinen 		ret = simplefb_parse_dt(pdev, &params);
429f7018c21STomi Valkeinen 
430f7018c21STomi Valkeinen 	if (ret)
431f7018c21STomi Valkeinen 		return ret;
432f7018c21STomi Valkeinen 
433748bd587SThomas Zimmermann 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
434748bd587SThomas Zimmermann 	if (!res) {
435f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "No memory resource\n");
436f7018c21STomi Valkeinen 		return -EINVAL;
437f7018c21STomi Valkeinen 	}
438f7018c21STomi Valkeinen 
439748bd587SThomas Zimmermann 	mem = request_mem_region(res->start, resource_size(res), "simplefb");
440748bd587SThomas Zimmermann 	if (!mem) {
441748bd587SThomas Zimmermann 		/*
442748bd587SThomas Zimmermann 		 * We cannot make this fatal. Sometimes this comes from magic
443748bd587SThomas Zimmermann 		 * spaces our resource handlers simply don't know about. Use
444748bd587SThomas Zimmermann 		 * the I/O-memory resource as-is and try to map that instead.
445748bd587SThomas Zimmermann 		 */
446748bd587SThomas Zimmermann 		dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res);
447748bd587SThomas Zimmermann 		mem = res;
448748bd587SThomas Zimmermann 	}
449748bd587SThomas Zimmermann 
4501270be4aSLuc Verhaegen 	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
451748bd587SThomas Zimmermann 	if (!info) {
452748bd587SThomas Zimmermann 		ret = -ENOMEM;
453748bd587SThomas Zimmermann 		goto error_release_mem_region;
454748bd587SThomas Zimmermann 	}
455f7018c21STomi Valkeinen 	platform_set_drvdata(pdev, info);
456f7018c21STomi Valkeinen 
4571270be4aSLuc Verhaegen 	par = info->par;
4581270be4aSLuc Verhaegen 
459f7018c21STomi Valkeinen 	info->fix = simplefb_fix;
460f7018c21STomi Valkeinen 	info->fix.smem_start = mem->start;
461f7018c21STomi Valkeinen 	info->fix.smem_len = resource_size(mem);
462f7018c21STomi Valkeinen 	info->fix.line_length = params.stride;
463f7018c21STomi Valkeinen 
464f7018c21STomi Valkeinen 	info->var = simplefb_var;
465f7018c21STomi Valkeinen 	info->var.xres = params.width;
466f7018c21STomi Valkeinen 	info->var.yres = params.height;
467f7018c21STomi Valkeinen 	info->var.xres_virtual = params.width;
468f7018c21STomi Valkeinen 	info->var.yres_virtual = params.height;
469f7018c21STomi Valkeinen 	info->var.bits_per_pixel = params.format->bits_per_pixel;
470f7018c21STomi Valkeinen 	info->var.red = params.format->red;
471f7018c21STomi Valkeinen 	info->var.green = params.format->green;
472f7018c21STomi Valkeinen 	info->var.blue = params.format->blue;
473f7018c21STomi Valkeinen 	info->var.transp = params.format->transp;
474f7018c21STomi Valkeinen 
475f7018c21STomi Valkeinen 	info->apertures = alloc_apertures(1);
476f7018c21STomi Valkeinen 	if (!info->apertures) {
477bf2fda15SLuc Verhaegen 		ret = -ENOMEM;
478bf2fda15SLuc Verhaegen 		goto error_fb_release;
479f7018c21STomi Valkeinen 	}
480f7018c21STomi Valkeinen 	info->apertures->ranges[0].base = info->fix.smem_start;
481f7018c21STomi Valkeinen 	info->apertures->ranges[0].size = info->fix.smem_len;
482f7018c21STomi Valkeinen 
483f7018c21STomi Valkeinen 	info->fbops = &simplefb_ops;
484f7018c21STomi Valkeinen 	info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
485f7018c21STomi Valkeinen 	info->screen_base = ioremap_wc(info->fix.smem_start,
486f7018c21STomi Valkeinen 				       info->fix.smem_len);
487f7018c21STomi Valkeinen 	if (!info->screen_base) {
488bf2fda15SLuc Verhaegen 		ret = -ENOMEM;
489bf2fda15SLuc Verhaegen 		goto error_fb_release;
490f7018c21STomi Valkeinen 	}
4911270be4aSLuc Verhaegen 	info->pseudo_palette = par->palette;
492f7018c21STomi Valkeinen 
493a3accfd7SHans de Goede 	ret = simplefb_clocks_get(par, pdev);
494fc219bfdSLuc Verhaegen 	if (ret < 0)
495fc219bfdSLuc Verhaegen 		goto error_unmap;
496fc219bfdSLuc Verhaegen 
497a3accfd7SHans de Goede 	ret = simplefb_regulators_get(par, pdev);
498814740e7SChen-Yu Tsai 	if (ret < 0)
499814740e7SChen-Yu Tsai 		goto error_clocks;
500814740e7SChen-Yu Tsai 
501a3accfd7SHans de Goede 	simplefb_clocks_enable(par, pdev);
502a3accfd7SHans de Goede 	simplefb_regulators_enable(par, pdev);
503a3accfd7SHans de Goede 
5045269a618SPeter Robinson 	dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n",
5055269a618SPeter Robinson 			     info->fix.smem_start, info->fix.smem_len);
506f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
507f7018c21STomi Valkeinen 			     params.format->name,
508f7018c21STomi Valkeinen 			     info->var.xres, info->var.yres,
509f7018c21STomi Valkeinen 			     info->var.bits_per_pixel, info->fix.line_length);
510f7018c21STomi Valkeinen 
511748bd587SThomas Zimmermann 	if (mem != res)
512748bd587SThomas Zimmermann 		par->mem = mem; /* release in clean-up handler */
513748bd587SThomas Zimmermann 
514f7018c21STomi Valkeinen 	ret = register_framebuffer(info);
515f7018c21STomi Valkeinen 	if (ret < 0) {
516f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
517814740e7SChen-Yu Tsai 		goto error_regulators;
518f7018c21STomi Valkeinen 	}
519f7018c21STomi Valkeinen 
520f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
521f7018c21STomi Valkeinen 
522f7018c21STomi Valkeinen 	return 0;
523bf2fda15SLuc Verhaegen 
524814740e7SChen-Yu Tsai error_regulators:
525814740e7SChen-Yu Tsai 	simplefb_regulators_destroy(par);
526fc219bfdSLuc Verhaegen error_clocks:
527fc219bfdSLuc Verhaegen 	simplefb_clocks_destroy(par);
528bf2fda15SLuc Verhaegen error_unmap:
529bf2fda15SLuc Verhaegen 	iounmap(info->screen_base);
530bf2fda15SLuc Verhaegen error_fb_release:
531bf2fda15SLuc Verhaegen 	framebuffer_release(info);
532748bd587SThomas Zimmermann error_release_mem_region:
533748bd587SThomas Zimmermann 	if (mem != res)
534748bd587SThomas Zimmermann 		release_mem_region(mem->start, resource_size(mem));
535bf2fda15SLuc Verhaegen 	return ret;
536f7018c21STomi Valkeinen }
537f7018c21STomi Valkeinen 
538f7018c21STomi Valkeinen static int simplefb_remove(struct platform_device *pdev)
539f7018c21STomi Valkeinen {
540f7018c21STomi Valkeinen 	struct fb_info *info = platform_get_drvdata(pdev);
541f7018c21STomi Valkeinen 
542666b90b3SJavier Martinez Canillas 	/* simplefb_destroy takes care of info cleanup */
543f7018c21STomi Valkeinen 	unregister_framebuffer(info);
544f7018c21STomi Valkeinen 
545f7018c21STomi Valkeinen 	return 0;
546f7018c21STomi Valkeinen }
547f7018c21STomi Valkeinen 
548f7018c21STomi Valkeinen static const struct of_device_id simplefb_of_match[] = {
549f7018c21STomi Valkeinen 	{ .compatible = "simple-framebuffer", },
550f7018c21STomi Valkeinen 	{ },
551f7018c21STomi Valkeinen };
552f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(of, simplefb_of_match);
553f7018c21STomi Valkeinen 
554f7018c21STomi Valkeinen static struct platform_driver simplefb_driver = {
555f7018c21STomi Valkeinen 	.driver = {
556f7018c21STomi Valkeinen 		.name = "simple-framebuffer",
557f7018c21STomi Valkeinen 		.of_match_table = simplefb_of_match,
558f7018c21STomi Valkeinen 	},
559f7018c21STomi Valkeinen 	.probe = simplefb_probe,
560f7018c21STomi Valkeinen 	.remove = simplefb_remove,
561f7018c21STomi Valkeinen };
56289c95001SHans de Goede 
5632f92ea21SHector Martin module_platform_driver(simplefb_driver);
564f7018c21STomi Valkeinen 
565f7018c21STomi Valkeinen MODULE_AUTHOR("Stephen Warren <[email protected]>");
566f7018c21STomi Valkeinen MODULE_DESCRIPTION("Simple framebuffer driver");
567f7018c21STomi Valkeinen MODULE_LICENSE("GPL v2");
568