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> 14 #include <linux/input.h> 15 #include <asm/bitops.h> 16 #include "pcsp_input.h" 17 #include "pcsp.h" 18 19 MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>"); 20 MODULE_DESCRIPTION("PC-Speaker driver"); 21 MODULE_LICENSE("GPL"); 22 MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}"); 23 MODULE_ALIAS("platform:pcspkr"); 24 25 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ 26 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ 27 static int enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */ 28 29 module_param(index, int, 0444); 30 MODULE_PARM_DESC(index, "Index value for pcsp soundcard."); 31 module_param(id, charp, 0444); 32 MODULE_PARM_DESC(id, "ID string for pcsp soundcard."); 33 module_param(enable, bool, 0444); 34 MODULE_PARM_DESC(enable, "Enable PC-Speaker sound."); 35 36 struct snd_pcsp pcsp_chip; 37 38 static int __devinit snd_pcsp_create(struct snd_card *card) 39 { 40 static struct snd_device_ops ops = { }; 41 struct timespec tp; 42 int err; 43 int div, min_div, order; 44 45 hrtimer_get_res(CLOCK_MONOTONIC, &tp); 46 if (tp.tv_sec || tp.tv_nsec > PCSP_MAX_PERIOD_NS) { 47 printk(KERN_ERR "PCSP: Timer resolution is not sufficient " 48 "(%linS)\n", tp.tv_nsec); 49 printk(KERN_ERR "PCSP: Make sure you have HPET and ACPI " 50 "enabled.\n"); 51 return -EIO; 52 } 53 54 if (loops_per_jiffy >= PCSP_MIN_LPJ && tp.tv_nsec <= PCSP_MIN_PERIOD_NS) 55 min_div = MIN_DIV; 56 else 57 min_div = MAX_DIV; 58 #if PCSP_DEBUG 59 printk("PCSP: lpj=%li, min_div=%i, res=%li\n", 60 loops_per_jiffy, min_div, tp.tv_nsec); 61 #endif 62 63 div = MAX_DIV / min_div; 64 order = fls(div) - 1; 65 66 pcsp_chip.max_treble = min(order, PCSP_MAX_TREBLE); 67 pcsp_chip.treble = min(pcsp_chip.max_treble, PCSP_DEFAULT_TREBLE); 68 pcsp_chip.playback_ptr = 0; 69 pcsp_chip.period_ptr = 0; 70 atomic_set(&pcsp_chip.timer_active, 0); 71 pcsp_chip.enable = 1; 72 pcsp_chip.pcspkr = 1; 73 74 spin_lock_init(&pcsp_chip.substream_lock); 75 76 pcsp_chip.card = card; 77 pcsp_chip.port = 0x61; 78 pcsp_chip.irq = -1; 79 pcsp_chip.dma = -1; 80 81 /* Register device */ 82 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, &pcsp_chip, &ops); 83 if (err < 0) 84 return err; 85 86 return 0; 87 } 88 89 static int __devinit snd_card_pcsp_probe(int devnum, struct device *dev) 90 { 91 struct snd_card *card; 92 int err; 93 94 if (devnum != 0) 95 return -EINVAL; 96 97 hrtimer_init(&pcsp_chip.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 98 pcsp_chip.timer.cb_mode = HRTIMER_CB_IRQSAFE; 99 pcsp_chip.timer.function = pcsp_do_timer; 100 101 card = snd_card_new(index, id, THIS_MODULE, 0); 102 if (!card) 103 return -ENOMEM; 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 { 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 } 146 147 #ifdef CONFIG_DEBUG_PAGEALLOC 148 /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */ 149 printk(KERN_WARNING 150 "PCSP: Warning, CONFIG_DEBUG_PAGEALLOC is enabled!\n" 151 "You have to disable it if you want to use the PC-Speaker " 152 "driver.\n" 153 "Unless it is disabled, enjoy the horrible, distorted " 154 "and crackling noise.\n"); 155 #endif 156 157 return 0; 158 } 159 160 static void __devexit alsa_card_pcsp_exit(struct snd_pcsp *chip) 161 { 162 snd_card_free(chip->card); 163 } 164 165 static int __devinit pcsp_probe(struct platform_device *dev) 166 { 167 int err; 168 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 183 static int __devexit pcsp_remove(struct platform_device *dev) 184 { 185 struct snd_pcsp *chip = platform_get_drvdata(dev); 186 alsa_card_pcsp_exit(chip); 187 pcspkr_input_remove(chip->input_dev); 188 platform_set_drvdata(dev, NULL); 189 return 0; 190 } 191 192 static void pcsp_stop_beep(struct snd_pcsp *chip) 193 { 194 unsigned long flags; 195 spin_lock_irqsave(&chip->substream_lock, flags); 196 if (!chip->playback_substream) 197 pcspkr_stop_sound(); 198 spin_unlock_irqrestore(&chip->substream_lock, flags); 199 } 200 201 static int pcsp_suspend(struct platform_device *dev, pm_message_t state) 202 { 203 struct snd_pcsp *chip = platform_get_drvdata(dev); 204 pcsp_stop_beep(chip); 205 snd_pcm_suspend_all(chip->pcm); 206 return 0; 207 } 208 209 static void pcsp_shutdown(struct platform_device *dev) 210 { 211 struct snd_pcsp *chip = platform_get_drvdata(dev); 212 pcsp_stop_beep(chip); 213 } 214 215 static struct platform_driver pcsp_platform_driver = { 216 .driver = { 217 .name = "pcspkr", 218 .owner = THIS_MODULE, 219 }, 220 .probe = pcsp_probe, 221 .remove = __devexit_p(pcsp_remove), 222 .suspend = pcsp_suspend, 223 .shutdown = pcsp_shutdown, 224 }; 225 226 static int __init pcsp_init(void) 227 { 228 if (!enable) 229 return -ENODEV; 230 return platform_driver_register(&pcsp_platform_driver); 231 } 232 233 static void __exit pcsp_exit(void) 234 { 235 platform_driver_unregister(&pcsp_platform_driver); 236 } 237 238 module_init(pcsp_init); 239 module_exit(pcsp_exit); 240