solo6x10.h 9.9 KB

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