1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/input/serio/ambakmi.c
5 * Copyright (C) 2000-2003 Deep Blue Solutions Ltd.
6 * Copyright (C) 2002 Russell King.
8 #include <linux/module.h>
9 #include <linux/serio.h>
10 #include <linux/errno.h>
11 #include <linux/interrupt.h>
12 #include <linux/ioport.h>
13 #include <linux/device.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/amba/bus.h>
18 #include <linux/amba/kmi.h>
19 #include <linux/clk.h>
24 #define KMI_BASE (kmi->base)
26 struct amba_kmi_port
{
35 static irqreturn_t
amba_kmi_int(int irq
, void *dev_id
)
37 struct amba_kmi_port
*kmi
= dev_id
;
38 unsigned int status
= readb(KMIIR
);
39 int handled
= IRQ_NONE
;
41 while (status
& KMIIR_RXINTR
) {
42 serio_interrupt(kmi
->io
, readb(KMIDATA
), 0);
43 status
= readb(KMIIR
);
44 handled
= IRQ_HANDLED
;
50 static int amba_kmi_write(struct serio
*io
, unsigned char val
)
52 struct amba_kmi_port
*kmi
= io
->port_data
;
53 unsigned int timeleft
= 10000; /* timeout in 100ms */
55 while ((readb(KMISTAT
) & KMISTAT_TXEMPTY
) == 0 && --timeleft
)
61 return timeleft
? 0 : SERIO_TIMEOUT
;
64 static int amba_kmi_open(struct serio
*io
)
66 struct amba_kmi_port
*kmi
= io
->port_data
;
70 ret
= clk_prepare_enable(kmi
->clk
);
74 divisor
= clk_get_rate(kmi
->clk
) / 8000000 - 1;
75 writeb(divisor
, KMICLKDIV
);
76 writeb(KMICR_EN
, KMICR
);
78 ret
= request_irq(kmi
->irq
, amba_kmi_int
, IRQF_SHARED
, "kmi-pl050",
81 printk(KERN_ERR
"kmi: failed to claim IRQ%d\n", kmi
->irq
);
86 writeb(KMICR_EN
| KMICR_RXINTREN
, KMICR
);
91 clk_disable_unprepare(kmi
->clk
);
96 static void amba_kmi_close(struct serio
*io
)
98 struct amba_kmi_port
*kmi
= io
->port_data
;
102 free_irq(kmi
->irq
, kmi
);
103 clk_disable_unprepare(kmi
->clk
);
106 static int amba_kmi_probe(struct amba_device
*dev
,
107 const struct amba_id
*id
)
109 struct amba_kmi_port
*kmi
;
113 ret
= amba_request_regions(dev
, NULL
);
117 kmi
= kzalloc(sizeof(struct amba_kmi_port
), GFP_KERNEL
);
118 io
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
125 io
->id
.type
= SERIO_8042
;
126 io
->write
= amba_kmi_write
;
127 io
->open
= amba_kmi_open
;
128 io
->close
= amba_kmi_close
;
129 strlcpy(io
->name
, dev_name(&dev
->dev
), sizeof(io
->name
));
130 strlcpy(io
->phys
, dev_name(&dev
->dev
), sizeof(io
->phys
));
132 io
->dev
.parent
= &dev
->dev
;
135 kmi
->base
= ioremap(dev
->res
.start
, resource_size(&dev
->res
));
141 kmi
->clk
= clk_get(&dev
->dev
, "KMIREFCLK");
142 if (IS_ERR(kmi
->clk
)) {
143 ret
= PTR_ERR(kmi
->clk
);
147 kmi
->irq
= dev
->irq
[0];
148 amba_set_drvdata(dev
, kmi
);
150 serio_register_port(kmi
->io
);
158 amba_release_regions(dev
);
162 static int amba_kmi_remove(struct amba_device
*dev
)
164 struct amba_kmi_port
*kmi
= amba_get_drvdata(dev
);
166 serio_unregister_port(kmi
->io
);
170 amba_release_regions(dev
);
174 static int __maybe_unused
amba_kmi_resume(struct device
*dev
)
176 struct amba_kmi_port
*kmi
= dev_get_drvdata(dev
);
178 /* kick the serio layer to rescan this port */
179 serio_reconnect(kmi
->io
);
184 static SIMPLE_DEV_PM_OPS(amba_kmi_dev_pm_ops
, NULL
, amba_kmi_resume
);
186 static const struct amba_id amba_kmi_idtable
[] = {
194 MODULE_DEVICE_TABLE(amba
, amba_kmi_idtable
);
196 static struct amba_driver ambakmi_driver
= {
199 .owner
= THIS_MODULE
,
200 .pm
= &amba_kmi_dev_pm_ops
,
202 .id_table
= amba_kmi_idtable
,
203 .probe
= amba_kmi_probe
,
204 .remove
= amba_kmi_remove
,
207 module_amba_driver(ambakmi_driver
);
209 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
210 MODULE_DESCRIPTION("AMBA KMI controller driver");
211 MODULE_LICENSE("GPL");