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"); | |
25 | MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}"); | |
26 | MODULE_ALIAS("platform:pcspkr"); | |
27 | ||
28 | static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ | |
29 | static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ | |
a67ff6a5 RR |
30 | static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */ |
31 | static bool nopcm; /* Disable PCM capability of the driver */ | |
9ab4d072 SS |
32 | |
33 | module_param(index, int, 0444); | |
34 | MODULE_PARM_DESC(index, "Index value for pcsp soundcard."); | |
35 | module_param(id, charp, 0444); | |
36 | MODULE_PARM_DESC(id, "ID string for pcsp soundcard."); | |
37 | module_param(enable, bool, 0444); | |
52337310 | 38 | MODULE_PARM_DESC(enable, "Enable PC-Speaker sound."); |
bcc2c6b7 SS |
39 | module_param(nopcm, bool, 0444); |
40 | MODULE_PARM_DESC(nopcm, "Disable PC-Speaker PCM sound. Only beeps remain."); | |
9ab4d072 SS |
41 | |
42 | struct snd_pcsp pcsp_chip; | |
43 | ||
fbbb01a1 | 44 | static int snd_pcsp_create(struct snd_card *card) |
9ab4d072 SS |
45 | { |
46 | static struct snd_device_ops ops = { }; | |
447fbbdc TG |
47 | unsigned int resolution = hrtimer_resolution; |
48 | int err, div, min_div, order; | |
75415df8 | 49 | |
bcc2c6b7 | 50 | if (!nopcm) { |
447fbbdc | 51 | if (resolution > PCSP_MAX_PERIOD_NS) { |
bcc2c6b7 | 52 | printk(KERN_ERR "PCSP: Timer resolution is not sufficient " |
c78c8818 | 53 | "(%unS)\n", resolution); |
bcc2c6b7 SS |
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 | } | |
9ab4d072 SS |
59 | } |
60 | ||
447fbbdc | 61 | if (loops_per_jiffy >= PCSP_MIN_LPJ && resolution <= PCSP_MIN_PERIOD_NS) |
9ab4d072 SS |
62 | min_div = MIN_DIV; |
63 | else | |
64 | min_div = MAX_DIV; | |
65 | #if PCSP_DEBUG | |
c78c8818 | 66 | printk(KERN_DEBUG "PCSP: lpj=%li, min_div=%i, res=%u\n", |
447fbbdc | 67 | loops_per_jiffy, min_div, resolution); |
9ab4d072 SS |
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 | ||
fbbb01a1 | 96 | static int snd_card_pcsp_probe(int devnum, struct device *dev) |
9ab4d072 SS |
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); | |
9ab4d072 SS |
105 | pcsp_chip.timer.function = pcsp_do_timer; |
106 | ||
5872f3f6 | 107 | err = snd_card_new(dev, index, id, THIS_MODULE, 0, &card); |
bd7dd77c TI |
108 | if (err < 0) |
109 | return err; | |
9ab4d072 SS |
110 | |
111 | err = snd_pcsp_create(card); | |
2cded8c8 ME |
112 | if (err < 0) |
113 | goto free_card; | |
114 | ||
bcc2c6b7 SS |
115 | if (!nopcm) { |
116 | err = snd_pcsp_new_pcm(&pcsp_chip); | |
2cded8c8 ME |
117 | if (err < 0) |
118 | goto free_card; | |
9ab4d072 | 119 | } |
bcc2c6b7 | 120 | err = snd_pcsp_new_mixer(&pcsp_chip, nopcm); |
2cded8c8 ME |
121 | if (err < 0) |
122 | goto free_card; | |
9ab4d072 | 123 | |
9ab4d072 SS |
124 | strcpy(card->driver, "PC-Speaker"); |
125 | strcpy(card->shortname, "pcsp"); | |
126 | sprintf(card->longname, "Internal PC-Speaker at port 0x%x", | |
127 | pcsp_chip.port); | |
128 | ||
129 | err = snd_card_register(card); | |
2cded8c8 ME |
130 | if (err < 0) |
131 | goto free_card; | |
9ab4d072 SS |
132 | |
133 | return 0; | |
2cded8c8 ME |
134 | |
135 | free_card: | |
136 | snd_card_free(card); | |
137 | return err; | |
9ab4d072 SS |
138 | } |
139 | ||
fbbb01a1 | 140 | static int alsa_card_pcsp_init(struct device *dev) |
9ab4d072 | 141 | { |
52337310 SS |
142 | int err; |
143 | ||
144 | err = snd_card_pcsp_probe(0, dev); | |
145 | if (err) { | |
146 | printk(KERN_ERR "PC-Speaker initialization failed.\n"); | |
147 | return err; | |
148 | } | |
9ab4d072 | 149 | |
9ab4d072 | 150 | /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */ |
505f6d22 JK |
151 | if (debug_pagealloc_enabled()) { |
152 | printk(KERN_WARNING "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, " | |
153 | "which may make the sound noisy.\n"); | |
154 | } | |
9ab4d072 | 155 | |
9ab4d072 SS |
156 | return 0; |
157 | } | |
158 | ||
fbbb01a1 | 159 | static void alsa_card_pcsp_exit(struct snd_pcsp *chip) |
9ab4d072 SS |
160 | { |
161 | snd_card_free(chip->card); | |
162 | } | |
163 | ||
fbbb01a1 | 164 | static int pcsp_probe(struct platform_device *dev) |
9ab4d072 SS |
165 | { |
166 | int err; | |
52337310 | 167 | |
9ab4d072 SS |
168 | err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev); |
169 | if (err < 0) | |
170 | return err; | |
171 | ||
172 | err = alsa_card_pcsp_init(&dev->dev); | |
173 | if (err < 0) { | |
174 | pcspkr_input_remove(pcsp_chip.input_dev); | |
175 | return err; | |
176 | } | |
177 | ||
178 | platform_set_drvdata(dev, &pcsp_chip); | |
179 | return 0; | |
180 | } | |
181 | ||
fbbb01a1 | 182 | static int pcsp_remove(struct platform_device *dev) |
9ab4d072 SS |
183 | { |
184 | struct snd_pcsp *chip = platform_get_drvdata(dev); | |
9ab4d072 | 185 | pcspkr_input_remove(chip->input_dev); |
6408eac2 | 186 | alsa_card_pcsp_exit(chip); |
9ab4d072 SS |
187 | return 0; |
188 | } | |
189 | ||
190 | static void pcsp_stop_beep(struct snd_pcsp *chip) | |
191 | { | |
96c7d478 TI |
192 | pcsp_sync_stop(chip); |
193 | pcspkr_stop_sound(); | |
9ab4d072 SS |
194 | } |
195 | ||
d34e4e00 | 196 | #ifdef CONFIG_PM_SLEEP |
284e7ca7 | 197 | static int pcsp_suspend(struct device *dev) |
9ab4d072 | 198 | { |
284e7ca7 | 199 | struct snd_pcsp *chip = dev_get_drvdata(dev); |
9ab4d072 | 200 | pcsp_stop_beep(chip); |
9ab4d072 SS |
201 | return 0; |
202 | } | |
284e7ca7 TI |
203 | |
204 | static SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL); | |
205 | #define PCSP_PM_OPS &pcsp_pm | |
983e0972 | 206 | #else |
284e7ca7 | 207 | #define PCSP_PM_OPS NULL |
d34e4e00 | 208 | #endif /* CONFIG_PM_SLEEP */ |
9ab4d072 SS |
209 | |
210 | static void pcsp_shutdown(struct platform_device *dev) | |
211 | { | |
212 | struct snd_pcsp *chip = platform_get_drvdata(dev); | |
213 | pcsp_stop_beep(chip); | |
214 | } | |
215 | ||
216 | static struct platform_driver pcsp_platform_driver = { | |
217 | .driver = { | |
218 | .name = "pcspkr", | |
284e7ca7 | 219 | .pm = PCSP_PM_OPS, |
9ab4d072 SS |
220 | }, |
221 | .probe = pcsp_probe, | |
fbbb01a1 | 222 | .remove = pcsp_remove, |
9ab4d072 SS |
223 | .shutdown = pcsp_shutdown, |
224 | }; | |
225 | ||
226 | static int __init pcsp_init(void) | |
227 | { | |
52337310 SS |
228 | if (!enable) |
229 | return -ENODEV; | |
9ab4d072 SS |
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); |