tegra_i2s.c 11 KB

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