lego_ev3_battery.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 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. iio_read_channel_processed(batt->iio_v, &val->intval);
  46. val->intval *= 2000;
  47. val->intval += 200000;
  48. /* plus adjust for shunt resistor drop */
  49. iio_read_channel_processed(batt->iio_i, &val2);
  50. val2 *= 1000;
  51. val2 /= 15;
  52. val->intval += val2;
  53. break;
  54. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  55. val->intval = batt->v_max;
  56. break;
  57. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  58. val->intval = batt->v_min;
  59. break;
  60. case POWER_SUPPLY_PROP_CURRENT_NOW:
  61. /* battery current is iio channel / 15 / 0.05 ohms */
  62. iio_read_channel_processed(batt->iio_i, &val->intval);
  63. val->intval *= 20000;
  64. val->intval /= 15;
  65. break;
  66. case POWER_SUPPLY_PROP_SCOPE:
  67. val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
  68. break;
  69. default:
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. static int lego_ev3_battery_set_property(struct power_supply *psy,
  75. enum power_supply_property psp,
  76. const union power_supply_propval *val)
  77. {
  78. struct lego_ev3_battery *batt = power_supply_get_drvdata(psy);
  79. switch (psp) {
  80. case POWER_SUPPLY_PROP_TECHNOLOGY:
  81. /*
  82. * Only allow changing technology from Unknown to NiMH. Li-ion
  83. * batteries are automatically detected and should not be
  84. * overridden. Rechargeable AA batteries, on the other hand,
  85. * cannot be automatically detected, and so must be manually
  86. * specified. This should only be set once during system init,
  87. * so there is no mechanism to go back to Unknown.
  88. */
  89. if (batt->technology != POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
  90. return -EINVAL;
  91. switch (val->intval) {
  92. case POWER_SUPPLY_TECHNOLOGY_NiMH:
  93. batt->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
  94. batt->v_max = 7800000;
  95. batt->v_min = 5400000;
  96. break;
  97. default:
  98. return -EINVAL;
  99. }
  100. break;
  101. default:
  102. return -EINVAL;
  103. }
  104. return 0;
  105. }
  106. static int lego_ev3_battery_property_is_writeable(struct power_supply *psy,
  107. enum power_supply_property psp)
  108. {
  109. struct lego_ev3_battery *batt = power_supply_get_drvdata(psy);
  110. return psp == POWER_SUPPLY_PROP_TECHNOLOGY &&
  111. batt->technology == POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  112. }
  113. static enum power_supply_property lego_ev3_battery_props[] = {
  114. POWER_SUPPLY_PROP_TECHNOLOGY,
  115. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  116. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  117. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  118. POWER_SUPPLY_PROP_CURRENT_NOW,
  119. POWER_SUPPLY_PROP_SCOPE,
  120. };
  121. static const struct power_supply_desc lego_ev3_battery_desc = {
  122. .name = "lego-ev3-battery",
  123. .type = POWER_SUPPLY_TYPE_BATTERY,
  124. .properties = lego_ev3_battery_props,
  125. .num_properties = ARRAY_SIZE(lego_ev3_battery_props),
  126. .get_property = lego_ev3_battery_get_property,
  127. .set_property = lego_ev3_battery_set_property,
  128. .property_is_writeable = lego_ev3_battery_property_is_writeable,
  129. };
  130. static int lego_ev3_battery_probe(struct platform_device *pdev)
  131. {
  132. struct device *dev = &pdev->dev;
  133. struct lego_ev3_battery *batt;
  134. struct power_supply_config psy_cfg = {};
  135. int err;
  136. batt = devm_kzalloc(dev, sizeof(*batt), GFP_KERNEL);
  137. if (!batt)
  138. return -ENOMEM;
  139. platform_set_drvdata(pdev, batt);
  140. batt->iio_v = devm_iio_channel_get(dev, "voltage");
  141. err = PTR_ERR_OR_ZERO(batt->iio_v);
  142. if (err) {
  143. if (err != -EPROBE_DEFER)
  144. dev_err(dev, "Failed to get voltage iio channel\n");
  145. return err;
  146. }
  147. batt->iio_i = devm_iio_channel_get(dev, "current");
  148. err = PTR_ERR_OR_ZERO(batt->iio_i);
  149. if (err) {
  150. if (err != -EPROBE_DEFER)
  151. dev_err(dev, "Failed to get current iio channel\n");
  152. return err;
  153. }
  154. batt->rechargeable_gpio = devm_gpiod_get(dev, "rechargeable", GPIOD_IN);
  155. err = PTR_ERR_OR_ZERO(batt->rechargeable_gpio);
  156. if (err) {
  157. if (err != -EPROBE_DEFER)
  158. dev_err(dev, "Failed to get rechargeable gpio\n");
  159. return err;
  160. }
  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");