blob: 68db7dec90b3ff716b4c597555aa2e1c4beac018 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jaya Kumar9b4ffa42005-11-17 10:12:23 +01002/*
Jaya Kumar9ac25592006-04-28 14:34:49 +02003 * Driver for audio on multifunction CS5535/6 companion device
Jaya Kumar9b4ffa42005-11-17 10:12:23 +01004 * Copyright (C) Jaya Kumar
5 *
6 * Based on Jaroslav Kysela and Takashi Iwai's examples.
7 * This work was sponsored by CIS(M) Sdn Bhd.
Jaya Kumar9b4ffa42005-11-17 10:12:23 +01008 */
9
10#include <linux/delay.h>
11#include <linux/interrupt.h>
12#include <linux/init.h>
13#include <linux/pci.h>
14#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040015#include <linux/module.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010016#include <linux/io.h>
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010017#include <sound/core.h>
18#include <sound/control.h>
19#include <sound/pcm.h>
20#include <sound/rawmidi.h>
21#include <sound/ac97_codec.h>
22#include <sound/initval.h>
23#include <sound/asoundef.h>
24#include "cs5535audio.h"
25
26#define DRIVER_NAME "cs5535audio"
27
Jaya Kumar9ac25592006-04-28 14:34:49 +020028static char *ac97_quirk;
29module_param(ac97_quirk, charp, 0444);
30MODULE_PARM_DESC(ac97_quirk, "AC'97 board specific workarounds.");
31
Joe Percheseab0fbf2015-04-04 13:38:25 -070032static const struct ac97_quirk ac97_quirks[] = {
Jaya Kumar9ac25592006-04-28 14:34:49 +020033#if 0 /* Not yet confirmed if all 5536 boards are HP only */
34 {
35 .subvendor = PCI_VENDOR_ID_AMD,
36 .subdevice = PCI_DEVICE_ID_AMD_CS5536_AUDIO,
37 .name = "AMD RDK",
38 .type = AC97_TUNE_HP_ONLY
39 },
40#endif
41 {}
42};
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010043
Takashi Iwai302e4c22006-05-23 13:24:30 +020044static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
45static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
Rusty Russella67ff6a2011-12-15 13:49:36 +103046static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010047
Takashi Iwai302e4c22006-05-23 13:24:30 +020048module_param_array(index, int, NULL, 0444);
Takashi Iwai1dbfd8c2006-05-02 18:31:31 +020049MODULE_PARM_DESC(index, "Index value for " DRIVER_NAME);
Takashi Iwai302e4c22006-05-23 13:24:30 +020050module_param_array(id, charp, NULL, 0444);
Takashi Iwai1dbfd8c2006-05-02 18:31:31 +020051MODULE_PARM_DESC(id, "ID string for " DRIVER_NAME);
Takashi Iwai302e4c22006-05-23 13:24:30 +020052module_param_array(enable, bool, NULL, 0444);
53MODULE_PARM_DESC(enable, "Enable " DRIVER_NAME);
Takashi Iwai1dbfd8c2006-05-02 18:31:31 +020054
Benoit Taine9baa3c32014-08-08 15:56:03 +020055static const struct pci_device_id snd_cs5535audio_ids[] = {
Jaya Kumar9ac25592006-04-28 14:34:49 +020056 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO) },
57 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_AUDIO) },
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010058 {}
59};
60
61MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
62
Takashi Iwai66f8df62005-11-17 14:56:21 +010063static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010064{
Takashi Iwai3e873172005-11-17 10:15:37 +010065 unsigned int tmp;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010066 do {
67 tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
68 if (!(tmp & CMD_NEW))
69 break;
David Vrabel10375932006-03-03 18:01:57 +010070 udelay(1);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010071 } while (--timeout);
72 if (!timeout)
Takashi Iwai00980aa2014-02-25 16:10:25 +010073 dev_err(cs5535au->card->dev,
74 "Failure writing to cs5535 codec\n");
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010075}
76
Takashi Iwai66f8df62005-11-17 14:56:21 +010077static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
78 unsigned short reg)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010079{
Takashi Iwai3e873172005-11-17 10:15:37 +010080 unsigned int regdata;
Takashi Iwai66f8df62005-11-17 14:56:21 +010081 unsigned int timeout;
Takashi Iwai3e873172005-11-17 10:15:37 +010082 unsigned int val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010083
Takashi Iwai3e873172005-11-17 10:15:37 +010084 regdata = ((unsigned int) reg) << 24;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010085 regdata |= ACC_CODEC_CNTL_RD_CMD;
86 regdata |= CMD_NEW;
87
88 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
David Vrabel10375932006-03-03 18:01:57 +010089 wait_till_cmd_acked(cs5535au, 50);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010090
91 timeout = 50;
92 do {
93 val = cs_readl(cs5535au, ACC_CODEC_STATUS);
Takashi Iwai3e873172005-11-17 10:15:37 +010094 if ((val & STS_NEW) && reg == (val >> 24))
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010095 break;
David Vrabel10375932006-03-03 18:01:57 +010096 udelay(1);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +010097 } while (--timeout);
98 if (!timeout)
Takashi Iwai00980aa2014-02-25 16:10:25 +010099 dev_err(cs5535au->card->dev,
100 "Failure reading codec reg 0x%x, Last value=0x%x\n",
101 reg, val);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100102
Takashi Iwai3e873172005-11-17 10:15:37 +0100103 return (unsigned short) val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100104}
105
Takashi Iwai66f8df62005-11-17 14:56:21 +0100106static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
107 unsigned short reg, unsigned short val)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100108{
Takashi Iwai3e873172005-11-17 10:15:37 +0100109 unsigned int regdata;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100110
Takashi Iwai3e873172005-11-17 10:15:37 +0100111 regdata = ((unsigned int) reg) << 24;
112 regdata |= val;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100113 regdata &= CMD_MASK;
114 regdata |= CMD_NEW;
115 regdata &= ACC_CODEC_CNTL_WR_CMD;
116
117 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
118 wait_till_cmd_acked(cs5535au, 50);
119}
120
Takashi Iwai66f8df62005-11-17 14:56:21 +0100121static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
122 unsigned short reg, unsigned short val)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100123{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100124 struct cs5535audio *cs5535au = ac97->private_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100125 snd_cs5535audio_codec_write(cs5535au, reg, val);
126}
127
Takashi Iwai66f8df62005-11-17 14:56:21 +0100128static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
129 unsigned short reg)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100130{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100131 struct cs5535audio *cs5535au = ac97->private_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100132 return snd_cs5535audio_codec_read(cs5535au, reg);
133}
134
Bill Pembertone23e7a12012-12-06 12:35:10 -0500135static int snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100136{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100137 struct snd_card *card = cs5535au->card;
138 struct snd_ac97_bus *pbus;
139 struct snd_ac97_template ac97;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100140 int err;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100141 static struct snd_ac97_bus_ops ops = {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100142 .write = snd_cs5535audio_ac97_codec_write,
143 .read = snd_cs5535audio_ac97_codec_read,
144 };
145
146 if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
147 return err;
148
149 memset(&ac97, 0, sizeof(ac97));
Jaya Kumarb035ce02008-11-05 17:30:08 -0500150 ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM
151 | AC97_SCAP_POWER_SAVE;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100152 ac97.private_data = cs5535au;
153 ac97.pci = cs5535au->pci;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100154
Andres Salomon3556d182008-11-06 16:44:08 -0500155 /* set any OLPC-specific scaps */
156 olpc_prequirks(card, &ac97);
157
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100158 if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
Takashi Iwai00980aa2014-02-25 16:10:25 +0100159 dev_err(card->dev, "mixer failed\n");
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100160 return err;
161 }
162
Jaya Kumar9ac25592006-04-28 14:34:49 +0200163 snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk);
164
Jaya Kumar57d4bf62008-11-06 16:43:34 -0500165 err = olpc_quirks(card, cs5535au->ac97);
166 if (err < 0) {
Takashi Iwai00980aa2014-02-25 16:10:25 +0100167 dev_err(card->dev, "olpc quirks failed\n");
Jaya Kumar57d4bf62008-11-06 16:43:34 -0500168 return err;
169 }
170
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100171 return 0;
172}
173
Takashi Iwai66f8df62005-11-17 14:56:21 +0100174static void process_bm0_irq(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100175{
176 u8 bm_stat;
177 spin_lock(&cs5535au->reg_lock);
178 bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
179 spin_unlock(&cs5535au->reg_lock);
180 if (bm_stat & EOP) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100181 snd_pcm_period_elapsed(cs5535au->playback_substream);
182 } else {
Takashi Iwai00980aa2014-02-25 16:10:25 +0100183 dev_err(cs5535au->card->dev,
184 "unexpected bm0 irq src, bm_stat=%x\n",
185 bm_stat);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100186 }
187}
188
Takashi Iwai66f8df62005-11-17 14:56:21 +0100189static void process_bm1_irq(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100190{
191 u8 bm_stat;
192 spin_lock(&cs5535au->reg_lock);
193 bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
194 spin_unlock(&cs5535au->reg_lock);
Colin Ian King45bf4102018-08-01 11:47:35 +0100195 if (bm_stat & EOP)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100196 snd_pcm_period_elapsed(cs5535au->capture_substream);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100197}
198
David Howells7d12e782006-10-05 14:55:46 +0100199static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100200{
201 u16 acc_irq_stat;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100202 unsigned char count;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100203 struct cs5535audio *cs5535au = dev_id;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100204
205 if (cs5535au == NULL)
206 return IRQ_NONE;
207
208 acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
209
210 if (!acc_irq_stat)
211 return IRQ_NONE;
Andres Salomon4ea24162007-09-03 15:43:43 +0200212 for (count = 0; count < 4; count++) {
Takashi Iwai3e873172005-11-17 10:15:37 +0100213 if (acc_irq_stat & (1 << count)) {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100214 switch (count) {
215 case IRQ_STS:
216 cs_readl(cs5535au, ACC_GPIO_STATUS);
217 break;
218 case WU_IRQ_STS:
219 cs_readl(cs5535au, ACC_GPIO_STATUS);
220 break;
221 case BM0_IRQ_STS:
222 process_bm0_irq(cs5535au);
223 break;
224 case BM1_IRQ_STS:
225 process_bm1_irq(cs5535au);
226 break;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100227 default:
Takashi Iwai00980aa2014-02-25 16:10:25 +0100228 dev_err(cs5535au->card->dev,
229 "Unexpected irq src: 0x%x\n",
230 acc_irq_stat);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100231 break;
232 }
233 }
234 }
235 return IRQ_HANDLED;
236}
237
Takashi Iwai66f8df62005-11-17 14:56:21 +0100238static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100239{
240 synchronize_irq(cs5535au->irq);
Julia Lawallec06b2b2014-01-03 00:40:30 +0100241 pci_set_power_state(cs5535au->pci, PCI_D3hot);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100242
243 if (cs5535au->irq >= 0)
244 free_irq(cs5535au->irq, cs5535au);
245
246 pci_release_regions(cs5535au->pci);
247 pci_disable_device(cs5535au->pci);
248 kfree(cs5535au);
249 return 0;
250}
251
Takashi Iwai66f8df62005-11-17 14:56:21 +0100252static int snd_cs5535audio_dev_free(struct snd_device *device)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100253{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100254 struct cs5535audio *cs5535au = device->device_data;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100255 return snd_cs5535audio_free(cs5535au);
256}
257
Bill Pembertone23e7a12012-12-06 12:35:10 -0500258static int snd_cs5535audio_create(struct snd_card *card,
259 struct pci_dev *pci,
260 struct cs5535audio **rcs5535au)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100261{
Takashi Iwai66f8df62005-11-17 14:56:21 +0100262 struct cs5535audio *cs5535au;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100263
264 int err;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100265 static struct snd_device_ops ops = {
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100266 .dev_free = snd_cs5535audio_dev_free,
267 };
268
269 *rcs5535au = NULL;
270 if ((err = pci_enable_device(pci)) < 0)
271 return err;
272
Quentin Lambert412b9792015-04-15 16:10:17 +0200273 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0 ||
274 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) {
Takashi Iwai00980aa2014-02-25 16:10:25 +0100275 dev_warn(card->dev, "unable to get 32bit dma\n");
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100276 err = -ENXIO;
277 goto pcifail;
278 }
279
280 cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
281 if (cs5535au == NULL) {
282 err = -ENOMEM;
283 goto pcifail;
284 }
285
286 spin_lock_init(&cs5535au->reg_lock);
287 cs5535au->card = card;
288 cs5535au->pci = pci;
289 cs5535au->irq = -1;
290
291 if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
292 kfree(cs5535au);
293 goto pcifail;
294 }
295
296 cs5535au->port = pci_resource_start(pci, 0);
297
298 if (request_irq(pci->irq, snd_cs5535audio_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +0200299 IRQF_SHARED, KBUILD_MODNAME, cs5535au)) {
Takashi Iwai00980aa2014-02-25 16:10:25 +0100300 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100301 err = -EBUSY;
302 goto sndfail;
303 }
304
305 cs5535au->irq = pci->irq;
306 pci_set_master(pci);
307
308 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
Takashi Iwai66f8df62005-11-17 14:56:21 +0100309 cs5535au, &ops)) < 0)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100310 goto sndfail;
311
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100312 *rcs5535au = cs5535au;
313 return 0;
314
315sndfail: /* leave the device alive, just kill the snd */
316 snd_cs5535audio_free(cs5535au);
317 return err;
318
319pcifail:
320 pci_disable_device(pci);
321 return err;
322}
323
Bill Pembertone23e7a12012-12-06 12:35:10 -0500324static int snd_cs5535audio_probe(struct pci_dev *pci,
325 const struct pci_device_id *pci_id)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100326{
327 static int dev;
Takashi Iwai66f8df62005-11-17 14:56:21 +0100328 struct snd_card *card;
329 struct cs5535audio *cs5535au;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100330 int err;
331
332 if (dev >= SNDRV_CARDS)
333 return -ENODEV;
Takashi Iwai302e4c22006-05-23 13:24:30 +0200334 if (!enable[dev]) {
335 dev++;
336 return -ENOENT;
337 }
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100338
Takashi Iwai60c57722014-01-29 14:20:19 +0100339 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
340 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +0100341 if (err < 0)
342 return err;
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100343
344 if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
345 goto probefail_out;
346
Jaya Kumar9ac25592006-04-28 14:34:49 +0200347 card->private_data = cs5535au;
348
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100349 if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
350 goto probefail_out;
351
352 if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
353 goto probefail_out;
354
355 strcpy(card->driver, DRIVER_NAME);
356
357 strcpy(card->shortname, "CS5535 Audio");
358 sprintf(card->longname, "%s %s at 0x%lx, irq %i",
359 card->shortname, card->driver,
360 cs5535au->port, cs5535au->irq);
361
362 if ((err = snd_card_register(card)) < 0)
363 goto probefail_out;
364
365 pci_set_drvdata(pci, card);
366 dev++;
367 return 0;
368
369probefail_out:
370 snd_card_free(card);
371 return err;
372}
373
Bill Pembertone23e7a12012-12-06 12:35:10 -0500374static void snd_cs5535audio_remove(struct pci_dev *pci)
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100375{
Andres Salomon3c554942009-12-14 18:00:36 -0800376 olpc_quirks_cleanup();
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100377 snd_card_free(pci_get_drvdata(pci));
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100378}
379
Takashi Iwaie9f66d92012-04-24 12:25:00 +0200380static struct pci_driver cs5535audio_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +0200381 .name = KBUILD_MODNAME,
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100382 .id_table = snd_cs5535audio_ids,
383 .probe = snd_cs5535audio_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -0500384 .remove = snd_cs5535audio_remove,
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200385#ifdef CONFIG_PM_SLEEP
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200386 .driver = {
387 .pm = &snd_cs5535audio_pm,
388 },
Jaya Kumar9ac25592006-04-28 14:34:49 +0200389#endif
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100390};
391
Takashi Iwaie9f66d92012-04-24 12:25:00 +0200392module_pci_driver(cs5535audio_driver);
Jaya Kumar9b4ffa42005-11-17 10:12:23 +0100393
394MODULE_AUTHOR("Jaya Kumar");
395MODULE_LICENSE("GPL");
396MODULE_DESCRIPTION("CS5535 Audio");
397MODULE_SUPPORTED_DEVICE("CS5535 Audio");