mx2_camera.c 39 KB

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