~mil/ppbbq10

ppbbq10/ppbbq10.c -rw-r--r-- 14.4 KiB
c69e8a4eMiles Alan Add WIP bumper-style case models 7 months ago
                                                                                
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include <linux/backlight.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/types.h>

/* defines */
#define CFG_CAPSLOCK_INT	BIT(2)
#define CFG_KEY_INT	BIT(4)
#define CFG_NUMLOCK_INT	BIT(3)
#define CFG_OVERFLOW_INT	BIT(1)
#define CFG_OVERFLOW_ON	BIT(0)
#define CFG_PANIC_INT	BIT(5)
#define CFG_REPORT_MODS	BIT(6)
#define CFG_USE_MODS	BIT(7)
#define INT_CAPSLOCK	BIT(1)
#define INT_KEY	BIT(3)
#define INT_NUMLOCK	BIT(2)
#define INT_OVERFLOW	BIT(0)
#define INT_PANIC	BIT(4)
#define KEY_COUNT_MASK	0x1F
#define NUM_KEYCODES	256
#define REG_BKL	0x05
#define REG_CFG	0x02
#define REG_DEB	0x06
#define REG_FIF	0x09
#define REG_FRQ	0x07
#define REG_INT	0x03
#define REG_KEY	0x04
#define REG_RST	0x08
#define REG_VER	0x01
#define STATE_HOLD	2
#define STATE_IDLE	0
#define STATE_PRESS	1
#define STATE_RELEASE	3
#define WRITE_MASK	BIT(7)
#define _KEY_CAPSLOCK	BIT(5)
#define _KEY_NUMLOCK	BIT(6)
#define DEBUG

/* structs */
struct ppbbq10_data {
	unsigned short int mod_held;
	unsigned short int mod_tapmodifier; // TODO: []mod_tapmodifier for multiple prefixes
	unsigned int layer;
	short layer_0[NUM_KEYCODES];
	short layer_1[NUM_KEYCODES];

	struct i2c_client *client;
	struct input_dev *input;
};


/* function declarations */
static int ppbbq10_backlight_update_status(struct backlight_device *bd);
static void __exit ppbbq10_exit(void);
static int __init ppbbq10_init(void);
static irqreturn_t ppbbq10_irq_handler(int irq, void *dev_id);
static int ppbbq10_poll_kthread(void *data);
static int ppbbq10_probe(struct i2c_client *client);
static void ppbbq10_read_fifo(struct ppbbq10_data *drv_data);
static int ppbbq10_read_reg(struct ppbbq10_data *drv_data, u8 reg, u8 *buf, u8 len);
static void ppbbq10_report_keyevent(struct ppbbq10_data *drv_data, u8 keystate, u8 keycode);
static void ppbbq10_update_locks(struct ppbbq10_data *drv_data);
static int ppbbq10_write_data(struct ppbbq10_data *drv_data, u8 reg, const u8 *buf, u8 len);
static int ppbbq10_write_reg(struct ppbbq10_data *drv_data, u8 reg);


