Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * GPIO Chip driver for Analog Devices
  3 * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
  4 *
  5 * Copyright 2009-2010 Analog Devices Inc.
  6 *
  7 * Licensed under the GPL-2 or later.
  8 */
  9
 10#include <linux/module.h>
 11#include <linux/kernel.h>
 12#include <linux/slab.h>
 13#include <linux/init.h>
 14#include <linux/i2c.h>
 15#include <linux/gpio.h>
 16#include <linux/interrupt.h>
 17#include <linux/irq.h>
 
 18
 19#include <linux/i2c/adp5588.h>
 20
 21#define DRV_NAME	"adp5588-gpio"
 22
 23/*
 24 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
 25 * since the Event Counter Register updated 25ms after the interrupt
 26 * asserted.
 27 */
 28#define WA_DELAYED_READOUT_REVID(rev)	((rev) < 4)
 29
 30struct adp5588_gpio {
 31	struct i2c_client *client;
 32	struct gpio_chip gpio_chip;
 33	struct mutex lock;	/* protect cached dir, dat_out */
 34	/* protect serialized access to the interrupt controller bus */
 35	struct mutex irq_lock;
 36	unsigned gpio_start;
 37	unsigned irq_base;
 38	uint8_t dat_out[3];
 39	uint8_t dir[3];
 40	uint8_t int_lvl[3];
 
 41	uint8_t int_en[3];
 42	uint8_t irq_mask[3];
 43	uint8_t irq_stat[3];
 44};
 45
 46static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
 47{
 48	int ret = i2c_smbus_read_byte_data(client, reg);
 49
 50	if (ret < 0)
 51		dev_err(&client->dev, "Read Error\n");
 52
 53	return ret;
 54}
 55
 56static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
 57{
 58	int ret = i2c_smbus_write_byte_data(client, reg, val);
 59
 60	if (ret < 0)
 61		dev_err(&client->dev, "Write Error\n");
 62
 63	return ret;
 64}
 65
 66static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
 67{
 68	struct adp5588_gpio *dev =
 69	    container_of(chip, struct adp5588_gpio, gpio_chip);
 
 
 
 
 
 
 
 
 
 70
 71	return !!(adp5588_gpio_read(dev->client,
 72		  GPIO_DAT_STAT1 + ADP5588_BANK(off)) & ADP5588_BIT(off));
 
 73}
 74
 75static void adp5588_gpio_set_value(struct gpio_chip *chip,
 76				   unsigned off, int val)
 77{
 78	unsigned bank, bit;
 79	struct adp5588_gpio *dev =
 80	    container_of(chip, struct adp5588_gpio, gpio_chip);
 81
 82	bank = ADP5588_BANK(off);
 83	bit = ADP5588_BIT(off);
 84
 85	mutex_lock(&dev->lock);
 86	if (val)
 87		dev->dat_out[bank] |= bit;
 88	else
 89		dev->dat_out[bank] &= ~bit;
 90
 91	adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
 92			   dev->dat_out[bank]);
 93	mutex_unlock(&dev->lock);
 94}
 95
 96static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
 97{
 98	int ret;
 99	unsigned bank;
100	struct adp5588_gpio *dev =
101	    container_of(chip, struct adp5588_gpio, gpio_chip);
102
103	bank = ADP5588_BANK(off);
104
105	mutex_lock(&dev->lock);
106	dev->dir[bank] &= ~ADP5588_BIT(off);
107	ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
108	mutex_unlock(&dev->lock);
109
110	return ret;
111}
112
113static int adp5588_gpio_direction_output(struct gpio_chip *chip,
114					 unsigned off, int val)
115{
116	int ret;
117	unsigned bank, bit;
118	struct adp5588_gpio *dev =
119	    container_of(chip, struct adp5588_gpio, gpio_chip);
120
121	bank = ADP5588_BANK(off);
122	bit = ADP5588_BIT(off);
123
124	mutex_lock(&dev->lock);
125	dev->dir[bank] |= bit;
126
127	if (val)
128		dev->dat_out[bank] |= bit;
129	else
130		dev->dat_out[bank] &= ~bit;
131
132	ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
133				 dev->dat_out[bank]);
134	ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
135				 dev->dir[bank]);
136	mutex_unlock(&dev->lock);
137
138	return ret;
139}
140
141#ifdef CONFIG_GPIO_ADP5588_IRQ
142static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off)
143{
144	struct adp5588_gpio *dev =
145		container_of(chip, struct adp5588_gpio, gpio_chip);
146	return dev->irq_base + off;
147}
148
149static void adp5588_irq_bus_lock(struct irq_data *d)
150{
151	struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
 
152
153	mutex_lock(&dev->irq_lock);
154}
155
156 /*
157  * genirq core code can issue chip->mask/unmask from atomic context.
158  * This doesn't work for slow busses where an access needs to sleep.
159  * bus_sync_unlock() is therefore called outside the atomic context,
160  * syncs the current irq mask state with the slow external controller
161  * and unlocks the bus.
162  */
163
164static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
165{
166	struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
 
167	int i;
168
169	for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++)
 
 
 
 
 
 
 
 
 
