gpio-lp873x.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
  4. * Keerthy <[email protected]>
  5. *
  6. * Based on the TPS65218 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/lp873x.h>
  13. #define BITS_PER_GPO 0x4
  14. #define LP873X_GPO_CTRL_OD 0x2
  15. struct lp873x_gpio {
  16. struct gpio_chip chip;
  17. struct lp873x *lp873;
  18. };
  19. static int lp873x_gpio_get_direction(struct gpio_chip *chip,
  20. unsigned int offset)
  21. {
  22. /* This device is output only */
  23. return GPIO_LINE_DIRECTION_OUT;
  24. }
  25. static int lp873x_gpio_direction_input(struct gpio_chip *chip,
  26. unsigned int offset)
  27. {
  28. /* This device is output only */
  29. return -EINVAL;
  30. }
  31. static int lp873x_gpio_direction_output(struct gpio_chip *chip,
  32. unsigned int offset, int value)
  33. {
  34. struct lp873x_gpio *gpio = gpiochip_get_data(chip);
  35. /* Set the initial value */
  36. return regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
  37. BIT(offset * BITS_PER_GPO),
  38. value ? BIT(offset * BITS_PER_GPO) : 0);
  39. }
  40. static int lp873x_gpio_get(struct gpio_chip *chip, unsigned int offset)
  41. {
  42. struct lp873x_gpio *gpio = gpiochip_get_data(chip);
  43. int ret, val;
  44. ret = regmap_read(gpio->lp873->regmap, LP873X_REG_GPO_CTRL, &val);
  45. if (ret < 0)
  46. return ret;
  47. return val & BIT(offset * BITS_PER_GPO);
  48. }
  49. static void lp873x_gpio_set(struct gpio_chip *chip, unsigned int offset,
  50. int value)
  51. {
  52. struct lp873x_gpio *gpio = gpiochip_get_data(chip);
  53. regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
  54. BIT(offset * BITS_PER_GPO),
  55. value ? BIT(offset * BITS_PER_GPO) : 0);
  56. }
  57. static int lp873x_gpio_request(struct gpio_chip *gc, unsigned int offset)
  58. {
  59. struct lp873x_gpio *gpio = gpiochip_get_data(gc);
  60. int ret;
  61. switch (offset) {
  62. case 0:
  63. /* No MUX Set up Needed for GPO */
  64. break;
  65. case 1:
  66. /* Setup the CLKIN_PIN_SEL MUX to GPO2 */
  67. ret = regmap_update_bits(gpio->lp873->regmap, LP873X_REG_CONFIG,
  68. LP873X_CONFIG_CLKIN_PIN_SEL, 0);
  69. if (ret)
  70. return ret;
  71. break;
  72. default:
  73. return -EINVAL;
  74. }
  75. return 0;
  76. }
  77. static int lp873x_gpio_set_config(struct gpio_chip *gc, unsigned offset,
  78. unsigned long config)
  79. {
  80. struct lp873x_gpio *gpio = gpiochip_get_data(gc);
  81. switch (pinconf_to_config_param(config)) {
  82. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  83. return regmap_update_bits(gpio->lp873->regmap,
  84. LP873X_REG_GPO_CTRL,
  85. BIT(offset * BITS_PER_GPO +
  86. LP873X_GPO_CTRL_OD),
  87. BIT(offset * BITS_PER_GPO +
  88. LP873X_GPO_CTRL_OD));
  89. case PIN_CONFIG_DRIVE_PUSH_PULL:
  90. return regmap_update_bits(gpio->lp873->regmap,
  91. LP873X_REG_GPO_CTRL,
  92. BIT(offset * BITS_PER_GPO +
  93. LP873X_GPO_CTRL_OD), 0);
  94. default:
  95. return -ENOTSUPP;
  96. }
  97. }
  98. static const struct gpio_chip template_chip = {
  99. .label = "lp873x-gpio",
  100. .owner = THIS_MODULE,
  101. .request = lp873x_gpio_request,
  102. .get_direction = lp873x_gpio_get_direction,
  103. .direction_input = lp873x_gpio_direction_input,
  104. .direction_output = lp873x_gpio_direction_output,
  105. .get = lp873x_gpio_get,
  106. .set = lp873x_gpio_set,
  107. .set_config = lp873x_gpio_set_config,
  108. .base = -1,
  109. .ngpio = 2,
  110. .can_sleep = true,
  111. };
  112. static int lp873x_gpio_probe(struct platform_device *pdev)
  113. {
  114. struct lp873x_gpio *gpio;
  115. int ret;
  116. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  117. if (!gpio)
  118. return -ENOMEM;
  119. platform_set_drvdata(pdev, gpio);
  120. gpio->lp873 = dev_get_drvdata(pdev->dev.parent);
  121. gpio->chip = template_chip;
  122. gpio->chip.parent = gpio->lp873->dev;
  123. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  124. if (ret < 0) {
  125. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. static const struct platform_device_id lp873x_gpio_id_table[] = {
  131. { "lp873x-gpio", },
  132. { /* sentinel */ }
  133. };
  134. MODULE_DEVICE_TABLE(platform, lp873x_gpio_id_table);
  135. static struct platform_driver lp873x_gpio_driver = {
  136. .driver = {
  137. .name = "lp873x-gpio",
  138. },
  139. .probe = lp873x_gpio_probe,
  140. .id_table = lp873x_gpio_id_table,
  141. };
  142. module_platform_driver(lp873x_gpio_driver);
  143. MODULE_AUTHOR("Keerthy <[email protected]>");
  144. MODULE_DESCRIPTION("LP873X GPIO driver");
  145. MODULE_LICENSE("GPL v2");