pxa_camera.c 50 KB

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