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> |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 18 | |
| 19 | #include <asm/gpio.h> |
| 20 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 21 | #define PCA953X_INPUT 0 |
| 22 | #define PCA953X_OUTPUT 1 |
| 23 | #define PCA953X_INVERT 2 |
| 24 | #define PCA953X_DIRECTION 3 |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 25 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 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 | }; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 31 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 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 | }; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 41 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 42 | struct pca953x_chip { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 43 | unsigned gpio_start; |
| 44 | uint16_t reg_output; |
| 45 | uint16_t reg_direction; |
| 46 | |
| 47 | struct i2c_client *client; |
| 48 | struct gpio_chip gpio_chip; |
| 49 | }; |
| 50 | |
| 51 | /* NOTE: we can't currently rely on fault codes to come from SMBus |
| 52 | * calls, so we map all errors to EIO here and return zero otherwise. |
| 53 | */ |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 54 | 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] | 55 | { |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 56 | int ret; |
| 57 | |
| 58 | if (chip->gpio_chip.ngpio <= 8) |
| 59 | ret = i2c_smbus_write_byte_data(chip->client, reg, val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 60 | else |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 61 | ret = i2c_smbus_write_word_data(chip->client, reg << 1, val); |
| 62 | |
| 63 | if (ret < 0) { |
| 64 | dev_err(&chip->client->dev, "failed writing register\n"); |
| 65 | return -EIO; |
| 66 | } |
| 67 | |
| 68 | return 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 71 | 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] | 72 | { |
| 73 | int ret; |
| 74 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 75 | if (chip->gpio_chip.ngpio <= 8) |
| 76 | ret = i2c_smbus_read_byte_data(chip->client, reg); |
| 77 | else |
| 78 | ret = i2c_smbus_read_word_data(chip->client, reg << 1); |
| 79 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 80 | if (ret < 0) { |
| 81 | dev_err(&chip->client->dev, "failed reading register\n"); |
| 82 | return -EIO; |
| 83 | } |
| 84 | |
| 85 | *val = (uint16_t)ret; |
| 86 | return 0; |
| 87 | } |
| 88 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 89 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 90 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 91 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 92 | uint16_t reg_val; |
| 93 | int ret; |
| 94 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 95 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 96 | |
| 97 | reg_val = chip->reg_direction | (1u << off); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 98 | ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 99 | if (ret) |
| 100 | return ret; |
| 101 | |
| 102 | chip->reg_direction = reg_val; |
| 103 | return 0; |
| 104 | } |
| 105 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 106 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 107 | unsigned off, int val) |
| 108 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 109 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 110 | uint16_t reg_val; |
| 111 | int ret; |
| 112 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 113 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 114 | |
| 115 | /* set output level */ |
| 116 | if (val) |
| 117 | reg_val = chip->reg_output | (1u << off); |
| 118 | else |
| 119 | reg_val = chip->reg_output & ~(1u << off); |
| 120 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 121 | ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 122 | if (ret) |
| 123 | return ret; |
| 124 | |
| 125 | chip->reg_output = reg_val; |
| 126 | |
| 127 | /* then direction */ |
| 128 | reg_val = chip->reg_direction & ~(1u << off); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 129 | ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | chip->reg_direction = reg_val; |
| 134 | return 0; |
| 135 | } |
| 136 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 137 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 138 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 139 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 140 | uint16_t reg_val; |
| 141 | int ret; |
| 142 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 143 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 144 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 145 | ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 146 | if (ret < 0) { |
| 147 | /* NOTE: diagnostic already emitted; that's all we should |
| 148 | * do unless gpio_*_value_cansleep() calls become different |
| 149 | * from their nonsleeping siblings (and report faults). |
| 150 | */ |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | return (reg_val & (1u << off)) ? 1 : 0; |
| 155 | } |
| 156 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 157 | 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] | 158 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 159 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 160 | uint16_t reg_val; |
| 161 | int ret; |
| 162 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 163 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 164 | |
| 165 | if (val) |
| 166 | reg_val = chip->reg_output | (1u << off); |
| 167 | else |
| 168 | reg_val = chip->reg_output & ~(1u << off); |
| 169 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 170 | ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 171 | if (ret) |
| 172 | return; |
| 173 | |
| 174 | chip->reg_output = reg_val; |
| 175 | } |
| 176 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 177 | static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 178 | { |
| 179 | struct gpio_chip *gc; |
| 180 | |
| 181 | gc = &chip->gpio_chip; |
| 182 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 183 | gc->direction_input = pca953x_gpio_direction_input; |
| 184 | gc->direction_output = pca953x_gpio_direction_output; |
| 185 | gc->get = pca953x_gpio_get_value; |
| 186 | gc->set = pca953x_gpio_set_value; |
Arnaud Patard | 8420780 | 2008-03-10 11:43:48 -0700 | [diff] [blame^] | 187 | gc->can_sleep = 1; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 188 | |
| 189 | gc->base = chip->gpio_start; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 190 | gc->ngpio = gpios; |
| 191 | gc->label = chip->client->name; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 194 | static int __devinit pca953x_probe(struct i2c_client *client) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 195 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 196 | struct pca953x_platform_data *pdata; |
| 197 | struct pca953x_chip *chip; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 198 | int ret, i; |
| 199 | const struct pca953x_desc *id = NULL; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 200 | |
| 201 | pdata = client->dev.platform_data; |
| 202 | if (pdata == NULL) |
| 203 | return -ENODEV; |
| 204 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 205 | /* this loop vanishes when we get i2c_device_id */ |
| 206 | for (i = 0; i < ARRAY_SIZE(pca953x_descs); i++) |
| 207 | if (!strcmp(pca953x_descs[i].name, client->name)) { |
| 208 | id = pca953x_descs + i; |
| 209 | break; |
| 210 | } |
| 211 | if (!id) |
| 212 | return -ENODEV; |
| 213 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 214 | chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 215 | if (chip == NULL) |
| 216 | return -ENOMEM; |
| 217 | |
| 218 | chip->client = client; |
| 219 | |
| 220 | chip->gpio_start = pdata->gpio_base; |
| 221 | |
| 222 | /* initialize cached registers from their original values. |
| 223 | * we can't share this chip with another i2c master. |
| 224 | */ |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 225 | pca953x_setup_gpio(chip, id->driver_data); |
| 226 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 227 | ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 228 | if (ret) |
| 229 | goto out_failed; |
| 230 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 231 | ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 232 | if (ret) |
| 233 | goto out_failed; |
| 234 | |
| 235 | /* set platform specific polarity inversion */ |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 236 | ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 237 | if (ret) |
| 238 | goto out_failed; |
| 239 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 240 | |
| 241 | ret = gpiochip_add(&chip->gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 242 | if (ret) |
| 243 | goto out_failed; |
| 244 | |
| 245 | if (pdata->setup) { |
| 246 | ret = pdata->setup(client, chip->gpio_chip.base, |
| 247 | chip->gpio_chip.ngpio, pdata->context); |
| 248 | if (ret < 0) |
| 249 | dev_warn(&client->dev, "setup failed, %d\n", ret); |
| 250 | } |
| 251 | |
| 252 | i2c_set_clientdata(client, chip); |
| 253 | return 0; |
| 254 | |
| 255 | out_failed: |
| 256 | kfree(chip); |
| 257 | return ret; |
| 258 | } |
| 259 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 260 | static int pca953x_remove(struct i2c_client *client) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 261 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 262 | struct pca953x_platform_data *pdata = client->dev.platform_data; |
| 263 | struct pca953x_chip *chip = i2c_get_clientdata(client); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 264 | int ret = 0; |
| 265 | |
| 266 | if (pdata->teardown) { |
| 267 | ret = pdata->teardown(client, chip->gpio_chip.base, |
| 268 | chip->gpio_chip.ngpio, pdata->context); |
| 269 | if (ret < 0) { |
| 270 | dev_err(&client->dev, "%s failed, %d\n", |
| 271 | "teardown", ret); |
| 272 | return ret; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | ret = gpiochip_remove(&chip->gpio_chip); |
| 277 | if (ret) { |
| 278 | dev_err(&client->dev, "%s failed, %d\n", |
| 279 | "gpiochip_remove()", ret); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | kfree(chip); |
| 284 | return 0; |
| 285 | } |
| 286 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 287 | static struct i2c_driver pca953x_driver = { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 288 | .driver = { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 289 | .name = "pca953x", |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 290 | }, |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 291 | .probe = pca953x_probe, |
| 292 | .remove = pca953x_remove, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 293 | }; |
| 294 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 295 | static int __init pca953x_init(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 296 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 297 | return i2c_add_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 298 | } |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 299 | module_init(pca953x_init); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 300 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 301 | static void __exit pca953x_exit(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 302 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 303 | i2c_del_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 304 | } |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 305 | module_exit(pca953x_exit); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 306 | |
| 307 | MODULE_AUTHOR("eric miao <eric.miao@marvell.com>"); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 308 | MODULE_DESCRIPTION("GPIO expander driver for PCA953x"); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 309 | MODULE_LICENSE("GPL"); |