blob: 568ca96cdb6d2abf0479ca57be07e4a0feb1e3a3 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Sebastian Reicheld8f44942017-05-15 11:24:37 +02002/* MCP23S08 SPI/I2C GPIO driver */
David Brownelle58b9e22008-02-04 22:28:25 -08003
4#include <linux/kernel.h>
5#include <linux/device.h>
David Brownelle58b9e22008-02-04 22:28:25 -08006#include <linux/mutex.h>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -04007#include <linux/module.h>
Linus Walleij1c5fb662018-09-13 13:58:21 +02008#include <linux/gpio/driver.h>
Peter Korsgaard752ad5e2011-07-15 10:25:32 +02009#include <linux/i2c.h>
David Brownelle58b9e22008-02-04 22:28:25 -080010#include <linux/spi/spi.h>
11#include <linux/spi/mcp23s08.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010013#include <asm/byteorder.h>
Lars Poeschel4e47f912014-01-16 11:44:15 +010014#include <linux/interrupt.h>
Lars Poeschel97ddb1c2013-04-04 12:02:02 +020015#include <linux/of_device.h>
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010016#include <linux/regmap.h>
Sebastian Reichel82039d22017-05-15 11:24:26 +020017#include <linux/pinctrl/pinctrl.h>
18#include <linux/pinctrl/pinconf.h>
19#include <linux/pinctrl/pinconf-generic.h>
David Brownelle58b9e22008-02-04 22:28:25 -080020
Sebastian Reicheld8f44942017-05-15 11:24:37 +020021/*
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010022 * MCP types supported by driver
23 */
24#define MCP_TYPE_S08 0
25#define MCP_TYPE_S17 1
Peter Korsgaard752ad5e2011-07-15 10:25:32 +020026#define MCP_TYPE_008 2
27#define MCP_TYPE_017 3
Phil Reid28c5a412016-03-01 14:25:41 +080028#define MCP_TYPE_S18 4
Phil Reidff0f2ce2017-10-06 13:08:07 +080029#define MCP_TYPE_018 5
David Brownelle58b9e22008-02-04 22:28:25 -080030
Sebastian Reichelce9bd0a2017-05-15 11:24:36 +020031#define MCP_MAX_DEV_PER_CS 8
32
David Brownelle58b9e22008-02-04 22:28:25 -080033/* Registers are all 8 bits wide.
34 *
35 * The mcp23s17 has twice as many bits, and can be configured to work
36 * with either 16 bit registers or with two adjacent 8 bit banks.
David Brownelle58b9e22008-02-04 22:28:25 -080037 */
38#define MCP_IODIR 0x00 /* init/reset: all ones */
39#define MCP_IPOL 0x01
40#define MCP_GPINTEN 0x02
41#define MCP_DEFVAL 0x03
42#define MCP_INTCON 0x04
43#define MCP_IOCON 0x05
Lars Poeschel4e47f912014-01-16 11:44:15 +010044# define IOCON_MIRROR (1 << 6)
David Brownelle58b9e22008-02-04 22:28:25 -080045# define IOCON_SEQOP (1 << 5)
46# define IOCON_HAEN (1 << 3)
47# define IOCON_ODR (1 << 2)
48# define IOCON_INTPOL (1 << 1)
Phil Reid35396992016-03-15 15:46:30 +080049# define IOCON_INTCC (1)
David Brownelle58b9e22008-02-04 22:28:25 -080050#define MCP_GPPU 0x06
51#define MCP_INTF 0x07
52#define MCP_INTCAP 0x08
53#define MCP_GPIO 0x09
54#define MCP_OLAT 0x0a
55
Peter Korsgaard0b7bb772011-03-09 17:56:30 +010056struct mcp23s08;
57
David Brownelle58b9e22008-02-04 22:28:25 -080058struct mcp23s08 {
David Brownelle58b9e22008-02-04 22:28:25 -080059 u8 addr;
Alexander Steina4e63552014-12-01 08:26:00 +010060 bool irq_active_high;
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010061 bool reg_shift;
David Brownelle58b9e22008-02-04 22:28:25 -080062
Lars Poeschel4e47f912014-01-16 11:44:15 +010063 u16 irq_rise;
64 u16 irq_fall;
65 int irq;
66 bool irq_controller;
Sebastian Reichel8f389102017-05-15 11:24:28 +020067 int cached_gpio;
68 /* lock protects regmap access with bypass/cache flags */
David Brownelle58b9e22008-02-04 22:28:25 -080069 struct mutex lock;
David Brownelle58b9e22008-02-04 22:28:25 -080070
71 struct gpio_chip chip;
Lars Poeschel19ab5ca2019-01-11 17:25:16 +010072 struct irq_chip irq_chip;
David Brownelle58b9e22008-02-04 22:28:25 -080073
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +010074 struct regmap *regmap;
75 struct device *dev;
Sebastian Reichel82039d22017-05-15 11:24:26 +020076
77 struct pinctrl_dev *pctldev;
78 struct pinctrl_desc pinctrl_desc;
David Brownelle58b9e22008-02-04 22:28:25 -080079};
80
Sebastian Reichel8f389102017-05-15 11:24:28 +020081static const struct reg_default mcp23x08_defaults[] = {
82 {.reg = MCP_IODIR, .def = 0xff},
83 {.reg = MCP_IPOL, .def = 0x00},
84 {.reg = MCP_GPINTEN, .def = 0x00},
85 {.reg = MCP_DEFVAL, .def = 0x00},
86 {.reg = MCP_INTCON, .def = 0x00},
87 {.reg = MCP_IOCON, .def = 0x00},
88 {.reg = MCP_GPPU, .def = 0x00},
89 {.reg = MCP_OLAT, .def = 0x00},
90};
91
92static const struct regmap_range mcp23x08_volatile_range = {
93 .range_min = MCP_INTF,
94 .range_max = MCP_GPIO,
95};
96
97static const struct regmap_access_table mcp23x08_volatile_table = {
98 .yes_ranges = &mcp23x08_volatile_range,
99 .n_yes_ranges = 1,
100};
101
102static const struct regmap_range mcp23x08_precious_range = {
103 .range_min = MCP_GPIO,
104 .range_max = MCP_GPIO,
105};
106
107static const struct regmap_access_table mcp23x08_precious_table = {
108 .yes_ranges = &mcp23x08_precious_range,
109 .n_yes_ranges = 1,
110};
111
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100112static const struct regmap_config mcp23x08_regmap = {
113 .reg_bits = 8,
114 .val_bits = 8,
115
116 .reg_stride = 1,
Sebastian Reichel8f389102017-05-15 11:24:28 +0200117 .volatile_table = &mcp23x08_volatile_table,
118 .precious_table = &mcp23x08_precious_table,
119 .reg_defaults = mcp23x08_defaults,
120 .num_reg_defaults = ARRAY_SIZE(mcp23x08_defaults),
121 .cache_type = REGCACHE_FLAT,
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100122 .max_register = MCP_OLAT,
123};
124
Sebastian Reichel8f389102017-05-15 11:24:28 +0200125static const struct reg_default mcp23x16_defaults[] = {
126 {.reg = MCP_IODIR << 1, .def = 0xffff},
127 {.reg = MCP_IPOL << 1, .def = 0x0000},
128 {.reg = MCP_GPINTEN << 1, .def = 0x0000},
129 {.reg = MCP_DEFVAL << 1, .def = 0x0000},
130 {.reg = MCP_INTCON << 1, .def = 0x0000},
131 {.reg = MCP_IOCON << 1, .def = 0x0000},
132 {.reg = MCP_GPPU << 1, .def = 0x0000},
133 {.reg = MCP_OLAT << 1, .def = 0x0000},
134};
135
136static const struct regmap_range mcp23x16_volatile_range = {
137 .range_min = MCP_INTF << 1,
138 .range_max = MCP_GPIO << 1,
139};
140
141static const struct regmap_access_table mcp23x16_volatile_table = {
142 .yes_ranges = &mcp23x16_volatile_range,
143 .n_yes_ranges = 1,
144};
145
146static const struct regmap_range mcp23x16_precious_range = {
147 .range_min = MCP_GPIO << 1,
148 .range_max = MCP_GPIO << 1,
149};
150
151static const struct regmap_access_table mcp23x16_precious_table = {
152 .yes_ranges = &mcp23x16_precious_range,
153 .n_yes_ranges = 1,
154};
155
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100156static const struct regmap_config mcp23x17_regmap = {
157 .reg_bits = 8,
158 .val_bits = 16,
159
160 .reg_stride = 2,
161 .max_register = MCP_OLAT << 1,
Sebastian Reichel8f389102017-05-15 11:24:28 +0200162 .volatile_table = &mcp23x16_volatile_table,
163 .precious_table = &mcp23x16_precious_table,
164 .reg_defaults = mcp23x16_defaults,
165 .num_reg_defaults = ARRAY_SIZE(mcp23x16_defaults),
166 .cache_type = REGCACHE_FLAT,
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100167 .val_format_endian = REGMAP_ENDIAN_LITTLE,
168};
169
Sebastian Reichel82039d22017-05-15 11:24:26 +0200170static int mcp_read(struct mcp23s08 *mcp, unsigned int reg, unsigned int *val)
171{
172 return regmap_read(mcp->regmap, reg << mcp->reg_shift, val);
173}
174
175static int mcp_write(struct mcp23s08 *mcp, unsigned int reg, unsigned int val)
176{
177 return regmap_write(mcp->regmap, reg << mcp->reg_shift, val);
178}
179
Sebastian Reichel8f389102017-05-15 11:24:28 +0200180static int mcp_set_mask(struct mcp23s08 *mcp, unsigned int reg,
181 unsigned int mask, bool enabled)
Sebastian Reichel82039d22017-05-15 11:24:26 +0200182{
183 u16 val = enabled ? 0xffff : 0x0000;
Sebastian Reichel82039d22017-05-15 11:24:26 +0200184 return regmap_update_bits(mcp->regmap, reg << mcp->reg_shift,
185 mask, val);
186}
187
Sebastian Reichel8f389102017-05-15 11:24:28 +0200188static int mcp_set_bit(struct mcp23s08 *mcp, unsigned int reg,
189 unsigned int pin, bool enabled)
Sebastian Reichel82039d22017-05-15 11:24:26 +0200190{
Sebastian Reichel8f389102017-05-15 11:24:28 +0200191 u16 mask = BIT(pin);
192 return mcp_set_mask(mcp, reg, mask, enabled);
Sebastian Reichel82039d22017-05-15 11:24:26 +0200193}
194
195static const struct pinctrl_pin_desc mcp23x08_pins[] = {
196 PINCTRL_PIN(0, "gpio0"),
197 PINCTRL_PIN(1, "gpio1"),
198 PINCTRL_PIN(2, "gpio2"),
199 PINCTRL_PIN(3, "gpio3"),
200 PINCTRL_PIN(4, "gpio4"),
201 PINCTRL_PIN(5, "gpio5"),
202 PINCTRL_PIN(6, "gpio6"),
203 PINCTRL_PIN(7, "gpio7"),
204};
205
206static const struct pinctrl_pin_desc mcp23x17_pins[] = {
207 PINCTRL_PIN(0, "gpio0"),
208 PINCTRL_PIN(1, "gpio1"),
209 PINCTRL_PIN(2, "gpio2"),
210 PINCTRL_PIN(3, "gpio3"),
211 PINCTRL_PIN(4, "gpio4"),
212 PINCTRL_PIN(5, "gpio5"),
213 PINCTRL_PIN(6, "gpio6"),
214 PINCTRL_PIN(7, "gpio7"),
215 PINCTRL_PIN(8, "gpio8"),
216 PINCTRL_PIN(9, "gpio9"),
217 PINCTRL_PIN(10, "gpio10"),
218 PINCTRL_PIN(11, "gpio11"),
219 PINCTRL_PIN(12, "gpio12"),
220 PINCTRL_PIN(13, "gpio13"),
221 PINCTRL_PIN(14, "gpio14"),
222 PINCTRL_PIN(15, "gpio15"),
223};
224
225static int mcp_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
226{
227 return 0;
228}
229
230static const char *mcp_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
231 unsigned int group)
232{
233 return NULL;
234}
235
236static int mcp_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
237 unsigned int group,
238 const unsigned int **pins,
239 unsigned int *num_pins)
240{
241 return -ENOTSUPP;
242}
243
244static const struct pinctrl_ops mcp_pinctrl_ops = {
245 .get_groups_count = mcp_pinctrl_get_groups_count,
246 .get_group_name = mcp_pinctrl_get_group_name,
247 .get_group_pins = mcp_pinctrl_get_group_pins,
248#ifdef CONFIG_OF
249 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
250 .dt_free_map = pinconf_generic_dt_free_map,
251#endif
252};
253
254static int mcp_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
255 unsigned long *config)
256{
257 struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev);
258 enum pin_config_param param = pinconf_to_config_param(*config);
259 unsigned int data, status;
260 int ret;
261
262 switch (param) {
263 case PIN_CONFIG_BIAS_PULL_UP:
264 ret = mcp_read(mcp, MCP_GPPU, &data);
265 if (ret < 0)
266 return ret;
267 status = (data & BIT(pin)) ? 1 : 0;
268 break;
269 default:
Sebastian Reichel82039d22017-05-15 11:24:26 +0200270 return -ENOTSUPP;
271 }
272
273 *config = 0;
274
275 return status ? 0 : -EINVAL;
276}
277
278static int mcp_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
279 unsigned long *configs, unsigned int num_configs)
280{
281 struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev);
282 enum pin_config_param param;
Phil Reid2a7893c2017-10-06 13:08:11 +0800283 u32 arg;
Sebastian Reichel82039d22017-05-15 11:24:26 +0200284 int ret = 0;
285 int i;
286
287 for (i = 0; i < num_configs; i++) {
288 param = pinconf_to_config_param(configs[i]);
289 arg = pinconf_to_config_argument(configs[i]);
290
291 switch (param) {
292 case PIN_CONFIG_BIAS_PULL_UP:
Sebastian Reichel82039d22017-05-15 11:24:26 +0200293 ret = mcp_set_bit(mcp, MCP_GPPU, pin, arg);
294 break;
295 default:
Jan Kundráte0e31692019-03-07 14:16:51 +0100296 dev_dbg(mcp->dev, "Invalid config param %04x\n", param);
Sebastian Reichel82039d22017-05-15 11:24:26 +0200297 return -ENOTSUPP;
298 }
299 }
300
301 return ret;
302}
303
304static const struct pinconf_ops mcp_pinconf_ops = {
305 .pin_config_get = mcp_pinconf_get,
306 .pin_config_set = mcp_pinconf_set,
307 .is_generic = true,
308};
309
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100310/*----------------------------------------------------------------------*/
311
312#ifdef CONFIG_SPI_MASTER
313
314static int mcp23sxx_spi_write(void *context, const void *data, size_t count)
315{
316 struct mcp23s08 *mcp = context;
317 struct spi_device *spi = to_spi_device(mcp->dev);
318 struct spi_message m;
319 struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, },
320 { .tx_buf = data, .len = count, }, };
321
322 spi_message_init(&m);
323 spi_message_add_tail(&t[0], &m);
324 spi_message_add_tail(&t[1], &m);
325
326 return spi_sync(spi, &m);
327}
328
329static int mcp23sxx_spi_gather_write(void *context,
330 const void *reg, size_t reg_size,
331 const void *val, size_t val_size)
332{
333 struct mcp23s08 *mcp = context;
334 struct spi_device *spi = to_spi_device(mcp->dev);
335 struct spi_message m;
336 struct spi_transfer t[3] = { { .tx_buf = &mcp->addr, .len = 1, },
337 { .tx_buf = reg, .len = reg_size, },
338 { .tx_buf = val, .len = val_size, }, };
339
340 spi_message_init(&m);
341 spi_message_add_tail(&t[0], &m);
342 spi_message_add_tail(&t[1], &m);
343 spi_message_add_tail(&t[2], &m);
344
345 return spi_sync(spi, &m);
346}
347
348static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size,
349 void *val, size_t val_size)
350{
351 struct mcp23s08 *mcp = context;
352 struct spi_device *spi = to_spi_device(mcp->dev);
353 u8 tx[2];
354
355 if (reg_size != 1)
356 return -EINVAL;
357
358 tx[0] = mcp->addr | 0x01;
359 tx[1] = *((u8 *) reg);
360
361 return spi_write_then_read(spi, tx, sizeof(tx), val, val_size);
362}
363
364static const struct regmap_bus mcp23sxx_spi_regmap = {
365 .write = mcp23sxx_spi_write,
366 .gather_write = mcp23sxx_spi_gather_write,
367 .read = mcp23sxx_spi_read,
368};
369
370#endif /* CONFIG_SPI_MASTER */
371
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100372/*----------------------------------------------------------------------*/
373
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100374/* A given spi_device can represent up to eight mcp23sxx chips
David Brownell8f1cc3b2008-07-25 01:46:09 -0700375 * sharing the same chipselect but using different addresses
376 * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
377 * Driver data holds all the per-chip data.
378 */
379struct mcp23s08_driver_data {
380 unsigned ngpio;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100381 struct mcp23s08 *mcp[8];
David Brownell8f1cc3b2008-07-25 01:46:09 -0700382 struct mcp23s08 chip[];
383};
384
David Brownelle58b9e22008-02-04 22:28:25 -0800385
386static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
387{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100388 struct mcp23s08 *mcp = gpiochip_get_data(chip);
David Brownelle58b9e22008-02-04 22:28:25 -0800389 int status;
390
391 mutex_lock(&mcp->lock);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200392 status = mcp_set_bit(mcp, MCP_IODIR, offset, true);
David Brownelle58b9e22008-02-04 22:28:25 -0800393 mutex_unlock(&mcp->lock);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200394
David Brownelle58b9e22008-02-04 22:28:25 -0800395 return status;
396}
397
398static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
399{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100400 struct mcp23s08 *mcp = gpiochip_get_data(chip);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100401 int status, ret;
David Brownelle58b9e22008-02-04 22:28:25 -0800402
403 mutex_lock(&mcp->lock);
404
405 /* REVISIT reading this clears any IRQ ... */
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100406 ret = mcp_read(mcp, MCP_GPIO, &status);
407 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800408 status = 0;
Dmitry Mastykin59861702017-10-18 17:21:02 +0300409 else {
410 mcp->cached_gpio = status;
David Brownelle58b9e22008-02-04 22:28:25 -0800411 status = !!(status & (1 << offset));
Dmitry Mastykin59861702017-10-18 17:21:02 +0300412 }
Sebastian Reichel8f389102017-05-15 11:24:28 +0200413
David Brownelle58b9e22008-02-04 22:28:25 -0800414 mutex_unlock(&mcp->lock);
415 return status;
416}
417
Sebastian Reichel8f389102017-05-15 11:24:28 +0200418static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, bool value)
David Brownelle58b9e22008-02-04 22:28:25 -0800419{
Sebastian Reichel8f389102017-05-15 11:24:28 +0200420 return mcp_set_mask(mcp, MCP_OLAT, mask, value);
David Brownelle58b9e22008-02-04 22:28:25 -0800421}
422
423static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
424{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100425 struct mcp23s08 *mcp = gpiochip_get_data(chip);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200426 unsigned mask = BIT(offset);
David Brownelle58b9e22008-02-04 22:28:25 -0800427
428 mutex_lock(&mcp->lock);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200429 __mcp23s08_set(mcp, mask, !!value);
David Brownelle58b9e22008-02-04 22:28:25 -0800430 mutex_unlock(&mcp->lock);
431}
432
433static int
434mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
435{
Linus Walleij9e03cf02015-12-07 10:09:36 +0100436 struct mcp23s08 *mcp = gpiochip_get_data(chip);
Sebastian Reichel8f389102017-05-15 11:24:28 +0200437 unsigned mask = BIT(offset);
David Brownelle58b9e22008-02-04 22:28:25 -0800438 int status;
439
440 mutex_lock(&mcp->lock);
441 status = __mcp23s08_set(mcp, mask, value);
442 if (status == 0) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200443 status = mcp_set_mask(mcp, MCP_IODIR, mask, false);
David Brownelle58b9e22008-02-04 22:28:25 -0800444 }
445 mutex_unlock(&mcp->lock);
446 return status;
447}
448
449/*----------------------------------------------------------------------*/
Lars Poeschel4e47f912014-01-16 11:44:15 +0100450static irqreturn_t mcp23s08_irq(int irq, void *data)
451{
452 struct mcp23s08 *mcp = data;
Sebastian Reichel8f389102017-05-15 11:24:28 +0200453 int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100454 unsigned int child_irq;
Robert Middleton2cd29f22017-03-15 16:56:47 -0400455 bool intf_set, intcap_changed, gpio_bit_changed,
456 defval_changed, gpio_set;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100457
458 mutex_lock(&mcp->lock);
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100459 if (mcp_read(mcp, MCP_INTF, &intf))
460 goto unlock;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100461
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100462 if (mcp_read(mcp, MCP_INTCAP, &intcap))
463 goto unlock;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100464
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100465 if (mcp_read(mcp, MCP_INTCON, &intcon))
466 goto unlock;
Sebastian Reichel8f389102017-05-15 11:24:28 +0200467
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100468 if (mcp_read(mcp, MCP_DEFVAL, &defval))
469 goto unlock;
Robert Middleton2cd29f22017-03-15 16:56:47 -0400470
471 /* This clears the interrupt(configurable on S18) */
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100472 if (mcp_read(mcp, MCP_GPIO, &gpio))
473 goto unlock;
474
Sebastian Reichel8f389102017-05-15 11:24:28 +0200475 gpio_orig = mcp->cached_gpio;
476 mcp->cached_gpio = gpio;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100477 mutex_unlock(&mcp->lock);
478
Sebastian Reichel8f389102017-05-15 11:24:28 +0200479 if (intf == 0) {
Robert Middleton2cd29f22017-03-15 16:56:47 -0400480 /* There is no interrupt pending */
481 return IRQ_HANDLED;
482 }
483
484 dev_dbg(mcp->chip.parent,
485 "intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n",
486 intcap, intf, gpio_orig, gpio);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100487
488 for (i = 0; i < mcp->chip.ngpio; i++) {
Robert Middleton2cd29f22017-03-15 16:56:47 -0400489 /* We must check all of the inputs on the chip,
490 * otherwise we may not notice a change on >=2 pins.
491 *
492 * On at least the mcp23s17, INTCAP is only updated
493 * one byte at a time(INTCAPA and INTCAPB are
494 * not written to at the same time - only on a per-bank
495 * basis).
496 *
497 * INTF only contains the single bit that caused the
498 * interrupt per-bank. On the mcp23s17, there is
499 * INTFA and INTFB. If two pins are changed on the A
500 * side at the same time, INTF will only have one bit
501 * set. If one pin on the A side and one pin on the B
502 * side are changed at the same time, INTF will have
503 * two bits set. Thus, INTF can't be the only check
504 * to see if the input has changed.
505 */
506
Sebastian Reichel8f389102017-05-15 11:24:28 +0200507 intf_set = intf & BIT(i);
Robert Middleton2cd29f22017-03-15 16:56:47 -0400508 if (i < 8 && intf_set)
509 intcap_mask = 0x00FF;
510 else if (i >= 8 && intf_set)
511 intcap_mask = 0xFF00;
512 else
513 intcap_mask = 0x00;
514
515 intcap_changed = (intcap_mask &
Sebastian Reichel8f389102017-05-15 11:24:28 +0200516 (intcap & BIT(i))) !=
Robert Middleton2cd29f22017-03-15 16:56:47 -0400517 (intcap_mask & (BIT(i) & gpio_orig));
Sebastian Reichel8f389102017-05-15 11:24:28 +0200518 gpio_set = BIT(i) & gpio;
Robert Middleton2cd29f22017-03-15 16:56:47 -0400519 gpio_bit_changed = (BIT(i) & gpio_orig) !=
Sebastian Reichel8f389102017-05-15 11:24:28 +0200520 (BIT(i) & gpio);
521 defval_changed = (BIT(i) & intcon) &&
522 ((BIT(i) & gpio) !=
523 (BIT(i) & defval));
Robert Middleton2cd29f22017-03-15 16:56:47 -0400524
525 if (((gpio_bit_changed || intcap_changed) &&
526 (BIT(i) & mcp->irq_rise) && gpio_set) ||
527 ((gpio_bit_changed || intcap_changed) &&
528 (BIT(i) & mcp->irq_fall) && !gpio_set) ||
529 defval_changed) {
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100530 child_irq = irq_find_mapping(mcp->chip.irq.domain, i);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100531 handle_nested_irq(child_irq);
532 }
533 }
534
535 return IRQ_HANDLED;
Markus Elfring7f6f50d2017-10-30 16:03:12 +0100536
537unlock:
538 mutex_unlock(&mcp->lock);
539 return IRQ_HANDLED;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100540}
541
Lars Poeschel4e47f912014-01-16 11:44:15 +0100542static void mcp23s08_irq_mask(struct irq_data *data)
543{
Phil Reiddad3d272016-03-18 16:07:06 +0800544 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
545 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100546 unsigned int pos = data->hwirq;
547
Sebastian Reichel8f389102017-05-15 11:24:28 +0200548 mcp_set_bit(mcp, MCP_GPINTEN, pos, false);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100549}
550
551static void mcp23s08_irq_unmask(struct irq_data *data)
552{
Phil Reiddad3d272016-03-18 16:07:06 +0800553 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
554 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100555 unsigned int pos = data->hwirq;
556
Sebastian Reichel8f389102017-05-15 11:24:28 +0200557 mcp_set_bit(mcp, MCP_GPINTEN, pos, true);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100558}
559
560static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type)
561{
Phil Reiddad3d272016-03-18 16:07:06 +0800562 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
563 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100564 unsigned int pos = data->hwirq;
565 int status = 0;
566
567 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200568 mcp_set_bit(mcp, MCP_INTCON, pos, false);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100569 mcp->irq_rise |= BIT(pos);
570 mcp->irq_fall |= BIT(pos);
571 } else if (type & IRQ_TYPE_EDGE_RISING) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200572 mcp_set_bit(mcp, MCP_INTCON, pos, false);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100573 mcp->irq_rise |= BIT(pos);
574 mcp->irq_fall &= ~BIT(pos);
575 } else if (type & IRQ_TYPE_EDGE_FALLING) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200576 mcp_set_bit(mcp, MCP_INTCON, pos, false);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100577 mcp->irq_rise &= ~BIT(pos);
578 mcp->irq_fall |= BIT(pos);
Alexander Stein16fe1ad2016-03-23 18:01:27 +0100579 } else if (type & IRQ_TYPE_LEVEL_HIGH) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200580 mcp_set_bit(mcp, MCP_INTCON, pos, true);
581 mcp_set_bit(mcp, MCP_DEFVAL, pos, false);
Alexander Stein16fe1ad2016-03-23 18:01:27 +0100582 } else if (type & IRQ_TYPE_LEVEL_LOW) {
Sebastian Reichel8f389102017-05-15 11:24:28 +0200583 mcp_set_bit(mcp, MCP_INTCON, pos, true);
584 mcp_set_bit(mcp, MCP_DEFVAL, pos, true);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100585 } else
586 return -EINVAL;
587
588 return status;
589}
590
591static void mcp23s08_irq_bus_lock(struct irq_data *data)
592{
Phil Reiddad3d272016-03-18 16:07:06 +0800593 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
594 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100595
Sebastian Reichel8f389102017-05-15 11:24:28 +0200596 mutex_lock(&mcp->lock);
597 regcache_cache_only(mcp->regmap, true);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100598}
599
600static void mcp23s08_irq_bus_unlock(struct irq_data *data)
601{
Phil Reiddad3d272016-03-18 16:07:06 +0800602 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
603 struct mcp23s08 *mcp = gpiochip_get_data(gc);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100604
Sebastian Reichel8f389102017-05-15 11:24:28 +0200605 regcache_cache_only(mcp->regmap, false);
606 regcache_sync(mcp->regmap);
607
Lars Poeschel4e47f912014-01-16 11:44:15 +0100608 mutex_unlock(&mcp->lock);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100609}
610
Lars Poeschel4e47f912014-01-16 11:44:15 +0100611static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
612{
613 struct gpio_chip *chip = &mcp->chip;
Phil Reiddad3d272016-03-18 16:07:06 +0800614 int err;
Alexander Steina4e63552014-12-01 08:26:00 +0100615 unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100616
Alexander Steina4e63552014-12-01 08:26:00 +0100617 if (mcp->irq_active_high)
618 irqflags |= IRQF_TRIGGER_HIGH;
619 else
620 irqflags |= IRQF_TRIGGER_LOW;
621
Linus Walleij58383c782015-11-04 09:56:26 +0100622 err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL,
623 mcp23s08_irq,
624 irqflags, dev_name(chip->parent), mcp);
Lars Poeschel4e47f912014-01-16 11:44:15 +0100625 if (err != 0) {
Linus Walleij58383c782015-11-04 09:56:26 +0100626 dev_err(chip->parent, "unable to request IRQ#%d: %d\n",
Lars Poeschel4e47f912014-01-16 11:44:15 +0100627 mcp->irq, err);
628 return err;
629 }
630
Marco Felschf259f892018-10-02 10:06:46 +0200631 return 0;
632}
633
634static int mcp23s08_irqchip_setup(struct mcp23s08 *mcp)
635{
636 struct gpio_chip *chip = &mcp->chip;
637 int err;
638
Linus Walleijd245b3f2016-11-24 10:57:25 +0100639 err = gpiochip_irqchip_add_nested(chip,
Lars Poeschel19ab5ca2019-01-11 17:25:16 +0100640 &mcp->irq_chip,
Linus Walleijd245b3f2016-11-24 10:57:25 +0100641 0,
642 handle_simple_irq,
643 IRQ_TYPE_NONE);
Phil Reiddad3d272016-03-18 16:07:06 +0800644 if (err) {
645 dev_err(chip->parent,
646 "could not connect irqchip to gpiochip: %d\n", err);
647 return err;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100648 }
Phil Reiddad3d272016-03-18 16:07:06 +0800649
Linus Walleijd245b3f2016-11-24 10:57:25 +0100650 gpiochip_set_nested_irqchip(chip,
Lars Poeschel19ab5ca2019-01-11 17:25:16 +0100651 &mcp->irq_chip,
Linus Walleijd245b3f2016-11-24 10:57:25 +0100652 mcp->irq);
Phil Reiddad3d272016-03-18 16:07:06 +0800653
Lars Poeschel4e47f912014-01-16 11:44:15 +0100654 return 0;
655}
656
Lars Poeschel4e47f912014-01-16 11:44:15 +0100657/*----------------------------------------------------------------------*/
David Brownelle58b9e22008-02-04 22:28:25 -0800658
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200659static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
Lars Poeschel4e47f912014-01-16 11:44:15 +0100660 void *data, unsigned addr, unsigned type,
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200661 unsigned int base, int cs)
David Brownelle58b9e22008-02-04 22:28:25 -0800662{
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100663 int status, ret;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100664 bool mirror = false;
Phil Reidfa2b7fa2018-02-19 17:25:20 +0800665 bool open_drain = false;
Jan Kundrát9b3e4202018-01-25 18:29:15 +0100666 struct regmap_config *one_regmap_config = NULL;
Jan Kundráted231752018-01-26 20:16:47 +0100667 int raw_chip_address = (addr & ~0x40) >> 1;
David Brownelle58b9e22008-02-04 22:28:25 -0800668
David Brownelle58b9e22008-02-04 22:28:25 -0800669 mutex_init(&mcp->lock);
670
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100671 mcp->dev = dev;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200672 mcp->addr = addr;
Alexander Steina4e63552014-12-01 08:26:00 +0100673 mcp->irq_active_high = false;
David Brownelle58b9e22008-02-04 22:28:25 -0800674
David Brownelle58b9e22008-02-04 22:28:25 -0800675 mcp->chip.direction_input = mcp23s08_direction_input;
676 mcp->chip.get = mcp23s08_get;
677 mcp->chip.direction_output = mcp23s08_direction_output;
678 mcp->chip.set = mcp23s08_set;
Linus Walleij60f749f2016-09-07 23:13:20 +0200679#ifdef CONFIG_OF_GPIO
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200680 mcp->chip.of_gpio_n_cells = 2;
681 mcp->chip.of_node = dev->of_node;
682#endif
David Brownelle58b9e22008-02-04 22:28:25 -0800683
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200684 switch (type) {
685#ifdef CONFIG_SPI_MASTER
686 case MCP_TYPE_S08:
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200687 case MCP_TYPE_S17:
Jan Kundrát9b3e4202018-01-25 18:29:15 +0100688 switch (type) {
689 case MCP_TYPE_S08:
690 one_regmap_config =
691 devm_kmemdup(dev, &mcp23x08_regmap,
692 sizeof(struct regmap_config), GFP_KERNEL);
693 mcp->reg_shift = 0;
694 mcp->chip.ngpio = 8;
Jan Kundráted231752018-01-26 20:16:47 +0100695 mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL,
696 "mcp23s08.%d", raw_chip_address);
Jan Kundrát9b3e4202018-01-25 18:29:15 +0100697 break;
698 case MCP_TYPE_S17:
699 one_regmap_config =
700 devm_kmemdup(dev, &mcp23x17_regmap,
701 sizeof(struct regmap_config), GFP_KERNEL);
702 mcp->reg_shift = 1;
703 mcp->chip.ngpio = 16;
Jan Kundráted231752018-01-26 20:16:47 +0100704 mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL,
705 "mcp23s17.%d", raw_chip_address);
Jan Kundrát9b3e4202018-01-25 18:29:15 +0100706 break;
707 }
708 if (!one_regmap_config)
709 return -ENOMEM;
710
Jan Kundráted231752018-01-26 20:16:47 +0100711 one_regmap_config->name = devm_kasprintf(dev, GFP_KERNEL, "%d", raw_chip_address);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100712 mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
Jan Kundrát9b3e4202018-01-25 18:29:15 +0100713 one_regmap_config);
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200714 break;
Phil Reid28c5a412016-03-01 14:25:41 +0800715
716 case MCP_TYPE_S18:
Jason Kridnerf1659882019-01-11 10:02:13 -0500717 one_regmap_config =
718 devm_kmemdup(dev, &mcp23x17_regmap,
719 sizeof(struct regmap_config), GFP_KERNEL);
720 if (!one_regmap_config)
721 return -ENOMEM;
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100722 mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
Jason Kridnerf1659882019-01-11 10:02:13 -0500723 one_regmap_config);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100724 mcp->reg_shift = 1;
Phil Reid28c5a412016-03-01 14:25:41 +0800725 mcp->chip.ngpio = 16;
726 mcp->chip.label = "mcp23s18";
727 break;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200728#endif /* CONFIG_SPI_MASTER */
729
Daniel M. Weekscbf24fa2012-11-06 23:51:05 -0500730#if IS_ENABLED(CONFIG_I2C)
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200731 case MCP_TYPE_008:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100732 mcp->regmap = devm_regmap_init_i2c(data, &mcp23x08_regmap);
733 mcp->reg_shift = 0;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200734 mcp->chip.ngpio = 8;
735 mcp->chip.label = "mcp23008";
736 break;
737
738 case MCP_TYPE_017:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100739 mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
740 mcp->reg_shift = 1;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200741 mcp->chip.ngpio = 16;
742 mcp->chip.label = "mcp23017";
743 break;
Phil Reidff0f2ce2017-10-06 13:08:07 +0800744
745 case MCP_TYPE_018:
746 mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
747 mcp->reg_shift = 1;
748 mcp->chip.ngpio = 16;
749 mcp->chip.label = "mcp23018";
750 break;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200751#endif /* CONFIG_I2C */
752
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200753 default:
754 dev_err(dev, "invalid device type (%d)\n", type);
755 return -EINVAL;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100756 }
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200757
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100758 if (IS_ERR(mcp->regmap))
759 return PTR_ERR(mcp->regmap);
760
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200761 mcp->chip.base = base;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100762 mcp->chip.can_sleep = true;
Linus Walleij58383c782015-11-04 09:56:26 +0100763 mcp->chip.parent = dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700764 mcp->chip.owner = THIS_MODULE;
David Brownelle58b9e22008-02-04 22:28:25 -0800765
David Brownell8f1cc3b2008-07-25 01:46:09 -0700766 /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
767 * and MCP_IOCON.HAEN = 1, so we work with all chips.
768 */
Lars Poeschel4e47f912014-01-16 11:44:15 +0100769
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100770 ret = mcp_read(mcp, MCP_IOCON, &status);
771 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800772 goto fail;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100773
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200774 mcp->irq_controller =
775 device_property_read_bool(dev, "interrupt-controller");
Alexander Steina4e63552014-12-01 08:26:00 +0100776 if (mcp->irq && mcp->irq_controller) {
Linus Walleij170680a2014-12-12 11:22:11 +0100777 mcp->irq_active_high =
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200778 device_property_read_bool(dev,
Linus Walleij170680a2014-12-12 11:22:11 +0100779 "microchip,irq-active-high");
Lars Poeschel4e47f912014-01-16 11:44:15 +0100780
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200781 mirror = device_property_read_bool(dev, "microchip,irq-mirror");
Phil Reidfa2b7fa2018-02-19 17:25:20 +0800782 open_drain = device_property_read_bool(dev, "drive-open-drain");
Alexander Steina4e63552014-12-01 08:26:00 +0100783 }
784
785 if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror ||
Phil Reidfa2b7fa2018-02-19 17:25:20 +0800786 mcp->irq_active_high || open_drain) {
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100787 /* mcp23s17 has IOCON twice, make sure they are in sync */
788 status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
789 status |= IOCON_HAEN | (IOCON_HAEN << 8);
Alexander Steina4e63552014-12-01 08:26:00 +0100790 if (mcp->irq_active_high)
791 status |= IOCON_INTPOL | (IOCON_INTPOL << 8);
792 else
793 status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8));
794
Lars Poeschel4e47f912014-01-16 11:44:15 +0100795 if (mirror)
796 status |= IOCON_MIRROR | (IOCON_MIRROR << 8);
797
Phil Reidfa2b7fa2018-02-19 17:25:20 +0800798 if (open_drain)
799 status |= IOCON_ODR | (IOCON_ODR << 8);
800
Phil Reidff0f2ce2017-10-06 13:08:07 +0800801 if (type == MCP_TYPE_S18 || type == MCP_TYPE_018)
Phil Reid35396992016-03-15 15:46:30 +0800802 status |= IOCON_INTCC | (IOCON_INTCC << 8);
803
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100804 ret = mcp_write(mcp, MCP_IOCON, status);
805 if (ret < 0)
David Brownelle58b9e22008-02-04 22:28:25 -0800806 goto fail;
807 }
808
Lars Poeschel4e47f912014-01-16 11:44:15 +0100809 if (mcp->irq && mcp->irq_controller) {
Marco Felschf259f892018-10-02 10:06:46 +0200810 ret = mcp23s08_irqchip_setup(mcp);
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100811 if (ret)
Lars Poeschel4e47f912014-01-16 11:44:15 +0100812 goto fail;
Lars Poeschel4e47f912014-01-16 11:44:15 +0100813 }
Sebastian Reichel82039d22017-05-15 11:24:26 +0200814
Dmitry Mastykin02e389e2017-12-28 18:19:24 +0300815 ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp);
816 if (ret < 0)
817 goto fail;
818
Jan Kundrát1781af52018-01-26 16:06:37 +0100819 if (one_regmap_config) {
820 mcp->pinctrl_desc.name = devm_kasprintf(dev, GFP_KERNEL,
821 "mcp23xxx-pinctrl.%d", raw_chip_address);
822 if (!mcp->pinctrl_desc.name)
823 return -ENOMEM;
824 } else {
825 mcp->pinctrl_desc.name = "mcp23xxx-pinctrl";
826 }
Sebastian Reichel82039d22017-05-15 11:24:26 +0200827 mcp->pinctrl_desc.pctlops = &mcp_pinctrl_ops;
828 mcp->pinctrl_desc.confops = &mcp_pinconf_ops;
829 mcp->pinctrl_desc.npins = mcp->chip.ngpio;
830 if (mcp->pinctrl_desc.npins == 8)
831 mcp->pinctrl_desc.pins = mcp23x08_pins;
832 else if (mcp->pinctrl_desc.npins == 16)
833 mcp->pinctrl_desc.pins = mcp23x17_pins;
834 mcp->pinctrl_desc.owner = THIS_MODULE;
835
836 mcp->pctldev = devm_pinctrl_register(dev, &mcp->pinctrl_desc, mcp);
837 if (IS_ERR(mcp->pctldev)) {
838 ret = PTR_ERR(mcp->pctldev);
839 goto fail;
840 }
841
Marco Felschf259f892018-10-02 10:06:46 +0200842 if (mcp->irq)
843 ret = mcp23s08_irq_setup(mcp);
844
David Brownell8f1cc3b2008-07-25 01:46:09 -0700845fail:
Sebastian Reichel3d84fdb2017-01-27 15:47:37 +0100846 if (ret < 0)
847 dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret);
848 return ret;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700849}
850
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200851/*----------------------------------------------------------------------*/
852
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200853#ifdef CONFIG_OF
854#ifdef CONFIG_SPI_MASTER
Jingoo Hanac791802014-05-07 18:05:17 +0900855static const struct of_device_id mcp23s08_spi_of_match[] = {
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200856 {
Lars Poeschel45971682013-08-28 10:38:50 +0200857 .compatible = "microchip,mcp23s08",
858 .data = (void *) MCP_TYPE_S08,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200859 },
860 {
Lars Poeschel45971682013-08-28 10:38:50 +0200861 .compatible = "microchip,mcp23s17",
862 .data = (void *) MCP_TYPE_S17,
863 },
Phil Reid28c5a412016-03-01 14:25:41 +0800864 {
865 .compatible = "microchip,mcp23s18",
866 .data = (void *) MCP_TYPE_S18,
867 },
Lars Poeschel45971682013-08-28 10:38:50 +0200868/* NOTE: The use of the mcp prefix is deprecated and will be removed. */
869 {
870 .compatible = "mcp,mcp23s08",
871 .data = (void *) MCP_TYPE_S08,
872 },
873 {
874 .compatible = "mcp,mcp23s17",
875 .data = (void *) MCP_TYPE_S17,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200876 },
877 { },
878};
879MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
880#endif
881
882#if IS_ENABLED(CONFIG_I2C)
Jingoo Hanac791802014-05-07 18:05:17 +0900883static const struct of_device_id mcp23s08_i2c_of_match[] = {
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200884 {
Lars Poeschel45971682013-08-28 10:38:50 +0200885 .compatible = "microchip,mcp23008",
886 .data = (void *) MCP_TYPE_008,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200887 },
888 {
Lars Poeschel45971682013-08-28 10:38:50 +0200889 .compatible = "microchip,mcp23017",
890 .data = (void *) MCP_TYPE_017,
891 },
Phil Reidff0f2ce2017-10-06 13:08:07 +0800892 {
893 .compatible = "microchip,mcp23018",
894 .data = (void *) MCP_TYPE_018,
895 },
Lars Poeschel45971682013-08-28 10:38:50 +0200896/* NOTE: The use of the mcp prefix is deprecated and will be removed. */
897 {
898 .compatible = "mcp,mcp23008",
899 .data = (void *) MCP_TYPE_008,
900 },
901 {
902 .compatible = "mcp,mcp23017",
903 .data = (void *) MCP_TYPE_017,
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200904 },
905 { },
906};
907MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
908#endif
909#endif /* CONFIG_OF */
910
911
Daniel M. Weekscbf24fa2012-11-06 23:51:05 -0500912#if IS_ENABLED(CONFIG_I2C)
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200913
Bill Pemberton38363092012-11-19 13:22:34 -0500914static int mcp230xx_probe(struct i2c_client *client,
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200915 const struct i2c_device_id *id)
916{
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800917 struct mcp23s08_platform_data *pdata, local_pdata;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200918 struct mcp23s08 *mcp;
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800919 int status;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200920
Sebastian Reichel5f853ac2017-05-15 11:24:33 +0200921 pdata = dev_get_platdata(&client->dev);
922 if (!pdata) {
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800923 pdata = &local_pdata;
924 pdata->base = -1;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200925 }
926
Sebastian Reichel2f98e782017-05-15 11:24:31 +0200927 mcp = devm_kzalloc(&client->dev, sizeof(*mcp), GFP_KERNEL);
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200928 if (!mcp)
929 return -ENOMEM;
930
Lars Poeschel4e47f912014-01-16 11:44:15 +0100931 mcp->irq = client->irq;
Lars Poeschel19ab5ca2019-01-11 17:25:16 +0100932 mcp->irq_chip.name = dev_name(&client->dev);
933 mcp->irq_chip.irq_mask = mcp23s08_irq_mask;
934 mcp->irq_chip.irq_unmask = mcp23s08_irq_unmask;
935 mcp->irq_chip.irq_set_type = mcp23s08_irq_set_type;
936 mcp->irq_chip.irq_bus_lock = mcp23s08_irq_bus_lock;
937 mcp->irq_chip.irq_bus_sync_unlock = mcp23s08_irq_bus_unlock;
938
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200939 status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +0200940 id->driver_data, pdata->base, 0);
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200941 if (status)
Sebastian Reichel2f98e782017-05-15 11:24:31 +0200942 return status;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200943
944 i2c_set_clientdata(client, mcp);
945
946 return 0;
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200947}
948
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200949static const struct i2c_device_id mcp230xx_id[] = {
950 { "mcp23008", MCP_TYPE_008 },
951 { "mcp23017", MCP_TYPE_017 },
Phil Reidff0f2ce2017-10-06 13:08:07 +0800952 { "mcp23018", MCP_TYPE_018 },
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200953 { },
954};
955MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
956
957static struct i2c_driver mcp230xx_driver = {
958 .driver = {
959 .name = "mcp230xx",
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200960 .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200961 },
962 .probe = mcp230xx_probe,
Peter Korsgaard752ad5e2011-07-15 10:25:32 +0200963 .id_table = mcp230xx_id,
964};
965
966static int __init mcp23s08_i2c_init(void)
967{
968 return i2c_add_driver(&mcp230xx_driver);
969}
970
971static void mcp23s08_i2c_exit(void)
972{
973 i2c_del_driver(&mcp230xx_driver);
974}
975
976#else
977
978static int __init mcp23s08_i2c_init(void) { return 0; }
979static void mcp23s08_i2c_exit(void) { }
980
981#endif /* CONFIG_I2C */
982
983/*----------------------------------------------------------------------*/
984
Peter Korsgaardd62b98f2011-07-15 10:25:31 +0200985#ifdef CONFIG_SPI_MASTER
986
David Brownell8f1cc3b2008-07-25 01:46:09 -0700987static int mcp23s08_probe(struct spi_device *spi)
988{
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800989 struct mcp23s08_platform_data *pdata, local_pdata;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700990 unsigned addr;
Linus Walleij596a1c52014-05-28 09:14:06 +0200991 int chips = 0;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700992 struct mcp23s08_driver_data *data;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +0100993 int status, type;
Sonic Zhang3af0dbd2014-09-01 11:19:52 +0800994 unsigned ngpio = 0;
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200995 const struct of_device_id *match;
David Brownell8f1cc3b2008-07-25 01:46:09 -0700996
Lars Poeschel97ddb1c2013-04-04 12:02:02 +0200997 match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +0200998 if (match)
SeongJae Parkde755c32014-01-18 13:53:04 +0900999 type = (int)(uintptr_t)match->data;
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +02001000 else
1001 type = spi_get_device_id(spi)->driver_data;
1002
1003 pdata = dev_get_platdata(&spi->dev);
1004 if (!pdata) {
1005 pdata = &local_pdata;
1006 pdata->base = -1;
1007
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +02001008 status = device_property_read_u32(&spi->dev,
Sebastian Reichelce9bd0a2017-05-15 11:24:36 +02001009 "microchip,spi-present-mask", &pdata->spi_present_mask);
Lars Poeschel97ddb1c2013-04-04 12:02:02 +02001010 if (status) {
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +02001011 status = device_property_read_u32(&spi->dev,
Sebastian Reichelce9bd0a2017-05-15 11:24:36 +02001012 "mcp,spi-present-mask",
1013 &pdata->spi_present_mask);
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +02001014
Lars Poeschel45971682013-08-28 10:38:50 +02001015 if (status) {
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +02001016 dev_err(&spi->dev, "missing spi-present-mask");
Lars Poeschel45971682013-08-28 10:38:50 +02001017 return -ENODEV;
1018 }
Lars Poeschel97ddb1c2013-04-04 12:02:02 +02001019 }
David Brownell8f1cc3b2008-07-25 01:46:09 -07001020 }
David Brownell8f1cc3b2008-07-25 01:46:09 -07001021
Sebastian Reichelce9bd0a2017-05-15 11:24:36 +02001022 if (!pdata->spi_present_mask || pdata->spi_present_mask > 0xff) {
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +02001023 dev_err(&spi->dev, "invalid spi-present-mask");
1024 return -ENODEV;
1025 }
1026
Sebastian Reichelce9bd0a2017-05-15 11:24:36 +02001027 for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
1028 if (pdata->spi_present_mask & BIT(addr))
Sebastian Reichel0d7fcd52017-05-15 11:24:34 +02001029 chips++;
1030 }
1031
Michael Welling99e4b982014-04-16 20:00:24 -05001032 if (!chips)
1033 return -ENODEV;
1034
Varka Bhadram7898b312015-03-31 09:49:08 +05301035 data = devm_kzalloc(&spi->dev,
Gustavo A. R. Silva16f4372f2019-01-04 11:37:33 -06001036 struct_size(data, chip, chips), GFP_KERNEL);
David Brownell8f1cc3b2008-07-25 01:46:09 -07001037 if (!data)
1038 return -ENOMEM;
Varka Bhadram7898b312015-03-31 09:49:08 +05301039
David Brownell8f1cc3b2008-07-25 01:46:09 -07001040 spi_set_drvdata(spi, data);
1041
Sebastian Reichelce9bd0a2017-05-15 11:24:36 +02001042 for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
1043 if (!(pdata->spi_present_mask & BIT(addr)))
David Brownell8f1cc3b2008-07-25 01:46:09 -07001044 continue;
1045 chips--;
1046 data->mcp[addr] = &data->chip[chips];
Alexander Steina231b88c2014-11-17 09:38:10 +01001047 data->mcp[addr]->irq = spi->irq;
Lars Poeschel19ab5ca2019-01-11 17:25:16 +01001048 data->mcp[addr]->irq_chip.name = dev_name(&spi->dev);
1049 data->mcp[addr]->irq_chip.irq_mask = mcp23s08_irq_mask;
1050 data->mcp[addr]->irq_chip.irq_unmask = mcp23s08_irq_unmask;
1051 data->mcp[addr]->irq_chip.irq_set_type = mcp23s08_irq_set_type;
1052 data->mcp[addr]->irq_chip.irq_bus_lock = mcp23s08_irq_bus_lock;
1053 data->mcp[addr]->irq_chip.irq_bus_sync_unlock =
1054 mcp23s08_irq_bus_unlock;
Peter Korsgaardd62b98f2011-07-15 10:25:31 +02001055 status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
Sebastian Reichel5b1a7e82017-05-15 11:24:35 +02001056 0x40 | (addr << 1), type,
1057 pdata->base, addr);
David Brownell8f1cc3b2008-07-25 01:46:09 -07001058 if (status < 0)
Sebastian Reicheld0e49da2017-05-15 11:24:32 +02001059 return status;
Peter Korsgaard0b7bb772011-03-09 17:56:30 +01001060
Sonic Zhang3af0dbd2014-09-01 11:19:52 +08001061 if (pdata->base != -1)
Phil Reid28c5a412016-03-01 14:25:41 +08001062 pdata->base += data->mcp[addr]->chip.ngpio;
1063 ngpio += data->mcp[addr]->chip.ngpio;
David Brownell8f1cc3b2008-07-25 01:46:09 -07001064 }
Lars Poeschel97ddb1c2013-04-04 12:02:02 +02001065 data->ngpio = ngpio;
David Brownelle58b9e22008-02-04 22:28:25 -08001066
David Brownelle58b9e22008-02-04 22:28:25 -08001067 return 0;
David Brownelle58b9e22008-02-04 22:28:25 -08001068}
1069
Peter Korsgaard0b7bb772011-03-09 17:56:30 +01001070static const struct spi_device_id mcp23s08_ids[] = {
1071 { "mcp23s08", MCP_TYPE_S08 },
1072 { "mcp23s17", MCP_TYPE_S17 },
Phil Reid28c5a412016-03-01 14:25:41 +08001073 { "mcp23s18", MCP_TYPE_S18 },
Peter Korsgaard0b7bb772011-03-09 17:56:30 +01001074 { },
1075};
1076MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
1077
David Brownelle58b9e22008-02-04 22:28:25 -08001078static struct spi_driver mcp23s08_driver = {
1079 .probe = mcp23s08_probe,
Peter Korsgaard0b7bb772011-03-09 17:56:30 +01001080 .id_table = mcp23s08_ids,
David Brownelle58b9e22008-02-04 22:28:25 -08001081 .driver = {
1082 .name = "mcp23s08",
Lars Poeschel97ddb1c2013-04-04 12:02:02 +02001083 .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
David Brownelle58b9e22008-02-04 22:28:25 -08001084 },
1085};
1086
Peter Korsgaardd62b98f2011-07-15 10:25:31 +02001087static int __init mcp23s08_spi_init(void)
1088{
1089 return spi_register_driver(&mcp23s08_driver);
1090}
1091
1092static void mcp23s08_spi_exit(void)
1093{
1094 spi_unregister_driver(&mcp23s08_driver);
1095}
1096
1097#else
1098
1099static int __init mcp23s08_spi_init(void) { return 0; }
1100static void mcp23s08_spi_exit(void) { }
1101
1102#endif /* CONFIG_SPI_MASTER */
1103
David Brownelle58b9e22008-02-04 22:28:25 -08001104/*----------------------------------------------------------------------*/
1105
1106static int __init mcp23s08_init(void)
1107{
Peter Korsgaard752ad5e2011-07-15 10:25:32 +02001108 int ret;
1109
1110 ret = mcp23s08_spi_init();
1111 if (ret)
1112 goto spi_fail;
1113
1114 ret = mcp23s08_i2c_init();
1115 if (ret)
1116 goto i2c_fail;
1117
1118 return 0;
1119
1120 i2c_fail:
1121 mcp23s08_spi_exit();
1122 spi_fail:
1123 return ret;
David Brownelle58b9e22008-02-04 22:28:25 -08001124}
Peter Korsgaard752ad5e2011-07-15 10:25:32 +02001125/* register after spi/i2c postcore initcall and before
David Brownell673c0c02008-10-15 22:02:46 -07001126 * subsys initcalls that may rely on these GPIOs
1127 */
1128subsys_initcall(mcp23s08_init);
David Brownelle58b9e22008-02-04 22:28:25 -08001129
1130static void __exit mcp23s08_exit(void)
1131{
Peter Korsgaardd62b98f2011-07-15 10:25:31 +02001132 mcp23s08_spi_exit();
Peter Korsgaard752ad5e2011-07-15 10:25:32 +02001133 mcp23s08_i2c_exit();
David Brownelle58b9e22008-02-04 22:28:25 -08001134}
1135module_exit(mcp23s08_exit);
1136
1137MODULE_LICENSE("GPL");