armada_fb.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2012 Russell King
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <drm/drmP.h>
  9. #include <drm/drm_crtc_helper.h>
  10. #include <drm/drm_fb_helper.h>
  11. #include "armada_drm.h"
  12. #include "armada_fb.h"
  13. #include "armada_gem.h"
  14. #include "armada_hw.h"
  15. static void armada_fb_destroy(struct drm_framebuffer *fb)
  16. {
  17. struct armada_framebuffer *dfb = drm_fb_to_armada_fb(fb);
  18. drm_framebuffer_cleanup(&dfb->fb);
  19. drm_gem_object_unreference_unlocked(&dfb->obj->obj);
  20. kfree(dfb);
  21. }
  22. static int armada_fb_create_handle(struct drm_framebuffer *fb,
  23. struct drm_file *dfile, unsigned int *handle)
  24. {
  25. struct armada_framebuffer *dfb = drm_fb_to_armada_fb(fb);
  26. return drm_gem_handle_create(dfile, &dfb->obj->obj, handle);
  27. }
  28. static const struct drm_framebuffer_funcs armada_fb_funcs = {
  29. .destroy = armada_fb_destroy,
  30. .create_handle = armada_fb_create_handle,
  31. };
  32. struct armada_framebuffer *armada_framebuffer_create(struct drm_device *dev,
  33. struct drm_mode_fb_cmd2 *mode, struct armada_gem_object *obj)
  34. {
  35. struct armada_framebuffer *dfb;
  36. uint8_t format, config;
  37. int ret;
  38. switch (mode->pixel_format) {
  39. #define FMT(drm, fmt, mod) \
  40. case DRM_FORMAT_##drm: \
  41. format = CFG_##fmt; \
  42. config = mod; \
  43. break
  44. FMT(RGB565, 565, CFG_SWAPRB);
  45. FMT(BGR565, 565, 0);
  46. FMT(ARGB1555, 1555, CFG_SWAPRB);
  47. FMT(ABGR1555, 1555, 0);
  48. FMT(RGB888, 888PACK, CFG_SWAPRB);
  49. FMT(BGR888, 888PACK, 0);
  50. FMT(XRGB8888, X888, CFG_SWAPRB);
  51. FMT(XBGR8888, X888, 0);
  52. FMT(ARGB8888, 8888, CFG_SWAPRB);
  53. FMT(ABGR8888, 8888, 0);
  54. FMT(YUYV, 422PACK, CFG_YUV2RGB | CFG_SWAPYU | CFG_SWAPUV);
  55. FMT(UYVY, 422PACK, CFG_YUV2RGB);
  56. FMT(VYUY, 422PACK, CFG_YUV2RGB | CFG_SWAPUV);
  57. FMT(YVYU, 422PACK, CFG_YUV2RGB | CFG_SWAPYU);
  58. FMT(YUV422, 422, CFG_YUV2RGB);
  59. FMT(YVU422, 422, CFG_YUV2RGB | CFG_SWAPUV);
  60. FMT(YUV420, 420, CFG_YUV2RGB);
  61. FMT(YVU420, 420, CFG_YUV2RGB | CFG_SWAPUV);
  62. FMT(C8, PSEUDO8, 0);
  63. #undef FMT
  64. default:
  65. return ERR_PTR(-EINVAL);
  66. }
  67. dfb = kzalloc(sizeof(*dfb), GFP_KERNEL);
  68. if (!dfb) {
  69. DRM_ERROR("failed to allocate Armada fb object\n");
  70. return ERR_PTR(-ENOMEM);
  71. }
  72. dfb->fmt = format;
  73. dfb->mod = config;
  74. dfb->obj = obj;
  75. drm_helper_mode_fill_fb_struct(&dfb->fb, mode);
  76. ret = drm_framebuffer_init(dev, &dfb->fb, &armada_fb_funcs);
  77. if (ret) {
  78. kfree(dfb);
  79. return ERR_PTR(ret);
  80. }
  81. /*
  82. * Take a reference on our object as we're successful - the
  83. * caller already holds a reference, which keeps us safe for
  84. * the above call, but the caller will drop their reference
  85. * to it. Hence we need to take our own reference.
  86. */
  87. drm_gem_object_reference(&obj->obj);
  88. return dfb;
  89. }
  90. static struct drm_framebuffer *armada_fb_create(struct drm_device *dev,
  91. struct drm_file *dfile, struct drm_mode_fb_cmd2 *mode)
  92. {
  93. struct armada_gem_object *obj;
  94. struct armada_framebuffer *dfb;
  95. int ret;
  96. DRM_DEBUG_DRIVER("w%u h%u pf%08x f%u p%u,%u,%u\n",
  97. mode->width, mode->height, mode->pixel_format,
  98. mode->flags, mode->pitches[0], mode->pitches[1],
  99. mode->pitches[2]);
  100. /* We can only handle a single plane at the moment */
  101. if (drm_format_num_planes(mode->pixel_format) > 1 &&
  102. (mode->handles[0] != mode->handles[1] ||
  103. mode->handles[0] != mode->handles[2])) {
  104. ret = -EINVAL;
  105. goto err;
  106. }
  107. obj = armada_gem_object_lookup(dev, dfile, mode->handles[0]);
  108. if (!obj) {
  109. ret = -ENOENT;
  110. goto err;
  111. }
  112. if (obj->obj.import_attach && !obj->sgt) {
  113. ret = armada_gem_map_import(obj);
  114. if (ret)
  115. goto err_unref;
  116. }
  117. /* Framebuffer objects must have a valid device address for scanout */
  118. if (obj->dev_addr == DMA_ERROR_CODE) {
  119. ret = -EINVAL;
  120. goto err_unref;
  121. }
  122. dfb = armada_framebuffer_create(dev, mode, obj);
  123. if (IS_ERR(dfb)) {
  124. ret = PTR_ERR(dfb);
  125. goto err;
  126. }
  127. drm_gem_object_unreference_unlocked(&obj->obj);
  128. return &dfb->fb;
  129. err_unref:
  130. drm_gem_object_unreference_unlocked(&obj->obj);
  131. err:
  132. DRM_ERROR("failed to initialize framebuffer: %d\n", ret);
  133. return ERR_PTR(ret);
  134. }
  135. static void armada_output_poll_changed(struct drm_device *dev)
  136. {
  137. struct armada_private *priv = dev->dev_private;
  138. struct drm_fb_helper *fbh = priv->fbdev;
  139. if (fbh)
  140. drm_fb_helper_hotplug_event(fbh);
  141. }
  142. const struct drm_mode_config_funcs armada_drm_mode_config_funcs = {
  143. .fb_create = armada_fb_create,
  144. .output_poll_changed = armada_output_poll_changed,
  145. };