| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 1 | /* | 
 | 2 |  * PC-Speaker driver for Linux | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 1997-2001  David Woodhouse | 
 | 5 |  * Copyright (C) 2001-2008  Stas Sergeev | 
 | 6 |  */ | 
 | 7 |  | 
 | 8 | #include <linux/init.h> | 
 | 9 | #include <linux/moduleparam.h> | 
 | 10 | #include <linux/platform_device.h> | 
 | 11 | #include <sound/core.h> | 
 | 12 | #include <sound/initval.h> | 
 | 13 | #include <sound/pcm.h> | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 14 | #include <linux/input.h> | 
| Mariusz Kozlowski | 9ecaeda | 2008-03-18 09:03:03 +0100 | [diff] [blame] | 15 | #include <linux/delay.h> | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 16 | #include <asm/bitops.h> | 
 | 17 | #include "pcsp_input.h" | 
 | 18 | #include "pcsp.h" | 
 | 19 |  | 
 | 20 | MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>"); | 
 | 21 | MODULE_DESCRIPTION("PC-Speaker driver"); | 
 | 22 | MODULE_LICENSE("GPL"); | 
 | 23 | MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}"); | 
 | 24 | MODULE_ALIAS("platform:pcspkr"); | 
 | 25 |  | 
 | 26 | static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */ | 
 | 27 | static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */ | 
 | 28 | static int enable = SNDRV_DEFAULT_ENABLE1;	/* Enable this card */ | 
 | 29 |  | 
 | 30 | module_param(index, int, 0444); | 
 | 31 | MODULE_PARM_DESC(index, "Index value for pcsp soundcard."); | 
 | 32 | module_param(id, charp, 0444); | 
 | 33 | MODULE_PARM_DESC(id, "ID string for pcsp soundcard."); | 
 | 34 | module_param(enable, bool, 0444); | 
