~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/media/video/mx3_camera.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include <media/v4l2-common.h>
23
23
#include <media/v4l2-dev.h>
24
 
#include <media/videobuf-dma-contig.h>
 
24
#include <media/videobuf2-dma-contig.h>
25
25
#include <media/soc_camera.h>
26
26
#include <media/soc_mediabus.h>
27
27
 
62
62
 
63
63
#define MAX_VIDEO_MEM 16
64
64
 
 
65
enum csi_buffer_state {
 
66
        CSI_BUF_NEEDS_INIT,
 
67
        CSI_BUF_PREPARED,
 
68
};
 
69
 
65
70
struct mx3_camera_buffer {
66
71
        /* common v4l buffer stuff -- must be first */
67
 
        struct videobuf_buffer                  vb;
68
 
        enum v4l2_mbus_pixelcode                code;
 
72
        struct vb2_buffer                       vb;
 
73
        enum csi_buffer_state                   state;
 
74
        struct list_head                        queue;
69
75
 
70
76
        /* One descriptot per scatterlist (per frame) */
71
77
        struct dma_async_tx_descriptor          *txd;
108
114
        struct list_head        capture;
109
115
        spinlock_t              lock;           /* Protects video buffer lists */
110
116
        struct mx3_camera_buffer *active;
 
117
        struct vb2_alloc_ctx    *alloc_ctx;
 
118
        enum v4l2_field         field;
 
119
        int                     sequence;
111
120
 
112
121
        /* IDMAC / dmaengine interface */
113
122
        struct idmac_channel    *idmac_channel[1];      /* We need one channel */
130
139
        __raw_writel(value, mx3->base + reg);
131
140
}
132
141
 
 
142
static struct mx3_camera_buffer *to_mx3_vb(struct vb2_buffer *vb)
 
143
{
 
144
        return container_of(vb, struct mx3_camera_buffer, vb);
 
145
}
 
146
 
133
147
/* Called from the IPU IDMAC ISR */
134
148
static void mx3_cam_dma_done(void *arg)
135
149
{
137
151
        struct dma_chan *chan = desc->txd.chan;
138
152
        struct idmac_channel *ichannel = to_idmac_chan(chan);
139
153
        struct mx3_camera_dev *mx3_cam = ichannel->client;
140
 
        struct videobuf_buffer *vb;
141
154
 
142
155
        dev_dbg(chan->device->dev, "callback cookie %d, active DMA 0x%08x\n",
143
156
                desc->txd.cookie, mx3_cam->active ? sg_dma_address(&mx3_cam->active->sg) : 0);
144
157
 
145
158
        spin_lock(&mx3_cam->lock);
146
159
        if (mx3_cam->active) {
147
 
                vb = &mx3_cam->active->vb;
 
160
                struct vb2_buffer *vb = &mx3_cam->active->vb;
 
161
                struct mx3_camera_buffer *buf = to_mx3_vb(vb);
148
162
 
149
 
                list_del_init(&vb->queue);
150
 
                vb->state = VIDEOBUF_DONE;
151
 
                do_gettimeofday(&vb->ts);
152
 
                vb->field_count++;
153
 
                wake_up(&vb->done);
 
163
                list_del_init(&buf->queue);
 
164
                do_gettimeofday(&vb->v4l2_buf.timestamp);
 
165
                vb->v4l2_buf.field = mx3_cam->field;
 
166
                vb->v4l2_buf.sequence = mx3_cam->sequence++;
 
167
                vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
154
168
        }
155
169
 
156
170
        if (list_empty(&mx3_cam->capture)) {
165
179
        }
166
180
 
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);
171
184
}
172
185
 
173
 
static void free_buffer(struct videobuf_queue *vq, struct mx3_camera_buffer *buf)
174
 
{
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;
179
 
 
180
 
        BUG_ON(in_interrupt());
181
 
 
182
 
        dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
183
 
                vb, vb->baddr, vb->bsize);
184
 
 
185
 
        /*
186
 
         * This waits until this buffer is out of danger, i.e., until it is no
187
 
         * longer in STATE_QUEUED or STATE_ACTIVE
188
 
         */
189
 
        videobuf_waiton(vq, vb, 0, 0);
190
 
        if (txd) {
191
 
                ichan = to_idmac_chan(txd->chan);
192
 
                async_tx_ack(txd);
193
 
        }
194
 
        videobuf_dma_contig_free(vq, vb);
195
 
        buf->txd = NULL;
196
 
 
197
 
        vb->state = VIDEOBUF_NEEDS_INIT;
