solo6x10.h 9.4 KB

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