hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2022 Rockchip Electronics Co., Ltd */
 
#define pr_fmt(fmt) "isp_rockit: %s:%d " fmt, __func__, __LINE__
 
#include <linux/of.h>
#include <linux/of_platform.h>
#include <soc/rockchip/rockchip_rockit.h>
 
#include "dev.h"
 
static struct rockit_rkcif_cfg *rockit_rkcif_cfg;
 
struct rkcif_rockit_buffer {
   struct rkcif_buffer cif_buf;
   struct dma_buf *dmabuf;
   void *mpi_mem;
   void *mpi_buf;
   struct list_head queue;
   int buf_id;
   union {
       u32 buff_addr;
       void *vaddr;
   };
};
 
static struct rkcif_stream *rkcif_rockit_get_stream(struct rockit_rkcif_cfg *input_rockit_cfg)
{
   struct rkcif_device *cif_dev = NULL;
   struct rkcif_stream *stream = NULL;
   u8 i;
 
   if (!rockit_rkcif_cfg) {
       pr_err("rockit_rkcif_cfg is null get stream failed\n");
       return NULL;
   }
   if (!input_rockit_cfg) {
       pr_err("input is null get stream failed\n");
       return NULL;
   }
 
   for (i = 0; i < rockit_rkcif_cfg->cif_num; i++) {
       if (!strcmp(rockit_rkcif_cfg->rkcif_dev_cfg[i].cif_name,
               input_rockit_cfg->cur_name)) {
           cif_dev = rockit_rkcif_cfg->rkcif_dev_cfg[i].cif_dev;
           break;
       }
   }
 
   if (cif_dev == NULL) {
       pr_err("Can not find cif_dev!");
       return NULL;
   }
 
   switch (input_rockit_cfg->nick_id) {
   case 0:
       stream = &cif_dev->stream[RKCIF_STREAM_MIPI_ID0];
       break;
   case 1:
       stream = &cif_dev->stream[RKCIF_STREAM_MIPI_ID1];
       break;
   case 2:
       stream = &cif_dev->stream[RKCIF_STREAM_MIPI_ID2];
       break;
   case 3:
       stream = &cif_dev->stream[RKCIF_STREAM_MIPI_ID3];
       break;
   default:
       stream = NULL;
       break;
   }
 
   return stream;
}
 
