blob: 328bd29264ce555e048da881af2dcc7e60e289d6 [file] [log] [blame]
Stas Sergeev9ab4d072008-03-03 10:53:54 +01001/*
2 * PC-Speaker driver for Linux
3 *
4 * Copyright (C) 1997-2001 David Woodhouse
5 * Copyright (C) 2001-2008 Stas Sergeev
6 */
7
8#include <linux/init.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -04009#include <linux/module.h>
Stas Sergeev9ab4d072008-03-03 10:53:54 +010010#include <linux/platform_device.h>
11#include <sound/core.h>
12#include <sound/initval.h>
13#include <sound/pcm.h>
Stas Sergeev9ab4d072008-03-03 10:53:54 +010014#include <linux/input.h>
Mariusz Kozlowski9ecaeda2008-03-18 09:03:03 +010015#include <linux/delay.h>
Stas Sergeev9ab4d072008-03-03 10:53:54 +010016#include <asm/bitops.h>
17#include "pcsp_input.h"
18#include "pcsp.h"
19
20MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>");
21MODULE_DESCRIPTION("PC-Speaker driver");
22MODULE_LICENSE("GPL");
23MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}");
24MODULE_ALIAS("platform:pcspkr");
25
26static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
27static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103028static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
29static bool nopcm; /* Disable PCM capability of the driver */
Stas Sergeev9ab4d072008-03-03 10:53:54 +010030
31module_param(index, int, 0444);
32MODULE_PARM_DESC(index, "Index value for pcsp soundcard.");
33module_param(id, charp, 0444);
34MODULE_PARM_DESC(id, "ID string for pcsp soundcard.");
35module_param(enable, bool, 0444);
Stas Sergeev52337312008-03-06 11:01:16 +010036MODULE_PARM_DESC(enable, "Enable PC-Speaker sound.");
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +010037module_param(nopcm, bool, 0444);
38MODULE_PARM_DESC(nopcm, "Disable PC-Speaker PCM sound. Only beeps remain.");
Stas Sergeev9ab4d072008-03-03 10:53:54 +010039
40struct snd_pcsp pcsp_chip;
41
Bill Pembertonfbbb01a2012-12-06 12:35:27 -050042static int snd_pcsp_create(struct snd_card *card)
Stas Sergeev9ab4d072008-03-03 10:53:54 +010043{
44 static struct snd_device_ops ops = { };
45 struct timespec tp;
46 int err;
47 int div, min_div, order;
48
Takashi Iwai75415df2013-10-29 15:15:20 +010049 hrtimer_get_res(CLOCK_MONOTONIC, &tp);
50
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +010051 if (!nopcm) {
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +010052 if (tp.tv_sec || tp.tv_nsec > PCSP_MAX_PERIOD_NS) {
53 printk(KERN_ERR "PCSP: Timer resolution is not sufficient "
54 "(%linS)\n", tp.tv_nsec);
55 printk(KERN_ERR "PCSP: Make sure you have HPET and ACPI "
56 "enabled.\n");
57 printk(KERN_ERR "PCSP: Turned into nopcm mode.\n");
58 nopcm = 1;
59 }
Stas Sergeev9ab4d072008-03-03 10:53:54 +010060 }
61
62 if (loops_per_jiffy >= PCSP_MIN_LPJ && tp.tv_nsec <= PCSP_MIN_PERIOD_NS)
63 min_div = MIN_DIV;
64 else
65 min_div = MAX_DIV;
66#if PCSP_DEBUG
Takashi Iwai45203832009-02-05 15:51:50 +010067 printk(KERN_DEBUG "PCSP: lpj=%li, min_div=%i, res=%li\n",
Stas Sergeev9ab4d072008-03-03 10:53:54 +010068 loops_per_jiffy, min_div, tp.tv_nsec);
69#endif
70
71 div = MAX_DIV / min_div;
72 order = fls(div) - 1;
73
74 pcsp_chip.max_treble = min(order, PCSP_MAX_TREBLE);
75 pcsp_chip.treble = min(pcsp_chip.max_treble, PCSP_DEFAULT_TREBLE);
76 pcsp_chip.playback_ptr = 0;
77 pcsp_chip.period_ptr = 0;
78 atomic_set(&pcsp_chip.timer_active, 0);
79 pcsp_chip.enable = 1;
80 pcsp_chip.pcspkr = 1;
81
82 spin_lock_init(&pcsp_chip.substream_lock);
83
84 pcsp_chip.card = card;
85 pcsp_chip.port = 0x61;
86 pcsp_chip.irq = -1;
87 pcsp_chip.dma = -1;
88
89 /* Register device */
90 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, &pcsp_chip, &ops);
91 if (err < 0)
92 return err;
93
94 return 0;
95}
96
Bill Pembertonfbbb01a2012-12-06 12:35:27 -050097static int snd_card_pcsp_probe(int devnum, struct device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +010098{
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);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100106 pcsp_chip.timer.function = pcsp_do_timer;
107
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100108 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
109 if (err < 0)
110 return err;
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100111
112 err = snd_pcsp_create(card);
113 if (err < 0) {
114 snd_card_free(card);
115 return err;
116 }
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +0100117 if (!nopcm) {
118 err = snd_pcsp_new_pcm(&pcsp_chip);
119 if (err < 0) {
120 snd_card_free(card);
121 return err;
122 }
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100123 }
Stas Sergeevbcc2c6b2009-11-01 11:13:19 +0100124 err = snd_pcsp_new_mixer(&pcsp_chip, nopcm);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100125 if (err < 0) {
126 snd_card_free(card);
127 return err;
128 }
129
130 snd_card_set_dev(pcsp_chip.card, dev);
131
132 strcpy(card->driver, "PC-Speaker");
133 strcpy(card->shortname, "pcsp");
134 sprintf(card->longname, "Internal PC-Speaker at port 0x%x",
135 pcsp_chip.port);
136
137 err = snd_card_register(card);
138 if (err < 0) {
139 snd_card_free(card);
140 return err;
141 }
142
143 return 0;
144}
145
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500146static int alsa_card_pcsp_init(struct device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100147{
Stas Sergeev52337312008-03-06 11:01:16 +0100148 int err;
149
150 err = snd_card_pcsp_probe(0, dev);
151 if (err) {
152 printk(KERN_ERR "PC-Speaker initialization failed.\n");
153 return err;
154 }
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100155
156#ifdef CONFIG_DEBUG_PAGEALLOC
157 /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
Stas Sergeevefd89d92008-04-23 17:16:38 +0200158 printk(KERN_WARNING "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, "
159 "which may make the sound noisy.\n");
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100160#endif
161
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100162 return 0;
163}
164
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500165static void alsa_card_pcsp_exit(struct snd_pcsp *chip)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100166{
167 snd_card_free(chip->card);
168}
169
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500170static int pcsp_probe(struct platform_device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100171{
172 int err;
Stas Sergeev52337312008-03-06 11:01:16 +0100173
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100174 err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev);
175 if (err < 0)
176 return err;
177
178 err = alsa_card_pcsp_init(&dev->dev);
179 if (err < 0) {
180 pcspkr_input_remove(pcsp_chip.input_dev);
181 return err;
182 }
183
184 platform_set_drvdata(dev, &pcsp_chip);
185 return 0;
186}
187
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500188static int pcsp_remove(struct platform_device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100189{
190 struct snd_pcsp *chip = platform_get_drvdata(dev);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100191 pcspkr_input_remove(chip->input_dev);
Takashi Iwai6408eac2013-11-14 15:45:12 +0100192 alsa_card_pcsp_exit(chip);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100193 return 0;
194}
195
196static void pcsp_stop_beep(struct snd_pcsp *chip)
197{
Takashi Iwai96c7d472008-08-11 10:18:39 +0200198 pcsp_sync_stop(chip);
199 pcspkr_stop_sound();
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100200}
201
Takashi Iwaid34e4e02012-08-09 15:47:15 +0200202#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200203static int pcsp_suspend(struct device *dev)
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100204{
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200205 struct snd_pcsp *chip = dev_get_drvdata(dev);
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100206 pcsp_stop_beep(chip);
207 snd_pcm_suspend_all(chip->pcm);
208 return 0;
209}
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200210
211static SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL);
212#define PCSP_PM_OPS &pcsp_pm
Johann Felix Soden983e0972008-05-02 09:54:31 +0200213#else
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200214#define PCSP_PM_OPS NULL
Takashi Iwaid34e4e02012-08-09 15:47:15 +0200215#endif /* CONFIG_PM_SLEEP */
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100216
217static void pcsp_shutdown(struct platform_device *dev)
218{
219 struct snd_pcsp *chip = platform_get_drvdata(dev);
220 pcsp_stop_beep(chip);
221}
222
223static struct platform_driver pcsp_platform_driver = {
224 .driver = {
225 .name = "pcspkr",
226 .owner = THIS_MODULE,
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200227 .pm = PCSP_PM_OPS,
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100228 },
229 .probe = pcsp_probe,
Bill Pembertonfbbb01a2012-12-06 12:35:27 -0500230 .remove = pcsp_remove,
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100231 .shutdown = pcsp_shutdown,
232};
233
234static int __init pcsp_init(void)
235{
Stas Sergeev52337312008-03-06 11:01:16 +0100236 if (!enable)
237 return -ENODEV;
Stas Sergeev9ab4d072008-03-03 10:53:54 +0100238 return platform_driver_register(&pcsp_platform_driver);
239}
240
241static void __exit pcsp_exit(void)
242{
243 platform_driver_unregister(&pcsp_platform_driver);
244}
245
246module_init(pcsp_init);
247module_exit(pcsp_exit);