blob: f96f92f00f92f2e20de5a629238f55543006bf83 [file] [log] [blame]
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001/*
2 * V4L2 Driver for i.MX3x camera host
3 *
4 * Copyright (C) 2008
5 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030014#include <linux/videodev2.h>
15#include <linux/platform_device.h>
16#include <linux/clk.h>
17#include <linux/vmalloc.h>
18#include <linux/interrupt.h>
Guennadi Liakhovetskif39c1ab2009-11-09 16:11:34 -030019#include <linux/sched.h>
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030020
21#include <media/v4l2-common.h>
22#include <media/v4l2-dev.h>
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -030023#include <media/videobuf2-dma-contig.h>
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030024#include <media/soc_camera.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030025#include <media/soc_mediabus.h>
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030026
27#include <mach/ipu.h>
28#include <mach/mx3_camera.h>
Sascha Hauera8de66352010-11-05 06:26:03 -030029#include <mach/dma.h>
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030030
31#define MX3_CAM_DRV_NAME "mx3-camera"
32
33/* CMOS Sensor Interface Registers */
34#define CSI_REG_START 0x60
35
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)
46
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
57
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)
61
62#define MAX_VIDEO_MEM 16
63
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -030064enum csi_buffer_state {
65 CSI_BUF_NEEDS_INIT,
66 CSI_BUF_PREPARED,
67};
68
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030069struct mx3_camera_buffer {
70 /* common v4l buffer stuff -- must be first */
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -030071 struct vb2_buffer vb;
72 enum csi_buffer_state state;
73 struct list_head queue;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030074
75 /* One descriptot per scatterlist (per frame) */
76 struct dma_async_tx_descriptor *txd;
77
78 /* We have to "build" a scatterlist ourselves - one element per frame */
79 struct scatterlist sg;
80};
81
82/**
83 * struct mx3_camera_dev - i.MX3x camera (CSI) object
84 * @dev: camera device, to which the coherent buffer is attached
85 * @icd: currently attached camera sensor
86 * @clk: pointer to clock
87 * @base: remapped register base address
88 * @pdata: platform data
89 * @platform_flags: platform flags
90 * @mclk: master clock frequency in Hz
91 * @capture: list of capture videobuffers
92 * @lock: protects video buffer lists
93 * @active: active video buffer
94 * @idmac_channel: array of pointers to IPU DMAC DMA channels
95 * @soc_host: embedded soc_host object
96 */
97struct mx3_camera_dev {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030098 /*
99 * i.MX3x is only supposed to handle one camera on its Camera Sensor
100 * Interface. If anyone ever builds hardware to enable more than one
101 * camera _simultaneously_, they will have to modify this driver too
102 */
103 struct soc_camera_device *icd;
104 struct clk *clk;
105
106 void __iomem *base;
107
108 struct mx3_camera_pdata *pdata;
109
110 unsigned long platform_flags;
111 unsigned long mclk;
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -0300112 u16 width_flags; /* max 15 bits */
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300113
114 struct list_head capture;
115 spinlock_t lock; /* Protects video buffer lists */
116 struct mx3_camera_buffer *active;
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300117 size_t buf_total;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300118 struct vb2_alloc_ctx *alloc_ctx;
119 enum v4l2_field field;
120 int sequence;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300121
122 /* IDMAC / dmaengine interface */
123 struct idmac_channel *idmac_channel[1]; /* We need one channel */
124
125 struct soc_camera_host soc_host;
126};
127
128struct dma_chan_request {
129 struct mx3_camera_dev *mx3_cam;
130 enum ipu_channel id;
131};
132
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300133static u32 csi_reg_read(struct mx3_camera_dev *mx3, off_t reg)
134{
135 return __raw_readl(mx3->base + reg);
136}
137
138static void csi_reg_write(struct mx3_camera_dev *mx3, u32 value, off_t reg)
139{
140 __raw_writel(value, mx3->base + reg);
141}
142
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300143static struct mx3_camera_buffer *to_mx3_vb(struct vb2_buffer *vb)
144{
145 return container_of(vb, struct mx3_camera_buffer, vb);
146}
147
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300148/* Called from the IPU IDMAC ISR */
149static void mx3_cam_dma_done(void *arg)
150{
151 struct idmac_tx_desc *desc = to_tx_desc(arg);
152 struct dma_chan *chan = desc->txd.chan;
153 struct idmac_channel *ichannel = to_idmac_chan(chan);
154 struct mx3_camera_dev *mx3_cam = ichannel->client;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300155
156 dev_dbg(chan->device->dev, "callback cookie %d, active DMA 0x%08x\n",
157 desc->txd.cookie, mx3_cam->active ? sg_dma_address(&mx3_cam->active->sg) : 0);
158
159 spin_lock(&mx3_cam->lock);
160 if (mx3_cam->active) {
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300161 struct vb2_buffer *vb = &mx3_cam->active->vb;
162 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300163
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300164 list_del_init(&buf->queue);
165 do_gettimeofday(&vb->v4l2_buf.timestamp);
166 vb->v4l2_buf.field = mx3_cam->field;
167 vb->v4l2_buf.sequence = mx3_cam->sequence++;
168 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300169 }
170
171 if (list_empty(&mx3_cam->capture)) {
172 mx3_cam->active = NULL;
173 spin_unlock(&mx3_cam->lock);
174
175 /*
176 * stop capture - without further buffers IPU_CHA_BUF0_RDY will
177 * not get updated
178 */
179 return;
180 }
181
182 mx3_cam->active = list_entry(mx3_cam->capture.next,
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300183 struct mx3_camera_buffer, queue);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300184 spin_unlock(&mx3_cam->lock);
185}
186
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300187/*
188 * Videobuf operations
189 */
190
191/*
192 * Calculate the __buffer__ (not data) size and number of buffers.
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300193 */
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300194static int mx3_videobuf_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300195 const struct v4l2_format *fmt,
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300196 unsigned int *count, unsigned int *num_planes,
Marek Szyprowski035aa142011-08-24 06:43:36 -0300197 unsigned int sizes[], void *alloc_ctxs[])
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300198{
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300199 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300200 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300201 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300202 int bytes_per_line;
203 unsigned int height;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300204
205 if (!mx3_cam->idmac_channel[0])
206 return -EINVAL;
207
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300208 if (fmt) {
209 const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd,
210 fmt->fmt.pix.pixelformat);
211 if (!xlate)
212 return -EINVAL;
213 bytes_per_line = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
214 xlate->host_fmt);
215 height = fmt->fmt.pix.height;
216 } else {
217 /* Called from VIDIOC_REQBUFS or in compatibility mode */
218 bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300219 icd->current_fmt->host_fmt);
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300220 height = icd->user_height;
221 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300222 if (bytes_per_line < 0)
223 return bytes_per_line;
224
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300225 sizes[0] = bytes_per_line * height;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300226
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300227 alloc_ctxs[0] = mx3_cam->alloc_ctx;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300228
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300229 if (!vq->num_buffers)
230 mx3_cam->sequence = 0;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300231
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300232 if (!*count)
233 *count = 2;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300234
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300235 /* If *num_planes != 0, we have already verified *count. */
236 if (!*num_planes &&
237 sizes[0] * *count + mx3_cam->buf_total > MAX_VIDEO_MEM * 1024 * 1024)
238 *count = (MAX_VIDEO_MEM * 1024 * 1024 - mx3_cam->buf_total) /
239 sizes[0];
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300240
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300241 *num_planes = 1;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300242
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300243 return 0;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300244}
245
246static enum pixel_fmt fourcc_to_ipu_pix(__u32 fourcc)
247{
248 /* Add more formats as need arises and test possibilities appear... */
249 switch (fourcc) {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300250 case V4L2_PIX_FMT_RGB24:
251 return IPU_PIX_FMT_RGB24;
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300252 case V4L2_PIX_FMT_UYVY:
253 case V4L2_PIX_FMT_RGB565:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300254 default:
255 return IPU_PIX_FMT_GENERIC;
256 }
257}
258
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300259static void mx3_videobuf_queue(struct vb2_buffer *vb)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300260{
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300261 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300262 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300263 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300264 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300265 struct scatterlist *sg = &buf->sg;
266 struct dma_async_tx_descriptor *txd;
267 struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300268 struct idmac_video_param *video = &ichan->params.video;
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300269 const struct soc_mbus_pixelfmt *host_fmt = icd->current_fmt->host_fmt;
270 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, host_fmt);
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300271 unsigned long flags;
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300272 dma_cookie_t cookie;
273 size_t new_size;
274
275 BUG_ON(bytes_per_line <= 0);
276
277 new_size = bytes_per_line * icd->user_height;
278
279 if (vb2_plane_size(vb, 0) < new_size) {
280 dev_err(icd->parent, "Buffer #%d too small (%lu < %zu)\n",
281 vb->v4l2_buf.index, vb2_plane_size(vb, 0), new_size);
282 goto error;
283 }
284
285 if (buf->state == CSI_BUF_NEEDS_INIT) {
286 sg_dma_address(sg) = vb2_dma_contig_plane_dma_addr(vb, 0);
287 sg_dma_len(sg) = new_size;
288
289 txd = ichan->dma_chan.device->device_prep_slave_sg(
290 &ichan->dma_chan, sg, 1, DMA_FROM_DEVICE,
291 DMA_PREP_INTERRUPT);
292 if (!txd)
293 goto error;
294
295 txd->callback_param = txd;
296 txd->callback = mx3_cam_dma_done;
297
298 buf->state = CSI_BUF_PREPARED;
299 buf->txd = txd;
300 } else {
301 txd = buf->txd;
302 }
303
304 vb2_set_plane_payload(vb, 0, new_size);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300305
306 /* This is the configuration of one sg-element */
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300307 video->out_pixel_fmt = fourcc_to_ipu_pix(host_fmt->fourcc);
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300308
309 if (video->out_pixel_fmt == IPU_PIX_FMT_GENERIC) {
310 /*
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300311 * If the IPU DMA channel is configured to transfer generic
312 * 8-bit data, we have to set up the geometry parameters
313 * correctly, according to the current pixel format. The DMA
314 * horizontal parameters in this case are expressed in bytes,
315 * not in pixels.
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300316 */
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300317 video->out_width = bytes_per_line;
318 video->out_height = icd->user_height;
319 video->out_stride = bytes_per_line;
320 } else {
321 /*
322 * For IPU known formats the pixel unit will be managed
323 * successfully by the IPU code
324 */
325 video->out_width = icd->user_width;
326 video->out_height = icd->user_height;
327 video->out_stride = icd->user_width;
328 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300329
330#ifdef DEBUG
331 /* helps to see what DMA actually has written */
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300332 if (vb2_plane_vaddr(vb, 0))
333 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300334#endif
335
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300336 spin_lock_irqsave(&mx3_cam->lock, flags);
337 list_add_tail(&buf->queue, &mx3_cam->capture);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300338
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300339 if (!mx3_cam->active)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300340 mx3_cam->active = buf;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300341
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300342 spin_unlock_irq(&mx3_cam->lock);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300343
344 cookie = txd->tx_submit(txd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300345 dev_dbg(icd->parent, "Submitted cookie %d DMA 0x%08x\n",
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300346 cookie, sg_dma_address(&buf->sg));
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300347
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300348 if (cookie >= 0)
349 return;
350
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300351 spin_lock_irq(&mx3_cam->lock);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300352
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300353 /* Submit error */
354 list_del_init(&buf->queue);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300355
356 if (mx3_cam->active == buf)
357 mx3_cam->active = NULL;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300358
359 spin_unlock_irqrestore(&mx3_cam->lock, flags);
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300360error:
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300361 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300362}
363
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300364static void mx3_videobuf_release(struct vb2_buffer *vb)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300365{
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300366 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300367 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300368 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300369 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
370 struct dma_async_tx_descriptor *txd = buf->txd;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300371 unsigned long flags;
372
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300373 dev_dbg(icd->parent,
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300374 "Release%s DMA 0x%08x, queue %sempty\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300375 mx3_cam->active == buf ? " active" : "", sg_dma_address(&buf->sg),
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300376 list_empty(&buf->queue) ? "" : "not ");
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300377
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300378 spin_lock_irqsave(&mx3_cam->lock, flags);
379
380 if (mx3_cam->active == buf)
381 mx3_cam->active = NULL;
382
383 /* Doesn't hurt also if the list is empty */
384 list_del_init(&buf->queue);
385 buf->state = CSI_BUF_NEEDS_INIT;
386
387 if (txd) {
388 buf->txd = NULL;
389 if (mx3_cam->idmac_channel[0])
390 async_tx_ack(txd);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300391 }
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300392
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300393 spin_unlock_irqrestore(&mx3_cam->lock, flags);
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300394
395 mx3_cam->buf_total -= vb2_plane_size(vb, 0);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300396}
397
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300398static int mx3_videobuf_init(struct vb2_buffer *vb)
399{
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300400 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
401 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
402 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300403 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300404
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300405 /* This is for locking debugging only */
406 INIT_LIST_HEAD(&buf->queue);
407 sg_init_table(&buf->sg, 1);
408
409 buf->state = CSI_BUF_NEEDS_INIT;
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300410
411 mx3_cam->buf_total += vb2_plane_size(vb, 0);
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300412
413 return 0;
414}
415
Guennadi Liakhovetski08a31b92011-03-25 05:24:01 -0300416static int mx3_stop_streaming(struct vb2_queue *q)
417{
418 struct soc_camera_device *icd = soc_camera_from_vb2q(q);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300419 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski08a31b92011-03-25 05:24:01 -0300420 struct mx3_camera_dev *mx3_cam = ici->priv;
421 struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
Guennadi Liakhovetski08a31b92011-03-25 05:24:01 -0300422 struct mx3_camera_buffer *buf, *tmp;
423 unsigned long flags;
424
425 if (ichan) {
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300426 struct dma_chan *chan = &ichan->dma_chan;
427 chan->device->device_control(chan, DMA_PAUSE, 0);
Guennadi Liakhovetski08a31b92011-03-25 05:24:01 -0300428 }
429
430 spin_lock_irqsave(&mx3_cam->lock, flags);
431
432 mx3_cam->active = NULL;
433
434 list_for_each_entry_safe(buf, tmp, &mx3_cam->capture, queue) {
Guennadi Liakhovetski08a31b92011-03-25 05:24:01 -0300435 list_del_init(&buf->queue);
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300436 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
Guennadi Liakhovetski08a31b92011-03-25 05:24:01 -0300437 }
438
439 spin_unlock_irqrestore(&mx3_cam->lock, flags);
440
441 return 0;
442}
443
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300444static struct vb2_ops mx3_videobuf_ops = {
445 .queue_setup = mx3_videobuf_setup,
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300446 .buf_queue = mx3_videobuf_queue,
447 .buf_cleanup = mx3_videobuf_release,
448 .buf_init = mx3_videobuf_init,
449 .wait_prepare = soc_camera_unlock,
450 .wait_finish = soc_camera_lock,
Guennadi Liakhovetski08a31b92011-03-25 05:24:01 -0300451 .stop_streaming = mx3_stop_streaming,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300452};
453
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300454static int mx3_camera_init_videobuf(struct vb2_queue *q,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300455 struct soc_camera_device *icd)
456{
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300457 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
458 q->io_modes = VB2_MMAP | VB2_USERPTR;
459 q->drv_priv = icd;
460 q->ops = &mx3_videobuf_ops;
461 q->mem_ops = &vb2_dma_contig_memops;
462 q->buf_struct_size = sizeof(struct mx3_camera_buffer);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300463
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300464 return vb2_queue_init(q);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300465}
466
467/* First part of ipu_csi_init_interface() */
468static void mx3_camera_activate(struct mx3_camera_dev *mx3_cam,
469 struct soc_camera_device *icd)
470{
471 u32 conf;
472 long rate;
473
474 /* Set default size: ipu_csi_set_window_size() */
475 csi_reg_write(mx3_cam, (640 - 1) | ((480 - 1) << 16), CSI_ACT_FRM_SIZE);
476 /* ...and position to 0:0: ipu_csi_set_window_pos() */
477 conf = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
478 csi_reg_write(mx3_cam, conf, CSI_OUT_FRM_CTRL);
479
480 /* We use only gated clock synchronisation mode so far */
481 conf = 0 << CSI_SENS_CONF_SENS_PRTCL_SHIFT;
482
483 /* Set generic data, platform-biggest bus-width */
484 conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
485
486 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
487 conf |= 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
488 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
489 conf |= 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
490 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
491 conf |= 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
492 else/* if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)*/
493 conf |= 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
494
495 if (mx3_cam->platform_flags & MX3_CAMERA_CLK_SRC)
496 conf |= 1 << CSI_SENS_CONF_SENS_CLKSRC_SHIFT;
497 if (mx3_cam->platform_flags & MX3_CAMERA_EXT_VSYNC)
498 conf |= 1 << CSI_SENS_CONF_EXT_VSYNC_SHIFT;
499 if (mx3_cam->platform_flags & MX3_CAMERA_DP)
500 conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
501 if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
502 conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
503 if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
504 conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
505 if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
506 conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
507
508 /* ipu_csi_init_interface() */
509 csi_reg_write(mx3_cam, conf, CSI_SENS_CONF);
510
511 clk_enable(mx3_cam->clk);
512 rate = clk_round_rate(mx3_cam->clk, mx3_cam->mclk);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300513 dev_dbg(icd->parent, "Set SENS_CONF to %x, rate %ld\n", conf, rate);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300514 if (rate)
515 clk_set_rate(mx3_cam->clk, rate);
516}
517
518/* Called with .video_lock held */
519static int mx3_camera_add_device(struct soc_camera_device *icd)
520{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300521 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300522 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300523
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300524 if (mx3_cam->icd)
525 return -EBUSY;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300526
527 mx3_camera_activate(mx3_cam, icd);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300528
Guennadi Liakhovetski07f92442011-11-03 10:14:00 -0300529 mx3_cam->buf_total = 0;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300530 mx3_cam->icd = icd;
531
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300532 dev_info(icd->parent, "MX3 Camera driver attached to camera %d\n",
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300533 icd->devnum);
534
535 return 0;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300536}
537
538/* Called with .video_lock held */
539static void mx3_camera_remove_device(struct soc_camera_device *icd)
540{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300541 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300542 struct mx3_camera_dev *mx3_cam = ici->priv;
543 struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
544
545 BUG_ON(icd != mx3_cam->icd);
546
547 if (*ichan) {
548 dma_release_channel(&(*ichan)->dma_chan);
549 *ichan = NULL;
550 }
551
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300552 clk_disable(mx3_cam->clk);
553
554 mx3_cam->icd = NULL;
555
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300556 dev_info(icd->parent, "MX3 Camera driver detached from camera %d\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300557 icd->devnum);
558}
559
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300560static int test_platform_param(struct mx3_camera_dev *mx3_cam,
561 unsigned char buswidth, unsigned long *flags)
562{
563 /*
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -0300564 * If requested data width is supported by the platform, use it or any
565 * possible lower value - i.MX31 is smart enough to shift bits
566 */
567 if (buswidth > fls(mx3_cam->width_flags))
568 return -EINVAL;
569
570 /*
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300571 * Platform specified synchronization and pixel clock polarities are
572 * only a recommendation and are only used during probing. MX3x
573 * camera interface only works in master mode, i.e., uses HSYNC and
574 * VSYNC signals from the sensor
575 */
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -0300576 *flags = V4L2_MBUS_MASTER |
577 V4L2_MBUS_HSYNC_ACTIVE_HIGH |
578 V4L2_MBUS_HSYNC_ACTIVE_LOW |
579 V4L2_MBUS_VSYNC_ACTIVE_HIGH |
580 V4L2_MBUS_VSYNC_ACTIVE_LOW |
581 V4L2_MBUS_PCLK_SAMPLE_RISING |
582 V4L2_MBUS_PCLK_SAMPLE_FALLING |
583 V4L2_MBUS_DATA_ACTIVE_HIGH |
584 V4L2_MBUS_DATA_ACTIVE_LOW;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300585
586 return 0;
587}
588
589static int mx3_camera_try_bus_param(struct soc_camera_device *icd,
590 const unsigned int depth)
591{
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -0300592 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300593 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300594 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -0300595 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
596 unsigned long bus_flags, common_flags;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300597 int ret = test_platform_param(mx3_cam, depth, &bus_flags);
598
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300599 dev_dbg(icd->parent, "request bus width %d bit: %d\n", depth, ret);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300600
601 if (ret < 0)
602 return ret;
603
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -0300604 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
605 if (!ret) {
606 common_flags = soc_mbus_config_compatible(&cfg,
607 bus_flags);
608 if (!common_flags) {
609 dev_warn(icd->parent,
610 "Flags incompatible: camera 0x%x, host 0x%lx\n",
611 cfg.flags, bus_flags);
612 return -EINVAL;
613 }
614 } else if (ret != -ENOIOCTLCMD) {
615 return ret;
616 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300617
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -0300618 return 0;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300619}
620
621static bool chan_filter(struct dma_chan *chan, void *arg)
622{
623 struct dma_chan_request *rq = arg;
624 struct mx3_camera_pdata *pdata;
625
Sascha Hauera8de66352010-11-05 06:26:03 -0300626 if (!imx_dma_is_ipu(chan))
627 return false;
628
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300629 if (!rq)
630 return false;
631
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300632 pdata = rq->mx3_cam->soc_host.v4l2_dev.dev->platform_data;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300633
634 return rq->id == chan->chan_id &&
635 pdata->dma_dev == chan->device->dev;
636}
637
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300638static const struct soc_mbus_pixelfmt mx3_camera_formats[] = {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300639 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300640 .fourcc = V4L2_PIX_FMT_SBGGR8,
641 .name = "Bayer BGGR (sRGB) 8 bit",
642 .bits_per_sample = 8,
643 .packing = SOC_MBUS_PACKING_NONE,
644 .order = SOC_MBUS_ORDER_LE,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300645 }, {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300646 .fourcc = V4L2_PIX_FMT_GREY,
647 .name = "Monochrome 8 bit",
648 .bits_per_sample = 8,
649 .packing = SOC_MBUS_PACKING_NONE,
650 .order = SOC_MBUS_ORDER_LE,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300651 },
652};
653
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300654/* This will be corrected as we get more formats */
655static bool mx3_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300656{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300657 return fmt->packing == SOC_MBUS_PACKING_NONE ||
658 (fmt->bits_per_sample == 8 &&
659 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
660 (fmt->bits_per_sample > 8 &&
661 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300662}
663
Hans Verkuil3805f202010-05-08 17:55:00 -0300664static int mx3_camera_get_formats(struct soc_camera_device *icd, unsigned int idx,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300665 struct soc_camera_format_xlate *xlate)
666{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300667 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300668 struct device *dev = icd->parent;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300669 int formats = 0, ret;
670 enum v4l2_mbus_pixelcode code;
671 const struct soc_mbus_pixelfmt *fmt;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300672
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300673 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
674 if (ret < 0)
675 /* No more formats */
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300676 return 0;
677
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300678 fmt = soc_mbus_get_fmtdesc(code);
679 if (!fmt) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300680 dev_warn(icd->parent,
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300681 "Unsupported format code #%u: %d\n", idx, code);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300682 return 0;
683 }
684
685 /* This also checks support for the requested bits-per-sample */
686 ret = mx3_camera_try_bus_param(icd, fmt->bits_per_sample);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300687 if (ret < 0)
688 return 0;
689
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300690 switch (code) {
691 case V4L2_MBUS_FMT_SBGGR10_1X10:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300692 formats++;
693 if (xlate) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300694 xlate->host_fmt = &mx3_camera_formats[0];
695 xlate->code = code;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300696 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300697 dev_dbg(dev, "Providing format %s using code %d\n",
698 mx3_camera_formats[0].name, code);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300699 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300700 break;
701 case V4L2_MBUS_FMT_Y10_1X10:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300702 formats++;
703 if (xlate) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300704 xlate->host_fmt = &mx3_camera_formats[1];
705 xlate->code = code;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300706 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300707 dev_dbg(dev, "Providing format %s using code %d\n",
708 mx3_camera_formats[1].name, code);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300709 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300710 break;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300711 default:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300712 if (!mx3_camera_packing_supported(fmt))
713 return 0;
714 }
715
716 /* Generic pass-through */
717 formats++;
718 if (xlate) {
719 xlate->host_fmt = fmt;
720 xlate->code = code;
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300721 dev_dbg(dev, "Providing format %c%c%c%c in pass-through mode\n",
722 (fmt->fourcc >> (0*8)) & 0xFF,
723 (fmt->fourcc >> (1*8)) & 0xFF,
724 (fmt->fourcc >> (2*8)) & 0xFF,
725 (fmt->fourcc >> (3*8)) & 0xFF);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300726 xlate++;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300727 }
728
729 return formats;
730}
731
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300732static void configure_geometry(struct mx3_camera_dev *mx3_cam,
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300733 unsigned int width, unsigned int height,
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300734 const struct soc_mbus_pixelfmt *fmt)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300735{
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300736 u32 ctrl, width_field, height_field;
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300737
738 if (fourcc_to_ipu_pix(fmt->fourcc) == IPU_PIX_FMT_GENERIC) {
739 /*
740 * As the CSI will be configured to output BAYER, here
741 * the width parameter count the number of samples to
742 * capture to complete the whole image width.
743 */
Guennadi Liakhovetskicc552b62011-05-20 04:25:09 -0300744 unsigned int num, den;
745 int ret = soc_mbus_samples_per_pixel(fmt, &num, &den);
746 BUG_ON(ret < 0);
747 width = width * num / den;
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300748 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300749
750 /* Setup frame size - this cannot be changed on-the-fly... */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300751 width_field = width - 1;
752 height_field = height - 1;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300753 csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_SENS_FRM_SIZE);
754
755 csi_reg_write(mx3_cam, width_field << 16, CSI_FLASH_STROBE_1);
756 csi_reg_write(mx3_cam, (height_field << 16) | 0x22, CSI_FLASH_STROBE_2);
757
758 csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_ACT_FRM_SIZE);
759
760 /* ...and position */
761 ctrl = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
762 /* Sensor does the cropping */
763 csi_reg_write(mx3_cam, ctrl | 0 | (0 << 8), CSI_OUT_FRM_CTRL);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300764}
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300765
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300766static int acquire_dma_channel(struct mx3_camera_dev *mx3_cam)
767{
768 dma_cap_mask_t mask;
769 struct dma_chan *chan;
770 struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
771 /* We have to use IDMAC_IC_7 for Bayer / generic data */
772 struct dma_chan_request rq = {.mx3_cam = mx3_cam,
773 .id = IDMAC_IC_7};
774
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300775 dma_cap_zero(mask);
776 dma_cap_set(DMA_SLAVE, mask);
777 dma_cap_set(DMA_PRIVATE, mask);
778 chan = dma_request_channel(mask, chan_filter, &rq);
779 if (!chan)
780 return -EBUSY;
781
782 *ichan = to_idmac_chan(chan);
783 (*ichan)->client = mx3_cam;
784
785 return 0;
786}
787
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300788/*
789 * FIXME: learn to use stride != width, then we can keep stride properly aligned
790 * and support arbitrary (even) widths.
791 */
Márton Némethe26b3142010-02-24 17:13:29 -0300792static inline void stride_align(__u32 *width)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300793{
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300794 if (ALIGN(*width, 8) < 4096)
795 *width = ALIGN(*width, 8);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300796 else
797 *width = *width & ~7;
798}
799
800/*
801 * As long as we don't implement host-side cropping and scaling, we can use
802 * default g_crop and cropcap from soc_camera.c
803 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300804static int mx3_camera_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300805 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300806{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300807 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300808 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300809 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300810 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300811 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300812 int ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300813
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300814 soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
815 soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
816
817 ret = v4l2_subdev_call(sd, video, s_crop, a);
818 if (ret < 0)
819 return ret;
820
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300821 /* The capture device might have changed its output sizes */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300822 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300823 if (ret < 0)
824 return ret;
825
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300826 if (mf.code != icd->current_fmt->code)
827 return -EINVAL;
828
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300829 if (mf.width & 7) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300830 /* Ouch! We can only handle 8-byte aligned width... */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300831 stride_align(&mf.width);
832 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300833 if (ret < 0)
834 return ret;
835 }
836
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300837 if (mf.width != icd->user_width || mf.height != icd->user_height)
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300838 configure_geometry(mx3_cam, mf.width, mf.height,
839 icd->current_fmt->host_fmt);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300840
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300841 dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300842 mf.width, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300843
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300844 icd->user_width = mf.width;
845 icd->user_height = mf.height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300846
847 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300848}
849
850static int mx3_camera_set_fmt(struct soc_camera_device *icd,
851 struct v4l2_format *f)
852{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300853 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300854 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300855 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300856 const struct soc_camera_format_xlate *xlate;
857 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300858 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300859 int ret;
860
861 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
862 if (!xlate) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300863 dev_warn(icd->parent, "Format %x not found\n",
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300864 pix->pixelformat);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300865 return -EINVAL;
866 }
867
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300868 stride_align(&pix->width);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300869 dev_dbg(icd->parent, "Set format %dx%d\n", pix->width, pix->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300870
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300871 /*
872 * Might have to perform a complete interface initialisation like in
873 * ipu_csi_init_interface() in mxc_v4l2_s_param(). Also consider
874 * mxc_v4l2_s_fmt()
875 */
876
Guennadi Liakhovetskid2dcad42011-05-18 06:49:54 -0300877 configure_geometry(mx3_cam, pix->width, pix->height, xlate->host_fmt);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300878
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300879 mf.width = pix->width;
880 mf.height = pix->height;
881 mf.field = pix->field;
882 mf.colorspace = pix->colorspace;
883 mf.code = xlate->code;
884
885 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
886 if (ret < 0)
887 return ret;
888
889 if (mf.code != xlate->code)
890 return -EINVAL;
891
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300892 if (!mx3_cam->idmac_channel[0]) {
893 ret = acquire_dma_channel(mx3_cam);
894 if (ret < 0)
895 return ret;
896 }
897
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300898 pix->width = mf.width;
899 pix->height = mf.height;
900 pix->field = mf.field;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300901 mx3_cam->field = mf.field;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300902 pix->colorspace = mf.colorspace;
903 icd->current_fmt = xlate;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300904
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300905 dev_dbg(icd->parent, "Sensor set %dx%d\n", pix->width, pix->height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300906
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300907 return ret;
908}
909
910static int mx3_camera_try_fmt(struct soc_camera_device *icd,
911 struct v4l2_format *f)
912{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300913 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300914 const struct soc_camera_format_xlate *xlate;
915 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300916 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300917 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300918 int ret;
919
920 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
921 if (pixfmt && !xlate) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300922 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300923 return -EINVAL;
924 }
925
926 /* limit to MX3 hardware capabilities */
927 if (pix->height > 4096)
928 pix->height = 4096;
929 if (pix->width > 4096)
930 pix->width = 4096;
931
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300932 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300933 mf.width = pix->width;
934 mf.height = pix->height;
935 mf.field = pix->field;
936 mf.colorspace = pix->colorspace;
937 mf.code = xlate->code;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300938
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300939 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
940 if (ret < 0)
941 return ret;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300942
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300943 pix->width = mf.width;
944 pix->height = mf.height;
945 pix->colorspace = mf.colorspace;
946
947 switch (mf.field) {
948 case V4L2_FIELD_ANY:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300949 pix->field = V4L2_FIELD_NONE;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300950 break;
951 case V4L2_FIELD_NONE:
952 break;
953 default:
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300954 dev_err(icd->parent, "Field type %d unsupported.\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300955 mf.field);
956 ret = -EINVAL;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300957 }
958
959 return ret;
960}
961
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300962static int mx3_camera_reqbufs(struct soc_camera_device *icd,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300963 struct v4l2_requestbuffers *p)
964{
965 return 0;
966}
967
968static unsigned int mx3_camera_poll(struct file *file, poll_table *pt)
969{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300970 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300971
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300972 return vb2_poll(&icd->vb2_vidq, file, pt);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300973}
974
975static int mx3_camera_querycap(struct soc_camera_host *ici,
976 struct v4l2_capability *cap)
977{
978 /* cap->name is set by the firendly caller:-> */
979 strlcpy(cap->card, "i.MX3x Camera", sizeof(cap->card));
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300980 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
981
982 return 0;
983}
984
985static int mx3_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
986{
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -0300987 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300988 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300989 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -0300990 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
991 unsigned long bus_flags, common_flags;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300992 u32 dw, sens_conf;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300993 const struct soc_mbus_pixelfmt *fmt;
994 int buswidth;
995 int ret;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300996 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300997 struct device *dev = icd->parent;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300998
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300999 fmt = soc_mbus_get_fmtdesc(icd->current_fmt->code);
1000 if (!fmt)
1001 return -EINVAL;
1002
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001003 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1004 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001005 dev_warn(dev, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001006 return -EINVAL;
1007 }
1008
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001009 buswidth = fmt->bits_per_sample;
1010 ret = test_platform_param(mx3_cam, buswidth, &bus_flags);
1011
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001012 dev_dbg(dev, "requested bus width %d bit: %d\n", buswidth, ret);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001013
1014 if (ret < 0)
1015 return ret;
1016
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001017 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
1018 if (!ret) {
1019 common_flags = soc_mbus_config_compatible(&cfg,
1020 bus_flags);
1021 if (!common_flags) {
1022 dev_warn(icd->parent,
1023 "Flags incompatible: camera 0x%x, host 0x%lx\n",
1024 cfg.flags, bus_flags);
1025 return -EINVAL;
1026 }
1027 } else if (ret != -ENOIOCTLCMD) {
1028 return ret;
1029 } else {
1030 common_flags = bus_flags;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001031 }
1032
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001033 dev_dbg(dev, "Flags cam: 0x%x host: 0x%lx common: 0x%lx\n",
1034 cfg.flags, bus_flags, common_flags);
1035
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001036 /* Make choices, based on platform preferences */
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001037 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
1038 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001039 if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001040 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001041 else
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001042 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001043 }
1044
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001045 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
1046 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001047 if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001048 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001049 else
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001050 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001051 }
1052
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001053 if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
1054 (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001055 if (mx3_cam->platform_flags & MX3_CAMERA_DP)
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001056 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001057 else
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001058 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001059 }
1060
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001061 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
1062 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001063 if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001064 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001065 else
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001066 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001067 }
1068
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001069 cfg.flags = common_flags;
1070 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
1071 if (ret < 0 && ret != -ENOIOCTLCMD) {
1072 dev_dbg(dev, "camera s_mbus_config(0x%lx) returned %d\n",
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001073 common_flags, ret);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001074 return ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001075 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001076
1077 /*
1078 * So far only gated clock mode is supported. Add a line
1079 * (3 << CSI_SENS_CONF_SENS_PRTCL_SHIFT) |
1080 * below and select the required mode when supporting other
1081 * synchronisation protocols.
1082 */
1083 sens_conf = csi_reg_read(mx3_cam, CSI_SENS_CONF) &
1084 ~((1 << CSI_SENS_CONF_VSYNC_POL_SHIFT) |
1085 (1 << CSI_SENS_CONF_HSYNC_POL_SHIFT) |
1086 (1 << CSI_SENS_CONF_DATA_POL_SHIFT) |
1087 (1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT) |
1088 (3 << CSI_SENS_CONF_DATA_FMT_SHIFT) |
1089 (3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT));
1090
1091 /* TODO: Support RGB and YUV formats */
1092
1093 /* This has been set in mx3_camera_activate(), but we clear it above */
1094 sens_conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
1095
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001096 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001097 sens_conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001098 if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001099 sens_conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001100 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001101 sens_conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001102 if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001103 sens_conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
1104
1105 /* Just do what we're asked to do */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001106 switch (xlate->host_fmt->bits_per_sample) {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001107 case 4:
1108 dw = 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1109 break;
1110 case 8:
1111 dw = 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1112 break;
1113 case 10:
1114 dw = 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1115 break;
1116 default:
1117 /*
1118 * Actually it can only be 15 now, default is just to silence
1119 * compiler warnings
1120 */
1121 case 15:
1122 dw = 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1123 }
1124
1125 csi_reg_write(mx3_cam, sens_conf | dw, CSI_SENS_CONF);
1126
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001127 dev_dbg(dev, "Set SENS_CONF to %x\n", sens_conf | dw);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001128
1129 return 0;
1130}
1131
1132static struct soc_camera_host_ops mx3_soc_camera_host_ops = {
1133 .owner = THIS_MODULE,
1134 .add = mx3_camera_add_device,
1135 .remove = mx3_camera_remove_device,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001136 .set_crop = mx3_camera_set_crop,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001137 .set_fmt = mx3_camera_set_fmt,
1138 .try_fmt = mx3_camera_try_fmt,
1139 .get_formats = mx3_camera_get_formats,
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -03001140 .init_videobuf2 = mx3_camera_init_videobuf,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001141 .reqbufs = mx3_camera_reqbufs,
1142 .poll = mx3_camera_poll,
1143 .querycap = mx3_camera_querycap,
1144 .set_bus_param = mx3_camera_set_bus_param,
1145};
1146
Jean Delvaree36bc312009-06-04 11:07:16 -03001147static int __devinit mx3_camera_probe(struct platform_device *pdev)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001148{
1149 struct mx3_camera_dev *mx3_cam;
1150 struct resource *res;
1151 void __iomem *base;
1152 int err = 0;
1153 struct soc_camera_host *soc_host;
1154
1155 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1156 if (!res) {
1157 err = -ENODEV;
1158 goto egetres;
1159 }
1160
Joe Perches101b25b2010-11-05 00:07:39 -03001161 mx3_cam = vzalloc(sizeof(*mx3_cam));
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001162 if (!mx3_cam) {
1163 dev_err(&pdev->dev, "Could not allocate mx3 camera object\n");
1164 err = -ENOMEM;
1165 goto ealloc;
1166 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001167
Guennadi Liakhovetskib71df972009-04-03 10:34:02 -03001168 mx3_cam->clk = clk_get(&pdev->dev, NULL);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001169 if (IS_ERR(mx3_cam->clk)) {
1170 err = PTR_ERR(mx3_cam->clk);
1171 goto eclkget;
1172 }
1173
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001174 mx3_cam->pdata = pdev->dev.platform_data;
1175 mx3_cam->platform_flags = mx3_cam->pdata->flags;
1176 if (!(mx3_cam->platform_flags & (MX3_CAMERA_DATAWIDTH_4 |
1177 MX3_CAMERA_DATAWIDTH_8 | MX3_CAMERA_DATAWIDTH_10 |
1178 MX3_CAMERA_DATAWIDTH_15))) {
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001179 /*
1180 * Platform hasn't set available data widths. This is bad.
1181 * Warn and use a default.
1182 */
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001183 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1184 "data widths, using default 8 bit\n");
1185 mx3_cam->platform_flags |= MX3_CAMERA_DATAWIDTH_8;
1186 }
Guennadi Liakhovetski579cea02011-07-27 16:00:29 -03001187 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)
1188 mx3_cam->width_flags = 1 << 3;
1189 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
1190 mx3_cam->width_flags |= 1 << 7;
1191 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
1192 mx3_cam->width_flags |= 1 << 9;
1193 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
1194 mx3_cam->width_flags |= 1 << 14;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001195
1196 mx3_cam->mclk = mx3_cam->pdata->mclk_10khz * 10000;
1197 if (!mx3_cam->mclk) {
1198 dev_warn(&pdev->dev,
1199 "mclk_10khz == 0! Please, fix your platform data. "
1200 "Using default 20MHz\n");
1201 mx3_cam->mclk = 20000000;
1202 }
1203
1204 /* list of video-buffers */
1205 INIT_LIST_HEAD(&mx3_cam->capture);
1206 spin_lock_init(&mx3_cam->lock);
1207
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001208 base = ioremap(res->start, resource_size(res));
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001209 if (!base) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001210 pr_err("Couldn't map %x@%x\n", resource_size(res), res->start);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001211 err = -ENOMEM;
1212 goto eioremap;
1213 }
1214
1215 mx3_cam->base = base;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001216
1217 soc_host = &mx3_cam->soc_host;
1218 soc_host->drv_name = MX3_CAM_DRV_NAME;
1219 soc_host->ops = &mx3_soc_camera_host_ops;
1220 soc_host->priv = mx3_cam;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001221 soc_host->v4l2_dev.dev = &pdev->dev;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001222 soc_host->nr = pdev->id;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001223
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -03001224 mx3_cam->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1225 if (IS_ERR(mx3_cam->alloc_ctx)) {
1226 err = PTR_ERR(mx3_cam->alloc_ctx);
1227 goto eallocctx;
1228 }
1229
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001230 err = soc_camera_host_register(soc_host);
1231 if (err)
1232 goto ecamhostreg;
1233
1234 /* IDMAC interface */
1235 dmaengine_get();
1236
1237 return 0;
1238
1239ecamhostreg:
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -03001240 vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
1241eallocctx:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001242 iounmap(base);
1243eioremap:
1244 clk_put(mx3_cam->clk);
1245eclkget:
1246 vfree(mx3_cam);
1247ealloc:
1248egetres:
1249 return err;
1250}
1251
1252static int __devexit mx3_camera_remove(struct platform_device *pdev)
1253{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001254 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1255 struct mx3_camera_dev *mx3_cam = container_of(soc_host,
1256 struct mx3_camera_dev, soc_host);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001257
1258 clk_put(mx3_cam->clk);
1259
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001260 soc_camera_host_unregister(soc_host);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001261
1262 iounmap(mx3_cam->base);
1263
1264 /*
1265 * The channel has either not been allocated,
1266 * or should have been released
1267 */
1268 if (WARN_ON(mx3_cam->idmac_channel[0]))
1269 dma_release_channel(&mx3_cam->idmac_channel[0]->dma_chan);
1270
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -03001271 vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
1272
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001273 vfree(mx3_cam);
1274
1275 dmaengine_put();
1276
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001277 return 0;
1278}
1279
1280static struct platform_driver mx3_camera_driver = {
1281 .driver = {
1282 .name = MX3_CAM_DRV_NAME,
1283 },
1284 .probe = mx3_camera_probe,
Jean Delvaree36bc312009-06-04 11:07:16 -03001285 .remove = __devexit_p(mx3_camera_remove),
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001286};
1287
1288
Jean Delvaree36bc312009-06-04 11:07:16 -03001289static int __init mx3_camera_init(void)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001290{
1291 return platform_driver_register(&mx3_camera_driver);
1292}
1293
1294static void __exit mx3_camera_exit(void)
1295{
1296 platform_driver_unregister(&mx3_camera_driver);
1297}
1298
1299module_init(mx3_camera_init);
1300module_exit(mx3_camera_exit);
1301
1302MODULE_DESCRIPTION("i.MX3x SoC Camera Host driver");
1303MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
1304MODULE_LICENSE("GPL v2");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03001305MODULE_VERSION("0.2.3");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001306MODULE_ALIAS("platform:" MX3_CAM_DRV_NAME);