How to use device tree to get a rotary-encoder device

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to use device tree to get a rotary-encoder device

4,967 Views
mopfi
Contributor I

What I want to do:
Create a rotary encoder device in Linux with Yocto Project and device tree (/dev/rotary-encoder). The device should be visible after boot, so that it can be used in user space.

My Hardware:
i.MX8M Mini Evaluation Board (imx8mm evk)
Rotary encoder using two GPIOs

What I tried:
I changed the dts file and added code for the rotary encoder (tmp/work-shared/imx8mmevk/kernel-source/arch/arm64/boot/dts/freescale/fsl-imx8mm-evk.dts):

/ {
    ...

    rotary-encoder {
        compatible = "rotary-encoder";
        status = "okay";
        gpios = <&pca6416 14 GPIO_ACTIVE_HIGH>, <&pca6416 13 GPIO_ACTIVE_HIGH>;
        linux,axis = <0>; /* REL_X */
        rotary-encoder,encoding = "gray";
        rotary-encoder,relative-axis;
    };
};

What is the right way and do I maybe need some device driver in addition to the device tree file? I thought Linux would have one for rotary encoders. The problem is that my knowledge about device trees is limited. I tried to find a solution via google, but couldn't. Maybe someone can help how to integrate this with the evaluation board.

0 Kudos
Reply
3 Replies

4,643 Views
mopfi
Contributor I

I still had no success.

If I use the code from here I get an error loading the module in Linux.

https://www.kernel.org/doc/Documentation/input/devices/rotary-encoder.rst

The function gpiod_add_lookup_table is not found. I guess it's maybe a problem with the kernel version.

Is there really nobody who ever tried using a rotary encoder on the eval board?

0 Kudos
Reply

4,643 Views
jamesbone
NXP TechSupport
NXP TechSupport

Based on the Linux Documentation the implementation of the Device Tree for a Rotary.Encoder should be:

rotary@0 {
compatible = "rotary-encoder";
gpios = <&gpio 19 1>, <&gpio 20 0>; /* GPIO19 is inverted */
linux,axis = <0>; /* REL_X */
rotary-encoder,encoding = "gray";
rotary-encoder,relative-axis;

};

You can find the complete documentation in the following link:

linux/rotary-encoder.txt at master · torvalds/linux · GitHub 

And you need consider how to implemented the driver and you can find the information in the following link:

https://www.kernel.org/doc/Documentation/input/devices/rotary-encoder.rst 

Best Regards

0 Kudos
Reply

4,643 Views
mopfi
Contributor I

Thank you for your answer. Some parts are still not clear to me.

1. Where does the code has to go inside the device tree file? At the moment my code is under the root element. Is that the right place or do i have to put it somewhere else? I already found the code you posted but modified it. I will revert it back to the suggested.

2. I also found the second link you posted. In the part "Board integration" I found this text:

Because the driver uses generic device properties, this can be done either via device tree, ACPI, or using static board files, like in example below:
[Code]

So i concluded it is enough to have a device tree file without a driver.

0 Kudos
Reply