axp20x_ac_power.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * AXP20X and AXP22X PMICs' ACIN power supply driver
  3. *
  4. * Copyright (C) 2016 Free Electrons
  5. * Quentin Schulz <quentin.schulz@free-electrons.com>
  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/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/power_supply.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. #include <linux/iio/consumer.h>
  25. #define AXP20X_PWR_STATUS_ACIN_PRESENT BIT(7)
  26. #define AXP20X_PWR_STATUS_ACIN_AVAIL BIT(6)
  27. #define DRVNAME "axp20x-ac-power-supply"
  28. struct axp20x_ac_power {
  29. struct regmap *regmap;
  30. struct power_supply *supply;
  31. struct iio_channel *acin_v;
  32. struct iio_channel *acin_i;
  33. };
  34. static irqreturn_t axp20x_ac_power_irq(int irq, void *devid)
  35. {
  36. struct axp20x_ac_power *power = devid;
  37. power_supply_changed(power->supply);
  38. return IRQ_HANDLED;
  39. }
  40. static int axp20x_ac_power_get_property(struct power_supply *psy,
  41. enum power_supply_property psp,
  42. union power_supply_propval *val)
  43. {
  44. struct axp20x_ac_power *power = power_supply_get_drvdata(psy);
  45. int ret, reg;
  46. switch (psp) {
  47. case POWER_SUPPLY_PROP_HEALTH:
  48. ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &reg);
  49. if (ret)
  50. return ret;
  51. if (reg & AXP20X_PWR_STATUS_ACIN_PRESENT) {
  52. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  53. return 0;
  54. }
  55. val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
  56. return 0;
  57. case POWER_SUPPLY_PROP_PRESENT:
  58. ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &reg);
  59. if (ret)
  60. return ret;
  61. val->intval = !!(reg & AXP20X_PWR_STATUS_ACIN_PRESENT);
  62. return 0;
  63. case POWER_SUPPLY_PROP_ONLINE:
  64. ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &reg);
  65. if (ret)
  66. return ret;
  67. val->intval = !!(reg & AXP20X_PWR_STATUS_ACIN_AVAIL);
  68. return 0;
  69. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  70. ret = iio_read_channel_processed(power->acin_v, &val->intval);
  71. if (ret)
  72. return ret;
  73. /* IIO framework gives mV but Power Supply framework gives uV */
  74. val->intval *= 1000;
  75. return 0;
  76. case POWER_SUPPLY_PROP_CURRENT_NOW:
  77. ret = iio_read_channel_processed(power->acin_i, &val->intval);
  78. if (ret)
  79. return ret;
  80. /* IIO framework gives mA but Power Supply framework gives uA */
  81. val->intval *= 1000;
  82. return 0;
  83. default:
  84. return -EINVAL;
  85. }
  86. return -EINVAL;
  87. }
  88. static enum power_supply_property axp20x_ac_power_properties[] = {
  89. POWER_SUPPLY_PROP_HEALTH,
  90. POWER_SUPPLY_PROP_PRESENT,
  91. POWER_SUPPLY_PROP_ONLINE,
  92. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  93. POWER_SUPPLY_PROP_CURRENT_NOW,
  94. };
  95. static enum power_supply_property axp22x_ac_power_properties[] = {
  96. POWER_SUPPLY_PROP_HEALTH,
  97. POWER_SUPPLY_PROP_PRESENT,
  98. POWER_SUPPLY_PROP_ONLINE,
  99. };
  100. static const struct power_supply_desc axp20x_ac_power_desc = {
  101. .name = "axp20x-ac",
  102. .type = POWER_SUPPLY_TYPE_MAINS,
  103. .properties = axp20x_ac_power_properties,
  104. .num_properties = ARRAY_SIZE(axp20x_ac_power_properties),
  105. .get_property = axp20x_ac_power_get_property,
  106. };
  107. static const struct power_supply_desc axp22x_ac_power_desc = {
  108. .name = "axp22x-ac",
  109. .type = POWER_SUPPLY_TYPE_MAINS,
  110. .properties = axp22x_ac_power_properties,
  111. .num_properties = ARRAY_SIZE(axp22x_ac_power_properties),
  112. .get_property = axp20x_ac_power_get_property,
  113. };
  114. struct axp_data {
  115. const struct power_supply_desc *power_desc;
  116. bool acin_adc;
  117. };
  118. static const struct axp_data axp20x_data = {
  119. .power_desc = &axp20x_ac_power_desc,
  120. .acin_adc = true,
  121. };
  122. static const struct axp_data axp22x_data = {
  123. .power_desc = &axp22x_ac_power_desc,
  124. .acin_adc = false,
  125. };
  126. static int axp20x_ac_power_probe(struct platform_device *pdev)
  127. {
  128. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  129. struct power_supply_config psy_cfg = {};
  130. struct axp20x_ac_power *power;
  131. const struct axp_data *axp_data;
  132. static const char * const irq_names[] = { "ACIN_PLUGIN", "ACIN_REMOVAL",
  133. NULL };
  134. int i, irq, ret;
  135. if (!of_device_is_available(pdev->dev.of_node))
  136. return -ENODEV;
  137. if (!axp20x) {
  138. dev_err(&pdev->dev, "Parent drvdata not set\n");
  139. return -EINVAL;
  140. }
  141. power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
  142. if (!power)
  143. return -ENOMEM;
  144. axp_data = of_device_get_match_data(&pdev->dev);
  145. if (axp_data->acin_adc) {
  146. power->acin_v = devm_iio_channel_get(&pdev->dev, "acin_v");
  147. if (IS_ERR(power->acin_v)) {
  148. if (PTR_ERR(power->acin_v) == -ENODEV)
  149. return -EPROBE_DEFER;
  150. return PTR_ERR(power->acin_v);
  151. }
  152. power->acin_i = devm_iio_channel_get(&pdev->dev, "acin_i");
  153. if (IS_ERR(power->acin_i)) {
  154. if (PTR_ERR(power->acin_i) == -ENODEV)
  155. return -EPROBE_DEFER;
  156. return PTR_ERR(power->acin_i);
  157. }
  158. }
  159. power->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  160. platform_set_drvdata(pdev, power);
  161. psy_cfg.of_node = pdev->dev.of_node;
  162. psy_cfg.drv_data = power;
  163. power->supply = devm_power_supply_register(&pdev->dev,
  164. axp_data->power_desc,
  165. &psy_cfg);
  166. if (IS_ERR(power->supply))
  167. return PTR_ERR(power->supply);
  168. /* Request irqs after registering, as irqs may trigger immediately */
  169. for (i = 0; irq_names[i]; i++) {
  170. irq = platform_get_irq_byname(pdev, irq_names[i]);
  171. if (irq < 0) {
  172. dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
  173. irq_names[i], irq);
  174. continue;
  175. }
  176. irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
  177. ret = devm_request_any_context_irq(&pdev->dev, irq,
  178. axp20x_ac_power_irq, 0,
  179. DRVNAME, power);
  180. if (ret < 0)
  181. dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
  182. irq_names[i], ret);
  183. }
  184. return 0;
  185. }
  186. static const struct of_device_id axp20x_ac_power_match[] = {
  187. {
  188. .compatible = "x-powers,axp202-ac-power-supply",
  189. .data = &axp20x_data,
  190. }, {
  191. .compatible = "x-powers,axp221-ac-power-supply",
  192. .data = &axp22x_data,
  193. }, { /* sentinel */ }
  194. };
  195. MODULE_DEVICE_TABLE(of, axp20x_ac_power_match);
  196. static struct platform_driver axp20x_ac_power_driver = {
  197. .probe = axp20x_ac_power_probe,
  198. .driver = {
  199. .name = DRVNAME,
  200. .of_match_table = axp20x_ac_power_match,
  201. },
  202. };
  203. module_platform_driver(axp20x_ac_power_driver);
  204. MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
  205. MODULE_DESCRIPTION("AXP20X and AXP22X PMICs' AC power supply driver");
  206. MODULE_LICENSE("GPL");