sbs-battery.c 25 KB

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