pxa_camera.c 43 KB

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