sbs-charger.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (c) 2016, Prodys S.L.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This adds support for sbs-charger compilant chips as defined here:
  10. * http://sbs-forum.org/specs/sbc110.pdf
  11. *
  12. * Implemetation based on sbs-battery.c
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/err.h>
  18. #include <linux/power_supply.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/gpio.h>
  23. #include <linux/regmap.h>
  24. #include <linux/of_gpio.h>
  25. #include <linux/bitops.h>
  26. #define SBS_CHARGER_REG_SPEC_INFO 0x11
  27. #define SBS_CHARGER_REG_STATUS 0x13
  28. #define SBS_CHARGER_REG_ALARM_WARNING 0x16
  29. #define SBS_CHARGER_STATUS_CHARGE_INHIBITED BIT(1)
  30. #define SBS_CHARGER_STATUS_RES_COLD BIT(9)
  31. #define SBS_CHARGER_STATUS_RES_HOT BIT(10)
  32. #define SBS_CHARGER_STATUS_BATTERY_PRESENT BIT(14)
  33. #define SBS_CHARGER_STATUS_AC_PRESENT BIT(15)
  34. #define SBS_CHARGER_POLL_TIME 500
  35. struct sbs_info {
  36. struct i2c_client *client;
  37. struct power_supply *power_supply;
  38. struct regmap *regmap;
  39. struct delayed_work work;
  40. unsigned int last_state;
  41. };
  42. static int sbs_get_property(struct power_supply *psy,
  43. enum power_supply_property psp,
  44. union power_supply_propval *val)
  45. {
  46. struct sbs_info *chip = power_supply_get_drvdata(psy);
  47. unsigned int reg;
  48. reg = chip->last_state;
  49. switch (psp) {
  50. case POWER_SUPPLY_PROP_PRESENT:
  51. val->intval = !!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT);
  52. break;
  53. case POWER_SUPPLY_PROP_ONLINE:
  54. val->intval = !!(reg & SBS_CHARGER_STATUS_AC_PRESENT);
  55. break;
  56. case POWER_SUPPLY_PROP_STATUS:
  57. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  58. if (!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT))
  59. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  60. else if (reg & SBS_CHARGER_STATUS_AC_PRESENT &&
  61. !(reg & SBS_CHARGER_STATUS_CHARGE_INHIBITED))
  62. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  63. else
  64. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  65. break;
  66. case POWER_SUPPLY_PROP_HEALTH:
  67. if (reg & SBS_CHARGER_STATUS_RES_COLD)
  68. val->intval = POWER_SUPPLY_HEALTH_COLD;
  69. if (reg & SBS_CHARGER_STATUS_RES_HOT)
  70. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  71. else
  72. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  73. break;
  74. default:
  75. return -EINVAL;
  76. }
  77. return 0;
  78. }
  79. static int sbs_check_state(struct sbs_info *chip)
  80. {
  81. unsigned int reg;
  82. int ret;
  83. ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &reg);
  84. if (!ret && reg != chip->last_state) {
  85. chip->last_state = reg;
  86. power_supply_changed(chip->power_supply);
  87. return 1;
  88. }
  89. return 0;
  90. }
  91. static void sbs_delayed_work(struct work_struct *work)
  92. {
  93. struct sbs_info *chip = container_of(work, struct sbs_info, work.work);
  94. sbs_check_state(chip);
  95. schedule_delayed_work(&chip->work,
  96. msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
  97. }
  98. static irqreturn_t sbs_irq_thread(int irq, void *data)
  99. {
  100. struct sbs_info *chip = data;
  101. int ret;
  102. ret = sbs_check_state(chip);
  103. return ret ? IRQ_HANDLED : IRQ_NONE;
  104. }
  105. static enum power_supply_property sbs_properties[] = {
  106. POWER_SUPPLY_PROP_STATUS,
  107. POWER_SUPPLY_PROP_PRESENT,
  108. POWER_SUPPLY_PROP_ONLINE,
  109. POWER_SUPPLY_PROP_HEALTH,
  110. };
  111. static bool sbs_readable_reg(struct device *dev, unsigned int reg)
  112. {
  113. return reg >= SBS_CHARGER_REG_SPEC_INFO;
  114. }
  115. static bool sbs_volatile_reg(struct device *dev, unsigned int reg)
  116. {
  117. switch (reg) {
  118. case SBS_CHARGER_REG_STATUS:
  119. return true;
  120. }
  121. return false;
  122. }
  123. static const struct regmap_config sbs_regmap = {
  124. .reg_bits = 8,
  125. .val_bits = 16,
  126. .max_register = SBS_CHARGER_REG_ALARM_WARNING,
  127. .readable_reg = sbs_readable_reg,
  128. .volatile_reg = sbs_volatile_reg,
  129. .val_format_endian = REGMAP_ENDIAN_LITTLE, /* since based on SMBus */
  130. };
  131. static const struct power_supply_desc sbs_desc = {
  132. .name = "sbs-charger",
  133. .type = POWER_SUPPLY_TYPE_MAINS,
  134. .properties = sbs_properties,
  135. .num_properties = ARRAY_SIZE(sbs_properties),
  136. .get_property = sbs_get_property,
  137. };
  138. static int sbs_probe(struct i2c_client *client,
  139. const struct i2c_device_id *id)
  140. {
  141. struct power_supply_config psy_cfg = {};
  142. struct sbs_info *chip;
  143. int ret, val;
  144. chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
  145. if (!chip)
  146. return -ENOMEM;
  147. chip->client = client;
  148. psy_cfg.of_node = client->dev.of_node;
  149. psy_cfg.drv_data = chip;
  150. i2c_set_clientdata(client, chip);
  151. chip->regmap = devm_regmap_init_i2c(client, &sbs_regmap);
  152. if (IS_ERR(chip->regmap))
  153. return PTR_ERR(chip->regmap);
  154. /*
  155. * Before we register, we need to make sure we can actually talk
  156. * to the battery.
  157. */
  158. ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &val);
  159. if (ret) {
  160. dev_err(&client->dev, "Failed to get device status\n");
  161. return ret;
  162. }
  163. chip->last_state = val;
  164. chip->power_supply = devm_power_supply_register(&client->dev, &sbs_desc,
  165. &psy_cfg);
  166. if (IS_ERR(chip->power_supply)) {
  167. dev_err(&client->dev, "Failed to register power supply\n");
  168. return PTR_ERR(chip->power_supply);
  169. }
  170. /*
  171. * The sbs-charger spec doesn't impose the use of an interrupt. So in
  172. * the case it wasn't provided we use polling in order get the charger's
  173. * status.
  174. */
  175. if (client->irq) {
  176. ret = devm_request_threaded_irq(&client->dev, client->irq,
  177. NULL, sbs_irq_thread,
  178. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  179. dev_name(&client->dev), chip);
  180. if (ret) {
  181. dev_err(&client->dev, "Failed to request irq, %d\n", ret);
  182. return ret;
  183. }
  184. } else {
  185. INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
  186. schedule_delayed_work(&chip->work,
  187. msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
  188. }
  189. dev_info(&client->dev,
  190. "%s: smart charger device registered\n", client->name);
  191. return 0;
  192. }
  193. static int sbs_remove(struct i2c_client *client)
  194. {
  195. struct sbs_info *chip = i2c_get_clientdata(client);
  196. cancel_delayed_work_sync(&chip->work);
  197. return 0;
  198. }
  199. #ifdef CONFIG_OF
  200. static const struct of_device_id sbs_dt_ids[] = {
  201. { .compatible = "sbs,sbs-charger" },
  202. { },
  203. };
  204. MODULE_DEVICE_TABLE(of, sbs_dt_ids);
  205. #endif
  206. static const struct i2c_device_id sbs_id[] = {
  207. { "sbs-charger", 0 },
  208. { }
  209. };
  210. MODULE_DEVICE_TABLE(i2c, sbs_id);
  211. static struct i2c_driver sbs_driver = {
  212. .probe = sbs_probe,
  213. .remove = sbs_remove,
  214. .id_table = sbs_id,
  215. .driver = {
  216. .name = "sbs-charger",
  217. .of_match_table = of_match_ptr(sbs_dt_ids),
  218. },
  219. };
  220. module_i2c_driver(sbs_driver);
  221. MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
  222. MODULE_DESCRIPTION("SBS smart charger driver");
  223. MODULE_LICENSE("GPL v2");