【问题标题】:How to use gpio from device tree in a i2c_driver?如何在 i2c_driver 中使用设备树中的 gpio?
【发布时间】:2022-10-23 04:39:19
【问题描述】:

我写了一个 i2c 驱动程序。

我想让它使用的 gpio 可以从设备树中配置。

我的设备树条目当前是:

&i2c1 {
    clock-frequency = <100000>;
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_i2c1>;
    status = "okay";
    ...
    myi2c: myi2c@43 {
        compatible = "fsl,myi2c";
        reg = <0x43>;
    };

我想将此行添加到 myi2c 节中:

        myi2c-gpios = <&gpio4 29 GPIO_ACTIVE_HIGH>;

如果我正在编写平台驱动程序,我可以看到如何做到这一点:

https://wiki.st.com/stm32mpu/wiki/How_to_control_a_GPIO_in_kernel_space:

static int gpio_init_probe(struct platform_device *pdev)
{
   int i = 0;

   printk("GPIO example init\n");

   /* "greenled" label is matching the device tree declaration. OUT_LOW is the value at init */
   green = devm_gpiod_get(&pdev->dev, "greenled", GPIOD_OUT_LOW);

但是在我的驱动程序的i2c_probe() 中,我无法访问struct platform_device *:

static int myi2c_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id)

如何从设备树中读取myi2c-gpios 的值并在我的 i2c 驱动程序中使用它?

【问题讨论】:

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


    【解决方案1】:

    我发现这个驱动程序用作示例:

    static int sn65dsi84_probe(struct i2c_client *client, const struct i2c_device_id *id)
    {
        ...
        struct gpio_desc *enable_gpio;
    
        enable_gpio = devm_gpiod_get_optional(&client->dev, "enable", GPIOD_OUT_HIGH);
        if (enable_gpio)
            gpiod_set_value_cansleep(enable_gpio, 1);
    

    它的设备树是:

    &i2c2 {
        clock-frequency = <100000>;
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_i2c2>;
        status = "okay";
    
        dsi_lvds_bridge: sn65dsi84@2c {
            status = "disabled";
            reg = <0x2c>;
            compatible = "ti,sn65dsi84";
            enable-gpios = <&gpio4 28 GPIO_ACTIVE_HIGH>;
    

    所以devm_gpiod_get_optional() 似乎是答案。

    【讨论】:

    • 您在 Q 中谈论 devm_gpiod_get() 和在 A 中谈论 devm_gpiod_get_optional()。我知道您知道它们略有不同。
    猜你喜欢
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    相关资源
    最近更新 更多