mx2_camera.c 40 KB

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