1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  m68k beeper driver for Linux
4  *
5  *  Copyright (c) 2002 Richard Zidlicky
6  *  Copyright (c) 2002 Vojtech Pavlik
7  *  Copyright (c) 1992 Orest Zborowski
8  */
9 
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/input.h>
15 #include <linux/platform_device.h>
16 #include <asm/machdep.h>
17 #include <asm/io.h>
18 
19 MODULE_AUTHOR("Richard Zidlicky <rz@linux-m68k.org>");
20 MODULE_DESCRIPTION("m68k beeper driver");
21 MODULE_LICENSE("GPL");
22 
23 static struct platform_device *m68kspkr_platform_device;
24 
m68kspkr_event(struct input_dev * dev,unsigned int type,unsigned int code,int value)25 static int m68kspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
26 {
27 	unsigned int count = 0;
28 
29 	if (type != EV_SND)
30 		return -1;
31 
32 	switch (code) {
33 		case SND_BELL: if (value) value = 1000;
34 		case SND_TONE: break;
35 		default: return -1;
36 	}
37 
38 	if (value > 20 && value < 32767)
39 		count = 1193182 / value;
40 
41 	mach_beep(count, -1);
42 
43 	return 0;
44 }
45 
m68kspkr_probe(struct platform_device * dev)46 static int m68kspkr_probe(struct platform_device *dev)
47 {
48 	struct input_dev *input_dev;
49 	int err;
50 
51 	input_dev = input_allocate_device();
52 	if (!input_dev)
53 		return -ENOMEM;
54 
55 	input_dev->name = "m68k beeper";
56 	input_dev->phys = "m68k/generic";
57 	input_dev->id.bustype = BUS_HOST;
58 	input_dev->id.vendor  = 0x001f;
59 	input_dev->id.product = 0x0001;
60 	input_dev->id.version = 0x0100;
61 	input_dev->dev.parent = &dev->dev;
62 
63 	input_dev->evbit[0] = BIT_MASK(EV_SND);
64 	input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
65 	input_dev->event = m68kspkr_event;
66 
67 	err = input_register_device(input_dev);
68 	if (err) {
69 		input_free_device(input_dev);
70 		return err;
71 	}
72 
73 	platform_set_drvdata(dev, input_dev);
74 
75 	return 0;
76 }
77 
m68kspkr_remove(struct platform_device * dev)78 static int m68kspkr_remove(struct platform_device *dev)
79 {
80 	struct input_dev *input_dev = platform_get_drvdata(dev);
81 
82 	input_unregister_device(input_dev);
83 	/* turn off the speaker */
84 	m68kspkr_event(NULL, EV_SND, SND_BELL, 0);
85 
86 	return 0;
87 }
88 
m68kspkr_shutdown(struct platform_device * dev)89 static void m68kspkr_shutdown(struct platform_device *dev)
90 {
91 	/* turn off the speaker */
92 	m68kspkr_event(NULL, EV_SND, SND_BELL, 0);
93 }
94 
95 static struct platform_driver m68kspkr_platform_driver = {
96 	.driver		= {
97 		.name	= "m68kspkr",
98 	},
99 	.probe		= m68kspkr_probe,
100 	.remove		= m68kspkr_remove,
101 	.shutdown	= m68kspkr_shutdown,
102 };
103 
m68kspkr_init(void)104 static int __init m68kspkr_init(void)
105 {
106 	int err;
107 
108 	if (!mach_beep) {
109 		printk(KERN_INFO "m68kspkr: no lowlevel beep support\n");
110 		return -ENODEV;
111         }
112 
113 	err = platform_driver_register(&m68kspkr_platform_driver);
114 	if (err)
115 		return err;
116 
117 	m68kspkr_platform_device = platform_device_alloc("m68kspkr", -1);
118 	if (!m68kspkr_platform_device) {
119 		err = -ENOMEM;
120 		goto err_unregister_driver;
121 	}
122 
123 	err = platform_device_add(m68kspkr_platform_device);
124 	if (err)
125 		goto err_free_device;
126 
127 	return 0;
128 
129  err_free_device:
130 	platform_device_put(m68kspkr_platform_device);
131  err_unregister_driver:
132 	platform_driver_unregister(&m68kspkr_platform_driver);
133 
134 	return err;
135 }
136 
m68kspkr_exit(void)137 static void __exit m68kspkr_exit(void)
138 {
139 	platform_device_unregister(m68kspkr_platform_device);
140 	platform_driver_unregister(&m68kspkr_platform_driver);
141 }
142 
143 module_init(m68kspkr_init);
144 module_exit(m68kspkr_exit);
145