xref: /openbmc/linux/sound/soc/intel/skylake/skl.c (revision cc18c5fd)
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 	/* enable controller wake up event */
195 	snd_hdac_chip_updatew(bus, WAKEEN, 0, STATESTS_INT_MASK);
196 
197 	return _skl_suspend(ebus);
198 }
199 
200 static int skl_runtime_resume(struct device *dev)
201 {
202 	struct pci_dev *pci = to_pci_dev(dev);
203 	struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
204 	struct hdac_bus *bus = ebus_to_hbus(ebus);
205 	struct skl *skl = ebus_to_skl(ebus);
206 	int status;
207 
208 	dev_dbg(bus->dev, "in %s\n", __func__);
209 
210 	/* Read STATESTS before controller reset */
211 	status = snd_hdac_chip_readw(bus, STATESTS);
212 
213 	skl_init_pci(skl);
214 	snd_hdac_bus_init_chip(bus, true);
215 	/* disable controller Wake Up event */
216 	snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, 0);
217 
218 	return _skl_resume(ebus);
219 }
220 #endif /* CONFIG_PM */
221 
222 static const struct dev_pm_ops skl_pm = {
223 	SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
224 	SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
225 };
226 
227 /*
228  * destructor
229  */
230 static int skl_free(struct hdac_ext_bus *ebus)
231 {
232 	struct skl *skl  = ebus_to_skl(ebus);
233 	struct hdac_bus *bus = ebus_to_hbus(ebus);
234 
235 	skl->init_failed = 1; /* to be sure */
236 
237 	snd_hdac_ext_stop_streams(ebus);
238 
239 	if (bus->irq >= 0)
240 		free_irq(bus->irq, (void *)bus);
241 	if (bus->remap_addr)
242 		iounmap(bus->remap_addr);
243 
244 	snd_hdac_bus_free_stream_pages(bus);
245 	snd_hdac_stream_free_all(ebus);
246 	snd_hdac_link_free_all(ebus);
247 	pci_release_regions(skl->pci);
248 	pci_disable_device(skl->pci);
249 
250 	snd_hdac_ext_bus_exit(ebus);
251 
252 	return 0;
253 }
254 
255 static int skl_machine_device_register(struct skl *skl, void *driver_data)
256 {
257 	struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
258 	struct platform_device *pdev;
259 	struct sst_acpi_mach *mach = driver_data;
260 	int ret;
261 
262 	mach = sst_acpi_find_machine(mach);
263 	if (mach == NULL) {
264 		dev_err(bus->dev, "No matching machine driver found\n");
265 		return -ENODEV;
266 	}
267 
268 	pdev = platform_device_alloc(mach->drv_name, -1);
269 	if (pdev == NULL) {
270 		dev_err(bus->dev, "platform device alloc failed\n");
271 		return -EIO;
272 	}
273 
274 	ret = platform_device_add(pdev);
275 	if (ret) {
276 		dev_err(bus->dev, "failed to add machine device\n");
277 		platform_device_put(pdev);
278 		return -EIO;
279 	}
280 	skl->i2s_dev = pdev;
281 
282 	return 0;
283 }
284 
285 static void skl_machine_device_unregister(struct skl *skl)
286 {
287 	if (skl->i2s_dev)
288 		platform_device_unregister(skl->i2s_dev);
289 }
290 
291 static int skl_dmic_device_register(struct skl *skl)
292 {
293 	struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
294 	struct platform_device *pdev;
295 	int ret;
296 
297 	/* SKL has one dmic port, so allocate dmic device for this */
298 	pdev = platform_device_alloc("dmic-codec", -1);
299 	if (!pdev) {
300 		dev_err(bus->dev, "failed to allocate dmic device\n");
301 		return -ENOMEM;
302 	}
303 
304 	ret = platform_device_add(pdev);
305 	if (ret) {
306 		dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
307 		platform_device_put(pdev);
308 		return ret;
309 	}
310 	skl->dmic_dev = pdev;
311 
312 	return 0;
313 }
314 
315 static void skl_dmic_device_unregister(struct skl *skl)
316 {
317 	if (skl->dmic_dev)
318 		platform_device_unregister(skl->dmic_dev);
319 }
320 
321 /*
322  * Probe the given codec address
323  */
324 static int probe_codec(struct hdac_ext_bus *ebus, int addr)
325 {
326 	struct hdac_bus *bus = ebus_to_hbus(ebus);
327 	unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
328 		(AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
329 	unsigned int res;
330 
331 	mutex_lock(&bus->cmd_mutex);
332 	snd_hdac_bus_send_cmd(bus, cmd);
333 	snd_hdac_bus_get_response(bus, addr, &res);
334 	mutex_unlock(&bus->cmd_mutex);
335 	if (res == -1)
336 		return -EIO;
337 	dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
338 
339 	return snd_hdac_ext_bus_device_init(ebus, addr);
340 }
341 
342 /* Codec initialization */
343 static int skl_codec_create(struct hdac_ext_bus *ebus)
344 {
345 	struct hdac_bus *bus = ebus_to_hbus(ebus);
346 	int c, max_slots;
347 
348 	max_slots = HDA_MAX_CODECS;
349 
350 	/* First try to probe all given codec slots */
351 	for (c = 0; c < max_slots; c++) {
352 		if ((bus->codec_mask & (1 << c))) {
353 			if (probe_codec(ebus, c) < 0) {
354 				/*
355 				 * Some BIOSen give you wrong codec addresses
356 				 * that don't exist
357 				 */
358 				dev_warn(bus->dev,
359 					 "Codec #%d probe error; disabling it...\n", c);
360 				bus->codec_mask &= ~(1 << c);
361 				/*
362 				 * More badly, accessing to a non-existing
363 				 * codec often screws up the controller bus,
364 				 * and disturbs the further communications.
365 				 * Thus if an error occurs during probing,
366 				 * better to reset the controller bus to get
367 				 * back to the sanity state.
368 				 */
369 				snd_hdac_bus_stop_chip(bus);
370 				snd_hdac_bus_init_chip(bus, true);
371 			}
372 		}
373 	}
374 
375 	return 0;
376 }
377 
378 static const struct hdac_bus_ops bus_core_ops = {
379 	.command = snd_hdac_bus_send_cmd,
380 	.get_response = snd_hdac_bus_get_response,
381 };
382 
383 /*
384  * constructor
385  */
386 static int skl_create(struct pci_dev *pci,
387 		      const struct hdac_io_ops *io_ops,
388 		      struct skl **rskl)
389 {
390 	struct skl *skl;
391 	struct hdac_ext_bus *ebus;
392 
393 	int err;
394 
395 	*rskl = NULL;
396 
397 	err = pci_enable_device(pci);
398 	if (err < 0)
399 		return err;
400 
401 	skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
402 	if (!skl) {
403 		pci_disable_device(pci);
404 		return -ENOMEM;
405 	}
406 	ebus = &skl->ebus;
407 	snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
408 	ebus->bus.use_posbuf = 1;
409 	skl->pci = pci;
410 
411 	ebus->bus.bdl_pos_adj = 0;
412 
413 	*rskl = skl;
414 
415 	return 0;
416 }
417 
418 static int skl_first_init(struct hdac_ext_bus *ebus)
419 {
420 	struct skl *skl = ebus_to_skl(ebus);
421 	struct hdac_bus *bus = ebus_to_hbus(ebus);
422 	struct pci_dev *pci = skl->pci;
423 	int err;
424 	unsigned short gcap;
425 	int cp_streams, pb_streams, start_idx;
426 
427 	err = pci_request_regions(pci, "Skylake HD audio");
428 	if (err < 0)
429 		return err;
430 
431 	bus->addr = pci_resource_start(pci, 0);
432 	bus->remap_addr = pci_ioremap_bar(pci, 0);
433 	if (bus->remap_addr == NULL) {
434 		dev_err(bus->dev, "ioremap error\n");
435 		return -ENXIO;
436 	}
437 
438 	snd_hdac_ext_bus_parse_capabilities(ebus);
439 
440 	if (skl_acquire_irq(ebus, 0) < 0)
441 		return -EBUSY;
442 
443 	pci_set_master(pci);
444 	synchronize_irq(bus->irq);
445 
446 	gcap = snd_hdac_chip_readw(bus, GCAP);
447 	dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
448 
449 	/* allow 64bit DMA address if supported by H/W */
450 	if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
451 		dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
452 	} else {
453 		dma_set_mask(bus->dev, DMA_BIT_MASK(32));
454 		dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
455 	}
456 
457 	/* read number of streams from GCAP register */
458 	cp_streams = (gcap >> 8) & 0x0f;
459 	pb_streams = (gcap >> 12) & 0x0f;
460 
461 	if (!pb_streams && !cp_streams)
462 		return -EIO;
463 
464 	ebus->num_streams = cp_streams + pb_streams;
465 
466 	/* initialize streams */
467 	snd_hdac_ext_stream_init_all
468 		(ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
469 	start_idx = cp_streams;
470 	snd_hdac_ext_stream_init_all
471 		(ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
472 
473 	err = snd_hdac_bus_alloc_stream_pages(bus);
474 	if (err < 0)
475 		return err;
476 
477 	/* initialize chip */
478 	skl_init_pci(skl);
479 
480 	snd_hdac_bus_init_chip(bus, true);
481 
482 	/* codec detection */
483 	if (!bus->codec_mask) {
484 		dev_info(bus->dev, "no hda codecs found!\n");
485 	}
486 
487 	return 0;
488 }
489 
490 static int skl_probe(struct pci_dev *pci,
491 		     const struct pci_device_id *pci_id)
492 {
493 	struct skl *skl;
494 	struct hdac_ext_bus *ebus = NULL;
495 	struct hdac_bus *bus = NULL;
496 	int err;
497 
498 	/* we use ext core ops, so provide NULL for ops here */
499 	err = skl_create(pci, NULL, &skl);
500 	if (err < 0)
501 		return err;
502 
503 	ebus = &skl->ebus;
504 	bus = ebus_to_hbus(ebus);
505 
506 	err = skl_first_init(ebus);
507 	if (err < 0)
508 		goto out_free;
509 
510 	skl->nhlt = skl_nhlt_init(bus->dev);
511 
512 	if (skl->nhlt == NULL)
513 		goto out_free;
514 
515 	pci_set_drvdata(skl->pci, ebus);
516 
517 	/* check if dsp is there */
518 	if (ebus->ppcap) {
519 		err = skl_machine_device_register(skl,
520 				  (void *)pci_id->driver_data);
521 		if (err < 0)
522 			goto out_free;
523 
524 		err = skl_init_dsp(skl);
525 		if (err < 0) {
526 			dev_dbg(bus->dev, "error failed to register dsp\n");
527 			goto out_mach_free;
528 		}
529 	}
530 	if (ebus->mlcap)
531 		snd_hdac_ext_bus_get_ml_capabilities(ebus);
532 
533 	/* create device for soc dmic */
534 	err = skl_dmic_device_register(skl);
535 	if (err < 0)
536 		goto out_dsp_free;
537 
538 	/* register platform dai and controls */
539 	err = skl_platform_register(bus->dev);
540 	if (err < 0)
541 		goto out_dmic_free;
542 
543 	/* create codec instances */
544 	err = skl_codec_create(ebus);
545 	if (err < 0)
546 		goto out_unregister;
547 
548 	/*configure PM */
549 	pm_runtime_set_autosuspend_delay(bus->dev, SKL_SUSPEND_DELAY);
550 	pm_runtime_use_autosuspend(bus->dev);
551 	pm_runtime_put_noidle(bus->dev);
552 	pm_runtime_allow(bus->dev);
553 
554 	return 0;
555 
556 out_unregister:
557 	skl_platform_unregister(bus->dev);
558 out_dmic_free:
559 	skl_dmic_device_unregister(skl);
560 out_dsp_free:
561 	skl_free_dsp(skl);
562 out_mach_free:
563 	skl_machine_device_unregister(skl);
564 out_free:
565 	skl->init_failed = 1;
566 	skl_free(ebus);
567 
568 	return err;
569 }
570 
571 static void skl_remove(struct pci_dev *pci)
572 {
573 	struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
574 	struct skl *skl = ebus_to_skl(ebus);
575 
576 	if (pci_dev_run_wake(pci))
577 		pm_runtime_get_noresume(&pci->dev);
578 	pci_dev_put(pci);
579 	skl_platform_unregister(&pci->dev);
580 	skl_free_dsp(skl);
581 	skl_machine_device_unregister(skl);
582 	skl_dmic_device_unregister(skl);
583 	skl_free(ebus);
584 	dev_set_drvdata(&pci->dev, NULL);
585 }
586 
587 static struct sst_acpi_mach sst_skl_devdata[] = {
588 	{ "INT343A", "skl_alc286s_i2s", "intel/dsp_fw_release.bin", NULL, NULL, NULL },
589 	{}
590 };
591 
592 /* PCI IDs */
593 static const struct pci_device_id skl_ids[] = {
594 	/* Sunrise Point-LP */
595 	{ PCI_DEVICE(0x8086, 0x9d70),
596 		.driver_data = (unsigned long)&sst_skl_devdata},
597 	{ 0, }
598 };
599 MODULE_DEVICE_TABLE(pci, skl_ids);
600 
601 /* pci_driver definition */
602 static struct pci_driver skl_driver = {
603 	.name = KBUILD_MODNAME,
604 	.id_table = skl_ids,
605 	.probe = skl_probe,
606 	.remove = skl_remove,
607 	.driver = {
608 		.pm = &skl_pm,
609 	},
610 };
611 module_pci_driver(skl_driver);
612 
613 MODULE_LICENSE("GPL v2");
614 MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");
615