mx2_camera.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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),
  575. icd, &icd->video_lock);
  576. }
  577. #define MX2_BUS_FLAGS (SOCAM_DATAWIDTH_8 | \
  578. SOCAM_MASTER | \
  579. SOCAM_VSYNC_ACTIVE_HIGH | \
  580. SOCAM_VSYNC_ACTIVE_LOW | \
  581. SOCAM_HSYNC_ACTIVE_HIGH | \
  582. SOCAM_HSYNC_ACTIVE_LOW | \
  583. SOCAM_PCLK_SAMPLE_RISING | \
  584. SOCAM_PCLK_SAMPLE_FALLING | \
  585. SOCAM_DATA_ACTIVE_HIGH | \
  586. SOCAM_DATA_ACTIVE_LOW)
  587. static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
  588. {
  589. u32 cntl;
  590. int count = 0;
  591. cntl = readl(pcdev->base_emma + PRP_CNTL);
  592. writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
  593. while (count++ < 100) {
  594. if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
  595. return 0;
  596. barrier();
  597. udelay(1);
  598. }
  599. return -ETIMEDOUT;
  600. }
  601. static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
  602. int bytesperline)
  603. {
  604. struct soc_camera_host *ici =
  605. to_soc_camera_host(icd->dev.parent);
  606. struct mx2_camera_dev *pcdev = ici->priv;
  607. writel(pcdev->discard_buffer_dma,
  608. pcdev->base_emma + PRP_DEST_RGB1_PTR);
  609. writel(pcdev->discard_buffer_dma,
  610. pcdev->base_emma + PRP_DEST_RGB2_PTR);
  611. /*
  612. * We only use the EMMA engine to get rid of the broken
  613. * DMA Engine. No color space consversion at the moment.
  614. * We set the incomming and outgoing pixelformat to an
  615. * 16 Bit wide format and adjust the bytesperline
  616. * accordingly. With this configuration the inputdata
  617. * will not be changed by the emma and could be any type
  618. * of 16 Bit Pixelformat.
  619. */
  620. writel(PRP_CNTL_CH1EN |
  621. PRP_CNTL_CSIEN |
  622. PRP_CNTL_DATA_IN_RGB16 |
  623. PRP_CNTL_CH1_OUT_RGB16 |
  624. PRP_CNTL_CH1_LEN |
  625. PRP_CNTL_CH1BYP |
  626. PRP_CNTL_CH1_TSKIP(0) |
  627. PRP_CNTL_IN_TSKIP(0),
  628. pcdev->base_emma + PRP_CNTL);
  629. writel(((bytesperline >> 1) << 16) | icd->user_height,
  630. pcdev->base_emma + PRP_SRC_FRAME_SIZE);
  631. writel(((bytesperline >> 1) << 16) | icd->user_height,
  632. pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
  633. writel(bytesperline,
  634. pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
  635. writel(0x2ca00565, /* RGB565 */
  636. pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
  637. writel(0x2ca00565, /* RGB565 */
  638. pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
  639. /* Enable interrupts */
  640. writel(PRP_INTR_RDERR |
  641. PRP_INTR_CH1WERR |
  642. PRP_INTR_CH2WERR |
  643. PRP_INTR_CH1FC |
  644. PRP_INTR_CH2FC |
  645. PRP_INTR_LBOVF |
  646. PRP_INTR_CH2OVF,
  647. pcdev->base_emma + PRP_INTR_CNTL);
  648. }
  649. static int mx2_camera_set_bus_param(struct soc_camera_device *icd,
  650. __u32 pixfmt)
  651. {
  652. struct soc_camera_host *ici =
  653. to_soc_camera_host(icd->dev.parent);
  654. struct mx2_camera_dev *pcdev = ici->priv;
  655. unsigned long camera_flags, common_flags;
  656. int ret = 0;
  657. int bytesperline;
  658. u32 csicr1 = pcdev->csicr1;
  659. camera_flags = icd->ops->query_bus_param(icd);
  660. common_flags = soc_camera_bus_param_compatible(camera_flags,
  661. MX2_BUS_FLAGS);
  662. if (!common_flags)
  663. return -EINVAL;
  664. if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
  665. (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
  666. if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
  667. common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
  668. else
  669. common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
  670. }
  671. if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
  672. (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
  673. if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
  674. common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
  675. else
  676. common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
  677. }
  678. ret = icd->ops->set_bus_param(icd, common_flags);
  679. if (ret < 0)
  680. return ret;
  681. if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
  682. csicr1 |= CSICR1_REDGE;
  683. if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
  684. csicr1 |= CSICR1_SOF_POL;
  685. if (common_flags & SOCAM_HSYNC_ACTIVE_HIGH)
  686. csicr1 |= CSICR1_HSYNC_POL;
  687. if (pcdev->platform_flags & MX2_CAMERA_SWAP16)
  688. csicr1 |= CSICR1_SWAP16_EN;
  689. if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
  690. csicr1 |= CSICR1_EXT_VSYNC;
  691. if (pcdev->platform_flags & MX2_CAMERA_CCIR)
  692. csicr1 |= CSICR1_CCIR_EN;
  693. if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
  694. csicr1 |= CSICR1_CCIR_MODE;
  695. if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
  696. csicr1 |= CSICR1_GCLK_MODE;
  697. if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
  698. csicr1 |= CSICR1_INV_DATA;
  699. if (pcdev->platform_flags & MX2_CAMERA_PACK_DIR_MSB)
  700. csicr1 |= CSICR1_PACK_DIR;
  701. pcdev->csicr1 = csicr1;
  702. bytesperline = soc_mbus_bytes_per_line(icd->user_width,
  703. icd->current_fmt->host_fmt);
  704. if (bytesperline < 0)
  705. return bytesperline;
  706. if (mx27_camera_emma(pcdev)) {
  707. ret = mx27_camera_emma_prp_reset(pcdev);
  708. if (ret)
  709. return ret;
  710. if (pcdev->discard_buffer)
  711. dma_free_coherent(ici->v4l2_dev.dev,
  712. pcdev->discard_size, pcdev->discard_buffer,
  713. pcdev->discard_buffer_dma);
  714. /*
  715. * I didn't manage to properly enable/disable the prp
  716. * on a per frame basis during running transfers,
  717. * thus we allocate a buffer here and use it to
  718. * discard frames when no buffer is available.
  719. * Feel free to work on this ;)
  720. */
  721. pcdev->discard_size = icd->user_height * bytesperline;
  722. pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
  723. pcdev->discard_size, &pcdev->discard_buffer_dma,
  724. GFP_KERNEL);
  725. if (!pcdev->discard_buffer)
  726. return -ENOMEM;
  727. mx27_camera_emma_buf_init(icd, bytesperline);
  728. } else if (cpu_is_mx25()) {
  729. writel((bytesperline * icd->user_height) >> 2,
  730. pcdev->base_csi + CSIRXCNT);
  731. writel((bytesperline << 16) | icd->user_height,
  732. pcdev->base_csi + CSIIMAG_PARA);
  733. }
  734. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  735. return 0;
  736. }
  737. static int mx2_camera_set_crop(struct soc_camera_device *icd,
  738. struct v4l2_crop *a)
  739. {
  740. struct v4l2_rect *rect = &a->c;
  741. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  742. struct v4l2_mbus_framefmt mf;
  743. int ret;
  744. soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
  745. soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
  746. ret = v4l2_subdev_call(sd, video, s_crop, a);
  747. if (ret < 0)
  748. return ret;
  749. /* The capture device might have changed its output */
  750. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  751. if (ret < 0)
  752. return ret;
  753. dev_dbg(icd->dev.parent, "Sensor cropped %dx%d\n",
  754. mf.width, mf.height);
  755. icd->user_width = mf.width;
  756. icd->user_height = mf.height;
  757. return ret;
  758. }
  759. static int mx2_camera_set_fmt(struct soc_camera_device *icd,
  760. struct v4l2_format *f)
  761. {
  762. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  763. const struct soc_camera_format_xlate *xlate;
  764. struct v4l2_pix_format *pix = &f->fmt.pix;
  765. struct v4l2_mbus_framefmt mf;
  766. int ret;
  767. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  768. if (!xlate) {
  769. dev_warn(icd->dev.parent, "Format %x not found\n",
  770. pix->pixelformat);
  771. return -EINVAL;
  772. }
  773. mf.width = pix->width;
  774. mf.height = pix->height;
  775. mf.field = pix->field;
  776. mf.colorspace = pix->colorspace;
  777. mf.code = xlate->code;
  778. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
  779. if (ret < 0 && ret != -ENOIOCTLCMD)
  780. return ret;
  781. if (mf.code != xlate->code)
  782. return -EINVAL;
  783. pix->width = mf.width;
  784. pix->height = mf.height;
  785. pix->field = mf.field;
  786. pix->colorspace = mf.colorspace;
  787. icd->current_fmt = xlate;
  788. return 0;
  789. }
  790. static int mx2_camera_try_fmt(struct soc_camera_device *icd,
  791. struct v4l2_format *f)
  792. {
  793. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  794. const struct soc_camera_format_xlate *xlate;
  795. struct v4l2_pix_format *pix = &f->fmt.pix;
  796. struct v4l2_mbus_framefmt mf;
  797. __u32 pixfmt = pix->pixelformat;
  798. unsigned int width_limit;
  799. int ret;
  800. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  801. if (pixfmt && !xlate) {
  802. dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
  803. return -EINVAL;
  804. }
  805. /* FIXME: implement MX27 limits */
  806. /* limit to MX25 hardware capabilities */
  807. if (cpu_is_mx25()) {
  808. if (xlate->host_fmt->bits_per_sample <= 8)
  809. width_limit = 0xffff * 4;
  810. else
  811. width_limit = 0xffff * 2;
  812. /* CSIIMAG_PARA limit */
  813. if (pix->width > width_limit)
  814. pix->width = width_limit;
  815. if (pix->height > 0xffff)
  816. pix->height = 0xffff;
  817. pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
  818. xlate->host_fmt);
  819. if (pix->bytesperline < 0)
  820. return pix->bytesperline;
  821. pix->sizeimage = pix->height * pix->bytesperline;
  822. if (pix->sizeimage > (4 * 0x3ffff)) { /* CSIRXCNT limit */
  823. dev_warn(icd->dev.parent,
  824. "Image size (%u) above limit\n",
  825. pix->sizeimage);
  826. return -EINVAL;
  827. }
  828. }
  829. /* limit to sensor capabilities */
  830. mf.width = pix->width;
  831. mf.height = pix->height;
  832. mf.field = pix->field;
  833. mf.colorspace = pix->colorspace;
  834. mf.code = xlate->code;
  835. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  836. if (ret < 0)
  837. return ret;
  838. if (mf.field == V4L2_FIELD_ANY)
  839. mf.field = V4L2_FIELD_NONE;
  840. if (mf.field != V4L2_FIELD_NONE) {
  841. dev_err(icd->dev.parent, "Field type %d unsupported.\n",
  842. mf.field);
  843. return -EINVAL;
  844. }
  845. pix->width = mf.width;
  846. pix->height = mf.height;
  847. pix->field = mf.field;
  848. pix->colorspace = mf.colorspace;
  849. return 0;
  850. }
  851. static int mx2_camera_querycap(struct soc_camera_host *ici,
  852. struct v4l2_capability *cap)
  853. {
  854. /* cap->name is set by the friendly caller:-> */
  855. strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
  856. cap->version = MX2_CAM_VERSION_CODE;
  857. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  858. return 0;
  859. }
  860. static int mx2_camera_reqbufs(struct soc_camera_device *icd,
  861. struct v4l2_requestbuffers *p)
  862. {
  863. int i;
  864. for (i = 0; i < p->count; i++) {
  865. struct mx2_buffer *buf = container_of(icd->vb_vidq.bufs[i],
  866. struct mx2_buffer, vb);
  867. INIT_LIST_HEAD(&buf->vb.queue);
  868. }
  869. return 0;
  870. }
  871. #ifdef CONFIG_MACH_MX27
  872. static void mx27_camera_frame_done(struct mx2_camera_dev *pcdev, int state)
  873. {
  874. struct videobuf_buffer *vb;
  875. struct mx2_buffer *buf;
  876. unsigned long flags;
  877. int ret;
  878. spin_lock_irqsave(&pcdev->lock, flags);
  879. if (!pcdev->active) {
  880. dev_err(pcdev->dev, "%s called with no active buffer!\n",
  881. __func__);
  882. goto out;
  883. }
  884. vb = &pcdev->active->vb;
  885. buf = container_of(vb, struct mx2_buffer, vb);
  886. WARN_ON(list_empty(&vb->queue));
  887. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  888. vb, vb->baddr, vb->bsize);
  889. /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
  890. list_del_init(&vb->queue);
  891. vb->state = state;
  892. do_gettimeofday(&vb->ts);
  893. vb->field_count++;
  894. wake_up(&vb->done);
  895. if (list_empty(&pcdev->capture)) {
  896. pcdev->active = NULL;
  897. goto out;
  898. }
  899. pcdev->active = list_entry(pcdev->capture.next,
  900. struct mx2_buffer, vb.queue);
  901. vb = &pcdev->active->vb;
  902. vb->state = VIDEOBUF_ACTIVE;
  903. ret = imx_dma_setup_single(pcdev->dma, videobuf_to_dma_contig(vb),
  904. vb->size, (u32)pcdev->base_dma + 0x10, DMA_MODE_READ);
  905. if (ret) {
  906. vb->state = VIDEOBUF_ERROR;
  907. pcdev->active = NULL;
  908. wake_up(&vb->done);
  909. }
  910. out:
  911. spin_unlock_irqrestore(&pcdev->lock, flags);
  912. }
  913. static void mx27_camera_dma_err_callback(int channel, void *data, int err)
  914. {
  915. struct mx2_camera_dev *pcdev = data;
  916. mx27_camera_frame_done(pcdev, VIDEOBUF_ERROR);
  917. }
  918. static void mx27_camera_dma_callback(int channel, void *data)
  919. {
  920. struct mx2_camera_dev *pcdev = data;
  921. mx27_camera_frame_done(pcdev, VIDEOBUF_DONE);
  922. }
  923. #define DMA_REQ_CSI_RX 31 /* FIXME: Add this to a resource */
  924. static int __devinit mx27_camera_dma_init(struct platform_device *pdev,
  925. struct mx2_camera_dev *pcdev)
  926. {
  927. int err;
  928. pcdev->dma = imx_dma_request_by_prio("CSI RX DMA", DMA_PRIO_HIGH);
  929. if (pcdev->dma < 0) {
  930. dev_err(&pdev->dev, "%s failed to request DMA channel\n",
  931. __func__);
  932. return pcdev->dma;
  933. }
  934. err = imx_dma_setup_handlers(pcdev->dma, mx27_camera_dma_callback,
  935. mx27_camera_dma_err_callback, pcdev);
  936. if (err) {
  937. dev_err(&pdev->dev, "%s failed to set DMA callback\n",
  938. __func__);
  939. goto err_out;
  940. }
  941. err = imx_dma_config_channel(pcdev->dma,
  942. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_FIFO,
  943. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
  944. DMA_REQ_CSI_RX, 1);
  945. if (err) {
  946. dev_err(&pdev->dev, "%s failed to config DMA channel\n",
  947. __func__);
  948. goto err_out;
  949. }
  950. imx_dma_config_burstlen(pcdev->dma, 64);
  951. return 0;
  952. err_out:
  953. imx_dma_free(pcdev->dma);
  954. return err;
  955. }
  956. #endif /* CONFIG_MACH_MX27 */
  957. static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
  958. {
  959. struct soc_camera_device *icd = file->private_data;
  960. return videobuf_poll_stream(file, &icd->vb_vidq, pt);
  961. }
  962. static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
  963. .owner = THIS_MODULE,
  964. .add = mx2_camera_add_device,
  965. .remove = mx2_camera_remove_device,
  966. .set_fmt = mx2_camera_set_fmt,
  967. .set_crop = mx2_camera_set_crop,
  968. .try_fmt = mx2_camera_try_fmt,
  969. .init_videobuf = mx2_camera_init_videobuf,
  970. .reqbufs = mx2_camera_reqbufs,
  971. .poll = mx2_camera_poll,
  972. .querycap = mx2_camera_querycap,
  973. .set_bus_param = mx2_camera_set_bus_param,
  974. };
  975. static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
  976. int bufnum, int state)
  977. {
  978. struct mx2_buffer *buf;
  979. struct videobuf_buffer *vb;
  980. unsigned long phys;
  981. if (!list_empty(&pcdev->active_bufs)) {
  982. buf = list_entry(pcdev->active_bufs.next,
  983. struct mx2_buffer, vb.queue);
  984. BUG_ON(buf->bufnum != bufnum);
  985. vb = &buf->vb;
  986. #ifdef DEBUG
  987. phys = videobuf_to_dma_contig(vb);
  988. if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum)
  989. != phys) {
  990. dev_err(pcdev->dev, "%p != %p\n", phys,
  991. readl(pcdev->base_emma +
  992. PRP_DEST_RGB1_PTR +
  993. 4 * bufnum));
  994. }
  995. #endif
  996. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__, vb,
  997. vb->baddr, vb->bsize);
  998. list_del(&vb->queue);
  999. vb->state = state;
  1000. do_gettimeofday(&vb->ts);
  1001. vb->field_count++;
  1002. wake_up(&vb->done);
  1003. }
  1004. if (list_empty(&pcdev->capture)) {
  1005. writel(pcdev->discard_buffer_dma, pcdev->base_emma +
  1006. PRP_DEST_RGB1_PTR + 4 * bufnum);
  1007. return;
  1008. }
  1009. buf = list_entry(pcdev->capture.next,
  1010. struct mx2_buffer, vb.queue);
  1011. buf->bufnum = !bufnum;
  1012. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  1013. vb = &buf->vb;
  1014. vb->state = VIDEOBUF_ACTIVE;
  1015. phys = videobuf_to_dma_contig(vb);
  1016. writel(phys, pcdev->base_emma + PRP_DEST_RGB1_PTR + 4 * bufnum);
  1017. }
  1018. static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
  1019. {
  1020. struct mx2_camera_dev *pcdev = data;
  1021. unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
  1022. struct mx2_buffer *buf;
  1023. if (status & (1 << 7)) { /* overflow */
  1024. u32 cntl;
  1025. /*
  1026. * We only disable channel 1 here since this is the only
  1027. * enabled channel
  1028. *
  1029. * FIXME: the correct DMA overflow handling should be resetting
  1030. * the buffer, returning an error frame, and continuing with
  1031. * the next one.
  1032. */
  1033. cntl = readl(pcdev->base_emma + PRP_CNTL);
  1034. writel(cntl & ~PRP_CNTL_CH1EN, pcdev->base_emma + PRP_CNTL);
  1035. writel(cntl, pcdev->base_emma + PRP_CNTL);
  1036. }
  1037. if ((status & (3 << 5)) == (3 << 5)
  1038. && !list_empty(&pcdev->active_bufs)) {
  1039. /*
  1040. * Both buffers have triggered, process the one we're expecting
  1041. * to first
  1042. */
  1043. buf = list_entry(pcdev->active_bufs.next,
  1044. struct mx2_buffer, vb.queue);
  1045. mx27_camera_frame_done_emma(pcdev, buf->bufnum, VIDEOBUF_DONE);
  1046. status &= ~(1 << (6 - buf->bufnum)); /* mark processed */
  1047. }
  1048. if (status & (1 << 6))
  1049. mx27_camera_frame_done_emma(pcdev, 0, VIDEOBUF_DONE);
  1050. if (status & (1 << 5))
  1051. mx27_camera_frame_done_emma(pcdev, 1, VIDEOBUF_DONE);
  1052. writel(status, pcdev->base_emma + PRP_INTRSTATUS);
  1053. return IRQ_HANDLED;
  1054. }
  1055. static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev)
  1056. {
  1057. struct resource *res_emma = pcdev->res_emma;
  1058. int err = 0;
  1059. if (!request_mem_region(res_emma->start, resource_size(res_emma),
  1060. MX2_CAM_DRV_NAME)) {
  1061. err = -EBUSY;
  1062. goto out;
  1063. }
  1064. pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma));
  1065. if (!pcdev->base_emma) {
  1066. err = -ENOMEM;
  1067. goto exit_release;
  1068. }
  1069. err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0,
  1070. MX2_CAM_DRV_NAME, pcdev);
  1071. if (err) {
  1072. dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n");
  1073. goto exit_iounmap;
  1074. }
  1075. pcdev->clk_emma = clk_get(NULL, "emma");
  1076. if (IS_ERR(pcdev->clk_emma)) {
  1077. err = PTR_ERR(pcdev->clk_emma);
  1078. goto exit_free_irq;
  1079. }
  1080. clk_enable(pcdev->clk_emma);
  1081. err = mx27_camera_emma_prp_reset(pcdev);
  1082. if (err)
  1083. goto exit_clk_emma_put;
  1084. return err;
  1085. exit_clk_emma_put:
  1086. clk_disable(pcdev->clk_emma);
  1087. clk_put(pcdev->clk_emma);
  1088. exit_free_irq:
  1089. free_irq(pcdev->irq_emma, pcdev);
  1090. exit_iounmap:
  1091. iounmap(pcdev->base_emma);
  1092. exit_release:
  1093. release_mem_region(res_emma->start, resource_size(res_emma));
  1094. out:
  1095. return err;
  1096. }
  1097. static int __devinit mx2_camera_probe(struct platform_device *pdev)
  1098. {
  1099. struct mx2_camera_dev *pcdev;
  1100. struct resource *res_csi, *res_emma;
  1101. void __iomem *base_csi;
  1102. int irq_csi, irq_emma;
  1103. irq_handler_t mx2_cam_irq_handler = cpu_is_mx25() ? mx25_camera_irq
  1104. : mx27_camera_irq;
  1105. int err = 0;
  1106. dev_dbg(&pdev->dev, "initialising\n");
  1107. res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1108. irq_csi = platform_get_irq(pdev, 0);
  1109. if (res_csi == NULL || irq_csi < 0) {
  1110. dev_err(&pdev->dev, "Missing platform resources data\n");
  1111. err = -ENODEV;
  1112. goto exit;
  1113. }
  1114. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1115. if (!pcdev) {
  1116. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1117. err = -ENOMEM;
  1118. goto exit;
  1119. }
  1120. pcdev->clk_csi = clk_get(&pdev->dev, NULL);
  1121. if (IS_ERR(pcdev->clk_csi)) {
  1122. err = PTR_ERR(pcdev->clk_csi);
  1123. goto exit_kfree;
  1124. }
  1125. dev_dbg(&pdev->dev, "Camera clock frequency: %ld\n",
  1126. clk_get_rate(pcdev->clk_csi));
  1127. /* Initialize DMA */
  1128. #ifdef CONFIG_MACH_MX27
  1129. if (cpu_is_mx27()) {
  1130. err = mx27_camera_dma_init(pdev, pcdev);
  1131. if (err)
  1132. goto exit_clk_put;
  1133. }
  1134. #endif /* CONFIG_MACH_MX27 */
  1135. pcdev->res_csi = res_csi;
  1136. pcdev->pdata = pdev->dev.platform_data;
  1137. if (pcdev->pdata) {
  1138. long rate;
  1139. pcdev->platform_flags = pcdev->pdata->flags;
  1140. rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2);
  1141. if (rate <= 0) {
  1142. err = -ENODEV;
  1143. goto exit_dma_free;
  1144. }
  1145. err = clk_set_rate(pcdev->clk_csi, rate);
  1146. if (err < 0)
  1147. goto exit_dma_free;
  1148. }
  1149. INIT_LIST_HEAD(&pcdev->capture);
  1150. INIT_LIST_HEAD(&pcdev->active_bufs);
  1151. spin_lock_init(&pcdev->lock);
  1152. /*
  1153. * Request the regions.
  1154. */
  1155. if (!request_mem_region(res_csi->start, resource_size(res_csi),
  1156. MX2_CAM_DRV_NAME)) {
  1157. err = -EBUSY;
  1158. goto exit_dma_free;
  1159. }
  1160. base_csi = ioremap(res_csi->start, resource_size(res_csi));
  1161. if (!base_csi) {
  1162. err = -ENOMEM;
  1163. goto exit_release;
  1164. }
  1165. pcdev->irq_csi = irq_csi;
  1166. pcdev->base_csi = base_csi;
  1167. pcdev->base_dma = res_csi->start;
  1168. pcdev->dev = &pdev->dev;
  1169. err = request_irq(pcdev->irq_csi, mx2_cam_irq_handler, 0,
  1170. MX2_CAM_DRV_NAME, pcdev);
  1171. if (err) {
  1172. dev_err(pcdev->dev, "Camera interrupt register failed \n");
  1173. goto exit_iounmap;
  1174. }
  1175. if (cpu_is_mx27()) {
  1176. /* EMMA support */
  1177. res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1178. irq_emma = platform_get_irq(pdev, 1);
  1179. if (res_emma && irq_emma >= 0) {
  1180. dev_info(&pdev->dev, "Using EMMA\n");
  1181. pcdev->use_emma = 1;
  1182. pcdev->res_emma = res_emma;
  1183. pcdev->irq_emma = irq_emma;
  1184. if (mx27_camera_emma_init(pcdev))
  1185. goto exit_free_irq;
  1186. }
  1187. }
  1188. pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
  1189. pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
  1190. pcdev->soc_host.priv = pcdev;
  1191. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1192. pcdev->soc_host.nr = pdev->id;
  1193. err = soc_camera_host_register(&pcdev->soc_host);
  1194. if (err)
  1195. goto exit_free_emma;
  1196. dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
  1197. clk_get_rate(pcdev->clk_csi));
  1198. return 0;
  1199. exit_free_emma:
  1200. if (mx27_camera_emma(pcdev)) {
  1201. free_irq(pcdev->irq_emma, pcdev);
  1202. clk_disable(pcdev->clk_emma);
  1203. clk_put(pcdev->clk_emma);
  1204. iounmap(pcdev->base_emma);
  1205. release_mem_region(res_emma->start, resource_size(res_emma));
  1206. }
  1207. exit_free_irq:
  1208. free_irq(pcdev->irq_csi, pcdev);
  1209. exit_iounmap:
  1210. iounmap(base_csi);
  1211. exit_release:
  1212. release_mem_region(res_csi->start, resource_size(res_csi));
  1213. exit_dma_free:
  1214. #ifdef CONFIG_MACH_MX27
  1215. if (cpu_is_mx27())
  1216. imx_dma_free(pcdev->dma);
  1217. exit_clk_put:
  1218. clk_put(pcdev->clk_csi);
  1219. #endif /* CONFIG_MACH_MX27 */
  1220. exit_kfree:
  1221. kfree(pcdev);
  1222. exit:
  1223. return err;
  1224. }
  1225. static int __devexit mx2_camera_remove(struct platform_device *pdev)
  1226. {
  1227. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1228. struct mx2_camera_dev *pcdev = container_of(soc_host,
  1229. struct mx2_camera_dev, soc_host);
  1230. struct resource *res;
  1231. clk_put(pcdev->clk_csi);
  1232. #ifdef CONFIG_MACH_MX27
  1233. if (cpu_is_mx27())
  1234. imx_dma_free(pcdev->dma);
  1235. #endif /* CONFIG_MACH_MX27 */
  1236. free_irq(pcdev->irq_csi, pcdev);
  1237. if (mx27_camera_emma(pcdev))
  1238. free_irq(pcdev->irq_emma, pcdev);
  1239. soc_camera_host_unregister(&pcdev->soc_host);
  1240. iounmap(pcdev->base_csi);
  1241. if (mx27_camera_emma(pcdev)) {
  1242. clk_disable(pcdev->clk_emma);
  1243. clk_put(pcdev->clk_emma);
  1244. iounmap(pcdev->base_emma);
  1245. res = pcdev->res_emma;
  1246. release_mem_region(res->start, resource_size(res));
  1247. }
  1248. res = pcdev->res_csi;
  1249. release_mem_region(res->start, resource_size(res));
  1250. kfree(pcdev);
  1251. dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
  1252. return 0;
  1253. }
  1254. static struct platform_driver mx2_camera_driver = {
  1255. .driver = {
  1256. .name = MX2_CAM_DRV_NAME,
  1257. },
  1258. .remove = __devexit_p(mx2_camera_remove),
  1259. };
  1260. static int __init mx2_camera_init(void)
  1261. {
  1262. return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe);
  1263. }
  1264. static void __exit mx2_camera_exit(void)
  1265. {
  1266. return platform_driver_unregister(&mx2_camera_driver);
  1267. }
  1268. module_init(mx2_camera_init);
  1269. module_exit(mx2_camera_exit);
  1270. MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver");
  1271. MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
  1272. MODULE_LICENSE("GPL");