vsp1_lif.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
  4. *
  5. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/device.h>
  10. #include <linux/gfp.h>
  11. #include <media/v4l2-subdev.h>
  12. #include "vsp1.h"
  13. #include "vsp1_dl.h"
  14. #include "vsp1_lif.h"
  15. #define LIF_MIN_SIZE 2U
  16. #define LIF_MAX_SIZE 8190U
  17. /* -----------------------------------------------------------------------------
  18. * Device Access
  19. */
  20. static inline void vsp1_lif_write(struct vsp1_lif *lif,
  21. struct vsp1_dl_body *dlb, u32 reg, u32 data)
  22. {
  23. vsp1_dl_body_write(dlb, reg + lif->entity.index * VI6_LIF_OFFSET,
  24. data);
  25. }
  26. /* -----------------------------------------------------------------------------
  27. * V4L2 Subdevice Operations
  28. */
  29. static const unsigned int lif_codes[] = {
  30. MEDIA_BUS_FMT_ARGB8888_1X32,
  31. MEDIA_BUS_FMT_AYUV8_1X32,
  32. };
  33. static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
  34. struct v4l2_subdev_pad_config *cfg,
  35. struct v4l2_subdev_mbus_code_enum *code)
  36. {
  37. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, lif_codes,
  38. ARRAY_SIZE(lif_codes));
  39. }
  40. static int lif_enum_frame_size(struct v4l2_subdev *subdev,
  41. struct v4l2_subdev_pad_config *cfg,
  42. struct v4l2_subdev_frame_size_enum *fse)
  43. {
  44. return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LIF_MIN_SIZE,
  45. LIF_MIN_SIZE, LIF_MAX_SIZE,
  46. LIF_MAX_SIZE);
  47. }
  48. static int lif_set_format(struct v4l2_subdev *subdev,
  49. struct v4l2_subdev_pad_config *cfg,
  50. struct v4l2_subdev_format *fmt)
  51. {
  52. return vsp1_subdev_set_pad_format(subdev, cfg, fmt, lif_codes,
  53. ARRAY_SIZE(lif_codes),
  54. LIF_MIN_SIZE, LIF_MIN_SIZE,
  55. LIF_MAX_SIZE, LIF_MAX_SIZE);
  56. }
  57. static const struct v4l2_subdev_pad_ops lif_pad_ops = {
  58. .init_cfg = vsp1_entity_init_cfg,
  59. .enum_mbus_code = lif_enum_mbus_code,
  60. .enum_frame_size = lif_enum_frame_size,
  61. .get_fmt = vsp1_subdev_get_pad_format,
  62. .set_fmt = lif_set_format,
  63. };
  64. static const struct v4l2_subdev_ops lif_ops = {
  65. .pad = &lif_pad_ops,
  66. };
  67. /* -----------------------------------------------------------------------------
  68. * VSP1 Entity Operations
  69. */
  70. static void lif_configure_stream(struct vsp1_entity *entity,
  71. struct vsp1_pipeline *pipe,
  72. struct vsp1_dl_body *dlb)
  73. {
  74. const struct v4l2_mbus_framefmt *format;
  75. struct vsp1_lif *lif = to_lif(&entity->subdev);
  76. unsigned int hbth = 1300;
  77. unsigned int obth = 400;
  78. unsigned int lbth = 200;
  79. format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
  80. LIF_PAD_SOURCE);
  81. obth = min(obth, (format->width + 1) / 2 * format->height - 4);
  82. vsp1_lif_write(lif, dlb, VI6_LIF_CSBTH,
  83. (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
  84. (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
  85. vsp1_lif_write(lif, dlb, VI6_LIF_CTRL,
  86. (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
  87. (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
  88. VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
  89. /*
  90. * On R-Car V3M the LIF0 buffer attribute register has to be set to a
  91. * non-default value to guarantee proper operation (otherwise artifacts
  92. * may appear on the output). The value required by the manual is not
  93. * explained but is likely a buffer size or threshold.
  94. */
  95. if ((entity->vsp1->version & VI6_IP_VERSION_MASK) ==
  96. (VI6_IP_VERSION_MODEL_VSPD_V3 | VI6_IP_VERSION_SOC_V3M))
  97. vsp1_lif_write(lif, dlb, VI6_LIF_LBA,
  98. VI6_LIF_LBA_LBA0 |
  99. (1536 << VI6_LIF_LBA_LBA1_SHIFT));
  100. }
  101. static const struct vsp1_entity_operations lif_entity_ops = {
  102. .configure_stream = lif_configure_stream,
  103. };
  104. /* -----------------------------------------------------------------------------
  105. * Initialization and Cleanup
  106. */
  107. struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1, unsigned int index)
  108. {
  109. struct vsp1_lif *lif;
  110. int ret;
  111. lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
  112. if (lif == NULL)
  113. return ERR_PTR(-ENOMEM);
  114. lif->entity.ops = &lif_entity_ops;
  115. lif->entity.type = VSP1_ENTITY_LIF;
  116. lif->entity.index = index;
  117. /*
  118. * The LIF is never exposed to userspace, but media entity registration
  119. * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
  120. * avoid triggering a WARN_ON(), the value won't be seen anywhere.
  121. */
  122. ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
  123. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  124. if (ret < 0)
  125. return ERR_PTR(ret);
  126. return lif;
  127. }