tegra20_ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * tegra20_ac97.c - Tegra20 AC97 platform driver
  3. *
  4. * Copyright (c) 2012 Lucas Stach <dev@lynxeye.de>
  5. *
  6. * Partly based on code copyright/by:
  7. *
  8. * Copyright (c) 2011,2012 Toradex Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/gpio.h>
  24. #include <linux/io.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/of_gpio.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/regmap.h>
  32. #include <linux/slab.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/pcm_params.h>
  36. #include <sound/soc.h>
  37. #include <sound/dmaengine_pcm.h>
  38. #include "tegra20_ac97.h"
  39. #define DRV_NAME "tegra20-ac97"
  40. static struct tegra20_ac97 *workdata;
  41. static void tegra20_ac97_codec_reset(struct snd_ac97 *ac97)
  42. {
  43. u32 readback;
  44. unsigned long timeout;
  45. /* reset line is not driven by DAC pad group, have to toggle GPIO */
  46. gpio_set_value(workdata->reset_gpio, 0);
  47. udelay(2);
  48. gpio_set_value(workdata->reset_gpio, 1);
  49. udelay(2);
  50. timeout = jiffies + msecs_to_jiffies(100);
  51. do {
  52. regmap_read(workdata->regmap, TEGRA20_AC97_STATUS1, &readback);
  53. if (readback & TEGRA20_AC97_STATUS1_CODEC1_RDY)
  54. break;
  55. usleep_range(1000, 2000);
  56. } while (!time_after(jiffies, timeout));
  57. }
  58. static void tegra20_ac97_codec_warm_reset(struct snd_ac97 *ac97)
  59. {
  60. u32 readback;
  61. unsigned long timeout;
  62. /*
  63. * although sync line is driven by the DAC pad group warm reset using
  64. * the controller cmd is not working, have to toggle sync line
  65. * manually.
  66. */
  67. gpio_request(workdata->sync_gpio, "codec-sync");
  68. gpio_direction_output(workdata->sync_gpio, 1);
  69. udelay(2);
  70. gpio_set_value(workdata->sync_gpio, 0);
  71. udelay(2);
  72. gpio_free(workdata->sync_gpio);
  73. timeout = jiffies + msecs_to_jiffies(100);
  74. do {
  75. regmap_read(workdata->regmap, TEGRA20_AC97_STATUS1, &readback);
  76. if (readback & TEGRA20_AC97_STATUS1_CODEC1_RDY)
  77. break;
  78. usleep_range(1000, 2000);
  79. } while (!time_after(jiffies, timeout));
  80. }
  81. static unsigned short tegra20_ac97_codec_read(struct snd_ac97 *ac97_snd,
  82. unsigned short reg)
  83. {
  84. u32 readback;
  85. unsigned long timeout;
  86. regmap_write(workdata->regmap, TEGRA20_AC97_CMD,
  87. (((reg | 0x80) << TEGRA20_AC97_CMD_CMD_ADDR_SHIFT) &
  88. TEGRA20_AC97_CMD_CMD_ADDR_MASK) |
  89. TEGRA20_AC97_CMD_BUSY);
  90. timeout = jiffies + msecs_to_jiffies(100);
  91. do {
  92. regmap_read(workdata->regmap, TEGRA20_AC97_STATUS1, &readback);
  93. if (readback & TEGRA20_AC97_STATUS1_STA_VALID1)
  94. break;
  95. usleep_range(1000, 2000);
  96. } while (!time_after(jiffies, timeout));
  97. return ((readback & TEGRA20_AC97_STATUS1_STA_DATA1_MASK) >>
  98. TEGRA20_AC97_STATUS1_STA_DATA1_SHIFT);
  99. }
  100. static void tegra20_ac97_codec_write(struct snd_ac97 *ac97_snd,
  101. unsigned short reg, unsigned short val)
  102. {
  103. u32 readback;
  104. unsigned long timeout;
  105. regmap_write(workdata->regmap, TEGRA20_AC97_CMD,
  106. ((reg << TEGRA20_AC97_CMD_CMD_ADDR_SHIFT) &
  107. TEGRA20_AC97_CMD_CMD_ADDR_MASK) |
  108. ((val << TEGRA20_AC97_CMD_CMD_DATA_SHIFT) &
  109. TEGRA20_AC97_CMD_CMD_DATA_MASK) |
  110. TEGRA20_AC97_CMD_BUSY);
  111. timeout = jiffies + msecs_to_jiffies(100);
  112. do {
  113. regmap_read(workdata->regmap, TEGRA20_AC97_CMD, &readback);
  114. if (!(readback & TEGRA20_AC97_CMD_BUSY))
  115. break;
  116. usleep_range(1000, 2000);
  117. } while (!time_after(jiffies, timeout));
  118. }
  119. static struct snd_ac97_bus_ops tegra20_ac97_ops = {
  120. .read = tegra20_ac97_codec_read,
  121. .write = tegra20_ac97_codec_write,
  122. .reset = tegra20_ac97_codec_reset,
  123. .warm_reset = tegra20_ac97_codec_warm_reset,
  124. };
  125. static inline void tegra20_ac97_start_playback(struct tegra20_ac97 *ac97)
  126. {
  127. regmap_update_bits(ac97->regmap, TEGRA20_AC97_FIFO1_SCR,
  128. TEGRA20_AC97_FIFO_SCR_PB_QRT_MT_EN,
  129. TEGRA20_AC97_FIFO_SCR_PB_QRT_MT_EN);
  130. regmap_update_bits(ac97->regmap, TEGRA20_AC97_CTRL,
  131. TEGRA20_AC97_CTRL_PCM_DAC_EN |
  132. TEGRA20_AC97_CTRL_STM_EN,
  133. TEGRA20_AC97_CTRL_PCM_DAC_EN |
  134. TEGRA20_AC97_CTRL_STM_EN);
  135. }
  136. static inline void tegra20_ac97_stop_playback(struct tegra20_ac97 *ac97)
  137. {
  138. regmap_update_bits(ac97->regmap, TEGRA20_AC97_FIFO1_SCR,
  139. TEGRA20_AC97_FIFO_SCR_PB_QRT_MT_EN, 0);
  140. regmap_update_bits(ac97->regmap, TEGRA20_AC97_CTRL,
  141. TEGRA20_AC97_CTRL_PCM_DAC_EN, 0);
  142. }
  143. static inline void tegra20_ac97_start_capture(struct tegra20_ac97 *ac97)
  144. {
  145. regmap_update_bits(ac97->regmap, TEGRA20_AC97_FIFO1_SCR,
  146. TEGRA20_AC97_FIFO_SCR_REC_FULL_EN,
  147. TEGRA20_AC97_FIFO_SCR_REC_FULL_EN);
  148. }
  149. static inline void tegra20_ac97_stop_capture(struct tegra20_ac97 *ac97)
  150. {
  151. regmap_update_bits(ac97->regmap, TEGRA20_AC97_FIFO1_SCR,
  152. TEGRA20_AC97_FIFO_SCR_REC_FULL_EN, 0);
  153. }
  154. static int tegra20_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
  155. struct snd_soc_dai *dai)
  156. {
  157. struct tegra20_ac97 *ac97 = snd_soc_dai_get_drvdata(dai);
  158. switch (cmd) {
  159. case SNDRV_PCM_TRIGGER_START:
  160. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  161. case SNDRV_PCM_TRIGGER_RESUME:
  162. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  163. tegra20_ac97_start_playback(ac97);
  164. else
  165. tegra20_ac97_start_capture(ac97);
  166. break;
  167. case SNDRV_PCM_TRIGGER_STOP:
  168. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  169. case SNDRV_PCM_TRIGGER_SUSPEND:
  170. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  171. tegra20_ac97_stop_playback(ac97);
  172. else
  173. tegra20_ac97_stop_capture(ac97);
  174. break;
  175. default:
  176. return -EINVAL;
  177. }
  178. return 0;
  179. }
  180. static const struct snd_soc_dai_ops tegra20_ac97_dai_ops = {
  181. .trigger = tegra20_ac97_trigger,
  182. };
  183. static int tegra20_ac97_probe(struct snd_soc_dai *dai)
  184. {
  185. struct tegra20_ac97 *ac97 = snd_soc_dai_get_drvdata(dai);
  186. dai->capture_dma_data = &ac97->capture_dma_data;
  187. dai->playback_dma_data = &ac97->playback_dma_data;
  188. return 0;
  189. }
  190. static struct snd_soc_dai_driver tegra20_ac97_dai = {
  191. .name = "tegra-ac97-pcm",
  192. .bus_control = true,
  193. .probe = tegra20_ac97_probe,
  194. .playback = {
  195. .stream_name = "PCM Playback",
  196. .channels_min = 2,
  197. .channels_max = 2,
  198. .rates = SNDRV_PCM_RATE_8000_48000,
  199. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  200. },
  201. .capture = {
  202. .stream_name = "PCM Capture",
  203. .channels_min = 2,
  204. .channels_max = 2,
  205. .rates = SNDRV_PCM_RATE_8000_48000,
  206. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  207. },
  208. .ops = &tegra20_ac97_dai_ops,
  209. };
  210. static const struct snd_soc_component_driver tegra20_ac97_component = {
  211. .name = DRV_NAME,
  212. };
  213. static bool tegra20_ac97_wr_rd_reg(struct device *dev, unsigned int reg)
  214. {
  215. switch (reg) {
  216. case TEGRA20_AC97_CTRL:
  217. case TEGRA20_AC97_CMD:
  218. case TEGRA20_AC97_STATUS1:
  219. case TEGRA20_AC97_FIFO1_SCR:
  220. case TEGRA20_AC97_FIFO_TX1:
  221. case TEGRA20_AC97_FIFO_RX1:
  222. return true;
  223. default:
  224. break;
  225. }
  226. return false;
  227. }
  228. static bool tegra20_ac97_volatile_reg(struct device *dev, unsigned int reg)
  229. {
  230. switch (reg) {
  231. case TEGRA20_AC97_STATUS1:
  232. case TEGRA20_AC97_FIFO1_SCR:
  233. case TEGRA20_AC97_FIFO_TX1:
  234. case TEGRA20_AC97_FIFO_RX1:
  235. return true;
  236. default:
  237. break;
  238. }
  239. return false;
  240. }
  241. static bool tegra20_ac97_precious_reg(struct device *dev, unsigned int reg)
  242. {
  243. switch (reg) {
  244. case TEGRA20_AC97_FIFO_TX1:
  245. case TEGRA20_AC97_FIFO_RX1:
  246. return true;
  247. default:
  248. break;
  249. }
  250. return false;
  251. }
  252. static const struct regmap_config tegra20_ac97_regmap_config = {
  253. .reg_bits = 32,
  254. .reg_stride = 4,
  255. .val_bits = 32,
  256. .max_register = TEGRA20_AC97_FIFO_RX1,
  257. .writeable_reg = tegra20_ac97_wr_rd_reg,
  258. .readable_reg = tegra20_ac97_wr_rd_reg,
  259. .volatile_reg = tegra20_ac97_volatile_reg,
  260. .precious_reg = tegra20_ac97_precious_reg,
  261. .cache_type = REGCACHE_FLAT,
  262. };
  263. static int tegra20_ac97_platform_probe(struct platform_device *pdev)
  264. {
  265. struct tegra20_ac97 *ac97;
  266. struct resource *mem;
  267. void __iomem *regs;
  268. int ret = 0;
  269. ac97 = devm_kzalloc(&pdev->dev, sizeof(struct tegra20_ac97),
  270. GFP_KERNEL);
  271. if (!ac97) {
  272. dev_err(&pdev->dev, "Can't allocate tegra20_ac97\n");
  273. ret = -ENOMEM;
  274. goto err;
  275. }
  276. dev_set_drvdata(&pdev->dev, ac97);
  277. ac97->clk_ac97 = devm_clk_get(&pdev->dev, NULL);
  278. if (IS_ERR(ac97->clk_ac97)) {
  279. dev_err(&pdev->dev, "Can't retrieve ac97 clock\n");
  280. ret = PTR_ERR(ac97->clk_ac97);
  281. goto err;
  282. }
  283. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  284. regs = devm_ioremap_resource(&pdev->dev, mem);
  285. if (IS_ERR(regs)) {
  286. ret = PTR_ERR(regs);
  287. goto err_clk_put;
  288. }
  289. ac97->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
  290. &tegra20_ac97_regmap_config);
  291. if (IS_ERR(ac97->regmap)) {
  292. dev_err(&pdev->dev, "regmap init failed\n");
  293. ret = PTR_ERR(ac97->regmap);
  294. goto err_clk_put;
  295. }
  296. ac97->reset_gpio = of_get_named_gpio(pdev->dev.of_node,
  297. "nvidia,codec-reset-gpio", 0);
  298. if (gpio_is_valid(ac97->reset_gpio)) {
  299. ret = devm_gpio_request_one(&pdev->dev, ac97->reset_gpio,
  300. GPIOF_OUT_INIT_HIGH, "codec-reset");
  301. if (ret) {
  302. dev_err(&pdev->dev, "could not get codec-reset GPIO\n");
  303. goto err_clk_put;
  304. }
  305. } else {
  306. dev_err(&pdev->dev, "no codec-reset GPIO supplied\n");
  307. goto err_clk_put;
  308. }
  309. ac97->sync_gpio = of_get_named_gpio(pdev->dev.of_node,
  310. "nvidia,codec-sync-gpio", 0);
  311. if (!gpio_is_valid(ac97->sync_gpio)) {
  312. dev_err(&pdev->dev, "no codec-sync GPIO supplied\n");
  313. goto err_clk_put;
  314. }
  315. ac97->capture_dma_data.addr = mem->start + TEGRA20_AC97_FIFO_RX1;
  316. ac97->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  317. ac97->capture_dma_data.maxburst = 4;
  318. ac97->playback_dma_data.addr = mem->start + TEGRA20_AC97_FIFO_TX1;
  319. ac97->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  320. ac97->playback_dma_data.maxburst = 4;
  321. ret = clk_prepare_enable(ac97->clk_ac97);
  322. if (ret) {
  323. dev_err(&pdev->dev, "clk_enable failed: %d\n", ret);
  324. goto err;
  325. }
  326. ret = snd_soc_set_ac97_ops(&tegra20_ac97_ops);
  327. if (ret) {
  328. dev_err(&pdev->dev, "Failed to set AC'97 ops: %d\n", ret);
  329. goto err_clk_disable_unprepare;
  330. }
  331. ret = snd_soc_register_component(&pdev->dev, &tegra20_ac97_component,
  332. &tegra20_ac97_dai, 1);
  333. if (ret) {
  334. dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  335. ret = -ENOMEM;
  336. goto err_clk_disable_unprepare;
  337. }
  338. ret = tegra_pcm_platform_register(&pdev->dev);
  339. if (ret) {
  340. dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
  341. goto err_unregister_component;
  342. }
  343. /* XXX: crufty ASoC AC97 API - only one AC97 codec allowed */
  344. workdata = ac97;
  345. return 0;
  346. err_unregister_component:
  347. snd_soc_unregister_component(&pdev->dev);
  348. err_clk_disable_unprepare:
  349. clk_disable_unprepare(ac97->clk_ac97);
  350. err_clk_put:
  351. err:
  352. snd_soc_set_ac97_ops(NULL);
  353. return ret;
  354. }
  355. static int tegra20_ac97_platform_remove(struct platform_device *pdev)
  356. {
  357. struct tegra20_ac97 *ac97 = dev_get_drvdata(&pdev->dev);
  358. tegra_pcm_platform_unregister(&pdev->dev);
  359. snd_soc_unregister_component(&pdev->dev);
  360. clk_disable_unprepare(ac97->clk_ac97);
  361. snd_soc_set_ac97_ops(NULL);
  362. return 0;
  363. }
  364. static const struct of_device_id tegra20_ac97_of_match[] = {
  365. { .compatible = "nvidia,tegra20-ac97", },
  366. {},
  367. };
  368. static struct platform_driver tegra20_ac97_driver = {
  369. .driver = {
  370. .name = DRV_NAME,
  371. .of_match_table = tegra20_ac97_of_match,
  372. },
  373. .probe = tegra20_ac97_platform_probe,
  374. .remove = tegra20_ac97_platform_remove,
  375. };
  376. module_platform_driver(tegra20_ac97_driver);
  377. MODULE_AUTHOR("Lucas Stach");
  378. MODULE_DESCRIPTION("Tegra20 AC97 ASoC driver");
  379. MODULE_LICENSE("GPL v2");
  380. MODULE_ALIAS("platform:" DRV_NAME);
  381. MODULE_DEVICE_TABLE(of, tegra20_ac97_of_match);