blob: 4776273fa10b3590ad4606f2c4d45df7b7d564af [file] [log] [blame]
Alessandro Zummo01387952006-01-29 21:50:40 -05001/*
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 Zummoa09d31ff2006-02-15 00:48:40 -050022#include <linux/interrupt.h>
Linus Walleije9c9fc22013-09-10 12:53:03 +020023#include <linux/gpio.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/hardware.h>
Alessandro Zummo01387952006-01-29 21:50:40 -050025
26MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
27MODULE_DESCRIPTION("ixp4xx beeper driver");
28MODULE_LICENSE("GPL");
Alessandro Zummo589499c2008-03-28 14:15:59 -070029MODULE_ALIAS("platform:ixp4xx-beeper");
Alessandro Zummo01387952006-01-29 21:50:40 -050030
31static DEFINE_SPINLOCK(beep_lock);
32
Linus Walleij075df312018-12-29 15:49:08 +010033static int ixp4xx_timer2_irq;
34
Alessandro Zummo01387952006-01-29 21:50:40 -050035static void ixp4xx_spkr_control(unsigned int pin, unsigned int count)
36{
37 unsigned long flags;
38
39 spin_lock_irqsave(&beep_lock, flags);
40
Linus Walleije9c9fc22013-09-10 12:53:03 +020041 if (count) {
42 gpio_direction_output(pin, 0);
Alessandro Zummo01387952006-01-29 21:50:40 -050043 *IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
44 } else {
Linus Walleije9c9fc22013-09-10 12:53:03 +020045 gpio_direction_output(pin, 1);
46 gpio_direction_input(pin);
Alessandro Zummo01387952006-01-29 21:50:40 -050047 *IXP4XX_OSRT2 = 0;
48 }
49
50 spin_unlock_irqrestore(&beep_lock, flags);
51}
52
53static int ixp4xx_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
54{
Frederik Deweerdt643bd272007-05-10 11:51:08 -070055 unsigned int pin = (unsigned int) input_get_drvdata(dev);
Alessandro Zummo01387952006-01-29 21:50:40 -050056 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)
Uwe Kleine-König3ae1a6b2013-11-26 15:19:04 +010072 count = (ixp4xx_timer_freq / (value * 4)) - 1;
Alessandro Zummo01387952006-01-29 21:50:40 -050073
74 ixp4xx_spkr_control(pin, count);
75
76 return 0;
77}
78
David Howells7d12e782006-10-05 14:55:46 +010079static irqreturn_t ixp4xx_spkr_interrupt(int irq, void *dev_id)
Alessandro Zummo01387952006-01-29 21:50:40 -050080{
Linus Walleijb22973d2013-09-10 13:06:57 +020081 unsigned int pin = (unsigned int) dev_id;
82
Alessandro Zummo01387952006-01-29 21:50:40 -050083 /* clear interrupt */
84 *IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND;
85
86 /* flip the beeper output */
Linus Walleijb22973d2013-09-10 13:06:57 +020087 gpio_set_value(pin, !gpio_get_value(pin));
Alessandro Zummo01387952006-01-29 21:50:40 -050088
89 return IRQ_HANDLED;
90}
91
Bill Pemberton5298cc42012-11-23 21:38:25 -080092static int ixp4xx_spkr_probe(struct platform_device *dev)
Alessandro Zummo01387952006-01-29 21:50:40 -050093{
94 struct input_dev *input_dev;
Linus Walleij075df312018-12-29 15:49:08 +010095 int irq;
Alessandro Zummo01387952006-01-29 21:50:40 -050096 int err;
97
98 input_dev = input_allocate_device();
99 if (!input_dev)
100 return -ENOMEM;
101
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400102 input_set_drvdata(input_dev, (void *) dev->id);
103
Alessandro Zummo01387952006-01-29 21:50:40 -0500104 input_dev->name = "ixp4xx beeper",
105 input_dev->phys = "ixp4xx/gpio";
106 input_dev->id.bustype = BUS_HOST;
107 input_dev->id.vendor = 0x001f;
108 input_dev->id.product = 0x0001;
109 input_dev->id.version = 0x0100;
Dmitry Torokhov293e6392007-04-12 01:35:32 -0400110 input_dev->dev.parent = &dev->dev;
Alessandro Zummo01387952006-01-29 21:50:40 -0500111
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700112 input_dev->evbit[0] = BIT_MASK(EV_SND);
113 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
Alessandro Zummo01387952006-01-29 21:50:40 -0500114 input_dev->event = ixp4xx_spkr_event;
115
Linus Walleij075df312018-12-29 15:49:08 +0100116 irq = platform_get_irq(dev, 0);
117 if (irq < 0) {
118 err = irq;
119 goto err_free_device;
120 }
121
Linus Walleijb22973d2013-09-10 13:06:57 +0200122 err = gpio_request(dev->id, "ixp4-beeper");
123 if (err)
124 goto err_free_device;
125
Linus Walleij075df312018-12-29 15:49:08 +0100126 err = request_irq(irq, &ixp4xx_spkr_interrupt,
Yong Zhangec4665c2011-09-07 14:04:16 -0700127 IRQF_NO_SUSPEND, "ixp4xx-beeper",
Ian Campbell2dd93202010-07-29 11:16:33 +0100128 (void *) dev->id);
Alessandro Zummo01387952006-01-29 21:50:40 -0500129 if (err)
Linus Walleijb22973d2013-09-10 13:06:57 +0200130 goto err_free_gpio;
Linus Walleij075df312018-12-29 15:49:08 +0100131 ixp4xx_timer2_irq = irq;
Alessandro Zummo01387952006-01-29 21:50:40 -0500132
133 err = input_register_device(input_dev);
134 if (err)
135 goto err_free_irq;
136
137 platform_set_drvdata(dev, input_dev);
138
139 return 0;
140
141 err_free_irq:
Linus Walleij075df312018-12-29 15:49:08 +0100142 free_irq(irq, (void *)dev->id);
Linus Walleijb22973d2013-09-10 13:06:57 +0200143 err_free_gpio:
144 gpio_free(dev->id);
Alessandro Zummo01387952006-01-29 21:50:40 -0500145 err_free_device:
146 input_free_device(input_dev);
147
148 return err;
149}
150
Bill Pembertone2619cf2012-11-23 21:50:47 -0800151static int ixp4xx_spkr_remove(struct platform_device *dev)
Alessandro Zummo01387952006-01-29 21:50:40 -0500152{
153 struct input_dev *input_dev = platform_get_drvdata(dev);
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400154 unsigned int pin = (unsigned int) input_get_drvdata(input_dev);
Alessandro Zummo01387952006-01-29 21:50:40 -0500155
156 input_unregister_device(input_dev);
Alessandro Zummo01387952006-01-29 21:50:40 -0500157
158 /* turn the speaker off */
Linus Walleij075df312018-12-29 15:49:08 +0100159 disable_irq(ixp4xx_timer2_irq);
Alessandro Zummo01387952006-01-29 21:50:40 -0500160 ixp4xx_spkr_control(pin, 0);
161
Linus Walleij075df312018-12-29 15:49:08 +0100162 free_irq(ixp4xx_timer2_irq, (void *)dev->id);
Linus Walleijb22973d2013-09-10 13:06:57 +0200163 gpio_free(dev->id);
Alessandro Zummo01387952006-01-29 21:50:40 -0500164
165 return 0;
166}
167
168static void ixp4xx_spkr_shutdown(struct platform_device *dev)
169{
170 struct input_dev *input_dev = platform_get_drvdata(dev);
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400171 unsigned int pin = (unsigned int) input_get_drvdata(input_dev);
Alessandro Zummo01387952006-01-29 21:50:40 -0500172
173 /* turn off the speaker */
Linus Walleij075df312018-12-29 15:49:08 +0100174 disable_irq(ixp4xx_timer2_irq);
Alessandro Zummo01387952006-01-29 21:50:40 -0500175 ixp4xx_spkr_control(pin, 0);
176}
177
178static struct platform_driver ixp4xx_spkr_platform_driver = {
179 .driver = {
180 .name = "ixp4xx-beeper",
Alessandro Zummo01387952006-01-29 21:50:40 -0500181 },
182 .probe = ixp4xx_spkr_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800183 .remove = ixp4xx_spkr_remove,
Alessandro Zummo01387952006-01-29 21:50:40 -0500184 .shutdown = ixp4xx_spkr_shutdown,
185};
JJ Ding840a7462011-11-29 11:08:40 -0800186module_platform_driver(ixp4xx_spkr_platform_driver);
Alessandro Zummo01387952006-01-29 21:50:40 -0500187