vimc-common.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * vimc-common.h Virtual Media Controller Driver
  3. *
  4. * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #ifndef _VIMC_COMMON_H_
  18. #define _VIMC_COMMON_H_
  19. #include <linux/slab.h>
  20. #include <media/media-device.h>
  21. #include <media/v4l2-device.h>
  22. /* VIMC-specific controls */
  23. #define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
  24. #define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
  25. #define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
  26. #define VIMC_FRAME_MAX_WIDTH 4096
  27. #define VIMC_FRAME_MAX_HEIGHT 2160
  28. #define VIMC_FRAME_MIN_WIDTH 16
  29. #define VIMC_FRAME_MIN_HEIGHT 16
  30. #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
  31. /**
  32. * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
  33. *
  34. * @fmt: the pointer to struct v4l2_pix_format or
  35. * struct v4l2_mbus_framefmt
  36. *
  37. * Entities must check if colorimetry given by the userspace is valid, if not
  38. * then set them as DEFAULT
  39. */
  40. #define vimc_colorimetry_clamp(fmt) \
  41. do { \
  42. if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
  43. || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
  44. (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
  45. (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
  46. (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
  47. (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
  48. } \
  49. if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
  50. (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
  51. if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
  52. (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
  53. if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
  54. (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
  55. } while (0)
  56. /**
  57. * struct vimc_platform_data - platform data to components
  58. *
  59. * @entity_name: The name of the entity to be created
  60. *
  61. * Board setup code will often provide additional information using the device's
  62. * platform_data field to hold additional information.
  63. * When injecting a new platform_device in the component system the core needs
  64. * to provide to the corresponding submodules the name of the entity that should
  65. * be used when registering the subdevice in the Media Controller system.
  66. */
  67. struct vimc_platform_data {
  68. char entity_name[32];
  69. };
  70. /**
  71. * struct vimc_pix_map - maps media bus code with v4l2 pixel format
  72. *
  73. * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
  74. * @bbp: number of bytes each pixel occupies
  75. * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
  76. *
  77. * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
  78. * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
  79. */
  80. struct vimc_pix_map {
  81. unsigned int code;
  82. unsigned int bpp;
  83. u32 pixelformat;
  84. bool bayer;
  85. };
  86. /**
  87. * struct vimc_ent_device - core struct that represents a node in the topology
  88. *
  89. * @ent: the pointer to struct media_entity for the node
  90. * @pads: the list of pads of the node
  91. * @process_frame: callback send a frame to that node
  92. * @vdev_get_format: callback that returns the current format a pad, used
  93. * only when is_media_entity_v4l2_video_device(ent) returns
  94. * true
  95. *
  96. * Each node of the topology must create a vimc_ent_device struct. Depending on
  97. * the node it will be of an instance of v4l2_subdev or video_device struct
  98. * where both contains a struct media_entity.
  99. * Those structures should embedded the vimc_ent_device struct through
  100. * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
  101. * vimc_ent_device struct to be retrieved from the corresponding struct
  102. * media_entity
  103. */
  104. struct vimc_ent_device {
  105. struct media_entity *ent;
  106. struct media_pad *pads;
  107. void * (*process_frame)(struct vimc_ent_device *ved,
  108. const void *frame);
  109. void (*vdev_get_format)(struct vimc_ent_device *ved,
  110. struct v4l2_pix_format *fmt);
  111. };
  112. /**
  113. * vimc_pads_init - initialize pads
  114. *
  115. * @num_pads: number of pads to initialize
  116. * @pads_flags: flags to use in each pad
  117. *
  118. * Helper functions to allocate/initialize pads
  119. */
  120. struct media_pad *vimc_pads_init(u16 num_pads,
  121. const unsigned long *pads_flag);
  122. /**
  123. * vimc_pads_cleanup - free pads
  124. *
  125. * @pads: pointer to the pads
  126. *
  127. * Helper function to free the pads initialized with vimc_pads_init
  128. */
  129. static inline void vimc_pads_cleanup(struct media_pad *pads)
  130. {
  131. kfree(pads);
  132. }
  133. /**
  134. * vimc_pipeline_s_stream - start stream through the pipeline
  135. *
  136. * @ent: the pointer to struct media_entity for the node
  137. * @enable: 1 to start the stream and 0 to stop
  138. *
  139. * Helper function to call the s_stream of the subdevices connected
  140. * in all the sink pads of the entity
  141. */
  142. int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
  143. /**
  144. * vimc_pix_map_by_index - get vimc_pix_map struct by its index
  145. *
  146. * @i: index of the vimc_pix_map struct in vimc_pix_map_list
  147. */
  148. const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
  149. /**
  150. * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
  151. *
  152. * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
  153. */
  154. const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
  155. /**
  156. * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
  157. *
  158. * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
  159. */
  160. const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
  161. /**
  162. * vimc_ent_sd_register - initialize and register a subdev node
  163. *
  164. * @ved: the vimc_ent_device struct to be initialize
  165. * @sd: the v4l2_subdev struct to be initialize and registered
  166. * @v4l2_dev: the v4l2 device to register the v4l2_subdev
  167. * @name: name of the sub-device. Please notice that the name must be
  168. * unique.
  169. * @function: media entity function defined by MEDIA_ENT_F_* macros
  170. * @num_pads: number of pads to initialize
  171. * @pads_flag: flags to use in each pad
  172. * @sd_ops: pointer to &struct v4l2_subdev_ops.
  173. *
  174. * Helper function initialize and register the struct vimc_ent_device and struct
  175. * v4l2_subdev which represents a subdev node in the topology
  176. */
  177. int vimc_ent_sd_register(struct vimc_ent_device *ved,
  178. struct v4l2_subdev *sd,
  179. struct v4l2_device *v4l2_dev,
  180. const char *const name,
  181. u32 function,
  182. u16 num_pads,
  183. const unsigned long *pads_flag,
  184. const struct v4l2_subdev_ops *sd_ops);
  185. /**
  186. * vimc_ent_sd_unregister - cleanup and unregister a subdev node
  187. *
  188. * @ved: the vimc_ent_device struct to be cleaned up
  189. * @sd: the v4l2_subdev struct to be unregistered
  190. *
  191. * Helper function cleanup and unregister the struct vimc_ent_device and struct
  192. * v4l2_subdev which represents a subdev node in the topology
  193. */
  194. void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
  195. struct v4l2_subdev *sd);
  196. /**
  197. * vimc_link_validate - validates a media link
  198. *
  199. * @link: pointer to &struct media_link
  200. *
  201. * This function calls validates if a media link is valid for streaming.
  202. */
  203. int vimc_link_validate(struct media_link *link);
  204. #endif