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/driver.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_config(struct gpio_chip *gc, unsigned offset,
  86. unsigned long config)
  87. {
  88. struct lp873x_gpio *gpio = gpiochip_get_data(gc);
  89. switch (pinconf_to_config_param(config)) {
  90. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  91. return regmap_update_bits(gpio->lp873->regmap,
  92. LP873X_REG_GPO_CTRL,
  93. BIT(offset * BITS_PER_GPO +
  94. LP873X_GPO_CTRL_OD),
  95. BIT(offset * BITS_PER_GPO +
  96. LP873X_GPO_CTRL_OD));
  97. case PIN_CONFIG_DRIVE_PUSH_PULL:
  98. return regmap_update_bits(gpio->lp873->regmap,
  99. LP873X_REG_GPO_CTRL,
  100. BIT(offset * BITS_PER_GPO +
  101. LP873X_GPO_CTRL_OD), 0);
  102. default:
  103. return -ENOTSUPP;
  104. }
  105. }
  106. static const struct gpio_chip template_chip = {
  107. .label = "lp873x-gpio",
  108. .owner = THIS_MODULE,
  109. .request = lp873x_gpio_request,
  110. .get_direction = lp873x_gpio_get_direction,
  111. .direction_input = lp873x_gpio_direction_input,
  112. .direction_output = lp873x_gpio_direction_output,
  113. .get = lp873x_gpio_get,
  114. .set = lp873x_gpio_set,
  115. .set_config = lp873x_gpio_set_config,
  116. .base = -1,
  117. .ngpio = 2,
  118. .can_sleep = true,
  119. };
  120. static int lp873x_gpio_probe(struct platform_device *pdev)
  121. {
  122. struct lp873x_gpio *gpio;
  123. int ret;
  124. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  125. if (!gpio)
  126. return -ENOMEM;
  127. platform_set_drvdata(pdev, gpio);
  128. gpio->lp873 = dev_get_drvdata(pdev->dev.parent);
  129. gpio->chip = template_chip;
  130. gpio->chip.parent = gpio->lp873->dev;
  131. ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  132. if (ret < 0) {
  133. dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
  134. return ret;
  135. }
  136. return 0;
  137. }
  138. static const struct platform_device_id lp873x_gpio_id_table[] = {
  139. { "lp873x-gpio", },
  140. { /* sentinel */ }
  141. };
  142. MODULE_DEVICE_TABLE(platform, lp873x_gpio_id_table);
  143. static struct platform_driver lp873x_gpio_driver = {
  144. .driver = {
  145. .name = "lp873x-gpio",
  146. },
  147. .probe = lp873x_gpio_probe,
  148. .id_table = lp873x_gpio_id_table,
  149. };
  150. module_platform_driver(lp873x_gpio_driver);
  151. MODULE_AUTHOR("Keerthy <j-keerthy@ti.com>");
  152. MODULE_DESCRIPTION("LP873X GPIO driver");
  153. MODULE_LICENSE("GPL v2");