| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/kobject.h>
- #include <linux/sysfs.h>
- #include <linux/acpi.h>
- #include <linux/io.h>
- #include <linux/delay.h>
- #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 void hwm_write_reg(uint16_t hwm_base, uint8_t reg, uint8_t val)
- {
- outb(reg, hwm_base + HWM_INDEX_OFFSET);
- outb(val, 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 int ec_raw_to_volt(uint8_t raw, int r_top_kohm, int r_bottom_kohm)
- {
- int vm = raw * 12; /* EC unit: 10mV */
- vm = (vm * (r_top_kohm + r_bottom_kohm)) / r_bottom_kohm;
- return vm ;
- }
- static int ec_wait_ibf(void)
- {
- int i = 0;
- while (inb(EC_CMD_PORT) & EC_IBF) {
- if (++i > TIMEOUT_LOOPS) {
- printk("Error: EC IBF Timeout!\n");
- return -1;
- }
- udelay(1);
- }
- return 0;
- }
- static int ec_wait_obf(void)
- {
- int i = 0;
- while (!(inb(EC_CMD_PORT) & EC_OBF)) {
- if (++i > TIMEOUT_LOOPS) {
- printk("Error: EC OBF Timeout!\n");
- return -1;
- }
- udelay(1);
- }
- return 0;
- }
- static int wait_ibf(void)
- {
- int i = 0;
- while (inb(EC_CMD_PORT) & EC_IBF)
- {
- if (++i > TIMEOUT_LOOPS)
- {
- return -1;
- }
- udelay(1);
- }
- return 0;
- }
- static int wait_obf(void)
- {
- int i = 0;
- while (!(inb(EC_CMD_PORT) & EC_OBF))
- {
- if (++i > TIMEOUT_LOOPS)
- {
- return -1;
- }
- udelay(1);
- }
- return 0;
- }
- static int ec_read_ram(uint8_t offset, uint8_t *data)
- {
- if (wait_ibf() < 0)
- return -1;
- outb(CMD_READ_RAM, EC_CMD_PORT);
- if (wait_ibf() < 0)
- return -1;
- outb(offset, EC_DATA_PORT);
- if (wait_obf() < 0)
- return -1;
- *data = inb(EC_DATA_PORT);
- return 0;
- }
- static int ec_write_ram(uint8_t offset, uint8_t data)
- {
- if (wait_ibf() < 0)
- return -1;
- outb(CMD_WRITE_RAM, EC_CMD_PORT);
- if (wait_ibf() < 0)
- return -1;
- outb(offset, EC_DATA_PORT);
- if (wait_ibf() < 0)
- return -1;
- outb(data, EC_DATA_PORT);
- return 0;
- }
- static int oem_ec_read_ram(uint8_t page, uint8_t offset, uint8_t *data)
- {
- unsigned char WEC, REC;
- switch(page)
- {
- case 0:
- {
- WEC = 0x96;
- REC = 0x95;
- break;
- }
-
- case 1:
- {
- WEC = 0x98;
- REC = 0x97;
- break;
- }
-
- default:
- {
- WEC = 0x81;
- REC = 0x80;
- break;
- }
- }
- if (wait_ibf() < 0)
- return -1;
- outb(REC, EC_CMD_PORT);
- if (wait_ibf() < 0)
- return -1;
- outb(offset, EC_DATA_PORT);
- if (wait_obf() < 0)
- return -1;
- *data = inb(EC_DATA_PORT);
- return 0;
- }
- static int oem_ec_write_ram(uint8_t page, uint8_t offset, uint8_t data)
- {
- unsigned char WEC, REC;
- switch(page)
- {
- case 0:
- {
- WEC = 0x96;
- REC = 0x95;
- break;
- }
-
- case 1:
- {
- WEC = 0x98;
- REC = 0x97;
- break;
- }
-
- default:
- {
- WEC = 0x81;
- REC = 0x80;
- break;
- }
- }
- if (wait_ibf() < 0)
- return -1;
- outb(WEC, EC_CMD_PORT);
- if (wait_ibf() < 0)
- return -1;
- outb(offset, EC_DATA_PORT);
- if (wait_ibf() < 0)
- return -1;
- outb(data, EC_DATA_PORT);
- return 0;
- }
- static ssize_t ac_power_show(struct kobject *kobj, struct kobj_attribute *attr,
- char *buf)
- {
- int ac_power_flag = 0;
- uint8_t val = 0x00;
- if (oem_ec_read_ram(2, 0x36, &val) < 0)
- return -1;
- ac_power_flag = (val & 0x03) ? 1 : 0;
- return sprintf(buf, "%d", ac_power_flag);
- }
- static ssize_t ac_power_store(struct kobject *kobj, struct kobj_attribute *attr,
- const char *buf, size_t count)
- {
- return -EINVAL;
- }
- static struct kobj_attribute ac_power =
- __ATTR(ac_power, 0644, ac_power_show, ac_power_store);
- static struct attribute *sysfs_power_attrs[] = {
- &ac_power.attr,
- NULL,
- };
- static struct attribute_group sysfs_power_attr_group = {
- .attrs = sysfs_power_attrs,
- };
- int sysfs_power_init(void)
- {
- int ret = 0;
- ret = sysfs_create_group(hwmon_kobj, &sysfs_power_attr_group);
- return ret;
- }
- void sysfs_power_exit(void)
- {
- sysfs_remove_group(hwmon_kobj, &sysfs_power_attr_group);
- }
|