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