2 * Generic IXP4xx beeper driver
4 * Copyright (C) 2005 Tower Technologies
7 * Copyright (C) 2004 Karen Spearel
9 * Author: Alessandro Zummo <a.zummo@towertech.it>
10 * Maintainers: http://www.nslu2-linux.org/
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.
18 #include <linux/module.h>
19 #include <linux/input.h>
20 #include <linux/delay.h>
21 #include <linux/platform_device.h>
22 #include <linux/interrupt.h>
23 #include <linux/gpio.h>
24 #include <mach/hardware.h>
26 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
27 MODULE_DESCRIPTION("ixp4xx beeper driver");
28 MODULE_LICENSE("GPL");
29 MODULE_ALIAS("platform:ixp4xx-beeper");
31 static DEFINE_SPINLOCK(beep_lock
);
33 static void ixp4xx_spkr_control(unsigned int pin
, unsigned int count
)
37 spin_lock_irqsave(&beep_lock
, flags
);
40 gpio_direction_output(pin
, 0);
41 *IXP4XX_OSRT2
= (count
& ~IXP4XX_OST_RELOAD_MASK
) | IXP4XX_OST_ENABLE
;
43 gpio_direction_output(pin
, 1);
44 gpio_direction_input(pin
);
48 spin_unlock_irqrestore(&beep_lock
, flags
);
51 static int ixp4xx_spkr_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
53 unsigned int pin
= (unsigned int) input_get_drvdata(dev
);
54 unsigned int count
= 0;
69 if (value
> 20 && value
< 32767)
70 count
= (ixp4xx_timer_freq
/ (value
* 4)) - 1;
72 ixp4xx_spkr_control(pin
, count
);
77 static irqreturn_t
ixp4xx_spkr_interrupt(int irq
, void *dev_id
)
79 unsigned int pin
= (unsigned int) dev_id
;
82 *IXP4XX_OSST
= IXP4XX_OSST_TIMER_2_PEND
;
84 /* flip the beeper output */
85 gpio_set_value(pin
, !gpio_get_value(pin
));
90 static int ixp4xx_spkr_probe(struct platform_device
*dev
)
92 struct input_dev
*input_dev
;
95 input_dev
= input_allocate_device();
99 input_set_drvdata(input_dev
, (void *) dev
->id
);
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;
107 input_dev
->dev
.parent
= &dev
->dev
;
109 input_dev
->evbit
[0] = BIT_MASK(EV_SND
);
110 input_dev
->sndbit
[0] = BIT_MASK(SND_BELL
) | BIT_MASK(SND_TONE
);
111 input_dev
->event
= ixp4xx_spkr_event
;
113 err
= gpio_request(dev
->id
, "ixp4-beeper");
115 goto err_free_device
;
117 err
= request_irq(IRQ_IXP4XX_TIMER2
, &ixp4xx_spkr_interrupt
,
118 IRQF_NO_SUSPEND
, "ixp4xx-beeper",
123 err
= input_register_device(input_dev
);
127 platform_set_drvdata(dev
, input_dev
);
132 free_irq(IRQ_IXP4XX_TIMER2
, (void *)dev
->id
);
136 input_free_device(input_dev
);
141 static int ixp4xx_spkr_remove(struct platform_device
*dev
)
143 struct input_dev
*input_dev
= platform_get_drvdata(dev
);
144 unsigned int pin
= (unsigned int) input_get_drvdata(input_dev
);
146 input_unregister_device(input_dev
);
148 /* turn the speaker off */
149 disable_irq(IRQ_IXP4XX_TIMER2
);
150 ixp4xx_spkr_control(pin
, 0);
152 free_irq(IRQ_IXP4XX_TIMER2
, (void *)dev
->id
);
158 static void ixp4xx_spkr_shutdown(struct platform_device
*dev
)
160 struct input_dev
*input_dev
= platform_get_drvdata(dev
);
161 unsigned int pin
= (unsigned int) input_get_drvdata(input_dev
);
163 /* turn off the speaker */
164 disable_irq(IRQ_IXP4XX_TIMER2
);
165 ixp4xx_spkr_control(pin
, 0);
168 static struct platform_driver ixp4xx_spkr_platform_driver
= {
170 .name
= "ixp4xx-beeper",
172 .probe
= ixp4xx_spkr_probe
,
173 .remove
= ixp4xx_spkr_remove
,
174 .shutdown
= ixp4xx_spkr_shutdown
,
176 module_platform_driver(ixp4xx_spkr_platform_driver
);