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
;
29 static const struct regmap_irq max77620_gpio_irqs
[] = {
31 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE0
,
32 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
33 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
38 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE1
,
39 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
40 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
45 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE2
,
46 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
47 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
52 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE3
,
53 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
54 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
59 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE4
,
60 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
61 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
66 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE5
,
67 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
68 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
73 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE6
,
74 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
75 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
80 .mask
= MAX77620_IRQ_LVL2_GPIO_EDGE7
,
81 .type_rising_mask
= MAX77620_CNFG_GPIO_INT_RISING
,
82 .type_falling_mask
= MAX77620_CNFG_GPIO_INT_FALLING
,
88 static struct regmap_irq_chip max77620_gpio_irq_chip
= {
89 .name
= "max77620-gpio",
90 .irqs
= max77620_gpio_irqs
,
91 .num_irqs
= ARRAY_SIZE(max77620_gpio_irqs
),
96 .status_base
= MAX77620_REG_IRQ_LVL2_GPIO
,
97 .type_base
= MAX77620_REG_GPIO0
,
100 static int max77620_gpio_dir_input(struct gpio_chip
*gc
, unsigned int offset
)
102 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
105 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
106 MAX77620_CNFG_GPIO_DIR_MASK
,
107 MAX77620_CNFG_GPIO_DIR_INPUT
);
109 dev_err(mgpio
->dev
, "CNFG_GPIOx dir update failed: %d\n", ret
);
114 static int max77620_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
116 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
120 ret
= regmap_read(mgpio
->rmap
, GPIO_REG_ADDR(offset
), &val
);
122 dev_err(mgpio
->dev
, "CNFG_GPIOx read failed: %d\n", ret
);
126 if (val
& MAX77620_CNFG_GPIO_DIR_MASK
)
127 return !!(val
& MAX77620_CNFG_GPIO_INPUT_VAL_MASK
);
129 return !!(val
& MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK
);
132 static int max77620_gpio_dir_output(struct gpio_chip
*gc
, unsigned int offset
,
135 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
139 val
= (value
) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH
:
140 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW
;
142 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
143 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK
, val
);
145 dev_err(mgpio
->dev
, "CNFG_GPIOx val update failed: %d\n", ret
);
149 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
150 MAX77620_CNFG_GPIO_DIR_MASK
,
151 MAX77620_CNFG_GPIO_DIR_OUTPUT
);
153 dev_err(mgpio
->dev
, "CNFG_GPIOx dir update failed: %d\n", ret
);
158 static int max77620_gpio_set_debounce(struct gpio_chip
*gc
,
160 unsigned int debounce
)
162 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
168 val
= MAX77620_CNFG_GPIO_DBNC_None
;
171 val
= MAX77620_CNFG_GPIO_DBNC_8ms
;
174 val
= MAX77620_CNFG_GPIO_DBNC_16ms
;
177 val
= MAX77620_CNFG_GPIO_DBNC_32ms
;
180 dev_err(mgpio
->dev
, "Illegal value %u\n", debounce
);
184 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
185 MAX77620_CNFG_GPIO_DBNC_MASK
, val
);
187 dev_err(mgpio
->dev
, "CNFG_GPIOx_DBNC update failed: %d\n", ret
);
192 static void max77620_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
195 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
199 val
= (value
) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH
:
200 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW
;
202 ret
= regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
203 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK
, val
);
205 dev_err(mgpio
->dev
, "CNFG_GPIO_OUT update failed: %d\n", ret
);
208 static int max77620_gpio_set_single_ended(struct gpio_chip
*gc
,
210 enum single_ended_mode mode
)
212 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
215 case LINE_MODE_OPEN_DRAIN
:
216 return regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
217 MAX77620_CNFG_GPIO_DRV_MASK
,
218 MAX77620_CNFG_GPIO_DRV_OPENDRAIN
);
219 case LINE_MODE_PUSH_PULL
:
220 return regmap_update_bits(mgpio
->rmap
, GPIO_REG_ADDR(offset
),
221 MAX77620_CNFG_GPIO_DRV_MASK
,
222 MAX77620_CNFG_GPIO_DRV_PUSHPULL
);
230 static int max77620_gpio_to_irq(struct gpio_chip
*gc
, unsigned int offset
)
232 struct max77620_gpio
*mgpio
= gpiochip_get_data(gc
);
233 struct max77620_chip
*chip
= dev_get_drvdata(mgpio
->dev
->parent
);
235 return regmap_irq_get_virq(chip
->gpio_irq_data
, offset
);
238 static int max77620_gpio_probe(struct platform_device
*pdev
)
240 struct max77620_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
241 struct max77620_gpio
*mgpio
;
245 gpio_irq
= platform_get_irq(pdev
, 0);
247 dev_err(&pdev
->dev
, "GPIO irq not available %d\n", gpio_irq
);
251 mgpio
= devm_kzalloc(&pdev
->dev
, sizeof(*mgpio
), GFP_KERNEL
);
255 mgpio
->rmap
= chip
->rmap
;
256 mgpio
->dev
= &pdev
->dev
;
257 mgpio
->gpio_irq
= gpio_irq
;
259 mgpio
->gpio_chip
.label
= pdev
->name
;
260 mgpio
->gpio_chip
.parent
= &pdev
->dev
;
261 mgpio
->gpio_chip
.direction_input
= max77620_gpio_dir_input
;
262 mgpio
->gpio_chip
.get
= max77620_gpio_get
;
263 mgpio
->gpio_chip
.direction_output
= max77620_gpio_dir_output
;
264 mgpio
->gpio_chip
.set_debounce
= max77620_gpio_set_debounce
;
265 mgpio
->gpio_chip
.set
= max77620_gpio_set
;
266 mgpio
->gpio_chip
.set_single_ended
= max77620_gpio_set_single_ended
;
267 mgpio
->gpio_chip
.to_irq
= max77620_gpio_to_irq
;
268 mgpio
->gpio_chip
.ngpio
= MAX77620_GPIO_NR
;
269 mgpio
->gpio_chip
.can_sleep
= 1;
270 mgpio
->gpio_chip
.base
= -1;
271 mgpio
->irq_base
= -1;
272 #ifdef CONFIG_OF_GPIO
273 mgpio
->gpio_chip
.of_node
= pdev
->dev
.parent
->of_node
;
276 platform_set_drvdata(pdev
, mgpio
);
278 ret
= devm_gpiochip_add_data(&pdev
->dev
, &mgpio
->gpio_chip
, mgpio
);
280 dev_err(&pdev
->dev
, "gpio_init: Failed to add max77620_gpio\n");
284 mgpio
->gpio_base
= mgpio
->gpio_chip
.base
;
285 ret
= devm_regmap_add_irq_chip(&pdev
->dev
, chip
->rmap
, mgpio
->gpio_irq
,
286 IRQF_ONESHOT
, mgpio
->irq_base
,
287 &max77620_gpio_irq_chip
,
288 &chip
->gpio_irq_data
);
290 dev_err(&pdev
->dev
, "Failed to add gpio irq_chip %d\n", ret
);
297 static const struct platform_device_id max77620_gpio_devtype
[] = {
298 { .name
= "max77620-gpio", },
301 MODULE_DEVICE_TABLE(platform
, max77620_gpio_devtype
);
303 static struct platform_driver max77620_gpio_driver
= {
304 .driver
.name
= "max77620-gpio",
305 .probe
= max77620_gpio_probe
,
306 .id_table
= max77620_gpio_devtype
,
309 module_platform_driver(max77620_gpio_driver
);
311 MODULE_DESCRIPTION("GPIO interface for MAX77620 and MAX20024 PMIC");
312 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
313 MODULE_AUTHOR("Chaitanya Bandi <bandik@nvidia.com>");
314 MODULE_ALIAS("platform:max77620-gpio");
315 MODULE_LICENSE("GPL v2");