Loading...
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>
27#include <linux/module.h>
28
29#include <asm/mach-types.h>
30#include <mach/board-ams-delta.h>
31
32#include <mach/ams-delta-fiq.h>
33
34MODULE_AUTHOR("Matt Callow");
35MODULE_DESCRIPTION("AMS Delta (E3) keyboard port driver");
36MODULE_LICENSE("GPL");
37
38static struct serio *ams_delta_serio;
39
40static 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
66static 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
92static int ams_delta_serio_open(struct serio *serio)
93{
94 /* enable keyboard */
95 gpio_set_value(AMS_DELTA_GPIO_PIN_KEYBRD_PWR, 1);
96
97 return 0;
98}
99
100static void ams_delta_serio_close(struct serio *serio)
101{
102 /* disable keyboard */
103 gpio_set_value(AMS_DELTA_GPIO_PIN_KEYBRD_PWR, 0);
104}
105
106static const struct gpio ams_delta_gpios[] __initconst_or_module = {
107 {
108 .gpio = AMS_DELTA_GPIO_PIN_KEYBRD_DATA,
109 .flags = GPIOF_DIR_IN,
110 .label = "serio-data",
111 },
112 {
113 .gpio = AMS_DELTA_GPIO_PIN_KEYBRD_CLK,
114 .flags = GPIOF_DIR_IN,
115 .label = "serio-clock",
116 },
117 {
118 .gpio = AMS_DELTA_GPIO_PIN_KEYBRD_PWR,
119 .flags = GPIOF_OUT_INIT_LOW,
120 .label = "serio-power",
121 },
122 {
123 .gpio = AMS_DELTA_GPIO_PIN_KEYBRD_DATAOUT,
124 .flags = GPIOF_OUT_INIT_LOW,
125 .label = "serio-dataout",
126 },
127};
128
129static int __init ams_delta_serio_init(void)
130{
131 int err;
132
133 if (!machine_is_ams_delta())
134 return -ENODEV;
135
136 ams_delta_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
137 if (!ams_delta_serio)
138 return -ENOMEM;
139
140 ams_delta_serio->id.type = SERIO_8042;
141 ams_delta_serio->open = ams_delta_serio_open;
142 ams_delta_serio->close = ams_delta_serio_close;
143 strlcpy(ams_delta_serio->name, "AMS DELTA keyboard adapter",
144 sizeof(ams_delta_serio->name));
145 strlcpy(ams_delta_serio->phys, "GPIO/serio0",
146 sizeof(ams_delta_serio->phys));
147
148 err = gpio_request_array(ams_delta_gpios,
149 ARRAY_SIZE(ams_delta_gpios));
150 if (err) {
151 pr_err("ams_delta_serio: Couldn't request gpio pins\n");
152 goto serio;
153 }
154
155 err = request_irq(gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK),
156 ams_delta_serio_interrupt, IRQ_TYPE_EDGE_RISING,
157 "ams-delta-serio", 0);
158 if (err < 0) {
159 pr_err("ams_delta_serio: couldn't request gpio interrupt %d\n",
160 gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK));
161 goto gpio;
162 }
163 /*
164 * Since GPIO register handling for keyboard clock pin is performed
165 * at FIQ level, switch back from edge to simple interrupt handler
166 * to avoid bad interaction.
167 */
168 irq_set_handler(gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK),
169 handle_simple_irq);
170
171 serio_register_port(ams_delta_serio);
172 dev_info(&ams_delta_serio->dev, "%s\n", ams_delta_serio->name);
173
174 return 0;
175gpio:
176 gpio_free_array(ams_delta_gpios,
177 ARRAY_SIZE(ams_delta_gpios));
178serio:
179 kfree(ams_delta_serio);
180 return err;
181}
182module_init(ams_delta_serio_init);
183
184static void __exit ams_delta_serio_exit(void)
185{
186 serio_unregister_port(ams_delta_serio);
187 free_irq(gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK), 0);
188 gpio_free_array(ams_delta_gpios,
189 ARRAY_SIZE(ams_delta_gpios));
190}
191module_exit(ams_delta_serio_exit);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Amstrad E3 (Delta) keyboard port driver
4 *
5 * Copyright (c) 2006 Matt Callow
6 * Copyright (c) 2010 Janusz Krzysztofik
7 *
8 * Thanks to Cliff Lawson for his help
9 *
10 * The Amstrad Delta keyboard (aka mailboard) uses normal PC-AT style serial
11 * transmission. The keyboard port is formed of two GPIO lines, for clock
12 * and data. Due to strict timing requirements of the interface,
13 * the serial data stream is read and processed by a FIQ handler.
14 * The resulting words are fetched by this driver from a circular buffer.
15 *
16 * Standard AT keyboard driver (atkbd) is used for handling the keyboard data.
17 * However, when used with the E3 mailboard that producecs non-standard
18 * scancodes, a custom key table must be prepared and loaded from userspace.
19 */
20#include <linux/irq.h>
21#include <linux/platform_data/ams-delta-fiq.h>
22#include <linux/platform_device.h>
23#include <linux/regulator/consumer.h>
24#include <linux/serio.h>
25#include <linux/slab.h>
26#include <linux/module.h>
27
28#define DRIVER_NAME "ams-delta-serio"
29
30MODULE_AUTHOR("Matt Callow");
31MODULE_DESCRIPTION("AMS Delta (E3) keyboard port driver");
32MODULE_LICENSE("GPL");
33
34struct ams_delta_serio {
35 struct serio *serio;
36 struct regulator *vcc;
37 unsigned int *fiq_buffer;
38};
39
40static int check_data(struct serio *serio, int data)
41{
42 int i, parity = 0;
43
44 /* check valid stop bit */
45 if (!(data & 0x400)) {
46 dev_warn(&serio->dev, "invalid stop bit, data=0x%X\n", data);
47 return SERIO_FRAME;
48 }
49 /* calculate the parity */
50 for (i = 1; i < 10; i++) {
51 if (data & (1 << i))
52 parity++;
53 }
54 /* it should be odd */
55 if (!(parity & 0x01)) {
56 dev_warn(&serio->dev,
57 "parity check failed, data=0x%X parity=0x%X\n", data,
58 parity);
59 return SERIO_PARITY;
60 }
61 return 0;
62}
63
64static irqreturn_t ams_delta_serio_interrupt(int irq, void *dev_id)
65{
66 struct ams_delta_serio *priv = dev_id;
67 int *circ_buff = &priv->fiq_buffer[FIQ_CIRC_BUFF];
68 int data, dfl;
69 u8 scancode;
70
71 priv->fiq_buffer[FIQ_IRQ_PEND] = 0;
72
73 /*
74 * Read data from the circular buffer, check it
75 * and then pass it on the serio
76 */
77 while (priv->fiq_buffer[FIQ_KEYS_CNT] > 0) {
78
79 data = circ_buff[priv->fiq_buffer[FIQ_HEAD_OFFSET]++];
80 priv->fiq_buffer[FIQ_KEYS_CNT]--;
81 if (priv->fiq_buffer[FIQ_HEAD_OFFSET] ==
82 priv->fiq_buffer[FIQ_BUF_LEN])
83 priv->fiq_buffer[FIQ_HEAD_OFFSET] = 0;
84
85 dfl = check_data(priv->serio, data);
86 scancode = (u8) (data >> 1) & 0xFF;
87 serio_interrupt(priv->serio, scancode, dfl);
88 }
89 return IRQ_HANDLED;
90}
91
92static int ams_delta_serio_open(struct serio *serio)
93{
94 struct ams_delta_serio *priv = serio->port_data;
95
96 /* enable keyboard */
97 return regulator_enable(priv->vcc);
98}
99
100static void ams_delta_serio_close(struct serio *serio)
101{
102 struct ams_delta_serio *priv = serio->port_data;
103
104 /* disable keyboard */
105 regulator_disable(priv->vcc);
106}
107
108static int ams_delta_serio_init(struct platform_device *pdev)
109{
110 struct ams_delta_serio *priv;
111 struct serio *serio;
112 int irq, err;
113
114 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
115 if (!priv)
116 return -ENOMEM;
117
118 priv->fiq_buffer = pdev->dev.platform_data;
119 if (!priv->fiq_buffer)
120 return -EINVAL;
121
122 priv->vcc = devm_regulator_get(&pdev->dev, "vcc");
123 if (IS_ERR(priv->vcc)) {
124 err = PTR_ERR(priv->vcc);
125 dev_err(&pdev->dev, "regulator request failed (%d)\n", err);
126 /*
127 * When running on a non-dt platform and requested regulator
128 * is not available, devm_regulator_get() never returns
129 * -EPROBE_DEFER as it is not able to justify if the regulator
130 * may still appear later. On the other hand, the board can
131 * still set full constriants flag at late_initcall in order
132 * to instruct devm_regulator_get() to returnn a dummy one
133 * if sufficient. Hence, if we get -ENODEV here, let's convert
134 * it to -EPROBE_DEFER and wait for the board to decide or
135 * let Deferred Probe infrastructure handle this error.
136 */
137 if (err == -ENODEV)
138 err = -EPROBE_DEFER;
139 return err;
140 }
141
142 irq = platform_get_irq(pdev, 0);
143 if (irq < 0)
144 return -ENXIO;
145
146 err = devm_request_irq(&pdev->dev, irq, ams_delta_serio_interrupt,
147 IRQ_TYPE_EDGE_RISING, DRIVER_NAME, priv);
148 if (err < 0) {
149 dev_err(&pdev->dev, "IRQ request failed (%d)\n", err);
150 return err;
151 }
152
153 serio = kzalloc(sizeof(*serio), GFP_KERNEL);
154 if (!serio)
155 return -ENOMEM;
156
157 priv->serio = serio;
158
159 serio->id.type = SERIO_8042;
160 serio->open = ams_delta_serio_open;
161 serio->close = ams_delta_serio_close;
162 strscpy(serio->name, "AMS DELTA keyboard adapter", sizeof(serio->name));
163 strscpy(serio->phys, dev_name(&pdev->dev), sizeof(serio->phys));
164 serio->dev.parent = &pdev->dev;
165 serio->port_data = priv;
166
167 serio_register_port(serio);
168
169 platform_set_drvdata(pdev, priv);
170
171 dev_info(&serio->dev, "%s\n", serio->name);
172
173 return 0;
174}
175
176static void ams_delta_serio_exit(struct platform_device *pdev)
177{
178 struct ams_delta_serio *priv = platform_get_drvdata(pdev);
179
180 serio_unregister_port(priv->serio);
181}
182
183static struct platform_driver ams_delta_serio_driver = {
184 .probe = ams_delta_serio_init,
185 .remove_new = ams_delta_serio_exit,
186 .driver = {
187 .name = DRIVER_NAME
188 },
189};
190module_platform_driver(ams_delta_serio_driver);