hda_tegra.c 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 *
 * Implementation of primary ALSA driver code base for NVIDIA Tegra HDA.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <linux/clk.h>
#include <linux/clocksource.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/of_device.h>
#include <linux/slab.h>
#include <linux/time.h>
34
#include <linux/string.h>
35
#include <linux/pm_runtime.h>
36 37 38 39

#include <sound/core.h>
#include <sound/initval.h>

40
#include <sound/hda_codec.h>
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include "hda_controller.h"

/* Defines for Nvidia Tegra HDA support */
#define HDA_BAR0           0x8000

#define HDA_CFG_CMD        0x1004
#define HDA_CFG_BAR0       0x1010

#define HDA_ENABLE_IO_SPACE       (1 << 0)
#define HDA_ENABLE_MEM_SPACE      (1 << 1)
#define HDA_ENABLE_BUS_MASTER     (1 << 2)
#define HDA_ENABLE_SERR           (1 << 8)
#define HDA_DISABLE_INTR          (1 << 10)
#define HDA_BAR0_INIT_PROGRAM     0xFFFFFFFF
#define HDA_BAR0_FINAL_PROGRAM    (1 << 14)

/* IPFS */
#define HDA_IPFS_CONFIG           0x180
#define HDA_IPFS_EN_FPCI          0x1

#define HDA_IPFS_FPCI_BAR0        0x80
#define HDA_FPCI_BAR0_START       0x40

#define HDA_IPFS_INTR_MASK        0x188
#define HDA_IPFS_EN_INTR          (1 << 16)

/* max number of SDs */
#define NUM_CAPTURE_SD 1
#define NUM_PLAYBACK_SD 1

struct hda_tegra {
	struct azx chip;
	struct device *dev;
	struct clk *hda_clk;
	struct clk *hda2codec_2x_clk;
	struct clk *hda2hdmi_clk;
	void __iomem *regs;
78
	struct work_struct probe_work;
79 80
};

Arnd Bergmann's avatar
Arnd Bergmann committed
81
#ifdef CONFIG_PM
82 83 84 85
static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
module_param(power_save, bint, 0644);
MODULE_PARM_DESC(power_save,
		 "Automatic power-saving timeout (in seconds, 0 = disable).");
Arnd Bergmann's avatar
Arnd Bergmann committed
86
#else
87
#define power_save	0
Arnd Bergmann's avatar
Arnd Bergmann committed
88
#endif
89 90 91 92

/*
 * DMA page allocation ops.
 */
93
static int dma_alloc_pages(struct hdac_bus *bus, int type, size_t size,
94 95
			   struct snd_dma_buffer *buf)
{
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 102 103 104 105 106
{
	snd_dma_free_pages(buf);
}

/*
 * Register access ops. Tegra HDA register access is DWORD only.
 */
107
static void hda_tegra_writel(u32 value, u32 __iomem *addr)
108 109 110 111
{
	writel(value, addr);
}

112
static u32 hda_tegra_readl(u32 __iomem *addr)
113 114 115 116
{
	return readl(addr);
}

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 122 123 124 125 126 127 128
	u32 v;

	v = readl(dword_addr);
	v &= ~(0xffff << shift);
	v |= value << shift;
	writel(v, dword_addr);
}

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 134 135 136 137 138
	u32 v;

	v = readl(dword_addr);
	return (v >> shift) & 0xffff;
}

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 144 145 146 147 148 149 150
	u32 v;

	v = readl(dword_addr);
	v &= ~(0xff << shift);
	v |= value << shift;
	writel(v, dword_addr);
}

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 156 157 158 159 160
	u32 v;

	v = readl(dword_addr);
	return (v >> shift) & 0xff;
}

161
static const struct hdac_io_ops hda_tegra_io_ops = {
162 163 164 165 166 167 168 169
	.reg_writel = hda_tegra_writel,
	.reg_readl = hda_tegra_readl,
	.reg_writew = hda_tegra_writew,
	.reg_readw = hda_tegra_readw,
	.reg_writeb = hda_tegra_writeb,
	.reg_readb = hda_tegra_readb,
	.dma_alloc_pages = dma_alloc_pages,
	.dma_free_pages = dma_free_pages,
170 171
};

172
static const struct hda_controller_ops hda_tegra_ops; /* nothing special */
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

