1 // SPDX-License-Identifier: GPL-2.0-only
4 * Implementation of primary ALSA driver code base for NVIDIA Tegra HDA.
8 #include <linux/clocksource.h>
9 #include <linux/completion.h>
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/mutex.h>
20 #include <linux/platform_device.h>
21 #include <linux/reset.h>
22 #include <linux/slab.h>
23 #include <linux/time.h>
24 #include <linux/string.h>
25 #include <linux/pm_runtime.h>
27 #include <sound/core.h>
28 #include <sound/initval.h>
30 #include <sound/hda_codec.h>
31 #include "hda_controller.h"
33 /* Defines for Nvidia Tegra HDA support */
34 #define HDA_BAR0 0x8000
36 #define HDA_CFG_CMD 0x1004
37 #define HDA_CFG_BAR0 0x1010
39 #define HDA_ENABLE_IO_SPACE (1 << 0)
40 #define HDA_ENABLE_MEM_SPACE (1 << 1)
41 #define HDA_ENABLE_BUS_MASTER (1 << 2)
42 #define HDA_ENABLE_SERR (1 << 8)
43 #define HDA_DISABLE_INTR (1 << 10)
44 #define HDA_BAR0_INIT_PROGRAM 0xFFFFFFFF
45 #define HDA_BAR0_FINAL_PROGRAM (1 << 14)
48 #define HDA_IPFS_CONFIG 0x180
49 #define HDA_IPFS_EN_FPCI 0x1
51 #define HDA_IPFS_FPCI_BAR0 0x80
52 #define HDA_FPCI_BAR0_START 0x40
54 #define HDA_IPFS_INTR_MASK 0x188
55 #define HDA_IPFS_EN_INTR (1 << 16)
58 #define FPCI_DBG_CFG_2 0x10F4
59 #define FPCI_GCAP_NSDO_SHIFT 18
60 #define FPCI_GCAP_NSDO_MASK (0x3 << FPCI_GCAP_NSDO_SHIFT)
62 /* max number of SDs */
63 #define NUM_CAPTURE_SD 1
64 #define NUM_PLAYBACK_SD 1
67 * Tegra194 does not reflect correct number of SDO lines. Below macro
68 * is used to update the GCAP register to workaround the issue.
70 #define TEGRA194_NUM_SDO_LINES 4
72 struct hda_tegra_soc {
73 bool has_hda2codec_2x_reset;
80 struct reset_control_bulk_data resets[3];
81 struct clk_bulk_data clocks[3];
85 struct work_struct probe_work;
86 const struct hda_tegra_soc *soc;
90 static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
91 module_param(power_save, bint, 0644);
92 MODULE_PARM_DESC(power_save,
93 "Automatic power-saving timeout (in seconds, 0 = disable).");
98 static const struct hda_controller_ops hda_tegra_ops; /* nothing special */
100 static void hda_tegra_init(struct hda_tegra *hda)
104 /* Enable PCI access */
105 v = readl(hda->regs + HDA_IPFS_CONFIG);
106 v |= HDA_IPFS_EN_FPCI;
107 writel(v, hda->regs + HDA_IPFS_CONFIG);
109 /* Enable MEM/IO space and bus master */
110 v = readl(hda->regs + HDA_CFG_CMD);
111 v &= ~HDA_DISABLE_INTR;
112 v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE |
113 HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR;
114 writel(v, hda->regs + HDA_CFG_CMD);
116 writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0);
117 writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0);
118 writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0);
120 v = readl(hda->regs + HDA_IPFS_INTR_MASK);
121 v |= HDA_IPFS_EN_INTR;
122 writel(v, hda->regs + HDA_IPFS_INTR_MASK);
128 static int __maybe_unused hda_tegra_suspend(struct device *dev)
130 struct snd_card *card = dev_get_drvdata(dev);
133 rc = pm_runtime_force_suspend(dev);
136 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
141 static int __maybe_unused hda_tegra_resume(struct device *dev)
143 struct snd_card *card = dev_get_drvdata(dev);
146 rc = pm_runtime_force_resume(dev);
149 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
154 static int __maybe_unused hda_tegra_runtime_suspend(struct device *dev)
156 struct snd_card *card = dev_get_drvdata(dev);
157 struct azx *chip = card->private_data;
158 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
160 if (chip && chip->running) {
161 /* enable controller wake up event */
162 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) |
166 azx_enter_link_reset(chip);
168 clk_bulk_disable_unprepare(hda->nclocks, hda->clocks);
173 static int __maybe_unused hda_tegra_runtime_resume(struct device *dev)
175 struct snd_card *card = dev_get_drvdata(dev);
176 struct azx *chip = card->private_data;
177 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
180 if (!chip->running) {
181 rc = reset_control_bulk_assert(hda->nresets, hda->resets);
186 rc = clk_bulk_prepare_enable(hda->nclocks, hda->clocks);
191 azx_init_chip(chip, 1);
192 /* disable controller wake up event*/
193 azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) &
196 usleep_range(10, 100);
198 rc = reset_control_bulk_deassert(hda->nresets, hda->resets);
206 static const struct dev_pm_ops hda_tegra_pm = {
207 SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
208 SET_RUNTIME_PM_OPS(hda_tegra_runtime_suspend,
209 hda_tegra_runtime_resume,
213 static int hda_tegra_dev_disconnect(struct snd_device *device)
215 struct azx *chip = device->device_data;
217 chip->bus.shutdown = 1;
224 static int hda_tegra_dev_free(struct snd_device *device)
226 struct azx *chip = device->device_data;
227 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
229 cancel_work_sync(&hda->probe_work);
230 if (azx_bus(chip)->chip_init) {
231 azx_stop_all_streams(chip);
235 azx_free_stream_pages(chip);
236 azx_free_streams(chip);
237 snd_hdac_bus_exit(azx_bus(chip));
242 static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
244 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
245 struct hdac_bus *bus = azx_bus(chip);
246 struct resource *res;
248 hda->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
249 if (IS_ERR(hda->regs))
250 return PTR_ERR(hda->regs);
252 bus->remap_addr = hda->regs + HDA_BAR0;
253 bus->addr = res->start + HDA_BAR0;
260 static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
262 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
263 struct hdac_bus *bus = azx_bus(chip);
264 struct snd_card *card = chip->card;
267 int irq_id = platform_get_irq(pdev, 0);
268 const char *sname, *drv_name = "tegra-hda";
269 struct device_node *np = pdev->dev.of_node;
274 err = hda_tegra_init_chip(chip, pdev);
278 err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
279 IRQF_SHARED, KBUILD_MODNAME, chip);
281 dev_err(chip->card->dev,
282 "unable to request IRQ %d, disabling device\n",
287 bus->dma_stop_delay = 100;
288 card->sync_irq = bus->irq;
291 * Tegra194 has 4 SDO lines and the STRIPE can be used to
292 * indicate how many of the SDO lines the stream should be
293 * striped. But GCAP register does not reflect the true
294 * capability of HW. Below workaround helps to fix this.
296 * GCAP_NSDO is bits 19:18 in T_AZA_DBG_CFG_2,
297 * 0 for 1 SDO, 1 for 2 SDO, 2 for 4 SDO lines.
299 if (of_device_is_compatible(np, "nvidia,tegra194-hda")) {
302 dev_info(card->dev, "Override SDO lines to %u\n",
303 TEGRA194_NUM_SDO_LINES);
305 val = readl(hda->regs + FPCI_DBG_CFG_2) & ~FPCI_GCAP_NSDO_MASK;
306 val |= (TEGRA194_NUM_SDO_LINES >> 1) << FPCI_GCAP_NSDO_SHIFT;
307 writel(val, hda->regs + FPCI_DBG_CFG_2);
310 gcap = azx_readw(chip, GCAP);
311 dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
313 chip->align_buffer_size = 1;
315 /* read number of streams from GCAP register instead of using
318 chip->capture_streams = (gcap >> 8) & 0x0f;
320 /* The GCAP register on Tegra234 implies no Input Streams(ISS) support,
321 * but the HW output stream descriptor programming should start with
322 * offset 0x20*4 from base stream descriptor address. This will be a
323 * problem while calculating the offset for output stream descriptor
324 * which will be considering input stream also. So here output stream
325 * starts with offset 0 which is wrong as HW register for output stream
326 * offset starts with 4.
328 if (of_device_is_compatible(np, "nvidia,tegra234-hda"))
329 chip->capture_streams = 4;
331 chip->playback_streams = (gcap >> 12) & 0x0f;
332 if (!chip->playback_streams && !chip->capture_streams) {
333 /* gcap didn't give any info, switching to old method */
334 chip->playback_streams = NUM_PLAYBACK_SD;
335 chip->capture_streams = NUM_CAPTURE_SD;
337 chip->capture_index_offset = 0;
338 chip->playback_index_offset = chip->capture_streams;
339 chip->num_streams = chip->playback_streams + chip->capture_streams;
341 /* initialize streams */
342 err = azx_init_streams(chip);
344 dev_err(card->dev, "failed to initialize streams: %d\n", err);
348 err = azx_alloc_stream_pages(chip);
350 dev_err(card->dev, "failed to allocate stream pages: %d\n",
355 /* initialize chip */
356 azx_init_chip(chip, 1);
359 * Playback (for 44.1K/48K, 2-channel, 16-bps) fails with
360 * 4 SDO lines due to legacy design limitation. Following
361 * is, from HD Audio Specification (Revision 1.0a), used to
362 * control striping of the stream across multiple SDO lines
363 * for sample rates <= 48K.
365 * { ((num_channels * bits_per_sample) / number of SDOs) >= 8 }
367 * Due to legacy design issue it is recommended that above
368 * ratio must be greater than 8. Since number of SDO lines is
369 * in powers of 2, next available ratio is 16 which can be
370 * used as a limiting factor here.
372 if (of_device_is_compatible(np, "nvidia,tegra30-hda"))
373 chip->bus.core.sdo_limit = 16;
375 /* codec detection */
376 if (!bus->codec_mask) {
377 dev_err(card->dev, "no codecs found!\n");
382 strscpy(card->driver, drv_name, sizeof(card->driver));
383 /* shortname for card */
384 sname = of_get_property(np, "nvidia,model", NULL);
387 if (strlen(sname) > sizeof(card->shortname))
388 dev_info(card->dev, "truncating shortname for card\n");
389 strscpy(card->shortname, sname, sizeof(card->shortname));
391 /* longname for card */
392 snprintf(card->longname, sizeof(card->longname),
393 "%s at 0x%lx irq %i",
394 card->shortname, bus->addr, bus->irq);
403 static void hda_tegra_probe_work(struct work_struct *work);
405 static int hda_tegra_create(struct snd_card *card,
406 unsigned int driver_caps,
407 struct hda_tegra *hda)
409 static const struct snd_device_ops ops = {
410 .dev_disconnect = hda_tegra_dev_disconnect,
411 .dev_free = hda_tegra_dev_free,
418 mutex_init(&chip->open_mutex);
420 chip->ops = &hda_tegra_ops;
421 chip->driver_caps = driver_caps;
422 chip->driver_type = driver_caps & 0xff;
424 chip->jackpoll_interval = msecs_to_jiffies(5000);
425 INIT_LIST_HEAD(&chip->pcm_list);
427 chip->codec_probe_mask = -1;
429 chip->single_cmd = false;
432 INIT_WORK(&hda->probe_work, hda_tegra_probe_work);
434 err = azx_bus_init(chip, NULL);
438 chip->bus.core.sync_write = 0;
439 chip->bus.core.needs_damn_long_delay = 1;
440 chip->bus.core.aligned_mmio = 1;
441 chip->bus.jackpoll_in_suspend = 1;
443 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
445 dev_err(card->dev, "Error creating device\n");
452 static const struct hda_tegra_soc tegra30_data = {
453 .has_hda2codec_2x_reset = true,
454 .has_hda2hdmi = true,
457 static const struct hda_tegra_soc tegra194_data = {
458 .has_hda2codec_2x_reset = false,
459 .has_hda2hdmi = true,
462 static const struct hda_tegra_soc tegra234_data = {
463 .has_hda2codec_2x_reset = true,
464 .has_hda2hdmi = false,
467 static const struct of_device_id hda_tegra_match[] = {
468 { .compatible = "nvidia,tegra30-hda", .data = &tegra30_data },
469 { .compatible = "nvidia,tegra194-hda", .data = &tegra194_data },
470 { .compatible = "nvidia,tegra234-hda", .data = &tegra234_data },
473 MODULE_DEVICE_TABLE(of, hda_tegra_match);
475 static int hda_tegra_probe(struct platform_device *pdev)
477 const unsigned int driver_flags = AZX_DCAPS_CORBRP_SELF_CLEAR |
478 AZX_DCAPS_PM_RUNTIME |
479 AZX_DCAPS_4K_BDLE_BOUNDARY;
480 struct snd_card *card;
482 struct hda_tegra *hda;
485 hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
488 hda->dev = &pdev->dev;
491 hda->soc = of_device_get_match_data(&pdev->dev);
493 err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
494 THIS_MODULE, 0, &card);
496 dev_err(&pdev->dev, "Error creating card!\n");
500 hda->resets[hda->nresets++].id = "hda";
503 * "hda2hdmi" is not applicable for Tegra234. This is because the
504 * codec is separate IP and not under display SOR partition now.
506 if (hda->soc->has_hda2hdmi)
507 hda->resets[hda->nresets++].id = "hda2hdmi";
510 * "hda2codec_2x" reset is not present on Tegra194. Though DT would
511 * be updated to reflect this, but to have backward compatibility
512 * below is necessary.
514 if (hda->soc->has_hda2codec_2x_reset)
515 hda->resets[hda->nresets++].id = "hda2codec_2x";
517 err = devm_reset_control_bulk_get_exclusive(&pdev->dev, hda->nresets,
522 hda->clocks[hda->nclocks++].id = "hda";
523 if (hda->soc->has_hda2hdmi)
524 hda->clocks[hda->nclocks++].id = "hda2hdmi";
525 hda->clocks[hda->nclocks++].id = "hda2codec_2x";
527 err = devm_clk_bulk_get(&pdev->dev, hda->nclocks, hda->clocks);
531 err = hda_tegra_create(card, driver_flags, hda);
534 card->private_data = chip;
536 dev_set_drvdata(&pdev->dev, card);
538 pm_runtime_enable(hda->dev);
539 if (!azx_has_pm_runtime(chip))
540 pm_runtime_forbid(hda->dev);
542 schedule_work(&hda->probe_work);
551 static void hda_tegra_probe_work(struct work_struct *work)
553 struct hda_tegra *hda = container_of(work, struct hda_tegra, probe_work);
554 struct azx *chip = &hda->chip;
555 struct platform_device *pdev = to_platform_device(hda->dev);
558 pm_runtime_get_sync(hda->dev);
559 err = hda_tegra_first_init(chip, pdev);
563 /* create codec instances */
564 err = azx_probe_codecs(chip, 8);
568 err = azx_codec_configure(chip);
572 err = snd_card_register(chip->card);
577 snd_hda_set_power_save(&chip->bus, power_save * 1000);
580 pm_runtime_put(hda->dev);
581 return; /* no error return from async probe */
584 static void hda_tegra_remove(struct platform_device *pdev)
586 snd_card_free(dev_get_drvdata(&pdev->dev));
587 pm_runtime_disable(&pdev->dev);
590 static void hda_tegra_shutdown(struct platform_device *pdev)
592 struct snd_card *card = dev_get_drvdata(&pdev->dev);
597 chip = card->private_data;
598 if (chip && chip->running)
602 static struct platform_driver tegra_platform_hda = {
606 .of_match_table = hda_tegra_match,
608 .probe = hda_tegra_probe,
609 .remove_new = hda_tegra_remove,
610 .shutdown = hda_tegra_shutdown,
612 module_platform_driver(tegra_platform_hda);
614 MODULE_DESCRIPTION("Tegra HDA bus driver");
615 MODULE_LICENSE("GPL v2");