ep93xx-i2s.c 13 KB

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