eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1 | /* |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 2 | * pca953x.c - 4/8/16 bit I/O ports |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com> |
| 5 | * Copyright (C) 2007 Marvell International Ltd. |
| 6 | * |
| 7 | * Derived from drivers/i2c/chips/pca9539.c |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/i2c.h> |
Guennadi Liakhovetski | d1c057e3 | 2008-02-06 01:39:02 -0800 | [diff] [blame] | 17 | #include <linux/i2c/pca953x.h> |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame^] | 18 | #ifdef CONFIG_OF_GPIO |
| 19 | #include <linux/of_platform.h> |
| 20 | #include <linux/of_gpio.h> |
| 21 | #endif |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 22 | |
| 23 | #include <asm/gpio.h> |
| 24 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 25 | #define PCA953X_INPUT 0 |
| 26 | #define PCA953X_OUTPUT 1 |
| 27 | #define PCA953X_INVERT 2 |
| 28 | #define PCA953X_DIRECTION 3 |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 29 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 30 | static const struct i2c_device_id pca953x_id[] = { |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 31 | { "pca9534", 8, }, |
| 32 | { "pca9535", 16, }, |
| 33 | { "pca9536", 4, }, |
| 34 | { "pca9537", 4, }, |
| 35 | { "pca9538", 8, }, |
| 36 | { "pca9539", 16, }, |
David Brownell | 69292b3 | 2008-05-23 13:04:42 -0700 | [diff] [blame] | 37 | { "pca9554", 8, }, |
Will Newton | f39e578 | 2008-05-01 04:35:10 -0700 | [diff] [blame] | 38 | { "pca9555", 16, }, |
| 39 | { "pca9557", 8, }, |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 40 | |
David Brownell | 7059d4b | 2008-07-04 09:59:37 -0700 | [diff] [blame] | 41 | { "max7310", 8, }, |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 42 | { "pca6107", 8, }, |
| 43 | { "tca6408", 8, }, |
| 44 | { "tca6416", 16, }, |
| 45 | /* NYET: { "tca6424", 24, }, */ |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 46 | { } |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 47 | }; |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 48 | MODULE_DEVICE_TABLE(i2c, pca953x_id); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 49 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 50 | struct pca953x_chip { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 51 | unsigned gpio_start; |
| 52 | uint16_t reg_output; |
| 53 | uint16_t reg_direction; |
| 54 | |
| 55 | struct i2c_client *client; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame^] | 56 | struct pca953x_platform_data *dyn_pdata; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 57 | struct gpio_chip gpio_chip; |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 58 | char **names; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 61 | static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 62 | { |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 63 | int ret; |
| 64 | |
| 65 | if (chip->gpio_chip.ngpio <= 8) |
| 66 | ret = i2c_smbus_write_byte_data(chip->client, reg, val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 67 | else |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 68 | ret = i2c_smbus_write_word_data(chip->client, reg << 1, val); |
| 69 | |
| 70 | if (ret < 0) { |
| 71 | dev_err(&chip->client->dev, "failed writing register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 72 | return ret; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | return 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 78 | static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 79 | { |
| 80 | int ret; |
| 81 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 82 | if (chip->gpio_chip.ngpio <= 8) |
| 83 | ret = i2c_smbus_read_byte_data(chip->client, reg); |
| 84 | else |
| 85 | ret = i2c_smbus_read_word_data(chip->client, reg << 1); |
| 86 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 87 | if (ret < 0) { |
| 88 | dev_err(&chip->client->dev, "failed reading register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 89 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | *val = (uint16_t)ret; |
| 93 | return 0; |
| 94 | } |
| 95 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 96 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 97 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 98 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 99 | uint16_t reg_val; |
| 100 | int ret; |
| 101 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 102 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 103 | |
| 104 | reg_val = chip->reg_direction | (1u << off); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 105 | ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 106 | if (ret) |
| 107 | return ret; |
| 108 | |
| 109 | chip->reg_direction = reg_val; |
| 110 | return 0; |
| 111 | } |
| 112 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 113 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 114 | unsigned off, int val) |
| 115 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 116 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 117 | uint16_t reg_val; |
| 118 | int ret; |
| 119 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 120 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 121 | |
| 122 | /* set output level */ |
| 123 | if (val) |
| 124 | reg_val = chip->reg_output | (1u << off); |
| 125 | else |
| 126 | reg_val = chip->reg_output & ~(1u << off); |
| 127 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 128 | ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 129 | if (ret) |
| 130 | return ret; |
| 131 | |
| 132 | chip->reg_output = reg_val; |
| 133 | |
| 134 | /* then direction */ |
| 135 | reg_val = chip->reg_direction & ~(1u << off); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 136 | ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 137 | if (ret) |
| 138 | return ret; |
| 139 | |
| 140 | chip->reg_direction = reg_val; |
| 141 | return 0; |
| 142 | } |
| 143 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 144 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 145 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 146 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 147 | uint16_t reg_val; |
| 148 | int ret; |
| 149 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 150 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 151 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 152 | ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 153 | if (ret < 0) { |
| 154 | /* NOTE: diagnostic already emitted; that's all we should |
| 155 | * do unless gpio_*_value_cansleep() calls become different |
| 156 | * from their nonsleeping siblings (and report faults). |
| 157 | */ |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | return (reg_val & (1u << off)) ? 1 : 0; |
| 162 | } |
| 163 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 164 | static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 165 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 166 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 167 | uint16_t reg_val; |
| 168 | int ret; |
| 169 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 170 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 171 | |
| 172 | if (val) |
| 173 | reg_val = chip->reg_output | (1u << off); |
| 174 | else |
| 175 | reg_val = chip->reg_output & ~(1u << off); |
| 176 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 177 | ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 178 | if (ret) |
| 179 | return; |
| 180 | |
| 181 | chip->reg_output = reg_val; |
| 182 | } |
| 183 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 184 | static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 185 | { |
| 186 | struct gpio_chip *gc; |
| 187 | |
| 188 | gc = &chip->gpio_chip; |
| 189 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 190 | gc->direction_input = pca953x_gpio_direction_input; |
| 191 | gc->direction_output = pca953x_gpio_direction_output; |
| 192 | gc->get = pca953x_gpio_get_value; |
| 193 | gc->set = pca953x_gpio_set_value; |
Arnaud Patard | 8420780 | 2008-03-10 11:43:48 -0700 | [diff] [blame] | 194 | gc->can_sleep = 1; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 195 | |
| 196 | gc->base = chip->gpio_start; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 197 | gc->ngpio = gpios; |
| 198 | gc->label = chip->client->name; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 199 | gc->dev = &chip->client->dev; |
Guennadi Liakhovetski | d72cbed | 2008-04-28 02:14:45 -0700 | [diff] [blame] | 200 | gc->owner = THIS_MODULE; |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 201 | gc->names = chip->names; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame^] | 204 | /* |
| 205 | * Handlers for alternative sources of platform_data |
| 206 | */ |
| 207 | #ifdef CONFIG_OF_GPIO |
| 208 | /* |
| 209 | * Translate OpenFirmware node properties into platform_data |
| 210 | */ |
| 211 | static struct pca953x_platform_data * |
| 212 | pca953x_get_alt_pdata(struct i2c_client *client) |
| 213 | { |
| 214 | struct pca953x_platform_data *pdata; |
| 215 | struct device_node *node; |
| 216 | const uint16_t *val; |
| 217 | |
| 218 | node = dev_archdata_get_node(&client->dev.archdata); |
| 219 | if (node == NULL) |
| 220 | return NULL; |
| 221 | |
| 222 | pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL); |
| 223 | if (pdata == NULL) { |
| 224 | dev_err(&client->dev, "Unable to allocate platform_data\n"); |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | pdata->gpio_base = -1; |
| 229 | val = of_get_property(node, "linux,gpio-base", NULL); |
| 230 | if (val) { |
| 231 | if (*val < 0) |
| 232 | dev_warn(&client->dev, |
| 233 | "invalid gpio-base in device tree\n"); |
| 234 | else |
| 235 | pdata->gpio_base = *val; |
| 236 | } |
| 237 | |
| 238 | val = of_get_property(node, "polarity", NULL); |
| 239 | if (val) |
| 240 | pdata->invert = *val; |
| 241 | |
| 242 | return pdata; |
| 243 | } |
| 244 | #else |
| 245 | static struct pca953x_platform_data * |
| 246 | pca953x_get_alt_pdata(struct i2c_client *client) |
| 247 | { |
| 248 | return NULL; |
| 249 | } |
| 250 | #endif |
| 251 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 252 | static int __devinit pca953x_probe(struct i2c_client *client, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 253 | const struct i2c_device_id *id) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 254 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 255 | struct pca953x_platform_data *pdata; |
| 256 | struct pca953x_chip *chip; |
Will Newton | f39e578 | 2008-05-01 04:35:10 -0700 | [diff] [blame] | 257 | int ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 258 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 259 | chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 260 | if (chip == NULL) |
| 261 | return -ENOMEM; |
| 262 | |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame^] | 263 | pdata = client->dev.platform_data; |
| 264 | if (pdata == NULL) { |
| 265 | pdata = pca953x_get_alt_pdata(client); |
| 266 | /* |
| 267 | * Unlike normal platform_data, this is allocated |
| 268 | * dynamically and must be freed in the driver |
| 269 | */ |
| 270 | chip->dyn_pdata = pdata; |
| 271 | } |
| 272 | |
| 273 | if (pdata == NULL) { |
| 274 | dev_dbg(&client->dev, "no platform data\n"); |
| 275 | ret = -EINVAL; |
| 276 | goto out_failed; |
| 277 | } |
| 278 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 279 | chip->client = client; |
| 280 | |
| 281 | chip->gpio_start = pdata->gpio_base; |
| 282 | |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 283 | chip->names = pdata->names; |
| 284 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 285 | /* initialize cached registers from their original values. |
| 286 | * we can't share this chip with another i2c master. |
| 287 | */ |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 288 | pca953x_setup_gpio(chip, id->driver_data); |
| 289 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 290 | ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 291 | if (ret) |
| 292 | goto out_failed; |
| 293 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 294 | ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 295 | if (ret) |
| 296 | goto out_failed; |
| 297 | |
| 298 | /* set platform specific polarity inversion */ |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 299 | ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 300 | if (ret) |
| 301 | goto out_failed; |
| 302 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 303 | |
| 304 | ret = gpiochip_add(&chip->gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 305 | if (ret) |
| 306 | goto out_failed; |
| 307 | |
| 308 | if (pdata->setup) { |
| 309 | ret = pdata->setup(client, chip->gpio_chip.base, |
| 310 | chip->gpio_chip.ngpio, pdata->context); |
| 311 | if (ret < 0) |
| 312 | dev_warn(&client->dev, "setup failed, %d\n", ret); |
| 313 | } |
| 314 | |
| 315 | i2c_set_clientdata(client, chip); |
| 316 | return 0; |
| 317 | |
| 318 | out_failed: |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame^] | 319 | kfree(chip->dyn_pdata); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 320 | kfree(chip); |
| 321 | return ret; |
| 322 | } |
| 323 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 324 | static int pca953x_remove(struct i2c_client *client) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 325 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 326 | struct pca953x_platform_data *pdata = client->dev.platform_data; |
| 327 | struct pca953x_chip *chip = i2c_get_clientdata(client); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 328 | int ret = 0; |
| 329 | |
| 330 | if (pdata->teardown) { |
| 331 | ret = pdata->teardown(client, chip->gpio_chip.base, |
| 332 | chip->gpio_chip.ngpio, pdata->context); |
| 333 | if (ret < 0) { |
| 334 | dev_err(&client->dev, "%s failed, %d\n", |
| 335 | "teardown", ret); |
| 336 | return ret; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | ret = gpiochip_remove(&chip->gpio_chip); |
| 341 | if (ret) { |
| 342 | dev_err(&client->dev, "%s failed, %d\n", |
| 343 | "gpiochip_remove()", ret); |
| 344 | return ret; |
| 345 | } |
| 346 | |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame^] | 347 | kfree(chip->dyn_pdata); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 348 | kfree(chip); |
| 349 | return 0; |
| 350 | } |
| 351 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 352 | static struct i2c_driver pca953x_driver = { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 353 | .driver = { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 354 | .name = "pca953x", |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 355 | }, |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 356 | .probe = pca953x_probe, |
| 357 | .remove = pca953x_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 358 | .id_table = pca953x_id, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 359 | }; |
| 360 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 361 | static int __init pca953x_init(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 362 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 363 | return i2c_add_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 364 | } |
David Brownell | 2f8d119 | 2008-10-15 22:03:13 -0700 | [diff] [blame] | 365 | /* register after i2c postcore initcall and before |
| 366 | * subsys initcalls that may rely on these GPIOs |
| 367 | */ |
| 368 | subsys_initcall(pca953x_init); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 369 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 370 | static void __exit pca953x_exit(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 371 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 372 | i2c_del_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 373 | } |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 374 | module_exit(pca953x_exit); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 375 | |
| 376 | MODULE_AUTHOR("eric miao <eric.miao@marvell.com>"); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 377 | MODULE_DESCRIPTION("GPIO expander driver for PCA953x"); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 378 | MODULE_LICENSE("GPL"); |