19ab4d072SStas Sergeev /* 29ab4d072SStas Sergeev * PC-Speaker driver for Linux 39ab4d072SStas Sergeev * 49ab4d072SStas Sergeev * Copyright (C) 1997-2001 David Woodhouse 59ab4d072SStas Sergeev * Copyright (C) 2001-2008 Stas Sergeev 69ab4d072SStas Sergeev */ 79ab4d072SStas Sergeev 89ab4d072SStas Sergeev #include <linux/init.h> 965a77217SPaul Gortmaker #include <linux/module.h> 109ab4d072SStas Sergeev #include <linux/platform_device.h> 119ab4d072SStas Sergeev #include <sound/core.h> 129ab4d072SStas Sergeev #include <sound/initval.h> 139ab4d072SStas Sergeev #include <sound/pcm.h> 149ab4d072SStas Sergeev #include <linux/input.h> 159ecaedaeSMariusz Kozlowski #include <linux/delay.h> 16976412fbSTakashi Iwai #include <linux/bitops.h> 179ab4d072SStas Sergeev #include "pcsp_input.h" 189ab4d072SStas Sergeev #include "pcsp.h" 199ab4d072SStas Sergeev 209ab4d072SStas Sergeev MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>"); 219ab4d072SStas Sergeev MODULE_DESCRIPTION("PC-Speaker driver"); 229ab4d072SStas Sergeev MODULE_LICENSE("GPL"); 239ab4d072SStas Sergeev MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}"); 249ab4d072SStas Sergeev MODULE_ALIAS("platform:pcspkr"); 259ab4d072SStas Sergeev 269ab4d072SStas Sergeev static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ 279ab4d072SStas Sergeev static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ 28a67ff6a5SRusty Russell static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */ 29a67ff6a5SRusty Russell static bool nopcm; /* Disable PCM capability of the driver */ 309ab4d072SStas Sergeev 319ab4d072SStas Sergeev module_param(index, int, 0444); 329ab4d072SStas Sergeev MODULE_PARM_DESC(index, "Index value for pcsp soundcard."); 339ab4d072SStas Sergeev module_param(id, charp, 0444); 349ab4d072SStas Sergeev MODULE_PARM_DESC(id, "ID string for pcsp soundcard."); 359ab4d072SStas Sergeev module_param(enable, bool, 0444); 3652337310SStas Sergeev MODULE_PARM_DESC(enable, "Enable PC-Speaker sound."); 37bcc2c6b7SStas Sergeev module_param(nopcm, bool, 0444); 38bcc2c6b7SStas Sergeev MODULE_PARM_DESC(nopcm, "Disable PC-Speaker PCM sound. Only beeps remain."); 399ab4d072SStas Sergeev 409ab4d072SStas Sergeev struct snd_pcsp pcsp_chip; 419ab4d072SStas Sergeev 42fbbb01a1SBill Pemberton static int snd_pcsp_create(struct snd_card *card) 439ab4d072SStas Sergeev { 449ab4d072SStas Sergeev static struct snd_device_ops ops = { }; 45447fbbdcSThomas Gleixner unsigned int resolution = hrtimer_resolution; 46447fbbdcSThomas Gleixner int err, div, min_div, order; 4775415df8STakashi Iwai 4875415df8STakashi Iwai if (!nopcm) { 49447fbbdcSThomas Gleixner if (resolution > PCSP_MAX_PERIOD_NS) { 509ab4d072SStas Sergeev printk(KERN_ERR "PCSP: Timer resolution is not sufficient " 51*c78c8818SThomas Gleixner "(%unS)\n", resolution); 529ab4d072SStas Sergeev printk(KERN_ERR "PCSP: Make sure you have HPET and ACPI " 539ab4d072SStas Sergeev "enabled.\n"); 54bcc2c6b7SStas Sergeev printk(KERN_ERR "PCSP: Turned into nopcm mode.\n"); 55bcc2c6b7SStas Sergeev nopcm = 1; 56bcc2c6b7SStas Sergeev } 579ab4d072SStas Sergeev } 589ab4d072SStas Sergeev 59447fbbdcSThomas Gleixner if (loops_per_jiffy >= PCSP_MIN_LPJ && resolution <= PCSP_MIN_PERIOD_NS) 609ab4d072SStas Sergeev min_div = MIN_DIV; 619ab4d072SStas Sergeev else 629ab4d072SStas Sergeev min_div = MAX_DIV; 639ab4d072SStas Sergeev #if PCSP_DEBUG 64*c78c8818SThomas Gleixner printk(KERN_DEBUG "PCSP: lpj=%li, min_div=%i, res=%u\n", 65447fbbdcSThomas Gleixner loops_per_jiffy, min_div, resolution); 669ab4d072SStas Sergeev #endif 679ab4d072SStas Sergeev 689ab4d072SStas Sergeev div = MAX_DIV / min_div; 699ab4d072SStas Sergeev order = fls(div) - 1; 709ab4d072SStas Sergeev 719ab4d072SStas Sergeev pcsp_chip.max_treble = min(order, PCSP_MAX_TREBLE); 729ab4d072SStas Sergeev pcsp_chip.treble = min(pcsp_chip.max_treble, PCSP_DEFAULT_TREBLE); 739ab4d072SStas Sergeev pcsp_chip.playback_ptr = 0; 749ab4d072SStas Sergeev pcsp_chip.period_ptr = 0; 759ab4d072SStas Sergeev atomic_set(&pcsp_chip.timer_active, 0); 769ab4d072SStas Sergeev pcsp_chip.enable = 1; 779ab4d072SStas Sergeev pcsp_chip.pcspkr = 1; 789ab4d072SStas Sergeev 799ab4d072SStas Sergeev spin_lock_init(&pcsp_chip.substream_lock); 809ab4d072SStas Sergeev 819ab4d072SStas Sergeev pcsp_chip.card = card; 829ab4d072SStas Sergeev pcsp_chip.port = 0x61; 839ab4d072SStas Sergeev pcsp_chip.irq = -1; 849ab4d072SStas Sergeev pcsp_chip.dma = -1; 859ab4d072SStas Sergeev 869ab4d072SStas Sergeev /* Register device */ 879ab4d072SStas Sergeev err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, &pcsp_chip, &ops); 889ab4d072SStas Sergeev if (err < 0) 899ab4d072SStas Sergeev return err; 909ab4d072SStas Sergeev 919ab4d072SStas Sergeev return 0; 929ab4d072SStas Sergeev } 939ab4d072SStas Sergeev 94fbbb01a1SBill Pemberton static int snd_card_pcsp_probe(int devnum, struct device *dev) 959ab4d072SStas Sergeev { 969ab4d072SStas Sergeev struct snd_card *card; 979ab4d072SStas Sergeev int err; 989ab4d072SStas Sergeev 999ab4d072SStas Sergeev if (devnum != 0) 1009ab4d072SStas Sergeev return -EINVAL; 1019ab4d072SStas Sergeev 1029ab4d072SStas Sergeev hrtimer_init(&pcsp_chip.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1039ab4d072SStas Sergeev pcsp_chip.timer.function = pcsp_do_timer; 1049ab4d072SStas Sergeev 1055872f3f6STakashi Iwai err = snd_card_new(dev, index, id, THIS_MODULE, 0, &card); 106bd7dd77cSTakashi Iwai if (err < 0) 107bd7dd77cSTakashi Iwai return err; 1089ab4d072SStas Sergeev 1099ab4d072SStas Sergeev err = snd_pcsp_create(card); 1109ab4d072SStas Sergeev if (err < 0) { 1119ab4d072SStas Sergeev snd_card_free(card); 1129ab4d072SStas Sergeev return err; 1139ab4d072SStas Sergeev } 114bcc2c6b7SStas Sergeev if (!nopcm) { 1159ab4d072SStas Sergeev err = snd_pcsp_new_pcm(&pcsp_chip); 1169ab4d072SStas Sergeev if (err < 0) { 1179ab4d072SStas Sergeev snd_card_free(card); 1189ab4d072SStas Sergeev return err; 1199ab4d072SStas Sergeev } 120bcc2c6b7SStas Sergeev } 121bcc2c6b7SStas Sergeev err = snd_pcsp_new_mixer(&pcsp_chip, nopcm); 1229ab4d072SStas Sergeev if (err < 0) { 1239ab4d072SStas Sergeev snd_card_free(card); 1249ab4d072SStas Sergeev return err; 1259ab4d072SStas Sergeev } 1269ab4d072SStas Sergeev 1279ab4d072SStas Sergeev strcpy(card->driver, "PC-Speaker"); 1289ab4d072SStas Sergeev strcpy(card->shortname, "pcsp"); 1299ab4d072SStas Sergeev sprintf(card->longname, "Internal PC-Speaker at port 0x%x", 1309ab4d072SStas Sergeev pcsp_chip.port); 1319ab4d072SStas Sergeev 1329ab4d072SStas Sergeev err = snd_card_register(card); 1339ab4d072SStas Sergeev if (err < 0) { 1349ab4d072SStas Sergeev snd_card_free(card); 1359ab4d072SStas Sergeev return err; 1369ab4d072SStas Sergeev } 1379ab4d072SStas Sergeev 1389ab4d072SStas Sergeev return 0; 1399ab4d072SStas Sergeev } 1409ab4d072SStas Sergeev 141fbbb01a1SBill Pemberton static int alsa_card_pcsp_init(struct device *dev) 1429ab4d072SStas Sergeev { 14352337310SStas Sergeev int err; 14452337310SStas Sergeev 14552337310SStas Sergeev err = snd_card_pcsp_probe(0, dev); 14652337310SStas Sergeev if (err) { 14752337310SStas Sergeev printk(KERN_ERR "PC-Speaker initialization failed.\n"); 14852337310SStas Sergeev return err; 14952337310SStas Sergeev } 1509ab4d072SStas Sergeev 1519ab4d072SStas Sergeev #ifdef CONFIG_DEBUG_PAGEALLOC 1529ab4d072SStas Sergeev /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */ 153efd89d9dSStas Sergeev printk(KERN_WARNING "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, " 154efd89d9dSStas Sergeev "which may make the sound noisy.\n"); 1559ab4d072SStas Sergeev #endif 1569ab4d072SStas Sergeev 1579ab4d072SStas Sergeev return 0; 1589ab4d072SStas Sergeev } 1599ab4d072SStas Sergeev 160fbbb01a1SBill Pemberton static void alsa_card_pcsp_exit(struct snd_pcsp *chip) 1619ab4d072SStas Sergeev { 1629ab4d072SStas Sergeev snd_card_free(chip->card); 1639ab4d072SStas Sergeev } 1649ab4d072SStas Sergeev 165fbbb01a1SBill Pemberton static int pcsp_probe(struct platform_device *dev) 1669ab4d072SStas Sergeev { 1679ab4d072SStas Sergeev int err; 16852337310SStas Sergeev 1699ab4d072SStas Sergeev err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev); 1709ab4d072SStas Sergeev if (err < 0) 1719ab4d072SStas Sergeev return err; 1729ab4d072SStas Sergeev 1739ab4d072SStas Sergeev err = alsa_card_pcsp_init(&dev->dev); 1749ab4d072SStas Sergeev if (err < 0) { 1759ab4d072SStas Sergeev pcspkr_input_remove(pcsp_chip.input_dev); 1769ab4d072SStas Sergeev return err; 1779ab4d072SStas Sergeev } 1789ab4d072SStas Sergeev 1799ab4d072SStas Sergeev platform_set_drvdata(dev, &pcsp_chip); 1809ab4d072SStas Sergeev return 0; 1819ab4d072SStas Sergeev } 1829ab4d072SStas Sergeev 183fbbb01a1SBill Pemberton static int pcsp_remove(struct platform_device *dev) 1849ab4d072SStas Sergeev { 1859ab4d072SStas Sergeev struct snd_pcsp *chip = platform_get_drvdata(dev); 1869ab4d072SStas Sergeev pcspkr_input_remove(chip->input_dev); 1876408eac2STakashi Iwai alsa_card_pcsp_exit(chip); 1889ab4d072SStas Sergeev return 0; 1899ab4d072SStas Sergeev } 1909ab4d072SStas Sergeev 1919ab4d072SStas Sergeev static void pcsp_stop_beep(struct snd_pcsp *chip) 1929ab4d072SStas Sergeev { 19396c7d478STakashi Iwai pcsp_sync_stop(chip); 1949ab4d072SStas Sergeev pcspkr_stop_sound(); 1959ab4d072SStas Sergeev } 1969ab4d072SStas Sergeev 197d34e4e00STakashi Iwai #ifdef CONFIG_PM_SLEEP 198284e7ca7STakashi Iwai static int pcsp_suspend(struct device *dev) 1999ab4d072SStas Sergeev { 200284e7ca7STakashi Iwai struct snd_pcsp *chip = dev_get_drvdata(dev); 2019ab4d072SStas Sergeev pcsp_stop_beep(chip); 2029ab4d072SStas Sergeev snd_pcm_suspend_all(chip->pcm); 2039ab4d072SStas Sergeev return 0; 2049ab4d072SStas Sergeev } 205284e7ca7STakashi Iwai 206284e7ca7STakashi Iwai static SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL); 207284e7ca7STakashi Iwai #define PCSP_PM_OPS &pcsp_pm 208983e0972SJohann Felix Soden #else 209284e7ca7STakashi Iwai #define PCSP_PM_OPS NULL 210d34e4e00STakashi Iwai #endif /* CONFIG_PM_SLEEP */ 2119ab4d072SStas Sergeev 2129ab4d072SStas Sergeev static void pcsp_shutdown(struct platform_device *dev) 2139ab4d072SStas Sergeev { 2149ab4d072SStas Sergeev struct snd_pcsp *chip = platform_get_drvdata(dev); 2159ab4d072SStas Sergeev pcsp_stop_beep(chip); 2169ab4d072SStas Sergeev } 2179ab4d072SStas Sergeev 2189ab4d072SStas Sergeev static struct platform_driver pcsp_platform_driver = { 2199ab4d072SStas Sergeev .driver = { 2209ab4d072SStas Sergeev .name = "pcspkr", 221284e7ca7STakashi Iwai .pm = PCSP_PM_OPS, 2229ab4d072SStas Sergeev }, 2239ab4d072SStas Sergeev .probe = pcsp_probe, 224fbbb01a1SBill Pemberton .remove = pcsp_remove, 2259ab4d072SStas Sergeev .shutdown = pcsp_shutdown, 2269ab4d072SStas Sergeev }; 2279ab4d072SStas Sergeev 2289ab4d072SStas Sergeev static int __init pcsp_init(void) 2299ab4d072SStas Sergeev { 23052337310SStas Sergeev if (!enable) 23152337310SStas Sergeev return -ENODEV; 2329ab4d072SStas Sergeev return platform_driver_register(&pcsp_platform_driver); 2339ab4d072SStas Sergeev } 2349ab4d072SStas Sergeev 2359ab4d072SStas Sergeev static void __exit pcsp_exit(void) 2369ab4d072SStas Sergeev { 2379ab4d072SStas Sergeev platform_driver_unregister(&pcsp_platform_driver); 2389ab4d072SStas Sergeev } 2399ab4d072SStas Sergeev 2409ab4d072SStas Sergeev module_init(pcsp_init); 2419ab4d072SStas Sergeev module_exit(pcsp_exit); 242