| Stas Sergeev | 5233731 | 2008-03-06 11:01:16 +0100 | [diff] [blame] | 35 | MODULE_PARM_DESC(enable, "Enable PC-Speaker sound."); | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 36 |  | 
 | 37 | struct snd_pcsp pcsp_chip; | 
 | 38 |  | 
 | 39 | static int __devinit snd_pcsp_create(struct snd_card *card) | 
 | 40 | { | 
 | 41 | 	static struct snd_device_ops ops = { }; | 
 | 42 | 	struct timespec tp; | 
 | 43 | 	int err; | 
 | 44 | 	int div, min_div, order; | 
 | 45 |  | 
 | 46 | 	hrtimer_get_res(CLOCK_MONOTONIC, &tp); | 
 | 47 | 	if (tp.tv_sec || tp.tv_nsec > PCSP_MAX_PERIOD_NS) { | 
 | 48 | 		printk(KERN_ERR "PCSP: Timer resolution is not sufficient " | 
 | 49 | 		       "(%linS)\n", tp.tv_nsec); | 
 | 50 | 		printk(KERN_ERR "PCSP: Make sure you have HPET and ACPI " | 
 | 51 | 		       "enabled.\n"); | 
 | 52 | 		return -EIO; | 
 | 53 | 	} | 
 | 54 |  | 
 | 55 | 	if (loops_per_jiffy >= PCSP_MIN_LPJ && tp.tv_nsec <= PCSP_MIN_PERIOD_NS) | 
 | 56 | 		min_div = MIN_DIV; | 
 | 57 | 	else | 
 | 58 | 		min_div = MAX_DIV; | 
 | 59 | #if PCSP_DEBUG | 
| Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 60 | 	printk(KERN_DEBUG "PCSP: lpj=%li, min_div=%i, res=%li\n", | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 61 | 	       loops_per_jiffy, min_div, tp.tv_nsec); | 
 | 62 | #endif | 
 | 63 |  | 
 | 64 | 	div = MAX_DIV / min_div; | 
 | 65 | 	order = fls(div) - 1; | 
 | 66 |  | 
 | 67 | 	pcsp_chip.max_treble = min(order, PCSP_MAX_TREBLE); | 
 | 68 | 	pcsp_chip.treble = min(pcsp_chip.max_treble, PCSP_DEFAULT_TREBLE); | 
 | 69 | 	pcsp_chip.playback_ptr = 0; | 
 | 70 | 	pcsp_chip.period_ptr = 0; | 
 | 71 | 	atomic_set(&pcsp_chip.timer_active, 0); | 
 | 72 | 	pcsp_chip.enable = 1; | 
 | 73 | 	pcsp_chip.pcspkr = 1; | 
 | 74 |  | 
 | 75 | 	spin_lock_init(&pcsp_chip.substream_lock); | 
 | 76 |  | 
 | 77 | 	pcsp_chip.card = card; | 
 | 78 | 	pcsp_chip.port = 0x61; | 
 | 79 | 	pcsp_chip.irq = -1; | 
 | 80 | 	pcsp_chip.dma = -1; | 
 | 81 |  | 
 | 82 | 	/* Register device */ | 
 | 83 | 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, &pcsp_chip, &ops); | 
 | 84 | 	if (err < 0) | 
 | 85 | 		return err; | 
 | 86 |  | 
 | 87 | 	return 0; | 
 | 88 | } | 
 | 89 |  | 
 | 90 | static int __devinit snd_card_pcsp_probe(int devnum, struct device *dev) | 
 | 91 | { | 
 | 92 | 	struct snd_card *card; | 
 | 93 | 	int err; | 
 | 94 |  | 
 | 95 | 	if (devnum != 0) | 
 | 96 | 		return -EINVAL; | 
 | 97 |  | 
 | 98 | 	hrtimer_init(&pcsp_chip.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 99 | 	pcsp_chip.timer.function = pcsp_do_timer; | 
 | 100 |  | 
| Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 101 | 	err = snd_card_create(index, id, THIS_MODULE, 0, &card); | 
 | 102 | 	if (err < 0) | 
 | 103 | 		return err; | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 104 |  | 
 | 105 | 	err = snd_pcsp_create(card); | 
 | 106 | 	if (err < 0) { | 
 | 107 | 		snd_card_free(card); | 
 | 108 | 		return err; | 
 | 109 | 	} | 
 | 110 | 	err = snd_pcsp_new_pcm(&pcsp_chip); | 
 | 111 | 	if (err < 0) { | 
 | 112 | 		snd_card_free(card); | 
 | 113 | 		return err; | 
 | 114 | 	} | 
 | 115 | 	err = snd_pcsp_new_mixer(&pcsp_chip); | 
 | 116 | 	if (err < 0) { | 
 | 117 | 		snd_card_free(card); | 
 | 118 | 		return err; | 
 | 119 | 	} | 
 | 120 |  | 
 | 121 | 	snd_card_set_dev(pcsp_chip.card, dev); | 
 | 122 |  | 
 | 123 | 	strcpy(card->driver, "PC-Speaker"); | 
 | 124 | 	strcpy(card->shortname, "pcsp"); | 
 | 125 | 	sprintf(card->longname, "Internal PC-Speaker at port 0x%x", | 
 | 126 | 		pcsp_chip.port); | 
 | 127 |  | 
 | 128 | 	err = snd_card_register(card); | 
 | 129 | 	if (err < 0) { | 
 | 130 | 		snd_card_free(card); | 
 | 131 | 		return err; | 
 | 132 | 	} | 
 | 133 |  | 
 | 134 | 	return 0; | 
 | 135 | } | 
 | 136 |  | 
 | 137 | static int __devinit alsa_card_pcsp_init(struct device *dev) | 
 | 138 | { | 
| Stas Sergeev | 5233731 | 2008-03-06 11:01:16 +0100 | [diff] [blame] | 139 | 	int err; | 
 | 140 |  | 
 | 141 | 	err = snd_card_pcsp_probe(0, dev); | 
 | 142 | 	if (err) { | 
 | 143 | 		printk(KERN_ERR "PC-Speaker initialization failed.\n"); | 
 | 144 | 		return err; | 
 | 145 | 	} | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 146 |  | 
 | 147 | #ifdef CONFIG_DEBUG_PAGEALLOC | 
 | 148 | 	/* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */ | 
| Stas Sergeev | efd89d9 | 2008-04-23 17:16:38 +0200 | [diff] [blame] | 149 | 	printk(KERN_WARNING "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, " | 
 | 150 | 	       "which may make the sound noisy.\n"); | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 151 | #endif | 
 | 152 |  | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 153 | 	return 0; | 
 | 154 | } | 
 | 155 |  | 
 | 156 | static void __devexit alsa_card_pcsp_exit(struct snd_pcsp *chip) | 
 | 157 | { | 
 | 158 | 	snd_card_free(chip->card); | 
 | 159 | } | 
 | 160 |  | 
 | 161 | static int __devinit pcsp_probe(struct platform_device *dev) | 
 | 162 | { | 
 | 163 | 	int err; | 
| Stas Sergeev | 5233731 | 2008-03-06 11:01:16 +0100 | [diff] [blame] | 164 |  | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 165 | 	err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev); | 
 | 166 | 	if (err < 0) | 
 | 167 | 		return err; | 
 | 168 |  | 
 | 169 | 	err = alsa_card_pcsp_init(&dev->dev); | 
 | 170 | 	if (err < 0) { | 
 | 171 | 		pcspkr_input_remove(pcsp_chip.input_dev); | 
 | 172 | 		return err; | 
 | 173 | 	} | 
 | 174 |  | 
 | 175 | 	platform_set_drvdata(dev, &pcsp_chip); | 
 | 176 | 	return 0; | 
 | 177 | } | 
 | 178 |  | 
 | 179 | static int __devexit pcsp_remove(struct platform_device *dev) | 
 | 180 | { | 
 | 181 | 	struct snd_pcsp *chip = platform_get_drvdata(dev); | 
 | 182 | 	alsa_card_pcsp_exit(chip); | 
 | 183 | 	pcspkr_input_remove(chip->input_dev); | 
 | 184 | 	platform_set_drvdata(dev, NULL); | 
 | 185 | 	return 0; | 
 | 186 | } | 
 | 187 |  | 
 | 188 | static void pcsp_stop_beep(struct snd_pcsp *chip) | 
 | 189 | { | 
| Takashi Iwai | 96c7d47 | 2008-08-11 10:18:39 +0200 | [diff] [blame] | 190 | 	pcsp_sync_stop(chip); | 
 | 191 | 	pcspkr_stop_sound(); | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 192 | } | 
 | 193 |  | 
