eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 2 | * PCA953x 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 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +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 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 32 | #define PCA957X_IN 0 |
| 33 | #define PCA957X_INVRT 1 |
| 34 | #define PCA957X_BKEN 2 |
| 35 | #define PCA957X_PUPD 3 |
| 36 | #define PCA957X_CFG 4 |
| 37 | #define PCA957X_OUT 5 |
| 38 | #define PCA957X_MSK 6 |
| 39 | #define PCA957X_INTS 7 |
| 40 | |
| 41 | #define PCA_GPIO_MASK 0x00FF |
| 42 | #define PCA_INT 0x0100 |
| 43 | #define PCA953X_TYPE 0x1000 |
| 44 | #define PCA957X_TYPE 0x2000 |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 45 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 46 | static const struct i2c_device_id pca953x_id[] = { |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 47 | { "pca9534", 8 | PCA953X_TYPE | PCA_INT, }, |
| 48 | { "pca9535", 16 | PCA953X_TYPE | PCA_INT, }, |
| 49 | { "pca9536", 4 | PCA953X_TYPE, }, |
| 50 | { "pca9537", 4 | PCA953X_TYPE | PCA_INT, }, |
| 51 | { "pca9538", 8 | PCA953X_TYPE | PCA_INT, }, |
| 52 | { "pca9539", 16 | PCA953X_TYPE | PCA_INT, }, |
| 53 | { "pca9554", 8 | PCA953X_TYPE | PCA_INT, }, |
| 54 | { "pca9555", 16 | PCA953X_TYPE | PCA_INT, }, |
| 55 | { "pca9556", 8 | PCA953X_TYPE, }, |
| 56 | { "pca9557", 8 | PCA953X_TYPE, }, |
| 57 | { "pca9574", 8 | PCA957X_TYPE | PCA_INT, }, |
| 58 | { "pca9575", 16 | PCA957X_TYPE | PCA_INT, }, |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 59 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 60 | { "max7310", 8 | PCA953X_TYPE, }, |
| 61 | { "max7312", 16 | PCA953X_TYPE | PCA_INT, }, |
| 62 | { "max7313", 16 | PCA953X_TYPE | PCA_INT, }, |
| 63 | { "max7315", 8 | PCA953X_TYPE | PCA_INT, }, |
| 64 | { "pca6107", 8 | PCA953X_TYPE | PCA_INT, }, |
| 65 | { "tca6408", 8 | PCA953X_TYPE | PCA_INT, }, |
| 66 | { "tca6416", 16 | PCA953X_TYPE | PCA_INT, }, |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 67 | /* NYET: { "tca6424", 24, }, */ |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 68 | { } |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 69 | }; |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 70 | MODULE_DEVICE_TABLE(i2c, pca953x_id); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 71 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 72 | struct pca953x_chip { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 73 | unsigned gpio_start; |
| 74 | uint16_t reg_output; |
| 75 | uint16_t reg_direction; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 76 | struct mutex i2c_lock; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 77 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 78 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
| 79 | struct mutex irq_lock; |
| 80 | uint16_t irq_mask; |
| 81 | uint16_t irq_stat; |
| 82 | uint16_t irq_trig_raise; |
| 83 | uint16_t irq_trig_fall; |
| 84 | int irq_base; |
| 85 | #endif |
| 86 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 87 | struct i2c_client *client; |
| 88 | struct gpio_chip gpio_chip; |
Uwe Kleine-König | 6215499 | 2010-05-26 14:42:17 -0700 | [diff] [blame] | 89 | const char *const *names; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 90 | int chip_type; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 93 | 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] | 94 | { |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 95 | int ret = 0; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 96 | |
| 97 | if (chip->gpio_chip.ngpio <= 8) |
| 98 | ret = i2c_smbus_write_byte_data(chip->client, reg, val); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 99 | else { |
| 100 | switch (chip->chip_type) { |
| 101 | case PCA953X_TYPE: |
| 102 | ret = i2c_smbus_write_word_data(chip->client, |
| 103 | reg << 1, val); |
| 104 | break; |
| 105 | case PCA957X_TYPE: |
| 106 | ret = i2c_smbus_write_byte_data(chip->client, reg << 1, |
| 107 | val & 0xff); |
| 108 | if (ret < 0) |
| 109 | break; |
| 110 | ret = i2c_smbus_write_byte_data(chip->client, |
| 111 | (reg << 1) + 1, |
| 112 | (val & 0xff00) >> 8); |
| 113 | break; |
| 114 | } |
| 115 | } |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 116 | |
| 117 | if (ret < 0) { |
| 118 | dev_err(&chip->client->dev, "failed writing register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 119 | return ret; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 125 | 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] | 126 | { |
| 127 | int ret; |
| 128 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 129 | if (chip->gpio_chip.ngpio <= 8) |
| 130 | ret = i2c_smbus_read_byte_data(chip->client, reg); |
| 131 | else |
| 132 | ret = i2c_smbus_read_word_data(chip->client, reg << 1); |
| 133 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 134 | if (ret < 0) { |
| 135 | dev_err(&chip->client->dev, "failed reading register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 136 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | *val = (uint16_t)ret; |
| 140 | return 0; |
| 141 | } |
| 142 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 143 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) |
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 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 146 | uint16_t reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 147 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 148 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 149 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 150 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 151 | mutex_lock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 152 | reg_val = chip->reg_direction | (1u << off); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 153 | |
| 154 | switch (chip->chip_type) { |
| 155 | case PCA953X_TYPE: |
| 156 | offset = PCA953X_DIRECTION; |
| 157 | break; |
| 158 | case PCA957X_TYPE: |
| 159 | offset = PCA957X_CFG; |
| 160 | break; |
| 161 | } |
| 162 | ret = pca953x_write_reg(chip, offset, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 163 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 164 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 165 | |
| 166 | chip->reg_direction = reg_val; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 167 | ret = 0; |
| 168 | exit: |
| 169 | mutex_unlock(&chip->i2c_lock); |
| 170 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 173 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 174 | unsigned off, int val) |
| 175 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 176 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 177 | uint16_t reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 178 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 179 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 180 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 181 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 182 | mutex_lock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 183 | /* set output level */ |
| 184 | if (val) |
| 185 | reg_val = chip->reg_output | (1u << off); |
| 186 | else |
| 187 | reg_val = chip->reg_output & ~(1u << off); |
| 188 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 189 | switch (chip->chip_type) { |
| 190 | case PCA953X_TYPE: |
| 191 | offset = PCA953X_OUTPUT; |
| 192 | break; |
| 193 | case PCA957X_TYPE: |
| 194 | offset = PCA957X_OUT; |
| 195 | break; |
| 196 | } |
| 197 | ret = pca953x_write_reg(chip, offset, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 198 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 199 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 200 | |
| 201 | chip->reg_output = reg_val; |
| 202 | |
| 203 | /* then direction */ |
| 204 | reg_val = chip->reg_direction & ~(1u << off); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 205 | switch (chip->chip_type) { |
| 206 | case PCA953X_TYPE: |
| 207 | offset = PCA953X_DIRECTION; |
| 208 | break; |
| 209 | case PCA957X_TYPE: |
| 210 | offset = PCA957X_CFG; |
| 211 | break; |
| 212 | } |
| 213 | ret = pca953x_write_reg(chip, offset, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 214 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 215 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 216 | |
| 217 | chip->reg_direction = reg_val; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 218 | ret = 0; |
| 219 | exit: |
| 220 | mutex_unlock(&chip->i2c_lock); |
| 221 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 224 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 225 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 226 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 227 | uint16_t reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 228 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 229 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 230 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 231 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 232 | mutex_lock(&chip->i2c_lock); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 233 | switch (chip->chip_type) { |
| 234 | case PCA953X_TYPE: |
| 235 | offset = PCA953X_INPUT; |
| 236 | break; |
| 237 | case PCA957X_TYPE: |
| 238 | offset = PCA957X_IN; |
| 239 | break; |
| 240 | } |
| 241 | ret = pca953x_read_reg(chip, offset, ®_val); |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 242 | mutex_unlock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 243 | if (ret < 0) { |
| 244 | /* NOTE: diagnostic already emitted; that's all we should |
| 245 | * do unless gpio_*_value_cansleep() calls become different |
| 246 | * from their nonsleeping siblings (and report faults). |
| 247 | */ |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | return (reg_val & (1u << off)) ? 1 : 0; |
| 252 | } |
| 253 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 254 | 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] | 255 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 256 | struct pca953x_chip *chip; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 257 | uint16_t reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 258 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 259 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 260 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 261 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 262 | mutex_lock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 263 | if (val) |
| 264 | reg_val = chip->reg_output | (1u << off); |
| 265 | else |
| 266 | reg_val = chip->reg_output & ~(1u << off); |
| 267 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 268 | switch (chip->chip_type) { |
| 269 | case PCA953X_TYPE: |
| 270 | offset = PCA953X_OUTPUT; |
| 271 | break; |
| 272 | case PCA957X_TYPE: |
| 273 | offset = PCA957X_OUT; |
| 274 | break; |
| 275 | } |
| 276 | ret = pca953x_write_reg(chip, offset, reg_val); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 277 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 278 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 279 | |
| 280 | chip->reg_output = reg_val; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 281 | exit: |
| 282 | mutex_unlock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 285 | static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 286 | { |
| 287 | struct gpio_chip *gc; |
| 288 | |
| 289 | gc = &chip->gpio_chip; |
| 290 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 291 | gc->direction_input = pca953x_gpio_direction_input; |
| 292 | gc->direction_output = pca953x_gpio_direction_output; |
| 293 | gc->get = pca953x_gpio_get_value; |
| 294 | gc->set = pca953x_gpio_set_value; |
Arnaud Patard | 8420780 | 2008-03-10 11:43:48 -0700 | [diff] [blame] | 295 | gc->can_sleep = 1; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 296 | |
| 297 | gc->base = chip->gpio_start; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 298 | gc->ngpio = gpios; |
| 299 | gc->label = chip->client->name; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 300 | gc->dev = &chip->client->dev; |
Guennadi Liakhovetski | d72cbed | 2008-04-28 02:14:45 -0700 | [diff] [blame] | 301 | gc->owner = THIS_MODULE; |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 302 | gc->names = chip->names; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 305 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
| 306 | static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off) |
| 307 | { |
| 308 | struct pca953x_chip *chip; |
| 309 | |
| 310 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
| 311 | return chip->irq_base + off; |
| 312 | } |
| 313 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 314 | static void pca953x_irq_mask(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 315 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 316 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 317 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 318 | chip->irq_mask &= ~(1 << (d->irq - chip->irq_base)); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 321 | static void pca953x_irq_unmask(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 322 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 323 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 324 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 325 | chip->irq_mask |= 1 << (d->irq - chip->irq_base); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 328 | static void pca953x_irq_bus_lock(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 329 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 330 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 331 | |
| 332 | mutex_lock(&chip->irq_lock); |
| 333 | } |
| 334 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 335 | static void pca953x_irq_bus_sync_unlock(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 336 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 337 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 338 | uint16_t new_irqs; |
| 339 | uint16_t level; |
| 340 | |
| 341 | /* Look for any newly setup interrupt */ |
| 342 | new_irqs = chip->irq_trig_fall | chip->irq_trig_raise; |
| 343 | new_irqs &= ~chip->reg_direction; |
| 344 | |
| 345 | while (new_irqs) { |
| 346 | level = __ffs(new_irqs); |
| 347 | pca953x_gpio_direction_input(&chip->gpio_chip, level); |
| 348 | new_irqs &= ~(1 << level); |
| 349 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 350 | |
| 351 | mutex_unlock(&chip->irq_lock); |
| 352 | } |
| 353 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 354 | static int pca953x_irq_set_type(struct irq_data *d, unsigned int type) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 355 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 356 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
| 357 | uint16_t level = d->irq - chip->irq_base; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 358 | uint16_t mask = 1 << level; |
| 359 | |
| 360 | if (!(type & IRQ_TYPE_EDGE_BOTH)) { |
| 361 | dev_err(&chip->client->dev, "irq %d: unsupported type %d\n", |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 362 | d->irq, type); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 363 | return -EINVAL; |
| 364 | } |
| 365 | |
| 366 | if (type & IRQ_TYPE_EDGE_FALLING) |
| 367 | chip->irq_trig_fall |= mask; |
| 368 | else |
| 369 | chip->irq_trig_fall &= ~mask; |
| 370 | |
| 371 | if (type & IRQ_TYPE_EDGE_RISING) |
| 372 | chip->irq_trig_raise |= mask; |
| 373 | else |
| 374 | chip->irq_trig_raise &= ~mask; |
| 375 | |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 376 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static struct irq_chip pca953x_irq_chip = { |
| 380 | .name = "pca953x", |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 381 | .irq_mask = pca953x_irq_mask, |
| 382 | .irq_unmask = pca953x_irq_unmask, |
| 383 | .irq_bus_lock = pca953x_irq_bus_lock, |
| 384 | .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock, |
| 385 | .irq_set_type = pca953x_irq_set_type, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 386 | }; |
| 387 | |
| 388 | static uint16_t pca953x_irq_pending(struct pca953x_chip *chip) |
| 389 | { |
| 390 | uint16_t cur_stat; |
| 391 | uint16_t old_stat; |
| 392 | uint16_t pending; |
| 393 | uint16_t trigger; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 394 | int ret, offset = 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 395 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 396 | switch (chip->chip_type) { |
| 397 | case PCA953X_TYPE: |
| 398 | offset = PCA953X_INPUT; |
| 399 | break; |
| 400 | case PCA957X_TYPE: |
| 401 | offset = PCA957X_IN; |
| 402 | break; |
| 403 | } |
| 404 | ret = pca953x_read_reg(chip, offset, &cur_stat); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 405 | if (ret) |
| 406 | return 0; |
| 407 | |
| 408 | /* Remove output pins from the equation */ |
| 409 | cur_stat &= chip->reg_direction; |
| 410 | |
| 411 | old_stat = chip->irq_stat; |
| 412 | trigger = (cur_stat ^ old_stat) & chip->irq_mask; |
| 413 | |
| 414 | if (!trigger) |
| 415 | return 0; |
| 416 | |
| 417 | chip->irq_stat = cur_stat; |
| 418 | |
| 419 | pending = (old_stat & chip->irq_trig_fall) | |
| 420 | (cur_stat & chip->irq_trig_raise); |
| 421 | pending &= trigger; |
| 422 | |
| 423 | return pending; |
| 424 | } |
| 425 | |
| 426 | static irqreturn_t pca953x_irq_handler(int irq, void *devid) |
| 427 | { |
| 428 | struct pca953x_chip *chip = devid; |
| 429 | uint16_t pending; |
| 430 | uint16_t level; |
| 431 | |
| 432 | pending = pca953x_irq_pending(chip); |
| 433 | |
| 434 | if (!pending) |
| 435 | return IRQ_HANDLED; |
| 436 | |
| 437 | do { |
| 438 | level = __ffs(pending); |
David Jander | 6dd599f | 2011-06-08 11:37:45 -0600 | [diff] [blame] | 439 | handle_nested_irq(level + chip->irq_base); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 440 | |
| 441 | pending &= ~(1 << level); |
| 442 | } while (pending); |
| 443 | |
| 444 | return IRQ_HANDLED; |
| 445 | } |
| 446 | |
| 447 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 448 | const struct i2c_device_id *id, |
| 449 | int irq_base) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 450 | { |
| 451 | struct i2c_client *client = chip->client; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 452 | int ret, offset = 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 453 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 454 | if (irq_base != -1 |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 455 | && (id->driver_data & PCA_INT)) { |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 456 | int lvl; |
| 457 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 458 | switch (chip->chip_type) { |
| 459 | case PCA953X_TYPE: |
| 460 | offset = PCA953X_INPUT; |
| 461 | break; |
| 462 | case PCA957X_TYPE: |
| 463 | offset = PCA957X_IN; |
| 464 | break; |
| 465 | } |
| 466 | ret = pca953x_read_reg(chip, offset, &chip->irq_stat); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 467 | if (ret) |
| 468 | goto out_failed; |
| 469 | |
| 470 | /* |
| 471 | * There is no way to know which GPIO line generated the |
| 472 | * interrupt. We have to rely on the previous read for |
| 473 | * this purpose. |
| 474 | */ |
| 475 | chip->irq_stat &= chip->reg_direction; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 476 | mutex_init(&chip->irq_lock); |
| 477 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 478 | chip->irq_base = irq_alloc_descs(-1, irq_base, chip->gpio_chip.ngpio, -1); |
David Jander | 910c8fb | 2011-06-08 11:34:37 -0600 | [diff] [blame] | 479 | if (chip->irq_base < 0) |
| 480 | goto out_failed; |
| 481 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 482 | for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) { |
| 483 | int irq = lvl + chip->irq_base; |
| 484 | |
David Jander | 910c8fb | 2011-06-08 11:34:37 -0600 | [diff] [blame] | 485 | irq_clear_status_flags(irq, IRQ_NOREQUEST); |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 486 | irq_set_chip_data(irq, chip); |
David Jander | 6dd599f | 2011-06-08 11:37:45 -0600 | [diff] [blame] | 487 | irq_set_chip(irq, &pca953x_irq_chip); |
| 488 | irq_set_nested_thread(irq, true); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 489 | #ifdef CONFIG_ARM |
| 490 | set_irq_flags(irq, IRQF_VALID); |
| 491 | #else |
Thomas Gleixner | b51804b | 2011-03-24 21:27:36 +0000 | [diff] [blame] | 492 | irq_set_noprobe(irq); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 493 | #endif |
| 494 | } |
| 495 | |
| 496 | ret = request_threaded_irq(client->irq, |
| 497 | NULL, |
| 498 | pca953x_irq_handler, |
David Jander | 17e8b42 | 2011-06-08 11:34:41 -0600 | [diff] [blame] | 499 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 500 | dev_name(&client->dev), chip); |
| 501 | if (ret) { |
| 502 | dev_err(&client->dev, "failed to request irq %d\n", |
| 503 | client->irq); |
| 504 | goto out_failed; |
| 505 | } |
| 506 | |
| 507 | chip->gpio_chip.to_irq = pca953x_gpio_to_irq; |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | |
| 512 | out_failed: |
Alek Du | 8a233f01 | 2010-10-26 14:22:41 -0700 | [diff] [blame] | 513 | chip->irq_base = -1; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | static void pca953x_irq_teardown(struct pca953x_chip *chip) |
| 518 | { |
David Jander | c609c05 | 2011-06-14 11:00:54 +0200 | [diff] [blame] | 519 | if (chip->irq_base != -1) { |
| 520 | irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 521 | free_irq(chip->client->irq, chip); |
David Jander | c609c05 | 2011-06-14 11:00:54 +0200 | [diff] [blame] | 522 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 523 | } |
| 524 | #else /* CONFIG_GPIO_PCA953X_IRQ */ |
| 525 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 526 | const struct i2c_device_id *id, |
| 527 | int irq_base) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 528 | { |
| 529 | struct i2c_client *client = chip->client; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 530 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 531 | if (irq_base != -1 && (id->driver_data & PCA_INT)) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 532 | dev_warn(&client->dev, "interrupt support not compiled in\n"); |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static void pca953x_irq_teardown(struct pca953x_chip *chip) |
| 538 | { |
| 539 | } |
| 540 | #endif |
| 541 | |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 542 | /* |
| 543 | * Handlers for alternative sources of platform_data |
| 544 | */ |
| 545 | #ifdef CONFIG_OF_GPIO |
| 546 | /* |
| 547 | * Translate OpenFirmware node properties into platform_data |
| 548 | */ |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 549 | void |
| 550 | pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, int *invert) |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 551 | { |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 552 | struct device_node *node; |
Dirk Eibach | 1648237 | 2011-02-24 10:20:43 +0100 | [diff] [blame] | 553 | const __be32 *val; |
| 554 | int size; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 555 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 556 | node = client->dev.of_node; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 557 | if (node == NULL) |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 558 | return; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 559 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 560 | *gpio_base = -1; |
Dirk Eibach | 1648237 | 2011-02-24 10:20:43 +0100 | [diff] [blame] | 561 | val = of_get_property(node, "linux,gpio-base", &size); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 562 | if (val) { |
Dirk Eibach | 1648237 | 2011-02-24 10:20:43 +0100 | [diff] [blame] | 563 | if (size != sizeof(*val)) |
| 564 | dev_warn(&client->dev, "%s: wrong linux,gpio-base\n", |
| 565 | node->full_name); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 566 | else |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 567 | *gpio_base = be32_to_cpup(val); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | val = of_get_property(node, "polarity", NULL); |
| 571 | if (val) |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 572 | *invert = *val; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 573 | } |
| 574 | #else |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 575 | void |
| 576 | pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, int *invert) |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 577 | { |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 578 | } |
| 579 | #endif |
| 580 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 581 | static int __devinit device_pca953x_init(struct pca953x_chip *chip, int invert) |
| 582 | { |
| 583 | int ret; |
| 584 | |
| 585 | ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output); |
| 586 | if (ret) |
| 587 | goto out; |
| 588 | |
| 589 | ret = pca953x_read_reg(chip, PCA953X_DIRECTION, |
| 590 | &chip->reg_direction); |
| 591 | if (ret) |
| 592 | goto out; |
| 593 | |
| 594 | /* set platform specific polarity inversion */ |
| 595 | ret = pca953x_write_reg(chip, PCA953X_INVERT, invert); |
| 596 | if (ret) |
| 597 | goto out; |
| 598 | return 0; |
| 599 | out: |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | static int __devinit device_pca957x_init(struct pca953x_chip *chip, int invert) |
| 604 | { |
| 605 | int ret; |
| 606 | uint16_t val = 0; |
| 607 | |
| 608 | /* Let every port in proper state, that could save power */ |
| 609 | pca953x_write_reg(chip, PCA957X_PUPD, 0x0); |
| 610 | pca953x_write_reg(chip, PCA957X_CFG, 0xffff); |
| 611 | pca953x_write_reg(chip, PCA957X_OUT, 0x0); |
| 612 | |
| 613 | ret = pca953x_read_reg(chip, PCA957X_IN, &val); |
| 614 | if (ret) |
| 615 | goto out; |
| 616 | ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output); |
| 617 | if (ret) |
| 618 | goto out; |
| 619 | ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction); |
| 620 | if (ret) |
| 621 | goto out; |
| 622 | |
| 623 | /* set platform specific polarity inversion */ |
| 624 | pca953x_write_reg(chip, PCA957X_INVRT, invert); |
| 625 | |
| 626 | /* To enable register 6, 7 to controll pull up and pull down */ |
| 627 | pca953x_write_reg(chip, PCA957X_BKEN, 0x202); |
| 628 | |
| 629 | return 0; |
| 630 | out: |
| 631 | return ret; |
| 632 | } |
| 633 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 634 | static int __devinit pca953x_probe(struct i2c_client *client, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 635 | const struct i2c_device_id *id) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 636 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 637 | struct pca953x_platform_data *pdata; |
| 638 | struct pca953x_chip *chip; |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 639 | int irq_base=-1, invert=0; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 640 | int ret = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 641 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 642 | chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 643 | if (chip == NULL) |
| 644 | return -ENOMEM; |
| 645 | |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 646 | pdata = client->dev.platform_data; |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 647 | if (pdata) { |
| 648 | irq_base = pdata->irq_base; |
| 649 | chip->gpio_start = pdata->gpio_base; |
| 650 | invert = pdata->invert; |
| 651 | chip->names = pdata->names; |
| 652 | } else { |
| 653 | pca953x_get_alt_pdata(client, &chip->gpio_start, &invert); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 654 | } |
| 655 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 656 | chip->client = client; |
| 657 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 658 | chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE); |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 659 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 660 | mutex_init(&chip->i2c_lock); |
| 661 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 662 | /* initialize cached registers from their original values. |
| 663 | * we can't share this chip with another i2c master. |
| 664 | */ |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 665 | pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK); |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 666 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 667 | if (chip->chip_type == PCA953X_TYPE) |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 668 | device_pca953x_init(chip, invert); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 669 | else if (chip->chip_type == PCA957X_TYPE) |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 670 | device_pca957x_init(chip, invert); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 671 | else |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 672 | goto out_failed; |
| 673 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 674 | ret = pca953x_irq_setup(chip, id, irq_base); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 675 | if (ret) |
| 676 | goto out_failed; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 677 | |
| 678 | ret = gpiochip_add(&chip->gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 679 | if (ret) |
Ben Dooks | 272df50 | 2011-03-31 07:18:46 +0900 | [diff] [blame] | 680 | goto out_failed_irq; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 681 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 682 | if (pdata && pdata->setup) { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 683 | ret = pdata->setup(client, chip->gpio_chip.base, |
| 684 | chip->gpio_chip.ngpio, pdata->context); |
| 685 | if (ret < 0) |
| 686 | dev_warn(&client->dev, "setup failed, %d\n", ret); |
| 687 | } |
| 688 | |
| 689 | i2c_set_clientdata(client, chip); |
| 690 | return 0; |
| 691 | |
Ben Dooks | 272df50 | 2011-03-31 07:18:46 +0900 | [diff] [blame] | 692 | out_failed_irq: |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 693 | pca953x_irq_teardown(chip); |
Ben Dooks | 272df50 | 2011-03-31 07:18:46 +0900 | [diff] [blame] | 694 | out_failed: |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 695 | kfree(chip); |
| 696 | return ret; |
| 697 | } |
| 698 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 699 | static int pca953x_remove(struct i2c_client *client) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 700 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 701 | struct pca953x_platform_data *pdata = client->dev.platform_data; |
| 702 | struct pca953x_chip *chip = i2c_get_clientdata(client); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 703 | int ret = 0; |
| 704 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame^] | 705 | if (pdata && pdata->teardown) { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 706 | ret = pdata->teardown(client, chip->gpio_chip.base, |
| 707 | chip->gpio_chip.ngpio, pdata->context); |
| 708 | if (ret < 0) { |
| 709 | dev_err(&client->dev, "%s failed, %d\n", |
| 710 | "teardown", ret); |
| 711 | return ret; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | ret = gpiochip_remove(&chip->gpio_chip); |
| 716 | if (ret) { |
| 717 | dev_err(&client->dev, "%s failed, %d\n", |
| 718 | "gpiochip_remove()", ret); |
| 719 | return ret; |
| 720 | } |
| 721 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 722 | pca953x_irq_teardown(chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 723 | kfree(chip); |
| 724 | return 0; |
| 725 | } |
| 726 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 727 | static struct i2c_driver pca953x_driver = { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 728 | .driver = { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 729 | .name = "pca953x", |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 730 | }, |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 731 | .probe = pca953x_probe, |
| 732 | .remove = pca953x_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 733 | .id_table = pca953x_id, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 734 | }; |
| 735 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 736 | static int __init pca953x_init(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 737 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 738 | return i2c_add_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 739 | } |
David Brownell | 2f8d119 | 2008-10-15 22:03:13 -0700 | [diff] [blame] | 740 | /* register after i2c postcore initcall and before |
| 741 | * subsys initcalls that may rely on these GPIOs |
| 742 | */ |
| 743 | subsys_initcall(pca953x_init); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 744 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 745 | static void __exit pca953x_exit(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 746 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 747 | i2c_del_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 748 | } |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 749 | module_exit(pca953x_exit); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 750 | |
| 751 | MODULE_AUTHOR("eric miao <eric.miao@marvell.com>"); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 752 | MODULE_DESCRIPTION("GPIO expander driver for PCA953x"); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 753 | MODULE_LICENSE("GPL"); |