27 #include <linux/module.h>
29 #include <linux/slab.h>
40 #define DRIVER_NAME "omap1-camera"
41 #define DRIVER_VERSION "0.0.2"
50 #define REG_CTRLCLOCK 0x00
51 #define REG_IT_STATUS 0x04
53 #define REG_STATUS 0x0C
54 #define REG_CAMDATA 0x10
56 #define REG_PEAK_COUNTER 0x18
59 #define LCLK_EN BIT(7)
60 #define DPLL_EN BIT(6)
61 #define MCLK_EN BIT(5)
62 #define CAMEXCLK_EN BIT(4)
64 #define FOSCMOD_SHIFT 0
65 #define FOSCMOD_MASK (0x7 << FOSCMOD_SHIFT)
66 #define FOSCMOD_12MHz 0x0
67 #define FOSCMOD_6MHz 0x2
68 #define FOSCMOD_9_6MHz 0x4
69 #define FOSCMOD_24MHz 0x5
70 #define FOSCMOD_8MHz 0x6
73 #define DATA_TRANSFER BIT(5)
74 #define FIFO_FULL BIT(4)
81 #define RAZ_FIFO BIT(18)
82 #define EN_FIFO_FULL BIT(17)
83 #define EN_NIRQ BIT(16)
84 #define THRESHOLD_SHIFT 9
85 #define THRESHOLD_MASK (0x7f << THRESHOLD_SHIFT)
87 #define EN_H_DOWN BIT(7)
88 #define EN_H_UP BIT(6)
89 #define EN_V_DOWN BIT(5)
90 #define EN_V_UP BIT(4)
91 #define ORDERCAMD BIT(3)
93 #define IRQ_MASK (EN_V_UP | EN_V_DOWN | EN_H_UP | EN_H_DOWN | \
94 EN_NIRQ | EN_FIFO_FULL)
97 #define HSTATUS BIT(1)
98 #define VSTATUS BIT(0)
101 #define CAM_RST BIT(0)
106 #define SOCAM_BUS_FLAGS (V4L2_MBUS_MASTER | \
107 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
108 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING | \
109 V4L2_MBUS_DATA_ACTIVE_HIGH)
112 #define FIFO_SIZE ((THRESHOLD_MASK >> THRESHOLD_SHIFT) + 1)
113 #define FIFO_SHIFT __fls(FIFO_SIZE)
115 #define DMA_BURST_SHIFT (1 + OMAP_DMA_DATA_BURST_4)
116 #define DMA_BURST_SIZE (1 << DMA_BURST_SHIFT)
118 #define DMA_ELEMENT_SHIFT OMAP_DMA_DATA_TYPE_S32
119 #define DMA_ELEMENT_SIZE (1 << DMA_ELEMENT_SHIFT)
121 #define DMA_FRAME_SHIFT_CONTIG (FIFO_SHIFT - 1)
122 #define DMA_FRAME_SHIFT_SG DMA_BURST_SHIFT
124 #define DMA_FRAME_SHIFT(x) ((x) == OMAP1_CAM_DMA_CONTIG ? \
125 DMA_FRAME_SHIFT_CONTIG : \
127 #define DMA_FRAME_SIZE(x) (1 << DMA_FRAME_SHIFT(x))
128 #define DMA_SYNC OMAP_DMA_SYNC_FRAME
129 #define THRESHOLD_LEVEL DMA_FRAME_SIZE
132 #define MAX_VIDEO_MEM 4
195 #define CAM_READ(pcdev, reg) \
196 cam_read(pcdev, REG_##reg, false)
197 #define CAM_WRITE(pcdev, reg, val) \
198 cam_write(pcdev, REG_##reg, val)
199 #define CAM_READ_CACHE(pcdev, reg) \
200 cam_read(pcdev, REG_##reg, true)
221 "%s: count=%d, size=%d\n", __func__, *count, *size);
306 block_size = buf->
vb.size;
355 if (list_empty(&pcdev->
capture))
361 list_del_init(&buf->
vb.queue);
443 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
455 try_next_sgbuf(pcdev->
dma_ch, buf);
459 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock | LCLK_EN);
468 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
505 buf = prepare_next_vb(pcdev);
513 "%s: capture not active, setup FIFO, start DMA\n", __func__);
524 try_next_sgbuf(pcdev->
dma_ch, buf);
527 start_capture(pcdev);
542 dev_dbg(dev,
"%s (done)\n", __func__);
545 dev_dbg(dev,
"%s (active)\n", __func__);
548 dev_dbg(dev,
"%s (queued)\n", __func__);
551 dev_dbg(dev,
"%s (prepared)\n", __func__);
569 suspend_capture(pcdev);
570 disable_capture(pcdev);
575 suspend_capture(pcdev);
578 if (waitqueue_active(&vb->
done)) {
591 suspend_capture(pcdev);
611 prepare_next_vb(pcdev);
617 }
else if (pcdev->
ready) {
625 dev_dbg(dev,
"%s: nobody waiting on videobuf, swap with next\n",
652 "%s: nobody waiting on videobuf, reuse it\n",
663 suspend_capture(pcdev);
670 dev_dbg(dev,
"%s: no more videobufs, stop capture\n", __func__);
671 disable_capture(pcdev);
695 try_next_sgbuf(pcdev->
dma_ch, buf);
699 dev_dbg(dev,
"%s: videobuf error; reset FIFO, restart DMA\n",
701 start_capture(pcdev);
714 prepare_next_vb(pcdev);
726 suspend_capture(pcdev);
727 disable_capture(pcdev);
754 try_next_sgbuf(pcdev->
dma_ch, buf);
773 buf = prepare_next_vb(pcdev);
777 try_next_sgbuf(pcdev->
dma_ch, buf);
782 videobuf_done(pcdev, buf->
result);
786 spin_unlock_irqrestore(&pcdev->
lock, flags);
797 it_status =
CAM_READ(pcdev, IT_STATUS);
804 dev_warn(dev,
"%s: unhandled camera interrupt, status == %#x\n",
805 __func__, it_status);
806 suspend_capture(pcdev);
807 disable_capture(pcdev);
812 dev_warn(dev,
"%s: FIFO overflow\n", __func__);
814 }
else if (it_status &
V_DOWN) {
835 dev_notice(dev,
"%s: unexpected end of video frame\n",
838 }
else if (it_status &
V_UP) {
864 dev_warn(dev,
"%s: unhandled camera interrupt, status == %#x\n",
865 __func__, it_status);
871 spin_unlock_irqrestore(&pcdev->
lock, flags);
876 .buf_setup = omap1_videobuf_setup,
877 .buf_prepare = omap1_videobuf_prepare,
878 .buf_queue = omap1_videobuf_queue,
879 .buf_release = omap1_videobuf_release,
887 static void sensor_reset(
struct omap1_cam_dev *pcdev,
bool reset)
912 ctrlclock =
CAM_READ(pcdev, CTRLCLOCK);
941 sensor_reset(pcdev,
false);
945 dev_dbg(icd->
parent,
"OMAP1 Camera driver attached to camera %d\n",
958 suspend_capture(pcdev);
959 disable_capture(pcdev);
961 sensor_reset(pcdev,
true);
979 "OMAP1 Camera driver detached from camera %d\n", icd->
devnum);
983 static const struct soc_mbus_lookup omap1_cam_formats[] = {
989 .bits_per_sample = 8,
999 .bits_per_sample = 8,
1009 .bits_per_sample = 8,
1019 .bits_per_sample = 8,
1029 .bits_per_sample = 8,
1039 .bits_per_sample = 8,
1049 .bits_per_sample = 8,
1059 .bits_per_sample = 8,
1068 unsigned int idx,
struct soc_camera_format_xlate *xlate)
1083 dev_warn(dev,
"%s: unsupported format code #%d: %d\n", __func__,
1109 "%s: providing format %s as byte swapped code #%d\n",
1110 __func__, xlate->host_fmt->
name, code);
1115 "%s: providing format %s in pass-through mode\n",
1116 __func__, fmt->
name);
1120 xlate->host_fmt =
fmt;
1128 static bool is_dma_aligned(
s32 bytes_per_line,
unsigned int height,
1137 static int dma_align(
int *
width,
int *height,
1143 if (bytes_per_line < 0)
1144 return bytes_per_line;
1146 if (!is_dma_aligned(bytes_per_line, *height, vb_mode)) {
1147 unsigned int pxalign =
__fls(bytes_per_line / *width);
1150 unsigned int incr = enlarge << salign;
1153 height, 1, *height + incr, 0, salign);
1159 #define subdev_call_with_sense(pcdev, dev, icd, sd, function, args...) \
1161 struct soc_camera_sense sense = { \
1162 .master_clock = pcdev->camexclk, \
1163 .pixel_clock_max = 0, \
1168 sense.pixel_clock_max = pcdev->pdata->lclk_khz_max * 1000; \
1169 icd->sense = &sense; \
1170 __ret = v4l2_subdev_call(sd, video, function, ##args); \
1171 icd->sense = NULL; \
1173 if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) { \
1174 if (sense.pixel_clock > sense.pixel_clock_max) { \
1176 "%s: pixel clock %lu set by the camera too high!\n", \
1177 __func__, sense.pixel_clock); \
1186 struct v4l2_mbus_framefmt *mf,
1187 const struct soc_camera_format_xlate *xlate)
1193 dev_err(dev,
"%s: s_mbus_fmt failed\n", __func__);
1197 if (mf->code != xlate->code) {
1198 dev_err(dev,
"%s: unexpected pixel code change\n", __func__);
1203 if (bytes_per_line < 0) {
1204 dev_err(dev,
"%s: soc_mbus_bytes_per_line() failed\n",
1206 return bytes_per_line;
1209 if (!is_dma_aligned(bytes_per_line, mf->height, pcdev->
vb_mode)) {
1210 dev_err(dev,
"%s: resulting geometry %ux%u not DMA aligned\n",
1211 __func__, mf->width, mf->height);
1221 const struct soc_camera_format_xlate *xlate = icd->
current_fmt;
1222 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1226 struct v4l2_mbus_framefmt mf;
1231 dev_warn(dev,
"%s: failed to crop to %ux%u@%u:%u\n", __func__,
1238 dev_warn(dev,
"%s: failed to fetch current format\n", __func__);
1242 ret = dma_align(&mf.width, &mf.height, xlate->host_fmt, pcdev->
vb_mode,
1245 dev_err(dev,
"%s: failed to align %ux%u %s with DMA\n",
1246 __func__, mf.width, mf.height,
1247 xlate->host_fmt->
name);
1253 ret = set_mbus_format(pcdev, dev, icd, sd, &mf, xlate);
1255 dev_err(dev,
"%s: failed to set format\n", __func__);
1269 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1270 const struct soc_camera_format_xlate *xlate;
1275 struct v4l2_mbus_framefmt mf;
1280 dev_warn(dev,
"%s: format %#x not found\n", __func__,
1285 mf.width = pix->
width;
1287 mf.field = pix->
field;
1289 mf.code = xlate->code;
1291 ret = dma_align(&mf.width, &mf.height, xlate->host_fmt, pcdev->
vb_mode,
1294 dev_err(dev,
"%s: failed to align %ux%u %s with DMA\n",
1296 xlate->host_fmt->name);
1300 ret = set_mbus_format(pcdev, dev, icd, sd, &mf, xlate);
1302 dev_err(dev,
"%s: failed to set format\n", __func__);
1306 pix->
width = mf.width;
1308 pix->
field = mf.field;
1318 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1319 const struct soc_camera_format_xlate *xlate;
1321 struct v4l2_mbus_framefmt mf;
1332 mf.width = pix->
width;
1334 mf.field = pix->
field;
1336 mf.code = xlate->code;
1343 pix->
width = mf.width;
1345 pix->
field = mf.field;
1351 static bool sg_mode;
1400 if (!sg_mode && q->
int_ops->mmap_mapper != omap1_cam_mmap_mapper) {
1402 q->
int_ops->mmap_mapper = omap1_cam_mmap_mapper;
1417 for (i = 0; i < p->
count; i++) {
1421 INIT_LIST_HEAD(&buf->
vb.queue);
1439 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1444 const struct soc_camera_format_xlate *xlate;
1454 if (!common_flags) {
1456 "Flags incompatible: camera 0x%x, host 0x%x\n",
1469 if (!pcdev->
pdata ||
1471 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
1473 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
1479 dev_dbg(dev,
"camera s_mbus_config(0x%lx) returned %d\n",
1485 if (ctrlclock & LCLK_EN)
1486 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
1488 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) {
1489 dev_dbg(dev,
"CTRLCLOCK_REG |= POLCLK\n");
1492 dev_dbg(dev,
"CTRLCLOCK_REG &= ~POLCLK\n");
1495 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
1497 if (ctrlclock & LCLK_EN)
1502 fmt = xlate->host_fmt;
1506 dev_dbg(dev,
"MODE_REG &= ~ORDERCAMD\n");
1509 dev_dbg(dev,
"MODE_REG |= ORDERCAMD\n");
1524 poll_wait(file, &buf->
vb.done, pt);
1535 .add = omap1_cam_add_device,
1536 .remove = omap1_cam_remove_device,
1537 .get_formats = omap1_cam_get_formats,
1538 .set_crop = omap1_cam_set_crop,
1539 .set_fmt = omap1_cam_set_fmt,
1540 .try_fmt = omap1_cam_try_fmt,
1541 .init_videobuf = omap1_cam_init_videobuf,
1542 .reqbufs = omap1_cam_reqbufs,
1543 .querycap = omap1_cam_querycap,
1544 .set_bus_param = omap1_cam_set_bus_param,
1545 .poll = omap1_cam_poll,
1559 if (!res || (
int)irq <= 0) {
1570 pcdev = kzalloc(
sizeof(*pcdev) + resource_size(res),
GFP_KERNEL);
1572 dev_err(&pdev->
dev,
"Could not allocate pcdev\n");
1580 pcdev->
pdata = pdev->
dev.platform_data;
1596 "Incorrect sensor clock frequency %ld kHz, "
1597 "should be one of 0, 6, 8, 9.6, 12 or 24 MHz, "
1598 "please correct your platform data\n",
1599 pcdev->
pdata->camexclk_khz);
1602 dev_info(&pdev->
dev,
"Not providing sensor clock\n");
1605 INIT_LIST_HEAD(&pcdev->
capture);
1624 sensor_reset(pcdev,
true);
1627 dma_isr, (
void *)pcdev, &pcdev->
dma_ch);
1629 dev_err(&pdev->
dev,
"Can't request DMA for OMAP1 Camera\n");
1645 dev_err(&pdev->
dev,
"Camera interrupt register failed\n");
1650 pcdev->
soc_host.ops = &omap1_host_ops;
1659 dev_info(&pdev->
dev,
"OMAP1 Camera Interface driver loaded\n");
1701 dev_info(&pdev->
dev,
"OMAP1 Camera Interface driver unloaded\n");
1710 .probe = omap1_cam_probe,
1711 .remove =
__exit_p(omap1_cam_remove),
1717 MODULE_PARM_DESC(sg_mode,
"videobuf mode, 0: dma-contig (default), 1: dma-sg");