/* variables */
static unsigned short layer_0[NUM_KEYCODES] = {
	[0x01] = KEY_UP,
	[0x02] = KEY_DOWN,
	[0x03] = KEY_LEFT,
	[0x04] = KEY_RIGHT,
	[0x05] = KEY_ENTER,
	[0x06] = KEY_MENU,
	[0x07] = KEY_BACK,
	[26] = KEY_LEFTALT,
	[27] = KEY_LEFTSHIFT,
	[28] = KEY_LEFTCTRL,
	[29] = KEY_YEN,
	['A'] = KEY_A,
	['B'] = KEY_B,
	['C'] = KEY_C,
	['D'] = KEY_D,
	['E'] = KEY_E,
	['F'] = KEY_F,
	['G'] = KEY_G,
	['H'] = KEY_H,
	['I'] = KEY_I,
	['J'] = KEY_J,
	['K'] = KEY_K,
	['L'] = KEY_L,
	['M'] = KEY_M,
	['N'] = KEY_N,
	['O'] = KEY_O,
	['P'] = KEY_P,
	['Q'] = KEY_Q,
	['R'] = KEY_R,
	['S'] = KEY_S,
	['T'] = KEY_T,
	['U'] = KEY_U,
	['W'] = KEY_W,
	['V'] = KEY_V,
	['X'] = KEY_X,
	['Y'] = KEY_Y,
	['Z'] = KEY_Z,
	[' '] = KEY_SPACE,
	['~'] = KEY_ESC,
	['$'] = KEY_TAB,
	['\b'] = KEY_BACKSPACE,
	['\n'] = KEY_ENTER,
};
static short layer_1[NUM_KEYCODES] = {
	[0x01] = KEY_UP,
	[0x02] = KEY_DOWN,
	[0x03] = KEY_LEFT,
	[0x04] = KEY_RIGHT,
	[0x05] = KEY_ENTER,
	[0x06] = KEY_MENU,
	[0x07] = KEY_BACK,
	[26] = KEY_LEFTALT,
	[27] = KEY_LEFTSHIFT,
	[28] = KEY_LEFTCTRL,
	[29] = KEY_YEN,
	['A'] = KEY_8 * -1, // (*)
	['B'] = KEY_1 * -1, // (!)
	['C'] = KEY_9,
	['D'] = KEY_5,
	['E'] = KEY_2,
	['F'] = KEY_6,
	['G'] = KEY_SLASH,
	['H'] = KEY_SEMICOLON * -1, // (:)
	['I'] = KEY_MINUS,
	['J'] = KEY_SEMICOLON,
	['K'] = KEY_APOSTROPHE,
	['L'] = KEY_APOSTROPHE * -1, // (")
	['M'] = KEY_DOT,
	['N'] = KEY_COMMA,
	['O'] = KEY_EQUAL * -1, // (+)
	['P'] = KEY_2 * -1, // (@)
	['Q'] = KEY_3 * -1, // (#)
	['R'] = KEY_3,
	['S'] = KEY_4,
	['T'] = KEY_9 * -1, // (()
	['U'] = KEY_MINUS * -1, // (_)
	['W'] = KEY_1,
	['V'] = KEY_SLASH * -1, // (?)
	['X'] = KEY_8,
	['Y'] = KEY_0 * -1, // ())
	['Z'] = KEY_7,

	[' '] = KEY_DOWN,
	['~'] = KEY_UP,
	['$'] = KEY_LEFT,
	['\b'] = KEY_TAB,
	['\n'] = KEY_RIGHT,
};
static struct i2c_board_info ppbbq10_i2c_board_info[] __initdata = {
	{ I2C_BOARD_INFO("ppbbq10", 0x1f), /*.irq = gpio_to_irq(11)*/ }
};
static const struct backlight_ops backlight_ops = {
	.options		= BL_CORE_SUSPENDRESUME,
	.update_status	= ppbbq10_backlight_update_status,
};
static const struct i2c_device_id ppbbq10_id[] = { { "ppbbq10", 0, } };
static struct i2c_adapter *adapter;
static struct i2c_client *client;
static struct backlight_properties backlight_props = {
	.type			= BACKLIGHT_PLATFORM,
	.max_brightness = 255,
	.brightness		= 127,
};
static struct task_struct *ppbbq10_kthread;
struct ppbbq10_data *drv_data;


/* function implementations */
static int
ppbbq10_backlight_update_status(struct backlight_device *bd)
{
	const struct backlight_properties *p = &bd->props;
	const u8 intensity = (p->power == FB_BLANK_UNBLANK) ? p->brightness : 0;
	struct ppbbq10_data *drv_data = dev_get_drvdata(&bd->dev);

	return ppbbq10_write_data(drv_data, REG_BKL, &intensity, sizeof(intensity));
}

static void __exit
ppbbq10_exit(void)
{
	printk(KERN_INFO "Unregister ppbbq10 kernel module.\n");
	i2c_unregister_device(client);
	kthread_stop(ppbbq10_kthread);

	/*i2c_del_driver(&ppbbq10_driver);*/
}

