File Index
Files and directories
Sign in
FILE

simplefb.c

File preview with syntax highlighting.

linux-6.19.14/drivers/video/fbdev/simplefb.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Simplest possible simple frame-buffer driver, as a platform device
  4 *
  5 * Copyright (c) 2013, Stephen Warren
  6 *
  7 * Based on q40fb.c, which was:
  8 * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org>
  9 *
 10 * Also based on offb.c, which was:
 11 * Copyright (C) 1997 Geert Uytterhoeven
 12 * Copyright (C) 1996 Paul Mackerras
 13 */
 14
 15#include <linux/aperture.h>
 16#include <linux/clk.h>
 17#include <linux/errno.h>
 18#include <linux/fb.h>
 19#include <linux/io.h>
 20#include <linux/module.h>
 21#include <linux/of.h>
 22#include <linux/of_clk.h>
 23#include <linux/of_platform.h>
 24#include <linux/of_reserved_mem.h>
 25#include <linux/parser.h>
 26#include <linux/platform_data/simplefb.h>
 27#include <linux/platform_device.h>
 28#include <linux/pm_domain.h>
 29#include <linux/regulator/consumer.h>
 30
 31static const struct fb_fix_screeninfo simplefb_fix = {
 32	.id		= "simple",
 33	.type		= FB_TYPE_PACKED_PIXELS,
 34	.visual		= FB_VISUAL_TRUECOLOR,
 35	.accel		= FB_ACCEL_NONE,
 36};
 37
 38static const struct fb_var_screeninfo simplefb_var = {
 39	.height		= -1,
 40	.width		= -1,
 41	.activate	= FB_ACTIVATE_NOW,
 42	.vmode		= FB_VMODE_NONINTERLACED,
 43};
 44
 45#define PSEUDO_PALETTE_SIZE 16
 46
 47static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 48			      u_int transp, struct fb_info *info)
 49{
 50	u32 *pal = info->pseudo_palette;
 51	u32 cr = red >> (16 - info->var.red.length);
 52	u32 cg = green >> (16 - info->var.green.length);
 53	u32 cb = blue >> (16 - info->var.blue.length);
 54	u32 value;
 55
 56	if (regno >= PSEUDO_PALETTE_SIZE)
 57		return -EINVAL;
 58
 59	value = (cr << info->var.red.offset) |
 60		(cg << info->var.green.offset) |
 61		(cb << info->var.blue.offset);
 62	if (info->var.transp.length > 0) {
 63		u32 mask = (1 << info->var.transp.length) - 1;
 64		mask <<= info->var.transp.offset;
 65		value |= mask;
 66	}
 67	pal[regno] = value;
 68
 69	return 0;
 70}
 71
 72struct simplefb_par {
 73	u32 palette[PSEUDO_PALETTE_SIZE];
 74	resource_size_t base;
 75	resource_size_t size;
 76	struct resource *mem;
 77#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
 78	bool clks_enabled;
 79	unsigned int clk_count;
 80	struct clk **clks;
 81#endif
 82#if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
 83	unsigned int num_genpds;
 84	struct device **genpds;
 85	struct device_link **genpd_links;
 86#endif
 87#if defined CONFIG_OF && defined CONFIG_REGULATOR
 88	bool regulators_enabled;
 89	u32 regulator_count;
 90	struct regulator **regulators;
 91#endif
 92};
 93
 94static void simplefb_clocks_destroy(struct simplefb_par *par);
 95static void simplefb_regulators_destroy(struct simplefb_par *par);
 96static void simplefb_detach_genpds(void *res);
 97
 98/*
 99 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
100 * of unregister_framebuffer() or fb_release(). Do any cleanup here.
101 */
102static void simplefb_destroy(struct fb_info *info)
103{
104	struct simplefb_par *par = info->par;
105	struct resource *mem = par->mem;
106
107	simplefb_regulators_destroy(info->par);
108	simplefb_clocks_destroy(info->par);
109	simplefb_detach_genpds(info->par);
110	if (info->screen_base)
111		iounmap(info->screen_base);
112
113	framebuffer_release(info);
114
115	if (mem)
116		release_mem_region(mem->start, resource_size(mem));
117}
118
119static const struct fb_ops simplefb_ops = {
120	.owner		= THIS_MODULE,
121	FB_DEFAULT_IOMEM_OPS,
122	.fb_destroy	= simplefb_destroy,
123	.fb_setcolreg	= simplefb_setcolreg,
124};
125
126static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
127
128struct simplefb_params {
129	u32 width;
130	u32 height;
131	u32 stride;
132	struct simplefb_format *format;
133	struct resource memory;
134};
135
136static int simplefb_parse_dt(struct platform_device *pdev,
137			   struct simplefb_params *params)
138{
139	struct device_node *np = pdev->dev.of_node;
140	int ret;
141	const char *format;
142	int i;
143
144	ret = of_property_read_u32(np, "width", &params->width);
145	if (ret) {
146		dev_err(&pdev->dev, "Can't parse width property\n");
147		return ret;
148	}
149
150	ret = of_property_read_u32(np, "height", &params->height);
151	if (ret) {
152		dev_err(&pdev->dev, "Can't parse height property\n");
153		return ret;
154	}
155
156	ret = of_property_read_u32(np, "stride", &params->stride);
157	if (ret) {
158		dev_err(&pdev->dev, "Can't parse stride property\n");
159		return ret;
160	}
161
162	ret = of_property_read_string(np, "format", &format);
163	if (ret) {
164		dev_err(&pdev->dev, "Can't parse format property\n");
165		return ret;
166	}
167	params->format = NULL;
168	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
169		if (strcmp(format, simplefb_formats[i].name))
170			continue;
171		params->format = &simplefb_formats[i];
172		break;
173	}
174	if (!params->format) {
175		dev_err(&pdev->dev, "Invalid format value\n");
176		return -EINVAL;
177	}
178
179	ret = of_reserved_mem_region_to_resource(np, 0, &params->memory);
180	if (!ret) {
181		if (of_property_present(np, "reg"))
182			dev_warn(&pdev->dev, "preferring \"memory-region\" over \"reg\" property\n");
183	} else {
184		memset(&params->memory, 0, sizeof(params->memory));
185	}
186
187	return 0;
188}
189
190static int simplefb_parse_pd(struct platform_device *pdev,
191			     struct simplefb_params *params)
192{
193	struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
194	int i;
195
196	params->width = pd->width;
197	params->height = pd->height;
198	params->stride = pd->stride;
199
200	params->format = NULL;
201	for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
202		if (strcmp(pd->format, simplefb_formats[i].name))
203			continue;
204
205		params->format = &simplefb_formats[i];
206		break;
207	}
208
209	if (!params->format) {
210		dev_err(&pdev->dev, "Invalid format value\n");
211		return -EINVAL;
212	}
213
214	memset(&params->memory, 0, sizeof(params->memory));
215
216	return 0;
217}
218
219#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
220/*
221 * Clock handling code.
222 *
223 * Here we handle the clocks property of our "simple-framebuffer" dt node.
224 * This is necessary so that we can make sure that any clocks needed by
225 * the display engine that the bootloader set up for us (and for which it
226 * provided a simplefb dt node), stay up, for the life of the simplefb
227 * driver.
228 *
229 * When the driver unloads, we cleanly disable, and then release the clocks.
230 *
231 * We only complain about errors here, no action is taken as the most likely
232 * error can only happen due to a mismatch between the bootloader which set
233 * up simplefb, and the clock definitions in the device tree. Chances are
234 * that there are no adverse effects, and if there are, a clean teardown of
235 * the fb probe will not help us much either. So just complain and carry on,
236 * and hope that the user actually gets a working fb at the end of things.
237 */
238static int simplefb_clocks_get(struct simplefb_par *par,
239			       struct platform_device *pdev)
240{
241	struct device_node *np = pdev->dev.of_node;
242	struct clk *clock;
243	int i;
244
245	if (dev_get_platdata(&pdev->dev) || !np)
246		return 0;
247
248	par->clk_count = of_clk_get_parent_count(np);
249	if (!par->clk_count)
250		return 0;
251
252	par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
253	if (!par->clks)
254		return -ENOMEM;
255
256	for (i = 0; i < par->clk_count; i++) {
257		clock = of_clk_get(np, i);
258		if (IS_ERR(clock)) {
259			if (PTR_ERR(clock) == -EPROBE_DEFER) {
260				while (--i >= 0) {
261					clk_put(par->clks[i]);
262				}
263				kfree(par->clks);
264				return -EPROBE_DEFER;
265			}
266			dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
267				__func__, i, PTR_ERR(clock));
268			continue;
269		}
270		par->clks[i] = clock;
271	}
272
273	return 0;
274}
275
276static void simplefb_clocks_enable(struct simplefb_par *par,
277				   struct platform_device *pdev)
278{
279	int i, ret;
280
281	for (i = 0; i < par->clk_count; i++) {
282		if (par->clks[i]) {
283			ret = clk_prepare_enable(par->clks[i]);
284			if (ret) {
285				dev_err(&pdev->dev,
286					"%s: failed to enable clock %d: %d\n",
287					__func__, i, ret);
288				clk_put(par->clks[i]);
289				par->clks[i] = NULL;
290			}
291		}
292	}
293	par->clks_enabled = true;
294}
295
296static void simplefb_clocks_destroy(struct simplefb_par *par)
297{
298	int i;
299
300	if (!par->clks)
301		return;
302
303	for (i = 0; i < par->clk_count; i++) {
304		if (par->clks[i]) {
305			if (par->clks_enabled)
306				clk_disable_unprepare(par->clks[i]);
307			clk_put(par->clks[i]);
308		}
309	}
310
311	kfree(par->clks);
312}
313#else
314static int simplefb_clocks_get(struct simplefb_par *par,
315	struct platform_device *pdev) { return 0; }
316static void simplefb_clocks_enable(struct simplefb_par *par,
317	struct platform_device *pdev) { }
318static void simplefb_clocks_destroy(struct simplefb_par *par) { }
319#endif
320
321#if defined CONFIG_OF && defined CONFIG_REGULATOR
322
323#define SUPPLY_SUFFIX "-supply"
324
325/*
326 * Regulator handling code.
327 *
328 * Here we handle the num-supplies and vin*-supply properties of our
329 * "simple-framebuffer" dt node. This is necessary so that we can make sure
330 * that any regulators needed by the display hardware that the bootloader
331 * set up for us (and for which it provided a simplefb dt node), stay up,
332 * for the life of the simplefb driver.
333 *
334 * When the driver unloads, we cleanly disable, and then release the
335 * regulators.
336 *
337 * We only complain about errors here, no action is taken as the most likely
338 * error can only happen due to a mismatch between the bootloader which set
339 * up simplefb, and the regulator definitions in the device tree. Chances are
340 * that there are no adverse effects, and if there are, a clean teardown of
341 * the fb probe will not help us much either. So just complain and carry on,
342 * and hope that the user actually gets a working fb at the end of things.
343 */
344static int simplefb_regulators_get(struct simplefb_par *par,
345				   struct platform_device *pdev)
346{
347	struct device_node *np = pdev->dev.of_node;
348	struct property *prop;
349	struct regulator *regulator;
350	const char *p;
351	int count = 0, i = 0;
352
353	if (dev_get_platdata(&pdev->dev) || !np)
354		return 0;
355
356	/* Count the number of regulator supplies */
357	for_each_property_of_node(np, prop) {
358		p = strstr(prop->name, SUPPLY_SUFFIX);
359		if (p && p != prop->name)
360			count++;
361	}
362
363	if (!count)
364		return 0;
365
366	par->regulators = devm_kcalloc(&pdev->dev, count,
367				       sizeof(struct regulator *), GFP_KERNEL);
368	if (!par->regulators)
369		return -ENOMEM;
370
371	/* Get all the regulators */
372	for_each_property_of_node(np, prop) {
373		char name[32]; /* 32 is max size of property name */
374
375		p = strstr(prop->name, SUPPLY_SUFFIX);
376		if (!p || p == prop->name)
377			continue;
378
379		strscpy(name, prop->name,
380			strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
381		regulator = devm_regulator_get_optional(&pdev->dev, name);
382		if (IS_ERR(regulator)) {
383			if (PTR_ERR(regulator) == -EPROBE_DEFER)
384				return -EPROBE_DEFER;
385			dev_err(&pdev->dev, "regulator %s not found: %ld\n",
386				name, PTR_ERR(regulator));
387			continue;
388		}
389		par->regulators[i++] = regulator;
390	}
391	par->regulator_count = i;
392
393	return 0;
394}
395
396static void simplefb_regulators_enable(struct simplefb_par *par,
397				       struct platform_device *pdev)
398{
399	int i, ret;
400
401	/* Enable all the regulators */
402	for (i = 0; i < par->regulator_count; i++) {
403		ret = regulator_enable(par->regulators[i]);
404		if (ret) {
405			dev_err(&pdev->dev,
406				"failed to enable regulator %d: %d\n",
407				i, ret);
408			devm_regulator_put(par->regulators[i]);
409			par->regulators[i] = NULL;
410		}
411	}
412	par->regulators_enabled = true;
413}
414
415static void simplefb_regulators_destroy(struct simplefb_par *par)
416{
417	int i;
418
419	if (!par->regulators || !par->regulators_enabled)
420		return;
421
422	for (i = 0; i < par->regulator_count; i++)
423		if (par->regulators[i])
424			regulator_disable(par->regulators[i]);
425}
426#else
427static int simplefb_regulators_get(struct simplefb_par *par,
428	struct platform_device *pdev) { return 0; }
429static void simplefb_regulators_enable(struct simplefb_par *par,
430	struct platform_device *pdev) { }
431static void simplefb_regulators_destroy(struct simplefb_par *par) { }
432#endif
433
434#if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
435static void simplefb_detach_genpds(void *res)
436{
437	struct simplefb_par *par = res;
438	unsigned int i = par->num_genpds;
439
440	if (par->num_genpds <= 1)
441		return;
442
443	while (i--) {
444		if (par->genpd_links[i])
445			device_link_del(par->genpd_links[i]);
446
447		if (!IS_ERR_OR_NULL(par->genpds[i]))
448			dev_pm_domain_detach(par->genpds[i], true);
449	}
450	par->num_genpds = 0;
451}
452
453static int simplefb_attach_genpds(struct simplefb_par *par,
454				  struct platform_device *pdev)
455{
456	struct device *dev = &pdev->dev;
457	unsigned int i, num_genpds;
458	int err;
459
460	err = of_count_phandle_with_args(dev->of_node, "power-domains",
461					 "#power-domain-cells");
462	if (err < 0) {
463		/* Nothing wrong if optional PDs are missing */
464		if (err == -ENOENT)
465			return 0;
466
467		dev_err(dev, "failed to parse power-domains: %d\n", err);
468		return err;
469	}
470
471	num_genpds = err;
472
473	/*
474	 * Single power-domain devices are handled by the driver core, so
475	 * nothing to do here.
476	 */
477	if (num_genpds <= 1) {
478		par->num_genpds = num_genpds;
479		return 0;
480	}
481
482	par->genpds = devm_kcalloc(dev, num_genpds, sizeof(*par->genpds),
483				   GFP_KERNEL);
484	if (!par->genpds)
485		return -ENOMEM;
486
487	par->genpd_links = devm_kcalloc(dev, num_genpds,
488					sizeof(*par->genpd_links),
489					GFP_KERNEL);
490	if (!par->genpd_links)
491		return -ENOMEM;
492
493	/*
494	 * Set par->num_genpds only after genpds and genpd_links are allocated
495	 * to exit early from simplefb_detach_genpds() without full
496	 * initialisation.
497	 */
498	par->num_genpds = num_genpds;
499
500	for (i = 0; i < par->num_genpds; i++) {
501		par->genpds[i] = dev_pm_domain_attach_by_id(dev, i);
502		if (IS_ERR(par->genpds[i])) {
503			err = PTR_ERR(par->genpds[i]);
504			if (err == -EPROBE_DEFER) {
505				simplefb_detach_genpds(par);
506				return err;
507			}
508
509			dev_warn(dev, "failed to attach domain %u: %d\n", i, err);
510			continue;
511		}
512
513		par->genpd_links[i] = device_link_add(dev, par->genpds[i],
514						      DL_FLAG_STATELESS |
515						      DL_FLAG_PM_RUNTIME |
516						      DL_FLAG_RPM_ACTIVE);
517		if (!par->genpd_links[i])
518			dev_warn(dev, "failed to link power-domain %u\n", i);
519	}
520
521	return 0;
522}
523#else
524static void simplefb_detach_genpds(void *res) { }
525static int simplefb_attach_genpds(struct simplefb_par *par,
526				  struct platform_device *pdev)
527{
528	return 0;
529}
530#endif
531
532static int simplefb_probe(struct platform_device *pdev)
533{
534	int ret;
535	struct simplefb_params params;
536	struct fb_info *info;
537	struct simplefb_par *par;
538	struct resource *res, *mem;
539
540	if (fb_get_options("simplefb", NULL))
541		return -ENODEV;
542
543	ret = -ENODEV;
544	if (dev_get_platdata(&pdev->dev))
545		ret = simplefb_parse_pd(pdev, &params);
546	else if (pdev->dev.of_node)
547		ret = simplefb_parse_dt(pdev, &params);
548
549	if (ret)
550		return ret;
551
552	if (params.memory.start == 0 && params.memory.end == 0) {
553		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
554		if (!res) {
555			dev_err(&pdev->dev, "No memory resource\n");
556			return -EINVAL;
557		}
558	} else {
559		res = &params.memory;
560	}
561
562	mem = request_mem_region(res->start, resource_size(res), "simplefb");
563	if (!mem) {
564		/*
565		 * We cannot make this fatal. Sometimes this comes from magic
566		 * spaces our resource handlers simply don't know about. Use
567		 * the I/O-memory resource as-is and try to map that instead.
568		 */
569		dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res);
570		mem = res;
571	}
572
573	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
574	if (!info) {
575		ret = -ENOMEM;
576		goto error_release_mem_region;
577	}
578	platform_set_drvdata(pdev, info);
579
580	par = info->par;
581
582	info->fix = simplefb_fix;
583	info->fix.smem_start = mem->start;
584	info->fix.smem_len = resource_size(mem);
585	info->fix.line_length = params.stride;
586
587	info->var = simplefb_var;
588	info->var.xres = params.width;
589	info->var.yres = params.height;
590	info->var.xres_virtual = params.width;
591	info->var.yres_virtual = params.height;
592	info->var.bits_per_pixel = params.format->bits_per_pixel;
593	info->var.red = params.format->red;
594	info->var.green = params.format->green;
595	info->var.blue = params.format->blue;
596	info->var.transp = params.format->transp;
597
598	par->base = info->fix.smem_start;
599	par->size = info->fix.smem_len;
600
601	info->fbops = &simplefb_ops;
602	info->screen_base = ioremap_wc(info->fix.smem_start,
603				       info->fix.smem_len);
604	if (!info->screen_base) {
605		ret = -ENOMEM;
606		goto error_fb_release;
607	}
608	info->pseudo_palette = par->palette;
609
610	ret = simplefb_clocks_get(par, pdev);
611	if (ret < 0)
612		goto error_unmap;
613
614	ret = simplefb_regulators_get(par, pdev);
615	if (ret < 0)
616		goto error_clocks;
617
618	ret = simplefb_attach_genpds(par, pdev);
619	if (ret < 0)
620		goto error_regulators;
621
622	simplefb_clocks_enable(par, pdev);
623	simplefb_regulators_enable(par, pdev);
624
625	dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n",
626			     info->fix.smem_start, info->fix.smem_len);
627	dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
628			     params.format->name,
629			     info->var.xres, info->var.yres,
630			     info->var.bits_per_pixel, info->fix.line_length);
631
632	if (mem != res)
633		par->mem = mem; /* release in clean-up handler */
634
635	ret = devm_aperture_acquire_for_platform_device(pdev, par->base, par->size);
636	if (ret) {
637		dev_err(&pdev->dev, "Unable to acquire aperture: %d\n", ret);
638		goto error_genpds;
639	}
640	ret = register_framebuffer(info);
641	if (ret < 0) {
642		dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
643		goto error_genpds;
644	}
645
646	dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
647
648	return 0;
649
650error_genpds:
651	simplefb_detach_genpds(par);
652error_regulators:
653	simplefb_regulators_destroy(par);
654error_clocks:
655	simplefb_clocks_destroy(par);
656error_unmap:
657	iounmap(info->screen_base);
658error_fb_release:
659	framebuffer_release(info);
660error_release_mem_region:
661	if (mem != res)
662		release_mem_region(mem->start, resource_size(mem));
663	return ret;
664}
665
666static void simplefb_remove(struct platform_device *pdev)
667{
668	struct fb_info *info = platform_get_drvdata(pdev);
669
670	/* simplefb_destroy takes care of info cleanup */
671	unregister_framebuffer(info);
672}
673
674static const struct of_device_id simplefb_of_match[] = {
675	{ .compatible = "simple-framebuffer", },
676	{ },
677};
678MODULE_DEVICE_TABLE(of, simplefb_of_match);
679
680static struct platform_driver simplefb_driver = {
681	.driver = {
682		.name = "simple-framebuffer",
683		.of_match_table = simplefb_of_match,
684	},
685	.probe = simplefb_probe,
686	.remove = simplefb_remove,
687};
688
689module_platform_driver(simplefb_driver);
690
691MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
692MODULE_DESCRIPTION("Simple framebuffer driver");
693MODULE_LICENSE("GPL v2");
~/ Read-only file preview.