battery_n810.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  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. #include "battery_n810.h"
  15. #include "fileaccess.h"
  16. #include "log.h"
  17. #include "util.h"
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. static int battery_n810_update(struct battery *b)
  22. {
  23. struct battery_n810 *bn = container_of(b, struct battery_n810, battery);
  24. int err, value;
  25. int value_changed = 0;
  26. err = file_read_int(bn->level_file, &value, 0);
  27. if (err) {
  28. logerr("WARNING: Failed to read battery charge status file\n");
  29. return -1;
  30. }
  31. if (value != bn->charge)
  32. value_changed = 1;
  33. bn->charge = value;
  34. if (value_changed)
  35. battery_notify_state_change(b);
  36. return 0;
  37. }
  38. static int battery_n810_charge_level(struct battery *b)
  39. {
  40. struct battery_n810 *bn = container_of(b, struct battery_n810, battery);
  41. return bn->charge;
  42. }
  43. static void battery_n810_destroy(struct battery *b)
  44. {
  45. struct battery_n810 *bn = container_of(b, struct battery_n810, battery);
  46. file_close(bn->level_file);
  47. free(bn);
  48. }
  49. static struct fileaccess * battery_n810_attr_open(int flags, const char *name)
  50. {
  51. struct fileaccess *fd;
  52. fd = sysfs_file_open(flags, "/devices/platform/n810bm/%s", name);
  53. if (fd)
  54. return fd;
  55. fd = sysfs_file_open(flags, "/devices/platform/tahvo/tahvo-n810bm/%s", name);
  56. if (fd)
  57. return fd;
  58. fd = sysfs_file_open(flags, "/devices/platform/retu/retu-n810bm/%s", name);
  59. if (fd)
  60. return fd;
  61. return NULL;
  62. }
  63. static struct battery * battery_n810_probe(void)
  64. {
  65. struct fileaccess *level_file;
  66. struct battery_n810 *bn;
  67. level_file = battery_n810_attr_open(O_RDONLY, "battery_level");
  68. if (!level_file)
  69. goto error;
  70. bn = zalloc(sizeof(*bn));
  71. if (!bn)
  72. goto err_close;
  73. bn->level_file = level_file;
  74. battery_init(&bn->battery, "n810");
  75. bn->battery.destroy = battery_n810_destroy;
  76. bn->battery.update = battery_n810_update;
  77. bn->battery.charge_level = battery_n810_charge_level;
  78. bn->battery.poll_interval = 10000;
  79. return &bn->battery;
  80. err_close:
  81. file_close(level_file);
  82. error:
  83. return NULL;
  84. }
  85. BATTERY_PROBE(n810, battery_n810_probe);