14 #include <linux/module.h>
18 #include <linux/errno.h>
21 #include <linux/kernel.h>
24 #include <linux/time.h>
25 #include <linux/device.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
37 #include <linux/videodev2.h>
42 #define PXA_CAM_VERSION "0.0.6"
43 #define PXA_CAM_DRV_NAME "pxa27x-camera"
58 #define CICR0_DMAEN (1 << 31)
59 #define CICR0_PAR_EN (1 << 30)
60 #define CICR0_SL_CAP_EN (1 << 29)
61 #define CICR0_ENB (1 << 28)
62 #define CICR0_DIS (1 << 27)
63 #define CICR0_SIM (0x7 << 24)
64 #define CICR0_TOM (1 << 9)
65 #define CICR0_RDAVM (1 << 8)
66 #define CICR0_FEM (1 << 7)
67 #define CICR0_EOLM (1 << 6)
68 #define CICR0_PERRM (1 << 5)
69 #define CICR0_QDM (1 << 4)
70 #define CICR0_CDM (1 << 3)
71 #define CICR0_SOFM (1 << 2)
72 #define CICR0_EOFM (1 << 1)
73 #define CICR0_FOM (1 << 0)
75 #define CICR1_TBIT (1 << 31)
76 #define CICR1_RGBT_CONV (0x3 << 29)
77 #define CICR1_PPL (0x7ff << 15)
78 #define CICR1_RGB_CONV (0x7 << 12)
79 #define CICR1_RGB_F (1 << 11)
80 #define CICR1_YCBCR_F (1 << 10)
81 #define CICR1_RGB_BPP (0x7 << 7)
82 #define CICR1_RAW_BPP (0x3 << 5)
83 #define CICR1_COLOR_SP (0x3 << 3)
84 #define CICR1_DW (0x7 << 0)
86 #define CICR2_BLW (0xff << 24)
88 #define CICR2_ELW (0xff << 16)
90 #define CICR2_HSW (0x3f << 10)
91 #define CICR2_BFPW (0x3f << 3)
93 #define CICR2_FSW (0x7 << 0)
96 #define CICR3_BFW (0xff << 24)
98 #define CICR3_EFW (0xff << 16)
100 #define CICR3_VSW (0x3f << 10)
101 #define CICR3_BFPW (0x3f << 3)
103 #define CICR3_LPF (0x7ff << 0)
105 #define CICR4_MCLK_DLY (0x3 << 24)
106 #define CICR4_PCLK_EN (1 << 23)
107 #define CICR4_PCP (1 << 22)
108 #define CICR4_HSP (1 << 21)
109 #define CICR4_VSP (1 << 20)
110 #define CICR4_MCLK_EN (1 << 19)
111 #define CICR4_FR_RATE (0x7 << 8)
112 #define CICR4_DIV (0xff << 0)
114 #define CISR_FTO (1 << 15)
115 #define CISR_RDAV_2 (1 << 14)
116 #define CISR_RDAV_1 (1 << 13)
117 #define CISR_RDAV_0 (1 << 12)
118 #define CISR_FEMPTY_2 (1 << 11)
119 #define CISR_FEMPTY_1 (1 << 10)
120 #define CISR_FEMPTY_0 (1 << 9)
121 #define CISR_EOL (1 << 8)
122 #define CISR_PAR_ERR (1 << 7)
123 #define CISR_CQD (1 << 6)
124 #define CISR_CDD (1 << 5)
125 #define CISR_SOF (1 << 4)
126 #define CISR_EOF (1 << 3)
127 #define CISR_IFO_2 (1 << 2)
128 #define CISR_IFO_1 (1 << 1)
129 #define CISR_IFO_0 (1 << 0)
131 #define CIFR_FLVL2 (0x7f << 23)
132 #define CIFR_FLVL1 (0x7f << 16)
133 #define CIFR_FLVL0 (0xff << 8)
134 #define CIFR_THL_0 (0x3 << 4)
135 #define CIFR_RESET_F (1 << 3)
136 #define CIFR_FEN2 (1 << 2)
137 #define CIFR_FEN1 (1 << 1)
138 #define CIFR_FEN0 (1 << 0)
140 #define CICR0_SIM_MP (0 << 24)
141 #define CICR0_SIM_SP (1 << 24)
142 #define CICR0_SIM_MS (2 << 24)
143 #define CICR0_SIM_EP (3 << 24)
144 #define CICR0_SIM_ES (4 << 24)
146 #define CICR1_DW_VAL(x) ((x) & CICR1_DW)
147 #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL)
148 #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP)
149 #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP)
150 #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV)
152 #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW)
153 #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW)
154 #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW)
155 #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW)
156 #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW)
158 #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW)
159 #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW)
160 #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW)
161 #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF)
163 #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
164 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
165 CICR0_EOFM | CICR0_FOM)
233 static const char *pxa_cam_driver_description =
"PXA_Camera";
251 if (*size * *count > vid_limit * 1024 * 1024)
252 *count = (
vid_limit * 1024 * 1024) / *size;
267 &buf->
vb, buf->
vb.baddr, buf->
vb.bsize);
278 if (buf->
dmas[i].sg_cpu)
280 buf->
dmas[i].sg_size,
282 buf->
dmas[i].sg_dma);
290 int sg_first_ofs,
int size)
295 offset = sg_first_ofs;
300 xfer_len =
roundup(
min(dma_len - offset, size), 8);
302 size =
max(0, size - xfer_len);
338 int dma_len = 0, xfer_len = 0;
344 sglen = calculate_dma_sglen(*sg_first, dma->
sglen,
345 *sg_first_ofs, size);
353 pxa_dma->
sglen = sglen;
354 offset = *sg_first_ofs;
356 dev_dbg(dev,
"DMA: sg_first=%p, sglen=%d, ofs=%d, dma.desc=%x\n",
357 *sg_first, sglen, *sg_first_ofs, pxa_dma->
sg_dma);
364 xfer_len =
roundup(
min(dma_len - offset, size), 8);
366 size =
max(0, size - xfer_len);
368 pxa_dma->
sg_cpu[
i].dsadr = pcdev->
res->start + cibr;
379 dev_vdbg(dev,
"DMA: desc.%08x->@phys=0x%08x, len=%d\n",
398 if (xfer_len >= dma_len) {
399 *sg_first_ofs = xfer_len - dma_len;
432 int size_y, size_u = 0, size_v = 0;
434 dev_dbg(dev,
"%s (vb=0x%p) 0x%08lx %d\n", __func__,
459 vb->
field != field) {
485 size_u = size_v = size / 4;
493 ret = pxa_init_dma_channel(pcdev, buf, dma, 0,
CIBR0, size_y,
496 dev_err(dev,
"DMA initialization for Y/RGB failed\n");
502 ret = pxa_init_dma_channel(pcdev, buf, dma, 1,
CIBR1,
503 size_u, &sg, &next_ofs);
505 dev_err(dev,
"DMA initialization for U failed\n");
511 ret = pxa_init_dma_channel(pcdev, buf, dma, 2,
CIBR2,
512 size_v, &sg, &next_ofs);
514 dev_err(dev,
"DMA initialization for V failed\n");
522 pxa_videobuf_set_actdma(pcdev, buf);
528 buf->
dmas[1].sg_cpu, buf->
dmas[1].sg_dma);
531 buf->
dmas[0].sg_cpu, buf->
dmas[0].sg_dma);
553 for (i = 0; i < pcdev->
channels; i++) {
555 "%s (channel=%d) ddadr=%08x\n", __func__,
556 i, active->
dmas[i].sg_dma);
566 for (i = 0; i < pcdev->
channels; i++) {
568 "%s (channel=%d)\n", __func__, i);
579 for (i = 0; i < pcdev->
channels; i++) {
580 buf_last_desc = buf->
dmas[
i].sg_cpu + buf->
dmas[
i].sglen;
600 static void pxa_camera_start_capture(
struct pxa_camera_dev *pcdev)
615 pxa_dma_stop_channels(pcdev);
639 pxa_dma_add_tail_buf(pcdev, buf);
642 pxa_camera_start_capture(pcdev);
653 dev_dbg(dev,
"%s (vb=0x%p) 0x%08lx %d\n", __func__,
658 dev_dbg(dev,
"%s (active)\n", __func__);
661 dev_dbg(dev,
"%s (queued)\n", __func__);
664 dev_dbg(dev,
"%s (prepared)\n", __func__);
667 dev_dbg(dev,
"%s (unknown)\n", __func__);
682 list_del_init(&vb->
queue);
690 if (list_empty(&pcdev->
capture)) {
691 pxa_camera_stop_capture(pcdev);
692 for (i = 0; i < pcdev->
channels; i++)
717 static void pxa_camera_check_link_miss(
struct pxa_camera_dev *pcdev)
719 int i, is_dma_stopped = 1;
721 for (i = 0; i < pcdev->
channels; i++)
725 "%s : top queued buffer=%p, dma_stopped=%d\n",
726 __func__, pcdev->
active, is_dma_stopped);
727 if (pcdev->
active && is_dma_stopped)
728 pxa_camera_start_capture(pcdev);
731 static void pxa_camera_dma_irq(
int channel,
struct pxa_camera_dev *pcdev,
742 status =
DCSR(channel);
751 dev_err(dev,
"DMA Bus Error IRQ!\n");
756 dev_err(dev,
"Unknown DMA IRQ source, status: 0x%08x\n",
780 dev_dbg(dev,
"%s channel=%d %s%s(vb=0x%p) dma.desc=%x\n",
784 if (status & DCSR_ENDINTR) {
789 if (camera_status & overrun &&
791 dev_dbg(dev,
"FIFO overrun! CISR: %x\n",
793 pxa_camera_stop_capture(pcdev);
794 pxa_camera_start_capture(pcdev);
799 pxa_camera_wakeup(pcdev, vb, buf);
800 pxa_camera_check_link_miss(pcdev);
805 spin_unlock_irqrestore(&pcdev->
lock, flags);
808 static void pxa_camera_dma_irq_y(
int channel,
void *
data)
811 pxa_camera_dma_irq(channel, pcdev,
DMA_Y);
814 static void pxa_camera_dma_irq_u(
int channel,
void *
data)
817 pxa_camera_dma_irq(channel, pcdev,
DMA_U);
820 static void pxa_camera_dma_irq_v(
int channel,
void *
data)
823 pxa_camera_dma_irq(channel, pcdev,
DMA_V);
827 .buf_setup = pxa_videobuf_setup,
828 .buf_prepare = pxa_videobuf_prepare,
829 .buf_queue = pxa_videobuf_queue,
830 .buf_release = pxa_videobuf_release,
854 unsigned long lcdclk;
857 pcdev->
ciclk = lcdclk;
860 if (mclk > lcdclk / 4) {
862 dev_warn(dev,
"Limiting master clock to %lu\n", mclk);
866 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
870 pcdev->
mclk = lcdclk / (2 * (div + 1));
872 dev_dbg(dev,
"LCD clock %luHz, target freq %luHz, divisor %u\n",
878 static void recalculate_fifo_timeout(
struct pxa_camera_dev *pcdev,
882 u32 ciclk_per_pixel = pcdev->
ciclk / pclk + 1;
909 recalculate_fifo_timeout(pcdev, pcdev->
mclk);
912 recalculate_fifo_timeout(pcdev, 13000000);
914 clk_prepare_enable(pcdev->
clk);
919 clk_disable_unprepare(pcdev->
clk);
925 unsigned long status, cifr, cicr0;
931 "Camera interrupt status 0x%lx\n", status);
947 pxa_videobuf_set_actdma(pcdev, buf);
949 pxa_dma_start_channels(pcdev);
971 pxa_camera_activate(pcdev);
975 dev_info(icd->
parent,
"PXA Camera driver attached to camera %d\n",
989 dev_info(icd->
parent,
"PXA Camera driver detached from camera %d\n",
1000 pxa_camera_deactivate(pcdev);
1006 unsigned char buswidth,
unsigned long *flags)
1031 unsigned long flags,
__u32 pixfmt)
1036 unsigned long dw,
bpp;
1037 u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0, y_skip_top;
1047 switch (icd->
current_fmt->host_fmt->bits_per_sample) {
1128 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1137 ret = test_platform_param(pcdev, icd->
current_fmt->host_fmt->bits_per_sample,
1146 if (!common_flags) {
1148 "Flags incompatible: camera 0x%x, host 0x%lx\n",
1149 cfg.
flags, bus_flags);
1155 common_flags = bus_flags;
1162 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
1164 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
1166 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
1170 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
1172 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
1174 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
1178 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
1180 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
1182 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
1188 dev_dbg(icd->
parent,
"camera s_mbus_config(0x%lx) returned %d\n",
1195 pxa_camera_setup_cicr(icd, common_flags, pixfmt);
1201 unsigned char buswidth)
1203 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1208 int ret = test_platform_param(pcdev, buswidth, &bus_flags);
1217 if (!common_flags) {
1219 "Flags incompatible: camera 0x%x, host 0x%lx\n",
1220 cfg.
flags, bus_flags);
1233 .name =
"Planar YUV422 16 bit",
1234 .bits_per_sample = 8,
1252 struct soc_camera_format_xlate *xlate)
1254 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1268 dev_err(dev,
"Invalid format code #%u: %d\n", idx, code);
1291 xlate->host_fmt = &pxa_camera_formats[0];
1294 dev_dbg(dev,
"Providing format %s using code %d\n",
1295 pxa_camera_formats[0].
name, code);
1303 dev_dbg(dev,
"Providing format %s packed\n",
1307 if (!pxa_camera_packing_supported(fmt))
1311 "Providing format %s in pass-through mode\n",
1318 xlate->host_fmt =
fmt;
1335 return height < 32 || height > 2048 || width < 48 || width > 2048 ||
1346 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1347 struct soc_camera_sense
sense = {
1348 .master_clock = pcdev->
mclk,
1349 .pixel_clock_max = pcdev->
ciclk / 4,
1351 struct v4l2_mbus_framefmt mf;
1365 dev_warn(dev,
"Failed to crop to %ux%u@%u:%u\n",
1374 if (pxa_camera_check_frame(mf.width, mf.height)) {
1380 &mf.height, 32, 2048, 0,
1386 if (pxa_camera_check_frame(mf.width, mf.height)) {
1388 "Inconsistent state. Use S_FMT to repair\n");
1393 if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
1394 if (sense.pixel_clock > sense.pixel_clock_max) {
1396 "pixel clock %lu set by the camera too high!",
1400 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1406 pxa_camera_setup_cicr(icd, cam->
flags, fourcc);
1417 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1418 const struct soc_camera_format_xlate *xlate =
NULL;
1419 struct soc_camera_sense sense = {
1420 .master_clock = pcdev->
mclk,
1421 .pixel_clock_max = pcdev->
ciclk / 4,
1424 struct v4l2_mbus_framefmt mf;
1438 mf.width = pix->
width;
1440 mf.field = pix->
field;
1442 mf.code = xlate->code;
1446 if (mf.code != xlate->code)
1452 dev_warn(dev,
"Failed to configure for format %x\n",
1454 }
else if (pxa_camera_check_frame(mf.width, mf.height)) {
1456 "Camera driver produced an unsupported frame %dx%d\n",
1457 mf.width, mf.height);
1459 }
else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
1460 if (sense.pixel_clock > sense.pixel_clock_max) {
1462 "pixel clock %lu set by the camera too high!",
1466 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1472 pix->
width = mf.width;
1474 pix->
field = mf.field;
1484 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1485 const struct soc_camera_format_xlate *xlate;
1487 struct v4l2_mbus_framefmt mf;
1504 &pix->
height, 32, 2048, 0,
1508 mf.width = pix->
width;
1513 mf.code = xlate->code;
1519 pix->
width = mf.width;
1549 for (i = 0; i < p->
count; i++) {
1553 INIT_LIST_HEAD(&buf->
vb.queue);
1567 poll_wait(file, &buf->
vb.done, pt);
1586 static int pxa_camera_suspend(
struct device *dev)
1608 static int pxa_camera_resume(
struct device *dev)
1632 if (!ret && pcdev->
active)
1633 pxa_camera_start_capture(pcdev);
1640 .add = pxa_camera_add_device,
1641 .remove = pxa_camera_remove_device,
1642 .set_crop = pxa_camera_set_crop,
1643 .get_formats = pxa_camera_get_formats,
1644 .put_formats = pxa_camera_put_formats,
1645 .set_fmt = pxa_camera_set_fmt,
1646 .try_fmt = pxa_camera_try_fmt,
1647 .init_videobuf = pxa_camera_init_videobuf,
1648 .reqbufs = pxa_camera_reqbufs,
1649 .poll = pxa_camera_poll,
1650 .querycap = pxa_camera_querycap,
1651 .set_bus_param = pxa_camera_set_bus_param,
1664 if (!res || irq < 0) {
1671 dev_err(&pdev->
dev,
"Could not allocate pcdev\n");
1677 if (IS_ERR(pcdev->
clk)) {
1678 err = PTR_ERR(pcdev->
clk);
1684 pcdev->
pdata = pdev->
dev.platform_data;
1692 dev_warn(&pdev->
dev,
"WARNING! Platform hasn't set available "
1693 "data widths, using default 10 bit\n");
1702 pcdev->
mclk = pcdev->
pdata->mclk_10khz * 10000;
1705 "mclk == 0! Please, fix your platform data. "
1706 "Using default 20MHz\n");
1707 pcdev->
mclk = 20000000;
1712 INIT_LIST_HEAD(&pcdev->
capture);
1734 pxa_camera_dma_irq_y, pcdev);
1736 dev_err(&pdev->
dev,
"Can't request DMA for Y\n");
1743 pxa_camera_dma_irq_u, pcdev);
1745 dev_err(&pdev->
dev,
"Can't request DMA for U\n");
1746 goto exit_free_dma_y;
1752 pxa_camera_dma_irq_v, pcdev);
1754 dev_err(&pdev->
dev,
"Can't request DMA for V\n");
1755 goto exit_free_dma_u;
1768 dev_err(&pdev->
dev,
"Camera interrupt register failed \n");
1773 pcdev->
soc_host.ops = &pxa_soc_camera_host_ops;
1827 dev_info(&pdev->
dev,
"PXA Camera driver unloaded\n");
1833 .suspend = pxa_camera_suspend,
1834 .resume = pxa_camera_resume,
1840 .pm = &pxa_camera_pm,
1842 .probe = pxa_camera_probe,