sbs-battery.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. * Gas Gauge driver for SBS Compliant Batteries
  3. *
  4. * Copyright (c) 2010, NVIDIA Corporation.
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/i2c.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/power/sbs-battery.h>
  26. #include <linux/power_supply.h>
  27. #include <linux/slab.h>
  28. #include <linux/stat.h>
  29. enum {
  30. REG_MANUFACTURER_DATA,
  31. REG_TEMPERATURE,
  32. REG_VOLTAGE,
  33. REG_CURRENT,
  34. REG_CAPACITY,
  35. REG_TIME_TO_EMPTY,
  36. REG_TIME_TO_FULL,
  37. REG_STATUS,
  38. REG_CAPACITY_LEVEL,
  39. REG_CYCLE_COUNT,
  40. REG_SERIAL_NUMBER,
  41. REG_REMAINING_CAPACITY,
  42. REG_REMAINING_CAPACITY_CHARGE,
  43. REG_FULL_CHARGE_CAPACITY,
  44. REG_FULL_CHARGE_CAPACITY_CHARGE,
  45. REG_DESIGN_CAPACITY,
  46. REG_DESIGN_CAPACITY_CHARGE,
  47. REG_DESIGN_VOLTAGE_MIN,
  48. REG_DESIGN_VOLTAGE_MAX,
  49. REG_MANUFACTURER,
  50. REG_MODEL_NAME,
  51. };
  52. /* Battery Mode defines */
  53. #define BATTERY_MODE_OFFSET 0x03
  54. #define BATTERY_MODE_MASK 0x8000
  55. enum sbs_battery_mode {
  56. BATTERY_MODE_AMPS = 0,
  57. BATTERY_MODE_WATTS = 0x8000
  58. };
  59. /* manufacturer access defines */
  60. #define MANUFACTURER_ACCESS_STATUS 0x0006
  61. #define MANUFACTURER_ACCESS_SLEEP 0x0011
  62. /* battery status value bits */
  63. #define BATTERY_INITIALIZED 0x80
  64. #define BATTERY_DISCHARGING 0x40
  65. #define BATTERY_FULL_CHARGED 0x20
  66. #define BATTERY_FULL_DISCHARGED 0x10
  67. /* min_value and max_value are only valid for numerical data */
  68. #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
  69. .psp = _psp, \
  70. .addr = _addr, \
  71. .min_value = _min_value, \
  72. .max_value = _max_value, \
  73. }
  74. static const struct chip_data {
  75. enum power_supply_property psp;
  76. u8 addr;
  77. int min_value;
  78. int max_value;
  79. } sbs_data[] = {
  80. [REG_MANUFACTURER_DATA] =
  81. SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
  82. [REG_TEMPERATURE] =
  83. SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
  84. [REG_VOLTAGE] =
  85. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
  86. [REG_CURRENT] =
  87. SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
  88. [REG_CAPACITY] =
  89. SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
  90. [REG_REMAINING_CAPACITY] =
  91. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
  92. [REG_REMAINING_CAPACITY_CHARGE] =
  93. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
  94. [REG_FULL_CHARGE_CAPACITY] =
  95. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
  96. [REG_FULL_CHARGE_CAPACITY_CHARGE] =
  97. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
  98. [REG_TIME_TO_EMPTY] =
  99. SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
  100. [REG_TIME_TO_FULL] =
  101. SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
  102. [REG_STATUS] =
  103. SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
  104. [REG_CAPACITY_LEVEL] =
  105. SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
  106. [REG_CYCLE_COUNT] =
  107. SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
  108. [REG_DESIGN_CAPACITY] =
  109. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
  110. [REG_DESIGN_CAPACITY_CHARGE] =
  111. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
  112. [REG_DESIGN_VOLTAGE_MIN] =
  113. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
  114. [REG_DESIGN_VOLTAGE_MAX] =
  115. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
  116. [REG_SERIAL_NUMBER] =
  117. SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
  118. /* Properties of type `const char *' */
  119. [REG_MANUFACTURER] =
  120. SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
  121. [REG_MODEL_NAME] =
  122. SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
  123. };
  124. static enum power_supply_property sbs_properties[] = {
  125. POWER_SUPPLY_PROP_STATUS,
  126. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  127. POWER_SUPPLY_PROP_HEALTH,
  128. POWER_SUPPLY_PROP_PRESENT,
  129. POWER_SUPPLY_PROP_TECHNOLOGY,
  130. POWER_SUPPLY_PROP_CYCLE_COUNT,
  131. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  132. POWER_SUPPLY_PROP_CURRENT_NOW,
  133. POWER_SUPPLY_PROP_CAPACITY,
  134. POWER_SUPPLY_PROP_TEMP,
  135. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  136. POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  137. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  138. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  139. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  140. POWER_SUPPLY_PROP_ENERGY_NOW,
  141. POWER_SUPPLY_PROP_ENERGY_FULL,
  142. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  143. POWER_SUPPLY_PROP_CHARGE_NOW,
  144. POWER_SUPPLY_PROP_CHARGE_FULL,
  145. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  146. /* Properties of type `const char *' */
  147. POWER_SUPPLY_PROP_MANUFACTURER,
  148. POWER_SUPPLY_PROP_MODEL_NAME
  149. };
  150. struct sbs_info {
  151. struct i2c_client *client;
  152. struct power_supply *power_supply;
  153. bool is_present;
  154. struct gpio_desc *gpio_detect;
  155. bool enable_detection;
  156. int last_state;
  157. int poll_time;
  158. u32 i2c_retry_count;
  159. u32 poll_retry_count;
  160. struct delayed_work work;
  161. struct mutex mode_lock;
  162. };
  163. static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
  164. static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
  165. static bool force_load;
  166. static int sbs_read_word_data(struct i2c_client *client, u8 address)
  167. {
  168. struct sbs_info *chip = i2c_get_clientdata(client);
  169. s32 ret = 0;
  170. int retries = 1;
  171. retries = chip->i2c_retry_count;
  172. while (retries > 0) {
  173. ret = i2c_smbus_read_word_data(client, address);
  174. if (ret >= 0)
  175. break;
  176. retries--;
  177. }
  178. if (ret < 0) {
  179. dev_dbg(&client->dev,
  180. "%s: i2c read at address 0x%x failed\n",
  181. __func__, address);
  182. return ret;
  183. }
  184. return ret;
  185. }
  186. static int sbs_read_string_data(struct i2c_client *client, u8 address,
  187. char *values)
  188. {
  189. struct sbs_info *chip = i2c_get_clientdata(client);
  190. s32 ret = 0, block_length = 0;
  191. int retries_length = 1, retries_block = 1;
  192. u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
  193. retries_length = chip->i2c_retry_count;
  194. retries_block = chip->i2c_retry_count;
  195. /* Adapter needs to support these two functions */
  196. if (!i2c_check_functionality(client->adapter,
  197. I2C_FUNC_SMBUS_BYTE_DATA |
  198. I2C_FUNC_SMBUS_I2C_BLOCK)){
  199. return -ENODEV;
  200. }
  201. /* Get the length of block data */
  202. while (retries_length > 0) {
  203. ret = i2c_smbus_read_byte_data(client, address);
  204. if (ret >= 0)
  205. break;
  206. retries_length--;
  207. }
  208. if (ret < 0) {
  209. dev_dbg(&client->dev,
  210. "%s: i2c read at address 0x%x failed\n",
  211. __func__, address);
  212. return ret;
  213. }
  214. /* block_length does not include NULL terminator */
  215. block_length = ret;
  216. if (block_length > I2C_SMBUS_BLOCK_MAX) {
  217. dev_err(&client->dev,
  218. "%s: Returned block_length is longer than 0x%x\n",
  219. __func__, I2C_SMBUS_BLOCK_MAX);
  220. return -EINVAL;
  221. }
  222. /* Get the block data */
  223. while (retries_block > 0) {
  224. ret = i2c_smbus_read_i2c_block_data(
  225. client, address,
  226. block_length + 1, block_buffer);
  227. if (ret >= 0)
  228. break;
  229. retries_block--;
  230. }
  231. if (ret < 0) {
  232. dev_dbg(&client->dev,
  233. "%s: i2c read at address 0x%x failed\n",
  234. __func__, address);
  235. return ret;
  236. }
  237. /* block_buffer[0] == block_length */
  238. memcpy(values, block_buffer + 1, block_length);
  239. values[block_length] = '\0';
  240. return ret;
  241. }
  242. static int sbs_write_word_data(struct i2c_client *client, u8 address,
  243. u16 value)
  244. {
  245. struct sbs_info *chip = i2c_get_clientdata(client);
  246. s32 ret = 0;
  247. int retries = 1;
  248. retries = chip->i2c_retry_count;
  249. while (retries > 0) {
  250. ret = i2c_smbus_write_word_data(client, address, value);
  251. if (ret >= 0)
  252. break;
  253. retries--;
  254. }
  255. if (ret < 0) {
  256. dev_dbg(&client->dev,
  257. "%s: i2c write to address 0x%x failed\n",
  258. __func__, address);
  259. return ret;
  260. }
  261. return 0;
  262. }
  263. static int sbs_status_correct(struct i2c_client *client, int *intval)
  264. {
  265. int ret;
  266. ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
  267. if (ret < 0)
  268. return ret;
  269. ret = (s16)ret;
  270. /* Not drawing current means full (cannot be not charging) */
  271. if (ret == 0)
  272. *intval = POWER_SUPPLY_STATUS_FULL;
  273. if (*intval == POWER_SUPPLY_STATUS_FULL) {
  274. /* Drawing or providing current when full */
  275. if (ret > 0)
  276. *intval = POWER_SUPPLY_STATUS_CHARGING;
  277. else if (ret < 0)
  278. *intval = POWER_SUPPLY_STATUS_DISCHARGING;
  279. }
  280. return 0;
  281. }
  282. static int sbs_get_battery_presence_and_health(
  283. struct i2c_client *client, enum power_supply_property psp,
  284. union power_supply_propval *val)
  285. {
  286. s32 ret;
  287. struct sbs_info *chip = i2c_get_clientdata(client);
  288. if (psp == POWER_SUPPLY_PROP_PRESENT && chip->gpio_detect) {
  289. ret = gpiod_get_value_cansleep(chip->gpio_detect);
  290. if (ret < 0)
  291. return ret;
  292. val->intval = ret;
  293. chip->is_present = val->intval;
  294. return ret;
  295. }
  296. /*
  297. * Write to ManufacturerAccess with ManufacturerAccess command
  298. * and then read the status. Do not check for error on the write
  299. * since not all batteries implement write access to this command,
  300. * while others mandate it.
  301. */
  302. sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
  303. MANUFACTURER_ACCESS_STATUS);
  304. ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
  305. if (ret < 0) {
  306. if (psp == POWER_SUPPLY_PROP_PRESENT)
  307. val->intval = 0; /* battery removed */
  308. return ret;
  309. }
  310. if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
  311. ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
  312. val->intval = 0;
  313. return 0;
  314. }
  315. /* Mask the upper nibble of 2nd byte and
  316. * lower byte of response then
  317. * shift the result by 8 to get status*/
  318. ret &= 0x0F00;
  319. ret >>= 8;
  320. if (psp == POWER_SUPPLY_PROP_PRESENT) {
  321. if (ret == 0x0F)
  322. /* battery removed */
  323. val->intval = 0;
  324. else
  325. val->intval = 1;
  326. } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
  327. if (ret == 0x09)
  328. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  329. else if (ret == 0x0B)
  330. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  331. else if (ret == 0x0C)
  332. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  333. else
  334. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  335. }
  336. return 0;
  337. }
  338. static int sbs_get_battery_property(struct i2c_client *client,
  339. int reg_offset, enum power_supply_property psp,
  340. union power_supply_propval *val)
  341. {
  342. struct sbs_info *chip = i2c_get_clientdata(client);
  343. s32 ret;
  344. ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
  345. if (ret < 0)
  346. return ret;
  347. /* returned values are 16 bit */
  348. if (sbs_data[reg_offset].min_value < 0)
  349. ret = (s16)ret;
  350. if (ret >= sbs_data[reg_offset].min_value &&
  351. ret <= sbs_data[reg_offset].max_value) {
  352. val->intval = ret;
  353. if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
  354. if (!(ret & BATTERY_INITIALIZED))
  355. val->intval =
  356. POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  357. else if (ret & BATTERY_FULL_CHARGED)
  358. val->intval =
  359. POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  360. else if (ret & BATTERY_FULL_DISCHARGED)
  361. val->intval =
  362. POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  363. else
  364. val->intval =
  365. POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  366. return 0;
  367. } else if (psp != POWER_SUPPLY_PROP_STATUS) {
  368. return 0;
  369. }
  370. if (ret & BATTERY_FULL_CHARGED)
  371. val->intval = POWER_SUPPLY_STATUS_FULL;
  372. else if (ret & BATTERY_DISCHARGING)
  373. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  374. else
  375. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  376. sbs_status_correct(client, &val->intval);
  377. if (chip->poll_time == 0)
  378. chip->last_state = val->intval;
  379. else if (chip->last_state != val->intval) {
  380. cancel_delayed_work_sync(&chip->work);
  381. power_supply_changed(chip->power_supply);
  382. chip->poll_time = 0;
  383. }
  384. } else {
  385. if (psp == POWER_SUPPLY_PROP_STATUS)
  386. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  387. else if (psp == POWER_SUPPLY_PROP_CAPACITY)
  388. /* sbs spec says that this can be >100 %
  389. * even if max value is 100 %
  390. */
  391. val->intval = min(ret, 100);
  392. else
  393. val->intval = 0;
  394. }
  395. return 0;
  396. }
  397. static int sbs_get_battery_string_property(struct i2c_client *client,
  398. int reg_offset, enum power_supply_property psp, char *val)
  399. {
  400. s32 ret;
  401. ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
  402. if (ret < 0)
  403. return ret;
  404. return 0;
  405. }
  406. static void sbs_unit_adjustment(struct i2c_client *client,
  407. enum power_supply_property psp, union power_supply_propval *val)
  408. {
  409. #define BASE_UNIT_CONVERSION 1000
  410. #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
  411. #define TIME_UNIT_CONVERSION 60
  412. #define TEMP_KELVIN_TO_CELSIUS 2731
  413. switch (psp) {
  414. case POWER_SUPPLY_PROP_ENERGY_NOW:
  415. case POWER_SUPPLY_PROP_ENERGY_FULL:
  416. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  417. /* sbs provides energy in units of 10mWh.
  418. * Convert to µWh
  419. */
  420. val->intval *= BATTERY_MODE_CAP_MULT_WATT;
  421. break;
  422. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  423. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  424. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  425. case POWER_SUPPLY_PROP_CURRENT_NOW:
  426. case POWER_SUPPLY_PROP_CHARGE_NOW:
  427. case POWER_SUPPLY_PROP_CHARGE_FULL:
  428. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  429. val->intval *= BASE_UNIT_CONVERSION;
  430. break;
  431. case POWER_SUPPLY_PROP_TEMP:
  432. /* sbs provides battery temperature in 0.1K
  433. * so convert it to 0.1°C
  434. */
  435. val->intval -= TEMP_KELVIN_TO_CELSIUS;
  436. break;
  437. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  438. case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
  439. /* sbs provides time to empty and time to full in minutes.
  440. * Convert to seconds
  441. */
  442. val->intval *= TIME_UNIT_CONVERSION;
  443. break;
  444. default:
  445. dev_dbg(&client->dev,
  446. "%s: no need for unit conversion %d\n", __func__, psp);
  447. }
  448. }
  449. static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
  450. enum sbs_battery_mode mode)
  451. {
  452. int ret, original_val;
  453. original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
  454. if (original_val < 0)
  455. return original_val;
  456. if ((original_val & BATTERY_MODE_MASK) == mode)
  457. return mode;
  458. if (mode == BATTERY_MODE_AMPS)
  459. ret = original_val & ~BATTERY_MODE_MASK;
  460. else
  461. ret = original_val | BATTERY_MODE_MASK;
  462. ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
  463. if (ret < 0)
  464. return ret;
  465. usleep_range(1000, 2000);
  466. return original_val & BATTERY_MODE_MASK;
  467. }
  468. static int sbs_get_battery_capacity(struct i2c_client *client,
  469. int reg_offset, enum power_supply_property psp,
  470. union power_supply_propval *val)
  471. {
  472. s32 ret;
  473. enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
  474. if (power_supply_is_amp_property(psp))
  475. mode = BATTERY_MODE_AMPS;
  476. mode = sbs_set_battery_mode(client, mode);
  477. if (mode < 0)
  478. return mode;
  479. ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
  480. if (ret < 0)
  481. return ret;
  482. val->intval = ret;
  483. ret = sbs_set_battery_mode(client, mode);
  484. if (ret < 0)
  485. return ret;
  486. return 0;
  487. }
  488. static char sbs_serial[5];
  489. static int sbs_get_battery_serial_number(struct i2c_client *client,
  490. union power_supply_propval *val)
  491. {
  492. int ret;
  493. ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
  494. if (ret < 0)
  495. return ret;
  496. ret = sprintf(sbs_serial, "%04x", ret);
  497. val->strval = sbs_serial;
  498. return 0;
  499. }
  500. static int sbs_get_property_index(struct i2c_client *client,
  501. enum power_supply_property psp)
  502. {
  503. int count;
  504. for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
  505. if (psp == sbs_data[count].psp)
  506. return count;
  507. dev_warn(&client->dev,
  508. "%s: Invalid Property - %d\n", __func__, psp);
  509. return -EINVAL;
  510. }
  511. static int sbs_get_property(struct power_supply *psy,
  512. enum power_supply_property psp,
  513. union power_supply_propval *val)
  514. {
  515. int ret = 0;
  516. struct sbs_info *chip = power_supply_get_drvdata(psy);
  517. struct i2c_client *client = chip->client;
  518. switch (psp) {
  519. case POWER_SUPPLY_PROP_PRESENT:
  520. case POWER_SUPPLY_PROP_HEALTH:
  521. ret = sbs_get_battery_presence_and_health(client, psp, val);
  522. if (psp == POWER_SUPPLY_PROP_PRESENT)
  523. return 0;
  524. break;
  525. case POWER_SUPPLY_PROP_TECHNOLOGY:
  526. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  527. goto done; /* don't trigger power_supply_changed()! */
  528. case POWER_SUPPLY_PROP_ENERGY_NOW:
  529. case POWER_SUPPLY_PROP_ENERGY_FULL:
  530. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  531. case POWER_SUPPLY_PROP_CHARGE_NOW:
  532. case POWER_SUPPLY_PROP_CHARGE_FULL:
  533. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  534. ret = sbs_get_property_index(client, psp);
  535. if (ret < 0)
  536. break;
  537. /* sbs_get_battery_capacity() will change the battery mode
  538. * temporarily to read the requested attribute. Ensure we stay
  539. * in the desired mode for the duration of the attribute read.
  540. */
  541. mutex_lock(&chip->mode_lock);
  542. ret = sbs_get_battery_capacity(client, ret, psp, val);
  543. mutex_unlock(&chip->mode_lock);
  544. break;
  545. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  546. ret = sbs_get_battery_serial_number(client, val);
  547. break;
  548. case POWER_SUPPLY_PROP_STATUS:
  549. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  550. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  551. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  552. case POWER_SUPPLY_PROP_CURRENT_NOW:
  553. case POWER_SUPPLY_PROP_TEMP:
  554. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  555. case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
  556. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  557. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  558. case POWER_SUPPLY_PROP_CAPACITY:
  559. ret = sbs_get_property_index(client, psp);
  560. if (ret < 0)
  561. break;
  562. ret = sbs_get_battery_property(client, ret, psp, val);
  563. break;
  564. case POWER_SUPPLY_PROP_MODEL_NAME:
  565. ret = sbs_get_property_index(client, psp);
  566. if (ret < 0)
  567. break;
  568. ret = sbs_get_battery_string_property(client, ret, psp,
  569. model_name);
  570. val->strval = model_name;
  571. break;
  572. case POWER_SUPPLY_PROP_MANUFACTURER:
  573. ret = sbs_get_property_index(client, psp);
  574. if (ret < 0)
  575. break;
  576. ret = sbs_get_battery_string_property(client, ret, psp,
  577. manufacturer);
  578. val->strval = manufacturer;
  579. break;
  580. default:
  581. dev_err(&client->dev,
  582. "%s: INVALID property\n", __func__);
  583. return -EINVAL;
  584. }
  585. if (!chip->enable_detection)
  586. goto done;
  587. if (!chip->gpio_detect &&
  588. chip->is_present != (ret >= 0)) {
  589. chip->is_present = (ret >= 0);
  590. power_supply_changed(chip->power_supply);
  591. }
  592. done:
  593. if (!ret) {
  594. /* Convert units to match requirements for power supply class */
  595. sbs_unit_adjustment(client, psp, val);
  596. }
  597. dev_dbg(&client->dev,
  598. "%s: property = %d, value = %x\n", __func__, psp, val->intval);
  599. if (ret && chip->is_present)
  600. return ret;
  601. /* battery not present, so return NODATA for properties */
  602. if (ret)
  603. return -ENODATA;
  604. return 0;
  605. }
  606. static void sbs_supply_changed(struct sbs_info *chip)
  607. {
  608. struct power_supply *battery = chip->power_supply;
  609. int ret;
  610. ret = gpiod_get_value_cansleep(chip->gpio_detect);
  611. if (ret < 0)
  612. return;
  613. chip->is_present = ret;
  614. power_supply_changed(battery);
  615. }
  616. static irqreturn_t sbs_irq(int irq, void *devid)
  617. {
  618. sbs_supply_changed(devid);
  619. return IRQ_HANDLED;
  620. }
  621. static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
  622. unsigned int data)
  623. {
  624. sbs_supply_changed(i2c_get_clientdata(client));
  625. }
  626. static void sbs_external_power_changed(struct power_supply *psy)
  627. {
  628. struct sbs_info *chip = power_supply_get_drvdata(psy);
  629. /* cancel outstanding work */
  630. cancel_delayed_work_sync(&chip->work);
  631. schedule_delayed_work(&chip->work, HZ);
  632. chip->poll_time = chip->poll_retry_count;
  633. }
  634. static void sbs_delayed_work(struct work_struct *work)
  635. {
  636. struct sbs_info *chip;
  637. s32 ret;
  638. chip = container_of(work, struct sbs_info, work.work);
  639. ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
  640. /* if the read failed, give up on this work */
  641. if (ret < 0) {
  642. chip->poll_time = 0;
  643. return;
  644. }
  645. if (ret & BATTERY_FULL_CHARGED)
  646. ret = POWER_SUPPLY_STATUS_FULL;
  647. else if (ret & BATTERY_DISCHARGING)
  648. ret = POWER_SUPPLY_STATUS_DISCHARGING;
  649. else
  650. ret = POWER_SUPPLY_STATUS_CHARGING;
  651. sbs_status_correct(chip->client, &ret);
  652. if (chip->last_state != ret) {
  653. chip->poll_time = 0;
  654. power_supply_changed(chip->power_supply);
  655. return;
  656. }
  657. if (chip->poll_time > 0) {
  658. schedule_delayed_work(&chip->work, HZ);
  659. chip->poll_time--;
  660. return;
  661. }
  662. }
  663. static const struct power_supply_desc sbs_default_desc = {
  664. .type = POWER_SUPPLY_TYPE_BATTERY,
  665. .properties = sbs_properties,
  666. .num_properties = ARRAY_SIZE(sbs_properties),
  667. .get_property = sbs_get_property,
  668. .external_power_changed = sbs_external_power_changed,
  669. };
  670. static int sbs_probe(struct i2c_client *client,
  671. const struct i2c_device_id *id)
  672. {
  673. struct sbs_info *chip;
  674. struct power_supply_desc *sbs_desc;
  675. struct sbs_platform_data *pdata = client->dev.platform_data;
  676. struct power_supply_config psy_cfg = {};
  677. int rc;
  678. int irq;
  679. sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
  680. sizeof(*sbs_desc), GFP_KERNEL);
  681. if (!sbs_desc)
  682. return -ENOMEM;
  683. sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
  684. dev_name(&client->dev));
  685. if (!sbs_desc->name)
  686. return -ENOMEM;
  687. chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
  688. if (!chip)
  689. return -ENOMEM;
  690. chip->client = client;
  691. chip->enable_detection = false;
  692. psy_cfg.of_node = client->dev.of_node;
  693. psy_cfg.drv_data = chip;
  694. chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
  695. mutex_init(&chip->mode_lock);
  696. /* use pdata if available, fall back to DT properties,
  697. * or hardcoded defaults if not
  698. */
  699. rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
  700. &chip->i2c_retry_count);
  701. if (rc)
  702. chip->i2c_retry_count = 0;
  703. rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
  704. &chip->poll_retry_count);
  705. if (rc)
  706. chip->poll_retry_count = 0;
  707. if (pdata) {
  708. chip->poll_retry_count = pdata->poll_retry_count;
  709. chip->i2c_retry_count = pdata->i2c_retry_count;
  710. }
  711. chip->i2c_retry_count = chip->i2c_retry_count + 1;
  712. chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
  713. "sbs,battery-detect", GPIOD_IN);
  714. if (IS_ERR(chip->gpio_detect)) {
  715. dev_err(&client->dev, "Failed to get gpio: %ld\n",
  716. PTR_ERR(chip->gpio_detect));
  717. return PTR_ERR(chip->gpio_detect);
  718. }
  719. i2c_set_clientdata(client, chip);
  720. if (!chip->gpio_detect)
  721. goto skip_gpio;
  722. irq = gpiod_to_irq(chip->gpio_detect);
  723. if (irq <= 0) {
  724. dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
  725. goto skip_gpio;
  726. }
  727. rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
  728. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  729. dev_name(&client->dev), chip);
  730. if (rc) {
  731. dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
  732. goto skip_gpio;
  733. }
  734. skip_gpio:
  735. /*
  736. * Before we register, we might need to make sure we can actually talk
  737. * to the battery.
  738. */
  739. if (!(force_load || chip->gpio_detect)) {
  740. rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
  741. if (rc < 0) {
  742. dev_err(&client->dev, "%s: Failed to get device status\n",
  743. __func__);
  744. goto exit_psupply;
  745. }
  746. }
  747. chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
  748. &psy_cfg);
  749. if (IS_ERR(chip->power_supply)) {
  750. dev_err(&client->dev,
  751. "%s: Failed to register power supply\n", __func__);
  752. rc = PTR_ERR(chip->power_supply);
  753. goto exit_psupply;
  754. }
  755. dev_info(&client->dev,
  756. "%s: battery gas gauge device registered\n", client->name);
  757. INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
  758. chip->enable_detection = true;
  759. return 0;
  760. exit_psupply:
  761. return rc;
  762. }
  763. static int sbs_remove(struct i2c_client *client)
  764. {
  765. struct sbs_info *chip = i2c_get_clientdata(client);
  766. cancel_delayed_work_sync(&chip->work);
  767. return 0;
  768. }
  769. #if defined CONFIG_PM_SLEEP
  770. static int sbs_suspend(struct device *dev)
  771. {
  772. struct i2c_client *client = to_i2c_client(dev);
  773. struct sbs_info *chip = i2c_get_clientdata(client);
  774. if (chip->poll_time > 0)
  775. cancel_delayed_work_sync(&chip->work);
  776. /*
  777. * Write to manufacturer access with sleep command.
  778. * Support is manufacturer dependend, so ignore errors.
  779. */
  780. sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
  781. MANUFACTURER_ACCESS_SLEEP);
  782. return 0;
  783. }
  784. static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
  785. #define SBS_PM_OPS (&sbs_pm_ops)
  786. #else
  787. #define SBS_PM_OPS NULL
  788. #endif
  789. static const struct i2c_device_id sbs_id[] = {
  790. { "bq20z75", 0 },
  791. { "sbs-battery", 1 },
  792. {}
  793. };
  794. MODULE_DEVICE_TABLE(i2c, sbs_id);
  795. static const struct of_device_id sbs_dt_ids[] = {
  796. { .compatible = "sbs,sbs-battery" },
  797. { .compatible = "ti,bq20z75" },
  798. { }
  799. };
  800. MODULE_DEVICE_TABLE(of, sbs_dt_ids);
  801. static struct i2c_driver sbs_battery_driver = {
  802. .probe = sbs_probe,
  803. .remove = sbs_remove,
  804. .alert = sbs_alert,
  805. .id_table = sbs_id,
  806. .driver = {
  807. .name = "sbs-battery",
  808. .of_match_table = sbs_dt_ids,
  809. .pm = SBS_PM_OPS,
  810. },
  811. };
  812. module_i2c_driver(sbs_battery_driver);
  813. MODULE_DESCRIPTION("SBS battery monitor driver");
  814. MODULE_LICENSE("GPL");
  815. module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
  816. MODULE_PARM_DESC(force_load,
  817. "Attempt to load the driver even if no battery is connected");