ambakmi.c revision 4e8718a1f960db0c48427f4583f89f4cb62f2480
1/*
2 *  linux/drivers/input/serio/ambakmi.c
3 *
4 *  Copyright (C) 2000-2003 Deep Blue Solutions Ltd.
5 *  Copyright (C) 2002 Russell King.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/serio.h>
15#include <linux/errno.h>
16#include <linux/interrupt.h>
17#include <linux/ioport.h>
18#include <linux/device.h>
19#include <linux/delay.h>
20#include <linux/slab.h>
21#include <linux/err.h>
22#include <linux/amba/bus.h>
23#include <linux/amba/kmi.h>
24#include <linux/clk.h>
25
26#include <asm/io.h>
27#include <asm/irq.h>
28
29#define KMI_BASE	(kmi->base)
30
31struct amba_kmi_port {
32	struct serio		*io;
33	struct clk		*clk;
34	void __iomem		*base;
35	unsigned int		irq;
36	unsigned int		divisor;
37	unsigned int		open;
38};
39
40static irqreturn_t amba_kmi_int(int irq, void *dev_id)
41{
42	struct amba_kmi_port *kmi = dev_id;
43	unsigned int status = readb(KMIIR);
44	int handled = IRQ_NONE;
45
46	while (status & KMIIR_RXINTR) {
47		serio_interrupt(kmi->io, readb(KMIDATA), 0);
48		status = readb(KMIIR);
49		handled = IRQ_HANDLED;
50	}
51
52	return handled;
53}
54
55static int amba_kmi_write(struct serio *io, unsigned char val)
56{
57	struct amba_kmi_port *kmi = io->port_data;
58	unsigned int timeleft = 10000; /* timeout in 100ms */
59
60	while ((readb(KMISTAT) & KMISTAT_TXEMPTY) == 0 && timeleft--)
61		udelay(10);
62
63	if (timeleft)
64		writeb(val, KMIDATA);
65
66	return timeleft ? 0 : SERIO_TIMEOUT;
67}
68
69static int amba_kmi_open(struct serio *io)
70{
71	struct amba_kmi_port *kmi = io->port_data;
72	unsigned int divisor;
73	int ret;
74
75	ret = clk_enable(kmi->clk);
76	if (ret)
77		goto out;
78
79	divisor = clk_get_rate(kmi->clk) / 8000000 - 1;
80	writeb(divisor, KMICLKDIV);
81	writeb(KMICR_EN, KMICR);
82
83	ret = request_irq(kmi->irq, amba_kmi_int, 0, "kmi-pl050", kmi);
84	if (ret) {
85		printk(KERN_ERR "kmi: failed to claim IRQ%d\n", kmi->irq);
86		writeb(0, KMICR);
87		goto clk_disable;
88	}
89
90	writeb(KMICR_EN | KMICR_RXINTREN, KMICR);
91
92	return 0;
93
94 clk_disable:
95	clk_disable(kmi->clk);
96 out:
97	return ret;
98}
99
100static void amba_kmi_close(struct serio *io)
101{
102	struct amba_kmi_port *kmi = io->port_data;
103
104	writeb(0, KMICR);
105
106	free_irq(kmi->irq, kmi);
107	clk_disable(kmi->clk);
108}
109
110static int amba_kmi_probe(struct amba_device *dev, void *id)
111{
112	struct amba_kmi_port *kmi;
113	struct serio *io;
114	int ret;
115
116	ret = amba_request_regions(dev, NULL);
117	if (ret)
118		return ret;
119
120	kmi = kzalloc(sizeof(struct amba_kmi_port), GFP_KERNEL);
121	io = kzalloc(sizeof(struct serio), GFP_KERNEL);
122	if (!kmi || !io) {
123		ret = -ENOMEM;
124		goto out;
125	}
126
127
128	io->id.type	= SERIO_8042;
129	io->write	= amba_kmi_write;
130	io->open	= amba_kmi_open;
131	io->close	= amba_kmi_close;
132	strlcpy(io->name, dev_name(&dev->dev), sizeof(io->name));
133	strlcpy(io->phys, dev_name(&dev->dev), sizeof(io->phys));
134	io->port_data	= kmi;
135	io->dev.parent	= &dev->dev;
136
137	kmi->io 	= io;
138	kmi->base	= ioremap(dev->res.start, KMI_SIZE);
139	if (!kmi->base) {
140		ret = -ENOMEM;
141		goto out;
142	}
143
144	kmi->clk = clk_get(&dev->dev, "KMIREFCLK");
145	if (IS_ERR(kmi->clk)) {
146		ret = PTR_ERR(kmi->clk);
147		goto unmap;
148	}
149
150	kmi->irq = dev->irq[0];
151	amba_set_drvdata(dev, kmi);
152
153	serio_register_port(kmi->io);
154	return 0;
155
156 unmap:
157	iounmap(kmi->base);
158 out:
159	kfree(kmi);
160	kfree(io);
161	amba_release_regions(dev);
162	return ret;
163}
164
165static int amba_kmi_remove(struct amba_device *dev)
166{
167	struct amba_kmi_port *kmi = amba_get_drvdata(dev);
168
169	amba_set_drvdata(dev, NULL);
170
171	serio_unregister_port(kmi->io);
172	clk_put(kmi->clk);
173	iounmap(kmi->base);
174	kfree(kmi);
175	amba_release_regions(dev);
176	return 0;
177}
178
179static int amba_kmi_resume(struct amba_device *dev)
180{
181	struct amba_kmi_port *kmi = amba_get_drvdata(dev);
182
183	/* kick the serio layer to rescan this port */
184	serio_reconnect(kmi->io);
185
186	return 0;
187}
188
189static struct amba_id amba_kmi_idtable[] = {
190	{
191		.id	= 0x00041050,
192		.mask	= 0x000fffff,
193	},
194	{ 0, 0 }
195};
196
197static struct amba_driver ambakmi_driver = {
198	.drv		= {
199		.name	= "kmi-pl050",
200	},
201	.id_table	= amba_kmi_idtable,
202	.probe		= amba_kmi_probe,
203	.remove		= amba_kmi_remove,
204	.resume		= amba_kmi_resume,
205};
206
207static int __init amba_kmi_init(void)
208{
209	return amba_driver_register(&ambakmi_driver);
210}
211
212static void __exit amba_kmi_exit(void)
213{
214	amba_driver_unregister(&ambakmi_driver);
215}
216
217module_init(amba_kmi_init);
218module_exit(amba_kmi_exit);
219
220MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
221MODULE_DESCRIPTION("AMBA KMI controller driver");
222MODULE_LICENSE("GPL");
223