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  */
160 struct rkisp1_buffer {
161 	struct vb2_v4l2_buffer vb;
162 	struct list_head queue;
163 	u32 buff_addr[VIDEO_MAX_PLANES];
164 };
165 
166 /*
167  * struct rkisp1_dummy_buffer - A buffer to write the next frame to in case
168  *				there are no vb2 buffers available.
169  *
170  * @vaddr:	return value of call to dma_alloc_attrs.
171  * @dma_addr:	dma address of the buffer.
172  * @size:	size of the buffer.
173  */
174 struct rkisp1_dummy_buffer {
175 	void *vaddr;
176 	dma_addr_t dma_addr;
177 	u32 size;
178 };
179 
180 struct rkisp1_device;
181 
182 /*
183  * struct rkisp1_capture - ISP capture video device
184  *
185  * @vnode:	  video node
186  * @rkisp1:	  pointer to rkisp1_device
187  * @id:		  id of the capture, one of RKISP1_SELFPATH, RKISP1_MAINPATH
188  * @ops:	  list of callbacks to configure the capture device.
189  * @config:	  a pointer to the list of registers to configure the capture format.
190  * @is_streaming: device is streaming
191  * @is_stopping:  stop_streaming callback was called and the device is in the process of
192  *		  stopping the streaming.
193  * @done:	  when stop_streaming callback is called, the device waits for the next irq
194  *		  handler to stop the streaming by waiting on the 'done' wait queue.
195  *		  If the irq handler is not called, the stream is stopped by the callback
196  *		  after timeout.
197  * @sp_y_stride:  the selfpath allows to configure a y stride that is longer than the image width.
198  * @buf.lock:	  lock to protect buf.queue
199  * @buf.queue:	  queued buffer list
200  * @buf.dummy:	  dummy space to store dropped data
201  *
202  * rkisp1 uses shadow registers, so it needs two buffers at a time
203  * @buf.curr:	  the buffer used for current frame
204  * @buf.next:	  the buffer used for next frame
205  * @pix.cfg:	  pixel configuration
206  * @pix.info:	  a pointer to the v4l2_format_info of the pixel format
207  * @pix.fmt:	  buffer format
208  */
209 struct rkisp1_capture {
210 	struct rkisp1_vdev_node vnode;
211 	struct rkisp1_device *rkisp1;
212 	enum rkisp1_stream_id id;
213 	const struct rkisp1_capture_ops *ops;
214 	const struct rkisp1_capture_config *config;
215 	bool is_streaming;
216 	bool is_stopping;
217 	wait_queue_head_t done;
218 	unsigned int sp_y_stride;
219 	struct {
220 		/* protects queue, curr and next */
221 		spinlock_t lock;
222 		struct list_head queue;
223 		struct rkisp1_dummy_buffer dummy;
224 		struct rkisp1_buffer *curr;
225 		struct rkisp1_buffer *next;
226 	} buf;
227 	struct {
228 		const struct rkisp1_capture_fmt_cfg *cfg;
229 		const struct v4l2_format_info *info;
230 		struct v4l2_pix_format_mplane fmt;
231 	} pix;
232 };
233 
234 /*
235  * struct rkisp1_stats - ISP Statistics device
236  *
237  * @vnode:	  video node
238  * @rkisp1:	  pointer to the rkisp1 device
239  * @lock:	  locks the buffer list 'stat'
240  * @stat:	  queue of rkisp1_buffer
241  * @vdev_fmt:	  v4l2_format of the metadata format
242  */
243 struct rkisp1_stats {
244 	struct rkisp1_vdev_node vnode;
245 	struct rkisp1_device *rkisp1;
246 
247 	spinlock_t lock; /* locks the buffers list 'stats' */
248 	struct list_head stat;
249 	struct v4l2_format vdev_fmt;
250 };
251 
252 /*
253  * struct rkisp1_params - ISP input parameters device
254  *
255  * @vnode:		video node
256  * @rkisp1:		pointer to the rkisp1 device
257  * @config_lock:	locks the buffer list 'params'
258  * @params:		queue of rkisp1_buffer
259  * @vdev_fmt:		v4l2_format of the metadata format
260  * @quantization:	the quantization configured on the isp's src pad
261  * @raw_type:		the bayer pattern on the isp video sink pad
262  */
263 struct rkisp1_params {
264 	struct rkisp1_vdev_node vnode;
265 	struct rkisp1_device *rkisp1;
266 
267 	spinlock_t config_lock; /* locks the buffers list 'params' */
268 	struct list_head params;
269 	struct v4l2_format vdev_fmt;
270 
271 	enum v4l2_quantization quantization;
272 	enum rkisp1_fmt_raw_pat_type raw_type;
273 };
274 
275 /*
276  * struct rkisp1_resizer - Resizer subdev
277  *
278  * @sd:	       v4l2_subdev variable
279  * @id:	       id of the resizer, one of RKISP1_SELFPATH, RKISP1_MAINPATH
280  * @rkisp1:    pointer to the rkisp1 device
281  * @pads:      media pads
282  * @pad_cfg:   configurations for the pads
283  * @config:    the set of registers to configure the resizer
284  * @pixel_enc: pixel encoding of the resizer
285  * @ops_lock:  a lock for the subdev ops
286  */
287 struct rkisp1_resizer {
288 	struct v4l2_subdev sd;
289 	enum rkisp1_stream_id id;
290 	struct rkisp1_device *rkisp1;
291 	struct media_pad pads[RKISP1_RSZ_PAD_MAX];
292 	struct v4l2_subdev_pad_config pad_cfg[RKISP1_RSZ_PAD_MAX];
293 	const struct rkisp1_rsz_config *config;
294 	enum v4l2_pixel_encoding pixel_enc;
295 	struct mutex ops_lock; /* serialize the subdevice ops */
296 };
297 
298 /*
299  * struct rkisp1_debug - Values to be exposed on debugfs.
300  *			 The parameters are counters of the number of times the
301  *			 event occurred since the driver was loaded.
302  *
303  * @data_loss:			  loss of data occurred within a line, processing failure
304  * @outform_size_error:		  size error is generated in outmux submodule
305  * @img_stabilization_size_error: size error is generated in image stabilization submodule
306  * @inform_size_err:		  size error is generated in inform submodule
307  * @mipi_error:			  mipi error occurred
308  * @stats_error:		  writing to the 'Interrupt clear register' did not clear
309  *				  it in the register 'Masked interrupt status'
310  * @stop_timeout:		  upon stream stop, the capture waits 1 second for the isr to stop
311  *				  the stream. This param is incremented in case of timeout.
312  * @frame_drop:			  a frame was ready but the buffer queue was empty so the frame
313  *				  was not sent to userspace
314  */
315 struct rkisp1_debug {
316 	struct dentry *debugfs_dir;
317 	unsigned long data_loss;
318 	unsigned long outform_size_error;
319 	unsigned long img_stabilization_size_error;
320 	unsigned long inform_size_error;
321 	unsigned long irq_delay;
322 	unsigned long mipi_error;
323 	unsigned long stats_error;
324 	unsigned long stop_timeout[2];
325 	unsigned long frame_drop[2];
326 };
327 
328 /*
329  * struct rkisp1_device - ISP platform device
330  *
331  * @base_addr:	   base register address
332  * @irq:	   the irq number
333  * @dev:	   a pointer to the struct device
334  * @clk_size:	   number of clocks
335  * @clks:	   array of clocks
336  * @v4l2_dev:	   v4l2_device variable
337  * @media_dev:	   media_device variable
338  * @notifier:	   a notifier to register on the v4l2-async API to be notified on the sensor
339  * @active_sensor: sensor in-use, set when streaming on
340  * @isp:	   ISP sub-device
341  * @resizer_devs:  resizer sub-devices
342  * @capture_devs:  capture devices
343  * @stats:	   ISP statistics metadata capture device
344  * @params:	   ISP parameters metadata output device
345  * @pipe:	   media pipeline
346  * @stream_lock:   serializes {start/stop}_streaming callbacks between the capture devices.
347  * @debug:	   debug params to be exposed on debugfs
348  */
349 struct rkisp1_device {
350 	void __iomem *base_addr;
351 	int irq;
352 	struct device *dev;
353 	unsigned int clk_size;
354 	struct clk_bulk_data clks[RKISP1_MAX_BUS_CLK];
355 	struct v4l2_device v4l2_dev;
356 	struct media_device media_dev;
357 	struct v4l2_async_notifier notifier;
358 	struct rkisp1_sensor_async *active_sensor;
359 	struct rkisp1_isp isp;
360 	struct rkisp1_resizer resizer_devs[2];
361 	struct rkisp1_capture capture_devs[2];
362 	struct rkisp1_stats stats;
363 	struct rkisp1_params params;
364 	struct media_pipeline pipe;
365 	struct mutex stream_lock; /* serialize {start/stop}_streaming cb between capture devices */
366 	struct rkisp1_debug debug;
367 };
368 
369 /*
370  * struct rkisp1_isp_mbus_info - ISP media bus info, Translates media bus code to hardware
371  *				 format values
372  *
373  * @mbus_code: media bus code
374  * @pixel_enc: pixel encoding
375  * @mipi_dt:   mipi data type
376  * @yuv_seq:   the order of the Y, Cb, Cr values
377  * @bus_width: bus width
378  * @bayer_pat: bayer pattern
379  * @direction: a bitmask of the flags indicating on which pad the format is supported on
380  */
381 struct rkisp1_isp_mbus_info {
382 	u32 mbus_code;
383 	enum v4l2_pixel_encoding pixel_enc;
384 	u32 mipi_dt;
385 	u32 yuv_seq;
386 	u8 bus_width;
387 	enum rkisp1_fmt_raw_pat_type bayer_pat;
388 	unsigned int direction;
389 };
390 
391 static inline void
392 rkisp1_write(struct rkisp1_device *rkisp1, u32 val, unsigned int addr)
393 {
394 	writel(val, rkisp1->base_addr + addr);
395 }
396 
397 static inline u32 rkisp1_read(struct rkisp1_device *rkisp1, unsigned int addr)
398 {
399 	return readl(rkisp1->base_addr + addr);
400 }
401 
402 /*
403  * rkisp1_cap_enum_mbus_codes - A helper function that return the i'th supported mbus code
404  *				of the capture entity. This is used to enumerate the supported
405  *				mbus codes on the source pad of the resizer.
406  *
407  * @cap:  the capture entity
408  * @code: the mbus code, the function reads the code->index and fills the code->code
409  */
410 int rkisp1_cap_enum_mbus_codes(struct rkisp1_capture *cap,
411 			       struct v4l2_subdev_mbus_code_enum *code);
412 
413 /*
414  * rkisp1_sd_adjust_crop_rect - adjust a rectangle to fit into another rectangle.
415  *
416  * @crop:   rectangle to adjust.
417  * @bounds: rectangle used as bounds.
418  */
419 void rkisp1_sd_adjust_crop_rect(struct v4l2_rect *crop,
420 				const struct v4l2_rect *bounds);
421 
422 /*
423  * rkisp1_sd_adjust_crop - adjust a rectangle to fit into media bus format
424  *
425  * @crop:   rectangle to adjust.
426  * @bounds: media bus format used as bounds.
427  */
428 void rkisp1_sd_adjust_crop(struct v4l2_rect *crop,
429 			   const struct v4l2_mbus_framefmt *bounds);
430 
431 /*
432  * rkisp1_isp_mbus_info - get the isp info of the media bus code
433  *
434  * @mbus_code: the media bus code
435  */
436 const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get(u32 mbus_code);
437 
438 /* rkisp1_params_configure - configure the params when stream starts.
439  *			     This function is called by the isp entity upon stream starts.
440  *			     The function applies the initial configuration of the parameters.
441  *
442  * @params:	  pointer to rkisp1_params.
443  * @bayer_pat:	  the bayer pattern on the isp video sink pad
444  * @quantization: the quantization configured on the isp's src pad
445  */
446 void rkisp1_params_configure(struct rkisp1_params *params,
447 			     enum rkisp1_fmt_raw_pat_type bayer_pat,
448 			     enum v4l2_quantization quantization);
449 
450 /* rkisp1_params_disable - disable all parameters.
451  *			   This function is called by the isp entity upon stream start
452  *			   when capturing bayer format.
453  *
454  * @params: pointer to rkisp1_params.
455  */
456 void rkisp1_params_disable(struct rkisp1_params *params);
457 
458 /* irq handlers */
459 void rkisp1_isp_isr(struct rkisp1_device *rkisp1);
460 void rkisp1_mipi_isr(struct rkisp1_device *rkisp1);
461 void rkisp1_capture_isr(struct rkisp1_device *rkisp1);
462 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris);
463 void rkisp1_params_isr(struct rkisp1_device *rkisp1);
464 
465 /* register/unregisters functions of the entities */
466 int rkisp1_capture_devs_register(struct rkisp1_device *rkisp1);
467 void rkisp1_capture_devs_unregister(struct rkisp1_device *rkisp1);
468 
469 int rkisp1_isp_register(struct rkisp1_device *rkisp1);
470 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);
471 
472 int rkisp1_resizer_devs_register(struct rkisp1_device *rkisp1);
473 void rkisp1_resizer_devs_unregister(struct rkisp1_device *rkisp1);
474 
475 int rkisp1_stats_register(struct rkisp1_device *rkisp1);
476 void rkisp1_stats_unregister(struct rkisp1_device *rkisp1);
477 
478 int rkisp1_params_register(struct rkisp1_device *rkisp1);
479 void rkisp1_params_unregister(struct rkisp1_device *rkisp1);
480 
481 #endif /* _RKISP1_COMMON_H */
482