vsp1_hgt.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vsp1_hgt.c -- R-Car VSP1 Histogram Generator 2D
  4. *
  5. * Copyright (C) 2016 Renesas Electronics Corporation
  6. *
  7. * Contact: Niklas Söderlund (niklas.soderlund@ragnatech.se)
  8. */
  9. #include <linux/device.h>
  10. #include <linux/gfp.h>
  11. #include <media/v4l2-subdev.h>
  12. #include <media/videobuf2-vmalloc.h>
  13. #include "vsp1.h"
  14. #include "vsp1_dl.h"
  15. #include "vsp1_hgt.h"
  16. #define HGT_DATA_SIZE ((2 + 6 * 32) * 4)
  17. /* -----------------------------------------------------------------------------
  18. * Device Access
  19. */
  20. static inline u32 vsp1_hgt_read(struct vsp1_hgt *hgt, u32 reg)
  21. {
  22. return vsp1_read(hgt->histo.entity.vsp1, reg);
  23. }
  24. static inline void vsp1_hgt_write(struct vsp1_hgt *hgt,
  25. struct vsp1_dl_body *dlb, u32 reg, u32 data)
  26. {
  27. vsp1_dl_body_write(dlb, reg, data);
  28. }
  29. /* -----------------------------------------------------------------------------
  30. * Frame End Handler
  31. */
  32. void vsp1_hgt_frame_end(struct vsp1_entity *entity)
  33. {
  34. struct vsp1_hgt *hgt = to_hgt(&entity->subdev);
  35. struct vsp1_histogram_buffer *buf;
  36. unsigned int m;
  37. unsigned int n;
  38. u32 *data;
  39. buf = vsp1_histogram_buffer_get(&hgt->histo);
  40. if (!buf)
  41. return;
  42. data = buf->addr;
  43. *data++ = vsp1_hgt_read(hgt, VI6_HGT_MAXMIN);
  44. *data++ = vsp1_hgt_read(hgt, VI6_HGT_SUM);
  45. for (m = 0; m < 6; ++m)
  46. for (n = 0; n < 32; ++n)
  47. *data++ = vsp1_hgt_read(hgt, VI6_HGT_HISTO(m, n));
  48. vsp1_histogram_buffer_complete(&hgt->histo, buf, HGT_DATA_SIZE);
  49. }
  50. /* -----------------------------------------------------------------------------
  51. * Controls
  52. */
  53. #define V4L2_CID_VSP1_HGT_HUE_AREAS (V4L2_CID_USER_BASE | 0x1001)
  54. static int hgt_hue_areas_try_ctrl(struct v4l2_ctrl *ctrl)
  55. {
  56. const u8 *values = ctrl->p_new.p_u8;
  57. unsigned int i;
  58. /*
  59. * The hardware has constraints on the hue area boundaries beyond the
  60. * control min, max and step. The values must match one of the following
  61. * expressions.
  62. *
  63. * 0L <= 0U <= 1L <= 1U <= 2L <= 2U <= 3L <= 3U <= 4L <= 4U <= 5L <= 5U
  64. * 0U <= 1L <= 1U <= 2L <= 2U <= 3L <= 3U <= 4L <= 4U <= 5L <= 5U <= 0L
  65. *
  66. * Start by verifying the common part...
  67. */
  68. for (i = 1; i < (HGT_NUM_HUE_AREAS * 2) - 1; ++i) {
  69. if (values[i] > values[i+1])
  70. return -EINVAL;
  71. }
  72. /* ... and handle 0L separately. */
  73. if (values[0] > values[1] && values[11] > values[0])
  74. return -EINVAL;
  75. return 0;
  76. }
  77. static int hgt_hue_areas_s_ctrl(struct v4l2_ctrl *ctrl)
  78. {
  79. struct vsp1_hgt *hgt = container_of(ctrl->handler, struct vsp1_hgt,
  80. ctrls);
  81. memcpy(hgt->hue_areas, ctrl->p_new.p_u8, sizeof(hgt->hue_areas));
  82. return 0;
  83. }
  84. static const struct v4l2_ctrl_ops hgt_hue_areas_ctrl_ops = {
  85. .try_ctrl = hgt_hue_areas_try_ctrl,
  86. .s_ctrl = hgt_hue_areas_s_ctrl,
  87. };
  88. static const struct v4l2_ctrl_config hgt_hue_areas = {
  89. .ops = &hgt_hue_areas_ctrl_ops,
  90. .id = V4L2_CID_VSP1_HGT_HUE_AREAS,
  91. .name = "Boundary Values for Hue Area",
  92. .type = V4L2_CTRL_TYPE_U8,
  93. .min = 0,
  94. .max = 255,
  95. .def = 0,
  96. .step = 1,
  97. .dims = { 12 },
  98. };
  99. /* -----------------------------------------------------------------------------
  100. * VSP1 Entity Operations
  101. */
  102. static void hgt_configure_stream(struct vsp1_entity *entity,
  103. struct vsp1_pipeline *pipe,
  104. struct vsp1_dl_body *dlb)
  105. {
  106. struct vsp1_hgt *hgt = to_hgt(&entity->subdev);
  107. struct v4l2_rect *compose;
  108. struct v4l2_rect *crop;
  109. unsigned int hratio;
  110. unsigned int vratio;
  111. u8 lower;
  112. u8 upper;
  113. unsigned int i;
  114. crop = vsp1_entity_get_pad_selection(entity, entity->config,
  115. HISTO_PAD_SINK, V4L2_SEL_TGT_CROP);
  116. compose = vsp1_entity_get_pad_selection(entity, entity->config,
  117. HISTO_PAD_SINK,
  118. V4L2_SEL_TGT_COMPOSE);
  119. vsp1_hgt_write(hgt, dlb, VI6_HGT_REGRST, VI6_HGT_REGRST_RCLEA);
  120. vsp1_hgt_write(hgt, dlb, VI6_HGT_OFFSET,
  121. (crop->left << VI6_HGT_OFFSET_HOFFSET_SHIFT) |
  122. (crop->top << VI6_HGT_OFFSET_VOFFSET_SHIFT));
  123. vsp1_hgt_write(hgt, dlb, VI6_HGT_SIZE,
  124. (crop->width << VI6_HGT_SIZE_HSIZE_SHIFT) |
  125. (crop->height << VI6_HGT_SIZE_VSIZE_SHIFT));
  126. mutex_lock(hgt->ctrls.lock);
  127. for (i = 0; i < HGT_NUM_HUE_AREAS; ++i) {
  128. lower = hgt->hue_areas[i*2 + 0];
  129. upper = hgt->hue_areas[i*2 + 1];
  130. vsp1_hgt_write(hgt, dlb, VI6_HGT_HUE_AREA(i),
  131. (lower << VI6_HGT_HUE_AREA_LOWER_SHIFT) |
  132. (upper << VI6_HGT_HUE_AREA_UPPER_SHIFT));
  133. }
  134. mutex_unlock(hgt->ctrls.lock);
  135. hratio = crop->width * 2 / compose->width / 3;
  136. vratio = crop->height * 2 / compose->height / 3;
  137. vsp1_hgt_write(hgt, dlb, VI6_HGT_MODE,
  138. (hratio << VI6_HGT_MODE_HRATIO_SHIFT) |
  139. (vratio << VI6_HGT_MODE_VRATIO_SHIFT));
  140. }
  141. static const struct vsp1_entity_operations hgt_entity_ops = {
  142. .configure_stream = hgt_configure_stream,
  143. .destroy = vsp1_histogram_destroy,
  144. };
  145. /* -----------------------------------------------------------------------------
  146. * Initialization and Cleanup
  147. */
  148. static const unsigned int hgt_mbus_formats[] = {
  149. MEDIA_BUS_FMT_AHSV8888_1X32,
  150. };
  151. struct vsp1_hgt *vsp1_hgt_create(struct vsp1_device *vsp1)
  152. {
  153. struct vsp1_hgt *hgt;
  154. int ret;
  155. hgt = devm_kzalloc(vsp1->dev, sizeof(*hgt), GFP_KERNEL);
  156. if (hgt == NULL)
  157. return ERR_PTR(-ENOMEM);
  158. /* Initialize the control handler. */
  159. v4l2_ctrl_handler_init(&hgt->ctrls, 1);
  160. v4l2_ctrl_new_custom(&hgt->ctrls, &hgt_hue_areas, NULL);
  161. hgt->histo.entity.subdev.ctrl_handler = &hgt->ctrls;
  162. /* Initialize the video device and queue for statistics data. */
  163. ret = vsp1_histogram_init(vsp1, &hgt->histo, VSP1_ENTITY_HGT, "hgt",
  164. &hgt_entity_ops, hgt_mbus_formats,
  165. ARRAY_SIZE(hgt_mbus_formats),
  166. HGT_DATA_SIZE, V4L2_META_FMT_VSP1_HGT);
  167. if (ret < 0) {
  168. vsp1_entity_destroy(&hgt->histo.entity);
  169. return ERR_PTR(ret);
  170. }
  171. v4l2_ctrl_handler_setup(&hgt->ctrls);
  172. return hgt;
  173. }