2012-09-18 16:57:10 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2012 Avionic Design GmbH
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2014-06-02 21:17:54 +08:00
|
|
|
#include <linux/gpio/driver.h>
|
2012-09-18 16:57:10 +08:00
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of_irq.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
#define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift)
|
|
|
|
#define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift)
|
|
|
|
#define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift)
|
|
|
|
#define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift)
|
|
|
|
#define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift)
|
|
|
|
|
|
|
|
struct adnp {
|
|
|
|
struct i2c_client *client;
|
|
|
|
struct gpio_chip gpio;
|
|
|
|
unsigned int reg_shift;
|
|
|
|
|
|
|
|
struct mutex i2c_lock;
|
|
|
|
struct mutex irq_lock;
|
|
|
|
|
|
|
|
u8 *irq_enable;
|
|
|
|
u8 *irq_level;
|
|
|
|
u8 *irq_rise;
|
|
|
|
u8 *irq_fall;
|
|
|
|
u8 *irq_high;
|
|
|
|
u8 *irq_low;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int adnp_read(struct adnp *adnp, unsigned offset, uint8_t *value)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = i2c_smbus_read_byte_data(adnp->client, offset);
|
|
|
|
if (err < 0) {
|
2015-11-04 16:56:26 +08:00
|
|
|
dev_err(adnp->gpio.parent, "%s failed: %d\n",
|
2012-09-18 16:57:10 +08:00
|
|
|
"i2c_smbus_read_byte_data()", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int adnp_write(struct adnp *adnp, unsigned offset, uint8_t value)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = i2c_smbus_write_byte_data(adnp->client, offset, value);
|
|
|
|
if (err < 0) {
|
2015-11-04 16:56:26 +08:00
|
|
|
dev_err(adnp->gpio.parent, "%s failed: %d\n",
|
2012-09-18 16:57:10 +08:00
|
|
|
"i2c_smbus_write_byte_data()", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int adnp_gpio_get(struct gpio_chip *chip, unsigned offset)
|
|
|
|
{
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(chip);
|
2012-09-18 16:57:10 +08:00
|
|
|
unsigned int reg = offset >> adnp->reg_shift;
|
|
|
|
unsigned int pos = offset & 7;
|
|
|
|
u8 value;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &value);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return (value & BIT(pos)) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __adnp_gpio_set(struct adnp *adnp, unsigned offset, int value)
|
|
|
|
{
|
|
|
|
unsigned int reg = offset >> adnp->reg_shift;
|
|
|
|
unsigned int pos = offset & 7;
|
|
|
|
int err;
|
|
|
|
u8 val;
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &val);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
val |= BIT(pos);
|
|
|
|
else
|
|
|
|
val &= ~BIT(pos);
|
|
|
|
|
|
|
|
adnp_write(adnp, GPIO_PLR(adnp) + reg, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adnp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
|
|
|
|
{
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(chip);
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
mutex_lock(&adnp->i2c_lock);
|
|
|
|
__adnp_gpio_set(adnp, offset, value);
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int adnp_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
|
|
|
|
{
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(chip);
|
2012-09-18 16:57:10 +08:00
|
|
|
unsigned int reg = offset >> adnp->reg_shift;
|
|
|
|
unsigned int pos = offset & 7;
|
|
|
|
u8 value;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mutex_lock(&adnp->i2c_lock);
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
value &= ~BIT(pos);
|
|
|
|
|
|
|
|
err = adnp_write(adnp, GPIO_DDR(adnp) + reg, value);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
2019-03-11 21:29:37 +08:00
|
|
|
if (value & BIT(pos)) {
|
|
|
|
err = -EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int adnp_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
|
|
|
|
int value)
|
|
|
|
{
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(chip);
|
2012-09-18 16:57:10 +08:00
|
|
|
unsigned int reg = offset >> adnp->reg_shift;
|
|
|
|
unsigned int pos = offset & 7;
|
|
|
|
int err;
|
|
|
|
u8 val;
|
|
|
|
|
|
|
|
mutex_lock(&adnp->i2c_lock);
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
val |= BIT(pos);
|
|
|
|
|
|
|
|
err = adnp_write(adnp, GPIO_DDR(adnp) + reg, val);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!(val & BIT(pos))) {
|
|
|
|
err = -EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
__adnp_gpio_set(adnp, offset, value);
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adnp_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
|
|
|
|
{
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(chip);
|
2012-09-18 16:57:10 +08:00
|
|
|
unsigned int num_regs = 1 << adnp->reg_shift, i, j;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
for (i = 0; i < num_regs; i++) {
|
|
|
|
u8 ddr, plr, ier, isr;
|
|
|
|
|
|
|
|
mutex_lock(&adnp->i2c_lock);
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_DDR(adnp) + i, &ddr);
|
2017-10-23 02:21:55 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto unlock;
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_PLR(adnp) + i, &plr);
|
2017-10-23 02:21:55 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto unlock;
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
|
2017-10-23 02:21:55 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto unlock;
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
|
2017-10-23 02:21:55 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto unlock;
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
|
|
|
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
unsigned int bit = (i << adnp->reg_shift) + j;
|
|
|
|
const char *direction = "input ";
|
|
|
|
const char *level = "low ";
|
|
|
|
const char *interrupt = "disabled";
|
|
|
|
const char *pending = "";
|
|
|
|
|
|
|
|
if (ddr & BIT(j))
|
|
|
|
direction = "output";
|
|
|
|
|
|
|
|
if (plr & BIT(j))
|
|
|
|
level = "high";
|
|
|
|
|
|
|
|
if (ier & BIT(j))
|
|
|
|
interrupt = "enabled ";
|
|
|
|
|
|
|
|
if (isr & BIT(j))
|
|
|
|
pending = "pending";
|
|
|
|
|
|
|
|
seq_printf(s, "%2u: %s %s IRQ %s %s\n", bit,
|
|
|
|
direction, level, interrupt, pending);
|
|
|
|
}
|
|
|
|
}
|
2017-10-23 02:21:55 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
unlock:
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
2012-09-18 16:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios)
|
|
|
|
{
|
|
|
|
struct gpio_chip *chip = &adnp->gpio;
|
2014-06-02 21:17:54 +08:00
|
|
|
int err;
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
adnp->reg_shift = get_count_order(num_gpios) - 3;
|
|
|
|
|
|
|
|
chip->direction_input = adnp_gpio_direction_input;
|
|
|
|
chip->direction_output = adnp_gpio_direction_output;
|
|
|
|
chip->get = adnp_gpio_get;
|
|
|
|
chip->set = adnp_gpio_set;
|
2013-12-04 21:42:46 +08:00
|
|
|
chip->can_sleep = true;
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_DEBUG_FS))
|
|
|
|
chip->dbg_show = adnp_gpio_dbg_show;
|
|
|
|
|
|
|
|
chip->base = -1;
|
|
|
|
chip->ngpio = num_gpios;
|
|
|
|
chip->label = adnp->client->name;
|
2015-11-04 16:56:26 +08:00
|
|
|
chip->parent = &adnp->client->dev;
|
|
|
|
chip->of_node = chip->parent->of_node;
|
2012-09-18 16:57:10 +08:00
|
|
|
chip->owner = THIS_MODULE;
|
|
|
|
|
2016-02-22 20:13:28 +08:00
|
|
|
err = devm_gpiochip_add_data(&adnp->client->dev, chip, adnp);
|
2014-06-02 21:17:54 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2012-09-18 16:57:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t adnp_irq(int irq, void *data)
|
|
|
|
{
|
|
|
|
struct adnp *adnp = data;
|
|
|
|
unsigned int num_regs, i;
|
|
|
|
|
|
|
|
num_regs = 1 << adnp->reg_shift;
|
|
|
|
|
|
|
|
for (i = 0; i < num_regs; i++) {
|
|
|
|
unsigned int base = i << adnp->reg_shift, bit;
|
|
|
|
u8 changed, level, isr, ier;
|
|
|
|
unsigned long pending;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mutex_lock(&adnp->i2c_lock);
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_PLR(adnp) + i, &level);
|
|
|
|
if (err < 0) {
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
|
|
|
|
if (err < 0) {
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
|
|
|
|
if (err < 0) {
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
|
|
|
|
|
|
|
/* determine pins that changed levels */
|
|
|
|
changed = level ^ adnp->irq_level[i];
|
|
|
|
|
|
|
|
/* compute edge-triggered interrupts */
|
|
|
|
pending = changed & ((adnp->irq_fall[i] & ~level) |
|
|
|
|
(adnp->irq_rise[i] & level));
|
|
|
|
|
|
|
|
/* add in level-triggered interrupts */
|
|
|
|
pending |= (adnp->irq_high[i] & level) |
|
|
|
|
(adnp->irq_low[i] & ~level);
|
|
|
|
|
|
|
|
/* mask out non-pending and disabled interrupts */
|
|
|
|
pending &= isr & ier;
|
|
|
|
|
|
|
|
for_each_set_bit(bit, &pending, 8) {
|
2013-10-12 01:10:00 +08:00
|
|
|
unsigned int child_irq;
|
2017-11-08 02:15:47 +08:00
|
|
|
child_irq = irq_find_mapping(adnp->gpio.irq.domain,
|
2014-06-02 21:17:54 +08:00
|
|
|
base + bit);
|
2013-10-12 01:10:00 +08:00
|
|
|
handle_nested_irq(child_irq);
|
2012-09-18 16:57:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2014-06-02 21:17:54 +08:00
|
|
|
static void adnp_irq_mask(struct irq_data *d)
|
2012-09-18 16:57:10 +08:00
|
|
|
{
|
2014-06-02 21:17:54 +08:00
|
|
|
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(gc);
|
2014-06-02 21:17:54 +08:00
|
|
|
unsigned int reg = d->hwirq >> adnp->reg_shift;
|
|
|
|
unsigned int pos = d->hwirq & 7;
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
adnp->irq_enable[reg] &= ~BIT(pos);
|
|
|
|
}
|
|
|
|
|
2014-06-02 21:17:54 +08:00
|
|
|
static void adnp_irq_unmask(struct irq_data *d)
|
2012-09-18 16:57:10 +08:00
|
|
|
{
|
2014-06-02 21:17:54 +08:00
|
|
|
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(gc);
|
2014-06-02 21:17:54 +08:00
|
|
|
unsigned int reg = d->hwirq >> adnp->reg_shift;
|
|
|
|
unsigned int pos = d->hwirq & 7;
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
adnp->irq_enable[reg] |= BIT(pos);
|
|
|
|
}
|
|
|
|
|
2014-06-02 21:17:54 +08:00
|
|
|
static int adnp_irq_set_type(struct irq_data *d, unsigned int type)
|
2012-09-18 16:57:10 +08:00
|
|
|
{
|
2014-06-02 21:17:54 +08:00
|
|
|
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(gc);
|
2014-06-02 21:17:54 +08:00
|
|
|
unsigned int reg = d->hwirq >> adnp->reg_shift;
|
|
|
|
unsigned int pos = d->hwirq & 7;
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
if (type & IRQ_TYPE_EDGE_RISING)
|
|
|
|
adnp->irq_rise[reg] |= BIT(pos);
|
|
|
|
else
|
|
|
|
adnp->irq_rise[reg] &= ~BIT(pos);
|
|
|
|
|
|
|
|
if (type & IRQ_TYPE_EDGE_FALLING)
|
|
|
|
adnp->irq_fall[reg] |= BIT(pos);
|
|
|
|
else
|
|
|
|
adnp->irq_fall[reg] &= ~BIT(pos);
|
|
|
|
|
|
|
|
if (type & IRQ_TYPE_LEVEL_HIGH)
|
|
|
|
adnp->irq_high[reg] |= BIT(pos);
|
|
|
|
else
|
|
|
|
adnp->irq_high[reg] &= ~BIT(pos);
|
|
|
|
|
|
|
|
if (type & IRQ_TYPE_LEVEL_LOW)
|
|
|
|
adnp->irq_low[reg] |= BIT(pos);
|
|
|
|
else
|
|
|
|
adnp->irq_low[reg] &= ~BIT(pos);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-02 21:17:54 +08:00
|
|
|
static void adnp_irq_bus_lock(struct irq_data *d)
|
2012-09-18 16:57:10 +08:00
|
|
|
{
|
2014-06-02 21:17:54 +08:00
|
|
|
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(gc);
|
2012-09-18 16:57:10 +08:00
|
|
|
|
|
|
|
mutex_lock(&adnp->irq_lock);
|
|
|
|
}
|
|
|
|
|
2014-06-02 21:17:54 +08:00
|
|
|
static void adnp_irq_bus_unlock(struct irq_data *d)
|
2012-09-18 16:57:10 +08:00
|
|
|
{
|
2014-06-02 21:17:54 +08:00
|
|
|
struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
|
2015-12-04 21:56:15 +08:00
|
|
|
struct adnp *adnp = gpiochip_get_data(gc);
|
2012-09-18 16:57:10 +08:00
|
|
|
unsigned int num_regs = 1 << adnp->reg_shift, i;
|
|
|
|
|
|
|
|
mutex_lock(&adnp->i2c_lock);
|
|
|
|
|
|
|
|
for (i = 0; i < num_regs; i++)
|
|
|
|
adnp_write(adnp, GPIO_IER(adnp) + i, adnp->irq_enable[i]);
|
|
|
|
|
|
|
|
mutex_unlock(&adnp->i2c_lock);
|
|
|
|
mutex_unlock(&adnp->irq_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct irq_chip adnp_irq_chip = {
|
|
|
|
.name = "gpio-adnp",
|
|
|
|
.irq_mask = adnp_irq_mask,
|
|
|
|
.irq_unmask = adnp_irq_unmask,
|
|
|
|
.irq_set_type = adnp_irq_set_type,
|
|
|
|
.irq_bus_lock = adnp_irq_bus_lock,
|
|
|
|
.irq_bus_sync_unlock = adnp_irq_bus_unlock,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int adnp_irq_setup(struct adnp *adnp)
|
|
|
|
{
|
|
|
|
unsigned int num_regs = 1 << adnp->reg_shift, i;
|
|
|
|
struct gpio_chip *chip = &adnp->gpio;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mutex_init(&adnp->irq_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate memory to keep track of the current level and trigger
|
|
|
|
* modes of the interrupts. To avoid multiple allocations, a single
|
|
|
|
* large buffer is allocated and pointers are setup to point at the
|
|
|
|
* corresponding offsets. For consistency, the layout of the buffer
|
|
|
|
* is chosen to match the register layout of the hardware in that
|
|
|
|
* each segment contains the corresponding bits for all interrupts.
|
|
|
|
*/
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:07:58 +08:00
|
|
|
adnp->irq_enable = devm_kcalloc(chip->parent, num_regs, 6,
|
2015-11-04 16:56:26 +08:00
|
|
|
GFP_KERNEL);
|
2012-09-18 16:57:10 +08:00
|
|
|
if (!adnp->irq_enable)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
adnp->irq_level = adnp->irq_enable + (num_regs * 1);
|
|
|
|
adnp->irq_rise = adnp->irq_enable + (num_regs * 2);
|
|
|
|
adnp->irq_fall = adnp->irq_enable + (num_regs * 3);
|
|
|
|
adnp->irq_high = adnp->irq_enable + (num_regs * 4);
|
|
|
|
adnp->irq_low = adnp->irq_enable + (num_regs * 5);
|
|
|
|
|
|
|
|
for (i = 0; i < num_regs; i++) {
|
|
|
|
/*
|
|
|
|
* Read the initial level of all pins to allow the emulation
|
|
|
|
* of edge triggered interrupts.
|
|
|
|
*/
|
|
|
|
err = adnp_read(adnp, GPIO_PLR(adnp) + i, &adnp->irq_level[i]);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* disable all interrupts */
|
|
|
|
err = adnp_write(adnp, GPIO_IER(adnp) + i, 0);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
adnp->irq_enable[i] = 0x00;
|
|
|
|
}
|
|
|
|
|
2015-11-04 16:56:26 +08:00
|
|
|
err = devm_request_threaded_irq(chip->parent, adnp->client->irq,
|
2014-06-02 21:17:54 +08:00
|
|
|
NULL, adnp_irq,
|
|
|
|
IRQF_TRIGGER_RISING | IRQF_ONESHOT,
|
2015-11-04 16:56:26 +08:00
|
|
|
dev_name(chip->parent), adnp);
|
2012-09-18 16:57:10 +08:00
|
|
|
if (err != 0) {
|
2015-11-04 16:56:26 +08:00
|
|
|
dev_err(chip->parent, "can't request IRQ#%d: %d\n",
|
2012-09-18 16:57:10 +08:00
|
|
|
adnp->client->irq, err);
|
2013-08-07 23:23:58 +08:00
|
|
|
return err;
|
2012-09-18 16:57:10 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 17:57:25 +08:00
|
|
|
err = gpiochip_irqchip_add_nested(chip,
|
|
|
|
&adnp_irq_chip,
|
|
|
|
0,
|
|
|
|
handle_simple_irq,
|
|
|
|
IRQ_TYPE_NONE);
|
2014-06-02 21:17:54 +08:00
|
|
|
if (err) {
|
2015-11-04 16:56:26 +08:00
|
|
|
dev_err(chip->parent,
|
2014-06-02 21:17:54 +08:00
|
|
|
"could not connect irqchip to gpiochip\n");
|
|
|
|
return err;
|
2012-09-18 16:57:10 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 20:27:54 +08:00
|
|
|
gpiochip_set_nested_irqchip(chip, &adnp_irq_chip, adnp->client->irq);
|
|
|
|
|
2014-06-02 21:17:54 +08:00
|
|
|
return 0;
|
2012-09-18 16:57:10 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 02:22:34 +08:00
|
|
|
static int adnp_i2c_probe(struct i2c_client *client,
|
2012-09-18 16:57:10 +08:00
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
|
|
|
struct device_node *np = client->dev.of_node;
|
|
|
|
struct adnp *adnp;
|
|
|
|
u32 num_gpios;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = of_property_read_u32(np, "nr-gpios", &num_gpios);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
client->irq = irq_of_parse_and_map(np, 0);
|
|
|
|
if (!client->irq)
|
|
|
|
return -EPROBE_DEFER;
|
|
|
|
|
|
|
|
adnp = devm_kzalloc(&client->dev, sizeof(*adnp), GFP_KERNEL);
|
|
|
|
if (!adnp)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mutex_init(&adnp->i2c_lock);
|
|
|
|
adnp->client = client;
|
|
|
|
|
|
|
|
err = adnp_gpio_setup(adnp, num_gpios);
|
2014-06-02 21:17:54 +08:00
|
|
|
if (err)
|
2012-09-18 16:57:10 +08:00
|
|
|
return err;
|
|
|
|
|
|
|
|
if (of_find_property(np, "interrupt-controller", NULL)) {
|
|
|
|
err = adnp_irq_setup(adnp);
|
2014-06-02 21:17:54 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
2012-09-18 16:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
i2c_set_clientdata(client, adnp);
|
|
|
|
|
2014-06-02 21:17:54 +08:00
|
|
|
return 0;
|
2012-09-18 16:57:10 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 02:25:05 +08:00
|
|
|
static const struct i2c_device_id adnp_i2c_id[] = {
|
2012-09-18 16:57:10 +08:00
|
|
|
{ "gpio-adnp" },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, adnp_i2c_id);
|
|
|
|
|
2012-11-20 02:25:05 +08:00
|
|
|
static const struct of_device_id adnp_of_match[] = {
|
2012-09-18 16:57:10 +08:00
|
|
|
{ .compatible = "ad,gpio-adnp", },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, adnp_of_match);
|
|
|
|
|
|
|
|
static struct i2c_driver adnp_i2c_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "gpio-adnp",
|
2013-09-28 20:04:07 +08:00
|
|
|
.of_match_table = adnp_of_match,
|
2012-09-18 16:57:10 +08:00
|
|
|
},
|
|
|
|
.probe = adnp_i2c_probe,
|
|
|
|
.id_table = adnp_i2c_id,
|
|
|
|
};
|
|
|
|
module_i2c_driver(adnp_i2c_driver);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander");
|
|
|
|
MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
|
|
|
|
MODULE_LICENSE("GPL");
|