bcm63xx-pcm-whistler.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. // linux/sound/bcm/bcm63xx-pcm-whistler.c
  3. // BCM63xx whistler pcm interface
  4. // Copyright (c) 2020 Broadcom Corporation
  5. // Author: Kevin-Ke Li <[email protected]>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/io.h>
  8. #include <linux/irq.h>
  9. #include <linux/module.h>
  10. #include <sound/pcm_params.h>
  11. #include <linux/regmap.h>
  12. #include <linux/of_device.h>
  13. #include <sound/soc.h>
  14. #include "bcm63xx-i2s.h"
  15. struct i2s_dma_desc {
  16. unsigned char *dma_area;
  17. dma_addr_t dma_addr;
  18. unsigned int dma_len;
  19. };
  20. struct bcm63xx_runtime_data {
  21. int dma_len;
  22. dma_addr_t dma_addr;
  23. dma_addr_t dma_addr_next;
  24. };
  25. static const struct snd_pcm_hardware bcm63xx_pcm_hardware = {
  26. .info = SNDRV_PCM_INFO_MMAP |
  27. SNDRV_PCM_INFO_MMAP_VALID |
  28. SNDRV_PCM_INFO_INTERLEAVED |
  29. SNDRV_PCM_INFO_PAUSE |
  30. SNDRV_PCM_INFO_RESUME,
  31. .formats = SNDRV_PCM_FMTBIT_S32_LE, /* support S32 only */
  32. .period_bytes_max = 8192 - 32,
  33. .periods_min = 1,
  34. .periods_max = PAGE_SIZE/sizeof(struct i2s_dma_desc),
  35. .buffer_bytes_max = 128 * 1024,
  36. .fifo_size = 32,
  37. };
  38. static int bcm63xx_pcm_hw_params(struct snd_soc_component *component,
  39. struct snd_pcm_substream *substream,
  40. struct snd_pcm_hw_params *params)
  41. {
  42. struct i2s_dma_desc *dma_desc;
  43. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  44. dma_desc = kzalloc(sizeof(*dma_desc), GFP_NOWAIT);
  45. if (!dma_desc)
  46. return -ENOMEM;
  47. snd_soc_dai_set_dma_data(asoc_rtd_to_cpu(rtd, 0), substream, dma_desc);
  48. return 0;
  49. }
  50. static int bcm63xx_pcm_hw_free(struct snd_soc_component *component,
  51. struct snd_pcm_substream *substream)
  52. {
  53. struct i2s_dma_desc *dma_desc;
  54. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  55. dma_desc = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  56. kfree(dma_desc);
  57. return 0;
  58. }
  59. static int bcm63xx_pcm_trigger(struct snd_soc_component *component,
  60. struct snd_pcm_substream *substream, int cmd)
  61. {
  62. int ret = 0;
  63. struct snd_soc_pcm_runtime *rtd;
  64. struct bcm_i2s_priv *i2s_priv;
  65. struct regmap *regmap_i2s;
  66. rtd = asoc_substream_to_rtd(substream);
  67. i2s_priv = dev_get_drvdata(asoc_rtd_to_cpu(rtd, 0)->dev);
  68. regmap_i2s = i2s_priv->regmap_i2s;
  69. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  70. switch (cmd) {
  71. case SNDRV_PCM_TRIGGER_START:
  72. regmap_update_bits(regmap_i2s,
  73. I2S_TX_IRQ_EN,
  74. I2S_TX_DESC_OFF_INTR_EN,
  75. I2S_TX_DESC_OFF_INTR_EN);
  76. regmap_update_bits(regmap_i2s,
  77. I2S_TX_CFG,
  78. I2S_TX_ENABLE_MASK,
  79. I2S_TX_ENABLE);
  80. break;
  81. case SNDRV_PCM_TRIGGER_STOP:
  82. case SNDRV_PCM_TRIGGER_SUSPEND:
  83. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  84. regmap_write(regmap_i2s,
  85. I2S_TX_IRQ_EN,
  86. 0);
  87. regmap_update_bits(regmap_i2s,
  88. I2S_TX_CFG,
  89. I2S_TX_ENABLE_MASK,
  90. 0);
  91. break;
  92. default:
  93. ret = -EINVAL;
  94. }
  95. } else {
  96. switch (cmd) {
  97. case SNDRV_PCM_TRIGGER_START:
  98. regmap_update_bits(regmap_i2s,
  99. I2S_RX_IRQ_EN,
  100. I2S_RX_DESC_OFF_INTR_EN_MSK,
  101. I2S_RX_DESC_OFF_INTR_EN);
  102. regmap_update_bits(regmap_i2s,
  103. I2S_RX_CFG,
  104. I2S_RX_ENABLE_MASK,
  105. I2S_RX_ENABLE);
  106. break;
  107. case SNDRV_PCM_TRIGGER_STOP:
  108. case SNDRV_PCM_TRIGGER_SUSPEND:
  109. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  110. regmap_update_bits(regmap_i2s,
  111. I2S_RX_IRQ_EN,
  112. I2S_RX_DESC_OFF_INTR_EN_MSK,
  113. 0);
  114. regmap_update_bits(regmap_i2s,
  115. I2S_RX_CFG,
  116. I2S_RX_ENABLE_MASK,
  117. 0);
  118. break;
  119. default:
  120. ret = -EINVAL;
  121. }
  122. }
  123. return ret;
  124. }
  125. static int bcm63xx_pcm_prepare(struct snd_soc_component *component,
  126. struct snd_pcm_substream *substream)
  127. {
  128. struct i2s_dma_desc *dma_desc;
  129. struct regmap *regmap_i2s;
  130. struct bcm_i2s_priv *i2s_priv;
  131. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  132. struct snd_pcm_runtime *runtime = substream->runtime;
  133. uint32_t regaddr_desclen, regaddr_descaddr;
  134. dma_desc = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  135. dma_desc->dma_len = snd_pcm_lib_period_bytes(substream);
  136. dma_desc->dma_addr = runtime->dma_addr;
  137. dma_desc->dma_area = runtime->dma_area;
  138. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  139. regaddr_desclen = I2S_TX_DESC_IFF_LEN;
  140. regaddr_descaddr = I2S_TX_DESC_IFF_ADDR;
  141. } else {
  142. regaddr_desclen = I2S_RX_DESC_IFF_LEN;
  143. regaddr_descaddr = I2S_RX_DESC_IFF_ADDR;
  144. }
  145. i2s_priv = dev_get_drvdata(asoc_rtd_to_cpu(rtd, 0)->dev);
  146. regmap_i2s = i2s_priv->regmap_i2s;
  147. regmap_write(regmap_i2s, regaddr_desclen, dma_desc->dma_len);
  148. regmap_write(regmap_i2s, regaddr_descaddr, dma_desc->dma_addr);
  149. return 0;
  150. }
  151. static snd_pcm_uframes_t
  152. bcm63xx_pcm_pointer(struct snd_soc_component *component,
  153. struct snd_pcm_substream *substream)
  154. {
  155. snd_pcm_uframes_t x;
  156. struct bcm63xx_runtime_data *prtd = substream->runtime->private_data;
  157. if (!prtd->dma_addr_next)
  158. prtd->dma_addr_next = substream->runtime->dma_addr;
  159. x = bytes_to_frames(substream->runtime,
  160. prtd->dma_addr_next - substream->runtime->dma_addr);
  161. return x == substream->runtime->buffer_size ? 0 : x;
  162. }
  163. static int bcm63xx_pcm_open(struct snd_soc_component *component,
  164. struct snd_pcm_substream *substream)
  165. {
  166. int ret = 0;
  167. struct snd_pcm_runtime *runtime = substream->runtime;
  168. struct bcm63xx_runtime_data *prtd;
  169. runtime->hw = bcm63xx_pcm_hardware;
  170. ret = snd_pcm_hw_constraint_step(runtime, 0,
  171. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  172. if (ret)
  173. goto out;
  174. ret = snd_pcm_hw_constraint_step(runtime, 0,
  175. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  176. if (ret)
  177. goto out;
  178. ret = snd_pcm_hw_constraint_integer(runtime,
  179. SNDRV_PCM_HW_PARAM_PERIODS);
  180. if (ret < 0)
  181. goto out;
  182. ret = -ENOMEM;
  183. prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
  184. if (!prtd)
  185. goto out;
  186. runtime->private_data = prtd;
  187. return 0;
  188. out:
  189. return ret;
  190. }
  191. static int bcm63xx_pcm_close(struct snd_soc_component *component,
  192. struct snd_pcm_substream *substream)
  193. {
  194. struct snd_pcm_runtime *runtime = substream->runtime;
  195. struct bcm63xx_runtime_data *prtd = runtime->private_data;
  196. kfree(prtd);
  197. return 0;
  198. }
  199. static irqreturn_t i2s_dma_isr(int irq, void *bcm_i2s_priv)
  200. {
  201. unsigned int availdepth, ifflevel, offlevel, int_status, val_1, val_2;
  202. struct bcm63xx_runtime_data *prtd;
  203. struct snd_pcm_substream *substream;
  204. struct snd_pcm_runtime *runtime;
  205. struct regmap *regmap_i2s;
  206. struct i2s_dma_desc *dma_desc;
  207. struct snd_soc_pcm_runtime *rtd;
  208. struct bcm_i2s_priv *i2s_priv;
  209. i2s_priv = (struct bcm_i2s_priv *)bcm_i2s_priv;
  210. regmap_i2s = i2s_priv->regmap_i2s;
  211. /* rx */
  212. regmap_read(regmap_i2s, I2S_RX_IRQ_CTL, &int_status);
  213. if (int_status & I2S_RX_DESC_OFF_INTR_EN_MSK) {
  214. substream = i2s_priv->capture_substream;
  215. runtime = substream->runtime;
  216. rtd = asoc_substream_to_rtd(substream);
  217. prtd = runtime->private_data;
  218. dma_desc = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  219. offlevel = (int_status & I2S_RX_DESC_OFF_LEVEL_MASK) >>
  220. I2S_RX_DESC_OFF_LEVEL_SHIFT;
  221. while (offlevel) {
  222. regmap_read(regmap_i2s, I2S_RX_DESC_OFF_ADDR, &val_1);
  223. regmap_read(regmap_i2s, I2S_RX_DESC_OFF_LEN, &val_2);
  224. offlevel--;
  225. }
  226. prtd->dma_addr_next = val_1 + val_2;
  227. ifflevel = (int_status & I2S_RX_DESC_IFF_LEVEL_MASK) >>
  228. I2S_RX_DESC_IFF_LEVEL_SHIFT;
  229. availdepth = I2S_DESC_FIFO_DEPTH - ifflevel;
  230. while (availdepth) {
  231. dma_desc->dma_addr +=
  232. snd_pcm_lib_period_bytes(substream);
  233. dma_desc->dma_area +=
  234. snd_pcm_lib_period_bytes(substream);
  235. if (dma_desc->dma_addr - runtime->dma_addr >=
  236. runtime->dma_bytes) {
  237. dma_desc->dma_addr = runtime->dma_addr;
  238. dma_desc->dma_area = runtime->dma_area;
  239. }
  240. prtd->dma_addr = dma_desc->dma_addr;
  241. regmap_write(regmap_i2s, I2S_RX_DESC_IFF_LEN,
  242. snd_pcm_lib_period_bytes(substream));
  243. regmap_write(regmap_i2s, I2S_RX_DESC_IFF_ADDR,
  244. dma_desc->dma_addr);
  245. availdepth--;
  246. }
  247. snd_pcm_period_elapsed(substream);
  248. /* Clear interrupt by writing 0 */
  249. regmap_update_bits(regmap_i2s, I2S_RX_IRQ_CTL,
  250. I2S_RX_INTR_MASK, 0);
  251. }
  252. /* tx */
  253. regmap_read(regmap_i2s, I2S_TX_IRQ_CTL, &int_status);
  254. if (int_status & I2S_TX_DESC_OFF_INTR_EN_MSK) {
  255. substream = i2s_priv->play_substream;
  256. runtime = substream->runtime;
  257. rtd = asoc_substream_to_rtd(substream);
  258. prtd = runtime->private_data;
  259. dma_desc = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
  260. offlevel = (int_status & I2S_TX_DESC_OFF_LEVEL_MASK) >>
  261. I2S_TX_DESC_OFF_LEVEL_SHIFT;
  262. while (offlevel) {
  263. regmap_read(regmap_i2s, I2S_TX_DESC_OFF_ADDR, &val_1);
  264. regmap_read(regmap_i2s, I2S_TX_DESC_OFF_LEN, &val_2);
  265. prtd->dma_addr_next = val_1 + val_2;
  266. offlevel--;
  267. }
  268. ifflevel = (int_status & I2S_TX_DESC_IFF_LEVEL_MASK) >>
  269. I2S_TX_DESC_IFF_LEVEL_SHIFT;
  270. availdepth = I2S_DESC_FIFO_DEPTH - ifflevel;
  271. while (availdepth) {
  272. dma_desc->dma_addr +=
  273. snd_pcm_lib_period_bytes(substream);
  274. dma_desc->dma_area +=
  275. snd_pcm_lib_period_bytes(substream);
  276. if (dma_desc->dma_addr - runtime->dma_addr >=
  277. runtime->dma_bytes) {
  278. dma_desc->dma_addr = runtime->dma_addr;
  279. dma_desc->dma_area = runtime->dma_area;
  280. }
  281. prtd->dma_addr = dma_desc->dma_addr;
  282. regmap_write(regmap_i2s, I2S_TX_DESC_IFF_LEN,
  283. snd_pcm_lib_period_bytes(substream));
  284. regmap_write(regmap_i2s, I2S_TX_DESC_IFF_ADDR,
  285. dma_desc->dma_addr);
  286. availdepth--;
  287. }
  288. snd_pcm_period_elapsed(substream);
  289. /* Clear interrupt by writing 0 */
  290. regmap_update_bits(regmap_i2s, I2S_TX_IRQ_CTL,
  291. I2S_TX_INTR_MASK, 0);
  292. }
  293. return IRQ_HANDLED;
  294. }
  295. static int bcm63xx_soc_pcm_new(struct snd_soc_component *component,
  296. struct snd_soc_pcm_runtime *rtd)
  297. {
  298. struct snd_pcm *pcm = rtd->pcm;
  299. struct bcm_i2s_priv *i2s_priv;
  300. int ret;
  301. i2s_priv = dev_get_drvdata(asoc_rtd_to_cpu(rtd, 0)->dev);
  302. of_dma_configure(pcm->card->dev, pcm->card->dev->of_node, 1);
  303. ret = dma_coerce_mask_and_coherent(pcm->card->dev, DMA_BIT_MASK(32));
  304. if (ret)
  305. return ret;
  306. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
  307. i2s_priv->play_substream =
  308. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  309. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
  310. i2s_priv->capture_substream =
  311. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  312. return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_WC,
  313. pcm->card->dev,
  314. bcm63xx_pcm_hardware.buffer_bytes_max);
  315. }
  316. static const struct snd_soc_component_driver bcm63xx_soc_platform = {
  317. .open = bcm63xx_pcm_open,
  318. .close = bcm63xx_pcm_close,
  319. .hw_params = bcm63xx_pcm_hw_params,
  320. .hw_free = bcm63xx_pcm_hw_free,
  321. .prepare = bcm63xx_pcm_prepare,
  322. .trigger = bcm63xx_pcm_trigger,
  323. .pointer = bcm63xx_pcm_pointer,
  324. .pcm_construct = bcm63xx_soc_pcm_new,
  325. };
  326. int bcm63xx_soc_platform_probe(struct platform_device *pdev,
  327. struct bcm_i2s_priv *i2s_priv)
  328. {
  329. int ret;
  330. ret = platform_get_irq(pdev, 0);
  331. if (ret < 0)
  332. return ret;
  333. ret = devm_request_irq(&pdev->dev, ret, i2s_dma_isr,
  334. irq_get_trigger_type(ret), "i2s_dma", (void *)i2s_priv);
  335. if (ret) {
  336. dev_err(&pdev->dev,
  337. "i2s_init: failed to request interrupt.ret=%d\n", ret);
  338. return ret;
  339. }
  340. return devm_snd_soc_register_component(&pdev->dev,
  341. &bcm63xx_soc_platform, NULL, 0);
  342. }
  343. int bcm63xx_soc_platform_remove(struct platform_device *pdev)
  344. {
  345. return 0;
  346. }
  347. MODULE_AUTHOR("Kevin,Li <[email protected]>");
  348. MODULE_DESCRIPTION("Broadcom DSL XPON ASOC PCM Interface");
  349. MODULE_LICENSE("GPL v2");