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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2016 NextThing Co
* Copyright (C) 2016-2019 Bootlin
*
* Author: Maxime Ripard <maxime.ripard@bootlin.com>
*/
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <media/videobuf2-dma-contig.h>
#include <media/videobuf2-v4l2.h>
#include "sun4i_csi.h"
struct sun4i_csi_buffer {
struct vb2_v4l2_buffer vb;
struct list_head list;
};
static inline struct sun4i_csi_buffer *
vb2_v4l2_to_csi_buffer(const struct vb2_v4l2_buffer *p)
{
return container_of(p, struct sun4i_csi_buffer, vb);
}
static inline struct sun4i_csi_buffer *
vb2_to_csi_buffer(const struct vb2_buffer *p)
{
return vb2_v4l2_to_csi_buffer(to_vb2_v4l2_buffer(p));
}
static void sun4i_csi_capture_start(struct sun4i_csi *csi)
{
writel(CSI_CPT_CTRL_VIDEO_START, csi->regs + CSI_CPT_CTRL_REG);
}
static void sun4i_csi_capture_stop(struct sun4i_csi *csi)
{
writel(0, csi->regs + CSI_CPT_CTRL_REG);
}
static int sun4i_csi_queue_setup(struct vb2_queue *vq,
unsigned int *nbuffers,
unsigned int *nplanes,
unsigned int sizes[],
struct device *alloc_devs[])
{
struct sun4i_csi *csi = vb2_get_drv_priv(vq);
unsigned int num_planes = csi->fmt.num_planes;
unsigned int i;
if (*nplanes) {
if (*nplanes != num_planes)
return -EINVAL;
for (i = 0; i < num_planes; i++)
if (sizes[i] < csi->fmt.plane_fmt[i].sizeimage)
return -EINVAL;
return 0;
}
*nplanes = num_planes;
for (i = 0; i < num_planes; i++)
sizes[i] = csi->fmt.plane_fmt[i].sizeimage;
return 0;
};
static int sun4i_csi_buffer_prepare(struct vb2_buffer *vb)
{
struct sun4i_csi *csi = vb2_get_drv_priv(vb->vb2_queue);
unsigned int i;
for (i = 0; i < csi->fmt.num_planes; i++) {
unsigned long size = csi->fmt.plane_fmt[i].sizeimage;
if (vb2_plane_size(vb, i) < size) {
dev_err(csi->dev, "buffer too small (%lu < %lu)\n",
vb2_plane_size(vb, i), size);
return -EINVAL;
}
vb2_set_plane_payload(vb, i, size);
}
return 0;
}
static int sun4i_csi_setup_scratch_buffer(struct sun4i_csi *csi,
unsigned int slot)
{
dma_addr_t addr = csi->scratch.paddr;
unsigned int plane;
dev_dbg(csi->dev,
"No more available buffer, using the scratch buffer\n");
for (plane = 0; plane < csi->fmt.num_planes; plane++) {
writel(addr, csi->regs + CSI_BUF_ADDR_REG(plane, slot));
addr += csi->fmt.plane_fmt[plane].sizeimage;
}
csi->current_buf[slot] = NULL;
return 0;
}
static int sun4i_csi_buffer_fill_slot(struct sun4i_csi *csi, unsigned int slot)
{
struct sun4i_csi_buffer *c_buf;
struct vb2_v4l2_buffer *v_buf;
unsigned int plane;
/*
* We should never end up in a situation where we overwrite an
* already filled slot.
*/
if (WARN_ON(csi->current_buf[slot]))
return -EINVAL;
if (list_empty(&csi->buf_list))
return sun4i_csi_setup_scratch_buffer(csi, slot);
c_buf = list_first_entry(&csi->buf_list, struct sun4i_csi_buffer, list);
list_del_init(&c_buf->list);
v_buf = &c_buf->vb;
csi->current_buf[slot] = v_buf;
for (plane = 0; plane < csi->fmt.num_planes; plane++) {
dma_addr_t buf_addr;
buf_addr = vb2_dma_contig_plane_dma_addr(&v_buf->vb2_buf,
plane);
writel(buf_addr, csi->regs + CSI_BUF_ADDR_REG(plane, slot));
}
return 0;
}
static int sun4i_csi_buffer_fill_all(struct sun4i_csi *csi)
{
unsigned int slot;
int ret;
for (slot = 0; slot < CSI_MAX_BUFFER; slot++) {
ret = sun4i_csi_buffer_fill_slot(csi, slot);
if (ret)
return ret;
}
return 0;
}
static void sun4i_csi_buffer_mark_done(struct sun4i_csi *csi,
unsigned int slot,
unsigned int sequence)
{
struct vb2_v4l2_buffer *v_buf;
if (!csi->current_buf[slot]) {
dev_dbg(csi->dev, "Scratch buffer was used, ignoring..\n");
return;
}
v_buf = csi->current_buf[slot];
v_buf->field = csi->fmt.field;
v_buf->sequence = sequence;
v_buf->vb2_buf.timestamp = ktime_get_ns();
vb2_buffer_done(&v_buf->vb2_buf, VB2_BUF_STATE_DONE);
csi->current_buf[slot] = NULL;
}
static int sun4i_csi_buffer_flip(struct sun4i_csi *csi, unsigned int sequence)
{
u32 reg = readl(csi->regs + CSI_BUF_CTRL_REG);
unsigned int next;
/* Our next buffer is not the current buffer */
next = !(reg & CSI_BUF_CTRL_DBS);
/* Report the previous buffer as done */
sun4i_csi_buffer_mark_done(csi, next, sequence);
/* Put a new buffer in there */
return sun4i_csi_buffer_fill_slot(csi, next);
}
static void sun4i_csi_buffer_queue(struct vb2_buffer *vb)
{
struct sun4i_csi *csi = vb2_get_drv_priv(vb->vb2_queue);
struct sun4i_csi_buffer *buf = vb2_to_csi_buffer(vb);
unsigned long flags;
spin_lock_irqsave(&csi->qlock, flags);
list_add_tail(&buf->list, &csi->buf_list);
spin_unlock_irqrestore(&csi->qlock, flags);
}
static void return_all_buffers(struct sun4i_csi *csi,
enum vb2_buffer_state state)
{
struct sun4i_csi_buffer *buf, *node;
unsigned int slot;
list_for_each_entry_safe(buf, node, &csi->buf_list, list) {
vb2_buffer_done(&buf->vb.vb2_buf, state);
list_del(&buf->list);
}
for (slot = 0; slot < CSI_MAX_BUFFER; slot++) {
struct vb2_v4l2_buffer *v_buf = csi->current_buf[slot];
if (!v_buf)
continue;
vb2_buffer_done(&v_buf->vb2_buf, state);
csi->current_buf[slot] = NULL;
}
}
static int sun4i_csi_start_streaming(struct vb2_queue *vq, unsigned int count)
{
struct sun4i_csi *csi = vb2_get_drv_priv(vq);
struct v4l2_fwnode_bus_parallel *bus = &csi->bus;
const struct sun4i_csi_format *csi_fmt;
unsigned long href_pol, pclk_pol, vref_pol;
|