198
 
}
199
 
 
200
186
/*
201
187
 * Videobuf operations
202
188
 */
203
189
 
204
190
/*
205
191
 * Calculate the __buffer__ (not data) size and number of buffers.
206
 
 * Called with .vb_lock held
207
192
 */
208
 
static int mx3_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
209
 
                              unsigned int *size)
 
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[])
210
196
{
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])
221
207
                return -EINVAL;
222
208
 
223
 
        *size = bytes_per_line * icd->user_height;
 
209
        *num_planes = 1;
 
210
 
 
211
        mx3_cam->sequence = 0;
 
212
        sizes[0] = bytes_per_line * icd->user_height;
 
213
        alloc_ctxs[0] = mx3_cam->alloc_ctx;
224
214
 
225
215
        if (!*count)
226
216
                *count = 32;
227
217
 
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];
230
220
 
231
221
        return 0;
232
222
}
233
223
 
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)
237
225
{
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;
243
232
        size_t new_size;
244
 
        int ret;
245
233
        int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
246
234
                                                icd->current_fmt->host_fmt);
247
235
 
248
236
        if (bytes_per_line < 0)
249
237
                return bytes_per_line;
250
238
 
 
239
        buf = to_mx3_vb(vb);
 
240
        sg = &buf->sg;
 
241
 
251
242
        new_size = bytes_per_line * icd->user_height;
252
243
 
253
 
        /*
254
 
         * I think, in buf_prepare you only have to protect global data,
255
 
         * the actual buffer is yours
256
 
         */
257
 
 
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;
265
 
                vb->field       = field;
266
 
                if (vb->state != VIDEOBUF_NEEDS_INIT)
267
 
                        free_buffer(vq, buf);
268
 
        }
269
 
 
270
 
        if (vb->baddr && vb->bsize < new_size) {
271
 
                /* User provided buffer, but it is too small */
272
 
                ret = -ENOMEM;
273
 
                goto out;
274
 
        }
275
 
 
276
 
        if (vb->state == VIDEOBUF_NEEDS_INIT) {
277
 
                struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
278
 
                struct scatterlist *sg = &buf->sg;
279
 
 
280
 
                /*
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.
284
 
                 */
285
 
                vb->size = new_size;
286
 
 
287
 
                /* This actually (allocates and) maps buffers */
288
 
                ret = videobuf_iolock(vq, vb, NULL);
289
 
                if (ret)
290
 
                        goto fail;
291
 
 
292
 
                /*
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
296
 
                 */
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);
 
247
                return -ENOBUFS;
 
248
        }
 
249
 
 
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;
300
253
 
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);
304
 
                if (!buf->txd) {
305
 
                        ret = -EIO;
306
 
                        goto fail;
307
 
                }
 
257
                if (!buf->txd)
 
258
                        return -EIO;
308
259
 
309
260
                buf->txd->callback_param        = buf->txd;
310
261
                buf->txd->callback              = mx3_cam_dma_done;
311
262
 
312
 
                vb->state = VIDEOBUF_PREPARED;
 
263
                buf->state = CSI_BUF_PREPARED;
313
264
        }
314
265
 
 
266
        vb2_set_plane_payload(vb, 0, new_size);
 
267
 
315
268
        return 0;
316
 
 
317
 
fail:
318
 
        free_buffer(vq, buf);
319
 
out:
320
 
        return ret;
321
269
}
322
270
 
323
271
static enum pixel_fmt fourcc_to_ipu_pix(__u32 fourcc)
324
272
{
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:
335
279
        default:
336
280
                return IPU_PIX_FMT_GENERIC;
337
281
        }
338
282
}
339
283
 
340
 
/*
341
 
 * Called with .vb_lock mutex held and
342
 
 * under spinlock_irqsave(&mx3_cam->lock, ...)
343
 
 */
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)
346
285
{
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;
357
 
 
358
 
        BUG_ON(!irqs_disabled());
 
295
        unsigned long flags;
359
296
 
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;
 
299
 
 
300
        if (video->out_pixel_fmt == IPU_PIX_FMT_GENERIC) {
 
301
                /*
 
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.
 
307
                 */
 
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);
 
311
 
 
312
                video->out_width        = bytes_per_line;
 
313
                video->out_height       = icd->user_height;
 
314
                video->out_stride       = bytes_per_line;
 
315
        } else {
 
316
                /*
 
317
                 * For IPU known formats the pixel unit will be managed
 
318
                 * successfully by the IPU code
 
319
                 */
 
320
                video->out_width        = icd->user_width;
 
321
                video->out_height       = icd->user_height;
 
322
                video->out_stride       = icd->user_width;
 
323
        }
