mx2_camera.c 49 KB

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