static void hda_tegra_init(struct hda_tegra *hda)
{
	u32 v;

	/* Enable PCI access */
	v = readl(hda->regs + HDA_IPFS_CONFIG);
	v |= HDA_IPFS_EN_FPCI;
	writel(v, hda->regs + HDA_IPFS_CONFIG);

	/* Enable MEM/IO space and bus master */
	v = readl(hda->regs + HDA_CFG_CMD);
	v &= ~HDA_DISABLE_INTR;
	v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE |
		HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR;
	writel(v, hda->regs + HDA_CFG_CMD);

	writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0);
	writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0);
	writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0);

	v = readl(hda->regs + HDA_IPFS_INTR_MASK);
	v |= HDA_IPFS_EN_INTR;
	writel(v, hda->regs + HDA_IPFS_INTR_MASK);
}

static int hda_tegra_enable_clocks(struct hda_tegra *data)
{
	int rc;

	rc = clk_prepare_enable(data->hda_clk);
	if (rc)
		return rc;
	rc = clk_prepare_enable(data->hda2codec_2x_clk);
	if (rc)
		goto disable_hda;
	rc = clk_prepare_enable(data->hda2hdmi_clk);
	if (rc)
		goto disable_codec_2x;

	return 0;

disable_codec_2x:
	clk_disable_unprepare(data->hda2codec_2x_clk);
disable_hda:
	clk_disable_unprepare(data->hda_clk);
	return rc;
}

222
#ifdef CONFIG_PM_SLEEP
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
static void hda_tegra_disable_clocks(struct hda_tegra *data)
{
	clk_disable_unprepare(data->hda2hdmi_clk);
	clk_disable_unprepare(data->hda2codec_2x_clk);
	clk_disable_unprepare(data->hda_clk);
}

/*
 * power management
 */
static int hda_tegra_suspend(struct device *dev)
{
	struct snd_card *card = dev_get_drvdata(dev);
	struct azx *chip = card->private_data;
	struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
238
	struct hdac_bus *bus = azx_bus(chip);
239 240 241 242

	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);

	azx_stop_chip(chip);
243
	synchronize_irq(bus->irq);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	azx_enter_link_reset(chip);
	hda_tegra_disable_clocks(hda);

	return 0;
}

static int hda_tegra_resume(struct device *dev)
{
	struct snd_card *card = dev_get_drvdata(dev);
	struct azx *chip = card->private_data;
	struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);

	hda_tegra_enable_clocks(hda);

	hda_tegra_init(hda);

	azx_init_chip(chip, 1);

	snd_power_change_state(card, SNDRV_CTL_POWER_D0);

	return 0;
}
#endif /* CONFIG_PM_SLEEP */

static const struct dev_pm_ops hda_tegra_pm = {
	SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
};

272 273 274 275 276 277 278 279
static int hda_tegra_dev_disconnect(struct snd_device *device)
{
	struct azx *chip = device->device_data;

	chip->bus.shutdown = 1;
	return 0;
}

280 281 282 283 284 285
/*
 * destructor
 */
static int hda_tegra_dev_free(struct snd_device *device)
{
	struct azx *chip = device->device_data;
286
	struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
287

288
	cancel_work_sync(&hda->probe_work);
289
	if (azx_bus(chip)->chip_init) {
290
		azx_stop_all_streams(chip);
291 292 293 294
		azx_stop_chip(chip);
	}

	azx_free_stream_pages(chip);
295
	azx_free_streams(chip);
296
	snd_hdac_bus_exit(azx_bus(chip));
297 298 299 300 301 302 303

	return 0;
}

