2 * Driver for PC-speaker like devices found on various Sparc systems.
4 * Copyright (c) 2002 Vojtech Pavlik
5 * Copyright (c) 2002, 2006 David S. Miller (davem@davemloft.net)
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/input.h>
11 #include <linux/platform_device.h>
17 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
18 MODULE_DESCRIPTION("Sparc Speaker beeper driver");
19 MODULE_LICENSE("GPL");
21 struct sparcspkr_state
{
24 int (*event
)(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
);
26 struct input_dev
*input_dev
;
29 static int ebus_spkr_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
31 struct sparcspkr_state
*state
= dev_get_drvdata(dev
->dev
.parent
);
32 unsigned int count
= 0;
39 case SND_BELL
: if (value
) value
= 1000;
44 if (value
> 20 && value
< 32767)
45 count
= 1193182 / value
;
47 spin_lock_irqsave(&state
->lock
, flags
);
49 /* EBUS speaker only has on/off state, the frequency does not
50 * appear to be programmable.
52 if (state
->iobase
& 0x2UL
)
53 outb(!!count
, state
->iobase
);
55 outl(!!count
, state
->iobase
);
57 spin_unlock_irqrestore(&state
->lock
, flags
);
62 static int isa_spkr_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
64 struct sparcspkr_state
*state
= dev_get_drvdata(dev
->dev
.parent
);
65 unsigned int count
= 0;
72 case SND_BELL
: if (value
) value
= 1000;
77 if (value
> 20 && value
< 32767)
78 count
= 1193182 / value
;
80 spin_lock_irqsave(&state
->lock
, flags
);
83 /* enable counter 2 */
84 outb(inb(state
->iobase
+ 0x61) | 3, state
->iobase
+ 0x61);
85 /* set command for counter 2, 2 byte write */
86 outb(0xB6, state
->iobase
+ 0x43);
87 /* select desired HZ */
88 outb(count
& 0xff, state
->iobase
+ 0x42);
89 outb((count
>> 8) & 0xff, state
->iobase
+ 0x42);
91 /* disable counter 2 */
92 outb(inb_p(state
->iobase
+ 0x61) & 0xFC, state
->iobase
+ 0x61);
95 spin_unlock_irqrestore(&state
->lock
, flags
);
100 static int __devinit
sparcspkr_probe(struct device
*dev
)
102 struct sparcspkr_state
*state
= dev_get_drvdata(dev
);
103 struct input_dev
*input_dev
;
106 input_dev
= input_allocate_device();
110 input_dev
->name
= state
->name
;
111 input_dev
->phys
= "sparc/input0";
112 input_dev
->id
.bustype
= BUS_ISA
;
113 input_dev
->id
.vendor
= 0x001f;
114 input_dev
->id
.product
= 0x0001;
115 input_dev
->id
.version
= 0x0100;
116 input_dev
->dev
.parent
= dev
;
118 input_dev
->evbit
[0] = BIT(EV_SND
);
119 input_dev
->sndbit
[0] = BIT(SND_BELL
) | BIT(SND_TONE
);
121 input_dev
->event
= state
->event
;
123 error
= input_register_device(input_dev
);
125 input_free_device(input_dev
);
129 state
->input_dev
= input_dev
;
134 static int __devexit
sparcspkr_remove(struct of_device
*dev
)
136 struct sparcspkr_state
*state
= dev_get_drvdata(&dev
->dev
);
137 struct input_dev
*input_dev
= state
->input_dev
;
139 /* turn off the speaker */
140 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
142 input_unregister_device(input_dev
);
144 dev_set_drvdata(&dev
->dev
, NULL
);
150 static int sparcspkr_shutdown(struct of_device
*dev
)
152 struct sparcspkr_state
*state
= dev_get_drvdata(&dev
->dev
);
153 struct input_dev
*input_dev
= state
->input_dev
;
155 /* turn off the speaker */
156 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
161 static int __devinit
ebus_beep_probe(struct of_device
*dev
, const struct of_device_id
*match
)
163 struct linux_ebus_device
*edev
= to_ebus_device(&dev
->dev
);
164 struct sparcspkr_state
*state
;
167 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
171 state
->name
= "Sparc EBUS Speaker";
172 state
->iobase
= edev
->resource
[0].start
;
173 state
->event
= ebus_spkr_event
;
174 spin_lock_init(&state
->lock
);
176 dev_set_drvdata(&dev
->dev
, state
);
178 err
= sparcspkr_probe(&dev
->dev
);
180 dev_set_drvdata(&dev
->dev
, NULL
);
187 static struct of_device_id ebus_beep_match
[] = {
194 static struct of_platform_driver ebus_beep_driver
= {
196 .match_table
= ebus_beep_match
,
197 .probe
= ebus_beep_probe
,
198 .remove
= sparcspkr_remove
,
199 .shutdown
= sparcspkr_shutdown
,
202 static int __devinit
isa_beep_probe(struct of_device
*dev
, const struct of_device_id
*match
)
204 struct sparc_isa_device
*idev
= to_isa_device(&dev
->dev
);
205 struct sparcspkr_state
*state
;
208 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
212 state
->name
= "Sparc ISA Speaker";
213 state
->iobase
= idev
->resource
.start
;
214 state
->event
= isa_spkr_event
;
215 spin_lock_init(&state
->lock
);
217 dev_set_drvdata(&dev
->dev
, state
);
219 err
= sparcspkr_probe(&dev
->dev
);
221 dev_set_drvdata(&dev
->dev
, NULL
);
228 static struct of_device_id isa_beep_match
[] = {
235 static struct of_platform_driver isa_beep_driver
= {
237 .match_table
= isa_beep_match
,
238 .probe
= isa_beep_probe
,
239 .remove
= sparcspkr_remove
,
240 .shutdown
= sparcspkr_shutdown
,
243 static int __init
sparcspkr_init(void)
245 int err
= of_register_driver(&ebus_beep_driver
, &ebus_bus_type
);
248 err
= of_register_driver(&isa_beep_driver
, &isa_bus_type
);
250 of_unregister_driver(&ebus_beep_driver
);
256 static void __exit
sparcspkr_exit(void)
258 of_unregister_driver(&ebus_beep_driver
);
259 of_unregister_driver(&isa_beep_driver
);
262 module_init(sparcspkr_init
);
263 module_exit(sparcspkr_exit
);