bq27x00_battery.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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 BQ27500_REG_SOC 0x2c
  52. #define BQ27500_REG_DCAP 0x3C /* Design capacity */
  53. #define BQ27500_FLAG_DSC BIT(0)
  54. #define BQ27500_FLAG_FC BIT(9)
  55. #define BQ27000_RS 20 /* Resistor sense */
  56. struct bq27x00_device_info;
  57. struct bq27x00_access_methods {
  58. int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
  59. };
  60. enum bq27x00_chip { BQ27000, BQ27500 };
  61. struct bq27x00_reg_cache {
  62. int temperature;
  63. int time_to_empty;
  64. int time_to_empty_avg;
  65. int time_to_full;
  66. int charge_full;
  67. int charge_counter;
  68. int capacity;
  69. int flags;
  70. int current_now;
  71. };
  72. struct bq27x00_device_info {
  73. struct device *dev;
  74. int id;
  75. enum bq27x00_chip chip;
  76. struct bq27x00_reg_cache cache;
  77. int charge_design_full;
  78. unsigned long last_update;
  79. struct delayed_work work;
  80. struct power_supply bat;
  81. struct bq27x00_access_methods bus;
  82. struct mutex lock;
  83. };
  84. static enum power_supply_property bq27x00_battery_props[] = {
  85. POWER_SUPPLY_PROP_STATUS,
  86. POWER_SUPPLY_PROP_PRESENT,
  87. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  88. POWER_SUPPLY_PROP_CURRENT_NOW,
  89. POWER_SUPPLY_PROP_CAPACITY,
  90. POWER_SUPPLY_PROP_TEMP,
  91. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  92. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  93. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  94. POWER_SUPPLY_PROP_TECHNOLOGY,
  95. POWER_SUPPLY_PROP_CHARGE_FULL,
  96. POWER_SUPPLY_PROP_CHARGE_NOW,
  97. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  98. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  99. POWER_SUPPLY_PROP_ENERGY_NOW,
  100. };
  101. static unsigned int poll_interval = 360;
  102. module_param(poll_interval, uint, 0644);
  103. MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
  104. "0 disables polling");
  105. /*
  106. * Common code for BQ27x00 devices
  107. */
  108. static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
  109. bool single)
  110. {
  111. return di->bus.read(di, reg, single);
  112. }
  113. /*
  114. * Return the battery Relative State-of-Charge
  115. * Or < 0 if something fails.
  116. */
  117. static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
  118. {
  119. int rsoc;
  120. if (di->chip == BQ27500)
  121. rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
  122. else
  123. rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
  124. if (rsoc < 0)
  125. dev_err(di->dev, "error reading relative State-of-Charge\n");
  126. return rsoc;
  127. }
  128. /*
  129. * Return a battery charge value in µAh
  130. * Or < 0 if something fails.
  131. */
  132. static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
  133. {
  134. int charge;
  135. charge = bq27x00_read(di, reg, false);
  136. if (charge < 0) {
  137. dev_err(di->dev, "error reading nominal available capacity\n");
  138. return charge;
  139. }
  140. if (di->chip == BQ27500)
  141. charge *= 1000;
  142. else
  143. charge = charge * 3570 / BQ27000_RS;
  144. return charge;
  145. }
  146. /*
  147. * Return the battery Nominal available capaciy in µAh
  148. * Or < 0 if something fails.
  149. */
  150. static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
  151. {
  152. return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
  153. }
  154. /*
  155. * Return the battery Last measured discharge in µAh
  156. * Or < 0 if something fails.
  157. */
  158. static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
  159. {
  160. return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
  161. }
  162. /*
  163. * Return the battery Initial last measured discharge in µAh
  164. * Or < 0 if something fails.
  165. */
  166. static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
  167. {
  168. int ilmd;
  169. if (di->chip == BQ27500)
  170. ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
  171. else
  172. ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
  173. if (ilmd < 0) {
  174. dev_err(di->dev, "error reading initial last measured discharge\n");
  175. return ilmd;
  176. }
  177. if (di->chip == BQ27500)
  178. ilmd *= 1000;
  179. else
  180. ilmd = ilmd * 256 * 3570 / BQ27000_RS;
  181. return ilmd;
  182. }
  183. /*
  184. * Return the battery Cycle count total
  185. * Or < 0 if something fails.
  186. */
  187. static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
  188. {
  189. int cyct;
  190. cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
  191. if (cyct < 0)
  192. dev_err(di->dev, "error reading cycle count total\n");
  193. return cyct;
  194. }
  195. /*
  196. * Read a time register.
  197. * Return < 0 if something fails.
  198. */
  199. static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
  200. {
  201. int tval;
  202. tval = bq27x00_read(di, reg, false);
  203. if (tval < 0) {
  204. dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
  205. return tval;
  206. }
  207. if (tval == 65535)
  208. return -ENODATA;
  209. return tval * 60;
  210. }
  211. static void bq27x00_update(struct bq27x00_device_info *di)
  212. {
  213. struct bq27x00_reg_cache cache = {0, };
  214. bool is_bq27500 = di->chip == BQ27500;
  215. cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
  216. if (cache.flags >= 0) {
  217. cache.capacity = bq27x00_battery_read_rsoc(di);
  218. cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
  219. cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
  220. cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
  221. cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
  222. cache.charge_full = bq27x00_battery_read_lmd(di);
  223. cache.charge_counter = bq27x00_battery_read_cyct(di);
  224. if (!is_bq27500)
  225. cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false);
  226. /* We only have to read charge design full once */
  227. if (di->charge_design_full <= 0)
  228. di->charge_design_full = bq27x00_battery_read_ilmd(di);
  229. }
  230. /* Ignore current_now which is a snapshot of the current battery state
  231. * and is likely to be different even between two consecutive reads */
  232. if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) {
  233. di->cache = cache;
  234. power_supply_changed(&di->bat);
  235. }
  236. di->last_update = jiffies;
  237. }
  238. static void bq27x00_battery_poll(struct work_struct *work)
  239. {
  240. struct bq27x00_device_info *di =
  241. container_of(work, struct bq27x00_device_info, work.work);
  242. bq27x00_update(di);
  243. if (poll_interval > 0) {
  244. /* The timer does not have to be accurate. */
  245. set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
  246. schedule_delayed_work(&di->work, poll_interval * HZ);
  247. }
  248. }
  249. /*
  250. * Return the battery temperature in tenths of degree Celsius
  251. * Or < 0 if something fails.
  252. */
  253. static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
  254. union power_supply_propval *val)
  255. {
  256. if (di->cache.temperature < 0)
  257. return di->cache.temperature;
  258. if (di->chip == BQ27500)
  259. val->intval = di->cache.temperature - 2731;
  260. else
  261. val->intval = ((di->cache.temperature * 5) - 5463) / 2;
  262. return 0;
  263. }
  264. /*
  265. * Return the battery average current
  266. * Note that current can be negative signed as well
  267. * Or 0 if something fails.
  268. */
  269. static int bq27x00_battery_current(struct bq27x00_device_info *di,
  270. union power_supply_propval *val)
  271. {
  272. int curr;
  273. if (di->chip == BQ27500)
  274. curr = bq27x00_read(di, BQ27x00_REG_AI, false);
  275. else
  276. curr = di->cache.current_now;
  277. if (curr < 0)
  278. return curr;
  279. if (di->chip == BQ27500) {
  280. /* bq27500 returns signed value */
  281. val->intval = (int)((s16)curr) * 1000;
  282. } else {
  283. if (di->cache.flags & BQ27000_FLAG_CHGS) {
  284. dev_dbg(di->dev, "negative current!\n");
  285. curr = -curr;
  286. }
  287. val->intval = curr * 3570 / BQ27000_RS;
  288. }
  289. return 0;
  290. }
  291. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  292. union power_supply_propval *val)
  293. {
  294. int status;
  295. if (di->chip == BQ27500) {
  296. if (di->cache.flags & BQ27500_FLAG_FC)
  297. status = POWER_SUPPLY_STATUS_FULL;
  298. else if (di->cache.flags & BQ27500_FLAG_DSC)
  299. status = POWER_SUPPLY_STATUS_DISCHARGING;
  300. else
  301. status = POWER_SUPPLY_STATUS_CHARGING;
  302. } else {
  303. if (di->cache.flags & BQ27000_FLAG_CHGS)
  304. status = POWER_SUPPLY_STATUS_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_CHARGE_COUNTER:
  410. ret = bq27x00_simple_value(di->cache.charge_counter, 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[1];
  462. unsigned char data[2];
  463. int ret;
  464. if (!client->adapter)
  465. return -ENODEV;
  466. msg->addr = client->addr;
  467. msg->flags = 0;
  468. msg->len = 1;
  469. msg->buf = data;
  470. data[0] = reg;
  471. ret = i2c_transfer(client->adapter, msg, 1);
  472. if (ret >= 0) {
  473. if (!single)
  474. msg->len = 2;
  475. else
  476. msg->len = 1;
  477. msg->flags = I2C_M_RD;
  478. ret = i2c_transfer(client->adapter, msg, 1);
  479. if (ret >= 0) {
  480. if (!single)
  481. ret = get_unaligned_le16(data);
  482. else
  483. ret = data[0];
  484. }
  485. }
  486. return ret;
  487. }
  488. static int bq27x00_battery_probe(struct i2c_client *client,
  489. const struct i2c_device_id *id)
  490. {
  491. char *name;
  492. struct bq27x00_device_info *di;
  493. int num;
  494. int retval = 0;
  495. /* Get new ID for the new battery device */
  496. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  497. if (retval == 0)
  498. return -ENOMEM;
  499. mutex_lock(&battery_mutex);
  500. retval = idr_get_new(&battery_id, client, &num);
  501. mutex_unlock(&battery_mutex);
  502. if (retval < 0)
  503. return retval;
  504. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  505. if (!name) {
  506. dev_err(&client->dev, "failed to allocate device name\n");
  507. retval = -ENOMEM;
  508. goto batt_failed_1;
  509. }
  510. di = kzalloc(sizeof(*di), GFP_KERNEL);
  511. if (!di) {
  512. dev_err(&client->dev, "failed to allocate device info data\n");
  513. retval = -ENOMEM;
  514. goto batt_failed_2;
  515. }
  516. di->id = num;
  517. di->dev = &client->dev;
  518. di->chip = id->driver_data;
  519. di->bat.name = name;
  520. di->bus.read = &bq27x00_read_i2c;
  521. if (bq27x00_powersupply_init(di))
  522. goto batt_failed_3;
  523. i2c_set_clientdata(client, di);
  524. return 0;
  525. batt_failed_3:
  526. kfree(di);
  527. batt_failed_2:
  528. kfree(name);
  529. batt_failed_1:
  530. mutex_lock(&battery_mutex);
  531. idr_remove(&battery_id, num);
  532. mutex_unlock(&battery_mutex);
  533. return retval;
  534. }
  535. static int bq27x00_battery_remove(struct i2c_client *client)
  536. {
  537. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  538. bq27x00_powersupply_unregister(di);
  539. kfree(di->bat.name);
  540. mutex_lock(&battery_mutex);
  541. idr_remove(&battery_id, di->id);
  542. mutex_unlock(&battery_mutex);
  543. kfree(di);
  544. return 0;
  545. }
  546. static const struct i2c_device_id bq27x00_id[] = {
  547. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  548. { "bq27500", BQ27500 },
  549. {},
  550. };
  551. MODULE_DEVICE_TABLE(i2c, bq27x00_id);
  552. static struct i2c_driver bq27x00_battery_driver = {
  553. .driver = {
  554. .name = "bq27x00-battery",
  555. },
  556. .probe = bq27x00_battery_probe,
  557. .remove = bq27x00_battery_remove,
  558. .id_table = bq27x00_id,
  559. };
  560. static inline int bq27x00_battery_i2c_init(void)
  561. {
  562. int ret = i2c_add_driver(&bq27x00_battery_driver);
  563. if (ret)
  564. printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
  565. return ret;
  566. }
  567. static inline void bq27x00_battery_i2c_exit(void)
  568. {
  569. i2c_del_driver(&bq27x00_battery_driver);
  570. }
  571. #else
  572. static inline int bq27x00_battery_i2c_init(void) { return 0; }
  573. static inline void bq27x00_battery_i2c_exit(void) {};
  574. #endif
  575. /* platform specific code */
  576. #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
  577. static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
  578. bool single)
  579. {
  580. struct device *dev = di->dev;
  581. struct bq27000_platform_data *pdata = dev->platform_data;
  582. unsigned int timeout = 3;
  583. int upper, lower;
  584. int temp;
  585. if (!single) {
  586. /* Make sure the value has not changed in between reading the
  587. * lower and the upper part */
  588. upper = pdata->read(dev, reg + 1);
  589. do {
  590. temp = upper;
  591. if (upper < 0)
  592. return upper;
  593. lower = pdata->read(dev, reg);
  594. if (lower < 0)
  595. return lower;
  596. upper = pdata->read(dev, reg + 1);
  597. } while (temp != upper && --timeout);
  598. if (timeout == 0)
  599. return -EIO;
  600. return (upper << 8) | lower;
  601. }
  602. return pdata->read(dev, reg);
  603. }
  604. static int __devinit bq27000_battery_probe(struct platform_device *pdev)
  605. {
  606. struct bq27x00_device_info *di;
  607. struct bq27000_platform_data *pdata = pdev->dev.platform_data;
  608. int ret;
  609. if (!pdata) {
  610. dev_err(&pdev->dev, "no platform_data supplied\n");
  611. return -EINVAL;
  612. }
  613. if (!pdata->read) {
  614. dev_err(&pdev->dev, "no hdq read callback supplied\n");
  615. return -EINVAL;
  616. }
  617. di = kzalloc(sizeof(*di), GFP_KERNEL);
  618. if (!di) {
  619. dev_err(&pdev->dev, "failed to allocate device info data\n");
  620. return -ENOMEM;
  621. }
  622. platform_set_drvdata(pdev, di);
  623. di->dev = &pdev->dev;
  624. di->chip = BQ27000;
  625. di->bat.name = pdata->name ?: dev_name(&pdev->dev);
  626. di->bus.read = &bq27000_read_platform;
  627. ret = bq27x00_powersupply_init(di);
  628. if (ret)
  629. goto err_free;
  630. return 0;
  631. err_free:
  632. platform_set_drvdata(pdev, NULL);
  633. kfree(di);
  634. return ret;
  635. }
  636. static int __devexit bq27000_battery_remove(struct platform_device *pdev)
  637. {
  638. struct bq27x00_device_info *di = platform_get_drvdata(pdev);
  639. bq27x00_powersupply_unregister(di);
  640. platform_set_drvdata(pdev, NULL);
  641. kfree(di);
  642. return 0;
  643. }
  644. static struct platform_driver bq27000_battery_driver = {
  645. .probe = bq27000_battery_probe,
  646. .remove = __devexit_p(bq27000_battery_remove),
  647. .driver = {
  648. .name = "bq27000-battery",
  649. .owner = THIS_MODULE,
  650. },
  651. };
  652. static inline int bq27x00_battery_platform_init(void)
  653. {
  654. int ret = platform_driver_register(&bq27000_battery_driver);
  655. if (ret)
  656. printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
  657. return ret;
  658. }
  659. static inline void bq27x00_battery_platform_exit(void)
  660. {
  661. platform_driver_unregister(&bq27000_battery_driver);
  662. }
  663. #else
  664. static inline int bq27x00_battery_platform_init(void) { return 0; }
  665. static inline void bq27x00_battery_platform_exit(void) {};
  666. #endif
  667. /*
  668. * Module stuff
  669. */
  670. static int __init bq27x00_battery_init(void)
  671. {
  672. int ret;
  673. ret = bq27x00_battery_i2c_init();
  674. if (ret)
  675. return ret;
  676. ret = bq27x00_battery_platform_init();
  677. if (ret)
  678. bq27x00_battery_i2c_exit();
  679. return ret;
  680. }
  681. module_init(bq27x00_battery_init);
  682. static void __exit bq27x00_battery_exit(void)
  683. {
  684. bq27x00_battery_platform_exit();
  685. bq27x00_battery_i2c_exit();
  686. }
  687. module_exit(bq27x00_battery_exit);
  688. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  689. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  690. MODULE_LICENSE("GPL");