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