pinctrl-da9062.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Dialog DA9062 pinctrl and GPIO driver.
  4. * Based on DA9055 GPIO driver.
  5. *
  6. * TODO:
  7. * - add pinmux and pinctrl support (gpio alternate mode)
  8. *
  9. * Documents:
  10. * [1] https://www.dialog-semiconductor.com/sites/default/files/da9062_datasheet_3v6.pdf
  11. *
  12. * Copyright (C) 2019 Pengutronix, Marco Felsch <kernel@pengutronix.de>
  13. */
  14. #include <linux/bits.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/property.h>
  18. #include <linux/regmap.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/gpio/driver.h>
  21. #include <linux/mfd/da9062/core.h>
  22. #include <linux/mfd/da9062/registers.h>
  23. #define DA9062_TYPE(offset) (4 * (offset % 2))
  24. #define DA9062_PIN_SHIFT(offset) (4 * (offset % 2))
  25. #define DA9062_PIN_ALTERNATE 0x00 /* gpio alternate mode */
  26. #define DA9062_PIN_GPI 0x01 /* gpio in */
  27. #define DA9062_PIN_GPO_OD 0x02 /* gpio out open-drain */
  28. #define DA9062_PIN_GPO_PP 0x03 /* gpio out push-pull */
  29. #define DA9062_GPIO_NUM 5
  30. struct da9062_pctl {
  31. struct da9062 *da9062;
  32. struct gpio_chip gc;
  33. unsigned int pin_config[DA9062_GPIO_NUM];
  34. };
  35. static int da9062_pctl_get_pin_mode(struct da9062_pctl *pctl,
  36. unsigned int offset)
  37. {
  38. struct regmap *regmap = pctl->da9062->regmap;
  39. int ret, val;
  40. ret = regmap_read(regmap, DA9062AA_GPIO_0_1 + (offset >> 1), &val);
  41. if (ret < 0)
  42. return ret;
  43. val >>= DA9062_PIN_SHIFT(offset);
  44. val &= DA9062AA_GPIO0_PIN_MASK;
  45. return val;
  46. }
  47. static int da9062_pctl_set_pin_mode(struct da9062_pctl *pctl,
  48. unsigned int offset, unsigned int mode_req)
  49. {
  50. struct regmap *regmap = pctl->da9062->regmap;
  51. unsigned int mode = mode_req;
  52. unsigned int mask;
  53. int ret;
  54. mode &= DA9062AA_GPIO0_PIN_MASK;
  55. mode <<= DA9062_PIN_SHIFT(offset);
  56. mask = DA9062AA_GPIO0_PIN_MASK << DA9062_PIN_SHIFT(offset);
  57. ret = regmap_update_bits(regmap, DA9062AA_GPIO_0_1 + (offset >> 1),
  58. mask, mode);
  59. if (!ret)
  60. pctl->pin_config[offset] = mode_req;
  61. return ret;
  62. }
  63. static int da9062_gpio_get(struct gpio_chip *gc, unsigned int offset)
  64. {
  65. struct da9062_pctl *pctl = gpiochip_get_data(gc);
  66. struct regmap *regmap = pctl->da9062->regmap;
  67. int gpio_mode, val;
  68. int ret;
  69. gpio_mode = da9062_pctl_get_pin_mode(pctl, offset);
  70. if (gpio_mode < 0)
  71. return gpio_mode;
  72. switch (gpio_mode) {
  73. case DA9062_PIN_ALTERNATE:
  74. return -ENOTSUPP;
  75. case DA9062_PIN_GPI:
  76. ret = regmap_read(regmap, DA9062AA_STATUS_B, &val);
  77. if (ret < 0)
  78. return ret;
  79. break;
  80. case DA9062_PIN_GPO_OD:
  81. case DA9062_PIN_GPO_PP:
  82. ret = regmap_read(regmap, DA9062AA_GPIO_MODE0_4, &val);
  83. if (ret < 0)
  84. return ret;
  85. }
  86. return !!(val & BIT(offset));
  87. }
  88. static int da9062_gpio_set(struct gpio_chip *gc, unsigned int offset,
  89. int value)
  90. {
  91. struct da9062_pctl *pctl = gpiochip_get_data(gc);
  92. struct regmap *regmap = pctl->da9062->regmap;
  93. return regmap_update_bits(regmap, DA9062AA_GPIO_MODE0_4, BIT(offset),
  94. value << offset);
  95. }
  96. static int da9062_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  97. {
  98. struct da9062_pctl *pctl = gpiochip_get_data(gc);
  99. int gpio_mode;
  100. gpio_mode = da9062_pctl_get_pin_mode(pctl, offset);
  101. if (gpio_mode < 0)
  102. return gpio_mode;
  103. switch (gpio_mode) {
  104. case DA9062_PIN_ALTERNATE:
  105. return -ENOTSUPP;
  106. case DA9062_PIN_GPI:
  107. return GPIO_LINE_DIRECTION_IN;
  108. case DA9062_PIN_GPO_OD:
  109. case DA9062_PIN_GPO_PP:
  110. return GPIO_LINE_DIRECTION_OUT;
  111. }
  112. return -EINVAL;
  113. }
  114. static int da9062_gpio_direction_input(struct gpio_chip *gc,
  115. unsigned int offset)
  116. {
  117. struct da9062_pctl *pctl = gpiochip_get_data(gc);
  118. struct regmap *regmap = pctl->da9062->regmap;
  119. struct gpio_desc *desc = gpio_device_get_desc(gc->gpiodev, offset);
  120. unsigned int gpi_type;
  121. int ret;
  122. ret = da9062_pctl_set_pin_mode(pctl, offset, DA9062_PIN_GPI);
  123. if (ret)
  124. return ret;
  125. /*
  126. * If the gpio is active low we should set it in hw too. No worries
  127. * about gpio_get() because we read and return the gpio-level. So the
  128. * gpiolib active_low handling is still correct.
  129. *
  130. * 0 - active low, 1 - active high
  131. */
  132. gpi_type = !gpiod_is_active_low(desc);
  133. return regmap_update_bits(regmap, DA9062AA_GPIO_0_1 + (offset >> 1),
  134. DA9062AA_GPIO0_TYPE_MASK << DA9062_TYPE(offset),
  135. gpi_type << DA9062_TYPE(offset));
  136. }
  137. static int da9062_gpio_direction_output(struct gpio_chip *gc,
  138. unsigned int offset, int value)
  139. {
  140. struct da9062_pctl *pctl = gpiochip_get_data(gc);
  141. unsigned int pin_config = pctl->pin_config[offset];
  142. int ret;
  143. ret = da9062_pctl_set_pin_mode(pctl, offset, pin_config);
  144. if (ret)
  145. return ret;
  146. return da9062_gpio_set(gc, offset, value);
  147. }
  148. static int da9062_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
  149. unsigned long config)
  150. {
  151. struct da9062_pctl *pctl = gpiochip_get_data(gc);
  152. struct regmap *regmap = pctl->da9062->regmap;
  153. int gpio_mode;
  154. /*
  155. * We need to meet the following restrictions [1, Figure 18]:
  156. * - PIN_CONFIG_BIAS_PULL_DOWN -> only allowed if the pin is used as
  157. * gpio input
  158. * - PIN_CONFIG_BIAS_PULL_UP -> only allowed if the pin is used as
  159. * gpio output open-drain.
  160. */
  161. switch (pinconf_to_config_param(config)) {
  162. case PIN_CONFIG_BIAS_DISABLE:
  163. return regmap_update_bits(regmap, DA9062AA_CONFIG_K,
  164. BIT(offset), 0);
  165. case PIN_CONFIG_BIAS_PULL_DOWN:
  166. gpio_mode = da9062_pctl_get_pin_mode(pctl, offset);
  167. if (gpio_mode < 0)
  168. return -EINVAL;
  169. else if (gpio_mode != DA9062_PIN_GPI)
  170. return -ENOTSUPP;
  171. return regmap_update_bits(regmap, DA9062AA_CONFIG_K,
  172. BIT(offset), BIT(offset));
  173. case PIN_CONFIG_BIAS_PULL_UP:
  174. gpio_mode = da9062_pctl_get_pin_mode(pctl, offset);
  175. if (gpio_mode < 0)
  176. return -EINVAL;
  177. else if (gpio_mode != DA9062_PIN_GPO_OD)
  178. return -ENOTSUPP;
  179. return regmap_update_bits(regmap, DA9062AA_CONFIG_K,
  180. BIT(offset), BIT(offset));
  181. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  182. return da9062_pctl_set_pin_mode(pctl, offset,
  183. DA9062_PIN_GPO_OD);
  184. case PIN_CONFIG_DRIVE_PUSH_PULL:
  185. return da9062_pctl_set_pin_mode(pctl, offset,
  186. DA9062_PIN_GPO_PP);
  187. default:
  188. return -ENOTSUPP;
  189. }
  190. }
  191. static int da9062_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
  192. {
  193. struct da9062_pctl *pctl = gpiochip_get_data(gc);
  194. struct da9062 *da9062 = pctl->da9062;
  195. return regmap_irq_get_virq(da9062->regmap_irq,
  196. DA9062_IRQ_GPI0 + offset);
  197. }
  198. static const struct gpio_chip reference_gc = {
  199. .owner = THIS_MODULE,
  200. .get = da9062_gpio_get,
  201. .set = da9062_gpio_set,
  202. .get_direction = da9062_gpio_get_direction,
  203. .direction_input = da9062_gpio_direction_input,
  204. .direction_output = da9062_gpio_direction_output,
  205. .set_config = da9062_gpio_set_config,
  206. .to_irq = da9062_gpio_to_irq,
  207. .can_sleep = true,
  208. .ngpio = DA9062_GPIO_NUM,
  209. .base = -1,
  210. };
  211. static int da9062_pctl_probe(struct platform_device *pdev)
  212. {
  213. struct device *parent = pdev->dev.parent;
  214. struct da9062_pctl *pctl;
  215. int i;
  216. device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
  217. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  218. if (!pctl)
  219. return -ENOMEM;
  220. pctl->da9062 = dev_get_drvdata(parent);
  221. if (!pctl->da9062)
  222. return -EINVAL;
  223. if (!device_property_present(parent, "gpio-controller"))
  224. return 0;
  225. for (i = 0; i < ARRAY_SIZE(pctl->pin_config); i++)
  226. pctl->pin_config[i] = DA9062_PIN_GPO_PP;
  227. /*
  228. * Currently the driver handles only the GPIO support. The
  229. * pinctrl/pinmux support can be added later if needed.
  230. */
  231. pctl->gc = reference_gc;
  232. pctl->gc.label = dev_name(&pdev->dev);
  233. pctl->gc.parent = &pdev->dev;
  234. platform_set_drvdata(pdev, pctl);
  235. return devm_gpiochip_add_data(&pdev->dev, &pctl->gc, pctl);
  236. }
  237. static const struct of_device_id da9062_compatible_reg_id_table[] = {
  238. { .compatible = "dlg,da9062-gpio" },
  239. { }
  240. };
  241. MODULE_DEVICE_TABLE(of, da9062_compatible_reg_id_table);
  242. static struct platform_driver da9062_pctl_driver = {
  243. .probe = da9062_pctl_probe,
  244. .driver = {
  245. .name = "da9062-gpio",
  246. .of_match_table = da9062_compatible_reg_id_table,
  247. },
  248. };
  249. module_platform_driver(da9062_pctl_driver);
  250. MODULE_AUTHOR("Marco Felsch <kernel@pengutronix.de>");
  251. MODULE_DESCRIPTION("DA9062 PMIC pinctrl and GPIO Driver");
  252. MODULE_LICENSE("GPL v2");
  253. MODULE_ALIAS("platform:da9062-gpio");