lundi 18 avril 2011

Adding a PCA9555 GPIO extender on a Linux based machine

Today I'm adding a PCA9555 http://www.nxp.com/documents/data_sheet/PCA9555.pdf to my Blackfin based board.

The PCA9555 is a 16 GPIO extender on I2C bus.

The new linux GPIO lib is very smart and easy to extend. What you need to do is just wire the chip on the I2C bus (sometimes called TWI), and add the configuration in your machine code.

To be added to your arch/xxx/mach-xxxx/mach-mymachine.c :

to includes :
#include

The platform data of your chip :

#if defined(CONFIG_I2C) || defined(CONFIG_GPIO_PCA953X)
/* PCA9555 */
static struct pca953x_platform_data stamp_gpio_ext_pdata = {
        .gpio_base = -1, // auto-guess the GPIO number
};
Of course you need to add one for each PCA9555 you want to add on the bus.

Now add the device to your I2C bus declaration.
You probably have something like :

static struct i2c_board_info __initdata bfin_i2c_board_info[] = {
 { ... }, { ... }

Just add to the array your chip :
{
  I2C_BOARD_INFO("pca9555",0x20),
  .platform_data = &stamp_gpio_ext_pdata,
 },
0x20 is the I2C address of the chip, you can guess it reading the datasheet or using the "i2cdetect" utility. For the PCA9555 the address is configured by the pin A0,A1,A2, with all threee are connected to zero volt, then the address is 0x20.

Check the PCA support in the kernel config menu (device->gpio), compile, run it and it's done !
Be carefull the new GPIO will be added at some very high number (240 in my case), you can find it by looking in /sys/class/gpio/ the new GPIO extender should be there.