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