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