1 // SPDX-License-Identifier: GPL-2.0-only
3 * MAXIM MAX77620 GPIO driver
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
8 #include <linux/gpio/driver.h>
9 #include <linux/interrupt.h>
10 #include <linux/mfd/max77620.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
15 #define GPIO_REG_ADDR(offset) (MAX77620_REG_GPIO0 + offset)
17 struct max77620_gpio
{
18 struct gpio_chip gpio_chip
;
21 struct mutex buslock
; /* irq_bus_lock */
22 unsigned int irq_type
[8];
26 static irqreturn_t
max77620_gpio_irqhandler(int irq
, void *data
)
28 struct max77620_gpio
*gpio
= data
;
29 unsigned int value
, offset
;
30 unsigned long pending
;
33 err
= regmap_read(gpio
->rmap
, MAX77620_REG_IRQ_LVL2_GPIO
, &value
);
35 dev_err(gpio
->dev
, "REG_IRQ_LVL2_GPIO read failed: %d\n", err
);
41 for_each_set_bit(offset
, &pending
, 8) {
44 virq
= irq_find_mapping(gpio
->gpio_chip
.irq
.domain
, offset
);
45 handle_nested_irq(virq
);
51 static void max77620_gpio_irq_mask(struct irq_data
*data
)
53 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
54 struct max77620_gpio
*gpio
= gpiochip_get_data(chip
);
56 gpio
->irq_enabled
[data
->hwirq
] = false;
59 static void max77620_gpio_irq_unmask(struct irq_data
*data
)
61 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
62 struct max77620_gpio
*gpio
= gpiochip_get_data(chip
);
64 gpio
->irq_enabled
[data
->hwirq
] = true;
67 static int max77620_gpio_set_irq_type(struct irq_data
*data
, unsigned int type
)
69 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
70 struct max77620_gpio
*gpio
= gpiochip_get_data(chip
);
71 unsigned int irq_type
;
74 case IRQ_TYPE_EDGE_RISING
:
75 irq_type
= MAX77620_CNFG_GPIO_INT_RISING
;
78 case IRQ_TYPE_EDGE_FALLING
:
79 irq_type
= MAX77620_CNFG_GPIO_INT_FALLING
;
82 case IRQ_TYPE_EDGE_BOTH
:
83 irq_type
= MAX77620_CNFG_GPIO_INT_RISING
|
84 MAX77620_CNFG_GPIO_INT_FALLING
;
91 gpio
->irq_type
[data
->hwirq
] = irq_type
;
96 static void max77620_gpio_bus_lock(struct irq_data
*data
)
98 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
99 struct max77620_gpio
*gpio
= gpiochip_get_data(chip
);
101 mutex_lock(&gpio
->buslock
);
104 static void max77620_gpio_bus_sync_unlock(struct irq_data
*data
)
106 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
107 struct max77620_gpio
*gpio
= gpiochip_get_data(chip
);
108 unsigned int value
, offset
= data
->hwirq
;
111 value
= gpio
->irq_enabled
[offset
] ? gpio
->irq_type
[offset
] : 0;
113 err
= regmap_update_bits(gpio
->rmap
, GPIO_REG_ADDR(offset
),
114 MAX77620_CNFG_GPIO_INT_MASK
, value
);
116 dev_err(chip
->parent
, "failed to update interrupt mask: %d\n",
119 mutex_unlock(&gpio
->buslock
);
122 static struct irq_chip max77620_gpio_irqchip
= {
123 .name
= "max77620-gpio",
124 .irq_mask
= max77620_gpio_irq_mask
,
125 .irq_unmask
= max77620_gpio_irq_unmask
,
126 .irq_set_type
= max77620_gpio_set_irq_type
,
127 .irq_bus_lock
= max77620_gpio_bus_lock
,
128 .irq_bus_sync_unlock
= max77620_gpio_bus_sync_unlock
,
129 .flags
= IRQCHIP_MASK_ON_SUSPEND
,
132 static int max77620_gpio_dir_input(struct gpio_chip
*gc
, unsigned int offset
)
134 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
137 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
138 MAX77620_CNFG_GPIO_DIR_MASK
,
139 MAX77620_CNFG_GPIO_DIR_INPUT
);
141 dev_err(mgpio
->dev
, "CNFG_GPIOx dir update failed: %d\n", ret
);
146 static int max77620_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
148 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
152 ret
= regmap_read(mgpio
->rmap
, GPIO_REG_ADDR(offset
), &val
);
154 dev_err(mgpio
->dev
, "CNFG_GPIOx read failed: %d\n", ret
);
158 if (val
& MAX77620_CNFG_GPIO_DIR_MASK
)
159 return !!(val
& MAX77620_CNFG_GPIO_INPUT_VAL_MASK
);
161 return !!(val
& MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK
);
164 static int max77620_gpio_dir_output(struct gpio_chip
*gc
, unsigned int offset
,
167 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
171 val
= (value
) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH
:
172 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW
;
174 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
175 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK
, val
);
177 dev_err(mgpio
->dev
, "CNFG_GPIOx val update failed: %d\n", ret
);
181 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
182 MAX77620_CNFG_GPIO_DIR_MASK
,
183 MAX77620_CNFG_GPIO_DIR_OUTPUT
);
185 dev_err(mgpio
->dev
, "CNFG_GPIOx dir update failed: %d\n", ret
);
190 static int max77620_gpio_set_debounce(struct max77620_gpio
*mgpio
,
192 unsigned int debounce
)
199 val
= MAX77620_CNFG_GPIO_DBNC_None
;
202 val
= MAX77620_CNFG_GPIO_DBNC_8ms
;
205 val
= MAX77620_CNFG_GPIO_DBNC_16ms
;
207 case 16001 ... 32000:
208 val
= MAX77620_CNFG_GPIO_DBNC_32ms
;
211 dev_err(mgpio
->dev
, "Illegal value %u\n", debounce
);
215 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
216 MAX77620_CNFG_GPIO_DBNC_MASK
, val
);
218 dev_err(mgpio
->dev
, "CNFG_GPIOx_DBNC update failed: %d\n", ret
);
223 static void max77620_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
226 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
230 val
= (value
) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH
:
231 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW
;
233 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
234 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK
, val
);
236 dev_err(mgpio
->dev
, "CNFG_GPIO_OUT update failed: %d\n", ret
);
239 static int max77620_gpio_set_config(struct gpio_chip
*gc
, unsigned int offset
,
240 unsigned long config
)
242 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
244 switch (pinconf_to_config_param(config
)) {
245 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
246 return regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
247 MAX77620_CNFG_GPIO_DRV_MASK
,
248 MAX77620_CNFG_GPIO_DRV_OPENDRAIN
);
249 case PIN_CONFIG_DRIVE_PUSH_PULL
:
250 return regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
251 MAX77620_CNFG_GPIO_DRV_MASK
,
252 MAX77620_CNFG_GPIO_DRV_PUSHPULL
);
253 case PIN_CONFIG_INPUT_DEBOUNCE
:
254 return max77620_gpio_set_debounce(mgpio
, offset
,
255 pinconf_to_config_argument(config
));
263 static int max77620_gpio_probe(struct platform_device
*pdev
)
265 struct max77620_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
266 struct max77620_gpio
*mgpio
;
270 gpio_irq
= platform_get_irq(pdev
, 0);
274 mgpio
= devm_kzalloc(&pdev
->dev
, sizeof(*mgpio
), GFP_KERNEL
);
278 mgpio
->rmap
= chip
->rmap
;
279 mgpio
->dev
= &pdev
->dev
;
281 mgpio
->gpio_chip
.label
= pdev
->name
;
282 mgpio
->gpio_chip
.parent
= &pdev
->dev
;
283 mgpio
->gpio_chip
.direction_input
= max77620_gpio_dir_input
;
284 mgpio
->gpio_chip
.get
= max77620_gpio_get
;
285 mgpio
->gpio_chip
.direction_output
= max77620_gpio_dir_output
;
286 mgpio
->gpio_chip
.set
= max77620_gpio_set
;
287 mgpio
->gpio_chip
.set_config
= max77620_gpio_set_config
;
288 mgpio
->gpio_chip
.ngpio
= MAX77620_GPIO_NR
;
289 mgpio
->gpio_chip
.can_sleep
= 1;
290 mgpio
->gpio_chip
.base
= -1;
291 #ifdef CONFIG_OF_GPIO
292 mgpio
->gpio_chip
.of_node
= pdev
->dev
.parent
->of_node
;
295 platform_set_drvdata(pdev
, mgpio
);
297 ret
= devm_gpiochip_add_data(&pdev
->dev
, &mgpio
->gpio_chip
, mgpio
);
299 dev_err(&pdev
->dev
, "gpio_init: Failed to add max77620_gpio\n");
303 mutex_init(&mgpio
->buslock
);
305 gpiochip_irqchip_add_nested(&mgpio
->gpio_chip
, &max77620_gpio_irqchip
,
306 0, handle_edge_irq
, IRQ_TYPE_NONE
);
308 ret
= request_threaded_irq(gpio_irq
, NULL
, max77620_gpio_irqhandler
,
309 IRQF_ONESHOT
, "max77620-gpio", mgpio
);
311 dev_err(&pdev
->dev
, "failed to request IRQ: %d\n", ret
);
315 gpiochip_set_nested_irqchip(&mgpio
->gpio_chip
, &max77620_gpio_irqchip
,
321 static const struct platform_device_id max77620_gpio_devtype
[] = {
322 { .name
= "max77620-gpio", },
323 { .name
= "max20024-gpio", },
326 MODULE_DEVICE_TABLE(platform
, max77620_gpio_devtype
);
328 static struct platform_driver max77620_gpio_driver
= {
329 .driver
.name
= "max77620-gpio",
330 .probe
= max77620_gpio_probe
,
331 .id_table
= max77620_gpio_devtype
,
334 module_platform_driver(max77620_gpio_driver
);
336 MODULE_DESCRIPTION("GPIO interface for MAX77620 and MAX20024 PMIC");
337 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
338 MODULE_AUTHOR("Chaitanya Bandi <bandik@nvidia.com>");
339 MODULE_ALIAS("platform:max77620-gpio");
340 MODULE_LICENSE("GPL v2");