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/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> 21 #include <linux/amba/bus.h> 22 #include <linux/amba/kmi.h> 23 #include <linux/clk.h> 24 25 #include <asm/io.h> 26 #include <asm/irq.h> 27 28 #define KMI_BASE (kmi->base) 29 30 struct 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 39 static irqreturn_t amba_kmi_int(int irq, void *dev_id) 40 { 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) { 46 serio_interrupt(kmi->io, readb(KMIDATA), 0); 47 status = readb(KMIIR); 48 handled = IRQ_HANDLED; 49 } 50 51 return handled; 52 } 53 54 static 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 59 while ((readb(KMISTAT) & KMISTAT_TXEMPTY) == 0 && --timeleft) 60 udelay(10); 61 62 if (timeleft) 63 writeb(val, KMIDATA); 64 65 return timeleft ? 0 : SERIO_TIMEOUT; 66 } 67 68 static 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 74 ret = clk_prepare_enable(kmi->clk); 75 if (ret) 76 goto out; 77 78 divisor = clk_get_rate(kmi->clk) / 8000000 - 1; 79 writeb(divisor, KMICLKDIV); 80 writeb(KMICR_EN, KMICR); 81 82 ret = request_irq(kmi->irq, amba_kmi_int, 0, "kmi-pl050", kmi); 83 if (ret) { 84 printk(KERN_ERR "kmi: failed to claim IRQ%d\n", kmi->irq); 85 writeb(0, KMICR); 86 goto clk_disable; 87 } 88 89 writeb(KMICR_EN | KMICR_RXINTREN, KMICR); 90 91 return 0; 92 93 clk_disable: 94 clk_disable_unprepare(kmi->clk); 95 out: 96 return ret; 97 } 98 99 static void amba_kmi_close(struct serio *io) 100 { 101 struct amba_kmi_port *kmi = io->port_data; 102 103 writeb(0, KMICR); 104 105 free_irq(kmi->irq, kmi); 106 clk_disable_unprepare(kmi->clk); 107 } 108 109 static int amba_kmi_probe(struct amba_device *dev, 110 const struct amba_id *id) 111 { 112 struct amba_kmi_port *kmi; 113 struct serio *io; 114 int ret; 115 116 ret = amba_request_regions(dev, NULL); 117 if (ret) 118 return ret; 119 120 kmi = kzalloc(sizeof(struct amba_kmi_port), GFP_KERNEL); 121 io = kzalloc(sizeof(struct serio), GFP_KERNEL); 122 if (!kmi || !io) { 123 ret = -ENOMEM; 124 goto out; 125 } 126 127 128 io->id.type = SERIO_8042; 129 io->write = amba_kmi_write; 130 io->open = amba_kmi_open; 131 io->close = amba_kmi_close; 132 strlcpy(io->name, dev_name(&dev->dev), sizeof(io->name)); 133 strlcpy(io->phys, dev_name(&dev->dev), sizeof(io->phys)); 134 io->port_data = kmi; 135 io->dev.parent = &dev->dev; 136 137 kmi->io = io; 138 kmi->base = ioremap(dev->res.start, resource_size(&dev->res)); 139 if (!kmi->base) { 140 ret = -ENOMEM; 141 goto out; 142 } 143 144 kmi->clk = clk_get(&dev->dev, "KMIREFCLK"); 145 if (IS_ERR(kmi->clk)) { 146 ret = PTR_ERR(kmi->clk); 147 goto unmap; 148 } 149 150 kmi->irq = dev->irq[0]; 151 amba_set_drvdata(dev, kmi); 152 153 serio_register_port(kmi->io); 154 return 0; 155 156 unmap: 157 iounmap(kmi->base); 158 out: 159 kfree(kmi); 160 kfree(io); 161 amba_release_regions(dev); 162 return ret; 163 } 164 165 static int amba_kmi_remove(struct amba_device *dev) 166 { 167 struct amba_kmi_port *kmi = amba_get_drvdata(dev); 168 169 serio_unregister_port(kmi->io); 170 clk_put(kmi->clk); 171 iounmap(kmi->base); 172 kfree(kmi); 173 amba_release_regions(dev); 174 return 0; 175 } 176 177 static int amba_kmi_resume(struct amba_device *dev) 178 { 179 struct amba_kmi_port *kmi = amba_get_drvdata(dev); 180 181 /* kick the serio layer to rescan this port */ 182 serio_reconnect(kmi->io); 183 184 return 0; 185 } 186 187 static struct amba_id amba_kmi_idtable[] = { 188 { 189 .id = 0x00041050, 190 .mask = 0x000fffff, 191 }, 192 { 0, 0 } 193 }; 194 195 MODULE_DEVICE_TABLE(amba, amba_kmi_idtable); 196 197 static struct amba_driver ambakmi_driver = { 198 .drv = { 199 .name = "kmi-pl050", 200 .owner = THIS_MODULE, 201 }, 202 .id_table = amba_kmi_idtable, 203 .probe = amba_kmi_probe, 204 .remove = amba_kmi_remove, 205 .resume = amba_kmi_resume, 206 }; 207 208 module_amba_driver(ambakmi_driver); 209 210 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); 211 MODULE_DESCRIPTION("AMBA KMI controller driver"); 212 MODULE_LICENSE("GPL"); 213