2 * PC-Speaker driver for Linux
4 * Copyright (C) 1997-2001 David Woodhouse
5 * Copyright (C) 2001-2008 Stas Sergeev
8 #include <linux/init.h>
9 #include <linux/moduleparam.h>
10 #include <linux/platform_device.h>
11 #include <sound/core.h>
12 #include <sound/initval.h>
13 #include <sound/pcm.h>
14 #include <linux/input.h>
15 #include <linux/delay.h>
16 #include <asm/bitops.h>
17 #include "pcsp_input.h"
20 MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>");
21 MODULE_DESCRIPTION("PC-Speaker driver");
22 MODULE_LICENSE("GPL");
23 MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}");
24 MODULE_ALIAS("platform:pcspkr");
26 static int index
= SNDRV_DEFAULT_IDX1
; /* Index 0-MAX */
27 static char *id
= SNDRV_DEFAULT_STR1
; /* ID for this card */
28 static int enable
= SNDRV_DEFAULT_ENABLE1
; /* Enable this card */
30 module_param(index
, int, 0444);
31 MODULE_PARM_DESC(index
, "Index value for pcsp soundcard.");
32 module_param(id
, charp
, 0444);
33 MODULE_PARM_DESC(id
, "ID string for pcsp soundcard.");
34 module_param(enable
, bool, 0444);
35 MODULE_PARM_DESC(enable
, "Enable PC-Speaker sound.");
37 struct snd_pcsp pcsp_chip
;
39 static int __devinit
snd_pcsp_create(struct snd_card
*card
)
41 static struct snd_device_ops ops
= { };
44 int div
, min_div
, order
;
46 hrtimer_get_res(CLOCK_MONOTONIC
, &tp
);
47 if (tp
.tv_sec
|| tp
.tv_nsec
> PCSP_MAX_PERIOD_NS
) {
48 printk(KERN_ERR
"PCSP: Timer resolution is not sufficient "
49 "(%linS)\n", tp
.tv_nsec
);
50 printk(KERN_ERR
"PCSP: Make sure you have HPET and ACPI "
55 if (loops_per_jiffy
>= PCSP_MIN_LPJ
&& tp
.tv_nsec
<= PCSP_MIN_PERIOD_NS
)
60 printk("PCSP: lpj=%li, min_div=%i, res=%li\n",
61 loops_per_jiffy
, min_div
, tp
.tv_nsec
);
64 div
= MAX_DIV
/ min_div
;
67 pcsp_chip
.max_treble
= min(order
, PCSP_MAX_TREBLE
);
68 pcsp_chip
.treble
= min(pcsp_chip
.max_treble
, PCSP_DEFAULT_TREBLE
);
69 pcsp_chip
.playback_ptr
= 0;
70 pcsp_chip
.period_ptr
= 0;
71 atomic_set(&pcsp_chip
.timer_active
, 0);
75 spin_lock_init(&pcsp_chip
.substream_lock
);
77 pcsp_chip
.card
= card
;
78 pcsp_chip
.port
= 0x61;
83 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, &pcsp_chip
, &ops
);
90 static int __devinit
snd_card_pcsp_probe(int devnum
, struct device
*dev
)
92 struct snd_card
*card
;
98 hrtimer_init(&pcsp_chip
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
99 pcsp_chip
.timer
.cb_mode
= HRTIMER_CB_SOFTIRQ
;
100 pcsp_chip
.timer
.function
= pcsp_do_timer
;
102 card
= snd_card_new(index
, id
, THIS_MODULE
, 0);
106 err
= snd_pcsp_create(card
);
111 err
= snd_pcsp_new_pcm(&pcsp_chip
);
116 err
= snd_pcsp_new_mixer(&pcsp_chip
);
122 snd_card_set_dev(pcsp_chip
.card
, dev
);
124 strcpy(card
->driver
, "PC-Speaker");
125 strcpy(card
->shortname
, "pcsp");
126 sprintf(card
->longname
, "Internal PC-Speaker at port 0x%x",
129 err
= snd_card_register(card
);
138 static int __devinit
alsa_card_pcsp_init(struct device
*dev
)
142 err
= snd_card_pcsp_probe(0, dev
);
144 printk(KERN_ERR
"PC-Speaker initialization failed.\n");
148 #ifdef CONFIG_DEBUG_PAGEALLOC
149 /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
150 printk(KERN_WARNING
"PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, "
151 "which may make the sound noisy.\n");
157 static void __devexit
alsa_card_pcsp_exit(struct snd_pcsp
*chip
)
159 snd_card_free(chip
->card
);
162 static int __devinit
pcsp_probe(struct platform_device
*dev
)
166 err
= pcspkr_input_init(&pcsp_chip
.input_dev
, &dev
->dev
);
170 err
= alsa_card_pcsp_init(&dev
->dev
);
172 pcspkr_input_remove(pcsp_chip
.input_dev
);
176 platform_set_drvdata(dev
, &pcsp_chip
);
180 static int __devexit
pcsp_remove(struct platform_device
*dev
)
182 struct snd_pcsp
*chip
= platform_get_drvdata(dev
);
183 alsa_card_pcsp_exit(chip
);
184 pcspkr_input_remove(chip
->input_dev
);
185 platform_set_drvdata(dev
, NULL
);
189 static void pcsp_stop_beep(struct snd_pcsp
*chip
)
191 spin_lock_irq(&chip
->substream_lock
);
192 if (!chip
->playback_substream
)
194 spin_unlock_irq(&chip
->substream_lock
);
198 static int pcsp_suspend(struct platform_device
*dev
, pm_message_t state
)
200 struct snd_pcsp
*chip
= platform_get_drvdata(dev
);
201 pcsp_stop_beep(chip
);
202 snd_pcm_suspend_all(chip
->pcm
);
206 #define pcsp_suspend NULL
207 #endif /* CONFIG_PM */
209 static void pcsp_shutdown(struct platform_device
*dev
)
211 struct snd_pcsp
*chip
= platform_get_drvdata(dev
);
212 pcsp_stop_beep(chip
);
215 static struct platform_driver pcsp_platform_driver
= {
218 .owner
= THIS_MODULE
,
221 .remove
= __devexit_p(pcsp_remove
),
222 .suspend
= pcsp_suspend
,
223 .shutdown
= pcsp_shutdown
,
226 static int __init
pcsp_init(void)
230 return platform_driver_register(&pcsp_platform_driver
);
233 static void __exit
pcsp_exit(void)
235 platform_driver_unregister(&pcsp_platform_driver
);
238 module_init(pcsp_init
);
239 module_exit(pcsp_exit
);