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