bq27x00_battery.c 19 KB

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