int rkcif_rockit_buf_queue(struct rockit_rkcif_cfg *input_rockit_cfg)
{
   struct rkcif_stream *stream = NULL;
   struct rkcif_rockit_buffer *rkcif_buf = NULL;
   struct rkcif_device *cif_dev = NULL;
   const struct vb2_mem_ops *g_ops = NULL;
   int i, ret, offset, dev_id;
   struct rkcif_stream_cfg *stream_cfg = NULL;
   void *mem = NULL;
   struct sg_table  *sg_tbl;
   unsigned long lock_flags = 0;
 
   stream = rkcif_rockit_get_stream(input_rockit_cfg);
 
   if (stream == NULL) {
       pr_err("the stream is NULL");
       return -EINVAL;
   }
 
   cif_dev = stream->cifdev;
   dev_id = cif_dev->csi_host_idx;
   g_ops = cif_dev->hw_dev->mem_ops;
 
   if (stream->id >= RKCIF_MAX_STREAM_MIPI)
       return -EINVAL;
 
   stream_cfg = &rockit_rkcif_cfg->rkcif_dev_cfg[dev_id].rkcif_stream_cfg[stream->id];
 
   stream_cfg->node = input_rockit_cfg->node;
 
   if (!input_rockit_cfg->buf)
       return -EINVAL;
 
   for (i = 0; i < ROCKIT_BUF_NUM_MAX; i++) {
       if (stream_cfg->buff_id[i] == input_rockit_cfg->mpi_id) {
           input_rockit_cfg->is_alloc = 0;
           break;
       }
   }
 
   if (input_rockit_cfg->is_alloc) {
       for (i = 0; i < ROCKIT_BUF_NUM_MAX; i++) {
           if (stream_cfg->buff_id[i] == 0) {
               stream_cfg->rkcif_buff[i] =
                   kzalloc(sizeof(struct rkcif_rockit_buffer), GFP_KERNEL);
               if (stream_cfg->rkcif_buff[i] == NULL) {
                   pr_err("rkisp_buff alloc failed!\n");
                   return -EINVAL;
               }
               stream_cfg->buff_id[i] = input_rockit_cfg->mpi_id;
               break;
           }
       }
       if (i == ROCKIT_BUF_NUM_MAX)
           return -EINVAL;
 
       rkcif_buf = stream_cfg->rkcif_buff[i];
       rkcif_buf->mpi_buf = input_rockit_cfg->mpibuf;
 
       mem = g_ops->attach_dmabuf(stream->cifdev->hw_dev->dev,
                      input_rockit_cfg->buf,
                      input_rockit_cfg->buf->size,
                      DMA_BIDIRECTIONAL);
       if (IS_ERR(mem))
           pr_err("the g_ops->attach_dmabuf is error!\n");
 
       rkcif_buf->mpi_mem = mem;
       rkcif_buf->dmabuf = input_rockit_cfg->buf;
 
       ret = g_ops->map_dmabuf(mem);
       if (ret)
           pr_err("the g_ops->map_dmabuf is error!\n");
 
       if (stream->cifdev->hw_dev->is_dma_sg_ops) {
           sg_tbl = (struct sg_table *)g_ops->cookie(mem);
           rkcif_buf->buff_addr = sg_dma_address(sg_tbl->sgl);
       } else {
           rkcif_buf->buff_addr = *((u32 *)g_ops->cookie(mem));
       }
       get_dma_buf(input_rockit_cfg->buf);
   } else {
       for (i = 0; i < ROCKIT_BUF_NUM_MAX; i++) {
           rkcif_buf = stream_cfg->rkcif_buff[i];
           if (stream_cfg->buff_id[i] == input_rockit_cfg->mpi_id)
               break;
       }
   }
 
   for (i = 0; i < stream->cif_fmt_out->mplanes; i++)
       rkcif_buf->cif_buf.buff_addr[i] = rkcif_buf->buff_addr;
 
   if (stream->cif_fmt_out->mplanes == 1) {
       for (i = 0; i < stream->cif_fmt_out->cplanes - 1; i++) {
           offset = stream->pixm.plane_fmt[i].bytesperline * stream->pixm.height;
           rkcif_buf->cif_buf.buff_addr[i + 1] =
               rkcif_buf->cif_buf.buff_addr[i] + offset;
       }
   }
 
   v4l2_dbg(2, rkcif_debug, &cif_dev->v4l2_dev,
        "stream:%d rockit_queue buf:0x%x\n",
        stream->id, rkcif_buf->cif_buf.buff_addr[0]);
 
   if (stream_cfg->is_discard)
       return -EINVAL;
 
   spin_lock_irqsave(&stream->vbq_lock, lock_flags);
   list_add_tail(&rkcif_buf->cif_buf.queue, &stream->rockit_buf_head);
   spin_unlock_irqrestore(&stream->vbq_lock, lock_flags);
 
   return 0;
}
 
