xref: /linux-6.15/drivers/video/fbdev/simplefb.c (revision 2cd82d06)
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 
36f7018c21STomi Valkeinen static 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 
43f7018c21STomi Valkeinen static 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 
77f7018c21STomi Valkeinen static void simplefb_destroy(struct fb_info *info)
78f7018c21STomi Valkeinen {
79f7018c21STomi Valkeinen 	if (info->screen_base)
80f7018c21STomi Valkeinen 		iounmap(info->screen_base);
81f7018c21STomi Valkeinen }
82f7018c21STomi Valkeinen 
83f7018c21STomi Valkeinen static struct fb_ops simplefb_ops = {
84f7018c21STomi Valkeinen 	.owner		= THIS_MODULE,
85f7018c21STomi Valkeinen 	.fb_destroy	= simplefb_destroy,
86f7018c21STomi Valkeinen 	.fb_setcolreg	= simplefb_setcolreg,
87f7018c21STomi Valkeinen 	.fb_fillrect	= cfb_fillrect,
88f7018c21STomi Valkeinen 	.fb_copyarea	= cfb_copyarea,
89f7018c21STomi Valkeinen 	.fb_imageblit	= cfb_imageblit,
90f7018c21STomi Valkeinen };
91f7018c21STomi Valkeinen 
92f7018c21STomi Valkeinen static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
93f7018c21STomi Valkeinen 
94f7018c21STomi Valkeinen struct simplefb_params {
95f7018c21STomi Valkeinen 	u32 width;
96f7018c21STomi Valkeinen 	u32 height;
97f7018c21STomi Valkeinen 	u32 stride;
98f7018c21STomi Valkeinen 	struct simplefb_format *format;
99f7018c21STomi Valkeinen };
100f7018c21STomi Valkeinen 
101f7018c21STomi Valkeinen static int simplefb_parse_dt(struct platform_device *pdev,
102f7018c21STomi Valkeinen 			   struct simplefb_params *params)
103f7018c21STomi Valkeinen {
104f7018c21STomi Valkeinen 	struct device_node *np = pdev->dev.of_node;
105f7018c21STomi Valkeinen 	int ret;
106f7018c21STomi Valkeinen 	const char *format;
107f7018c21STomi Valkeinen 	int i;
108f7018c21STomi Valkeinen 
109f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "width", &params->width);
110f7018c21STomi Valkeinen 	if (ret) {
111f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse width property\n");
112f7018c21STomi Valkeinen 		return ret;
113f7018c21STomi Valkeinen 	}
114f7018c21STomi Valkeinen 
115f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "height", &params->height);
116f7018c21STomi Valkeinen 	if (ret) {
117f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse height property\n");
118f7018c21STomi Valkeinen 		return ret;
119f7018c21STomi Valkeinen 	}
120f7018c21STomi Valkeinen 
121f7018c21STomi Valkeinen 	ret = of_property_read_u32(np, "stride", &params->stride);
122f7018c21STomi Valkeinen 	if (ret) {
123f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse stride property\n");
124f7018c21STomi Valkeinen 		return ret;
125f7018c21STomi Valkeinen 	}
126f7018c21STomi Valkeinen 
127f7018c21STomi Valkeinen 	ret = of_property_read_string(np, "format", &format);
128f7018c21STomi Valkeinen 	if (ret) {
129f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Can't parse format property\n");
130f7018c21STomi Valkeinen 		return ret;
131f7018c21STomi Valkeinen 	}
132f7018c21STomi Valkeinen 	params->format = NULL;
133f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
134f7018c21STomi Valkeinen 		if (strcmp(format, simplefb_formats[i].name))
135f7018c21STomi Valkeinen 			continue;
136f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
137f7018c21STomi Valkeinen 		break;
138f7018c21STomi Valkeinen 	}
139f7018c21STomi Valkeinen 	if (!params->format) {
140f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
141f7018c21STomi Valkeinen 		return -EINVAL;
142f7018c21STomi Valkeinen 	}
143f7018c21STomi Valkeinen 
144f7018c21STomi Valkeinen 	return 0;
145f7018c21STomi Valkeinen }
146f7018c21STomi Valkeinen 
147f7018c21STomi Valkeinen static int simplefb_parse_pd(struct platform_device *pdev,
148f7018c21STomi Valkeinen 			     struct simplefb_params *params)
149f7018c21STomi Valkeinen {
150f7018c21STomi Valkeinen 	struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
151f7018c21STomi Valkeinen 	int i;
152f7018c21STomi Valkeinen 
153f7018c21STomi Valkeinen 	params->width = pd->width;
154f7018c21STomi Valkeinen 	params->height = pd->height;
155f7018c21STomi Valkeinen 	params->stride = pd->stride;
156f7018c21STomi Valkeinen 
157f7018c21STomi Valkeinen 	params->format = NULL;
158f7018c21STomi Valkeinen 	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
159f7018c21STomi Valkeinen 		if (strcmp(pd->format, simplefb_formats[i].name))
160f7018c21STomi Valkeinen 			continue;
161f7018c21STomi Valkeinen 
162f7018c21STomi Valkeinen 		params->format = &simplefb_formats[i];
163f7018c21STomi Valkeinen 		break;
164f7018c21STomi Valkeinen 	}
165f7018c21STomi Valkeinen 
166f7018c21STomi Valkeinen 	if (!params->format) {
167f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Invalid format value\n");
168f7018c21STomi Valkeinen 		return -EINVAL;
169f7018c21STomi Valkeinen 	}
170f7018c21STomi Valkeinen 
171f7018c21STomi Valkeinen 	return 0;
172f7018c21STomi Valkeinen }
173f7018c21STomi Valkeinen 
1741270be4aSLuc Verhaegen struct simplefb_par {
1751270be4aSLuc Verhaegen 	u32 palette[PSEUDO_PALETTE_SIZE];
1768284731eSHans de Goede #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
177*2cd82d06SStephen Boyd 	unsigned int clk_count;
178fc219bfdSLuc Verhaegen 	struct clk **clks;
179fc219bfdSLuc Verhaegen #endif
180814740e7SChen-Yu Tsai #if defined CONFIG_OF && defined CONFIG_REGULATOR
181814740e7SChen-Yu Tsai 	u32 regulator_count;
182814740e7SChen-Yu Tsai 	struct regulator **regulators;
183814740e7SChen-Yu Tsai #endif
1841270be4aSLuc Verhaegen };
1851270be4aSLuc Verhaegen 
1868284731eSHans de Goede #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
187fc219bfdSLuc Verhaegen /*
188fc219bfdSLuc Verhaegen  * Clock handling code.
189fc219bfdSLuc Verhaegen  *
190fc219bfdSLuc Verhaegen  * Here we handle the clocks property of our "simple-framebuffer" dt node.
191fc219bfdSLuc Verhaegen  * This is necessary so that we can make sure that any clocks needed by
192fc219bfdSLuc Verhaegen  * the display engine that the bootloader set up for us (and for which it
193fc219bfdSLuc Verhaegen  * provided a simplefb dt node), stay up, for the life of the simplefb
194fc219bfdSLuc Verhaegen  * driver.
195fc219bfdSLuc Verhaegen  *
196fc219bfdSLuc Verhaegen  * When the driver unloads, we cleanly disable, and then release the clocks.
197fc219bfdSLuc Verhaegen  *
198fc219bfdSLuc Verhaegen  * We only complain about errors here, no action is taken as the most likely
199fc219bfdSLuc Verhaegen  * error can only happen due to a mismatch between the bootloader which set
200fc219bfdSLuc Verhaegen  * up simplefb, and the clock definitions in the device tree. Chances are
201fc219bfdSLuc Verhaegen  * that there are no adverse effects, and if there are, a clean teardown of
202fc219bfdSLuc Verhaegen  * the fb probe will not help us much either. So just complain and carry on,
203fc219bfdSLuc Verhaegen  * and hope that the user actually gets a working fb at the end of things.
204fc219bfdSLuc Verhaegen  */
205fc219bfdSLuc Verhaegen static int simplefb_clocks_init(struct simplefb_par *par,
206fc219bfdSLuc Verhaegen 				struct platform_device *pdev)
207fc219bfdSLuc Verhaegen {
208fc219bfdSLuc Verhaegen 	struct device_node *np = pdev->dev.of_node;
209fc219bfdSLuc Verhaegen 	struct clk *clock;
210fc219bfdSLuc Verhaegen 	int i, ret;
211fc219bfdSLuc Verhaegen 
212fc219bfdSLuc Verhaegen 	if (dev_get_platdata(&pdev->dev) || !np)
213fc219bfdSLuc Verhaegen 		return 0;
214fc219bfdSLuc Verhaegen 
215fc219bfdSLuc Verhaegen 	par->clk_count = of_clk_get_parent_count(np);
216*2cd82d06SStephen Boyd 	if (!par->clk_count)
217fc219bfdSLuc Verhaegen 		return 0;
218fc219bfdSLuc Verhaegen 
219fc219bfdSLuc Verhaegen 	par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
220fc219bfdSLuc Verhaegen 	if (!par->clks)
221fc219bfdSLuc Verhaegen 		return -ENOMEM;
222fc219bfdSLuc Verhaegen 
223fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
224fc219bfdSLuc Verhaegen 		clock = of_clk_get(np, i);
225fc219bfdSLuc Verhaegen 		if (IS_ERR(clock)) {
226fc219bfdSLuc Verhaegen 			if (PTR_ERR(clock) == -EPROBE_DEFER) {
227fc219bfdSLuc Verhaegen 				while (--i >= 0) {
228fc219bfdSLuc Verhaegen 					if (par->clks[i])
229fc219bfdSLuc Verhaegen 						clk_put(par->clks[i]);
230fc219bfdSLuc Verhaegen 				}
231fc219bfdSLuc Verhaegen 				kfree(par->clks);
232fc219bfdSLuc Verhaegen 				return -EPROBE_DEFER;
233fc219bfdSLuc Verhaegen 			}
234fc219bfdSLuc Verhaegen 			dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
235fc219bfdSLuc Verhaegen 				__func__, i, PTR_ERR(clock));
236fc219bfdSLuc Verhaegen 			continue;
237fc219bfdSLuc Verhaegen 		}
238fc219bfdSLuc Verhaegen 		par->clks[i] = clock;
239fc219bfdSLuc Verhaegen 	}
240fc219bfdSLuc Verhaegen 
241fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
242fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
243fc219bfdSLuc Verhaegen 			ret = clk_prepare_enable(par->clks[i]);
244fc219bfdSLuc Verhaegen 			if (ret) {
245fc219bfdSLuc Verhaegen 				dev_err(&pdev->dev,
246fc219bfdSLuc Verhaegen 					"%s: failed to enable clock %d: %d\n",
247fc219bfdSLuc Verhaegen 					__func__, i, ret);
248fc219bfdSLuc Verhaegen 				clk_put(par->clks[i]);
249fc219bfdSLuc Verhaegen 				par->clks[i] = NULL;
250fc219bfdSLuc Verhaegen 			}
251fc219bfdSLuc Verhaegen 		}
252fc219bfdSLuc Verhaegen 	}
253fc219bfdSLuc Verhaegen 
254fc219bfdSLuc Verhaegen 	return 0;
255fc219bfdSLuc Verhaegen }
256fc219bfdSLuc Verhaegen 
257fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par)
258fc219bfdSLuc Verhaegen {
259fc219bfdSLuc Verhaegen 	int i;
260fc219bfdSLuc Verhaegen 
261fc219bfdSLuc Verhaegen 	if (!par->clks)
262fc219bfdSLuc Verhaegen 		return;
263fc219bfdSLuc Verhaegen 
264fc219bfdSLuc Verhaegen 	for (i = 0; i < par->clk_count; i++) {
265fc219bfdSLuc Verhaegen 		if (par->clks[i]) {
266fc219bfdSLuc Verhaegen 			clk_disable_unprepare(par->clks[i]);
267fc219bfdSLuc Verhaegen 			clk_put(par->clks[i]);
268fc219bfdSLuc Verhaegen 		}
269fc219bfdSLuc Verhaegen 	}
270fc219bfdSLuc Verhaegen 
271fc219bfdSLuc Verhaegen 	kfree(par->clks);
272fc219bfdSLuc Verhaegen }
273fc219bfdSLuc Verhaegen #else
274fc219bfdSLuc Verhaegen static int simplefb_clocks_init(struct simplefb_par *par,
275fc219bfdSLuc Verhaegen 	struct platform_device *pdev) { return 0; }
276fc219bfdSLuc Verhaegen static void simplefb_clocks_destroy(struct simplefb_par *par) { }
277fc219bfdSLuc Verhaegen #endif
278fc219bfdSLuc Verhaegen 
279814740e7SChen-Yu Tsai #if defined CONFIG_OF && defined CONFIG_REGULATOR
280814740e7SChen-Yu Tsai 
281814740e7SChen-Yu Tsai #define SUPPLY_SUFFIX "-supply"
282814740e7SChen-Yu Tsai 
283814740e7SChen-Yu Tsai /*
284814740e7SChen-Yu Tsai  * Regulator handling code.
285814740e7SChen-Yu Tsai  *
286814740e7SChen-Yu Tsai  * Here we handle the num-supplies and vin*-supply properties of our
287814740e7SChen-Yu Tsai  * "simple-framebuffer" dt node. This is necessary so that we can make sure
288814740e7SChen-Yu Tsai  * that any regulators needed by the display hardware that the bootloader
289814740e7SChen-Yu Tsai  * set up for us (and for which it provided a simplefb dt node), stay up,
290814740e7SChen-Yu Tsai  * for the life of the simplefb driver.
291814740e7SChen-Yu Tsai  *
292814740e7SChen-Yu Tsai  * When the driver unloads, we cleanly disable, and then release the
293814740e7SChen-Yu Tsai  * regulators.
294814740e7SChen-Yu Tsai  *
295814740e7SChen-Yu Tsai  * We only complain about errors here, no action is taken as the most likely
296814740e7SChen-Yu Tsai  * error can only happen due to a mismatch between the bootloader which set
297814740e7SChen-Yu Tsai  * up simplefb, and the regulator definitions in the device tree. Chances are
298814740e7SChen-Yu Tsai  * that there are no adverse effects, and if there are, a clean teardown of
299814740e7SChen-Yu Tsai  * the fb probe will not help us much either. So just complain and carry on,
300814740e7SChen-Yu Tsai  * and hope that the user actually gets a working fb at the end of things.
301814740e7SChen-Yu Tsai  */
302814740e7SChen-Yu Tsai static int simplefb_regulators_init(struct simplefb_par *par,
303814740e7SChen-Yu Tsai 	struct platform_device *pdev)
304814740e7SChen-Yu Tsai {
305814740e7SChen-Yu Tsai 	struct device_node *np = pdev->dev.of_node;
306814740e7SChen-Yu Tsai 	struct property *prop;
307814740e7SChen-Yu Tsai 	struct regulator *regulator;
308814740e7SChen-Yu Tsai 	const char *p;
309814740e7SChen-Yu Tsai 	int count = 0, i = 0, ret;
310814740e7SChen-Yu Tsai 
311814740e7SChen-Yu Tsai 	if (dev_get_platdata(&pdev->dev) || !np)
312814740e7SChen-Yu Tsai 		return 0;
313814740e7SChen-Yu Tsai 
314814740e7SChen-Yu Tsai 	/* Count the number of regulator supplies */
315814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
316814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
317814740e7SChen-Yu Tsai 		if (p && p != prop->name)
318814740e7SChen-Yu Tsai 			count++;
319814740e7SChen-Yu Tsai 	}
320814740e7SChen-Yu Tsai 
321814740e7SChen-Yu Tsai 	if (!count)
322814740e7SChen-Yu Tsai 		return 0;
323814740e7SChen-Yu Tsai 
324814740e7SChen-Yu Tsai 	par->regulators = devm_kcalloc(&pdev->dev, count,
325814740e7SChen-Yu Tsai 				       sizeof(struct regulator *), GFP_KERNEL);
326814740e7SChen-Yu Tsai 	if (!par->regulators)
327814740e7SChen-Yu Tsai 		return -ENOMEM;
328814740e7SChen-Yu Tsai 
329814740e7SChen-Yu Tsai 	/* Get all the regulators */
330814740e7SChen-Yu Tsai 	for_each_property_of_node(np, prop) {
331814740e7SChen-Yu Tsai 		char name[32]; /* 32 is max size of property name */
332814740e7SChen-Yu Tsai 
333814740e7SChen-Yu Tsai 		p = strstr(prop->name, SUPPLY_SUFFIX);
334814740e7SChen-Yu Tsai 		if (!p || p == prop->name)
335814740e7SChen-Yu Tsai 			continue;
336814740e7SChen-Yu Tsai 
337814740e7SChen-Yu Tsai 		strlcpy(name, prop->name,
338814740e7SChen-Yu Tsai 			strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
339814740e7SChen-Yu Tsai 		regulator = devm_regulator_get_optional(&pdev->dev, name);
340814740e7SChen-Yu Tsai 		if (IS_ERR(regulator)) {
341814740e7SChen-Yu Tsai 			if (PTR_ERR(regulator) == -EPROBE_DEFER)
342814740e7SChen-Yu Tsai 				return -EPROBE_DEFER;
343814740e7SChen-Yu Tsai 			dev_err(&pdev->dev, "regulator %s not found: %ld\n",
344814740e7SChen-Yu Tsai 				name, PTR_ERR(regulator));
345814740e7SChen-Yu Tsai 			continue;
346814740e7SChen-Yu Tsai 		}
347814740e7SChen-Yu Tsai 		par->regulators[i++] = regulator;
348814740e7SChen-Yu Tsai 	}
349814740e7SChen-Yu Tsai 	par->regulator_count = i;
350814740e7SChen-Yu Tsai 
351814740e7SChen-Yu Tsai 	/* Enable all the regulators */
352814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++) {
353814740e7SChen-Yu Tsai 		ret = regulator_enable(par->regulators[i]);
354814740e7SChen-Yu Tsai 		if (ret) {
355814740e7SChen-Yu Tsai 			dev_err(&pdev->dev,
356814740e7SChen-Yu Tsai 				"failed to enable regulator %d: %d\n",
357814740e7SChen-Yu Tsai 				i, ret);
358814740e7SChen-Yu Tsai 			devm_regulator_put(par->regulators[i]);
359814740e7SChen-Yu Tsai 			par->regulators[i] = NULL;
360814740e7SChen-Yu Tsai 		}
361814740e7SChen-Yu Tsai 	}
362814740e7SChen-Yu Tsai 
363814740e7SChen-Yu Tsai 	return 0;
364814740e7SChen-Yu Tsai }
365814740e7SChen-Yu Tsai 
366814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par)
367814740e7SChen-Yu Tsai {
368814740e7SChen-Yu Tsai 	int i;
369814740e7SChen-Yu Tsai 
370814740e7SChen-Yu Tsai 	if (!par->regulators)
371814740e7SChen-Yu Tsai 		return;
372814740e7SChen-Yu Tsai 
373814740e7SChen-Yu Tsai 	for (i = 0; i < par->regulator_count; i++)
374814740e7SChen-Yu Tsai 		if (par->regulators[i])
375814740e7SChen-Yu Tsai 			regulator_disable(par->regulators[i]);
376814740e7SChen-Yu Tsai }
377814740e7SChen-Yu Tsai #else
378814740e7SChen-Yu Tsai static int simplefb_regulators_init(struct simplefb_par *par,
379814740e7SChen-Yu Tsai 	struct platform_device *pdev) { return 0; }
380814740e7SChen-Yu Tsai static void simplefb_regulators_destroy(struct simplefb_par *par) { }
381814740e7SChen-Yu Tsai #endif
382814740e7SChen-Yu Tsai 
383f7018c21STomi Valkeinen static int simplefb_probe(struct platform_device *pdev)
384f7018c21STomi Valkeinen {
385f7018c21STomi Valkeinen 	int ret;
386f7018c21STomi Valkeinen 	struct simplefb_params params;
387f7018c21STomi Valkeinen 	struct fb_info *info;
3881270be4aSLuc Verhaegen 	struct simplefb_par *par;
389f7018c21STomi Valkeinen 	struct resource *mem;
390f7018c21STomi Valkeinen 
391f7018c21STomi Valkeinen 	if (fb_get_options("simplefb", NULL))
392f7018c21STomi Valkeinen 		return -ENODEV;
393f7018c21STomi Valkeinen 
394f7018c21STomi Valkeinen 	ret = -ENODEV;
395f7018c21STomi Valkeinen 	if (dev_get_platdata(&pdev->dev))
396f7018c21STomi Valkeinen 		ret = simplefb_parse_pd(pdev, &params);
397f7018c21STomi Valkeinen 	else if (pdev->dev.of_node)
398f7018c21STomi Valkeinen 		ret = simplefb_parse_dt(pdev, &params);
399f7018c21STomi Valkeinen 
400f7018c21STomi Valkeinen 	if (ret)
401f7018c21STomi Valkeinen 		return ret;
402f7018c21STomi Valkeinen 
403f7018c21STomi Valkeinen 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
404f7018c21STomi Valkeinen 	if (!mem) {
405f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "No memory resource\n");
406f7018c21STomi Valkeinen 		return -EINVAL;
407f7018c21STomi Valkeinen 	}
408f7018c21STomi Valkeinen 
4091270be4aSLuc Verhaegen 	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
410f7018c21STomi Valkeinen 	if (!info)
411f7018c21STomi Valkeinen 		return -ENOMEM;
412f7018c21STomi Valkeinen 	platform_set_drvdata(pdev, info);
413f7018c21STomi Valkeinen 
4141270be4aSLuc Verhaegen 	par = info->par;
4151270be4aSLuc Verhaegen 
416f7018c21STomi Valkeinen 	info->fix = simplefb_fix;
417f7018c21STomi Valkeinen 	info->fix.smem_start = mem->start;
418f7018c21STomi Valkeinen 	info->fix.smem_len = resource_size(mem);
419f7018c21STomi Valkeinen 	info->fix.line_length = params.stride;
420f7018c21STomi Valkeinen 
421f7018c21STomi Valkeinen 	info->var = simplefb_var;
422f7018c21STomi Valkeinen 	info->var.xres = params.width;
423f7018c21STomi Valkeinen 	info->var.yres = params.height;
424f7018c21STomi Valkeinen 	info->var.xres_virtual = params.width;
425f7018c21STomi Valkeinen 	info->var.yres_virtual = params.height;
426f7018c21STomi Valkeinen 	info->var.bits_per_pixel = params.format->bits_per_pixel;
427f7018c21STomi Valkeinen 	info->var.red = params.format->red;
428f7018c21STomi Valkeinen 	info->var.green = params.format->green;
429f7018c21STomi Valkeinen 	info->var.blue = params.format->blue;
430f7018c21STomi Valkeinen 	info->var.transp = params.format->transp;
431f7018c21STomi Valkeinen 
432f7018c21STomi Valkeinen 	info->apertures = alloc_apertures(1);
433f7018c21STomi Valkeinen 	if (!info->apertures) {
434bf2fda15SLuc Verhaegen 		ret = -ENOMEM;
435bf2fda15SLuc Verhaegen 		goto error_fb_release;
436f7018c21STomi Valkeinen 	}
437f7018c21STomi Valkeinen 	info->apertures->ranges[0].base = info->fix.smem_start;
438f7018c21STomi Valkeinen 	info->apertures->ranges[0].size = info->fix.smem_len;
439f7018c21STomi Valkeinen 
440f7018c21STomi Valkeinen 	info->fbops = &simplefb_ops;
441f7018c21STomi Valkeinen 	info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
442f7018c21STomi Valkeinen 	info->screen_base = ioremap_wc(info->fix.smem_start,
443f7018c21STomi Valkeinen 				       info->fix.smem_len);
444f7018c21STomi Valkeinen 	if (!info->screen_base) {
445bf2fda15SLuc Verhaegen 		ret = -ENOMEM;
446bf2fda15SLuc Verhaegen 		goto error_fb_release;
447f7018c21STomi Valkeinen 	}
4481270be4aSLuc Verhaegen 	info->pseudo_palette = par->palette;
449f7018c21STomi Valkeinen 
450fc219bfdSLuc Verhaegen 	ret = simplefb_clocks_init(par, pdev);
451fc219bfdSLuc Verhaegen 	if (ret < 0)
452fc219bfdSLuc Verhaegen 		goto error_unmap;
453fc219bfdSLuc Verhaegen 
454814740e7SChen-Yu Tsai 	ret = simplefb_regulators_init(par, pdev);
455814740e7SChen-Yu Tsai 	if (ret < 0)
456814740e7SChen-Yu Tsai 		goto error_clocks;
457814740e7SChen-Yu Tsai 
458f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
459f7018c21STomi Valkeinen 			     info->fix.smem_start, info->fix.smem_len,
460f7018c21STomi Valkeinen 			     info->screen_base);
461f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
462f7018c21STomi Valkeinen 			     params.format->name,
463f7018c21STomi Valkeinen 			     info->var.xres, info->var.yres,
464f7018c21STomi Valkeinen 			     info->var.bits_per_pixel, info->fix.line_length);
465f7018c21STomi Valkeinen 
466f7018c21STomi Valkeinen 	ret = register_framebuffer(info);
467f7018c21STomi Valkeinen 	if (ret < 0) {
468f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
469814740e7SChen-Yu Tsai 		goto error_regulators;
470f7018c21STomi Valkeinen 	}
471f7018c21STomi Valkeinen 
472f7018c21STomi Valkeinen 	dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
473f7018c21STomi Valkeinen 
474f7018c21STomi Valkeinen 	return 0;
475bf2fda15SLuc Verhaegen 
476814740e7SChen-Yu Tsai error_regulators:
477814740e7SChen-Yu Tsai 	simplefb_regulators_destroy(par);
478fc219bfdSLuc Verhaegen error_clocks:
479fc219bfdSLuc Verhaegen 	simplefb_clocks_destroy(par);
480bf2fda15SLuc Verhaegen error_unmap:
481bf2fda15SLuc Verhaegen 	iounmap(info->screen_base);
482bf2fda15SLuc Verhaegen error_fb_release:
483bf2fda15SLuc Verhaegen 	framebuffer_release(info);
484bf2fda15SLuc Verhaegen 	return ret;
485f7018c21STomi Valkeinen }
486f7018c21STomi Valkeinen 
487f7018c21STomi Valkeinen static int simplefb_remove(struct platform_device *pdev)
488f7018c21STomi Valkeinen {
489f7018c21STomi Valkeinen 	struct fb_info *info = platform_get_drvdata(pdev);
490fc219bfdSLuc Verhaegen 	struct simplefb_par *par = info->par;
491f7018c21STomi Valkeinen 
492f7018c21STomi Valkeinen 	unregister_framebuffer(info);
493814740e7SChen-Yu Tsai 	simplefb_regulators_destroy(par);
494fc219bfdSLuc Verhaegen 	simplefb_clocks_destroy(par);
495f7018c21STomi Valkeinen 	framebuffer_release(info);
496f7018c21STomi Valkeinen 
497f7018c21STomi Valkeinen 	return 0;
498f7018c21STomi Valkeinen }
499f7018c21STomi Valkeinen 
500f7018c21STomi Valkeinen static const struct of_device_id simplefb_of_match[] = {
501f7018c21STomi Valkeinen 	{ .compatible = "simple-framebuffer", },
502f7018c21STomi Valkeinen 	{ },
503f7018c21STomi Valkeinen };
504f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(of, simplefb_of_match);
505f7018c21STomi Valkeinen 
506f7018c21STomi Valkeinen static struct platform_driver simplefb_driver = {
507f7018c21STomi Valkeinen 	.driver = {
508f7018c21STomi Valkeinen 		.name = "simple-framebuffer",
509f7018c21STomi Valkeinen 		.of_match_table = simplefb_of_match,
510f7018c21STomi Valkeinen 	},
511f7018c21STomi Valkeinen 	.probe = simplefb_probe,
512f7018c21STomi Valkeinen 	.remove = simplefb_remove,
513f7018c21STomi Valkeinen };
51489c95001SHans de Goede 
51589c95001SHans de Goede static int __init simplefb_init(void)
51689c95001SHans de Goede {
51789c95001SHans de Goede 	int ret;
51889c95001SHans de Goede 	struct device_node *np;
51989c95001SHans de Goede 
52089c95001SHans de Goede 	ret = platform_driver_register(&simplefb_driver);
52189c95001SHans de Goede 	if (ret)
52289c95001SHans de Goede 		return ret;
52389c95001SHans de Goede 
524d9e02019SHans de Goede 	if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
52589c95001SHans de Goede 		for_each_child_of_node(of_chosen, np) {
52689c95001SHans de Goede 			if (of_device_is_compatible(np, "simple-framebuffer"))
52789c95001SHans de Goede 				of_platform_device_create(np, NULL, NULL);
52889c95001SHans de Goede 		}
52989c95001SHans de Goede 	}
53089c95001SHans de Goede 
53189c95001SHans de Goede 	return 0;
53289c95001SHans de Goede }
53389c95001SHans de Goede 
5340c5b240cSHans de Goede fs_initcall(simplefb_init);
535f7018c21STomi Valkeinen 
536f7018c21STomi Valkeinen MODULE_AUTHOR("Stephen Warren <[email protected]>");
537f7018c21STomi Valkeinen MODULE_DESCRIPTION("Simple framebuffer driver");
538f7018c21STomi Valkeinen MODULE_LICENSE("GPL v2");
539