axp20x_ac_power.c 11 KB

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