]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - sound/soc/intel/skylake/skl.c
ASoC: Intel: Skylake: Request IRQ late only after all context are initialized
[mirror_ubuntu-focal-kernel.git] / sound / soc / intel / skylake / skl.c
CommitLineData
d8c2dab8
JK
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>
d8018361 28#include <linux/firmware.h>
a26a3f53 29#include <linux/delay.h>
d8c2dab8 30#include <sound/pcm.h>
7feb2f78 31#include <sound/soc-acpi.h>
6980c057
VK
32#include <sound/hda_register.h>
33#include <sound/hdaudio.h>
34#include <sound/hda_i915.h>
d8c2dab8 35#include "skl.h"
0c8ba9d2
J
36#include "skl-sst-dsp.h"
37#include "skl-sst-ipc.h"
d8c2dab8 38
f65cf7d6
YZ
39static struct skl_machine_pdata skl_dmic_data;
40
d8c2dab8
JK
41/*
42 * initialize the PCI registers
43 */
44static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg,
45 unsigned char mask, unsigned char val)
46{
47 unsigned char data;
48
49 pci_read_config_byte(pci, reg, &data);
50 data &= ~mask;
51 data |= (val & mask);
52 pci_write_config_byte(pci, reg, data);
53}
54
55static void skl_init_pci(struct skl *skl)
56{
57 struct hdac_ext_bus *ebus = &skl->ebus;
58
59 /*
60 * Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
61 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
62 * Ensuring these bits are 0 clears playback static on some HD Audio
63 * codecs.
64 * The PCI register TCSEL is defined in the Intel manuals.
65 */
66 dev_dbg(ebus_to_hbus(ebus)->dev, "Clearing TCSEL\n");
67 skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
68}
69
0c8ba9d2
J
70static void update_pci_dword(struct pci_dev *pci,
71 unsigned int reg, u32 mask, u32 val)
72{
73 u32 data = 0;
74
75 pci_read_config_dword(pci, reg, &data);
76 data &= ~mask;
77 data |= (val & mask);
78 pci_write_config_dword(pci, reg, data);
79}
80
81/*
82 * skl_enable_miscbdcge - enable/dsiable CGCTL.MISCBDCGE bits
83 *
84 * @dev: device pointer
85 * @enable: enable/disable flag
86 */
87static void skl_enable_miscbdcge(struct device *dev, bool enable)
88{
89 struct pci_dev *pci = to_pci_dev(dev);
90 u32 val;
91
92 val = enable ? AZX_CGCTL_MISCBDCGE_MASK : 0;
93
94 update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_MISCBDCGE_MASK, val);
95}
96
97/*
98 * While performing reset, controller may not come back properly causing
99 * issues, so recommendation is to set CGCTL.MISCBDCGE to 0 then do reset
100 * (init chip) and then again set CGCTL.MISCBDCGE to 1
101 */
102static int skl_init_chip(struct hdac_bus *bus, bool full_reset)
103{
104 int ret;
105
106 skl_enable_miscbdcge(bus->dev, false);
107 ret = snd_hdac_bus_init_chip(bus, full_reset);
108 skl_enable_miscbdcge(bus->dev, true);
109
110 return ret;
111}
112
a26a3f53
PS
113void skl_update_d0i3c(struct device *dev, bool enable)
114{
115 struct pci_dev *pci = to_pci_dev(dev);
116 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
117 struct hdac_bus *bus = ebus_to_hbus(ebus);
118 u8 reg;
119 int timeout = 50;
120
121 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
122 /* Do not write to D0I3C until command in progress bit is cleared */
123 while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
124 udelay(10);
125 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
126 }
127
128 /* Highly unlikely. But if it happens, flag error explicitly */
129 if (!timeout) {
130 dev_err(bus->dev, "Before D0I3C update: D0I3C CIP timeout\n");
131 return;
132 }
133
134 if (enable)
135 reg = reg | AZX_REG_VS_D0I3C_I3;
136 else
137 reg = reg & (~AZX_REG_VS_D0I3C_I3);
138
139 snd_hdac_chip_writeb(bus, VS_D0I3C, reg);
140
141 timeout = 50;
142 /* Wait for cmd in progress to be cleared before exiting the function */
143 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
144 while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
145 udelay(10);
146 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
147 }
148
149 /* Highly unlikely. But if it happens, flag error explicitly */
150 if (!timeout) {
151 dev_err(bus->dev, "After D0I3C update: D0I3C CIP timeout\n");
152 return;
153 }
154
155 dev_dbg(bus->dev, "D0I3C register = 0x%x\n",
156 snd_hdac_chip_readb(bus, VS_D0I3C));
157}
158
d8c2dab8
JK
159/* called from IRQ */
160static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
161{
162 snd_pcm_period_elapsed(hstr->substream);
163}
164
165static irqreturn_t skl_interrupt(int irq, void *dev_id)
166{
167 struct hdac_ext_bus *ebus = dev_id;
168 struct hdac_bus *bus = ebus_to_hbus(ebus);
169 u32 status;
170
171 if (!pm_runtime_active(bus->dev))
172 return IRQ_NONE;
173
174 spin_lock(&bus->reg_lock);
175
176 status = snd_hdac_chip_readl(bus, INTSTS);
177 if (status == 0 || status == 0xffffffff) {
178 spin_unlock(&bus->reg_lock);
179 return IRQ_NONE;
180 }
181
182 /* clear rirb int */
183 status = snd_hdac_chip_readb(bus, RIRBSTS);
184 if (status & RIRB_INT_MASK) {
185 if (status & RIRB_INT_RESPONSE)
186 snd_hdac_bus_update_rirb(bus);
187 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
188 }
189
190 spin_unlock(&bus->reg_lock);
191
192 return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
193}
194
195static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
196{
197 struct hdac_ext_bus *ebus = dev_id;
198 struct hdac_bus *bus = ebus_to_hbus(ebus);
199 u32 status;
200
201 status = snd_hdac_chip_readl(bus, INTSTS);
202
203 snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
204
205 return IRQ_HANDLED;
206}
207
208static int skl_acquire_irq(struct hdac_ext_bus *ebus, int do_disconnect)
209{
210 struct skl *skl = ebus_to_skl(ebus);
211 struct hdac_bus *bus = ebus_to_hbus(ebus);
212 int ret;
213
214 ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
215 skl_threaded_handler,
216 IRQF_SHARED,
217 KBUILD_MODNAME, ebus);
218 if (ret) {
219 dev_err(bus->dev,
220 "unable to grab IRQ %d, disabling device\n",
221 skl->pci->irq);
222 return ret;
223 }
224
225 bus->irq = skl->pci->irq;
226 pci_intx(skl->pci, 1);
227
228 return 0;
229}
230
8b4a133c
J
231static int skl_suspend_late(struct device *dev)
232{
233 struct pci_dev *pci = to_pci_dev(dev);
234 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
235 struct skl *skl = ebus_to_skl(ebus);
236
237 return skl_suspend_late_dsp(skl);
238}
239
61722f44
JK
240#ifdef CONFIG_PM
241static int _skl_suspend(struct hdac_ext_bus *ebus)
242{
243 struct skl *skl = ebus_to_skl(ebus);
244 struct hdac_bus *bus = ebus_to_hbus(ebus);
51a01b8c 245 struct pci_dev *pci = to_pci_dev(bus->dev);
61722f44
JK
246 int ret;
247
248 snd_hdac_ext_bus_link_power_down_all(ebus);
249
250 ret = skl_suspend_dsp(skl);
251 if (ret < 0)
252 return ret;
253
254 snd_hdac_bus_stop_chip(bus);
51a01b8c
D
255 update_pci_dword(pci, AZX_PCIREG_PGCTL,
256 AZX_PGCTL_LSRMD_MASK, AZX_PGCTL_LSRMD_MASK);
0c8ba9d2 257 skl_enable_miscbdcge(bus->dev, false);
61722f44 258 snd_hdac_bus_enter_link_reset(bus);
0c8ba9d2 259 skl_enable_miscbdcge(bus->dev, true);
fe3f4442 260 skl_cleanup_resources(skl);
61722f44
JK
261
262 return 0;
263}
264
265static int _skl_resume(struct hdac_ext_bus *ebus)
266{
267 struct skl *skl = ebus_to_skl(ebus);
268 struct hdac_bus *bus = ebus_to_hbus(ebus);
269
270 skl_init_pci(skl);
0c8ba9d2 271 skl_init_chip(bus, true);
61722f44
JK
272
273 return skl_resume_dsp(skl);
274}
275#endif
276
d8c2dab8
JK
277#ifdef CONFIG_PM_SLEEP
278/*
279 * power management
280 */
281static int skl_suspend(struct device *dev)
282{
283 struct pci_dev *pci = to_pci_dev(dev);
284 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
4557c305 285 struct skl *skl = ebus_to_skl(ebus);
1f4956fd 286 struct hdac_bus *bus = ebus_to_hbus(ebus);
af037412 287 int ret = 0;
d8c2dab8 288
4557c305
JK
289 /*
290 * Do not suspend if streams which are marked ignore suspend are
291 * running, we need to save the state for these and continue
292 */
293 if (skl->supend_active) {
cce6c149 294 /* turn off the links and stop the CORB/RIRB DMA if it is On */
c2e20cd8 295 snd_hdac_ext_bus_link_power_down_all(ebus);
cce6c149
VK
296
297 if (ebus->cmd_dma_state)
298 snd_hdac_bus_stop_cmd_io(&ebus->bus);
299
1f4956fd 300 enable_irq_wake(bus->irq);
4557c305 301 pci_save_state(pci);
4557c305 302 } else {
af037412
SP
303 ret = _skl_suspend(ebus);
304 if (ret < 0)
305 return ret;
1665c177 306 skl->skl_sst->fw_loaded = false;
4557c305 307 }
af037412
SP
308
309 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
310 ret = snd_hdac_display_power(bus, false);
311 if (ret < 0)
312 dev_err(bus->dev,
313 "Cannot turn OFF display power on i915\n");
314 }
315
316 return ret;
d8c2dab8
JK
317}
318
319static int skl_resume(struct device *dev)
320{
321 struct pci_dev *pci = to_pci_dev(dev);
322 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
4557c305 323 struct skl *skl = ebus_to_skl(ebus);
1f4956fd 324 struct hdac_bus *bus = ebus_to_hbus(ebus);
cce6c149 325 struct hdac_ext_link *hlink = NULL;
4557c305
JK
326 int ret;
327
6980c057
VK
328 /* Turned OFF in HDMI codec driver after codec reconfiguration */
329 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
330 ret = snd_hdac_display_power(bus, true);
331 if (ret < 0) {
332 dev_err(bus->dev,
333 "Cannot turn on display power on i915\n");
334 return ret;
335 }
336 }
337
4557c305
JK
338 /*
339 * resume only when we are not in suspend active, otherwise need to
340 * restore the device
341 */
342 if (skl->supend_active) {
343 pci_restore_state(pci);
c2e20cd8 344 snd_hdac_ext_bus_link_power_up_all(ebus);
1f4956fd 345 disable_irq_wake(bus->irq);
cce6c149
VK
346 /*
347 * turn On the links which are On before active suspend
348 * and start the CORB/RIRB DMA if On before
349 * active suspend.
350 */
351 list_for_each_entry(hlink, &ebus->hlink_list, list) {
352 if (hlink->ref_count)
353 snd_hdac_ext_bus_link_power_up(hlink);
354 }
355
356 if (ebus->cmd_dma_state)
357 snd_hdac_bus_init_cmd_io(&ebus->bus);
cc20c4df 358 ret = 0;
4557c305
JK
359 } else {
360 ret = _skl_resume(ebus);
cce6c149
VK
361
362 /* turn off the links which are off before suspend */
363 list_for_each_entry(hlink, &ebus->hlink_list, list) {
364 if (!hlink->ref_count)
365 snd_hdac_ext_bus_link_power_down(hlink);
366 }
367
368 if (!ebus->cmd_dma_state)
369 snd_hdac_bus_stop_cmd_io(&ebus->bus);
4557c305 370 }
d8c2dab8 371
4557c305 372 return ret;
d8c2dab8
JK
373}
374#endif /* CONFIG_PM_SLEEP */
375
376#ifdef CONFIG_PM
377static int skl_runtime_suspend(struct device *dev)
378{
379 struct pci_dev *pci = to_pci_dev(dev);
380 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
381 struct hdac_bus *bus = ebus_to_hbus(ebus);
382
383 dev_dbg(bus->dev, "in %s\n", __func__);
384
61722f44 385 return _skl_suspend(ebus);
d8c2dab8
JK
386}
387
388static int skl_runtime_resume(struct device *dev)
389{
390 struct pci_dev *pci = to_pci_dev(dev);
391 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
392 struct hdac_bus *bus = ebus_to_hbus(ebus);
d8c2dab8
JK
393
394 dev_dbg(bus->dev, "in %s\n", __func__);
395
61722f44 396 return _skl_resume(ebus);
d8c2dab8
JK
397}
398#endif /* CONFIG_PM */
399
400static const struct dev_pm_ops skl_pm = {
401 SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
402 SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
8b4a133c 403 .suspend_late = skl_suspend_late,
d8c2dab8
JK
404};
405
406/*
407 * destructor
408 */
409static int skl_free(struct hdac_ext_bus *ebus)
410{
411 struct skl *skl = ebus_to_skl(ebus);
412 struct hdac_bus *bus = ebus_to_hbus(ebus);
413
ab1b732d 414 skl->init_done = 0; /* to be sure */
d8c2dab8
JK
415
416 snd_hdac_ext_stop_streams(ebus);
417
418 if (bus->irq >= 0)
c360e0c3 419 free_irq(bus->irq, (void *)ebus);
d8c2dab8
JK
420 snd_hdac_bus_free_stream_pages(bus);
421 snd_hdac_stream_free_all(ebus);
422 snd_hdac_link_free_all(ebus);
077411e5
VK
423
424 if (bus->remap_addr)
425 iounmap(bus->remap_addr);
426
d8c2dab8
JK
427 pci_release_regions(skl->pci);
428 pci_disable_device(skl->pci);
429
430 snd_hdac_ext_bus_exit(ebus);
431
ab1b732d 432 cancel_work_sync(&skl->probe_work);
5b2fe898
VK
433 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
434 snd_hdac_i915_exit(&ebus->bus);
ab1b732d 435
d8c2dab8
JK
436 return 0;
437}
438
bc2bd45b
SP
439/*
440 * For each ssp there are 3 clocks (mclk/sclk/sclkfs).
441 * e.g. for ssp0, clocks will be named as
442 * "ssp0_mclk", "ssp0_sclk", "ssp0_sclkfs"
443 * So for skl+, there are 6 ssps, so 18 clocks will be created.
444 */
445static struct skl_ssp_clk skl_ssp_clks[] = {
446 {.name = "ssp0_mclk"}, {.name = "ssp1_mclk"}, {.name = "ssp2_mclk"},
447 {.name = "ssp3_mclk"}, {.name = "ssp4_mclk"}, {.name = "ssp5_mclk"},
448 {.name = "ssp0_sclk"}, {.name = "ssp1_sclk"}, {.name = "ssp2_sclk"},
449 {.name = "ssp3_sclk"}, {.name = "ssp4_sclk"}, {.name = "ssp5_sclk"},
450 {.name = "ssp0_sclkfs"}, {.name = "ssp1_sclkfs"},
451 {.name = "ssp2_sclkfs"},
452 {.name = "ssp3_sclkfs"}, {.name = "ssp4_sclkfs"},
453 {.name = "ssp5_sclkfs"},
454};
455
cc18c5fd
VK
456static int skl_machine_device_register(struct skl *skl, void *driver_data)
457{
458 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
459 struct platform_device *pdev;
7feb2f78 460 struct snd_soc_acpi_mach *mach = driver_data;
cc18c5fd
VK
461 int ret;
462
7feb2f78 463 mach = snd_soc_acpi_find_machine(mach);
cc18c5fd
VK
464 if (mach == NULL) {
465 dev_err(bus->dev, "No matching machine driver found\n");
466 return -ENODEV;
467 }
aecf6fd8 468 skl->fw_name = mach->fw_filename;
cc18c5fd
VK
469
470 pdev = platform_device_alloc(mach->drv_name, -1);
471 if (pdev == NULL) {
472 dev_err(bus->dev, "platform device alloc failed\n");
473 return -EIO;
474 }
475
476 ret = platform_device_add(pdev);
477 if (ret) {
478 dev_err(bus->dev, "failed to add machine device\n");
479 platform_device_put(pdev);
480 return -EIO;
481 }
f65cf7d6 482
c3ae22e3
GS
483 if (mach->pdata) {
484 skl->use_tplg_pcm =
485 ((struct skl_machine_pdata *)mach->pdata)->use_tplg_pcm;
f65cf7d6 486 dev_set_drvdata(&pdev->dev, mach->pdata);
c3ae22e3 487 }
f65cf7d6 488
cc18c5fd
VK
489 skl->i2s_dev = pdev;
490
491 return 0;
492}
493
494static void skl_machine_device_unregister(struct skl *skl)
495{
496 if (skl->i2s_dev)
497 platform_device_unregister(skl->i2s_dev);
498}
499
d8c2dab8
JK
500static int skl_dmic_device_register(struct skl *skl)
501{
502 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
503 struct platform_device *pdev;
504 int ret;
505
506 /* SKL has one dmic port, so allocate dmic device for this */
507 pdev = platform_device_alloc("dmic-codec", -1);
508 if (!pdev) {
509 dev_err(bus->dev, "failed to allocate dmic device\n");
510 return -ENOMEM;
511 }
512
513 ret = platform_device_add(pdev);
514 if (ret) {
515 dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
516 platform_device_put(pdev);
517 return ret;
518 }
519 skl->dmic_dev = pdev;
520
521 return 0;
522}
523
524static void skl_dmic_device_unregister(struct skl *skl)
525{
526 if (skl->dmic_dev)
527 platform_device_unregister(skl->dmic_dev);
528}
529
bc2bd45b
SP
530static struct skl_clk_parent_src skl_clk_src[] = {
531 { .clk_id = SKL_XTAL, .name = "xtal" },
532 { .clk_id = SKL_CARDINAL, .name = "cardinal", .rate = 24576000 },
533 { .clk_id = SKL_PLL, .name = "pll", .rate = 96000000 },
534};
535
536struct skl_clk_parent_src *skl_get_parent_clk(u8 clk_id)
537{
538 unsigned int i;
539
540 for (i = 0; i < ARRAY_SIZE(skl_clk_src); i++) {
541 if (skl_clk_src[i].clk_id == clk_id)
542 return &skl_clk_src[i];
543 }
544
545 return NULL;
546}
547
8e79ec98 548static void init_skl_xtal_rate(int pci_id)
bc2bd45b
SP
549{
550 switch (pci_id) {
551 case 0x9d70:
552 case 0x9d71:
553 skl_clk_src[0].rate = 24000000;
554 return;
555
556 default:
557 skl_clk_src[0].rate = 19200000;
558 return;
559 }
560}
561
562static int skl_clock_device_register(struct skl *skl)
563{
564 struct platform_device_info pdevinfo = {NULL};
565 struct skl_clk_pdata *clk_pdata;
566
567 clk_pdata = devm_kzalloc(&skl->pci->dev, sizeof(*clk_pdata),
568 GFP_KERNEL);
569 if (!clk_pdata)
570 return -ENOMEM;
571
572 init_skl_xtal_rate(skl->pci->device);
573
574 clk_pdata->parent_clks = skl_clk_src;
575 clk_pdata->ssp_clks = skl_ssp_clks;
576 clk_pdata->num_clks = ARRAY_SIZE(skl_ssp_clks);
577
578 /* Query NHLT to fill the rates and parent */
579 skl_get_clks(skl, clk_pdata->ssp_clks);
580 clk_pdata->pvt_data = skl;
581
582 /* Register Platform device */
583 pdevinfo.parent = &skl->pci->dev;
584 pdevinfo.id = -1;
585 pdevinfo.name = "skl-ssp-clk";
586 pdevinfo.data = clk_pdata;
587 pdevinfo.size_data = sizeof(*clk_pdata);
588 skl->clk_dev = platform_device_register_full(&pdevinfo);
589 return PTR_ERR_OR_ZERO(skl->clk_dev);
590}
591
592static void skl_clock_device_unregister(struct skl *skl)
593{
594 if (skl->clk_dev)
595 platform_device_unregister(skl->clk_dev);
596}
597
d8c2dab8
JK
598/*
599 * Probe the given codec address
600 */
601static int probe_codec(struct hdac_ext_bus *ebus, int addr)
602{
603 struct hdac_bus *bus = ebus_to_hbus(ebus);
604 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
605 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
e6a33532 606 unsigned int res = -1;
d8c2dab8
JK
607
608 mutex_lock(&bus->cmd_mutex);
609 snd_hdac_bus_send_cmd(bus, cmd);
610 snd_hdac_bus_get_response(bus, addr, &res);
611 mutex_unlock(&bus->cmd_mutex);
612 if (res == -1)
613 return -EIO;
614 dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
615
616 return snd_hdac_ext_bus_device_init(ebus, addr);
617}
618
619/* Codec initialization */
693c0fb2 620static void skl_codec_create(struct hdac_ext_bus *ebus)
d8c2dab8
JK
621{
622 struct hdac_bus *bus = ebus_to_hbus(ebus);
623 int c, max_slots;
624
625 max_slots = HDA_MAX_CODECS;
626
627 /* First try to probe all given codec slots */
628 for (c = 0; c < max_slots; c++) {
629 if ((bus->codec_mask & (1 << c))) {
630 if (probe_codec(ebus, c) < 0) {
631 /*
632 * Some BIOSen give you wrong codec addresses
633 * that don't exist
634 */
635 dev_warn(bus->dev,
636 "Codec #%d probe error; disabling it...\n", c);
637 bus->codec_mask &= ~(1 << c);
638 /*
639 * More badly, accessing to a non-existing
640 * codec often screws up the controller bus,
641 * and disturbs the further communications.
642 * Thus if an error occurs during probing,
643 * better to reset the controller bus to get
644 * back to the sanity state.
645 */
646 snd_hdac_bus_stop_chip(bus);
0c8ba9d2 647 skl_init_chip(bus, true);
d8c2dab8
JK
648 }
649 }
650 }
d8c2dab8
JK
651}
652
653static const struct hdac_bus_ops bus_core_ops = {
654 .command = snd_hdac_bus_send_cmd,
655 .get_response = snd_hdac_bus_get_response,
656};
657
ab1b732d
VK
658static int skl_i915_init(struct hdac_bus *bus)
659{
660 int err;
661
662 /*
663 * The HDMI codec is in GPU so we need to ensure that it is powered
664 * up and ready for probe
665 */
666 err = snd_hdac_i915_init(bus);
667 if (err < 0)
668 return err;
669
670 err = snd_hdac_display_power(bus, true);
671 if (err < 0)
672 dev_err(bus->dev, "Cannot turn on display power on i915\n");
673
674 return err;
675}
676
677static void skl_probe_work(struct work_struct *work)
678{
679 struct skl *skl = container_of(work, struct skl, probe_work);
680 struct hdac_ext_bus *ebus = &skl->ebus;
681 struct hdac_bus *bus = ebus_to_hbus(ebus);
682 struct hdac_ext_link *hlink = NULL;
683 int err;
684
685 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
686 err = skl_i915_init(bus);
687 if (err < 0)
688 return;
689 }
690
691 err = skl_init_chip(bus, true);
692 if (err < 0) {
693 dev_err(bus->dev, "Init chip failed with err: %d\n", err);
694 goto out_err;
695 }
696
697 /* codec detection */
698 if (!bus->codec_mask)
699 dev_info(bus->dev, "no hda codecs found!\n");
700
701 /* create codec instances */
693c0fb2 702 skl_codec_create(ebus);
ab1b732d
VK
703
704 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
705 err = snd_hdac_display_power(bus, false);
706 if (err < 0) {
707 dev_err(bus->dev, "Cannot turn off display power on i915\n");
708 return;
709 }
710 }
711
712 /* register platform dai and controls */
713 err = skl_platform_register(bus->dev);
714 if (err < 0)
715 return;
716 /*
717 * we are done probing so decrement link counts
718 */
719 list_for_each_entry(hlink, &ebus->hlink_list, list)
720 snd_hdac_ext_bus_link_put(ebus, hlink);
721
722 /* configure PM */
723 pm_runtime_put_noidle(bus->dev);
724 pm_runtime_allow(bus->dev);
725 skl->init_done = 1;
726
727 return;
728
729out_err:
730 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
731 err = snd_hdac_display_power(bus, false);
732}
733
d8c2dab8
JK
734/*
735 * constructor
736 */
737static int skl_create(struct pci_dev *pci,
738 const struct hdac_io_ops *io_ops,
739 struct skl **rskl)
740{
741 struct skl *skl;
742 struct hdac_ext_bus *ebus;
743
744 int err;
745
746 *rskl = NULL;
747
748 err = pci_enable_device(pci);
749 if (err < 0)
750 return err;
751
752 skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
753 if (!skl) {
754 pci_disable_device(pci);
755 return -ENOMEM;
756 }
757 ebus = &skl->ebus;
758 snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
759 ebus->bus.use_posbuf = 1;
760 skl->pci = pci;
ab1b732d 761 INIT_WORK(&skl->probe_work, skl_probe_work);
d8c2dab8
JK
762
763 ebus->bus.bdl_pos_adj = 0;
764
765 *rskl = skl;
766
767 return 0;
768}
769
770static int skl_first_init(struct hdac_ext_bus *ebus)
771{
772 struct skl *skl = ebus_to_skl(ebus);
773 struct hdac_bus *bus = ebus_to_hbus(ebus);
774 struct pci_dev *pci = skl->pci;
775 int err;
776 unsigned short gcap;
777 int cp_streams, pb_streams, start_idx;
778
779 err = pci_request_regions(pci, "Skylake HD audio");
780 if (err < 0)
781 return err;
782
783 bus->addr = pci_resource_start(pci, 0);
784 bus->remap_addr = pci_ioremap_bar(pci, 0);
785 if (bus->remap_addr == NULL) {
786 dev_err(bus->dev, "ioremap error\n");
787 return -ENXIO;
788 }
789
60767abc 790 skl_init_chip(bus, true);
791
ec8ae570 792 snd_hdac_bus_parse_capabilities(bus);
05057001 793
d8c2dab8
JK
794 if (skl_acquire_irq(ebus, 0) < 0)
795 return -EBUSY;
796
797 pci_set_master(pci);
798 synchronize_irq(bus->irq);
799
800 gcap = snd_hdac_chip_readw(bus, GCAP);
801 dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
802
803 /* allow 64bit DMA address if supported by H/W */
804 if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
805 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
806 } else {
807 dma_set_mask(bus->dev, DMA_BIT_MASK(32));
808 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
809 }
810
811 /* read number of streams from GCAP register */
812 cp_streams = (gcap >> 8) & 0x0f;
813 pb_streams = (gcap >> 12) & 0x0f;
814
815 if (!pb_streams && !cp_streams)
816 return -EIO;
817
818 ebus->num_streams = cp_streams + pb_streams;
819
820 /* initialize streams */
821 snd_hdac_ext_stream_init_all
822 (ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
823 start_idx = cp_streams;
824 snd_hdac_ext_stream_init_all
825 (ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
826
827 err = snd_hdac_bus_alloc_stream_pages(bus);
828 if (err < 0)
829 return err;
830
831 /* initialize chip */
832 skl_init_pci(skl);
833
ab1b732d 834 return skl_init_chip(bus, true);
d8c2dab8
JK
835}
836
837static int skl_probe(struct pci_dev *pci,
838 const struct pci_device_id *pci_id)
839{
840 struct skl *skl;
841 struct hdac_ext_bus *ebus = NULL;
842 struct hdac_bus *bus = NULL;
843 int err;
844
845 /* we use ext core ops, so provide NULL for ops here */
846 err = skl_create(pci, NULL, &skl);
847 if (err < 0)
848 return err;
849
850 ebus = &skl->ebus;
851 bus = ebus_to_hbus(ebus);
852
853 err = skl_first_init(ebus);
854 if (err < 0)
855 goto out_free;
856
4b235c43
VK
857 skl->pci_id = pci->device;
858
2e9dc2b6
VK
859 device_disable_async_suspend(bus->dev);
860
87b2bdf0
JK
861 skl->nhlt = skl_nhlt_init(bus->dev);
862
979cf59a
WY
863 if (skl->nhlt == NULL) {
864 err = -ENODEV;
ab1b732d 865 goto out_free;
979cf59a 866 }
87b2bdf0 867
0cf5a171
SP
868 err = skl_nhlt_create_sysfs(skl);
869 if (err < 0)
870 goto out_nhlt_free;
871
4b235c43
VK
872 skl_nhlt_update_topology_bin(skl);
873
d8c2dab8
JK
874 pci_set_drvdata(skl->pci, ebus);
875
f65cf7d6
YZ
876 skl_dmic_data.dmic_num = skl_get_dmic_geo(skl);
877
05057001 878 /* check if dsp is there */
ec8ae570 879 if (bus->ppcap) {
bc2bd45b
SP
880 /* create device for dsp clk */
881 err = skl_clock_device_register(skl);
882 if (err < 0)
883 goto out_clk_free;
884
cc18c5fd
VK
885 err = skl_machine_device_register(skl,
886 (void *)pci_id->driver_data);
887 if (err < 0)
c286b3f9 888 goto out_nhlt_free;
cc18c5fd 889
2a29b200
JK
890 err = skl_init_dsp(skl);
891 if (err < 0) {
892 dev_dbg(bus->dev, "error failed to register dsp\n");
cc18c5fd 893 goto out_mach_free;
2a29b200 894 }
0c8ba9d2
J
895 skl->skl_sst->enable_miscbdcge = skl_enable_miscbdcge;
896
05057001 897 }
ec8ae570 898 if (bus->mlcap)
05057001
JK
899 snd_hdac_ext_bus_get_ml_capabilities(ebus);
900
ab1b732d
VK
901 snd_hdac_bus_stop_chip(bus);
902
d8c2dab8
JK
903 /* create device for soc dmic */
904 err = skl_dmic_device_register(skl);
905 if (err < 0)
2a29b200 906 goto out_dsp_free;
d8c2dab8 907
ab1b732d 908 schedule_work(&skl->probe_work);
d8c2dab8
JK
909
910 return 0;
911
2a29b200
JK
912out_dsp_free:
913 skl_free_dsp(skl);
cc18c5fd
VK
914out_mach_free:
915 skl_machine_device_unregister(skl);
bc2bd45b
SP
916out_clk_free:
917 skl_clock_device_unregister(skl);
c286b3f9
JK
918out_nhlt_free:
919 skl_nhlt_free(skl->nhlt);
d8c2dab8 920out_free:
d8c2dab8
JK
921 skl_free(ebus);
922
923 return err;
924}
925
c5a76a24
JK
926static void skl_shutdown(struct pci_dev *pci)
927{
928 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
929 struct hdac_bus *bus = ebus_to_hbus(ebus);
930 struct hdac_stream *s;
931 struct hdac_ext_stream *stream;
932 struct skl *skl;
933
934 if (ebus == NULL)
935 return;
936
937 skl = ebus_to_skl(ebus);
938
ab1b732d 939 if (!skl->init_done)
c5a76a24
JK
940 return;
941
942 snd_hdac_ext_stop_streams(ebus);
943 list_for_each_entry(s, &bus->stream_list, list) {
944 stream = stream_to_hdac_ext_stream(s);
945 snd_hdac_ext_stream_decouple(ebus, stream, false);
946 }
947
948 snd_hdac_bus_stop_chip(bus);
949}
950
d8c2dab8
JK
951static void skl_remove(struct pci_dev *pci)
952{
953 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
954 struct skl *skl = ebus_to_skl(ebus);
955
1b00126c 956 release_firmware(skl->tplg);
d8018361 957
6d13f62d 958 pm_runtime_get_noresume(&pci->dev);
7373f481
VK
959
960 /* codec removal, invoke bus_device_remove */
961 snd_hdac_ext_bus_device_remove(ebus);
962
5cdf6c09 963 skl->debugfs = NULL;
d8c2dab8 964 skl_platform_unregister(&pci->dev);
2a29b200 965 skl_free_dsp(skl);
cc18c5fd 966 skl_machine_device_unregister(skl);
d8c2dab8 967 skl_dmic_device_unregister(skl);
bc2bd45b 968 skl_clock_device_unregister(skl);
0cf5a171 969 skl_nhlt_remove_sysfs(skl);
c286b3f9 970 skl_nhlt_free(skl->nhlt);
d8c2dab8
JK
971 skl_free(ebus);
972 dev_set_drvdata(&pci->dev, NULL);
973}
974
7feb2f78 975static struct snd_soc_acpi_codecs skl_codecs = {
9a90c972 976 .num_codecs = 1,
d70c4a04 977 .codecs = {"10508825"}
9a90c972
HP
978};
979
7feb2f78 980static struct snd_soc_acpi_codecs kbl_codecs = {
9a90c972 981 .num_codecs = 1,
d70c4a04 982 .codecs = {"10508825"}
9a90c972
HP
983};
984
7feb2f78 985static struct snd_soc_acpi_codecs bxt_codecs = {
9a90c972
HP
986 .num_codecs = 1,
987 .codecs = {"MX98357A"}
988};
989
7feb2f78 990static struct snd_soc_acpi_codecs kbl_poppy_codecs = {
9a90c972
HP
991 .num_codecs = 1,
992 .codecs = {"10EC5663"}
993};
54746dab 994
7feb2f78 995static struct snd_soc_acpi_codecs kbl_5663_5514_codecs = {
ad7fb5a3
HP
996 .num_codecs = 2,
997 .codecs = {"10EC5663", "10EC5514"}
998};
999
c3ae22e3
GS
1000static struct skl_machine_pdata cnl_pdata = {
1001 .use_tplg_pcm = true,
1002};
ad7fb5a3 1003
7feb2f78 1004static struct snd_soc_acpi_mach sst_skl_devdata[] = {
9bf70cd4
N
1005 {
1006 .id = "INT343A",
1007 .drv_name = "skl_alc286s_i2s",
1008 .fw_filename = "intel/dsp_fw_release.bin",
1009 },
1010 {
1011 .id = "INT343B",
1012 .drv_name = "skl_n88l25_s4567",
1013 .fw_filename = "intel/dsp_fw_release.bin",
7feb2f78 1014 .machine_quirk = snd_soc_acpi_codec_list,
54746dab 1015 .quirk_data = &skl_codecs,
9bf70cd4
N
1016 .pdata = &skl_dmic_data
1017 },
1018 {
1019 .id = "MX98357A",
1020 .drv_name = "skl_n88l25_m98357a",
1021 .fw_filename = "intel/dsp_fw_release.bin",
7feb2f78 1022 .machine_quirk = snd_soc_acpi_codec_list,
54746dab 1023 .quirk_data = &skl_codecs,
9bf70cd4
N
1024 .pdata = &skl_dmic_data
1025 },
cc18c5fd
VK
1026 {}
1027};
1028
7feb2f78 1029static struct snd_soc_acpi_mach sst_bxtp_devdata[] = {
9bf70cd4
N
1030 {
1031 .id = "INT343A",
1032 .drv_name = "bxt_alc298s_i2s",
1033 .fw_filename = "intel/dsp_fw_bxtn.bin",
1034 },
1035 {
1036 .id = "DLGS7219",
1037 .drv_name = "bxt_da7219_max98357a_i2s",
1038 .fw_filename = "intel/dsp_fw_bxtn.bin",
7feb2f78 1039 .machine_quirk = snd_soc_acpi_codec_list,
54746dab 1040 .quirk_data = &bxt_codecs,
9bf70cd4 1041 },
b76e3f93 1042 {}
b379b1fa
SV
1043};
1044
7feb2f78 1045static struct snd_soc_acpi_mach sst_kbl_devdata[] = {
9bf70cd4
N
1046 {
1047 .id = "INT343A",
1048 .drv_name = "kbl_alc286s_i2s",
1049 .fw_filename = "intel/dsp_fw_kbl.bin",
1050 },
1051 {
1052 .id = "INT343B",
1053 .drv_name = "kbl_n88l25_s4567",
1054 .fw_filename = "intel/dsp_fw_kbl.bin",
7feb2f78 1055 .machine_quirk = snd_soc_acpi_codec_list,
54746dab 1056 .quirk_data = &kbl_codecs,
9bf70cd4
N
1057 .pdata = &skl_dmic_data
1058 },
1059 {
1060 .id = "MX98357A",
1061 .drv_name = "kbl_n88l25_m98357a",
1062 .fw_filename = "intel/dsp_fw_kbl.bin",
7feb2f78 1063 .machine_quirk = snd_soc_acpi_codec_list,
54746dab 1064 .quirk_data = &kbl_codecs,
9bf70cd4
N
1065 .pdata = &skl_dmic_data
1066 },
ad7fb5a3
HP
1067 {
1068 .id = "MX98927",
1069 .drv_name = "kbl_r5514_5663_max",
1070 .fw_filename = "intel/dsp_fw_kbl.bin",
7feb2f78 1071 .machine_quirk = snd_soc_acpi_codec_list,
ad7fb5a3
HP
1072 .quirk_data = &kbl_5663_5514_codecs,
1073 .pdata = &skl_dmic_data
1074 },
0809d987
N
1075 {
1076 .id = "MX98927",
1077 .drv_name = "kbl_rt5663_m98927",
1078 .fw_filename = "intel/dsp_fw_kbl.bin",
7feb2f78 1079 .machine_quirk = snd_soc_acpi_codec_list,
0809d987
N
1080 .quirk_data = &kbl_poppy_codecs,
1081 .pdata = &skl_dmic_data
1082 },
0dfa7a04
KC
1083 {
1084 .id = "10EC5663",
1085 .drv_name = "kbl_rt5663",
1086 .fw_filename = "intel/dsp_fw_kbl.bin",
1087 },
0809d987 1088
451dfb5f
VK
1089 {}
1090};
1091
7feb2f78 1092static struct snd_soc_acpi_mach sst_glk_devdata[] = {
9bf70cd4
N
1093 {
1094 .id = "INT343A",
1095 .drv_name = "glk_alc298s_i2s",
1096 .fw_filename = "intel/dsp_fw_glk.bin",
1097 },
b76e3f93 1098 {}
25504863
VK
1099};
1100
7feb2f78 1101static const struct snd_soc_acpi_mach sst_cnl_devdata[] = {
86d7ce3d
GS
1102 {
1103 .id = "INT34C2",
1104 .drv_name = "cnl_rt274",
1105 .fw_filename = "intel/dsp_fw_cnl.bin",
c3ae22e3 1106 .pdata = &cnl_pdata,
86d7ce3d 1107 },
364497ac 1108 {}
86d7ce3d
GS
1109};
1110
d8c2dab8
JK
1111/* PCI IDs */
1112static const struct pci_device_id skl_ids[] = {
1113 /* Sunrise Point-LP */
cc18c5fd
VK
1114 { PCI_DEVICE(0x8086, 0x9d70),
1115 .driver_data = (unsigned long)&sst_skl_devdata},
b379b1fa
SV
1116 /* BXT-P */
1117 { PCI_DEVICE(0x8086, 0x5a98),
1118 .driver_data = (unsigned long)&sst_bxtp_devdata},
451dfb5f
VK
1119 /* KBL */
1120 { PCI_DEVICE(0x8086, 0x9D71),
1121 .driver_data = (unsigned long)&sst_kbl_devdata},
25504863
VK
1122 /* GLK */
1123 { PCI_DEVICE(0x8086, 0x3198),
1124 .driver_data = (unsigned long)&sst_glk_devdata},
86d7ce3d
GS
1125 /* CNL */
1126 { PCI_DEVICE(0x8086, 0x9dc8),
1127 .driver_data = (unsigned long)&sst_cnl_devdata},
d8c2dab8
JK
1128 { 0, }
1129};
1130MODULE_DEVICE_TABLE(pci, skl_ids);
1131
1132/* pci_driver definition */
1133static struct pci_driver skl_driver = {
1134 .name = KBUILD_MODNAME,
1135 .id_table = skl_ids,
1136 .probe = skl_probe,
1137 .remove = skl_remove,
c5a76a24 1138 .shutdown = skl_shutdown,
d8c2dab8
JK
1139 .driver = {
1140 .pm = &skl_pm,
1141 },
1142};
1143module_pci_driver(skl_driver);
1144
1145MODULE_LICENSE("GPL v2");
1146MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");