gpio-regmap.c 8.8 KB

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