tegra20_ac97.c 12 KB

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