mcam-core.h 9.1 KB

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