mx2_camera.c 42 KB

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