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