solo6x10.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
  3. *
  4. * Original author:
  5. * Ben Collins <bcollins@ubuntu.com>
  6. *
  7. * Additional work by:
  8. * John Brooks <john.brooks@bluecherry.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #ifndef __SOLO6X10_H
  21. #define __SOLO6X10_H
  22. #include <linux/pci.h>
  23. #include <linux/i2c.h>
  24. #include <linux/mutex.h>
  25. #include <linux/list.h>
  26. #include <linux/wait.h>
  27. #include <linux/stringify.h>
  28. #include <linux/io.h>
  29. #include <linux/atomic.h>
  30. #include <linux/slab.h>
  31. #include <linux/videodev2.h>
  32. #include <media/v4l2-dev.h>
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-ctrls.h>
  35. #include <media/videobuf2-v4l2.h>
  36. #include "solo6x10-regs.h"
  37. #ifndef PCI_VENDOR_ID_SOFTLOGIC
  38. #define PCI_VENDOR_ID_SOFTLOGIC 0x9413
  39. #define PCI_DEVICE_ID_SOLO6010 0x6010
  40. #define PCI_DEVICE_ID_SOLO6110 0x6110
  41. #endif
  42. #ifndef PCI_VENDOR_ID_BLUECHERRY
  43. #define PCI_VENDOR_ID_BLUECHERRY 0x1BB3
  44. /* Neugent Softlogic 6010 based cards */
  45. #define PCI_DEVICE_ID_NEUSOLO_4 0x4304
  46. #define PCI_DEVICE_ID_NEUSOLO_9 0x4309
  47. #define PCI_DEVICE_ID_NEUSOLO_16 0x4310
  48. /* Bluecherry Softlogic 6010 based cards */
  49. #define PCI_DEVICE_ID_BC_SOLO_4 0x4E04
  50. #define PCI_DEVICE_ID_BC_SOLO_9 0x4E09
  51. #define PCI_DEVICE_ID_BC_SOLO_16 0x4E10
  52. /* Bluecherry Softlogic 6110 based cards */
  53. #define PCI_DEVICE_ID_BC_6110_4 0x5304
  54. #define PCI_DEVICE_ID_BC_6110_8 0x5308
  55. #define PCI_DEVICE_ID_BC_6110_16 0x5310
  56. #endif /* Bluecherry */
  57. /* Used in pci_device_id, and solo_dev->type */
  58. #define SOLO_DEV_6010 0
  59. #define SOLO_DEV_6110 1
  60. #define SOLO6X10_NAME "solo6x10"
  61. #define SOLO_MAX_CHANNELS 16
  62. #define SOLO6X10_VERSION "3.0.0"
  63. /*
  64. * The SOLO6x10 actually has 8 i2c channels, but we only use 2.
  65. * 0 - Techwell chip(s)
  66. * 1 - SAA7128
  67. */
  68. #define SOLO_I2C_ADAPTERS 2
  69. #define SOLO_I2C_TW 0
  70. #define SOLO_I2C_SAA 1
  71. /* DMA Engine setup */
  72. #define SOLO_NR_P2M 4
  73. #define SOLO_NR_P2M_DESC 256
  74. #define SOLO_P2M_DESC_SIZE (SOLO_NR_P2M_DESC * 16)
  75. /* Encoder standard modes */
  76. #define SOLO_ENC_MODE_CIF 2
  77. #define SOLO_ENC_MODE_HD1 1
  78. #define SOLO_ENC_MODE_D1 9
  79. #define SOLO_DEFAULT_QP 3
  80. #define SOLO_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000)
  81. #define V4L2_CID_MOTION_TRACE (SOLO_CID_CUSTOM_BASE+2)
  82. #define V4L2_CID_OSD_TEXT (SOLO_CID_CUSTOM_BASE+3)
  83. /*
  84. * Motion thresholds are in a table of 64x64 samples, with
  85. * each sample representing 16x16 pixels of the source. In
  86. * effect, 44x30 samples are used for NTSC, and 44x36 for PAL.
  87. * The 5th sample on the 10th row is (10*64)+5 = 645.
  88. *
  89. * Internally it is stored as a 45x45 array (45*16 = 720, which is the
  90. * maximum PAL/NTSC width).
  91. */
  92. #define SOLO_MOTION_SZ (45)
  93. enum SOLO_I2C_STATE {
  94. IIC_STATE_IDLE,
  95. IIC_STATE_START,
  96. IIC_STATE_READ,
  97. IIC_STATE_WRITE,
  98. IIC_STATE_STOP
  99. };
  100. /* Defined in Table 4-16, Page 68-69 of the 6010 Datasheet */
  101. struct solo_p2m_desc {
  102. u32 ctrl;
  103. u32 cfg;
  104. u32 dma_addr;
  105. u32 ext_addr;
  106. };
  107. struct solo_p2m_dev {
  108. struct mutex mutex;
  109. struct completion completion;
  110. int desc_count;
  111. int desc_idx;
  112. struct solo_p2m_desc *descs;
  113. int error;
  114. };
  115. #define OSD_TEXT_MAX 44
  116. struct solo_vb2_buf {
  117. struct vb2_v4l2_buffer vb;
  118. struct list_head list;
  119. };
  120. enum solo_enc_types {
  121. SOLO_ENC_TYPE_STD,
  122. SOLO_ENC_TYPE_EXT,
  123. };
  124. struct solo_enc_dev {
  125. struct solo_dev *solo_dev;
  126. /* V4L2 Items */
  127. struct v4l2_ctrl_handler hdl;
  128. struct v4l2_ctrl *md_thresholds;
  129. struct video_device *vfd;
  130. /* General accounting */
  131. struct mutex lock;
  132. spinlock_t motion_lock;
  133. u8 ch;
  134. u8 mode, gop, qp, interlaced, interval;
  135. u8 bw_weight;
  136. u16 motion_thresh;
  137. bool motion_global;
  138. bool motion_enabled;
  139. u16 width;
  140. u16 height;
  141. /* OSD buffers */
  142. char osd_text[OSD_TEXT_MAX + 1];
  143. u8 osd_buf[SOLO_EOSD_EXT_SIZE_MAX]
  144. __aligned(4);
  145. /* VOP stuff */
  146. u8 vop[64];
  147. int vop_len;
  148. u8 jpeg_header[1024];
  149. int jpeg_len;
  150. u32 fmt;
  151. enum solo_enc_types type;
  152. u32 sequence;
  153. struct vb2_queue vidq;
  154. struct list_head vidq_active;
  155. void *alloc_ctx;
  156. int desc_count;
  157. int desc_nelts;
  158. struct solo_p2m_desc *desc_items;
  159. dma_addr_t desc_dma;
  160. spinlock_t av_lock;
  161. };
  162. /* The SOLO6x10 PCI Device */
  163. struct solo_dev {
  164. /* General stuff */
  165. struct pci_dev *pdev;
  166. int type;
  167. unsigned int time_sync;
  168. unsigned int usec_lsb;
  169. unsigned int clock_mhz;
  170. u8 __iomem *reg_base;
  171. int nr_chans;
  172. int nr_ext;
  173. u32 irq_mask;
  174. u32 motion_mask;
  175. struct v4l2_device v4l2_dev;
  176. /* tw28xx accounting */
  177. u8 tw2865, tw2864, tw2815;
  178. u8 tw28_cnt;
  179. /* i2c related items */
  180. struct i2c_adapter i2c_adap[SOLO_I2C_ADAPTERS];
  181. enum SOLO_I2C_STATE i2c_state;
  182. struct mutex i2c_mutex;
  183. int i2c_id;
  184. wait_queue_head_t i2c_wait;
  185. struct i2c_msg *i2c_msg;
  186. unsigned int i2c_msg_num;
  187. unsigned int i2c_msg_ptr;
  188. /* P2M DMA Engine */
  189. struct solo_p2m_dev p2m_dev[SOLO_NR_P2M];
  190. atomic_t p2m_count;
  191. int p2m_jiffies;
  192. unsigned int p2m_timeouts;
  193. /* V4L2 Display items */
  194. struct video_device *vfd;
  195. unsigned int erasing;
  196. unsigned int frame_blank;
  197. u8 cur_disp_ch;
  198. wait_queue_head_t disp_thread_wait;
  199. struct v4l2_ctrl_handler disp_hdl;
  200. /* V4L2 Encoder items */
  201. struct solo_enc_dev *v4l2_enc[SOLO_MAX_CHANNELS];
  202. u16 enc_bw_remain;
  203. /* IDX into hw mp4 encoder */
  204. u8 enc_idx;
  205. /* Current video settings */
  206. u32 video_type;
  207. u16 video_hsize, video_vsize;
  208. u16 vout_hstart, vout_vstart;
  209. u16 vin_hstart, vin_vstart;
  210. u8 fps;
  211. /* JPEG Qp setting */
  212. spinlock_t jpeg_qp_lock;
  213. u32 jpeg_qp[2];
  214. /* Audio components */
  215. struct snd_card *snd_card;
  216. struct snd_pcm *snd_pcm;
  217. atomic_t snd_users;
  218. int g723_hw_idx;
  219. /* sysfs stuffs */
  220. struct device dev;
  221. int sdram_size;
  222. struct bin_attribute sdram_attr;
  223. unsigned int sys_config;
  224. /* Ring thread */
  225. struct task_struct *ring_thread;
  226. wait_queue_head_t ring_thread_wait;
  227. /* VOP_HEADER handling */
  228. void *vh_buf;
  229. dma_addr_t vh_dma;
  230. int vh_size;
  231. /* Buffer handling */
  232. struct vb2_queue vidq;
  233. struct vb2_alloc_ctx *alloc_ctx;
  234. u32 sequence;
  235. struct task_struct *kthread;
  236. struct mutex lock;
  237. spinlock_t slock;
  238. int old_write;
  239. struct list_head vidq_active;
  240. };
  241. static inline u32 solo_reg_read(struct solo_dev *solo_dev, int reg)
  242. {
  243. return readl(solo_dev->reg_base + reg);
  244. }
  245. static inline void solo_reg_write(struct solo_dev *solo_dev, int reg,
  246. u32 data)
  247. {
  248. u16 val;
  249. writel(data, solo_dev->reg_base + reg);
  250. pci_read_config_word(solo_dev->pdev, PCI_STATUS, &val);
  251. }
  252. static inline void solo_irq_on(struct solo_dev *dev, u32 mask)
  253. {
  254. dev->irq_mask |= mask;
  255. solo_reg_write(dev, SOLO_IRQ_MASK, dev->irq_mask);
  256. }
  257. static inline void solo_irq_off(struct solo_dev *dev, u32 mask)
  258. {
  259. dev->irq_mask &= ~mask;
  260. solo_reg_write(dev, SOLO_IRQ_MASK, dev->irq_mask);
  261. }
  262. /* Init/exit routines for subsystems */
  263. int solo_disp_init(struct solo_dev *solo_dev);
  264. void solo_disp_exit(struct solo_dev *solo_dev);
  265. int solo_gpio_init(struct solo_dev *solo_dev);
  266. void solo_gpio_exit(struct solo_dev *solo_dev);
  267. int solo_i2c_init(struct solo_dev *solo_dev);
  268. void solo_i2c_exit(struct solo_dev *solo_dev);
  269. int solo_p2m_init(struct solo_dev *solo_dev);
  270. void solo_p2m_exit(struct solo_dev *solo_dev);
  271. int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr);
  272. void solo_v4l2_exit(struct solo_dev *solo_dev);
  273. int solo_enc_init(struct solo_dev *solo_dev);
  274. void solo_enc_exit(struct solo_dev *solo_dev);
  275. int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr);
  276. void solo_enc_v4l2_exit(struct solo_dev *solo_dev);
  277. int solo_g723_init(struct solo_dev *solo_dev);
  278. void solo_g723_exit(struct solo_dev *solo_dev);
  279. /* ISR's */
  280. int solo_i2c_isr(struct solo_dev *solo_dev);
  281. void solo_p2m_isr(struct solo_dev *solo_dev, int id);
  282. void solo_p2m_error_isr(struct solo_dev *solo_dev);
  283. void solo_enc_v4l2_isr(struct solo_dev *solo_dev);
  284. void solo_g723_isr(struct solo_dev *solo_dev);
  285. void solo_motion_isr(struct solo_dev *solo_dev);
  286. void solo_video_in_isr(struct solo_dev *solo_dev);
  287. /* i2c read/write */
  288. u8 solo_i2c_readbyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off);
  289. void solo_i2c_writebyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off,
  290. u8 data);
  291. /* P2M DMA */
  292. int solo_p2m_dma_t(struct solo_dev *solo_dev, int wr,
  293. dma_addr_t dma_addr, u32 ext_addr, u32 size,
  294. int repeat, u32 ext_size);
  295. int solo_p2m_dma(struct solo_dev *solo_dev, int wr,
  296. void *sys_addr, u32 ext_addr, u32 size,
  297. int repeat, u32 ext_size);
  298. void solo_p2m_fill_desc(struct solo_p2m_desc *desc, int wr,
  299. dma_addr_t dma_addr, u32 ext_addr, u32 size,
  300. int repeat, u32 ext_size);
  301. int solo_p2m_dma_desc(struct solo_dev *solo_dev,
  302. struct solo_p2m_desc *desc, dma_addr_t desc_dma,
  303. int desc_cnt);
  304. /* Global s_std ioctl */
  305. int solo_set_video_type(struct solo_dev *solo_dev, bool is_50hz);
  306. void solo_update_mode(struct solo_enc_dev *solo_enc);
  307. /* Set the threshold for motion detection */
  308. int solo_set_motion_threshold(struct solo_dev *solo_dev, u8 ch, u16 val);
  309. int solo_set_motion_block(struct solo_dev *solo_dev, u8 ch,
  310. const u16 *thresholds);
  311. #define SOLO_DEF_MOT_THRESH 0x0300
  312. /* Write text on OSD */
  313. int solo_osd_print(struct solo_enc_dev *solo_enc);
  314. /* EEPROM commands */
  315. unsigned int solo_eeprom_ewen(struct solo_dev *solo_dev, int w_en);
  316. __be16 solo_eeprom_read(struct solo_dev *solo_dev, int loc);
  317. int solo_eeprom_write(struct solo_dev *solo_dev, int loc,
  318. __be16 data);
  319. /* JPEG Qp functions */
  320. void solo_s_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch,
  321. unsigned int qp);
  322. int solo_g_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch);
  323. #define CHK_FLAGS(v, flags) (((v) & (flags)) == (flags))
  324. #endif /* __SOLO6X10_H */