Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: handle pca953{4,5,6,7,8} too

This third part of an extension to support more pca953x chips updates the
logic to handle the smaller register widths used by the 4-bit and 8-bit parts,
and to use the chip type to determine how many GPIOs it provides.

As long as we don't support interrupt and reset capabilities, those size
issues are the only software-visible differences between these parts.

Signed-off-by: Guennadi Liakhovetski <g.liakhovetski@pengutronix.de>
Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Guennadi Liakhovetski and committed by
Linus Torvalds
f5e8ff48 f3dc3630

+54 -17
+54 -17
drivers/gpio/pca953x.c
··· 1 1 /* 2 - * pca953x.c - 16-bit I/O port with interrupt and reset 2 + * pca953x.c - 4/8/16 bit I/O ports 3 3 * 4 4 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com> 5 5 * Copyright (C) 2007 Marvell International Ltd. ··· 18 18 19 19 #include <asm/gpio.h> 20 20 21 + #define PCA953X_INPUT 0 22 + #define PCA953X_OUTPUT 1 23 + #define PCA953X_INVERT 2 24 + #define PCA953X_DIRECTION 3 21 25 22 - #define NR_PCA953X_GPIOS 16 26 + /* This is temporary - in 2.6.26 i2c_driver_data should replace it. */ 27 + struct pca953x_desc { 28 + char name[I2C_NAME_SIZE]; 29 + unsigned long driver_data; 30 + }; 23 31 24 - #define PCA953X_INPUT 0 25 - #define PCA953X_OUTPUT 2 26 - #define PCA953X_INVERT 4 27 - #define PCA953X_DIRECTION 6 32 + static const struct pca953x_desc pca953x_descs[] = { 33 + { "pca9534", 8, }, 34 + { "pca9535", 16, }, 35 + { "pca9536", 4, }, 36 + { "pca9537", 4, }, 37 + { "pca9538", 8, }, 38 + { "pca9539", 16, }, 39 + /* REVISIT several pca955x parts should work here too */ 40 + }; 28 41 29 42 struct pca953x_chip { 30 43 unsigned gpio_start; ··· 53 40 */ 54 41 static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val) 55 42 { 56 - if (i2c_smbus_write_word_data(chip->client, reg, val) < 0) 57 - return -EIO; 43 + int ret; 44 + 45 + if (chip->gpio_chip.ngpio <= 8) 46 + ret = i2c_smbus_write_byte_data(chip->client, reg, val); 58 47 else 59 - return 0; 48 + ret = i2c_smbus_write_word_data(chip->client, reg << 1, val); 49 + 50 + if (ret < 0) { 51 + dev_err(&chip->client->dev, "failed writing register\n"); 52 + return -EIO; 53 + } 54 + 55 + return 0; 60 56 } 61 57 62 58 static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val) 63 59 { 64 60 int ret; 65 61 66 - ret = i2c_smbus_read_word_data(chip->client, reg); 62 + if (chip->gpio_chip.ngpio <= 8) 63 + ret = i2c_smbus_read_byte_data(chip->client, reg); 64 + else 65 + ret = i2c_smbus_read_word_data(chip->client, reg << 1); 66 + 67 67 if (ret < 0) { 68 68 dev_err(&chip->client->dev, "failed reading register\n"); 69 69 return -EIO; ··· 174 148 chip->reg_output = reg_val; 175 149 } 176 150 177 - static int pca953x_init_gpio(struct pca953x_chip *chip) 151 + static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) 178 152 { 179 153 struct gpio_chip *gc; 180 154 ··· 186 160 gc->set = pca953x_gpio_set_value; 187 161 188 162 gc->base = chip->gpio_start; 189 - gc->ngpio = NR_PCA953X_GPIOS; 190 - gc->label = "pca953x"; 191 - 192 - return gpiochip_add(gc); 163 + gc->ngpio = gpios; 164 + gc->label = chip->client->name; 193 165 } 194 166 195 167 static int __devinit pca953x_probe(struct i2c_client *client) 196 168 { 197 169 struct pca953x_platform_data *pdata; 198 170 struct pca953x_chip *chip; 199 - int ret; 171 + int ret, i; 172 + const struct pca953x_desc *id = NULL; 200 173 201 174 pdata = client->dev.platform_data; 202 175 if (pdata == NULL) 176 + return -ENODEV; 177 + 178 + /* this loop vanishes when we get i2c_device_id */ 179 + for (i = 0; i < ARRAY_SIZE(pca953x_descs); i++) 180 + if (!strcmp(pca953x_descs[i].name, client->name)) { 181 + id = pca953x_descs + i; 182 + break; 183 + } 184 + if (!id) 203 185 return -ENODEV; 204 186 205 187 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL); ··· 221 187 /* initialize cached registers from their original values. 222 188 * we can't share this chip with another i2c master. 223 189 */ 190 + pca953x_setup_gpio(chip, id->driver_data); 191 + 224 192 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output); 225 193 if (ret) 226 194 goto out_failed; ··· 236 200 if (ret) 237 201 goto out_failed; 238 202 239 - ret = pca953x_init_gpio(chip); 203 + 204 + ret = gpiochip_add(&chip->gpio_chip); 240 205 if (ret) 241 206 goto out_failed; 242 207