pxa_camera.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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 <linux/sched.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 <media/soc_mediabus.h>
  34. #include <linux/videodev2.h>
  35. #include <mach/dma.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. /*
  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. enum v4l2_mbus_pixelcode code;
  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 soc_camera_host soc_host;
  175. /*
  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. */
  180. struct soc_camera_device *icd;
  181. struct clk *clk;
  182. unsigned int irq;
  183. void __iomem *base;
  184. int channels;
  185. unsigned int dma_chans[3];
  186. struct pxacamera_platform_data *pdata;
  187. struct resource *res;
  188. unsigned long platform_flags;
  189. unsigned long ciclk;
  190. unsigned long mclk;
  191. u32 mclk_divisor;
  192. struct list_head capture;
  193. spinlock_t lock;
  194. struct pxa_buffer *active;
  195. struct pxa_dma_desc *sg_tail[3];
  196. u32 save_cicr[5];
  197. };
  198. struct pxa_cam {
  199. unsigned long flags;
  200. };
  201. static const char *pxa_cam_driver_description = "PXA_Camera";
  202. static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
  203. /*
  204. * Videobuf operations
  205. */
  206. static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
  207. unsigned int *size)
  208. {
  209. struct soc_camera_device *icd = vq->priv_data;
  210. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  211. icd->current_fmt->host_fmt);
  212. if (bytes_per_line < 0)
  213. return bytes_per_line;
  214. dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
  215. *size = bytes_per_line * icd->user_height;
  216. if (0 == *count)
  217. *count = 32;
  218. while (*size * *count > vid_limit * 1024 * 1024)
  219. (*count)--;
  220. return 0;
  221. }
  222. static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
  223. {
  224. struct soc_camera_device *icd = vq->priv_data;
  225. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  226. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  227. int i;
  228. BUG_ON(in_interrupt());
  229. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  230. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  231. /*
  232. * This waits until this buffer is out of danger, i.e., until it is no
  233. * longer in STATE_QUEUED or STATE_ACTIVE
  234. */
  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(ici->v4l2_dev.dev,
  241. 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 calculate_dma_sglen(struct scatterlist *sglist, int sglen,
  249. int sg_first_ofs, int size)
  250. {
  251. int i, offset, dma_len, xfer_len;
  252. struct scatterlist *sg;
  253. offset = sg_first_ofs;
  254. for_each_sg(sglist, sg, sglen, i) {
  255. dma_len = sg_dma_len(sg);
  256. /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
  257. xfer_len = roundup(min(dma_len - offset, size), 8);
  258. size = max(0, size - xfer_len);
  259. offset = 0;
  260. if (size == 0)
  261. break;
  262. }
  263. BUG_ON(size != 0);
  264. return i + 1;
  265. }
  266. /**
  267. * pxa_init_dma_channel - init dma descriptors
  268. * @pcdev: pxa camera device
  269. * @buf: pxa buffer to find pxa dma channel
  270. * @dma: dma video buffer
  271. * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V')
  272. * @cibr: camera Receive Buffer Register
  273. * @size: bytes to transfer
  274. * @sg_first: first element of sg_list
  275. * @sg_first_ofs: offset in first element of sg_list
  276. *
  277. * Prepares the pxa dma descriptors to transfer one camera channel.
  278. * Beware sg_first and sg_first_ofs are both input and output parameters.
  279. *
  280. * Returns 0 or -ENOMEM if no coherent memory is available
  281. */
  282. static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
  283. struct pxa_buffer *buf,
  284. struct videobuf_dmabuf *dma, int channel,
  285. int cibr, int size,
  286. struct scatterlist **sg_first, int *sg_first_ofs)
  287. {
  288. struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
  289. struct device *dev = pcdev->soc_host.v4l2_dev.dev;
  290. struct scatterlist *sg;
  291. int i, offset, sglen;
  292. int dma_len = 0, xfer_len = 0;
  293. if (pxa_dma->sg_cpu)
  294. dma_free_coherent(dev, pxa_dma->sg_size,
  295. pxa_dma->sg_cpu, pxa_dma->sg_dma);
  296. sglen = calculate_dma_sglen(*sg_first, dma->sglen,
  297. *sg_first_ofs, size);
  298. pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
  299. pxa_dma->sg_cpu = dma_alloc_coherent(dev, pxa_dma->sg_size,
  300. &pxa_dma->sg_dma, GFP_KERNEL);
  301. if (!pxa_dma->sg_cpu)
  302. return -ENOMEM;
  303. pxa_dma->sglen = sglen;
  304. offset = *sg_first_ofs;
  305. dev_dbg(dev, "DMA: sg_first=%p, sglen=%d, ofs=%d, dma.desc=%x\n",
  306. *sg_first, sglen, *sg_first_ofs, pxa_dma->sg_dma);
  307. for_each_sg(*sg_first, sg, sglen, i) {
  308. dma_len = sg_dma_len(sg);
  309. /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
  310. xfer_len = roundup(min(dma_len - offset, size), 8);
  311. size = max(0, size - xfer_len);
  312. pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
  313. pxa_dma->sg_cpu[i].dtadr = sg_dma_address(sg) + offset;
  314. pxa_dma->sg_cpu[i].dcmd =
  315. DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
  316. #ifdef DEBUG
  317. if (!i)
  318. pxa_dma->sg_cpu[i].dcmd |= DCMD_STARTIRQEN;
  319. #endif
  320. pxa_dma->sg_cpu[i].ddadr =
  321. pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
  322. dev_vdbg(dev, "DMA: desc.%08x->@phys=0x%08x, len=%d\n",
  323. pxa_dma->sg_dma + i * sizeof(struct pxa_dma_desc),
  324. sg_dma_address(sg) + offset, xfer_len);
  325. offset = 0;
  326. if (size == 0)
  327. break;
  328. }
  329. pxa_dma->sg_cpu[sglen].ddadr = DDADR_STOP;
  330. pxa_dma->sg_cpu[sglen].dcmd = DCMD_FLOWSRC | DCMD_BURST8 | DCMD_ENDIRQEN;
  331. /*
  332. * Handle 1 special case :
  333. * - in 3 planes (YUV422P format), we might finish with xfer_len equal
  334. * to dma_len (end on PAGE boundary). In this case, the sg element
  335. * for next plane should be the next after the last used to store the
  336. * last scatter gather RAM page
  337. */
  338. if (xfer_len >= dma_len) {
  339. *sg_first_ofs = xfer_len - dma_len;
  340. *sg_first = sg_next(sg);
  341. } else {
  342. *sg_first_ofs = xfer_len;
  343. *sg_first = sg;
  344. }
  345. return 0;
  346. }
  347. static void pxa_videobuf_set_actdma(struct pxa_camera_dev *pcdev,
  348. struct pxa_buffer *buf)
  349. {
  350. buf->active_dma = DMA_Y;
  351. if (pcdev->channels == 3)
  352. buf->active_dma |= DMA_U | DMA_V;
  353. }
  354. /*
  355. * Please check the DMA prepared buffer structure in :
  356. * Documentation/video4linux/pxa_camera.txt
  357. * Please check also in pxa_camera_check_link_miss() to understand why DMA chain
  358. * modification while DMA chain is running will work anyway.
  359. */
  360. static int pxa_videobuf_prepare(struct videobuf_queue *vq,
  361. struct videobuf_buffer *vb, enum v4l2_field field)
  362. {
  363. struct soc_camera_device *icd = vq->priv_data;
  364. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  365. struct pxa_camera_dev *pcdev = ici->priv;
  366. struct device *dev = pcdev->soc_host.v4l2_dev.dev;
  367. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  368. int ret;
  369. int size_y, size_u = 0, size_v = 0;
  370. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  371. icd->current_fmt->host_fmt);
  372. if (bytes_per_line < 0)
  373. return bytes_per_line;
  374. dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  375. vb, vb->baddr, vb->bsize);
  376. /* Added list head initialization on alloc */
  377. WARN_ON(!list_empty(&vb->queue));
  378. #ifdef DEBUG
  379. /*
  380. * This can be useful if you want to see if we actually fill
  381. * the buffer with something
  382. */
  383. memset((void *)vb->baddr, 0xaa, vb->bsize);
  384. #endif
  385. BUG_ON(NULL == icd->current_fmt);
  386. /*
  387. * I think, in buf_prepare you only have to protect global data,
  388. * the actual buffer is yours
  389. */
  390. buf->inwork = 1;
  391. if (buf->code != icd->current_fmt->code ||
  392. vb->width != icd->user_width ||
  393. vb->height != icd->user_height ||
  394. vb->field != field) {
  395. buf->code = icd->current_fmt->code;
  396. vb->width = icd->user_width;
  397. vb->height = icd->user_height;
  398. vb->field = field;
  399. vb->state = VIDEOBUF_NEEDS_INIT;
  400. }
  401. vb->size = bytes_per_line * vb->height;
  402. if (0 != vb->baddr && vb->bsize < vb->size) {
  403. ret = -EINVAL;
  404. goto out;
  405. }
  406. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  407. int size = vb->size;
  408. int next_ofs = 0;
  409. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  410. struct scatterlist *sg;
  411. ret = videobuf_iolock(vq, vb, NULL);
  412. if (ret)
  413. goto fail;
  414. if (pcdev->channels == 3) {
  415. size_y = size / 2;
  416. size_u = size_v = size / 4;
  417. } else {
  418. size_y = size;
  419. }
  420. sg = dma->sglist;
  421. /* init DMA for Y channel */
  422. ret = pxa_init_dma_channel(pcdev, buf, dma, 0, CIBR0, size_y,
  423. &sg, &next_ofs);
  424. if (ret) {
  425. dev_err(dev, "DMA initialization for Y/RGB failed\n");
  426. goto fail;
  427. }
  428. /* init DMA for U channel */
  429. if (size_u)
  430. ret = pxa_init_dma_channel(pcdev, buf, dma, 1, CIBR1,
  431. size_u, &sg, &next_ofs);
  432. if (ret) {
  433. dev_err(dev, "DMA initialization for U failed\n");
  434. goto fail_u;
  435. }
  436. /* init DMA for V channel */
  437. if (size_v)
  438. ret = pxa_init_dma_channel(pcdev, buf, dma, 2, CIBR2,
  439. size_v, &sg, &next_ofs);
  440. if (ret) {
  441. dev_err(dev, "DMA initialization for V failed\n");
  442. goto fail_v;
  443. }
  444. vb->state = VIDEOBUF_PREPARED;
  445. }
  446. buf->inwork = 0;
  447. pxa_videobuf_set_actdma(pcdev, buf);
  448. return 0;
  449. fail_v:
  450. dma_free_coherent(dev, buf->dmas[1].sg_size,
  451. buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
  452. fail_u:
  453. dma_free_coherent(dev, buf->dmas[0].sg_size,
  454. buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
  455. fail:
  456. free_buffer(vq, buf);
  457. out:
  458. buf->inwork = 0;
  459. return ret;
  460. }
  461. /**
  462. * pxa_dma_start_channels - start DMA channel for active buffer
  463. * @pcdev: pxa camera device
  464. *
  465. * Initialize DMA channels to the beginning of the active video buffer, and
  466. * start these channels.
  467. */
  468. static void pxa_dma_start_channels(struct pxa_camera_dev *pcdev)
  469. {
  470. int i;
  471. struct pxa_buffer *active;
  472. active = pcdev->active;
  473. for (i = 0; i < pcdev->channels; i++) {
  474. dev_dbg(pcdev->soc_host.v4l2_dev.dev,
  475. "%s (channel=%d) ddadr=%08x\n", __func__,
  476. i, active->dmas[i].sg_dma);
  477. DDADR(pcdev->dma_chans[i]) = active->dmas[i].sg_dma;
  478. DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
  479. }
  480. }
  481. static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev)
  482. {
  483. int i;
  484. for (i = 0; i < pcdev->channels; i++) {
  485. dev_dbg(pcdev->soc_host.v4l2_dev.dev,
  486. "%s (channel=%d)\n", __func__, i);
  487. DCSR(pcdev->dma_chans[i]) = 0;
  488. }
  489. }
  490. static void pxa_dma_add_tail_buf(struct pxa_camera_dev *pcdev,
  491. struct pxa_buffer *buf)
  492. {
  493. int i;
  494. struct pxa_dma_desc *buf_last_desc;
  495. for (i = 0; i < pcdev->channels; i++) {
  496. buf_last_desc = buf->dmas[i].sg_cpu + buf->dmas[i].sglen;
  497. buf_last_desc->ddadr = DDADR_STOP;
  498. if (pcdev->sg_tail[i])
  499. /* Link the new buffer to the old tail */
  500. pcdev->sg_tail[i]->ddadr = buf->dmas[i].sg_dma;
  501. /* Update the channel tail */
  502. pcdev->sg_tail[i] = buf_last_desc;
  503. }
  504. }
  505. /**
  506. * pxa_camera_start_capture - start video capturing
  507. * @pcdev: camera device
  508. *
  509. * Launch capturing. DMA channels should not be active yet. They should get
  510. * activated at the end of frame interrupt, to capture only whole frames, and
  511. * never begin the capture of a partial frame.
  512. */
  513. static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev)
  514. {
  515. unsigned long cicr0, cifr;
  516. dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s\n", __func__);
  517. /* Reset the FIFOs */
  518. cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
  519. __raw_writel(cifr, pcdev->base + CIFR);
  520. /* Enable End-Of-Frame Interrupt */
  521. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
  522. cicr0 &= ~CICR0_EOFM;
  523. __raw_writel(cicr0, pcdev->base + CICR0);
  524. }
  525. static void pxa_camera_stop_capture(struct pxa_camera_dev *pcdev)
  526. {
  527. unsigned long cicr0;
  528. pxa_dma_stop_channels(pcdev);
  529. cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
  530. __raw_writel(cicr0, pcdev->base + CICR0);
  531. pcdev->active = NULL;
  532. dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s\n", __func__);
  533. }
  534. /* Called under spinlock_irqsave(&pcdev->lock, ...) */
  535. static void pxa_videobuf_queue(struct videobuf_queue *vq,
  536. struct videobuf_buffer *vb)
  537. {
  538. struct soc_camera_device *icd = vq->priv_data;
  539. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  540. struct pxa_camera_dev *pcdev = ici->priv;
  541. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  542. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d active=%p\n",
  543. __func__, vb, vb->baddr, vb->bsize, pcdev->active);
  544. list_add_tail(&vb->queue, &pcdev->capture);
  545. vb->state = VIDEOBUF_ACTIVE;
  546. pxa_dma_add_tail_buf(pcdev, buf);
  547. if (!pcdev->active)
  548. pxa_camera_start_capture(pcdev);
  549. }
  550. static void pxa_videobuf_release(struct videobuf_queue *vq,
  551. struct videobuf_buffer *vb)
  552. {
  553. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  554. #ifdef DEBUG
  555. struct soc_camera_device *icd = vq->priv_data;
  556. struct device *dev = icd->dev.parent;
  557. dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  558. vb, vb->baddr, vb->bsize);
  559. switch (vb->state) {
  560. case VIDEOBUF_ACTIVE:
  561. dev_dbg(dev, "%s (active)\n", __func__);
  562. break;
  563. case VIDEOBUF_QUEUED:
  564. dev_dbg(dev, "%s (queued)\n", __func__);
  565. break;
  566. case VIDEOBUF_PREPARED:
  567. dev_dbg(dev, "%s (prepared)\n", __func__);
  568. break;
  569. default:
  570. dev_dbg(dev, "%s (unknown)\n", __func__);
  571. break;
  572. }
  573. #endif
  574. free_buffer(vq, buf);
  575. }
  576. static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
  577. struct videobuf_buffer *vb,
  578. struct pxa_buffer *buf)
  579. {
  580. int i;
  581. /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
  582. list_del_init(&vb->queue);
  583. vb->state = VIDEOBUF_DONE;
  584. do_gettimeofday(&vb->ts);
  585. vb->field_count++;
  586. wake_up(&vb->done);
  587. dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s dequeud buffer (vb=0x%p)\n",
  588. __func__, vb);
  589. if (list_empty(&pcdev->capture)) {
  590. pxa_camera_stop_capture(pcdev);
  591. for (i = 0; i < pcdev->channels; i++)
  592. pcdev->sg_tail[i] = NULL;
  593. return;
  594. }
  595. pcdev->active = list_entry(pcdev->capture.next,
  596. struct pxa_buffer, vb.queue);
  597. }
  598. /**
  599. * pxa_camera_check_link_miss - check missed DMA linking
  600. * @pcdev: camera device
  601. *
  602. * The DMA chaining is done with DMA running. This means a tiny temporal window
  603. * remains, where a buffer is queued on the chain, while the chain is already
  604. * stopped. This means the tailed buffer would never be transfered by DMA.
  605. * This function restarts the capture for this corner case, where :
  606. * - DADR() == DADDR_STOP
  607. * - a videobuffer is queued on the pcdev->capture list
  608. *
  609. * Please check the "DMA hot chaining timeslice issue" in
  610. * Documentation/video4linux/pxa_camera.txt
  611. *
  612. * Context: should only be called within the dma irq handler
  613. */
  614. static void pxa_camera_check_link_miss(struct pxa_camera_dev *pcdev)
  615. {
  616. int i, is_dma_stopped = 1;
  617. for (i = 0; i < pcdev->channels; i++)
  618. if (DDADR(pcdev->dma_chans[i]) != DDADR_STOP)
  619. is_dma_stopped = 0;
  620. dev_dbg(pcdev->soc_host.v4l2_dev.dev,
  621. "%s : top queued buffer=%p, dma_stopped=%d\n",
  622. __func__, pcdev->active, is_dma_stopped);
  623. if (pcdev->active && is_dma_stopped)
  624. pxa_camera_start_capture(pcdev);
  625. }
  626. static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
  627. enum pxa_camera_active_dma act_dma)
  628. {
  629. struct device *dev = pcdev->soc_host.v4l2_dev.dev;
  630. struct pxa_buffer *buf;
  631. unsigned long flags;
  632. u32 status, camera_status, overrun;
  633. struct videobuf_buffer *vb;
  634. spin_lock_irqsave(&pcdev->lock, flags);
  635. status = DCSR(channel);
  636. DCSR(channel) = status;
  637. camera_status = __raw_readl(pcdev->base + CISR);
  638. overrun = CISR_IFO_0;
  639. if (pcdev->channels == 3)
  640. overrun |= CISR_IFO_1 | CISR_IFO_2;
  641. if (status & DCSR_BUSERR) {
  642. dev_err(dev, "DMA Bus Error IRQ!\n");
  643. goto out;
  644. }
  645. if (!(status & (DCSR_ENDINTR | DCSR_STARTINTR))) {
  646. dev_err(dev, "Unknown DMA IRQ source, status: 0x%08x\n",
  647. status);
  648. goto out;
  649. }
  650. /*
  651. * pcdev->active should not be NULL in DMA irq handler.
  652. *
  653. * But there is one corner case : if capture was stopped due to an
  654. * overrun of channel 1, and at that same channel 2 was completed.
  655. *
  656. * When handling the overrun in DMA irq for channel 1, we'll stop the
  657. * capture and restart it (and thus set pcdev->active to NULL). But the
  658. * DMA irq handler will already be pending for channel 2. So on entering
  659. * the DMA irq handler for channel 2 there will be no active buffer, yet
  660. * that is normal.
  661. */
  662. if (!pcdev->active)
  663. goto out;
  664. vb = &pcdev->active->vb;
  665. buf = container_of(vb, struct pxa_buffer, vb);
  666. WARN_ON(buf->inwork || list_empty(&vb->queue));
  667. dev_dbg(dev, "%s channel=%d %s%s(vb=0x%p) dma.desc=%x\n",
  668. __func__, channel, status & DCSR_STARTINTR ? "SOF " : "",
  669. status & DCSR_ENDINTR ? "EOF " : "", vb, DDADR(channel));
  670. if (status & DCSR_ENDINTR) {
  671. /*
  672. * It's normal if the last frame creates an overrun, as there
  673. * are no more DMA descriptors to fetch from QCI fifos
  674. */
  675. if (camera_status & overrun &&
  676. !list_is_last(pcdev->capture.next, &pcdev->capture)) {
  677. dev_dbg(dev, "FIFO overrun! CISR: %x\n",
  678. camera_status);
  679. pxa_camera_stop_capture(pcdev);
  680. pxa_camera_start_capture(pcdev);
  681. goto out;
  682. }
  683. buf->active_dma &= ~act_dma;
  684. if (!buf->active_dma) {
  685. pxa_camera_wakeup(pcdev, vb, buf);
  686. pxa_camera_check_link_miss(pcdev);
  687. }
  688. }
  689. out:
  690. spin_unlock_irqrestore(&pcdev->lock, flags);
  691. }
  692. static void pxa_camera_dma_irq_y(int channel, void *data)
  693. {
  694. struct pxa_camera_dev *pcdev = data;
  695. pxa_camera_dma_irq(channel, pcdev, DMA_Y);
  696. }
  697. static void pxa_camera_dma_irq_u(int channel, void *data)
  698. {
  699. struct pxa_camera_dev *pcdev = data;
  700. pxa_camera_dma_irq(channel, pcdev, DMA_U);
  701. }
  702. static void pxa_camera_dma_irq_v(int channel, void *data)
  703. {
  704. struct pxa_camera_dev *pcdev = data;
  705. pxa_camera_dma_irq(channel, pcdev, DMA_V);
  706. }
  707. static struct videobuf_queue_ops pxa_videobuf_ops = {
  708. .buf_setup = pxa_videobuf_setup,
  709. .buf_prepare = pxa_videobuf_prepare,
  710. .buf_queue = pxa_videobuf_queue,
  711. .buf_release = pxa_videobuf_release,
  712. };
  713. static void pxa_camera_init_videobuf(struct videobuf_queue *q,
  714. struct soc_camera_device *icd)
  715. {
  716. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  717. struct pxa_camera_dev *pcdev = ici->priv;
  718. /*
  719. * We must pass NULL as dev pointer, then all pci_* dma operations
  720. * transform to normal dma_* ones.
  721. */
  722. videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
  723. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  724. sizeof(struct pxa_buffer), icd);
  725. }
  726. static u32 mclk_get_divisor(struct platform_device *pdev,
  727. struct pxa_camera_dev *pcdev)
  728. {
  729. unsigned long mclk = pcdev->mclk;
  730. struct device *dev = &pdev->dev;
  731. u32 div;
  732. unsigned long lcdclk;
  733. lcdclk = clk_get_rate(pcdev->clk);
  734. pcdev->ciclk = lcdclk;
  735. /* mclk <= ciclk / 4 (27.4.2) */
  736. if (mclk > lcdclk / 4) {
  737. mclk = lcdclk / 4;
  738. dev_warn(dev, "Limiting master clock to %lu\n", mclk);
  739. }
  740. /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
  741. div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
  742. /* If we're not supplying MCLK, leave it at 0 */
  743. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  744. pcdev->mclk = lcdclk / (2 * (div + 1));
  745. dev_dbg(dev, "LCD clock %luHz, target freq %luHz, divisor %u\n",
  746. lcdclk, mclk, div);
  747. return div;
  748. }
  749. static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
  750. unsigned long pclk)
  751. {
  752. /* We want a timeout > 1 pixel time, not ">=" */
  753. u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
  754. __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
  755. }
  756. static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
  757. {
  758. struct pxacamera_platform_data *pdata = pcdev->pdata;
  759. struct device *dev = pcdev->soc_host.v4l2_dev.dev;
  760. u32 cicr4 = 0;
  761. dev_dbg(dev, "Registered platform device at %p data %p\n",
  762. pcdev, pdata);
  763. if (pdata && pdata->init) {
  764. dev_dbg(dev, "%s: Init gpios\n", __func__);
  765. pdata->init(dev);
  766. }
  767. /* disable all interrupts */
  768. __raw_writel(0x3ff, pcdev->base + CICR0);
  769. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  770. cicr4 |= CICR4_PCLK_EN;
  771. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  772. cicr4 |= CICR4_MCLK_EN;
  773. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  774. cicr4 |= CICR4_PCP;
  775. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  776. cicr4 |= CICR4_HSP;
  777. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  778. cicr4 |= CICR4_VSP;
  779. __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
  780. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  781. /* Initialise the timeout under the assumption pclk = mclk */
  782. recalculate_fifo_timeout(pcdev, pcdev->mclk);
  783. else
  784. /* "Safe default" - 13MHz */
  785. recalculate_fifo_timeout(pcdev, 13000000);
  786. clk_enable(pcdev->clk);
  787. }
  788. static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
  789. {
  790. clk_disable(pcdev->clk);
  791. }
  792. static irqreturn_t pxa_camera_irq(int irq, void *data)
  793. {
  794. struct pxa_camera_dev *pcdev = data;
  795. unsigned long status, cicr0;
  796. struct pxa_buffer *buf;
  797. struct videobuf_buffer *vb;
  798. status = __raw_readl(pcdev->base + CISR);
  799. dev_dbg(pcdev->soc_host.v4l2_dev.dev,
  800. "Camera interrupt status 0x%lx\n", status);
  801. if (!status)
  802. return IRQ_NONE;
  803. __raw_writel(status, pcdev->base + CISR);
  804. if (status & CISR_EOF) {
  805. pcdev->active = list_first_entry(&pcdev->capture,
  806. struct pxa_buffer, vb.queue);
  807. vb = &pcdev->active->vb;
  808. buf = container_of(vb, struct pxa_buffer, vb);
  809. pxa_videobuf_set_actdma(pcdev, buf);
  810. pxa_dma_start_channels(pcdev);
  811. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
  812. __raw_writel(cicr0, pcdev->base + CICR0);
  813. }
  814. return IRQ_HANDLED;
  815. }
  816. /*
  817. * The following two functions absolutely depend on the fact, that
  818. * there can be only one camera on PXA quick capture interface
  819. * Called with .video_lock held
  820. */
  821. static int pxa_camera_add_device(struct soc_camera_device *icd)
  822. {
  823. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  824. struct pxa_camera_dev *pcdev = ici->priv;
  825. if (pcdev->icd)
  826. return -EBUSY;
  827. pxa_camera_activate(pcdev);
  828. pcdev->icd = icd;
  829. dev_info(icd->dev.parent, "PXA Camera driver attached to camera %d\n",
  830. icd->devnum);
  831. return 0;
  832. }
  833. /* Called with .video_lock held */
  834. static void pxa_camera_remove_device(struct soc_camera_device *icd)
  835. {
  836. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  837. struct pxa_camera_dev *pcdev = ici->priv;
  838. BUG_ON(icd != pcdev->icd);
  839. dev_info(icd->dev.parent, "PXA Camera driver detached from camera %d\n",
  840. icd->devnum);
  841. /* disable capture, disable interrupts */
  842. __raw_writel(0x3ff, pcdev->base + CICR0);
  843. /* Stop DMA engine */
  844. DCSR(pcdev->dma_chans[0]) = 0;
  845. DCSR(pcdev->dma_chans[1]) = 0;
  846. DCSR(pcdev->dma_chans[2]) = 0;
  847. pxa_camera_deactivate(pcdev);
  848. pcdev->icd = NULL;
  849. }
  850. static int test_platform_param(struct pxa_camera_dev *pcdev,
  851. unsigned char buswidth, unsigned long *flags)
  852. {
  853. /*
  854. * Platform specified synchronization and pixel clock polarities are
  855. * only a recommendation and are only used during probing. The PXA270
  856. * quick capture interface supports both.
  857. */
  858. *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  859. SOCAM_MASTER : SOCAM_SLAVE) |
  860. SOCAM_HSYNC_ACTIVE_HIGH |
  861. SOCAM_HSYNC_ACTIVE_LOW |
  862. SOCAM_VSYNC_ACTIVE_HIGH |
  863. SOCAM_VSYNC_ACTIVE_LOW |
  864. SOCAM_DATA_ACTIVE_HIGH |
  865. SOCAM_PCLK_SAMPLE_RISING |
  866. SOCAM_PCLK_SAMPLE_FALLING;
  867. /* If requested data width is supported by the platform, use it */
  868. switch (buswidth) {
  869. case 10:
  870. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
  871. return -EINVAL;
  872. *flags |= SOCAM_DATAWIDTH_10;
  873. break;
  874. case 9:
  875. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
  876. return -EINVAL;
  877. *flags |= SOCAM_DATAWIDTH_9;
  878. break;
  879. case 8:
  880. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
  881. return -EINVAL;
  882. *flags |= SOCAM_DATAWIDTH_8;
  883. break;
  884. default:
  885. return -EINVAL;
  886. }
  887. return 0;
  888. }
  889. static void pxa_camera_setup_cicr(struct soc_camera_device *icd,
  890. unsigned long flags, __u32 pixfmt)
  891. {
  892. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  893. struct pxa_camera_dev *pcdev = ici->priv;
  894. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  895. unsigned long dw, bpp;
  896. u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0, y_skip_top;
  897. int ret = v4l2_subdev_call(sd, sensor, g_skip_top_lines, &y_skip_top);
  898. if (ret < 0)
  899. y_skip_top = 0;
  900. /*
  901. * Datawidth is now guaranteed to be equal to one of the three values.
  902. * We fix bit-per-pixel equal to data-width...
  903. */
  904. switch (flags & SOCAM_DATAWIDTH_MASK) {
  905. case SOCAM_DATAWIDTH_10:
  906. dw = 4;
  907. bpp = 0x40;
  908. break;
  909. case SOCAM_DATAWIDTH_9:
  910. dw = 3;
  911. bpp = 0x20;
  912. break;
  913. default:
  914. /*
  915. * Actually it can only be 8 now,
  916. * default is just to silence compiler warnings
  917. */
  918. case SOCAM_DATAWIDTH_8:
  919. dw = 2;
  920. bpp = 0;
  921. }
  922. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  923. cicr4 |= CICR4_PCLK_EN;
  924. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  925. cicr4 |= CICR4_MCLK_EN;
  926. if (flags & SOCAM_PCLK_SAMPLE_FALLING)
  927. cicr4 |= CICR4_PCP;
  928. if (flags & SOCAM_HSYNC_ACTIVE_LOW)
  929. cicr4 |= CICR4_HSP;
  930. if (flags & SOCAM_VSYNC_ACTIVE_LOW)
  931. cicr4 |= CICR4_VSP;
  932. cicr0 = __raw_readl(pcdev->base + CICR0);
  933. if (cicr0 & CICR0_ENB)
  934. __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
  935. cicr1 = CICR1_PPL_VAL(icd->user_width - 1) | bpp | dw;
  936. switch (pixfmt) {
  937. case V4L2_PIX_FMT_YUV422P:
  938. pcdev->channels = 3;
  939. cicr1 |= CICR1_YCBCR_F;
  940. /*
  941. * Normally, pxa bus wants as input UYVY format. We allow all
  942. * reorderings of the YUV422 format, as no processing is done,
  943. * and the YUV stream is just passed through without any
  944. * transformation. Note that UYVY is the only format that
  945. * should be used if pxa framebuffer Overlay2 is used.
  946. */
  947. case V4L2_PIX_FMT_UYVY:
  948. case V4L2_PIX_FMT_VYUY:
  949. case V4L2_PIX_FMT_YUYV:
  950. case V4L2_PIX_FMT_YVYU:
  951. cicr1 |= CICR1_COLOR_SP_VAL(2);
  952. break;
  953. case V4L2_PIX_FMT_RGB555:
  954. cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
  955. CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
  956. break;
  957. case V4L2_PIX_FMT_RGB565:
  958. cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
  959. break;
  960. }
  961. cicr2 = 0;
  962. cicr3 = CICR3_LPF_VAL(icd->user_height - 1) |
  963. CICR3_BFW_VAL(min((u32)255, y_skip_top));
  964. cicr4 |= pcdev->mclk_divisor;
  965. __raw_writel(cicr1, pcdev->base + CICR1);
  966. __raw_writel(cicr2, pcdev->base + CICR2);
  967. __raw_writel(cicr3, pcdev->base + CICR3);
  968. __raw_writel(cicr4, pcdev->base + CICR4);
  969. /* CIF interrupts are not used, only DMA */
  970. cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  971. CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
  972. cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
  973. __raw_writel(cicr0, pcdev->base + CICR0);
  974. }
  975. static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
  976. {
  977. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  978. struct pxa_camera_dev *pcdev = ici->priv;
  979. unsigned long bus_flags, camera_flags, common_flags;
  980. const struct soc_mbus_pixelfmt *fmt;
  981. int ret;
  982. struct pxa_cam *cam = icd->host_priv;
  983. fmt = soc_mbus_get_fmtdesc(icd->current_fmt->code);
  984. if (!fmt)
  985. return -EINVAL;
  986. ret = test_platform_param(pcdev, fmt->bits_per_sample, &bus_flags);
  987. if (ret < 0)
  988. return ret;
  989. camera_flags = icd->ops->query_bus_param(icd);
  990. common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
  991. if (!common_flags)
  992. return -EINVAL;
  993. pcdev->channels = 1;
  994. /* Make choises, based on platform preferences */
  995. if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
  996. (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
  997. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  998. common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
  999. else
  1000. common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
  1001. }
  1002. if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
  1003. (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
  1004. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  1005. common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
  1006. else
  1007. common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
  1008. }
  1009. if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
  1010. (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
  1011. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  1012. common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
  1013. else
  1014. common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
  1015. }
  1016. cam->flags = common_flags;
  1017. ret = icd->ops->set_bus_param(icd, common_flags);
  1018. if (ret < 0)
  1019. return ret;
  1020. pxa_camera_setup_cicr(icd, common_flags, pixfmt);
  1021. return 0;
  1022. }
  1023. static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
  1024. unsigned char buswidth)
  1025. {
  1026. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1027. struct pxa_camera_dev *pcdev = ici->priv;
  1028. unsigned long bus_flags, camera_flags;
  1029. int ret = test_platform_param(pcdev, buswidth, &bus_flags);
  1030. if (ret < 0)
  1031. return ret;
  1032. camera_flags = icd->ops->query_bus_param(icd);
  1033. return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
  1034. }
  1035. static const struct soc_mbus_pixelfmt pxa_camera_formats[] = {
  1036. {
  1037. .fourcc = V4L2_PIX_FMT_YUV422P,
  1038. .name = "Planar YUV422 16 bit",
  1039. .bits_per_sample = 8,
  1040. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  1041. .order = SOC_MBUS_ORDER_LE,
  1042. },
  1043. };
  1044. /* This will be corrected as we get more formats */
  1045. static bool pxa_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
  1046. {
  1047. return fmt->packing == SOC_MBUS_PACKING_NONE ||
  1048. (fmt->bits_per_sample == 8 &&
  1049. fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
  1050. (fmt->bits_per_sample > 8 &&
  1051. fmt->packing == SOC_MBUS_PACKING_EXTEND16);
  1052. }
  1053. static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
  1054. struct soc_camera_format_xlate *xlate)
  1055. {
  1056. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1057. struct device *dev = icd->dev.parent;
  1058. int formats = 0, ret;
  1059. struct pxa_cam *cam;
  1060. enum v4l2_mbus_pixelcode code;
  1061. const struct soc_mbus_pixelfmt *fmt;
  1062. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  1063. if (ret < 0)
  1064. /* No more formats */
  1065. return 0;
  1066. fmt = soc_mbus_get_fmtdesc(code);
  1067. if (!fmt) {
  1068. dev_err(dev, "Invalid format code #%d: %d\n", idx, code);
  1069. return 0;
  1070. }
  1071. /* This also checks support for the requested bits-per-sample */
  1072. ret = pxa_camera_try_bus_param(icd, fmt->bits_per_sample);
  1073. if (ret < 0)
  1074. return 0;
  1075. if (!icd->host_priv) {
  1076. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  1077. if (!cam)
  1078. return -ENOMEM;
  1079. icd->host_priv = cam;
  1080. } else {
  1081. cam = icd->host_priv;
  1082. }
  1083. switch (code) {
  1084. case V4L2_MBUS_FMT_YUYV8_2X8_BE:
  1085. formats++;
  1086. if (xlate) {
  1087. xlate->host_fmt = &pxa_camera_formats[0];
  1088. xlate->code = code;
  1089. xlate++;
  1090. dev_dbg(dev, "Providing format %s using code %d\n",
  1091. pxa_camera_formats[0].name, code);
  1092. }
  1093. case V4L2_MBUS_FMT_YVYU8_2X8_BE:
  1094. case V4L2_MBUS_FMT_YUYV8_2X8_LE:
  1095. case V4L2_MBUS_FMT_YVYU8_2X8_LE:
  1096. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  1097. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  1098. if (xlate)
  1099. dev_dbg(dev, "Providing format %s packed\n",
  1100. fmt->name);
  1101. break;
  1102. default:
  1103. if (!pxa_camera_packing_supported(fmt))
  1104. return 0;
  1105. if (xlate)
  1106. dev_dbg(dev,
  1107. "Providing format %s in pass-through mode\n",
  1108. fmt->name);
  1109. }
  1110. /* Generic pass-through */
  1111. formats++;
  1112. if (xlate) {
  1113. xlate->host_fmt = fmt;
  1114. xlate->code = code;
  1115. xlate++;
  1116. }
  1117. return formats;
  1118. }
  1119. static void pxa_camera_put_formats(struct soc_camera_device *icd)
  1120. {
  1121. kfree(icd->host_priv);
  1122. icd->host_priv = NULL;
  1123. }
  1124. static int pxa_camera_check_frame(u32 width, u32 height)
  1125. {
  1126. /* limit to pxa hardware capabilities */
  1127. return height < 32 || height > 2048 || width < 48 || width > 2048 ||
  1128. (width & 0x01);
  1129. }
  1130. static int pxa_camera_set_crop(struct soc_camera_device *icd,
  1131. struct v4l2_crop *a)
  1132. {
  1133. struct v4l2_rect *rect = &a->c;
  1134. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1135. struct pxa_camera_dev *pcdev = ici->priv;
  1136. struct device *dev = icd->dev.parent;
  1137. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1138. struct soc_camera_sense sense = {
  1139. .master_clock = pcdev->mclk,
  1140. .pixel_clock_max = pcdev->ciclk / 4,
  1141. };
  1142. struct v4l2_mbus_framefmt mf;
  1143. struct pxa_cam *cam = icd->host_priv;
  1144. u32 fourcc = icd->current_fmt->host_fmt->fourcc;
  1145. int ret;
  1146. /* If PCLK is used to latch data from the sensor, check sense */
  1147. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  1148. icd->sense = &sense;
  1149. ret = v4l2_subdev_call(sd, video, s_crop, a);
  1150. icd->sense = NULL;
  1151. if (ret < 0) {
  1152. dev_warn(dev, "Failed to crop to %ux%u@%u:%u\n",
  1153. rect->width, rect->height, rect->left, rect->top);
  1154. return ret;
  1155. }
  1156. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  1157. if (ret < 0)
  1158. return ret;
  1159. if (pxa_camera_check_frame(mf.width, mf.height)) {
  1160. /*
  1161. * Camera cropping produced a frame beyond our capabilities.
  1162. * FIXME: just extract a subframe, that we can process.
  1163. */
  1164. v4l_bound_align_image(&mf.width, 48, 2048, 1,
  1165. &mf.height, 32, 2048, 0,
  1166. fourcc == V4L2_PIX_FMT_YUV422P ? 4 : 0);
  1167. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
  1168. if (ret < 0)
  1169. return ret;
  1170. if (pxa_camera_check_frame(mf.width, mf.height)) {
  1171. dev_warn(icd->dev.parent,
  1172. "Inconsistent state. Use S_FMT to repair\n");
  1173. return -EINVAL;
  1174. }
  1175. }
  1176. if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
  1177. if (sense.pixel_clock > sense.pixel_clock_max) {
  1178. dev_err(dev,
  1179. "pixel clock %lu set by the camera too high!",
  1180. sense.pixel_clock);
  1181. return -EIO;
  1182. }
  1183. recalculate_fifo_timeout(pcdev, sense.pixel_clock);
  1184. }
  1185. icd->user_width = mf.width;
  1186. icd->user_height = mf.height;
  1187. pxa_camera_setup_cicr(icd, cam->flags, fourcc);
  1188. return ret;
  1189. }
  1190. static int pxa_camera_set_fmt(struct soc_camera_device *icd,
  1191. struct v4l2_format *f)
  1192. {
  1193. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1194. struct pxa_camera_dev *pcdev = ici->priv;
  1195. struct device *dev = icd->dev.parent;
  1196. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1197. const struct soc_camera_format_xlate *xlate = NULL;
  1198. struct soc_camera_sense sense = {
  1199. .master_clock = pcdev->mclk,
  1200. .pixel_clock_max = pcdev->ciclk / 4,
  1201. };
  1202. struct v4l2_pix_format *pix = &f->fmt.pix;
  1203. struct v4l2_mbus_framefmt mf;
  1204. int ret;
  1205. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  1206. if (!xlate) {
  1207. dev_warn(dev, "Format %x not found\n", pix->pixelformat);
  1208. return -EINVAL;
  1209. }
  1210. /* If PCLK is used to latch data from the sensor, check sense */
  1211. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  1212. /* The caller holds a mutex. */
  1213. icd->sense = &sense;
  1214. mf.width = pix->width;
  1215. mf.height = pix->height;
  1216. mf.field = pix->field;
  1217. mf.colorspace = pix->colorspace;
  1218. mf.code = xlate->code;
  1219. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
  1220. if (mf.code != xlate->code)
  1221. return -EINVAL;
  1222. icd->sense = NULL;
  1223. if (ret < 0) {
  1224. dev_warn(dev, "Failed to configure for format %x\n",
  1225. pix->pixelformat);
  1226. } else if (pxa_camera_check_frame(mf.width, mf.height)) {
  1227. dev_warn(dev,
  1228. "Camera driver produced an unsupported frame %dx%d\n",
  1229. mf.width, mf.height);
  1230. ret = -EINVAL;
  1231. } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
  1232. if (sense.pixel_clock > sense.pixel_clock_max) {
  1233. dev_err(dev,
  1234. "pixel clock %lu set by the camera too high!",
  1235. sense.pixel_clock);
  1236. return -EIO;
  1237. }
  1238. recalculate_fifo_timeout(pcdev, sense.pixel_clock);
  1239. }
  1240. if (ret < 0)
  1241. return ret;
  1242. pix->width = mf.width;
  1243. pix->height = mf.height;
  1244. pix->field = mf.field;
  1245. pix->colorspace = mf.colorspace;
  1246. icd->current_fmt = xlate;
  1247. return ret;
  1248. }
  1249. static int pxa_camera_try_fmt(struct soc_camera_device *icd,
  1250. struct v4l2_format *f)
  1251. {
  1252. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1253. const struct soc_camera_format_xlate *xlate;
  1254. struct v4l2_pix_format *pix = &f->fmt.pix;
  1255. struct v4l2_mbus_framefmt mf;
  1256. __u32 pixfmt = pix->pixelformat;
  1257. int ret;
  1258. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1259. if (!xlate) {
  1260. dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
  1261. return -EINVAL;
  1262. }
  1263. /*
  1264. * Limit to pxa hardware capabilities. YUV422P planar format requires
  1265. * images size to be a multiple of 16 bytes. If not, zeros will be
  1266. * inserted between Y and U planes, and U and V planes, which violates
  1267. * the YUV422P standard.
  1268. */
  1269. v4l_bound_align_image(&pix->width, 48, 2048, 1,
  1270. &pix->height, 32, 2048, 0,
  1271. pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
  1272. pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
  1273. xlate->host_fmt);
  1274. if (pix->bytesperline < 0)
  1275. return pix->bytesperline;
  1276. pix->sizeimage = pix->height * pix->bytesperline;
  1277. /* limit to sensor capabilities */
  1278. mf.width = pix->width;
  1279. mf.height = pix->height;
  1280. mf.field = pix->field;
  1281. mf.colorspace = pix->colorspace;
  1282. mf.code = xlate->code;
  1283. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  1284. if (ret < 0)
  1285. return ret;
  1286. pix->width = mf.width;
  1287. pix->height = mf.height;
  1288. pix->colorspace = mf.colorspace;
  1289. switch (mf.field) {
  1290. case V4L2_FIELD_ANY:
  1291. case V4L2_FIELD_NONE:
  1292. pix->field = V4L2_FIELD_NONE;
  1293. break;
  1294. default:
  1295. /* TODO: support interlaced at least in pass-through mode */
  1296. dev_err(icd->dev.parent, "Field type %d unsupported.\n",
  1297. mf.field);
  1298. return -EINVAL;
  1299. }
  1300. return ret;
  1301. }
  1302. static int pxa_camera_reqbufs(struct soc_camera_file *icf,
  1303. struct v4l2_requestbuffers *p)
  1304. {
  1305. int i;
  1306. /*
  1307. * This is for locking debugging only. I removed spinlocks and now I
  1308. * check whether .prepare is ever called on a linked buffer, or whether
  1309. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  1310. * it hadn't triggered
  1311. */
  1312. for (i = 0; i < p->count; i++) {
  1313. struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
  1314. struct pxa_buffer, vb);
  1315. buf->inwork = 0;
  1316. INIT_LIST_HEAD(&buf->vb.queue);
  1317. }
  1318. return 0;
  1319. }
  1320. static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
  1321. {
  1322. struct soc_camera_file *icf = file->private_data;
  1323. struct pxa_buffer *buf;
  1324. buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
  1325. vb.stream);
  1326. poll_wait(file, &buf->vb.done, pt);
  1327. if (buf->vb.state == VIDEOBUF_DONE ||
  1328. buf->vb.state == VIDEOBUF_ERROR)
  1329. return POLLIN|POLLRDNORM;
  1330. return 0;
  1331. }
  1332. static int pxa_camera_querycap(struct soc_camera_host *ici,
  1333. struct v4l2_capability *cap)
  1334. {
  1335. /* cap->name is set by the firendly caller:-> */
  1336. strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
  1337. cap->version = PXA_CAM_VERSION_CODE;
  1338. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1339. return 0;
  1340. }
  1341. static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
  1342. {
  1343. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1344. struct pxa_camera_dev *pcdev = ici->priv;
  1345. int i = 0, ret = 0;
  1346. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
  1347. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
  1348. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
  1349. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
  1350. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
  1351. if ((pcdev->icd) && (pcdev->icd->ops->suspend))
  1352. ret = pcdev->icd->ops->suspend(pcdev->icd, state);
  1353. return ret;
  1354. }
  1355. static int pxa_camera_resume(struct soc_camera_device *icd)
  1356. {
  1357. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1358. struct pxa_camera_dev *pcdev = ici->priv;
  1359. int i = 0, ret = 0;
  1360. DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  1361. DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  1362. DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  1363. __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
  1364. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
  1365. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
  1366. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
  1367. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
  1368. if ((pcdev->icd) && (pcdev->icd->ops->resume))
  1369. ret = pcdev->icd->ops->resume(pcdev->icd);
  1370. /* Restart frame capture if active buffer exists */
  1371. if (!ret && pcdev->active)
  1372. pxa_camera_start_capture(pcdev);
  1373. return ret;
  1374. }
  1375. static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
  1376. .owner = THIS_MODULE,
  1377. .add = pxa_camera_add_device,
  1378. .remove = pxa_camera_remove_device,
  1379. .suspend = pxa_camera_suspend,
  1380. .resume = pxa_camera_resume,
  1381. .set_crop = pxa_camera_set_crop,
  1382. .get_formats = pxa_camera_get_formats,
  1383. .put_formats = pxa_camera_put_formats,
  1384. .set_fmt = pxa_camera_set_fmt,
  1385. .try_fmt = pxa_camera_try_fmt,
  1386. .init_videobuf = pxa_camera_init_videobuf,
  1387. .reqbufs = pxa_camera_reqbufs,
  1388. .poll = pxa_camera_poll,
  1389. .querycap = pxa_camera_querycap,
  1390. .set_bus_param = pxa_camera_set_bus_param,
  1391. };
  1392. static int __devinit pxa_camera_probe(struct platform_device *pdev)
  1393. {
  1394. struct pxa_camera_dev *pcdev;
  1395. struct resource *res;
  1396. void __iomem *base;
  1397. int irq;
  1398. int err = 0;
  1399. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1400. irq = platform_get_irq(pdev, 0);
  1401. if (!res || irq < 0) {
  1402. err = -ENODEV;
  1403. goto exit;
  1404. }
  1405. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1406. if (!pcdev) {
  1407. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1408. err = -ENOMEM;
  1409. goto exit;
  1410. }
  1411. pcdev->clk = clk_get(&pdev->dev, NULL);
  1412. if (IS_ERR(pcdev->clk)) {
  1413. err = PTR_ERR(pcdev->clk);
  1414. goto exit_kfree;
  1415. }
  1416. pcdev->res = res;
  1417. pcdev->pdata = pdev->dev.platform_data;
  1418. pcdev->platform_flags = pcdev->pdata->flags;
  1419. if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
  1420. PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
  1421. /*
  1422. * Platform hasn't set available data widths. This is bad.
  1423. * Warn and use a default.
  1424. */
  1425. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
  1426. "data widths, using default 10 bit\n");
  1427. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  1428. }
  1429. pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
  1430. if (!pcdev->mclk) {
  1431. dev_warn(&pdev->dev,
  1432. "mclk == 0! Please, fix your platform data. "
  1433. "Using default 20MHz\n");
  1434. pcdev->mclk = 20000000;
  1435. }
  1436. pcdev->mclk_divisor = mclk_get_divisor(pdev, pcdev);
  1437. INIT_LIST_HEAD(&pcdev->capture);
  1438. spin_lock_init(&pcdev->lock);
  1439. /*
  1440. * Request the regions.
  1441. */
  1442. if (!request_mem_region(res->start, resource_size(res),
  1443. PXA_CAM_DRV_NAME)) {
  1444. err = -EBUSY;
  1445. goto exit_clk;
  1446. }
  1447. base = ioremap(res->start, resource_size(res));
  1448. if (!base) {
  1449. err = -ENOMEM;
  1450. goto exit_release;
  1451. }
  1452. pcdev->irq = irq;
  1453. pcdev->base = base;
  1454. /* request dma */
  1455. err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
  1456. pxa_camera_dma_irq_y, pcdev);
  1457. if (err < 0) {
  1458. dev_err(&pdev->dev, "Can't request DMA for Y\n");
  1459. goto exit_iounmap;
  1460. }
  1461. pcdev->dma_chans[0] = err;
  1462. dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
  1463. err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
  1464. pxa_camera_dma_irq_u, pcdev);
  1465. if (err < 0) {
  1466. dev_err(&pdev->dev, "Can't request DMA for U\n");
  1467. goto exit_free_dma_y;
  1468. }
  1469. pcdev->dma_chans[1] = err;
  1470. dev_dbg(&pdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
  1471. err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
  1472. pxa_camera_dma_irq_v, pcdev);
  1473. if (err < 0) {
  1474. dev_err(&pdev->dev, "Can't request DMA for V\n");
  1475. goto exit_free_dma_u;
  1476. }
  1477. pcdev->dma_chans[2] = err;
  1478. dev_dbg(&pdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
  1479. DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  1480. DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  1481. DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  1482. /* request irq */
  1483. err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
  1484. pcdev);
  1485. if (err) {
  1486. dev_err(&pdev->dev, "Camera interrupt register failed \n");
  1487. goto exit_free_dma;
  1488. }
  1489. pcdev->soc_host.drv_name = PXA_CAM_DRV_NAME;
  1490. pcdev->soc_host.ops = &pxa_soc_camera_host_ops;
  1491. pcdev->soc_host.priv = pcdev;
  1492. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1493. pcdev->soc_host.nr = pdev->id;
  1494. err = soc_camera_host_register(&pcdev->soc_host);
  1495. if (err)
  1496. goto exit_free_irq;
  1497. return 0;
  1498. exit_free_irq:
  1499. free_irq(pcdev->irq, pcdev);
  1500. exit_free_dma:
  1501. pxa_free_dma(pcdev->dma_chans[2]);
  1502. exit_free_dma_u:
  1503. pxa_free_dma(pcdev->dma_chans[1]);
  1504. exit_free_dma_y:
  1505. pxa_free_dma(pcdev->dma_chans[0]);
  1506. exit_iounmap:
  1507. iounmap(base);
  1508. exit_release:
  1509. release_mem_region(res->start, resource_size(res));
  1510. exit_clk:
  1511. clk_put(pcdev->clk);
  1512. exit_kfree:
  1513. kfree(pcdev);
  1514. exit:
  1515. return err;
  1516. }
  1517. static int __devexit pxa_camera_remove(struct platform_device *pdev)
  1518. {
  1519. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1520. struct pxa_camera_dev *pcdev = container_of(soc_host,
  1521. struct pxa_camera_dev, soc_host);
  1522. struct resource *res;
  1523. clk_put(pcdev->clk);
  1524. pxa_free_dma(pcdev->dma_chans[0]);
  1525. pxa_free_dma(pcdev->dma_chans[1]);
  1526. pxa_free_dma(pcdev->dma_chans[2]);
  1527. free_irq(pcdev->irq, pcdev);
  1528. soc_camera_host_unregister(soc_host);
  1529. iounmap(pcdev->base);
  1530. res = pcdev->res;
  1531. release_mem_region(res->start, resource_size(res));
  1532. kfree(pcdev);
  1533. dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
  1534. return 0;
  1535. }
  1536. static struct platform_driver pxa_camera_driver = {
  1537. .driver = {
  1538. .name = PXA_CAM_DRV_NAME,
  1539. },
  1540. .probe = pxa_camera_probe,
  1541. .remove = __devexit_p(pxa_camera_remove),
  1542. };
  1543. static int __init pxa_camera_init(void)
  1544. {
  1545. return platform_driver_register(&pxa_camera_driver);
  1546. }
  1547. static void __exit pxa_camera_exit(void)
  1548. {
  1549. platform_driver_unregister(&pxa_camera_driver);
  1550. }
  1551. module_init(pxa_camera_init);
  1552. module_exit(pxa_camera_exit);
  1553. MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
  1554. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1555. MODULE_LICENSE("GPL");
  1556. MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME);