tegra20_i2s.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * tegra20_i2s.c - Tegra20 I2S driver
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010,2012 - 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/debugfs.h>
  32. #include <linux/device.h>
  33. #include <linux/io.h>
  34. #include <linux/module.h>
  35. #include <linux/of.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/slab.h>
  40. #include <sound/core.h>
  41. #include <sound/pcm.h>
  42. #include <sound/pcm_params.h>
  43. #include <sound/soc.h>
  44. #include "tegra20_i2s.h"
  45. #define DRV_NAME "tegra20-i2s"
  46. static inline void tegra20_i2s_write(struct tegra20_i2s *i2s, u32 reg, u32 val)
  47. {
  48. __raw_writel(val, i2s->regs + reg);
  49. }
  50. static inline u32 tegra20_i2s_read(struct tegra20_i2s *i2s, u32 reg)
  51. {
  52. return __raw_readl(i2s->regs + reg);
  53. }
  54. static int tegra20_i2s_runtime_suspend(struct device *dev)
  55. {
  56. struct tegra20_i2s *i2s = dev_get_drvdata(dev);
  57. clk_disable(i2s->clk_i2s);
  58. return 0;
  59. }
  60. static int tegra20_i2s_runtime_resume(struct device *dev)
  61. {
  62. struct tegra20_i2s *i2s = dev_get_drvdata(dev);
  63. int ret;
  64. ret = clk_enable(i2s->clk_i2s);
  65. if (ret) {
  66. dev_err(dev, "clk_enable failed: %d\n", ret);
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. #ifdef CONFIG_DEBUG_FS
  72. static int tegra20_i2s_show(struct seq_file *s, void *unused)
  73. {
  74. #define REG(r) { r, #r }
  75. static const struct {
  76. int offset;
  77. const char *name;
  78. } regs[] = {
  79. REG(TEGRA20_I2S_CTRL),
  80. REG(TEGRA20_I2S_STATUS),
  81. REG(TEGRA20_I2S_TIMING),
  82. REG(TEGRA20_I2S_FIFO_SCR),
  83. REG(TEGRA20_I2S_PCM_CTRL),
  84. REG(TEGRA20_I2S_NW_CTRL),
  85. REG(TEGRA20_I2S_TDM_CTRL),
  86. REG(TEGRA20_I2S_TDM_TX_RX_CTRL),
  87. };
  88. #undef REG
  89. struct tegra20_i2s *i2s = s->private;
  90. int i;
  91. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  92. u32 val = tegra20_i2s_read(i2s, regs[i].offset);
  93. seq_printf(s, "%s = %08x\n", regs[i].name, val);
  94. }
  95. return 0;
  96. }
  97. static int tegra20_i2s_debug_open(struct inode *inode, struct file *file)
  98. {
  99. return single_open(file, tegra20_i2s_show, inode->i_private);
  100. }
  101. static const struct file_operations tegra20_i2s_debug_fops = {
  102. .open = tegra20_i2s_debug_open,
  103. .read = seq_read,
  104. .llseek = seq_lseek,
  105. .release = single_release,
  106. };
  107. static void tegra20_i2s_debug_add(struct tegra20_i2s *i2s)
  108. {
  109. i2s->debug = debugfs_create_file(i2s->dai.name, S_IRUGO,
  110. snd_soc_debugfs_root, i2s,
  111. &tegra20_i2s_debug_fops);
  112. }
  113. static void tegra20_i2s_debug_remove(struct tegra20_i2s *i2s)
  114. {
  115. if (i2s->debug)
  116. debugfs_remove(i2s->debug);
  117. }
  118. #else
  119. static inline void tegra20_i2s_debug_add(struct tegra20_i2s *i2s, int id)
  120. {
  121. }
  122. static inline void tegra20_i2s_debug_remove(struct tegra20_i2s *i2s)
  123. {
  124. }
  125. #endif
  126. static int tegra20_i2s_set_fmt(struct snd_soc_dai *dai,
  127. unsigned int fmt)
  128. {
  129. struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  130. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  131. case SND_SOC_DAIFMT_NB_NF:
  132. break;
  133. default:
  134. return -EINVAL;
  135. }
  136. i2s->reg_ctrl &= ~TEGRA20_I2S_CTRL_MASTER_ENABLE;
  137. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  138. case SND_SOC_DAIFMT_CBS_CFS:
  139. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_MASTER_ENABLE;
  140. break;
  141. case SND_SOC_DAIFMT_CBM_CFM:
  142. break;
  143. default:
  144. return -EINVAL;
  145. }
  146. i2s->reg_ctrl &= ~(TEGRA20_I2S_CTRL_BIT_FORMAT_MASK |
  147. TEGRA20_I2S_CTRL_LRCK_MASK);
  148. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  149. case SND_SOC_DAIFMT_DSP_A:
  150. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_DSP;
  151. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_L_LOW;
  152. break;
  153. case SND_SOC_DAIFMT_DSP_B:
  154. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_DSP;
  155. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_R_LOW;
  156. break;
  157. case SND_SOC_DAIFMT_I2S:
  158. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_I2S;
  159. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_L_LOW;
  160. break;
  161. case SND_SOC_DAIFMT_RIGHT_J:
  162. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_RJM;
  163. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_L_LOW;
  164. break;
  165. case SND_SOC_DAIFMT_LEFT_J:
  166. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_LJM;
  167. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_L_LOW;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. return 0;
  173. }
  174. static int tegra20_i2s_hw_params(struct snd_pcm_substream *substream,
  175. struct snd_pcm_hw_params *params,
  176. struct snd_soc_dai *dai)
  177. {
  178. struct device *dev = substream->pcm->card->dev;
  179. struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  180. u32 reg;
  181. int ret, sample_size, srate, i2sclock, bitcnt;
  182. i2s->reg_ctrl &= ~TEGRA20_I2S_CTRL_BIT_SIZE_MASK;
  183. switch (params_format(params)) {
  184. case SNDRV_PCM_FORMAT_S16_LE:
  185. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_SIZE_16;
  186. sample_size = 16;
  187. break;
  188. case SNDRV_PCM_FORMAT_S24_LE:
  189. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_SIZE_24;
  190. sample_size = 24;
  191. break;
  192. case SNDRV_PCM_FORMAT_S32_LE:
  193. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_SIZE_32;
  194. sample_size = 32;
  195. break;
  196. default:
  197. return -EINVAL;
  198. }
  199. srate = params_rate(params);
  200. /* Final "* 2" required by Tegra hardware */
  201. i2sclock = srate * params_channels(params) * sample_size * 2;
  202. ret = clk_set_rate(i2s->clk_i2s, i2sclock);
  203. if (ret) {
  204. dev_err(dev, "Can't set I2S clock rate: %d\n", ret);
  205. return ret;
  206. }
  207. bitcnt = (i2sclock / (2 * srate)) - 1;
  208. if (bitcnt < 0 || bitcnt > TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_MASK_US)
  209. return -EINVAL;
  210. reg = bitcnt << TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
  211. if (i2sclock % (2 * srate))
  212. reg |= TEGRA20_I2S_TIMING_NON_SYM_ENABLE;
  213. tegra20_i2s_write(i2s, TEGRA20_I2S_TIMING, reg);
  214. tegra20_i2s_write(i2s, TEGRA20_I2S_FIFO_SCR,
  215. TEGRA20_I2S_FIFO_SCR_FIFO2_ATN_LVL_FOUR_SLOTS |
  216. TEGRA20_I2S_FIFO_SCR_FIFO1_ATN_LVL_FOUR_SLOTS);
  217. return 0;
  218. }
  219. static void tegra20_i2s_start_playback(struct tegra20_i2s *i2s)
  220. {
  221. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_FIFO1_ENABLE;
  222. tegra20_i2s_write(i2s, TEGRA20_I2S_CTRL, i2s->reg_ctrl);
  223. }
  224. static void tegra20_i2s_stop_playback(struct tegra20_i2s *i2s)
  225. {
  226. i2s->reg_ctrl &= ~TEGRA20_I2S_CTRL_FIFO1_ENABLE;
  227. tegra20_i2s_write(i2s, TEGRA20_I2S_CTRL, i2s->reg_ctrl);
  228. }
  229. static void tegra20_i2s_start_capture(struct tegra20_i2s *i2s)
  230. {
  231. i2s->reg_ctrl |= TEGRA20_I2S_CTRL_FIFO2_ENABLE;
  232. tegra20_i2s_write(i2s, TEGRA20_I2S_CTRL, i2s->reg_ctrl);
  233. }
  234. static void tegra20_i2s_stop_capture(struct tegra20_i2s *i2s)
  235. {
  236. i2s->reg_ctrl &= ~TEGRA20_I2S_CTRL_FIFO2_ENABLE;
  237. tegra20_i2s_write(i2s, TEGRA20_I2S_CTRL, i2s->reg_ctrl);
  238. }
  239. static int tegra20_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  240. struct snd_soc_dai *dai)
  241. {
  242. struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  243. switch (cmd) {
  244. case SNDRV_PCM_TRIGGER_START:
  245. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  246. case SNDRV_PCM_TRIGGER_RESUME:
  247. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  248. tegra20_i2s_start_playback(i2s);
  249. else
  250. tegra20_i2s_start_capture(i2s);
  251. break;
  252. case SNDRV_PCM_TRIGGER_STOP:
  253. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  254. case SNDRV_PCM_TRIGGER_SUSPEND:
  255. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  256. tegra20_i2s_stop_playback(i2s);
  257. else
  258. tegra20_i2s_stop_capture(i2s);
  259. break;
  260. default:
  261. return -EINVAL;
  262. }
  263. return 0;
  264. }
  265. static int tegra20_i2s_probe(struct snd_soc_dai *dai)
  266. {
  267. struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  268. dai->capture_dma_data = &i2s->capture_dma_data;
  269. dai->playback_dma_data = &i2s->playback_dma_data;
  270. return 0;
  271. }
  272. static const struct snd_soc_dai_ops tegra20_i2s_dai_ops = {
  273. .set_fmt = tegra20_i2s_set_fmt,
  274. .hw_params = tegra20_i2s_hw_params,
  275. .trigger = tegra20_i2s_trigger,
  276. };
  277. static const struct snd_soc_dai_driver tegra20_i2s_dai_template = {
  278. .probe = tegra20_i2s_probe,
  279. .playback = {
  280. .channels_min = 2,
  281. .channels_max = 2,
  282. .rates = SNDRV_PCM_RATE_8000_96000,
  283. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  284. },
  285. .capture = {
  286. .channels_min = 2,
  287. .channels_max = 2,
  288. .rates = SNDRV_PCM_RATE_8000_96000,
  289. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  290. },
  291. .ops = &tegra20_i2s_dai_ops,
  292. .symmetric_rates = 1,
  293. };
  294. static __devinit int tegra20_i2s_platform_probe(struct platform_device *pdev)
  295. {
  296. struct tegra20_i2s *i2s;
  297. struct resource *mem, *memregion, *dmareq;
  298. u32 of_dma[2];
  299. u32 dma_ch;
  300. int ret;
  301. i2s = devm_kzalloc(&pdev->dev, sizeof(struct tegra20_i2s), GFP_KERNEL);
  302. if (!i2s) {
  303. dev_err(&pdev->dev, "Can't allocate tegra20_i2s\n");
  304. ret = -ENOMEM;
  305. goto err;
  306. }
  307. dev_set_drvdata(&pdev->dev, i2s);
  308. i2s->dai = tegra20_i2s_dai_template;
  309. i2s->dai.name = dev_name(&pdev->dev);
  310. i2s->clk_i2s = clk_get(&pdev->dev, NULL);
  311. if (IS_ERR(i2s->clk_i2s)) {
  312. dev_err(&pdev->dev, "Can't retrieve i2s clock\n");
  313. ret = PTR_ERR(i2s->clk_i2s);
  314. goto err;
  315. }
  316. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  317. if (!mem) {
  318. dev_err(&pdev->dev, "No memory resource\n");
  319. ret = -ENODEV;
  320. goto err_clk_put;
  321. }
  322. dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  323. if (!dmareq) {
  324. if (of_property_read_u32_array(pdev->dev.of_node,
  325. "nvidia,dma-request-selector",
  326. of_dma, 2) < 0) {
  327. dev_err(&pdev->dev, "No DMA resource\n");
  328. ret = -ENODEV;
  329. goto err_clk_put;
  330. }
  331. dma_ch = of_dma[1];
  332. } else {
  333. dma_ch = dmareq->start;
  334. }
  335. memregion = devm_request_mem_region(&pdev->dev, mem->start,
  336. resource_size(mem), DRV_NAME);
  337. if (!memregion) {
  338. dev_err(&pdev->dev, "Memory region already claimed\n");
  339. ret = -EBUSY;
  340. goto err_clk_put;
  341. }
  342. i2s->regs = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
  343. if (!i2s->regs) {
  344. dev_err(&pdev->dev, "ioremap failed\n");
  345. ret = -ENOMEM;
  346. goto err_clk_put;
  347. }
  348. i2s->capture_dma_data.addr = mem->start + TEGRA20_I2S_FIFO2;
  349. i2s->capture_dma_data.wrap = 4;
  350. i2s->capture_dma_data.width = 32;
  351. i2s->capture_dma_data.req_sel = dma_ch;
  352. i2s->playback_dma_data.addr = mem->start + TEGRA20_I2S_FIFO1;
  353. i2s->playback_dma_data.wrap = 4;
  354. i2s->playback_dma_data.width = 32;
  355. i2s->playback_dma_data.req_sel = dma_ch;
  356. i2s->reg_ctrl = TEGRA20_I2S_CTRL_FIFO_FORMAT_PACKED;
  357. pm_runtime_enable(&pdev->dev);
  358. if (!pm_runtime_enabled(&pdev->dev)) {
  359. ret = tegra20_i2s_runtime_resume(&pdev->dev);
  360. if (ret)
  361. goto err_pm_disable;
  362. }
  363. ret = snd_soc_register_dai(&pdev->dev, &i2s->dai);
  364. if (ret) {
  365. dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  366. ret = -ENOMEM;
  367. goto err_suspend;
  368. }
  369. ret = tegra_pcm_platform_register(&pdev->dev);
  370. if (ret) {
  371. dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
  372. goto err_unregister_dai;
  373. }
  374. tegra20_i2s_debug_add(i2s);
  375. return 0;
  376. err_unregister_dai:
  377. snd_soc_unregister_dai(&pdev->dev);
  378. err_suspend:
  379. if (!pm_runtime_status_suspended(&pdev->dev))
  380. tegra20_i2s_runtime_suspend(&pdev->dev);
  381. err_pm_disable:
  382. pm_runtime_disable(&pdev->dev);
  383. err_clk_put:
  384. clk_put(i2s->clk_i2s);
  385. err:
  386. return ret;
  387. }
  388. static int __devexit tegra20_i2s_platform_remove(struct platform_device *pdev)
  389. {
  390. struct tegra20_i2s *i2s = dev_get_drvdata(&pdev->dev);
  391. pm_runtime_disable(&pdev->dev);
  392. if (!pm_runtime_status_suspended(&pdev->dev))
  393. tegra20_i2s_runtime_suspend(&pdev->dev);
  394. tegra_pcm_platform_unregister(&pdev->dev);
  395. snd_soc_unregister_dai(&pdev->dev);
  396. tegra20_i2s_debug_remove(i2s);
  397. clk_put(i2s->clk_i2s);
  398. return 0;
  399. }
  400. static const struct of_device_id tegra20_i2s_of_match[] __devinitconst = {
  401. { .compatible = "nvidia,tegra20-i2s", },
  402. {},
  403. };
  404. static const struct dev_pm_ops tegra20_i2s_pm_ops __devinitconst = {
  405. SET_RUNTIME_PM_OPS(tegra20_i2s_runtime_suspend,
  406. tegra20_i2s_runtime_resume, NULL)
  407. };
  408. static struct platform_driver tegra20_i2s_driver = {
  409. .driver = {
  410. .name = DRV_NAME,
  411. .owner = THIS_MODULE,
  412. .of_match_table = tegra20_i2s_of_match,
  413. .pm = &tegra20_i2s_pm_ops,
  414. },
  415. .probe = tegra20_i2s_platform_probe,
  416. .remove = __devexit_p(tegra20_i2s_platform_remove),
  417. };
  418. module_platform_driver(tegra20_i2s_driver);
  419. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  420. MODULE_DESCRIPTION("Tegra20 I2S ASoC driver");
  421. MODULE_LICENSE("GPL");
  422. MODULE_ALIAS("platform:" DRV_NAME);
  423. MODULE_DEVICE_TABLE(of, tegra20_i2s_of_match);