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