mx2_camera.c 38 KB

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