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