mx2_camera.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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. /*
  373. * The following two functions absolutely depend on the fact, that
  374. * there can be only one camera on mx2 camera sensor interface
  375. */
  376. static int mx2_camera_add_device(struct soc_camera_device *icd)
  377. {
  378. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  379. struct mx2_camera_dev *pcdev = ici->priv;
  380. int ret;
  381. u32 csicr1;
  382. ret = clk_prepare_enable(pcdev->clk_csi_ahb);
  383. if (ret < 0)
  384. return ret;
  385. ret = clk_prepare_enable(pcdev->clk_csi_per);
  386. if (ret < 0)
  387. goto exit_csi_ahb;
  388. csicr1 = CSICR1_MCLKEN | CSICR1_PRP_IF_EN | CSICR1_FCC |
  389. CSICR1_RXFF_LEVEL(0);
  390. pcdev->csicr1 = csicr1;
  391. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  392. pcdev->frame_count = 0;
  393. dev_info(icd->parent, "Camera driver attached to camera %d\n",
  394. icd->devnum);
  395. return 0;
  396. exit_csi_ahb:
  397. clk_disable_unprepare(pcdev->clk_csi_ahb);
  398. return ret;
  399. }
  400. static void mx2_camera_remove_device(struct soc_camera_device *icd)
  401. {
  402. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  403. struct mx2_camera_dev *pcdev = ici->priv;
  404. dev_info(icd->parent, "Camera driver detached from camera %d\n",
  405. icd->devnum);
  406. mx2_camera_deactivate(pcdev);
  407. }
  408. /*
  409. * Videobuf operations
  410. */
  411. static int mx2_videobuf_setup(struct vb2_queue *vq,
  412. const struct v4l2_format *fmt,
  413. unsigned int *count, unsigned int *num_planes,
  414. unsigned int sizes[], void *alloc_ctxs[])
  415. {
  416. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  417. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  418. struct mx2_camera_dev *pcdev = ici->priv;
  419. dev_dbg(icd->parent, "count=%d, size=%d\n", *count, sizes[0]);
  420. /* TODO: support for VIDIOC_CREATE_BUFS not ready */
  421. if (fmt != NULL)
  422. return -ENOTTY;
  423. alloc_ctxs[0] = pcdev->alloc_ctx;
  424. sizes[0] = icd->sizeimage;
  425. if (0 == *count)
  426. *count = 32;
  427. if (!*num_planes &&
  428. sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024)
  429. *count = (MAX_VIDEO_MEM * 1024 * 1024) / sizes[0];
  430. *num_planes = 1;
  431. return 0;
  432. }
  433. static int mx2_videobuf_prepare(struct vb2_buffer *vb)
  434. {
  435. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  436. int ret = 0;
  437. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  438. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  439. #ifdef DEBUG
  440. /*
  441. * This can be useful if you want to see if we actually fill
  442. * the buffer with something
  443. */
  444. memset((void *)vb2_plane_vaddr(vb, 0),
  445. 0xaa, vb2_get_plane_payload(vb, 0));
  446. #endif
  447. vb2_set_plane_payload(vb, 0, icd->sizeimage);
  448. if (vb2_plane_vaddr(vb, 0) &&
  449. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  450. ret = -EINVAL;
  451. goto out;
  452. }
  453. return 0;
  454. out:
  455. return ret;
  456. }
  457. static void mx2_videobuf_queue(struct vb2_buffer *vb)
  458. {
  459. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  460. struct soc_camera_host *ici =
  461. to_soc_camera_host(icd->parent);
  462. struct mx2_camera_dev *pcdev = ici->priv;
  463. struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
  464. unsigned long flags;
  465. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  466. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  467. spin_lock_irqsave(&pcdev->lock, flags);
  468. list_add_tail(&buf->internal.queue, &pcdev->capture);
  469. spin_unlock_irqrestore(&pcdev->lock, flags);
  470. }
  471. static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
  472. int bytesperline)
  473. {
  474. struct soc_camera_host *ici =
  475. to_soc_camera_host(icd->parent);
  476. struct mx2_camera_dev *pcdev = ici->priv;
  477. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  478. writel((pcdev->s_width << 16) | pcdev->s_height,
  479. pcdev->base_emma + PRP_SRC_FRAME_SIZE);
  480. writel(prp->cfg.src_pixel,
  481. pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
  482. if (prp->cfg.channel == 1) {
  483. writel((icd->user_width << 16) | icd->user_height,
  484. pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
  485. writel(bytesperline,
  486. pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
  487. writel(prp->cfg.ch1_pixel,
  488. pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
  489. } else { /* channel 2 */
  490. writel((icd->user_width << 16) | icd->user_height,
  491. pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
  492. }
  493. /* Enable interrupts */
  494. writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL);
  495. }
  496. static void mx2_prp_resize_commit(struct mx2_camera_dev *pcdev)
  497. {
  498. int dir;
  499. for (dir = RESIZE_DIR_H; dir <= RESIZE_DIR_V; dir++) {
  500. unsigned char *s = pcdev->resizing[dir].s;
  501. int len = pcdev->resizing[dir].len;
  502. unsigned int coeff[2] = {0, 0};
  503. unsigned int valid = 0;
  504. int i;
  505. if (len == 0)
  506. continue;
  507. for (i = RESIZE_NUM_MAX - 1; i >= 0; i--) {
  508. int j;
  509. j = i > 9 ? 1 : 0;
  510. coeff[j] = (coeff[j] << BC_COEF) |
  511. (s[i] & (SZ_COEF - 1));
  512. if (i == 5 || i == 15)
  513. coeff[j] <<= 1;
  514. valid = (valid << 1) | (s[i] >> BC_COEF);
  515. }
  516. valid |= PRP_RZ_VALID_TBL_LEN(len);
  517. if (pcdev->resizing[dir].algo == RESIZE_ALGO_BILINEAR)
  518. valid |= PRP_RZ_VALID_BILINEAR;
  519. if (pcdev->emma_prp->cfg.channel == 1) {
  520. if (dir == RESIZE_DIR_H) {
  521. writel(coeff[0], pcdev->base_emma +
  522. PRP_CH1_RZ_HORI_COEF1);
  523. writel(coeff[1], pcdev->base_emma +
  524. PRP_CH1_RZ_HORI_COEF2);
  525. writel(valid, pcdev->base_emma +
  526. PRP_CH1_RZ_HORI_VALID);
  527. } else {
  528. writel(coeff[0], pcdev->base_emma +
  529. PRP_CH1_RZ_VERT_COEF1);
  530. writel(coeff[1], pcdev->base_emma +
  531. PRP_CH1_RZ_VERT_COEF2);
  532. writel(valid, pcdev->base_emma +
  533. PRP_CH1_RZ_VERT_VALID);
  534. }
  535. } else {
  536. if (dir == RESIZE_DIR_H) {
  537. writel(coeff[0], pcdev->base_emma +
  538. PRP_CH2_RZ_HORI_COEF1);
  539. writel(coeff[1], pcdev->base_emma +
  540. PRP_CH2_RZ_HORI_COEF2);
  541. writel(valid, pcdev->base_emma +
  542. PRP_CH2_RZ_HORI_VALID);
  543. } else {
  544. writel(coeff[0], pcdev->base_emma +
  545. PRP_CH2_RZ_VERT_COEF1);
  546. writel(coeff[1], pcdev->base_emma +
  547. PRP_CH2_RZ_VERT_COEF2);
  548. writel(valid, pcdev->base_emma +
  549. PRP_CH2_RZ_VERT_VALID);
  550. }
  551. }
  552. }
  553. }
  554. static int mx2_start_streaming(struct vb2_queue *q, unsigned int count)
  555. {
  556. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  557. struct soc_camera_host *ici =
  558. to_soc_camera_host(icd->parent);
  559. struct mx2_camera_dev *pcdev = ici->priv;
  560. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  561. struct vb2_buffer *vb;
  562. struct mx2_buffer *buf;
  563. unsigned long phys;
  564. int bytesperline;
  565. unsigned long flags;
  566. if (count < 2)
  567. return -EINVAL;
  568. spin_lock_irqsave(&pcdev->lock, flags);
  569. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  570. internal.queue);
  571. buf->internal.bufnum = 0;
  572. vb = &buf->vb;
  573. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  574. mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
  575. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  576. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  577. internal.queue);
  578. buf->internal.bufnum = 1;
  579. vb = &buf->vb;
  580. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  581. mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
  582. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  583. bytesperline = soc_mbus_bytes_per_line(icd->user_width,
  584. icd->current_fmt->host_fmt);
  585. if (bytesperline < 0) {
  586. spin_unlock_irqrestore(&pcdev->lock, flags);
  587. return bytesperline;
  588. }
  589. /*
  590. * I didn't manage to properly enable/disable the prp
  591. * on a per frame basis during running transfers,
  592. * thus we allocate a buffer here and use it to
  593. * discard frames when no buffer is available.
  594. * Feel free to work on this ;)
  595. */
  596. pcdev->discard_size = icd->user_height * bytesperline;
  597. pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
  598. pcdev->discard_size,
  599. &pcdev->discard_buffer_dma, GFP_ATOMIC);
  600. if (!pcdev->discard_buffer) {
  601. spin_unlock_irqrestore(&pcdev->lock, flags);
  602. return -ENOMEM;
  603. }
  604. pcdev->buf_discard[0].discard = true;
  605. list_add_tail(&pcdev->buf_discard[0].queue,
  606. &pcdev->discard);
  607. pcdev->buf_discard[1].discard = true;
  608. list_add_tail(&pcdev->buf_discard[1].queue,
  609. &pcdev->discard);
  610. mx2_prp_resize_commit(pcdev);
  611. mx27_camera_emma_buf_init(icd, bytesperline);
  612. if (prp->cfg.channel == 1) {
  613. writel(PRP_CNTL_CH1EN |
  614. PRP_CNTL_CSIEN |
  615. prp->cfg.in_fmt |
  616. prp->cfg.out_fmt |
  617. PRP_CNTL_CH1_LEN |
  618. PRP_CNTL_CH1BYP |
  619. PRP_CNTL_CH1_TSKIP(0) |
  620. PRP_CNTL_IN_TSKIP(0),
  621. pcdev->base_emma + PRP_CNTL);
  622. } else {
  623. writel(PRP_CNTL_CH2EN |
  624. PRP_CNTL_CSIEN |
  625. prp->cfg.in_fmt |
  626. prp->cfg.out_fmt |
  627. PRP_CNTL_CH2_LEN |
  628. PRP_CNTL_CH2_TSKIP(0) |
  629. PRP_CNTL_IN_TSKIP(0),
  630. pcdev->base_emma + PRP_CNTL);
  631. }
  632. spin_unlock_irqrestore(&pcdev->lock, flags);
  633. return 0;
  634. }
  635. static int mx2_stop_streaming(struct vb2_queue *q)
  636. {
  637. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  638. struct soc_camera_host *ici =
  639. to_soc_camera_host(icd->parent);
  640. struct mx2_camera_dev *pcdev = ici->priv;
  641. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  642. unsigned long flags;
  643. void *b;
  644. u32 cntl;
  645. spin_lock_irqsave(&pcdev->lock, flags);
  646. cntl = readl(pcdev->base_emma + PRP_CNTL);
  647. if (prp->cfg.channel == 1) {
  648. writel(cntl & ~PRP_CNTL_CH1EN,
  649. pcdev->base_emma + PRP_CNTL);
  650. } else {
  651. writel(cntl & ~PRP_CNTL_CH2EN,
  652. pcdev->base_emma + PRP_CNTL);
  653. }
  654. INIT_LIST_HEAD(&pcdev->capture);
  655. INIT_LIST_HEAD(&pcdev->active_bufs);
  656. INIT_LIST_HEAD(&pcdev->discard);
  657. b = pcdev->discard_buffer;
  658. pcdev->discard_buffer = NULL;
  659. spin_unlock_irqrestore(&pcdev->lock, flags);
  660. dma_free_coherent(ici->v4l2_dev.dev,
  661. pcdev->discard_size, b, pcdev->discard_buffer_dma);
  662. return 0;
  663. }
  664. static struct vb2_ops mx2_videobuf_ops = {
  665. .queue_setup = mx2_videobuf_setup,
  666. .buf_prepare = mx2_videobuf_prepare,
  667. .buf_queue = mx2_videobuf_queue,
  668. .start_streaming = mx2_start_streaming,
  669. .stop_streaming = mx2_stop_streaming,
  670. };
  671. static int mx2_camera_init_videobuf(struct vb2_queue *q,
  672. struct soc_camera_device *icd)
  673. {
  674. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  675. q->io_modes = VB2_MMAP | VB2_USERPTR;
  676. q->drv_priv = icd;
  677. q->ops = &mx2_videobuf_ops;
  678. q->mem_ops = &vb2_dma_contig_memops;
  679. q->buf_struct_size = sizeof(struct mx2_buffer);
  680. q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  681. return vb2_queue_init(q);
  682. }
  683. #define MX2_BUS_FLAGS (V4L2_MBUS_MASTER | \
  684. V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
  685. V4L2_MBUS_VSYNC_ACTIVE_LOW | \
  686. V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
  687. V4L2_MBUS_HSYNC_ACTIVE_LOW | \
  688. V4L2_MBUS_PCLK_SAMPLE_RISING | \
  689. V4L2_MBUS_PCLK_SAMPLE_FALLING | \
  690. V4L2_MBUS_DATA_ACTIVE_HIGH | \
  691. V4L2_MBUS_DATA_ACTIVE_LOW)
  692. static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
  693. {
  694. u32 cntl;
  695. int count = 0;
  696. cntl = readl(pcdev->base_emma + PRP_CNTL);
  697. writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
  698. while (count++ < 100) {
  699. if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
  700. return 0;
  701. barrier();
  702. udelay(1);
  703. }
  704. return -ETIMEDOUT;
  705. }
  706. static int mx2_camera_set_bus_param(struct soc_camera_device *icd)
  707. {
  708. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  709. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  710. struct mx2_camera_dev *pcdev = ici->priv;
  711. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  712. unsigned long common_flags;
  713. int ret;
  714. int bytesperline;
  715. u32 csicr1 = pcdev->csicr1;
  716. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  717. if (!ret) {
  718. common_flags = soc_mbus_config_compatible(&cfg, MX2_BUS_FLAGS);
  719. if (!common_flags) {
  720. dev_warn(icd->parent,
  721. "Flags incompatible: camera 0x%x, host 0x%x\n",
  722. cfg.flags, MX2_BUS_FLAGS);
  723. return -EINVAL;
  724. }
  725. } else if (ret != -ENOIOCTLCMD) {
  726. return ret;
  727. } else {
  728. common_flags = MX2_BUS_FLAGS;
  729. }
  730. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  731. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  732. if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
  733. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  734. else
  735. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  736. }
  737. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  738. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  739. if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
  740. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  741. else
  742. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  743. }
  744. cfg.flags = common_flags;
  745. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  746. if (ret < 0 && ret != -ENOIOCTLCMD) {
  747. dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
  748. common_flags, ret);
  749. return ret;
  750. }
  751. csicr1 = (csicr1 & ~CSICR1_FMT_MASK) | pcdev->emma_prp->cfg.csicr1;
  752. if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  753. csicr1 |= CSICR1_REDGE;
  754. if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  755. csicr1 |= CSICR1_SOF_POL;
  756. if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  757. csicr1 |= CSICR1_HSYNC_POL;
  758. if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
  759. csicr1 |= CSICR1_EXT_VSYNC;
  760. if (pcdev->platform_flags & MX2_CAMERA_CCIR)
  761. csicr1 |= CSICR1_CCIR_EN;
  762. if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
  763. csicr1 |= CSICR1_CCIR_MODE;
  764. if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
  765. csicr1 |= CSICR1_GCLK_MODE;
  766. if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
  767. csicr1 |= CSICR1_INV_DATA;
  768. pcdev->csicr1 = csicr1;
  769. bytesperline = soc_mbus_bytes_per_line(icd->user_width,
  770. icd->current_fmt->host_fmt);
  771. if (bytesperline < 0)
  772. return bytesperline;
  773. ret = mx27_camera_emma_prp_reset(pcdev);
  774. if (ret)
  775. return ret;
  776. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  777. return 0;
  778. }
  779. static int mx2_camera_set_crop(struct soc_camera_device *icd,
  780. const struct v4l2_crop *a)
  781. {
  782. struct v4l2_crop a_writable = *a;
  783. struct v4l2_rect *rect = &a_writable.c;
  784. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  785. struct v4l2_mbus_framefmt mf;
  786. int ret;
  787. soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
  788. soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
  789. ret = v4l2_subdev_call(sd, video, s_crop, a);
  790. if (ret < 0)
  791. return ret;
  792. /* The capture device might have changed its output */
  793. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  794. if (ret < 0)
  795. return ret;
  796. dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
  797. mf.width, mf.height);
  798. icd->user_width = mf.width;
  799. icd->user_height = mf.height;
  800. return ret;
  801. }
  802. static int mx2_camera_get_formats(struct soc_camera_device *icd,
  803. unsigned int idx,
  804. struct soc_camera_format_xlate *xlate)
  805. {
  806. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  807. const struct soc_mbus_pixelfmt *fmt;
  808. struct device *dev = icd->parent;
  809. enum v4l2_mbus_pixelcode code;
  810. int ret, formats = 0;
  811. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  812. if (ret < 0)
  813. /* no more formats */
  814. return 0;
  815. fmt = soc_mbus_get_fmtdesc(code);
  816. if (!fmt) {
  817. dev_err(dev, "Invalid format code #%u: %d\n", idx, code);
  818. return 0;
  819. }
  820. if (code == V4L2_MBUS_FMT_YUYV8_2X8 ||
  821. code == V4L2_MBUS_FMT_UYVY8_2X8) {
  822. formats++;
  823. if (xlate) {
  824. /*
  825. * CH2 can output YUV420 which is a standard format in
  826. * soc_mediabus.c
  827. */
  828. xlate->host_fmt =
  829. soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_1_5X8);
  830. xlate->code = code;
  831. dev_dbg(dev, "Providing host format %s for sensor code %d\n",
  832. xlate->host_fmt->name, code);
  833. xlate++;
  834. }
  835. }
  836. if (code == V4L2_MBUS_FMT_UYVY8_2X8) {
  837. formats++;
  838. if (xlate) {
  839. xlate->host_fmt =
  840. soc_mbus_get_fmtdesc(V4L2_MBUS_FMT_YUYV8_2X8);
  841. xlate->code = code;
  842. dev_dbg(dev, "Providing host format %s for sensor code %d\n",
  843. xlate->host_fmt->name, code);
  844. xlate++;
  845. }
  846. }
  847. /* Generic pass-trough */
  848. formats++;
  849. if (xlate) {
  850. xlate->host_fmt = fmt;
  851. xlate->code = code;
  852. xlate++;
  853. }
  854. return formats;
  855. }
  856. static int mx2_emmaprp_resize(struct mx2_camera_dev *pcdev,
  857. struct v4l2_mbus_framefmt *mf_in,
  858. struct v4l2_pix_format *pix_out, bool apply)
  859. {
  860. int num, den;
  861. unsigned long m;
  862. int i, dir;
  863. for (dir = RESIZE_DIR_H; dir <= RESIZE_DIR_V; dir++) {
  864. struct emma_prp_resize tmprsz;
  865. unsigned char *s = tmprsz.s;
  866. int len = 0;
  867. int in, out;
  868. if (dir == RESIZE_DIR_H) {
  869. in = mf_in->width;
  870. out = pix_out->width;
  871. } else {
  872. in = mf_in->height;
  873. out = pix_out->height;
  874. }
  875. if (in < out)
  876. return -EINVAL;
  877. else if (in == out)
  878. continue;
  879. /* Calculate ratio */
  880. m = gcd(in, out);
  881. num = in / m;
  882. den = out / m;
  883. if (num > RESIZE_NUM_MAX)
  884. return -EINVAL;
  885. if ((num >= 2 * den) && (den == 1) &&
  886. (num < 9) && (!(num & 0x01))) {
  887. int sum = 0;
  888. int j;
  889. /* Average scaling for >= 2:1 ratios */
  890. /* Support can be added for num >=9 and odd values */
  891. tmprsz.algo = RESIZE_ALGO_AVERAGING;
  892. len = num;
  893. for (i = 0; i < (len / 2); i++)
  894. s[i] = 8;
  895. do {
  896. for (i = 0; i < (len / 2); i++) {
  897. s[i] = s[i] >> 1;
  898. sum = 0;
  899. for (j = 0; j < (len / 2); j++)
  900. sum += s[j];
  901. if (sum == 4)
  902. break;
  903. }
  904. } while (sum != 4);
  905. for (i = (len / 2); i < len; i++)
  906. s[i] = s[len - i - 1];
  907. s[len - 1] |= SZ_COEF;
  908. } else {
  909. /* bilinear scaling for < 2:1 ratios */
  910. int v; /* overflow counter */
  911. int coeff, nxt; /* table output */
  912. int in_pos_inc = 2 * den;
  913. int out_pos = num;
  914. int out_pos_inc = 2 * num;
  915. int init_carry = num - den;
  916. int carry = init_carry;
  917. tmprsz.algo = RESIZE_ALGO_BILINEAR;
  918. v = den + in_pos_inc;
  919. do {
  920. coeff = v - out_pos;
  921. out_pos += out_pos_inc;
  922. carry += out_pos_inc;
  923. for (nxt = 0; v < out_pos; nxt++) {
  924. v += in_pos_inc;
  925. carry -= in_pos_inc;
  926. }
  927. if (len > RESIZE_NUM_MAX)
  928. return -EINVAL;
  929. coeff = ((coeff << BC_COEF) +
  930. (in_pos_inc >> 1)) / in_pos_inc;
  931. if (coeff >= (SZ_COEF - 1))
  932. coeff--;
  933. coeff |= SZ_COEF;
  934. s[len] = (unsigned char)coeff;
  935. len++;
  936. for (i = 1; i < nxt; i++) {
  937. if (len >= RESIZE_NUM_MAX)
  938. return -EINVAL;
  939. s[len] = 0;
  940. len++;
  941. }
  942. } while (carry != init_carry);
  943. }
  944. tmprsz.len = len;
  945. if (dir == RESIZE_DIR_H)
  946. mf_in->width = pix_out->width;
  947. else
  948. mf_in->height = pix_out->height;
  949. if (apply)
  950. memcpy(&pcdev->resizing[dir], &tmprsz, sizeof(tmprsz));
  951. }
  952. return 0;
  953. }
  954. static int mx2_camera_set_fmt(struct soc_camera_device *icd,
  955. struct v4l2_format *f)
  956. {
  957. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  958. struct mx2_camera_dev *pcdev = ici->priv;
  959. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  960. const struct soc_camera_format_xlate *xlate;
  961. struct v4l2_pix_format *pix = &f->fmt.pix;
  962. struct v4l2_mbus_framefmt mf;
  963. int ret;
  964. dev_dbg(icd->parent, "%s: requested params: width = %d, height = %d\n",
  965. __func__, pix->width, pix->height);
  966. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  967. if (!xlate) {
  968. dev_warn(icd->parent, "Format %x not found\n",
  969. pix->pixelformat);
  970. return -EINVAL;
  971. }
  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, s_mbus_fmt, &mf);
  978. if (ret < 0 && ret != -ENOIOCTLCMD)
  979. return ret;
  980. /* Store width and height returned by the sensor for resizing */
  981. pcdev->s_width = mf.width;
  982. pcdev->s_height = mf.height;
  983. dev_dbg(icd->parent, "%s: sensor params: width = %d, height = %d\n",
  984. __func__, pcdev->s_width, pcdev->s_height);
  985. pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code,
  986. xlate->host_fmt->fourcc);
  987. memset(pcdev->resizing, 0, sizeof(pcdev->resizing));
  988. if ((mf.width != pix->width || mf.height != pix->height) &&
  989. pcdev->emma_prp->cfg.in_fmt == PRP_CNTL_DATA_IN_YUV422) {
  990. if (mx2_emmaprp_resize(pcdev, &mf, pix, true) < 0)
  991. dev_dbg(icd->parent, "%s: can't resize\n", __func__);
  992. }
  993. if (mf.code != xlate->code)
  994. return -EINVAL;
  995. pix->width = mf.width;
  996. pix->height = mf.height;
  997. pix->field = mf.field;
  998. pix->colorspace = mf.colorspace;
  999. icd->current_fmt = xlate;
  1000. dev_dbg(icd->parent, "%s: returned params: width = %d, height = %d\n",
  1001. __func__, pix->width, pix->height);
  1002. return 0;
  1003. }
  1004. static int mx2_camera_try_fmt(struct soc_camera_device *icd,
  1005. struct v4l2_format *f)
  1006. {
  1007. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1008. const struct soc_camera_format_xlate *xlate;
  1009. struct v4l2_pix_format *pix = &f->fmt.pix;
  1010. struct v4l2_mbus_framefmt mf;
  1011. __u32 pixfmt = pix->pixelformat;
  1012. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1013. struct mx2_camera_dev *pcdev = ici->priv;
  1014. struct mx2_fmt_cfg *emma_prp;
  1015. int ret;
  1016. dev_dbg(icd->parent, "%s: requested params: width = %d, height = %d\n",
  1017. __func__, pix->width, pix->height);
  1018. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1019. if (pixfmt && !xlate) {
  1020. dev_warn(icd->parent, "Format %x not found\n", pixfmt);
  1021. return -EINVAL;
  1022. }
  1023. /*
  1024. * limit to MX27 hardware capabilities: width must be a multiple of 8 as
  1025. * requested by the CSI. (Table 39-2 in the i.MX27 Reference Manual).
  1026. */
  1027. pix->width &= ~0x7;
  1028. /* limit to sensor capabilities */
  1029. mf.width = pix->width;
  1030. mf.height = pix->height;
  1031. mf.field = pix->field;
  1032. mf.colorspace = pix->colorspace;
  1033. mf.code = xlate->code;
  1034. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  1035. if (ret < 0)
  1036. return ret;
  1037. dev_dbg(icd->parent, "%s: sensor params: width = %d, height = %d\n",
  1038. __func__, pcdev->s_width, pcdev->s_height);
  1039. /* If the sensor does not support image size try PrP resizing */
  1040. emma_prp = mx27_emma_prp_get_format(xlate->code,
  1041. xlate->host_fmt->fourcc);
  1042. if ((mf.width != pix->width || mf.height != pix->height) &&
  1043. emma_prp->cfg.in_fmt == PRP_CNTL_DATA_IN_YUV422) {
  1044. if (mx2_emmaprp_resize(pcdev, &mf, pix, false) < 0)
  1045. dev_dbg(icd->parent, "%s: can't resize\n", __func__);
  1046. }
  1047. if (mf.field == V4L2_FIELD_ANY)
  1048. mf.field = V4L2_FIELD_NONE;
  1049. /*
  1050. * Driver supports interlaced images provided they have
  1051. * both fields so that they can be processed as if they
  1052. * were progressive.
  1053. */
  1054. if (mf.field != V4L2_FIELD_NONE && !V4L2_FIELD_HAS_BOTH(mf.field)) {
  1055. dev_err(icd->parent, "Field type %d unsupported.\n",
  1056. mf.field);
  1057. return -EINVAL;
  1058. }
  1059. pix->width = mf.width;
  1060. pix->height = mf.height;
  1061. pix->field = mf.field;
  1062. pix->colorspace = mf.colorspace;
  1063. dev_dbg(icd->parent, "%s: returned params: width = %d, height = %d\n",
  1064. __func__, pix->width, pix->height);
  1065. return 0;
  1066. }
  1067. static int mx2_camera_querycap(struct soc_camera_host *ici,
  1068. struct v4l2_capability *cap)
  1069. {
  1070. /* cap->name is set by the friendly caller:-> */
  1071. strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
  1072. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1073. return 0;
  1074. }
  1075. static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
  1076. {
  1077. struct soc_camera_device *icd = file->private_data;
  1078. return vb2_poll(&icd->vb2_vidq, file, pt);
  1079. }
  1080. static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
  1081. .owner = THIS_MODULE,
  1082. .add = mx2_camera_add_device,
  1083. .remove = mx2_camera_remove_device,
  1084. .set_fmt = mx2_camera_set_fmt,
  1085. .set_crop = mx2_camera_set_crop,
  1086. .get_formats = mx2_camera_get_formats,
  1087. .try_fmt = mx2_camera_try_fmt,
  1088. .init_videobuf2 = mx2_camera_init_videobuf,
  1089. .poll = mx2_camera_poll,
  1090. .querycap = mx2_camera_querycap,
  1091. .set_bus_param = mx2_camera_set_bus_param,
  1092. };
  1093. static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
  1094. int bufnum, bool err)
  1095. {
  1096. #ifdef DEBUG
  1097. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  1098. #endif
  1099. struct mx2_buf_internal *ibuf;
  1100. struct mx2_buffer *buf;
  1101. struct vb2_buffer *vb;
  1102. unsigned long phys;
  1103. ibuf = list_first_entry(&pcdev->active_bufs, struct mx2_buf_internal,
  1104. queue);
  1105. BUG_ON(ibuf->bufnum != bufnum);
  1106. if (ibuf->discard) {
  1107. /*
  1108. * Discard buffer must not be returned to user space.
  1109. * Just return it to the discard queue.
  1110. */
  1111. list_move_tail(pcdev->active_bufs.next, &pcdev->discard);
  1112. } else {
  1113. buf = mx2_ibuf_to_buf(ibuf);
  1114. vb = &buf->vb;
  1115. #ifdef DEBUG
  1116. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  1117. if (prp->cfg.channel == 1) {
  1118. if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR +
  1119. 4 * bufnum) != phys) {
  1120. dev_err(pcdev->dev, "%lx != %x\n", phys,
  1121. readl(pcdev->base_emma +
  1122. PRP_DEST_RGB1_PTR + 4 * bufnum));
  1123. }
  1124. } else {
  1125. if (readl(pcdev->base_emma + PRP_DEST_Y_PTR -
  1126. 0x14 * bufnum) != phys) {
  1127. dev_err(pcdev->dev, "%lx != %x\n", phys,
  1128. readl(pcdev->base_emma +
  1129. PRP_DEST_Y_PTR - 0x14 * bufnum));
  1130. }
  1131. }
  1132. #endif
  1133. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, vb,
  1134. vb2_plane_vaddr(vb, 0),
  1135. vb2_get_plane_payload(vb, 0));
  1136. list_del_init(&buf->internal.queue);
  1137. v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
  1138. vb->v4l2_buf.sequence = pcdev->frame_count;
  1139. if (err)
  1140. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  1141. else
  1142. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  1143. }
  1144. pcdev->frame_count++;
  1145. if (list_empty(&pcdev->capture)) {
  1146. if (list_empty(&pcdev->discard)) {
  1147. dev_warn(pcdev->dev, "%s: trying to access empty discard list\n",
  1148. __func__);
  1149. return;
  1150. }
  1151. ibuf = list_first_entry(&pcdev->discard,
  1152. struct mx2_buf_internal, queue);
  1153. ibuf->bufnum = bufnum;
  1154. list_move_tail(pcdev->discard.next, &pcdev->active_bufs);
  1155. mx27_update_emma_buf(pcdev, pcdev->discard_buffer_dma, bufnum);
  1156. return;
  1157. }
  1158. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  1159. internal.queue);
  1160. buf->internal.bufnum = bufnum;
  1161. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  1162. vb = &buf->vb;
  1163. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  1164. mx27_update_emma_buf(pcdev, phys, bufnum);
  1165. }
  1166. static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
  1167. {
  1168. struct mx2_camera_dev *pcdev = data;
  1169. unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
  1170. struct mx2_buf_internal *ibuf;
  1171. spin_lock(&pcdev->lock);
  1172. if (list_empty(&pcdev->active_bufs)) {
  1173. dev_warn(pcdev->dev, "%s: called while active list is empty\n",
  1174. __func__);
  1175. if (!status) {
  1176. spin_unlock(&pcdev->lock);
  1177. return IRQ_NONE;
  1178. }
  1179. }
  1180. if (status & (1 << 7)) { /* overflow */
  1181. u32 cntl = readl(pcdev->base_emma + PRP_CNTL);
  1182. writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN),
  1183. pcdev->base_emma + PRP_CNTL);
  1184. writel(cntl, pcdev->base_emma + PRP_CNTL);
  1185. ibuf = list_first_entry(&pcdev->active_bufs,
  1186. struct mx2_buf_internal, queue);
  1187. mx27_camera_frame_done_emma(pcdev,
  1188. ibuf->bufnum, true);
  1189. status &= ~(1 << 7);
  1190. } else if (((status & (3 << 5)) == (3 << 5)) ||
  1191. ((status & (3 << 3)) == (3 << 3))) {
  1192. /*
  1193. * Both buffers have triggered, process the one we're expecting
  1194. * to first
  1195. */
  1196. ibuf = list_first_entry(&pcdev->active_bufs,
  1197. struct mx2_buf_internal, queue);
  1198. mx27_camera_frame_done_emma(pcdev, ibuf->bufnum, false);
  1199. status &= ~(1 << (6 - ibuf->bufnum)); /* mark processed */
  1200. } else if ((status & (1 << 6)) || (status & (1 << 4))) {
  1201. mx27_camera_frame_done_emma(pcdev, 0, false);
  1202. } else if ((status & (1 << 5)) || (status & (1 << 3))) {
  1203. mx27_camera_frame_done_emma(pcdev, 1, false);
  1204. }
  1205. spin_unlock(&pcdev->lock);
  1206. writel(status, pcdev->base_emma + PRP_INTRSTATUS);
  1207. return IRQ_HANDLED;
  1208. }
  1209. static int mx27_camera_emma_init(struct platform_device *pdev)
  1210. {
  1211. struct mx2_camera_dev *pcdev = platform_get_drvdata(pdev);
  1212. struct resource *res_emma;
  1213. int irq_emma;
  1214. int err = 0;
  1215. res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1216. irq_emma = platform_get_irq(pdev, 1);
  1217. if (!res_emma || !irq_emma) {
  1218. dev_err(pcdev->dev, "no EMMA resources\n");
  1219. err = -ENODEV;
  1220. goto out;
  1221. }
  1222. pcdev->base_emma = devm_ioremap_resource(pcdev->dev, res_emma);
  1223. if (IS_ERR(pcdev->base_emma)) {
  1224. err = PTR_ERR(pcdev->base_emma);
  1225. goto out;
  1226. }
  1227. err = devm_request_irq(pcdev->dev, irq_emma, mx27_camera_emma_irq, 0,
  1228. MX2_CAM_DRV_NAME, pcdev);
  1229. if (err) {
  1230. dev_err(pcdev->dev, "Camera EMMA interrupt register failed\n");
  1231. goto out;
  1232. }
  1233. pcdev->clk_emma_ipg = devm_clk_get(pcdev->dev, "emma-ipg");
  1234. if (IS_ERR(pcdev->clk_emma_ipg)) {
  1235. err = PTR_ERR(pcdev->clk_emma_ipg);
  1236. goto out;
  1237. }
  1238. clk_prepare_enable(pcdev->clk_emma_ipg);
  1239. pcdev->clk_emma_ahb = devm_clk_get(pcdev->dev, "emma-ahb");
  1240. if (IS_ERR(pcdev->clk_emma_ahb)) {
  1241. err = PTR_ERR(pcdev->clk_emma_ahb);
  1242. goto exit_clk_emma_ipg;
  1243. }
  1244. clk_prepare_enable(pcdev->clk_emma_ahb);
  1245. err = mx27_camera_emma_prp_reset(pcdev);
  1246. if (err)
  1247. goto exit_clk_emma_ahb;
  1248. return err;
  1249. exit_clk_emma_ahb:
  1250. clk_disable_unprepare(pcdev->clk_emma_ahb);
  1251. exit_clk_emma_ipg:
  1252. clk_disable_unprepare(pcdev->clk_emma_ipg);
  1253. out:
  1254. return err;
  1255. }
  1256. static int mx2_camera_probe(struct platform_device *pdev)
  1257. {
  1258. struct mx2_camera_dev *pcdev;
  1259. struct resource *res_csi;
  1260. int irq_csi;
  1261. int err = 0;
  1262. dev_dbg(&pdev->dev, "initialising\n");
  1263. res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1264. irq_csi = platform_get_irq(pdev, 0);
  1265. if (res_csi == NULL || irq_csi < 0) {
  1266. dev_err(&pdev->dev, "Missing platform resources data\n");
  1267. err = -ENODEV;
  1268. goto exit;
  1269. }
  1270. pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
  1271. if (!pcdev) {
  1272. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1273. err = -ENOMEM;
  1274. goto exit;
  1275. }
  1276. pcdev->clk_csi_ahb = devm_clk_get(&pdev->dev, "ahb");
  1277. if (IS_ERR(pcdev->clk_csi_ahb)) {
  1278. dev_err(&pdev->dev, "Could not get csi ahb clock\n");
  1279. err = PTR_ERR(pcdev->clk_csi_ahb);
  1280. goto exit;
  1281. }
  1282. pcdev->clk_csi_per = devm_clk_get(&pdev->dev, "per");
  1283. if (IS_ERR(pcdev->clk_csi_per)) {
  1284. dev_err(&pdev->dev, "Could not get csi per clock\n");
  1285. err = PTR_ERR(pcdev->clk_csi_per);
  1286. goto exit;
  1287. }
  1288. pcdev->pdata = pdev->dev.platform_data;
  1289. if (pcdev->pdata) {
  1290. long rate;
  1291. pcdev->platform_flags = pcdev->pdata->flags;
  1292. rate = clk_round_rate(pcdev->clk_csi_per,
  1293. pcdev->pdata->clk * 2);
  1294. if (rate <= 0) {
  1295. err = -ENODEV;
  1296. goto exit;
  1297. }
  1298. err = clk_set_rate(pcdev->clk_csi_per, rate);
  1299. if (err < 0)
  1300. goto exit;
  1301. }
  1302. INIT_LIST_HEAD(&pcdev->capture);
  1303. INIT_LIST_HEAD(&pcdev->active_bufs);
  1304. INIT_LIST_HEAD(&pcdev->discard);
  1305. spin_lock_init(&pcdev->lock);
  1306. pcdev->base_csi = devm_ioremap_resource(&pdev->dev, res_csi);
  1307. if (IS_ERR(pcdev->base_csi)) {
  1308. err = PTR_ERR(pcdev->base_csi);
  1309. goto exit;
  1310. }
  1311. pcdev->dev = &pdev->dev;
  1312. platform_set_drvdata(pdev, pcdev);
  1313. err = mx27_camera_emma_init(pdev);
  1314. if (err)
  1315. goto exit;
  1316. /*
  1317. * We're done with drvdata here. Clear the pointer so that
  1318. * v4l2 core can start using drvdata on its purpose.
  1319. */
  1320. platform_set_drvdata(pdev, NULL);
  1321. pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
  1322. pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
  1323. pcdev->soc_host.priv = pcdev;
  1324. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1325. pcdev->soc_host.nr = pdev->id;
  1326. pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1327. if (IS_ERR(pcdev->alloc_ctx)) {
  1328. err = PTR_ERR(pcdev->alloc_ctx);
  1329. goto eallocctx;
  1330. }
  1331. err = soc_camera_host_register(&pcdev->soc_host);
  1332. if (err)
  1333. goto exit_free_emma;
  1334. dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
  1335. clk_get_rate(pcdev->clk_csi_per));
  1336. return 0;
  1337. exit_free_emma:
  1338. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1339. eallocctx:
  1340. clk_disable_unprepare(pcdev->clk_emma_ipg);
  1341. clk_disable_unprepare(pcdev->clk_emma_ahb);
  1342. exit:
  1343. return err;
  1344. }
  1345. static int mx2_camera_remove(struct platform_device *pdev)
  1346. {
  1347. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1348. struct mx2_camera_dev *pcdev = container_of(soc_host,
  1349. struct mx2_camera_dev, soc_host);
  1350. soc_camera_host_unregister(&pcdev->soc_host);
  1351. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1352. clk_disable_unprepare(pcdev->clk_emma_ipg);
  1353. clk_disable_unprepare(pcdev->clk_emma_ahb);
  1354. dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
  1355. return 0;
  1356. }
  1357. static struct platform_driver mx2_camera_driver = {
  1358. .driver = {
  1359. .name = MX2_CAM_DRV_NAME,
  1360. },
  1361. .id_table = mx2_camera_devtype,
  1362. .remove = mx2_camera_remove,
  1363. };
  1364. module_platform_driver_probe(mx2_camera_driver, mx2_camera_probe);
  1365. MODULE_DESCRIPTION("i.MX27 SoC Camera Host driver");
  1366. MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
  1367. MODULE_LICENSE("GPL");
  1368. MODULE_VERSION(MX2_CAM_VERSION);