170		if (dev->int_en[i] ^ dev->irq_mask[i]) {
171			dev->int_en[i] = dev->irq_mask[i];
172			adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i,
173					   dev->int_en[i]);
174		}
 
175
176	mutex_unlock(&dev->irq_lock);
177}
178
179static void adp5588_irq_mask(struct irq_data *d)
180{
181	struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
182	unsigned gpio = d->irq - dev->irq_base;
183
184	dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
185}
186
187static void adp5588_irq_unmask(struct irq_data *d)
188{
189	struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
190	unsigned gpio = d->irq - dev->irq_base;
191
192	dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
193}
194
195static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
196{
197	struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
198	uint16_t gpio = d->irq - dev->irq_base;
 
199	unsigned bank, bit;
200
201	if ((type & IRQ_TYPE_EDGE_BOTH)) {
202		dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
203			d->irq, type);
204		return -EINVAL;
205	}
206
207	bank = ADP5588_BANK(gpio);
208	bit = ADP5588_BIT(gpio);
209
210	if (type & IRQ_TYPE_LEVEL_HIGH)
211		dev->int_lvl[bank] |= bit;
212	else if (type & IRQ_TYPE_LEVEL_LOW)
213		dev->int_lvl[bank] &= ~bit;
214	else
215		return -EINVAL;
 
 
216
217	adp5588_gpio_direction_input(&dev->gpio_chip, gpio);
218	adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank,
219			   dev->int_lvl[bank]);
220
221	return 0;
222}
223
224static struct irq_chip adp5588_irq_chip = {
225	.name			= "adp5588",
226	.irq_mask		= adp5588_irq_mask,
227	.irq_unmask		= adp5588_irq_unmask,
228	.irq_bus_lock		= adp5588_irq_bus_lock,
229	.irq_bus_sync_unlock	= adp5588_irq_bus_sync_unlock,
230	.irq_set_type		= adp5588_irq_set_type,
231};
232
233static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
234{
235	int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
236
237	if (ret < 0)
238		dev_err(&client->dev, "Read INT_STAT Error\n");
239
240	return ret;
241}
242
243static irqreturn_t adp5588_irq_handler(int irq, void *devid)
244{
245	struct adp5588_gpio *dev = devid;
246	unsigned status, bank, bit, pending;
247	int ret;
248	status = adp5588_gpio_read(dev->client, INT_STAT);
249
250	if (status & ADP5588_GPI_INT) {
251		ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat);
252		if (ret < 0)
253			memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
254
255		for (bank = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
256			bank++, bit = 0) {
257			pending = dev->irq_stat[bank] & dev->irq_mask[bank];
258
259			while (pending) {
260				if (pending & (1 << bit)) {
261					handle_nested_irq(dev->irq_base +
262							  (bank << 3) + bit);
263					pending &= ~(1 << bit);
264
265				}
266				bit++;
 
 
 
 
 
 
 
 
 
 
 
 
 
267			}
268		}
269	}
270
271	adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
272
273	return IRQ_HANDLED;
274}
275
276static int adp5588_irq_setup(struct adp5588_gpio *dev)
277{
278	struct i2c_client *client = dev->client;
279	struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
280	unsigned gpio;
281	int ret;
 
 
 
282
283	adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
284	adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
285	adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */
286
287	dev->irq_base = pdata->irq_base;
288	mutex_init(&dev->irq_lock);
289
290	for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
291		int irq = gpio + dev->irq_base;
292		irq_set_chip_data(irq, dev);
293		irq_set_chip_and_handler(irq, &adp5588_irq_chip,
294					 handle_level_irq);
295		irq_set_nested_thread(irq, 1);
296#ifdef CONFIG_ARM
297		/*
298		 * ARM needs us to explicitly flag the IRQ as VALID,
299		 * once we do so, it will also set the noprobe.
300		 */
301		set_irq_flags(irq, IRQF_VALID);
302#else
303		irq_set_noprobe(irq);
304#endif
305	}
306
307	ret = request_threaded_irq(client->irq,
308				   NULL,
309				   adp5588_irq_handler,
310				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
311				   dev_name(&client->dev), dev);
312	if (ret) {
313		dev_err(&client->dev, "failed to request irq %d\n",
314			client->irq);
315		goto out;
 
 
 
 
 
 
 
 
 
316	}
 
 
 
