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