ingenic-battery.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Battery driver for the Ingenic JZ47xx SoCs
  4. * Copyright (c) 2019 Artur Rojek <contact@artur-rojek.eu>
  5. *
  6. * based on drivers/power/supply/jz4740-battery.c
  7. */
  8. #include <linux/iio/consumer.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/power_supply.h>
  13. #include <linux/property.h>
  14. struct ingenic_battery {
  15. struct device *dev;
  16. struct iio_channel *channel;
  17. struct power_supply_desc desc;
  18. struct power_supply *battery;
  19. struct power_supply_battery_info info;
  20. };
  21. static int ingenic_battery_get_property(struct power_supply *psy,
  22. enum power_supply_property psp,
  23. union power_supply_propval *val)
  24. {
  25. struct ingenic_battery *bat = power_supply_get_drvdata(psy);
  26. struct power_supply_battery_info *info = &bat->info;
  27. int ret;
  28. switch (psp) {
  29. case POWER_SUPPLY_PROP_HEALTH:
  30. ret = iio_read_channel_processed(bat->channel, &val->intval);
  31. val->intval *= 1000;
  32. if (val->intval < info->voltage_min_design_uv)
  33. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  34. else if (val->intval > info->voltage_max_design_uv)
  35. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  36. else
  37. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  38. return ret;
  39. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  40. ret = iio_read_channel_processed(bat->channel, &val->intval);
  41. val->intval *= 1000;
  42. return ret;
  43. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  44. val->intval = info->voltage_min_design_uv;
  45. return 0;
  46. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  47. val->intval = info->voltage_max_design_uv;
  48. return 0;
  49. default:
  50. return -EINVAL;
  51. };
  52. }
  53. /* Set the most appropriate IIO channel voltage reference scale
  54. * based on the battery's max voltage.
  55. */
  56. static int ingenic_battery_set_scale(struct ingenic_battery *bat)
  57. {
  58. const int *scale_raw;
  59. int scale_len, scale_type, best_idx = -1, best_mV, max_raw, i, ret;
  60. u64 max_mV;
  61. ret = iio_read_max_channel_raw(bat->channel, &max_raw);
  62. if (ret) {
  63. dev_err(bat->dev, "Unable to read max raw channel value\n");
  64. return ret;
  65. }
  66. ret = iio_read_avail_channel_attribute(bat->channel, &scale_raw,
  67. &scale_type, &scale_len,
  68. IIO_CHAN_INFO_SCALE);
  69. if (ret < 0) {
  70. dev_err(bat->dev, "Unable to read channel avail scale\n");
  71. return ret;
  72. }
  73. if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2)
  74. return -EINVAL;
  75. max_mV = bat->info.voltage_max_design_uv / 1000;
  76. for (i = 0; i < scale_len; i += 2) {
  77. u64 scale_mV = (max_raw * scale_raw[i]) >> scale_raw[i + 1];
  78. if (scale_mV < max_mV)
  79. continue;
  80. if (best_idx >= 0 && scale_mV > best_mV)
  81. continue;
  82. best_mV = scale_mV;
  83. best_idx = i;
  84. }
  85. if (best_idx < 0) {
  86. dev_err(bat->dev, "Unable to find matching voltage scale\n");
  87. return -EINVAL;
  88. }
  89. /* Only set scale if there is more than one (fractional) entry */
  90. if (scale_len > 2) {
  91. ret = iio_write_channel_attribute(bat->channel,
  92. scale_raw[best_idx],
  93. scale_raw[best_idx + 1],
  94. IIO_CHAN_INFO_SCALE);
  95. if (ret)
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. static enum power_supply_property ingenic_battery_properties[] = {
  101. POWER_SUPPLY_PROP_HEALTH,
  102. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  103. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  104. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  105. };
  106. static int ingenic_battery_probe(struct platform_device *pdev)
  107. {
  108. struct device *dev = &pdev->dev;
  109. struct ingenic_battery *bat;
  110. struct power_supply_config psy_cfg = {};
  111. struct power_supply_desc *desc;
  112. int ret;
  113. bat = devm_kzalloc(dev, sizeof(*bat), GFP_KERNEL);
  114. if (!bat)
  115. return -ENOMEM;
  116. bat->dev = dev;
  117. bat->channel = devm_iio_channel_get(dev, "battery");
  118. if (IS_ERR(bat->channel))
  119. return PTR_ERR(bat->channel);
  120. desc = &bat->desc;
  121. desc->name = "jz-battery";
  122. desc->type = POWER_SUPPLY_TYPE_BATTERY;
  123. desc->properties = ingenic_battery_properties;
  124. desc->num_properties = ARRAY_SIZE(ingenic_battery_properties);
  125. desc->get_property = ingenic_battery_get_property;
  126. psy_cfg.drv_data = bat;
  127. psy_cfg.of_node = dev->of_node;
  128. bat->battery = devm_power_supply_register(dev, desc, &psy_cfg);
  129. if (IS_ERR(bat->battery))
  130. return dev_err_probe(dev, PTR_ERR(bat->battery),
  131. "Unable to register battery\n");
  132. ret = power_supply_get_battery_info(bat->battery, &bat->info);
  133. if (ret) {
  134. dev_err(dev, "Unable to get battery info: %d\n", ret);
  135. return ret;
  136. }
  137. if (bat->info.voltage_min_design_uv < 0) {
  138. dev_err(dev, "Unable to get voltage min design\n");
  139. return bat->info.voltage_min_design_uv;
  140. }
  141. if (bat->info.voltage_max_design_uv < 0) {
  142. dev_err(dev, "Unable to get voltage max design\n");
  143. return bat->info.voltage_max_design_uv;
  144. }
  145. return ingenic_battery_set_scale(bat);
  146. }
  147. #ifdef CONFIG_OF
  148. static const struct of_device_id ingenic_battery_of_match[] = {
  149. { .compatible = "ingenic,jz4740-battery", },
  150. { },
  151. };
  152. MODULE_DEVICE_TABLE(of, ingenic_battery_of_match);
  153. #endif
  154. static struct platform_driver ingenic_battery_driver = {
  155. .driver = {
  156. .name = "ingenic-battery",
  157. .of_match_table = of_match_ptr(ingenic_battery_of_match),
  158. },
  159. .probe = ingenic_battery_probe,
  160. };
  161. module_platform_driver(ingenic_battery_driver);
  162. MODULE_DESCRIPTION("Battery driver for Ingenic JZ47xx SoCs");
  163. MODULE_AUTHOR("Artur Rojek <contact@artur-rojek.eu>");
  164. MODULE_LICENSE("GPL");