317
318	dev->gpio_chip.to_irq = adp5588_gpio_to_irq;
319	adp5588_gpio_write(client, CFG,
320		ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT);
321
322	return 0;
323
324out:
325	dev->irq_base = 0;
326	return ret;
327}
328
329static void adp5588_irq_teardown(struct adp5588_gpio *dev)
330{
331	if (dev->irq_base)
332		free_irq(dev->client->irq, dev);
333}
334
335#else
336static int adp5588_irq_setup(struct adp5588_gpio *dev)
337{
338	struct i2c_client *client = dev->client;
339	dev_warn(&client->dev, "interrupt support not compiled in\n");
340
341	return 0;
342}
343
344static void adp5588_irq_teardown(struct adp5588_gpio *dev)
345{
346}
347#endif /* CONFIG_GPIO_ADP5588_IRQ */
348
349static int __devinit adp5588_gpio_probe(struct i2c_client *client,
350					const struct i2c_device_id *id)
351{
352	struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
 
353	struct adp5588_gpio *dev;
354	struct gpio_chip *gc;
355	int ret, i, revid;
356
357	if (pdata == NULL) {
358		dev_err(&client->dev, "missing platform data\n");
359		return -ENODEV;
360	}
361
362	if (!i2c_check_functionality(client->adapter,
363					I2C_FUNC_SMBUS_BYTE_DATA)) {
364		dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
365		return -EIO;
366	}
367
368	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
369	if (dev == NULL) {
370		dev_err(&client->dev, "failed to alloc memory\n");
371		return -ENOMEM;
372	}
373
374	dev->client = client;
375
376	gc = &dev->gpio_chip;
377	gc->direction_input = adp5588_gpio_direction_input;
378	gc->direction_output = adp5588_gpio_direction_output;
379	gc->get = adp5588_gpio_get_value;
380	gc->set = adp5588_gpio_set_value;
381	gc->can_sleep = 1;
 
 
 
 
 
 
 
 
382
383	gc->base = pdata->gpio_start;
384	gc->ngpio = ADP5588_MAXGPIO;
385	gc->label = client->name;
386	gc->owner = THIS_MODULE;
387
388	mutex_init(&dev->lock);
389
390	ret = adp5588_gpio_read(dev->client, DEV_ID);
391	if (ret < 0)
392		goto err;
393
394	revid = ret & ADP5588_DEVICE_ID_MASK;
395
396	for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
397		dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
398		dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
399		ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
400		ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
401				(pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
402		ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
403		if (ret)
404			goto err;
405	}
406
407	if (pdata->irq_base) {
408		if (WA_DELAYED_READOUT_REVID(revid)) {
409			dev_warn(&client->dev, "GPIO int not supported\n");
410		} else {
411			ret = adp5588_irq_setup(dev);
412			if (ret)
413				goto err;
414		}
415	}
416
417	ret = gpiochip_add(&dev->gpio_chip);
418	if (ret)
419		goto err_irq;
420
421	dev_info(&client->dev, "gpios %d..%d (IRQ Base %d) on a %s Rev. %d\n",
422			gc->base, gc->base + gc->ngpio - 1,
423			pdata->irq_base, client->name, revid);
424
425	if (pdata->setup) {
426		ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
427		if (ret < 0)
428			dev_warn(&client->dev, "setup failed, %d\n", ret);
429	}
430
431	i2c_set_clientdata(client, dev);
432
433	return 0;
434
435err_irq:
436	adp5588_irq_teardown(dev);
437err:
438	kfree(dev);
439	return ret;
440}
441
442static int __devexit adp5588_gpio_remove(struct i2c_client *client)
443{
444	struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
 
445	struct adp5588_gpio *dev = i2c_get_clientdata(client);
446	int ret;
447
448	if (pdata->teardown) {
449		ret = pdata->teardown(client,
450				      dev->gpio_chip.base, dev->gpio_chip.ngpio,
451				      pdata->context);
452		if (ret < 0) {
453			dev_err(&client->dev, "teardown failed %d\n", ret);
454			return ret;
455		}
456	}
457
458	if (dev->irq_base)
459		free_irq(dev->client->irq, dev);
460
461	ret = gpiochip_remove(&dev->gpio_chip);
462	if (ret) {
463		dev_err(&client->dev, "gpiochip_remove failed %d\n", ret);
464		return ret;
465	}
466
467	kfree(dev);
468	return 0;
469}
470
471static const struct i2c_device_id adp5588_gpio_id[] = {
472	{DRV_NAME, 0},
473	{}
474};
475
476MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
477
 
 
 
 
 
 
 
 
478static struct i2c_driver adp5588_gpio_driver = {
479	.driver = {
480		   .name = DRV_NAME,
481		   },
482	.probe = adp5588_gpio_probe,
483	.remove = __devexit_p(adp5588_gpio_remove),
 
484	.id_table = adp5588_gpio_id,
485};
486
487static int __init adp5588_gpio_init(void)
488{
489	return i2c_add_driver(&adp5588_gpio_driver);
490}
491
492module_init(adp5588_gpio_init);
493
494static void __exit adp5588_gpio_exit(void)
495{
496	i2c_del_driver(&adp5588_gpio_driver);
497}
498
499module_exit(adp5588_gpio_exit);
500
501MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
502MODULE_DESCRIPTION("GPIO ADP5588 Driver");
503MODULE_LICENSE("GPL");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * GPIO Chip driver for Analog Devices
  4 * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
  5 *
  6 * Copyright 2009-2010 Analog Devices Inc.
 
 
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <linux/slab.h>
 12#include <linux/init.h>
 13#include <linux/i2c.h>
 14#include <linux/gpio/driver.h>
 15#include <linux/interrupt.h>
 16#include <linux/irq.h>
 17#include <linux/of_device.h>
 18
 19#include <linux/platform_data/adp5588.h>
 20
 21#define DRV_NAME	"adp5588-gpio"
 22
 23/*
 24 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
 25 * since the Event Counter Register updated 25ms after the interrupt
 26 * asserted.
 27 */
 28#define WA_DELAYED_READOUT_REVID(rev)	((rev) < 4)
 29
 30struct adp5588_gpio {
 31	struct i2c_client *client;
 32	struct gpio_chip gpio_chip;
 33	struct mutex lock;	/* protect cached dir, dat_out */
 34	/* protect serialized access to the interrupt controller bus */
 35	struct mutex irq_lock;
 
 
 36	uint8_t dat_out[3];
 37	uint8_t dir[3];
 38	uint8_t int_lvl_low[3];
 39	uint8_t int_lvl_high[3];
 40	uint8_t int_en[3];
 41	uint8_t irq_mask[3];
 42	uint8_t int_input_en[3];
 43};
 44
 45static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
 46{
 47	int ret = i2c_smbus_read_byte_data(client, reg);
 48
 49	if (ret < 0)
 50		dev_err(&client->dev, "Read Error\n");
 51
 52	return ret;
 53}
 54
 55static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
 56{
 57	int ret = i2c_smbus_write_byte_data(client, reg, val);
 58
 59	if (ret < 0)
 60		dev_err(&client->dev, "Write Error\n");
 61
 62	return ret;
 63}
 64
 65static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
 66{
 67	struct adp5588_gpio *dev = gpiochip_get_data(chip);
 68	unsigned bank = ADP5588_BANK(off);
 69	unsigned bit = ADP5588_BIT(off);
 70	int val;
 71
 72	mutex_lock(&dev->lock);
 73
 74	if (dev->dir[bank] & bit)
 75		val = dev->dat_out[bank];
 76	else
 77		val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank);
 78
 79	mutex_unlock(&dev->lock);
 80
 81	return !!(val & bit);
 82}
 83
 84static void adp5588_gpio_set_value(struct gpio_chip *chip,
 85				   unsigned off, int val)
 86{
 87	unsigned bank, bit;
 88	struct adp5588_gpio *dev = gpiochip_get_data(chip);
 
 89
 90	bank = ADP5588_BANK(off);
 91	bit = ADP5588_BIT(off);
 92
 93	mutex_lock(&dev->lock);
 94	if (val)
 95		dev->dat_out[bank] |= bit;
 96	else
 97		dev->dat_out[bank] &= ~bit;
 98
 99	adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
100			   dev->dat_out[bank]);
101	mutex_unlock(&dev->lock);
102}
103
104static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
105{
106	int ret;
107	unsigned bank;
108	struct adp5588_gpio *dev = gpiochip_get_data(chip);
 
109
110	bank = ADP5588_BANK(off);
111
112	mutex_lock(&dev->lock);
113	dev->dir[bank] &= ~ADP5588_BIT(off);
114	ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
115	mutex_unlock(&dev->lock);
116
117	return ret;
118}
119
120static int adp5588_gpio_direction_output(struct gpio_chip *chip,
121					 unsigned off, int val)
122{
123	int ret;
124	unsigned bank, bit;
125	struct adp5588_gpio *dev = gpiochip_get_data(chip);
 
126
127	bank = ADP5588_BANK(off);
128	bit = ADP5588_BIT(off);
129
130	mutex_lock(&dev->lock);
131	dev->dir[bank] |= bit;
132
133	if (val)
134		dev->dat_out[bank] |= bit;
135	else
136		dev->dat_out[bank] &= ~bit;
137
138	ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
139				 dev->dat_out[bank]);
140	ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
141				 dev->dir[bank]);
142	mutex_unlock(&dev->lock);
143
144	return ret;
145}
146
147#ifdef CONFIG_GPIO_ADP5588_IRQ
 
 
 
 
 
 
148
149static void adp5588_irq_bus_lock(struct irq_data *d)
150{
151	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
152	struct adp5588_gpio *dev = gpiochip_get_data(gc);
153
154	mutex_lock(&dev->irq_lock);
155}
156
157 /*
158  * genirq core code can issue chip->mask/unmask from atomic context.
159  * This doesn't work for slow busses where an access needs to sleep.
160  * bus_sync_unlock() is therefore called outside the atomic context,
161  * syncs the current irq mask state with the slow external controller
162  * and unlocks the bus.
163  */
164
165static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
166{
167	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
168	struct adp5588_gpio *dev = gpiochip_get_data(gc);
169	int i;
170
171	for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
172		if (dev->int_input_en[i]) {
173			mutex_lock(&dev->lock);
174			dev->dir[i] &= ~dev->int_input_en[i];
175			dev->int_input_en[i] = 0;
176			adp5588_gpio_write(dev->client, GPIO_DIR1 + i,
177					   dev->dir[i]);
178			mutex_unlock(&dev->lock);
179		}
180
181		if (dev->int_en[i] ^ dev->irq_mask[i]) {
182			dev->int_en[i] = dev->irq_mask[i];
183			adp5588_gpio_write(dev->client, GPI_EM1 + i,
184					   dev->int_en[i]);
185		}
186	}
187
188	mutex_unlock(&dev->irq_lock);
189}
190
191static void adp5588_irq_mask(struct irq_data *d)
192{
193	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
194	struct adp5588_gpio *dev = gpiochip_get_data(gc);
195
196	dev->irq_mask[ADP5588_BANK(d->hwirq)] &= ~ADP5588_BIT(d->hwirq);
197}
198
199static void adp5588_irq_unmask(struct irq_data *d)
200{
201	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
202	struct adp5588_gpio *dev = gpiochip_get_data(gc);
203
204	dev->irq_mask[ADP5588_BANK(d->hwirq)] |= ADP5588_BIT(d->hwirq);
205}
206
207static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
208{
209	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
210	struct adp5588_gpio *dev = gpiochip_get_data(gc);
211	uint16_t gpio = d->hwirq;
212	unsigned bank, bit;
213
 
 
 
 
 
 
214	bank = ADP5588_BANK(gpio);
215	bit = ADP5588_BIT(gpio);
216
217	dev->int_lvl_low[bank] &= ~bit;
218	dev->int_lvl_high[bank] &= ~bit;
219
220	if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_HIGH)
221		dev->int_lvl_high[bank] |= bit;
222
223	if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_LOW)
224		dev->int_lvl_low[bank] |= bit;
225
226	dev->int_input_en[bank] |= bit;
 
 
227
228	return 0;
229}
230
231static struct irq_chip adp5588_irq_chip = {
232	.name			= "adp5588",
233	.irq_mask		= adp5588_irq_mask,
234	.irq_unmask		= adp5588_irq_unmask,
235	.irq_bus_lock		= adp5588_irq_bus_lock,
236	.irq_bus_sync_unlock	= adp5588_irq_bus_sync_unlock,
237	.irq_set_type		= adp5588_irq_set_type,
238};
239
 
 
 
 
 
 
 
 
 
 
240static irqreturn_t adp5588_irq_handler(int irq, void *devid)
241{
242	struct adp5588_gpio *dev = devid;
243	int status = adp5588_gpio_read(dev->client, INT_STAT);
 
 
244
245	if (status & ADP5588_KE_INT) {
246		int ev_cnt = adp5588_gpio_read(dev->client, KEY_LCK_EC_STAT);
 
 
247
248		if (ev_cnt > 0) {
249			int i;
 
 
 
 
 
 
 
250
251			for (i = 0; i < (ev_cnt & ADP5588_KEC); i++) {
252				int key = adp5588_gpio_read(dev->client,
253							    Key_EVENTA + i);
254				/* GPIN events begin at 97,
255				 * bit 7 indicates logic level
256				 */
257				int gpio = (key & 0x7f) - 97;
258				int lvl = key & (1 << 7);
259				int bank = ADP5588_BANK(gpio);
260				int bit = ADP5588_BIT(gpio);
261
262				if ((lvl && dev->int_lvl_high[bank] & bit) ||
263				    (!lvl && dev->int_lvl_low[bank] & bit))
264					handle_nested_irq(irq_find_mapping(
265					      dev->gpio_chip.irq.domain, gpio));
266			}
267		}
268	}
269
270	adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
271
272	return IRQ_HANDLED;
273}
274
275static int adp5588_irq_setup(struct adp5588_gpio *dev)
276{
277	struct i2c_client *client = dev->client;
 
 
278	int ret;
279	struct adp5588_gpio_platform_data *pdata =
280			dev_get_platdata(&client->dev);
281	int irq_base = pdata ? pdata->irq_base : 0;
282
283	adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
284	adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
 
285
 
286	mutex_init(&dev->irq_lock);
287
288	ret = devm_request_threaded_irq(&client->dev, client->irq,
289					NULL, adp5588_irq_handler, IRQF_ONESHOT
290					| IRQF_TRIGGER_FALLING | IRQF_SHARED,
291					dev_name(&client->dev), dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292	if (ret) {
293		dev_err(&client->dev, "failed to request irq %d\n",
294			client->irq);
295		return ret;
296	}
297	ret = gpiochip_irqchip_add_nested(&dev->gpio_chip,
298					  &adp5588_irq_chip, irq_base,
299					  handle_simple_irq,
300					  IRQ_TYPE_NONE);
301	if (ret) {
302		dev_err(&client->dev,
303			"could not connect irqchip to gpiochip\n");
304		return ret;
305	}
306	gpiochip_set_nested_irqchip(&dev->gpio_chip,
307				    &adp5588_irq_chip,
308				    client->irq);
309
 
310	adp5588_gpio_write(client, CFG,
311		ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_KE_IEN);
312
313	return 0;
 
 
 
 
 
 
 
 
 
 
314}
315
316#else
317static int adp5588_irq_setup(struct adp5588_gpio *dev)
318{
319	struct i2c_client *client = dev->client;
320	dev_warn(&client->dev, "interrupt support not compiled in\n");
321
322	return 0;
323}
324
 
 
 
325#endif /* CONFIG_GPIO_ADP5588_IRQ */
326
327static int adp5588_gpio_probe(struct i2c_client *client)
 
328{
329	struct adp5588_gpio_platform_data *pdata =
330			dev_get_platdata(&client->dev);
331	struct adp5588_gpio *dev;
332	struct gpio_chip *gc;
333	int ret, i, revid;
334	unsigned int pullup_dis_mask = 0;
 
 
 
 
335
336	if (!i2c_check_functionality(client->adapter,
337					I2C_FUNC_SMBUS_BYTE_DATA)) {
338		dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
339		return -EIO;
340	}
341
342	dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
343	if (!dev)
 
344		return -ENOMEM;
 
345
346	dev->client = client;
347
348	gc = &dev->gpio_chip;
349	gc->direction_input = adp5588_gpio_direction_input;
350	gc->direction_output = adp5588_gpio_direction_output;
351	gc->get = adp5588_gpio_get_value;
352	gc->set = adp5588_gpio_set_value;
353	gc->can_sleep = true;
354	gc->base = -1;
355	gc->parent = &client->dev;
356
357	if (pdata) {
358		gc->base = pdata->gpio_start;
359		gc->names = pdata->names;
360		pullup_dis_mask = pdata->pullup_dis_mask;
361	}
362
 
363	gc->ngpio = ADP5588_MAXGPIO;
364	gc->label = client->name;
365	gc->owner = THIS_MODULE;
366
367	mutex_init(&dev->lock);
368
369	ret = adp5588_gpio_read(dev->client, DEV_ID);
370	if (ret < 0)
371		return ret;
372
373	revid = ret & ADP5588_DEVICE_ID_MASK;
374
375	for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
376		dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
377		dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
378		ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
379		ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
380				(pullup_dis_mask >> (8 * i)) & 0xFF);
381		ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
382		if (ret)
383			return ret;
384	}
385
386	if (client->irq) {
387		if (WA_DELAYED_READOUT_REVID(revid)) {
388			dev_warn(&client->dev, "GPIO int not supported\n");
389		} else {
390			ret = adp5588_irq_setup(dev);
391			if (ret)
392				return ret;
393		}
394	}
395
396	ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev);
397	if (ret)
398		return ret;
 
 
 
 
399
400	if (pdata && pdata->setup) {
401		ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
402		if (ret < 0)
403			dev_warn(&client->dev, "setup failed, %d\n", ret);
404	}
405
406	i2c_set_clientdata(client, dev);
407
408	return 0;
 
 
 
 
 
 
409}
410
411static int adp5588_gpio_remove(struct i2c_client *client)
412{
413	struct adp5588_gpio_platform_data *pdata =
414			dev_get_platdata(&client->dev);
415	struct adp5588_gpio *dev = i2c_get_clientdata(client);
416	int ret;
417
418	if (pdata && pdata->teardown) {
419		ret = pdata->teardown(client,
420				      dev->gpio_chip.base, dev->gpio_chip.ngpio,
421				      pdata->context);
422		if (ret < 0) {
423			dev_err(&client->dev, "teardown failed %d\n", ret);
424			return ret;
425		}
426	}
427
428	if (dev->client->irq)
429		free_irq(dev->client->irq, dev);
430
 
 
 
 
 
 
 
431	return 0;
432}
433
434static const struct i2c_device_id adp5588_gpio_id[] = {
435	{DRV_NAME, 0},
436	{}
437};
 
438MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
439
440#ifdef CONFIG_OF
441static const struct of_device_id adp5588_gpio_of_id[] = {
442	{ .compatible = "adi," DRV_NAME, },
443	{},
444};
445MODULE_DEVICE_TABLE(of, adp5588_gpio_of_id);
446#endif
447
448static struct i2c_driver adp5588_gpio_driver = {
449	.driver = {
450		.name = DRV_NAME,
451		.of_match_table = of_match_ptr(adp5588_gpio_of_id),
452	},
453	.probe_new = adp5588_gpio_probe,
454	.remove = adp5588_gpio_remove,
455	.id_table = adp5588_gpio_id,
456};
457
458module_i2c_driver(adp5588_gpio_driver);
 
 
 
 
 
 
 
 
 
 
 
 
459
460MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
461MODULE_DESCRIPTION("GPIO ADP5588 Driver");
462MODULE_LICENSE("GPL");