Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]

Diff markup

Differences between /linux/drivers/media/video/pxa_camera.c (Version 2.6.31.13) and /linux/drivers/media/video/pxa_camera.c (Version 2.6.11.8)


  1 /*                                                  1 
  2  * V4L2 Driver for PXA camera host                
  3  *                                                
  4  * Copyright (C) 2006, Sascha Hauer, Pengutron    
  5  * Copyright (C) 2008, Guennadi Liakhovetski <    
  6  *                                                
  7  * This program is free software; you can redi    
  8  * it under the terms of the GNU General Publi    
  9  * the Free Software Foundation; either versio    
 10  * (at your option) any later version.            
 11  */                                               
 12                                                   
 13 #include <linux/init.h>                           
 14 #include <linux/module.h>                         
 15 #include <linux/io.h>                             
 16 #include <linux/delay.h>                          
 17 #include <linux/dma-mapping.h>                    
 18 #include <linux/errno.h>                          
 19 #include <linux/fs.h>                             
 20 #include <linux/interrupt.h>                      
 21 #include <linux/kernel.h>                         
 22 #include <linux/mm.h>                             
 23 #include <linux/moduleparam.h>                    
 24 #include <linux/time.h>                           
 25 #include <linux/version.h>                        
 26 #include <linux/device.h>                         
 27 #include <linux/platform_device.h>                
 28 #include <linux/clk.h>                            
 29                                                   
 30 #include <media/v4l2-common.h>                    
 31 #include <media/v4l2-dev.h>                       
 32 #include <media/videobuf-dma-sg.h>                
 33 #include <media/soc_camera.h>                     
 34                                                   
 35 #include <linux/videodev2.h>                      
 36                                                   
 37 #include <mach/dma.h>                             
 38 #include <mach/camera.h>                          
 39                                                   
 40 #define PXA_CAM_VERSION_CODE KERNEL_VERSION(0,    
 41 #define PXA_CAM_DRV_NAME "pxa27x-camera"          
 42                                                   
 43 /* Camera Interface */                            
 44 #define CICR0           0x0000                    
 45 #define CICR1           0x0004                    
 46 #define CICR2           0x0008                    
 47 #define CICR3           0x000C                    
 48 #define CICR4           0x0010                    
 49 #define CISR            0x0014                    
 50 #define CIFR            0x0018                    
 51 #define CITOR           0x001C                    
 52 #define CIBR0           0x0028                    
 53 #define CIBR1           0x0030                    
 54 #define CIBR2           0x0038                    
 55                                                   
 56 #define CICR0_DMAEN     (1 << 31)       /* DMA    
 57 #define CICR0_PAR_EN    (1 << 30)       /* Par    
 58 #define CICR0_SL_CAP_EN (1 << 29)       /* Cap    
 59 #define CICR0_ENB       (1 << 28)       /* Cam    
 60 #define CICR0_DIS       (1 << 27)       /* Cam    
 61 #define CICR0_SIM       (0x7 << 24)     /* Sen    
 62 #define CICR0_TOM       (1 << 9)        /* Tim    
 63 #define CICR0_RDAVM     (1 << 8)        /* Rec    
 64 #define CICR0_FEM       (1 << 7)        /* FIF    
 65 #define CICR0_EOLM      (1 << 6)        /* End    
 66 #define CICR0_PERRM     (1 << 5)        /* Par    
 67 #define CICR0_QDM       (1 << 4)        /* Qui    
 68 #define CICR0_CDM       (1 << 3)        /* Dis    
 69 #define CICR0_SOFM      (1 << 2)        /* Sta    
 70 #define CICR0_EOFM      (1 << 1)        /* End    
 71 #define CICR0_FOM       (1 << 0)        /* FIF    
 72                                                   
 73 #define CICR1_TBIT      (1 << 31)       /* Tra    
 74 #define CICR1_RGBT_CONV (0x3 << 29)     /* RGB    
 75 #define CICR1_PPL       (0x7ff << 15)   /* Pix    
 76 #define CICR1_RGB_CONV  (0x7 << 12)     /* RGB    
 77 #define CICR1_RGB_F     (1 << 11)       /* RGB    
 78 #define CICR1_YCBCR_F   (1 << 10)       /* YCb    
 79 #define CICR1_RGB_BPP   (0x7 << 7)      /* RGB    
 80 #define CICR1_RAW_BPP   (0x3 << 5)      /* Raw    
 81 #define CICR1_COLOR_SP  (0x3 << 3)      /* Col    
 82 #define CICR1_DW        (0x7 << 0)      /* Dat    
 83                                                   
 84 #define CICR2_BLW       (0xff << 24)    /* Beg    
 85                                            wai    
 86 #define CICR2_ELW       (0xff << 16)    /* End    
 87                                            wai    
 88 #define CICR2_HSW       (0x3f << 10)    /* Hor    
 89 #define CICR2_BFPW      (0x3f << 3)     /* Beg    
 90                                            wai    
 91 #define CICR2_FSW       (0x7 << 0)      /* Fra    
 92                                            wai    
 93                                                   
 94 #define CICR3_BFW       (0xff << 24)    /* Beg    
 95                                            wai    
 96 #define CICR3_EFW       (0xff << 16)    /* End    
 97                                            wai    
 98 #define CICR3_VSW       (0x3f << 10)    /* Ver    
 99 #define CICR3_BFPW      (0x3f << 3)     /* Beg    
