sbs-battery.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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/power/sbs-battery.h>
  30. enum {
  31. REG_MANUFACTURER_DATA,
  32. REG_TEMPERATURE,
  33. REG_VOLTAGE,
  34. REG_CURRENT,
  35. REG_CAPACITY,
  36. REG_TIME_TO_EMPTY,
  37. REG_TIME_TO_FULL,
  38. REG_STATUS,
  39. REG_CYCLE_COUNT,
  40. REG_SERIAL_NUMBER,
  41. REG_REMAINING_CAPACITY,
  42. REG_REMAINING_CAPACITY_CHARGE,
  43. REG_FULL_CHARGE_CAPACITY,
  44. REG_FULL_CHARGE_CAPACITY_CHARGE,
  45. REG_DESIGN_CAPACITY,
  46. REG_DESIGN_CAPACITY_CHARGE,
  47. REG_DESIGN_VOLTAGE,
  48. };
  49. /* Battery Mode defines */
  50. #define BATTERY_MODE_OFFSET 0x03
  51. #define BATTERY_MODE_MASK 0x8000
  52. enum sbs_battery_mode {
  53. BATTERY_MODE_AMPS,
  54. BATTERY_MODE_WATTS
  55. };
  56. /* manufacturer access defines */
  57. #define MANUFACTURER_ACCESS_STATUS 0x0006
  58. #define MANUFACTURER_ACCESS_SLEEP 0x0011
  59. /* battery status value bits */
  60. #define BATTERY_DISCHARGING 0x40
  61. #define BATTERY_FULL_CHARGED 0x20
  62. #define BATTERY_FULL_DISCHARGED 0x10
  63. #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
  64. .psp = _psp, \
  65. .addr = _addr, \
  66. .min_value = _min_value, \
  67. .max_value = _max_value, \
  68. }
  69. static const struct chip_data {
  70. enum power_supply_property psp;
  71. u8 addr;
  72. int min_value;
  73. int max_value;
  74. } sbs_data[] = {
  75. [REG_MANUFACTURER_DATA] =
  76. SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
  77. [REG_TEMPERATURE] =
  78. SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
  79. [REG_VOLTAGE] =
  80. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
  81. [REG_CURRENT] =
  82. SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
  83. [REG_CAPACITY] =
  84. SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0E, 0, 100),
  85. [REG_REMAINING_CAPACITY] =
  86. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
  87. [REG_REMAINING_CAPACITY_CHARGE] =
  88. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
  89. [REG_FULL_CHARGE_CAPACITY] =
  90. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
  91. [REG_FULL_CHARGE_CAPACITY_CHARGE] =
  92. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
  93. [REG_TIME_TO_EMPTY] =
  94. SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
  95. [REG_TIME_TO_FULL] =
  96. SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
  97. [REG_STATUS] =
  98. SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
  99. [REG_CYCLE_COUNT] =
  100. SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
  101. [REG_DESIGN_CAPACITY] =
  102. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
  103. [REG_DESIGN_CAPACITY_CHARGE] =
  104. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
  105. [REG_DESIGN_VOLTAGE] =
  106. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
  107. [REG_SERIAL_NUMBER] =
  108. SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
  109. };
  110. static enum power_supply_property sbs_properties[] = {
  111. POWER_SUPPLY_PROP_STATUS,
  112. POWER_SUPPLY_PROP_HEALTH,
  113. POWER_SUPPLY_PROP_PRESENT,
  114. POWER_SUPPLY_PROP_TECHNOLOGY,
  115. POWER_SUPPLY_PROP_CYCLE_COUNT,
  116. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  117. POWER_SUPPLY_PROP_CURRENT_NOW,
  118. POWER_SUPPLY_PROP_CAPACITY,
  119. POWER_SUPPLY_PROP_TEMP,
  120. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  121. POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  122. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  123. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  124. POWER_SUPPLY_PROP_ENERGY_NOW,
  125. POWER_SUPPLY_PROP_ENERGY_FULL,
  126. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  127. POWER_SUPPLY_PROP_CHARGE_NOW,
  128. POWER_SUPPLY_PROP_CHARGE_FULL,
  129. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  130. };
  131. struct sbs_info {
  132. struct i2c_client *client;
  133. struct power_supply power_supply;
  134. struct sbs_platform_data *pdata;
  135. bool is_present;
  136. bool gpio_detect;
  137. bool enable_detection;
  138. int irq;
  139. int last_state;
  140. int poll_time;
  141. struct delayed_work work;
  142. int ignore_changes;
  143. };
  144. static int sbs_read_word_data(struct i2c_client *client, u8 address)
  145. {
  146. struct sbs_info *chip = i2c_get_clientdata(client);
  147. s32 ret = 0;
  148. int retries = 1;
  149. if (chip->pdata)
  150. retries = max(chip->pdata->i2c_retry_count + 1, 1);
  151. while (retries > 0) {
  152. ret = i2c_smbus_read_word_data(client, address);
  153. if (ret >= 0)
  154. break;
  155. retries--;
  156. }
  157. if (ret < 0) {
  158. dev_dbg(&client->dev,
  159. "%s: i2c read at address 0x%x failed\n",
  160. __func__, address);
  161. return ret;
  162. }
  163. return le16_to_cpu(ret);
  164. }
  165. static int sbs_write_word_data(struct i2c_client *client, u8 address,
  166. u16 value)
  167. {
  168. struct sbs_info *chip = i2c_get_clientdata(client);
  169. s32 ret = 0;
  170. int retries = 1;
  171. if (chip->pdata)
  172. retries = max(chip->pdata->i2c_retry_count + 1, 1);
  173. while (retries > 0) {
  174. ret = i2c_smbus_write_word_data(client, address,
  175. le16_to_cpu(value));
  176. if (ret >= 0)
  177. break;
  178. retries--;
  179. }
  180. if (ret < 0) {
  181. dev_dbg(&client->dev,
  182. "%s: i2c write to address 0x%x failed\n",
  183. __func__, address);
  184. return ret;
  185. }
  186. return 0;
  187. }
  188. static int sbs_get_battery_presence_and_health(
  189. struct i2c_client *client, enum power_supply_property psp,
  190. union power_supply_propval *val)
  191. {
  192. s32 ret;
  193. struct sbs_info *chip = i2c_get_clientdata(client);
  194. if (psp == POWER_SUPPLY_PROP_PRESENT &&
  195. chip->gpio_detect) {
  196. ret = gpio_get_value(chip->pdata->battery_detect);
  197. if (ret == chip->pdata->battery_detect_present)
  198. val->intval = 1;
  199. else
  200. val->intval = 0;
  201. chip->is_present = val->intval;
  202. return ret;
  203. }
  204. /* Write to ManufacturerAccess with
  205. * ManufacturerAccess command and then
  206. * read the status */
  207. ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
  208. MANUFACTURER_ACCESS_STATUS);
  209. if (ret < 0) {
  210. if (psp == POWER_SUPPLY_PROP_PRESENT)
  211. val->intval = 0; /* battery removed */
  212. return ret;
  213. }
  214. ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
  215. if (ret < 0)
  216. return ret;
  217. if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
  218. ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
  219. val->intval = 0;
  220. return 0;
  221. }
  222. /* Mask the upper nibble of 2nd byte and
  223. * lower byte of response then
  224. * shift the result by 8 to get status*/
  225. ret &= 0x0F00;
  226. ret >>= 8;
  227. if (psp == POWER_SUPPLY_PROP_PRESENT) {
  228. if (ret == 0x0F)
  229. /* battery removed */
  230. val->intval = 0;
  231. else
  232. val->intval = 1;
  233. } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
  234. if (ret == 0x09)
  235. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  236. else if (ret == 0x0B)
  237. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  238. else if (ret == 0x0C)
  239. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  240. else
  241. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  242. }
  243. return 0;
  244. }
  245. static int sbs_get_battery_property(struct i2c_client *client,
  246. int reg_offset, enum power_supply_property psp,
  247. union power_supply_propval *val)
  248. {
  249. struct sbs_info *chip = i2c_get_clientdata(client);
  250. s32 ret;
  251. ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
  252. if (ret < 0)
  253. return ret;
  254. /* returned values are 16 bit */
  255. if (sbs_data[reg_offset].min_value < 0)
  256. ret = (s16)ret;
  257. if (ret >= sbs_data[reg_offset].min_value &&
  258. ret <= sbs_data[reg_offset].max_value) {
  259. val->intval = ret;
  260. if (psp != POWER_SUPPLY_PROP_STATUS)
  261. return 0;
  262. if (ret & BATTERY_FULL_CHARGED)
  263. val->intval = POWER_SUPPLY_STATUS_FULL;
  264. else if (ret & BATTERY_FULL_DISCHARGED)
  265. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  266. else if (ret & BATTERY_DISCHARGING)
  267. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  268. else
  269. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  270. if (chip->poll_time == 0)
  271. chip->last_state = val->intval;
  272. else if (chip->last_state != val->intval) {
  273. cancel_delayed_work_sync(&chip->work);
  274. power_supply_changed(&chip->power_supply);
  275. chip->poll_time = 0;
  276. }
  277. } else {
  278. if (psp == POWER_SUPPLY_PROP_STATUS)
  279. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  280. else
  281. val->intval = 0;
  282. }
  283. return 0;
  284. }
  285. static void sbs_unit_adjustment(struct i2c_client *client,
  286. enum power_supply_property psp, union power_supply_propval *val)
  287. {
  288. #define BASE_UNIT_CONVERSION 1000
  289. #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
  290. #define TIME_UNIT_CONVERSION 60
  291. #define TEMP_KELVIN_TO_CELSIUS 2731
  292. switch (psp) {
  293. case POWER_SUPPLY_PROP_ENERGY_NOW:
  294. case POWER_SUPPLY_PROP_ENERGY_FULL:
  295. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  296. /* sbs provides energy in units of 10mWh.
  297. * Convert to µWh
  298. */
  299. val->intval *= BATTERY_MODE_CAP_MULT_WATT;
  300. break;
  301. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  302. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  303. case POWER_SUPPLY_PROP_CURRENT_NOW:
  304. case POWER_SUPPLY_PROP_CHARGE_NOW:
  305. case POWER_SUPPLY_PROP_CHARGE_FULL:
  306. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  307. val->intval *= BASE_UNIT_CONVERSION;
  308. break;
  309. case POWER_SUPPLY_PROP_TEMP:
  310. /* sbs provides battery temperature in 0.1K
  311. * so convert it to 0.1°C
  312. */
  313. val->intval -= TEMP_KELVIN_TO_CELSIUS;
  314. break;
  315. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  316. case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
  317. /* sbs provides time to empty and time to full in minutes.
  318. * Convert to seconds
  319. */
  320. val->intval *= TIME_UNIT_CONVERSION;
  321. break;
  322. default:
  323. dev_dbg(&client->dev,
  324. "%s: no need for unit conversion %d\n", __func__, psp);
  325. }
  326. }
  327. static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
  328. enum sbs_battery_mode mode)
  329. {
  330. int ret, original_val;
  331. original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
  332. if (original_val < 0)
  333. return original_val;
  334. if ((original_val & BATTERY_MODE_MASK) == mode)
  335. return mode;
  336. if (mode == BATTERY_MODE_AMPS)
  337. ret = original_val & ~BATTERY_MODE_MASK;
  338. else
  339. ret = original_val | BATTERY_MODE_MASK;
  340. ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
  341. if (ret < 0)
  342. return ret;
  343. return original_val & BATTERY_MODE_MASK;
  344. }
  345. static int sbs_get_battery_capacity(struct i2c_client *client,
  346. int reg_offset, enum power_supply_property psp,
  347. union power_supply_propval *val)
  348. {
  349. s32 ret;
  350. enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
  351. if (power_supply_is_amp_property(psp))
  352. mode = BATTERY_MODE_AMPS;
  353. mode = sbs_set_battery_mode(client, mode);
  354. if (mode < 0)
  355. return mode;
  356. ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
  357. if (ret < 0)
  358. return ret;
  359. if (psp == POWER_SUPPLY_PROP_CAPACITY) {
  360. /* sbs spec says that this can be >100 %
  361. * even if max value is 100 % */
  362. val->intval = min(ret, 100);
  363. } else
  364. val->intval = ret;
  365. ret = sbs_set_battery_mode(client, mode);
  366. if (ret < 0)
  367. return ret;
  368. return 0;
  369. }
  370. static char sbs_serial[5];
  371. static int sbs_get_battery_serial_number(struct i2c_client *client,
  372. union power_supply_propval *val)
  373. {
  374. int ret;
  375. ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
  376. if (ret < 0)
  377. return ret;
  378. ret = sprintf(sbs_serial, "%04x", ret);
  379. val->strval = sbs_serial;
  380. return 0;
  381. }
  382. static int sbs_get_property_index(struct i2c_client *client,
  383. enum power_supply_property psp)
  384. {
  385. int count;
  386. for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
  387. if (psp == sbs_data[count].psp)
  388. return count;
  389. dev_warn(&client->dev,
  390. "%s: Invalid Property - %d\n", __func__, psp);
  391. return -EINVAL;
  392. }
  393. static int sbs_get_property(struct power_supply *psy,
  394. enum power_supply_property psp,
  395. union power_supply_propval *val)
  396. {
  397. int ret = 0;
  398. struct sbs_info *chip = container_of(psy,
  399. struct sbs_info, power_supply);
  400. struct i2c_client *client = chip->client;
  401. switch (psp) {
  402. case POWER_SUPPLY_PROP_PRESENT:
  403. case POWER_SUPPLY_PROP_HEALTH:
  404. ret = sbs_get_battery_presence_and_health(client, psp, val);
  405. if (psp == POWER_SUPPLY_PROP_PRESENT)
  406. return 0;
  407. break;
  408. case POWER_SUPPLY_PROP_TECHNOLOGY:
  409. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  410. break;
  411. case POWER_SUPPLY_PROP_ENERGY_NOW:
  412. case POWER_SUPPLY_PROP_ENERGY_FULL:
  413. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  414. case POWER_SUPPLY_PROP_CHARGE_NOW:
  415. case POWER_SUPPLY_PROP_CHARGE_FULL:
  416. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  417. case POWER_SUPPLY_PROP_CAPACITY:
  418. ret = sbs_get_property_index(client, psp);
  419. if (ret < 0)
  420. break;
  421. ret = sbs_get_battery_capacity(client, ret, psp, val);
  422. break;
  423. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  424. ret = sbs_get_battery_serial_number(client, val);
  425. break;
  426. case POWER_SUPPLY_PROP_STATUS:
  427. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  428. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  429. case POWER_SUPPLY_PROP_CURRENT_NOW:
  430. case POWER_SUPPLY_PROP_TEMP:
  431. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  432. case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
  433. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  434. ret = sbs_get_property_index(client, psp);
  435. if (ret < 0)
  436. break;
  437. ret = sbs_get_battery_property(client, ret, psp, val);
  438. break;
  439. default:
  440. dev_err(&client->dev,
  441. "%s: INVALID property\n", __func__);
  442. return -EINVAL;
  443. }
  444. if (!chip->enable_detection)
  445. goto done;
  446. if (!chip->gpio_detect &&
  447. chip->is_present != (ret >= 0)) {
  448. chip->is_present = (ret >= 0);
  449. power_supply_changed(&chip->power_supply);
  450. }
  451. done:
  452. if (!ret) {
  453. /* Convert units to match requirements for power supply class */
  454. sbs_unit_adjustment(client, psp, val);
  455. }
  456. dev_dbg(&client->dev,
  457. "%s: property = %d, value = %x\n", __func__, psp, val->intval);
  458. if (ret && chip->is_present)
  459. return ret;
  460. /* battery not present, so return NODATA for properties */
  461. if (ret)
  462. return -ENODATA;
  463. return 0;
  464. }
  465. static irqreturn_t sbs_irq(int irq, void *devid)
  466. {
  467. struct power_supply *battery = devid;
  468. power_supply_changed(battery);
  469. return IRQ_HANDLED;
  470. }
  471. static void sbs_external_power_changed(struct power_supply *psy)
  472. {
  473. struct sbs_info *chip;
  474. chip = container_of(psy, struct sbs_info, power_supply);
  475. if (chip->ignore_changes > 0) {
  476. chip->ignore_changes--;
  477. return;
  478. }
  479. /* cancel outstanding work */
  480. cancel_delayed_work_sync(&chip->work);
  481. schedule_delayed_work(&chip->work, HZ);
  482. chip->poll_time = chip->pdata->poll_retry_count;
  483. }
  484. static void sbs_delayed_work(struct work_struct *work)
  485. {
  486. struct sbs_info *chip;
  487. s32 ret;
  488. chip = container_of(work, struct sbs_info, work.work);
  489. ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
  490. /* if the read failed, give up on this work */
  491. if (ret < 0) {
  492. chip->poll_time = 0;
  493. return;
  494. }
  495. if (ret & BATTERY_FULL_CHARGED)
  496. ret = POWER_SUPPLY_STATUS_FULL;
  497. else if (ret & BATTERY_FULL_DISCHARGED)
  498. ret = POWER_SUPPLY_STATUS_NOT_CHARGING;
  499. else if (ret & BATTERY_DISCHARGING)
  500. ret = POWER_SUPPLY_STATUS_DISCHARGING;
  501. else
  502. ret = POWER_SUPPLY_STATUS_CHARGING;
  503. if (chip->last_state != ret) {
  504. chip->poll_time = 0;
  505. power_supply_changed(&chip->power_supply);
  506. return;
  507. }
  508. if (chip->poll_time > 0) {
  509. schedule_delayed_work(&chip->work, HZ);
  510. chip->poll_time--;
  511. return;
  512. }
  513. }
  514. #if defined(CONFIG_OF)
  515. #include <linux/of_device.h>
  516. #include <linux/of_gpio.h>
  517. static const struct of_device_id sbs_dt_ids[] = {
  518. { .compatible = "sbs,sbs-battery" },
  519. { .compatible = "ti,bq20z75" },
  520. { }
  521. };
  522. MODULE_DEVICE_TABLE(of, sbs_dt_ids);
  523. static struct sbs_platform_data *sbs_of_populate_pdata(
  524. struct i2c_client *client)
  525. {
  526. struct device_node *of_node = client->dev.of_node;
  527. struct sbs_platform_data *pdata = client->dev.platform_data;
  528. enum of_gpio_flags gpio_flags;
  529. int rc;
  530. u32 prop;
  531. /* verify this driver matches this device */
  532. if (!of_node)
  533. return NULL;
  534. /* if platform data is set, honor it */
  535. if (pdata)
  536. return pdata;
  537. /* first make sure at least one property is set, otherwise
  538. * it won't change behavior from running without pdata.
  539. */
  540. if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) &&
  541. !of_get_property(of_node, "sbs,poll-retry-count", NULL) &&
  542. !of_get_property(of_node, "sbs,battery-detect-gpios", NULL))
  543. goto of_out;
  544. pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data),
  545. GFP_KERNEL);
  546. if (!pdata)
  547. goto of_out;
  548. rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop);
  549. if (!rc)
  550. pdata->i2c_retry_count = prop;
  551. rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop);
  552. if (!rc)
  553. pdata->poll_retry_count = prop;
  554. if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) {
  555. pdata->battery_detect = -1;
  556. goto of_out;
  557. }
  558. pdata->battery_detect = of_get_named_gpio_flags(of_node,
  559. "sbs,battery-detect-gpios", 0, &gpio_flags);
  560. if (gpio_flags & OF_GPIO_ACTIVE_LOW)
  561. pdata->battery_detect_present = 0;
  562. else
  563. pdata->battery_detect_present = 1;
  564. of_out:
  565. return pdata;
  566. }
  567. #else
  568. #define sbs_dt_ids NULL
  569. static struct sbs_platform_data *sbs_of_populate_pdata(
  570. struct i2c_client *client)
  571. {
  572. return client->dev.platform_data;
  573. }
  574. #endif
  575. static int __devinit sbs_probe(struct i2c_client *client,
  576. const struct i2c_device_id *id)
  577. {
  578. struct sbs_info *chip;
  579. struct sbs_platform_data *pdata = client->dev.platform_data;
  580. int rc;
  581. int irq;
  582. char *name;
  583. name = kasprintf(GFP_KERNEL, "sbs-%s", dev_name(&client->dev));
  584. if (!name) {
  585. dev_err(&client->dev, "Failed to allocate device name\n");
  586. return -ENOMEM;
  587. }
  588. chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL);
  589. if (!chip) {
  590. rc = -ENOMEM;
  591. goto exit_free_name;
  592. }
  593. chip->client = client;
  594. chip->enable_detection = false;
  595. chip->gpio_detect = false;
  596. chip->power_supply.name = name;
  597. chip->power_supply.type = POWER_SUPPLY_TYPE_BATTERY;
  598. chip->power_supply.properties = sbs_properties;
  599. chip->power_supply.num_properties = ARRAY_SIZE(sbs_properties);
  600. chip->power_supply.get_property = sbs_get_property;
  601. /* ignore first notification of external change, it is generated
  602. * from the power_supply_register call back
  603. */
  604. chip->ignore_changes = 1;
  605. chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
  606. chip->power_supply.external_power_changed = sbs_external_power_changed;
  607. pdata = sbs_of_populate_pdata(client);
  608. if (pdata) {
  609. chip->gpio_detect = gpio_is_valid(pdata->battery_detect);
  610. chip->pdata = pdata;
  611. }
  612. i2c_set_clientdata(client, chip);
  613. if (!chip->gpio_detect)
  614. goto skip_gpio;
  615. rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
  616. if (rc) {
  617. dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
  618. chip->gpio_detect = false;
  619. goto skip_gpio;
  620. }
  621. rc = gpio_direction_input(pdata->battery_detect);
  622. if (rc) {
  623. dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
  624. gpio_free(pdata->battery_detect);
  625. chip->gpio_detect = false;
  626. goto skip_gpio;
  627. }
  628. irq = gpio_to_irq(pdata->battery_detect);
  629. if (irq <= 0) {
  630. dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
  631. gpio_free(pdata->battery_detect);
  632. chip->gpio_detect = false;
  633. goto skip_gpio;
  634. }
  635. rc = request_irq(irq, sbs_irq,
  636. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  637. dev_name(&client->dev), &chip->power_supply);
  638. if (rc) {
  639. dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
  640. gpio_free(pdata->battery_detect);
  641. chip->gpio_detect = false;
  642. goto skip_gpio;
  643. }
  644. chip->irq = irq;
  645. skip_gpio:
  646. rc = power_supply_register(&client->dev, &chip->power_supply);
  647. if (rc) {
  648. dev_err(&client->dev,
  649. "%s: Failed to register power supply\n", __func__);
  650. goto exit_psupply;
  651. }
  652. dev_info(&client->dev,
  653. "%s: battery gas gauge device registered\n", client->name);
  654. INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
  655. chip->enable_detection = true;
  656. return 0;
  657. exit_psupply:
  658. if (chip->irq)
  659. free_irq(chip->irq, &chip->power_supply);
  660. if (chip->gpio_detect)
  661. gpio_free(pdata->battery_detect);
  662. kfree(chip);
  663. exit_free_name:
  664. kfree(name);
  665. return rc;
  666. }
  667. static int __devexit sbs_remove(struct i2c_client *client)
  668. {
  669. struct sbs_info *chip = i2c_get_clientdata(client);
  670. if (chip->irq)
  671. free_irq(chip->irq, &chip->power_supply);
  672. if (chip->gpio_detect)
  673. gpio_free(chip->pdata->battery_detect);
  674. power_supply_unregister(&chip->power_supply);
  675. cancel_delayed_work_sync(&chip->work);
  676. kfree(chip->power_supply.name);
  677. kfree(chip);
  678. chip = NULL;
  679. return 0;
  680. }
  681. #if defined CONFIG_PM
  682. static int sbs_suspend(struct i2c_client *client,
  683. pm_message_t state)
  684. {
  685. struct sbs_info *chip = i2c_get_clientdata(client);
  686. s32 ret;
  687. if (chip->poll_time > 0)
  688. cancel_delayed_work_sync(&chip->work);
  689. /* write to manufacturer access with sleep command */
  690. ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
  691. MANUFACTURER_ACCESS_SLEEP);
  692. if (chip->is_present && ret < 0)
  693. return ret;
  694. return 0;
  695. }
  696. #else
  697. #define sbs_suspend NULL
  698. #endif
  699. /* any smbus transaction will wake up sbs */
  700. #define sbs_resume NULL
  701. static const struct i2c_device_id sbs_id[] = {
  702. { "bq20z75", 0 },
  703. { "sbs-battery", 1 },
  704. {}
  705. };
  706. MODULE_DEVICE_TABLE(i2c, sbs_id);
  707. static struct i2c_driver sbs_battery_driver = {
  708. .probe = sbs_probe,
  709. .remove = __devexit_p(sbs_remove),
  710. .suspend = sbs_suspend,
  711. .resume = sbs_resume,
  712. .id_table = sbs_id,
  713. .driver = {
  714. .name = "sbs-battery",
  715. .of_match_table = sbs_dt_ids,
  716. },
  717. };
  718. module_i2c_driver(sbs_battery_driver);
  719. MODULE_DESCRIPTION("SBS battery monitor driver");
  720. MODULE_LICENSE("GPL");