vsp1_lif.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
  3. *
  4. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/gfp.h>
  15. #include <media/v4l2-subdev.h>
  16. #include "vsp1.h"
  17. #include "vsp1_dl.h"
  18. #include "vsp1_lif.h"
  19. #define LIF_MIN_SIZE 2U
  20. #define LIF_MAX_SIZE 8190U
  21. /* -----------------------------------------------------------------------------
  22. * Device Access
  23. */
  24. static inline void vsp1_lif_write(struct vsp1_lif *lif, struct vsp1_dl_list *dl,
  25. u32 reg, u32 data)
  26. {
  27. vsp1_dl_list_write(dl, reg + lif->entity.index * VI6_LIF_OFFSET, data);
  28. }
  29. /* -----------------------------------------------------------------------------
  30. * V4L2 Subdevice Operations
  31. */
  32. static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
  33. struct v4l2_subdev_pad_config *cfg,
  34. struct v4l2_subdev_mbus_code_enum *code)
  35. {
  36. static const unsigned int codes[] = {
  37. MEDIA_BUS_FMT_ARGB8888_1X32,
  38. MEDIA_BUS_FMT_AYUV8_1X32,
  39. };
  40. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
  41. ARRAY_SIZE(codes));
  42. }
  43. static int lif_enum_frame_size(struct v4l2_subdev *subdev,
  44. struct v4l2_subdev_pad_config *cfg,
  45. struct v4l2_subdev_frame_size_enum *fse)
  46. {
  47. return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LIF_MIN_SIZE,
  48. LIF_MIN_SIZE, LIF_MAX_SIZE,
  49. LIF_MAX_SIZE);
  50. }
  51. static int lif_set_format(struct v4l2_subdev *subdev,
  52. struct v4l2_subdev_pad_config *cfg,
  53. struct v4l2_subdev_format *fmt)
  54. {
  55. struct vsp1_lif *lif = to_lif(subdev);
  56. struct v4l2_subdev_pad_config *config;
  57. struct v4l2_mbus_framefmt *format;
  58. int ret = 0;
  59. mutex_lock(&lif->entity.lock);
  60. config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
  61. if (!config) {
  62. ret = -EINVAL;
  63. goto done;
  64. }
  65. /* Default to YUV if the requested format is not supported. */
  66. if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  67. fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
  68. fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
  69. format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad);
  70. if (fmt->pad == LIF_PAD_SOURCE) {
  71. /*
  72. * The LIF source format is always identical to its sink
  73. * format.
  74. */
  75. fmt->format = *format;
  76. goto done;
  77. }
  78. format->code = fmt->format.code;
  79. format->width = clamp_t(unsigned int, fmt->format.width,
  80. LIF_MIN_SIZE, LIF_MAX_SIZE);
  81. format->height = clamp_t(unsigned int, fmt->format.height,
  82. LIF_MIN_SIZE, LIF_MAX_SIZE);
  83. format->field = V4L2_FIELD_NONE;
  84. format->colorspace = V4L2_COLORSPACE_SRGB;
  85. fmt->format = *format;
  86. /* Propagate the format to the source pad. */
  87. format = vsp1_entity_get_pad_format(&lif->entity, config,
  88. LIF_PAD_SOURCE);
  89. *format = fmt->format;
  90. done:
  91. mutex_unlock(&lif->entity.lock);
  92. return ret;
  93. }
  94. static const struct v4l2_subdev_pad_ops lif_pad_ops = {
  95. .init_cfg = vsp1_entity_init_cfg,
  96. .enum_mbus_code = lif_enum_mbus_code,
  97. .enum_frame_size = lif_enum_frame_size,
  98. .get_fmt = vsp1_subdev_get_pad_format,
  99. .set_fmt = lif_set_format,
  100. };
  101. static const struct v4l2_subdev_ops lif_ops = {
  102. .pad = &lif_pad_ops,
  103. };
  104. /* -----------------------------------------------------------------------------
  105. * VSP1 Entity Operations
  106. */
  107. static void lif_configure(struct vsp1_entity *entity,
  108. struct vsp1_pipeline *pipe,
  109. struct vsp1_dl_list *dl,
  110. enum vsp1_entity_params params)
  111. {
  112. const struct v4l2_mbus_framefmt *format;
  113. struct vsp1_lif *lif = to_lif(&entity->subdev);
  114. unsigned int hbth = 1300;
  115. unsigned int obth = 400;
  116. unsigned int lbth = 200;
  117. if (params != VSP1_ENTITY_PARAMS_INIT)
  118. return;
  119. format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
  120. LIF_PAD_SOURCE);
  121. obth = min(obth, (format->width + 1) / 2 * format->height - 4);
  122. vsp1_lif_write(lif, dl, VI6_LIF_CSBTH,
  123. (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
  124. (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
  125. vsp1_lif_write(lif, dl, VI6_LIF_CTRL,
  126. (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
  127. (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
  128. VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
  129. }
  130. static const struct vsp1_entity_operations lif_entity_ops = {
  131. .configure = lif_configure,
  132. };
  133. /* -----------------------------------------------------------------------------
  134. * Initialization and Cleanup
  135. */
  136. struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1, unsigned int index)
  137. {
  138. struct vsp1_lif *lif;
  139. int ret;
  140. lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
  141. if (lif == NULL)
  142. return ERR_PTR(-ENOMEM);
  143. lif->entity.ops = &lif_entity_ops;
  144. lif->entity.type = VSP1_ENTITY_LIF;
  145. lif->entity.index = index;
  146. /*
  147. * The LIF is never exposed to userspace, but media entity registration
  148. * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
  149. * avoid triggering a WARN_ON(), the value won't be seen anywhere.
  150. */
  151. ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
  152. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  153. if (ret < 0)
  154. return ERR_PTR(ret);
  155. return lif;
  156. }