simplefb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Simplest possible simple frame-buffer driver, as a platform device
  3. *
  4. * Copyright (c) 2013, Stephen Warren
  5. *
  6. * Based on q40fb.c, which was:
  7. * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org>
  8. *
  9. * Also based on offb.c, which was:
  10. * Copyright (C) 1997 Geert Uytterhoeven
  11. * Copyright (C) 1996 Paul Mackerras
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms and conditions of the GNU General Public License,
  15. * version 2, as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. * more details.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/fb.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_data/simplefb.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/clk.h>
  29. #include <linux/of.h>
  30. #include <linux/of_clk.h>
  31. #include <linux/of_platform.h>
  32. #include <linux/parser.h>
  33. #include <linux/regulator/consumer.h>
  34. static const struct fb_fix_screeninfo simplefb_fix = {
  35. .id = "simple",
  36. .type = FB_TYPE_PACKED_PIXELS,
  37. .visual = FB_VISUAL_TRUECOLOR,
  38. .accel = FB_ACCEL_NONE,
  39. };
  40. static const struct fb_var_screeninfo simplefb_var = {
  41. .height = -1,
  42. .width = -1,
  43. .activate = FB_ACTIVATE_NOW,
  44. .vmode = FB_VMODE_NONINTERLACED,
  45. };
  46. #define PSEUDO_PALETTE_SIZE 16
  47. static 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. if (regno >= PSEUDO_PALETTE_SIZE)
  56. return -EINVAL;
  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. return 0;
  67. }
  68. struct simplefb_par;
  69. static void simplefb_clocks_destroy(struct simplefb_par *par);
  70. static void simplefb_regulators_destroy(struct simplefb_par *par);
  71. static void simplefb_destroy(struct fb_info *info)
  72. {
  73. simplefb_regulators_destroy(info->par);
  74. simplefb_clocks_destroy(info->par);
  75. if (info->screen_base)
  76. iounmap(info->screen_base);
  77. }
  78. static struct fb_ops simplefb_ops = {
  79. .owner = THIS_MODULE,
  80. .fb_destroy = simplefb_destroy,
  81. .fb_setcolreg = simplefb_setcolreg,
  82. .fb_fillrect = cfb_fillrect,
  83. .fb_copyarea = cfb_copyarea,
  84. .fb_imageblit = cfb_imageblit,
  85. };
  86. static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
  87. struct simplefb_params {
  88. u32 width;
  89. u32 height;
  90. u32 stride;
  91. struct simplefb_format *format;
  92. };
  93. static int simplefb_parse_dt(struct platform_device *pdev,
  94. struct simplefb_params *params)
  95. {
  96. struct device_node *np = pdev->dev.of_node;
  97. int ret;
  98. const char *format;
  99. int i;
  100. ret = of_property_read_u32(np, "width", &params->width);
  101. if (ret) {
  102. dev_err(&pdev->dev, "Can't parse width property\n");
  103. return ret;
  104. }
  105. ret = of_property_read_u32(np, "height", &params->height);
  106. if (ret) {
  107. dev_err(&pdev->dev, "Can't parse height property\n");
  108. return ret;
  109. }
  110. ret = of_property_read_u32(np, "stride", &params->stride);
  111. if (ret) {
  112. dev_err(&pdev->dev, "Can't parse stride property\n");
  113. return ret;
  114. }
  115. ret = of_property_read_string(np, "format", &format);
  116. if (ret) {
  117. dev_err(&pdev->dev, "Can't parse format property\n");
  118. return ret;
  119. }
  120. params->format = NULL;
  121. for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
  122. if (strcmp(format, simplefb_formats[i].name))
  123. continue;
  124. params->format = &simplefb_formats[i];
  125. break;
  126. }
  127. if (!params->format) {
  128. dev_err(&pdev->dev, "Invalid format value\n");
  129. return -EINVAL;
  130. }
  131. return 0;
  132. }
  133. static int simplefb_parse_pd(struct platform_device *pdev,
  134. struct simplefb_params *params)
  135. {
  136. struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
  137. int i;
  138. params->width = pd->width;
  139. params->height = pd->height;
  140. params->stride = pd->stride;
  141. params->format = NULL;
  142. for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
  143. if (strcmp(pd->format, simplefb_formats[i].name))
  144. continue;
  145. params->format = &simplefb_formats[i];
  146. break;
  147. }
  148. if (!params->format) {
  149. dev_err(&pdev->dev, "Invalid format value\n");
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. struct simplefb_par {
  155. u32 palette[PSEUDO_PALETTE_SIZE];
  156. #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
  157. bool clks_enabled;
  158. unsigned int clk_count;
  159. struct clk **clks;
  160. #endif
  161. #if defined CONFIG_OF && defined CONFIG_REGULATOR
  162. bool regulators_enabled;
  163. u32 regulator_count;
  164. struct regulator **regulators;
  165. #endif
  166. };
  167. #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
  168. /*
  169. * Clock handling code.
  170. *
  171. * Here we handle the clocks property of our "simple-framebuffer" dt node.
  172. * This is necessary so that we can make sure that any clocks needed by
  173. * the display engine that the bootloader set up for us (and for which it
  174. * provided a simplefb dt node), stay up, for the life of the simplefb
  175. * driver.
  176. *
  177. * When the driver unloads, we cleanly disable, and then release the clocks.
  178. *
  179. * We only complain about errors here, no action is taken as the most likely
  180. * error can only happen due to a mismatch between the bootloader which set
  181. * up simplefb, and the clock definitions in the device tree. Chances are
  182. * that there are no adverse effects, and if there are, a clean teardown of
  183. * the fb probe will not help us much either. So just complain and carry on,
  184. * and hope that the user actually gets a working fb at the end of things.
  185. */
  186. static int simplefb_clocks_get(struct simplefb_par *par,
  187. struct platform_device *pdev)
  188. {
  189. struct device_node *np = pdev->dev.of_node;
  190. struct clk *clock;
  191. int i;
  192. if (dev_get_platdata(&pdev->dev) || !np)
  193. return 0;
  194. par->clk_count = of_clk_get_parent_count(np);
  195. if (!par->clk_count)
  196. return 0;
  197. par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
  198. if (!par->clks)
  199. return -ENOMEM;
  200. for (i = 0; i < par->clk_count; i++) {
  201. clock = of_clk_get(np, i);
  202. if (IS_ERR(clock)) {
  203. if (PTR_ERR(clock) == -EPROBE_DEFER) {
  204. while (--i >= 0) {
  205. if (par->clks[i])
  206. clk_put(par->clks[i]);
  207. }
  208. kfree(par->clks);
  209. return -EPROBE_DEFER;
  210. }
  211. dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
  212. __func__, i, PTR_ERR(clock));
  213. continue;
  214. }
  215. par->clks[i] = clock;
  216. }
  217. return 0;
  218. }
  219. static void simplefb_clocks_enable(struct simplefb_par *par,
  220. struct platform_device *pdev)
  221. {
  222. int i, ret;
  223. for (i = 0; i < par->clk_count; i++) {
  224. if (par->clks[i]) {
  225. ret = clk_prepare_enable(par->clks[i]);
  226. if (ret) {
  227. dev_err(&pdev->dev,
  228. "%s: failed to enable clock %d: %d\n",
  229. __func__, i, ret);
  230. clk_put(par->clks[i]);
  231. par->clks[i] = NULL;
  232. }
  233. }
  234. }
  235. par->clks_enabled = true;
  236. }
  237. static void simplefb_clocks_destroy(struct simplefb_par *par)
  238. {
  239. int i;
  240. if (!par->clks)
  241. return;
  242. for (i = 0; i < par->clk_count; i++) {
  243. if (par->clks[i]) {
  244. if (par->clks_enabled)
  245. clk_disable_unprepare(par->clks[i]);
  246. clk_put(par->clks[i]);
  247. }
  248. }
  249. kfree(par->clks);
  250. }
  251. #else
  252. static int simplefb_clocks_get(struct simplefb_par *par,
  253. struct platform_device *pdev) { return 0; }
  254. static void simplefb_clocks_enable(struct simplefb_par *par,
  255. struct platform_device *pdev) { }
  256. static void simplefb_clocks_destroy(struct simplefb_par *par) { }
  257. #endif
  258. #if defined CONFIG_OF && defined CONFIG_REGULATOR
  259. #define SUPPLY_SUFFIX "-supply"
  260. /*
  261. * Regulator handling code.
  262. *
  263. * Here we handle the num-supplies and vin*-supply properties of our
  264. * "simple-framebuffer" dt node. This is necessary so that we can make sure
  265. * that any regulators needed by the display hardware that the bootloader
  266. * set up for us (and for which it provided a simplefb dt node), stay up,
  267. * for the life of the simplefb driver.
  268. *
  269. * When the driver unloads, we cleanly disable, and then release the
  270. * regulators.
  271. *
  272. * We only complain about errors here, no action is taken as the most likely
  273. * error can only happen due to a mismatch between the bootloader which set
  274. * up simplefb, and the regulator definitions in the device tree. Chances are
  275. * that there are no adverse effects, and if there are, a clean teardown of
  276. * the fb probe will not help us much either. So just complain and carry on,
  277. * and hope that the user actually gets a working fb at the end of things.
  278. */
  279. static int simplefb_regulators_get(struct simplefb_par *par,
  280. struct platform_device *pdev)
  281. {
  282. struct device_node *np = pdev->dev.of_node;
  283. struct property *prop;
  284. struct regulator *regulator;
  285. const char *p;
  286. int count = 0, i = 0;
  287. if (dev_get_platdata(&pdev->dev) || !np)
  288. return 0;
  289. /* Count the number of regulator supplies */
  290. for_each_property_of_node(np, prop) {
  291. p = strstr(prop->name, SUPPLY_SUFFIX);
  292. if (p && p != prop->name)
  293. count++;
  294. }
  295. if (!count)
  296. return 0;
  297. par->regulators = devm_kcalloc(&pdev->dev, count,
  298. sizeof(struct regulator *), GFP_KERNEL);
  299. if (!par->regulators)
  300. return -ENOMEM;
  301. /* Get all the regulators */
  302. for_each_property_of_node(np, prop) {
  303. char name[32]; /* 32 is max size of property name */
  304. p = strstr(prop->name, SUPPLY_SUFFIX);
  305. if (!p || p == prop->name)
  306. continue;
  307. strlcpy(name, prop->name,
  308. strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
  309. regulator = devm_regulator_get_optional(&pdev->dev, name);
  310. if (IS_ERR(regulator)) {
  311. if (PTR_ERR(regulator) == -EPROBE_DEFER)
  312. return -EPROBE_DEFER;
  313. dev_err(&pdev->dev, "regulator %s not found: %ld\n",
  314. name, PTR_ERR(regulator));
  315. continue;
  316. }
  317. par->regulators[i++] = regulator;
  318. }
  319. par->regulator_count = i;
  320. return 0;
  321. }
  322. static void simplefb_regulators_enable(struct simplefb_par *par,
  323. struct platform_device *pdev)
  324. {
  325. int i, ret;
  326. /* Enable all the regulators */
  327. for (i = 0; i < par->regulator_count; i++) {
  328. ret = regulator_enable(par->regulators[i]);
  329. if (ret) {
  330. dev_err(&pdev->dev,
  331. "failed to enable regulator %d: %d\n",
  332. i, ret);
  333. devm_regulator_put(par->regulators[i]);
  334. par->regulators[i] = NULL;
  335. }
  336. }
  337. par->regulators_enabled = true;
  338. }
  339. static void simplefb_regulators_destroy(struct simplefb_par *par)
  340. {
  341. int i;
  342. if (!par->regulators || !par->regulators_enabled)
  343. return;
  344. for (i = 0; i < par->regulator_count; i++)
  345. if (par->regulators[i])
  346. regulator_disable(par->regulators[i]);
  347. }
  348. #else
  349. static int simplefb_regulators_get(struct simplefb_par *par,
  350. struct platform_device *pdev) { return 0; }
  351. static void simplefb_regulators_enable(struct simplefb_par *par,
  352. struct platform_device *pdev) { }
  353. static void simplefb_regulators_destroy(struct simplefb_par *par) { }
  354. #endif
  355. static int simplefb_probe(struct platform_device *pdev)
  356. {
  357. int ret;
  358. struct simplefb_params params;
  359. struct fb_info *info;
  360. struct simplefb_par *par;
  361. struct resource *mem;
  362. if (fb_get_options("simplefb", NULL))
  363. return -ENODEV;
  364. ret = -ENODEV;
  365. if (dev_get_platdata(&pdev->dev))
  366. ret = simplefb_parse_pd(pdev, &params);
  367. else if (pdev->dev.of_node)
  368. ret = simplefb_parse_dt(pdev, &params);
  369. if (ret)
  370. return ret;
  371. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  372. if (!mem) {
  373. dev_err(&pdev->dev, "No memory resource\n");
  374. return -EINVAL;
  375. }
  376. info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
  377. if (!info)
  378. return -ENOMEM;
  379. platform_set_drvdata(pdev, info);
  380. par = info->par;
  381. info->fix = simplefb_fix;
  382. info->fix.smem_start = mem->start;
  383. info->fix.smem_len = resource_size(mem);
  384. info->fix.line_length = params.stride;
  385. info->var = simplefb_var;
  386. info->var.xres = params.width;
  387. info->var.yres = params.height;
  388. info->var.xres_virtual = params.width;
  389. info->var.yres_virtual = params.height;
  390. info->var.bits_per_pixel = params.format->bits_per_pixel;
  391. info->var.red = params.format->red;
  392. info->var.green = params.format->green;
  393. info->var.blue = params.format->blue;
  394. info->var.transp = params.format->transp;
  395. info->apertures = alloc_apertures(1);
  396. if (!info->apertures) {
  397. ret = -ENOMEM;
  398. goto error_fb_release;
  399. }
  400. info->apertures->ranges[0].base = info->fix.smem_start;
  401. info->apertures->ranges[0].size = info->fix.smem_len;
  402. info->fbops = &simplefb_ops;
  403. info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
  404. info->screen_base = ioremap_wc(info->fix.smem_start,
  405. info->fix.smem_len);
  406. if (!info->screen_base) {
  407. ret = -ENOMEM;
  408. goto error_fb_release;
  409. }
  410. info->pseudo_palette = par->palette;
  411. ret = simplefb_clocks_get(par, pdev);
  412. if (ret < 0)
  413. goto error_unmap;
  414. ret = simplefb_regulators_get(par, pdev);
  415. if (ret < 0)
  416. goto error_clocks;
  417. simplefb_clocks_enable(par, pdev);
  418. simplefb_regulators_enable(par, pdev);
  419. dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
  420. info->fix.smem_start, info->fix.smem_len,
  421. info->screen_base);
  422. dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
  423. params.format->name,
  424. info->var.xres, info->var.yres,
  425. info->var.bits_per_pixel, info->fix.line_length);
  426. ret = register_framebuffer(info);
  427. if (ret < 0) {
  428. dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
  429. goto error_regulators;
  430. }
  431. dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
  432. return 0;
  433. error_regulators:
  434. simplefb_regulators_destroy(par);
  435. error_clocks:
  436. simplefb_clocks_destroy(par);
  437. error_unmap:
  438. iounmap(info->screen_base);
  439. error_fb_release:
  440. framebuffer_release(info);
  441. return ret;
  442. }
  443. static int simplefb_remove(struct platform_device *pdev)
  444. {
  445. struct fb_info *info = platform_get_drvdata(pdev);
  446. unregister_framebuffer(info);
  447. framebuffer_release(info);
  448. return 0;
  449. }
  450. static const struct of_device_id simplefb_of_match[] = {
  451. { .compatible = "simple-framebuffer", },
  452. { },
  453. };
  454. MODULE_DEVICE_TABLE(of, simplefb_of_match);
  455. static struct platform_driver simplefb_driver = {
  456. .driver = {
  457. .name = "simple-framebuffer",
  458. .of_match_table = simplefb_of_match,
  459. },
  460. .probe = simplefb_probe,
  461. .remove = simplefb_remove,
  462. };
  463. static int __init simplefb_init(void)
  464. {
  465. int ret;
  466. struct device_node *np;
  467. ret = platform_driver_register(&simplefb_driver);
  468. if (ret)
  469. return ret;
  470. if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
  471. for_each_child_of_node(of_chosen, np) {
  472. if (of_device_is_compatible(np, "simple-framebuffer"))
  473. of_platform_device_create(np, NULL, NULL);
  474. }
  475. }
  476. return 0;
  477. }
  478. fs_initcall(simplefb_init);
  479. MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
  480. MODULE_DESCRIPTION("Simple framebuffer driver");
  481. MODULE_LICENSE("GPL v2");