ep93xx-i2s.c 11 KB

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