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 13 #ifndef __RCAR_VIN__ 14 #define __RCAR_VIN__ 15 16 #include <linux/kref.h> 17 18 #include <media/v4l2-async.h> 19 #include <media/v4l2-ctrls.h> 20 #include <media/v4l2-dev.h> 21 #include <media/v4l2-device.h> 22 #include <media/v4l2-fwnode.h> 23 #include <media/videobuf2-v4l2.h> 24 25 /* Number of HW buffers */ 26 #define HW_BUFFER_NUM 3 27 28 /* Address alignment mask for HW buffers */ 29 #define HW_BUFFER_MASK 0x7f 30 31 /* Max number on VIN instances that can be in a system */ 32 #define RCAR_VIN_NUM 32 33 34 struct rvin_group; 35 36 enum model_id { 37 RCAR_H1, 38 RCAR_M1, 39 RCAR_GEN2, 40 RCAR_GEN3, 41 }; 42 43 enum rvin_csi_id { 44 RVIN_CSI20, 45 RVIN_CSI21, 46 RVIN_CSI40, 47 RVIN_CSI41, 48 RVIN_CSI_MAX, 49 }; 50 51 enum rvin_isp_id { 52 RVIN_ISP0, 53 RVIN_ISP1, 54 RVIN_ISP2, 55 RVIN_ISP4, 56 RVIN_ISP_MAX, 57 }; 58 59 #define RVIN_REMOTES_MAX \ 60 (((unsigned int)RVIN_CSI_MAX) > ((unsigned int)RVIN_ISP_MAX) ? \ 61 RVIN_CSI_MAX : RVIN_ISP_MAX) 62 63 /** 64 * enum rvin_dma_state - DMA states 65 * @STOPPED: No operation in progress 66 * @STARTING: Capture starting up 67 * @RUNNING: Operation in progress have buffers 68 * @STOPPING: Stopping operation 69 * @SUSPENDED: Capture is suspended 70 */ 71 enum rvin_dma_state { 72 STOPPED = 0, 73 STARTING, 74 RUNNING, 75 STOPPING, 76 SUSPENDED, 77 }; 78 79 /** 80 * enum rvin_buffer_type 81 * 82 * Describes how a buffer is given to the hardware. To be able 83 * to capture SEQ_TB/BT it's needed to capture to the same vb2 84 * buffer twice so the type of buffer needs to be kept. 85 * 86 * @FULL: One capture fills the whole vb2 buffer 87 * @HALF_TOP: One capture fills the top half of the vb2 buffer 88 * @HALF_BOTTOM: One capture fills the bottom half of the vb2 buffer 89 */ 90 enum rvin_buffer_type { 91 FULL, 92 HALF_TOP, 93 HALF_BOTTOM, 94 }; 95 96 /** 97 * struct rvin_video_format - Data format stored in memory 98 * @fourcc: Pixelformat 99 * @bpp: Bytes per pixel 100 */ 101 struct rvin_video_format { 102 u32 fourcc; 103 u8 bpp; 104 }; 105 106 /** 107 * struct rvin_parallel_entity - Parallel video input endpoint descriptor 108 * @asd: sub-device descriptor for async framework 109 * @subdev: subdevice matched using async framework 110 * @mbus_type: media bus type 111 * @bus: media bus parallel configuration 112 * @source_pad: source pad of remote subdevice 113 * @sink_pad: sink pad of remote subdevice 114 * 115 */ 116 struct rvin_parallel_entity { 117 struct v4l2_async_subdev *asd; 118 struct v4l2_subdev *subdev; 119 120 enum v4l2_mbus_type mbus_type; 121 struct v4l2_fwnode_bus_parallel bus; 122 123 unsigned int source_pad; 124 unsigned int sink_pad; 125 }; 126 127 /** 128 * struct rvin_group_route - describes a route from a channel of a 129 * CSI-2 receiver to a VIN 130 * 131 * @csi: CSI-2 receiver ID. 132 * @channel: Output channel of the CSI-2 receiver. 133 * @vin: VIN ID. 134 * @mask: Bitmask of the different CHSEL register values that 135 * allow for a route from @csi + @chan to @vin. 136 * 137 * .. note:: 138 * Each R-Car CSI-2 receiver has four output channels facing the VIN 139 * devices, each channel can carry one CSI-2 Virtual Channel (VC). 140 * There is no correlation between channel number and CSI-2 VC. It's 141 * up to the CSI-2 receiver driver to configure which VC is output 142 * on which channel, the VIN devices only care about output channels. 143 * 144 * There are in some cases multiple CHSEL register settings which would 145 * allow for the same route from @csi + @channel to @vin. For example 146 * on R-Car H3 both the CHSEL values 0 and 3 allow for a route from 147 * CSI40/VC0 to VIN0. All possible CHSEL values for a route need to be 148 * recorded as a bitmask in @mask, in this example bit 0 and 3 should 149 * be set. 150 */ 151 struct rvin_group_route { 152 enum rvin_csi_id csi; 153 unsigned int channel; 154 unsigned int vin; 155 unsigned int mask; 156 }; 157 158 /** 159 * struct rvin_info - Information about the particular VIN implementation 160 * @model: VIN model 161 * @use_mc: use media controller instead of controlling subdevice 162 * @use_isp: the VIN is connected to the ISP and not to the CSI-2 163 * @nv12: support outputing NV12 pixel format 164 * @max_width: max input width the VIN supports 165 * @max_height: max input height the VIN supports 166 * @routes: list of possible routes from the CSI-2 recivers to 167 * all VINs. The list mush be NULL terminated. 168 */ 169 struct rvin_info { 170 enum model_id model; 171 bool use_mc; 172 bool use_isp; 173 bool nv12; 174 175 unsigned int max_width; 176 unsigned int max_height; 177 const struct rvin_group_route *routes; 178 }; 179 180 /** 181 * struct rvin_dev - Renesas VIN device structure 182 * @dev: (OF) device 183 * @base: device I/O register space remapped to virtual memory 184 * @info: info about VIN instance 185 * 186 * @vdev: V4L2 video device associated with VIN 187 * @v4l2_dev: V4L2 device 188 * @ctrl_handler: V4L2 control handler 189 * @notifier: V4L2 asynchronous subdevs notifier 190 * 191 * @parallel: parallel input subdevice descriptor 192 * 193 * @group: Gen3 CSI group 194 * @id: Gen3 group id for this VIN 195 * @pad: media pad for the video device entity 196 * 197 * @lock: protects @queue 198 * @queue: vb2 buffers queue 199 * @scratch: cpu address for scratch buffer 200 * @scratch_phys: physical address of the scratch buffer 201 * 202 * @qlock: protects @buf_hw, @buf_list, @sequence and @state 203 * @buf_hw: Keeps track of buffers given to HW slot 204 * @buf_list: list of queued buffers 205 * @sequence: V4L2 buffers sequence number 206 * @state: keeps track of operation state 207 * 208 * @is_csi: flag to mark the VIN as using a CSI-2 subdevice 209 * @chsel: Cached value of the current CSI-2 channel selection 210 * 211 * @mbus_code: media bus format code 212 * @format: active V4L2 pixel format 213 * 214 * @crop: active cropping 215 * @compose: active composing 216 * @src_rect: active size of the video source 217 * @std: active video standard of the video source 218 * 219 * @alpha: Alpha component to fill in for supported pixel formats 220 */ 221 struct rvin_dev { 222 struct device *dev; 223 void __iomem *base; 224 const struct rvin_info *info; 225 226 struct video_device vdev; 227 struct v4l2_device v4l2_dev; 228 struct v4l2_ctrl_handler ctrl_handler; 229 struct v4l2_async_notifier notifier; 230 231 struct rvin_parallel_entity parallel; 232 233 struct rvin_group *group; 234 unsigned int id; 235 struct media_pad pad; 236 237 struct mutex lock; 238 struct vb2_queue queue; 239 void *scratch; 240 dma_addr_t scratch_phys; 241 242 spinlock_t qlock; 243 struct { 244 struct vb2_v4l2_buffer *buffer; 245 enum rvin_buffer_type type; 246 dma_addr_t phys; 247 } buf_hw[HW_BUFFER_NUM]; 248 struct list_head buf_list; 249 unsigned int sequence; 250 enum rvin_dma_state state; 251 252 bool is_csi; 253 unsigned int chsel; 254 255 u32 mbus_code; 256 struct v4l2_pix_format format; 257 258 struct v4l2_rect crop; 259 struct v4l2_rect compose; 260 struct v4l2_rect src_rect; 261 v4l2_std_id std; 262 263 unsigned int alpha; 264 }; 265 266 #define vin_to_source(vin) ((vin)->parallel.subdev) 267 268 /* Debug */ 269 #define vin_dbg(d, fmt, arg...) dev_dbg(d->dev, fmt, ##arg) 270 #define vin_info(d, fmt, arg...) dev_info(d->dev, fmt, ##arg) 271 #define vin_warn(d, fmt, arg...) dev_warn(d->dev, fmt, ##arg) 272 #define vin_err(d, fmt, arg...) dev_err(d->dev, fmt, ##arg) 273 274 /** 275 * struct rvin_group - VIN CSI2 group information 276 * @refcount: number of VIN instances using the group 277 * 278 * @mdev: media device which represents the group 279 * 280 * @lock: protects the count, notifier, vin and csi members 281 * @count: number of enabled VIN instances found in DT 282 * @notifier: group notifier for CSI-2 async subdevices 283 * @vin: VIN instances which are part of the group 284 * @link_setup: Callback to create all links for the media graph 285 * @remotes: array of pairs of fwnode and subdev pointers 286 * to all remote subdevices. 287 */ 288 struct rvin_group { 289 struct kref refcount; 290 291 struct media_device mdev; 292 293 struct mutex lock; 294 unsigned int count; 295 struct v4l2_async_notifier notifier; 296 struct rvin_dev *vin[RCAR_VIN_NUM]; 297 298 int (*link_setup)(struct rvin_dev *vin); 299 300 struct { 301 struct v4l2_async_subdev *asd; 302 struct v4l2_subdev *subdev; 303 } remotes[RVIN_REMOTES_MAX]; 304 }; 305 306 int rvin_dma_register(struct rvin_dev *vin, int irq); 307 void rvin_dma_unregister(struct rvin_dev *vin); 308 309 int rvin_v4l2_register(struct rvin_dev *vin); 310 void rvin_v4l2_unregister(struct rvin_dev *vin); 311 312 const struct rvin_video_format *rvin_format_from_pixel(struct rvin_dev *vin, 313 u32 pixelformat); 314 315 316 /* Cropping, composing and scaling */ 317 void rvin_crop_scale_comp(struct rvin_dev *vin); 318 319 int rvin_set_channel_routing(struct rvin_dev *vin, u8 chsel); 320 void rvin_set_alpha(struct rvin_dev *vin, unsigned int alpha); 321 322 int rvin_start_streaming(struct rvin_dev *vin); 323 void rvin_stop_streaming(struct rvin_dev *vin); 324 325 #endif 326