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