创建玩一个switch类之后,最普通的用法就是把一个GPIO端口,用作一个switch信号,所以switch_gpio.c就这么诞生了,这是我的理解,不一定正确。
1.两个相关的数据结构:
struct gpio_switch_platform_data {
const char *name;
unsigned gpio;
/* if NULL, switch_dev.name will be printed */
const char *name_on;
const char *name_off;
/* if NULL, "0" or "1" will be printed */
const char *state_on;
const char *state_off;
};
struct gpio_switch_data {
struct switch_dev sdev;
unsigned gpio;
const char *name_on;
const char *name_off;
const char *state_on;
const char *state_off;
int irq;
struct work_struct work;
};
2. exit/init:
static struct platform_driver gpio_switch_driver = {
.probe = gpio_switch_probe,
.remove = __devexit_p(gpio_switch_remove),
.driver = {
.name = "switch-gpio",
.owner = THIS_MODULE,
},
};
static int __init gpio_switch_init(void)
{
return platform_driver_register(&gpio_switch_driver); //把gpio_switch注册成一个platform驱动。
}
static void __exit gpio_switch_exit(void)
{
platform_driver_unregister(&gpio_switch_driver);
}
module_init(gpio_switch_init);
module_exit(gpio_switch_exit);
3. probe/remove
static int gpio_switch_probe(struct platform_device *pdev)
{
struct gpio_switch_platform_data *pdata = pdev->dev.platform_data; //pdev包含的设备的platform专有数据,这种platform_data会在boar_i2c_info结构体中声明。
struct gpio_switch_data *switch_data;
int ret = 0;
if (!pdata)
return -EBUSY;
switch_data = kzalloc(sizeof(struct gpio_switch_data), GFP_KERNEL);
if (!switch_data)
return -ENOMEM;
switch_data->sdev.name = pdata->name; //把外面声明的pdata内容,赋给switch_data
switch_data->gpio = pdata->gpio;
switch_data->name_on = pdata->name_on;
switch_data->name_off = pdata->name_off;
switch_data->state_on = pdata->state_on;
switch_data->state_off = pdata->state_off;
switch_data->sdev.print_state = switch_gpio_print_state;
ret = switch_dev_register(&switch_data->sdev); //向switch class中注册一个switch设备。
if (ret < 0)
goto err_switch_dev_register;
ret = gpio_request(switch_data->gpio, pdev->name); //获得gpio
if (ret < 0)
goto err_request_gpio;
ret = gpio_direction_input(switch_data->gpio); //定义gpio输入为输入信号
if (ret < 0)
goto err_set_gpio_input;
INIT_WORK(&switch_data->work, gpio_switch_work); //初始化一个工作队列gpio_switch_work()
switch_data->irq = gpio_to_irq(switch_data->gpio); //获得中断gpio
if (switch_data->irq < 0) {
ret = switch_data->irq;
goto err_detect_irq_num_failed;
}
ret = request_irq(switch_data->irq, gpio_irq_handler,
IRQF_TRIGGER_LOW, pdev->name, switch_data); //注册一个中断,
//中断号为switch_data->irq,
//中断处理程序为gpio_irq_handler(),
//中断类型为IRQF_TRIGGER_LOW
//中断设备名pdev->name
//中断设备地址switch_data
if (ret < 0)
goto err_request_irq;
/* Perform initial detection */
gpio_switch_work(&switch_data->work);
return 0;
err_request_irq:
err_detect_irq_num_failed:
err_set_gpio_input:
gpio_free(switch_data->gpio);
err_request_gpio:
switch_dev_unregister(&switch_data->sdev);
err_switch_dev_register:
kfree(switch_data);
return ret;
}
static int __devexit gpio_switch_remove(struct platform_device *pdev)
{
struct gpio_switch_data *switch_data = platform_get_drvdata(pdev);
cancel_work_sync(&switch_data->work);
gpio_free(switch_data->gpio);
switch_dev_unregister(&switch_data->sdev);
kfree(switch_data);
return 0;
}
4. 一些处理函数:
static void gpio_switch_work(struct work_struct *work)
{
int state;
struct gpio_switch_data *data =
container_of(work, struct gpio_switch_data, work); //通过work,来获得gpio_switch_data的地址
state = gpio_get_value(data->gpio);
switch_set_state(&data->sdev, state); //设置state
}
static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
{
struct gpio_switch_data *switch_data =
(struct gpio_switch_data *)dev_id;
schedule_work(&switch_data->work); //通过 工作队列 来处理
return IRQ_HANDLED;
}
static ssize_t switch_gpio_print_state(struct switch_dev *sdev, char *buf)
{
struct gpio_switch_data *switch_data =
container_of(sdev, struct gpio_switch_data, sdev);
const char *state;
if (switch_get_state(sdev))
state = switch_data->state_on;
else
state = switch_data->state_off;
if (state)
return sprintf(buf, "%s\n", state); //输出state
return -1;
}