Search code examples
linux-kernelgpioi2c

Forcing a kernel module's GPIOs to always be outputs


I'm writing a driver for the pca9570 i2c GPO expander. (It has four output bits, whose values are set by a simple one-byte write to its i2c address.)

I'm currently setting up the GPIO as follows:

struct gpio_chip *gc = &chip->gpio_chip;

gc->set = pca9570_gpio_set_value;
gc->get = pca9570_gpio_get_value;
gc->direction_output = pca9570_gpio_direction_output;
gc->direction_input = pca9570_gpio_direction_input;

gc->can_sleep = true;

gc->base = gpio_start;
gc->ngpio = PCA9570_GPIO_COUNT;
gc->label = chip->client->name;
gc->owner = THIS_MODULE;

gc->dev = &chip->client->dev;

How can this module tell the kernel that it can only output data? i.e. I want the result of user-space root@arm:/sys/class/gpio/gpio508# cat direction to be out, not in as it currently is.

I'm half-thinking that this driver needs to add some client-side gpio calls to set the direction as output on initialisation, and whenever a user tries to set them as inputs. Is this the correct way to do it?

Are there any existing "output only" gpio drivers which I could copy?


Solution

  • According to datasheet http://www.nxp.com/documents/data_sheet/PCA9570.pdf The PCA9570 is a CMOS device that provides 4 bits of General Purpose parallel Output (GPO) expansion in low voltage processor and handheld battery powered mobile applications.

    To satisfy this requirement the driver in Linux kernel must not implement ->get_direction() and ->direction_input() callbacks as stated in the code:

                if (chip->get_direction) {
                 ...
                } else if (!chip->direction_input) {
                        /*
                         * If the chip lacks the .direction_input callback
                         * we logically assume all lines are outputs.
                         */
                        set_bit(FLAG_IS_OUT, &desc->flags);