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_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_info - Model-specific ISP Information 96 * 97 * @clks: array of ISP clock names 98 * @clk_size: number of entries in the @clks array 99 * @isrs: array of ISP interrupt descriptors 100 * @isr_size: number of entries in the @isrs array 101 * @isp_ver: ISP version 102 * 103 * This structure contains information about the ISP specific to a particular 104 * ISP model, version, or integration in a particular SoC. 105 */ 106 struct rkisp1_info { 107 const char * const *clks; 108 unsigned int clk_size; 109 const struct rkisp1_isr_data *isrs; 110 unsigned int isr_size; 111 enum rkisp1_cif_isp_version isp_ver; 112 }; 113 114 /* 115 * struct rkisp1_sensor_async - A container for the v4l2_async_subdev to add to the notifier 116 * of the v4l2-async API 117 * 118 * @asd: async_subdev variable for the sensor 119 * @index: index of the sensor (counting sensor found in DT) 120 * @source_ep: fwnode for the sensor source endpoint 121 * @lanes: number of lanes 122 * @mbus_type: type of bus (currently only CSI2 is supported) 123 * @mbus_flags: media bus (V4L2_MBUS_*) flags 124 * @sd: a pointer to v4l2_subdev struct of the sensor 125 * @pixel_rate_ctrl: pixel rate of the sensor, used to initialize the phy 126 * @dphy: a pointer to the phy 127 */ 128 struct rkisp1_sensor_async { 129 struct v4l2_async_subdev asd; 130 unsigned int index; 131 struct fwnode_handle *source_ep; 132 unsigned int lanes; 133 enum v4l2_mbus_type mbus_type; 134 unsigned int mbus_flags; 135 struct v4l2_subdev *sd; 136 struct v4l2_ctrl *pixel_rate_ctrl; 137 struct phy *dphy; 138 }; 139 140 /* 141 * struct rkisp1_isp - ISP subdev entity 142 * 143 * @sd: v4l2_subdev variable 144 * @rkisp1: pointer to rkisp1_device 145 * @pads: media pads 146 * @pad_cfg: pads configurations 147 * @sink_fmt: input format 148 * @src_fmt: output format 149 * @ops_lock: ops serialization 150 * @is_dphy_errctrl_disabled: if dphy errctrl is disabled (avoid endless interrupt) 151 * @frame_sequence: used to synchronize frame_id between video devices. 152 */ 153 struct rkisp1_isp { 154 struct v4l2_subdev sd; 155 struct media_pad pads[RKISP1_ISP_PAD_MAX]; 156 struct v4l2_subdev_pad_config pad_cfg[RKISP1_ISP_PAD_MAX]; 157 const struct rkisp1_mbus_info *sink_fmt; 158 const struct rkisp1_mbus_info *src_fmt; 159 struct mutex ops_lock; /* serialize the subdevice ops */ 160 bool is_dphy_errctrl_disabled; 161 __u32 frame_sequence; 162 }; 163 164 /* 165 * struct rkisp1_vdev_node - Container for the video nodes: params, stats, mainpath, selfpath 166 * 167 * @buf_queue: queue of buffers 168 * @vlock: lock of the video node 169 * @vdev: video node 170 * @pad: media pad 171 */ 172 struct rkisp1_vdev_node { 173 struct vb2_queue buf_queue; 174 struct mutex vlock; /* ioctl serialization mutex */ 175 struct video_device vdev; 176 struct media_pad pad; 177 }; 178 179 /* 180 * struct rkisp1_buffer - A container for the vb2 buffers used by the video devices: 181 * params, stats, mainpath, selfpath 182 * 183 * @vb: vb2 buffer 184 * @queue: entry of the buffer in the queue 185 * @buff_addr: dma addresses of each plane, used only by the capture devices: selfpath, mainpath 186 */ 187 struct rkisp1_buffer { 188 struct vb2_v4l2_buffer vb; 189 struct list_head queue; 190 u32 buff_addr[VIDEO_MAX_PLANES]; 191 }; 192 193 /* 194 * struct rkisp1_dummy_buffer - A buffer to write the next frame to in case 195 * there are no vb2 buffers available. 196 * 197 * @vaddr: return value of call to dma_alloc_attrs. 198 * @dma_addr: dma address of the buffer. 199 * @size: size of the buffer. 200 */ 201 struct rkisp1_dummy_buffer { 202 void *vaddr; 203 dma_addr_t dma_addr; 204 u32 size; 205 }; 206 207 struct rkisp1_device; 208 209 /* 210 * struct rkisp1_capture - ISP capture video device 211 * 212 * @vnode: video node 213 * @rkisp1: pointer to rkisp1_device 214 * @id: id of the capture, one of RKISP1_SELFPATH, RKISP1_MAINPATH 215 * @ops: list of callbacks to configure the capture device. 216 * @config: a pointer to the list of registers to configure the capture format. 217 * @is_streaming: device is streaming 218 * @is_stopping: stop_streaming callback was called and the device is in the process of 219 * stopping the streaming. 220 * @done: when stop_streaming callback is called, the device waits for the next irq 221 * handler to stop the streaming by waiting on the 'done' wait queue. 222 * If the irq handler is not called, the stream is stopped by the callback 223 * after timeout. 224 * @sp_y_stride: the selfpath allows to configure a y stride that is longer than the image width. 225 * @buf.lock: lock to protect buf.queue 226 * @buf.queue: queued buffer list 227 * @buf.dummy: dummy space to store dropped data 228 * 229 * rkisp1 uses shadow registers, so it needs two buffers at a time 230 * @buf.curr: the buffer used for current frame 231 * @buf.next: the buffer used for next frame 232 * @pix.cfg: pixel configuration 233 * @pix.info: a pointer to the v4l2_format_info of the pixel format 234 * @pix.fmt: buffer format 235 */ 236 struct rkisp1_capture { 237 struct rkisp1_vdev_node vnode; 238 struct rkisp1_device *rkisp1; 239 enum rkisp1_stream_id id; 240 const struct rkisp1_capture_ops *ops; 241 const struct rkisp1_capture_config *config; 242 bool is_streaming; 243 bool is_stopping; 244 wait_queue_head_t done; 245 unsigned int sp_y_stride; 246 struct { 247 /* protects queue, curr and next */ 248 spinlock_t lock; 249 struct list_head queue; 250 struct rkisp1_dummy_buffer dummy; 251 struct rkisp1_buffer *curr; 252 struct rkisp1_buffer *next; 253 } buf; 254 struct { 255 const struct rkisp1_capture_fmt_cfg *cfg; 256 const struct v4l2_format_info *info; 257 struct v4l2_pix_format_mplane fmt; 258 } pix; 259 }; 260 261 struct rkisp1_stats; 262 struct rkisp1_stats_ops { 263 void (*get_awb_meas)(struct rkisp1_stats *stats, 264 struct rkisp1_stat_buffer *pbuf); 265 void (*get_aec_meas)(struct rkisp1_stats *stats, 266 struct rkisp1_stat_buffer *pbuf); 267 void (*get_hst_meas)(struct rkisp1_stats *stats, 268 struct rkisp1_stat_buffer *pbuf); 269 }; 270 271 /* 272 * struct rkisp1_stats - ISP Statistics device 273 * 274 * @vnode: video node 275 * @rkisp1: pointer to the rkisp1 device 276 * @lock: locks the buffer list 'stat' 277 * @stat: queue of rkisp1_buffer 278 * @vdev_fmt: v4l2_format of the metadata format 279 */ 280 struct rkisp1_stats { 281 struct rkisp1_vdev_node vnode; 282 struct rkisp1_device *rkisp1; 283 const struct rkisp1_stats_ops *ops; 284 285 spinlock_t lock; /* locks the buffers list 'stats' */ 286 struct list_head stat; 287 struct v4l2_format vdev_fmt; 288 }; 289 290 struct rkisp1_params; 291 struct rkisp1_params_ops { 292 void (*lsc_matrix_config)(struct rkisp1_params *params, 293 const struct rkisp1_cif_isp_lsc_config *pconfig); 294 void (*goc_config)(struct rkisp1_params *params, 295 const struct rkisp1_cif_isp_goc_config *arg); 296 void (*awb_meas_config)(struct rkisp1_params *params, 297 const struct rkisp1_cif_isp_awb_meas_config *arg); 298 void (*awb_meas_enable)(struct rkisp1_params *params, 299 const struct rkisp1_cif_isp_awb_meas_config *arg, 300 bool en); 301 void (*awb_gain_config)(struct rkisp1_params *params, 302 const struct rkisp1_cif_isp_awb_gain_config *arg); 303 void (*aec_config)(struct rkisp1_params *params, 304 const struct rkisp1_cif_isp_aec_config *arg); 305 void (*hst_config)(struct rkisp1_params *params, 306 const struct rkisp1_cif_isp_hst_config *arg); 307 void (*hst_enable)(struct rkisp1_params *params, 308 const struct rkisp1_cif_isp_hst_config *arg, bool en); 309 void (*afm_config)(struct rkisp1_params *params, 310 const struct rkisp1_cif_isp_afc_config *arg); 311 }; 312 313 /* 314 * struct rkisp1_params - ISP input parameters device 315 * 316 * @vnode: video node 317 * @rkisp1: pointer to the rkisp1 device 318 * @ops: pointer to the variant-specific operations 319 * @config_lock: locks the buffer list 'params' 320 * @params: queue of rkisp1_buffer 321 * @vdev_fmt: v4l2_format of the metadata format 322 * @quantization: the quantization configured on the isp's src pad 323 * @raw_type: the bayer pattern on the isp video sink pad 324 */ 325 struct rkisp1_params { 326 struct rkisp1_vdev_node vnode; 327 struct rkisp1_device *rkisp1; 328 const struct rkisp1_params_ops *ops; 329 330 spinlock_t config_lock; /* locks the buffers list 'params' */ 331 struct list_head params; 332 struct v4l2_format vdev_fmt; 333 334 enum v4l2_quantization quantization; 335 enum rkisp1_fmt_raw_pat_type raw_type; 336 }; 337 338 /* 339 * struct rkisp1_resizer - Resizer subdev 340 * 341 * @sd: v4l2_subdev variable 342 * @regs_base: base register address offset 343 * @id: id of the resizer, one of RKISP1_SELFPATH, RKISP1_MAINPATH 344 * @rkisp1: pointer to the rkisp1 device 345 * @pads: media pads 346 * @pad_cfg: configurations for the pads 347 * @config: the set of registers to configure the resizer 348 * @pixel_enc: pixel encoding of the resizer 349 * @ops_lock: a lock for the subdev ops 350 */ 351 struct rkisp1_resizer { 352 struct v4l2_subdev sd; 353 u32 regs_base; 354 enum rkisp1_stream_id id; 355 struct rkisp1_device *rkisp1; 356 struct media_pad pads[RKISP1_RSZ_PAD_MAX]; 357 struct v4l2_subdev_pad_config pad_cfg[RKISP1_RSZ_PAD_MAX]; 358 const struct rkisp1_rsz_config *config; 359 enum v4l2_pixel_encoding pixel_enc; 360 struct mutex ops_lock; /* serialize the subdevice ops */ 361 }; 362 363 /* 364 * struct rkisp1_debug - Values to be exposed on debugfs. 365 * The parameters are counters of the number of times the 366 * event occurred since the driver was loaded. 367 * 368 * @data_loss: loss of data occurred within a line, processing failure 369 * @outform_size_error: size error is generated in outmux submodule 370 * @img_stabilization_size_error: size error is generated in image stabilization submodule 371 * @inform_size_err: size error is generated in inform submodule 372 * @mipi_error: mipi error occurred 373 * @stats_error: writing to the 'Interrupt clear register' did not clear 374 * it in the register 'Masked interrupt status' 375 * @stop_timeout: upon stream stop, the capture waits 1 second for the isr to stop 376 * the stream. This param is incremented in case of timeout. 377 * @frame_drop: a frame was ready but the buffer queue was empty so the frame 378 * was not sent to userspace 379 */ 380 struct rkisp1_debug { 381 struct dentry *debugfs_dir; 382 unsigned long data_loss; 383 unsigned long outform_size_error; 384 unsigned long img_stabilization_size_error; 385 unsigned long inform_size_error; 386 unsigned long irq_delay; 387 unsigned long mipi_error; 388 unsigned long stats_error; 389 unsigned long stop_timeout[2]; 390 unsigned long frame_drop[2]; 391 }; 392 393 /* 394 * struct rkisp1_device - ISP platform device 395 * 396 * @base_addr: base register address 397 * @irq: the irq number 398 * @dev: a pointer to the struct device 399 * @clk_size: number of clocks 400 * @clks: array of clocks 401 * @v4l2_dev: v4l2_device variable 402 * @media_dev: media_device variable 403 * @notifier: a notifier to register on the v4l2-async API to be notified on the sensor 404 * @active_sensor: sensor in-use, set when streaming on 405 * @isp: ISP sub-device 406 * @resizer_devs: resizer sub-devices 407 * @capture_devs: capture devices 408 * @stats: ISP statistics metadata capture device 409 * @params: ISP parameters metadata output device 410 * @pipe: media pipeline 411 * @stream_lock: serializes {start/stop}_streaming callbacks between the capture devices. 412 * @debug: debug params to be exposed on debugfs 413 * @info: version-specific ISP information 414 */ 415 struct rkisp1_device { 416 void __iomem *base_addr; 417 struct device *dev; 418 unsigned int clk_size; 419 struct clk_bulk_data clks[RKISP1_MAX_BUS_CLK]; 420 struct v4l2_device v4l2_dev; 421 struct media_device media_dev; 422 struct v4l2_async_notifier notifier; 423 struct rkisp1_sensor_async *active_sensor; 424 struct rkisp1_isp isp; 425 struct rkisp1_resizer resizer_devs[2]; 426 struct rkisp1_capture capture_devs[2]; 427 struct rkisp1_stats stats; 428 struct rkisp1_params params; 429 struct media_pipeline pipe; 430 struct mutex stream_lock; /* serialize {start/stop}_streaming cb between capture devices */ 431 struct rkisp1_debug debug; 432 const struct rkisp1_info *info; 433 }; 434 435 /* 436 * struct rkisp1_mbus_info - ISP media bus info, Translates media bus code to hardware 437 * format values 438 * 439 * @mbus_code: media bus code 440 * @pixel_enc: pixel encoding 441 * @mipi_dt: mipi data type 442 * @yuv_seq: the order of the Y, Cb, Cr values 443 * @bus_width: bus width 444 * @bayer_pat: bayer pattern 445 * @direction: a bitmask of the flags indicating on which pad the format is supported on 446 */ 447 struct rkisp1_mbus_info { 448 u32 mbus_code; 449 enum v4l2_pixel_encoding pixel_enc; 450 u32 mipi_dt; 451 u32 yuv_seq; 452 u8 bus_width; 453 enum rkisp1_fmt_raw_pat_type bayer_pat; 454 unsigned int direction; 455 }; 456 457 static inline void 458 rkisp1_write(struct rkisp1_device *rkisp1, unsigned int addr, u32 val) 459 { 460 writel(val, rkisp1->base_addr + addr); 461 } 462 463 static inline u32 rkisp1_read(struct rkisp1_device *rkisp1, unsigned int addr) 464 { 465 return readl(rkisp1->base_addr + addr); 466 } 467 468 /* 469 * rkisp1_cap_enum_mbus_codes - A helper function that return the i'th supported mbus code 470 * of the capture entity. This is used to enumerate the supported 471 * mbus codes on the source pad of the resizer. 472 * 473 * @cap: the capture entity 474 * @code: the mbus code, the function reads the code->index and fills the code->code 475 */ 476 int rkisp1_cap_enum_mbus_codes(struct rkisp1_capture *cap, 477 struct v4l2_subdev_mbus_code_enum *code); 478 479 /* 480 * rkisp1_mbus_info_get_by_index - Retrieve the ith supported mbus info 481 * 482 * @index: index of the mbus info to fetch 483 */ 484 const struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_index(unsigned int index); 485 486 /* 487 * rkisp1_sd_adjust_crop_rect - adjust a rectangle to fit into another rectangle. 488 * 489 * @crop: rectangle to adjust. 490 * @bounds: rectangle used as bounds. 491 */ 492 void rkisp1_sd_adjust_crop_rect(struct v4l2_rect *crop, 493 const struct v4l2_rect *bounds); 494 495 /* 496 * rkisp1_sd_adjust_crop - adjust a rectangle to fit into media bus format 497 * 498 * @crop: rectangle to adjust. 499 * @bounds: media bus format used as bounds. 500 */ 501 void rkisp1_sd_adjust_crop(struct v4l2_rect *crop, 502 const struct v4l2_mbus_framefmt *bounds); 503 504 /* 505 * rkisp1_mbus_info_get_by_code - get the isp info of the media bus code 506 * 507 * @mbus_code: the media bus code 508 */ 509 const struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_code(u32 mbus_code); 510 511 /* rkisp1_params_configure - configure the params when stream starts. 512 * This function is called by the isp entity upon stream starts. 513 * The function applies the initial configuration of the parameters. 514 * 515 * @params: pointer to rkisp1_params. 516 * @bayer_pat: the bayer pattern on the isp video sink pad 517 * @quantization: the quantization configured on the isp's src pad 518 */ 519 void rkisp1_params_configure(struct rkisp1_params *params, 520 enum rkisp1_fmt_raw_pat_type bayer_pat, 521 enum v4l2_quantization quantization); 522 523 /* rkisp1_params_disable - disable all parameters. 524 * This function is called by the isp entity upon stream start 525 * when capturing bayer format. 526 * 527 * @params: pointer to rkisp1_params. 528 */ 529 void rkisp1_params_disable(struct rkisp1_params *params); 530 531 /* irq handlers */ 532 irqreturn_t rkisp1_isp_isr(int irq, void *ctx); 533 irqreturn_t rkisp1_mipi_isr(int irq, void *ctx); 534 irqreturn_t rkisp1_capture_isr(int irq, void *ctx); 535 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris); 536 void rkisp1_params_isr(struct rkisp1_device *rkisp1); 537 538 /* register/unregisters functions of the entities */ 539 int rkisp1_capture_devs_register(struct rkisp1_device *rkisp1); 540 void rkisp1_capture_devs_unregister(struct rkisp1_device *rkisp1); 541 542 int rkisp1_isp_register(struct rkisp1_device *rkisp1); 543 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1); 544 545 int rkisp1_resizer_devs_register(struct rkisp1_device *rkisp1); 546 void rkisp1_resizer_devs_unregister(struct rkisp1_device *rkisp1); 547 548 int rkisp1_stats_register(struct rkisp1_device *rkisp1); 549 void rkisp1_stats_unregister(struct rkisp1_device *rkisp1); 550 551 int rkisp1_params_register(struct rkisp1_device *rkisp1); 552 void rkisp1_params_unregister(struct rkisp1_device *rkisp1); 553 554 #if IS_ENABLED(CONFIG_DEBUG_FS) 555 void rkisp1_debug_init(struct rkisp1_device *rkisp1); 556 void rkisp1_debug_cleanup(struct rkisp1_device *rkisp1); 557 #else 558 static inline void rkisp1_debug_init(struct rkisp1_device *rkisp1) 559 { 560 } 561 static inline void rkisp1_debug_cleanup(struct rkisp1_device *rkisp1) 562 { 563 } 564 #endif 565 566 #endif /* _RKISP1_COMMON_H */ 567