1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices ADP5585 GPIO driver
6 * Copyright 2024 Ideas on Board Oy
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>
17 #define ADP5585_GPIO_MAX 11
19 struct adp5585_gpio_dev
{
20 struct gpio_chip gpio_chip
;
21 struct regmap
*regmap
;
24 static int adp5585_gpio_get_direction(struct gpio_chip
*chip
, unsigned int off
)
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
);
31 regmap_read(adp5585_gpio
->regmap
, ADP5585_GPIO_DIRECTION_A
+ bank
, &val
);
33 return val
& bit
? GPIO_LINE_DIRECTION_OUT
: GPIO_LINE_DIRECTION_IN
;
36 static int adp5585_gpio_direction_input(struct gpio_chip
*chip
, unsigned int off
)
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
);
42 return regmap_clear_bits(adp5585_gpio
->regmap
,
43 ADP5585_GPIO_DIRECTION_A
+ bank
, bit
);
46 static int adp5585_gpio_direction_output(struct gpio_chip
*chip
, unsigned int off
, int val
)
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
);
53 ret
= regmap_update_bits(adp5585_gpio
->regmap
,
54 ADP5585_GPO_DATA_OUT_A
+ bank
, bit
,
59 return regmap_set_bits(adp5585_gpio
->regmap
,
60 ADP5585_GPIO_DIRECTION_A
+ bank
, bit
);
63 static int adp5585_gpio_get_value(struct gpio_chip
*chip
, unsigned int off
)
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
);
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
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
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
);
89 static void adp5585_gpio_set_value(struct gpio_chip
*chip
, unsigned int off
, int val
)
91 struct adp5585_gpio_dev
*adp5585_gpio
= gpiochip_get_data(chip
);
92 unsigned int bank
= ADP5585_BANK(off
);
93 unsigned int bit
= ADP5585_BIT(off
);
95 regmap_update_bits(adp5585_gpio
->regmap
, ADP5585_GPO_DATA_OUT_A
+ bank
,
99 static int adp5585_gpio_set_bias(struct adp5585_gpio_dev
*adp5585_gpio
,
100 unsigned int off
, unsigned int bias
)
102 unsigned int bit
, reg
, mask
, val
;
105 * The bias configuration fields are 2 bits wide and laid down in
106 * consecutive registers ADP5585_RPULL_CONFIG_*, with a hole of 4 bits
109 bit
= off
* 2 + (off
> 5 ? 4 : 0);
110 reg
= ADP5585_RPULL_CONFIG_A
+ bit
/ 8;
111 mask
= ADP5585_Rx_PULL_CFG_MASK
<< (bit
% 8);
112 val
= bias
<< (bit
% 8);
114 return regmap_update_bits(adp5585_gpio
->regmap
, reg
, mask
, val
);
117 static int adp5585_gpio_set_drive(struct adp5585_gpio_dev
*adp5585_gpio
,
118 unsigned int off
, enum pin_config_param drive
)
120 unsigned int bank
= ADP5585_BANK(off
);
121 unsigned int bit
= ADP5585_BIT(off
);
123 return regmap_update_bits(adp5585_gpio
->regmap
,
124 ADP5585_GPO_OUT_MODE_A
+ bank
, bit
,
125 drive
== PIN_CONFIG_DRIVE_OPEN_DRAIN
? bit
: 0);
128 static int adp5585_gpio_set_debounce(struct adp5585_gpio_dev
*adp5585_gpio
,
129 unsigned int off
, unsigned int debounce
)
131 unsigned int bank
= ADP5585_BANK(off
);
132 unsigned int bit
= ADP5585_BIT(off
);
134 return regmap_update_bits(adp5585_gpio
->regmap
,
135 ADP5585_DEBOUNCE_DIS_A
+ bank
, bit
,
139 static int adp5585_gpio_set_config(struct gpio_chip
*chip
, unsigned int off
,
140 unsigned long config
)
142 struct adp5585_gpio_dev
*adp5585_gpio
= gpiochip_get_data(chip
);
143 enum pin_config_param param
= pinconf_to_config_param(config
);
144 u32 arg
= pinconf_to_config_argument(config
);
147 case PIN_CONFIG_BIAS_DISABLE
:
148 return adp5585_gpio_set_bias(adp5585_gpio
, off
,
149 ADP5585_Rx_PULL_CFG_DISABLE
);
151 case PIN_CONFIG_BIAS_PULL_DOWN
:
152 return adp5585_gpio_set_bias(adp5585_gpio
, off
, arg
?
153 ADP5585_Rx_PULL_CFG_PD_300K
:
154 ADP5585_Rx_PULL_CFG_DISABLE
);
156 case PIN_CONFIG_BIAS_PULL_UP
:
157 return adp5585_gpio_set_bias(adp5585_gpio
, off
, arg
?
158 ADP5585_Rx_PULL_CFG_PU_300K
:
159 ADP5585_Rx_PULL_CFG_DISABLE
);
161 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
162 case PIN_CONFIG_DRIVE_PUSH_PULL
:
163 return adp5585_gpio_set_drive(adp5585_gpio
, off
, param
);
165 case PIN_CONFIG_INPUT_DEBOUNCE
:
166 return adp5585_gpio_set_debounce(adp5585_gpio
, off
, arg
);
173 static int adp5585_gpio_probe(struct platform_device
*pdev
)
175 struct adp5585_dev
*adp5585
= dev_get_drvdata(pdev
->dev
.parent
);
176 struct adp5585_gpio_dev
*adp5585_gpio
;
177 struct device
*dev
= &pdev
->dev
;
178 struct gpio_chip
*gc
;
181 adp5585_gpio
= devm_kzalloc(dev
, sizeof(*adp5585_gpio
), GFP_KERNEL
);
185 adp5585_gpio
->regmap
= adp5585
->regmap
;
187 device_set_of_node_from_dev(dev
, dev
->parent
);
189 gc
= &adp5585_gpio
->gpio_chip
;
191 gc
->get_direction
= adp5585_gpio_get_direction
;
192 gc
->direction_input
= adp5585_gpio_direction_input
;
193 gc
->direction_output
= adp5585_gpio_direction_output
;
194 gc
->get
= adp5585_gpio_get_value
;
195 gc
->set
= adp5585_gpio_set_value
;
196 gc
->set_config
= adp5585_gpio_set_config
;
197 gc
->can_sleep
= true;
200 gc
->ngpio
= ADP5585_GPIO_MAX
;
201 gc
->label
= pdev
->name
;
202 gc
->owner
= THIS_MODULE
;
204 ret
= devm_gpiochip_add_data(dev
, &adp5585_gpio
->gpio_chip
,
207 return dev_err_probe(dev
, ret
, "failed to add GPIO chip\n");
212 static const struct platform_device_id adp5585_gpio_id_table
[] = {
216 MODULE_DEVICE_TABLE(platform
, adp5585_gpio_id_table
);
218 static struct platform_driver adp5585_gpio_driver
= {
220 .name
= "adp5585-gpio",
222 .probe
= adp5585_gpio_probe
,
223 .id_table
= adp5585_gpio_id_table
,
225 module_platform_driver(adp5585_gpio_driver
);
227 MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>");
228 MODULE_DESCRIPTION("GPIO ADP5585 Driver");
229 MODULE_LICENSE("GPL");