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