chv3-i2s.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/of.h>
  4. #include <linux/platform_device.h>
  5. #include <sound/soc.h>
  6. /*
  7. * The I2S interface consists of two ring buffers - one for RX and one for
  8. * TX. A ring buffer has a producer index and a consumer index. Depending
  9. * on which way the data is flowing, either the software or the hardware
  10. * writes data and updates the producer index, and the other end reads data
  11. * and updates the consumer index.
  12. *
  13. * The pointer managed by software is updated using the .ack callback
  14. * (see chv3_dma_ack). This seems to be the only way to reliably obtain
  15. * the appl_ptr from within the driver and pass it to hardware.
  16. *
  17. * Because of the two pointer design, the ring buffer can never be full. With
  18. * capture this isn't a problem, because the hardware being the producer
  19. * will wait for the consumer index to move out of the way. With playback,
  20. * however, this is problematic, because ALSA wants to fill up the buffer
  21. * completely when waiting for hardware. In the .ack callback, the driver
  22. * would have to wait for the consumer index to move out of the way by
  23. * busy-waiting, which would keep stalling the kernel for quite a long time.
  24. *
  25. * The workaround to this problem is to "lie" to ALSA that the hw_pointer
  26. * is one frame behind what it actually is (see chv3_dma_pointer). This
  27. * way, ALSA will not try to fill up the entire buffer, and all callbacks
  28. * are wait-free.
  29. */
  30. #define I2S_TX_ENABLE 0x00
  31. #define I2S_TX_BASE_ADDR 0x04
  32. #define I2S_TX_BUFFER_SIZE 0x08
  33. #define I2S_TX_PRODUCER_IDX 0x0c
  34. #define I2S_TX_CONSUMER_IDX 0x10
  35. #define I2S_RX_ENABLE 0x14
  36. #define I2S_RX_BASE_ADDR 0x18
  37. #define I2S_RX_BUFFER_SIZE 0x1c
  38. #define I2S_RX_PRODUCER_IDX 0x20
  39. #define I2S_RX_CONSUMER_IDX 0x24
  40. #define I2S_SOFT_RESET 0x2c
  41. #define I2S_SOFT_RESET_RX_BIT 0x1
  42. #define I2S_SOFT_RESET_TX_BIT 0x2
  43. #define I2S_RX_IRQ 0x4c
  44. #define I2S_RX_IRQ_CONST 0x50
  45. #define I2S_TX_IRQ 0x54
  46. #define I2S_TX_IRQ_CONST 0x58
  47. #define I2S_IRQ_MASK 0x8
  48. #define I2S_IRQ_CLR 0xc
  49. #define I2S_IRQ_RX_BIT 0x1
  50. #define I2S_IRQ_TX_BIT 0x2
  51. #define I2S_MAX_BUFFER_SIZE 0x200000
  52. struct chv3_i2s_dev {
  53. struct device *dev;
  54. void __iomem *iobase;
  55. void __iomem *iobase_irq;
  56. struct snd_pcm_substream *rx_substream;
  57. struct snd_pcm_substream *tx_substream;
  58. int tx_bytes_to_fetch;
  59. };
  60. static struct snd_soc_dai_driver chv3_i2s_dai = {
  61. .name = "chv3-i2s",
  62. .capture = {
  63. .channels_min = 1,
  64. .channels_max = 128,
  65. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  66. .rate_min = 8000,
  67. .rate_max = 96000,
  68. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  69. },
  70. .playback = {
  71. .channels_min = 1,
  72. .channels_max = 128,
  73. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  74. .rate_min = 8000,
  75. .rate_max = 96000,
  76. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  77. },
  78. };
  79. static const struct snd_pcm_hardware chv3_dma_hw = {
  80. .info = SNDRV_PCM_INFO_INTERLEAVED |
  81. SNDRV_PCM_INFO_MMAP |
  82. SNDRV_PCM_INFO_MMAP_VALID |
  83. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  84. .buffer_bytes_max = I2S_MAX_BUFFER_SIZE,
  85. .period_bytes_min = 64,
  86. .period_bytes_max = 8192,
  87. .periods_min = 4,
  88. .periods_max = 256,
  89. };
  90. static inline void chv3_i2s_wr(struct chv3_i2s_dev *i2s, int offset, u32 val)
  91. {
  92. writel(val, i2s->iobase + offset);
  93. }
  94. static inline u32 chv3_i2s_rd(struct chv3_i2s_dev *i2s, int offset)
  95. {
  96. return readl(i2s->iobase + offset);
  97. }
  98. static irqreturn_t chv3_i2s_isr(int irq, void *data)
  99. {
  100. struct chv3_i2s_dev *i2s = data;
  101. u32 reg;
  102. reg = readl(i2s->iobase_irq + I2S_IRQ_CLR);
  103. if (!reg)
  104. return IRQ_NONE;
  105. if (reg & I2S_IRQ_RX_BIT)
  106. snd_pcm_period_elapsed(i2s->rx_substream);
  107. if (reg & I2S_IRQ_TX_BIT)
  108. snd_pcm_period_elapsed(i2s->tx_substream);
  109. writel(reg, i2s->iobase_irq + I2S_IRQ_CLR);
  110. return IRQ_HANDLED;
  111. }
  112. static int chv3_dma_open(struct snd_soc_component *component,
  113. struct snd_pcm_substream *substream)
  114. {
  115. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  116. struct chv3_i2s_dev *i2s = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  117. int res;
  118. snd_soc_set_runtime_hwparams(substream, &chv3_dma_hw);
  119. res = snd_pcm_hw_constraint_pow2(substream->runtime, 0,
  120. SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  121. if (res)
  122. return res;
  123. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  124. i2s->rx_substream = substream;
  125. else
  126. i2s->tx_substream = substream;
  127. return 0;
  128. }
  129. static int chv3_dma_close(struct snd_soc_component *component,
  130. struct snd_pcm_substream *substream)
  131. {
  132. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  133. struct chv3_i2s_dev *i2s = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  134. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
  135. chv3_i2s_wr(i2s, I2S_RX_ENABLE, 0);
  136. else
  137. chv3_i2s_wr(i2s, I2S_TX_ENABLE, 0);
  138. return 0;
  139. }
  140. static int chv3_dma_pcm_construct(struct snd_soc_component *component,
  141. struct snd_soc_pcm_runtime *rtd)
  142. {
  143. struct chv3_i2s_dev *i2s = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  144. struct snd_pcm_substream *substream;
  145. int res;
  146. substream = rtd->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  147. if (substream) {
  148. res = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, i2s->dev,
  149. I2S_MAX_BUFFER_SIZE, &substream->dma_buffer);
  150. if (res)
  151. return res;
  152. }
  153. substream = rtd->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  154. if (substream) {
  155. res = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, i2s->dev,
  156. I2S_MAX_BUFFER_SIZE, &substream->dma_buffer);
  157. if (res)
  158. return res;
  159. }
  160. return 0;
  161. }
  162. static int chv3_dma_hw_params(struct snd_soc_component *component,
  163. struct snd_pcm_substream *substream,
  164. struct snd_pcm_hw_params *params)
  165. {
  166. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  167. return 0;
  168. }
  169. static int chv3_dma_prepare(struct snd_soc_component *component,
  170. struct snd_pcm_substream *substream)
  171. {
  172. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  173. struct chv3_i2s_dev *i2s = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  174. unsigned int buffer_bytes, period_bytes, period_size;
  175. buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  176. period_bytes = snd_pcm_lib_period_bytes(substream);
  177. period_size = substream->runtime->period_size;
  178. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
  179. chv3_i2s_wr(i2s, I2S_SOFT_RESET, I2S_SOFT_RESET_RX_BIT);
  180. chv3_i2s_wr(i2s, I2S_RX_BASE_ADDR, substream->dma_buffer.addr);
  181. chv3_i2s_wr(i2s, I2S_RX_BUFFER_SIZE, buffer_bytes);
  182. chv3_i2s_wr(i2s, I2S_RX_IRQ, (period_size << 8) | 1);
  183. chv3_i2s_wr(i2s, I2S_RX_ENABLE, 1);
  184. } else {
  185. chv3_i2s_wr(i2s, I2S_SOFT_RESET, I2S_SOFT_RESET_TX_BIT);
  186. chv3_i2s_wr(i2s, I2S_TX_BASE_ADDR, substream->dma_buffer.addr);
  187. chv3_i2s_wr(i2s, I2S_TX_BUFFER_SIZE, buffer_bytes);
  188. chv3_i2s_wr(i2s, I2S_TX_IRQ, ((period_bytes / i2s->tx_bytes_to_fetch) << 8) | 1);
  189. chv3_i2s_wr(i2s, I2S_TX_ENABLE, 1);
  190. }
  191. writel(I2S_IRQ_RX_BIT | I2S_IRQ_TX_BIT, i2s->iobase_irq + I2S_IRQ_MASK);
  192. return 0;
  193. }
  194. static snd_pcm_uframes_t chv3_dma_pointer(struct snd_soc_component *component,
  195. struct snd_pcm_substream *substream)
  196. {
  197. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  198. struct chv3_i2s_dev *i2s = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  199. u32 frame_bytes, buffer_bytes;
  200. u32 idx_bytes;
  201. frame_bytes = substream->runtime->frame_bits * 8;
  202. buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  203. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) {
  204. idx_bytes = chv3_i2s_rd(i2s, I2S_RX_PRODUCER_IDX);
  205. } else {
  206. idx_bytes = chv3_i2s_rd(i2s, I2S_TX_CONSUMER_IDX);
  207. /* lag the pointer by one frame */
  208. idx_bytes = (idx_bytes - frame_bytes) & (buffer_bytes - 1);
  209. }
  210. return bytes_to_frames(substream->runtime, idx_bytes);
  211. }
  212. static int chv3_dma_ack(struct snd_soc_component *component,
  213. struct snd_pcm_substream *substream)
  214. {
  215. struct snd_pcm_runtime *runtime = substream->runtime;
  216. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  217. struct chv3_i2s_dev *i2s = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  218. unsigned int bytes, idx;
  219. bytes = frames_to_bytes(runtime, runtime->control->appl_ptr);
  220. idx = bytes & (snd_pcm_lib_buffer_bytes(substream) - 1);
  221. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
  222. chv3_i2s_wr(i2s, I2S_RX_CONSUMER_IDX, idx);
  223. else
  224. chv3_i2s_wr(i2s, I2S_TX_PRODUCER_IDX, idx);
  225. return 0;
  226. }
  227. static const struct snd_soc_component_driver chv3_i2s_comp = {
  228. .name = "chv3-i2s-comp",
  229. .open = chv3_dma_open,
  230. .close = chv3_dma_close,
  231. .pcm_construct = chv3_dma_pcm_construct,
  232. .hw_params = chv3_dma_hw_params,
  233. .prepare = chv3_dma_prepare,
  234. .pointer = chv3_dma_pointer,
  235. .ack = chv3_dma_ack,
  236. };
  237. static int chv3_i2s_probe(struct platform_device *pdev)
  238. {
  239. struct chv3_i2s_dev *i2s;
  240. int res;
  241. int irq;
  242. i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
  243. if (!i2s)
  244. return -ENOMEM;
  245. i2s->iobase = devm_platform_ioremap_resource(pdev, 0);
  246. if (IS_ERR(i2s->iobase))
  247. return PTR_ERR(i2s->iobase);
  248. i2s->iobase_irq = devm_platform_ioremap_resource(pdev, 1);
  249. if (IS_ERR(i2s->iobase_irq))
  250. return PTR_ERR(i2s->iobase_irq);
  251. i2s->tx_bytes_to_fetch = (chv3_i2s_rd(i2s, I2S_TX_IRQ_CONST) >> 8) & 0xffff;
  252. i2s->dev = &pdev->dev;
  253. dev_set_drvdata(&pdev->dev, i2s);
  254. irq = platform_get_irq(pdev, 0);
  255. if (irq < 0)
  256. return -ENXIO;
  257. res = devm_request_irq(i2s->dev, irq, chv3_i2s_isr, 0, "chv3-i2s", i2s);
  258. if (res)
  259. return res;
  260. res = devm_snd_soc_register_component(&pdev->dev, &chv3_i2s_comp,
  261. &chv3_i2s_dai, 1);
  262. if (res) {
  263. dev_err(&pdev->dev, "couldn't register component: %d\n", res);
  264. return res;
  265. }
  266. return 0;
  267. }
  268. static const struct of_device_id chv3_i2s_of_match[] = {
  269. { .compatible = "google,chv3-i2s" },
  270. {},
  271. };
  272. MODULE_DEVICE_TABLE(of, chv3_i2s_of_match);
  273. static struct platform_driver chv3_i2s_driver = {
  274. .probe = chv3_i2s_probe,
  275. .driver = {
  276. .name = "chv3-i2s",
  277. .of_match_table = chv3_i2s_of_match,
  278. },
  279. };
  280. module_platform_driver(chv3_i2s_driver);
  281. MODULE_AUTHOR("Pawel Anikiel <pan@semihalf.com>");
  282. MODULE_DESCRIPTION("Chameleon v3 I2S interface");
  283. MODULE_LICENSE("GPL");