1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Analog Devices ADP5585 GPIO driver
4 *
5 * Copyright 2022 NXP
6 * Copyright 2024 Ideas on Board Oy
7 */
8
9 #include <linux/device.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/mfd/adp5585.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/types.h>
16
17 #define ADP5585_GPIO_MAX 11
18
19 struct adp5585_gpio_dev {
20 struct gpio_chip gpio_chip;
21 struct regmap *regmap;
22 };
23
adp5585_gpio_get_direction(struct gpio_chip * chip,unsigned int off)24 static int adp5585_gpio_get_direction(struct gpio_chip *chip, unsigned int off)
25 {
26 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip);
27 unsigned int bank = ADP5585_BANK(off);
28 unsigned int bit = ADP5585_BIT(off);
29 unsigned int val;
30
31 regmap_read(adp5585_gpio->regmap, ADP5585_GPIO_DIRECTION_A + bank, &val);
32
33 return val & bit ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
34 }
35
adp5585_gpio_direction_input(struct gpio_chip * chip,unsigned int off)36 static int adp5585_gpio_direction_input(struct gpio_chip *chip, unsigned int off)
37 {
38 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip);
39 unsigned int bank = ADP5585_BANK(off);
40 unsigned int bit = ADP5585_BIT(off);
41
42 return regmap_clear_bits(adp5585_gpio->regmap,
43 ADP5585_GPIO_DIRECTION_A + bank, bit);
44 }
45
adp5585_gpio_direction_output(struct gpio_chip * chip,unsigned int off,int val)46 static int adp5585_gpio_direction_output(struct gpio_chip *chip, unsigned int off, int val)
47 {
48 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip);
49 unsigned int bank = ADP5585_BANK(off);
50 unsigned int bit = ADP5585_BIT(off);
51 int ret;
52
53 ret = regmap_update_bits(adp5585_gpio->regmap,
54 ADP5585_GPO_DATA_OUT_A + bank, bit,
55 val ? bit : 0);
56 if (ret)
57 return ret;
58
59 return regmap_set_bits(adp5585_gpio->regmap,
60 ADP5585_GPIO_DIRECTION_A + bank, bit);
61 }
62
adp5585_gpio_get_value(struct gpio_chip * chip,unsigned int off)63 static int adp5585_gpio_get_value(struct gpio_chip *chip, unsigned int off)
64 {
65 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip);
66 unsigned int bank = ADP5585_BANK(off);
67 unsigned int bit = ADP5585_BIT(off);
68 unsigned int reg;
69 unsigned int val;
70
71 /*
72 * The input status register doesn't reflect the pin state when the
73 * GPIO is configured as an output. Check the direction, and read the
74 * input status from GPI_STATUS or output value from GPO_DATA_OUT
75 * accordingly.
76 *
77 * We don't need any locking, as concurrent access to the same GPIO
78 * isn't allowed by the GPIO API, so there's no risk of the
79 * .direction_input(), .direction_output() or .set() operations racing
80 * with this.
81 */
82 regmap_read(adp5585_gpio->regmap, ADP5585_GPIO_DIRECTION_A + bank, &val);
83 reg = val & bit ? ADP5585_GPO_DATA_OUT_A : ADP5585_GPI_STATUS_A;
84 regmap_read(adp5585_gpio->regmap, reg + bank, &val);
85
86 return !!(val & bit);
87 }
88
adp5585_gpio_set_value(struct gpio_chip * chip,unsigned int off,int val)89 static int adp5585_gpio_set_value(struct gpio_chip *chip, unsigned int off,
90 int val)
91 {
92 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip);
93 unsigned int bank = ADP5585_BANK(off);
94 unsigned int bit = ADP5585_BIT(off);
95
96 return regmap_update_bits(adp5585_gpio->regmap,
97 ADP5585_GPO_DATA_OUT_A + bank,
98 bit, val ? bit : 0);
99 }
100
adp5585_gpio_set_bias(struct adp5585_gpio_dev * adp5585_gpio,unsigned int off,unsigned int bias)101 static int adp5585_gpio_set_bias(struct adp5585_gpio_dev *adp5585_gpio,
102 unsigned int off, unsigned int bias)
103 {
104 unsigned int bit, reg, mask, val;
105
106 /*
107 * The bias configuration fields are 2 bits wide and laid down in
108 * consecutive registers ADP5585_RPULL_CONFIG_*, with a hole of 4 bits
109 * after R5.
110 */
111 bit = off * 2 + (off > 5 ? 4 : 0);
112 reg = ADP5585_RPULL_CONFIG_A + bit / 8;
113 mask = ADP5585_Rx_PULL_CFG_MASK << (bit % 8);
114 val = bias << (bit % 8);
115
116 return regmap_update_bits(adp5585_gpio->regmap, reg, mask, val);
117 }
118
adp5585_gpio_set_drive(struct adp5585_gpio_dev * adp5585_gpio,unsigned int off,enum pin_config_param drive)119 static int adp5585_gpio_set_drive(struct adp5585_gpio_dev *adp5585_gpio,
120 unsigned int off, enum pin_config_param drive)
121 {
122 unsigned int bank = ADP5585_BANK(off);
123 unsigned int bit = ADP5585_BIT(off);
124
125 return regmap_update_bits(adp5585_gpio->regmap,
126 ADP5585_GPO_OUT_MODE_A + bank, bit,
127 drive == PIN_CONFIG_DRIVE_OPEN_DRAIN ? bit : 0);
128 }
129
adp5585_gpio_set_debounce(struct adp5585_gpio_dev * adp5585_gpio,unsigned int off,unsigned int debounce)130 static int adp5585_gpio_set_debounce(struct adp5585_gpio_dev *adp5585_gpio,
131 unsigned int off, unsigned int debounce)
132 {
133 unsigned int bank = ADP5585_BANK(off);
134 unsigned int bit = ADP5585_BIT(off);
135
136 return regmap_update_bits(adp5585_gpio->regmap,
137 ADP5585_DEBOUNCE_DIS_A + bank, bit,
138 debounce ? 0 : bit);
139 }
140
adp5585_gpio_set_config(struct gpio_chip * chip,unsigned int off,unsigned long config)141 static int adp5585_gpio_set_config(struct gpio_chip *chip, unsigned int off,
142 unsigned long config)
143 {
144 struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip);
145 enum pin_config_param param = pinconf_to_config_param(config);
146 u32 arg = pinconf_to_config_argument(config);
147
148 switch (param) {
149 case PIN_CONFIG_BIAS_DISABLE:
150 return adp5585_gpio_set_bias(adp5585_gpio, off,
151 ADP5585_Rx_PULL_CFG_DISABLE);
152
153 case PIN_CONFIG_BIAS_PULL_DOWN:
154 return adp5585_gpio_set_bias(adp5585_gpio, off, arg ?
155 ADP5585_Rx_PULL_CFG_PD_300K :
156 ADP5585_Rx_PULL_CFG_DISABLE);
157
158 case PIN_CONFIG_BIAS_PULL_UP:
159 return adp5585_gpio_set_bias(adp5585_gpio, off, arg ?
160 ADP5585_Rx_PULL_CFG_PU_300K :
161 ADP5585_Rx_PULL_CFG_DISABLE);
162
163 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
164 case PIN_CONFIG_DRIVE_PUSH_PULL:
165 return adp5585_gpio_set_drive(adp5585_gpio, off, param);
166
167 case PIN_CONFIG_INPUT_DEBOUNCE:
168 return adp5585_gpio_set_debounce(adp5585_gpio, off, arg);
169
170 default:
171 return -ENOTSUPP;
172 };
173 }
174
adp5585_gpio_probe(struct platform_device * pdev)175 static int adp5585_gpio_probe(struct platform_device *pdev)
176 {
177 struct adp5585_dev *adp5585 = dev_get_drvdata(pdev->dev.parent);
178 struct adp5585_gpio_dev *adp5585_gpio;
179 struct device *dev = &pdev->dev;
180 struct gpio_chip *gc;
181 int ret;
182
183 adp5585_gpio = devm_kzalloc(dev, sizeof(*adp5585_gpio), GFP_KERNEL);
184 if (!adp5585_gpio)
185 return -ENOMEM;
186
187 adp5585_gpio->regmap = adp5585->regmap;
188
189 device_set_of_node_from_dev(dev, dev->parent);
190
191 gc = &adp5585_gpio->gpio_chip;
192 gc->parent = dev;
193 gc->get_direction = adp5585_gpio_get_direction;
194 gc->direction_input = adp5585_gpio_direction_input;
195 gc->direction_output = adp5585_gpio_direction_output;
196 gc->get = adp5585_gpio_get_value;
197 gc->set_rv = adp5585_gpio_set_value;
198 gc->set_config = adp5585_gpio_set_config;
199 gc->can_sleep = true;
200
201 gc->base = -1;
202 gc->ngpio = ADP5585_GPIO_MAX;
203 gc->label = pdev->name;
204 gc->owner = THIS_MODULE;
205
206 ret = devm_gpiochip_add_data(dev, &adp5585_gpio->gpio_chip,
207 adp5585_gpio);
208 if (ret)
209 return dev_err_probe(dev, ret, "failed to add GPIO chip\n");
210
211 return 0;
212 }
213
214 static const struct platform_device_id adp5585_gpio_id_table[] = {
215 { "adp5585-gpio" },
216 { /* Sentinel */ }
217 };
218 MODULE_DEVICE_TABLE(platform, adp5585_gpio_id_table);
219
220 static struct platform_driver adp5585_gpio_driver = {
221 .driver = {
222 .name = "adp5585-gpio",
223 },
224 .probe = adp5585_gpio_probe,
225 .id_table = adp5585_gpio_id_table,
226 };
227 module_platform_driver(adp5585_gpio_driver);
228
229 MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>");
230 MODULE_DESCRIPTION("GPIO ADP5585 Driver");
231 MODULE_LICENSE("GPL");
232