mx2_camera.c 48 KB

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