mx2_camera.c 41 KB

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