pxa_camera.c 48 KB

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