axp20x_ac_power.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AXP20X and AXP22X PMICs' ACIN power supply driver
  4. *
  5. * Copyright (C) 2016 Free Electrons
  6. * Quentin Schulz <quentin.schulz@free-electrons.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mfd/axp20x.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/iio/consumer.h>
  21. #define AXP20X_PWR_STATUS_ACIN_PRESENT BIT(7)
  22. #define AXP20X_PWR_STATUS_ACIN_AVAIL BIT(6)
  23. #define AXP813_ACIN_PATH_SEL BIT(7)
  24. #define AXP813_ACIN_PATH_SEL_TO_BIT(x) (!!(x) << 7)
  25. #define AXP813_VHOLD_MASK GENMASK(5, 3)
  26. #define AXP813_VHOLD_UV_TO_BIT(x) ((((x) / 100000) - 40) << 3)
  27. #define AXP813_VHOLD_REG_TO_UV(x) \
  28. (((((x) & AXP813_VHOLD_MASK) >> 3) + 40) * 100000)
  29. #define AXP813_CURR_LIMIT_MASK GENMASK(2, 0)
  30. #define AXP813_CURR_LIMIT_UA_TO_BIT(x) (((x) / 500000) - 3)
  31. #define AXP813_CURR_LIMIT_REG_TO_UA(x) \
  32. ((((x) & AXP813_CURR_LIMIT_MASK) + 3) * 500000)
  33. #define DRVNAME "axp20x-ac-power-supply"
  34. struct axp20x_ac_power {
  35. struct regmap *regmap;
  36. struct power_supply *supply;
  37. struct iio_channel *acin_v;
  38. struct iio_channel *acin_i;
  39. bool has_acin_path_sel;
  40. unsigned int num_irqs;
  41. unsigned int irqs[] __counted_by(num_irqs);
  42. };
  43. static irqreturn_t axp20x_ac_power_irq(int irq, void *devid)
  44. {
  45. struct axp20x_ac_power *power = devid;
  46. power_supply_changed(power->supply);
  47. return IRQ_HANDLED;
  48. }
  49. static int axp20x_ac_power_get_property(struct power_supply *psy,
  50. enum power_supply_property psp,
  51. union power_supply_propval *val)
  52. {
  53. struct axp20x_ac_power *power = power_supply_get_drvdata(psy);
  54. int ret, reg;
  55. switch (psp) {
  56. case POWER_SUPPLY_PROP_HEALTH:
  57. ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &reg);
  58. if (ret)
  59. return ret;
  60. if (reg & AXP20X_PWR_STATUS_ACIN_PRESENT) {
  61. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  62. return 0;
  63. }
  64. val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
  65. return 0;
  66. case POWER_SUPPLY_PROP_PRESENT:
  67. ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &reg);
  68. if (ret)
  69. return ret;
  70. val->intval = !!(reg & AXP20X_PWR_STATUS_ACIN_PRESENT);
  71. return 0;
  72. case POWER_SUPPLY_PROP_ONLINE:
  73. ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &reg);
  74. if (ret)
  75. return ret;
  76. val->intval = !!(reg & AXP20X_PWR_STATUS_ACIN_AVAIL);
  77. /* ACIN_PATH_SEL disables ACIN even if ACIN_AVAIL is set. */
  78. if (val->intval && power->has_acin_path_sel) {
  79. ret = regmap_read(power->regmap, AXP813_ACIN_PATH_CTRL,
  80. &reg);
  81. if (ret)
  82. return ret;
  83. val->intval = !!(reg & AXP813_ACIN_PATH_SEL);
  84. }
  85. return 0;
  86. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  87. ret = iio_read_channel_processed(power->acin_v, &val->intval);
  88. if (ret)
  89. return ret;
  90. /* IIO framework gives mV but Power Supply framework gives uV */
  91. val->intval *= 1000;
  92. return 0;
  93. case POWER_SUPPLY_PROP_CURRENT_NOW:
  94. ret = iio_read_channel_processed(power->acin_i, &val->intval);
  95. if (ret)
  96. return ret;
  97. /* IIO framework gives mA but Power Supply framework gives uA */
  98. val->intval *= 1000;
  99. return 0;
  100. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  101. ret = regmap_read(power->regmap, AXP813_ACIN_PATH_CTRL, &reg);
  102. if (ret)
  103. return ret;
  104. val->intval = AXP813_VHOLD_REG_TO_UV(reg);
  105. return 0;
  106. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  107. ret = regmap_read(power->regmap, AXP813_ACIN_PATH_CTRL, &reg);
  108. if (ret)
  109. return ret;
  110. val->intval = AXP813_CURR_LIMIT_REG_TO_UA(reg);
  111. /* AXP813 datasheet defines values 11x as 4000mA */
  112. if (val->intval > 4000000)
  113. val->intval = 4000000;
  114. return 0;
  115. default:
  116. return -EINVAL;
  117. }
  118. return -EINVAL;
  119. }
  120. static int axp813_ac_power_set_property(struct power_supply *psy,
  121. enum power_supply_property psp,
  122. const union power_supply_propval *val)
  123. {
  124. struct axp20x_ac_power *power = power_supply_get_drvdata(psy);
  125. switch (psp) {
  126. case POWER_SUPPLY_PROP_ONLINE:
  127. return regmap_update_bits(power->regmap, AXP813_ACIN_PATH_CTRL,
  128. AXP813_ACIN_PATH_SEL,
  129. AXP813_ACIN_PATH_SEL_TO_BIT(val->intval));
  130. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  131. if (val->intval < 4000000 || val->intval > 4700000)
  132. return -EINVAL;
  133. return regmap_update_bits(power->regmap, AXP813_ACIN_PATH_CTRL,
  134. AXP813_VHOLD_MASK,
  135. AXP813_VHOLD_UV_TO_BIT(val->intval));
  136. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  137. if (val->intval < 1500000 || val->intval > 4000000)
  138. return -EINVAL;
  139. return regmap_update_bits(power->regmap, AXP813_ACIN_PATH_CTRL,
  140. AXP813_CURR_LIMIT_MASK,
  141. AXP813_CURR_LIMIT_UA_TO_BIT(val->intval));
  142. default:
  143. return -EINVAL;
  144. }
  145. return -EINVAL;
  146. }
  147. static int axp813_ac_power_prop_writeable(struct power_supply *psy,
  148. enum power_supply_property psp)
  149. {
  150. return psp == POWER_SUPPLY_PROP_ONLINE ||
  151. psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
  152. psp == POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
  153. }
  154. static enum power_supply_property axp20x_ac_power_properties[] = {
  155. POWER_SUPPLY_PROP_HEALTH,
  156. POWER_SUPPLY_PROP_PRESENT,
  157. POWER_SUPPLY_PROP_ONLINE,
  158. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  159. POWER_SUPPLY_PROP_CURRENT_NOW,
  160. };
  161. static enum power_supply_property axp22x_ac_power_properties[] = {
  162. POWER_SUPPLY_PROP_HEALTH,
  163. POWER_SUPPLY_PROP_PRESENT,
  164. POWER_SUPPLY_PROP_ONLINE,
  165. };
  166. static enum power_supply_property axp813_ac_power_properties[] = {
  167. POWER_SUPPLY_PROP_HEALTH,
  168. POWER_SUPPLY_PROP_PRESENT,
  169. POWER_SUPPLY_PROP_ONLINE,
  170. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  171. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  172. };
  173. static const struct power_supply_desc axp20x_ac_power_desc = {
  174. .name = "axp20x-ac",
  175. .type = POWER_SUPPLY_TYPE_MAINS,
  176. .properties = axp20x_ac_power_properties,
  177. .num_properties = ARRAY_SIZE(axp20x_ac_power_properties),
  178. .get_property = axp20x_ac_power_get_property,
  179. };
  180. static const struct power_supply_desc axp22x_ac_power_desc = {
  181. .name = "axp22x-ac",
  182. .type = POWER_SUPPLY_TYPE_MAINS,
  183. .properties = axp22x_ac_power_properties,
  184. .num_properties = ARRAY_SIZE(axp22x_ac_power_properties),
  185. .get_property = axp20x_ac_power_get_property,
  186. };
  187. static const struct power_supply_desc axp813_ac_power_desc = {
  188. .name = "axp813-ac",
  189. .type = POWER_SUPPLY_TYPE_MAINS,
  190. .properties = axp813_ac_power_properties,
  191. .num_properties = ARRAY_SIZE(axp813_ac_power_properties),
  192. .property_is_writeable = axp813_ac_power_prop_writeable,
  193. .get_property = axp20x_ac_power_get_property,
  194. .set_property = axp813_ac_power_set_property,
  195. };
  196. static const char * const axp20x_irq_names[] = {
  197. "ACIN_PLUGIN",
  198. "ACIN_REMOVAL",
  199. };
  200. struct axp_data {
  201. const struct power_supply_desc *power_desc;
  202. const char * const *irq_names;
  203. unsigned int num_irq_names;
  204. bool acin_adc;
  205. bool acin_path_sel;
  206. };
  207. static const struct axp_data axp20x_data = {
  208. .power_desc = &axp20x_ac_power_desc,
  209. .irq_names = axp20x_irq_names,
  210. .num_irq_names = ARRAY_SIZE(axp20x_irq_names),
  211. .acin_adc = true,
  212. .acin_path_sel = false,
  213. };
  214. static const struct axp_data axp22x_data = {
  215. .power_desc = &axp22x_ac_power_desc,
  216. .irq_names = axp20x_irq_names,
  217. .num_irq_names = ARRAY_SIZE(axp20x_irq_names),
  218. .acin_adc = false,
  219. .acin_path_sel = false,
  220. };
  221. static const struct axp_data axp813_data = {
  222. .power_desc = &axp813_ac_power_desc,
  223. .irq_names = axp20x_irq_names,
  224. .num_irq_names = ARRAY_SIZE(axp20x_irq_names),
  225. .acin_adc = false,
  226. .acin_path_sel = true,
  227. };
  228. #ifdef CONFIG_PM_SLEEP
  229. static int axp20x_ac_power_suspend(struct device *dev)
  230. {
  231. struct axp20x_ac_power *power = dev_get_drvdata(dev);
  232. int i = 0;
  233. /*
  234. * Allow wake via ACIN_PLUGIN only.
  235. *
  236. * As nested threaded IRQs are not automatically disabled during
  237. * suspend, we must explicitly disable the remainder of the IRQs.
  238. */
  239. if (device_may_wakeup(&power->supply->dev))
  240. enable_irq_wake(power->irqs[i++]);
  241. while (i < power->num_irqs)
  242. disable_irq(power->irqs[i++]);
  243. return 0;
  244. }
  245. static int axp20x_ac_power_resume(struct device *dev)
  246. {
  247. struct axp20x_ac_power *power = dev_get_drvdata(dev);
  248. int i = 0;
  249. if (device_may_wakeup(&power->supply->dev))
  250. disable_irq_wake(power->irqs[i++]);
  251. while (i < power->num_irqs)
  252. enable_irq(power->irqs[i++]);
  253. return 0;
  254. }
  255. #endif
  256. static SIMPLE_DEV_PM_OPS(axp20x_ac_power_pm_ops, axp20x_ac_power_suspend,
  257. axp20x_ac_power_resume);
  258. static int axp20x_ac_power_probe(struct platform_device *pdev)
  259. {
  260. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  261. struct power_supply_config psy_cfg = {};
  262. struct axp20x_ac_power *power;
  263. const struct axp_data *axp_data;
  264. int i, irq, ret;
  265. if (!of_device_is_available(pdev->dev.of_node))
  266. return -ENODEV;
  267. if (!axp20x) {
  268. dev_err(&pdev->dev, "Parent drvdata not set\n");
  269. return -EINVAL;
  270. }
  271. axp_data = of_device_get_match_data(&pdev->dev);
  272. power = devm_kzalloc(&pdev->dev,
  273. struct_size(power, irqs, axp_data->num_irq_names),
  274. GFP_KERNEL);
  275. if (!power)
  276. return -ENOMEM;
  277. if (axp_data->acin_adc) {
  278. power->acin_v = devm_iio_channel_get(&pdev->dev, "acin_v");
  279. if (IS_ERR(power->acin_v)) {
  280. if (PTR_ERR(power->acin_v) == -ENODEV)
  281. return -EPROBE_DEFER;
  282. return PTR_ERR(power->acin_v);
  283. }
  284. power->acin_i = devm_iio_channel_get(&pdev->dev, "acin_i");
  285. if (IS_ERR(power->acin_i)) {
  286. if (PTR_ERR(power->acin_i) == -ENODEV)
  287. return -EPROBE_DEFER;
  288. return PTR_ERR(power->acin_i);
  289. }
  290. }
  291. power->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  292. power->has_acin_path_sel = axp_data->acin_path_sel;
  293. power->num_irqs = axp_data->num_irq_names;
  294. platform_set_drvdata(pdev, power);
  295. psy_cfg.of_node = pdev->dev.of_node;
  296. psy_cfg.drv_data = power;
  297. power->supply = devm_power_supply_register(&pdev->dev,
  298. axp_data->power_desc,
  299. &psy_cfg);
  300. if (IS_ERR(power->supply))
  301. return PTR_ERR(power->supply);
  302. /* Request irqs after registering, as irqs may trigger immediately */
  303. for (i = 0; i < axp_data->num_irq_names; i++) {
  304. irq = platform_get_irq_byname(pdev, axp_data->irq_names[i]);
  305. if (irq < 0)
  306. return irq;
  307. power->irqs[i] = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
  308. ret = devm_request_any_context_irq(&pdev->dev, power->irqs[i],
  309. axp20x_ac_power_irq, 0,
  310. DRVNAME, power);
  311. if (ret < 0) {
  312. dev_err(&pdev->dev, "Error requesting %s IRQ: %d\n",
  313. axp_data->irq_names[i], ret);
  314. return ret;
  315. }
  316. }
  317. return 0;
  318. }
  319. static const struct of_device_id axp20x_ac_power_match[] = {
  320. {
  321. .compatible = "x-powers,axp202-ac-power-supply",
  322. .data = &axp20x_data,
  323. }, {
  324. .compatible = "x-powers,axp221-ac-power-supply",
  325. .data = &axp22x_data,
  326. }, {
  327. .compatible = "x-powers,axp813-ac-power-supply",
  328. .data = &axp813_data,
  329. }, { /* sentinel */ }
  330. };
  331. MODULE_DEVICE_TABLE(of, axp20x_ac_power_match);
  332. static struct platform_driver axp20x_ac_power_driver = {
  333. .probe = axp20x_ac_power_probe,
  334. .driver = {
  335. .name = DRVNAME,
  336. .of_match_table = axp20x_ac_power_match,
  337. .pm = &axp20x_ac_power_pm_ops,
  338. },
  339. };
  340. module_platform_driver(axp20x_ac_power_driver);
  341. MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
  342. MODULE_DESCRIPTION("AXP20X and AXP22X PMICs' AC power supply driver");
  343. MODULE_LICENSE("GPL");