mx2_camera.c 42 KB

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