simplefb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. static struct fb_fix_screeninfo simplefb_fix = {
  29. .id = "simple",
  30. .type = FB_TYPE_PACKED_PIXELS,
  31. .visual = FB_VISUAL_TRUECOLOR,
  32. .accel = FB_ACCEL_NONE,
  33. };
  34. static struct fb_var_screeninfo simplefb_var = {
  35. .height = -1,
  36. .width = -1,
  37. .activate = FB_ACTIVATE_NOW,
  38. .vmode = FB_VMODE_NONINTERLACED,
  39. };
  40. static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  41. u_int transp, struct fb_info *info)
  42. {
  43. u32 *pal = info->pseudo_palette;
  44. u32 cr = red >> (16 - info->var.red.length);
  45. u32 cg = green >> (16 - info->var.green.length);
  46. u32 cb = blue >> (16 - info->var.blue.length);
  47. u32 value;
  48. if (regno >= 16)
  49. return -EINVAL;
  50. value = (cr << info->var.red.offset) |
  51. (cg << info->var.green.offset) |
  52. (cb << info->var.blue.offset);
  53. if (info->var.transp.length > 0) {
  54. u32 mask = (1 << info->var.transp.length) - 1;
  55. mask <<= info->var.transp.offset;
  56. value |= mask;
  57. }
  58. pal[regno] = value;
  59. return 0;
  60. }
  61. static struct fb_ops simplefb_ops = {
  62. .owner = THIS_MODULE,
  63. .fb_setcolreg = simplefb_setcolreg,
  64. .fb_fillrect = cfb_fillrect,
  65. .fb_copyarea = cfb_copyarea,
  66. .fb_imageblit = cfb_imageblit,
  67. };
  68. static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
  69. struct simplefb_params {
  70. u32 width;
  71. u32 height;
  72. u32 stride;
  73. struct simplefb_format *format;
  74. };
  75. static int simplefb_parse_dt(struct platform_device *pdev,
  76. struct simplefb_params *params)
  77. {
  78. struct device_node *np = pdev->dev.of_node;
  79. int ret;
  80. const char *format;
  81. int i;
  82. ret = of_property_read_u32(np, "width", &params->width);
  83. if (ret) {
  84. dev_err(&pdev->dev, "Can't parse width property\n");
  85. return ret;
  86. }
  87. ret = of_property_read_u32(np, "height", &params->height);
  88. if (ret) {
  89. dev_err(&pdev->dev, "Can't parse height property\n");
  90. return ret;
  91. }
  92. ret = of_property_read_u32(np, "stride", &params->stride);
  93. if (ret) {
  94. dev_err(&pdev->dev, "Can't parse stride property\n");
  95. return ret;
  96. }
  97. ret = of_property_read_string(np, "format", &format);
  98. if (ret) {
  99. dev_err(&pdev->dev, "Can't parse format property\n");
  100. return ret;
  101. }
  102. params->format = NULL;
  103. for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
  104. if (strcmp(format, simplefb_formats[i].name))
  105. continue;
  106. params->format = &simplefb_formats[i];
  107. break;
  108. }
  109. if (!params->format) {
  110. dev_err(&pdev->dev, "Invalid format value\n");
  111. return -EINVAL;
  112. }
  113. return 0;
  114. }
  115. static int simplefb_parse_pd(struct platform_device *pdev,
  116. struct simplefb_params *params)
  117. {
  118. struct simplefb_platform_data *pd = pdev->dev.platform_data;
  119. int i;
  120. params->width = pd->width;
  121. params->height = pd->height;
  122. params->stride = pd->stride;
  123. params->format = NULL;
  124. for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
  125. if (strcmp(pd->format, simplefb_formats[i].name))
  126. continue;
  127. params->format = &simplefb_formats[i];
  128. break;
  129. }
  130. if (!params->format) {
  131. dev_err(&pdev->dev, "Invalid format value\n");
  132. return -EINVAL;
  133. }
  134. return 0;
  135. }
  136. static int simplefb_probe(struct platform_device *pdev)
  137. {
  138. int ret;
  139. struct simplefb_params params;
  140. struct fb_info *info;
  141. struct resource *mem;
  142. if (fb_get_options("simplefb", NULL))
  143. return -ENODEV;
  144. ret = -ENODEV;
  145. if (pdev->dev.platform_data)
  146. ret = simplefb_parse_pd(pdev, &params);
  147. else if (pdev->dev.of_node)
  148. ret = simplefb_parse_dt(pdev, &params);
  149. if (ret)
  150. return ret;
  151. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  152. if (!mem) {
  153. dev_err(&pdev->dev, "No memory resource\n");
  154. return -EINVAL;
  155. }
  156. info = framebuffer_alloc(sizeof(u32) * 16, &pdev->dev);
  157. if (!info)
  158. return -ENOMEM;
  159. platform_set_drvdata(pdev, info);
  160. info->fix = simplefb_fix;
  161. info->fix.smem_start = mem->start;
  162. info->fix.smem_len = resource_size(mem);
  163. info->fix.line_length = params.stride;
  164. info->var = simplefb_var;
  165. info->var.xres = params.width;
  166. info->var.yres = params.height;
  167. info->var.xres_virtual = params.width;
  168. info->var.yres_virtual = params.height;
  169. info->var.bits_per_pixel = params.format->bits_per_pixel;
  170. info->var.red = params.format->red;
  171. info->var.green = params.format->green;
  172. info->var.blue = params.format->blue;
  173. info->var.transp = params.format->transp;
  174. info->apertures = alloc_apertures(1);
  175. if (!info->apertures) {
  176. framebuffer_release(info);
  177. return -ENOMEM;
  178. }
  179. info->apertures->ranges[0].base = info->fix.smem_start;
  180. info->apertures->ranges[0].size = info->fix.smem_len;
  181. info->fbops = &simplefb_ops;
  182. info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
  183. info->screen_base = devm_ioremap(&pdev->dev, info->fix.smem_start,
  184. info->fix.smem_len);
  185. if (!info->screen_base) {
  186. framebuffer_release(info);
  187. return -ENODEV;
  188. }
  189. info->pseudo_palette = (void *)(info + 1);
  190. ret = register_framebuffer(info);
  191. if (ret < 0) {
  192. dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
  193. framebuffer_release(info);
  194. return ret;
  195. }
  196. dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
  197. return 0;
  198. }
  199. static int simplefb_remove(struct platform_device *pdev)
  200. {
  201. struct fb_info *info = platform_get_drvdata(pdev);
  202. unregister_framebuffer(info);
  203. framebuffer_release(info);
  204. return 0;
  205. }
  206. static const struct of_device_id simplefb_of_match[] = {
  207. { .compatible = "simple-framebuffer", },
  208. { },
  209. };
  210. MODULE_DEVICE_TABLE(of, simplefb_of_match);
  211. static struct platform_driver simplefb_driver = {
  212. .driver = {
  213. .name = "simple-framebuffer",
  214. .owner = THIS_MODULE,
  215. .of_match_table = simplefb_of_match,
  216. },
  217. .probe = simplefb_probe,
  218. .remove = simplefb_remove,
  219. };
  220. module_platform_driver(simplefb_driver);
  221. MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
  222. MODULE_DESCRIPTION("Simple framebuffer driver");
  223. MODULE_LICENSE("GPL v2");