2 * V4L2 Driver for SuperH Mobile CEU interface
4 * Copyright (C) 2008 Magnus Damm
6 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/init.h>
18 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
27 #include <linux/moduleparam.h>
28 #include <linux/time.h>
29 #include <linux/version.h>
30 #include <linux/device.h>
31 #include <linux/platform_device.h>
32 #include <linux/mutex.h>
33 #include <linux/videodev2.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-dev.h>
37 #include <media/soc_camera.h>
38 #include <media/sh_mobile_ceu.h>
39 #include <media/videobuf-dma-contig.h>
41 /* register offsets for sh7722 / sh7723 */
50 #define CSTCR 0x20 /* not on sh7723 */
51 #define CSECR 0x24 /* not on sh7723 */
77 static DEFINE_MUTEX(camera_lock
);
79 /* per video frame buffer */
80 struct sh_mobile_ceu_buffer
{
81 struct videobuf_buffer vb
; /* v4l buffer must be first */
82 const struct soc_camera_data_format
*fmt
;
85 struct sh_mobile_ceu_dev
{
87 struct soc_camera_host ici
;
88 struct soc_camera_device
*icd
;
92 unsigned long video_limit
;
94 /* lock used to protect videobuf */
96 struct list_head capture
;
97 struct videobuf_buffer
*active
;
99 struct sh_mobile_ceu_info
*pdata
;
102 static void ceu_write(struct sh_mobile_ceu_dev
*priv
,
103 unsigned long reg_offs
, unsigned long data
)
105 iowrite32(data
, priv
->base
+ reg_offs
);
108 static unsigned long ceu_read(struct sh_mobile_ceu_dev
*priv
,
109 unsigned long reg_offs
)
111 return ioread32(priv
->base
+ reg_offs
);
115 * Videobuf operations
117 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue
*vq
,
121 struct soc_camera_device
*icd
= vq
->priv_data
;
122 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
123 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
124 int bytes_per_pixel
= (icd
->current_fmt
->depth
+ 7) >> 3;
126 *size
= PAGE_ALIGN(icd
->width
* icd
->height
* bytes_per_pixel
);
131 if (pcdev
->video_limit
) {
132 while (*size
* *count
> pcdev
->video_limit
)
136 dev_dbg(&icd
->dev
, "count=%d, size=%d\n", *count
, *size
);
141 static void free_buffer(struct videobuf_queue
*vq
,
142 struct sh_mobile_ceu_buffer
*buf
)
144 struct soc_camera_device
*icd
= vq
->priv_data
;
146 dev_dbg(&icd
->dev
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
147 &buf
->vb
, buf
->vb
.baddr
, buf
->vb
.bsize
);
152 videobuf_dma_contig_free(vq
, &buf
->vb
);
153 dev_dbg(&icd
->dev
, "%s freed\n", __func__
);
154 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
157 static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev
*pcdev
)
159 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) & ~1);
160 ceu_write(pcdev
, CETCR
, ~ceu_read(pcdev
, CETCR
) & 0x0317f313);
161 ceu_write(pcdev
, CEIER
, ceu_read(pcdev
, CEIER
) | 1);
163 ceu_write(pcdev
, CAPCR
, ceu_read(pcdev
, CAPCR
) & ~0x10000);
165 ceu_write(pcdev
, CETCR
, 0x0317f313 ^ 0x10);
168 ceu_write(pcdev
, CDAYR
, videobuf_to_dma_contig(pcdev
->active
));
169 ceu_write(pcdev
, CAPSR
, 0x1); /* start capture */
173 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue
*vq
,
174 struct videobuf_buffer
*vb
,
175 enum v4l2_field field
)
177 struct soc_camera_device
*icd
= vq
->priv_data
;
178 struct sh_mobile_ceu_buffer
*buf
;
181 buf
= container_of(vb
, struct sh_mobile_ceu_buffer
, vb
);
183 dev_dbg(&icd
->dev
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
184 vb
, vb
->baddr
, vb
->bsize
);
186 /* Added list head initialization on alloc */
187 WARN_ON(!list_empty(&vb
->queue
));
190 /* This can be useful if you want to see if we actually fill
191 * the buffer with something */
192 memset((void *)vb
->baddr
, 0xaa, vb
->bsize
);
195 BUG_ON(NULL
== icd
->current_fmt
);
197 if (buf
->fmt
!= icd
->current_fmt
||
198 vb
->width
!= icd
->width
||
199 vb
->height
!= icd
->height
||
200 vb
->field
!= field
) {
201 buf
->fmt
= icd
->current_fmt
;
202 vb
->width
= icd
->width
;
203 vb
->height
= icd
->height
;
205 vb
->state
= VIDEOBUF_NEEDS_INIT
;
208 vb
->size
= vb
->width
* vb
->height
* ((buf
->fmt
->depth
+ 7) >> 3);
209 if (0 != vb
->baddr
&& vb
->bsize
< vb
->size
) {
214 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
215 ret
= videobuf_iolock(vq
, vb
, NULL
);
218 vb
->state
= VIDEOBUF_PREPARED
;
223 free_buffer(vq
, buf
);
228 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue
*vq
,
229 struct videobuf_buffer
*vb
)
231 struct soc_camera_device
*icd
= vq
->priv_data
;
232 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
233 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
236 dev_dbg(&icd
->dev
, "%s (vb=0x%p) 0x%08lx %zd\n", __func__
,
237 vb
, vb
->baddr
, vb
->bsize
);
239 vb
->state
= VIDEOBUF_ACTIVE
;
240 spin_lock_irqsave(&pcdev
->lock
, flags
);
241 list_add_tail(&vb
->queue
, &pcdev
->capture
);
243 if (!pcdev
->active
) {
245 sh_mobile_ceu_capture(pcdev
);
248 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
251 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue
*vq
,
252 struct videobuf_buffer
*vb
)
254 free_buffer(vq
, container_of(vb
, struct sh_mobile_ceu_buffer
, vb
));
257 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops
= {
258 .buf_setup
= sh_mobile_ceu_videobuf_setup
,
259 .buf_prepare
= sh_mobile_ceu_videobuf_prepare
,
260 .buf_queue
= sh_mobile_ceu_videobuf_queue
,
261 .buf_release
= sh_mobile_ceu_videobuf_release
,
264 static irqreturn_t
sh_mobile_ceu_irq(int irq
, void *data
)
266 struct sh_mobile_ceu_dev
*pcdev
= data
;
267 struct videobuf_buffer
*vb
;
270 spin_lock_irqsave(&pcdev
->lock
, flags
);
273 list_del_init(&vb
->queue
);
275 if (!list_empty(&pcdev
->capture
))
276 pcdev
->active
= list_entry(pcdev
->capture
.next
,
277 struct videobuf_buffer
, queue
);
279 pcdev
->active
= NULL
;
281 sh_mobile_ceu_capture(pcdev
);
283 vb
->state
= VIDEOBUF_DONE
;
284 do_gettimeofday(&vb
->ts
);
287 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
292 static int sh_mobile_ceu_add_device(struct soc_camera_device
*icd
)
294 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
295 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
298 mutex_lock(&camera_lock
);
304 "SuperH Mobile CEU driver attached to camera %d\n",
307 if (pcdev
->pdata
->enable_camera
)
308 pcdev
->pdata
->enable_camera();
310 ret
= icd
->ops
->init(icd
);
314 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
315 while (ceu_read(pcdev
, CSTSR
) & 1)
320 mutex_unlock(&camera_lock
);
325 static void sh_mobile_ceu_remove_device(struct soc_camera_device
*icd
)
327 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
328 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
330 BUG_ON(icd
!= pcdev
->icd
);
332 /* disable capture, disable interrupts */
333 ceu_write(pcdev
, CEIER
, 0);
334 ceu_write(pcdev
, CAPSR
, 1 << 16); /* reset */
335 icd
->ops
->release(icd
);
336 if (pcdev
->pdata
->disable_camera
)
337 pcdev
->pdata
->disable_camera();
340 "SuperH Mobile CEU driver detached from camera %d\n",
346 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device
*icd
,
349 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
350 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
351 int ret
, buswidth
, width
, cfszr_width
, cdwdr_width
;
352 unsigned long camera_flags
, common_flags
, value
;
354 camera_flags
= icd
->ops
->query_bus_param(icd
);
355 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
356 pcdev
->pdata
->flags
);
360 ret
= icd
->ops
->set_bus_param(icd
, common_flags
);
364 switch (common_flags
& SOCAM_DATAWIDTH_MASK
) {
365 case SOCAM_DATAWIDTH_8
:
368 case SOCAM_DATAWIDTH_16
:
375 ceu_write(pcdev
, CRCNTR
, 0);
376 ceu_write(pcdev
, CRCMPR
, 0);
379 value
|= (common_flags
& SOCAM_VSYNC_ACTIVE_LOW
) ? (1 << 1) : 0;
380 value
|= (common_flags
& SOCAM_HSYNC_ACTIVE_LOW
) ? (1 << 0) : 0;
381 value
|= (buswidth
== 16) ? (1 << 12) : 0;
382 ceu_write(pcdev
, CAMCR
, value
);
384 ceu_write(pcdev
, CAPCR
, 0x00300000);
385 ceu_write(pcdev
, CAIFR
, 0);
389 width
= icd
->width
* (icd
->current_fmt
->depth
/ 8);
390 width
= (buswidth
== 16) ? width
/ 2 : width
;
391 cfszr_width
= (buswidth
== 8) ? width
/ 2 : width
;
392 cdwdr_width
= (buswidth
== 16) ? width
* 2 : width
;
394 ceu_write(pcdev
, CAMOR
, 0);
395 ceu_write(pcdev
, CAPWR
, (icd
->height
<< 16) | width
);
396 ceu_write(pcdev
, CFLCR
, 0); /* data fetch mode - no scaling */
397 ceu_write(pcdev
, CFSZR
, (icd
->height
<< 16) | cfszr_width
);
398 ceu_write(pcdev
, CLFCR
, 0); /* data fetch mode - no lowpass filter */
399 ceu_write(pcdev
, CDOCR
, 0x00000016);
401 ceu_write(pcdev
, CDWDR
, cdwdr_width
);
402 ceu_write(pcdev
, CFWCR
, 0); /* keep "datafetch firewall" disabled */
404 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
405 /* in data fetch mode: no need for CDACR, CDBYR, CDBCR */
410 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device
*icd
,
413 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
414 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
415 unsigned long camera_flags
, common_flags
;
417 camera_flags
= icd
->ops
->query_bus_param(icd
);
418 common_flags
= soc_camera_bus_param_compatible(camera_flags
,
419 pcdev
->pdata
->flags
);
426 static int sh_mobile_ceu_set_fmt_cap(struct soc_camera_device
*icd
,
427 __u32 pixfmt
, struct v4l2_rect
*rect
)
429 return icd
->ops
->set_fmt_cap(icd
, pixfmt
, rect
);
432 static int sh_mobile_ceu_try_fmt_cap(struct soc_camera_device
*icd
,
433 struct v4l2_format
*f
)
435 /* FIXME: calculate using depth and bus width */
437 if (f
->fmt
.pix
.height
< 4)
438 f
->fmt
.pix
.height
= 4;
439 if (f
->fmt
.pix
.height
> 1920)
440 f
->fmt
.pix
.height
= 1920;
441 if (f
->fmt
.pix
.width
< 2)
442 f
->fmt
.pix
.width
= 2;
443 if (f
->fmt
.pix
.width
> 2560)
444 f
->fmt
.pix
.width
= 2560;
445 f
->fmt
.pix
.width
&= ~0x01;
446 f
->fmt
.pix
.height
&= ~0x03;
448 /* limit to sensor capabilities */
449 return icd
->ops
->try_fmt_cap(icd
, f
);
452 static int sh_mobile_ceu_reqbufs(struct soc_camera_file
*icf
,
453 struct v4l2_requestbuffers
*p
)
457 /* This is for locking debugging only. I removed spinlocks and now I
458 * check whether .prepare is ever called on a linked buffer, or whether
459 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
460 * it hadn't triggered */
461 for (i
= 0; i
< p
->count
; i
++) {
462 struct sh_mobile_ceu_buffer
*buf
;
464 buf
= container_of(icf
->vb_vidq
.bufs
[i
],
465 struct sh_mobile_ceu_buffer
, vb
);
466 INIT_LIST_HEAD(&buf
->vb
.queue
);
472 static unsigned int sh_mobile_ceu_poll(struct file
*file
, poll_table
*pt
)
474 struct soc_camera_file
*icf
= file
->private_data
;
475 struct sh_mobile_ceu_buffer
*buf
;
477 buf
= list_entry(icf
->vb_vidq
.stream
.next
,
478 struct sh_mobile_ceu_buffer
, vb
.stream
);
480 poll_wait(file
, &buf
->vb
.done
, pt
);
482 if (buf
->vb
.state
== VIDEOBUF_DONE
||
483 buf
->vb
.state
== VIDEOBUF_ERROR
)
484 return POLLIN
|POLLRDNORM
;
489 static int sh_mobile_ceu_querycap(struct soc_camera_host
*ici
,
490 struct v4l2_capability
*cap
)
492 strlcpy(cap
->card
, "SuperH_Mobile_CEU", sizeof(cap
->card
));
493 cap
->version
= KERNEL_VERSION(0, 0, 5);
494 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
498 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue
*q
,
499 struct soc_camera_device
*icd
)
501 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
502 struct sh_mobile_ceu_dev
*pcdev
= ici
->priv
;
504 videobuf_queue_dma_contig_init(q
,
505 &sh_mobile_ceu_videobuf_ops
,
506 &ici
->dev
, &pcdev
->lock
,
507 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
509 sizeof(struct sh_mobile_ceu_buffer
),
513 static struct soc_camera_host_ops sh_mobile_ceu_host_ops
= {
514 .owner
= THIS_MODULE
,
515 .add
= sh_mobile_ceu_add_device
,
516 .remove
= sh_mobile_ceu_remove_device
,
517 .set_fmt_cap
= sh_mobile_ceu_set_fmt_cap
,
518 .try_fmt_cap
= sh_mobile_ceu_try_fmt_cap
,
519 .reqbufs
= sh_mobile_ceu_reqbufs
,
520 .poll
= sh_mobile_ceu_poll
,
521 .querycap
= sh_mobile_ceu_querycap
,
522 .try_bus_param
= sh_mobile_ceu_try_bus_param
,
523 .set_bus_param
= sh_mobile_ceu_set_bus_param
,
524 .init_videobuf
= sh_mobile_ceu_init_videobuf
,
527 static int sh_mobile_ceu_probe(struct platform_device
*pdev
)
529 struct sh_mobile_ceu_dev
*pcdev
;
530 struct resource
*res
;
535 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
536 irq
= platform_get_irq(pdev
, 0);
538 dev_err(&pdev
->dev
, "Not enough CEU platform resources.\n");
543 pcdev
= kzalloc(sizeof(*pcdev
), GFP_KERNEL
);
545 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
550 platform_set_drvdata(pdev
, pcdev
);
551 INIT_LIST_HEAD(&pcdev
->capture
);
552 spin_lock_init(&pcdev
->lock
);
554 pcdev
->pdata
= pdev
->dev
.platform_data
;
557 dev_err(&pdev
->dev
, "CEU platform data not set.\n");
561 base
= ioremap_nocache(res
->start
, res
->end
- res
->start
+ 1);
564 dev_err(&pdev
->dev
, "Unable to ioremap CEU registers.\n");
570 pcdev
->video_limit
= 0; /* only enabled if second resource exists */
571 pcdev
->dev
= &pdev
->dev
;
573 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
575 err
= dma_declare_coherent_memory(&pdev
->dev
, res
->start
,
577 (res
->end
- res
->start
) + 1,
579 DMA_MEMORY_EXCLUSIVE
);
581 dev_err(&pdev
->dev
, "Unable to declare CEU memory.\n");
586 pcdev
->video_limit
= (res
->end
- res
->start
) + 1;
590 err
= request_irq(pcdev
->irq
, sh_mobile_ceu_irq
, IRQF_DISABLED
,
591 pdev
->dev
.bus_id
, pcdev
);
593 dev_err(&pdev
->dev
, "Unable to register CEU interrupt.\n");
594 goto exit_release_mem
;
597 pcdev
->ici
.priv
= pcdev
;
598 pcdev
->ici
.dev
.parent
= &pdev
->dev
;
599 pcdev
->ici
.nr
= pdev
->id
;
600 pcdev
->ici
.drv_name
= pdev
->dev
.bus_id
,
601 pcdev
->ici
.ops
= &sh_mobile_ceu_host_ops
,
603 err
= soc_camera_host_register(&pcdev
->ici
);
610 free_irq(pcdev
->irq
, pcdev
);
612 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
613 dma_release_declared_memory(&pdev
->dev
);
622 static int sh_mobile_ceu_remove(struct platform_device
*pdev
)
624 struct sh_mobile_ceu_dev
*pcdev
= platform_get_drvdata(pdev
);
626 soc_camera_host_unregister(&pcdev
->ici
);
627 free_irq(pcdev
->irq
, pcdev
);
628 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1))
629 dma_release_declared_memory(&pdev
->dev
);
630 iounmap(pcdev
->base
);
635 static struct platform_driver sh_mobile_ceu_driver
= {
637 .name
= "sh_mobile_ceu",
639 .probe
= sh_mobile_ceu_probe
,
640 .remove
= sh_mobile_ceu_remove
,
643 static int __init
sh_mobile_ceu_init(void)
645 return platform_driver_register(&sh_mobile_ceu_driver
);
648 static void __exit
sh_mobile_ceu_exit(void)
650 platform_driver_unregister(&sh_mobile_ceu_driver
);
653 module_init(sh_mobile_ceu_init
);
654 module_exit(sh_mobile_ceu_exit
);
656 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
657 MODULE_AUTHOR("Magnus Damm");
658 MODULE_LICENSE("GPL");