ep93xx-i2s.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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_GLCTRL 0x0C
  34. #define EP93XX_I2S_TXLINCTRLDATA 0x28
  35. #define EP93XX_I2S_TXCTRL 0x2C
  36. #define EP93XX_I2S_TXWRDLEN 0x30
  37. #define EP93XX_I2S_TX0EN 0x34
  38. #define EP93XX_I2S_RXLINCTRLDATA 0x58
  39. #define EP93XX_I2S_RXCTRL 0x5C
  40. #define EP93XX_I2S_RXWRDLEN 0x60
  41. #define EP93XX_I2S_RX0EN 0x64
  42. #define EP93XX_I2S_WRDLEN_16 (0 << 0)
  43. #define EP93XX_I2S_WRDLEN_24 (1 << 0)
  44. #define EP93XX_I2S_WRDLEN_32 (2 << 0)
  45. #define EP93XX_I2S_LINCTRLDATA_R_JUST (1 << 2) /* Right justify */
  46. #define EP93XX_I2S_CLKCFG_LRS (1 << 0) /* lrclk polarity */
  47. #define EP93XX_I2S_CLKCFG_CKP (1 << 1) /* Bit clock polarity */
  48. #define EP93XX_I2S_CLKCFG_REL (1 << 2) /* First bit transition */
  49. #define EP93XX_I2S_CLKCFG_MASTER (1 << 3) /* Master mode */
  50. #define EP93XX_I2S_CLKCFG_NBCG (1 << 4) /* Not bit clock gating */
  51. struct ep93xx_i2s_info {
  52. struct clk *mclk;
  53. struct clk *sclk;
  54. struct clk *lrclk;
  55. void __iomem *regs;
  56. struct snd_dmaengine_dai_dma_data dma_params_rx;
  57. struct snd_dmaengine_dai_dma_data dma_params_tx;
  58. };
  59. static struct ep93xx_dma_data ep93xx_i2s_dma_data[] = {
  60. [SNDRV_PCM_STREAM_PLAYBACK] = {
  61. .name = "i2s-pcm-out",
  62. .port = EP93XX_DMA_I2S1,
  63. .direction = DMA_MEM_TO_DEV,
  64. },
  65. [SNDRV_PCM_STREAM_CAPTURE] = {
  66. .name = "i2s-pcm-in",
  67. .port = EP93XX_DMA_I2S1,
  68. .direction = DMA_DEV_TO_MEM,
  69. },
  70. };
  71. static inline void ep93xx_i2s_write_reg(struct ep93xx_i2s_info *info,
  72. unsigned reg, unsigned val)
  73. {
  74. __raw_writel(val, info->regs + reg);
  75. }
  76. static inline unsigned ep93xx_i2s_read_reg(struct ep93xx_i2s_info *info,
  77. unsigned reg)
  78. {
  79. return __raw_readl(info->regs + reg);
  80. }
  81. static void ep93xx_i2s_enable(struct ep93xx_i2s_info *info, int stream)
  82. {
  83. unsigned base_reg;
  84. int i;
  85. if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
  86. (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
  87. /* Enable clocks */
  88. clk_enable(info->mclk);
  89. clk_enable(info->sclk);
  90. clk_enable(info->lrclk);
  91. /* Enable i2s */
  92. ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 1);
  93. }
  94. /* Enable fifos */
  95. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  96. base_reg = EP93XX_I2S_TX0EN;
  97. else
  98. base_reg = EP93XX_I2S_RX0EN;
  99. for (i = 0; i < 3; i++)
  100. ep93xx_i2s_write_reg(info, base_reg + (i * 4), 1);
  101. }
  102. static void ep93xx_i2s_disable(struct ep93xx_i2s_info *info, int stream)
  103. {
  104. unsigned base_reg;
  105. int i;
  106. /* Disable fifos */
  107. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  108. base_reg = EP93XX_I2S_TX0EN;
  109. else
  110. base_reg = EP93XX_I2S_RX0EN;
  111. for (i = 0; i < 3; i++)
  112. ep93xx_i2s_write_reg(info, base_reg + (i * 4), 0);
  113. if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
  114. (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
  115. /* Disable i2s */
  116. ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 0);
  117. /* Disable clocks */
  118. clk_disable(info->lrclk);
  119. clk_disable(info->sclk);
  120. clk_disable(info->mclk);
  121. }
  122. }
  123. static int ep93xx_i2s_dai_probe(struct snd_soc_dai *dai)
  124. {
  125. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
  126. info->dma_params_tx.filter_data =
  127. &ep93xx_i2s_dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  128. info->dma_params_rx.filter_data =
  129. &ep93xx_i2s_dma_data[SNDRV_PCM_STREAM_CAPTURE];
  130. dai->playback_dma_data = &info->dma_params_tx;
  131. dai->capture_dma_data = &info->dma_params_rx;
  132. return 0;
  133. }
  134. static void ep93xx_i2s_shutdown(struct snd_pcm_substream *substream,
  135. struct snd_soc_dai *dai)
  136. {
  137. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
  138. ep93xx_i2s_disable(info, substream->stream);
  139. }
  140. static int ep93xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  141. unsigned int fmt)
  142. {
  143. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(cpu_dai);
  144. unsigned int clk_cfg, lin_ctrl;
  145. clk_cfg = ep93xx_i2s_read_reg(info, EP93XX_I2S_RXCLKCFG);
  146. lin_ctrl = ep93xx_i2s_read_reg(info, EP93XX_I2S_RXLINCTRLDATA);
  147. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  148. case SND_SOC_DAIFMT_I2S:
  149. clk_cfg |= EP93XX_I2S_CLKCFG_REL;
  150. lin_ctrl &= ~EP93XX_I2S_LINCTRLDATA_R_JUST;
  151. break;
  152. case SND_SOC_DAIFMT_LEFT_J:
  153. clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
  154. lin_ctrl &= ~EP93XX_I2S_LINCTRLDATA_R_JUST;
  155. break;
  156. case SND_SOC_DAIFMT_RIGHT_J:
  157. clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
  158. lin_ctrl |= EP93XX_I2S_LINCTRLDATA_R_JUST;
  159. break;
  160. default:
  161. return -EINVAL;
  162. }
  163. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  164. case SND_SOC_DAIFMT_CBS_CFS:
  165. /* CPU is master */
  166. clk_cfg |= EP93XX_I2S_CLKCFG_MASTER;
  167. break;
  168. case SND_SOC_DAIFMT_CBM_CFM:
  169. /* Codec is master */
  170. clk_cfg &= ~EP93XX_I2S_CLKCFG_MASTER;
  171. break;
  172. default:
  173. return -EINVAL;
  174. }
  175. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  176. case SND_SOC_DAIFMT_NB_NF:
  177. /* Negative bit clock, lrclk low on left word */
  178. clk_cfg &= ~(EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_REL);
  179. break;
  180. case SND_SOC_DAIFMT_NB_IF:
  181. /* Negative bit clock, lrclk low on right word */
  182. clk_cfg &= ~EP93XX_I2S_CLKCFG_CKP;
  183. clk_cfg |= EP93XX_I2S_CLKCFG_REL;
  184. break;
  185. case SND_SOC_DAIFMT_IB_NF:
  186. /* Positive bit clock, lrclk low on left word */
  187. clk_cfg |= EP93XX_I2S_CLKCFG_CKP;
  188. clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
  189. break;
  190. case SND_SOC_DAIFMT_IB_IF:
  191. /* Positive bit clock, lrclk low on right word */
  192. clk_cfg |= EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_REL;
  193. break;
  194. }
  195. /* Write new register values */
  196. ep93xx_i2s_write_reg(info, EP93XX_I2S_RXCLKCFG, clk_cfg);
  197. ep93xx_i2s_write_reg(info, EP93XX_I2S_TXCLKCFG, clk_cfg);
  198. ep93xx_i2s_write_reg(info, EP93XX_I2S_RXLINCTRLDATA, lin_ctrl);
  199. ep93xx_i2s_write_reg(info, EP93XX_I2S_TXLINCTRLDATA, lin_ctrl);
  200. return 0;
  201. }
  202. static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream,
  203. struct snd_pcm_hw_params *params,
  204. struct snd_soc_dai *dai)
  205. {
  206. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
  207. unsigned word_len, div, sdiv, lrdiv;
  208. int err;
  209. switch (params_format(params)) {
  210. case SNDRV_PCM_FORMAT_S16_LE:
  211. word_len = EP93XX_I2S_WRDLEN_16;
  212. break;
  213. case SNDRV_PCM_FORMAT_S24_LE:
  214. word_len = EP93XX_I2S_WRDLEN_24;
  215. break;
  216. case SNDRV_PCM_FORMAT_S32_LE:
  217. word_len = EP93XX_I2S_WRDLEN_32;
  218. break;
  219. default:
  220. return -EINVAL;
  221. }
  222. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  223. ep93xx_i2s_write_reg(info, EP93XX_I2S_TXWRDLEN, word_len);
  224. else
  225. ep93xx_i2s_write_reg(info, EP93XX_I2S_RXWRDLEN, word_len);
  226. /*
  227. * EP93xx I2S module can be setup so SCLK / LRCLK value can be
  228. * 32, 64, 128. MCLK / SCLK value can be 2 and 4.
  229. * We set LRCLK equal to `rate' and minimum SCLK / LRCLK
  230. * value is 64, because our sample size is 32 bit * 2 channels.
  231. * I2S standard permits us to transmit more bits than
  232. * the codec uses.
  233. */
  234. div = clk_get_rate(info->mclk) / params_rate(params);
  235. sdiv = 4;
  236. if (div > (256 + 512) / 2) {
  237. lrdiv = 128;
  238. } else {
  239. lrdiv = 64;
  240. if (div < (128 + 256) / 2)
  241. sdiv = 2;
  242. }
  243. err = clk_set_rate(info->sclk, clk_get_rate(info->mclk) / sdiv);
  244. if (err)
  245. return err;
  246. err = clk_set_rate(info->lrclk, clk_get_rate(info->sclk) / lrdiv);
  247. if (err)
  248. return err;
  249. ep93xx_i2s_enable(info, substream->stream);
  250. return 0;
  251. }
  252. static int ep93xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id,
  253. unsigned int freq, int dir)
  254. {
  255. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(cpu_dai);
  256. if (dir == SND_SOC_CLOCK_IN || clk_id != 0)
  257. return -EINVAL;
  258. return clk_set_rate(info->mclk, freq);
  259. }
  260. #ifdef CONFIG_PM
  261. static int ep93xx_i2s_suspend(struct snd_soc_dai *dai)
  262. {
  263. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
  264. if (!dai->active)
  265. return 0;
  266. ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_PLAYBACK);
  267. ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_CAPTURE);
  268. return 0;
  269. }
  270. static int ep93xx_i2s_resume(struct snd_soc_dai *dai)
  271. {
  272. struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
  273. if (!dai->active)
  274. return 0;
  275. ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_PLAYBACK);
  276. ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_CAPTURE);
  277. return 0;
  278. }
  279. #else
  280. #define ep93xx_i2s_suspend NULL
  281. #define ep93xx_i2s_resume NULL
  282. #endif
  283. static const struct snd_soc_dai_ops ep93xx_i2s_dai_ops = {
  284. .shutdown = ep93xx_i2s_shutdown,
  285. .hw_params = ep93xx_i2s_hw_params,
  286. .set_sysclk = ep93xx_i2s_set_sysclk,
  287. .set_fmt = ep93xx_i2s_set_dai_fmt,
  288. };
  289. #define EP93XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
  290. static struct snd_soc_dai_driver ep93xx_i2s_dai = {
  291. .symmetric_rates= 1,
  292. .probe = ep93xx_i2s_dai_probe,
  293. .suspend = ep93xx_i2s_suspend,
  294. .resume = ep93xx_i2s_resume,
  295. .playback = {
  296. .channels_min = 2,
  297. .channels_max = 2,
  298. .rates = SNDRV_PCM_RATE_8000_192000,
  299. .formats = EP93XX_I2S_FORMATS,
  300. },
  301. .capture = {
  302. .channels_min = 2,
  303. .channels_max = 2,
  304. .rates = SNDRV_PCM_RATE_8000_192000,
  305. .formats = EP93XX_I2S_FORMATS,
  306. },
  307. .ops = &ep93xx_i2s_dai_ops,
  308. };
  309. static const struct snd_soc_component_driver ep93xx_i2s_component = {
  310. .name = "ep93xx-i2s",
  311. };
  312. static int ep93xx_i2s_probe(struct platform_device *pdev)
  313. {
  314. struct ep93xx_i2s_info *info;
  315. struct resource *res;
  316. int err;
  317. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  318. if (!info)
  319. return -ENOMEM;
  320. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  321. info->regs = devm_ioremap_resource(&pdev->dev, res);
  322. if (IS_ERR(info->regs))
  323. return PTR_ERR(info->regs);
  324. info->mclk = clk_get(&pdev->dev, "mclk");
  325. if (IS_ERR(info->mclk)) {
  326. err = PTR_ERR(info->mclk);
  327. goto fail;
  328. }
  329. info->sclk = clk_get(&pdev->dev, "sclk");
  330. if (IS_ERR(info->sclk)) {
  331. err = PTR_ERR(info->sclk);
  332. goto fail_put_mclk;
  333. }
  334. info->lrclk = clk_get(&pdev->dev, "lrclk");
  335. if (IS_ERR(info->lrclk)) {
  336. err = PTR_ERR(info->lrclk);
  337. goto fail_put_sclk;
  338. }
  339. dev_set_drvdata(&pdev->dev, info);
  340. err = snd_soc_register_component(&pdev->dev, &ep93xx_i2s_component,
  341. &ep93xx_i2s_dai, 1);
  342. if (err)
  343. goto fail_put_lrclk;
  344. err = devm_ep93xx_pcm_platform_register(&pdev->dev);
  345. if (err)
  346. goto fail_unregister;
  347. return 0;
  348. fail_unregister:
  349. snd_soc_unregister_component(&pdev->dev);
  350. fail_put_lrclk:
  351. clk_put(info->lrclk);
  352. fail_put_sclk:
  353. clk_put(info->sclk);
  354. fail_put_mclk:
  355. clk_put(info->mclk);
  356. fail:
  357. return err;
  358. }
  359. static int ep93xx_i2s_remove(struct platform_device *pdev)
  360. {
  361. struct ep93xx_i2s_info *info = dev_get_drvdata(&pdev->dev);
  362. snd_soc_unregister_component(&pdev->dev);
  363. clk_put(info->lrclk);
  364. clk_put(info->sclk);
  365. clk_put(info->mclk);
  366. return 0;
  367. }
  368. static struct platform_driver ep93xx_i2s_driver = {
  369. .probe = ep93xx_i2s_probe,
  370. .remove = ep93xx_i2s_remove,
  371. .driver = {
  372. .name = "ep93xx-i2s",
  373. },
  374. };
  375. module_platform_driver(ep93xx_i2s_driver);
  376. MODULE_ALIAS("platform:ep93xx-i2s");
  377. MODULE_AUTHOR("Ryan Mallon");
  378. MODULE_DESCRIPTION("EP93XX I2S driver");
  379. MODULE_LICENSE("GPL");