13 #include <linux/module.h>
14 #include <linux/videodev2.h>
19 #include <linux/sched.h>
31 #define MX3_CAM_DRV_NAME "mx3-camera"
34 #define CSI_REG_START 0x60
36 #define CSI_SENS_CONF (0x60 - CSI_REG_START)
37 #define CSI_SENS_FRM_SIZE (0x64 - CSI_REG_START)
38 #define CSI_ACT_FRM_SIZE (0x68 - CSI_REG_START)
39 #define CSI_OUT_FRM_CTRL (0x6C - CSI_REG_START)
40 #define CSI_TST_CTRL (0x70 - CSI_REG_START)
41 #define CSI_CCIR_CODE_1 (0x74 - CSI_REG_START)
42 #define CSI_CCIR_CODE_2 (0x78 - CSI_REG_START)
43 #define CSI_CCIR_CODE_3 (0x7C - CSI_REG_START)
44 #define CSI_FLASH_STROBE_1 (0x80 - CSI_REG_START)
45 #define CSI_FLASH_STROBE_2 (0x84 - CSI_REG_START)
47 #define CSI_SENS_CONF_VSYNC_POL_SHIFT 0
48 #define CSI_SENS_CONF_HSYNC_POL_SHIFT 1
49 #define CSI_SENS_CONF_DATA_POL_SHIFT 2
50 #define CSI_SENS_CONF_PIX_CLK_POL_SHIFT 3
51 #define CSI_SENS_CONF_SENS_PRTCL_SHIFT 4
52 #define CSI_SENS_CONF_SENS_CLKSRC_SHIFT 7
53 #define CSI_SENS_CONF_DATA_FMT_SHIFT 8
54 #define CSI_SENS_CONF_DATA_WIDTH_SHIFT 10
55 #define CSI_SENS_CONF_EXT_VSYNC_SHIFT 15
56 #define CSI_SENS_CONF_DIVRATIO_SHIFT 16
58 #define CSI_SENS_CONF_DATA_FMT_RGB_YUV444 (0UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
59 #define CSI_SENS_CONF_DATA_FMT_YUV422 (2UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
60 #define CSI_SENS_CONF_DATA_FMT_BAYER (3UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
62 #define MAX_VIDEO_MEM 16
143 static void mx3_cam_dma_done(
void *
arg)
150 dev_dbg(chan->
device->dev,
"callback cookie %d, active DMA 0x%08x\n",
153 spin_lock(&mx3_cam->
lock);
158 list_del_init(&buf->
queue);
165 if (list_empty(&mx3_cam->
capture)) {
167 spin_unlock(&mx3_cam->
lock);
178 spin_unlock(&mx3_cam->
lock);
188 static int mx3_videobuf_setup(
struct vb2_queue *vq,
191 unsigned int sizes[],
void *alloc_ctxs[])
202 fmt->
fmt.
pix.pixelformat);
203 unsigned int bytes_per_line;
259 static void mx3_videobuf_queue(
struct vb2_buffer *vb)
276 if (vb2_plane_size(vb, 0) < new_size) {
278 vb->
v4l2_buf.index, vb2_plane_size(vb, 0), new_size);
286 txd = dmaengine_prep_slave_sg(
300 vb2_set_plane_payload(vb, 0, new_size);
338 spin_unlock_irq(&mx3_cam->
lock);
347 spin_lock_irq(&mx3_cam->
lock);
350 list_del_init(&buf->
queue);
352 if (mx3_cam->
active == buf)
355 spin_unlock_irqrestore(&mx3_cam->
lock, flags);
360 static void mx3_videobuf_release(
struct vb2_buffer *vb)
370 "Release%s DMA 0x%08x, queue %sempty\n",
372 list_empty(&buf->
queue) ?
"" :
"not ");
376 if (mx3_cam->
active == buf)
380 list_del_init(&buf->
queue);
388 spin_unlock_irqrestore(&mx3_cam->
lock, flags);
390 mx3_cam->
buf_total -= vb2_plane_size(vb, 0);
393 static int mx3_videobuf_init(
struct vb2_buffer *vb)
402 INIT_LIST_HEAD(&buf->
queue);
405 mx3_cam->
buf_total += vb2_plane_size(vb, 0);
411 static int mx3_stop_streaming(
struct vb2_queue *
q)
430 list_del_init(&buf->
queue);
434 spin_unlock_irqrestore(&mx3_cam->
lock, flags);
439 static struct vb2_ops mx3_videobuf_ops = {
440 .queue_setup = mx3_videobuf_setup,
441 .buf_queue = mx3_videobuf_queue,
442 .buf_cleanup = mx3_videobuf_release,
443 .buf_init = mx3_videobuf_init,
446 .stop_streaming = mx3_stop_streaming,
449 static int mx3_camera_init_videobuf(
struct vb2_queue *q,
455 q->
ops = &mx3_videobuf_ops;
506 clk_prepare_enable(mx3_cam->
clk);
508 dev_dbg(icd->
parent,
"Set SENS_CONF to %x, rate %ld\n", conf, rate);
522 mx3_camera_activate(mx3_cam, icd);
527 dev_info(icd->
parent,
"MX3 Camera driver attached to camera %d\n",
547 clk_disable_unprepare(mx3_cam->
clk);
551 dev_info(icd->
parent,
"MX3 Camera driver detached from camera %d\n",
556 unsigned char buswidth,
unsigned long *flags)
585 const unsigned int depth)
592 int ret = test_platform_param(mx3_cam, depth, &bus_flags);
594 dev_dbg(icd->
parent,
"request bus width %d bit: %d\n", depth, ret);
605 "Flags incompatible: camera 0x%x, host 0x%lx\n",
606 cfg.
flags, bus_flags);
616 static bool chan_filter(
struct dma_chan *chan,
void *arg)
621 if (!imx_dma_is_ipu(chan))
627 pdata = rq->
mx3_cam->soc_host.v4l2_dev.dev->platform_data;
636 .name =
"Bayer BGGR (sRGB) 8 bit",
637 .bits_per_sample = 8,
643 .name =
"Monochrome 8 bit",
644 .bits_per_sample = 8,
662 struct soc_camera_format_xlate *xlate)
664 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
678 "Unsupported format code #%u: %d\n", idx, code);
691 xlate->host_fmt = &mx3_camera_formats[0];
694 dev_dbg(dev,
"Providing format %s using code %d\n",
695 mx3_camera_formats[0].
name, code);
701 xlate->host_fmt = &mx3_camera_formats[1];
704 dev_dbg(dev,
"Providing format %s using code %d\n",
705 mx3_camera_formats[1].
name, code);
709 if (!mx3_camera_packing_supported(fmt))
716 xlate->host_fmt =
fmt;
718 dev_dbg(dev,
"Providing format %c%c%c%c in pass-through mode\n",
719 (fmt->
fourcc >> (0*8)) & 0xFF,
720 (fmt->
fourcc >> (1*8)) & 0xFF,
721 (fmt->
fourcc >> (2*8)) & 0xFF,
722 (fmt->
fourcc >> (3*8)) & 0xFF);
733 u32 ctrl, width_field, height_field;
744 width = width * num /
den;
748 width_field = width - 1;
749 height_field = height - 1;
755 csi_reg_write(mx3_cam, width_field | (height_field << 16),
CSI_ACT_FRM_SIZE);
789 static inline void stride_align(
__u32 *width)
791 if (
ALIGN(*width, 8) < 4096)
792 *width =
ALIGN(*width, 8);
794 *width = *width & ~7;
808 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
809 struct v4l2_mbus_framefmt mf;
812 soc_camera_limit_side(&rect->
left, &rect->
width, 0, 2, 4096);
813 soc_camera_limit_side(&rect->
top, &rect->
height, 0, 2, 4096);
829 stride_align(&mf.width);
836 configure_geometry(mx3_cam, mf.width, mf.height,
840 mf.width, mf.height);
853 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
854 const struct soc_camera_format_xlate *xlate;
856 struct v4l2_mbus_framefmt mf;
866 stride_align(&pix->
width);
875 configure_geometry(mx3_cam, pix->
width, pix->
height, xlate->host_fmt);
877 mf.width = pix->
width;
879 mf.field = pix->
field;
881 mf.code = xlate->code;
887 if (mf.code != xlate->code)
891 ret = acquire_dma_channel(mx3_cam);
896 pix->
width = mf.width;
898 pix->
field = mf.field;
899 mx3_cam->
field = mf.field;
911 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
912 const struct soc_camera_format_xlate *xlate;
914 struct v4l2_mbus_framefmt mf;
919 if (pixfmt && !xlate) {
927 if (pix->
width > 4096)
931 mf.width = pix->
width;
933 mf.field = pix->
field;
935 mf.code = xlate->code;
941 pix->
width = mf.width;
985 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
995 const struct soc_camera_format_xlate *xlate;
1004 dev_warn(dev,
"Format %x not found\n", pixfmt);
1009 ret = test_platform_param(mx3_cam, buswidth, &bus_flags);
1011 dev_dbg(dev,
"requested bus width %d bit: %d\n", buswidth, ret);
1020 if (!common_flags) {
1022 "Flags incompatible: camera 0x%x, host 0x%lx\n",
1023 cfg.
flags, bus_flags);
1029 common_flags = bus_flags;
1032 dev_dbg(dev,
"Flags cam: 0x%x host: 0x%lx common: 0x%lx\n",
1033 cfg.
flags, bus_flags, common_flags);
1039 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
1041 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
1047 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
1049 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
1055 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
1057 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
1063 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
1065 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
1071 dev_dbg(dev,
"camera s_mbus_config(0x%lx) returned %d\n",
1095 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
1097 if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
1099 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1101 if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
1105 switch (xlate->host_fmt->bits_per_sample) {
1126 dev_dbg(dev,
"Set SENS_CONF to %x\n", sens_conf | dw);
1133 .add = mx3_camera_add_device,
1134 .remove = mx3_camera_remove_device,
1135 .set_crop = mx3_camera_set_crop,
1136 .set_fmt = mx3_camera_set_fmt,
1137 .try_fmt = mx3_camera_try_fmt,
1138 .get_formats = mx3_camera_get_formats,
1139 .init_videobuf2 = mx3_camera_init_videobuf,
1140 .reqbufs = mx3_camera_reqbufs,
1141 .poll = mx3_camera_poll,
1142 .querycap = mx3_camera_querycap,
1143 .set_bus_param = mx3_camera_set_bus_param,
1160 mx3_cam =
vzalloc(
sizeof(*mx3_cam));
1162 dev_err(&pdev->
dev,
"Could not allocate mx3 camera object\n");
1168 if (IS_ERR(mx3_cam->
clk)) {
1169 err = PTR_ERR(mx3_cam->
clk);
1173 mx3_cam->
pdata = pdev->
dev.platform_data;
1180 dev_warn(&pdev->
dev,
"WARNING! Platform hasn't set available "
1181 "data widths, using default 8 bit\n");
1193 mx3_cam->
mclk = mx3_cam->
pdata->mclk_10khz * 10000;
1194 if (!mx3_cam->
mclk) {
1196 "mclk_10khz == 0! Please, fix your platform data. "
1197 "Using default 20MHz\n");
1198 mx3_cam->
mclk = 20000000;
1202 INIT_LIST_HEAD(&mx3_cam->
capture);
1207 pr_err(
"Couldn't map %x@%x\n", resource_size(res), res->
start);
1212 mx3_cam->
base = base;
1216 soc_host->
ops = &mx3_soc_camera_host_ops;
1217 soc_host->
priv = mx3_cam;
1219 soc_host->
nr = pdev->
id;
1281 .probe = mx3_camera_probe,