Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Generic IXP4xx beeper driver |
| 3 | * |
| 4 | * Copyright (C) 2005 Tower Technologies |
| 5 | * |
| 6 | * based on nslu2-io.c |
| 7 | * Copyright (C) 2004 Karen Spearel |
| 8 | * |
| 9 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 10 | * Maintainers: http://www.nslu2-linux.org/ |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/platform_device.h> |
Alessandro Zummo | a09d31ff | 2006-02-15 00:48:40 -0500 | [diff] [blame] | 22 | #include <linux/interrupt.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 23 | #include <mach/hardware.h> |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 24 | |
| 25 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 26 | MODULE_DESCRIPTION("ixp4xx beeper driver"); |
| 27 | MODULE_LICENSE("GPL"); |
Alessandro Zummo | 589499c | 2008-03-28 14:15:59 -0700 | [diff] [blame] | 28 | MODULE_ALIAS("platform:ixp4xx-beeper"); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 29 | |
| 30 | static DEFINE_SPINLOCK(beep_lock); |
| 31 | |
| 32 | static void ixp4xx_spkr_control(unsigned int pin, unsigned int count) |
| 33 | { |
| 34 | unsigned long flags; |
| 35 | |
| 36 | spin_lock_irqsave(&beep_lock, flags); |
| 37 | |
| 38 | if (count) { |
| 39 | gpio_line_config(pin, IXP4XX_GPIO_OUT); |
| 40 | gpio_line_set(pin, IXP4XX_GPIO_LOW); |
| 41 | |
| 42 | *IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE; |
| 43 | } else { |
| 44 | gpio_line_config(pin, IXP4XX_GPIO_IN); |
| 45 | gpio_line_set(pin, IXP4XX_GPIO_HIGH); |
| 46 | |
| 47 | *IXP4XX_OSRT2 = 0; |
| 48 | } |
| 49 | |
| 50 | spin_unlock_irqrestore(&beep_lock, flags); |
| 51 | } |
| 52 | |
| 53 | static int ixp4xx_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 54 | { |
Frederik Deweerdt | 643bd27 | 2007-05-10 11:51:08 -0700 | [diff] [blame] | 55 | unsigned int pin = (unsigned int) input_get_drvdata(dev); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 56 | unsigned int count = 0; |
| 57 | |
| 58 | if (type != EV_SND) |
| 59 | return -1; |
| 60 | |
| 61 | switch (code) { |
| 62 | case SND_BELL: |
| 63 | if (value) |
| 64 | value = 1000; |
| 65 | case SND_TONE: |
| 66 | break; |
| 67 | default: |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | if (value > 20 && value < 32767) |
| 72 | #ifndef FREQ |
| 73 | count = (ixp4xx_get_board_tick_rate() / (value * 4)) - 1; |
| 74 | #else |
| 75 | count = (FREQ / (value * 4)) - 1; |
| 76 | #endif |
| 77 | |
| 78 | ixp4xx_spkr_control(pin, count); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 83 | static irqreturn_t ixp4xx_spkr_interrupt(int irq, void *dev_id) |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 84 | { |
| 85 | /* clear interrupt */ |
| 86 | *IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND; |
| 87 | |
| 88 | /* flip the beeper output */ |
| 89 | *IXP4XX_GPIO_GPOUTR ^= (1 << (unsigned int) dev_id); |
| 90 | |
| 91 | return IRQ_HANDLED; |
| 92 | } |
| 93 | |
| 94 | static int __devinit ixp4xx_spkr_probe(struct platform_device *dev) |
| 95 | { |
| 96 | struct input_dev *input_dev; |
| 97 | int err; |
| 98 | |
| 99 | input_dev = input_allocate_device(); |
| 100 | if (!input_dev) |
| 101 | return -ENOMEM; |
| 102 | |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame] | 103 | input_set_drvdata(input_dev, (void *) dev->id); |
| 104 | |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 105 | input_dev->name = "ixp4xx beeper", |
| 106 | input_dev->phys = "ixp4xx/gpio"; |
| 107 | input_dev->id.bustype = BUS_HOST; |
| 108 | input_dev->id.vendor = 0x001f; |
| 109 | input_dev->id.product = 0x0001; |
| 110 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 293e639 | 2007-04-12 01:35:32 -0400 | [diff] [blame] | 111 | input_dev->dev.parent = &dev->dev; |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 112 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 113 | input_dev->evbit[0] = BIT_MASK(EV_SND); |
| 114 | input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 115 | input_dev->event = ixp4xx_spkr_event; |
| 116 | |
| 117 | err = request_irq(IRQ_IXP4XX_TIMER2, &ixp4xx_spkr_interrupt, |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 118 | IRQF_DISABLED | IRQF_TIMER, "ixp4xx-beeper", (void *) dev->id); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 119 | if (err) |
| 120 | goto err_free_device; |
| 121 | |
| 122 | err = input_register_device(input_dev); |
| 123 | if (err) |
| 124 | goto err_free_irq; |
| 125 | |
| 126 | platform_set_drvdata(dev, input_dev); |
| 127 | |
| 128 | return 0; |
| 129 | |
| 130 | err_free_irq: |
| 131 | free_irq(IRQ_IXP4XX_TIMER2, dev); |
| 132 | err_free_device: |
| 133 | input_free_device(input_dev); |
| 134 | |
| 135 | return err; |
| 136 | } |
| 137 | |
| 138 | static int __devexit ixp4xx_spkr_remove(struct platform_device *dev) |
| 139 | { |
| 140 | struct input_dev *input_dev = platform_get_drvdata(dev); |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame] | 141 | unsigned int pin = (unsigned int) input_get_drvdata(input_dev); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 142 | |
| 143 | input_unregister_device(input_dev); |
| 144 | platform_set_drvdata(dev, NULL); |
| 145 | |
| 146 | /* turn the speaker off */ |
| 147 | disable_irq(IRQ_IXP4XX_TIMER2); |
| 148 | ixp4xx_spkr_control(pin, 0); |
| 149 | |
| 150 | free_irq(IRQ_IXP4XX_TIMER2, dev); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static void ixp4xx_spkr_shutdown(struct platform_device *dev) |
| 156 | { |
| 157 | struct input_dev *input_dev = platform_get_drvdata(dev); |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame] | 158 | unsigned int pin = (unsigned int) input_get_drvdata(input_dev); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 159 | |
| 160 | /* turn off the speaker */ |
| 161 | disable_irq(IRQ_IXP4XX_TIMER2); |
| 162 | ixp4xx_spkr_control(pin, 0); |
| 163 | } |
| 164 | |
| 165 | static struct platform_driver ixp4xx_spkr_platform_driver = { |
| 166 | .driver = { |
| 167 | .name = "ixp4xx-beeper", |
| 168 | .owner = THIS_MODULE, |
| 169 | }, |
| 170 | .probe = ixp4xx_spkr_probe, |
| 171 | .remove = __devexit_p(ixp4xx_spkr_remove), |
| 172 | .shutdown = ixp4xx_spkr_shutdown, |
| 173 | }; |
| 174 | |
| 175 | static int __init ixp4xx_spkr_init(void) |
| 176 | { |
| 177 | return platform_driver_register(&ixp4xx_spkr_platform_driver); |
| 178 | } |
| 179 | |
| 180 | static void __exit ixp4xx_spkr_exit(void) |
| 181 | { |
| 182 | platform_driver_unregister(&ixp4xx_spkr_platform_driver); |
| 183 | } |
| 184 | |
| 185 | module_init(ixp4xx_spkr_init); |
| 186 | module_exit(ixp4xx_spkr_exit); |