xref: /openbmc/linux/drivers/gpio/gpio-pca9570.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
116d44b60SSungbo Eo // SPDX-License-Identifier: GPL-2.0-only
216d44b60SSungbo Eo /*
316d44b60SSungbo Eo  * Driver for PCA9570 I2C GPO expander
416d44b60SSungbo Eo  *
516d44b60SSungbo Eo  * Copyright (C) 2020 Sungbo Eo <mans0n@gorani.run>
616d44b60SSungbo Eo  *
716d44b60SSungbo Eo  * Based on gpio-tpic2810.c
816d44b60SSungbo Eo  * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
916d44b60SSungbo Eo  *	Andrew F. Davis <afd@ti.com>
1016d44b60SSungbo Eo  */
1116d44b60SSungbo Eo 
1216d44b60SSungbo Eo #include <linux/gpio/driver.h>
1316d44b60SSungbo Eo #include <linux/i2c.h>
1416d44b60SSungbo Eo #include <linux/module.h>
1516d44b60SSungbo Eo #include <linux/mutex.h>
1616d44b60SSungbo Eo #include <linux/property.h>
1716d44b60SSungbo Eo 
18fbb19fe1SShubhrajyoti Datta #define SLG7XL45106_GPO_REG	0xDB
19fbb19fe1SShubhrajyoti Datta 
2016d44b60SSungbo Eo /**
21a3f7c1d6SBartosz Golaszewski  * struct pca9570_chip_data - GPIO platformdata
22b8a34582SShubhrajyoti Datta  * @ngpio: no of gpios
23b8a34582SShubhrajyoti Datta  * @command: Command to be sent
24b8a34582SShubhrajyoti Datta  */
25a3f7c1d6SBartosz Golaszewski struct pca9570_chip_data {
26b8a34582SShubhrajyoti Datta 	u16 ngpio;
27b8a34582SShubhrajyoti Datta 	u32 command;
28b8a34582SShubhrajyoti Datta };
29b8a34582SShubhrajyoti Datta 
30b8a34582SShubhrajyoti Datta /**
3116d44b60SSungbo Eo  * struct pca9570 - GPIO driver data
3216d44b60SSungbo Eo  * @chip: GPIO controller chip
33*82dbbfdfSBartosz Golaszewski  * @chip_data: GPIO controller platform data
3416d44b60SSungbo Eo  * @lock: Protects write sequences
3516d44b60SSungbo Eo  * @out: Buffer for device register
3616d44b60SSungbo Eo  */
3716d44b60SSungbo Eo struct pca9570 {
3816d44b60SSungbo Eo 	struct gpio_chip chip;
39a3f7c1d6SBartosz Golaszewski 	const struct pca9570_chip_data *chip_data;
4016d44b60SSungbo Eo 	struct mutex lock;
4116d44b60SSungbo Eo 	u8 out;
4216d44b60SSungbo Eo };
4316d44b60SSungbo Eo 
pca9570_read(struct pca9570 * gpio,u8 * value)4416d44b60SSungbo Eo static int pca9570_read(struct pca9570 *gpio, u8 *value)
4516d44b60SSungbo Eo {
4616d44b60SSungbo Eo 	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
4716d44b60SSungbo Eo 	int ret;
4816d44b60SSungbo Eo 
49a3f7c1d6SBartosz Golaszewski 	if (gpio->chip_data->command != 0)
50a3f7c1d6SBartosz Golaszewski 		ret = i2c_smbus_read_byte_data(client, gpio->chip_data->command);
51fbb19fe1SShubhrajyoti Datta 	else
5216d44b60SSungbo Eo 		ret = i2c_smbus_read_byte(client);
53fbb19fe1SShubhrajyoti Datta 
5416d44b60SSungbo Eo 	if (ret < 0)
5516d44b60SSungbo Eo 		return ret;
5616d44b60SSungbo Eo 
5716d44b60SSungbo Eo 	*value = ret;
5816d44b60SSungbo Eo 	return 0;
5916d44b60SSungbo Eo }
6016d44b60SSungbo Eo 
pca9570_write(struct pca9570 * gpio,u8 value)6116d44b60SSungbo Eo static int pca9570_write(struct pca9570 *gpio, u8 value)
6216d44b60SSungbo Eo {
6316d44b60SSungbo Eo 	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
6416d44b60SSungbo Eo 
65a3f7c1d6SBartosz Golaszewski 	if (gpio->chip_data->command != 0)
66a3f7c1d6SBartosz Golaszewski 		return i2c_smbus_write_byte_data(client, gpio->chip_data->command, value);
67fbb19fe1SShubhrajyoti Datta 
6816d44b60SSungbo Eo 	return i2c_smbus_write_byte(client, value);
6916d44b60SSungbo Eo }
7016d44b60SSungbo Eo 
pca9570_get_direction(struct gpio_chip * chip,unsigned offset)7116d44b60SSungbo Eo static int pca9570_get_direction(struct gpio_chip *chip,
7216d44b60SSungbo Eo 				 unsigned offset)
7316d44b60SSungbo Eo {
7416d44b60SSungbo Eo 	/* This device always output */
7516d44b60SSungbo Eo 	return GPIO_LINE_DIRECTION_OUT;
7616d44b60SSungbo Eo }
7716d44b60SSungbo Eo 
pca9570_get(struct gpio_chip * chip,unsigned offset)7816d44b60SSungbo Eo static int pca9570_get(struct gpio_chip *chip, unsigned offset)
7916d44b60SSungbo Eo {
8016d44b60SSungbo Eo 	struct pca9570 *gpio = gpiochip_get_data(chip);
8116d44b60SSungbo Eo 	u8 buffer;
8216d44b60SSungbo Eo 	int ret;
8316d44b60SSungbo Eo 
8416d44b60SSungbo Eo 	ret = pca9570_read(gpio, &buffer);
8516d44b60SSungbo Eo 	if (ret)
8616d44b60SSungbo Eo 		return ret;
8716d44b60SSungbo Eo 
8816d44b60SSungbo Eo 	return !!(buffer & BIT(offset));
8916d44b60SSungbo Eo }
9016d44b60SSungbo Eo 
pca9570_set(struct gpio_chip * chip,unsigned offset,int value)9116d44b60SSungbo Eo static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
9216d44b60SSungbo Eo {
9316d44b60SSungbo Eo 	struct pca9570 *gpio = gpiochip_get_data(chip);
9416d44b60SSungbo Eo 	u8 buffer;
9516d44b60SSungbo Eo 	int ret;
9616d44b60SSungbo Eo 
9716d44b60SSungbo Eo 	mutex_lock(&gpio->lock);
9816d44b60SSungbo Eo 
9916d44b60SSungbo Eo 	buffer = gpio->out;
10016d44b60SSungbo Eo 	if (value)
10116d44b60SSungbo Eo 		buffer |= BIT(offset);
10216d44b60SSungbo Eo 	else
10316d44b60SSungbo Eo 		buffer &= ~BIT(offset);
10416d44b60SSungbo Eo 
10516d44b60SSungbo Eo 	ret = pca9570_write(gpio, buffer);
10616d44b60SSungbo Eo 	if (ret)
10716d44b60SSungbo Eo 		goto out;
10816d44b60SSungbo Eo 
10916d44b60SSungbo Eo 	gpio->out = buffer;
11016d44b60SSungbo Eo 
11116d44b60SSungbo Eo out:
11216d44b60SSungbo Eo 	mutex_unlock(&gpio->lock);
11316d44b60SSungbo Eo }
11416d44b60SSungbo Eo 
pca9570_probe(struct i2c_client * client)11516d44b60SSungbo Eo static int pca9570_probe(struct i2c_client *client)
11616d44b60SSungbo Eo {
11716d44b60SSungbo Eo 	struct pca9570 *gpio;
11816d44b60SSungbo Eo 
11916d44b60SSungbo Eo 	gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
12016d44b60SSungbo Eo 	if (!gpio)
12116d44b60SSungbo Eo 		return -ENOMEM;
12216d44b60SSungbo Eo 
12316d44b60SSungbo Eo 	gpio->chip.label = client->name;
12416d44b60SSungbo Eo 	gpio->chip.parent = &client->dev;
12516d44b60SSungbo Eo 	gpio->chip.owner = THIS_MODULE;
12616d44b60SSungbo Eo 	gpio->chip.get_direction = pca9570_get_direction;
12716d44b60SSungbo Eo 	gpio->chip.get = pca9570_get;
12816d44b60SSungbo Eo 	gpio->chip.set = pca9570_set;
12916d44b60SSungbo Eo 	gpio->chip.base = -1;
130a3f7c1d6SBartosz Golaszewski 	gpio->chip_data = device_get_match_data(&client->dev);
131a3f7c1d6SBartosz Golaszewski 	gpio->chip.ngpio = gpio->chip_data->ngpio;
13216d44b60SSungbo Eo 	gpio->chip.can_sleep = true;
13316d44b60SSungbo Eo 
13416d44b60SSungbo Eo 	mutex_init(&gpio->lock);
13516d44b60SSungbo Eo 
13616d44b60SSungbo Eo 	/* Read the current output level */
13716d44b60SSungbo Eo 	pca9570_read(gpio, &gpio->out);
13816d44b60SSungbo Eo 
13916d44b60SSungbo Eo 	i2c_set_clientdata(client, gpio);
14016d44b60SSungbo Eo 
14116d44b60SSungbo Eo 	return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
14216d44b60SSungbo Eo }
14316d44b60SSungbo Eo 
144a3f7c1d6SBartosz Golaszewski static const struct pca9570_chip_data pca9570_gpio = {
145b8a34582SShubhrajyoti Datta 	.ngpio = 4,
146b8a34582SShubhrajyoti Datta };
147b8a34582SShubhrajyoti Datta 
148a3f7c1d6SBartosz Golaszewski static const struct pca9570_chip_data pca9571_gpio = {
149b8a34582SShubhrajyoti Datta 	.ngpio = 8,
150b8a34582SShubhrajyoti Datta };
151b8a34582SShubhrajyoti Datta 
152a3f7c1d6SBartosz Golaszewski static const struct pca9570_chip_data slg7xl45106_gpio = {
153fbb19fe1SShubhrajyoti Datta 	.ngpio = 8,
154fbb19fe1SShubhrajyoti Datta 	.command = SLG7XL45106_GPO_REG,
155fbb19fe1SShubhrajyoti Datta };
156fbb19fe1SShubhrajyoti Datta 
15716d44b60SSungbo Eo static const struct i2c_device_id pca9570_id_table[] = {
158b8a34582SShubhrajyoti Datta 	{ "pca9570", (kernel_ulong_t)&pca9570_gpio},
159b8a34582SShubhrajyoti Datta 	{ "pca9571", (kernel_ulong_t)&pca9571_gpio },
160fbb19fe1SShubhrajyoti Datta 	{ "slg7xl45106", (kernel_ulong_t)&slg7xl45106_gpio },
16116d44b60SSungbo Eo 	{ /* sentinel */ }
16216d44b60SSungbo Eo };
16316d44b60SSungbo Eo MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
16416d44b60SSungbo Eo 
16516d44b60SSungbo Eo static const struct of_device_id pca9570_of_match_table[] = {
166fbb19fe1SShubhrajyoti Datta 	{ .compatible = "dlg,slg7xl45106", .data = &slg7xl45106_gpio},
167b8a34582SShubhrajyoti Datta 	{ .compatible = "nxp,pca9570", .data = &pca9570_gpio },
168b8a34582SShubhrajyoti Datta 	{ .compatible = "nxp,pca9571", .data = &pca9571_gpio },
16916d44b60SSungbo Eo 	{ /* sentinel */ }
17016d44b60SSungbo Eo };
17116d44b60SSungbo Eo MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
17216d44b60SSungbo Eo 
17316d44b60SSungbo Eo static struct i2c_driver pca9570_driver = {
17416d44b60SSungbo Eo 	.driver = {
17516d44b60SSungbo Eo 		.name = "pca9570",
17616d44b60SSungbo Eo 		.of_match_table = pca9570_of_match_table,
17716d44b60SSungbo Eo 	},
178b41cabb7SUwe Kleine-König 	.probe = pca9570_probe,
17916d44b60SSungbo Eo 	.id_table = pca9570_id_table,
18016d44b60SSungbo Eo };
18116d44b60SSungbo Eo module_i2c_driver(pca9570_driver);
18216d44b60SSungbo Eo 
18316d44b60SSungbo Eo MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
18416d44b60SSungbo Eo MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
18516d44b60SSungbo Eo MODULE_LICENSE("GPL v2");
186