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