1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd. */
3
4 #include <media/v4l2-common.h>
5 #include <media/v4l2-ioctl.h>
6 #include <media/videobuf2-core.h>
7 #include <media/videobuf2-vmalloc.h>
8 #include <media/v4l2-event.h>
9 #include <media/v4l2-mc.h>
10 #include <uapi/linux/rk-video-format.h>
11 #include "dev.h"
12 #include "regs.h"
13
fec_enable(struct rkispp_params_vdev * params_vdev,bool en)14 static void fec_enable(struct rkispp_params_vdev *params_vdev, bool en)
15 {
16 struct rkispp_device *dev = params_vdev->dev;
17 u32 buf_idx;
18
19 if (en) {
20 buf_idx = params_vdev->buf_fec_idx;
21 if (!params_vdev->buf_fec[buf_idx].vaddr) {
22 dev_err(dev->dev, "no fec buffer allocated\n");
23 return;
24 }
25 }
26 rkispp_set_bits(params_vdev->dev, RKISPP_FEC_CORE_CTRL, SW_FEC_EN, en);
27 }
28
fec_config(struct rkispp_params_vdev * params_vdev,struct fec_config * arg)29 static void fec_config(struct rkispp_params_vdev *params_vdev,
30 struct fec_config *arg)
31 {
32 struct rkispp_device *dev = params_vdev->dev;
33 struct rkispp_fec_head *fec_data;
34 u32 width, height, mesh_size;
35 dma_addr_t dma_addr;
36 u32 val, i, buf_idx;
37
38 width = dev->ispp_sdev.out_fmt.width;
39 height = dev->ispp_sdev.out_fmt.height;
40 mesh_size = cal_fec_mesh(width, height, 1);
41 if (arg->mesh_size > mesh_size) {
42 v4l2_err(&dev->v4l2_dev,
43 "Input mesh size too large. mesh size 0x%x, 0x%x\n",
44 arg->mesh_size, mesh_size);
45 return;
46 }
47
48 for (i = 0; i < FEC_MESH_BUF_NUM; i++) {
49 if (arg->buf_fd == params_vdev->buf_fec[i].dma_fd)
50 break;
51 }
52 if (i == FEC_MESH_BUF_NUM) {
53 dev_err(dev->dev, "cannot find fec buf fd(%d)\n", arg->buf_fd);
54 return;
55 }
56
57 if (!params_vdev->buf_fec[i].vaddr) {
58 dev_err(dev->dev, "no fec buffer allocated\n");
59 return;
60 }
61
62 buf_idx = params_vdev->buf_fec_idx;
63 fec_data = (struct rkispp_fec_head *)params_vdev->buf_fec[buf_idx].vaddr;
64 fec_data->stat = FEC_BUF_INIT;
65
66 buf_idx = i;
67 fec_data = (struct rkispp_fec_head *)params_vdev->buf_fec[buf_idx].vaddr;
68 fec_data->stat = FEC_BUF_CHIPINUSE;
69 params_vdev->buf_fec_idx = buf_idx;
70
71 rkispp_prepare_buffer(dev, ¶ms_vdev->buf_fec[buf_idx]);
72
73 dma_addr = params_vdev->buf_fec[buf_idx].dma_addr;
74 val = dma_addr + fec_data->meshxf_oft;
75 rkispp_write(params_vdev->dev, RKISPP_FEC_MESH_XFRA_BASE, val);
76 val = dma_addr + fec_data->meshyf_oft;
77 rkispp_write(params_vdev->dev, RKISPP_FEC_MESH_YFRA_BASE, val);
78 val = dma_addr + fec_data->meshxi_oft;
79 rkispp_write(params_vdev->dev, RKISPP_FEC_MESH_XINT_BASE, val);
80 val = dma_addr + fec_data->meshyi_oft;
81 rkispp_write(params_vdev->dev, RKISPP_FEC_MESH_YINT_BASE, val);
82
83 val = 0;
84 if (arg->mesh_density)
85 val = SW_MESH_DENSITY;
86 rkispp_set_bits(params_vdev->dev, RKISPP_FEC_CORE_CTRL, SW_MESH_DENSITY, val);
87
88 rkispp_write(params_vdev->dev, RKISPP_FEC_MESH_SIZE, arg->mesh_size);
89
90 val = arg->dst_width << 16 | arg->dst_height;
91 rkispp_write(params_vdev->dev, RKISPP_FEC_DST_SIZE, val);
92
93 val = arg->src_width << 16 | arg->src_height;
94 rkispp_write(params_vdev->dev, RKISPP_FEC_SRC_SIZE, val);
95
96 val = arg->fec_bic_mode;
97 rkispp_set_bits(params_vdev->dev, RKISPP_FEC_CORE_CTRL, SW_BIC_MODE, val);
98 }
99
fec_data_abandon(struct rkispp_params_vdev * vdev,struct fec_params_cfg * params)100 static void fec_data_abandon(struct rkispp_params_vdev *vdev,
101 struct fec_params_cfg *params)
102 {
103 struct rkispp_fec_head *data;
104 int i;
105
106 for (i = 0; i < FEC_MESH_BUF_NUM; i++) {
107 if (params->fec_cfg.buf_fd == vdev->buf_fec[i].dma_fd) {
108 data = (struct rkispp_fec_head *)vdev->buf_fec[i].vaddr;
109 if (data)
110 data->stat = FEC_BUF_INIT;
111 break;
112 }
113 }
114 }
115
rkispp_params_cfg(struct rkispp_params_vdev * params_vdev,u32 frame_id)116 static void rkispp_params_cfg(struct rkispp_params_vdev *params_vdev, u32 frame_id)
117 {
118 struct fec_params_cfg *new_params = NULL;
119 u32 module_en_update, module_cfg_update, module_ens;
120
121 spin_lock(¶ms_vdev->config_lock);
122 if (!params_vdev->streamon) {
123 spin_unlock(¶ms_vdev->config_lock);
124 return;
125 }
126
127 /* get buffer by frame_id */
128 while (!list_empty(¶ms_vdev->params) && !params_vdev->cur_buf) {
129 params_vdev->cur_buf = list_first_entry(¶ms_vdev->params,
130 struct rkispp_buffer, queue);
131
132 new_params = (struct fec_params_cfg *)(params_vdev->cur_buf->vaddr[0]);
133 if (new_params->frame_id < frame_id) {
134 if (new_params->module_cfg_update & ISPP_MODULE_FEC)
135 fec_data_abandon(params_vdev, new_params);
136 list_del(¶ms_vdev->cur_buf->queue);
137 vb2_buffer_done(¶ms_vdev->cur_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
138 params_vdev->cur_buf = NULL;
139 continue;
140 } else if (new_params->frame_id == frame_id) {
141 list_del(¶ms_vdev->cur_buf->queue);
142 } else {
143 params_vdev->cur_buf = NULL;
144 }
145 break;
146 }
147
148 if (!params_vdev->cur_buf) {
149 spin_unlock(¶ms_vdev->config_lock);
150 return;
151 }
152
153 new_params = (struct fec_params_cfg *)(params_vdev->cur_buf->vaddr[0]);
154
155 module_en_update = new_params->module_en_update;
156 module_cfg_update = new_params->module_cfg_update;
157 module_ens = new_params->module_ens;
158 if (params_vdev->dev->hw_dev->is_fec_ext) {
159 module_en_update &= ~ISPP_MODULE_FEC;
160 module_cfg_update &= ~ISPP_MODULE_FEC;
161 module_ens &= ~ISPP_MODULE_FEC;
162 }
163
164
165 if (module_cfg_update & ISPP_MODULE_FEC)
166 fec_config(params_vdev,
167 &new_params->fec_cfg);
168 if (module_en_update & ISPP_MODULE_FEC)
169 fec_enable(params_vdev,
170 !!(module_ens & ISPP_MODULE_FEC));
171
172 vb2_buffer_done(¶ms_vdev->cur_buf->vb.vb2_buf,
173 VB2_BUF_STATE_DONE);
174 params_vdev->cur_buf = NULL;
175
176 spin_unlock(¶ms_vdev->config_lock);
177 }
178
params_vb2_buf_queue(struct vb2_buffer * vb)179 static void params_vb2_buf_queue(struct vb2_buffer *vb)
180 {
181 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
182 struct rkispp_buffer *params_buf = to_rkispp_buffer(vbuf);
183 struct vb2_queue *vq = vb->vb2_queue;
184 struct rkispp_params_vdev *params_vdev = vq->drv_priv;
185 struct fec_params_cfg *new_params;
186 unsigned long flags;
187
188 new_params = (struct fec_params_cfg *)vb2_plane_vaddr(vb, 0);
189 spin_lock_irqsave(¶ms_vdev->config_lock, flags);
190 if (params_vdev->first_params) {
191 params_vdev->first_params = false;
192 wake_up(¶ms_vdev->dev->sync_onoff);
193 }
194 spin_unlock_irqrestore(¶ms_vdev->config_lock, flags);
195 params_buf->vaddr[0] = new_params;
196 spin_lock_irqsave(¶ms_vdev->config_lock, flags);
197 list_add_tail(¶ms_buf->queue, ¶ms_vdev->params);
198 spin_unlock_irqrestore(¶ms_vdev->config_lock, flags);
199 }
200
201 static struct rkispp_params_ops rkispp_params_ops = {
202 .rkispp_params_cfg = rkispp_params_cfg,
203 .rkispp_params_vb2_buf_queue = params_vb2_buf_queue,
204 };
205
rkispp_params_init_ops_v20(struct rkispp_params_vdev * params_vdev)206 void rkispp_params_init_ops_v20(struct rkispp_params_vdev *params_vdev)
207 {
208 params_vdev->params_ops = &rkispp_params_ops;
209 }
210