| Johann Felix Soden | 983e097 | 2008-05-02 09:54:31 +0200 | [diff] [blame] | 194 | #ifdef CONFIG_PM | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 195 | static int pcsp_suspend(struct platform_device *dev, pm_message_t state) | 
 | 196 | { | 
 | 197 | 	struct snd_pcsp *chip = platform_get_drvdata(dev); | 
 | 198 | 	pcsp_stop_beep(chip); | 
 | 199 | 	snd_pcm_suspend_all(chip->pcm); | 
 | 200 | 	return 0; | 
 | 201 | } | 
| Johann Felix Soden | 983e097 | 2008-05-02 09:54:31 +0200 | [diff] [blame] | 202 | #else | 
 | 203 | #define pcsp_suspend NULL | 
 | 204 | #endif	/* CONFIG_PM */ | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 205 |  | 
 | 206 | static void pcsp_shutdown(struct platform_device *dev) | 
 | 207 | { | 
 | 208 | 	struct snd_pcsp *chip = platform_get_drvdata(dev); | 
 | 209 | 	pcsp_stop_beep(chip); | 
 | 210 | } | 
 | 211 |  | 
 | 212 | static struct platform_driver pcsp_platform_driver = { | 
 | 213 | 	.driver		= { | 
 | 214 | 		.name	= "pcspkr", | 
 | 215 | 		.owner	= THIS_MODULE, | 
 | 216 | 	}, | 
 | 217 | 	.probe		= pcsp_probe, | 
 | 218 | 	.remove		= __devexit_p(pcsp_remove), | 
 | 219 | 	.suspend	= pcsp_suspend, | 
 | 220 | 	.shutdown	= pcsp_shutdown, | 
 | 221 | }; | 
 | 222 |  | 
 | 223 | static int __init pcsp_init(void) | 
 | 224 | { | 
| Stas Sergeev | 5233731 | 2008-03-06 11:01:16 +0100 | [diff] [blame] | 225 | 	if (!enable) | 
 | 226 | 		return -ENODEV; | 
| Stas Sergeev | 9ab4d07 | 2008-03-03 10:53:54 +0100 | [diff] [blame] | 227 | 	return platform_driver_register(&pcsp_platform_driver); | 
 | 228 | } | 
 | 229 |  | 
 | 230 | static void __exit pcsp_exit(void) | 
 | 231 | { | 
 | 232 | 	platform_driver_unregister(&pcsp_platform_driver); | 
 | 233 | } | 
 | 234 |  | 
 | 235 | module_init(pcsp_init); | 
 | 236 | module_exit(pcsp_exit); |