Merge tag 'input-for-v6.10-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / gpio / gpio-pca9570.c
CommitLineData
16d44b60
SE
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
fbb19fe1
SD
18#define SLG7XL45106_GPO_REG 0xDB
19
b8a34582 20/**
a3f7c1d6 21 * struct pca9570_chip_data - GPIO platformdata
b8a34582
SD
22 * @ngpio: no of gpios
23 * @command: Command to be sent
24 */
a3f7c1d6 25struct pca9570_chip_data {
b8a34582
SD
26 u16 ngpio;
27 u32 command;
28};
29
16d44b60
SE
30/**
31 * struct pca9570 - GPIO driver data
32 * @chip: GPIO controller chip
82dbbfdf 33 * @chip_data: GPIO controller platform data
16d44b60
SE
34 * @lock: Protects write sequences
35 * @out: Buffer for device register
36 */
37struct pca9570 {
38 struct gpio_chip chip;
a3f7c1d6 39 const struct pca9570_chip_data *chip_data;
16d44b60
SE
40 struct mutex lock;
41 u8 out;
42};
43
44static int pca9570_read(struct pca9570 *gpio, u8 *value)
45{
46 struct i2c_client *client = to_i2c_client(gpio->chip.parent);
47 int ret;
48
a3f7c1d6
BG
49 if (gpio->chip_data->command != 0)
50 ret = i2c_smbus_read_byte_data(client, gpio->chip_data->command);
fbb19fe1
SD
51 else
52 ret = i2c_smbus_read_byte(client);
53
16d44b60
SE
54 if (ret < 0)
55 return ret;
56
57 *value = ret;
58 return 0;
59}
60
61static int pca9570_write(struct pca9570 *gpio, u8 value)
62{
63 struct i2c_client *client = to_i2c_client(gpio->chip.parent);
64
a3f7c1d6
BG
65 if (gpio->chip_data->command != 0)
66 return i2c_smbus_write_byte_data(client, gpio->chip_data->command, value);
fbb19fe1 67
16d44b60
SE
68 return i2c_smbus_write_byte(client, value);
69}
70
71static 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
78static 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
91static 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
111out:
112 mutex_unlock(&gpio->lock);
113}
114
115static 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;
a3f7c1d6
BG
130 gpio->chip_data = device_get_match_data(&client->dev);
131 gpio->chip.ngpio = gpio->chip_data->ngpio;
16d44b60
SE
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
a3f7c1d6 144static const struct pca9570_chip_data pca9570_gpio = {
b8a34582
SD
145 .ngpio = 4,
146};
147
a3f7c1d6 148static const struct pca9570_chip_data pca9571_gpio = {
b8a34582
SD
149 .ngpio = 8,
150};
151
a3f7c1d6 152static const struct pca9570_chip_data slg7xl45106_gpio = {
fbb19fe1
SD
153 .ngpio = 8,
154 .command = SLG7XL45106_GPO_REG,
155};
156
16d44b60 157static const struct i2c_device_id pca9570_id_table[] = {
b8a34582
SD
158 { "pca9570", (kernel_ulong_t)&pca9570_gpio},
159 { "pca9571", (kernel_ulong_t)&pca9571_gpio },
fbb19fe1 160 { "slg7xl45106", (kernel_ulong_t)&slg7xl45106_gpio },
16d44b60
SE
161 { /* sentinel */ }
162};
163MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
164
165static const struct of_device_id pca9570_of_match_table[] = {
fbb19fe1 166 { .compatible = "dlg,slg7xl45106", .data = &slg7xl45106_gpio},
b8a34582
SD
167 { .compatible = "nxp,pca9570", .data = &pca9570_gpio },
168 { .compatible = "nxp,pca9571", .data = &pca9571_gpio },
16d44b60
SE
169 { /* sentinel */ }
170};
171MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
172
173static struct i2c_driver pca9570_driver = {
174 .driver = {
175 .name = "pca9570",
176 .of_match_table = pca9570_of_match_table,
177 },
b41cabb7 178 .probe = pca9570_probe,
16d44b60
SE
179 .id_table = pca9570_id_table,
180};
181module_i2c_driver(pca9570_driver);
182
183MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
184MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
185MODULE_LICENSE("GPL v2");