365
324
 
366
325
#ifdef DEBUG
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));
369
329
#endif
370
330
 
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);
372
333
 
373
 
        if (!mx3_cam->active) {
 
334
        if (!mx3_cam->active)
374
335
                mx3_cam->active = buf;
375
 
                vb->state = VIDEOBUF_ACTIVE;
376
 
        } else {
377
 
                vb->state = VIDEOBUF_QUEUED;
378
 
        }
379
336
 
380
337
        spin_unlock_irq(&mx3_cam->lock);
381
338
 
383
340
        dev_dbg(icd->dev.parent, "Submitted cookie %d DMA 0x%08x\n",
384
341
                cookie, sg_dma_address(&buf->sg));
385
342
 
 
343
        if (cookie >= 0)
 
344
                return;
 
345
 
386
346
        spin_lock_irq(&mx3_cam->lock);
387
347
 
388
 
        if (cookie >= 0)
389
 
                return;
390
 
 
391
348
        /* Submit error */
392
 
        vb->state = VIDEOBUF_PREPARED;
393
 
 
394
 
        list_del_init(&vb->queue);
 
349
        list_del_init(&buf->queue);
395
350
 
396
351
        if (mx3_cam->active == buf)
397
352
                mx3_cam->active = NULL;
 
353
 
 
354
        spin_unlock_irqrestore(&mx3_cam->lock, flags);
 
355
        vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
398
356
}
399
357
 
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)
403
359
{
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;
410
366
 
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;
419
 
 
420
 
                list_del_init(&vb->queue);
421
 
                if (mx3_cam->active == buf)
422
 
                        mx3_cam->active = NULL;
423
 
        }
424
 
        spin_unlock_irqrestore(&mx3_cam->lock, flags);
425
 
        free_buffer(vq, buf);
426
 
}
427
 
 
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 ");
 
371
 
 
372
        spin_lock_irqsave(&mx3_cam->lock, flags);
 
373
 
 
374
        if (mx3_cam->active == buf)
 
375
                mx3_cam->active = NULL;
 
376
 
 
377
        /* Doesn't hurt also if the list is empty */
 
378
        list_del_init(&buf->queue);
 
379
        buf->state = CSI_BUF_NEEDS_INIT;
 
380
 
 
381
        if (txd) {
 
382
                buf->txd = NULL;
 
383
                if (mx3_cam->idmac_channel[0])
 
384
                        async_tx_ack(txd);
 
385
        }
 
386
 
 
387
        spin_unlock_irqrestore(&mx3_cam->lock, flags);
 
388
}
 
389
 
 
390
static int mx3_videobuf_init(struct vb2_buffer *vb)
 
391
{
 
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);
 
396
 
 
397
        buf->state = CSI_BUF_NEEDS_INIT;
 
398
        buf->txd = NULL;
 
399
 
 
400
        return 0;
 
401
}
 
402
 
 
403
static int mx3_stop_streaming(struct vb2_queue *q)
 
404
{
 
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;
 
411
        unsigned long flags;
 
412
 
 
413
        if (ichan) {
 
414
                chan = &ichan->dma_chan;
 
415
                chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
 
416
        }
 
417
 
 
418
        spin_lock_irqsave(&mx3_cam->lock, flags);
 
419
 
 
420
        mx3_cam->active = NULL;
 
421
 
 
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);
 
425
        }
 
426
 
 
427
        spin_unlock_irqrestore(&mx3_cam->lock, flags);
 
428
 
 
429
        return 0;
 
430
}
 
431
 
 
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,
433
441
};
434
442
 
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)
437
445
{
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;
 
448
        q->drv_priv = icd;
 
449
        q->ops = &mx3_videobuf_ops;
 
450
        q->mem_ops = &vb2_dma_contig_memops;
 
451
        q->buf_struct_size = sizeof(struct mx3_camera_buffer);
440
452
 
441
 
        videobuf_queue_dma_contig_init(q, &mx3_videobuf_ops, icd->dev.parent,
442
 
                                       &mx3_cam->lock,
443
 
                                       V4L2_BUF_TYPE_VIDEO_CAPTURE,
444
 
                                       V4L2_FIELD_NONE,
445
 
                                       sizeof(struct mx3_camera_buffer), icd,
446
 
                                       &icd->video_lock);
 
453
        return vb2_queue_init(q);
447
454
}
448
455
 
