xref: /linux/sound/drivers/pcsp/pcsp.c (revision a91605b86a234b01d2f99b38411ac30a40bdf067)
1 /*
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>
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 <asm/bitops.h>
16 #include "pcsp_input.h"
17 #include "pcsp.h"
18 
19 MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>");
20 MODULE_DESCRIPTION("PC-Speaker driver");
21 MODULE_LICENSE("GPL");
22 MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}");
23 MODULE_ALIAS("platform:pcspkr");
24 
25 static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
26 static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
27 static int enable = SNDRV_DEFAULT_ENABLE1;	/* Enable this card */
28 
29 module_param(index, int, 0444);
30 MODULE_PARM_DESC(index, "Index value for pcsp soundcard.");
31 module_param(id, charp, 0444);
32 MODULE_PARM_DESC(id, "ID string for pcsp soundcard.");
33 module_param(enable, bool, 0444);
34 MODULE_PARM_DESC(enable, "dummy");
35 
36 struct snd_pcsp pcsp_chip;
37 
38 static int __devinit snd_pcsp_create(struct snd_card *card)
39 {
40 	static struct snd_device_ops ops = { };
41 	struct timespec tp;
42 	int err;
43 	int div, min_div, order;
44 
45 	hrtimer_get_res(CLOCK_MONOTONIC, &tp);
46 	if (tp.tv_sec || tp.tv_nsec > PCSP_MAX_PERIOD_NS) {
47 		printk(KERN_ERR "PCSP: Timer resolution is not sufficient "
48 		       "(%linS)\n", tp.tv_nsec);
49 		printk(KERN_ERR "PCSP: Make sure you have HPET and ACPI "
50 		       "enabled.\n");
51 		return -EIO;
52 	}
53 
54 	if (loops_per_jiffy >= PCSP_MIN_LPJ && tp.tv_nsec <= PCSP_MIN_PERIOD_NS)
55 		min_div = MIN_DIV;
56 	else
57 		min_div = MAX_DIV;
58 #if PCSP_DEBUG
59 	printk("PCSP: lpj=%li, min_div=%i, res=%li\n",
60 	       loops_per_jiffy, min_div, tp.tv_nsec);
61 #endif
62 
63 	div = MAX_DIV / min_div;
64 	order = fls(div) - 1;
65 
66 	pcsp_chip.max_treble = min(order, PCSP_MAX_TREBLE);
67 	pcsp_chip.treble = min(pcsp_chip.max_treble, PCSP_DEFAULT_TREBLE);
68 	pcsp_chip.playback_ptr = 0;
69 	pcsp_chip.period_ptr = 0;
70 	atomic_set(&pcsp_chip.timer_active, 0);
71 	pcsp_chip.enable = 1;
72 	pcsp_chip.pcspkr = 1;
73 
74 	spin_lock_init(&pcsp_chip.substream_lock);
75 
76 	pcsp_chip.card = card;
77 	pcsp_chip.port = 0x61;
78 	pcsp_chip.irq = -1;
79 	pcsp_chip.dma = -1;
80 
81 	/* Register device */
82 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, &pcsp_chip, &ops);
83 	if (err < 0)
84 		return err;
85 
86 	return 0;
87 }
88 
89 static int __devinit snd_card_pcsp_probe(int devnum, struct device *dev)
90 {
91 	struct snd_card *card;
92 	int err;
93 
94 	if (devnum != 0)
95 		return -EINVAL;
96 
97 	hrtimer_init(&pcsp_chip.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
98 	pcsp_chip.timer.cb_mode = HRTIMER_CB_IRQSAFE;
99 	pcsp_chip.timer.function = pcsp_do_timer;
100 
101 	card = snd_card_new(index, id, THIS_MODULE, 0);
102 	if (!card)
103 		return -ENOMEM;
104 
105 	err = snd_pcsp_create(card);
106 	if (err < 0) {
107 		snd_card_free(card);
108 		return err;
109 	}
110 	err = snd_pcsp_new_pcm(&pcsp_chip);
111 	if (err < 0) {
112 		snd_card_free(card);
113 		return err;
114 	}
115 	err = snd_pcsp_new_mixer(&pcsp_chip);
116 	if (err < 0) {
117 		snd_card_free(card);
118 		return err;
119 	}
120 
121 	snd_card_set_dev(pcsp_chip.card, dev);
122 
123 	strcpy(card->driver, "PC-Speaker");
124 	strcpy(card->shortname, "pcsp");
125 	sprintf(card->longname, "Internal PC-Speaker at port 0x%x",
126 		pcsp_chip.port);
127 
128 	err = snd_card_register(card);
129 	if (err < 0) {
130 		snd_card_free(card);
131 		return err;
132 	}
133 
134 	return 0;
135 }
136 
137 static int __devinit alsa_card_pcsp_init(struct device *dev)
138 {
139 	int devnum = 0, cards = 0;
140 
141 #ifdef CONFIG_DEBUG_PAGEALLOC
142 	/* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
143 	printk(KERN_WARNING
144 	       "PCSP: Warning, CONFIG_DEBUG_PAGEALLOC is enabled!\n"
145 	       "You have to disable it if you want to use the PC-Speaker "
146 	       "driver.\n"
147 	       "Unless it is disabled, enjoy the horrible, distorted "
148 	       "and crackling noise.\n");
149 #endif
150 
151 	if (enable) {
152 		if (snd_card_pcsp_probe(devnum, dev) >= 0)
153 			cards++;
154 		if (!cards) {
155 			printk(KERN_ERR "PC-Speaker initialization failed.\n");
156 			return -ENODEV;
157 		}
158 	}
159 
160 	return 0;
161 }
162 
163 static void __devexit alsa_card_pcsp_exit(struct snd_pcsp *chip)
164 {
165 	snd_card_free(chip->card);
166 }
167 
168 static int __devinit pcsp_probe(struct platform_device *dev)
169 {
170 	int err;
171 	err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev);
172 	if (err < 0)
173 		return err;
174 
175 	err = alsa_card_pcsp_init(&dev->dev);
176 	if (err < 0) {
177 		pcspkr_input_remove(pcsp_chip.input_dev);
178 		return err;
179 	}
180 
181 	platform_set_drvdata(dev, &pcsp_chip);
182 	return 0;
183 }
184 
185 static int __devexit pcsp_remove(struct platform_device *dev)
186 {
187 	struct snd_pcsp *chip = platform_get_drvdata(dev);
188 	alsa_card_pcsp_exit(chip);
189 	pcspkr_input_remove(chip->input_dev);
190 	platform_set_drvdata(dev, NULL);
191 	return 0;
192 }
193 
194 static void pcsp_stop_beep(struct snd_pcsp *chip)
195 {
196 	unsigned long flags;
197 	spin_lock_irqsave(&chip->substream_lock, flags);
198 	if (!chip->playback_substream)
199 		pcspkr_stop_sound();
200 	spin_unlock_irqrestore(&chip->substream_lock, flags);
201 }
202 
203 static int pcsp_suspend(struct platform_device *dev, pm_message_t state)
204 {
205 	struct snd_pcsp *chip = platform_get_drvdata(dev);
206 	pcsp_stop_beep(chip);
207 	snd_pcm_suspend_all(chip->pcm);
208 	return 0;
209 }
210 
211 static void pcsp_shutdown(struct platform_device *dev)
212 {
213 	struct snd_pcsp *chip = platform_get_drvdata(dev);
214 	pcsp_stop_beep(chip);
215 }
216 
217 static struct platform_driver pcsp_platform_driver = {
218 	.driver		= {
219 		.name	= "pcspkr",
220 		.owner	= THIS_MODULE,
221 	},
222 	.probe		= pcsp_probe,
223 	.remove		= __devexit_p(pcsp_remove),
224 	.suspend	= pcsp_suspend,
225 	.shutdown	= pcsp_shutdown,
226 };
227 
228 static int __init pcsp_init(void)
229 {
230 	return platform_driver_register(&pcsp_platform_driver);
231 }
232 
233 static void __exit pcsp_exit(void)
234 {
235 	platform_driver_unregister(&pcsp_platform_driver);
236 }
237 
238 module_init(pcsp_init);
239 module_exit(pcsp_exit);
240