xref: /linux-6.15/drivers/video/fbdev/simplefb.c (revision 367984eb)
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 
15d9702b2aSThomas Zimmermann #include <linux/aperture.h>
16f7018c21STomi Valkeinen #include <linux/errno.h>
17f7018c21STomi Valkeinen #include <linux/fb.h>
18f7018c21STomi Valkeinen #include <linux/io.h>
19f7018c21STomi Valkeinen #include <linux/module.h>
20f7018c21STomi Valkeinen #include <linux/platform_data/simplefb.h>
21f7018c21STomi Valkeinen #include <linux/platform_device.h>
22d49c6993SStephen Boyd #include <linux/clk.h>
23814740e7SChen-Yu Tsai #include <linux/of.h>
24db3b4fd1SGeert Uytterhoeven #include <linux/of_clk.h>
2589c95001SHans de Goede #include <linux/of_platform.h>
26814740e7SChen-Yu Tsai #include <linux/parser.h>
27814740e7SChen-Yu Tsai #include <linux/regulator/consumer.h>
28f7018c21STomi Valkeinen 
29ca9384c5SJulia Lawall static const struct fb_fix_screeninfo simplefb_fix = {
30f7018c21STomi Valkeinen 	.id		= "simple",
31f7018c21STomi Valkeinen 	.type		= FB_TYPE_PACKED_PIXELS,
32f7018c21STomi Valkeinen 	.visual		= FB_VISUAL_TRUECOLOR,
33f7018c21STomi Valkeinen 	.accel		= FB_ACCEL_NONE,
34f7018c21STomi Valkeinen };
35f7018c21STomi Valkeinen 
36ca9384c5SJulia Lawall static const struct fb_var_screeninfo simplefb_var = {
37f7018c21STomi Valkeinen 	.height		= -1,
38f7018c21STomi Valkeinen 	.width		= -1,
39f7018c21STomi Valkeinen 	.activate	= FB_ACTIVATE_NOW,
40f7018c21STomi Valkeinen 	.vmode		= FB_VMODE_NONINTERLACED,
41f7018c21STomi Valkeinen };
42f7018c21STomi Valkeinen 
431270be4aSLuc Verhaegen #define PSEUDO_PALETTE_SIZE 16
441270be4aSLuc Verhaegen 
45f7018c21STomi Valkeinen static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
46f7018c21STomi Valkeinen 			      u_int transp, struct fb_info *info)
47f7018c21STomi Valkeinen {
48f7018c21STomi Valkeinen 	u32 *pal = info->pseudo_palette;
49f7018c21STomi Valkeinen 	u32 cr = red >> (16 - info->var.red.length);
50f7018c21STomi Valkeinen 	u32 cg = green >> (16 - info->var.green.length);
51f7018c21STomi Valkeinen 	u32 cb = blue >> (16 - info->var.blue.length);
52f7018c21STomi Valkeinen 	u32 value;
53f7018c21STomi Valkeinen 
541270be4aSLuc Verhaegen 	if (regno >= PSEUDO_PALETTE_SIZE)
55f7018c21STomi Valkeinen 		return -EINVAL;
56f7018c21STomi Valkeinen 
57f7018c21STomi Valkeinen 	value = (cr << info->var.red.offset) |
58f7018c21STomi Valkeinen 		(cg << info->var.green.offset) |
59f7018c21STomi Valkeinen 		(cb << info->var.blue.offset);
60f7018c21STomi Valkeinen 	if (info->var.transp.length > 0) {
61f7018c21STomi Valkeinen 		u32 mask = (1 << info->var.transp.length) - 1;
62f7018c21STomi Valkeinen 		mask <<= info->var.transp.offset;
63f7018c21STomi Valkeinen 		value |= mask;
64f7018c21STomi Valkeinen 	}
65f7018c21STomi Valkeinen 	pal[regno] = value;
66f7018c21STomi Valkeinen 
67f7018c21STomi Valkeinen 	return 0;
68f7018c21STomi Valkeinen }
69f7018c21STomi Valkeinen 
70748bd587SThomas Zimmermann struct simplefb_par {
71748bd587SThomas Zimmermann 	u32 palette[PSEUDO_PALETTE_SIZE];
72d9702b2aSThomas Zimmermann 	resource_size_t base;
73d9702b2aSThomas Zimmermann 	resource_size_t size;
74748bd587SThomas Zimmermann 	struct resource *mem;
75748bd587SThomas Zimmermann #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
76748bd587SThomas Zimmermann 	bool clks_enabled;
77748bd587SThomas Zimmermann 	unsigned int clk_count;
78748bd587SThomas Zimmermann 	struct clk **clks;
79748bd587SThomas Zimmermann #endif
80748bd587SThomas Zimmermann #if defined CONFIG_OF && defined CONFIG_REGULATOR
81748bd587SThomas Zimmermann 	bool regulators_enabled;
82748bd587SThomas Zimmermann 	u32 regulator_count;
83748bd587SThomas Zimmermann 	struct regulator **regulators;
84748bd587SThomas Zimmermann #endif
85748bd587SThomas Zimmermann };
86748bd587SThomas Zimmermann 
877c9806e8SChen-Yu Tsai static void simplefb_clocks_destroy(struct simplefb_par *par);
887c9806e8SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par);
897c9806e8SChen-Yu Tsai 
90666b90b3SJavier Martinez Canillas /*
91666b90b3SJavier Martinez Canillas  * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
92666b90b3SJavier Martinez Canillas  * of unregister_framebuffer() or fb_release(). Do any cleanup here.
93666b90b3SJavier Martinez Canillas  */
94f7018c21STomi Valkeinen static void simplefb_destroy(struct fb_info *info)
95f7018c21STomi Valkeinen {
96748bd587SThomas Zimmermann 	struct simplefb_par *par = info->par;
97748bd587SThomas Zimmermann 	struct resource *mem = par->mem;
98748bd587SThomas Zimmermann 
997c9806e8SChen-Yu Tsai 	simplefb_regulators_destroy(info->par);
1007c9806e8SChen-Yu Tsai 	simplefb_clocks_destroy(info->par);
101f7018c21STomi Valkeinen 	if (info->screen_base)
102f7018c21STomi Valkeinen 		iounmap(info->screen_base);
103748bd587SThomas Zimmermann 
104666b90b3SJavier Martinez Canillas 	framebuffer_release(info);
105666b90b3SJavier Martinez Canillas 
106748bd587SThomas Zimmermann 	if (mem)
107748bd587SThomas Zimmermann 		release_mem_region(mem->start, resource_size(mem));
108f7018c21STomi Valkeinen }
109f7018c21STomi Valkeinen 
1108a48ac33SJani Nikula static const struct fb_ops simplefb_ops = {
111f7018c21STomi Valkeinen 	.owner		= THIS_MODULE,
112*367984ebSThomas Zimmermann 	FB_DEFAULT_IOMEM_OPS,
113f7018c21STomi Valkeinen 	.fb_destroy	= simplefb_destroy,
114f7018c21STomi Valkeinen 	.fb_setcolreg	= simplefb_setcolreg,
115f7018c21STomi Valkeinen };
116f7018c21STomi Valkeinen 
117f7018c21STomi Valkeinen static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
118f7018c21STomi Valkeinen 
119f7018c21STomi Valkeinen struct simplefb_params {
120f7018c21STomi Valkeinen 	u32 width;
121f7018c21STomi Valkeinen 	u32 height;
122f7018c21STomi Valkeinen 	u32 stride;
123f7018c21STomi Valkeinen 	struct simplefb_format *format;
124f7018c21STomi Valkeinen };
125f7018c21STomi Valkeinen 
126f7018c21STomi Valkeinen static int simplefb_parse_dt(struct platform_device *pdev,
127f7018c21STomi Valkeinen 			   struct simplefb_params *params)
128f7018c21STomi Valkeinen {
129f7018c21STomi Valkeinen 	struct device_node *np = pdev->dev.of_node;
130f7018c21STomi Valkeinen 	int ret;
131f7018c21STomi Valkeinen 	const char *format;
132f7018c21STomi Valkeinen 	int i;
133f7018c21STomi Valkeinen 
134f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "width", &params->width);
135f7018c21STomi Valkeinen 	if (ret) {
136f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse width property\n");
137f7018c21STomi Valkeinen 		return ret;
138f7018c21STomi Valkeinen 	}
139f7018c21STomi Valkeinen 
140f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "height", &params->height);
141f7018c21STomi Valkeinen 	if (ret) {
142f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse height property\n");
143f7018c21STomi Valkeinen 		return ret;
144f7018c21STomi Valkeinen 	}
145f7018c21STomi Valkeinen 
146f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "stride", &params->stride);
147f7018c21STomi Valkeinen 	if (ret) {
148f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse stride property\n");
149f7018c21STomi Valkeinen 		return ret;
150f7018c21STomi Valkeinen 	}
151f7018c21STomi Valkeinen 
152f7018c21STomi Valkeinen 	ret = of_property_read_string(np, "format", &format);
153f7018c21STomi Valkeinen 	if (ret) {
154f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse format property\n");
155f7018c21STomi Valkeinen 		return ret;
156f7018c21STomi Valkeinen 	}
157f7018c21STomi Valkeinen 	params->format = NULL;
158f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
159f7018c21STomi Valkeinen 		if (strcmp(format, simplefb_formats[i].name))
160f7018c21STomi Valkeinen 			continue;
161f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
162f7018c21STomi Valkeinen 		break;
163f7018c21STomi Valkeinen 	}
164f7018c21STomi Valkeinen 	if (!params->format) {
165f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
166f7018c21STomi Valkeinen 		return -EINVAL;
167f7018c21STomi Valkeinen 	}
168f7018c21STomi Valkeinen 
169f7018c21STomi Valkeinen 	return 0;
170f7018c21STomi Valkeinen }
171f7018c21STomi Valkeinen 
172f7018c21STomi Valkeinen static int simplefb_parse_pd(struct platform_device *pdev,
173f7018c21STomi Valkeinen 			     struct simplefb_params *params)
174f7018c21STomi Valkeinen {
175f7018c21STomi Valkeinen 	struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
176f7018c21STomi Valkeinen 	int i;
177f7018c21STomi Valkeinen 
178f7018c21STomi Valkeinen 	params->width = pd->width;
179f7018c21STomi Valkeinen 	params->height = pd->height;
180f7018c21STomi Valkeinen 	params->stride = pd->stride;
181f7018c21STomi Valkeinen 
182f7018c21STomi Valkeinen 	params->format = NULL;
183f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
184f7018c21STomi Valkeinen 		if (strcmp(pd->format, simplefb_formats[i].name))
185f7018c21STomi Valkeinen 			continue;
186f7018c21STomi Valkeinen 
187f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
188f7018c21STomi Valkeinen 		break;
189f7018c21STomi Valkeinen 	}
190f7018c21STomi Valkeinen 
191f7018c21STomi Valkeinen 	if (!params->format) {
192f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
193f7018c21STomi Valkeinen 		return -EINVAL;
194f7018c21STomi Valkeinen 	}
195f7018c21STomi Valkeinen 
196f7018c21STomi Valkeinen 	return 0;
197f7018c21STomi Valkeinen }
198f7018c21STomi Valkeinen 
1998284731eSHans de Goede #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
200fc219bfdSLuc Verhaegen /*
201fc219bfdSLuc Verhaegen  * Clock handling code.
202fc219bfdSLuc Verhaegen  *
203fc219bfdSLuc Verhaegen  * Here we handle the clocks property of our "simple-framebuffer" dt node.
204fc219bfdSLuc Verhaegen  * This is necessary so that we can make sure that any clocks needed by
205fc219bfdSLuc Verhaegen  * the display engine that the bootloader set up for us (and for which it
206fc219bfdSLuc Verhaegen  * provided a simplefb dt node), stay up, for the life of the simplefb
207fc219bfdSLuc Verhaegen  * driver.
208fc219bfdSLuc Verhaegen  *
209fc219bfdSLuc Verhaegen  * When the driver unloads, we cleanly disable, and then release the clocks.
210fc219bfdSLuc Verhaegen  *
211fc219bfdSLuc Verhaegen  * We only complain about errors here, no action is taken as the most likely
212fc219bfdSLuc Verhaegen  * error can only happen due to a mismatch between the bootloader which set
213fc219bfdSLuc Verhaegen  * up simplefb, and the clock definitions in the device tree. Chances are
214fc219bfdSLuc Verhaegen  * that there are no adverse effects, and if there are, a clean teardown of
215fc219bfdSLuc Verhaegen  * the fb probe will not help us much either. So just complain and carry on,
216fc219bfdSLuc Verhaegen  * and hope that the user actually gets a working fb at the end of things.
217fc219bfdSLuc Verhaegen  */
218a3accfd7SHans de Goede static int simplefb_clocks_get(struct simplefb_par *par,
219fc219bfdSLuc Verhaegen 			       struct platform_device *pdev)
220fc219bfdSLuc Verhaegen {
221fc219bfdSLuc Verhaegen 	struct device_node *np = pdev->dev.of_node;
222fc219bfdSLuc Verhaegen 	struct clk *clock;
223a3accfd7SHans de Goede 	int i;
224fc219bfdSLuc Verhaegen 
225fc219bfdSLuc Verhaegen 	if (dev_get_platdata(&pdev->dev) || !np)
226fc219bfdSLuc Verhaegen 		return 0;
227fc219bfdSLuc Verhaegen 
228fc219bfdSLuc Verhaegen 	par->clk_count = of_clk_get_parent_count(np);
2292cd82d06SStephen Boyd 	if (!par->clk_count)
230fc219bfdSLuc Verhaegen 		return 0;
231fc219bfdSLuc Verhaegen 
232fc219bfdSLuc Verhaegen 	par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
233fc219bfdSLuc Verhaegen 	if (!par->clks)
234fc219bfdSLuc Verhaegen 		return -ENOMEM;
235fc219bfdSLuc Verhaegen 
236fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
237fc219bfdSLuc Verhaegen 		clock = of_clk_get(np, i);
238fc219bfdSLuc Verhaegen 		if (IS_ERR(clock)) {
239fc219bfdSLuc Verhaegen 			if (PTR_ERR(clock) == -EPROBE_DEFER) {
240fc219bfdSLuc Verhaegen 				while (--i >= 0) {
241fc219bfdSLuc Verhaegen 					clk_put(par->clks[i]);
242fc219bfdSLuc Verhaegen 				}
243fc219bfdSLuc Verhaegen 				kfree(par->clks);
244fc219bfdSLuc Verhaegen 				return -EPROBE_DEFER;
245fc219bfdSLuc Verhaegen 			}
246fc219bfdSLuc Verhaegen 			dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
247fc219bfdSLuc Verhaegen 				__func__, i, PTR_ERR(clock));
248fc219bfdSLuc Verhaegen 			continue;
249fc219bfdSLuc Verhaegen 		}
250fc219bfdSLuc Verhaegen 		par->clks[i] = clock;
251fc219bfdSLuc Verhaegen 	}
252fc219bfdSLuc Verhaegen 
253a3accfd7SHans de Goede 	return 0;
254a3accfd7SHans de Goede }
255a3accfd7SHans de Goede 
256a3accfd7SHans de Goede static void simplefb_clocks_enable(struct simplefb_par *par,
257a3accfd7SHans de Goede 				   struct platform_device *pdev)
258a3accfd7SHans de Goede {
259a3accfd7SHans de Goede 	int i, ret;
260a3accfd7SHans de Goede 
261fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
262fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
263fc219bfdSLuc Verhaegen 			ret = clk_prepare_enable(par->clks[i]);
264fc219bfdSLuc Verhaegen 			if (ret) {
265fc219bfdSLuc Verhaegen 				dev_err(&pdev->dev,
266fc219bfdSLuc Verhaegen 					"%s: failed to enable clock %d: %d\n",
267fc219bfdSLuc Verhaegen 					__func__, i, ret);
268fc219bfdSLuc Verhaegen 				clk_put(par->clks[i]);
269fc219bfdSLuc Verhaegen 				par->clks[i] = NULL;
270fc219bfdSLuc Verhaegen 			}
271fc219bfdSLuc Verhaegen 		}
272fc219bfdSLuc Verhaegen 	}
273a3accfd7SHans de Goede 	par->clks_enabled = true;
274fc219bfdSLuc Verhaegen }
275fc219bfdSLuc Verhaegen 
276fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par)
277fc219bfdSLuc Verhaegen {
278fc219bfdSLuc Verhaegen 	int i;
279fc219bfdSLuc Verhaegen 
280fc219bfdSLuc Verhaegen 	if (!par->clks)
281fc219bfdSLuc Verhaegen 		return;
282fc219bfdSLuc Verhaegen 
283fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
284fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
285a3accfd7SHans de Goede 			if (par->clks_enabled)
286fc219bfdSLuc Verhaegen 				clk_disable_unprepare(par->clks[i]);
287fc219bfdSLuc Verhaegen 			clk_put(par->clks[i]);
288fc219bfdSLuc Verhaegen 		}
289fc219bfdSLuc Verhaegen 	}
290fc219bfdSLuc Verhaegen 
291fc219bfdSLuc Verhaegen 	kfree(par->clks);
292fc219bfdSLuc Verhaegen }
293fc219bfdSLuc Verhaegen #else
294a3accfd7SHans de Goede static int simplefb_clocks_get(struct simplefb_par *par,
295fc219bfdSLuc Verhaegen 	struct platform_device *pdev) { return 0; }
296a3accfd7SHans de Goede static void simplefb_clocks_enable(struct simplefb_par *par,
297a3accfd7SHans de Goede 	struct platform_device *pdev) { }
298fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par) { }
299fc219bfdSLuc Verhaegen #endif
300fc219bfdSLuc Verhaegen 
301814740e7SChen-Yu Tsai #if defined CONFIG_OF && defined CONFIG_REGULATOR
302814740e7SChen-Yu Tsai 
303814740e7SChen-Yu Tsai #define SUPPLY_SUFFIX "-supply"
304814740e7SChen-Yu Tsai 
305814740e7SChen-Yu Tsai /*
306814740e7SChen-Yu Tsai  * Regulator handling code.
307814740e7SChen-Yu Tsai  *
308814740e7SChen-Yu Tsai  * Here we handle the num-supplies and vin*-supply properties of our
309814740e7SChen-Yu Tsai  * "simple-framebuffer" dt node. This is necessary so that we can make sure
310814740e7SChen-Yu Tsai  * that any regulators needed by the display hardware that the bootloader
311814740e7SChen-Yu Tsai  * set up for us (and for which it provided a simplefb dt node), stay up,
312814740e7SChen-Yu Tsai  * for the life of the simplefb driver.
313814740e7SChen-Yu Tsai  *
314814740e7SChen-Yu Tsai  * When the driver unloads, we cleanly disable, and then release the
315814740e7SChen-Yu Tsai  * regulators.
316814740e7SChen-Yu Tsai  *
317814740e7SChen-Yu Tsai  * We only complain about errors here, no action is taken as the most likely
318814740e7SChen-Yu Tsai  * error can only happen due to a mismatch between the bootloader which set
319814740e7SChen-Yu Tsai  * up simplefb, and the regulator definitions in the device tree. Chances are
320814740e7SChen-Yu Tsai  * that there are no adverse effects, and if there are, a clean teardown of
321814740e7SChen-Yu Tsai  * the fb probe will not help us much either. So just complain and carry on,
322814740e7SChen-Yu Tsai  * and hope that the user actually gets a working fb at the end of things.
323814740e7SChen-Yu Tsai  */
324a3accfd7SHans de Goede static int simplefb_regulators_get(struct simplefb_par *par,
325814740e7SChen-Yu Tsai 				   struct platform_device *pdev)
326814740e7SChen-Yu Tsai {
327814740e7SChen-Yu Tsai 	struct device_node *np = pdev->dev.of_node;
328814740e7SChen-Yu Tsai 	struct property *prop;
329814740e7SChen-Yu Tsai 	struct regulator *regulator;
330814740e7SChen-Yu Tsai 	const char *p;
331a3accfd7SHans de Goede 	int count = 0, i = 0;
332814740e7SChen-Yu Tsai 
333814740e7SChen-Yu Tsai 	if (dev_get_platdata(&pdev->dev) || !np)
334814740e7SChen-Yu Tsai 		return 0;
335814740e7SChen-Yu Tsai 
336814740e7SChen-Yu Tsai 	/* Count the number of regulator supplies */
337814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
338814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
339814740e7SChen-Yu Tsai 		if (p && p != prop->name)
340814740e7SChen-Yu Tsai 			count++;
341814740e7SChen-Yu Tsai 	}
342814740e7SChen-Yu Tsai 
343814740e7SChen-Yu Tsai 	if (!count)
344814740e7SChen-Yu Tsai 		return 0;
345814740e7SChen-Yu Tsai 
346814740e7SChen-Yu Tsai 	par->regulators = devm_kcalloc(&pdev->dev, count,
347814740e7SChen-Yu Tsai 				       sizeof(struct regulator *), GFP_KERNEL);
348814740e7SChen-Yu Tsai 	if (!par->regulators)
349814740e7SChen-Yu Tsai 		return -ENOMEM;
350814740e7SChen-Yu Tsai 
351814740e7SChen-Yu Tsai 	/* Get all the regulators */
352814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
353814740e7SChen-Yu Tsai 		char name[32]; /* 32 is max size of property name */
354814740e7SChen-Yu Tsai 
355814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
356814740e7SChen-Yu Tsai 		if (!p || p == prop->name)
357814740e7SChen-Yu Tsai 			continue;
358814740e7SChen-Yu Tsai 
3598d026858SWolfram Sang 		strscpy(name, prop->name,
360814740e7SChen-Yu Tsai 			strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
361814740e7SChen-Yu Tsai 		regulator = devm_regulator_get_optional(&pdev->dev, name);
362814740e7SChen-Yu Tsai 		if (IS_ERR(regulator)) {
363814740e7SChen-Yu Tsai 			if (PTR_ERR(regulator) == -EPROBE_DEFER)
364814740e7SChen-Yu Tsai 				return -EPROBE_DEFER;
365814740e7SChen-Yu Tsai 			dev_err(&pdev->dev, "regulator %s not found: %ld\n",
366814740e7SChen-Yu Tsai 				name, PTR_ERR(regulator));
367814740e7SChen-Yu Tsai 			continue;
368814740e7SChen-Yu Tsai 		}
369814740e7SChen-Yu Tsai 		par->regulators[i++] = regulator;
370814740e7SChen-Yu Tsai 	}
371814740e7SChen-Yu Tsai 	par->regulator_count = i;
372814740e7SChen-Yu Tsai 
373a3accfd7SHans de Goede 	return 0;
374a3accfd7SHans de Goede }
375a3accfd7SHans de Goede 
376a3accfd7SHans de Goede static void simplefb_regulators_enable(struct simplefb_par *par,
377a3accfd7SHans de Goede 				       struct platform_device *pdev)
378a3accfd7SHans de Goede {
379a3accfd7SHans de Goede 	int i, ret;
380a3accfd7SHans de Goede 
381814740e7SChen-Yu Tsai 	/* Enable all the regulators */
382814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++) {
383814740e7SChen-Yu Tsai 		ret = regulator_enable(par->regulators[i]);
384814740e7SChen-Yu Tsai 		if (ret) {
385814740e7SChen-Yu Tsai 			dev_err(&pdev->dev,
386814740e7SChen-Yu Tsai 				"failed to enable regulator %d: %d\n",
387814740e7SChen-Yu Tsai 				i, ret);
388814740e7SChen-Yu Tsai 			devm_regulator_put(par->regulators[i]);
389814740e7SChen-Yu Tsai 			par->regulators[i] = NULL;
390814740e7SChen-Yu Tsai 		}
391814740e7SChen-Yu Tsai 	}
392a3accfd7SHans de Goede 	par->regulators_enabled = true;
393814740e7SChen-Yu Tsai }
394814740e7SChen-Yu Tsai 
395814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par)
396814740e7SChen-Yu Tsai {
397814740e7SChen-Yu Tsai 	int i;
398814740e7SChen-Yu Tsai 
399a3accfd7SHans de Goede 	if (!par->regulators || !par->regulators_enabled)
400814740e7SChen-Yu Tsai 		return;
401814740e7SChen-Yu Tsai 
402814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++)
403814740e7SChen-Yu Tsai 		if (par->regulators[i])
404814740e7SChen-Yu Tsai 			regulator_disable(par->regulators[i]);
405814740e7SChen-Yu Tsai }
406814740e7SChen-Yu Tsai #else
407a3accfd7SHans de Goede static int simplefb_regulators_get(struct simplefb_par *par,
408814740e7SChen-Yu Tsai 	struct platform_device *pdev) { return 0; }
409a3accfd7SHans de Goede static void simplefb_regulators_enable(struct simplefb_par *par,
410a3accfd7SHans de Goede 	struct platform_device *pdev) { }
411814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par) { }
412814740e7SChen-Yu Tsai #endif
413814740e7SChen-Yu Tsai 
414f7018c21STomi Valkeinen static int simplefb_probe(struct platform_device *pdev)
415f7018c21STomi Valkeinen {
416f7018c21STomi Valkeinen 	int ret;
417f7018c21STomi Valkeinen 	struct simplefb_params params;
418f7018c21STomi Valkeinen 	struct fb_info *info;
4191270be4aSLuc Verhaegen 	struct simplefb_par *par;
420748bd587SThomas Zimmermann 	struct resource *res, *mem;
421f7018c21STomi Valkeinen 
422f7018c21STomi Valkeinen 	if (fb_get_options("simplefb", NULL))
423f7018c21STomi Valkeinen 		return -ENODEV;
424f7018c21STomi Valkeinen 
425f7018c21STomi Valkeinen 	ret = -ENODEV;
426f7018c21STomi Valkeinen 	if (dev_get_platdata(&pdev->dev))
427f7018c21STomi Valkeinen 		ret = simplefb_parse_pd(pdev, &params);
428f7018c21STomi Valkeinen 	else if (pdev->dev.of_node)
429f7018c21STomi Valkeinen 		ret = simplefb_parse_dt(pdev, &params);
430f7018c21STomi Valkeinen 
431f7018c21STomi Valkeinen 	if (ret)
432f7018c21STomi Valkeinen 		return ret;
433f7018c21STomi Valkeinen 
434748bd587SThomas Zimmermann 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
435748bd587SThomas Zimmermann 	if (!res) {
436f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "No memory resource\n");
437f7018c21STomi Valkeinen 		return -EINVAL;
438f7018c21STomi Valkeinen 	}
439f7018c21STomi Valkeinen 
440748bd587SThomas Zimmermann 	mem = request_mem_region(res->start, resource_size(res), "simplefb");
441748bd587SThomas Zimmermann 	if (!mem) {
442748bd587SThomas Zimmermann 		/*
443748bd587SThomas Zimmermann 		 * We cannot make this fatal. Sometimes this comes from magic
444748bd587SThomas Zimmermann 		 * spaces our resource handlers simply don't know about. Use
445748bd587SThomas Zimmermann 		 * the I/O-memory resource as-is and try to map that instead.
446748bd587SThomas Zimmermann 		 */
447748bd587SThomas Zimmermann 		dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res);
448748bd587SThomas Zimmermann 		mem = res;
449748bd587SThomas Zimmermann 	}
450748bd587SThomas Zimmermann 
4511270be4aSLuc Verhaegen 	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
452748bd587SThomas Zimmermann 	if (!info) {
453748bd587SThomas Zimmermann 		ret = -ENOMEM;
454748bd587SThomas Zimmermann 		goto error_release_mem_region;
455748bd587SThomas Zimmermann 	}
456f7018c21STomi Valkeinen 	platform_set_drvdata(pdev, info);
457f7018c21STomi Valkeinen 
4581270be4aSLuc Verhaegen 	par = info->par;
4591270be4aSLuc Verhaegen 
460f7018c21STomi Valkeinen 	info->fix = simplefb_fix;
461f7018c21STomi Valkeinen 	info->fix.smem_start = mem->start;
462f7018c21STomi Valkeinen 	info->fix.smem_len = resource_size(mem);
463f7018c21STomi Valkeinen 	info->fix.line_length = params.stride;
464f7018c21STomi Valkeinen 
465f7018c21STomi Valkeinen 	info->var = simplefb_var;
466f7018c21STomi Valkeinen 	info->var.xres = params.width;
467f7018c21STomi Valkeinen 	info->var.yres = params.height;
468f7018c21STomi Valkeinen 	info->var.xres_virtual = params.width;
469f7018c21STomi Valkeinen 	info->var.yres_virtual = params.height;
470f7018c21STomi Valkeinen 	info->var.bits_per_pixel = params.format->bits_per_pixel;
471f7018c21STomi Valkeinen 	info->var.red = params.format->red;
472f7018c21STomi Valkeinen 	info->var.green = params.format->green;
473f7018c21STomi Valkeinen 	info->var.blue = params.format->blue;
474f7018c21STomi Valkeinen 	info->var.transp = params.format->transp;
475f7018c21STomi Valkeinen 
476d9702b2aSThomas Zimmermann 	par->base = info->fix.smem_start;
477d9702b2aSThomas Zimmermann 	par->size = info->fix.smem_len;
478f7018c21STomi Valkeinen 
479f7018c21STomi Valkeinen 	info->fbops = &simplefb_ops;
480f7018c21STomi Valkeinen 	info->screen_base = ioremap_wc(info->fix.smem_start,
481f7018c21STomi Valkeinen 				       info->fix.smem_len);
482f7018c21STomi Valkeinen 	if (!info->screen_base) {
483bf2fda15SLuc Verhaegen 		ret = -ENOMEM;
484bf2fda15SLuc Verhaegen 		goto error_fb_release;
485f7018c21STomi Valkeinen 	}
4861270be4aSLuc Verhaegen 	info->pseudo_palette = par->palette;
487f7018c21STomi Valkeinen 
488a3accfd7SHans de Goede 	ret = simplefb_clocks_get(par, pdev);
489fc219bfdSLuc Verhaegen 	if (ret < 0)
490fc219bfdSLuc Verhaegen 		goto error_unmap;
491fc219bfdSLuc Verhaegen 
492a3accfd7SHans de Goede 	ret = simplefb_regulators_get(par, pdev);
493814740e7SChen-Yu Tsai 	if (ret < 0)
494814740e7SChen-Yu Tsai 		goto error_clocks;
495814740e7SChen-Yu Tsai 
496a3accfd7SHans de Goede 	simplefb_clocks_enable(par, pdev);
497a3accfd7SHans de Goede 	simplefb_regulators_enable(par, pdev);
498a3accfd7SHans de Goede 
4995269a618SPeter Robinson 	dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n",
5005269a618SPeter Robinson 			     info->fix.smem_start, info->fix.smem_len);
501f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
502f7018c21STomi Valkeinen 			     params.format->name,
503f7018c21STomi Valkeinen 			     info->var.xres, info->var.yres,
504f7018c21STomi Valkeinen 			     info->var.bits_per_pixel, info->fix.line_length);
505f7018c21STomi Valkeinen 
506748bd587SThomas Zimmermann 	if (mem != res)
507748bd587SThomas Zimmermann 		par->mem = mem; /* release in clean-up handler */
508748bd587SThomas Zimmermann 
509d9702b2aSThomas Zimmermann 	ret = devm_aperture_acquire_for_platform_device(pdev, par->base, par->size);
510d9702b2aSThomas Zimmermann 	if (ret) {
511d9702b2aSThomas Zimmermann 		dev_err(&pdev->dev, "Unable to acquire aperture: %d\n", ret);
512d9702b2aSThomas Zimmermann 		goto error_regulators;
513d9702b2aSThomas 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 
5384ba34066SUwe Kleine-König static void 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 
546f7018c21STomi Valkeinen static const struct of_device_id simplefb_of_match[] = {
547f7018c21STomi Valkeinen 	{ .compatible = "simple-framebuffer", },
548f7018c21STomi Valkeinen 	{ },
549f7018c21STomi Valkeinen };
550f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(of, simplefb_of_match);
551f7018c21STomi Valkeinen 
552f7018c21STomi Valkeinen static struct platform_driver simplefb_driver = {
553f7018c21STomi Valkeinen 	.driver = {
554f7018c21STomi Valkeinen 		.name = "simple-framebuffer",
555f7018c21STomi Valkeinen 		.of_match_table = simplefb_of_match,
556f7018c21STomi Valkeinen 	},
557f7018c21STomi Valkeinen 	.probe = simplefb_probe,
5584ba34066SUwe Kleine-König 	.remove_new = simplefb_remove,
559f7018c21STomi Valkeinen };
56089c95001SHans de Goede 
5612f92ea21SHector Martin module_platform_driver(simplefb_driver);
562f7018c21STomi Valkeinen 
563f7018c21STomi Valkeinen MODULE_AUTHOR("Stephen Warren <[email protected]>");
564f7018c21STomi Valkeinen MODULE_DESCRIPTION("Simple framebuffer driver");
565f7018c21STomi Valkeinen MODULE_LICENSE("GPL v2");
566