mx2_camera.c 50 KB

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