armada_fbdev.c 4.2 KB

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