eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 1 | /* |
Aaron Sierra | 1e19169 | 2014-02-07 16:35:48 -0600 | [diff] [blame] | 2 | * PCA953x 4/8/16/24/40 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> |
Maxime Ripard | 55ecd26 | 2012-11-08 18:01:51 +0100 | [diff] [blame] | 19 | #include <linux/irqdomain.h> |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 20 | #include <linux/i2c.h> |
Vivien Didelot | 5877457 | 2013-08-29 15:24:14 -0400 | [diff] [blame] | 21 | #include <linux/platform_data/pca953x.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 23 | #ifdef CONFIG_OF_GPIO |
| 24 | #include <linux/of_platform.h> |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 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 | |
Andreas Schallenberg | ae79c19 | 2012-05-09 09:46:17 +0200 | [diff] [blame] | 32 | #define REG_ADDR_AI 0x80 |
| 33 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 34 | #define PCA957X_IN 0 |
| 35 | #define PCA957X_INVRT 1 |
| 36 | #define PCA957X_BKEN 2 |
| 37 | #define PCA957X_PUPD 3 |
| 38 | #define PCA957X_CFG 4 |
| 39 | #define PCA957X_OUT 5 |
| 40 | #define PCA957X_MSK 6 |
| 41 | #define PCA957X_INTS 7 |
| 42 | |
| 43 | #define PCA_GPIO_MASK 0x00FF |
| 44 | #define PCA_INT 0x0100 |
| 45 | #define PCA953X_TYPE 0x1000 |
| 46 | #define PCA957X_TYPE 0x2000 |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 47 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 48 | static const struct i2c_device_id pca953x_id[] = { |
Gregory CLEMENT | 89f5df0 | 2013-01-22 22:10:24 +0100 | [diff] [blame] | 49 | { "pca9505", 40 | PCA953X_TYPE | PCA_INT, }, |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 50 | { "pca9534", 8 | PCA953X_TYPE | PCA_INT, }, |
| 51 | { "pca9535", 16 | PCA953X_TYPE | PCA_INT, }, |
| 52 | { "pca9536", 4 | PCA953X_TYPE, }, |
| 53 | { "pca9537", 4 | PCA953X_TYPE | PCA_INT, }, |
| 54 | { "pca9538", 8 | PCA953X_TYPE | PCA_INT, }, |
| 55 | { "pca9539", 16 | PCA953X_TYPE | PCA_INT, }, |
| 56 | { "pca9554", 8 | PCA953X_TYPE | PCA_INT, }, |
| 57 | { "pca9555", 16 | PCA953X_TYPE | PCA_INT, }, |
| 58 | { "pca9556", 8 | PCA953X_TYPE, }, |
| 59 | { "pca9557", 8 | PCA953X_TYPE, }, |
| 60 | { "pca9574", 8 | PCA957X_TYPE | PCA_INT, }, |
| 61 | { "pca9575", 16 | PCA957X_TYPE | PCA_INT, }, |
Aaron Sierra | eb32b5a | 2014-02-13 13:59:23 +0100 | [diff] [blame] | 62 | { "pca9698", 40 | PCA953X_TYPE, }, |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 63 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 64 | { "max7310", 8 | PCA953X_TYPE, }, |
| 65 | { "max7312", 16 | PCA953X_TYPE | PCA_INT, }, |
| 66 | { "max7313", 16 | PCA953X_TYPE | PCA_INT, }, |
| 67 | { "max7315", 8 | PCA953X_TYPE | PCA_INT, }, |
| 68 | { "pca6107", 8 | PCA953X_TYPE | PCA_INT, }, |
| 69 | { "tca6408", 8 | PCA953X_TYPE | PCA_INT, }, |
| 70 | { "tca6416", 16 | PCA953X_TYPE | PCA_INT, }, |
Andreas Schallenberg | ae79c19 | 2012-05-09 09:46:17 +0200 | [diff] [blame] | 71 | { "tca6424", 24 | PCA953X_TYPE | PCA_INT, }, |
Aaron Sierra | e73760a | 2014-02-07 16:36:21 -0600 | [diff] [blame^] | 72 | { "xra1202", 8 | PCA953X_TYPE }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 73 | { } |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 74 | }; |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 75 | MODULE_DEVICE_TABLE(i2c, pca953x_id); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 76 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 77 | #define MAX_BANK 5 |
| 78 | #define BANK_SZ 8 |
| 79 | |
| 80 | #define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ) |
| 81 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 82 | struct pca953x_chip { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 83 | unsigned gpio_start; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 84 | u8 reg_output[MAX_BANK]; |
| 85 | u8 reg_direction[MAX_BANK]; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 86 | struct mutex i2c_lock; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 87 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 88 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
| 89 | struct mutex irq_lock; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 90 | u8 irq_mask[MAX_BANK]; |
| 91 | u8 irq_stat[MAX_BANK]; |
| 92 | u8 irq_trig_raise[MAX_BANK]; |
| 93 | u8 irq_trig_fall[MAX_BANK]; |
Maxime Ripard | 55ecd26 | 2012-11-08 18:01:51 +0100 | [diff] [blame] | 94 | struct irq_domain *domain; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 95 | #endif |
| 96 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 97 | struct i2c_client *client; |
| 98 | struct gpio_chip gpio_chip; |
Uwe Kleine-König | 6215499 | 2010-05-26 14:42:17 -0700 | [diff] [blame] | 99 | const char *const *names; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 100 | int chip_type; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 103 | static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val, |
| 104 | int off) |
| 105 | { |
| 106 | int ret; |
| 107 | int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
| 108 | int offset = off / BANK_SZ; |
| 109 | |
| 110 | ret = i2c_smbus_read_byte_data(chip->client, |
| 111 | (reg << bank_shift) + offset); |
| 112 | *val = ret; |
| 113 | |
| 114 | if (ret < 0) { |
| 115 | dev_err(&chip->client->dev, "failed reading register\n"); |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val, |
| 123 | int off) |
| 124 | { |
| 125 | int ret = 0; |
| 126 | int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
| 127 | int offset = off / BANK_SZ; |
| 128 | |
| 129 | ret = i2c_smbus_write_byte_data(chip->client, |
| 130 | (reg << bank_shift) + offset, val); |
| 131 | |
| 132 | if (ret < 0) { |
| 133 | dev_err(&chip->client->dev, "failed writing register\n"); |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 141 | { |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 142 | int ret = 0; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 143 | |
| 144 | if (chip->gpio_chip.ngpio <= 8) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 145 | ret = i2c_smbus_write_byte_data(chip->client, reg, *val); |
| 146 | else if (chip->gpio_chip.ngpio >= 24) { |
| 147 | int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 148 | ret = i2c_smbus_write_i2c_block_data(chip->client, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 149 | (reg << bank_shift) | REG_ADDR_AI, |
| 150 | NBANK(chip), val); |
Laurent Navet | 50e4443 | 2013-03-20 13:15:58 +0100 | [diff] [blame] | 151 | } else { |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 152 | switch (chip->chip_type) { |
| 153 | case PCA953X_TYPE: |
| 154 | ret = i2c_smbus_write_word_data(chip->client, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 155 | reg << 1, (u16) *val); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 156 | break; |
| 157 | case PCA957X_TYPE: |
| 158 | ret = i2c_smbus_write_byte_data(chip->client, reg << 1, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 159 | val[0]); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 160 | if (ret < 0) |
| 161 | break; |
| 162 | ret = i2c_smbus_write_byte_data(chip->client, |
| 163 | (reg << 1) + 1, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 164 | val[1]); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | } |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 168 | |
| 169 | if (ret < 0) { |
| 170 | dev_err(&chip->client->dev, "failed writing register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 171 | return ret; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | return 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 177 | static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 178 | { |
| 179 | int ret; |
| 180 | |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 181 | if (chip->gpio_chip.ngpio <= 8) { |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 182 | ret = i2c_smbus_read_byte_data(chip->client, reg); |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 183 | *val = ret; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 184 | } else if (chip->gpio_chip.ngpio >= 24) { |
| 185 | int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); |
| 186 | |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 187 | ret = i2c_smbus_read_i2c_block_data(chip->client, |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 188 | (reg << bank_shift) | REG_ADDR_AI, |
| 189 | NBANK(chip), val); |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 190 | } else { |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 191 | ret = i2c_smbus_read_word_data(chip->client, reg << 1); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 192 | val[0] = (u16)ret & 0xFF; |
| 193 | val[1] = (u16)ret >> 8; |
Andreas Schallenberg | 96b7064 | 2012-05-21 09:52:43 +0200 | [diff] [blame] | 194 | } |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 195 | if (ret < 0) { |
| 196 | dev_err(&chip->client->dev, "failed reading register\n"); |
David Brownell | ab5dc37 | 2009-01-06 14:42:27 -0800 | [diff] [blame] | 197 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 198 | } |
| 199 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 203 | static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 204 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 205 | struct pca953x_chip *chip; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 206 | u8 reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 207 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 208 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 209 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 210 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 211 | mutex_lock(&chip->i2c_lock); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 212 | reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ)); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 213 | |
| 214 | switch (chip->chip_type) { |
| 215 | case PCA953X_TYPE: |
| 216 | offset = PCA953X_DIRECTION; |
| 217 | break; |
| 218 | case PCA957X_TYPE: |
| 219 | offset = PCA957X_CFG; |
| 220 | break; |
| 221 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 222 | ret = pca953x_write_single(chip, offset, reg_val, off); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 223 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 224 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 225 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 226 | chip->reg_direction[off / BANK_SZ] = reg_val; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 227 | ret = 0; |
| 228 | exit: |
| 229 | mutex_unlock(&chip->i2c_lock); |
| 230 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 233 | static int pca953x_gpio_direction_output(struct gpio_chip *gc, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 234 | unsigned off, int val) |
| 235 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 236 | struct pca953x_chip *chip; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 237 | u8 reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 238 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 239 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 240 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 241 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 242 | mutex_lock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 243 | /* set output level */ |
| 244 | if (val) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 245 | reg_val = chip->reg_output[off / BANK_SZ] |
| 246 | | (1u << (off % BANK_SZ)); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 247 | else |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 248 | reg_val = chip->reg_output[off / BANK_SZ] |
| 249 | & ~(1u << (off % BANK_SZ)); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 250 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 251 | switch (chip->chip_type) { |
| 252 | case PCA953X_TYPE: |
| 253 | offset = PCA953X_OUTPUT; |
| 254 | break; |
| 255 | case PCA957X_TYPE: |
| 256 | offset = PCA957X_OUT; |
| 257 | break; |
| 258 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 259 | ret = pca953x_write_single(chip, offset, reg_val, off); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 260 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 261 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 262 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 263 | chip->reg_output[off / BANK_SZ] = reg_val; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 264 | |
| 265 | /* then direction */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 266 | reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ)); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 267 | switch (chip->chip_type) { |
| 268 | case PCA953X_TYPE: |
| 269 | offset = PCA953X_DIRECTION; |
| 270 | break; |
| 271 | case PCA957X_TYPE: |
| 272 | offset = PCA957X_CFG; |
| 273 | break; |
| 274 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 275 | ret = pca953x_write_single(chip, offset, reg_val, off); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 276 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 277 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 278 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 279 | chip->reg_direction[off / BANK_SZ] = reg_val; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 280 | ret = 0; |
| 281 | exit: |
| 282 | mutex_unlock(&chip->i2c_lock); |
| 283 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 286 | static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 287 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 288 | struct pca953x_chip *chip; |
Andreas Schallenberg | ae79c19 | 2012-05-09 09:46:17 +0200 | [diff] [blame] | 289 | u32 reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 290 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 291 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 292 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 293 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 294 | mutex_lock(&chip->i2c_lock); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 295 | switch (chip->chip_type) { |
| 296 | case PCA953X_TYPE: |
| 297 | offset = PCA953X_INPUT; |
| 298 | break; |
| 299 | case PCA957X_TYPE: |
| 300 | offset = PCA957X_IN; |
| 301 | break; |
| 302 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 303 | ret = pca953x_read_single(chip, offset, ®_val, off); |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 304 | mutex_unlock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 305 | if (ret < 0) { |
| 306 | /* NOTE: diagnostic already emitted; that's all we should |
| 307 | * do unless gpio_*_value_cansleep() calls become different |
| 308 | * from their nonsleeping siblings (and report faults). |
| 309 | */ |
| 310 | return 0; |
| 311 | } |
| 312 | |
Andrew Ruder | 40a625d | 2013-08-07 16:52:40 -0500 | [diff] [blame] | 313 | return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 316 | 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] | 317 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 318 | struct pca953x_chip *chip; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 319 | u8 reg_val; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 320 | int ret, offset = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 321 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 322 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 323 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 324 | mutex_lock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 325 | if (val) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 326 | reg_val = chip->reg_output[off / BANK_SZ] |
| 327 | | (1u << (off % BANK_SZ)); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 328 | else |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 329 | reg_val = chip->reg_output[off / BANK_SZ] |
| 330 | & ~(1u << (off % BANK_SZ)); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 331 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 332 | switch (chip->chip_type) { |
| 333 | case PCA953X_TYPE: |
| 334 | offset = PCA953X_OUTPUT; |
| 335 | break; |
| 336 | case PCA957X_TYPE: |
| 337 | offset = PCA957X_OUT; |
| 338 | break; |
| 339 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 340 | ret = pca953x_write_single(chip, offset, reg_val, off); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 341 | if (ret) |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 342 | goto exit; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 343 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 344 | chip->reg_output[off / BANK_SZ] = reg_val; |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 345 | exit: |
| 346 | mutex_unlock(&chip->i2c_lock); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 349 | static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 350 | { |
| 351 | struct gpio_chip *gc; |
| 352 | |
| 353 | gc = &chip->gpio_chip; |
| 354 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 355 | gc->direction_input = pca953x_gpio_direction_input; |
| 356 | gc->direction_output = pca953x_gpio_direction_output; |
| 357 | gc->get = pca953x_gpio_get_value; |
| 358 | gc->set = pca953x_gpio_set_value; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 359 | gc->can_sleep = true; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 360 | |
| 361 | gc->base = chip->gpio_start; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 362 | gc->ngpio = gpios; |
| 363 | gc->label = chip->client->name; |
David Brownell | d8f388d8 | 2008-07-25 01:46:07 -0700 | [diff] [blame] | 364 | gc->dev = &chip->client->dev; |
Guennadi Liakhovetski | d72cbed | 2008-04-28 02:14:45 -0700 | [diff] [blame] | 365 | gc->owner = THIS_MODULE; |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 366 | gc->names = chip->names; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 369 | #ifdef CONFIG_GPIO_PCA953X_IRQ |
| 370 | static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off) |
| 371 | { |
| 372 | struct pca953x_chip *chip; |
| 373 | |
| 374 | chip = container_of(gc, struct pca953x_chip, gpio_chip); |
Gregory CLEMENT | 0e8f2fd | 2013-01-25 17:59:27 +0100 | [diff] [blame] | 375 | return irq_create_mapping(chip->domain, off); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 378 | static void pca953x_irq_mask(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 379 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 380 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 381 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 382 | chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ)); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 385 | static void pca953x_irq_unmask(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 386 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 387 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 388 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 389 | chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 392 | static void pca953x_irq_bus_lock(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 393 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 394 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 395 | |
| 396 | mutex_lock(&chip->irq_lock); |
| 397 | } |
| 398 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 399 | static void pca953x_irq_bus_sync_unlock(struct irq_data *d) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 400 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 401 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 402 | u8 new_irqs; |
| 403 | int level, i; |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 404 | |
| 405 | /* Look for any newly setup interrupt */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 406 | for (i = 0; i < NBANK(chip); i++) { |
| 407 | new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i]; |
| 408 | new_irqs &= ~chip->reg_direction[i]; |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 409 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 410 | while (new_irqs) { |
| 411 | level = __ffs(new_irqs); |
| 412 | pca953x_gpio_direction_input(&chip->gpio_chip, |
| 413 | level + (BANK_SZ * i)); |
| 414 | new_irqs &= ~(1 << level); |
| 415 | } |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 416 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 417 | |
| 418 | mutex_unlock(&chip->irq_lock); |
| 419 | } |
| 420 | |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 421 | 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] | 422 | { |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 423 | struct pca953x_chip *chip = irq_data_get_irq_chip_data(d); |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 424 | int bank_nb = d->hwirq / BANK_SZ; |
| 425 | u8 mask = 1 << (d->hwirq % BANK_SZ); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 426 | |
| 427 | if (!(type & IRQ_TYPE_EDGE_BOTH)) { |
| 428 | dev_err(&chip->client->dev, "irq %d: unsupported type %d\n", |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 429 | d->irq, type); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 430 | return -EINVAL; |
| 431 | } |
| 432 | |
| 433 | if (type & IRQ_TYPE_EDGE_FALLING) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 434 | chip->irq_trig_fall[bank_nb] |= mask; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 435 | else |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 436 | chip->irq_trig_fall[bank_nb] &= ~mask; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 437 | |
| 438 | if (type & IRQ_TYPE_EDGE_RISING) |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 439 | chip->irq_trig_raise[bank_nb] |= mask; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 440 | else |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 441 | chip->irq_trig_raise[bank_nb] &= ~mask; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 442 | |
Marc Zyngier | a2cb9ae | 2010-04-27 13:13:07 -0700 | [diff] [blame] | 443 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static struct irq_chip pca953x_irq_chip = { |
| 447 | .name = "pca953x", |
Lennert Buytenhek | 6f5cfc0 | 2011-01-12 17:00:15 -0800 | [diff] [blame] | 448 | .irq_mask = pca953x_irq_mask, |
| 449 | .irq_unmask = pca953x_irq_unmask, |
| 450 | .irq_bus_lock = pca953x_irq_bus_lock, |
| 451 | .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock, |
| 452 | .irq_set_type = pca953x_irq_set_type, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 453 | }; |
| 454 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 455 | static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 456 | { |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 457 | u8 cur_stat[MAX_BANK]; |
| 458 | u8 old_stat[MAX_BANK]; |
| 459 | u8 pendings = 0; |
| 460 | u8 trigger[MAX_BANK], triggers = 0; |
| 461 | int ret, i, offset = 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 462 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 463 | switch (chip->chip_type) { |
| 464 | case PCA953X_TYPE: |
| 465 | offset = PCA953X_INPUT; |
| 466 | break; |
| 467 | case PCA957X_TYPE: |
| 468 | offset = PCA957X_IN; |
| 469 | break; |
| 470 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 471 | ret = pca953x_read_regs(chip, offset, cur_stat); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 472 | if (ret) |
| 473 | return 0; |
| 474 | |
| 475 | /* Remove output pins from the equation */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 476 | for (i = 0; i < NBANK(chip); i++) |
| 477 | cur_stat[i] &= chip->reg_direction[i]; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 478 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 479 | memcpy(old_stat, chip->irq_stat, NBANK(chip)); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 480 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 481 | for (i = 0; i < NBANK(chip); i++) { |
| 482 | trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i]; |
| 483 | triggers += trigger[i]; |
| 484 | } |
| 485 | |
| 486 | if (!triggers) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 487 | return 0; |
| 488 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 489 | memcpy(chip->irq_stat, cur_stat, NBANK(chip)); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 490 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 491 | for (i = 0; i < NBANK(chip); i++) { |
| 492 | pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) | |
| 493 | (cur_stat[i] & chip->irq_trig_raise[i]); |
| 494 | pending[i] &= trigger[i]; |
| 495 | pendings += pending[i]; |
| 496 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 497 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 498 | return pendings; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static irqreturn_t pca953x_irq_handler(int irq, void *devid) |
| 502 | { |
| 503 | struct pca953x_chip *chip = devid; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 504 | u8 pending[MAX_BANK]; |
| 505 | u8 level; |
| 506 | int i; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 507 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 508 | if (!pca953x_irq_pending(chip, pending)) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 509 | return IRQ_HANDLED; |
| 510 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 511 | for (i = 0; i < NBANK(chip); i++) { |
| 512 | while (pending[i]) { |
| 513 | level = __ffs(pending[i]); |
| 514 | handle_nested_irq(irq_find_mapping(chip->domain, |
| 515 | level + (BANK_SZ * i))); |
| 516 | pending[i] &= ~(1 << level); |
| 517 | } |
| 518 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 519 | |
| 520 | return IRQ_HANDLED; |
| 521 | } |
| 522 | |
Gregory CLEMENT | 0e8f2fd | 2013-01-25 17:59:27 +0100 | [diff] [blame] | 523 | static int pca953x_gpio_irq_map(struct irq_domain *d, unsigned int irq, |
| 524 | irq_hw_number_t hwirq) |
| 525 | { |
| 526 | irq_clear_status_flags(irq, IRQ_NOREQUEST); |
| 527 | irq_set_chip_data(irq, d->host_data); |
| 528 | irq_set_chip(irq, &pca953x_irq_chip); |
| 529 | irq_set_nested_thread(irq, true); |
| 530 | #ifdef CONFIG_ARM |
| 531 | set_irq_flags(irq, IRQF_VALID); |
| 532 | #else |
| 533 | irq_set_noprobe(irq); |
| 534 | #endif |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static const struct irq_domain_ops pca953x_irq_simple_ops = { |
| 540 | .map = pca953x_gpio_irq_map, |
| 541 | .xlate = irq_domain_xlate_twocell, |
| 542 | }; |
| 543 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 544 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 545 | const struct i2c_device_id *id, |
| 546 | int irq_base) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 547 | { |
| 548 | struct i2c_client *client = chip->client; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 549 | int ret, i, offset = 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 550 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 551 | if (irq_base != -1 |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 552 | && (id->driver_data & PCA_INT)) { |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 553 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 554 | switch (chip->chip_type) { |
| 555 | case PCA953X_TYPE: |
| 556 | offset = PCA953X_INPUT; |
| 557 | break; |
| 558 | case PCA957X_TYPE: |
| 559 | offset = PCA957X_IN; |
| 560 | break; |
| 561 | } |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 562 | ret = pca953x_read_regs(chip, offset, chip->irq_stat); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 563 | if (ret) |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 564 | return ret; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 565 | |
| 566 | /* |
| 567 | * There is no way to know which GPIO line generated the |
| 568 | * interrupt. We have to rely on the previous read for |
| 569 | * this purpose. |
| 570 | */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 571 | for (i = 0; i < NBANK(chip); i++) |
| 572 | chip->irq_stat[i] &= chip->reg_direction[i]; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 573 | mutex_init(&chip->irq_lock); |
| 574 | |
Gregory CLEMENT | 0e8f2fd | 2013-01-25 17:59:27 +0100 | [diff] [blame] | 575 | chip->domain = irq_domain_add_simple(client->dev.of_node, |
Maxime Ripard | 55ecd26 | 2012-11-08 18:01:51 +0100 | [diff] [blame] | 576 | chip->gpio_chip.ngpio, |
Gregory CLEMENT | 0e8f2fd | 2013-01-25 17:59:27 +0100 | [diff] [blame] | 577 | irq_base, |
| 578 | &pca953x_irq_simple_ops, |
Alexandre Belloni | e7a7f97 | 2013-04-03 19:40:14 +0200 | [diff] [blame] | 579 | chip); |
Gregory CLEMENT | 0e8f2fd | 2013-01-25 17:59:27 +0100 | [diff] [blame] | 580 | if (!chip->domain) |
| 581 | return -ENODEV; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 582 | |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 583 | ret = devm_request_threaded_irq(&client->dev, |
| 584 | client->irq, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 585 | NULL, |
| 586 | pca953x_irq_handler, |
David Jander | 17e8b42 | 2011-06-08 11:34:41 -0600 | [diff] [blame] | 587 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 588 | dev_name(&client->dev), chip); |
| 589 | if (ret) { |
| 590 | dev_err(&client->dev, "failed to request irq %d\n", |
| 591 | client->irq); |
Gregory CLEMENT | 0e8f2fd | 2013-01-25 17:59:27 +0100 | [diff] [blame] | 592 | return ret; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | chip->gpio_chip.to_irq = pca953x_gpio_to_irq; |
| 596 | } |
| 597 | |
| 598 | return 0; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 601 | #else /* CONFIG_GPIO_PCA953X_IRQ */ |
| 602 | static int pca953x_irq_setup(struct pca953x_chip *chip, |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 603 | const struct i2c_device_id *id, |
| 604 | int irq_base) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 605 | { |
| 606 | struct i2c_client *client = chip->client; |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 607 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 608 | if (irq_base != -1 && (id->driver_data & PCA_INT)) |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 609 | dev_warn(&client->dev, "interrupt support not compiled in\n"); |
| 610 | |
| 611 | return 0; |
| 612 | } |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 613 | #endif |
| 614 | |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 615 | /* |
| 616 | * Handlers for alternative sources of platform_data |
| 617 | */ |
| 618 | #ifdef CONFIG_OF_GPIO |
| 619 | /* |
| 620 | * Translate OpenFirmware node properties into platform_data |
David Jander | a57339b | 2011-06-14 11:00:56 +0200 | [diff] [blame] | 621 | * WARNING: This is DEPRECATED and will be removed eventually! |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 622 | */ |
Mark Brown | 404ba2b | 2011-11-21 17:57:25 +0000 | [diff] [blame] | 623 | static void |
Chandrabhanu Mahapatra | 6a7b36a | 2012-07-10 19:05:37 +0530 | [diff] [blame] | 624 | pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert) |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 625 | { |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 626 | struct device_node *node; |
Dirk Eibach | 1648237 | 2011-02-24 10:20:43 +0100 | [diff] [blame] | 627 | const __be32 *val; |
| 628 | int size; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 629 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 630 | node = client->dev.of_node; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 631 | if (node == NULL) |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 632 | return; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 633 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 634 | *gpio_base = -1; |
Dirk Eibach | 1648237 | 2011-02-24 10:20:43 +0100 | [diff] [blame] | 635 | val = of_get_property(node, "linux,gpio-base", &size); |
David Jander | a57339b | 2011-06-14 11:00:56 +0200 | [diff] [blame] | 636 | WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 637 | if (val) { |
Dirk Eibach | 1648237 | 2011-02-24 10:20:43 +0100 | [diff] [blame] | 638 | if (size != sizeof(*val)) |
| 639 | dev_warn(&client->dev, "%s: wrong linux,gpio-base\n", |
| 640 | node->full_name); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 641 | else |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 642 | *gpio_base = be32_to_cpup(val); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | val = of_get_property(node, "polarity", NULL); |
David Jander | a57339b | 2011-06-14 11:00:56 +0200 | [diff] [blame] | 646 | WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__); |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 647 | if (val) |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 648 | *invert = *val; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 649 | } |
| 650 | #else |
Mark Brown | 404ba2b | 2011-11-21 17:57:25 +0000 | [diff] [blame] | 651 | static void |
Chandrabhanu Mahapatra | 6a7b36a | 2012-07-10 19:05:37 +0530 | [diff] [blame] | 652 | pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert) |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 653 | { |
Hartmut Knaack | 25fcf2b | 2011-10-11 00:22:45 +0200 | [diff] [blame] | 654 | *gpio_base = -1; |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 655 | } |
| 656 | #endif |
| 657 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 658 | static int device_pca953x_init(struct pca953x_chip *chip, u32 invert) |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 659 | { |
| 660 | int ret; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 661 | u8 val[MAX_BANK]; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 662 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 663 | ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 664 | if (ret) |
| 665 | goto out; |
| 666 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 667 | ret = pca953x_read_regs(chip, PCA953X_DIRECTION, |
| 668 | chip->reg_direction); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 669 | if (ret) |
| 670 | goto out; |
| 671 | |
| 672 | /* set platform specific polarity inversion */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 673 | if (invert) |
| 674 | memset(val, 0xFF, NBANK(chip)); |
| 675 | else |
| 676 | memset(val, 0, NBANK(chip)); |
| 677 | |
| 678 | ret = pca953x_write_regs(chip, PCA953X_INVERT, val); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 679 | out: |
| 680 | return ret; |
| 681 | } |
| 682 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 683 | static int device_pca957x_init(struct pca953x_chip *chip, u32 invert) |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 684 | { |
| 685 | int ret; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 686 | u8 val[MAX_BANK]; |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 687 | |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 688 | ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 689 | if (ret) |
| 690 | goto out; |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 691 | ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 692 | if (ret) |
| 693 | goto out; |
| 694 | |
| 695 | /* set platform specific polarity inversion */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 696 | if (invert) |
| 697 | memset(val, 0xFF, NBANK(chip)); |
| 698 | else |
| 699 | memset(val, 0, NBANK(chip)); |
| 700 | pca953x_write_regs(chip, PCA957X_INVRT, val); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 701 | |
| 702 | /* To enable register 6, 7 to controll pull up and pull down */ |
Gregory CLEMENT | f5f0b7a | 2013-01-22 22:10:23 +0100 | [diff] [blame] | 703 | memset(val, 0x02, NBANK(chip)); |
| 704 | pca953x_write_regs(chip, PCA957X_BKEN, val); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 705 | |
| 706 | return 0; |
| 707 | out: |
| 708 | return ret; |
| 709 | } |
| 710 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 711 | static int pca953x_probe(struct i2c_client *client, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 712 | const struct i2c_device_id *id) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 713 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 714 | struct pca953x_platform_data *pdata; |
| 715 | struct pca953x_chip *chip; |
Chandrabhanu Mahapatra | 6a7b36a | 2012-07-10 19:05:37 +0530 | [diff] [blame] | 716 | int irq_base = 0; |
Wolfram Sang | 7ea2aa2 | 2011-10-14 15:32:00 +0200 | [diff] [blame] | 717 | int ret; |
Chandrabhanu Mahapatra | 6a7b36a | 2012-07-10 19:05:37 +0530 | [diff] [blame] | 718 | u32 invert = 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 719 | |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 720 | chip = devm_kzalloc(&client->dev, |
| 721 | sizeof(struct pca953x_chip), GFP_KERNEL); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 722 | if (chip == NULL) |
| 723 | return -ENOMEM; |
| 724 | |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 725 | pdata = dev_get_platdata(&client->dev); |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 726 | if (pdata) { |
| 727 | irq_base = pdata->irq_base; |
| 728 | chip->gpio_start = pdata->gpio_base; |
| 729 | invert = pdata->invert; |
| 730 | chip->names = pdata->names; |
| 731 | } else { |
| 732 | pca953x_get_alt_pdata(client, &chip->gpio_start, &invert); |
David Jander | a57339b | 2011-06-14 11:00:56 +0200 | [diff] [blame] | 733 | #ifdef CONFIG_OF_GPIO |
| 734 | /* If I2C node has no interrupts property, disable GPIO interrupts */ |
| 735 | if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL) |
| 736 | irq_base = -1; |
| 737 | #endif |
Nate Case | 1965d30 | 2009-06-17 16:26:17 -0700 | [diff] [blame] | 738 | } |
| 739 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 740 | chip->client = client; |
| 741 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 742 | chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE); |
Daniel Silverstone | 77906a54 | 2009-06-17 16:26:15 -0700 | [diff] [blame] | 743 | |
Roland Stigge | 6e20fb1 | 2011-02-10 15:01:23 -0800 | [diff] [blame] | 744 | mutex_init(&chip->i2c_lock); |
| 745 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 746 | /* initialize cached registers from their original values. |
| 747 | * we can't share this chip with another i2c master. |
| 748 | */ |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 749 | pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK); |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 750 | |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 751 | if (chip->chip_type == PCA953X_TYPE) |
Wolfram Sang | 7ea2aa2 | 2011-10-14 15:32:00 +0200 | [diff] [blame] | 752 | ret = device_pca953x_init(chip, invert); |
Haojian Zhuang | 33226ff | 2011-04-18 22:12:46 +0800 | [diff] [blame] | 753 | else |
Wolfram Sang | 7ea2aa2 | 2011-10-14 15:32:00 +0200 | [diff] [blame] | 754 | ret = device_pca957x_init(chip, invert); |
| 755 | if (ret) |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 756 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 757 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 758 | ret = pca953x_irq_setup(chip, id, irq_base); |
Marc Zyngier | 89ea8bb | 2010-03-05 13:44:36 -0800 | [diff] [blame] | 759 | if (ret) |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 760 | return ret; |
Guennadi Liakhovetski | f5e8ff4 | 2008-02-06 01:39:04 -0800 | [diff] [blame] | 761 | |
| 762 | ret = gpiochip_add(&chip->gpio_chip); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 763 | if (ret) |
Linus Walleij | b42748c | 2013-01-25 17:59:28 +0100 | [diff] [blame] | 764 | return ret; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 765 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 766 | if (pdata && pdata->setup) { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 767 | ret = pdata->setup(client, chip->gpio_chip.base, |
| 768 | chip->gpio_chip.ngpio, pdata->context); |
| 769 | if (ret < 0) |
| 770 | dev_warn(&client->dev, "setup failed, %d\n", ret); |
| 771 | } |
| 772 | |
| 773 | i2c_set_clientdata(client, chip); |
| 774 | return 0; |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 777 | static int pca953x_remove(struct i2c_client *client) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 778 | { |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 779 | struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 780 | struct pca953x_chip *chip = i2c_get_clientdata(client); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 781 | int ret = 0; |
| 782 | |
David Jander | c6dcf59 | 2011-06-14 11:00:55 +0200 | [diff] [blame] | 783 | if (pdata && pdata->teardown) { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 784 | ret = pdata->teardown(client, chip->gpio_chip.base, |
| 785 | chip->gpio_chip.ngpio, pdata->context); |
| 786 | if (ret < 0) { |
| 787 | dev_err(&client->dev, "%s failed, %d\n", |
| 788 | "teardown", ret); |
| 789 | return ret; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | ret = gpiochip_remove(&chip->gpio_chip); |
| 794 | if (ret) { |
| 795 | dev_err(&client->dev, "%s failed, %d\n", |
| 796 | "gpiochip_remove()", ret); |
| 797 | return ret; |
| 798 | } |
| 799 | |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 800 | return 0; |
| 801 | } |
| 802 | |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 803 | static const struct of_device_id pca953x_dt_ids[] = { |
Gregory CLEMENT | 89f5df0 | 2013-01-22 22:10:24 +0100 | [diff] [blame] | 804 | { .compatible = "nxp,pca9505", }, |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 805 | { .compatible = "nxp,pca9534", }, |
| 806 | { .compatible = "nxp,pca9535", }, |
| 807 | { .compatible = "nxp,pca9536", }, |
| 808 | { .compatible = "nxp,pca9537", }, |
| 809 | { .compatible = "nxp,pca9538", }, |
| 810 | { .compatible = "nxp,pca9539", }, |
| 811 | { .compatible = "nxp,pca9554", }, |
| 812 | { .compatible = "nxp,pca9555", }, |
| 813 | { .compatible = "nxp,pca9556", }, |
| 814 | { .compatible = "nxp,pca9557", }, |
| 815 | { .compatible = "nxp,pca9574", }, |
| 816 | { .compatible = "nxp,pca9575", }, |
Aaron Sierra | eb32b5a | 2014-02-13 13:59:23 +0100 | [diff] [blame] | 817 | { .compatible = "nxp,pca9698", }, |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 818 | |
| 819 | { .compatible = "maxim,max7310", }, |
| 820 | { .compatible = "maxim,max7312", }, |
| 821 | { .compatible = "maxim,max7313", }, |
| 822 | { .compatible = "maxim,max7315", }, |
| 823 | |
| 824 | { .compatible = "ti,pca6107", }, |
| 825 | { .compatible = "ti,tca6408", }, |
| 826 | { .compatible = "ti,tca6416", }, |
| 827 | { .compatible = "ti,tca6424", }, |
Aaron Sierra | e73760a | 2014-02-07 16:36:21 -0600 | [diff] [blame^] | 828 | |
| 829 | { .compatible = "exar,xra1202", }, |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 830 | { } |
| 831 | }; |
| 832 | |
| 833 | MODULE_DEVICE_TABLE(of, pca953x_dt_ids); |
| 834 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 835 | static struct i2c_driver pca953x_driver = { |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 836 | .driver = { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 837 | .name = "pca953x", |
Maxime Ripard | ed32620 | 2012-11-08 18:01:52 +0100 | [diff] [blame] | 838 | .of_match_table = pca953x_dt_ids, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 839 | }, |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 840 | .probe = pca953x_probe, |
| 841 | .remove = pca953x_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 842 | .id_table = pca953x_id, |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 843 | }; |
| 844 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 845 | static int __init pca953x_init(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 846 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 847 | return i2c_add_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 848 | } |
David Brownell | 2f8d119 | 2008-10-15 22:03:13 -0700 | [diff] [blame] | 849 | /* register after i2c postcore initcall and before |
| 850 | * subsys initcalls that may rely on these GPIOs |
| 851 | */ |
| 852 | subsys_initcall(pca953x_init); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 853 | |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 854 | static void __exit pca953x_exit(void) |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 855 | { |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 856 | i2c_del_driver(&pca953x_driver); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 857 | } |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 858 | module_exit(pca953x_exit); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 859 | |
| 860 | MODULE_AUTHOR("eric miao <eric.miao@marvell.com>"); |
Guennadi Liakhovetski | f3dc363 | 2008-02-06 01:39:03 -0800 | [diff] [blame] | 861 | MODULE_DESCRIPTION("GPIO expander driver for PCA953x"); |
eric miao | 9e60fdc | 2008-02-04 22:28:26 -0800 | [diff] [blame] | 862 | MODULE_LICENSE("GPL"); |