bq27x00_battery.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * BQ27x00 battery driver
  3. *
  4. * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
  5. * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
  6. * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
  7. * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
  8. *
  9. * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  10. *
  11. * This package is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18. *
  19. */
  20. /*
  21. * Datasheets:
  22. * http://focus.ti.com/docs/prod/folders/print/bq27000.html
  23. * http://focus.ti.com/docs/prod/folders/print/bq27500.html
  24. */
  25. #include <linux/module.h>
  26. #include <linux/param.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/delay.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/power_supply.h>
  32. #include <linux/idr.h>
  33. #include <linux/i2c.h>
  34. #include <linux/slab.h>
  35. #include <asm/unaligned.h>
  36. #include <linux/power/bq27x00_battery.h>
  37. #define DRIVER_VERSION "1.2.0"
  38. #define BQ27x00_REG_TEMP 0x06
  39. #define BQ27x00_REG_VOLT 0x08
  40. #define BQ27x00_REG_AI 0x14
  41. #define BQ27x00_REG_FLAGS 0x0A
  42. #define BQ27x00_REG_TTE 0x16
  43. #define BQ27x00_REG_TTF 0x18
  44. #define BQ27x00_REG_TTECP 0x26
  45. #define BQ27x00_REG_NAC 0x0C /* Nominal available capaciy */
  46. #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
  47. #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
  48. #define BQ27x00_REG_AE 0x22 /* Available enery */
  49. #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
  50. #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
  51. #define BQ27000_FLAG_CHGS BIT(7)
  52. #define BQ27000_FLAG_FC BIT(5)
  53. #define BQ27500_REG_SOC 0x2C
  54. #define BQ27500_REG_DCAP 0x3C /* Design capacity */
  55. #define BQ27500_FLAG_DSC BIT(0)
  56. #define BQ27500_FLAG_FC BIT(9)
  57. #define BQ27000_RS 20 /* Resistor sense */
  58. struct bq27x00_device_info;
  59. struct bq27x00_access_methods {
  60. int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
  61. };
  62. enum bq27x00_chip { BQ27000, BQ27500 };
  63. struct bq27x00_reg_cache {
  64. int temperature;
  65. int time_to_empty;
  66. int time_to_empty_avg;
  67. int time_to_full;
  68. int charge_full;
  69. int cycle_count;
  70. int capacity;
  71. int flags;
  72. int current_now;
  73. };
  74. struct bq27x00_device_info {
  75. struct device *dev;
  76. int id;
  77. enum bq27x00_chip chip;
  78. struct bq27x00_reg_cache cache;
  79. int charge_design_full;
  80. unsigned long last_update;
  81. struct delayed_work work;
  82. struct power_supply bat;
  83. struct bq27x00_access_methods bus;
  84. struct mutex lock;
  85. };
  86. static enum power_supply_property bq27x00_battery_props[] = {
  87. POWER_SUPPLY_PROP_STATUS,
  88. POWER_SUPPLY_PROP_PRESENT,
  89. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  90. POWER_SUPPLY_PROP_CURRENT_NOW,
  91. POWER_SUPPLY_PROP_CAPACITY,
  92. POWER_SUPPLY_PROP_TEMP,
  93. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  94. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  95. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  96. POWER_SUPPLY_PROP_TECHNOLOGY,
  97. POWER_SUPPLY_PROP_CHARGE_FULL,
  98. POWER_SUPPLY_PROP_CHARGE_NOW,
  99. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  100. POWER_SUPPLY_PROP_CYCLE_COUNT,
  101. POWER_SUPPLY_PROP_ENERGY_NOW,
  102. };
  103. static unsigned int poll_interval = 360;
  104. module_param(poll_interval, uint, 0644);
  105. MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
  106. "0 disables polling");
  107. /*
  108. * Common code for BQ27x00 devices
  109. */
  110. static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
  111. bool single)
  112. {
  113. return di->bus.read(di, reg, single);
  114. }
  115. /*
  116. * Return the battery Relative State-of-Charge
  117. * Or < 0 if something fails.
  118. */
  119. static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
  120. {
  121. int rsoc;
  122. if (di->chip == BQ27500)
  123. rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
  124. else
  125. rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
  126. if (rsoc < 0)
  127. dev_err(di->dev, "error reading relative State-of-Charge\n");
  128. return rsoc;
  129. }
  130. /*
  131. * Return a battery charge value in µAh
  132. * Or < 0 if something fails.
  133. */
  134. static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
  135. {
  136. int charge;
  137. charge = bq27x00_read(di, reg, false);
  138. if (charge < 0) {
  139. dev_err(di->dev, "error reading nominal available capacity\n");
  140. return charge;
  141. }
  142. if (di->chip == BQ27500)
  143. charge *= 1000;
  144. else
  145. charge = charge * 3570 / BQ27000_RS;
  146. return charge;
  147. }
  148. /*
  149. * Return the battery Nominal available capaciy in µAh
  150. * Or < 0 if something fails.
  151. */
  152. static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
  153. {
  154. return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
  155. }
  156. /*
  157. * Return the battery Last measured discharge in µAh
  158. * Or < 0 if something fails.
  159. */
  160. static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
  161. {
  162. return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
  163. }
  164. /*
  165. * Return the battery Initial last measured discharge in µAh
  166. * Or < 0 if something fails.
  167. */
  168. static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
  169. {
  170. int ilmd;
  171. if (di->chip == BQ27500)
  172. ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
  173. else
  174. ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
  175. if (ilmd < 0) {
  176. dev_err(di->dev, "error reading initial last measured discharge\n");
  177. return ilmd;
  178. }
  179. if (di->chip == BQ27500)
  180. ilmd *= 1000;
  181. else
  182. ilmd = ilmd * 256 * 3570 / BQ27000_RS;
  183. return ilmd;
  184. }
  185. /*
  186. * Return the battery Cycle count total
  187. * Or < 0 if something fails.
  188. */
  189. static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
  190. {
  191. int cyct;
  192. cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
  193. if (cyct < 0)
  194. dev_err(di->dev, "error reading cycle count total\n");
  195. return cyct;
  196. }
  197. /*
  198. * Read a time register.
  199. * Return < 0 if something fails.
  200. */
  201. static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
  202. {
  203. int tval;
  204. tval = bq27x00_read(di, reg, false);
  205. if (tval < 0) {
  206. dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
  207. return tval;
  208. }
  209. if (tval == 65535)
  210. return -ENODATA;
  211. return tval * 60;
  212. }
  213. static void bq27x00_update(struct bq27x00_device_info *di)
  214. {
  215. struct bq27x00_reg_cache cache = {0, };
  216. bool is_bq27500 = di->chip == BQ27500;
  217. cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
  218. if (cache.flags >= 0) {
  219. cache.capacity = bq27x00_battery_read_rsoc(di);
  220. cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
  221. cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
  222. cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
  223. cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
  224. cache.charge_full = bq27x00_battery_read_lmd(di);
  225. cache.cycle_count = bq27x00_battery_read_cyct(di);
  226. if (!is_bq27500)
  227. cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false);
  228. /* We only have to read charge design full once */
  229. if (di->charge_design_full <= 0)
  230. di->charge_design_full = bq27x00_battery_read_ilmd(di);
  231. }
  232. /* Ignore current_now which is a snapshot of the current battery state
  233. * and is likely to be different even between two consecutive reads */
  234. if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) {
  235. di->cache = cache;
  236. power_supply_changed(&di->bat);
  237. }
  238. di->last_update = jiffies;
  239. }
  240. static void bq27x00_battery_poll(struct work_struct *work)
  241. {
  242. struct bq27x00_device_info *di =
  243. container_of(work, struct bq27x00_device_info, work.work);
  244. bq27x00_update(di);
  245. if (poll_interval > 0) {
  246. /* The timer does not have to be accurate. */
  247. set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
  248. schedule_delayed_work(&di->work, poll_interval * HZ);
  249. }
  250. }
  251. /*
  252. * Return the battery temperature in tenths of degree Celsius
  253. * Or < 0 if something fails.
  254. */
  255. static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
  256. union power_supply_propval *val)
  257. {
  258. if (di->cache.temperature < 0)
  259. return di->cache.temperature;
  260. if (di->chip == BQ27500)
  261. val->intval = di->cache.temperature - 2731;
  262. else
  263. val->intval = ((di->cache.temperature * 5) - 5463) / 2;
  264. return 0;
  265. }
  266. /*
  267. * Return the battery average current in µA
  268. * Note that current can be negative signed as well
  269. * Or 0 if something fails.
  270. */
  271. static int bq27x00_battery_current(struct bq27x00_device_info *di,
  272. union power_supply_propval *val)
  273. {
  274. int curr;
  275. if (di->chip == BQ27500)
  276. curr = bq27x00_read(di, BQ27x00_REG_AI, false);
  277. else
  278. curr = di->cache.current_now;
  279. if (curr < 0)
  280. return curr;
  281. if (di->chip == BQ27500) {
  282. /* bq27500 returns signed value */
  283. val->intval = (int)((s16)curr) * 1000;
  284. } else {
  285. if (di->cache.flags & BQ27000_FLAG_CHGS) {
  286. dev_dbg(di->dev, "negative current!\n");
  287. curr = -curr;
  288. }
  289. val->intval = curr * 3570 / BQ27000_RS;
  290. }
  291. return 0;
  292. }
  293. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  294. union power_supply_propval *val)
  295. {
  296. int status;
  297. if (di->chip == BQ27500) {
  298. if (di->cache.flags & BQ27500_FLAG_FC)
  299. status = POWER_SUPPLY_STATUS_FULL;
  300. else if (di->cache.flags & BQ27500_FLAG_DSC)
  301. status = POWER_SUPPLY_STATUS_DISCHARGING;
  302. else
  303. status = POWER_SUPPLY_STATUS_CHARGING;
  304. } else {
  305. if (di->cache.flags & BQ27000_FLAG_FC)
  306. status = POWER_SUPPLY_STATUS_FULL;
  307. else if (di->cache.flags & BQ27000_FLAG_CHGS)
  308. status = POWER_SUPPLY_STATUS_CHARGING;
  309. else if (power_supply_am_i_supplied(&di->bat))
  310. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  311. else
  312. status = POWER_SUPPLY_STATUS_DISCHARGING;
  313. }
  314. val->intval = status;
  315. return 0;
  316. }
  317. /*
  318. * Return the battery Voltage in milivolts
  319. * Or < 0 if something fails.
  320. */
  321. static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
  322. union power_supply_propval *val)
  323. {
  324. int volt;
  325. volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
  326. if (volt < 0)
  327. return volt;
  328. val->intval = volt * 1000;
  329. return 0;
  330. }
  331. /*
  332. * Return the battery Available energy in µWh
  333. * Or < 0 if something fails.
  334. */
  335. static int bq27x00_battery_energy(struct bq27x00_device_info *di,
  336. union power_supply_propval *val)
  337. {
  338. int ae;
  339. ae = bq27x00_read(di, BQ27x00_REG_AE, false);
  340. if (ae < 0) {
  341. dev_err(di->dev, "error reading available energy\n");
  342. return ae;
  343. }
  344. if (di->chip == BQ27500)
  345. ae *= 1000;
  346. else
  347. ae = ae * 29200 / BQ27000_RS;
  348. val->intval = ae;
  349. return 0;
  350. }
  351. static int bq27x00_simple_value(int value,
  352. union power_supply_propval *val)
  353. {
  354. if (value < 0)
  355. return value;
  356. val->intval = value;
  357. return 0;
  358. }
  359. #define to_bq27x00_device_info(x) container_of((x), \
  360. struct bq27x00_device_info, bat);
  361. static int bq27x00_battery_get_property(struct power_supply *psy,
  362. enum power_supply_property psp,
  363. union power_supply_propval *val)
  364. {
  365. int ret = 0;
  366. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  367. mutex_lock(&di->lock);
  368. if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
  369. cancel_delayed_work_sync(&di->work);
  370. bq27x00_battery_poll(&di->work.work);
  371. }
  372. mutex_unlock(&di->lock);
  373. if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
  374. return -ENODEV;
  375. switch (psp) {
  376. case POWER_SUPPLY_PROP_STATUS:
  377. ret = bq27x00_battery_status(di, val);
  378. break;
  379. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  380. ret = bq27x00_battery_voltage(di, val);
  381. break;
  382. case POWER_SUPPLY_PROP_PRESENT:
  383. val->intval = di->cache.flags < 0 ? 0 : 1;
  384. break;
  385. case POWER_SUPPLY_PROP_CURRENT_NOW:
  386. ret = bq27x00_battery_current(di, val);
  387. break;
  388. case POWER_SUPPLY_PROP_CAPACITY:
  389. ret = bq27x00_simple_value(di->cache.capacity, val);
  390. break;
  391. case POWER_SUPPLY_PROP_TEMP:
  392. ret = bq27x00_battery_temperature(di, val);
  393. break;
  394. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  395. ret = bq27x00_simple_value(di->cache.time_to_empty, val);
  396. break;
  397. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  398. ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
  399. break;
  400. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  401. ret = bq27x00_simple_value(di->cache.time_to_full, val);
  402. break;
  403. case POWER_SUPPLY_PROP_TECHNOLOGY:
  404. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  405. break;
  406. case POWER_SUPPLY_PROP_CHARGE_NOW:
  407. ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
  408. break;
  409. case POWER_SUPPLY_PROP_CHARGE_FULL:
  410. ret = bq27x00_simple_value(di->cache.charge_full, val);
  411. break;
  412. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  413. ret = bq27x00_simple_value(di->charge_design_full, val);
  414. break;
  415. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  416. ret = bq27x00_simple_value(di->cache.cycle_count, val);
  417. break;
  418. case POWER_SUPPLY_PROP_ENERGY_NOW:
  419. ret = bq27x00_battery_energy(di, val);
  420. break;
  421. default:
  422. return -EINVAL;
  423. }
  424. return ret;
  425. }
  426. static void bq27x00_external_power_changed(struct power_supply *psy)
  427. {
  428. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  429. cancel_delayed_work_sync(&di->work);
  430. schedule_delayed_work(&di->work, 0);
  431. }
  432. static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
  433. {
  434. int ret;
  435. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  436. di->bat.properties = bq27x00_battery_props;
  437. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  438. di->bat.get_property = bq27x00_battery_get_property;
  439. di->bat.external_power_changed = bq27x00_external_power_changed;
  440. INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
  441. mutex_init(&di->lock);
  442. ret = power_supply_register(di->dev, &di->bat);
  443. if (ret) {
  444. dev_err(di->dev, "failed to register battery: %d\n", ret);
  445. return ret;
  446. }
  447. dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  448. bq27x00_update(di);
  449. return 0;
  450. }
  451. static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
  452. {
  453. cancel_delayed_work_sync(&di->work);
  454. power_supply_unregister(&di->bat);
  455. mutex_destroy(&di->lock);
  456. }
  457. /* i2c specific code */
  458. #ifdef CONFIG_BATTERY_BQ27X00_I2C
  459. /* If the system has several batteries we need a different name for each
  460. * of them...
  461. */
  462. static DEFINE_IDR(battery_id);
  463. static DEFINE_MUTEX(battery_mutex);
  464. static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
  465. {
  466. struct i2c_client *client = to_i2c_client(di->dev);
  467. struct i2c_msg msg[2];
  468. unsigned char data[2];
  469. int ret;
  470. if (!client->adapter)
  471. return -ENODEV;
  472. msg[0].addr = client->addr;
  473. msg[0].flags = 0;
  474. msg[0].buf = &reg;
  475. msg[0].len = sizeof(reg);
  476. msg[1].addr = client->addr;
  477. msg[1].flags = I2C_M_RD;
  478. msg[1].buf = data;
  479. if (single)
  480. msg[1].len = 1;
  481. else
  482. msg[1].len = 2;
  483. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  484. if (ret < 0)
  485. return ret;
  486. if (!single)
  487. ret = get_unaligned_le16(data);
  488. else
  489. ret = data[0];
  490. return ret;
  491. }
  492. static int bq27x00_battery_probe(struct i2c_client *client,
  493. const struct i2c_device_id *id)
  494. {
  495. char *name;
  496. struct bq27x00_device_info *di;
  497. int num;
  498. int retval = 0;
  499. /* Get new ID for the new battery device */
  500. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  501. if (retval == 0)
  502. return -ENOMEM;
  503. mutex_lock(&battery_mutex);
  504. retval = idr_get_new(&battery_id, client, &num);
  505. mutex_unlock(&battery_mutex);
  506. if (retval < 0)
  507. return retval;
  508. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  509. if (!name) {
  510. dev_err(&client->dev, "failed to allocate device name\n");
  511. retval = -ENOMEM;
  512. goto batt_failed_1;
  513. }
  514. di = kzalloc(sizeof(*di), GFP_KERNEL);
  515. if (!di) {
  516. dev_err(&client->dev, "failed to allocate device info data\n");
  517. retval = -ENOMEM;
  518. goto batt_failed_2;
  519. }
  520. di->id = num;
  521. di->dev = &client->dev;
  522. di->chip = id->driver_data;
  523. di->bat.name = name;
  524. di->bus.read = &bq27x00_read_i2c;
  525. if (bq27x00_powersupply_init(di))
  526. goto batt_failed_3;
  527. i2c_set_clientdata(client, di);
  528. return 0;
  529. batt_failed_3:
  530. kfree(di);
  531. batt_failed_2:
  532. kfree(name);
  533. batt_failed_1:
  534. mutex_lock(&battery_mutex);
  535. idr_remove(&battery_id, num);
  536. mutex_unlock(&battery_mutex);
  537. return retval;
  538. }
  539. static int bq27x00_battery_remove(struct i2c_client *client)
  540. {
  541. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  542. bq27x00_powersupply_unregister(di);
  543. kfree(di->bat.name);
  544. mutex_lock(&battery_mutex);
  545. idr_remove(&battery_id, di->id);
  546. mutex_unlock(&battery_mutex);
  547. kfree(di);
  548. return 0;
  549. }
  550. static const struct i2c_device_id bq27x00_id[] = {
  551. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  552. { "bq27500", BQ27500 },
  553. {},
  554. };
  555. MODULE_DEVICE_TABLE(i2c, bq27x00_id);
  556. static struct i2c_driver bq27x00_battery_driver = {
  557. .driver = {
  558. .name = "bq27x00-battery",
  559. },
  560. .probe = bq27x00_battery_probe,
  561. .remove = bq27x00_battery_remove,
  562. .id_table = bq27x00_id,
  563. };
  564. static inline int bq27x00_battery_i2c_init(void)
  565. {
  566. int ret = i2c_add_driver(&bq27x00_battery_driver);
  567. if (ret)
  568. printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
  569. return ret;
  570. }
  571. static inline void bq27x00_battery_i2c_exit(void)
  572. {
  573. i2c_del_driver(&bq27x00_battery_driver);
  574. }
  575. #else
  576. static inline int bq27x00_battery_i2c_init(void) { return 0; }
  577. static inline void bq27x00_battery_i2c_exit(void) {};
  578. #endif
  579. /* platform specific code */
  580. #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
  581. static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
  582. bool single)
  583. {
  584. struct device *dev = di->dev;
  585. struct bq27000_platform_data *pdata = dev->platform_data;
  586. unsigned int timeout = 3;
  587. int upper, lower;
  588. int temp;
  589. if (!single) {
  590. /* Make sure the value has not changed in between reading the
  591. * lower and the upper part */
  592. upper = pdata->read(dev, reg + 1);
  593. do {
  594. temp = upper;
  595. if (upper < 0)
  596. return upper;
  597. lower = pdata->read(dev, reg);
  598. if (lower < 0)
  599. return lower;
  600. upper = pdata->read(dev, reg + 1);
  601. } while (temp != upper && --timeout);
  602. if (timeout == 0)
  603. return -EIO;
  604. return (upper << 8) | lower;
  605. }
  606. return pdata->read(dev, reg);
  607. }
  608. static int __devinit bq27000_battery_probe(struct platform_device *pdev)
  609. {
  610. struct bq27x00_device_info *di;
  611. struct bq27000_platform_data *pdata = pdev->dev.platform_data;
  612. int ret;
  613. if (!pdata) {
  614. dev_err(&pdev->dev, "no platform_data supplied\n");
  615. return -EINVAL;
  616. }
  617. if (!pdata->read) {
  618. dev_err(&pdev->dev, "no hdq read callback supplied\n");
  619. return -EINVAL;
  620. }
  621. di = kzalloc(sizeof(*di), GFP_KERNEL);
  622. if (!di) {
  623. dev_err(&pdev->dev, "failed to allocate device info data\n");
  624. return -ENOMEM;
  625. }
  626. platform_set_drvdata(pdev, di);
  627. di->dev = &pdev->dev;
  628. di->chip = BQ27000;
  629. di->bat.name = pdata->name ?: dev_name(&pdev->dev);
  630. di->bus.read = &bq27000_read_platform;
  631. ret = bq27x00_powersupply_init(di);
  632. if (ret)
  633. goto err_free;
  634. return 0;
  635. err_free:
  636. platform_set_drvdata(pdev, NULL);
  637. kfree(di);
  638. return ret;
  639. }
  640. static int __devexit bq27000_battery_remove(struct platform_device *pdev)
  641. {
  642. struct bq27x00_device_info *di = platform_get_drvdata(pdev);
  643. bq27x00_powersupply_unregister(di);
  644. platform_set_drvdata(pdev, NULL);
  645. kfree(di);
  646. return 0;
  647. }
  648. static struct platform_driver bq27000_battery_driver = {
  649. .probe = bq27000_battery_probe,
  650. .remove = __devexit_p(bq27000_battery_remove),
  651. .driver = {
  652. .name = "bq27000-battery",
  653. .owner = THIS_MODULE,
  654. },
  655. };
  656. static inline int bq27x00_battery_platform_init(void)
  657. {
  658. int ret = platform_driver_register(&bq27000_battery_driver);
  659. if (ret)
  660. printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
  661. return ret;
  662. }
  663. static inline void bq27x00_battery_platform_exit(void)
  664. {
  665. platform_driver_unregister(&bq27000_battery_driver);
  666. }
  667. #else
  668. static inline int bq27x00_battery_platform_init(void) { return 0; }
  669. static inline void bq27x00_battery_platform_exit(void) {};
  670. #endif
  671. /*
  672. * Module stuff
  673. */
  674. static int __init bq27x00_battery_init(void)
  675. {
  676. int ret;
  677. ret = bq27x00_battery_i2c_init();
  678. if (ret)
  679. return ret;
  680. ret = bq27x00_battery_platform_init();
  681. if (ret)
  682. bq27x00_battery_i2c_exit();
  683. return ret;
  684. }
  685. module_init(bq27x00_battery_init);
  686. static void __exit bq27x00_battery_exit(void)
  687. {
  688. bq27x00_battery_platform_exit();
  689. bq27x00_battery_i2c_exit();
  690. }
  691. module_exit(bq27x00_battery_exit);
  692. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  693. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  694. MODULE_LICENSE("GPL");