30 #include <linux/kernel.h>
32 #include <linux/videodev2.h>
33 #include <linux/pci.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
39 #include <linux/module.h>
46 #define OMAP24XXCAM_VERSION "0.0.1"
48 #define RESET_TIMEOUT_NS 10000
56 static int video_nr = -1;
61 static int capture_mem = 1280 * 960 * 2 * 2;
86 if (IS_ERR(cam->
fck)) {
88 rval = PTR_ERR(cam->
fck);
89 omap24xxcam_clock_put(cam);
94 if (IS_ERR(cam->
ick)) {
96 rval = PTR_ERR(cam->
ick);
97 omap24xxcam_clock_put(cam);
195 const u32 cc_irqstatus_err =
207 if (cc_irqstatus & cc_irqstatus_err
209 dev_dbg(cam->
dev,
"resetting camera, cc_irqstatus 0x%x\n",
211 omap24xxcam_reset(cam);
230 static void omap24xxcam_vbq_free_mmap_buffer(
struct videobuf_buffer *vb)
244 page = sg_page(&dma->
sglist[i]);
246 ClearPageReserved(page++);
257 static void omap24xxcam_vbq_free_mmap_buffers(
struct videobuf_queue *vbq)
268 vbq->
ops->buf_release(vbq, vbq->
bufs[i]);
269 omap24xxcam_vbq_free_mmap_buffer(vbq->
bufs[i]);
283 static int omap24xxcam_vbq_alloc_mmap_buffer(
struct videobuf_buffer *vb)
288 int max_pages,
err = 0, i = 0;
314 while (page ==
NULL) {
317 if (page ==
NULL && !order) {
336 SetPageReserved(page++);
349 omap24xxcam_vbq_free_mmap_buffer(vb);
353 static int omap24xxcam_vbq_alloc_mmap_buffers(
struct videobuf_queue *vbq,
362 for (i = 0; i <
count; i++) {
363 err = omap24xxcam_vbq_alloc_mmap_buffer(vbq->
bufs[i]);
366 dev_dbg(fh->
cam->dev,
"sglen is %d for buffer %d\n",
376 omap24xxcam_vbq_free_mmap_buffer(vbq->
bufs[i]);
402 omap24xxcam_core_disable(cam);
407 if (csr & csr_error) {
410 dev_dbg(cam->
dev,
"resetting camera, csr 0x%x\n", csr);
411 omap24xxcam_reset(cam);
450 if (*cnt > VIDEO_MAX_FRAME)
453 *size = fh->
pix.sizeimage;
456 if (*size * *cnt > fh->
cam->capture_mem)
457 *cnt = fh->
cam->capture_mem / *
size;
491 if (fh->
pix.sizeimage > vb->
bsize) {
502 if (fh->
pix.sizeimage > vb->
size) {
509 omap24xxcam_vbq_release(vbq, vb);
539 omap24xxcam_vbq_release(vbq, vb);
563 omap24xxcam_vbq_complete, vb);
569 omap24xxcam_core_enable(cam);
579 dev_err(cam->
dev,
"failed to queue a video buffer for dma!\n");
580 dev_err(cam->
dev,
"likely a bug in the driver!\n");
586 .buf_setup = omap24xxcam_vbq_setup,
587 .buf_prepare = omap24xxcam_vbq_prepare,
588 .buf_queue = omap24xxcam_vbq_queue,
589 .buf_release = omap24xxcam_vbq_release,
618 & CAM_SYSSTATUS_RESETDONE))
619 dev_err(cam->
dev,
"camera soft reset timeout\n");
627 omap24xxcam_poweron_reset(cam);
638 omap24xxcam_core_hwinit(cam);
646 static void omap24xxcam_stalled_dma_reset(
unsigned long data)
651 dev_dbg(cam->
dev,
"dma stalled, resetting camera\n");
652 omap24xxcam_reset(cam);
678 omap24xxcam_core_disable(cam);
705 omap24xxcam_hwinit(cam);
707 omap24xxcam_sensor_if_enable(cam);
712 omap24xxcam_core_enable(cam);
734 omap24xxcam_capture_stop(cam);
735 omap24xxcam_capture_cont(cam);
745 unsigned int irqhandled = 0;
756 omap24xxcam_core_isr(cam);
760 dev_err(cam->
dev,
"unhandled camera MMU interrupt!\n");
781 rval = vidioc_int_g_ifparm(cam->
sdev, &
p);
783 dev_err(cam->
dev,
"vidioc_int_g_ifparm failed with %d\n", rval);
793 if (
p.u.bt656.frame_start_on_rising_vs)
795 if (
p.u.bt656.bt_sync_correct)
799 if (
p.u.bt656.latch_clk_inv)
801 if (
p.u.bt656.nobt_hs_inv)
803 if (
p.u.bt656.nobt_vs_inv)
806 switch (
p.u.bt656.mode) {
824 "bt656 interface mode %d not supported\n",
833 if (
p.u.bt656.clock_curr != cam->
if_u.
bt656.xclk) {
834 u32 xclk =
p.u.bt656.clock_curr;
846 if (
CAM_MCLK / divisor <
p.u.bt656.clock_min
854 if (xclk <
p.u.bt656.clock_min
855 || xclk >
p.u.bt656.clock_max)
860 omap24xxcam_core_xclk_set(cam, cam->
if_u.
bt656.xclk);
864 dev_err(cam->
dev,
"interface type %d not supported\n",
876 omap24xxcam_core_xclk_set(cam, 0);
889 omap24xxcam_clock_on(cam);
890 err = omap24xxcam_sensor_if_enable(cam);
892 dev_err(cam->
dev,
"sensor interface could not be enabled at "
893 "initialisation, %d\n", err);
899 vidioc_int_s_power(sdev, 1);
901 err = vidioc_int_dev_init(sdev);
903 dev_err(cam->
dev,
"cannot initialize sensor, error %d\n", err);
912 omap24xxcam_sensor_if_disable(cam);
913 omap24xxcam_clock_off(cam);
915 vidioc_int_s_power(sdev, 0);
923 vidioc_int_dev_exit(cam->
sdev);
928 omap24xxcam_sensor_if_disable(cam);
929 omap24xxcam_clock_off(cam);
930 vidioc_int_s_power(cam->
sdev, 0);
940 omap24xxcam_clock_on(cam);
942 omap24xxcam_sensor_if_enable(cam);
944 rval = vidioc_int_s_power(cam->
sdev, 1);
948 rval = vidioc_int_init(cam->
sdev);
955 omap24xxcam_sensor_disable(cam);
969 omap24xxcam_capture_stop(cam);
971 if (vidioc_int_reset(cam->
sdev) == 0) {
972 vidioc_int_init(cam->
sdev);
975 omap24xxcam_sensor_disable(cam);
976 omap24xxcam_sensor_enable(cam);
979 omap24xxcam_capture_cont(cam);
988 static int vidioc_querycap(
struct file *
file,
void *fh,
1001 static int vidioc_enum_fmt_vid_cap(
struct file *
file,
void *fh,
1008 rval = vidioc_int_enum_fmt_cap(cam->
sdev, f);
1013 static int vidioc_g_fmt_vid_cap(
struct file *
file,
void *fh,
1021 rval = vidioc_int_g_fmt_cap(cam->
sdev, f);
1027 static int vidioc_s_fmt_vid_cap(
struct file *
file,
void *fh,
1040 rval = vidioc_int_s_fmt_cap(cam->
sdev, f);
1051 memset(f, 0,
sizeof(*f));
1052 vidioc_g_fmt_vid_cap(file, fh, f);
1057 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *fh,
1065 rval = vidioc_int_try_fmt_cap(cam->
sdev, f);
1071 static int vidioc_reqbufs(
struct file *file,
void *fh,
1084 omap24xxcam_vbq_free_mmap_buffers(&ofh->
vbq);
1096 rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->
vbq, rval);
1098 omap24xxcam_vbq_free_mmap_buffers(&ofh->
vbq);
1104 static int vidioc_querybuf(
struct file *file,
void *fh,
1112 static int vidioc_qbuf(
struct file *file,
void *fh,
struct v4l2_buffer *b)
1119 static int vidioc_dqbuf(
struct file *file,
void *fh,
struct v4l2_buffer *b)
1126 videobuf_dqbuf_again:
1135 rval = vidioc_int_g_needs_reset(cam->
sdev, (
void *)vb->
baddr);
1151 goto videobuf_dqbuf_again;
1162 static int vidioc_streamon(
struct file *file,
void *fh,
enum v4l2_buf_type i)
1174 rval = omap24xxcam_sensor_if_enable(cam);
1176 dev_dbg(cam->
dev,
"vidioc_int_g_ifparm failed\n");
1192 static int vidioc_streamoff(
struct file *file,
void *fh,
enum v4l2_buf_type i)
1216 static int vidioc_enum_input(
struct file *file,
void *fh,
1228 static int vidioc_g_input(
struct file *file,
void *fh,
unsigned int *i)
1235 static int vidioc_s_input(
struct file *file,
void *fh,
unsigned int i)
1243 static int vidioc_queryctrl(
struct file *file,
void *fh,
1250 rval = vidioc_int_queryctrl(cam->
sdev, a);
1255 static int vidioc_g_ctrl(
struct file *file,
void *fh,
1263 rval = vidioc_int_g_ctrl(cam->
sdev, a);
1269 static int vidioc_s_ctrl(
struct file *file,
void *fh,
1277 rval = vidioc_int_s_ctrl(cam->
sdev, a);
1283 static int vidioc_g_parm(
struct file *file,
void *fh,
1290 rval = vidioc_int_g_parm(cam->
sdev, a);
1296 static int vidioc_s_parm(
struct file *file,
void *fh,
1311 rval = vidioc_int_g_parm(cam->
sdev, &old_streamparm);
1315 rval = vidioc_int_s_parm(cam->
sdev, a);
1319 rval = omap24xxcam_sensor_if_enable(cam);
1325 vidioc_int_s_parm(cam->
sdev, &old_streamparm);
1339 static unsigned int omap24xxcam_poll(
struct file *file,
1354 if (list_empty(&fh->
vbq.stream)) {
1361 poll_wait(file, &vb->
done, wait);
1369 static int omap24xxcam_mmap_buffers(
struct file *file,
1411 for (j = 0; j < dma->
sglen; j++) {
1428 static int omap24xxcam_mmap(
struct file *file,
struct vm_area_struct *vma)
1441 rval = omap24xxcam_mmap_buffers(file, vma);
1452 static int omap24xxcam_open(
struct file *file)
1458 if (!cam || !cam->
vfd)
1466 if (cam->
sdev ==
NULL || !try_module_get(cam->
sdev->module)) {
1468 goto out_try_module_get;
1472 omap24xxcam_hwinit(cam);
1473 if (omap24xxcam_sensor_enable(cam)) {
1475 goto out_omap24xxcam_sensor_enable;
1498 out_omap24xxcam_sensor_enable:
1499 omap24xxcam_poweron_reset(cam);
1500 module_put(cam->
sdev->module);
1508 static int omap24xxcam_release(
struct file *file)
1531 omap24xxcam_vbq_free_mmap_buffers(&fh->
vbq);
1543 omap24xxcam_sensor_disable(cam);
1544 omap24xxcam_poweron_reset(cam);
1550 module_put(cam->
sdev->module);
1558 .poll = omap24xxcam_poll,
1559 .mmap = omap24xxcam_mmap,
1560 .open = omap24xxcam_open,
1561 .release = omap24xxcam_release,
1579 omap24xxcam_capture_stop(cam);
1581 omap24xxcam_sensor_disable(cam);
1582 omap24xxcam_poweron_reset(cam);
1594 omap24xxcam_hwinit(cam);
1595 omap24xxcam_sensor_enable(cam);
1598 omap24xxcam_capture_cont(cam);
1645 dev_err(cam->
dev,
"could not register sysfs entry\n");
1653 dev_err(cam->
dev,
"could not allocate video device struct\n");
1662 vfd->
fops = &omap24xxcam_fops;
1663 vfd->
ioctl_ops = &omap24xxcam_ioctl_fops;
1665 omap24xxcam_hwinit(cam);
1667 rval = omap24xxcam_sensor_init(cam);
1672 dev_err(cam->
dev,
"could not register V4L device\n");
1677 omap24xxcam_poweron_reset(cam);
1680 video_device_node_name(vfd));
1685 omap24xxcam_device_unregister(s);
1694 omap24xxcam_sensor_exit(cam);
1697 if (!video_is_registered(cam->
vfd)) {
1720 .attach = omap24xxcam_device_register,
1721 .detach = omap24xxcam_device_unregister,
1729 .master = &omap24xxcam_master
1747 dev_err(&pdev->
dev,
"could not allocate memory\n");
1751 platform_set_drvdata(pdev, cam);
1760 if (capture_mem < 320 * 240 * 2 * 2)
1761 capture_mem = 320 * 240 * 2 * 2;
1772 "cannot reserve camera register I/O region\n");
1781 dev_err(cam->
dev,
"cannot map camera register I/O region\n");
1794 "could not install interrupt service routine\n");
1799 if (omap24xxcam_clock_get(cam))
1809 omap24xxcam_stalled_dma_reset,
1810 (
unsigned long)cam);
1812 omap24xxcam.
priv = cam;
1820 omap24xxcam_remove(pdev);
1835 omap24xxcam_clock_put(cam);
1858 .probe = omap24xxcam_probe,
1859 .remove = omap24xxcam_remove,
1861 .suspend = omap24xxcam_suspend,
1862 .resume = omap24xxcam_resume,
1878 "Minor number for video device (-1 ==> auto assign)");
1881 "buffers (default 4800kiB)");