pcspkr.c 3.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  PC Speaker beeper driver for Linux
 *
 *  Copyright (c) 2002 Vojtech Pavlik
 *  Copyright (c) 1992 Orest Zborowski
 *
 */

/*
 * 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/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/input.h>
19
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
20 21 22 23 24
#include <asm/io.h>

MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
MODULE_DESCRIPTION("PC Speaker beeper driver");
MODULE_LICENSE("GPL");
25
MODULE_ALIAS("platform:pcspkr");
L
Linus Torvalds 已提交
26

27
#if defined(CONFIG_MIPS) || defined(CONFIG_X86)
28 29 30
/* Use the global PIT lock ! */
#include <asm/i8253.h>
#else
T
Thomas Gleixner 已提交
31
#include <asm/8253pit.h>
32 33
static DEFINE_SPINLOCK(i8253_lock);
#endif
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

static int pcspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
{
	unsigned int count = 0;
	unsigned long flags;

	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)
		count = PIT_TICK_RATE / value;

52
	spin_lock_irqsave(&i8253_lock, flags);
L
Linus Torvalds 已提交
53 54 55 56 57 58 59

	if (count) {
		/* set command for counter 2, 2 byte write */
		outb_p(0xB6, 0x43);
		/* select desired HZ */
		outb_p(count & 0xff, 0x42);
		outb((count >> 8) & 0xff, 0x42);
60 61
		/* enable counter 2 */
		outb_p(inb_p(0x61) | 3, 0x61);
L
Linus Torvalds 已提交
62 63 64 65 66
	} else {
		/* disable counter 2 */
		outb(inb_p(0x61) & 0xFC, 0x61);
	}

67
	spin_unlock_irqrestore(&i8253_lock, flags);
L
Linus Torvalds 已提交
68 69 70 71

	return 0;
}

72
static int __devinit pcspkr_probe(struct platform_device *dev)
L
Linus Torvalds 已提交
73
{
74 75 76
	struct input_dev *pcspkr_dev;
	int err;

77 78 79
	pcspkr_dev = input_allocate_device();
	if (!pcspkr_dev)
		return -ENOMEM;
L
Linus Torvalds 已提交
80

81
	pcspkr_dev->name = "PC Speaker";
82
	pcspkr_dev->phys = "isa0061/input0";
83 84 85 86
	pcspkr_dev->id.bustype = BUS_ISA;
	pcspkr_dev->id.vendor = 0x001f;
	pcspkr_dev->id.product = 0x0001;
	pcspkr_dev->id.version = 0x0100;
87
	pcspkr_dev->dev.parent = &dev->dev;
L
Linus Torvalds 已提交
88

89 90
	pcspkr_dev->evbit[0] = BIT_MASK(EV_SND);
	pcspkr_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
91
	pcspkr_dev->event = pcspkr_event;
L
Linus Torvalds 已提交
92

93 94 95 96 97 98 99
	err = input_register_device(pcspkr_dev);
	if (err) {
		input_free_device(pcspkr_dev);
		return err;
	}

	platform_set_drvdata(dev, pcspkr_dev);
L
Linus Torvalds 已提交
100 101 102 103

	return 0;
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
static int __devexit pcspkr_remove(struct platform_device *dev)
{
	struct input_dev *pcspkr_dev = platform_get_drvdata(dev);

	input_unregister_device(pcspkr_dev);
	platform_set_drvdata(dev, NULL);
	/* turn off the speaker */
	pcspkr_event(NULL, EV_SND, SND_BELL, 0);

	return 0;
}

static int pcspkr_suspend(struct platform_device *dev, pm_message_t state)
{
	pcspkr_event(NULL, EV_SND, SND_BELL, 0);

	return 0;
}

static void pcspkr_shutdown(struct platform_device *dev)
L
Linus Torvalds 已提交
124 125 126 127 128
{
	/* turn off the speaker */
	pcspkr_event(NULL, EV_SND, SND_BELL, 0);
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142
static struct platform_driver pcspkr_platform_driver = {
	.driver		= {
		.name	= "pcspkr",
		.owner	= THIS_MODULE,
	},
	.probe		= pcspkr_probe,
	.remove		= __devexit_p(pcspkr_remove),
	.suspend	= pcspkr_suspend,
	.shutdown	= pcspkr_shutdown,
};


static int __init pcspkr_init(void)
{
143
	return platform_driver_register(&pcspkr_platform_driver);
144 145 146 147 148 149 150
}

static void __exit pcspkr_exit(void)
{
	platform_driver_unregister(&pcspkr_platform_driver);
}

L
Linus Torvalds 已提交
151 152
module_init(pcspkr_init);
module_exit(pcspkr_exit);