Path: blob/master/sound/pci/cs5535audio/cs5535audio.c
10818 views
/*1* Driver for audio on multifunction CS5535/6 companion device2* Copyright (C) Jaya Kumar3*4* Based on Jaroslav Kysela and Takashi Iwai's examples.5* This work was sponsored by CIS(M) Sdn Bhd.6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA20*21*/2223#include <linux/delay.h>24#include <linux/interrupt.h>25#include <linux/init.h>26#include <linux/pci.h>27#include <linux/slab.h>28#include <linux/moduleparam.h>29#include <asm/io.h>30#include <sound/core.h>31#include <sound/control.h>32#include <sound/pcm.h>33#include <sound/rawmidi.h>34#include <sound/ac97_codec.h>35#include <sound/initval.h>36#include <sound/asoundef.h>37#include "cs5535audio.h"3839#define DRIVER_NAME "cs5535audio"4041static char *ac97_quirk;42module_param(ac97_quirk, charp, 0444);43MODULE_PARM_DESC(ac97_quirk, "AC'97 board specific workarounds.");4445static struct ac97_quirk ac97_quirks[] __devinitdata = {46#if 0 /* Not yet confirmed if all 5536 boards are HP only */47{48.subvendor = PCI_VENDOR_ID_AMD,49.subdevice = PCI_DEVICE_ID_AMD_CS5536_AUDIO,50.name = "AMD RDK",51.type = AC97_TUNE_HP_ONLY52},53#endif54{}55};5657static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;58static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;59static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;6061module_param_array(index, int, NULL, 0444);62MODULE_PARM_DESC(index, "Index value for " DRIVER_NAME);63module_param_array(id, charp, NULL, 0444);64MODULE_PARM_DESC(id, "ID string for " DRIVER_NAME);65module_param_array(enable, bool, NULL, 0444);66MODULE_PARM_DESC(enable, "Enable " DRIVER_NAME);6768static DEFINE_PCI_DEVICE_TABLE(snd_cs5535audio_ids) = {69{ PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_AUDIO) },70{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_AUDIO) },71{}72};7374MODULE_DEVICE_TABLE(pci, snd_cs5535audio_ids);7576static void wait_till_cmd_acked(struct cs5535audio *cs5535au, unsigned long timeout)77{78unsigned int tmp;79do {80tmp = cs_readl(cs5535au, ACC_CODEC_CNTL);81if (!(tmp & CMD_NEW))82break;83udelay(1);84} while (--timeout);85if (!timeout)86snd_printk(KERN_ERR "Failure writing to cs5535 codec\n");87}8889static unsigned short snd_cs5535audio_codec_read(struct cs5535audio *cs5535au,90unsigned short reg)91{92unsigned int regdata;93unsigned int timeout;94unsigned int val;9596regdata = ((unsigned int) reg) << 24;97regdata |= ACC_CODEC_CNTL_RD_CMD;98regdata |= CMD_NEW;99100cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);101wait_till_cmd_acked(cs5535au, 50);102103timeout = 50;104do {105val = cs_readl(cs5535au, ACC_CODEC_STATUS);106if ((val & STS_NEW) && reg == (val >> 24))107break;108udelay(1);109} while (--timeout);110if (!timeout)111snd_printk(KERN_ERR "Failure reading codec reg 0x%x,"112"Last value=0x%x\n", reg, val);113114return (unsigned short) val;115}116117static void snd_cs5535audio_codec_write(struct cs5535audio *cs5535au,118unsigned short reg, unsigned short val)119{120unsigned int regdata;121122regdata = ((unsigned int) reg) << 24;123regdata |= val;124regdata &= CMD_MASK;125regdata |= CMD_NEW;126regdata &= ACC_CODEC_CNTL_WR_CMD;127128cs_writel(cs5535au, ACC_CODEC_CNTL, regdata);129wait_till_cmd_acked(cs5535au, 50);130}131132static void snd_cs5535audio_ac97_codec_write(struct snd_ac97 *ac97,133unsigned short reg, unsigned short val)134{135struct cs5535audio *cs5535au = ac97->private_data;136snd_cs5535audio_codec_write(cs5535au, reg, val);137}138139static unsigned short snd_cs5535audio_ac97_codec_read(struct snd_ac97 *ac97,140unsigned short reg)141{142struct cs5535audio *cs5535au = ac97->private_data;143return snd_cs5535audio_codec_read(cs5535au, reg);144}145146static int __devinit snd_cs5535audio_mixer(struct cs5535audio *cs5535au)147{148struct snd_card *card = cs5535au->card;149struct snd_ac97_bus *pbus;150struct snd_ac97_template ac97;151int err;152static struct snd_ac97_bus_ops ops = {153.write = snd_cs5535audio_ac97_codec_write,154.read = snd_cs5535audio_ac97_codec_read,155};156157if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)158return err;159160memset(&ac97, 0, sizeof(ac97));161ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM162| AC97_SCAP_POWER_SAVE;163ac97.private_data = cs5535au;164ac97.pci = cs5535au->pci;165166/* set any OLPC-specific scaps */167olpc_prequirks(card, &ac97);168169if ((err = snd_ac97_mixer(pbus, &ac97, &cs5535au->ac97)) < 0) {170snd_printk(KERN_ERR "mixer failed\n");171return err;172}173174snd_ac97_tune_hardware(cs5535au->ac97, ac97_quirks, ac97_quirk);175176err = olpc_quirks(card, cs5535au->ac97);177if (err < 0) {178snd_printk(KERN_ERR "olpc quirks failed\n");179return err;180}181182return 0;183}184185static void process_bm0_irq(struct cs5535audio *cs5535au)186{187u8 bm_stat;188spin_lock(&cs5535au->reg_lock);189bm_stat = cs_readb(cs5535au, ACC_BM0_STATUS);190spin_unlock(&cs5535au->reg_lock);191if (bm_stat & EOP) {192struct cs5535audio_dma *dma;193dma = cs5535au->playback_substream->runtime->private_data;194snd_pcm_period_elapsed(cs5535au->playback_substream);195} else {196snd_printk(KERN_ERR "unexpected bm0 irq src, bm_stat=%x\n",197bm_stat);198}199}200201static void process_bm1_irq(struct cs5535audio *cs5535au)202{203u8 bm_stat;204spin_lock(&cs5535au->reg_lock);205bm_stat = cs_readb(cs5535au, ACC_BM1_STATUS);206spin_unlock(&cs5535au->reg_lock);207if (bm_stat & EOP) {208struct cs5535audio_dma *dma;209dma = cs5535au->capture_substream->runtime->private_data;210snd_pcm_period_elapsed(cs5535au->capture_substream);211}212}213214static irqreturn_t snd_cs5535audio_interrupt(int irq, void *dev_id)215{216u16 acc_irq_stat;217unsigned char count;218struct cs5535audio *cs5535au = dev_id;219220if (cs5535au == NULL)221return IRQ_NONE;222223acc_irq_stat = cs_readw(cs5535au, ACC_IRQ_STATUS);224225if (!acc_irq_stat)226return IRQ_NONE;227for (count = 0; count < 4; count++) {228if (acc_irq_stat & (1 << count)) {229switch (count) {230case IRQ_STS:231cs_readl(cs5535au, ACC_GPIO_STATUS);232break;233case WU_IRQ_STS:234cs_readl(cs5535au, ACC_GPIO_STATUS);235break;236case BM0_IRQ_STS:237process_bm0_irq(cs5535au);238break;239case BM1_IRQ_STS:240process_bm1_irq(cs5535au);241break;242default:243snd_printk(KERN_ERR "Unexpected irq src: "244"0x%x\n", acc_irq_stat);245break;246}247}248}249return IRQ_HANDLED;250}251252static int snd_cs5535audio_free(struct cs5535audio *cs5535au)253{254synchronize_irq(cs5535au->irq);255pci_set_power_state(cs5535au->pci, 3);256257if (cs5535au->irq >= 0)258free_irq(cs5535au->irq, cs5535au);259260pci_release_regions(cs5535au->pci);261pci_disable_device(cs5535au->pci);262kfree(cs5535au);263return 0;264}265266static int snd_cs5535audio_dev_free(struct snd_device *device)267{268struct cs5535audio *cs5535au = device->device_data;269return snd_cs5535audio_free(cs5535au);270}271272static int __devinit snd_cs5535audio_create(struct snd_card *card,273struct pci_dev *pci,274struct cs5535audio **rcs5535au)275{276struct cs5535audio *cs5535au;277278int err;279static struct snd_device_ops ops = {280.dev_free = snd_cs5535audio_dev_free,281};282283*rcs5535au = NULL;284if ((err = pci_enable_device(pci)) < 0)285return err;286287if (pci_set_dma_mask(pci, DMA_BIT_MASK(32)) < 0 ||288pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32)) < 0) {289printk(KERN_WARNING "unable to get 32bit dma\n");290err = -ENXIO;291goto pcifail;292}293294cs5535au = kzalloc(sizeof(*cs5535au), GFP_KERNEL);295if (cs5535au == NULL) {296err = -ENOMEM;297goto pcifail;298}299300spin_lock_init(&cs5535au->reg_lock);301cs5535au->card = card;302cs5535au->pci = pci;303cs5535au->irq = -1;304305if ((err = pci_request_regions(pci, "CS5535 Audio")) < 0) {306kfree(cs5535au);307goto pcifail;308}309310cs5535au->port = pci_resource_start(pci, 0);311312if (request_irq(pci->irq, snd_cs5535audio_interrupt,313IRQF_SHARED, "CS5535 Audio", cs5535au)) {314snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);315err = -EBUSY;316goto sndfail;317}318319cs5535au->irq = pci->irq;320pci_set_master(pci);321322if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,323cs5535au, &ops)) < 0)324goto sndfail;325326snd_card_set_dev(card, &pci->dev);327328*rcs5535au = cs5535au;329return 0;330331sndfail: /* leave the device alive, just kill the snd */332snd_cs5535audio_free(cs5535au);333return err;334335pcifail:336pci_disable_device(pci);337return err;338}339340static int __devinit snd_cs5535audio_probe(struct pci_dev *pci,341const struct pci_device_id *pci_id)342{343static int dev;344struct snd_card *card;345struct cs5535audio *cs5535au;346int err;347348if (dev >= SNDRV_CARDS)349return -ENODEV;350if (!enable[dev]) {351dev++;352return -ENOENT;353}354355err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);356if (err < 0)357return err;358359if ((err = snd_cs5535audio_create(card, pci, &cs5535au)) < 0)360goto probefail_out;361362card->private_data = cs5535au;363364if ((err = snd_cs5535audio_mixer(cs5535au)) < 0)365goto probefail_out;366367if ((err = snd_cs5535audio_pcm(cs5535au)) < 0)368goto probefail_out;369370strcpy(card->driver, DRIVER_NAME);371372strcpy(card->shortname, "CS5535 Audio");373sprintf(card->longname, "%s %s at 0x%lx, irq %i",374card->shortname, card->driver,375cs5535au->port, cs5535au->irq);376377if ((err = snd_card_register(card)) < 0)378goto probefail_out;379380pci_set_drvdata(pci, card);381dev++;382return 0;383384probefail_out:385snd_card_free(card);386return err;387}388389static void __devexit snd_cs5535audio_remove(struct pci_dev *pci)390{391olpc_quirks_cleanup();392snd_card_free(pci_get_drvdata(pci));393pci_set_drvdata(pci, NULL);394}395396static struct pci_driver driver = {397.name = DRIVER_NAME,398.id_table = snd_cs5535audio_ids,399.probe = snd_cs5535audio_probe,400.remove = __devexit_p(snd_cs5535audio_remove),401#ifdef CONFIG_PM402.suspend = snd_cs5535audio_suspend,403.resume = snd_cs5535audio_resume,404#endif405};406407static int __init alsa_card_cs5535audio_init(void)408{409return pci_register_driver(&driver);410}411412static void __exit alsa_card_cs5535audio_exit(void)413{414pci_unregister_driver(&driver);415}416417module_init(alsa_card_cs5535audio_init)418module_exit(alsa_card_cs5535audio_exit)419420MODULE_AUTHOR("Jaya Kumar");421MODULE_LICENSE("GPL");422MODULE_DESCRIPTION("CS5535 Audio");423MODULE_SUPPORTED_DEVICE("CS5535 Audio");424425426