#include #include #include #include #include #include #include #include #include "gpioregs.h" extern struct kobject *hwmon_kobj; /* Helper functions for IO access */ static uint8_t hwm_read_reg(uint16_t hwm_base, uint8_t reg) { outb(reg, hwm_base + HWM_INDEX_OFFSET); return inb(hwm_base + HWM_DATA_OFFSET); } static uint8_t hwm_read_reg_retry(uint16_t hwm_base, uint8_t reg) { uint8_t v = hwm_read_reg(hwm_base, reg); if (v == 0xFF) { v = hwm_read_reg(hwm_base, reg); } return v; } static int vin_raw_to_volt(uint8_t raw, int r_top_kohm, int r_bottom_kohm) { int vm = raw * 11; if (r_top_kohm > 0 && r_bottom_kohm > 0) { vm = (vm * (r_top_kohm + r_bottom_kohm)) / r_bottom_kohm; } return vm; } static ssize_t voltage_vbat_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { int vlotage = 0; uint8_t raw; raw = hwm_read_reg_retry(IT8786_HWM_BASE_DEFAULT, 0x28); vlotage = vin_raw_to_volt(raw, 10, 10); return sprintf(buf, "%d\n", vlotage); } static ssize_t voltage_vbat_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { return -EINVAL; } static struct kobj_attribute voltage_vbat = __ATTR(voltage_vbat, 0644, voltage_vbat_show, voltage_vbat_store); static struct attribute *vbat_attrs[] = { &voltage_vbat.attr, NULL, }; static struct attribute_group vbat_attr_group = { .attrs = vbat_attrs, }; int vbat_init(void) { int ret = 0; ret = sysfs_create_group(hwmon_kobj, &vbat_attr_group); return ret; } void vbat_exit(void) { sysfs_remove_group(hwmon_kobj, &vbat_attr_group); }