android9.0(内核版本kernel-4.9)内核GPIO驱动实现-----高通平台

针对kernel-4.9(android9.0)内核GPIO驱动变化作一下记录:
之前的内核版本可以直接在源码中定义管脚后,直接使用内核GPIO申请和控制接口就可以操作,但是到android这样操作直接的结果就是提示GPIO申请失败,所以必须要修改才能实现。
kernel-4.9之前使用:
#define MC2_GPIO_TOMCU_PIN6 6
gpio_request(MC2_GPIO_TOMCU_PIN6 , “GPIO6”);
gpio_direction_output(MC2_GPIO_TOMCU_PIN6, 0);
gpio_set_value(MC2_GPIO_TOMCU_PIN6,1)
即可操作。
kernel-4.9以及之后的不能使用以上方式,管脚定义必须放到设备树对应的驱动组件中,驱动中在probe()函数中通过 gpio_ldo_pin = of_get_named_gpio(pdev->dev.of_node, “qcom,gpio_ldo_pin”, 0);方式来取得对应的GPIO,仍后再调用GPIO请求函数去申请,请参考高通平台的实现方式:
高通平台参考以下驱动:
1、dts文件

   gpio_ldo_power {
        compatible = "xcz,gpio_ldo_power";
        qcom,gpio_ldo_pin = <&msm_gpio 22 0>; //GPIO22
    };

2、驱动

#include <linux/types.h>
#include <linux/pm.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/fsl_devices.h>
#include <asm/setup.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/stat.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/spinlock.h>
#include <linux/err.h>
#include <linux/regulator/consumer.h>

int gpio_ldo_pin = -1;
int gpio_flag = -1;
static struct class *gpio_ldo_power_class = NULL;
static struct device *gpio_ldo_power_dev = NULL;

#define CTL_POWER_ON    "1"
#define CTL_POWER_OFF   "0"

static ssize_t gpio_22_show(struct device *dev,
        struct device_attribute *attr, char *buf)
{
    printk("%s\n", __func__);
    sprintf(buf, "gpio_22 is %d\n", gpio_flag);
    return strlen(buf);
}

static ssize_t gpio_22_store(struct device *dev,
        struct device_attribute *attr, const char *buf,
        size_t count)
{
    if(!strncmp(buf, CTL_POWER_ON, strlen(CTL_POWER_ON))) {
        printk("%s: to enable gpio_22\n", __func__);
        gpio_set_value(gpio_ldo_pin, 1);
        gpio_flag = 1;

    } else if(!strncmp(buf, CTL_POWER_OFF, strlen(CTL_POWER_OFF))) {
        printk("%s: to disable gpio_22\n", __func__);
        gpio_set_value(gpio_ldo_pin, 0);
        gpio_flag = 0;
    }

    return count;
}

static struct device_attribute gpio_22_dev_attr = {
    .attr = {
        .name = "gpio_22",
        .mode = S_IRWXU|S_IRWXG|S_IRWXO,
    },
    .show = gpio_22_show,
    .store = gpio_22_store,
};



static int gpio_ldo_power_probe(struct platform_device *pdev)
{
    int ret = 0;

    printk("xcz enter gpio_ldo_power_probe \n");

    gpio_ldo_pin = of_get_named_gpio(pdev->dev.of_node, "qcom,gpio_ldo_pin", 0);
    if (gpio_ldo_pin < 0)
        printk("xcz gpio_ldo_pin is not available \n");

    ret = gpio_request(gpio_ldo_pin, "gpio_ldo_pin");
    if(0 != ret) {
        printk("xcz gpio request %d failed.", gpio_ldo_pin);
        goto fail1;
    }
    
    gpio_direction_output(gpio_ldo_pin, 0);

    gpio_set_value(gpio_ldo_pin, 0);
    gpio_flag = 0;

    gpio_ldo_power_class = class_create(THIS_MODULE, "gpio_ldo_power");
    if(IS_ERR(gpio_ldo_power_class))
    {
        ret = PTR_ERR(gpio_ldo_power_class);
        printk("Failed to create class.\n");
        return ret;
    }

    gpio_ldo_power_dev = device_create(gpio_ldo_power_class, NULL, 0, NULL, "gpio_gpio_22");
    if (IS_ERR(gpio_ldo_power_dev))
    {
        ret = PTR_ERR(gpio_ldo_power_class);
        printk("Failed to create device(gpio_ldo_power_dev)!\n");
        return ret;
    }

    ret = device_create_file(gpio_ldo_power_dev, &gpio_22_dev_attr);
    if(ret)
    {
        pr_err("%s: gpio_22 creat sysfs failed\n",__func__);
        return ret;
    }

    printk("xcz enter gpio_ldo_power_probe, ok \n");

fail1:
    return ret;
}

static int gpio_ldo_power_remove(struct platform_device *pdev)
{
    device_destroy(gpio_ldo_power_class, 0);
    class_destroy(gpio_ldo_power_class);
    device_remove_file(gpio_ldo_power_dev, &gpio_22_dev_attr);

    return 0;
}

static int gpio_ldo_power_suspend(struct platform_device *pdev,pm_message_t state)
{
    return 0;
}

static int gpio_ldo_power_resume(struct platform_device *pdev)
{
    return 0;
}

static struct of_device_id gpio_ldo_power_dt_match[] = {
    { .compatible = "xcz,gpio_ldo_power",},
    { },
};
MODULE_DEVICE_TABLE(of, gpio_ldo_power_dt_match);

static struct platform_driver gpio_power_driver = {
    .driver = {
        .name = "gpio_ldo_power",
        .owner = THIS_MODULE,
        .of_match_table = of_match_ptr(gpio_ldo_power_dt_match),
    },
    .probe = gpio_ldo_power_probe,
    .remove = gpio_ldo_power_remove,
    .suspend = gpio_ldo_power_suspend,
    .resume = gpio_ldo_power_resume,
};

static __init int gpio_power_init(void)
{
    return platform_driver_register(&gpio_power_driver);
}

static void __exit gpio_power_exit(void)
{
    platform_driver_unregister(&gpio_power_driver);
}

module_init(gpio_power_init);
module_exit(gpio_power_exit);
MODULE_AUTHOR("GPIO_LDO_POWER, Inc.");
MODULE_DESCRIPTION("XCZ GPIO_LDO_POWER");
MODULE_LICENSE("GPL");
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值