xref: /linux/drivers/gpio/gpio-pca9570.c (revision 2da68a77)
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 /**
19  * struct pca9570 - GPIO driver data
20  * @chip: GPIO controller chip
21  * @lock: Protects write sequences
22  * @out: Buffer for device register
23  */
24 struct pca9570 {
25 	struct gpio_chip chip;
26 	struct mutex lock;
27 	u8 out;
28 };
29 
30 static int pca9570_read(struct pca9570 *gpio, u8 *value)
31 {
32 	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
33 	int ret;
34 
35 	ret = i2c_smbus_read_byte(client);
36 	if (ret < 0)
37 		return ret;
38 
39 	*value = ret;
40 	return 0;
41 }
42 
43 static int pca9570_write(struct pca9570 *gpio, u8 value)
44 {
45 	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
46 
47 	return i2c_smbus_write_byte(client, value);
48 }
49 
50 static int pca9570_get_direction(struct gpio_chip *chip,
51 				 unsigned offset)
52 {
53 	/* This device always output */
54 	return GPIO_LINE_DIRECTION_OUT;
55 }
56 
57 static int pca9570_get(struct gpio_chip *chip, unsigned offset)
58 {
59 	struct pca9570 *gpio = gpiochip_get_data(chip);
60 	u8 buffer;
61 	int ret;
62 
63 	ret = pca9570_read(gpio, &buffer);
64 	if (ret)
65 		return ret;
66 
67 	return !!(buffer & BIT(offset));
68 }
69 
70 static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
71 {
72 	struct pca9570 *gpio = gpiochip_get_data(chip);
73 	u8 buffer;
74 	int ret;
75 
76 	mutex_lock(&gpio->lock);
77 
78 	buffer = gpio->out;
79 	if (value)
80 		buffer |= BIT(offset);
81 	else
82 		buffer &= ~BIT(offset);
83 
84 	ret = pca9570_write(gpio, buffer);
85 	if (ret)
86 		goto out;
87 
88 	gpio->out = buffer;
89 
90 out:
91 	mutex_unlock(&gpio->lock);
92 }
93 
94 static int pca9570_probe(struct i2c_client *client)
95 {
96 	struct pca9570 *gpio;
97 
98 	gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
99 	if (!gpio)
100 		return -ENOMEM;
101 
102 	gpio->chip.label = client->name;
103 	gpio->chip.parent = &client->dev;
104 	gpio->chip.owner = THIS_MODULE;
105 	gpio->chip.get_direction = pca9570_get_direction;
106 	gpio->chip.get = pca9570_get;
107 	gpio->chip.set = pca9570_set;
108 	gpio->chip.base = -1;
109 	gpio->chip.ngpio = (uintptr_t)device_get_match_data(&client->dev);
110 	gpio->chip.can_sleep = true;
111 
112 	mutex_init(&gpio->lock);
113 
114 	/* Read the current output level */
115 	pca9570_read(gpio, &gpio->out);
116 
117 	i2c_set_clientdata(client, gpio);
118 
119 	return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
120 }
121 
122 static const struct i2c_device_id pca9570_id_table[] = {
123 	{ "pca9570", 4 },
124 	{ "pca9571", 8 },
125 	{ /* sentinel */ }
126 };
127 MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
128 
129 static const struct of_device_id pca9570_of_match_table[] = {
130 	{ .compatible = "nxp,pca9570", .data = (void *)4 },
131 	{ .compatible = "nxp,pca9571", .data = (void *)8 },
132 	{ /* sentinel */ }
133 };
134 MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
135 
136 static struct i2c_driver pca9570_driver = {
137 	.driver = {
138 		.name = "pca9570",
139 		.of_match_table = pca9570_of_match_table,
140 	},
141 	.probe_new = pca9570_probe,
142 	.id_table = pca9570_id_table,
143 };
144 module_i2c_driver(pca9570_driver);
145 
146 MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
147 MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
148 MODULE_LICENSE("GPL v2");
149