armada_fbdev.c 4.7 KB

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