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