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>
12 #include <linux/slab.h>
16 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
17 MODULE_DESCRIPTION("Sparc Speaker beeper driver");
18 MODULE_LICENSE("GPL");
20 struct grover_beep_info
{
21 void __iomem
*freq_regs
;
22 void __iomem
*enable_reg
;
25 struct bbc_beep_info
{
30 struct sparcspkr_state
{
32 int (*event
)(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
);
34 struct input_dev
*input_dev
;
36 struct grover_beep_info grover
;
37 struct bbc_beep_info bbc
;
41 static u32
bbc_count_to_reg(struct bbc_beep_info
*info
, unsigned int count
)
43 u32 val
, clock_freq
= info
->clock_freq
;
49 if (count
<= clock_freq
>> 20)
52 if (count
>= clock_freq
>> 12)
56 for (i
= 19; i
>= 11; i
--) {
58 if (count
<= clock_freq
>> i
)
65 static int bbc_spkr_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
67 struct sparcspkr_state
*state
= dev_get_drvdata(dev
->dev
.parent
);
68 struct bbc_beep_info
*info
= &state
->u
.bbc
;
69 unsigned int count
= 0;
76 case SND_BELL
: if (value
) value
= 1000;
81 if (value
> 20 && value
< 32767)
82 count
= 1193182 / value
;
84 count
= bbc_count_to_reg(info
, count
);
86 spin_lock_irqsave(&state
->lock
, flags
);
89 sbus_writeb(0x01, info
->regs
+ 0);
90 sbus_writeb(0x00, info
->regs
+ 2);
91 sbus_writeb((count
>> 16) & 0xff, info
->regs
+ 3);
92 sbus_writeb((count
>> 8) & 0xff, info
->regs
+ 4);
93 sbus_writeb(0x00, info
->regs
+ 5);
95 sbus_writeb(0x00, info
->regs
+ 0);
98 spin_unlock_irqrestore(&state
->lock
, flags
);
103 static int grover_spkr_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
105 struct sparcspkr_state
*state
= dev_get_drvdata(dev
->dev
.parent
);
106 struct grover_beep_info
*info
= &state
->u
.grover
;
107 unsigned int count
= 0;
114 case SND_BELL
: if (value
) value
= 1000;
115 case SND_TONE
: break;
119 if (value
> 20 && value
< 32767)
120 count
= 1193182 / value
;
122 spin_lock_irqsave(&state
->lock
, flags
);
125 /* enable counter 2 */
126 sbus_writeb(sbus_readb(info
->enable_reg
) | 3, info
->enable_reg
);
127 /* set command for counter 2, 2 byte write */
128 sbus_writeb(0xB6, info
->freq_regs
+ 1);
129 /* select desired HZ */
130 sbus_writeb(count
& 0xff, info
->freq_regs
+ 0);
131 sbus_writeb((count
>> 8) & 0xff, info
->freq_regs
+ 0);
133 /* disable counter 2 */
134 sbus_writeb(sbus_readb(info
->enable_reg
) & 0xFC, info
->enable_reg
);
137 spin_unlock_irqrestore(&state
->lock
, flags
);
142 static int sparcspkr_probe(struct device
*dev
)
144 struct sparcspkr_state
*state
= dev_get_drvdata(dev
);
145 struct input_dev
*input_dev
;
148 input_dev
= input_allocate_device();
152 input_dev
->name
= state
->name
;
153 input_dev
->phys
= "sparc/input0";
154 input_dev
->id
.bustype
= BUS_ISA
;
155 input_dev
->id
.vendor
= 0x001f;
156 input_dev
->id
.product
= 0x0001;
157 input_dev
->id
.version
= 0x0100;
158 input_dev
->dev
.parent
= dev
;
160 input_dev
->evbit
[0] = BIT_MASK(EV_SND
);
161 input_dev
->sndbit
[0] = BIT_MASK(SND_BELL
) | BIT_MASK(SND_TONE
);
163 input_dev
->event
= state
->event
;
165 error
= input_register_device(input_dev
);
167 input_free_device(input_dev
);
171 state
->input_dev
= input_dev
;
176 static void sparcspkr_shutdown(struct platform_device
*dev
)
178 struct sparcspkr_state
*state
= platform_get_drvdata(dev
);
179 struct input_dev
*input_dev
= state
->input_dev
;
181 /* turn off the speaker */
182 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
185 static int bbc_beep_probe(struct platform_device
*op
)
187 struct sparcspkr_state
*state
;
188 struct bbc_beep_info
*info
;
189 struct device_node
*dp
;
192 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
196 state
->name
= "Sparc BBC Speaker";
197 state
->event
= bbc_spkr_event
;
198 spin_lock_init(&state
->lock
);
200 dp
= of_find_node_by_path("/");
205 info
= &state
->u
.bbc
;
206 info
->clock_freq
= of_getintprop_default(dp
, "clock-frequency", 0);
207 if (!info
->clock_freq
)
210 info
->regs
= of_ioremap(&op
->resource
[0], 0, 6, "bbc beep");
214 platform_set_drvdata(op
, state
);
216 err
= sparcspkr_probe(&op
->dev
);
218 goto out_clear_drvdata
;
223 of_iounmap(&op
->resource
[0], info
->regs
, 6);
231 static int bbc_remove(struct platform_device
*op
)
233 struct sparcspkr_state
*state
= platform_get_drvdata(op
);
234 struct input_dev
*input_dev
= state
->input_dev
;
235 struct bbc_beep_info
*info
= &state
->u
.bbc
;
237 /* turn off the speaker */
238 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
240 input_unregister_device(input_dev
);
242 of_iounmap(&op
->resource
[0], info
->regs
, 6);
249 static const struct of_device_id bbc_beep_match
[] = {
252 .compatible
= "SUNW,bbc-beep",
256 MODULE_DEVICE_TABLE(of
, bbc_beep_match
);
258 static struct platform_driver bbc_beep_driver
= {
261 .of_match_table
= bbc_beep_match
,
263 .probe
= bbc_beep_probe
,
264 .remove
= bbc_remove
,
265 .shutdown
= sparcspkr_shutdown
,
268 static int grover_beep_probe(struct platform_device
*op
)
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 platform_set_drvdata(op
, state
);
293 err
= sparcspkr_probe(&op
->dev
);
295 goto out_clear_drvdata
;
300 of_iounmap(&op
->resource
[3], info
->enable_reg
, 1);
303 of_iounmap(&op
->resource
[2], info
->freq_regs
, 2);
310 static int grover_remove(struct platform_device
*op
)
312 struct sparcspkr_state
*state
= platform_get_drvdata(op
);
313 struct grover_beep_info
*info
= &state
->u
.grover
;
314 struct input_dev
*input_dev
= state
->input_dev
;
316 /* turn off the speaker */
317 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
319 input_unregister_device(input_dev
);
321 of_iounmap(&op
->resource
[3], info
->enable_reg
, 1);
322 of_iounmap(&op
->resource
[2], info
->freq_regs
, 2);
329 static const struct of_device_id grover_beep_match
[] = {
332 .compatible
= "SUNW,smbus-beep",
336 MODULE_DEVICE_TABLE(of
, grover_beep_match
);
338 static struct platform_driver grover_beep_driver
= {
340 .name
= "groverbeep",
341 .of_match_table
= grover_beep_match
,
343 .probe
= grover_beep_probe
,
344 .remove
= grover_remove
,
345 .shutdown
= sparcspkr_shutdown
,
348 static int __init
sparcspkr_init(void)
350 int err
= platform_driver_register(&bbc_beep_driver
);
353 err
= platform_driver_register(&grover_beep_driver
);
355 platform_driver_unregister(&bbc_beep_driver
);
361 static void __exit
sparcspkr_exit(void)
363 platform_driver_unregister(&bbc_beep_driver
);
364 platform_driver_unregister(&grover_beep_driver
);
367 module_init(sparcspkr_init
);
368 module_exit(sparcspkr_exit
);