ingenic-battery.c 5.1 KB

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