gpio-lp873x.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
  3. * Keerthy <j-keerthy@ti.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  10. * kind, whether expressed or implied; without even the implied warranty
  11. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License version 2 for more details.
  13. *
  14. * Based on the TPS65218 driver
  15. */
  16. #include <linux/gpio.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/mfd/lp873x.h>
  21. #define BITS_PER_GPO 0x4
  22. #define LP873X_GPO_CTRL_OD 0x2
  23. struct lp873x_gpio {
  24. struct gpio_chip chip;
  25. struct lp873x *lp873;
  26. };
  27. static int lp873x_gpio_get_direction(struct gpio_chip *chip,
  28. unsigned int offset)
  29. {
  30. /* This device is output only */
  31. return 0;
  32. }
  33. static int lp873x_gpio_direction_input(struct gpio_chip *chip,
  34. unsigned int offset)
  35. {
  36. /* This device is output only */
  37. return -EINVAL;
  38. }
  39. static int lp873x_gpio_direction_output(struct gpio_chip *chip,
  40. unsigned int offset, int value)
  41. {
  42. struct lp873x_gpio *gpio = gpiochip_get_data(chip);
  43. /* Set the initial value */
  44. return regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
  45. BIT(offset * BITS_PER_GPO),
  46. value ? BIT(offset * BITS_PER_GPO) : 0);
  47. }
  48. static int lp873x_gpio_get(struct gpio_chip *chip, unsigned int offset)
  49. {
  50. struct lp873x_gpio *gpio = gpiochip_get_data(chip);
  51. int ret, val;
  52. ret = regmap_read(gpio->lp873->regmap, LP873X_REG_GPO_CTRL, &val);
  53. if (ret < 0)
  54. return ret;
  55. return val & BIT(offset * BITS_PER_GPO);
  56. }
  57. static void lp873x_gpio_set(struct gpio_chip *chip, unsigned int offset,
  58. int value)
  59. {
  60. struct lp873x_gpio *gpio = gpiochip_get_data(chip);
  61. regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
  62. BIT(offset * BITS_PER_GPO),
  63. value ? BIT(offset * BITS_PER_GPO) : 0);
  64. }
  65. static int lp873x_gpio_request(struct gpio_chip *gc, unsigned int offset)
  66. {
  67. struct lp873x_gpio *gpio = gpiochip_get_data(gc);
  68. int ret;
  69. switch (offset) {
  70. case 0:
  71. /* No MUX Set up Needed for GPO */
  72. break;
  73. case 1:
  74. /* Setup the CLKIN_PIN_SEL MUX to GPO2 */
  75. ret = regmap_update_bits(gpio->lp873->regmap, LP873X_REG_CONFIG,
  76. LP873X_CONFIG_CLKIN_PIN_SEL, 0);
  77. if (ret)
  78. return ret;
  79. break;
  80. default:
  81. return -EINVAL;
  82. }
  83. return 0;
  84. }
  85. static int lp873x_gpio_set_single_ended(struct gpio_chip *gc,
  86. unsigned int offset,
  87. enum single_ended_mode mode)
  88. {
  89. struct lp873x_gpio *gpio = gpiochip_get_data(gc);
  90. switch (mode) {
  91. case LINE_MODE_OPEN_DRAIN:
  92. return regmap_update_bits(gpio->lp873->regmap,
  93. LP873X_REG_GPO_CTRL,
  94. BIT(offset * BITS_PER_GPO +
  95. LP873X_GPO_CTRL_OD),
  96. BIT(offset * BITS_PER_GPO +
  97. LP873X_GPO_CTRL_OD));
  98. case LINE_MODE_PUSH_PULL:
  99. return regmap_update_bits(gpio->lp873->regmap,
  100. LP873X_REG_GPO_CTRL,
  101. BIT(offset * BITS_PER_GPO +
  102. LP873X_GPO_CTRL_OD), 0);
  103. default:
  104. return -ENOTSUPP;
  105. }
  106. }
  107. static const struct gpio_chip template_chip = {
  108. .label = "lp873x-gpio",
  109. .owner = THIS_MODULE,
  110. .request = lp873x_gpio_request,
  111. .get_direction = lp873x_gpio_get_direction,
  112. .direction_input = lp873x_gpio_direction_input,
  113. .direction_output = lp873x_gpio_direction_output,
  114. .get = lp873x_gpio_get,
  115. .set = lp873x_gpio_set,
  116. .set_single_ended = lp873x_gpio_set_single_ended,
  117. .base = -1,
  118. .ngpio = 2,
  119. .can_sleep = true,
  120. };
  121. static int lp873x_gpio_probe(struct platform_device *pdev)
  122. {
  123. struct lp873x_gpio *gpio;
  124. int ret;
  125. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  126. if (!gpio)
  127. return -ENOMEM;
  128. platform_set_drvdata(pdev, gpio);
  129. gpio->lp873 = dev_get_drvdata(pdev->dev.parent);
  130. gpio->chip = template_chip;
  131. gpio->chip.parent = gpio->lp873->dev;
  132. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  133. if (ret < 0) {
  134. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  135. return ret;
  136. }
  137. return 0;
  138. }
  139. static const struct platform_device_id lp873x_gpio_id_table[] = {
  140. { "lp873x-gpio", },
  141. { /* sentinel */ }
  142. };
  143. MODULE_DEVICE_TABLE(platform, lp873x_gpio_id_table);
  144. static struct platform_driver lp873x_gpio_driver = {
  145. .driver = {
  146. .name = "lp873x-gpio",
  147. },
  148. .probe = lp873x_gpio_probe,
  149. .id_table = lp873x_gpio_id_table,
  150. };
  151. module_platform_driver(lp873x_gpio_driver);
  152. MODULE_AUTHOR("Keerthy <j-keerthy@ti.com>");
  153. MODULE_DESCRIPTION("LP873X GPIO driver");
  154. MODULE_LICENSE("GPL v2");