mx2_camera.c 50 KB

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