pxa_camera.c 43 KB

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