1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd. */
3
4 #ifndef _RKISPP_COMMON_H
5 #define _RKISPP_COMMON_H
6
7 #include <linux/clk.h>
8 #include <linux/mutex.h>
9 #include <media/media-device.h>
10 #include <media/media-entity.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-device.h>
13 #include <media/videobuf2-v4l2.h>
14
15 #include "../isp/isp_ispp.h"
16
17 #define RKISPP_PLANE_Y 0
18 #define RKISPP_PLANE_UV 1
19
20 #define RKISPP_MAX_WIDTH_V10 4416
21 #define RKISPP_MAX_HEIGHT_V10 3312
22 #define RKISPP_MIN_WIDTH_V10 66
23 #define RKISPP_MIN_HEIGHT_V10 258
24
25 #define RKISPP_MAX_WIDTH_V20 8188
26 #define RKISPP_MAX_HEIGHT_V20 8188
27 #define RKISPP_MIN_WIDTH_V20 128
28 #define RKISPP_MIN_HEIGHT_V20 128
29
30 #define RKISPP_VIDEO_NAME_LEN 16
31
32 #define RKISPP_BUF_POOL_MAX RKISP_ISPP_BUF_MAX
33
34 struct rkispp_device;
35
36 enum rkispp_ver {
37 ISPP_V10 = 0x00,
38 ISPP_V20 = 0x01,
39 };
40
41 enum rkispp_event_cmd {
42 CMD_STREAM,
43 CMD_INIT_POOL,
44 CMD_FREE_POOL,
45 CMD_QUEUE_DMABUF,
46 };
47
48 struct rkispp_isp_buf_pool {
49 struct rkisp_ispp_buf *dbufs;
50 void *mem_priv[GROUP_BUF_MAX];
51 dma_addr_t dma[GROUP_BUF_MAX];
52 void *vaddr[GROUP_BUF_MAX];
53 u8 group_buf_max;
54 };
55
56 /* One structure per video node */
57 struct rkispp_vdev_node {
58 struct vb2_queue buf_queue;
59 struct video_device vdev;
60 struct media_pad pad;
61 };
62
63 struct rkispp_buffer {
64 struct vb2_v4l2_buffer vb;
65 struct list_head queue;
66 union {
67 u32 buff_addr[VIDEO_MAX_PLANES];
68 void *vaddr[VIDEO_MAX_PLANES];
69 };
70 };
71
72 struct rkispp_dummy_buffer {
73 struct list_head list;
74 struct dma_buf *dbuf;
75 dma_addr_t dma_addr;
76 void *mem_priv;
77 void *vaddr;
78 struct page **pages;
79 /* timestamp in ns */
80 u64 timestamp;
81 u32 size;
82 u32 id;
83 u32 index;
84 int dma_fd;
85 bool is_need_vaddr;
86 bool is_need_dbuf;
87 bool is_need_dmafd;
88 };
89
vdev_to_node(struct video_device * vdev)90 static inline struct rkispp_vdev_node *vdev_to_node(struct video_device *vdev)
91 {
92 return container_of(vdev, struct rkispp_vdev_node, vdev);
93 }
94
queue_to_node(struct vb2_queue * q)95 static inline struct rkispp_vdev_node *queue_to_node(struct vb2_queue *q)
96 {
97 return container_of(q, struct rkispp_vdev_node, buf_queue);
98 }
99
to_rkispp_buffer(struct vb2_v4l2_buffer * vb)100 static inline struct rkispp_buffer *to_rkispp_buffer(struct vb2_v4l2_buffer *vb)
101 {
102 return container_of(vb, struct rkispp_buffer, vb);
103 }
104
to_vb2_queue(struct file * file)105 static inline struct vb2_queue *to_vb2_queue(struct file *file)
106 {
107 struct rkispp_vdev_node *vnode = video_drvdata(file);
108
109 return &vnode->buf_queue;
110 }
111
112 extern int rkispp_debug;
113 extern bool rkispp_monitor;
114 extern bool rkispp_reg_withstream;
115 extern char rkispp_reg_withstream_video_name[RKISPP_VIDEO_NAME_LEN];
116 extern unsigned int rkispp_debug_reg;
117 extern struct platform_driver rkispp_plat_drv;
118 extern char rkispp_dump_path[128];
119
120 void rkispp_write(struct rkispp_device *dev, u32 reg, u32 val);
121 void rkispp_set_bits(struct rkispp_device *dev, u32 reg, u32 mask, u32 val);
122 u32 rkispp_read(struct rkispp_device *dev, u32 reg);
123 void rkispp_clear_bits(struct rkispp_device *dev, u32 reg, u32 mask);
124 void rkispp_update_regs(struct rkispp_device *dev, u32 start, u32 end);
125 int rkispp_allow_buffer(struct rkispp_device *dev,
126 struct rkispp_dummy_buffer *buf);
127 void rkispp_free_buffer(struct rkispp_device *dev,
128 struct rkispp_dummy_buffer *buf);
129 void rkispp_prepare_buffer(struct rkispp_device *dev,
130 struct rkispp_dummy_buffer *buf);
131 void rkispp_finish_buffer(struct rkispp_device *dev,
132 struct rkispp_dummy_buffer *buf);
133
134 int rkispp_attach_hw(struct rkispp_device *ispp);
135 int rkispp_event_handle(struct rkispp_device *ispp, u32 cmd, void *arg);
136 int rkispp_alloc_common_dummy_buf(struct rkispp_device *dev);
137 void rkispp_free_common_dummy_buf(struct rkispp_device *dev);
138 int rkispp_find_regbuf_by_id(struct rkispp_device *ispp, struct rkisp_ispp_reg **free_buf,
139 u32 dev_id, u32 frame_id);
140 void rkispp_release_regbuf(struct rkispp_device *ispp, struct rkisp_ispp_reg *freebuf);
141 void rkispp_request_regbuf(struct rkispp_device *dev, struct rkisp_ispp_reg **free_buf);
142 bool rkispp_is_reg_withstream_global(void);
143 bool rkispp_is_reg_withstream_local(struct device *dev);
144 void rkispp_set_clk_rate(struct clk *clk, unsigned long rate);
145 #endif
146