bq20z75.c 20 KB

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