blob: 8630c0c9e60aa79857b0c70034b360240fe0e8f7 [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>
14#include <linux/version.h>
15#include <linux/videodev2.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/vmalloc.h>
19#include <linux/interrupt.h>
Guennadi Liakhovetskif39c1ab2009-11-09 16:11:34 -030020#include <linux/sched.h>
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030021
22#include <media/v4l2-common.h>
23#include <media/v4l2-dev.h>
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -030024#include <media/videobuf2-dma-contig.h>
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030025#include <media/soc_camera.h>
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -030026#include <media/soc_mediabus.h>
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030027
28#include <mach/ipu.h>
29#include <mach/mx3_camera.h>
Sascha Hauera8de66352010-11-05 06:26:03 -030030#include <mach/dma.h>
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030031
32#define MX3_CAM_DRV_NAME "mx3-camera"
33
34/* CMOS Sensor Interface Registers */
35#define CSI_REG_START 0x60
36
37#define CSI_SENS_CONF (0x60 - CSI_REG_START)
38#define CSI_SENS_FRM_SIZE (0x64 - CSI_REG_START)
39#define CSI_ACT_FRM_SIZE (0x68 - CSI_REG_START)
40#define CSI_OUT_FRM_CTRL (0x6C - CSI_REG_START)
41#define CSI_TST_CTRL (0x70 - CSI_REG_START)
42#define CSI_CCIR_CODE_1 (0x74 - CSI_REG_START)
43#define CSI_CCIR_CODE_2 (0x78 - CSI_REG_START)
44#define CSI_CCIR_CODE_3 (0x7C - CSI_REG_START)
45#define CSI_FLASH_STROBE_1 (0x80 - CSI_REG_START)
46#define CSI_FLASH_STROBE_2 (0x84 - CSI_REG_START)
47
48#define CSI_SENS_CONF_VSYNC_POL_SHIFT 0
49#define CSI_SENS_CONF_HSYNC_POL_SHIFT 1
50#define CSI_SENS_CONF_DATA_POL_SHIFT 2
51#define CSI_SENS_CONF_PIX_CLK_POL_SHIFT 3
52#define CSI_SENS_CONF_SENS_PRTCL_SHIFT 4
53#define CSI_SENS_CONF_SENS_CLKSRC_SHIFT 7
54#define CSI_SENS_CONF_DATA_FMT_SHIFT 8
55#define CSI_SENS_CONF_DATA_WIDTH_SHIFT 10
56#define CSI_SENS_CONF_EXT_VSYNC_SHIFT 15
57#define CSI_SENS_CONF_DIVRATIO_SHIFT 16
58
59#define CSI_SENS_CONF_DATA_FMT_RGB_YUV444 (0UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
60#define CSI_SENS_CONF_DATA_FMT_YUV422 (2UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
61#define CSI_SENS_CONF_DATA_FMT_BAYER (3UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
62
63#define MAX_VIDEO_MEM 16
64
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -030065enum csi_buffer_state {
66 CSI_BUF_NEEDS_INIT,
67 CSI_BUF_PREPARED,
68};
69
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030070struct mx3_camera_buffer {
71 /* common v4l buffer stuff -- must be first */
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -030072 struct vb2_buffer vb;
73 enum csi_buffer_state state;
74 struct list_head queue;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030075
76 /* One descriptot per scatterlist (per frame) */
77 struct dma_async_tx_descriptor *txd;
78
79 /* We have to "build" a scatterlist ourselves - one element per frame */
80 struct scatterlist sg;
81};
82
83/**
84 * struct mx3_camera_dev - i.MX3x camera (CSI) object
85 * @dev: camera device, to which the coherent buffer is attached
86 * @icd: currently attached camera sensor
87 * @clk: pointer to clock
88 * @base: remapped register base address
89 * @pdata: platform data
90 * @platform_flags: platform flags
91 * @mclk: master clock frequency in Hz
92 * @capture: list of capture videobuffers
93 * @lock: protects video buffer lists
94 * @active: active video buffer
95 * @idmac_channel: array of pointers to IPU DMAC DMA channels
96 * @soc_host: embedded soc_host object
97 */
98struct mx3_camera_dev {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030099 /*
100 * i.MX3x is only supposed to handle one camera on its Camera Sensor
101 * Interface. If anyone ever builds hardware to enable more than one
102 * camera _simultaneously_, they will have to modify this driver too
103 */
104 struct soc_camera_device *icd;
105 struct clk *clk;
106
107 void __iomem *base;
108
109 struct mx3_camera_pdata *pdata;
110
111 unsigned long platform_flags;
112 unsigned long mclk;
113
114 struct list_head capture;
115 spinlock_t lock; /* Protects video buffer lists */
116 struct mx3_camera_buffer *active;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300117 struct vb2_alloc_ctx *alloc_ctx;
118 enum v4l2_field field;
119 int sequence;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300120
121 /* IDMAC / dmaengine interface */
122 struct idmac_channel *idmac_channel[1]; /* We need one channel */
123
124 struct soc_camera_host soc_host;
125};
126
127struct dma_chan_request {
128 struct mx3_camera_dev *mx3_cam;
129 enum ipu_channel id;
130};
131
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300132static u32 csi_reg_read(struct mx3_camera_dev *mx3, off_t reg)
133{
134 return __raw_readl(mx3->base + reg);
135}
136
137static void csi_reg_write(struct mx3_camera_dev *mx3, u32 value, off_t reg)
138{
139 __raw_writel(value, mx3->base + reg);
140}
141
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300142static struct mx3_camera_buffer *to_mx3_vb(struct vb2_buffer *vb)
143{
144 return container_of(vb, struct mx3_camera_buffer, vb);
145}
146
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300147/* Called from the IPU IDMAC ISR */
148static void mx3_cam_dma_done(void *arg)
149{
150 struct idmac_tx_desc *desc = to_tx_desc(arg);
151 struct dma_chan *chan = desc->txd.chan;
152 struct idmac_channel *ichannel = to_idmac_chan(chan);
153 struct mx3_camera_dev *mx3_cam = ichannel->client;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300154
155 dev_dbg(chan->device->dev, "callback cookie %d, active DMA 0x%08x\n",
156 desc->txd.cookie, mx3_cam->active ? sg_dma_address(&mx3_cam->active->sg) : 0);
157
158 spin_lock(&mx3_cam->lock);
159 if (mx3_cam->active) {
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300160 struct vb2_buffer *vb = &mx3_cam->active->vb;
161 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300162
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300163 list_del_init(&buf->queue);
164 do_gettimeofday(&vb->v4l2_buf.timestamp);
165 vb->v4l2_buf.field = mx3_cam->field;
166 vb->v4l2_buf.sequence = mx3_cam->sequence++;
167 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300168 }
169
170 if (list_empty(&mx3_cam->capture)) {
171 mx3_cam->active = NULL;
172 spin_unlock(&mx3_cam->lock);
173
174 /*
175 * stop capture - without further buffers IPU_CHA_BUF0_RDY will
176 * not get updated
177 */
178 return;
179 }
180
181 mx3_cam->active = list_entry(mx3_cam->capture.next,
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300182 struct mx3_camera_buffer, queue);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300183 spin_unlock(&mx3_cam->lock);
184}
185
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300186/*
187 * Videobuf operations
188 */
189
190/*
191 * Calculate the __buffer__ (not data) size and number of buffers.
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300192 */
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300193static int mx3_videobuf_setup(struct vb2_queue *vq,
194 unsigned int *count, unsigned int *num_planes,
195 unsigned long sizes[], void *alloc_ctxs[])
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300196{
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300197 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300198 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
199 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300200 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
201 icd->current_fmt->host_fmt);
202
203 if (bytes_per_line < 0)
204 return bytes_per_line;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300205
206 if (!mx3_cam->idmac_channel[0])
207 return -EINVAL;
208
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300209 *num_planes = 1;
210
211 mx3_cam->sequence = 0;
212 sizes[0] = bytes_per_line * icd->user_height;
213 alloc_ctxs[0] = mx3_cam->alloc_ctx;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300214
215 if (!*count)
216 *count = 32;
217
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300218 if (sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024)
219 *count = MAX_VIDEO_MEM * 1024 * 1024 / sizes[0];
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300220
221 return 0;
222}
223
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300224static int mx3_videobuf_prepare(struct vb2_buffer *vb)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300225{
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300226 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300227 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
228 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300229 struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
230 struct scatterlist *sg;
231 struct mx3_camera_buffer *buf;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300232 size_t new_size;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300233 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
234 icd->current_fmt->host_fmt);
235
236 if (bytes_per_line < 0)
237 return bytes_per_line;
238
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300239 buf = to_mx3_vb(vb);
240 sg = &buf->sg;
241
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300242 new_size = bytes_per_line * icd->user_height;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300243
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300244 if (vb2_plane_size(vb, 0) < new_size) {
245 dev_err(icd->dev.parent, "Buffer too small (%lu < %zu)\n",
246 vb2_plane_size(vb, 0), new_size);
247 return -ENOBUFS;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300248 }
249
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300250 if (buf->state == CSI_BUF_NEEDS_INIT) {
251 sg_dma_address(sg) = vb2_dma_contig_plane_paddr(vb, 0);
252 sg_dma_len(sg) = new_size;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300253
254 buf->txd = ichan->dma_chan.device->device_prep_slave_sg(
255 &ichan->dma_chan, sg, 1, DMA_FROM_DEVICE,
256 DMA_PREP_INTERRUPT);
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300257 if (!buf->txd)
258 return -EIO;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300259
260 buf->txd->callback_param = buf->txd;
261 buf->txd->callback = mx3_cam_dma_done;
262
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300263 buf->state = CSI_BUF_PREPARED;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300264 }
265
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300266 vb2_set_plane_payload(vb, 0, new_size);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300267
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300268 return 0;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300269}
270
271static enum pixel_fmt fourcc_to_ipu_pix(__u32 fourcc)
272{
273 /* Add more formats as need arises and test possibilities appear... */
274 switch (fourcc) {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300275 case V4L2_PIX_FMT_RGB24:
276 return IPU_PIX_FMT_RGB24;
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300277 case V4L2_PIX_FMT_UYVY:
278 case V4L2_PIX_FMT_RGB565:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300279 default:
280 return IPU_PIX_FMT_GENERIC;
281 }
282}
283
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300284static void mx3_videobuf_queue(struct vb2_buffer *vb)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300285{
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300286 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300287 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
288 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300289 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300290 struct dma_async_tx_descriptor *txd = buf->txd;
291 struct idmac_channel *ichan = to_idmac_chan(txd->chan);
292 struct idmac_video_param *video = &ichan->params.video;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300293 dma_cookie_t cookie;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300294 u32 fourcc = icd->current_fmt->host_fmt->fourcc;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300295 unsigned long flags;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300296
297 /* This is the configuration of one sg-element */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300298 video->out_pixel_fmt = fourcc_to_ipu_pix(fourcc);
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300299
300 if (video->out_pixel_fmt == IPU_PIX_FMT_GENERIC) {
301 /*
302 * If the IPU DMA channel is configured to transport
303 * generic 8-bit data, we have to set up correctly the
304 * geometry parameters upon the current pixel format.
305 * So, since the DMA horizontal parameters are expressed
306 * in bytes not pixels, convert these in the right unit.
307 */
308 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
309 icd->current_fmt->host_fmt);
310 BUG_ON(bytes_per_line <= 0);
311
312 video->out_width = bytes_per_line;
313 video->out_height = icd->user_height;
314 video->out_stride = bytes_per_line;
315 } else {
316 /*
317 * For IPU known formats the pixel unit will be managed
318 * successfully by the IPU code
319 */
320 video->out_width = icd->user_width;
321 video->out_height = icd->user_height;
322 video->out_stride = icd->user_width;
323 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300324
325#ifdef DEBUG
326 /* helps to see what DMA actually has written */
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300327 if (vb2_plane_vaddr(vb, 0))
328 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300329#endif
330
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300331 spin_lock_irqsave(&mx3_cam->lock, flags);
332 list_add_tail(&buf->queue, &mx3_cam->capture);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300333
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300334 if (!mx3_cam->active)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300335 mx3_cam->active = buf;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300336
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300337 spin_unlock_irq(&mx3_cam->lock);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300338
339 cookie = txd->tx_submit(txd);
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300340 dev_dbg(icd->dev.parent, "Submitted cookie %d DMA 0x%08x\n",
341 cookie, sg_dma_address(&buf->sg));
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300342
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300343 if (cookie >= 0)
344 return;
345
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300346 spin_lock_irq(&mx3_cam->lock);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300347
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300348 /* Submit error */
349 list_del_init(&buf->queue);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300350
351 if (mx3_cam->active == buf)
352 mx3_cam->active = NULL;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300353
354 spin_unlock_irqrestore(&mx3_cam->lock, flags);
355 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300356}
357
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300358static void mx3_videobuf_release(struct vb2_buffer *vb)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300359{
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300360 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300361 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
362 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300363 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
364 struct dma_async_tx_descriptor *txd = buf->txd;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300365 unsigned long flags;
366
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300367 dev_dbg(icd->dev.parent,
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300368 "Release%s DMA 0x%08x, queue %sempty\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300369 mx3_cam->active == buf ? " active" : "", sg_dma_address(&buf->sg),
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300370 list_empty(&buf->queue) ? "" : "not ");
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300371
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300372 spin_lock_irqsave(&mx3_cam->lock, flags);
373
374 if (mx3_cam->active == buf)
375 mx3_cam->active = NULL;
376
377 /* Doesn't hurt also if the list is empty */
378 list_del_init(&buf->queue);
379 buf->state = CSI_BUF_NEEDS_INIT;
380
381 if (txd) {
382 buf->txd = NULL;
383 if (mx3_cam->idmac_channel[0])
384 async_tx_ack(txd);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300385 }
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300386
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300387 spin_unlock_irqrestore(&mx3_cam->lock, flags);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300388}
389
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300390static int mx3_videobuf_init(struct vb2_buffer *vb)
391{
392 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
393 /* This is for locking debugging only */
394 INIT_LIST_HEAD(&buf->queue);
395 sg_init_table(&buf->sg, 1);
396
397 buf->state = CSI_BUF_NEEDS_INIT;
398 buf->txd = NULL;
399
400 return 0;
401}
402
Guennadi Liakhovetski08a31b92011-03-25 05:24:01 -0300403static int mx3_stop_streaming(struct vb2_queue *q)
404{
405 struct soc_camera_device *icd = soc_camera_from_vb2q(q);
406 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
407 struct mx3_camera_dev *mx3_cam = ici->priv;
408 struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
409 struct dma_chan *chan;
410 struct mx3_camera_buffer *buf, *tmp;
411 unsigned long flags;
412
413 if (ichan) {
414 chan = &ichan->dma_chan;
415 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
416 }
417
418 spin_lock_irqsave(&mx3_cam->lock, flags);
419
420 mx3_cam->active = NULL;
421
422 list_for_each_entry_safe(buf, tmp, &mx3_cam->capture, queue) {
423 buf->state = CSI_BUF_NEEDS_INIT;
424 list_del_init(&buf->queue);
425 }
426
427 spin_unlock_irqrestore(&mx3_cam->lock, flags);
428
429 return 0;
430}
431
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300432static struct vb2_ops mx3_videobuf_ops = {
433 .queue_setup = mx3_videobuf_setup,
434 .buf_prepare = mx3_videobuf_prepare,
435 .buf_queue = mx3_videobuf_queue,
436 .buf_cleanup = mx3_videobuf_release,
437 .buf_init = mx3_videobuf_init,
438 .wait_prepare = soc_camera_unlock,
439 .wait_finish = soc_camera_lock,
Guennadi Liakhovetski08a31b92011-03-25 05:24:01 -0300440 .stop_streaming = mx3_stop_streaming,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300441};
442
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300443static int mx3_camera_init_videobuf(struct vb2_queue *q,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300444 struct soc_camera_device *icd)
445{
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300446 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
447 q->io_modes = VB2_MMAP | VB2_USERPTR;
448 q->drv_priv = icd;
449 q->ops = &mx3_videobuf_ops;
450 q->mem_ops = &vb2_dma_contig_memops;
451 q->buf_struct_size = sizeof(struct mx3_camera_buffer);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300452
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300453 return vb2_queue_init(q);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300454}
455
456/* First part of ipu_csi_init_interface() */
457static void mx3_camera_activate(struct mx3_camera_dev *mx3_cam,
458 struct soc_camera_device *icd)
459{
460 u32 conf;
461 long rate;
462
463 /* Set default size: ipu_csi_set_window_size() */
464 csi_reg_write(mx3_cam, (640 - 1) | ((480 - 1) << 16), CSI_ACT_FRM_SIZE);
465 /* ...and position to 0:0: ipu_csi_set_window_pos() */
466 conf = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
467 csi_reg_write(mx3_cam, conf, CSI_OUT_FRM_CTRL);
468
469 /* We use only gated clock synchronisation mode so far */
470 conf = 0 << CSI_SENS_CONF_SENS_PRTCL_SHIFT;
471
472 /* Set generic data, platform-biggest bus-width */
473 conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
474
475 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
476 conf |= 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
477 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
478 conf |= 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
479 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
480 conf |= 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
481 else/* if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)*/
482 conf |= 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
483
484 if (mx3_cam->platform_flags & MX3_CAMERA_CLK_SRC)
485 conf |= 1 << CSI_SENS_CONF_SENS_CLKSRC_SHIFT;
486 if (mx3_cam->platform_flags & MX3_CAMERA_EXT_VSYNC)
487 conf |= 1 << CSI_SENS_CONF_EXT_VSYNC_SHIFT;
488 if (mx3_cam->platform_flags & MX3_CAMERA_DP)
489 conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
490 if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
491 conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
492 if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
493 conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
494 if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
495 conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
496
497 /* ipu_csi_init_interface() */
498 csi_reg_write(mx3_cam, conf, CSI_SENS_CONF);
499
500 clk_enable(mx3_cam->clk);
501 rate = clk_round_rate(mx3_cam->clk, mx3_cam->mclk);
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300502 dev_dbg(icd->dev.parent, "Set SENS_CONF to %x, rate %ld\n", conf, rate);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300503 if (rate)
504 clk_set_rate(mx3_cam->clk, rate);
505}
506
507/* Called with .video_lock held */
508static int mx3_camera_add_device(struct soc_camera_device *icd)
509{
510 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
511 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300512
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300513 if (mx3_cam->icd)
514 return -EBUSY;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300515
516 mx3_camera_activate(mx3_cam, icd);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300517
518 mx3_cam->icd = icd;
519
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300520 dev_info(icd->dev.parent, "MX3 Camera driver attached to camera %d\n",
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300521 icd->devnum);
522
523 return 0;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300524}
525
526/* Called with .video_lock held */
527static void mx3_camera_remove_device(struct soc_camera_device *icd)
528{
529 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
530 struct mx3_camera_dev *mx3_cam = ici->priv;
531 struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
532
533 BUG_ON(icd != mx3_cam->icd);
534
535 if (*ichan) {
536 dma_release_channel(&(*ichan)->dma_chan);
537 *ichan = NULL;
538 }
539
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300540 clk_disable(mx3_cam->clk);
541
542 mx3_cam->icd = NULL;
543
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300544 dev_info(icd->dev.parent, "MX3 Camera driver detached from camera %d\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300545 icd->devnum);
546}
547
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300548static int test_platform_param(struct mx3_camera_dev *mx3_cam,
549 unsigned char buswidth, unsigned long *flags)
550{
551 /*
552 * Platform specified synchronization and pixel clock polarities are
553 * only a recommendation and are only used during probing. MX3x
554 * camera interface only works in master mode, i.e., uses HSYNC and
555 * VSYNC signals from the sensor
556 */
557 *flags = SOCAM_MASTER |
558 SOCAM_HSYNC_ACTIVE_HIGH |
559 SOCAM_HSYNC_ACTIVE_LOW |
560 SOCAM_VSYNC_ACTIVE_HIGH |
561 SOCAM_VSYNC_ACTIVE_LOW |
562 SOCAM_PCLK_SAMPLE_RISING |
563 SOCAM_PCLK_SAMPLE_FALLING |
564 SOCAM_DATA_ACTIVE_HIGH |
565 SOCAM_DATA_ACTIVE_LOW;
566
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300567 /*
568 * If requested data width is supported by the platform, use it or any
569 * possible lower value - i.MX31 is smart enough to schift bits
570 */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300571 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300572 *flags |= SOCAM_DATAWIDTH_15 | SOCAM_DATAWIDTH_10 |
573 SOCAM_DATAWIDTH_8 | SOCAM_DATAWIDTH_4;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300574 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300575 *flags |= SOCAM_DATAWIDTH_10 | SOCAM_DATAWIDTH_8 |
576 SOCAM_DATAWIDTH_4;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300577 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
578 *flags |= SOCAM_DATAWIDTH_8 | SOCAM_DATAWIDTH_4;
579 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)
580 *flags |= SOCAM_DATAWIDTH_4;
581
582 switch (buswidth) {
583 case 15:
584 if (!(*flags & SOCAM_DATAWIDTH_15))
585 return -EINVAL;
586 break;
587 case 10:
588 if (!(*flags & SOCAM_DATAWIDTH_10))
589 return -EINVAL;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300590 break;
591 case 8:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300592 if (!(*flags & SOCAM_DATAWIDTH_8))
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300593 return -EINVAL;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300594 break;
595 case 4:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300596 if (!(*flags & SOCAM_DATAWIDTH_4))
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300597 return -EINVAL;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300598 break;
599 default:
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300600 dev_warn(mx3_cam->soc_host.v4l2_dev.dev,
601 "Unsupported bus width %d\n", buswidth);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300602 return -EINVAL;
603 }
604
605 return 0;
606}
607
608static int mx3_camera_try_bus_param(struct soc_camera_device *icd,
609 const unsigned int depth)
610{
611 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
612 struct mx3_camera_dev *mx3_cam = ici->priv;
613 unsigned long bus_flags, camera_flags;
614 int ret = test_platform_param(mx3_cam, depth, &bus_flags);
615
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300616 dev_dbg(icd->dev.parent, "request bus width %d bit: %d\n", depth, ret);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300617
618 if (ret < 0)
619 return ret;
620
621 camera_flags = icd->ops->query_bus_param(icd);
622
623 ret = soc_camera_bus_param_compatible(camera_flags, bus_flags);
624 if (ret < 0)
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300625 dev_warn(icd->dev.parent,
626 "Flags incompatible: camera %lx, host %lx\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300627 camera_flags, bus_flags);
628
629 return ret;
630}
631
632static bool chan_filter(struct dma_chan *chan, void *arg)
633{
634 struct dma_chan_request *rq = arg;
635 struct mx3_camera_pdata *pdata;
636
Sascha Hauera8de66352010-11-05 06:26:03 -0300637 if (!imx_dma_is_ipu(chan))
638 return false;
639
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300640 if (!rq)
641 return false;
642
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300643 pdata = rq->mx3_cam->soc_host.v4l2_dev.dev->platform_data;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300644
645 return rq->id == chan->chan_id &&
646 pdata->dma_dev == chan->device->dev;
647}
648
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300649static const struct soc_mbus_pixelfmt mx3_camera_formats[] = {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300650 {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300651 .fourcc = V4L2_PIX_FMT_SBGGR8,
652 .name = "Bayer BGGR (sRGB) 8 bit",
653 .bits_per_sample = 8,
654 .packing = SOC_MBUS_PACKING_NONE,
655 .order = SOC_MBUS_ORDER_LE,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300656 }, {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300657 .fourcc = V4L2_PIX_FMT_GREY,
658 .name = "Monochrome 8 bit",
659 .bits_per_sample = 8,
660 .packing = SOC_MBUS_PACKING_NONE,
661 .order = SOC_MBUS_ORDER_LE,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300662 },
663};
664
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300665/* This will be corrected as we get more formats */
666static bool mx3_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300667{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300668 return fmt->packing == SOC_MBUS_PACKING_NONE ||
669 (fmt->bits_per_sample == 8 &&
670 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
671 (fmt->bits_per_sample > 8 &&
672 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300673}
674
Hans Verkuil3805f202010-05-08 17:55:00 -0300675static int mx3_camera_get_formats(struct soc_camera_device *icd, unsigned int idx,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300676 struct soc_camera_format_xlate *xlate)
677{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300678 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
679 struct device *dev = icd->dev.parent;
680 int formats = 0, ret;
681 enum v4l2_mbus_pixelcode code;
682 const struct soc_mbus_pixelfmt *fmt;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300683
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300684 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
685 if (ret < 0)
686 /* No more formats */
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300687 return 0;
688
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300689 fmt = soc_mbus_get_fmtdesc(code);
690 if (!fmt) {
691 dev_err(icd->dev.parent,
Hans Verkuil3805f202010-05-08 17:55:00 -0300692 "Invalid format code #%u: %d\n", idx, code);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300693 return 0;
694 }
695
696 /* This also checks support for the requested bits-per-sample */
697 ret = mx3_camera_try_bus_param(icd, fmt->bits_per_sample);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300698 if (ret < 0)
699 return 0;
700
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300701 switch (code) {
702 case V4L2_MBUS_FMT_SBGGR10_1X10:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300703 formats++;
704 if (xlate) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300705 xlate->host_fmt = &mx3_camera_formats[0];
706 xlate->code = code;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300707 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300708 dev_dbg(dev, "Providing format %s using code %d\n",
709 mx3_camera_formats[0].name, code);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300710 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300711 break;
712 case V4L2_MBUS_FMT_Y10_1X10:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300713 formats++;
714 if (xlate) {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300715 xlate->host_fmt = &mx3_camera_formats[1];
716 xlate->code = code;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300717 xlate++;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300718 dev_dbg(dev, "Providing format %s using code %d\n",
719 mx3_camera_formats[1].name, code);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300720 }
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300721 break;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300722 default:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300723 if (!mx3_camera_packing_supported(fmt))
724 return 0;
725 }
726
727 /* Generic pass-through */
728 formats++;
729 if (xlate) {
730 xlate->host_fmt = fmt;
731 xlate->code = code;
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300732 dev_dbg(dev, "Providing format %c%c%c%c in pass-through mode\n",
733 (fmt->fourcc >> (0*8)) & 0xFF,
734 (fmt->fourcc >> (1*8)) & 0xFF,
735 (fmt->fourcc >> (2*8)) & 0xFF,
736 (fmt->fourcc >> (3*8)) & 0xFF);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300737 xlate++;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300738 }
739
740 return formats;
741}
742
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300743static void configure_geometry(struct mx3_camera_dev *mx3_cam,
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300744 unsigned int width, unsigned int height,
745 enum v4l2_mbus_pixelcode code)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300746{
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300747 u32 ctrl, width_field, height_field;
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300748 const struct soc_mbus_pixelfmt *fmt;
749
750 fmt = soc_mbus_get_fmtdesc(code);
751 BUG_ON(!fmt);
752
753 if (fourcc_to_ipu_pix(fmt->fourcc) == IPU_PIX_FMT_GENERIC) {
754 /*
755 * As the CSI will be configured to output BAYER, here
756 * the width parameter count the number of samples to
757 * capture to complete the whole image width.
758 */
759 width *= soc_mbus_samples_per_pixel(fmt);
760 BUG_ON(width < 0);
761 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300762
763 /* Setup frame size - this cannot be changed on-the-fly... */
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300764 width_field = width - 1;
765 height_field = height - 1;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300766 csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_SENS_FRM_SIZE);
767
768 csi_reg_write(mx3_cam, width_field << 16, CSI_FLASH_STROBE_1);
769 csi_reg_write(mx3_cam, (height_field << 16) | 0x22, CSI_FLASH_STROBE_2);
770
771 csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_ACT_FRM_SIZE);
772
773 /* ...and position */
774 ctrl = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
775 /* Sensor does the cropping */
776 csi_reg_write(mx3_cam, ctrl | 0 | (0 << 8), CSI_OUT_FRM_CTRL);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300777}
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300778
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300779static int acquire_dma_channel(struct mx3_camera_dev *mx3_cam)
780{
781 dma_cap_mask_t mask;
782 struct dma_chan *chan;
783 struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
784 /* We have to use IDMAC_IC_7 for Bayer / generic data */
785 struct dma_chan_request rq = {.mx3_cam = mx3_cam,
786 .id = IDMAC_IC_7};
787
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300788 dma_cap_zero(mask);
789 dma_cap_set(DMA_SLAVE, mask);
790 dma_cap_set(DMA_PRIVATE, mask);
791 chan = dma_request_channel(mask, chan_filter, &rq);
792 if (!chan)
793 return -EBUSY;
794
795 *ichan = to_idmac_chan(chan);
796 (*ichan)->client = mx3_cam;
797
798 return 0;
799}
800
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300801/*
802 * FIXME: learn to use stride != width, then we can keep stride properly aligned
803 * and support arbitrary (even) widths.
804 */
Márton Némethe26b3142010-02-24 17:13:29 -0300805static inline void stride_align(__u32 *width)
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300806{
807 if (((*width + 7) & ~7) < 4096)
808 *width = (*width + 7) & ~7;
809 else
810 *width = *width & ~7;
811}
812
813/*
814 * As long as we don't implement host-side cropping and scaling, we can use
815 * default g_crop and cropcap from soc_camera.c
816 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300817static int mx3_camera_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300818 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300819{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300820 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300821 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
822 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300823 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300824 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300825 int ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300826
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300827 soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
828 soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
829
830 ret = v4l2_subdev_call(sd, video, s_crop, a);
831 if (ret < 0)
832 return ret;
833
834 /* The capture device might have changed its output */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300835 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300836 if (ret < 0)
837 return ret;
838
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300839 if (mf.width & 7) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300840 /* Ouch! We can only handle 8-byte aligned width... */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300841 stride_align(&mf.width);
842 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300843 if (ret < 0)
844 return ret;
845 }
846
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300847 if (mf.width != icd->user_width || mf.height != icd->user_height)
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300848 configure_geometry(mx3_cam, mf.width, mf.height, mf.code);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300849
850 dev_dbg(icd->dev.parent, "Sensor cropped %dx%d\n",
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300851 mf.width, mf.height);
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300852
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300853 icd->user_width = mf.width;
854 icd->user_height = mf.height;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300855
856 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300857}
858
859static int mx3_camera_set_fmt(struct soc_camera_device *icd,
860 struct v4l2_format *f)
861{
862 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
863 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300864 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300865 const struct soc_camera_format_xlate *xlate;
866 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300867 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300868 int ret;
869
870 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
871 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300872 dev_warn(icd->dev.parent, "Format %x not found\n",
873 pix->pixelformat);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300874 return -EINVAL;
875 }
876
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300877 stride_align(&pix->width);
878 dev_dbg(icd->dev.parent, "Set format %dx%d\n", pix->width, pix->height);
879
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300880 /*
881 * Might have to perform a complete interface initialisation like in
882 * ipu_csi_init_interface() in mxc_v4l2_s_param(). Also consider
883 * mxc_v4l2_s_fmt()
884 */
885
Alberto Panizzoa48be1d2011-01-17 06:52:10 -0300886 configure_geometry(mx3_cam, pix->width, pix->height, xlate->code);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300887
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300888 mf.width = pix->width;
889 mf.height = pix->height;
890 mf.field = pix->field;
891 mf.colorspace = pix->colorspace;
892 mf.code = xlate->code;
893
894 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
895 if (ret < 0)
896 return ret;
897
898 if (mf.code != xlate->code)
899 return -EINVAL;
900
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300901 if (!mx3_cam->idmac_channel[0]) {
902 ret = acquire_dma_channel(mx3_cam);
903 if (ret < 0)
904 return ret;
905 }
906
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300907 pix->width = mf.width;
908 pix->height = mf.height;
909 pix->field = mf.field;
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300910 mx3_cam->field = mf.field;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300911 pix->colorspace = mf.colorspace;
912 icd->current_fmt = xlate;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300913
Anatolij Gustschin44facdc8c2011-01-31 09:58:01 -0300914 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
915 xlate->host_fmt);
916 if (pix->bytesperline < 0)
917 return pix->bytesperline;
918 pix->sizeimage = pix->height * pix->bytesperline;
919
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300920 dev_dbg(icd->dev.parent, "Sensor set %dx%d\n", pix->width, pix->height);
921
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300922 return ret;
923}
924
925static int mx3_camera_try_fmt(struct soc_camera_device *icd,
926 struct v4l2_format *f)
927{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300928 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300929 const struct soc_camera_format_xlate *xlate;
930 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300931 struct v4l2_mbus_framefmt mf;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300932 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300933 int ret;
934
935 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
936 if (pixfmt && !xlate) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300937 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300938 return -EINVAL;
939 }
940
941 /* limit to MX3 hardware capabilities */
942 if (pix->height > 4096)
943 pix->height = 4096;
944 if (pix->width > 4096)
945 pix->width = 4096;
946
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300947 pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
948 xlate->host_fmt);
949 if (pix->bytesperline < 0)
950 return pix->bytesperline;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300951 pix->sizeimage = pix->height * pix->bytesperline;
952
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300953 /* limit to sensor capabilities */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300954 mf.width = pix->width;
955 mf.height = pix->height;
956 mf.field = pix->field;
957 mf.colorspace = pix->colorspace;
958 mf.code = xlate->code;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300959
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300960 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
961 if (ret < 0)
962 return ret;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300963
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300964 pix->width = mf.width;
965 pix->height = mf.height;
966 pix->colorspace = mf.colorspace;
967
968 switch (mf.field) {
969 case V4L2_FIELD_ANY:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300970 pix->field = V4L2_FIELD_NONE;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300971 break;
972 case V4L2_FIELD_NONE:
973 break;
974 default:
975 dev_err(icd->dev.parent, "Field type %d unsupported.\n",
976 mf.field);
977 ret = -EINVAL;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300978 }
979
980 return ret;
981}
982
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300983static int mx3_camera_reqbufs(struct soc_camera_device *icd,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300984 struct v4l2_requestbuffers *p)
985{
986 return 0;
987}
988
989static unsigned int mx3_camera_poll(struct file *file, poll_table *pt)
990{
Guennadi Liakhovetski57bee292010-08-17 14:29:51 -0300991 struct soc_camera_device *icd = file->private_data;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300992
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -0300993 return vb2_poll(&icd->vb2_vidq, file, pt);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300994}
995
996static int mx3_camera_querycap(struct soc_camera_host *ici,
997 struct v4l2_capability *cap)
998{
999 /* cap->name is set by the firendly caller:-> */
1000 strlcpy(cap->card, "i.MX3x Camera", sizeof(cap->card));
1001 cap->version = KERNEL_VERSION(0, 2, 2);
1002 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1003
1004 return 0;
1005}
1006
1007static int mx3_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
1008{
1009 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1010 struct mx3_camera_dev *mx3_cam = ici->priv;
1011 unsigned long bus_flags, camera_flags, common_flags;
1012 u32 dw, sens_conf;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001013 const struct soc_mbus_pixelfmt *fmt;
1014 int buswidth;
1015 int ret;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001016 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001017 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001018
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001019 fmt = soc_mbus_get_fmtdesc(icd->current_fmt->code);
1020 if (!fmt)
1021 return -EINVAL;
1022
1023 buswidth = fmt->bits_per_sample;
1024 ret = test_platform_param(mx3_cam, buswidth, &bus_flags);
1025
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001026 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1027 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001028 dev_warn(dev, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001029 return -EINVAL;
1030 }
1031
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001032 dev_dbg(dev, "requested bus width %d bit: %d\n", buswidth, ret);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001033
1034 if (ret < 0)
1035 return ret;
1036
1037 camera_flags = icd->ops->query_bus_param(icd);
1038
1039 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001040 dev_dbg(dev, "Flags cam: 0x%lx host: 0x%lx common: 0x%lx\n",
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001041 camera_flags, bus_flags, common_flags);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001042 if (!common_flags) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001043 dev_dbg(dev, "no common flags");
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001044 return -EINVAL;
1045 }
1046
1047 /* Make choices, based on platform preferences */
1048 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
1049 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
1050 if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
1051 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
1052 else
1053 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
1054 }
1055
1056 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
1057 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
1058 if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
1059 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
1060 else
1061 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
1062 }
1063
1064 if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
1065 (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
1066 if (mx3_cam->platform_flags & MX3_CAMERA_DP)
1067 common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
1068 else
1069 common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
1070 }
1071
1072 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
1073 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
1074 if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
1075 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
1076 else
1077 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
1078 }
1079
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001080 /*
1081 * Make the camera work in widest common mode, we'll take care of
1082 * the rest
1083 */
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001084 if (common_flags & SOCAM_DATAWIDTH_15)
1085 common_flags = (common_flags & ~SOCAM_DATAWIDTH_MASK) |
1086 SOCAM_DATAWIDTH_15;
1087 else if (common_flags & SOCAM_DATAWIDTH_10)
1088 common_flags = (common_flags & ~SOCAM_DATAWIDTH_MASK) |
1089 SOCAM_DATAWIDTH_10;
1090 else if (common_flags & SOCAM_DATAWIDTH_8)
1091 common_flags = (common_flags & ~SOCAM_DATAWIDTH_MASK) |
1092 SOCAM_DATAWIDTH_8;
1093 else
1094 common_flags = (common_flags & ~SOCAM_DATAWIDTH_MASK) |
1095 SOCAM_DATAWIDTH_4;
1096
1097 ret = icd->ops->set_bus_param(icd, common_flags);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001098 if (ret < 0) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001099 dev_dbg(dev, "camera set_bus_param(%lx) returned %d\n",
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001100 common_flags, ret);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001101 return ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001102 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001103
1104 /*
1105 * So far only gated clock mode is supported. Add a line
1106 * (3 << CSI_SENS_CONF_SENS_PRTCL_SHIFT) |
1107 * below and select the required mode when supporting other
1108 * synchronisation protocols.
1109 */
1110 sens_conf = csi_reg_read(mx3_cam, CSI_SENS_CONF) &
1111 ~((1 << CSI_SENS_CONF_VSYNC_POL_SHIFT) |
1112 (1 << CSI_SENS_CONF_HSYNC_POL_SHIFT) |
1113 (1 << CSI_SENS_CONF_DATA_POL_SHIFT) |
1114 (1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT) |
1115 (3 << CSI_SENS_CONF_DATA_FMT_SHIFT) |
1116 (3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT));
1117
1118 /* TODO: Support RGB and YUV formats */
1119
1120 /* This has been set in mx3_camera_activate(), but we clear it above */
1121 sens_conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
1122
1123 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
1124 sens_conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
1125 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
1126 sens_conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
1127 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
1128 sens_conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
1129 if (common_flags & SOCAM_DATA_ACTIVE_LOW)
1130 sens_conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
1131
1132 /* Just do what we're asked to do */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -03001133 switch (xlate->host_fmt->bits_per_sample) {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001134 case 4:
1135 dw = 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1136 break;
1137 case 8:
1138 dw = 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1139 break;
1140 case 10:
1141 dw = 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1142 break;
1143 default:
1144 /*
1145 * Actually it can only be 15 now, default is just to silence
1146 * compiler warnings
1147 */
1148 case 15:
1149 dw = 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1150 }
1151
1152 csi_reg_write(mx3_cam, sens_conf | dw, CSI_SENS_CONF);
1153
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001154 dev_dbg(dev, "Set SENS_CONF to %x\n", sens_conf | dw);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001155
1156 return 0;
1157}
1158
1159static struct soc_camera_host_ops mx3_soc_camera_host_ops = {
1160 .owner = THIS_MODULE,
1161 .add = mx3_camera_add_device,
1162 .remove = mx3_camera_remove_device,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001163 .set_crop = mx3_camera_set_crop,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001164 .set_fmt = mx3_camera_set_fmt,
1165 .try_fmt = mx3_camera_try_fmt,
1166 .get_formats = mx3_camera_get_formats,
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -03001167 .init_videobuf2 = mx3_camera_init_videobuf,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001168 .reqbufs = mx3_camera_reqbufs,
1169 .poll = mx3_camera_poll,
1170 .querycap = mx3_camera_querycap,
1171 .set_bus_param = mx3_camera_set_bus_param,
1172};
1173
Jean Delvaree36bc312009-06-04 11:07:16 -03001174static int __devinit mx3_camera_probe(struct platform_device *pdev)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001175{
1176 struct mx3_camera_dev *mx3_cam;
1177 struct resource *res;
1178 void __iomem *base;
1179 int err = 0;
1180 struct soc_camera_host *soc_host;
1181
1182 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1183 if (!res) {
1184 err = -ENODEV;
1185 goto egetres;
1186 }
1187
Joe Perches101b25b2010-11-05 00:07:39 -03001188 mx3_cam = vzalloc(sizeof(*mx3_cam));
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001189 if (!mx3_cam) {
1190 dev_err(&pdev->dev, "Could not allocate mx3 camera object\n");
1191 err = -ENOMEM;
1192 goto ealloc;
1193 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001194
Guennadi Liakhovetskib71df972009-04-03 10:34:02 -03001195 mx3_cam->clk = clk_get(&pdev->dev, NULL);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001196 if (IS_ERR(mx3_cam->clk)) {
1197 err = PTR_ERR(mx3_cam->clk);
1198 goto eclkget;
1199 }
1200
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001201 mx3_cam->pdata = pdev->dev.platform_data;
1202 mx3_cam->platform_flags = mx3_cam->pdata->flags;
1203 if (!(mx3_cam->platform_flags & (MX3_CAMERA_DATAWIDTH_4 |
1204 MX3_CAMERA_DATAWIDTH_8 | MX3_CAMERA_DATAWIDTH_10 |
1205 MX3_CAMERA_DATAWIDTH_15))) {
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -03001206 /*
1207 * Platform hasn't set available data widths. This is bad.
1208 * Warn and use a default.
1209 */
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001210 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1211 "data widths, using default 8 bit\n");
1212 mx3_cam->platform_flags |= MX3_CAMERA_DATAWIDTH_8;
1213 }
1214
1215 mx3_cam->mclk = mx3_cam->pdata->mclk_10khz * 10000;
1216 if (!mx3_cam->mclk) {
1217 dev_warn(&pdev->dev,
1218 "mclk_10khz == 0! Please, fix your platform data. "
1219 "Using default 20MHz\n");
1220 mx3_cam->mclk = 20000000;
1221 }
1222
1223 /* list of video-buffers */
1224 INIT_LIST_HEAD(&mx3_cam->capture);
1225 spin_lock_init(&mx3_cam->lock);
1226
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001227 base = ioremap(res->start, resource_size(res));
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001228 if (!base) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001229 pr_err("Couldn't map %x@%x\n", resource_size(res), res->start);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001230 err = -ENOMEM;
1231 goto eioremap;
1232 }
1233
1234 mx3_cam->base = base;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001235
1236 soc_host = &mx3_cam->soc_host;
1237 soc_host->drv_name = MX3_CAM_DRV_NAME;
1238 soc_host->ops = &mx3_soc_camera_host_ops;
1239 soc_host->priv = mx3_cam;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001240 soc_host->v4l2_dev.dev = &pdev->dev;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001241 soc_host->nr = pdev->id;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001242
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -03001243 mx3_cam->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1244 if (IS_ERR(mx3_cam->alloc_ctx)) {
1245 err = PTR_ERR(mx3_cam->alloc_ctx);
1246 goto eallocctx;
1247 }
1248
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001249 err = soc_camera_host_register(soc_host);
1250 if (err)
1251 goto ecamhostreg;
1252
1253 /* IDMAC interface */
1254 dmaengine_get();
1255
1256 return 0;
1257
1258ecamhostreg:
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -03001259 vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
1260eallocctx:
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001261 iounmap(base);
1262eioremap:
1263 clk_put(mx3_cam->clk);
1264eclkget:
1265 vfree(mx3_cam);
1266ealloc:
1267egetres:
1268 return err;
1269}
1270
1271static int __devexit mx3_camera_remove(struct platform_device *pdev)
1272{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001273 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1274 struct mx3_camera_dev *mx3_cam = container_of(soc_host,
1275 struct mx3_camera_dev, soc_host);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001276
1277 clk_put(mx3_cam->clk);
1278
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001279 soc_camera_host_unregister(soc_host);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001280
1281 iounmap(mx3_cam->base);
1282
1283 /*
1284 * The channel has either not been allocated,
1285 * or should have been released
1286 */
1287 if (WARN_ON(mx3_cam->idmac_channel[0]))
1288 dma_release_channel(&mx3_cam->idmac_channel[0]->dma_chan);
1289
Guennadi Liakhovetski379fa5d2011-02-21 07:14:01 -03001290 vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
1291
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001292 vfree(mx3_cam);
1293
1294 dmaengine_put();
1295
1296 dev_info(&pdev->dev, "i.MX3x Camera driver unloaded\n");
1297
1298 return 0;
1299}
1300
1301static struct platform_driver mx3_camera_driver = {
1302 .driver = {
1303 .name = MX3_CAM_DRV_NAME,
1304 },
1305 .probe = mx3_camera_probe,
Jean Delvaree36bc312009-06-04 11:07:16 -03001306 .remove = __devexit_p(mx3_camera_remove),
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001307};
1308
1309
Jean Delvaree36bc312009-06-04 11:07:16 -03001310static int __init mx3_camera_init(void)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001311{
1312 return platform_driver_register(&mx3_camera_driver);
1313}
1314
1315static void __exit mx3_camera_exit(void)
1316{
1317 platform_driver_unregister(&mx3_camera_driver);
1318}
1319
1320module_init(mx3_camera_init);
1321module_exit(mx3_camera_exit);
1322
1323MODULE_DESCRIPTION("i.MX3x SoC Camera Host driver");
1324MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
1325MODULE_LICENSE("GPL v2");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001326MODULE_ALIAS("platform:" MX3_CAM_DRV_NAME);