0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/gpio/driver.h>
0013 #include <linux/i2c.h>
0014 #include <linux/module.h>
0015 #include <linux/mutex.h>
0016 #include <linux/property.h>
0017
0018
0019
0020
0021
0022
0023
0024 struct pca9570 {
0025 struct gpio_chip chip;
0026 struct mutex lock;
0027 u8 out;
0028 };
0029
0030 static int pca9570_read(struct pca9570 *gpio, u8 *value)
0031 {
0032 struct i2c_client *client = to_i2c_client(gpio->chip.parent);
0033 int ret;
0034
0035 ret = i2c_smbus_read_byte(client);
0036 if (ret < 0)
0037 return ret;
0038
0039 *value = ret;
0040 return 0;
0041 }
0042
0043 static int pca9570_write(struct pca9570 *gpio, u8 value)
0044 {
0045 struct i2c_client *client = to_i2c_client(gpio->chip.parent);
0046
0047 return i2c_smbus_write_byte(client, value);
0048 }
0049
0050 static int pca9570_get_direction(struct gpio_chip *chip,
0051 unsigned offset)
0052 {
0053
0054 return GPIO_LINE_DIRECTION_OUT;
0055 }
0056
0057 static int pca9570_get(struct gpio_chip *chip, unsigned offset)
0058 {
0059 struct pca9570 *gpio = gpiochip_get_data(chip);
0060 u8 buffer;
0061 int ret;
0062
0063 ret = pca9570_read(gpio, &buffer);
0064 if (ret)
0065 return ret;
0066
0067 return !!(buffer & BIT(offset));
0068 }
0069
0070 static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
0071 {
0072 struct pca9570 *gpio = gpiochip_get_data(chip);
0073 u8 buffer;
0074 int ret;
0075
0076 mutex_lock(&gpio->lock);
0077
0078 buffer = gpio->out;
0079 if (value)
0080 buffer |= BIT(offset);
0081 else
0082 buffer &= ~BIT(offset);
0083
0084 ret = pca9570_write(gpio, buffer);
0085 if (ret)
0086 goto out;
0087
0088 gpio->out = buffer;
0089
0090 out:
0091 mutex_unlock(&gpio->lock);
0092 }
0093
0094 static int pca9570_probe(struct i2c_client *client)
0095 {
0096 struct pca9570 *gpio;
0097
0098 gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
0099 if (!gpio)
0100 return -ENOMEM;
0101
0102 gpio->chip.label = client->name;
0103 gpio->chip.parent = &client->dev;
0104 gpio->chip.owner = THIS_MODULE;
0105 gpio->chip.get_direction = pca9570_get_direction;
0106 gpio->chip.get = pca9570_get;
0107 gpio->chip.set = pca9570_set;
0108 gpio->chip.base = -1;
0109 gpio->chip.ngpio = (uintptr_t)device_get_match_data(&client->dev);
0110 gpio->chip.can_sleep = true;
0111
0112 mutex_init(&gpio->lock);
0113
0114
0115 pca9570_read(gpio, &gpio->out);
0116
0117 i2c_set_clientdata(client, gpio);
0118
0119 return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
0120 }
0121
0122 static const struct i2c_device_id pca9570_id_table[] = {
0123 { "pca9570", 4 },
0124 { "pca9571", 8 },
0125 { }
0126 };
0127 MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
0128
0129 static const struct of_device_id pca9570_of_match_table[] = {
0130 { .compatible = "nxp,pca9570", .data = (void *)4 },
0131 { .compatible = "nxp,pca9571", .data = (void *)8 },
0132 { }
0133 };
0134 MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
0135
0136 static struct i2c_driver pca9570_driver = {
0137 .driver = {
0138 .name = "pca9570",
0139 .of_match_table = pca9570_of_match_table,
0140 },
0141 .probe_new = pca9570_probe,
0142 .id_table = pca9570_id_table,
0143 };
0144 module_i2c_driver(pca9570_driver);
0145
0146 MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
0147 MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
0148 MODULE_LICENSE("GPL v2");