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 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 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 78 static void 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 87 static void m68kspkr_shutdown(struct platform_device *dev) 88 { 89 /* turn off the speaker */ 90 m68kspkr_event(NULL, EV_SND, SND_BELL, 0); 91 } 92 93 static struct platform_driver m68kspkr_platform_driver = { 94 .driver = { 95 .name = "m68kspkr", 96 }, 97 .probe = m68kspkr_probe, 98 .remove_new = m68kspkr_remove, 99 .shutdown = m68kspkr_shutdown, 100 }; 101 102 static int __init m68kspkr_init(void) 103 { 104 int err; 105 106 if (!mach_beep) { 107 printk(KERN_INFO "m68kspkr: no lowlevel beep support\n"); 108 return -ENODEV; 109 } 110 111 err = platform_driver_register(&m68kspkr_platform_driver); 112 if (err) 113 return err; 114 115 m68kspkr_platform_device = platform_device_alloc("m68kspkr", -1); 116 if (!m68kspkr_platform_device) { 117 err = -ENOMEM; 118 goto err_unregister_driver; 119 } 120 121 err = platform_device_add(m68kspkr_platform_device); 122 if (err) 123 goto err_free_device; 124 125 return 0; 126 127 err_free_device: 128 platform_device_put(m68kspkr_platform_device); 129 err_unregister_driver: 130 platform_driver_unregister(&m68kspkr_platform_driver); 131 132 return err; 133 } 134 135 static void __exit m68kspkr_exit(void) 136 { 137 platform_device_unregister(m68kspkr_platform_device); 138 platform_driver_unregister(&m68kspkr_platform_driver); 139 } 140 141 module_init(m68kspkr_init); 142 module_exit(m68kspkr_exit); 143