1422b552dSJavier Herrero /* 2422b552dSJavier Herrero * OpenCores Keyboard Controller Driver 3422b552dSJavier Herrero * http://www.opencores.org/project,keyboardcontroller 4422b552dSJavier Herrero * 5422b552dSJavier Herrero * Copyright 2007-2009 HV Sistemas S.L. 6422b552dSJavier Herrero * 7422b552dSJavier Herrero * Licensed under the GPL-2 or later. 8422b552dSJavier Herrero */ 9422b552dSJavier Herrero 10422b552dSJavier Herrero #include <linux/input.h> 11422b552dSJavier Herrero #include <linux/interrupt.h> 12422b552dSJavier Herrero #include <linux/io.h> 13422b552dSJavier Herrero #include <linux/ioport.h> 14422b552dSJavier Herrero #include <linux/kernel.h> 15422b552dSJavier Herrero #include <linux/module.h> 16422b552dSJavier Herrero #include <linux/platform_device.h> 17*5a0e3ad6STejun Heo #include <linux/slab.h> 18422b552dSJavier Herrero 19422b552dSJavier Herrero struct opencores_kbd { 20422b552dSJavier Herrero struct input_dev *input; 21422b552dSJavier Herrero struct resource *addr_res; 22422b552dSJavier Herrero void __iomem *addr; 23422b552dSJavier Herrero int irq; 24422b552dSJavier Herrero unsigned short keycodes[128]; 25422b552dSJavier Herrero }; 26422b552dSJavier Herrero 27422b552dSJavier Herrero static irqreturn_t opencores_kbd_isr(int irq, void *dev_id) 28422b552dSJavier Herrero { 29422b552dSJavier Herrero struct opencores_kbd *opencores_kbd = dev_id; 30422b552dSJavier Herrero struct input_dev *input = opencores_kbd->input; 31422b552dSJavier Herrero unsigned char c; 32422b552dSJavier Herrero 33422b552dSJavier Herrero c = readb(opencores_kbd->addr); 34422b552dSJavier Herrero input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1); 35422b552dSJavier Herrero input_sync(input); 36422b552dSJavier Herrero 37422b552dSJavier Herrero return IRQ_HANDLED; 38422b552dSJavier Herrero } 39422b552dSJavier Herrero 40422b552dSJavier Herrero static int __devinit opencores_kbd_probe(struct platform_device *pdev) 41422b552dSJavier Herrero { 42422b552dSJavier Herrero struct input_dev *input; 43422b552dSJavier Herrero struct opencores_kbd *opencores_kbd; 44422b552dSJavier Herrero struct resource *res; 45422b552dSJavier Herrero int irq, i, error; 46422b552dSJavier Herrero 47422b552dSJavier Herrero res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 48422b552dSJavier Herrero if (!res) { 49422b552dSJavier Herrero dev_err(&pdev->dev, "missing board memory resource\n"); 50422b552dSJavier Herrero return -EINVAL; 51422b552dSJavier Herrero } 52422b552dSJavier Herrero 53422b552dSJavier Herrero irq = platform_get_irq(pdev, 0); 54422b552dSJavier Herrero if (irq < 0) { 55422b552dSJavier Herrero dev_err(&pdev->dev, "missing board IRQ resource\n"); 56422b552dSJavier Herrero return -EINVAL; 57422b552dSJavier Herrero } 58422b552dSJavier Herrero 59422b552dSJavier Herrero opencores_kbd = kzalloc(sizeof(*opencores_kbd), GFP_KERNEL); 60422b552dSJavier Herrero input = input_allocate_device(); 61422b552dSJavier Herrero if (!opencores_kbd || !input) { 62422b552dSJavier Herrero dev_err(&pdev->dev, "failed to allocate device structures\n"); 63422b552dSJavier Herrero error = -ENOMEM; 64422b552dSJavier Herrero goto err_free_mem; 65422b552dSJavier Herrero } 66422b552dSJavier Herrero 67422b552dSJavier Herrero opencores_kbd->addr_res = res; 68422b552dSJavier Herrero res = request_mem_region(res->start, resource_size(res), pdev->name); 69422b552dSJavier Herrero if (!res) { 70422b552dSJavier Herrero dev_err(&pdev->dev, "failed to request I/O memory\n"); 71422b552dSJavier Herrero error = -EBUSY; 72422b552dSJavier Herrero goto err_free_mem; 73422b552dSJavier Herrero } 74422b552dSJavier Herrero 75422b552dSJavier Herrero opencores_kbd->addr = ioremap(res->start, resource_size(res)); 76422b552dSJavier Herrero if (!opencores_kbd->addr) { 77422b552dSJavier Herrero dev_err(&pdev->dev, "failed to remap I/O memory\n"); 78422b552dSJavier Herrero error = -ENXIO; 79422b552dSJavier Herrero goto err_rel_mem; 80422b552dSJavier Herrero } 81422b552dSJavier Herrero 82422b552dSJavier Herrero opencores_kbd->input = input; 83422b552dSJavier Herrero opencores_kbd->irq = irq; 84422b552dSJavier Herrero 85422b552dSJavier Herrero input->name = pdev->name; 86422b552dSJavier Herrero input->phys = "opencores-kbd/input0"; 87422b552dSJavier Herrero input->dev.parent = &pdev->dev; 88422b552dSJavier Herrero 89422b552dSJavier Herrero input_set_drvdata(input, opencores_kbd); 90422b552dSJavier Herrero 91422b552dSJavier Herrero input->id.bustype = BUS_HOST; 92422b552dSJavier Herrero input->id.vendor = 0x0001; 93422b552dSJavier Herrero input->id.product = 0x0001; 94422b552dSJavier Herrero input->id.version = 0x0100; 95422b552dSJavier Herrero 96422b552dSJavier Herrero input->keycode = opencores_kbd->keycodes; 97422b552dSJavier Herrero input->keycodesize = sizeof(opencores_kbd->keycodes[0]); 98422b552dSJavier Herrero input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes); 99422b552dSJavier Herrero 100422b552dSJavier Herrero __set_bit(EV_KEY, input->evbit); 101422b552dSJavier Herrero 102422b552dSJavier Herrero for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) { 103422b552dSJavier Herrero /* 104422b552dSJavier Herrero * OpenCores controller happens to have scancodes match 105422b552dSJavier Herrero * our KEY_* definitions. 106422b552dSJavier Herrero */ 107422b552dSJavier Herrero opencores_kbd->keycodes[i] = i; 108422b552dSJavier Herrero __set_bit(opencores_kbd->keycodes[i], input->keybit); 109422b552dSJavier Herrero } 110422b552dSJavier Herrero __clear_bit(KEY_RESERVED, input->keybit); 111422b552dSJavier Herrero 112422b552dSJavier Herrero error = request_irq(irq, &opencores_kbd_isr, 113422b552dSJavier Herrero IRQF_TRIGGER_RISING, pdev->name, opencores_kbd); 114422b552dSJavier Herrero if (error) { 115422b552dSJavier Herrero dev_err(&pdev->dev, "unable to claim irq %d\n", irq); 116422b552dSJavier Herrero goto err_unmap_mem; 117422b552dSJavier Herrero } 118422b552dSJavier Herrero 119422b552dSJavier Herrero error = input_register_device(input); 120422b552dSJavier Herrero if (error) { 121422b552dSJavier Herrero dev_err(&pdev->dev, "unable to register input device\n"); 122422b552dSJavier Herrero goto err_free_irq; 123422b552dSJavier Herrero } 124422b552dSJavier Herrero 125422b552dSJavier Herrero platform_set_drvdata(pdev, opencores_kbd); 126422b552dSJavier Herrero 127422b552dSJavier Herrero return 0; 128422b552dSJavier Herrero 129422b552dSJavier Herrero err_free_irq: 130422b552dSJavier Herrero free_irq(irq, opencores_kbd); 131422b552dSJavier Herrero err_unmap_mem: 132422b552dSJavier Herrero iounmap(opencores_kbd->addr); 133422b552dSJavier Herrero err_rel_mem: 134422b552dSJavier Herrero release_mem_region(res->start, resource_size(res)); 135422b552dSJavier Herrero err_free_mem: 136422b552dSJavier Herrero input_free_device(input); 137422b552dSJavier Herrero kfree(opencores_kbd); 138422b552dSJavier Herrero 139422b552dSJavier Herrero return error; 140422b552dSJavier Herrero } 141422b552dSJavier Herrero 142422b552dSJavier Herrero static int __devexit opencores_kbd_remove(struct platform_device *pdev) 143422b552dSJavier Herrero { 144422b552dSJavier Herrero struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev); 145422b552dSJavier Herrero 146422b552dSJavier Herrero free_irq(opencores_kbd->irq, opencores_kbd); 147422b552dSJavier Herrero 148422b552dSJavier Herrero iounmap(opencores_kbd->addr); 149422b552dSJavier Herrero release_mem_region(opencores_kbd->addr_res->start, 150422b552dSJavier Herrero resource_size(opencores_kbd->addr_res)); 151422b552dSJavier Herrero input_unregister_device(opencores_kbd->input); 152422b552dSJavier Herrero kfree(opencores_kbd); 153422b552dSJavier Herrero 154422b552dSJavier Herrero platform_set_drvdata(pdev, NULL); 155422b552dSJavier Herrero 156422b552dSJavier Herrero return 0; 157422b552dSJavier Herrero } 158422b552dSJavier Herrero 159422b552dSJavier Herrero static struct platform_driver opencores_kbd_device_driver = { 160422b552dSJavier Herrero .probe = opencores_kbd_probe, 161422b552dSJavier Herrero .remove = __devexit_p(opencores_kbd_remove), 162422b552dSJavier Herrero .driver = { 163422b552dSJavier Herrero .name = "opencores-kbd", 164422b552dSJavier Herrero }, 165422b552dSJavier Herrero }; 166422b552dSJavier Herrero 167422b552dSJavier Herrero static int __init opencores_kbd_init(void) 168422b552dSJavier Herrero { 169422b552dSJavier Herrero return platform_driver_register(&opencores_kbd_device_driver); 170422b552dSJavier Herrero } 171422b552dSJavier Herrero module_init(opencores_kbd_init); 172422b552dSJavier Herrero 173422b552dSJavier Herrero static void __exit opencores_kbd_exit(void) 174422b552dSJavier Herrero { 175422b552dSJavier Herrero platform_driver_unregister(&opencores_kbd_device_driver); 176422b552dSJavier Herrero } 177422b552dSJavier Herrero module_exit(opencores_kbd_exit); 178422b552dSJavier Herrero 179422b552dSJavier Herrero MODULE_LICENSE("GPL"); 180422b552dSJavier Herrero MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>"); 181422b552dSJavier Herrero MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller"); 182