forked from realPy/dothat-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdothat-cap1166.c
626 lines (377 loc) · 11.8 KB
/
dothat-cap1166.c
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* * Copyright 2015-2016
* *
* * Author: Manuel BARRAUD <[email protected]>
* *
* * This file is subject to the terms and conditions of version 2 of
* * the GNU General Public License. See the file COPYING in the main
* * directory of this archive for more details.
*
* CAPP1166
*/
#include "dothat-cap1166.h"
#include <linux/delay.h>
char *cap1166_state_str[4]={"None","Press","Hold","Release"};
static void pcap1166_timer(unsigned long data)
{
struct pcap1166 *ptrData = (struct pcap1166*)data;
schedule_work(&ptrData->work);
}
volatile static int value=0;
/*
int get_twos_comp(u8 delta)
{
int val=delta;
if (( val & (1<< 7)) != 0){
val = val - (1 << 8);
}
return val;
}
*/
int get_twos_comp(u8 delta)
{
int val=delta;
if (( val & 128)){
val = val - 256;
}
return val;
}
/*
static char *binrep (unsigned int val, char *buff, int sz) {
char *pbuff = buff;
if (sz < 1) return NULL;
if (val == 0) {
*pbuff++ = '0';
*pbuff = '\0';
return buff;
}
pbuff += sz;
*pbuff-- = '\0';
while (val != 0) {
if (sz-- == 0) return NULL;
*pbuff-- = ((val & 1) == 1) ? '1' : '0';
val >>= 1;
}
return pbuff+1;
}
static char *u8_2strbin(u8 val, char *buff, int sz) {
int i;
char *pbuff = buff;
for(i=7;i>=0;i--) {
*pbuff=(val & (1<<i)) ? '1' : '0';
pbuff++;
}
*pbuff='\0';
return buff;
}
*/
void send_ev_for_button(u8 button,CAP1166_STATE state,struct pcap1166 * data) {
int key=0;
printk(KERN_INFO "button %d %s!\n",button,cap1166_state_str[state]);
switch(button) {
case 0:
key=KEY_BACK;
break;
case 1:
key=KEY_UP;
break;
case 2:
key=KEY_DOWN;
break;
case 3:
key=KEY_LEFT;
break;
case 4:
key=KEY_ENTER;
break;
case 5:
key=KEY_RIGHT;
break;
}
if(state==CAP1166_STATE_PRESS) {
input_report_key(data->idev, key, 1);
input_sync(data->idev);
}
if(state==CAP1166_STATE_RELEASE) {
input_report_key(data->idev, key, 0);
input_sync(data->idev);
}
}
void cap1166_get_inputs_status(struct pcap1166 * data)
{
u8 touched;
u8 threshold[6];
u8 delta[6];
u8 i;
int deltaC;
u8 previousState;
u8 general_status;
touched = i2c_smbus_read_byte_data(data->client,CAP1166_R_INPUT_STATUS);
general_status = i2c_smbus_read_byte_data(data->client,CAP1166_R_GENERAL_STATUS);
if(general_status & 2) {
printk(KERN_INFO "cap1166: MTP warn\n");
}
i2c_smbus_read_i2c_block_data(data->client,CAP1166_R_INPUT_1_THRESH, sizeof(threshold), threshold);
i2c_smbus_read_i2c_block_data(data->client,CAP1166_R_INPUT_1_DELTA, sizeof(delta), delta);
//if(delta[5])
//printk(KERN_INFO "BUTTON event %d[%s] [%s] %d\n",touched,u8_2strbin(touched,buffer,32),u8_2strbin(general_status,b_gstatus,32),delta[5]);
for(i=0;i<6;i++) {
if (((1 << i) & touched)) {
deltaC = get_twos_comp(delta[i]);
previousState=data->buttonState[i];
if (deltaC >= threshold[i]) {
if(previousState==CAP1166_STATE_PRESS) {
//si HOLD ENABLE
data->buttonState[i]=CAP1166_STATE_HOLD;
send_ev_for_button(i,CAP1166_STATE_HOLD,data);
}
if(previousState==CAP1166_STATE_NONE ||previousState==CAP1166_STATE_RELEASE)
{
data->buttonState[i]=CAP1166_STATE_PRESS;
send_ev_for_button(i,CAP1166_STATE_PRESS,data);
}
}/*else {
if(previousState!=CAP1166_STATE_RELEASE) {
data->buttonState[i]=CAP1166_STATE_RELEASE;
send_ev_for_button(i,CAP1166_STATE_RELEASE);
}else
data->buttonState[i]=CAP1166_STATE_NONE;
}*/
}/* else {
if(data->buttonState[i]!=CAP1166_STATE_NONE) {
data->buttonState[i]=CAP1166_STATE_RELEASE;
send_ev_for_button(i,CAP1166_STATE_RELEASE);
}
data->buttonState[i]=CAP1166_STATE_NONE;
}*/
}
}
void check_release(struct pcap1166 * data)
{
u8 touched;
int i;
touched = i2c_smbus_read_byte_data(data->client,CAP1166_R_INPUT_STATUS);
//printk(KERN_INFO "BUTTON event %d[%s]\n",touched,u8_2strbin(touched,buffer,32));
for(i=0;i<6;i++) {
if ((data->buttonState[i]!=CAP1166_STATE_NONE&&data->buttonState[i]!=CAP1166_STATE_RELEASE)&&(((1 << i) & touched)==0)) {
send_ev_for_button(i,CAP1166_STATE_RELEASE,data);
data->buttonState[i]=CAP1166_STATE_NONE;
}
}
}
void cap1166_ack_new_event(struct pcap1166 * data)
{
u8 maincontrol=0;
maincontrol = i2c_smbus_read_byte_data(data->client,CAP1166_R_MAIN_CONTROL);
maincontrol= (maincontrol & 0b11111110);
i2c_smbus_write_byte_data(data->client,CAP1166_R_MAIN_CONTROL,maincontrol);
}
void cap1166_poll_new_event(struct pcap1166 * data)
{
u8 maincontrol;
maincontrol=i2c_smbus_read_byte_data(data->client,CAP1166_R_MAIN_CONTROL);
if((maincontrol & 1)) {
//printk(KERN_INFO "cap1166: New trigger\n");
cap1166_get_inputs_status(data);
data->needCheckRelease=1;
//acknownledge event
cap1166_ack_new_event(data);
}else {
if(data->needCheckRelease) {
check_release(data);
data->needCheckRelease=0;
}
}
}
static void pcap1166_work(struct work_struct *work)
{
struct pcap1166 *ptrData = container_of(work, struct pcap1166, work);
mutex_lock(&ptrData->lock);
cap1166_poll_new_event(ptrData);
//printk(KERN_INFO "cap1166: Poll\n");
if(!ptrData->willUnload) {
mod_timer(&ptrData->timer, jiffies+HZ/20);
}else
printk(KERN_INFO "crash prevent\n");
mutex_unlock(&ptrData->lock);
}
void cap1166_enable_repeats(struct i2c_client *client,u8 inputs)
{
i2c_smbus_write_byte_data(client,CAP1166_R_REPEAT_ENABLE,inputs);
}
void cap1166_enable_interrupts(struct i2c_client *client,u8 interrupts)
{
i2c_smbus_write_byte_data(client,CAP1166_R_INTERRUPT_EN ,interrupts);
}
void cap1166_enable_inputs(struct i2c_client *client,u8 inputs)
{
i2c_smbus_write_byte_data(client,CAP1166_R_INPUT_ENABLE ,inputs);
}
void cap1166_recalibrate(struct pcap1166 *data)
{
i2c_smbus_write_byte_data(data->client,CAP1166_R_RECALIBRATION_REGISTER,0xA);
i2c_smbus_write_byte_data(data->client,CAP1166_R_CALIBRATION,0b00111111);
printk(KERN_INFO "recalibration...\n");
while(i2c_smbus_read_byte_data(data->client,CAP1166_R_CALIBRATION)){
printk(KERN_INFO "recalibration not yet finished\n");
msleep(10);
}
}
void cap1166_touch_init(struct pcap1166 *data)
{
int i;
int result;
//force recalibration
data->idev=devm_input_allocate_device(&data->client->dev);
data->idev->name = "dot3k touch button";
data->idev->evbit[0] = BIT(EV_KEY);
set_bit(KEY_RIGHT, data->idev->keybit);
set_bit(KEY_LEFT, data->idev->keybit);
set_bit(KEY_UP, data->idev->keybit);
set_bit(KEY_DOWN, data->idev->keybit);
set_bit(KEY_BACK, data->idev->keybit);
set_bit(KEY_ENTER, data->idev->keybit);
cap1166_recalibrate(data);
cap1166_enable_inputs(data->client,0b111111);
cap1166_enable_interrupts(data->client,0b111111);
i2c_smbus_write_byte_data(data->client,CAP1166_R_SAMPLING_CONFIG, 0b00001000); // 1sample per measure, 1.28ms time, 35ms cycle
i2c_smbus_write_byte_data(data->client,CAP1166_R_SENSITIVITY, 0b01100000); // 2x sensitivity
i2c_smbus_write_byte_data(data->client,CAP1166_R_GENERAL_CONFIG, 0b00111000);
i2c_smbus_write_byte_data(data->client,CAP1166_R_CONFIGURATION2, 0b01100000);
cap1166_enable_repeats(data->client,0b00000000);
for(i=0;i<8;i++) {
data->buttonState[i]=CAP1166_STATE_NONE;
}
result=input_register_device(data->idev);
mutex_init(&data->lock);
INIT_WORK(&data->work, pcap1166_work);
init_timer(&data->timer);
data->timer.function = pcap1166_timer;
data->timer.data = (unsigned long)data;
schedule_work(&data->work);
}
void cap1166_touch_detach(struct pcap1166 *data)
{
data->willUnload=1;
del_timer_sync(&data->timer);
cancel_work_sync(&data->work);
//input_unregister_device(data->idev);
//flush_scheduled_work();
}
int cap1166_graph_set_leds(struct i2c_client *client,u8 leds)
{
return i2c_smbus_write_byte_data(client,CAP1166_LED_OUTPUT_CON,leds);
}
u8 cap1166_graph_get_leds(struct i2c_client *client)
{
return i2c_smbus_read_byte_data(client,CAP1166_LED_OUTPUT_CON);
}
int cap1166_graph_set_polarity(struct i2c_client *client,u8 leds)
{
//inverse polarity
return i2c_smbus_write_byte_data(client,CAP1166_R_LED_POLARITY,leds);
}
u8 cap1166_graph_get_polarity(struct i2c_client *client)
{
return i2c_smbus_read_byte_data(client,CAP1166_R_LED_POLARITY);
}
int cap1166_graph_set_leds_luminosity(struct i2c_client *client,u8 duty)
{
//the luminosity is apply to all leds set logical ON
return i2c_smbus_write_byte_data(client,CAP1166_LED_DIRECT_DUTY,duty);
}
u8 cap1166_graph_get_leds_luminosity(struct i2c_client *client)
{
return i2c_smbus_read_byte_data(client,CAP1166_LED_DIRECT_DUTY);
}
void cap1166_graph_set_leds_blink(struct i2c_client *client,u8 leds)
{
//the luminosity is apply to all leds set logical ON
int i;
u8 registerLeds1=0;
u8 registerLeds2=0;
struct pcap1166 *data=NULL;
for(i=0;i<6;i++) {
if(i<4) {
if(leds & ( 1<<i)) {
registerLeds1|=((1<<i*2)|(1<<(i*2+1)));
}
}else {
if(leds & ( 1<<i)) {
registerLeds2|=((1<<(i-4)*2)|(1<<((i-4)*2+1)));
}
}
}
data = i2c_get_clientdata(client);
if(data)
data->leds_blink=leds;
i2c_smbus_write_byte_data(client,CAP1166_LED_BEHAVIOR1,registerLeds1);
i2c_smbus_write_byte_data(client,CAP1166_LED_BEHAVIOR2,registerLeds2);
}
int cap1166_graph_set_breath_period(struct i2c_client *client,u8 period)
{
return i2c_smbus_write_byte_data(client,CAP1166_LED_BREATH_PERIOD,period);
}
u8 cap1166_graph_get_breath_period(struct i2c_client *client)
{
return i2c_smbus_read_byte_data(client,CAP1166_LED_BREATH_PERIOD);
}
int cap1166_graph_off(struct i2c_client *client)
{
cap1166_graph_set_leds(client,0b00000000);
cap1166_graph_set_polarity(client,0b00000000);
cap1166_graph_set_leds_luminosity(client,0b000000);
return 0;
}
//up to down
int cap1166_set_progress_bar(struct i2c_client *client,u8 value)
{
//from 0 to 255 use userland to tranpose in pourcentage
int step=0;
u8 full_leds;
u8 duty_led;
int i;
u8 polarity=0;
step=NUM_LEDS*STEP_DUTY*value/255;
full_leds=step/16;
duty_led=step%16;
//the tips here is to use polarity to light the full leds and set only the duty leds at the good luminosity
for(i=0;i<full_leds;i++)
polarity|=1<<i;
if(step>0) {
cap1166_graph_set_leds_luminosity(client,duty_led <<4);
cap1166_graph_set_polarity(client,polarity);
cap1166_graph_set_leds(client,1<<(full_leds));
}else
cap1166_graph_off(client);
return 0;
}
//down to up
int cap1166_set_progress_bar_reverse(struct i2c_client *client,u8 value)
{
//from 0 to 255 use userland to tranpose in pourcentage
int step=0;
u8 full_leds;
u8 duty_led;
int i;
u8 polarity=0;
step=NUM_LEDS*STEP_DUTY*value/255;
full_leds=step/16;
duty_led=step%16;
//the tips here is to use polarity to light the full leds and set only the duty leds at the good luminosity
for(i=NUM_LEDS;i>=(NUM_LEDS-full_leds);i--)
polarity|=1<<i;
if(step>0) {
cap1166_graph_set_leds_luminosity(client,duty_led <<4);
cap1166_graph_set_polarity(client,polarity);
cap1166_graph_set_leds(client,1<<(NUM_LEDS-full_leds-1));
}else
cap1166_graph_off(client);
return 0;
}
u8 cap1166_get_ProductID(struct i2c_client *client)
{
u8 product_id=0;
product_id=i2c_smbus_read_byte_data(client,CAP1166_PRODUCT_ID);
return product_id;
}