WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpio / gpio-max77650.c
blob3075f2513c6f08981c4d4de772d19029d52248af
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (C) 2018 BayLibre SAS
4 // Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
5 //
6 // GPIO driver for MAXIM 77650/77651 charger/power-supply.
8 #include <linux/gpio/driver.h>
9 #include <linux/i2c.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 {
35 struct regmap *map;
36 struct gpio_chip gc;
37 int irq;
40 static int max77650_gpio_direction_input(struct gpio_chip *gc,
41 unsigned int offset)
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);
55 int mask, regval;
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);
69 int rv, regval;
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);
75 if (rv)
76 dev_err(gc->parent, "cannot set GPIO value: %d\n", rv);
79 static int max77650_gpio_get_value(struct gpio_chip *gc,
80 unsigned int offset)
82 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
83 unsigned int val;
84 int rv;
86 rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val);
87 if (rv)
88 return rv;
90 return MAX77650_GPIO_INVAL_BITS(val);
93 static int max77650_gpio_get_direction(struct gpio_chip *gc,
94 unsigned int offset)
96 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
97 unsigned int val;
98 int rv;
100 rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val);
101 if (rv)
102 return rv;
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);
128 default:
129 return -ENOTSUPP;
133 static int max77650_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
135 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
137 return chip->irq;
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;
146 dev = &pdev->dev;
147 parent = dev->parent;
148 i2c = to_i2c_client(parent);
150 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
151 if (!chip)
152 return -ENOMEM;
154 chip->map = dev_get_regmap(parent, NULL);
155 if (!chip->map)
156 return -ENODEV;
158 chip->irq = platform_get_irq_byname(pdev, "GPI");
159 if (chip->irq < 0)
160 return chip->irq;
162 chip->gc.base = -1;
163 chip->gc.ngpio = 1;
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 = {
181 .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");