xref: /openbmc/linux/drivers/gpio/gpio-pca9570.c (revision 5e0266f0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for PCA9570 I2C GPO expander
4  *
5  * Copyright (C) 2020 Sungbo Eo <mans0n@gorani.run>
6  *
7  * Based on gpio-tpic2810.c
8  * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
9  *	Andrew F. Davis <afd@ti.com>
10  */
11 
12 #include <linux/gpio/driver.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/property.h>
17 
18 #define SLG7XL45106_GPO_REG	0xDB
19 
20 /**
21  * struct pca9570_chip_data - GPIO platformdata
22  * @ngpio: no of gpios
23  * @command: Command to be sent
24  */
25 struct pca9570_chip_data {
26 	u16 ngpio;
27 	u32 command;
28 };
29 
30 /**
31  * struct pca9570 - GPIO driver data
32  * @chip: GPIO controller chip
33  * @p_data: GPIO controller platform data
34  * @lock: Protects write sequences
35  * @out: Buffer for device register
36  */
37 struct pca9570 {
38 	struct gpio_chip chip;
39 	const struct pca9570_chip_data *chip_data;
40 	struct mutex lock;
41 	u8 out;
42 };
43 
44 static int pca9570_read(struct pca9570 *gpio, u8 *value)
45 {
46 	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
47 	int ret;
48 
49 	if (gpio->chip_data->command != 0)
50 		ret = i2c_smbus_read_byte_data(client, gpio->chip_data->command);
51 	else
52 		ret = i2c_smbus_read_byte(client);
53 
54 	if (ret < 0)
55 		return ret;
56 
57 	*value = ret;
58 	return 0;
59 }
60 
61 static int pca9570_write(struct pca9570 *gpio, u8 value)
62 {
63 	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
64 
65 	if (gpio->chip_data->command != 0)
66 		return i2c_smbus_write_byte_data(client, gpio->chip_data->command, value);
67 
68 	return i2c_smbus_write_byte(client, value);
69 }
70 
71 static int pca9570_get_direction(struct gpio_chip *chip,
72 				 unsigned offset)
73 {
74 	/* This device always output */
75 	return GPIO_LINE_DIRECTION_OUT;
76 }
77 
78 static int pca9570_get(struct gpio_chip *chip, unsigned offset)
79 {
80 	struct pca9570 *gpio = gpiochip_get_data(chip);
81 	u8 buffer;
82 	int ret;
83 
84 	ret = pca9570_read(gpio, &buffer);
85 	if (ret)
86 		return ret;
87 
88 	return !!(buffer & BIT(offset));
89 }
90 
91 static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
92 {
93 	struct pca9570 *gpio = gpiochip_get_data(chip);
94 	u8 buffer;
95 	int ret;
96 
97 	mutex_lock(&gpio->lock);
98 
99 	buffer = gpio->out;
100 	if (value)
101 		buffer |= BIT(offset);
102 	else
103 		buffer &= ~BIT(offset);
104 
105 	ret = pca9570_write(gpio, buffer);
106 	if (ret)
107 		goto out;
108 
109 	gpio->out = buffer;
110 
111 out:
112 	mutex_unlock(&gpio->lock);
113 }
114 
115 static int pca9570_probe(struct i2c_client *client)
116 {
117 	struct pca9570 *gpio;
118 
119 	gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
120 	if (!gpio)
121 		return -ENOMEM;
122 
123 	gpio->chip.label = client->name;
124 	gpio->chip.parent = &client->dev;
125 	gpio->chip.owner = THIS_MODULE;
126 	gpio->chip.get_direction = pca9570_get_direction;
127 	gpio->chip.get = pca9570_get;
128 	gpio->chip.set = pca9570_set;
129 	gpio->chip.base = -1;
130 	gpio->chip_data = device_get_match_data(&client->dev);
131 	gpio->chip.ngpio = gpio->chip_data->ngpio;
132 	gpio->chip.can_sleep = true;
133 
134 	mutex_init(&gpio->lock);
135 
136 	/* Read the current output level */
137 	pca9570_read(gpio, &gpio->out);
138 
139 	i2c_set_clientdata(client, gpio);
140 
141 	return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
142 }
143 
144 static const struct pca9570_chip_data pca9570_gpio = {
145 	.ngpio = 4,
146 };
147 
148 static const struct pca9570_chip_data pca9571_gpio = {
149 	.ngpio = 8,
150 };
151 
152 static const struct pca9570_chip_data slg7xl45106_gpio = {
153 	.ngpio = 8,
154 	.command = SLG7XL45106_GPO_REG,
155 };
156 
157 static const struct i2c_device_id pca9570_id_table[] = {
158 	{ "pca9570", (kernel_ulong_t)&pca9570_gpio},
159 	{ "pca9571", (kernel_ulong_t)&pca9571_gpio },
160 	{ "slg7xl45106", (kernel_ulong_t)&slg7xl45106_gpio },
161 	{ /* sentinel */ }
162 };
163 MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
164 
165 static const struct of_device_id pca9570_of_match_table[] = {
166 	{ .compatible = "dlg,slg7xl45106", .data = &slg7xl45106_gpio},
167 	{ .compatible = "nxp,pca9570", .data = &pca9570_gpio },
168 	{ .compatible = "nxp,pca9571", .data = &pca9571_gpio },
169 	{ /* sentinel */ }
170 };
171 MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
172 
173 static struct i2c_driver pca9570_driver = {
174 	.driver = {
175 		.name = "pca9570",
176 		.of_match_table = pca9570_of_match_table,
177 	},
178 	.probe_new = pca9570_probe,
179 	.id_table = pca9570_id_table,
180 };
181 module_i2c_driver(pca9570_driver);
182 
183 MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
184 MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
185 MODULE_LICENSE("GPL v2");
186