static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
{
	struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
304
	struct hdac_bus *bus = azx_bus(chip);
305 306 307 308 309 310
	struct device *dev = hda->dev;
	struct resource *res;
	int err;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	hda->regs = devm_ioremap_resource(dev, res);
311 312
	if (IS_ERR(hda->regs))
		return PTR_ERR(hda->regs);
313

314 315
	bus->remap_addr = hda->regs + HDA_BAR0;
	bus->addr = res->start + HDA_BAR0;
316 317

	err = hda_tegra_enable_clocks(hda);
318 319
	if (err) {
		dev_err(dev, "failed to get enable clocks\n");
320
		return err;
321
	}
322 323 324 325 326 327

	hda_tegra_init(hda);

	return 0;
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
static int hda_tegra_init_clk(struct hda_tegra *hda)
{
	struct device *dev = hda->dev;

	hda->hda_clk = devm_clk_get(dev, "hda");
	if (IS_ERR(hda->hda_clk)) {
		dev_err(dev, "failed to get hda clock\n");
		return PTR_ERR(hda->hda_clk);
	}
	hda->hda2codec_2x_clk = devm_clk_get(dev, "hda2codec_2x");
	if (IS_ERR(hda->hda2codec_2x_clk)) {
		dev_err(dev, "failed to get hda2codec_2x clock\n");
		return PTR_ERR(hda->hda2codec_2x_clk);
	}
	hda->hda2hdmi_clk = devm_clk_get(dev, "hda2hdmi");
	if (IS_ERR(hda->hda2hdmi_clk)) {
		dev_err(dev, "failed to get hda2hdmi clock\n");
		return PTR_ERR(hda->hda2hdmi_clk);
	}

	return 0;
}

351 352
static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
{
353
	struct hdac_bus *bus = azx_bus(chip);
354 355 356 357
	struct snd_card *card = chip->card;
	int err;
	unsigned short gcap;
	int irq_id = platform_get_irq(pdev, 0);
358 359
	const char *sname;
	struct device_node *root;
360 361 362 363 364 365 366 367 368 369 370 371 372

	err = hda_tegra_init_chip(chip, pdev);
	if (err)
		return err;

	err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
			     IRQF_SHARED, KBUILD_MODNAME, chip);
	if (err) {
		dev_err(chip->card->dev,
			"unable to request IRQ %d, disabling device\n",
			irq_id);
		return err;
	}
373
	bus->irq = irq_id;
374

375
	synchronize_irq(bus->irq);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

	gcap = azx_readw(chip, GCAP);
	dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);

	/* read number of streams from GCAP register instead of using
	 * hardcoded value
	 */
	chip->capture_streams = (gcap >> 8) & 0x0f;
	chip->playback_streams = (gcap >> 12) & 0x0f;
	if (!chip->playback_streams && !chip->capture_streams) {
		/* gcap didn't give any info, switching to old method */
		chip->playback_streams = NUM_PLAYBACK_SD;
		chip->capture_streams = NUM_CAPTURE_SD;
	}
	chip->capture_index_offset = 0;
	chip->playback_index_offset = chip->capture_streams;
	chip->num_streams = chip->playback_streams + chip->capture_streams;

394 395
	/* initialize streams */
	err = azx_init_streams(chip);
396 397
	if (err < 0) {
		dev_err(card->dev, "failed to initialize streams: %d\n", err);
398
		return err;
399
	}
400

401
	err = azx_alloc_stream_pages(chip);
402 403 404
	if (err < 0) {
		dev_err(card->dev, "failed to allocate stream pages: %d\n",
			err);
405
		return err;
406
	}
407 408 409 410 411

	/* initialize chip */
	azx_init_chip(chip, 1);

	/* codec detection */
412
	if (!bus->codec_mask) {
413 414 415 416
		dev_err(card->dev, "no codecs found!\n");
		return -ENODEV;
	}

417
	/* driver name */
418
	strcpy(card->driver, "tegra-hda");
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

	root = of_find_node_by_path("/");
	sname = of_get_property(root, "compatible", NULL);
	of_node_put(root);
	if (!sname) {
		dev_err(card->dev,
			"failed to get compatible property from root node\n");
		return -ENODEV;
	}
	/* shortname for card */
	if (strlen(sname) > sizeof(card->shortname))
		dev_info(card->dev, "truncating shortname for card\n");
	strncpy(card->shortname, sname, sizeof(card->shortname));

	/* longname for card */
434 435
	snprintf(card->longname, sizeof(card->longname),
		 "%s at 0x%lx irq %i",
436
		 card->shortname, bus->addr, bus->irq);
437 438 439 440 441 442 443

	return 0;
}

/*
 * constructor
 */
444 445 446

static void hda_tegra_probe_work(struct work_struct *work);

447 448 449 450 451
static int hda_tegra_create(struct snd_card *card,
			    unsigned int driver_caps,
			    struct hda_tegra *hda)
{
	static struct snd_device_ops ops = {
452
		.dev_disconnect = hda_tegra_dev_disconnect,
453 454 455 456 457 458 459 460 461
		.dev_free = hda_tegra_dev_free,
	};
	struct azx *chip;
	int err;

	chip = &hda->chip;

