2 * Driver for PC-speaker like devices found on various Sparc systems.
4 * Copyright (c) 2002 Vojtech Pavlik
5 * Copyright (c) 2002, 2006, 2008 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/of_device.h>
15 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
16 MODULE_DESCRIPTION("Sparc Speaker beeper driver");
17 MODULE_LICENSE("GPL");
19 struct grover_beep_info
{
20 void __iomem
*freq_regs
;
21 void __iomem
*enable_reg
;
24 struct bbc_beep_info
{
29 struct sparcspkr_state
{
31 int (*event
)(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
);
33 struct input_dev
*input_dev
;
35 struct grover_beep_info grover
;
36 struct bbc_beep_info bbc
;
40 static u32
bbc_count_to_reg(struct bbc_beep_info
*info
, unsigned int count
)
42 u32 val
, clock_freq
= info
->clock_freq
;
48 if (count
<= clock_freq
>> 20)
51 if (count
>= clock_freq
>> 12)
55 for (i
= 19; i
>= 11; i
--) {
57 if (count
<= clock_freq
>> i
)
64 static int bbc_spkr_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
66 struct sparcspkr_state
*state
= dev_get_drvdata(dev
->dev
.parent
);
67 struct bbc_beep_info
*info
= &state
->u
.bbc
;
68 unsigned int count
= 0;
75 case SND_BELL
: if (value
) value
= 1000;
80 if (value
> 20 && value
< 32767)
81 count
= 1193182 / value
;
83 count
= bbc_count_to_reg(info
, count
);
85 spin_lock_irqsave(&state
->lock
, flags
);
88 outb(0x01, info
->regs
+ 0);
89 outb(0x00, info
->regs
+ 2);
90 outb((count
>> 16) & 0xff, info
->regs
+ 3);
91 outb((count
>> 8) & 0xff, info
->regs
+ 4);
92 outb(0x00, info
->regs
+ 5);
94 outb(0x00, info
->regs
+ 0);
97 spin_unlock_irqrestore(&state
->lock
, flags
);
102 static int grover_spkr_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
104 struct sparcspkr_state
*state
= dev_get_drvdata(dev
->dev
.parent
);
105 struct grover_beep_info
*info
= &state
->u
.grover
;
106 unsigned int count
= 0;
113 case SND_BELL
: if (value
) value
= 1000;
114 case SND_TONE
: break;
118 if (value
> 20 && value
< 32767)
119 count
= 1193182 / value
;
121 spin_lock_irqsave(&state
->lock
, flags
);
124 /* enable counter 2 */
125 outb(inb(info
->enable_reg
) | 3, info
->enable_reg
);
126 /* set command for counter 2, 2 byte write */
127 outb(0xB6, info
->freq_regs
+ 1);
128 /* select desired HZ */
129 outb(count
& 0xff, info
->freq_regs
+ 0);
130 outb((count
>> 8) & 0xff, info
->freq_regs
+ 0);
132 /* disable counter 2 */
133 outb(inb_p(info
->enable_reg
) & 0xFC, info
->enable_reg
);
136 spin_unlock_irqrestore(&state
->lock
, flags
);
141 static int __devinit
sparcspkr_probe(struct device
*dev
)
143 struct sparcspkr_state
*state
= dev_get_drvdata(dev
);
144 struct input_dev
*input_dev
;
147 input_dev
= input_allocate_device();
151 input_dev
->name
= state
->name
;
152 input_dev
->phys
= "sparc/input0";
153 input_dev
->id
.bustype
= BUS_ISA
;
154 input_dev
->id
.vendor
= 0x001f;
155 input_dev
->id
.product
= 0x0001;
156 input_dev
->id
.version
= 0x0100;
157 input_dev
->dev
.parent
= dev
;
159 input_dev
->evbit
[0] = BIT_MASK(EV_SND
);
160 input_dev
->sndbit
[0] = BIT_MASK(SND_BELL
) | BIT_MASK(SND_TONE
);
162 input_dev
->event
= state
->event
;
164 error
= input_register_device(input_dev
);
166 input_free_device(input_dev
);
170 state
->input_dev
= input_dev
;
175 static int sparcspkr_shutdown(struct of_device
*dev
)
177 struct sparcspkr_state
*state
= dev_get_drvdata(&dev
->dev
);
178 struct input_dev
*input_dev
= state
->input_dev
;
180 /* turn off the speaker */
181 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
186 static int __devinit
bbc_beep_probe(struct of_device
*op
, const struct of_device_id
*match
)
188 struct sparcspkr_state
*state
;
189 struct bbc_beep_info
*info
;
190 struct device_node
*dp
;
193 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
197 state
->name
= "Sparc BBC Speaker";
198 state
->event
= bbc_spkr_event
;
199 spin_lock_init(&state
->lock
);
201 dp
= of_find_node_by_path("/");
206 info
= &state
->u
.bbc
;
207 info
->clock_freq
= of_getintprop_default(dp
, "clock-frequency", 0);
208 if (!info
->clock_freq
)
211 info
->regs
= of_ioremap(&op
->resource
[0], 0, 6, "bbc beep");
215 dev_set_drvdata(&op
->dev
, state
);
217 err
= sparcspkr_probe(&op
->dev
);
219 goto out_clear_drvdata
;
224 dev_set_drvdata(&op
->dev
, NULL
);
225 of_iounmap(&op
->resource
[0], info
->regs
, 6);
233 static int bbc_remove(struct of_device
*op
)
235 struct sparcspkr_state
*state
= dev_get_drvdata(&op
->dev
);
236 struct input_dev
*input_dev
= state
->input_dev
;
237 struct bbc_beep_info
*info
= &state
->u
.bbc
;
239 /* turn off the speaker */
240 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
242 input_unregister_device(input_dev
);
244 of_iounmap(&op
->resource
[0], info
->regs
, 6);
246 dev_set_drvdata(&op
->dev
, NULL
);
252 static const struct of_device_id bbc_beep_match
[] = {
255 .compatible
= "SUNW,bbc-beep",
260 static struct of_platform_driver bbc_beep_driver
= {
262 .match_table
= bbc_beep_match
,
263 .probe
= bbc_beep_probe
,
264 .remove
= __devexit_p(bbc_remove
),
265 .shutdown
= sparcspkr_shutdown
,
268 static int __devinit
grover_beep_probe(struct of_device
*op
, const struct of_device_id
*match
)
270 struct sparcspkr_state
*state
;
271 struct grover_beep_info
*info
;
274 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
278 state
->name
= "Sparc Grover Speaker";
279 state
->event
= grover_spkr_event
;
280 spin_lock_init(&state
->lock
);
282 info
= &state
->u
.grover
;
283 info
->freq_regs
= of_ioremap(&op
->resource
[2], 0, 2, "grover beep freq");
284 if (!info
->freq_regs
)
287 info
->enable_reg
= of_ioremap(&op
->resource
[3], 0, 1, "grover beep enable");
288 if (!info
->enable_reg
)
289 goto out_unmap_freq_regs
;
291 dev_set_drvdata(&op
->dev
, state
);
293 err
= sparcspkr_probe(&op
->dev
);
295 goto out_clear_drvdata
;
300 dev_set_drvdata(&op
->dev
, NULL
);
301 of_iounmap(&op
->resource
[3], info
->enable_reg
, 1);
304 of_iounmap(&op
->resource
[2], info
->freq_regs
, 2);
311 static int grover_remove(struct of_device
*op
)
313 struct sparcspkr_state
*state
= dev_get_drvdata(&op
->dev
);
314 struct grover_beep_info
*info
= &state
->u
.grover
;
315 struct input_dev
*input_dev
= state
->input_dev
;
317 /* turn off the speaker */
318 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
320 input_unregister_device(input_dev
);
322 of_iounmap(&op
->resource
[3], info
->enable_reg
, 1);
323 of_iounmap(&op
->resource
[2], info
->freq_regs
, 2);
325 dev_set_drvdata(&op
->dev
, NULL
);
331 static const struct of_device_id grover_beep_match
[] = {
334 .compatible
= "SUNW,smbus-beep",
339 static struct of_platform_driver grover_beep_driver
= {
340 .name
= "groverbeep",
341 .match_table
= grover_beep_match
,
342 .probe
= grover_beep_probe
,
343 .remove
= __devexit_p(grover_remove
),
344 .shutdown
= sparcspkr_shutdown
,
347 static int __init
sparcspkr_init(void)
349 int err
= of_register_driver(&bbc_beep_driver
,
350 &of_platform_bus_type
);
353 err
= of_register_driver(&grover_beep_driver
,
354 &of_platform_bus_type
);
356 of_unregister_driver(&bbc_beep_driver
);
362 static void __exit
sparcspkr_exit(void)
364 of_unregister_driver(&bbc_beep_driver
);
365 of_unregister_driver(&grover_beep_driver
);
368 module_init(sparcspkr_init
);
369 module_exit(sparcspkr_exit
);