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/serio.h>
14 #include <linux/errno.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/amba/bus.h>
22 #include <linux/amba/kmi.h>
23 #include <linux/clk.h>
28 #define KMI_BASE (kmi->base)
30 struct amba_kmi_port
{
39 static irqreturn_t
amba_kmi_int(int irq
, void *dev_id
)
41 struct amba_kmi_port
*kmi
= dev_id
;
42 unsigned int status
= readb(KMIIR
);
43 int handled
= IRQ_NONE
;
45 while (status
& KMIIR_RXINTR
) {
46 serio_interrupt(kmi
->io
, readb(KMIDATA
), 0);
47 status
= readb(KMIIR
);
48 handled
= IRQ_HANDLED
;
54 static int amba_kmi_write(struct serio
*io
, unsigned char val
)
56 struct amba_kmi_port
*kmi
= io
->port_data
;
57 unsigned int timeleft
= 10000; /* timeout in 100ms */
59 while ((readb(KMISTAT
) & KMISTAT_TXEMPTY
) == 0 && --timeleft
)
65 return timeleft
? 0 : SERIO_TIMEOUT
;
68 static int amba_kmi_open(struct serio
*io
)
70 struct amba_kmi_port
*kmi
= io
->port_data
;
74 ret
= clk_prepare_enable(kmi
->clk
);
78 divisor
= clk_get_rate(kmi
->clk
) / 8000000 - 1;
79 writeb(divisor
, KMICLKDIV
);
80 writeb(KMICR_EN
, KMICR
);
82 ret
= request_irq(kmi
->irq
, amba_kmi_int
, IRQF_SHARED
, "kmi-pl050",
85 printk(KERN_ERR
"kmi: failed to claim IRQ%d\n", kmi
->irq
);
90 writeb(KMICR_EN
| KMICR_RXINTREN
, KMICR
);
95 clk_disable_unprepare(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_unprepare(kmi
->clk
);
110 static int amba_kmi_probe(struct amba_device
*dev
,
111 const struct amba_id
*id
)
113 struct amba_kmi_port
*kmi
;
117 ret
= amba_request_regions(dev
, NULL
);
121 kmi
= kzalloc(sizeof(struct amba_kmi_port
), GFP_KERNEL
);
122 io
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
129 io
->id
.type
= SERIO_8042
;
130 io
->write
= amba_kmi_write
;
131 io
->open
= amba_kmi_open
;
132 io
->close
= amba_kmi_close
;
133 strlcpy(io
->name
, dev_name(&dev
->dev
), sizeof(io
->name
));
134 strlcpy(io
->phys
, dev_name(&dev
->dev
), sizeof(io
->phys
));
136 io
->dev
.parent
= &dev
->dev
;
139 kmi
->base
= ioremap(dev
->res
.start
, resource_size(&dev
->res
));
145 kmi
->clk
= clk_get(&dev
->dev
, "KMIREFCLK");
146 if (IS_ERR(kmi
->clk
)) {
147 ret
= PTR_ERR(kmi
->clk
);
151 kmi
->irq
= dev
->irq
[0];
152 amba_set_drvdata(dev
, kmi
);
154 serio_register_port(kmi
->io
);
162 amba_release_regions(dev
);
166 static int amba_kmi_remove(struct amba_device
*dev
)
168 struct amba_kmi_port
*kmi
= amba_get_drvdata(dev
);
170 serio_unregister_port(kmi
->io
);
174 amba_release_regions(dev
);
178 static int __maybe_unused
amba_kmi_resume(struct device
*dev
)
180 struct amba_kmi_port
*kmi
= dev_get_drvdata(dev
);
182 /* kick the serio layer to rescan this port */
183 serio_reconnect(kmi
->io
);
188 static SIMPLE_DEV_PM_OPS(amba_kmi_dev_pm_ops
, NULL
, amba_kmi_resume
);
190 static const struct amba_id amba_kmi_idtable
[] = {
198 MODULE_DEVICE_TABLE(amba
, amba_kmi_idtable
);
200 static struct amba_driver ambakmi_driver
= {
203 .owner
= THIS_MODULE
,
204 .pm
= &amba_kmi_dev_pm_ops
,
206 .id_table
= amba_kmi_idtable
,
207 .probe
= amba_kmi_probe
,
208 .remove
= amba_kmi_remove
,
211 module_amba_driver(ambakmi_driver
);
213 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
214 MODULE_DESCRIPTION("AMBA KMI controller driver");
215 MODULE_LICENSE("GPL");