int rkcif_rockit_buf_done(struct rkcif_stream *stream, struct rkcif_buffer *buf)
{
   struct rkcif_device *dev = stream->cifdev;
   struct rkcif_rockit_buffer *rkcif_buf = NULL;
   struct rkcif_stream_cfg *stream_cfg = NULL;
   u32 dev_id = stream->cifdev->csi_host_idx;
 
   if (!rockit_rkcif_cfg ||
       !rockit_rkcif_cfg->rkcif_rockit_mpibuf_done ||
       stream->id >= RKCIF_MAX_STREAM_MIPI)
       return -EINVAL;
 
   stream_cfg = &rockit_rkcif_cfg->rkcif_dev_cfg[dev_id].rkcif_stream_cfg[stream->id];
   rkcif_buf =
       container_of(buf, struct rkcif_rockit_buffer, cif_buf);
 
   rockit_rkcif_cfg->mpibuf = rkcif_buf->mpi_buf;
   rockit_rkcif_cfg->buf = rkcif_buf->dmabuf;
 
   rockit_rkcif_cfg->frame.u64PTS = buf->vb.vb2_buf.timestamp;
 
   rockit_rkcif_cfg->frame.u32TimeRef = buf->vb.sequence;
 
   rockit_rkcif_cfg->frame.u32Height = stream->pixm.height;
 
   rockit_rkcif_cfg->frame.u32Width = stream->pixm.width;
 
   rockit_rkcif_cfg->frame.enPixelFormat = stream->pixm.pixelformat;
 
   rockit_rkcif_cfg->frame.u32VirWidth = stream->pixm.width;
 
   rockit_rkcif_cfg->frame.u32VirHeight = stream->pixm.height;
 
   rockit_rkcif_cfg->cur_name = dev_name(dev->dev);
 
   rockit_rkcif_cfg->node = stream_cfg->node;
 
   if (list_empty(&stream->rockit_buf_head))
       rockit_rkcif_cfg->is_empty = true;
   else
       rockit_rkcif_cfg->is_empty = false;
 
   if (rockit_rkcif_cfg->rkcif_rockit_mpibuf_done)
       rockit_rkcif_cfg->rkcif_rockit_mpibuf_done(rockit_rkcif_cfg);
 
   return 0;
}
 
static int rkcif_rockit_buf_free(struct rkcif_stream *stream)
{
   struct rkcif_rockit_buffer *rkcif_buf = NULL;
   u32 i = 0, dev_id = stream->cifdev->csi_host_idx;
   const struct vb2_mem_ops *g_ops = stream->cifdev->hw_dev->mem_ops;
   struct rkcif_stream_cfg *stream_cfg = NULL;
 
   if (!rockit_rkcif_cfg || stream->id >= RKCIF_MAX_STREAM_MIPI)
       return -EINVAL;
 
   stream_cfg = &rockit_rkcif_cfg->rkcif_dev_cfg[dev_id].rkcif_stream_cfg[stream->id];
   stream_cfg->is_discard = false;
   for (i = 0; i < ROCKIT_BUF_NUM_MAX; i++) {
       if (stream_cfg->rkcif_buff[i]) {
           rkcif_buf = (struct rkcif_rockit_buffer *)stream_cfg->rkcif_buff[i];
           if (rkcif_buf->mpi_mem) {
               g_ops->unmap_dmabuf(rkcif_buf->mpi_mem);
               g_ops->detach_dmabuf(rkcif_buf->mpi_mem);
               dma_buf_put(rkcif_buf->dmabuf);
           }
           kfree(stream_cfg->rkcif_buff[i]);
           stream_cfg->rkcif_buff[i] = NULL;
           stream_cfg->buff_id[i] = 0;
       }
   }
   stream->curr_buf_rockit = NULL;
   stream->next_buf_rockit = NULL;
   INIT_LIST_HEAD(&stream->rockit_buf_head);
   return 0;
}
 
int rkcif_rockit_pause_stream(struct rockit_rkcif_cfg *input_rockit_cfg)
{
   struct rkcif_stream *stream = NULL;
 
   stream = rkcif_rockit_get_stream(input_rockit_cfg);
 
   if (stream == NULL) {
       pr_err("the stream is NULL");
       return -EINVAL;
   }
 
   rkcif_do_stop_stream(stream, RKCIF_STREAM_MODE_ROCKIT);
   rkcif_rockit_buf_free(stream);
   return 0;
}
EXPORT_SYMBOL(rkcif_rockit_pause_stream);
 
