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