1 /* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
3 * Rockchip ISP1 Driver - Common definitions
5 * Copyright (C) 2019 Collabora, Ltd.
7 * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd.
8 * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
11 #ifndef _RKISP1_COMMON_H
12 #define _RKISP1_COMMON_H
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>
24 #include "rkisp1-regs.h"
30 * flags on the 'direction' field in struct rkisp1_mbus_info' that indicate
31 * on which pad the media bus format is supported
33 #define RKISP1_ISP_SD_SRC BIT(0)
34 #define RKISP1_ISP_SD_SINK BIT(1)
36 /* min and max values for the widths and heights of the entities */
37 #define RKISP1_ISP_MAX_WIDTH 4032
38 #define RKISP1_ISP_MAX_HEIGHT 3024
39 #define RKISP1_ISP_MIN_WIDTH 32
40 #define RKISP1_ISP_MIN_HEIGHT 32
42 #define RKISP1_RSZ_MP_SRC_MAX_WIDTH 4416
43 #define RKISP1_RSZ_MP_SRC_MAX_HEIGHT 3312
44 #define RKISP1_RSZ_SP_SRC_MAX_WIDTH 1920
45 #define RKISP1_RSZ_SP_SRC_MAX_HEIGHT 1920
46 #define RKISP1_RSZ_SRC_MIN_WIDTH 32
47 #define RKISP1_RSZ_SRC_MIN_HEIGHT 16
49 /* the default width and height of all the entities */
50 #define RKISP1_DEFAULT_WIDTH 800
51 #define RKISP1_DEFAULT_HEIGHT 600
53 #define RKISP1_DRIVER_NAME "rkisp1"
54 #define RKISP1_BUS_INFO "platform:" RKISP1_DRIVER_NAME
56 /* maximum number of clocks */
57 #define RKISP1_MAX_BUS_CLK 8
59 /* a bitmask of the ready stats */
60 #define RKISP1_STATS_MEAS_MASK (RKISP1_CIF_ISP_AWB_DONE | \
61 RKISP1_CIF_ISP_AFM_FIN | \
62 RKISP1_CIF_ISP_EXP_END | \
63 RKISP1_CIF_ISP_HIST_MEASURE_RDY)
66 enum rkisp1_irq_line {
73 /* enum for the resizer pads */
80 /* enum for the csi receiver pads */
87 /* enum for the capture id */
88 enum rkisp1_stream_id {
94 enum rkisp1_fmt_raw_pat_type {
101 /* enum for the isp pads */
102 enum rkisp1_isp_pad {
103 RKISP1_ISP_PAD_SINK_VIDEO,
104 RKISP1_ISP_PAD_SINK_PARAMS,
105 RKISP1_ISP_PAD_SOURCE_VIDEO,
106 RKISP1_ISP_PAD_SOURCE_STATS,
111 * enum rkisp1_feature - ISP features
113 * @RKISP1_FEATURE_MIPI_CSI2: The ISP has an internal MIPI CSI-2 receiver
114 * @RKISP1_FEATURE_MAIN_STRIDE: The ISP supports configurable stride on the main path
115 * @RKISP1_FEATURE_SELF_PATH: The ISP has a self path
116 * @RKISP1_FEATURE_DUAL_CROP: The ISP has the dual crop block at the resizer input
117 * @RKISP1_FEATURE_DMA_34BIT: The ISP uses 34-bit DMA addresses
119 * The ISP features are stored in a bitmask in &rkisp1_info.features and allow
120 * the driver to implement support for features present in some ISP versions
123 enum rkisp1_feature {
124 RKISP1_FEATURE_MIPI_CSI2 = BIT(0),
125 RKISP1_FEATURE_MAIN_STRIDE = BIT(1),
126 RKISP1_FEATURE_SELF_PATH = BIT(2),
127 RKISP1_FEATURE_DUAL_CROP = BIT(3),
128 RKISP1_FEATURE_DMA_34BIT = BIT(4),
131 #define rkisp1_has_feature(rkisp1, feature) \
132 ((rkisp1)->info->features & RKISP1_FEATURE_##feature)
135 * struct rkisp1_info - Model-specific ISP Information
137 * @clks: array of ISP clock names
138 * @clk_size: number of entries in the @clks array
139 * @isrs: array of ISP interrupt descriptors
140 * @isr_size: number of entries in the @isrs array
141 * @isp_ver: ISP version
142 * @features: bitmask of rkisp1_feature features implemented by the ISP
144 * This structure contains information about the ISP specific to a particular
145 * ISP model, version, or integration in a particular SoC.
148 const char * const *clks;
149 unsigned int clk_size;
150 const struct rkisp1_isr_data *isrs;
151 unsigned int isr_size;
152 enum rkisp1_cif_isp_version isp_ver;
153 unsigned int features;
157 * struct rkisp1_sensor_async - A container for the v4l2_async_subdev to add to the notifier
158 * of the v4l2-async API
160 * @asd: async_subdev variable for the sensor
161 * @index: index of the sensor (counting sensor found in DT)
162 * @source_ep: fwnode for the sensor source endpoint
163 * @lanes: number of lanes
164 * @mbus_type: type of bus (currently only CSI2 is supported)
165 * @mbus_flags: media bus (V4L2_MBUS_*) flags
166 * @sd: a pointer to v4l2_subdev struct of the sensor
167 * @pixel_rate_ctrl: pixel rate of the sensor, used to initialize the phy
168 * @port: port number (0: MIPI, 1: Parallel)
170 struct rkisp1_sensor_async {
171 struct v4l2_async_connection asd;
173 struct fwnode_handle *source_ep;
175 enum v4l2_mbus_type mbus_type;
176 unsigned int mbus_flags;
177 struct v4l2_subdev *sd;
178 struct v4l2_ctrl *pixel_rate_ctrl;
183 * struct rkisp1_csi - CSI receiver subdev
185 * @rkisp1: pointer to the rkisp1 device
186 * @dphy: a pointer to the phy
187 * @is_dphy_errctrl_disabled: if dphy errctrl is disabled (avoid endless interrupt)
188 * @sd: v4l2_subdev variable
190 * @source: source in-use, set when starting streaming
193 struct rkisp1_device *rkisp1;
195 bool is_dphy_errctrl_disabled;
196 struct v4l2_subdev sd;
197 struct media_pad pads[RKISP1_CSI_PAD_NUM];
198 struct v4l2_subdev *source;
202 * struct rkisp1_isp - ISP subdev entity
204 * @sd: v4l2_subdev variable
205 * @rkisp1: pointer to rkisp1_device
207 * @sink_fmt: input format
208 * @frame_sequence: used to synchronize frame_id between video devices.
211 struct v4l2_subdev sd;
212 struct rkisp1_device *rkisp1;
213 struct media_pad pads[RKISP1_ISP_PAD_MAX];
214 const struct rkisp1_mbus_info *sink_fmt;
215 __u32 frame_sequence;
219 * struct rkisp1_vdev_node - Container for the video nodes: params, stats, mainpath, selfpath
221 * @buf_queue: queue of buffers
222 * @vlock: lock of the video node
226 struct rkisp1_vdev_node {
227 struct vb2_queue buf_queue;
228 struct mutex vlock; /* ioctl serialization mutex */
229 struct video_device vdev;
230 struct media_pad pad;
234 * struct rkisp1_buffer - A container for the vb2 buffers used by the video devices:
235 * params, stats, mainpath, selfpath
238 * @queue: entry of the buffer in the queue
239 * @buff_addr: dma addresses of each plane, used only by the capture devices: selfpath, mainpath
241 struct rkisp1_buffer {
242 struct vb2_v4l2_buffer vb;
243 struct list_head queue;
244 dma_addr_t buff_addr[VIDEO_MAX_PLANES];
248 * struct rkisp1_dummy_buffer - A buffer to write the next frame to in case
249 * there are no vb2 buffers available.
251 * @vaddr: return value of call to dma_alloc_attrs.
252 * @dma_addr: dma address of the buffer.
253 * @size: size of the buffer.
255 struct rkisp1_dummy_buffer {
261 struct rkisp1_device;
264 * struct rkisp1_capture - ISP capture video device
267 * @rkisp1: pointer to rkisp1_device
268 * @id: id of the capture, one of RKISP1_SELFPATH, RKISP1_MAINPATH
269 * @ops: list of callbacks to configure the capture device.
270 * @config: a pointer to the list of registers to configure the capture format.
271 * @is_streaming: device is streaming
272 * @is_stopping: stop_streaming callback was called and the device is in the process of
273 * stopping the streaming.
274 * @done: when stop_streaming callback is called, the device waits for the next irq
275 * handler to stop the streaming by waiting on the 'done' wait queue.
276 * If the irq handler is not called, the stream is stopped by the callback
278 * @stride: the line stride for the first plane, in pixel units
279 * @buf.lock: lock to protect buf.queue
280 * @buf.queue: queued buffer list
281 * @buf.dummy: dummy space to store dropped data
283 * rkisp1 uses shadow registers, so it needs two buffers at a time
284 * @buf.curr: the buffer used for current frame
285 * @buf.next: the buffer used for next frame
286 * @pix.cfg: pixel configuration
287 * @pix.info: a pointer to the v4l2_format_info of the pixel format
288 * @pix.fmt: buffer format
290 struct rkisp1_capture {
291 struct rkisp1_vdev_node vnode;
292 struct rkisp1_device *rkisp1;
293 enum rkisp1_stream_id id;
294 const struct rkisp1_capture_ops *ops;
295 const struct rkisp1_capture_config *config;
298 wait_queue_head_t done;
301 /* protects queue, curr and next */
303 struct list_head queue;
304 struct rkisp1_dummy_buffer dummy;
305 struct rkisp1_buffer *curr;
306 struct rkisp1_buffer *next;
309 const struct rkisp1_capture_fmt_cfg *cfg;
310 const struct v4l2_format_info *info;
311 struct v4l2_pix_format_mplane fmt;
316 struct rkisp1_stats_ops {
317 void (*get_awb_meas)(struct rkisp1_stats *stats,
318 struct rkisp1_stat_buffer *pbuf);
319 void (*get_aec_meas)(struct rkisp1_stats *stats,
320 struct rkisp1_stat_buffer *pbuf);
321 void (*get_hst_meas)(struct rkisp1_stats *stats,
322 struct rkisp1_stat_buffer *pbuf);
326 * struct rkisp1_stats - ISP Statistics device
329 * @rkisp1: pointer to the rkisp1 device
330 * @lock: locks the buffer list 'stat'
331 * @stat: queue of rkisp1_buffer
332 * @vdev_fmt: v4l2_format of the metadata format
334 struct rkisp1_stats {
335 struct rkisp1_vdev_node vnode;
336 struct rkisp1_device *rkisp1;
337 const struct rkisp1_stats_ops *ops;
339 spinlock_t lock; /* locks the buffers list 'stats' */
340 struct list_head stat;
341 struct v4l2_format vdev_fmt;
344 struct rkisp1_params;
345 struct rkisp1_params_ops {
346 void (*lsc_matrix_config)(struct rkisp1_params *params,
347 const struct rkisp1_cif_isp_lsc_config *pconfig);
348 void (*goc_config)(struct rkisp1_params *params,
349 const struct rkisp1_cif_isp_goc_config *arg);
350 void (*awb_meas_config)(struct rkisp1_params *params,
351 const struct rkisp1_cif_isp_awb_meas_config *arg);
352 void (*awb_meas_enable)(struct rkisp1_params *params,
353 const struct rkisp1_cif_isp_awb_meas_config *arg,
355 void (*awb_gain_config)(struct rkisp1_params *params,
356 const struct rkisp1_cif_isp_awb_gain_config *arg);
357 void (*aec_config)(struct rkisp1_params *params,
358 const struct rkisp1_cif_isp_aec_config *arg);
359 void (*hst_config)(struct rkisp1_params *params,
360 const struct rkisp1_cif_isp_hst_config *arg);
361 void (*hst_enable)(struct rkisp1_params *params,
362 const struct rkisp1_cif_isp_hst_config *arg, bool en);
363 void (*afm_config)(struct rkisp1_params *params,
364 const struct rkisp1_cif_isp_afc_config *arg);
368 * struct rkisp1_params - ISP input parameters device
371 * @rkisp1: pointer to the rkisp1 device
372 * @ops: pointer to the variant-specific operations
373 * @config_lock: locks the buffer list 'params'
374 * @params: queue of rkisp1_buffer
375 * @vdev_fmt: v4l2_format of the metadata format
376 * @quantization: the quantization configured on the isp's src pad
377 * @raw_type: the bayer pattern on the isp video sink pad
379 struct rkisp1_params {
380 struct rkisp1_vdev_node vnode;
381 struct rkisp1_device *rkisp1;
382 const struct rkisp1_params_ops *ops;
384 spinlock_t config_lock; /* locks the buffers list 'params' */
385 struct list_head params;
386 struct v4l2_format vdev_fmt;
388 enum v4l2_quantization quantization;
389 enum v4l2_ycbcr_encoding ycbcr_encoding;
390 enum rkisp1_fmt_raw_pat_type raw_type;
394 * struct rkisp1_resizer - Resizer subdev
396 * @sd: v4l2_subdev variable
397 * @regs_base: base register address offset
398 * @id: id of the resizer, one of RKISP1_SELFPATH, RKISP1_MAINPATH
399 * @rkisp1: pointer to the rkisp1 device
401 * @config: the set of registers to configure the resizer
403 struct rkisp1_resizer {
404 struct v4l2_subdev sd;
406 enum rkisp1_stream_id id;
407 struct rkisp1_device *rkisp1;
408 struct media_pad pads[RKISP1_RSZ_PAD_MAX];
409 const struct rkisp1_rsz_config *config;
413 * struct rkisp1_debug - Values to be exposed on debugfs.
414 * The parameters are counters of the number of times the
415 * event occurred since the driver was loaded.
417 * @data_loss: loss of data occurred within a line, processing failure
418 * @outform_size_error: size error is generated in outmux submodule
419 * @img_stabilization_size_error: size error is generated in image stabilization submodule
420 * @inform_size_err: size error is generated in inform submodule
421 * @mipi_error: mipi error occurred
422 * @stats_error: writing to the 'Interrupt clear register' did not clear
423 * it in the register 'Masked interrupt status'
424 * @stop_timeout: upon stream stop, the capture waits 1 second for the isr to stop
425 * the stream. This param is incremented in case of timeout.
426 * @frame_drop: a frame was ready but the buffer queue was empty so the frame
427 * was not sent to userspace
429 struct rkisp1_debug {
430 struct dentry *debugfs_dir;
431 unsigned long data_loss;
432 unsigned long outform_size_error;
433 unsigned long img_stabilization_size_error;
434 unsigned long inform_size_error;
435 unsigned long irq_delay;
436 unsigned long mipi_error;
437 unsigned long stats_error;
438 unsigned long stop_timeout[2];
439 unsigned long frame_drop[2];
440 unsigned long complete_frames;
444 * struct rkisp1_device - ISP platform device
446 * @base_addr: base register address
447 * @dev: a pointer to the struct device
448 * @clk_size: number of clocks
449 * @clks: array of clocks
450 * @gasket: the gasket - i.MX8MP only
451 * @gasket_id: the gasket ID (0 or 1) - i.MX8MP only
452 * @v4l2_dev: v4l2_device variable
453 * @media_dev: media_device variable
454 * @notifier: a notifier to register on the v4l2-async API to be notified on the sensor
455 * @source: source subdev in-use, set when starting streaming
456 * @csi: internal CSI-2 receiver
457 * @isp: ISP sub-device
458 * @resizer_devs: resizer sub-devices
459 * @capture_devs: capture devices
460 * @stats: ISP statistics metadata capture device
461 * @params: ISP parameters metadata output device
462 * @pipe: media pipeline
463 * @stream_lock: serializes {start/stop}_streaming callbacks between the capture devices.
464 * @debug: debug params to be exposed on debugfs
465 * @info: version-specific ISP information
466 * @irqs: IRQ line numbers
467 * @irqs_enabled: the hardware is enabled and can cause interrupts
469 struct rkisp1_device {
470 void __iomem *base_addr;
472 unsigned int clk_size;
473 struct clk_bulk_data clks[RKISP1_MAX_BUS_CLK];
474 struct regmap *gasket;
475 unsigned int gasket_id;
476 struct v4l2_device v4l2_dev;
477 struct media_device media_dev;
478 struct v4l2_async_notifier notifier;
479 struct v4l2_subdev *source;
480 struct rkisp1_csi csi;
481 struct rkisp1_isp isp;
482 struct rkisp1_resizer resizer_devs[2];
483 struct rkisp1_capture capture_devs[2];
484 struct rkisp1_stats stats;
485 struct rkisp1_params params;
486 struct media_pipeline pipe;
487 struct mutex stream_lock; /* serialize {start/stop}_streaming cb between capture devices */
488 struct rkisp1_debug debug;
489 const struct rkisp1_info *info;
490 int irqs[RKISP1_NUM_IRQS];
495 * struct rkisp1_mbus_info - ISP media bus info, Translates media bus code to hardware
498 * @mbus_code: media bus code
499 * @pixel_enc: pixel encoding
500 * @mipi_dt: mipi data type
501 * @yuv_seq: the order of the Y, Cb, Cr values
502 * @bus_width: bus width
503 * @bayer_pat: bayer pattern
504 * @direction: a bitmask of the flags indicating on which pad the format is supported on
506 struct rkisp1_mbus_info {
508 enum v4l2_pixel_encoding pixel_enc;
512 enum rkisp1_fmt_raw_pat_type bayer_pat;
513 unsigned int direction;
517 rkisp1_write(struct rkisp1_device *rkisp1, unsigned int addr, u32 val)
519 writel(val, rkisp1->base_addr + addr);
522 static inline u32 rkisp1_read(struct rkisp1_device *rkisp1, unsigned int addr)
524 return readl(rkisp1->base_addr + addr);
528 * rkisp1_cap_enum_mbus_codes - A helper function that return the i'th supported mbus code
529 * of the capture entity. This is used to enumerate the supported
530 * mbus codes on the source pad of the resizer.
532 * @cap: the capture entity
533 * @code: the mbus code, the function reads the code->index and fills the code->code
535 int rkisp1_cap_enum_mbus_codes(struct rkisp1_capture *cap,
536 struct v4l2_subdev_mbus_code_enum *code);
539 * rkisp1_mbus_info_get_by_index - Retrieve the ith supported mbus info
541 * @index: index of the mbus info to fetch
543 const struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_index(unsigned int index);
546 * rkisp1_path_count - Return the number of paths supported by the device
548 * Some devices only have a main path, while other device have both a main path
549 * and a self path. This function returns the number of paths that this device
550 * has, based on the feature flags. It should be used insted of checking
551 * ARRAY_SIZE of capture_devs/resizer_devs.
553 static inline unsigned int rkisp1_path_count(struct rkisp1_device *rkisp1)
555 return rkisp1_has_feature(rkisp1, SELF_PATH) ? 2 : 1;
559 * rkisp1_sd_adjust_crop_rect - adjust a rectangle to fit into another rectangle.
561 * @crop: rectangle to adjust.
562 * @bounds: rectangle used as bounds.
564 void rkisp1_sd_adjust_crop_rect(struct v4l2_rect *crop,
565 const struct v4l2_rect *bounds);
568 * rkisp1_sd_adjust_crop - adjust a rectangle to fit into media bus format
570 * @crop: rectangle to adjust.
571 * @bounds: media bus format used as bounds.
573 void rkisp1_sd_adjust_crop(struct v4l2_rect *crop,
574 const struct v4l2_mbus_framefmt *bounds);
577 * rkisp1_mbus_info_get_by_code - get the isp info of the media bus code
579 * @mbus_code: the media bus code
581 const struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_code(u32 mbus_code);
584 * rkisp1_params_pre_configure - Configure the params before stream start
586 * @params: pointer to rkisp1_params
587 * @bayer_pat: the bayer pattern on the isp video sink pad
588 * @quantization: the quantization configured on the isp's src pad
589 * @ycbcr_encoding: the ycbcr_encoding configured on the isp's src pad
591 * This function is called by the ISP entity just before the ISP gets started.
592 * It applies the initial ISP parameters from the first params buffer, but
593 * skips LSC as it needs to be configured after the ISP is started.
595 void rkisp1_params_pre_configure(struct rkisp1_params *params,
596 enum rkisp1_fmt_raw_pat_type bayer_pat,
597 enum v4l2_quantization quantization,
598 enum v4l2_ycbcr_encoding ycbcr_encoding);
601 * rkisp1_params_post_configure - Configure the params after stream start
603 * @params: pointer to rkisp1_params
605 * This function is called by the ISP entity just after the ISP gets started.
606 * It applies the initial ISP LSC parameters from the first params buffer.
608 void rkisp1_params_post_configure(struct rkisp1_params *params);
610 /* rkisp1_params_disable - disable all parameters.
611 * This function is called by the isp entity upon stream start
612 * when capturing bayer format.
614 * @params: pointer to rkisp1_params.
616 void rkisp1_params_disable(struct rkisp1_params *params);
619 irqreturn_t rkisp1_isp_isr(int irq, void *ctx);
620 irqreturn_t rkisp1_csi_isr(int irq, void *ctx);
621 irqreturn_t rkisp1_capture_isr(int irq, void *ctx);
622 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris);
623 void rkisp1_params_isr(struct rkisp1_device *rkisp1);
625 /* register/unregisters functions of the entities */
626 int rkisp1_capture_devs_register(struct rkisp1_device *rkisp1);
627 void rkisp1_capture_devs_unregister(struct rkisp1_device *rkisp1);
629 int rkisp1_isp_register(struct rkisp1_device *rkisp1);
630 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);
632 int rkisp1_resizer_devs_register(struct rkisp1_device *rkisp1);
633 void rkisp1_resizer_devs_unregister(struct rkisp1_device *rkisp1);
635 int rkisp1_stats_register(struct rkisp1_device *rkisp1);
636 void rkisp1_stats_unregister(struct rkisp1_device *rkisp1);
638 int rkisp1_params_register(struct rkisp1_device *rkisp1);
639 void rkisp1_params_unregister(struct rkisp1_device *rkisp1);
641 #if IS_ENABLED(CONFIG_DEBUG_FS)
642 void rkisp1_debug_init(struct rkisp1_device *rkisp1);
643 void rkisp1_debug_cleanup(struct rkisp1_device *rkisp1);
645 static inline void rkisp1_debug_init(struct rkisp1_device *rkisp1)
648 static inline void rkisp1_debug_cleanup(struct rkisp1_device *rkisp1)
653 #endif /* _RKISP1_COMMON_H */