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