armada_fb.c 4.1 KB

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