mx2_camera.c 50 KB

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