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