Janusz Krzysztofik | 2945393 | 2010-04-28 01:10:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Amstrad E3 (Delta) keyboard port driver |
| 3 | * |
| 4 | * Copyright (c) 2006 Matt Callow |
| 5 | * Copyright (c) 2010 Janusz Krzysztofik |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License version 2 as published by |
| 9 | * the Free Software Foundation. |
| 10 | * |
| 11 | * Thanks to Cliff Lawson for his help |
| 12 | * |
| 13 | * The Amstrad Delta keyboard (aka mailboard) uses normal PC-AT style serial |
| 14 | * transmission. The keyboard port is formed of two GPIO lines, for clock |
| 15 | * and data. Due to strict timing requirements of the interface, |
| 16 | * the serial data stream is read and processed by a FIQ handler. |
| 17 | * The resulting words are fetched by this driver from a circular buffer. |
| 18 | * |
| 19 | * Standard AT keyboard driver (atkbd) is used for handling the keyboard data. |
| 20 | * However, when used with the E3 mailboard that producecs non-standard |
| 21 | * scancodes, a custom key table must be prepared and loaded from userspace. |
| 22 | */ |
| 23 | #include <linux/gpio.h> |
| 24 | #include <linux/irq.h> |
| 25 | #include <linux/serio.h> |
| 26 | #include <linux/slab.h> |
Aaro Koskinen | 77f6ca5 | 2011-11-09 10:13:33 -0800 | [diff] [blame] | 27 | #include <linux/module.h> |
Janusz Krzysztofik | 2945393 | 2010-04-28 01:10:50 +0000 | [diff] [blame] | 28 | |
| 29 | #include <asm/mach-types.h> |
| 30 | #include <plat/board-ams-delta.h> |
| 31 | |
| 32 | #include <mach/ams-delta-fiq.h> |
| 33 | |
| 34 | MODULE_AUTHOR("Matt Callow"); |
| 35 | MODULE_DESCRIPTION("AMS Delta (E3) keyboard port driver"); |
| 36 | MODULE_LICENSE("GPL"); |
| 37 | |
| 38 | static struct serio *ams_delta_serio; |
| 39 | |
| 40 | static int check_data(int data) |
| 41 | { |
| 42 | int i, parity = 0; |
| 43 | |
| 44 | /* check valid stop bit */ |
| 45 | if (!(data & 0x400)) { |
| 46 | dev_warn(&ams_delta_serio->dev, |
| 47 | "invalid stop bit, data=0x%X\n", |
| 48 | data); |
| 49 | return SERIO_FRAME; |
| 50 | } |
| 51 | /* calculate the parity */ |
| 52 | for (i = 1; i < 10; i++) { |
| 53 | if (data & (1 << i)) |
| 54 | parity++; |
| 55 | } |
| 56 | /* it should be odd */ |
| 57 | if (!(parity & 0x01)) { |
| 58 | dev_warn(&ams_delta_serio->dev, |
| 59 | "paritiy check failed, data=0x%X parity=0x%X\n", |
| 60 | data, parity); |
| 61 | return SERIO_PARITY; |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static irqreturn_t ams_delta_serio_interrupt(int irq, void *dev_id) |
| 67 | { |
| 68 | int *circ_buff = &fiq_buffer[FIQ_CIRC_BUFF]; |
| 69 | int data, dfl; |
| 70 | u8 scancode; |
| 71 | |
| 72 | fiq_buffer[FIQ_IRQ_PEND] = 0; |
| 73 | |
| 74 | /* |
| 75 | * Read data from the circular buffer, check it |
| 76 | * and then pass it on the serio |
| 77 | */ |
| 78 | while (fiq_buffer[FIQ_KEYS_CNT] > 0) { |
| 79 | |
| 80 | data = circ_buff[fiq_buffer[FIQ_HEAD_OFFSET]++]; |
| 81 | fiq_buffer[FIQ_KEYS_CNT]--; |
| 82 | if (fiq_buffer[FIQ_HEAD_OFFSET] == fiq_buffer[FIQ_BUF_LEN]) |
| 83 | fiq_buffer[FIQ_HEAD_OFFSET] = 0; |
| 84 | |
| 85 | dfl = check_data(data); |
| 86 | scancode = (u8) (data >> 1) & 0xFF; |
| 87 | serio_interrupt(ams_delta_serio, scancode, dfl); |
| 88 | } |
| 89 | return IRQ_HANDLED; |
| 90 | } |
| 91 | |
| 92 | static int ams_delta_serio_open(struct serio *serio) |
| 93 | { |
| 94 | /* enable keyboard */ |
| 95 | ams_delta_latch2_write(AMD_DELTA_LATCH2_KEYBRD_PWR, |
| 96 | AMD_DELTA_LATCH2_KEYBRD_PWR); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void ams_delta_serio_close(struct serio *serio) |
| 102 | { |
| 103 | /* disable keyboard */ |
| 104 | ams_delta_latch2_write(AMD_DELTA_LATCH2_KEYBRD_PWR, 0); |
| 105 | } |
| 106 | |
| 107 | static int __init ams_delta_serio_init(void) |
| 108 | { |
| 109 | int err; |
| 110 | |
| 111 | if (!machine_is_ams_delta()) |
| 112 | return -ENODEV; |
| 113 | |
| 114 | ams_delta_serio = kzalloc(sizeof(struct serio), GFP_KERNEL); |
| 115 | if (!ams_delta_serio) |
| 116 | return -ENOMEM; |
| 117 | |
| 118 | ams_delta_serio->id.type = SERIO_8042; |
| 119 | ams_delta_serio->open = ams_delta_serio_open; |
| 120 | ams_delta_serio->close = ams_delta_serio_close; |
| 121 | strlcpy(ams_delta_serio->name, "AMS DELTA keyboard adapter", |
| 122 | sizeof(ams_delta_serio->name)); |
| 123 | strlcpy(ams_delta_serio->phys, "GPIO/serio0", |
| 124 | sizeof(ams_delta_serio->phys)); |
| 125 | |
| 126 | err = gpio_request(AMS_DELTA_GPIO_PIN_KEYBRD_DATA, "serio-data"); |
| 127 | if (err) { |
| 128 | pr_err("ams_delta_serio: Couldn't request gpio pin for data\n"); |
| 129 | goto serio; |
| 130 | } |
| 131 | gpio_direction_input(AMS_DELTA_GPIO_PIN_KEYBRD_DATA); |
| 132 | |
| 133 | err = gpio_request(AMS_DELTA_GPIO_PIN_KEYBRD_CLK, "serio-clock"); |
| 134 | if (err) { |
| 135 | pr_err("ams_delta_serio: couldn't request gpio pin for clock\n"); |
| 136 | goto gpio_data; |
| 137 | } |
| 138 | gpio_direction_input(AMS_DELTA_GPIO_PIN_KEYBRD_CLK); |
| 139 | |
| 140 | err = request_irq(gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK), |
| 141 | ams_delta_serio_interrupt, IRQ_TYPE_EDGE_RISING, |
| 142 | "ams-delta-serio", 0); |
| 143 | if (err < 0) { |
| 144 | pr_err("ams_delta_serio: couldn't request gpio interrupt %d\n", |
| 145 | gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK)); |
| 146 | goto gpio_clk; |
| 147 | } |
| 148 | /* |
| 149 | * Since GPIO register handling for keyboard clock pin is performed |
| 150 | * at FIQ level, switch back from edge to simple interrupt handler |
| 151 | * to avoid bad interaction. |
| 152 | */ |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 153 | irq_set_handler(gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK), |
Janusz Krzysztofik | 2945393 | 2010-04-28 01:10:50 +0000 | [diff] [blame] | 154 | handle_simple_irq); |
| 155 | |
| 156 | serio_register_port(ams_delta_serio); |
| 157 | dev_info(&ams_delta_serio->dev, "%s\n", ams_delta_serio->name); |
| 158 | |
| 159 | return 0; |
| 160 | gpio_clk: |
| 161 | gpio_free(AMS_DELTA_GPIO_PIN_KEYBRD_CLK); |
| 162 | gpio_data: |
| 163 | gpio_free(AMS_DELTA_GPIO_PIN_KEYBRD_DATA); |
| 164 | serio: |
| 165 | kfree(ams_delta_serio); |
| 166 | return err; |
| 167 | } |
| 168 | module_init(ams_delta_serio_init); |
| 169 | |
| 170 | static void __exit ams_delta_serio_exit(void) |
| 171 | { |
| 172 | serio_unregister_port(ams_delta_serio); |
| 173 | free_irq(OMAP_GPIO_IRQ(AMS_DELTA_GPIO_PIN_KEYBRD_CLK), 0); |
| 174 | gpio_free(AMS_DELTA_GPIO_PIN_KEYBRD_CLK); |
| 175 | gpio_free(AMS_DELTA_GPIO_PIN_KEYBRD_DATA); |
Janusz Krzysztofik | 2945393 | 2010-04-28 01:10:50 +0000 | [diff] [blame] | 176 | } |
| 177 | module_exit(ams_delta_serio_exit); |