vsp1_hgt.c 5.8 KB

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