1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 ROHM Semiconductors
3 // gpio-bd70528.c ROHM BD70528MWV gpio driver
5 #include <linux/gpio/driver.h>
6 #include <linux/mfd/rohm-bd70528.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/regmap.h>
11 #define GPIO_IN_REG(offset) (BD70528_REG_GPIO1_IN + (offset) * 2)
12 #define GPIO_OUT_REG(offset) (BD70528_REG_GPIO1_OUT + (offset) * 2)
15 struct rohm_regmap_dev chip;
16 struct gpio_chip gpio;
19 static int bd70528_set_debounce(struct bd70528_gpio *bdgpio,
20 unsigned int offset, unsigned int debounce)
26 val = BD70528_DEBOUNCE_DISABLE;
29 val = BD70528_DEBOUNCE_15MS;
32 val = BD70528_DEBOUNCE_30MS;
35 val = BD70528_DEBOUNCE_50MS;
38 dev_err(bdgpio->chip.dev,
39 "Invalid debounce value %u\n", debounce);
42 return regmap_update_bits(bdgpio->chip.regmap, GPIO_IN_REG(offset),
43 BD70528_DEBOUNCE_MASK, val);
46 static int bd70528_get_direction(struct gpio_chip *chip, unsigned int offset)
48 struct bd70528_gpio *bdgpio = gpiochip_get_data(chip);
51 /* Do we need to do something to IRQs here? */
52 ret = regmap_read(bdgpio->chip.regmap, GPIO_OUT_REG(offset), &val);
54 dev_err(bdgpio->chip.dev, "Could not read gpio direction\n");
58 return !(val & BD70528_GPIO_OUT_EN_MASK);
61 static int bd70528_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
64 struct bd70528_gpio *bdgpio = gpiochip_get_data(chip);
66 switch (pinconf_to_config_param(config)) {
67 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
68 return regmap_update_bits(bdgpio->chip.regmap,
70 BD70528_GPIO_DRIVE_MASK,
71 BD70528_GPIO_OPEN_DRAIN);
73 case PIN_CONFIG_DRIVE_PUSH_PULL:
74 return regmap_update_bits(bdgpio->chip.regmap,
76 BD70528_GPIO_DRIVE_MASK,
77 BD70528_GPIO_PUSH_PULL);
79 case PIN_CONFIG_INPUT_DEBOUNCE:
80 return bd70528_set_debounce(bdgpio, offset,
81 pinconf_to_config_argument(config));
89 static int bd70528_direction_input(struct gpio_chip *chip, unsigned int offset)
91 struct bd70528_gpio *bdgpio = gpiochip_get_data(chip);
93 /* Do we need to do something to IRQs here? */
94 return regmap_update_bits(bdgpio->chip.regmap, GPIO_OUT_REG(offset),
95 BD70528_GPIO_OUT_EN_MASK,
96 BD70528_GPIO_OUT_DISABLE);
99 static void bd70528_gpio_set(struct gpio_chip *chip, unsigned int offset,
103 struct bd70528_gpio *bdgpio = gpiochip_get_data(chip);
104 u8 val = (value) ? BD70528_GPIO_OUT_HI : BD70528_GPIO_OUT_LO;
106 ret = regmap_update_bits(bdgpio->chip.regmap, GPIO_OUT_REG(offset),
107 BD70528_GPIO_OUT_MASK, val);
109 dev_err(bdgpio->chip.dev, "Could not set gpio to %d\n", value);
112 static int bd70528_direction_output(struct gpio_chip *chip, unsigned int offset,
115 struct bd70528_gpio *bdgpio = gpiochip_get_data(chip);
117 bd70528_gpio_set(chip, offset, value);
118 return regmap_update_bits(bdgpio->chip.regmap, GPIO_OUT_REG(offset),
119 BD70528_GPIO_OUT_EN_MASK,
120 BD70528_GPIO_OUT_ENABLE);
123 #define GPIO_IN_STATE_MASK(offset) (BD70528_GPIO_IN_STATE_BASE << (offset))
125 static int bd70528_gpio_get_o(struct bd70528_gpio *bdgpio, unsigned int offset)
130 ret = regmap_read(bdgpio->chip.regmap, GPIO_OUT_REG(offset), &val);
132 ret = !!(val & BD70528_GPIO_OUT_MASK);
134 dev_err(bdgpio->chip.dev, "GPIO (out) state read failed\n");
139 static int bd70528_gpio_get_i(struct bd70528_gpio *bdgpio, unsigned int offset)
144 ret = regmap_read(bdgpio->chip.regmap, BD70528_REG_GPIO_STATE, &val);
147 ret = !(val & GPIO_IN_STATE_MASK(offset));
149 dev_err(bdgpio->chip.dev, "GPIO (in) state read failed\n");
154 static int bd70528_gpio_get(struct gpio_chip *chip, unsigned int offset)
157 struct bd70528_gpio *bdgpio = gpiochip_get_data(chip);
160 * There is a race condition where someone might be changing the
161 * GPIO direction after we get it but before we read the value. But
162 * application design where GPIO direction may be changed just when
163 * we read GPIO value would be pointless as reader could not know
164 * whether the returned high/low state is caused by input or output.
165 * Or then there must be other ways to mitigate the issue. Thus
166 * locking would make no sense.
168 ret = bd70528_get_direction(chip, offset);
170 ret = bd70528_gpio_get_o(bdgpio, offset);
172 ret = bd70528_gpio_get_i(bdgpio, offset);
174 dev_err(bdgpio->chip.dev, "failed to read GPIO direction\n");
179 static int bd70528_probe(struct platform_device *pdev)
181 struct bd70528_gpio *bdgpio;
182 struct rohm_regmap_dev *bd70528;
185 bd70528 = dev_get_drvdata(pdev->dev.parent);
187 dev_err(&pdev->dev, "No MFD driver data\n");
191 bdgpio = devm_kzalloc(&pdev->dev, sizeof(*bdgpio),
195 bdgpio->chip.dev = &pdev->dev;
196 bdgpio->gpio.parent = pdev->dev.parent;
197 bdgpio->gpio.label = "bd70528-gpio";
198 bdgpio->gpio.owner = THIS_MODULE;
199 bdgpio->gpio.get_direction = bd70528_get_direction;
200 bdgpio->gpio.direction_input = bd70528_direction_input;
201 bdgpio->gpio.direction_output = bd70528_direction_output;
202 bdgpio->gpio.set_config = bd70528_gpio_set_config;
203 bdgpio->gpio.can_sleep = true;
204 bdgpio->gpio.get = bd70528_gpio_get;
205 bdgpio->gpio.set = bd70528_gpio_set;
206 bdgpio->gpio.ngpio = 4;
207 bdgpio->gpio.base = -1;
208 #ifdef CONFIG_OF_GPIO
209 bdgpio->gpio.of_node = pdev->dev.parent->of_node;
211 bdgpio->chip.regmap = bd70528->regmap;
213 ret = devm_gpiochip_add_data(&pdev->dev, &bdgpio->gpio,
216 dev_err(&pdev->dev, "gpio_init: Failed to add bd70528-gpio\n");
221 static struct platform_driver bd70528_gpio = {
223 .name = "bd70528-gpio"
225 .probe = bd70528_probe,
228 module_platform_driver(bd70528_gpio);
230 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
231 MODULE_DESCRIPTION("BD70528 voltage regulator driver");
232 MODULE_LICENSE("GPL");