mx2_camera.c 45 KB

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