]>
Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
9ab4d072 SS |
2 | /* |
3 | * PC-Speaker driver for Linux | |
4 | * | |
5 | * Copyright (C) 1997-2001 David Woodhouse | |
6 | * Copyright (C) 2001-2008 Stas Sergeev | |
7 | */ | |
8 | ||
9 | #include <linux/init.h> | |
65a77217 | 10 | #include <linux/module.h> |
9ab4d072 SS |
11 | #include <linux/platform_device.h> |
12 | #include <sound/core.h> | |
13 | #include <sound/initval.h> | |
14 | #include <sound/pcm.h> | |
9ab4d072 | 15 | #include <linux/input.h> |
9ecaedae | 16 | #include <linux/delay.h> |
976412fb | 17 | #include <linux/bitops.h> |
505f6d22 | 18 | #include <linux/mm.h> |
9ab4d072 SS |
19 | #include "pcsp_input.h" |
20 | #include "pcsp.h" | |
21 | ||
22 | MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>"); | |
23 | MODULE_DESCRIPTION("PC-Speaker driver"); | |
24 | MODULE_LICENSE("GPL"); | |
9ab4d072 SS |
25 | MODULE_ALIAS("platform:pcspkr"); |
26 | ||
27 | static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ | |
28 | static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ | |
a67ff6a5 RR |
29 | static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */ |
30 | static bool nopcm; /* Disable PCM capability of the driver */ | |
9ab4d072 SS |
31 | |
32 | module_param(index, int, 0444); | |
33 | MODULE_PARM_DESC(index, "Index value for pcsp soundcard."); | |
34 | module_param(id, charp, 0444); | |
35 | MODULE_PARM_DESC(id, "ID string for pcsp soundcard."); | |
36 | module_param(enable, bool, 0444); | |
52337310 | 37 | MODULE_PARM_DESC(enable, "Enable PC-Speaker sound."); |
bcc2c6b7 SS |
38 | module_param(nopcm, bool, 0444); |
39 | MODULE_PARM_DESC(nopcm, "Disable PC-Speaker PCM sound. Only beeps remain."); | |
9ab4d072 SS |
40 | |
41 | struct snd_pcsp pcsp_chip; | |
42 | ||
fbbb01a1 | 43 | static int snd_pcsp_create(struct snd_card *card) |
9ab4d072 | 44 | { |
34273b23 | 45 | static const struct snd_device_ops ops = { }; |
447fbbdc TG |
46 | unsigned int resolution = hrtimer_resolution; |
47 | int err, div, min_div, order; | |
75415df8 | 48 | |
bcc2c6b7 | 49 | if (!nopcm) { |
447fbbdc | 50 | if (resolution > PCSP_MAX_PERIOD_NS) { |
bcc2c6b7 | 51 | printk(KERN_ERR "PCSP: Timer resolution is not sufficient " |
c78c8818 | 52 | "(%unS)\n", resolution); |
bcc2c6b7 SS |
53 | printk(KERN_ERR "PCSP: Make sure you have HPET and ACPI " |
54 | "enabled.\n"); | |
55 | printk(KERN_ERR "PCSP: Turned into nopcm mode.\n"); | |
56 | nopcm = 1; | |
57 | } | |
9ab4d072 SS |
58 | } |
59 | ||
447fbbdc | 60 | if (loops_per_jiffy >= PCSP_MIN_LPJ && resolution <= PCSP_MIN_PERIOD_NS) |
9ab4d072 SS |
61 | min_div = MIN_DIV; |
62 | else | |
63 | min_div = MAX_DIV; | |
64 | #if PCSP_DEBUG | |
c78c8818 | 65 | printk(KERN_DEBUG "PCSP: lpj=%li, min_div=%i, res=%u\n", |
447fbbdc | 66 | loops_per_jiffy, min_div, resolution); |
9ab4d072 SS |
67 | #endif |
68 | ||
69 | div = MAX_DIV / min_div; | |
70 | order = fls(div) - 1; | |
71 | ||
72 | pcsp_chip.max_treble = min(order, PCSP_MAX_TREBLE); | |
73 | pcsp_chip.treble = min(pcsp_chip.max_treble, PCSP_DEFAULT_TREBLE); | |
74 | pcsp_chip.playback_ptr = 0; | |
75 | pcsp_chip.period_ptr = 0; | |
76 | atomic_set(&pcsp_chip.timer_active, 0); | |
77 | pcsp_chip.enable = 1; | |
78 | pcsp_chip.pcspkr = 1; | |
79 | ||
80 | spin_lock_init(&pcsp_chip.substream_lock); | |
81 | ||
82 | pcsp_chip.card = card; | |
83 | pcsp_chip.port = 0x61; | |
84 | pcsp_chip.irq = -1; | |
85 | pcsp_chip.dma = -1; | |
86 | ||
87 | /* Register device */ | |
88 | err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, &pcsp_chip, &ops); | |
89 | if (err < 0) | |
90 | return err; | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
fbbb01a1 | 95 | static int snd_card_pcsp_probe(int devnum, struct device *dev) |
9ab4d072 SS |
96 | { |
97 | struct snd_card *card; | |
98 | int err; | |
99 | ||
100 | if (devnum != 0) | |
101 | return -EINVAL; | |
102 | ||
103 | hrtimer_init(&pcsp_chip.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
9ab4d072 SS |
104 | pcsp_chip.timer.function = pcsp_do_timer; |
105 | ||
5872f3f6 | 106 | err = snd_card_new(dev, index, id, THIS_MODULE, 0, &card); |
bd7dd77c TI |
107 | if (err < 0) |
108 | return err; | |
9ab4d072 SS |
109 | |
110 | err = snd_pcsp_create(card); | |
2cded8c8 ME |
111 | if (err < 0) |
112 | goto free_card; | |
113 | ||
bcc2c6b7 SS |
114 | if (!nopcm) { |
115 | err = snd_pcsp_new_pcm(&pcsp_chip); | |
2cded8c8 ME |
116 | if (err < 0) |
117 | goto free_card; | |
9ab4d072 | 118 | } |
bcc2c6b7 | 119 | err = snd_pcsp_new_mixer(&pcsp_chip, nopcm); |
2cded8c8 ME |
120 | if (err < 0) |
121 | goto free_card; | |
9ab4d072 | 122 | |
9ab4d072 SS |
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); | |
2cded8c8 ME |
129 | if (err < 0) |
130 | goto free_card; | |
9ab4d072 SS |
131 | |
132 | return 0; | |
2cded8c8 ME |
133 | |
134 | free_card: | |
135 | snd_card_free(card); | |
136 | return err; | |
9ab4d072 SS |
137 | } |
138 | ||
fbbb01a1 | 139 | static int alsa_card_pcsp_init(struct device *dev) |
9ab4d072 | 140 | { |
52337310 SS |
141 | int err; |
142 | ||
143 | err = snd_card_pcsp_probe(0, dev); | |
144 | if (err) { | |
145 | printk(KERN_ERR "PC-Speaker initialization failed.\n"); | |
146 | return err; | |
147 | } | |
9ab4d072 | 148 | |
9ab4d072 | 149 | /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */ |
505f6d22 JK |
150 | if (debug_pagealloc_enabled()) { |
151 | printk(KERN_WARNING "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, " | |
152 | "which may make the sound noisy.\n"); | |
153 | } | |
9ab4d072 | 154 | |
9ab4d072 SS |
155 | return 0; |
156 | } | |
157 | ||
fbbb01a1 | 158 | static void alsa_card_pcsp_exit(struct snd_pcsp *chip) |
9ab4d072 SS |
159 | { |
160 | snd_card_free(chip->card); | |
161 | } | |
162 | ||
fbbb01a1 | 163 | static int pcsp_probe(struct platform_device *dev) |
9ab4d072 SS |
164 | { |
165 | int err; | |
52337310 | 166 | |
9ab4d072 SS |
167 | err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev); |
168 | if (err < 0) | |
169 | return err; | |
170 | ||
171 | err = alsa_card_pcsp_init(&dev->dev); | |
172 | if (err < 0) { | |
173 | pcspkr_input_remove(pcsp_chip.input_dev); | |
174 | return err; | |
175 | } | |
176 | ||
177 | platform_set_drvdata(dev, &pcsp_chip); | |
178 | return 0; | |
179 | } | |
180 | ||
fbbb01a1 | 181 | static int pcsp_remove(struct platform_device *dev) |
9ab4d072 SS |
182 | { |
183 | struct snd_pcsp *chip = platform_get_drvdata(dev); | |
9ab4d072 | 184 | pcspkr_input_remove(chip->input_dev); |
6408eac2 | 185 | alsa_card_pcsp_exit(chip); |
9ab4d072 SS |
186 | return 0; |
187 | } | |
188 | ||
189 | static void pcsp_stop_beep(struct snd_pcsp *chip) | |
190 | { | |
96c7d478 TI |
191 | pcsp_sync_stop(chip); |
192 | pcspkr_stop_sound(); | |
9ab4d072 SS |
193 | } |
194 | ||
d34e4e00 | 195 | #ifdef CONFIG_PM_SLEEP |
284e7ca7 | 196 | static int pcsp_suspend(struct device *dev) |
9ab4d072 | 197 | { |
284e7ca7 | 198 | struct snd_pcsp *chip = dev_get_drvdata(dev); |
9ab4d072 | 199 | pcsp_stop_beep(chip); |
9ab4d072 SS |
200 | return 0; |
201 | } | |
284e7ca7 TI |
202 | |
203 | static SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL); | |
204 | #define PCSP_PM_OPS &pcsp_pm | |
983e0972 | 205 | #else |
284e7ca7 | 206 | #define PCSP_PM_OPS NULL |
d34e4e00 | 207 | #endif /* CONFIG_PM_SLEEP */ |
9ab4d072 SS |
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", | |
284e7ca7 | 218 | .pm = PCSP_PM_OPS, |
9ab4d072 SS |
219 | }, |
220 | .probe = pcsp_probe, | |
fbbb01a1 | 221 | .remove = pcsp_remove, |
9ab4d072 SS |
222 | .shutdown = pcsp_shutdown, |
223 | }; | |
224 | ||
225 | static int __init pcsp_init(void) | |
226 | { | |
52337310 SS |
227 | if (!enable) |
228 | return -ENODEV; | |
9ab4d072 SS |
229 | return platform_driver_register(&pcsp_platform_driver); |
230 | } | |
231 | ||
232 | static void __exit pcsp_exit(void) | |
233 | { | |
234 | platform_driver_unregister(&pcsp_platform_driver); | |
235 | } | |
236 | ||
237 | module_init(pcsp_init); | |
238 | module_exit(pcsp_exit); |