blob: 9e3f8c04dd322bd0d3e21fe5b57dfc89ef4eccce [file] [log] [blame]
Jeeja KPd8c2dab2015-07-09 15:20:09 +05301/*
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>
Vinod Kould8018362016-01-05 17:16:04 +053028#include <linux/firmware.h>
Pardha Saradhi Ka26a3f52016-11-03 17:07:16 +053029#include <linux/delay.h>
Jeeja KPd8c2dab2015-07-09 15:20:09 +053030#include <sound/pcm.h>
Vinod Koulcc18c5f2015-11-05 21:34:13 +053031#include "../common/sst-acpi.h"
Vinod Koul6980c052016-02-17 21:34:06 +053032#include <sound/hda_register.h>
33#include <sound/hdaudio.h>
34#include <sound/hda_i915.h>
Jeeja KPd8c2dab2015-07-09 15:20:09 +053035#include "skl.h"
Jayachandran B0c8ba9d2015-12-18 15:12:03 +053036#include "skl-sst-dsp.h"
37#include "skl-sst-ipc.h"
Jeeja KPd8c2dab2015-07-09 15:20:09 +053038
Yong Zhif65cf7d62016-05-26 21:30:15 -070039static struct skl_machine_pdata skl_dmic_data;
40
Jeeja KPd8c2dab2015-07-09 15:20:09 +053041/*
42 * initialize the PCI registers
43 */
44static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg,
45 unsigned char mask, unsigned char val)
46{
47 unsigned char data;
48
49 pci_read_config_byte(pci, reg, &data);
50 data &= ~mask;
51 data |= (val & mask);
52 pci_write_config_byte(pci, reg, data);
53}
54
55static void skl_init_pci(struct skl *skl)
56{
57 struct hdac_ext_bus *ebus = &skl->ebus;
58
59 /*
60 * Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
61 * TCSEL == Traffic Class Select Register, which sets PCI express QOS
62 * Ensuring these bits are 0 clears playback static on some HD Audio
63 * codecs.
64 * The PCI register TCSEL is defined in the Intel manuals.
65 */
66 dev_dbg(ebus_to_hbus(ebus)->dev, "Clearing TCSEL\n");
67 skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
68}
69
Jayachandran B0c8ba9d2015-12-18 15:12:03 +053070static void update_pci_dword(struct pci_dev *pci,
71 unsigned int reg, u32 mask, u32 val)
72{
73 u32 data = 0;
74
75 pci_read_config_dword(pci, reg, &data);
76 data &= ~mask;
77 data |= (val & mask);
78 pci_write_config_dword(pci, reg, data);
79}
80
81/*
82 * skl_enable_miscbdcge - enable/dsiable CGCTL.MISCBDCGE bits
83 *
84 * @dev: device pointer
85 * @enable: enable/disable flag
86 */
87static void skl_enable_miscbdcge(struct device *dev, bool enable)
88{
89 struct pci_dev *pci = to_pci_dev(dev);
90 u32 val;
91
92 val = enable ? AZX_CGCTL_MISCBDCGE_MASK : 0;
93
94 update_pci_dword(pci, AZX_PCIREG_CGCTL, AZX_CGCTL_MISCBDCGE_MASK, val);
95}
96
97/*
98 * While performing reset, controller may not come back properly causing
99 * issues, so recommendation is to set CGCTL.MISCBDCGE to 0 then do reset
100 * (init chip) and then again set CGCTL.MISCBDCGE to 1
101 */
102static int skl_init_chip(struct hdac_bus *bus, bool full_reset)
103{
104 int ret;
105
106 skl_enable_miscbdcge(bus->dev, false);
107 ret = snd_hdac_bus_init_chip(bus, full_reset);
108 skl_enable_miscbdcge(bus->dev, true);
109
110 return ret;
111}
112
Pardha Saradhi Ka26a3f52016-11-03 17:07:16 +0530113void skl_update_d0i3c(struct device *dev, bool enable)
114{
115 struct pci_dev *pci = to_pci_dev(dev);
116 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
117 struct hdac_bus *bus = ebus_to_hbus(ebus);
118 u8 reg;
119 int timeout = 50;
120
121 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
122 /* Do not write to D0I3C until command in progress bit is cleared */
123 while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
124 udelay(10);
125 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
126 }
127
128 /* Highly unlikely. But if it happens, flag error explicitly */
129 if (!timeout) {
130 dev_err(bus->dev, "Before D0I3C update: D0I3C CIP timeout\n");
131 return;
132 }
133
134 if (enable)
135 reg = reg | AZX_REG_VS_D0I3C_I3;
136 else
137 reg = reg & (~AZX_REG_VS_D0I3C_I3);
138
139 snd_hdac_chip_writeb(bus, VS_D0I3C, reg);
140
141 timeout = 50;
142 /* Wait for cmd in progress to be cleared before exiting the function */
143 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
144 while ((reg & AZX_REG_VS_D0I3C_CIP) && --timeout) {
145 udelay(10);
146 reg = snd_hdac_chip_readb(bus, VS_D0I3C);
147 }
148
149 /* Highly unlikely. But if it happens, flag error explicitly */
150 if (!timeout) {
151 dev_err(bus->dev, "After D0I3C update: D0I3C CIP timeout\n");
152 return;
153 }
154
155 dev_dbg(bus->dev, "D0I3C register = 0x%x\n",
156 snd_hdac_chip_readb(bus, VS_D0I3C));
157}
158
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530159/* called from IRQ */
160static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
161{
162 snd_pcm_period_elapsed(hstr->substream);
163}
164
165static irqreturn_t skl_interrupt(int irq, void *dev_id)
166{
167 struct hdac_ext_bus *ebus = dev_id;
168 struct hdac_bus *bus = ebus_to_hbus(ebus);
169 u32 status;
170
171 if (!pm_runtime_active(bus->dev))
172 return IRQ_NONE;
173
174 spin_lock(&bus->reg_lock);
175
176 status = snd_hdac_chip_readl(bus, INTSTS);
177 if (status == 0 || status == 0xffffffff) {
178 spin_unlock(&bus->reg_lock);
179 return IRQ_NONE;
180 }
181
182 /* clear rirb int */
183 status = snd_hdac_chip_readb(bus, RIRBSTS);
184 if (status & RIRB_INT_MASK) {
185 if (status & RIRB_INT_RESPONSE)
186 snd_hdac_bus_update_rirb(bus);
187 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
188 }
189
190 spin_unlock(&bus->reg_lock);
191
192 return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
193}
194
195static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
196{
197 struct hdac_ext_bus *ebus = dev_id;
198 struct hdac_bus *bus = ebus_to_hbus(ebus);
199 u32 status;
200
201 status = snd_hdac_chip_readl(bus, INTSTS);
202
203 snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
204
205 return IRQ_HANDLED;
206}
207
208static int skl_acquire_irq(struct hdac_ext_bus *ebus, int do_disconnect)
209{
210 struct skl *skl = ebus_to_skl(ebus);
211 struct hdac_bus *bus = ebus_to_hbus(ebus);
212 int ret;
213
214 ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
215 skl_threaded_handler,
216 IRQF_SHARED,
217 KBUILD_MODNAME, ebus);
218 if (ret) {
219 dev_err(bus->dev,
220 "unable to grab IRQ %d, disabling device\n",
221 skl->pci->irq);
222 return ret;
223 }
224
225 bus->irq = skl->pci->irq;
226 pci_intx(skl->pci, 1);
227
228 return 0;
229}
230
Jayachandran B8b4a1332016-11-03 17:07:21 +0530231static int skl_suspend_late(struct device *dev)
232{
233 struct pci_dev *pci = to_pci_dev(dev);
234 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
235 struct skl *skl = ebus_to_skl(ebus);
236
237 return skl_suspend_late_dsp(skl);
238}
239
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530240#ifdef CONFIG_PM
Jeeja KP61722f42015-10-27 09:23:00 +0900241static int _skl_suspend(struct hdac_ext_bus *ebus)
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530242{
Jeeja KP2a29b202015-10-07 11:31:58 +0100243 struct skl *skl = ebus_to_skl(ebus);
Jeeja KP61722f42015-10-27 09:23:00 +0900244 struct hdac_bus *bus = ebus_to_hbus(ebus);
Dharageswari R51a01b82016-06-03 18:29:37 +0530245 struct pci_dev *pci = to_pci_dev(bus->dev);
Jeeja KP2a29b202015-10-07 11:31:58 +0100246 int ret;
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530247
Jeeja KP01bb84b2015-10-09 09:01:51 +0100248 snd_hdac_ext_bus_link_power_down_all(ebus);
249
Jeeja KP2a29b202015-10-07 11:31:58 +0100250 ret = skl_suspend_dsp(skl);
251 if (ret < 0)
252 return ret;
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530253
254 snd_hdac_bus_stop_chip(bus);
Dharageswari R51a01b82016-06-03 18:29:37 +0530255 update_pci_dword(pci, AZX_PCIREG_PGCTL,
256 AZX_PGCTL_LSRMD_MASK, AZX_PGCTL_LSRMD_MASK);
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530257 skl_enable_miscbdcge(bus->dev, false);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530258 snd_hdac_bus_enter_link_reset(bus);
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530259 skl_enable_miscbdcge(bus->dev, true);
Dharageswari Rfe3f4442016-06-03 18:29:39 +0530260 skl_cleanup_resources(skl);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530261
262 return 0;
263}
264
Jeeja KP61722f42015-10-27 09:23:00 +0900265static int _skl_resume(struct hdac_ext_bus *ebus)
266{
267 struct skl *skl = ebus_to_skl(ebus);
268 struct hdac_bus *bus = ebus_to_hbus(ebus);
269
270 skl_init_pci(skl);
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530271 skl_init_chip(bus, true);
Jeeja KP61722f42015-10-27 09:23:00 +0900272
273 return skl_resume_dsp(skl);
274}
275#endif
276
277#ifdef CONFIG_PM_SLEEP
278/*
279 * power management
280 */
281static int skl_suspend(struct device *dev)
282{
283 struct pci_dev *pci = to_pci_dev(dev);
284 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
Jeeja KP4557c302015-12-03 23:30:00 +0530285 struct skl *skl = ebus_to_skl(ebus);
Jeeja KP1f4956f2015-12-18 15:12:06 +0530286 struct hdac_bus *bus = ebus_to_hbus(ebus);
Subhransu S. Prustyaf037412016-04-01 13:36:27 +0530287 int ret = 0;
Jeeja KP61722f42015-10-27 09:23:00 +0900288
Jeeja KP4557c302015-12-03 23:30:00 +0530289 /*
290 * Do not suspend if streams which are marked ignore suspend are
291 * running, we need to save the state for these and continue
292 */
293 if (skl->supend_active) {
Vinod Koulcce6c142016-05-12 08:58:54 +0530294 /* turn off the links and stop the CORB/RIRB DMA if it is On */
Vinod Koulc2e20cd2015-12-18 15:12:05 +0530295 snd_hdac_ext_bus_link_power_down_all(ebus);
Vinod Koulcce6c142016-05-12 08:58:54 +0530296
297 if (ebus->cmd_dma_state)
298 snd_hdac_bus_stop_cmd_io(&ebus->bus);
299
Jeeja KP1f4956f2015-12-18 15:12:06 +0530300 enable_irq_wake(bus->irq);
Jeeja KP4557c302015-12-03 23:30:00 +0530301 pci_save_state(pci);
Jeeja KP4557c302015-12-03 23:30:00 +0530302 } else {
Subhransu S. Prustyaf037412016-04-01 13:36:27 +0530303 ret = _skl_suspend(ebus);
304 if (ret < 0)
305 return ret;
Jayachandran B1665c172016-06-13 17:59:01 +0530306 skl->skl_sst->fw_loaded = false;
Jeeja KP4557c302015-12-03 23:30:00 +0530307 }
Subhransu S. Prustyaf037412016-04-01 13:36:27 +0530308
309 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
310 ret = snd_hdac_display_power(bus, false);
311 if (ret < 0)
312 dev_err(bus->dev,
313 "Cannot turn OFF display power on i915\n");
314 }
315
316 return ret;
Jeeja KP61722f42015-10-27 09:23:00 +0900317}
318
319static int skl_resume(struct device *dev)
320{
321 struct pci_dev *pci = to_pci_dev(dev);
322 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
Jeeja KP4557c302015-12-03 23:30:00 +0530323 struct skl *skl = ebus_to_skl(ebus);
Jeeja KP1f4956f2015-12-18 15:12:06 +0530324 struct hdac_bus *bus = ebus_to_hbus(ebus);
Vinod Koulcce6c142016-05-12 08:58:54 +0530325 struct hdac_ext_link *hlink = NULL;
Jeeja KP4557c302015-12-03 23:30:00 +0530326 int ret;
Jeeja KP61722f42015-10-27 09:23:00 +0900327
Vinod Koul6980c052016-02-17 21:34:06 +0530328 /* Turned OFF in HDMI codec driver after codec reconfiguration */
329 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
330 ret = snd_hdac_display_power(bus, true);
331 if (ret < 0) {
332 dev_err(bus->dev,
333 "Cannot turn on display power on i915\n");
334 return ret;
335 }
336 }
337
Jeeja KP4557c302015-12-03 23:30:00 +0530338 /*
339 * resume only when we are not in suspend active, otherwise need to
340 * restore the device
341 */
342 if (skl->supend_active) {
343 pci_restore_state(pci);
Vinod Koulc2e20cd2015-12-18 15:12:05 +0530344 snd_hdac_ext_bus_link_power_up_all(ebus);
Jeeja KP1f4956f2015-12-18 15:12:06 +0530345 disable_irq_wake(bus->irq);
Vinod Koulcce6c142016-05-12 08:58:54 +0530346 /*
347 * turn On the links which are On before active suspend
348 * and start the CORB/RIRB DMA if On before
349 * active suspend.
350 */
351 list_for_each_entry(hlink, &ebus->hlink_list, list) {
352 if (hlink->ref_count)
353 snd_hdac_ext_bus_link_power_up(hlink);
354 }
355
356 if (ebus->cmd_dma_state)
357 snd_hdac_bus_init_cmd_io(&ebus->bus);
Jeeja KP4557c302015-12-03 23:30:00 +0530358 } else {
359 ret = _skl_resume(ebus);
Vinod Koulcce6c142016-05-12 08:58:54 +0530360
361 /* turn off the links which are off before suspend */
362 list_for_each_entry(hlink, &ebus->hlink_list, list) {
363 if (!hlink->ref_count)
364 snd_hdac_ext_bus_link_power_down(hlink);
365 }
366
367 if (!ebus->cmd_dma_state)
368 snd_hdac_bus_stop_cmd_io(&ebus->bus);
Jeeja KP4557c302015-12-03 23:30:00 +0530369 }
370
371 return ret;
Jeeja KP61722f42015-10-27 09:23:00 +0900372}
373#endif /* CONFIG_PM_SLEEP */
374
375#ifdef CONFIG_PM
376static int skl_runtime_suspend(struct device *dev)
377{
378 struct pci_dev *pci = to_pci_dev(dev);
379 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
380 struct hdac_bus *bus = ebus_to_hbus(ebus);
381
382 dev_dbg(bus->dev, "in %s\n", __func__);
383
Jeeja KP61722f42015-10-27 09:23:00 +0900384 return _skl_suspend(ebus);
385}
386
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530387static int skl_runtime_resume(struct device *dev)
388{
389 struct pci_dev *pci = to_pci_dev(dev);
390 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
391 struct hdac_bus *bus = ebus_to_hbus(ebus);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530392
393 dev_dbg(bus->dev, "in %s\n", __func__);
394
Jeeja KP61722f42015-10-27 09:23:00 +0900395 return _skl_resume(ebus);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530396}
397#endif /* CONFIG_PM */
398
399static const struct dev_pm_ops skl_pm = {
400 SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
401 SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
Jayachandran B8b4a1332016-11-03 17:07:21 +0530402 .suspend_late = skl_suspend_late,
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530403};
404
405/*
406 * destructor
407 */
408static int skl_free(struct hdac_ext_bus *ebus)
409{
410 struct skl *skl = ebus_to_skl(ebus);
411 struct hdac_bus *bus = ebus_to_hbus(ebus);
412
Vinod Koulab1b7322017-05-04 14:09:10 +0530413 skl->init_done = 0; /* to be sure */
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530414
415 snd_hdac_ext_stop_streams(ebus);
416
417 if (bus->irq >= 0)
418 free_irq(bus->irq, (void *)bus);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530419 snd_hdac_bus_free_stream_pages(bus);
420 snd_hdac_stream_free_all(ebus);
421 snd_hdac_link_free_all(ebus);
Vinod Koul077411e2016-03-15 16:39:26 +0530422
423 if (bus->remap_addr)
424 iounmap(bus->remap_addr);
425
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530426 pci_release_regions(skl->pci);
427 pci_disable_device(skl->pci);
428
429 snd_hdac_ext_bus_exit(ebus);
430
Vinod Koulab1b7322017-05-04 14:09:10 +0530431 cancel_work_sync(&skl->probe_work);
Vinod Koul5b2fe892016-03-15 16:39:27 +0530432 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
433 snd_hdac_i915_exit(&ebus->bus);
Vinod Koulab1b7322017-05-04 14:09:10 +0530434
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530435 return 0;
436}
437
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530438static int skl_machine_device_register(struct skl *skl, void *driver_data)
439{
440 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
441 struct platform_device *pdev;
442 struct sst_acpi_mach *mach = driver_data;
443 int ret;
444
445 mach = sst_acpi_find_machine(mach);
446 if (mach == NULL) {
447 dev_err(bus->dev, "No matching machine driver found\n");
448 return -ENODEV;
449 }
Vinod Koulaecf6fd2015-11-05 21:34:15 +0530450 skl->fw_name = mach->fw_filename;
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530451
452 pdev = platform_device_alloc(mach->drv_name, -1);
453 if (pdev == NULL) {
454 dev_err(bus->dev, "platform device alloc failed\n");
455 return -EIO;
456 }
457
458 ret = platform_device_add(pdev);
459 if (ret) {
460 dev_err(bus->dev, "failed to add machine device\n");
461 platform_device_put(pdev);
462 return -EIO;
463 }
Yong Zhif65cf7d62016-05-26 21:30:15 -0700464
465 if (mach->pdata)
466 dev_set_drvdata(&pdev->dev, mach->pdata);
467
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530468 skl->i2s_dev = pdev;
469
470 return 0;
471}
472
473static void skl_machine_device_unregister(struct skl *skl)
474{
475 if (skl->i2s_dev)
476 platform_device_unregister(skl->i2s_dev);
477}
478
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530479static int skl_dmic_device_register(struct skl *skl)
480{
481 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
482 struct platform_device *pdev;
483 int ret;
484
485 /* SKL has one dmic port, so allocate dmic device for this */
486 pdev = platform_device_alloc("dmic-codec", -1);
487 if (!pdev) {
488 dev_err(bus->dev, "failed to allocate dmic device\n");
489 return -ENOMEM;
490 }
491
492 ret = platform_device_add(pdev);
493 if (ret) {
494 dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
495 platform_device_put(pdev);
496 return ret;
497 }
498 skl->dmic_dev = pdev;
499
500 return 0;
501}
502
503static void skl_dmic_device_unregister(struct skl *skl)
504{
505 if (skl->dmic_dev)
506 platform_device_unregister(skl->dmic_dev);
507}
508
509/*
510 * Probe the given codec address
511 */
512static int probe_codec(struct hdac_ext_bus *ebus, int addr)
513{
514 struct hdac_bus *bus = ebus_to_hbus(ebus);
515 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
516 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
Dan Carpentere6a33532017-04-20 13:17:02 +0300517 unsigned int res = -1;
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530518
519 mutex_lock(&bus->cmd_mutex);
520 snd_hdac_bus_send_cmd(bus, cmd);
521 snd_hdac_bus_get_response(bus, addr, &res);
522 mutex_unlock(&bus->cmd_mutex);
523 if (res == -1)
524 return -EIO;
525 dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
526
527 return snd_hdac_ext_bus_device_init(ebus, addr);
528}
529
530/* Codec initialization */
531static int skl_codec_create(struct hdac_ext_bus *ebus)
532{
533 struct hdac_bus *bus = ebus_to_hbus(ebus);
534 int c, max_slots;
535
536 max_slots = HDA_MAX_CODECS;
537
538 /* First try to probe all given codec slots */
539 for (c = 0; c < max_slots; c++) {
540 if ((bus->codec_mask & (1 << c))) {
541 if (probe_codec(ebus, c) < 0) {
542 /*
543 * Some BIOSen give you wrong codec addresses
544 * that don't exist
545 */
546 dev_warn(bus->dev,
547 "Codec #%d probe error; disabling it...\n", c);
548 bus->codec_mask &= ~(1 << c);
549 /*
550 * More badly, accessing to a non-existing
551 * codec often screws up the controller bus,
552 * and disturbs the further communications.
553 * Thus if an error occurs during probing,
554 * better to reset the controller bus to get
555 * back to the sanity state.
556 */
557 snd_hdac_bus_stop_chip(bus);
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530558 skl_init_chip(bus, true);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530559 }
560 }
561 }
562
563 return 0;
564}
565
566static const struct hdac_bus_ops bus_core_ops = {
567 .command = snd_hdac_bus_send_cmd,
568 .get_response = snd_hdac_bus_get_response,
569};
570
Vinod Koulab1b7322017-05-04 14:09:10 +0530571static int skl_i915_init(struct hdac_bus *bus)
572{
573 int err;
574
575 /*
576 * The HDMI codec is in GPU so we need to ensure that it is powered
577 * up and ready for probe
578 */
579 err = snd_hdac_i915_init(bus);
580 if (err < 0)
581 return err;
582
583 err = snd_hdac_display_power(bus, true);
584 if (err < 0)
585 dev_err(bus->dev, "Cannot turn on display power on i915\n");
586
587 return err;
588}
589
590static void skl_probe_work(struct work_struct *work)
591{
592 struct skl *skl = container_of(work, struct skl, probe_work);
593 struct hdac_ext_bus *ebus = &skl->ebus;
594 struct hdac_bus *bus = ebus_to_hbus(ebus);
595 struct hdac_ext_link *hlink = NULL;
596 int err;
597
598 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
599 err = skl_i915_init(bus);
600 if (err < 0)
601 return;
602 }
603
604 err = skl_init_chip(bus, true);
605 if (err < 0) {
606 dev_err(bus->dev, "Init chip failed with err: %d\n", err);
607 goto out_err;
608 }
609
610 /* codec detection */
611 if (!bus->codec_mask)
612 dev_info(bus->dev, "no hda codecs found!\n");
613
614 /* create codec instances */
615 err = skl_codec_create(ebus);
616 if (err < 0)
617 goto out_err;
618
619 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
620 err = snd_hdac_display_power(bus, false);
621 if (err < 0) {
622 dev_err(bus->dev, "Cannot turn off display power on i915\n");
623 return;
624 }
625 }
626
627 /* register platform dai and controls */
628 err = skl_platform_register(bus->dev);
629 if (err < 0)
630 return;
631 /*
632 * we are done probing so decrement link counts
633 */
634 list_for_each_entry(hlink, &ebus->hlink_list, list)
635 snd_hdac_ext_bus_link_put(ebus, hlink);
636
637 /* configure PM */
638 pm_runtime_put_noidle(bus->dev);
639 pm_runtime_allow(bus->dev);
640 skl->init_done = 1;
641
642 return;
643
644out_err:
645 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
646 err = snd_hdac_display_power(bus, false);
647}
648
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530649/*
650 * constructor
651 */
652static int skl_create(struct pci_dev *pci,
653 const struct hdac_io_ops *io_ops,
654 struct skl **rskl)
655{
656 struct skl *skl;
657 struct hdac_ext_bus *ebus;
658
659 int err;
660
661 *rskl = NULL;
662
663 err = pci_enable_device(pci);
664 if (err < 0)
665 return err;
666
667 skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
668 if (!skl) {
669 pci_disable_device(pci);
670 return -ENOMEM;
671 }
672 ebus = &skl->ebus;
673 snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
674 ebus->bus.use_posbuf = 1;
675 skl->pci = pci;
Vinod Koulab1b7322017-05-04 14:09:10 +0530676 INIT_WORK(&skl->probe_work, skl_probe_work);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530677
678 ebus->bus.bdl_pos_adj = 0;
679
680 *rskl = skl;
681
682 return 0;
683}
684
685static int skl_first_init(struct hdac_ext_bus *ebus)
686{
687 struct skl *skl = ebus_to_skl(ebus);
688 struct hdac_bus *bus = ebus_to_hbus(ebus);
689 struct pci_dev *pci = skl->pci;
690 int err;
691 unsigned short gcap;
692 int cp_streams, pb_streams, start_idx;
693
694 err = pci_request_regions(pci, "Skylake HD audio");
695 if (err < 0)
696 return err;
697
698 bus->addr = pci_resource_start(pci, 0);
699 bus->remap_addr = pci_ioremap_bar(pci, 0);
700 if (bus->remap_addr == NULL) {
701 dev_err(bus->dev, "ioremap error\n");
702 return -ENXIO;
703 }
704
Vinod Koulec8ae572016-08-04 15:46:01 +0530705 snd_hdac_bus_parse_capabilities(bus);
Jeeja KP05057002015-07-09 15:20:11 +0530706
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530707 if (skl_acquire_irq(ebus, 0) < 0)
708 return -EBUSY;
709
710 pci_set_master(pci);
711 synchronize_irq(bus->irq);
712
713 gcap = snd_hdac_chip_readw(bus, GCAP);
714 dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
715
716 /* allow 64bit DMA address if supported by H/W */
717 if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
718 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
719 } else {
720 dma_set_mask(bus->dev, DMA_BIT_MASK(32));
721 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
722 }
723
724 /* read number of streams from GCAP register */
725 cp_streams = (gcap >> 8) & 0x0f;
726 pb_streams = (gcap >> 12) & 0x0f;
727
728 if (!pb_streams && !cp_streams)
729 return -EIO;
730
731 ebus->num_streams = cp_streams + pb_streams;
732
733 /* initialize streams */
734 snd_hdac_ext_stream_init_all
735 (ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
736 start_idx = cp_streams;
737 snd_hdac_ext_stream_init_all
738 (ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
739
740 err = snd_hdac_bus_alloc_stream_pages(bus);
741 if (err < 0)
742 return err;
743
744 /* initialize chip */
745 skl_init_pci(skl);
746
Vinod Koulab1b7322017-05-04 14:09:10 +0530747 return skl_init_chip(bus, true);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530748}
749
750static int skl_probe(struct pci_dev *pci,
751 const struct pci_device_id *pci_id)
752{
753 struct skl *skl;
754 struct hdac_ext_bus *ebus = NULL;
755 struct hdac_bus *bus = NULL;
756 int err;
757
758 /* we use ext core ops, so provide NULL for ops here */
759 err = skl_create(pci, NULL, &skl);
760 if (err < 0)
761 return err;
762
763 ebus = &skl->ebus;
764 bus = ebus_to_hbus(ebus);
765
766 err = skl_first_init(ebus);
767 if (err < 0)
768 goto out_free;
769
Vinod Koul4b235c42016-02-19 11:42:34 +0530770 skl->pci_id = pci->device;
771
Vinod Koul2e9dc2b2016-06-22 19:44:19 +0530772 device_disable_async_suspend(bus->dev);
773
Jeeja KP87b2bdf2015-10-07 11:31:59 +0100774 skl->nhlt = skl_nhlt_init(bus->dev);
775
Wei Yongjun979cf592016-08-12 11:45:18 +0000776 if (skl->nhlt == NULL) {
777 err = -ENODEV;
Vinod Koulab1b7322017-05-04 14:09:10 +0530778 goto out_free;
Wei Yongjun979cf592016-08-12 11:45:18 +0000779 }
Jeeja KP87b2bdf2015-10-07 11:31:59 +0100780
Subhransu S. Prusty0cf5a172017-01-11 16:31:02 +0530781 err = skl_nhlt_create_sysfs(skl);
782 if (err < 0)
783 goto out_nhlt_free;
784
Vinod Koul4b235c42016-02-19 11:42:34 +0530785 skl_nhlt_update_topology_bin(skl);
786
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530787 pci_set_drvdata(skl->pci, ebus);
788
Yong Zhif65cf7d62016-05-26 21:30:15 -0700789 skl_dmic_data.dmic_num = skl_get_dmic_geo(skl);
790
Jeeja KP05057002015-07-09 15:20:11 +0530791 /* check if dsp is there */
Vinod Koulec8ae572016-08-04 15:46:01 +0530792 if (bus->ppcap) {
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530793 err = skl_machine_device_register(skl,
794 (void *)pci_id->driver_data);
795 if (err < 0)
Jeeja KPc286b3f2016-05-05 11:19:19 +0530796 goto out_nhlt_free;
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530797
Jeeja KP2a29b202015-10-07 11:31:58 +0100798 err = skl_init_dsp(skl);
799 if (err < 0) {
800 dev_dbg(bus->dev, "error failed to register dsp\n");
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530801 goto out_mach_free;
Jeeja KP2a29b202015-10-07 11:31:58 +0100802 }
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530803 skl->skl_sst->enable_miscbdcge = skl_enable_miscbdcge;
804
Jeeja KP05057002015-07-09 15:20:11 +0530805 }
Vinod Koulec8ae572016-08-04 15:46:01 +0530806 if (bus->mlcap)
Jeeja KP05057002015-07-09 15:20:11 +0530807 snd_hdac_ext_bus_get_ml_capabilities(ebus);
808
Vinod Koulab1b7322017-05-04 14:09:10 +0530809 snd_hdac_bus_stop_chip(bus);
810
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530811 /* create device for soc dmic */
812 err = skl_dmic_device_register(skl);
813 if (err < 0)
Jeeja KP2a29b202015-10-07 11:31:58 +0100814 goto out_dsp_free;
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530815
Vinod Koulab1b7322017-05-04 14:09:10 +0530816 schedule_work(&skl->probe_work);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530817
818 return 0;
819
Jeeja KP2a29b202015-10-07 11:31:58 +0100820out_dsp_free:
821 skl_free_dsp(skl);
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530822out_mach_free:
823 skl_machine_device_unregister(skl);
Jeeja KPc286b3f2016-05-05 11:19:19 +0530824out_nhlt_free:
825 skl_nhlt_free(skl->nhlt);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530826out_free:
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530827 skl_free(ebus);
828
829 return err;
830}
831
Jeeja KPc5a76a22016-02-05 12:19:09 +0530832static void skl_shutdown(struct pci_dev *pci)
833{
834 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
835 struct hdac_bus *bus = ebus_to_hbus(ebus);
836 struct hdac_stream *s;
837 struct hdac_ext_stream *stream;
838 struct skl *skl;
839
840 if (ebus == NULL)
841 return;
842
843 skl = ebus_to_skl(ebus);
844
Vinod Koulab1b7322017-05-04 14:09:10 +0530845 if (!skl->init_done)
Jeeja KPc5a76a22016-02-05 12:19:09 +0530846 return;
847
848 snd_hdac_ext_stop_streams(ebus);
849 list_for_each_entry(s, &bus->stream_list, list) {
850 stream = stream_to_hdac_ext_stream(s);
851 snd_hdac_ext_stream_decouple(ebus, stream, false);
852 }
853
854 snd_hdac_bus_stop_chip(bus);
855}
856
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530857static void skl_remove(struct pci_dev *pci)
858{
859 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
860 struct skl *skl = ebus_to_skl(ebus);
861
Markus Elfring1b001262016-07-22 18:58:14 +0200862 release_firmware(skl->tplg);
Vinod Kould8018362016-01-05 17:16:04 +0530863
Lukas Wunner6d13f622016-10-20 12:26:16 +0200864 pm_runtime_get_noresume(&pci->dev);
Vinod Koul7373f482016-03-15 16:39:24 +0530865
866 /* codec removal, invoke bus_device_remove */
867 snd_hdac_ext_bus_device_remove(ebus);
868
Vinod Koul5cdf6c02017-06-30 09:06:05 +0530869 skl->debugfs = NULL;
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530870 skl_platform_unregister(&pci->dev);
Jeeja KP2a29b202015-10-07 11:31:58 +0100871 skl_free_dsp(skl);
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530872 skl_machine_device_unregister(skl);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530873 skl_dmic_device_unregister(skl);
Subhransu S. Prusty0cf5a172017-01-11 16:31:02 +0530874 skl_nhlt_remove_sysfs(skl);
Jeeja KPc286b3f2016-05-05 11:19:19 +0530875 skl_nhlt_free(skl->nhlt);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530876 skl_free(ebus);
877 dev_set_drvdata(&pci->dev, NULL);
878}
879
Harsha Priya N9a90c972017-06-14 10:32:18 -0700880static struct sst_codecs skl_codecs = {
881 .num_codecs = 1,
882 .codecs = {"NAU88L25"}
883};
884
885static struct sst_codecs kbl_codecs = {
886 .num_codecs = 1,
887 .codecs = {"NAU88L25"}
888};
889
890static struct sst_codecs bxt_codecs = {
891 .num_codecs = 1,
892 .codecs = {"MX98357A"}
893};
894
895static struct sst_codecs kbl_poppy_codecs = {
896 .num_codecs = 1,
897 .codecs = {"10EC5663"}
898};
Naveen M54746da2017-05-15 13:42:15 +0530899
Harsha Priya Nad7fb5a2017-06-14 10:32:19 -0700900static struct sst_codecs kbl_5663_5514_codecs = {
901 .num_codecs = 2,
902 .codecs = {"10EC5663", "10EC5514"}
903};
904
905
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530906static struct sst_acpi_mach sst_skl_devdata[] = {
Naveen M9bf70cd2017-05-15 13:42:12 +0530907 {
908 .id = "INT343A",
909 .drv_name = "skl_alc286s_i2s",
910 .fw_filename = "intel/dsp_fw_release.bin",
911 },
912 {
913 .id = "INT343B",
914 .drv_name = "skl_n88l25_s4567",
915 .fw_filename = "intel/dsp_fw_release.bin",
Naveen M54746da2017-05-15 13:42:15 +0530916 .machine_quirk = sst_acpi_codec_list,
917 .quirk_data = &skl_codecs,
Naveen M9bf70cd2017-05-15 13:42:12 +0530918 .pdata = &skl_dmic_data
919 },
920 {
921 .id = "MX98357A",
922 .drv_name = "skl_n88l25_m98357a",
923 .fw_filename = "intel/dsp_fw_release.bin",
Naveen M54746da2017-05-15 13:42:15 +0530924 .machine_quirk = sst_acpi_codec_list,
925 .quirk_data = &skl_codecs,
Naveen M9bf70cd2017-05-15 13:42:12 +0530926 .pdata = &skl_dmic_data
927 },
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530928 {}
929};
930
Senthilnathan Veppurb379b1f2016-03-11 10:12:54 +0530931static struct sst_acpi_mach sst_bxtp_devdata[] = {
Naveen M9bf70cd2017-05-15 13:42:12 +0530932 {
933 .id = "INT343A",
934 .drv_name = "bxt_alc298s_i2s",
935 .fw_filename = "intel/dsp_fw_bxtn.bin",
936 },
937 {
938 .id = "DLGS7219",
939 .drv_name = "bxt_da7219_max98357a_i2s",
940 .fw_filename = "intel/dsp_fw_bxtn.bin",
Naveen M54746da2017-05-15 13:42:15 +0530941 .machine_quirk = sst_acpi_codec_list,
942 .quirk_data = &bxt_codecs,
Naveen M9bf70cd2017-05-15 13:42:12 +0530943 },
Vinod Koulb76e3f92017-07-25 12:08:12 +0530944 {}
Senthilnathan Veppurb379b1f2016-03-11 10:12:54 +0530945};
946
Vinod Koul451dfb5f2016-07-11 22:02:08 +0530947static struct sst_acpi_mach sst_kbl_devdata[] = {
Naveen M9bf70cd2017-05-15 13:42:12 +0530948 {
949 .id = "INT343A",
950 .drv_name = "kbl_alc286s_i2s",
951 .fw_filename = "intel/dsp_fw_kbl.bin",
952 },
953 {
954 .id = "INT343B",
955 .drv_name = "kbl_n88l25_s4567",
956 .fw_filename = "intel/dsp_fw_kbl.bin",
Naveen M54746da2017-05-15 13:42:15 +0530957 .machine_quirk = sst_acpi_codec_list,
958 .quirk_data = &kbl_codecs,
Naveen M9bf70cd2017-05-15 13:42:12 +0530959 .pdata = &skl_dmic_data
960 },
961 {
962 .id = "MX98357A",
963 .drv_name = "kbl_n88l25_m98357a",
964 .fw_filename = "intel/dsp_fw_kbl.bin",
Naveen M54746da2017-05-15 13:42:15 +0530965 .machine_quirk = sst_acpi_codec_list,
966 .quirk_data = &kbl_codecs,
Naveen M9bf70cd2017-05-15 13:42:12 +0530967 .pdata = &skl_dmic_data
968 },
Naveen M0809d982017-05-15 13:42:17 +0530969 {
970 .id = "MX98927",
Harsha Priya Nad7fb5a2017-06-14 10:32:19 -0700971 .drv_name = "kbl_r5514_5663_max",
972 .fw_filename = "intel/dsp_fw_kbl.bin",
973 .machine_quirk = sst_acpi_codec_list,
974 .quirk_data = &kbl_5663_5514_codecs,
975 .pdata = &skl_dmic_data
976 },
977 {
978 .id = "MX98927",
Naveen M0809d982017-05-15 13:42:17 +0530979 .drv_name = "kbl_rt5663_m98927",
980 .fw_filename = "intel/dsp_fw_kbl.bin",
981 .machine_quirk = sst_acpi_codec_list,
982 .quirk_data = &kbl_poppy_codecs,
983 .pdata = &skl_dmic_data
984 },
985
Vinod Koul451dfb5f2016-07-11 22:02:08 +0530986 {}
987};
988
Vinod Koul25504862017-02-09 16:44:03 +0530989static struct sst_acpi_mach sst_glk_devdata[] = {
Naveen M9bf70cd2017-05-15 13:42:12 +0530990 {
991 .id = "INT343A",
992 .drv_name = "glk_alc298s_i2s",
993 .fw_filename = "intel/dsp_fw_glk.bin",
994 },
Vinod Koulb76e3f92017-07-25 12:08:12 +0530995 {}
Vinod Koul25504862017-02-09 16:44:03 +0530996};
997
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530998/* PCI IDs */
999static const struct pci_device_id skl_ids[] = {
1000 /* Sunrise Point-LP */
Vinod Koulcc18c5f2015-11-05 21:34:13 +05301001 { PCI_DEVICE(0x8086, 0x9d70),
1002 .driver_data = (unsigned long)&sst_skl_devdata},
Senthilnathan Veppurb379b1f2016-03-11 10:12:54 +05301003 /* BXT-P */
1004 { PCI_DEVICE(0x8086, 0x5a98),
1005 .driver_data = (unsigned long)&sst_bxtp_devdata},
Vinod Koul451dfb5f2016-07-11 22:02:08 +05301006 /* KBL */
1007 { PCI_DEVICE(0x8086, 0x9D71),
1008 .driver_data = (unsigned long)&sst_kbl_devdata},
Vinod Koul25504862017-02-09 16:44:03 +05301009 /* GLK */
1010 { PCI_DEVICE(0x8086, 0x3198),
1011 .driver_data = (unsigned long)&sst_glk_devdata},
Jeeja KPd8c2dab2015-07-09 15:20:09 +05301012 { 0, }
1013};
1014MODULE_DEVICE_TABLE(pci, skl_ids);
1015
1016/* pci_driver definition */
1017static struct pci_driver skl_driver = {
1018 .name = KBUILD_MODNAME,
1019 .id_table = skl_ids,
1020 .probe = skl_probe,
1021 .remove = skl_remove,
Jeeja KPc5a76a22016-02-05 12:19:09 +05301022 .shutdown = skl_shutdown,
Jeeja KPd8c2dab2015-07-09 15:20:09 +05301023 .driver = {
1024 .pm = &skl_pm,
1025 },
1026};
1027module_pci_driver(skl_driver);
1028
1029MODULE_LICENSE("GPL v2");
1030MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");