blob: c9e080a57a602802879d9f835f7eabebab01603e [file] [log] [blame]
Josh Wu195ebc42011-06-07 22:40:19 -03001/*
2 * Copyright (c) 2011 Atmel Corporation
3 * Josh Wu, <josh.wu@atmel.com>
4 *
5 * Based on previous work by Lars Haring, <lars.haring@atmel.com>
6 * and Sedji Gaouaou
7 * Based on the bttv driver for Bt848 with respective copyright holders
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/clk.h>
15#include <linux/completion.h>
16#include <linux/delay.h>
17#include <linux/fs.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/slab.h>
24
25#include <media/atmel-isi.h>
26#include <media/soc_camera.h>
27#include <media/soc_mediabus.h>
28#include <media/videobuf2-dma-contig.h>
29
30#define MAX_BUFFER_NUM 32
31#define MAX_SUPPORT_WIDTH 2048
32#define MAX_SUPPORT_HEIGHT 2048
33#define VID_LIMIT_BYTES (16 * 1024 * 1024)
34#define MIN_FRAME_RATE 15
35#define FRAME_INTERVAL_MILLI_SEC (1000 / MIN_FRAME_RATE)
36
37/* ISI states */
38enum {
39 ISI_STATE_IDLE = 0,
40 ISI_STATE_READY,
41 ISI_STATE_WAIT_SOF,
42};
43
44/* Frame buffer descriptor */
45struct fbd {
46 /* Physical address of the frame buffer */
47 u32 fb_address;
48 /* DMA Control Register(only in HISI2) */
49 u32 dma_ctrl;
50 /* Physical address of the next fbd */
51 u32 next_fbd_address;
52};
53
54static void set_dma_ctrl(struct fbd *fb_desc, u32 ctrl)
55{
56 fb_desc->dma_ctrl = ctrl;
57}
58
59struct isi_dma_desc {
60 struct list_head list;
61 struct fbd *p_fbd;
62 u32 fbd_phys;
63};
64
65/* Frame buffer data */
66struct frame_buffer {
67 struct vb2_buffer vb;
68 struct isi_dma_desc *p_dma_desc;
69 struct list_head list;
70};
71
72struct atmel_isi {
73 /* Protects the access of variables shared with the ISR */
74 spinlock_t lock;
75 void __iomem *regs;
76
77 int sequence;
78 /* State of the ISI module in capturing mode */
79 int state;
80
81 /* Wait queue for waiting for SOF */
82 wait_queue_head_t vsync_wq;
83
84 struct vb2_alloc_ctx *alloc_ctx;
85
86 /* Allocate descriptors for dma buffer use */
87 struct fbd *p_fb_descriptors;
88 u32 fb_descriptors_phys;
89 struct list_head dma_desc_head;
90 struct isi_dma_desc dma_desc[MAX_BUFFER_NUM];
91
92 struct completion complete;
Josh Wud8ec0962011-12-08 07:18:49 -030093 /* ISI peripherial clock */
Josh Wu195ebc42011-06-07 22:40:19 -030094 struct clk *pclk;
Josh Wud8ec0962011-12-08 07:18:49 -030095 /* ISI_MCK, feed to camera sensor to generate pixel clock */
96 struct clk *mck;
Josh Wu195ebc42011-06-07 22:40:19 -030097 unsigned int irq;
98
99 struct isi_platform_data *pdata;
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300100 u16 width_flags; /* max 12 bits */
Josh Wu195ebc42011-06-07 22:40:19 -0300101
102 struct list_head video_buffer_list;
103 struct frame_buffer *active;
104
Josh Wu195ebc42011-06-07 22:40:19 -0300105 struct soc_camera_host soc_host;
106};
107
108static void isi_writel(struct atmel_isi *isi, u32 reg, u32 val)
109{
110 writel(val, isi->regs + reg);
111}
112static u32 isi_readl(struct atmel_isi *isi, u32 reg)
113{
114 return readl(isi->regs + reg);
115}
116
117static int configure_geometry(struct atmel_isi *isi, u32 width,
118 u32 height, enum v4l2_mbus_pixelcode code)
119{
120 u32 cfg2, cr;
121
122 switch (code) {
123 /* YUV, including grey */
124 case V4L2_MBUS_FMT_Y8_1X8:
125 cr = ISI_CFG2_GRAYSCALE;
126 break;
127 case V4L2_MBUS_FMT_UYVY8_2X8:
128 cr = ISI_CFG2_YCC_SWAP_MODE_3;
129 break;
130 case V4L2_MBUS_FMT_VYUY8_2X8:
131 cr = ISI_CFG2_YCC_SWAP_MODE_2;
132 break;
133 case V4L2_MBUS_FMT_YUYV8_2X8:
134 cr = ISI_CFG2_YCC_SWAP_MODE_1;
135 break;
136 case V4L2_MBUS_FMT_YVYU8_2X8:
137 cr = ISI_CFG2_YCC_SWAP_DEFAULT;
138 break;
139 /* RGB, TODO */
140 default:
141 return -EINVAL;
142 }
143
144 isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
145
146 cfg2 = isi_readl(isi, ISI_CFG2);
147 cfg2 |= cr;
148 /* Set width */
149 cfg2 &= ~(ISI_CFG2_IM_HSIZE_MASK);
150 cfg2 |= ((width - 1) << ISI_CFG2_IM_HSIZE_OFFSET) &
151 ISI_CFG2_IM_HSIZE_MASK;
152 /* Set height */
153 cfg2 &= ~(ISI_CFG2_IM_VSIZE_MASK);
154 cfg2 |= ((height - 1) << ISI_CFG2_IM_VSIZE_OFFSET)
155 & ISI_CFG2_IM_VSIZE_MASK;
156 isi_writel(isi, ISI_CFG2, cfg2);
157
158 return 0;
159}
160
161static irqreturn_t atmel_isi_handle_streaming(struct atmel_isi *isi)
162{
163 if (isi->active) {
164 struct vb2_buffer *vb = &isi->active->vb;
165 struct frame_buffer *buf = isi->active;
166
167 list_del_init(&buf->list);
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300168 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
Josh Wu195ebc42011-06-07 22:40:19 -0300169 vb->v4l2_buf.sequence = isi->sequence++;
170 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
171 }
172
173 if (list_empty(&isi->video_buffer_list)) {
174 isi->active = NULL;
175 } else {
176 /* start next dma frame. */
177 isi->active = list_entry(isi->video_buffer_list.next,
178 struct frame_buffer, list);
179 isi_writel(isi, ISI_DMA_C_DSCR,
180 isi->active->p_dma_desc->fbd_phys);
181 isi_writel(isi, ISI_DMA_C_CTRL,
182 ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
183 isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
184 }
185 return IRQ_HANDLED;
186}
187
188/* ISI interrupt service routine */
189static irqreturn_t isi_interrupt(int irq, void *dev_id)
190{
191 struct atmel_isi *isi = dev_id;
192 u32 status, mask, pending;
193 irqreturn_t ret = IRQ_NONE;
194
195 spin_lock(&isi->lock);
196
197 status = isi_readl(isi, ISI_STATUS);
198 mask = isi_readl(isi, ISI_INTMASK);
199 pending = status & mask;
200
201 if (pending & ISI_CTRL_SRST) {
202 complete(&isi->complete);
203 isi_writel(isi, ISI_INTDIS, ISI_CTRL_SRST);
204 ret = IRQ_HANDLED;
205 } else if (pending & ISI_CTRL_DIS) {
206 complete(&isi->complete);
207 isi_writel(isi, ISI_INTDIS, ISI_CTRL_DIS);
208 ret = IRQ_HANDLED;
209 } else {
210 if ((pending & ISI_SR_VSYNC) &&
211 (isi->state == ISI_STATE_IDLE)) {
212 isi->state = ISI_STATE_READY;
213 wake_up_interruptible(&isi->vsync_wq);
214 ret = IRQ_HANDLED;
215 }
216 if (likely(pending & ISI_SR_CXFR_DONE))
217 ret = atmel_isi_handle_streaming(isi);
218 }
219
220 spin_unlock(&isi->lock);
221 return ret;
222}
223
224#define WAIT_ISI_RESET 1
225#define WAIT_ISI_DISABLE 0
226static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset)
227{
228 unsigned long timeout;
229 /*
230 * The reset or disable will only succeed if we have a
231 * pixel clock from the camera.
232 */
233 init_completion(&isi->complete);
234
235 if (wait_reset) {
236 isi_writel(isi, ISI_INTEN, ISI_CTRL_SRST);
237 isi_writel(isi, ISI_CTRL, ISI_CTRL_SRST);
238 } else {
239 isi_writel(isi, ISI_INTEN, ISI_CTRL_DIS);
240 isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
241 }
242
243 timeout = wait_for_completion_timeout(&isi->complete,
244 msecs_to_jiffies(100));
245 if (timeout == 0)
246 return -ETIMEDOUT;
247
248 return 0;
249}
250
251/* ------------------------------------------------------------------
252 Videobuf operations
253 ------------------------------------------------------------------*/
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300254static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
255 unsigned int *nbuffers, unsigned int *nplanes,
256 unsigned int sizes[], void *alloc_ctxs[])
Josh Wu195ebc42011-06-07 22:40:19 -0300257{
258 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300259 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300260 struct atmel_isi *isi = ici->priv;
261 unsigned long size;
Laurent Pinchart2b61d462012-03-21 08:03:21 -0300262 int ret;
Josh Wu195ebc42011-06-07 22:40:19 -0300263
264 /* Reset ISI */
265 ret = atmel_isi_wait_status(isi, WAIT_ISI_RESET);
266 if (ret < 0) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300267 dev_err(icd->parent, "Reset ISI timed out\n");
Josh Wu195ebc42011-06-07 22:40:19 -0300268 return ret;
269 }
270 /* Disable all interrupts */
271 isi_writel(isi, ISI_INTDIS, ~0UL);
272
Laurent Pinchart2b61d462012-03-21 08:03:21 -0300273 size = icd->sizeimage;
Josh Wu195ebc42011-06-07 22:40:19 -0300274
275 if (!*nbuffers || *nbuffers > MAX_BUFFER_NUM)
276 *nbuffers = MAX_BUFFER_NUM;
277
278 if (size * *nbuffers > VID_LIMIT_BYTES)
279 *nbuffers = VID_LIMIT_BYTES / size;
280
281 *nplanes = 1;
282 sizes[0] = size;
283 alloc_ctxs[0] = isi->alloc_ctx;
284
285 isi->sequence = 0;
286 isi->active = NULL;
287
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300288 dev_dbg(icd->parent, "%s, count=%d, size=%ld\n", __func__,
Josh Wu195ebc42011-06-07 22:40:19 -0300289 *nbuffers, size);
290
291 return 0;
292}
293
294static int buffer_init(struct vb2_buffer *vb)
295{
296 struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
297
298 buf->p_dma_desc = NULL;
299 INIT_LIST_HEAD(&buf->list);
300
301 return 0;
302}
303
304static int buffer_prepare(struct vb2_buffer *vb)
305{
306 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
307 struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300308 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300309 struct atmel_isi *isi = ici->priv;
310 unsigned long size;
311 struct isi_dma_desc *desc;
Josh Wu195ebc42011-06-07 22:40:19 -0300312
Laurent Pinchart2b61d462012-03-21 08:03:21 -0300313 size = icd->sizeimage;
Josh Wu195ebc42011-06-07 22:40:19 -0300314
315 if (vb2_plane_size(vb, 0) < size) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300316 dev_err(icd->parent, "%s data will not fit into plane (%lu < %lu)\n",
Josh Wu195ebc42011-06-07 22:40:19 -0300317 __func__, vb2_plane_size(vb, 0), size);
318 return -EINVAL;
319 }
320
321 vb2_set_plane_payload(&buf->vb, 0, size);
322
323 if (!buf->p_dma_desc) {
324 if (list_empty(&isi->dma_desc_head)) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300325 dev_err(icd->parent, "Not enough dma descriptors.\n");
Josh Wu195ebc42011-06-07 22:40:19 -0300326 return -EINVAL;
327 } else {
328 /* Get an available descriptor */
329 desc = list_entry(isi->dma_desc_head.next,
330 struct isi_dma_desc, list);
331 /* Delete the descriptor since now it is used */
332 list_del_init(&desc->list);
333
334 /* Initialize the dma descriptor */
335 desc->p_fbd->fb_address =
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300336 vb2_dma_contig_plane_dma_addr(vb, 0);
Josh Wu195ebc42011-06-07 22:40:19 -0300337 desc->p_fbd->next_fbd_address = 0;
338 set_dma_ctrl(desc->p_fbd, ISI_DMA_CTRL_WB);
339
340 buf->p_dma_desc = desc;
341 }
342 }
343 return 0;
344}
345
346static void buffer_cleanup(struct vb2_buffer *vb)
347{
348 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300349 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300350 struct atmel_isi *isi = ici->priv;
351 struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
352
353 /* This descriptor is available now and we add to head list */
354 if (buf->p_dma_desc)
355 list_add(&buf->p_dma_desc->list, &isi->dma_desc_head);
356}
357
358static void start_dma(struct atmel_isi *isi, struct frame_buffer *buffer)
359{
360 u32 ctrl, cfg1;
361
362 cfg1 = isi_readl(isi, ISI_CFG1);
363 /* Enable irq: cxfr for the codec path, pxfr for the preview path */
364 isi_writel(isi, ISI_INTEN,
365 ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
366
367 /* Check if already in a frame */
368 if (isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) {
Guennadi Liakhovetskif7f6ce22013-04-04 08:21:12 -0300369 dev_err(isi->soc_host.icd->parent, "Already in frame handling.\n");
Josh Wu195ebc42011-06-07 22:40:19 -0300370 return;
371 }
372
373 isi_writel(isi, ISI_DMA_C_DSCR, buffer->p_dma_desc->fbd_phys);
374 isi_writel(isi, ISI_DMA_C_CTRL, ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
375 isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
376
377 /* Enable linked list */
378 cfg1 |= isi->pdata->frate | ISI_CFG1_DISCR;
379
380 /* Enable codec path and ISI */
381 ctrl = ISI_CTRL_CDC | ISI_CTRL_EN;
382 isi_writel(isi, ISI_CTRL, ctrl);
383 isi_writel(isi, ISI_CFG1, cfg1);
384}
385
386static void buffer_queue(struct vb2_buffer *vb)
387{
388 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300389 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300390 struct atmel_isi *isi = ici->priv;
391 struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
392 unsigned long flags = 0;
393
394 spin_lock_irqsave(&isi->lock, flags);
395 list_add_tail(&buf->list, &isi->video_buffer_list);
396
397 if (isi->active == NULL) {
398 isi->active = buf;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300399 if (vb2_is_streaming(vb->vb2_queue))
400 start_dma(isi, buf);
Josh Wu195ebc42011-06-07 22:40:19 -0300401 }
402 spin_unlock_irqrestore(&isi->lock, flags);
403}
404
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300405static int start_streaming(struct vb2_queue *vq, unsigned int count)
Josh Wu195ebc42011-06-07 22:40:19 -0300406{
407 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300408 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300409 struct atmel_isi *isi = ici->priv;
410
411 u32 sr = 0;
412 int ret;
413
414 spin_lock_irq(&isi->lock);
415 isi->state = ISI_STATE_IDLE;
416 /* Clear any pending SOF interrupt */
417 sr = isi_readl(isi, ISI_STATUS);
418 /* Enable VSYNC interrupt for SOF */
419 isi_writel(isi, ISI_INTEN, ISI_SR_VSYNC);
420 isi_writel(isi, ISI_CTRL, ISI_CTRL_EN);
421 spin_unlock_irq(&isi->lock);
422
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300423 dev_dbg(icd->parent, "Waiting for SOF\n");
Josh Wu195ebc42011-06-07 22:40:19 -0300424 ret = wait_event_interruptible(isi->vsync_wq,
425 isi->state != ISI_STATE_IDLE);
426 if (ret)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300427 goto err;
Josh Wu195ebc42011-06-07 22:40:19 -0300428
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300429 if (isi->state != ISI_STATE_READY) {
430 ret = -EIO;
431 goto err;
432 }
Josh Wu195ebc42011-06-07 22:40:19 -0300433
434 spin_lock_irq(&isi->lock);
435 isi->state = ISI_STATE_WAIT_SOF;
436 isi_writel(isi, ISI_INTDIS, ISI_SR_VSYNC);
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300437 if (count)
438 start_dma(isi, isi->active);
Josh Wu195ebc42011-06-07 22:40:19 -0300439 spin_unlock_irq(&isi->lock);
440
441 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300442err:
443 isi->active = NULL;
444 isi->sequence = 0;
445 INIT_LIST_HEAD(&isi->video_buffer_list);
446 return ret;
Josh Wu195ebc42011-06-07 22:40:19 -0300447}
448
449/* abort streaming and wait for last buffer */
450static int stop_streaming(struct vb2_queue *vq)
451{
452 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300453 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300454 struct atmel_isi *isi = ici->priv;
455 struct frame_buffer *buf, *node;
456 int ret = 0;
457 unsigned long timeout;
458
459 spin_lock_irq(&isi->lock);
460 isi->active = NULL;
461 /* Release all active buffers */
462 list_for_each_entry_safe(buf, node, &isi->video_buffer_list, list) {
463 list_del_init(&buf->list);
464 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
465 }
466 spin_unlock_irq(&isi->lock);
467
468 timeout = jiffies + FRAME_INTERVAL_MILLI_SEC * HZ;
469 /* Wait until the end of the current frame. */
470 while ((isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) &&
471 time_before(jiffies, timeout))
472 msleep(1);
473
474 if (time_after(jiffies, timeout)) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300475 dev_err(icd->parent,
Josh Wu195ebc42011-06-07 22:40:19 -0300476 "Timeout waiting for finishing codec request\n");
477 return -ETIMEDOUT;
478 }
479
480 /* Disable interrupts */
481 isi_writel(isi, ISI_INTDIS,
482 ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
483
484 /* Disable ISI and wait for it is done */
485 ret = atmel_isi_wait_status(isi, WAIT_ISI_DISABLE);
486 if (ret < 0)
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300487 dev_err(icd->parent, "Disable ISI timed out\n");
Josh Wu195ebc42011-06-07 22:40:19 -0300488
489 return ret;
490}
491
492static struct vb2_ops isi_video_qops = {
493 .queue_setup = queue_setup,
494 .buf_init = buffer_init,
495 .buf_prepare = buffer_prepare,
496 .buf_cleanup = buffer_cleanup,
497 .buf_queue = buffer_queue,
498 .start_streaming = start_streaming,
499 .stop_streaming = stop_streaming,
500 .wait_prepare = soc_camera_unlock,
501 .wait_finish = soc_camera_lock,
502};
503
504/* ------------------------------------------------------------------
505 SOC camera operations for the device
506 ------------------------------------------------------------------*/
507static int isi_camera_init_videobuf(struct vb2_queue *q,
508 struct soc_camera_device *icd)
509{
510 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
511 q->io_modes = VB2_MMAP;
512 q->drv_priv = icd;
513 q->buf_struct_size = sizeof(struct frame_buffer);
514 q->ops = &isi_video_qops;
515 q->mem_ops = &vb2_dma_contig_memops;
Kamil Debski6aa69f92013-01-25 06:29:57 -0300516 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Josh Wu195ebc42011-06-07 22:40:19 -0300517
518 return vb2_queue_init(q);
519}
520
521static int isi_camera_set_fmt(struct soc_camera_device *icd,
522 struct v4l2_format *f)
523{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300524 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300525 struct atmel_isi *isi = ici->priv;
526 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
527 const struct soc_camera_format_xlate *xlate;
528 struct v4l2_pix_format *pix = &f->fmt.pix;
529 struct v4l2_mbus_framefmt mf;
530 int ret;
531
532 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
533 if (!xlate) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300534 dev_warn(icd->parent, "Format %x not found\n",
Josh Wu195ebc42011-06-07 22:40:19 -0300535 pix->pixelformat);
536 return -EINVAL;
537 }
538
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300539 dev_dbg(icd->parent, "Plan to set format %dx%d\n",
Josh Wu195ebc42011-06-07 22:40:19 -0300540 pix->width, pix->height);
541
542 mf.width = pix->width;
543 mf.height = pix->height;
544 mf.field = pix->field;
545 mf.colorspace = pix->colorspace;
546 mf.code = xlate->code;
547
548 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
549 if (ret < 0)
550 return ret;
551
552 if (mf.code != xlate->code)
553 return -EINVAL;
554
555 ret = configure_geometry(isi, pix->width, pix->height, xlate->code);
556 if (ret < 0)
557 return ret;
558
559 pix->width = mf.width;
560 pix->height = mf.height;
561 pix->field = mf.field;
562 pix->colorspace = mf.colorspace;
563 icd->current_fmt = xlate;
564
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300565 dev_dbg(icd->parent, "Finally set format %dx%d\n",
Josh Wu195ebc42011-06-07 22:40:19 -0300566 pix->width, pix->height);
567
568 return ret;
569}
570
571static int isi_camera_try_fmt(struct soc_camera_device *icd,
572 struct v4l2_format *f)
573{
574 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
575 const struct soc_camera_format_xlate *xlate;
576 struct v4l2_pix_format *pix = &f->fmt.pix;
577 struct v4l2_mbus_framefmt mf;
578 u32 pixfmt = pix->pixelformat;
579 int ret;
580
581 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
582 if (pixfmt && !xlate) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300583 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
Josh Wu195ebc42011-06-07 22:40:19 -0300584 return -EINVAL;
585 }
586
587 /* limit to Atmel ISI hardware capabilities */
588 if (pix->height > MAX_SUPPORT_HEIGHT)
589 pix->height = MAX_SUPPORT_HEIGHT;
590 if (pix->width > MAX_SUPPORT_WIDTH)
591 pix->width = MAX_SUPPORT_WIDTH;
592
593 /* limit to sensor capabilities */
594 mf.width = pix->width;
595 mf.height = pix->height;
596 mf.field = pix->field;
597 mf.colorspace = pix->colorspace;
598 mf.code = xlate->code;
599
600 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
601 if (ret < 0)
602 return ret;
603
604 pix->width = mf.width;
605 pix->height = mf.height;
606 pix->colorspace = mf.colorspace;
607
608 switch (mf.field) {
609 case V4L2_FIELD_ANY:
610 pix->field = V4L2_FIELD_NONE;
611 break;
612 case V4L2_FIELD_NONE:
613 break;
614 default:
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300615 dev_err(icd->parent, "Field type %d unsupported.\n",
Josh Wu195ebc42011-06-07 22:40:19 -0300616 mf.field);
617 ret = -EINVAL;
618 }
619
620 return ret;
621}
622
623static const struct soc_mbus_pixelfmt isi_camera_formats[] = {
624 {
625 .fourcc = V4L2_PIX_FMT_YUYV,
626 .name = "Packed YUV422 16 bit",
627 .bits_per_sample = 8,
628 .packing = SOC_MBUS_PACKING_2X8_PADHI,
629 .order = SOC_MBUS_ORDER_LE,
Laurent Pinchartad3b81f2012-03-21 08:03:23 -0300630 .layout = SOC_MBUS_LAYOUT_PACKED,
Josh Wu195ebc42011-06-07 22:40:19 -0300631 },
632};
633
634/* This will be corrected as we get more formats */
635static bool isi_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
636{
637 return fmt->packing == SOC_MBUS_PACKING_NONE ||
638 (fmt->bits_per_sample == 8 &&
639 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
640 (fmt->bits_per_sample > 8 &&
641 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
642}
643
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300644#define ISI_BUS_PARAM (V4L2_MBUS_MASTER | \
645 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
646 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
647 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
648 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
649 V4L2_MBUS_PCLK_SAMPLE_RISING | \
650 V4L2_MBUS_PCLK_SAMPLE_FALLING | \
651 V4L2_MBUS_DATA_ACTIVE_HIGH)
Josh Wu195ebc42011-06-07 22:40:19 -0300652
653static int isi_camera_try_bus_param(struct soc_camera_device *icd,
654 unsigned char buswidth)
655{
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300656 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300657 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300658 struct atmel_isi *isi = ici->priv;
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300659 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
660 unsigned long common_flags;
Josh Wu195ebc42011-06-07 22:40:19 -0300661 int ret;
662
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300663 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
664 if (!ret) {
665 common_flags = soc_mbus_config_compatible(&cfg,
666 ISI_BUS_PARAM);
667 if (!common_flags) {
668 dev_warn(icd->parent,
669 "Flags incompatible: camera 0x%x, host 0x%x\n",
670 cfg.flags, ISI_BUS_PARAM);
671 return -EINVAL;
672 }
673 } else if (ret != -ENOIOCTLCMD) {
674 return ret;
675 }
676
677 if ((1 << (buswidth - 1)) & isi->width_flags)
678 return 0;
679 return -EINVAL;
Josh Wu195ebc42011-06-07 22:40:19 -0300680}
681
682
683static int isi_camera_get_formats(struct soc_camera_device *icd,
684 unsigned int idx,
685 struct soc_camera_format_xlate *xlate)
686{
687 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
688 int formats = 0, ret;
689 /* sensor format */
690 enum v4l2_mbus_pixelcode code;
691 /* soc camera host format */
692 const struct soc_mbus_pixelfmt *fmt;
693
694 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
695 if (ret < 0)
696 /* No more formats */
697 return 0;
698
699 fmt = soc_mbus_get_fmtdesc(code);
700 if (!fmt) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300701 dev_err(icd->parent,
Josh Wu195ebc42011-06-07 22:40:19 -0300702 "Invalid format code #%u: %d\n", idx, code);
703 return 0;
704 }
705
706 /* This also checks support for the requested bits-per-sample */
707 ret = isi_camera_try_bus_param(icd, fmt->bits_per_sample);
708 if (ret < 0) {
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300709 dev_err(icd->parent,
Josh Wu195ebc42011-06-07 22:40:19 -0300710 "Fail to try the bus parameters.\n");
711 return 0;
712 }
713
714 switch (code) {
715 case V4L2_MBUS_FMT_UYVY8_2X8:
716 case V4L2_MBUS_FMT_VYUY8_2X8:
717 case V4L2_MBUS_FMT_YUYV8_2X8:
718 case V4L2_MBUS_FMT_YVYU8_2X8:
719 formats++;
720 if (xlate) {
721 xlate->host_fmt = &isi_camera_formats[0];
722 xlate->code = code;
723 xlate++;
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300724 dev_dbg(icd->parent, "Providing format %s using code %d\n",
Josh Wu195ebc42011-06-07 22:40:19 -0300725 isi_camera_formats[0].name, code);
726 }
727 break;
728 default:
729 if (!isi_camera_packing_supported(fmt))
730 return 0;
731 if (xlate)
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300732 dev_dbg(icd->parent,
Josh Wu195ebc42011-06-07 22:40:19 -0300733 "Providing format %s in pass-through mode\n",
734 fmt->name);
735 }
736
737 /* Generic pass-through */
738 formats++;
739 if (xlate) {
740 xlate->host_fmt = fmt;
741 xlate->code = code;
742 xlate++;
743 }
744
745 return formats;
746}
747
Guennadi Liakhovetskidd669e92012-12-24 09:31:33 -0300748/* Called with .host_lock held */
Josh Wu195ebc42011-06-07 22:40:19 -0300749static int isi_camera_add_device(struct soc_camera_device *icd)
750{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300751 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300752 struct atmel_isi *isi = ici->priv;
753 int ret;
754
Josh Wu195ebc42011-06-07 22:40:19 -0300755 ret = clk_enable(isi->pclk);
756 if (ret)
757 return ret;
758
Josh Wud8ec0962011-12-08 07:18:49 -0300759 ret = clk_enable(isi->mck);
760 if (ret) {
761 clk_disable(isi->pclk);
762 return ret;
763 }
764
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300765 dev_dbg(icd->parent, "Atmel ISI Camera driver attached to camera %d\n",
Josh Wu195ebc42011-06-07 22:40:19 -0300766 icd->devnum);
767 return 0;
768}
Guennadi Liakhovetskidd669e92012-12-24 09:31:33 -0300769/* Called with .host_lock held */
Josh Wu195ebc42011-06-07 22:40:19 -0300770static void isi_camera_remove_device(struct soc_camera_device *icd)
771{
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300772 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300773 struct atmel_isi *isi = ici->priv;
774
Josh Wud8ec0962011-12-08 07:18:49 -0300775 clk_disable(isi->mck);
Josh Wu195ebc42011-06-07 22:40:19 -0300776 clk_disable(isi->pclk);
Josh Wu195ebc42011-06-07 22:40:19 -0300777
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300778 dev_dbg(icd->parent, "Atmel ISI Camera driver detached from camera %d\n",
Josh Wu195ebc42011-06-07 22:40:19 -0300779 icd->devnum);
780}
781
782static unsigned int isi_camera_poll(struct file *file, poll_table *pt)
783{
784 struct soc_camera_device *icd = file->private_data;
785
786 return vb2_poll(&icd->vb2_vidq, file, pt);
787}
788
789static int isi_camera_querycap(struct soc_camera_host *ici,
790 struct v4l2_capability *cap)
791{
792 strcpy(cap->driver, "atmel-isi");
793 strcpy(cap->card, "Atmel Image Sensor Interface");
794 cap->capabilities = (V4L2_CAP_VIDEO_CAPTURE |
795 V4L2_CAP_STREAMING);
796 return 0;
797}
798
Guennadi Liakhovetski8843d112011-09-21 17:52:51 -0300799static int isi_camera_set_bus_param(struct soc_camera_device *icd)
Josh Wu195ebc42011-06-07 22:40:19 -0300800{
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300801 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300802 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
Josh Wu195ebc42011-06-07 22:40:19 -0300803 struct atmel_isi *isi = ici->priv;
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300804 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
805 unsigned long common_flags;
Josh Wu195ebc42011-06-07 22:40:19 -0300806 int ret;
807 u32 cfg1 = 0;
808
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300809 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
810 if (!ret) {
811 common_flags = soc_mbus_config_compatible(&cfg,
812 ISI_BUS_PARAM);
813 if (!common_flags) {
814 dev_warn(icd->parent,
815 "Flags incompatible: camera 0x%x, host 0x%x\n",
816 cfg.flags, ISI_BUS_PARAM);
817 return -EINVAL;
818 }
819 } else if (ret != -ENOIOCTLCMD) {
820 return ret;
821 } else {
822 common_flags = ISI_BUS_PARAM;
823 }
824 dev_dbg(icd->parent, "Flags cam: 0x%x host: 0x%x common: 0x%lx\n",
825 cfg.flags, ISI_BUS_PARAM, common_flags);
Josh Wu195ebc42011-06-07 22:40:19 -0300826
827 /* Make choises, based on platform preferences */
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300828 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
829 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
Josh Wu195ebc42011-06-07 22:40:19 -0300830 if (isi->pdata->hsync_act_low)
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300831 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
Josh Wu195ebc42011-06-07 22:40:19 -0300832 else
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300833 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
Josh Wu195ebc42011-06-07 22:40:19 -0300834 }
835
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300836 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
837 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
Josh Wu195ebc42011-06-07 22:40:19 -0300838 if (isi->pdata->vsync_act_low)
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300839 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
Josh Wu195ebc42011-06-07 22:40:19 -0300840 else
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300841 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
Josh Wu195ebc42011-06-07 22:40:19 -0300842 }
843
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300844 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
845 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
Josh Wu195ebc42011-06-07 22:40:19 -0300846 if (isi->pdata->pclk_act_falling)
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300847 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
Josh Wu195ebc42011-06-07 22:40:19 -0300848 else
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300849 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
Josh Wu195ebc42011-06-07 22:40:19 -0300850 }
851
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300852 cfg.flags = common_flags;
853 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
854 if (ret < 0 && ret != -ENOIOCTLCMD) {
855 dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
Josh Wu195ebc42011-06-07 22:40:19 -0300856 common_flags, ret);
857 return ret;
858 }
859
860 /* set bus param for ISI */
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300861 if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
Josh Wu195ebc42011-06-07 22:40:19 -0300862 cfg1 |= ISI_CFG1_HSYNC_POL_ACTIVE_LOW;
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300863 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
Josh Wu195ebc42011-06-07 22:40:19 -0300864 cfg1 |= ISI_CFG1_VSYNC_POL_ACTIVE_LOW;
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -0300865 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
Josh Wu195ebc42011-06-07 22:40:19 -0300866 cfg1 |= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING;
867
868 if (isi->pdata->has_emb_sync)
869 cfg1 |= ISI_CFG1_EMB_SYNC;
Josh Wud8ec0962011-12-08 07:18:49 -0300870 if (isi->pdata->full_mode)
Josh Wu195ebc42011-06-07 22:40:19 -0300871 cfg1 |= ISI_CFG1_FULL_MODE;
872
873 isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
874 isi_writel(isi, ISI_CFG1, cfg1);
875
876 return 0;
877}
878
879static struct soc_camera_host_ops isi_soc_camera_host_ops = {
880 .owner = THIS_MODULE,
881 .add = isi_camera_add_device,
882 .remove = isi_camera_remove_device,
883 .set_fmt = isi_camera_set_fmt,
884 .try_fmt = isi_camera_try_fmt,
885 .get_formats = isi_camera_get_formats,
886 .init_videobuf2 = isi_camera_init_videobuf,
887 .poll = isi_camera_poll,
888 .querycap = isi_camera_querycap,
889 .set_bus_param = isi_camera_set_bus_param,
890};
891
892/* -----------------------------------------------------------------------*/
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800893static int atmel_isi_remove(struct platform_device *pdev)
Josh Wu195ebc42011-06-07 22:40:19 -0300894{
895 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
896 struct atmel_isi *isi = container_of(soc_host,
897 struct atmel_isi, soc_host);
898
899 free_irq(isi->irq, isi);
900 soc_camera_host_unregister(soc_host);
901 vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
902 dma_free_coherent(&pdev->dev,
903 sizeof(struct fbd) * MAX_BUFFER_NUM,
904 isi->p_fb_descriptors,
905 isi->fb_descriptors_phys);
906
907 iounmap(isi->regs);
Josh Wu03652e02012-01-11 00:58:29 -0300908 clk_unprepare(isi->mck);
Josh Wud8ec0962011-12-08 07:18:49 -0300909 clk_put(isi->mck);
Josh Wu03652e02012-01-11 00:58:29 -0300910 clk_unprepare(isi->pclk);
Josh Wu195ebc42011-06-07 22:40:19 -0300911 clk_put(isi->pclk);
912 kfree(isi);
913
914 return 0;
915}
916
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800917static int atmel_isi_probe(struct platform_device *pdev)
Josh Wu195ebc42011-06-07 22:40:19 -0300918{
919 unsigned int irq;
920 struct atmel_isi *isi;
921 struct clk *pclk;
922 struct resource *regs;
923 int ret, i;
924 struct device *dev = &pdev->dev;
925 struct soc_camera_host *soc_host;
926 struct isi_platform_data *pdata;
927
928 pdata = dev->platform_data;
Josh Wud8ec0962011-12-08 07:18:49 -0300929 if (!pdata || !pdata->data_width_flags || !pdata->mck_hz) {
Josh Wu195ebc42011-06-07 22:40:19 -0300930 dev_err(&pdev->dev,
931 "No config available for Atmel ISI\n");
932 return -EINVAL;
933 }
934
935 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
936 if (!regs)
937 return -ENXIO;
938
939 pclk = clk_get(&pdev->dev, "isi_clk");
940 if (IS_ERR(pclk))
941 return PTR_ERR(pclk);
942
Josh Wu03652e02012-01-11 00:58:29 -0300943 ret = clk_prepare(pclk);
944 if (ret)
945 goto err_clk_prepare_pclk;
946
Josh Wu195ebc42011-06-07 22:40:19 -0300947 isi = kzalloc(sizeof(struct atmel_isi), GFP_KERNEL);
948 if (!isi) {
949 ret = -ENOMEM;
950 dev_err(&pdev->dev, "Can't allocate interface!\n");
951 goto err_alloc_isi;
952 }
953
954 isi->pclk = pclk;
955 isi->pdata = pdata;
956 isi->active = NULL;
957 spin_lock_init(&isi->lock);
958 init_waitqueue_head(&isi->vsync_wq);
959 INIT_LIST_HEAD(&isi->video_buffer_list);
960 INIT_LIST_HEAD(&isi->dma_desc_head);
961
Josh Wud8ec0962011-12-08 07:18:49 -0300962 /* Get ISI_MCK, provided by programmable clock or external clock */
963 isi->mck = clk_get(dev, "isi_mck");
964 if (IS_ERR(isi->mck)) {
965 dev_err(dev, "Failed to get isi_mck\n");
966 ret = PTR_ERR(isi->mck);
967 goto err_clk_get;
968 }
969
Josh Wu03652e02012-01-11 00:58:29 -0300970 ret = clk_prepare(isi->mck);
971 if (ret)
972 goto err_clk_prepare_mck;
973
Josh Wud8ec0962011-12-08 07:18:49 -0300974 /* Set ISI_MCK's frequency, it should be faster than pixel clock */
975 ret = clk_set_rate(isi->mck, pdata->mck_hz);
976 if (ret < 0)
977 goto err_set_mck_rate;
978
Josh Wu195ebc42011-06-07 22:40:19 -0300979 isi->p_fb_descriptors = dma_alloc_coherent(&pdev->dev,
980 sizeof(struct fbd) * MAX_BUFFER_NUM,
981 &isi->fb_descriptors_phys,
982 GFP_KERNEL);
983 if (!isi->p_fb_descriptors) {
984 ret = -ENOMEM;
985 dev_err(&pdev->dev, "Can't allocate descriptors!\n");
986 goto err_alloc_descriptors;
987 }
988
989 for (i = 0; i < MAX_BUFFER_NUM; i++) {
990 isi->dma_desc[i].p_fbd = isi->p_fb_descriptors + i;
991 isi->dma_desc[i].fbd_phys = isi->fb_descriptors_phys +
992 i * sizeof(struct fbd);
993 list_add(&isi->dma_desc[i].list, &isi->dma_desc_head);
994 }
995
996 isi->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
997 if (IS_ERR(isi->alloc_ctx)) {
998 ret = PTR_ERR(isi->alloc_ctx);
999 goto err_alloc_ctx;
1000 }
1001
1002 isi->regs = ioremap(regs->start, resource_size(regs));
1003 if (!isi->regs) {
1004 ret = -ENOMEM;
1005 goto err_ioremap;
1006 }
1007
Guennadi Liakhovetskia4e9f102011-07-27 12:18:37 -03001008 if (pdata->data_width_flags & ISI_DATAWIDTH_8)
1009 isi->width_flags = 1 << 7;
1010 if (pdata->data_width_flags & ISI_DATAWIDTH_10)
1011 isi->width_flags |= 1 << 9;
1012
Josh Wu195ebc42011-06-07 22:40:19 -03001013 isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
1014
1015 irq = platform_get_irq(pdev, 0);
Tushar Beherae35abd42012-11-16 03:50:37 -03001016 if (IS_ERR_VALUE(irq)) {
Josh Wu195ebc42011-06-07 22:40:19 -03001017 ret = irq;
1018 goto err_req_irq;
1019 }
1020
1021 ret = request_irq(irq, isi_interrupt, 0, "isi", isi);
1022 if (ret) {
1023 dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
1024 goto err_req_irq;
1025 }
1026 isi->irq = irq;
1027
1028 soc_host = &isi->soc_host;
1029 soc_host->drv_name = "isi-camera";
1030 soc_host->ops = &isi_soc_camera_host_ops;
1031 soc_host->priv = isi;
1032 soc_host->v4l2_dev.dev = &pdev->dev;
1033 soc_host->nr = pdev->id;
1034
1035 ret = soc_camera_host_register(soc_host);
1036 if (ret) {
1037 dev_err(&pdev->dev, "Unable to register soc camera host\n");
1038 goto err_register_soc_camera_host;
1039 }
1040 return 0;
1041
1042err_register_soc_camera_host:
1043 free_irq(isi->irq, isi);
1044err_req_irq:
1045 iounmap(isi->regs);
1046err_ioremap:
1047 vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
1048err_alloc_ctx:
1049 dma_free_coherent(&pdev->dev,
1050 sizeof(struct fbd) * MAX_BUFFER_NUM,
1051 isi->p_fb_descriptors,
1052 isi->fb_descriptors_phys);
1053err_alloc_descriptors:
Josh Wud8ec0962011-12-08 07:18:49 -03001054err_set_mck_rate:
Josh Wu03652e02012-01-11 00:58:29 -03001055 clk_unprepare(isi->mck);
1056err_clk_prepare_mck:
Josh Wud8ec0962011-12-08 07:18:49 -03001057 clk_put(isi->mck);
1058err_clk_get:
Josh Wu195ebc42011-06-07 22:40:19 -03001059 kfree(isi);
1060err_alloc_isi:
Josh Wu03652e02012-01-11 00:58:29 -03001061 clk_unprepare(pclk);
1062err_clk_prepare_pclk:
Julia Lawall0a4524d2011-10-28 19:58:16 -03001063 clk_put(pclk);
Josh Wu195ebc42011-06-07 22:40:19 -03001064
1065 return ret;
1066}
1067
1068static struct platform_driver atmel_isi_driver = {
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001069 .remove = atmel_isi_remove,
Josh Wu195ebc42011-06-07 22:40:19 -03001070 .driver = {
1071 .name = "atmel_isi",
1072 .owner = THIS_MODULE,
1073 },
1074};
1075
Fabio Porcedda8c31aec2013-03-14 14:09:31 -03001076module_platform_driver_probe(atmel_isi_driver, atmel_isi_probe);
Josh Wu195ebc42011-06-07 22:40:19 -03001077
1078MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>");
1079MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux");
1080MODULE_LICENSE("GPL");
1081MODULE_SUPPORTED_DEVICE("video");