Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/serio.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/ioport.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/err.h> |
Russell King | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 22 | #include <linux/amba/bus.h> |
| 23 | #include <linux/amba/kmi.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 24 | #include <linux/clk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <asm/io.h> |
| 27 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #define KMI_BASE (kmi->base) |
| 30 | |
| 31 | struct amba_kmi_port { |
| 32 | struct serio *io; |
| 33 | struct clk *clk; |
| 34 | void __iomem *base; |
| 35 | unsigned int irq; |
| 36 | unsigned int divisor; |
| 37 | unsigned int open; |
| 38 | }; |
| 39 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 40 | static irqreturn_t amba_kmi_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | struct amba_kmi_port *kmi = dev_id; |
| 43 | unsigned int status = readb(KMIIR); |
| 44 | int handled = IRQ_NONE; |
| 45 | |
| 46 | while (status & KMIIR_RXINTR) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 47 | serio_interrupt(kmi->io, readb(KMIDATA), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | status = readb(KMIIR); |
| 49 | handled = IRQ_HANDLED; |
| 50 | } |
| 51 | |
| 52 | return handled; |
| 53 | } |
| 54 | |
| 55 | static int amba_kmi_write(struct serio *io, unsigned char val) |
| 56 | { |
| 57 | struct amba_kmi_port *kmi = io->port_data; |
| 58 | unsigned int timeleft = 10000; /* timeout in 100ms */ |
| 59 | |
Roel Kluin | 4ab7376 | 2009-02-01 16:55:45 -0800 | [diff] [blame] | 60 | while ((readb(KMISTAT) & KMISTAT_TXEMPTY) == 0 && --timeleft) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | udelay(10); |
| 62 | |
| 63 | if (timeleft) |
| 64 | writeb(val, KMIDATA); |
| 65 | |
| 66 | return timeleft ? 0 : SERIO_TIMEOUT; |
| 67 | } |
| 68 | |
| 69 | static int amba_kmi_open(struct serio *io) |
| 70 | { |
| 71 | struct amba_kmi_port *kmi = io->port_data; |
| 72 | unsigned int divisor; |
| 73 | int ret; |
| 74 | |
Pawel Moll | 59d1f5c | 2012-09-19 11:10:47 -0700 | [diff] [blame] | 75 | ret = clk_prepare_enable(kmi->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (ret) |
Russell King | a8d3584 | 2006-01-03 18:41:37 +0000 | [diff] [blame] | 77 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | divisor = clk_get_rate(kmi->clk) / 8000000 - 1; |
| 80 | writeb(divisor, KMICLKDIV); |
| 81 | writeb(KMICR_EN, KMICR); |
| 82 | |
| 83 | ret = request_irq(kmi->irq, amba_kmi_int, 0, "kmi-pl050", kmi); |
| 84 | 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 Moll | 59d1f5c | 2012-09-19 11:10:47 -0700 | [diff] [blame] | 95 | clk_disable_unprepare(kmi->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | out: |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static 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 Moll | 59d1f5c | 2012-09-19 11:10:47 -0700 | [diff] [blame] | 107 | clk_disable_unprepare(kmi->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Russell King | aa25afa | 2011-02-19 15:55:00 +0000 | [diff] [blame] | 110 | static int __devinit amba_kmi_probe(struct amba_device *dev, |
| 111 | const struct amba_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
| 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 Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 121 | kmi = kzalloc(sizeof(struct amba_kmi_port), GFP_KERNEL); |
| 122 | io = kzalloc(sizeof(struct serio), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | if (!kmi || !io) { |
| 124 | ret = -ENOMEM; |
| 125 | goto out; |
| 126 | } |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 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 Sievers | 4e8718a | 2009-01-29 22:56:08 -0800 | [diff] [blame] | 133 | strlcpy(io->name, dev_name(&dev->dev), sizeof(io->name)); |
| 134 | strlcpy(io->phys, dev_name(&dev->dev), sizeof(io->phys)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | io->port_data = kmi; |
| 136 | io->dev.parent = &dev->dev; |
| 137 | |
Dmitry Torokhov | 266429d | 2009-12-11 21:50:47 -0800 | [diff] [blame] | 138 | kmi->io = io; |
Linus Walleij | dc890c2 | 2009-06-07 23:27:31 +0100 | [diff] [blame] | 139 | kmi->base = ioremap(dev->res.start, resource_size(&dev->res)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | 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 | |
Dmitry Torokhov | 266429d | 2009-12-11 21:50:47 -0800 | [diff] [blame] | 166 | static int __devexit amba_kmi_remove(struct amba_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
| 168 | struct amba_kmi_port *kmi = amba_get_drvdata(dev); |
| 169 | |
| 170 | amba_set_drvdata(dev, NULL); |
| 171 | |
| 172 | serio_unregister_port(kmi->io); |
| 173 | clk_put(kmi->clk); |
| 174 | iounmap(kmi->base); |
| 175 | kfree(kmi); |
| 176 | amba_release_regions(dev); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int amba_kmi_resume(struct amba_device *dev) |
| 181 | { |
| 182 | struct amba_kmi_port *kmi = amba_get_drvdata(dev); |
| 183 | |
| 184 | /* kick the serio layer to rescan this port */ |
| 185 | serio_reconnect(kmi->io); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static struct amba_id amba_kmi_idtable[] = { |
| 191 | { |
| 192 | .id = 0x00041050, |
| 193 | .mask = 0x000fffff, |
| 194 | }, |
| 195 | { 0, 0 } |
| 196 | }; |
| 197 | |
Dave Martin | 2dfff23 | 2011-10-05 15:15:21 +0100 | [diff] [blame] | 198 | MODULE_DEVICE_TABLE(amba, amba_kmi_idtable); |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | static struct amba_driver ambakmi_driver = { |
| 201 | .drv = { |
| 202 | .name = "kmi-pl050", |
Dmitry Torokhov | 898d105 | 2009-12-11 21:10:18 -0800 | [diff] [blame] | 203 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | }, |
| 205 | .id_table = amba_kmi_idtable, |
| 206 | .probe = amba_kmi_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame^] | 207 | .remove = amba_kmi_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | .resume = amba_kmi_resume, |
| 209 | }; |
| 210 | |
viresh kumar | 9e5ed09 | 2012-03-15 10:40:38 +0100 | [diff] [blame] | 211 | module_amba_driver(ambakmi_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); |
| 214 | MODULE_DESCRIPTION("AMBA KMI controller driver"); |
| 215 | MODULE_LICENSE("GPL"); |