gpio-lp87565.c 4.7 KB

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