xref: /openbmc/linux/sound/soc/intel/skylake/skl.c (revision ee1e4e3f)
1 /*
2  *  skl.c - Implementation of ASoC Intel SKL HD Audio driver
3  *
4  *  Copyright (C) 2014-2015 Intel Corp
5  *  Author: Jeeja KP <jeeja.kp@intel.com>
6  *
7  *  Derived mostly from Intel HDA driver with following copyrights:
8  *  Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
9  *                     PeiSen Hou <pshou@realtek.com.tw>
10  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; version 2 of the License.
15  *
16  *  This program is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22  */
23 
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/platform_device.h>
28 #include <linux/firmware.h>
29 #include <sound/pcm.h>
30 #include "../common/sst-acpi.h"
31 #include "skl.h"
32 
33 /*
34  * initialize the PCI registers
35  */
36 static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg,
37 			    unsigned char mask, unsigned char val)
38 {
39 	unsigned char data;
40 
41 	pci_read_config_byte(pci, reg, &data);
42 	data &= ~mask;
43 	data |= (val & mask);
44 	pci_write_config_byte(pci, reg, data);
45 }
46 
47 static void skl_init_pci(struct skl *skl)
48 {
49 	struct hdac_ext_bus *ebus = &skl->ebus;
50 
51 	/*
52 	 * Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
53 	 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
54 	 * Ensuring these bits are 0 clears playback static on some HD Audio
55 	 * codecs.
56 	 * The PCI register TCSEL is defined in the Intel manuals.
57 	 */
58 	dev_dbg(ebus_to_hbus(ebus)->dev, "Clearing TCSEL\n");
59 	skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
60 }
61 
62 /* called from IRQ */
63 static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
64 {
65 	snd_pcm_period_elapsed(hstr->substream);
66 }
67 
68 static irqreturn_t skl_interrupt(int irq, void *dev_id)
69 {
70 	struct hdac_ext_bus *ebus = dev_id;
71 	struct hdac_bus *bus = ebus_to_hbus(ebus);
72 	u32 status;
73 
74 	if (!pm_runtime_active(bus->dev))
75 		return IRQ_NONE;
76 
77 	spin_lock(&bus->reg_lock);
78 
79 	status = snd_hdac_chip_readl(bus, INTSTS);
80 	if (status == 0 || status == 0xffffffff) {
81 		spin_unlock(&bus->reg_lock);
82 		return IRQ_NONE;
83 	}
84 
85 	/* clear rirb int */
86 	status = snd_hdac_chip_readb(bus, RIRBSTS);
87 	if (status & RIRB_INT_MASK) {
88 		if (status & RIRB_INT_RESPONSE)
89 			snd_hdac_bus_update_rirb(bus);
90 		snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
91 	}
92 
93 	spin_unlock(&bus->reg_lock);
94 
95 	return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
96 }
97 
98 static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
99 {
100 	struct hdac_ext_bus *ebus = dev_id;
101 	struct hdac_bus *bus = ebus_to_hbus(ebus);
102 	u32 status;
103 
104 	status = snd_hdac_chip_readl(bus, INTSTS);
105 
106 	snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
107 
108 	return IRQ_HANDLED;
109 }
110 
111 static int skl_acquire_irq(struct hdac_ext_bus *ebus, int do_disconnect)
112 {
113 	struct skl *skl = ebus_to_skl(ebus);
114 	struct hdac_bus *bus = ebus_to_hbus(ebus);
115 	int ret;
116 
117 	ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
118 			skl_threaded_handler,
119 			IRQF_SHARED,
120 			KBUILD_MODNAME, ebus);
121 	if (ret) {
122 		dev_err(bus->dev,
123 			"unable to grab IRQ %d, disabling device\n",
124 			skl->pci->irq);
125 		return ret;
126 	}
127 
128 	bus->irq = skl->pci->irq;
129 	pci_intx(skl->pci, 1);
130 
131 	return 0;
132 }
133 
134 #ifdef CONFIG_PM
135 static int _skl_suspend(struct hdac_ext_bus *ebus)
136 {
137 	struct skl *skl = ebus_to_skl(ebus);
138 	struct hdac_bus *bus = ebus_to_hbus(ebus);
139 	int ret;
140 
141 	snd_hdac_ext_bus_link_power_down_all(ebus);
142 
143 	ret = skl_suspend_dsp(skl);
144 	if (ret < 0)
145 		return ret;
146 
147 	snd_hdac_bus_stop_chip(bus);
148 	snd_hdac_bus_enter_link_reset(bus);
149 
150 	return 0;
151 }
152 
153 static int _skl_resume(struct hdac_ext_bus *ebus)
154 {
155 	struct skl *skl = ebus_to_skl(ebus);
156 	struct hdac_bus *bus = ebus_to_hbus(ebus);
157 
158 	skl_init_pci(skl);
159 	snd_hdac_bus_init_chip(bus, true);
160 
161 	return skl_resume_dsp(skl);
162 }
163 #endif
164 
165 #ifdef CONFIG_PM_SLEEP
166 /*
167  * power management
168  */
169 static int skl_suspend(struct device *dev)
170 {
171 	struct pci_dev *pci = to_pci_dev(dev);
172 	struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
173 	struct skl *skl  = ebus_to_skl(ebus);
174 
175 	/*
176 	 * Do not suspend if streams which are marked ignore suspend are
177 	 * running, we need to save the state for these and continue
178 	 */
179 	if (skl->supend_active) {
180 		pci_save_state(pci);
181 		pci_disable_device(pci);
182 		return 0;
183 	} else {
184 		return _skl_suspend(ebus);
185 	}
186 }
187 
188 static int skl_resume(struct device *dev)
189 {
190 	struct pci_dev *pci = to_pci_dev(dev);
191 	struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
192 	struct skl *skl  = ebus_to_skl(ebus);
193 	int ret;
194 
195 	/*
196 	 * resume only when we are not in suspend active, otherwise need to
197 	 * restore the device
198 	 */
199 	if (skl->supend_active) {
200 		pci_restore_state(pci);
201 		ret = pci_enable_device(pci);
202 	} else {
203 		ret = _skl_resume(ebus);
204 	}
205 
206 	return ret;
207 }
208 #endif /* CONFIG_PM_SLEEP */
209 
210 #ifdef CONFIG_PM
211 static int skl_runtime_suspend(struct device *dev)
212 {
213 	struct pci_dev *pci = to_pci_dev(dev);
214 	struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
215 	struct hdac_bus *bus = ebus_to_hbus(ebus);
216 
217 	dev_dbg(bus->dev, "in %s\n", __func__);
218 
219 	return _skl_suspend(ebus);
220 }
221 
222 static int skl_runtime_resume(struct device *dev)
223 {
224 	struct pci_dev *pci = to_pci_dev(dev);
225 	struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
226 	struct hdac_bus *bus = ebus_to_hbus(ebus);
227 
228 	dev_dbg(bus->dev, "in %s\n", __func__);
229 
230 	return _skl_resume(ebus);
231 }
232 #endif /* CONFIG_PM */
233 
234 static const struct dev_pm_ops skl_pm = {
235 	SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
236 	SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
237 };
238 
239 /*
240  * destructor
241  */
242 static int skl_free(struct hdac_ext_bus *ebus)
243 {
244 	struct skl *skl  = ebus_to_skl(ebus);
245 	struct hdac_bus *bus = ebus_to_hbus(ebus);
246 
247 	skl->init_failed = 1; /* to be sure */
248 
249 	snd_hdac_ext_stop_streams(ebus);
250 
251 	if (bus->irq >= 0)
252 		free_irq(bus->irq, (void *)bus);
253 	if (bus->remap_addr)
254 		iounmap(bus->remap_addr);
255 
256 	snd_hdac_bus_free_stream_pages(bus);
257 	snd_hdac_stream_free_all(ebus);
258 	snd_hdac_link_free_all(ebus);
259 	pci_release_regions(skl->pci);
260 	pci_disable_device(skl->pci);
261 
262 	snd_hdac_ext_bus_exit(ebus);
263 
264 	return 0;
265 }
266 
267 static int skl_machine_device_register(struct skl *skl, void *driver_data)
268 {
269 	struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
270 	struct platform_device *pdev;
271 	struct sst_acpi_mach *mach = driver_data;
272 	int ret;
273 
274 	mach = sst_acpi_find_machine(mach);
275 	if (mach == NULL) {
276 		dev_err(bus->dev, "No matching machine driver found\n");
277 		return -ENODEV;
278 	}
279 	skl->fw_name = mach->fw_filename;
280 
281 	pdev = platform_device_alloc(mach->drv_name, -1);
282 	if (pdev == NULL) {
283 		dev_err(bus->dev, "platform device alloc failed\n");
284 		return -EIO;
285 	}
286 
287 	ret = platform_device_add(pdev);
288 	if (ret) {
289 		dev_err(bus->dev, "failed to add machine device\n");
290 		platform_device_put(pdev);
291 		return -EIO;
292 	}
293 	skl->i2s_dev = pdev;
294 
295 	return 0;
296 }
297 
298 static void skl_machine_device_unregister(struct skl *skl)
299 {
300 	if (skl->i2s_dev)
301 		platform_device_unregister(skl->i2s_dev);
302 }
303 
304 static int skl_dmic_device_register(struct skl *skl)
305 {
306 	struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
307 	struct platform_device *pdev;
308 	int ret;
309 
310 	/* SKL has one dmic port, so allocate dmic device for this */
311 	pdev = platform_device_alloc("dmic-codec", -1);
312 	if (!pdev) {
313 		dev_err(bus->dev, "failed to allocate dmic device\n");
314 		return -ENOMEM;
315 	}
316 
317 	ret = platform_device_add(pdev);
318 	if (ret) {
319 		dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
320 		platform_device_put(pdev);
321 		return ret;
322 	}
323 	skl->dmic_dev = pdev;
324 
325 	return 0;
326 }
327 
328 static void skl_dmic_device_unregister(struct skl *skl)
329 {
330 	if (skl->dmic_dev)
331 		platform_device_unregister(skl->dmic_dev);
332 }
333 
334 /*
335  * Probe the given codec address
336  */
337 static int probe_codec(struct hdac_ext_bus *ebus, int addr)
338 {
339 	struct hdac_bus *bus = ebus_to_hbus(ebus);
340 	unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
341 		(AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
342 	unsigned int res;
343 
344 	mutex_lock(&bus->cmd_mutex);
345 	snd_hdac_bus_send_cmd(bus, cmd);
346 	snd_hdac_bus_get_response(bus, addr, &res);
347 	mutex_unlock(&bus->cmd_mutex);
348 	if (res == -1)
349 		return -EIO;
350 	dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
351 
352 	return snd_hdac_ext_bus_device_init(ebus, addr);
353 }
354 
355 /* Codec initialization */
356 static int skl_codec_create(struct hdac_ext_bus *ebus)
357 {
358 	struct hdac_bus *bus = ebus_to_hbus(ebus);
359 	int c, max_slots;
360 
361 	max_slots = HDA_MAX_CODECS;
362 
363 	/* First try to probe all given codec slots */
364 	for (c = 0; c < max_slots; c++) {
365 		if ((bus->codec_mask & (1 << c))) {
366 			if (probe_codec(ebus, c) < 0) {
367 				/*
368 				 * Some BIOSen give you wrong codec addresses
369 				 * that don't exist
370 				 */
371 				dev_warn(bus->dev,
372 					 "Codec #%d probe error; disabling it...\n", c);
373 				bus->codec_mask &= ~(1 << c);
374 				/*
375 				 * More badly, accessing to a non-existing
376 				 * codec often screws up the controller bus,
377 				 * and disturbs the further communications.
378 				 * Thus if an error occurs during probing,
379 				 * better to reset the controller bus to get
380 				 * back to the sanity state.
381 				 */
382 				snd_hdac_bus_stop_chip(bus);
383 				snd_hdac_bus_init_chip(bus, true);
384 			}
385 		}
386 	}
387 
388 	return 0;
389 }
390 
391 static const struct hdac_bus_ops bus_core_ops = {
392 	.command = snd_hdac_bus_send_cmd,
393 	.get_response = snd_hdac_bus_get_response,
394 };
395 
396 /*
397  * constructor
398  */
399 static int skl_create(struct pci_dev *pci,
400 		      const struct hdac_io_ops *io_ops,
401 		      struct skl **rskl)
402 {
403 	struct skl *skl;
404 	struct hdac_ext_bus *ebus;
405 
406 	int err;
407 
408 	*rskl = NULL;
409 
410 	err = pci_enable_device(pci);
411 	if (err < 0)
412 		return err;
413 
414 	skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
415 	if (!skl) {
416 		pci_disable_device(pci);
417 		return -ENOMEM;
418 	}
419 	ebus = &skl->ebus;
420 	snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
421 	ebus->bus.use_posbuf = 1;
422 	skl->pci = pci;
423 
424 	ebus->bus.bdl_pos_adj = 0;
425 
426 	*rskl = skl;
427 
428 	return 0;
429 }
430 
431 static int skl_first_init(struct hdac_ext_bus *ebus)
432 {
433 	struct skl *skl = ebus_to_skl(ebus);
434 	struct hdac_bus *bus = ebus_to_hbus(ebus);
435 	struct pci_dev *pci = skl->pci;
436 	int err;
437 	unsigned short gcap;
438 	int cp_streams, pb_streams, start_idx;
439 
440 	err = pci_request_regions(pci, "Skylake HD audio");
441 	if (err < 0)
442 		return err;
443 
444 	bus->addr = pci_resource_start(pci, 0);
445 	bus->remap_addr = pci_ioremap_bar(pci, 0);
446 	if (bus->remap_addr == NULL) {
447 		dev_err(bus->dev, "ioremap error\n");
448 		return -ENXIO;
449 	}
450 
451 	snd_hdac_ext_bus_parse_capabilities(ebus);
452 
453 	if (skl_acquire_irq(ebus, 0) < 0)
454 		return -EBUSY;
455 
456 	pci_set_master(pci);
457 	synchronize_irq(bus->irq);
458 
459 	gcap = snd_hdac_chip_readw(bus, GCAP);
460 	dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
461 
462 	/* allow 64bit DMA address if supported by H/W */
463 	if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
464 		dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
465 	} else {
466 		dma_set_mask(bus->dev, DMA_BIT_MASK(32));
467 		dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
468 	}
469 
470 	/* read number of streams from GCAP register */
471 	cp_streams = (gcap >> 8) & 0x0f;
472 	pb_streams = (gcap >> 12) & 0x0f;
473 
474 	if (!pb_streams && !cp_streams)
475 		return -EIO;
476 
477 	ebus->num_streams = cp_streams + pb_streams;
478 
479 	/* initialize streams */
480 	snd_hdac_ext_stream_init_all
481 		(ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
482 	start_idx = cp_streams;
483 	snd_hdac_ext_stream_init_all
484 		(ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
485 
486 	err = snd_hdac_bus_alloc_stream_pages(bus);
487 	if (err < 0)
488 		return err;
489 
490 	/* initialize chip */
491 	skl_init_pci(skl);
492 
493 	snd_hdac_bus_init_chip(bus, true);
494 
495 	/* codec detection */
496 	if (!bus->codec_mask) {
497 		dev_info(bus->dev, "no hda codecs found!\n");
498 	}
499 
500 	return 0;
501 }
502 
503 static int skl_probe(struct pci_dev *pci,
504 		     const struct pci_device_id *pci_id)
505 {
506 	struct skl *skl;
507 	struct hdac_ext_bus *ebus = NULL;
508 	struct hdac_bus *bus = NULL;
509 	int err;
510 
511 	/* we use ext core ops, so provide NULL for ops here */
512 	err = skl_create(pci, NULL, &skl);
513 	if (err < 0)
514 		return err;
515 
516 	ebus = &skl->ebus;
517 	bus = ebus_to_hbus(ebus);
518 
519 	err = skl_first_init(ebus);
520 	if (err < 0)
521 		goto out_free;
522 
523 	skl->nhlt = skl_nhlt_init(bus->dev);
524 
525 	if (skl->nhlt == NULL)
526 		goto out_free;
527 
528 	pci_set_drvdata(skl->pci, ebus);
529 
530 	/* check if dsp is there */
531 	if (ebus->ppcap) {
532 		err = skl_machine_device_register(skl,
533 				  (void *)pci_id->driver_data);
534 		if (err < 0)
535 			goto out_free;
536 
537 		err = skl_init_dsp(skl);
538 		if (err < 0) {
539 			dev_dbg(bus->dev, "error failed to register dsp\n");
540 			goto out_mach_free;
541 		}
542 	}
543 	if (ebus->mlcap)
544 		snd_hdac_ext_bus_get_ml_capabilities(ebus);
545 
546 	/* create device for soc dmic */
547 	err = skl_dmic_device_register(skl);
548 	if (err < 0)
549 		goto out_dsp_free;
550 
551 	/* register platform dai and controls */
552 	err = skl_platform_register(bus->dev);
553 	if (err < 0)
554 		goto out_dmic_free;
555 
556 	/* create codec instances */
557 	err = skl_codec_create(ebus);
558 	if (err < 0)
559 		goto out_unregister;
560 
561 	/*configure PM */
562 	pm_runtime_set_autosuspend_delay(bus->dev, SKL_SUSPEND_DELAY);
563 	pm_runtime_use_autosuspend(bus->dev);
564 	pm_runtime_put_noidle(bus->dev);
565 	pm_runtime_allow(bus->dev);
566 
567 	return 0;
568 
569 out_unregister:
570 	skl_platform_unregister(bus->dev);
571 out_dmic_free:
572 	skl_dmic_device_unregister(skl);
573 out_dsp_free:
574 	skl_free_dsp(skl);
575 out_mach_free:
576 	skl_machine_device_unregister(skl);
577 out_free:
578 	skl->init_failed = 1;
579 	skl_free(ebus);
580 
581 	return err;
582 }
583 
584 static void skl_remove(struct pci_dev *pci)
585 {
586 	struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
587 	struct skl *skl = ebus_to_skl(ebus);
588 
589 	if (skl->tplg)
590 		release_firmware(skl->tplg);
591 
592 	if (pci_dev_run_wake(pci))
593 		pm_runtime_get_noresume(&pci->dev);
594 	pci_dev_put(pci);
595 	skl_platform_unregister(&pci->dev);
596 	skl_free_dsp(skl);
597 	skl_machine_device_unregister(skl);
598 	skl_dmic_device_unregister(skl);
599 	skl_free(ebus);
600 	dev_set_drvdata(&pci->dev, NULL);
601 }
602 
603 static struct sst_acpi_mach sst_skl_devdata[] = {
604 	{ "INT343A", "skl_alc286s_i2s", "intel/dsp_fw_release.bin", NULL, NULL, NULL },
605 	{ "INT343B", "skl_nau88l25_ssm4567_i2s", "intel/dsp_fw_release.bin",
606 				NULL, NULL, NULL },
607 	{ "MX98357A", "skl_nau88l25_max98357a_i2s", "intel/dsp_fw_release.bin",
608 				NULL, NULL, NULL },
609 	{}
610 };
611 
612 /* PCI IDs */
613 static const struct pci_device_id skl_ids[] = {
614 	/* Sunrise Point-LP */
615 	{ PCI_DEVICE(0x8086, 0x9d70),
616 		.driver_data = (unsigned long)&sst_skl_devdata},
617 	{ 0, }
618 };
619 MODULE_DEVICE_TABLE(pci, skl_ids);
620 
621 /* pci_driver definition */
622 static struct pci_driver skl_driver = {
623 	.name = KBUILD_MODNAME,
624 	.id_table = skl_ids,
625 	.probe = skl_probe,
626 	.remove = skl_remove,
627 	.driver = {
628 		.pm = &skl_pm,
629 	},
630 };
631 module_pci_driver(skl_driver);
632 
633 MODULE_LICENSE("GPL v2");
634 MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");
635