static int __init
ppbbq10_init(void)
{
	adapter = i2c_get_adapter(3);
	printk(KERN_INFO "Initialize ppbq10 kernel module.\n");
	if (!adapter) {
		printk(KERN_INFO "Error acquiring i2c adapter 3.\n");
		return -1;
	}
	client = i2c_new_client_device(adapter, ppbbq10_i2c_board_info);
	//client->irq = gpio_to_irq(2);
	ppbbq10_probe(client);
	ppbbq10_kthread = kthread_create(ppbbq10_poll_kthread, NULL, "ppbbq10_pollthread");
	wake_up_process(ppbbq10_kthread);
	return 0;
}

static irqreturn_t
ppbbq10_irq_handler(int irq, void *dev_id)
{
	struct i2c_client *client = drv_data->client;
	int error;
	u8 reg;

	pr_debug("%s fired\n", __func__);

	error = ppbbq10_read_reg(drv_data, REG_INT, &reg, sizeof(u8));
	if (error < 0) {
		dev_err(&client->dev, "%s: failed to read KEY: %d\n",
			__func__, error);
		return IRQ_NONE;
	}

	pr_debug("int: 0x%02x\n", reg);

	if (reg == 0x00)
		return IRQ_NONE;

	if (reg & INT_OVERFLOW)
		dev_warn(&client->dev, "overflow occurred\n");

	if (reg & INT_CAPSLOCK || reg & INT_NUMLOCK)
		ppbbq10_update_locks(drv_data);

	if (reg & INT_KEY)
		ppbbq10_read_fifo(drv_data);

	reg = 0x00;
	error = ppbbq10_write_data(drv_data, REG_INT, &reg, sizeof(reg));
	if (error < 0)
		dev_err(&client->dev, "%s: failed to clear REG_INT, error: %d\n",
			__func__, error);

	return IRQ_HANDLED;
}

static int ppbbq10_poll_kthread(void *data)
{
	while (!kthread_should_stop()) {
		usleep_range(1000 * 1, (1000 * 1) + 1); // 5ms poll
		ppbbq10_irq_handler(0, NULL);
	}
	return 0;
}

static int
ppbbq10_probe(struct i2c_client *client)
{
	struct device *dev = &client->dev;
	struct backlight_device *bd;
	struct input_dev *input;
	int error;
	int i;
	u8 reg;

	drv_data = devm_kzalloc(dev, sizeof(*drv_data), GFP_KERNEL);
	if (!drv_data)
		return -ENOMEM;

	drv_data->client = client;
	memcpy(drv_data->layer_0, layer_0, sizeof(drv_data->layer_0));
	memcpy(drv_data->layer_1, layer_1, sizeof(drv_data->layer_1));
	drv_data->layer = 0;

	error = ppbbq10_write_reg(drv_data, REG_RST);
	if (error)
		return -ENODEV;

	msleep(100);

	error = ppbbq10_read_reg(drv_data, REG_VER, &reg, sizeof(reg));
	if (error)
		return -ENODEV;

	printk("%s version: 0x%02X\n", __func__, reg);

	reg = CFG_OVERFLOW_ON | CFG_OVERFLOW_INT | CFG_CAPSLOCK_INT |
			CFG_NUMLOCK_INT | CFG_KEY_INT | CFG_REPORT_MODS;
	error = ppbbq10_write_data(drv_data, REG_CFG, &reg, sizeof(reg));
	if (error)
		return -ENODEV;

	// TODO: cleanup
	//bd = devm_backlight_device_register(dev, client->name, dev,
	//				    drv_data,
	//				    &backlight_ops,
	//				    &backlight_props);
	//if (IS_ERR(bd))
	//	return PTR_ERR(bd);

	//error = backlight_update_status(bd);
	//if (error)
	//	return -ENODEV;

	input = devm_input_allocate_device(dev);
	if (!input)
		return -ENOMEM;

	drv_data->input = input;
	drv_data->mod_held = 0;
	drv_data->mod_tapmodifier = 0;
	drv_data->layer = 0;

	input->name = client->name;
	input->id.bustype = BUS_I2C;
	input->id.vendor  = 0x0001;
	input->id.product = 0x0001;
	input->id.version = 0x0001;
	input->keycode = drv_data->layer_0;
	input->keycodesize = sizeof(drv_data->layer_0[0]);
	input->keycodemax = ARRAY_SIZE(drv_data->layer_0) + ARRAY_SIZE(drv_data->layer_1) ;

	for (i = 0; i < NUM_KEYCODES; i++)
		__set_bit(drv_data->layer_0[i], input->keybit);
	for (i = 0; i < NUM_KEYCODES; i++)
		__set_bit(drv_data->layer_1[i], input->keybit);

	__clear_bit(KEY_RESERVED, input->keybit);

	__set_bit(EV_REP, input->evbit);
	__set_bit(EV_KEY, input->evbit);

	input_set_capability(input, EV_MSC, MSC_SCAN);

	error = devm_request_threaded_irq(dev, client->irq,
										NULL, ppbbq10_irq_handler,
										IRQF_SHARED | IRQF_ONESHOT,
										client->name, drv_data);
	if (error) {
		dev_err(dev, "Failed to claim irq %d; error %d\n", client->irq, error);
		//return error;
	}

	error = input_register_device(input);
	if (error) {
		dev_err(dev, "Failed to register input device, error: %d\n", error);
		return error;
	}

	return 0;
}

