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