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