mx2_camera.c 49 KB

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