hda_tegra.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Implementation of primary ALSA driver code base for NVIDIA Tegra HDA.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/clocksource.h>
  8. #include <linux/completion.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/reset.h>
  21. #include <linux/slab.h>
  22. #include <linux/time.h>
  23. #include <linux/string.h>
  24. #include <linux/pm_runtime.h>
  25. #include <sound/core.h>
  26. #include <sound/initval.h>
  27. #include <sound/hda_codec.h>
  28. #include "hda_controller.h"
  29. /* Defines for Nvidia Tegra HDA support */
  30. #define HDA_BAR0 0x8000
  31. #define HDA_CFG_CMD 0x1004
  32. #define HDA_CFG_BAR0 0x1010
  33. #define HDA_ENABLE_IO_SPACE (1 << 0)
  34. #define HDA_ENABLE_MEM_SPACE (1 << 1)
  35. #define HDA_ENABLE_BUS_MASTER (1 << 2)
  36. #define HDA_ENABLE_SERR (1 << 8)
  37. #define HDA_DISABLE_INTR (1 << 10)
  38. #define HDA_BAR0_INIT_PROGRAM 0xFFFFFFFF
  39. #define HDA_BAR0_FINAL_PROGRAM (1 << 14)
  40. /* IPFS */
  41. #define HDA_IPFS_CONFIG 0x180
  42. #define HDA_IPFS_EN_FPCI 0x1
  43. #define HDA_IPFS_FPCI_BAR0 0x80
  44. #define HDA_FPCI_BAR0_START 0x40
  45. #define HDA_IPFS_INTR_MASK 0x188
  46. #define HDA_IPFS_EN_INTR (1 << 16)
  47. /* FPCI */
  48. #define FPCI_DBG_CFG_2 0x10F4
  49. #define FPCI_GCAP_NSDO_SHIFT 18
  50. #define FPCI_GCAP_NSDO_MASK (0x3 << FPCI_GCAP_NSDO_SHIFT)
  51. /* max number of SDs */
  52. #define NUM_CAPTURE_SD 1
  53. #define NUM_PLAYBACK_SD 1
  54. /*
  55. * Tegra194 does not reflect correct number of SDO lines. Below macro
  56. * is used to update the GCAP register to workaround the issue.
  57. */
  58. #define TEGRA194_NUM_SDO_LINES 4
  59. struct hda_tegra_soc {
  60. bool has_hda2codec_2x_reset;
  61. bool has_hda2hdmi;
  62. };
  63. struct hda_tegra {
  64. struct azx chip;
  65. struct device *dev;
  66. struct reset_control_bulk_data resets[3];
  67. struct clk_bulk_data clocks[3];
  68. unsigned int nresets;
  69. unsigned int nclocks;
  70. void __iomem *regs;
  71. struct work_struct probe_work;
  72. const struct hda_tegra_soc *soc;
  73. };
  74. #ifdef CONFIG_PM
  75. static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
  76. module_param(power_save, bint, 0644);
  77. MODULE_PARM_DESC(power_save,
  78. "Automatic power-saving timeout (in seconds, 0 = disable).");
  79. #else
  80. #define power_save 0
  81. #endif
  82. static const struct hda_controller_ops hda_tegra_ops; /* nothing special */
  83. static void hda_tegra_init(struct hda_tegra *hda)
  84. {
  85. u32 v;
  86. /* Enable PCI access */
  87. v = readl(hda->regs + HDA_IPFS_CONFIG);
  88. v |= HDA_IPFS_EN_FPCI;
  89. writel(v, hda->regs + HDA_IPFS_CONFIG);
  90. /* Enable MEM/IO space and bus master */
  91. v = readl(hda->regs + HDA_CFG_CMD);
  92. v &= ~HDA_DISABLE_INTR;
  93. v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE |
  94. HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR;
  95. writel(v, hda->regs + HDA_CFG_CMD);
  96. writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0);
  97. writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0);
  98. writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0);
  99. v = readl(hda->regs + HDA_IPFS_INTR_MASK);
  100. v |= HDA_IPFS_EN_INTR;
  101. writel(v, hda->regs + HDA_IPFS_INTR_MASK);
  102. }
  103. /*
  104. * power management
  105. */
  106. static int __maybe_unused hda_tegra_suspend(struct device *dev)
  107. {
  108. struct snd_card *card = dev_get_drvdata(dev);
  109. int rc;
  110. rc = pm_runtime_force_suspend(dev);
  111. if (rc < 0)
  112. return rc;
  113. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  114. return 0;
  115. }
  116. static int __maybe_unused hda_tegra_resume(struct device *dev)
  117. {
  118. struct snd_card *card = dev_get_drvdata(dev);
  119. int rc;
  120. rc = pm_runtime_force_resume(dev);
  121. if (rc < 0)
  122. return rc;
  123. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  124. return 0;
  125. }
  126. static int __maybe_unused hda_tegra_runtime_suspend(struct device *dev)
  127. {
  128. struct snd_card *card = dev_get_drvdata(dev);
  129. struct azx *chip = card->private_data;
  130. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  131. if (chip && chip->running) {
  132. /* enable controller wake up event */
  133. azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) |
  134. STATESTS_INT_MASK);
  135. azx_stop_chip(chip);
  136. azx_enter_link_reset(chip);
  137. }
  138. clk_bulk_disable_unprepare(hda->nclocks, hda->clocks);
  139. return 0;
  140. }
  141. static int __maybe_unused hda_tegra_runtime_resume(struct device *dev)
  142. {
  143. struct snd_card *card = dev_get_drvdata(dev);
  144. struct azx *chip = card->private_data;
  145. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  146. int rc;
  147. if (!chip->running) {
  148. rc = reset_control_bulk_assert(hda->nresets, hda->resets);
  149. if (rc)
  150. return rc;
  151. }
  152. rc = clk_bulk_prepare_enable(hda->nclocks, hda->clocks);
  153. if (rc != 0)
  154. return rc;
  155. if (chip->running) {
  156. hda_tegra_init(hda);
  157. azx_init_chip(chip, 1);
  158. /* disable controller wake up event*/
  159. azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) &
  160. ~STATESTS_INT_MASK);
  161. } else {
  162. usleep_range(10, 100);
  163. rc = reset_control_bulk_deassert(hda->nresets, hda->resets);
  164. if (rc)
  165. return rc;
  166. }
  167. return 0;
  168. }
  169. static const struct dev_pm_ops hda_tegra_pm = {
  170. SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
  171. SET_RUNTIME_PM_OPS(hda_tegra_runtime_suspend,
  172. hda_tegra_runtime_resume,
  173. NULL)
  174. };
  175. static int hda_tegra_dev_disconnect(struct snd_device *device)
  176. {
  177. struct azx *chip = device->device_data;
  178. chip->bus.shutdown = 1;
  179. return 0;
  180. }
  181. /*
  182. * destructor
  183. */
  184. static int hda_tegra_dev_free(struct snd_device *device)
  185. {
  186. struct azx *chip = device->device_data;
  187. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  188. cancel_work_sync(&hda->probe_work);
  189. if (azx_bus(chip)->chip_init) {
  190. azx_stop_all_streams(chip);
  191. azx_stop_chip(chip);
  192. }
  193. azx_free_stream_pages(chip);
  194. azx_free_streams(chip);
  195. snd_hdac_bus_exit(azx_bus(chip));
  196. return 0;
  197. }
  198. static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
  199. {
  200. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  201. struct hdac_bus *bus = azx_bus(chip);
  202. struct resource *res;
  203. hda->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  204. if (IS_ERR(hda->regs))
  205. return PTR_ERR(hda->regs);
  206. bus->remap_addr = hda->regs + HDA_BAR0;
  207. bus->addr = res->start + HDA_BAR0;
  208. hda_tegra_init(hda);
  209. return 0;
  210. }
  211. static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
  212. {
  213. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  214. struct hdac_bus *bus = azx_bus(chip);
  215. struct snd_card *card = chip->card;
  216. int err;
  217. unsigned short gcap;
  218. int irq_id = platform_get_irq(pdev, 0);
  219. const char *sname, *drv_name = "tegra-hda";
  220. struct device_node *np = pdev->dev.of_node;
  221. if (irq_id < 0)
  222. return irq_id;
  223. err = hda_tegra_init_chip(chip, pdev);
  224. if (err)
  225. return err;
  226. err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
  227. IRQF_SHARED, KBUILD_MODNAME, chip);
  228. if (err) {
  229. dev_err(chip->card->dev,
  230. "unable to request IRQ %d, disabling device\n",
  231. irq_id);
  232. return err;
  233. }
  234. bus->irq = irq_id;
  235. bus->dma_stop_delay = 100;
  236. card->sync_irq = bus->irq;
  237. /*
  238. * Tegra194 has 4 SDO lines and the STRIPE can be used to
  239. * indicate how many of the SDO lines the stream should be
  240. * striped. But GCAP register does not reflect the true
  241. * capability of HW. Below workaround helps to fix this.
  242. *
  243. * GCAP_NSDO is bits 19:18 in T_AZA_DBG_CFG_2,
  244. * 0 for 1 SDO, 1 for 2 SDO, 2 for 4 SDO lines.
  245. */
  246. if (of_device_is_compatible(np, "nvidia,tegra194-hda")) {
  247. u32 val;
  248. dev_info(card->dev, "Override SDO lines to %u\n",
  249. TEGRA194_NUM_SDO_LINES);
  250. val = readl(hda->regs + FPCI_DBG_CFG_2) & ~FPCI_GCAP_NSDO_MASK;
  251. val |= (TEGRA194_NUM_SDO_LINES >> 1) << FPCI_GCAP_NSDO_SHIFT;
  252. writel(val, hda->regs + FPCI_DBG_CFG_2);
  253. }
  254. gcap = azx_readw(chip, GCAP);
  255. dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
  256. chip->align_buffer_size = 1;
  257. /* read number of streams from GCAP register instead of using
  258. * hardcoded value
  259. */
  260. chip->capture_streams = (gcap >> 8) & 0x0f;
  261. /* The GCAP register on Tegra234 implies no Input Streams(ISS) support,
  262. * but the HW output stream descriptor programming should start with
  263. * offset 0x20*4 from base stream descriptor address. This will be a
  264. * problem while calculating the offset for output stream descriptor
  265. * which will be considering input stream also. So here output stream
  266. * starts with offset 0 which is wrong as HW register for output stream
  267. * offset starts with 4.
  268. */
  269. if (of_device_is_compatible(np, "nvidia,tegra234-hda"))
  270. chip->capture_streams = 4;
  271. chip->playback_streams = (gcap >> 12) & 0x0f;
  272. if (!chip->playback_streams && !chip->capture_streams) {
  273. /* gcap didn't give any info, switching to old method */
  274. chip->playback_streams = NUM_PLAYBACK_SD;
  275. chip->capture_streams = NUM_CAPTURE_SD;
  276. }
  277. chip->capture_index_offset = 0;
  278. chip->playback_index_offset = chip->capture_streams;
  279. chip->num_streams = chip->playback_streams + chip->capture_streams;
  280. /* initialize streams */
  281. err = azx_init_streams(chip);
  282. if (err < 0) {
  283. dev_err(card->dev, "failed to initialize streams: %d\n", err);
  284. return err;
  285. }
  286. err = azx_alloc_stream_pages(chip);
  287. if (err < 0) {
  288. dev_err(card->dev, "failed to allocate stream pages: %d\n",
  289. err);
  290. return err;
  291. }
  292. /* initialize chip */
  293. azx_init_chip(chip, 1);
  294. /*
  295. * Playback (for 44.1K/48K, 2-channel, 16-bps) fails with
  296. * 4 SDO lines due to legacy design limitation. Following
  297. * is, from HD Audio Specification (Revision 1.0a), used to
  298. * control striping of the stream across multiple SDO lines
  299. * for sample rates <= 48K.
  300. *
  301. * { ((num_channels * bits_per_sample) / number of SDOs) >= 8 }
  302. *
  303. * Due to legacy design issue it is recommended that above
  304. * ratio must be greater than 8. Since number of SDO lines is
  305. * in powers of 2, next available ratio is 16 which can be
  306. * used as a limiting factor here.
  307. */
  308. if (of_device_is_compatible(np, "nvidia,tegra30-hda"))
  309. chip->bus.core.sdo_limit = 16;
  310. /* codec detection */
  311. if (!bus->codec_mask) {
  312. dev_err(card->dev, "no codecs found!\n");
  313. return -ENODEV;
  314. }
  315. /* driver name */
  316. strscpy(card->driver, drv_name, sizeof(card->driver));
  317. /* shortname for card */
  318. sname = of_get_property(np, "nvidia,model", NULL);
  319. if (!sname)
  320. sname = drv_name;
  321. if (strlen(sname) > sizeof(card->shortname))
  322. dev_info(card->dev, "truncating shortname for card\n");
  323. strscpy(card->shortname, sname, sizeof(card->shortname));
  324. /* longname for card */
  325. snprintf(card->longname, sizeof(card->longname),
  326. "%s at 0x%lx irq %i",
  327. card->shortname, bus->addr, bus->irq);
  328. return 0;
  329. }
  330. /*
  331. * constructor
  332. */
  333. static void hda_tegra_probe_work(struct work_struct *work);
  334. static int hda_tegra_create(struct snd_card *card,
  335. unsigned int driver_caps,
  336. struct hda_tegra *hda)
  337. {
  338. static const struct snd_device_ops ops = {
  339. .dev_disconnect = hda_tegra_dev_disconnect,
  340. .dev_free = hda_tegra_dev_free,
  341. };
  342. struct azx *chip;
  343. int err;
  344. chip = &hda->chip;
  345. mutex_init(&chip->open_mutex);
  346. chip->card = card;
  347. chip->ops = &hda_tegra_ops;
  348. chip->driver_caps = driver_caps;
  349. chip->driver_type = driver_caps & 0xff;
  350. chip->dev_index = 0;
  351. chip->jackpoll_interval = msecs_to_jiffies(5000);
  352. INIT_LIST_HEAD(&chip->pcm_list);
  353. chip->codec_probe_mask = -1;
  354. chip->single_cmd = false;
  355. chip->snoop = true;
  356. INIT_WORK(&hda->probe_work, hda_tegra_probe_work);
  357. err = azx_bus_init(chip, NULL);
  358. if (err < 0)
  359. return err;
  360. chip->bus.core.sync_write = 0;
  361. chip->bus.core.needs_damn_long_delay = 1;
  362. chip->bus.core.aligned_mmio = 1;
  363. chip->bus.jackpoll_in_suspend = 1;
  364. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  365. if (err < 0) {
  366. dev_err(card->dev, "Error creating device\n");
  367. return err;
  368. }
  369. return 0;
  370. }
  371. static const struct hda_tegra_soc tegra30_data = {
  372. .has_hda2codec_2x_reset = true,
  373. .has_hda2hdmi = true,
  374. };
  375. static const struct hda_tegra_soc tegra194_data = {
  376. .has_hda2codec_2x_reset = false,
  377. .has_hda2hdmi = true,
  378. };
  379. static const struct hda_tegra_soc tegra234_data = {
  380. .has_hda2codec_2x_reset = true,
  381. .has_hda2hdmi = false,
  382. };
  383. static const struct of_device_id hda_tegra_match[] = {
  384. { .compatible = "nvidia,tegra30-hda", .data = &tegra30_data },
  385. { .compatible = "nvidia,tegra194-hda", .data = &tegra194_data },
  386. { .compatible = "nvidia,tegra234-hda", .data = &tegra234_data },
  387. {},
  388. };
  389. MODULE_DEVICE_TABLE(of, hda_tegra_match);
  390. static int hda_tegra_probe(struct platform_device *pdev)
  391. {
  392. const unsigned int driver_flags = AZX_DCAPS_CORBRP_SELF_CLEAR |
  393. AZX_DCAPS_PM_RUNTIME |
  394. AZX_DCAPS_4K_BDLE_BOUNDARY;
  395. struct snd_card *card;
  396. struct azx *chip;
  397. struct hda_tegra *hda;
  398. int err;
  399. hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
  400. if (!hda)
  401. return -ENOMEM;
  402. hda->dev = &pdev->dev;
  403. chip = &hda->chip;
  404. hda->soc = of_device_get_match_data(&pdev->dev);
  405. err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  406. THIS_MODULE, 0, &card);
  407. if (err < 0) {
  408. dev_err(&pdev->dev, "Error creating card!\n");
  409. return err;
  410. }
  411. hda->resets[hda->nresets++].id = "hda";
  412. /*
  413. * "hda2hdmi" is not applicable for Tegra234. This is because the
  414. * codec is separate IP and not under display SOR partition now.
  415. */
  416. if (hda->soc->has_hda2hdmi)
  417. hda->resets[hda->nresets++].id = "hda2hdmi";
  418. /*
  419. * "hda2codec_2x" reset is not present on Tegra194. Though DT would
  420. * be updated to reflect this, but to have backward compatibility
  421. * below is necessary.
  422. */
  423. if (hda->soc->has_hda2codec_2x_reset)
  424. hda->resets[hda->nresets++].id = "hda2codec_2x";
  425. err = devm_reset_control_bulk_get_exclusive(&pdev->dev, hda->nresets,
  426. hda->resets);
  427. if (err)
  428. goto out_free;
  429. hda->clocks[hda->nclocks++].id = "hda";
  430. if (hda->soc->has_hda2hdmi)
  431. hda->clocks[hda->nclocks++].id = "hda2hdmi";
  432. hda->clocks[hda->nclocks++].id = "hda2codec_2x";
  433. err = devm_clk_bulk_get(&pdev->dev, hda->nclocks, hda->clocks);
  434. if (err < 0)
  435. goto out_free;
  436. err = hda_tegra_create(card, driver_flags, hda);
  437. if (err < 0)
  438. goto out_free;
  439. card->private_data = chip;
  440. dev_set_drvdata(&pdev->dev, card);
  441. pm_runtime_enable(hda->dev);
  442. if (!azx_has_pm_runtime(chip))
  443. pm_runtime_forbid(hda->dev);
  444. schedule_work(&hda->probe_work);
  445. return 0;
  446. out_free:
  447. snd_card_free(card);
  448. return err;
  449. }
  450. static void hda_tegra_probe_work(struct work_struct *work)
  451. {
  452. struct hda_tegra *hda = container_of(work, struct hda_tegra, probe_work);
  453. struct azx *chip = &hda->chip;
  454. struct platform_device *pdev = to_platform_device(hda->dev);
  455. int err;
  456. pm_runtime_get_sync(hda->dev);
  457. err = hda_tegra_first_init(chip, pdev);
  458. if (err < 0)
  459. goto out_free;
  460. /* create codec instances */
  461. err = azx_probe_codecs(chip, 8);
  462. if (err < 0)
  463. goto out_free;
  464. err = azx_codec_configure(chip);
  465. if (err < 0)
  466. goto out_free;
  467. err = snd_card_register(chip->card);
  468. if (err < 0)
  469. goto out_free;
  470. chip->running = 1;
  471. snd_hda_set_power_save(&chip->bus, power_save * 1000);
  472. out_free:
  473. pm_runtime_put(hda->dev);
  474. return; /* no error return from async probe */
  475. }
  476. static void hda_tegra_remove(struct platform_device *pdev)
  477. {
  478. snd_card_free(dev_get_drvdata(&pdev->dev));
  479. pm_runtime_disable(&pdev->dev);
  480. }
  481. static void hda_tegra_shutdown(struct platform_device *pdev)
  482. {
  483. struct snd_card *card = dev_get_drvdata(&pdev->dev);
  484. struct azx *chip;
  485. if (!card)
  486. return;
  487. chip = card->private_data;
  488. if (chip && chip->running)
  489. azx_stop_chip(chip);
  490. }
  491. static struct platform_driver tegra_platform_hda = {
  492. .driver = {
  493. .name = "tegra-hda",
  494. .pm = &hda_tegra_pm,
  495. .of_match_table = hda_tegra_match,
  496. },
  497. .probe = hda_tegra_probe,
  498. .remove_new = hda_tegra_remove,
  499. .shutdown = hda_tegra_shutdown,
  500. };
  501. module_platform_driver(tegra_platform_hda);
  502. MODULE_DESCRIPTION("Tegra HDA bus driver");
  503. MODULE_LICENSE("GPL v2");