blob: 794ecc9a552dd01ec9ad48933d58ae3c1a994801 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Alessandro Zummo01387952006-01-29 21:50:40 -05002/*
3 * Generic IXP4xx beeper driver
4 *
5 * Copyright (C) 2005 Tower Technologies
6 *
7 * based on nslu2-io.c
8 * Copyright (C) 2004 Karen Spearel
9 *
10 * Author: Alessandro Zummo <a.zummo@towertech.it>
11 * Maintainers: http://www.nslu2-linux.org/
Alessandro Zummo01387952006-01-29 21:50:40 -050012 */
13
14#include <linux/module.h>
15#include <linux/input.h>
16#include <linux/delay.h>
17#include <linux/platform_device.h>
Alessandro Zummoa09d31ff2006-02-15 00:48:40 -050018#include <linux/interrupt.h>
Linus Walleije9c9fc22013-09-10 12:53:03 +020019#include <linux/gpio.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010020#include <mach/hardware.h>
Alessandro Zummo01387952006-01-29 21:50:40 -050021
22MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
23MODULE_DESCRIPTION("ixp4xx beeper driver");
24MODULE_LICENSE("GPL");
Alessandro Zummo589499c2008-03-28 14:15:59 -070025MODULE_ALIAS("platform:ixp4xx-beeper");
Alessandro Zummo01387952006-01-29 21:50:40 -050026
27static DEFINE_SPINLOCK(beep_lock);
28
Linus Walleij075df312018-12-29 15:49:08 +010029static int ixp4xx_timer2_irq;
30
Alessandro Zummo01387952006-01-29 21:50:40 -050031static void ixp4xx_spkr_control(unsigned int pin, unsigned int count)
32{
33 unsigned long flags;
34
35 spin_lock_irqsave(&beep_lock, flags);
36
Linus Walleije9c9fc22013-09-10 12:53:03 +020037 if (count) {
38 gpio_direction_output(pin, 0);
Alessandro Zummo01387952006-01-29 21:50:40 -050039 *IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
40 } else {
Linus Walleije9c9fc22013-09-10 12:53:03 +020041 gpio_direction_output(pin, 1);
42 gpio_direction_input(pin);
Alessandro Zummo01387952006-01-29 21:50:40 -050043 *IXP4XX_OSRT2 = 0;
44 }
45
46 spin_unlock_irqrestore(&beep_lock, flags);
47}
48
49static int ixp4xx_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
50{
Frederik Deweerdt643bd272007-05-10 11:51:08 -070051 unsigned int pin = (unsigned int) input_get_drvdata(dev);
Alessandro Zummo01387952006-01-29 21:50:40 -050052 unsigned int count = 0;
53
54 if (type != EV_SND)
55 return -1;
56
57 switch (code) {
58 case SND_BELL:
59 if (value)
60 value = 1000;
61 case SND_TONE:
62 break;
63 default:
64 return -1;
65 }
66
67 if (value > 20 && value < 32767)
Uwe Kleine-König3ae1a6b2013-11-26 15:19:04 +010068 count = (ixp4xx_timer_freq / (value * 4)) - 1;
Alessandro Zummo01387952006-01-29 21:50:40 -050069
70 ixp4xx_spkr_control(pin, count);
71
72 return 0;
73}
74
David Howells7d12e782006-10-05 14:55:46 +010075static irqreturn_t ixp4xx_spkr_interrupt(int irq, void *dev_id)
Alessandro Zummo01387952006-01-29 21:50:40 -050076{
Linus Walleijb22973d2013-09-10 13:06:57 +020077 unsigned int pin = (unsigned int) dev_id;
78
Alessandro Zummo01387952006-01-29 21:50:40 -050079 /* clear interrupt */
80 *IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND;
81
82 /* flip the beeper output */
Linus Walleijb22973d2013-09-10 13:06:57 +020083 gpio_set_value(pin, !gpio_get_value(pin));
Alessandro Zummo01387952006-01-29 21:50:40 -050084
85 return IRQ_HANDLED;
86}
87
Bill Pemberton5298cc42012-11-23 21:38:25 -080088static int ixp4xx_spkr_probe(struct platform_device *dev)
Alessandro Zummo01387952006-01-29 21:50:40 -050089{
90 struct input_dev *input_dev;
Linus Walleij075df312018-12-29 15:49:08 +010091 int irq;
Alessandro Zummo01387952006-01-29 21:50:40 -050092 int err;
93
94 input_dev = input_allocate_device();
95 if (!input_dev)
96 return -ENOMEM;
97
Dmitry Torokhov373f9712007-04-12 01:34:33 -040098 input_set_drvdata(input_dev, (void *) dev->id);
99
Alessandro Zummo01387952006-01-29 21:50:40 -0500100 input_dev->name = "ixp4xx beeper",
101 input_dev->phys = "ixp4xx/gpio";
102 input_dev->id.bustype = BUS_HOST;
103 input_dev->id.vendor = 0x001f;
104 input_dev->id.product = 0x0001;
105 input_dev->id.version = 0x0100;
Dmitry Torokhov293e6392007-04-12 01:35:32 -0400106 input_dev->dev.parent = &dev->dev;
Alessandro Zummo01387952006-01-29 21:50:40 -0500107
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700108 input_dev->evbit[0] = BIT_MASK(EV_SND);
109 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
Alessandro Zummo01387952006-01-29 21:50:40 -0500110 input_dev->event = ixp4xx_spkr_event;
111
Linus Walleij075df312018-12-29 15:49:08 +0100112 irq = platform_get_irq(dev, 0);
113 if (irq < 0) {
114 err = irq;
115 goto err_free_device;
116 }
117
Linus Walleijb22973d2013-09-10 13:06:57 +0200118 err = gpio_request(dev->id, "ixp4-beeper");
119 if (err)
120 goto err_free_device;
121
Linus Walleij075df312018-12-29 15:49:08 +0100122 err = request_irq(irq, &ixp4xx_spkr_interrupt,
Yong Zhangec4665c2011-09-07 14:04:16 -0700123 IRQF_NO_SUSPEND, "ixp4xx-beeper",
Ian Campbell2dd93202010-07-29 11:16:33 +0100124 (void *) dev->id);
Alessandro Zummo01387952006-01-29 21:50:40 -0500125 if (err)
Linus Walleijb22973d2013-09-10 13:06:57 +0200126 goto err_free_gpio;
Linus Walleij075df312018-12-29 15:49:08 +0100127 ixp4xx_timer2_irq = irq;
Alessandro Zummo01387952006-01-29 21:50:40 -0500128
129 err = input_register_device(input_dev);
130 if (err)
131 goto err_free_irq;
132
133 platform_set_drvdata(dev, input_dev);
134
135 return 0;
136
137 err_free_irq:
Linus Walleij075df312018-12-29 15:49:08 +0100138 free_irq(irq, (void *)dev->id);
Linus Walleijb22973d2013-09-10 13:06:57 +0200139 err_free_gpio:
140 gpio_free(dev->id);
Alessandro Zummo01387952006-01-29 21:50:40 -0500141 err_free_device:
142 input_free_device(input_dev);
143
144 return err;
145}
146
Bill Pembertone2619cf2012-11-23 21:50:47 -0800147static int ixp4xx_spkr_remove(struct platform_device *dev)
Alessandro Zummo01387952006-01-29 21:50:40 -0500148{
149 struct input_dev *input_dev = platform_get_drvdata(dev);
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400150 unsigned int pin = (unsigned int) input_get_drvdata(input_dev);
Alessandro Zummo01387952006-01-29 21:50:40 -0500151
152 input_unregister_device(input_dev);
Alessandro Zummo01387952006-01-29 21:50:40 -0500153
154 /* turn the speaker off */
Linus Walleij075df312018-12-29 15:49:08 +0100155 disable_irq(ixp4xx_timer2_irq);
Alessandro Zummo01387952006-01-29 21:50:40 -0500156 ixp4xx_spkr_control(pin, 0);
157
Linus Walleij075df312018-12-29 15:49:08 +0100158 free_irq(ixp4xx_timer2_irq, (void *)dev->id);
Linus Walleijb22973d2013-09-10 13:06:57 +0200159 gpio_free(dev->id);
Alessandro Zummo01387952006-01-29 21:50:40 -0500160
161 return 0;
162}
163
164static void ixp4xx_spkr_shutdown(struct platform_device *dev)
165{
166 struct input_dev *input_dev = platform_get_drvdata(dev);
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400167 unsigned int pin = (unsigned int) input_get_drvdata(input_dev);
Alessandro Zummo01387952006-01-29 21:50:40 -0500168
169 /* turn off the speaker */
Linus Walleij075df312018-12-29 15:49:08 +0100170 disable_irq(ixp4xx_timer2_irq);
Alessandro Zummo01387952006-01-29 21:50:40 -0500171 ixp4xx_spkr_control(pin, 0);
172}
173
174static struct platform_driver ixp4xx_spkr_platform_driver = {
175 .driver = {
176 .name = "ixp4xx-beeper",
Alessandro Zummo01387952006-01-29 21:50:40 -0500177 },
178 .probe = ixp4xx_spkr_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800179 .remove = ixp4xx_spkr_remove,
Alessandro Zummo01387952006-01-29 21:50:40 -0500180 .shutdown = ixp4xx_spkr_shutdown,
181};
JJ Ding840a7462011-11-29 11:08:40 -0800182module_platform_driver(ixp4xx_spkr_platform_driver);
Alessandro Zummo01387952006-01-29 21:50:40 -0500183