xref: /openbmc/linux/sound/pci/hda/hda_tegra.c (revision 5a170e9e)
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 
19 #include <linux/clk.h>
20 #include <linux/clocksource.h>
21 #include <linux/completion.h>
22 #include <linux/delay.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/io.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/mutex.h>
31 #include <linux/of_device.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/string.h>
35 #include <linux/pm_runtime.h>
36 
37 #include <sound/core.h>
38 #include <sound/initval.h>
39 
40 #include <sound/hda_codec.h>
41 #include "hda_controller.h"
42 
43 /* Defines for Nvidia Tegra HDA support */
44 #define HDA_BAR0           0x8000
45 
46 #define HDA_CFG_CMD        0x1004
47 #define HDA_CFG_BAR0       0x1010
48 
49 #define HDA_ENABLE_IO_SPACE       (1 << 0)
50 #define HDA_ENABLE_MEM_SPACE      (1 << 1)
51 #define HDA_ENABLE_BUS_MASTER     (1 << 2)
52 #define HDA_ENABLE_SERR           (1 << 8)
53 #define HDA_DISABLE_INTR          (1 << 10)
54 #define HDA_BAR0_INIT_PROGRAM     0xFFFFFFFF
55 #define HDA_BAR0_FINAL_PROGRAM    (1 << 14)
56 
57 /* IPFS */
58 #define HDA_IPFS_CONFIG           0x180
59 #define HDA_IPFS_EN_FPCI          0x1
60 
61 #define HDA_IPFS_FPCI_BAR0        0x80
62 #define HDA_FPCI_BAR0_START       0x40
63 
64 #define HDA_IPFS_INTR_MASK        0x188
65 #define HDA_IPFS_EN_INTR          (1 << 16)
66 
67 /* max number of SDs */
68 #define NUM_CAPTURE_SD 1
69 #define NUM_PLAYBACK_SD 1
70 
71 struct hda_tegra {
72 	struct azx chip;
73 	struct device *dev;
74 	struct clk *hda_clk;
75 	struct clk *hda2codec_2x_clk;
76 	struct clk *hda2hdmi_clk;
77 	void __iomem *regs;
78 	struct work_struct probe_work;
79 };
80 
81 #ifdef CONFIG_PM
82 static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
83 module_param(power_save, bint, 0644);
84 MODULE_PARM_DESC(power_save,
85 		 "Automatic power-saving timeout (in seconds, 0 = disable).");
86 #else
87 #define power_save	0
88 #endif
89 
90 /*
91  * DMA page allocation ops.
92  */
93 static int dma_alloc_pages(struct hdac_bus *bus, int type, size_t size,
94 			   struct snd_dma_buffer *buf)
95 {
96 	return snd_dma_alloc_pages(type, bus->dev, size, buf);
97 }
98 
99 static void dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
100 {
101 	snd_dma_free_pages(buf);
102 }
103 
104 /*
105  * Register access ops. Tegra HDA register access is DWORD only.
106  */
107 static void hda_tegra_writel(u32 value, u32 __iomem *addr)
108 {
109 	writel(value, addr);
110 }
111 
112 static u32 hda_tegra_readl(u32 __iomem *addr)
113 {
114 	return readl(addr);
115 }
116 
117 static void hda_tegra_writew(u16 value, u16 __iomem  *addr)
118 {
119 	unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
120 	void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
121 	u32 v;
122 
123 	v = readl(dword_addr);
124 	v &= ~(0xffff << shift);
125 	v |= value << shift;
126 	writel(v, dword_addr);
127 }
128 
129 static u16 hda_tegra_readw(u16 __iomem *addr)
130 {
131 	unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
132 	void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
133 	u32 v;
134 
135 	v = readl(dword_addr);
136 	return (v >> shift) & 0xffff;
137 }
138 
139 static void hda_tegra_writeb(u8 value, u8 __iomem *addr)
140 {
141 	unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
142 	void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
143 	u32 v;
144 
145 	v = readl(dword_addr);
146 	v &= ~(0xff << shift);
147 	v |= value << shift;
148 	writel(v, dword_addr);
149 }
150 
151 static u8 hda_tegra_readb(u8 __iomem *addr)
152 {
153 	unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
154 	void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
155 	u32 v;
156 
157 	v = readl(dword_addr);
158 	return (v >> shift) & 0xff;
159 }
160 
161 static const struct hdac_io_ops hda_tegra_io_ops = {
162 	.reg_writel = hda_tegra_writel,
163 	.reg_readl = hda_tegra_readl,
164 	.reg_writew = hda_tegra_writew,
165 	.reg_readw = hda_tegra_readw,
166 	.reg_writeb = hda_tegra_writeb,
167 	.reg_readb = hda_tegra_readb,
168 	.dma_alloc_pages = dma_alloc_pages,
169 	.dma_free_pages = dma_free_pages,
170 };
171 
172 static const struct hda_controller_ops hda_tegra_ops; /* nothing special */
173 
174 static void hda_tegra_init(struct hda_tegra *hda)
175 {
176 	u32 v;
177 
178 	/* Enable PCI access */
179 	v = readl(hda->regs + HDA_IPFS_CONFIG);
180 	v |= HDA_IPFS_EN_FPCI;
181 	writel(v, hda->regs + HDA_IPFS_CONFIG);
182 
183 	/* Enable MEM/IO space and bus master */
184 	v = readl(hda->regs + HDA_CFG_CMD);
185 	v &= ~HDA_DISABLE_INTR;
186 	v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE |
187 		HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR;
188 	writel(v, hda->regs + HDA_CFG_CMD);
189 
190 	writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0);
191 	writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0);
192 	writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0);
193 
194 	v = readl(hda->regs + HDA_IPFS_INTR_MASK);
195 	v |= HDA_IPFS_EN_INTR;
196 	writel(v, hda->regs + HDA_IPFS_INTR_MASK);
197 }
198 
199 static int hda_tegra_enable_clocks(struct hda_tegra *data)
200 {
201 	int rc;
202 
203 	rc = clk_prepare_enable(data->hda_clk);
204 	if (rc)
205 		return rc;
206 	rc = clk_prepare_enable(data->hda2codec_2x_clk);
207 	if (rc)
208 		goto disable_hda;
209 	rc = clk_prepare_enable(data->hda2hdmi_clk);
210 	if (rc)
211 		goto disable_codec_2x;
212 
213 	return 0;
214 
215 disable_codec_2x:
216 	clk_disable_unprepare(data->hda2codec_2x_clk);
217 disable_hda:
218 	clk_disable_unprepare(data->hda_clk);
219 	return rc;
220 }
221 
222 #ifdef CONFIG_PM_SLEEP
223 static void hda_tegra_disable_clocks(struct hda_tegra *data)
224 {
225 	clk_disable_unprepare(data->hda2hdmi_clk);
226 	clk_disable_unprepare(data->hda2codec_2x_clk);
227 	clk_disable_unprepare(data->hda_clk);
228 }
229 
230 /*
231  * power management
232  */
233 static int hda_tegra_suspend(struct device *dev)
234 {
235 	struct snd_card *card = dev_get_drvdata(dev);
236 	int rc;
237 
238 	rc = pm_runtime_force_suspend(dev);
239 	if (rc < 0)
240 		return rc;
241 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
242 
243 	return 0;
244 }
245 
246 static int hda_tegra_resume(struct device *dev)
247 {
248 	struct snd_card *card = dev_get_drvdata(dev);
249 	int rc;
250 
251 	rc = pm_runtime_force_resume(dev);
252 	if (rc < 0)
253 		return rc;
254 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
255 
256 	return 0;
257 }
258 #endif /* CONFIG_PM_SLEEP */
259 
260 #ifdef CONFIG_PM
261 static int hda_tegra_runtime_suspend(struct device *dev)
262 {
263 	struct snd_card *card = dev_get_drvdata(dev);
264 	struct azx *chip = card->private_data;
265 	struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
266 	struct hdac_bus *bus = azx_bus(chip);
267 
268 	if (chip && chip->running) {
269 		azx_stop_chip(chip);
270 		synchronize_irq(bus->irq);
271 		azx_enter_link_reset(chip);
272 	}
273 	hda_tegra_disable_clocks(hda);
274 
275 	return 0;
276 }
277 
278 static int hda_tegra_runtime_resume(struct device *dev)
279 {
280 	struct snd_card *card = dev_get_drvdata(dev);
281 	struct azx *chip = card->private_data;
282 	struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
283 	int rc;
284 
285 	rc = hda_tegra_enable_clocks(hda);
286 	if (rc != 0)
287 		return rc;
288 	if (chip && chip->running) {
289 		hda_tegra_init(hda);
290 		azx_init_chip(chip, 1);
291 	}
292 
293 	return 0;
294 }
295 #endif /* CONFIG_PM */
296 
297 static const struct dev_pm_ops hda_tegra_pm = {
298 	SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
299 	SET_RUNTIME_PM_OPS(hda_tegra_runtime_suspend,
300 			   hda_tegra_runtime_resume,
301 			   NULL)
302 };
303 
304 static int hda_tegra_dev_disconnect(struct snd_device *device)
305 {
306 	struct azx *chip = device->device_data;
307 
308 	chip->bus.shutdown = 1;
309 	return 0;
310 }
311 
312 /*
313  * destructor
314  */
315 static int hda_tegra_dev_free(struct snd_device *device)
316 {
317 	struct azx *chip = device->device_data;
318 	struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
319 
320 	cancel_work_sync(&hda->probe_work);
321 	if (azx_bus(chip)->chip_init) {
322 		azx_stop_all_streams(chip);
323 		azx_stop_chip(chip);
324 	}
325 
326 	azx_free_stream_pages(chip);
327 	azx_free_streams(chip);
328 	snd_hdac_bus_exit(azx_bus(chip));
329 
330 	return 0;
331 }
332 
333 static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
334 {
335 	struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
336 	struct hdac_bus *bus = azx_bus(chip);
337 	struct device *dev = hda->dev;
338 	struct resource *res;
339 
340 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
341 	hda->regs = devm_ioremap_resource(dev, res);
342 	if (IS_ERR(hda->regs))
343 		return PTR_ERR(hda->regs);
344 
345 	bus->remap_addr = hda->regs + HDA_BAR0;
346 	bus->addr = res->start + HDA_BAR0;
347 
348 	hda_tegra_init(hda);
349 
350 	return 0;
351 }
352 
353 static int hda_tegra_init_clk(struct hda_tegra *hda)
354 {
355 	struct device *dev = hda->dev;
356 
357 	hda->hda_clk = devm_clk_get(dev, "hda");
358 	if (IS_ERR(hda->hda_clk)) {
359 		dev_err(dev, "failed to get hda clock\n");
360 		return PTR_ERR(hda->hda_clk);
361 	}
362 	hda->hda2codec_2x_clk = devm_clk_get(dev, "hda2codec_2x");
363 	if (IS_ERR(hda->hda2codec_2x_clk)) {
364 		dev_err(dev, "failed to get hda2codec_2x clock\n");
365 		return PTR_ERR(hda->hda2codec_2x_clk);
366 	}
367 	hda->hda2hdmi_clk = devm_clk_get(dev, "hda2hdmi");
368 	if (IS_ERR(hda->hda2hdmi_clk)) {
369 		dev_err(dev, "failed to get hda2hdmi clock\n");
370 		return PTR_ERR(hda->hda2hdmi_clk);
371 	}
372 
373 	return 0;
374 }
375 
376 static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
377 {
378 	struct hdac_bus *bus = azx_bus(chip);
379 	struct snd_card *card = chip->card;
380 	int err;
381 	unsigned short gcap;
382 	int irq_id = platform_get_irq(pdev, 0);
383 	const char *sname;
384 	struct device_node *root;
385 
386 	err = hda_tegra_init_chip(chip, pdev);
387 	if (err)
388 		return err;
389 
390 	err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
391 			     IRQF_SHARED, KBUILD_MODNAME, chip);
392 	if (err) {
393 		dev_err(chip->card->dev,
394 			"unable to request IRQ %d, disabling device\n",
395 			irq_id);
396 		return err;
397 	}
398 	bus->irq = irq_id;
399 
400 	synchronize_irq(bus->irq);
401 
402 	gcap = azx_readw(chip, GCAP);
403 	dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
404 
405 	/* read number of streams from GCAP register instead of using
406 	 * hardcoded value
407 	 */
408 	chip->capture_streams = (gcap >> 8) & 0x0f;
409 	chip->playback_streams = (gcap >> 12) & 0x0f;
410 	if (!chip->playback_streams && !chip->capture_streams) {
411 		/* gcap didn't give any info, switching to old method */
412 		chip->playback_streams = NUM_PLAYBACK_SD;
413 		chip->capture_streams = NUM_CAPTURE_SD;
414 	}
415 	chip->capture_index_offset = 0;
416 	chip->playback_index_offset = chip->capture_streams;
417 	chip->num_streams = chip->playback_streams + chip->capture_streams;
418 
419 	/* initialize streams */
420 	err = azx_init_streams(chip);
421 	if (err < 0) {
422 		dev_err(card->dev, "failed to initialize streams: %d\n", err);
423 		return err;
424 	}
425 
426 	err = azx_alloc_stream_pages(chip);
427 	if (err < 0) {
428 		dev_err(card->dev, "failed to allocate stream pages: %d\n",
429 			err);
430 		return err;
431 	}
432 
433 	/* initialize chip */
434 	azx_init_chip(chip, 1);
435 
436 	/* codec detection */
437 	if (!bus->codec_mask) {
438 		dev_err(card->dev, "no codecs found!\n");
439 		return -ENODEV;
440 	}
441 
442 	/* driver name */
443 	strcpy(card->driver, "tegra-hda");
444 
445 	root = of_find_node_by_path("/");
446 	sname = of_get_property(root, "compatible", NULL);
447 	of_node_put(root);
448 	if (!sname) {
449 		dev_err(card->dev,
450 			"failed to get compatible property from root node\n");
451 		return -ENODEV;
452 	}
453 	/* shortname for card */
454 	if (strlen(sname) > sizeof(card->shortname))
455 		dev_info(card->dev, "truncating shortname for card\n");
456 	strncpy(card->shortname, sname, sizeof(card->shortname));
457 
458 	/* longname for card */
459 	snprintf(card->longname, sizeof(card->longname),
460 		 "%s at 0x%lx irq %i",
461 		 card->shortname, bus->addr, bus->irq);
462 
463 	return 0;
464 }
465 
466 /*
467  * constructor
468  */
469 
470 static void hda_tegra_probe_work(struct work_struct *work);
471 
472 static int hda_tegra_create(struct snd_card *card,
473 			    unsigned int driver_caps,
474 			    struct hda_tegra *hda)
475 {
476 	static struct snd_device_ops ops = {
477 		.dev_disconnect = hda_tegra_dev_disconnect,
478 		.dev_free = hda_tegra_dev_free,
479 	};
480 	struct azx *chip;
481 	int err;
482 
483 	chip = &hda->chip;
484 
485 	mutex_init(&chip->open_mutex);
486 	chip->card = card;
487 	chip->ops = &hda_tegra_ops;
488 	chip->driver_caps = driver_caps;
489 	chip->driver_type = driver_caps & 0xff;
490 	chip->dev_index = 0;
491 	INIT_LIST_HEAD(&chip->pcm_list);
492 
493 	chip->codec_probe_mask = -1;
494 
495 	chip->single_cmd = false;
496 	chip->snoop = true;
497 
498 	INIT_WORK(&hda->probe_work, hda_tegra_probe_work);
499 
500 	err = azx_bus_init(chip, NULL, &hda_tegra_io_ops);
501 	if (err < 0)
502 		return err;
503 
504 	chip->bus.needs_damn_long_delay = 1;
505 
506 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
507 	if (err < 0) {
508 		dev_err(card->dev, "Error creating device\n");
509 		return err;
510 	}
511 
512 	return 0;
513 }
514 
515 static const struct of_device_id hda_tegra_match[] = {
516 	{ .compatible = "nvidia,tegra30-hda" },
517 	{},
518 };
519 MODULE_DEVICE_TABLE(of, hda_tegra_match);
520 
521 static int hda_tegra_probe(struct platform_device *pdev)
522 {
523 	const unsigned int driver_flags = AZX_DCAPS_CORBRP_SELF_CLEAR |
524 					  AZX_DCAPS_PM_RUNTIME;
525 	struct snd_card *card;
526 	struct azx *chip;
527 	struct hda_tegra *hda;
528 	int err;
529 
530 	hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
531 	if (!hda)
532 		return -ENOMEM;
533 	hda->dev = &pdev->dev;
534 	chip = &hda->chip;
535 
536 	err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
537 			   THIS_MODULE, 0, &card);
538 	if (err < 0) {
539 		dev_err(&pdev->dev, "Error creating card!\n");
540 		return err;
541 	}
542 
543 	err = hda_tegra_init_clk(hda);
544 	if (err < 0)
545 		goto out_free;
546 
547 	err = hda_tegra_create(card, driver_flags, hda);
548 	if (err < 0)
549 		goto out_free;
550 	card->private_data = chip;
551 
552 	dev_set_drvdata(&pdev->dev, card);
553 
554 	pm_runtime_enable(hda->dev);
555 	if (!azx_has_pm_runtime(chip))
556 		pm_runtime_forbid(hda->dev);
557 
558 	schedule_work(&hda->probe_work);
559 
560 	return 0;
561 
562 out_free:
563 	snd_card_free(card);
564 	return err;
565 }
566 
567 static void hda_tegra_probe_work(struct work_struct *work)
568 {
569 	struct hda_tegra *hda = container_of(work, struct hda_tegra, probe_work);
570 	struct azx *chip = &hda->chip;
571 	struct platform_device *pdev = to_platform_device(hda->dev);
572 	int err;
573 
574 	pm_runtime_get_sync(hda->dev);
575 	err = hda_tegra_first_init(chip, pdev);
576 	if (err < 0)
577 		goto out_free;
578 
579 	/* create codec instances */
580 	err = azx_probe_codecs(chip, 8);
581 	if (err < 0)
582 		goto out_free;
583 
584 	err = azx_codec_configure(chip);
585 	if (err < 0)
586 		goto out_free;
587 
588 	err = snd_card_register(chip->card);
589 	if (err < 0)
590 		goto out_free;
591 
592 	chip->running = 1;
593 	snd_hda_set_power_save(&chip->bus, power_save * 1000);
594 
595  out_free:
596 	pm_runtime_put(hda->dev);
597 	return; /* no error return from async probe */
598 }
599 
600 static int hda_tegra_remove(struct platform_device *pdev)
601 {
602 	int ret;
603 
604 	ret = snd_card_free(dev_get_drvdata(&pdev->dev));
605 	pm_runtime_disable(&pdev->dev);
606 
607 	return ret;
608 }
609 
610 static void hda_tegra_shutdown(struct platform_device *pdev)
611 {
612 	struct snd_card *card = dev_get_drvdata(&pdev->dev);
613 	struct azx *chip;
614 
615 	if (!card)
616 		return;
617 	chip = card->private_data;
618 	if (chip && chip->running)
619 		azx_stop_chip(chip);
620 }
621 
622 static struct platform_driver tegra_platform_hda = {
623 	.driver = {
624 		.name = "tegra-hda",
625 		.pm = &hda_tegra_pm,
626 		.of_match_table = hda_tegra_match,
627 	},
628 	.probe = hda_tegra_probe,
629 	.remove = hda_tegra_remove,
630 	.shutdown = hda_tegra_shutdown,
631 };
632 module_platform_driver(tegra_platform_hda);
633 
634 MODULE_DESCRIPTION("Tegra HDA bus driver");
635 MODULE_LICENSE("GPL v2");
636