mcam-core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Marvell camera core structures.
  3. *
  4. * Copyright 2011 Jonathan Corbet corbet@lwn.net
  5. */
  6. /*
  7. * Tracking of streaming I/O buffers.
  8. * FIXME doesn't belong in this file
  9. */
  10. struct mcam_sio_buffer {
  11. struct list_head list;
  12. struct v4l2_buffer v4lbuf;
  13. char *buffer; /* Where it lives in kernel space */
  14. int mapcount;
  15. struct mcam_camera *cam;
  16. };
  17. enum mcam_state {
  18. S_NOTREADY, /* Not yet initialized */
  19. S_IDLE, /* Just hanging around */
  20. S_FLAKED, /* Some sort of problem */
  21. S_SINGLEREAD, /* In read() */
  22. S_SPECREAD, /* Speculative read (for future read()) */
  23. S_STREAMING /* Streaming data */
  24. };
  25. #define MAX_DMA_BUFS 3
  26. /*
  27. * A description of one of our devices.
  28. * Locking: controlled by s_mutex. Certain fields, however, require
  29. * the dev_lock spinlock; they are marked as such by comments.
  30. * dev_lock is also required for access to device registers.
  31. */
  32. struct mcam_camera {
  33. /*
  34. * These fields should be set by the platform code prior to
  35. * calling mcam_register().
  36. */
  37. struct i2c_adapter i2c_adapter;
  38. unsigned char __iomem *regs;
  39. spinlock_t dev_lock;
  40. struct device *dev; /* For messages, dma alloc */
  41. unsigned int chip_id;
  42. /*
  43. * Callbacks from the core to the platform code.
  44. */
  45. void (*plat_power_up) (struct mcam_camera *cam);
  46. void (*plat_power_down) (struct mcam_camera *cam);
  47. /*
  48. * Everything below here is private to the mcam core and
  49. * should not be touched by the platform code.
  50. */
  51. struct v4l2_device v4l2_dev;
  52. enum mcam_state state;
  53. unsigned long flags; /* Buffer status, mainly (dev_lock) */
  54. int users; /* How many open FDs */
  55. struct file *owner; /* Who has data access (v4l2) */
  56. /*
  57. * Subsystem structures.
  58. */
  59. struct video_device vdev;
  60. struct v4l2_subdev *sensor;
  61. unsigned short sensor_addr;
  62. struct list_head dev_list; /* link to other devices */
  63. /* DMA buffers */
  64. unsigned int nbufs; /* How many are alloc'd */
  65. int next_buf; /* Next to consume (dev_lock) */
  66. unsigned int dma_buf_size; /* allocated size */
  67. void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
  68. dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
  69. unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
  70. unsigned int sequence; /* Frame sequence number */
  71. unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
  72. /* Streaming buffers */
  73. unsigned int n_sbufs; /* How many we have */
  74. struct mcam_sio_buffer *sb_bufs; /* The array of housekeeping structs */
  75. struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
  76. struct list_head sb_full; /* With data (user space owns) (dev_lock) */
  77. struct tasklet_struct s_tasklet;
  78. /* Current operating parameters */
  79. u32 sensor_type; /* Currently ov7670 only */
  80. struct v4l2_pix_format pix_format;
  81. enum v4l2_mbus_pixelcode mbus_code;
  82. /* Locks */
  83. struct mutex s_mutex; /* Access to this structure */
  84. /* Misc */
  85. wait_queue_head_t iowait; /* Waiting on frame data */
  86. };
  87. /*
  88. * Register I/O functions. These are here because the platform code
  89. * may legitimately need to mess with the register space.
  90. */
  91. /*
  92. * Device register I/O
  93. */
  94. static inline void mcam_reg_write(struct mcam_camera *cam, unsigned int reg,
  95. unsigned int val)
  96. {
  97. iowrite32(val, cam->regs + reg);
  98. }
  99. static inline unsigned int mcam_reg_read(struct mcam_camera *cam,
  100. unsigned int reg)
  101. {
  102. return ioread32(cam->regs + reg);
  103. }
  104. static inline void mcam_reg_write_mask(struct mcam_camera *cam, unsigned int reg,
  105. unsigned int val, unsigned int mask)
  106. {
  107. unsigned int v = mcam_reg_read(cam, reg);
  108. v = (v & ~mask) | (val & mask);
  109. mcam_reg_write(cam, reg, v);
  110. }
  111. static inline void mcam_reg_clear_bit(struct mcam_camera *cam,
  112. unsigned int reg, unsigned int val)
  113. {
  114. mcam_reg_write_mask(cam, reg, 0, val);
  115. }
  116. static inline void mcam_reg_set_bit(struct mcam_camera *cam,
  117. unsigned int reg, unsigned int val)
  118. {
  119. mcam_reg_write_mask(cam, reg, val, val);
  120. }
  121. /*
  122. * Functions for use by platform code.
  123. */
  124. int mccic_register(struct mcam_camera *cam);
  125. int mccic_irq(struct mcam_camera *cam, unsigned int irqs);
  126. void mccic_shutdown(struct mcam_camera *cam);
  127. #ifdef CONFIG_PM
  128. void mccic_suspend(struct mcam_camera *cam);
  129. int mccic_resume(struct mcam_camera *cam);
  130. #endif
  131. /*
  132. * Register definitions for the m88alp01 camera interface. Offsets in bytes
  133. * as given in the spec.
  134. */
  135. #define REG_Y0BAR 0x00
  136. #define REG_Y1BAR 0x04
  137. #define REG_Y2BAR 0x08
  138. /* ... */
  139. #define REG_IMGPITCH 0x24 /* Image pitch register */
  140. #define IMGP_YP_SHFT 2 /* Y pitch params */
  141. #define IMGP_YP_MASK 0x00003ffc /* Y pitch field */
  142. #define IMGP_UVP_SHFT 18 /* UV pitch (planar) */
  143. #define IMGP_UVP_MASK 0x3ffc0000
  144. #define REG_IRQSTATRAW 0x28 /* RAW IRQ Status */
  145. #define IRQ_EOF0 0x00000001 /* End of frame 0 */
  146. #define IRQ_EOF1 0x00000002 /* End of frame 1 */
  147. #define IRQ_EOF2 0x00000004 /* End of frame 2 */
  148. #define IRQ_SOF0 0x00000008 /* Start of frame 0 */
  149. #define IRQ_SOF1 0x00000010 /* Start of frame 1 */
  150. #define IRQ_SOF2 0x00000020 /* Start of frame 2 */
  151. #define IRQ_OVERFLOW 0x00000040 /* FIFO overflow */
  152. #define IRQ_TWSIW 0x00010000 /* TWSI (smbus) write */
  153. #define IRQ_TWSIR 0x00020000 /* TWSI read */
  154. #define IRQ_TWSIE 0x00040000 /* TWSI error */
  155. #define TWSIIRQS (IRQ_TWSIW|IRQ_TWSIR|IRQ_TWSIE)
  156. #define FRAMEIRQS (IRQ_EOF0|IRQ_EOF1|IRQ_EOF2|IRQ_SOF0|IRQ_SOF1|IRQ_SOF2)
  157. #define ALLIRQS (TWSIIRQS|FRAMEIRQS|IRQ_OVERFLOW)
  158. #define REG_IRQMASK 0x2c /* IRQ mask - same bits as IRQSTAT */
  159. #define REG_IRQSTAT 0x30 /* IRQ status / clear */
  160. #define REG_IMGSIZE 0x34 /* Image size */
  161. #define IMGSZ_V_MASK 0x1fff0000
  162. #define IMGSZ_V_SHIFT 16
  163. #define IMGSZ_H_MASK 0x00003fff
  164. #define REG_IMGOFFSET 0x38 /* IMage offset */
  165. #define REG_CTRL0 0x3c /* Control 0 */
  166. #define C0_ENABLE 0x00000001 /* Makes the whole thing go */
  167. /* Mask for all the format bits */
  168. #define C0_DF_MASK 0x00fffffc /* Bits 2-23 */
  169. /* RGB ordering */
  170. #define C0_RGB4_RGBX 0x00000000
  171. #define C0_RGB4_XRGB 0x00000004
  172. #define C0_RGB4_BGRX 0x00000008
  173. #define C0_RGB4_XBGR 0x0000000c
  174. #define C0_RGB5_RGGB 0x00000000
  175. #define C0_RGB5_GRBG 0x00000004
  176. #define C0_RGB5_GBRG 0x00000008
  177. #define C0_RGB5_BGGR 0x0000000c
  178. /* Spec has two fields for DIN and DOUT, but they must match, so
  179. combine them here. */
  180. #define C0_DF_YUV 0x00000000 /* Data is YUV */
  181. #define C0_DF_RGB 0x000000a0 /* ... RGB */
  182. #define C0_DF_BAYER 0x00000140 /* ... Bayer */
  183. /* 8-8-8 must be missing from the below - ask */
  184. #define C0_RGBF_565 0x00000000
  185. #define C0_RGBF_444 0x00000800
  186. #define C0_RGB_BGR 0x00001000 /* Blue comes first */
  187. #define C0_YUV_PLANAR 0x00000000 /* YUV 422 planar format */
  188. #define C0_YUV_PACKED 0x00008000 /* YUV 422 packed */
  189. #define C0_YUV_420PL 0x0000a000 /* YUV 420 planar */
  190. /* Think that 420 packed must be 111 - ask */
  191. #define C0_YUVE_YUYV 0x00000000 /* Y1CbY0Cr */
  192. #define C0_YUVE_YVYU 0x00010000 /* Y1CrY0Cb */
  193. #define C0_YUVE_VYUY 0x00020000 /* CrY1CbY0 */
  194. #define C0_YUVE_UYVY 0x00030000 /* CbY1CrY0 */
  195. #define C0_YUVE_XYUV 0x00000000 /* 420: .YUV */
  196. #define C0_YUVE_XYVU 0x00010000 /* 420: .YVU */
  197. #define C0_YUVE_XUVY 0x00020000 /* 420: .UVY */
  198. #define C0_YUVE_XVUY 0x00030000 /* 420: .VUY */
  199. /* Bayer bits 18,19 if needed */
  200. #define C0_HPOL_LOW 0x01000000 /* HSYNC polarity active low */
  201. #define C0_VPOL_LOW 0x02000000 /* VSYNC polarity active low */
  202. #define C0_VCLK_LOW 0x04000000 /* VCLK on falling edge */
  203. #define C0_DOWNSCALE 0x08000000 /* Enable downscaler */
  204. #define C0_SIFM_MASK 0xc0000000 /* SIF mode bits */
  205. #define C0_SIF_HVSYNC 0x00000000 /* Use H/VSYNC */
  206. #define CO_SOF_NOSYNC 0x40000000 /* Use inband active signaling */
  207. #define REG_CTRL1 0x40 /* Control 1 */
  208. #define C1_444ALPHA 0x00f00000 /* Alpha field in RGB444 */
  209. #define C1_ALPHA_SHFT 20
  210. #define C1_DMAB32 0x00000000 /* 32-byte DMA burst */
  211. #define C1_DMAB16 0x02000000 /* 16-byte DMA burst */
  212. #define C1_DMAB64 0x04000000 /* 64-byte DMA burst */
  213. #define C1_DMAB_MASK 0x06000000
  214. #define C1_TWOBUFS 0x08000000 /* Use only two DMA buffers */
  215. #define C1_PWRDWN 0x10000000 /* Power down */
  216. #define REG_CLKCTRL 0x88 /* Clock control */
  217. #define CLK_DIV_MASK 0x0000ffff /* Upper bits RW "reserved" */
  218. #define REG_GPR 0xb4 /* General purpose register. This
  219. controls inputs to the power and reset
  220. pins on the OV7670 used with OLPC;
  221. other deployments could differ. */
  222. #define GPR_C1EN 0x00000020 /* Pad 1 (power down) enable */
  223. #define GPR_C0EN 0x00000010 /* Pad 0 (reset) enable */
  224. #define GPR_C1 0x00000002 /* Control 1 value */
  225. /*
  226. * Control 0 is wired to reset on OLPC machines. For ov7x sensors,
  227. * it is active low, for 0v6x, instead, it's active high. What
  228. * fun.
  229. */
  230. #define GPR_C0 0x00000001 /* Control 0 value */
  231. #define REG_TWSIC0 0xb8 /* TWSI (smbus) control 0 */
  232. #define TWSIC0_EN 0x00000001 /* TWSI enable */
  233. #define TWSIC0_MODE 0x00000002 /* 1 = 16-bit, 0 = 8-bit */
  234. #define TWSIC0_SID 0x000003fc /* Slave ID */
  235. #define TWSIC0_SID_SHIFT 2
  236. #define TWSIC0_CLKDIV 0x0007fc00 /* Clock divider */
  237. #define TWSIC0_MASKACK 0x00400000 /* Mask ack from sensor */
  238. #define TWSIC0_OVMAGIC 0x00800000 /* Make it work on OV sensors */
  239. #define REG_TWSIC1 0xbc /* TWSI control 1 */
  240. #define TWSIC1_DATA 0x0000ffff /* Data to/from camchip */
  241. #define TWSIC1_ADDR 0x00ff0000 /* Address (register) */
  242. #define TWSIC1_ADDR_SHIFT 16
  243. #define TWSIC1_READ 0x01000000 /* Set for read op */
  244. #define TWSIC1_WSTAT 0x02000000 /* Write status */
  245. #define TWSIC1_RVALID 0x04000000 /* Read data valid */
  246. #define TWSIC1_ERROR 0x08000000 /* Something screwed up */
  247. #define REG_UBAR 0xc4 /* Upper base address register */
  248. /*
  249. * Here's the weird global control registers which are said to live
  250. * way up here.
  251. */
  252. #define REG_GL_CSR 0x3004 /* Control/status register */
  253. #define GCSR_SRS 0x00000001 /* SW Reset set */
  254. #define GCSR_SRC 0x00000002 /* SW Reset clear */
  255. #define GCSR_MRS 0x00000004 /* Master reset set */
  256. #define GCSR_MRC 0x00000008 /* HW Reset clear */
  257. #define GCSR_CCIC_EN 0x00004000 /* CCIC Clock enable */
  258. #define REG_GL_IMASK 0x300c /* Interrupt mask register */
  259. #define GIMSK_CCIC_EN 0x00000004 /* CCIC Interrupt enable */
  260. #define REG_GL_FCR 0x3038 /* GPIO functional control register */
  261. #define GFCR_GPIO_ON 0x08 /* Camera GPIO enabled */
  262. #define REG_GL_GPIOR 0x315c /* GPIO register */
  263. #define GGPIO_OUT 0x80000 /* GPIO output */
  264. #define GGPIO_VAL 0x00008 /* Output pin value */
  265. #define REG_LEN (REG_GL_IMASK + 4)
  266. /*
  267. * Useful stuff that probably belongs somewhere global.
  268. */
  269. #define VGA_WIDTH 640
  270. #define VGA_HEIGHT 480