从设备树获取gpio编号的正确方法

时间:2016-08-18 10:48:16

标签: linux-kernel linux-device-driver device-tree

这是

of_get_named_gpio(child, "gpio-name", 0);

获取GPIO号码的正确方法是什么?相应的设备树如下所示:

gpio-name = <&gpio0 21 0>;

我收到此错误:

  

探测失败,错误-2

错误代码-2代表什么?这是设备树解析错误吗?

1 个答案:

答案 0 :(得分:0)

此代码的快照显示了如何获取/设置特定的GPIO。它是经过测试的代码。

     struct device_node *np = client->dev.of_node;
     int gpio;
     u8 buf[MAX_I2C_DATA_LEN];   
     int ret, tries = 0;

     if (!np)
             return -ENODEV;            

     gpio = of_get_named_gpio(np, "**gpio-name-used-in-dts-file**", 0); 
     if (!gpio_is_valid(gpio))
             return -ENODEV;            

     ret = gpio_request(gpio, "egalax_irq");  
     if (ret < 0) {            
          dev_err(&client->dev,
         "request gpio failed, cannot wake up controller: %d\n",ret);
             return ret;
     }   

     gpio_direction_output(gpio, 1); 
     ret = gpio_get_value(gpio);

     ""ret"" Will have gpio value.