int rkcif_rockit_config_stream(struct rockit_rkcif_cfg *input_rockit_cfg,
               int width, int height, int v4l2_fmt)
{
   struct rkcif_stream *stream = NULL;
   int ret;
 
   stream = rkcif_rockit_get_stream(input_rockit_cfg);
 
   if (stream == NULL) {
       pr_err("the stream is NULL");
       return -EINVAL;
   }
   stream->pixm.pixelformat = v4l2_fmt;
   stream->pixm.width = width;
   stream->pixm.height = height;
   stream->pixm.plane_fmt[0].bytesperline = 0;
   ret = rkcif_set_fmt(stream, &stream->pixm, false);
   if (ret < 0) {
       pr_err("stream id %d config failed\n", stream->id);
       return -EINVAL;
   }
 
   return 0;
}
EXPORT_SYMBOL(rkcif_rockit_config_stream);
 
int rkcif_rockit_resume_stream(struct rockit_rkcif_cfg *input_rockit_cfg)
{
   struct rkcif_stream *stream = NULL;
   int ret = 0;
 
   stream = rkcif_rockit_get_stream(input_rockit_cfg);
 
   if (stream == NULL) {
       pr_err("the stream is NULL");
       return -EINVAL;
   }
 
   ret = rkcif_do_start_stream(stream, RKCIF_STREAM_MODE_ROCKIT);
   if (ret < 0) {
       pr_err("stream id %d start failed\n", stream->id);
       return -EINVAL;
   }
 
   return 0;
}
EXPORT_SYMBOL(rkcif_rockit_resume_stream);
 
void rkcif_rockit_dev_init(struct rkcif_device *dev)
{
   int i;
 
   if (rockit_rkcif_cfg == NULL) {
       rockit_rkcif_cfg = kzalloc(sizeof(struct rockit_rkcif_cfg), GFP_KERNEL);
       if (rockit_rkcif_cfg == NULL)
           return;
   }
   rockit_rkcif_cfg->cif_num = dev->hw_dev->dev_num;
   for (i = 0; i < rockit_rkcif_cfg->cif_num; i++) {
       if (dev->hw_dev->cif_dev[i]) {
           rockit_rkcif_cfg->rkcif_dev_cfg[i].cif_name = dev_name(dev->hw_dev->cif_dev[i]->dev);
           rockit_rkcif_cfg->rkcif_dev_cfg[i].cif_dev =
               dev->hw_dev->cif_dev[i];
       }
   }
}
 
void rkcif_rockit_dev_deinit(void)
{
   kfree(rockit_rkcif_cfg);
   rockit_rkcif_cfg = NULL;
}
 
void *rkcif_rockit_function_register(void *function, int cmd)
{
   if (rockit_rkcif_cfg == NULL) {
       pr_err("rockit_cfg is null function register failed");
       return NULL;
   }
 
   switch (cmd) {
   case ROCKIT_BUF_QUE:
       function = rkcif_rockit_buf_queue;
       break;
   case ROCKIT_MPIBUF_DONE:
       rockit_rkcif_cfg->rkcif_rockit_mpibuf_done = function;
       if (!rockit_rkcif_cfg->rkcif_rockit_mpibuf_done)
           pr_err("get rkcif_rockit_mpibuf_done failed!");
       break;
   default:
       break;
   }
   return function;
}
EXPORT_SYMBOL(rkcif_rockit_function_register);
 
int rkcif_rockit_get_cifdev(char **name)
{
   int i = 0;
 
   if (rockit_rkcif_cfg == NULL) {
       pr_err("rockit_cfg is null");
       return -EINVAL;
   }
 
   if (name == NULL) {
       pr_err("the name is null");
       return -EINVAL;
   }
 
   for (i = 0; i < rockit_rkcif_cfg->cif_num; i++)
       name[i] = (char *)rockit_rkcif_cfg->rkcif_dev_cfg[i].cif_name;
   if (name[0] == NULL)
       return -EINVAL;
   else
       return 0;
}
EXPORT_SYMBOL(rkcif_rockit_get_cifdev);