tegra_i2s.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 <linux/of.h>
  39. #include <mach/iomap.h>
  40. #include <sound/core.h>
  41. #include <sound/pcm.h>
  42. #include <sound/pcm_params.h>
  43. #include <sound/soc.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. clk_enable(i2s->clk_i2s);
  75. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  76. u32 val = tegra_i2s_read(i2s, regs[i].offset);
  77. seq_printf(s, "%s = %08x\n", regs[i].name, val);
  78. }
  79. clk_disable(i2s->clk_i2s);
  80. return 0;
  81. }
  82. static int tegra_i2s_debug_open(struct inode *inode, struct file *file)
  83. {
  84. return single_open(file, tegra_i2s_show, inode->i_private);
  85. }
  86. static const struct file_operations tegra_i2s_debug_fops = {
  87. .open = tegra_i2s_debug_open,
  88. .read = seq_read,
  89. .llseek = seq_lseek,
  90. .release = single_release,
  91. };
  92. static void tegra_i2s_debug_add(struct tegra_i2s *i2s)
  93. {
  94. i2s->debug = debugfs_create_file(i2s->dai.name, S_IRUGO,
  95. snd_soc_debugfs_root, i2s,
  96. &tegra_i2s_debug_fops);
  97. }
  98. static void tegra_i2s_debug_remove(struct tegra_i2s *i2s)
  99. {
  100. if (i2s->debug)
  101. debugfs_remove(i2s->debug);
  102. }
  103. #else
  104. static inline void tegra_i2s_debug_add(struct tegra_i2s *i2s)
  105. {
  106. }
  107. static inline void tegra_i2s_debug_remove(struct tegra_i2s *i2s)
  108. {
  109. }
  110. #endif
  111. static int tegra_i2s_set_fmt(struct snd_soc_dai *dai,
  112. unsigned int fmt)
  113. {
  114. struct tegra_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  115. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  116. case SND_SOC_DAIFMT_NB_NF:
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. i2s->reg_ctrl &= ~TEGRA_I2S_CTRL_MASTER_ENABLE;
  122. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  123. case SND_SOC_DAIFMT_CBS_CFS:
  124. i2s->reg_ctrl |= TEGRA_I2S_CTRL_MASTER_ENABLE;
  125. break;
  126. case SND_SOC_DAIFMT_CBM_CFM:
  127. break;
  128. default:
  129. return -EINVAL;
  130. }
  131. i2s->reg_ctrl &= ~(TEGRA_I2S_CTRL_BIT_FORMAT_MASK |
  132. TEGRA_I2S_CTRL_LRCK_MASK);
  133. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  134. case SND_SOC_DAIFMT_DSP_A:
  135. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_DSP;
  136. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_L_LOW;
  137. break;
  138. case SND_SOC_DAIFMT_DSP_B:
  139. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_DSP;
  140. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_R_LOW;
  141. break;
  142. case SND_SOC_DAIFMT_I2S:
  143. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_I2S;
  144. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_L_LOW;
  145. break;
  146. case SND_SOC_DAIFMT_RIGHT_J:
  147. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_RJM;
  148. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_L_LOW;
  149. break;
  150. case SND_SOC_DAIFMT_LEFT_J:
  151. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_FORMAT_LJM;
  152. i2s->reg_ctrl |= TEGRA_I2S_CTRL_LRCK_L_LOW;
  153. break;
  154. default:
  155. return -EINVAL;
  156. }
  157. return 0;
  158. }
  159. static int tegra_i2s_hw_params(struct snd_pcm_substream *substream,
  160. struct snd_pcm_hw_params *params,
  161. struct snd_soc_dai *dai)
  162. {
  163. struct device *dev = substream->pcm->card->dev;
  164. struct tegra_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  165. u32 reg;
  166. int ret, sample_size, srate, i2sclock, bitcnt;
  167. i2s->reg_ctrl &= ~TEGRA_I2S_CTRL_BIT_SIZE_MASK;
  168. switch (params_format(params)) {
  169. case SNDRV_PCM_FORMAT_S16_LE:
  170. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_SIZE_16;
  171. sample_size = 16;
  172. break;
  173. case SNDRV_PCM_FORMAT_S24_LE:
  174. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_SIZE_24;
  175. sample_size = 24;
  176. break;
  177. case SNDRV_PCM_FORMAT_S32_LE:
  178. i2s->reg_ctrl |= TEGRA_I2S_CTRL_BIT_SIZE_32;
  179. sample_size = 32;
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. srate = params_rate(params);
  185. /* Final "* 2" required by Tegra hardware */
  186. i2sclock = srate * params_channels(params) * sample_size * 2;
  187. ret = clk_set_rate(i2s->clk_i2s, i2sclock);
  188. if (ret) {
  189. dev_err(dev, "Can't set I2S clock rate: %d\n", ret);
  190. return ret;
  191. }
  192. bitcnt = (i2sclock / (2 * srate)) - 1;
  193. if (bitcnt < 0 || bitcnt > TEGRA_I2S_TIMING_CHANNEL_BIT_COUNT_MASK_US)
  194. return -EINVAL;
  195. reg = bitcnt << TEGRA_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
  196. if (i2sclock % (2 * srate))
  197. reg |= TEGRA_I2S_TIMING_NON_SYM_ENABLE;
  198. if (!i2s->clk_refs)
  199. clk_enable(i2s->clk_i2s);
  200. tegra_i2s_write(i2s, TEGRA_I2S_TIMING, reg);
  201. tegra_i2s_write(i2s, TEGRA_I2S_FIFO_SCR,
  202. TEGRA_I2S_FIFO_SCR_FIFO2_ATN_LVL_FOUR_SLOTS |
  203. TEGRA_I2S_FIFO_SCR_FIFO1_ATN_LVL_FOUR_SLOTS);
  204. if (!i2s->clk_refs)
  205. clk_disable(i2s->clk_i2s);
  206. return 0;
  207. }
  208. static void tegra_i2s_start_playback(struct tegra_i2s *i2s)
  209. {
  210. i2s->reg_ctrl |= TEGRA_I2S_CTRL_FIFO1_ENABLE;
  211. tegra_i2s_write(i2s, TEGRA_I2S_CTRL, i2s->reg_ctrl);
  212. }
  213. static void tegra_i2s_stop_playback(struct tegra_i2s *i2s)
  214. {
  215. i2s->reg_ctrl &= ~TEGRA_I2S_CTRL_FIFO1_ENABLE;
  216. tegra_i2s_write(i2s, TEGRA_I2S_CTRL, i2s->reg_ctrl);
  217. }
  218. static void tegra_i2s_start_capture(struct tegra_i2s *i2s)
  219. {
  220. i2s->reg_ctrl |= TEGRA_I2S_CTRL_FIFO2_ENABLE;
  221. tegra_i2s_write(i2s, TEGRA_I2S_CTRL, i2s->reg_ctrl);
  222. }
  223. static void tegra_i2s_stop_capture(struct tegra_i2s *i2s)
  224. {
  225. i2s->reg_ctrl &= ~TEGRA_I2S_CTRL_FIFO2_ENABLE;
  226. tegra_i2s_write(i2s, TEGRA_I2S_CTRL, i2s->reg_ctrl);
  227. }
  228. static int tegra_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  229. struct snd_soc_dai *dai)
  230. {
  231. struct tegra_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  232. switch (cmd) {
  233. case SNDRV_PCM_TRIGGER_START:
  234. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  235. case SNDRV_PCM_TRIGGER_RESUME:
  236. if (!i2s->clk_refs)
  237. clk_enable(i2s->clk_i2s);
  238. i2s->clk_refs++;
  239. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  240. tegra_i2s_start_playback(i2s);
  241. else
  242. tegra_i2s_start_capture(i2s);
  243. break;
  244. case SNDRV_PCM_TRIGGER_STOP:
  245. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  246. case SNDRV_PCM_TRIGGER_SUSPEND:
  247. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  248. tegra_i2s_stop_playback(i2s);
  249. else
  250. tegra_i2s_stop_capture(i2s);
  251. i2s->clk_refs--;
  252. if (!i2s->clk_refs)
  253. clk_disable(i2s->clk_i2s);
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. return 0;
  259. }
  260. static int tegra_i2s_probe(struct snd_soc_dai *dai)
  261. {
  262. struct tegra_i2s * i2s = snd_soc_dai_get_drvdata(dai);
  263. dai->capture_dma_data = &i2s->capture_dma_data;
  264. dai->playback_dma_data = &i2s->playback_dma_data;
  265. return 0;
  266. }
  267. static const struct snd_soc_dai_ops tegra_i2s_dai_ops = {
  268. .set_fmt = tegra_i2s_set_fmt,
  269. .hw_params = tegra_i2s_hw_params,
  270. .trigger = tegra_i2s_trigger,
  271. };
  272. static const struct snd_soc_dai_driver tegra_i2s_dai_template = {
  273. .probe = tegra_i2s_probe,
  274. .playback = {
  275. .channels_min = 2,
  276. .channels_max = 2,
  277. .rates = SNDRV_PCM_RATE_8000_96000,
  278. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  279. },
  280. .capture = {
  281. .channels_min = 2,
  282. .channels_max = 2,
  283. .rates = SNDRV_PCM_RATE_8000_96000,
  284. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  285. },
  286. .ops = &tegra_i2s_dai_ops,
  287. .symmetric_rates = 1,
  288. };
  289. static __devinit int tegra_i2s_platform_probe(struct platform_device *pdev)
  290. {
  291. struct tegra_i2s * i2s;
  292. struct resource *mem, *memregion, *dmareq;
  293. u32 of_dma[2];
  294. u32 dma_ch;
  295. int ret;
  296. i2s = devm_kzalloc(&pdev->dev, sizeof(struct tegra_i2s), GFP_KERNEL);
  297. if (!i2s) {
  298. dev_err(&pdev->dev, "Can't allocate tegra_i2s\n");
  299. ret = -ENOMEM;
  300. goto err;
  301. }
  302. dev_set_drvdata(&pdev->dev, i2s);
  303. i2s->dai = tegra_i2s_dai_template;
  304. i2s->dai.name = dev_name(&pdev->dev);
  305. i2s->clk_i2s = clk_get(&pdev->dev, NULL);
  306. if (IS_ERR(i2s->clk_i2s)) {
  307. dev_err(&pdev->dev, "Can't retrieve i2s clock\n");
  308. ret = PTR_ERR(i2s->clk_i2s);
  309. goto err;
  310. }
  311. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  312. if (!mem) {
  313. dev_err(&pdev->dev, "No memory resource\n");
  314. ret = -ENODEV;
  315. goto err_clk_put;
  316. }
  317. dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  318. if (!dmareq) {
  319. if (of_property_read_u32_array(pdev->dev.of_node,
  320. "nvidia,dma-request-selector",
  321. of_dma, 2) < 0) {
  322. dev_err(&pdev->dev, "No DMA resource\n");
  323. ret = -ENODEV;
  324. goto err_clk_put;
  325. }
  326. dma_ch = of_dma[1];
  327. } else {
  328. dma_ch = dmareq->start;
  329. }
  330. memregion = devm_request_mem_region(&pdev->dev, mem->start,
  331. resource_size(mem), DRV_NAME);
  332. if (!memregion) {
  333. dev_err(&pdev->dev, "Memory region already claimed\n");
  334. ret = -EBUSY;
  335. goto err_clk_put;
  336. }
  337. i2s->regs = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
  338. if (!i2s->regs) {
  339. dev_err(&pdev->dev, "ioremap failed\n");
  340. ret = -ENOMEM;
  341. goto err_clk_put;
  342. }
  343. i2s->capture_dma_data.addr = mem->start + TEGRA_I2S_FIFO2;
  344. i2s->capture_dma_data.wrap = 4;
  345. i2s->capture_dma_data.width = 32;
  346. i2s->capture_dma_data.req_sel = dma_ch;
  347. i2s->playback_dma_data.addr = mem->start + TEGRA_I2S_FIFO1;
  348. i2s->playback_dma_data.wrap = 4;
  349. i2s->playback_dma_data.width = 32;
  350. i2s->playback_dma_data.req_sel = dma_ch;
  351. i2s->reg_ctrl = TEGRA_I2S_CTRL_FIFO_FORMAT_PACKED;
  352. ret = snd_soc_register_dai(&pdev->dev, &i2s->dai);
  353. if (ret) {
  354. dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  355. ret = -ENOMEM;
  356. goto err_clk_put;
  357. }
  358. tegra_i2s_debug_add(i2s);
  359. return 0;
  360. err_clk_put:
  361. clk_put(i2s->clk_i2s);
  362. err:
  363. return ret;
  364. }
  365. static int __devexit tegra_i2s_platform_remove(struct platform_device *pdev)
  366. {
  367. struct tegra_i2s *i2s = dev_get_drvdata(&pdev->dev);
  368. snd_soc_unregister_dai(&pdev->dev);
  369. tegra_i2s_debug_remove(i2s);
  370. clk_put(i2s->clk_i2s);
  371. return 0;
  372. }
  373. static const struct of_device_id tegra_i2s_of_match[] __devinitconst = {
  374. { .compatible = "nvidia,tegra20-i2s", },
  375. {},
  376. };
  377. static struct platform_driver tegra_i2s_driver = {
  378. .driver = {
  379. .name = DRV_NAME,
  380. .owner = THIS_MODULE,
  381. .of_match_table = tegra_i2s_of_match,
  382. },
  383. .probe = tegra_i2s_platform_probe,
  384. .remove = __devexit_p(tegra_i2s_platform_remove),
  385. };
  386. module_platform_driver(tegra_i2s_driver);
  387. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  388. MODULE_DESCRIPTION("Tegra I2S ASoC driver");
  389. MODULE_LICENSE("GPL");
  390. MODULE_ALIAS("platform:" DRV_NAME);
  391. MODULE_DEVICE_TABLE(of, tegra_i2s_of_match);