1 /* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */ 2 /* 3 * Rockchip ISP1 Driver - Common definitions 4 * 5 * Copyright (C) 2019 Collabora, Ltd. 6 * 7 * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd. 8 * Copyright (C) 2017 Rockchip Electronics Co., Ltd. 9 */ 10 11 #ifndef _RKISP1_COMMON_H 12 #define _RKISP1_COMMON_H 13 14 #include <linux/clk.h> 15 #include <linux/interrupt.h> 16 #include <linux/mutex.h> 17 #include <linux/rkisp1-config.h> 18 #include <media/media-device.h> 19 #include <media/media-entity.h> 20 #include <media/v4l2-ctrls.h> 21 #include <media/v4l2-device.h> 22 #include <media/videobuf2-v4l2.h> 23 24 #include "rkisp1-regs.h" 25 26 struct dentry; 27 28 /* 29 * flags on the 'direction' field in struct 'rkisp1_isp_mbus_info' that indicate 30 * on which pad the media bus format is supported 31 */ 32 #define RKISP1_ISP_SD_SRC BIT(0) 33 #define RKISP1_ISP_SD_SINK BIT(1) 34 35 /* min and max values for the widths and heights of the entities */ 36 #define RKISP1_ISP_MAX_WIDTH 4032 37 #define RKISP1_ISP_MAX_HEIGHT 3024 38 #define RKISP1_ISP_MIN_WIDTH 32 39 #define RKISP1_ISP_MIN_HEIGHT 32 40 41 #define RKISP1_RSZ_MP_SRC_MAX_WIDTH 4416 42 #define RKISP1_RSZ_MP_SRC_MAX_HEIGHT 3312 43 #define RKISP1_RSZ_SP_SRC_MAX_WIDTH 1920 44 #define RKISP1_RSZ_SP_SRC_MAX_HEIGHT 1920 45 #define RKISP1_RSZ_SRC_MIN_WIDTH 32 46 #define RKISP1_RSZ_SRC_MIN_HEIGHT 16 47 48 /* the default width and height of all the entities */ 49 #define RKISP1_DEFAULT_WIDTH 800 50 #define RKISP1_DEFAULT_HEIGHT 600 51 52 #define RKISP1_DRIVER_NAME "rkisp1" 53 #define RKISP1_BUS_INFO "platform:" RKISP1_DRIVER_NAME 54 55 /* maximum number of clocks */ 56 #define RKISP1_MAX_BUS_CLK 8 57 58 /* a bitmask of the ready stats */ 59 #define RKISP1_STATS_MEAS_MASK (RKISP1_CIF_ISP_AWB_DONE | \ 60 RKISP1_CIF_ISP_AFM_FIN | \ 61 RKISP1_CIF_ISP_EXP_END | \ 62 RKISP1_CIF_ISP_HIST_MEASURE_RDY) 63 64 /* enum for the resizer pads */ 65 enum rkisp1_rsz_pad { 66 RKISP1_RSZ_PAD_SINK, 67 RKISP1_RSZ_PAD_SRC, 68 RKISP1_RSZ_PAD_MAX 69 }; 70 71 /* enum for the capture id */ 72 enum rkisp1_stream_id { 73 RKISP1_MAINPATH, 74 RKISP1_SELFPATH, 75 }; 76 77 /* bayer patterns */ 78 enum rkisp1_fmt_raw_pat_type { 79 RKISP1_RAW_RGGB = 0, 80 RKISP1_RAW_GRBG, 81 RKISP1_RAW_GBRG, 82 RKISP1_RAW_BGGR, 83 }; 84 85 /* enum for the isp pads */ 86 enum rkisp1_isp_pad { 87 RKISP1_ISP_PAD_SINK_VIDEO, 88 RKISP1_ISP_PAD_SINK_PARAMS, 89 RKISP1_ISP_PAD_SOURCE_VIDEO, 90 RKISP1_ISP_PAD_SOURCE_STATS, 91 RKISP1_ISP_PAD_MAX 92 }; 93 94 /* 95 * struct rkisp1_sensor_async - A container for the v4l2_async_subdev to add to the notifier 96 * of the v4l2-async API 97 * 98 * @asd: async_subdev variable for the sensor 99 * @lanes: number of lanes 100 * @mbus_type: type of bus (currently only CSI2 is supported) 101 * @mbus_flags: media bus (V4L2_MBUS_*) flags 102 * @sd: a pointer to v4l2_subdev struct of the sensor 103 * @pixel_rate_ctrl: pixel rate of the sensor, used to initialize the phy 104 * @dphy: a pointer to the phy 105 */ 106 struct rkisp1_sensor_async { 107 struct v4l2_async_subdev asd; 108 unsigned int lanes; 109 enum v4l2_mbus_type mbus_type; 110 unsigned int mbus_flags; 111 struct v4l2_subdev *sd; 112 struct v4l2_ctrl *pixel_rate_ctrl; 113 struct phy *dphy; 114 }; 115 116 /* 117 * struct rkisp1_isp - ISP subdev entity 118 * 119 * @sd: v4l2_subdev variable 120 * @rkisp1: pointer to rkisp1_device 121 * @pads: media pads 122 * @pad_cfg: pads configurations 123 * @sink_fmt: input format 124 * @src_fmt: output format 125 * @ops_lock: ops serialization 126 * @is_dphy_errctrl_disabled: if dphy errctrl is disabled (avoid endless interrupt) 127 * @frame_sequence: used to synchronize frame_id between video devices. 128 */ 129 struct rkisp1_isp { 130 struct v4l2_subdev sd; 131 struct media_pad pads[RKISP1_ISP_PAD_MAX]; 132 struct v4l2_subdev_pad_config pad_cfg[RKISP1_ISP_PAD_MAX]; 133 const struct rkisp1_isp_mbus_info *sink_fmt; 134 const struct rkisp1_isp_mbus_info *src_fmt; 135 struct mutex ops_lock; /* serialize the subdevice ops */ 136 bool is_dphy_errctrl_disabled; 137 __u32 frame_sequence; 138 }; 139 140 /* 141 * struct rkisp1_vdev_node - Container for the video nodes: params, stats, mainpath, selfpath 142 * 143 * @buf_queue: queue of buffers 144 * @vlock: lock of the video node 145 * @vdev: video node 146 * @pad: media pad 147 */ 148 struct rkisp1_vdev_node { 149 struct vb2_queue buf_queue; 150 struct mutex vlock; /* ioctl serialization mutex */ 151 struct video_device vdev; 152 struct media_pad pad; 153 }; 154 155 /* 156 * struct rkisp1_buffer - A container for the vb2 buffers used by the video devices: 157 * params, stats, mainpath, selfpath 158 * 159 * @vb: vb2 buffer 160 * @queue: entry of the buffer in the queue 161 * @buff_addr: dma addresses of each plane, used only by the capture devices: selfpath, mainpath 162 */ 163 struct rkisp1_buffer { 164 struct vb2_v4l2_buffer vb; 165 struct list_head queue; 166 u32 buff_addr[VIDEO_MAX_PLANES]; 167 }; 168 169 /* 170 * struct rkisp1_dummy_buffer - A buffer to write the next frame to in case 171 * there are no vb2 buffers available. 172 * 173 * @vaddr: return value of call to dma_alloc_attrs. 174 * @dma_addr: dma address of the buffer. 175 * @size: size of the buffer. 176 */ 177 struct rkisp1_dummy_buffer { 178 void *vaddr; 179 dma_addr_t dma_addr; 180 u32 size; 181 }; 182 183 struct rkisp1_device; 184 185 /* 186 * struct rkisp1_capture - ISP capture video device 187 * 188 * @vnode: video node 189 * @rkisp1: pointer to rkisp1_device 190 * @id: id of the capture, one of RKISP1_SELFPATH, RKISP1_MAINPATH 191 * @ops: list of callbacks to configure the capture device. 192 * @config: a pointer to the list of registers to configure the capture format. 193 * @is_streaming: device is streaming 194 * @is_stopping: stop_streaming callback was called and the device is in the process of 195 * stopping the streaming. 196 * @done: when stop_streaming callback is called, the device waits for the next irq 197 * handler to stop the streaming by waiting on the 'done' wait queue. 198 * If the irq handler is not called, the stream is stopped by the callback 199 * after timeout. 200 * @sp_y_stride: the selfpath allows to configure a y stride that is longer than the image width. 201 * @buf.lock: lock to protect buf.queue 202 * @buf.queue: queued buffer list 203 * @buf.dummy: dummy space to store dropped data 204 * 205 * rkisp1 uses shadow registers, so it needs two buffers at a time 206 * @buf.curr: the buffer used for current frame 207 * @buf.next: the buffer used for next frame 208 * @pix.cfg: pixel configuration 209 * @pix.info: a pointer to the v4l2_format_info of the pixel format 210 * @pix.fmt: buffer format 211 */ 212 struct rkisp1_capture { 213 struct rkisp1_vdev_node vnode; 214 struct rkisp1_device *rkisp1; 215 enum rkisp1_stream_id id; 216 const struct rkisp1_capture_ops *ops; 217 const struct rkisp1_capture_config *config; 218 bool is_streaming; 219 bool is_stopping; 220 wait_queue_head_t done; 221 unsigned int sp_y_stride; 222 struct { 223 /* protects queue, curr and next */ 224 spinlock_t lock; 225 struct list_head queue; 226 struct rkisp1_dummy_buffer dummy; 227 struct rkisp1_buffer *curr; 228 struct rkisp1_buffer *next; 229 } buf; 230 struct { 231 const struct rkisp1_capture_fmt_cfg *cfg; 232 const struct v4l2_format_info *info; 233 struct v4l2_pix_format_mplane fmt; 234 } pix; 235 }; 236 237 struct rkisp1_stats; 238 struct rkisp1_stats_ops { 239 void (*get_awb_meas)(struct rkisp1_stats *stats, 240 struct rkisp1_stat_buffer *pbuf); 241 void (*get_aec_meas)(struct rkisp1_stats *stats, 242 struct rkisp1_stat_buffer *pbuf); 243 void (*get_hst_meas)(struct rkisp1_stats *stats, 244 struct rkisp1_stat_buffer *pbuf); 245 }; 246 247 /* 248 * struct rkisp1_stats - ISP Statistics device 249 * 250 * @vnode: video node 251 * @rkisp1: pointer to the rkisp1 device 252 * @lock: locks the buffer list 'stat' 253 * @stat: queue of rkisp1_buffer 254 * @vdev_fmt: v4l2_format of the metadata format 255 */ 256 struct rkisp1_stats { 257 struct rkisp1_vdev_node vnode; 258 struct rkisp1_device *rkisp1; 259 const struct rkisp1_stats_ops *ops; 260 261 spinlock_t lock; /* locks the buffers list 'stats' */ 262 struct list_head stat; 263 struct v4l2_format vdev_fmt; 264 }; 265 266 struct rkisp1_params; 267 struct rkisp1_params_ops { 268 void (*lsc_matrix_config)(struct rkisp1_params *params, 269 const struct rkisp1_cif_isp_lsc_config *pconfig); 270 void (*goc_config)(struct rkisp1_params *params, 271 const struct rkisp1_cif_isp_goc_config *arg); 272 void (*awb_meas_config)(struct rkisp1_params *params, 273 const struct rkisp1_cif_isp_awb_meas_config *arg); 274 void (*awb_meas_enable)(struct rkisp1_params *params, 275 const struct rkisp1_cif_isp_awb_meas_config *arg, 276 bool en); 277 void (*awb_gain_config)(struct rkisp1_params *params, 278 const struct rkisp1_cif_isp_awb_gain_config *arg); 279 void (*aec_config)(struct rkisp1_params *params, 280 const struct rkisp1_cif_isp_aec_config *arg); 281 void (*hst_config)(struct rkisp1_params *params, 282 const struct rkisp1_cif_isp_hst_config *arg); 283 void (*hst_enable)(struct rkisp1_params *params, 284 const struct rkisp1_cif_isp_hst_config *arg, bool en); 285 void (*afm_config)(struct rkisp1_params *params, 286 const struct rkisp1_cif_isp_afc_config *arg); 287 }; 288 289 /* 290 * struct rkisp1_params - ISP input parameters device 291 * 292 * @vnode: video node 293 * @rkisp1: pointer to the rkisp1 device 294 * @ops: pointer to the variant-specific operations 295 * @config_lock: locks the buffer list 'params' 296 * @params: queue of rkisp1_buffer 297 * @vdev_fmt: v4l2_format of the metadata format 298 * @quantization: the quantization configured on the isp's src pad 299 * @raw_type: the bayer pattern on the isp video sink pad 300 */ 301 struct rkisp1_params { 302 struct rkisp1_vdev_node vnode; 303 struct rkisp1_device *rkisp1; 304 const struct rkisp1_params_ops *ops; 305 306 spinlock_t config_lock; /* locks the buffers list 'params' */ 307 struct list_head params; 308 struct v4l2_format vdev_fmt; 309 310 enum v4l2_quantization quantization; 311 enum rkisp1_fmt_raw_pat_type raw_type; 312 }; 313 314 /* 315 * struct rkisp1_resizer - Resizer subdev 316 * 317 * @sd: v4l2_subdev variable 318 * @regs_base: base register address offset 319 * @id: id of the resizer, one of RKISP1_SELFPATH, RKISP1_MAINPATH 320 * @rkisp1: pointer to the rkisp1 device 321 * @pads: media pads 322 * @pad_cfg: configurations for the pads 323 * @config: the set of registers to configure the resizer 324 * @pixel_enc: pixel encoding of the resizer 325 * @ops_lock: a lock for the subdev ops 326 */ 327 struct rkisp1_resizer { 328 struct v4l2_subdev sd; 329 u32 regs_base; 330 enum rkisp1_stream_id id; 331 struct rkisp1_device *rkisp1; 332 struct media_pad pads[RKISP1_RSZ_PAD_MAX]; 333 struct v4l2_subdev_pad_config pad_cfg[RKISP1_RSZ_PAD_MAX]; 334 const struct rkisp1_rsz_config *config; 335 enum v4l2_pixel_encoding pixel_enc; 336 struct mutex ops_lock; /* serialize the subdevice ops */ 337 }; 338 339 /* 340 * struct rkisp1_debug - Values to be exposed on debugfs. 341 * The parameters are counters of the number of times the 342 * event occurred since the driver was loaded. 343 * 344 * @data_loss: loss of data occurred within a line, processing failure 345 * @outform_size_error: size error is generated in outmux submodule 346 * @img_stabilization_size_error: size error is generated in image stabilization submodule 347 * @inform_size_err: size error is generated in inform submodule 348 * @mipi_error: mipi error occurred 349 * @stats_error: writing to the 'Interrupt clear register' did not clear 350 * it in the register 'Masked interrupt status' 351 * @stop_timeout: upon stream stop, the capture waits 1 second for the isr to stop 352 * the stream. This param is incremented in case of timeout. 353 * @frame_drop: a frame was ready but the buffer queue was empty so the frame 354 * was not sent to userspace 355 */ 356 struct rkisp1_debug { 357 struct dentry *debugfs_dir; 358 unsigned long data_loss; 359 unsigned long outform_size_error; 360 unsigned long img_stabilization_size_error; 361 unsigned long inform_size_error; 362 unsigned long irq_delay; 363 unsigned long mipi_error; 364 unsigned long stats_error; 365 unsigned long stop_timeout[2]; 366 unsigned long frame_drop[2]; 367 }; 368 369 /* 370 * struct rkisp1_device - ISP platform device 371 * 372 * @base_addr: base register address 373 * @irq: the irq number 374 * @dev: a pointer to the struct device 375 * @clk_size: number of clocks 376 * @clks: array of clocks 377 * @v4l2_dev: v4l2_device variable 378 * @media_dev: media_device variable 379 * @notifier: a notifier to register on the v4l2-async API to be notified on the sensor 380 * @active_sensor: sensor in-use, set when streaming on 381 * @isp: ISP sub-device 382 * @resizer_devs: resizer sub-devices 383 * @capture_devs: capture devices 384 * @stats: ISP statistics metadata capture device 385 * @params: ISP parameters metadata output device 386 * @pipe: media pipeline 387 * @stream_lock: serializes {start/stop}_streaming callbacks between the capture devices. 388 * @debug: debug params to be exposed on debugfs 389 */ 390 struct rkisp1_device { 391 void __iomem *base_addr; 392 struct device *dev; 393 unsigned int clk_size; 394 struct clk_bulk_data clks[RKISP1_MAX_BUS_CLK]; 395 struct v4l2_device v4l2_dev; 396 struct media_device media_dev; 397 struct v4l2_async_notifier notifier; 398 struct rkisp1_sensor_async *active_sensor; 399 struct rkisp1_isp isp; 400 struct rkisp1_resizer resizer_devs[2]; 401 struct rkisp1_capture capture_devs[2]; 402 struct rkisp1_stats stats; 403 struct rkisp1_params params; 404 struct media_pipeline pipe; 405 struct mutex stream_lock; /* serialize {start/stop}_streaming cb between capture devices */ 406 struct rkisp1_debug debug; 407 }; 408 409 /* 410 * struct rkisp1_isp_mbus_info - ISP media bus info, Translates media bus code to hardware 411 * format values 412 * 413 * @mbus_code: media bus code 414 * @pixel_enc: pixel encoding 415 * @mipi_dt: mipi data type 416 * @yuv_seq: the order of the Y, Cb, Cr values 417 * @bus_width: bus width 418 * @bayer_pat: bayer pattern 419 * @direction: a bitmask of the flags indicating on which pad the format is supported on 420 */ 421 struct rkisp1_isp_mbus_info { 422 u32 mbus_code; 423 enum v4l2_pixel_encoding pixel_enc; 424 u32 mipi_dt; 425 u32 yuv_seq; 426 u8 bus_width; 427 enum rkisp1_fmt_raw_pat_type bayer_pat; 428 unsigned int direction; 429 }; 430 431 static inline void 432 rkisp1_write(struct rkisp1_device *rkisp1, unsigned int addr, u32 val) 433 { 434 writel(val, rkisp1->base_addr + addr); 435 } 436 437 static inline u32 rkisp1_read(struct rkisp1_device *rkisp1, unsigned int addr) 438 { 439 return readl(rkisp1->base_addr + addr); 440 } 441 442 /* 443 * rkisp1_cap_enum_mbus_codes - A helper function that return the i'th supported mbus code 444 * of the capture entity. This is used to enumerate the supported 445 * mbus codes on the source pad of the resizer. 446 * 447 * @cap: the capture entity 448 * @code: the mbus code, the function reads the code->index and fills the code->code 449 */ 450 int rkisp1_cap_enum_mbus_codes(struct rkisp1_capture *cap, 451 struct v4l2_subdev_mbus_code_enum *code); 452 453 /* 454 * rkisp1_sd_adjust_crop_rect - adjust a rectangle to fit into another rectangle. 455 * 456 * @crop: rectangle to adjust. 457 * @bounds: rectangle used as bounds. 458 */ 459 void rkisp1_sd_adjust_crop_rect(struct v4l2_rect *crop, 460 const struct v4l2_rect *bounds); 461 462 /* 463 * rkisp1_sd_adjust_crop - adjust a rectangle to fit into media bus format 464 * 465 * @crop: rectangle to adjust. 466 * @bounds: media bus format used as bounds. 467 */ 468 void rkisp1_sd_adjust_crop(struct v4l2_rect *crop, 469 const struct v4l2_mbus_framefmt *bounds); 470 471 /* 472 * rkisp1_isp_mbus_info - get the isp info of the media bus code 473 * 474 * @mbus_code: the media bus code 475 */ 476 const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get(u32 mbus_code); 477 478 /* rkisp1_params_configure - configure the params when stream starts. 479 * This function is called by the isp entity upon stream starts. 480 * The function applies the initial configuration of the parameters. 481 * 482 * @params: pointer to rkisp1_params. 483 * @bayer_pat: the bayer pattern on the isp video sink pad 484 * @quantization: the quantization configured on the isp's src pad 485 */ 486 void rkisp1_params_configure(struct rkisp1_params *params, 487 enum rkisp1_fmt_raw_pat_type bayer_pat, 488 enum v4l2_quantization quantization); 489 490 /* rkisp1_params_disable - disable all parameters. 491 * This function is called by the isp entity upon stream start 492 * when capturing bayer format. 493 * 494 * @params: pointer to rkisp1_params. 495 */ 496 void rkisp1_params_disable(struct rkisp1_params *params); 497 498 /* irq handlers */ 499 irqreturn_t rkisp1_isp_isr(int irq, void *ctx); 500 irqreturn_t rkisp1_mipi_isr(int irq, void *ctx); 501 irqreturn_t rkisp1_capture_isr(int irq, void *ctx); 502 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris); 503 void rkisp1_params_isr(struct rkisp1_device *rkisp1); 504 505 /* register/unregisters functions of the entities */ 506 int rkisp1_capture_devs_register(struct rkisp1_device *rkisp1); 507 void rkisp1_capture_devs_unregister(struct rkisp1_device *rkisp1); 508 509 int rkisp1_isp_register(struct rkisp1_device *rkisp1); 510 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1); 511 512 int rkisp1_resizer_devs_register(struct rkisp1_device *rkisp1); 513 void rkisp1_resizer_devs_unregister(struct rkisp1_device *rkisp1); 514 515 int rkisp1_stats_register(struct rkisp1_device *rkisp1); 516 void rkisp1_stats_unregister(struct rkisp1_device *rkisp1); 517 518 int rkisp1_params_register(struct rkisp1_device *rkisp1); 519 void rkisp1_params_unregister(struct rkisp1_device *rkisp1); 520 521 #if IS_ENABLED(CONFIG_DEBUG_FS) 522 void rkisp1_debug_init(struct rkisp1_device *rkisp1); 523 void rkisp1_debug_cleanup(struct rkisp1_device *rkisp1); 524 #else 525 static inline void rkisp1_debug_init(struct rkisp1_device *rkisp1) 526 { 527 } 528 static inline void rkisp1_debug_cleanup(struct rkisp1_device *rkisp1) 529 { 530 } 531 #endif 532 533 #endif /* _RKISP1_COMMON_H */ 534