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/errno.h> 17 #include <linux/fb.h> 18 #include <linux/io.h> 19 #include <linux/module.h> 20 #include <linux/platform_data/simplefb.h> 21 #include <linux/platform_device.h> 22 #include <linux/clk.h> 23 #include <linux/of.h> 24 #include <linux/of_clk.h> 25 #include <linux/of_platform.h> 26 #include <linux/parser.h> 27 #include <linux/regulator/consumer.h> 28 29 static const struct fb_fix_screeninfo simplefb_fix = { 30 .id = "simple", 31 .type = FB_TYPE_PACKED_PIXELS, 32 .visual = FB_VISUAL_TRUECOLOR, 33 .accel = FB_ACCEL_NONE, 34 }; 35 36 static const struct fb_var_screeninfo simplefb_var = { 37 .height = -1, 38 .width = -1, 39 .activate = FB_ACTIVATE_NOW, 40 .vmode = FB_VMODE_NONINTERLACED, 41 }; 42 43 #define PSEUDO_PALETTE_SIZE 16 44 45 static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue, 46 u_int transp, struct fb_info *info) 47 { 48 u32 *pal = info->pseudo_palette; 49 u32 cr = red >> (16 - info->var.red.length); 50 u32 cg = green >> (16 - info->var.green.length); 51 u32 cb = blue >> (16 - info->var.blue.length); 52 u32 value; 53 54 if (regno >= PSEUDO_PALETTE_SIZE) 55 return -EINVAL; 56 57 value = (cr << info->var.red.offset) | 58 (cg << info->var.green.offset) | 59 (cb << info->var.blue.offset); 60 if (info->var.transp.length > 0) { 61 u32 mask = (1 << info->var.transp.length) - 1; 62 mask <<= info->var.transp.offset; 63 value |= mask; 64 } 65 pal[regno] = value; 66 67 return 0; 68 } 69 70 struct simplefb_par { 71 u32 palette[PSEUDO_PALETTE_SIZE]; 72 resource_size_t base; 73 resource_size_t size; 74 struct resource *mem; 75 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK 76 bool clks_enabled; 77 unsigned int clk_count; 78 struct clk **clks; 79 #endif 80 #if defined CONFIG_OF && defined CONFIG_REGULATOR 81 bool regulators_enabled; 82 u32 regulator_count; 83 struct regulator **regulators; 84 #endif 85 }; 86 87 static void simplefb_clocks_destroy(struct simplefb_par *par); 88 static void simplefb_regulators_destroy(struct simplefb_par *par); 89 90 /* 91 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end 92 * of unregister_framebuffer() or fb_release(). Do any cleanup here. 93 */ 94 static void simplefb_destroy(struct fb_info *info) 95 { 96 struct simplefb_par *par = info->par; 97 struct resource *mem = par->mem; 98 99 simplefb_regulators_destroy(info->par); 100 simplefb_clocks_destroy(info->par); 101 if (info->screen_base) 102 iounmap(info->screen_base); 103 104 framebuffer_release(info); 105 106 if (mem) 107 release_mem_region(mem->start, resource_size(mem)); 108 } 109 110 static const struct fb_ops simplefb_ops = { 111 .owner = THIS_MODULE, 112 .fb_destroy = simplefb_destroy, 113 .fb_setcolreg = simplefb_setcolreg, 114 .fb_fillrect = cfb_fillrect, 115 .fb_copyarea = cfb_copyarea, 116 .fb_imageblit = cfb_imageblit, 117 }; 118 119 static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS; 120 121 struct simplefb_params { 122 u32 width; 123 u32 height; 124 u32 stride; 125 struct simplefb_format *format; 126 }; 127 128 static int simplefb_parse_dt(struct platform_device *pdev, 129 struct simplefb_params *params) 130 { 131 struct device_node *np = pdev->dev.of_node; 132 int ret; 133 const char *format; 134 int i; 135 136 ret = of_property_read_u32(np, "width", ¶ms->width); 137 if (ret) { 138 dev_err(&pdev->dev, "Can't parse width property\n"); 139 return ret; 140 } 141 142 ret = of_property_read_u32(np, "height", ¶ms->height); 143 if (ret) { 144 dev_err(&pdev->dev, "Can't parse height property\n"); 145 return ret; 146 } 147 148 ret = of_property_read_u32(np, "stride", ¶ms->stride); 149 if (ret) { 150 dev_err(&pdev->dev, "Can't parse stride property\n"); 151 return ret; 152 } 153 154 ret = of_property_read_string(np, "format", &format); 155 if (ret) { 156 dev_err(&pdev->dev, "Can't parse format property\n"); 157 return ret; 158 } 159 params->format = NULL; 160 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) { 161 if (strcmp(format, simplefb_formats[i].name)) 162 continue; 163 params->format = &simplefb_formats[i]; 164 break; 165 } 166 if (!params->format) { 167 dev_err(&pdev->dev, "Invalid format value\n"); 168 return -EINVAL; 169 } 170 171 return 0; 172 } 173 174 static int simplefb_parse_pd(struct platform_device *pdev, 175 struct simplefb_params *params) 176 { 177 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev); 178 int i; 179 180 params->width = pd->width; 181 params->height = pd->height; 182 params->stride = pd->stride; 183 184 params->format = NULL; 185 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) { 186 if (strcmp(pd->format, simplefb_formats[i].name)) 187 continue; 188 189 params->format = &simplefb_formats[i]; 190 break; 191 } 192 193 if (!params->format) { 194 dev_err(&pdev->dev, "Invalid format value\n"); 195 return -EINVAL; 196 } 197 198 return 0; 199 } 200 201 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK 202 /* 203 * Clock handling code. 204 * 205 * Here we handle the clocks property of our "simple-framebuffer" dt node. 206 * This is necessary so that we can make sure that any clocks needed by 207 * the display engine that the bootloader set up for us (and for which it 208 * provided a simplefb dt node), stay up, for the life of the simplefb 209 * driver. 210 * 211 * When the driver unloads, we cleanly disable, and then release the clocks. 212 * 213 * We only complain about errors here, no action is taken as the most likely 214 * error can only happen due to a mismatch between the bootloader which set 215 * up simplefb, and the clock definitions in the device tree. Chances are 216 * that there are no adverse effects, and if there are, a clean teardown of 217 * the fb probe will not help us much either. So just complain and carry on, 218 * and hope that the user actually gets a working fb at the end of things. 219 */ 220 static int simplefb_clocks_get(struct simplefb_par *par, 221 struct platform_device *pdev) 222 { 223 struct device_node *np = pdev->dev.of_node; 224 struct clk *clock; 225 int i; 226 227 if (dev_get_platdata(&pdev->dev) || !np) 228 return 0; 229 230 par->clk_count = of_clk_get_parent_count(np); 231 if (!par->clk_count) 232 return 0; 233 234 par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL); 235 if (!par->clks) 236 return -ENOMEM; 237 238 for (i = 0; i < par->clk_count; i++) { 239 clock = of_clk_get(np, i); 240 if (IS_ERR(clock)) { 241 if (PTR_ERR(clock) == -EPROBE_DEFER) { 242 while (--i >= 0) { 243 clk_put(par->clks[i]); 244 } 245 kfree(par->clks); 246 return -EPROBE_DEFER; 247 } 248 dev_err(&pdev->dev, "%s: clock %d not found: %ld\n", 249 __func__, i, PTR_ERR(clock)); 250 continue; 251 } 252 par->clks[i] = clock; 253 } 254 255 return 0; 256 } 257 258 static void simplefb_clocks_enable(struct simplefb_par *par, 259 struct platform_device *pdev) 260 { 261 int i, ret; 262 263 for (i = 0; i < par->clk_count; i++) { 264 if (par->clks[i]) { 265 ret = clk_prepare_enable(par->clks[i]); 266 if (ret) { 267 dev_err(&pdev->dev, 268 "%s: failed to enable clock %d: %d\n", 269 __func__, i, ret); 270 clk_put(par->clks[i]); 271 par->clks[i] = NULL; 272 } 273 } 274 } 275 par->clks_enabled = true; 276 } 277 278 static void simplefb_clocks_destroy(struct simplefb_par *par) 279 { 280 int i; 281 282 if (!par->clks) 283 return; 284 285 for (i = 0; i < par->clk_count; i++) { 286 if (par->clks[i]) { 287 if (par->clks_enabled) 288 clk_disable_unprepare(par->clks[i]); 289 clk_put(par->clks[i]); 290 } 291 } 292 293 kfree(par->clks); 294 } 295 #else 296 static int simplefb_clocks_get(struct simplefb_par *par, 297 struct platform_device *pdev) { return 0; } 298 static void simplefb_clocks_enable(struct simplefb_par *par, 299 struct platform_device *pdev) { } 300 static void simplefb_clocks_destroy(struct simplefb_par *par) { } 301 #endif 302 303 #if defined CONFIG_OF && defined CONFIG_REGULATOR 304 305 #define SUPPLY_SUFFIX "-supply" 306 307 /* 308 * Regulator handling code. 309 * 310 * Here we handle the num-supplies and vin*-supply properties of our 311 * "simple-framebuffer" dt node. This is necessary so that we can make sure 312 * that any regulators needed by the display hardware that the bootloader 313 * set up for us (and for which it provided a simplefb dt node), stay up, 314 * for the life of the simplefb driver. 315 * 316 * When the driver unloads, we cleanly disable, and then release the 317 * regulators. 318 * 319 * We only complain about errors here, no action is taken as the most likely 320 * error can only happen due to a mismatch between the bootloader which set 321 * up simplefb, and the regulator definitions in the device tree. Chances are 322 * that there are no adverse effects, and if there are, a clean teardown of 323 * the fb probe will not help us much either. So just complain and carry on, 324 * and hope that the user actually gets a working fb at the end of things. 325 */ 326 static int simplefb_regulators_get(struct simplefb_par *par, 327 struct platform_device *pdev) 328 { 329 struct device_node *np = pdev->dev.of_node; 330 struct property *prop; 331 struct regulator *regulator; 332 const char *p; 333 int count = 0, i = 0; 334 335 if (dev_get_platdata(&pdev->dev) || !np) 336 return 0; 337 338 /* Count the number of regulator supplies */ 339 for_each_property_of_node(np, prop) { 340 p = strstr(prop->name, SUPPLY_SUFFIX); 341 if (p && p != prop->name) 342 count++; 343 } 344 345 if (!count) 346 return 0; 347 348 par->regulators = devm_kcalloc(&pdev->dev, count, 349 sizeof(struct regulator *), GFP_KERNEL); 350 if (!par->regulators) 351 return -ENOMEM; 352 353 /* Get all the regulators */ 354 for_each_property_of_node(np, prop) { 355 char name[32]; /* 32 is max size of property name */ 356 357 p = strstr(prop->name, SUPPLY_SUFFIX); 358 if (!p || p == prop->name) 359 continue; 360 361 strscpy(name, prop->name, 362 strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1); 363 regulator = devm_regulator_get_optional(&pdev->dev, name); 364 if (IS_ERR(regulator)) { 365 if (PTR_ERR(regulator) == -EPROBE_DEFER) 366 return -EPROBE_DEFER; 367 dev_err(&pdev->dev, "regulator %s not found: %ld\n", 368 name, PTR_ERR(regulator)); 369 continue; 370 } 371 par->regulators[i++] = regulator; 372 } 373 par->regulator_count = i; 374 375 return 0; 376 } 377 378 static void simplefb_regulators_enable(struct simplefb_par *par, 379 struct platform_device *pdev) 380 { 381 int i, ret; 382 383 /* Enable all the regulators */ 384 for (i = 0; i < par->regulator_count; i++) { 385 ret = regulator_enable(par->regulators[i]); 386 if (ret) { 387 dev_err(&pdev->dev, 388 "failed to enable regulator %d: %d\n", 389 i, ret); 390 devm_regulator_put(par->regulators[i]); 391 par->regulators[i] = NULL; 392 } 393 } 394 par->regulators_enabled = true; 395 } 396 397 static void simplefb_regulators_destroy(struct simplefb_par *par) 398 { 399 int i; 400 401 if (!par->regulators || !par->regulators_enabled) 402 return; 403 404 for (i = 0; i < par->regulator_count; i++) 405 if (par->regulators[i]) 406 regulator_disable(par->regulators[i]); 407 } 408 #else 409 static int simplefb_regulators_get(struct simplefb_par *par, 410 struct platform_device *pdev) { return 0; } 411 static void simplefb_regulators_enable(struct simplefb_par *par, 412 struct platform_device *pdev) { } 413 static void simplefb_regulators_destroy(struct simplefb_par *par) { } 414 #endif 415 416 static int simplefb_probe(struct platform_device *pdev) 417 { 418 int ret; 419 struct simplefb_params params; 420 struct fb_info *info; 421 struct simplefb_par *par; 422 struct resource *res, *mem; 423 424 if (fb_get_options("simplefb", NULL)) 425 return -ENODEV; 426 427 ret = -ENODEV; 428 if (dev_get_platdata(&pdev->dev)) 429 ret = simplefb_parse_pd(pdev, ¶ms); 430 else if (pdev->dev.of_node) 431 ret = simplefb_parse_dt(pdev, ¶ms); 432 433 if (ret) 434 return ret; 435 436 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 437 if (!res) { 438 dev_err(&pdev->dev, "No memory resource\n"); 439 return -EINVAL; 440 } 441 442 mem = request_mem_region(res->start, resource_size(res), "simplefb"); 443 if (!mem) { 444 /* 445 * We cannot make this fatal. Sometimes this comes from magic 446 * spaces our resource handlers simply don't know about. Use 447 * the I/O-memory resource as-is and try to map that instead. 448 */ 449 dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n", res); 450 mem = res; 451 } 452 453 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev); 454 if (!info) { 455 ret = -ENOMEM; 456 goto error_release_mem_region; 457 } 458 platform_set_drvdata(pdev, info); 459 460 par = info->par; 461 462 info->fix = simplefb_fix; 463 info->fix.smem_start = mem->start; 464 info->fix.smem_len = resource_size(mem); 465 info->fix.line_length = params.stride; 466 467 info->var = simplefb_var; 468 info->var.xres = params.width; 469 info->var.yres = params.height; 470 info->var.xres_virtual = params.width; 471 info->var.yres_virtual = params.height; 472 info->var.bits_per_pixel = params.format->bits_per_pixel; 473 info->var.red = params.format->red; 474 info->var.green = params.format->green; 475 info->var.blue = params.format->blue; 476 info->var.transp = params.format->transp; 477 478 par->base = info->fix.smem_start; 479 par->size = info->fix.smem_len; 480 481 info->fbops = &simplefb_ops; 482 info->flags = FBINFO_DEFAULT; 483 info->screen_base = ioremap_wc(info->fix.smem_start, 484 info->fix.smem_len); 485 if (!info->screen_base) { 486 ret = -ENOMEM; 487 goto error_fb_release; 488 } 489 info->pseudo_palette = par->palette; 490 491 ret = simplefb_clocks_get(par, pdev); 492 if (ret < 0) 493 goto error_unmap; 494 495 ret = simplefb_regulators_get(par, pdev); 496 if (ret < 0) 497 goto error_clocks; 498 499 simplefb_clocks_enable(par, pdev); 500 simplefb_regulators_enable(par, pdev); 501 502 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes\n", 503 info->fix.smem_start, info->fix.smem_len); 504 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n", 505 params.format->name, 506 info->var.xres, info->var.yres, 507 info->var.bits_per_pixel, info->fix.line_length); 508 509 if (mem != res) 510 par->mem = mem; /* release in clean-up handler */ 511 512 ret = devm_aperture_acquire_for_platform_device(pdev, par->base, par->size); 513 if (ret) { 514 dev_err(&pdev->dev, "Unable to acquire aperture: %d\n", ret); 515 goto error_regulators; 516 } 517 ret = register_framebuffer(info); 518 if (ret < 0) { 519 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret); 520 goto error_regulators; 521 } 522 523 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node); 524 525 return 0; 526 527 error_regulators: 528 simplefb_regulators_destroy(par); 529 error_clocks: 530 simplefb_clocks_destroy(par); 531 error_unmap: 532 iounmap(info->screen_base); 533 error_fb_release: 534 framebuffer_release(info); 535 error_release_mem_region: 536 if (mem != res) 537 release_mem_region(mem->start, resource_size(mem)); 538 return ret; 539 } 540 541 static void simplefb_remove(struct platform_device *pdev) 542 { 543 struct fb_info *info = platform_get_drvdata(pdev); 544 545 /* simplefb_destroy takes care of info cleanup */ 546 unregister_framebuffer(info); 547 } 548 549 static const struct of_device_id simplefb_of_match[] = { 550 { .compatible = "simple-framebuffer", }, 551 { }, 552 }; 553 MODULE_DEVICE_TABLE(of, simplefb_of_match); 554 555 static struct platform_driver simplefb_driver = { 556 .driver = { 557 .name = "simple-framebuffer", 558 .of_match_table = simplefb_of_match, 559 }, 560 .probe = simplefb_probe, 561 .remove_new = simplefb_remove, 562 }; 563 564 module_platform_driver(simplefb_driver); 565 566 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>"); 567 MODULE_DESCRIPTION("Simple framebuffer driver"); 568 MODULE_LICENSE("GPL v2"); 569