bq27x00_battery.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. };
  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_CYCLE_COUNT,
  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.cycle_count = bq27x00_battery_read_cyct(di);
  225. /* We only have to read charge design full once */
  226. if (di->charge_design_full <= 0)
  227. di->charge_design_full = bq27x00_battery_read_ilmd(di);
  228. }
  229. if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
  230. di->cache = cache;
  231. power_supply_changed(&di->bat);
  232. }
  233. di->last_update = jiffies;
  234. }
  235. static void bq27x00_battery_poll(struct work_struct *work)
  236. {
  237. struct bq27x00_device_info *di =
  238. container_of(work, struct bq27x00_device_info, work.work);
  239. bq27x00_update(di);
  240. if (poll_interval > 0) {
  241. /* The timer does not have to be accurate. */
  242. set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
  243. schedule_delayed_work(&di->work, poll_interval * HZ);
  244. }
  245. }
  246. /*
  247. * Return the battery temperature in tenths of degree Celsius
  248. * Or < 0 if something fails.
  249. */
  250. static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
  251. union power_supply_propval *val)
  252. {
  253. if (di->cache.temperature < 0)
  254. return di->cache.temperature;
  255. if (di->chip == BQ27500)
  256. val->intval = di->cache.temperature - 2731;
  257. else
  258. val->intval = ((di->cache.temperature * 5) - 5463) / 2;
  259. return 0;
  260. }
  261. /*
  262. * Return the battery average current in µA
  263. * Note that current can be negative signed as well
  264. * Or 0 if something fails.
  265. */
  266. static int bq27x00_battery_current(struct bq27x00_device_info *di,
  267. union power_supply_propval *val)
  268. {
  269. int curr;
  270. int flags;
  271. curr = bq27x00_read(di, BQ27x00_REG_AI, false);
  272. if (curr < 0)
  273. return curr;
  274. if (di->chip == BQ27500) {
  275. /* bq27500 returns signed value */
  276. val->intval = (int)((s16)curr) * 1000;
  277. } else {
  278. flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
  279. if (flags & BQ27000_FLAG_CHGS) {
  280. dev_dbg(di->dev, "negative current!\n");
  281. curr = -curr;
  282. }
  283. val->intval = curr * 3570 / BQ27000_RS;
  284. }
  285. return 0;
  286. }
  287. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  288. union power_supply_propval *val)
  289. {
  290. int status;
  291. if (di->chip == BQ27500) {
  292. if (di->cache.flags & BQ27500_FLAG_FC)
  293. status = POWER_SUPPLY_STATUS_FULL;
  294. else if (di->cache.flags & BQ27500_FLAG_DSC)
  295. status = POWER_SUPPLY_STATUS_DISCHARGING;
  296. else
  297. status = POWER_SUPPLY_STATUS_CHARGING;
  298. } else {
  299. if (di->cache.flags & BQ27000_FLAG_FC)
  300. status = POWER_SUPPLY_STATUS_FULL;
  301. else if (di->cache.flags & BQ27000_FLAG_CHGS)
  302. status = POWER_SUPPLY_STATUS_CHARGING;
  303. else if (power_supply_am_i_supplied(&di->bat))
  304. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  305. else
  306. status = POWER_SUPPLY_STATUS_DISCHARGING;
  307. }
  308. val->intval = status;
  309. return 0;
  310. }
  311. /*
  312. * Return the battery Voltage in milivolts
  313. * Or < 0 if something fails.
  314. */
  315. static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
  316. union power_supply_propval *val)
  317. {
  318. int volt;
  319. volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
  320. if (volt < 0)
  321. return volt;
  322. val->intval = volt * 1000;
  323. return 0;
  324. }
  325. /*
  326. * Return the battery Available energy in µWh
  327. * Or < 0 if something fails.
  328. */
  329. static int bq27x00_battery_energy(struct bq27x00_device_info *di,
  330. union power_supply_propval *val)
  331. {
  332. int ae;
  333. ae = bq27x00_read(di, BQ27x00_REG_AE, false);
  334. if (ae < 0) {
  335. dev_err(di->dev, "error reading available energy\n");
  336. return ae;
  337. }
  338. if (di->chip == BQ27500)
  339. ae *= 1000;
  340. else
  341. ae = ae * 29200 / BQ27000_RS;
  342. val->intval = ae;
  343. return 0;
  344. }
  345. static int bq27x00_simple_value(int value,
  346. union power_supply_propval *val)
  347. {
  348. if (value < 0)
  349. return value;
  350. val->intval = value;
  351. return 0;
  352. }
  353. #define to_bq27x00_device_info(x) container_of((x), \
  354. struct bq27x00_device_info, bat);
  355. static int bq27x00_battery_get_property(struct power_supply *psy,
  356. enum power_supply_property psp,
  357. union power_supply_propval *val)
  358. {
  359. int ret = 0;
  360. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  361. mutex_lock(&di->lock);
  362. if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
  363. cancel_delayed_work_sync(&di->work);
  364. bq27x00_battery_poll(&di->work.work);
  365. }
  366. mutex_unlock(&di->lock);
  367. if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
  368. return -ENODEV;
  369. switch (psp) {
  370. case POWER_SUPPLY_PROP_STATUS:
  371. ret = bq27x00_battery_status(di, val);
  372. break;
  373. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  374. ret = bq27x00_battery_voltage(di, val);
  375. break;
  376. case POWER_SUPPLY_PROP_PRESENT:
  377. val->intval = di->cache.flags < 0 ? 0 : 1;
  378. break;
  379. case POWER_SUPPLY_PROP_CURRENT_NOW:
  380. ret = bq27x00_battery_current(di, val);
  381. break;
  382. case POWER_SUPPLY_PROP_CAPACITY:
  383. ret = bq27x00_simple_value(di->cache.capacity, val);
  384. break;
  385. case POWER_SUPPLY_PROP_TEMP:
  386. ret = bq27x00_battery_temperature(di, val);
  387. break;
  388. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  389. ret = bq27x00_simple_value(di->cache.time_to_empty, val);
  390. break;
  391. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  392. ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
  393. break;
  394. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  395. ret = bq27x00_simple_value(di->cache.time_to_full, val);
  396. break;
  397. case POWER_SUPPLY_PROP_TECHNOLOGY:
  398. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  399. break;
  400. case POWER_SUPPLY_PROP_CHARGE_NOW:
  401. ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
  402. break;
  403. case POWER_SUPPLY_PROP_CHARGE_FULL:
  404. ret = bq27x00_simple_value(di->cache.charge_full, val);
  405. break;
  406. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  407. ret = bq27x00_simple_value(di->charge_design_full, val);
  408. break;
  409. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  410. ret = bq27x00_simple_value(di->cache.cycle_count, val);
  411. break;
  412. case POWER_SUPPLY_PROP_ENERGY_NOW:
  413. ret = bq27x00_battery_energy(di, val);
  414. break;
  415. default:
  416. return -EINVAL;
  417. }
  418. return ret;
  419. }
  420. static void bq27x00_external_power_changed(struct power_supply *psy)
  421. {
  422. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  423. cancel_delayed_work_sync(&di->work);
  424. schedule_delayed_work(&di->work, 0);
  425. }
  426. static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
  427. {
  428. int ret;
  429. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  430. di->bat.properties = bq27x00_battery_props;
  431. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  432. di->bat.get_property = bq27x00_battery_get_property;
  433. di->bat.external_power_changed = bq27x00_external_power_changed;
  434. INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
  435. mutex_init(&di->lock);
  436. ret = power_supply_register(di->dev, &di->bat);
  437. if (ret) {
  438. dev_err(di->dev, "failed to register battery: %d\n", ret);
  439. return ret;
  440. }
  441. dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  442. bq27x00_update(di);
  443. return 0;
  444. }
  445. static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
  446. {
  447. cancel_delayed_work_sync(&di->work);
  448. power_supply_unregister(&di->bat);
  449. mutex_destroy(&di->lock);
  450. }
  451. /* i2c specific code */
  452. #ifdef CONFIG_BATTERY_BQ27X00_I2C
  453. /* If the system has several batteries we need a different name for each
  454. * of them...
  455. */
  456. static DEFINE_IDR(battery_id);
  457. static DEFINE_MUTEX(battery_mutex);
  458. static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
  459. {
  460. struct i2c_client *client = to_i2c_client(di->dev);
  461. struct i2c_msg msg[2];
  462. unsigned char data[2];
  463. int ret;
  464. if (!client->adapter)
  465. return -ENODEV;
  466. msg[0].addr = client->addr;
  467. msg[0].flags = 0;
  468. msg[0].buf = &reg;
  469. msg[0].len = sizeof(reg);
  470. msg[1].addr = client->addr;
  471. msg[1].flags = I2C_M_RD;
  472. msg[1].buf = data;
  473. if (single)
  474. msg[1].len = 1;
  475. else
  476. msg[1].len = 2;
  477. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  478. if (ret < 0)
  479. return ret;
  480. if (!single)
  481. ret = get_unaligned_le16(data);
  482. else
  483. ret = data[0];
  484. return ret;
  485. }
  486. static int bq27x00_battery_probe(struct i2c_client *client,
  487. const struct i2c_device_id *id)
  488. {
  489. char *name;
  490. struct bq27x00_device_info *di;
  491. int num;
  492. int retval = 0;
  493. /* Get new ID for the new battery device */
  494. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  495. if (retval == 0)
  496. return -ENOMEM;
  497. mutex_lock(&battery_mutex);
  498. retval = idr_get_new(&battery_id, client, &num);
  499. mutex_unlock(&battery_mutex);
  500. if (retval < 0)
  501. return retval;
  502. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  503. if (!name) {
  504. dev_err(&client->dev, "failed to allocate device name\n");
  505. retval = -ENOMEM;
  506. goto batt_failed_1;
  507. }
  508. di = kzalloc(sizeof(*di), GFP_KERNEL);
  509. if (!di) {
  510. dev_err(&client->dev, "failed to allocate device info data\n");
  511. retval = -ENOMEM;
  512. goto batt_failed_2;
  513. }
  514. di->id = num;
  515. di->dev = &client->dev;
  516. di->chip = id->driver_data;
  517. di->bat.name = name;
  518. di->bus.read = &bq27x00_read_i2c;
  519. if (bq27x00_powersupply_init(di))
  520. goto batt_failed_3;
  521. i2c_set_clientdata(client, di);
  522. return 0;
  523. batt_failed_3:
  524. kfree(di);
  525. batt_failed_2:
  526. kfree(name);
  527. batt_failed_1:
  528. mutex_lock(&battery_mutex);
  529. idr_remove(&battery_id, num);
  530. mutex_unlock(&battery_mutex);
  531. return retval;
  532. }
  533. static int bq27x00_battery_remove(struct i2c_client *client)
  534. {
  535. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  536. bq27x00_powersupply_unregister(di);
  537. kfree(di->bat.name);
  538. mutex_lock(&battery_mutex);
  539. idr_remove(&battery_id, di->id);
  540. mutex_unlock(&battery_mutex);
  541. kfree(di);
  542. return 0;
  543. }
  544. static const struct i2c_device_id bq27x00_id[] = {
  545. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  546. { "bq27500", BQ27500 },
  547. {},
  548. };
  549. MODULE_DEVICE_TABLE(i2c, bq27x00_id);
  550. static struct i2c_driver bq27x00_battery_driver = {
  551. .driver = {
  552. .name = "bq27x00-battery",
  553. },
  554. .probe = bq27x00_battery_probe,
  555. .remove = bq27x00_battery_remove,
  556. .id_table = bq27x00_id,
  557. };
  558. static inline int bq27x00_battery_i2c_init(void)
  559. {
  560. int ret = i2c_add_driver(&bq27x00_battery_driver);
  561. if (ret)
  562. printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
  563. return ret;
  564. }
  565. static inline void bq27x00_battery_i2c_exit(void)
  566. {
  567. i2c_del_driver(&bq27x00_battery_driver);
  568. }
  569. #else
  570. static inline int bq27x00_battery_i2c_init(void) { return 0; }
  571. static inline void bq27x00_battery_i2c_exit(void) {};
  572. #endif
  573. /* platform specific code */
  574. #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
  575. static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
  576. bool single)
  577. {
  578. struct device *dev = di->dev;
  579. struct bq27000_platform_data *pdata = dev->platform_data;
  580. unsigned int timeout = 3;
  581. int upper, lower;
  582. int temp;
  583. if (!single) {
  584. /* Make sure the value has not changed in between reading the
  585. * lower and the upper part */
  586. upper = pdata->read(dev, reg + 1);
  587. do {
  588. temp = upper;
  589. if (upper < 0)
  590. return upper;
  591. lower = pdata->read(dev, reg);
  592. if (lower < 0)
  593. return lower;
  594. upper = pdata->read(dev, reg + 1);
  595. } while (temp != upper && --timeout);
  596. if (timeout == 0)
  597. return -EIO;
  598. return (upper << 8) | lower;
  599. }
  600. return pdata->read(dev, reg);
  601. }
  602. static int __devinit bq27000_battery_probe(struct platform_device *pdev)
  603. {
  604. struct bq27x00_device_info *di;
  605. struct bq27000_platform_data *pdata = pdev->dev.platform_data;
  606. int ret;
  607. if (!pdata) {
  608. dev_err(&pdev->dev, "no platform_data supplied\n");
  609. return -EINVAL;
  610. }
  611. if (!pdata->read) {
  612. dev_err(&pdev->dev, "no hdq read callback supplied\n");
  613. return -EINVAL;
  614. }
  615. di = kzalloc(sizeof(*di), GFP_KERNEL);
  616. if (!di) {
  617. dev_err(&pdev->dev, "failed to allocate device info data\n");
  618. return -ENOMEM;
  619. }
  620. platform_set_drvdata(pdev, di);
  621. di->dev = &pdev->dev;
  622. di->chip = BQ27000;
  623. di->bat.name = pdata->name ?: dev_name(&pdev->dev);
  624. di->bus.read = &bq27000_read_platform;
  625. ret = bq27x00_powersupply_init(di);
  626. if (ret)
  627. goto err_free;
  628. return 0;
  629. err_free:
  630. platform_set_drvdata(pdev, NULL);
  631. kfree(di);
  632. return ret;
  633. }
  634. static int __devexit bq27000_battery_remove(struct platform_device *pdev)
  635. {
  636. struct bq27x00_device_info *di = platform_get_drvdata(pdev);
  637. bq27x00_powersupply_unregister(di);
  638. platform_set_drvdata(pdev, NULL);
  639. kfree(di);
  640. return 0;
  641. }
  642. static struct platform_driver bq27000_battery_driver = {
  643. .probe = bq27000_battery_probe,
  644. .remove = __devexit_p(bq27000_battery_remove),
  645. .driver = {
  646. .name = "bq27000-battery",
  647. .owner = THIS_MODULE,
  648. },
  649. };
  650. static inline int bq27x00_battery_platform_init(void)
  651. {
  652. int ret = platform_driver_register(&bq27000_battery_driver);
  653. if (ret)
  654. printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
  655. return ret;
  656. }
  657. static inline void bq27x00_battery_platform_exit(void)
  658. {
  659. platform_driver_unregister(&bq27000_battery_driver);
  660. }
  661. #else
  662. static inline int bq27x00_battery_platform_init(void) { return 0; }
  663. static inline void bq27x00_battery_platform_exit(void) {};
  664. #endif
  665. /*
  666. * Module stuff
  667. */
  668. static int __init bq27x00_battery_init(void)
  669. {
  670. int ret;
  671. ret = bq27x00_battery_i2c_init();
  672. if (ret)
  673. return ret;
  674. ret = bq27x00_battery_platform_init();
  675. if (ret)
  676. bq27x00_battery_i2c_exit();
  677. return ret;
  678. }
  679. module_init(bq27x00_battery_init);
  680. static void __exit bq27x00_battery_exit(void)
  681. {
  682. bq27x00_battery_platform_exit();
  683. bq27x00_battery_i2c_exit();
  684. }
  685. module_exit(bq27x00_battery_exit);
  686. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  687. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  688. MODULE_LICENSE("GPL");