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[] = {
29 .mask = MAX77620_IRQ_LVL2_GPIO_EDGE0,
31 .type_rising_val = MAX77620_CNFG_GPIO_INT_RISING,
32 .type_falling_val = MAX77620_CNFG_GPIO_INT_FALLING,
33 .type_reg_mask = MAX77620_CNFG_GPIO_INT_MASK,
35 .types_supported = IRQ_TYPE_EDGE_BOTH,
40 .mask = MAX77620_IRQ_LVL2_GPIO_EDGE1,
42 .type_rising_val = MAX77620_CNFG_GPIO_INT_RISING,
43 .type_falling_val = MAX77620_CNFG_GPIO_INT_FALLING,
44 .type_reg_mask = MAX77620_CNFG_GPIO_INT_MASK,
46 .types_supported = IRQ_TYPE_EDGE_BOTH,
51 .mask = MAX77620_IRQ_LVL2_GPIO_EDGE2,
53 .type_rising_val = MAX77620_CNFG_GPIO_INT_RISING,
54 .type_falling_val = MAX77620_CNFG_GPIO_INT_FALLING,
55 .type_reg_mask = MAX77620_CNFG_GPIO_INT_MASK,
57 .types_supported = IRQ_TYPE_EDGE_BOTH,
62 .mask = MAX77620_IRQ_LVL2_GPIO_EDGE3,
64 .type_rising_val = MAX77620_CNFG_GPIO_INT_RISING,
65 .type_falling_val = MAX77620_CNFG_GPIO_INT_FALLING,
66 .type_reg_mask = MAX77620_CNFG_GPIO_INT_MASK,
68 .types_supported = IRQ_TYPE_EDGE_BOTH,
73 .mask = MAX77620_IRQ_LVL2_GPIO_EDGE4,
75 .type_rising_val = MAX77620_CNFG_GPIO_INT_RISING,
76 .type_falling_val = MAX77620_CNFG_GPIO_INT_FALLING,
77 .type_reg_mask = MAX77620_CNFG_GPIO_INT_MASK,
79 .types_supported = IRQ_TYPE_EDGE_BOTH,
84 .mask = MAX77620_IRQ_LVL2_GPIO_EDGE5,
86 .type_rising_val = MAX77620_CNFG_GPIO_INT_RISING,
87 .type_falling_val = MAX77620_CNFG_GPIO_INT_FALLING,
88 .type_reg_mask = MAX77620_CNFG_GPIO_INT_MASK,
90 .types_supported = IRQ_TYPE_EDGE_BOTH,
95 .mask = MAX77620_IRQ_LVL2_GPIO_EDGE6,
97 .type_rising_val = MAX77620_CNFG_GPIO_INT_RISING,
98 .type_falling_val = MAX77620_CNFG_GPIO_INT_FALLING,
99 .type_reg_mask = MAX77620_CNFG_GPIO_INT_MASK,
100 .type_reg_offset = 6,
101 .types_supported = IRQ_TYPE_EDGE_BOTH,
106 .mask = MAX77620_IRQ_LVL2_GPIO_EDGE7,
108 .type_rising_val = MAX77620_CNFG_GPIO_INT_RISING,
109 .type_falling_val = MAX77620_CNFG_GPIO_INT_FALLING,
110 .type_reg_mask = MAX77620_CNFG_GPIO_INT_MASK,
111 .type_reg_offset = 7,
112 .types_supported = IRQ_TYPE_EDGE_BOTH,
117 static const struct regmap_irq_chip max77620_gpio_irq_chip = {
118 .name = "max77620-gpio",
119 .irqs = max77620_gpio_irqs,
120 .num_irqs = ARRAY_SIZE(max77620_gpio_irqs),
124 .type_reg_stride = 1,
125 .status_base = MAX77620_REG_IRQ_LVL2_GPIO,
126 .type_base = MAX77620_REG_GPIO0,
129 static int max77620_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
131 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
134 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
135 MAX77620_CNFG_GPIO_DIR_MASK,
136 MAX77620_CNFG_GPIO_DIR_INPUT);
138 dev_err(mgpio->dev, "CNFG_GPIOx dir update failed: %d\n", ret);
143 static int max77620_gpio_get(struct gpio_chip *gc, unsigned int offset)
145 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
149 ret = regmap_read(mgpio->rmap, GPIO_REG_ADDR(offset), &val);
151 dev_err(mgpio->dev, "CNFG_GPIOx read failed: %d\n", ret);
155 if (val & MAX77620_CNFG_GPIO_DIR_MASK)
156 return !!(val & MAX77620_CNFG_GPIO_INPUT_VAL_MASK);
158 return !!(val & MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK);
161 static int max77620_gpio_dir_output(struct gpio_chip *gc, unsigned int offset,
164 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
168 val = (value) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH :
169 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW;
171 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
172 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK, val);
174 dev_err(mgpio->dev, "CNFG_GPIOx val update failed: %d\n", ret);
178 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
179 MAX77620_CNFG_GPIO_DIR_MASK,
180 MAX77620_CNFG_GPIO_DIR_OUTPUT);
182 dev_err(mgpio->dev, "CNFG_GPIOx dir update failed: %d\n", ret);
187 static int max77620_gpio_set_debounce(struct max77620_gpio *mgpio,
189 unsigned int debounce)
196 val = MAX77620_CNFG_GPIO_DBNC_None;
199 val = MAX77620_CNFG_GPIO_DBNC_8ms;
202 val = MAX77620_CNFG_GPIO_DBNC_16ms;
205 val = MAX77620_CNFG_GPIO_DBNC_32ms;
208 dev_err(mgpio->dev, "Illegal value %u\n", debounce);
212 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
213 MAX77620_CNFG_GPIO_DBNC_MASK, val);
215 dev_err(mgpio->dev, "CNFG_GPIOx_DBNC update failed: %d\n", ret);
220 static void max77620_gpio_set(struct gpio_chip *gc, unsigned int offset,
223 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
227 val = (value) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH :
228 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW;
230 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
231 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK, val);
233 dev_err(mgpio->dev, "CNFG_GPIO_OUT update failed: %d\n", ret);
236 static int max77620_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
237 unsigned long config)
239 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
241 switch (pinconf_to_config_param(config)) {
242 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
243 return regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
244 MAX77620_CNFG_GPIO_DRV_MASK,
245 MAX77620_CNFG_GPIO_DRV_OPENDRAIN);
246 case PIN_CONFIG_DRIVE_PUSH_PULL:
247 return regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
248 MAX77620_CNFG_GPIO_DRV_MASK,
249 MAX77620_CNFG_GPIO_DRV_PUSHPULL);
250 case PIN_CONFIG_INPUT_DEBOUNCE:
251 return max77620_gpio_set_debounce(mgpio, offset,
252 pinconf_to_config_argument(config));
260 static int max77620_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
262 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
263 struct max77620_chip *chip = dev_get_drvdata(mgpio->dev->parent);
265 return regmap_irq_get_virq(chip->gpio_irq_data, offset);
268 static int max77620_gpio_probe(struct platform_device *pdev)
270 struct max77620_chip *chip = dev_get_drvdata(pdev->dev.parent);
271 struct max77620_gpio *mgpio;
275 gpio_irq = platform_get_irq(pdev, 0);
277 dev_err(&pdev->dev, "GPIO irq not available %d\n", gpio_irq);
281 mgpio = devm_kzalloc(&pdev->dev, sizeof(*mgpio), GFP_KERNEL);
285 mgpio->rmap = chip->rmap;
286 mgpio->dev = &pdev->dev;
288 mgpio->gpio_chip.label = pdev->name;
289 mgpio->gpio_chip.parent = &pdev->dev;
290 mgpio->gpio_chip.direction_input = max77620_gpio_dir_input;
291 mgpio->gpio_chip.get = max77620_gpio_get;
292 mgpio->gpio_chip.direction_output = max77620_gpio_dir_output;
293 mgpio->gpio_chip.set = max77620_gpio_set;
294 mgpio->gpio_chip.set_config = max77620_gpio_set_config;
295 mgpio->gpio_chip.to_irq = max77620_gpio_to_irq;
296 mgpio->gpio_chip.ngpio = MAX77620_GPIO_NR;
297 mgpio->gpio_chip.can_sleep = 1;
298 mgpio->gpio_chip.base = -1;
299 #ifdef CONFIG_OF_GPIO
300 mgpio->gpio_chip.of_node = pdev->dev.parent->of_node;
303 platform_set_drvdata(pdev, mgpio);
305 ret = devm_gpiochip_add_data(&pdev->dev, &mgpio->gpio_chip, mgpio);
307 dev_err(&pdev->dev, "gpio_init: Failed to add max77620_gpio\n");
311 ret = devm_regmap_add_irq_chip(&pdev->dev, chip->rmap, gpio_irq,
313 &max77620_gpio_irq_chip,
314 &chip->gpio_irq_data);
316 dev_err(&pdev->dev, "Failed to add gpio irq_chip %d\n", ret);
323 static const struct platform_device_id max77620_gpio_devtype[] = {
324 { .name = "max77620-gpio", },
325 { .name = "max20024-gpio", },
328 MODULE_DEVICE_TABLE(platform, max77620_gpio_devtype);
330 static struct platform_driver max77620_gpio_driver = {
331 .driver.name = "max77620-gpio",
332 .probe = max77620_gpio_probe,
333 .id_table = max77620_gpio_devtype,
336 module_platform_driver(max77620_gpio_driver);
338 MODULE_DESCRIPTION("GPIO interface for MAX77620 and MAX20024 PMIC");
341 MODULE_ALIAS("platform:max77620-gpio");
342 MODULE_LICENSE("GPL v2");