This repository was archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdriver_axp209.c
More file actions
executable file
·120 lines (105 loc) · 3.87 KB
/
driver_axp209.c
File metadata and controls
executable file
·120 lines (105 loc) · 3.87 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
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <linux/input.h>
#include "read_conf_file.h"
#include <assert.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "smbus.h"
#include "driver_axp209.h"
/****************************************************************
* Defines
****************************************************************/
#define DEBUG_AXP209_PRINTF (0)
#if (DEBUG_AXP209_PRINTF)
#define DEBUG_PRINTF(...) printf(__VA_ARGS__);
#else
#define DEBUG_PRINTF(...)
#endif
/****************************************************************
* Static variables
****************************************************************/
static int fd_axp209;
static char i2c0_sysfs_filename[] = "/dev/i2c-0";
/****************************************************************
* Static functions
****************************************************************/
/****************************************************************
* Public functions
****************************************************************/
bool axp209_init(void) {
int err;
/* Open i2c file interface */
if ((fd_axp209 = open(i2c0_sysfs_filename,O_RDWR)) < 0) {
printf("In axp209_init - Failed to open the I2C bus %s", i2c0_sysfs_filename);
// ERROR HANDLING; you can check errno to see what went wrong
return false;
}
/* Acquire AXP209 bus */
if (ioctl(fd_axp209, I2C_SLAVE, AXP209_I2C_ADDR) < 0) {
printf("In axp209_init - Failed to acquire bus access and/or talk to slave, trying to force it\n");
// ERROR HANDLING; you can check errno to see what went wrong
if (ioctl(fd_axp209, I2C_SLAVE_FORCE, AXP209_I2C_ADDR) < 0) {
printf("In axp209_init - Failed to acquire FORCED bus access and/or talk to slave.\n");
// ERROR HANDLING; you can check errno to see what went wrong
return false;
}
}
/* Set PEK Long press delay to 2.5s*/
err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_REG_PEK_PARAMS, 0x9F);
if(err < 0){
printf("ERROR Setting AXP209 PEK Long press delay to 2.5s\n");
}
/* Set N_OE Shutdown delay to 3s*/
err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_REG_32H, 0x47);
if(err < 0){
printf("ERROR Setting AXP209 N_OE Shutdown delay to 3S\n");
}
/* Enable only chosen interrupts (PEK short and long presses)*/
/*err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_1_ENABLE, 0x00);
if(err < 0){
printf("ERROR initializing interrupts 1 for AXP209\n");
}
err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_2_ENABLE, 0x00);
if(err < 0){
printf("ERROR initializing interrupts 2 for AXP209\n");
}*/
err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_3_ENABLE, 0x03);
if(err < 0){
printf("ERROR initializing interrupts 3 for AXP209\n");
}
/*err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_4_ENABLE, 0x00);
if(err < 0){
printf("ERROR initializing interrupts 4 for AXP209\n");
}
err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_5_ENABLE, 0x00);
if(err < 0){
printf("ERROR initializing interrupts 5 for AXP209\n");
}*/
return true;
}
bool axp209_deinit(void) {
// Close I2C open interface
close(fd_axp209);
return true;
}
int axp209_read_interrupt_bank_3(void){
int val = i2c_smbus_read_byte_data ( fd_axp209 , AXP209_INTERRUPT_BANK_3_STATUS );
if(val < 0){
return val;
}
// Clear interrupts
int err = i2c_smbus_write_byte_data( fd_axp209 ,AXP209_INTERRUPT_BANK_3_STATUS, 0xFF );
if(err < 0){
return err;
}
DEBUG_PRINTF("READ AXP209_INTERRUPT_BANK_3_STATUS: 0x%02X\n",val);
return 0xFF & val;
}