mcam-core.h 9.9 KB

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