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 * @chip_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 int pca9570_set(struct gpio_chip *chip, unsigned int 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 return ret; 114 } 115 116 static int pca9570_probe(struct i2c_client *client) 117 { 118 struct pca9570 *gpio; 119 120 gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL); 121 if (!gpio) 122 return -ENOMEM; 123 124 gpio->chip.label = client->name; 125 gpio->chip.parent = &client->dev; 126 gpio->chip.owner = THIS_MODULE; 127 gpio->chip.get_direction = pca9570_get_direction; 128 gpio->chip.get = pca9570_get; 129 gpio->chip.set = pca9570_set; 130 gpio->chip.base = -1; 131 gpio->chip_data = device_get_match_data(&client->dev); 132 gpio->chip.ngpio = gpio->chip_data->ngpio; 133 gpio->chip.can_sleep = true; 134 135 mutex_init(&gpio->lock); 136 137 /* Read the current output level */ 138 pca9570_read(gpio, &gpio->out); 139 140 i2c_set_clientdata(client, gpio); 141 142 return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio); 143 } 144 145 static const struct pca9570_chip_data pca9570_gpio = { 146 .ngpio = 4, 147 }; 148 149 static const struct pca9570_chip_data pca9571_gpio = { 150 .ngpio = 8, 151 }; 152 153 static const struct pca9570_chip_data slg7xl45106_gpio = { 154 .ngpio = 8, 155 .command = SLG7XL45106_GPO_REG, 156 }; 157 158 static const struct i2c_device_id pca9570_id_table[] = { 159 { "pca9570", (kernel_ulong_t)&pca9570_gpio}, 160 { "pca9571", (kernel_ulong_t)&pca9571_gpio }, 161 { "slg7xl45106", (kernel_ulong_t)&slg7xl45106_gpio }, 162 { /* sentinel */ } 163 }; 164 MODULE_DEVICE_TABLE(i2c, pca9570_id_table); 165 166 static const struct of_device_id pca9570_of_match_table[] = { 167 { .compatible = "dlg,slg7xl45106", .data = &slg7xl45106_gpio}, 168 { .compatible = "nxp,pca9570", .data = &pca9570_gpio }, 169 { .compatible = "nxp,pca9571", .data = &pca9571_gpio }, 170 { /* sentinel */ } 171 }; 172 MODULE_DEVICE_TABLE(of, pca9570_of_match_table); 173 174 static struct i2c_driver pca9570_driver = { 175 .driver = { 176 .name = "pca9570", 177 .of_match_table = pca9570_of_match_table, 178 }, 179 .probe = pca9570_probe, 180 .id_table = pca9570_id_table, 181 }; 182 module_i2c_driver(pca9570_driver); 183 184 MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>"); 185 MODULE_DESCRIPTION("GPIO expander driver for PCA9570"); 186 MODULE_LICENSE("GPL v2"); 187