xref: /linux/sound/drivers/pcsp/pcsp.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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 
22 MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>");
23 MODULE_DESCRIPTION("PC-Speaker driver");
24 MODULE_LICENSE("GPL");
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 */
29 static bool enable = SNDRV_DEFAULT_ENABLE1;	/* Enable this card */
30 static bool nopcm;	/* Disable PCM capability of the driver */
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);
37 MODULE_PARM_DESC(enable, "Enable PC-Speaker sound.");
38 module_param(nopcm, bool, 0444);
39 MODULE_PARM_DESC(nopcm, "Disable PC-Speaker PCM sound. Only beeps remain.");
40 
41 struct snd_pcsp pcsp_chip;
42 
snd_pcsp_create(struct snd_card * card)43 static 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(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 	card->private_data = &pcsp_chip;
87 
88 	return 0;
89 }
90 
91 static void pcsp_stop_beep(struct snd_pcsp *chip);
92 
alsa_card_pcsp_free(struct snd_card * card)93 static void alsa_card_pcsp_free(struct snd_card *card)
94 {
95 	pcsp_stop_beep(card->private_data);
96 }
97 
snd_card_pcsp_probe(int devnum,struct device * dev)98 static 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_init(&pcsp_chip.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
107 	pcsp_chip.timer.function = pcsp_do_timer;
108 
109 	err = snd_devm_card_new(dev, index, id, THIS_MODULE, 0, &card);
110 	if (err < 0)
111 		return err;
112 
113 	err = snd_pcsp_create(card);
114 	if (err < 0)
115 		return err;
116 
117 	if (!nopcm) {
118 		err = snd_pcsp_new_pcm(&pcsp_chip);
119 		if (err < 0)
120 			return err;
121 	}
122 	err = snd_pcsp_new_mixer(&pcsp_chip, nopcm);
123 	if (err < 0)
124 		return err;
125 
126 	strcpy(card->driver, "PC-Speaker");
127 	strcpy(card->shortname, "pcsp");
128 	sprintf(card->longname, "Internal PC-Speaker at port 0x%x",
129 		pcsp_chip.port);
130 
131 	err = snd_card_register(card);
132 	if (err < 0)
133 		return err;
134 	card->private_free = alsa_card_pcsp_free;
135 
136 	return 0;
137 }
138 
alsa_card_pcsp_init(struct device * dev)139 static int alsa_card_pcsp_init(struct device *dev)
140 {
141 	int err;
142 
143 	err = snd_card_pcsp_probe(0, dev);
144 	if (err) {
145 		dev_err(dev, "PC-Speaker initialization failed.\n");
146 		return err;
147 	}
148 
149 	/* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
150 	if (debug_pagealloc_enabled()) {
151 		dev_warn(dev,
152 			 "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, which may make the sound noisy.\n");
153 	}
154 
155 	return 0;
156 }
157 
pcsp_probe(struct platform_device * dev)158 static int pcsp_probe(struct platform_device *dev)
159 {
160 	int err;
161 
162 	err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev);
163 	if (err < 0)
164 		return err;
165 
166 	err = alsa_card_pcsp_init(&dev->dev);
167 	if (err < 0)
168 		return err;
169 
170 	platform_set_drvdata(dev, &pcsp_chip);
171 	return 0;
172 }
173 
pcsp_stop_beep(struct snd_pcsp * chip)174 static void pcsp_stop_beep(struct snd_pcsp *chip)
175 {
176 	pcsp_sync_stop(chip);
177 	pcspkr_stop_sound();
178 }
179 
pcsp_suspend(struct device * dev)180 static int pcsp_suspend(struct device *dev)
181 {
182 	struct snd_pcsp *chip = dev_get_drvdata(dev);
183 	pcsp_stop_beep(chip);
184 	return 0;
185 }
186 
187 static DEFINE_SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL);
188 
pcsp_shutdown(struct platform_device * dev)189 static void pcsp_shutdown(struct platform_device *dev)
190 {
191 	struct snd_pcsp *chip = platform_get_drvdata(dev);
192 	pcsp_stop_beep(chip);
193 }
194 
195 static struct platform_driver pcsp_platform_driver = {
196 	.driver		= {
197 		.name	= "pcspkr",
198 		.pm	= &pcsp_pm,
199 	},
200 	.probe		= pcsp_probe,
201 	.shutdown	= pcsp_shutdown,
202 };
203 
pcsp_init(void)204 static int __init pcsp_init(void)
205 {
206 	if (!enable)
207 		return -ENODEV;
208 	return platform_driver_register(&pcsp_platform_driver);
209 }
210 
pcsp_exit(void)211 static void __exit pcsp_exit(void)
212 {
213 	platform_driver_unregister(&pcsp_platform_driver);
214 }
215 
216 module_init(pcsp_init);
217 module_exit(pcsp_exit);
218