ep93xx-i2s.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/sound/soc/ep93xx-i2s.c
  4. * EP93xx I2S driver
  5. *
  6. * Copyright (C) 2010 Ryan Mallon
  7. *
  8. * Based on the original driver by:
  9. * Copyright (C) 2007 Chase Douglas <chasedouglas@gmail>
  10. * Copyright (C) 2006 Lennert Buytenhek <[email protected]>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/clk.h>
  16. #include <linux/io.h>
  17. #include <sound/core.h>
  18. #include <sound/dmaengine_pcm.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/initval.h>
  22. #include <sound/soc.h>
  23. #include <linux/platform_data/dma-ep93xx.h>
  24. #include <linux/soc/cirrus/ep93xx.h>
  25. #include "ep93xx-pcm.h"
  26. #define EP93XX_I2S_TXCLKCFG 0x00
  27. #define EP93XX_I2S_RXCLKCFG 0x04
  28. #define EP93XX_I2S_GLSTS 0x08
  29. #define EP93XX_I2S_GLCTRL 0x0C
  30. #define EP93XX_I2S_I2STX0LFT 0x10
  31. #define EP93XX_I2S_I2STX0RT 0x14
  32. #define EP93XX_I2S_TXLINCTRLDATA 0x28
  33. #define EP93XX_I2S_TXCTRL 0x2C
  34. #define EP93XX_I2S_TXWRDLEN 0x30
  35. #define EP93XX_I2S_TX0EN 0x34
  36. #define EP93XX_I2S_RXLINCTRLDATA 0x58
  37. #define EP93XX_I2S_RXCTRL 0x5C
  38. #define EP93XX_I2S_RXWRDLEN 0x60
  39. #define EP93XX_I2S_RX0EN 0x64
  40. #define EP93XX_I2S_WRDLEN_16 (0 << 0)
  41. #define EP93XX_I2S_WRDLEN_24 (1 << 0)
  42. #define EP93XX_I2S_WRDLEN_32 (2 << 0)
  43. #define EP93XX_I2S_RXLINCTRLDATA_R_JUST BIT(1) /* Right justify */
  44. #define EP93XX_I2S_TXLINCTRLDATA_R_JUST BIT(2) /* Right justify */
  45. /*
  46. * Transmit empty interrupt level select:
  47. * 0 - Generate interrupt when FIFO is half empty
  48. * 1 - Generate interrupt when FIFO is empty
  49. */
  50. #define EP93XX_I2S_TXCTRL_TXEMPTY_LVL BIT(0)
  51. #define EP93XX_I2S_TXCTRL_TXUFIE BIT(1) /* Transmit interrupt enable */
  52. #define EP93XX_I2S_CLKCFG_LRS (1 << 0) /* lrclk polarity */
  53. #define EP93XX_I2S_CLKCFG_CKP (1 << 1) /* Bit clock polarity */
  54. #define EP93XX_I2S_CLKCFG_REL (1 << 2) /* First bit transition */
  55. #define EP93XX_I2S_CLKCFG_MASTER (1 << 3) /* Master mode */
  56. #define EP93XX_I2S_CLKCFG_NBCG (1 << 4) /* Not bit clock gating */
  57. #define EP93XX_I2S_GLSTS_TX0_FIFO_FULL BIT(12)
  58. struct ep93xx_i2s_info {
  59. struct clk *mclk;
  60. struct clk *sclk;
  61. struct clk *lrclk;
  62. void __iomem *regs;
  63. struct snd_dmaengine_dai_dma_data dma_params_rx;
  64. struct snd_dmaengine_dai_dma_data dma_params_tx;
  65. };
  66. static struct ep93xx_dma_data ep93xx_i2s_dma_data[] = {
  67. [SNDRV_PCM_STREAM_PLAYBACK] = {
  68. .name = "i2s-pcm-out",
  69. .port = EP93XX_DMA_I2S1,
  70. .direction = DMA_MEM_TO_DEV,
  71. },
  72. [SNDRV_PCM_STREAM_CAPTURE] = {
  73. .name = "i2s-pcm-in",
  74. .port = EP93XX_DMA_I2S1,
  75. .direction = DMA_DEV_TO_MEM,
  76. },
  77. };
  78. static inline void ep93xx_i2s_write_reg(struct ep93xx_i2s_info *info,
  79. unsigned reg, unsigned val)
  80. {
  81. __raw_writel(val, info->regs + reg);
  82. }
  83. static inline unsigned ep93xx_i2s_read_reg(struct ep93xx_i2s_info *info,
  84. unsigned reg)
  85. {
  86. return __raw_readl(info->regs + reg);
  87. }
  88. static void ep93xx_i2s_enable(struct ep93xx_i2s_info *info, int stream)
  89. {
  90. unsigned base_reg;
  91. if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
  92. (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
  93. /* Enable clocks */
  94. clk_prepare_enable(info->mclk);
  95. clk_prepare_enable(info->sclk);
  96. clk_prepare_enable(info->lrclk);
  97. /* Enable i2s */
  98. ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 1);
  99. }
  100. /* Enable fifo */
  101. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  102. base_reg = EP93XX_I2S_TX0EN;
  103. else
  104. base_reg = EP93XX_I2S_RX0EN;
  105. ep93xx_i2s_write_reg(info, base_reg, 1);
  106. /* Enable TX IRQs (FIFO empty or underflow) */
  107. if (IS_ENABLED(CONFIG_SND_EP93XX_SOC_I2S_WATCHDOG) &&
  108. stream == SNDRV_PCM_STREAM_PLAYBACK)
  109. ep93xx_i2s_write_reg(info, EP93XX_I2S_TXCTRL,
  110. EP93XX_I2S_TXCTRL_TXEMPTY_LVL |
  111. EP93XX_I2S_TXCTRL_TXUFIE);
  112. }
  113. static void ep93xx_i2s_disable(struct ep93xx_i2s_info *info, int stream)
  114. {
  115. unsigned base_reg;
  116. /* Disable IRQs */
  117. if (IS_ENABLED(CONFIG_SND_EP93XX_SOC_I2S_WATCHDOG) &&
  118. stream == SNDRV_PCM_STREAM_PLAYBACK)
  119. ep93xx_i2s_write_reg(info, EP93XX_I2S_TXCTRL, 0);
  120. /* Disable fifo */
  121. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  122. base_reg = EP93XX_I2S_TX0EN;
  123. else
  124. base_reg = EP93XX_I2S_RX0EN;
  125. ep93xx_i2s_write_reg(info, base_reg, 0);
  126. if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
  127. (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
  128. /* Disable i2s */
  129. ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 0);
  130. /* Disable clocks */
  131. clk_disable_unprepare(info->lrclk);
  132. clk_disable_unprepare(info->sclk);
  133. clk_disable_unprepare(info->mclk);
  134. }
  135. }
  136. /*
  137. * According to documentation I2S controller can handle underflow conditions
  138. * just fine, but in reality the state machine is sometimes confused so that
  139. * the whole stream is shifted by one byte. The watchdog below disables the TX
  140. * FIFO, fills the buffer with zeroes and re-enables the FIFO. State machine
  141. * is being reset and by filling the buffer we get some time before next
  142. * underflow happens.
  143. */
  144. static irqreturn_t ep93xx_i2s_interrupt(int irq, void *dev_id)
  145. {
  146. struct ep93xx_i2s_info *info = dev_id;
  147. /* Disable FIFO */
  148. ep93xx_i2s_write_reg(info, EP93XX_I2S_TX0EN, 0);
  149. /*
  150. * Fill TX FIFO with zeroes, this way we can defer next IRQs as much as
  151. * possible and get more time for DMA to catch up. Actually there are
  152. * only 8 samples in this FIFO, so even on 8kHz maximum deferral here is
  153. * 1ms.
  154. */
  155. while (!(ep93xx_i2s_read_reg(info, EP93XX_I2S_GLSTS) &
  156. EP93XX_I2S_GLSTS_TX0_FIFO_FULL)) {
  157. ep93xx_i2s_write_reg(info, EP93XX_I2S_I2STX0LFT, 0);
  158. ep93xx_i2s_write_reg(info, EP93XX_I2S_I2STX0RT, 0);
  159. }
  160. /* Re-enable FIFO */
  161. ep93xx_i2s_write_reg(info, EP93XX_I2S_TX0EN, 1);
  162. return IRQ_HANDLED;
  163. }
  164. static int ep93xx_i2s_dai_probe(struct snd_soc_dai *dai)
  165. {
  166. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
  167. info->dma_params_tx.filter_data =
  168. &ep93xx_i2s_dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  169. info->dma_params_rx.filter_data =
  170. &ep93xx_i2s_dma_data[SNDRV_PCM_STREAM_CAPTURE];
  171. dai->playback_dma_data = &info->dma_params_tx;
  172. dai->capture_dma_data = &info->dma_params_rx;
  173. return 0;
  174. }
  175. static void ep93xx_i2s_shutdown(struct snd_pcm_substream *substream,
  176. struct snd_soc_dai *dai)
  177. {
  178. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
  179. ep93xx_i2s_disable(info, substream->stream);
  180. }
  181. static int ep93xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  182. unsigned int fmt)
  183. {
  184. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(cpu_dai);
  185. unsigned int clk_cfg;
  186. unsigned int txlin_ctrl = 0;
  187. unsigned int rxlin_ctrl = 0;
  188. clk_cfg = ep93xx_i2s_read_reg(info, EP93XX_I2S_RXCLKCFG);
  189. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  190. case SND_SOC_DAIFMT_I2S:
  191. clk_cfg |= EP93XX_I2S_CLKCFG_REL;
  192. break;
  193. case SND_SOC_DAIFMT_LEFT_J:
  194. clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
  195. break;
  196. case SND_SOC_DAIFMT_RIGHT_J:
  197. clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
  198. rxlin_ctrl |= EP93XX_I2S_RXLINCTRLDATA_R_JUST;
  199. txlin_ctrl |= EP93XX_I2S_TXLINCTRLDATA_R_JUST;
  200. break;
  201. default:
  202. return -EINVAL;
  203. }
  204. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  205. case SND_SOC_DAIFMT_BP_FP:
  206. /* CPU is provider */
  207. clk_cfg |= EP93XX_I2S_CLKCFG_MASTER;
  208. break;
  209. case SND_SOC_DAIFMT_BC_FC:
  210. /* Codec is provider */
  211. clk_cfg &= ~EP93XX_I2S_CLKCFG_MASTER;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  217. case SND_SOC_DAIFMT_NB_NF:
  218. /* Negative bit clock, lrclk low on left word */
  219. clk_cfg &= ~(EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_LRS);
  220. break;
  221. case SND_SOC_DAIFMT_NB_IF:
  222. /* Negative bit clock, lrclk low on right word */
  223. clk_cfg &= ~EP93XX_I2S_CLKCFG_CKP;
  224. clk_cfg |= EP93XX_I2S_CLKCFG_LRS;
  225. break;
  226. case SND_SOC_DAIFMT_IB_NF:
  227. /* Positive bit clock, lrclk low on left word */
  228. clk_cfg |= EP93XX_I2S_CLKCFG_CKP;
  229. clk_cfg &= ~EP93XX_I2S_CLKCFG_LRS;
  230. break;
  231. case SND_SOC_DAIFMT_IB_IF:
  232. /* Positive bit clock, lrclk low on right word */
  233. clk_cfg |= EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_LRS;
  234. break;
  235. }
  236. /* Write new register values */
  237. ep93xx_i2s_write_reg(info, EP93XX_I2S_RXCLKCFG, clk_cfg);
  238. ep93xx_i2s_write_reg(info, EP93XX_I2S_TXCLKCFG, clk_cfg);
  239. ep93xx_i2s_write_reg(info, EP93XX_I2S_RXLINCTRLDATA, rxlin_ctrl);
  240. ep93xx_i2s_write_reg(info, EP93XX_I2S_TXLINCTRLDATA, txlin_ctrl);
  241. return 0;
  242. }
  243. static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream,
  244. struct snd_pcm_hw_params *params,
  245. struct snd_soc_dai *dai)
  246. {
  247. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
  248. unsigned word_len, div, sdiv, lrdiv;
  249. int err;
  250. switch (params_format(params)) {
  251. case SNDRV_PCM_FORMAT_S16_LE:
  252. word_len = EP93XX_I2S_WRDLEN_16;
  253. break;
  254. case SNDRV_PCM_FORMAT_S24_LE:
  255. word_len = EP93XX_I2S_WRDLEN_24;
  256. break;
  257. case SNDRV_PCM_FORMAT_S32_LE:
  258. word_len = EP93XX_I2S_WRDLEN_32;
  259. break;
  260. default:
  261. return -EINVAL;
  262. }
  263. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  264. ep93xx_i2s_write_reg(info, EP93XX_I2S_TXWRDLEN, word_len);
  265. else
  266. ep93xx_i2s_write_reg(info, EP93XX_I2S_RXWRDLEN, word_len);
  267. /*
  268. * EP93xx I2S module can be setup so SCLK / LRCLK value can be
  269. * 32, 64, 128. MCLK / SCLK value can be 2 and 4.
  270. * We set LRCLK equal to `rate' and minimum SCLK / LRCLK
  271. * value is 64, because our sample size is 32 bit * 2 channels.
  272. * I2S standard permits us to transmit more bits than
  273. * the codec uses.
  274. */
  275. div = clk_get_rate(info->mclk) / params_rate(params);
  276. sdiv = 4;
  277. if (div > (256 + 512) / 2) {
  278. lrdiv = 128;
  279. } else {
  280. lrdiv = 64;
  281. if (div < (128 + 256) / 2)
  282. sdiv = 2;
  283. }
  284. err = clk_set_rate(info->sclk, clk_get_rate(info->mclk) / sdiv);
  285. if (err)
  286. return err;
  287. err = clk_set_rate(info->lrclk, clk_get_rate(info->sclk) / lrdiv);
  288. if (err)
  289. return err;
  290. ep93xx_i2s_enable(info, substream->stream);
  291. return 0;
  292. }
  293. static int ep93xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id,
  294. unsigned int freq, int dir)
  295. {
  296. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(cpu_dai);
  297. if (dir == SND_SOC_CLOCK_IN || clk_id != 0)
  298. return -EINVAL;
  299. return clk_set_rate(info->mclk, freq);
  300. }
  301. #ifdef CONFIG_PM
  302. static int ep93xx_i2s_suspend(struct snd_soc_component *component)
  303. {
  304. struct ep93xx_i2s_info *info = snd_soc_component_get_drvdata(component);
  305. if (!snd_soc_component_active(component))
  306. return 0;
  307. ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_PLAYBACK);
  308. ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_CAPTURE);
  309. return 0;
  310. }
  311. static int ep93xx_i2s_resume(struct snd_soc_component *component)
  312. {
  313. struct ep93xx_i2s_info *info = snd_soc_component_get_drvdata(component);
  314. if (!snd_soc_component_active(component))
  315. return 0;
  316. ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_PLAYBACK);
  317. ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_CAPTURE);
  318. return 0;
  319. }
  320. #else
  321. #define ep93xx_i2s_suspend NULL
  322. #define ep93xx_i2s_resume NULL
  323. #endif
  324. static const struct snd_soc_dai_ops ep93xx_i2s_dai_ops = {
  325. .shutdown = ep93xx_i2s_shutdown,
  326. .hw_params = ep93xx_i2s_hw_params,
  327. .set_sysclk = ep93xx_i2s_set_sysclk,
  328. .set_fmt = ep93xx_i2s_set_dai_fmt,
  329. };
  330. #define EP93XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
  331. static struct snd_soc_dai_driver ep93xx_i2s_dai = {
  332. .symmetric_rate = 1,
  333. .probe = ep93xx_i2s_dai_probe,
  334. .playback = {
  335. .channels_min = 2,
  336. .channels_max = 2,
  337. .rates = SNDRV_PCM_RATE_8000_192000,
  338. .formats = EP93XX_I2S_FORMATS,
  339. },
  340. .capture = {
  341. .channels_min = 2,
  342. .channels_max = 2,
  343. .rates = SNDRV_PCM_RATE_8000_192000,
  344. .formats = EP93XX_I2S_FORMATS,
  345. },
  346. .ops = &ep93xx_i2s_dai_ops,
  347. };
  348. static const struct snd_soc_component_driver ep93xx_i2s_component = {
  349. .name = "ep93xx-i2s",
  350. .suspend = ep93xx_i2s_suspend,
  351. .resume = ep93xx_i2s_resume,
  352. .legacy_dai_naming = 1,
  353. };
  354. static int ep93xx_i2s_probe(struct platform_device *pdev)
  355. {
  356. struct ep93xx_i2s_info *info;
  357. int err;
  358. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  359. if (!info)
  360. return -ENOMEM;
  361. info->regs = devm_platform_ioremap_resource(pdev, 0);
  362. if (IS_ERR(info->regs))
  363. return PTR_ERR(info->regs);
  364. if (IS_ENABLED(CONFIG_SND_EP93XX_SOC_I2S_WATCHDOG)) {
  365. int irq = platform_get_irq(pdev, 0);
  366. if (irq <= 0)
  367. return irq < 0 ? irq : -ENODEV;
  368. err = devm_request_irq(&pdev->dev, irq, ep93xx_i2s_interrupt, 0,
  369. pdev->name, info);
  370. if (err)
  371. return err;
  372. }
  373. info->mclk = clk_get(&pdev->dev, "mclk");
  374. if (IS_ERR(info->mclk)) {
  375. err = PTR_ERR(info->mclk);
  376. goto fail;
  377. }
  378. info->sclk = clk_get(&pdev->dev, "sclk");
  379. if (IS_ERR(info->sclk)) {
  380. err = PTR_ERR(info->sclk);
  381. goto fail_put_mclk;
  382. }
  383. info->lrclk = clk_get(&pdev->dev, "lrclk");
  384. if (IS_ERR(info->lrclk)) {
  385. err = PTR_ERR(info->lrclk);
  386. goto fail_put_sclk;
  387. }
  388. dev_set_drvdata(&pdev->dev, info);
  389. err = devm_snd_soc_register_component(&pdev->dev, &ep93xx_i2s_component,
  390. &ep93xx_i2s_dai, 1);
  391. if (err)
  392. goto fail_put_lrclk;
  393. err = devm_ep93xx_pcm_platform_register(&pdev->dev);
  394. if (err)
  395. goto fail_put_lrclk;
  396. return 0;
  397. fail_put_lrclk:
  398. clk_put(info->lrclk);
  399. fail_put_sclk:
  400. clk_put(info->sclk);
  401. fail_put_mclk:
  402. clk_put(info->mclk);
  403. fail:
  404. return err;
  405. }
  406. static int ep93xx_i2s_remove(struct platform_device *pdev)
  407. {
  408. struct ep93xx_i2s_info *info = dev_get_drvdata(&pdev->dev);
  409. clk_put(info->lrclk);
  410. clk_put(info->sclk);
  411. clk_put(info->mclk);
  412. return 0;
  413. }
  414. static struct platform_driver ep93xx_i2s_driver = {
  415. .probe = ep93xx_i2s_probe,
  416. .remove = ep93xx_i2s_remove,
  417. .driver = {
  418. .name = "ep93xx-i2s",
  419. },
  420. };
  421. module_platform_driver(ep93xx_i2s_driver);
  422. MODULE_ALIAS("platform:ep93xx-i2s");
  423. MODULE_AUTHOR("Ryan Mallon");
  424. MODULE_DESCRIPTION("EP93XX I2S driver");
  425. MODULE_LICENSE("GPL");