1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for PC-speaker like devices found on various Sparc systems.
5 * Copyright (c) 2002 Vojtech Pavlik
6 * Copyright (c) 2002, 2006, 2008 David S. Miller (davem@davemloft.net)
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/input.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
18 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
19 MODULE_DESCRIPTION("Sparc Speaker beeper driver");
20 MODULE_LICENSE("GPL");
22 struct grover_beep_info
{
23 void __iomem
*freq_regs
;
24 void __iomem
*enable_reg
;
27 struct bbc_beep_info
{
32 struct sparcspkr_state
{
34 int (*event
)(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
);
36 struct input_dev
*input_dev
;
38 struct grover_beep_info grover
;
39 struct bbc_beep_info bbc
;
43 static u32
bbc_count_to_reg(struct bbc_beep_info
*info
, unsigned int count
)
45 u32 val
, clock_freq
= info
->clock_freq
;
51 if (count
<= clock_freq
>> 20)
54 if (count
>= clock_freq
>> 12)
58 for (i
= 19; i
>= 11; i
--) {
60 if (count
<= clock_freq
>> i
)
67 static int bbc_spkr_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
69 struct sparcspkr_state
*state
= dev_get_drvdata(dev
->dev
.parent
);
70 struct bbc_beep_info
*info
= &state
->u
.bbc
;
71 unsigned int count
= 0;
77 case SND_BELL
: if (value
) value
= 1000;
82 if (value
> 20 && value
< 32767)
83 count
= 1193182 / value
;
85 count
= bbc_count_to_reg(info
, count
);
87 guard(spinlock_irqsave
)(&state
->lock
);
90 sbus_writeb(0x01, info
->regs
+ 0);
91 sbus_writeb(0x00, info
->regs
+ 2);
92 sbus_writeb((count
>> 16) & 0xff, info
->regs
+ 3);
93 sbus_writeb((count
>> 8) & 0xff, info
->regs
+ 4);
94 sbus_writeb(0x00, info
->regs
+ 5);
96 sbus_writeb(0x00, info
->regs
+ 0);
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;
112 case SND_BELL
: if (value
) value
= 1000;
113 case SND_TONE
: break;
117 if (value
> 20 && value
< 32767)
118 count
= 1193182 / value
;
120 guard(spinlock_irqsave
)(&state
->lock
);
123 /* enable counter 2 */
124 sbus_writeb(sbus_readb(info
->enable_reg
) | 3, info
->enable_reg
);
125 /* set command for counter 2, 2 byte write */
126 sbus_writeb(0xB6, info
->freq_regs
+ 1);
127 /* select desired HZ */
128 sbus_writeb(count
& 0xff, info
->freq_regs
+ 0);
129 sbus_writeb((count
>> 8) & 0xff, info
->freq_regs
+ 0);
131 /* disable counter 2 */
132 sbus_writeb(sbus_readb(info
->enable_reg
) & 0xFC, info
->enable_reg
);
138 static int sparcspkr_probe(struct device
*dev
)
140 struct sparcspkr_state
*state
= dev_get_drvdata(dev
);
141 struct input_dev
*input_dev
;
144 input_dev
= input_allocate_device();
148 input_dev
->name
= state
->name
;
149 input_dev
->phys
= "sparc/input0";
150 input_dev
->id
.bustype
= BUS_ISA
;
151 input_dev
->id
.vendor
= 0x001f;
152 input_dev
->id
.product
= 0x0001;
153 input_dev
->id
.version
= 0x0100;
154 input_dev
->dev
.parent
= dev
;
156 input_dev
->evbit
[0] = BIT_MASK(EV_SND
);
157 input_dev
->sndbit
[0] = BIT_MASK(SND_BELL
) | BIT_MASK(SND_TONE
);
159 input_dev
->event
= state
->event
;
161 error
= input_register_device(input_dev
);
163 input_free_device(input_dev
);
167 state
->input_dev
= input_dev
;
172 static void sparcspkr_shutdown(struct platform_device
*dev
)
174 struct sparcspkr_state
*state
= platform_get_drvdata(dev
);
175 struct input_dev
*input_dev
= state
->input_dev
;
177 /* turn off the speaker */
178 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
181 static int bbc_beep_probe(struct platform_device
*op
)
183 struct sparcspkr_state
*state
;
184 struct bbc_beep_info
*info
;
187 state
= devm_kzalloc(&op
->dev
, sizeof(*state
), GFP_KERNEL
);
191 state
->name
= "Sparc BBC Speaker";
192 state
->event
= bbc_spkr_event
;
193 spin_lock_init(&state
->lock
);
195 struct device_node
*dp
__free(device_node
) = of_find_node_by_path("/");
199 info
= &state
->u
.bbc
;
200 info
->clock_freq
= of_getintprop_default(dp
, "clock-frequency", 0);
201 if (!info
->clock_freq
)
204 info
->regs
= of_ioremap(&op
->resource
[0], 0, 6, "bbc beep");
208 platform_set_drvdata(op
, state
);
210 err
= sparcspkr_probe(&op
->dev
);
212 of_iounmap(&op
->resource
[0], info
->regs
, 6);
219 static void bbc_remove(struct platform_device
*op
)
221 struct sparcspkr_state
*state
= platform_get_drvdata(op
);
222 struct input_dev
*input_dev
= state
->input_dev
;
223 struct bbc_beep_info
*info
= &state
->u
.bbc
;
225 /* turn off the speaker */
226 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
228 input_unregister_device(input_dev
);
230 of_iounmap(&op
->resource
[0], info
->regs
, 6);
233 static const struct of_device_id bbc_beep_match
[] = {
236 .compatible
= "SUNW,bbc-beep",
240 MODULE_DEVICE_TABLE(of
, bbc_beep_match
);
242 static struct platform_driver bbc_beep_driver
= {
245 .of_match_table
= bbc_beep_match
,
247 .probe
= bbc_beep_probe
,
248 .remove
= bbc_remove
,
249 .shutdown
= sparcspkr_shutdown
,
252 static int grover_beep_probe(struct platform_device
*op
)
254 struct sparcspkr_state
*state
;
255 struct grover_beep_info
*info
;
258 state
= devm_kzalloc(&op
->dev
, sizeof(*state
), GFP_KERNEL
);
262 state
->name
= "Sparc Grover Speaker";
263 state
->event
= grover_spkr_event
;
264 spin_lock_init(&state
->lock
);
266 info
= &state
->u
.grover
;
267 info
->freq_regs
= of_ioremap(&op
->resource
[2], 0, 2, "grover beep freq");
268 if (!info
->freq_regs
)
271 info
->enable_reg
= of_ioremap(&op
->resource
[3], 0, 1, "grover beep enable");
272 if (!info
->enable_reg
)
273 goto out_unmap_freq_regs
;
275 platform_set_drvdata(op
, state
);
277 err
= sparcspkr_probe(&op
->dev
);
279 goto out_clear_drvdata
;
284 of_iounmap(&op
->resource
[3], info
->enable_reg
, 1);
287 of_iounmap(&op
->resource
[2], info
->freq_regs
, 2);
292 static void grover_remove(struct platform_device
*op
)
294 struct sparcspkr_state
*state
= platform_get_drvdata(op
);
295 struct grover_beep_info
*info
= &state
->u
.grover
;
296 struct input_dev
*input_dev
= state
->input_dev
;
298 /* turn off the speaker */
299 state
->event(input_dev
, EV_SND
, SND_BELL
, 0);
301 input_unregister_device(input_dev
);
303 of_iounmap(&op
->resource
[3], info
->enable_reg
, 1);
304 of_iounmap(&op
->resource
[2], info
->freq_regs
, 2);
307 static const struct of_device_id grover_beep_match
[] = {
310 .compatible
= "SUNW,smbus-beep",
314 MODULE_DEVICE_TABLE(of
, grover_beep_match
);
316 static struct platform_driver grover_beep_driver
= {
318 .name
= "groverbeep",
319 .of_match_table
= grover_beep_match
,
321 .probe
= grover_beep_probe
,
322 .remove
= grover_remove
,
323 .shutdown
= sparcspkr_shutdown
,
326 static struct platform_driver
* const drivers
[] = {
331 static int __init
sparcspkr_init(void)
333 return platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
336 static void __exit
sparcspkr_exit(void)
338 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
341 module_init(sparcspkr_init
);
342 module_exit(sparcspkr_exit
);