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> |
| 22 | #include <linux/platform_device.h> |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 23 | #include <linux/pm_runtime.h> |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | |
| 26 | #include <media/atmel-isi.h> |
| 27 | #include <media/soc_camera.h> |
| 28 | #include <media/soc_mediabus.h> |
Josh Wu | 8ff19bc | 2014-07-28 04:25:17 -0300 | [diff] [blame] | 29 | #include <media/v4l2-of.h> |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 30 | #include <media/videobuf2-dma-contig.h> |
| 31 | |
| 32 | #define MAX_BUFFER_NUM 32 |
| 33 | #define MAX_SUPPORT_WIDTH 2048 |
| 34 | #define MAX_SUPPORT_HEIGHT 2048 |
| 35 | #define VID_LIMIT_BYTES (16 * 1024 * 1024) |
| 36 | #define MIN_FRAME_RATE 15 |
| 37 | #define FRAME_INTERVAL_MILLI_SEC (1000 / MIN_FRAME_RATE) |
| 38 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 39 | /* Frame buffer descriptor */ |
| 40 | struct fbd { |
| 41 | /* Physical address of the frame buffer */ |
| 42 | u32 fb_address; |
| 43 | /* DMA Control Register(only in HISI2) */ |
| 44 | u32 dma_ctrl; |
| 45 | /* Physical address of the next fbd */ |
| 46 | u32 next_fbd_address; |
| 47 | }; |
| 48 | |
| 49 | static void set_dma_ctrl(struct fbd *fb_desc, u32 ctrl) |
| 50 | { |
| 51 | fb_desc->dma_ctrl = ctrl; |
| 52 | } |
| 53 | |
| 54 | struct isi_dma_desc { |
| 55 | struct list_head list; |
| 56 | struct fbd *p_fbd; |
Mauro Carvalho Chehab | 8f05232 | 2014-08-22 05:52:54 -0500 | [diff] [blame] | 57 | dma_addr_t fbd_phys; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /* Frame buffer data */ |
| 61 | struct frame_buffer { |
| 62 | struct vb2_buffer vb; |
| 63 | struct isi_dma_desc *p_dma_desc; |
| 64 | struct list_head list; |
| 65 | }; |
| 66 | |
| 67 | struct atmel_isi { |
| 68 | /* Protects the access of variables shared with the ISR */ |
| 69 | spinlock_t lock; |
| 70 | void __iomem *regs; |
| 71 | |
| 72 | int sequence; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 73 | |
| 74 | struct vb2_alloc_ctx *alloc_ctx; |
| 75 | |
| 76 | /* Allocate descriptors for dma buffer use */ |
| 77 | struct fbd *p_fb_descriptors; |
Mauro Carvalho Chehab | 8f05232 | 2014-08-22 05:52:54 -0500 | [diff] [blame] | 78 | dma_addr_t fb_descriptors_phys; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 79 | struct list_head dma_desc_head; |
| 80 | struct isi_dma_desc dma_desc[MAX_BUFFER_NUM]; |
| 81 | |
| 82 | struct completion complete; |
Josh Wu | d8ec096 | 2011-12-08 07:18:49 -0300 | [diff] [blame] | 83 | /* ISI peripherial clock */ |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 84 | struct clk *pclk; |
| 85 | unsigned int irq; |
| 86 | |
Josh Wu | 833e106 | 2014-07-25 07:13:39 -0300 | [diff] [blame] | 87 | struct isi_platform_data pdata; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 88 | u16 width_flags; /* max 12 bits */ |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 89 | |
| 90 | struct list_head video_buffer_list; |
| 91 | struct frame_buffer *active; |
| 92 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 93 | struct soc_camera_host soc_host; |
| 94 | }; |
| 95 | |
| 96 | static void isi_writel(struct atmel_isi *isi, u32 reg, u32 val) |
| 97 | { |
| 98 | writel(val, isi->regs + reg); |
| 99 | } |
| 100 | static u32 isi_readl(struct atmel_isi *isi, u32 reg) |
| 101 | { |
| 102 | return readl(isi->regs + reg); |
| 103 | } |
| 104 | |
| 105 | static int configure_geometry(struct atmel_isi *isi, u32 width, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 106 | u32 height, u32 code) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 107 | { |
| 108 | u32 cfg2, cr; |
| 109 | |
| 110 | switch (code) { |
| 111 | /* YUV, including grey */ |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 112 | case MEDIA_BUS_FMT_Y8_1X8: |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 113 | cr = ISI_CFG2_GRAYSCALE; |
| 114 | break; |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 115 | case MEDIA_BUS_FMT_VYUY8_2X8: |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 116 | cr = ISI_CFG2_YCC_SWAP_MODE_3; |
| 117 | break; |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 118 | case MEDIA_BUS_FMT_UYVY8_2X8: |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 119 | cr = ISI_CFG2_YCC_SWAP_MODE_2; |
| 120 | break; |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 121 | case MEDIA_BUS_FMT_YVYU8_2X8: |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 122 | cr = ISI_CFG2_YCC_SWAP_MODE_1; |
| 123 | break; |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 124 | case MEDIA_BUS_FMT_YUYV8_2X8: |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 125 | cr = ISI_CFG2_YCC_SWAP_DEFAULT; |
| 126 | break; |
| 127 | /* RGB, TODO */ |
| 128 | default: |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS); |
| 133 | |
| 134 | cfg2 = isi_readl(isi, ISI_CFG2); |
Josh Wu | bd6f274 | 2013-12-10 09:25:47 -0300 | [diff] [blame] | 135 | /* Set YCC swap mode */ |
| 136 | cfg2 &= ~ISI_CFG2_YCC_SWAP_MODE_MASK; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 137 | cfg2 |= cr; |
| 138 | /* Set width */ |
| 139 | cfg2 &= ~(ISI_CFG2_IM_HSIZE_MASK); |
| 140 | cfg2 |= ((width - 1) << ISI_CFG2_IM_HSIZE_OFFSET) & |
| 141 | ISI_CFG2_IM_HSIZE_MASK; |
| 142 | /* Set height */ |
| 143 | cfg2 &= ~(ISI_CFG2_IM_VSIZE_MASK); |
| 144 | cfg2 |= ((height - 1) << ISI_CFG2_IM_VSIZE_OFFSET) |
| 145 | & ISI_CFG2_IM_VSIZE_MASK; |
| 146 | isi_writel(isi, ISI_CFG2, cfg2); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static irqreturn_t atmel_isi_handle_streaming(struct atmel_isi *isi) |
| 152 | { |
| 153 | if (isi->active) { |
| 154 | struct vb2_buffer *vb = &isi->active->vb; |
| 155 | struct frame_buffer *buf = isi->active; |
| 156 | |
| 157 | list_del_init(&buf->list); |
Sakari Ailus | 8e6057b | 2012-09-15 15:14:42 -0300 | [diff] [blame] | 158 | v4l2_get_timestamp(&vb->v4l2_buf.timestamp); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 159 | vb->v4l2_buf.sequence = isi->sequence++; |
| 160 | vb2_buffer_done(vb, VB2_BUF_STATE_DONE); |
| 161 | } |
| 162 | |
| 163 | if (list_empty(&isi->video_buffer_list)) { |
| 164 | isi->active = NULL; |
| 165 | } else { |
| 166 | /* start next dma frame. */ |
| 167 | isi->active = list_entry(isi->video_buffer_list.next, |
| 168 | struct frame_buffer, list); |
| 169 | isi_writel(isi, ISI_DMA_C_DSCR, |
Mauro Carvalho Chehab | 8f05232 | 2014-08-22 05:52:54 -0500 | [diff] [blame] | 170 | (u32)isi->active->p_dma_desc->fbd_phys); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 171 | isi_writel(isi, ISI_DMA_C_CTRL, |
| 172 | ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE); |
| 173 | isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH); |
| 174 | } |
| 175 | return IRQ_HANDLED; |
| 176 | } |
| 177 | |
| 178 | /* ISI interrupt service routine */ |
| 179 | static irqreturn_t isi_interrupt(int irq, void *dev_id) |
| 180 | { |
| 181 | struct atmel_isi *isi = dev_id; |
| 182 | u32 status, mask, pending; |
| 183 | irqreturn_t ret = IRQ_NONE; |
| 184 | |
| 185 | spin_lock(&isi->lock); |
| 186 | |
| 187 | status = isi_readl(isi, ISI_STATUS); |
| 188 | mask = isi_readl(isi, ISI_INTMASK); |
| 189 | pending = status & mask; |
| 190 | |
| 191 | if (pending & ISI_CTRL_SRST) { |
| 192 | complete(&isi->complete); |
| 193 | isi_writel(isi, ISI_INTDIS, ISI_CTRL_SRST); |
| 194 | ret = IRQ_HANDLED; |
| 195 | } else if (pending & ISI_CTRL_DIS) { |
| 196 | complete(&isi->complete); |
| 197 | isi_writel(isi, ISI_INTDIS, ISI_CTRL_DIS); |
| 198 | ret = IRQ_HANDLED; |
| 199 | } else { |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 200 | if (likely(pending & ISI_SR_CXFR_DONE)) |
| 201 | ret = atmel_isi_handle_streaming(isi); |
| 202 | } |
| 203 | |
| 204 | spin_unlock(&isi->lock); |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | #define WAIT_ISI_RESET 1 |
| 209 | #define WAIT_ISI_DISABLE 0 |
| 210 | static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset) |
| 211 | { |
| 212 | unsigned long timeout; |
| 213 | /* |
| 214 | * The reset or disable will only succeed if we have a |
| 215 | * pixel clock from the camera. |
| 216 | */ |
| 217 | init_completion(&isi->complete); |
| 218 | |
| 219 | if (wait_reset) { |
| 220 | isi_writel(isi, ISI_INTEN, ISI_CTRL_SRST); |
| 221 | isi_writel(isi, ISI_CTRL, ISI_CTRL_SRST); |
| 222 | } else { |
| 223 | isi_writel(isi, ISI_INTEN, ISI_CTRL_DIS); |
| 224 | isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS); |
| 225 | } |
| 226 | |
| 227 | timeout = wait_for_completion_timeout(&isi->complete, |
| 228 | msecs_to_jiffies(100)); |
| 229 | if (timeout == 0) |
| 230 | return -ETIMEDOUT; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | /* ------------------------------------------------------------------ |
| 236 | Videobuf operations |
| 237 | ------------------------------------------------------------------*/ |
Guennadi Liakhovetski | fc714e7 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 238 | static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt, |
| 239 | unsigned int *nbuffers, unsigned int *nplanes, |
| 240 | unsigned int sizes[], void *alloc_ctxs[]) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 241 | { |
| 242 | struct soc_camera_device *icd = soc_camera_from_vb2q(vq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 243 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 244 | struct atmel_isi *isi = ici->priv; |
| 245 | unsigned long size; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 246 | |
Laurent Pinchart | 2b61d46e | 2012-03-21 08:03:21 -0300 | [diff] [blame] | 247 | size = icd->sizeimage; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 248 | |
| 249 | if (!*nbuffers || *nbuffers > MAX_BUFFER_NUM) |
| 250 | *nbuffers = MAX_BUFFER_NUM; |
| 251 | |
| 252 | if (size * *nbuffers > VID_LIMIT_BYTES) |
| 253 | *nbuffers = VID_LIMIT_BYTES / size; |
| 254 | |
| 255 | *nplanes = 1; |
| 256 | sizes[0] = size; |
| 257 | alloc_ctxs[0] = isi->alloc_ctx; |
| 258 | |
| 259 | isi->sequence = 0; |
| 260 | isi->active = NULL; |
| 261 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 262 | dev_dbg(icd->parent, "%s, count=%d, size=%ld\n", __func__, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 263 | *nbuffers, size); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int buffer_init(struct vb2_buffer *vb) |
| 269 | { |
| 270 | struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb); |
| 271 | |
| 272 | buf->p_dma_desc = NULL; |
| 273 | INIT_LIST_HEAD(&buf->list); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int buffer_prepare(struct vb2_buffer *vb) |
| 279 | { |
| 280 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); |
| 281 | struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 282 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 283 | struct atmel_isi *isi = ici->priv; |
| 284 | unsigned long size; |
| 285 | struct isi_dma_desc *desc; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 286 | |
Laurent Pinchart | 2b61d46e | 2012-03-21 08:03:21 -0300 | [diff] [blame] | 287 | size = icd->sizeimage; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 288 | |
| 289 | if (vb2_plane_size(vb, 0) < size) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 290 | dev_err(icd->parent, "%s data will not fit into plane (%lu < %lu)\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 291 | __func__, vb2_plane_size(vb, 0), size); |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | |
| 295 | vb2_set_plane_payload(&buf->vb, 0, size); |
| 296 | |
| 297 | if (!buf->p_dma_desc) { |
| 298 | if (list_empty(&isi->dma_desc_head)) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 299 | dev_err(icd->parent, "Not enough dma descriptors.\n"); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 300 | return -EINVAL; |
| 301 | } else { |
| 302 | /* Get an available descriptor */ |
| 303 | desc = list_entry(isi->dma_desc_head.next, |
| 304 | struct isi_dma_desc, list); |
| 305 | /* Delete the descriptor since now it is used */ |
| 306 | list_del_init(&desc->list); |
| 307 | |
| 308 | /* Initialize the dma descriptor */ |
| 309 | desc->p_fbd->fb_address = |
Marek Szyprowski | ba7fcb0 | 2011-08-29 03:20:56 -0300 | [diff] [blame] | 310 | vb2_dma_contig_plane_dma_addr(vb, 0); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 311 | desc->p_fbd->next_fbd_address = 0; |
| 312 | set_dma_ctrl(desc->p_fbd, ISI_DMA_CTRL_WB); |
| 313 | |
| 314 | buf->p_dma_desc = desc; |
| 315 | } |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static void buffer_cleanup(struct vb2_buffer *vb) |
| 321 | { |
| 322 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 323 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 324 | struct atmel_isi *isi = ici->priv; |
| 325 | struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb); |
| 326 | |
| 327 | /* This descriptor is available now and we add to head list */ |
| 328 | if (buf->p_dma_desc) |
| 329 | list_add(&buf->p_dma_desc->list, &isi->dma_desc_head); |
| 330 | } |
| 331 | |
| 332 | static void start_dma(struct atmel_isi *isi, struct frame_buffer *buffer) |
| 333 | { |
| 334 | u32 ctrl, cfg1; |
| 335 | |
| 336 | cfg1 = isi_readl(isi, ISI_CFG1); |
| 337 | /* Enable irq: cxfr for the codec path, pxfr for the preview path */ |
| 338 | isi_writel(isi, ISI_INTEN, |
| 339 | ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE); |
| 340 | |
| 341 | /* Check if already in a frame */ |
| 342 | if (isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) { |
Guennadi Liakhovetski | f7f6ce2 | 2013-04-04 08:21:12 -0300 | [diff] [blame] | 343 | dev_err(isi->soc_host.icd->parent, "Already in frame handling.\n"); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
Mauro Carvalho Chehab | 8f05232 | 2014-08-22 05:52:54 -0500 | [diff] [blame] | 347 | isi_writel(isi, ISI_DMA_C_DSCR, (u32)buffer->p_dma_desc->fbd_phys); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 348 | isi_writel(isi, ISI_DMA_C_CTRL, ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE); |
| 349 | isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH); |
| 350 | |
Josh Wu | bd6f274 | 2013-12-10 09:25:47 -0300 | [diff] [blame] | 351 | cfg1 &= ~ISI_CFG1_FRATE_DIV_MASK; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 352 | /* Enable linked list */ |
Josh Wu | 833e106 | 2014-07-25 07:13:39 -0300 | [diff] [blame] | 353 | cfg1 |= isi->pdata.frate | ISI_CFG1_DISCR; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 354 | |
| 355 | /* Enable codec path and ISI */ |
| 356 | ctrl = ISI_CTRL_CDC | ISI_CTRL_EN; |
| 357 | isi_writel(isi, ISI_CTRL, ctrl); |
| 358 | isi_writel(isi, ISI_CFG1, cfg1); |
| 359 | } |
| 360 | |
| 361 | static void buffer_queue(struct vb2_buffer *vb) |
| 362 | { |
| 363 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 364 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 365 | struct atmel_isi *isi = ici->priv; |
| 366 | struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb); |
| 367 | unsigned long flags = 0; |
| 368 | |
| 369 | spin_lock_irqsave(&isi->lock, flags); |
| 370 | list_add_tail(&buf->list, &isi->video_buffer_list); |
| 371 | |
| 372 | if (isi->active == NULL) { |
| 373 | isi->active = buf; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 374 | if (vb2_is_streaming(vb->vb2_queue)) |
| 375 | start_dma(isi, buf); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 376 | } |
| 377 | spin_unlock_irqrestore(&isi->lock, flags); |
| 378 | } |
| 379 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 380 | static int start_streaming(struct vb2_queue *vq, unsigned int count) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 381 | { |
| 382 | struct soc_camera_device *icd = soc_camera_from_vb2q(vq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 383 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 384 | struct atmel_isi *isi = ici->priv; |
Laurent Pinchart | c768626 | 2013-11-02 21:25:06 -0300 | [diff] [blame] | 385 | int ret; |
| 386 | |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 387 | pm_runtime_get_sync(ici->v4l2_dev.dev); |
| 388 | |
Laurent Pinchart | c768626 | 2013-11-02 21:25:06 -0300 | [diff] [blame] | 389 | /* Reset ISI */ |
| 390 | ret = atmel_isi_wait_status(isi, WAIT_ISI_RESET); |
| 391 | if (ret < 0) { |
| 392 | dev_err(icd->parent, "Reset ISI timed out\n"); |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 393 | pm_runtime_put(ici->v4l2_dev.dev); |
Laurent Pinchart | c768626 | 2013-11-02 21:25:06 -0300 | [diff] [blame] | 394 | return ret; |
| 395 | } |
| 396 | /* Disable all interrupts */ |
Mauro Carvalho Chehab | 9842a41 | 2014-08-22 05:53:27 -0500 | [diff] [blame] | 397 | isi_writel(isi, ISI_INTDIS, (u32)~0UL); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 398 | |
| 399 | spin_lock_irq(&isi->lock); |
Josh Wu | 1426f61b | 2013-10-24 04:27:11 -0300 | [diff] [blame] | 400 | /* Clear any pending interrupt */ |
Mauro Carvalho Chehab | b91677a | 2014-08-26 11:21:43 -0300 | [diff] [blame] | 401 | isi_readl(isi, ISI_STATUS); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 402 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 403 | if (count) |
| 404 | start_dma(isi, isi->active); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 405 | spin_unlock_irq(&isi->lock); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /* abort streaming and wait for last buffer */ |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 411 | static void stop_streaming(struct vb2_queue *vq) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 412 | { |
| 413 | struct soc_camera_device *icd = soc_camera_from_vb2q(vq); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 414 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 415 | struct atmel_isi *isi = ici->priv; |
| 416 | struct frame_buffer *buf, *node; |
| 417 | int ret = 0; |
| 418 | unsigned long timeout; |
| 419 | |
| 420 | spin_lock_irq(&isi->lock); |
| 421 | isi->active = NULL; |
| 422 | /* Release all active buffers */ |
| 423 | list_for_each_entry_safe(buf, node, &isi->video_buffer_list, list) { |
| 424 | list_del_init(&buf->list); |
| 425 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 426 | } |
| 427 | spin_unlock_irq(&isi->lock); |
| 428 | |
| 429 | timeout = jiffies + FRAME_INTERVAL_MILLI_SEC * HZ; |
| 430 | /* Wait until the end of the current frame. */ |
| 431 | while ((isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) && |
| 432 | time_before(jiffies, timeout)) |
| 433 | msleep(1); |
| 434 | |
Josh Wu | 8c03783 | 2015-05-26 06:54:45 -0300 | [diff] [blame] | 435 | if (time_after(jiffies, timeout)) |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 436 | dev_err(icd->parent, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 437 | "Timeout waiting for finishing codec request\n"); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 438 | |
| 439 | /* Disable interrupts */ |
| 440 | isi_writel(isi, ISI_INTDIS, |
| 441 | ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE); |
| 442 | |
| 443 | /* Disable ISI and wait for it is done */ |
| 444 | ret = atmel_isi_wait_status(isi, WAIT_ISI_DISABLE); |
| 445 | if (ret < 0) |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 446 | dev_err(icd->parent, "Disable ISI timed out\n"); |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 447 | |
| 448 | pm_runtime_put(ici->v4l2_dev.dev); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static struct vb2_ops isi_video_qops = { |
| 452 | .queue_setup = queue_setup, |
| 453 | .buf_init = buffer_init, |
| 454 | .buf_prepare = buffer_prepare, |
| 455 | .buf_cleanup = buffer_cleanup, |
| 456 | .buf_queue = buffer_queue, |
| 457 | .start_streaming = start_streaming, |
| 458 | .stop_streaming = stop_streaming, |
Lad, Prabhakar | 976036d | 2014-11-26 19:42:27 -0300 | [diff] [blame] | 459 | .wait_prepare = vb2_ops_wait_prepare, |
| 460 | .wait_finish = vb2_ops_wait_finish, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | /* ------------------------------------------------------------------ |
| 464 | SOC camera operations for the device |
| 465 | ------------------------------------------------------------------*/ |
| 466 | static int isi_camera_init_videobuf(struct vb2_queue *q, |
| 467 | struct soc_camera_device *icd) |
| 468 | { |
Lad, Prabhakar | 976036d | 2014-11-26 19:42:27 -0300 | [diff] [blame] | 469 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
| 470 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 471 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 472 | q->io_modes = VB2_MMAP; |
| 473 | q->drv_priv = icd; |
| 474 | q->buf_struct_size = sizeof(struct frame_buffer); |
| 475 | q->ops = &isi_video_qops; |
| 476 | q->mem_ops = &vb2_dma_contig_memops; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 477 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
Lad, Prabhakar | 976036d | 2014-11-26 19:42:27 -0300 | [diff] [blame] | 478 | q->lock = &ici->host_lock; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 479 | |
| 480 | return vb2_queue_init(q); |
| 481 | } |
| 482 | |
| 483 | static int isi_camera_set_fmt(struct soc_camera_device *icd, |
| 484 | struct v4l2_format *f) |
| 485 | { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 486 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 487 | struct atmel_isi *isi = ici->priv; |
| 488 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 489 | const struct soc_camera_format_xlate *xlate; |
| 490 | struct v4l2_pix_format *pix = &f->fmt.pix; |
Hans Verkuil | ebf984b | 2015-04-09 04:05:59 -0300 | [diff] [blame] | 491 | struct v4l2_subdev_format format = { |
| 492 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, |
| 493 | }; |
| 494 | struct v4l2_mbus_framefmt *mf = &format.format; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 495 | int ret; |
| 496 | |
| 497 | xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat); |
| 498 | if (!xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 499 | dev_warn(icd->parent, "Format %x not found\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 500 | pix->pixelformat); |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 504 | dev_dbg(icd->parent, "Plan to set format %dx%d\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 505 | pix->width, pix->height); |
| 506 | |
Hans Verkuil | ebf984b | 2015-04-09 04:05:59 -0300 | [diff] [blame] | 507 | mf->width = pix->width; |
| 508 | mf->height = pix->height; |
| 509 | mf->field = pix->field; |
| 510 | mf->colorspace = pix->colorspace; |
| 511 | mf->code = xlate->code; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 512 | |
Hans Verkuil | ebf984b | 2015-04-09 04:05:59 -0300 | [diff] [blame] | 513 | ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &format); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 514 | if (ret < 0) |
| 515 | return ret; |
| 516 | |
Hans Verkuil | ebf984b | 2015-04-09 04:05:59 -0300 | [diff] [blame] | 517 | if (mf->code != xlate->code) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 518 | return -EINVAL; |
| 519 | |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 520 | /* Enable PM and peripheral clock before operate isi registers */ |
| 521 | pm_runtime_get_sync(ici->v4l2_dev.dev); |
| 522 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 523 | ret = configure_geometry(isi, pix->width, pix->height, xlate->code); |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 524 | |
| 525 | pm_runtime_put(ici->v4l2_dev.dev); |
| 526 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 527 | if (ret < 0) |
| 528 | return ret; |
| 529 | |
Hans Verkuil | ebf984b | 2015-04-09 04:05:59 -0300 | [diff] [blame] | 530 | pix->width = mf->width; |
| 531 | pix->height = mf->height; |
| 532 | pix->field = mf->field; |
| 533 | pix->colorspace = mf->colorspace; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 534 | icd->current_fmt = xlate; |
| 535 | |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 536 | dev_dbg(icd->parent, "Finally set format %dx%d\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 537 | pix->width, pix->height); |
| 538 | |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | static int isi_camera_try_fmt(struct soc_camera_device *icd, |
| 543 | struct v4l2_format *f) |
| 544 | { |
| 545 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 546 | const struct soc_camera_format_xlate *xlate; |
| 547 | struct v4l2_pix_format *pix = &f->fmt.pix; |
Hans Verkuil | 5eab498 | 2015-04-09 04:05:35 -0300 | [diff] [blame] | 548 | struct v4l2_subdev_pad_config pad_cfg; |
| 549 | struct v4l2_subdev_format format = { |
| 550 | .which = V4L2_SUBDEV_FORMAT_TRY, |
| 551 | }; |
| 552 | struct v4l2_mbus_framefmt *mf = &format.format; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 553 | u32 pixfmt = pix->pixelformat; |
| 554 | int ret; |
| 555 | |
| 556 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); |
| 557 | if (pixfmt && !xlate) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 558 | dev_warn(icd->parent, "Format %x not found\n", pixfmt); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 559 | return -EINVAL; |
| 560 | } |
| 561 | |
| 562 | /* limit to Atmel ISI hardware capabilities */ |
| 563 | if (pix->height > MAX_SUPPORT_HEIGHT) |
| 564 | pix->height = MAX_SUPPORT_HEIGHT; |
| 565 | if (pix->width > MAX_SUPPORT_WIDTH) |
| 566 | pix->width = MAX_SUPPORT_WIDTH; |
| 567 | |
| 568 | /* limit to sensor capabilities */ |
Hans Verkuil | 5eab498 | 2015-04-09 04:05:35 -0300 | [diff] [blame] | 569 | mf->width = pix->width; |
| 570 | mf->height = pix->height; |
| 571 | mf->field = pix->field; |
| 572 | mf->colorspace = pix->colorspace; |
| 573 | mf->code = xlate->code; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 574 | |
Hans Verkuil | 5eab498 | 2015-04-09 04:05:35 -0300 | [diff] [blame] | 575 | ret = v4l2_subdev_call(sd, pad, set_fmt, &pad_cfg, &format); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 576 | if (ret < 0) |
| 577 | return ret; |
| 578 | |
Hans Verkuil | 5eab498 | 2015-04-09 04:05:35 -0300 | [diff] [blame] | 579 | pix->width = mf->width; |
| 580 | pix->height = mf->height; |
| 581 | pix->colorspace = mf->colorspace; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 582 | |
Hans Verkuil | 5eab498 | 2015-04-09 04:05:35 -0300 | [diff] [blame] | 583 | switch (mf->field) { |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 584 | case V4L2_FIELD_ANY: |
| 585 | pix->field = V4L2_FIELD_NONE; |
| 586 | break; |
| 587 | case V4L2_FIELD_NONE: |
| 588 | break; |
| 589 | default: |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 590 | dev_err(icd->parent, "Field type %d unsupported.\n", |
Hans Verkuil | 5eab498 | 2015-04-09 04:05:35 -0300 | [diff] [blame] | 591 | mf->field); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 592 | ret = -EINVAL; |
| 593 | } |
| 594 | |
| 595 | return ret; |
| 596 | } |
| 597 | |
| 598 | static const struct soc_mbus_pixelfmt isi_camera_formats[] = { |
| 599 | { |
| 600 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 601 | .name = "Packed YUV422 16 bit", |
| 602 | .bits_per_sample = 8, |
| 603 | .packing = SOC_MBUS_PACKING_2X8_PADHI, |
| 604 | .order = SOC_MBUS_ORDER_LE, |
Laurent Pinchart | ad3b81f | 2012-03-21 08:03:23 -0300 | [diff] [blame] | 605 | .layout = SOC_MBUS_LAYOUT_PACKED, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 606 | }, |
| 607 | }; |
| 608 | |
| 609 | /* This will be corrected as we get more formats */ |
| 610 | static bool isi_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt) |
| 611 | { |
| 612 | return fmt->packing == SOC_MBUS_PACKING_NONE || |
| 613 | (fmt->bits_per_sample == 8 && |
| 614 | fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) || |
| 615 | (fmt->bits_per_sample > 8 && |
| 616 | fmt->packing == SOC_MBUS_PACKING_EXTEND16); |
| 617 | } |
| 618 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 619 | #define ISI_BUS_PARAM (V4L2_MBUS_MASTER | \ |
| 620 | V4L2_MBUS_HSYNC_ACTIVE_HIGH | \ |
| 621 | V4L2_MBUS_HSYNC_ACTIVE_LOW | \ |
| 622 | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \ |
| 623 | V4L2_MBUS_VSYNC_ACTIVE_LOW | \ |
| 624 | V4L2_MBUS_PCLK_SAMPLE_RISING | \ |
| 625 | V4L2_MBUS_PCLK_SAMPLE_FALLING | \ |
| 626 | V4L2_MBUS_DATA_ACTIVE_HIGH) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 627 | |
| 628 | static int isi_camera_try_bus_param(struct soc_camera_device *icd, |
| 629 | unsigned char buswidth) |
| 630 | { |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 631 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 632 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 633 | struct atmel_isi *isi = ici->priv; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 634 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
| 635 | unsigned long common_flags; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 636 | int ret; |
| 637 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 638 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); |
| 639 | if (!ret) { |
| 640 | common_flags = soc_mbus_config_compatible(&cfg, |
| 641 | ISI_BUS_PARAM); |
| 642 | if (!common_flags) { |
| 643 | dev_warn(icd->parent, |
| 644 | "Flags incompatible: camera 0x%x, host 0x%x\n", |
| 645 | cfg.flags, ISI_BUS_PARAM); |
| 646 | return -EINVAL; |
| 647 | } |
| 648 | } else if (ret != -ENOIOCTLCMD) { |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | if ((1 << (buswidth - 1)) & isi->width_flags) |
| 653 | return 0; |
| 654 | return -EINVAL; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | |
| 658 | static int isi_camera_get_formats(struct soc_camera_device *icd, |
| 659 | unsigned int idx, |
| 660 | struct soc_camera_format_xlate *xlate) |
| 661 | { |
| 662 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
| 663 | int formats = 0, ret; |
| 664 | /* sensor format */ |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 665 | struct v4l2_subdev_mbus_code_enum code = { |
| 666 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, |
| 667 | .index = idx, |
| 668 | }; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 669 | /* soc camera host format */ |
| 670 | const struct soc_mbus_pixelfmt *fmt; |
| 671 | |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 672 | ret = v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 673 | if (ret < 0) |
| 674 | /* No more formats */ |
| 675 | return 0; |
| 676 | |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 677 | fmt = soc_mbus_get_fmtdesc(code.code); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 678 | if (!fmt) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 679 | dev_err(icd->parent, |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 680 | "Invalid format code #%u: %d\n", idx, code.code); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | /* This also checks support for the requested bits-per-sample */ |
| 685 | ret = isi_camera_try_bus_param(icd, fmt->bits_per_sample); |
| 686 | if (ret < 0) { |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 687 | dev_err(icd->parent, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 688 | "Fail to try the bus parameters.\n"); |
| 689 | return 0; |
| 690 | } |
| 691 | |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 692 | switch (code.code) { |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 693 | case MEDIA_BUS_FMT_UYVY8_2X8: |
| 694 | case MEDIA_BUS_FMT_VYUY8_2X8: |
| 695 | case MEDIA_BUS_FMT_YUYV8_2X8: |
| 696 | case MEDIA_BUS_FMT_YVYU8_2X8: |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 697 | formats++; |
| 698 | if (xlate) { |
| 699 | xlate->host_fmt = &isi_camera_formats[0]; |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 700 | xlate->code = code.code; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 701 | xlate++; |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 702 | dev_dbg(icd->parent, "Providing format %s using code %d\n", |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 703 | isi_camera_formats[0].name, code.code); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 704 | } |
| 705 | break; |
| 706 | default: |
| 707 | if (!isi_camera_packing_supported(fmt)) |
| 708 | return 0; |
| 709 | if (xlate) |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 710 | dev_dbg(icd->parent, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 711 | "Providing format %s in pass-through mode\n", |
| 712 | fmt->name); |
| 713 | } |
| 714 | |
| 715 | /* Generic pass-through */ |
| 716 | formats++; |
| 717 | if (xlate) { |
| 718 | xlate->host_fmt = fmt; |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 719 | xlate->code = code.code; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 720 | xlate++; |
| 721 | } |
| 722 | |
| 723 | return formats; |
| 724 | } |
| 725 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 726 | static int isi_camera_add_device(struct soc_camera_device *icd) |
| 727 | { |
Guennadi Liakhovetski | ffebad7 | 2013-04-04 09:56:09 -0300 | [diff] [blame] | 728 | dev_dbg(icd->parent, "Atmel ISI Camera driver attached to camera %d\n", |
| 729 | icd->devnum); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static void isi_camera_remove_device(struct soc_camera_device *icd) |
| 735 | { |
| 736 | dev_dbg(icd->parent, "Atmel ISI Camera driver detached from camera %d\n", |
| 737 | icd->devnum); |
| 738 | } |
| 739 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 740 | static unsigned int isi_camera_poll(struct file *file, poll_table *pt) |
| 741 | { |
| 742 | struct soc_camera_device *icd = file->private_data; |
| 743 | |
| 744 | return vb2_poll(&icd->vb2_vidq, file, pt); |
| 745 | } |
| 746 | |
| 747 | static int isi_camera_querycap(struct soc_camera_host *ici, |
| 748 | struct v4l2_capability *cap) |
| 749 | { |
| 750 | strcpy(cap->driver, "atmel-isi"); |
| 751 | strcpy(cap->card, "Atmel Image Sensor Interface"); |
Guennadi Liakhovetski | 7d96c3e | 2015-01-18 16:30:11 -0300 | [diff] [blame] | 752 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 753 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
| 754 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 755 | return 0; |
| 756 | } |
| 757 | |
Guennadi Liakhovetski | 8843d11 | 2011-09-21 17:52:51 -0300 | [diff] [blame] | 758 | static int isi_camera_set_bus_param(struct soc_camera_device *icd) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 759 | { |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 760 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); |
Guennadi Liakhovetski | 7dfff95 | 2011-07-15 20:03:38 -0300 | [diff] [blame] | 761 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 762 | struct atmel_isi *isi = ici->priv; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 763 | struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; |
| 764 | unsigned long common_flags; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 765 | int ret; |
| 766 | u32 cfg1 = 0; |
| 767 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 768 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); |
| 769 | if (!ret) { |
| 770 | common_flags = soc_mbus_config_compatible(&cfg, |
| 771 | ISI_BUS_PARAM); |
| 772 | if (!common_flags) { |
| 773 | dev_warn(icd->parent, |
| 774 | "Flags incompatible: camera 0x%x, host 0x%x\n", |
| 775 | cfg.flags, ISI_BUS_PARAM); |
| 776 | return -EINVAL; |
| 777 | } |
| 778 | } else if (ret != -ENOIOCTLCMD) { |
| 779 | return ret; |
| 780 | } else { |
| 781 | common_flags = ISI_BUS_PARAM; |
| 782 | } |
| 783 | dev_dbg(icd->parent, "Flags cam: 0x%x host: 0x%x common: 0x%lx\n", |
| 784 | cfg.flags, ISI_BUS_PARAM, common_flags); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 785 | |
| 786 | /* Make choises, based on platform preferences */ |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 787 | if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) && |
| 788 | (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) { |
Josh Wu | 833e106 | 2014-07-25 07:13:39 -0300 | [diff] [blame] | 789 | if (isi->pdata.hsync_act_low) |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 790 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 791 | else |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 792 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 793 | } |
| 794 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 795 | if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) && |
| 796 | (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) { |
Josh Wu | 833e106 | 2014-07-25 07:13:39 -0300 | [diff] [blame] | 797 | if (isi->pdata.vsync_act_low) |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 798 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 799 | else |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 800 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 801 | } |
| 802 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 803 | if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) && |
| 804 | (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) { |
Josh Wu | 833e106 | 2014-07-25 07:13:39 -0300 | [diff] [blame] | 805 | if (isi->pdata.pclk_act_falling) |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 806 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 807 | else |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 808 | common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 809 | } |
| 810 | |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 811 | cfg.flags = common_flags; |
| 812 | ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg); |
| 813 | if (ret < 0 && ret != -ENOIOCTLCMD) { |
| 814 | dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n", |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 815 | common_flags, ret); |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | /* set bus param for ISI */ |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 820 | if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 821 | cfg1 |= ISI_CFG1_HSYNC_POL_ACTIVE_LOW; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 822 | if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 823 | cfg1 |= ISI_CFG1_VSYNC_POL_ACTIVE_LOW; |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 824 | if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 825 | cfg1 |= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING; |
| 826 | |
Josh Wu | 833e106 | 2014-07-25 07:13:39 -0300 | [diff] [blame] | 827 | if (isi->pdata.has_emb_sync) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 828 | cfg1 |= ISI_CFG1_EMB_SYNC; |
Josh Wu | 833e106 | 2014-07-25 07:13:39 -0300 | [diff] [blame] | 829 | if (isi->pdata.full_mode) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 830 | cfg1 |= ISI_CFG1_FULL_MODE; |
| 831 | |
Josh Wu | ce037f1 | 2014-11-25 06:30:25 -0300 | [diff] [blame] | 832 | cfg1 |= ISI_CFG1_THMASK_BEATS_16; |
| 833 | |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 834 | /* Enable PM and peripheral clock before operate isi registers */ |
| 835 | pm_runtime_get_sync(ici->v4l2_dev.dev); |
| 836 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 837 | isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS); |
| 838 | isi_writel(isi, ISI_CFG1, cfg1); |
| 839 | |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 840 | pm_runtime_put(ici->v4l2_dev.dev); |
| 841 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | static struct soc_camera_host_ops isi_soc_camera_host_ops = { |
| 846 | .owner = THIS_MODULE, |
| 847 | .add = isi_camera_add_device, |
| 848 | .remove = isi_camera_remove_device, |
| 849 | .set_fmt = isi_camera_set_fmt, |
| 850 | .try_fmt = isi_camera_try_fmt, |
| 851 | .get_formats = isi_camera_get_formats, |
| 852 | .init_videobuf2 = isi_camera_init_videobuf, |
| 853 | .poll = isi_camera_poll, |
| 854 | .querycap = isi_camera_querycap, |
| 855 | .set_bus_param = isi_camera_set_bus_param, |
| 856 | }; |
| 857 | |
| 858 | /* -----------------------------------------------------------------------*/ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 859 | static int atmel_isi_remove(struct platform_device *pdev) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 860 | { |
| 861 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); |
| 862 | struct atmel_isi *isi = container_of(soc_host, |
| 863 | struct atmel_isi, soc_host); |
| 864 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 865 | soc_camera_host_unregister(soc_host); |
| 866 | vb2_dma_contig_cleanup_ctx(isi->alloc_ctx); |
| 867 | dma_free_coherent(&pdev->dev, |
| 868 | sizeof(struct fbd) * MAX_BUFFER_NUM, |
| 869 | isi->p_fb_descriptors, |
| 870 | isi->fb_descriptors_phys); |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 871 | pm_runtime_disable(&pdev->dev); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 872 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 873 | return 0; |
| 874 | } |
| 875 | |
Josh Wu | 8ff19bc | 2014-07-28 04:25:17 -0300 | [diff] [blame] | 876 | static int atmel_isi_probe_dt(struct atmel_isi *isi, |
| 877 | struct platform_device *pdev) |
| 878 | { |
| 879 | struct device_node *np= pdev->dev.of_node; |
| 880 | struct v4l2_of_endpoint ep; |
| 881 | int err; |
| 882 | |
| 883 | /* Default settings for ISI */ |
| 884 | isi->pdata.full_mode = 1; |
Josh Wu | 8ff19bc | 2014-07-28 04:25:17 -0300 | [diff] [blame] | 885 | isi->pdata.frate = ISI_CFG1_FRATE_CAPTURE_ALL; |
| 886 | |
| 887 | np = of_graph_get_next_endpoint(np, NULL); |
| 888 | if (!np) { |
| 889 | dev_err(&pdev->dev, "Could not find the endpoint\n"); |
| 890 | return -EINVAL; |
| 891 | } |
| 892 | |
| 893 | err = v4l2_of_parse_endpoint(np, &ep); |
| 894 | if (err) { |
| 895 | dev_err(&pdev->dev, "Could not parse the endpoint\n"); |
| 896 | goto err_probe_dt; |
| 897 | } |
| 898 | |
| 899 | switch (ep.bus.parallel.bus_width) { |
| 900 | case 8: |
| 901 | isi->pdata.data_width_flags = ISI_DATAWIDTH_8; |
| 902 | break; |
| 903 | case 10: |
| 904 | isi->pdata.data_width_flags = |
| 905 | ISI_DATAWIDTH_8 | ISI_DATAWIDTH_10; |
| 906 | break; |
| 907 | default: |
| 908 | dev_err(&pdev->dev, "Unsupported bus width: %d\n", |
| 909 | ep.bus.parallel.bus_width); |
| 910 | err = -EINVAL; |
| 911 | goto err_probe_dt; |
| 912 | } |
| 913 | |
| 914 | err_probe_dt: |
| 915 | of_node_put(np); |
| 916 | |
| 917 | return err; |
| 918 | } |
| 919 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 920 | static int atmel_isi_probe(struct platform_device *pdev) |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 921 | { |
| 922 | unsigned int irq; |
| 923 | struct atmel_isi *isi; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 924 | struct resource *regs; |
| 925 | int ret, i; |
| 926 | struct device *dev = &pdev->dev; |
| 927 | struct soc_camera_host *soc_host; |
| 928 | struct isi_platform_data *pdata; |
| 929 | |
| 930 | pdata = dev->platform_data; |
Josh Wu | 8ff19bc | 2014-07-28 04:25:17 -0300 | [diff] [blame] | 931 | if ((!pdata || !pdata->data_width_flags) && !pdev->dev.of_node) { |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 932 | dev_err(&pdev->dev, |
| 933 | "No config available for Atmel ISI\n"); |
| 934 | return -EINVAL; |
| 935 | } |
| 936 | |
Laurent Pinchart | c52c0cb | 2013-11-25 12:13:50 -0300 | [diff] [blame] | 937 | isi = devm_kzalloc(&pdev->dev, sizeof(struct atmel_isi), GFP_KERNEL); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 938 | if (!isi) { |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 939 | dev_err(&pdev->dev, "Can't allocate interface!\n"); |
Laurent Pinchart | c52c0cb | 2013-11-25 12:13:50 -0300 | [diff] [blame] | 940 | return -ENOMEM; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 941 | } |
| 942 | |
Laurent Pinchart | c52c0cb | 2013-11-25 12:13:50 -0300 | [diff] [blame] | 943 | isi->pclk = devm_clk_get(&pdev->dev, "isi_clk"); |
| 944 | if (IS_ERR(isi->pclk)) |
| 945 | return PTR_ERR(isi->pclk); |
| 946 | |
Josh Wu | 8ff19bc | 2014-07-28 04:25:17 -0300 | [diff] [blame] | 947 | if (pdata) { |
| 948 | memcpy(&isi->pdata, pdata, sizeof(isi->pdata)); |
| 949 | } else { |
| 950 | ret = atmel_isi_probe_dt(isi, pdev); |
| 951 | if (ret) |
| 952 | return ret; |
| 953 | } |
| 954 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 955 | isi->active = NULL; |
| 956 | spin_lock_init(&isi->lock); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 957 | INIT_LIST_HEAD(&isi->video_buffer_list); |
| 958 | INIT_LIST_HEAD(&isi->dma_desc_head); |
| 959 | |
| 960 | isi->p_fb_descriptors = dma_alloc_coherent(&pdev->dev, |
| 961 | sizeof(struct fbd) * MAX_BUFFER_NUM, |
| 962 | &isi->fb_descriptors_phys, |
| 963 | GFP_KERNEL); |
| 964 | if (!isi->p_fb_descriptors) { |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 965 | dev_err(&pdev->dev, "Can't allocate descriptors!\n"); |
Laurent Pinchart | c01d568 | 2013-11-25 12:21:33 -0300 | [diff] [blame] | 966 | return -ENOMEM; |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | for (i = 0; i < MAX_BUFFER_NUM; i++) { |
| 970 | isi->dma_desc[i].p_fbd = isi->p_fb_descriptors + i; |
| 971 | isi->dma_desc[i].fbd_phys = isi->fb_descriptors_phys + |
| 972 | i * sizeof(struct fbd); |
| 973 | list_add(&isi->dma_desc[i].list, &isi->dma_desc_head); |
| 974 | } |
| 975 | |
| 976 | isi->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 977 | if (IS_ERR(isi->alloc_ctx)) { |
| 978 | ret = PTR_ERR(isi->alloc_ctx); |
| 979 | goto err_alloc_ctx; |
| 980 | } |
| 981 | |
Laurent Pinchart | c52c0cb | 2013-11-25 12:13:50 -0300 | [diff] [blame] | 982 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 983 | isi->regs = devm_ioremap_resource(&pdev->dev, regs); |
| 984 | if (IS_ERR(isi->regs)) { |
| 985 | ret = PTR_ERR(isi->regs); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 986 | goto err_ioremap; |
| 987 | } |
| 988 | |
Josh Wu | 833e106 | 2014-07-25 07:13:39 -0300 | [diff] [blame] | 989 | if (isi->pdata.data_width_flags & ISI_DATAWIDTH_8) |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 990 | isi->width_flags = 1 << 7; |
Josh Wu | 833e106 | 2014-07-25 07:13:39 -0300 | [diff] [blame] | 991 | if (isi->pdata.data_width_flags & ISI_DATAWIDTH_10) |
Guennadi Liakhovetski | a4e9f10 | 2011-07-27 12:18:37 -0300 | [diff] [blame] | 992 | isi->width_flags |= 1 << 9; |
| 993 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 994 | irq = platform_get_irq(pdev, 0); |
Tushar Behera | e35abd4 | 2012-11-16 03:50:37 -0300 | [diff] [blame] | 995 | if (IS_ERR_VALUE(irq)) { |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 996 | ret = irq; |
| 997 | goto err_req_irq; |
| 998 | } |
| 999 | |
Laurent Pinchart | c52c0cb | 2013-11-25 12:13:50 -0300 | [diff] [blame] | 1000 | ret = devm_request_irq(&pdev->dev, irq, isi_interrupt, 0, "isi", isi); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1001 | if (ret) { |
| 1002 | dev_err(&pdev->dev, "Unable to request irq %d\n", irq); |
| 1003 | goto err_req_irq; |
| 1004 | } |
| 1005 | isi->irq = irq; |
| 1006 | |
| 1007 | soc_host = &isi->soc_host; |
| 1008 | soc_host->drv_name = "isi-camera"; |
| 1009 | soc_host->ops = &isi_soc_camera_host_ops; |
| 1010 | soc_host->priv = isi; |
| 1011 | soc_host->v4l2_dev.dev = &pdev->dev; |
| 1012 | soc_host->nr = pdev->id; |
| 1013 | |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 1014 | pm_suspend_ignore_children(&pdev->dev, true); |
| 1015 | pm_runtime_enable(&pdev->dev); |
| 1016 | |
Josh Wu | 3900623 | 2014-07-25 07:13:38 -0300 | [diff] [blame] | 1017 | if (isi->pdata.asd_sizes) { |
| 1018 | soc_host->asd = isi->pdata.asd; |
| 1019 | soc_host->asd_sizes = isi->pdata.asd_sizes; |
| 1020 | } |
| 1021 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1022 | ret = soc_camera_host_register(soc_host); |
| 1023 | if (ret) { |
| 1024 | dev_err(&pdev->dev, "Unable to register soc camera host\n"); |
| 1025 | goto err_register_soc_camera_host; |
| 1026 | } |
| 1027 | return 0; |
| 1028 | |
| 1029 | err_register_soc_camera_host: |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 1030 | pm_runtime_disable(&pdev->dev); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1031 | err_req_irq: |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1032 | err_ioremap: |
| 1033 | vb2_dma_contig_cleanup_ctx(isi->alloc_ctx); |
| 1034 | err_alloc_ctx: |
| 1035 | dma_free_coherent(&pdev->dev, |
| 1036 | sizeof(struct fbd) * MAX_BUFFER_NUM, |
| 1037 | isi->p_fb_descriptors, |
| 1038 | isi->fb_descriptors_phys); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1039 | |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 1043 | static int atmel_isi_runtime_suspend(struct device *dev) |
| 1044 | { |
| 1045 | struct soc_camera_host *soc_host = to_soc_camera_host(dev); |
| 1046 | struct atmel_isi *isi = container_of(soc_host, |
| 1047 | struct atmel_isi, soc_host); |
| 1048 | |
| 1049 | clk_disable_unprepare(isi->pclk); |
| 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | static int atmel_isi_runtime_resume(struct device *dev) |
| 1054 | { |
| 1055 | struct soc_camera_host *soc_host = to_soc_camera_host(dev); |
| 1056 | struct atmel_isi *isi = container_of(soc_host, |
| 1057 | struct atmel_isi, soc_host); |
| 1058 | |
| 1059 | return clk_prepare_enable(isi->pclk); |
| 1060 | } |
| 1061 | |
| 1062 | static const struct dev_pm_ops atmel_isi_dev_pm_ops = { |
| 1063 | SET_RUNTIME_PM_OPS(atmel_isi_runtime_suspend, |
| 1064 | atmel_isi_runtime_resume, NULL) |
| 1065 | }; |
| 1066 | |
Josh Wu | 8ff19bc | 2014-07-28 04:25:17 -0300 | [diff] [blame] | 1067 | static const struct of_device_id atmel_isi_of_match[] = { |
| 1068 | { .compatible = "atmel,at91sam9g45-isi" }, |
| 1069 | { } |
| 1070 | }; |
| 1071 | MODULE_DEVICE_TABLE(of, atmel_isi_of_match); |
| 1072 | |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1073 | static struct platform_driver atmel_isi_driver = { |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1074 | .remove = atmel_isi_remove, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1075 | .driver = { |
| 1076 | .name = "atmel_isi", |
Josh Wu | 8ff19bc | 2014-07-28 04:25:17 -0300 | [diff] [blame] | 1077 | .of_match_table = of_match_ptr(atmel_isi_of_match), |
Josh Wu | f3745a3a | 2015-05-26 06:54:46 -0300 | [diff] [blame] | 1078 | .pm = &atmel_isi_dev_pm_ops, |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1079 | }, |
| 1080 | }; |
| 1081 | |
Fabio Porcedda | 8c31aec | 2013-03-14 14:09:31 -0300 | [diff] [blame] | 1082 | module_platform_driver_probe(atmel_isi_driver, atmel_isi_probe); |
Josh Wu | 195ebc4 | 2011-06-07 22:40:19 -0300 | [diff] [blame] | 1083 | |
| 1084 | MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>"); |
| 1085 | MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux"); |
| 1086 | MODULE_LICENSE("GPL"); |
| 1087 | MODULE_SUPPORTED_DEVICE("video"); |