tegra_i2s.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * tegra_i2s.c - Tegra I2S driver
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010 - NVIDIA, Inc.
  6. *
  7. * Based on code copyright/by:
  8. *
  9. * Copyright (c) 2009-2010, NVIDIA Corporation.
  10. * Scott Peterson <speterson@nvidia.com>
  11. *
  12. * Copyright (C) 2010 Google, Inc.
  13. * Iliyan Malchev <malchev@google.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  27. * 02110-1301 USA
  28. *
  29. */
  30. #include <linux/clk.h>
  31. #include <linux/module.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/device.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/slab.h>
  37. #include <linux/io.h>
  38. #include <mach/iomap.h>
  39. #include <sound/core.h>
  40. #include <sound/pcm.h>
  41. #include <sound/pcm_params.h>
  42. #include <sound/soc.h>
  43. #include "tegra_das.h"
  44. #include "tegra_i2s.h"
  45. #define DRV_NAME "tegra-i2s"
  46. static inline void tegra_i2s_write(struct tegra_i2s *i2s, u32 reg, u32 val)
  47. {
  48. __raw_writel(val, i2s->regs + reg);
  49. }
  50. static inline u32 tegra_i2s_read(struct tegra_i2s *i2s, u32 reg)
  51. {
  52. return __raw_readl(i2s->regs + reg);
  53. }
  54. #ifdef CONFIG_DEBUG_FS
  55. static int tegra_i2s_show(struct seq_file *s, void *unused)
  56. {
  57. #define REG(r) { r, #r }
  58. static const struct {
  59. int offset;
  60. const char *name;
  61. } regs[] = {
  62. REG(TEGRA_I2S_CTRL),
  63. REG(TEGRA_I2S_STATUS),
  64. REG(TEGRA_I2S_TIMING),
  65. REG(TEGRA_I2S_FIFO_SCR),
  66. REG(TEGRA_I2S_PCM_CTRL),
  67. REG(TEGRA_I2S_NW_CTRL),
  68. REG(TEGRA_I2S_TDM_CTRL),
  69. REG(TEGRA_I2S_TDM_TX_RX_CTRL),
  70. };
  71. #undef REG
  72. struct tegra_i2s *i2s = s->private;
  73. int i;
  74. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  75. u32 val = tegra_i2s_read(i2s, regs[i].offset);
  76. seq_printf(s, "%s = %08x\n", regs[i].name, val);
  77. }
  78. return 0;
  79. }
  80. static int tegra_i2s_debug_open(struct inode *inode, struct file *file)
  81. {
  82. return single_open(file, tegra_i2s_show, inode->i_private);
  83. }
  84. static const struct file_operations tegra_i2s_debug_fops = {
  85. .open = tegra_i2s_debug_open,
  86. .read = seq_read,
  87. .llseek = seq_lseek,
  88. .release = single_release,
  89. };
  90. static void tegra_i2s_debug_add(struct tegra_i2s *i2s, int id)
  91. {
  92. char name[] = DRV_NAME ".0";
  93. snprintf(name, sizeof(name), DRV_NAME".%1d", id);
  94. i2s->debug = debugfs_create_file(name, S_IRUGO, snd_soc_debugfs_root,
  95. i2s, &tegra_i2s_debug_fops);
  96. }
  97. static void tegra_i2s_debug_remove(struct tegra_i2s *i2s)
  98. {
  99. if (i2s->debug)
  100. debugfs_remove(i2s->debug);
  101. }
  102. #else
  103. static inline void tegra_i2s_debug_add(struct tegra_i2s *i2s, int id)
  104. {
  105. }
  106. static inline void tegra_i2s_debug_remove(struct tegra_i2s *i2s)
  107. {
  108. }
  109. #endif
  110. static int tegra_i2s_set_fmt(struct snd_soc_dai *dai,
  111. unsigned int fmt)
  112. {
  113. struct tegra_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  114. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  115. case SND_SOC_DAIFMT_NB_NF:
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. i2s->reg_ctrl &= ~TEGRA_I2S_CTRL_MASTER_ENABLE;
  121. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  122. case SND_SOC_DAIFMT_CBS_CFS:
  123. i2s->reg_ctrl |= TEGRA_I2S_CTRL_MASTER_ENABLE;
  124. break;
  125. case SND_SOC_DAIFMT_CBM_CFM:
  126. break;
  127. default:
  128. return -EINVAL;
  129. }
  130. i2s->reg_ctrl &= ~(TEGRA_I2S_CTRL_BIT_FORMAT_MASK |
  131. TEGRA_I2S_CTRL_LRCK_MASK);
  132. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  133. case SND_SOC_DAIFMT_DSP_A:
  134. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_DSP;
  135. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_L_LOW;
  136. break;
  137. case SND_SOC_DAIFMT_DSP_B:
  138. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_DSP;
  139. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_R_LOW;
  140. break;
  141. case SND_SOC_DAIFMT_I2S:
  142. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_I2S;
  143. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_L_LOW;
  144. break;
  145. case SND_SOC_DAIFMT_RIGHT_J:
  146. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_RJM;
  147. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_L_LOW;
  148. break;
  149. case SND_SOC_DAIFMT_LEFT_J:
  150. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_LJM;
  151. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_L_LOW;
  152. break;
  153. default:
  154. return -EINVAL;
  155. }
  156. return 0;
  157. }
  158. static int tegra_i2s_hw_params(struct snd_pcm_substream *substream,
  159. struct snd_pcm_hw_params *params,
  160. struct snd_soc_dai *dai)
  161. {
  162. struct device *dev = substream->pcm->card->dev;
  163. struct tegra_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  164. u32 reg;
  165. int ret, sample_size, srate, i2sclock, bitcnt;
  166. i2s->reg_ctrl &= ~TEGRA_I2S_CTRL_BIT_SIZE_MASK;
  167. switch (params_format(params)) {
  168. case SNDRV_PCM_FORMAT_S16_LE:
  169. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_SIZE_16;
  170. sample_size = 16;
  171. break;
  172. case SNDRV_PCM_FORMAT_S24_LE:
  173. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_SIZE_24;
  174. sample_size = 24;
  175. break;
  176. case SNDRV_PCM_FORMAT_S32_LE:
  177. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_SIZE_32;
  178. sample_size = 32;
  179. break;
  180. default:
  181. return -EINVAL;
  182. }
  183. srate = params_rate(params);
  184. /* Final "* 2" required by Tegra hardware */
  185. i2sclock = srate * params_channels(params) * sample_size * 2;
  186. ret = clk_set_rate(i2s->clk_i2s, i2sclock);
  187. if (ret) {
  188. dev_err(dev, "Can't set I2S clock rate: %d\n", ret);
  189. return ret;
  190. }
  191. bitcnt = (i2sclock / (2 * srate)) - 1;
  192. if (bitcnt < 0 || bitcnt > TEGRA_I2S_TIMING_CHANNEL_BIT_COUNT_MASK_US)
  193. return -EINVAL;
  194. reg = bitcnt << TEGRA_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
  195. if (i2sclock % (2 * srate))
  196. reg |= TEGRA_I2S_TIMING_NON_SYM_ENABLE;
  197. if (!i2s->clk_refs)
  198. clk_enable(i2s->clk_i2s);
  199. tegra_i2s_write(i2s, TEGRA_I2S_TIMING, reg);
  200. tegra_i2s_write(i2s, TEGRA_I2S_FIFO_SCR,
  201. TEGRA_I2S_FIFO_SCR_FIFO2_ATN_LVL_FOUR_SLOTS |
  202. TEGRA_I2S_FIFO_SCR_FIFO1_ATN_LVL_FOUR_SLOTS);
  203. if (!i2s->clk_refs)
  204. clk_disable(i2s->clk_i2s);
  205. return 0;
  206. }
  207. static void tegra_i2s_start_playback(struct tegra_i2s *i2s)
  208. {
  209. i2s->reg_ctrl |= TEGRA_I2S_CTRL_FIFO1_ENABLE;
  210. tegra_i2s_write(i2s, TEGRA_I2S_CTRL, i2s->reg_ctrl);
  211. }
  212. static void tegra_i2s_stop_playback(struct tegra_i2s *i2s)
  213. {
  214. i2s->reg_ctrl &= ~TEGRA_I2S_CTRL_FIFO1_ENABLE;
  215. tegra_i2s_write(i2s, TEGRA_I2S_CTRL, i2s->reg_ctrl);
  216. }
  217. static void tegra_i2s_start_capture(struct tegra_i2s *i2s)
  218. {
  219. i2s->reg_ctrl |= TEGRA_I2S_CTRL_FIFO2_ENABLE;
  220. tegra_i2s_write(i2s, TEGRA_I2S_CTRL, i2s->reg_ctrl);
  221. }
  222. static void tegra_i2s_stop_capture(struct tegra_i2s *i2s)
  223. {
  224. i2s->reg_ctrl &= ~TEGRA_I2S_CTRL_FIFO2_ENABLE;
  225. tegra_i2s_write(i2s, TEGRA_I2S_CTRL, i2s->reg_ctrl);
  226. }
  227. static int tegra_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  228. struct snd_soc_dai *dai)
  229. {
  230. struct tegra_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  231. switch (cmd) {
  232. case SNDRV_PCM_TRIGGER_START:
  233. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  234. case SNDRV_PCM_TRIGGER_RESUME:
  235. if (!i2s->clk_refs)
  236. clk_enable(i2s->clk_i2s);
  237. i2s->clk_refs++;
  238. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  239. tegra_i2s_start_playback(i2s);
  240. else
  241. tegra_i2s_start_capture(i2s);
  242. break;
  243. case SNDRV_PCM_TRIGGER_STOP:
  244. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  245. case SNDRV_PCM_TRIGGER_SUSPEND:
  246. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  247. tegra_i2s_stop_playback(i2s);
  248. else
  249. tegra_i2s_stop_capture(i2s);
  250. i2s->clk_refs--;
  251. if (!i2s->clk_refs)
  252. clk_disable(i2s->clk_i2s);
  253. break;
  254. default:
  255. return -EINVAL;
  256. }
  257. return 0;
  258. }
  259. static int tegra_i2s_probe(struct snd_soc_dai *dai)
  260. {
  261. struct tegra_i2s * i2s = snd_soc_dai_get_drvdata(dai);
  262. dai->capture_dma_data = &i2s->capture_dma_data;
  263. dai->playback_dma_data = &i2s->playback_dma_data;
  264. return 0;
  265. }
  266. static struct snd_soc_dai_ops tegra_i2s_dai_ops = {
  267. .set_fmt = tegra_i2s_set_fmt,
  268. .hw_params = tegra_i2s_hw_params,
  269. .trigger = tegra_i2s_trigger,
  270. };
  271. struct snd_soc_dai_driver tegra_i2s_dai[] = {
  272. {
  273. .name = DRV_NAME ".0",
  274. .probe = tegra_i2s_probe,
  275. .playback = {
  276. .channels_min = 2,
  277. .channels_max = 2,
  278. .rates = SNDRV_PCM_RATE_8000_96000,
  279. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  280. },
  281. .capture = {
  282. .channels_min = 2,
  283. .channels_max = 2,
  284. .rates = SNDRV_PCM_RATE_8000_96000,
  285. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  286. },
  287. .ops = &tegra_i2s_dai_ops,
  288. .symmetric_rates = 1,
  289. },
  290. {
  291. .name = DRV_NAME ".1",
  292. .probe = tegra_i2s_probe,
  293. .playback = {
  294. .channels_min = 2,
  295. .channels_max = 2,
  296. .rates = SNDRV_PCM_RATE_8000_96000,
  297. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  298. },
  299. .capture = {
  300. .channels_min = 2,
  301. .channels_max = 2,
  302. .rates = SNDRV_PCM_RATE_8000_96000,
  303. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  304. },
  305. .ops = &tegra_i2s_dai_ops,
  306. .symmetric_rates = 1,
  307. },
  308. };
  309. static __devinit int tegra_i2s_platform_probe(struct platform_device *pdev)
  310. {
  311. struct tegra_i2s * i2s;
  312. char clk_name[12]; /* tegra-i2s.0 */
  313. struct resource *mem, *memregion, *dmareq;
  314. int ret;
  315. if ((pdev->id < 0) ||
  316. (pdev->id >= ARRAY_SIZE(tegra_i2s_dai))) {
  317. dev_err(&pdev->dev, "ID %d out of range\n", pdev->id);
  318. return -EINVAL;
  319. }
  320. /*
  321. * FIXME: Until a codec driver exists for the tegra DAS, hard-code a
  322. * 1:1 mapping between audio controllers and audio ports.
  323. */
  324. ret = tegra_das_connect_dap_to_dac(TEGRA_DAS_DAP_ID_1 + pdev->id,
  325. TEGRA_DAS_DAP_SEL_DAC1 + pdev->id);
  326. if (ret) {
  327. dev_err(&pdev->dev, "Can't set up DAP connection\n");
  328. return ret;
  329. }
  330. ret = tegra_das_connect_dac_to_dap(TEGRA_DAS_DAC_ID_1 + pdev->id,
  331. TEGRA_DAS_DAC_SEL_DAP1 + pdev->id);
  332. if (ret) {
  333. dev_err(&pdev->dev, "Can't set up DAC connection\n");
  334. return ret;
  335. }
  336. i2s = kzalloc(sizeof(struct tegra_i2s), GFP_KERNEL);
  337. if (!i2s) {
  338. dev_err(&pdev->dev, "Can't allocate tegra_i2s\n");
  339. ret = -ENOMEM;
  340. goto exit;
  341. }
  342. dev_set_drvdata(&pdev->dev, i2s);
  343. snprintf(clk_name, sizeof(clk_name), DRV_NAME ".%d", pdev->id);
  344. i2s->clk_i2s = clk_get_sys(clk_name, NULL);
  345. if (IS_ERR(i2s->clk_i2s)) {
  346. dev_err(&pdev->dev, "Can't retrieve i2s clock\n");
  347. ret = PTR_ERR(i2s->clk_i2s);
  348. goto err_free;
  349. }
  350. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  351. if (!mem) {
  352. dev_err(&pdev->dev, "No memory resource\n");
  353. ret = -ENODEV;
  354. goto err_clk_put;
  355. }
  356. dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  357. if (!dmareq) {
  358. dev_err(&pdev->dev, "No DMA resource\n");
  359. ret = -ENODEV;
  360. goto err_clk_put;
  361. }
  362. memregion = request_mem_region(mem->start, resource_size(mem),
  363. DRV_NAME);
  364. if (!memregion) {
  365. dev_err(&pdev->dev, "Memory region already claimed\n");
  366. ret = -EBUSY;
  367. goto err_clk_put;
  368. }
  369. i2s->regs = ioremap(mem->start, resource_size(mem));
  370. if (!i2s->regs) {
  371. dev_err(&pdev->dev, "ioremap failed\n");
  372. ret = -ENOMEM;
  373. goto err_release;
  374. }
  375. i2s->capture_dma_data.addr = mem->start + TEGRA_I2S_FIFO2;
  376. i2s->capture_dma_data.wrap = 4;
  377. i2s->capture_dma_data.width = 32;
  378. i2s->capture_dma_data.req_sel = dmareq->start;
  379. i2s->playback_dma_data.addr = mem->start + TEGRA_I2S_FIFO1;
  380. i2s->playback_dma_data.wrap = 4;
  381. i2s->playback_dma_data.width = 32;
  382. i2s->playback_dma_data.req_sel = dmareq->start;
  383. i2s->reg_ctrl = TEGRA_I2S_CTRL_FIFO_FORMAT_PACKED;
  384. ret = snd_soc_register_dai(&pdev->dev, &tegra_i2s_dai[pdev->id]);
  385. if (ret) {
  386. dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  387. ret = -ENOMEM;
  388. goto err_unmap;
  389. }
  390. tegra_i2s_debug_add(i2s, pdev->id);
  391. return 0;
  392. err_unmap:
  393. iounmap(i2s->regs);
  394. err_release:
  395. release_mem_region(mem->start, resource_size(mem));
  396. err_clk_put:
  397. clk_put(i2s->clk_i2s);
  398. err_free:
  399. kfree(i2s);
  400. exit:
  401. return ret;
  402. }
  403. static int __devexit tegra_i2s_platform_remove(struct platform_device *pdev)
  404. {
  405. struct tegra_i2s *i2s = dev_get_drvdata(&pdev->dev);
  406. struct resource *res;
  407. snd_soc_unregister_dai(&pdev->dev);
  408. tegra_i2s_debug_remove(i2s);
  409. iounmap(i2s->regs);
  410. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  411. release_mem_region(res->start, resource_size(res));
  412. clk_put(i2s->clk_i2s);
  413. kfree(i2s);
  414. return 0;
  415. }
  416. static struct platform_driver tegra_i2s_driver = {
  417. .driver = {
  418. .name = DRV_NAME,
  419. .owner = THIS_MODULE,
  420. },
  421. .probe = tegra_i2s_platform_probe,
  422. .remove = __devexit_p(tegra_i2s_platform_remove),
  423. };
  424. static int __init snd_tegra_i2s_init(void)
  425. {
  426. return platform_driver_register(&tegra_i2s_driver);
  427. }
  428. module_init(snd_tegra_i2s_init);
  429. static void __exit snd_tegra_i2s_exit(void)
  430. {
  431. platform_driver_unregister(&tegra_i2s_driver);
  432. }
  433. module_exit(snd_tegra_i2s_exit);
  434. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  435. MODULE_DESCRIPTION("Tegra I2S ASoC driver");
  436. MODULE_LICENSE("GPL");
  437. MODULE_ALIAS("platform:" DRV_NAME);