mx2_camera.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. /*
  2. * V4L2 Driver for i.MX27/i.MX25 camera host
  3. *
  4. * Copyright (C) 2008, Sascha Hauer, Pengutronix
  5. * Copyright (C) 2010, Baruch Siach, Orex Computed Radiography
  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/slab.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/errno.h>
  19. #include <linux/fs.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/time.h>
  25. #include <linux/version.h>
  26. #include <linux/device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/mutex.h>
  29. #include <linux/clk.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-dev.h>
  32. #include <media/videobuf-core.h>
  33. #include <media/videobuf-dma-contig.h>
  34. #include <media/soc_camera.h>
  35. #include <media/soc_mediabus.h>
  36. #include <linux/videodev2.h>
  37. #include <mach/mx2_cam.h>
  38. #ifdef CONFIG_MACH_MX27
  39. #include <mach/dma-mx1-mx2.h>
  40. #endif
  41. #include <mach/hardware.h>
  42. #include <asm/dma.h>
  43. #define MX2_CAM_DRV_NAME "mx2-camera"
  44. #define MX2_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
  45. #define MX2_CAM_DRIVER_DESCRIPTION "i.MX2x_Camera"
  46. /* reset values */
  47. #define CSICR1_RESET_VAL 0x40000800
  48. #define CSICR2_RESET_VAL 0x0
  49. #define CSICR3_RESET_VAL 0x0
  50. /* csi control reg 1 */
  51. #define CSICR1_SWAP16_EN (1 << 31)
  52. #define CSICR1_EXT_VSYNC (1 << 30)
  53. #define CSICR1_EOF_INTEN (1 << 29)
  54. #define CSICR1_PRP_IF_EN (1 << 28)
  55. #define CSICR1_CCIR_MODE (1 << 27)
  56. #define CSICR1_COF_INTEN (1 << 26)
  57. #define CSICR1_SF_OR_INTEN (1 << 25)
  58. #define CSICR1_RF_OR_INTEN (1 << 24)
  59. #define CSICR1_STATFF_LEVEL (3 << 22)
  60. #define CSICR1_STATFF_INTEN (1 << 21)
  61. #define CSICR1_RXFF_LEVEL(l) (((l) & 3) << 19) /* MX27 */
  62. #define CSICR1_FB2_DMA_INTEN (1 << 20) /* MX25 */
  63. #define CSICR1_FB1_DMA_INTEN (1 << 19) /* MX25 */
  64. #define CSICR1_RXFF_INTEN (1 << 18)
  65. #define CSICR1_SOF_POL (1 << 17)
  66. #define CSICR1_SOF_INTEN (1 << 16)
  67. #define CSICR1_MCLKDIV(d) (((d) & 0xF) << 12)
  68. #define CSICR1_HSYNC_POL (1 << 11)
  69. #define CSICR1_CCIR_EN (1 << 10)
  70. #define CSICR1_MCLKEN (1 << 9)
  71. #define CSICR1_FCC (1 << 8)
  72. #define CSICR1_PACK_DIR (1 << 7)
  73. #define CSICR1_CLR_STATFIFO (1 << 6)
  74. #define CSICR1_CLR_RXFIFO (1 << 5)
  75. #define CSICR1_GCLK_MODE (1 << 4)
  76. #define CSICR1_INV_DATA (1 << 3)
  77. #define CSICR1_INV_PCLK (1 << 2)
  78. #define CSICR1_REDGE (1 << 1)
  79. #define SHIFT_STATFF_LEVEL 22
  80. #define SHIFT_RXFF_LEVEL 19
  81. #define SHIFT_MCLKDIV 12
  82. /* control reg 3 */
  83. #define CSICR3_FRMCNT (0xFFFF << 16)
  84. #define CSICR3_FRMCNT_RST (1 << 15)
  85. #define CSICR3_DMA_REFLASH_RFF (1 << 14)
  86. #define CSICR3_DMA_REFLASH_SFF (1 << 13)
  87. #define CSICR3_DMA_REQ_EN_RFF (1 << 12)
  88. #define CSICR3_DMA_REQ_EN_SFF (1 << 11)
  89. #define CSICR3_RXFF_LEVEL(l) (((l) & 7) << 4) /* MX25 */
  90. #define CSICR3_CSI_SUP (1 << 3)
  91. #define CSICR3_ZERO_PACK_EN (1 << 2)
  92. #define CSICR3_ECC_INT_EN (1 << 1)
  93. #define CSICR3_ECC_AUTO_EN (1 << 0)
  94. #define SHIFT_FRMCNT 16
  95. /* csi status reg */
  96. #define CSISR_SFF_OR_INT (1 << 25)
  97. #define CSISR_RFF_OR_INT (1 << 24)
  98. #define CSISR_STATFF_INT (1 << 21)
  99. #define CSISR_DMA_TSF_FB2_INT (1 << 20) /* MX25 */
  100. #define CSISR_DMA_TSF_FB1_INT (1 << 19) /* MX25 */
  101. #define CSISR_RXFF_INT (1 << 18)
  102. #define CSISR_EOF_INT (1 << 17)
  103. #define CSISR_SOF_INT (1 << 16)
  104. #define CSISR_F2_INT (1 << 15)
  105. #define CSISR_F1_INT (1 << 14)
  106. #define CSISR_COF_INT (1 << 13)
  107. #define CSISR_ECC_INT (1 << 1)
  108. #define CSISR_DRDY (1 << 0)
  109. #define CSICR1 0x00
  110. #define CSICR2 0x04
  111. #define CSISR (cpu_is_mx27() ? 0x08 : 0x18)
  112. #define CSISTATFIFO 0x0c
  113. #define CSIRFIFO 0x10
  114. #define CSIRXCNT 0x14
  115. #define CSICR3 (cpu_is_mx27() ? 0x1C : 0x08)
  116. #define CSIDMASA_STATFIFO 0x20
  117. #define CSIDMATA_STATFIFO 0x24
  118. #define CSIDMASA_FB1 0x28
  119. #define CSIDMASA_FB2 0x2c
  120. #define CSIFBUF_PARA 0x30
  121. #define CSIIMAG_PARA 0x34
  122. /* EMMA PrP */
  123. #define PRP_CNTL 0x00
  124. #define PRP_INTR_CNTL 0x04
  125. #define PRP_INTRSTATUS 0x08
  126. #define PRP_SOURCE_Y_PTR 0x0c
  127. #define PRP_SOURCE_CB_PTR 0x10
  128. #define PRP_SOURCE_CR_PTR 0x14
  129. #define PRP_DEST_RGB1_PTR 0x18
  130. #define PRP_DEST_RGB2_PTR 0x1c
  131. #define PRP_DEST_Y_PTR 0x20
  132. #define PRP_DEST_CB_PTR 0x24
  133. #define PRP_DEST_CR_PTR 0x28
  134. #define PRP_SRC_FRAME_SIZE 0x2c
  135. #define PRP_DEST_CH1_LINE_STRIDE 0x30
  136. #define PRP_SRC_PIXEL_FORMAT_CNTL 0x34
  137. #define PRP_CH1_PIXEL_FORMAT_CNTL 0x38
  138. #define PRP_CH1_OUT_IMAGE_SIZE 0x3c
  139. #define PRP_CH2_OUT_IMAGE_SIZE 0x40
  140. #define PRP_SRC_LINE_STRIDE 0x44
  141. #define PRP_CSC_COEF_012 0x48
  142. #define PRP_CSC_COEF_345 0x4c
  143. #define PRP_CSC_COEF_678 0x50
  144. #define PRP_CH1_RZ_HORI_COEF1 0x54
  145. #define PRP_CH1_RZ_HORI_COEF2 0x58
  146. #define PRP_CH1_RZ_HORI_VALID 0x5c
  147. #define PRP_CH1_RZ_VERT_COEF1 0x60
  148. #define PRP_CH1_RZ_VERT_COEF2 0x64
  149. #define PRP_CH1_RZ_VERT_VALID 0x68
  150. #define PRP_CH2_RZ_HORI_COEF1 0x6c
  151. #define PRP_CH2_RZ_HORI_COEF2 0x70
  152. #define PRP_CH2_RZ_HORI_VALID 0x74
  153. #define PRP_CH2_RZ_VERT_COEF1 0x78
  154. #define PRP_CH2_RZ_VERT_COEF2 0x7c
  155. #define PRP_CH2_RZ_VERT_VALID 0x80
  156. #define PRP_CNTL_CH1EN (1 << 0)
  157. #define PRP_CNTL_CH2EN (1 << 1)
  158. #define PRP_CNTL_CSIEN (1 << 2)
  159. #define PRP_CNTL_DATA_IN_YUV420 (0 << 3)
  160. #define PRP_CNTL_DATA_IN_YUV422 (1 << 3)
  161. #define PRP_CNTL_DATA_IN_RGB16 (2 << 3)
  162. #define PRP_CNTL_DATA_IN_RGB32 (3 << 3)
  163. #define PRP_CNTL_CH1_OUT_RGB8 (0 << 5)
  164. #define PRP_CNTL_CH1_OUT_RGB16 (1 << 5)
  165. #define PRP_CNTL_CH1_OUT_RGB32 (2 << 5)
  166. #define PRP_CNTL_CH1_OUT_YUV422 (3 << 5)
  167. #define PRP_CNTL_CH2_OUT_YUV420 (0 << 7)
  168. #define PRP_CNTL_CH2_OUT_YUV422 (1 << 7)
  169. #define PRP_CNTL_CH2_OUT_YUV444 (2 << 7)
  170. #define PRP_CNTL_CH1_LEN (1 << 9)
  171. #define PRP_CNTL_CH2_LEN (1 << 10)
  172. #define PRP_CNTL_SKIP_FRAME (1 << 11)
  173. #define PRP_CNTL_SWRST (1 << 12)
  174. #define PRP_CNTL_CLKEN (1 << 13)
  175. #define PRP_CNTL_WEN (1 << 14)
  176. #define PRP_CNTL_CH1BYP (1 << 15)
  177. #define PRP_CNTL_IN_TSKIP(x) ((x) << 16)
  178. #define PRP_CNTL_CH1_TSKIP(x) ((x) << 19)
  179. #define PRP_CNTL_CH2_TSKIP(x) ((x) << 22)
  180. #define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25)
  181. #define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27)
  182. #define PRP_CNTL_CH2B1EN (1 << 29)
  183. #define PRP_CNTL_CH2B2EN (1 << 30)
  184. #define PRP_CNTL_CH2FEN (1 << 31)
  185. /* IRQ Enable and status register */
  186. #define PRP_INTR_RDERR (1 << 0)
  187. #define PRP_INTR_CH1WERR (1 << 1)
  188. #define PRP_INTR_CH2WERR (1 << 2)
  189. #define PRP_INTR_CH1FC (1 << 3)
  190. #define PRP_INTR_CH2FC (1 << 5)
  191. #define PRP_INTR_LBOVF (1 << 7)
  192. #define PRP_INTR_CH2OVF (1 << 8)
  193. #define mx27_camera_emma(pcdev) (cpu_is_mx27() && pcdev->use_emma)
  194. #define MAX_VIDEO_MEM 16
  195. struct mx2_camera_dev {
  196. struct device *dev;
  197. struct soc_camera_host soc_host;
  198. struct soc_camera_device *icd;
  199. struct clk *clk_csi, *clk_emma;
  200. unsigned int irq_csi, irq_emma;
  201. void __iomem *base_csi, *base_emma;
  202. unsigned long base_dma;
  203. struct mx2_camera_platform_data *pdata;
  204. struct resource *res_csi, *res_emma;
  205. unsigned long platform_flags;
  206. struct list_head capture;
  207. struct list_head active_bufs;
  208. spinlock_t lock;
  209. int dma;
  210. struct mx2_buffer *active;
  211. struct mx2_buffer *fb1_active;
  212. struct mx2_buffer *fb2_active;
  213. int use_emma;
  214. u32 csicr1;
  215. void *discard_buffer;
  216. dma_addr_t discard_buffer_dma;
  217. size_t discard_size;
  218. };
  219. /* buffer for one video frame */
  220. struct mx2_buffer {
  221. /* common v4l buffer stuff -- must be first */
  222. struct videobuf_buffer vb;
  223. enum v4l2_mbus_pixelcode code;
  224. int bufnum;
  225. };
  226. static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev)
  227. {
  228. unsigned long flags;
  229. clk_disable(pcdev->clk_csi);
  230. writel(0, pcdev->base_csi + CSICR1);
  231. if (mx27_camera_emma(pcdev)) {
  232. writel(0, pcdev->base_emma + PRP_CNTL);
  233. } else if (cpu_is_mx25()) {
  234. spin_lock_irqsave(&pcdev->lock, flags);
  235. pcdev->fb1_active = NULL;
  236. pcdev->fb2_active = NULL;
  237. writel(0, pcdev->base_csi + CSIDMASA_FB1);
  238. writel(0, pcdev->base_csi + CSIDMASA_FB2);
  239. spin_unlock_irqrestore(&pcdev->lock, flags);
  240. }
  241. }
  242. /*
  243. * The following two functions absolutely depend on the fact, that
  244. * there can be only one camera on mx2 camera sensor interface
  245. */
  246. static int mx2_camera_add_device(struct soc_camera_device *icd)
  247. {
  248. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  249. struct mx2_camera_dev *pcdev = ici->priv;
  250. int ret;
  251. u32 csicr1;
  252. if (pcdev->icd)
  253. return -EBUSY;
  254. ret = clk_enable(pcdev->clk_csi);
  255. if (ret < 0)
  256. return ret;
  257. csicr1 = CSICR1_MCLKEN;
  258. if (mx27_camera_emma(pcdev)) {
  259. csicr1 |= CSICR1_PRP_IF_EN | CSICR1_FCC |
  260. CSICR1_RXFF_LEVEL(0);
  261. } else if (cpu_is_mx27())
  262. csicr1 |= CSICR1_SOF_INTEN | CSICR1_RXFF_LEVEL(2);
  263. pcdev->csicr1 = csicr1;
  264. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  265. pcdev->icd = icd;
  266. dev_info(icd->dev.parent, "Camera driver attached to camera %d\n",
  267. icd->devnum);
  268. return 0;
  269. }
  270. static void mx2_camera_remove_device(struct soc_camera_device *icd)
  271. {
  272. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  273. struct mx2_camera_dev *pcdev = ici->priv;
  274. BUG_ON(icd != pcdev->icd);
  275. dev_info(icd->dev.parent, "Camera driver detached from camera %d\n",
  276. icd->devnum);
  277. mx2_camera_deactivate(pcdev);
  278. if (pcdev->discard_buffer) {
  279. dma_free_coherent(ici->v4l2_dev.dev, pcdev->discard_size,
  280. pcdev->discard_buffer,
  281. pcdev->discard_buffer_dma);
  282. pcdev->discard_buffer = NULL;
  283. }
  284. pcdev->icd = NULL;
  285. }
  286. #ifdef CONFIG_MACH_MX27
  287. static void mx27_camera_dma_enable(struct mx2_camera_dev *pcdev)
  288. {
  289. u32 tmp;
  290. imx_dma_enable(pcdev->dma);
  291. tmp = readl(pcdev->base_csi + CSICR1);
  292. tmp |= CSICR1_RF_OR_INTEN;
  293. writel(tmp, pcdev->base_csi + CSICR1);
  294. }
  295. static irqreturn_t mx27_camera_irq(int irq_csi, void *data)
  296. {
  297. struct mx2_camera_dev *pcdev = data;
  298. u32 status = readl(pcdev->base_csi + CSISR);
  299. if (status & CSISR_SOF_INT && pcdev->active) {
  300. u32 tmp;
  301. tmp = readl(pcdev->base_csi + CSICR1);
  302. writel(tmp | CSICR1_CLR_RXFIFO, pcdev->base_csi + CSICR1);
  303. mx27_camera_dma_enable(pcdev);
  304. }
  305. writel(CSISR_SOF_INT | CSISR_RFF_OR_INT, pcdev->base_csi + CSISR);
  306. return IRQ_HANDLED;
  307. }
  308. #else
  309. static irqreturn_t mx27_camera_irq(int irq_csi, void *data)
  310. {
  311. return IRQ_NONE;
  312. }
  313. #endif /* CONFIG_MACH_MX27 */
  314. static void mx25_camera_frame_done(struct mx2_camera_dev *pcdev, int fb,
  315. int state)
  316. {
  317. struct videobuf_buffer *vb;
  318. struct mx2_buffer *buf;
  319. struct mx2_buffer **fb_active = fb == 1 ? &pcdev->fb1_active :
  320. &pcdev->fb2_active;
  321. u32 fb_reg = fb == 1 ? CSIDMASA_FB1 : CSIDMASA_FB2;
  322. unsigned long flags;
  323. spin_lock_irqsave(&pcdev->lock, flags);
  324. if (*fb_active == NULL)
  325. goto out;
  326. vb = &(*fb_active)->vb;
  327. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  328. vb, vb->baddr, vb->bsize);
  329. vb->state = state;
  330. do_gettimeofday(&vb->ts);
  331. vb->field_count++;
  332. wake_up(&vb->done);
  333. if (list_empty(&pcdev->capture)) {
  334. buf = NULL;
  335. writel(0, pcdev->base_csi + fb_reg);
  336. } else {
  337. buf = list_entry(pcdev->capture.next, struct mx2_buffer,
  338. vb.queue);
  339. vb = &buf->vb;
  340. list_del(&vb->queue);
  341. vb->state = VIDEOBUF_ACTIVE;
  342. writel(videobuf_to_dma_contig(vb), pcdev->base_csi + fb_reg);
  343. }
  344. *fb_active = buf;
  345. out:
  346. spin_unlock_irqrestore(&pcdev->lock, flags);
  347. }
  348. static irqreturn_t mx25_camera_irq(int irq_csi, void *data)
  349. {
  350. struct mx2_camera_dev *pcdev = data;
  351. u32 status = readl(pcdev->base_csi + CSISR);
  352. if (status & CSISR_DMA_TSF_FB1_INT)
  353. mx25_camera_frame_done(pcdev, 1, VIDEOBUF_DONE);
  354. else if (status & CSISR_DMA_TSF_FB2_INT)
  355. mx25_camera_frame_done(pcdev, 2, VIDEOBUF_DONE);
  356. /* FIXME: handle CSISR_RFF_OR_INT */
  357. writel(status, pcdev->base_csi + CSISR);
  358. return IRQ_HANDLED;
  359. }
  360. /*
  361. * Videobuf operations
  362. */
  363. static int mx2_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
  364. unsigned int *size)
  365. {
  366. struct soc_camera_device *icd = vq->priv_data;
  367. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  368. icd->current_fmt->host_fmt);
  369. dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
  370. if (bytes_per_line < 0)
  371. return bytes_per_line;
  372. *size = bytes_per_line * icd->user_height;
  373. if (0 == *count)
  374. *count = 32;
  375. if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
  376. *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
  377. return 0;
  378. }
  379. static void free_buffer(struct videobuf_queue *vq, struct mx2_buffer *buf)
  380. {
  381. struct soc_camera_device *icd = vq->priv_data;
  382. struct videobuf_buffer *vb = &buf->vb;
  383. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  384. vb, vb->baddr, vb->bsize);
  385. /*
  386. * This waits until this buffer is out of danger, i.e., until it is no
  387. * longer in state VIDEOBUF_QUEUED or VIDEOBUF_ACTIVE
  388. */
  389. videobuf_waiton(vq, vb, 0, 0);
  390. videobuf_dma_contig_free(vq, vb);
  391. dev_dbg(&icd->dev, "%s freed\n", __func__);
  392. vb->state = VIDEOBUF_NEEDS_INIT;
  393. }
  394. static int mx2_videobuf_prepare(struct videobuf_queue *vq,
  395. struct videobuf_buffer *vb, enum v4l2_field field)
  396. {
  397. struct soc_camera_device *icd = vq->priv_data;
  398. struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
  399. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  400. icd->current_fmt->host_fmt);
  401. int ret = 0;
  402. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  403. vb, vb->baddr, vb->bsize);
  404. if (bytes_per_line < 0)
  405. return bytes_per_line;
  406. #ifdef DEBUG
  407. /*
  408. * This can be useful if you want to see if we actually fill
  409. * the buffer with something
  410. */
  411. memset((void *)vb->baddr, 0xaa, vb->bsize);
  412. #endif
  413. if (buf->code != icd->current_fmt->code ||
  414. vb->width != icd->user_width ||
  415. vb->height != icd->user_height ||
  416. vb->field != field) {
  417. buf->code = icd->current_fmt->code;
  418. vb->width = icd->user_width;
  419. vb->height = icd->user_height;
  420. vb->field = field;
  421. vb->state = VIDEOBUF_NEEDS_INIT;
  422. }
  423. vb->size = bytes_per_line * vb->height;
  424. if (vb->baddr && vb->bsize < vb->size) {
  425. ret = -EINVAL;
  426. goto out;
  427. }
  428. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  429. ret = videobuf_iolock(vq, vb, NULL);
  430. if (ret)
  431. goto fail;
  432. vb->state = VIDEOBUF_PREPARED;
  433. }
  434. return 0;
  435. fail:
  436. free_buffer(vq, buf);
  437. out:
  438. return ret;
  439. }
  440. static void mx2_videobuf_queue(struct videobuf_queue *vq,
  441. struct videobuf_buffer *vb)
  442. {
  443. struct soc_camera_device *icd = vq->priv_data;
  444. struct soc_camera_host *ici =
  445. to_soc_camera_host(icd->dev.parent);
  446. struct mx2_camera_dev *pcdev = ici->priv;
  447. struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
  448. unsigned long flags;
  449. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  450. vb, vb->baddr, vb->bsize);
  451. spin_lock_irqsave(&pcdev->lock, flags);
  452. vb->state = VIDEOBUF_QUEUED;
  453. list_add_tail(&vb->queue, &pcdev->capture);
  454. if (mx27_camera_emma(pcdev)) {
  455. goto out;
  456. #ifdef CONFIG_MACH_MX27
  457. } else if (cpu_is_mx27()) {
  458. int ret;
  459. if (pcdev->active == NULL) {
  460. ret = imx_dma_setup_single(pcdev->dma,
  461. videobuf_to_dma_contig(vb), vb->size,
  462. (u32)pcdev->base_dma + 0x10,
  463. DMA_MODE_READ);
  464. if (ret) {
  465. vb->state = VIDEOBUF_ERROR;
  466. wake_up(&vb->done);
  467. goto out;
  468. }
  469. vb->state = VIDEOBUF_ACTIVE;
  470. pcdev->active = buf;
  471. }
  472. #endif
  473. } else { /* cpu_is_mx25() */
  474. u32 csicr3, dma_inten = 0;
  475. if (pcdev->fb1_active == NULL) {
  476. writel(videobuf_to_dma_contig(vb),
  477. pcdev->base_csi + CSIDMASA_FB1);
  478. pcdev->fb1_active = buf;
  479. dma_inten = CSICR1_FB1_DMA_INTEN;
  480. } else if (pcdev->fb2_active == NULL) {
  481. writel(videobuf_to_dma_contig(vb),
  482. pcdev->base_csi + CSIDMASA_FB2);
  483. pcdev->fb2_active = buf;
  484. dma_inten = CSICR1_FB2_DMA_INTEN;
  485. }
  486. if (dma_inten) {
  487. list_del(&vb->queue);
  488. vb->state = VIDEOBUF_ACTIVE;
  489. csicr3 = readl(pcdev->base_csi + CSICR3);
  490. /* Reflash DMA */
  491. writel(csicr3 | CSICR3_DMA_REFLASH_RFF,
  492. pcdev->base_csi + CSICR3);
  493. /* clear & enable interrupts */
  494. writel(dma_inten, pcdev->base_csi + CSISR);
  495. pcdev->csicr1 |= dma_inten;
  496. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  497. /* enable DMA */
  498. csicr3 |= CSICR3_DMA_REQ_EN_RFF | CSICR3_RXFF_LEVEL(1);
  499. writel(csicr3, pcdev->base_csi + CSICR3);
  500. }
  501. }
  502. out:
  503. spin_unlock_irqrestore(&pcdev->lock, flags);
  504. }
  505. static void mx2_videobuf_release(struct videobuf_queue *vq,
  506. struct videobuf_buffer *vb)
  507. {
  508. struct soc_camera_device *icd = vq->priv_data;
  509. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  510. struct mx2_camera_dev *pcdev = ici->priv;
  511. struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
  512. unsigned long flags;
  513. #ifdef DEBUG
  514. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  515. vb, vb->baddr, vb->bsize);
  516. switch (vb->state) {
  517. case VIDEOBUF_ACTIVE:
  518. dev_info(&icd->dev, "%s (active)\n", __func__);
  519. break;
  520. case VIDEOBUF_QUEUED:
  521. dev_info(&icd->dev, "%s (queued)\n", __func__);
  522. break;
  523. case VIDEOBUF_PREPARED:
  524. dev_info(&icd->dev, "%s (prepared)\n", __func__);
  525. break;
  526. default:
  527. dev_info(&icd->dev, "%s (unknown) %d\n", __func__,
  528. vb->state);
  529. break;
  530. }
  531. #endif
  532. /*
  533. * Terminate only queued but inactive buffers. Active buffers are
  534. * released when they become inactive after videobuf_waiton().
  535. *
  536. * FIXME: implement forced termination of active buffers for mx27 and
  537. * mx27 eMMA, so that the user won't get stuck in an uninterruptible
  538. * state. This requires a specific handling for each of the these DMA
  539. * types.
  540. */
  541. spin_lock_irqsave(&pcdev->lock, flags);
  542. if (vb->state == VIDEOBUF_QUEUED) {
  543. list_del(&vb->queue);
  544. vb->state = VIDEOBUF_ERROR;
  545. } else if (cpu_is_mx25() && vb->state == VIDEOBUF_ACTIVE) {
  546. if (pcdev->fb1_active == buf) {
  547. pcdev->csicr1 &= ~CSICR1_FB1_DMA_INTEN;
  548. writel(0, pcdev->base_csi + CSIDMASA_FB1);
  549. pcdev->fb1_active = NULL;
  550. } else if (pcdev->fb2_active == buf) {
  551. pcdev->csicr1 &= ~CSICR1_FB2_DMA_INTEN;
  552. writel(0, pcdev->base_csi + CSIDMASA_FB2);
  553. pcdev->fb2_active = NULL;
  554. }
  555. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  556. vb->state = VIDEOBUF_ERROR;
  557. }
  558. spin_unlock_irqrestore(&pcdev->lock, flags);
  559. free_buffer(vq, buf);
  560. }
  561. static struct videobuf_queue_ops mx2_videobuf_ops = {
  562. .buf_setup = mx2_videobuf_setup,
  563. .buf_prepare = mx2_videobuf_prepare,
  564. .buf_queue = mx2_videobuf_queue,
  565. .buf_release = mx2_videobuf_release,
  566. };
  567. static void mx2_camera_init_videobuf(struct videobuf_queue *q,
  568. struct soc_camera_device *icd)
  569. {
  570. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  571. struct mx2_camera_dev *pcdev = ici->priv;
  572. videobuf_queue_dma_contig_init(q, &mx2_videobuf_ops, pcdev->dev,
  573. &pcdev->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  574. V4L2_FIELD_NONE, sizeof(struct mx2_buffer), icd, NULL);
  575. }
  576. #define MX2_BUS_FLAGS (SOCAM_DATAWIDTH_8 | \
  577. SOCAM_MASTER | \
  578. SOCAM_VSYNC_ACTIVE_HIGH | \
  579. SOCAM_VSYNC_ACTIVE_LOW | \
  580. SOCAM_HSYNC_ACTIVE_HIGH | \
  581. SOCAM_HSYNC_ACTIVE_LOW | \
  582. SOCAM_PCLK_SAMPLE_RISING | \
  583. SOCAM_PCLK_SAMPLE_FALLING | \
  584. SOCAM_DATA_ACTIVE_HIGH | \
  585. SOCAM_DATA_ACTIVE_LOW)
  586. static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
  587. {
  588. u32 cntl;
  589. int count = 0;
  590. cntl = readl(pcdev->base_emma + PRP_CNTL);
  591. writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
  592. while (count++ < 100) {
  593. if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
  594. return 0;
  595. barrier();
  596. udelay(1);
  597. }
  598. return -ETIMEDOUT;
  599. }
  600. static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
  601. int bytesperline)
  602. {
  603. struct soc_camera_host *ici =
  604. to_soc_camera_host(icd->dev.parent);
  605. struct mx2_camera_dev *pcdev = ici->priv;
  606. writel(pcdev->discard_buffer_dma,
  607. pcdev->base_emma + PRP_DEST_RGB1_PTR);
  608. writel(pcdev->discard_buffer_dma,
  609. pcdev->base_emma + PRP_DEST_RGB2_PTR);
  610. /*
  611. * We only use the EMMA engine to get rid of the broken
  612. * DMA Engine. No color space consversion at the moment.
  613. * We set the incomming and outgoing pixelformat to an
  614. * 16 Bit wide format and adjust the bytesperline
  615. * accordingly. With this configuration the inputdata
  616. * will not be changed by the emma and could be any type
  617. * of 16 Bit Pixelformat.
  618. */
  619. writel(PRP_CNTL_CH1EN |
  620. PRP_CNTL_CSIEN |
  621. PRP_CNTL_DATA_IN_RGB16 |
  622. PRP_CNTL_CH1_OUT_RGB16 |
  623. PRP_CNTL_CH1_LEN |
  624. PRP_CNTL_CH1BYP |
  625. PRP_CNTL_CH1_TSKIP(0) |
  626. PRP_CNTL_IN_TSKIP(0),
  627. pcdev->base_emma + PRP_CNTL);
  628. writel(((bytesperline >> 1) << 16) | icd->user_height,
  629. pcdev->base_emma + PRP_SRC_FRAME_SIZE);
  630. writel(((bytesperline >> 1) << 16) | icd->user_height,
  631. pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
  632. writel(bytesperline,
  633. pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
  634. writel(0x2ca00565, /* RGB565 */
  635. pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
  636. writel(0x2ca00565, /* RGB565 */
  637. pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
  638. /* Enable interrupts */
  639. writel(PRP_INTR_RDERR |
  640. PRP_INTR_CH1WERR |
  641. PRP_INTR_CH2WERR |
  642. PRP_INTR_CH1FC |
  643. PRP_INTR_CH2FC |
  644. PRP_INTR_LBOVF |
  645. PRP_INTR_CH2OVF,
  646. pcdev->base_emma + PRP_INTR_CNTL);
  647. }
  648. static int mx2_camera_set_bus_param(struct soc_camera_device *icd,
  649. __u32 pixfmt)
  650. {
  651. struct soc_camera_host *ici =
  652. to_soc_camera_host(icd->dev.parent);
  653. struct mx2_camera_dev *pcdev = ici->priv;
  654. unsigned long camera_flags, common_flags;
  655. int ret = 0;
  656. int bytesperline;
  657. u32 csicr1 = pcdev->csicr1;
  658. camera_flags = icd->ops->query_bus_param(icd);
  659. common_flags = soc_camera_bus_param_compatible(camera_flags,
  660. MX2_BUS_FLAGS);
  661. if (!common_flags)
  662. return -EINVAL;
  663. if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
  664. (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
  665. if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
  666. common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
  667. else
  668. common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
  669. }
  670. if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
  671. (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
  672. if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
  673. common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
  674. else
  675. common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
  676. }
  677. ret = icd->ops->set_bus_param(icd, common_flags);
  678. if (ret < 0)
  679. return ret;
  680. if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
  681. csicr1 |= CSICR1_REDGE;
  682. if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
  683. csicr1 |= CSICR1_INV_PCLK;
  684. if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
  685. csicr1 |= CSICR1_SOF_POL;
  686. if (common_flags & SOCAM_HSYNC_ACTIVE_HIGH)
  687. csicr1 |= CSICR1_HSYNC_POL;
  688. if (pcdev->platform_flags & MX2_CAMERA_SWAP16)
  689. csicr1 |= CSICR1_SWAP16_EN;
  690. if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
  691. csicr1 |= CSICR1_EXT_VSYNC;
  692. if (pcdev->platform_flags & MX2_CAMERA_CCIR)
  693. csicr1 |= CSICR1_CCIR_EN;
  694. if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
  695. csicr1 |= CSICR1_CCIR_MODE;
  696. if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
  697. csicr1 |= CSICR1_GCLK_MODE;
  698. if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
  699. csicr1 |= CSICR1_INV_DATA;
  700. if (pcdev->platform_flags & MX2_CAMERA_PACK_DIR_MSB)
  701. csicr1 |= CSICR1_PACK_DIR;
  702. pcdev->csicr1 = csicr1;
  703. bytesperline = soc_mbus_bytes_per_line(icd->user_width,
  704. icd->current_fmt->host_fmt);
  705. if (bytesperline < 0)
  706. return bytesperline;
  707. if (mx27_camera_emma(pcdev)) {
  708. ret = mx27_camera_emma_prp_reset(pcdev);
  709. if (ret)
  710. return ret;
  711. if (pcdev->discard_buffer)
  712. dma_free_coherent(ici->v4l2_dev.dev,
  713. pcdev->discard_size, pcdev->discard_buffer,
  714. pcdev->discard_buffer_dma);
  715. /*
  716. * I didn't manage to properly enable/disable the prp
  717. * on a per frame basis during running transfers,
  718. * thus we allocate a buffer here and use it to
  719. * discard frames when no buffer is available.
  720. * Feel free to work on this ;)
  721. */
  722. pcdev->discard_size = icd->user_height * bytesperline;
  723. pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
  724. pcdev->discard_size, &pcdev->discard_buffer_dma,
  725. GFP_KERNEL);
  726. if (!pcdev->discard_buffer)
  727. return -ENOMEM;
  728. mx27_camera_emma_buf_init(icd, bytesperline);
  729. } else if (cpu_is_mx25()) {
  730. writel((bytesperline * icd->user_height) >> 2,
  731. pcdev->base_csi + CSIRXCNT);
  732. writel((bytesperline << 16) | icd->user_height,
  733. pcdev->base_csi + CSIIMAG_PARA);
  734. }
  735. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  736. return 0;
  737. }
  738. static int mx2_camera_set_crop(struct soc_camera_device *icd,
  739. struct v4l2_crop *a)
  740. {
  741. struct v4l2_rect *rect = &a->c;
  742. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  743. struct v4l2_mbus_framefmt mf;
  744. int ret;
  745. soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
  746. soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
  747. ret = v4l2_subdev_call(sd, video, s_crop, a);
  748. if (ret < 0)
  749. return ret;
  750. /* The capture device might have changed its output */
  751. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  752. if (ret < 0)
  753. return ret;
  754. dev_dbg(icd->dev.parent, "Sensor cropped %dx%d\n",
  755. mf.width, mf.height);
  756. icd->user_width = mf.width;
  757. icd->user_height = mf.height;
  758. return ret;
  759. }
  760. static int mx2_camera_set_fmt(struct soc_camera_device *icd,
  761. struct v4l2_format *f)
  762. {
  763. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  764. const struct soc_camera_format_xlate *xlate;
  765. struct v4l2_pix_format *pix = &f->fmt.pix;
  766. struct v4l2_mbus_framefmt mf;
  767. int ret;
  768. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  769. if (!xlate) {
  770. dev_warn(icd->dev.parent, "Format %x not found\n",
  771. pix->pixelformat);
  772. return -EINVAL;
  773. }
  774. mf.width = pix->width;
  775. mf.height = pix->height;
  776. mf.field = pix->field;
  777. mf.colorspace = pix->colorspace;
  778. mf.code = xlate->code;
  779. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
  780. if (ret < 0 && ret != -ENOIOCTLCMD)
  781. return ret;
  782. if (mf.code != xlate->code)
  783. return -EINVAL;
  784. pix->width = mf.width;
  785. pix->height = mf.height;
  786. pix->field = mf.field;
  787. pix->colorspace = mf.colorspace;
  788. icd->current_fmt = xlate;
  789. return 0;
  790. }
  791. static int mx2_camera_try_fmt(struct soc_camera_device *icd,
  792. struct v4l2_format *f)
  793. {
  794. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  795. const struct soc_camera_format_xlate *xlate;
  796. struct v4l2_pix_format *pix = &f->fmt.pix;
  797. struct v4l2_mbus_framefmt mf;
  798. __u32 pixfmt = pix->pixelformat;
  799. unsigned int width_limit;
  800. int ret;
  801. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  802. if (pixfmt && !xlate) {
  803. dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
  804. return -EINVAL;
  805. }
  806. /* FIXME: implement MX27 limits */
  807. /* limit to MX25 hardware capabilities */
  808. if (cpu_is_mx25()) {
  809. if (xlate->host_fmt->bits_per_sample <= 8)
  810. width_limit = 0xffff * 4;
  811. else
  812. width_limit = 0xffff * 2;
  813. /* CSIIMAG_PARA limit */
  814. if (pix->width > width_limit)
  815. pix->width = width_limit;
  816. if (pix->height > 0xffff)
  817. pix->height = 0xffff;
  818. pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
  819. xlate->host_fmt);
  820. if (pix->bytesperline < 0)
  821. return pix->bytesperline;
  822. pix->sizeimage = pix->height * pix->bytesperline;
  823. if (pix->sizeimage > (4 * 0x3ffff)) { /* CSIRXCNT limit */
  824. dev_warn(icd->dev.parent,
  825. "Image size (%u) above limit\n",
  826. pix->sizeimage);
  827. return -EINVAL;
  828. }
  829. }
  830. /* limit to sensor capabilities */
  831. mf.width = pix->width;
  832. mf.height = pix->height;
  833. mf.field = pix->field;
  834. mf.colorspace = pix->colorspace;
  835. mf.code = xlate->code;
  836. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  837. if (ret < 0)
  838. return ret;
  839. if (mf.field == V4L2_FIELD_ANY)
  840. mf.field = V4L2_FIELD_NONE;
  841. if (mf.field != V4L2_FIELD_NONE) {
  842. dev_err(icd->dev.parent, "Field type %d unsupported.\n",
  843. mf.field);
  844. return -EINVAL;
  845. }
  846. pix->width = mf.width;
  847. pix->height = mf.height;
  848. pix->field = mf.field;
  849. pix->colorspace = mf.colorspace;
  850. return 0;
  851. }
  852. static int mx2_camera_querycap(struct soc_camera_host *ici,
  853. struct v4l2_capability *cap)
  854. {
  855. /* cap->name is set by the friendly caller:-> */
  856. strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
  857. cap->version = MX2_CAM_VERSION_CODE;
  858. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  859. return 0;
  860. }
  861. static int mx2_camera_reqbufs(struct soc_camera_device *icd,
  862. struct v4l2_requestbuffers *p)
  863. {
  864. int i;
  865. for (i = 0; i < p->count; i++) {
  866. struct mx2_buffer *buf = container_of(icd->vb_vidq.bufs[i],
  867. struct mx2_buffer, vb);
  868. INIT_LIST_HEAD(&buf->vb.queue);
  869. }
  870. return 0;
  871. }
  872. #ifdef CONFIG_MACH_MX27
  873. static void mx27_camera_frame_done(struct mx2_camera_dev *pcdev, int state)
  874. {
  875. struct videobuf_buffer *vb;
  876. struct mx2_buffer *buf;
  877. unsigned long flags;
  878. int ret;
  879. spin_lock_irqsave(&pcdev->lock, flags);
  880. if (!pcdev->active) {
  881. dev_err(pcdev->dev, "%s called with no active buffer!\n",
  882. __func__);
  883. goto out;
  884. }
  885. vb = &pcdev->active->vb;
  886. buf = container_of(vb, struct mx2_buffer, vb);
  887. WARN_ON(list_empty(&vb->queue));
  888. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  889. vb, vb->baddr, vb->bsize);
  890. /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
  891. list_del_init(&vb->queue);
  892. vb->state = state;
  893. do_gettimeofday(&vb->ts);
  894. vb->field_count++;
  895. wake_up(&vb->done);
  896. if (list_empty(&pcdev->capture)) {
  897. pcdev->active = NULL;
  898. goto out;
  899. }
  900. pcdev->active = list_entry(pcdev->capture.next,
  901. struct mx2_buffer, vb.queue);
  902. vb = &pcdev->active->vb;
  903. vb->state = VIDEOBUF_ACTIVE;
  904. ret = imx_dma_setup_single(pcdev->dma, videobuf_to_dma_contig(vb),
  905. vb->size, (u32)pcdev->base_dma + 0x10, DMA_MODE_READ);
  906. if (ret) {
  907. vb->state = VIDEOBUF_ERROR;
  908. pcdev->active = NULL;
  909. wake_up(&vb->done);
  910. }
  911. out:
  912. spin_unlock_irqrestore(&pcdev->lock, flags);
  913. }
  914. static void mx27_camera_dma_err_callback(int channel, void *data, int err)
  915. {
  916. struct mx2_camera_dev *pcdev = data;
  917. mx27_camera_frame_done(pcdev, VIDEOBUF_ERROR);
  918. }
  919. static void mx27_camera_dma_callback(int channel, void *data)
  920. {
  921. struct mx2_camera_dev *pcdev = data;
  922. mx27_camera_frame_done(pcdev, VIDEOBUF_DONE);
  923. }
  924. #define DMA_REQ_CSI_RX 31 /* FIXME: Add this to a resource */
  925. static int __devinit mx27_camera_dma_init(struct platform_device *pdev,
  926. struct mx2_camera_dev *pcdev)
  927. {
  928. int err;
  929. pcdev->dma = imx_dma_request_by_prio("CSI RX DMA", DMA_PRIO_HIGH);
  930. if (pcdev->dma < 0) {
  931. dev_err(&pdev->dev, "%s failed to request DMA channel\n",
  932. __func__);
  933. return pcdev->dma;
  934. }
  935. err = imx_dma_setup_handlers(pcdev->dma, mx27_camera_dma_callback,
  936. mx27_camera_dma_err_callback, pcdev);
  937. if (err) {
  938. dev_err(&pdev->dev, "%s failed to set DMA callback\n",
  939. __func__);
  940. goto err_out;
  941. }
  942. err = imx_dma_config_channel(pcdev->dma,
  943. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_FIFO,
  944. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
  945. DMA_REQ_CSI_RX, 1);
  946. if (err) {
  947. dev_err(&pdev->dev, "%s failed to config DMA channel\n",
  948. __func__);
  949. goto err_out;
  950. }
  951. imx_dma_config_burstlen(pcdev->dma, 64);
  952. return 0;
  953. err_out:
  954. imx_dma_free(pcdev->dma);
  955. return err;
  956. }
  957. #endif /* CONFIG_MACH_MX27 */
  958. static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
  959. {
  960. struct soc_camera_device *icd = file->private_data;
  961. return videobuf_poll_stream(file, &icd->vb_vidq, pt);
  962. }
  963. static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
  964. .owner = THIS_MODULE,
  965. .add = mx2_camera_add_device,
  966. .remove = mx2_camera_remove_device,
  967. .set_fmt = mx2_camera_set_fmt,
  968. .set_crop = mx2_camera_set_crop,
  969. .try_fmt = mx2_camera_try_fmt,
  970. .init_videobuf = mx2_camera_init_videobuf,
  971. .reqbufs = mx2_camera_reqbufs,
  972. .poll = mx2_camera_poll,
  973. .querycap = mx2_camera_querycap,
  974. .set_bus_param = mx2_camera_set_bus_param,
  975. };
  976. static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
  977. int bufnum, int state)
  978. {
  979. struct mx2_buffer *buf;
  980. struct videobuf_buffer *vb;
  981. unsigned long phys;
  982. if (!list_empty(&pcdev->active_bufs)) {
  983. buf = list_entry(pcdev->active_bufs.next,
  984. struct mx2_buffer, vb.queue);
  985. BUG_ON(buf->bufnum != bufnum);
  986. vb = &buf->vb;
  987. #ifdef DEBUG
  988. phys = videobuf_to_dma_contig(vb);
  989. if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum)
  990. != phys) {
  991. dev_err(pcdev->dev, "%p != %p\n", phys,
  992. readl(pcdev->base_emma +
  993. PRP_DEST_RGB1_PTR +
  994. 4 * bufnum));
  995. }
  996. #endif
  997. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, vb,
  998. vb->baddr, vb->bsize);
  999. list_del(&vb->queue);
  1000. vb->state = state;
  1001. do_gettimeofday(&vb->ts);
  1002. vb->field_count++;
  1003. wake_up(&vb->done);
  1004. }
  1005. if (list_empty(&pcdev->capture)) {
  1006. writel(pcdev->discard_buffer_dma, pcdev->base_emma +
  1007. PRP_DEST_RGB1_PTR + 4 * bufnum);
  1008. return;
  1009. }
  1010. buf = list_entry(pcdev->capture.next,
  1011. struct mx2_buffer, vb.queue);
  1012. buf->bufnum = !bufnum;
  1013. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  1014. vb = &buf->vb;
  1015. vb->state = VIDEOBUF_ACTIVE;
  1016. phys = videobuf_to_dma_contig(vb);
  1017. writel(phys, pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum);
  1018. }
  1019. static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
  1020. {
  1021. struct mx2_camera_dev *pcdev = data;
  1022. unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
  1023. struct mx2_buffer *buf;
  1024. if (status & (1 << 7)) { /* overflow */
  1025. u32 cntl;
  1026. /*
  1027. * We only disable channel 1 here since this is the only
  1028. * enabled channel
  1029. *
  1030. * FIXME: the correct DMA overflow handling should be resetting
  1031. * the buffer, returning an error frame, and continuing with
  1032. * the next one.
  1033. */
  1034. cntl = readl(pcdev->base_emma + PRP_CNTL);
  1035. writel(cntl & ~PRP_CNTL_CH1EN, pcdev->base_emma + PRP_CNTL);
  1036. writel(cntl, pcdev->base_emma + PRP_CNTL);
  1037. }
  1038. if ((status & (3 << 5)) == (3 << 5)
  1039. && !list_empty(&pcdev->active_bufs)) {
  1040. /*
  1041. * Both buffers have triggered, process the one we're expecting
  1042. * to first
  1043. */
  1044. buf = list_entry(pcdev->active_bufs.next,
  1045. struct mx2_buffer, vb.queue);
  1046. mx27_camera_frame_done_emma(pcdev, buf->bufnum, VIDEOBUF_DONE);
  1047. status &= ~(1 << (6 - buf->bufnum)); /* mark processed */
  1048. }
  1049. if (status & (1 << 6))
  1050. mx27_camera_frame_done_emma(pcdev, 0, VIDEOBUF_DONE);
  1051. if (status & (1 << 5))
  1052. mx27_camera_frame_done_emma(pcdev, 1, VIDEOBUF_DONE);
  1053. writel(status, pcdev->base_emma + PRP_INTRSTATUS);
  1054. return IRQ_HANDLED;
  1055. }
  1056. static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev)
  1057. {
  1058. struct resource *res_emma = pcdev->res_emma;
  1059. int err = 0;
  1060. if (!request_mem_region(res_emma->start, resource_size(res_emma),
  1061. MX2_CAM_DRV_NAME)) {
  1062. err = -EBUSY;
  1063. goto out;
  1064. }
  1065. pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma));
  1066. if (!pcdev->base_emma) {
  1067. err = -ENOMEM;
  1068. goto exit_release;
  1069. }
  1070. err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0,
  1071. MX2_CAM_DRV_NAME, pcdev);
  1072. if (err) {
  1073. dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n");
  1074. goto exit_iounmap;
  1075. }
  1076. pcdev->clk_emma = clk_get(NULL, "emma");
  1077. if (IS_ERR(pcdev->clk_emma)) {
  1078. err = PTR_ERR(pcdev->clk_emma);
  1079. goto exit_free_irq;
  1080. }
  1081. clk_enable(pcdev->clk_emma);
  1082. err = mx27_camera_emma_prp_reset(pcdev);
  1083. if (err)
  1084. goto exit_clk_emma_put;
  1085. return err;
  1086. exit_clk_emma_put:
  1087. clk_disable(pcdev->clk_emma);
  1088. clk_put(pcdev->clk_emma);
  1089. exit_free_irq:
  1090. free_irq(pcdev->irq_emma, pcdev);
  1091. exit_iounmap:
  1092. iounmap(pcdev->base_emma);
  1093. exit_release:
  1094. release_mem_region(res_emma->start, resource_size(res_emma));
  1095. out:
  1096. return err;
  1097. }
  1098. static int __devinit mx2_camera_probe(struct platform_device *pdev)
  1099. {
  1100. struct mx2_camera_dev *pcdev;
  1101. struct resource *res_csi, *res_emma;
  1102. void __iomem *base_csi;
  1103. int irq_csi, irq_emma;
  1104. irq_handler_t mx2_cam_irq_handler = cpu_is_mx25() ? mx25_camera_irq
  1105. : mx27_camera_irq;
  1106. int err = 0;
  1107. dev_dbg(&pdev->dev, "initialising\n");
  1108. res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1109. irq_csi = platform_get_irq(pdev, 0);
  1110. if (res_csi == NULL || irq_csi < 0) {
  1111. dev_err(&pdev->dev, "Missing platform resources data\n");
  1112. err = -ENODEV;
  1113. goto exit;
  1114. }
  1115. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1116. if (!pcdev) {
  1117. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1118. err = -ENOMEM;
  1119. goto exit;
  1120. }
  1121. pcdev->clk_csi = clk_get(&pdev->dev, NULL);
  1122. if (IS_ERR(pcdev->clk_csi)) {
  1123. err = PTR_ERR(pcdev->clk_csi);
  1124. goto exit_kfree;
  1125. }
  1126. dev_dbg(&pdev->dev, "Camera clock frequency: %ld\n",
  1127. clk_get_rate(pcdev->clk_csi));
  1128. /* Initialize DMA */
  1129. #ifdef CONFIG_MACH_MX27
  1130. if (cpu_is_mx27()) {
  1131. err = mx27_camera_dma_init(pdev, pcdev);
  1132. if (err)
  1133. goto exit_clk_put;
  1134. }
  1135. #endif /* CONFIG_MACH_MX27 */
  1136. pcdev->res_csi = res_csi;
  1137. pcdev->pdata = pdev->dev.platform_data;
  1138. if (pcdev->pdata) {
  1139. long rate;
  1140. pcdev->platform_flags = pcdev->pdata->flags;
  1141. rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2);
  1142. if (rate <= 0) {
  1143. err = -ENODEV;
  1144. goto exit_dma_free;
  1145. }
  1146. err = clk_set_rate(pcdev->clk_csi, rate);
  1147. if (err < 0)
  1148. goto exit_dma_free;
  1149. }
  1150. INIT_LIST_HEAD(&pcdev->capture);
  1151. INIT_LIST_HEAD(&pcdev->active_bufs);
  1152. spin_lock_init(&pcdev->lock);
  1153. /*
  1154. * Request the regions.
  1155. */
  1156. if (!request_mem_region(res_csi->start, resource_size(res_csi),
  1157. MX2_CAM_DRV_NAME)) {
  1158. err = -EBUSY;
  1159. goto exit_dma_free;
  1160. }
  1161. base_csi = ioremap(res_csi->start, resource_size(res_csi));
  1162. if (!base_csi) {
  1163. err = -ENOMEM;
  1164. goto exit_release;
  1165. }
  1166. pcdev->irq_csi = irq_csi;
  1167. pcdev->base_csi = base_csi;
  1168. pcdev->base_dma = res_csi->start;
  1169. pcdev->dev = &pdev->dev;
  1170. err = request_irq(pcdev->irq_csi, mx2_cam_irq_handler, 0,
  1171. MX2_CAM_DRV_NAME, pcdev);
  1172. if (err) {
  1173. dev_err(pcdev->dev, "Camera interrupt register failed \n");
  1174. goto exit_iounmap;
  1175. }
  1176. if (cpu_is_mx27()) {
  1177. /* EMMA support */
  1178. res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1179. irq_emma = platform_get_irq(pdev, 1);
  1180. if (res_emma && irq_emma >= 0) {
  1181. dev_info(&pdev->dev, "Using EMMA\n");
  1182. pcdev->use_emma = 1;
  1183. pcdev->res_emma = res_emma;
  1184. pcdev->irq_emma = irq_emma;
  1185. if (mx27_camera_emma_init(pcdev))
  1186. goto exit_free_irq;
  1187. }
  1188. }
  1189. pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
  1190. pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
  1191. pcdev->soc_host.priv = pcdev;
  1192. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1193. pcdev->soc_host.nr = pdev->id;
  1194. err = soc_camera_host_register(&pcdev->soc_host);
  1195. if (err)
  1196. goto exit_free_emma;
  1197. dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
  1198. clk_get_rate(pcdev->clk_csi));
  1199. return 0;
  1200. exit_free_emma:
  1201. if (mx27_camera_emma(pcdev)) {
  1202. free_irq(pcdev->irq_emma, pcdev);
  1203. clk_disable(pcdev->clk_emma);
  1204. clk_put(pcdev->clk_emma);
  1205. iounmap(pcdev->base_emma);
  1206. release_mem_region(res_emma->start, resource_size(res_emma));
  1207. }
  1208. exit_free_irq:
  1209. free_irq(pcdev->irq_csi, pcdev);
  1210. exit_iounmap:
  1211. iounmap(base_csi);
  1212. exit_release:
  1213. release_mem_region(res_csi->start, resource_size(res_csi));
  1214. exit_dma_free:
  1215. #ifdef CONFIG_MACH_MX27
  1216. if (cpu_is_mx27())
  1217. imx_dma_free(pcdev->dma);
  1218. exit_clk_put:
  1219. clk_put(pcdev->clk_csi);
  1220. #endif /* CONFIG_MACH_MX27 */
  1221. exit_kfree:
  1222. kfree(pcdev);
  1223. exit:
  1224. return err;
  1225. }
  1226. static int __devexit mx2_camera_remove(struct platform_device *pdev)
  1227. {
  1228. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1229. struct mx2_camera_dev *pcdev = container_of(soc_host,
  1230. struct mx2_camera_dev, soc_host);
  1231. struct resource *res;
  1232. clk_put(pcdev->clk_csi);
  1233. #ifdef CONFIG_MACH_MX27
  1234. if (cpu_is_mx27())
  1235. imx_dma_free(pcdev->dma);
  1236. #endif /* CONFIG_MACH_MX27 */
  1237. free_irq(pcdev->irq_csi, pcdev);
  1238. if (mx27_camera_emma(pcdev))
  1239. free_irq(pcdev->irq_emma, pcdev);
  1240. soc_camera_host_unregister(&pcdev->soc_host);
  1241. iounmap(pcdev->base_csi);
  1242. if (mx27_camera_emma(pcdev)) {
  1243. clk_disable(pcdev->clk_emma);
  1244. clk_put(pcdev->clk_emma);
  1245. iounmap(pcdev->base_emma);
  1246. res = pcdev->res_emma;
  1247. release_mem_region(res->start, resource_size(res));
  1248. }
  1249. res = pcdev->res_csi;
  1250. release_mem_region(res->start, resource_size(res));
  1251. kfree(pcdev);
  1252. dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
  1253. return 0;
  1254. }
  1255. static struct platform_driver mx2_camera_driver = {
  1256. .driver = {
  1257. .name = MX2_CAM_DRV_NAME,
  1258. },
  1259. .remove = __devexit_p(mx2_camera_remove),
  1260. };
  1261. static int __init mx2_camera_init(void)
  1262. {
  1263. return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe);
  1264. }
  1265. static void __exit mx2_camera_exit(void)
  1266. {
  1267. return platform_driver_unregister(&mx2_camera_driver);
  1268. }
  1269. module_init(mx2_camera_init);
  1270. module_exit(mx2_camera_exit);
  1271. MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver");
  1272. MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
  1273. MODULE_LICENSE("GPL");