gpio-lp87565.c 4.7 KB

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