drivers/gpio/gpio-pca9570.c

Source file repositories/reference/linux-study-clean/drivers/gpio/gpio-pca9570.c

File Facts

System
Linux kernel
Corpus path
drivers/gpio/gpio-pca9570.c
Extension
.c
Size
4529 bytes
Lines
194
Domain
Driver Families
Bucket
drivers/gpio
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct pca9570_chip_data {
	u16 ngpio;
	u32 command;
};

/**
 * struct pca9570 - GPIO driver data
 * @chip: GPIO controller chip
 * @chip_data: GPIO controller platform data
 * @lock: Protects write sequences
 * @out: Buffer for device register
 */
struct pca9570 {
	struct gpio_chip chip;
	const struct pca9570_chip_data *chip_data;
	struct mutex lock;
	u8 out;
};

static int pca9570_read(struct pca9570 *gpio, u8 *value)
{
	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
	int ret;

	if (gpio->chip_data->command != 0)
		ret = i2c_smbus_read_byte_data(client, gpio->chip_data->command);
	else
		ret = i2c_smbus_read_byte(client);

	if (ret < 0)
		return ret;

	*value = ret;
	return 0;
}

static int pca9570_write(struct pca9570 *gpio, u8 value)
{
	struct i2c_client *client = to_i2c_client(gpio->chip.parent);

	if (gpio->chip_data->command != 0)
		return i2c_smbus_write_byte_data(client, gpio->chip_data->command, value);

	return i2c_smbus_write_byte(client, value);
}

static int pca9570_get_direction(struct gpio_chip *chip,
				 unsigned offset)
{
	/* This device always output */
	return GPIO_LINE_DIRECTION_OUT;
}

static int pca9570_get(struct gpio_chip *chip, unsigned offset)
{
	struct pca9570 *gpio = gpiochip_get_data(chip);
	u8 buffer;
	int ret;

	ret = pca9570_read(gpio, &buffer);
	if (ret)
		return ret;

	return !!(buffer & BIT(offset));
}

static int pca9570_set(struct gpio_chip *chip, unsigned int offset, int value)
{
	struct pca9570 *gpio = gpiochip_get_data(chip);
	u8 buffer;
	int ret;

	guard(mutex)(&gpio->lock);

	buffer = gpio->out;
	if (value)
		buffer |= BIT(offset);
	else
		buffer &= ~BIT(offset);

	ret = pca9570_write(gpio, buffer);
	if (ret)
		return ret;

	gpio->out = buffer;

	return 0;
}

static int pca9570_probe(struct i2c_client *client)

Annotation

Implementation Notes