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