blob: d5b51e9900bb874e4aba8b6a19f758c90b38aa5d [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>
20
21#include <media/v4l2-common.h>
22#include <media/v4l2-dev.h>
23#include <media/videobuf-dma-contig.h>
24#include <media/soc_camera.h>
25
26#include <mach/ipu.h>
27#include <mach/mx3_camera.h>
28
29#define MX3_CAM_DRV_NAME "mx3-camera"
30
31/* CMOS Sensor Interface Registers */
32#define CSI_REG_START 0x60
33
34#define CSI_SENS_CONF (0x60 - CSI_REG_START)
35#define CSI_SENS_FRM_SIZE (0x64 - CSI_REG_START)
36#define CSI_ACT_FRM_SIZE (0x68 - CSI_REG_START)
37#define CSI_OUT_FRM_CTRL (0x6C - CSI_REG_START)
38#define CSI_TST_CTRL (0x70 - CSI_REG_START)
39#define CSI_CCIR_CODE_1 (0x74 - CSI_REG_START)
40#define CSI_CCIR_CODE_2 (0x78 - CSI_REG_START)
41#define CSI_CCIR_CODE_3 (0x7C - CSI_REG_START)
42#define CSI_FLASH_STROBE_1 (0x80 - CSI_REG_START)
43#define CSI_FLASH_STROBE_2 (0x84 - CSI_REG_START)
44
45#define CSI_SENS_CONF_VSYNC_POL_SHIFT 0
46#define CSI_SENS_CONF_HSYNC_POL_SHIFT 1
47#define CSI_SENS_CONF_DATA_POL_SHIFT 2
48#define CSI_SENS_CONF_PIX_CLK_POL_SHIFT 3
49#define CSI_SENS_CONF_SENS_PRTCL_SHIFT 4
50#define CSI_SENS_CONF_SENS_CLKSRC_SHIFT 7
51#define CSI_SENS_CONF_DATA_FMT_SHIFT 8
52#define CSI_SENS_CONF_DATA_WIDTH_SHIFT 10
53#define CSI_SENS_CONF_EXT_VSYNC_SHIFT 15
54#define CSI_SENS_CONF_DIVRATIO_SHIFT 16
55
56#define CSI_SENS_CONF_DATA_FMT_RGB_YUV444 (0UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
57#define CSI_SENS_CONF_DATA_FMT_YUV422 (2UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
58#define CSI_SENS_CONF_DATA_FMT_BAYER (3UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
59
60#define MAX_VIDEO_MEM 16
61
62struct mx3_camera_buffer {
63 /* common v4l buffer stuff -- must be first */
64 struct videobuf_buffer vb;
65 const struct soc_camera_data_format *fmt;
66
67 /* One descriptot per scatterlist (per frame) */
68 struct dma_async_tx_descriptor *txd;
69
70 /* We have to "build" a scatterlist ourselves - one element per frame */
71 struct scatterlist sg;
72};
73
74/**
75 * struct mx3_camera_dev - i.MX3x camera (CSI) object
76 * @dev: camera device, to which the coherent buffer is attached
77 * @icd: currently attached camera sensor
78 * @clk: pointer to clock
79 * @base: remapped register base address
80 * @pdata: platform data
81 * @platform_flags: platform flags
82 * @mclk: master clock frequency in Hz
83 * @capture: list of capture videobuffers
84 * @lock: protects video buffer lists
85 * @active: active video buffer
86 * @idmac_channel: array of pointers to IPU DMAC DMA channels
87 * @soc_host: embedded soc_host object
88 */
89struct mx3_camera_dev {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -030090 /*
91 * i.MX3x is only supposed to handle one camera on its Camera Sensor
92 * Interface. If anyone ever builds hardware to enable more than one
93 * camera _simultaneously_, they will have to modify this driver too
94 */
95 struct soc_camera_device *icd;
96 struct clk *clk;
97
98 void __iomem *base;
99
100 struct mx3_camera_pdata *pdata;
101
102 unsigned long platform_flags;
103 unsigned long mclk;
104
105 struct list_head capture;
106 spinlock_t lock; /* Protects video buffer lists */
107 struct mx3_camera_buffer *active;
108
109 /* IDMAC / dmaengine interface */
110 struct idmac_channel *idmac_channel[1]; /* We need one channel */
111
112 struct soc_camera_host soc_host;
113};
114
115struct dma_chan_request {
116 struct mx3_camera_dev *mx3_cam;
117 enum ipu_channel id;
118};
119
120static int mx3_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt);
121
122static u32 csi_reg_read(struct mx3_camera_dev *mx3, off_t reg)
123{
124 return __raw_readl(mx3->base + reg);
125}
126
127static void csi_reg_write(struct mx3_camera_dev *mx3, u32 value, off_t reg)
128{
129 __raw_writel(value, mx3->base + reg);
130}
131
132/* Called from the IPU IDMAC ISR */
133static void mx3_cam_dma_done(void *arg)
134{
135 struct idmac_tx_desc *desc = to_tx_desc(arg);
136 struct dma_chan *chan = desc->txd.chan;
137 struct idmac_channel *ichannel = to_idmac_chan(chan);
138 struct mx3_camera_dev *mx3_cam = ichannel->client;
139 struct videobuf_buffer *vb;
140
141 dev_dbg(chan->device->dev, "callback cookie %d, active DMA 0x%08x\n",
142 desc->txd.cookie, mx3_cam->active ? sg_dma_address(&mx3_cam->active->sg) : 0);
143
144 spin_lock(&mx3_cam->lock);
145 if (mx3_cam->active) {
146 vb = &mx3_cam->active->vb;
147
148 list_del_init(&vb->queue);
149 vb->state = VIDEOBUF_DONE;
150 do_gettimeofday(&vb->ts);
151 vb->field_count++;
152 wake_up(&vb->done);
153 }
154
155 if (list_empty(&mx3_cam->capture)) {
156 mx3_cam->active = NULL;
157 spin_unlock(&mx3_cam->lock);
158
159 /*
160 * stop capture - without further buffers IPU_CHA_BUF0_RDY will
161 * not get updated
162 */
163 return;
164 }
165
166 mx3_cam->active = list_entry(mx3_cam->capture.next,
167 struct mx3_camera_buffer, vb.queue);
168 mx3_cam->active->vb.state = VIDEOBUF_ACTIVE;
169 spin_unlock(&mx3_cam->lock);
170}
171
172static void free_buffer(struct videobuf_queue *vq, struct mx3_camera_buffer *buf)
173{
174 struct soc_camera_device *icd = vq->priv_data;
175 struct videobuf_buffer *vb = &buf->vb;
176 struct dma_async_tx_descriptor *txd = buf->txd;
177 struct idmac_channel *ichan;
178
179 BUG_ON(in_interrupt());
180
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300181 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300182 vb, vb->baddr, vb->bsize);
183
184 /*
185 * This waits until this buffer is out of danger, i.e., until it is no
186 * longer in STATE_QUEUED or STATE_ACTIVE
187 */
188 videobuf_waiton(vb, 0, 0);
189 if (txd) {
190 ichan = to_idmac_chan(txd->chan);
191 async_tx_ack(txd);
192 }
193 videobuf_dma_contig_free(vq, vb);
194 buf->txd = NULL;
195
196 vb->state = VIDEOBUF_NEEDS_INIT;
197}
198
199/*
200 * Videobuf operations
201 */
202
203/*
204 * Calculate the __buffer__ (not data) size and number of buffers.
205 * Called with .vb_lock held
206 */
207static int mx3_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
208 unsigned int *size)
209{
210 struct soc_camera_device *icd = vq->priv_data;
211 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
212 struct mx3_camera_dev *mx3_cam = ici->priv;
213 /*
214 * bits-per-pixel (depth) as specified in camera's pixel format does
215 * not necessarily match what the camera interface writes to RAM, but
216 * it should be good enough for now.
217 */
218 unsigned int bpp = DIV_ROUND_UP(icd->current_fmt->depth, 8);
219
220 if (!mx3_cam->idmac_channel[0])
221 return -EINVAL;
222
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300223 *size = icd->rect_current.width * icd->rect_current.height * bpp;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300224
225 if (!*count)
226 *count = 32;
227
228 if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
229 *count = MAX_VIDEO_MEM * 1024 * 1024 / *size;
230
231 return 0;
232}
233
234/* Called with .vb_lock held */
235static int mx3_videobuf_prepare(struct videobuf_queue *vq,
236 struct videobuf_buffer *vb, enum v4l2_field field)
237{
238 struct soc_camera_device *icd = vq->priv_data;
239 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
240 struct mx3_camera_dev *mx3_cam = ici->priv;
241 struct mx3_camera_buffer *buf =
242 container_of(vb, struct mx3_camera_buffer, vb);
243 /* current_fmt _must_ always be set */
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300244 size_t new_size = icd->rect_current.width * icd->rect_current.height *
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300245 ((icd->current_fmt->depth + 7) >> 3);
246 int ret;
247
248 /*
249 * I think, in buf_prepare you only have to protect global data,
250 * the actual buffer is yours
251 */
252
253 if (buf->fmt != icd->current_fmt ||
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300254 vb->width != icd->rect_current.width ||
255 vb->height != icd->rect_current.height ||
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300256 vb->field != field) {
257 buf->fmt = icd->current_fmt;
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300258 vb->width = icd->rect_current.width;
259 vb->height = icd->rect_current.height;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300260 vb->field = field;
261 if (vb->state != VIDEOBUF_NEEDS_INIT)
262 free_buffer(vq, buf);
263 }
264
265 if (vb->baddr && vb->bsize < new_size) {
266 /* User provided buffer, but it is too small */
267 ret = -ENOMEM;
268 goto out;
269 }
270
271 if (vb->state == VIDEOBUF_NEEDS_INIT) {
272 struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
273 struct scatterlist *sg = &buf->sg;
274
275 /*
276 * The total size of video-buffers that will be allocated / mapped.
277 * *size that we calculated in videobuf_setup gets assigned to
278 * vb->bsize, and now we use the same calculation to get vb->size.
279 */
280 vb->size = new_size;
281
282 /* This actually (allocates and) maps buffers */
283 ret = videobuf_iolock(vq, vb, NULL);
284 if (ret)
285 goto fail;
286
287 /*
288 * We will have to configure the IDMAC channel. It has two slots
289 * for DMA buffers, we shall enter the first two buffers there,
290 * and then submit new buffers in DMA-ready interrupts
291 */
292 sg_init_table(sg, 1);
293 sg_dma_address(sg) = videobuf_to_dma_contig(vb);
294 sg_dma_len(sg) = vb->size;
295
296 buf->txd = ichan->dma_chan.device->device_prep_slave_sg(
297 &ichan->dma_chan, sg, 1, DMA_FROM_DEVICE,
298 DMA_PREP_INTERRUPT);
299 if (!buf->txd) {
300 ret = -EIO;
301 goto fail;
302 }
303
304 buf->txd->callback_param = buf->txd;
305 buf->txd->callback = mx3_cam_dma_done;
306
307 vb->state = VIDEOBUF_PREPARED;
308 }
309
310 return 0;
311
312fail:
313 free_buffer(vq, buf);
314out:
315 return ret;
316}
317
318static enum pixel_fmt fourcc_to_ipu_pix(__u32 fourcc)
319{
320 /* Add more formats as need arises and test possibilities appear... */
321 switch (fourcc) {
322 case V4L2_PIX_FMT_RGB565:
323 return IPU_PIX_FMT_RGB565;
324 case V4L2_PIX_FMT_RGB24:
325 return IPU_PIX_FMT_RGB24;
326 case V4L2_PIX_FMT_RGB332:
327 return IPU_PIX_FMT_RGB332;
328 case V4L2_PIX_FMT_YUV422P:
329 return IPU_PIX_FMT_YVU422P;
330 default:
331 return IPU_PIX_FMT_GENERIC;
332 }
333}
334
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300335/*
336 * Called with .vb_lock mutex held and
337 * under spinlock_irqsave(&mx3_cam->lock, ...)
338 */
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300339static void mx3_videobuf_queue(struct videobuf_queue *vq,
340 struct videobuf_buffer *vb)
341{
342 struct soc_camera_device *icd = vq->priv_data;
343 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
344 struct mx3_camera_dev *mx3_cam = ici->priv;
345 struct mx3_camera_buffer *buf =
346 container_of(vb, struct mx3_camera_buffer, vb);
347 struct dma_async_tx_descriptor *txd = buf->txd;
348 struct idmac_channel *ichan = to_idmac_chan(txd->chan);
349 struct idmac_video_param *video = &ichan->params.video;
350 const struct soc_camera_data_format *data_fmt = icd->current_fmt;
351 dma_cookie_t cookie;
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300352
353 BUG_ON(!irqs_disabled());
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300354
355 /* This is the configuration of one sg-element */
356 video->out_pixel_fmt = fourcc_to_ipu_pix(data_fmt->fourcc);
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300357 video->out_width = icd->rect_current.width;
358 video->out_height = icd->rect_current.height;
359 video->out_stride = icd->rect_current.width;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300360
361#ifdef DEBUG
362 /* helps to see what DMA actually has written */
363 memset((void *)vb->baddr, 0xaa, vb->bsize);
364#endif
365
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300366 list_add_tail(&vb->queue, &mx3_cam->capture);
367
368 if (!mx3_cam->active) {
369 mx3_cam->active = buf;
370 vb->state = VIDEOBUF_ACTIVE;
371 } else {
372 vb->state = VIDEOBUF_QUEUED;
373 }
374
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300375 spin_unlock_irq(&mx3_cam->lock);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300376
377 cookie = txd->tx_submit(txd);
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300378 dev_dbg(icd->dev.parent, "Submitted cookie %d DMA 0x%08x\n",
379 cookie, sg_dma_address(&buf->sg));
Guennadi Liakhovetski2dd54a52009-08-05 20:06:31 -0300380
381 spin_lock_irq(&mx3_cam->lock);
382
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300383 if (cookie >= 0)
384 return;
385
386 /* Submit error */
387 vb->state = VIDEOBUF_PREPARED;
388
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300389 list_del_init(&vb->queue);
390
391 if (mx3_cam->active == buf)
392 mx3_cam->active = NULL;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300393}
394
395/* Called with .vb_lock held */
396static void mx3_videobuf_release(struct videobuf_queue *vq,
397 struct videobuf_buffer *vb)
398{
399 struct soc_camera_device *icd = vq->priv_data;
400 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
401 struct mx3_camera_dev *mx3_cam = ici->priv;
402 struct mx3_camera_buffer *buf =
403 container_of(vb, struct mx3_camera_buffer, vb);
404 unsigned long flags;
405
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300406 dev_dbg(icd->dev.parent,
407 "Release%s DMA 0x%08x (state %d), queue %sempty\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300408 mx3_cam->active == buf ? " active" : "", sg_dma_address(&buf->sg),
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300409 vb->state, list_empty(&vb->queue) ? "" : "not ");
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300410 spin_lock_irqsave(&mx3_cam->lock, flags);
411 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
412 !list_empty(&vb->queue)) {
413 vb->state = VIDEOBUF_ERROR;
414
415 list_del_init(&vb->queue);
416 if (mx3_cam->active == buf)
417 mx3_cam->active = NULL;
418 }
419 spin_unlock_irqrestore(&mx3_cam->lock, flags);
420 free_buffer(vq, buf);
421}
422
423static struct videobuf_queue_ops mx3_videobuf_ops = {
424 .buf_setup = mx3_videobuf_setup,
425 .buf_prepare = mx3_videobuf_prepare,
426 .buf_queue = mx3_videobuf_queue,
427 .buf_release = mx3_videobuf_release,
428};
429
430static void mx3_camera_init_videobuf(struct videobuf_queue *q,
431 struct soc_camera_device *icd)
432{
433 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
434 struct mx3_camera_dev *mx3_cam = ici->priv;
435
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300436 videobuf_queue_dma_contig_init(q, &mx3_videobuf_ops, icd->dev.parent,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300437 &mx3_cam->lock,
438 V4L2_BUF_TYPE_VIDEO_CAPTURE,
439 V4L2_FIELD_NONE,
440 sizeof(struct mx3_camera_buffer), icd);
441}
442
443/* First part of ipu_csi_init_interface() */
444static void mx3_camera_activate(struct mx3_camera_dev *mx3_cam,
445 struct soc_camera_device *icd)
446{
447 u32 conf;
448 long rate;
449
450 /* Set default size: ipu_csi_set_window_size() */
451 csi_reg_write(mx3_cam, (640 - 1) | ((480 - 1) << 16), CSI_ACT_FRM_SIZE);
452 /* ...and position to 0:0: ipu_csi_set_window_pos() */
453 conf = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
454 csi_reg_write(mx3_cam, conf, CSI_OUT_FRM_CTRL);
455
456 /* We use only gated clock synchronisation mode so far */
457 conf = 0 << CSI_SENS_CONF_SENS_PRTCL_SHIFT;
458
459 /* Set generic data, platform-biggest bus-width */
460 conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
461
462 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
463 conf |= 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
464 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
465 conf |= 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
466 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
467 conf |= 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
468 else/* if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)*/
469 conf |= 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
470
471 if (mx3_cam->platform_flags & MX3_CAMERA_CLK_SRC)
472 conf |= 1 << CSI_SENS_CONF_SENS_CLKSRC_SHIFT;
473 if (mx3_cam->platform_flags & MX3_CAMERA_EXT_VSYNC)
474 conf |= 1 << CSI_SENS_CONF_EXT_VSYNC_SHIFT;
475 if (mx3_cam->platform_flags & MX3_CAMERA_DP)
476 conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
477 if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
478 conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
479 if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
480 conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
481 if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
482 conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
483
484 /* ipu_csi_init_interface() */
485 csi_reg_write(mx3_cam, conf, CSI_SENS_CONF);
486
487 clk_enable(mx3_cam->clk);
488 rate = clk_round_rate(mx3_cam->clk, mx3_cam->mclk);
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300489 dev_dbg(icd->dev.parent, "Set SENS_CONF to %x, rate %ld\n", conf, rate);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300490 if (rate)
491 clk_set_rate(mx3_cam->clk, rate);
492}
493
494/* Called with .video_lock held */
495static int mx3_camera_add_device(struct soc_camera_device *icd)
496{
497 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
498 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300499
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300500 if (mx3_cam->icd)
501 return -EBUSY;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300502
503 mx3_camera_activate(mx3_cam, icd);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300504
505 mx3_cam->icd = icd;
506
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300507 dev_info(icd->dev.parent, "MX3 Camera driver attached to camera %d\n",
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300508 icd->devnum);
509
510 return 0;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300511}
512
513/* Called with .video_lock held */
514static void mx3_camera_remove_device(struct soc_camera_device *icd)
515{
516 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
517 struct mx3_camera_dev *mx3_cam = ici->priv;
518 struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
519
520 BUG_ON(icd != mx3_cam->icd);
521
522 if (*ichan) {
523 dma_release_channel(&(*ichan)->dma_chan);
524 *ichan = NULL;
525 }
526
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300527 clk_disable(mx3_cam->clk);
528
529 mx3_cam->icd = NULL;
530
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300531 dev_info(icd->dev.parent, "MX3 Camera driver detached from camera %d\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300532 icd->devnum);
533}
534
535static bool channel_change_requested(struct soc_camera_device *icd,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300536 struct v4l2_rect *rect)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300537{
538 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
539 struct mx3_camera_dev *mx3_cam = ici->priv;
540 struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
541
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300542 /* Do buffers have to be re-allocated or channel re-configured? */
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300543 return ichan && rect->width * rect->height >
544 icd->rect_current.width * icd->rect_current.height;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300545}
546
547static int test_platform_param(struct mx3_camera_dev *mx3_cam,
548 unsigned char buswidth, unsigned long *flags)
549{
550 /*
551 * Platform specified synchronization and pixel clock polarities are
552 * only a recommendation and are only used during probing. MX3x
553 * camera interface only works in master mode, i.e., uses HSYNC and
554 * VSYNC signals from the sensor
555 */
556 *flags = SOCAM_MASTER |
557 SOCAM_HSYNC_ACTIVE_HIGH |
558 SOCAM_HSYNC_ACTIVE_LOW |
559 SOCAM_VSYNC_ACTIVE_HIGH |
560 SOCAM_VSYNC_ACTIVE_LOW |
561 SOCAM_PCLK_SAMPLE_RISING |
562 SOCAM_PCLK_SAMPLE_FALLING |
563 SOCAM_DATA_ACTIVE_HIGH |
564 SOCAM_DATA_ACTIVE_LOW;
565
566 /* If requested data width is supported by the platform, use it or any
567 * possible lower value - i.MX31 is smart enough to schift bits */
568 switch (buswidth) {
569 case 15:
570 if (!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15))
571 return -EINVAL;
572 *flags |= SOCAM_DATAWIDTH_15 | SOCAM_DATAWIDTH_10 |
573 SOCAM_DATAWIDTH_8 | SOCAM_DATAWIDTH_4;
574 break;
575 case 10:
576 if (!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10))
577 return -EINVAL;
578 *flags |= SOCAM_DATAWIDTH_10 | SOCAM_DATAWIDTH_8 |
579 SOCAM_DATAWIDTH_4;
580 break;
581 case 8:
582 if (!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8))
583 return -EINVAL;
584 *flags |= SOCAM_DATAWIDTH_8 | SOCAM_DATAWIDTH_4;
585 break;
586 case 4:
587 if (!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4))
588 return -EINVAL;
589 *flags |= SOCAM_DATAWIDTH_4;
590 break;
591 default:
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300592 dev_info(mx3_cam->soc_host.v4l2_dev.dev, "Unsupported bus width %d\n",
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -0300593 buswidth);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300594 return -EINVAL;
595 }
596
597 return 0;
598}
599
600static int mx3_camera_try_bus_param(struct soc_camera_device *icd,
601 const unsigned int depth)
602{
603 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
604 struct mx3_camera_dev *mx3_cam = ici->priv;
605 unsigned long bus_flags, camera_flags;
606 int ret = test_platform_param(mx3_cam, depth, &bus_flags);
607
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300608 dev_dbg(icd->dev.parent, "requested bus width %d bit: %d\n",
609 depth, ret);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300610
611 if (ret < 0)
612 return ret;
613
614 camera_flags = icd->ops->query_bus_param(icd);
615
616 ret = soc_camera_bus_param_compatible(camera_flags, bus_flags);
617 if (ret < 0)
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300618 dev_warn(icd->dev.parent,
619 "Flags incompatible: camera %lx, host %lx\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300620 camera_flags, bus_flags);
621
622 return ret;
623}
624
625static bool chan_filter(struct dma_chan *chan, void *arg)
626{
627 struct dma_chan_request *rq = arg;
628 struct mx3_camera_pdata *pdata;
629
630 if (!rq)
631 return false;
632
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300633 pdata = rq->mx3_cam->soc_host.v4l2_dev.dev->platform_data;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300634
635 return rq->id == chan->chan_id &&
636 pdata->dma_dev == chan->device->dev;
637}
638
639static const struct soc_camera_data_format mx3_camera_formats[] = {
640 {
641 .name = "Bayer (sRGB) 8 bit",
642 .depth = 8,
643 .fourcc = V4L2_PIX_FMT_SBGGR8,
644 .colorspace = V4L2_COLORSPACE_SRGB,
645 }, {
646 .name = "Monochrome 8 bit",
647 .depth = 8,
648 .fourcc = V4L2_PIX_FMT_GREY,
649 .colorspace = V4L2_COLORSPACE_JPEG,
650 },
651};
652
653static bool buswidth_supported(struct soc_camera_host *ici, int depth)
654{
655 struct mx3_camera_dev *mx3_cam = ici->priv;
656
657 switch (depth) {
658 case 4:
659 return !!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4);
660 case 8:
661 return !!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8);
662 case 10:
663 return !!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10);
664 case 15:
665 return !!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15);
666 }
667 return false;
668}
669
670static int mx3_camera_get_formats(struct soc_camera_device *icd, int idx,
671 struct soc_camera_format_xlate *xlate)
672{
673 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
674 int formats = 0, buswidth, ret;
675
676 buswidth = icd->formats[idx].depth;
677
678 if (!buswidth_supported(ici, buswidth))
679 return 0;
680
681 ret = mx3_camera_try_bus_param(icd, buswidth);
682 if (ret < 0)
683 return 0;
684
685 switch (icd->formats[idx].fourcc) {
686 case V4L2_PIX_FMT_SGRBG10:
687 formats++;
688 if (xlate) {
689 xlate->host_fmt = &mx3_camera_formats[0];
690 xlate->cam_fmt = icd->formats + idx;
691 xlate->buswidth = buswidth;
692 xlate++;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300693 dev_dbg(icd->dev.parent,
694 "Providing format %s using %s\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300695 mx3_camera_formats[0].name,
696 icd->formats[idx].name);
697 }
698 goto passthrough;
699 case V4L2_PIX_FMT_Y16:
700 formats++;
701 if (xlate) {
702 xlate->host_fmt = &mx3_camera_formats[1];
703 xlate->cam_fmt = icd->formats + idx;
704 xlate->buswidth = buswidth;
705 xlate++;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300706 dev_dbg(icd->dev.parent,
707 "Providing format %s using %s\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300708 mx3_camera_formats[0].name,
709 icd->formats[idx].name);
710 }
711 default:
712passthrough:
713 /* Generic pass-through */
714 formats++;
715 if (xlate) {
716 xlate->host_fmt = icd->formats + idx;
717 xlate->cam_fmt = icd->formats + idx;
718 xlate->buswidth = buswidth;
719 xlate++;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300720 dev_dbg(icd->dev.parent,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300721 "Providing format %s in pass-through mode\n",
722 icd->formats[idx].name);
723 }
724 }
725
726 return formats;
727}
728
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300729static void configure_geometry(struct mx3_camera_dev *mx3_cam,
730 struct v4l2_rect *rect)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300731{
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300732 u32 ctrl, width_field, height_field;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300733
734 /* Setup frame size - this cannot be changed on-the-fly... */
735 width_field = rect->width - 1;
736 height_field = rect->height - 1;
737 csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_SENS_FRM_SIZE);
738
739 csi_reg_write(mx3_cam, width_field << 16, CSI_FLASH_STROBE_1);
740 csi_reg_write(mx3_cam, (height_field << 16) | 0x22, CSI_FLASH_STROBE_2);
741
742 csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_ACT_FRM_SIZE);
743
744 /* ...and position */
745 ctrl = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
746 /* Sensor does the cropping */
747 csi_reg_write(mx3_cam, ctrl | 0 | (0 << 8), CSI_OUT_FRM_CTRL);
748
749 /*
750 * No need to free resources here if we fail, we'll see if we need to
751 * do this next time we are called
752 */
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300753}
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300754
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300755static int acquire_dma_channel(struct mx3_camera_dev *mx3_cam)
756{
757 dma_cap_mask_t mask;
758 struct dma_chan *chan;
759 struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
760 /* We have to use IDMAC_IC_7 for Bayer / generic data */
761 struct dma_chan_request rq = {.mx3_cam = mx3_cam,
762 .id = IDMAC_IC_7};
763
764 if (*ichan) {
765 struct videobuf_buffer *vb, *_vb;
766 dma_release_channel(&(*ichan)->dma_chan);
767 *ichan = NULL;
768 mx3_cam->active = NULL;
769 list_for_each_entry_safe(vb, _vb, &mx3_cam->capture, queue) {
770 list_del_init(&vb->queue);
771 vb->state = VIDEOBUF_ERROR;
772 wake_up(&vb->done);
773 }
774 }
775
776 dma_cap_zero(mask);
777 dma_cap_set(DMA_SLAVE, mask);
778 dma_cap_set(DMA_PRIVATE, mask);
779 chan = dma_request_channel(mask, chan_filter, &rq);
780 if (!chan)
781 return -EBUSY;
782
783 *ichan = to_idmac_chan(chan);
784 (*ichan)->client = mx3_cam;
785
786 return 0;
787}
788
789static int mx3_camera_set_crop(struct soc_camera_device *icd,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300790 struct v4l2_crop *a)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300791{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300792 struct v4l2_rect *rect = &a->c;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300793 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
794 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300795 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300796
797 /*
798 * We now know pixel formats and can decide upon DMA-channel(s)
799 * So far only direct camera-to-memory is supported
800 */
801 if (channel_change_requested(icd, rect)) {
802 int ret = acquire_dma_channel(mx3_cam);
803 if (ret < 0)
804 return ret;
805 }
806
807 configure_geometry(mx3_cam, rect);
808
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300809 return v4l2_subdev_call(sd, video, s_crop, a);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300810}
811
812static int mx3_camera_set_fmt(struct soc_camera_device *icd,
813 struct v4l2_format *f)
814{
815 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
816 struct mx3_camera_dev *mx3_cam = ici->priv;
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300817 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300818 const struct soc_camera_format_xlate *xlate;
819 struct v4l2_pix_format *pix = &f->fmt.pix;
820 struct v4l2_rect rect = {
Guennadi Liakhovetskia0705b02009-08-25 11:46:17 -0300821 .left = icd->rect_current.left,
822 .top = icd->rect_current.top,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300823 .width = pix->width,
824 .height = pix->height,
825 };
826 int ret;
827
828 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
829 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300830 dev_warn(icd->dev.parent, "Format %x not found\n",
831 pix->pixelformat);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300832 return -EINVAL;
833 }
834
835 ret = acquire_dma_channel(mx3_cam);
836 if (ret < 0)
837 return ret;
838
839 /*
840 * Might have to perform a complete interface initialisation like in
841 * ipu_csi_init_interface() in mxc_v4l2_s_param(). Also consider
842 * mxc_v4l2_s_fmt()
843 */
844
845 configure_geometry(mx3_cam, &rect);
846
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300847 ret = v4l2_subdev_call(sd, video, s_fmt, f);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300848 if (!ret) {
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300849 icd->buswidth = xlate->buswidth;
850 icd->current_fmt = xlate->host_fmt;
851 }
852
853 return ret;
854}
855
856static int mx3_camera_try_fmt(struct soc_camera_device *icd,
857 struct v4l2_format *f)
858{
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300859 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300860 const struct soc_camera_format_xlate *xlate;
861 struct v4l2_pix_format *pix = &f->fmt.pix;
862 __u32 pixfmt = pix->pixelformat;
863 enum v4l2_field field;
864 int ret;
865
866 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
867 if (pixfmt && !xlate) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300868 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300869 return -EINVAL;
870 }
871
872 /* limit to MX3 hardware capabilities */
873 if (pix->height > 4096)
874 pix->height = 4096;
875 if (pix->width > 4096)
876 pix->width = 4096;
877
878 pix->bytesperline = pix->width *
879 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
880 pix->sizeimage = pix->height * pix->bytesperline;
881
882 /* camera has to see its format, but the user the original one */
883 pix->pixelformat = xlate->cam_fmt->fourcc;
884 /* limit to sensor capabilities */
Guennadi Liakhovetskic9c1f1c2009-08-25 11:46:59 -0300885 ret = v4l2_subdev_call(sd, video, try_fmt, f);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300886 pix->pixelformat = xlate->host_fmt->fourcc;
887
888 field = pix->field;
889
890 if (field == V4L2_FIELD_ANY) {
891 pix->field = V4L2_FIELD_NONE;
892 } else if (field != V4L2_FIELD_NONE) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300893 dev_err(icd->dev.parent, "Field type %d unsupported.\n", field);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300894 return -EINVAL;
895 }
896
897 return ret;
898}
899
900static int mx3_camera_reqbufs(struct soc_camera_file *icf,
901 struct v4l2_requestbuffers *p)
902{
903 return 0;
904}
905
906static unsigned int mx3_camera_poll(struct file *file, poll_table *pt)
907{
908 struct soc_camera_file *icf = file->private_data;
909
910 return videobuf_poll_stream(file, &icf->vb_vidq, pt);
911}
912
913static int mx3_camera_querycap(struct soc_camera_host *ici,
914 struct v4l2_capability *cap)
915{
916 /* cap->name is set by the firendly caller:-> */
917 strlcpy(cap->card, "i.MX3x Camera", sizeof(cap->card));
918 cap->version = KERNEL_VERSION(0, 2, 2);
919 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
920
921 return 0;
922}
923
924static int mx3_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
925{
926 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
927 struct mx3_camera_dev *mx3_cam = ici->priv;
928 unsigned long bus_flags, camera_flags, common_flags;
929 u32 dw, sens_conf;
930 int ret = test_platform_param(mx3_cam, icd->buswidth, &bus_flags);
931 const struct soc_camera_format_xlate *xlate;
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300932 struct device *dev = icd->dev.parent;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300933
934 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
935 if (!xlate) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300936 dev_warn(dev, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300937 return -EINVAL;
938 }
939
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300940 dev_dbg(dev, "requested bus width %d bit: %d\n",
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300941 icd->buswidth, ret);
942
943 if (ret < 0)
944 return ret;
945
946 camera_flags = icd->ops->query_bus_param(icd);
947
948 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300949 dev_dbg(dev, "Flags cam: 0x%lx host: 0x%lx common: 0x%lx\n",
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300950 camera_flags, bus_flags, common_flags);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300951 if (!common_flags) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -0300952 dev_dbg(dev, "no common flags");
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -0300953 return -EINVAL;
954 }
955
956 /* Make choices, based on platform preferences */
957 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
958 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
959 if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
960 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
961 else
962 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
963 }
964
965 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
966 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
967 if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
968 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
969 else
970 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
971 }
972
973 if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
974 (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
975 if (mx3_cam->platform_flags & MX3_CAMERA_DP)
976 common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
977 else
978 common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
979 }
980
981 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
982 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
983 if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
984 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
985 else
986 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
987 }
988
989 /* Make the camera work in widest common mode, we'll take care of
990 * the rest */
991 if (common_flags & SOCAM_DATAWIDTH_15)
992 common_flags = (common_flags & ~SOCAM_DATAWIDTH_MASK) |
993 SOCAM_DATAWIDTH_15;
994 else if (common_flags & SOCAM_DATAWIDTH_10)
995 common_flags = (common_flags & ~SOCAM_DATAWIDTH_MASK) |
996 SOCAM_DATAWIDTH_10;
997 else if (common_flags & SOCAM_DATAWIDTH_8)
998 common_flags = (common_flags & ~SOCAM_DATAWIDTH_MASK) |
999 SOCAM_DATAWIDTH_8;
1000 else
1001 common_flags = (common_flags & ~SOCAM_DATAWIDTH_MASK) |
1002 SOCAM_DATAWIDTH_4;
1003
1004 ret = icd->ops->set_bus_param(icd, common_flags);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001005 if (ret < 0) {
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001006 dev_dbg(dev, "camera set_bus_param(%lx) returned %d\n",
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001007 common_flags, ret);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001008 return ret;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001009 }
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001010
1011 /*
1012 * So far only gated clock mode is supported. Add a line
1013 * (3 << CSI_SENS_CONF_SENS_PRTCL_SHIFT) |
1014 * below and select the required mode when supporting other
1015 * synchronisation protocols.
1016 */
1017 sens_conf = csi_reg_read(mx3_cam, CSI_SENS_CONF) &
1018 ~((1 << CSI_SENS_CONF_VSYNC_POL_SHIFT) |
1019 (1 << CSI_SENS_CONF_HSYNC_POL_SHIFT) |
1020 (1 << CSI_SENS_CONF_DATA_POL_SHIFT) |
1021 (1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT) |
1022 (3 << CSI_SENS_CONF_DATA_FMT_SHIFT) |
1023 (3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT));
1024
1025 /* TODO: Support RGB and YUV formats */
1026
1027 /* This has been set in mx3_camera_activate(), but we clear it above */
1028 sens_conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
1029
1030 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
1031 sens_conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
1032 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
1033 sens_conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
1034 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
1035 sens_conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
1036 if (common_flags & SOCAM_DATA_ACTIVE_LOW)
1037 sens_conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
1038
1039 /* Just do what we're asked to do */
1040 switch (xlate->host_fmt->depth) {
1041 case 4:
1042 dw = 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1043 break;
1044 case 8:
1045 dw = 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1046 break;
1047 case 10:
1048 dw = 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1049 break;
1050 default:
1051 /*
1052 * Actually it can only be 15 now, default is just to silence
1053 * compiler warnings
1054 */
1055 case 15:
1056 dw = 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1057 }
1058
1059 csi_reg_write(mx3_cam, sens_conf | dw, CSI_SENS_CONF);
1060
Guennadi Liakhovetski0166b742009-08-25 11:47:00 -03001061 dev_dbg(dev, "Set SENS_CONF to %x\n", sens_conf | dw);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001062
1063 return 0;
1064}
1065
1066static struct soc_camera_host_ops mx3_soc_camera_host_ops = {
1067 .owner = THIS_MODULE,
1068 .add = mx3_camera_add_device,
1069 .remove = mx3_camera_remove_device,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001070 .set_crop = mx3_camera_set_crop,
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001071 .set_fmt = mx3_camera_set_fmt,
1072 .try_fmt = mx3_camera_try_fmt,
1073 .get_formats = mx3_camera_get_formats,
1074 .init_videobuf = mx3_camera_init_videobuf,
1075 .reqbufs = mx3_camera_reqbufs,
1076 .poll = mx3_camera_poll,
1077 .querycap = mx3_camera_querycap,
1078 .set_bus_param = mx3_camera_set_bus_param,
1079};
1080
Jean Delvaree36bc312009-06-04 11:07:16 -03001081static int __devinit mx3_camera_probe(struct platform_device *pdev)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001082{
1083 struct mx3_camera_dev *mx3_cam;
1084 struct resource *res;
1085 void __iomem *base;
1086 int err = 0;
1087 struct soc_camera_host *soc_host;
1088
1089 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1090 if (!res) {
1091 err = -ENODEV;
1092 goto egetres;
1093 }
1094
1095 mx3_cam = vmalloc(sizeof(*mx3_cam));
1096 if (!mx3_cam) {
1097 dev_err(&pdev->dev, "Could not allocate mx3 camera object\n");
1098 err = -ENOMEM;
1099 goto ealloc;
1100 }
1101 memset(mx3_cam, 0, sizeof(*mx3_cam));
1102
Guennadi Liakhovetskib71df972009-04-03 10:34:02 -03001103 mx3_cam->clk = clk_get(&pdev->dev, NULL);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001104 if (IS_ERR(mx3_cam->clk)) {
1105 err = PTR_ERR(mx3_cam->clk);
1106 goto eclkget;
1107 }
1108
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001109 mx3_cam->pdata = pdev->dev.platform_data;
1110 mx3_cam->platform_flags = mx3_cam->pdata->flags;
1111 if (!(mx3_cam->platform_flags & (MX3_CAMERA_DATAWIDTH_4 |
1112 MX3_CAMERA_DATAWIDTH_8 | MX3_CAMERA_DATAWIDTH_10 |
1113 MX3_CAMERA_DATAWIDTH_15))) {
1114 /* Platform hasn't set available data widths. This is bad.
1115 * Warn and use a default. */
1116 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1117 "data widths, using default 8 bit\n");
1118 mx3_cam->platform_flags |= MX3_CAMERA_DATAWIDTH_8;
1119 }
1120
1121 mx3_cam->mclk = mx3_cam->pdata->mclk_10khz * 10000;
1122 if (!mx3_cam->mclk) {
1123 dev_warn(&pdev->dev,
1124 "mclk_10khz == 0! Please, fix your platform data. "
1125 "Using default 20MHz\n");
1126 mx3_cam->mclk = 20000000;
1127 }
1128
1129 /* list of video-buffers */
1130 INIT_LIST_HEAD(&mx3_cam->capture);
1131 spin_lock_init(&mx3_cam->lock);
1132
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001133 base = ioremap(res->start, resource_size(res));
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001134 if (!base) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001135 pr_err("Couldn't map %x@%x\n", resource_size(res), res->start);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001136 err = -ENOMEM;
1137 goto eioremap;
1138 }
1139
1140 mx3_cam->base = base;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001141
1142 soc_host = &mx3_cam->soc_host;
1143 soc_host->drv_name = MX3_CAM_DRV_NAME;
1144 soc_host->ops = &mx3_soc_camera_host_ops;
1145 soc_host->priv = mx3_cam;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001146 soc_host->v4l2_dev.dev = &pdev->dev;
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001147 soc_host->nr = pdev->id;
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001148
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001149 err = soc_camera_host_register(soc_host);
1150 if (err)
1151 goto ecamhostreg;
1152
1153 /* IDMAC interface */
1154 dmaengine_get();
1155
1156 return 0;
1157
1158ecamhostreg:
1159 iounmap(base);
1160eioremap:
1161 clk_put(mx3_cam->clk);
1162eclkget:
1163 vfree(mx3_cam);
1164ealloc:
1165egetres:
1166 return err;
1167}
1168
1169static int __devexit mx3_camera_remove(struct platform_device *pdev)
1170{
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001171 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1172 struct mx3_camera_dev *mx3_cam = container_of(soc_host,
1173 struct mx3_camera_dev, soc_host);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001174
1175 clk_put(mx3_cam->clk);
1176
Guennadi Liakhovetskieff505f2009-04-24 12:55:48 -03001177 soc_camera_host_unregister(soc_host);
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001178
1179 iounmap(mx3_cam->base);
1180
1181 /*
1182 * The channel has either not been allocated,
1183 * or should have been released
1184 */
1185 if (WARN_ON(mx3_cam->idmac_channel[0]))
1186 dma_release_channel(&mx3_cam->idmac_channel[0]->dma_chan);
1187
1188 vfree(mx3_cam);
1189
1190 dmaengine_put();
1191
1192 dev_info(&pdev->dev, "i.MX3x Camera driver unloaded\n");
1193
1194 return 0;
1195}
1196
1197static struct platform_driver mx3_camera_driver = {
1198 .driver = {
1199 .name = MX3_CAM_DRV_NAME,
1200 },
1201 .probe = mx3_camera_probe,
Jean Delvaree36bc312009-06-04 11:07:16 -03001202 .remove = __devexit_p(mx3_camera_remove),
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001203};
1204
1205
Jean Delvaree36bc312009-06-04 11:07:16 -03001206static int __init mx3_camera_init(void)
Guennadi Liakhovetski4f671302009-02-23 12:13:24 -03001207{
1208 return platform_driver_register(&mx3_camera_driver);
1209}
1210
1211static void __exit mx3_camera_exit(void)
1212{
1213 platform_driver_unregister(&mx3_camera_driver);
1214}
1215
1216module_init(mx3_camera_init);
1217module_exit(mx3_camera_exit);
1218
1219MODULE_DESCRIPTION("i.MX3x SoC Camera Host driver");
1220MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
1221MODULE_LICENSE("GPL v2");
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001222MODULE_ALIAS("platform:" MX3_CAM_DRV_NAME);