pxa_camera.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /*
  2. * V4L2 Driver for PXA camera host
  3. *
  4. * Copyright (C) 2006, Sascha Hauer, Pengutronix
  5. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/io.h>
  15. #include <linux/delay.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/errno.h>
  18. #include <linux/fs.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/time.h>
  24. #include <linux/version.h>
  25. #include <linux/device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mutex.h>
  28. #include <linux/clk.h>
  29. #include <media/v4l2-common.h>
  30. #include <media/v4l2-dev.h>
  31. #include <media/videobuf-dma-sg.h>
  32. #include <media/soc_camera.h>
  33. #include <linux/videodev2.h>
  34. #include <asm/dma.h>
  35. #include <mach/pxa-regs.h>
  36. #include <mach/camera.h>
  37. #define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
  38. #define PXA_CAM_DRV_NAME "pxa27x-camera"
  39. /* Camera Interface */
  40. #define CICR0 0x0000
  41. #define CICR1 0x0004
  42. #define CICR2 0x0008
  43. #define CICR3 0x000C
  44. #define CICR4 0x0010
  45. #define CISR 0x0014
  46. #define CIFR 0x0018
  47. #define CITOR 0x001C
  48. #define CIBR0 0x0028
  49. #define CIBR1 0x0030
  50. #define CIBR2 0x0038
  51. #define CICR0_DMAEN (1 << 31) /* DMA request enable */
  52. #define CICR0_PAR_EN (1 << 30) /* Parity enable */
  53. #define CICR0_SL_CAP_EN (1 << 29) /* Capture enable for slave mode */
  54. #define CICR0_ENB (1 << 28) /* Camera interface enable */
  55. #define CICR0_DIS (1 << 27) /* Camera interface disable */
  56. #define CICR0_SIM (0x7 << 24) /* Sensor interface mode mask */
  57. #define CICR0_TOM (1 << 9) /* Time-out mask */
  58. #define CICR0_RDAVM (1 << 8) /* Receive-data-available mask */
  59. #define CICR0_FEM (1 << 7) /* FIFO-empty mask */
  60. #define CICR0_EOLM (1 << 6) /* End-of-line mask */
  61. #define CICR0_PERRM (1 << 5) /* Parity-error mask */
  62. #define CICR0_QDM (1 << 4) /* Quick-disable mask */
  63. #define CICR0_CDM (1 << 3) /* Disable-done mask */
  64. #define CICR0_SOFM (1 << 2) /* Start-of-frame mask */
  65. #define CICR0_EOFM (1 << 1) /* End-of-frame mask */
  66. #define CICR0_FOM (1 << 0) /* FIFO-overrun mask */
  67. #define CICR1_TBIT (1 << 31) /* Transparency bit */
  68. #define CICR1_RGBT_CONV (0x3 << 29) /* RGBT conversion mask */
  69. #define CICR1_PPL (0x7ff << 15) /* Pixels per line mask */
  70. #define CICR1_RGB_CONV (0x7 << 12) /* RGB conversion mask */
  71. #define CICR1_RGB_F (1 << 11) /* RGB format */
  72. #define CICR1_YCBCR_F (1 << 10) /* YCbCr format */
  73. #define CICR1_RGB_BPP (0x7 << 7) /* RGB bis per pixel mask */
  74. #define CICR1_RAW_BPP (0x3 << 5) /* Raw bis per pixel mask */
  75. #define CICR1_COLOR_SP (0x3 << 3) /* Color space mask */
  76. #define CICR1_DW (0x7 << 0) /* Data width mask */
  77. #define CICR2_BLW (0xff << 24) /* Beginning-of-line pixel clock
  78. wait count mask */
  79. #define CICR2_ELW (0xff << 16) /* End-of-line pixel clock
  80. wait count mask */
  81. #define CICR2_HSW (0x3f << 10) /* Horizontal sync pulse width mask */
  82. #define CICR2_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
  83. wait count mask */
  84. #define CICR2_FSW (0x7 << 0) /* Frame stabilization
  85. wait count mask */
  86. #define CICR3_BFW (0xff << 24) /* Beginning-of-frame line clock
  87. wait count mask */
  88. #define CICR3_EFW (0xff << 16) /* End-of-frame line clock
  89. wait count mask */
  90. #define CICR3_VSW (0x3f << 10) /* Vertical sync pulse width mask */
  91. #define CICR3_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
  92. wait count mask */
  93. #define CICR3_LPF (0x7ff << 0) /* Lines per frame mask */
  94. #define CICR4_MCLK_DLY (0x3 << 24) /* MCLK Data Capture Delay mask */
  95. #define CICR4_PCLK_EN (1 << 23) /* Pixel clock enable */
  96. #define CICR4_PCP (1 << 22) /* Pixel clock polarity */
  97. #define CICR4_HSP (1 << 21) /* Horizontal sync polarity */
  98. #define CICR4_VSP (1 << 20) /* Vertical sync polarity */
  99. #define CICR4_MCLK_EN (1 << 19) /* MCLK enable */
  100. #define CICR4_FR_RATE (0x7 << 8) /* Frame rate mask */
  101. #define CICR4_DIV (0xff << 0) /* Clock divisor mask */
  102. #define CISR_FTO (1 << 15) /* FIFO time-out */
  103. #define CISR_RDAV_2 (1 << 14) /* Channel 2 receive data available */
  104. #define CISR_RDAV_1 (1 << 13) /* Channel 1 receive data available */
  105. #define CISR_RDAV_0 (1 << 12) /* Channel 0 receive data available */
  106. #define CISR_FEMPTY_2 (1 << 11) /* Channel 2 FIFO empty */
  107. #define CISR_FEMPTY_1 (1 << 10) /* Channel 1 FIFO empty */
  108. #define CISR_FEMPTY_0 (1 << 9) /* Channel 0 FIFO empty */
  109. #define CISR_EOL (1 << 8) /* End of line */
  110. #define CISR_PAR_ERR (1 << 7) /* Parity error */
  111. #define CISR_CQD (1 << 6) /* Camera interface quick disable */
  112. #define CISR_CDD (1 << 5) /* Camera interface disable done */
  113. #define CISR_SOF (1 << 4) /* Start of frame */
  114. #define CISR_EOF (1 << 3) /* End of frame */
  115. #define CISR_IFO_2 (1 << 2) /* FIFO overrun for Channel 2 */
  116. #define CISR_IFO_1 (1 << 1) /* FIFO overrun for Channel 1 */
  117. #define CISR_IFO_0 (1 << 0) /* FIFO overrun for Channel 0 */
  118. #define CIFR_FLVL2 (0x7f << 23) /* FIFO 2 level mask */
  119. #define CIFR_FLVL1 (0x7f << 16) /* FIFO 1 level mask */
  120. #define CIFR_FLVL0 (0xff << 8) /* FIFO 0 level mask */
  121. #define CIFR_THL_0 (0x3 << 4) /* Threshold Level for Channel 0 FIFO */
  122. #define CIFR_RESET_F (1 << 3) /* Reset input FIFOs */
  123. #define CIFR_FEN2 (1 << 2) /* FIFO enable for channel 2 */
  124. #define CIFR_FEN1 (1 << 1) /* FIFO enable for channel 1 */
  125. #define CIFR_FEN0 (1 << 0) /* FIFO enable for channel 0 */
  126. #define CICR0_SIM_MP (0 << 24)
  127. #define CICR0_SIM_SP (1 << 24)
  128. #define CICR0_SIM_MS (2 << 24)
  129. #define CICR0_SIM_EP (3 << 24)
  130. #define CICR0_SIM_ES (4 << 24)
  131. #define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
  132. #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
  133. #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
  134. #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
  135. #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
  136. #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
  137. #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
  138. #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
  139. #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
  140. #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
  141. #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
  142. #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
  143. #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
  144. #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
  145. #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
  146. CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
  147. CICR0_EOFM | CICR0_FOM)
  148. static DEFINE_MUTEX(camera_lock);
  149. /*
  150. * Structures
  151. */
  152. enum pxa_camera_active_dma {
  153. DMA_Y = 0x1,
  154. DMA_U = 0x2,
  155. DMA_V = 0x4,
  156. };
  157. /* descriptor needed for the PXA DMA engine */
  158. struct pxa_cam_dma {
  159. dma_addr_t sg_dma;
  160. struct pxa_dma_desc *sg_cpu;
  161. size_t sg_size;
  162. int sglen;
  163. };
  164. /* buffer for one video frame */
  165. struct pxa_buffer {
  166. /* common v4l buffer stuff -- must be first */
  167. struct videobuf_buffer vb;
  168. const struct soc_camera_data_format *fmt;
  169. /* our descriptor lists for Y, U and V channels */
  170. struct pxa_cam_dma dmas[3];
  171. int inwork;
  172. enum pxa_camera_active_dma active_dma;
  173. };
  174. struct pxa_camera_dev {
  175. struct device *dev;
  176. /* PXA27x is only supposed to handle one camera on its Quick Capture
  177. * interface. If anyone ever builds hardware to enable more than
  178. * one camera, they will have to modify this driver too */
  179. struct soc_camera_device *icd;
  180. struct clk *clk;
  181. unsigned int irq;
  182. void __iomem *base;
  183. int channels;
  184. unsigned int dma_chans[3];
  185. struct pxacamera_platform_data *pdata;
  186. struct resource *res;
  187. unsigned long platform_flags;
  188. unsigned long ciclk;
  189. unsigned long mclk;
  190. u32 mclk_divisor;
  191. struct list_head capture;
  192. spinlock_t lock;
  193. struct pxa_buffer *active;
  194. struct pxa_dma_desc *sg_tail[3];
  195. u32 save_cicr[5];
  196. };
  197. static const char *pxa_cam_driver_description = "PXA_Camera";
  198. static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
  199. /*
  200. * Videobuf operations
  201. */
  202. static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
  203. unsigned int *size)
  204. {
  205. struct soc_camera_device *icd = vq->priv_data;
  206. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  207. struct pxa_camera_dev *pcdev = ici->priv;
  208. dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
  209. /* planar capture requires Y, U and V buffers to be page aligned */
  210. if (pcdev->channels == 3) {
  211. *size = PAGE_ALIGN(icd->width * icd->height); /* Y pages */
  212. *size += PAGE_ALIGN(icd->width * icd->height / 2); /* U pages */
  213. *size += PAGE_ALIGN(icd->width * icd->height / 2); /* V pages */
  214. } else {
  215. *size = icd->width * icd->height *
  216. ((icd->current_fmt->depth + 7) >> 3);
  217. }
  218. if (0 == *count)
  219. *count = 32;
  220. while (*size * *count > vid_limit * 1024 * 1024)
  221. (*count)--;
  222. return 0;
  223. }
  224. static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
  225. {
  226. struct soc_camera_device *icd = vq->priv_data;
  227. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  228. struct pxa_camera_dev *pcdev = ici->priv;
  229. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  230. int i;
  231. BUG_ON(in_interrupt());
  232. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  233. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  234. /* This waits until this buffer is out of danger, i.e., until it is no
  235. * longer in STATE_QUEUED or STATE_ACTIVE */
  236. videobuf_waiton(&buf->vb, 0, 0);
  237. videobuf_dma_unmap(vq, dma);
  238. videobuf_dma_free(dma);
  239. for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
  240. if (buf->dmas[i].sg_cpu)
  241. dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
  242. buf->dmas[i].sg_cpu,
  243. buf->dmas[i].sg_dma);
  244. buf->dmas[i].sg_cpu = NULL;
  245. }
  246. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  247. }
  248. static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
  249. struct pxa_buffer *buf,
  250. struct videobuf_dmabuf *dma, int channel,
  251. int sglen, int sg_start, int cibr,
  252. unsigned int size)
  253. {
  254. struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
  255. int i;
  256. if (pxa_dma->sg_cpu)
  257. dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
  258. pxa_dma->sg_cpu, pxa_dma->sg_dma);
  259. pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
  260. pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
  261. &pxa_dma->sg_dma, GFP_KERNEL);
  262. if (!pxa_dma->sg_cpu)
  263. return -ENOMEM;
  264. pxa_dma->sglen = sglen;
  265. for (i = 0; i < sglen; i++) {
  266. int sg_i = sg_start + i;
  267. struct scatterlist *sg = dma->sglist;
  268. unsigned int dma_len = sg_dma_len(&sg[sg_i]), xfer_len;
  269. pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
  270. pxa_dma->sg_cpu[i].dtadr = sg_dma_address(&sg[sg_i]);
  271. /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
  272. xfer_len = (min(dma_len, size) + 7) & ~7;
  273. pxa_dma->sg_cpu[i].dcmd =
  274. DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
  275. size -= dma_len;
  276. pxa_dma->sg_cpu[i].ddadr =
  277. pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
  278. }
  279. pxa_dma->sg_cpu[sglen - 1].ddadr = DDADR_STOP;
  280. pxa_dma->sg_cpu[sglen - 1].dcmd |= DCMD_ENDIRQEN;
  281. return 0;
  282. }
  283. static int pxa_videobuf_prepare(struct videobuf_queue *vq,
  284. struct videobuf_buffer *vb, enum v4l2_field field)
  285. {
  286. struct soc_camera_device *icd = vq->priv_data;
  287. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  288. struct pxa_camera_dev *pcdev = ici->priv;
  289. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  290. int ret;
  291. int sglen_y, sglen_yu = 0, sglen_u = 0, sglen_v = 0;
  292. int size_y, size_u = 0, size_v = 0;
  293. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  294. vb, vb->baddr, vb->bsize);
  295. /* Added list head initialization on alloc */
  296. WARN_ON(!list_empty(&vb->queue));
  297. #ifdef DEBUG
  298. /* This can be useful if you want to see if we actually fill
  299. * the buffer with something */
  300. memset((void *)vb->baddr, 0xaa, vb->bsize);
  301. #endif
  302. BUG_ON(NULL == icd->current_fmt);
  303. /* I think, in buf_prepare you only have to protect global data,
  304. * the actual buffer is yours */
  305. buf->inwork = 1;
  306. if (buf->fmt != icd->current_fmt ||
  307. vb->width != icd->width ||
  308. vb->height != icd->height ||
  309. vb->field != field) {
  310. buf->fmt = icd->current_fmt;
  311. vb->width = icd->width;
  312. vb->height = icd->height;
  313. vb->field = field;
  314. vb->state = VIDEOBUF_NEEDS_INIT;
  315. }
  316. vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
  317. if (0 != vb->baddr && vb->bsize < vb->size) {
  318. ret = -EINVAL;
  319. goto out;
  320. }
  321. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  322. unsigned int size = vb->size;
  323. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  324. ret = videobuf_iolock(vq, vb, NULL);
  325. if (ret)
  326. goto fail;
  327. if (pcdev->channels == 3) {
  328. /* FIXME the calculations should be more precise */
  329. sglen_y = dma->sglen / 2;
  330. sglen_u = sglen_v = dma->sglen / 4 + 1;
  331. sglen_yu = sglen_y + sglen_u;
  332. size_y = size / 2;
  333. size_u = size_v = size / 4;
  334. } else {
  335. sglen_y = dma->sglen;
  336. size_y = size;
  337. }
  338. /* init DMA for Y channel */
  339. ret = pxa_init_dma_channel(pcdev, buf, dma, 0, sglen_y,
  340. 0, 0x28, size_y);
  341. if (ret) {
  342. dev_err(pcdev->dev,
  343. "DMA initialization for Y/RGB failed\n");
  344. goto fail;
  345. }
  346. if (pcdev->channels == 3) {
  347. /* init DMA for U channel */
  348. ret = pxa_init_dma_channel(pcdev, buf, dma, 1, sglen_u,
  349. sglen_y, 0x30, size_u);
  350. if (ret) {
  351. dev_err(pcdev->dev,
  352. "DMA initialization for U failed\n");
  353. goto fail_u;
  354. }
  355. /* init DMA for V channel */
  356. ret = pxa_init_dma_channel(pcdev, buf, dma, 2, sglen_v,
  357. sglen_yu, 0x38, size_v);
  358. if (ret) {
  359. dev_err(pcdev->dev,
  360. "DMA initialization for V failed\n");
  361. goto fail_v;
  362. }
  363. }
  364. vb->state = VIDEOBUF_PREPARED;
  365. }
  366. buf->inwork = 0;
  367. buf->active_dma = DMA_Y;
  368. if (pcdev->channels == 3)
  369. buf->active_dma |= DMA_U | DMA_V;
  370. return 0;
  371. fail_v:
  372. dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
  373. buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
  374. fail_u:
  375. dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
  376. buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
  377. fail:
  378. free_buffer(vq, buf);
  379. out:
  380. buf->inwork = 0;
  381. return ret;
  382. }
  383. static void pxa_videobuf_queue(struct videobuf_queue *vq,
  384. struct videobuf_buffer *vb)
  385. {
  386. struct soc_camera_device *icd = vq->priv_data;
  387. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  388. struct pxa_camera_dev *pcdev = ici->priv;
  389. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  390. struct pxa_buffer *active;
  391. unsigned long flags;
  392. int i;
  393. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  394. vb, vb->baddr, vb->bsize);
  395. spin_lock_irqsave(&pcdev->lock, flags);
  396. list_add_tail(&vb->queue, &pcdev->capture);
  397. vb->state = VIDEOBUF_ACTIVE;
  398. active = pcdev->active;
  399. if (!active) {
  400. unsigned long cifr, cicr0;
  401. cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
  402. __raw_writel(cifr, pcdev->base + CIFR);
  403. for (i = 0; i < pcdev->channels; i++) {
  404. DDADR(pcdev->dma_chans[i]) = buf->dmas[i].sg_dma;
  405. DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
  406. pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen - 1;
  407. }
  408. pcdev->active = buf;
  409. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
  410. __raw_writel(cicr0, pcdev->base + CICR0);
  411. } else {
  412. struct pxa_cam_dma *buf_dma;
  413. struct pxa_cam_dma *act_dma;
  414. int nents;
  415. for (i = 0; i < pcdev->channels; i++) {
  416. buf_dma = &buf->dmas[i];
  417. act_dma = &active->dmas[i];
  418. nents = buf_dma->sglen;
  419. /* Stop DMA engine */
  420. DCSR(pcdev->dma_chans[i]) = 0;
  421. /* Add the descriptors we just initialized to
  422. the currently running chain */
  423. pcdev->sg_tail[i]->ddadr = buf_dma->sg_dma;
  424. pcdev->sg_tail[i] = buf_dma->sg_cpu + buf_dma->sglen - 1;
  425. /* Setup a dummy descriptor with the DMA engines current
  426. * state
  427. */
  428. buf_dma->sg_cpu[nents].dsadr =
  429. pcdev->res->start + 0x28 + i*8; /* CIBRx */
  430. buf_dma->sg_cpu[nents].dtadr =
  431. DTADR(pcdev->dma_chans[i]);
  432. buf_dma->sg_cpu[nents].dcmd =
  433. DCMD(pcdev->dma_chans[i]);
  434. if (DDADR(pcdev->dma_chans[i]) == DDADR_STOP) {
  435. /* The DMA engine is on the last
  436. descriptor, set the next descriptors
  437. address to the descriptors we just
  438. initialized */
  439. buf_dma->sg_cpu[nents].ddadr = buf_dma->sg_dma;
  440. } else {
  441. buf_dma->sg_cpu[nents].ddadr =
  442. DDADR(pcdev->dma_chans[i]);
  443. }
  444. /* The next descriptor is the dummy descriptor */
  445. DDADR(pcdev->dma_chans[i]) = buf_dma->sg_dma + nents *
  446. sizeof(struct pxa_dma_desc);
  447. DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
  448. }
  449. }
  450. spin_unlock_irqrestore(&pcdev->lock, flags);
  451. }
  452. static void pxa_videobuf_release(struct videobuf_queue *vq,
  453. struct videobuf_buffer *vb)
  454. {
  455. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  456. #ifdef DEBUG
  457. struct soc_camera_device *icd = vq->priv_data;
  458. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  459. vb, vb->baddr, vb->bsize);
  460. switch (vb->state) {
  461. case VIDEOBUF_ACTIVE:
  462. dev_dbg(&icd->dev, "%s (active)\n", __func__);
  463. break;
  464. case VIDEOBUF_QUEUED:
  465. dev_dbg(&icd->dev, "%s (queued)\n", __func__);
  466. break;
  467. case VIDEOBUF_PREPARED:
  468. dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
  469. break;
  470. default:
  471. dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
  472. break;
  473. }
  474. #endif
  475. free_buffer(vq, buf);
  476. }
  477. static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
  478. struct videobuf_buffer *vb,
  479. struct pxa_buffer *buf)
  480. {
  481. unsigned long cicr0;
  482. /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
  483. list_del_init(&vb->queue);
  484. vb->state = VIDEOBUF_DONE;
  485. do_gettimeofday(&vb->ts);
  486. vb->field_count++;
  487. wake_up(&vb->done);
  488. if (list_empty(&pcdev->capture)) {
  489. pcdev->active = NULL;
  490. DCSR(pcdev->dma_chans[0]) = 0;
  491. DCSR(pcdev->dma_chans[1]) = 0;
  492. DCSR(pcdev->dma_chans[2]) = 0;
  493. cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
  494. __raw_writel(cicr0, pcdev->base + CICR0);
  495. return;
  496. }
  497. pcdev->active = list_entry(pcdev->capture.next,
  498. struct pxa_buffer, vb.queue);
  499. }
  500. static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
  501. enum pxa_camera_active_dma act_dma)
  502. {
  503. struct pxa_buffer *buf;
  504. unsigned long flags;
  505. u32 status, camera_status, overrun;
  506. struct videobuf_buffer *vb;
  507. unsigned long cifr, cicr0;
  508. spin_lock_irqsave(&pcdev->lock, flags);
  509. status = DCSR(channel);
  510. DCSR(channel) = status | DCSR_ENDINTR;
  511. if (status & DCSR_BUSERR) {
  512. dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
  513. goto out;
  514. }
  515. if (!(status & DCSR_ENDINTR)) {
  516. dev_err(pcdev->dev, "Unknown DMA IRQ source, "
  517. "status: 0x%08x\n", status);
  518. goto out;
  519. }
  520. if (!pcdev->active) {
  521. dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
  522. goto out;
  523. }
  524. camera_status = __raw_readl(pcdev->base + CISR);
  525. overrun = CISR_IFO_0;
  526. if (pcdev->channels == 3)
  527. overrun |= CISR_IFO_1 | CISR_IFO_2;
  528. if (camera_status & overrun) {
  529. dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n", camera_status);
  530. /* Stop the Capture Interface */
  531. cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
  532. __raw_writel(cicr0, pcdev->base + CICR0);
  533. /* Stop DMA */
  534. DCSR(channel) = 0;
  535. /* Reset the FIFOs */
  536. cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
  537. __raw_writel(cifr, pcdev->base + CIFR);
  538. /* Enable End-Of-Frame Interrupt */
  539. cicr0 &= ~CICR0_EOFM;
  540. __raw_writel(cicr0, pcdev->base + CICR0);
  541. /* Restart the Capture Interface */
  542. __raw_writel(cicr0 | CICR0_ENB, pcdev->base + CICR0);
  543. goto out;
  544. }
  545. vb = &pcdev->active->vb;
  546. buf = container_of(vb, struct pxa_buffer, vb);
  547. WARN_ON(buf->inwork || list_empty(&vb->queue));
  548. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  549. vb, vb->baddr, vb->bsize);
  550. buf->active_dma &= ~act_dma;
  551. if (!buf->active_dma)
  552. pxa_camera_wakeup(pcdev, vb, buf);
  553. out:
  554. spin_unlock_irqrestore(&pcdev->lock, flags);
  555. }
  556. static void pxa_camera_dma_irq_y(int channel, void *data)
  557. {
  558. struct pxa_camera_dev *pcdev = data;
  559. pxa_camera_dma_irq(channel, pcdev, DMA_Y);
  560. }
  561. static void pxa_camera_dma_irq_u(int channel, void *data)
  562. {
  563. struct pxa_camera_dev *pcdev = data;
  564. pxa_camera_dma_irq(channel, pcdev, DMA_U);
  565. }
  566. static void pxa_camera_dma_irq_v(int channel, void *data)
  567. {
  568. struct pxa_camera_dev *pcdev = data;
  569. pxa_camera_dma_irq(channel, pcdev, DMA_V);
  570. }
  571. static struct videobuf_queue_ops pxa_videobuf_ops = {
  572. .buf_setup = pxa_videobuf_setup,
  573. .buf_prepare = pxa_videobuf_prepare,
  574. .buf_queue = pxa_videobuf_queue,
  575. .buf_release = pxa_videobuf_release,
  576. };
  577. static void pxa_camera_init_videobuf(struct videobuf_queue *q,
  578. struct soc_camera_device *icd)
  579. {
  580. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  581. struct pxa_camera_dev *pcdev = ici->priv;
  582. /* We must pass NULL as dev pointer, then all pci_* dma operations
  583. * transform to normal dma_* ones. */
  584. videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
  585. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  586. sizeof(struct pxa_buffer), icd);
  587. }
  588. static u32 mclk_get_divisor(struct pxa_camera_dev *pcdev)
  589. {
  590. unsigned long mclk = pcdev->mclk;
  591. u32 div;
  592. unsigned long lcdclk;
  593. lcdclk = clk_get_rate(pcdev->clk);
  594. pcdev->ciclk = lcdclk;
  595. /* mclk <= ciclk / 4 (27.4.2) */
  596. if (mclk > lcdclk / 4) {
  597. mclk = lcdclk / 4;
  598. dev_warn(pcdev->dev, "Limiting master clock to %lu\n", mclk);
  599. }
  600. /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
  601. div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
  602. /* If we're not supplying MCLK, leave it at 0 */
  603. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  604. pcdev->mclk = lcdclk / (2 * (div + 1));
  605. dev_dbg(pcdev->dev, "LCD clock %luHz, target freq %luHz, "
  606. "divisor %u\n", lcdclk, mclk, div);
  607. return div;
  608. }
  609. static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
  610. unsigned long pclk)
  611. {
  612. /* We want a timeout > 1 pixel time, not ">=" */
  613. u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
  614. __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
  615. }
  616. static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
  617. {
  618. struct pxacamera_platform_data *pdata = pcdev->pdata;
  619. u32 cicr4 = 0;
  620. dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
  621. pcdev, pdata);
  622. if (pdata && pdata->init) {
  623. dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
  624. pdata->init(pcdev->dev);
  625. }
  626. /* disable all interrupts */
  627. __raw_writel(0x3ff, pcdev->base + CICR0);
  628. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  629. cicr4 |= CICR4_PCLK_EN;
  630. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  631. cicr4 |= CICR4_MCLK_EN;
  632. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  633. cicr4 |= CICR4_PCP;
  634. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  635. cicr4 |= CICR4_HSP;
  636. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  637. cicr4 |= CICR4_VSP;
  638. __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
  639. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  640. /* Initialise the timeout under the assumption pclk = mclk */
  641. recalculate_fifo_timeout(pcdev, pcdev->mclk);
  642. else
  643. /* "Safe default" - 13MHz */
  644. recalculate_fifo_timeout(pcdev, 13000000);
  645. clk_enable(pcdev->clk);
  646. }
  647. static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
  648. {
  649. clk_disable(pcdev->clk);
  650. }
  651. static irqreturn_t pxa_camera_irq(int irq, void *data)
  652. {
  653. struct pxa_camera_dev *pcdev = data;
  654. unsigned long status, cicr0;
  655. status = __raw_readl(pcdev->base + CISR);
  656. dev_dbg(pcdev->dev, "Camera interrupt status 0x%lx\n", status);
  657. if (!status)
  658. return IRQ_NONE;
  659. __raw_writel(status, pcdev->base + CISR);
  660. if (status & CISR_EOF) {
  661. int i;
  662. for (i = 0; i < pcdev->channels; i++) {
  663. DDADR(pcdev->dma_chans[i]) =
  664. pcdev->active->dmas[i].sg_dma;
  665. DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
  666. }
  667. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
  668. __raw_writel(cicr0, pcdev->base + CICR0);
  669. }
  670. return IRQ_HANDLED;
  671. }
  672. /* The following two functions absolutely depend on the fact, that
  673. * there can be only one camera on PXA quick capture interface */
  674. static int pxa_camera_add_device(struct soc_camera_device *icd)
  675. {
  676. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  677. struct pxa_camera_dev *pcdev = ici->priv;
  678. int ret;
  679. mutex_lock(&camera_lock);
  680. if (pcdev->icd) {
  681. ret = -EBUSY;
  682. goto ebusy;
  683. }
  684. dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
  685. icd->devnum);
  686. pxa_camera_activate(pcdev);
  687. ret = icd->ops->init(icd);
  688. if (!ret)
  689. pcdev->icd = icd;
  690. ebusy:
  691. mutex_unlock(&camera_lock);
  692. return ret;
  693. }
  694. static void pxa_camera_remove_device(struct soc_camera_device *icd)
  695. {
  696. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  697. struct pxa_camera_dev *pcdev = ici->priv;
  698. BUG_ON(icd != pcdev->icd);
  699. dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
  700. icd->devnum);
  701. /* disable capture, disable interrupts */
  702. __raw_writel(0x3ff, pcdev->base + CICR0);
  703. /* Stop DMA engine */
  704. DCSR(pcdev->dma_chans[0]) = 0;
  705. DCSR(pcdev->dma_chans[1]) = 0;
  706. DCSR(pcdev->dma_chans[2]) = 0;
  707. icd->ops->release(icd);
  708. pxa_camera_deactivate(pcdev);
  709. pcdev->icd = NULL;
  710. }
  711. static int test_platform_param(struct pxa_camera_dev *pcdev,
  712. unsigned char buswidth, unsigned long *flags)
  713. {
  714. /*
  715. * Platform specified synchronization and pixel clock polarities are
  716. * only a recommendation and are only used during probing. The PXA270
  717. * quick capture interface supports both.
  718. */
  719. *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  720. SOCAM_MASTER : SOCAM_SLAVE) |
  721. SOCAM_HSYNC_ACTIVE_HIGH |
  722. SOCAM_HSYNC_ACTIVE_LOW |
  723. SOCAM_VSYNC_ACTIVE_HIGH |
  724. SOCAM_VSYNC_ACTIVE_LOW |
  725. SOCAM_PCLK_SAMPLE_RISING |
  726. SOCAM_PCLK_SAMPLE_FALLING;
  727. /* If requested data width is supported by the platform, use it */
  728. switch (buswidth) {
  729. case 10:
  730. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
  731. return -EINVAL;
  732. *flags |= SOCAM_DATAWIDTH_10;
  733. break;
  734. case 9:
  735. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
  736. return -EINVAL;
  737. *flags |= SOCAM_DATAWIDTH_9;
  738. break;
  739. case 8:
  740. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
  741. return -EINVAL;
  742. *flags |= SOCAM_DATAWIDTH_8;
  743. break;
  744. default:
  745. return -EINVAL;
  746. }
  747. return 0;
  748. }
  749. static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
  750. {
  751. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  752. struct pxa_camera_dev *pcdev = ici->priv;
  753. unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
  754. u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0;
  755. int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
  756. if (ret < 0)
  757. return ret;
  758. camera_flags = icd->ops->query_bus_param(icd);
  759. common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
  760. if (!common_flags)
  761. return -EINVAL;
  762. pcdev->channels = 1;
  763. /* Make choises, based on platform preferences */
  764. if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
  765. (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
  766. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  767. common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
  768. else
  769. common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
  770. }
  771. if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
  772. (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
  773. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  774. common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
  775. else
  776. common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
  777. }
  778. if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
  779. (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
  780. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  781. common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
  782. else
  783. common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
  784. }
  785. ret = icd->ops->set_bus_param(icd, common_flags);
  786. if (ret < 0)
  787. return ret;
  788. /* Datawidth is now guaranteed to be equal to one of the three values.
  789. * We fix bit-per-pixel equal to data-width... */
  790. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  791. case SOCAM_DATAWIDTH_10:
  792. dw = 4;
  793. bpp = 0x40;
  794. break;
  795. case SOCAM_DATAWIDTH_9:
  796. dw = 3;
  797. bpp = 0x20;
  798. break;
  799. default:
  800. /* Actually it can only be 8 now,
  801. * default is just to silence compiler warnings */
  802. case SOCAM_DATAWIDTH_8:
  803. dw = 2;
  804. bpp = 0;
  805. }
  806. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  807. cicr4 |= CICR4_PCLK_EN;
  808. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  809. cicr4 |= CICR4_MCLK_EN;
  810. if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
  811. cicr4 |= CICR4_PCP;
  812. if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
  813. cicr4 |= CICR4_HSP;
  814. if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
  815. cicr4 |= CICR4_VSP;
  816. cicr0 = __raw_readl(pcdev->base + CICR0);
  817. if (cicr0 & CICR0_ENB)
  818. __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
  819. cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
  820. switch (pixfmt) {
  821. case V4L2_PIX_FMT_YUV422P:
  822. pcdev->channels = 3;
  823. cicr1 |= CICR1_YCBCR_F;
  824. /*
  825. * Normally, pxa bus wants as input UYVY format. We allow all
  826. * reorderings of the YUV422 format, as no processing is done,
  827. * and the YUV stream is just passed through without any
  828. * transformation. Note that UYVY is the only format that
  829. * should be used if pxa framebuffer Overlay2 is used.
  830. */
  831. case V4L2_PIX_FMT_UYVY:
  832. case V4L2_PIX_FMT_VYUY:
  833. case V4L2_PIX_FMT_YUYV:
  834. case V4L2_PIX_FMT_YVYU:
  835. cicr1 |= CICR1_COLOR_SP_VAL(2);
  836. break;
  837. case V4L2_PIX_FMT_RGB555:
  838. cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
  839. CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
  840. break;
  841. case V4L2_PIX_FMT_RGB565:
  842. cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
  843. break;
  844. }
  845. cicr2 = 0;
  846. cicr3 = CICR3_LPF_VAL(icd->height - 1) |
  847. CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
  848. cicr4 |= pcdev->mclk_divisor;
  849. __raw_writel(cicr1, pcdev->base + CICR1);
  850. __raw_writel(cicr2, pcdev->base + CICR2);
  851. __raw_writel(cicr3, pcdev->base + CICR3);
  852. __raw_writel(cicr4, pcdev->base + CICR4);
  853. /* CIF interrupts are not used, only DMA */
  854. cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  855. CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
  856. cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
  857. __raw_writel(cicr0, pcdev->base + CICR0);
  858. return 0;
  859. }
  860. static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
  861. unsigned char buswidth)
  862. {
  863. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  864. struct pxa_camera_dev *pcdev = ici->priv;
  865. unsigned long bus_flags, camera_flags;
  866. int ret = test_platform_param(pcdev, buswidth, &bus_flags);
  867. if (ret < 0)
  868. return ret;
  869. camera_flags = icd->ops->query_bus_param(icd);
  870. return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
  871. }
  872. static const struct soc_camera_data_format pxa_camera_formats[] = {
  873. {
  874. .name = "Planar YUV422 16 bit",
  875. .depth = 16,
  876. .fourcc = V4L2_PIX_FMT_YUV422P,
  877. .colorspace = V4L2_COLORSPACE_JPEG,
  878. },
  879. };
  880. static bool buswidth_supported(struct soc_camera_device *icd, int depth)
  881. {
  882. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  883. struct pxa_camera_dev *pcdev = ici->priv;
  884. switch (depth) {
  885. case 8:
  886. return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8);
  887. case 9:
  888. return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9);
  889. case 10:
  890. return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10);
  891. }
  892. return false;
  893. }
  894. static int required_buswidth(const struct soc_camera_data_format *fmt)
  895. {
  896. switch (fmt->fourcc) {
  897. case V4L2_PIX_FMT_UYVY:
  898. case V4L2_PIX_FMT_VYUY:
  899. case V4L2_PIX_FMT_YUYV:
  900. case V4L2_PIX_FMT_YVYU:
  901. case V4L2_PIX_FMT_RGB565:
  902. case V4L2_PIX_FMT_RGB555:
  903. return 8;
  904. default:
  905. return fmt->depth;
  906. }
  907. }
  908. static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
  909. struct soc_camera_format_xlate *xlate)
  910. {
  911. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  912. int formats = 0, buswidth, ret;
  913. buswidth = required_buswidth(icd->formats + idx);
  914. if (!buswidth_supported(icd, buswidth))
  915. return 0;
  916. ret = pxa_camera_try_bus_param(icd, buswidth);
  917. if (ret < 0)
  918. return 0;
  919. switch (icd->formats[idx].fourcc) {
  920. case V4L2_PIX_FMT_UYVY:
  921. formats++;
  922. if (xlate) {
  923. xlate->host_fmt = &pxa_camera_formats[0];
  924. xlate->cam_fmt = icd->formats + idx;
  925. xlate->buswidth = buswidth;
  926. xlate++;
  927. dev_dbg(&ici->dev, "Providing format %s using %s\n",
  928. pxa_camera_formats[0].name,
  929. icd->formats[idx].name);
  930. }
  931. case V4L2_PIX_FMT_VYUY:
  932. case V4L2_PIX_FMT_YUYV:
  933. case V4L2_PIX_FMT_YVYU:
  934. case V4L2_PIX_FMT_RGB565:
  935. case V4L2_PIX_FMT_RGB555:
  936. formats++;
  937. if (xlate) {
  938. xlate->host_fmt = icd->formats + idx;
  939. xlate->cam_fmt = icd->formats + idx;
  940. xlate->buswidth = buswidth;
  941. xlate++;
  942. dev_dbg(&ici->dev, "Providing format %s packed\n",
  943. icd->formats[idx].name);
  944. }
  945. break;
  946. default:
  947. /* Generic pass-through */
  948. formats++;
  949. if (xlate) {
  950. xlate->host_fmt = icd->formats + idx;
  951. xlate->cam_fmt = icd->formats + idx;
  952. xlate->buswidth = icd->formats[idx].depth;
  953. xlate++;
  954. dev_dbg(&ici->dev,
  955. "Providing format %s in pass-through mode\n",
  956. icd->formats[idx].name);
  957. }
  958. }
  959. return formats;
  960. }
  961. static int pxa_camera_set_fmt(struct soc_camera_device *icd,
  962. __u32 pixfmt, struct v4l2_rect *rect)
  963. {
  964. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  965. struct pxa_camera_dev *pcdev = ici->priv;
  966. const struct soc_camera_data_format *host_fmt, *cam_fmt = NULL;
  967. const struct soc_camera_format_xlate *xlate;
  968. struct soc_camera_sense sense = {
  969. .master_clock = pcdev->mclk,
  970. .pixel_clock_max = pcdev->ciclk / 4,
  971. };
  972. int ret, buswidth;
  973. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  974. if (!xlate) {
  975. dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
  976. return -EINVAL;
  977. }
  978. buswidth = xlate->buswidth;
  979. host_fmt = xlate->host_fmt;
  980. cam_fmt = xlate->cam_fmt;
  981. /* If PCLK is used to latch data from the sensor, check sense */
  982. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  983. icd->sense = &sense;
  984. switch (pixfmt) {
  985. case 0: /* Only geometry change */
  986. ret = icd->ops->set_fmt(icd, pixfmt, rect);
  987. break;
  988. default:
  989. ret = icd->ops->set_fmt(icd, cam_fmt->fourcc, rect);
  990. }
  991. icd->sense = NULL;
  992. if (ret < 0) {
  993. dev_warn(&ici->dev, "Failed to configure for format %x\n",
  994. pixfmt);
  995. } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
  996. if (sense.pixel_clock > sense.pixel_clock_max) {
  997. dev_err(&ici->dev,
  998. "pixel clock %lu set by the camera too high!",
  999. sense.pixel_clock);
  1000. return -EIO;
  1001. }
  1002. recalculate_fifo_timeout(pcdev, sense.pixel_clock);
  1003. }
  1004. if (pixfmt && !ret) {
  1005. icd->buswidth = buswidth;
  1006. icd->current_fmt = host_fmt;
  1007. }
  1008. return ret;
  1009. }
  1010. static int pxa_camera_try_fmt(struct soc_camera_device *icd,
  1011. struct v4l2_format *f)
  1012. {
  1013. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1014. const struct soc_camera_format_xlate *xlate;
  1015. struct v4l2_pix_format *pix = &f->fmt.pix;
  1016. __u32 pixfmt = pix->pixelformat;
  1017. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1018. if (!xlate) {
  1019. dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
  1020. return -EINVAL;
  1021. }
  1022. /* limit to pxa hardware capabilities */
  1023. if (pix->height < 32)
  1024. pix->height = 32;
  1025. if (pix->height > 2048)
  1026. pix->height = 2048;
  1027. if (pix->width < 48)
  1028. pix->width = 48;
  1029. if (pix->width > 2048)
  1030. pix->width = 2048;
  1031. pix->width &= ~0x01;
  1032. pix->bytesperline = pix->width *
  1033. DIV_ROUND_UP(xlate->host_fmt->depth, 8);
  1034. pix->sizeimage = pix->height * pix->bytesperline;
  1035. /* limit to sensor capabilities */
  1036. return icd->ops->try_fmt(icd, f);
  1037. }
  1038. static int pxa_camera_reqbufs(struct soc_camera_file *icf,
  1039. struct v4l2_requestbuffers *p)
  1040. {
  1041. int i;
  1042. /* This is for locking debugging only. I removed spinlocks and now I
  1043. * check whether .prepare is ever called on a linked buffer, or whether
  1044. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  1045. * it hadn't triggered */
  1046. for (i = 0; i < p->count; i++) {
  1047. struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
  1048. struct pxa_buffer, vb);
  1049. buf->inwork = 0;
  1050. INIT_LIST_HEAD(&buf->vb.queue);
  1051. }
  1052. return 0;
  1053. }
  1054. static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
  1055. {
  1056. struct soc_camera_file *icf = file->private_data;
  1057. struct pxa_buffer *buf;
  1058. buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
  1059. vb.stream);
  1060. poll_wait(file, &buf->vb.done, pt);
  1061. if (buf->vb.state == VIDEOBUF_DONE ||
  1062. buf->vb.state == VIDEOBUF_ERROR)
  1063. return POLLIN|POLLRDNORM;
  1064. return 0;
  1065. }
  1066. static int pxa_camera_querycap(struct soc_camera_host *ici,
  1067. struct v4l2_capability *cap)
  1068. {
  1069. /* cap->name is set by the firendly caller:-> */
  1070. strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
  1071. cap->version = PXA_CAM_VERSION_CODE;
  1072. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1073. return 0;
  1074. }
  1075. static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
  1076. {
  1077. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1078. struct pxa_camera_dev *pcdev = ici->priv;
  1079. int i = 0, ret = 0;
  1080. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
  1081. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
  1082. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
  1083. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
  1084. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
  1085. if ((pcdev->icd) && (pcdev->icd->ops->suspend))
  1086. ret = pcdev->icd->ops->suspend(pcdev->icd, state);
  1087. return ret;
  1088. }
  1089. static int pxa_camera_resume(struct soc_camera_device *icd)
  1090. {
  1091. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1092. struct pxa_camera_dev *pcdev = ici->priv;
  1093. int i = 0, ret = 0;
  1094. DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  1095. DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  1096. DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  1097. __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
  1098. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
  1099. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
  1100. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
  1101. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
  1102. if ((pcdev->icd) && (pcdev->icd->ops->resume))
  1103. ret = pcdev->icd->ops->resume(pcdev->icd);
  1104. /* Restart frame capture if active buffer exists */
  1105. if (!ret && pcdev->active) {
  1106. unsigned long cifr, cicr0;
  1107. /* Reset the FIFOs */
  1108. cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
  1109. __raw_writel(cifr, pcdev->base + CIFR);
  1110. cicr0 = __raw_readl(pcdev->base + CICR0);
  1111. cicr0 &= ~CICR0_EOFM; /* Enable End-Of-Frame Interrupt */
  1112. cicr0 |= CICR0_ENB; /* Restart the Capture Interface */
  1113. __raw_writel(cicr0, pcdev->base + CICR0);
  1114. }
  1115. return ret;
  1116. }
  1117. static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
  1118. .owner = THIS_MODULE,
  1119. .add = pxa_camera_add_device,
  1120. .remove = pxa_camera_remove_device,
  1121. .suspend = pxa_camera_suspend,
  1122. .resume = pxa_camera_resume,
  1123. .get_formats = pxa_camera_get_formats,
  1124. .set_fmt = pxa_camera_set_fmt,
  1125. .try_fmt = pxa_camera_try_fmt,
  1126. .init_videobuf = pxa_camera_init_videobuf,
  1127. .reqbufs = pxa_camera_reqbufs,
  1128. .poll = pxa_camera_poll,
  1129. .querycap = pxa_camera_querycap,
  1130. .set_bus_param = pxa_camera_set_bus_param,
  1131. };
  1132. /* Should be allocated dynamically too, but we have only one. */
  1133. static struct soc_camera_host pxa_soc_camera_host = {
  1134. .drv_name = PXA_CAM_DRV_NAME,
  1135. .ops = &pxa_soc_camera_host_ops,
  1136. };
  1137. static int pxa_camera_probe(struct platform_device *pdev)
  1138. {
  1139. struct pxa_camera_dev *pcdev;
  1140. struct resource *res;
  1141. void __iomem *base;
  1142. int irq;
  1143. int err = 0;
  1144. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1145. irq = platform_get_irq(pdev, 0);
  1146. if (!res || irq < 0) {
  1147. err = -ENODEV;
  1148. goto exit;
  1149. }
  1150. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1151. if (!pcdev) {
  1152. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1153. err = -ENOMEM;
  1154. goto exit;
  1155. }
  1156. pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
  1157. if (IS_ERR(pcdev->clk)) {
  1158. err = PTR_ERR(pcdev->clk);
  1159. goto exit_kfree;
  1160. }
  1161. dev_set_drvdata(&pdev->dev, pcdev);
  1162. pcdev->res = res;
  1163. pcdev->pdata = pdev->dev.platform_data;
  1164. pcdev->platform_flags = pcdev->pdata->flags;
  1165. if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
  1166. PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
  1167. /* Platform hasn't set available data widths. This is bad.
  1168. * Warn and use a default. */
  1169. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
  1170. "data widths, using default 10 bit\n");
  1171. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  1172. }
  1173. pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
  1174. if (!pcdev->mclk) {
  1175. dev_warn(&pdev->dev,
  1176. "mclk == 0! Please, fix your platform data. "
  1177. "Using default 20MHz\n");
  1178. pcdev->mclk = 20000000;
  1179. }
  1180. pcdev->dev = &pdev->dev;
  1181. pcdev->mclk_divisor = mclk_get_divisor(pcdev);
  1182. INIT_LIST_HEAD(&pcdev->capture);
  1183. spin_lock_init(&pcdev->lock);
  1184. /*
  1185. * Request the regions.
  1186. */
  1187. if (!request_mem_region(res->start, res->end - res->start + 1,
  1188. PXA_CAM_DRV_NAME)) {
  1189. err = -EBUSY;
  1190. goto exit_clk;
  1191. }
  1192. base = ioremap(res->start, res->end - res->start + 1);
  1193. if (!base) {
  1194. err = -ENOMEM;
  1195. goto exit_release;
  1196. }
  1197. pcdev->irq = irq;
  1198. pcdev->base = base;
  1199. /* request dma */
  1200. err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
  1201. pxa_camera_dma_irq_y, pcdev);
  1202. if (err < 0) {
  1203. dev_err(pcdev->dev, "Can't request DMA for Y\n");
  1204. goto exit_iounmap;
  1205. }
  1206. pcdev->dma_chans[0] = err;
  1207. dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
  1208. err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
  1209. pxa_camera_dma_irq_u, pcdev);
  1210. if (err < 0) {
  1211. dev_err(pcdev->dev, "Can't request DMA for U\n");
  1212. goto exit_free_dma_y;
  1213. }
  1214. pcdev->dma_chans[1] = err;
  1215. dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
  1216. err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
  1217. pxa_camera_dma_irq_v, pcdev);
  1218. if (err < 0) {
  1219. dev_err(pcdev->dev, "Can't request DMA for V\n");
  1220. goto exit_free_dma_u;
  1221. }
  1222. pcdev->dma_chans[2] = err;
  1223. dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
  1224. DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  1225. DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  1226. DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  1227. /* request irq */
  1228. err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
  1229. pcdev);
  1230. if (err) {
  1231. dev_err(pcdev->dev, "Camera interrupt register failed \n");
  1232. goto exit_free_dma;
  1233. }
  1234. pxa_soc_camera_host.priv = pcdev;
  1235. pxa_soc_camera_host.dev.parent = &pdev->dev;
  1236. pxa_soc_camera_host.nr = pdev->id;
  1237. err = soc_camera_host_register(&pxa_soc_camera_host);
  1238. if (err)
  1239. goto exit_free_irq;
  1240. return 0;
  1241. exit_free_irq:
  1242. free_irq(pcdev->irq, pcdev);
  1243. exit_free_dma:
  1244. pxa_free_dma(pcdev->dma_chans[2]);
  1245. exit_free_dma_u:
  1246. pxa_free_dma(pcdev->dma_chans[1]);
  1247. exit_free_dma_y:
  1248. pxa_free_dma(pcdev->dma_chans[0]);
  1249. exit_iounmap:
  1250. iounmap(base);
  1251. exit_release:
  1252. release_mem_region(res->start, res->end - res->start + 1);
  1253. exit_clk:
  1254. clk_put(pcdev->clk);
  1255. exit_kfree:
  1256. kfree(pcdev);
  1257. exit:
  1258. return err;
  1259. }
  1260. static int __devexit pxa_camera_remove(struct platform_device *pdev)
  1261. {
  1262. struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
  1263. struct resource *res;
  1264. clk_put(pcdev->clk);
  1265. pxa_free_dma(pcdev->dma_chans[0]);
  1266. pxa_free_dma(pcdev->dma_chans[1]);
  1267. pxa_free_dma(pcdev->dma_chans[2]);
  1268. free_irq(pcdev->irq, pcdev);
  1269. soc_camera_host_unregister(&pxa_soc_camera_host);
  1270. iounmap(pcdev->base);
  1271. res = pcdev->res;
  1272. release_mem_region(res->start, res->end - res->start + 1);
  1273. kfree(pcdev);
  1274. dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
  1275. return 0;
  1276. }
  1277. static struct platform_driver pxa_camera_driver = {
  1278. .driver = {
  1279. .name = PXA_CAM_DRV_NAME,
  1280. },
  1281. .probe = pxa_camera_probe,
  1282. .remove = __exit_p(pxa_camera_remove),
  1283. };
  1284. static int __devinit pxa_camera_init(void)
  1285. {
  1286. return platform_driver_register(&pxa_camera_driver);
  1287. }
  1288. static void __exit pxa_camera_exit(void)
  1289. {
  1290. platform_driver_unregister(&pxa_camera_driver);
  1291. }
  1292. module_init(pxa_camera_init);
  1293. module_exit(pxa_camera_exit);
  1294. MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
  1295. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1296. MODULE_LICENSE("GPL");