mx2_camera.c 45 KB

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