Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright (C) 2018 ROHM Semiconductors |
| 3 | // gpio-bd70528.c ROHM BD70528MWV gpio driver |
| 4 | |
| 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> |
| 10 | |
| 11 | #define GPIO_IN_REG(offset) (BD70528_REG_GPIO1_IN + (offset) * 2) |
| 12 | #define GPIO_OUT_REG(offset) (BD70528_REG_GPIO1_OUT + (offset) * 2) |
| 13 | |
| 14 | struct bd70528_gpio { |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 15 | struct regmap *regmap; |
| 16 | struct device *dev; |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 17 | struct gpio_chip gpio; |
| 18 | }; |
| 19 | |
| 20 | static int bd70528_set_debounce(struct bd70528_gpio *bdgpio, |
| 21 | unsigned int offset, unsigned int debounce) |
| 22 | { |
| 23 | u8 val; |
| 24 | |
| 25 | switch (debounce) { |
| 26 | case 0: |
| 27 | val = BD70528_DEBOUNCE_DISABLE; |
| 28 | break; |
Thierry Reding | f88c117 | 2019-11-08 17:07:47 +0100 | [diff] [blame] | 29 | case 1 ... 15000: |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 30 | val = BD70528_DEBOUNCE_15MS; |
| 31 | break; |
Thierry Reding | f88c117 | 2019-11-08 17:07:47 +0100 | [diff] [blame] | 32 | case 15001 ... 30000: |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 33 | val = BD70528_DEBOUNCE_30MS; |
| 34 | break; |
Thierry Reding | f88c117 | 2019-11-08 17:07:47 +0100 | [diff] [blame] | 35 | case 30001 ... 50000: |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 36 | val = BD70528_DEBOUNCE_50MS; |
| 37 | break; |
| 38 | default: |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 39 | dev_err(bdgpio->dev, |
Colin Ian King | cbf2be7 | 2019-06-28 17:14:19 +0100 | [diff] [blame] | 40 | "Invalid debounce value %u\n", debounce); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 41 | return -EINVAL; |
| 42 | } |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 43 | return regmap_update_bits(bdgpio->regmap, GPIO_IN_REG(offset), |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 44 | BD70528_DEBOUNCE_MASK, val); |
| 45 | } |
| 46 | |
| 47 | static int bd70528_get_direction(struct gpio_chip *chip, unsigned int offset) |
| 48 | { |
| 49 | struct bd70528_gpio *bdgpio = gpiochip_get_data(chip); |
| 50 | int val, ret; |
| 51 | |
| 52 | /* Do we need to do something to IRQs here? */ |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 53 | ret = regmap_read(bdgpio->regmap, GPIO_OUT_REG(offset), &val); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 54 | if (ret) { |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 55 | dev_err(bdgpio->dev, "Could not read gpio direction\n"); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 56 | return ret; |
| 57 | } |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 58 | if (val & BD70528_GPIO_OUT_EN_MASK) |
| 59 | return GPIO_LINE_DIRECTION_OUT; |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 60 | |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 61 | return GPIO_LINE_DIRECTION_IN; |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int bd70528_gpio_set_config(struct gpio_chip *chip, unsigned int offset, |
| 65 | unsigned long config) |
| 66 | { |
| 67 | struct bd70528_gpio *bdgpio = gpiochip_get_data(chip); |
| 68 | |
| 69 | switch (pinconf_to_config_param(config)) { |
| 70 | case PIN_CONFIG_DRIVE_OPEN_DRAIN: |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 71 | return regmap_update_bits(bdgpio->regmap, |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 72 | GPIO_OUT_REG(offset), |
| 73 | BD70528_GPIO_DRIVE_MASK, |
| 74 | BD70528_GPIO_OPEN_DRAIN); |
| 75 | break; |
| 76 | case PIN_CONFIG_DRIVE_PUSH_PULL: |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 77 | return regmap_update_bits(bdgpio->regmap, |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 78 | GPIO_OUT_REG(offset), |
| 79 | BD70528_GPIO_DRIVE_MASK, |
| 80 | BD70528_GPIO_PUSH_PULL); |
| 81 | break; |
| 82 | case PIN_CONFIG_INPUT_DEBOUNCE: |
| 83 | return bd70528_set_debounce(bdgpio, offset, |
| 84 | pinconf_to_config_argument(config)); |
| 85 | break; |
| 86 | default: |
| 87 | break; |
| 88 | } |
| 89 | return -ENOTSUPP; |
| 90 | } |
| 91 | |
| 92 | static int bd70528_direction_input(struct gpio_chip *chip, unsigned int offset) |
| 93 | { |
| 94 | struct bd70528_gpio *bdgpio = gpiochip_get_data(chip); |
| 95 | |
| 96 | /* Do we need to do something to IRQs here? */ |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 97 | return regmap_update_bits(bdgpio->regmap, GPIO_OUT_REG(offset), |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 98 | BD70528_GPIO_OUT_EN_MASK, |
| 99 | BD70528_GPIO_OUT_DISABLE); |
| 100 | } |
| 101 | |
| 102 | static void bd70528_gpio_set(struct gpio_chip *chip, unsigned int offset, |
| 103 | int value) |
| 104 | { |
| 105 | int ret; |
| 106 | struct bd70528_gpio *bdgpio = gpiochip_get_data(chip); |
| 107 | u8 val = (value) ? BD70528_GPIO_OUT_HI : BD70528_GPIO_OUT_LO; |
| 108 | |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 109 | ret = regmap_update_bits(bdgpio->regmap, GPIO_OUT_REG(offset), |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 110 | BD70528_GPIO_OUT_MASK, val); |
| 111 | if (ret) |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 112 | dev_err(bdgpio->dev, "Could not set gpio to %d\n", value); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static int bd70528_direction_output(struct gpio_chip *chip, unsigned int offset, |
| 116 | int value) |
| 117 | { |
| 118 | struct bd70528_gpio *bdgpio = gpiochip_get_data(chip); |
| 119 | |
| 120 | bd70528_gpio_set(chip, offset, value); |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 121 | return regmap_update_bits(bdgpio->regmap, GPIO_OUT_REG(offset), |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 122 | BD70528_GPIO_OUT_EN_MASK, |
| 123 | BD70528_GPIO_OUT_ENABLE); |
| 124 | } |
| 125 | |
| 126 | #define GPIO_IN_STATE_MASK(offset) (BD70528_GPIO_IN_STATE_BASE << (offset)) |
| 127 | |
| 128 | static int bd70528_gpio_get_o(struct bd70528_gpio *bdgpio, unsigned int offset) |
| 129 | { |
| 130 | int ret; |
| 131 | unsigned int val; |
| 132 | |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 133 | ret = regmap_read(bdgpio->regmap, GPIO_OUT_REG(offset), &val); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 134 | if (!ret) |
| 135 | ret = !!(val & BD70528_GPIO_OUT_MASK); |
| 136 | else |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 137 | dev_err(bdgpio->dev, "GPIO (out) state read failed\n"); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static int bd70528_gpio_get_i(struct bd70528_gpio *bdgpio, unsigned int offset) |
| 143 | { |
| 144 | unsigned int val; |
| 145 | int ret; |
| 146 | |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 147 | ret = regmap_read(bdgpio->regmap, BD70528_REG_GPIO_STATE, &val); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 148 | |
| 149 | if (!ret) |
| 150 | ret = !(val & GPIO_IN_STATE_MASK(offset)); |
| 151 | else |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 152 | dev_err(bdgpio->dev, "GPIO (in) state read failed\n"); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static int bd70528_gpio_get(struct gpio_chip *chip, unsigned int offset) |
| 158 | { |
Colin Ian King | f8650b8 | 2019-06-29 13:33:06 +0100 | [diff] [blame] | 159 | int ret; |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 160 | struct bd70528_gpio *bdgpio = gpiochip_get_data(chip); |
| 161 | |
| 162 | /* |
| 163 | * There is a race condition where someone might be changing the |
| 164 | * GPIO direction after we get it but before we read the value. But |
| 165 | * application design where GPIO direction may be changed just when |
| 166 | * we read GPIO value would be pointless as reader could not know |
| 167 | * whether the returned high/low state is caused by input or output. |
| 168 | * Or then there must be other ways to mitigate the issue. Thus |
| 169 | * locking would make no sense. |
| 170 | */ |
| 171 | ret = bd70528_get_direction(chip, offset); |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 172 | if (ret == GPIO_LINE_DIRECTION_OUT) |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 173 | ret = bd70528_gpio_get_o(bdgpio, offset); |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 174 | else if (ret == GPIO_LINE_DIRECTION_IN) |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 175 | ret = bd70528_gpio_get_i(bdgpio, offset); |
| 176 | else |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 177 | dev_err(bdgpio->dev, "failed to read GPIO direction\n"); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | static int bd70528_probe(struct platform_device *pdev) |
| 183 | { |
Bartosz Golaszewski | cb38cd7 | 2021-01-06 11:11:33 +0100 | [diff] [blame] | 184 | struct device *dev = &pdev->dev; |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 185 | struct bd70528_gpio *bdgpio; |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 186 | int ret; |
| 187 | |
Bartosz Golaszewski | cb38cd7 | 2021-01-06 11:11:33 +0100 | [diff] [blame] | 188 | bdgpio = devm_kzalloc(dev, sizeof(*bdgpio), GFP_KERNEL); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 189 | if (!bdgpio) |
| 190 | return -ENOMEM; |
Bartosz Golaszewski | cb38cd7 | 2021-01-06 11:11:33 +0100 | [diff] [blame] | 191 | bdgpio->dev = dev; |
| 192 | bdgpio->gpio.parent = dev->parent; |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 193 | bdgpio->gpio.label = "bd70528-gpio"; |
| 194 | bdgpio->gpio.owner = THIS_MODULE; |
| 195 | bdgpio->gpio.get_direction = bd70528_get_direction; |
| 196 | bdgpio->gpio.direction_input = bd70528_direction_input; |
| 197 | bdgpio->gpio.direction_output = bd70528_direction_output; |
| 198 | bdgpio->gpio.set_config = bd70528_gpio_set_config; |
| 199 | bdgpio->gpio.can_sleep = true; |
| 200 | bdgpio->gpio.get = bd70528_gpio_get; |
| 201 | bdgpio->gpio.set = bd70528_gpio_set; |
| 202 | bdgpio->gpio.ngpio = 4; |
| 203 | bdgpio->gpio.base = -1; |
| 204 | #ifdef CONFIG_OF_GPIO |
Bartosz Golaszewski | cb38cd7 | 2021-01-06 11:11:33 +0100 | [diff] [blame] | 205 | bdgpio->gpio.of_node = dev->parent->of_node; |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 206 | #endif |
Bartosz Golaszewski | cb38cd7 | 2021-01-06 11:11:33 +0100 | [diff] [blame] | 207 | bdgpio->regmap = dev_get_regmap(dev->parent, NULL); |
Matti Vaittinen | 82bf0af | 2021-01-05 14:53:35 +0200 | [diff] [blame] | 208 | if (!bdgpio->regmap) |
| 209 | return -ENODEV; |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 210 | |
Bartosz Golaszewski | cb38cd7 | 2021-01-06 11:11:33 +0100 | [diff] [blame] | 211 | ret = devm_gpiochip_add_data(dev, &bdgpio->gpio, bdgpio); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 212 | if (ret) |
Bartosz Golaszewski | cb38cd7 | 2021-01-06 11:11:33 +0100 | [diff] [blame] | 213 | dev_err(dev, "gpio_init: Failed to add bd70528-gpio\n"); |
Matti Vaittinen | 18bc64b | 2019-06-03 10:26:45 +0300 | [diff] [blame] | 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static struct platform_driver bd70528_gpio = { |
| 219 | .driver = { |
| 220 | .name = "bd70528-gpio" |
| 221 | }, |
| 222 | .probe = bd70528_probe, |
| 223 | }; |
| 224 | |
| 225 | module_platform_driver(bd70528_gpio); |
| 226 | |
| 227 | MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>"); |
| 228 | MODULE_DESCRIPTION("BD70528 voltage regulator driver"); |
| 229 | MODULE_LICENSE("GPL"); |
Matti Vaittinen | bd84f28 | 2019-10-23 15:21:50 +0300 | [diff] [blame] | 230 | MODULE_ALIAS("platform:bd70528-gpio"); |