xref: /openbmc/linux/sound/isa/ad1848/ad1848.c (revision f7a9275d949cb0bf1f259a1546e52a0bf518151c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Generic driver for AD1848/AD1847/CS4248 chips (0.1 Alpha)
31da177e4SLinus Torvalds  *  Copyright (c) by Tugrul Galatali <galatalt@stuy.edu>,
41da177e4SLinus Torvalds  *                   Jaroslav Kysela <perex@suse.cz>
51da177e4SLinus Torvalds  *  Based on card-4232.c by Jaroslav Kysela <perex@suse.cz>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
91da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
101da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
111da177e4SLinus Torvalds  *   (at your option) any later version.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
141da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
151da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
161da177e4SLinus Torvalds  *   GNU General Public License for more details.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
191da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
201da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <sound/driver.h>
251da177e4SLinus Torvalds #include <linux/init.h>
2668668845STakashi Iwai #include <linux/err.h>
2768668845STakashi Iwai #include <linux/platform_device.h>
281da177e4SLinus Torvalds #include <linux/time.h>
291da177e4SLinus Torvalds #include <linux/wait.h>
301da177e4SLinus Torvalds #include <linux/moduleparam.h>
311da177e4SLinus Torvalds #include <sound/core.h>
321da177e4SLinus Torvalds #include <sound/ad1848.h>
331da177e4SLinus Torvalds #include <sound/initval.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds MODULE_AUTHOR("Tugrul Galatali <galatalt@stuy.edu>, Jaroslav Kysela <perex@suse.cz>");
361da177e4SLinus Torvalds MODULE_DESCRIPTION("AD1848/AD1847/CS4248");
371da177e4SLinus Torvalds MODULE_LICENSE("GPL");
381da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{Analog Devices,AD1848},"
391da177e4SLinus Torvalds 	        "{Analog Devices,AD1847},"
401da177e4SLinus Torvalds 		"{Crystal Semiconductors,CS4248}}");
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
431da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
441da177e4SLinus Torvalds static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;	/* Enable this card */
451da177e4SLinus Torvalds static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
461da177e4SLinus Torvalds static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 5,7,9,11,12,15 */
471da177e4SLinus Torvalds static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3,5,6,7 */
481da177e4SLinus Torvalds static int thinkpad[SNDRV_CARDS];			/* Thinkpad special case */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
511da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for AD1848 soundcard.");
521da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
531da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for AD1848 soundcard.");
541da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
551da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable AD1848 soundcard.");
561da177e4SLinus Torvalds module_param_array(port, long, NULL, 0444);
571da177e4SLinus Torvalds MODULE_PARM_DESC(port, "Port # for AD1848 driver.");
581da177e4SLinus Torvalds module_param_array(irq, int, NULL, 0444);
591da177e4SLinus Torvalds MODULE_PARM_DESC(irq, "IRQ # for AD1848 driver.");
601da177e4SLinus Torvalds module_param_array(dma1, int, NULL, 0444);
611da177e4SLinus Torvalds MODULE_PARM_DESC(dma1, "DMA1 # for AD1848 driver.");
621da177e4SLinus Torvalds module_param_array(thinkpad, bool, NULL, 0444);
631da177e4SLinus Torvalds MODULE_PARM_DESC(thinkpad, "Enable only for the onboard CS4248 of IBM Thinkpad 360/750/755 series.");
641da177e4SLinus Torvalds 
65*f7a9275dSClemens Ladisch static struct platform_device *devices[SNDRV_CARDS];
66*f7a9275dSClemens Ladisch 
671da177e4SLinus Torvalds 
6868668845STakashi Iwai static int __init snd_ad1848_probe(struct platform_device *pdev)
691da177e4SLinus Torvalds {
7068668845STakashi Iwai 	int dev = pdev->id;
71c8ff6647STakashi Iwai 	struct snd_card *card;
72c8ff6647STakashi Iwai 	struct snd_ad1848 *chip;
73c8ff6647STakashi Iwai 	struct snd_pcm *pcm;
741da177e4SLinus Torvalds 	int err;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	if (port[dev] == SNDRV_AUTO_PORT) {
771da177e4SLinus Torvalds 		snd_printk(KERN_ERR "ad1848: specify port\n");
781da177e4SLinus Torvalds 		return -EINVAL;
791da177e4SLinus Torvalds 	}
801da177e4SLinus Torvalds 	if (irq[dev] == SNDRV_AUTO_IRQ) {
811da177e4SLinus Torvalds 		snd_printk(KERN_ERR "ad1848: specify irq\n");
821da177e4SLinus Torvalds 		return -EINVAL;
831da177e4SLinus Torvalds 	}
841da177e4SLinus Torvalds 	if (dma1[dev] == SNDRV_AUTO_DMA) {
851da177e4SLinus Torvalds 		snd_printk(KERN_ERR "ad1848: specify dma1\n");
861da177e4SLinus Torvalds 		return -EINVAL;
871da177e4SLinus Torvalds 	}
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
901da177e4SLinus Torvalds 	if (card == NULL)
911da177e4SLinus Torvalds 		return -ENOMEM;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	if ((err = snd_ad1848_create(card, port[dev],
941da177e4SLinus Torvalds 				     irq[dev],
951da177e4SLinus Torvalds 				     dma1[dev],
961da177e4SLinus Torvalds 				     thinkpad[dev] ? AD1848_HW_THINKPAD : AD1848_HW_DETECT,
9743bcd973STakashi Iwai 				     &chip)) < 0)
9843bcd973STakashi Iwai 		goto _err;
9968668845STakashi Iwai 	card->private_data = chip;
1001da177e4SLinus Torvalds 
10143bcd973STakashi Iwai 	if ((err = snd_ad1848_pcm(chip, 0, &pcm)) < 0)
10243bcd973STakashi Iwai 		goto _err;
10343bcd973STakashi Iwai 
10443bcd973STakashi Iwai 	if ((err = snd_ad1848_mixer(chip)) < 0)
10543bcd973STakashi Iwai 		goto _err;
10643bcd973STakashi Iwai 
1071da177e4SLinus Torvalds 	strcpy(card->driver, "AD1848");
1081da177e4SLinus Torvalds 	strcpy(card->shortname, pcm->name);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
1111da177e4SLinus Torvalds 		pcm->name, chip->port, irq[dev], dma1[dev]);
1121da177e4SLinus Torvalds 
11343bcd973STakashi Iwai 	if (thinkpad[dev])
1141da177e4SLinus Torvalds 		strcat(card->longname, " [Thinkpad]");
1151da177e4SLinus Torvalds 
11668668845STakashi Iwai 	snd_card_set_dev(card, &pdev->dev);
11743bcd973STakashi Iwai 
11843bcd973STakashi Iwai 	if ((err = snd_card_register(card)) < 0)
11943bcd973STakashi Iwai 		goto _err;
12043bcd973STakashi Iwai 
12168668845STakashi Iwai 	platform_set_drvdata(pdev, card);
1221da177e4SLinus Torvalds 	return 0;
12343bcd973STakashi Iwai 
12443bcd973STakashi Iwai  _err:
12543bcd973STakashi Iwai 	snd_card_free(card);
12643bcd973STakashi Iwai 	return err;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
12968668845STakashi Iwai static int __devexit snd_ad1848_remove(struct platform_device *devptr)
13068668845STakashi Iwai {
13168668845STakashi Iwai 	snd_card_free(platform_get_drvdata(devptr));
13268668845STakashi Iwai 	platform_set_drvdata(devptr, NULL);
13368668845STakashi Iwai 	return 0;
13468668845STakashi Iwai }
13568668845STakashi Iwai 
13668668845STakashi Iwai #ifdef CONFIG_PM
13768668845STakashi Iwai static int snd_ad1848_suspend(struct platform_device *pdev, pm_message_t state)
13868668845STakashi Iwai {
13968668845STakashi Iwai 	struct snd_card *card = platform_get_drvdata(pdev);
14068668845STakashi Iwai 	struct snd_ad1848 *chip = card->private_data;
14168668845STakashi Iwai 
14268668845STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
14368668845STakashi Iwai 	chip->suspend(chip);
14468668845STakashi Iwai 	return 0;
14568668845STakashi Iwai }
14668668845STakashi Iwai 
14768668845STakashi Iwai static int snd_ad1848_resume(struct platform_device *pdev)
14868668845STakashi Iwai {
14968668845STakashi Iwai 	struct snd_card *card = platform_get_drvdata(pdev);
15068668845STakashi Iwai 	struct snd_ad1848 *chip = card->private_data;
15168668845STakashi Iwai 
15268668845STakashi Iwai 	chip->resume(chip);
15368668845STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
15468668845STakashi Iwai 	return 0;
15568668845STakashi Iwai }
15668668845STakashi Iwai #endif
15768668845STakashi Iwai 
15868668845STakashi Iwai #define SND_AD1848_DRIVER	"snd_ad1848"
15968668845STakashi Iwai 
16068668845STakashi Iwai static struct platform_driver snd_ad1848_driver = {
16168668845STakashi Iwai 	.probe		= snd_ad1848_probe,
16268668845STakashi Iwai 	.remove		= __devexit_p(snd_ad1848_remove),
16368668845STakashi Iwai #ifdef CONFIG_PM
16468668845STakashi Iwai 	.suspend	= snd_ad1848_suspend,
16568668845STakashi Iwai 	.resume		= snd_ad1848_resume,
16668668845STakashi Iwai #endif
16768668845STakashi Iwai 	.driver		= {
16868668845STakashi Iwai 		.name	= SND_AD1848_DRIVER
16968668845STakashi Iwai 	},
17068668845STakashi Iwai };
17168668845STakashi Iwai 
172*f7a9275dSClemens Ladisch static void __init_or_module snd_ad1848_unregister_all(void)
173*f7a9275dSClemens Ladisch {
174*f7a9275dSClemens Ladisch 	int i;
175*f7a9275dSClemens Ladisch 
176*f7a9275dSClemens Ladisch 	for (i = 0; i < ARRAY_SIZE(devices); ++i)
177*f7a9275dSClemens Ladisch 		platform_device_unregister(devices[i]);
178*f7a9275dSClemens Ladisch 	platform_driver_unregister(&snd_ad1848_driver);
179*f7a9275dSClemens Ladisch }
180*f7a9275dSClemens Ladisch 
1811da177e4SLinus Torvalds static int __init alsa_card_ad1848_init(void)
1821da177e4SLinus Torvalds {
18368668845STakashi Iwai 	int i, cards, err;
1841da177e4SLinus Torvalds 
18568668845STakashi Iwai 	err = platform_driver_register(&snd_ad1848_driver);
18668668845STakashi Iwai 	if (err < 0)
18768668845STakashi Iwai 		return err;
18868668845STakashi Iwai 
18968668845STakashi Iwai 	cards = 0;
19068668845STakashi Iwai 	for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
19168668845STakashi Iwai 		struct platform_device *device;
19268668845STakashi Iwai 		device = platform_device_register_simple(SND_AD1848_DRIVER,
19368668845STakashi Iwai 							 i, NULL, 0);
19468668845STakashi Iwai 		if (IS_ERR(device)) {
19568668845STakashi Iwai 			err = PTR_ERR(device);
19668668845STakashi Iwai 			goto errout;
19768668845STakashi Iwai 		}
198*f7a9275dSClemens Ladisch 		devices[i] = device;
1991da177e4SLinus Torvalds 		cards++;
20068668845STakashi Iwai 	}
2011da177e4SLinus Torvalds 	if (!cards) {
2021da177e4SLinus Torvalds #ifdef MODULE
2031da177e4SLinus Torvalds 		printk(KERN_ERR "AD1848 soundcard not found or device busy\n");
2041da177e4SLinus Torvalds #endif
20568668845STakashi Iwai 		err = -ENODEV;
20668668845STakashi Iwai 		goto errout;
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 	return 0;
20968668845STakashi Iwai 
21068668845STakashi Iwai  errout:
211*f7a9275dSClemens Ladisch 	snd_ad1848_unregister_all();
21268668845STakashi Iwai 	return err;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds static void __exit alsa_card_ad1848_exit(void)
2161da177e4SLinus Torvalds {
217*f7a9275dSClemens Ladisch 	snd_ad1848_unregister_all();
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds module_init(alsa_card_ad1848_init)
2211da177e4SLinus Torvalds module_exit(alsa_card_ad1848_exit)
222