blob: cf69dafa91c0c9d1658f3d0be9a890bd8f1d9081 [file] [log] [blame]
Dylan Reid3c320f32014-05-19 19:18:27 -07001/*
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>
Dylan Reid3c320f32014-05-19 19:18:27 -070032#include <linux/slab.h>
33#include <linux/time.h>
34
35#include <sound/core.h>
36#include <sound/initval.h>
37
38#include "hda_codec.h"
39#include "hda_controller.h"
40#include "hda_priv.h"
41
42/* Defines for Nvidia Tegra HDA support */
43#define HDA_BAR0 0x8000
44
45#define HDA_CFG_CMD 0x1004
46#define HDA_CFG_BAR0 0x1010
47
48#define HDA_ENABLE_IO_SPACE (1 << 0)
49#define HDA_ENABLE_MEM_SPACE (1 << 1)
50#define HDA_ENABLE_BUS_MASTER (1 << 2)
51#define HDA_ENABLE_SERR (1 << 8)
52#define HDA_DISABLE_INTR (1 << 10)
53#define HDA_BAR0_INIT_PROGRAM 0xFFFFFFFF
54#define HDA_BAR0_FINAL_PROGRAM (1 << 14)
55
56/* IPFS */
57#define HDA_IPFS_CONFIG 0x180
58#define HDA_IPFS_EN_FPCI 0x1
59
60#define HDA_IPFS_FPCI_BAR0 0x80
61#define HDA_FPCI_BAR0_START 0x40
62
63#define HDA_IPFS_INTR_MASK 0x188
64#define HDA_IPFS_EN_INTR (1 << 16)
65
66/* max number of SDs */
67#define NUM_CAPTURE_SD 1
68#define NUM_PLAYBACK_SD 1
69
70struct hda_tegra {
71 struct azx chip;
72 struct device *dev;
73 struct clk *hda_clk;
74 struct clk *hda2codec_2x_clk;
75 struct clk *hda2hdmi_clk;
76 void __iomem *regs;
77};
78
Arnd Bergmann16c23952014-05-26 21:15:20 +020079#ifdef CONFIG_PM
Dylan Reid3c320f32014-05-19 19:18:27 -070080static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
81module_param(power_save, bint, 0644);
82MODULE_PARM_DESC(power_save,
83 "Automatic power-saving timeout (in seconds, 0 = disable).");
Arnd Bergmann16c23952014-05-26 21:15:20 +020084#else
85static int power_save = 0;
86#endif
Dylan Reid3c320f32014-05-19 19:18:27 -070087
88/*
89 * DMA page allocation ops.
90 */
91static int dma_alloc_pages(struct azx *chip, int type, size_t size,
92 struct snd_dma_buffer *buf)
93{
94 return snd_dma_alloc_pages(type, chip->card->dev, size, buf);
95}
96
97static void dma_free_pages(struct azx *chip, struct snd_dma_buffer *buf)
98{
99 snd_dma_free_pages(buf);
100}
101
102static int substream_alloc_pages(struct azx *chip,
103 struct snd_pcm_substream *substream,
104 size_t size)
105{
106 struct azx_dev *azx_dev = get_azx_dev(substream);
107
108 azx_dev->bufsize = 0;
109 azx_dev->period_bytes = 0;
110 azx_dev->format_val = 0;
111 return snd_pcm_lib_malloc_pages(substream, size);
112}
113
114static int substream_free_pages(struct azx *chip,
115 struct snd_pcm_substream *substream)
116{
117 return snd_pcm_lib_free_pages(substream);
118}
119
120/*
121 * Register access ops. Tegra HDA register access is DWORD only.
122 */
123static void hda_tegra_writel(u32 value, u32 *addr)
124{
125 writel(value, addr);
126}
127
128static u32 hda_tegra_readl(u32 *addr)
129{
130 return readl(addr);
131}
132
133static void hda_tegra_writew(u16 value, u16 *addr)
134{
135 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
136 void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
137 u32 v;
138
139 v = readl(dword_addr);
140 v &= ~(0xffff << shift);
141 v |= value << shift;
142 writel(v, dword_addr);
143}
144
145static u16 hda_tegra_readw(u16 *addr)
146{
147 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
148 void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
149 u32 v;
150
151 v = readl(dword_addr);
152 return (v >> shift) & 0xffff;
153}
154
155static void hda_tegra_writeb(u8 value, u8 *addr)
156{
157 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
158 void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
159 u32 v;
160
161 v = readl(dword_addr);
162 v &= ~(0xff << shift);
163 v |= value << shift;
164 writel(v, dword_addr);
165}
166
167static u8 hda_tegra_readb(u8 *addr)
168{
169 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
170 void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
171 u32 v;
172
173 v = readl(dword_addr);
174 return (v >> shift) & 0xff;
175}
176
177static const struct hda_controller_ops hda_tegra_ops = {
178 .reg_writel = hda_tegra_writel,
179 .reg_readl = hda_tegra_readl,
180 .reg_writew = hda_tegra_writew,
181 .reg_readw = hda_tegra_readw,
182 .reg_writeb = hda_tegra_writeb,
183 .reg_readb = hda_tegra_readb,
184 .dma_alloc_pages = dma_alloc_pages,
185 .dma_free_pages = dma_free_pages,
186 .substream_alloc_pages = substream_alloc_pages,
187 .substream_free_pages = substream_free_pages,
188};
189
190static void hda_tegra_init(struct hda_tegra *hda)
191{
192 u32 v;
193
194 /* Enable PCI access */
195 v = readl(hda->regs + HDA_IPFS_CONFIG);
196 v |= HDA_IPFS_EN_FPCI;
197 writel(v, hda->regs + HDA_IPFS_CONFIG);
198
199 /* Enable MEM/IO space and bus master */
200 v = readl(hda->regs + HDA_CFG_CMD);
201 v &= ~HDA_DISABLE_INTR;
202 v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE |
203 HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR;
204 writel(v, hda->regs + HDA_CFG_CMD);
205
206 writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0);
207 writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0);
208 writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0);
209
210 v = readl(hda->regs + HDA_IPFS_INTR_MASK);
211 v |= HDA_IPFS_EN_INTR;
212 writel(v, hda->regs + HDA_IPFS_INTR_MASK);
213}
214
215static int hda_tegra_enable_clocks(struct hda_tegra *data)
216{
217 int rc;
218
219 rc = clk_prepare_enable(data->hda_clk);
220 if (rc)
221 return rc;
222 rc = clk_prepare_enable(data->hda2codec_2x_clk);
223 if (rc)
224 goto disable_hda;
225 rc = clk_prepare_enable(data->hda2hdmi_clk);
226 if (rc)
227 goto disable_codec_2x;
228
229 return 0;
230
231disable_codec_2x:
232 clk_disable_unprepare(data->hda2codec_2x_clk);
233disable_hda:
234 clk_disable_unprepare(data->hda_clk);
235 return rc;
236}
237
238static void hda_tegra_disable_clocks(struct hda_tegra *data)
239{
240 clk_disable_unprepare(data->hda2hdmi_clk);
241 clk_disable_unprepare(data->hda2codec_2x_clk);
242 clk_disable_unprepare(data->hda_clk);
243}
244
245#ifdef CONFIG_PM_SLEEP
246/*
247 * power management
248 */
249static int hda_tegra_suspend(struct device *dev)
250{
251 struct snd_card *card = dev_get_drvdata(dev);
252 struct azx *chip = card->private_data;
253 struct azx_pcm *p;
254 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
255
256 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
257 list_for_each_entry(p, &chip->pcm_list, list)
258 snd_pcm_suspend_all(p->pcm);
259 if (chip->initialized)
260 snd_hda_suspend(chip->bus);
261
262 azx_stop_chip(chip);
263 azx_enter_link_reset(chip);
264 hda_tegra_disable_clocks(hda);
265
266 return 0;
267}
268
269static int hda_tegra_resume(struct device *dev)
270{
271 struct snd_card *card = dev_get_drvdata(dev);
272 struct azx *chip = card->private_data;
273 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
274 int status;
275
276 hda_tegra_enable_clocks(hda);
277
278 /* Read STATESTS before controller reset */
279 status = azx_readw(chip, STATESTS);
280
281 hda_tegra_init(hda);
282
283 azx_init_chip(chip, 1);
284
285 snd_hda_resume(chip->bus);
286 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
287
288 return 0;
289}
290#endif /* CONFIG_PM_SLEEP */
291
292static const struct dev_pm_ops hda_tegra_pm = {
293 SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
294};
295
296/*
Dylan Reid3c320f32014-05-19 19:18:27 -0700297 * destructor
298 */
299static int hda_tegra_dev_free(struct snd_device *device)
300{
301 int i;
302 struct azx *chip = device->device_data;
303
Takashi Iwai703c7592014-06-26 17:24:45 +0200304 azx_notifier_unregister(chip);
Dylan Reid3c320f32014-05-19 19:18:27 -0700305
306 if (chip->initialized) {
307 for (i = 0; i < chip->num_streams; i++)
308 azx_stream_stop(chip, &chip->azx_dev[i]);
309 azx_stop_chip(chip);
310 }
311
312 azx_free_stream_pages(chip);
313
314 return 0;
315}
316
317static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
318{
319 struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
320 struct device *dev = hda->dev;
321 struct resource *res;
322 int err;
323
324 hda->hda_clk = devm_clk_get(dev, "hda");
325 if (IS_ERR(hda->hda_clk))
326 return PTR_ERR(hda->hda_clk);
327 hda->hda2codec_2x_clk = devm_clk_get(dev, "hda2codec_2x");
328 if (IS_ERR(hda->hda2codec_2x_clk))
329 return PTR_ERR(hda->hda2codec_2x_clk);
330 hda->hda2hdmi_clk = devm_clk_get(dev, "hda2hdmi");
331 if (IS_ERR(hda->hda2hdmi_clk))
332 return PTR_ERR(hda->hda2hdmi_clk);
333
334 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
335 hda->regs = devm_ioremap_resource(dev, res);
336 if (IS_ERR(chip->remap_addr))
337 return PTR_ERR(chip->remap_addr);
338
339 chip->remap_addr = hda->regs + HDA_BAR0;
340 chip->addr = res->start + HDA_BAR0;
341
342 err = hda_tegra_enable_clocks(hda);
343 if (err)
344 return err;
345
346 hda_tegra_init(hda);
347
348 return 0;
349}
350
351/*
352 * The codecs were powered up in snd_hda_codec_new().
353 * Now all initialization done, so turn them down if possible
354 */
355static void power_down_all_codecs(struct azx *chip)
356{
357 struct hda_codec *codec;
358 list_for_each_entry(codec, &chip->bus->codec_list, list)
359 snd_hda_power_down(codec);
360}
361
362static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
363{
364 struct snd_card *card = chip->card;
365 int err;
366 unsigned short gcap;
367 int irq_id = platform_get_irq(pdev, 0);
368
369 err = hda_tegra_init_chip(chip, pdev);
370 if (err)
371 return err;
372
373 err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
374 IRQF_SHARED, KBUILD_MODNAME, chip);
375 if (err) {
376 dev_err(chip->card->dev,
377 "unable to request IRQ %d, disabling device\n",
378 irq_id);
379 return err;
380 }
381 chip->irq = irq_id;
382
383 synchronize_irq(chip->irq);
384
385 gcap = azx_readw(chip, GCAP);
386 dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
387
388 /* read number of streams from GCAP register instead of using
389 * hardcoded value
390 */
391 chip->capture_streams = (gcap >> 8) & 0x0f;
392 chip->playback_streams = (gcap >> 12) & 0x0f;
393 if (!chip->playback_streams && !chip->capture_streams) {
394 /* gcap didn't give any info, switching to old method */
395 chip->playback_streams = NUM_PLAYBACK_SD;
396 chip->capture_streams = NUM_CAPTURE_SD;
397 }
398 chip->capture_index_offset = 0;
399 chip->playback_index_offset = chip->capture_streams;
400 chip->num_streams = chip->playback_streams + chip->capture_streams;
401 chip->azx_dev = devm_kcalloc(card->dev, chip->num_streams,
402 sizeof(*chip->azx_dev), GFP_KERNEL);
403 if (!chip->azx_dev)
404 return -ENOMEM;
405
406 err = azx_alloc_stream_pages(chip);
407 if (err < 0)
408 return err;
409
410 /* initialize streams */
411 azx_init_stream(chip);
412
413 /* initialize chip */
414 azx_init_chip(chip, 1);
415
416 /* codec detection */
417 if (!chip->codec_mask) {
418 dev_err(card->dev, "no codecs found!\n");
419 return -ENODEV;
420 }
421
422 strcpy(card->driver, "tegra-hda");
423 strcpy(card->shortname, "tegra-hda");
424 snprintf(card->longname, sizeof(card->longname),
425 "%s at 0x%lx irq %i",
426 card->shortname, chip->addr, chip->irq);
427
428 return 0;
429}
430
431/*
432 * constructor
433 */
434static int hda_tegra_create(struct snd_card *card,
435 unsigned int driver_caps,
436 const struct hda_controller_ops *hda_ops,
437 struct hda_tegra *hda)
438{
439 static struct snd_device_ops ops = {
440 .dev_free = hda_tegra_dev_free,
441 };
442 struct azx *chip;
443 int err;
444
445 chip = &hda->chip;
446
447 spin_lock_init(&chip->reg_lock);
448 mutex_init(&chip->open_mutex);
449 chip->card = card;
450 chip->ops = hda_ops;
451 chip->irq = -1;
452 chip->driver_caps = driver_caps;
453 chip->driver_type = driver_caps & 0xff;
454 chip->dev_index = 0;
455 INIT_LIST_HEAD(&chip->pcm_list);
456 INIT_LIST_HEAD(&chip->list);
457
Dylan Reid3c320f32014-05-19 19:18:27 -0700458 chip->codec_probe_mask = -1;
459
460 chip->single_cmd = false;
461 chip->snoop = true;
462
463 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
464 if (err < 0) {
465 dev_err(card->dev, "Error creating device\n");
466 return err;
467 }
468
469 return 0;
470}
471
472static const struct of_device_id hda_tegra_match[] = {
473 { .compatible = "nvidia,tegra30-hda" },
474 {},
475};
Dylan Reidf73387c2014-05-20 11:26:12 -0700476MODULE_DEVICE_TABLE(of, hda_tegra_match);
Dylan Reid3c320f32014-05-19 19:18:27 -0700477
478static int hda_tegra_probe(struct platform_device *pdev)
479{
480 struct snd_card *card;
481 struct azx *chip;
482 struct hda_tegra *hda;
483 int err;
484 const unsigned int driver_flags = AZX_DCAPS_RIRB_DELAY;
485
486 hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
487 if (!hda)
488 return -ENOMEM;
489 hda->dev = &pdev->dev;
490 chip = &hda->chip;
491
492 err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
493 THIS_MODULE, 0, &card);
494 if (err < 0) {
495 dev_err(&pdev->dev, "Error creating card!\n");
496 return err;
497 }
498
499 err = hda_tegra_create(card, driver_flags, &hda_tegra_ops, hda);
500 if (err < 0)
501 goto out_free;
502 card->private_data = chip;
503
504 dev_set_drvdata(&pdev->dev, card);
505
506 err = hda_tegra_first_init(chip, pdev);
507 if (err < 0)
508 goto out_free;
509
510 /* create codec instances */
511 err = azx_codec_create(chip, NULL, 0, &power_save);
512 if (err < 0)
513 goto out_free;
514
515 err = azx_codec_configure(chip);
516 if (err < 0)
517 goto out_free;
518
519 /* create PCM streams */
520 err = snd_hda_build_pcms(chip->bus);
521 if (err < 0)
522 goto out_free;
523
524 /* create mixer controls */
525 err = azx_mixer_create(chip);
526 if (err < 0)
527 goto out_free;
528
529 err = snd_card_register(chip->card);
530 if (err < 0)
531 goto out_free;
532
533 chip->running = 1;
534 power_down_all_codecs(chip);
Takashi Iwai703c7592014-06-26 17:24:45 +0200535 azx_notifier_register(chip);
Dylan Reid3c320f32014-05-19 19:18:27 -0700536
537 return 0;
538
539out_free:
540 snd_card_free(card);
541 return err;
542}
543
544static int hda_tegra_remove(struct platform_device *pdev)
545{
546 return snd_card_free(dev_get_drvdata(&pdev->dev));
547}
548
549static struct platform_driver tegra_platform_hda = {
550 .driver = {
551 .name = "tegra-hda",
552 .pm = &hda_tegra_pm,
553 .of_match_table = hda_tegra_match,
554 },
555 .probe = hda_tegra_probe,
556 .remove = hda_tegra_remove,
557};
558module_platform_driver(tegra_platform_hda);
559
560MODULE_DESCRIPTION("Tegra HDA bus driver");
561MODULE_LICENSE("GPL v2");