pxa_camera.c 48 KB

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