vimc-common.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * vimc-common.h Virtual Media Controller Driver
  4. *
  5. * Copyright (C) 2015-2017 Helen Koike <[email protected]>
  6. */
  7. #ifndef _VIMC_COMMON_H_
  8. #define _VIMC_COMMON_H_
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <media/media-device.h>
  12. #include <media/v4l2-device.h>
  13. #define VIMC_PDEV_NAME "vimc"
  14. /* VIMC-specific controls */
  15. #define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
  16. #define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
  17. #define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
  18. #define VIMC_CID_MEAN_WIN_SIZE (VIMC_CID_VIMC_BASE + 1)
  19. #define VIMC_CID_OSD_TEXT_MODE (VIMC_CID_VIMC_BASE + 2)
  20. #define VIMC_FRAME_MAX_WIDTH 4096
  21. #define VIMC_FRAME_MAX_HEIGHT 2160
  22. #define VIMC_FRAME_MIN_WIDTH 16
  23. #define VIMC_FRAME_MIN_HEIGHT 16
  24. #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
  25. /* Source and sink pad checks */
  26. #define VIMC_IS_SRC(pad) (pad)
  27. #define VIMC_IS_SINK(pad) (!(pad))
  28. #define VIMC_PIX_FMT_MAX_CODES 8
  29. extern unsigned int vimc_allocator;
  30. enum vimc_allocator_type {
  31. VIMC_ALLOCATOR_VMALLOC = 0,
  32. VIMC_ALLOCATOR_DMA_CONTIG = 1,
  33. };
  34. /**
  35. * vimc_colorimetry_clamp - Adjust colorimetry parameters
  36. *
  37. * @fmt: the pointer to struct v4l2_pix_format or
  38. * struct v4l2_mbus_framefmt
  39. *
  40. * Entities must check if colorimetry given by the userspace is valid, if not
  41. * then set them as DEFAULT
  42. */
  43. #define vimc_colorimetry_clamp(fmt) \
  44. do { \
  45. if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
  46. || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
  47. (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
  48. (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
  49. (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
  50. (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
  51. } \
  52. if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
  53. (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
  54. if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
  55. (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
  56. if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
  57. (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
  58. } while (0)
  59. /**
  60. * struct vimc_pix_map - maps media bus code with v4l2 pixel format
  61. *
  62. * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
  63. * @bpp: number of bytes each pixel occupies
  64. * @pixelformat: pixel format defined by V4L2_PIX_FMT_* macros
  65. * @bayer: true if this is a bayer format
  66. *
  67. * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
  68. * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
  69. */
  70. struct vimc_pix_map {
  71. unsigned int code[VIMC_PIX_FMT_MAX_CODES];
  72. unsigned int bpp;
  73. u32 pixelformat;
  74. bool bayer;
  75. };
  76. /**
  77. * struct vimc_ent_device - core struct that represents an entity in the
  78. * topology
  79. *
  80. * @dev: a pointer of the device struct of the driver
  81. * @ent: the pointer to struct media_entity for the node
  82. * @process_frame: callback send a frame to that node
  83. * @vdev_get_format: callback that returns the current format a pad, used
  84. * only when is_media_entity_v4l2_video_device(ent) returns
  85. * true
  86. *
  87. * Each node of the topology must create a vimc_ent_device struct. Depending on
  88. * the node it will be of an instance of v4l2_subdev or video_device struct
  89. * where both contains a struct media_entity.
  90. * Those structures should embedded the vimc_ent_device struct through
  91. * v4l2_set_subdevdata() and video_set_drvdata() respectively, allowing the
  92. * vimc_ent_device struct to be retrieved from the corresponding struct
  93. * media_entity
  94. */
  95. struct vimc_ent_device {
  96. struct device *dev;
  97. struct media_entity *ent;
  98. void * (*process_frame)(struct vimc_ent_device *ved,
  99. const void *frame);
  100. void (*vdev_get_format)(struct vimc_ent_device *ved,
  101. struct v4l2_pix_format *fmt);
  102. };
  103. /**
  104. * struct vimc_device - main device for vimc driver
  105. *
  106. * @pipe_cfg: pointer to the vimc pipeline configuration structure
  107. * @ent_devs: array of vimc_ent_device pointers
  108. * @mdev: the associated media_device parent
  109. * @v4l2_dev: Internal v4l2 parent device
  110. */
  111. struct vimc_device {
  112. const struct vimc_pipeline_config *pipe_cfg;
  113. struct vimc_ent_device **ent_devs;
  114. struct media_device mdev;
  115. struct v4l2_device v4l2_dev;
  116. };
  117. /**
  118. * struct vimc_ent_type Structure for the callbacks of the entity types
  119. *
  120. *
  121. * @add: initializes and registers
  122. * vimc entity - called from vimc-core
  123. * @unregister: unregisters vimc entity - called from vimc-core
  124. * @release: releases vimc entity - called from the v4l2_dev
  125. * release callback
  126. */
  127. struct vimc_ent_type {
  128. struct vimc_ent_device *(*add)(struct vimc_device *vimc,
  129. const char *vcfg_name);
  130. void (*unregister)(struct vimc_ent_device *ved);
  131. void (*release)(struct vimc_ent_device *ved);
  132. };
  133. /**
  134. * struct vimc_ent_config Structure which describes individual
  135. * configuration for each entity
  136. *
  137. * @name: entity name
  138. * @type: contain the callbacks of this entity type
  139. *
  140. */
  141. struct vimc_ent_config {
  142. const char *name;
  143. struct vimc_ent_type *type;
  144. };
  145. /**
  146. * vimc_is_source - returns true if the entity has only source pads
  147. *
  148. * @ent: pointer to &struct media_entity
  149. *
  150. */
  151. bool vimc_is_source(struct media_entity *ent);
  152. extern struct vimc_ent_type vimc_sensor_type;
  153. extern struct vimc_ent_type vimc_debayer_type;
  154. extern struct vimc_ent_type vimc_scaler_type;
  155. extern struct vimc_ent_type vimc_capture_type;
  156. extern struct vimc_ent_type vimc_lens_type;
  157. /**
  158. * vimc_pix_map_by_index - get vimc_pix_map struct by its index
  159. *
  160. * @i: index of the vimc_pix_map struct in vimc_pix_map_list
  161. */
  162. const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
  163. /**
  164. * vimc_mbus_code_by_index - get mbus code by its index
  165. *
  166. * @index: index of the mbus code in vimc_pix_map_list
  167. *
  168. * Returns 0 if no mbus code is found for the given index.
  169. */
  170. u32 vimc_mbus_code_by_index(unsigned int index);
  171. /**
  172. * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
  173. *
  174. * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
  175. */
  176. const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
  177. /**
  178. * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
  179. *
  180. * @pixelformat: pixel format defined by V4L2_PIX_FMT_* macros
  181. */
  182. const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
  183. /**
  184. * vimc_ent_sd_register - initialize and register a subdev node
  185. *
  186. * @ved: the vimc_ent_device struct to be initialize
  187. * @sd: the v4l2_subdev struct to be initialize and registered
  188. * @v4l2_dev: the v4l2 device to register the v4l2_subdev
  189. * @name: name of the sub-device. Please notice that the name must be
  190. * unique.
  191. * @function: media entity function defined by MEDIA_ENT_F_* macros
  192. * @num_pads: number of pads to initialize
  193. * @pads: the array of pads of the entity, the caller should set the
  194. * flags of the pads
  195. * @sd_ops: pointer to &struct v4l2_subdev_ops.
  196. *
  197. * Helper function initialize and register the struct vimc_ent_device and struct
  198. * v4l2_subdev which represents a subdev node in the topology
  199. */
  200. int vimc_ent_sd_register(struct vimc_ent_device *ved,
  201. struct v4l2_subdev *sd,
  202. struct v4l2_device *v4l2_dev,
  203. const char *const name,
  204. u32 function,
  205. u16 num_pads,
  206. struct media_pad *pads,
  207. const struct v4l2_subdev_ops *sd_ops);
  208. /**
  209. * vimc_vdev_link_validate - validates a media link
  210. *
  211. * @link: pointer to &struct media_link
  212. *
  213. * This function calls validates if a media link is valid for streaming.
  214. */
  215. int vimc_vdev_link_validate(struct media_link *link);
  216. #endif