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