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