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"
29 * flags on the 'direction' field in struct rkisp1_mbus_info' that indicate
30 * on which pad the media bus format is supported
32 #define RKISP1_ISP_SD_SRC BIT(0)
33 #define RKISP1_ISP_SD_SINK BIT(1)
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
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
48 /* the default width and height of all the entities */
49 #define RKISP1_DEFAULT_WIDTH 800
50 #define RKISP1_DEFAULT_HEIGHT 600
52 #define RKISP1_DRIVER_NAME "rkisp1"
53 #define RKISP1_BUS_INFO "platform:" RKISP1_DRIVER_NAME
55 /* maximum number of clocks */
56 #define RKISP1_MAX_BUS_CLK 8
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)
64 /* enum for the resizer pads */
71 /* enum for the csi receiver pads */
78 /* enum for the capture id */
79 enum rkisp1_stream_id {
85 enum rkisp1_fmt_raw_pat_type {
92 /* enum for the isp pads */
94 RKISP1_ISP_PAD_SINK_VIDEO,
95 RKISP1_ISP_PAD_SINK_PARAMS,
96 RKISP1_ISP_PAD_SOURCE_VIDEO,
97 RKISP1_ISP_PAD_SOURCE_STATS,
102 * enum rkisp1_feature - ISP features
104 * @RKISP1_FEATURE_MIPI_CSI2: The ISP has an internal MIPI CSI-2 receiver
106 * The ISP features are stored in a bitmask in &rkisp1_info.features and allow
107 * the driver to implement support for features present in some ISP versions
110 enum rkisp1_feature {
111 RKISP1_FEATURE_MIPI_CSI2 = BIT(0),
115 * struct rkisp1_info - Model-specific ISP Information
117 * @clks: array of ISP clock names
118 * @clk_size: number of entries in the @clks array
119 * @isrs: array of ISP interrupt descriptors
120 * @isr_size: number of entries in the @isrs array
121 * @isp_ver: ISP version
122 * @features: bitmask of rkisp1_feature features implemented by the ISP
124 * This structure contains information about the ISP specific to a particular
125 * ISP model, version, or integration in a particular SoC.
128 const char * const *clks;
129 unsigned int clk_size;
130 const struct rkisp1_isr_data *isrs;
131 unsigned int isr_size;
132 enum rkisp1_cif_isp_version isp_ver;
133 unsigned int features;
137 * struct rkisp1_sensor_async - A container for the v4l2_async_subdev to add to the notifier
138 * of the v4l2-async API
140 * @asd: async_subdev variable for the sensor
141 * @index: index of the sensor (counting sensor found in DT)
142 * @source_ep: fwnode for the sensor source endpoint
143 * @lanes: number of lanes
144 * @mbus_type: type of bus (currently only CSI2 is supported)
145 * @mbus_flags: media bus (V4L2_MBUS_*) flags
146 * @sd: a pointer to v4l2_subdev struct of the sensor
147 * @pixel_rate_ctrl: pixel rate of the sensor, used to initialize the phy
148 * @port: port number (0: MIPI, 1: Parallel)
150 struct rkisp1_sensor_async {
151 struct v4l2_async_connection asd;
153 struct fwnode_handle *source_ep;
155 enum v4l2_mbus_type mbus_type;
156 unsigned int mbus_flags;
157 struct v4l2_subdev *sd;
158 struct v4l2_ctrl *pixel_rate_ctrl;
163 * struct rkisp1_csi - CSI receiver subdev
165 * @rkisp1: pointer to the rkisp1 device
166 * @dphy: a pointer to the phy
167 * @is_dphy_errctrl_disabled: if dphy errctrl is disabled (avoid endless interrupt)
168 * @sd: v4l2_subdev variable
170 * @source: source in-use, set when starting streaming
173 struct rkisp1_device *rkisp1;
175 bool is_dphy_errctrl_disabled;
176 struct v4l2_subdev sd;
177 struct media_pad pads[RKISP1_CSI_PAD_NUM];
178 struct v4l2_subdev *source;
182 * struct rkisp1_isp - ISP subdev entity
184 * @sd: v4l2_subdev variable
185 * @rkisp1: pointer to rkisp1_device
187 * @sink_fmt: input format
188 * @frame_sequence: used to synchronize frame_id between video devices.
191 struct v4l2_subdev sd;
192 struct rkisp1_device *rkisp1;
193 struct media_pad pads[RKISP1_ISP_PAD_MAX];
194 const struct rkisp1_mbus_info *sink_fmt;
195 __u32 frame_sequence;
199 * struct rkisp1_vdev_node - Container for the video nodes: params, stats, mainpath, selfpath
201 * @buf_queue: queue of buffers
202 * @vlock: lock of the video node
206 struct rkisp1_vdev_node {
207 struct vb2_queue buf_queue;
208 struct mutex vlock; /* ioctl serialization mutex */
209 struct video_device vdev;
210 struct media_pad pad;
214 * struct rkisp1_buffer - A container for the vb2 buffers used by the video devices:
215 * params, stats, mainpath, selfpath
218 * @queue: entry of the buffer in the queue
219 * @buff_addr: dma addresses of each plane, used only by the capture devices: selfpath, mainpath
221 struct rkisp1_buffer {
222 struct vb2_v4l2_buffer vb;
223 struct list_head queue;
224 u32 buff_addr[VIDEO_MAX_PLANES];
228 * struct rkisp1_dummy_buffer - A buffer to write the next frame to in case
229 * there are no vb2 buffers available.
231 * @vaddr: return value of call to dma_alloc_attrs.
232 * @dma_addr: dma address of the buffer.
233 * @size: size of the buffer.
235 struct rkisp1_dummy_buffer {
241 struct rkisp1_device;
244 * struct rkisp1_capture - ISP capture video device
247 * @rkisp1: pointer to rkisp1_device
248 * @id: id of the capture, one of RKISP1_SELFPATH, RKISP1_MAINPATH
249 * @ops: list of callbacks to configure the capture device.
250 * @config: a pointer to the list of registers to configure the capture format.
251 * @is_streaming: device is streaming
252 * @is_stopping: stop_streaming callback was called and the device is in the process of
253 * stopping the streaming.
254 * @done: when stop_streaming callback is called, the device waits for the next irq
255 * handler to stop the streaming by waiting on the 'done' wait queue.
256 * If the irq handler is not called, the stream is stopped by the callback
258 * @sp_y_stride: the selfpath allows to configure a y stride that is longer than the image width.
259 * @buf.lock: lock to protect buf.queue
260 * @buf.queue: queued buffer list
261 * @buf.dummy: dummy space to store dropped data
263 * rkisp1 uses shadow registers, so it needs two buffers at a time
264 * @buf.curr: the buffer used for current frame
265 * @buf.next: the buffer used for next frame
266 * @pix.cfg: pixel configuration
267 * @pix.info: a pointer to the v4l2_format_info of the pixel format
268 * @pix.fmt: buffer format
270 struct rkisp1_capture {
271 struct rkisp1_vdev_node vnode;
272 struct rkisp1_device *rkisp1;
273 enum rkisp1_stream_id id;
274 const struct rkisp1_capture_ops *ops;
275 const struct rkisp1_capture_config *config;
278 wait_queue_head_t done;
279 unsigned int sp_y_stride;
281 /* protects queue, curr and next */
283 struct list_head queue;
284 struct rkisp1_dummy_buffer dummy;
285 struct rkisp1_buffer *curr;
286 struct rkisp1_buffer *next;
289 const struct rkisp1_capture_fmt_cfg *cfg;
290 const struct v4l2_format_info *info;
291 struct v4l2_pix_format_mplane fmt;
296 struct rkisp1_stats_ops {
297 void (*get_awb_meas)(struct rkisp1_stats *stats,
298 struct rkisp1_stat_buffer *pbuf);
299 void (*get_aec_meas)(struct rkisp1_stats *stats,
300 struct rkisp1_stat_buffer *pbuf);
301 void (*get_hst_meas)(struct rkisp1_stats *stats,
302 struct rkisp1_stat_buffer *pbuf);
306 * struct rkisp1_stats - ISP Statistics device
309 * @rkisp1: pointer to the rkisp1 device
310 * @lock: locks the buffer list 'stat'
311 * @stat: queue of rkisp1_buffer
312 * @vdev_fmt: v4l2_format of the metadata format
314 struct rkisp1_stats {
315 struct rkisp1_vdev_node vnode;
316 struct rkisp1_device *rkisp1;
317 const struct rkisp1_stats_ops *ops;
319 spinlock_t lock; /* locks the buffers list 'stats' */
320 struct list_head stat;
321 struct v4l2_format vdev_fmt;
324 struct rkisp1_params;
325 struct rkisp1_params_ops {
326 void (*lsc_matrix_config)(struct rkisp1_params *params,
327 const struct rkisp1_cif_isp_lsc_config *pconfig);
328 void (*goc_config)(struct rkisp1_params *params,
329 const struct rkisp1_cif_isp_goc_config *arg);
330 void (*awb_meas_config)(struct rkisp1_params *params,
331 const struct rkisp1_cif_isp_awb_meas_config *arg);
332 void (*awb_meas_enable)(struct rkisp1_params *params,
333 const struct rkisp1_cif_isp_awb_meas_config *arg,
335 void (*awb_gain_config)(struct rkisp1_params *params,
336 const struct rkisp1_cif_isp_awb_gain_config *arg);
337 void (*aec_config)(struct rkisp1_params *params,
338 const struct rkisp1_cif_isp_aec_config *arg);
339 void (*hst_config)(struct rkisp1_params *params,
340 const struct rkisp1_cif_isp_hst_config *arg);
341 void (*hst_enable)(struct rkisp1_params *params,
342 const struct rkisp1_cif_isp_hst_config *arg, bool en);
343 void (*afm_config)(struct rkisp1_params *params,
344 const struct rkisp1_cif_isp_afc_config *arg);
348 * struct rkisp1_params - ISP input parameters device
351 * @rkisp1: pointer to the rkisp1 device
352 * @ops: pointer to the variant-specific operations
353 * @config_lock: locks the buffer list 'params'
354 * @params: queue of rkisp1_buffer
355 * @vdev_fmt: v4l2_format of the metadata format
356 * @quantization: the quantization configured on the isp's src pad
357 * @raw_type: the bayer pattern on the isp video sink pad
359 struct rkisp1_params {
360 struct rkisp1_vdev_node vnode;
361 struct rkisp1_device *rkisp1;
362 const struct rkisp1_params_ops *ops;
364 spinlock_t config_lock; /* locks the buffers list 'params' */
365 struct list_head params;
366 struct v4l2_format vdev_fmt;
368 enum v4l2_quantization quantization;
369 enum v4l2_ycbcr_encoding ycbcr_encoding;
370 enum rkisp1_fmt_raw_pat_type raw_type;
374 * struct rkisp1_resizer - Resizer subdev
376 * @sd: v4l2_subdev variable
377 * @regs_base: base register address offset
378 * @id: id of the resizer, one of RKISP1_SELFPATH, RKISP1_MAINPATH
379 * @rkisp1: pointer to the rkisp1 device
381 * @config: the set of registers to configure the resizer
383 struct rkisp1_resizer {
384 struct v4l2_subdev sd;
386 enum rkisp1_stream_id id;
387 struct rkisp1_device *rkisp1;
388 struct media_pad pads[RKISP1_RSZ_PAD_MAX];
389 const struct rkisp1_rsz_config *config;
393 * struct rkisp1_debug - Values to be exposed on debugfs.
394 * The parameters are counters of the number of times the
395 * event occurred since the driver was loaded.
397 * @data_loss: loss of data occurred within a line, processing failure
398 * @outform_size_error: size error is generated in outmux submodule
399 * @img_stabilization_size_error: size error is generated in image stabilization submodule
400 * @inform_size_err: size error is generated in inform submodule
401 * @mipi_error: mipi error occurred
402 * @stats_error: writing to the 'Interrupt clear register' did not clear
403 * it in the register 'Masked interrupt status'
404 * @stop_timeout: upon stream stop, the capture waits 1 second for the isr to stop
405 * the stream. This param is incremented in case of timeout.
406 * @frame_drop: a frame was ready but the buffer queue was empty so the frame
407 * was not sent to userspace
409 struct rkisp1_debug {
410 struct dentry *debugfs_dir;
411 unsigned long data_loss;
412 unsigned long outform_size_error;
413 unsigned long img_stabilization_size_error;
414 unsigned long inform_size_error;
415 unsigned long irq_delay;
416 unsigned long mipi_error;
417 unsigned long stats_error;
418 unsigned long stop_timeout[2];
419 unsigned long frame_drop[2];
423 * struct rkisp1_device - ISP platform device
425 * @base_addr: base register address
426 * @irq: the irq number
427 * @dev: a pointer to the struct device
428 * @clk_size: number of clocks
429 * @clks: array of clocks
430 * @v4l2_dev: v4l2_device variable
431 * @media_dev: media_device variable
432 * @notifier: a notifier to register on the v4l2-async API to be notified on the sensor
433 * @source: source subdev in-use, set when starting streaming
434 * @csi: internal CSI-2 receiver
435 * @isp: ISP sub-device
436 * @resizer_devs: resizer sub-devices
437 * @capture_devs: capture devices
438 * @stats: ISP statistics metadata capture device
439 * @params: ISP parameters metadata output device
440 * @pipe: media pipeline
441 * @stream_lock: serializes {start/stop}_streaming callbacks between the capture devices.
442 * @debug: debug params to be exposed on debugfs
443 * @info: version-specific ISP information
445 struct rkisp1_device {
446 void __iomem *base_addr;
448 unsigned int clk_size;
449 struct clk_bulk_data clks[RKISP1_MAX_BUS_CLK];
450 struct v4l2_device v4l2_dev;
451 struct media_device media_dev;
452 struct v4l2_async_notifier notifier;
453 struct v4l2_subdev *source;
454 struct rkisp1_csi csi;
455 struct rkisp1_isp isp;
456 struct rkisp1_resizer resizer_devs[2];
457 struct rkisp1_capture capture_devs[2];
458 struct rkisp1_stats stats;
459 struct rkisp1_params params;
460 struct media_pipeline pipe;
461 struct mutex stream_lock; /* serialize {start/stop}_streaming cb between capture devices */
462 struct rkisp1_debug debug;
463 const struct rkisp1_info *info;
467 * struct rkisp1_mbus_info - ISP media bus info, Translates media bus code to hardware
470 * @mbus_code: media bus code
471 * @pixel_enc: pixel encoding
472 * @mipi_dt: mipi data type
473 * @yuv_seq: the order of the Y, Cb, Cr values
474 * @bus_width: bus width
475 * @bayer_pat: bayer pattern
476 * @direction: a bitmask of the flags indicating on which pad the format is supported on
478 struct rkisp1_mbus_info {
480 enum v4l2_pixel_encoding pixel_enc;
484 enum rkisp1_fmt_raw_pat_type bayer_pat;
485 unsigned int direction;
489 rkisp1_write(struct rkisp1_device *rkisp1, unsigned int addr, u32 val)
491 writel(val, rkisp1->base_addr + addr);
494 static inline u32 rkisp1_read(struct rkisp1_device *rkisp1, unsigned int addr)
496 return readl(rkisp1->base_addr + addr);
500 * rkisp1_cap_enum_mbus_codes - A helper function that return the i'th supported mbus code
501 * of the capture entity. This is used to enumerate the supported
502 * mbus codes on the source pad of the resizer.
504 * @cap: the capture entity
505 * @code: the mbus code, the function reads the code->index and fills the code->code
507 int rkisp1_cap_enum_mbus_codes(struct rkisp1_capture *cap,
508 struct v4l2_subdev_mbus_code_enum *code);
511 * rkisp1_mbus_info_get_by_index - Retrieve the ith supported mbus info
513 * @index: index of the mbus info to fetch
515 const struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_index(unsigned int index);
518 * rkisp1_sd_adjust_crop_rect - adjust a rectangle to fit into another rectangle.
520 * @crop: rectangle to adjust.
521 * @bounds: rectangle used as bounds.
523 void rkisp1_sd_adjust_crop_rect(struct v4l2_rect *crop,
524 const struct v4l2_rect *bounds);
527 * rkisp1_sd_adjust_crop - adjust a rectangle to fit into media bus format
529 * @crop: rectangle to adjust.
530 * @bounds: media bus format used as bounds.
532 void rkisp1_sd_adjust_crop(struct v4l2_rect *crop,
533 const struct v4l2_mbus_framefmt *bounds);
536 * rkisp1_mbus_info_get_by_code - get the isp info of the media bus code
538 * @mbus_code: the media bus code
540 const struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_code(u32 mbus_code);
543 * rkisp1_params_pre_configure - Configure the params before stream start
545 * @params: pointer to rkisp1_params
546 * @bayer_pat: the bayer pattern on the isp video sink pad
547 * @quantization: the quantization configured on the isp's src pad
548 * @ycbcr_encoding: the ycbcr_encoding configured on the isp's src pad
550 * This function is called by the ISP entity just before the ISP gets started.
551 * It applies the initial ISP parameters from the first params buffer, but
552 * skips LSC as it needs to be configured after the ISP is started.
554 void rkisp1_params_pre_configure(struct rkisp1_params *params,
555 enum rkisp1_fmt_raw_pat_type bayer_pat,
556 enum v4l2_quantization quantization,
557 enum v4l2_ycbcr_encoding ycbcr_encoding);
560 * rkisp1_params_post_configure - Configure the params after stream start
562 * @params: pointer to rkisp1_params
564 * This function is called by the ISP entity just after the ISP gets started.
565 * It applies the initial ISP LSC parameters from the first params buffer.
567 void rkisp1_params_post_configure(struct rkisp1_params *params);
569 /* rkisp1_params_disable - disable all parameters.
570 * This function is called by the isp entity upon stream start
571 * when capturing bayer format.
573 * @params: pointer to rkisp1_params.
575 void rkisp1_params_disable(struct rkisp1_params *params);
578 irqreturn_t rkisp1_isp_isr(int irq, void *ctx);
579 irqreturn_t rkisp1_csi_isr(int irq, void *ctx);
580 irqreturn_t rkisp1_capture_isr(int irq, void *ctx);
581 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris);
582 void rkisp1_params_isr(struct rkisp1_device *rkisp1);
584 /* register/unregisters functions of the entities */
585 int rkisp1_capture_devs_register(struct rkisp1_device *rkisp1);
586 void rkisp1_capture_devs_unregister(struct rkisp1_device *rkisp1);
588 int rkisp1_isp_register(struct rkisp1_device *rkisp1);
589 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);
591 int rkisp1_resizer_devs_register(struct rkisp1_device *rkisp1);
592 void rkisp1_resizer_devs_unregister(struct rkisp1_device *rkisp1);
594 int rkisp1_stats_register(struct rkisp1_device *rkisp1);
595 void rkisp1_stats_unregister(struct rkisp1_device *rkisp1);
597 int rkisp1_params_register(struct rkisp1_device *rkisp1);
598 void rkisp1_params_unregister(struct rkisp1_device *rkisp1);
600 #if IS_ENABLED(CONFIG_DEBUG_FS)
601 void rkisp1_debug_init(struct rkisp1_device *rkisp1);
602 void rkisp1_debug_cleanup(struct rkisp1_device *rkisp1);
604 static inline void rkisp1_debug_init(struct rkisp1_device *rkisp1)
607 static inline void rkisp1_debug_cleanup(struct rkisp1_device *rkisp1)
612 #endif /* _RKISP1_COMMON_H */