mx2_camera.c 39 KB

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