1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2018 BayLibre SAS
4 // Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
6 // GPIO driver for MAXIM 77650/77651 charger/power-supply.
8 #include <linux/gpio/driver.h>
10 #include <linux/mfd/max77650.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
15 #define MAX77650_GPIO_DIR_MASK BIT(0)
16 #define MAX77650_GPIO_INVAL_MASK BIT(1)
17 #define MAX77650_GPIO_DRV_MASK BIT(2)
18 #define MAX77650_GPIO_OUTVAL_MASK BIT(3)
19 #define MAX77650_GPIO_DEBOUNCE_MASK BIT(4)
21 #define MAX77650_GPIO_DIR_OUT 0x00
22 #define MAX77650_GPIO_DIR_IN BIT(0)
23 #define MAX77650_GPIO_OUT_LOW 0x00
24 #define MAX77650_GPIO_OUT_HIGH BIT(3)
25 #define MAX77650_GPIO_DRV_OPEN_DRAIN 0x00
26 #define MAX77650_GPIO_DRV_PUSH_PULL BIT(2)
27 #define MAX77650_GPIO_DEBOUNCE BIT(4)
29 #define MAX77650_GPIO_DIR_BITS(_reg) \
30 ((_reg) & MAX77650_GPIO_DIR_MASK)
31 #define MAX77650_GPIO_INVAL_BITS(_reg) \
32 (((_reg) & MAX77650_GPIO_INVAL_MASK) >> 1)
34 struct max77650_gpio_chip {
40 static int max77650_gpio_direction_input(struct gpio_chip *gc,
43 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
45 return regmap_update_bits(chip->map,
46 MAX77650_REG_CNFG_GPIO,
47 MAX77650_GPIO_DIR_MASK,
48 MAX77650_GPIO_DIR_IN);
51 static int max77650_gpio_direction_output(struct gpio_chip *gc,
52 unsigned int offset, int value)
54 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
57 mask = MAX77650_GPIO_DIR_MASK | MAX77650_GPIO_OUTVAL_MASK;
58 regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW;
59 regval |= MAX77650_GPIO_DIR_OUT;
61 return regmap_update_bits(chip->map,
62 MAX77650_REG_CNFG_GPIO, mask, regval);
65 static void max77650_gpio_set_value(struct gpio_chip *gc,
66 unsigned int offset, int value)
68 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
71 regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW;
73 rv = regmap_update_bits(chip->map, MAX77650_REG_CNFG_GPIO,
74 MAX77650_GPIO_OUTVAL_MASK, regval);
76 dev_err(gc->parent, "cannot set GPIO value: %d\n", rv);
79 static int max77650_gpio_get_value(struct gpio_chip *gc,
82 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
86 rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val);
90 return MAX77650_GPIO_INVAL_BITS(val);
93 static int max77650_gpio_get_direction(struct gpio_chip *gc,
96 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
100 rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val);
104 return MAX77650_GPIO_DIR_BITS(val);
107 static int max77650_gpio_set_config(struct gpio_chip *gc,
108 unsigned int offset, unsigned long cfg)
110 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
112 switch (pinconf_to_config_param(cfg)) {
113 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
114 return regmap_update_bits(chip->map,
115 MAX77650_REG_CNFG_GPIO,
116 MAX77650_GPIO_DRV_MASK,
117 MAX77650_GPIO_DRV_OPEN_DRAIN);
118 case PIN_CONFIG_DRIVE_PUSH_PULL:
119 return regmap_update_bits(chip->map,
120 MAX77650_REG_CNFG_GPIO,
121 MAX77650_GPIO_DRV_MASK,
122 MAX77650_GPIO_DRV_PUSH_PULL);
123 case PIN_CONFIG_INPUT_DEBOUNCE:
124 return regmap_update_bits(chip->map,
125 MAX77650_REG_CNFG_GPIO,
126 MAX77650_GPIO_DEBOUNCE_MASK,
127 MAX77650_GPIO_DEBOUNCE);
133 static int max77650_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
135 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
140 static int max77650_gpio_probe(struct platform_device *pdev)
142 struct max77650_gpio_chip *chip;
143 struct device *dev, *parent;
144 struct i2c_client *i2c;
147 parent = dev->parent;
148 i2c = to_i2c_client(parent);
150 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
154 chip->map = dev_get_regmap(parent, NULL);
158 chip->irq = platform_get_irq_byname(pdev, "GPI");
164 chip->gc.label = i2c->name;
165 chip->gc.parent = dev;
166 chip->gc.owner = THIS_MODULE;
167 chip->gc.can_sleep = true;
169 chip->gc.direction_input = max77650_gpio_direction_input;
170 chip->gc.direction_output = max77650_gpio_direction_output;
171 chip->gc.set = max77650_gpio_set_value;
172 chip->gc.get = max77650_gpio_get_value;
173 chip->gc.get_direction = max77650_gpio_get_direction;
174 chip->gc.set_config = max77650_gpio_set_config;
175 chip->gc.to_irq = max77650_gpio_to_irq;
177 return devm_gpiochip_add_data(dev, &chip->gc, chip);
180 static struct platform_driver max77650_gpio_driver = {
182 .name = "max77650-gpio",
184 .probe = max77650_gpio_probe,
186 module_platform_driver(max77650_gpio_driver);
188 MODULE_DESCRIPTION("MAXIM 77650/77651 GPIO driver");
189 MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
190 MODULE_LICENSE("GPL v2");
191 MODULE_ALIAS("platform:max77650-gpio");