A small USV for ALIX boards with i2c support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.1KB

  1. /***************************************************************************
  2. * Copyright (C) 05/2009 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <unistd.h>
  20. #include <stdint.h>
  21. #include <fcntl.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include "i2c-dev.h"
  26. #include "configfile.h"
  27. #include "logging.h"
  28. #include "usvdevice.h"
  29. /* eeprom parameters / thresholds & defaults */
  30. #include "../eeprom.h"
  31. enum {
  32. REG_STATUS = 0x00,
  33. REG_CURRENT = 0x10,
  34. REG_UBAT = 0x11,
  35. REG_UIN = 0x12,
  36. REG_UIN_LOSS = 0x20,
  37. REG_UIN_RESTORE = 0x21,
  38. REG_UBAT_FULL = 0x22,
  39. REG_UBAT_LOW = 0x23,
  40. REG_UBAT_CRIT = 0x24,
  41. REG_IBAT_FULL = 0x25,
  42. REG_CRC = 0x26,
  43. };
  44. static const char *i2c_device;
  45. static int i2c_address;
  46. static int _usvdev_open(void)
  47. {
  48. int fd = open(i2c_device, O_RDWR);
  49. if (fd < 0)
  50. return -1;
  51. unsigned long funcs;
  52. if (ioctl(fd, I2C_FUNCS, &funcs)) {
  53. close(fd);
  54. return -1;
  55. }
  56. if ((funcs & I2C_FUNC_SMBUS_WORD_DATA) != I2C_FUNC_SMBUS_WORD_DATA) {
  57. close(fd);
  58. return -1;
  59. }
  60. if (ioctl(fd, I2C_SLAVE, i2c_address) < 0) {
  61. close(fd);
  62. return -1;
  63. }
  64. return fd;
  65. }
  66. static int usvdev_open(void)
  67. {
  68. static int failcnt = 0;
  69. int dev = _usvdev_open();
  70. if (dev < 0) {
  71. if (failcnt++ == 0)
  72. log_print(LOG_WARN, "failed to open %s:0x%02x", i2c_device, i2c_address);
  73. return -1;
  74. }
  75. if (failcnt != 0) {
  76. log_print(LOG_WARN, "opened %s:0x%02x after %d failed attempts",
  77. i2c_device, i2c_address, failcnt);
  78. failcnt = 0;
  79. }
  80. return dev;
  81. }
  82. int usvdev_getstatus(struct usvdev_status *status)
  83. {
  84. int dev = usvdev_open();
  85. if (dev < 0)
  86. return -1;
  87. status->state = i2c_smbus_read_word_data(dev, REG_STATUS);
  88. status->ibat = i2c_smbus_read_word_data(dev, REG_CURRENT);
  89. status->ubat = i2c_smbus_read_word_data(dev, REG_UBAT);
  90. status->uin = i2c_smbus_read_word_data(dev, REG_UIN);
  91. close(dev);
  92. if (status->state < 0 || status->ibat < 0 || status->ubat < 0 || status->uin < 0) {
  93. status->state = -1;
  94. status->ibat = 0;
  95. status->ubat = 0;
  96. status->uin = 0;
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. int usvdev_setstate(int state)
  102. {
  103. int dev = usvdev_open();
  104. if (dev < 0)
  105. return -1;
  106. int retval = i2c_smbus_write_word_data(dev, REG_STATUS, state);
  107. close(dev);
  108. return retval;
  109. }
  110. int usvdev_init(void)
  111. {
  112. i2c_device = config_get_string("global", "i2c-device", "/dev/i2c-0");
  113. i2c_address = config_get_int("global", "i2c-address" , 16);
  114. int dev = usvdev_open();
  115. if (dev < 0) {
  116. log_print(LOG_WARN, "could not check thresholds");
  117. return 0;
  118. }
  119. struct ee_param defaults = DEFAULT_PARAMETERS;
  120. struct ee_param config;
  121. config.uin_loss = config_get_int("thresholds", "uin_loss" , defaults.uin_loss);
  122. config.uin_restore = config_get_int("thresholds", "uin_restore" , defaults.uin_restore);
  123. config.ubat_full = config_get_int("thresholds", "ubat_full" , defaults.ubat_full);
  124. config.ubat_low = config_get_int("thresholds", "ubat_low" , defaults.ubat_low);
  125. config.ubat_critical = config_get_int("thresholds", "ubat_critical" , defaults.ubat_critical);
  126. config.ibat_full = config_get_int("thresholds", "ibat_full" , defaults.ibat_full);
  127. struct ee_param device;
  128. device.uin_loss = i2c_smbus_read_word_data(dev, REG_UIN_LOSS);
  129. device.uin_restore = i2c_smbus_read_word_data(dev, REG_UIN_RESTORE);
  130. device.ubat_full = i2c_smbus_read_word_data(dev, REG_UBAT_FULL);
  131. device.ubat_low = i2c_smbus_read_word_data(dev, REG_UBAT_LOW);
  132. device.ubat_critical = i2c_smbus_read_word_data(dev, REG_UBAT_CRIT);
  133. device.ibat_full = i2c_smbus_read_word_data(dev, REG_IBAT_FULL);
  134. device.crc16 = 0x0000;
  135. int changed = 0;
  136. if (device.uin_loss != config.uin_loss) {
  137. log_print(LOG_INFO, "update UIN_LOSS: %d => %d", device.uin_loss, config.uin_loss);
  138. i2c_smbus_write_word_data(dev, REG_UIN_LOSS, config.uin_loss);
  139. changed = 1;
  140. }
  141. if (device.uin_restore != config.uin_restore) {
  142. log_print(LOG_INFO, "update UIN_RESTORE: %d => %d", device.uin_restore, config.uin_restore);
  143. i2c_smbus_write_word_data(dev, REG_UIN_RESTORE, config.uin_restore);
  144. changed = 1;
  145. }
  146. if (device.ubat_full != config.ubat_full) {
  147. log_print(LOG_INFO, "update UBAT_FULL: %d => %d", device.ubat_full, config.ubat_full);
  148. i2c_smbus_write_word_data(dev, REG_UBAT_FULL, config.ubat_full);
  149. changed = 1;
  150. }
  151. if (device.ubat_low != config.ubat_low) {
  152. log_print(LOG_INFO, "update UBAT_LOW: %d => %d", device.ubat_low, config.ubat_low);
  153. i2c_smbus_write_word_data(dev, REG_UBAT_LOW, config.ubat_low);
  154. changed = 1;
  155. }
  156. if (device.ubat_critical != config.ubat_critical) {
  157. log_print(LOG_INFO, "update UBAT_CRIT: %d => %d", device.ubat_critical, config.ubat_critical);
  158. i2c_smbus_write_word_data(dev, REG_UBAT_CRIT, config.ubat_critical);
  159. changed = 1;
  160. }
  161. if (device.ibat_full != config.ibat_full) {
  162. log_print(LOG_INFO, "update IBAT_FULL: %d => %d", device.ibat_full, config.ibat_full);
  163. i2c_smbus_write_word_data(dev, REG_IBAT_FULL, config.ibat_full);
  164. changed = 1;
  165. }
  166. if (changed == 1) {
  167. i2c_smbus_write_word_data(dev, REG_CRC, 0x0000);
  168. log_print(LOG_INFO, "committing threshold changes");
  169. }
  170. close(dev);
  171. return 0;
  172. }