armada_fb.c 3.5 KB

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