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