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