xref: /linux-6.15/drivers/video/fbdev/simplefb.c (revision a3accfd7)
1f7018c21STomi Valkeinen /*
2f7018c21STomi Valkeinen  * Simplest possible simple frame-buffer driver, as a platform device
3f7018c21STomi Valkeinen  *
4f7018c21STomi Valkeinen  * Copyright (c) 2013, Stephen Warren
5f7018c21STomi Valkeinen  *
6f7018c21STomi Valkeinen  * Based on q40fb.c, which was:
7f7018c21STomi Valkeinen  * Copyright (C) 2001 Richard Zidlicky <[email protected]>
8f7018c21STomi Valkeinen  *
9f7018c21STomi Valkeinen  * Also based on offb.c, which was:
10f7018c21STomi Valkeinen  * Copyright (C) 1997 Geert Uytterhoeven
11f7018c21STomi Valkeinen  * Copyright (C) 1996 Paul Mackerras
12f7018c21STomi Valkeinen  *
13f7018c21STomi Valkeinen  * This program is free software; you can redistribute it and/or modify it
14f7018c21STomi Valkeinen  * under the terms and conditions of the GNU General Public License,
15f7018c21STomi Valkeinen  * version 2, as published by the Free Software Foundation.
16f7018c21STomi Valkeinen  *
17f7018c21STomi Valkeinen  * This program is distributed in the hope it will be useful, but WITHOUT
18f7018c21STomi Valkeinen  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19f7018c21STomi Valkeinen  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
20f7018c21STomi Valkeinen  * more details.
21f7018c21STomi Valkeinen  */
22f7018c21STomi Valkeinen 
23f7018c21STomi Valkeinen #include <linux/errno.h>
24f7018c21STomi Valkeinen #include <linux/fb.h>
25f7018c21STomi Valkeinen #include <linux/io.h>
26f7018c21STomi Valkeinen #include <linux/module.h>
27f7018c21STomi Valkeinen #include <linux/platform_data/simplefb.h>
28f7018c21STomi Valkeinen #include <linux/platform_device.h>
29d49c6993SStephen Boyd #include <linux/clk.h>
30fc219bfdSLuc Verhaegen #include <linux/clk-provider.h>
31814740e7SChen-Yu Tsai #include <linux/of.h>
3289c95001SHans de Goede #include <linux/of_platform.h>
33814740e7SChen-Yu Tsai #include <linux/parser.h>
34814740e7SChen-Yu Tsai #include <linux/regulator/consumer.h>
35f7018c21STomi Valkeinen 
36ca9384c5SJulia Lawall static const struct fb_fix_screeninfo simplefb_fix = {
37f7018c21STomi Valkeinen 	.id		= "simple",
38f7018c21STomi Valkeinen 	.type		= FB_TYPE_PACKED_PIXELS,
39f7018c21STomi Valkeinen 	.visual		= FB_VISUAL_TRUECOLOR,
40f7018c21STomi Valkeinen 	.accel		= FB_ACCEL_NONE,
41f7018c21STomi Valkeinen };
42f7018c21STomi Valkeinen 
43ca9384c5SJulia Lawall static const struct fb_var_screeninfo simplefb_var = {
44f7018c21STomi Valkeinen 	.height		= -1,
45f7018c21STomi Valkeinen 	.width		= -1,
46f7018c21STomi Valkeinen 	.activate	= FB_ACTIVATE_NOW,
47f7018c21STomi Valkeinen 	.vmode		= FB_VMODE_NONINTERLACED,
48f7018c21STomi Valkeinen };
49f7018c21STomi Valkeinen 
501270be4aSLuc Verhaegen #define PSEUDO_PALETTE_SIZE 16
511270be4aSLuc Verhaegen 
52f7018c21STomi Valkeinen static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
53f7018c21STomi Valkeinen 			      u_int transp, struct fb_info *info)
54f7018c21STomi Valkeinen {
55f7018c21STomi Valkeinen 	u32 *pal = info->pseudo_palette;
56f7018c21STomi Valkeinen 	u32 cr = red >> (16 - info->var.red.length);
57f7018c21STomi Valkeinen 	u32 cg = green >> (16 - info->var.green.length);
58f7018c21STomi Valkeinen 	u32 cb = blue >> (16 - info->var.blue.length);
59f7018c21STomi Valkeinen 	u32 value;
60f7018c21STomi Valkeinen 
611270be4aSLuc Verhaegen 	if (regno >= PSEUDO_PALETTE_SIZE)
62f7018c21STomi Valkeinen 		return -EINVAL;
63f7018c21STomi Valkeinen 
64f7018c21STomi Valkeinen 	value = (cr << info->var.red.offset) |
65f7018c21STomi Valkeinen 		(cg << info->var.green.offset) |
66f7018c21STomi Valkeinen 		(cb << info->var.blue.offset);
67f7018c21STomi Valkeinen 	if (info->var.transp.length > 0) {
68f7018c21STomi Valkeinen 		u32 mask = (1 << info->var.transp.length) - 1;
69f7018c21STomi Valkeinen 		mask <<= info->var.transp.offset;
70f7018c21STomi Valkeinen 		value |= mask;
71f7018c21STomi Valkeinen 	}
72f7018c21STomi Valkeinen 	pal[regno] = value;
73f7018c21STomi Valkeinen 
74f7018c21STomi Valkeinen 	return 0;
75f7018c21STomi Valkeinen }
76f7018c21STomi Valkeinen 
777c9806e8SChen-Yu Tsai struct simplefb_par;
787c9806e8SChen-Yu Tsai static void simplefb_clocks_destroy(struct simplefb_par *par);
797c9806e8SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par);
807c9806e8SChen-Yu Tsai 
81f7018c21STomi Valkeinen static void simplefb_destroy(struct fb_info *info)
82f7018c21STomi Valkeinen {
837c9806e8SChen-Yu Tsai 	simplefb_regulators_destroy(info->par);
847c9806e8SChen-Yu Tsai 	simplefb_clocks_destroy(info->par);
85f7018c21STomi Valkeinen 	if (info->screen_base)
86f7018c21STomi Valkeinen 		iounmap(info->screen_base);
87f7018c21STomi Valkeinen }
88f7018c21STomi Valkeinen 
89f7018c21STomi Valkeinen static struct fb_ops simplefb_ops = {
90f7018c21STomi Valkeinen 	.owner		= THIS_MODULE,
91f7018c21STomi Valkeinen 	.fb_destroy	= simplefb_destroy,
92f7018c21STomi Valkeinen 	.fb_setcolreg	= simplefb_setcolreg,
93f7018c21STomi Valkeinen 	.fb_fillrect	= cfb_fillrect,
94f7018c21STomi Valkeinen 	.fb_copyarea	= cfb_copyarea,
95f7018c21STomi Valkeinen 	.fb_imageblit	= cfb_imageblit,
96f7018c21STomi Valkeinen };
97f7018c21STomi Valkeinen 
98f7018c21STomi Valkeinen static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
99f7018c21STomi Valkeinen 
100f7018c21STomi Valkeinen struct simplefb_params {
101f7018c21STomi Valkeinen 	u32 width;
102f7018c21STomi Valkeinen 	u32 height;
103f7018c21STomi Valkeinen 	u32 stride;
104f7018c21STomi Valkeinen 	struct simplefb_format *format;
105f7018c21STomi Valkeinen };
106f7018c21STomi Valkeinen 
107f7018c21STomi Valkeinen static int simplefb_parse_dt(struct platform_device *pdev,
108f7018c21STomi Valkeinen 			   struct simplefb_params *params)
109f7018c21STomi Valkeinen {
110f7018c21STomi Valkeinen 	struct device_node *np = pdev->dev.of_node;
111f7018c21STomi Valkeinen 	int ret;
112f7018c21STomi Valkeinen 	const char *format;
113f7018c21STomi Valkeinen 	int i;
114f7018c21STomi Valkeinen 
115f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "width", &params->width);
116f7018c21STomi Valkeinen 	if (ret) {
117f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse width property\n");
118f7018c21STomi Valkeinen 		return ret;
119f7018c21STomi Valkeinen 	}
120f7018c21STomi Valkeinen 
121f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "height", &params->height);
122f7018c21STomi Valkeinen 	if (ret) {
123f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse height property\n");
124f7018c21STomi Valkeinen 		return ret;
125f7018c21STomi Valkeinen 	}
126f7018c21STomi Valkeinen 
127f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "stride", &params->stride);
128f7018c21STomi Valkeinen 	if (ret) {
129f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse stride property\n");
130f7018c21STomi Valkeinen 		return ret;
131f7018c21STomi Valkeinen 	}
132f7018c21STomi Valkeinen 
133f7018c21STomi Valkeinen 	ret = of_property_read_string(np, "format", &format);
134f7018c21STomi Valkeinen 	if (ret) {
135f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse format property\n");
136f7018c21STomi Valkeinen 		return ret;
137f7018c21STomi Valkeinen 	}
138f7018c21STomi Valkeinen 	params->format = NULL;
139f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
140f7018c21STomi Valkeinen 		if (strcmp(format, simplefb_formats[i].name))
141f7018c21STomi Valkeinen 			continue;
142f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
143f7018c21STomi Valkeinen 		break;
144f7018c21STomi Valkeinen 	}
145f7018c21STomi Valkeinen 	if (!params->format) {
146f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
147f7018c21STomi Valkeinen 		return -EINVAL;
148f7018c21STomi Valkeinen 	}
149f7018c21STomi Valkeinen 
150f7018c21STomi Valkeinen 	return 0;
151f7018c21STomi Valkeinen }
152f7018c21STomi Valkeinen 
153f7018c21STomi Valkeinen static int simplefb_parse_pd(struct platform_device *pdev,
154f7018c21STomi Valkeinen 			     struct simplefb_params *params)
155f7018c21STomi Valkeinen {
156f7018c21STomi Valkeinen 	struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
157f7018c21STomi Valkeinen 	int i;
158f7018c21STomi Valkeinen 
159f7018c21STomi Valkeinen 	params->width = pd->width;
160f7018c21STomi Valkeinen 	params->height = pd->height;
161f7018c21STomi Valkeinen 	params->stride = pd->stride;
162f7018c21STomi Valkeinen 
163f7018c21STomi Valkeinen 	params->format = NULL;
164f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
165f7018c21STomi Valkeinen 		if (strcmp(pd->format, simplefb_formats[i].name))
166f7018c21STomi Valkeinen 			continue;
167f7018c21STomi Valkeinen 
168f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
169f7018c21STomi Valkeinen 		break;
170f7018c21STomi Valkeinen 	}
171f7018c21STomi Valkeinen 
172f7018c21STomi Valkeinen 	if (!params->format) {
173f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
174f7018c21STomi Valkeinen 		return -EINVAL;
175f7018c21STomi Valkeinen 	}
176f7018c21STomi Valkeinen 
177f7018c21STomi Valkeinen 	return 0;
178f7018c21STomi Valkeinen }
179f7018c21STomi Valkeinen 
1801270be4aSLuc Verhaegen struct simplefb_par {
1811270be4aSLuc Verhaegen 	u32 palette[PSEUDO_PALETTE_SIZE];
1828284731eSHans de Goede #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
183*a3accfd7SHans de Goede 	bool clks_enabled;
1842cd82d06SStephen Boyd 	unsigned int clk_count;
185fc219bfdSLuc Verhaegen 	struct clk **clks;
186fc219bfdSLuc Verhaegen #endif
187814740e7SChen-Yu Tsai #if defined CONFIG_OF && defined CONFIG_REGULATOR
188*a3accfd7SHans de Goede 	bool regulators_enabled;
189814740e7SChen-Yu Tsai 	u32 regulator_count;
190814740e7SChen-Yu Tsai 	struct regulator **regulators;
191814740e7SChen-Yu Tsai #endif
1921270be4aSLuc Verhaegen };
1931270be4aSLuc Verhaegen 
1948284731eSHans de Goede #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
195fc219bfdSLuc Verhaegen /*
196fc219bfdSLuc Verhaegen  * Clock handling code.
197fc219bfdSLuc Verhaegen  *
198fc219bfdSLuc Verhaegen  * Here we handle the clocks property of our "simple-framebuffer" dt node.
199fc219bfdSLuc Verhaegen  * This is necessary so that we can make sure that any clocks needed by
200fc219bfdSLuc Verhaegen  * the display engine that the bootloader set up for us (and for which it
201fc219bfdSLuc Verhaegen  * provided a simplefb dt node), stay up, for the life of the simplefb
202fc219bfdSLuc Verhaegen  * driver.
203fc219bfdSLuc Verhaegen  *
204fc219bfdSLuc Verhaegen  * When the driver unloads, we cleanly disable, and then release the clocks.
205fc219bfdSLuc Verhaegen  *
206fc219bfdSLuc Verhaegen  * We only complain about errors here, no action is taken as the most likely
207fc219bfdSLuc Verhaegen  * error can only happen due to a mismatch between the bootloader which set
208fc219bfdSLuc Verhaegen  * up simplefb, and the clock definitions in the device tree. Chances are
209fc219bfdSLuc Verhaegen  * that there are no adverse effects, and if there are, a clean teardown of
210fc219bfdSLuc Verhaegen  * the fb probe will not help us much either. So just complain and carry on,
211fc219bfdSLuc Verhaegen  * and hope that the user actually gets a working fb at the end of things.
212fc219bfdSLuc Verhaegen  */
213*a3accfd7SHans de Goede static int simplefb_clocks_get(struct simplefb_par *par,
214fc219bfdSLuc Verhaegen 			       struct platform_device *pdev)
215fc219bfdSLuc Verhaegen {
216fc219bfdSLuc Verhaegen 	struct device_node *np = pdev->dev.of_node;
217fc219bfdSLuc Verhaegen 	struct clk *clock;
218*a3accfd7SHans de Goede 	int i;
219fc219bfdSLuc Verhaegen 
220fc219bfdSLuc Verhaegen 	if (dev_get_platdata(&pdev->dev) || !np)
221fc219bfdSLuc Verhaegen 		return 0;
222fc219bfdSLuc Verhaegen 
223fc219bfdSLuc Verhaegen 	par->clk_count = of_clk_get_parent_count(np);
2242cd82d06SStephen Boyd 	if (!par->clk_count)
225fc219bfdSLuc Verhaegen 		return 0;
226fc219bfdSLuc Verhaegen 
227fc219bfdSLuc Verhaegen 	par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
228fc219bfdSLuc Verhaegen 	if (!par->clks)
229fc219bfdSLuc Verhaegen 		return -ENOMEM;
230fc219bfdSLuc Verhaegen 
231fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
232fc219bfdSLuc Verhaegen 		clock = of_clk_get(np, i);
233fc219bfdSLuc Verhaegen 		if (IS_ERR(clock)) {
234fc219bfdSLuc Verhaegen 			if (PTR_ERR(clock) == -EPROBE_DEFER) {
235fc219bfdSLuc Verhaegen 				while (--i >= 0) {
236fc219bfdSLuc Verhaegen 					if (par->clks[i])
237fc219bfdSLuc Verhaegen 						clk_put(par->clks[i]);
238fc219bfdSLuc Verhaegen 				}
239fc219bfdSLuc Verhaegen 				kfree(par->clks);
240fc219bfdSLuc Verhaegen 				return -EPROBE_DEFER;
241fc219bfdSLuc Verhaegen 			}
242fc219bfdSLuc Verhaegen 			dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
243fc219bfdSLuc Verhaegen 				__func__, i, PTR_ERR(clock));
244fc219bfdSLuc Verhaegen 			continue;
245fc219bfdSLuc Verhaegen 		}
246fc219bfdSLuc Verhaegen 		par->clks[i] = clock;
247fc219bfdSLuc Verhaegen 	}
248fc219bfdSLuc Verhaegen 
249*a3accfd7SHans de Goede 	return 0;
250*a3accfd7SHans de Goede }
251*a3accfd7SHans de Goede 
252*a3accfd7SHans de Goede static void simplefb_clocks_enable(struct simplefb_par *par,
253*a3accfd7SHans de Goede 				   struct platform_device *pdev)
254*a3accfd7SHans de Goede {
255*a3accfd7SHans de Goede 	int i, ret;
256*a3accfd7SHans de Goede 
257fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
258fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
259fc219bfdSLuc Verhaegen 			ret = clk_prepare_enable(par->clks[i]);
260fc219bfdSLuc Verhaegen 			if (ret) {
261fc219bfdSLuc Verhaegen 				dev_err(&pdev->dev,
262fc219bfdSLuc Verhaegen 					"%s: failed to enable clock %d: %d\n",
263fc219bfdSLuc Verhaegen 					__func__, i, ret);
264fc219bfdSLuc Verhaegen 				clk_put(par->clks[i]);
265fc219bfdSLuc Verhaegen 				par->clks[i] = NULL;
266fc219bfdSLuc Verhaegen 			}
267fc219bfdSLuc Verhaegen 		}
268fc219bfdSLuc Verhaegen 	}
269*a3accfd7SHans de Goede 	par->clks_enabled = true;
270fc219bfdSLuc Verhaegen }
271fc219bfdSLuc Verhaegen 
272fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par)
273fc219bfdSLuc Verhaegen {
274fc219bfdSLuc Verhaegen 	int i;
275fc219bfdSLuc Verhaegen 
276fc219bfdSLuc Verhaegen 	if (!par->clks)
277fc219bfdSLuc Verhaegen 		return;
278fc219bfdSLuc Verhaegen 
279fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
280fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
281*a3accfd7SHans de Goede 			if (par->clks_enabled)
282fc219bfdSLuc Verhaegen 				clk_disable_unprepare(par->clks[i]);
283fc219bfdSLuc Verhaegen 			clk_put(par->clks[i]);
284fc219bfdSLuc Verhaegen 		}
285fc219bfdSLuc Verhaegen 	}
286fc219bfdSLuc Verhaegen 
287fc219bfdSLuc Verhaegen 	kfree(par->clks);
288fc219bfdSLuc Verhaegen }
289fc219bfdSLuc Verhaegen #else
290*a3accfd7SHans de Goede static int simplefb_clocks_get(struct simplefb_par *par,
291fc219bfdSLuc Verhaegen 	struct platform_device *pdev) { return 0; }
292*a3accfd7SHans de Goede static void simplefb_clocks_enable(struct simplefb_par *par,
293*a3accfd7SHans de Goede 	struct platform_device *pdev) { }
294fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par) { }
295fc219bfdSLuc Verhaegen #endif
296fc219bfdSLuc Verhaegen 
297814740e7SChen-Yu Tsai #if defined CONFIG_OF && defined CONFIG_REGULATOR
298814740e7SChen-Yu Tsai 
299814740e7SChen-Yu Tsai #define SUPPLY_SUFFIX "-supply"
300814740e7SChen-Yu Tsai 
301814740e7SChen-Yu Tsai /*
302814740e7SChen-Yu Tsai  * Regulator handling code.
303814740e7SChen-Yu Tsai  *
304814740e7SChen-Yu Tsai  * Here we handle the num-supplies and vin*-supply properties of our
305814740e7SChen-Yu Tsai  * "simple-framebuffer" dt node. This is necessary so that we can make sure
306814740e7SChen-Yu Tsai  * that any regulators needed by the display hardware that the bootloader
307814740e7SChen-Yu Tsai  * set up for us (and for which it provided a simplefb dt node), stay up,
308814740e7SChen-Yu Tsai  * for the life of the simplefb driver.
309814740e7SChen-Yu Tsai  *
310814740e7SChen-Yu Tsai  * When the driver unloads, we cleanly disable, and then release the
311814740e7SChen-Yu Tsai  * regulators.
312814740e7SChen-Yu Tsai  *
313814740e7SChen-Yu Tsai  * We only complain about errors here, no action is taken as the most likely
314814740e7SChen-Yu Tsai  * error can only happen due to a mismatch between the bootloader which set
315814740e7SChen-Yu Tsai  * up simplefb, and the regulator definitions in the device tree. Chances are
316814740e7SChen-Yu Tsai  * that there are no adverse effects, and if there are, a clean teardown of
317814740e7SChen-Yu Tsai  * the fb probe will not help us much either. So just complain and carry on,
318814740e7SChen-Yu Tsai  * and hope that the user actually gets a working fb at the end of things.
319814740e7SChen-Yu Tsai  */
320*a3accfd7SHans de Goede static int simplefb_regulators_get(struct simplefb_par *par,
321814740e7SChen-Yu Tsai 				   struct platform_device *pdev)
322814740e7SChen-Yu Tsai {
323814740e7SChen-Yu Tsai 	struct device_node *np = pdev->dev.of_node;
324814740e7SChen-Yu Tsai 	struct property *prop;
325814740e7SChen-Yu Tsai 	struct regulator *regulator;
326814740e7SChen-Yu Tsai 	const char *p;
327*a3accfd7SHans de Goede 	int count = 0, i = 0;
328814740e7SChen-Yu Tsai 
329814740e7SChen-Yu Tsai 	if (dev_get_platdata(&pdev->dev) || !np)
330814740e7SChen-Yu Tsai 		return 0;
331814740e7SChen-Yu Tsai 
332814740e7SChen-Yu Tsai 	/* Count the number of regulator supplies */
333814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
334814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
335814740e7SChen-Yu Tsai 		if (p && p != prop->name)
336814740e7SChen-Yu Tsai 			count++;
337814740e7SChen-Yu Tsai 	}
338814740e7SChen-Yu Tsai 
339814740e7SChen-Yu Tsai 	if (!count)
340814740e7SChen-Yu Tsai 		return 0;
341814740e7SChen-Yu Tsai 
342814740e7SChen-Yu Tsai 	par->regulators = devm_kcalloc(&pdev->dev, count,
343814740e7SChen-Yu Tsai 				       sizeof(struct regulator *), GFP_KERNEL);
344814740e7SChen-Yu Tsai 	if (!par->regulators)
345814740e7SChen-Yu Tsai 		return -ENOMEM;
346814740e7SChen-Yu Tsai 
347814740e7SChen-Yu Tsai 	/* Get all the regulators */
348814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
349814740e7SChen-Yu Tsai 		char name[32]; /* 32 is max size of property name */
350814740e7SChen-Yu Tsai 
351814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
352814740e7SChen-Yu Tsai 		if (!p || p == prop->name)
353814740e7SChen-Yu Tsai 			continue;
354814740e7SChen-Yu Tsai 
355814740e7SChen-Yu Tsai 		strlcpy(name, prop->name,
356814740e7SChen-Yu Tsai 			strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
357814740e7SChen-Yu Tsai 		regulator = devm_regulator_get_optional(&pdev->dev, name);
358814740e7SChen-Yu Tsai 		if (IS_ERR(regulator)) {
359814740e7SChen-Yu Tsai 			if (PTR_ERR(regulator) == -EPROBE_DEFER)
360814740e7SChen-Yu Tsai 				return -EPROBE_DEFER;
361814740e7SChen-Yu Tsai 			dev_err(&pdev->dev, "regulator %s not found: %ld\n",
362814740e7SChen-Yu Tsai 				name, PTR_ERR(regulator));
363814740e7SChen-Yu Tsai 			continue;
364814740e7SChen-Yu Tsai 		}
365814740e7SChen-Yu Tsai 		par->regulators[i++] = regulator;
366814740e7SChen-Yu Tsai 	}
367814740e7SChen-Yu Tsai 	par->regulator_count = i;
368814740e7SChen-Yu Tsai 
369*a3accfd7SHans de Goede 	return 0;
370*a3accfd7SHans de Goede }
371*a3accfd7SHans de Goede 
372*a3accfd7SHans de Goede static void simplefb_regulators_enable(struct simplefb_par *par,
373*a3accfd7SHans de Goede 				       struct platform_device *pdev)
374*a3accfd7SHans de Goede {
375*a3accfd7SHans de Goede 	int i, ret;
376*a3accfd7SHans de Goede 
377814740e7SChen-Yu Tsai 	/* Enable all the regulators */
378814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++) {
379814740e7SChen-Yu Tsai 		ret = regulator_enable(par->regulators[i]);
380814740e7SChen-Yu Tsai 		if (ret) {
381814740e7SChen-Yu Tsai 			dev_err(&pdev->dev,
382814740e7SChen-Yu Tsai 				"failed to enable regulator %d: %d\n",
383814740e7SChen-Yu Tsai 				i, ret);
384814740e7SChen-Yu Tsai 			devm_regulator_put(par->regulators[i]);
385814740e7SChen-Yu Tsai 			par->regulators[i] = NULL;
386814740e7SChen-Yu Tsai 		}
387814740e7SChen-Yu Tsai 	}
388*a3accfd7SHans de Goede 	par->regulators_enabled = true;
389814740e7SChen-Yu Tsai }
390814740e7SChen-Yu Tsai 
391814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par)
392814740e7SChen-Yu Tsai {
393814740e7SChen-Yu Tsai 	int i;
394814740e7SChen-Yu Tsai 
395*a3accfd7SHans de Goede 	if (!par->regulators || !par->regulators_enabled)
396814740e7SChen-Yu Tsai 		return;
397814740e7SChen-Yu Tsai 
398814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++)
399814740e7SChen-Yu Tsai 		if (par->regulators[i])
400814740e7SChen-Yu Tsai 			regulator_disable(par->regulators[i]);
401814740e7SChen-Yu Tsai }
402814740e7SChen-Yu Tsai #else
403*a3accfd7SHans de Goede static int simplefb_regulators_get(struct simplefb_par *par,
404814740e7SChen-Yu Tsai 	struct platform_device *pdev) { return 0; }
405*a3accfd7SHans de Goede static void simplefb_regulators_enable(struct simplefb_par *par,
406*a3accfd7SHans de Goede 	struct platform_device *pdev) { }
407814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par) { }
408814740e7SChen-Yu Tsai #endif
409814740e7SChen-Yu Tsai 
410f7018c21STomi Valkeinen static int simplefb_probe(struct platform_device *pdev)
411f7018c21STomi Valkeinen {
412f7018c21STomi Valkeinen 	int ret;
413f7018c21STomi Valkeinen 	struct simplefb_params params;
414f7018c21STomi Valkeinen 	struct fb_info *info;
4151270be4aSLuc Verhaegen 	struct simplefb_par *par;
416f7018c21STomi Valkeinen 	struct resource *mem;
417f7018c21STomi Valkeinen 
418f7018c21STomi Valkeinen 	if (fb_get_options("simplefb", NULL))
419f7018c21STomi Valkeinen 		return -ENODEV;
420f7018c21STomi Valkeinen 
421f7018c21STomi Valkeinen 	ret = -ENODEV;
422f7018c21STomi Valkeinen 	if (dev_get_platdata(&pdev->dev))
423f7018c21STomi Valkeinen 		ret = simplefb_parse_pd(pdev, &params);
424f7018c21STomi Valkeinen 	else if (pdev->dev.of_node)
425f7018c21STomi Valkeinen 		ret = simplefb_parse_dt(pdev, &params);
426f7018c21STomi Valkeinen 
427f7018c21STomi Valkeinen 	if (ret)
428f7018c21STomi Valkeinen 		return ret;
429f7018c21STomi Valkeinen 
430f7018c21STomi Valkeinen 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
431f7018c21STomi Valkeinen 	if (!mem) {
432f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "No memory resource\n");
433f7018c21STomi Valkeinen 		return -EINVAL;
434f7018c21STomi Valkeinen 	}
435f7018c21STomi Valkeinen 
4361270be4aSLuc Verhaegen 	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
437f7018c21STomi Valkeinen 	if (!info)
438f7018c21STomi Valkeinen 		return -ENOMEM;
439f7018c21STomi Valkeinen 	platform_set_drvdata(pdev, info);
440f7018c21STomi Valkeinen 
4411270be4aSLuc Verhaegen 	par = info->par;
4421270be4aSLuc Verhaegen 
443f7018c21STomi Valkeinen 	info->fix = simplefb_fix;
444f7018c21STomi Valkeinen 	info->fix.smem_start = mem->start;
445f7018c21STomi Valkeinen 	info->fix.smem_len = resource_size(mem);
446f7018c21STomi Valkeinen 	info->fix.line_length = params.stride;
447f7018c21STomi Valkeinen 
448f7018c21STomi Valkeinen 	info->var = simplefb_var;
449f7018c21STomi Valkeinen 	info->var.xres = params.width;
450f7018c21STomi Valkeinen 	info->var.yres = params.height;
451f7018c21STomi Valkeinen 	info->var.xres_virtual = params.width;
452f7018c21STomi Valkeinen 	info->var.yres_virtual = params.height;
453f7018c21STomi Valkeinen 	info->var.bits_per_pixel = params.format->bits_per_pixel;
454f7018c21STomi Valkeinen 	info->var.red = params.format->red;
455f7018c21STomi Valkeinen 	info->var.green = params.format->green;
456f7018c21STomi Valkeinen 	info->var.blue = params.format->blue;
457f7018c21STomi Valkeinen 	info->var.transp = params.format->transp;
458f7018c21STomi Valkeinen 
459f7018c21STomi Valkeinen 	info->apertures = alloc_apertures(1);
460f7018c21STomi Valkeinen 	if (!info->apertures) {
461bf2fda15SLuc Verhaegen 		ret = -ENOMEM;
462bf2fda15SLuc Verhaegen 		goto error_fb_release;
463f7018c21STomi Valkeinen 	}
464f7018c21STomi Valkeinen 	info->apertures->ranges[0].base = info->fix.smem_start;
465f7018c21STomi Valkeinen 	info->apertures->ranges[0].size = info->fix.smem_len;
466f7018c21STomi Valkeinen 
467f7018c21STomi Valkeinen 	info->fbops = &simplefb_ops;
468f7018c21STomi Valkeinen 	info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
469f7018c21STomi Valkeinen 	info->screen_base = ioremap_wc(info->fix.smem_start,
470f7018c21STomi Valkeinen 				       info->fix.smem_len);
471f7018c21STomi Valkeinen 	if (!info->screen_base) {
472bf2fda15SLuc Verhaegen 		ret = -ENOMEM;
473bf2fda15SLuc Verhaegen 		goto error_fb_release;
474f7018c21STomi Valkeinen 	}
4751270be4aSLuc Verhaegen 	info->pseudo_palette = par->palette;
476f7018c21STomi Valkeinen 
477*a3accfd7SHans de Goede 	ret = simplefb_clocks_get(par, pdev);
478fc219bfdSLuc Verhaegen 	if (ret < 0)
479fc219bfdSLuc Verhaegen 		goto error_unmap;
480fc219bfdSLuc Verhaegen 
481*a3accfd7SHans de Goede 	ret = simplefb_regulators_get(par, pdev);
482814740e7SChen-Yu Tsai 	if (ret < 0)
483814740e7SChen-Yu Tsai 		goto error_clocks;
484814740e7SChen-Yu Tsai 
485*a3accfd7SHans de Goede 	simplefb_clocks_enable(par, pdev);
486*a3accfd7SHans de Goede 	simplefb_regulators_enable(par, pdev);
487*a3accfd7SHans de Goede 
488f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
489f7018c21STomi Valkeinen 			     info->fix.smem_start, info->fix.smem_len,
490f7018c21STomi Valkeinen 			     info->screen_base);
491f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
492f7018c21STomi Valkeinen 			     params.format->name,
493f7018c21STomi Valkeinen 			     info->var.xres, info->var.yres,
494f7018c21STomi Valkeinen 			     info->var.bits_per_pixel, info->fix.line_length);
495f7018c21STomi Valkeinen 
496f7018c21STomi Valkeinen 	ret = register_framebuffer(info);
497f7018c21STomi Valkeinen 	if (ret < 0) {
498f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
499814740e7SChen-Yu Tsai 		goto error_regulators;
500f7018c21STomi Valkeinen 	}
501f7018c21STomi Valkeinen 
502f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
503f7018c21STomi Valkeinen 
504f7018c21STomi Valkeinen 	return 0;
505bf2fda15SLuc Verhaegen 
506814740e7SChen-Yu Tsai error_regulators:
507814740e7SChen-Yu Tsai 	simplefb_regulators_destroy(par);
508fc219bfdSLuc Verhaegen error_clocks:
509fc219bfdSLuc Verhaegen 	simplefb_clocks_destroy(par);
510bf2fda15SLuc Verhaegen error_unmap:
511bf2fda15SLuc Verhaegen 	iounmap(info->screen_base);
512bf2fda15SLuc Verhaegen error_fb_release:
513bf2fda15SLuc Verhaegen 	framebuffer_release(info);
514bf2fda15SLuc Verhaegen 	return ret;
515f7018c21STomi Valkeinen }
516f7018c21STomi Valkeinen 
517f7018c21STomi Valkeinen static int simplefb_remove(struct platform_device *pdev)
518f7018c21STomi Valkeinen {
519f7018c21STomi Valkeinen 	struct fb_info *info = platform_get_drvdata(pdev);
520f7018c21STomi Valkeinen 
521f7018c21STomi Valkeinen 	unregister_framebuffer(info);
522f7018c21STomi Valkeinen 	framebuffer_release(info);
523f7018c21STomi Valkeinen 
524f7018c21STomi Valkeinen 	return 0;
525f7018c21STomi Valkeinen }
526f7018c21STomi Valkeinen 
527f7018c21STomi Valkeinen static const struct of_device_id simplefb_of_match[] = {
528f7018c21STomi Valkeinen 	{ .compatible = "simple-framebuffer", },
529f7018c21STomi Valkeinen 	{ },
530f7018c21STomi Valkeinen };
531f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(of, simplefb_of_match);
532f7018c21STomi Valkeinen 
533f7018c21STomi Valkeinen static struct platform_driver simplefb_driver = {
534f7018c21STomi Valkeinen 	.driver = {
535f7018c21STomi Valkeinen 		.name = "simple-framebuffer",
536f7018c21STomi Valkeinen 		.of_match_table = simplefb_of_match,
537f7018c21STomi Valkeinen 	},
538f7018c21STomi Valkeinen 	.probe = simplefb_probe,
539f7018c21STomi Valkeinen 	.remove = simplefb_remove,
540f7018c21STomi Valkeinen };
54189c95001SHans de Goede 
54289c95001SHans de Goede static int __init simplefb_init(void)
54389c95001SHans de Goede {
54489c95001SHans de Goede 	int ret;
54589c95001SHans de Goede 	struct device_node *np;
54689c95001SHans de Goede 
54789c95001SHans de Goede 	ret = platform_driver_register(&simplefb_driver);
54889c95001SHans de Goede 	if (ret)
54989c95001SHans de Goede 		return ret;
55089c95001SHans de Goede 
551d9e02019SHans de Goede 	if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
55289c95001SHans de Goede 		for_each_child_of_node(of_chosen, np) {
55389c95001SHans de Goede 			if (of_device_is_compatible(np, "simple-framebuffer"))
55489c95001SHans de Goede 				of_platform_device_create(np, NULL, NULL);
55589c95001SHans de Goede 		}
55689c95001SHans de Goede 	}
55789c95001SHans de Goede 
55889c95001SHans de Goede 	return 0;
55989c95001SHans de Goede }
56089c95001SHans de Goede 
5610c5b240cSHans de Goede fs_initcall(simplefb_init);
562f7018c21STomi Valkeinen 
563f7018c21STomi Valkeinen MODULE_AUTHOR("Stephen Warren <[email protected]>");
564f7018c21STomi Valkeinen MODULE_DESCRIPTION("Simple framebuffer driver");
565f7018c21STomi Valkeinen MODULE_LICENSE("GPL v2");
566