sbs-battery.c 23 KB

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