hda_tegra.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. *
  3. * Implementation of primary ALSA driver code base for NVIDIA Tegra HDA.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/clocksource.h>
  20. #include <linux/completion.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/mutex.h>
  30. #include <linux/of_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/time.h>
  33. #include <sound/core.h>
  34. #include <sound/initval.h>
  35. #include "hda_codec.h"
  36. #include "hda_controller.h"
  37. /* Defines for Nvidia Tegra HDA support */
  38. #define HDA_BAR0 0x8000
  39. #define HDA_CFG_CMD 0x1004
  40. #define HDA_CFG_BAR0 0x1010
  41. #define HDA_ENABLE_IO_SPACE (1 << 0)
  42. #define HDA_ENABLE_MEM_SPACE (1 << 1)
  43. #define HDA_ENABLE_BUS_MASTER (1 << 2)
  44. #define HDA_ENABLE_SERR (1 << 8)
  45. #define HDA_DISABLE_INTR (1 << 10)
  46. #define HDA_BAR0_INIT_PROGRAM 0xFFFFFFFF
  47. #define HDA_BAR0_FINAL_PROGRAM (1 << 14)
  48. /* IPFS */
  49. #define HDA_IPFS_CONFIG 0x180
  50. #define HDA_IPFS_EN_FPCI 0x1
  51. #define HDA_IPFS_FPCI_BAR0 0x80
  52. #define HDA_FPCI_BAR0_START 0x40
  53. #define HDA_IPFS_INTR_MASK 0x188
  54. #define HDA_IPFS_EN_INTR (1 << 16)
  55. /* max number of SDs */
  56. #define NUM_CAPTURE_SD 1
  57. #define NUM_PLAYBACK_SD 1
  58. struct hda_tegra {
  59. struct azx chip;
  60. struct device *dev;
  61. struct clk *hda_clk;
  62. struct clk *hda2codec_2x_clk;
  63. struct clk *hda2hdmi_clk;
  64. void __iomem *regs;
  65. };
  66. #ifdef CONFIG_PM
  67. static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
  68. module_param(power_save, bint, 0644);
  69. MODULE_PARM_DESC(power_save,
  70. "Automatic power-saving timeout (in seconds, 0 = disable).");
  71. #else
  72. #define power_save 0
  73. #endif
  74. /*
  75. * DMA page allocation ops.
  76. */
  77. static int dma_alloc_pages(struct azx *chip, int type, size_t size,
  78. struct snd_dma_buffer *buf)
  79. {
  80. return snd_dma_alloc_pages(type, chip->card->dev, size, buf);
  81. }
  82. static void dma_free_pages(struct azx *chip, struct snd_dma_buffer *buf)
  83. {
  84. snd_dma_free_pages(buf);
  85. }
  86. static int substream_alloc_pages(struct azx *chip,
  87. struct snd_pcm_substream *substream,
  88. size_t size)
  89. {
  90. struct azx_dev *azx_dev = get_azx_dev(substream);
  91. azx_dev->bufsize = 0;
  92. azx_dev->period_bytes = 0;
  93. azx_dev->format_val = 0;
  94. return snd_pcm_lib_malloc_pages(substream, size);
  95. }
  96. static int substream_free_pages(struct azx *chip,
  97. struct snd_pcm_substream *substream)
  98. {
  99. return snd_pcm_lib_free_pages(substream);
  100. }
  101. /*
  102. * Register access ops. Tegra HDA register access is DWORD only.
  103. */
  104. static void hda_tegra_writel(u32 value, u32 *addr)
  105. {
  106. writel(value, addr);
  107. }
  108. static u32 hda_tegra_readl(u32 *addr)
  109. {
  110. return readl(addr);
  111. }
  112. static void hda_tegra_writew(u16 value, u16 *addr)
  113. {
  114. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  115. void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
  116. u32 v;
  117. v = readl(dword_addr);
  118. v &= ~(0xffff << shift);
  119. v |= value << shift;
  120. writel(v, dword_addr);
  121. }
  122. static u16 hda_tegra_readw(u16 *addr)
  123. {
  124. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  125. void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
  126. u32 v;
  127. v = readl(dword_addr);
  128. return (v >> shift) & 0xffff;
  129. }
  130. static void hda_tegra_writeb(u8 value, u8 *addr)
  131. {
  132. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  133. void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
  134. u32 v;
  135. v = readl(dword_addr);
  136. v &= ~(0xff << shift);
  137. v |= value << shift;
  138. writel(v, dword_addr);
  139. }
  140. static u8 hda_tegra_readb(u8 *addr)
  141. {
  142. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  143. void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
  144. u32 v;
  145. v = readl(dword_addr);
  146. return (v >> shift) & 0xff;
  147. }
  148. static const struct hda_controller_ops hda_tegra_ops = {
  149. .reg_writel = hda_tegra_writel,
  150. .reg_readl = hda_tegra_readl,
  151. .reg_writew = hda_tegra_writew,
  152. .reg_readw = hda_tegra_readw,
  153. .reg_writeb = hda_tegra_writeb,
  154. .reg_readb = hda_tegra_readb,
  155. .dma_alloc_pages = dma_alloc_pages,
  156. .dma_free_pages = dma_free_pages,
  157. .substream_alloc_pages = substream_alloc_pages,
  158. .substream_free_pages = substream_free_pages,
  159. };
  160. static void hda_tegra_init(struct hda_tegra *hda)
  161. {
  162. u32 v;
  163. /* Enable PCI access */
  164. v = readl(hda->regs + HDA_IPFS_CONFIG);
  165. v |= HDA_IPFS_EN_FPCI;
  166. writel(v, hda->regs + HDA_IPFS_CONFIG);
  167. /* Enable MEM/IO space and bus master */
  168. v = readl(hda->regs + HDA_CFG_CMD);
  169. v &= ~HDA_DISABLE_INTR;
  170. v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE |
  171. HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR;
  172. writel(v, hda->regs + HDA_CFG_CMD);
  173. writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0);
  174. writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0);
  175. writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0);
  176. v = readl(hda->regs + HDA_IPFS_INTR_MASK);
  177. v |= HDA_IPFS_EN_INTR;
  178. writel(v, hda->regs + HDA_IPFS_INTR_MASK);
  179. }
  180. static int hda_tegra_enable_clocks(struct hda_tegra *data)
  181. {
  182. int rc;
  183. rc = clk_prepare_enable(data->hda_clk);
  184. if (rc)
  185. return rc;
  186. rc = clk_prepare_enable(data->hda2codec_2x_clk);
  187. if (rc)
  188. goto disable_hda;
  189. rc = clk_prepare_enable(data->hda2hdmi_clk);
  190. if (rc)
  191. goto disable_codec_2x;
  192. return 0;
  193. disable_codec_2x:
  194. clk_disable_unprepare(data->hda2codec_2x_clk);
  195. disable_hda:
  196. clk_disable_unprepare(data->hda_clk);
  197. return rc;
  198. }
  199. #ifdef CONFIG_PM_SLEEP
  200. static void hda_tegra_disable_clocks(struct hda_tegra *data)
  201. {
  202. clk_disable_unprepare(data->hda2hdmi_clk);
  203. clk_disable_unprepare(data->hda2codec_2x_clk);
  204. clk_disable_unprepare(data->hda_clk);
  205. }
  206. /*
  207. * power management
  208. */
  209. static int hda_tegra_suspend(struct device *dev)
  210. {
  211. struct snd_card *card = dev_get_drvdata(dev);
  212. struct azx *chip = card->private_data;
  213. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  214. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  215. azx_stop_chip(chip);
  216. azx_enter_link_reset(chip);
  217. hda_tegra_disable_clocks(hda);
  218. return 0;
  219. }
  220. static int hda_tegra_resume(struct device *dev)
  221. {
  222. struct snd_card *card = dev_get_drvdata(dev);
  223. struct azx *chip = card->private_data;
  224. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  225. hda_tegra_enable_clocks(hda);
  226. hda_tegra_init(hda);
  227. azx_init_chip(chip, 1);
  228. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  229. return 0;
  230. }
  231. #endif /* CONFIG_PM_SLEEP */
  232. static const struct dev_pm_ops hda_tegra_pm = {
  233. SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
  234. };
  235. /*
  236. * destructor
  237. */
  238. static int hda_tegra_dev_free(struct snd_device *device)
  239. {
  240. int i;
  241. struct azx *chip = device->device_data;
  242. if (chip->initialized) {
  243. for (i = 0; i < chip->num_streams; i++)
  244. azx_stream_stop(chip, &chip->azx_dev[i]);
  245. azx_stop_chip(chip);
  246. }
  247. azx_free_stream_pages(chip);
  248. return 0;
  249. }
  250. static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
  251. {
  252. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  253. struct device *dev = hda->dev;
  254. struct resource *res;
  255. int err;
  256. hda->hda_clk = devm_clk_get(dev, "hda");
  257. if (IS_ERR(hda->hda_clk))
  258. return PTR_ERR(hda->hda_clk);
  259. hda->hda2codec_2x_clk = devm_clk_get(dev, "hda2codec_2x");
  260. if (IS_ERR(hda->hda2codec_2x_clk))
  261. return PTR_ERR(hda->hda2codec_2x_clk);
  262. hda->hda2hdmi_clk = devm_clk_get(dev, "hda2hdmi");
  263. if (IS_ERR(hda->hda2hdmi_clk))
  264. return PTR_ERR(hda->hda2hdmi_clk);
  265. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  266. hda->regs = devm_ioremap_resource(dev, res);
  267. if (IS_ERR(hda->regs))
  268. return PTR_ERR(hda->regs);
  269. chip->remap_addr = hda->regs + HDA_BAR0;
  270. chip->addr = res->start + HDA_BAR0;
  271. err = hda_tegra_enable_clocks(hda);
  272. if (err)
  273. return err;
  274. hda_tegra_init(hda);
  275. return 0;
  276. }
  277. static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
  278. {
  279. struct snd_card *card = chip->card;
  280. int err;
  281. unsigned short gcap;
  282. int irq_id = platform_get_irq(pdev, 0);
  283. err = hda_tegra_init_chip(chip, pdev);
  284. if (err)
  285. return err;
  286. err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
  287. IRQF_SHARED, KBUILD_MODNAME, chip);
  288. if (err) {
  289. dev_err(chip->card->dev,
  290. "unable to request IRQ %d, disabling device\n",
  291. irq_id);
  292. return err;
  293. }
  294. chip->irq = irq_id;
  295. synchronize_irq(chip->irq);
  296. gcap = azx_readw(chip, GCAP);
  297. dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
  298. /* read number of streams from GCAP register instead of using
  299. * hardcoded value
  300. */
  301. chip->capture_streams = (gcap >> 8) & 0x0f;
  302. chip->playback_streams = (gcap >> 12) & 0x0f;
  303. if (!chip->playback_streams && !chip->capture_streams) {
  304. /* gcap didn't give any info, switching to old method */
  305. chip->playback_streams = NUM_PLAYBACK_SD;
  306. chip->capture_streams = NUM_CAPTURE_SD;
  307. }
  308. chip->capture_index_offset = 0;
  309. chip->playback_index_offset = chip->capture_streams;
  310. chip->num_streams = chip->playback_streams + chip->capture_streams;
  311. chip->azx_dev = devm_kcalloc(card->dev, chip->num_streams,
  312. sizeof(*chip->azx_dev), GFP_KERNEL);
  313. if (!chip->azx_dev)
  314. return -ENOMEM;
  315. err = azx_alloc_stream_pages(chip);
  316. if (err < 0)
  317. return err;
  318. /* initialize streams */
  319. azx_init_stream(chip);
  320. /* initialize chip */
  321. azx_init_chip(chip, 1);
  322. /* codec detection */
  323. if (!chip->codec_mask) {
  324. dev_err(card->dev, "no codecs found!\n");
  325. return -ENODEV;
  326. }
  327. strcpy(card->driver, "tegra-hda");
  328. strcpy(card->shortname, "tegra-hda");
  329. snprintf(card->longname, sizeof(card->longname),
  330. "%s at 0x%lx irq %i",
  331. card->shortname, chip->addr, chip->irq);
  332. return 0;
  333. }
  334. /*
  335. * constructor
  336. */
  337. static int hda_tegra_create(struct snd_card *card,
  338. unsigned int driver_caps,
  339. const struct hda_controller_ops *hda_ops,
  340. struct hda_tegra *hda)
  341. {
  342. static struct snd_device_ops ops = {
  343. .dev_free = hda_tegra_dev_free,
  344. };
  345. struct azx *chip;
  346. int err;
  347. chip = &hda->chip;
  348. spin_lock_init(&chip->reg_lock);
  349. mutex_init(&chip->open_mutex);
  350. chip->card = card;
  351. chip->ops = hda_ops;
  352. chip->irq = -1;
  353. chip->driver_caps = driver_caps;
  354. chip->driver_type = driver_caps & 0xff;
  355. chip->dev_index = 0;
  356. INIT_LIST_HEAD(&chip->pcm_list);
  357. chip->codec_probe_mask = -1;
  358. chip->single_cmd = false;
  359. chip->snoop = true;
  360. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  361. if (err < 0) {
  362. dev_err(card->dev, "Error creating device\n");
  363. return err;
  364. }
  365. return 0;
  366. }
  367. static const struct of_device_id hda_tegra_match[] = {
  368. { .compatible = "nvidia,tegra30-hda" },
  369. {},
  370. };
  371. MODULE_DEVICE_TABLE(of, hda_tegra_match);
  372. static int hda_tegra_probe(struct platform_device *pdev)
  373. {
  374. struct snd_card *card;
  375. struct azx *chip;
  376. struct hda_tegra *hda;
  377. int err;
  378. const unsigned int driver_flags = AZX_DCAPS_RIRB_DELAY;
  379. hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
  380. if (!hda)
  381. return -ENOMEM;
  382. hda->dev = &pdev->dev;
  383. chip = &hda->chip;
  384. err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  385. THIS_MODULE, 0, &card);
  386. if (err < 0) {
  387. dev_err(&pdev->dev, "Error creating card!\n");
  388. return err;
  389. }
  390. err = hda_tegra_create(card, driver_flags, &hda_tegra_ops, hda);
  391. if (err < 0)
  392. goto out_free;
  393. card->private_data = chip;
  394. dev_set_drvdata(&pdev->dev, card);
  395. err = hda_tegra_first_init(chip, pdev);
  396. if (err < 0)
  397. goto out_free;
  398. /* create codec instances */
  399. err = azx_bus_create(chip, NULL);
  400. if (err < 0)
  401. goto out_free;
  402. err = azx_probe_codecs(chip, 0);
  403. if (err < 0)
  404. goto out_free;
  405. err = azx_codec_configure(chip);
  406. if (err < 0)
  407. goto out_free;
  408. err = snd_card_register(chip->card);
  409. if (err < 0)
  410. goto out_free;
  411. chip->running = 1;
  412. snd_hda_set_power_save(chip->bus, power_save * 1000);
  413. return 0;
  414. out_free:
  415. snd_card_free(card);
  416. return err;
  417. }
  418. static int hda_tegra_remove(struct platform_device *pdev)
  419. {
  420. return snd_card_free(dev_get_drvdata(&pdev->dev));
  421. }
  422. static void hda_tegra_shutdown(struct platform_device *pdev)
  423. {
  424. struct snd_card *card = dev_get_drvdata(&pdev->dev);
  425. struct azx *chip;
  426. if (!card)
  427. return;
  428. chip = card->private_data;
  429. if (chip && chip->running)
  430. azx_stop_chip(chip);
  431. }
  432. static struct platform_driver tegra_platform_hda = {
  433. .driver = {
  434. .name = "tegra-hda",
  435. .pm = &hda_tegra_pm,
  436. .of_match_table = hda_tegra_match,
  437. },
  438. .probe = hda_tegra_probe,
  439. .remove = hda_tegra_remove,
  440. .shutdown = hda_tegra_shutdown,
  441. };
  442. module_platform_driver(tegra_platform_hda);
  443. MODULE_DESCRIPTION("Tegra HDA bus driver");
  444. MODULE_LICENSE("GPL v2");