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 | { |
447fbbdc | 45 | unsigned int resolution = hrtimer_resolution; |
3a1e341c | 46 | int div, min_div, order; |
75415df8 | 47 | |
bcc2c6b7 | 48 | if (!nopcm) { |
447fbbdc | 49 | if (resolution > PCSP_MAX_PERIOD_NS) { |
bcc2c6b7 | 50 | printk(KERN_ERR "PCSP: Timer resolution is not sufficient " |
c78c8818 | 51 | "(%unS)\n", resolution); |
bcc2c6b7 SS |
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 | } | |
9ab4d072 SS |
57 | } |
58 | ||
447fbbdc | 59 | if (loops_per_jiffy >= PCSP_MIN_LPJ && resolution <= PCSP_MIN_PERIOD_NS) |
9ab4d072 SS |
60 | min_div = MIN_DIV; |
61 | else | |
62 | min_div = MAX_DIV; | |
63 | #if PCSP_DEBUG | |
c78c8818 | 64 | printk(KERN_DEBUG "PCSP: lpj=%li, min_div=%i, res=%u\n", |
447fbbdc | 65 | loops_per_jiffy, min_div, resolution); |
9ab4d072 SS |
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; | |
3a1e341c | 85 | card->private_data = &pcsp_chip; |
9ab4d072 SS |
86 | |
87 | return 0; | |
88 | } | |
89 | ||
3a1e341c TI |
90 | static void pcsp_stop_beep(struct snd_pcsp *chip); |
91 | ||
92 | static void alsa_card_pcsp_free(struct snd_card *card) | |
93 | { | |
94 | pcsp_stop_beep(card->private_data); | |
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 | ||
3a1e341c | 108 | err = snd_devm_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); | |
2cded8c8 | 113 | if (err < 0) |
3a1e341c | 114 | return err; |
2cded8c8 | 115 | |
bcc2c6b7 SS |
116 | if (!nopcm) { |
117 | err = snd_pcsp_new_pcm(&pcsp_chip); | |
2cded8c8 | 118 | if (err < 0) |
3a1e341c | 119 | return err; |
9ab4d072 | 120 | } |
bcc2c6b7 | 121 | err = snd_pcsp_new_mixer(&pcsp_chip, nopcm); |
2cded8c8 | 122 | if (err < 0) |
3a1e341c | 123 | return err; |
9ab4d072 | 124 | |
9ab4d072 SS |
125 | strcpy(card->driver, "PC-Speaker"); |
126 | strcpy(card->shortname, "pcsp"); | |
127 | sprintf(card->longname, "Internal PC-Speaker at port 0x%x", | |
128 | pcsp_chip.port); | |
129 | ||
130 | err = snd_card_register(card); | |
2cded8c8 | 131 | if (err < 0) |
3a1e341c TI |
132 | return err; |
133 | card->private_free = alsa_card_pcsp_free; | |
9ab4d072 SS |
134 | |
135 | return 0; | |
136 | } | |
137 | ||
fbbb01a1 | 138 | static int alsa_card_pcsp_init(struct device *dev) |
9ab4d072 | 139 | { |
52337310 SS |
140 | int err; |
141 | ||
142 | err = snd_card_pcsp_probe(0, dev); | |
143 | if (err) { | |
144 | printk(KERN_ERR "PC-Speaker initialization failed.\n"); | |
145 | return err; | |
146 | } | |
9ab4d072 | 147 | |
9ab4d072 | 148 | /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */ |
505f6d22 JK |
149 | if (debug_pagealloc_enabled()) { |
150 | printk(KERN_WARNING "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, " | |
151 | "which may make the sound noisy.\n"); | |
152 | } | |
9ab4d072 | 153 | |
9ab4d072 SS |
154 | return 0; |
155 | } | |
156 | ||
fbbb01a1 | 157 | static int pcsp_probe(struct platform_device *dev) |
9ab4d072 SS |
158 | { |
159 | int err; | |
52337310 | 160 | |
9ab4d072 SS |
161 | err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev); |
162 | if (err < 0) | |
163 | return err; | |
164 | ||
165 | err = alsa_card_pcsp_init(&dev->dev); | |
3a1e341c | 166 | if (err < 0) |
9ab4d072 | 167 | return err; |
9ab4d072 SS |
168 | |
169 | platform_set_drvdata(dev, &pcsp_chip); | |
170 | return 0; | |
171 | } | |
172 | ||
9ab4d072 SS |
173 | static void pcsp_stop_beep(struct snd_pcsp *chip) |
174 | { | |
96c7d478 TI |
175 | pcsp_sync_stop(chip); |
176 | pcspkr_stop_sound(); | |
9ab4d072 SS |
177 | } |
178 | ||
d34e4e00 | 179 | #ifdef CONFIG_PM_SLEEP |
284e7ca7 | 180 | static int pcsp_suspend(struct device *dev) |
9ab4d072 | 181 | { |
284e7ca7 | 182 | struct snd_pcsp *chip = dev_get_drvdata(dev); |
9ab4d072 | 183 | pcsp_stop_beep(chip); |
9ab4d072 SS |
184 | return 0; |
185 | } | |
284e7ca7 TI |
186 | |
187 | static SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL); | |
188 | #define PCSP_PM_OPS &pcsp_pm | |
983e0972 | 189 | #else |
284e7ca7 | 190 | #define PCSP_PM_OPS NULL |
d34e4e00 | 191 | #endif /* CONFIG_PM_SLEEP */ |
9ab4d072 SS |
192 | |
193 | static void pcsp_shutdown(struct platform_device *dev) | |
194 | { | |
195 | struct snd_pcsp *chip = platform_get_drvdata(dev); | |
196 | pcsp_stop_beep(chip); | |
197 | } | |
198 | ||
199 | static struct platform_driver pcsp_platform_driver = { | |
200 | .driver = { | |
201 | .name = "pcspkr", | |
284e7ca7 | 202 | .pm = PCSP_PM_OPS, |
9ab4d072 SS |
203 | }, |
204 | .probe = pcsp_probe, | |
9ab4d072 SS |
205 | .shutdown = pcsp_shutdown, |
206 | }; | |
207 | ||
208 | static int __init pcsp_init(void) | |
209 | { | |
52337310 SS |
210 | if (!enable) |
211 | return -ENODEV; | |
9ab4d072 SS |
212 | return platform_driver_register(&pcsp_platform_driver); |
213 | } | |
214 | ||
215 | static void __exit pcsp_exit(void) | |
216 | { | |
217 | platform_driver_unregister(&pcsp_platform_driver); | |
218 | } | |
219 | ||
220 | module_init(pcsp_init); | |
221 | module_exit(pcsp_exit); |