driver_axp209.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include <sys/select.h>
  10. #include <linux/input.h>
  11. #include "read_conf_file.h"
  12. #include <assert.h>
  13. #include <linux/i2c.h>
  14. #include <linux/i2c-dev.h>
  15. #include "smbus.h"
  16. #include "driver_axp209.h"
  17. /****************************************************************
  18. * Defines
  19. ****************************************************************/
  20. #define DEBUG_AXP209_PRINTF (0)
  21. #if (DEBUG_AXP209_PRINTF)
  22. #define DEBUG_PRINTF(...) printf(__VA_ARGS__);
  23. #else
  24. #define DEBUG_PRINTF(...)
  25. #endif
  26. /****************************************************************
  27. * Static variables
  28. ****************************************************************/
  29. static int fd_axp209;
  30. static char i2c0_sysfs_filename[] = "/dev/i2c-0";
  31. /****************************************************************
  32. * Static functions
  33. ****************************************************************/
  34. /****************************************************************
  35. * Public functions
  36. ****************************************************************/
  37. bool axp209_init(void) {
  38. int err;
  39. /* Open i2c file interface */
  40. if ((fd_axp209 = open(i2c0_sysfs_filename,O_RDWR)) < 0) {
  41. printf("In axp209_init - Failed to open the I2C bus %s", i2c0_sysfs_filename);
  42. // ERROR HANDLING; you can check errno to see what went wrong
  43. return false;
  44. }
  45. /* Acquire AXP209 bus */
  46. if (ioctl(fd_axp209, I2C_SLAVE, AXP209_I2C_ADDR) < 0) {
  47. printf("In axp209_init - Failed to acquire bus access and/or talk to slave, trying to force it\n");
  48. // ERROR HANDLING; you can check errno to see what went wrong
  49. if (ioctl(fd_axp209, I2C_SLAVE_FORCE, AXP209_I2C_ADDR) < 0) {
  50. printf("In axp209_init - Failed to acquire FORCED bus access and/or talk to slave.\n");
  51. // ERROR HANDLING; you can check errno to see what went wrong
  52. return false;
  53. }
  54. }
  55. /* Set PEK Long press delay to 2.5s*/
  56. err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_REG_PEK_PARAMS, 0x9F);
  57. if(err < 0){
  58. printf("ERROR Setting AXP209 PEK Long press delay to 2.5s\n");
  59. }
  60. /* Set N_OE Shutdown delay to 3s*/
  61. err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_REG_32H, 0x47);
  62. if(err < 0){
  63. printf("ERROR Setting AXP209 N_OE Shutdown delay to 3S\n");
  64. }
  65. /* Enable only chosen interrupts (PEK short and long presses)*/
  66. /*err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_1_ENABLE, 0x00);
  67. if(err < 0){
  68. printf("ERROR initializing interrupts 1 for AXP209\n");
  69. }
  70. err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_2_ENABLE, 0x00);
  71. if(err < 0){
  72. printf("ERROR initializing interrupts 2 for AXP209\n");
  73. }*/
  74. err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_3_ENABLE, 0x03);
  75. if(err < 0){
  76. printf("ERROR initializing interrupts 3 for AXP209\n");
  77. }
  78. /*err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_4_ENABLE, 0x00);
  79. if(err < 0){
  80. printf("ERROR initializing interrupts 4 for AXP209\n");
  81. }
  82. err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_5_ENABLE, 0x00);
  83. if(err < 0){
  84. printf("ERROR initializing interrupts 5 for AXP209\n");
  85. }*/
  86. return true;
  87. }
  88. bool axp209_deinit(void) {
  89. // Close I2C open interface
  90. close(fd_axp209);
  91. return true;
  92. }
  93. int axp209_read_interrupt_bank_3(void){
  94. int val = i2c_smbus_read_byte_data ( fd_axp209 , AXP209_INTERRUPT_BANK_3_STATUS );
  95. if(val < 0){
  96. return val;
  97. }
  98. // Clear interrupts
  99. int err = i2c_smbus_write_byte_data( fd_axp209 ,AXP209_INTERRUPT_BANK_3_STATUS, 0xFF );
  100. if(err < 0){
  101. return err;
  102. }
  103. DEBUG_PRINTF("READ AXP209_INTERRUPT_BANK_3_STATUS: 0x%02X\n",val);
  104. return 0xFF & val;
  105. }