blob: 4408245b61d2c6707bf136fc0d82a7e11795916a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/drivers/input/serio/ambakmi.c
4 *
5 * Copyright (C) 2000-2003 Deep Blue Solutions Ltd.
6 * Copyright (C) 2002 Russell King.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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>
Russell Kinga62c80e2006-01-07 13:52:45 +000017#include <linux/amba/bus.h>
18#include <linux/amba/kmi.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000019#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/io.h>
22#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define KMI_BASE (kmi->base)
25
26struct amba_kmi_port {
27 struct serio *io;
28 struct clk *clk;
29 void __iomem *base;
30 unsigned int irq;
31 unsigned int divisor;
32 unsigned int open;
33};
34
David Howells7d12e782006-10-05 14:55:46 +010035static irqreturn_t amba_kmi_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 struct amba_kmi_port *kmi = dev_id;
38 unsigned int status = readb(KMIIR);
39 int handled = IRQ_NONE;
40
41 while (status & KMIIR_RXINTR) {
David Howells7d12e782006-10-05 14:55:46 +010042 serio_interrupt(kmi->io, readb(KMIDATA), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 status = readb(KMIIR);
44 handled = IRQ_HANDLED;
45 }
46
47 return handled;
48}
49
50static int amba_kmi_write(struct serio *io, unsigned char val)
51{
52 struct amba_kmi_port *kmi = io->port_data;
53 unsigned int timeleft = 10000; /* timeout in 100ms */
54
Roel Kluin4ab73762009-02-01 16:55:45 -080055 while ((readb(KMISTAT) & KMISTAT_TXEMPTY) == 0 && --timeleft)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 udelay(10);
57
58 if (timeleft)
59 writeb(val, KMIDATA);
60
61 return timeleft ? 0 : SERIO_TIMEOUT;
62}
63
64static int amba_kmi_open(struct serio *io)
65{
66 struct amba_kmi_port *kmi = io->port_data;
67 unsigned int divisor;
68 int ret;
69
Pawel Moll59d1f5c2012-09-19 11:10:47 -070070 ret = clk_prepare_enable(kmi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +000072 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 divisor = clk_get_rate(kmi->clk) / 8000000 - 1;
75 writeb(divisor, KMICLKDIV);
76 writeb(KMICR_EN, KMICR);
77
Liviu Dudau5d61b542014-05-13 23:17:12 -070078 ret = request_irq(kmi->irq, amba_kmi_int, IRQF_SHARED, "kmi-pl050",
79 kmi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (ret) {
81 printk(KERN_ERR "kmi: failed to claim IRQ%d\n", kmi->irq);
82 writeb(0, KMICR);
83 goto clk_disable;
84 }
85
86 writeb(KMICR_EN | KMICR_RXINTREN, KMICR);
87
88 return 0;
89
90 clk_disable:
Pawel Moll59d1f5c2012-09-19 11:10:47 -070091 clk_disable_unprepare(kmi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 out:
93 return ret;
94}
95
96static void amba_kmi_close(struct serio *io)
97{
98 struct amba_kmi_port *kmi = io->port_data;
99
100 writeb(0, KMICR);
101
102 free_irq(kmi->irq, kmi);
Pawel Moll59d1f5c2012-09-19 11:10:47 -0700103 clk_disable_unprepare(kmi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Bill Pemberton5298cc42012-11-23 21:38:25 -0800106static int amba_kmi_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +0000107 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 struct amba_kmi_port *kmi;
110 struct serio *io;
111 int ret;
112
113 ret = amba_request_regions(dev, NULL);
114 if (ret)
115 return ret;
116
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700117 kmi = kzalloc(sizeof(struct amba_kmi_port), GFP_KERNEL);
118 io = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (!kmi || !io) {
120 ret = -ENOMEM;
121 goto out;
122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 io->id.type = SERIO_8042;
126 io->write = amba_kmi_write;
127 io->open = amba_kmi_open;
128 io->close = amba_kmi_close;
Kay Sievers4e8718a2009-01-29 22:56:08 -0800129 strlcpy(io->name, dev_name(&dev->dev), sizeof(io->name));
130 strlcpy(io->phys, dev_name(&dev->dev), sizeof(io->phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 io->port_data = kmi;
132 io->dev.parent = &dev->dev;
133
Dmitry Torokhov266429d2009-12-11 21:50:47 -0800134 kmi->io = io;
Linus Walleijdc890c22009-06-07 23:27:31 +0100135 kmi->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!kmi->base) {
137 ret = -ENOMEM;
138 goto out;
139 }
140
141 kmi->clk = clk_get(&dev->dev, "KMIREFCLK");
142 if (IS_ERR(kmi->clk)) {
143 ret = PTR_ERR(kmi->clk);
144 goto unmap;
145 }
146
147 kmi->irq = dev->irq[0];
148 amba_set_drvdata(dev, kmi);
149
150 serio_register_port(kmi->io);
151 return 0;
152
153 unmap:
154 iounmap(kmi->base);
155 out:
156 kfree(kmi);
157 kfree(io);
158 amba_release_regions(dev);
159 return ret;
160}
161
Uwe Kleine-König3fd269e2021-01-26 17:58:34 +0100162static void amba_kmi_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct amba_kmi_port *kmi = amba_get_drvdata(dev);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 serio_unregister_port(kmi->io);
167 clk_put(kmi->clk);
168 iounmap(kmi->base);
169 kfree(kmi);
170 amba_release_regions(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Ulf Hanssoncee3d8c2015-07-16 10:32:40 -0700173static int __maybe_unused amba_kmi_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Ulf Hanssoncee3d8c2015-07-16 10:32:40 -0700175 struct amba_kmi_port *kmi = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* kick the serio layer to rescan this port */
178 serio_reconnect(kmi->io);
179
180 return 0;
181}
182
Ulf Hanssoncee3d8c2015-07-16 10:32:40 -0700183static SIMPLE_DEV_PM_OPS(amba_kmi_dev_pm_ops, NULL, amba_kmi_resume);
184
Arvind Yadavb54bf2f2017-08-23 11:37:05 -0700185static const struct amba_id amba_kmi_idtable[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 {
187 .id = 0x00041050,
188 .mask = 0x000fffff,
189 },
190 { 0, 0 }
191};
192
Dave Martin2dfff232011-10-05 15:15:21 +0100193MODULE_DEVICE_TABLE(amba, amba_kmi_idtable);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static struct amba_driver ambakmi_driver = {
196 .drv = {
197 .name = "kmi-pl050",
Dmitry Torokhov898d1052009-12-11 21:10:18 -0800198 .owner = THIS_MODULE,
Ulf Hanssoncee3d8c2015-07-16 10:32:40 -0700199 .pm = &amba_kmi_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 },
201 .id_table = amba_kmi_idtable,
202 .probe = amba_kmi_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800203 .remove = amba_kmi_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
viresh kumar9e5ed092012-03-15 10:40:38 +0100206module_amba_driver(ambakmi_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
209MODULE_DESCRIPTION("AMBA KMI controller driver");
210MODULE_LICENSE("GPL");