| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- #include <linux/uaccess.h>
- #include <linux/slab.h>
- #include <linux/pci.h>
- #include <linux/i2c.h>
- #include <linux/acpi.h>
- #include <linux/interrupt.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/mutex.h>
- #include <linux/delay.h>
- #include <linux/sched.h>
- #include <linux/timer.h>
- #include <linux/workqueue.h>
- #include "gpioregs.h"
- // blue is charge
- // white is health
- // 因为我们的LED没有蓝色和白色。只有红色和绿色。所以用红色表示充电,绿色表示健康
- enum battery_led_color
- {
- BATTERY_LED_OFF = 0x00,
- BATTERY_LED_WHITE = 0x01,
- BATTERY_LED_BLUE = 0x02,
- BATTERY_LED_WHITE_BLINK = 0x03,
- BATTERY_LED_BLUE_BLINK = 0x04
- };
- extern struct kobject *vfiec_kobj;
- static struct kobject *batteryled_kobj;
- static unsigned int led_charge_val = 0;
- static unsigned int led_health_val = 0;
- static struct delayed_work delay_work1;
- 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;
- }
- /* ==================== max_fade_brightness ==================== */
- static ssize_t led_health_show(struct kobject *kobj,
- struct kobj_attribute *attr,
- char *buf)
- {
- uint8_t data = 0;
- int ret = 0;
- ret = oem_ec_read_ram(2, 0x31, &data);
- return sprintf(buf, "%02x\n", data);
- }
- static ssize_t led_health_store(struct kobject *kobj,
- struct kobj_attribute *attr,
- const char *buf, size_t count)
- {
- u32 val;
- int ret;
- printk("%s: %s\n", __func__, buf);
- ret = kstrtou32(buf, 10, &val);
- if (ret < 0)
- return ret;
- if(val == 0x0 || val == 0x1 || val == 0x3)
- {
- led_health_val = val;
- }
- else
- {
- return -EINVAL;
- }
- schedule_delayed_work(&delay_work1, msecs_to_jiffies(10));
- return count;
- }
- static struct kobj_attribute led_health =
- __ATTR(led_health, 0644, led_health_show, led_health_store);
- /* ==================== mode ==================== */
- static ssize_t led_charge_show(struct kobject *kobj, struct kobj_attribute *attr,
- char *buf)
- {
- uint8_t data = 0;
- int ret = 0;
- ret = oem_ec_read_ram(2, 0x31, &data);
- return sprintf(buf, "%02x\n", data);
- }
- static ssize_t led_charge_store(struct kobject *kobj, struct kobj_attribute *attr,
- const char *buf, size_t count)
- {
- u32 val;
- int ret;
- printk("%s: %s\n", __func__, buf);
- ret = kstrtou32(buf, 10, &val);
- if (ret < 0)
- return ret;
- if(val == 0x0 || val == 0x2 || val == 0x4)
- {
- led_charge_val = val;
- }
- else
- {
- return -EINVAL;
- }
- schedule_delayed_work(&delay_work1, msecs_to_jiffies(10));
- return count;
- }
- static struct kobj_attribute led_charge =
- __ATTR(led_charge, 0644, led_charge_show, led_charge_store);
- /* ==================== 属性组 ==================== */
- static struct attribute *batteryled_attrs[] = {
- &led_charge.attr,
- &led_health.attr,
- NULL,
- };
- static struct attribute_group batteryled_attr_group = {
- .attrs = batteryled_attrs,
- };
- static void delay_work_func(struct work_struct *work)
- {
- static uint8_t blink_flag = 0;
- uint8_t data = 0;
- if(blink_flag == 0)
- {
- //闪烁的时候灭
- blink_flag = 1;
- if(led_health_val == 0x0)
- {
- data = data & 0xf3;//灭
- }
- else if(led_health_val == 0x1)
- {
- data = data | 0x04;//亮
- }
- else if(led_health_val == 0x3)
- {
- data = data & 0xf3;//灭
- }
-
- if(led_charge_val == 0x0)
- {
- data = data & 0xcf;
- }
- else if(led_charge_val == 0x2)
- {
- data = data | 0x20;
- }
- else if(led_charge_val == 0x4)
- {
- data = data & 0xcf;
- }
- }
- else if(blink_flag == 1)
- {
- // 闪烁的时候亮
- blink_flag = 0;
- if(led_health_val == 0x0)
- {
- data = data & 0xf3;//灭
- }
- else if(led_health_val == 0x1)
- {
- data = data | 0x04;//亮
- }
- else if(led_health_val == 0x3)
- {
- data = data | 0x04;//亮
- }
-
- if(led_charge_val == 0x0)
- {
- data = data & 0xcf;
- }
- else if(led_charge_val == 0x2)
- {
- data = data | 0x20;
- }
- else if(led_charge_val == 0x4)
- {
- data = data | 0x20;
- }
- }
- oem_ec_write_ram(2, 0x31, data);
- oem_ec_write_ram(2, 0x21, 0x3C);
- schedule_delayed_work(&delay_work1, msecs_to_jiffies(500));
- }
- int batteryled_init(void)
- {
- int ret;
- INIT_DELAYED_WORK(&delay_work1, delay_work_func);
- /* 创建 /sys/kernel/vfiec/batteryled */
- batteryled_kobj = kobject_create_and_add("batteryled", vfiec_kobj);
- if (!batteryled_kobj)
- {
- ret = -ENOMEM;
- return ret;
- }
- /* 创建属性文件 */
- ret = sysfs_create_group(batteryled_kobj, &batteryled_attr_group);
- if (ret)
- {
- pr_err("Failed to create sysfs group: %d\n", ret);
- goto free_batteryled_kobj;
- }
- return 0;
- free_batteryled_kobj:
- kobject_put(batteryled_kobj);
- return ret;
- }
- void batteryled_exit(void)
- {
- sysfs_remove_group(batteryled_kobj, &batteryled_attr_group);
- kobject_put(batteryled_kobj);
- }
|