static void
ppbbq10_read_fifo(struct ppbbq10_data *drv_data)
{
	struct input_dev *input = drv_data->input;
	struct i2c_client *client = drv_data->client;
	u8 data[2];
	u8 count;
	int error;

	error = ppbbq10_read_reg(drv_data, REG_KEY, data, sizeof(u8));
	if (error < 0) {
		dev_err(&client->dev, "%s failed to read REG_KEY, error: %d\n",
			__func__, error);
		return;
	}

	pr_debug("%s status: 0x%02X\n", __func__, data[0]);

	count = (data[0] & KEY_COUNT_MASK);

	while (count > 0) {
		error = ppbbq10_read_reg(drv_data, REG_FIF, data, sizeof(u8) * 2);
		if (error < 0) {
			dev_err(&client->dev, "%s failed to read REG_FIF, error: %d\n",
				__func__, error);
			return;
		}
		count -= 1;

		if (data[0] == STATE_PRESS || data[0] == STATE_RELEASE && data[1] != 29)
			input_event(input, EV_MSC, MSC_SCAN, data[1]);
		ppbbq10_report_keyevent(drv_data, data[0], data[1]);
	}

	input_sync(input);
}

static int
ppbbq10_read_reg(struct ppbbq10_data *drv_data, u8 reg, u8 *buf, u8 len)
{
	struct i2c_client *client = drv_data->client;
	int error;

	struct i2c_msg msgs[] = {
		{ .addr = client->addr, .flags = client->flags, .len = sizeof(u8), .buf = &reg, },
		{ .addr = client->addr, .flags = client->flags | I2C_M_RD, .len = len, .buf = buf }
	};

	error = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
	if (error != ARRAY_SIZE(msgs)) {
		dev_err(&client->dev, "%s failed, reg: %d, error: %d\n",
			__func__, reg, error);

		return error;
	}

	return 0;
}

