tegra20_ac97.c 12 KB

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