100                                            wai    
101 #define CICR3_LPF       (0x7ff << 0)    /* Lin    
102                                                   
103 #define CICR4_MCLK_DLY  (0x3 << 24)     /* MCL    
104 #define CICR4_PCLK_EN   (1 << 23)       /* Pix    
105 #define CICR4_PCP       (1 << 22)       /* Pix    
106 #define CICR4_HSP       (1 << 21)       /* Hor    
107 #define CICR4_VSP       (1 << 20)       /* Ver    
108 #define CICR4_MCLK_EN   (1 << 19)       /* MCL    
109 #define CICR4_FR_RATE   (0x7 << 8)      /* Fra    
110 #define CICR4_DIV       (0xff << 0)     /* Clo    
111                                                   
112 #define CISR_FTO        (1 << 15)       /* FIF    
113 #define CISR_RDAV_2     (1 << 14)       /* Cha    
114 #define CISR_RDAV_1     (1 << 13)       /* Cha    
115 #define CISR_RDAV_0     (1 << 12)       /* Cha    
116 #define CISR_FEMPTY_2   (1 << 11)       /* Cha    
117 #define CISR_FEMPTY_1   (1 << 10)       /* Cha    
118 #define CISR_FEMPTY_0   (1 << 9)        /* Cha    
119 #define CISR_EOL        (1 << 8)        /* End    
120 #define CISR_PAR_ERR    (1 << 7)        /* Par    
121 #define CISR_CQD        (1 << 6)        /* Cam    
122 #define CISR_CDD        (1 << 5)        /* Cam    
123 #define CISR_SOF        (1 << 4)        /* Sta    
124 #define CISR_EOF        (1 << 3)        /* End    
125 #define CISR_IFO_2      (1 << 2)        /* FIF    
126 #define CISR_IFO_1      (1 << 1)        /* FIF    
127 #define CISR_IFO_0      (1 << 0)        /* FIF    
128                                                   
129 #define CIFR_FLVL2      (0x7f << 23)    /* FIF    
130 #define CIFR_FLVL1      (0x7f << 16)    /* FIF    
131 #define CIFR_FLVL0      (0xff << 8)     /* FIF    
132 #define CIFR_THL_0      (0x3 << 4)      /* Thr    
133 #define CIFR_RESET_F    (1 << 3)        /* Res    
134 #define CIFR_FEN2       (1 << 2)        /* FIF    
135 #define CIFR_FEN1       (1 << 1)        /* FIF    
136 #define CIFR_FEN0       (1 << 0)        /* FIF    
137                                                   
138 #define CICR0_SIM_MP    (0 << 24)                 
139 #define CICR0_SIM_SP    (1 << 24)                 
140 #define CICR0_SIM_MS    (2 << 24)                 
141 #define CICR0_SIM_EP    (3 << 24)                 
142 #define CICR0_SIM_ES    (4 << 24)                 
143                                                   
144 #define CICR1_DW_VAL(x)   ((x) & CICR1_DW)        
145 #define CICR1_PPL_VAL(x)  (((x) << 15) & CICR1    
146 #define CICR1_COLOR_SP_VAL(x)   (((x) << 3) &     
147 #define CICR1_RGB_BPP_VAL(x)    (((x) << 7) &     
148 #define CICR1_RGBT_CONV_VAL(x)  (((x) << 29) &    
149                                                   
150 #define CICR2_BLW_VAL(x)  (((x) << 24) & CICR2    
151 #define CICR2_ELW_VAL(x)  (((x) << 16) & CICR2    
152 #define CICR2_HSW_VAL(x)  (((x) << 10) & CICR2    
153 #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_    
154 #define CICR2_FSW_VAL(x)  (((x) << 0) & CICR2_    
155                                                   
156 #define CICR3_BFW_VAL(x)  (((x) << 24) & CICR3    
157 #define CICR3_EFW_VAL(x)  (((x) << 16) & CICR3    
158 #define CICR3_VSW_VAL(x)  (((x) << 11) & CICR3    
159 #define CICR3_LPF_VAL(x)  (((x) << 0) & CICR3_    
160                                                   
161 #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAV    
162                         CICR0_PERRM | CICR0_QD    
163                         CICR0_EOFM | CICR0_FOM    
164                                                   
165 /*                                                
166  * Structures                                     
167  */                                               
168 enum pxa_camera_active_dma {                      
169         DMA_Y = 0x1,                              
170         DMA_U = 0x2,                              
171         DMA_V = 0x4,                              
172 };                                                
173                                                   
174 /* descriptor needed for the PXA DMA engine */    
175 struct pxa_cam_dma {                              
176         dma_addr_t              sg_dma;           
177         struct pxa_dma_desc     *sg_cpu;          
178         size_t                  sg_size;          
179         int                     sglen;            
180 };                                                
181                                                   
182 /* buffer for one video frame */                  
183 struct pxa_buffer {                               
184         /* common v4l buffer stuff -- must be     
185         struct videobuf_buffer vb;                
186                                                   
187         const struct soc_camera_data_format       
188                                                   
189         /* our descriptor lists for Y, U and V    
190         struct pxa_cam_dma dmas[3];               
191                                                   
192         int                     inwork;           
193                                                   
194         enum pxa_camera_active_dma active_dma;    
195 };                                                
196                                                   
197 struct pxa_camera_dev {                           
198         struct soc_camera_host  soc_host;         
199         /* PXA27x is only supposed to handle o    
200          * interface. If anyone ever builds ha    
201          * one camera, they will have to modif    
202         struct soc_camera_device *icd;            
203         struct clk              *clk;             
204                                                   
205         unsigned int            irq;              
206         void __iomem            *base;            
207                                                   
208         int                     channels;         
209         unsigned int            dma_chans[3];     
210                                                   
211         struct pxacamera_platform_data *pdata;    
212         struct resource         *res;             
213         unsigned long           platform_flags    
214         unsigned long           ciclk;            
215         unsigned long           mclk;             
216         u32                     mclk_divisor;     
217                                                   
218         struct list_head        capture;          
219                                                   
220         spinlock_t              lock;             
221                                                   
222         struct pxa_buffer       *active;          
223         struct pxa_dma_desc     *sg_tail[3];      
224                                                   
225         u32                     save_cicr[5];     
226 };                                                
227                                                   
228 static const char *pxa_cam_driver_description     
229                                                   
230 static unsigned int vid_limit = 16;     /* Vid    
231                                                   
232 /*                                                
233  *  Videobuf operations                           
234  */                                               
235 static int pxa_videobuf_setup(struct videobuf_    
236                               unsigned int *si    
237 {                                                 
238         struct soc_camera_device *icd = vq->pr    
239                                                   
240         dev_dbg(&icd->dev, "count=%d, size=%d\    
241                                                   
242         *size = roundup(icd->width * icd->heig    
243                         ((icd->current_fmt->de    
244                                                   
245         if (0 == *count)                          
246                 *count = 32;                      
247         while (*size * *count > vid_limit * 10    
248                 (*count)--;                       
249                                                   
250         return 0;                                 
251 }                                                 
252                                                   
253 static void free_buffer(struct videobuf_queue     
254 {                                                 
255         struct soc_camera_device *icd = vq->pr    
256         struct soc_camera_host *ici = to_soc_c    
257         struct videobuf_dmabuf *dma = videobuf    
258         int i;                                    
259                                                   
260         BUG_ON(in_interrupt());                   
261                                                   
262         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08    
263                 &buf->vb, buf->vb.baddr, buf->    
264                                                   
265         /* This waits until this buffer is out    
266          * longer in STATE_QUEUED or STATE_ACT    
267         videobuf_waiton(&buf->vb, 0, 0);          
268         videobuf_dma_unmap(vq, dma);              
269         videobuf_dma_free(dma);                   
270                                                   
271         for (i = 0; i < ARRAY_SIZE(buf->dmas);    
272                 if (buf->dmas[i].sg_cpu)          
273                         dma_free_coherent(ici-    
274                                           buf-    
275                                           buf-    
276                 buf->dmas[i].sg_cpu = NULL;       
277         }                                         
278                                                   
279         buf->vb.state = VIDEOBUF_NEEDS_INIT;      
280 }                                                 
281                                                   
282 static int calculate_dma_sglen(struct scatterl    
283                                int sg_first_of    
284 {                                                 
285         int i, offset, dma_len, xfer_len;         
286         struct scatterlist *sg;                   
287                                                   
288         offset = sg_first_ofs;                    
289         for_each_sg(sglist, sg, sglen, i) {       
290                 dma_len = sg_dma_len(sg);         
291                                                   
292                 /* PXA27x Developer's Manual 2    
293                 xfer_len = roundup(min(dma_len    
294                                                   
295                 size = max(0, size - xfer_len)    
296                 offset = 0;                       
297                 if (size == 0)                    
298                         break;                    
299         }                                         
300                                                   
301         BUG_ON(size != 0);                        
302         return i + 1;                             
303 }                                                 
304                                                   
305 /**                                               
306  * pxa_init_dma_channel - init dma descriptors    
307  * @pcdev: pxa camera device                      
308  * @buf: pxa buffer to find pxa dma channel       
309  * @dma: dma video buffer                         
310  * @channel: dma channel (0 => 'Y', 1 => 'U',     
311  * @cibr: camera Receive Buffer Register          
312  * @size: bytes to transfer                       
313  * @sg_first: first element of sg_list            
314  * @sg_first_ofs: offset in first element of s    
315  *                                                
316  * Prepares the pxa dma descriptors to transfe    
317  * Beware sg_first and sg_first_ofs are both i    
318  *                                                
319  * Returns 0 or -ENOMEM if no coherent memory     
320  */                                               
321 static int pxa_init_dma_channel(struct pxa_cam    
322                                 struct pxa_buf    
323                                 struct videobu    
324                                 int cibr, int     
325                                 struct scatter    
326 {                                                 
327         struct pxa_cam_dma *pxa_dma = &buf->dm    
328         struct scatterlist *sg;                   
329         int i, offset, sglen;                     
330         int dma_len = 0, xfer_len = 0;            
331                                                   
332         if (pxa_dma->sg_cpu)                      
333                 dma_free_coherent(pcdev->soc_h    
334                                   pxa_dma->sg_    
335                                                   
336         sglen = calculate_dma_sglen(*sg_first,    
337                                     *sg_first_    
338                                                   
339         pxa_dma->sg_size = (sglen + 1) * sizeo    
340         pxa_dma->sg_cpu = dma_alloc_coherent(p    
341                                              &    
342         if (!pxa_dma->sg_cpu)                     
343                 return -ENOMEM;                   
344                                                   
345         pxa_dma->sglen = sglen;                   
346         offset = *sg_first_ofs;                   
347                                                   
348         dev_dbg(pcdev->soc_host.dev, "DMA: sg_    
349                 *sg_first, sglen, *sg_first_of    
350                                                   
351                                                   
352         for_each_sg(*sg_first, sg, sglen, i) {    
353                 dma_len = sg_dma_len(sg);         
354                                                   
355                 /* PXA27x Developer's Manual 2    
356                 xfer_len = roundup(min(dma_len    
357                                                   
358                 size = max(0, size - xfer_len)    
359                                                   
360                 pxa_dma->sg_cpu[i].dsadr = pcd    
361                 pxa_dma->sg_cpu[i].dtadr = sg_    
362                 pxa_dma->sg_cpu[i].dcmd =         
363                         DCMD_FLOWSRC | DCMD_BU    
364 #ifdef DEBUG                                      
365                 if (!i)                           
366                         pxa_dma->sg_cpu[i].dcm    
367 #endif                                            
368                 pxa_dma->sg_cpu[i].ddadr =        
369                         pxa_dma->sg_dma + (i +    
370                                                   
371                 dev_vdbg(pcdev->soc_host.dev,     
372                          pxa_dma->sg_dma + i *    
373                          sg_dma_address(sg) +     
374                 offset = 0;                       
375                                                   
376                 if (size == 0)                    
377                         break;                    
378         }                                         
379                                                   
380         pxa_dma->sg_cpu[sglen].ddadr = DDADR_S    
381         pxa_dma->sg_cpu[sglen].dcmd  = DCMD_FL    
382                                                   
383         /*                                        
384          * Handle 1 special case :                
385          *  - in 3 planes (YUV422P format), we    
386          *    to dma_len (end on PAGE boundary    
387          *    for next plane should be the nex    
388          *    last scatter gather RAM page        
389          */                                       
390         if (xfer_len >= dma_len) {                
391                 *sg_first_ofs = xfer_len - dma    
392                 *sg_first = sg_next(sg);          
393         } else {                                  
394                 *sg_first_ofs = xfer_len;         
395                 *sg_first = sg;                   
396         }                                         
397                                                   
398         return 0;                                 
399 }                                                 
400                                                   
401 static void pxa_videobuf_set_actdma(struct pxa    
402                                     struct pxa    
403 {                                                 
404         buf->active_dma = DMA_Y;                  
405         if (pcdev->channels == 3)                 
406                 buf->active_dma |= DMA_U | DMA    
407 }                                                 
408                                                   
409 /*                                                
410  * Please check the DMA prepared buffer struct    
411  *   Documentation/video4linux/pxa_camera.txt     
412  * Please check also in pxa_camera_check_link_    
413  * modification while DMA chain is running wil    
414  */                                               
415 static int pxa_videobuf_prepare(struct videobu    
416                 struct videobuf_buffer *vb, en    
417 {                                                 
418         struct soc_camera_device *icd = vq->pr    
419         struct soc_camera_host *ici = to_soc_c    
420         struct pxa_camera_dev *pcdev = ici->pr    
421         struct pxa_buffer *buf = container_of(    
422         int ret;                                  
423         int size_y, size_u = 0, size_v = 0;       
424                                                   
425         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08    
426                 vb, vb->baddr, vb->bsize);        
427                                                   
428         /* Added list head initialization on a    
429         WARN_ON(!list_empty(&vb->queue));         
430                                                   
431 #ifdef DEBUG                                      
432         /* This can be useful if you want to s    
433          * the buffer with something */           
434         memset((void *)vb->baddr, 0xaa, vb->bs    
435 #endif                                            
436                                                   
437         BUG_ON(NULL == icd->current_fmt);         
438                                                   
439         /* I think, in buf_prepare you only ha    
440          * the actual buffer is yours */          
441         buf->inwork = 1;                          
442                                                   
443         if (buf->fmt    != icd->current_fmt ||    
444             vb->width   != icd->width ||          
445             vb->height  != icd->height ||         
446             vb->field   != field) {               
447                 buf->fmt        = icd->current    
448                 vb->width       = icd->width;     
449                 vb->height      = icd->height;    
450                 vb->field       = field;          
451                 vb->state       = VIDEOBUF_NEE    
452         }                                         
453                                                   
454         vb->size = vb->width * vb->height * ((    
455         if (0 != vb->baddr && vb->bsize < vb->    
456                 ret = -EINVAL;                    
457                 goto out;                         
458         }                                         
459                                                   
460         if (vb->state == VIDEOBUF_NEEDS_INIT)     
461                 int size = vb->size;              
462                 int next_ofs = 0;                 
463                 struct videobuf_dmabuf *dma =     
464                 struct scatterlist *sg;           
465                                                   
466                 ret = videobuf_iolock(vq, vb,     
467                 if (ret)                          
468                         goto fail;                
469                                                   
470                 if (pcdev->channels == 3) {       
471                         size_y = size / 2;        
472                         size_u = size_v = size    
473                 } else {                          
474                         size_y = size;            
475                 }                                 
476                                                   
477                 sg = dma->sglist;                 
478                                                   
479                 /* init DMA for Y channel */      
480                 ret = pxa_init_dma_channel(pcd    
481                                            &sg    
482                 if (ret) {                        
483                         dev_err(pcdev->soc_hos    
484                                 "DMA initializ    
485                         goto fail;                
486                 }                                 
487                                                   
488                 /* init DMA for U channel */      
489                 if (size_u)                       
490                         ret = pxa_init_dma_cha    
491                                                   
492                 if (ret) {                        
493                         dev_err(pcdev->soc_hos    
494                                 "DMA initializ    
495                         goto fail_u;              
496                 }                                 
497                                                   
498                 /* init DMA for V channel */      
499                 if (size_v)                       
500                         ret = pxa_init_dma_cha    
501                                                   
502                 if (ret) {                        
503                         dev_err(pcdev->soc_hos    
504                                 "DMA initializ    
505                         goto fail_v;              
506                 }                                 
507                                                   
508                 vb->state = VIDEOBUF_PREPARED;    
509         }                                         
510                                                   
511         buf->inwork = 0;                          
512         pxa_videobuf_set_actdma(pcdev, buf);      
513                                                   
514         return 0;                                 
515                                                   
516 fail_v:                                           
517         dma_free_coherent(pcdev->soc_host.dev,    
518                           buf->dmas[1].sg_cpu,    
519 fail_u:                                           
520         dma_free_coherent(pcdev->soc_host.dev,    
521                           buf->dmas[0].sg_cpu,    
522 fail:                                             
523         free_buffer(vq, buf);                     
524 out:                                              
525         buf->inwork = 0;                          
526         return ret;                               
527 }                                                 
528                                                   
529 /**                                               
530  * pxa_dma_start_channels - start DMA channel     
531  * @pcdev: pxa camera device                      
532  *                                                
533  * Initialize DMA channels to the beginning of    
534  * start these channels.                          
535  */                                               
536 static void pxa_dma_start_channels(struct pxa_    
537 {                                                 
538         int i;                                    
539         struct pxa_buffer *active;                
540                                                   
541         active = pcdev->active;                   
542                                                   
543         for (i = 0; i < pcdev->channels; i++)     
544                 dev_dbg(pcdev->soc_host.dev, "    
545                         i, active->dmas[i].sg_    
546                 DDADR(pcdev->dma_chans[i]) = a    
547                 DCSR(pcdev->dma_chans[i]) = DC    
548         }                                         
549 }                                                 
550                                                   
551 static void pxa_dma_stop_channels(struct pxa_c    
552 {                                                 
553         int i;                                    
554                                                   
555         for (i = 0; i < pcdev->channels; i++)     
556                 dev_dbg(pcdev->soc_host.dev, "    
557                 DCSR(pcdev->dma_chans[i]) = 0;    
558         }                                         
559 }                                                 
560                                                   
561 static void pxa_dma_add_tail_buf(struct pxa_ca    
562                                  struct pxa_bu    
563 {                                                 
564         int i;                                    
565         struct pxa_dma_desc *buf_last_desc;       
566                                                   
567         for (i = 0; i < pcdev->channels; i++)     
568                 buf_last_desc = buf->dmas[i].s    
569                 buf_last_desc->ddadr = DDADR_S    
570                                                   
571                 if (pcdev->sg_tail[i])            
572                         /* Link the new buffer    
573                         pcdev->sg_tail[i]->dda    
574                                                   
575                 /* Update the channel tail */     
576                 pcdev->sg_tail[i] = buf_last_d    
577         }                                         
578 }                                                 
579                                                   
580 /**                                               
581  * pxa_camera_start_capture - start video capt    
582  * @pcdev: camera device                          
583  *                                                
584  * Launch capturing. DMA channels should not b    
585  * activated at the end of frame interrupt, to    
586  * never begin the capture of a partial frame.    
587  */                                               
588 static void pxa_camera_start_capture(struct px    
589 {                                                 
590         unsigned long cicr0, cifr;                
591                                                   
592         dev_dbg(pcdev->soc_host.dev, "%s\n", _    
593         /* Reset the FIFOs */                     
594         cifr = __raw_readl(pcdev->base + CIFR)    
595         __raw_writel(cifr, pcdev->base + CIFR)    
596         /* Enable End-Of-Frame Interrupt */       
597         cicr0 = __raw_readl(pcdev->base + CICR    
598         cicr0 &= ~CICR0_EOFM;                     
599         __raw_writel(cicr0, pcdev->base + CICR    
600 }                                                 
601                                                   
602 static void pxa_camera_stop_capture(struct pxa    
603 {                                                 
604         unsigned long cicr0;                      
605                                                   
606         pxa_dma_stop_channels(pcdev);             
607                                                   
608         cicr0 = __raw_readl(pcdev->base + CICR    
609         __raw_writel(cicr0, pcdev->base + CICR    
610                                                   
611         pcdev->active = NULL;                     
612         dev_dbg(pcdev->soc_host.dev, "%s\n", _    
613 }                                                 
614                                                   
615 /* Called under spinlock_irqsave(&pcdev->lock,    
616 static void pxa_videobuf_queue(struct videobuf    
617                                struct videobuf    
618 {                                                 
619         struct soc_camera_device *icd = vq->pr    
620         struct soc_camera_host *ici = to_soc_c    
621         struct pxa_camera_dev *pcdev = ici->pr    
622         struct pxa_buffer *buf = container_of(    
623                                                   
624         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08    
625                 vb, vb->baddr, vb->bsize, pcde    
626                                                   
627         list_add_tail(&vb->queue, &pcdev->capt    
628                                                   
629         vb->state = VIDEOBUF_ACTIVE;              
630         pxa_dma_add_tail_buf(pcdev, buf);         
631                                                   
632         if (!pcdev->active)                       
633                 pxa_camera_start_capture(pcdev    
634 }                                                 
635                                                   
636 static void pxa_videobuf_release(struct videob    
637                                  struct videob    
638 {                                                 
639         struct pxa_buffer *buf = container_of(    
640 #ifdef DEBUG                                      
641         struct soc_camera_device *icd = vq->pr    
642                                                   
643         dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08    
644                 vb, vb->baddr, vb->bsize);        
645                                                   
646         switch (vb->state) {                      
647         case VIDEOBUF_ACTIVE:                     
648                 dev_dbg(&icd->dev, "%s (active    
649                 break;                            
650         case VIDEOBUF_QUEUED:                     
651                 dev_dbg(&icd->dev, "%s (queued    
652                 break;                            
653         case VIDEOBUF_PREPARED:                   
654                 dev_dbg(&icd->dev, "%s (prepar    
655                 break;                            
656         default:                                  
657                 dev_dbg(&icd->dev, "%s (unknow    
658                 break;                            
659         }                                         
660 #endif                                            
661                                                   
662         free_buffer(vq, buf);                     
663 }                                                 
664                                                   
665 static void pxa_camera_wakeup(struct pxa_camer    
666                               struct videobuf_    
667                               struct pxa_buffe    
668 {                                                 
669         int i;                                    
670                                                   
671         /* _init is used to debug races, see c    
672         list_del_init(&vb->queue);                
673         vb->state = VIDEOBUF_DONE;                
674         do_gettimeofday(&vb->ts);                 
675         vb->field_count++;                        
676         wake_up(&vb->done);                       
677         dev_dbg(pcdev->soc_host.dev, "%s deque    
678                                                   
679         if (list_empty(&pcdev->capture)) {        
680                 pxa_camera_stop_capture(pcdev)    
681                 for (i = 0; i < pcdev->channel    
682                         pcdev->sg_tail[i] = NU    
683                 return;                           
684         }                                         
685                                                   
686         pcdev->active = list_entry(pcdev->capt    
687                                    struct pxa_    
688 }                                                 
689                                                   
690 /**                                               
691  * pxa_camera_check_link_miss - check missed D    
692  * @pcdev: camera device                          
693  *                                                
694  * The DMA chaining is done with DMA running.     
695  * remains, where a buffer is queued on the ch    
696  * stopped. This means the tailed buffer would    
697  * This function restarts the capture for this    
698  *  - DADR() == DADDR_STOP                        
699  *  - a videobuffer is queued on the pcdev->ca    
700  *                                                
701  * Please check the "DMA hot chaining timeslic    
702  *   Documentation/video4linux/pxa_camera.txt     
703  *                                                
704  * Context: should only be called within the d    
705  */                                               
706 static void pxa_camera_check_link_miss(struct     
707 {                                                 
708         int i, is_dma_stopped = 1;                
709                                                   
710         for (i = 0; i < pcdev->channels; i++)     
711                 if (DDADR(pcdev->dma_chans[i])    
712                         is_dma_stopped = 0;       
713         dev_dbg(pcdev->soc_host.dev, "%s : top    
714                 __func__, pcdev->active, is_dm    
715         if (pcdev->active && is_dma_stopped)      
716                 pxa_camera_start_capture(pcdev    
717 }                                                 
718                                                   
719 static void pxa_camera_dma_irq(int channel, st    
720                                enum pxa_camera    
721 {                                                 
722         struct pxa_buffer *buf;                   
723         unsigned long flags;                      
724         u32 status, camera_status, overrun;       
725         struct videobuf_buffer *vb;               
726                                                   
727         spin_lock_irqsave(&pcdev->lock, flags)    
728                                                   
729         status = DCSR(channel);                   
730         DCSR(channel) = status;                   
731                                                   
732         camera_status = __raw_readl(pcdev->bas    
733         overrun = CISR_IFO_0;                     
734         if (pcdev->channels == 3)                 
735                 overrun |= CISR_IFO_1 | CISR_I    
736                                                   
737         if (status & DCSR_BUSERR) {               
738                 dev_err(pcdev->soc_host.dev, "    
739                 goto out;                         
740         }                                         
741                                                   
742         if (!(status & (DCSR_ENDINTR | DCSR_ST    
743                 dev_err(pcdev->soc_host.dev, "    
744                         "status: 0x%08x\n", st    
745                 goto out;                         
746         }                                         
747                                                   
748         /*                                        
749          * pcdev->active should not be NULL in    
750          *                                        
751          * But there is one corner case : if c    
752          * overrun of channel 1, and at that s    
753          *                                        
754          * When handling the overrun in DMA ir    
755          * capture and restart it (and thus se    
756          * DMA irq handler will already be pen    
757          * the DMA irq handler for channel 2 t    
758          * that is normal.                        
759          */                                       
760         if (!pcdev->active)                       
761                 goto out;                         
762                                                   
763         vb = &pcdev->active->vb;                  
764         buf = container_of(vb, struct pxa_buff    
765         WARN_ON(buf->inwork || list_empty(&vb-    
766                                                   
767         dev_dbg(pcdev->soc_host.dev, "%s chann    
768                 __func__, channel, status & DC    
769                 status & DCSR_ENDINTR ? "EOF "    
770                                                   
771         if (status & DCSR_ENDINTR) {              
772                 /*                                
773                  * It's normal if the last fra    
774                  * are no more DMA descriptors    
775                  */                               
776                 if (camera_status & overrun &&    
777                     !list_is_last(pcdev->captu    
778                         dev_dbg(pcdev->soc_hos    
779                                 camera_status)    
780                         pxa_camera_stop_captur    
781                         pxa_camera_start_captu    
782                         goto out;                 
783                 }                                 
784                 buf->active_dma &= ~act_dma;      
785                 if (!buf->active_dma) {           
786                         pxa_camera_wakeup(pcde    
787                         pxa_camera_check_link_    
788                 }                                 
789         }                                         
790                                                   
791 out:                                              
792         spin_unlock_irqrestore(&pcdev->lock, f    
793 }                                                 
794                                                   
795 static void pxa_camera_dma_irq_y(int channel,     
796 {                                                 
797         struct pxa_camera_dev *pcdev = data;      
798         pxa_camera_dma_irq(channel, pcdev, DMA    
799 }                                                 
800                                                   
801 static void pxa_camera_dma_irq_u(int channel,     
802 {                                                 
803         struct pxa_camera_dev *pcdev = data;      
804         pxa_camera_dma_irq(channel, pcdev, DMA    
805 }                                                 
806                                                   
807 static void pxa_camera_dma_irq_v(int channel,     
808 {                                                 
809         struct pxa_camera_dev *pcdev = data;      
810         pxa_camera_dma_irq(channel, pcdev, DMA    
811 }                                                 
812                                                   
813 static struct videobuf_queue_ops pxa_videobuf_    
814         .buf_setup      = pxa_videobuf_setup,     
815         .buf_prepare    = pxa_videobuf_prepare    
816         .buf_queue      = pxa_videobuf_queue,     
817         .buf_release    = pxa_videobuf_release    
818 };                                                
819                                                   
820 static void pxa_camera_init_videobuf(struct vi    
821                               struct soc_camer    
822 {                                                 
823         struct soc_camera_host *ici = to_soc_c    
824         struct pxa_camera_dev *pcdev = ici->pr    
825                                                   
826         /* We must pass NULL as dev pointer, t    
827          * transform to normal dma_* ones. */     
828         videobuf_queue_sg_init(q, &pxa_videobu    
829                                 V4L2_BUF_TYPE_    
830                                 sizeof(struct     
831 }                                                 
832                                                   
833 static u32 mclk_get_divisor(struct pxa_camera_    
834 {                                                 
835         unsigned long mclk = pcdev->mclk;         
836         u32 div;                                  
837         unsigned long lcdclk;                     
838                                                   
839         lcdclk = clk_get_rate(pcdev->clk);        
840         pcdev->ciclk = lcdclk;                    
841                                                   
842         /* mclk <= ciclk / 4 (27.4.2) */          
843         if (mclk > lcdclk / 4) {                  
844                 mclk = lcdclk / 4;                
845                 dev_warn(pcdev->soc_host.dev,     
846         }                                         
847                                                   
848         /* We verify mclk != 0, so if anyone b    
849         div = (lcdclk + 2 * mclk - 1) / (2 * m    
850                                                   
851         /* If we're not supplying MCLK, leave     
852         if (pcdev->platform_flags & PXA_CAMERA    
853                 pcdev->mclk = lcdclk / (2 * (d    
854                                                   
855         dev_dbg(pcdev->soc_host.dev, "LCD cloc    
856                 "divisor %u\n", lcdclk, mclk,     
857                                                   
858         return div;                               
859 }                                                 
860                                                   
861 static void recalculate_fifo_timeout(struct px    
862                                      unsigned     
863 {                                                 
864         /* We want a timeout > 1 pixel time, n    
865         u32 ciclk_per_pixel = pcdev->ciclk / p    
866                                                   
867         __raw_writel(ciclk_per_pixel, pcdev->b    
868 }                                                 
869                                                   
870 static void pxa_camera_activate(struct pxa_cam    
871 {                                                 
872         struct pxacamera_platform_data *pdata     
873         u32 cicr4 = 0;                            
874                                                   
875         dev_dbg(pcdev->soc_host.dev, "Register    
876                 pcdev, pdata);                    
877                                                   
878         if (pdata && pdata->init) {               
879                 dev_dbg(pcdev->soc_host.dev, "    
880                 pdata->init(pcdev->soc_host.de    
881         }                                         
882                                                   
883         /* disable all interrupts */              
884         __raw_writel(0x3ff, pcdev->base + CICR    
885                                                   
886         if (pcdev->platform_flags & PXA_CAMERA    
887                 cicr4 |= CICR4_PCLK_EN;           
888         if (pcdev->platform_flags & PXA_CAMERA    
889                 cicr4 |= CICR4_MCLK_EN;           
890         if (pcdev->platform_flags & PXA_CAMERA    
891                 cicr4 |= CICR4_PCP;               
892         if (pcdev->platform_flags & PXA_CAMERA    
893                 cicr4 |= CICR4_HSP;               
894         if (pcdev->platform_flags & PXA_CAMERA    
895                 cicr4 |= CICR4_VSP;               
896                                                   
897         __raw_writel(pcdev->mclk_divisor | cic    
898                                                   
899         if (pcdev->platform_flags & PXA_CAMERA    
900                 /* Initialise the timeout unde    
901                 recalculate_fifo_timeout(pcdev    
902         else                                      
903                 /* "Safe default" - 13MHz */      
904                 recalculate_fifo_timeout(pcdev    
905                                                   
906         clk_enable(pcdev->clk);                   
907 }                                                 
908                                                   
909 static void pxa_camera_deactivate(struct pxa_c    
910 {                                                 
911         clk_disable(pcdev->clk);                  
912 }                                                 
913                                                   
914 static irqreturn_t pxa_camera_irq(int irq, voi    
915 {                                                 
916         struct pxa_camera_dev *pcdev = data;      
917         unsigned long status, cicr0;              
918         struct pxa_buffer *buf;                   
919         struct videobuf_buffer *vb;               
920                                                   
921         status = __raw_readl(pcdev->base + CIS    
922         dev_dbg(pcdev->soc_host.dev, "Camera i    
923                                                   
924         if (!status)                              
925                 return IRQ_NONE;                  
926                                                   
927         __raw_writel(status, pcdev->base + CIS    
928                                                   
929         if (status & CISR_EOF) {                  
930                 pcdev->active = list_first_ent    
931                                            str    
932                 vb = &pcdev->active->vb;          
933                 buf = container_of(vb, struct     
934                 pxa_videobuf_set_actdma(pcdev,    
935                                                   
936                 pxa_dma_start_channels(pcdev);    
937                                                   
938                 cicr0 = __raw_readl(pcdev->bas    
939                 __raw_writel(cicr0, pcdev->bas    
940         }                                         
941                                                   
942         return IRQ_HANDLED;                       
943 }                                                 
944                                                   
945 /*                                                
946  * The following two functions absolutely depe    
947  * there can be only one camera on PXA quick c    
948  * Called with .video_lock held                   
949  */                                               
950 static int pxa_camera_add_device(struct soc_ca    
951 {                                                 
952         struct soc_camera_host *ici = to_soc_c    
953         struct pxa_camera_dev *pcdev = ici->pr    
954         int ret;                                  
955                                                   
956         if (pcdev->icd) {                         
957                 ret = -EBUSY;                     
958                 goto ebusy;                       
959         }                                         
960                                                   
961         dev_info(&icd->dev, "PXA Camera driver    
962                  icd->devnum);                    
963                                                   
964         pxa_camera_activate(pcdev);               
965         ret = icd->ops->init(icd);                
966                                                   
967         if (!ret)                                 
968                 pcdev->icd = icd;                 
969                                                   
970 ebusy:                                            
971         return ret;                               
972 }                                                 
973                                                   
974 /* Called with .video_lock held */                
975 static void pxa_camera_remove_device(struct so    
976 {                                                 
977         struct soc_camera_host *ici = to_soc_c    
978         struct pxa_camera_dev *pcdev = ici->pr    
979                                                   
980         BUG_ON(icd != pcdev->icd);                
981                                                   
982         dev_info(&icd->dev, "PXA Camera driver    
983                  icd->devnum);                    
984                                                   
985         /* disable capture, disable interrupts    
986         __raw_writel(0x3ff, pcdev->base + CICR    
987                                                   
988         /* Stop DMA engine */                     
989         DCSR(pcdev->dma_chans[0]) = 0;            
990         DCSR(pcdev->dma_chans[1]) = 0;            
991         DCSR(pcdev->dma_chans[2]) = 0;            
992                                                   
993         icd->ops->release(icd);                   
994                                                   
995         pxa_camera_deactivate(pcdev);             
996                                                   
997         pcdev->icd = NULL;                        
998 }                                                 
999                                                   
1000 static int test_platform_param(struct pxa_cam    
1001                                unsigned char     
1002 {                                                
1003         /*                                       
1004          * Platform specified synchronization    
1005          * only a recommendation and are only    
1006          * quick capture interface supports b    
1007          */                                      
1008         *flags = (pcdev->platform_flags & PXA    
1009                   SOCAM_MASTER : SOCAM_SLAVE)    
1010                 SOCAM_HSYNC_ACTIVE_HIGH |        
1011                 SOCAM_HSYNC_ACTIVE_LOW |         
1012                 SOCAM_VSYNC_ACTIVE_HIGH |        
1013                 SOCAM_VSYNC_ACTIVE_LOW |         
1014                 SOCAM_DATA_ACTIVE_HIGH |         
1015                 SOCAM_PCLK_SAMPLE_RISING |       
1016                 SOCAM_PCLK_SAMPLE_FALLING;       
1017                                                  
1018         /* If requested data width is support    
1019         switch (buswidth) {                      
1020         case 10:                                 
1021                 if (!(pcdev->platform_flags &    
1022                         return -EINVAL;          
1023                 *flags |= SOCAM_DATAWIDTH_10;    
1024                 break;                           
1025         case 9:                                  
1026                 if (!(pcdev->platform_flags &    
1027                         return -EINVAL;          
1028                 *flags |= SOCAM_DATAWIDTH_9;     
1029                 break;                           
1030         case 8:                                  
1031                 if (!(pcdev->platform_flags &    
1032                         return -EINVAL;          
1033                 *flags |= SOCAM_DATAWIDTH_8;     
1034                 break;                           
1035         default:                                 
1036                 return -EINVAL;                  
1037         }                                        
1038                                                  
1039         return 0;                                
1040 }                                                
1041                                                  
1042 static int pxa_camera_set_bus_param(struct so    
1043 {                                                
1044         struct soc_camera_host *ici = to_soc_    
1045         struct pxa_camera_dev *pcdev = ici->p    
1046         unsigned long dw, bpp, bus_flags, cam    
1047         u32 cicr0, cicr1, cicr2, cicr3, cicr4    
1048         int ret = test_platform_param(pcdev,     
1049                                                  
1050         if (ret < 0)                             
1051                 return ret;                      
1052                                                  
1053         camera_flags = icd->ops->query_bus_pa    
1054                                                  
1055         common_flags = soc_camera_bus_param_c    
1056         if (!common_flags)                       
1057                 return -EINVAL;                  
1058                                                  
1059         pcdev->channels = 1;                     
1060                                                  
1061         /* Make choises, based on platform pr    
1062         if ((common_flags & SOCAM_HSYNC_ACTIV    
1063             (common_flags & SOCAM_HSYNC_ACTIV    
1064                 if (pcdev->platform_flags & P    
1065                         common_flags &= ~SOCA    
1066                 else                             
1067                         common_flags &= ~SOCA    
1068         }                                        
1069                                                  
1070         if ((common_flags & SOCAM_VSYNC_ACTIV    
1071             (common_flags & SOCAM_VSYNC_ACTIV    
1072                 if (pcdev->platform_flags & P    
1073                         common_flags &= ~SOCA    
1074                 else                             
1075                         common_flags &= ~SOCA    
1076         }                                        
1077                                                  
1078         if ((common_flags & SOCAM_PCLK_SAMPLE    
1079             (common_flags & SOCAM_PCLK_SAMPLE    
1080                 if (pcdev->platform_flags & P    
1081                         common_flags &= ~SOCA    
1082                 else                             
1083                         common_flags &= ~SOCA    
1084         }                                        
1085                                                  
1086         ret = icd->ops->set_bus_param(icd, co    
1087         if (ret < 0)                             
1088                 return ret;                      
1089                                                  
1090         /* Datawidth is now guaranteed to be     
1091          * We fix bit-per-pixel equal to data    
1092         switch (common_flags & SOCAM_DATAWIDT    
1093         case SOCAM_DATAWIDTH_10:                 
1094                 dw = 4;                          
1095                 bpp = 0x40;                      
1096                 break;                           
1097         case SOCAM_DATAWIDTH_9:                  
1098                 dw = 3;                          
1099                 bpp = 0x20;                      
1100                 break;                           
1101         default:                                 
1102                 /* Actually it can only be 8     
1103                  * default is just to silence    
1104         case SOCAM_DATAWIDTH_8:                  
1105                 dw = 2;                          
1106                 bpp = 0;                         
1107         }                                        
1108                                                  
1109         if (pcdev->platform_flags & PXA_CAMER    
1110                 cicr4 |= CICR4_PCLK_EN;          
1111         if (pcdev->platform_flags & PXA_CAMER    
1112                 cicr4 |= CICR4_MCLK_EN;          
1113         if (common_flags & SOCAM_PCLK_SAMPLE_    
1114                 cicr4 |= CICR4_PCP;              
1115         if (common_flags & SOCAM_HSYNC_ACTIVE    
1116                 cicr4 |= CICR4_HSP;              
1117         if (common_flags & SOCAM_VSYNC_ACTIVE    
1118                 cicr4 |= CICR4_VSP;              
1119                                                  
1120         cicr0 = __raw_readl(pcdev->base + CIC    
1121         if (cicr0 & CICR0_ENB)                   
1122                 __raw_writel(cicr0 & ~CICR0_E    
1123                                                  
1124         cicr1 = CICR1_PPL_VAL(icd->width - 1)    
1125                                                  
1126         switch (pixfmt) {                        
1127         case V4L2_PIX_FMT_YUV422P:               
1128                 pcdev->channels = 3;             
1129                 cicr1 |= CICR1_YCBCR_F;          
1130                 /*                               
1131                  * Normally, pxa bus wants as    
1132                  * reorderings of the YUV422     
1133                  * and the YUV stream is just    
1134                  * transformation. Note that     
1135                  * should be used if pxa fram    
1136                  */                              
1137         case V4L2_PIX_FMT_UYVY:                  
1138         case V4L2_PIX_FMT_VYUY:                  
1139         case V4L2_PIX_FMT_YUYV:                  
1140         case V4L2_PIX_FMT_YVYU:                  
1141                 cicr1 |= CICR1_COLOR_SP_VAL(2    
1142                 break;                           
1143         case V4L2_PIX_FMT_RGB555:                
1144                 cicr1 |= CICR1_RGB_BPP_VAL(1)    
1145                         CICR1_TBIT | CICR1_CO    
1146                 break;                           
1147         case V4L2_PIX_FMT_RGB565:                
1148                 cicr1 |= CICR1_COLOR_SP_VAL(1    
1149                 break;                           
1150         }                                        
1151                                                  
1152         cicr2 = 0;                               
1153         cicr3 = CICR3_LPF_VAL(icd->height - 1    
1154                 CICR3_BFW_VAL(min((unsigned s    
1155         cicr4 |= pcdev->mclk_divisor;            
1156                                                  
1157         __raw_writel(cicr1, pcdev->base + CIC    
1158         __raw_writel(cicr2, pcdev->base + CIC    
1159         __raw_writel(cicr3, pcdev->base + CIC    
1160         __raw_writel(cicr4, pcdev->base + CIC    
1161                                                  
1162         /* CIF interrupts are not used, only     
1163         cicr0 = (cicr0 & CICR0_ENB) | (pcdev-    
1164                 CICR0_SIM_MP : (CICR0_SL_CAP_    
1165         cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK    
1166         __raw_writel(cicr0, pcdev->base + CIC    
1167                                                  
1168         return 0;                                
1169 }                                                
1170                                                  
1171 static int pxa_camera_try_bus_param(struct so    
1172                                     unsigned     
1173 {                                                
1174         struct soc_camera_host *ici = to_soc_    
1175         struct pxa_camera_dev *pcdev = ici->p    
1176         unsigned long bus_flags, camera_flags    
1177         int ret = test_platform_param(pcdev,     
1178                                                  
1179         if (ret < 0)                             
1180                 return ret;                      
1181                                                  
1182         camera_flags = icd->ops->query_bus_pa    
1183                                                  
1184         return soc_camera_bus_param_compatibl    
1185 }                                                
1186                                                  
1187 static const struct soc_camera_data_format px    
1188         {                                        
1189                 .name           = "Planar YUV    
1190                 .depth          = 16,            
1191                 .fourcc         = V4L2_PIX_FM    
1192                 .colorspace     = V4L2_COLORS    
1193         },                                       
1194 };                                               
1195                                                  
1196 static bool buswidth_supported(struct soc_cam    
1197 {                                                
1198         struct soc_camera_host *ici = to_soc_    
1199         struct pxa_camera_dev *pcdev = ici->p    
1200                                                  
1201         switch (depth) {                         
1202         case 8:                                  
1203                 return !!(pcdev->platform_fla    
1204         case 9:                                  
1205                 return !!(pcdev->platform_fla    
1206         case 10:                                 
1207                 return !!(pcdev->platform_fla    
1208         }                                        
1209         return false;                            
1210 }                                                
1211                                                  
1212 static int required_buswidth(const struct soc    
1213 {                                                
1214         switch (fmt->fourcc) {                   
1215         case V4L2_PIX_FMT_UYVY:                  
1216         case V4L2_PIX_FMT_VYUY:                  
1217         case V4L2_PIX_FMT_YUYV:                  
1218         case V4L2_PIX_FMT_YVYU:                  
1219         case V4L2_PIX_FMT_RGB565:                
1220         case V4L2_PIX_FMT_RGB555:                
1221                 return 8;                        
1222         default:                                 
1223                 return fmt->depth;               
1224         }                                        
1225 }                                                
1226                                                  
1227 static int pxa_camera_get_formats(struct soc_    
1228                                   struct soc_    
1229 {                                                
1230         struct soc_camera_host *ici = to_soc_    
1231         int formats = 0, buswidth, ret;          
1232                                                  
1233         buswidth = required_buswidth(icd->for    
1234                                                  
1235         if (!buswidth_supported(icd, buswidth    
1236                 return 0;                        
1237                                                  
1238         ret = pxa_camera_try_bus_param(icd, b    
1239         if (ret < 0)                             
1240                 return 0;                        
1241                                                  
1242         switch (icd->formats[idx].fourcc) {      
1243         case V4L2_PIX_FMT_UYVY:                  
1244                 formats++;                       
1245                 if (xlate) {                     
1246                         xlate->host_fmt = &px    
1247                         xlate->cam_fmt = icd-    
1248                         xlate->buswidth = bus    
1249                         xlate++;                 
1250                         dev_dbg(ici->dev, "Pr    
1251                                 pxa_camera_fo    
1252                                 icd->formats[    
1253                 }                                
1254         case V4L2_PIX_FMT_VYUY:                  
1255         case V4L2_PIX_FMT_YUYV:                  
1256         case V4L2_PIX_FMT_YVYU:                  
1257         case V4L2_PIX_FMT_RGB565:                
1258         case V4L2_PIX_FMT_RGB555:                
1259                 formats++;                       
1260                 if (xlate) {                     
1261                         xlate->host_fmt = icd    
1262                         xlate->cam_fmt = icd-    
1263                         xlate->buswidth = bus    
1264                         xlate++;                 
1265                         dev_dbg(ici->dev, "Pr    
1266                                 icd->formats[    
1267                 }                                
1268                 break;                           
1269         default:                                 
1270                 /* Generic pass-through */       
1271                 formats++;                       
1272                 if (xlate) {                     
1273                         xlate->host_fmt = icd    
1274                         xlate->cam_fmt = icd-    
1275                         xlate->buswidth = icd    
1276                         xlate++;                 
1277                         dev_dbg(ici->dev,        
1278                                 "Providing fo    
1279                                 icd->formats[    
1280                 }                                
1281         }                                        
1282                                                  
1283         return formats;                          
1284 }                                                
1285                                                  
1286 static int pxa_camera_set_crop(struct soc_cam    
1287                                struct v4l2_re    
1288 {                                                
1289         struct soc_camera_host *ici = to_soc_    
1290         struct pxa_camera_dev *pcdev = ici->p    
1291         struct soc_camera_sense sense = {        
1292                 .master_clock = pcdev->mclk,     
1293                 .pixel_clock_max = pcdev->cic    
1294         };                                       
1295         int ret;                                 
1296                                                  
1297         /* If PCLK is used to latch data from    
1298         if (pcdev->platform_flags & PXA_CAMER    
1299                 icd->sense = &sense;             
1300                                                  
1301         ret = icd->ops->set_crop(icd, rect);     
1302                                                  
1303         icd->sense = NULL;                       
1304                                                  
1305         if (ret < 0) {                           
1306                 dev_warn(ici->dev, "Failed to    
1307                          rect->width, rect->h    
1308         } else if (sense.flags & SOCAM_SENSE_    
1309                 if (sense.pixel_clock > sense    
1310                         dev_err(ici->dev,        
1311                                 "pixel clock     
1312                                 sense.pixel_c    
1313                         return -EIO;             
1314                 }                                
1315                 recalculate_fifo_timeout(pcde    
1316         }                                        
1317                                                  
1318         return ret;                              
1319 }                                                
1320                                                  
1321 static int pxa_camera_set_fmt(struct soc_came    
1322                               struct v4l2_for    
1323 {                                                
1324         struct soc_camera_host *ici = to_soc_    
1325         struct pxa_camera_dev *pcdev = ici->p    
1326         const struct soc_camera_data_format *    
1327         const struct soc_camera_format_xlate     
1328         struct soc_camera_sense sense = {        
1329                 .master_clock = pcdev->mclk,     
1330                 .pixel_clock_max = pcdev->cic    
1331         };                                       
1332         struct v4l2_pix_format *pix = &f->fmt    
1333         struct v4l2_format cam_f = *f;           
1334         int ret;                                 
1335                                                  
1336         xlate = soc_camera_xlate_by_fourcc(ic    
1337         if (!xlate) {                            
1338                 dev_warn(ici->dev, "Format %x    
1339                 return -EINVAL;                  
1340         }                                        
1341                                                  
1342         cam_fmt = xlate->cam_fmt;                
1343                                                  
1344         /* If PCLK is used to latch data from    
1345         if (pcdev->platform_flags & PXA_CAMER    
1346                 icd->sense = &sense;             
1347                                                  
1348         cam_f.fmt.pix.pixelformat = cam_fmt->    
1349         ret = icd->ops->set_fmt(icd, &cam_f);    
1350                                                  
1351         icd->sense = NULL;                       
1352                                                  
1353         if (ret < 0) {                           
1354                 dev_warn(ici->dev, "Failed to    
1355                          pix->pixelformat);      
1356         } else if (sense.flags & SOCAM_SENSE_    
1357                 if (sense.pixel_clock > sense    
1358                         dev_err(ici->dev,        
1359                                 "pixel clock     
1360                                 sense.pixel_c    
1361                         return -EIO;             
1362                 }                                
1363                 recalculate_fifo_timeout(pcde    
1364         }                                        
1365                                                  
1366         if (!ret) {                              
1367                 icd->buswidth = xlate->buswid    
1368                 icd->current_fmt = xlate->hos    
1369         }                                        
1370                                                  
1371         return ret;                              
1372 }                                                
1373                                                  
1374 static int pxa_camera_try_fmt(struct soc_came    
1375                               struct v4l2_for    
1376 {                                                
1377         struct soc_camera_host *ici = to_soc_    
1378         const struct soc_camera_format_xlate     
1379         struct v4l2_pix_format *pix = &f->fmt    
1380         __u32 pixfmt = pix->pixelformat;         
1381         enum v4l2_field field;                   
1382         int ret;                                 
1383                                                  
1384         xlate = soc_camera_xlate_by_fourcc(ic    
1385         if (!xlate) {                            
1386                 dev_warn(ici->dev, "Format %x    
1387                 return -EINVAL;                  
1388         }                                        
1389                                                  
1390         /*                                       
1391          * Limit to pxa hardware capabilities    
1392          * images size to be a multiple of 16    
1393          * inserted between Y and U planes, a    
1394          * the YUV422P standard.                 
1395          */                                      
1396         v4l_bound_align_image(&pix->width, 48    
1397                               &pix->height, 3    
1398                               xlate->host_fmt    
1399                                                  
1400         pix->bytesperline = pix->width *         
1401                 DIV_ROUND_UP(xlate->host_fmt-    
1402         pix->sizeimage = pix->height * pix->b    
1403                                                  
1404         /* camera has to see its format, but     
1405         pix->pixelformat = xlate->cam_fmt->fo    
1406         /* limit to sensor capabilities */       
1407         ret = icd->ops->try_fmt(icd, f);         
1408         pix->pixelformat = xlate->host_fmt->f    
1409                                                  
1410         field = pix->field;                      
1411                                                  
1412         if (field == V4L2_FIELD_ANY) {           
1413                 pix->field = V4L2_FIELD_NONE;    
1414         } else if (field != V4L2_FIELD_NONE)     
1415                 dev_err(&icd->dev, "Field typ    
1416                 return -EINVAL;                  
1417         }                                        
1418                                                  
1419         return ret;                              
1420 }                                                
1421                                                  
1422 static int pxa_camera_reqbufs(struct soc_came    
1423                               struct v4l2_req    
1424 {                                                
1425         int i;                                   
1426                                                  
1427         /* This is for locking debugging only    
1428          * check whether .prepare is ever cal    
1429          * a dma IRQ can occur for an in-work    
1430          * it hadn't triggered */                
1431         for (i = 0; i < p->count; i++) {         
1432                 struct pxa_buffer *buf = cont    
1433                                                  
1434                 buf->inwork = 0;                 
1435                 INIT_LIST_HEAD(&buf->vb.queue    
1436         }                                        
1437                                                  
1438         return 0;                                
1439 }                                                
1440                                                  
1441 static unsigned int pxa_camera_poll(struct fi    
1442 {                                                
1443         struct soc_camera_file *icf = file->p    
1444         struct pxa_buffer *buf;                  
1445                                                  
1446         buf = list_entry(icf->vb_vidq.stream.    
1447                          vb.stream);             
1448                                                  
1449         poll_wait(file, &buf->vb.done, pt);      
1450                                                  
1451         if (buf->vb.state == VIDEOBUF_DONE ||    
1452             buf->vb.state == VIDEOBUF_ERROR)     
1453                 return POLLIN|POLLRDNORM;        
1454                                                  
1455         return 0;                                
1456 }                                                
1457                                                  
1458 static int pxa_camera_querycap(struct soc_cam    
1459                                struct v4l2_ca    
1460 {                                                
1461         /* cap->name is set by the firendly c    
1462         strlcpy(cap->card, pxa_cam_driver_des    
1463         cap->version = PXA_CAM_VERSION_CODE;     
1464         cap->capabilities = V4L2_CAP_VIDEO_CA    
1465                                                  
1466         return 0;                                
1467 }                                                
1468                                                  
1469 static int pxa_camera_suspend(struct soc_came    
1470 {                                                
1471         struct soc_camera_host *ici = to_soc_    
1472         struct pxa_camera_dev *pcdev = ici->p    
1473         int i = 0, ret = 0;                      
1474                                                  
1475         pcdev->save_cicr[i++] = __raw_readl(p    
1476         pcdev->save_cicr[i++] = __raw_readl(p    
1477         pcdev->save_cicr[i++] = __raw_readl(p    
1478         pcdev->save_cicr[i++] = __raw_readl(p    
1479         pcdev->save_cicr[i++] = __raw_readl(p    
1480                                                  
1481         if ((pcdev->icd) && (pcdev->icd->ops-    
1482                 ret = pcdev->icd->ops->suspen    
1483                                                  
1484         return ret;                              
1485 }                                                
1486                                                  
1487 static int pxa_camera_resume(struct soc_camer    
1488 {                                                
1489         struct soc_camera_host *ici = to_soc_    
1490         struct pxa_camera_dev *pcdev = ici->p    
1491         int i = 0, ret = 0;                      
1492                                                  
1493         DRCMR(68) = pcdev->dma_chans[0] | DRC    
1494         DRCMR(69) = pcdev->dma_chans[1] | DRC    
1495         DRCMR(70) = pcdev->dma_chans[2] | DRC    
1496                                                  
1497         __raw_writel(pcdev->save_cicr[i++] &     
1498         __raw_writel(pcdev->save_cicr[i++], p    
1499         __raw_writel(pcdev->save_cicr[i++], p    
1500         __raw_writel(pcdev->save_cicr[i++], p    
1501         __raw_writel(pcdev->save_cicr[i++], p    
1502                                                  
1503         if ((pcdev->icd) && (pcdev->icd->ops-    
1504                 ret = pcdev->icd->ops->resume    
1505                                                  
1506         /* Restart frame capture if active bu    
1507         if (!ret && pcdev->active)               
1508                 pxa_camera_start_capture(pcde    
1509                                                  
1510         return ret;                              
1511 }                                                
1512                                                  
1513 static struct soc_camera_host_ops pxa_soc_cam    
1514         .owner          = THIS_MODULE,           
1515         .add            = pxa_camera_add_devi    
1516         .remove         = pxa_camera_remove_d    
1517         .suspend        = pxa_camera_suspend,    
1518         .resume         = pxa_camera_resume,     
1519         .set_crop       = pxa_camera_set_crop    
1520         .get_formats    = pxa_camera_get_form    
1521         .set_fmt        = pxa_camera_set_fmt,    
1522         .try_fmt        = pxa_camera_try_fmt,    
1523         .init_videobuf  = pxa_camera_init_vid    
1524         .reqbufs        = pxa_camera_reqbufs,    
1525         .poll           = pxa_camera_poll,       
1526         .querycap       = pxa_camera_querycap    
1527         .set_bus_param  = pxa_camera_set_bus_    
1528 };                                               
1529                                                  
1530 static int __devinit pxa_camera_probe(struct     
1531 {                                                
1532         struct pxa_camera_dev *pcdev;            
1533         struct resource *res;                    
1534         void __iomem *base;                      
1535         int irq;                                 
1536         int err = 0;                             
1537                                                  
1538         res = platform_get_resource(pdev, IOR    
1539         irq = platform_get_irq(pdev, 0);         
1540         if (!res || irq < 0) {                   
1541                 err = -ENODEV;                   
1542                 goto exit;                       
1543         }                                        
1544                                                  
1545         pcdev = kzalloc(sizeof(*pcdev), GFP_K    
1546         if (!pcdev) {                            
1547                 dev_err(&pdev->dev, "Could no    
1548                 err = -ENOMEM;                   
1549                 goto exit;                       
1550         }                                        
1551                                                  
1552         pcdev->clk = clk_get(&pdev->dev, NULL    
1553         if (IS_ERR(pcdev->clk)) {                
1554                 err = PTR_ERR(pcdev->clk);       
1555                 goto exit_kfree;                 
1556         }                                        
1557                                                  
1558         pcdev->res = res;                        
1559                                                  
1560         pcdev->pdata = pdev->dev.platform_dat    
1561         pcdev->platform_flags = pcdev->pdata-    
1562         if (!(pcdev->platform_flags & (PXA_CA    
1563                         PXA_CAMERA_DATAWIDTH_    
1564                 /* Platform hasn't set availa    
1565                  * Warn and use a default. */    
1566                 dev_warn(&pdev->dev, "WARNING    
1567                          "data widths, using     
1568                 pcdev->platform_flags |= PXA_    
1569         }                                        
1570         pcdev->mclk = pcdev->pdata->mclk_10kh    
1571         if (!pcdev->mclk) {                      
1572                 dev_warn(&pdev->dev,             
1573                          "mclk == 0! Please,     
1574                          "Using default 20MHz    
1575                 pcdev->mclk = 20000000;          
1576         }                                        
1577                                                  
1578         pcdev->soc_host.dev = &pdev->dev;        
1579         pcdev->mclk_divisor = mclk_get_diviso    
1580                                                  
1581         INIT_LIST_HEAD(&pcdev->capture);         
1582         spin_lock_init(&pcdev->lock);            
1583                                                  
1584         /*                                       
1585          * Request the regions.                  
1586          */                                      
1587         if (!request_mem_region(res->start, r    
1588                                 PXA_CAM_DRV_N    
1589                 err = -EBUSY;                    
1590                 goto exit_clk;                   
1591         }                                        
1592                                                  
1593         base = ioremap(res->start, resource_s    
1594         if (!base) {                             
1595                 err = -ENOMEM;                   
1596                 goto exit_release;               
1597         }                                        
1598         pcdev->irq = irq;                        
1599         pcdev->base = base;                      
1600                                                  
1601         /* request dma */                        
1602         err = pxa_request_dma("CI_Y", DMA_PRI    
1603                               pxa_camera_dma_    
1604         if (err < 0) {                           
1605                 dev_err(&pdev->dev, "Can't re    
1606                 goto exit_iounmap;               
1607         }                                        
1608         pcdev->dma_chans[0] = err;               
1609         dev_dbg(&pdev->dev, "got DMA channel     
1610                                                  
1611         err = pxa_request_dma("CI_U", DMA_PRI    
1612                               pxa_camera_dma_    
1613         if (err < 0) {                           
1614                 dev_err(&pdev->dev, "Can't re    
1615                 goto exit_free_dma_y;            
1616         }                                        
1617         pcdev->dma_chans[1] = err;               
1618         dev_dbg(&pdev->dev, "got DMA channel     
1619                                                  
1620         err = pxa_request_dma("CI_V", DMA_PRI    
1621                               pxa_camera_dma_    
1622         if (err < 0) {                           
1623                 dev_err(&pdev->dev, "Can't re    
1624                 goto exit_free_dma_u;            
1625         }                                        
1626         pcdev->dma_chans[2] = err;               
1627         dev_dbg(&pdev->dev, "got DMA channel     
1628                                                  
1629         DRCMR(68) = pcdev->dma_chans[0] | DRC    
1630         DRCMR(69) = pcdev->dma_chans[1] | DRC    
1631         DRCMR(70) = pcdev->dma_chans[2] | DRC    
1632                                                  
1633         /* request irq */                        
1634         err = request_irq(pcdev->irq, pxa_cam    
1635                           pcdev);                
1636         if (err) {                               
1637                 dev_err(&pdev->dev, "Camera i    
1638                 goto exit_free_dma;              
1639         }                                        
1640                                                  
1641         pcdev->soc_host.drv_name        = PXA    
1642         pcdev->soc_host.ops             = &px    
1643         pcdev->soc_host.priv            = pcd    
1644         pcdev->soc_host.nr              = pde    
1645                                                  
1646         err = soc_camera_host_register(&pcdev    
1647         if (err)                                 
1648                 goto exit_free_irq;              
1649                                                  
1650         return 0;                                
1651                                                  
1652 exit_free_irq:                                   
1653         free_irq(pcdev->irq, pcdev);             
1654 exit_free_dma:                                   
1655         pxa_free_dma(pcdev->dma_chans[2]);       
1656 exit_free_dma_u:                                 
1657         pxa_free_dma(pcdev->dma_chans[1]);       
1658 exit_free_dma_y:                                 
1659         pxa_free_dma(pcdev->dma_chans[0]);       
1660 exit_iounmap:                                    
1661         iounmap(base);                           
1662 exit_release:                                    
1663         release_mem_region(res->start, resour    
1664 exit_clk:                                        
1665         clk_put(pcdev->clk);                     
1666 exit_kfree:                                      
1667         kfree(pcdev);                            
1668 exit:                                            
1669         return err;                              
1670 }                                                
1671                                                  
1672 static int __devexit pxa_camera_remove(struct    
1673 {                                                
1674         struct soc_camera_host *soc_host = to    
1675         struct pxa_camera_dev *pcdev = contai    
1676                                         struc    
1677         struct resource *res;                    
1678                                                  
1679         clk_put(pcdev->clk);                     
1680                                                  
1681         pxa_free_dma(pcdev->dma_chans[0]);       
1682         pxa_free_dma(pcdev->dma_chans[1]);       
1683         pxa_free_dma(pcdev->dma_chans[2]);       
1684         free_irq(pcdev->irq, pcdev);             
1685                                                  
1686         soc_camera_host_unregister(soc_host);    
1687                                                  
1688         iounmap(pcdev->base);                    
1689                                                  
1690         res = pcdev->res;                        
1691         release_mem_region(res->start, resour    
1692                                                  
1693         kfree(pcdev);                            
1694                                                  
1695         dev_info(&pdev->dev, "PXA Camera driv    
1696                                                  
1697         return 0;                                
1698 }                                                
1699                                                  
1700 static struct platform_driver pxa_camera_driv    
1701         .driver         = {                      
1702                 .name   = PXA_CAM_DRV_NAME,      
1703         },                                       
1704         .probe          = pxa_camera_probe,      
1705         .remove         = __devexit_p(pxa_cam    
1706 };                                               
1707                                                  
1708                                                  
1709 static int __init pxa_camera_init(void)          
1710 {                                                
1711         return platform_driver_register(&pxa_    
1712 }                                                
1713                                                  
1714 static void __exit pxa_camera_exit(void)         
1715 {                                                
1716         platform_driver_unregister(&pxa_camer    
1717 }                                                
1718                                                  
1719 module_init(pxa_camera_init);                    
1720 module_exit(pxa_camera_exit);                    
1721                                                  
1722 MODULE_DESCRIPTION("PXA27x SoC Camera Host dr    
1723 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@    
1724 MODULE_LICENSE("GPL");                           
1725                                                  
  This page was automatically generated by the LXR engine.