ixp4xx-beeper.c 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Generic IXP4xx beeper driver
 *
 * Copyright (C) 2005 Tower Technologies
 *
 * based on nslu2-io.c
 *  Copyright (C) 2004 Karen Spearel
 *
 * Author: Alessandro Zummo <a.zummo@towertech.it>
 * Maintainers: http://www.nslu2-linux.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/input.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
22
#include <linux/interrupt.h>
23
#include <mach/hardware.h>
24 25 26 27

MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
MODULE_DESCRIPTION("ixp4xx beeper driver");
MODULE_LICENSE("GPL");
28
MODULE_ALIAS("platform:ixp4xx-beeper");
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

static DEFINE_SPINLOCK(beep_lock);

static void ixp4xx_spkr_control(unsigned int pin, unsigned int count)
{
	unsigned long flags;

	spin_lock_irqsave(&beep_lock, flags);

	 if (count) {
		gpio_line_config(pin, IXP4XX_GPIO_OUT);
		gpio_line_set(pin, IXP4XX_GPIO_LOW);

		*IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
	} else {
		gpio_line_config(pin, IXP4XX_GPIO_IN);
		gpio_line_set(pin, IXP4XX_GPIO_HIGH);

		*IXP4XX_OSRT2 = 0;
	}

	spin_unlock_irqrestore(&beep_lock, flags);
}

static int ixp4xx_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
{
F
Frederik Deweerdt 已提交
55
	unsigned int pin = (unsigned int) input_get_drvdata(dev);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	unsigned int count = 0;

	if (type != EV_SND)
		return -1;

	switch (code) {
		case SND_BELL:
			if (value)
				value = 1000;
		case SND_TONE:
			break;
		default:
			return -1;
	}

	if (value > 20 && value < 32767)
72
		count = (IXP4XX_TIMER_FREQ / (value * 4)) - 1;
73 74 75 76 77 78

	ixp4xx_spkr_control(pin, count);

	return 0;
}

79
static irqreturn_t ixp4xx_spkr_interrupt(int irq, void *dev_id)
80 81 82 83 84 85 86 87 88 89
{
	/* clear interrupt */
	*IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND;

	/* flip the beeper output */
	*IXP4XX_GPIO_GPOUTR ^= (1 << (unsigned int) dev_id);

	return IRQ_HANDLED;
}

B
Bill Pemberton 已提交
90
static int ixp4xx_spkr_probe(struct platform_device *dev)
91 92 93 94 95 96 97 98
{
	struct input_dev *input_dev;
	int err;

	input_dev = input_allocate_device();
	if (!input_dev)
		return -ENOMEM;

99 100
	input_set_drvdata(input_dev, (void *) dev->id);

101 102 103 104 105 106
	input_dev->name = "ixp4xx beeper",
	input_dev->phys = "ixp4xx/gpio";
	input_dev->id.bustype = BUS_HOST;
	input_dev->id.vendor  = 0x001f;
	input_dev->id.product = 0x0001;
	input_dev->id.version = 0x0100;
107
	input_dev->dev.parent = &dev->dev;
108

109 110
	input_dev->evbit[0] = BIT_MASK(EV_SND);
	input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
111 112 113
	input_dev->event = ixp4xx_spkr_event;

	err = request_irq(IRQ_IXP4XX_TIMER2, &ixp4xx_spkr_interrupt,
114
			  IRQF_NO_SUSPEND, "ixp4xx-beeper",
115
			  (void *) dev->id);
116 117 118 119 120 121 122 123 124 125 126 127
	if (err)
		goto err_free_device;

	err = input_register_device(input_dev);
	if (err)
		goto err_free_irq;

	platform_set_drvdata(dev, input_dev);

	return 0;

 err_free_irq:
128
	free_irq(IRQ_IXP4XX_TIMER2, (void *)dev->id);
129 130 131 132 133 134
 err_free_device:
	input_free_device(input_dev);

	return err;
}

B
Bill Pemberton 已提交
135
static int ixp4xx_spkr_remove(struct platform_device *dev)
136 137
{
	struct input_dev *input_dev = platform_get_drvdata(dev);
138
	unsigned int pin = (unsigned int) input_get_drvdata(input_dev);
139 140 141 142 143 144 145 146

	input_unregister_device(input_dev);
	platform_set_drvdata(dev, NULL);

	/* turn the speaker off */
	disable_irq(IRQ_IXP4XX_TIMER2);
	ixp4xx_spkr_control(pin, 0);

147
	free_irq(IRQ_IXP4XX_TIMER2, (void *)dev->id);
148 149 150 151 152 153 154

	return 0;
}

static void ixp4xx_spkr_shutdown(struct platform_device *dev)
{
	struct input_dev *input_dev = platform_get_drvdata(dev);
155
	unsigned int pin = (unsigned int) input_get_drvdata(input_dev);
156 157 158 159 160 161 162 163 164 165 166 167

	/* turn off the speaker */
	disable_irq(IRQ_IXP4XX_TIMER2);
	ixp4xx_spkr_control(pin, 0);
}

static struct platform_driver ixp4xx_spkr_platform_driver = {
	.driver		= {
		.name	= "ixp4xx-beeper",
		.owner	= THIS_MODULE,
	},
	.probe		= ixp4xx_spkr_probe,
B
Bill Pemberton 已提交
168
	.remove		= ixp4xx_spkr_remove,
169 170
	.shutdown	= ixp4xx_spkr_shutdown,
};
171
module_platform_driver(ixp4xx_spkr_platform_driver);
172