gpio-max77650.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2018 BayLibre SAS
  4. // Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
  5. //
  6. // GPIO driver for MAXIM 77650/77651 charger/power-supply.
  7. #include <linux/gpio/driver.h>
  8. #include <linux/i2c.h>
  9. #include <linux/mfd/max77650.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #define MAX77650_GPIO_DIR_MASK BIT(0)
  14. #define MAX77650_GPIO_INVAL_MASK BIT(1)
  15. #define MAX77650_GPIO_DRV_MASK BIT(2)
  16. #define MAX77650_GPIO_OUTVAL_MASK BIT(3)
  17. #define MAX77650_GPIO_DEBOUNCE_MASK BIT(4)
  18. #define MAX77650_GPIO_DIR_OUT 0x00
  19. #define MAX77650_GPIO_DIR_IN BIT(0)
  20. #define MAX77650_GPIO_OUT_LOW 0x00
  21. #define MAX77650_GPIO_OUT_HIGH BIT(3)
  22. #define MAX77650_GPIO_DRV_OPEN_DRAIN 0x00
  23. #define MAX77650_GPIO_DRV_PUSH_PULL BIT(2)
  24. #define MAX77650_GPIO_DEBOUNCE BIT(4)
  25. #define MAX77650_GPIO_DIR_BITS(_reg) \
  26. ((_reg) & MAX77650_GPIO_DIR_MASK)
  27. #define MAX77650_GPIO_INVAL_BITS(_reg) \
  28. (((_reg) & MAX77650_GPIO_INVAL_MASK) >> 1)
  29. struct max77650_gpio_chip {
  30. struct regmap *map;
  31. struct gpio_chip gc;
  32. int irq;
  33. };
  34. static int max77650_gpio_direction_input(struct gpio_chip *gc,
  35. unsigned int offset)
  36. {
  37. struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
  38. return regmap_update_bits(chip->map,
  39. MAX77650_REG_CNFG_GPIO,
  40. MAX77650_GPIO_DIR_MASK,
  41. MAX77650_GPIO_DIR_IN);
  42. }
  43. static int max77650_gpio_direction_output(struct gpio_chip *gc,
  44. unsigned int offset, int value)
  45. {
  46. struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
  47. int mask, regval;
  48. mask = MAX77650_GPIO_DIR_MASK | MAX77650_GPIO_OUTVAL_MASK;
  49. regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW;
  50. regval |= MAX77650_GPIO_DIR_OUT;
  51. return regmap_update_bits(chip->map,
  52. MAX77650_REG_CNFG_GPIO, mask, regval);
  53. }
  54. static void max77650_gpio_set_value(struct gpio_chip *gc,
  55. unsigned int offset, int value)
  56. {
  57. struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
  58. int rv, regval;
  59. regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW;
  60. rv = regmap_update_bits(chip->map, MAX77650_REG_CNFG_GPIO,
  61. MAX77650_GPIO_OUTVAL_MASK, regval);
  62. if (rv)
  63. dev_err(gc->parent, "cannot set GPIO value: %d\n", rv);
  64. }
  65. static int max77650_gpio_get_value(struct gpio_chip *gc,
  66. unsigned int offset)
  67. {
  68. struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
  69. unsigned int val;
  70. int rv;
  71. rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val);
  72. if (rv)
  73. return rv;
  74. return MAX77650_GPIO_INVAL_BITS(val);
  75. }
  76. static int max77650_gpio_get_direction(struct gpio_chip *gc,
  77. unsigned int offset)
  78. {
  79. struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
  80. unsigned int val;
  81. int rv;
  82. rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val);
  83. if (rv)
  84. return rv;
  85. return MAX77650_GPIO_DIR_BITS(val);
  86. }
  87. static int max77650_gpio_set_config(struct gpio_chip *gc,
  88. unsigned int offset, unsigned long cfg)
  89. {
  90. struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
  91. switch (pinconf_to_config_param(cfg)) {
  92. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  93. return regmap_update_bits(chip->map,
  94. MAX77650_REG_CNFG_GPIO,
  95. MAX77650_GPIO_DRV_MASK,
  96. MAX77650_GPIO_DRV_OPEN_DRAIN);
  97. case PIN_CONFIG_DRIVE_PUSH_PULL:
  98. return regmap_update_bits(chip->map,
  99. MAX77650_REG_CNFG_GPIO,
  100. MAX77650_GPIO_DRV_MASK,
  101. MAX77650_GPIO_DRV_PUSH_PULL);
  102. case PIN_CONFIG_INPUT_DEBOUNCE:
  103. return regmap_update_bits(chip->map,
  104. MAX77650_REG_CNFG_GPIO,
  105. MAX77650_GPIO_DEBOUNCE_MASK,
  106. MAX77650_GPIO_DEBOUNCE);
  107. default:
  108. return -ENOTSUPP;
  109. }
  110. }
  111. static int max77650_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
  112. {
  113. struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
  114. return chip->irq;
  115. }
  116. static int max77650_gpio_probe(struct platform_device *pdev)
  117. {
  118. struct max77650_gpio_chip *chip;
  119. struct device *dev, *parent;
  120. struct i2c_client *i2c;
  121. dev = &pdev->dev;
  122. parent = dev->parent;
  123. i2c = to_i2c_client(parent);
  124. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  125. if (!chip)
  126. return -ENOMEM;
  127. chip->map = dev_get_regmap(parent, NULL);
  128. if (!chip->map)
  129. return -ENODEV;
  130. chip->irq = platform_get_irq_byname(pdev, "GPI");
  131. if (chip->irq < 0)
  132. return chip->irq;
  133. chip->gc.base = -1;
  134. chip->gc.ngpio = 1;
  135. chip->gc.label = i2c->name;
  136. chip->gc.parent = dev;
  137. chip->gc.owner = THIS_MODULE;
  138. chip->gc.can_sleep = true;
  139. chip->gc.direction_input = max77650_gpio_direction_input;
  140. chip->gc.direction_output = max77650_gpio_direction_output;
  141. chip->gc.set = max77650_gpio_set_value;
  142. chip->gc.get = max77650_gpio_get_value;
  143. chip->gc.get_direction = max77650_gpio_get_direction;
  144. chip->gc.set_config = max77650_gpio_set_config;
  145. chip->gc.to_irq = max77650_gpio_to_irq;
  146. return devm_gpiochip_add_data(dev, &chip->gc, chip);
  147. }
  148. static struct platform_driver max77650_gpio_driver = {
  149. .driver = {
  150. .name = "max77650-gpio",
  151. },
  152. .probe = max77650_gpio_probe,
  153. };
  154. module_platform_driver(max77650_gpio_driver);
  155. MODULE_DESCRIPTION("MAXIM 77650/77651 GPIO driver");
  156. MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
  157. MODULE_LICENSE("GPL v2");
  158. MODULE_ALIAS("platform:max77650-gpio");