2 * MAXIM MAX77620 GPIO driver
4 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
11 #include <linux/gpio/driver.h>
12 #include <linux/interrupt.h>
13 #include <linux/mfd/max77620.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
18 #define GPIO_REG_ADDR(offset) (MAX77620_REG_GPIO0 + offset)
20 struct max77620_gpio
{
21 struct gpio_chip gpio_chip
;
26 static const struct regmap_irq max77620_gpio_irqs
[] = {
28 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE0
,
29 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
30 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
35 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE1
,
36 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
37 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
42 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE2
,
43 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
44 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
49 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE3
,
50 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
51 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
56 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE4
,
57 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
58 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
63 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE5
,
64 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
65 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
70 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE6
,
71 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
72 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
77 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE7
,
78 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
79 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
85 static const struct regmap_irq_chip max77620_gpio_irq_chip
= {
86 .name
= "max77620-gpio",
87 .irqs
= max77620_gpio_irqs
,
88 .num_irqs
= ARRAY_SIZE(max77620_gpio_irqs
),
93 .status_base
= MAX77620_REG_IRQ_LVL2_GPIO
,
94 .type_base
= MAX77620_REG_GPIO0
,
97 static int max77620_gpio_dir_input(struct gpio_chip
*gc
, unsigned int offset
)
99 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
102 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
103 MAX77620_CNFG_GPIO_DIR_MASK
,
104 MAX77620_CNFG_GPIO_DIR_INPUT
);
106 dev_err(mgpio
->dev
, "CNFG_GPIOx dir update failed: %d\n", ret
);
111 static int max77620_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
113 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
117 ret
= regmap_read(mgpio
->rmap
, GPIO_REG_ADDR(offset
), &val
);
119 dev_err(mgpio
->dev
, "CNFG_GPIOx read failed: %d\n", ret
);
123 if (val
& MAX77620_CNFG_GPIO_DIR_MASK
)
124 return !!(val
& MAX77620_CNFG_GPIO_INPUT_VAL_MASK
);
126 return !!(val
& MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK
);
129 static int max77620_gpio_dir_output(struct gpio_chip
*gc
, unsigned int offset
,
132 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
136 val
= (value
) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH
:
137 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW
;
139 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
140 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK
, val
);
142 dev_err(mgpio
->dev
, "CNFG_GPIOx val update failed: %d\n", ret
);
146 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
147 MAX77620_CNFG_GPIO_DIR_MASK
,
148 MAX77620_CNFG_GPIO_DIR_OUTPUT
);
150 dev_err(mgpio
->dev
, "CNFG_GPIOx dir update failed: %d\n", ret
);
155 static int max77620_gpio_set_debounce(struct max77620_gpio
*mgpio
,
157 unsigned int debounce
)
164 val
= MAX77620_CNFG_GPIO_DBNC_None
;
167 val
= MAX77620_CNFG_GPIO_DBNC_8ms
;
170 val
= MAX77620_CNFG_GPIO_DBNC_16ms
;
173 val
= MAX77620_CNFG_GPIO_DBNC_32ms
;
176 dev_err(mgpio
->dev
, "Illegal value %u\n", debounce
);
180 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
181 MAX77620_CNFG_GPIO_DBNC_MASK
, val
);
183 dev_err(mgpio
->dev
, "CNFG_GPIOx_DBNC update failed: %d\n", ret
);
188 static void max77620_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
191 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
195 val
= (value
) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH
:
196 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW
;
198 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
199 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK
, val
);
201 dev_err(mgpio
->dev
, "CNFG_GPIO_OUT update failed: %d\n", ret
);
204 static int max77620_gpio_set_config(struct gpio_chip
*gc
, unsigned int offset
,
205 unsigned long config
)
207 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
209 switch (pinconf_to_config_param(config
)) {
210 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
211 return regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
212 MAX77620_CNFG_GPIO_DRV_MASK
,
213 MAX77620_CNFG_GPIO_DRV_OPENDRAIN
);
214 case PIN_CONFIG_DRIVE_PUSH_PULL
:
215 return regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
216 MAX77620_CNFG_GPIO_DRV_MASK
,
217 MAX77620_CNFG_GPIO_DRV_PUSHPULL
);
218 case PIN_CONFIG_INPUT_DEBOUNCE
:
219 return max77620_gpio_set_debounce(mgpio
, offset
,
220 pinconf_to_config_argument(config
));
228 static int max77620_gpio_to_irq(struct gpio_chip
*gc
, unsigned int offset
)
230 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
231 struct max77620_chip
*chip
= dev_get_drvdata(mgpio
->dev
->parent
);
233 return regmap_irq_get_virq(chip
->gpio_irq_data
, offset
);
236 static int max77620_gpio_probe(struct platform_device
*pdev
)
238 struct max77620_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
239 struct max77620_gpio
*mgpio
;
243 gpio_irq
= platform_get_irq(pdev
, 0);
245 dev_err(&pdev
->dev
, "GPIO irq not available %d\n", gpio_irq
);
249 mgpio
= devm_kzalloc(&pdev
->dev
, sizeof(*mgpio
), GFP_KERNEL
);
253 mgpio
->rmap
= chip
->rmap
;
254 mgpio
->dev
= &pdev
->dev
;
256 mgpio
->gpio_chip
.label
= pdev
->name
;
257 mgpio
->gpio_chip
.parent
= &pdev
->dev
;
258 mgpio
->gpio_chip
.direction_input
= max77620_gpio_dir_input
;
259 mgpio
->gpio_chip
.get
= max77620_gpio_get
;
260 mgpio
->gpio_chip
.direction_output
= max77620_gpio_dir_output
;
261 mgpio
->gpio_chip
.set
= max77620_gpio_set
;
262 mgpio
->gpio_chip
.set_config
= max77620_gpio_set_config
;
263 mgpio
->gpio_chip
.to_irq
= max77620_gpio_to_irq
;
264 mgpio
->gpio_chip
.ngpio
= MAX77620_GPIO_NR
;
265 mgpio
->gpio_chip
.can_sleep
= 1;
266 mgpio
->gpio_chip
.base
= -1;
267 #ifdef CONFIG_OF_GPIO
268 mgpio
->gpio_chip
.of_node
= pdev
->dev
.parent
->of_node
;
271 platform_set_drvdata(pdev
, mgpio
);
273 ret
= devm_gpiochip_add_data(&pdev
->dev
, &mgpio
->gpio_chip
, mgpio
);
275 dev_err(&pdev
->dev
, "gpio_init: Failed to add max77620_gpio\n");
279 ret
= devm_regmap_add_irq_chip(&pdev
->dev
, chip
->rmap
, gpio_irq
,
281 &max77620_gpio_irq_chip
,
282 &chip
->gpio_irq_data
);
284 dev_err(&pdev
->dev
, "Failed to add gpio irq_chip %d\n", ret
);
291 static const struct platform_device_id max77620_gpio_devtype
[] = {
292 { .name
= "max77620-gpio", },
293 { .name
= "max20024-gpio", },
296 MODULE_DEVICE_TABLE(platform
, max77620_gpio_devtype
);
298 static struct platform_driver max77620_gpio_driver
= {
299 .driver
.name
= "max77620-gpio",
300 .probe
= max77620_gpio_probe
,
301 .id_table
= max77620_gpio_devtype
,
304 module_platform_driver(max77620_gpio_driver
);
306 MODULE_DESCRIPTION("GPIO interface for MAX77620 and MAX20024 PMIC");
307 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
308 MODULE_AUTHOR("Chaitanya Bandi <bandik@nvidia.com>");
309 MODULE_ALIAS("platform:max77620-gpio");
310 MODULE_LICENSE("GPL v2");