static void
ppbbq10_report_keyevent(struct ppbbq10_data *drv_data, u8 key_state, u8 key_code)
{
	unsigned int key_mapped;
	printk(KERN_INFO "%s key %d/%c, state: %d\n", __func__, key_code, key_code, key_state);

	// Map raw key_code to mapped key, if sym is tapped modifier or held,
	// prime shift as the tapped modifier is mapped key < 0
	if (drv_data->mod_held == KEY_YEN || drv_data->mod_tapmodifier == KEY_YEN) {
		key_mapped = abs(drv_data->layer_1[key_code]);
		drv_data->mod_tapmodifier = drv_data->layer_1[key_code] < 0 ? KEY_LEFTSHIFT : 0;
	} else {
		key_mapped = drv_data->layer_0[key_code];
	}

	// If pressing a modifier
	if (key_mapped == KEY_UNKNOWN) {
		pr_warn("%s code 0x%02X UNKNOWN - not mapped\n", __func__, key_mapped);
	} else if (
		key_mapped == KEY_LEFTALT ||
		key_mapped == KEY_LEFTSHIFT ||
		key_mapped == KEY_LEFTCTRL ||
		key_mapped == KEY_YEN
	) {
		if (key_mapped != KEY_YEN && key_state != STATE_HOLD) {
			printk(KERN_INFO "Registering modifier key: [%c], e.g. keymapped [%d] with state [%d]\n", key_code, key_mapped, key_state);
			input_report_key(drv_data->input, key_mapped, key_state == STATE_PRESS);
		}

		if (key_state == STATE_HOLD) {
			drv_data->mod_held = key_mapped;
		} else if (key_state == STATE_RELEASE && drv_data->mod_held == 0) {
			drv_data->mod_tapmodifier = key_mapped;
		} else if (key_state == STATE_RELEASE) {
			drv_data->mod_held = 0;
		}
	} else if (drv_data->mod_tapmodifier && key_state == STATE_RELEASE) {
		printk(KERN_INFO "Registering key [%c] with tap modifier [%d]\n", key_code, drv_data->mod_tapmodifier);
		input_report_key(drv_data->input, drv_data->mod_tapmodifier, 1);
		input_report_key(drv_data->input, key_mapped, 1);
		input_report_key(drv_data->input, key_mapped, 0);
		input_report_key(drv_data->input, drv_data->mod_tapmodifier, 0);
		drv_data->mod_tapmodifier = 0;
	} else if (drv_data->mod_tapmodifier == 0 && key_state != STATE_HOLD) {
		printk(KERN_INFO "Registering key normally: [%c] w state [%d]\n", key_code, key_state);
		input_report_key(drv_data->input, key_mapped, key_state == STATE_PRESS);
	}
}


static void
ppbbq10_update_locks(struct ppbbq10_data *drv_data)
{
	struct input_dev *input = drv_data->input;
	struct i2c_client *client = drv_data->client;
	u8 data;
	int error;

	error = ppbbq10_read_reg(drv_data, REG_KEY, &data, sizeof(u8));
	if (error < 0) {
		dev_err(&client->dev, "%s failed to read REG_KEY, error: %d\n",
			__func__, error);
		return;
	}

	pr_warn("%s status: 0x%02X\n", __func__, data);

	//input_report_key(input, KEY_NUMLOCK, data & _KEY_NUMLOCK);
	//input_report_key(input, KEY_CAPSLOCK, data & _KEY_CAPSLOCK);

	input_sync(input);
}

static int
ppbbq10_write_reg(struct ppbbq10_data *drv_data, u8 reg)
{
	struct i2c_client *client = drv_data->client;
	int error;

	struct i2c_msg msgs[] = {
		{ .addr = client->addr, .flags = I2C_M_STOP, .len = sizeof(u8), .buf = &reg, },
	};

	error = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
	if (error != ARRAY_SIZE(msgs)) {
		dev_err(&client->dev, "%s failed, reg: %d, error: %d\n",
		__func__, reg, error);

		return error;
	}

	return 0;
}

static int
ppbbq10_write_data(struct ppbbq10_data *drv_data, u8 reg, const u8 *buf, u8 len)
{
	struct i2c_client *client = drv_data->client;
	int error;

	//reg |= 0x80;
	u8 buff[2];
	buff[0] = reg | 0x80;
	buff[1] = *buf;


	struct i2c_msg msgs[] = {
		//{ .addr = client->addr, .flags = 0, .len = sizeof(u8), .buf = &reg, },
		//{ .addr = client->addr, .flags = 0, .len = len, .buf = (u8*)buf, },
		{ .addr = client->addr, .flags = 0, .len = sizeof(buff), .buf = buff, },
	};

	error = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
	if (error != ARRAY_SIZE(msgs)) {
		dev_err(&client->dev, "%s failed, reg: %d, error: %d\n",
			__func__, reg, error);

		return error;
	}

	return 0;
}

module_init(ppbbq10_init);
module_exit(ppbbq10_exit);
MODULE_AUTHOR("arturo182 <arturo182@tlen.pl> & miles alan <m@milesalan.com>");
MODULE_DESCRIPTION("Keyboard driver for BBQ10 PMOD that works OOB with the Pinephone");
MODULE_LICENSE("GPL");