axp20x_usb_power.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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/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 DRVNAME "axp20x-usb-power-supply"
  26. #define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5)
  27. #define AXP20X_PWR_STATUS_VBUS_USED BIT(4)
  28. #define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
  29. #define AXP20X_VBUS_VHOLD_uV(b) (4000000 + (((b) >> 3) & 7) * 100000)
  30. #define AXP20X_VBUS_VHOLD_MASK GENMASK(5, 3)
  31. #define AXP20X_VBUS_VHOLD_OFFSET 3
  32. #define AXP20X_VBUS_CLIMIT_MASK 3
  33. #define AXP20X_VBUC_CLIMIT_900mA 0
  34. #define AXP20X_VBUC_CLIMIT_500mA 1
  35. #define AXP20X_VBUC_CLIMIT_100mA 2
  36. #define AXP20X_VBUC_CLIMIT_NONE 3
  37. #define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
  38. #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
  39. #define AXP20X_VBUS_MON_VBUS_VALID BIT(3)
  40. struct axp20x_usb_power {
  41. struct device_node *np;
  42. struct regmap *regmap;
  43. struct power_supply *supply;
  44. enum axp20x_variants axp20x_id;
  45. struct iio_channel *vbus_v;
  46. struct iio_channel *vbus_i;
  47. };
  48. static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
  49. {
  50. struct axp20x_usb_power *power = devid;
  51. power_supply_changed(power->supply);
  52. return IRQ_HANDLED;
  53. }
  54. static int axp20x_usb_power_get_property(struct power_supply *psy,
  55. enum power_supply_property psp, union power_supply_propval *val)
  56. {
  57. struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
  58. unsigned int input, v;
  59. int ret;
  60. switch (psp) {
  61. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  62. ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
  63. if (ret)
  64. return ret;
  65. val->intval = AXP20X_VBUS_VHOLD_uV(v);
  66. return 0;
  67. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  68. if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
  69. ret = iio_read_channel_processed(power->vbus_v,
  70. &val->intval);
  71. if (ret)
  72. return ret;
  73. /*
  74. * IIO framework gives mV but Power Supply framework
  75. * gives uV.
  76. */
  77. val->intval *= 1000;
  78. return 0;
  79. }
  80. ret = axp20x_read_variable_width(power->regmap,
  81. AXP20X_VBUS_V_ADC_H, 12);
  82. if (ret < 0)
  83. return ret;
  84. val->intval = ret * 1700; /* 1 step = 1.7 mV */
  85. return 0;
  86. case POWER_SUPPLY_PROP_CURRENT_MAX:
  87. ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
  88. if (ret)
  89. return ret;
  90. switch (v & AXP20X_VBUS_CLIMIT_MASK) {
  91. case AXP20X_VBUC_CLIMIT_100mA:
  92. if (power->axp20x_id == AXP221_ID)
  93. val->intval = -1; /* No 100mA limit */
  94. else
  95. val->intval = 100000;
  96. break;
  97. case AXP20X_VBUC_CLIMIT_500mA:
  98. val->intval = 500000;
  99. break;
  100. case AXP20X_VBUC_CLIMIT_900mA:
  101. val->intval = 900000;
  102. break;
  103. case AXP20X_VBUC_CLIMIT_NONE:
  104. val->intval = -1;
  105. break;
  106. }
  107. return 0;
  108. case POWER_SUPPLY_PROP_CURRENT_NOW:
  109. if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
  110. ret = iio_read_channel_processed(power->vbus_i,
  111. &val->intval);
  112. if (ret)
  113. return ret;
  114. /*
  115. * IIO framework gives mA but Power Supply framework
  116. * gives uA.
  117. */
  118. val->intval *= 1000;
  119. return 0;
  120. }
  121. ret = axp20x_read_variable_width(power->regmap,
  122. AXP20X_VBUS_I_ADC_H, 12);
  123. if (ret < 0)
  124. return ret;
  125. val->intval = ret * 375; /* 1 step = 0.375 mA */
  126. return 0;
  127. default:
  128. break;
  129. }
  130. /* All the properties below need the input-status reg value */
  131. ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
  132. if (ret)
  133. return ret;
  134. switch (psp) {
  135. case POWER_SUPPLY_PROP_HEALTH:
  136. if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
  137. val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
  138. break;
  139. }
  140. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  141. if (power->axp20x_id == AXP202_ID) {
  142. ret = regmap_read(power->regmap,
  143. AXP20X_USB_OTG_STATUS, &v);
  144. if (ret)
  145. return ret;
  146. if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
  147. val->intval =
  148. POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  149. }
  150. break;
  151. case POWER_SUPPLY_PROP_PRESENT:
  152. val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
  153. break;
  154. case POWER_SUPPLY_PROP_ONLINE:
  155. val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
  156. break;
  157. default:
  158. return -EINVAL;
  159. }
  160. return 0;
  161. }
  162. static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
  163. int intval)
  164. {
  165. int val;
  166. switch (intval) {
  167. case 4000000:
  168. case 4100000:
  169. case 4200000:
  170. case 4300000:
  171. case 4400000:
  172. case 4500000:
  173. case 4600000:
  174. case 4700000:
  175. val = (intval - 4000000) / 100000;
  176. return regmap_update_bits(power->regmap,
  177. AXP20X_VBUS_IPSOUT_MGMT,
  178. AXP20X_VBUS_VHOLD_MASK,
  179. val << AXP20X_VBUS_VHOLD_OFFSET);
  180. default:
  181. return -EINVAL;
  182. }
  183. return -EINVAL;
  184. }
  185. static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
  186. int intval)
  187. {
  188. int val;
  189. switch (intval) {
  190. case 100000:
  191. if (power->axp20x_id == AXP221_ID)
  192. return -EINVAL;
  193. case 500000:
  194. case 900000:
  195. val = (900000 - intval) / 400000;
  196. return regmap_update_bits(power->regmap,
  197. AXP20X_VBUS_IPSOUT_MGMT,
  198. AXP20X_VBUS_CLIMIT_MASK, val);
  199. default:
  200. return -EINVAL;
  201. }
  202. return -EINVAL;
  203. }
  204. static int axp20x_usb_power_set_property(struct power_supply *psy,
  205. enum power_supply_property psp,
  206. const union power_supply_propval *val)
  207. {
  208. struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
  209. switch (psp) {
  210. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  211. return axp20x_usb_power_set_voltage_min(power, val->intval);
  212. case POWER_SUPPLY_PROP_CURRENT_MAX:
  213. return axp20x_usb_power_set_current_max(power, val->intval);
  214. default:
  215. return -EINVAL;
  216. }
  217. return -EINVAL;
  218. }
  219. static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
  220. enum power_supply_property psp)
  221. {
  222. return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
  223. psp == POWER_SUPPLY_PROP_CURRENT_MAX;
  224. }
  225. static enum power_supply_property axp20x_usb_power_properties[] = {
  226. POWER_SUPPLY_PROP_HEALTH,
  227. POWER_SUPPLY_PROP_PRESENT,
  228. POWER_SUPPLY_PROP_ONLINE,
  229. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  230. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  231. POWER_SUPPLY_PROP_CURRENT_MAX,
  232. POWER_SUPPLY_PROP_CURRENT_NOW,
  233. };
  234. static enum power_supply_property axp22x_usb_power_properties[] = {
  235. POWER_SUPPLY_PROP_HEALTH,
  236. POWER_SUPPLY_PROP_PRESENT,
  237. POWER_SUPPLY_PROP_ONLINE,
  238. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  239. POWER_SUPPLY_PROP_CURRENT_MAX,
  240. };
  241. static const struct power_supply_desc axp20x_usb_power_desc = {
  242. .name = "axp20x-usb",
  243. .type = POWER_SUPPLY_TYPE_USB,
  244. .properties = axp20x_usb_power_properties,
  245. .num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
  246. .property_is_writeable = axp20x_usb_power_prop_writeable,
  247. .get_property = axp20x_usb_power_get_property,
  248. .set_property = axp20x_usb_power_set_property,
  249. };
  250. static const struct power_supply_desc axp22x_usb_power_desc = {
  251. .name = "axp20x-usb",
  252. .type = POWER_SUPPLY_TYPE_USB,
  253. .properties = axp22x_usb_power_properties,
  254. .num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
  255. .property_is_writeable = axp20x_usb_power_prop_writeable,
  256. .get_property = axp20x_usb_power_get_property,
  257. .set_property = axp20x_usb_power_set_property,
  258. };
  259. static int configure_iio_channels(struct platform_device *pdev,
  260. struct axp20x_usb_power *power)
  261. {
  262. power->vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v");
  263. if (IS_ERR(power->vbus_v)) {
  264. if (PTR_ERR(power->vbus_v) == -ENODEV)
  265. return -EPROBE_DEFER;
  266. return PTR_ERR(power->vbus_v);
  267. }
  268. power->vbus_i = devm_iio_channel_get(&pdev->dev, "vbus_i");
  269. if (IS_ERR(power->vbus_i)) {
  270. if (PTR_ERR(power->vbus_i) == -ENODEV)
  271. return -EPROBE_DEFER;
  272. return PTR_ERR(power->vbus_i);
  273. }
  274. return 0;
  275. }
  276. static int configure_adc_registers(struct axp20x_usb_power *power)
  277. {
  278. /* Enable vbus voltage and current measurement */
  279. return regmap_update_bits(power->regmap, AXP20X_ADC_EN1,
  280. AXP20X_ADC_EN1_VBUS_CURR |
  281. AXP20X_ADC_EN1_VBUS_VOLT,
  282. AXP20X_ADC_EN1_VBUS_CURR |
  283. AXP20X_ADC_EN1_VBUS_VOLT);
  284. }
  285. static int axp20x_usb_power_probe(struct platform_device *pdev)
  286. {
  287. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  288. struct power_supply_config psy_cfg = {};
  289. struct axp20x_usb_power *power;
  290. static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN",
  291. "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL };
  292. static const char * const axp22x_irq_names[] = {
  293. "VBUS_PLUGIN", "VBUS_REMOVAL", NULL };
  294. const char * const *irq_names;
  295. const struct power_supply_desc *usb_power_desc;
  296. int i, irq, ret;
  297. if (!of_device_is_available(pdev->dev.of_node))
  298. return -ENODEV;
  299. if (!axp20x) {
  300. dev_err(&pdev->dev, "Parent drvdata not set\n");
  301. return -EINVAL;
  302. }
  303. power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
  304. if (!power)
  305. return -ENOMEM;
  306. power->axp20x_id = (enum axp20x_variants)of_device_get_match_data(
  307. &pdev->dev);
  308. power->np = pdev->dev.of_node;
  309. power->regmap = axp20x->regmap;
  310. if (power->axp20x_id == AXP202_ID) {
  311. /* Enable vbus valid checking */
  312. ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON,
  313. AXP20X_VBUS_MON_VBUS_VALID,
  314. AXP20X_VBUS_MON_VBUS_VALID);
  315. if (ret)
  316. return ret;
  317. if (IS_ENABLED(CONFIG_AXP20X_ADC))
  318. ret = configure_iio_channels(pdev, power);
  319. else
  320. ret = configure_adc_registers(power);
  321. if (ret)
  322. return ret;
  323. usb_power_desc = &axp20x_usb_power_desc;
  324. irq_names = axp20x_irq_names;
  325. } else if (power->axp20x_id == AXP221_ID ||
  326. power->axp20x_id == AXP223_ID) {
  327. usb_power_desc = &axp22x_usb_power_desc;
  328. irq_names = axp22x_irq_names;
  329. } else {
  330. dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
  331. axp20x->variant);
  332. return -EINVAL;
  333. }
  334. psy_cfg.of_node = pdev->dev.of_node;
  335. psy_cfg.drv_data = power;
  336. power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
  337. &psy_cfg);
  338. if (IS_ERR(power->supply))
  339. return PTR_ERR(power->supply);
  340. /* Request irqs after registering, as irqs may trigger immediately */
  341. for (i = 0; irq_names[i]; i++) {
  342. irq = platform_get_irq_byname(pdev, irq_names[i]);
  343. if (irq < 0) {
  344. dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
  345. irq_names[i], irq);
  346. continue;
  347. }
  348. irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
  349. ret = devm_request_any_context_irq(&pdev->dev, irq,
  350. axp20x_usb_power_irq, 0, DRVNAME, power);
  351. if (ret < 0)
  352. dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
  353. irq_names[i], ret);
  354. }
  355. return 0;
  356. }
  357. static const struct of_device_id axp20x_usb_power_match[] = {
  358. {
  359. .compatible = "x-powers,axp202-usb-power-supply",
  360. .data = (void *)AXP202_ID,
  361. }, {
  362. .compatible = "x-powers,axp221-usb-power-supply",
  363. .data = (void *)AXP221_ID,
  364. }, {
  365. .compatible = "x-powers,axp223-usb-power-supply",
  366. .data = (void *)AXP223_ID,
  367. }, { /* sentinel */ }
  368. };
  369. MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
  370. static struct platform_driver axp20x_usb_power_driver = {
  371. .probe = axp20x_usb_power_probe,
  372. .driver = {
  373. .name = DRVNAME,
  374. .of_match_table = axp20x_usb_power_match,
  375. },
  376. };
  377. module_platform_driver(axp20x_usb_power_driver);
  378. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  379. MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
  380. MODULE_LICENSE("GPL");