1 /*
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/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/of.h>
26 
27 #include <linux/videodev2.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-dev.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-event.h>
33 #include <media/v4l2-of.h>
34 #include <media/videobuf2-dma-contig.h>
35 #include <media/v4l2-image-sizes.h>
36 
37 #include "atmel-isi.h"
38 
39 #define MAX_SUPPORT_WIDTH		2048
40 #define MAX_SUPPORT_HEIGHT		2048
41 #define MIN_FRAME_RATE			15
42 #define FRAME_INTERVAL_MILLI_SEC	(1000 / MIN_FRAME_RATE)
43 
44 /* Frame buffer descriptor */
45 struct 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 
54 static void set_dma_ctrl(struct fbd *fb_desc, u32 ctrl)
55 {
56 	fb_desc->dma_ctrl = ctrl;
57 }
58 
59 struct isi_dma_desc {
60 	struct list_head list;
61 	struct fbd *p_fbd;
62 	dma_addr_t fbd_phys;
63 };
64 
65 /* Frame buffer data */
66 struct frame_buffer {
67 	struct vb2_v4l2_buffer vb;
68 	struct isi_dma_desc *p_dma_desc;
69 	struct list_head list;
70 };
71 
72 struct isi_graph_entity {
73 	struct device_node *node;
74 
75 	struct v4l2_async_subdev asd;
76 	struct v4l2_subdev *subdev;
77 };
78 
79 /*
80  * struct isi_format - ISI media bus format information
81  * @fourcc:		Fourcc code for this format
82  * @mbus_code:		V4L2 media bus format code.
83  * @bpp:		Bytes per pixel (when stored in memory)
84  * @swap:		Byte swap configuration value
85  * @support:		Indicates format supported by subdev
86  * @skip:		Skip duplicate format supported by subdev
87  */
88 struct isi_format {
89 	u32	fourcc;
90 	u32	mbus_code;
91 	u8	bpp;
92 	u32	swap;
93 };
94 
95 
96 struct atmel_isi {
97 	/* Protects the access of variables shared with the ISR */
98 	spinlock_t			irqlock;
99 	struct device			*dev;
100 	void __iomem			*regs;
101 
102 	int				sequence;
103 
104 	/* Allocate descriptors for dma buffer use */
105 	struct fbd			*p_fb_descriptors;
106 	dma_addr_t			fb_descriptors_phys;
107 	struct				list_head dma_desc_head;
108 	struct isi_dma_desc		dma_desc[VIDEO_MAX_FRAME];
109 	bool				enable_preview_path;
110 
111 	struct completion		complete;
112 	/* ISI peripherial clock */
113 	struct clk			*pclk;
114 	unsigned int			irq;
115 
116 	struct isi_platform_data	pdata;
117 	u16				width_flags;	/* max 12 bits */
118 
119 	struct list_head		video_buffer_list;
120 	struct frame_buffer		*active;
121 
122 	struct v4l2_device		v4l2_dev;
123 	struct video_device		*vdev;
124 	struct v4l2_async_notifier	notifier;
125 	struct isi_graph_entity		entity;
126 	struct v4l2_format		fmt;
127 
128 	const struct isi_format		**user_formats;
129 	unsigned int			num_user_formats;
130 	const struct isi_format		*current_fmt;
131 
132 	struct mutex			lock;
133 	struct vb2_queue		queue;
134 };
135 
136 #define notifier_to_isi(n) container_of(n, struct atmel_isi, notifier)
137 
138 static void isi_writel(struct atmel_isi *isi, u32 reg, u32 val)
139 {
140 	writel(val, isi->regs + reg);
141 }
142 static u32 isi_readl(struct atmel_isi *isi, u32 reg)
143 {
144 	return readl(isi->regs + reg);
145 }
146 
147 static void configure_geometry(struct atmel_isi *isi)
148 {
149 	u32 cfg2, psize;
150 	u32 fourcc = isi->current_fmt->fourcc;
151 
152 	isi->enable_preview_path = fourcc == V4L2_PIX_FMT_RGB565 ||
153 				   fourcc == V4L2_PIX_FMT_RGB32;
154 
155 	/* According to sensor's output format to set cfg2 */
156 	cfg2 = isi->current_fmt->swap;
157 
158 	isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
159 	/* Set width */
160 	cfg2 |= ((isi->fmt.fmt.pix.width - 1) << ISI_CFG2_IM_HSIZE_OFFSET) &
161 			ISI_CFG2_IM_HSIZE_MASK;
162 	/* Set height */
163 	cfg2 |= ((isi->fmt.fmt.pix.height - 1) << ISI_CFG2_IM_VSIZE_OFFSET)
164 			& ISI_CFG2_IM_VSIZE_MASK;
165 	isi_writel(isi, ISI_CFG2, cfg2);
166 
167 	/* No down sampling, preview size equal to sensor output size */
168 	psize = ((isi->fmt.fmt.pix.width - 1) << ISI_PSIZE_PREV_HSIZE_OFFSET) &
169 		ISI_PSIZE_PREV_HSIZE_MASK;
170 	psize |= ((isi->fmt.fmt.pix.height - 1) << ISI_PSIZE_PREV_VSIZE_OFFSET) &
171 		ISI_PSIZE_PREV_VSIZE_MASK;
172 	isi_writel(isi, ISI_PSIZE, psize);
173 	isi_writel(isi, ISI_PDECF, ISI_PDECF_NO_SAMPLING);
174 }
175 
176 static irqreturn_t atmel_isi_handle_streaming(struct atmel_isi *isi)
177 {
178 	if (isi->active) {
179 		struct vb2_v4l2_buffer *vbuf = &isi->active->vb;
180 		struct frame_buffer *buf = isi->active;
181 
182 		list_del_init(&buf->list);
183 		vbuf->vb2_buf.timestamp = ktime_get_ns();
184 		vbuf->sequence = isi->sequence++;
185 		vbuf->field = V4L2_FIELD_NONE;
186 		vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
187 	}
188 
189 	if (list_empty(&isi->video_buffer_list)) {
190 		isi->active = NULL;
191 	} else {
192 		/* start next dma frame. */
193 		isi->active = list_entry(isi->video_buffer_list.next,
194 					struct frame_buffer, list);
195 		if (!isi->enable_preview_path) {
196 			isi_writel(isi, ISI_DMA_C_DSCR,
197 				(u32)isi->active->p_dma_desc->fbd_phys);
198 			isi_writel(isi, ISI_DMA_C_CTRL,
199 				ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
200 			isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
201 		} else {
202 			isi_writel(isi, ISI_DMA_P_DSCR,
203 				(u32)isi->active->p_dma_desc->fbd_phys);
204 			isi_writel(isi, ISI_DMA_P_CTRL,
205 				ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
206 			isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_P_CH);
207 		}
208 	}
209 	return IRQ_HANDLED;
210 }
211 
212 /* ISI interrupt service routine */
213 static irqreturn_t isi_interrupt(int irq, void *dev_id)
214 {
215 	struct atmel_isi *isi = dev_id;
216 	u32 status, mask, pending;
217 	irqreturn_t ret = IRQ_NONE;
218 
219 	spin_lock(&isi->irqlock);
220 
221 	status = isi_readl(isi, ISI_STATUS);
222 	mask = isi_readl(isi, ISI_INTMASK);
223 	pending = status & mask;
224 
225 	if (pending & ISI_CTRL_SRST) {
226 		complete(&isi->complete);
227 		isi_writel(isi, ISI_INTDIS, ISI_CTRL_SRST);
228 		ret = IRQ_HANDLED;
229 	} else if (pending & ISI_CTRL_DIS) {
230 		complete(&isi->complete);
231 		isi_writel(isi, ISI_INTDIS, ISI_CTRL_DIS);
232 		ret = IRQ_HANDLED;
233 	} else {
234 		if (likely(pending & ISI_SR_CXFR_DONE) ||
235 				likely(pending & ISI_SR_PXFR_DONE))
236 			ret = atmel_isi_handle_streaming(isi);
237 	}
238 
239 	spin_unlock(&isi->irqlock);
240 	return ret;
241 }
242 
243 #define	WAIT_ISI_RESET		1
244 #define	WAIT_ISI_DISABLE	0
245 static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset)
246 {
247 	unsigned long timeout;
248 	/*
249 	 * The reset or disable will only succeed if we have a
250 	 * pixel clock from the camera.
251 	 */
252 	init_completion(&isi->complete);
253 
254 	if (wait_reset) {
255 		isi_writel(isi, ISI_INTEN, ISI_CTRL_SRST);
256 		isi_writel(isi, ISI_CTRL, ISI_CTRL_SRST);
257 	} else {
258 		isi_writel(isi, ISI_INTEN, ISI_CTRL_DIS);
259 		isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
260 	}
261 
262 	timeout = wait_for_completion_timeout(&isi->complete,
263 			msecs_to_jiffies(500));
264 	if (timeout == 0)
265 		return -ETIMEDOUT;
266 
267 	return 0;
268 }
269 
270 /* ------------------------------------------------------------------
271 	Videobuf operations
272    ------------------------------------------------------------------*/
273 static int queue_setup(struct vb2_queue *vq,
274 				unsigned int *nbuffers, unsigned int *nplanes,
275 				unsigned int sizes[], struct device *alloc_devs[])
276 {
277 	struct atmel_isi *isi = vb2_get_drv_priv(vq);
278 	unsigned long size;
279 
280 	size = isi->fmt.fmt.pix.sizeimage;
281 
282 	/* Make sure the image size is large enough. */
283 	if (*nplanes)
284 		return sizes[0] < size ? -EINVAL : 0;
285 
286 	*nplanes = 1;
287 	sizes[0] = size;
288 
289 	isi->active = NULL;
290 
291 	dev_dbg(isi->dev, "%s, count=%d, size=%ld\n", __func__,
292 		*nbuffers, size);
293 
294 	return 0;
295 }
296 
297 static int buffer_init(struct vb2_buffer *vb)
298 {
299 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
300 	struct frame_buffer *buf = container_of(vbuf, struct frame_buffer, vb);
301 
302 	buf->p_dma_desc = NULL;
303 	INIT_LIST_HEAD(&buf->list);
304 
305 	return 0;
306 }
307 
308 static int buffer_prepare(struct vb2_buffer *vb)
309 {
310 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
311 	struct frame_buffer *buf = container_of(vbuf, struct frame_buffer, vb);
312 	struct atmel_isi *isi = vb2_get_drv_priv(vb->vb2_queue);
313 	unsigned long size;
314 	struct isi_dma_desc *desc;
315 
316 	size = isi->fmt.fmt.pix.sizeimage;
317 
318 	if (vb2_plane_size(vb, 0) < size) {
319 		dev_err(isi->dev, "%s data will not fit into plane (%lu < %lu)\n",
320 				__func__, vb2_plane_size(vb, 0), size);
321 		return -EINVAL;
322 	}
323 
324 	vb2_set_plane_payload(vb, 0, size);
325 
326 	if (!buf->p_dma_desc) {
327 		if (list_empty(&isi->dma_desc_head)) {
328 			dev_err(isi->dev, "Not enough dma descriptors.\n");
329 			return -EINVAL;
330 		} else {
331 			/* Get an available descriptor */
332 			desc = list_entry(isi->dma_desc_head.next,
333 						struct isi_dma_desc, list);
334 			/* Delete the descriptor since now it is used */
335 			list_del_init(&desc->list);
336 
337 			/* Initialize the dma descriptor */
338 			desc->p_fbd->fb_address =
339 					vb2_dma_contig_plane_dma_addr(vb, 0);
340 			desc->p_fbd->next_fbd_address = 0;
341 			set_dma_ctrl(desc->p_fbd, ISI_DMA_CTRL_WB);
342 
343 			buf->p_dma_desc = desc;
344 		}
345 	}
346 	return 0;
347 }
348 
349 static void buffer_cleanup(struct vb2_buffer *vb)
350 {
351 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
352 	struct atmel_isi *isi = vb2_get_drv_priv(vb->vb2_queue);
353 	struct frame_buffer *buf = container_of(vbuf, struct frame_buffer, vb);
354 
355 	/* This descriptor is available now and we add to head list */
356 	if (buf->p_dma_desc)
357 		list_add(&buf->p_dma_desc->list, &isi->dma_desc_head);
358 }
359 
360 static void start_dma(struct atmel_isi *isi, struct frame_buffer *buffer)
361 {
362 	u32 ctrl, cfg1;
363 
364 	cfg1 = isi_readl(isi, ISI_CFG1);
365 	/* Enable irq: cxfr for the codec path, pxfr for the preview path */
366 	isi_writel(isi, ISI_INTEN,
367 			ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
368 
369 	/* Check if already in a frame */
370 	if (!isi->enable_preview_path) {
371 		if (isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) {
372 			dev_err(isi->dev, "Already in frame handling.\n");
373 			return;
374 		}
375 
376 		isi_writel(isi, ISI_DMA_C_DSCR,
377 				(u32)buffer->p_dma_desc->fbd_phys);
378 		isi_writel(isi, ISI_DMA_C_CTRL,
379 				ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
380 		isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
381 	} else {
382 		isi_writel(isi, ISI_DMA_P_DSCR,
383 				(u32)buffer->p_dma_desc->fbd_phys);
384 		isi_writel(isi, ISI_DMA_P_CTRL,
385 				ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
386 		isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_P_CH);
387 	}
388 
389 	cfg1 &= ~ISI_CFG1_FRATE_DIV_MASK;
390 	/* Enable linked list */
391 	cfg1 |= isi->pdata.frate | ISI_CFG1_DISCR;
392 
393 	/* Enable ISI */
394 	ctrl = ISI_CTRL_EN;
395 
396 	if (!isi->enable_preview_path)
397 		ctrl |= ISI_CTRL_CDC;
398 
399 	isi_writel(isi, ISI_CTRL, ctrl);
400 	isi_writel(isi, ISI_CFG1, cfg1);
401 }
402 
403 static void buffer_queue(struct vb2_buffer *vb)
404 {
405 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
406 	struct atmel_isi *isi = vb2_get_drv_priv(vb->vb2_queue);
407 	struct frame_buffer *buf = container_of(vbuf, struct frame_buffer, vb);
408 	unsigned long flags = 0;
409 
410 	spin_lock_irqsave(&isi->irqlock, flags);
411 	list_add_tail(&buf->list, &isi->video_buffer_list);
412 
413 	if (isi->active == NULL) {
414 		isi->active = buf;
415 		if (vb2_is_streaming(vb->vb2_queue))
416 			start_dma(isi, buf);
417 	}
418 	spin_unlock_irqrestore(&isi->irqlock, flags);
419 }
420 
421 static int start_streaming(struct vb2_queue *vq, unsigned int count)
422 {
423 	struct atmel_isi *isi = vb2_get_drv_priv(vq);
424 	struct frame_buffer *buf, *node;
425 	int ret;
426 
427 	/* Enable stream on the sub device */
428 	ret = v4l2_subdev_call(isi->entity.subdev, video, s_stream, 1);
429 	if (ret && ret != -ENOIOCTLCMD) {
430 		dev_err(isi->dev, "stream on failed in subdev\n");
431 		goto err_start_stream;
432 	}
433 
434 	pm_runtime_get_sync(isi->dev);
435 
436 	/* Reset ISI */
437 	ret = atmel_isi_wait_status(isi, WAIT_ISI_RESET);
438 	if (ret < 0) {
439 		dev_err(isi->dev, "Reset ISI timed out\n");
440 		goto err_reset;
441 	}
442 	/* Disable all interrupts */
443 	isi_writel(isi, ISI_INTDIS, (u32)~0UL);
444 
445 	isi->sequence = 0;
446 	configure_geometry(isi);
447 
448 	spin_lock_irq(&isi->irqlock);
449 	/* Clear any pending interrupt */
450 	isi_readl(isi, ISI_STATUS);
451 
452 	start_dma(isi, isi->active);
453 	spin_unlock_irq(&isi->irqlock);
454 
455 	return 0;
456 
457 err_reset:
458 	pm_runtime_put(isi->dev);
459 	v4l2_subdev_call(isi->entity.subdev, video, s_stream, 0);
460 
461 err_start_stream:
462 	spin_lock_irq(&isi->irqlock);
463 	isi->active = NULL;
464 	/* Release all active buffers */
465 	list_for_each_entry_safe(buf, node, &isi->video_buffer_list, list) {
466 		list_del_init(&buf->list);
467 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
468 	}
469 	spin_unlock_irq(&isi->irqlock);
470 
471 	return ret;
472 }
473 
474 /* abort streaming and wait for last buffer */
475 static void stop_streaming(struct vb2_queue *vq)
476 {
477 	struct atmel_isi *isi = vb2_get_drv_priv(vq);
478 	struct frame_buffer *buf, *node;
479 	int ret = 0;
480 	unsigned long timeout;
481 
482 	/* Disable stream on the sub device */
483 	ret = v4l2_subdev_call(isi->entity.subdev, video, s_stream, 0);
484 	if (ret && ret != -ENOIOCTLCMD)
485 		dev_err(isi->dev, "stream off failed in subdev\n");
486 
487 	spin_lock_irq(&isi->irqlock);
488 	isi->active = NULL;
489 	/* Release all active buffers */
490 	list_for_each_entry_safe(buf, node, &isi->video_buffer_list, list) {
491 		list_del_init(&buf->list);
492 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
493 	}
494 	spin_unlock_irq(&isi->irqlock);
495 
496 	if (!isi->enable_preview_path) {
497 		timeout = jiffies + FRAME_INTERVAL_MILLI_SEC * HZ;
498 		/* Wait until the end of the current frame. */
499 		while ((isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) &&
500 				time_before(jiffies, timeout))
501 			msleep(1);
502 
503 		if (time_after(jiffies, timeout))
504 			dev_err(isi->dev,
505 				"Timeout waiting for finishing codec request\n");
506 	}
507 
508 	/* Disable interrupts */
509 	isi_writel(isi, ISI_INTDIS,
510 			ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
511 
512 	/* Disable ISI and wait for it is done */
513 	ret = atmel_isi_wait_status(isi, WAIT_ISI_DISABLE);
514 	if (ret < 0)
515 		dev_err(isi->dev, "Disable ISI timed out\n");
516 
517 	pm_runtime_put(isi->dev);
518 }
519 
520 static const struct vb2_ops isi_video_qops = {
521 	.queue_setup		= queue_setup,
522 	.buf_init		= buffer_init,
523 	.buf_prepare		= buffer_prepare,
524 	.buf_cleanup		= buffer_cleanup,
525 	.buf_queue		= buffer_queue,
526 	.start_streaming	= start_streaming,
527 	.stop_streaming		= stop_streaming,
528 	.wait_prepare		= vb2_ops_wait_prepare,
529 	.wait_finish		= vb2_ops_wait_finish,
530 };
531 
532 static int isi_g_fmt_vid_cap(struct file *file, void *priv,
533 			      struct v4l2_format *fmt)
534 {
535 	struct atmel_isi *isi = video_drvdata(file);
536 
537 	*fmt = isi->fmt;
538 
539 	return 0;
540 }
541 
542 static const struct isi_format *find_format_by_fourcc(struct atmel_isi *isi,
543 						      unsigned int fourcc)
544 {
545 	unsigned int num_formats = isi->num_user_formats;
546 	const struct isi_format *fmt;
547 	unsigned int i;
548 
549 	for (i = 0; i < num_formats; i++) {
550 		fmt = isi->user_formats[i];
551 		if (fmt->fourcc == fourcc)
552 			return fmt;
553 	}
554 
555 	return NULL;
556 }
557 
558 static int isi_try_fmt(struct atmel_isi *isi, struct v4l2_format *f,
559 		       const struct isi_format **current_fmt)
560 {
561 	const struct isi_format *isi_fmt;
562 	struct v4l2_pix_format *pixfmt = &f->fmt.pix;
563 	struct v4l2_subdev_pad_config pad_cfg;
564 	struct v4l2_subdev_format format = {
565 		.which = V4L2_SUBDEV_FORMAT_TRY,
566 	};
567 	int ret;
568 
569 	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
570 		return -EINVAL;
571 
572 	isi_fmt = find_format_by_fourcc(isi, pixfmt->pixelformat);
573 	if (!isi_fmt) {
574 		isi_fmt = isi->user_formats[isi->num_user_formats - 1];
575 		pixfmt->pixelformat = isi_fmt->fourcc;
576 	}
577 
578 	/* Limit to Atmel ISC hardware capabilities */
579 	if (pixfmt->width > MAX_SUPPORT_WIDTH)
580 		pixfmt->width = MAX_SUPPORT_WIDTH;
581 	if (pixfmt->height > MAX_SUPPORT_HEIGHT)
582 		pixfmt->height = MAX_SUPPORT_HEIGHT;
583 
584 	v4l2_fill_mbus_format(&format.format, pixfmt, isi_fmt->mbus_code);
585 	ret = v4l2_subdev_call(isi->entity.subdev, pad, set_fmt,
586 			       &pad_cfg, &format);
587 	if (ret < 0)
588 		return ret;
589 
590 	v4l2_fill_pix_format(pixfmt, &format.format);
591 
592 	pixfmt->field = V4L2_FIELD_NONE;
593 	pixfmt->bytesperline = pixfmt->width * isi_fmt->bpp;
594 	pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
595 
596 	if (current_fmt)
597 		*current_fmt = isi_fmt;
598 
599 	return 0;
600 }
601 
602 static int isi_set_fmt(struct atmel_isi *isi, struct v4l2_format *f)
603 {
604 	struct v4l2_subdev_format format = {
605 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
606 	};
607 	const struct isi_format *current_fmt;
608 	int ret;
609 
610 	ret = isi_try_fmt(isi, f, &current_fmt);
611 	if (ret)
612 		return ret;
613 
614 	v4l2_fill_mbus_format(&format.format, &f->fmt.pix,
615 			      current_fmt->mbus_code);
616 	ret = v4l2_subdev_call(isi->entity.subdev, pad,
617 			       set_fmt, NULL, &format);
618 	if (ret < 0)
619 		return ret;
620 
621 	isi->fmt = *f;
622 	isi->current_fmt = current_fmt;
623 
624 	return 0;
625 }
626 
627 static int isi_s_fmt_vid_cap(struct file *file, void *priv,
628 			      struct v4l2_format *f)
629 {
630 	struct atmel_isi *isi = video_drvdata(file);
631 
632 	if (vb2_is_streaming(&isi->queue))
633 		return -EBUSY;
634 
635 	return isi_set_fmt(isi, f);
636 }
637 
638 static int isi_try_fmt_vid_cap(struct file *file, void *priv,
639 				struct v4l2_format *f)
640 {
641 	struct atmel_isi *isi = video_drvdata(file);
642 
643 	return isi_try_fmt(isi, f, NULL);
644 }
645 
646 static int isi_enum_fmt_vid_cap(struct file *file, void  *priv,
647 				struct v4l2_fmtdesc *f)
648 {
649 	struct atmel_isi *isi = video_drvdata(file);
650 
651 	if (f->index >= isi->num_user_formats)
652 		return -EINVAL;
653 
654 	f->pixelformat = isi->user_formats[f->index]->fourcc;
655 	return 0;
656 }
657 
658 static int isi_querycap(struct file *file, void *priv,
659 			struct v4l2_capability *cap)
660 {
661 	strlcpy(cap->driver, "atmel-isi", sizeof(cap->driver));
662 	strlcpy(cap->card, "Atmel Image Sensor Interface", sizeof(cap->card));
663 	strlcpy(cap->bus_info, "platform:isi", sizeof(cap->bus_info));
664 	return 0;
665 }
666 
667 static int isi_enum_input(struct file *file, void *priv,
668 			   struct v4l2_input *i)
669 {
670 	if (i->index != 0)
671 		return -EINVAL;
672 
673 	i->type = V4L2_INPUT_TYPE_CAMERA;
674 	strlcpy(i->name, "Camera", sizeof(i->name));
675 	return 0;
676 }
677 
678 static int isi_g_input(struct file *file, void *priv, unsigned int *i)
679 {
680 	*i = 0;
681 	return 0;
682 }
683 
684 static int isi_s_input(struct file *file, void *priv, unsigned int i)
685 {
686 	if (i > 0)
687 		return -EINVAL;
688 	return 0;
689 }
690 
691 static int isi_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
692 {
693 	struct atmel_isi *isi = video_drvdata(file);
694 
695 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
696 		return -EINVAL;
697 
698 	a->parm.capture.readbuffers = 2;
699 	return v4l2_subdev_call(isi->entity.subdev, video, g_parm, a);
700 }
701 
702 static int isi_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
703 {
704 	struct atmel_isi *isi = video_drvdata(file);
705 
706 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
707 		return -EINVAL;
708 
709 	a->parm.capture.readbuffers = 2;
710 	return v4l2_subdev_call(isi->entity.subdev, video, s_parm, a);
711 }
712 
713 static int isi_enum_framesizes(struct file *file, void *fh,
714 			       struct v4l2_frmsizeenum *fsize)
715 {
716 	struct atmel_isi *isi = video_drvdata(file);
717 	const struct isi_format *isi_fmt;
718 	struct v4l2_subdev_frame_size_enum fse = {
719 		.index = fsize->index,
720 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
721 	};
722 	int ret;
723 
724 	isi_fmt = find_format_by_fourcc(isi, fsize->pixel_format);
725 	if (!isi_fmt)
726 		return -EINVAL;
727 
728 	fse.code = isi_fmt->mbus_code;
729 
730 	ret = v4l2_subdev_call(isi->entity.subdev, pad, enum_frame_size,
731 			       NULL, &fse);
732 	if (ret)
733 		return ret;
734 
735 	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
736 	fsize->discrete.width = fse.max_width;
737 	fsize->discrete.height = fse.max_height;
738 
739 	return 0;
740 }
741 
742 static int isi_enum_frameintervals(struct file *file, void *fh,
743 				    struct v4l2_frmivalenum *fival)
744 {
745 	struct atmel_isi *isi = video_drvdata(file);
746 	const struct isi_format *isi_fmt;
747 	struct v4l2_subdev_frame_interval_enum fie = {
748 		.index = fival->index,
749 		.width = fival->width,
750 		.height = fival->height,
751 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
752 	};
753 	int ret;
754 
755 	isi_fmt = find_format_by_fourcc(isi, fival->pixel_format);
756 	if (!isi_fmt)
757 		return -EINVAL;
758 
759 	fie.code = isi_fmt->mbus_code;
760 
761 	ret = v4l2_subdev_call(isi->entity.subdev, pad,
762 			       enum_frame_interval, NULL, &fie);
763 	if (ret)
764 		return ret;
765 
766 	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
767 	fival->discrete = fie.interval;
768 
769 	return 0;
770 }
771 
772 static void isi_camera_set_bus_param(struct atmel_isi *isi)
773 {
774 	u32 cfg1 = 0;
775 
776 	/* set bus param for ISI */
777 	if (isi->pdata.hsync_act_low)
778 		cfg1 |= ISI_CFG1_HSYNC_POL_ACTIVE_LOW;
779 	if (isi->pdata.vsync_act_low)
780 		cfg1 |= ISI_CFG1_VSYNC_POL_ACTIVE_LOW;
781 	if (isi->pdata.pclk_act_falling)
782 		cfg1 |= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING;
783 	if (isi->pdata.has_emb_sync)
784 		cfg1 |= ISI_CFG1_EMB_SYNC;
785 	if (isi->pdata.full_mode)
786 		cfg1 |= ISI_CFG1_FULL_MODE;
787 
788 	cfg1 |= ISI_CFG1_THMASK_BEATS_16;
789 
790 	/* Enable PM and peripheral clock before operate isi registers */
791 	pm_runtime_get_sync(isi->dev);
792 
793 	isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
794 	isi_writel(isi, ISI_CFG1, cfg1);
795 
796 	pm_runtime_put(isi->dev);
797 }
798 
799 /* -----------------------------------------------------------------------*/
800 static int atmel_isi_parse_dt(struct atmel_isi *isi,
801 			struct platform_device *pdev)
802 {
803 	struct device_node *np = pdev->dev.of_node;
804 	struct v4l2_of_endpoint ep;
805 	int err;
806 
807 	/* Default settings for ISI */
808 	isi->pdata.full_mode = 1;
809 	isi->pdata.frate = ISI_CFG1_FRATE_CAPTURE_ALL;
810 
811 	np = of_graph_get_next_endpoint(np, NULL);
812 	if (!np) {
813 		dev_err(&pdev->dev, "Could not find the endpoint\n");
814 		return -EINVAL;
815 	}
816 
817 	err = v4l2_of_parse_endpoint(np, &ep);
818 	of_node_put(np);
819 	if (err) {
820 		dev_err(&pdev->dev, "Could not parse the endpoint\n");
821 		return err;
822 	}
823 
824 	switch (ep.bus.parallel.bus_width) {
825 	case 8:
826 		isi->pdata.data_width_flags = ISI_DATAWIDTH_8;
827 		break;
828 	case 10:
829 		isi->pdata.data_width_flags =
830 				ISI_DATAWIDTH_8 | ISI_DATAWIDTH_10;
831 		break;
832 	default:
833 		dev_err(&pdev->dev, "Unsupported bus width: %d\n",
834 				ep.bus.parallel.bus_width);
835 		return -EINVAL;
836 	}
837 
838 	if (ep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
839 		isi->pdata.hsync_act_low = true;
840 	if (ep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
841 		isi->pdata.vsync_act_low = true;
842 	if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
843 		isi->pdata.pclk_act_falling = true;
844 
845 	if (ep.bus_type == V4L2_MBUS_BT656)
846 		isi->pdata.has_emb_sync = true;
847 
848 	return 0;
849 }
850 
851 static int isi_open(struct file *file)
852 {
853 	struct atmel_isi *isi = video_drvdata(file);
854 	struct v4l2_subdev *sd = isi->entity.subdev;
855 	int ret;
856 
857 	if (mutex_lock_interruptible(&isi->lock))
858 		return -ERESTARTSYS;
859 
860 	ret = v4l2_fh_open(file);
861 	if (ret < 0)
862 		goto unlock;
863 
864 	if (!v4l2_fh_is_singular_file(file))
865 		goto fh_rel;
866 
867 	ret = v4l2_subdev_call(sd, core, s_power, 1);
868 	if (ret < 0 && ret != -ENOIOCTLCMD)
869 		goto fh_rel;
870 
871 	ret = isi_set_fmt(isi, &isi->fmt);
872 	if (ret)
873 		v4l2_subdev_call(sd, core, s_power, 0);
874 fh_rel:
875 	if (ret)
876 		v4l2_fh_release(file);
877 unlock:
878 	mutex_unlock(&isi->lock);
879 	return ret;
880 }
881 
882 static int isi_release(struct file *file)
883 {
884 	struct atmel_isi *isi = video_drvdata(file);
885 	struct v4l2_subdev *sd = isi->entity.subdev;
886 	bool fh_singular;
887 	int ret;
888 
889 	mutex_lock(&isi->lock);
890 
891 	fh_singular = v4l2_fh_is_singular_file(file);
892 
893 	ret = _vb2_fop_release(file, NULL);
894 
895 	if (fh_singular)
896 		v4l2_subdev_call(sd, core, s_power, 0);
897 
898 	mutex_unlock(&isi->lock);
899 
900 	return ret;
901 }
902 
903 static const struct v4l2_ioctl_ops isi_ioctl_ops = {
904 	.vidioc_querycap		= isi_querycap,
905 
906 	.vidioc_try_fmt_vid_cap		= isi_try_fmt_vid_cap,
907 	.vidioc_g_fmt_vid_cap		= isi_g_fmt_vid_cap,
908 	.vidioc_s_fmt_vid_cap		= isi_s_fmt_vid_cap,
909 	.vidioc_enum_fmt_vid_cap	= isi_enum_fmt_vid_cap,
910 
911 	.vidioc_enum_input		= isi_enum_input,
912 	.vidioc_g_input			= isi_g_input,
913 	.vidioc_s_input			= isi_s_input,
914 
915 	.vidioc_g_parm			= isi_g_parm,
916 	.vidioc_s_parm			= isi_s_parm,
917 	.vidioc_enum_framesizes		= isi_enum_framesizes,
918 	.vidioc_enum_frameintervals	= isi_enum_frameintervals,
919 
920 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
921 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
922 	.vidioc_querybuf		= vb2_ioctl_querybuf,
923 	.vidioc_qbuf			= vb2_ioctl_qbuf,
924 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
925 	.vidioc_expbuf			= vb2_ioctl_expbuf,
926 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
927 	.vidioc_streamon		= vb2_ioctl_streamon,
928 	.vidioc_streamoff		= vb2_ioctl_streamoff,
929 
930 	.vidioc_log_status		= v4l2_ctrl_log_status,
931 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
932 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
933 };
934 
935 static const struct v4l2_file_operations isi_fops = {
936 	.owner		= THIS_MODULE,
937 	.unlocked_ioctl	= video_ioctl2,
938 	.open		= isi_open,
939 	.release	= isi_release,
940 	.poll		= vb2_fop_poll,
941 	.mmap		= vb2_fop_mmap,
942 	.read		= vb2_fop_read,
943 };
944 
945 static int isi_set_default_fmt(struct atmel_isi *isi)
946 {
947 	struct v4l2_format f = {
948 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
949 		.fmt.pix = {
950 			.width		= VGA_WIDTH,
951 			.height		= VGA_HEIGHT,
952 			.field		= V4L2_FIELD_NONE,
953 			.pixelformat	= isi->user_formats[0]->fourcc,
954 		},
955 	};
956 	int ret;
957 
958 	ret = isi_try_fmt(isi, &f, NULL);
959 	if (ret)
960 		return ret;
961 	isi->current_fmt = isi->user_formats[0];
962 	isi->fmt = f;
963 	return 0;
964 }
965 
966 static const struct isi_format isi_formats[] = {
967 	{
968 		.fourcc = V4L2_PIX_FMT_YUYV,
969 		.mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
970 		.bpp = 2,
971 		.swap = ISI_CFG2_YCC_SWAP_DEFAULT,
972 	}, {
973 		.fourcc = V4L2_PIX_FMT_YUYV,
974 		.mbus_code = MEDIA_BUS_FMT_YVYU8_2X8,
975 		.bpp = 2,
976 		.swap = ISI_CFG2_YCC_SWAP_MODE_1,
977 	}, {
978 		.fourcc = V4L2_PIX_FMT_YUYV,
979 		.mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
980 		.bpp = 2,
981 		.swap = ISI_CFG2_YCC_SWAP_MODE_2,
982 	}, {
983 		.fourcc = V4L2_PIX_FMT_YUYV,
984 		.mbus_code = MEDIA_BUS_FMT_VYUY8_2X8,
985 		.bpp = 2,
986 		.swap = ISI_CFG2_YCC_SWAP_MODE_3,
987 	}, {
988 		.fourcc = V4L2_PIX_FMT_RGB565,
989 		.mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
990 		.bpp = 2,
991 		.swap = ISI_CFG2_YCC_SWAP_MODE_2,
992 	}, {
993 		.fourcc = V4L2_PIX_FMT_RGB565,
994 		.mbus_code = MEDIA_BUS_FMT_YVYU8_2X8,
995 		.bpp = 2,
996 		.swap = ISI_CFG2_YCC_SWAP_MODE_3,
997 	}, {
998 		.fourcc = V4L2_PIX_FMT_RGB565,
999 		.mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
1000 		.bpp = 2,
1001 		.swap = ISI_CFG2_YCC_SWAP_DEFAULT,
1002 	}, {
1003 		.fourcc = V4L2_PIX_FMT_RGB565,
1004 		.mbus_code = MEDIA_BUS_FMT_VYUY8_2X8,
1005 		.bpp = 2,
1006 		.swap = ISI_CFG2_YCC_SWAP_MODE_1,
1007 	},
1008 };
1009 
1010 static int isi_formats_init(struct atmel_isi *isi)
1011 {
1012 	const struct isi_format *isi_fmts[ARRAY_SIZE(isi_formats)];
1013 	unsigned int num_fmts = 0, i, j;
1014 	struct v4l2_subdev *subdev = isi->entity.subdev;
1015 	struct v4l2_subdev_mbus_code_enum mbus_code = {
1016 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
1017 	};
1018 
1019 	while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
1020 				 NULL, &mbus_code)) {
1021 		for (i = 0; i < ARRAY_SIZE(isi_formats); i++) {
1022 			if (isi_formats[i].mbus_code != mbus_code.code)
1023 				continue;
1024 
1025 			/* Code supported, have we got this fourcc yet? */
1026 			for (j = 0; j < num_fmts; j++)
1027 				if (isi_fmts[j]->fourcc == isi_formats[i].fourcc)
1028 					/* Already available */
1029 					break;
1030 			if (j == num_fmts)
1031 				/* new */
1032 				isi_fmts[num_fmts++] = isi_formats + i;
1033 		}
1034 		mbus_code.index++;
1035 	}
1036 
1037 	if (!num_fmts)
1038 		return -ENXIO;
1039 
1040 	isi->num_user_formats = num_fmts;
1041 	isi->user_formats = devm_kcalloc(isi->dev,
1042 					 num_fmts, sizeof(struct isi_format *),
1043 					 GFP_KERNEL);
1044 	if (!isi->user_formats) {
1045 		dev_err(isi->dev, "could not allocate memory\n");
1046 		return -ENOMEM;
1047 	}
1048 
1049 	memcpy(isi->user_formats, isi_fmts,
1050 	       num_fmts * sizeof(struct isi_format *));
1051 	isi->current_fmt = isi->user_formats[0];
1052 
1053 	return 0;
1054 }
1055 
1056 static int isi_graph_notify_complete(struct v4l2_async_notifier *notifier)
1057 {
1058 	struct atmel_isi *isi = notifier_to_isi(notifier);
1059 	int ret;
1060 
1061 	isi->vdev->ctrl_handler	= isi->entity.subdev->ctrl_handler;
1062 	ret = isi_formats_init(isi);
1063 	if (ret) {
1064 		dev_err(isi->dev, "No supported mediabus format found\n");
1065 		return ret;
1066 	}
1067 	isi_camera_set_bus_param(isi);
1068 
1069 	ret = isi_set_default_fmt(isi);
1070 	if (ret) {
1071 		dev_err(isi->dev, "Could not set default format\n");
1072 		return ret;
1073 	}
1074 
1075 	ret = video_register_device(isi->vdev, VFL_TYPE_GRABBER, -1);
1076 	if (ret) {
1077 		dev_err(isi->dev, "Failed to register video device\n");
1078 		return ret;
1079 	}
1080 
1081 	dev_dbg(isi->dev, "Device registered as %s\n",
1082 		video_device_node_name(isi->vdev));
1083 	return 0;
1084 }
1085 
1086 static void isi_graph_notify_unbind(struct v4l2_async_notifier *notifier,
1087 				     struct v4l2_subdev *sd,
1088 				     struct v4l2_async_subdev *asd)
1089 {
1090 	struct atmel_isi *isi = notifier_to_isi(notifier);
1091 
1092 	dev_dbg(isi->dev, "Removing %s\n", video_device_node_name(isi->vdev));
1093 
1094 	/* Checks internaly if vdev have been init or not */
1095 	video_unregister_device(isi->vdev);
1096 }
1097 
1098 static int isi_graph_notify_bound(struct v4l2_async_notifier *notifier,
1099 				   struct v4l2_subdev *subdev,
1100 				   struct v4l2_async_subdev *asd)
1101 {
1102 	struct atmel_isi *isi = notifier_to_isi(notifier);
1103 
1104 	dev_dbg(isi->dev, "subdev %s bound\n", subdev->name);
1105 
1106 	isi->entity.subdev = subdev;
1107 
1108 	return 0;
1109 }
1110 
1111 static int isi_graph_parse(struct atmel_isi *isi, struct device_node *node)
1112 {
1113 	struct device_node *ep = NULL;
1114 	struct device_node *remote;
1115 
1116 	while (1) {
1117 		ep = of_graph_get_next_endpoint(node, ep);
1118 		if (!ep)
1119 			return -EINVAL;
1120 
1121 		remote = of_graph_get_remote_port_parent(ep);
1122 		if (!remote) {
1123 			of_node_put(ep);
1124 			return -EINVAL;
1125 		}
1126 
1127 		/* Remote node to connect */
1128 		isi->entity.node = remote;
1129 		isi->entity.asd.match_type = V4L2_ASYNC_MATCH_OF;
1130 		isi->entity.asd.match.of.node = remote;
1131 		return 0;
1132 	}
1133 }
1134 
1135 static int isi_graph_init(struct atmel_isi *isi)
1136 {
1137 	struct v4l2_async_subdev **subdevs = NULL;
1138 	int ret;
1139 
1140 	/* Parse the graph to extract a list of subdevice DT nodes. */
1141 	ret = isi_graph_parse(isi, isi->dev->of_node);
1142 	if (ret < 0) {
1143 		dev_err(isi->dev, "Graph parsing failed\n");
1144 		return ret;
1145 	}
1146 
1147 	/* Register the subdevices notifier. */
1148 	subdevs = devm_kzalloc(isi->dev, sizeof(*subdevs), GFP_KERNEL);
1149 	if (subdevs == NULL) {
1150 		of_node_put(isi->entity.node);
1151 		return -ENOMEM;
1152 	}
1153 
1154 	subdevs[0] = &isi->entity.asd;
1155 
1156 	isi->notifier.subdevs = subdevs;
1157 	isi->notifier.num_subdevs = 1;
1158 	isi->notifier.bound = isi_graph_notify_bound;
1159 	isi->notifier.unbind = isi_graph_notify_unbind;
1160 	isi->notifier.complete = isi_graph_notify_complete;
1161 
1162 	ret = v4l2_async_notifier_register(&isi->v4l2_dev, &isi->notifier);
1163 	if (ret < 0) {
1164 		dev_err(isi->dev, "Notifier registration failed\n");
1165 		of_node_put(isi->entity.node);
1166 		return ret;
1167 	}
1168 
1169 	return 0;
1170 }
1171 
1172 
1173 static int atmel_isi_probe(struct platform_device *pdev)
1174 {
1175 	int irq;
1176 	struct atmel_isi *isi;
1177 	struct vb2_queue *q;
1178 	struct resource *regs;
1179 	int ret, i;
1180 
1181 	isi = devm_kzalloc(&pdev->dev, sizeof(struct atmel_isi), GFP_KERNEL);
1182 	if (!isi) {
1183 		dev_err(&pdev->dev, "Can't allocate interface!\n");
1184 		return -ENOMEM;
1185 	}
1186 
1187 	isi->pclk = devm_clk_get(&pdev->dev, "isi_clk");
1188 	if (IS_ERR(isi->pclk))
1189 		return PTR_ERR(isi->pclk);
1190 
1191 	ret = atmel_isi_parse_dt(isi, pdev);
1192 	if (ret)
1193 		return ret;
1194 
1195 	isi->active = NULL;
1196 	isi->dev = &pdev->dev;
1197 	mutex_init(&isi->lock);
1198 	spin_lock_init(&isi->irqlock);
1199 	INIT_LIST_HEAD(&isi->video_buffer_list);
1200 	INIT_LIST_HEAD(&isi->dma_desc_head);
1201 
1202 	q = &isi->queue;
1203 
1204 	/* Initialize the top-level structure */
1205 	ret = v4l2_device_register(&pdev->dev, &isi->v4l2_dev);
1206 	if (ret)
1207 		return ret;
1208 
1209 	isi->vdev = video_device_alloc();
1210 	if (isi->vdev == NULL) {
1211 		ret = -ENOMEM;
1212 		goto err_vdev_alloc;
1213 	}
1214 
1215 	/* video node */
1216 	isi->vdev->fops = &isi_fops;
1217 	isi->vdev->v4l2_dev = &isi->v4l2_dev;
1218 	isi->vdev->queue = &isi->queue;
1219 	strlcpy(isi->vdev->name, KBUILD_MODNAME, sizeof(isi->vdev->name));
1220 	isi->vdev->release = video_device_release;
1221 	isi->vdev->ioctl_ops = &isi_ioctl_ops;
1222 	isi->vdev->lock = &isi->lock;
1223 	isi->vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1224 		V4L2_CAP_READWRITE;
1225 	video_set_drvdata(isi->vdev, isi);
1226 
1227 	/* buffer queue */
1228 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1229 	q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
1230 	q->lock = &isi->lock;
1231 	q->drv_priv = isi;
1232 	q->buf_struct_size = sizeof(struct frame_buffer);
1233 	q->ops = &isi_video_qops;
1234 	q->mem_ops = &vb2_dma_contig_memops;
1235 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1236 	q->min_buffers_needed = 2;
1237 	q->dev = &pdev->dev;
1238 
1239 	ret = vb2_queue_init(q);
1240 	if (ret < 0) {
1241 		dev_err(&pdev->dev, "failed to initialize VB2 queue\n");
1242 		goto err_vb2_queue;
1243 	}
1244 	isi->p_fb_descriptors = dma_alloc_coherent(&pdev->dev,
1245 				sizeof(struct fbd) * VIDEO_MAX_FRAME,
1246 				&isi->fb_descriptors_phys,
1247 				GFP_KERNEL);
1248 	if (!isi->p_fb_descriptors) {
1249 		dev_err(&pdev->dev, "Can't allocate descriptors!\n");
1250 		ret = -ENOMEM;
1251 		goto err_dma_alloc;
1252 	}
1253 
1254 	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1255 		isi->dma_desc[i].p_fbd = isi->p_fb_descriptors + i;
1256 		isi->dma_desc[i].fbd_phys = isi->fb_descriptors_phys +
1257 					i * sizeof(struct fbd);
1258 		list_add(&isi->dma_desc[i].list, &isi->dma_desc_head);
1259 	}
1260 
1261 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1262 	isi->regs = devm_ioremap_resource(&pdev->dev, regs);
1263 	if (IS_ERR(isi->regs)) {
1264 		ret = PTR_ERR(isi->regs);
1265 		goto err_ioremap;
1266 	}
1267 
1268 	if (isi->pdata.data_width_flags & ISI_DATAWIDTH_8)
1269 		isi->width_flags = 1 << 7;
1270 	if (isi->pdata.data_width_flags & ISI_DATAWIDTH_10)
1271 		isi->width_flags |= 1 << 9;
1272 
1273 	irq = platform_get_irq(pdev, 0);
1274 	if (irq < 0) {
1275 		ret = irq;
1276 		goto err_req_irq;
1277 	}
1278 
1279 	ret = devm_request_irq(&pdev->dev, irq, isi_interrupt, 0, "isi", isi);
1280 	if (ret) {
1281 		dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
1282 		goto err_req_irq;
1283 	}
1284 	isi->irq = irq;
1285 
1286 	ret = isi_graph_init(isi);
1287 	if (ret < 0)
1288 		goto err_req_irq;
1289 
1290 	pm_suspend_ignore_children(&pdev->dev, true);
1291 	pm_runtime_enable(&pdev->dev);
1292 	platform_set_drvdata(pdev, isi);
1293 	return 0;
1294 
1295 err_req_irq:
1296 err_ioremap:
1297 	dma_free_coherent(&pdev->dev,
1298 			sizeof(struct fbd) * VIDEO_MAX_FRAME,
1299 			isi->p_fb_descriptors,
1300 			isi->fb_descriptors_phys);
1301 err_dma_alloc:
1302 err_vb2_queue:
1303 	video_device_release(isi->vdev);
1304 err_vdev_alloc:
1305 	v4l2_device_unregister(&isi->v4l2_dev);
1306 
1307 	return ret;
1308 }
1309 
1310 static int atmel_isi_remove(struct platform_device *pdev)
1311 {
1312 	struct atmel_isi *isi = platform_get_drvdata(pdev);
1313 
1314 	dma_free_coherent(&pdev->dev,
1315 			sizeof(struct fbd) * VIDEO_MAX_FRAME,
1316 			isi->p_fb_descriptors,
1317 			isi->fb_descriptors_phys);
1318 	pm_runtime_disable(&pdev->dev);
1319 	v4l2_async_notifier_unregister(&isi->notifier);
1320 	v4l2_device_unregister(&isi->v4l2_dev);
1321 
1322 	return 0;
1323 }
1324 
1325 #ifdef CONFIG_PM
1326 static int atmel_isi_runtime_suspend(struct device *dev)
1327 {
1328 	struct atmel_isi *isi = dev_get_drvdata(dev);
1329 
1330 	clk_disable_unprepare(isi->pclk);
1331 
1332 	return 0;
1333 }
1334 static int atmel_isi_runtime_resume(struct device *dev)
1335 {
1336 	struct atmel_isi *isi = dev_get_drvdata(dev);
1337 
1338 	return clk_prepare_enable(isi->pclk);
1339 }
1340 #endif /* CONFIG_PM */
1341 
1342 static const struct dev_pm_ops atmel_isi_dev_pm_ops = {
1343 	SET_RUNTIME_PM_OPS(atmel_isi_runtime_suspend,
1344 				atmel_isi_runtime_resume, NULL)
1345 };
1346 
1347 static const struct of_device_id atmel_isi_of_match[] = {
1348 	{ .compatible = "atmel,at91sam9g45-isi" },
1349 	{ }
1350 };
1351 MODULE_DEVICE_TABLE(of, atmel_isi_of_match);
1352 
1353 static struct platform_driver atmel_isi_driver = {
1354 	.driver		= {
1355 		.name = "atmel_isi",
1356 		.of_match_table = of_match_ptr(atmel_isi_of_match),
1357 		.pm	= &atmel_isi_dev_pm_ops,
1358 	},
1359 	.probe		= atmel_isi_probe,
1360 	.remove		= atmel_isi_remove,
1361 };
1362 
1363 module_platform_driver(atmel_isi_driver);
1364 
1365 MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>");
1366 MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux");
1367 MODULE_LICENSE("GPL");
1368 MODULE_SUPPORTED_DEVICE("video");
1369