mx2_camera.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  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. .in_fmt = V4L2_MBUS_FMT_UYVY8_2X8,
  313. .out_fmt = V4L2_PIX_FMT_YUV420,
  314. .cfg = {
  315. .channel = 2,
  316. .in_fmt = PRP_CNTL_DATA_IN_YUV422,
  317. .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
  318. .src_pixel = 0x22000888, /* YUV422 (YUYV) */
  319. .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
  320. PRP_INTR_CH2FC | PRP_INTR_LBOVF |
  321. PRP_INTR_CH2OVF,
  322. }
  323. },
  324. };
  325. static struct mx2_fmt_cfg *mx27_emma_prp_get_format(
  326. enum v4l2_mbus_pixelcode in_fmt,
  327. u32 out_fmt)
  328. {
  329. int i;
  330. for (i = 1; i < ARRAY_SIZE(mx27_emma_prp_table); i++)
  331. if ((mx27_emma_prp_table[i].in_fmt == in_fmt) &&
  332. (mx27_emma_prp_table[i].out_fmt == out_fmt)) {
  333. return &mx27_emma_prp_table[i];
  334. }
  335. /* If no match return the most generic configuration */
  336. return &mx27_emma_prp_table[0];
  337. };
  338. static void mx27_update_emma_buf(struct mx2_camera_dev *pcdev,
  339. unsigned long phys, int bufnum)
  340. {
  341. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  342. if (prp->cfg.channel == 1) {
  343. writel(phys, pcdev->base_emma +
  344. PRP_DEST_RGB1_PTR + 4 * bufnum);
  345. } else {
  346. writel(phys, pcdev->base_emma +
  347. PRP_DEST_Y_PTR - 0x14 * bufnum);
  348. if (prp->out_fmt == V4L2_PIX_FMT_YUV420) {
  349. u32 imgsize = pcdev->icd->user_height *
  350. pcdev->icd->user_width;
  351. writel(phys + imgsize, pcdev->base_emma +
  352. PRP_DEST_CB_PTR - 0x14 * bufnum);
  353. writel(phys + ((5 * imgsize) / 4), pcdev->base_emma +
  354. PRP_DEST_CR_PTR - 0x14 * bufnum);
  355. }
  356. }
  357. }
  358. static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev)
  359. {
  360. unsigned long flags;
  361. clk_disable(pcdev->clk_csi);
  362. writel(0, pcdev->base_csi + CSICR1);
  363. if (cpu_is_mx27()) {
  364. writel(0, pcdev->base_emma + PRP_CNTL);
  365. } else if (cpu_is_mx25()) {
  366. spin_lock_irqsave(&pcdev->lock, flags);
  367. pcdev->fb1_active = NULL;
  368. pcdev->fb2_active = NULL;
  369. writel(0, pcdev->base_csi + CSIDMASA_FB1);
  370. writel(0, pcdev->base_csi + CSIDMASA_FB2);
  371. spin_unlock_irqrestore(&pcdev->lock, flags);
  372. }
  373. }
  374. /*
  375. * The following two functions absolutely depend on the fact, that
  376. * there can be only one camera on mx2 camera sensor interface
  377. */
  378. static int mx2_camera_add_device(struct soc_camera_device *icd)
  379. {
  380. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  381. struct mx2_camera_dev *pcdev = ici->priv;
  382. int ret;
  383. u32 csicr1;
  384. if (pcdev->icd)
  385. return -EBUSY;
  386. ret = clk_enable(pcdev->clk_csi);
  387. if (ret < 0)
  388. return ret;
  389. csicr1 = CSICR1_MCLKEN;
  390. if (cpu_is_mx27()) {
  391. csicr1 |= CSICR1_PRP_IF_EN | CSICR1_FCC |
  392. CSICR1_RXFF_LEVEL(0);
  393. } else if (cpu_is_mx27())
  394. csicr1 |= CSICR1_SOF_INTEN | CSICR1_RXFF_LEVEL(2);
  395. pcdev->csicr1 = csicr1;
  396. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  397. pcdev->icd = icd;
  398. pcdev->frame_count = 0;
  399. dev_info(icd->parent, "Camera driver attached to camera %d\n",
  400. icd->devnum);
  401. return 0;
  402. }
  403. static void mx2_camera_remove_device(struct soc_camera_device *icd)
  404. {
  405. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  406. struct mx2_camera_dev *pcdev = ici->priv;
  407. BUG_ON(icd != pcdev->icd);
  408. dev_info(icd->parent, "Camera driver detached from camera %d\n",
  409. icd->devnum);
  410. mx2_camera_deactivate(pcdev);
  411. pcdev->icd = NULL;
  412. }
  413. static void mx25_camera_frame_done(struct mx2_camera_dev *pcdev, int fb,
  414. int state)
  415. {
  416. struct vb2_buffer *vb;
  417. struct mx2_buffer *buf;
  418. struct mx2_buffer **fb_active = fb == 1 ? &pcdev->fb1_active :
  419. &pcdev->fb2_active;
  420. u32 fb_reg = fb == 1 ? CSIDMASA_FB1 : CSIDMASA_FB2;
  421. unsigned long flags;
  422. spin_lock_irqsave(&pcdev->lock, flags);
  423. if (*fb_active == NULL)
  424. goto out;
  425. vb = &(*fb_active)->vb;
  426. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  427. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  428. do_gettimeofday(&vb->v4l2_buf.timestamp);
  429. vb->v4l2_buf.sequence++;
  430. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  431. if (list_empty(&pcdev->capture)) {
  432. buf = NULL;
  433. writel(0, pcdev->base_csi + fb_reg);
  434. } else {
  435. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  436. internal.queue);
  437. vb = &buf->vb;
  438. list_del(&buf->internal.queue);
  439. buf->state = MX2_STATE_ACTIVE;
  440. writel(vb2_dma_contig_plane_dma_addr(vb, 0),
  441. pcdev->base_csi + fb_reg);
  442. }
  443. *fb_active = buf;
  444. out:
  445. spin_unlock_irqrestore(&pcdev->lock, flags);
  446. }
  447. static irqreturn_t mx25_camera_irq(int irq_csi, void *data)
  448. {
  449. struct mx2_camera_dev *pcdev = data;
  450. u32 status = readl(pcdev->base_csi + CSISR);
  451. if (status & CSISR_DMA_TSF_FB1_INT)
  452. mx25_camera_frame_done(pcdev, 1, MX2_STATE_DONE);
  453. else if (status & CSISR_DMA_TSF_FB2_INT)
  454. mx25_camera_frame_done(pcdev, 2, MX2_STATE_DONE);
  455. /* FIXME: handle CSISR_RFF_OR_INT */
  456. writel(status, pcdev->base_csi + CSISR);
  457. return IRQ_HANDLED;
  458. }
  459. /*
  460. * Videobuf operations
  461. */
  462. static int mx2_videobuf_setup(struct vb2_queue *vq,
  463. const struct v4l2_format *fmt,
  464. unsigned int *count, unsigned int *num_planes,
  465. unsigned int sizes[], void *alloc_ctxs[])
  466. {
  467. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  468. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  469. struct mx2_camera_dev *pcdev = ici->priv;
  470. dev_dbg(icd->parent, "count=%d, size=%d\n", *count, sizes[0]);
  471. /* TODO: support for VIDIOC_CREATE_BUFS not ready */
  472. if (fmt != NULL)
  473. return -ENOTTY;
  474. alloc_ctxs[0] = pcdev->alloc_ctx;
  475. sizes[0] = icd->sizeimage;
  476. if (0 == *count)
  477. *count = 32;
  478. if (!*num_planes &&
  479. sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024)
  480. *count = (MAX_VIDEO_MEM * 1024 * 1024) / sizes[0];
  481. *num_planes = 1;
  482. return 0;
  483. }
  484. static int mx2_videobuf_prepare(struct vb2_buffer *vb)
  485. {
  486. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  487. int ret = 0;
  488. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  489. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  490. #ifdef DEBUG
  491. /*
  492. * This can be useful if you want to see if we actually fill
  493. * the buffer with something
  494. */
  495. memset((void *)vb2_plane_vaddr(vb, 0),
  496. 0xaa, vb2_get_plane_payload(vb, 0));
  497. #endif
  498. vb2_set_plane_payload(vb, 0, icd->sizeimage);
  499. if (vb2_plane_vaddr(vb, 0) &&
  500. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  501. ret = -EINVAL;
  502. goto out;
  503. }
  504. return 0;
  505. out:
  506. return ret;
  507. }
  508. static void mx2_videobuf_queue(struct vb2_buffer *vb)
  509. {
  510. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  511. struct soc_camera_host *ici =
  512. to_soc_camera_host(icd->parent);
  513. struct mx2_camera_dev *pcdev = ici->priv;
  514. struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
  515. unsigned long flags;
  516. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  517. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  518. spin_lock_irqsave(&pcdev->lock, flags);
  519. buf->state = MX2_STATE_QUEUED;
  520. list_add_tail(&buf->internal.queue, &pcdev->capture);
  521. if (cpu_is_mx25()) {
  522. u32 csicr3, dma_inten = 0;
  523. if (pcdev->fb1_active == NULL) {
  524. writel(vb2_dma_contig_plane_dma_addr(vb, 0),
  525. pcdev->base_csi + CSIDMASA_FB1);
  526. pcdev->fb1_active = buf;
  527. dma_inten = CSICR1_FB1_DMA_INTEN;
  528. } else if (pcdev->fb2_active == NULL) {
  529. writel(vb2_dma_contig_plane_dma_addr(vb, 0),
  530. pcdev->base_csi + CSIDMASA_FB2);
  531. pcdev->fb2_active = buf;
  532. dma_inten = CSICR1_FB2_DMA_INTEN;
  533. }
  534. if (dma_inten) {
  535. list_del(&buf->internal.queue);
  536. buf->state = MX2_STATE_ACTIVE;
  537. csicr3 = readl(pcdev->base_csi + CSICR3);
  538. /* Reflash DMA */
  539. writel(csicr3 | CSICR3_DMA_REFLASH_RFF,
  540. pcdev->base_csi + CSICR3);
  541. /* clear & enable interrupts */
  542. writel(dma_inten, pcdev->base_csi + CSISR);
  543. pcdev->csicr1 |= dma_inten;
  544. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  545. /* enable DMA */
  546. csicr3 |= CSICR3_DMA_REQ_EN_RFF | CSICR3_RXFF_LEVEL(1);
  547. writel(csicr3, pcdev->base_csi + CSICR3);
  548. }
  549. }
  550. spin_unlock_irqrestore(&pcdev->lock, flags);
  551. }
  552. static void mx2_videobuf_release(struct vb2_buffer *vb)
  553. {
  554. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  555. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  556. struct mx2_camera_dev *pcdev = ici->priv;
  557. struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
  558. unsigned long flags;
  559. #ifdef DEBUG
  560. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  561. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  562. switch (buf->state) {
  563. case MX2_STATE_ACTIVE:
  564. dev_info(icd->parent, "%s (active)\n", __func__);
  565. break;
  566. case MX2_STATE_QUEUED:
  567. dev_info(icd->parent, "%s (queued)\n", __func__);
  568. break;
  569. default:
  570. dev_info(icd->parent, "%s (unknown) %d\n", __func__,
  571. buf->state);
  572. break;
  573. }
  574. #endif
  575. /*
  576. * Terminate only queued but inactive buffers. Active buffers are
  577. * released when they become inactive after videobuf_waiton().
  578. *
  579. * FIXME: implement forced termination of active buffers for mx27 and
  580. * mx27 eMMA, so that the user won't get stuck in an uninterruptible
  581. * state. This requires a specific handling for each of the these DMA
  582. * types.
  583. */
  584. spin_lock_irqsave(&pcdev->lock, flags);
  585. if (cpu_is_mx25() && buf->state == MX2_STATE_ACTIVE) {
  586. if (pcdev->fb1_active == buf) {
  587. pcdev->csicr1 &= ~CSICR1_FB1_DMA_INTEN;
  588. writel(0, pcdev->base_csi + CSIDMASA_FB1);
  589. pcdev->fb1_active = NULL;
  590. } else if (pcdev->fb2_active == buf) {
  591. pcdev->csicr1 &= ~CSICR1_FB2_DMA_INTEN;
  592. writel(0, pcdev->base_csi + CSIDMASA_FB2);
  593. pcdev->fb2_active = NULL;
  594. }
  595. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  596. }
  597. spin_unlock_irqrestore(&pcdev->lock, flags);
  598. }
  599. static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
  600. int bytesperline)
  601. {
  602. struct soc_camera_host *ici =
  603. to_soc_camera_host(icd->parent);
  604. struct mx2_camera_dev *pcdev = ici->priv;
  605. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  606. writel((pcdev->s_width << 16) | pcdev->s_height,
  607. pcdev->base_emma + PRP_SRC_FRAME_SIZE);
  608. writel(prp->cfg.src_pixel,
  609. pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
  610. if (prp->cfg.channel == 1) {
  611. writel((icd->user_width << 16) | icd->user_height,
  612. pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
  613. writel(bytesperline,
  614. pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
  615. writel(prp->cfg.ch1_pixel,
  616. pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
  617. } else { /* channel 2 */
  618. writel((icd->user_width << 16) | icd->user_height,
  619. pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
  620. }
  621. /* Enable interrupts */
  622. writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL);
  623. }
  624. static void mx2_prp_resize_commit(struct mx2_camera_dev *pcdev)
  625. {
  626. int dir;
  627. for (dir = RESIZE_DIR_H; dir <= RESIZE_DIR_V; dir++) {
  628. unsigned char *s = pcdev->resizing[dir].s;
  629. int len = pcdev->resizing[dir].len;
  630. unsigned int coeff[2] = {0, 0};
  631. unsigned int valid = 0;
  632. int i;
  633. if (len == 0)
  634. continue;
  635. for (i = RESIZE_NUM_MAX - 1; i >= 0; i--) {
  636. int j;
  637. j = i > 9 ? 1 : 0;
  638. coeff[j] = (coeff[j] << BC_COEF) |
  639. (s[i] & (SZ_COEF - 1));
  640. if (i == 5 || i == 15)
  641. coeff[j] <<= 1;
  642. valid = (valid << 1) | (s[i] >> BC_COEF);
  643. }
  644. valid |= PRP_RZ_VALID_TBL_LEN(len);
  645. if (pcdev->resizing[dir].algo == RESIZE_ALGO_BILINEAR)
  646. valid |= PRP_RZ_VALID_BILINEAR;
  647. if (pcdev->emma_prp->cfg.channel == 1) {
  648. if (dir == RESIZE_DIR_H) {
  649. writel(coeff[0], pcdev->base_emma +
  650. PRP_CH1_RZ_HORI_COEF1);
  651. writel(coeff[1], pcdev->base_emma +
  652. PRP_CH1_RZ_HORI_COEF2);
  653. writel(valid, pcdev->base_emma +
  654. PRP_CH1_RZ_HORI_VALID);
  655. } else {
  656. writel(coeff[0], pcdev->base_emma +
  657. PRP_CH1_RZ_VERT_COEF1);
  658. writel(coeff[1], pcdev->base_emma +
  659. PRP_CH1_RZ_VERT_COEF2);
  660. writel(valid, pcdev->base_emma +
  661. PRP_CH1_RZ_VERT_VALID);
  662. }
  663. } else {
  664. if (dir == RESIZE_DIR_H) {
  665. writel(coeff[0], pcdev->base_emma +
  666. PRP_CH2_RZ_HORI_COEF1);
  667. writel(coeff[1], pcdev->base_emma +
  668. PRP_CH2_RZ_HORI_COEF2);
  669. writel(valid, pcdev->base_emma +
  670. PRP_CH2_RZ_HORI_VALID);
  671. } else {
  672. writel(coeff[0], pcdev->base_emma +
  673. PRP_CH2_RZ_VERT_COEF1);
  674. writel(coeff[1], pcdev->base_emma +
  675. PRP_CH2_RZ_VERT_COEF2);
  676. writel(valid, pcdev->base_emma +
  677. PRP_CH2_RZ_VERT_VALID);
  678. }
  679. }
  680. }
  681. }
  682. static int mx2_start_streaming(struct vb2_queue *q, unsigned int count)
  683. {
  684. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  685. struct soc_camera_host *ici =
  686. to_soc_camera_host(icd->parent);
  687. struct mx2_camera_dev *pcdev = ici->priv;
  688. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  689. struct vb2_buffer *vb;
  690. struct mx2_buffer *buf;
  691. unsigned long phys;
  692. int bytesperline;
  693. if (cpu_is_mx27()) {
  694. unsigned long flags;
  695. if (count < 2)
  696. return -EINVAL;
  697. spin_lock_irqsave(&pcdev->lock, flags);
  698. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  699. internal.queue);
  700. buf->internal.bufnum = 0;
  701. vb = &buf->vb;
  702. buf->state = MX2_STATE_ACTIVE;
  703. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  704. mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
  705. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  706. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  707. internal.queue);
  708. buf->internal.bufnum = 1;
  709. vb = &buf->vb;
  710. buf->state = MX2_STATE_ACTIVE;
  711. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  712. mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
  713. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  714. bytesperline = soc_mbus_bytes_per_line(icd->user_width,
  715. icd->current_fmt->host_fmt);
  716. if (bytesperline < 0)
  717. return bytesperline;
  718. /*
  719. * I didn't manage to properly enable/disable the prp
  720. * on a per frame basis during running transfers,
  721. * thus we allocate a buffer here and use it to
  722. * discard frames when no buffer is available.
  723. * Feel free to work on this ;)
  724. */
  725. pcdev->discard_size = icd->user_height * bytesperline;
  726. pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
  727. pcdev->discard_size, &pcdev->discard_buffer_dma,
  728. GFP_KERNEL);
  729. if (!pcdev->discard_buffer)
  730. return -ENOMEM;
  731. pcdev->buf_discard[0].discard = true;
  732. list_add_tail(&pcdev->buf_discard[0].queue,
  733. &pcdev->discard);
  734. pcdev->buf_discard[1].discard = true;
  735. list_add_tail(&pcdev->buf_discard[1].queue,
  736. &pcdev->discard);
  737. mx2_prp_resize_commit(pcdev);
  738. mx27_camera_emma_buf_init(icd, bytesperline);
  739. if (prp->cfg.channel == 1) {
  740. writel(PRP_CNTL_CH1EN |
  741. PRP_CNTL_CSIEN |
  742. prp->cfg.in_fmt |
  743. prp->cfg.out_fmt |
  744. PRP_CNTL_CH1_LEN |
  745. PRP_CNTL_CH1BYP |
  746. PRP_CNTL_CH1_TSKIP(0) |
  747. PRP_CNTL_IN_TSKIP(0),
  748. pcdev->base_emma + PRP_CNTL);
  749. } else {
  750. writel(PRP_CNTL_CH2EN |
  751. PRP_CNTL_CSIEN |
  752. prp->cfg.in_fmt |
  753. prp->cfg.out_fmt |
  754. PRP_CNTL_CH2_LEN |
  755. PRP_CNTL_CH2_TSKIP(0) |
  756. PRP_CNTL_IN_TSKIP(0),
  757. pcdev->base_emma + PRP_CNTL);
  758. }
  759. spin_unlock_irqrestore(&pcdev->lock, flags);
  760. }
  761. return 0;
  762. }
  763. static int mx2_stop_streaming(struct vb2_queue *q)
  764. {
  765. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  766. struct soc_camera_host *ici =
  767. to_soc_camera_host(icd->parent);
  768. struct mx2_camera_dev *pcdev = ici->priv;
  769. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  770. unsigned long flags;
  771. void *b;
  772. u32 cntl;
  773. if (cpu_is_mx27()) {
  774. spin_lock_irqsave(&pcdev->lock, flags);
  775. cntl = readl(pcdev->base_emma + PRP_CNTL);
  776. if (prp->cfg.channel == 1) {
  777. writel(cntl & ~PRP_CNTL_CH1EN,
  778. pcdev->base_emma + PRP_CNTL);
  779. } else {
  780. writel(cntl & ~PRP_CNTL_CH2EN,
  781. pcdev->base_emma + PRP_CNTL);
  782. }
  783. INIT_LIST_HEAD(&pcdev->capture);
  784. INIT_LIST_HEAD(&pcdev->active_bufs);
  785. INIT_LIST_HEAD(&pcdev->discard);
  786. b = pcdev->discard_buffer;
  787. pcdev->discard_buffer = NULL;
  788. spin_unlock_irqrestore(&pcdev->lock, flags);
  789. dma_free_coherent(ici->v4l2_dev.dev,
  790. pcdev->discard_size, b, pcdev->discard_buffer_dma);
  791. }
  792. return 0;
  793. }
  794. static struct vb2_ops mx2_videobuf_ops = {
  795. .queue_setup = mx2_videobuf_setup,
  796. .buf_prepare = mx2_videobuf_prepare,
  797. .buf_queue = mx2_videobuf_queue,
  798. .buf_cleanup = mx2_videobuf_release,
  799. .start_streaming = mx2_start_streaming,
  800. .stop_streaming = mx2_stop_streaming,
  801. };
  802. static int mx2_camera_init_videobuf(struct vb2_queue *q,
  803. struct soc_camera_device *icd)
  804. {
  805. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  806. q->io_modes = VB2_MMAP | VB2_USERPTR;
  807. q->drv_priv = icd;
  808. q->ops = &mx2_videobuf_ops;
  809. q->mem_ops = &vb2_dma_contig_memops;
  810. q->buf_struct_size = sizeof(struct mx2_buffer);
  811. return vb2_queue_init(q);
  812. }
  813. #define MX2_BUS_FLAGS (V4L2_MBUS_MASTER | \
  814. V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
  815. V4L2_MBUS_VSYNC_ACTIVE_LOW | \
  816. V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
  817. V4L2_MBUS_HSYNC_ACTIVE_LOW | \
  818. V4L2_MBUS_PCLK_SAMPLE_RISING | \
  819. V4L2_MBUS_PCLK_SAMPLE_FALLING | \
  820. V4L2_MBUS_DATA_ACTIVE_HIGH | \
  821. V4L2_MBUS_DATA_ACTIVE_LOW)
  822. static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
  823. {
  824. u32 cntl;
  825. int count = 0;
  826. cntl = readl(pcdev->base_emma + PRP_CNTL);
  827. writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
  828. while (count++ < 100) {
  829. if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
  830. return 0;
  831. barrier();
  832. udelay(1);
  833. }
  834. return -ETIMEDOUT;
  835. }
  836. static int mx2_camera_set_bus_param(struct soc_camera_device *icd)
  837. {
  838. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  839. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  840. struct mx2_camera_dev *pcdev = ici->priv;
  841. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  842. const struct soc_camera_format_xlate *xlate;
  843. unsigned long common_flags;
  844. int ret;
  845. int bytesperline;
  846. u32 csicr1 = pcdev->csicr1;
  847. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  848. if (!ret) {
  849. common_flags = soc_mbus_config_compatible(&cfg, MX2_BUS_FLAGS);
  850. if (!common_flags) {
  851. dev_warn(icd->parent,
  852. "Flags incompatible: camera 0x%x, host 0x%x\n",
  853. cfg.flags, MX2_BUS_FLAGS);
  854. return -EINVAL;
  855. }
  856. } else if (ret != -ENOIOCTLCMD) {
  857. return ret;
  858. } else {
  859. common_flags = MX2_BUS_FLAGS;
  860. }
  861. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  862. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  863. if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
  864. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  865. else
  866. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  867. }
  868. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  869. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  870. if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
  871. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  872. else
  873. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  874. }
  875. cfg.flags = common_flags;
  876. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  877. if (ret < 0 && ret != -ENOIOCTLCMD) {
  878. dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
  879. common_flags, ret);
  880. return ret;
  881. }
  882. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  883. if (!xlate) {
  884. dev_warn(icd->parent, "Format %x not found\n", pixfmt);
  885. return -EINVAL;
  886. }
  887. if (xlate->code == V4L2_MBUS_FMT_YUYV8_2X8) {
  888. csicr1 |= CSICR1_PACK_DIR;
  889. csicr1 &= ~CSICR1_SWAP16_EN;
  890. dev_dbg(icd->parent, "already yuyv format, don't convert\n");
  891. } else if (xlate->code == V4L2_MBUS_FMT_UYVY8_2X8) {
  892. csicr1 &= ~CSICR1_PACK_DIR;
  893. csicr1 |= CSICR1_SWAP16_EN;
  894. dev_dbg(icd->parent, "convert uyvy mbus format into yuyv\n");
  895. } else {
  896. dev_warn(icd->parent, "mbus format not supported\n");
  897. return -EINVAL;
  898. }
  899. if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  900. csicr1 |= CSICR1_REDGE;
  901. if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  902. csicr1 |= CSICR1_SOF_POL;
  903. if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  904. csicr1 |= CSICR1_HSYNC_POL;
  905. if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
  906. csicr1 |= CSICR1_EXT_VSYNC;
  907. if (pcdev->platform_flags & MX2_CAMERA_CCIR)
  908. csicr1 |= CSICR1_CCIR_EN;
  909. if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
  910. csicr1 |= CSICR1_CCIR_MODE;
  911. if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
  912. csicr1 |= CSICR1_GCLK_MODE;
  913. if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
  914. csicr1 |= CSICR1_INV_DATA;
  915. pcdev->csicr1 = csicr1;
  916. bytesperline = soc_mbus_bytes_per_line(icd->user_width,
  917. icd->current_fmt->host_fmt);
  918. if (bytesperline < 0)
  919. return bytesperline;
  920. if (cpu_is_mx27()) {
  921. ret = mx27_camera_emma_prp_reset(pcdev);
  922. if (ret)
  923. return ret;
  924. } else if (cpu_is_mx25()) {
  925. writel((bytesperline * icd->user_height) >> 2,
  926. pcdev->base_csi + CSIRXCNT);
  927. writel((bytesperline << 16) | icd->user_height,
  928. pcdev->base_csi + CSIIMAG_PARA);
  929. }
  930. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  931. return 0;
  932. }
  933. static int mx2_camera_set_crop(struct soc_camera_device *icd,
  934. struct v4l2_crop *a)
  935. {
  936. struct v4l2_rect *rect = &a->c;
  937. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  938. struct v4l2_mbus_framefmt mf;
  939. int ret;
  940. soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
  941. soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
  942. ret = v4l2_subdev_call(sd, video, s_crop, a);
  943. if (ret < 0)
  944. return ret;
  945. /* The capture device might have changed its output */
  946. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  947. if (ret < 0)
  948. return ret;
  949. dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
  950. mf.width, mf.height);
  951. icd->user_width = mf.width;
  952. icd->user_height = mf.height;
  953. return ret;
  954. }
  955. static int mx2_camera_get_formats(struct soc_camera_device *icd,
  956. unsigned int idx,
  957. struct soc_camera_format_xlate *xlate)
  958. {
  959. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  960. const struct soc_mbus_pixelfmt *fmt;
  961. struct device *dev = icd->parent;
  962. enum v4l2_mbus_pixelcode code;
  963. int ret, formats = 0;
  964. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  965. if (ret < 0)
  966. /* no more formats */
  967. return 0;
  968. fmt = soc_mbus_get_fmtdesc(code);
  969. if (!fmt) {
  970. dev_err(dev, "Invalid format code #%u: %d\n", idx, code);
  971. return 0;
  972. }
  973. if (code == V4L2_MBUS_FMT_YUYV8_2X8 ||
  974. code == V4L2_MBUS_FMT_UYVY8_2X8) {
  975. formats++;
  976. if (xlate) {
  977. /*
  978. * CH2 can output YUV420 which is a standard format in
  979. * soc_mediabus.c
  980. */
  981. xlate->host_fmt =
  982. soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_1_5X8);
  983. xlate->code = code;
  984. dev_dbg(dev, "Providing host format %s for sensor code %d\n",
  985. xlate->host_fmt->name, code);
  986. xlate++;
  987. }
  988. }
  989. if (code == V4L2_MBUS_FMT_UYVY8_2X8) {
  990. formats++;
  991. if (xlate) {
  992. xlate->host_fmt =
  993. soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_2X8);
  994. xlate->code = code;
  995. dev_dbg(dev, "Providing host format %s for sensor code %d\n",
  996. xlate->host_fmt->name, code);
  997. xlate++;
  998. }
  999. }
  1000. /* Generic pass-trough */
  1001. formats++;
  1002. if (xlate) {
  1003. xlate->host_fmt = fmt;
  1004. xlate->code = code;
  1005. xlate++;
  1006. }
  1007. return formats;
  1008. }
  1009. static int mx2_emmaprp_resize(struct mx2_camera_dev *pcdev,
  1010. struct v4l2_mbus_framefmt *mf_in,
  1011. struct v4l2_pix_format *pix_out, bool apply)
  1012. {
  1013. int num, den;
  1014. unsigned long m;
  1015. int i, dir;
  1016. for (dir = RESIZE_DIR_H; dir <= RESIZE_DIR_V; dir++) {
  1017. struct emma_prp_resize tmprsz;
  1018. unsigned char *s = tmprsz.s;
  1019. int len = 0;
  1020. int in, out;
  1021. if (dir == RESIZE_DIR_H) {
  1022. in = mf_in->width;
  1023. out = pix_out->width;
  1024. } else {
  1025. in = mf_in->height;
  1026. out = pix_out->height;
  1027. }
  1028. if (in < out)
  1029. return -EINVAL;
  1030. else if (in == out)
  1031. continue;
  1032. /* Calculate ratio */
  1033. m = gcd(in, out);
  1034. num = in / m;
  1035. den = out / m;
  1036. if (num > RESIZE_NUM_MAX)
  1037. return -EINVAL;
  1038. if ((num >= 2 * den) && (den == 1) &&
  1039. (num < 9) && (!(num & 0x01))) {
  1040. int sum = 0;
  1041. int j;
  1042. /* Average scaling for >= 2:1 ratios */
  1043. /* Support can be added for num >=9 and odd values */
  1044. tmprsz.algo = RESIZE_ALGO_AVERAGING;
  1045. len = num;
  1046. for (i = 0; i < (len / 2); i++)
  1047. s[i] = 8;
  1048. do {
  1049. for (i = 0; i < (len / 2); i++) {
  1050. s[i] = s[i] >> 1;
  1051. sum = 0;
  1052. for (j = 0; j < (len / 2); j++)
  1053. sum += s[j];
  1054. if (sum == 4)
  1055. break;
  1056. }
  1057. } while (sum != 4);
  1058. for (i = (len / 2); i < len; i++)
  1059. s[i] = s[len - i - 1];
  1060. s[len - 1] |= SZ_COEF;
  1061. } else {
  1062. /* bilinear scaling for < 2:1 ratios */
  1063. int v; /* overflow counter */
  1064. int coeff, nxt; /* table output */
  1065. int in_pos_inc = 2 * den;
  1066. int out_pos = num;
  1067. int out_pos_inc = 2 * num;
  1068. int init_carry = num - den;
  1069. int carry = init_carry;
  1070. tmprsz.algo = RESIZE_ALGO_BILINEAR;
  1071. v = den + in_pos_inc;
  1072. do {
  1073. coeff = v - out_pos;
  1074. out_pos += out_pos_inc;
  1075. carry += out_pos_inc;
  1076. for (nxt = 0; v < out_pos; nxt++) {
  1077. v += in_pos_inc;
  1078. carry -= in_pos_inc;
  1079. }
  1080. if (len > RESIZE_NUM_MAX)
  1081. return -EINVAL;
  1082. coeff = ((coeff << BC_COEF) +
  1083. (in_pos_inc >> 1)) / in_pos_inc;
  1084. if (coeff >= (SZ_COEF - 1))
  1085. coeff--;
  1086. coeff |= SZ_COEF;
  1087. s[len] = (unsigned char)coeff;
  1088. len++;
  1089. for (i = 1; i < nxt; i++) {
  1090. if (len >= RESIZE_NUM_MAX)
  1091. return -EINVAL;
  1092. s[len] = 0;
  1093. len++;
  1094. }
  1095. } while (carry != init_carry);
  1096. }
  1097. tmprsz.len = len;
  1098. if (dir == RESIZE_DIR_H)
  1099. mf_in->width = pix_out->width;
  1100. else
  1101. mf_in->height = pix_out->height;
  1102. if (apply)
  1103. memcpy(&pcdev->resizing[dir], &tmprsz, sizeof(tmprsz));
  1104. }
  1105. return 0;
  1106. }
  1107. static int mx2_camera_set_fmt(struct soc_camera_device *icd,
  1108. struct v4l2_format *f)
  1109. {
  1110. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1111. struct mx2_camera_dev *pcdev = ici->priv;
  1112. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1113. const struct soc_camera_format_xlate *xlate;
  1114. struct v4l2_pix_format *pix = &f->fmt.pix;
  1115. struct v4l2_mbus_framefmt mf;
  1116. int ret;
  1117. dev_dbg(icd->parent, "%s: requested params: width = %d, height = %d\n",
  1118. __func__, pix->width, pix->height);
  1119. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  1120. if (!xlate) {
  1121. dev_warn(icd->parent, "Format %x not found\n",
  1122. pix->pixelformat);
  1123. return -EINVAL;
  1124. }
  1125. mf.width = pix->width;
  1126. mf.height = pix->height;
  1127. mf.field = pix->field;
  1128. mf.colorspace = pix->colorspace;
  1129. mf.code = xlate->code;
  1130. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
  1131. if (ret < 0 && ret != -ENOIOCTLCMD)
  1132. return ret;
  1133. /* Store width and height returned by the sensor for resizing */
  1134. pcdev->s_width = mf.width;
  1135. pcdev->s_height = mf.height;
  1136. dev_dbg(icd->parent, "%s: sensor params: width = %d, height = %d\n",
  1137. __func__, pcdev->s_width, pcdev->s_height);
  1138. pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code,
  1139. xlate->host_fmt->fourcc);
  1140. memset(pcdev->resizing, 0, sizeof(pcdev->resizing));
  1141. if ((mf.width != pix->width || mf.height != pix->height) &&
  1142. pcdev->emma_prp->cfg.in_fmt == PRP_CNTL_DATA_IN_YUV422) {
  1143. if (mx2_emmaprp_resize(pcdev, &mf, pix, true) < 0)
  1144. dev_dbg(icd->parent, "%s: can't resize\n", __func__);
  1145. }
  1146. if (mf.code != xlate->code)
  1147. return -EINVAL;
  1148. pix->width = mf.width;
  1149. pix->height = mf.height;
  1150. pix->field = mf.field;
  1151. pix->colorspace = mf.colorspace;
  1152. icd->current_fmt = xlate;
  1153. dev_dbg(icd->parent, "%s: returned params: width = %d, height = %d\n",
  1154. __func__, pix->width, pix->height);
  1155. return 0;
  1156. }
  1157. static int mx2_camera_try_fmt(struct soc_camera_device *icd,
  1158. struct v4l2_format *f)
  1159. {
  1160. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1161. const struct soc_camera_format_xlate *xlate;
  1162. struct v4l2_pix_format *pix = &f->fmt.pix;
  1163. struct v4l2_mbus_framefmt mf;
  1164. __u32 pixfmt = pix->pixelformat;
  1165. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1166. struct mx2_camera_dev *pcdev = ici->priv;
  1167. unsigned int width_limit;
  1168. int ret;
  1169. dev_dbg(icd->parent, "%s: requested params: width = %d, height = %d\n",
  1170. __func__, pix->width, pix->height);
  1171. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1172. if (pixfmt && !xlate) {
  1173. dev_warn(icd->parent, "Format %x not found\n", pixfmt);
  1174. return -EINVAL;
  1175. }
  1176. /* FIXME: implement MX27 limits */
  1177. /* limit to MX25 hardware capabilities */
  1178. if (cpu_is_mx25()) {
  1179. if (xlate->host_fmt->bits_per_sample <= 8)
  1180. width_limit = 0xffff * 4;
  1181. else
  1182. width_limit = 0xffff * 2;
  1183. /* CSIIMAG_PARA limit */
  1184. if (pix->width > width_limit)
  1185. pix->width = width_limit;
  1186. if (pix->height > 0xffff)
  1187. pix->height = 0xffff;
  1188. pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
  1189. xlate->host_fmt);
  1190. if (pix->bytesperline < 0)
  1191. return pix->bytesperline;
  1192. pix->sizeimage = soc_mbus_image_size(xlate->host_fmt,
  1193. pix->bytesperline, pix->height);
  1194. /* Check against the CSIRXCNT limit */
  1195. if (pix->sizeimage > 4 * 0x3ffff) {
  1196. /* Adjust geometry, preserve aspect ratio */
  1197. unsigned int new_height = int_sqrt(div_u64(0x3ffffULL *
  1198. 4 * pix->height, pix->bytesperline));
  1199. pix->width = new_height * pix->width / pix->height;
  1200. pix->height = new_height;
  1201. pix->bytesperline = soc_mbus_bytes_per_line(pix->width,
  1202. xlate->host_fmt);
  1203. BUG_ON(pix->bytesperline < 0);
  1204. pix->sizeimage = soc_mbus_image_size(xlate->host_fmt,
  1205. pix->bytesperline, pix->height);
  1206. }
  1207. }
  1208. /* limit to sensor capabilities */
  1209. mf.width = pix->width;
  1210. mf.height = pix->height;
  1211. mf.field = pix->field;
  1212. mf.colorspace = pix->colorspace;
  1213. mf.code = xlate->code;
  1214. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  1215. if (ret < 0)
  1216. return ret;
  1217. dev_dbg(icd->parent, "%s: sensor params: width = %d, height = %d\n",
  1218. __func__, pcdev->s_width, pcdev->s_height);
  1219. /* If the sensor does not support image size try PrP resizing */
  1220. pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code,
  1221. xlate->host_fmt->fourcc);
  1222. memset(pcdev->resizing, 0, sizeof(pcdev->resizing));
  1223. if ((mf.width != pix->width || mf.height != pix->height) &&
  1224. pcdev->emma_prp->cfg.in_fmt == PRP_CNTL_DATA_IN_YUV422) {
  1225. if (mx2_emmaprp_resize(pcdev, &mf, pix, false) < 0)
  1226. dev_dbg(icd->parent, "%s: can't resize\n", __func__);
  1227. }
  1228. if (mf.field == V4L2_FIELD_ANY)
  1229. mf.field = V4L2_FIELD_NONE;
  1230. /*
  1231. * Driver supports interlaced images provided they have
  1232. * both fields so that they can be processed as if they
  1233. * were progressive.
  1234. */
  1235. if (mf.field != V4L2_FIELD_NONE && !V4L2_FIELD_HAS_BOTH(mf.field)) {
  1236. dev_err(icd->parent, "Field type %d unsupported.\n",
  1237. mf.field);
  1238. return -EINVAL;
  1239. }
  1240. pix->width = mf.width;
  1241. pix->height = mf.height;
  1242. pix->field = mf.field;
  1243. pix->colorspace = mf.colorspace;
  1244. dev_dbg(icd->parent, "%s: returned params: width = %d, height = %d\n",
  1245. __func__, pix->width, pix->height);
  1246. return 0;
  1247. }
  1248. static int mx2_camera_querycap(struct soc_camera_host *ici,
  1249. struct v4l2_capability *cap)
  1250. {
  1251. /* cap->name is set by the friendly caller:-> */
  1252. strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
  1253. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1254. return 0;
  1255. }
  1256. static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
  1257. {
  1258. struct soc_camera_device *icd = file->private_data;
  1259. return vb2_poll(&icd->vb2_vidq, file, pt);
  1260. }
  1261. static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
  1262. .owner = THIS_MODULE,
  1263. .add = mx2_camera_add_device,
  1264. .remove = mx2_camera_remove_device,
  1265. .set_fmt = mx2_camera_set_fmt,
  1266. .set_crop = mx2_camera_set_crop,
  1267. .get_formats = mx2_camera_get_formats,
  1268. .try_fmt = mx2_camera_try_fmt,
  1269. .init_videobuf2 = mx2_camera_init_videobuf,
  1270. .poll = mx2_camera_poll,
  1271. .querycap = mx2_camera_querycap,
  1272. .set_bus_param = mx2_camera_set_bus_param,
  1273. };
  1274. static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
  1275. int bufnum, bool err)
  1276. {
  1277. #ifdef DEBUG
  1278. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  1279. #endif
  1280. struct mx2_buf_internal *ibuf;
  1281. struct mx2_buffer *buf;
  1282. struct vb2_buffer *vb;
  1283. unsigned long phys;
  1284. ibuf = list_first_entry(&pcdev->active_bufs, struct mx2_buf_internal,
  1285. queue);
  1286. BUG_ON(ibuf->bufnum != bufnum);
  1287. if (ibuf->discard) {
  1288. /*
  1289. * Discard buffer must not be returned to user space.
  1290. * Just return it to the discard queue.
  1291. */
  1292. list_move_tail(pcdev->active_bufs.next, &pcdev->discard);
  1293. } else {
  1294. buf = mx2_ibuf_to_buf(ibuf);
  1295. vb = &buf->vb;
  1296. #ifdef DEBUG
  1297. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  1298. if (prp->cfg.channel == 1) {
  1299. if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR +
  1300. 4 * bufnum) != phys) {
  1301. dev_err(pcdev->dev, "%lx != %x\n", phys,
  1302. readl(pcdev->base_emma +
  1303. PRP_DEST_RGB1_PTR + 4 * bufnum));
  1304. }
  1305. } else {
  1306. if (readl(pcdev->base_emma + PRP_DEST_Y_PTR -
  1307. 0x14 * bufnum) != phys) {
  1308. dev_err(pcdev->dev, "%lx != %x\n", phys,
  1309. readl(pcdev->base_emma +
  1310. PRP_DEST_Y_PTR - 0x14 * bufnum));
  1311. }
  1312. }
  1313. #endif
  1314. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, vb,
  1315. vb2_plane_vaddr(vb, 0),
  1316. vb2_get_plane_payload(vb, 0));
  1317. list_del_init(&buf->internal.queue);
  1318. do_gettimeofday(&vb->v4l2_buf.timestamp);
  1319. vb->v4l2_buf.sequence = pcdev->frame_count;
  1320. if (err)
  1321. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  1322. else
  1323. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  1324. }
  1325. pcdev->frame_count++;
  1326. if (list_empty(&pcdev->capture)) {
  1327. if (list_empty(&pcdev->discard)) {
  1328. dev_warn(pcdev->dev, "%s: trying to access empty discard list\n",
  1329. __func__);
  1330. return;
  1331. }
  1332. ibuf = list_first_entry(&pcdev->discard,
  1333. struct mx2_buf_internal, queue);
  1334. ibuf->bufnum = bufnum;
  1335. list_move_tail(pcdev->discard.next, &pcdev->active_bufs);
  1336. mx27_update_emma_buf(pcdev, pcdev->discard_buffer_dma, bufnum);
  1337. return;
  1338. }
  1339. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  1340. internal.queue);
  1341. buf->internal.bufnum = bufnum;
  1342. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  1343. vb = &buf->vb;
  1344. buf->state = MX2_STATE_ACTIVE;
  1345. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  1346. mx27_update_emma_buf(pcdev, phys, bufnum);
  1347. }
  1348. static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
  1349. {
  1350. struct mx2_camera_dev *pcdev = data;
  1351. unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
  1352. struct mx2_buf_internal *ibuf;
  1353. spin_lock(&pcdev->lock);
  1354. if (list_empty(&pcdev->active_bufs)) {
  1355. dev_warn(pcdev->dev, "%s: called while active list is empty\n",
  1356. __func__);
  1357. if (!status) {
  1358. spin_unlock(&pcdev->lock);
  1359. return IRQ_NONE;
  1360. }
  1361. }
  1362. if (status & (1 << 7)) { /* overflow */
  1363. u32 cntl = readl(pcdev->base_emma + PRP_CNTL);
  1364. writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN),
  1365. pcdev->base_emma + PRP_CNTL);
  1366. writel(cntl, pcdev->base_emma + PRP_CNTL);
  1367. ibuf = list_first_entry(&pcdev->active_bufs,
  1368. struct mx2_buf_internal, queue);
  1369. mx27_camera_frame_done_emma(pcdev,
  1370. ibuf->bufnum, true);
  1371. status &= ~(1 << 7);
  1372. } else if (((status & (3 << 5)) == (3 << 5)) ||
  1373. ((status & (3 << 3)) == (3 << 3))) {
  1374. /*
  1375. * Both buffers have triggered, process the one we're expecting
  1376. * to first
  1377. */
  1378. ibuf = list_first_entry(&pcdev->active_bufs,
  1379. struct mx2_buf_internal, queue);
  1380. mx27_camera_frame_done_emma(pcdev, ibuf->bufnum, false);
  1381. status &= ~(1 << (6 - ibuf->bufnum)); /* mark processed */
  1382. } else if ((status & (1 << 6)) || (status & (1 << 4))) {
  1383. mx27_camera_frame_done_emma(pcdev, 0, false);
  1384. } else if ((status & (1 << 5)) || (status & (1 << 3))) {
  1385. mx27_camera_frame_done_emma(pcdev, 1, false);
  1386. }
  1387. spin_unlock(&pcdev->lock);
  1388. writel(status, pcdev->base_emma + PRP_INTRSTATUS);
  1389. return IRQ_HANDLED;
  1390. }
  1391. static int __devinit mx27_camera_emma_init(struct mx2_camera_dev *pcdev)
  1392. {
  1393. struct resource *res_emma = pcdev->res_emma;
  1394. int err = 0;
  1395. if (!request_mem_region(res_emma->start, resource_size(res_emma),
  1396. MX2_CAM_DRV_NAME)) {
  1397. err = -EBUSY;
  1398. goto out;
  1399. }
  1400. pcdev->base_emma = ioremap(res_emma->start, resource_size(res_emma));
  1401. if (!pcdev->base_emma) {
  1402. err = -ENOMEM;
  1403. goto exit_release;
  1404. }
  1405. err = request_irq(pcdev->irq_emma, mx27_camera_emma_irq, 0,
  1406. MX2_CAM_DRV_NAME, pcdev);
  1407. if (err) {
  1408. dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n");
  1409. goto exit_iounmap;
  1410. }
  1411. pcdev->clk_emma = clk_get(NULL, "emma");
  1412. if (IS_ERR(pcdev->clk_emma)) {
  1413. err = PTR_ERR(pcdev->clk_emma);
  1414. goto exit_free_irq;
  1415. }
  1416. clk_enable(pcdev->clk_emma);
  1417. err = mx27_camera_emma_prp_reset(pcdev);
  1418. if (err)
  1419. goto exit_clk_emma_put;
  1420. return err;
  1421. exit_clk_emma_put:
  1422. clk_disable(pcdev->clk_emma);
  1423. clk_put(pcdev->clk_emma);
  1424. exit_free_irq:
  1425. free_irq(pcdev->irq_emma, pcdev);
  1426. exit_iounmap:
  1427. iounmap(pcdev->base_emma);
  1428. exit_release:
  1429. release_mem_region(res_emma->start, resource_size(res_emma));
  1430. out:
  1431. return err;
  1432. }
  1433. static int __devinit mx2_camera_probe(struct platform_device *pdev)
  1434. {
  1435. struct mx2_camera_dev *pcdev;
  1436. struct resource *res_csi, *res_emma;
  1437. void __iomem *base_csi;
  1438. int irq_csi, irq_emma;
  1439. int err = 0;
  1440. dev_dbg(&pdev->dev, "initialising\n");
  1441. res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1442. irq_csi = platform_get_irq(pdev, 0);
  1443. if (res_csi == NULL || irq_csi < 0) {
  1444. dev_err(&pdev->dev, "Missing platform resources data\n");
  1445. err = -ENODEV;
  1446. goto exit;
  1447. }
  1448. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1449. if (!pcdev) {
  1450. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1451. err = -ENOMEM;
  1452. goto exit;
  1453. }
  1454. pcdev->clk_csi = clk_get(&pdev->dev, NULL);
  1455. if (IS_ERR(pcdev->clk_csi)) {
  1456. dev_err(&pdev->dev, "Could not get csi clock\n");
  1457. err = PTR_ERR(pcdev->clk_csi);
  1458. goto exit_kfree;
  1459. }
  1460. pcdev->res_csi = res_csi;
  1461. pcdev->pdata = pdev->dev.platform_data;
  1462. if (pcdev->pdata) {
  1463. long rate;
  1464. pcdev->platform_flags = pcdev->pdata->flags;
  1465. rate = clk_round_rate(pcdev->clk_csi, pcdev->pdata->clk * 2);
  1466. if (rate <= 0) {
  1467. err = -ENODEV;
  1468. goto exit_dma_free;
  1469. }
  1470. err = clk_set_rate(pcdev->clk_csi, rate);
  1471. if (err < 0)
  1472. goto exit_dma_free;
  1473. }
  1474. INIT_LIST_HEAD(&pcdev->capture);
  1475. INIT_LIST_HEAD(&pcdev->active_bufs);
  1476. INIT_LIST_HEAD(&pcdev->discard);
  1477. spin_lock_init(&pcdev->lock);
  1478. /*
  1479. * Request the regions.
  1480. */
  1481. if (!request_mem_region(res_csi->start, resource_size(res_csi),
  1482. MX2_CAM_DRV_NAME)) {
  1483. err = -EBUSY;
  1484. goto exit_dma_free;
  1485. }
  1486. base_csi = ioremap(res_csi->start, resource_size(res_csi));
  1487. if (!base_csi) {
  1488. err = -ENOMEM;
  1489. goto exit_release;
  1490. }
  1491. pcdev->irq_csi = irq_csi;
  1492. pcdev->base_csi = base_csi;
  1493. pcdev->base_dma = res_csi->start;
  1494. pcdev->dev = &pdev->dev;
  1495. if (cpu_is_mx25()) {
  1496. err = request_irq(pcdev->irq_csi, mx25_camera_irq, 0,
  1497. MX2_CAM_DRV_NAME, pcdev);
  1498. if (err) {
  1499. dev_err(pcdev->dev, "Camera interrupt register failed \n");
  1500. goto exit_iounmap;
  1501. }
  1502. }
  1503. if (cpu_is_mx27()) {
  1504. /* EMMA support */
  1505. res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1506. irq_emma = platform_get_irq(pdev, 1);
  1507. if (!res_emma || !irq_emma) {
  1508. dev_err(&pdev->dev, "no EMMA resources\n");
  1509. goto exit_free_irq;
  1510. }
  1511. pcdev->res_emma = res_emma;
  1512. pcdev->irq_emma = irq_emma;
  1513. if (mx27_camera_emma_init(pcdev))
  1514. goto exit_free_irq;
  1515. }
  1516. pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
  1517. pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
  1518. pcdev->soc_host.priv = pcdev;
  1519. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1520. pcdev->soc_host.nr = pdev->id;
  1521. if (cpu_is_mx25())
  1522. pcdev->soc_host.capabilities = SOCAM_HOST_CAP_STRIDE;
  1523. pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1524. if (IS_ERR(pcdev->alloc_ctx)) {
  1525. err = PTR_ERR(pcdev->alloc_ctx);
  1526. goto eallocctx;
  1527. }
  1528. err = soc_camera_host_register(&pcdev->soc_host);
  1529. if (err)
  1530. goto exit_free_emma;
  1531. dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
  1532. clk_get_rate(pcdev->clk_csi));
  1533. return 0;
  1534. exit_free_emma:
  1535. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1536. eallocctx:
  1537. if (cpu_is_mx27()) {
  1538. free_irq(pcdev->irq_emma, pcdev);
  1539. clk_disable(pcdev->clk_emma);
  1540. clk_put(pcdev->clk_emma);
  1541. iounmap(pcdev->base_emma);
  1542. release_mem_region(pcdev->res_emma->start, resource_size(pcdev->res_emma));
  1543. }
  1544. exit_free_irq:
  1545. if (cpu_is_mx25())
  1546. free_irq(pcdev->irq_csi, pcdev);
  1547. exit_iounmap:
  1548. iounmap(base_csi);
  1549. exit_release:
  1550. release_mem_region(res_csi->start, resource_size(res_csi));
  1551. exit_dma_free:
  1552. clk_put(pcdev->clk_csi);
  1553. exit_kfree:
  1554. kfree(pcdev);
  1555. exit:
  1556. return err;
  1557. }
  1558. static int __devexit mx2_camera_remove(struct platform_device *pdev)
  1559. {
  1560. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1561. struct mx2_camera_dev *pcdev = container_of(soc_host,
  1562. struct mx2_camera_dev, soc_host);
  1563. struct resource *res;
  1564. clk_put(pcdev->clk_csi);
  1565. if (cpu_is_mx25())
  1566. free_irq(pcdev->irq_csi, pcdev);
  1567. if (cpu_is_mx27())
  1568. free_irq(pcdev->irq_emma, pcdev);
  1569. soc_camera_host_unregister(&pcdev->soc_host);
  1570. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1571. iounmap(pcdev->base_csi);
  1572. if (cpu_is_mx27()) {
  1573. clk_disable(pcdev->clk_emma);
  1574. clk_put(pcdev->clk_emma);
  1575. iounmap(pcdev->base_emma);
  1576. res = pcdev->res_emma;
  1577. release_mem_region(res->start, resource_size(res));
  1578. }
  1579. res = pcdev->res_csi;
  1580. release_mem_region(res->start, resource_size(res));
  1581. kfree(pcdev);
  1582. dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
  1583. return 0;
  1584. }
  1585. static struct platform_driver mx2_camera_driver = {
  1586. .driver = {
  1587. .name = MX2_CAM_DRV_NAME,
  1588. },
  1589. .remove = __devexit_p(mx2_camera_remove),
  1590. };
  1591. static int __init mx2_camera_init(void)
  1592. {
  1593. return platform_driver_probe(&mx2_camera_driver, &mx2_camera_probe);
  1594. }
  1595. static void __exit mx2_camera_exit(void)
  1596. {
  1597. return platform_driver_unregister(&mx2_camera_driver);
  1598. }
  1599. module_init(mx2_camera_init);
  1600. module_exit(mx2_camera_exit);
  1601. MODULE_DESCRIPTION("i.MX27/i.MX25 SoC Camera Host driver");
  1602. MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
  1603. MODULE_LICENSE("GPL");
  1604. MODULE_VERSION(MX2_CAM_VERSION);