1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic IXP4xx beeper driver
5 * Copyright (C) 2005 Tower Technologies
8 * Copyright (C) 2004 Karen Spearel
10 * Author: Alessandro Zummo <a.zummo@towertech.it>
11 * Maintainers: http://www.nslu2-linux.org/
14 #include <linux/module.h>
15 #include <linux/input.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/interrupt.h>
19 #include <linux/gpio.h>
20 #include <mach/hardware.h>
22 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
23 MODULE_DESCRIPTION("ixp4xx beeper driver");
24 MODULE_LICENSE("GPL");
25 MODULE_ALIAS("platform:ixp4xx-beeper");
27 static DEFINE_SPINLOCK(beep_lock
);
29 static int ixp4xx_timer2_irq
;
31 static void ixp4xx_spkr_control(unsigned int pin
, unsigned int count
)
35 spin_lock_irqsave(&beep_lock
, flags
);
38 gpio_direction_output(pin
, 0);
39 *IXP4XX_OSRT2
= (count
& ~IXP4XX_OST_RELOAD_MASK
) | IXP4XX_OST_ENABLE
;
41 gpio_direction_output(pin
, 1);
42 gpio_direction_input(pin
);
46 spin_unlock_irqrestore(&beep_lock
, flags
);
49 static int ixp4xx_spkr_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
51 unsigned int pin
= (unsigned int) input_get_drvdata(dev
);
52 unsigned int count
= 0;
67 if (value
> 20 && value
< 32767)
68 count
= (ixp4xx_timer_freq
/ (value
* 4)) - 1;
70 ixp4xx_spkr_control(pin
, count
);
75 static irqreturn_t
ixp4xx_spkr_interrupt(int irq
, void *dev_id
)
77 unsigned int pin
= (unsigned int) dev_id
;
80 *IXP4XX_OSST
= IXP4XX_OSST_TIMER_2_PEND
;
82 /* flip the beeper output */
83 gpio_set_value(pin
, !gpio_get_value(pin
));
88 static int ixp4xx_spkr_probe(struct platform_device
*dev
)
90 struct input_dev
*input_dev
;
94 input_dev
= input_allocate_device();
98 input_set_drvdata(input_dev
, (void *) dev
->id
);
100 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;
106 input_dev
->dev
.parent
= &dev
->dev
;
108 input_dev
->evbit
[0] = BIT_MASK(EV_SND
);
109 input_dev
->sndbit
[0] = BIT_MASK(SND_BELL
) | BIT_MASK(SND_TONE
);
110 input_dev
->event
= ixp4xx_spkr_event
;
112 irq
= platform_get_irq(dev
, 0);
115 goto err_free_device
;
118 err
= gpio_request(dev
->id
, "ixp4-beeper");
120 goto err_free_device
;
122 err
= request_irq(irq
, &ixp4xx_spkr_interrupt
,
123 IRQF_NO_SUSPEND
, "ixp4xx-beeper",
127 ixp4xx_timer2_irq
= irq
;
129 err
= input_register_device(input_dev
);
133 platform_set_drvdata(dev
, input_dev
);
138 free_irq(irq
, (void *)dev
->id
);
142 input_free_device(input_dev
);
147 static int ixp4xx_spkr_remove(struct platform_device
*dev
)
149 struct input_dev
*input_dev
= platform_get_drvdata(dev
);
150 unsigned int pin
= (unsigned int) input_get_drvdata(input_dev
);
152 input_unregister_device(input_dev
);
154 /* turn the speaker off */
155 disable_irq(ixp4xx_timer2_irq
);
156 ixp4xx_spkr_control(pin
, 0);
158 free_irq(ixp4xx_timer2_irq
, (void *)dev
->id
);
164 static void ixp4xx_spkr_shutdown(struct platform_device
*dev
)
166 struct input_dev
*input_dev
= platform_get_drvdata(dev
);
167 unsigned int pin
= (unsigned int) input_get_drvdata(input_dev
);
169 /* turn off the speaker */
170 disable_irq(ixp4xx_timer2_irq
);
171 ixp4xx_spkr_control(pin
, 0);
174 static struct platform_driver ixp4xx_spkr_platform_driver
= {
176 .name
= "ixp4xx-beeper",
178 .probe
= ixp4xx_spkr_probe
,
179 .remove
= ixp4xx_spkr_remove
,
180 .shutdown
= ixp4xx_spkr_shutdown
,
182 module_platform_driver(ixp4xx_spkr_platform_driver
);