gpio-lp87565.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
  4. * Keerthy <[email protected]>
  5. *
  6. * Based on the LP873X driver
  7. */
  8. #include <linux/gpio/driver.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/regmap.h>
  12. #include <linux/mfd/lp87565.h>
  13. struct lp87565_gpio {
  14. struct gpio_chip chip;
  15. struct regmap *map;
  16. };
  17. static int lp87565_gpio_get(struct gpio_chip *chip, unsigned int offset)
  18. {
  19. struct lp87565_gpio *gpio = gpiochip_get_data(chip);
  20. int ret, val;
  21. ret = regmap_read(gpio->map, LP87565_REG_GPIO_IN, &val);
  22. if (ret < 0)
  23. return ret;
  24. return !!(val & BIT(offset));
  25. }
  26. static void lp87565_gpio_set(struct gpio_chip *chip, unsigned int offset,
  27. int value)
  28. {
  29. struct lp87565_gpio *gpio = gpiochip_get_data(chip);
  30. regmap_update_bits(gpio->map, LP87565_REG_GPIO_OUT,
  31. BIT(offset), value ? BIT(offset) : 0);
  32. }
  33. static int lp87565_gpio_get_direction(struct gpio_chip *chip,
  34. unsigned int offset)
  35. {
  36. struct lp87565_gpio *gpio = gpiochip_get_data(chip);
  37. int ret, val;
  38. ret = regmap_read(gpio->map, LP87565_REG_GPIO_CONFIG, &val);
  39. if (ret < 0)
  40. return ret;
  41. if (val & BIT(offset))
  42. return GPIO_LINE_DIRECTION_OUT;
  43. return GPIO_LINE_DIRECTION_IN;
  44. }
  45. static int lp87565_gpio_direction_input(struct gpio_chip *chip,
  46. unsigned int offset)
  47. {
  48. struct lp87565_gpio *gpio = gpiochip_get_data(chip);
  49. return regmap_update_bits(gpio->map,
  50. LP87565_REG_GPIO_CONFIG,
  51. BIT(offset), 0);
  52. }
  53. static int lp87565_gpio_direction_output(struct gpio_chip *chip,
  54. unsigned int offset, int value)
  55. {
  56. struct lp87565_gpio *gpio = gpiochip_get_data(chip);
  57. lp87565_gpio_set(chip, offset, value);
  58. return regmap_update_bits(gpio->map,
  59. LP87565_REG_GPIO_CONFIG,
  60. BIT(offset), BIT(offset));
  61. }
  62. static int lp87565_gpio_request(struct gpio_chip *gc, unsigned int offset)
  63. {
  64. struct lp87565_gpio *gpio = gpiochip_get_data(gc);
  65. int ret;
  66. switch (offset) {
  67. case 0:
  68. case 1:
  69. case 2:
  70. /*
  71. * MUX can program the pin to be in EN1/2/3 pin mode
  72. * Or GPIO1/2/3 mode.
  73. * Setup the GPIO*_SEL MUX to GPIO mode
  74. */
  75. ret = regmap_update_bits(gpio->map,
  76. LP87565_REG_PIN_FUNCTION,
  77. BIT(offset), BIT(offset));
  78. if (ret)
  79. return ret;
  80. break;
  81. default:
  82. return -EINVAL;
  83. }
  84. return 0;
  85. }
  86. static int lp87565_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
  87. unsigned long config)
  88. {
  89. struct lp87565_gpio *gpio = gpiochip_get_data(gc);
  90. switch (pinconf_to_config_param(config)) {
  91. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  92. return regmap_update_bits(gpio->map,
  93. LP87565_REG_GPIO_CONFIG,
  94. BIT(offset +
  95. __ffs(LP87565_GPIO1_OD)),
  96. BIT(offset +
  97. __ffs(LP87565_GPIO1_OD)));
  98. case PIN_CONFIG_DRIVE_PUSH_PULL:
  99. return regmap_update_bits(gpio->map,
  100. LP87565_REG_GPIO_CONFIG,
  101. BIT(offset +
  102. __ffs(LP87565_GPIO1_OD)), 0);
  103. default:
  104. return -ENOTSUPP;
  105. }
  106. }
  107. static const struct gpio_chip template_chip = {
  108. .label = "lp87565-gpio",
  109. .owner = THIS_MODULE,
  110. .request = lp87565_gpio_request,
  111. .get_direction = lp87565_gpio_get_direction,
  112. .direction_input = lp87565_gpio_direction_input,
  113. .direction_output = lp87565_gpio_direction_output,
  114. .get = lp87565_gpio_get,
  115. .set = lp87565_gpio_set,
  116. .set_config = lp87565_gpio_set_config,
  117. .base = -1,
  118. .ngpio = 3,
  119. .can_sleep = true,
  120. };
  121. static int lp87565_gpio_probe(struct platform_device *pdev)
  122. {
  123. struct lp87565_gpio *gpio;
  124. struct lp87565 *lp87565;
  125. int ret;
  126. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  127. if (!gpio)
  128. return -ENOMEM;
  129. lp87565 = dev_get_drvdata(pdev->dev.parent);
  130. gpio->chip = template_chip;
  131. gpio->chip.parent = lp87565->dev;
  132. gpio->map = lp87565->regmap;
  133. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  134. if (ret < 0) {
  135. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  136. return ret;
  137. }
  138. return 0;
  139. }
  140. static const struct platform_device_id lp87565_gpio_id_table[] = {
  141. { "lp87565-q1-gpio", },
  142. { /* sentinel */ }
  143. };
  144. MODULE_DEVICE_TABLE(platform, lp87565_gpio_id_table);
  145. static struct platform_driver lp87565_gpio_driver = {
  146. .driver = {
  147. .name = "lp87565-gpio",
  148. },
  149. .probe = lp87565_gpio_probe,
  150. .id_table = lp87565_gpio_id_table,
  151. };
  152. module_platform_driver(lp87565_gpio_driver);
  153. MODULE_AUTHOR("Keerthy <[email protected]>");
  154. MODULE_DESCRIPTION("LP87565 GPIO driver");
  155. MODULE_LICENSE("GPL v2");