1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Rockchip CIF Driver
4 *
5 * Copyright (C) 2018 Rockchip Electronics Co., Ltd.
6 */
7
8 #ifndef _RKCIF_DEV_H
9 #define _RKCIF_DEV_H
10
11 #include <linux/mutex.h>
12 #include <media/media-device.h>
13 #include <media/media-entity.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-device.h>
16 #include <media/videobuf2-v4l2.h>
17 #include <media/v4l2-mc.h>
18 #include <linux/workqueue.h>
19 #include <linux/rk-camera-module.h>
20 #include <linux/rkcif-config.h>
21 #include <linux/soc/rockchip/rockchip_thunderboot_service.h>
22
23 #include "regs.h"
24 #include "version.h"
25 #include "cif-luma.h"
26 #include "mipi-csi2.h"
27 #include "hw.h"
28 #include "subdev-itf.h"
29
30 #if IS_ENABLED(CONFIG_CPU_RV1106)
31 #include <linux/soc/rockchip/rk_sdmmc.h>
32 #endif
33
34 #define CIF_DRIVER_NAME "rkcif"
35 #define CIF_VIDEODEVICE_NAME "stream_cif"
36
37 #define OF_CIF_MONITOR_PARA "rockchip,cif-monitor"
38 #define OF_CIF_WAIT_LINE "wait-line"
39
40 #define CIF_MONITOR_PARA_NUM (5)
41
42 #define RKCIF_SINGLE_STREAM 1
43 #define RKCIF_STREAM_CIF 0
44 #define CIF_DVP_VDEV_NAME CIF_VIDEODEVICE_NAME "_dvp"
45 #define CIF_MIPI_ID0_VDEV_NAME CIF_VIDEODEVICE_NAME "_mipi_id0"
46 #define CIF_MIPI_ID1_VDEV_NAME CIF_VIDEODEVICE_NAME "_mipi_id1"
47 #define CIF_MIPI_ID2_VDEV_NAME CIF_VIDEODEVICE_NAME "_mipi_id2"
48 #define CIF_MIPI_ID3_VDEV_NAME CIF_VIDEODEVICE_NAME "_mipi_id3"
49
50 #define CIF_DVP_ID0_VDEV_NAME CIF_VIDEODEVICE_NAME "_dvp_id0"
51 #define CIF_DVP_ID1_VDEV_NAME CIF_VIDEODEVICE_NAME "_dvp_id1"
52 #define CIF_DVP_ID2_VDEV_NAME CIF_VIDEODEVICE_NAME "_dvp_id2"
53 #define CIF_DVP_ID3_VDEV_NAME CIF_VIDEODEVICE_NAME "_dvp_id3"
54
55 #define RKCIF_PLANE_Y 0
56 #define RKCIF_PLANE_CBCR 1
57
58 /*
59 * RK1808 support 5 channel inputs simultaneously:
60 * dvp + 4 mipi virtual channels;
61 * RV1126/RK356X support 4 channels of BT.656/BT.1120/MIPI
62 */
63 #define RKCIF_MULTI_STREAMS_NUM 5
64 #define RKCIF_STREAM_MIPI_ID0 0
65 #define RKCIF_STREAM_MIPI_ID1 1
66 #define RKCIF_STREAM_MIPI_ID2 2
67 #define RKCIF_STREAM_MIPI_ID3 3
68 #define RKCIF_MAX_STREAM_MIPI 4
69 #define RKCIF_MAX_STREAM_LVDS 4
70 #define RKCIF_MAX_STREAM_DVP 4
71 #define RKCIF_STREAM_DVP 4
72
73 #define RKCIF_MAX_SENSOR 2
74 #define RKCIF_MAX_CSI_CHANNEL 4
75 #define RKCIF_MAX_PIPELINE 4
76
77 #define RKCIF_DEFAULT_WIDTH 640
78 #define RKCIF_DEFAULT_HEIGHT 480
79 #define RKCIF_FS_DETECTED_NUM 2
80
81 #define RKCIF_MAX_INTERVAL_NS 5000000
82 /*
83 * for HDR mode sync buf
84 */
85 #define RDBK_MAX 3
86 #define RDBK_TOISP_MAX 2
87 #define RDBK_L 0
88 #define RDBK_M 1
89 #define RDBK_S 2
90
91 /*
92 * for distinguishing cropping from senosr or usr
93 */
94 #define CROP_SRC_SENSOR_MASK (0x1 << 0)
95 #define CROP_SRC_USR_MASK (0x1 << 1)
96
97 enum rkcif_workmode {
98 RKCIF_WORKMODE_ONEFRAME = 0x00,
99 RKCIF_WORKMODE_PINGPONG = 0x01,
100 RKCIF_WORKMODE_LINELOOP = 0x02
101 };
102
103 enum rkcif_stream_mode {
104 RKCIF_STREAM_MODE_NONE = 0x0,
105 RKCIF_STREAM_MODE_CAPTURE = 0x01,
106 RKCIF_STREAM_MODE_TOISP = 0x02,
107 RKCIF_STREAM_MODE_TOSCALE = 0x04,
108 RKCIF_STREAM_MODE_TOISP_RDBK = 0x08,
109 RKCIF_STREAM_MODE_ROCKIT = 0x10
110 };
111
112 enum rkcif_yuvaddr_state {
113 RKCIF_YUV_ADDR_STATE_UPDATE = 0x0,
114 RKCIF_YUV_ADDR_STATE_INIT = 0x1
115 };
116
117 enum rkcif_state {
118 RKCIF_STATE_DISABLED,
119 RKCIF_STATE_READY,
120 RKCIF_STATE_STREAMING,
121 RKCIF_STATE_RESET_IN_STREAMING,
122 };
123
124 enum rkcif_lvds_pad {
125 RKCIF_LVDS_PAD_SINK = 0x0,
126 RKCIF_LVDS_PAD_SRC_ID0,
127 RKCIF_LVDS_PAD_SRC_ID1,
128 RKCIF_LVDS_PAD_SRC_ID2,
129 RKCIF_LVDS_PAD_SRC_ID3,
130 RKCIF_LVDS_PAD_SCL_ID0,
131 RKCIF_LVDS_PAD_SCL_ID1,
132 RKCIF_LVDS_PAD_SCL_ID2,
133 RKCIF_LVDS_PAD_SCL_ID3,
134 RKCIF_LVDS_PAD_MAX,
135 };
136
137 enum rkcif_lvds_state {
138 RKCIF_LVDS_STOP = 0,
139 RKCIF_LVDS_START,
140 };
141
142 enum rkcif_inf_id {
143 RKCIF_DVP,
144 RKCIF_MIPI_LVDS,
145 };
146
147 enum rkcif_clk_edge {
148 RKCIF_CLK_RISING = 0x0,
149 RKCIF_CLK_FALLING,
150 };
151
152 /*
153 * for distinguishing cropping from senosr or usr
154 */
155 enum rkcif_crop_src {
156 CROP_SRC_ACT = 0x0,
157 CROP_SRC_SENSOR,
158 CROP_SRC_USR,
159 CROP_SRC_MAX
160 };
161
162 /*
163 * struct rkcif_pipeline - An CIF hardware pipeline
164 *
165 * Capture device call other devices via pipeline
166 *
167 * @num_subdevs: number of linked subdevs
168 * @power_cnt: pipeline power count
169 * @stream_cnt: stream power count
170 */
171 struct rkcif_pipeline {
172 struct media_pipeline pipe;
173 int num_subdevs;
174 atomic_t power_cnt;
175 atomic_t stream_cnt;
176 struct v4l2_subdev *subdevs[RKCIF_MAX_PIPELINE];
177 int (*open)(struct rkcif_pipeline *p,
178 struct media_entity *me, bool prepare);
179 int (*close)(struct rkcif_pipeline *p);
180 int (*set_stream)(struct rkcif_pipeline *p, bool on);
181 };
182
183 struct rkcif_buffer {
184 struct vb2_v4l2_buffer vb;
185 struct list_head queue;
186 union {
187 u32 buff_addr[VIDEO_MAX_PLANES];
188 void *vaddr[VIDEO_MAX_PLANES];
189 };
190 struct dma_buf *dbuf;
191 u64 fe_timestamp;
192 };
193
194 struct rkcif_tools_buffer {
195 struct vb2_v4l2_buffer *vb;
196 struct rkisp_rx_buf *dbufs;
197 struct list_head list;
198 u32 frame_idx;
199 u64 timestamp;
200 int use_cnt;
201 };
202
203 extern int rkcif_debug;
204
205 /*
206 * struct rkcif_sensor_info - Sensor infomations
207 * @sd: v4l2 subdev of sensor
208 * @mbus: media bus configuration
209 * @fi: v4l2 subdev frame interval
210 * @lanes: lane num of sensor
211 * @raw_rect: raw output rectangle of sensor, not crop or selection
212 * @selection: selection info of sensor
213 */
214 struct rkcif_sensor_info {
215 struct v4l2_subdev *sd;
216 struct v4l2_mbus_config mbus;
217 struct v4l2_subdev_frame_interval fi;
218 int lanes;
219 struct v4l2_rect raw_rect;
220 struct v4l2_subdev_selection selection;
221 int dsi_input_en;
222 };
223
224 enum cif_fmt_type {
225 CIF_FMT_TYPE_YUV = 0,
226 CIF_FMT_TYPE_RAW,
227 };
228
229 /*
230 * struct cif_output_fmt - The output format
231 *
232 * @bpp: bits per pixel for each cplanes
233 * @fourcc: pixel format in fourcc
234 * @fmt_val: the fmt val corresponding to CIF_FOR register
235 * @csi_fmt_val: the fmt val corresponding to CIF_CSI_ID_CTRL
236 * @cplanes: number of colour planes
237 * @mplanes: number of planes for format
238 * @raw_bpp: bits per pixel for raw format
239 * @fmt_type: image format, raw or yuv
240 */
241 struct cif_output_fmt {
242 u8 bpp[VIDEO_MAX_PLANES];
243 u32 fourcc;
244 u32 fmt_val;
245 u32 csi_fmt_val;
246 u8 cplanes;
247 u8 mplanes;
248 u8 raw_bpp;
249 enum cif_fmt_type fmt_type;
250 };
251
252 /*
253 * struct cif_input_fmt - The input mbus format from sensor
254 *
255 * @mbus_code: mbus format
256 * @dvp_fmt_val: the fmt val corresponding to CIF_FOR register
257 * @csi_fmt_val: the fmt val corresponding to CIF_CSI_ID_CTRL
258 * @fmt_type: image format, raw or yuv
259 * @field: the field type of the input from sensor
260 */
261 struct cif_input_fmt {
262 u32 mbus_code;
263 u32 dvp_fmt_val;
264 u32 csi_fmt_val;
265 u32 csi_yuv_order;
266 enum cif_fmt_type fmt_type;
267 enum v4l2_field field;
268 };
269
270 struct csi_channel_info {
271 unsigned char id;
272 unsigned char enable; /* capture enable */
273 unsigned char vc;
274 unsigned char data_type;
275 unsigned char data_bit;
276 unsigned char crop_en;
277 unsigned char cmd_mode_en;
278 unsigned char fmt_val;
279 unsigned char csi_fmt_val;
280 unsigned int width;
281 unsigned int height;
282 unsigned int virtual_width;
283 unsigned int left_virtual_width;
284 unsigned int crop_st_x;
285 unsigned int crop_st_y;
286 unsigned int dsi_input;
287 struct rkmodule_lvds_cfg lvds_cfg;
288 struct rkmodule_capture_info capture_info;
289 };
290
291 struct rkcif_vdev_node {
292 struct vb2_queue buf_queue;
293 /* vfd lock */
294 struct mutex vlock;
295 struct video_device vdev;
296 struct media_pad pad;
297 };
298
299 /*
300 * the mark that csi frame0/1 interrupts enabled
301 * in CIF_MIPI_INTEN
302 */
303 enum cif_frame_ready {
304 CIF_CSI_FRAME0_READY = 0x1,
305 CIF_CSI_FRAME1_READY
306 };
307
308 /* struct rkcif_hdr - hdr configured
309 * @op_mode: hdr optional mode
310 */
311 struct rkcif_hdr {
312 u8 mode;
313 };
314
315 /* struct rkcif_fps_stats - take notes on timestamp of buf
316 * @frm0_timestamp: timesstamp of buf in frm0
317 * @frm1_timestamp: timesstamp of buf in frm1
318 */
319 struct rkcif_fps_stats {
320 u64 frm0_timestamp;
321 u64 frm1_timestamp;
322 };
323
324 /* struct rkcif_fps_stats - take notes on timestamp of buf
325 * @fs_timestamp: timesstamp of frame start
326 * @fe_timestamp: timesstamp of frame end
327 * @wk_timestamp: timesstamp of buf send to user in wake up mode
328 * @readout_time: one frame of readout time
329 * @early_time: early time of buf send to user
330 * @total_time: totaltime of readout time in hdr
331 */
332 struct rkcif_readout_stats {
333 u64 fs_timestamp;
334 u64 fe_timestamp;
335 u64 wk_timestamp;
336 u64 readout_time;
337 u64 early_time;
338 u64 total_time;
339 };
340
341 /* struct rkcif_irq_stats - take notes on irq number
342 * @csi_overflow_cnt: count of csi overflow irq
343 * @csi_bwidth_lack_cnt: count of csi bandwidth lack irq
344 * @dvp_bus_err_cnt: count of dvp bus err irq
345 * @dvp_overflow_cnt: count dvp overflow irq
346 * @dvp_line_err_cnt: count dvp line err irq
347 * @dvp_pix_err_cnt: count dvp pix err irq
348 * @all_frm_end_cnt: raw frame end count
349 * @all_err_cnt: all err count
350 * @
351 */
352 struct rkcif_irq_stats {
353 u64 csi_overflow_cnt;
354 u64 csi_bwidth_lack_cnt;
355 u64 csi_size_err_cnt;
356 u64 dvp_bus_err_cnt;
357 u64 dvp_overflow_cnt;
358 u64 dvp_line_err_cnt;
359 u64 dvp_pix_err_cnt;
360 u64 dvp_size_err_cnt;
361 u64 dvp_bwidth_lack_cnt;
362 u64 frm_end_cnt[RKCIF_MAX_STREAM_MIPI];
363 u64 not_active_buf_cnt[RKCIF_MAX_STREAM_MIPI];
364 u64 trig_simult_cnt[RKCIF_MAX_STREAM_MIPI];
365 u64 all_err_cnt;
366 };
367
368 /*
369 * the detecting mode of cif reset timer
370 * related with dts property:rockchip,cif-monitor
371 */
372 enum rkcif_monitor_mode {
373 RKCIF_MONITOR_MODE_IDLE = 0x0,
374 RKCIF_MONITOR_MODE_CONTINUE,
375 RKCIF_MONITOR_MODE_TRIGGER,
376 RKCIF_MONITOR_MODE_HOTPLUG,
377 };
378
379 /*
380 * the parameters to resume when reset cif in running
381 */
382 struct rkcif_resume_info {
383 u32 frm_sync_seq;
384 };
385
386 struct rkcif_work_struct {
387 struct work_struct work;
388 enum rkmodule_reset_src reset_src;
389 struct rkcif_resume_info resume_info;
390 };
391
392 struct rkcif_timer {
393 struct timer_list timer;
394 spinlock_t timer_lock;
395 spinlock_t csi2_err_lock;
396 unsigned long cycle;
397 /* unit: us */
398 unsigned long line_end_cycle;
399 unsigned int run_cnt;
400 unsigned int max_run_cnt;
401 unsigned int stop_index_of_run_cnt;
402 unsigned int last_buf_wakeup_cnt[RKCIF_MAX_STREAM_MIPI];
403 unsigned long csi2_err_cnt_even;
404 unsigned long csi2_err_cnt_odd;
405 unsigned int csi2_err_ref_cnt;
406 unsigned int csi2_err_fs_fe_cnt;
407 unsigned int csi2_err_fs_fe_detect_cnt;
408 unsigned int frm_num_of_monitor_cycle;
409 unsigned int triggered_frame_num;
410 unsigned int vts;
411 unsigned int raw_height;
412 /* unit: ms */
413 unsigned int err_time_interval;
414 unsigned int csi2_err_triggered_cnt;
415 unsigned int notifer_called_cnt;
416 unsigned long frame_end_cycle_us;
417 u64 csi2_first_err_timestamp;
418 bool is_triggered;
419 bool is_buf_stop_update;
420 bool is_running;
421 bool is_csi2_err_occurred;
422 bool has_been_init;
423 bool is_ctrl_by_user;
424 enum rkcif_monitor_mode monitor_mode;
425 enum rkmodule_reset_src reset_src;
426 };
427
428 struct rkcif_extend_info {
429 struct v4l2_pix_format_mplane pixm;
430 bool is_extended;
431 };
432
433 enum rkcif_capture_mode {
434 RKCIF_TO_DDR = 0,
435 RKCIF_TO_ISP_DDR,
436 RKCIF_TO_ISP_DMA,
437 };
438
439 /*
440 * list: used for buf rotation
441 * list_free: only used to release buf asynchronously
442 */
443 struct rkcif_rx_buffer {
444 int buf_idx;
445 struct list_head list;
446 struct list_head list_free;
447 struct rkisp_rx_buf dbufs;
448 struct rkcif_dummy_buffer dummy;
449 struct rkisp_thunderboot_shmem shmem;
450 u64 fe_timestamp;
451 };
452
453 enum rkcif_dma_en_mode {
454 RKCIF_DMAEN_BY_VICAP = 0x1,
455 RKCIF_DMAEN_BY_ISP = 0x2,
456 RKCIF_DMAEN_BY_VICAP_TO_ISP = 0x4,
457 RKCIF_DMAEN_BY_ISP_TO_VICAP = 0x8,
458 RKCIF_DMAEN_BY_ROCKIT = 0x10,
459 };
460
461 struct rkcif_skip_info {
462 u8 cap_m;
463 u8 skip_n;
464 bool skip_en;
465 bool skip_to_en;
466 bool skip_to_dis;
467 };
468
469 struct rkcif_sync_cfg {
470 u32 type;
471 u32 group;
472 };
473
474 /*
475 * struct rkcif_stream - Stream states TODO
476 *
477 * @vbq_lock: lock to protect buf_queue
478 * @buf_queue: queued buffer list
479 * @dummy_buf: dummy space to store dropped data
480 * @crop_enable: crop status when stream off
481 * @crop_dyn_en: crop status when streaming
482 * rkcif use shadowsock registers, so it need two buffer at a time
483 * @curr_buf: the buffer used for current frame
484 * @next_buf: the buffer used for next frame
485 * @fps_lock: to protect parameters about calculating fps
486 */
487 struct rkcif_stream {
488 unsigned id:3;
489 struct rkcif_device *cifdev;
490 struct rkcif_vdev_node vnode;
491 enum rkcif_state state;
492 wait_queue_head_t wq_stopped;
493 unsigned int frame_idx;
494 int frame_phase;
495 int frame_phase_cache;
496 unsigned int crop_mask;
497 /* lock between irq and buf_queue */
498 struct list_head buf_head;
499 struct rkcif_buffer *curr_buf;
500 struct rkcif_buffer *next_buf;
501 struct rkcif_rx_buffer *curr_buf_toisp;
502 struct rkcif_rx_buffer *next_buf_toisp;
503 struct list_head rockit_buf_head;
504 struct rkcif_buffer *curr_buf_rockit;
505 struct rkcif_buffer *next_buf_rockit;
506
507 spinlock_t vbq_lock; /* vfd lock */
508 spinlock_t fps_lock;
509 /* TODO: pad for dvp and mipi separately? */
510 struct media_pad pad;
511
512 const struct cif_output_fmt *cif_fmt_out;
513 const struct cif_input_fmt *cif_fmt_in;
514 struct v4l2_pix_format_mplane pixm;
515 struct v4l2_rect crop[CROP_SRC_MAX];
516 struct rkcif_fps_stats fps_stats;
517 struct rkcif_extend_info extend_line;
518 struct rkcif_readout_stats readout;
519 unsigned int fs_cnt_in_single_frame;
520 unsigned int capture_mode;
521 struct rkcif_scale_vdev *scale_vdev;
522 struct rkcif_tools_vdev *tools_vdev;
523 int dma_en;
524 int to_en_dma;
525 int to_stop_dma;
526 int buf_owner;
527 int buf_replace_cnt;
528 struct list_head rx_buf_head_vicap;
529 unsigned int cur_stream_mode;
530 struct rkcif_rx_buffer rx_buf[RKISP_VICAP_BUF_CNT_MAX];
531 struct list_head rx_buf_head;
532 int total_buf_num;
533 u64 line_int_cnt;
534 int lack_buf_cnt;
535 unsigned int buf_wake_up_cnt;
536 struct rkcif_skip_info skip_info;
537 struct tasklet_struct vb_done_tasklet;
538 struct list_head vb_done_list;
539 int last_rx_buf_idx;
540 int last_frame_idx;
541 int new_fource_idx;
542 atomic_t buf_cnt;
543 bool stopping;
544 bool crop_enable;
545 bool crop_dyn_en;
546 bool is_compact;
547 bool is_dvp_yuv_addr_init;
548 bool is_fs_fe_not_paired;
549 bool is_line_wake_up;
550 bool is_line_inten;
551 bool is_can_stop;
552 bool is_buf_active;
553 bool is_high_align;
554 bool to_en_scale;
555 bool is_finish_stop_dma;
556 bool is_in_vblank;
557 bool is_change_toisp;
558 bool is_stop_capture;
559 };
560
561 struct rkcif_lvds_subdev {
562 struct rkcif_device *cifdev;
563 struct v4l2_subdev sd;
564 struct v4l2_subdev *remote_sd;
565 struct media_pad pads[RKCIF_LVDS_PAD_MAX];
566 struct v4l2_mbus_framefmt in_fmt;
567 struct v4l2_rect crop;
568 const struct cif_output_fmt *cif_fmt_out;
569 const struct cif_input_fmt *cif_fmt_in;
570 enum rkcif_lvds_state state;
571 struct rkcif_sensor_info sensor_self;
572 atomic_t frm_sync_seq;
573 };
574
575 struct rkcif_dvp_sof_subdev {
576 struct rkcif_device *cifdev;
577 struct v4l2_subdev sd;
578 atomic_t frm_sync_seq;
579 };
580
to_rkcif_buffer(struct vb2_v4l2_buffer * vb)581 static inline struct rkcif_buffer *to_rkcif_buffer(struct vb2_v4l2_buffer *vb)
582 {
583 return container_of(vb, struct rkcif_buffer, vb);
584 }
585
586 static inline
vdev_to_node(struct video_device * vdev)587 struct rkcif_vdev_node *vdev_to_node(struct video_device *vdev)
588 {
589 return container_of(vdev, struct rkcif_vdev_node, vdev);
590 }
591
592 static inline
to_rkcif_stream(struct rkcif_vdev_node * vnode)593 struct rkcif_stream *to_rkcif_stream(struct rkcif_vdev_node *vnode)
594 {
595 return container_of(vnode, struct rkcif_stream, vnode);
596 }
597
queue_to_node(struct vb2_queue * q)598 static inline struct rkcif_vdev_node *queue_to_node(struct vb2_queue *q)
599 {
600 return container_of(q, struct rkcif_vdev_node, buf_queue);
601 }
602
to_vb2_queue(struct file * file)603 static inline struct vb2_queue *to_vb2_queue(struct file *file)
604 {
605 struct rkcif_vdev_node *vnode = video_drvdata(file);
606
607 return &vnode->buf_queue;
608 }
609
610 #define SCALE_DRIVER_NAME "rkcif_scale"
611
612 #define RKCIF_SCALE_CH0 0
613 #define RKCIF_SCALE_CH1 1
614 #define RKCIF_SCALE_CH2 2
615 #define RKCIF_SCALE_CH3 3
616 #define RKCIF_MAX_SCALE_CH 4
617
618 #define CIF_SCALE_CH0_VDEV_NAME CIF_DRIVER_NAME "_scale_ch0"
619 #define CIF_SCALE_CH1_VDEV_NAME CIF_DRIVER_NAME "_scale_ch1"
620 #define CIF_SCALE_CH2_VDEV_NAME CIF_DRIVER_NAME "_scale_ch2"
621 #define CIF_SCALE_CH3_VDEV_NAME CIF_DRIVER_NAME "_scale_ch3"
622
623 #define RKCIF_SCALE_ENUM_SIZE_MAX 3
624 #define RKCIF_MAX_SDITF 4
625
626 enum scale_ch_sw {
627 SCALE_MIPI0_ID0,
628 SCALE_MIPI0_ID1,
629 SCALE_MIPI0_ID2,
630 SCALE_MIPI0_ID3,
631 SCALE_MIPI1_ID0,
632 SCALE_MIPI1_ID1,
633 SCALE_MIPI1_ID2,
634 SCALE_MIPI1_ID3,
635 SCALE_MIPI2_ID0,
636 SCALE_MIPI2_ID1,
637 SCALE_MIPI2_ID2,
638 SCALE_MIPI2_ID3,
639 SCALE_MIPI3_ID0,
640 SCALE_MIPI3_ID1,
641 SCALE_MIPI3_ID2,
642 SCALE_MIPI3_ID3,
643 SCALE_MIPI4_ID0,
644 SCALE_MIPI4_ID1,
645 SCALE_MIPI4_ID2,
646 SCALE_MIPI4_ID3,
647 SCALE_MIPI5_ID0,
648 SCALE_MIPI5_ID1,
649 SCALE_MIPI5_ID2,
650 SCALE_MIPI5_ID3,
651 SCALE_DVP,
652 SCALE_CH_MAX,
653 };
654
655 enum scale_mode {
656 SCALE_8TIMES,
657 SCALE_16TIMES,
658 SCALE_32TIMES,
659 };
660
661 struct rkcif_scale_ch_info {
662 u32 width;
663 u32 height;
664 u32 vir_width;
665 };
666
667 struct rkcif_scale_src_res {
668 u32 width;
669 u32 height;
670 };
671
672 /*
673 * struct rkcif_scale_vdev - CIF Capture device
674 *
675 * @irq_lock: buffer queue lock
676 * @stat: stats buffer list
677 * @readout_wq: workqueue for statistics information read
678 */
679 struct rkcif_scale_vdev {
680 unsigned int ch:3;
681 struct rkcif_device *cifdev;
682 struct rkcif_vdev_node vnode;
683 struct rkcif_stream *stream;
684 struct list_head buf_head;
685 spinlock_t vbq_lock; /* vfd lock */
686 wait_queue_head_t wq_stopped;
687 struct v4l2_pix_format_mplane pixm;
688 const struct cif_output_fmt *scale_out_fmt;
689 struct rkcif_scale_ch_info ch_info;
690 struct rkcif_scale_src_res src_res;
691 struct rkcif_buffer *curr_buf;
692 struct rkcif_buffer *next_buf;
693 struct bayer_blc blc;
694 enum rkcif_state state;
695 unsigned int ch_src;
696 unsigned int scale_mode;
697 int frame_phase;
698 unsigned int frame_idx;
699 bool stopping;
700 };
701
702 static inline
to_rkcif_scale_vdev(struct rkcif_vdev_node * vnode)703 struct rkcif_scale_vdev *to_rkcif_scale_vdev(struct rkcif_vdev_node *vnode)
704 {
705 return container_of(vnode, struct rkcif_scale_vdev, vnode);
706 }
707
708 void rkcif_init_scale_vdev(struct rkcif_device *cif_dev, u32 ch);
709 int rkcif_register_scale_vdevs(struct rkcif_device *cif_dev,
710 int stream_num,
711 bool is_multi_input);
712 void rkcif_unregister_scale_vdevs(struct rkcif_device *cif_dev,
713 int stream_num);
714
715 #define TOOLS_DRIVER_NAME "rkcif_tools"
716
717 #define RKCIF_TOOLS_CH0 0
718 #define RKCIF_TOOLS_CH1 1
719 #define RKCIF_TOOLS_CH2 2
720 #define RKCIF_MAX_TOOLS_CH 3
721
722 #define CIF_TOOLS_CH0_VDEV_NAME CIF_DRIVER_NAME "_tools_id0"
723 #define CIF_TOOLS_CH1_VDEV_NAME CIF_DRIVER_NAME "_tools_id1"
724 #define CIF_TOOLS_CH2_VDEV_NAME CIF_DRIVER_NAME "_tools_id2"
725
726 /*
727 * struct rkcif_tools_vdev - CIF Capture device
728 *
729 * @irq_lock: buffer queue lock
730 * @stat: stats buffer list
731 * @readout_wq: workqueue for statistics information read
732 */
733 struct rkcif_tools_vdev {
734 unsigned int ch:3;
735 struct rkcif_device *cifdev;
736 struct rkcif_vdev_node vnode;
737 struct rkcif_stream *stream;
738 struct list_head buf_head;
739 struct list_head buf_done_head;
740 struct list_head src_buf_head;
741 spinlock_t vbq_lock; /* vfd lock */
742 wait_queue_head_t wq_stopped;
743 struct v4l2_pix_format_mplane pixm;
744 const struct cif_output_fmt *tools_out_fmt;
745 struct rkcif_buffer *curr_buf;
746 struct work_struct work;
747 enum rkcif_state state;
748 int frame_phase;
749 unsigned int frame_idx;
750 bool stopping;
751 };
752
753 static inline
to_rkcif_tools_vdev(struct rkcif_vdev_node * vnode)754 struct rkcif_tools_vdev *to_rkcif_tools_vdev(struct rkcif_vdev_node *vnode)
755 {
756 return container_of(vnode, struct rkcif_tools_vdev, vnode);
757 }
758
759 void rkcif_init_tools_vdev(struct rkcif_device *cif_dev, u32 ch);
760 int rkcif_register_tools_vdevs(struct rkcif_device *cif_dev,
761 int stream_num,
762 bool is_multi_input);
763 void rkcif_unregister_tools_vdevs(struct rkcif_device *cif_dev,
764 int stream_num);
765
766 enum rkcif_err_state {
767 RKCIF_ERR_ID0_NOT_BUF = 0x1,
768 RKCIF_ERR_ID1_NOT_BUF = 0x2,
769 RKCIF_ERR_ID2_NOT_BUF = 0x4,
770 RKCIF_ERR_ID3_NOT_BUF = 0x8,
771 RKCIF_ERR_ID0_TRIG_SIMULT = 0x10,
772 RKCIF_ERR_ID1_TRIG_SIMULT = 0x20,
773 RKCIF_ERR_ID2_TRIG_SIMULT = 0x40,
774 RKCIF_ERR_ID3_TRIG_SIMULT = 0x80,
775 RKCIF_ERR_SIZE = 0x100,
776 RKCIF_ERR_OVERFLOW = 0x200,
777 RKCIF_ERR_BANDWIDTH_LACK = 0x400,
778 RKCIF_ERR_BUS = 0X800,
779 RKCIF_ERR_ID0_MULTI_FS = 0x1000,
780 RKCIF_ERR_ID1_MULTI_FS = 0x2000,
781 RKCIF_ERR_ID2_MULTI_FS = 0x4000,
782 RKCIF_ERR_ID3_MULTI_FS = 0x8000,
783 RKCIF_ERR_PIXEL = 0x10000,
784 RKCIF_ERR_LINE = 0x20000,
785 };
786
787 struct rkcif_err_state_work {
788 struct work_struct work;
789 u64 last_timestamp;
790 u32 err_state;
791 u32 intstat;
792 u32 lastline;
793 u32 lastpixel;
794 };
795
796 /*
797 * struct rkcif_device - ISP platform device
798 * @base_addr: base register address
799 * @active_sensor: sensor in-use, set when streaming on
800 * @stream: capture video device
801 */
802 struct rkcif_device {
803 struct list_head list;
804 struct device *dev;
805 struct v4l2_device v4l2_dev;
806 struct media_device media_dev;
807 struct v4l2_async_notifier notifier;
808
809 struct rkcif_sensor_info sensors[RKCIF_MAX_SENSOR];
810 u32 num_sensors;
811 struct rkcif_sensor_info *active_sensor;
812 struct rkcif_sensor_info terminal_sensor;
813
814 struct rkcif_stream stream[RKCIF_MULTI_STREAMS_NUM];
815 struct rkcif_scale_vdev scale_vdev[RKCIF_MULTI_STREAMS_NUM];
816 struct rkcif_tools_vdev tools_vdev[RKCIF_MAX_TOOLS_CH];
817 struct rkcif_pipeline pipe;
818
819 struct csi_channel_info channels[RKCIF_MAX_CSI_CHANNEL];
820 int num_channels;
821 int chip_id;
822 atomic_t stream_cnt;
823 atomic_t power_cnt;
824 struct mutex stream_lock; /* lock between streams */
825 struct mutex scale_lock; /* lock between scale dev */
826 struct mutex tools_lock; /* lock between tools dev */
827 enum rkcif_workmode workmode;
828 bool can_be_reset;
829 struct rkmodule_hdr_cfg hdr;
830 struct rkcif_buffer *rdbk_buf[RDBK_MAX];
831 struct rkcif_rx_buffer *rdbk_rx_buf[RDBK_MAX];
832 struct rkcif_luma_vdev luma_vdev;
833 struct rkcif_lvds_subdev lvds_subdev;
834 struct rkcif_dvp_sof_subdev dvp_sof_subdev;
835 struct rkcif_hw *hw_dev;
836 irqreturn_t (*isr_hdl)(int irq, struct rkcif_device *cif_dev);
837 int inf_id;
838
839 struct sditf_priv *sditf[RKCIF_MAX_SDITF];
840 struct proc_dir_entry *proc_dir;
841 struct rkcif_irq_stats irq_stats;
842 spinlock_t hdr_lock; /* lock for hdr buf sync */
843 spinlock_t buffree_lock;
844 struct rkcif_timer reset_watchdog_timer;
845 struct rkcif_work_struct reset_work;
846 int id_use_cnt;
847 unsigned int csi_host_idx;
848 unsigned int csi_host_idx_def;
849 unsigned int dvp_sof_in_oneframe;
850 unsigned int wait_line;
851 unsigned int wait_line_bak;
852 unsigned int wait_line_cache;
853 struct completion cmpl_ntf;
854 struct csi2_dphy_hw *dphy_hw;
855 phys_addr_t resmem_pa;
856 size_t resmem_size;
857 struct rk_tb_client tb_client;
858 bool is_start_hdr;
859 bool reset_work_cancel;
860 bool iommu_en;
861 bool is_use_dummybuf;
862 bool is_notifier_isp;
863 bool is_thunderboot;
864 bool is_rdbk_to_online;
865 bool is_support_tools;
866 int rdbk_debug;
867 struct rkcif_sync_cfg sync_cfg;
868 int sditf_cnt;
869 u32 early_line;
870 int isp_runtime_max;
871 int sensor_linetime;
872 u32 err_state;
873 struct rkcif_err_state_work err_state_work;
874 };
875
876 extern struct platform_driver rkcif_plat_drv;
877 void rkcif_set_fps(struct rkcif_stream *stream, struct rkcif_fps *fps);
878 int rkcif_do_start_stream(struct rkcif_stream *stream,
879 enum rkcif_stream_mode mode);
880 void rkcif_do_stop_stream(struct rkcif_stream *stream,
881 enum rkcif_stream_mode mode);
882 void rkcif_irq_handle_scale(struct rkcif_device *cif_dev,
883 unsigned int intstat_glb);
884 void rkcif_buf_queue(struct vb2_buffer *vb);
885
886 void rkcif_vb_done_tasklet(struct rkcif_stream *stream, struct rkcif_buffer *buf);
887
888 int rkcif_scale_start(struct rkcif_scale_vdev *scale_vdev);
889
890 const struct
891 cif_input_fmt *rkcif_get_input_fmt(struct rkcif_device *dev,
892 struct v4l2_rect *rect,
893 u32 pad_id, struct csi_channel_info *csi_info);
894
895 void rkcif_write_register(struct rkcif_device *dev,
896 enum cif_reg_index index, u32 val);
897 void rkcif_write_register_or(struct rkcif_device *dev,
898 enum cif_reg_index index, u32 val);
899 void rkcif_write_register_and(struct rkcif_device *dev,
900 enum cif_reg_index index, u32 val);
901 unsigned int rkcif_read_register(struct rkcif_device *dev,
902 enum cif_reg_index index);
903 void rkcif_write_grf_reg(struct rkcif_device *dev,
904 enum cif_reg_index index, u32 val);
905 u32 rkcif_read_grf_reg(struct rkcif_device *dev,
906 enum cif_reg_index index);
907 void rkcif_unregister_stream_vdevs(struct rkcif_device *dev,
908 int stream_num);
909 int rkcif_register_stream_vdevs(struct rkcif_device *dev,
910 int stream_num,
911 bool is_multi_input);
912 void rkcif_stream_init(struct rkcif_device *dev, u32 id);
913 void rkcif_set_default_fmt(struct rkcif_device *cif_dev);
914 void rkcif_irq_oneframe(struct rkcif_device *cif_dev);
915 void rkcif_irq_pingpong(struct rkcif_device *cif_dev);
916 void rkcif_irq_pingpong_v1(struct rkcif_device *cif_dev);
917 unsigned int rkcif_irq_global(struct rkcif_device *cif_dev);
918 void rkcif_irq_handle_toisp(struct rkcif_device *cif_dev, unsigned int intstat_glb);
919 int rkcif_register_lvds_subdev(struct rkcif_device *dev);
920 void rkcif_unregister_lvds_subdev(struct rkcif_device *dev);
921 int rkcif_register_dvp_sof_subdev(struct rkcif_device *dev);
922 void rkcif_unregister_dvp_sof_subdev(struct rkcif_device *dev);
923 void rkcif_irq_lite_lvds(struct rkcif_device *cif_dev);
924 int rkcif_plat_init(struct rkcif_device *cif_dev, struct device_node *node, int inf_id);
925 int rkcif_plat_uninit(struct rkcif_device *cif_dev);
926 int rkcif_attach_hw(struct rkcif_device *cif_dev);
927 int rkcif_update_sensor_info(struct rkcif_stream *stream);
928 int rkcif_reset_notifier(struct notifier_block *nb, unsigned long action, void *data);
929 void rkcif_reset_watchdog_timer_handler(struct timer_list *t);
930 void rkcif_config_dvp_clk_sampling_edge(struct rkcif_device *dev,
931 enum rkcif_clk_edge edge);
932 void rkcif_enable_dvp_clk_dual_edge(struct rkcif_device *dev, bool on);
933 void rkcif_reset_work(struct work_struct *work);
934
935 void rkcif_vb_done_oneframe(struct rkcif_stream *stream,
936 struct vb2_v4l2_buffer *vb_done);
937
938 int rkcif_init_rx_buf(struct rkcif_stream *stream, int buf_num);
939 void rkcif_free_rx_buf(struct rkcif_stream *stream, int buf_num);
940
941 int rkcif_set_fmt(struct rkcif_stream *stream,
942 struct v4l2_pix_format_mplane *pixm,
943 bool try);
944 void rkcif_enable_dma_capture(struct rkcif_stream *stream, bool is_only_enable);
945
946 void rkcif_do_soft_reset(struct rkcif_device *dev);
947
948 u32 rkcif_mbus_pixelcode_to_v4l2(u32 pixelcode);
949
950 void rkcif_config_dvp_pin(struct rkcif_device *dev, bool on);
951
952 s32 rkcif_get_sensor_vblank_def(struct rkcif_device *dev);
953 s32 rkcif_get_sensor_vblank(struct rkcif_device *dev);
954 int rkcif_get_linetime(struct rkcif_stream *stream);
955
956 void rkcif_assign_check_buffer_update_toisp(struct rkcif_stream *stream);
957
958 struct rkcif_rx_buffer *to_cif_rx_buf(struct rkisp_rx_buf *dbufs);
959
960 int rkcif_clr_unready_dev(void);
961
962 const struct
963 cif_output_fmt *rkcif_find_output_fmt(struct rkcif_stream *stream, u32 pixelfmt);
964 /* Rockit */
965 int rkcif_rockit_buf_done(struct rkcif_stream *stream, struct rkcif_buffer *buf);
966 void rkcif_rockit_dev_init(struct rkcif_device *dev);
967 void rkcif_rockit_dev_deinit(void);
968
969 void rkcif_err_print_work(struct work_struct *work);
970
971 #endif
972