pxa_camera.c 43 KB

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