linux下switch_gpio.c的使用

linux中通过无设备树的方法对 switch_gpio.c的使用如下

diff --git a/drivers/switch/switch_gpio.c b/drivers/switch/switch_gpio.c
old mode 100644
new mode 100755
index 621d62d..1b4436a
--- a/drivers/switch/switch_gpio.c
+++ b/drivers/switch/switch_gpio.c
@@ -24,7 +24,7 @@
 #include <linux/switch.h>
 #include <linux/workqueue.h>
 #include <linux/gpio.h>
-
+#define H2W_SWITCH_GPIO_NUM   54 //gpio num
 struct gpio_switch_data {
        struct switch_dev sdev;
        unsigned gpio;
@@ -43,7 +43,11 @@ static void gpio_switch_work(struct work_struct *work)
                container_of(work, struct gpio_switch_data, work);
 
        state = gpio_get_value(data->gpio);
-       switch_set_state(&data->sdev, state);
+       if(state)
+         switch_set_state(&data->sdev, state);
+    else
+         switch_set_state(&data->sdev, state);
+       printk("------------------state=%d--------------------",state);
 }
 
 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
@@ -112,7 +116,7 @@ static int gpio_switch_probe(struct platform_device *pdev)
        }
 
        ret = request_irq(switch_data->irq, gpio_irq_handler,
-                         IRQF_TRIGGER_LOW, pdev->name, switch_data);
+                         (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), pdev->name, switch_data);
        if (ret < 0)
                goto err_request_irq;
 
@@ -145,6 +149,24 @@ static int gpio_switch_remove(struct platform_device *pdev)
        return 0;
 }
 
+ 
+static struct gpio_switch_platform_data pdata = {
+       .name      = "Linedet",
+       .gpio      = H2W_SWITCH_GPIO_NUM,
+       .name_on   = "1",
+       .name_off  = "0",
+       .state_on  = "1",
+       .state_off = "0",
+};
+ 
+static struct platform_device gpio_switch_device = {
+       .name = "switch-gpio",
+       .dev = {
+               .platform_data = &pdata,
+       },
+};
+ 
+
 static struct platform_driver gpio_switch_driver = {
        .probe          = gpio_switch_probe,
        .remove         = gpio_switch_remove,
@@ -156,12 +178,15 @@ static struct platform_driver gpio_switch_driver = {
 
 static int __init gpio_switch_init(void)
 {
-       return platform_driver_register(&gpio_switch_driver);
+       platform_driver_register(&gpio_switch_driver);
+       platform_device_register(&gpio_switch_device);
+       return 0;
 }
 
 static void __exit gpio_switch_exit(void)
 {
        platform_driver_unregister(&gpio_switch_driver);
+       platform_device_unregister(&gpio_switch_device);
 }
 
 module_init(gpio_switch_init);

完整代码:

/*
 *  drivers/switch/switch_gpio.c
 *
 * Copyright (C) 2008 Google, Inc.
 * Author: Mike Lockwood <lockwood@android.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/switch.h>
#include <linux/workqueue.h>
#include <linux/gpio.h>
#define  HotPlug_GPIO_NUM   44 //gpio num
#define  Name "hdmiin"
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;
};

static void gpio_switch_work(struct work_struct *work)
{
	int state;
	struct gpio_switch_data	*data =
		container_of(work, struct gpio_switch_data, work);

	state = gpio_get_value(data->gpio);
    if(!state)
         switch_set_state(&data->sdev, state);
    else
         switch_set_state(&data->sdev, state);
       printk("------------------state=%d--------------------",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);
	return -1;
}

static int gpio_switch_probe(struct platform_device *pdev)
{
	struct gpio_switch_platform_data *pdata = pdev->dev.platform_data;
	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;
	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);
	if (ret < 0)
		goto err_switch_dev_register;

	ret = gpio_request(switch_data->gpio, pdev->name);
	if (ret < 0)
		goto err_request_gpio;

	ret = gpio_direction_input(switch_data->gpio);
	if (ret < 0)
		goto err_set_gpio_input;

	INIT_WORK(&switch_data->work, gpio_switch_work);

	switch_data->irq = gpio_to_irq(switch_data->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_RISING | IRQF_TRIGGER_FALLING), 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 struct gpio_switch_platform_data pdata = {
       .name      = "hdmiin",
       .gpio      = HotPlug_GPIO_NUM,
       .name_on   = "1",
       .name_off  = "0",
       .state_on  = "1",
       .state_off = "0",
};
 
static struct platform_device gpio_switch_device = {
       .name = Name,
       .dev = {
               .platform_data = &pdata,
       },
};

static int 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;
}

static struct platform_driver gpio_switch_driver = {
	.probe		= gpio_switch_probe,
	.remove		= gpio_switch_remove,
	.driver		= {
		.name	= Name,
		.owner	= THIS_MODULE,
	},
};

static int __init gpio_switch_init(void)
{
    platform_driver_register(&gpio_switch_driver);
    platform_device_register(&gpio_switch_device);
    return 0;
}

static void __exit gpio_switch_exit(void)
{
	platform_driver_unregister(&gpio_switch_driver);
	platform_device_unregister(&gpio_switch_device);
}

module_init(gpio_switch_init);
module_exit(gpio_switch_exit);

MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
MODULE_DESCRIPTION("GPIO Switch driver");
MODULE_LICENSE("GPL");

测试:

# cat /sys/devices/virtual/switch/hdmiin/state
0
# cat /sys/devices/virtual/switch/hdmiin/state
1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那肯定是很多年以后!

你的鼓励就我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值