]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/soc/intel/skylake/skl.c
ASoC: Intel: Skylake: Initialize NHLT table
[karo-tx-linux.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 <sound/pcm.h>
29 #include "skl.h"
30
31 /*
32  * initialize the PCI registers
33  */
34 static void skl_update_pci_byte(struct pci_dev *pci, unsigned int reg,
35                             unsigned char mask, unsigned char val)
36 {
37         unsigned char data;
38
39         pci_read_config_byte(pci, reg, &data);
40         data &= ~mask;
41         data |= (val & mask);
42         pci_write_config_byte(pci, reg, data);
43 }
44
45 static void skl_init_pci(struct skl *skl)
46 {
47         struct hdac_ext_bus *ebus = &skl->ebus;
48
49         /*
50          * Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
51          * TCSEL == Traffic Class Select Register, which sets PCI express QOS
52          * Ensuring these bits are 0 clears playback static on some HD Audio
53          * codecs.
54          * The PCI register TCSEL is defined in the Intel manuals.
55          */
56         dev_dbg(ebus_to_hbus(ebus)->dev, "Clearing TCSEL\n");
57         skl_update_pci_byte(skl->pci, AZX_PCIREG_TCSEL, 0x07, 0);
58 }
59
60 /* called from IRQ */
61 static void skl_stream_update(struct hdac_bus *bus, struct hdac_stream *hstr)
62 {
63         snd_pcm_period_elapsed(hstr->substream);
64 }
65
66 static irqreturn_t skl_interrupt(int irq, void *dev_id)
67 {
68         struct hdac_ext_bus *ebus = dev_id;
69         struct hdac_bus *bus = ebus_to_hbus(ebus);
70         u32 status;
71
72         if (!pm_runtime_active(bus->dev))
73                 return IRQ_NONE;
74
75         spin_lock(&bus->reg_lock);
76
77         status = snd_hdac_chip_readl(bus, INTSTS);
78         if (status == 0 || status == 0xffffffff) {
79                 spin_unlock(&bus->reg_lock);
80                 return IRQ_NONE;
81         }
82
83         /* clear rirb int */
84         status = snd_hdac_chip_readb(bus, RIRBSTS);
85         if (status & RIRB_INT_MASK) {
86                 if (status & RIRB_INT_RESPONSE)
87                         snd_hdac_bus_update_rirb(bus);
88                 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
89         }
90
91         spin_unlock(&bus->reg_lock);
92
93         return snd_hdac_chip_readl(bus, INTSTS) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
94 }
95
96 static irqreturn_t skl_threaded_handler(int irq, void *dev_id)
97 {
98         struct hdac_ext_bus *ebus = dev_id;
99         struct hdac_bus *bus = ebus_to_hbus(ebus);
100         u32 status;
101
102         status = snd_hdac_chip_readl(bus, INTSTS);
103
104         snd_hdac_bus_handle_stream_irq(bus, status, skl_stream_update);
105
106         return IRQ_HANDLED;
107 }
108
109 static int skl_acquire_irq(struct hdac_ext_bus *ebus, int do_disconnect)
110 {
111         struct skl *skl = ebus_to_skl(ebus);
112         struct hdac_bus *bus = ebus_to_hbus(ebus);
113         int ret;
114
115         ret = request_threaded_irq(skl->pci->irq, skl_interrupt,
116                         skl_threaded_handler,
117                         IRQF_SHARED,
118                         KBUILD_MODNAME, ebus);
119         if (ret) {
120                 dev_err(bus->dev,
121                         "unable to grab IRQ %d, disabling device\n",
122                         skl->pci->irq);
123                 return ret;
124         }
125
126         bus->irq = skl->pci->irq;
127         pci_intx(skl->pci, 1);
128
129         return 0;
130 }
131
132 #ifdef CONFIG_PM_SLEEP
133 /*
134  * power management
135  */
136 static int skl_suspend(struct device *dev)
137 {
138         struct pci_dev *pci = to_pci_dev(dev);
139         struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
140         struct hdac_bus *bus = ebus_to_hbus(ebus);
141
142         snd_hdac_bus_stop_chip(bus);
143         snd_hdac_bus_enter_link_reset(bus);
144
145         return 0;
146 }
147
148 static int skl_resume(struct device *dev)
149 {
150         struct pci_dev *pci = to_pci_dev(dev);
151         struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
152         struct hdac_bus *bus = ebus_to_hbus(ebus);
153         struct skl *hda = ebus_to_skl(ebus);
154
155         skl_init_pci(hda);
156
157         snd_hdac_bus_init_chip(bus, 1);
158
159         return 0;
160 }
161 #endif /* CONFIG_PM_SLEEP */
162
163 #ifdef CONFIG_PM
164 static int skl_runtime_suspend(struct device *dev)
165 {
166         struct pci_dev *pci = to_pci_dev(dev);
167         struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
168         struct hdac_bus *bus = ebus_to_hbus(ebus);
169         struct skl *skl = ebus_to_skl(ebus);
170         int ret;
171
172         dev_dbg(bus->dev, "in %s\n", __func__);
173
174         /* enable controller wake up event */
175         snd_hdac_chip_updatew(bus, WAKEEN, 0, STATESTS_INT_MASK);
176         ret = skl_suspend_dsp(skl);
177         if (ret < 0)
178                 return ret;
179
180         snd_hdac_bus_stop_chip(bus);
181         snd_hdac_bus_enter_link_reset(bus);
182
183         return 0;
184 }
185
186 static int skl_runtime_resume(struct device *dev)
187 {
188         struct pci_dev *pci = to_pci_dev(dev);
189         struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
190         struct hdac_bus *bus = ebus_to_hbus(ebus);
191         struct skl *skl = ebus_to_skl(ebus);
192         int status;
193
194         dev_dbg(bus->dev, "in %s\n", __func__);
195
196         /* Read STATESTS before controller reset */
197         status = snd_hdac_chip_readw(bus, STATESTS);
198
199         skl_init_pci(skl);
200         snd_hdac_bus_init_chip(bus, true);
201         /* disable controller Wake Up event */
202         snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, 0);
203
204         return skl_resume_dsp(skl);
205 }
206 #endif /* CONFIG_PM */
207
208 static const struct dev_pm_ops skl_pm = {
209         SET_SYSTEM_SLEEP_PM_OPS(skl_suspend, skl_resume)
210         SET_RUNTIME_PM_OPS(skl_runtime_suspend, skl_runtime_resume, NULL)
211 };
212
213 /*
214  * destructor
215  */
216 static int skl_free(struct hdac_ext_bus *ebus)
217 {
218         struct skl *skl  = ebus_to_skl(ebus);
219         struct hdac_bus *bus = ebus_to_hbus(ebus);
220
221         skl->init_failed = 1; /* to be sure */
222
223         snd_hdac_ext_stop_streams(ebus);
224
225         if (bus->irq >= 0)
226                 free_irq(bus->irq, (void *)bus);
227         if (bus->remap_addr)
228                 iounmap(bus->remap_addr);
229
230         snd_hdac_bus_free_stream_pages(bus);
231         snd_hdac_stream_free_all(ebus);
232         snd_hdac_link_free_all(ebus);
233         pci_release_regions(skl->pci);
234         pci_disable_device(skl->pci);
235
236         snd_hdac_ext_bus_exit(ebus);
237
238         return 0;
239 }
240
241 static int skl_dmic_device_register(struct skl *skl)
242 {
243         struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
244         struct platform_device *pdev;
245         int ret;
246
247         /* SKL has one dmic port, so allocate dmic device for this */
248         pdev = platform_device_alloc("dmic-codec", -1);
249         if (!pdev) {
250                 dev_err(bus->dev, "failed to allocate dmic device\n");
251                 return -ENOMEM;
252         }
253
254         ret = platform_device_add(pdev);
255         if (ret) {
256                 dev_err(bus->dev, "failed to add dmic device: %d\n", ret);
257                 platform_device_put(pdev);
258                 return ret;
259         }
260         skl->dmic_dev = pdev;
261
262         return 0;
263 }
264
265 static void skl_dmic_device_unregister(struct skl *skl)
266 {
267         if (skl->dmic_dev)
268                 platform_device_unregister(skl->dmic_dev);
269 }
270
271 /*
272  * Probe the given codec address
273  */
274 static int probe_codec(struct hdac_ext_bus *ebus, int addr)
275 {
276         struct hdac_bus *bus = ebus_to_hbus(ebus);
277         unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
278                 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
279         unsigned int res;
280
281         mutex_lock(&bus->cmd_mutex);
282         snd_hdac_bus_send_cmd(bus, cmd);
283         snd_hdac_bus_get_response(bus, addr, &res);
284         mutex_unlock(&bus->cmd_mutex);
285         if (res == -1)
286                 return -EIO;
287         dev_dbg(bus->dev, "codec #%d probed OK\n", addr);
288
289         return snd_hdac_ext_bus_device_init(ebus, addr);
290 }
291
292 /* Codec initialization */
293 static int skl_codec_create(struct hdac_ext_bus *ebus)
294 {
295         struct hdac_bus *bus = ebus_to_hbus(ebus);
296         int c, max_slots;
297
298         max_slots = HDA_MAX_CODECS;
299
300         /* First try to probe all given codec slots */
301         for (c = 0; c < max_slots; c++) {
302                 if ((bus->codec_mask & (1 << c))) {
303                         if (probe_codec(ebus, c) < 0) {
304                                 /*
305                                  * Some BIOSen give you wrong codec addresses
306                                  * that don't exist
307                                  */
308                                 dev_warn(bus->dev,
309                                          "Codec #%d probe error; disabling it...\n", c);
310                                 bus->codec_mask &= ~(1 << c);
311                                 /*
312                                  * More badly, accessing to a non-existing
313                                  * codec often screws up the controller bus,
314                                  * and disturbs the further communications.
315                                  * Thus if an error occurs during probing,
316                                  * better to reset the controller bus to get
317                                  * back to the sanity state.
318                                  */
319                                 snd_hdac_bus_stop_chip(bus);
320                                 snd_hdac_bus_init_chip(bus, true);
321                         }
322                 }
323         }
324
325         return 0;
326 }
327
328 static const struct hdac_bus_ops bus_core_ops = {
329         .command = snd_hdac_bus_send_cmd,
330         .get_response = snd_hdac_bus_get_response,
331 };
332
333 /*
334  * constructor
335  */
336 static int skl_create(struct pci_dev *pci,
337                       const struct hdac_io_ops *io_ops,
338                       struct skl **rskl)
339 {
340         struct skl *skl;
341         struct hdac_ext_bus *ebus;
342
343         int err;
344
345         *rskl = NULL;
346
347         err = pci_enable_device(pci);
348         if (err < 0)
349                 return err;
350
351         skl = devm_kzalloc(&pci->dev, sizeof(*skl), GFP_KERNEL);
352         if (!skl) {
353                 pci_disable_device(pci);
354                 return -ENOMEM;
355         }
356         ebus = &skl->ebus;
357         snd_hdac_ext_bus_init(ebus, &pci->dev, &bus_core_ops, io_ops);
358         ebus->bus.use_posbuf = 1;
359         skl->pci = pci;
360
361         ebus->bus.bdl_pos_adj = 0;
362
363         *rskl = skl;
364
365         return 0;
366 }
367
368 static int skl_first_init(struct hdac_ext_bus *ebus)
369 {
370         struct skl *skl = ebus_to_skl(ebus);
371         struct hdac_bus *bus = ebus_to_hbus(ebus);
372         struct pci_dev *pci = skl->pci;
373         int err;
374         unsigned short gcap;
375         int cp_streams, pb_streams, start_idx;
376
377         err = pci_request_regions(pci, "Skylake HD audio");
378         if (err < 0)
379                 return err;
380
381         bus->addr = pci_resource_start(pci, 0);
382         bus->remap_addr = pci_ioremap_bar(pci, 0);
383         if (bus->remap_addr == NULL) {
384                 dev_err(bus->dev, "ioremap error\n");
385                 return -ENXIO;
386         }
387
388         snd_hdac_ext_bus_parse_capabilities(ebus);
389
390         if (skl_acquire_irq(ebus, 0) < 0)
391                 return -EBUSY;
392
393         pci_set_master(pci);
394         synchronize_irq(bus->irq);
395
396         gcap = snd_hdac_chip_readw(bus, GCAP);
397         dev_dbg(bus->dev, "chipset global capabilities = 0x%x\n", gcap);
398
399         /* allow 64bit DMA address if supported by H/W */
400         if (!dma_set_mask(bus->dev, DMA_BIT_MASK(64))) {
401                 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(64));
402         } else {
403                 dma_set_mask(bus->dev, DMA_BIT_MASK(32));
404                 dma_set_coherent_mask(bus->dev, DMA_BIT_MASK(32));
405         }
406
407         /* read number of streams from GCAP register */
408         cp_streams = (gcap >> 8) & 0x0f;
409         pb_streams = (gcap >> 12) & 0x0f;
410
411         if (!pb_streams && !cp_streams)
412                 return -EIO;
413
414         ebus->num_streams = cp_streams + pb_streams;
415
416         /* initialize streams */
417         snd_hdac_ext_stream_init_all
418                 (ebus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
419         start_idx = cp_streams;
420         snd_hdac_ext_stream_init_all
421                 (ebus, start_idx, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
422
423         err = snd_hdac_bus_alloc_stream_pages(bus);
424         if (err < 0)
425                 return err;
426
427         /* initialize chip */
428         skl_init_pci(skl);
429
430         snd_hdac_bus_init_chip(bus, true);
431
432         /* codec detection */
433         if (!bus->codec_mask) {
434                 dev_err(bus->dev, "no codecs found!\n");
435                 return -ENODEV;
436         }
437
438         return 0;
439 }
440
441 static int skl_probe(struct pci_dev *pci,
442                      const struct pci_device_id *pci_id)
443 {
444         struct skl *skl;
445         struct hdac_ext_bus *ebus = NULL;
446         struct hdac_bus *bus = NULL;
447         int err;
448
449         /* we use ext core ops, so provide NULL for ops here */
450         err = skl_create(pci, NULL, &skl);
451         if (err < 0)
452                 return err;
453
454         ebus = &skl->ebus;
455         bus = ebus_to_hbus(ebus);
456
457         err = skl_first_init(ebus);
458         if (err < 0)
459                 goto out_free;
460
461         skl->nhlt = skl_nhlt_init(bus->dev);
462
463         if (skl->nhlt == NULL)
464                 goto out_free;
465
466         pci_set_drvdata(skl->pci, ebus);
467
468         /* check if dsp is there */
469         if (ebus->ppcap) {
470                 err = skl_init_dsp(skl);
471                 if (err < 0) {
472                         dev_dbg(bus->dev, "error failed to register dsp\n");
473                         goto out_free;
474                 }
475         }
476         if (ebus->mlcap)
477                 snd_hdac_ext_bus_get_ml_capabilities(ebus);
478
479         /* create device for soc dmic */
480         err = skl_dmic_device_register(skl);
481         if (err < 0)
482                 goto out_dsp_free;
483
484         /* register platform dai and controls */
485         err = skl_platform_register(bus->dev);
486         if (err < 0)
487                 goto out_dmic_free;
488
489         /* create codec instances */
490         err = skl_codec_create(ebus);
491         if (err < 0)
492                 goto out_unregister;
493
494         /*configure PM */
495         pm_runtime_set_autosuspend_delay(bus->dev, SKL_SUSPEND_DELAY);
496         pm_runtime_use_autosuspend(bus->dev);
497         pm_runtime_put_noidle(bus->dev);
498         pm_runtime_allow(bus->dev);
499
500         return 0;
501
502 out_unregister:
503         skl_platform_unregister(bus->dev);
504 out_dmic_free:
505         skl_dmic_device_unregister(skl);
506 out_dsp_free:
507         skl_free_dsp(skl);
508 out_free:
509         skl->init_failed = 1;
510         skl_free(ebus);
511
512         return err;
513 }
514
515 static void skl_remove(struct pci_dev *pci)
516 {
517         struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
518         struct skl *skl = ebus_to_skl(ebus);
519
520         if (pci_dev_run_wake(pci))
521                 pm_runtime_get_noresume(&pci->dev);
522         pci_dev_put(pci);
523         skl_platform_unregister(&pci->dev);
524         skl_free_dsp(skl);
525         skl_dmic_device_unregister(skl);
526         skl_free(ebus);
527         dev_set_drvdata(&pci->dev, NULL);
528 }
529
530 /* PCI IDs */
531 static const struct pci_device_id skl_ids[] = {
532         /* Sunrise Point-LP */
533         { PCI_DEVICE(0x8086, 0x9d70), 0},
534         { 0, }
535 };
536 MODULE_DEVICE_TABLE(pci, skl_ids);
537
538 /* pci_driver definition */
539 static struct pci_driver skl_driver = {
540         .name = KBUILD_MODNAME,
541         .id_table = skl_ids,
542         .probe = skl_probe,
543         .remove = skl_remove,
544         .driver = {
545                 .pm = &skl_pm,
546         },
547 };
548 module_pci_driver(skl_driver);
549
550 MODULE_LICENSE("GPL v2");
551 MODULE_DESCRIPTION("Intel Skylake ASoC HDA driver");