tw686x.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
  4. *
  5. * Copyright (C) 2015 Industrial Research Institute for Automation
  6. * and Measurements PIAP
  7. * Written by Krzysztof Ha?asa
  8. */
  9. #include <linux/mutex.h>
  10. #include <linux/pci.h>
  11. #include <linux/timer.h>
  12. #include <linux/videodev2.h>
  13. #include <media/v4l2-common.h>
  14. #include <media/v4l2-ctrls.h>
  15. #include <media/v4l2-device.h>
  16. #include <media/v4l2-ioctl.h>
  17. #include <media/videobuf2-v4l2.h>
  18. #include <sound/pcm.h>
  19. #include "tw686x-regs.h"
  20. #define TYPE_MAX_CHANNELS 0x0f
  21. #define TYPE_SECOND_GEN 0x10
  22. #define TW686X_DEF_PHASE_REF 0x1518
  23. #define TW686X_AUDIO_PAGE_MAX 16
  24. #define TW686X_AUDIO_PERIODS_MIN 2
  25. #define TW686X_AUDIO_PERIODS_MAX TW686X_AUDIO_PAGE_MAX
  26. #define TW686X_DMA_MODE_MEMCPY 0
  27. #define TW686X_DMA_MODE_CONTIG 1
  28. #define TW686X_DMA_MODE_SG 2
  29. struct tw686x_format {
  30. char *name;
  31. unsigned int fourcc;
  32. unsigned int depth;
  33. unsigned int mode;
  34. };
  35. struct tw686x_dma_desc {
  36. dma_addr_t phys;
  37. void *virt;
  38. unsigned int size;
  39. };
  40. struct tw686x_sg_desc {
  41. /* 3 MSBits for flags, 13 LSBits for length */
  42. __le32 flags_length;
  43. __le32 phys;
  44. };
  45. struct tw686x_audio_buf {
  46. dma_addr_t dma;
  47. void *virt;
  48. struct list_head list;
  49. };
  50. struct tw686x_v4l2_buf {
  51. struct vb2_v4l2_buffer vb;
  52. struct list_head list;
  53. };
  54. struct tw686x_audio_channel {
  55. struct tw686x_dev *dev;
  56. struct snd_pcm_substream *ss;
  57. unsigned int ch;
  58. struct tw686x_audio_buf *curr_bufs[2];
  59. struct tw686x_dma_desc dma_descs[2];
  60. dma_addr_t ptr;
  61. struct tw686x_audio_buf buf[TW686X_AUDIO_PAGE_MAX];
  62. struct list_head buf_list;
  63. spinlock_t lock;
  64. };
  65. struct tw686x_video_channel {
  66. struct tw686x_dev *dev;
  67. struct vb2_queue vidq;
  68. struct list_head vidq_queued;
  69. struct video_device *device;
  70. struct tw686x_v4l2_buf *curr_bufs[2];
  71. struct tw686x_dma_desc dma_descs[2];
  72. struct tw686x_sg_desc *sg_descs[2];
  73. struct v4l2_ctrl_handler ctrl_handler;
  74. const struct tw686x_format *format;
  75. struct mutex vb_mutex;
  76. spinlock_t qlock;
  77. v4l2_std_id video_standard;
  78. unsigned int width, height;
  79. unsigned int h_halve, v_halve;
  80. unsigned int ch;
  81. unsigned int num;
  82. unsigned int fps;
  83. unsigned int input;
  84. unsigned int sequence;
  85. unsigned int pb;
  86. bool no_signal;
  87. };
  88. struct tw686x_dma_ops {
  89. int (*setup)(struct tw686x_dev *dev);
  90. int (*alloc)(struct tw686x_video_channel *vc, unsigned int pb);
  91. void (*free)(struct tw686x_video_channel *vc, unsigned int pb);
  92. void (*buf_refill)(struct tw686x_video_channel *vc, unsigned int pb);
  93. const struct vb2_mem_ops *mem_ops;
  94. enum v4l2_field field;
  95. u32 hw_dma_mode;
  96. };
  97. /* struct tw686x_dev - global device status */
  98. struct tw686x_dev {
  99. /*
  100. * spinlock controlling access to the shared device registers
  101. * (DMA enable/disable)
  102. */
  103. spinlock_t lock;
  104. struct v4l2_device v4l2_dev;
  105. struct snd_card *snd_card;
  106. char name[32];
  107. unsigned int type;
  108. unsigned int dma_mode;
  109. struct pci_dev *pci_dev;
  110. __u32 __iomem *mmio;
  111. const struct tw686x_dma_ops *dma_ops;
  112. struct tw686x_video_channel *video_channels;
  113. struct tw686x_audio_channel *audio_channels;
  114. /* Per-device audio parameters */
  115. int audio_rate;
  116. int period_size;
  117. int audio_enabled;
  118. struct timer_list dma_delay_timer;
  119. u32 pending_dma_en; /* must be protected by lock */
  120. u32 pending_dma_cmd; /* must be protected by lock */
  121. };
  122. static inline uint32_t reg_read(struct tw686x_dev *dev, unsigned int reg)
  123. {
  124. return readl(dev->mmio + reg);
  125. }
  126. static inline void reg_write(struct tw686x_dev *dev, unsigned int reg,
  127. uint32_t value)
  128. {
  129. writel(value, dev->mmio + reg);
  130. }
  131. static inline unsigned int max_channels(struct tw686x_dev *dev)
  132. {
  133. return dev->type & TYPE_MAX_CHANNELS; /* 4 or 8 channels */
  134. }
  135. static inline unsigned is_second_gen(struct tw686x_dev *dev)
  136. {
  137. /* each channel has its own DMA SG table */
  138. return dev->type & TYPE_SECOND_GEN;
  139. }
  140. void tw686x_enable_channel(struct tw686x_dev *dev, unsigned int channel);
  141. void tw686x_disable_channel(struct tw686x_dev *dev, unsigned int channel);
  142. int tw686x_video_init(struct tw686x_dev *dev);
  143. void tw686x_video_free(struct tw686x_dev *dev);
  144. void tw686x_video_irq(struct tw686x_dev *dev, unsigned long requests,
  145. unsigned int pb_status, unsigned int fifo_status,
  146. unsigned int *reset_ch);
  147. int tw686x_audio_init(struct tw686x_dev *dev);
  148. void tw686x_audio_free(struct tw686x_dev *dev);
  149. void tw686x_audio_irq(struct tw686x_dev *dev, unsigned long requests,
  150. unsigned int pb_status);