~mil/ppbbq10

ref: cafdd2d5d06342579a906597c1715c24965fb915 ppbbq10/ppbbq10.c -rw-r--r-- 10.8 KiB
cafdd2d5Miles Alan Disable backlight functionality 10 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
#include <linux/backlight.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.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 keymap_norm[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_probe(struct i2c_client *client, const struct i2c_device_id *id);
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_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 keymap_norm[NUM_KEYCODES] = {
	[0x01] = KEY_UP,
	[0x02] = KEY_DOWN,
	[0x03] = KEY_LEFT,
	[0x04] = KEY_RIGHT,
	[0x05] = KEY_ENTER,
	[0x06] = KEY_MENU,
	[0x07] = KEY_BACK,
	[17] = KEY_LEFTALT,
	[18] = KEY_LEFTSHIFT,
	[19] = KEY_LEFTCTRL,
	['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_0,
	['$'] = KEY_GRAVE,
	['\b'] = KEY_BACKSPACE,
	['\n'] = KEY_ENTER,
};
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,
};

/* 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);
	/*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);
	return 0;
}

static irqreturn_t
ppbbq10_irq_handler(int irq, void *dev_id)
{
	struct ppbbq10_data *drv_data = 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_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	struct device *dev = &client->dev;
	struct ppbbq10_data *drv_data;
	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->keymap_norm, keymap_norm, sizeof(drv_data->keymap_norm));

	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;

	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->keymap_norm;
	input->keycodesize = sizeof(drv_data->keymap_norm[0]);
	input->keycodemax = ARRAY_SIZE(drv_data->keymap_norm);

	for (i = 0; i < NUM_KEYCODES; i++)
		__set_bit(drv_data->keymap_norm[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;
	unsigned int keycode;
	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;
		}

		pr_debug("%s key %d/%c, state: %d\n",
			__func__, data[1], data[1], data[0]);

		count -= 1;

		if (data[0] == STATE_PRESS || data[0] == STATE_RELEASE) {
			input_event(input, EV_MSC, MSC_SCAN, data[1]);

			keycode = drv_data->keymap_norm[data[1]];
			if (keycode == KEY_UNKNOWN) {
				pr_warn("%s code 0x%02X UNKNOWN\n", __func__, data[1]);
			} else {
				input_report_key(input, keycode, data[0] == STATE_PRESS);
			}
		}
	}

	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_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");