blob: c6606cacb6a7ad0925d27a51138ad7a9c36ca5ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Russell Kinga62c80e2006-01-07 13:52:45 +000021#include <linux/amba/bus.h>
22#include <linux/amba/kmi.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/io.h>
26#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#define KMI_BASE (kmi->base)
29
30struct amba_kmi_port {
31 struct serio *io;
32 struct clk *clk;
33 void __iomem *base;
34 unsigned int irq;
35 unsigned int divisor;
36 unsigned int open;
37};
38
David Howells7d12e782006-10-05 14:55:46 +010039static irqreturn_t amba_kmi_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 struct amba_kmi_port *kmi = dev_id;
42 unsigned int status = readb(KMIIR);
43 int handled = IRQ_NONE;
44
45 while (status & KMIIR_RXINTR) {
David Howells7d12e782006-10-05 14:55:46 +010046 serio_interrupt(kmi->io, readb(KMIDATA), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 status = readb(KMIIR);
48 handled = IRQ_HANDLED;
49 }
50
51 return handled;
52}
53
54static int amba_kmi_write(struct serio *io, unsigned char val)
55{
56 struct amba_kmi_port *kmi = io->port_data;
57 unsigned int timeleft = 10000; /* timeout in 100ms */
58
Roel Kluin4ab73762009-02-01 16:55:45 -080059 while ((readb(KMISTAT) & KMISTAT_TXEMPTY) == 0 && --timeleft)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 udelay(10);
61
62 if (timeleft)
63 writeb(val, KMIDATA);
64
65 return timeleft ? 0 : SERIO_TIMEOUT;
66}
67
68static int amba_kmi_open(struct serio *io)
69{
70 struct amba_kmi_port *kmi = io->port_data;
71 unsigned int divisor;
72 int ret;
73
Pawel Moll59d1f5c2012-09-19 11:10:47 -070074 ret = clk_prepare_enable(kmi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +000076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 divisor = clk_get_rate(kmi->clk) / 8000000 - 1;
79 writeb(divisor, KMICLKDIV);
80 writeb(KMICR_EN, KMICR);
81
Liviu Dudau5d61b542014-05-13 23:17:12 -070082 ret = request_irq(kmi->irq, amba_kmi_int, IRQF_SHARED, "kmi-pl050",
83 kmi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 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:
Pawel Moll59d1f5c2012-09-19 11:10:47 -070095 clk_disable_unprepare(kmi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 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);
Pawel Moll59d1f5c2012-09-19 11:10:47 -0700107 clk_disable_unprepare(kmi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Bill Pemberton5298cc42012-11-23 21:38:25 -0800110static int amba_kmi_probe(struct amba_device *dev,
Russell Kingaa25afa2011-02-19 15:55:00 +0000111 const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct amba_kmi_port *kmi;
114 struct serio *io;
115 int ret;
116
117 ret = amba_request_regions(dev, NULL);
118 if (ret)
119 return ret;
120
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700121 kmi = kzalloc(sizeof(struct amba_kmi_port), GFP_KERNEL);
122 io = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!kmi || !io) {
124 ret = -ENOMEM;
125 goto out;
126 }
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 io->id.type = SERIO_8042;
130 io->write = amba_kmi_write;
131 io->open = amba_kmi_open;
132 io->close = amba_kmi_close;
Kay Sievers4e8718a2009-01-29 22:56:08 -0800133 strlcpy(io->name, dev_name(&dev->dev), sizeof(io->name));
134 strlcpy(io->phys, dev_name(&dev->dev), sizeof(io->phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 io->port_data = kmi;
136 io->dev.parent = &dev->dev;
137
Dmitry Torokhov266429d2009-12-11 21:50:47 -0800138 kmi->io = io;
Linus Walleijdc890c22009-06-07 23:27:31 +0100139 kmi->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!kmi->base) {
141 ret = -ENOMEM;
142 goto out;
143 }
144
145 kmi->clk = clk_get(&dev->dev, "KMIREFCLK");
146 if (IS_ERR(kmi->clk)) {
147 ret = PTR_ERR(kmi->clk);
148 goto unmap;
149 }
150
151 kmi->irq = dev->irq[0];
152 amba_set_drvdata(dev, kmi);
153
154 serio_register_port(kmi->io);
155 return 0;
156
157 unmap:
158 iounmap(kmi->base);
159 out:
160 kfree(kmi);
161 kfree(io);
162 amba_release_regions(dev);
163 return ret;
164}
165
Bill Pembertone2619cf2012-11-23 21:50:47 -0800166static int amba_kmi_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct amba_kmi_port *kmi = amba_get_drvdata(dev);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 serio_unregister_port(kmi->io);
171 clk_put(kmi->clk);
172 iounmap(kmi->base);
173 kfree(kmi);
174 amba_release_regions(dev);
175 return 0;
176}
177
Ulf Hanssoncee3d8c2015-07-16 10:32:40 -0700178static int __maybe_unused amba_kmi_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Ulf Hanssoncee3d8c2015-07-16 10:32:40 -0700180 struct amba_kmi_port *kmi = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /* kick the serio layer to rescan this port */
183 serio_reconnect(kmi->io);
184
185 return 0;
186}
187
Ulf Hanssoncee3d8c2015-07-16 10:32:40 -0700188static SIMPLE_DEV_PM_OPS(amba_kmi_dev_pm_ops, NULL, amba_kmi_resume);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static struct amba_id amba_kmi_idtable[] = {
191 {
192 .id = 0x00041050,
193 .mask = 0x000fffff,
194 },
195 { 0, 0 }
196};
197
Dave Martin2dfff232011-10-05 15:15:21 +0100198MODULE_DEVICE_TABLE(amba, amba_kmi_idtable);
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static struct amba_driver ambakmi_driver = {
201 .drv = {
202 .name = "kmi-pl050",
Dmitry Torokhov898d1052009-12-11 21:10:18 -0800203 .owner = THIS_MODULE,
Ulf Hanssoncee3d8c2015-07-16 10:32:40 -0700204 .pm = &amba_kmi_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 },
206 .id_table = amba_kmi_idtable,
207 .probe = amba_kmi_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800208 .remove = amba_kmi_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
viresh kumar9e5ed092012-03-15 10:40:38 +0100211module_amba_driver(ambakmi_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
214MODULE_DESCRIPTION("AMBA KMI controller driver");
215MODULE_LICENSE("GPL");