449
456
/* First part of ipu_csi_init_interface() */
538
545
                 icd->devnum);
539
546
}
540
547
 
541
 
static bool channel_change_requested(struct soc_camera_device *icd,
542
 
                                     struct v4l2_rect *rect)
543
 
{
544
 
        struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
545
 
        struct mx3_camera_dev *mx3_cam = ici->priv;
546
 
        struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
547
 
 
548
 
        /* Do buffers have to be re-allocated or channel re-configured? */
549
 
        return ichan && rect->width * rect->height >
550
 
                icd->user_width * icd->user_height;
551
 
}
552
 
 
553
548
static int test_platform_param(struct mx3_camera_dev *mx3_cam,
554
549
                               unsigned char buswidth, unsigned long *flags)
555
550
{
693
688
 
694
689
        fmt = soc_mbus_get_fmtdesc(code);
695
690
        if (!fmt) {
696
 
                dev_err(icd->dev.parent,
697
 
                        "Invalid format code #%u: %d\n", idx, code);
 
691
                dev_warn(icd->dev.parent,
 
692
                         "Unsupported format code #%u: %d\n", idx, code);
698
693
                return 0;
699
694
        }
700
695
 
734
729
        if (xlate) {
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);
737
737
                xlate++;
738
 
                dev_dbg(dev, "Providing format %x in pass-through mode\n",
739
 
                        xlate->host_fmt->fourcc);
740
738
        }
741
739
 
742
740
        return formats;
743
741
}
744
742
 
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)
747
746
{
748
747
        u32 ctrl, width_field, height_field;
749
748
 
 
749
        if (fourcc_to_ipu_pix(fmt->fourcc) == IPU_PIX_FMT_GENERIC) {
 
750
                /*
 
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.
 
754
                 */
 
755
                unsigned int num, den;
 
756
                int ret = soc_mbus_samples_per_pixel(fmt, &num, &den);
 
757
                BUG_ON(ret < 0);
 
758
                width = width * num / den;
 
759
        }
 
760
 
750
761
        /* Setup frame size - this cannot be changed on-the-fly... */
751
762
        width_field = width - 1;
752
763
        height_field = height - 1;
772
783
        struct dma_chan_request rq = {.mx3_cam = mx3_cam,
773
784
                                      .id = IDMAC_IC_7};
774
785
 
775
 
        if (*ichan) {
776
 
                struct videobuf_buffer *vb, *_vb;
777
 
                dma_release_channel(&(*ichan)->dma_chan);
778
 
                *ichan = NULL;
779
 
                mx3_cam->active = NULL;
780
 
                list_for_each_entry_safe(vb, _vb, &mx3_cam->capture, queue) {
781
 
                        list_del_init(&vb->queue);
782
 
                        vb->state = VIDEOBUF_ERROR;
783
 
                        wake_up(&vb->done);
784
 
                }
785
 
        }
786
 
 
787
786
        dma_cap_zero(mask);
788
787
        dma_cap_set(DMA_SLAVE, mask);
789
788
        dma_cap_set(DMA_PRIVATE, mask);
803
802
 */
804
803
static inline void stride_align(__u32 *width)
805
804
{
806
 
        if (((*width + 7) &  ~7) < 4096)
807
 
                *width = (*width + 7) &  ~7;
 
805
        if (ALIGN(*width, 8) < 4096)
 
806
                *width = ALIGN(*width, 8);
808
807
        else
809
808
                *width = *width &  ~7;
810
809
}
830
829
        if (ret < 0)
831
830
                return ret;
832
831
 
833
 
        /* The capture device might have changed its output  */
 
832
        /* The capture device might have changed its output sizes */
834
833
        ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
835
834
        if (ret < 0)
836
835
                return ret;
837
836
 
 
837
        if (mf.code != icd->current_fmt->code)
 
838
                return -EINVAL;
 
839
 
838
840
        if (mf.width & 7) {
839
841
                /* Ouch! We can only handle 8-byte aligned width... */
840
842
                stride_align(&mf.width);
843
845
                        return ret;
844
846
        }
845
847
 
846
 
        if (mf.width != icd->user_width || mf.height != icd->user_height) {
847
 
                /*
848
 
                 * We now know pixel formats and can decide upon DMA-channel(s)
849
 
                 * So far only direct camera-to-memory is supported
850
 
                 */
851
 
                if (channel_change_requested(icd, rect)) {
852
 
                        ret = acquire_dma_channel(mx3_cam);
853
 
                        if (ret < 0)
854
 
                                return ret;
855
 
                }
856
 
 
857
 
                configure_geometry(mx3_cam, mf.width, mf.height);
858
 
        }
 
848
        if (mf.width != icd->user_width || mf.height != icd->user_height)
 
849
                configure_geometry(mx3_cam, mf.width, mf.height,
 
850
                                   icd->current_fmt->host_fmt);
859
851
 
860
852
        dev_dbg(icd->dev.parent, "Sensor cropped %dx%d\n",
861
853
                mf.width, mf.height);
887
879
        stride_align(&pix->width);
888
880
        dev_dbg(icd->dev.parent, "Set format %dx%d\n", pix->width, pix->height);
889
881
 
890
 
        ret = acquire_dma_channel(mx3_cam);
891
 
        if (ret < 0)
892
 
                return ret;
893
 
 
894
882
        /*
895
883
         * Might have to perform a complete interface initialisation like in
896
884
         * ipu_csi_init_interface() in mxc_v4l2_s_param(). Also consider
897
885
         * mxc_v4l2_s_fmt()
898
886
         */
899
887
 
900
 
        configure_geometry(mx3_cam, pix->width, pix->height);
 
888
        configure_geometry(mx3_cam, pix->width, pix->height, xlate->host_fmt);
901
889
 
902
890
        mf.width        = pix->width;
903
891
        mf.height       = pix->height;
912
900
        if (mf.code != xlate->code)
913
901
                return -EINVAL;
914
902
 
 
903
        if (!mx3_cam->idmac_channel[0]) {
 
904
                ret = acquire_dma_channel(mx3_cam);
 
905
                if (ret < 0)
 
906
                        return ret;
 
907
        }
 
908
 
915
909
        pix->width              = mf.width;
916
910
        pix->height             = mf.height;
917
911
        pix->field              = mf.field;
 
912
        mx3_cam->field          = mf.field;
918
913
        pix->colorspace         = mf.colorspace;
919
914
        icd->current_fmt        = xlate;
920
915
 
 
916
        pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
 
917
                                                    xlate->host_fmt);
 
