lego_ev3_battery.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Battery driver for LEGO MINDSTORMS EV3
  3. *
  4. * Copyright (C) 2017 David Lechner <david@lechnology.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  10. * kind, whether express or implied; without even the implied warranty
  11. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/iio/consumer.h>
  18. #include <linux/iio/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/power_supply.h>
  24. struct lego_ev3_battery {
  25. struct iio_channel *iio_v;
  26. struct iio_channel *iio_i;
  27. struct gpio_desc *rechargeable_gpio;
  28. struct power_supply *psy;
  29. int technology;
  30. int v_max;
  31. int v_min;
  32. };
  33. static int lego_ev3_battery_get_property(struct power_supply *psy,
  34. enum power_supply_property psp,
  35. union power_supply_propval *val)
  36. {
  37. struct lego_ev3_battery *batt = power_supply_get_drvdata(psy);
  38. int ret, val2;
  39. switch (psp) {
  40. case POWER_SUPPLY_PROP_TECHNOLOGY:
  41. val->intval = batt->technology;
  42. break;
  43. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  44. /* battery voltage is iio channel * 2 + Vce of transistor */
  45. ret = iio_read_channel_processed(batt->iio_v, &val->intval);
  46. if (ret)
  47. return ret;
  48. val->intval *= 2000;
  49. val->intval += 50000;
  50. /* plus adjust for shunt resistor drop */
  51. ret = iio_read_channel_processed(batt->iio_i, &val2);
  52. if (ret)
  53. return ret;
  54. val2 *= 1000;
  55. val2 /= 15;
  56. val->intval += val2;
  57. break;
  58. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  59. val->intval = batt->v_max;
  60. break;
  61. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  62. val->intval = batt->v_min;
  63. break;
  64. case POWER_SUPPLY_PROP_CURRENT_NOW:
  65. /* battery current is iio channel / 15 / 0.05 ohms */
  66. ret = iio_read_channel_processed(batt->iio_i, &val->intval);
  67. if (ret)
  68. return ret;
  69. val->intval *= 20000;
  70. val->intval /= 15;
  71. break;
  72. case POWER_SUPPLY_PROP_SCOPE:
  73. val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
  74. break;
  75. default:
  76. return -EINVAL;
  77. }
  78. return 0;
  79. }
  80. static int lego_ev3_battery_set_property(struct power_supply *psy,
  81. enum power_supply_property psp,
  82. const union power_supply_propval *val)
  83. {
  84. struct lego_ev3_battery *batt = power_supply_get_drvdata(psy);
  85. switch (psp) {
  86. case POWER_SUPPLY_PROP_TECHNOLOGY:
  87. /*
  88. * Only allow changing technology from Unknown to NiMH. Li-ion
  89. * batteries are automatically detected and should not be
  90. * overridden. Rechargeable AA batteries, on the other hand,
  91. * cannot be automatically detected, and so must be manually
  92. * specified. This should only be set once during system init,
  93. * so there is no mechanism to go back to Unknown.
  94. */
  95. if (batt->technology != POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
  96. return -EINVAL;
  97. switch (val->intval) {
  98. case POWER_SUPPLY_TECHNOLOGY_NiMH:
  99. batt->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
  100. batt->v_max = 7800000;
  101. batt->v_min = 5400000;
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. break;
  107. default:
  108. return -EINVAL;
  109. }
  110. return 0;
  111. }
  112. static int lego_ev3_battery_property_is_writeable(struct power_supply *psy,
  113. enum power_supply_property psp)
  114. {
  115. struct lego_ev3_battery *batt = power_supply_get_drvdata(psy);
  116. return psp == POWER_SUPPLY_PROP_TECHNOLOGY &&
  117. batt->technology == POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  118. }
  119. static enum power_supply_property lego_ev3_battery_props[] = {
  120. POWER_SUPPLY_PROP_TECHNOLOGY,
  121. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  122. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  123. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  124. POWER_SUPPLY_PROP_CURRENT_NOW,
  125. POWER_SUPPLY_PROP_SCOPE,
  126. };
  127. static const struct power_supply_desc lego_ev3_battery_desc = {
  128. .name = "lego-ev3-battery",
  129. .type = POWER_SUPPLY_TYPE_BATTERY,
  130. .properties = lego_ev3_battery_props,
  131. .num_properties = ARRAY_SIZE(lego_ev3_battery_props),
  132. .get_property = lego_ev3_battery_get_property,
  133. .set_property = lego_ev3_battery_set_property,
  134. .property_is_writeable = lego_ev3_battery_property_is_writeable,
  135. };
  136. static int lego_ev3_battery_probe(struct platform_device *pdev)
  137. {
  138. struct device *dev = &pdev->dev;
  139. struct lego_ev3_battery *batt;
  140. struct power_supply_config psy_cfg = {};
  141. int err;
  142. batt = devm_kzalloc(dev, sizeof(*batt), GFP_KERNEL);
  143. if (!batt)
  144. return -ENOMEM;
  145. platform_set_drvdata(pdev, batt);
  146. batt->iio_v = devm_iio_channel_get(dev, "voltage");
  147. err = PTR_ERR_OR_ZERO(batt->iio_v);
  148. if (err)
  149. return dev_err_probe(dev, err,
  150. "Failed to get voltage iio channel\n");
  151. batt->iio_i = devm_iio_channel_get(dev, "current");
  152. err = PTR_ERR_OR_ZERO(batt->iio_i);
  153. if (err)
  154. return dev_err_probe(dev, err,
  155. "Failed to get current iio channel\n");
  156. batt->rechargeable_gpio = devm_gpiod_get(dev, "rechargeable", GPIOD_IN);
  157. err = PTR_ERR_OR_ZERO(batt->rechargeable_gpio);
  158. if (err)
  159. return dev_err_probe(dev, err,
  160. "Failed to get rechargeable gpio\n");
  161. /*
  162. * The rechargeable battery indication switch cannot be changed without
  163. * removing the battery, so we only need to read it once.
  164. */
  165. if (gpiod_get_value(batt->rechargeable_gpio)) {
  166. /* 2-cell Li-ion, 7.4V nominal */
  167. batt->technology = POWER_SUPPLY_TECHNOLOGY_LION;
  168. batt->v_max = 84000000;
  169. batt->v_min = 60000000;
  170. } else {
  171. /* 6x AA Alkaline, 9V nominal */
  172. batt->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  173. batt->v_max = 90000000;
  174. batt->v_min = 48000000;
  175. }
  176. psy_cfg.of_node = pdev->dev.of_node;
  177. psy_cfg.drv_data = batt;
  178. batt->psy = devm_power_supply_register(dev, &lego_ev3_battery_desc,
  179. &psy_cfg);
  180. err = PTR_ERR_OR_ZERO(batt->psy);
  181. if (err) {
  182. dev_err(dev, "failed to register power supply\n");
  183. return err;
  184. }
  185. return 0;
  186. }
  187. static const struct of_device_id of_lego_ev3_battery_match[] = {
  188. { .compatible = "lego,ev3-battery", },
  189. { }
  190. };
  191. MODULE_DEVICE_TABLE(of, of_lego_ev3_battery_match);
  192. static struct platform_driver lego_ev3_battery_driver = {
  193. .driver = {
  194. .name = "lego-ev3-battery",
  195. .of_match_table = of_lego_ev3_battery_match,
  196. },
  197. .probe = lego_ev3_battery_probe,
  198. };
  199. module_platform_driver(lego_ev3_battery_driver);
  200. MODULE_LICENSE("GPL");
  201. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  202. MODULE_DESCRIPTION("LEGO MINDSTORMS EV3 Battery Driver");