simplefb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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-provider.h>
  29. #include <linux/of_platform.h>
  30. static struct fb_fix_screeninfo simplefb_fix = {
  31. .id = "simple",
  32. .type = FB_TYPE_PACKED_PIXELS,
  33. .visual = FB_VISUAL_TRUECOLOR,
  34. .accel = FB_ACCEL_NONE,
  35. };
  36. static struct fb_var_screeninfo simplefb_var = {
  37. .height = -1,
  38. .width = -1,
  39. .activate = FB_ACTIVATE_NOW,
  40. .vmode = FB_VMODE_NONINTERLACED,
  41. };
  42. #define PSEUDO_PALETTE_SIZE 16
  43. static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  44. u_int transp, struct fb_info *info)
  45. {
  46. u32 *pal = info->pseudo_palette;
  47. u32 cr = red >> (16 - info->var.red.length);
  48. u32 cg = green >> (16 - info->var.green.length);
  49. u32 cb = blue >> (16 - info->var.blue.length);
  50. u32 value;
  51. if (regno >= PSEUDO_PALETTE_SIZE)
  52. return -EINVAL;
  53. value = (cr << info->var.red.offset) |
  54. (cg << info->var.green.offset) |
  55. (cb << info->var.blue.offset);
  56. if (info->var.transp.length > 0) {
  57. u32 mask = (1 << info->var.transp.length) - 1;
  58. mask <<= info->var.transp.offset;
  59. value |= mask;
  60. }
  61. pal[regno] = value;
  62. return 0;
  63. }
  64. static void simplefb_destroy(struct fb_info *info)
  65. {
  66. if (info->screen_base)
  67. iounmap(info->screen_base);
  68. }
  69. static struct fb_ops simplefb_ops = {
  70. .owner = THIS_MODULE,
  71. .fb_destroy = simplefb_destroy,
  72. .fb_setcolreg = simplefb_setcolreg,
  73. .fb_fillrect = cfb_fillrect,
  74. .fb_copyarea = cfb_copyarea,
  75. .fb_imageblit = cfb_imageblit,
  76. };
  77. static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
  78. struct simplefb_params {
  79. u32 width;
  80. u32 height;
  81. u32 stride;
  82. struct simplefb_format *format;
  83. };
  84. static int simplefb_parse_dt(struct platform_device *pdev,
  85. struct simplefb_params *params)
  86. {
  87. struct device_node *np = pdev->dev.of_node;
  88. int ret;
  89. const char *format;
  90. int i;
  91. ret = of_property_read_u32(np, "width", &params->width);
  92. if (ret) {
  93. dev_err(&pdev->dev, "Can't parse width property\n");
  94. return ret;
  95. }
  96. ret = of_property_read_u32(np, "height", &params->height);
  97. if (ret) {
  98. dev_err(&pdev->dev, "Can't parse height property\n");
  99. return ret;
  100. }
  101. ret = of_property_read_u32(np, "stride", &params->stride);
  102. if (ret) {
  103. dev_err(&pdev->dev, "Can't parse stride property\n");
  104. return ret;
  105. }
  106. ret = of_property_read_string(np, "format", &format);
  107. if (ret) {
  108. dev_err(&pdev->dev, "Can't parse format property\n");
  109. return ret;
  110. }
  111. params->format = NULL;
  112. for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
  113. if (strcmp(format, simplefb_formats[i].name))
  114. continue;
  115. params->format = &simplefb_formats[i];
  116. break;
  117. }
  118. if (!params->format) {
  119. dev_err(&pdev->dev, "Invalid format value\n");
  120. return -EINVAL;
  121. }
  122. return 0;
  123. }
  124. static int simplefb_parse_pd(struct platform_device *pdev,
  125. struct simplefb_params *params)
  126. {
  127. struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
  128. int i;
  129. params->width = pd->width;
  130. params->height = pd->height;
  131. params->stride = pd->stride;
  132. params->format = NULL;
  133. for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
  134. if (strcmp(pd->format, simplefb_formats[i].name))
  135. continue;
  136. params->format = &simplefb_formats[i];
  137. break;
  138. }
  139. if (!params->format) {
  140. dev_err(&pdev->dev, "Invalid format value\n");
  141. return -EINVAL;
  142. }
  143. return 0;
  144. }
  145. struct simplefb_par {
  146. u32 palette[PSEUDO_PALETTE_SIZE];
  147. #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
  148. int clk_count;
  149. struct clk **clks;
  150. #endif
  151. };
  152. #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
  153. /*
  154. * Clock handling code.
  155. *
  156. * Here we handle the clocks property of our "simple-framebuffer" dt node.
  157. * This is necessary so that we can make sure that any clocks needed by
  158. * the display engine that the bootloader set up for us (and for which it
  159. * provided a simplefb dt node), stay up, for the life of the simplefb
  160. * driver.
  161. *
  162. * When the driver unloads, we cleanly disable, and then release the clocks.
  163. *
  164. * We only complain about errors here, no action is taken as the most likely
  165. * error can only happen due to a mismatch between the bootloader which set
  166. * up simplefb, and the clock definitions in the device tree. Chances are
  167. * that there are no adverse effects, and if there are, a clean teardown of
  168. * the fb probe will not help us much either. So just complain and carry on,
  169. * and hope that the user actually gets a working fb at the end of things.
  170. */
  171. static int simplefb_clocks_init(struct simplefb_par *par,
  172. struct platform_device *pdev)
  173. {
  174. struct device_node *np = pdev->dev.of_node;
  175. struct clk *clock;
  176. int i, ret;
  177. if (dev_get_platdata(&pdev->dev) || !np)
  178. return 0;
  179. par->clk_count = of_clk_get_parent_count(np);
  180. if (par->clk_count <= 0)
  181. return 0;
  182. par->clks = kcalloc(par->clk_count, sizeof(struct clk *), GFP_KERNEL);
  183. if (!par->clks)
  184. return -ENOMEM;
  185. for (i = 0; i < par->clk_count; i++) {
  186. clock = of_clk_get(np, i);
  187. if (IS_ERR(clock)) {
  188. if (PTR_ERR(clock) == -EPROBE_DEFER) {
  189. while (--i >= 0) {
  190. if (par->clks[i])
  191. clk_put(par->clks[i]);
  192. }
  193. kfree(par->clks);
  194. return -EPROBE_DEFER;
  195. }
  196. dev_err(&pdev->dev, "%s: clock %d not found: %ld\n",
  197. __func__, i, PTR_ERR(clock));
  198. continue;
  199. }
  200. par->clks[i] = clock;
  201. }
  202. for (i = 0; i < par->clk_count; i++) {
  203. if (par->clks[i]) {
  204. ret = clk_prepare_enable(par->clks[i]);
  205. if (ret) {
  206. dev_err(&pdev->dev,
  207. "%s: failed to enable clock %d: %d\n",
  208. __func__, i, ret);
  209. clk_put(par->clks[i]);
  210. par->clks[i] = NULL;
  211. }
  212. }
  213. }
  214. return 0;
  215. }
  216. static void simplefb_clocks_destroy(struct simplefb_par *par)
  217. {
  218. int i;
  219. if (!par->clks)
  220. return;
  221. for (i = 0; i < par->clk_count; i++) {
  222. if (par->clks[i]) {
  223. clk_disable_unprepare(par->clks[i]);
  224. clk_put(par->clks[i]);
  225. }
  226. }
  227. kfree(par->clks);
  228. }
  229. #else
  230. static int simplefb_clocks_init(struct simplefb_par *par,
  231. struct platform_device *pdev) { return 0; }
  232. static void simplefb_clocks_destroy(struct simplefb_par *par) { }
  233. #endif
  234. static int simplefb_probe(struct platform_device *pdev)
  235. {
  236. int ret;
  237. struct simplefb_params params;
  238. struct fb_info *info;
  239. struct simplefb_par *par;
  240. struct resource *mem;
  241. if (fb_get_options("simplefb", NULL))
  242. return -ENODEV;
  243. ret = -ENODEV;
  244. if (dev_get_platdata(&pdev->dev))
  245. ret = simplefb_parse_pd(pdev, &params);
  246. else if (pdev->dev.of_node)
  247. ret = simplefb_parse_dt(pdev, &params);
  248. if (ret)
  249. return ret;
  250. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  251. if (!mem) {
  252. dev_err(&pdev->dev, "No memory resource\n");
  253. return -EINVAL;
  254. }
  255. info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
  256. if (!info)
  257. return -ENOMEM;
  258. platform_set_drvdata(pdev, info);
  259. par = info->par;
  260. info->fix = simplefb_fix;
  261. info->fix.smem_start = mem->start;
  262. info->fix.smem_len = resource_size(mem);
  263. info->fix.line_length = params.stride;
  264. info->var = simplefb_var;
  265. info->var.xres = params.width;
  266. info->var.yres = params.height;
  267. info->var.xres_virtual = params.width;
  268. info->var.yres_virtual = params.height;
  269. info->var.bits_per_pixel = params.format->bits_per_pixel;
  270. info->var.red = params.format->red;
  271. info->var.green = params.format->green;
  272. info->var.blue = params.format->blue;
  273. info->var.transp = params.format->transp;
  274. info->apertures = alloc_apertures(1);
  275. if (!info->apertures) {
  276. ret = -ENOMEM;
  277. goto error_fb_release;
  278. }
  279. info->apertures->ranges[0].base = info->fix.smem_start;
  280. info->apertures->ranges[0].size = info->fix.smem_len;
  281. info->fbops = &simplefb_ops;
  282. info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
  283. info->screen_base = ioremap_wc(info->fix.smem_start,
  284. info->fix.smem_len);
  285. if (!info->screen_base) {
  286. ret = -ENOMEM;
  287. goto error_fb_release;
  288. }
  289. info->pseudo_palette = par->palette;
  290. ret = simplefb_clocks_init(par, pdev);
  291. if (ret < 0)
  292. goto error_unmap;
  293. dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
  294. info->fix.smem_start, info->fix.smem_len,
  295. info->screen_base);
  296. dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
  297. params.format->name,
  298. info->var.xres, info->var.yres,
  299. info->var.bits_per_pixel, info->fix.line_length);
  300. ret = register_framebuffer(info);
  301. if (ret < 0) {
  302. dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
  303. goto error_clocks;
  304. }
  305. dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
  306. return 0;
  307. error_clocks:
  308. simplefb_clocks_destroy(par);
  309. error_unmap:
  310. iounmap(info->screen_base);
  311. error_fb_release:
  312. framebuffer_release(info);
  313. return ret;
  314. }
  315. static int simplefb_remove(struct platform_device *pdev)
  316. {
  317. struct fb_info *info = platform_get_drvdata(pdev);
  318. struct simplefb_par *par = info->par;
  319. unregister_framebuffer(info);
  320. simplefb_clocks_destroy(par);
  321. framebuffer_release(info);
  322. return 0;
  323. }
  324. static const struct of_device_id simplefb_of_match[] = {
  325. { .compatible = "simple-framebuffer", },
  326. { },
  327. };
  328. MODULE_DEVICE_TABLE(of, simplefb_of_match);
  329. static struct platform_driver simplefb_driver = {
  330. .driver = {
  331. .name = "simple-framebuffer",
  332. .of_match_table = simplefb_of_match,
  333. },
  334. .probe = simplefb_probe,
  335. .remove = simplefb_remove,
  336. };
  337. static int __init simplefb_init(void)
  338. {
  339. int ret;
  340. struct device_node *np;
  341. ret = platform_driver_register(&simplefb_driver);
  342. if (ret)
  343. return ret;
  344. if (IS_ENABLED(CONFIG_OF_ADDRESS) && of_chosen) {
  345. for_each_child_of_node(of_chosen, np) {
  346. if (of_device_is_compatible(np, "simple-framebuffer"))
  347. of_platform_device_create(np, NULL, NULL);
  348. }
  349. }
  350. return 0;
  351. }
  352. fs_initcall(simplefb_init);
  353. MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
  354. MODULE_DESCRIPTION("Simple framebuffer driver");
  355. MODULE_LICENSE("GPL v2");