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