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. /* fall through */
  194. case 500000:
  195. case 900000:
  196. val = (900000 - intval) / 400000;
  197. return regmap_update_bits(power->regmap,
  198. AXP20X_VBUS_IPSOUT_MGMT,
  199. AXP20X_VBUS_CLIMIT_MASK, val);
  200. default:
  201. return -EINVAL;
  202. }
  203. return -EINVAL;
  204. }
  205. static int axp20x_usb_power_set_property(struct power_supply *psy,
  206. enum power_supply_property psp,
  207. const union power_supply_propval *val)
  208. {
  209. struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
  210. switch (psp) {
  211. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  212. return axp20x_usb_power_set_voltage_min(power, val->intval);
  213. case POWER_SUPPLY_PROP_CURRENT_MAX:
  214. return axp20x_usb_power_set_current_max(power, val->intval);
  215. default:
  216. return -EINVAL;
  217. }
  218. return -EINVAL;
  219. }
  220. static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
  221. enum power_supply_property psp)
  222. {
  223. return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
  224. psp == POWER_SUPPLY_PROP_CURRENT_MAX;
  225. }
  226. static enum power_supply_property axp20x_usb_power_properties[] = {
  227. POWER_SUPPLY_PROP_HEALTH,
  228. POWER_SUPPLY_PROP_PRESENT,
  229. POWER_SUPPLY_PROP_ONLINE,
  230. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  231. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  232. POWER_SUPPLY_PROP_CURRENT_MAX,
  233. POWER_SUPPLY_PROP_CURRENT_NOW,
  234. };
  235. static enum power_supply_property axp22x_usb_power_properties[] = {
  236. POWER_SUPPLY_PROP_HEALTH,
  237. POWER_SUPPLY_PROP_PRESENT,
  238. POWER_SUPPLY_PROP_ONLINE,
  239. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  240. POWER_SUPPLY_PROP_CURRENT_MAX,
  241. };
  242. static const struct power_supply_desc axp20x_usb_power_desc = {
  243. .name = "axp20x-usb",
  244. .type = POWER_SUPPLY_TYPE_USB,
  245. .properties = axp20x_usb_power_properties,
  246. .num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
  247. .property_is_writeable = axp20x_usb_power_prop_writeable,
  248. .get_property = axp20x_usb_power_get_property,
  249. .set_property = axp20x_usb_power_set_property,
  250. };
  251. static const struct power_supply_desc axp22x_usb_power_desc = {
  252. .name = "axp20x-usb",
  253. .type = POWER_SUPPLY_TYPE_USB,
  254. .properties = axp22x_usb_power_properties,
  255. .num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
  256. .property_is_writeable = axp20x_usb_power_prop_writeable,
  257. .get_property = axp20x_usb_power_get_property,
  258. .set_property = axp20x_usb_power_set_property,
  259. };
  260. static int configure_iio_channels(struct platform_device *pdev,
  261. struct axp20x_usb_power *power)
  262. {
  263. power->vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v");
  264. if (IS_ERR(power->vbus_v)) {
  265. if (PTR_ERR(power->vbus_v) == -ENODEV)
  266. return -EPROBE_DEFER;
  267. return PTR_ERR(power->vbus_v);
  268. }
  269. power->vbus_i = devm_iio_channel_get(&pdev->dev, "vbus_i");
  270. if (IS_ERR(power->vbus_i)) {
  271. if (PTR_ERR(power->vbus_i) == -ENODEV)
  272. return -EPROBE_DEFER;
  273. return PTR_ERR(power->vbus_i);
  274. }
  275. return 0;
  276. }
  277. static int configure_adc_registers(struct axp20x_usb_power *power)
  278. {
  279. /* Enable vbus voltage and current measurement */
  280. return regmap_update_bits(power->regmap, AXP20X_ADC_EN1,
  281. AXP20X_ADC_EN1_VBUS_CURR |
  282. AXP20X_ADC_EN1_VBUS_VOLT,
  283. AXP20X_ADC_EN1_VBUS_CURR |
  284. AXP20X_ADC_EN1_VBUS_VOLT);
  285. }
  286. static int axp20x_usb_power_probe(struct platform_device *pdev)
  287. {
  288. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  289. struct power_supply_config psy_cfg = {};
  290. struct axp20x_usb_power *power;
  291. static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN",
  292. "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL };
  293. static const char * const axp22x_irq_names[] = {
  294. "VBUS_PLUGIN", "VBUS_REMOVAL", NULL };
  295. const char * const *irq_names;
  296. const struct power_supply_desc *usb_power_desc;
  297. int i, irq, ret;
  298. if (!of_device_is_available(pdev->dev.of_node))
  299. return -ENODEV;
  300. if (!axp20x) {
  301. dev_err(&pdev->dev, "Parent drvdata not set\n");
  302. return -EINVAL;
  303. }
  304. power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
  305. if (!power)
  306. return -ENOMEM;
  307. power->axp20x_id = (enum axp20x_variants)of_device_get_match_data(
  308. &pdev->dev);
  309. power->np = pdev->dev.of_node;
  310. power->regmap = axp20x->regmap;
  311. if (power->axp20x_id == AXP202_ID) {
  312. /* Enable vbus valid checking */
  313. ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON,
  314. AXP20X_VBUS_MON_VBUS_VALID,
  315. AXP20X_VBUS_MON_VBUS_VALID);
  316. if (ret)
  317. return ret;
  318. if (IS_ENABLED(CONFIG_AXP20X_ADC))
  319. ret = configure_iio_channels(pdev, power);
  320. else
  321. ret = configure_adc_registers(power);
  322. if (ret)
  323. return ret;
  324. usb_power_desc = &axp20x_usb_power_desc;
  325. irq_names = axp20x_irq_names;
  326. } else if (power->axp20x_id == AXP221_ID ||
  327. power->axp20x_id == AXP223_ID) {
  328. usb_power_desc = &axp22x_usb_power_desc;
  329. irq_names = axp22x_irq_names;
  330. } else {
  331. dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
  332. axp20x->variant);
  333. return -EINVAL;
  334. }
  335. psy_cfg.of_node = pdev->dev.of_node;
  336. psy_cfg.drv_data = power;
  337. power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
  338. &psy_cfg);
  339. if (IS_ERR(power->supply))
  340. return PTR_ERR(power->supply);
  341. /* Request irqs after registering, as irqs may trigger immediately */
  342. for (i = 0; irq_names[i]; i++) {
  343. irq = platform_get_irq_byname(pdev, irq_names[i]);
  344. if (irq < 0) {
  345. dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
  346. irq_names[i], irq);
  347. continue;
  348. }
  349. irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
  350. ret = devm_request_any_context_irq(&pdev->dev, irq,
  351. axp20x_usb_power_irq, 0, DRVNAME, power);
  352. if (ret < 0)
  353. dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
  354. irq_names[i], ret);
  355. }
  356. return 0;
  357. }
  358. static const struct of_device_id axp20x_usb_power_match[] = {
  359. {
  360. .compatible = "x-powers,axp202-usb-power-supply",
  361. .data = (void *)AXP202_ID,
  362. }, {
  363. .compatible = "x-powers,axp221-usb-power-supply",
  364. .data = (void *)AXP221_ID,
  365. }, {
  366. .compatible = "x-powers,axp223-usb-power-supply",
  367. .data = (void *)AXP223_ID,
  368. }, { /* sentinel */ }
  369. };
  370. MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
  371. static struct platform_driver axp20x_usb_power_driver = {
  372. .probe = axp20x_usb_power_probe,
  373. .driver = {
  374. .name = DRVNAME,
  375. .of_match_table = axp20x_usb_power_match,
  376. },
  377. };
  378. module_platform_driver(axp20x_usb_power_driver);
  379. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  380. MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
  381. MODULE_LICENSE("GPL");