mx2_camera.c 42 KB

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