kirkwood-i2s.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * kirkwood-i2s.c
  3. *
  4. * (c) 2010 Arnaud Patard <apatard@mandriva.com>
  5. * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/mbus.h>
  18. #include <linux/delay.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <plat/audio.h>
  23. #include "kirkwood.h"
  24. #define DRV_NAME "kirkwood-i2s"
  25. #define KIRKWOOD_I2S_RATES \
  26. (SNDRV_PCM_RATE_44100 | \
  27. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
  28. #define KIRKWOOD_I2S_FORMATS \
  29. (SNDRV_PCM_FMTBIT_S16_LE | \
  30. SNDRV_PCM_FMTBIT_S24_LE | \
  31. SNDRV_PCM_FMTBIT_S32_LE)
  32. static int kirkwood_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
  33. unsigned int fmt)
  34. {
  35. struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(cpu_dai);
  36. unsigned long mask;
  37. unsigned long value;
  38. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  39. case SND_SOC_DAIFMT_RIGHT_J:
  40. mask = KIRKWOOD_I2S_CTL_RJ;
  41. break;
  42. case SND_SOC_DAIFMT_LEFT_J:
  43. mask = KIRKWOOD_I2S_CTL_LJ;
  44. break;
  45. case SND_SOC_DAIFMT_I2S:
  46. mask = KIRKWOOD_I2S_CTL_I2S;
  47. break;
  48. default:
  49. return -EINVAL;
  50. }
  51. /*
  52. * Set same format for playback and record
  53. * This avoids some troubles.
  54. */
  55. value = readl(priv->io+KIRKWOOD_I2S_PLAYCTL);
  56. value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
  57. value |= mask;
  58. writel(value, priv->io+KIRKWOOD_I2S_PLAYCTL);
  59. value = readl(priv->io+KIRKWOOD_I2S_RECCTL);
  60. value &= ~KIRKWOOD_I2S_CTL_JUST_MASK;
  61. value |= mask;
  62. writel(value, priv->io+KIRKWOOD_I2S_RECCTL);
  63. return 0;
  64. }
  65. static inline void kirkwood_set_dco(void __iomem *io, unsigned long rate)
  66. {
  67. unsigned long value;
  68. value = KIRKWOOD_DCO_CTL_OFFSET_0;
  69. switch (rate) {
  70. default:
  71. case 44100:
  72. value |= KIRKWOOD_DCO_CTL_FREQ_11;
  73. break;
  74. case 48000:
  75. value |= KIRKWOOD_DCO_CTL_FREQ_12;
  76. break;
  77. case 96000:
  78. value |= KIRKWOOD_DCO_CTL_FREQ_24;
  79. break;
  80. }
  81. writel(value, io + KIRKWOOD_DCO_CTL);
  82. /* wait for dco locked */
  83. do {
  84. cpu_relax();
  85. value = readl(io + KIRKWOOD_DCO_SPCR_STATUS);
  86. value &= KIRKWOOD_DCO_SPCR_STATUS;
  87. } while (value == 0);
  88. }
  89. static int kirkwood_i2s_startup(struct snd_pcm_substream *substream,
  90. struct snd_soc_dai *dai)
  91. {
  92. struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
  93. snd_soc_dai_set_dma_data(dai, substream, priv);
  94. return 0;
  95. }
  96. static int kirkwood_i2s_hw_params(struct snd_pcm_substream *substream,
  97. struct snd_pcm_hw_params *params,
  98. struct snd_soc_dai *dai)
  99. {
  100. struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
  101. unsigned int i2s_reg, reg;
  102. unsigned long i2s_value, value;
  103. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  104. i2s_reg = KIRKWOOD_I2S_PLAYCTL;
  105. reg = KIRKWOOD_PLAYCTL;
  106. } else {
  107. i2s_reg = KIRKWOOD_I2S_RECCTL;
  108. reg = KIRKWOOD_RECCTL;
  109. }
  110. /* set dco conf */
  111. kirkwood_set_dco(priv->io, params_rate(params));
  112. i2s_value = readl(priv->io+i2s_reg);
  113. i2s_value &= ~KIRKWOOD_I2S_CTL_SIZE_MASK;
  114. value = readl(priv->io+reg);
  115. value &= ~KIRKWOOD_PLAYCTL_SIZE_MASK;
  116. /*
  117. * Size settings in play/rec i2s control regs and play/rec control
  118. * regs must be the same.
  119. */
  120. switch (params_format(params)) {
  121. case SNDRV_PCM_FORMAT_S16_LE:
  122. i2s_value |= KIRKWOOD_I2S_CTL_SIZE_16;
  123. value |= KIRKWOOD_PLAYCTL_SIZE_16_C;
  124. break;
  125. /*
  126. * doesn't work... S20_3LE != kirkwood 20bit format ?
  127. *
  128. case SNDRV_PCM_FORMAT_S20_3LE:
  129. i2s_value |= KIRKWOOD_I2S_CTL_SIZE_20;
  130. value |= KIRKWOOD_PLAYCTL_SIZE_20;
  131. break;
  132. */
  133. case SNDRV_PCM_FORMAT_S24_LE:
  134. i2s_value |= KIRKWOOD_I2S_CTL_SIZE_24;
  135. value |= KIRKWOOD_PLAYCTL_SIZE_24;
  136. break;
  137. case SNDRV_PCM_FORMAT_S32_LE:
  138. i2s_value |= KIRKWOOD_I2S_CTL_SIZE_32;
  139. value |= KIRKWOOD_PLAYCTL_SIZE_32;
  140. break;
  141. default:
  142. return -EINVAL;
  143. }
  144. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  145. value &= ~KIRKWOOD_PLAYCTL_MONO_MASK;
  146. if (params_channels(params) == 1)
  147. value |= KIRKWOOD_PLAYCTL_MONO_BOTH;
  148. else
  149. value |= KIRKWOOD_PLAYCTL_MONO_OFF;
  150. }
  151. writel(i2s_value, priv->io+i2s_reg);
  152. writel(value, priv->io+reg);
  153. return 0;
  154. }
  155. static int kirkwood_i2s_play_trigger(struct snd_pcm_substream *substream,
  156. int cmd, struct snd_soc_dai *dai)
  157. {
  158. struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
  159. unsigned long value;
  160. /*
  161. * specs says KIRKWOOD_PLAYCTL must be read 2 times before
  162. * changing it. So read 1 time here and 1 later.
  163. */
  164. value = readl(priv->io + KIRKWOOD_PLAYCTL);
  165. switch (cmd) {
  166. case SNDRV_PCM_TRIGGER_START:
  167. /* stop audio, enable interrupts */
  168. value = readl(priv->io + KIRKWOOD_PLAYCTL);
  169. value |= KIRKWOOD_PLAYCTL_PAUSE;
  170. writel(value, priv->io + KIRKWOOD_PLAYCTL);
  171. value = readl(priv->io + KIRKWOOD_INT_MASK);
  172. value |= KIRKWOOD_INT_CAUSE_PLAY_BYTES;
  173. writel(value, priv->io + KIRKWOOD_INT_MASK);
  174. /* configure audio & enable i2s playback */
  175. value = readl(priv->io + KIRKWOOD_PLAYCTL);
  176. value &= ~KIRKWOOD_PLAYCTL_BURST_MASK;
  177. value &= ~(KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE
  178. | KIRKWOOD_PLAYCTL_SPDIF_EN);
  179. if (priv->burst == 32)
  180. value |= KIRKWOOD_PLAYCTL_BURST_32;
  181. else
  182. value |= KIRKWOOD_PLAYCTL_BURST_128;
  183. value |= KIRKWOOD_PLAYCTL_I2S_EN;
  184. writel(value, priv->io + KIRKWOOD_PLAYCTL);
  185. break;
  186. case SNDRV_PCM_TRIGGER_STOP:
  187. /* stop audio, disable interrupts */
  188. value = readl(priv->io + KIRKWOOD_PLAYCTL);
  189. value |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE;
  190. writel(value, priv->io + KIRKWOOD_PLAYCTL);
  191. value = readl(priv->io + KIRKWOOD_INT_MASK);
  192. value &= ~KIRKWOOD_INT_CAUSE_PLAY_BYTES;
  193. writel(value, priv->io + KIRKWOOD_INT_MASK);
  194. /* disable all playbacks */
  195. value = readl(priv->io + KIRKWOOD_PLAYCTL);
  196. value &= ~(KIRKWOOD_PLAYCTL_I2S_EN | KIRKWOOD_PLAYCTL_SPDIF_EN);
  197. writel(value, priv->io + KIRKWOOD_PLAYCTL);
  198. break;
  199. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  200. case SNDRV_PCM_TRIGGER_SUSPEND:
  201. value = readl(priv->io + KIRKWOOD_PLAYCTL);
  202. value |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE;
  203. writel(value, priv->io + KIRKWOOD_PLAYCTL);
  204. break;
  205. case SNDRV_PCM_TRIGGER_RESUME:
  206. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  207. value = readl(priv->io + KIRKWOOD_PLAYCTL);
  208. value &= ~(KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE);
  209. writel(value, priv->io + KIRKWOOD_PLAYCTL);
  210. break;
  211. default:
  212. return -EINVAL;
  213. }
  214. return 0;
  215. }
  216. static int kirkwood_i2s_rec_trigger(struct snd_pcm_substream *substream,
  217. int cmd, struct snd_soc_dai *dai)
  218. {
  219. struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
  220. unsigned long value;
  221. value = readl(priv->io + KIRKWOOD_RECCTL);
  222. switch (cmd) {
  223. case SNDRV_PCM_TRIGGER_START:
  224. /* stop audio, enable interrupts */
  225. value = readl(priv->io + KIRKWOOD_RECCTL);
  226. value |= KIRKWOOD_RECCTL_PAUSE;
  227. writel(value, priv->io + KIRKWOOD_RECCTL);
  228. value = readl(priv->io + KIRKWOOD_INT_MASK);
  229. value |= KIRKWOOD_INT_CAUSE_REC_BYTES;
  230. writel(value, priv->io + KIRKWOOD_INT_MASK);
  231. /* configure audio & enable i2s record */
  232. value = readl(priv->io + KIRKWOOD_RECCTL);
  233. value &= ~KIRKWOOD_RECCTL_BURST_MASK;
  234. value &= ~KIRKWOOD_RECCTL_MONO;
  235. value &= ~(KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE
  236. | KIRKWOOD_RECCTL_SPDIF_EN);
  237. if (priv->burst == 32)
  238. value |= KIRKWOOD_RECCTL_BURST_32;
  239. else
  240. value |= KIRKWOOD_RECCTL_BURST_128;
  241. value |= KIRKWOOD_RECCTL_I2S_EN;
  242. writel(value, priv->io + KIRKWOOD_RECCTL);
  243. break;
  244. case SNDRV_PCM_TRIGGER_STOP:
  245. /* stop audio, disable interrupts */
  246. value = readl(priv->io + KIRKWOOD_RECCTL);
  247. value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
  248. writel(value, priv->io + KIRKWOOD_RECCTL);
  249. value = readl(priv->io + KIRKWOOD_INT_MASK);
  250. value &= ~KIRKWOOD_INT_CAUSE_REC_BYTES;
  251. writel(value, priv->io + KIRKWOOD_INT_MASK);
  252. /* disable all records */
  253. value = readl(priv->io + KIRKWOOD_RECCTL);
  254. value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN);
  255. writel(value, priv->io + KIRKWOOD_RECCTL);
  256. break;
  257. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  258. case SNDRV_PCM_TRIGGER_SUSPEND:
  259. value = readl(priv->io + KIRKWOOD_RECCTL);
  260. value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE;
  261. writel(value, priv->io + KIRKWOOD_RECCTL);
  262. break;
  263. case SNDRV_PCM_TRIGGER_RESUME:
  264. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  265. value = readl(priv->io + KIRKWOOD_RECCTL);
  266. value &= ~(KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE);
  267. writel(value, priv->io + KIRKWOOD_RECCTL);
  268. break;
  269. default:
  270. return -EINVAL;
  271. }
  272. return 0;
  273. }
  274. static int kirkwood_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  275. struct snd_soc_dai *dai)
  276. {
  277. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  278. return kirkwood_i2s_play_trigger(substream, cmd, dai);
  279. else
  280. return kirkwood_i2s_rec_trigger(substream, cmd, dai);
  281. return 0;
  282. }
  283. static int kirkwood_i2s_probe(struct snd_soc_dai *dai)
  284. {
  285. struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai);
  286. unsigned long value;
  287. unsigned int reg_data;
  288. /* put system in a "safe" state : */
  289. /* disable audio interrupts */
  290. writel(0xffffffff, priv->io + KIRKWOOD_INT_CAUSE);
  291. writel(0, priv->io + KIRKWOOD_INT_MASK);
  292. reg_data = readl(priv->io + 0x1200);
  293. reg_data &= (~(0x333FF8));
  294. reg_data |= 0x111D18;
  295. writel(reg_data, priv->io + 0x1200);
  296. msleep(500);
  297. reg_data = readl(priv->io + 0x1200);
  298. reg_data &= (~(0x333FF8));
  299. reg_data |= 0x111D18;
  300. writel(reg_data, priv->io + 0x1200);
  301. /* disable playback/record */
  302. value = readl(priv->io + KIRKWOOD_PLAYCTL);
  303. value &= ~(KIRKWOOD_PLAYCTL_I2S_EN|KIRKWOOD_PLAYCTL_SPDIF_EN);
  304. writel(value, priv->io + KIRKWOOD_PLAYCTL);
  305. value = readl(priv->io + KIRKWOOD_RECCTL);
  306. value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN);
  307. writel(value, priv->io + KIRKWOOD_RECCTL);
  308. return 0;
  309. }
  310. static int kirkwood_i2s_remove(struct snd_soc_dai *dai)
  311. {
  312. return 0;
  313. }
  314. static struct snd_soc_dai_ops kirkwood_i2s_dai_ops = {
  315. .startup = kirkwood_i2s_startup,
  316. .trigger = kirkwood_i2s_trigger,
  317. .hw_params = kirkwood_i2s_hw_params,
  318. .set_fmt = kirkwood_i2s_set_fmt,
  319. };
  320. static struct snd_soc_dai_driver kirkwood_i2s_dai = {
  321. .probe = kirkwood_i2s_probe,
  322. .remove = kirkwood_i2s_remove,
  323. .playback = {
  324. .channels_min = 1,
  325. .channels_max = 2,
  326. .rates = KIRKWOOD_I2S_RATES,
  327. .formats = KIRKWOOD_I2S_FORMATS,},
  328. .capture = {
  329. .channels_min = 1,
  330. .channels_max = 2,
  331. .rates = KIRKWOOD_I2S_RATES,
  332. .formats = KIRKWOOD_I2S_FORMATS,},
  333. .ops = &kirkwood_i2s_dai_ops,
  334. };
  335. static __devinit int kirkwood_i2s_dev_probe(struct platform_device *pdev)
  336. {
  337. struct resource *mem;
  338. struct kirkwood_asoc_platform_data *data =
  339. pdev->dev.platform_data;
  340. struct kirkwood_dma_data *priv;
  341. int err;
  342. priv = kzalloc(sizeof(struct kirkwood_dma_data), GFP_KERNEL);
  343. if (!priv) {
  344. dev_err(&pdev->dev, "allocation failed\n");
  345. err = -ENOMEM;
  346. goto error;
  347. }
  348. dev_set_drvdata(&pdev->dev, priv);
  349. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  350. if (!mem) {
  351. dev_err(&pdev->dev, "platform_get_resource failed\n");
  352. err = -ENXIO;
  353. goto err_alloc;
  354. }
  355. priv->mem = request_mem_region(mem->start, SZ_16K, DRV_NAME);
  356. if (!priv->mem) {
  357. dev_err(&pdev->dev, "request_mem_region failed\n");
  358. err = -EBUSY;
  359. goto error;
  360. }
  361. priv->io = ioremap(priv->mem->start, SZ_16K);
  362. if (!priv->io) {
  363. dev_err(&pdev->dev, "ioremap failed\n");
  364. err = -ENOMEM;
  365. goto err_iomem;
  366. }
  367. priv->irq = platform_get_irq(pdev, 0);
  368. if (priv->irq <= 0) {
  369. dev_err(&pdev->dev, "platform_get_irq failed\n");
  370. err = -ENXIO;
  371. goto err_ioremap;
  372. }
  373. if (!data || !data->dram) {
  374. dev_err(&pdev->dev, "no platform data ?!\n");
  375. err = -EINVAL;
  376. goto err_ioremap;
  377. }
  378. priv->dram = data->dram;
  379. priv->burst = data->burst;
  380. return snd_soc_register_dai(&pdev->dev, &kirkwood_i2s_dai);
  381. err_ioremap:
  382. iounmap(priv->io);
  383. err_iomem:
  384. release_mem_region(priv->mem->start, SZ_16K);
  385. err_alloc:
  386. kfree(priv);
  387. error:
  388. return err;
  389. }
  390. static __devexit int kirkwood_i2s_dev_remove(struct platform_device *pdev)
  391. {
  392. struct kirkwood_dma_data *priv = dev_get_drvdata(&pdev->dev);
  393. snd_soc_unregister_dai(&pdev->dev);
  394. iounmap(priv->io);
  395. release_mem_region(priv->mem->start, SZ_16K);
  396. kfree(priv);
  397. return 0;
  398. }
  399. static struct platform_driver kirkwood_i2s_driver = {
  400. .probe = kirkwood_i2s_dev_probe,
  401. .remove = kirkwood_i2s_dev_remove,
  402. .driver = {
  403. .name = DRV_NAME,
  404. .owner = THIS_MODULE,
  405. },
  406. };
  407. static int __init kirkwood_i2s_init(void)
  408. {
  409. return platform_driver_register(&kirkwood_i2s_driver);
  410. }
  411. module_init(kirkwood_i2s_init);
  412. static void __exit kirkwood_i2s_exit(void)
  413. {
  414. platform_driver_unregister(&kirkwood_i2s_driver);
  415. }
  416. module_exit(kirkwood_i2s_exit);
  417. /* Module information */
  418. MODULE_AUTHOR("Arnaud Patard, <arnaud.patard@rtp-net.org>");
  419. MODULE_DESCRIPTION("Kirkwood I2S SoC Interface");
  420. MODULE_LICENSE("GPL");
  421. MODULE_ALIAS("platform:kirkwood-i2s");