forked from ardangelo/beepberry-keyboard-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_fw.c
More file actions
341 lines (275 loc) · 7.93 KB
/
input_fw.c
File metadata and controls
341 lines (275 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// SPDX-License-Identifier: GPL-2.0-only
// Input firmware subsystem
#include <linux/input.h>
#include "config.h"
#include "i2c_helper.h"
#include "input_iface.h"
#include "bbq20kbd_pmod_codes.h"
// Globals
static uint8_t g_brightness;
static uint8_t g_last_brightness;
static uint8_t g_handle_poweroff;
// Helpers
static void input_fw_run_poweroff(struct kbd_ctx* ctx)
{
// Set LED to red
kbd_write_i2c_u8(ctx->i2c_client, REG_LED_R, 0xff);
kbd_write_i2c_u8(ctx->i2c_client, REG_LED_G, 0x0);
kbd_write_i2c_u8(ctx->i2c_client, REG_LED_B, 0x0);
kbd_write_i2c_u8(ctx->i2c_client, REG_LED, 0x1);
// Run poweroff
static const char * const poweroff_argv[] = {
"/sbin/poweroff", "now", NULL };
call_usermodehelper(poweroff_argv[0], (char**)poweroff_argv, NULL, UMH_NO_WAIT);
}
int input_fw_probe(struct i2c_client* i2c_client, struct kbd_ctx *ctx)
{
int rc;
uint8_t reg_value;
// Initialize keyboard context
g_brightness = 0x10;
g_last_brightness = 0x00;
g_handle_poweroff = 0;
// Get firmware version
if (kbd_read_i2c_u8(i2c_client, REG_VER, &ctx->version_number)) {
return -ENODEV;
}
dev_info(&i2c_client->dev,
"%s BBQX0KBD Software version: 0x%02X\n", __func__,
g_ctx->version_number);
// Write configuration 1
if (kbd_write_i2c_u8(i2c_client, REG_CFG, REG_CFG_DEFAULT_SETTING)) {
return -ENODEV;
}
// Read back configuration 1 setting
if (kbd_read_i2c_u8(i2c_client, REG_CFG, ®_value)) {
return -ENODEV;
}
dev_info_ld(&i2c_client->dev,
"%s Configuration Register Value: 0x%02X\n", __func__, reg_value);
// Write configuration 2
// No USB output and disable touch. Touch settings will be updated later
// based on module parameters
if (kbd_write_i2c_u8(i2c_client, REG_CF2, 0)) {
return -ENODEV;
}
// Read back configuration 2 setting
if (kbd_read_i2c_u8(i2c_client, REG_CF2, ®_value)) {
return rc;
}
dev_info_ld(&i2c_client->dev,
"%s Configuration 2 Register Value: 0x%02X\n",
__func__, reg_value);
// Update keyboard brightness
(void)kbd_write_i2c_u8(i2c_client, REG_BKL, g_brightness);
// Notify firmware that driver has initialized
// Clear boot indicator LED
(void)kbd_write_i2c_u8(i2c_client, REG_LED, 0);
(void)kbd_write_i2c_u8(i2c_client, REG_DRIVER_STATE, 1);
return 0;
}
void input_fw_shutdown(struct i2c_client* i2c_client, struct kbd_ctx *ctx)
{
uint8_t reg_value;
dev_info_fe(&i2c_client->dev,
"%s Shutting Down Keyboard And Screen Backlight.\n", __func__);
// Turn off LED and notify firmware that driver has shut down
(void)kbd_write_i2c_u8(i2c_client, REG_LED, 0);
(void)kbd_write_i2c_u8(i2c_client, REG_DRIVER_STATE, 0);
// Turn off backlight
(void)kbd_write_i2c_u8(i2c_client, REG_BKL, 0);
// Reenable touch events
(void)kbd_write_i2c_u8(i2c_client, REG_CF2, REG_CFG2_DEFAULT_SETTING);
// Read back version
(void)kbd_read_i2c_u8(i2c_client, REG_VER, ®_value);
}
int input_fw_consumes_keycode(struct kbd_ctx* ctx,
uint8_t *remapped_keycode, uint8_t keycode, uint8_t state)
{
// Power key runs /sbin/poweroff if `handle_poweroff` is set
if (keycode == KEY_POWER) {
if ((state == KEY_STATE_PRESSED) && g_handle_poweroff) {
input_fw_run_poweroff(ctx);
}
// Allow power key to be handled by OS
}
return 0;
}
// Brightness helpers
void input_fw_decrease_brightness(struct kbd_ctx* ctx)
{
// Decrease by delta, min at 0x0 brightness
g_brightness = (g_brightness < BBQ10_BRIGHTNESS_DELTA)
? 0x0
: g_brightness - BBQ10_BRIGHTNESS_DELTA;
// Set backlight using I2C
(void)kbd_write_i2c_u8(ctx->i2c_client, REG_BKL, g_brightness);
}
void input_fw_increase_brightness(struct kbd_ctx* ctx)
{
// Increase by delta, max at 0xff brightness
g_brightness = (g_brightness > (0xff - BBQ10_BRIGHTNESS_DELTA))
? 0xff
: g_brightness + BBQ10_BRIGHTNESS_DELTA;
// Set backlight using I2C
(void)kbd_write_i2c_u8(ctx->i2c_client, REG_BKL, g_brightness);
}
void input_fw_toggle_brightness(struct kbd_ctx* ctx)
{
// Toggle, save last brightness in context
if (g_last_brightness) {
g_brightness = g_last_brightness;
g_last_brightness = 0;
} else {
g_last_brightness = g_brightness;
g_brightness = 0;
}
// Set backlight using I2C
(void)kbd_write_i2c_u8(ctx->i2c_client, REG_BKL, g_brightness);
}
// I2C helpers
int input_fw_enable_touch_interrupts(struct kbd_ctx* ctx)
{
int rc;
uint8_t reg_value;
// Get old CF2 value
if ((rc = kbd_read_i2c_u8(ctx->i2c_client, REG_CF2, ®_value))) {
return rc;
}
// Set touch interrupt bit
reg_value |= REG_CF2_TOUCH_INT;
// Write new CF2 value
if ((rc = kbd_write_i2c_u8(ctx->i2c_client, REG_CF2, reg_value))) {
return rc;
}
// Set touch state
ctx->raised_touch_event = 1;
return 0;
}
int input_fw_disable_touch_interrupts(struct kbd_ctx* ctx)
{
int rc;
uint8_t reg_value;
// Get old CF2 value
if ((rc = kbd_read_i2c_u8(ctx->i2c_client, REG_CF2, ®_value))) {
return rc;
}
// Clear touch interrupt bit
reg_value &= ~REG_CF2_TOUCH_INT;
// Write new CF2 value
if ((rc = kbd_write_i2c_u8(ctx->i2c_client, REG_CF2, reg_value))) {
return rc;
}
// Clear touch state
ctx->raised_touch_event = 0;
return 0;
}
// Transfer from I2C FIFO to internal context FIFO
void input_fw_read_fifo(struct kbd_ctx* ctx)
{
uint8_t fifo_idx;
int rc;
// Read number of FIFO items
if (kbd_read_i2c_u8(ctx->i2c_client, REG_KEY, &ctx->key_fifo_count)) {
return;
}
ctx->key_fifo_count &= REG_KEY_KEYCOUNT_MASK;
// Read and transfer all FIFO items
for (fifo_idx = 0; fifo_idx < ctx->key_fifo_count; fifo_idx++) {
// Read 2 fifo items
if ((rc = kbd_read_i2c_2u8(ctx->i2c_client, REG_FIF,
(uint8_t*)&ctx->key_fifo_data[fifo_idx]))) {
dev_err(&ctx->i2c_client->dev,
"%s Could not read REG_FIF, Error: %d\n", __func__, rc);
return;
}
// Advance FIFO position
dev_info_fe(&ctx->i2c_client->dev,
"%s %02d: 0x%02x%02x State %d Scancode %d\n",
__func__, fifo_idx,
((uint8_t*)&ctx->key_fifo_data[fifo_idx])[0],
((uint8_t*)&ctx->key_fifo_data[fifo_idx])[1],
ctx->key_fifo_data[fifo_idx].state,
ctx->key_fifo_data[fifo_idx].scancode);
}
}
// RTC helpers
int input_fw_get_rtc(uint8_t* year, uint8_t* mon, uint8_t* day,
uint8_t* hour, uint8_t* min, uint8_t* sec)
{
int rc;
if (!g_ctx || !g_ctx->i2c_client) {
return -EAGAIN;
}
if ((rc = kbd_read_i2c_u8(g_ctx->i2c_client, REG_RTC_YEAR, year))) {
return rc;
}
if ((rc = kbd_read_i2c_u8(g_ctx->i2c_client, REG_RTC_MON, mon))) {
return rc;
}
if ((rc = kbd_read_i2c_u8(g_ctx->i2c_client, REG_RTC_MDAY, day))) {
return rc;
}
if ((rc = kbd_read_i2c_u8(g_ctx->i2c_client, REG_RTC_HOUR, hour))) {
return rc;
}
if ((rc = kbd_read_i2c_u8(g_ctx->i2c_client, REG_RTC_MIN, min))) {
return rc;
}
if ((rc = kbd_read_i2c_u8(g_ctx->i2c_client, REG_RTC_SEC, sec))) {
return rc;
}
return 0;
}
int input_fw_set_rtc(uint8_t year, uint8_t mon, uint8_t day,
uint8_t hour, uint8_t min, uint8_t sec)
{
int rc;
if (!g_ctx || !g_ctx->i2c_client) {
return -EAGAIN;
}
if ((rc = kbd_write_i2c_u8(g_ctx->i2c_client, REG_RTC_YEAR, year))) {
return rc;
}
if ((rc = kbd_write_i2c_u8(g_ctx->i2c_client, REG_RTC_MON, mon))) {
return rc;
}
if ((rc = kbd_write_i2c_u8(g_ctx->i2c_client, REG_RTC_MDAY, day))) {
return rc;
}
if ((rc = kbd_write_i2c_u8(g_ctx->i2c_client, REG_RTC_HOUR, hour))) {
return rc;
}
if ((rc = kbd_write_i2c_u8(g_ctx->i2c_client, REG_RTC_MIN, min))) {
return rc;
}
if ((rc = kbd_write_i2c_u8(g_ctx->i2c_client, REG_RTC_SEC, sec))) {
return rc;
}
if ((rc = kbd_write_i2c_u8(g_ctx->i2c_client, REG_RTC_COMMIT, 0x1))) {
return rc;
}
return 0;
}
void input_fw_set_handle_poweroff(struct kbd_ctx* ctx, uint8_t handle_poweroff)
{
g_handle_poweroff = handle_poweroff;
}
void input_fw_set_auto_off(struct kbd_ctx* ctx, uint8_t auto_off)
{
uint8_t reg_value;
// Get original value
if (kbd_read_i2c_u8(ctx->i2c_client, REG_CF2, ®_value)) {
return;
}
// Update auto-off bit
if (auto_off) {
reg_value |= REG_CF2_AUTO_OFF;
} else {
reg_value &= ~REG_CF2_AUTO_OFF;
}
// Update value
if (kbd_write_i2c_u8(ctx->i2c_client, REG_CF2, reg_value)) {
return;
}
}