gpio-bd70528.c 6.4 KB

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