iriver Dicple D88

如何取得電源按鍵的狀態


pwrkey.c

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/major.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/spinlock.h>
#include <linux/pm.h>
#include <linux/pm_legacy.h>
#include <linux/cdev.h>
#include <linux/init.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/jzsoc.h>
 
#define NAME           "pwrkey"
#define GPIO_PW_I      GPIO_WAKEUP
#define POWEROFF_PIN   GPIO_PW_I

static struct class *myclass = NULL;
static struct device *mydev = NULL;

static unsigned int read_gpio_pin(void)
{
    unsigned int try_loop = 1000;

    unsigned int t, v;
    unsigned int i;
    
    i = try_loop;

    v = t = 0;

    while (i--) {
        t = __gpio_get_pin(POWEROFF_PIN);
        if (v != t)
            i = try_loop;
        
        v = t;
    }
    
    return v;
}

static ssize_t pwrkey_show(struct device *dev, struct device_attribute *attr, char *buf)
{
    return sprintf(buf, "%d\n", read_gpio_pin());
}
static DEVICE_ATTR(pwrkey, 664, pwrkey_show, NULL);

int ldd_init(void)
{
    int r = 0;

    myclass = class_create(THIS_MODULE, NAME);
    mydev = device_create(myclass, NULL, MKDEV(168, 0), "%s", NAME);
    r = device_create_file(mydev, &dev_attr_pwrkey);
    return 0;
}
  
void ldd_exit(void)
{
    device_remove_file(mydev, &dev_attr_pwrkey);
    device_destroy(myclass, MKDEV(168, 0));
    class_destroy(myclass);
}
  
module_init(ldd_init);
module_exit(ldd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Steward Fu");
MODULE_DESCRIPTION("Powerkey driver for d88 handheld");  

P.S. /sys/class/pwrkey/pwrkey/pwrkey


返回上一頁