2 * linux/drivers/input/serio/ambakmi.c
4 * Copyright (C) 2000-2003 Deep Blue Solutions Ltd.
5 * Copyright (C) 2002 Russell King.
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.
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>
29 #define KMI_BASE (kmi->base)
31 struct amba_kmi_port
{
40 static irqreturn_t
amba_kmi_int(int irq
, void *dev_id
, struct pt_regs
*regs
)
42 struct amba_kmi_port
*kmi
= dev_id
;
43 unsigned int status
= readb(KMIIR
);
44 int handled
= IRQ_NONE
;
46 while (status
& KMIIR_RXINTR
) {
47 serio_interrupt(kmi
->io
, readb(KMIDATA
), 0, regs
);
48 status
= readb(KMIIR
);
49 handled
= IRQ_HANDLED
;
55 static int amba_kmi_write(struct serio
*io
, unsigned char val
)
57 struct amba_kmi_port
*kmi
= io
->port_data
;
58 unsigned int timeleft
= 10000; /* timeout in 100ms */
60 while ((readb(KMISTAT
) & KMISTAT_TXEMPTY
) == 0 && timeleft
--)
66 return timeleft
? 0 : SERIO_TIMEOUT
;
69 static int amba_kmi_open(struct serio
*io
)
71 struct amba_kmi_port
*kmi
= io
->port_data
;
75 ret
= clk_enable(kmi
->clk
);
79 divisor
= clk_get_rate(kmi
->clk
) / 8000000 - 1;
80 writeb(divisor
, KMICLKDIV
);
81 writeb(KMICR_EN
, KMICR
);
83 ret
= request_irq(kmi
->irq
, amba_kmi_int
, 0, "kmi-pl050", kmi
);
85 printk(KERN_ERR
"kmi: failed to claim IRQ%d\n", kmi
->irq
);
90 writeb(KMICR_EN
| KMICR_RXINTREN
, KMICR
);
95 clk_disable(kmi
->clk
);
100 static void amba_kmi_close(struct serio
*io
)
102 struct amba_kmi_port
*kmi
= io
->port_data
;
106 free_irq(kmi
->irq
, kmi
);
107 clk_disable(kmi
->clk
);
110 static int amba_kmi_probe(struct amba_device
*dev
, void *id
)
112 struct amba_kmi_port
*kmi
;
116 ret
= amba_request_regions(dev
, NULL
);
120 kmi
= kmalloc(sizeof(struct amba_kmi_port
), GFP_KERNEL
);
121 io
= kmalloc(sizeof(struct serio
), GFP_KERNEL
);
127 memset(kmi
, 0, sizeof(struct amba_kmi_port
));
128 memset(io
, 0, sizeof(struct serio
));
130 io
->id
.type
= SERIO_8042
;
131 io
->write
= amba_kmi_write
;
132 io
->open
= amba_kmi_open
;
133 io
->close
= amba_kmi_close
;
134 strlcpy(io
->name
, dev
->dev
.bus_id
, sizeof(io
->name
));
135 strlcpy(io
->phys
, dev
->dev
.bus_id
, sizeof(io
->phys
));
137 io
->dev
.parent
= &dev
->dev
;
140 kmi
->base
= ioremap(dev
->res
.start
, KMI_SIZE
);
146 kmi
->clk
= clk_get(&dev
->dev
, "KMIREFCLK");
147 if (IS_ERR(kmi
->clk
)) {
148 ret
= PTR_ERR(kmi
->clk
);
152 kmi
->irq
= dev
->irq
[0];
153 amba_set_drvdata(dev
, kmi
);
155 serio_register_port(kmi
->io
);
163 amba_release_regions(dev
);
167 static int amba_kmi_remove(struct amba_device
*dev
)
169 struct amba_kmi_port
*kmi
= amba_get_drvdata(dev
);
171 amba_set_drvdata(dev
, NULL
);
173 serio_unregister_port(kmi
->io
);
177 amba_release_regions(dev
);
181 static int amba_kmi_resume(struct amba_device
*dev
)
183 struct amba_kmi_port
*kmi
= amba_get_drvdata(dev
);
185 /* kick the serio layer to rescan this port */
186 serio_reconnect(kmi
->io
);
191 static struct amba_id amba_kmi_idtable
[] = {
199 static struct amba_driver ambakmi_driver
= {
203 .id_table
= amba_kmi_idtable
,
204 .probe
= amba_kmi_probe
,
205 .remove
= amba_kmi_remove
,
206 .resume
= amba_kmi_resume
,
209 static int __init
amba_kmi_init(void)
211 return amba_driver_register(&ambakmi_driver
);
214 static void __exit
amba_kmi_exit(void)
216 amba_driver_unregister(&ambakmi_driver
);
219 module_init(amba_kmi_init
);
220 module_exit(amba_kmi_exit
);
222 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
223 MODULE_DESCRIPTION("AMBA KMI controller driver");
224 MODULE_LICENSE("GPL");