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