pxa_camera.c 51 KB

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