tegra20_i2s.c 12 KB

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