Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for PC-speaker like devices found on various Sparc systems. |
| 3 | * |
| 4 | * Copyright (c) 2002 Vojtech Pavlik |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 5 | * Copyright (c) 2002, 2006 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/input.h> |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 11 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/ebus.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <asm/isa.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 17 | MODULE_AUTHOR("David S. Miller <davem@davemloft.net>"); |
Dmitry Torokhov | 76b7cdd | 2005-09-15 02:01:51 -0500 | [diff] [blame] | 18 | MODULE_DESCRIPTION("Sparc Speaker beeper driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | MODULE_LICENSE("GPL"); |
| 20 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 21 | struct sparcspkr_state { |
| 22 | const char *name; |
| 23 | unsigned long iobase; |
| 24 | int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value); |
| 25 | spinlock_t lock; |
| 26 | struct input_dev *input_dev; |
| 27 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | static int ebus_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 30 | { |
Dmitry Torokhov | 293e639 | 2007-04-12 01:35:32 -0400 | [diff] [blame] | 31 | struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | unsigned int count = 0; |
| 33 | unsigned long flags; |
| 34 | |
| 35 | if (type != EV_SND) |
| 36 | return -1; |
| 37 | |
| 38 | switch (code) { |
| 39 | case SND_BELL: if (value) value = 1000; |
| 40 | case SND_TONE: break; |
| 41 | default: return -1; |
| 42 | } |
| 43 | |
| 44 | if (value > 20 && value < 32767) |
| 45 | count = 1193182 / value; |
| 46 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 47 | spin_lock_irqsave(&state->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* EBUS speaker only has on/off state, the frequency does not |
| 50 | * appear to be programmable. |
| 51 | */ |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 52 | if (state->iobase & 0x2UL) |
| 53 | outb(!!count, state->iobase); |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 54 | else |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 55 | outl(!!count, state->iobase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 57 | spin_unlock_irqrestore(&state->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static int isa_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 63 | { |
Dmitry Torokhov | 293e639 | 2007-04-12 01:35:32 -0400 | [diff] [blame] | 64 | struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | unsigned int count = 0; |
| 66 | unsigned long flags; |
| 67 | |
| 68 | if (type != EV_SND) |
| 69 | return -1; |
| 70 | |
| 71 | switch (code) { |
| 72 | case SND_BELL: if (value) value = 1000; |
| 73 | case SND_TONE: break; |
| 74 | default: return -1; |
| 75 | } |
| 76 | |
| 77 | if (value > 20 && value < 32767) |
| 78 | count = 1193182 / value; |
| 79 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 80 | spin_lock_irqsave(&state->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | if (count) { |
| 83 | /* enable counter 2 */ |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 84 | outb(inb(state->iobase + 0x61) | 3, state->iobase + 0x61); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | /* set command for counter 2, 2 byte write */ |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 86 | outb(0xB6, state->iobase + 0x43); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* select desired HZ */ |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 88 | outb(count & 0xff, state->iobase + 0x42); |
| 89 | outb((count >> 8) & 0xff, state->iobase + 0x42); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } else { |
| 91 | /* disable counter 2 */ |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 92 | outb(inb_p(state->iobase + 0x61) & 0xFC, state->iobase + 0x61); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 95 | spin_unlock_irqrestore(&state->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 100 | static int __devinit sparcspkr_probe(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 102 | struct sparcspkr_state *state = dev_get_drvdata(dev); |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 103 | struct input_dev *input_dev; |
| 104 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 106 | input_dev = input_allocate_device(); |
| 107 | if (!input_dev) |
Dmitry Torokhov | 76b7cdd | 2005-09-15 02:01:51 -0500 | [diff] [blame] | 108 | return -ENOMEM; |
| 109 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 110 | input_dev->name = state->name; |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 111 | input_dev->phys = "sparc/input0"; |
| 112 | input_dev->id.bustype = BUS_ISA; |
| 113 | input_dev->id.vendor = 0x001f; |
| 114 | input_dev->id.product = 0x0001; |
| 115 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 293e639 | 2007-04-12 01:35:32 -0400 | [diff] [blame] | 116 | input_dev->dev.parent = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 118 | input_dev->evbit[0] = BIT(EV_SND); |
| 119 | input_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 121 | input_dev->event = state->event; |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 122 | |
| 123 | error = input_register_device(input_dev); |
| 124 | if (error) { |
| 125 | input_free_device(input_dev); |
| 126 | return error; |
| 127 | } |
| 128 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 129 | state->input_dev = input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | return 0; |
| 132 | } |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 133 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 134 | static int __devexit sparcspkr_remove(struct of_device *dev) |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 135 | { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 136 | struct sparcspkr_state *state = dev_get_drvdata(&dev->dev); |
| 137 | struct input_dev *input_dev = state->input_dev; |
| 138 | |
| 139 | /* turn off the speaker */ |
| 140 | state->event(input_dev, EV_SND, SND_BELL, 0); |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 141 | |
| 142 | input_unregister_device(input_dev); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 143 | |
| 144 | dev_set_drvdata(&dev->dev, NULL); |
| 145 | kfree(state); |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 150 | static int sparcspkr_shutdown(struct of_device *dev) |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 151 | { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 152 | struct sparcspkr_state *state = dev_get_drvdata(&dev->dev); |
| 153 | struct input_dev *input_dev = state->input_dev; |
| 154 | |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 155 | /* turn off the speaker */ |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 156 | state->event(input_dev, EV_SND, SND_BELL, 0); |
| 157 | |
| 158 | return 0; |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 159 | } |
| 160 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 161 | static int __devinit ebus_beep_probe(struct of_device *dev, const struct of_device_id *match) |
| 162 | { |
| 163 | struct linux_ebus_device *edev = to_ebus_device(&dev->dev); |
| 164 | struct sparcspkr_state *state; |
| 165 | int err; |
| 166 | |
| 167 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 168 | if (!state) |
| 169 | return -ENOMEM; |
| 170 | |
| 171 | state->name = "Sparc EBUS Speaker"; |
| 172 | state->iobase = edev->resource[0].start; |
| 173 | state->event = ebus_spkr_event; |
| 174 | spin_lock_init(&state->lock); |
| 175 | |
| 176 | dev_set_drvdata(&dev->dev, state); |
| 177 | |
| 178 | err = sparcspkr_probe(&dev->dev); |
| 179 | if (err) { |
| 180 | dev_set_drvdata(&dev->dev, NULL); |
| 181 | kfree(state); |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static struct of_device_id ebus_beep_match[] = { |
| 188 | { |
| 189 | .name = "beep", |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 190 | }, |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 191 | {}, |
| 192 | }; |
| 193 | |
| 194 | static struct of_platform_driver ebus_beep_driver = { |
| 195 | .name = "beep", |
| 196 | .match_table = ebus_beep_match, |
| 197 | .probe = ebus_beep_probe, |
| 198 | .remove = sparcspkr_remove, |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 199 | .shutdown = sparcspkr_shutdown, |
| 200 | }; |
| 201 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 202 | static int __devinit isa_beep_probe(struct of_device *dev, const struct of_device_id *match) |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 203 | { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 204 | struct sparc_isa_device *idev = to_isa_device(&dev->dev); |
| 205 | struct sparcspkr_state *state; |
| 206 | int err; |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 207 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 208 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 209 | if (!state) |
| 210 | return -ENOMEM; |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 211 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 212 | state->name = "Sparc ISA Speaker"; |
| 213 | state->iobase = idev->resource.start; |
| 214 | state->event = isa_spkr_event; |
| 215 | spin_lock_init(&state->lock); |
| 216 | |
| 217 | dev_set_drvdata(&dev->dev, state); |
| 218 | |
| 219 | err = sparcspkr_probe(&dev->dev); |
| 220 | if (err) { |
| 221 | dev_set_drvdata(&dev->dev, NULL); |
| 222 | kfree(state); |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 225 | return 0; |
Dmitry Torokhov | f5b6407 | 2005-12-21 00:52:35 -0500 | [diff] [blame] | 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 228 | static struct of_device_id isa_beep_match[] = { |
| 229 | { |
| 230 | .name = "dma", |
| 231 | }, |
| 232 | {}, |
| 233 | }; |
| 234 | |
| 235 | static struct of_platform_driver isa_beep_driver = { |
| 236 | .name = "beep", |
| 237 | .match_table = isa_beep_match, |
| 238 | .probe = isa_beep_probe, |
| 239 | .remove = sparcspkr_remove, |
| 240 | .shutdown = sparcspkr_shutdown, |
| 241 | }; |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | static int __init sparcspkr_init(void) |
| 244 | { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 245 | int err = of_register_driver(&ebus_beep_driver, &ebus_bus_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 247 | if (!err) { |
| 248 | err = of_register_driver(&isa_beep_driver, &isa_bus_type); |
| 249 | if (err) |
| 250 | of_unregister_driver(&ebus_beep_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 253 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static void __exit sparcspkr_exit(void) |
| 257 | { |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 258 | of_unregister_driver(&ebus_beep_driver); |
| 259 | of_unregister_driver(&isa_beep_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | module_init(sparcspkr_init); |
| 263 | module_exit(sparcspkr_exit); |