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> 175a0e3ad6STejun Heo #include <linux/slab.h> 18422b552dSJavier Herrero 19422b552dSJavier Herrero struct opencores_kbd { 20422b552dSJavier Herrero struct input_dev *input; 21422b552dSJavier Herrero void __iomem *addr; 22422b552dSJavier Herrero int irq; 23422b552dSJavier Herrero unsigned short keycodes[128]; 24422b552dSJavier Herrero }; 25422b552dSJavier Herrero 26422b552dSJavier Herrero static irqreturn_t opencores_kbd_isr(int irq, void *dev_id) 27422b552dSJavier Herrero { 28422b552dSJavier Herrero struct opencores_kbd *opencores_kbd = dev_id; 29422b552dSJavier Herrero struct input_dev *input = opencores_kbd->input; 30422b552dSJavier Herrero unsigned char c; 31422b552dSJavier Herrero 32422b552dSJavier Herrero c = readb(opencores_kbd->addr); 33422b552dSJavier Herrero input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1); 34422b552dSJavier Herrero input_sync(input); 35422b552dSJavier Herrero 36422b552dSJavier Herrero return IRQ_HANDLED; 37422b552dSJavier Herrero } 38422b552dSJavier Herrero 395298cc4cSBill Pemberton static int opencores_kbd_probe(struct platform_device *pdev) 40422b552dSJavier Herrero { 41422b552dSJavier Herrero struct input_dev *input; 42422b552dSJavier Herrero struct opencores_kbd *opencores_kbd; 43422b552dSJavier Herrero struct resource *res; 44422b552dSJavier Herrero int irq, i, error; 45422b552dSJavier Herrero 46422b552dSJavier Herrero res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 47422b552dSJavier Herrero if (!res) { 48422b552dSJavier Herrero dev_err(&pdev->dev, "missing board memory resource\n"); 49422b552dSJavier Herrero return -EINVAL; 50422b552dSJavier Herrero } 51422b552dSJavier Herrero 52422b552dSJavier Herrero irq = platform_get_irq(pdev, 0); 53422b552dSJavier Herrero if (irq < 0) { 54422b552dSJavier Herrero dev_err(&pdev->dev, "missing board IRQ resource\n"); 55422b552dSJavier Herrero return -EINVAL; 56422b552dSJavier Herrero } 57422b552dSJavier Herrero 58*848d4793SPramod Gurav opencores_kbd = devm_kzalloc(&pdev->dev, sizeof(*opencores_kbd), 59*848d4793SPramod Gurav GFP_KERNEL); 60*848d4793SPramod Gurav if (!opencores_kbd) 61*848d4793SPramod Gurav return -ENOMEM; 62422b552dSJavier Herrero 63*848d4793SPramod Gurav input = devm_input_allocate_device(&pdev->dev); 64*848d4793SPramod Gurav if (!input) { 65*848d4793SPramod Gurav dev_err(&pdev->dev, "failed to allocate input device\n"); 66*848d4793SPramod Gurav return -ENOMEM; 67422b552dSJavier Herrero } 68422b552dSJavier Herrero 69422b552dSJavier Herrero opencores_kbd->input = input; 70*848d4793SPramod Gurav 71*848d4793SPramod Gurav opencores_kbd->addr = devm_ioremap_resource(&pdev->dev, res); 72*848d4793SPramod Gurav if (IS_ERR(opencores_kbd->addr)) 73*848d4793SPramod Gurav error = PTR_ERR(opencores_kbd->addr); 74422b552dSJavier Herrero 75422b552dSJavier Herrero input->name = pdev->name; 76422b552dSJavier Herrero input->phys = "opencores-kbd/input0"; 77422b552dSJavier Herrero 78422b552dSJavier Herrero input_set_drvdata(input, opencores_kbd); 79422b552dSJavier Herrero 80422b552dSJavier Herrero input->id.bustype = BUS_HOST; 81422b552dSJavier Herrero input->id.vendor = 0x0001; 82422b552dSJavier Herrero input->id.product = 0x0001; 83422b552dSJavier Herrero input->id.version = 0x0100; 84422b552dSJavier Herrero 85422b552dSJavier Herrero input->keycode = opencores_kbd->keycodes; 86422b552dSJavier Herrero input->keycodesize = sizeof(opencores_kbd->keycodes[0]); 87422b552dSJavier Herrero input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes); 88422b552dSJavier Herrero 89422b552dSJavier Herrero __set_bit(EV_KEY, input->evbit); 90422b552dSJavier Herrero 91422b552dSJavier Herrero for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) { 92422b552dSJavier Herrero /* 93422b552dSJavier Herrero * OpenCores controller happens to have scancodes match 94422b552dSJavier Herrero * our KEY_* definitions. 95422b552dSJavier Herrero */ 96422b552dSJavier Herrero opencores_kbd->keycodes[i] = i; 97422b552dSJavier Herrero __set_bit(opencores_kbd->keycodes[i], input->keybit); 98422b552dSJavier Herrero } 99422b552dSJavier Herrero __clear_bit(KEY_RESERVED, input->keybit); 100422b552dSJavier Herrero 101*848d4793SPramod Gurav error = devm_request_irq(&pdev->dev, irq, &opencores_kbd_isr, 102*848d4793SPramod Gurav IRQF_TRIGGER_RISING, 103*848d4793SPramod Gurav pdev->name, opencores_kbd); 104422b552dSJavier Herrero if (error) { 105422b552dSJavier Herrero dev_err(&pdev->dev, "unable to claim irq %d\n", irq); 106*848d4793SPramod Gurav return error; 107422b552dSJavier Herrero } 108422b552dSJavier Herrero 109422b552dSJavier Herrero error = input_register_device(input); 110422b552dSJavier Herrero if (error) { 111422b552dSJavier Herrero dev_err(&pdev->dev, "unable to register input device\n"); 112422b552dSJavier Herrero return error; 113422b552dSJavier Herrero } 114422b552dSJavier Herrero 115*848d4793SPramod Gurav platform_set_drvdata(pdev, opencores_kbd); 116422b552dSJavier Herrero 117422b552dSJavier Herrero return 0; 118422b552dSJavier Herrero } 119422b552dSJavier Herrero 120422b552dSJavier Herrero static struct platform_driver opencores_kbd_device_driver = { 121422b552dSJavier Herrero .probe = opencores_kbd_probe, 122422b552dSJavier Herrero .driver = { 123422b552dSJavier Herrero .name = "opencores-kbd", 124422b552dSJavier Herrero }, 125422b552dSJavier Herrero }; 1265146c84fSJJ Ding module_platform_driver(opencores_kbd_device_driver); 127422b552dSJavier Herrero 128422b552dSJavier Herrero MODULE_LICENSE("GPL"); 129422b552dSJavier Herrero MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>"); 130422b552dSJavier Herrero MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller"); 131