driver_axp209.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if ((fd_axp209 = open(i2c0_sysfs_filename,O_RDWR)) < 0) {
  39. printf("In axp209_init - Failed to open the I2C bus %s", i2c0_sysfs_filename);
  40. // ERROR HANDLING; you can check errno to see what went wrong
  41. return false;
  42. }
  43. if (ioctl(fd_axp209, I2C_SLAVE, AXP209_I2C_ADDR) < 0) {
  44. printf("In axp209_init - Failed to acquire bus access and/or talk to slave.\n");
  45. // ERROR HANDLING; you can check errno to see what went wrong
  46. if (ioctl(fd_axp209, I2C_SLAVE_FORCE, AXP209_I2C_ADDR) < 0) {
  47. printf("In axp209_init - Failed to acquire FORCED bus access and/or talk to slave.\n");
  48. // ERROR HANDLING; you can check errno to see what went wrong
  49. return false;
  50. }
  51. }
  52. // Enable only chosen interrupts (PEK short and long presses)
  53. int err;
  54. /*err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_1_ENABLE, 0x00);
  55. if(err < 0){
  56. printf("ERROR initializing interrupts 1 for AXP209\n");
  57. }
  58. err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_2_ENABLE, 0x00);
  59. if(err < 0){
  60. printf("ERROR initializing interrupts 2 for AXP209\n");
  61. }*/
  62. err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_3_ENABLE, 0x03);
  63. if(err < 0){
  64. printf("ERROR initializing interrupts 3 for AXP209\n");
  65. }
  66. /*err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_4_ENABLE, 0x00);
  67. if(err < 0){
  68. printf("ERROR initializing interrupts 4 for AXP209\n");
  69. }
  70. err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_5_ENABLE, 0x00);
  71. if(err < 0){
  72. printf("ERROR initializing interrupts 5 for AXP209\n");
  73. }*/
  74. return true;
  75. }
  76. bool axp209_deinit(void) {
  77. // Close I2C open interface
  78. close(fd_axp209);
  79. return true;
  80. }
  81. int axp209_read_interrupt_bank_3(void){
  82. int val = i2c_smbus_read_byte_data ( fd_axp209 , AXP209_INTERRUPT_BANK_3_STATUS );
  83. if(val < 0){
  84. return val;
  85. }
  86. // Clear interrupts
  87. int err = i2c_smbus_write_byte_data( fd_axp209 ,AXP209_INTERRUPT_BANK_3_STATUS, 0xFF );
  88. if(err < 0){
  89. return err;
  90. }
  91. DEBUG_PRINTF("READ AXP209_INTERRUPT_BANK_3_STATUS: 0x%02X\n",val);
  92. return 0xFF & val;
  93. }