mcam-core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Marvell camera core structures.
  3. *
  4. * Copyright 2011 Jonathan Corbet corbet@lwn.net
  5. */
  6. #ifndef _MCAM_CORE_H
  7. #define _MCAM_CORE_H
  8. #include <linux/list.h>
  9. #include <media/v4l2-common.h>
  10. #include <media/v4l2-ctrls.h>
  11. #include <media/v4l2-dev.h>
  12. #include <media/videobuf2-v4l2.h>
  13. /*
  14. * Create our own symbols for the supported buffer modes, but, for now,
  15. * base them entirely on which videobuf2 options have been selected.
  16. */
  17. #if IS_ENABLED(CONFIG_VIDEOBUF2_VMALLOC)
  18. #define MCAM_MODE_VMALLOC 1
  19. #endif
  20. #if IS_ENABLED(CONFIG_VIDEOBUF2_DMA_CONTIG)
  21. #define MCAM_MODE_DMA_CONTIG 1
  22. #endif
  23. #if IS_ENABLED(CONFIG_VIDEOBUF2_DMA_SG)
  24. #define MCAM_MODE_DMA_SG 1
  25. #endif
  26. #if !defined(MCAM_MODE_VMALLOC) && !defined(MCAM_MODE_DMA_CONTIG) && \
  27. !defined(MCAM_MODE_DMA_SG)
  28. #error One of the videobuf buffer modes must be selected in the config
  29. #endif
  30. enum mcam_state {
  31. S_NOTREADY, /* Not yet initialized */
  32. S_IDLE, /* Just hanging around */
  33. S_FLAKED, /* Some sort of problem */
  34. S_STREAMING, /* Streaming data */
  35. S_BUFWAIT /* streaming requested but no buffers yet */
  36. };
  37. #define MAX_DMA_BUFS 3
  38. /*
  39. * Different platforms work best with different buffer modes, so we
  40. * let the platform pick.
  41. */
  42. enum mcam_buffer_mode {
  43. B_vmalloc = 0,
  44. B_DMA_contig = 1,
  45. B_DMA_sg = 2
  46. };
  47. enum mcam_chip_id {
  48. MCAM_CAFE,
  49. MCAM_ARMADA610,
  50. };
  51. /*
  52. * Is a given buffer mode supported by the current kernel configuration?
  53. */
  54. static inline int mcam_buffer_mode_supported(enum mcam_buffer_mode mode)
  55. {
  56. switch (mode) {
  57. #ifdef MCAM_MODE_VMALLOC
  58. case B_vmalloc:
  59. #endif
  60. #ifdef MCAM_MODE_DMA_CONTIG
  61. case B_DMA_contig:
  62. #endif
  63. #ifdef MCAM_MODE_DMA_SG
  64. case B_DMA_sg:
  65. #endif
  66. return 1;
  67. default:
  68. return 0;
  69. }
  70. }
  71. /*
  72. * Basic frame states
  73. */
  74. struct mcam_frame_state {
  75. unsigned int frames;
  76. unsigned int singles;
  77. unsigned int delivered;
  78. };
  79. #define NR_MCAM_CLK 3
  80. /*
  81. * A description of one of our devices.
  82. * Locking: controlled by s_mutex. Certain fields, however, require
  83. * the dev_lock spinlock; they are marked as such by comments.
  84. * dev_lock is also required for access to device registers.
  85. */
  86. struct mcam_camera {
  87. /*
  88. * These fields should be set by the platform code prior to
  89. * calling mcam_register().
  90. */
  91. struct i2c_adapter *i2c_adapter;
  92. unsigned char __iomem *regs;
  93. unsigned regs_size; /* size in bytes of the register space */
  94. spinlock_t dev_lock;
  95. struct device *dev; /* For messages, dma alloc */
  96. enum mcam_chip_id chip_id;
  97. short int clock_speed; /* Sensor clock speed, default 30 */
  98. short int use_smbus; /* SMBUS or straight I2c? */
  99. enum mcam_buffer_mode buffer_mode;
  100. int mclk_min; /* The minimal value of mclk */
  101. int mclk_src; /* which clock source the mclk derives from */
  102. int mclk_div; /* Clock Divider Value for MCLK */
  103. int ccic_id;
  104. enum v4l2_mbus_type bus_type;
  105. /* MIPI support */
  106. /* The dphy config value, allocated in board file
  107. * dphy[0]: DPHY3
  108. * dphy[1]: DPHY5
  109. * dphy[2]: DPHY6
  110. */
  111. int *dphy;
  112. bool mipi_enabled; /* flag whether mipi is enabled already */
  113. int lane; /* lane number */
  114. /* clock tree support */
  115. struct clk *clk[NR_MCAM_CLK];
  116. /*
  117. * Callbacks from the core to the platform code.
  118. */
  119. int (*plat_power_up) (struct mcam_camera *cam);
  120. void (*plat_power_down) (struct mcam_camera *cam);
  121. void (*calc_dphy) (struct mcam_camera *cam);
  122. void (*ctlr_reset) (struct mcam_camera *cam);
  123. /*
  124. * Everything below here is private to the mcam core and
  125. * should not be touched by the platform code.
  126. */
  127. struct v4l2_device v4l2_dev;
  128. struct v4l2_ctrl_handler ctrl_handler;
  129. enum mcam_state state;
  130. unsigned long flags; /* Buffer status, mainly (dev_lock) */
  131. struct mcam_frame_state frame_state; /* Frame state counter */
  132. /*
  133. * Subsystem structures.
  134. */
  135. struct video_device vdev;
  136. struct v4l2_subdev *sensor;
  137. unsigned short sensor_addr;
  138. /* Videobuf2 stuff */
  139. struct vb2_queue vb_queue;
  140. struct list_head buffers; /* Available frames */
  141. unsigned int nbufs; /* How many are alloc'd */
  142. int next_buf; /* Next to consume (dev_lock) */
  143. char bus_info[32]; /* querycap bus_info */
  144. /* DMA buffers - vmalloc mode */
  145. #ifdef MCAM_MODE_VMALLOC
  146. unsigned int dma_buf_size; /* allocated size */
  147. void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
  148. dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
  149. struct tasklet_struct s_tasklet;
  150. #endif
  151. unsigned int sequence; /* Frame sequence number */
  152. unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual bufs */
  153. /* DMA buffers - DMA modes */
  154. struct mcam_vb_buffer *vb_bufs[MAX_DMA_BUFS];
  155. struct vb2_alloc_ctx *vb_alloc_ctx;
  156. struct vb2_alloc_ctx *vb_alloc_ctx_sg;
  157. /* Mode-specific ops, set at open time */
  158. void (*dma_setup)(struct mcam_camera *cam);
  159. void (*frame_complete)(struct mcam_camera *cam, int frame);
  160. /* Current operating parameters */
  161. struct v4l2_pix_format pix_format;
  162. u32 mbus_code;
  163. /* Locks */
  164. struct mutex s_mutex; /* Access to this structure */
  165. };
  166. /*
  167. * Register I/O functions. These are here because the platform code
  168. * may legitimately need to mess with the register space.
  169. */
  170. /*
  171. * Device register I/O
  172. */
  173. static inline void mcam_reg_write(struct mcam_camera *cam, unsigned int reg,
  174. unsigned int val)
  175. {
  176. iowrite32(val, cam->regs + reg);
  177. }
  178. static inline unsigned int mcam_reg_read(struct mcam_camera *cam,
  179. unsigned int reg)
  180. {
  181. return ioread32(cam->regs + reg);
  182. }
  183. static inline void mcam_reg_write_mask(struct mcam_camera *cam, unsigned int reg,
  184. unsigned int val, unsigned int mask)
  185. {
  186. unsigned int v = mcam_reg_read(cam, reg);
  187. v = (v & ~mask) | (val & mask);
  188. mcam_reg_write(cam, reg, v);
  189. }
  190. static inline void mcam_reg_clear_bit(struct mcam_camera *cam,
  191. unsigned int reg, unsigned int val)
  192. {
  193. mcam_reg_write_mask(cam, reg, 0, val);
  194. }
  195. static inline void mcam_reg_set_bit(struct mcam_camera *cam,
  196. unsigned int reg, unsigned int val)
  197. {
  198. mcam_reg_write_mask(cam, reg, val, val);
  199. }
  200. /*
  201. * Functions for use by platform code.
  202. */
  203. int mccic_register(struct mcam_camera *cam);
  204. int mccic_irq(struct mcam_camera *cam, unsigned int irqs);
  205. void mccic_shutdown(struct mcam_camera *cam);
  206. #ifdef CONFIG_PM
  207. void mccic_suspend(struct mcam_camera *cam);
  208. int mccic_resume(struct mcam_camera *cam);
  209. #endif
  210. /*
  211. * Register definitions for the m88alp01 camera interface. Offsets in bytes
  212. * as given in the spec.
  213. */
  214. #define REG_Y0BAR 0x00
  215. #define REG_Y1BAR 0x04
  216. #define REG_Y2BAR 0x08
  217. #define REG_U0BAR 0x0c
  218. #define REG_U1BAR 0x10
  219. #define REG_U2BAR 0x14
  220. #define REG_V0BAR 0x18
  221. #define REG_V1BAR 0x1C
  222. #define REG_V2BAR 0x20
  223. /*
  224. * register definitions for MIPI support
  225. */
  226. #define REG_CSI2_CTRL0 0x100
  227. #define CSI2_C0_MIPI_EN (0x1 << 0)
  228. #define CSI2_C0_ACT_LANE(n) ((n-1) << 1)
  229. #define REG_CSI2_DPHY3 0x12c
  230. #define REG_CSI2_DPHY5 0x134
  231. #define REG_CSI2_DPHY6 0x138
  232. /* ... */
  233. #define REG_IMGPITCH 0x24 /* Image pitch register */
  234. #define IMGP_YP_SHFT 2 /* Y pitch params */
  235. #define IMGP_YP_MASK 0x00003ffc /* Y pitch field */
  236. #define IMGP_UVP_SHFT 18 /* UV pitch (planar) */
  237. #define IMGP_UVP_MASK 0x3ffc0000
  238. #define REG_IRQSTATRAW 0x28 /* RAW IRQ Status */
  239. #define IRQ_EOF0 0x00000001 /* End of frame 0 */
  240. #define IRQ_EOF1 0x00000002 /* End of frame 1 */
  241. #define IRQ_EOF2 0x00000004 /* End of frame 2 */
  242. #define IRQ_SOF0 0x00000008 /* Start of frame 0 */
  243. #define IRQ_SOF1 0x00000010 /* Start of frame 1 */
  244. #define IRQ_SOF2 0x00000020 /* Start of frame 2 */
  245. #define IRQ_OVERFLOW 0x00000040 /* FIFO overflow */
  246. #define IRQ_TWSIW 0x00010000 /* TWSI (smbus) write */
  247. #define IRQ_TWSIR 0x00020000 /* TWSI read */
  248. #define IRQ_TWSIE 0x00040000 /* TWSI error */
  249. #define TWSIIRQS (IRQ_TWSIW|IRQ_TWSIR|IRQ_TWSIE)
  250. #define FRAMEIRQS (IRQ_EOF0|IRQ_EOF1|IRQ_EOF2|IRQ_SOF0|IRQ_SOF1|IRQ_SOF2)
  251. #define ALLIRQS (TWSIIRQS|FRAMEIRQS|IRQ_OVERFLOW)
  252. #define REG_IRQMASK 0x2c /* IRQ mask - same bits as IRQSTAT */
  253. #define REG_IRQSTAT 0x30 /* IRQ status / clear */
  254. #define REG_IMGSIZE 0x34 /* Image size */
  255. #define IMGSZ_V_MASK 0x1fff0000
  256. #define IMGSZ_V_SHIFT 16
  257. #define IMGSZ_H_MASK 0x00003fff
  258. #define REG_IMGOFFSET 0x38 /* IMage offset */
  259. #define REG_CTRL0 0x3c /* Control 0 */
  260. #define C0_ENABLE 0x00000001 /* Makes the whole thing go */
  261. /* Mask for all the format bits */
  262. #define C0_DF_MASK 0x00fffffc /* Bits 2-23 */
  263. /* RGB ordering */
  264. #define C0_RGB4_RGBX 0x00000000
  265. #define C0_RGB4_XRGB 0x00000004
  266. #define C0_RGB4_BGRX 0x00000008
  267. #define C0_RGB4_XBGR 0x0000000c
  268. #define C0_RGB5_RGGB 0x00000000
  269. #define C0_RGB5_GRBG 0x00000004
  270. #define C0_RGB5_GBRG 0x00000008
  271. #define C0_RGB5_BGGR 0x0000000c
  272. /* Spec has two fields for DIN and DOUT, but they must match, so
  273. combine them here. */
  274. #define C0_DF_YUV 0x00000000 /* Data is YUV */
  275. #define C0_DF_RGB 0x000000a0 /* ... RGB */
  276. #define C0_DF_BAYER 0x00000140 /* ... Bayer */
  277. /* 8-8-8 must be missing from the below - ask */
  278. #define C0_RGBF_565 0x00000000
  279. #define C0_RGBF_444 0x00000800
  280. #define C0_RGB_BGR 0x00001000 /* Blue comes first */
  281. #define C0_YUV_PLANAR 0x00000000 /* YUV 422 planar format */
  282. #define C0_YUV_PACKED 0x00008000 /* YUV 422 packed */
  283. #define C0_YUV_420PL 0x0000a000 /* YUV 420 planar */
  284. /* Think that 420 packed must be 111 - ask */
  285. #define C0_YUVE_YUYV 0x00000000 /* Y1CbY0Cr */
  286. #define C0_YUVE_YVYU 0x00010000 /* Y1CrY0Cb */
  287. #define C0_YUVE_VYUY 0x00020000 /* CrY1CbY0 */
  288. #define C0_YUVE_UYVY 0x00030000 /* CbY1CrY0 */
  289. #define C0_YUVE_NOSWAP 0x00000000 /* no bytes swapping */
  290. #define C0_YUVE_SWAP13 0x00010000 /* swap byte 1 and 3 */
  291. #define C0_YUVE_SWAP24 0x00020000 /* swap byte 2 and 4 */
  292. #define C0_YUVE_SWAP1324 0x00030000 /* swap bytes 1&3 and 2&4 */
  293. /* Bayer bits 18,19 if needed */
  294. #define C0_EOF_VSYNC 0x00400000 /* Generate EOF by VSYNC */
  295. #define C0_VEDGE_CTRL 0x00800000 /* Detect falling edge of VSYNC */
  296. #define C0_HPOL_LOW 0x01000000 /* HSYNC polarity active low */
  297. #define C0_VPOL_LOW 0x02000000 /* VSYNC polarity active low */
  298. #define C0_VCLK_LOW 0x04000000 /* VCLK on falling edge */
  299. #define C0_DOWNSCALE 0x08000000 /* Enable downscaler */
  300. /* SIFMODE */
  301. #define C0_SIF_HVSYNC 0x00000000 /* Use H/VSYNC */
  302. #define C0_SOF_NOSYNC 0x40000000 /* Use inband active signaling */
  303. #define C0_SIFM_MASK 0xc0000000 /* SIF mode bits */
  304. /* Bits below C1_444ALPHA are not present in Cafe */
  305. #define REG_CTRL1 0x40 /* Control 1 */
  306. #define C1_CLKGATE 0x00000001 /* Sensor clock gate */
  307. #define C1_DESC_ENA 0x00000100 /* DMA descriptor enable */
  308. #define C1_DESC_3WORD 0x00000200 /* Three-word descriptors used */
  309. #define C1_444ALPHA 0x00f00000 /* Alpha field in RGB444 */
  310. #define C1_ALPHA_SHFT 20
  311. #define C1_DMAB32 0x00000000 /* 32-byte DMA burst */
  312. #define C1_DMAB16 0x02000000 /* 16-byte DMA burst */
  313. #define C1_DMAB64 0x04000000 /* 64-byte DMA burst */
  314. #define C1_DMAB_MASK 0x06000000
  315. #define C1_TWOBUFS 0x08000000 /* Use only two DMA buffers */
  316. #define C1_PWRDWN 0x10000000 /* Power down */
  317. #define REG_CLKCTRL 0x88 /* Clock control */
  318. #define CLK_DIV_MASK 0x0000ffff /* Upper bits RW "reserved" */
  319. /* This appears to be a Cafe-only register */
  320. #define REG_UBAR 0xc4 /* Upper base address register */
  321. /* Armada 610 DMA descriptor registers */
  322. #define REG_DMA_DESC_Y 0x200
  323. #define REG_DMA_DESC_U 0x204
  324. #define REG_DMA_DESC_V 0x208
  325. #define REG_DESC_LEN_Y 0x20c /* Lengths are in bytes */
  326. #define REG_DESC_LEN_U 0x210
  327. #define REG_DESC_LEN_V 0x214
  328. /*
  329. * Useful stuff that probably belongs somewhere global.
  330. */
  331. #define VGA_WIDTH 640
  332. #define VGA_HEIGHT 480
  333. #endif /* _MCAM_CORE_H */