gpio-bd71828.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2018 ROHM Semiconductors
  3. #include <linux/gpio/driver.h>
  4. #include <linux/mfd/rohm-bd71828.h>
  5. #include <linux/module.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/regmap.h>
  8. #define GPIO_OUT_REG(off) (BD71828_REG_GPIO_CTRL1 + (off))
  9. #define HALL_GPIO_OFFSET 3
  10. struct bd71828_gpio {
  11. struct regmap *regmap;
  12. struct device *dev;
  13. struct gpio_chip gpio;
  14. };
  15. static void bd71828_gpio_set(struct gpio_chip *chip, unsigned int offset,
  16. int value)
  17. {
  18. int ret;
  19. struct bd71828_gpio *bdgpio = gpiochip_get_data(chip);
  20. u8 val = (value) ? BD71828_GPIO_OUT_HI : BD71828_GPIO_OUT_LO;
  21. /*
  22. * The HALL input pin can only be used as input. If this is the pin
  23. * we are dealing with - then we are done
  24. */
  25. if (offset == HALL_GPIO_OFFSET)
  26. return;
  27. ret = regmap_update_bits(bdgpio->regmap, GPIO_OUT_REG(offset),
  28. BD71828_GPIO_OUT_MASK, val);
  29. if (ret)
  30. dev_err(bdgpio->dev, "Could not set gpio to %d\n", value);
  31. }
  32. static int bd71828_gpio_get(struct gpio_chip *chip, unsigned int offset)
  33. {
  34. int ret;
  35. unsigned int val;
  36. struct bd71828_gpio *bdgpio = gpiochip_get_data(chip);
  37. if (offset == HALL_GPIO_OFFSET)
  38. ret = regmap_read(bdgpio->regmap, BD71828_REG_IO_STAT,
  39. &val);
  40. else
  41. ret = regmap_read(bdgpio->regmap, GPIO_OUT_REG(offset),
  42. &val);
  43. if (!ret)
  44. ret = (val & BD71828_GPIO_OUT_MASK);
  45. return ret;
  46. }
  47. static int bd71828_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  48. unsigned long config)
  49. {
  50. struct bd71828_gpio *bdgpio = gpiochip_get_data(chip);
  51. if (offset == HALL_GPIO_OFFSET)
  52. return -ENOTSUPP;
  53. switch (pinconf_to_config_param(config)) {
  54. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  55. return regmap_update_bits(bdgpio->regmap,
  56. GPIO_OUT_REG(offset),
  57. BD71828_GPIO_DRIVE_MASK,
  58. BD71828_GPIO_OPEN_DRAIN);
  59. case PIN_CONFIG_DRIVE_PUSH_PULL:
  60. return regmap_update_bits(bdgpio->regmap,
  61. GPIO_OUT_REG(offset),
  62. BD71828_GPIO_DRIVE_MASK,
  63. BD71828_GPIO_PUSH_PULL);
  64. default:
  65. break;
  66. }
  67. return -ENOTSUPP;
  68. }
  69. static int bd71828_get_direction(struct gpio_chip *chip, unsigned int offset)
  70. {
  71. /*
  72. * Pin usage is selected by OTP data. We can't read it runtime. Hence
  73. * we trust that if the pin is not excluded by "gpio-reserved-ranges"
  74. * the OTP configuration is set to OUT. (Other pins but HALL input pin
  75. * on BD71828 can't really be used for general purpose input - input
  76. * states are used for specific cases like regulator control or
  77. * PMIC_ON_REQ.
  78. */
  79. if (offset == HALL_GPIO_OFFSET)
  80. return GPIO_LINE_DIRECTION_IN;
  81. return GPIO_LINE_DIRECTION_OUT;
  82. }
  83. static int bd71828_probe(struct platform_device *pdev)
  84. {
  85. struct device *dev = &pdev->dev;
  86. struct bd71828_gpio *bdgpio;
  87. bdgpio = devm_kzalloc(dev, sizeof(*bdgpio), GFP_KERNEL);
  88. if (!bdgpio)
  89. return -ENOMEM;
  90. bdgpio->dev = dev;
  91. bdgpio->gpio.parent = dev->parent;
  92. bdgpio->gpio.label = "bd71828-gpio";
  93. bdgpio->gpio.owner = THIS_MODULE;
  94. bdgpio->gpio.get_direction = bd71828_get_direction;
  95. bdgpio->gpio.set_config = bd71828_gpio_set_config;
  96. bdgpio->gpio.can_sleep = true;
  97. bdgpio->gpio.get = bd71828_gpio_get;
  98. bdgpio->gpio.set = bd71828_gpio_set;
  99. bdgpio->gpio.base = -1;
  100. /*
  101. * See if we need some implementation to mark some PINs as
  102. * not controllable based on DT info or if core can handle
  103. * "gpio-reserved-ranges" and exclude them from control
  104. */
  105. bdgpio->gpio.ngpio = 4;
  106. bdgpio->regmap = dev_get_regmap(dev->parent, NULL);
  107. if (!bdgpio->regmap)
  108. return -ENODEV;
  109. return devm_gpiochip_add_data(dev, &bdgpio->gpio, bdgpio);
  110. }
  111. static struct platform_driver bd71828_gpio = {
  112. .driver = {
  113. .name = "bd71828-gpio"
  114. },
  115. .probe = bd71828_probe,
  116. };
  117. module_platform_driver(bd71828_gpio);
  118. MODULE_AUTHOR("Matti Vaittinen <[email protected]>");
  119. MODULE_DESCRIPTION("BD71828 voltage regulator driver");
  120. MODULE_LICENSE("GPL");
  121. MODULE_ALIAS("platform:bd71828-gpio");