18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for PCA9570 I2C GPO expander
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2020 Sungbo Eo <mans0n@gorani.run>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on gpio-tpic2810.c
88c2ecf20Sopenharmony_ci * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
98c2ecf20Sopenharmony_ci *	Andrew F. Davis <afd@ti.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
138c2ecf20Sopenharmony_ci#include <linux/i2c.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/mutex.h>
168c2ecf20Sopenharmony_ci#include <linux/property.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/**
198c2ecf20Sopenharmony_ci * struct pca9570 - GPIO driver data
208c2ecf20Sopenharmony_ci * @chip: GPIO controller chip
218c2ecf20Sopenharmony_ci * @lock: Protects write sequences
228c2ecf20Sopenharmony_ci * @out: Buffer for device register
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_cistruct pca9570 {
258c2ecf20Sopenharmony_ci	struct gpio_chip chip;
268c2ecf20Sopenharmony_ci	struct mutex lock;
278c2ecf20Sopenharmony_ci	u8 out;
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic int pca9570_read(struct pca9570 *gpio, u8 *value)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
338c2ecf20Sopenharmony_ci	int ret;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte(client);
368c2ecf20Sopenharmony_ci	if (ret < 0)
378c2ecf20Sopenharmony_ci		return ret;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	*value = ret;
408c2ecf20Sopenharmony_ci	return 0;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int pca9570_write(struct pca9570 *gpio, u8 value)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte(client, value);
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int pca9570_get_direction(struct gpio_chip *chip,
518c2ecf20Sopenharmony_ci				 unsigned offset)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	/* This device always output */
548c2ecf20Sopenharmony_ci	return GPIO_LINE_DIRECTION_OUT;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int pca9570_get(struct gpio_chip *chip, unsigned offset)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct pca9570 *gpio = gpiochip_get_data(chip);
608c2ecf20Sopenharmony_ci	u8 buffer;
618c2ecf20Sopenharmony_ci	int ret;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	ret = pca9570_read(gpio, &buffer);
648c2ecf20Sopenharmony_ci	if (ret)
658c2ecf20Sopenharmony_ci		return ret;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	return !!(buffer & BIT(offset));
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct pca9570 *gpio = gpiochip_get_data(chip);
738c2ecf20Sopenharmony_ci	u8 buffer;
748c2ecf20Sopenharmony_ci	int ret;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	mutex_lock(&gpio->lock);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	buffer = gpio->out;
798c2ecf20Sopenharmony_ci	if (value)
808c2ecf20Sopenharmony_ci		buffer |= BIT(offset);
818c2ecf20Sopenharmony_ci	else
828c2ecf20Sopenharmony_ci		buffer &= ~BIT(offset);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	ret = pca9570_write(gpio, buffer);
858c2ecf20Sopenharmony_ci	if (ret)
868c2ecf20Sopenharmony_ci		goto out;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	gpio->out = buffer;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciout:
918c2ecf20Sopenharmony_ci	mutex_unlock(&gpio->lock);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic int pca9570_probe(struct i2c_client *client)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct pca9570 *gpio;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
998c2ecf20Sopenharmony_ci	if (!gpio)
1008c2ecf20Sopenharmony_ci		return -ENOMEM;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	gpio->chip.label = client->name;
1038c2ecf20Sopenharmony_ci	gpio->chip.parent = &client->dev;
1048c2ecf20Sopenharmony_ci	gpio->chip.owner = THIS_MODULE;
1058c2ecf20Sopenharmony_ci	gpio->chip.get_direction = pca9570_get_direction;
1068c2ecf20Sopenharmony_ci	gpio->chip.get = pca9570_get;
1078c2ecf20Sopenharmony_ci	gpio->chip.set = pca9570_set;
1088c2ecf20Sopenharmony_ci	gpio->chip.base = -1;
1098c2ecf20Sopenharmony_ci	gpio->chip.ngpio = (uintptr_t)device_get_match_data(&client->dev);
1108c2ecf20Sopenharmony_ci	gpio->chip.can_sleep = true;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	mutex_init(&gpio->lock);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	/* Read the current output level */
1158c2ecf20Sopenharmony_ci	pca9570_read(gpio, &gpio->out);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, gpio);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic const struct i2c_device_id pca9570_id_table[] = {
1238c2ecf20Sopenharmony_ci	{ "pca9570", 4 },
1248c2ecf20Sopenharmony_ci	{ /* sentinel */ }
1258c2ecf20Sopenharmony_ci};
1268c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, pca9570_id_table);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic const struct of_device_id pca9570_of_match_table[] = {
1298c2ecf20Sopenharmony_ci	{ .compatible = "nxp,pca9570", .data = (void *)4 },
1308c2ecf20Sopenharmony_ci	{ /* sentinel */ }
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, pca9570_of_match_table);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic struct i2c_driver pca9570_driver = {
1358c2ecf20Sopenharmony_ci	.driver = {
1368c2ecf20Sopenharmony_ci		.name = "pca9570",
1378c2ecf20Sopenharmony_ci		.of_match_table = pca9570_of_match_table,
1388c2ecf20Sopenharmony_ci	},
1398c2ecf20Sopenharmony_ci	.probe_new = pca9570_probe,
1408c2ecf20Sopenharmony_ci	.id_table = pca9570_id_table,
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_cimodule_i2c_driver(pca9570_driver);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
1458c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("GPIO expander driver for PCA9570");
1468c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
147