sbs-manager.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Driver for SBS compliant Smart Battery System Managers
  3. *
  4. * The device communicates via i2c at address 0x0a and multiplexes access to up
  5. * to four smart batteries at address 0x0b.
  6. *
  7. * Via sysfs interface the online state and charge type are presented.
  8. *
  9. * Datasheet SBSM: http://sbs-forum.org/specs/sbsm100b.pdf
  10. * Datasheet LTC1760: http://cds.linear.com/docs/en/datasheet/1760fb.pdf
  11. *
  12. * Karl-Heinz Schneider <karl-heinz@schneider-inet.de>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/gpio.h>
  19. #include <linux/module.h>
  20. #include <linux/i2c.h>
  21. #include <linux/i2c-mux.h>
  22. #include <linux/power_supply.h>
  23. #include <linux/property.h>
  24. #define SBSM_MAX_BATS 4
  25. #define SBSM_RETRY_CNT 3
  26. /* registers addresses */
  27. #define SBSM_CMD_BATSYSSTATE 0x01
  28. #define SBSM_CMD_BATSYSSTATECONT 0x02
  29. #define SBSM_CMD_BATSYSINFO 0x04
  30. #define SBSM_CMD_LTC 0x3c
  31. #define SBSM_MASK_BAT_SUPPORTED GENMASK(3, 0)
  32. #define SBSM_MASK_CHARGE_BAT GENMASK(7, 4)
  33. #define SBSM_BIT_AC_PRESENT BIT(0)
  34. #define SBSM_BIT_TURBO BIT(7)
  35. #define SBSM_SMB_BAT_OFFSET 11
  36. struct sbsm_data {
  37. struct i2c_client *client;
  38. struct i2c_mux_core *muxc;
  39. struct power_supply *psy;
  40. u8 cur_chan; /* currently selected channel */
  41. struct gpio_chip chip;
  42. bool is_ltc1760; /* special capabilities */
  43. unsigned int supported_bats;
  44. unsigned int last_state;
  45. unsigned int last_state_cont;
  46. };
  47. static enum power_supply_property sbsm_props[] = {
  48. POWER_SUPPLY_PROP_ONLINE,
  49. POWER_SUPPLY_PROP_CHARGE_TYPE,
  50. };
  51. static int sbsm_read_word(struct i2c_client *client, u8 address)
  52. {
  53. int reg, retries;
  54. for (retries = SBSM_RETRY_CNT; retries > 0; retries--) {
  55. reg = i2c_smbus_read_word_data(client, address);
  56. if (reg >= 0)
  57. break;
  58. }
  59. if (reg < 0) {
  60. dev_err(&client->dev, "failed to read register 0x%02x\n",
  61. address);
  62. }
  63. return reg;
  64. }
  65. static int sbsm_write_word(struct i2c_client *client, u8 address, u16 word)
  66. {
  67. int ret, retries;
  68. for (retries = SBSM_RETRY_CNT; retries > 0; retries--) {
  69. ret = i2c_smbus_write_word_data(client, address, word);
  70. if (ret >= 0)
  71. break;
  72. }
  73. if (ret < 0)
  74. dev_err(&client->dev, "failed to write to register 0x%02x\n",
  75. address);
  76. return ret;
  77. }
  78. static int sbsm_get_property(struct power_supply *psy,
  79. enum power_supply_property psp,
  80. union power_supply_propval *val)
  81. {
  82. struct sbsm_data *data = power_supply_get_drvdata(psy);
  83. int regval = 0;
  84. switch (psp) {
  85. case POWER_SUPPLY_PROP_ONLINE:
  86. regval = sbsm_read_word(data->client, SBSM_CMD_BATSYSSTATECONT);
  87. if (regval < 0)
  88. return regval;
  89. val->intval = !!(regval & SBSM_BIT_AC_PRESENT);
  90. break;
  91. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  92. regval = sbsm_read_word(data->client, SBSM_CMD_BATSYSSTATE);
  93. if (regval < 0)
  94. return regval;
  95. if ((regval & SBSM_MASK_CHARGE_BAT) == 0) {
  96. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  97. return 0;
  98. }
  99. val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  100. if (data->is_ltc1760) {
  101. /* charge mode fast if turbo is active */
  102. regval = sbsm_read_word(data->client, SBSM_CMD_LTC);
  103. if (regval < 0)
  104. return regval;
  105. else if (regval & SBSM_BIT_TURBO)
  106. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  107. }
  108. break;
  109. default:
  110. return -EINVAL;
  111. }
  112. return 0;
  113. }
  114. static int sbsm_prop_is_writeable(struct power_supply *psy,
  115. enum power_supply_property psp)
  116. {
  117. struct sbsm_data *data = power_supply_get_drvdata(psy);
  118. return (psp == POWER_SUPPLY_PROP_CHARGE_TYPE) && data->is_ltc1760;
  119. }
  120. static int sbsm_set_property(struct power_supply *psy,
  121. enum power_supply_property psp,
  122. const union power_supply_propval *val)
  123. {
  124. struct sbsm_data *data = power_supply_get_drvdata(psy);
  125. int ret = -EINVAL;
  126. u16 regval;
  127. switch (psp) {
  128. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  129. /* write 1 to TURBO if type fast is given */
  130. if (!data->is_ltc1760)
  131. break;
  132. regval = val->intval ==
  133. POWER_SUPPLY_CHARGE_TYPE_FAST ? SBSM_BIT_TURBO : 0;
  134. ret = sbsm_write_word(data->client, SBSM_CMD_LTC, regval);
  135. break;
  136. default:
  137. break;
  138. }
  139. return ret;
  140. }
  141. /*
  142. * Switch to battery
  143. * Parameter chan is directly the content of SMB_BAT* nibble
  144. */
  145. static int sbsm_select(struct i2c_mux_core *muxc, u32 chan)
  146. {
  147. struct sbsm_data *data = i2c_mux_priv(muxc);
  148. struct device *dev = &data->client->dev;
  149. int ret = 0;
  150. u16 reg;
  151. if (data->cur_chan == chan)
  152. return ret;
  153. /* chan goes from 1 ... 4 */
  154. reg = BIT(SBSM_SMB_BAT_OFFSET + chan);
  155. ret = sbsm_write_word(data->client, SBSM_CMD_BATSYSSTATE, reg);
  156. if (ret)
  157. dev_err(dev, "Failed to select channel %i\n", chan);
  158. else
  159. data->cur_chan = chan;
  160. return ret;
  161. }
  162. static int sbsm_gpio_get_value(struct gpio_chip *gc, unsigned int off)
  163. {
  164. struct sbsm_data *data = gpiochip_get_data(gc);
  165. int ret;
  166. ret = sbsm_read_word(data->client, SBSM_CMD_BATSYSSTATE);
  167. if (ret < 0)
  168. return ret;
  169. return ret & BIT(off);
  170. }
  171. /*
  172. * This needs to be defined or the GPIO lib fails to register the pin.
  173. * But the 'gpio' is always an input.
  174. */
  175. static int sbsm_gpio_direction_input(struct gpio_chip *gc, unsigned int off)
  176. {
  177. return 0;
  178. }
  179. static int sbsm_do_alert(struct device *dev, void *d)
  180. {
  181. struct i2c_client *client = i2c_verify_client(dev);
  182. struct i2c_driver *driver;
  183. if (!client || client->addr != 0x0b)
  184. return 0;
  185. device_lock(dev);
  186. if (client->dev.driver) {
  187. driver = to_i2c_driver(client->dev.driver);
  188. if (driver->alert)
  189. driver->alert(client, I2C_PROTOCOL_SMBUS_ALERT, 0);
  190. else
  191. dev_warn(&client->dev, "no driver alert()!\n");
  192. } else {
  193. dev_dbg(&client->dev, "alert with no driver\n");
  194. }
  195. device_unlock(dev);
  196. return -EBUSY;
  197. }
  198. static void sbsm_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
  199. unsigned int d)
  200. {
  201. struct sbsm_data *sbsm = i2c_get_clientdata(client);
  202. int ret, i, irq_bat = 0, state = 0;
  203. ret = sbsm_read_word(sbsm->client, SBSM_CMD_BATSYSSTATE);
  204. if (ret >= 0) {
  205. irq_bat = ret ^ sbsm->last_state;
  206. sbsm->last_state = ret;
  207. state = ret;
  208. }
  209. ret = sbsm_read_word(sbsm->client, SBSM_CMD_BATSYSSTATECONT);
  210. if ((ret >= 0) &&
  211. ((ret ^ sbsm->last_state_cont) & SBSM_BIT_AC_PRESENT)) {
  212. irq_bat |= sbsm->supported_bats & state;
  213. power_supply_changed(sbsm->psy);
  214. }
  215. sbsm->last_state_cont = ret;
  216. for (i = 0; i < SBSM_MAX_BATS; i++) {
  217. if (irq_bat & BIT(i)) {
  218. device_for_each_child(&sbsm->muxc->adapter[i]->dev,
  219. NULL, sbsm_do_alert);
  220. }
  221. }
  222. }
  223. static int sbsm_gpio_setup(struct sbsm_data *data)
  224. {
  225. struct gpio_chip *gc = &data->chip;
  226. struct i2c_client *client = data->client;
  227. struct device *dev = &client->dev;
  228. int ret;
  229. if (!device_property_present(dev, "gpio-controller"))
  230. return 0;
  231. ret = sbsm_read_word(client, SBSM_CMD_BATSYSSTATE);
  232. if (ret < 0)
  233. return ret;
  234. data->last_state = ret;
  235. ret = sbsm_read_word(client, SBSM_CMD_BATSYSSTATECONT);
  236. if (ret < 0)
  237. return ret;
  238. data->last_state_cont = ret;
  239. gc->get = sbsm_gpio_get_value;
  240. gc->direction_input = sbsm_gpio_direction_input;
  241. gc->can_sleep = true;
  242. gc->base = -1;
  243. gc->ngpio = SBSM_MAX_BATS;
  244. gc->label = client->name;
  245. gc->parent = dev;
  246. gc->owner = THIS_MODULE;
  247. ret = devm_gpiochip_add_data(dev, gc, data);
  248. if (ret) {
  249. dev_err(dev, "devm_gpiochip_add_data failed: %d\n", ret);
  250. return ret;
  251. }
  252. return ret;
  253. }
  254. static const struct power_supply_desc sbsm_default_psy_desc = {
  255. .type = POWER_SUPPLY_TYPE_MAINS,
  256. .properties = sbsm_props,
  257. .num_properties = ARRAY_SIZE(sbsm_props),
  258. .get_property = &sbsm_get_property,
  259. .set_property = &sbsm_set_property,
  260. .property_is_writeable = &sbsm_prop_is_writeable,
  261. };
  262. static int sbsm_probe(struct i2c_client *client,
  263. const struct i2c_device_id *id)
  264. {
  265. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  266. struct sbsm_data *data;
  267. struct device *dev = &client->dev;
  268. struct power_supply_desc *psy_desc;
  269. struct power_supply_config psy_cfg = {};
  270. int ret = 0, i;
  271. /* Device listens only at address 0x0a */
  272. if (client->addr != 0x0a)
  273. return -EINVAL;
  274. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  275. return -EPFNOSUPPORT;
  276. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  277. if (!data)
  278. return -ENOMEM;
  279. i2c_set_clientdata(client, data);
  280. data->client = client;
  281. data->is_ltc1760 = !!strstr(id->name, "ltc1760");
  282. ret = sbsm_read_word(client, SBSM_CMD_BATSYSINFO);
  283. if (ret < 0)
  284. return ret;
  285. data->supported_bats = ret & SBSM_MASK_BAT_SUPPORTED;
  286. data->muxc = i2c_mux_alloc(adapter, dev, SBSM_MAX_BATS, 0,
  287. I2C_MUX_LOCKED, &sbsm_select, NULL);
  288. if (!data->muxc) {
  289. dev_err(dev, "failed to alloc i2c mux\n");
  290. ret = -ENOMEM;
  291. goto err_mux_alloc;
  292. }
  293. data->muxc->priv = data;
  294. /* register muxed i2c channels. One for each supported battery */
  295. for (i = 0; i < SBSM_MAX_BATS; ++i) {
  296. if (data->supported_bats & BIT(i)) {
  297. ret = i2c_mux_add_adapter(data->muxc, 0, i + 1, 0);
  298. if (ret)
  299. break;
  300. }
  301. }
  302. if (ret) {
  303. dev_err(dev, "failed to register i2c mux channel %d\n", i + 1);
  304. goto err_mux_register;
  305. }
  306. psy_desc = devm_kmemdup(dev, &sbsm_default_psy_desc,
  307. sizeof(struct power_supply_desc),
  308. GFP_KERNEL);
  309. if (!psy_desc) {
  310. ret = -ENOMEM;
  311. goto err_psy;
  312. }
  313. psy_desc->name = devm_kasprintf(dev, GFP_KERNEL, "sbsm-%s",
  314. dev_name(&client->dev));
  315. if (!psy_desc->name) {
  316. ret = -ENOMEM;
  317. goto err_psy;
  318. }
  319. ret = sbsm_gpio_setup(data);
  320. if (ret < 0)
  321. goto err_psy;
  322. psy_cfg.drv_data = data;
  323. psy_cfg.of_node = dev->of_node;
  324. data->psy = devm_power_supply_register(dev, psy_desc, &psy_cfg);
  325. if (IS_ERR(data->psy)) {
  326. ret = PTR_ERR(data->psy);
  327. dev_err(dev, "failed to register power supply %s\n",
  328. psy_desc->name);
  329. goto err_psy;
  330. }
  331. return 0;
  332. err_psy:
  333. err_mux_register:
  334. i2c_mux_del_adapters(data->muxc);
  335. err_mux_alloc:
  336. return ret;
  337. }
  338. static int sbsm_remove(struct i2c_client *client)
  339. {
  340. struct sbsm_data *data = i2c_get_clientdata(client);
  341. i2c_mux_del_adapters(data->muxc);
  342. return 0;
  343. }
  344. static const struct i2c_device_id sbsm_ids[] = {
  345. { "sbs-manager", 0 },
  346. { "ltc1760", 0 },
  347. { }
  348. };
  349. MODULE_DEVICE_TABLE(i2c, sbsm_ids);
  350. #ifdef CONFIG_OF
  351. static const struct of_device_id sbsm_dt_ids[] = {
  352. { .compatible = "sbs,sbs-manager" },
  353. { .compatible = "lltc,ltc1760" },
  354. { }
  355. };
  356. MODULE_DEVICE_TABLE(of, sbsm_dt_ids);
  357. #endif
  358. static struct i2c_driver sbsm_driver = {
  359. .driver = {
  360. .name = "sbsm",
  361. .of_match_table = of_match_ptr(sbsm_dt_ids),
  362. },
  363. .probe = sbsm_probe,
  364. .remove = sbsm_remove,
  365. .alert = sbsm_alert,
  366. .id_table = sbsm_ids
  367. };
  368. module_i2c_driver(sbsm_driver);
  369. MODULE_LICENSE("GPL");
  370. MODULE_AUTHOR("Karl-Heinz Schneider <karl-heinz@schneider-inet.de>");
  371. MODULE_DESCRIPTION("SBSM Smart Battery System Manager");