mx2_camera.c 48 KB

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