1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * regmap based generic GPIO driver 4 * 5 * Copyright 2020 Michael Walle <michael@walle.cc> 6 */ 7 8 #include <linux/gpio/driver.h> 9 #include <linux/gpio/regmap.h> 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/regmap.h> 13 14 struct gpio_regmap { 15 struct device *parent; 16 struct regmap *regmap; 17 struct gpio_chip gpio_chip; 18 19 int reg_stride; 20 int ngpio_per_reg; 21 unsigned int reg_dat_base; 22 unsigned int reg_set_base; 23 unsigned int reg_clr_base; 24 unsigned int reg_dir_in_base; 25 unsigned int reg_dir_out_base; 26 27 int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int base, 28 unsigned int offset, unsigned int *reg, 29 unsigned int *mask); 30 31 void *driver_data; 32 }; 33 34 static unsigned int gpio_regmap_addr(unsigned int addr) 35 { 36 if (addr == GPIO_REGMAP_ADDR_ZERO) 37 return 0; 38 39 return addr; 40 } 41 42 static int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, 43 unsigned int base, unsigned int offset, 44 unsigned int *reg, unsigned int *mask) 45 { 46 unsigned int line = offset % gpio->ngpio_per_reg; 47 unsigned int stride = offset / gpio->ngpio_per_reg; 48 49 *reg = base + stride * gpio->reg_stride; 50 *mask = BIT(line); 51 52 return 0; 53 } 54 55 static int gpio_regmap_get(struct gpio_chip *chip, unsigned int offset) 56 { 57 struct gpio_regmap *gpio = gpiochip_get_data(chip); 58 unsigned int base, val, reg, mask; 59 int ret; 60 61 /* we might not have an output register if we are input only */ 62 if (gpio->reg_dat_base) 63 base = gpio_regmap_addr(gpio->reg_dat_base); 64 else 65 base = gpio_regmap_addr(gpio->reg_set_base); 66 67 ret = gpio->reg_mask_xlate(gpio, base, offset, ®, &mask); 68 if (ret) 69 return ret; 70 71 ret = regmap_read(gpio->regmap, reg, &val); 72 if (ret) 73 return ret; 74 75 return !!(val & mask); 76 } 77 78 static void gpio_regmap_set(struct gpio_chip *chip, unsigned int offset, 79 int val) 80 { 81 struct gpio_regmap *gpio = gpiochip_get_data(chip); 82 unsigned int base = gpio_regmap_addr(gpio->reg_set_base); 83 unsigned int reg, mask; 84 85 gpio->reg_mask_xlate(gpio, base, offset, ®, &mask); 86 if (val) 87 regmap_update_bits(gpio->regmap, reg, mask, mask); 88 else 89 regmap_update_bits(gpio->regmap, reg, mask, 0); 90 } 91 92 static void gpio_regmap_set_with_clear(struct gpio_chip *chip, 93 unsigned int offset, int val) 94 { 95 struct gpio_regmap *gpio = gpiochip_get_data(chip); 96 unsigned int base, reg, mask; 97 98 if (val) 99 base = gpio_regmap_addr(gpio->reg_set_base); 100 else 101 base = gpio_regmap_addr(gpio->reg_clr_base); 102 103 gpio->reg_mask_xlate(gpio, base, offset, ®, &mask); 104 regmap_write(gpio->regmap, reg, mask); 105 } 106 107 static int gpio_regmap_get_direction(struct gpio_chip *chip, 108 unsigned int offset) 109 { 110 struct gpio_regmap *gpio = gpiochip_get_data(chip); 111 unsigned int base, val, reg, mask; 112 int invert, ret; 113 114 if (gpio->reg_dat_base && !gpio->reg_set_base) 115 return GPIO_LINE_DIRECTION_IN; 116 if (gpio->reg_set_base && !gpio->reg_dat_base) 117 return GPIO_LINE_DIRECTION_OUT; 118 119 if (gpio->reg_dir_out_base) { 120 base = gpio_regmap_addr(gpio->reg_dir_out_base); 121 invert = 0; 122 } else if (gpio->reg_dir_in_base) { 123 base = gpio_regmap_addr(gpio->reg_dir_in_base); 124 invert = 1; 125 } else { 126 return -EOPNOTSUPP; 127 } 128 129 ret = gpio->reg_mask_xlate(gpio, base, offset, ®, &mask); 130 if (ret) 131 return ret; 132 133 ret = regmap_read(gpio->regmap, reg, &val); 134 if (ret) 135 return ret; 136 137 if (!!(val & mask) ^ invert) 138 return GPIO_LINE_DIRECTION_OUT; 139 else 140 return GPIO_LINE_DIRECTION_IN; 141 } 142 143 static int gpio_regmap_set_direction(struct gpio_chip *chip, 144 unsigned int offset, bool output) 145 { 146 struct gpio_regmap *gpio = gpiochip_get_data(chip); 147 unsigned int base, val, reg, mask; 148 int invert, ret; 149 150 if (gpio->reg_dir_out_base) { 151 base = gpio_regmap_addr(gpio->reg_dir_out_base); 152 invert = 0; 153 } else if (gpio->reg_dir_in_base) { 154 base = gpio_regmap_addr(gpio->reg_dir_in_base); 155 invert = 1; 156 } else { 157 return -EOPNOTSUPP; 158 } 159 160 ret = gpio->reg_mask_xlate(gpio, base, offset, ®, &mask); 161 if (ret) 162 return ret; 163 164 if (invert) 165 val = output ? 0 : mask; 166 else 167 val = output ? mask : 0; 168 169 return regmap_update_bits(gpio->regmap, reg, mask, val); 170 } 171 172 static int gpio_regmap_direction_input(struct gpio_chip *chip, 173 unsigned int offset) 174 { 175 return gpio_regmap_set_direction(chip, offset, false); 176 } 177 178 static int gpio_regmap_direction_output(struct gpio_chip *chip, 179 unsigned int offset, int value) 180 { 181 gpio_regmap_set(chip, offset, value); 182 183 return gpio_regmap_set_direction(chip, offset, true); 184 } 185 186 void *gpio_regmap_get_drvdata(struct gpio_regmap *gpio) 187 { 188 return gpio->driver_data; 189 } 190 EXPORT_SYMBOL_GPL(gpio_regmap_get_drvdata); 191 192 /** 193 * gpio_regmap_register() - Register a generic regmap GPIO controller 194 * @config: configuration for gpio_regmap 195 * 196 * Return: A pointer to the registered gpio_regmap or ERR_PTR error value. 197 */ 198 struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config) 199 { 200 struct gpio_regmap *gpio; 201 struct gpio_chip *chip; 202 int ret; 203 204 if (!config->parent) 205 return ERR_PTR(-EINVAL); 206 207 if (!config->ngpio) 208 return ERR_PTR(-EINVAL); 209 210 /* we need at least one */ 211 if (!config->reg_dat_base && !config->reg_set_base) 212 return ERR_PTR(-EINVAL); 213 214 /* if we have a direction register we need both input and output */ 215 if ((config->reg_dir_out_base || config->reg_dir_in_base) && 216 (!config->reg_dat_base || !config->reg_set_base)) 217 return ERR_PTR(-EINVAL); 218 219 /* we don't support having both registers simultaneously for now */ 220 if (config->reg_dir_out_base && config->reg_dir_in_base) 221 return ERR_PTR(-EINVAL); 222 223 gpio = kzalloc(sizeof(*gpio), GFP_KERNEL); 224 if (!gpio) 225 return ERR_PTR(-ENOMEM); 226 227 gpio->parent = config->parent; 228 gpio->driver_data = config->drvdata; 229 gpio->regmap = config->regmap; 230 gpio->ngpio_per_reg = config->ngpio_per_reg; 231 gpio->reg_stride = config->reg_stride; 232 gpio->reg_mask_xlate = config->reg_mask_xlate; 233 gpio->reg_dat_base = config->reg_dat_base; 234 gpio->reg_set_base = config->reg_set_base; 235 gpio->reg_clr_base = config->reg_clr_base; 236 gpio->reg_dir_in_base = config->reg_dir_in_base; 237 gpio->reg_dir_out_base = config->reg_dir_out_base; 238 239 /* if not set, assume there is only one register */ 240 if (!gpio->ngpio_per_reg) 241 gpio->ngpio_per_reg = config->ngpio; 242 243 /* if not set, assume they are consecutive */ 244 if (!gpio->reg_stride) 245 gpio->reg_stride = 1; 246 247 if (!gpio->reg_mask_xlate) 248 gpio->reg_mask_xlate = gpio_regmap_simple_xlate; 249 250 chip = &gpio->gpio_chip; 251 chip->parent = config->parent; 252 chip->fwnode = config->fwnode; 253 chip->base = -1; 254 chip->ngpio = config->ngpio; 255 chip->names = config->names; 256 chip->label = config->label ?: dev_name(config->parent); 257 chip->can_sleep = regmap_might_sleep(config->regmap); 258 259 chip->get = gpio_regmap_get; 260 if (gpio->reg_set_base && gpio->reg_clr_base) 261 chip->set = gpio_regmap_set_with_clear; 262 else if (gpio->reg_set_base) 263 chip->set = gpio_regmap_set; 264 265 chip->get_direction = gpio_regmap_get_direction; 266 if (gpio->reg_dir_in_base || gpio->reg_dir_out_base) { 267 chip->direction_input = gpio_regmap_direction_input; 268 chip->direction_output = gpio_regmap_direction_output; 269 } 270 271 ret = gpiochip_add_data(chip, gpio); 272 if (ret < 0) 273 goto err_free_gpio; 274 275 if (config->irq_domain) { 276 ret = gpiochip_irqchip_add_domain(chip, config->irq_domain); 277 if (ret) 278 goto err_remove_gpiochip; 279 } 280 281 return gpio; 282 283 err_remove_gpiochip: 284 gpiochip_remove(chip); 285 err_free_gpio: 286 kfree(gpio); 287 return ERR_PTR(ret); 288 } 289 EXPORT_SYMBOL_GPL(gpio_regmap_register); 290 291 /** 292 * gpio_regmap_unregister() - Unregister a generic regmap GPIO controller 293 * @gpio: gpio_regmap device to unregister 294 */ 295 void gpio_regmap_unregister(struct gpio_regmap *gpio) 296 { 297 gpiochip_remove(&gpio->gpio_chip); 298 kfree(gpio); 299 } 300 EXPORT_SYMBOL_GPL(gpio_regmap_unregister); 301 302 static void devm_gpio_regmap_unregister(void *res) 303 { 304 gpio_regmap_unregister(res); 305 } 306 307 /** 308 * devm_gpio_regmap_register() - resource managed gpio_regmap_register() 309 * @dev: device that is registering this GPIO device 310 * @config: configuration for gpio_regmap 311 * 312 * Managed gpio_regmap_register(). For generic regmap GPIO device registered by 313 * this function, gpio_regmap_unregister() is automatically called on driver 314 * detach. See gpio_regmap_register() for more information. 315 * 316 * Return: A pointer to the registered gpio_regmap or ERR_PTR error value. 317 */ 318 struct gpio_regmap *devm_gpio_regmap_register(struct device *dev, 319 const struct gpio_regmap_config *config) 320 { 321 struct gpio_regmap *gpio; 322 int ret; 323 324 gpio = gpio_regmap_register(config); 325 326 if (IS_ERR(gpio)) 327 return gpio; 328 329 ret = devm_add_action_or_reset(dev, devm_gpio_regmap_unregister, gpio); 330 if (ret) 331 return ERR_PTR(ret); 332 333 return gpio; 334 } 335 EXPORT_SYMBOL_GPL(devm_gpio_regmap_register); 336 337 MODULE_AUTHOR("Michael Walle <michael@walle.cc>"); 338 MODULE_DESCRIPTION("GPIO generic regmap driver core"); 339 MODULE_LICENSE("GPL"); 340