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> |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/irq.h> |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 19 | #include <linux/i2c.h> |
Guennadi Liakhovetski | d1c057e3 | 2008-02-06 01:39:02 -0800 | [diff] [blame] | 20 | #include <linux/i2c/pca953x.h> |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 21 | #ifdef CONFIG_OF_GPIO |
| 22 | #include <linux/of_platform.h> |
| 23 | #include <linux/of_gpio.h> |
| 24 | #endif |
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 | #define PCA953X_INPUT 0 |
| 27 | #define PCA953X_OUTPUT 1 |
| 28 | #define PCA953X_INVERT 2 |
| 29 | #define PCA953X_DIRECTION 3 |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 30 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 31 | #define PCA953X_GPIOS 0x00FF |
| 32 | #define PCA953X_INT 0x0100 |
| 33 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 34 | static const struct i2c_device_id pca953x_id[] = { |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 35 | { "pca9534", 8 | PCA953X_INT, }, |
| 36 | { "pca9535", 16 | PCA953X_INT, }, |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 37 | { "pca9536", 4, }, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 38 | { "pca9537", 4 | PCA953X_INT, }, |
| 39 | { "pca9538", 8 | PCA953X_INT, }, |
| 40 | { "pca9539", 16 | PCA953X_INT, }, |
| 41 | { "pca9554", 8 | PCA953X_INT, }, |
| 42 | { "pca9555", 16 | PCA953X_INT, }, |
Nate Case | 10dfb54 | 2009-06-17 16:26:18 -0700 | [diff] [blame] | 43 | { "pca9556", 8, }, |
Will Newton | f39e578 | 2008-05-01 04:35:10 -0700 | [diff] [blame] | 44 | { "pca9557", 8, }, |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 45 | |
David Brownell | 7059d4b | 2008-07-04 09:59:37 -0700 | [diff] [blame] | 46 | { "max7310", 8, }, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 47 | { "max7312", 16 | PCA953X_INT, }, |
| 48 | { "max7313", 16 | PCA953X_INT, }, |
| 49 | { "max7315", 8 | PCA953X_INT, }, |
| 50 | { "pca6107", 8 | PCA953X_INT, }, |
| 51 | { "tca6408", 8 | PCA953X_INT, }, |
| 52 | { "tca6416", 16 | PCA953X_INT, }, |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 53 | /* NYET: { "tca6424", 24, }, */ |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 54 | { } |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 55 | }; |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 56 | MODULE_DEVICE_TABLE(i2c, pca953x_id); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 57 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 58 | struct pca953x_chip { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 59 | unsigned gpio_start; |
| 60 | uint16_t reg_output; |
| 61 | uint16_t reg_direction; |
| 62 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 63 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
| 64 | struct mutex irq_lock; |
| 65 | uint16_t irq_mask; |
| 66 | uint16_t irq_stat; |
| 67 | uint16_t irq_trig_raise; |
| 68 | uint16_t irq_trig_fall; |
| 69 | int irq_base; |
| 70 | #endif |
| 71 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 72 | struct i2c_client *client; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 73 | struct pca953x_platform_data *dyn_pdata; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 74 | struct gpio_chip gpio_chip; |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 75 | char **names; |
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_write_reg(struct pca953x_chip *chip, int reg, uint16_t val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 79 | { |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 80 | int ret; |
| 81 | |
| 82 | if (chip->gpio_chip.ngpio <= 8) |
| 83 | ret = i2c_smbus_write_byte_data(chip->client, reg, val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 84 | else |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 85 | ret = i2c_smbus_write_word_data(chip->client, reg << 1, val); |
| 86 | |
| 87 | if (ret < 0) { |
| 88 | dev_err(&chip->client->dev, "failed writing register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 89 | return ret; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 95 | 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] | 96 | { |
| 97 | int ret; |
| 98 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 99 | if (chip->gpio_chip.ngpio <= 8) |
| 100 | ret = i2c_smbus_read_byte_data(chip->client, reg); |
| 101 | else |
| 102 | ret = i2c_smbus_read_word_data(chip->client, reg << 1); |
| 103 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 104 | if (ret < 0) { |
| 105 | dev_err(&chip->client->dev, "failed reading register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 106 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | *val = (uint16_t)ret; |
| 110 | return 0; |
| 111 | } |
| 112 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 113 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 114 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 115 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 116 | uint16_t reg_val; |
| 117 | int ret; |
| 118 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 119 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 120 | |
| 121 | reg_val = chip->reg_direction | (1u << off); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 122 | ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 123 | if (ret) |
| 124 | return ret; |
| 125 | |
| 126 | chip->reg_direction = reg_val; |
| 127 | return 0; |
| 128 | } |
| 129 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 130 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 131 | unsigned off, int val) |
| 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 | /* set output level */ |
| 140 | if (val) |
| 141 | reg_val = chip->reg_output | (1u << off); |
| 142 | else |
| 143 | reg_val = chip->reg_output & ~(1u << off); |
| 144 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 145 | ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 146 | if (ret) |
| 147 | return ret; |
| 148 | |
| 149 | chip->reg_output = reg_val; |
| 150 | |
| 151 | /* then direction */ |
| 152 | reg_val = chip->reg_direction & ~(1u << off); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 153 | ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | chip->reg_direction = reg_val; |
| 158 | return 0; |
| 159 | } |
| 160 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 161 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 162 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 163 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 164 | uint16_t reg_val; |
| 165 | int ret; |
| 166 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 167 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 168 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 169 | ret = pca953x_read_reg(chip, PCA953X_INPUT, ®_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 170 | if (ret < 0) { |
| 171 | /* NOTE: diagnostic already emitted; that's all we should |
| 172 | * do unless gpio_*_value_cansleep() calls become different |
| 173 | * from their nonsleeping siblings (and report faults). |
| 174 | */ |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | return (reg_val & (1u << off)) ? 1 : 0; |
| 179 | } |
| 180 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 181 | 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] | 182 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 183 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 184 | uint16_t reg_val; |
| 185 | int ret; |
| 186 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 187 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 188 | |
| 189 | if (val) |
| 190 | reg_val = chip->reg_output | (1u << off); |
| 191 | else |
| 192 | reg_val = chip->reg_output & ~(1u << off); |
| 193 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 194 | ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 195 | if (ret) |
| 196 | return; |
| 197 | |
| 198 | chip->reg_output = reg_val; |
| 199 | } |
| 200 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 201 | static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 202 | { |
| 203 | struct gpio_chip *gc; |
| 204 | |
| 205 | gc = &chip->gpio_chip; |
| 206 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 207 | gc->direction_input = pca953x_gpio_direction_input; |
| 208 | gc->direction_output = pca953x_gpio_direction_output; |
| 209 | gc->get = pca953x_gpio_get_value; |
| 210 | gc->set = pca953x_gpio_set_value; |
Arnaud Patard | 8420780 | 2008-03-10 11:43:48 -0700 | [diff] [blame] | 211 | gc->can_sleep = 1; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 212 | |
| 213 | gc->base = chip->gpio_start; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 214 | gc->ngpio = gpios; |
| 215 | gc->label = chip->client->name; |
David Brownell | d8f388d | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 216 | gc->dev = &chip->client->dev; |
Guennadi Liakhovetski | d72cbed | 2008-04-28 02:14:45 -0700 | [diff] [blame] | 217 | gc->owner = THIS_MODULE; |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 218 | gc->names = chip->names; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 221 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
| 222 | static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off) |
| 223 | { |
| 224 | struct pca953x_chip *chip; |
| 225 | |
| 226 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
| 227 | return chip->irq_base + off; |
| 228 | } |
| 229 | |
| 230 | static void pca953x_irq_mask(unsigned int irq) |
| 231 | { |
| 232 | struct pca953x_chip *chip = get_irq_chip_data(irq); |
| 233 | |
| 234 | chip->irq_mask &= ~(1 << (irq - chip->irq_base)); |
| 235 | } |
| 236 | |
| 237 | static void pca953x_irq_unmask(unsigned int irq) |
| 238 | { |
| 239 | struct pca953x_chip *chip = get_irq_chip_data(irq); |
| 240 | |
| 241 | chip->irq_mask |= 1 << (irq - chip->irq_base); |
| 242 | } |
| 243 | |
| 244 | static void pca953x_irq_bus_lock(unsigned int irq) |
| 245 | { |
| 246 | struct pca953x_chip *chip = get_irq_chip_data(irq); |
| 247 | |
| 248 | mutex_lock(&chip->irq_lock); |
| 249 | } |
| 250 | |
| 251 | static void pca953x_irq_bus_sync_unlock(unsigned int irq) |
| 252 | { |
| 253 | struct pca953x_chip *chip = get_irq_chip_data(irq); |
| 254 | |
| 255 | mutex_unlock(&chip->irq_lock); |
| 256 | } |
| 257 | |
| 258 | static int pca953x_irq_set_type(unsigned int irq, unsigned int type) |
| 259 | { |
| 260 | struct pca953x_chip *chip = get_irq_chip_data(irq); |
| 261 | uint16_t level = irq - chip->irq_base; |
| 262 | uint16_t mask = 1 << level; |
| 263 | |
| 264 | if (!(type & IRQ_TYPE_EDGE_BOTH)) { |
| 265 | dev_err(&chip->client->dev, "irq %d: unsupported type %d\n", |
| 266 | irq, type); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | if (type & IRQ_TYPE_EDGE_FALLING) |
| 271 | chip->irq_trig_fall |= mask; |
| 272 | else |
| 273 | chip->irq_trig_fall &= ~mask; |
| 274 | |
| 275 | if (type & IRQ_TYPE_EDGE_RISING) |
| 276 | chip->irq_trig_raise |= mask; |
| 277 | else |
| 278 | chip->irq_trig_raise &= ~mask; |
| 279 | |
| 280 | return pca953x_gpio_direction_input(&chip->gpio_chip, level); |
| 281 | } |
| 282 | |
| 283 | static struct irq_chip pca953x_irq_chip = { |
| 284 | .name = "pca953x", |
| 285 | .mask = pca953x_irq_mask, |
| 286 | .unmask = pca953x_irq_unmask, |
| 287 | .bus_lock = pca953x_irq_bus_lock, |
| 288 | .bus_sync_unlock = pca953x_irq_bus_sync_unlock, |
| 289 | .set_type = pca953x_irq_set_type, |
| 290 | }; |
| 291 | |
| 292 | static uint16_t pca953x_irq_pending(struct pca953x_chip *chip) |
| 293 | { |
| 294 | uint16_t cur_stat; |
| 295 | uint16_t old_stat; |
| 296 | uint16_t pending; |
| 297 | uint16_t trigger; |
| 298 | int ret; |
| 299 | |
| 300 | ret = pca953x_read_reg(chip, PCA953X_INPUT, &cur_stat); |
| 301 | if (ret) |
| 302 | return 0; |
| 303 | |
| 304 | /* Remove output pins from the equation */ |
| 305 | cur_stat &= chip->reg_direction; |
| 306 | |
| 307 | old_stat = chip->irq_stat; |
| 308 | trigger = (cur_stat ^ old_stat) & chip->irq_mask; |
| 309 | |
| 310 | if (!trigger) |
| 311 | return 0; |
| 312 | |
| 313 | chip->irq_stat = cur_stat; |
| 314 | |
| 315 | pending = (old_stat & chip->irq_trig_fall) | |
| 316 | (cur_stat & chip->irq_trig_raise); |
| 317 | pending &= trigger; |
| 318 | |
| 319 | return pending; |
| 320 | } |
| 321 | |
| 322 | static irqreturn_t pca953x_irq_handler(int irq, void *devid) |
| 323 | { |
| 324 | struct pca953x_chip *chip = devid; |
| 325 | uint16_t pending; |
| 326 | uint16_t level; |
| 327 | |
| 328 | pending = pca953x_irq_pending(chip); |
| 329 | |
| 330 | if (!pending) |
| 331 | return IRQ_HANDLED; |
| 332 | |
| 333 | do { |
| 334 | level = __ffs(pending); |
| 335 | handle_nested_irq(level + chip->irq_base); |
| 336 | |
| 337 | pending &= ~(1 << level); |
| 338 | } while (pending); |
| 339 | |
| 340 | return IRQ_HANDLED; |
| 341 | } |
| 342 | |
| 343 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
| 344 | const struct i2c_device_id *id) |
| 345 | { |
| 346 | struct i2c_client *client = chip->client; |
| 347 | struct pca953x_platform_data *pdata = client->dev.platform_data; |
| 348 | int ret; |
| 349 | |
| 350 | if (pdata->irq_base && (id->driver_data & PCA953X_INT)) { |
| 351 | int lvl; |
| 352 | |
| 353 | ret = pca953x_read_reg(chip, PCA953X_INPUT, |
| 354 | &chip->irq_stat); |
| 355 | if (ret) |
| 356 | goto out_failed; |
| 357 | |
| 358 | /* |
| 359 | * There is no way to know which GPIO line generated the |
| 360 | * interrupt. We have to rely on the previous read for |
| 361 | * this purpose. |
| 362 | */ |
| 363 | chip->irq_stat &= chip->reg_direction; |
| 364 | chip->irq_base = pdata->irq_base; |
| 365 | mutex_init(&chip->irq_lock); |
| 366 | |
| 367 | for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) { |
| 368 | int irq = lvl + chip->irq_base; |
| 369 | |
| 370 | set_irq_chip_data(irq, chip); |
| 371 | set_irq_chip_and_handler(irq, &pca953x_irq_chip, |
| 372 | handle_edge_irq); |
| 373 | set_irq_nested_thread(irq, 1); |
| 374 | #ifdef CONFIG_ARM |
| 375 | set_irq_flags(irq, IRQF_VALID); |
| 376 | #else |
| 377 | set_irq_noprobe(irq); |
| 378 | #endif |
| 379 | } |
| 380 | |
| 381 | ret = request_threaded_irq(client->irq, |
| 382 | NULL, |
| 383 | pca953x_irq_handler, |
| 384 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 385 | dev_name(&client->dev), chip); |
| 386 | if (ret) { |
| 387 | dev_err(&client->dev, "failed to request irq %d\n", |
| 388 | client->irq); |
| 389 | goto out_failed; |
| 390 | } |
| 391 | |
| 392 | chip->gpio_chip.to_irq = pca953x_gpio_to_irq; |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | |
| 397 | out_failed: |
| 398 | chip->irq_base = 0; |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | static void pca953x_irq_teardown(struct pca953x_chip *chip) |
| 403 | { |
| 404 | if (chip->irq_base) |
| 405 | free_irq(chip->client->irq, chip); |
| 406 | } |
| 407 | #else /* CONFIG_GPIO_PCA953X_IRQ */ |
| 408 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
| 409 | const struct i2c_device_id *id) |
| 410 | { |
| 411 | struct i2c_client *client = chip->client; |
| 412 | struct pca953x_platform_data *pdata = client->dev.platform_data; |
| 413 | |
| 414 | if (pdata->irq_base && (id->driver_data & PCA953X_INT)) |
| 415 | dev_warn(&client->dev, "interrupt support not compiled in\n"); |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static void pca953x_irq_teardown(struct pca953x_chip *chip) |
| 421 | { |
| 422 | } |
| 423 | #endif |
| 424 | |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 425 | /* |
| 426 | * Handlers for alternative sources of platform_data |
| 427 | */ |
| 428 | #ifdef CONFIG_OF_GPIO |
| 429 | /* |
| 430 | * Translate OpenFirmware node properties into platform_data |
| 431 | */ |
| 432 | static struct pca953x_platform_data * |
| 433 | pca953x_get_alt_pdata(struct i2c_client *client) |
| 434 | { |
| 435 | struct pca953x_platform_data *pdata; |
| 436 | struct device_node *node; |
| 437 | const uint16_t *val; |
| 438 | |
| 439 | node = dev_archdata_get_node(&client->dev.archdata); |
| 440 | if (node == NULL) |
| 441 | return NULL; |
| 442 | |
| 443 | pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL); |
| 444 | if (pdata == NULL) { |
| 445 | dev_err(&client->dev, "Unable to allocate platform_data\n"); |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | pdata->gpio_base = -1; |
| 450 | val = of_get_property(node, "linux,gpio-base", NULL); |
| 451 | if (val) { |
| 452 | if (*val < 0) |
| 453 | dev_warn(&client->dev, |
| 454 | "invalid gpio-base in device tree\n"); |
| 455 | else |
| 456 | pdata->gpio_base = *val; |
| 457 | } |
| 458 | |
| 459 | val = of_get_property(node, "polarity", NULL); |
| 460 | if (val) |
| 461 | pdata->invert = *val; |
| 462 | |
| 463 | return pdata; |
| 464 | } |
| 465 | #else |
| 466 | static struct pca953x_platform_data * |
| 467 | pca953x_get_alt_pdata(struct i2c_client *client) |
| 468 | { |
| 469 | return NULL; |
| 470 | } |
| 471 | #endif |
| 472 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 473 | static int __devinit pca953x_probe(struct i2c_client *client, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 474 | const struct i2c_device_id *id) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 475 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 476 | struct pca953x_platform_data *pdata; |
| 477 | struct pca953x_chip *chip; |
Will Newton | f39e578 | 2008-05-01 04:35:10 -0700 | [diff] [blame] | 478 | int ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 479 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 480 | chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 481 | if (chip == NULL) |
| 482 | return -ENOMEM; |
| 483 | |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 484 | pdata = client->dev.platform_data; |
| 485 | if (pdata == NULL) { |
| 486 | pdata = pca953x_get_alt_pdata(client); |
| 487 | /* |
| 488 | * Unlike normal platform_data, this is allocated |
| 489 | * dynamically and must be freed in the driver |
| 490 | */ |
| 491 | chip->dyn_pdata = pdata; |
| 492 | } |
| 493 | |
| 494 | if (pdata == NULL) { |
| 495 | dev_dbg(&client->dev, "no platform data\n"); |
| 496 | ret = -EINVAL; |
| 497 | goto out_failed; |
| 498 | } |
| 499 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 500 | chip->client = client; |
| 501 | |
| 502 | chip->gpio_start = pdata->gpio_base; |
| 503 | |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 504 | chip->names = pdata->names; |
| 505 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 506 | /* initialize cached registers from their original values. |
| 507 | * we can't share this chip with another i2c master. |
| 508 | */ |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 509 | pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS); |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 510 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 511 | ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 512 | if (ret) |
| 513 | goto out_failed; |
| 514 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 515 | ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 516 | if (ret) |
| 517 | goto out_failed; |
| 518 | |
| 519 | /* set platform specific polarity inversion */ |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 520 | ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 521 | if (ret) |
| 522 | goto out_failed; |
| 523 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 524 | ret = pca953x_irq_setup(chip, id); |
| 525 | if (ret) |
| 526 | goto out_failed; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 527 | |
| 528 | ret = gpiochip_add(&chip->gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 529 | if (ret) |
| 530 | goto out_failed; |
| 531 | |
| 532 | if (pdata->setup) { |
| 533 | ret = pdata->setup(client, chip->gpio_chip.base, |
| 534 | chip->gpio_chip.ngpio, pdata->context); |
| 535 | if (ret < 0) |
| 536 | dev_warn(&client->dev, "setup failed, %d\n", ret); |
| 537 | } |
| 538 | |
| 539 | i2c_set_clientdata(client, chip); |
| 540 | return 0; |
| 541 | |
| 542 | out_failed: |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 543 | pca953x_irq_teardown(chip); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 544 | kfree(chip->dyn_pdata); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 545 | kfree(chip); |
| 546 | return ret; |
| 547 | } |
| 548 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 549 | static int pca953x_remove(struct i2c_client *client) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 550 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 551 | struct pca953x_platform_data *pdata = client->dev.platform_data; |
| 552 | struct pca953x_chip *chip = i2c_get_clientdata(client); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 553 | int ret = 0; |
| 554 | |
| 555 | if (pdata->teardown) { |
| 556 | ret = pdata->teardown(client, chip->gpio_chip.base, |
| 557 | chip->gpio_chip.ngpio, pdata->context); |
| 558 | if (ret < 0) { |
| 559 | dev_err(&client->dev, "%s failed, %d\n", |
| 560 | "teardown", ret); |
| 561 | return ret; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | ret = gpiochip_remove(&chip->gpio_chip); |
| 566 | if (ret) { |
| 567 | dev_err(&client->dev, "%s failed, %d\n", |
| 568 | "gpiochip_remove()", ret); |
| 569 | return ret; |
| 570 | } |
| 571 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 572 | pca953x_irq_teardown(chip); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 573 | kfree(chip->dyn_pdata); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 574 | kfree(chip); |
| 575 | return 0; |
| 576 | } |
| 577 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 578 | static struct i2c_driver pca953x_driver = { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 579 | .driver = { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 580 | .name = "pca953x", |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 581 | }, |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 582 | .probe = pca953x_probe, |
| 583 | .remove = pca953x_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 584 | .id_table = pca953x_id, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 585 | }; |
| 586 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 587 | static int __init pca953x_init(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 588 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 589 | return i2c_add_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 590 | } |
David Brownell | 2f8d119 | 2008-10-15 22:03:13 -0700 | [diff] [blame] | 591 | /* register after i2c postcore initcall and before |
| 592 | * subsys initcalls that may rely on these GPIOs |
| 593 | */ |
| 594 | subsys_initcall(pca953x_init); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 595 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 596 | static void __exit pca953x_exit(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 597 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 598 | i2c_del_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 599 | } |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 600 | module_exit(pca953x_exit); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 601 | |
| 602 | MODULE_AUTHOR("eric miao <eric.miao@marvell.com>"); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 603 | MODULE_DESCRIPTION("GPIO expander driver for PCA953x"); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 604 | MODULE_LICENSE("GPL"); |