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