918
        if (pix->bytesperline < 0)
 
919
                return pix->bytesperline;
 
920
        pix->sizeimage = pix->height * pix->bytesperline;
 
921
 
921
922
        dev_dbg(icd->dev.parent, "Sensor set %dx%d\n", pix->width, pix->height);
922
923
 
923
924
        return ret;
991
992
{
992
993
        struct soc_camera_device *icd = file->private_data;
993
994
 
994
 
        return videobuf_poll_stream(file, &icd->vb_vidq, pt);
 
995
        return vb2_poll(&icd->vb2_vidq, file, pt);
995
996
}
996
997
 
997
998
static int mx3_camera_querycap(struct soc_camera_host *ici,
1165
1166
        .set_fmt        = mx3_camera_set_fmt,
1166
1167
        .try_fmt        = mx3_camera_try_fmt,
1167
1168
        .get_formats    = mx3_camera_get_formats,
1168
 
        .init_videobuf  = mx3_camera_init_videobuf,
 
1169
        .init_videobuf2 = mx3_camera_init_videobuf,
1169
1170
        .reqbufs        = mx3_camera_reqbufs,
1170
1171
        .poll           = mx3_camera_poll,
1171
1172
        .querycap       = mx3_camera_querycap,
1241
1242
        soc_host->v4l2_dev.dev  = &pdev->dev;
1242
1243
        soc_host->nr            = pdev->id;
1243
1244
 
 
1245
        mx3_cam->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
 
1246
        if (IS_ERR(mx3_cam->alloc_ctx)) {
 
1247
                err = PTR_ERR(mx3_cam->alloc_ctx);
 
1248
                goto eallocctx;
 
1249
        }
 
1250
 
1244
1251
        err = soc_camera_host_register(soc_host);
1245
1252
        if (err)
1246
1253
                goto ecamhostreg;
1251
1258
        return 0;
1252
1259
 
1253
1260
ecamhostreg:
 
1261
        vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
 
1262
eallocctx:
1254
1263
        iounmap(base);
1255
1264
eioremap:
1256
1265
        clk_put(mx3_cam->clk);
1280
1289
        if (WARN_ON(mx3_cam->idmac_channel[0]))
1281
1290
                dma_release_channel(&mx3_cam->idmac_channel[0]->dma_chan);
1282
1291
 
 
1292
        vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
 
1293
 
1283
1294
        vfree(mx3_cam);
1284
1295
 
1285
1296
        dmaengine_put();