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