blob: ed59783e98461ba8fc9281c246aaf931de32a420 [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
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530231#ifdef CONFIG_PM
Jeeja KP61722f42015-10-27 09:23:00 +0900232static int _skl_suspend(struct hdac_ext_bus *ebus)
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530233{
Jeeja KP2a29b202015-10-07 11:31:58 +0100234 struct skl *skl = ebus_to_skl(ebus);
Jeeja KP61722f42015-10-27 09:23:00 +0900235 struct hdac_bus *bus = ebus_to_hbus(ebus);
Dharageswari R51a01b82016-06-03 18:29:37 +0530236 struct pci_dev *pci = to_pci_dev(bus->dev);
Jeeja KP2a29b202015-10-07 11:31:58 +0100237 int ret;
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530238
Jeeja KP01bb84b2015-10-09 09:01:51 +0100239 snd_hdac_ext_bus_link_power_down_all(ebus);
240
Jeeja KP2a29b202015-10-07 11:31:58 +0100241 ret = skl_suspend_dsp(skl);
242 if (ret < 0)
243 return ret;
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530244
245 snd_hdac_bus_stop_chip(bus);
Dharageswari R51a01b82016-06-03 18:29:37 +0530246 update_pci_dword(pci, AZX_PCIREG_PGCTL,
247 AZX_PGCTL_LSRMD_MASK, AZX_PGCTL_LSRMD_MASK);
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530248 skl_enable_miscbdcge(bus->dev, false);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530249 snd_hdac_bus_enter_link_reset(bus);
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530250 skl_enable_miscbdcge(bus->dev, true);
Dharageswari Rfe3f4442016-06-03 18:29:39 +0530251 skl_cleanup_resources(skl);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530252
253 return 0;
254}
255
Jeeja KP61722f42015-10-27 09:23:00 +0900256static int _skl_resume(struct hdac_ext_bus *ebus)
257{
258 struct skl *skl = ebus_to_skl(ebus);
259 struct hdac_bus *bus = ebus_to_hbus(ebus);
260
261 skl_init_pci(skl);
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530262 skl_init_chip(bus, true);
Jeeja KP61722f42015-10-27 09:23:00 +0900263
264 return skl_resume_dsp(skl);
265}
266#endif
267
268#ifdef CONFIG_PM_SLEEP
269/*
270 * power management
271 */
272static int skl_suspend(struct device *dev)
273{
274 struct pci_dev *pci = to_pci_dev(dev);
275 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
Jeeja KP4557c302015-12-03 23:30:00 +0530276 struct skl *skl = ebus_to_skl(ebus);
Jeeja KP1f4956f2015-12-18 15:12:06 +0530277 struct hdac_bus *bus = ebus_to_hbus(ebus);
Subhransu S. Prustyaf037412016-04-01 13:36:27 +0530278 int ret = 0;
Jeeja KP61722f42015-10-27 09:23:00 +0900279
Jeeja KP4557c302015-12-03 23:30:00 +0530280 /*
281 * Do not suspend if streams which are marked ignore suspend are
282 * running, we need to save the state for these and continue
283 */
284 if (skl->supend_active) {
Vinod Koulcce6c142016-05-12 08:58:54 +0530285 /* turn off the links and stop the CORB/RIRB DMA if it is On */
Vinod Koulc2e20cd2015-12-18 15:12:05 +0530286 snd_hdac_ext_bus_link_power_down_all(ebus);
Vinod Koulcce6c142016-05-12 08:58:54 +0530287
288 if (ebus->cmd_dma_state)
289 snd_hdac_bus_stop_cmd_io(&ebus->bus);
290
Jeeja KP1f4956f2015-12-18 15:12:06 +0530291 enable_irq_wake(bus->irq);
Jeeja KP4557c302015-12-03 23:30:00 +0530292 pci_save_state(pci);
Jeeja KP4557c302015-12-03 23:30:00 +0530293 } else {
Subhransu S. Prustyaf037412016-04-01 13:36:27 +0530294 ret = _skl_suspend(ebus);
295 if (ret < 0)
296 return ret;
Jayachandran B1665c172016-06-13 17:59:01 +0530297 skl->skl_sst->fw_loaded = false;
Jeeja KP4557c302015-12-03 23:30:00 +0530298 }
Subhransu S. Prustyaf037412016-04-01 13:36:27 +0530299
300 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
301 ret = snd_hdac_display_power(bus, false);
302 if (ret < 0)
303 dev_err(bus->dev,
304 "Cannot turn OFF display power on i915\n");
305 }
306
307 return ret;
Jeeja KP61722f42015-10-27 09:23:00 +0900308}
309
310static int skl_resume(struct device *dev)
311{
312 struct pci_dev *pci = to_pci_dev(dev);
313 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
Jeeja KP4557c302015-12-03 23:30:00 +0530314 struct skl *skl = ebus_to_skl(ebus);
Jeeja KP1f4956f2015-12-18 15:12:06 +0530315 struct hdac_bus *bus = ebus_to_hbus(ebus);
Vinod Koulcce6c142016-05-12 08:58:54 +0530316 struct hdac_ext_link *hlink = NULL;
Jeeja KP4557c302015-12-03 23:30:00 +0530317 int ret;
Jeeja KP61722f42015-10-27 09:23:00 +0900318
Vinod Koul6980c052016-02-17 21:34:06 +0530319 /* Turned OFF in HDMI codec driver after codec reconfiguration */
320 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
321 ret = snd_hdac_display_power(bus, true);
322 if (ret < 0) {
323 dev_err(bus->dev,
324 "Cannot turn on display power on i915\n");
325 return ret;
326 }
327 }
328
Jeeja KP4557c302015-12-03 23:30:00 +0530329 /*
330 * resume only when we are not in suspend active, otherwise need to
331 * restore the device
332 */
333 if (skl->supend_active) {
334 pci_restore_state(pci);
Vinod Koulc2e20cd2015-12-18 15:12:05 +0530335 snd_hdac_ext_bus_link_power_up_all(ebus);
Jeeja KP1f4956f2015-12-18 15:12:06 +0530336 disable_irq_wake(bus->irq);
Vinod Koulcce6c142016-05-12 08:58:54 +0530337 /*
338 * turn On the links which are On before active suspend
339 * and start the CORB/RIRB DMA if On before
340 * active suspend.
341 */
342 list_for_each_entry(hlink, &ebus->hlink_list, list) {
343 if (hlink->ref_count)
344 snd_hdac_ext_bus_link_power_up(hlink);
345 }
346
347 if (ebus->cmd_dma_state)
348 snd_hdac_bus_init_cmd_io(&ebus->bus);
Jeeja KP4557c302015-12-03 23:30:00 +0530349 } else {
350 ret = _skl_resume(ebus);
Vinod Koulcce6c142016-05-12 08:58:54 +0530351
352 /* turn off the links which are off before suspend */
353 list_for_each_entry(hlink, &ebus->hlink_list, list) {
354 if (!hlink->ref_count)
355 snd_hdac_ext_bus_link_power_down(hlink);
356 }
357
358 if (!ebus->cmd_dma_state)
359 snd_hdac_bus_stop_cmd_io(&ebus->bus);
Jeeja KP4557c302015-12-03 23:30:00 +0530360 }
361
362 return ret;
Jeeja KP61722f42015-10-27 09:23:00 +0900363}
364#endif /* CONFIG_PM_SLEEP */
365
366#ifdef CONFIG_PM
367static int skl_runtime_suspend(struct device *dev)
368{
369 struct pci_dev *pci = to_pci_dev(dev);
370 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
371 struct hdac_bus *bus = ebus_to_hbus(ebus);
372
373 dev_dbg(bus->dev, "in %s\n", __func__);
374
Jeeja KP61722f42015-10-27 09:23:00 +0900375 return _skl_suspend(ebus);
376}
377
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530378static int skl_runtime_resume(struct device *dev)
379{
380 struct pci_dev *pci = to_pci_dev(dev);
381 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
382 struct hdac_bus *bus = ebus_to_hbus(ebus);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530383
384 dev_dbg(bus->dev, "in %s\n", __func__);
385
Jeeja KP61722f42015-10-27 09:23:00 +0900386 return _skl_resume(ebus);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530387}
388#endif /* CONFIG_PM */
389
390static const struct dev_pm_ops skl_pm = {
391 SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
392 SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
393};
394
395/*
396 * destructor
397 */
398static int skl_free(struct hdac_ext_bus *ebus)
399{
400 struct skl *skl = ebus_to_skl(ebus);
401 struct hdac_bus *bus = ebus_to_hbus(ebus);
402
403 skl->init_failed = 1; /* to be sure */
404
405 snd_hdac_ext_stop_streams(ebus);
406
407 if (bus->irq >= 0)
408 free_irq(bus->irq, (void *)bus);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530409 snd_hdac_bus_free_stream_pages(bus);
410 snd_hdac_stream_free_all(ebus);
411 snd_hdac_link_free_all(ebus);
Vinod Koul077411e2016-03-15 16:39:26 +0530412
413 if (bus->remap_addr)
414 iounmap(bus->remap_addr);
415
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530416 pci_release_regions(skl->pci);
417 pci_disable_device(skl->pci);
418
419 snd_hdac_ext_bus_exit(ebus);
420
Vinod Koul5b2fe892016-03-15 16:39:27 +0530421 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI))
422 snd_hdac_i915_exit(&ebus->bus);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530423 return 0;
424}
425
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530426static int skl_machine_device_register(struct skl *skl, void *driver_data)
427{
428 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
429 struct platform_device *pdev;
430 struct sst_acpi_mach *mach = driver_data;
431 int ret;
432
433 mach = sst_acpi_find_machine(mach);
434 if (mach == NULL) {
435 dev_err(bus->dev, "No matching machine driver found\n");
436 return -ENODEV;
437 }
Vinod Koulaecf6fd2015-11-05 21:34:15 +0530438 skl->fw_name = mach->fw_filename;
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530439
440 pdev = platform_device_alloc(mach->drv_name, -1);
441 if (pdev == NULL) {
442 dev_err(bus->dev, "platform device alloc failed\n");
443 return -EIO;
444 }
445
446 ret = platform_device_add(pdev);
447 if (ret) {
448 dev_err(bus->dev, "failed to add machine device\n");
449 platform_device_put(pdev);
450 return -EIO;
451 }
Yong Zhif65cf7d62016-05-26 21:30:15 -0700452
453 if (mach->pdata)
454 dev_set_drvdata(&pdev->dev, mach->pdata);
455
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530456 skl->i2s_dev = pdev;
457
458 return 0;
459}
460
461static void skl_machine_device_unregister(struct skl *skl)
462{
463 if (skl->i2s_dev)
464 platform_device_unregister(skl->i2s_dev);
465}
466
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530467static int skl_dmic_device_register(struct skl *skl)
468{
469 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
470 struct platform_device *pdev;
471 int ret;
472
473 /* SKL has one dmic port, so allocate dmic device for this */
474 pdev = platform_device_alloc("dmic-codec", -1);
475 if (!pdev) {
476 dev_err(bus->dev, "failed to allocate dmic device\n");
477 return -ENOMEM;
478 }
479
480 ret = platform_device_add(pdev);
481 if (ret) {
482 dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
483 platform_device_put(pdev);
484 return ret;
485 }
486 skl->dmic_dev = pdev;
487
488 return 0;
489}
490
491static void skl_dmic_device_unregister(struct skl *skl)
492{
493 if (skl->dmic_dev)
494 platform_device_unregister(skl->dmic_dev);
495}
496
497/*
498 * Probe the given codec address
499 */
500static int probe_codec(struct hdac_ext_bus *ebus, int addr)
501{
502 struct hdac_bus *bus = ebus_to_hbus(ebus);
503 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
504 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
505 unsigned int res;
506
507 mutex_lock(&bus->cmd_mutex);
508 snd_hdac_bus_send_cmd(bus, cmd);
509 snd_hdac_bus_get_response(bus, addr, &res);
510 mutex_unlock(&bus->cmd_mutex);
511 if (res == -1)
512 return -EIO;
513 dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
514
515 return snd_hdac_ext_bus_device_init(ebus, addr);
516}
517
518/* Codec initialization */
519static int skl_codec_create(struct hdac_ext_bus *ebus)
520{
521 struct hdac_bus *bus = ebus_to_hbus(ebus);
522 int c, max_slots;
523
524 max_slots = HDA_MAX_CODECS;
525
526 /* First try to probe all given codec slots */
527 for (c = 0; c < max_slots; c++) {
528 if ((bus->codec_mask & (1 << c))) {
529 if (probe_codec(ebus, c) < 0) {
530 /*
531 * Some BIOSen give you wrong codec addresses
532 * that don't exist
533 */
534 dev_warn(bus->dev,
535 "Codec #%d probe error; disabling it...\n", c);
536 bus->codec_mask &= ~(1 << c);
537 /*
538 * More badly, accessing to a non-existing
539 * codec often screws up the controller bus,
540 * and disturbs the further communications.
541 * Thus if an error occurs during probing,
542 * better to reset the controller bus to get
543 * back to the sanity state.
544 */
545 snd_hdac_bus_stop_chip(bus);
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530546 skl_init_chip(bus, true);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530547 }
548 }
549 }
550
551 return 0;
552}
553
554static const struct hdac_bus_ops bus_core_ops = {
555 .command = snd_hdac_bus_send_cmd,
556 .get_response = snd_hdac_bus_get_response,
557};
558
559/*
560 * constructor
561 */
562static int skl_create(struct pci_dev *pci,
563 const struct hdac_io_ops *io_ops,
564 struct skl **rskl)
565{
566 struct skl *skl;
567 struct hdac_ext_bus *ebus;
568
569 int err;
570
571 *rskl = NULL;
572
573 err = pci_enable_device(pci);
574 if (err < 0)
575 return err;
576
577 skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
578 if (!skl) {
579 pci_disable_device(pci);
580 return -ENOMEM;
581 }
582 ebus = &skl->ebus;
583 snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
584 ebus->bus.use_posbuf = 1;
585 skl->pci = pci;
586
587 ebus->bus.bdl_pos_adj = 0;
588
589 *rskl = skl;
590
591 return 0;
592}
593
Vinod Koul6980c052016-02-17 21:34:06 +0530594static int skl_i915_init(struct hdac_bus *bus)
595{
596 int err;
597
598 /*
599 * The HDMI codec is in GPU so we need to ensure that it is powered
600 * up and ready for probe
601 */
602 err = snd_hdac_i915_init(bus);
603 if (err < 0)
604 return err;
605
606 err = snd_hdac_display_power(bus, true);
607 if (err < 0) {
608 dev_err(bus->dev, "Cannot turn on display power on i915\n");
609 return err;
610 }
611
612 return err;
613}
614
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530615static int skl_first_init(struct hdac_ext_bus *ebus)
616{
617 struct skl *skl = ebus_to_skl(ebus);
618 struct hdac_bus *bus = ebus_to_hbus(ebus);
619 struct pci_dev *pci = skl->pci;
620 int err;
621 unsigned short gcap;
622 int cp_streams, pb_streams, start_idx;
623
624 err = pci_request_regions(pci, "Skylake HD audio");
625 if (err < 0)
626 return err;
627
628 bus->addr = pci_resource_start(pci, 0);
629 bus->remap_addr = pci_ioremap_bar(pci, 0);
630 if (bus->remap_addr == NULL) {
631 dev_err(bus->dev, "ioremap error\n");
632 return -ENXIO;
633 }
634
Vinod Koulec8ae572016-08-04 15:46:01 +0530635 snd_hdac_bus_parse_capabilities(bus);
Jeeja KP05057002015-07-09 15:20:11 +0530636
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530637 if (skl_acquire_irq(ebus, 0) < 0)
638 return -EBUSY;
639
640 pci_set_master(pci);
641 synchronize_irq(bus->irq);
642
643 gcap = snd_hdac_chip_readw(bus, GCAP);
644 dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
645
646 /* allow 64bit DMA address if supported by H/W */
647 if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
648 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
649 } else {
650 dma_set_mask(bus->dev, DMA_BIT_MASK(32));
651 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
652 }
653
654 /* read number of streams from GCAP register */
655 cp_streams = (gcap >> 8) & 0x0f;
656 pb_streams = (gcap >> 12) & 0x0f;
657
658 if (!pb_streams && !cp_streams)
659 return -EIO;
660
661 ebus->num_streams = cp_streams + pb_streams;
662
663 /* initialize streams */
664 snd_hdac_ext_stream_init_all
665 (ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
666 start_idx = cp_streams;
667 snd_hdac_ext_stream_init_all
668 (ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
669
670 err = snd_hdac_bus_alloc_stream_pages(bus);
671 if (err < 0)
672 return err;
673
674 /* initialize chip */
675 skl_init_pci(skl);
676
Vinod Koul6980c052016-02-17 21:34:06 +0530677 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
678 err = skl_i915_init(bus);
679 if (err < 0)
680 return err;
681 }
682
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530683 skl_init_chip(bus, true);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530684
685 /* codec detection */
686 if (!bus->codec_mask) {
Jeeja KP029890c2015-10-27 09:22:47 +0900687 dev_info(bus->dev, "no hda codecs found!\n");
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530688 }
689
690 return 0;
691}
692
693static int skl_probe(struct pci_dev *pci,
694 const struct pci_device_id *pci_id)
695{
696 struct skl *skl;
697 struct hdac_ext_bus *ebus = NULL;
698 struct hdac_bus *bus = NULL;
Vinod Koulcce6c142016-05-12 08:58:54 +0530699 struct hdac_ext_link *hlink = NULL;
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530700 int err;
701
702 /* we use ext core ops, so provide NULL for ops here */
703 err = skl_create(pci, NULL, &skl);
704 if (err < 0)
705 return err;
706
707 ebus = &skl->ebus;
708 bus = ebus_to_hbus(ebus);
709
710 err = skl_first_init(ebus);
711 if (err < 0)
712 goto out_free;
713
Vinod Koul4b235c42016-02-19 11:42:34 +0530714 skl->pci_id = pci->device;
715
Vinod Koul2e9dc2b2016-06-22 19:44:19 +0530716 device_disable_async_suspend(bus->dev);
717
Jeeja KP87b2bdf2015-10-07 11:31:59 +0100718 skl->nhlt = skl_nhlt_init(bus->dev);
719
Wei Yongjun979cf592016-08-12 11:45:18 +0000720 if (skl->nhlt == NULL) {
721 err = -ENODEV;
Jeeja KP87b2bdf2015-10-07 11:31:59 +0100722 goto out_free;
Wei Yongjun979cf592016-08-12 11:45:18 +0000723 }
Jeeja KP87b2bdf2015-10-07 11:31:59 +0100724
Vinod Koul4b235c42016-02-19 11:42:34 +0530725 skl_nhlt_update_topology_bin(skl);
726
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530727 pci_set_drvdata(skl->pci, ebus);
728
Yong Zhif65cf7d62016-05-26 21:30:15 -0700729 skl_dmic_data.dmic_num = skl_get_dmic_geo(skl);
730
Jeeja KP05057002015-07-09 15:20:11 +0530731 /* check if dsp is there */
Vinod Koulec8ae572016-08-04 15:46:01 +0530732 if (bus->ppcap) {
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530733 err = skl_machine_device_register(skl,
734 (void *)pci_id->driver_data);
735 if (err < 0)
Jeeja KPc286b3f2016-05-05 11:19:19 +0530736 goto out_nhlt_free;
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530737
Jeeja KP2a29b202015-10-07 11:31:58 +0100738 err = skl_init_dsp(skl);
739 if (err < 0) {
740 dev_dbg(bus->dev, "error failed to register dsp\n");
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530741 goto out_mach_free;
Jeeja KP2a29b202015-10-07 11:31:58 +0100742 }
Jayachandran B0c8ba9d2015-12-18 15:12:03 +0530743 skl->skl_sst->enable_miscbdcge = skl_enable_miscbdcge;
744
Jeeja KP05057002015-07-09 15:20:11 +0530745 }
Vinod Koulec8ae572016-08-04 15:46:01 +0530746 if (bus->mlcap)
Jeeja KP05057002015-07-09 15:20:11 +0530747 snd_hdac_ext_bus_get_ml_capabilities(ebus);
748
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530749 /* create device for soc dmic */
750 err = skl_dmic_device_register(skl);
751 if (err < 0)
Jeeja KP2a29b202015-10-07 11:31:58 +0100752 goto out_dsp_free;
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530753
754 /* register platform dai and controls */
755 err = skl_platform_register(bus->dev);
756 if (err < 0)
757 goto out_dmic_free;
758
759 /* create codec instances */
760 err = skl_codec_create(ebus);
761 if (err < 0)
762 goto out_unregister;
763
Vinod Koul6980c052016-02-17 21:34:06 +0530764 if (IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)) {
765 err = snd_hdac_display_power(bus, false);
766 if (err < 0) {
767 dev_err(bus->dev, "Cannot turn off display power on i915\n");
768 return err;
769 }
770 }
771
Vinod Koulcce6c142016-05-12 08:58:54 +0530772 /*
773 * we are done probling so decrement link counts
774 */
775 list_for_each_entry(hlink, &ebus->hlink_list, list)
776 snd_hdac_ext_bus_link_put(ebus, hlink);
777
Vinod Koul957427d2016-06-22 19:44:21 +0530778 /* configure PM */
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530779 pm_runtime_put_noidle(bus->dev);
780 pm_runtime_allow(bus->dev);
781
782 return 0;
783
784out_unregister:
785 skl_platform_unregister(bus->dev);
786out_dmic_free:
787 skl_dmic_device_unregister(skl);
Jeeja KP2a29b202015-10-07 11:31:58 +0100788out_dsp_free:
789 skl_free_dsp(skl);
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530790out_mach_free:
791 skl_machine_device_unregister(skl);
Jeeja KPc286b3f2016-05-05 11:19:19 +0530792out_nhlt_free:
793 skl_nhlt_free(skl->nhlt);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530794out_free:
795 skl->init_failed = 1;
796 skl_free(ebus);
797
798 return err;
799}
800
Jeeja KPc5a76a22016-02-05 12:19:09 +0530801static void skl_shutdown(struct pci_dev *pci)
802{
803 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
804 struct hdac_bus *bus = ebus_to_hbus(ebus);
805 struct hdac_stream *s;
806 struct hdac_ext_stream *stream;
807 struct skl *skl;
808
809 if (ebus == NULL)
810 return;
811
812 skl = ebus_to_skl(ebus);
813
814 if (skl->init_failed)
815 return;
816
817 snd_hdac_ext_stop_streams(ebus);
818 list_for_each_entry(s, &bus->stream_list, list) {
819 stream = stream_to_hdac_ext_stream(s);
820 snd_hdac_ext_stream_decouple(ebus, stream, false);
821 }
822
823 snd_hdac_bus_stop_chip(bus);
824}
825
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530826static void skl_remove(struct pci_dev *pci)
827{
828 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
829 struct skl *skl = ebus_to_skl(ebus);
830
Markus Elfring1b001262016-07-22 18:58:14 +0200831 release_firmware(skl->tplg);
Vinod Kould8018362016-01-05 17:16:04 +0530832
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530833 if (pci_dev_run_wake(pci))
834 pm_runtime_get_noresume(&pci->dev);
Vinod Koul7373f482016-03-15 16:39:24 +0530835
836 /* codec removal, invoke bus_device_remove */
837 snd_hdac_ext_bus_device_remove(ebus);
838
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530839 skl_platform_unregister(&pci->dev);
Jeeja KP2a29b202015-10-07 11:31:58 +0100840 skl_free_dsp(skl);
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530841 skl_machine_device_unregister(skl);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530842 skl_dmic_device_unregister(skl);
Jeeja KPc286b3f2016-05-05 11:19:19 +0530843 skl_nhlt_free(skl->nhlt);
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530844 skl_free(ebus);
845 dev_set_drvdata(&pci->dev, NULL);
846}
847
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530848static struct sst_acpi_mach sst_skl_devdata[] = {
849 { "INT343A", "skl_alc286s_i2s", "intel/dsp_fw_release.bin", NULL, NULL, NULL },
Vinod Koul400ada02016-07-13 22:13:43 +0530850 { "INT343B", "skl_n88l25_s4567", "intel/dsp_fw_release.bin",
Yong Zhif65cf7d62016-05-26 21:30:15 -0700851 NULL, NULL, &skl_dmic_data },
Vinod Koula2f5b8d2016-07-13 22:13:44 +0530852 { "MX98357A", "skl_n88l25_m98357a", "intel/dsp_fw_release.bin",
Yong Zhif65cf7d62016-05-26 21:30:15 -0700853 NULL, NULL, &skl_dmic_data },
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530854 {}
855};
856
Senthilnathan Veppurb379b1f2016-03-11 10:12:54 +0530857static struct sst_acpi_mach sst_bxtp_devdata[] = {
858 { "INT343A", "bxt_alc298s_i2s", "intel/dsp_fw_bxtn.bin", NULL, NULL, NULL },
Sathyanarayana Nujellade159962016-05-31 23:33:16 -0700859 { "DLGS7219", "bxt_da7219_max98357a_i2s", "intel/dsp_fw_bxtn.bin", NULL, NULL, NULL },
Senthilnathan Veppurb379b1f2016-03-11 10:12:54 +0530860};
861
Vinod Koul451dfb5f2016-07-11 22:02:08 +0530862static struct sst_acpi_mach sst_kbl_devdata[] = {
863 { "INT343A", "kbl_alc286s_i2s", "intel/dsp_fw_kbl.bin", NULL, NULL, NULL },
Vinod Koul156a0712016-07-13 22:13:45 +0530864 { "INT343B", "kbl_n88l25_s4567", "intel/dsp_fw_kbl.bin", NULL, NULL, &skl_dmic_data },
865 { "MX98357A", "kbl_n88l25_m98357a", "intel/dsp_fw_kbl.bin", NULL, NULL, &skl_dmic_data },
Vinod Koul451dfb5f2016-07-11 22:02:08 +0530866 {}
867};
868
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530869/* PCI IDs */
870static const struct pci_device_id skl_ids[] = {
871 /* Sunrise Point-LP */
Vinod Koulcc18c5f2015-11-05 21:34:13 +0530872 { PCI_DEVICE(0x8086, 0x9d70),
873 .driver_data = (unsigned long)&sst_skl_devdata},
Senthilnathan Veppurb379b1f2016-03-11 10:12:54 +0530874 /* BXT-P */
875 { PCI_DEVICE(0x8086, 0x5a98),
876 .driver_data = (unsigned long)&sst_bxtp_devdata},
Vinod Koul451dfb5f2016-07-11 22:02:08 +0530877 /* KBL */
878 { PCI_DEVICE(0x8086, 0x9D71),
879 .driver_data = (unsigned long)&sst_kbl_devdata},
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530880 { 0, }
881};
882MODULE_DEVICE_TABLE(pci, skl_ids);
883
884/* pci_driver definition */
885static struct pci_driver skl_driver = {
886 .name = KBUILD_MODNAME,
887 .id_table = skl_ids,
888 .probe = skl_probe,
889 .remove = skl_remove,
Jeeja KPc5a76a22016-02-05 12:19:09 +0530890 .shutdown = skl_shutdown,
Jeeja KPd8c2dab2015-07-09 15:20:09 +0530891 .driver = {
892 .pm = &skl_pm,
893 },
894};
895module_pci_driver(skl_driver);
896
897MODULE_LICENSE("GPL v2");
898MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");