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