167
181
mx3_cam->active = list_entry(mx3_cam->capture.next,
168
struct mx3_camera_buffer, vb.queue);
169
mx3_cam->active->vb.state = VIDEOBUF_ACTIVE;
182
struct mx3_camera_buffer, queue);
170
183
spin_unlock(&mx3_cam->lock);
173
static void free_buffer(struct videobuf_queue *vq, struct mx3_camera_buffer *buf)
175
struct soc_camera_device *icd = vq->priv_data;
176
struct videobuf_buffer *vb = &buf->vb;
177
struct dma_async_tx_descriptor *txd = buf->txd;
178
struct idmac_channel *ichan;
180
BUG_ON(in_interrupt());
182
dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
183
vb, vb->baddr, vb->bsize);
186
* This waits until this buffer is out of danger, i.e., until it is no
187
* longer in STATE_QUEUED or STATE_ACTIVE
189
videobuf_waiton(vq, vb, 0, 0);
191
ichan = to_idmac_chan(txd->chan);
194
videobuf_dma_contig_free(vq, vb);
197
vb->state = VIDEOBUF_NEEDS_INIT;
201
187
* Videobuf operations
205
191
* Calculate the __buffer__ (not data) size and number of buffers.
206
* Called with .vb_lock held
208
static int mx3_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
193
static int mx3_videobuf_setup(struct vb2_queue *vq,
194
unsigned int *count, unsigned int *num_planes,
195
unsigned long sizes[], void *alloc_ctxs[])
211
struct soc_camera_device *icd = vq->priv_data;
197
struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
212
198
struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
213
199
struct mx3_camera_dev *mx3_cam = ici->priv;
214
200
int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
220
206
if (!mx3_cam->idmac_channel[0])
223
*size = bytes_per_line * icd->user_height;
211
mx3_cam->sequence = 0;
212
sizes[0] = bytes_per_line * icd->user_height;
213
alloc_ctxs[0] = mx3_cam->alloc_ctx;
228
if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
229
*count = MAX_VIDEO_MEM * 1024 * 1024 / *size;
218
if (sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024)
219
*count = MAX_VIDEO_MEM * 1024 * 1024 / sizes[0];
234
/* Called with .vb_lock held */
235
static int mx3_videobuf_prepare(struct videobuf_queue *vq,
236
struct videobuf_buffer *vb, enum v4l2_field field)
224
static int mx3_videobuf_prepare(struct vb2_buffer *vb)
238
struct soc_camera_device *icd = vq->priv_data;
226
struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
239
227
struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
240
228
struct mx3_camera_dev *mx3_cam = ici->priv;
241
struct mx3_camera_buffer *buf =
242
container_of(vb, struct mx3_camera_buffer, vb);
229
struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
230
struct scatterlist *sg;
231
struct mx3_camera_buffer *buf;
245
233
int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
246
234
icd->current_fmt->host_fmt);
248
236
if (bytes_per_line < 0)
249
237
return bytes_per_line;
251
242
new_size = bytes_per_line * icd->user_height;
254
* I think, in buf_prepare you only have to protect global data,
255
* the actual buffer is yours
258
if (buf->code != icd->current_fmt->code ||
259
vb->width != icd->user_width ||
260
vb->height != icd->user_height ||
261
vb->field != field) {
262
buf->code = icd->current_fmt->code;
263
vb->width = icd->user_width;
264
vb->height = icd->user_height;
266
if (vb->state != VIDEOBUF_NEEDS_INIT)
267
free_buffer(vq, buf);
270
if (vb->baddr && vb->bsize < new_size) {
271
/* User provided buffer, but it is too small */
276
if (vb->state == VIDEOBUF_NEEDS_INIT) {
277
struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
278
struct scatterlist *sg = &buf->sg;
281
* The total size of video-buffers that will be allocated / mapped.
282
* *size that we calculated in videobuf_setup gets assigned to
283
* vb->bsize, and now we use the same calculation to get vb->size.
287
/* This actually (allocates and) maps buffers */
288
ret = videobuf_iolock(vq, vb, NULL);
293
* We will have to configure the IDMAC channel. It has two slots
294
* for DMA buffers, we shall enter the first two buffers there,
295
* and then submit new buffers in DMA-ready interrupts
297
sg_init_table(sg, 1);
298
sg_dma_address(sg) = videobuf_to_dma_contig(vb);
299
sg_dma_len(sg) = vb->size;
244
if (vb2_plane_size(vb, 0) < new_size) {
245
dev_err(icd->dev.parent, "Buffer too small (%lu < %zu)\n",
246
vb2_plane_size(vb, 0), new_size);
250
if (buf->state == CSI_BUF_NEEDS_INIT) {
251
sg_dma_address(sg) = vb2_dma_contig_plane_paddr(vb, 0);
252
sg_dma_len(sg) = new_size;
301
254
buf->txd = ichan->dma_chan.device->device_prep_slave_sg(
302
255
&ichan->dma_chan, sg, 1, DMA_FROM_DEVICE,
303
256
DMA_PREP_INTERRUPT);
309
260
buf->txd->callback_param = buf->txd;
310
261
buf->txd->callback = mx3_cam_dma_done;
312
vb->state = VIDEOBUF_PREPARED;
263
buf->state = CSI_BUF_PREPARED;
266
vb2_set_plane_payload(vb, 0, new_size);
318
free_buffer(vq, buf);
323
271
static enum pixel_fmt fourcc_to_ipu_pix(__u32 fourcc)
325
273
/* Add more formats as need arises and test possibilities appear... */
326
274
switch (fourcc) {
327
case V4L2_PIX_FMT_RGB565:
328
return IPU_PIX_FMT_RGB565;
329
275
case V4L2_PIX_FMT_RGB24:
330
276
return IPU_PIX_FMT_RGB24;
331
case V4L2_PIX_FMT_RGB332:
332
return IPU_PIX_FMT_RGB332;
333
case V4L2_PIX_FMT_YUV422P:
334
return IPU_PIX_FMT_YVU422P;
277
case V4L2_PIX_FMT_UYVY:
278
case V4L2_PIX_FMT_RGB565:
336
280
return IPU_PIX_FMT_GENERIC;
341
* Called with .vb_lock mutex held and
342
* under spinlock_irqsave(&mx3_cam->lock, ...)
344
static void mx3_videobuf_queue(struct videobuf_queue *vq,
345
struct videobuf_buffer *vb)
284
static void mx3_videobuf_queue(struct vb2_buffer *vb)
347
struct soc_camera_device *icd = vq->priv_data;
286
struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
348
287
struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
349
288
struct mx3_camera_dev *mx3_cam = ici->priv;
350
struct mx3_camera_buffer *buf =
351
container_of(vb, struct mx3_camera_buffer, vb);
289
struct mx3_camera_buffer *buf = to_mx3_vb(vb);
352
290
struct dma_async_tx_descriptor *txd = buf->txd;
353
291
struct idmac_channel *ichan = to_idmac_chan(txd->chan);
354
292
struct idmac_video_param *video = &ichan->params.video;
355
293
dma_cookie_t cookie;
356
294
u32 fourcc = icd->current_fmt->host_fmt->fourcc;
358
BUG_ON(!irqs_disabled());
360
297
/* This is the configuration of one sg-element */
361
298
video->out_pixel_fmt = fourcc_to_ipu_pix(fourcc);
362
video->out_width = icd->user_width;
363
video->out_height = icd->user_height;
364
video->out_stride = icd->user_width;
300
if (video->out_pixel_fmt == IPU_PIX_FMT_GENERIC) {
302
* If the IPU DMA channel is configured to transport
303
* generic 8-bit data, we have to set up correctly the
304
* geometry parameters upon the current pixel format.
305
* So, since the DMA horizontal parameters are expressed
306
* in bytes not pixels, convert these in the right unit.
308
int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
309
icd->current_fmt->host_fmt);
310
BUG_ON(bytes_per_line <= 0);
312
video->out_width = bytes_per_line;
313
video->out_height = icd->user_height;
314
video->out_stride = bytes_per_line;
317
* For IPU known formats the pixel unit will be managed
318
* successfully by the IPU code
320
video->out_width = icd->user_width;
321
video->out_height = icd->user_height;
322
video->out_stride = icd->user_width;
367
326
/* helps to see what DMA actually has written */
368
memset((void *)vb->baddr, 0xaa, vb->bsize);
327
if (vb2_plane_vaddr(vb, 0))
328
memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
371
list_add_tail(&vb->queue, &mx3_cam->capture);
331
spin_lock_irqsave(&mx3_cam->lock, flags);
332
list_add_tail(&buf->queue, &mx3_cam->capture);
373
if (!mx3_cam->active) {
334
if (!mx3_cam->active)
374
335
mx3_cam->active = buf;
375
vb->state = VIDEOBUF_ACTIVE;
377
vb->state = VIDEOBUF_QUEUED;
380
337
spin_unlock_irq(&mx3_cam->lock);
383
340
dev_dbg(icd->dev.parent, "Submitted cookie %d DMA 0x%08x\n",
384
341
cookie, sg_dma_address(&buf->sg));
386
346
spin_lock_irq(&mx3_cam->lock);
391
348
/* Submit error */
392
vb->state = VIDEOBUF_PREPARED;
394
list_del_init(&vb->queue);
349
list_del_init(&buf->queue);
396
351
if (mx3_cam->active == buf)
397
352
mx3_cam->active = NULL;
354
spin_unlock_irqrestore(&mx3_cam->lock, flags);
355
vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
400
/* Called with .vb_lock held */
401
static void mx3_videobuf_release(struct videobuf_queue *vq,
402
struct videobuf_buffer *vb)
358
static void mx3_videobuf_release(struct vb2_buffer *vb)
404
struct soc_camera_device *icd = vq->priv_data;
360
struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
405
361
struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
406
362
struct mx3_camera_dev *mx3_cam = ici->priv;
407
struct mx3_camera_buffer *buf =
408
container_of(vb, struct mx3_camera_buffer, vb);
363
struct mx3_camera_buffer *buf = to_mx3_vb(vb);
364
struct dma_async_tx_descriptor *txd = buf->txd;
409
365
unsigned long flags;
411
367
dev_dbg(icd->dev.parent,
412
"Release%s DMA 0x%08x (state %d), queue %sempty\n",
368
"Release%s DMA 0x%08x, queue %sempty\n",
413
369
mx3_cam->active == buf ? " active" : "", sg_dma_address(&buf->sg),
414
vb->state, list_empty(&vb->queue) ? "" : "not ");
415
spin_lock_irqsave(&mx3_cam->lock, flags);
416
if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
417
!list_empty(&vb->queue)) {
418
vb->state = VIDEOBUF_ERROR;
420
list_del_init(&vb->queue);
421
if (mx3_cam->active == buf)
422
mx3_cam->active = NULL;
424
spin_unlock_irqrestore(&mx3_cam->lock, flags);
425
free_buffer(vq, buf);
428
static struct videobuf_queue_ops mx3_videobuf_ops = {
429
.buf_setup = mx3_videobuf_setup,
430
.buf_prepare = mx3_videobuf_prepare,
431
.buf_queue = mx3_videobuf_queue,
432
.buf_release = mx3_videobuf_release,
370
list_empty(&buf->queue) ? "" : "not ");
372
spin_lock_irqsave(&mx3_cam->lock, flags);
374
if (mx3_cam->active == buf)
375
mx3_cam->active = NULL;
377
/* Doesn't hurt also if the list is empty */
378
list_del_init(&buf->queue);
379
buf->state = CSI_BUF_NEEDS_INIT;
383
if (mx3_cam->idmac_channel[0])
387
spin_unlock_irqrestore(&mx3_cam->lock, flags);
390
static int mx3_videobuf_init(struct vb2_buffer *vb)
392
struct mx3_camera_buffer *buf = to_mx3_vb(vb);
393
/* This is for locking debugging only */
394
INIT_LIST_HEAD(&buf->queue);
395
sg_init_table(&buf->sg, 1);
397
buf->state = CSI_BUF_NEEDS_INIT;
403
static int mx3_stop_streaming(struct vb2_queue *q)
405
struct soc_camera_device *icd = soc_camera_from_vb2q(q);
406
struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
407
struct mx3_camera_dev *mx3_cam = ici->priv;
408
struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
409
struct dma_chan *chan;
410
struct mx3_camera_buffer *buf, *tmp;
414
chan = &ichan->dma_chan;
415
chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
418
spin_lock_irqsave(&mx3_cam->lock, flags);
420
mx3_cam->active = NULL;
422
list_for_each_entry_safe(buf, tmp, &mx3_cam->capture, queue) {
423
buf->state = CSI_BUF_NEEDS_INIT;
424
list_del_init(&buf->queue);
427
spin_unlock_irqrestore(&mx3_cam->lock, flags);
432
static struct vb2_ops mx3_videobuf_ops = {
433
.queue_setup = mx3_videobuf_setup,
434
.buf_prepare = mx3_videobuf_prepare,
435
.buf_queue = mx3_videobuf_queue,
436
.buf_cleanup = mx3_videobuf_release,
437
.buf_init = mx3_videobuf_init,
438
.wait_prepare = soc_camera_unlock,
439
.wait_finish = soc_camera_lock,
440
.stop_streaming = mx3_stop_streaming,
435
static void mx3_camera_init_videobuf(struct videobuf_queue *q,
443
static int mx3_camera_init_videobuf(struct vb2_queue *q,
436
444
struct soc_camera_device *icd)
438
struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
439
struct mx3_camera_dev *mx3_cam = ici->priv;
446
q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
447
q->io_modes = VB2_MMAP | VB2_USERPTR;
449
q->ops = &mx3_videobuf_ops;
450
q->mem_ops = &vb2_dma_contig_memops;
451
q->buf_struct_size = sizeof(struct mx3_camera_buffer);
441
videobuf_queue_dma_contig_init(q, &mx3_videobuf_ops, icd->dev.parent,
443
V4L2_BUF_TYPE_VIDEO_CAPTURE,
445
sizeof(struct mx3_camera_buffer), icd,
453
return vb2_queue_init(q);
449
456
/* First part of ipu_csi_init_interface() */
735
730
xlate->host_fmt = fmt;
736
731
xlate->code = code;
732
dev_dbg(dev, "Providing format %c%c%c%c in pass-through mode\n",
733
(fmt->fourcc >> (0*8)) & 0xFF,
734
(fmt->fourcc >> (1*8)) & 0xFF,
735
(fmt->fourcc >> (2*8)) & 0xFF,
736
(fmt->fourcc >> (3*8)) & 0xFF);
738
dev_dbg(dev, "Providing format %x in pass-through mode\n",
739
xlate->host_fmt->fourcc);
745
743
static void configure_geometry(struct mx3_camera_dev *mx3_cam,
746
unsigned int width, unsigned int height)
744
unsigned int width, unsigned int height,
745
const struct soc_mbus_pixelfmt *fmt)
748
747
u32 ctrl, width_field, height_field;
749
if (fourcc_to_ipu_pix(fmt->fourcc) == IPU_PIX_FMT_GENERIC) {
751
* As the CSI will be configured to output BAYER, here
752
* the width parameter count the number of samples to
753
* capture to complete the whole image width.
755
unsigned int num, den;
756
int ret = soc_mbus_samples_per_pixel(fmt, &num, &den);
758
width = width * num / den;
750
761
/* Setup frame size - this cannot be changed on-the-fly... */
751
762
width_field = width - 1;
752
763
height_field = height - 1;