armada_fbdev.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2012 Russell King
  3. * Written from the i915 driver.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <drm/drm_fb_helper.h>
  13. #include "armada_crtc.h"
  14. #include "armada_drm.h"
  15. #include "armada_fb.h"
  16. #include "armada_gem.h"
  17. static /*const*/ struct fb_ops armada_fb_ops = {
  18. .owner = THIS_MODULE,
  19. DRM_FB_HELPER_DEFAULT_OPS,
  20. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  21. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  22. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  23. };
  24. static int armada_fbdev_create(struct drm_fb_helper *fbh,
  25. struct drm_fb_helper_surface_size *sizes)
  26. {
  27. struct drm_device *dev = fbh->dev;
  28. struct drm_mode_fb_cmd2 mode;
  29. struct armada_framebuffer *dfb;
  30. struct armada_gem_object *obj;
  31. struct fb_info *info;
  32. int size, ret;
  33. void *ptr;
  34. memset(&mode, 0, sizeof(mode));
  35. mode.width = sizes->surface_width;
  36. mode.height = sizes->surface_height;
  37. mode.pitches[0] = armada_pitch(mode.width, sizes->surface_bpp);
  38. mode.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  39. sizes->surface_depth);
  40. size = mode.pitches[0] * mode.height;
  41. obj = armada_gem_alloc_private_object(dev, size);
  42. if (!obj) {
  43. DRM_ERROR("failed to allocate fb memory\n");
  44. return -ENOMEM;
  45. }
  46. ret = armada_gem_linear_back(dev, obj);
  47. if (ret) {
  48. drm_gem_object_put_unlocked(&obj->obj);
  49. return ret;
  50. }
  51. ptr = armada_gem_map_object(dev, obj);
  52. if (!ptr) {
  53. drm_gem_object_put_unlocked(&obj->obj);
  54. return -ENOMEM;
  55. }
  56. dfb = armada_framebuffer_create(dev, &mode, obj);
  57. /*
  58. * A reference is now held by the framebuffer object if
  59. * successful, otherwise this drops the ref for the error path.
  60. */
  61. drm_gem_object_put_unlocked(&obj->obj);
  62. if (IS_ERR(dfb))
  63. return PTR_ERR(dfb);
  64. info = drm_fb_helper_alloc_fbi(fbh);
  65. if (IS_ERR(info)) {
  66. ret = PTR_ERR(info);
  67. goto err_fballoc;
  68. }
  69. strlcpy(info->fix.id, "armada-drmfb", sizeof(info->fix.id));
  70. info->par = fbh;
  71. info->fbops = &armada_fb_ops;
  72. info->fix.smem_start = obj->phys_addr;
  73. info->fix.smem_len = obj->obj.size;
  74. info->screen_size = obj->obj.size;
  75. info->screen_base = ptr;
  76. fbh->fb = &dfb->fb;
  77. drm_fb_helper_fill_fix(info, dfb->fb.pitches[0],
  78. dfb->fb.format->depth);
  79. drm_fb_helper_fill_var(info, fbh, sizes->fb_width, sizes->fb_height);
  80. DRM_DEBUG_KMS("allocated %dx%d %dbpp fb: 0x%08llx\n",
  81. dfb->fb.width, dfb->fb.height, dfb->fb.format->cpp[0] * 8,
  82. (unsigned long long)obj->phys_addr);
  83. return 0;
  84. err_fballoc:
  85. dfb->fb.funcs->destroy(&dfb->fb);
  86. return ret;
  87. }
  88. static int armada_fb_probe(struct drm_fb_helper *fbh,
  89. struct drm_fb_helper_surface_size *sizes)
  90. {
  91. int ret = 0;
  92. if (!fbh->fb) {
  93. ret = armada_fbdev_create(fbh, sizes);
  94. if (ret == 0)
  95. ret = 1;
  96. }
  97. return ret;
  98. }
  99. static const struct drm_fb_helper_funcs armada_fb_helper_funcs = {
  100. .fb_probe = armada_fb_probe,
  101. };
  102. int armada_fbdev_init(struct drm_device *dev)
  103. {
  104. struct armada_private *priv = dev->dev_private;
  105. struct drm_fb_helper *fbh;
  106. int ret;
  107. fbh = devm_kzalloc(dev->dev, sizeof(*fbh), GFP_KERNEL);
  108. if (!fbh)
  109. return -ENOMEM;
  110. priv->fbdev = fbh;
  111. drm_fb_helper_prepare(dev, fbh, &armada_fb_helper_funcs);
  112. ret = drm_fb_helper_init(dev, fbh, 1);
  113. if (ret) {
  114. DRM_ERROR("failed to initialize drm fb helper\n");
  115. goto err_fb_helper;
  116. }
  117. ret = drm_fb_helper_single_add_all_connectors(fbh);
  118. if (ret) {
  119. DRM_ERROR("failed to add fb connectors\n");
  120. goto err_fb_setup;
  121. }
  122. ret = drm_fb_helper_initial_config(fbh, 32);
  123. if (ret) {
  124. DRM_ERROR("failed to set initial config\n");
  125. goto err_fb_setup;
  126. }
  127. return 0;
  128. err_fb_setup:
  129. drm_fb_helper_fini(fbh);
  130. err_fb_helper:
  131. priv->fbdev = NULL;
  132. return ret;
  133. }
  134. void armada_fbdev_fini(struct drm_device *dev)
  135. {
  136. struct armada_private *priv = dev->dev_private;
  137. struct drm_fb_helper *fbh = priv->fbdev;
  138. if (fbh) {
  139. drm_fb_helper_unregister_fbi(fbh);
  140. drm_fb_helper_fini(fbh);
  141. if (fbh->fb)
  142. fbh->fb->funcs->destroy(fbh->fb);
  143. priv->fbdev = NULL;
  144. }
  145. }