tw686x.h 4.5 KB

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