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> |
Linus Walleij | e9c9fc2 | 2013-09-10 12:53:03 +0200 | [diff] [blame] | 23 | #include <linux/gpio.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 24 | #include <mach/hardware.h> |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 25 | |
| 26 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 27 | MODULE_DESCRIPTION("ixp4xx beeper driver"); |
| 28 | MODULE_LICENSE("GPL"); |
Alessandro Zummo | 589499c | 2008-03-28 14:15:59 -0700 | [diff] [blame] | 29 | MODULE_ALIAS("platform:ixp4xx-beeper"); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 30 | |
| 31 | static DEFINE_SPINLOCK(beep_lock); |
| 32 | |
| 33 | static void ixp4xx_spkr_control(unsigned int pin, unsigned int count) |
| 34 | { |
| 35 | unsigned long flags; |
| 36 | |
| 37 | spin_lock_irqsave(&beep_lock, flags); |
| 38 | |
Linus Walleij | e9c9fc2 | 2013-09-10 12:53:03 +0200 | [diff] [blame] | 39 | if (count) { |
| 40 | gpio_direction_output(pin, 0); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 41 | *IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE; |
| 42 | } else { |
Linus Walleij | e9c9fc2 | 2013-09-10 12:53:03 +0200 | [diff] [blame] | 43 | gpio_direction_output(pin, 1); |
| 44 | gpio_direction_input(pin); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 45 | *IXP4XX_OSRT2 = 0; |
| 46 | } |
| 47 | |
| 48 | spin_unlock_irqrestore(&beep_lock, flags); |
| 49 | } |
| 50 | |
| 51 | static int ixp4xx_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 52 | { |
Frederik Deweerdt | 643bd27 | 2007-05-10 11:51:08 -0700 | [diff] [blame] | 53 | unsigned int pin = (unsigned int) input_get_drvdata(dev); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 54 | unsigned int count = 0; |
| 55 | |
| 56 | if (type != EV_SND) |
| 57 | return -1; |
| 58 | |
| 59 | switch (code) { |
| 60 | case SND_BELL: |
| 61 | if (value) |
| 62 | value = 1000; |
| 63 | case SND_TONE: |
| 64 | break; |
| 65 | default: |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | if (value > 20 && value < 32767) |
Uwe Kleine-König | 3ae1a6b | 2013-11-26 15:19:04 +0100 | [diff] [blame] | 70 | count = (ixp4xx_timer_freq / (value * 4)) - 1; |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 71 | |
| 72 | ixp4xx_spkr_control(pin, count); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 77 | static irqreturn_t ixp4xx_spkr_interrupt(int irq, void *dev_id) |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 78 | { |
Linus Walleij | b22973d | 2013-09-10 13:06:57 +0200 | [diff] [blame] | 79 | unsigned int pin = (unsigned int) dev_id; |
| 80 | |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 81 | /* clear interrupt */ |
| 82 | *IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND; |
| 83 | |
| 84 | /* flip the beeper output */ |
Linus Walleij | b22973d | 2013-09-10 13:06:57 +0200 | [diff] [blame] | 85 | gpio_set_value(pin, !gpio_get_value(pin)); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 86 | |
| 87 | return IRQ_HANDLED; |
| 88 | } |
| 89 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 90 | static int ixp4xx_spkr_probe(struct platform_device *dev) |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 91 | { |
| 92 | struct input_dev *input_dev; |
| 93 | int err; |
| 94 | |
| 95 | input_dev = input_allocate_device(); |
| 96 | if (!input_dev) |
| 97 | return -ENOMEM; |
| 98 | |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame] | 99 | input_set_drvdata(input_dev, (void *) dev->id); |
| 100 | |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 101 | input_dev->name = "ixp4xx beeper", |
| 102 | input_dev->phys = "ixp4xx/gpio"; |
| 103 | input_dev->id.bustype = BUS_HOST; |
| 104 | input_dev->id.vendor = 0x001f; |
| 105 | input_dev->id.product = 0x0001; |
| 106 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 293e639 | 2007-04-12 01:35:32 -0400 | [diff] [blame] | 107 | input_dev->dev.parent = &dev->dev; |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 108 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 109 | input_dev->evbit[0] = BIT_MASK(EV_SND); |
| 110 | input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 111 | input_dev->event = ixp4xx_spkr_event; |
| 112 | |
Linus Walleij | b22973d | 2013-09-10 13:06:57 +0200 | [diff] [blame] | 113 | err = gpio_request(dev->id, "ixp4-beeper"); |
| 114 | if (err) |
| 115 | goto err_free_device; |
| 116 | |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 117 | err = request_irq(IRQ_IXP4XX_TIMER2, &ixp4xx_spkr_interrupt, |
Yong Zhang | ec4665c | 2011-09-07 14:04:16 -0700 | [diff] [blame] | 118 | IRQF_NO_SUSPEND, "ixp4xx-beeper", |
Ian Campbell | 2dd9320 | 2010-07-29 11:16:33 +0100 | [diff] [blame] | 119 | (void *) dev->id); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 120 | if (err) |
Linus Walleij | b22973d | 2013-09-10 13:06:57 +0200 | [diff] [blame] | 121 | goto err_free_gpio; |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 122 | |
| 123 | err = input_register_device(input_dev); |
| 124 | if (err) |
| 125 | goto err_free_irq; |
| 126 | |
| 127 | platform_set_drvdata(dev, input_dev); |
| 128 | |
| 129 | return 0; |
| 130 | |
| 131 | err_free_irq: |
Lars-Peter Clausen | 25c9b7b | 2013-05-23 09:30:15 -0700 | [diff] [blame] | 132 | free_irq(IRQ_IXP4XX_TIMER2, (void *)dev->id); |
Linus Walleij | b22973d | 2013-09-10 13:06:57 +0200 | [diff] [blame] | 133 | err_free_gpio: |
| 134 | gpio_free(dev->id); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 135 | err_free_device: |
| 136 | input_free_device(input_dev); |
| 137 | |
| 138 | return err; |
| 139 | } |
| 140 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 141 | static int ixp4xx_spkr_remove(struct platform_device *dev) |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 142 | { |
| 143 | struct input_dev *input_dev = platform_get_drvdata(dev); |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame] | 144 | unsigned int pin = (unsigned int) input_get_drvdata(input_dev); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 145 | |
| 146 | input_unregister_device(input_dev); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 147 | |
| 148 | /* turn the speaker off */ |
| 149 | disable_irq(IRQ_IXP4XX_TIMER2); |
| 150 | ixp4xx_spkr_control(pin, 0); |
| 151 | |
Lars-Peter Clausen | 25c9b7b | 2013-05-23 09:30:15 -0700 | [diff] [blame] | 152 | free_irq(IRQ_IXP4XX_TIMER2, (void *)dev->id); |
Linus Walleij | b22973d | 2013-09-10 13:06:57 +0200 | [diff] [blame] | 153 | gpio_free(dev->id); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static void ixp4xx_spkr_shutdown(struct platform_device *dev) |
| 159 | { |
| 160 | struct input_dev *input_dev = platform_get_drvdata(dev); |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame] | 161 | unsigned int pin = (unsigned int) input_get_drvdata(input_dev); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 162 | |
| 163 | /* turn off the speaker */ |
| 164 | disable_irq(IRQ_IXP4XX_TIMER2); |
| 165 | ixp4xx_spkr_control(pin, 0); |
| 166 | } |
| 167 | |
| 168 | static struct platform_driver ixp4xx_spkr_platform_driver = { |
| 169 | .driver = { |
| 170 | .name = "ixp4xx-beeper", |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 171 | }, |
| 172 | .probe = ixp4xx_spkr_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 173 | .remove = ixp4xx_spkr_remove, |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 174 | .shutdown = ixp4xx_spkr_shutdown, |
| 175 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 176 | module_platform_driver(ixp4xx_spkr_platform_driver); |
Alessandro Zummo | 0138795 | 2006-01-29 21:50:40 -0500 | [diff] [blame] | 177 | |