sbs-battery.c 21 KB

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