sbs-battery.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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/power/sbs-battery.h>
  31. enum {
  32. REG_MANUFACTURER_DATA,
  33. REG_TEMPERATURE,
  34. REG_VOLTAGE,
  35. REG_CURRENT,
  36. REG_CAPACITY,
  37. REG_TIME_TO_EMPTY,
  38. REG_TIME_TO_FULL,
  39. REG_STATUS,
  40. REG_CYCLE_COUNT,
  41. REG_SERIAL_NUMBER,
  42. REG_REMAINING_CAPACITY,
  43. REG_REMAINING_CAPACITY_CHARGE,
  44. REG_FULL_CHARGE_CAPACITY,
  45. REG_FULL_CHARGE_CAPACITY_CHARGE,
  46. REG_DESIGN_CAPACITY,
  47. REG_DESIGN_CAPACITY_CHARGE,
  48. REG_DESIGN_VOLTAGE_MIN,
  49. REG_DESIGN_VOLTAGE_MAX,
  50. REG_MANUFACTURER,
  51. REG_MODEL_NAME,
  52. };
  53. /* Battery Mode defines */
  54. #define BATTERY_MODE_OFFSET 0x03
  55. #define BATTERY_MODE_MASK 0x8000
  56. enum sbs_battery_mode {
  57. BATTERY_MODE_AMPS,
  58. BATTERY_MODE_WATTS
  59. };
  60. /* manufacturer access defines */
  61. #define MANUFACTURER_ACCESS_STATUS 0x0006
  62. #define MANUFACTURER_ACCESS_SLEEP 0x0011
  63. /* battery status value bits */
  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_CYCLE_COUNT] =
  105. SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
  106. [REG_DESIGN_CAPACITY] =
  107. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
  108. [REG_DESIGN_CAPACITY_CHARGE] =
  109. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
  110. [REG_DESIGN_VOLTAGE_MIN] =
  111. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
  112. [REG_DESIGN_VOLTAGE_MAX] =
  113. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
  114. [REG_SERIAL_NUMBER] =
  115. SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
  116. /* Properties of type `const char *' */
  117. [REG_MANUFACTURER] =
  118. SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
  119. [REG_MODEL_NAME] =
  120. SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
  121. };
  122. static enum power_supply_property sbs_properties[] = {
  123. POWER_SUPPLY_PROP_STATUS,
  124. POWER_SUPPLY_PROP_HEALTH,
  125. POWER_SUPPLY_PROP_PRESENT,
  126. POWER_SUPPLY_PROP_TECHNOLOGY,
  127. POWER_SUPPLY_PROP_CYCLE_COUNT,
  128. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  129. POWER_SUPPLY_PROP_CURRENT_NOW,
  130. POWER_SUPPLY_PROP_CAPACITY,
  131. POWER_SUPPLY_PROP_TEMP,
  132. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  133. POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  134. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  135. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  136. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  137. POWER_SUPPLY_PROP_ENERGY_NOW,
  138. POWER_SUPPLY_PROP_ENERGY_FULL,
  139. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  140. POWER_SUPPLY_PROP_CHARGE_NOW,
  141. POWER_SUPPLY_PROP_CHARGE_FULL,
  142. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  143. /* Properties of type `const char *' */
  144. POWER_SUPPLY_PROP_MANUFACTURER,
  145. POWER_SUPPLY_PROP_MODEL_NAME
  146. };
  147. struct sbs_info {
  148. struct i2c_client *client;
  149. struct power_supply power_supply;
  150. struct sbs_platform_data *pdata;
  151. bool is_present;
  152. bool gpio_detect;
  153. bool enable_detection;
  154. int irq;
  155. int last_state;
  156. int poll_time;
  157. struct delayed_work work;
  158. int ignore_changes;
  159. };
  160. static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
  161. static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
  162. static int sbs_read_word_data(struct i2c_client *client, u8 address)
  163. {
  164. struct sbs_info *chip = i2c_get_clientdata(client);
  165. s32 ret = 0;
  166. int retries = 1;
  167. if (chip->pdata)
  168. retries = max(chip->pdata->i2c_retry_count + 1, 1);
  169. while (retries > 0) {
  170. ret = i2c_smbus_read_word_data(client, address);
  171. if (ret >= 0)
  172. break;
  173. retries--;
  174. }
  175. if (ret < 0) {
  176. dev_dbg(&client->dev,
  177. "%s: i2c read at address 0x%x failed\n",
  178. __func__, address);
  179. return ret;
  180. }
  181. return le16_to_cpu(ret);
  182. }
  183. static int sbs_read_string_data(struct i2c_client *client, u8 address,
  184. char *values)
  185. {
  186. struct sbs_info *chip = i2c_get_clientdata(client);
  187. s32 ret = 0, block_length = 0;
  188. int retries_length = 1, retries_block = 1;
  189. u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
  190. if (chip->pdata) {
  191. retries_length = max(chip->pdata->i2c_retry_count + 1, 1);
  192. retries_block = max(chip->pdata->i2c_retry_count + 1, 1);
  193. }
  194. /* Adapter needs to support these two functions */
  195. if (!i2c_check_functionality(client->adapter,
  196. I2C_FUNC_SMBUS_BYTE_DATA |
  197. I2C_FUNC_SMBUS_I2C_BLOCK)){
  198. return -ENODEV;
  199. }
  200. /* Get the length of block data */
  201. while (retries_length > 0) {
  202. ret = i2c_smbus_read_byte_data(client, address);
  203. if (ret >= 0)
  204. break;
  205. retries_length--;
  206. }
  207. if (ret < 0) {
  208. dev_dbg(&client->dev,
  209. "%s: i2c read at address 0x%x failed\n",
  210. __func__, address);
  211. return ret;
  212. }
  213. /* block_length does not include NULL terminator */
  214. block_length = ret;
  215. if (block_length > I2C_SMBUS_BLOCK_MAX) {
  216. dev_err(&client->dev,
  217. "%s: Returned block_length is longer than 0x%x\n",
  218. __func__, I2C_SMBUS_BLOCK_MAX);
  219. return -EINVAL;
  220. }
  221. /* Get the block data */
  222. while (retries_block > 0) {
  223. ret = i2c_smbus_read_i2c_block_data(
  224. client, address,
  225. block_length + 1, block_buffer);
  226. if (ret >= 0)
  227. break;
  228. retries_block--;
  229. }
  230. if (ret < 0) {
  231. dev_dbg(&client->dev,
  232. "%s: i2c read at address 0x%x failed\n",
  233. __func__, address);
  234. return ret;
  235. }
  236. /* block_buffer[0] == block_length */
  237. memcpy(values, block_buffer + 1, block_length);
  238. values[block_length] = '\0';
  239. return le16_to_cpu(ret);
  240. }
  241. static int sbs_write_word_data(struct i2c_client *client, u8 address,
  242. u16 value)
  243. {
  244. struct sbs_info *chip = i2c_get_clientdata(client);
  245. s32 ret = 0;
  246. int retries = 1;
  247. if (chip->pdata)
  248. retries = max(chip->pdata->i2c_retry_count + 1, 1);
  249. while (retries > 0) {
  250. ret = i2c_smbus_write_word_data(client, address,
  251. le16_to_cpu(value));
  252. if (ret >= 0)
  253. break;
  254. retries--;
  255. }
  256. if (ret < 0) {
  257. dev_dbg(&client->dev,
  258. "%s: i2c write to address 0x%x failed\n",
  259. __func__, address);
  260. return ret;
  261. }
  262. return 0;
  263. }
  264. static int sbs_get_battery_presence_and_health(
  265. struct i2c_client *client, enum power_supply_property psp,
  266. union power_supply_propval *val)
  267. {
  268. s32 ret;
  269. struct sbs_info *chip = i2c_get_clientdata(client);
  270. if (psp == POWER_SUPPLY_PROP_PRESENT &&
  271. chip->gpio_detect) {
  272. ret = gpio_get_value(chip->pdata->battery_detect);
  273. if (ret == chip->pdata->battery_detect_present)
  274. val->intval = 1;
  275. else
  276. val->intval = 0;
  277. chip->is_present = val->intval;
  278. return ret;
  279. }
  280. /* Write to ManufacturerAccess with
  281. * ManufacturerAccess command and then
  282. * read the status */
  283. ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
  284. MANUFACTURER_ACCESS_STATUS);
  285. if (ret < 0) {
  286. if (psp == POWER_SUPPLY_PROP_PRESENT)
  287. val->intval = 0; /* battery removed */
  288. return ret;
  289. }
  290. ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
  291. if (ret < 0)
  292. return ret;
  293. if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
  294. ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
  295. val->intval = 0;
  296. return 0;
  297. }
  298. /* Mask the upper nibble of 2nd byte and
  299. * lower byte of response then
  300. * shift the result by 8 to get status*/
  301. ret &= 0x0F00;
  302. ret >>= 8;
  303. if (psp == POWER_SUPPLY_PROP_PRESENT) {
  304. if (ret == 0x0F)
  305. /* battery removed */
  306. val->intval = 0;
  307. else
  308. val->intval = 1;
  309. } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
  310. if (ret == 0x09)
  311. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  312. else if (ret == 0x0B)
  313. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  314. else if (ret == 0x0C)
  315. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  316. else
  317. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  318. }
  319. return 0;
  320. }
  321. static int sbs_get_battery_property(struct i2c_client *client,
  322. int reg_offset, enum power_supply_property psp,
  323. union power_supply_propval *val)
  324. {
  325. struct sbs_info *chip = i2c_get_clientdata(client);
  326. s32 ret;
  327. ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
  328. if (ret < 0)
  329. return ret;
  330. /* returned values are 16 bit */
  331. if (sbs_data[reg_offset].min_value < 0)
  332. ret = (s16)ret;
  333. if (ret >= sbs_data[reg_offset].min_value &&
  334. ret <= sbs_data[reg_offset].max_value) {
  335. val->intval = ret;
  336. if (psp != POWER_SUPPLY_PROP_STATUS)
  337. return 0;
  338. if (ret & BATTERY_FULL_CHARGED)
  339. val->intval = POWER_SUPPLY_STATUS_FULL;
  340. else if (ret & BATTERY_FULL_DISCHARGED)
  341. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  342. else if (ret & BATTERY_DISCHARGING)
  343. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  344. else
  345. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  346. if (chip->poll_time == 0)
  347. chip->last_state = val->intval;
  348. else if (chip->last_state != val->intval) {
  349. cancel_delayed_work_sync(&chip->work);
  350. power_supply_changed(&chip->power_supply);
  351. chip->poll_time = 0;
  352. }
  353. } else {
  354. if (psp == POWER_SUPPLY_PROP_STATUS)
  355. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  356. else
  357. val->intval = 0;
  358. }
  359. return 0;
  360. }
  361. static int sbs_get_battery_string_property(struct i2c_client *client,
  362. int reg_offset, enum power_supply_property psp, char *val)
  363. {
  364. s32 ret;
  365. ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
  366. if (ret < 0)
  367. return ret;
  368. return 0;
  369. }
  370. static void sbs_unit_adjustment(struct i2c_client *client,
  371. enum power_supply_property psp, union power_supply_propval *val)
  372. {
  373. #define BASE_UNIT_CONVERSION 1000
  374. #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
  375. #define TIME_UNIT_CONVERSION 60
  376. #define TEMP_KELVIN_TO_CELSIUS 2731
  377. switch (psp) {
  378. case POWER_SUPPLY_PROP_ENERGY_NOW:
  379. case POWER_SUPPLY_PROP_ENERGY_FULL:
  380. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  381. /* sbs provides energy in units of 10mWh.
  382. * Convert to µWh
  383. */
  384. val->intval *= BATTERY_MODE_CAP_MULT_WATT;
  385. break;
  386. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  387. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  388. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  389. case POWER_SUPPLY_PROP_CURRENT_NOW:
  390. case POWER_SUPPLY_PROP_CHARGE_NOW:
  391. case POWER_SUPPLY_PROP_CHARGE_FULL:
  392. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  393. val->intval *= BASE_UNIT_CONVERSION;
  394. break;
  395. case POWER_SUPPLY_PROP_TEMP:
  396. /* sbs provides battery temperature in 0.1K
  397. * so convert it to 0.1°C
  398. */
  399. val->intval -= TEMP_KELVIN_TO_CELSIUS;
  400. break;
  401. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  402. case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
  403. /* sbs provides time to empty and time to full in minutes.
  404. * Convert to seconds
  405. */
  406. val->intval *= TIME_UNIT_CONVERSION;
  407. break;
  408. default:
  409. dev_dbg(&client->dev,
  410. "%s: no need for unit conversion %d\n", __func__, psp);
  411. }
  412. }
  413. static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
  414. enum sbs_battery_mode mode)
  415. {
  416. int ret, original_val;
  417. original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
  418. if (original_val < 0)
  419. return original_val;
  420. if ((original_val & BATTERY_MODE_MASK) == mode)
  421. return mode;
  422. if (mode == BATTERY_MODE_AMPS)
  423. ret = original_val & ~BATTERY_MODE_MASK;
  424. else
  425. ret = original_val | BATTERY_MODE_MASK;
  426. ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
  427. if (ret < 0)
  428. return ret;
  429. return original_val & BATTERY_MODE_MASK;
  430. }
  431. static int sbs_get_battery_capacity(struct i2c_client *client,
  432. int reg_offset, enum power_supply_property psp,
  433. union power_supply_propval *val)
  434. {
  435. s32 ret;
  436. enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
  437. if (power_supply_is_amp_property(psp))
  438. mode = BATTERY_MODE_AMPS;
  439. mode = sbs_set_battery_mode(client, mode);
  440. if (mode < 0)
  441. return mode;
  442. ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
  443. if (ret < 0)
  444. return ret;
  445. if (psp == POWER_SUPPLY_PROP_CAPACITY) {
  446. /* sbs spec says that this can be >100 %
  447. * even if max value is 100 % */
  448. val->intval = min(ret, 100);
  449. } else
  450. val->intval = ret;
  451. ret = sbs_set_battery_mode(client, mode);
  452. if (ret < 0)
  453. return ret;
  454. return 0;
  455. }
  456. static char sbs_serial[5];
  457. static int sbs_get_battery_serial_number(struct i2c_client *client,
  458. union power_supply_propval *val)
  459. {
  460. int ret;
  461. ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
  462. if (ret < 0)
  463. return ret;
  464. ret = sprintf(sbs_serial, "%04x", ret);
  465. val->strval = sbs_serial;
  466. return 0;
  467. }
  468. static int sbs_get_property_index(struct i2c_client *client,
  469. enum power_supply_property psp)
  470. {
  471. int count;
  472. for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
  473. if (psp == sbs_data[count].psp)
  474. return count;
  475. dev_warn(&client->dev,
  476. "%s: Invalid Property - %d\n", __func__, psp);
  477. return -EINVAL;
  478. }
  479. static int sbs_get_property(struct power_supply *psy,
  480. enum power_supply_property psp,
  481. union power_supply_propval *val)
  482. {
  483. int ret = 0;
  484. struct sbs_info *chip = container_of(psy,
  485. struct sbs_info, power_supply);
  486. struct i2c_client *client = chip->client;
  487. switch (psp) {
  488. case POWER_SUPPLY_PROP_PRESENT:
  489. case POWER_SUPPLY_PROP_HEALTH:
  490. ret = sbs_get_battery_presence_and_health(client, psp, val);
  491. if (psp == POWER_SUPPLY_PROP_PRESENT)
  492. return 0;
  493. break;
  494. case POWER_SUPPLY_PROP_TECHNOLOGY:
  495. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  496. goto done; /* don't trigger power_supply_changed()! */
  497. case POWER_SUPPLY_PROP_ENERGY_NOW:
  498. case POWER_SUPPLY_PROP_ENERGY_FULL:
  499. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  500. case POWER_SUPPLY_PROP_CHARGE_NOW:
  501. case POWER_SUPPLY_PROP_CHARGE_FULL:
  502. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  503. case POWER_SUPPLY_PROP_CAPACITY:
  504. ret = sbs_get_property_index(client, psp);
  505. if (ret < 0)
  506. break;
  507. ret = sbs_get_battery_capacity(client, ret, psp, val);
  508. break;
  509. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  510. ret = sbs_get_battery_serial_number(client, val);
  511. break;
  512. case POWER_SUPPLY_PROP_STATUS:
  513. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  514. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  515. case POWER_SUPPLY_PROP_CURRENT_NOW:
  516. case POWER_SUPPLY_PROP_TEMP:
  517. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  518. case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
  519. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  520. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  521. ret = sbs_get_property_index(client, psp);
  522. if (ret < 0)
  523. break;
  524. ret = sbs_get_battery_property(client, ret, psp, val);
  525. break;
  526. case POWER_SUPPLY_PROP_MODEL_NAME:
  527. ret = sbs_get_property_index(client, psp);
  528. if (ret < 0)
  529. break;
  530. ret = sbs_get_battery_string_property(client, ret, psp,
  531. model_name);
  532. val->strval = model_name;
  533. break;
  534. case POWER_SUPPLY_PROP_MANUFACTURER:
  535. ret = sbs_get_property_index(client, psp);
  536. if (ret < 0)
  537. break;
  538. ret = sbs_get_battery_string_property(client, ret, psp,
  539. manufacturer);
  540. val->strval = manufacturer;
  541. break;
  542. default:
  543. dev_err(&client->dev,
  544. "%s: INVALID property\n", __func__);
  545. return -EINVAL;
  546. }
  547. if (!chip->enable_detection)
  548. goto done;
  549. if (!chip->gpio_detect &&
  550. chip->is_present != (ret >= 0)) {
  551. chip->is_present = (ret >= 0);
  552. power_supply_changed(&chip->power_supply);
  553. }
  554. done:
  555. if (!ret) {
  556. /* Convert units to match requirements for power supply class */
  557. sbs_unit_adjustment(client, psp, val);
  558. }
  559. dev_dbg(&client->dev,
  560. "%s: property = %d, value = %x\n", __func__, psp, val->intval);
  561. if (ret && chip->is_present)
  562. return ret;
  563. /* battery not present, so return NODATA for properties */
  564. if (ret)
  565. return -ENODATA;
  566. return 0;
  567. }
  568. static irqreturn_t sbs_irq(int irq, void *devid)
  569. {
  570. struct power_supply *battery = devid;
  571. power_supply_changed(battery);
  572. return IRQ_HANDLED;
  573. }
  574. static void sbs_external_power_changed(struct power_supply *psy)
  575. {
  576. struct sbs_info *chip;
  577. chip = container_of(psy, struct sbs_info, power_supply);
  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 int sbs_probe(struct i2c_client *client,
  678. const struct i2c_device_id *id)
  679. {
  680. struct sbs_info *chip;
  681. struct sbs_platform_data *pdata = client->dev.platform_data;
  682. int rc;
  683. int irq;
  684. char *name;
  685. name = kasprintf(GFP_KERNEL, "sbs-%s", dev_name(&client->dev));
  686. if (!name) {
  687. dev_err(&client->dev, "Failed to allocate device name\n");
  688. return -ENOMEM;
  689. }
  690. chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL);
  691. if (!chip) {
  692. rc = -ENOMEM;
  693. goto exit_free_name;
  694. }
  695. chip->client = client;
  696. chip->enable_detection = false;
  697. chip->gpio_detect = false;
  698. chip->power_supply.name = name;
  699. chip->power_supply.type = POWER_SUPPLY_TYPE_BATTERY;
  700. chip->power_supply.properties = sbs_properties;
  701. chip->power_supply.num_properties = ARRAY_SIZE(sbs_properties);
  702. chip->power_supply.get_property = sbs_get_property;
  703. chip->power_supply.of_node = client->dev.of_node;
  704. /* ignore first notification of external change, it is generated
  705. * from the power_supply_register call back
  706. */
  707. chip->ignore_changes = 1;
  708. chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
  709. chip->power_supply.external_power_changed = sbs_external_power_changed;
  710. pdata = sbs_of_populate_pdata(client);
  711. if (pdata) {
  712. chip->gpio_detect = gpio_is_valid(pdata->battery_detect);
  713. chip->pdata = pdata;
  714. }
  715. i2c_set_clientdata(client, chip);
  716. if (!chip->gpio_detect)
  717. goto skip_gpio;
  718. rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
  719. if (rc) {
  720. dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
  721. chip->gpio_detect = false;
  722. goto skip_gpio;
  723. }
  724. rc = gpio_direction_input(pdata->battery_detect);
  725. if (rc) {
  726. dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
  727. gpio_free(pdata->battery_detect);
  728. chip->gpio_detect = false;
  729. goto skip_gpio;
  730. }
  731. irq = gpio_to_irq(pdata->battery_detect);
  732. if (irq <= 0) {
  733. dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
  734. gpio_free(pdata->battery_detect);
  735. chip->gpio_detect = false;
  736. goto skip_gpio;
  737. }
  738. rc = request_irq(irq, sbs_irq,
  739. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  740. dev_name(&client->dev), &chip->power_supply);
  741. if (rc) {
  742. dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
  743. gpio_free(pdata->battery_detect);
  744. chip->gpio_detect = false;
  745. goto skip_gpio;
  746. }
  747. chip->irq = irq;
  748. skip_gpio:
  749. /*
  750. * Before we register, we need to make sure we can actually talk
  751. * to the battery.
  752. */
  753. rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
  754. if (rc < 0) {
  755. dev_err(&client->dev, "%s: Failed to get device status\n",
  756. __func__);
  757. goto exit_psupply;
  758. }
  759. rc = power_supply_register(&client->dev, &chip->power_supply);
  760. if (rc) {
  761. dev_err(&client->dev,
  762. "%s: Failed to register power supply\n", __func__);
  763. goto exit_psupply;
  764. }
  765. dev_info(&client->dev,
  766. "%s: battery gas gauge device registered\n", client->name);
  767. INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
  768. chip->enable_detection = true;
  769. return 0;
  770. exit_psupply:
  771. if (chip->irq)
  772. free_irq(chip->irq, &chip->power_supply);
  773. if (chip->gpio_detect)
  774. gpio_free(pdata->battery_detect);
  775. kfree(chip);
  776. exit_free_name:
  777. kfree(name);
  778. return rc;
  779. }
  780. static int sbs_remove(struct i2c_client *client)
  781. {
  782. struct sbs_info *chip = i2c_get_clientdata(client);
  783. if (chip->irq)
  784. free_irq(chip->irq, &chip->power_supply);
  785. if (chip->gpio_detect)
  786. gpio_free(chip->pdata->battery_detect);
  787. power_supply_unregister(&chip->power_supply);
  788. cancel_delayed_work_sync(&chip->work);
  789. kfree(chip->power_supply.name);
  790. kfree(chip);
  791. chip = NULL;
  792. return 0;
  793. }
  794. #if defined CONFIG_PM_SLEEP
  795. static int sbs_suspend(struct device *dev)
  796. {
  797. struct i2c_client *client = to_i2c_client(dev);
  798. struct sbs_info *chip = i2c_get_clientdata(client);
  799. s32 ret;
  800. if (chip->poll_time > 0)
  801. cancel_delayed_work_sync(&chip->work);
  802. /* write to manufacturer access with sleep command */
  803. ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
  804. MANUFACTURER_ACCESS_SLEEP);
  805. if (chip->is_present && ret < 0)
  806. return ret;
  807. return 0;
  808. }
  809. static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
  810. #define SBS_PM_OPS (&sbs_pm_ops)
  811. #else
  812. #define SBS_PM_OPS NULL
  813. #endif
  814. static const struct i2c_device_id sbs_id[] = {
  815. { "bq20z75", 0 },
  816. { "sbs-battery", 1 },
  817. {}
  818. };
  819. MODULE_DEVICE_TABLE(i2c, sbs_id);
  820. static struct i2c_driver sbs_battery_driver = {
  821. .probe = sbs_probe,
  822. .remove = sbs_remove,
  823. .id_table = sbs_id,
  824. .driver = {
  825. .name = "sbs-battery",
  826. .of_match_table = of_match_ptr(sbs_dt_ids),
  827. .pm = SBS_PM_OPS,
  828. },
  829. };
  830. module_i2c_driver(sbs_battery_driver);
  831. MODULE_DESCRIPTION("SBS battery monitor driver");
  832. MODULE_LICENSE("GPL");