Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/drivers/input/serio/ambakmi.c |
| 4 | * |
| 5 | * Copyright (C) 2000-2003 Deep Blue Solutions Ltd. |
| 6 | * Copyright (C) 2002 Russell King. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Russell King | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 17 | #include <linux/amba/bus.h> |
| 18 | #include <linux/amba/kmi.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 19 | #include <linux/clk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/io.h> |
| 22 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #define KMI_BASE (kmi->base) |
| 25 | |
| 26 | struct 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 Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 35 | static irqreturn_t amba_kmi_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 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 Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 42 | serio_interrupt(kmi->io, readb(KMIDATA), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | status = readb(KMIIR); |
| 44 | handled = IRQ_HANDLED; |
| 45 | } |
| 46 | |
| 47 | return handled; |
| 48 | } |
| 49 | |
| 50 | static 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 Kluin | 4ab7376 | 2009-02-01 16:55:45 -0800 | [diff] [blame] | 55 | while ((readb(KMISTAT) & KMISTAT_TXEMPTY) == 0 && --timeleft) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | udelay(10); |
| 57 | |
| 58 | if (timeleft) |
| 59 | writeb(val, KMIDATA); |
| 60 | |
| 61 | return timeleft ? 0 : SERIO_TIMEOUT; |
| 62 | } |
| 63 | |
| 64 | static 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 Moll | 59d1f5c | 2012-09-19 11:10:47 -0700 | [diff] [blame] | 70 | ret = clk_prepare_enable(kmi->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if (ret) |
Russell King | a8d3584 | 2006-01-03 18:41:37 +0000 | [diff] [blame] | 72 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | divisor = clk_get_rate(kmi->clk) / 8000000 - 1; |
| 75 | writeb(divisor, KMICLKDIV); |
| 76 | writeb(KMICR_EN, KMICR); |
| 77 | |
Liviu Dudau | 5d61b54 | 2014-05-13 23:17:12 -0700 | [diff] [blame] | 78 | ret = request_irq(kmi->irq, amba_kmi_int, IRQF_SHARED, "kmi-pl050", |
| 79 | kmi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | 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 Moll | 59d1f5c | 2012-09-19 11:10:47 -0700 | [diff] [blame] | 91 | clk_disable_unprepare(kmi->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | out: |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static 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 Moll | 59d1f5c | 2012-09-19 11:10:47 -0700 | [diff] [blame] | 103 | clk_disable_unprepare(kmi->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 106 | static int amba_kmi_probe(struct amba_device *dev, |
Russell King | aa25afa | 2011-02-19 15:55:00 +0000 | [diff] [blame] | 107 | const struct amba_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 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 Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 117 | kmi = kzalloc(sizeof(struct amba_kmi_port), GFP_KERNEL); |
| 118 | io = kzalloc(sizeof(struct serio), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | if (!kmi || !io) { |
| 120 | ret = -ENOMEM; |
| 121 | goto out; |
| 122 | } |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 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 Sievers | 4e8718a | 2009-01-29 22:56:08 -0800 | [diff] [blame] | 129 | strlcpy(io->name, dev_name(&dev->dev), sizeof(io->name)); |
| 130 | strlcpy(io->phys, dev_name(&dev->dev), sizeof(io->phys)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | io->port_data = kmi; |
| 132 | io->dev.parent = &dev->dev; |
| 133 | |
Dmitry Torokhov | 266429d | 2009-12-11 21:50:47 -0800 | [diff] [blame] | 134 | kmi->io = io; |
Linus Walleij | dc890c2 | 2009-06-07 23:27:31 +0100 | [diff] [blame] | 135 | kmi->base = ioremap(dev->res.start, resource_size(&dev->res)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | 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önig | 3fd269e | 2021-01-26 17:58:34 +0100 | [diff] [blame] | 162 | static void amba_kmi_remove(struct amba_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | struct amba_kmi_port *kmi = amba_get_drvdata(dev); |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | serio_unregister_port(kmi->io); |
| 167 | clk_put(kmi->clk); |
| 168 | iounmap(kmi->base); |
| 169 | kfree(kmi); |
| 170 | amba_release_regions(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Ulf Hansson | cee3d8c | 2015-07-16 10:32:40 -0700 | [diff] [blame] | 173 | static int __maybe_unused amba_kmi_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Ulf Hansson | cee3d8c | 2015-07-16 10:32:40 -0700 | [diff] [blame] | 175 | struct amba_kmi_port *kmi = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | /* kick the serio layer to rescan this port */ |
| 178 | serio_reconnect(kmi->io); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Ulf Hansson | cee3d8c | 2015-07-16 10:32:40 -0700 | [diff] [blame] | 183 | static SIMPLE_DEV_PM_OPS(amba_kmi_dev_pm_ops, NULL, amba_kmi_resume); |
| 184 | |
Arvind Yadav | b54bf2f | 2017-08-23 11:37:05 -0700 | [diff] [blame] | 185 | static const struct amba_id amba_kmi_idtable[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
| 187 | .id = 0x00041050, |
| 188 | .mask = 0x000fffff, |
| 189 | }, |
| 190 | { 0, 0 } |
| 191 | }; |
| 192 | |
Dave Martin | 2dfff23 | 2011-10-05 15:15:21 +0100 | [diff] [blame] | 193 | MODULE_DEVICE_TABLE(amba, amba_kmi_idtable); |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | static struct amba_driver ambakmi_driver = { |
| 196 | .drv = { |
| 197 | .name = "kmi-pl050", |
Dmitry Torokhov | 898d105 | 2009-12-11 21:10:18 -0800 | [diff] [blame] | 198 | .owner = THIS_MODULE, |
Ulf Hansson | cee3d8c | 2015-07-16 10:32:40 -0700 | [diff] [blame] | 199 | .pm = &amba_kmi_dev_pm_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | }, |
| 201 | .id_table = amba_kmi_idtable, |
| 202 | .probe = amba_kmi_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 203 | .remove = amba_kmi_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
viresh kumar | 9e5ed09 | 2012-03-15 10:40:38 +0100 | [diff] [blame] | 206 | module_amba_driver(ambakmi_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); |
| 209 | MODULE_DESCRIPTION("AMBA KMI controller driver"); |
| 210 | MODULE_LICENSE("GPL"); |