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