mx2_camera.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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 soc_camera_device *icd;
  214. struct clk *clk_emma_ahb, *clk_emma_ipg;
  215. struct clk *clk_csi_ahb, *clk_csi_per;
  216. void __iomem *base_csi, *base_emma;
  217. struct mx2_camera_platform_data *pdata;
  218. unsigned long platform_flags;
  219. struct list_head capture;
  220. struct list_head active_bufs;
  221. struct list_head discard;
  222. spinlock_t lock;
  223. int dma;
  224. struct mx2_buffer *active;
  225. struct mx2_buffer *fb1_active;
  226. struct mx2_buffer *fb2_active;
  227. u32 csicr1;
  228. enum mx2_camera_type devtype;
  229. struct mx2_buf_internal buf_discard[2];
  230. void *discard_buffer;
  231. dma_addr_t discard_buffer_dma;
  232. size_t discard_size;
  233. struct mx2_fmt_cfg *emma_prp;
  234. struct emma_prp_resize resizing[2];
  235. unsigned int s_width, s_height;
  236. u32 frame_count;
  237. struct vb2_alloc_ctx *alloc_ctx;
  238. };
  239. static struct platform_device_id mx2_camera_devtype[] = {
  240. {
  241. .name = "imx27-camera",
  242. .driver_data = IMX27_CAMERA,
  243. }, {
  244. /* sentinel */
  245. }
  246. };
  247. MODULE_DEVICE_TABLE(platform, mx2_camera_devtype);
  248. static struct mx2_buffer *mx2_ibuf_to_buf(struct mx2_buf_internal *int_buf)
  249. {
  250. return container_of(int_buf, struct mx2_buffer, internal);
  251. }
  252. static struct mx2_fmt_cfg mx27_emma_prp_table[] = {
  253. /*
  254. * This is a generic configuration which is valid for most
  255. * prp input-output format combinations.
  256. * We set the incoming and outgoing pixelformat to a
  257. * 16 Bit wide format and adjust the bytesperline
  258. * accordingly. With this configuration the inputdata
  259. * will not be changed by the emma and could be any type
  260. * of 16 Bit Pixelformat.
  261. */
  262. {
  263. .in_fmt = 0,
  264. .out_fmt = 0,
  265. .cfg = {
  266. .channel = 1,
  267. .in_fmt = PRP_CNTL_DATA_IN_RGB16,
  268. .out_fmt = PRP_CNTL_CH1_OUT_RGB16,
  269. .src_pixel = 0x2ca00565, /* RGB565 */
  270. .ch1_pixel = 0x2ca00565, /* RGB565 */
  271. .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
  272. PRP_INTR_CH1FC | PRP_INTR_LBOVF,
  273. .csicr1 = 0,
  274. }
  275. },
  276. {
  277. .in_fmt = V4L2_MBUS_FMT_UYVY8_2X8,
  278. .out_fmt = V4L2_PIX_FMT_YUYV,
  279. .cfg = {
  280. .channel = 1,
  281. .in_fmt = PRP_CNTL_DATA_IN_YUV422,
  282. .out_fmt = PRP_CNTL_CH1_OUT_YUV422,
  283. .src_pixel = 0x22000888, /* YUV422 (YUYV) */
  284. .ch1_pixel = 0x62000888, /* YUV422 (YUYV) */
  285. .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
  286. PRP_INTR_CH1FC | PRP_INTR_LBOVF,
  287. .csicr1 = CSICR1_SWAP16_EN,
  288. }
  289. },
  290. {
  291. .in_fmt = V4L2_MBUS_FMT_YUYV8_2X8,
  292. .out_fmt = V4L2_PIX_FMT_YUYV,
  293. .cfg = {
  294. .channel = 1,
  295. .in_fmt = PRP_CNTL_DATA_IN_YUV422,
  296. .out_fmt = PRP_CNTL_CH1_OUT_YUV422,
  297. .src_pixel = 0x22000888, /* YUV422 (YUYV) */
  298. .ch1_pixel = 0x62000888, /* YUV422 (YUYV) */
  299. .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
  300. PRP_INTR_CH1FC | PRP_INTR_LBOVF,
  301. .csicr1 = CSICR1_PACK_DIR,
  302. }
  303. },
  304. {
  305. .in_fmt = V4L2_MBUS_FMT_YUYV8_2X8,
  306. .out_fmt = V4L2_PIX_FMT_YUV420,
  307. .cfg = {
  308. .channel = 2,
  309. .in_fmt = PRP_CNTL_DATA_IN_YUV422,
  310. .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
  311. .src_pixel = 0x22000888, /* YUV422 (YUYV) */
  312. .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
  313. PRP_INTR_CH2FC | PRP_INTR_LBOVF |
  314. PRP_INTR_CH2OVF,
  315. .csicr1 = CSICR1_PACK_DIR,
  316. }
  317. },
  318. {
  319. .in_fmt = V4L2_MBUS_FMT_UYVY8_2X8,
  320. .out_fmt = V4L2_PIX_FMT_YUV420,
  321. .cfg = {
  322. .channel = 2,
  323. .in_fmt = PRP_CNTL_DATA_IN_YUV422,
  324. .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
  325. .src_pixel = 0x22000888, /* YUV422 (YUYV) */
  326. .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
  327. PRP_INTR_CH2FC | PRP_INTR_LBOVF |
  328. PRP_INTR_CH2OVF,
  329. .csicr1 = CSICR1_SWAP16_EN,
  330. }
  331. },
  332. };
  333. static struct mx2_fmt_cfg *mx27_emma_prp_get_format(
  334. enum v4l2_mbus_pixelcode in_fmt,
  335. u32 out_fmt)
  336. {
  337. int i;
  338. for (i = 1; i < ARRAY_SIZE(mx27_emma_prp_table); i++)
  339. if ((mx27_emma_prp_table[i].in_fmt == in_fmt) &&
  340. (mx27_emma_prp_table[i].out_fmt == out_fmt)) {
  341. return &mx27_emma_prp_table[i];
  342. }
  343. /* If no match return the most generic configuration */
  344. return &mx27_emma_prp_table[0];
  345. };
  346. static void mx27_update_emma_buf(struct mx2_camera_dev *pcdev,
  347. unsigned long phys, int bufnum)
  348. {
  349. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  350. if (prp->cfg.channel == 1) {
  351. writel(phys, pcdev->base_emma +
  352. PRP_DEST_RGB1_PTR + 4 * bufnum);
  353. } else {
  354. writel(phys, pcdev->base_emma +
  355. PRP_DEST_Y_PTR - 0x14 * bufnum);
  356. if (prp->out_fmt == V4L2_PIX_FMT_YUV420) {
  357. u32 imgsize = pcdev->icd->user_height *
  358. pcdev->icd->user_width;
  359. writel(phys + imgsize, pcdev->base_emma +
  360. PRP_DEST_CB_PTR - 0x14 * bufnum);
  361. writel(phys + ((5 * imgsize) / 4), pcdev->base_emma +
  362. PRP_DEST_CR_PTR - 0x14 * bufnum);
  363. }
  364. }
  365. }
  366. static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev)
  367. {
  368. clk_disable_unprepare(pcdev->clk_csi_ahb);
  369. clk_disable_unprepare(pcdev->clk_csi_per);
  370. writel(0, pcdev->base_csi + CSICR1);
  371. writel(0, pcdev->base_emma + PRP_CNTL);
  372. }
  373. /*
  374. * The following two functions absolutely depend on the fact, that
  375. * there can be only one camera on mx2 camera sensor interface
  376. */
  377. static int mx2_camera_add_device(struct soc_camera_device *icd)
  378. {
  379. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  380. struct mx2_camera_dev *pcdev = ici->priv;
  381. int ret;
  382. u32 csicr1;
  383. if (pcdev->icd)
  384. return -EBUSY;
  385. ret = clk_prepare_enable(pcdev->clk_csi_ahb);
  386. if (ret < 0)
  387. return ret;
  388. ret = clk_prepare_enable(pcdev->clk_csi_per);
  389. if (ret < 0)
  390. goto exit_csi_ahb;
  391. csicr1 = CSICR1_MCLKEN | CSICR1_PRP_IF_EN | CSICR1_FCC |
  392. CSICR1_RXFF_LEVEL(0);
  393. pcdev->csicr1 = csicr1;
  394. writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
  395. pcdev->icd = icd;
  396. pcdev->frame_count = 0;
  397. dev_info(icd->parent, "Camera driver attached to camera %d\n",
  398. icd->devnum);
  399. return 0;
  400. exit_csi_ahb:
  401. clk_disable_unprepare(pcdev->clk_csi_ahb);
  402. return ret;
  403. }
  404. static void mx2_camera_remove_device(struct soc_camera_device *icd)
  405. {
  406. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  407. struct mx2_camera_dev *pcdev = ici->priv;
  408. BUG_ON(icd != pcdev->icd);
  409. dev_info(icd->parent, "Camera driver detached from camera %d\n",
  410. icd->devnum);
  411. mx2_camera_deactivate(pcdev);
  412. pcdev->icd = NULL;
  413. }
  414. /*
  415. * Videobuf operations
  416. */
  417. static int mx2_videobuf_setup(struct vb2_queue *vq,
  418. const struct v4l2_format *fmt,
  419. unsigned int *count, unsigned int *num_planes,
  420. unsigned int sizes[], void *alloc_ctxs[])
  421. {
  422. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  423. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  424. struct mx2_camera_dev *pcdev = ici->priv;
  425. dev_dbg(icd->parent, "count=%d, size=%d\n", *count, sizes[0]);
  426. /* TODO: support for VIDIOC_CREATE_BUFS not ready */
  427. if (fmt != NULL)
  428. return -ENOTTY;
  429. alloc_ctxs[0] = pcdev->alloc_ctx;
  430. sizes[0] = icd->sizeimage;
  431. if (0 == *count)
  432. *count = 32;
  433. if (!*num_planes &&
  434. sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024)
  435. *count = (MAX_VIDEO_MEM * 1024 * 1024) / sizes[0];
  436. *num_planes = 1;
  437. return 0;
  438. }
  439. static int mx2_videobuf_prepare(struct vb2_buffer *vb)
  440. {
  441. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  442. int ret = 0;
  443. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  444. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  445. #ifdef DEBUG
  446. /*
  447. * This can be useful if you want to see if we actually fill
  448. * the buffer with something
  449. */
  450. memset((void *)vb2_plane_vaddr(vb, 0),
  451. 0xaa, vb2_get_plane_payload(vb, 0));
  452. #endif
  453. vb2_set_plane_payload(vb, 0, icd->sizeimage);
  454. if (vb2_plane_vaddr(vb, 0) &&
  455. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  456. ret = -EINVAL;
  457. goto out;
  458. }
  459. return 0;
  460. out:
  461. return ret;
  462. }
  463. static void mx2_videobuf_queue(struct vb2_buffer *vb)
  464. {
  465. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  466. struct soc_camera_host *ici =
  467. to_soc_camera_host(icd->parent);
  468. struct mx2_camera_dev *pcdev = ici->priv;
  469. struct mx2_buffer *buf = container_of(vb, struct mx2_buffer, vb);
  470. unsigned long flags;
  471. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
  472. vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
  473. spin_lock_irqsave(&pcdev->lock, flags);
  474. list_add_tail(&buf->internal.queue, &pcdev->capture);
  475. spin_unlock_irqrestore(&pcdev->lock, flags);
  476. }
  477. static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
  478. int bytesperline)
  479. {
  480. struct soc_camera_host *ici =
  481. to_soc_camera_host(icd->parent);
  482. struct mx2_camera_dev *pcdev = ici->priv;
  483. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  484. writel((pcdev->s_width << 16) | pcdev->s_height,
  485. pcdev->base_emma + PRP_SRC_FRAME_SIZE);
  486. writel(prp->cfg.src_pixel,
  487. pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
  488. if (prp->cfg.channel == 1) {
  489. writel((icd->user_width << 16) | icd->user_height,
  490. pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
  491. writel(bytesperline,
  492. pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
  493. writel(prp->cfg.ch1_pixel,
  494. pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
  495. } else { /* channel 2 */
  496. writel((icd->user_width << 16) | icd->user_height,
  497. pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
  498. }
  499. /* Enable interrupts */
  500. writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL);
  501. }
  502. static void mx2_prp_resize_commit(struct mx2_camera_dev *pcdev)
  503. {
  504. int dir;
  505. for (dir = RESIZE_DIR_H; dir <= RESIZE_DIR_V; dir++) {
  506. unsigned char *s = pcdev->resizing[dir].s;
  507. int len = pcdev->resizing[dir].len;
  508. unsigned int coeff[2] = {0, 0};
  509. unsigned int valid = 0;
  510. int i;
  511. if (len == 0)
  512. continue;
  513. for (i = RESIZE_NUM_MAX - 1; i >= 0; i--) {
  514. int j;
  515. j = i > 9 ? 1 : 0;
  516. coeff[j] = (coeff[j] << BC_COEF) |
  517. (s[i] & (SZ_COEF - 1));
  518. if (i == 5 || i == 15)
  519. coeff[j] <<= 1;
  520. valid = (valid << 1) | (s[i] >> BC_COEF);
  521. }
  522. valid |= PRP_RZ_VALID_TBL_LEN(len);
  523. if (pcdev->resizing[dir].algo == RESIZE_ALGO_BILINEAR)
  524. valid |= PRP_RZ_VALID_BILINEAR;
  525. if (pcdev->emma_prp->cfg.channel == 1) {
  526. if (dir == RESIZE_DIR_H) {
  527. writel(coeff[0], pcdev->base_emma +
  528. PRP_CH1_RZ_HORI_COEF1);
  529. writel(coeff[1], pcdev->base_emma +
  530. PRP_CH1_RZ_HORI_COEF2);
  531. writel(valid, pcdev->base_emma +
  532. PRP_CH1_RZ_HORI_VALID);
  533. } else {
  534. writel(coeff[0], pcdev->base_emma +
  535. PRP_CH1_RZ_VERT_COEF1);
  536. writel(coeff[1], pcdev->base_emma +
  537. PRP_CH1_RZ_VERT_COEF2);
  538. writel(valid, pcdev->base_emma +
  539. PRP_CH1_RZ_VERT_VALID);
  540. }
  541. } else {
  542. if (dir == RESIZE_DIR_H) {
  543. writel(coeff[0], pcdev->base_emma +
  544. PRP_CH2_RZ_HORI_COEF1);
  545. writel(coeff[1], pcdev->base_emma +
  546. PRP_CH2_RZ_HORI_COEF2);
  547. writel(valid, pcdev->base_emma +
  548. PRP_CH2_RZ_HORI_VALID);
  549. } else {
  550. writel(coeff[0], pcdev->base_emma +
  551. PRP_CH2_RZ_VERT_COEF1);
  552. writel(coeff[1], pcdev->base_emma +
  553. PRP_CH2_RZ_VERT_COEF2);
  554. writel(valid, pcdev->base_emma +
  555. PRP_CH2_RZ_VERT_VALID);
  556. }
  557. }
  558. }
  559. }
  560. static int mx2_start_streaming(struct vb2_queue *q, unsigned int count)
  561. {
  562. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  563. struct soc_camera_host *ici =
  564. to_soc_camera_host(icd->parent);
  565. struct mx2_camera_dev *pcdev = ici->priv;
  566. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  567. struct vb2_buffer *vb;
  568. struct mx2_buffer *buf;
  569. unsigned long phys;
  570. int bytesperline;
  571. unsigned long flags;
  572. if (count < 2)
  573. return -EINVAL;
  574. spin_lock_irqsave(&pcdev->lock, flags);
  575. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  576. internal.queue);
  577. buf->internal.bufnum = 0;
  578. vb = &buf->vb;
  579. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  580. mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
  581. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  582. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  583. internal.queue);
  584. buf->internal.bufnum = 1;
  585. vb = &buf->vb;
  586. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  587. mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
  588. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  589. bytesperline = soc_mbus_bytes_per_line(icd->user_width,
  590. icd->current_fmt->host_fmt);
  591. if (bytesperline < 0) {
  592. spin_unlock_irqrestore(&pcdev->lock, flags);
  593. return bytesperline;
  594. }
  595. /*
  596. * I didn't manage to properly enable/disable the prp
  597. * on a per frame basis during running transfers,
  598. * thus we allocate a buffer here and use it to
  599. * discard frames when no buffer is available.
  600. * Feel free to work on this ;)
  601. */
  602. pcdev->discard_size = icd->user_height * bytesperline;
  603. pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
  604. pcdev->discard_size,
  605. &pcdev->discard_buffer_dma, GFP_ATOMIC);
  606. if (!pcdev->discard_buffer) {
  607. spin_unlock_irqrestore(&pcdev->lock, flags);
  608. return -ENOMEM;
  609. }
  610. pcdev->buf_discard[0].discard = true;
  611. list_add_tail(&pcdev->buf_discard[0].queue,
  612. &pcdev->discard);
  613. pcdev->buf_discard[1].discard = true;
  614. list_add_tail(&pcdev->buf_discard[1].queue,
  615. &pcdev->discard);
  616. mx2_prp_resize_commit(pcdev);
  617. mx27_camera_emma_buf_init(icd, bytesperline);
  618. if (prp->cfg.channel == 1) {
  619. writel(PRP_CNTL_CH1EN |
  620. PRP_CNTL_CSIEN |
  621. prp->cfg.in_fmt |
  622. prp->cfg.out_fmt |
  623. PRP_CNTL_CH1_LEN |
  624. PRP_CNTL_CH1BYP |
  625. PRP_CNTL_CH1_TSKIP(0) |
  626. PRP_CNTL_IN_TSKIP(0),
  627. pcdev->base_emma + PRP_CNTL);
  628. } else {
  629. writel(PRP_CNTL_CH2EN |
  630. PRP_CNTL_CSIEN |
  631. prp->cfg.in_fmt |
  632. prp->cfg.out_fmt |
  633. PRP_CNTL_CH2_LEN |
  634. PRP_CNTL_CH2_TSKIP(0) |
  635. PRP_CNTL_IN_TSKIP(0),
  636. pcdev->base_emma + PRP_CNTL);
  637. }
  638. spin_unlock_irqrestore(&pcdev->lock, flags);
  639. return 0;
  640. }
  641. static int mx2_stop_streaming(struct vb2_queue *q)
  642. {
  643. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  644. struct soc_camera_host *ici =
  645. to_soc_camera_host(icd->parent);
  646. struct mx2_camera_dev *pcdev = ici->priv;
  647. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  648. unsigned long flags;
  649. void *b;
  650. u32 cntl;
  651. spin_lock_irqsave(&pcdev->lock, flags);
  652. cntl = readl(pcdev->base_emma + PRP_CNTL);
  653. if (prp->cfg.channel == 1) {
  654. writel(cntl & ~PRP_CNTL_CH1EN,
  655. pcdev->base_emma + PRP_CNTL);
  656. } else {
  657. writel(cntl & ~PRP_CNTL_CH2EN,
  658. pcdev->base_emma + PRP_CNTL);
  659. }
  660. INIT_LIST_HEAD(&pcdev->capture);
  661. INIT_LIST_HEAD(&pcdev->active_bufs);
  662. INIT_LIST_HEAD(&pcdev->discard);
  663. b = pcdev->discard_buffer;
  664. pcdev->discard_buffer = NULL;
  665. spin_unlock_irqrestore(&pcdev->lock, flags);
  666. dma_free_coherent(ici->v4l2_dev.dev,
  667. pcdev->discard_size, b, pcdev->discard_buffer_dma);
  668. return 0;
  669. }
  670. static struct vb2_ops mx2_videobuf_ops = {
  671. .queue_setup = mx2_videobuf_setup,
  672. .buf_prepare = mx2_videobuf_prepare,
  673. .buf_queue = mx2_videobuf_queue,
  674. .start_streaming = mx2_start_streaming,
  675. .stop_streaming = mx2_stop_streaming,
  676. };
  677. static int mx2_camera_init_videobuf(struct vb2_queue *q,
  678. struct soc_camera_device *icd)
  679. {
  680. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  681. q->io_modes = VB2_MMAP | VB2_USERPTR;
  682. q->drv_priv = icd;
  683. q->ops = &mx2_videobuf_ops;
  684. q->mem_ops = &vb2_dma_contig_memops;
  685. q->buf_struct_size = sizeof(struct mx2_buffer);
  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. .set_fmt = mx2_camera_set_fmt,
  1090. .set_crop = mx2_camera_set_crop,
  1091. .get_formats = mx2_camera_get_formats,
  1092. .try_fmt = mx2_camera_try_fmt,
  1093. .init_videobuf2 = mx2_camera_init_videobuf,
  1094. .poll = mx2_camera_poll,
  1095. .querycap = mx2_camera_querycap,
  1096. .set_bus_param = mx2_camera_set_bus_param,
  1097. };
  1098. static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
  1099. int bufnum, bool err)
  1100. {
  1101. #ifdef DEBUG
  1102. struct mx2_fmt_cfg *prp = pcdev->emma_prp;
  1103. #endif
  1104. struct mx2_buf_internal *ibuf;
  1105. struct mx2_buffer *buf;
  1106. struct vb2_buffer *vb;
  1107. unsigned long phys;
  1108. ibuf = list_first_entry(&pcdev->active_bufs, struct mx2_buf_internal,
  1109. queue);
  1110. BUG_ON(ibuf->bufnum != bufnum);
  1111. if (ibuf->discard) {
  1112. /*
  1113. * Discard buffer must not be returned to user space.
  1114. * Just return it to the discard queue.
  1115. */
  1116. list_move_tail(pcdev->active_bufs.next, &pcdev->discard);
  1117. } else {
  1118. buf = mx2_ibuf_to_buf(ibuf);
  1119. vb = &buf->vb;
  1120. #ifdef DEBUG
  1121. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  1122. if (prp->cfg.channel == 1) {
  1123. if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR +
  1124. 4 * bufnum) != phys) {
  1125. dev_err(pcdev->dev, "%lx != %x\n", phys,
  1126. readl(pcdev->base_emma +
  1127. PRP_DEST_RGB1_PTR + 4 * bufnum));
  1128. }
  1129. } else {
  1130. if (readl(pcdev->base_emma + PRP_DEST_Y_PTR -
  1131. 0x14 * bufnum) != phys) {
  1132. dev_err(pcdev->dev, "%lx != %x\n", phys,
  1133. readl(pcdev->base_emma +
  1134. PRP_DEST_Y_PTR - 0x14 * bufnum));
  1135. }
  1136. }
  1137. #endif
  1138. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, vb,
  1139. vb2_plane_vaddr(vb, 0),
  1140. vb2_get_plane_payload(vb, 0));
  1141. list_del_init(&buf->internal.queue);
  1142. v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
  1143. vb->v4l2_buf.sequence = pcdev->frame_count;
  1144. if (err)
  1145. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  1146. else
  1147. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  1148. }
  1149. pcdev->frame_count++;
  1150. if (list_empty(&pcdev->capture)) {
  1151. if (list_empty(&pcdev->discard)) {
  1152. dev_warn(pcdev->dev, "%s: trying to access empty discard list\n",
  1153. __func__);
  1154. return;
  1155. }
  1156. ibuf = list_first_entry(&pcdev->discard,
  1157. struct mx2_buf_internal, queue);
  1158. ibuf->bufnum = bufnum;
  1159. list_move_tail(pcdev->discard.next, &pcdev->active_bufs);
  1160. mx27_update_emma_buf(pcdev, pcdev->discard_buffer_dma, bufnum);
  1161. return;
  1162. }
  1163. buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
  1164. internal.queue);
  1165. buf->internal.bufnum = bufnum;
  1166. list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
  1167. vb = &buf->vb;
  1168. phys = vb2_dma_contig_plane_dma_addr(vb, 0);
  1169. mx27_update_emma_buf(pcdev, phys, bufnum);
  1170. }
  1171. static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
  1172. {
  1173. struct mx2_camera_dev *pcdev = data;
  1174. unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
  1175. struct mx2_buf_internal *ibuf;
  1176. spin_lock(&pcdev->lock);
  1177. if (list_empty(&pcdev->active_bufs)) {
  1178. dev_warn(pcdev->dev, "%s: called while active list is empty\n",
  1179. __func__);
  1180. if (!status) {
  1181. spin_unlock(&pcdev->lock);
  1182. return IRQ_NONE;
  1183. }
  1184. }
  1185. if (status & (1 << 7)) { /* overflow */
  1186. u32 cntl = readl(pcdev->base_emma + PRP_CNTL);
  1187. writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN),
  1188. pcdev->base_emma + PRP_CNTL);
  1189. writel(cntl, pcdev->base_emma + PRP_CNTL);
  1190. ibuf = list_first_entry(&pcdev->active_bufs,
  1191. struct mx2_buf_internal, queue);
  1192. mx27_camera_frame_done_emma(pcdev,
  1193. ibuf->bufnum, true);
  1194. status &= ~(1 << 7);
  1195. } else if (((status & (3 << 5)) == (3 << 5)) ||
  1196. ((status & (3 << 3)) == (3 << 3))) {
  1197. /*
  1198. * Both buffers have triggered, process the one we're expecting
  1199. * to first
  1200. */
  1201. ibuf = list_first_entry(&pcdev->active_bufs,
  1202. struct mx2_buf_internal, queue);
  1203. mx27_camera_frame_done_emma(pcdev, ibuf->bufnum, false);
  1204. status &= ~(1 << (6 - ibuf->bufnum)); /* mark processed */
  1205. } else if ((status & (1 << 6)) || (status & (1 << 4))) {
  1206. mx27_camera_frame_done_emma(pcdev, 0, false);
  1207. } else if ((status & (1 << 5)) || (status & (1 << 3))) {
  1208. mx27_camera_frame_done_emma(pcdev, 1, false);
  1209. }
  1210. spin_unlock(&pcdev->lock);
  1211. writel(status, pcdev->base_emma + PRP_INTRSTATUS);
  1212. return IRQ_HANDLED;
  1213. }
  1214. static int mx27_camera_emma_init(struct platform_device *pdev)
  1215. {
  1216. struct mx2_camera_dev *pcdev = platform_get_drvdata(pdev);
  1217. struct resource *res_emma;
  1218. int irq_emma;
  1219. int err = 0;
  1220. res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1221. irq_emma = platform_get_irq(pdev, 1);
  1222. if (!res_emma || !irq_emma) {
  1223. dev_err(pcdev->dev, "no EMMA resources\n");
  1224. err = -ENODEV;
  1225. goto out;
  1226. }
  1227. pcdev->base_emma = devm_ioremap_resource(pcdev->dev, res_emma);
  1228. if (IS_ERR(pcdev->base_emma)) {
  1229. err = PTR_ERR(pcdev->base_emma);
  1230. goto out;
  1231. }
  1232. err = devm_request_irq(pcdev->dev, irq_emma, mx27_camera_emma_irq, 0,
  1233. MX2_CAM_DRV_NAME, pcdev);
  1234. if (err) {
  1235. dev_err(pcdev->dev, "Camera EMMA interrupt register failed \n");
  1236. goto out;
  1237. }
  1238. pcdev->clk_emma_ipg = devm_clk_get(pcdev->dev, "emma-ipg");
  1239. if (IS_ERR(pcdev->clk_emma_ipg)) {
  1240. err = PTR_ERR(pcdev->clk_emma_ipg);
  1241. goto out;
  1242. }
  1243. clk_prepare_enable(pcdev->clk_emma_ipg);
  1244. pcdev->clk_emma_ahb = devm_clk_get(pcdev->dev, "emma-ahb");
  1245. if (IS_ERR(pcdev->clk_emma_ahb)) {
  1246. err = PTR_ERR(pcdev->clk_emma_ahb);
  1247. goto exit_clk_emma_ipg;
  1248. }
  1249. clk_prepare_enable(pcdev->clk_emma_ahb);
  1250. err = mx27_camera_emma_prp_reset(pcdev);
  1251. if (err)
  1252. goto exit_clk_emma_ahb;
  1253. return err;
  1254. exit_clk_emma_ahb:
  1255. clk_disable_unprepare(pcdev->clk_emma_ahb);
  1256. exit_clk_emma_ipg:
  1257. clk_disable_unprepare(pcdev->clk_emma_ipg);
  1258. out:
  1259. return err;
  1260. }
  1261. static int mx2_camera_probe(struct platform_device *pdev)
  1262. {
  1263. struct mx2_camera_dev *pcdev;
  1264. struct resource *res_csi;
  1265. int irq_csi;
  1266. int err = 0;
  1267. dev_dbg(&pdev->dev, "initialising\n");
  1268. res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1269. irq_csi = platform_get_irq(pdev, 0);
  1270. if (res_csi == NULL || irq_csi < 0) {
  1271. dev_err(&pdev->dev, "Missing platform resources data\n");
  1272. err = -ENODEV;
  1273. goto exit;
  1274. }
  1275. pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
  1276. if (!pcdev) {
  1277. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1278. err = -ENOMEM;
  1279. goto exit;
  1280. }
  1281. pcdev->clk_csi_ahb = devm_clk_get(&pdev->dev, "ahb");
  1282. if (IS_ERR(pcdev->clk_csi_ahb)) {
  1283. dev_err(&pdev->dev, "Could not get csi ahb clock\n");
  1284. err = PTR_ERR(pcdev->clk_csi_ahb);
  1285. goto exit;
  1286. }
  1287. pcdev->clk_csi_per = devm_clk_get(&pdev->dev, "per");
  1288. if (IS_ERR(pcdev->clk_csi_per)) {
  1289. dev_err(&pdev->dev, "Could not get csi per clock\n");
  1290. err = PTR_ERR(pcdev->clk_csi_per);
  1291. goto exit;
  1292. }
  1293. pcdev->pdata = pdev->dev.platform_data;
  1294. if (pcdev->pdata) {
  1295. long rate;
  1296. pcdev->platform_flags = pcdev->pdata->flags;
  1297. rate = clk_round_rate(pcdev->clk_csi_per,
  1298. pcdev->pdata->clk * 2);
  1299. if (rate <= 0) {
  1300. err = -ENODEV;
  1301. goto exit;
  1302. }
  1303. err = clk_set_rate(pcdev->clk_csi_per, rate);
  1304. if (err < 0)
  1305. goto exit;
  1306. }
  1307. INIT_LIST_HEAD(&pcdev->capture);
  1308. INIT_LIST_HEAD(&pcdev->active_bufs);
  1309. INIT_LIST_HEAD(&pcdev->discard);
  1310. spin_lock_init(&pcdev->lock);
  1311. pcdev->base_csi = devm_ioremap_resource(&pdev->dev, res_csi);
  1312. if (IS_ERR(pcdev->base_csi)) {
  1313. err = PTR_ERR(pcdev->base_csi);
  1314. goto exit;
  1315. }
  1316. pcdev->dev = &pdev->dev;
  1317. platform_set_drvdata(pdev, pcdev);
  1318. err = mx27_camera_emma_init(pdev);
  1319. if (err)
  1320. goto exit;
  1321. /*
  1322. * We're done with drvdata here. Clear the pointer so that
  1323. * v4l2 core can start using drvdata on its purpose.
  1324. */
  1325. platform_set_drvdata(pdev, NULL);
  1326. pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
  1327. pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
  1328. pcdev->soc_host.priv = pcdev;
  1329. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1330. pcdev->soc_host.nr = pdev->id;
  1331. pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1332. if (IS_ERR(pcdev->alloc_ctx)) {
  1333. err = PTR_ERR(pcdev->alloc_ctx);
  1334. goto eallocctx;
  1335. }
  1336. err = soc_camera_host_register(&pcdev->soc_host);
  1337. if (err)
  1338. goto exit_free_emma;
  1339. dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
  1340. clk_get_rate(pcdev->clk_csi_per));
  1341. return 0;
  1342. exit_free_emma:
  1343. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1344. eallocctx:
  1345. clk_disable_unprepare(pcdev->clk_emma_ipg);
  1346. clk_disable_unprepare(pcdev->clk_emma_ahb);
  1347. exit:
  1348. return err;
  1349. }
  1350. static int mx2_camera_remove(struct platform_device *pdev)
  1351. {
  1352. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1353. struct mx2_camera_dev *pcdev = container_of(soc_host,
  1354. struct mx2_camera_dev, soc_host);
  1355. soc_camera_host_unregister(&pcdev->soc_host);
  1356. vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
  1357. clk_disable_unprepare(pcdev->clk_emma_ipg);
  1358. clk_disable_unprepare(pcdev->clk_emma_ahb);
  1359. dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
  1360. return 0;
  1361. }
  1362. static struct platform_driver mx2_camera_driver = {
  1363. .driver = {
  1364. .name = MX2_CAM_DRV_NAME,
  1365. },
  1366. .id_table = mx2_camera_devtype,
  1367. .remove = mx2_camera_remove,
  1368. .probe = mx2_camera_probe,
  1369. };
  1370. module_platform_driver(mx2_camera_driver);
  1371. MODULE_DESCRIPTION("i.MX27 SoC Camera Host driver");
  1372. MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
  1373. MODULE_LICENSE("GPL");
  1374. MODULE_VERSION(MX2_CAM_VERSION);