1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * skl.c - Implementation of ASoC Intel SKL HD Audio driver
4 *
5 * Copyright (C) 2014-2015 Intel Corp
6 * Author: Jeeja KP <jeeja.kp@intel.com>
7 *
8 * Derived mostly from Intel HDA driver with following copyrights:
9 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
10 * PeiSen Hou <pshou@realtek.com.tw>
11 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 *
13 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 */
15
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/platform_device.h>
20 #include <linux/firmware.h>
21 #include <linux/delay.h>
22 #include <sound/pcm.h>
23 #include <sound/soc-acpi.h>
24 #include <sound/soc-acpi-intel-match.h>
25 #include <sound/hda_register.h>
26 #include <sound/hdaudio.h>
27 #include <sound/hda_i915.h>
28 #include <sound/hda_codec.h>
29 #include <sound/intel-nhlt.h>
30 #include "skl.h"
31 #include "skl-sst-dsp.h"
32 #include "skl-sst-ipc.h"
33
34 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
35 #include "../../../soc/codecs/hdac_hda.h"
36 #endif
37 static int skl_pci_binding;
38 module_param_named(pci_binding, skl_pci_binding, int, 0444);
39 MODULE_PARM_DESC(pci_binding, "PCI binding (0=auto, 1=only legacy, 2=only asoc");
40
41 /*
42 * initialize the PCI registers
43 */
skl_update_pci_byte(struct pci_dev * pci,unsigned int reg,unsigned char mask,unsigned char val)44 static 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
skl_init_pci(struct skl_dev * skl)55 static void skl_init_pci(struct skl_dev *skl)
56 {
57 struct hdac_bus *bus = skl_to_bus(skl);
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(bus->dev, "Clearing TCSEL\n");
67 skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
68 }
69
update_pci_dword(struct pci_dev * pci,unsigned int reg,u32 mask,u32 val)70 static 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 */
skl_enable_miscbdcge(struct device * dev,bool enable)87 static 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 * skl_clock_power_gating: Enable/Disable clock and power gating
99 *
100 * @dev: Device pointer
101 * @enable: Enable/Disable flag
102 */
skl_clock_power_gating(struct device * dev,bool enable)103 static void skl_clock_power_gating(struct device *dev, bool enable)
104 {
105 struct pci_dev *pci = to_pci_dev(dev);
106 struct hdac_bus *bus = pci_get_drvdata(pci);
107 u32 val;
108
109 /* Update PDCGE bit of CGCTL register */
110 val = enable ? AZX_CGCTL_ADSPDCGE : 0;
111 update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_ADSPDCGE, val);
112
113 /* Update L1SEN bit of EM2 register */
114 val = enable ? AZX_REG_VS_EM2_L1SEN : 0;
115 snd_hdac_chip_updatel(bus, VS_EM2, AZX_REG_VS_EM2_L1SEN, val);
116
117 /* Update ADSPPGD bit of PGCTL register */
118 val = enable ? 0 : AZX_PGCTL_ADSPPGD;
119 update_pci_dword(pci, AZX_PCIREG_PGCTL, AZX_PGCTL_ADSPPGD, val);
120 }
121
122 /*
123 * While performing reset, controller may not come back properly causing
124 * issues, so recommendation is to set CGCTL.MISCBDCGE to 0 then do reset
125 * (init chip) and then again set CGCTL.MISCBDCGE to 1
126 */
skl_init_chip(struct hdac_bus * bus,bool full_reset)127 static int skl_init_chip(struct hdac_bus *bus, bool full_reset)
128 {
129 struct hdac_ext_link *hlink;
130 int ret;
131
132 snd_hdac_set_codec_wakeup(bus, true);
133 skl_enable_miscbdcge(bus->dev, false);
134 ret = snd_hdac_bus_init_chip(bus, full_reset);
135
136 /* Reset stream-to-link mapping */
137 list_for_each_entry(hlink, &bus->hlink_list, list)
138 writel(0, hlink->ml_addr + AZX_REG_ML_LOSIDV);
139
140 skl_enable_miscbdcge(bus->dev, true);
141 snd_hdac_set_codec_wakeup(bus, false);
142
143 return ret;
144 }
145
skl_update_d0i3c(struct device * dev,bool enable)146 void skl_update_d0i3c(struct device *dev, bool enable)
147 {
148 struct pci_dev *pci = to_pci_dev(dev);
149 struct hdac_bus *bus = pci_get_drvdata(pci);
150 u8 reg;
151 int timeout = 50;
152
153 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
154 /* Do not write to D0I3C until command in progress bit is cleared */
155 while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
156 udelay(10);
157 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
158 }
159
160 /* Highly unlikely. But if it happens, flag error explicitly */
161 if (!timeout) {
162 dev_err(bus->dev, "Before D0I3C update: D0I3C CIP timeout\n");
163 return;
164 }
165
166 if (enable)
167 reg = reg | AZX_REG_VS_D0I3C_I3;
168 else
169 reg = reg & (~AZX_REG_VS_D0I3C_I3);
170
171 snd_hdac_chip_writeb(bus, VS_D0I3C, reg);
172
173 timeout = 50;
174 /* Wait for cmd in progress to be cleared before exiting the function */
175 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
176 while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
177 udelay(10);
178 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
179 }
180
181 /* Highly unlikely. But if it happens, flag error explicitly */
182 if (!timeout) {
183 dev_err(bus->dev, "After D0I3C update: D0I3C CIP timeout\n");
184 return;
185 }
186
187 dev_dbg(bus->dev, "D0I3C register = 0x%x\n",
188 snd_hdac_chip_readb(bus, VS_D0I3C));
189 }
190
191 /**
192 * skl_dum_set - set DUM bit in EM2 register
193 * @bus: HD-audio core bus
194 *
195 * Addresses incorrect position reporting for capture streams.
196 * Used on device power up.
197 */
skl_dum_set(struct hdac_bus * bus)198 static void skl_dum_set(struct hdac_bus *bus)
199 {
200 /* For the DUM bit to be set, CRST needs to be out of reset state */
201 if (!(snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET)) {
202 skl_enable_miscbdcge(bus->dev, false);
203 snd_hdac_bus_exit_link_reset(bus);
204 skl_enable_miscbdcge(bus->dev, true);
205 }
206
207 snd_hdac_chip_updatel(bus, VS_EM2, AZX_VS_EM2_DUM, AZX_VS_EM2_DUM);
208 }
209
210 /* called from IRQ */
skl_stream_update(struct hdac_bus * bus,struct hdac_stream * hstr)211 static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
212 {
213 snd_pcm_period_elapsed(hstr->substream);
214 }
215
skl_interrupt(int irq,void * dev_id)216 static irqreturn_t skl_interrupt(int irq, void *dev_id)
217 {
218 struct hdac_bus *bus = dev_id;
219 u32 status;
220
221 if (!pm_runtime_active(bus->dev))
222 return IRQ_NONE;
223
224 spin_lock(&bus->reg_lock);
225
226 status = snd_hdac_chip_readl(bus, INTSTS);
227 if (status == 0 || status == 0xffffffff) {
228 spin_unlock(&bus->reg_lock);
229 return IRQ_NONE;
230 }
231
232 /* clear rirb int */
233 status = snd_hdac_chip_readb(bus, RIRBSTS);
234 if (status & RIRB_INT_MASK) {
235 if (status & RIRB_INT_RESPONSE)
236 snd_hdac_bus_update_rirb(bus);
237 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
238 }
239
240 spin_unlock(&bus->reg_lock);
241
242 return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
243 }
244
skl_threaded_handler(int irq,void * dev_id)245 static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
246 {
247 struct hdac_bus *bus = dev_id;
248 u32 status;
249
250 status = snd_hdac_chip_readl(bus, INTSTS);
251
252 snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
253
254 return IRQ_HANDLED;
255 }
256
skl_acquire_irq(struct hdac_bus * bus,int do_disconnect)257 static int skl_acquire_irq(struct hdac_bus *bus, int do_disconnect)
258 {
259 struct skl_dev *skl = bus_to_skl(bus);
260 int ret;
261
262 ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
263 skl_threaded_handler,
264 IRQF_SHARED,
265 KBUILD_MODNAME, bus);
266 if (ret) {
267 dev_err(bus->dev,
268 "unable to grab IRQ %d, disabling device\n",
269 skl->pci->irq);
270 return ret;
271 }
272
273 bus->irq = skl->pci->irq;
274 pci_intx(skl->pci, 1);
275
276 return 0;
277 }
278
skl_suspend_late(struct device * dev)279 static int skl_suspend_late(struct device *dev)
280 {
281 struct pci_dev *pci = to_pci_dev(dev);
282 struct hdac_bus *bus = pci_get_drvdata(pci);
283 struct skl_dev *skl = bus_to_skl(bus);
284
285 return skl_suspend_late_dsp(skl);
286 }
287
288 #ifdef CONFIG_PM
_skl_suspend(struct hdac_bus * bus)289 static int _skl_suspend(struct hdac_bus *bus)
290 {
291 struct skl_dev *skl = bus_to_skl(bus);
292 struct pci_dev *pci = to_pci_dev(bus->dev);
293 int ret;
294
295 snd_hdac_ext_bus_link_power_down_all(bus);
296
297 ret = skl_suspend_dsp(skl);
298 if (ret < 0)
299 return ret;
300
301 snd_hdac_bus_stop_chip(bus);
302 update_pci_dword(pci, AZX_PCIREG_PGCTL,
303 AZX_PGCTL_LSRMD_MASK, AZX_PGCTL_LSRMD_MASK);
304 skl_enable_miscbdcge(bus->dev, false);
305 snd_hdac_bus_enter_link_reset(bus);
306 skl_enable_miscbdcge(bus->dev, true);
307 skl_cleanup_resources(skl);
308
309 return 0;
310 }
311
_skl_resume(struct hdac_bus * bus)312 static int _skl_resume(struct hdac_bus *bus)
313 {
314 struct skl_dev *skl = bus_to_skl(bus);
315
316 skl_init_pci(skl);
317 skl_dum_set(bus);
318 skl_init_chip(bus, true);
319
320 return skl_resume_dsp(skl);
321 }
322 #endif
323
324 #ifdef CONFIG_PM_SLEEP
325 /*
326 * power management
327 */
skl_suspend(struct device * dev)328 static int skl_suspend(struct device *dev)
329 {
330 struct pci_dev *pci = to_pci_dev(dev);
331 struct hdac_bus *bus = pci_get_drvdata(pci);
332 struct skl_dev *skl = bus_to_skl(bus);
333 int ret;
334
335 /*
336 * Do not suspend if streams which are marked ignore suspend are
337 * running, we need to save the state for these and continue
338 */
339 if (skl->supend_active) {
340 /* turn off the links and stop the CORB/RIRB DMA if it is On */
341 snd_hdac_ext_bus_link_power_down_all(bus);
342
343 if (bus->cmd_dma_state)
344 snd_hdac_bus_stop_cmd_io(bus);
345
346 enable_irq_wake(bus->irq);
347 pci_save_state(pci);
348 } else {
349 ret = _skl_suspend(bus);
350 if (ret < 0)
351 return ret;
352 skl->fw_loaded = false;
353 }
354
355 return 0;
356 }
357
skl_resume(struct device * dev)358 static int skl_resume(struct device *dev)
359 {
360 struct pci_dev *pci = to_pci_dev(dev);
361 struct hdac_bus *bus = pci_get_drvdata(pci);
362 struct skl_dev *skl = bus_to_skl(bus);
363 struct hdac_ext_link *hlink = NULL;
364 int ret;
365
366 /*
367 * resume only when we are not in suspend active, otherwise need to
368 * restore the device
369 */
370 if (skl->supend_active) {
371 pci_restore_state(pci);
372 snd_hdac_ext_bus_link_power_up_all(bus);
373 disable_irq_wake(bus->irq);
374 /*
375 * turn On the links which are On before active suspend
376 * and start the CORB/RIRB DMA if On before
377 * active suspend.
378 */
379 list_for_each_entry(hlink, &bus->hlink_list, list) {
380 if (hlink->ref_count)
381 snd_hdac_ext_bus_link_power_up(hlink);
382 }
383
384 ret = 0;
385 if (bus->cmd_dma_state)
386 snd_hdac_bus_init_cmd_io(bus);
387 } else {
388 ret = _skl_resume(bus);
389
390 /* turn off the links which are off before suspend */
391 list_for_each_entry(hlink, &bus->hlink_list, list) {
392 if (!hlink->ref_count)
393 snd_hdac_ext_bus_link_power_down(hlink);
394 }
395
396 if (!bus->cmd_dma_state)
397 snd_hdac_bus_stop_cmd_io(bus);
398 }
399
400 return ret;
401 }
402 #endif /* CONFIG_PM_SLEEP */
403
404 #ifdef CONFIG_PM
skl_runtime_suspend(struct device * dev)405 static int skl_runtime_suspend(struct device *dev)
406 {
407 struct pci_dev *pci = to_pci_dev(dev);
408 struct hdac_bus *bus = pci_get_drvdata(pci);
409
410 dev_dbg(bus->dev, "in %s\n", __func__);
411
412 return _skl_suspend(bus);
413 }
414
skl_runtime_resume(struct device * dev)415 static int skl_runtime_resume(struct device *dev)
416 {
417 struct pci_dev *pci = to_pci_dev(dev);
418 struct hdac_bus *bus = pci_get_drvdata(pci);
419
420 dev_dbg(bus->dev, "in %s\n", __func__);
421
422 return _skl_resume(bus);
423 }
424 #endif /* CONFIG_PM */
425
426 static const struct dev_pm_ops skl_pm = {
427 SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
428 SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
429 .suspend_late = skl_suspend_late,
430 };
431
432 /*
433 * destructor
434 */
skl_free(struct hdac_bus * bus)435 static int skl_free(struct hdac_bus *bus)
436 {
437 struct skl_dev *skl = bus_to_skl(bus);
438
439 skl->init_done = 0; /* to be sure */
440
441 snd_hdac_stop_streams_and_chip(bus);
442
443 if (bus->irq >= 0)
444 free_irq(bus->irq, (void *)bus);
445 snd_hdac_bus_free_stream_pages(bus);
446 snd_hdac_stream_free_all(bus);
447 snd_hdac_link_free_all(bus);
448
449 if (bus->remap_addr)
450 iounmap(bus->remap_addr);
451
452 pci_release_regions(skl->pci);
453 pci_disable_device(skl->pci);
454
455 snd_hdac_ext_bus_exit(bus);
456
457 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
458 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
459 snd_hdac_i915_exit(bus);
460 }
461
462 return 0;
463 }
464
465 /*
466 * For each ssp there are 3 clocks (mclk/sclk/sclkfs).
467 * e.g. for ssp0, clocks will be named as
468 * "ssp0_mclk", "ssp0_sclk", "ssp0_sclkfs"
469 * So for skl+, there are 6 ssps, so 18 clocks will be created.
470 */
471 static struct skl_ssp_clk skl_ssp_clks[] = {
472 {.name = "ssp0_mclk"}, {.name = "ssp1_mclk"}, {.name = "ssp2_mclk"},
473 {.name = "ssp3_mclk"}, {.name = "ssp4_mclk"}, {.name = "ssp5_mclk"},
474 {.name = "ssp0_sclk"}, {.name = "ssp1_sclk"}, {.name = "ssp2_sclk"},
475 {.name = "ssp3_sclk"}, {.name = "ssp4_sclk"}, {.name = "ssp5_sclk"},
476 {.name = "ssp0_sclkfs"}, {.name = "ssp1_sclkfs"},
477 {.name = "ssp2_sclkfs"},
478 {.name = "ssp3_sclkfs"}, {.name = "ssp4_sclkfs"},
479 {.name = "ssp5_sclkfs"},
480 };
481
skl_find_hda_machine(struct skl_dev * skl,struct snd_soc_acpi_mach * machines)482 static struct snd_soc_acpi_mach *skl_find_hda_machine(struct skl_dev *skl,
483 struct snd_soc_acpi_mach *machines)
484 {
485 struct snd_soc_acpi_mach *mach;
486
487 /* point to common table */
488 mach = snd_soc_acpi_intel_hda_machines;
489
490 /* all entries in the machine table use the same firmware */
491 mach->fw_filename = machines->fw_filename;
492
493 return mach;
494 }
495
skl_find_machine(struct skl_dev * skl,void * driver_data)496 static int skl_find_machine(struct skl_dev *skl, void *driver_data)
497 {
498 struct hdac_bus *bus = skl_to_bus(skl);
499 struct snd_soc_acpi_mach *mach = driver_data;
500 struct skl_machine_pdata *pdata;
501
502 mach = snd_soc_acpi_find_machine(mach);
503 if (!mach) {
504 dev_dbg(bus->dev, "No matching I2S machine driver found\n");
505 mach = skl_find_hda_machine(skl, driver_data);
506 if (!mach) {
507 dev_err(bus->dev, "No matching machine driver found\n");
508 return -ENODEV;
509 }
510 }
511
512 skl->mach = mach;
513 skl->fw_name = mach->fw_filename;
514 pdata = mach->pdata;
515
516 if (pdata) {
517 skl->use_tplg_pcm = pdata->use_tplg_pcm;
518 mach->mach_params.dmic_num =
519 intel_nhlt_get_dmic_geo(&skl->pci->dev,
520 skl->nhlt);
521 }
522
523 return 0;
524 }
525
skl_machine_device_register(struct skl_dev * skl)526 static int skl_machine_device_register(struct skl_dev *skl)
527 {
528 struct snd_soc_acpi_mach *mach = skl->mach;
529 struct hdac_bus *bus = skl_to_bus(skl);
530 struct platform_device *pdev;
531 int ret;
532
533 pdev = platform_device_alloc(mach->drv_name, -1);
534 if (pdev == NULL) {
535 dev_err(bus->dev, "platform device alloc failed\n");
536 return -EIO;
537 }
538
539 mach->mach_params.platform = dev_name(bus->dev);
540 mach->mach_params.codec_mask = bus->codec_mask;
541
542 ret = platform_device_add_data(pdev, (const void *)mach, sizeof(*mach));
543 if (ret) {
544 dev_err(bus->dev, "failed to add machine device platform data\n");
545 platform_device_put(pdev);
546 return ret;
547 }
548
549 ret = platform_device_add(pdev);
550 if (ret) {
551 dev_err(bus->dev, "failed to add machine device\n");
552 platform_device_put(pdev);
553 return -EIO;
554 }
555
556
557 skl->i2s_dev = pdev;
558
559 return 0;
560 }
561
skl_machine_device_unregister(struct skl_dev * skl)562 static void skl_machine_device_unregister(struct skl_dev *skl)
563 {
564 if (skl->i2s_dev)
565 platform_device_unregister(skl->i2s_dev);
566 }
567
skl_dmic_device_register(struct skl_dev * skl)568 static int skl_dmic_device_register(struct skl_dev *skl)
569 {
570 struct hdac_bus *bus = skl_to_bus(skl);
571 struct platform_device *pdev;
572 int ret;
573
574 /* SKL has one dmic port, so allocate dmic device for this */
575 pdev = platform_device_alloc("dmic-codec", -1);
576 if (!pdev) {
577 dev_err(bus->dev, "failed to allocate dmic device\n");
578 return -ENOMEM;
579 }
580
581 ret = platform_device_add(pdev);
582 if (ret) {
583 dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
584 platform_device_put(pdev);
585 return ret;
586 }
587 skl->dmic_dev = pdev;
588
589 return 0;
590 }
591
skl_dmic_device_unregister(struct skl_dev * skl)592 static void skl_dmic_device_unregister(struct skl_dev *skl)
593 {
594 if (skl->dmic_dev)
595 platform_device_unregister(skl->dmic_dev);
596 }
597
598 static struct skl_clk_parent_src skl_clk_src[] = {
599 { .clk_id = SKL_XTAL, .name = "xtal" },
600 { .clk_id = SKL_CARDINAL, .name = "cardinal", .rate = 24576000 },
601 { .clk_id = SKL_PLL, .name = "pll", .rate = 96000000 },
602 };
603
skl_get_parent_clk(u8 clk_id)604 struct skl_clk_parent_src *skl_get_parent_clk(u8 clk_id)
605 {
606 unsigned int i;
607
608 for (i = 0; i < ARRAY_SIZE(skl_clk_src); i++) {
609 if (skl_clk_src[i].clk_id == clk_id)
610 return &skl_clk_src[i];
611 }
612
613 return NULL;
614 }
615
init_skl_xtal_rate(int pci_id)616 static void init_skl_xtal_rate(int pci_id)
617 {
618 switch (pci_id) {
619 case 0x9d70:
620 case 0x9d71:
621 skl_clk_src[0].rate = 24000000;
622 return;
623
624 default:
625 skl_clk_src[0].rate = 19200000;
626 return;
627 }
628 }
629
skl_clock_device_register(struct skl_dev * skl)630 static int skl_clock_device_register(struct skl_dev *skl)
631 {
632 struct platform_device_info pdevinfo = {NULL};
633 struct skl_clk_pdata *clk_pdata;
634
635 if (!skl->nhlt)
636 return 0;
637
638 clk_pdata = devm_kzalloc(&skl->pci->dev, sizeof(*clk_pdata),
639 GFP_KERNEL);
640 if (!clk_pdata)
641 return -ENOMEM;
642
643 init_skl_xtal_rate(skl->pci->device);
644
645 clk_pdata->parent_clks = skl_clk_src;
646 clk_pdata->ssp_clks = skl_ssp_clks;
647 clk_pdata->num_clks = ARRAY_SIZE(skl_ssp_clks);
648
649 /* Query NHLT to fill the rates and parent */
650 skl_get_clks(skl, clk_pdata->ssp_clks);
651 clk_pdata->pvt_data = skl;
652
653 /* Register Platform device */
654 pdevinfo.parent = &skl->pci->dev;
655 pdevinfo.id = -1;
656 pdevinfo.name = "skl-ssp-clk";
657 pdevinfo.data = clk_pdata;
658 pdevinfo.size_data = sizeof(*clk_pdata);
659 skl->clk_dev = platform_device_register_full(&pdevinfo);
660 return PTR_ERR_OR_ZERO(skl->clk_dev);
661 }
662
skl_clock_device_unregister(struct skl_dev * skl)663 static void skl_clock_device_unregister(struct skl_dev *skl)
664 {
665 if (skl->clk_dev)
666 platform_device_unregister(skl->clk_dev);
667 }
668
669 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
670
671 #define IDISP_INTEL_VENDOR_ID 0x80860000
672
673 /*
674 * load the legacy codec driver
675 */
load_codec_module(struct hda_codec * codec)676 static void load_codec_module(struct hda_codec *codec)
677 {
678 #ifdef MODULE
679 char modalias[MODULE_NAME_LEN];
680 const char *mod = NULL;
681
682 snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
683 mod = modalias;
684 dev_dbg(&codec->core.dev, "loading %s codec module\n", mod);
685 request_module(mod);
686 #endif
687 }
688
689 #endif /* CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC */
690
691 /*
692 * Probe the given codec address
693 */
probe_codec(struct hdac_bus * bus,int addr)694 static int probe_codec(struct hdac_bus *bus, int addr)
695 {
696 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
697 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
698 unsigned int res = -1;
699 struct skl_dev *skl = bus_to_skl(bus);
700 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
701 struct hdac_hda_priv *hda_codec;
702 int err;
703 #endif
704 struct hdac_device *hdev;
705
706 mutex_lock(&bus->cmd_mutex);
707 snd_hdac_bus_send_cmd(bus, cmd);
708 snd_hdac_bus_get_response(bus, addr, &res);
709 mutex_unlock(&bus->cmd_mutex);
710 if (res == -1)
711 return -EIO;
712 dev_dbg(bus->dev, "codec #%d probed OK: %x\n", addr, res);
713
714 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
715 hda_codec = devm_kzalloc(&skl->pci->dev, sizeof(*hda_codec),
716 GFP_KERNEL);
717 if (!hda_codec)
718 return -ENOMEM;
719
720 hda_codec->codec.bus = skl_to_hbus(skl);
721 hdev = &hda_codec->codec.core;
722
723 err = snd_hdac_ext_bus_device_init(bus, addr, hdev);
724 if (err < 0)
725 return err;
726
727 /* use legacy bus only for HDA codecs, idisp uses ext bus */
728 if ((res & 0xFFFF0000) != IDISP_INTEL_VENDOR_ID) {
729 hdev->type = HDA_DEV_LEGACY;
730 load_codec_module(&hda_codec->codec);
731 }
732 return 0;
733 #else
734 hdev = devm_kzalloc(&skl->pci->dev, sizeof(*hdev), GFP_KERNEL);
735 if (!hdev)
736 return -ENOMEM;
737
738 return snd_hdac_ext_bus_device_init(bus, addr, hdev);
739 #endif /* CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC */
740 }
741
742 /* Codec initialization */
skl_codec_create(struct hdac_bus * bus)743 static void skl_codec_create(struct hdac_bus *bus)
744 {
745 int c, max_slots;
746
747 max_slots = HDA_MAX_CODECS;
748
749 /* First try to probe all given codec slots */
750 for (c = 0; c < max_slots; c++) {
751 if ((bus->codec_mask & (1 << c))) {
752 if (probe_codec(bus, c) < 0) {
753 /*
754 * Some BIOSen give you wrong codec addresses
755 * that don't exist
756 */
757 dev_warn(bus->dev,
758 "Codec #%d probe error; disabling it...\n", c);
759 bus->codec_mask &= ~(1 << c);
760 /*
761 * More badly, accessing to a non-existing
762 * codec often screws up the controller bus,
763 * and disturbs the further communications.
764 * Thus if an error occurs during probing,
765 * better to reset the controller bus to get
766 * back to the sanity state.
767 */
768 snd_hdac_bus_stop_chip(bus);
769 skl_init_chip(bus, true);
770 }
771 }
772 }
773 }
774
775 static const struct hdac_bus_ops bus_core_ops = {
776 .command = snd_hdac_bus_send_cmd,
777 .get_response = snd_hdac_bus_get_response,
778 };
779
skl_i915_init(struct hdac_bus * bus)780 static int skl_i915_init(struct hdac_bus *bus)
781 {
782 int err;
783
784 /*
785 * The HDMI codec is in GPU so we need to ensure that it is powered
786 * up and ready for probe
787 */
788 err = snd_hdac_i915_init(bus);
789 if (err < 0)
790 return err;
791
792 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
793
794 return 0;
795 }
796
skl_probe_work(struct work_struct * work)797 static void skl_probe_work(struct work_struct *work)
798 {
799 struct skl_dev *skl = container_of(work, struct skl_dev, probe_work);
800 struct hdac_bus *bus = skl_to_bus(skl);
801 struct hdac_ext_link *hlink = NULL;
802 int err;
803
804 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
805 err = skl_i915_init(bus);
806 if (err < 0)
807 return;
808 }
809
810 skl_init_pci(skl);
811 skl_dum_set(bus);
812
813 err = skl_init_chip(bus, true);
814 if (err < 0) {
815 dev_err(bus->dev, "Init chip failed with err: %d\n", err);
816 goto out_err;
817 }
818
819 /* codec detection */
820 if (!bus->codec_mask)
821 dev_info(bus->dev, "no hda codecs found!\n");
822
823 /* create codec instances */
824 skl_codec_create(bus);
825
826 /* register platform dai and controls */
827 err = skl_platform_register(bus->dev);
828 if (err < 0) {
829 dev_err(bus->dev, "platform register failed: %d\n", err);
830 goto out_err;
831 }
832
833 err = skl_machine_device_register(skl);
834 if (err < 0) {
835 dev_err(bus->dev, "machine register failed: %d\n", err);
836 goto out_err;
837 }
838
839 /*
840 * we are done probing so decrement link counts
841 */
842 list_for_each_entry(hlink, &bus->hlink_list, list)
843 snd_hdac_ext_bus_link_put(bus, hlink);
844
845 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
846 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
847
848 /* configure PM */
849 pm_runtime_put_noidle(bus->dev);
850 pm_runtime_allow(bus->dev);
851 skl->init_done = 1;
852
853 return;
854
855 out_err:
856 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
857 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
858 }
859
860 /*
861 * constructor
862 */
skl_create(struct pci_dev * pci,struct skl_dev ** rskl)863 static int skl_create(struct pci_dev *pci,
864 struct skl_dev **rskl)
865 {
866 struct hdac_ext_bus_ops *ext_ops = NULL;
867 struct skl_dev *skl;
868 struct hdac_bus *bus;
869 struct hda_bus *hbus;
870 int err;
871
872 *rskl = NULL;
873
874 err = pci_enable_device(pci);
875 if (err < 0)
876 return err;
877
878 skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
879 if (!skl) {
880 pci_disable_device(pci);
881 return -ENOMEM;
882 }
883
884 hbus = skl_to_hbus(skl);
885 bus = skl_to_bus(skl);
886
887 INIT_LIST_HEAD(&skl->ppl_list);
888 INIT_LIST_HEAD(&skl->bind_list);
889
890 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
891 ext_ops = snd_soc_hdac_hda_get_ops();
892 #endif
893 snd_hdac_ext_bus_init(bus, &pci->dev, &bus_core_ops, ext_ops);
894 bus->use_posbuf = 1;
895 skl->pci = pci;
896 INIT_WORK(&skl->probe_work, skl_probe_work);
897 bus->bdl_pos_adj = 0;
898
899 mutex_init(&hbus->prepare_mutex);
900 hbus->pci = pci;
901 hbus->mixer_assigned = -1;
902 hbus->modelname = "sklbus";
903
904 *rskl = skl;
905
906 return 0;
907 }
908
skl_first_init(struct hdac_bus * bus)909 static int skl_first_init(struct hdac_bus *bus)
910 {
911 struct skl_dev *skl = bus_to_skl(bus);
912 struct pci_dev *pci = skl->pci;
913 int err;
914 unsigned short gcap;
915 int cp_streams, pb_streams, start_idx;
916
917 err = pci_request_regions(pci, "Skylake HD audio");
918 if (err < 0)
919 return err;
920
921 bus->addr = pci_resource_start(pci, 0);
922 bus->remap_addr = pci_ioremap_bar(pci, 0);
923 if (bus->remap_addr == NULL) {
924 dev_err(bus->dev, "ioremap error\n");
925 return -ENXIO;
926 }
927
928 snd_hdac_bus_parse_capabilities(bus);
929
930 /* check if PPCAP exists */
931 if (!bus->ppcap) {
932 dev_err(bus->dev, "bus ppcap not set, HDaudio or DSP not present?\n");
933 return -ENODEV;
934 }
935
936 if (skl_acquire_irq(bus, 0) < 0)
937 return -EBUSY;
938
939 pci_set_master(pci);
940 synchronize_irq(bus->irq);
941
942 gcap = snd_hdac_chip_readw(bus, GCAP);
943 dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
944
945 /* read number of streams from GCAP register */
946 cp_streams = (gcap >> 8) & 0x0f;
947 pb_streams = (gcap >> 12) & 0x0f;
948
949 if (!pb_streams && !cp_streams) {
950 dev_err(bus->dev, "no streams found in GCAP definitions?\n");
951 return -EIO;
952 }
953
954 bus->num_streams = cp_streams + pb_streams;
955
956 /* allow 64bit DMA address if supported by H/W */
957 if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
958 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
959 } else {
960 dma_set_mask(bus->dev, DMA_BIT_MASK(32));
961 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
962 }
963
964 /* initialize streams */
965 snd_hdac_ext_stream_init_all
966 (bus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
967 start_idx = cp_streams;
968 snd_hdac_ext_stream_init_all
969 (bus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
970
971 err = snd_hdac_bus_alloc_stream_pages(bus);
972 if (err < 0)
973 return err;
974
975 return 0;
976 }
977
skl_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)978 static int skl_probe(struct pci_dev *pci,
979 const struct pci_device_id *pci_id)
980 {
981 struct skl_dev *skl;
982 struct hdac_bus *bus = NULL;
983 int err;
984
985 switch (skl_pci_binding) {
986 case SND_SKL_PCI_BIND_AUTO:
987 /*
988 * detect DSP by checking class/subclass/prog-id information
989 * class=04 subclass 03 prog-if 00: no DSP, use legacy driver
990 * class=04 subclass 01 prog-if 00: DSP is present
991 * (and may be required e.g. for DMIC or SSP support)
992 * class=04 subclass 03 prog-if 80: use DSP or legacy mode
993 */
994 if (pci->class == 0x040300) {
995 dev_info(&pci->dev, "The DSP is not enabled on this platform, aborting probe\n");
996 return -ENODEV;
997 }
998 if (pci->class != 0x040100 && pci->class != 0x040380) {
999 dev_err(&pci->dev, "Unknown PCI class/subclass/prog-if information (0x%06x) found, aborting probe\n", pci->class);
1000 return -ENODEV;
1001 }
1002 dev_info(&pci->dev, "DSP detected with PCI class/subclass/prog-if info 0x%06x\n", pci->class);
1003 break;
1004 case SND_SKL_PCI_BIND_LEGACY:
1005 dev_info(&pci->dev, "Module parameter forced binding with HDaudio legacy, aborting probe\n");
1006 return -ENODEV;
1007 case SND_SKL_PCI_BIND_ASOC:
1008 dev_info(&pci->dev, "Module parameter forced binding with SKL driver, bypassed detection logic\n");
1009 break;
1010 default:
1011 dev_err(&pci->dev, "invalid value for skl_pci_binding module parameter, ignored\n");
1012 break;
1013 }
1014
1015 /* we use ext core ops, so provide NULL for ops here */
1016 err = skl_create(pci, &skl);
1017 if (err < 0)
1018 return err;
1019
1020 bus = skl_to_bus(skl);
1021
1022 err = skl_first_init(bus);
1023 if (err < 0) {
1024 dev_err(bus->dev, "skl_first_init failed with err: %d\n", err);
1025 goto out_free;
1026 }
1027
1028 skl->pci_id = pci->device;
1029
1030 device_disable_async_suspend(bus->dev);
1031
1032 skl->nhlt = intel_nhlt_init(bus->dev);
1033
1034 if (skl->nhlt == NULL) {
1035 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_SKYLAKE_HDAUDIO_CODEC)
1036 dev_err(bus->dev, "no nhlt info found\n");
1037 err = -ENODEV;
1038 goto out_free;
1039 #else
1040 dev_warn(bus->dev, "no nhlt info found, continuing to try to enable HDaudio codec\n");
1041 #endif
1042 } else {
1043
1044 err = skl_nhlt_create_sysfs(skl);
1045 if (err < 0) {
1046 dev_err(bus->dev, "skl_nhlt_create_sysfs failed with err: %d\n", err);
1047 goto out_nhlt_free;
1048 }
1049
1050 skl_nhlt_update_topology_bin(skl);
1051
1052 /* create device for dsp clk */
1053 err = skl_clock_device_register(skl);
1054 if (err < 0) {
1055 dev_err(bus->dev, "skl_clock_device_register failed with err: %d\n", err);
1056 goto out_clk_free;
1057 }
1058 }
1059
1060 pci_set_drvdata(skl->pci, bus);
1061
1062
1063 err = skl_find_machine(skl, (void *)pci_id->driver_data);
1064 if (err < 0) {
1065 dev_err(bus->dev, "skl_find_machine failed with err: %d\n", err);
1066 goto out_nhlt_free;
1067 }
1068
1069 err = skl_init_dsp(skl);
1070 if (err < 0) {
1071 dev_dbg(bus->dev, "error failed to register dsp\n");
1072 goto out_nhlt_free;
1073 }
1074 skl->enable_miscbdcge = skl_enable_miscbdcge;
1075 skl->clock_power_gating = skl_clock_power_gating;
1076
1077 if (bus->mlcap)
1078 snd_hdac_ext_bus_get_ml_capabilities(bus);
1079
1080 /* create device for soc dmic */
1081 err = skl_dmic_device_register(skl);
1082 if (err < 0) {
1083 dev_err(bus->dev, "skl_dmic_device_register failed with err: %d\n", err);
1084 goto out_dsp_free;
1085 }
1086
1087 schedule_work(&skl->probe_work);
1088
1089 return 0;
1090
1091 out_dsp_free:
1092 skl_free_dsp(skl);
1093 out_clk_free:
1094 skl_clock_device_unregister(skl);
1095 out_nhlt_free:
1096 if (skl->nhlt)
1097 intel_nhlt_free(skl->nhlt);
1098 out_free:
1099 skl_free(bus);
1100
1101 return err;
1102 }
1103
skl_shutdown(struct pci_dev * pci)1104 static void skl_shutdown(struct pci_dev *pci)
1105 {
1106 struct hdac_bus *bus = pci_get_drvdata(pci);
1107 struct hdac_stream *s;
1108 struct hdac_ext_stream *stream;
1109 struct skl_dev *skl;
1110
1111 if (!bus)
1112 return;
1113
1114 skl = bus_to_skl(bus);
1115
1116 if (!skl->init_done)
1117 return;
1118
1119 snd_hdac_stop_streams(bus);
1120 snd_hdac_ext_bus_link_power_down_all(bus);
1121 skl_dsp_sleep(skl->dsp);
1122
1123 list_for_each_entry(s, &bus->stream_list, list) {
1124 stream = stream_to_hdac_ext_stream(s);
1125 snd_hdac_ext_stream_decouple(bus, stream, false);
1126 }
1127
1128 snd_hdac_bus_stop_chip(bus);
1129 }
1130
skl_remove(struct pci_dev * pci)1131 static void skl_remove(struct pci_dev *pci)
1132 {
1133 struct hdac_bus *bus = pci_get_drvdata(pci);
1134 struct skl_dev *skl = bus_to_skl(bus);
1135
1136 cancel_work_sync(&skl->probe_work);
1137
1138 pm_runtime_get_noresume(&pci->dev);
1139
1140 /* codec removal, invoke bus_device_remove */
1141 snd_hdac_ext_bus_device_remove(bus);
1142
1143 skl_platform_unregister(&pci->dev);
1144 skl_free_dsp(skl);
1145 skl_machine_device_unregister(skl);
1146 skl_dmic_device_unregister(skl);
1147 skl_clock_device_unregister(skl);
1148 skl_nhlt_remove_sysfs(skl);
1149 if (skl->nhlt)
1150 intel_nhlt_free(skl->nhlt);
1151 skl_free(bus);
1152 dev_set_drvdata(&pci->dev, NULL);
1153 }
1154
1155 /* PCI IDs */
1156 static const struct pci_device_id skl_ids[] = {
1157 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_SKL)
1158 /* Sunrise Point-LP */
1159 { PCI_DEVICE(0x8086, 0x9d70),
1160 .driver_data = (unsigned long)&snd_soc_acpi_intel_skl_machines},
1161 #endif
1162 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_APL)
1163 /* BXT-P */
1164 { PCI_DEVICE(0x8086, 0x5a98),
1165 .driver_data = (unsigned long)&snd_soc_acpi_intel_bxt_machines},
1166 #endif
1167 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_KBL)
1168 /* KBL */
1169 { PCI_DEVICE(0x8086, 0x9D71),
1170 .driver_data = (unsigned long)&snd_soc_acpi_intel_kbl_machines},
1171 #endif
1172 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_GLK)
1173 /* GLK */
1174 { PCI_DEVICE(0x8086, 0x3198),
1175 .driver_data = (unsigned long)&snd_soc_acpi_intel_glk_machines},
1176 #endif
1177 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CNL)
1178 /* CNL */
1179 { PCI_DEVICE(0x8086, 0x9dc8),
1180 .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
1181 #endif
1182 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CFL)
1183 /* CFL */
1184 { PCI_DEVICE(0x8086, 0xa348),
1185 .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
1186 #endif
1187 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CML_LP)
1188 /* CML-LP */
1189 { PCI_DEVICE(0x8086, 0x02c8),
1190 .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
1191 #endif
1192 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CML_H)
1193 /* CML-H */
1194 { PCI_DEVICE(0x8086, 0x06c8),
1195 .driver_data = (unsigned long)&snd_soc_acpi_intel_cnl_machines},
1196 #endif
1197 { 0, }
1198 };
1199 MODULE_DEVICE_TABLE(pci, skl_ids);
1200
1201 /* pci_driver definition */
1202 static struct pci_driver skl_driver = {
1203 .name = KBUILD_MODNAME,
1204 .id_table = skl_ids,
1205 .probe = skl_probe,
1206 .remove = skl_remove,
1207 .shutdown = skl_shutdown,
1208 .driver = {
1209 .pm = &skl_pm,
1210 },
1211 };
1212 module_pci_driver(skl_driver);
1213
1214 MODULE_LICENSE("GPL v2");
1215 MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");
1216