	mutex_init(&chip->open_mutex);
	chip->card = card;
462
	chip->ops = &hda_tegra_ops;
463 464 465 466 467 468 469 470 471 472
	chip->driver_caps = driver_caps;
	chip->driver_type = driver_caps & 0xff;
	chip->dev_index = 0;
	INIT_LIST_HEAD(&chip->pcm_list);

	chip->codec_probe_mask = -1;

	chip->single_cmd = false;
	chip->snoop = true;

473 474
	INIT_WORK(&hda->probe_work, hda_tegra_probe_work);

475 476 477 478
	err = azx_bus_init(chip, NULL, &hda_tegra_io_ops);
	if (err < 0)
		return err;

479 480
	chip->bus.needs_damn_long_delay = 1;

481 482 483 484 485 486 487 488 489 490 491 492 493
	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
	if (err < 0) {
		dev_err(card->dev, "Error creating device\n");
		return err;
	}

	return 0;
}

static const struct of_device_id hda_tegra_match[] = {
	{ .compatible = "nvidia,tegra30-hda" },
	{},
};
494
MODULE_DEVICE_TABLE(of, hda_tegra_match);
495 496 497

static int hda_tegra_probe(struct platform_device *pdev)
{
498
	const unsigned int driver_flags = AZX_DCAPS_CORBRP_SELF_CLEAR;
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	struct snd_card *card;
	struct azx *chip;
	struct hda_tegra *hda;
	int err;

	hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
	if (!hda)
		return -ENOMEM;
	hda->dev = &pdev->dev;
	chip = &hda->chip;

	err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
			   THIS_MODULE, 0, &card);
	if (err < 0) {
		dev_err(&pdev->dev, "Error creating card!\n");
		return err;
	}

517 518 519 520
	err = hda_tegra_init_clk(hda);
	if (err < 0)
		goto out_free;

521
	err = hda_tegra_create(card, driver_flags, hda);
522 523 524 525 526
	if (err < 0)
		goto out_free;
	card->private_data = chip;

	dev_set_drvdata(&pdev->dev, card);
527 528 529 530 531

	pm_runtime_enable(hda->dev);
	if (!azx_has_pm_runtime(chip))
		pm_runtime_forbid(hda->dev);

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	schedule_work(&hda->probe_work);

	return 0;

out_free:
	snd_card_free(card);
	return err;
}

static void hda_tegra_probe_work(struct work_struct *work)
{
	struct hda_tegra *hda = container_of(work, struct hda_tegra, probe_work);
	struct azx *chip = &hda->chip;
	struct platform_device *pdev = to_platform_device(hda->dev);
	int err;
547

548
	pm_runtime_get_sync(hda->dev);
549 550 551 552 553
	err = hda_tegra_first_init(chip, pdev);
	if (err < 0)
		goto out_free;

	/* create codec instances */
554
	err = azx_probe_codecs(chip, 8);
555 556 557 558 559 560 561 562 563 564 565 566
	if (err < 0)
		goto out_free;

	err = azx_codec_configure(chip);
	if (err < 0)
		goto out_free;

	err = snd_card_register(chip->card);
	if (err < 0)
		goto out_free;

	chip->running = 1;
567
	snd_hda_set_power_save(&chip->bus, power_save * 1000);
568

569
 out_free:
570
	pm_runtime_put(hda->dev);
571
	return; /* no error return from async probe */
572 573 574 575
}

static int hda_tegra_remove(struct platform_device *pdev)
{
576 577 578 579 580 581
	int ret;

	ret = snd_card_free(dev_get_drvdata(&pdev->dev));
	pm_runtime_disable(&pdev->dev);

	return ret;
582 583
}

584 585 586 587 588 589 590 591 592 593 594 595
static void hda_tegra_shutdown(struct platform_device *pdev)
{
	struct snd_card *card = dev_get_drvdata(&pdev->dev);
	struct azx *chip;

	if (!card)
		return;
	chip = card->private_data;
	if (chip && chip->running)
		azx_stop_chip(chip);
}

596 597 598 599 600 601 602 603
static struct platform_driver tegra_platform_hda = {
	.driver = {
		.name = "tegra-hda",
		.pm = &hda_tegra_pm,
		.of_match_table = hda_tegra_match,
	},
	.probe = hda_tegra_probe,
	.remove = hda_tegra_remove,
604
	.shutdown = hda_tegra_shutdown,
605 606 607 608 609
};
module_platform_driver(tegra_platform_hda);

MODULE_DESCRIPTION("Tegra HDA bus driver");
MODULE_LICENSE("GPL v2");