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