tw5864.h 5.1 KB

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