1/*
2 *  PC Speaker beeper driver for Linux
3 *
4 *  Copyright (c) 2002 Vojtech Pavlik
5 *  Copyright (c) 1992 Orest Zborowski
6 *
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/input.h>
19#include <linux/platform_device.h>
20#include <asm/8253pit.h>
21#include <asm/io.h>
22
23MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
24MODULE_DESCRIPTION("PC Speaker beeper driver");
25MODULE_LICENSE("GPL");
26
27static DEFINE_SPINLOCK(i8253_beep_lock);
28
29static int pcspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
30{
31	unsigned int count = 0;
32	unsigned long flags;
33
34	if (type != EV_SND)
35		return -1;
36
37	switch (code) {
38		case SND_BELL: if (value) value = 1000;
39		case SND_TONE: break;
40		default: return -1;
41	}
42
43	if (value > 20 && value < 32767)
44		count = PIT_TICK_RATE / value;
45
46	spin_lock_irqsave(&i8253_beep_lock, flags);
47
48	if (count) {
49		/* enable counter 2 */
50		outb_p(inb_p(0x61) | 3, 0x61);
51		/* set command for counter 2, 2 byte write */
52		outb_p(0xB6, 0x43);
53		/* select desired HZ */
54		outb_p(count & 0xff, 0x42);
55		outb((count >> 8) & 0xff, 0x42);
56	} else {
57		/* disable counter 2 */
58		outb(inb_p(0x61) & 0xFC, 0x61);
59	}
60
61	spin_unlock_irqrestore(&i8253_beep_lock, flags);
62
63	return 0;
64}
65
66static int __devinit pcspkr_probe(struct platform_device *dev)
67{
68	struct input_dev *pcspkr_dev;
69	int err;
70
71	pcspkr_dev = input_allocate_device();
72	if (!pcspkr_dev)
73		return -ENOMEM;
74
75	pcspkr_dev->name = "PC Speaker";
76	pcspkr_dev->phys = "isa0061/input0";
77	pcspkr_dev->id.bustype = BUS_ISA;
78	pcspkr_dev->id.vendor = 0x001f;
79	pcspkr_dev->id.product = 0x0001;
80	pcspkr_dev->id.version = 0x0100;
81	pcspkr_dev->dev.parent = &dev->dev;
82
83	pcspkr_dev->evbit[0] = BIT(EV_SND);
84	pcspkr_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
85	pcspkr_dev->event = pcspkr_event;
86
87	err = input_register_device(pcspkr_dev);
88	if (err) {
89		input_free_device(pcspkr_dev);
90		return err;
91	}
92
93	platform_set_drvdata(dev, pcspkr_dev);
94
95	return 0;
96}
97
98static int __devexit pcspkr_remove(struct platform_device *dev)
99{
100	struct input_dev *pcspkr_dev = platform_get_drvdata(dev);
101
102	input_unregister_device(pcspkr_dev);
103	platform_set_drvdata(dev, NULL);
104	/* turn off the speaker */
105	pcspkr_event(NULL, EV_SND, SND_BELL, 0);
106
107	return 0;
108}
109
110static int pcspkr_suspend(struct platform_device *dev, pm_message_t state)
111{
112	pcspkr_event(NULL, EV_SND, SND_BELL, 0);
113
114	return 0;
115}
116
117static void pcspkr_shutdown(struct platform_device *dev)
118{
119	/* turn off the speaker */
120	pcspkr_event(NULL, EV_SND, SND_BELL, 0);
121}
122
123static struct platform_driver pcspkr_platform_driver = {
124	.driver		= {
125		.name	= "pcspkr",
126		.owner	= THIS_MODULE,
127	},
128	.probe		= pcspkr_probe,
129	.remove		= __devexit_p(pcspkr_remove),
130	.suspend	= pcspkr_suspend,
131	.shutdown	= pcspkr_shutdown,
132};
133
134
135static int __init pcspkr_init(void)
136{
137	return platform_driver_register(&pcspkr_platform_driver);
138}
139
140static void __exit pcspkr_exit(void)
141{
142	platform_driver_unregister(&pcspkr_platform_driver);
143}
144
145module_init(pcspkr_init);
146module_exit(pcspkr_exit);
147