1// SPDX-License-Identifier: GPL-2.0-only
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>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <sound/core.h>
13#include <sound/initval.h>
14#include <sound/pcm.h>
15#include <linux/input.h>
16#include <linux/delay.h>
17#include <linux/bitops.h>
18#include <linux/mm.h>
19#include "pcsp_input.h"
20#include "pcsp.h"
21
22MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>");
23MODULE_DESCRIPTION("PC-Speaker driver");
24MODULE_LICENSE("GPL");
25MODULE_ALIAS("platform:pcspkr");
26
27static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
28static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
29static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
30static bool nopcm; /* Disable PCM capability of the driver */
31
32module_param(index, int, 0444);
33MODULE_PARM_DESC(index, "Index value for pcsp soundcard.");
34module_param(id, charp, 0444);
35MODULE_PARM_DESC(id, "ID string for pcsp soundcard.");
36module_param(enable, bool, 0444);
37MODULE_PARM_DESC(enable, "Enable PC-Speaker sound.");
38module_param(nopcm, bool, 0444);
39MODULE_PARM_DESC(nopcm, "Disable PC-Speaker PCM sound. Only beeps remain.");
40
41struct snd_pcsp pcsp_chip;
42
43static int snd_pcsp_create(struct snd_card *card)
44{
45 unsigned int resolution = hrtimer_resolution;
46 int div, min_div, order;
47
48 if (!nopcm) {
49 if (resolution > PCSP_MAX_PERIOD_NS) {
50 dev_err(card->dev,
51 "PCSP: Timer resolution is not sufficient (%unS)\n",
52 resolution);
53 dev_err(card->dev,
54 "PCSP: Make sure you have HPET and ACPI enabled.\n");
55 dev_err(card->dev, "PCSP: Turned into nopcm mode.\n");
56 nopcm = 1;
57 }
58 }
59
60 if (loops_per_jiffy >= PCSP_MIN_LPJ && resolution <= PCSP_MIN_PERIOD_NS)
61 min_div = MIN_DIV;
62 else
63 min_div = MAX_DIV;
64#if PCSP_DEBUG
65 dev_dbg(card->dev, "PCSP: lpj=%li, min_div=%i, res=%u\n",
66 loops_per_jiffy, min_div, resolution);
67#endif
68
69 div = MAX_DIV / min_div;
70 order = fls(x: 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(v: &pcsp_chip.timer_active, i: 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 card->private_data = &pcsp_chip;
87
88 return 0;
89}
90
91static void pcsp_stop_beep(struct snd_pcsp *chip);
92
93static void alsa_card_pcsp_free(struct snd_card *card)
94{
95 pcsp_stop_beep(chip: card->private_data);
96}
97
98static int snd_card_pcsp_probe(int devnum, struct device *dev)
99{
100 struct snd_card *card;
101 int err;
102
103 if (devnum != 0)
104 return -EINVAL;
105
106 hrtimer_setup(timer: &pcsp_chip.timer, function: pcsp_do_timer, CLOCK_MONOTONIC, mode: HRTIMER_MODE_REL);
107
108 err = snd_devm_card_new(parent: dev, idx: index, xid: id, THIS_MODULE, extra_size: 0, card_ret: &card);
109 if (err < 0)
110 return err;
111
112 err = snd_pcsp_create(card);
113 if (err < 0)
114 return err;
115
116 if (!nopcm) {
117 err = snd_pcsp_new_pcm(chip: &pcsp_chip);
118 if (err < 0)
119 return err;
120 }
121 err = snd_pcsp_new_mixer(chip: &pcsp_chip, nopcm);
122 if (err < 0)
123 return err;
124
125 strscpy(card->driver, "PC-Speaker");
126 strscpy(card->shortname, "pcsp");
127 sprintf(buf: card->longname, fmt: "Internal PC-Speaker at port 0x%x",
128 pcsp_chip.port);
129
130 err = snd_card_register(card);
131 if (err < 0)
132 return err;
133 card->private_free = alsa_card_pcsp_free;
134
135 return 0;
136}
137
138static int alsa_card_pcsp_init(struct device *dev)
139{
140 int err;
141
142 err = snd_card_pcsp_probe(devnum: 0, dev);
143 if (err) {
144 dev_err(dev, "PC-Speaker initialization failed.\n");
145 return err;
146 }
147
148 /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
149 if (debug_pagealloc_enabled()) {
150 dev_warn(dev,
151 "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, which may make the sound noisy.\n");
152 }
153
154 return 0;
155}
156
157static int pcsp_probe(struct platform_device *dev)
158{
159 int err;
160
161 err = pcspkr_input_init(rdev: &pcsp_chip.input_dev, dev: &dev->dev);
162 if (err < 0)
163 return err;
164
165 err = alsa_card_pcsp_init(dev: &dev->dev);
166 if (err < 0)
167 return err;
168
169 platform_set_drvdata(pdev: dev, data: &pcsp_chip);
170 return 0;
171}
172
173static void pcsp_stop_beep(struct snd_pcsp *chip)
174{
175 pcsp_sync_stop(chip);
176 pcspkr_stop_sound();
177}
178
179static int pcsp_suspend(struct device *dev)
180{
181 struct snd_pcsp *chip = dev_get_drvdata(dev);
182 pcsp_stop_beep(chip);
183 return 0;
184}
185
186static DEFINE_SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL);
187
188static void pcsp_shutdown(struct platform_device *dev)
189{
190 struct snd_pcsp *chip = platform_get_drvdata(pdev: dev);
191 pcsp_stop_beep(chip);
192}
193
194static struct platform_driver pcsp_platform_driver = {
195 .driver = {
196 .name = "pcspkr",
197 .pm = &pcsp_pm,
198 },
199 .probe = pcsp_probe,
200 .shutdown = pcsp_shutdown,
201};
202
203static int __init pcsp_init(void)
204{
205 if (!enable)
206 return -ENODEV;
207 return platform_driver_register(&pcsp_platform_driver);
208}
209
210static void __exit pcsp_exit(void)
211{
212 platform_driver_unregister(&pcsp_platform_driver);
213}
214
215module_init(pcsp_init);
216module_exit(pcsp_exit);
217

source code of linux/sound/drivers/pcsp/pcsp.c