vbat.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/kobject.h>
  5. #include <linux/sysfs.h>
  6. #include <linux/acpi.h>
  7. #include <linux/io.h>
  8. #include <linux/delay.h>
  9. #include "gpioregs.h"
  10. extern struct kobject *hwmon_kobj;
  11. /* Helper functions for IO access */
  12. static uint8_t hwm_read_reg(uint16_t hwm_base, uint8_t reg)
  13. {
  14. outb(reg, hwm_base + HWM_INDEX_OFFSET);
  15. return inb(hwm_base + HWM_DATA_OFFSET);
  16. }
  17. static uint8_t hwm_read_reg_retry(uint16_t hwm_base, uint8_t reg)
  18. {
  19. uint8_t v = hwm_read_reg(hwm_base, reg);
  20. if (v == 0xFF)
  21. {
  22. v = hwm_read_reg(hwm_base, reg);
  23. }
  24. return v;
  25. }
  26. static int vin_raw_to_volt(uint8_t raw, int r_top_kohm, int r_bottom_kohm)
  27. {
  28. int vm = raw * 11;
  29. if (r_top_kohm > 0 && r_bottom_kohm > 0)
  30. {
  31. vm = (vm * (r_top_kohm + r_bottom_kohm)) / r_bottom_kohm;
  32. }
  33. return vm;
  34. }
  35. static ssize_t voltage_vbat_show(struct kobject *kobj, struct kobj_attribute *attr,
  36. char *buf)
  37. {
  38. int vlotage = 0;
  39. uint8_t raw;
  40. raw = hwm_read_reg_retry(IT8786_HWM_BASE_DEFAULT, 0x28);
  41. vlotage = vin_raw_to_volt(raw, 10, 10);
  42. return sprintf(buf, "%d\n", vlotage);
  43. }
  44. static ssize_t voltage_vbat_store(struct kobject *kobj, struct kobj_attribute *attr,
  45. const char *buf, size_t count)
  46. {
  47. return -EINVAL;
  48. }
  49. static struct kobj_attribute voltage_vbat =
  50. __ATTR(voltage_vbat, 0644, voltage_vbat_show, voltage_vbat_store);
  51. static struct attribute *vbat_attrs[] = {
  52. &voltage_vbat.attr,
  53. NULL,
  54. };
  55. static struct attribute_group vbat_attr_group = {
  56. .attrs = vbat_attrs,
  57. };
  58. int vbat_init(void)
  59. {
  60. int ret = 0;
  61. ret = sysfs_create_group(hwmon_kobj, &vbat_attr_group);
  62. return ret;
  63. }
  64. void vbat_exit(void)
  65. {
  66. sysfs_remove_group(hwmon_kobj, &vbat_attr_group);
  67. }