rcar-vin.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Driver for Renesas R-Car VIN
  4. *
  5. * Copyright (C) 2016 Renesas Electronics Corp.
  6. * Copyright (C) 2011-2013 Renesas Solutions Corp.
  7. * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
  8. * Copyright (C) 2008 Magnus Damm
  9. *
  10. * Based on the soc-camera rcar_vin driver
  11. */
  12. #ifndef __RCAR_VIN__
  13. #define __RCAR_VIN__
  14. #include <linux/kref.h>
  15. #include <media/v4l2-async.h>
  16. #include <media/v4l2-ctrls.h>
  17. #include <media/v4l2-dev.h>
  18. #include <media/v4l2-device.h>
  19. #include <media/videobuf2-v4l2.h>
  20. /* Number of HW buffers */
  21. #define HW_BUFFER_NUM 3
  22. /* Address alignment mask for HW buffers */
  23. #define HW_BUFFER_MASK 0x7f
  24. /* Max number on VIN instances that can be in a system */
  25. #define RCAR_VIN_NUM 8
  26. struct rvin_group;
  27. enum model_id {
  28. RCAR_H1,
  29. RCAR_M1,
  30. RCAR_GEN2,
  31. RCAR_GEN3,
  32. };
  33. enum rvin_csi_id {
  34. RVIN_CSI20,
  35. RVIN_CSI21,
  36. RVIN_CSI40,
  37. RVIN_CSI41,
  38. RVIN_CSI_MAX,
  39. };
  40. /**
  41. * STOPPED - No operation in progress
  42. * STARTING - Capture starting up
  43. * RUNNING - Operation in progress have buffers
  44. * STOPPING - Stopping operation
  45. */
  46. enum rvin_dma_state {
  47. STOPPED = 0,
  48. STARTING,
  49. RUNNING,
  50. STOPPING,
  51. };
  52. /**
  53. * struct rvin_video_format - Data format stored in memory
  54. * @fourcc: Pixelformat
  55. * @bpp: Bytes per pixel
  56. */
  57. struct rvin_video_format {
  58. u32 fourcc;
  59. u8 bpp;
  60. };
  61. /**
  62. * struct rvin_parallel_entity - Parallel video input endpoint descriptor
  63. * @asd: sub-device descriptor for async framework
  64. * @subdev: subdevice matched using async framework
  65. * @mbus_type: media bus type
  66. * @mbus_flags: media bus configuration flags
  67. * @source_pad: source pad of remote subdevice
  68. * @sink_pad: sink pad of remote subdevice
  69. *
  70. */
  71. struct rvin_parallel_entity {
  72. struct v4l2_async_subdev asd;
  73. struct v4l2_subdev *subdev;
  74. enum v4l2_mbus_type mbus_type;
  75. unsigned int mbus_flags;
  76. unsigned int source_pad;
  77. unsigned int sink_pad;
  78. };
  79. /**
  80. * struct rvin_group_route - describes a route from a channel of a
  81. * CSI-2 receiver to a VIN
  82. *
  83. * @csi: CSI-2 receiver ID.
  84. * @channel: Output channel of the CSI-2 receiver.
  85. * @vin: VIN ID.
  86. * @mask: Bitmask of the different CHSEL register values that
  87. * allow for a route from @csi + @chan to @vin.
  88. *
  89. * .. note::
  90. * Each R-Car CSI-2 receiver has four output channels facing the VIN
  91. * devices, each channel can carry one CSI-2 Virtual Channel (VC).
  92. * There is no correlation between channel number and CSI-2 VC. It's
  93. * up to the CSI-2 receiver driver to configure which VC is output
  94. * on which channel, the VIN devices only care about output channels.
  95. *
  96. * There are in some cases multiple CHSEL register settings which would
  97. * allow for the same route from @csi + @channel to @vin. For example
  98. * on R-Car H3 both the CHSEL values 0 and 3 allow for a route from
  99. * CSI40/VC0 to VIN0. All possible CHSEL values for a route need to be
  100. * recorded as a bitmask in @mask, in this example bit 0 and 3 should
  101. * be set.
  102. */
  103. struct rvin_group_route {
  104. enum rvin_csi_id csi;
  105. unsigned int channel;
  106. unsigned int vin;
  107. unsigned int mask;
  108. };
  109. /**
  110. * struct rvin_info - Information about the particular VIN implementation
  111. * @model: VIN model
  112. * @use_mc: use media controller instead of controlling subdevice
  113. * @max_width: max input width the VIN supports
  114. * @max_height: max input height the VIN supports
  115. * @routes: list of possible routes from the CSI-2 recivers to
  116. * all VINs. The list mush be NULL terminated.
  117. */
  118. struct rvin_info {
  119. enum model_id model;
  120. bool use_mc;
  121. unsigned int max_width;
  122. unsigned int max_height;
  123. const struct rvin_group_route *routes;
  124. };
  125. /**
  126. * struct rvin_dev - Renesas VIN device structure
  127. * @dev: (OF) device
  128. * @base: device I/O register space remapped to virtual memory
  129. * @info: info about VIN instance
  130. *
  131. * @vdev: V4L2 video device associated with VIN
  132. * @v4l2_dev: V4L2 device
  133. * @ctrl_handler: V4L2 control handler
  134. * @notifier: V4L2 asynchronous subdevs notifier
  135. *
  136. * @parallel: parallel input subdevice descriptor
  137. *
  138. * @group: Gen3 CSI group
  139. * @id: Gen3 group id for this VIN
  140. * @pad: media pad for the video device entity
  141. *
  142. * @lock: protects @queue
  143. * @queue: vb2 buffers queue
  144. * @scratch: cpu address for scratch buffer
  145. * @scratch_phys: physical address of the scratch buffer
  146. *
  147. * @qlock: protects @queue_buf, @buf_list, @sequence
  148. * @state
  149. * @queue_buf: Keeps track of buffers given to HW slot
  150. * @buf_list: list of queued buffers
  151. * @sequence: V4L2 buffers sequence number
  152. * @state: keeps track of operation state
  153. *
  154. * @is_csi: flag to mark the VIN as using a CSI-2 subdevice
  155. *
  156. * @mbus_code: media bus format code
  157. * @format: active V4L2 pixel format
  158. *
  159. * @crop: active cropping
  160. * @compose: active composing
  161. * @source: active size of the video source
  162. * @std: active video standard of the video source
  163. */
  164. struct rvin_dev {
  165. struct device *dev;
  166. void __iomem *base;
  167. const struct rvin_info *info;
  168. struct video_device vdev;
  169. struct v4l2_device v4l2_dev;
  170. struct v4l2_ctrl_handler ctrl_handler;
  171. struct v4l2_async_notifier notifier;
  172. struct rvin_parallel_entity *parallel;
  173. struct rvin_group *group;
  174. unsigned int id;
  175. struct media_pad pad;
  176. struct mutex lock;
  177. struct vb2_queue queue;
  178. void *scratch;
  179. dma_addr_t scratch_phys;
  180. spinlock_t qlock;
  181. struct vb2_v4l2_buffer *queue_buf[HW_BUFFER_NUM];
  182. struct list_head buf_list;
  183. unsigned int sequence;
  184. enum rvin_dma_state state;
  185. bool is_csi;
  186. u32 mbus_code;
  187. struct v4l2_pix_format format;
  188. struct v4l2_rect crop;
  189. struct v4l2_rect compose;
  190. struct v4l2_rect source;
  191. v4l2_std_id std;
  192. };
  193. #define vin_to_source(vin) ((vin)->parallel->subdev)
  194. /* Debug */
  195. #define vin_dbg(d, fmt, arg...) dev_dbg(d->dev, fmt, ##arg)
  196. #define vin_info(d, fmt, arg...) dev_info(d->dev, fmt, ##arg)
  197. #define vin_warn(d, fmt, arg...) dev_warn(d->dev, fmt, ##arg)
  198. #define vin_err(d, fmt, arg...) dev_err(d->dev, fmt, ##arg)
  199. /**
  200. * struct rvin_group - VIN CSI2 group information
  201. * @refcount: number of VIN instances using the group
  202. *
  203. * @mdev: media device which represents the group
  204. *
  205. * @lock: protects the count, notifier, vin and csi members
  206. * @count: number of enabled VIN instances found in DT
  207. * @notifier: group notifier for CSI-2 async subdevices
  208. * @vin: VIN instances which are part of the group
  209. * @csi: array of pairs of fwnode and subdev pointers
  210. * to all CSI-2 subdevices.
  211. */
  212. struct rvin_group {
  213. struct kref refcount;
  214. struct media_device mdev;
  215. struct mutex lock;
  216. unsigned int count;
  217. struct v4l2_async_notifier notifier;
  218. struct rvin_dev *vin[RCAR_VIN_NUM];
  219. struct {
  220. struct fwnode_handle *fwnode;
  221. struct v4l2_subdev *subdev;
  222. } csi[RVIN_CSI_MAX];
  223. };
  224. int rvin_dma_register(struct rvin_dev *vin, int irq);
  225. void rvin_dma_unregister(struct rvin_dev *vin);
  226. int rvin_v4l2_register(struct rvin_dev *vin);
  227. void rvin_v4l2_unregister(struct rvin_dev *vin);
  228. const struct rvin_video_format *rvin_format_from_pixel(u32 pixelformat);
  229. /* Cropping, composing and scaling */
  230. void rvin_crop_scale_comp(struct rvin_dev *vin);
  231. int rvin_set_channel_routing(struct rvin_dev *vin, u8 chsel);
  232. #endif