1*80503b23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2422b552dSJavier Herrero /* 3422b552dSJavier Herrero * OpenCores Keyboard Controller Driver 4422b552dSJavier Herrero * http://www.opencores.org/project,keyboardcontroller 5422b552dSJavier Herrero * 6422b552dSJavier Herrero * Copyright 2007-2009 HV Sistemas S.L. 7422b552dSJavier Herrero */ 8422b552dSJavier Herrero 9422b552dSJavier Herrero #include <linux/input.h> 10422b552dSJavier Herrero #include <linux/interrupt.h> 11422b552dSJavier Herrero #include <linux/io.h> 12422b552dSJavier Herrero #include <linux/ioport.h> 13422b552dSJavier Herrero #include <linux/kernel.h> 14422b552dSJavier Herrero #include <linux/module.h> 15422b552dSJavier Herrero #include <linux/platform_device.h> 165a0e3ad6STejun Heo #include <linux/slab.h> 17422b552dSJavier Herrero 18422b552dSJavier Herrero struct opencores_kbd { 19422b552dSJavier Herrero struct input_dev *input; 20422b552dSJavier Herrero void __iomem *addr; 21422b552dSJavier Herrero int irq; 22422b552dSJavier Herrero unsigned short keycodes[128]; 23422b552dSJavier Herrero }; 24422b552dSJavier Herrero 25422b552dSJavier Herrero static irqreturn_t opencores_kbd_isr(int irq, void *dev_id) 26422b552dSJavier Herrero { 27422b552dSJavier Herrero struct opencores_kbd *opencores_kbd = dev_id; 28422b552dSJavier Herrero struct input_dev *input = opencores_kbd->input; 29422b552dSJavier Herrero unsigned char c; 30422b552dSJavier Herrero 31422b552dSJavier Herrero c = readb(opencores_kbd->addr); 32422b552dSJavier Herrero input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1); 33422b552dSJavier Herrero input_sync(input); 34422b552dSJavier Herrero 35422b552dSJavier Herrero return IRQ_HANDLED; 36422b552dSJavier Herrero } 37422b552dSJavier Herrero 385298cc4cSBill Pemberton static int opencores_kbd_probe(struct platform_device *pdev) 39422b552dSJavier Herrero { 40422b552dSJavier Herrero struct input_dev *input; 41422b552dSJavier Herrero struct opencores_kbd *opencores_kbd; 42422b552dSJavier Herrero struct resource *res; 43422b552dSJavier Herrero int irq, i, error; 44422b552dSJavier Herrero 45422b552dSJavier Herrero res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 46422b552dSJavier Herrero if (!res) { 47422b552dSJavier Herrero dev_err(&pdev->dev, "missing board memory resource\n"); 48422b552dSJavier Herrero return -EINVAL; 49422b552dSJavier Herrero } 50422b552dSJavier Herrero 51422b552dSJavier Herrero irq = platform_get_irq(pdev, 0); 52422b552dSJavier Herrero if (irq < 0) { 53422b552dSJavier Herrero dev_err(&pdev->dev, "missing board IRQ resource\n"); 54422b552dSJavier Herrero return -EINVAL; 55422b552dSJavier Herrero } 56422b552dSJavier Herrero 57848d4793SPramod Gurav opencores_kbd = devm_kzalloc(&pdev->dev, sizeof(*opencores_kbd), 58848d4793SPramod Gurav GFP_KERNEL); 59848d4793SPramod Gurav if (!opencores_kbd) 60848d4793SPramod Gurav return -ENOMEM; 61422b552dSJavier Herrero 62848d4793SPramod Gurav input = devm_input_allocate_device(&pdev->dev); 63848d4793SPramod Gurav if (!input) { 64848d4793SPramod Gurav dev_err(&pdev->dev, "failed to allocate input device\n"); 65848d4793SPramod Gurav return -ENOMEM; 66422b552dSJavier Herrero } 67422b552dSJavier Herrero 68422b552dSJavier Herrero opencores_kbd->input = input; 69848d4793SPramod Gurav 70848d4793SPramod Gurav opencores_kbd->addr = devm_ioremap_resource(&pdev->dev, res); 71848d4793SPramod Gurav if (IS_ERR(opencores_kbd->addr)) 7242b63e60SDmitry Torokhov return PTR_ERR(opencores_kbd->addr); 73422b552dSJavier Herrero 74422b552dSJavier Herrero input->name = pdev->name; 75422b552dSJavier Herrero input->phys = "opencores-kbd/input0"; 76422b552dSJavier Herrero 77422b552dSJavier Herrero input->id.bustype = BUS_HOST; 78422b552dSJavier Herrero input->id.vendor = 0x0001; 79422b552dSJavier Herrero input->id.product = 0x0001; 80422b552dSJavier Herrero input->id.version = 0x0100; 81422b552dSJavier Herrero 82422b552dSJavier Herrero input->keycode = opencores_kbd->keycodes; 83422b552dSJavier Herrero input->keycodesize = sizeof(opencores_kbd->keycodes[0]); 84422b552dSJavier Herrero input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes); 85422b552dSJavier Herrero 86422b552dSJavier Herrero __set_bit(EV_KEY, input->evbit); 87422b552dSJavier Herrero 88422b552dSJavier Herrero for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) { 89422b552dSJavier Herrero /* 90422b552dSJavier Herrero * OpenCores controller happens to have scancodes match 91422b552dSJavier Herrero * our KEY_* definitions. 92422b552dSJavier Herrero */ 93422b552dSJavier Herrero opencores_kbd->keycodes[i] = i; 94422b552dSJavier Herrero __set_bit(opencores_kbd->keycodes[i], input->keybit); 95422b552dSJavier Herrero } 96422b552dSJavier Herrero __clear_bit(KEY_RESERVED, input->keybit); 97422b552dSJavier Herrero 98848d4793SPramod Gurav error = devm_request_irq(&pdev->dev, irq, &opencores_kbd_isr, 99848d4793SPramod Gurav IRQF_TRIGGER_RISING, 100848d4793SPramod Gurav pdev->name, opencores_kbd); 101422b552dSJavier Herrero if (error) { 102422b552dSJavier Herrero dev_err(&pdev->dev, "unable to claim irq %d\n", irq); 103848d4793SPramod Gurav return error; 104422b552dSJavier Herrero } 105422b552dSJavier Herrero 106422b552dSJavier Herrero error = input_register_device(input); 107422b552dSJavier Herrero if (error) { 108422b552dSJavier Herrero dev_err(&pdev->dev, "unable to register input device\n"); 109422b552dSJavier Herrero return error; 110422b552dSJavier Herrero } 111422b552dSJavier Herrero 112422b552dSJavier Herrero return 0; 113422b552dSJavier Herrero } 114422b552dSJavier Herrero 115422b552dSJavier Herrero static struct platform_driver opencores_kbd_device_driver = { 116422b552dSJavier Herrero .probe = opencores_kbd_probe, 117422b552dSJavier Herrero .driver = { 118422b552dSJavier Herrero .name = "opencores-kbd", 119422b552dSJavier Herrero }, 120422b552dSJavier Herrero }; 1215146c84fSJJ Ding module_platform_driver(opencores_kbd_device_driver); 122422b552dSJavier Herrero 123422b552dSJavier Herrero MODULE_LICENSE("GPL"); 124422b552dSJavier Herrero MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>"); 125422b552dSJavier Herrero MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller"); 126