blob: fd79e57c85caec55f5a922d408edf031ea5acca7 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Stas Sergeev9ab4d072008-03-03 10:53:54 +01002/*
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>
Paul Gortmaker65a77212011-07-15 13:13:37 -040010#include <linux/module.h>
Stas Sergeev9ab4d072008-03-03 10:53:54 +010011#include <linux/platform_device.h>
12#include <sound/core.h>
13#include <sound/initval.h>
14#include <sound/pcm.h>
Stas Sergeev9ab4d072008-03-03 10:53:54 +010015#include <linux/input.h>
Mariusz Kozlowski9ecaeda2008-03-18 09:03:03 +010016#include <linux/delay.h>
Takashi Iwai976412f2015-01-28 17:24:43 +010017#include <linux/bitops.h>
Joonsoo Kim505f6d22016-03-17 14:17:56 -070018#include <linux/mm.h>
Stas Sergeev9ab4d072008-03-03 10:53:54 +010019#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_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}");
26MODULE_ALIAS("platform:pcspkr");
27
28static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
29static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103030static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
31static bool nopcm; /* Disable PCM capability of the driver */
Stas Sergeev9ab4d072008-03-03 10:53:54 +010032
33module_param(index, int, 0444);
34MODULE_PARM_DESC(index, "Index value for pcsp soundcard.");
35module_param(id, charp, 0444);
36MODULE_PARM_DESC(id, "ID string for pcsp soundcard.");
37module_param(enable, bool, 0444);
Stas Sergeev52337312008-03-06 11:01:16 +010038MODULE_PARM_DESC(enable, "Enable PC-Speaker sound.");
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +010039module_param(nopcm, bool, 0444);
40MODULE_PARM_DESC(nopcm, "Disable PC-Speaker PCM sound. Only beeps remain.");
Stas Sergeev9ab4d072008-03-03 10:53:54 +010041
42struct snd_pcsp pcsp_chip;
43
Bill Pembertonfbbb01a2012-12-06 12:35:27 -050044static int snd_pcsp_create(struct snd_card *card)
Stas Sergeev9ab4d072008-03-03 10:53:54 +010045{
Takashi Iwai34273b22020-01-03 09:16:21 +010046 static const struct snd_device_ops ops = { };
Thomas Gleixner447fbbd2015-04-14 21:08:30 +000047 unsigned int resolution = hrtimer_resolution;
48 int err, div, min_div, order;
Takashi Iwai75415df2013-10-29 15:15:20 +010049
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +010050 if (!nopcm) {
Thomas Gleixner447fbbd2015-04-14 21:08:30 +000051 if (resolution > PCSP_MAX_PERIOD_NS) {
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +010052 printk(KERN_ERR "PCSP: Timer resolution is not sufficient "
Thomas Gleixnerc78c8812015-05-12 09:14:51 +020053 "(%unS)\n", resolution);
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +010054 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 }
Stas Sergeev9ab4d072008-03-03 10:53:54 +010059 }
60
Thomas Gleixner447fbbd2015-04-14 21:08:30 +000061 if (loops_per_jiffy >= PCSP_MIN_LPJ && resolution <= PCSP_MIN_PERIOD_NS)
Stas Sergeev9ab4d072008-03-03 10:53:54 +010062 min_div = MIN_DIV;
63 else
64 min_div = MAX_DIV;
65#if PCSP_DEBUG
Thomas Gleixnerc78c8812015-05-12 09:14:51 +020066 printk(KERN_DEBUG "PCSP: lpj=%li, min_div=%i, res=%u\n",
Thomas Gleixner447fbbd2015-04-14 21:08:30 +000067 loops_per_jiffy, min_div, resolution);
Stas Sergeev9ab4d072008-03-03 10:53:54 +010068#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
Bill Pembertonfbbb01a2012-12-06 12:35:27 -050096static int snd_card_pcsp_probe(int devnum, struct device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +010097{
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);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100105 pcsp_chip.timer.function = pcsp_do_timer;
106
Takashi Iwai5872f3f2014-01-29 12:59:08 +0100107 err = snd_card_new(dev, index, id, THIS_MODULE, 0, &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100108 if (err < 0)
109 return err;
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100110
111 err = snd_pcsp_create(card);
Markus Elfring2cded8c2017-08-22 17:33:33 +0200112 if (err < 0)
113 goto free_card;
114
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +0100115 if (!nopcm) {
116 err = snd_pcsp_new_pcm(&pcsp_chip);
Markus Elfring2cded8c2017-08-22 17:33:33 +0200117 if (err < 0)
118 goto free_card;
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100119 }
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +0100120 err = snd_pcsp_new_mixer(&pcsp_chip, nopcm);
Markus Elfring2cded8c2017-08-22 17:33:33 +0200121 if (err < 0)
122 goto free_card;
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100123
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100124 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);
Markus Elfring2cded8c2017-08-22 17:33:33 +0200130 if (err < 0)
131 goto free_card;
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100132
133 return 0;
Markus Elfring2cded8c2017-08-22 17:33:33 +0200134
135free_card:
136 snd_card_free(card);
137 return err;
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100138}
139
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500140static int alsa_card_pcsp_init(struct device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100141{
Stas Sergeev52337312008-03-06 11:01:16 +0100142 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 }
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100149
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100150 /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
Joonsoo Kim505f6d22016-03-17 14:17:56 -0700151 if (debug_pagealloc_enabled()) {
152 printk(KERN_WARNING "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, "
153 "which may make the sound noisy.\n");
154 }
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100155
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100156 return 0;
157}
158
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500159static void alsa_card_pcsp_exit(struct snd_pcsp *chip)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100160{
161 snd_card_free(chip->card);
162}
163
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500164static int pcsp_probe(struct platform_device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100165{
166 int err;
Stas Sergeev52337312008-03-06 11:01:16 +0100167
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100168 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
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500182static int pcsp_remove(struct platform_device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100183{
184 struct snd_pcsp *chip = platform_get_drvdata(dev);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100185 pcspkr_input_remove(chip->input_dev);
Takashi Iwai6408eac2013-11-14 15:45:12 +0100186 alsa_card_pcsp_exit(chip);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100187 return 0;
188}
189
190static void pcsp_stop_beep(struct snd_pcsp *chip)
191{
Takashi Iwai96c7d472008-08-11 10:18:39 +0200192 pcsp_sync_stop(chip);
193 pcspkr_stop_sound();
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100194}
195
Takashi Iwaid34e4e02012-08-09 15:47:15 +0200196#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200197static int pcsp_suspend(struct device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100198{
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200199 struct snd_pcsp *chip = dev_get_drvdata(dev);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100200 pcsp_stop_beep(chip);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100201 return 0;
202}
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200203
204static SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL);
205#define PCSP_PM_OPS &pcsp_pm
Johann Felix Soden983e0972008-05-02 09:54:31 +0200206#else
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200207#define PCSP_PM_OPS NULL
Takashi Iwaid34e4e02012-08-09 15:47:15 +0200208#endif /* CONFIG_PM_SLEEP */
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100209
210static void pcsp_shutdown(struct platform_device *dev)
211{
212 struct snd_pcsp *chip = platform_get_drvdata(dev);
213 pcsp_stop_beep(chip);
214}
215
216static struct platform_driver pcsp_platform_driver = {
217 .driver = {
218 .name = "pcspkr",
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200219 .pm = PCSP_PM_OPS,
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100220 },
221 .probe = pcsp_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500222 .remove = pcsp_remove,
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100223 .shutdown = pcsp_shutdown,
224};
225
226static int __init pcsp_init(void)
227{
Stas Sergeev52337312008-03-06 11:01:16 +0100228 if (!enable)
229 return -ENODEV;
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100230 return platform_driver_register(&pcsp_platform_driver);
231}
232
233static void __exit pcsp_exit(void)
234{
235 platform_driver_unregister(&pcsp_platform_driver);
236}
237
238module_init(pcsp_init);
239module_exit(pcsp_exit);