2 * Amstrad E3 (Delta) keyboard port driver
4 * Copyright (c) 2006 Matt Callow
5 * Copyright (c) 2010 Janusz Krzysztofik
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.
11 * Thanks to Cliff Lawson for his help
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.
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.
23 #include <linux/irq.h>
24 #include <linux/platform_data/ams-delta-fiq.h>
25 #include <linux/platform_device.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/serio.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
31 #define DRIVER_NAME "ams-delta-serio"
33 MODULE_AUTHOR("Matt Callow");
34 MODULE_DESCRIPTION("AMS Delta (E3) keyboard port driver");
35 MODULE_LICENSE("GPL");
37 struct ams_delta_serio
{
39 struct regulator
*vcc
;
40 unsigned int *fiq_buffer
;
43 static int check_data(struct serio
*serio
, int data
)
47 /* check valid stop bit */
48 if (!(data
& 0x400)) {
49 dev_warn(&serio
->dev
, "invalid stop bit, data=0x%X\n", data
);
52 /* calculate the parity */
53 for (i
= 1; i
< 10; i
++) {
57 /* it should be odd */
58 if (!(parity
& 0x01)) {
60 "parity check failed, data=0x%X parity=0x%X\n", data
,
67 static irqreturn_t
ams_delta_serio_interrupt(int irq
, void *dev_id
)
69 struct ams_delta_serio
*priv
= dev_id
;
70 int *circ_buff
= &priv
->fiq_buffer
[FIQ_CIRC_BUFF
];
74 priv
->fiq_buffer
[FIQ_IRQ_PEND
] = 0;
77 * Read data from the circular buffer, check it
78 * and then pass it on the serio
80 while (priv
->fiq_buffer
[FIQ_KEYS_CNT
] > 0) {
82 data
= circ_buff
[priv
->fiq_buffer
[FIQ_HEAD_OFFSET
]++];
83 priv
->fiq_buffer
[FIQ_KEYS_CNT
]--;
84 if (priv
->fiq_buffer
[FIQ_HEAD_OFFSET
] ==
85 priv
->fiq_buffer
[FIQ_BUF_LEN
])
86 priv
->fiq_buffer
[FIQ_HEAD_OFFSET
] = 0;
88 dfl
= check_data(priv
->serio
, data
);
89 scancode
= (u8
) (data
>> 1) & 0xFF;
90 serio_interrupt(priv
->serio
, scancode
, dfl
);
95 static int ams_delta_serio_open(struct serio
*serio
)
97 struct ams_delta_serio
*priv
= serio
->port_data
;
100 return regulator_enable(priv
->vcc
);
103 static void ams_delta_serio_close(struct serio
*serio
)
105 struct ams_delta_serio
*priv
= serio
->port_data
;
107 /* disable keyboard */
108 regulator_disable(priv
->vcc
);
111 static int ams_delta_serio_init(struct platform_device
*pdev
)
113 struct ams_delta_serio
*priv
;
117 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
121 priv
->fiq_buffer
= pdev
->dev
.platform_data
;
122 if (!priv
->fiq_buffer
)
125 priv
->vcc
= devm_regulator_get(&pdev
->dev
, "vcc");
126 if (IS_ERR(priv
->vcc
)) {
127 err
= PTR_ERR(priv
->vcc
);
128 dev_err(&pdev
->dev
, "regulator request failed (%d)\n", err
);
130 * When running on a non-dt platform and requested regulator
131 * is not available, devm_regulator_get() never returns
132 * -EPROBE_DEFER as it is not able to justify if the regulator
133 * may still appear later. On the other hand, the board can
134 * still set full constriants flag at late_initcall in order
135 * to instruct devm_regulator_get() to returnn a dummy one
136 * if sufficient. Hence, if we get -ENODEV here, let's convert
137 * it to -EPROBE_DEFER and wait for the board to decide or
138 * let Deferred Probe infrastructure handle this error.
145 irq
= platform_get_irq(pdev
, 0);
149 err
= devm_request_irq(&pdev
->dev
, irq
, ams_delta_serio_interrupt
,
150 IRQ_TYPE_EDGE_RISING
, DRIVER_NAME
, priv
);
152 dev_err(&pdev
->dev
, "IRQ request failed (%d)\n", err
);
156 serio
= kzalloc(sizeof(*serio
), GFP_KERNEL
);
162 serio
->id
.type
= SERIO_8042
;
163 serio
->open
= ams_delta_serio_open
;
164 serio
->close
= ams_delta_serio_close
;
165 strlcpy(serio
->name
, "AMS DELTA keyboard adapter", sizeof(serio
->name
));
166 strlcpy(serio
->phys
, dev_name(&pdev
->dev
), sizeof(serio
->phys
));
167 serio
->dev
.parent
= &pdev
->dev
;
168 serio
->port_data
= priv
;
170 serio_register_port(serio
);
172 platform_set_drvdata(pdev
, priv
);
174 dev_info(&serio
->dev
, "%s\n", serio
->name
);
179 static int ams_delta_serio_exit(struct platform_device
*pdev
)
181 struct ams_delta_serio
*priv
= platform_get_drvdata(pdev
);
183 serio_unregister_port(priv
->serio
);
188 static struct platform_driver ams_delta_serio_driver
= {
189 .probe
= ams_delta_serio_init
,
190 .remove
= ams_delta_serio_exit
,
195 module_platform_driver(ams_delta_serio_driver
);