tw5864.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * TW5864 driver - common header file
  3. *
  4. * Copyright (C) 2016 Bluecherry, LLC <maintainers@bluecherrydvr.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/pci.h>
  17. #include <linux/videodev2.h>
  18. #include <linux/notifier.h>
  19. #include <linux/delay.h>
  20. #include <linux/mutex.h>
  21. #include <linux/io.h>
  22. #include <linux/interrupt.h>
  23. #include <media/v4l2-common.h>
  24. #include <media/v4l2-ioctl.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/videobuf2-dma-sg.h>
  28. #include "tw5864-reg.h"
  29. #define PCI_DEVICE_ID_TECHWELL_5864 0x5864
  30. #define TW5864_NORMS V4L2_STD_ALL
  31. /* ----------------------------------------------------------- */
  32. /* card configuration */
  33. #define TW5864_INPUTS 4
  34. /* The TW5864 uses 192 (16x12) detection cells in full screen for motion
  35. * detection. Each detection cell is composed of 44 pixels and 20 lines for
  36. * NTSC and 24 lines for PAL.
  37. */
  38. #define MD_CELLS_HOR 16
  39. #define MD_CELLS_VERT 12
  40. #define MD_CELLS (MD_CELLS_HOR * MD_CELLS_VERT)
  41. #define H264_VLC_BUF_SIZE 0x80000
  42. #define H264_MV_BUF_SIZE 0x2000 /* device writes 5396 bytes */
  43. #define QP_VALUE 28
  44. #define MAX_GOP_SIZE 255
  45. #define GOP_SIZE MAX_GOP_SIZE
  46. enum resolution {
  47. D1 = 1,
  48. HD1 = 2, /* half d1 - 360x(240|288) */
  49. CIF = 3,
  50. QCIF = 4,
  51. };
  52. /* ----------------------------------------------------------- */
  53. /* device / file handle status */
  54. struct tw5864_dev; /* forward delclaration */
  55. /* buffer for one video/vbi/ts frame */
  56. struct tw5864_buf {
  57. struct vb2_v4l2_buffer vb;
  58. struct list_head list;
  59. unsigned int size;
  60. };
  61. struct tw5864_dma_buf {
  62. void *addr;
  63. dma_addr_t dma_addr;
  64. };
  65. enum tw5864_vid_std {
  66. STD_NTSC = 0, /* NTSC (M) */
  67. STD_PAL = 1, /* PAL (B, D, G, H, I) */
  68. STD_SECAM = 2, /* SECAM */
  69. STD_NTSC443 = 3, /* NTSC4.43 */
  70. STD_PAL_M = 4, /* PAL (M) */
  71. STD_PAL_CN = 5, /* PAL (CN) */
  72. STD_PAL_60 = 6, /* PAL 60 */
  73. STD_INVALID = 7,
  74. STD_AUTO = 7,
  75. };
  76. struct tw5864_input {
  77. int nr; /* input number */
  78. struct tw5864_dev *root;
  79. struct mutex lock; /* used for vidq and vdev */
  80. spinlock_t slock; /* used for sync between ISR, tasklet & V4L2 API */
  81. struct video_device vdev;
  82. struct v4l2_ctrl_handler hdl;
  83. struct vb2_queue vidq;
  84. struct list_head active;
  85. enum resolution resolution;
  86. unsigned int width, height;
  87. unsigned int frame_seqno;
  88. unsigned int frame_gop_seqno;
  89. unsigned int h264_idr_pic_id;
  90. int enabled;
  91. enum tw5864_vid_std std;
  92. v4l2_std_id v4l2_std;
  93. int tail_nb_bits;
  94. u8 tail;
  95. u8 *buf_cur_ptr;
  96. int buf_cur_space_left;
  97. u32 reg_interlacing;
  98. u32 reg_vlc;
  99. u32 reg_dsp_codec;
  100. u32 reg_dsp;
  101. u32 reg_emu;
  102. u32 reg_dsp_qp;
  103. u32 reg_dsp_ref_mvp_lambda;
  104. u32 reg_dsp_i4x4_weight;
  105. u32 buf_id;
  106. struct tw5864_buf *vb;
  107. struct v4l2_ctrl *md_threshold_grid_ctrl;
  108. u16 md_threshold_grid_values[12 * 16];
  109. int qp;
  110. int gop;
  111. /*
  112. * In (1/MAX_FPS) units.
  113. * For max FPS (default), set to 1.
  114. * For 1 FPS, set to e.g. 32.
  115. */
  116. int frame_interval;
  117. unsigned long new_frame_deadline;
  118. };
  119. struct tw5864_h264_frame {
  120. struct tw5864_dma_buf vlc;
  121. struct tw5864_dma_buf mv;
  122. int vlc_len;
  123. u32 checksum;
  124. struct tw5864_input *input;
  125. u64 timestamp;
  126. unsigned int seqno;
  127. unsigned int gop_seqno;
  128. };
  129. /* global device status */
  130. struct tw5864_dev {
  131. spinlock_t slock; /* used for sync between ISR, tasklet & V4L2 API */
  132. struct v4l2_device v4l2_dev;
  133. struct tw5864_input inputs[TW5864_INPUTS];
  134. #define H264_BUF_CNT 4
  135. struct tw5864_h264_frame h264_buf[H264_BUF_CNT];
  136. int h264_buf_r_index;
  137. int h264_buf_w_index;
  138. struct tasklet_struct tasklet;
  139. int encoder_busy;
  140. /* Input number to check next for ready raw picture (in RR fashion) */
  141. int next_input;
  142. /* pci i/o */
  143. char name[64];
  144. struct pci_dev *pci;
  145. void __iomem *mmio;
  146. u32 irqmask;
  147. };
  148. #define tw_readl(reg) readl(dev->mmio + reg)
  149. #define tw_mask_readl(reg, mask) \
  150. (tw_readl(reg) & (mask))
  151. #define tw_mask_shift_readl(reg, mask, shift) \
  152. (tw_mask_readl((reg), ((mask) << (shift))) >> (shift))
  153. #define tw_writel(reg, value) writel((value), dev->mmio + reg)
  154. #define tw_mask_writel(reg, mask, value) \
  155. tw_writel(reg, (tw_readl(reg) & ~(mask)) | ((value) & (mask)))
  156. #define tw_mask_shift_writel(reg, mask, shift, value) \
  157. tw_mask_writel((reg), ((mask) << (shift)), ((value) << (shift)))
  158. #define tw_setl(reg, bit) tw_writel((reg), tw_readl(reg) | (bit))
  159. #define tw_clearl(reg, bit) tw_writel((reg), tw_readl(reg) & ~(bit))
  160. u8 tw5864_indir_readb(struct tw5864_dev *dev, u16 addr);
  161. #define tw_indir_readb(addr) tw5864_indir_readb(dev, addr)
  162. void tw5864_indir_writeb(struct tw5864_dev *dev, u16 addr, u8 data);
  163. #define tw_indir_writeb(addr, data) tw5864_indir_writeb(dev, addr, data)
  164. void tw5864_irqmask_apply(struct tw5864_dev *dev);
  165. int tw5864_video_init(struct tw5864_dev *dev, int *video_nr);
  166. void tw5864_video_fini(struct tw5864_dev *dev);
  167. void tw5864_prepare_frame_headers(struct tw5864_input *input);
  168. void tw5864_h264_put_stream_header(u8 **buf, size_t *space_left, int qp,
  169. int width, int height);
  170. void tw5864_h264_put_slice_header(u8 **buf, size_t *space_left,
  171. unsigned int idr_pic_id,
  172. unsigned int frame_gop_seqno,
  173. int *tail_nb_bits, u8 *tail);
  174. void tw5864_request_encoded_frame(struct tw5864_input *input);