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