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 <media/media-device.h>
17 #include <media/media-entity.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <media/videobuf2-v4l2.h>
21
22 #include "rkisp1-regs.h"
23 #include "uapi/rkisp1-config.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 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' and 'is_streaming'
244 * @stat: queue of rkisp1_buffer
245 * @vdev_fmt: v4l2_format of the metadata format
246 * @is_streaming: device is streaming
247 */
248 struct rkisp1_stats {
249 struct rkisp1_vdev_node vnode;
250 struct rkisp1_device *rkisp1;
251
252 spinlock_t lock; /* locks the buffers list 'stats' and 'is_streaming' */
253 struct list_head stat;
254 struct v4l2_format vdev_fmt;
255 bool is_streaming;
256 };
257
258 /*
259 * struct rkisp1_params - ISP input parameters device
260 *
261 * @vnode: video node
262 * @rkisp1: pointer to the rkisp1 device
263 * @config_lock: locks the buffer list 'params' and 'is_streaming'
264 * @params: queue of rkisp1_buffer
265 * @vdev_fmt: v4l2_format of the metadata format
266 * @is_streaming: device is streaming
267 * @quantization: the quantization configured on the isp's src pad
268 * @raw_type: the bayer pattern on the isp video sink pad
269 */
270 struct rkisp1_params {
271 struct rkisp1_vdev_node vnode;
272 struct rkisp1_device *rkisp1;
273
274 spinlock_t config_lock; /* locks the buffers list 'params' and 'is_streaming' */
275 struct list_head params;
276 struct v4l2_format vdev_fmt;
277 bool is_streaming;
278
279 enum v4l2_quantization quantization;
280 enum rkisp1_fmt_raw_pat_type raw_type;
281 };
282
283 /*
284 * struct rkisp1_resizer - Resizer subdev
285 *
286 * @sd: v4l2_subdev variable
287 * @id: id of the resizer, one of RKISP1_SELFPATH, RKISP1_MAINPATH
288 * @rkisp1: pointer to the rkisp1 device
289 * @pads: media pads
290 * @pad_cfg: configurations for the pads
291 * @config: the set of registers to configure the resizer
292 * @pixel_enc: pixel encoding of the resizer
293 * @ops_lock: a lock for the subdev ops
294 */
295 struct rkisp1_resizer {
296 struct v4l2_subdev sd;
297 enum rkisp1_stream_id id;
298 struct rkisp1_device *rkisp1;
299 struct media_pad pads[RKISP1_RSZ_PAD_MAX];
300 struct v4l2_subdev_pad_config pad_cfg[RKISP1_RSZ_PAD_MAX];
301 const struct rkisp1_rsz_config *config;
302 enum v4l2_pixel_encoding pixel_enc;
303 struct mutex ops_lock; /* serialize the subdevice ops */
304 };
305
306 /*
307 * struct rkisp1_debug - Values to be exposed on debugfs.
308 * The parameters are counters of the number of times the
309 * event occurred since the driver was loaded.
310 *
311 * @data_loss: loss of data occurred within a line, processing failure
312 * @outform_size_error: size error is generated in outmux submodule
313 * @img_stabilization_size_error: size error is generated in image stabilization submodule
314 * @inform_size_err: size error is generated in inform submodule
315 * @mipi_error: mipi error occurred
316 * @stats_error: writing to the 'Interrupt clear register' did not clear
317 * it in the register 'Masked interrupt status'
318 * @stop_timeout: upon stream stop, the capture waits 1 second for the isr to stop
319 * the stream. This param is incremented in case of timeout.
320 * @frame_drop: a frame was ready but the buffer queue was empty so the frame
321 * was not sent to userspace
322 */
323 struct rkisp1_debug {
324 struct dentry *debugfs_dir;
325 unsigned long data_loss;
326 unsigned long outform_size_error;
327 unsigned long img_stabilization_size_error;
328 unsigned long inform_size_error;
329 unsigned long irq_delay;
330 unsigned long mipi_error;
331 unsigned long stats_error;
332 unsigned long stop_timeout[2];
333 unsigned long frame_drop[2];
334 };
335
336 /*
337 * struct rkisp1_device - ISP platform device
338 *
339 * @base_addr: base register address
340 * @irq: the irq number
341 * @dev: a pointer to the struct device
342 * @clk_size: number of clocks
343 * @clks: array of clocks
344 * @v4l2_dev: v4l2_device variable
345 * @media_dev: media_device variable
346 * @notifier: a notifier to register on the v4l2-async API to be notified on the sensor
347 * @active_sensor: sensor in-use, set when streaming on
348 * @isp: ISP sub-device
349 * @resizer_devs: resizer sub-devices
350 * @capture_devs: capture devices
351 * @stats: ISP statistics metadata capture device
352 * @params: ISP parameters metadata output device
353 * @pipe: media pipeline
354 * @stream_lock: serializes {start/stop}_streaming callbacks between the capture devices.
355 * @debug: debug params to be exposed on debugfs
356 */
357 struct rkisp1_device {
358 void __iomem *base_addr;
359 int irq;
360 struct device *dev;
361 unsigned int clk_size;
362 struct clk_bulk_data clks[RKISP1_MAX_BUS_CLK];
363 struct v4l2_device v4l2_dev;
364 struct media_device media_dev;
365 struct v4l2_async_notifier notifier;
366 struct rkisp1_sensor_async *active_sensor;
367 struct rkisp1_isp isp;
368 struct rkisp1_resizer resizer_devs[2];
369 struct rkisp1_capture capture_devs[2];
370 struct rkisp1_stats stats;
371 struct rkisp1_params params;
372 struct media_pipeline pipe;
373 struct mutex stream_lock; /* serialize {start/stop}_streaming cb between capture devices */
374 struct rkisp1_debug debug;
375 };
376
377 /*
378 * struct rkisp1_isp_mbus_info - ISP media bus info, Translates media bus code to hardware
379 * format values
380 *
381 * @mbus_code: media bus code
382 * @pixel_enc: pixel encoding
383 * @mipi_dt: mipi data type
384 * @yuv_seq: the order of the Y, Cb, Cr values
385 * @bus_width: bus width
386 * @bayer_pat: bayer pattern
387 * @direction: a bitmask of the flags indicating on which pad the format is supported on
388 */
389 struct rkisp1_isp_mbus_info {
390 u32 mbus_code;
391 enum v4l2_pixel_encoding pixel_enc;
392 u32 mipi_dt;
393 u32 yuv_seq;
394 u8 bus_width;
395 enum rkisp1_fmt_raw_pat_type bayer_pat;
396 unsigned int direction;
397 };
398
399 static inline void
rkisp1_write(struct rkisp1_device * rkisp1,u32 val,unsigned int addr)400 rkisp1_write(struct rkisp1_device *rkisp1, u32 val, unsigned int addr)
401 {
402 writel(val, rkisp1->base_addr + addr);
403 }
404
rkisp1_read(struct rkisp1_device * rkisp1,unsigned int addr)405 static inline u32 rkisp1_read(struct rkisp1_device *rkisp1, unsigned int addr)
406 {
407 return readl(rkisp1->base_addr + addr);
408 }
409
410 /*
411 * rkisp1_cap_enum_mbus_codes - A helper function that return the i'th supported mbus code
412 * of the capture entity. This is used to enumerate the supported
413 * mbus codes on the source pad of the resizer.
414 *
415 * @cap: the capture entity
416 * @code: the mbus code, the function reads the code->index and fills the code->code
417 */
418 int rkisp1_cap_enum_mbus_codes(struct rkisp1_capture *cap,
419 struct v4l2_subdev_mbus_code_enum *code);
420
421 /*
422 * rkisp1_sd_adjust_crop_rect - adjust a rectangle to fit into another rectangle.
423 *
424 * @crop: rectangle to adjust.
425 * @bounds: rectangle used as bounds.
426 */
427 void rkisp1_sd_adjust_crop_rect(struct v4l2_rect *crop,
428 const struct v4l2_rect *bounds);
429
430 /*
431 * rkisp1_sd_adjust_crop - adjust a rectangle to fit into media bus format
432 *
433 * @crop: rectangle to adjust.
434 * @bounds: media bus format used as bounds.
435 */
436 void rkisp1_sd_adjust_crop(struct v4l2_rect *crop,
437 const struct v4l2_mbus_framefmt *bounds);
438
439 /*
440 * rkisp1_isp_mbus_info - get the isp info of the media bus code
441 *
442 * @mbus_code: the media bus code
443 */
444 const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get(u32 mbus_code);
445
446 /* rkisp1_params_configure - configure the params when stream starts.
447 * This function is called by the isp entity upon stream starts.
448 * The function applies the initial configuration of the parameters.
449 *
450 * @params: pointer to rkisp1_params.
451 * @bayer_pat: the bayer pattern on the isp video sink pad
452 * @quantization: the quantization configured on the isp's src pad
453 */
454 void rkisp1_params_configure(struct rkisp1_params *params,
455 enum rkisp1_fmt_raw_pat_type bayer_pat,
456 enum v4l2_quantization quantization);
457
458 /* rkisp1_params_disable - disable all parameters.
459 * This function is called by the isp entity upon stream start
460 * when capturing bayer format.
461 *
462 * @params: pointer to rkisp1_params.
463 */
464 void rkisp1_params_disable(struct rkisp1_params *params);
465
466 /* irq handlers */
467 void rkisp1_isp_isr(struct rkisp1_device *rkisp1);
468 void rkisp1_mipi_isr(struct rkisp1_device *rkisp1);
469 void rkisp1_capture_isr(struct rkisp1_device *rkisp1);
470 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris);
471 void rkisp1_params_isr(struct rkisp1_device *rkisp1);
472
473 /* register/unregisters functions of the entities */
474 int rkisp1_capture_devs_register(struct rkisp1_device *rkisp1);
475 void rkisp1_capture_devs_unregister(struct rkisp1_device *rkisp1);
476
477 int rkisp1_isp_register(struct rkisp1_device *rkisp1);
478 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);
479
480 int rkisp1_resizer_devs_register(struct rkisp1_device *rkisp1);
481 void rkisp1_resizer_devs_unregister(struct rkisp1_device *rkisp1);
482
483 int rkisp1_stats_register(struct rkisp1_device *rkisp1);
484 void rkisp1_stats_unregister(struct rkisp1_device *rkisp1);
485
486 int rkisp1_params_register(struct rkisp1_device *rkisp1);
487 void rkisp1_params_unregister(struct rkisp1_device *rkisp1);
488
489 #endif /* _RKISP1_COMMON_H */
490