axp20x_usb_power.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * AXP20x PMIC USB power supply status driver
  3. *
  4. * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
  5. * Copyright (C) 2014 Bruno Prémont <bonbons@linux-vserver.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mfd/axp20x.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #define DRVNAME "axp20x-usb-power-supply"
  24. #define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5)
  25. #define AXP20X_PWR_STATUS_VBUS_USED BIT(4)
  26. #define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
  27. #define AXP20X_VBUS_VHOLD_uV(b) (4000000 + (((b) >> 3) & 7) * 100000)
  28. #define AXP20X_VBUS_CLIMIT_MASK 3
  29. #define AXP20X_VBUC_CLIMIT_900mA 0
  30. #define AXP20X_VBUC_CLIMIT_500mA 1
  31. #define AXP20X_VBUC_CLIMIT_100mA 2
  32. #define AXP20X_VBUC_CLIMIT_NONE 3
  33. #define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
  34. #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
  35. #define AXP20X_VBUS_MON_VBUS_VALID BIT(3)
  36. struct axp20x_usb_power {
  37. struct device_node *np;
  38. struct regmap *regmap;
  39. struct power_supply *supply;
  40. };
  41. static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
  42. {
  43. struct axp20x_usb_power *power = devid;
  44. power_supply_changed(power->supply);
  45. return IRQ_HANDLED;
  46. }
  47. static int axp20x_usb_power_get_property(struct power_supply *psy,
  48. enum power_supply_property psp, union power_supply_propval *val)
  49. {
  50. struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
  51. unsigned int input, v;
  52. int ret;
  53. switch (psp) {
  54. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  55. ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
  56. if (ret)
  57. return ret;
  58. val->intval = AXP20X_VBUS_VHOLD_uV(v);
  59. return 0;
  60. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  61. ret = axp20x_read_variable_width(power->regmap,
  62. AXP20X_VBUS_V_ADC_H, 12);
  63. if (ret < 0)
  64. return ret;
  65. val->intval = ret * 1700; /* 1 step = 1.7 mV */
  66. return 0;
  67. case POWER_SUPPLY_PROP_CURRENT_MAX:
  68. ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
  69. if (ret)
  70. return ret;
  71. switch (v & AXP20X_VBUS_CLIMIT_MASK) {
  72. case AXP20X_VBUC_CLIMIT_100mA:
  73. if (of_device_is_compatible(power->np,
  74. "x-powers,axp202-usb-power-supply")) {
  75. val->intval = 100000;
  76. } else {
  77. val->intval = -1; /* No 100mA limit */
  78. }
  79. break;
  80. case AXP20X_VBUC_CLIMIT_500mA:
  81. val->intval = 500000;
  82. break;
  83. case AXP20X_VBUC_CLIMIT_900mA:
  84. val->intval = 900000;
  85. break;
  86. case AXP20X_VBUC_CLIMIT_NONE:
  87. val->intval = -1;
  88. break;
  89. }
  90. return 0;
  91. case POWER_SUPPLY_PROP_CURRENT_NOW:
  92. ret = axp20x_read_variable_width(power->regmap,
  93. AXP20X_VBUS_I_ADC_H, 12);
  94. if (ret < 0)
  95. return ret;
  96. val->intval = ret * 375; /* 1 step = 0.375 mA */
  97. return 0;
  98. default:
  99. break;
  100. }
  101. /* All the properties below need the input-status reg value */
  102. ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
  103. if (ret)
  104. return ret;
  105. switch (psp) {
  106. case POWER_SUPPLY_PROP_HEALTH:
  107. if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
  108. val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
  109. break;
  110. }
  111. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  112. if (of_device_is_compatible(power->np,
  113. "x-powers,axp202-usb-power-supply")) {
  114. ret = regmap_read(power->regmap,
  115. AXP20X_USB_OTG_STATUS, &v);
  116. if (ret)
  117. return ret;
  118. if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
  119. val->intval =
  120. POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  121. }
  122. break;
  123. case POWER_SUPPLY_PROP_PRESENT:
  124. val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
  125. break;
  126. case POWER_SUPPLY_PROP_ONLINE:
  127. val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
  128. break;
  129. default:
  130. return -EINVAL;
  131. }
  132. return 0;
  133. }
  134. static enum power_supply_property axp20x_usb_power_properties[] = {
  135. POWER_SUPPLY_PROP_HEALTH,
  136. POWER_SUPPLY_PROP_PRESENT,
  137. POWER_SUPPLY_PROP_ONLINE,
  138. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  139. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  140. POWER_SUPPLY_PROP_CURRENT_MAX,
  141. POWER_SUPPLY_PROP_CURRENT_NOW,
  142. };
  143. static enum power_supply_property axp22x_usb_power_properties[] = {
  144. POWER_SUPPLY_PROP_HEALTH,
  145. POWER_SUPPLY_PROP_PRESENT,
  146. POWER_SUPPLY_PROP_ONLINE,
  147. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  148. POWER_SUPPLY_PROP_CURRENT_MAX,
  149. };
  150. static const struct power_supply_desc axp20x_usb_power_desc = {
  151. .name = "axp20x-usb",
  152. .type = POWER_SUPPLY_TYPE_USB,
  153. .properties = axp20x_usb_power_properties,
  154. .num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
  155. .get_property = axp20x_usb_power_get_property,
  156. };
  157. static const struct power_supply_desc axp22x_usb_power_desc = {
  158. .name = "axp20x-usb",
  159. .type = POWER_SUPPLY_TYPE_USB,
  160. .properties = axp22x_usb_power_properties,
  161. .num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
  162. .get_property = axp20x_usb_power_get_property,
  163. };
  164. static int axp20x_usb_power_probe(struct platform_device *pdev)
  165. {
  166. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  167. struct power_supply_config psy_cfg = {};
  168. struct axp20x_usb_power *power;
  169. static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN",
  170. "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL };
  171. static const char * const axp22x_irq_names[] = {
  172. "VBUS_PLUGIN", "VBUS_REMOVAL", NULL };
  173. static const char * const *irq_names;
  174. const struct power_supply_desc *usb_power_desc;
  175. int i, irq, ret;
  176. if (!of_device_is_available(pdev->dev.of_node))
  177. return -ENODEV;
  178. if (!axp20x) {
  179. dev_err(&pdev->dev, "Parent drvdata not set\n");
  180. return -EINVAL;
  181. }
  182. power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
  183. if (!power)
  184. return -ENOMEM;
  185. power->np = pdev->dev.of_node;
  186. power->regmap = axp20x->regmap;
  187. if (of_device_is_compatible(power->np,
  188. "x-powers,axp202-usb-power-supply")) {
  189. /* Enable vbus valid checking */
  190. ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON,
  191. AXP20X_VBUS_MON_VBUS_VALID,
  192. AXP20X_VBUS_MON_VBUS_VALID);
  193. if (ret)
  194. return ret;
  195. /* Enable vbus voltage and current measurement */
  196. ret = regmap_update_bits(power->regmap, AXP20X_ADC_EN1,
  197. AXP20X_ADC_EN1_VBUS_CURR | AXP20X_ADC_EN1_VBUS_VOLT,
  198. AXP20X_ADC_EN1_VBUS_CURR | AXP20X_ADC_EN1_VBUS_VOLT);
  199. if (ret)
  200. return ret;
  201. usb_power_desc = &axp20x_usb_power_desc;
  202. irq_names = axp20x_irq_names;
  203. } else if (of_device_is_compatible(power->np,
  204. "x-powers,axp221-usb-power-supply")) {
  205. usb_power_desc = &axp22x_usb_power_desc;
  206. irq_names = axp22x_irq_names;
  207. } else {
  208. dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
  209. axp20x->variant);
  210. return -EINVAL;
  211. }
  212. psy_cfg.of_node = pdev->dev.of_node;
  213. psy_cfg.drv_data = power;
  214. power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
  215. &psy_cfg);
  216. if (IS_ERR(power->supply))
  217. return PTR_ERR(power->supply);
  218. /* Request irqs after registering, as irqs may trigger immediately */
  219. for (i = 0; irq_names[i]; i++) {
  220. irq = platform_get_irq_byname(pdev, irq_names[i]);
  221. if (irq < 0) {
  222. dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
  223. irq_names[i], irq);
  224. continue;
  225. }
  226. irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
  227. ret = devm_request_any_context_irq(&pdev->dev, irq,
  228. axp20x_usb_power_irq, 0, DRVNAME, power);
  229. if (ret < 0)
  230. dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
  231. irq_names[i], ret);
  232. }
  233. return 0;
  234. }
  235. static const struct of_device_id axp20x_usb_power_match[] = {
  236. { .compatible = "x-powers,axp202-usb-power-supply" },
  237. { .compatible = "x-powers,axp221-usb-power-supply" },
  238. { }
  239. };
  240. MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
  241. static struct platform_driver axp20x_usb_power_driver = {
  242. .probe = axp20x_usb_power_probe,
  243. .driver = {
  244. .name = DRVNAME,
  245. .of_match_table = axp20x_usb_power_match,
  246. },
  247. };
  248. module_platform_driver(axp20x_usb_power_driver);
  249. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  250. MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
  251. MODULE_LICENSE("GPL");