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