162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Simplest possible simple frame-buffer driver, as a platform device 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2013, Stephen Warren 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Based on q40fb.c, which was: 862306a36Sopenharmony_ci * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org> 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * Also based on offb.c, which was: 1162306a36Sopenharmony_ci * Copyright (C) 1997 Geert Uytterhoeven 1262306a36Sopenharmony_ci * Copyright (C) 1996 Paul Mackerras 1362306a36Sopenharmony_ci */ 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include <linux/aperture.h> 1662306a36Sopenharmony_ci#include <linux/errno.h> 1762306a36Sopenharmony_ci#include <linux/fb.h> 1862306a36Sopenharmony_ci#include <linux/io.h> 1962306a36Sopenharmony_ci#include <linux/module.h> 2062306a36Sopenharmony_ci#include <linux/platform_data/simplefb.h> 2162306a36Sopenharmony_ci#include <linux/platform_device.h> 2262306a36Sopenharmony_ci#include <linux/clk.h> 2362306a36Sopenharmony_ci#include <linux/of.h> 2462306a36Sopenharmony_ci#include <linux/of_clk.h> 2562306a36Sopenharmony_ci#include <linux/of_platform.h> 2662306a36Sopenharmony_ci#include <linux/parser.h> 2762306a36Sopenharmony_ci#include <linux/regulator/consumer.h> 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_cistatic const struct fb_fix_screeninfo simplefb_fix = { 3062306a36Sopenharmony_ci .id = "simple", 3162306a36Sopenharmony_ci .type = FB_TYPE_PACKED_PIXELS, 3262306a36Sopenharmony_ci .visual = FB_VISUAL_TRUECOLOR, 3362306a36Sopenharmony_ci .accel = FB_ACCEL_NONE, 3462306a36Sopenharmony_ci}; 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_cistatic const struct fb_var_screeninfo simplefb_var = { 3762306a36Sopenharmony_ci .height = -1, 3862306a36Sopenharmony_ci .width = -1, 3962306a36Sopenharmony_ci .activate = FB_ACTIVATE_NOW, 4062306a36Sopenharmony_ci .vmode = FB_VMODE_NONINTERLACED, 4162306a36Sopenharmony_ci}; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci#define PSEUDO_PALETTE_SIZE 16 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_cistatic int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue, 4662306a36Sopenharmony_ci u_int transp, struct fb_info *info) 4762306a36Sopenharmony_ci{ 4862306a36Sopenharmony_ci u32 *pal = info->pseudo_palette; 4962306a36Sopenharmony_ci u32 cr = red >> (16 - info->var.red.length); 5062306a36Sopenharmony_ci u32 cg = green >> (16 - info->var.green.length); 5162306a36Sopenharmony_ci u32 cb = blue >> (16 - info->var.blue.length); 5262306a36Sopenharmony_ci u32 value; 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci if (regno >= PSEUDO_PALETTE_SIZE) 5562306a36Sopenharmony_ci return -EINVAL; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci value = (cr << info->var.red.offset) | 5862306a36Sopenharmony_ci (cg << info->var.green.offset) | 5962306a36Sopenharmony_ci (cb << info->var.blue.offset); 6062306a36Sopenharmony_ci if (info->var.transp.length > 0) { 6162306a36Sopenharmony_ci u32 mask = (1 << info->var.transp.length) - 1; 6262306a36Sopenharmony_ci mask <<= info->var.transp.offset; 6362306a36Sopenharmony_ci value |= mask; 6462306a36Sopenharmony_ci } 6562306a36Sopenharmony_ci pal[regno] = value; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci return 0; 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cistruct simplefb_par { 7162306a36Sopenharmony_ci u32 palette[PSEUDO_PALETTE_SIZE]; 7262306a36Sopenharmony_ci resource_size_t base; 7362306a36Sopenharmony_ci resource_size_t size; 7462306a36Sopenharmony_ci struct resource *mem; 7562306a36Sopenharmony_ci#if defined CONFIG_OF && defined CONFIG_COMMON_CLK 7662306a36Sopenharmony_ci bool clks_enabled; 7762306a36Sopenharmony_ci unsigned int clk_count; 7862306a36Sopenharmony_ci struct clk **clks; 7962306a36Sopenharmony_ci#endif 8062306a36Sopenharmony_ci#if defined CONFIG_OF && defined CONFIG_REGULATOR 8162306a36Sopenharmony_ci bool regulators_enabled; 8262306a36Sopenharmony_ci u32 regulator_count; 8362306a36Sopenharmony_ci struct regulator **regulators; 8462306a36Sopenharmony_ci#endif 8562306a36Sopenharmony_ci}; 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_cistatic void simplefb_clocks_destroy(struct simplefb_par *par); 8862306a36Sopenharmony_cistatic void simplefb_regulators_destroy(struct simplefb_par *par); 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci/* 9162306a36Sopenharmony_ci * fb_ops.fb_destroy is called by the last put_fb_info() call at the end 9262306a36Sopenharmony_ci * of unregister_framebuffer() or fb_release(). Do any cleanup here. 9362306a36Sopenharmony_ci */ 9462306a36Sopenharmony_cistatic void simplefb_destroy(struct fb_info *info) 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci struct simplefb_par *par = info->par; 9762306a36Sopenharmony_ci struct resource *mem = par->mem; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci simplefb_regulators_destroy(info->par); 10062306a36Sopenharmony_ci simplefb_clocks_destroy(info->par); 10162306a36Sopenharmony_ci if (info->screen_base) 10262306a36Sopenharmony_ci iounmap(info->screen_base); 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci framebuffer_release(info); 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci if (mem) 10762306a36Sopenharmony_ci release_mem_region(mem->start, resource_size(mem)); 10862306a36Sopenharmony_ci} 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_cistatic const struct fb_ops simplefb_ops = { 11162306a36Sopenharmony_ci .owner = THIS_MODULE, 11262306a36Sopenharmony_ci FB_DEFAULT_IOMEM_OPS, 11362306a36Sopenharmony_ci .fb_destroy = simplefb_destroy, 11462306a36Sopenharmony_ci .fb_setcolreg = simplefb_setcolreg, 11562306a36Sopenharmony_ci}; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_cistatic struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS; 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_cistruct simplefb_params { 12062306a36Sopenharmony_ci u32 width; 12162306a36Sopenharmony_ci u32 height; 12262306a36Sopenharmony_ci u32 stride; 12362306a36Sopenharmony_ci struct simplefb_format *format; 12462306a36Sopenharmony_ci}; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_cistatic int simplefb_parse_dt(struct platform_device *pdev, 12762306a36Sopenharmony_ci struct simplefb_params *params) 12862306a36Sopenharmony_ci{ 12962306a36Sopenharmony_ci struct device_node *np = pdev->dev.of_node; 13062306a36Sopenharmony_ci int ret; 13162306a36Sopenharmony_ci const char *format; 13262306a36Sopenharmony_ci int i; 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci ret = of_property_read_u32(np, "width", ¶ms->width); 13562306a36Sopenharmony_ci if (ret) { 13662306a36Sopenharmony_ci dev_err(&pdev->dev, "Can't parse width property\n"); 13762306a36Sopenharmony_ci return ret; 13862306a36Sopenharmony_ci } 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci ret = of_property_read_u32(np, "height", ¶ms->height); 14162306a36Sopenharmony_ci if (ret) { 14262306a36Sopenharmony_ci dev_err(&pdev->dev, "Can't parse height property\n"); 14362306a36Sopenharmony_ci return ret; 14462306a36Sopenharmony_ci } 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci ret = of_property_read_u32(np, "stride", ¶ms->stride); 14762306a36Sopenharmony_ci if (ret) { 14862306a36Sopenharmony_ci dev_err(&pdev->dev, "Can't parse stride property\n"); 14962306a36Sopenharmony_ci return ret; 15062306a36Sopenharmony_ci } 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci ret = of_property_read_string(np, "format", &format); 15362306a36Sopenharmony_ci if (ret) { 15462306a36Sopenharmony_ci dev_err(&pdev->dev, "Can't parse format property\n"); 15562306a36Sopenharmony_ci return ret; 15662306a36Sopenharmony_ci } 15762306a36Sopenharmony_ci params->format = NULL; 15862306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) { 15962306a36Sopenharmony_ci if (strcmp(format, simplefb_formats[i].name)) 16062306a36Sopenharmony_ci continue; 16162306a36Sopenharmony_ci params->format = &simplefb_formats[i]; 16262306a36Sopenharmony_ci break; 16362306a36Sopenharmony_ci } 16462306a36Sopenharmony_ci if (!params->format) { 16562306a36Sopenharmony_ci dev_err(&pdev->dev, "Invalid format value\n"); 16662306a36Sopenharmony_ci return -EINVAL; 16762306a36Sopenharmony_ci } 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci return 0; 17062306a36Sopenharmony_ci} 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_cistatic int simplefb_parse_pd(struct platform_device *pdev, 17362306a36Sopenharmony_ci struct simplefb_params *params) 17462306a36Sopenharmony_ci{ 17562306a36Sopenharmony_ci struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev); 17662306a36Sopenharmony_ci int i; 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci params->width = pd->width; 17962306a36Sopenharmony_ci params->height = pd->height; 18062306a36Sopenharmony_ci params->stride = pd->stride; 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci params->format = NULL; 18362306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) { 18462306a36Sopenharmony_ci if (strcmp(pd->format, simplefb_formats[i].name)) 18562306a36Sopenharmony_ci continue; 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci params->format = &simplefb_formats[i]; 18862306a36Sopenharmony_ci break; 18962306a36Sopenharmony_ci } 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci if (!params->format) { 19262306a36Sopenharmony_ci dev_err(&pdev->dev, "Invalid format value\n"); 19362306a36Sopenharmony_ci return -EINVAL; 19462306a36Sopenharmony_ci } 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci return 0; 19762306a36Sopenharmony_ci} 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci#if defined CONFIG_OF && defined CONFIG_COMMON_CLK 20062306a36Sopenharmony_ci/* 20162306a36Sopenharmony_ci * Clock handling code. 20262306a36Sopenharmony_ci * 20362306a36Sopenharmony_ci * Here we handle the clocks property of our "simple-framebuffer" dt node. 20462306a36Sopenharmony_ci * This is necessary so that we can make sure that any clocks needed by 20562306a36Sopenharmony_ci * the display engine that the bootloader set up for us (and for which it 20662306a36Sopenharmony_ci * provided a simplefb dt node), stay up, for the life of the simplefb 20762306a36Sopenharmony_ci * driver. 20862306a36Sopenharmony_ci * 20962306a36Sopenharmony_ci * When the driver unloads, we cleanly disable, and then release the clocks. 21062306a36Sopenharmony_ci * 21162306a36Sopenharmony_ci * We only complain about errors here, no action is taken as the most likely 21262306a36Sopenharmony_ci * error can only happen due to a mismatch between the bootloader which set 21362306a36Sopenharmony_ci * up simplefb, and the clock definitions in the device tree. Chances are 21462306a36Sopenharmony_ci * that there are no adverse effects, and if there are, a clean teardown of 21562306a36Sopenharmony_ci * the fb probe will not help us much either. So just complain and carry on, 21662306a36Sopenharmony_ci * and hope that the user actually gets a working fb at the end of things. 21762306a36Sopenharmony_ci */ 21862306a36Sopenharmony_cistatic int simplefb_clocks_get(struct simplefb_par *par, 21962306a36Sopenharmony_ci struct platform_device *pdev) 22062306a36Sopenharmony_ci{ 22162306a36Sopenharmony_ci struct device_node *np = pdev->dev.of_node; 22262306a36Sopenharmony_ci struct clk *clock; 22362306a36Sopenharmony_ci int i; 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci if (dev_get_platdata(&pdev->dev) || !np) 22662306a36Sopenharmony_ci return 0; 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci par->clk_count = of_clk_get_parent_count(np); 22962306a36Sopenharmony_ci if (!par->clk_count) 23062306a36Sopenharmony_ci return 0; 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL); 23362306a36Sopenharmony_ci if (!par->clks) 23462306a36Sopenharmony_ci return -ENOMEM; 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci for (i = 0; i < par->clk_count; i++) { 23762306a36Sopenharmony_ci clock = of_clk_get(np, i); 23862306a36Sopenharmony_ci if (IS_ERR(clock)) { 23962306a36Sopenharmony_ci if (PTR_ERR(clock) == -EPROBE_DEFER) { 24062306a36Sopenharmony_ci while (--i >= 0) { 24162306a36Sopenharmony_ci clk_put(par->clks[i]); 24262306a36Sopenharmony_ci } 24362306a36Sopenharmony_ci kfree(par->clks); 24462306a36Sopenharmony_ci return -EPROBE_DEFER; 24562306a36Sopenharmony_ci } 24662306a36Sopenharmony_ci dev_err(&pdev->dev, "%s: clock %d not found: %ld\n", 24762306a36Sopenharmony_ci __func__, i, PTR_ERR(clock)); 24862306a36Sopenharmony_ci continue; 24962306a36Sopenharmony_ci } 25062306a36Sopenharmony_ci par->clks[i] = clock; 25162306a36Sopenharmony_ci } 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci return 0; 25462306a36Sopenharmony_ci} 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_cistatic void simplefb_clocks_enable(struct simplefb_par *par, 25762306a36Sopenharmony_ci struct platform_device *pdev) 25862306a36Sopenharmony_ci{ 25962306a36Sopenharmony_ci int i, ret; 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci for (i = 0; i < par->clk_count; i++) { 26262306a36Sopenharmony_ci if (par->clks[i]) { 26362306a36Sopenharmony_ci ret = clk_prepare_enable(par->clks[i]); 26462306a36Sopenharmony_ci if (ret) { 26562306a36Sopenharmony_ci dev_err(&pdev->dev, 26662306a36Sopenharmony_ci "%s: failed to enable clock %d: %d\n", 26762306a36Sopenharmony_ci __func__, i, ret); 26862306a36Sopenharmony_ci clk_put(par->clks[i]); 26962306a36Sopenharmony_ci par->clks[i] = NULL; 27062306a36Sopenharmony_ci } 27162306a36Sopenharmony_ci } 27262306a36Sopenharmony_ci } 27362306a36Sopenharmony_ci par->clks_enabled = true; 27462306a36Sopenharmony_ci} 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_cistatic void simplefb_clocks_destroy(struct simplefb_par *par) 27762306a36Sopenharmony_ci{ 27862306a36Sopenharmony_ci int i; 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci if (!par->clks) 28162306a36Sopenharmony_ci return; 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ci for (i = 0; i < par->clk_count; i++) { 28462306a36Sopenharmony_ci if (par->clks[i]) { 28562306a36Sopenharmony_ci if (par->clks_enabled) 28662306a36Sopenharmony_ci clk_disable_unprepare(par->clks[i]); 28762306a36Sopenharmony_ci clk_put(par->clks[i]); 28862306a36Sopenharmony_ci } 28962306a36Sopenharmony_ci } 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci kfree(par->clks); 29262306a36Sopenharmony_ci} 29362306a36Sopenharmony_ci#else 29462306a36Sopenharmony_cistatic int simplefb_clocks_get(struct simplefb_par *par, 29562306a36Sopenharmony_ci struct platform_device *pdev) { return 0; } 29662306a36Sopenharmony_cistatic void simplefb_clocks_enable(struct simplefb_par *par, 29762306a36Sopenharmony_ci struct platform_device *pdev) { } 29862306a36Sopenharmony_cistatic void simplefb_clocks_destroy(struct simplefb_par *par) { } 29962306a36Sopenharmony_ci#endif 30062306a36Sopenharmony_ci 30162306a36Sopenharmony_ci#if defined CONFIG_OF && defined CONFIG_REGULATOR 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci#define SUPPLY_SUFFIX "-supply" 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci/* 30662306a36Sopenharmony_ci * Regulator handling code. 30762306a36Sopenharmony_ci * 30862306a36Sopenharmony_ci * Here we handle the num-supplies and vin*-supply properties of our 30962306a36Sopenharmony_ci * "simple-framebuffer" dt node. This is necessary so that we can make sure 31062306a36Sopenharmony_ci * that any regulators needed by the display hardware that the bootloader 31162306a36Sopenharmony_ci * set up for us (and for which it provided a simplefb dt node), stay up, 31262306a36Sopenharmony_ci * for the life of the simplefb driver. 31362306a36Sopenharmony_ci * 31462306a36Sopenharmony_ci * When the driver unloads, we cleanly disable, and then release the 31562306a36Sopenharmony_ci * regulators. 31662306a36Sopenharmony_ci * 31762306a36Sopenharmony_ci * We only complain about errors here, no action is taken as the most likely 31862306a36Sopenharmony_ci * error can only happen due to a mismatch between the bootloader which set 31962306a36Sopenharmony_ci * up simplefb, and the regulator definitions in the device tree. Chances are 32062306a36Sopenharmony_ci * that there are no adverse effects, and if there are, a clean teardown of 32162306a36Sopenharmony_ci * the fb probe will not help us much either. So just complain and carry on, 32262306a36Sopenharmony_ci * and hope that the user actually gets a working fb at the end of things. 32362306a36Sopenharmony_ci */ 32462306a36Sopenharmony_cistatic int simplefb_regulators_get(struct simplefb_par *par, 32562306a36Sopenharmony_ci struct platform_device *pdev) 32662306a36Sopenharmony_ci{ 32762306a36Sopenharmony_ci struct device_node *np = pdev->dev.of_node; 32862306a36Sopenharmony_ci struct property *prop; 32962306a36Sopenharmony_ci struct regulator *regulator; 33062306a36Sopenharmony_ci const char *p; 33162306a36Sopenharmony_ci int count = 0, i = 0; 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci if (dev_get_platdata(&pdev->dev) || !np) 33462306a36Sopenharmony_ci return 0; 33562306a36Sopenharmony_ci 33662306a36Sopenharmony_ci /* Count the number of regulator supplies */ 33762306a36Sopenharmony_ci for_each_property_of_node(np, prop) { 33862306a36Sopenharmony_ci p = strstr(prop->name, SUPPLY_SUFFIX); 33962306a36Sopenharmony_ci if (p && p != prop->name) 34062306a36Sopenharmony_ci count++; 34162306a36Sopenharmony_ci } 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci if (!count) 34462306a36Sopenharmony_ci return 0; 34562306a36Sopenharmony_ci 34662306a36Sopenharmony_ci par->regulators = devm_kcalloc(&pdev->dev, count, 34762306a36Sopenharmony_ci sizeof(struct regulator *), GFP_KERNEL); 34862306a36Sopenharmony_ci if (!par->regulators) 34962306a36Sopenharmony_ci return -ENOMEM; 35062306a36Sopenharmony_ci 35162306a36Sopenharmony_ci /* Get all the regulators */ 35262306a36Sopenharmony_ci for_each_property_of_node(np, prop) { 35362306a36Sopenharmony_ci char name[32]; /* 32 is max size of property name */ 35462306a36Sopenharmony_ci 35562306a36Sopenharmony_ci p = strstr(prop->name, SUPPLY_SUFFIX); 35662306a36Sopenharmony_ci if (!p || p == prop->name) 35762306a36Sopenharmony_ci continue; 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci strscpy(name, prop->name, 36062306a36Sopenharmony_ci strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1); 36162306a36Sopenharmony_ci regulator = devm_regulator_get_optional(&pdev->dev, name); 36262306a36Sopenharmony_ci if (IS_ERR(regulator)) { 36362306a36Sopenharmony_ci if (PTR_ERR(regulator) == -EPROBE_DEFER) 36462306a36Sopenharmony_ci return -EPROBE_DEFER; 36562306a36Sopenharmony_ci dev_err(&pdev->dev, "regulator %s not found: %ld\n", 36662306a36Sopenharmony_ci name, PTR_ERR(regulator)); 36762306a36Sopenharmony_ci continue; 36862306a36Sopenharmony_ci } 36962306a36Sopenharmony_ci par->regulators[i++] = regulator; 37062306a36Sopenharmony_ci } 37162306a36Sopenharmony_ci par->regulator_count = i; 37262306a36Sopenharmony_ci 37362306a36Sopenharmony_ci return 0; 37462306a36Sopenharmony_ci} 37562306a36Sopenharmony_ci 37662306a36Sopenharmony_cistatic void simplefb_regulators_enable(struct simplefb_par *par, 37762306a36Sopenharmony_ci struct platform_device *pdev) 37862306a36Sopenharmony_ci{ 37962306a36Sopenharmony_ci int i, ret; 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_ci /* Enable all the regulators */ 38262306a36Sopenharmony_ci for (i = 0; i < par->regulator_count; i++) { 38362306a36Sopenharmony_ci ret = regulator_enable(par->regulators[i]); 38462306a36Sopenharmony_ci if (ret) { 38562306a36Sopenharmony_ci dev_err(&pdev->dev, 38662306a36Sopenharmony_ci "failed to enable regulator %d: %d\n", 38762306a36Sopenharmony_ci i, ret); 38862306a36Sopenharmony_ci devm_regulator_put(par->regulators[i]); 38962306a36Sopenharmony_ci par->regulators[i] = NULL; 39062306a36Sopenharmony_ci } 39162306a36Sopenharmony_ci } 39262306a36Sopenharmony_ci par->regulators_enabled = true; 39362306a36Sopenharmony_ci} 39462306a36Sopenharmony_ci 39562306a36Sopenharmony_cistatic void simplefb_regulators_destroy(struct simplefb_par *par) 39662306a36Sopenharmony_ci{ 39762306a36Sopenharmony_ci int i; 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci if (!par->regulators || !par->regulators_enabled) 40062306a36Sopenharmony_ci return; 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci for (i = 0; i < par->regulator_count; i++) 40362306a36Sopenharmony_ci if (par->regulators[i]) 40462306a36Sopenharmony_ci regulator_disable(par->regulators[i]); 40562306a36Sopenharmony_ci} 40662306a36Sopenharmony_ci#else 40762306a36Sopenharmony_cistatic int simplefb_regulators_get(struct simplefb_par *par, 40862306a36Sopenharmony_ci struct platform_device *pdev) { return 0; } 40962306a36Sopenharmony_cistatic void simplefb_regulators_enable(struct simplefb_par *par, 41062306a36Sopenharmony_ci struct platform_device *pdev) { } 41162306a36Sopenharmony_cistatic void simplefb_regulators_destroy(struct simplefb_par *par) { } 41262306a36Sopenharmony_ci#endif 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_cistatic int simplefb_probe(struct platform_device *pdev) 41562306a36Sopenharmony_ci{ 41662306a36Sopenharmony_ci int ret; 41762306a36Sopenharmony_ci struct simplefb_params params; 41862306a36Sopenharmony_ci struct fb_info *info; 41962306a36Sopenharmony_ci struct simplefb_par *par; 42062306a36Sopenharmony_ci struct resource *res, *mem; 42162306a36Sopenharmony_ci 42262306a36Sopenharmony_ci if (fb_get_options("simplefb", NULL)) 42362306a36Sopenharmony_ci return -ENODEV; 42462306a36Sopenharmony_ci 42562306a36Sopenharmony_ci ret = -ENODEV; 42662306a36Sopenharmony_ci if (dev_get_platdata(&pdev->dev)) 42762306a36Sopenharmony_ci ret = simplefb_parse_pd(pdev, ¶ms); 42862306a36Sopenharmony_ci else if (pdev->dev.of_node) 42962306a36Sopenharmony_ci ret = simplefb_parse_dt(pdev, ¶ms); 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_ci if (ret) 43262306a36Sopenharmony_ci return ret; 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 43562306a36Sopenharmony_ci if (!res) { 43662306a36Sopenharmony_ci dev_err(&pdev->dev, "No memory resource\n"); 43762306a36Sopenharmony_ci return -EINVAL; 43862306a36Sopenharmony_ci } 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_ci mem = request_mem_region(res->start, resource_size(res), "simplefb"); 44162306a36Sopenharmony_ci if (!mem) { 44262306a36Sopenharmony_ci /* 44362306a36Sopenharmony_ci * We cannot make this fatal. Sometimes this comes from magic 44462306a36Sopenharmony_ci * spaces our resource handlers simply don't know about. Use 44562306a36Sopenharmony_ci * the I/O-memory resource as-is and try to map that instead. 44662306a36Sopenharmony_ci */ 44762306a36Sopenharmony_ci dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res); 44862306a36Sopenharmony_ci mem = res; 44962306a36Sopenharmony_ci } 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev); 45262306a36Sopenharmony_ci if (!info) { 45362306a36Sopenharmony_ci ret = -ENOMEM; 45462306a36Sopenharmony_ci goto error_release_mem_region; 45562306a36Sopenharmony_ci } 45662306a36Sopenharmony_ci platform_set_drvdata(pdev, info); 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci par = info->par; 45962306a36Sopenharmony_ci 46062306a36Sopenharmony_ci info->fix = simplefb_fix; 46162306a36Sopenharmony_ci info->fix.smem_start = mem->start; 46262306a36Sopenharmony_ci info->fix.smem_len = resource_size(mem); 46362306a36Sopenharmony_ci info->fix.line_length = params.stride; 46462306a36Sopenharmony_ci 46562306a36Sopenharmony_ci info->var = simplefb_var; 46662306a36Sopenharmony_ci info->var.xres = params.width; 46762306a36Sopenharmony_ci info->var.yres = params.height; 46862306a36Sopenharmony_ci info->var.xres_virtual = params.width; 46962306a36Sopenharmony_ci info->var.yres_virtual = params.height; 47062306a36Sopenharmony_ci info->var.bits_per_pixel = params.format->bits_per_pixel; 47162306a36Sopenharmony_ci info->var.red = params.format->red; 47262306a36Sopenharmony_ci info->var.green = params.format->green; 47362306a36Sopenharmony_ci info->var.blue = params.format->blue; 47462306a36Sopenharmony_ci info->var.transp = params.format->transp; 47562306a36Sopenharmony_ci 47662306a36Sopenharmony_ci par->base = info->fix.smem_start; 47762306a36Sopenharmony_ci par->size = info->fix.smem_len; 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_ci info->fbops = &simplefb_ops; 48062306a36Sopenharmony_ci info->screen_base = ioremap_wc(info->fix.smem_start, 48162306a36Sopenharmony_ci info->fix.smem_len); 48262306a36Sopenharmony_ci if (!info->screen_base) { 48362306a36Sopenharmony_ci ret = -ENOMEM; 48462306a36Sopenharmony_ci goto error_fb_release; 48562306a36Sopenharmony_ci } 48662306a36Sopenharmony_ci info->pseudo_palette = par->palette; 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_ci ret = simplefb_clocks_get(par, pdev); 48962306a36Sopenharmony_ci if (ret < 0) 49062306a36Sopenharmony_ci goto error_unmap; 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ci ret = simplefb_regulators_get(par, pdev); 49362306a36Sopenharmony_ci if (ret < 0) 49462306a36Sopenharmony_ci goto error_clocks; 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_ci simplefb_clocks_enable(par, pdev); 49762306a36Sopenharmony_ci simplefb_regulators_enable(par, pdev); 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n", 50062306a36Sopenharmony_ci info->fix.smem_start, info->fix.smem_len); 50162306a36Sopenharmony_ci dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n", 50262306a36Sopenharmony_ci params.format->name, 50362306a36Sopenharmony_ci info->var.xres, info->var.yres, 50462306a36Sopenharmony_ci info->var.bits_per_pixel, info->fix.line_length); 50562306a36Sopenharmony_ci 50662306a36Sopenharmony_ci if (mem != res) 50762306a36Sopenharmony_ci par->mem = mem; /* release in clean-up handler */ 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci ret = devm_aperture_acquire_for_platform_device(pdev, par->base, par->size); 51062306a36Sopenharmony_ci if (ret) { 51162306a36Sopenharmony_ci dev_err(&pdev->dev, "Unable to acquire aperture: %d\n", ret); 51262306a36Sopenharmony_ci goto error_regulators; 51362306a36Sopenharmony_ci } 51462306a36Sopenharmony_ci ret = register_framebuffer(info); 51562306a36Sopenharmony_ci if (ret < 0) { 51662306a36Sopenharmony_ci dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret); 51762306a36Sopenharmony_ci goto error_regulators; 51862306a36Sopenharmony_ci } 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node); 52162306a36Sopenharmony_ci 52262306a36Sopenharmony_ci return 0; 52362306a36Sopenharmony_ci 52462306a36Sopenharmony_cierror_regulators: 52562306a36Sopenharmony_ci simplefb_regulators_destroy(par); 52662306a36Sopenharmony_cierror_clocks: 52762306a36Sopenharmony_ci simplefb_clocks_destroy(par); 52862306a36Sopenharmony_cierror_unmap: 52962306a36Sopenharmony_ci iounmap(info->screen_base); 53062306a36Sopenharmony_cierror_fb_release: 53162306a36Sopenharmony_ci framebuffer_release(info); 53262306a36Sopenharmony_cierror_release_mem_region: 53362306a36Sopenharmony_ci if (mem != res) 53462306a36Sopenharmony_ci release_mem_region(mem->start, resource_size(mem)); 53562306a36Sopenharmony_ci return ret; 53662306a36Sopenharmony_ci} 53762306a36Sopenharmony_ci 53862306a36Sopenharmony_cistatic void simplefb_remove(struct platform_device *pdev) 53962306a36Sopenharmony_ci{ 54062306a36Sopenharmony_ci struct fb_info *info = platform_get_drvdata(pdev); 54162306a36Sopenharmony_ci 54262306a36Sopenharmony_ci /* simplefb_destroy takes care of info cleanup */ 54362306a36Sopenharmony_ci unregister_framebuffer(info); 54462306a36Sopenharmony_ci} 54562306a36Sopenharmony_ci 54662306a36Sopenharmony_cistatic const struct of_device_id simplefb_of_match[] = { 54762306a36Sopenharmony_ci { .compatible = "simple-framebuffer", }, 54862306a36Sopenharmony_ci { }, 54962306a36Sopenharmony_ci}; 55062306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, simplefb_of_match); 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_cistatic struct platform_driver simplefb_driver = { 55362306a36Sopenharmony_ci .driver = { 55462306a36Sopenharmony_ci .name = "simple-framebuffer", 55562306a36Sopenharmony_ci .of_match_table = simplefb_of_match, 55662306a36Sopenharmony_ci }, 55762306a36Sopenharmony_ci .probe = simplefb_probe, 55862306a36Sopenharmony_ci .remove_new = simplefb_remove, 55962306a36Sopenharmony_ci}; 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_cimodule_platform_driver(simplefb_driver); 56262306a36Sopenharmony_ci 56362306a36Sopenharmony_ciMODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>"); 56462306a36Sopenharmony_ciMODULE_DESCRIPTION("Simple framebuffer driver"); 56562306a36Sopenharmony_ciMODULE_LICENSE("GPL v2"); 566