net: dsa: mt7530: set CPU port to fallback mode
[linux/fpc-iii.git] / sound / pci / cs5535audio / cs5535audio.c
blob4590086d9cd8040ec0ff4d4a4b3220c5a2aaea77
1 /*
2 * Driver for audio on multifunction CS5535/6 companion device
3 * Copyright (C) Jaya Kumar
5 * Based on Jaroslav Kysela and Takashi Iwai's examples.
6 * This work was sponsored by CIS(M) Sdn Bhd.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/io.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/pcm.h>
34 #include <sound/rawmidi.h>
35 #include <sound/ac97_codec.h>
36 #include <sound/initval.h>
37 #include <sound/asoundef.h>
38 #include "cs5535audio.h"
40 #define DRIVER_NAME "cs5535audio"
42 static char *ac97_quirk;
43 module_param(ac97_quirk, charp, 0444);
44 MODULE_PARM_DESC(ac97_quirk, "AC'97 board specific workarounds.");
46 static const struct ac97_quirk ac97_quirks[] = {
47 #if 0 /* Not yet confirmed if all 5536 boards are HP only */
49 .subvendor = PCI_VENDOR_ID_AMD,
50 .subdevice = PCI_DEVICE_ID_AMD_CS5536_AUDIO,
51 .name = "AMD RDK",
52 .type = AC97_TUNE_HP_ONLY
54 #endif
58 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
59 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
60 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
62 module_param_array(index, int, NULL, 0444);
63 MODULE_PARM_DESC(index, "Index value for " DRIVER_NAME);
64 module_param_array(id, charp, NULL, 0444);
65 MODULE_PARM_DESC(id, "ID string for " DRIVER_NAME);
66 module_param_array(enable, bool, NULL, 0444);
67 MODULE_PARM_DESC(enable, "Enable " DRIVER_NAME);
69 static const struct pci_device_id snd_cs5535audio_ids[] = {
70 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO) },
71 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_AUDIO) },
75 MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);
77 static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)
79 unsigned int tmp;
80 do {
81 tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);
82 if (!(tmp & CMD_NEW))
83 break;
84 udelay(1);
85 } while (--timeout);
86 if (!timeout)
87 dev_err(cs5535au->card->dev,
88 "Failure writing to cs5535 codec\n");
91 static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,
92 unsigned short reg)
94 unsigned int regdata;
95 unsigned int timeout;
96 unsigned int val;
98 regdata = ((unsigned int) reg) << 24;
99 regdata |= ACC_CODEC_CNTL_RD_CMD;
100 regdata |= CMD_NEW;
102 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
103 wait_till_cmd_acked(cs5535au, 50);
105 timeout = 50;
106 do {
107 val = cs_readl(cs5535au, ACC_CODEC_STATUS);
108 if ((val & STS_NEW) && reg == (val >> 24))
109 break;
110 udelay(1);
111 } while (--timeout);
112 if (!timeout)
113 dev_err(cs5535au->card->dev,
114 "Failure reading codec reg 0x%x, Last value=0x%x\n",
115 reg, val);
117 return (unsigned short) val;
120 static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,
121 unsigned short reg, unsigned short val)
123 unsigned int regdata;
125 regdata = ((unsigned int) reg) << 24;
126 regdata |= val;
127 regdata &= CMD_MASK;
128 regdata |= CMD_NEW;
129 regdata &= ACC_CODEC_CNTL_WR_CMD;
131 cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);
132 wait_till_cmd_acked(cs5535au, 50);
135 static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,
136 unsigned short reg, unsigned short val)
138 struct cs5535audio *cs5535au = ac97->private_data;
139 snd_cs5535audio_codec_write(cs5535au, reg, val);
142 static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,
143 unsigned short reg)
145 struct cs5535audio *cs5535au = ac97->private_data;
146 return snd_cs5535audio_codec_read(cs5535au, reg);
149 static int snd_cs5535audio_mixer(struct cs5535audio *cs5535au)
151 struct snd_card *card = cs5535au->card;
152 struct snd_ac97_bus *pbus;
153 struct snd_ac97_template ac97;
154 int err;
155 static struct snd_ac97_bus_ops ops = {
156 .write = snd_cs5535audio_ac97_codec_write,
157 .read = snd_cs5535audio_ac97_codec_read,
160 if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
161 return err;
163 memset(&ac97, 0, sizeof(ac97));
164 ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM
165 | AC97_SCAP_POWER_SAVE;
166 ac97.private_data = cs5535au;
167 ac97.pci = cs5535au->pci;
169 /* set any OLPC-specific scaps */
170 olpc_prequirks(card, &ac97);
172 if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {
173 dev_err(card->dev, "mixer failed\n");
174 return err;
177 snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk);
179 err = olpc_quirks(card, cs5535au->ac97);
180 if (err < 0) {
181 dev_err(card->dev, "olpc quirks failed\n");
182 return err;
185 return 0;
188 static void process_bm0_irq(struct cs5535audio *cs5535au)
190 u8 bm_stat;
191 spin_lock(&cs5535au->reg_lock);
192 bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);
193 spin_unlock(&cs5535au->reg_lock);
194 if (bm_stat & EOP) {
195 snd_pcm_period_elapsed(cs5535au->playback_substream);
196 } else {
197 dev_err(cs5535au->card->dev,
198 "unexpected bm0 irq src, bm_stat=%x\n",
199 bm_stat);
203 static void process_bm1_irq(struct cs5535audio *cs5535au)
205 u8 bm_stat;
206 spin_lock(&cs5535au->reg_lock);
207 bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);
208 spin_unlock(&cs5535au->reg_lock);
209 if (bm_stat & EOP)
210 snd_pcm_period_elapsed(cs5535au->capture_substream);
213 static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id)
215 u16 acc_irq_stat;
216 unsigned char count;
217 struct cs5535audio *cs5535au = dev_id;
219 if (cs5535au == NULL)
220 return IRQ_NONE;
222 acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);
224 if (!acc_irq_stat)
225 return IRQ_NONE;
226 for (count = 0; count < 4; count++) {
227 if (acc_irq_stat & (1 << count)) {
228 switch (count) {
229 case IRQ_STS:
230 cs_readl(cs5535au, ACC_GPIO_STATUS);
231 break;
232 case WU_IRQ_STS:
233 cs_readl(cs5535au, ACC_GPIO_STATUS);
234 break;
235 case BM0_IRQ_STS:
236 process_bm0_irq(cs5535au);
237 break;
238 case BM1_IRQ_STS:
239 process_bm1_irq(cs5535au);
240 break;
241 default:
242 dev_err(cs5535au->card->dev,
243 "Unexpected irq src: 0x%x\n",
244 acc_irq_stat);
245 break;
249 return IRQ_HANDLED;
252 static int snd_cs5535audio_free(struct cs5535audio *cs5535au)
254 synchronize_irq(cs5535au->irq);
255 pci_set_power_state(cs5535au->pci, PCI_D3hot);
257 if (cs5535au->irq >= 0)
258 free_irq(cs5535au->irq, cs5535au);
260 pci_release_regions(cs5535au->pci);
261 pci_disable_device(cs5535au->pci);
262 kfree(cs5535au);
263 return 0;
266 static int snd_cs5535audio_dev_free(struct snd_device *device)
268 struct cs5535audio *cs5535au = device->device_data;
269 return snd_cs5535audio_free(cs5535au);
272 static int snd_cs5535audio_create(struct snd_card *card,
273 struct pci_dev *pci,
274 struct cs5535audio **rcs5535au)
276 struct cs5535audio *cs5535au;
278 int err;
279 static struct snd_device_ops ops = {
280 .dev_free = snd_cs5535audio_dev_free,
283 *rcs5535au = NULL;
284 if ((err = pci_enable_device(pci)) < 0)
285 return err;
287 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0 ||
288 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) {
289 dev_warn(card->dev, "unable to get 32bit dma\n");
290 err = -ENXIO;
291 goto pcifail;
294 cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);
295 if (cs5535au == NULL) {
296 err = -ENOMEM;
297 goto pcifail;
300 spin_lock_init(&cs5535au->reg_lock);
301 cs5535au->card = card;
302 cs5535au->pci = pci;
303 cs5535au->irq = -1;
305 if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {
306 kfree(cs5535au);
307 goto pcifail;
310 cs5535au->port = pci_resource_start(pci, 0);
312 if (request_irq(pci->irq, snd_cs5535audio_interrupt,
313 IRQF_SHARED, KBUILD_MODNAME, cs5535au)) {
314 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
315 err = -EBUSY;
316 goto sndfail;
319 cs5535au->irq = pci->irq;
320 pci_set_master(pci);
322 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
323 cs5535au, &ops)) < 0)
324 goto sndfail;
326 *rcs5535au = cs5535au;
327 return 0;
329 sndfail: /* leave the device alive, just kill the snd */
330 snd_cs5535audio_free(cs5535au);
331 return err;
333 pcifail:
334 pci_disable_device(pci);
335 return err;
338 static int snd_cs5535audio_probe(struct pci_dev *pci,
339 const struct pci_device_id *pci_id)
341 static int dev;
342 struct snd_card *card;
343 struct cs5535audio *cs5535au;
344 int err;
346 if (dev >= SNDRV_CARDS)
347 return -ENODEV;
348 if (!enable[dev]) {
349 dev++;
350 return -ENOENT;
353 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
354 0, &card);
355 if (err < 0)
356 return err;
358 if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)
359 goto probefail_out;
361 card->private_data = cs5535au;
363 if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)
364 goto probefail_out;
366 if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)
367 goto probefail_out;
369 strcpy(card->driver, DRIVER_NAME);
371 strcpy(card->shortname, "CS5535 Audio");
372 sprintf(card->longname, "%s %s at 0x%lx, irq %i",
373 card->shortname, card->driver,
374 cs5535au->port, cs5535au->irq);
376 if ((err = snd_card_register(card)) < 0)
377 goto probefail_out;
379 pci_set_drvdata(pci, card);
380 dev++;
381 return 0;
383 probefail_out:
384 snd_card_free(card);
385 return err;
388 static void snd_cs5535audio_remove(struct pci_dev *pci)
390 olpc_quirks_cleanup();
391 snd_card_free(pci_get_drvdata(pci));
394 static struct pci_driver cs5535audio_driver = {
395 .name = KBUILD_MODNAME,
396 .id_table = snd_cs5535audio_ids,
397 .probe = snd_cs5535audio_probe,
398 .remove = snd_cs5535audio_remove,
399 #ifdef CONFIG_PM_SLEEP
400 .driver = {
401 .pm = &snd_cs5535audio_pm,
403 #endif
406 module_pci_driver(cs5535audio_driver);
408 MODULE_AUTHOR("Jaya Kumar");
409 MODULE_LICENSE("GPL");
410 MODULE_DESCRIPTION("CS5535 Audio");
411 MODULE_SUPPORTED_DEVICE("CS5535 Audio");