/* * The pcf857x, pca857x, and pca967x chips only expose one read and one * write register. Writing a "one" bit (to match the reset state) lets * that pin be used as an input; it's not an open-drain model, but acts * a bit like one. This is described as "quasi-bidirectional"; read the * chip documentation for details. * * Many other I2C GPIO expander chips (like the pca953x models) have * more complex register models and more conventional circuitry using * push/pull drivers. They often use the same 0x20..0x27 addresses as * pcf857x parts, making the "legacy" I2C driver model problematic.
*/ struct pcf857x { struct gpio_chip chip; struct i2c_client *client; struct mutex lock; /* protect 'out' */ unsignedint out; /* software latch */ unsignedint status; /* current status */ unsignedint irq_enabled; /* enabled irqs */
int (*write)(struct i2c_client *client, unsignedint data); int (*read)(struct i2c_client *client);
};
/* * call the interrupt handler iff gpio is used as * interrupt source, just to avoid bad irqs
*/
mutex_lock(&gpio->lock);
change = (gpio->status ^ status) & gpio->irq_enabled;
gpio->status = status;
mutex_unlock(&gpio->lock);
reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH); if (IS_ERR(reset_gpio)) return dev_err_probe(&client->dev, PTR_ERR(reset_gpio), "failed to get reset GPIO\n");
if (reset_gpio) { /* Reset already held with devm_gpiod_get_optional with GPIOD_OUT_HIGH */
fsleep(4); /* tw(rst) > 4us */
gpiod_set_value_cansleep(reset_gpio, 0);
fsleep(100); /* trst > 100uS */
/* * Performing a reset means "The PCA9670 registers and I2C-bus * state machine will be held in their default state until the * RESET input is once again HIGH". * * This is the same as writing 1 for all pins, which is the same * as n_latch=0, the default value of the variable.
*/
} else {
device_property_read_u32(&client->dev, "lines-initial-states",
&n_latch);
}
/* NOTE: the OnSemi jlc1562b is also largely compatible with * these parts, notably for output. It has a low-resolution * DAC instead of pin change IRQs; and its inputs can be the * result of comparators.
*/
/* 8574 addresses are 0x20..0x27; 8574a uses 0x38..0x3f; * 9670, 9672, 9764, and 9764a use quite a variety. * * NOTE: we don't distinguish here between *4 and *4a parts.
*/ if (gpio->chip.ngpio == 8) {
gpio->write = i2c_write_le8;
gpio->read = i2c_read_le8;
if (!i2c_check_functionality(client->adapter,
I2C_FUNC_SMBUS_BYTE))
status = -EIO;
/* fail if there's no chip present */ else
status = i2c_smbus_read_byte(client);
/* '75/'75c addresses are 0x20..0x27, just like the '74; * the '75c doesn't have a current source pulling high. * 9671, 9673, and 9765 use quite a variety of addresses. * * NOTE: we don't distinguish here between '75 and '75c parts.
*/
} elseif (gpio->chip.ngpio == 16) {
gpio->write = i2c_write_le16;
gpio->read = i2c_read_le16;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
status = -EIO;
/* fail if there's no chip present */ else
status = i2c_read_le16(client);
} else {
dev_dbg(&client->dev, "unsupported number of gpios\n");
status = -EINVAL;
}
/* NOTE: these chips have strange "quasi-bidirectional" I/O pins. * We can't actually know whether a pin is configured (a) as output * and driving the signal low, or (b) as input and reporting a low * value ... without knowing the last value written since the chip * came out of reset (if any). We can't read the latched output. * * In short, the only reliable solution for setting up pin direction * is to do it explicitly. The setup() method can do that, but it * may cause transient glitching since it can't know the last value * written (some pins may need to be driven low). * * Using n_latch avoids that trouble. When left initialized to zero, * our software copy of the "latch" then matches the chip's all-ones * reset state. Otherwise it flags pins to be driven low.
*/
gpio->out = ~n_latch;
gpio->status = gpio->read(gpio->client);
/* Enable irqchip if we have an interrupt */ if (client->irq) { struct gpio_irq_chip *girq;
status = devm_request_threaded_irq(&client->dev, client->irq,
NULL, pcf857x_irq, IRQF_ONESHOT |
IRQF_TRIGGER_FALLING | IRQF_SHARED,
dev_name(&client->dev), gpio); if (status) goto fail;
girq = &gpio->chip.irq;
gpio_irq_chip_set_chip(girq, &pcf857x_irq_chip); /* This will let us handle the parent IRQ in the driver */
girq->parent_handler = NULL;
girq->num_parents = 0;
girq->parents = NULL;
girq->default_type = IRQ_TYPE_NONE;
girq->handler = handle_level_irq;
girq->threaded = true;
}
status = devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio); if (status < 0) goto fail;
dev_info(&client->dev, "probed\n");
return 0;
fail:
dev_dbg(&client->dev, "probe error %d for '%s'\n", status,
client->name);
staticint __init pcf857x_init(void)
{ return i2c_add_driver(&pcf857x_driver);
} /* register after i2c postcore initcall and before * subsys initcalls that may rely on these GPIOs
*/
subsys_initcall(pcf857x_init);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.