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/module.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 <linux/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 bool enable
= SNDRV_DEFAULT_ENABLE1
; /* Enable this card */
29 static bool nopcm
; /* Disable PCM capability of the driver */
31 module_param(index
, int, 0444);
32 MODULE_PARM_DESC(index
, "Index value for pcsp soundcard.");
33 module_param(id
, charp
, 0444);
34 MODULE_PARM_DESC(id
, "ID string for pcsp soundcard.");
35 module_param(enable
, bool, 0444);
36 MODULE_PARM_DESC(enable
, "Enable PC-Speaker sound.");
37 module_param(nopcm
, bool, 0444);
38 MODULE_PARM_DESC(nopcm
, "Disable PC-Speaker PCM sound. Only beeps remain.");
40 struct snd_pcsp pcsp_chip
;
42 static int snd_pcsp_create(struct snd_card
*card
)
44 static struct snd_device_ops ops
= { };
45 unsigned int resolution
= hrtimer_resolution
;
46 int err
, div
, min_div
, order
;
49 if (resolution
> PCSP_MAX_PERIOD_NS
) {
50 printk(KERN_ERR
"PCSP: Timer resolution is not sufficient "
51 "(%unS)\n", resolution
);
52 printk(KERN_ERR
"PCSP: Make sure you have HPET and ACPI "
54 printk(KERN_ERR
"PCSP: Turned into nopcm mode.\n");
59 if (loops_per_jiffy
>= PCSP_MIN_LPJ
&& resolution
<= PCSP_MIN_PERIOD_NS
)
64 printk(KERN_DEBUG
"PCSP: lpj=%li, min_div=%i, res=%u\n",
65 loops_per_jiffy
, min_div
, resolution
);
68 div
= MAX_DIV
/ min_div
;
71 pcsp_chip
.max_treble
= min(order
, PCSP_MAX_TREBLE
);
72 pcsp_chip
.treble
= min(pcsp_chip
.max_treble
, PCSP_DEFAULT_TREBLE
);
73 pcsp_chip
.playback_ptr
= 0;
74 pcsp_chip
.period_ptr
= 0;
75 atomic_set(&pcsp_chip
.timer_active
, 0);
79 spin_lock_init(&pcsp_chip
.substream_lock
);
81 pcsp_chip
.card
= card
;
82 pcsp_chip
.port
= 0x61;
87 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, &pcsp_chip
, &ops
);
94 static int snd_card_pcsp_probe(int devnum
, struct device
*dev
)
96 struct snd_card
*card
;
102 hrtimer_init(&pcsp_chip
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
103 pcsp_chip
.timer
.function
= pcsp_do_timer
;
105 err
= snd_card_new(dev
, index
, id
, THIS_MODULE
, 0, &card
);
109 err
= snd_pcsp_create(card
);
115 err
= snd_pcsp_new_pcm(&pcsp_chip
);
121 err
= snd_pcsp_new_mixer(&pcsp_chip
, nopcm
);
127 strcpy(card
->driver
, "PC-Speaker");
128 strcpy(card
->shortname
, "pcsp");
129 sprintf(card
->longname
, "Internal PC-Speaker at port 0x%x",
132 err
= snd_card_register(card
);
141 static int alsa_card_pcsp_init(struct device
*dev
)
145 err
= snd_card_pcsp_probe(0, dev
);
147 printk(KERN_ERR
"PC-Speaker initialization failed.\n");
151 #ifdef CONFIG_DEBUG_PAGEALLOC
152 /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
153 printk(KERN_WARNING
"PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, "
154 "which may make the sound noisy.\n");
160 static void alsa_card_pcsp_exit(struct snd_pcsp
*chip
)
162 snd_card_free(chip
->card
);
165 static int pcsp_probe(struct platform_device
*dev
)
169 err
= pcspkr_input_init(&pcsp_chip
.input_dev
, &dev
->dev
);
173 err
= alsa_card_pcsp_init(&dev
->dev
);
175 pcspkr_input_remove(pcsp_chip
.input_dev
);
179 platform_set_drvdata(dev
, &pcsp_chip
);
183 static int pcsp_remove(struct platform_device
*dev
)
185 struct snd_pcsp
*chip
= platform_get_drvdata(dev
);
186 pcspkr_input_remove(chip
->input_dev
);
187 alsa_card_pcsp_exit(chip
);
191 static void pcsp_stop_beep(struct snd_pcsp
*chip
)
193 pcsp_sync_stop(chip
);
197 #ifdef CONFIG_PM_SLEEP
198 static int pcsp_suspend(struct device
*dev
)
200 struct snd_pcsp
*chip
= dev_get_drvdata(dev
);
201 pcsp_stop_beep(chip
);
202 snd_pcm_suspend_all(chip
->pcm
);
206 static SIMPLE_DEV_PM_OPS(pcsp_pm
, pcsp_suspend
, NULL
);
207 #define PCSP_PM_OPS &pcsp_pm
209 #define PCSP_PM_OPS NULL
210 #endif /* CONFIG_PM_SLEEP */
212 static void pcsp_shutdown(struct platform_device
*dev
)
214 struct snd_pcsp
*chip
= platform_get_drvdata(dev
);
215 pcsp_stop_beep(chip
);
218 static struct platform_driver pcsp_platform_driver
= {
224 .remove
= pcsp_remove
,
225 .shutdown
= pcsp_shutdown
,
228 static int __init
pcsp_init(void)
232 return platform_driver_register(&pcsp_platform_driver
);
235 static void __exit
pcsp_exit(void)
237 platform_driver_unregister(&pcsp_platform_driver
);
240 module_init(pcsp_init
);
241 module_exit(pcsp_exit
);