simplefb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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/clk-provider.h>
  30. #include <linux/of.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. unsigned int clk_count;
  158. struct clk **clks;
  159. #endif
  160. #if defined CONFIG_OF && defined CONFIG_REGULATOR
  161. u32 regulator_count;
  162. struct regulator **regulators;
  163. #endif
  164. };
  165. #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
  166. /*
  167. * Clock handling code.
  168. *
  169. * Here we handle the clocks property of our "simple-framebuffer" dt node.
  170. * This is necessary so that we can make sure that any clocks needed by
  171. * the display engine that the bootloader set up for us (and for which it
  172. * provided a simplefb dt node), stay up, for the life of the simplefb
  173. * driver.
  174. *
  175. * When the driver unloads, we cleanly disable, and then release the clocks.
  176. *
  177. * We only complain about errors here, no action is taken as the most likely
  178. * error can only happen due to a mismatch between the bootloader which set
  179. * up simplefb, and the clock definitions in the device tree. Chances are
  180. * that there are no adverse effects, and if there are, a clean teardown of
  181. * the fb probe will not help us much either. So just complain and carry on,
  182. * and hope that the user actually gets a working fb at the end of things.
  183. */
  184. static int simplefb_clocks_init(struct simplefb_par *par,
  185. struct platform_device *pdev)
  186. {
  187. struct device_node *np = pdev->dev.of_node;
  188. struct clk *clock;
  189. int i, ret;
  190. if (dev_get_platdata(&pdev->dev) || !np)
  191. return 0;
  192. par->clk_count = of_clk_get_parent_count(np);
  193. if (!par->clk_count)
  194. return 0;
  195. par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
  196. if (!par->clks)
  197. return -ENOMEM;
  198. for (i = 0; i < par->clk_count; i++) {
  199. clock = of_clk_get(np, i);
  200. if (IS_ERR(clock)) {
  201. if (PTR_ERR(clock) == -EPROBE_DEFER) {
  202. while (--i >= 0) {
  203. if (par->clks[i])
  204. clk_put(par->clks[i]);
  205. }
  206. kfree(par->clks);
  207. return -EPROBE_DEFER;
  208. }
  209. dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
  210. __func__, i, PTR_ERR(clock));
  211. continue;
  212. }
  213. par->clks[i] = clock;
  214. }
  215. for (i = 0; i < par->clk_count; i++) {
  216. if (par->clks[i]) {
  217. ret = clk_prepare_enable(par->clks[i]);
  218. if (ret) {
  219. dev_err(&pdev->dev,
  220. "%s: failed to enable clock %d: %d\n",
  221. __func__, i, ret);
  222. clk_put(par->clks[i]);
  223. par->clks[i] = NULL;
  224. }
  225. }
  226. }
  227. return 0;
  228. }
  229. static void simplefb_clocks_destroy(struct simplefb_par *par)
  230. {
  231. int i;
  232. if (!par->clks)
  233. return;
  234. for (i = 0; i < par->clk_count; i++) {
  235. if (par->clks[i]) {
  236. clk_disable_unprepare(par->clks[i]);
  237. clk_put(par->clks[i]);
  238. }
  239. }
  240. kfree(par->clks);
  241. }
  242. #else
  243. static int simplefb_clocks_init(struct simplefb_par *par,
  244. struct platform_device *pdev) { return 0; }
  245. static void simplefb_clocks_destroy(struct simplefb_par *par) { }
  246. #endif
  247. #if defined CONFIG_OF && defined CONFIG_REGULATOR
  248. #define SUPPLY_SUFFIX "-supply"
  249. /*
  250. * Regulator handling code.
  251. *
  252. * Here we handle the num-supplies and vin*-supply properties of our
  253. * "simple-framebuffer" dt node. This is necessary so that we can make sure
  254. * that any regulators needed by the display hardware that the bootloader
  255. * set up for us (and for which it provided a simplefb dt node), stay up,
  256. * for the life of the simplefb driver.
  257. *
  258. * When the driver unloads, we cleanly disable, and then release the
  259. * regulators.
  260. *
  261. * We only complain about errors here, no action is taken as the most likely
  262. * error can only happen due to a mismatch between the bootloader which set
  263. * up simplefb, and the regulator definitions in the device tree. Chances are
  264. * that there are no adverse effects, and if there are, a clean teardown of
  265. * the fb probe will not help us much either. So just complain and carry on,
  266. * and hope that the user actually gets a working fb at the end of things.
  267. */
  268. static int simplefb_regulators_init(struct simplefb_par *par,
  269. struct platform_device *pdev)
  270. {
  271. struct device_node *np = pdev->dev.of_node;
  272. struct property *prop;
  273. struct regulator *regulator;
  274. const char *p;
  275. int count = 0, i = 0, ret;
  276. if (dev_get_platdata(&pdev->dev) || !np)
  277. return 0;
  278. /* Count the number of regulator supplies */
  279. for_each_property_of_node(np, prop) {
  280. p = strstr(prop->name, SUPPLY_SUFFIX);
  281. if (p && p != prop->name)
  282. count++;
  283. }
  284. if (!count)
  285. return 0;
  286. par->regulators = devm_kcalloc(&pdev->dev, count,
  287. sizeof(struct regulator *), GFP_KERNEL);
  288. if (!par->regulators)
  289. return -ENOMEM;
  290. /* Get all the regulators */
  291. for_each_property_of_node(np, prop) {
  292. char name[32]; /* 32 is max size of property name */
  293. p = strstr(prop->name, SUPPLY_SUFFIX);
  294. if (!p || p == prop->name)
  295. continue;
  296. strlcpy(name, prop->name,
  297. strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1);
  298. regulator = devm_regulator_get_optional(&pdev->dev, name);
  299. if (IS_ERR(regulator)) {
  300. if (PTR_ERR(regulator) == -EPROBE_DEFER)
  301. return -EPROBE_DEFER;
  302. dev_err(&pdev->dev, "regulator %s not found: %ld\n",
  303. name, PTR_ERR(regulator));
  304. continue;
  305. }
  306. par->regulators[i++] = regulator;
  307. }
  308. par->regulator_count = i;
  309. /* Enable all the regulators */
  310. for (i = 0; i < par->regulator_count; i++) {
  311. ret = regulator_enable(par->regulators[i]);
  312. if (ret) {
  313. dev_err(&pdev->dev,
  314. "failed to enable regulator %d: %d\n",
  315. i, ret);
  316. devm_regulator_put(par->regulators[i]);
  317. par->regulators[i] = NULL;
  318. }
  319. }
  320. return 0;
  321. }
  322. static void simplefb_regulators_destroy(struct simplefb_par *par)
  323. {
  324. int i;
  325. if (!par->regulators)
  326. return;
  327. for (i = 0; i < par->regulator_count; i++)
  328. if (par->regulators[i])
  329. regulator_disable(par->regulators[i]);
  330. }
  331. #else
  332. static int simplefb_regulators_init(struct simplefb_par *par,
  333. struct platform_device *pdev) { return 0; }
  334. static void simplefb_regulators_destroy(struct simplefb_par *par) { }
  335. #endif
  336. static int simplefb_probe(struct platform_device *pdev)
  337. {
  338. int ret;
  339. struct simplefb_params params;
  340. struct fb_info *info;
  341. struct simplefb_par *par;
  342. struct resource *mem;
  343. if (fb_get_options("simplefb", NULL))
  344. return -ENODEV;
  345. ret = -ENODEV;
  346. if (dev_get_platdata(&pdev->dev))
  347. ret = simplefb_parse_pd(pdev, &params);
  348. else if (pdev->dev.of_node)
  349. ret = simplefb_parse_dt(pdev, &params);
  350. if (ret)
  351. return ret;
  352. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  353. if (!mem) {
  354. dev_err(&pdev->dev, "No memory resource\n");
  355. return -EINVAL;
  356. }
  357. info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
  358. if (!info)
  359. return -ENOMEM;
  360. platform_set_drvdata(pdev, info);
  361. par = info->par;
  362. info->fix = simplefb_fix;
  363. info->fix.smem_start = mem->start;
  364. info->fix.smem_len = resource_size(mem);
  365. info->fix.line_length = params.stride;
  366. info->var = simplefb_var;
  367. info->var.xres = params.width;
  368. info->var.yres = params.height;
  369. info->var.xres_virtual = params.width;
  370. info->var.yres_virtual = params.height;
  371. info->var.bits_per_pixel = params.format->bits_per_pixel;
  372. info->var.red = params.format->red;
  373. info->var.green = params.format->green;
  374. info->var.blue = params.format->blue;
  375. info->var.transp = params.format->transp;
  376. info->apertures = alloc_apertures(1);
  377. if (!info->apertures) {
  378. ret = -ENOMEM;
  379. goto error_fb_release;
  380. }
  381. info->apertures->ranges[0].base = info->fix.smem_start;
  382. info->apertures->ranges[0].size = info->fix.smem_len;
  383. info->fbops = &simplefb_ops;
  384. info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
  385. info->screen_base = ioremap_wc(info->fix.smem_start,
  386. info->fix.smem_len);
  387. if (!info->screen_base) {
  388. ret = -ENOMEM;
  389. goto error_fb_release;
  390. }
  391. info->pseudo_palette = par->palette;
  392. ret = simplefb_clocks_init(par, pdev);
  393. if (ret < 0)
  394. goto error_unmap;
  395. ret = simplefb_regulators_init(par, pdev);
  396. if (ret < 0)
  397. goto error_clocks;
  398. dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
  399. info->fix.smem_start, info->fix.smem_len,
  400. info->screen_base);
  401. dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
  402. params.format->name,
  403. info->var.xres, info->var.yres,
  404. info->var.bits_per_pixel, info->fix.line_length);
  405. ret = register_framebuffer(info);
  406. if (ret < 0) {
  407. dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
  408. goto error_regulators;
  409. }
  410. dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
  411. return 0;
  412. error_regulators:
  413. simplefb_regulators_destroy(par);
  414. error_clocks:
  415. simplefb_clocks_destroy(par);
  416. error_unmap:
  417. iounmap(info->screen_base);
  418. error_fb_release:
  419. framebuffer_release(info);
  420. return ret;
  421. }
  422. static int simplefb_remove(struct platform_device *pdev)
  423. {
  424. struct fb_info *info = platform_get_drvdata(pdev);
  425. unregister_framebuffer(info);
  426. framebuffer_release(info);
  427. return 0;
  428. }
  429. static const struct of_device_id simplefb_of_match[] = {
  430. { .compatible = "simple-framebuffer", },
  431. { },
  432. };
  433. MODULE_DEVICE_TABLE(of, simplefb_of_match);
  434. static struct platform_driver simplefb_driver = {
  435. .driver = {
  436. .name = "simple-framebuffer",
  437. .of_match_table = simplefb_of_match,
  438. },
  439. .probe = simplefb_probe,
  440. .remove = simplefb_remove,
  441. };
  442. static int __init simplefb_init(void)
  443. {
  444. int ret;
  445. struct device_node *np;
  446. ret = platform_driver_register(&simplefb_driver);
  447. if (ret)
  448. return ret;
  449. if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
  450. for_each_child_of_node(of_chosen, np) {
  451. if (of_device_is_compatible(np, "simple-framebuffer"))
  452. of_platform_device_create(np, NULL, NULL);
  453. }
  454. }
  455. return 0;
  456. }
  457. fs_initcall(simplefb_init);
  458. MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
  459. MODULE_DESCRIPTION("Simple framebuffer driver");
  460. MODULE_LICENSE("GPL v2");