bq27x00_battery.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. *
  7. * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  8. *
  9. * This package is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/param.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/power_supply.h>
  25. #include <linux/idr.h>
  26. #include <linux/i2c.h>
  27. #include <linux/slab.h>
  28. #include <asm/unaligned.h>
  29. #include <linux/bq27x00_battery.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/fs.h>
  32. #include <linux/device.h>
  33. #ifdef CONFIG_HAS_EARLYSUSPEND
  34. #include <linux/earlysuspend.h>
  35. static struct early_suspend bq27x00_early_suspend;
  36. #endif
  37. #define DRIVER_VERSION "1.1.0"
  38. #define SUPPORT_DFI_WRITE
  39. #define BQ27x00_REG_TEMP 0x06
  40. #define BQ27x00_REG_VOLT 0x08
  41. #define BQ27x00_REG_AI 0x14
  42. #define BQ27x00_REG_FLAGS 0x0A
  43. #define BQ27x00_REG_TTE 0x16
  44. #define BQ27x00_REG_TTF 0x18
  45. #define BQ27x00_REG_TTECP 0x26
  46. #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
  47. #define BQ27000_FLAG_CHGS BIT(7)
  48. #define BQ27500_REG_SOC 0x2c
  49. #define BQ27500_FLAG_DSC BIT(0)
  50. #define BQ27500_FLAG_FC BIT(9)
  51. static int polling_count = 0;
  52. static int ac_status = 0;
  53. #ifdef CONFIG_USB_ANDROID
  54. static int usb_status = 0;
  55. static int new_usb_status = 0;
  56. #endif /* CONFIG_USB_ANDROID */
  57. static int battery_capacity = 100;
  58. static int new_battery_capacity = 100;
  59. static int charge_status = -1;
  60. static int new_charge_status = -1;
  61. static char fw_ver[10] = {0};
  62. /* If the system has several batteries we need a different name for each
  63. * of them...
  64. */
  65. static DEFINE_IDR(battery_id);
  66. static DEFINE_MUTEX(battery_mutex);
  67. static struct timer_list polling_timer;
  68. static struct work_struct battery_work;
  69. struct bq27x00_device_info;
  70. struct bq27x00_access_methods {
  71. int (*read)(u8 reg, int *rt_value, int b_single,
  72. struct bq27x00_device_info *di);
  73. };
  74. enum bq27x00_chip { BQ27000, BQ27500 };
  75. static struct bq27x00_battery_pdata *pdata;
  76. struct bq27x00_device_info {
  77. struct device *dev;
  78. int id;
  79. int config_major;
  80. struct bq27x00_access_methods *bus;
  81. struct power_supply bat;
  82. struct power_supply ac;
  83. struct power_supply usb;
  84. enum bq27x00_chip chip;
  85. struct class *config_class;
  86. struct i2c_client *client;
  87. };
  88. static struct bq27x00_device_info *device_info;
  89. static enum power_supply_property ac_power_props[] = {
  90. POWER_SUPPLY_PROP_ONLINE,
  91. };
  92. static char *ac_supply_list[] = {
  93. "battery",
  94. };
  95. static enum power_supply_property bq27x00_battery_props[] = {
  96. POWER_SUPPLY_PROP_STATUS,
  97. POWER_SUPPLY_PROP_HEALTH,
  98. POWER_SUPPLY_PROP_PRESENT,
  99. POWER_SUPPLY_PROP_TECHNOLOGY,
  100. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  101. POWER_SUPPLY_PROP_CURRENT_NOW,
  102. POWER_SUPPLY_PROP_CAPACITY,
  103. POWER_SUPPLY_PROP_TEMP,
  104. POWER_SUPPLY_PROP_MANUFACTURER,
  105. // POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  106. // POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  107. // POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  108. };
  109. /*
  110. * Common code for BQ27x00 devices
  111. */
  112. #ifdef SUPPORT_DFI_WRITE
  113. static int _bq27x00_read_i2c(struct bq27x00_device_info *di,u16 slave_addr, u8 reg, u8 len, u8 *data);
  114. static int _bq27x00_write_i2c(struct bq27x00_device_info *di,u16 slave_addr, u8 reg, u8 len, u8 *data);
  115. #endif
  116. static int bq27x00_read(u8 reg, int *rt_value, int b_single,
  117. struct bq27x00_device_info *di)
  118. {
  119. return di->bus->read(reg, rt_value, b_single, di);
  120. }
  121. /*
  122. * Return the battery temperature in tenths of degree Celsius
  123. * Or < 0 if something fails.
  124. */
  125. static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
  126. {
  127. int ret;
  128. int temp = 0;
  129. ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
  130. if (ret) {
  131. dev_err(di->dev, "error reading temperature\n");
  132. return ret;
  133. }
  134. if (di->chip == BQ27500)
  135. return (temp - 2731)/10; //k
  136. else
  137. return ((temp >> 2) - 273);
  138. }
  139. /*
  140. * Return the battery Voltage in milivolts
  141. * Or < 0 if something fails.
  142. */
  143. static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
  144. {
  145. int ret;
  146. int volt = 0;
  147. ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
  148. if (ret) {
  149. dev_err(di->dev, "error reading voltage\n");
  150. return ret;
  151. }
  152. return volt * 1000;
  153. }
  154. /*
  155. * Return the battery average current
  156. * Note that current can be negative signed as well
  157. * Or 0 if something fails.
  158. */
  159. static int bq27x00_battery_current(struct bq27x00_device_info *di)
  160. {
  161. int ret;
  162. int curr = 0;
  163. int flags = 0;
  164. ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
  165. if (ret) {
  166. dev_err(di->dev, "error reading current\n");
  167. return 0;
  168. }
  169. if (di->chip == BQ27500) {
  170. /* bq27500 returns signed value */
  171. curr = (int)(s16)curr;
  172. } else {
  173. ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
  174. if (ret < 0) {
  175. dev_err(di->dev, "error reading flags\n");
  176. return 0;
  177. }
  178. if (flags & BQ27000_FLAG_CHGS) {
  179. dev_dbg(di->dev, "negative current!\n");
  180. curr = -curr;
  181. }
  182. }
  183. return curr * 1000;
  184. }
  185. /*
  186. * Return the battery Relative State-of-Charge
  187. * Or < 0 if something fails.
  188. */
  189. static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
  190. {
  191. int ret;
  192. int rsoc = 0;
  193. if (di->chip == BQ27500)
  194. ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
  195. else
  196. ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
  197. if (ret) {
  198. dev_err(di->dev, "error reading relative State-of-Charge\n");
  199. return ret;
  200. }
  201. //drop wrong capacity value
  202. if(rsoc > 100){
  203. dev_err(di->dev, "error reading rsoc\n");
  204. rsoc = battery_capacity;
  205. }
  206. return rsoc;
  207. }
  208. static int bq27x00_battery_status(struct bq27x00_device_info *di)
  209. {
  210. int flags = 0;
  211. int status;
  212. int ret;
  213. ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
  214. if (ret < 0) {
  215. dev_err(di->dev, "error reading flags\n");
  216. return ret;
  217. }
  218. if (di->chip == BQ27500) {
  219. if (flags & BQ27500_FLAG_FC)
  220. status = POWER_SUPPLY_STATUS_FULL;
  221. else if (flags & BQ27500_FLAG_DSC)
  222. status = POWER_SUPPLY_STATUS_DISCHARGING;
  223. else
  224. status = POWER_SUPPLY_STATUS_CHARGING;
  225. } else {
  226. if (flags & BQ27000_FLAG_CHGS)
  227. status = POWER_SUPPLY_STATUS_CHARGING;
  228. else
  229. status = POWER_SUPPLY_STATUS_DISCHARGING;
  230. }
  231. return status;
  232. }
  233. #if 0
  234. /*
  235. * Read a time register.
  236. * Return < 0 if something fails.
  237. */
  238. static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
  239. union power_supply_propval *val)
  240. {
  241. int tval = 0;
  242. int ret;
  243. ret = bq27x00_read(reg, &tval, 0, di);
  244. if (ret) {
  245. dev_err(di->dev, "error reading register %02x\n", reg);
  246. return ret;
  247. }
  248. if (tval == 65535)
  249. return -ENODATA;
  250. val->intval = tval * 60;
  251. return 0;
  252. }
  253. #endif
  254. #ifdef SUPPORT_DFI_WRITE
  255. static int bq27x00_battery_get_version(struct bq27x00_device_info *di)
  256. {
  257. u8 data[2];
  258. mdelay(200);
  259. data[1] = 0x00;
  260. data[0] = 0x02;
  261. _bq27x00_write_i2c(di,0x55,0x00,2,data);
  262. mdelay(10);
  263. _bq27x00_read_i2c(di,0x55,0x00,2,data);
  264. printk("FW version = %x.%x\n", data[1],data[0]);
  265. sprintf(fw_ver,"BQ%x.%x",data[1],data[0]);
  266. return 0;
  267. }
  268. #endif
  269. #ifdef CONFIG_USB_ANDROID
  270. int pc_connect(int status)
  271. {
  272. new_usb_status = status;
  273. if(new_usb_status == usb_status)
  274. return 1;
  275. if(device_info){
  276. usb_status = new_usb_status;
  277. power_supply_changed(&device_info->usb);
  278. }
  279. return 0;
  280. }
  281. static int gadget_is_usb_online(void)
  282. {
  283. return usb_status;
  284. }
  285. EXPORT_SYMBOL(pc_connect);
  286. #endif
  287. #define to_bq27x00_device_info(x) container_of((x), \
  288. struct bq27x00_device_info, bat);
  289. static int ac_power_get_property(struct power_supply *psy,
  290. enum power_supply_property psp, union power_supply_propval *val)
  291. {
  292. int retval = 0;
  293. if (psy->type == POWER_SUPPLY_TYPE_MAINS)
  294. val->intval = pdata->is_ac_online ? pdata->is_ac_online() : 0;
  295. else
  296. val->intval = pdata->is_usb_online ? pdata->is_usb_online() : 0;
  297. return retval;
  298. }
  299. static int bq27x00_battery_get_property(struct power_supply *psy,
  300. enum power_supply_property psp,
  301. union power_supply_propval *val)
  302. {
  303. int ret = 0;
  304. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  305. switch (psp) {
  306. case POWER_SUPPLY_PROP_STATUS:
  307. // if(pdata->is_ac_online())
  308. // {
  309. // if(pdata->get_charge_status())
  310. // val->intval = POWER_SUPPLY_STATUS_FULL;
  311. // else
  312. // val->intval = POWER_SUPPLY_STATUS_CHARGING;
  313. // }
  314. // else
  315. // val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  316. val->intval = new_charge_status;
  317. break;
  318. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  319. case POWER_SUPPLY_PROP_PRESENT:
  320. val->intval = bq27x00_battery_voltage(di);
  321. if (psp == POWER_SUPPLY_PROP_PRESENT)
  322. val->intval = val->intval <= 0 ? 0 : 1;
  323. break;
  324. case POWER_SUPPLY_PROP_CURRENT_NOW:
  325. val->intval = bq27x00_battery_current(di);
  326. break;
  327. case POWER_SUPPLY_PROP_CAPACITY:
  328. val->intval = bq27x00_battery_rsoc(di);
  329. break;
  330. case POWER_SUPPLY_PROP_TEMP:
  331. val->intval = bq27x00_battery_temperature(di);
  332. break;
  333. case POWER_SUPPLY_PROP_MANUFACTURER:
  334. #ifdef SUPPORT_DFI_WRITE
  335. val->strval = fw_ver;
  336. #endif
  337. break;
  338. // case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  339. // ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
  340. // break;
  341. // case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  342. // ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
  343. // break;
  344. // case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  345. // ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
  346. // break;
  347. case POWER_SUPPLY_PROP_TECHNOLOGY:
  348. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  349. break;
  350. case POWER_SUPPLY_PROP_HEALTH:
  351. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  352. break;
  353. default:
  354. return -EINVAL;
  355. }
  356. return ret;
  357. }
  358. static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
  359. {
  360. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  361. di->bat.properties = bq27x00_battery_props;
  362. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  363. di->bat.get_property = bq27x00_battery_get_property;
  364. di->bat.external_power_changed = NULL;
  365. di->ac.name = "ac";
  366. di->ac.type = POWER_SUPPLY_TYPE_MAINS;
  367. di->ac.supplied_to = ac_supply_list,
  368. di->ac.num_supplicants = ARRAY_SIZE(ac_supply_list),
  369. di->ac.properties = ac_power_props;
  370. di->ac.num_properties = ARRAY_SIZE(ac_power_props);
  371. di->ac.get_property = ac_power_get_property;
  372. di->usb.name = "usb";
  373. di->usb.type = POWER_SUPPLY_TYPE_USB;
  374. di->usb.supplied_to = ac_supply_list,
  375. di->usb.num_supplicants = ARRAY_SIZE(ac_supply_list),
  376. di->usb.properties = ac_power_props;
  377. di->usb.num_properties = ARRAY_SIZE(ac_power_props);
  378. di->usb.get_property = ac_power_get_property;
  379. }
  380. /*
  381. * i2c specific code
  382. */
  383. static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
  384. struct bq27x00_device_info *di)
  385. {
  386. struct i2c_client *client = di->client;
  387. struct i2c_msg msg[1];
  388. unsigned char data[2];
  389. int err;
  390. if (!client->adapter)
  391. return -ENODEV;
  392. msg->addr = client->addr;
  393. msg->flags = 0;
  394. msg->len = 1;
  395. msg->buf = data;
  396. data[0] = reg;
  397. err = i2c_transfer(client->adapter, msg, 1);
  398. if (err >= 0) {
  399. if (!b_single)
  400. msg->len = 2;
  401. else
  402. msg->len = 1;
  403. msg->flags = I2C_M_RD;
  404. err = i2c_transfer(client->adapter, msg, 1);
  405. if (err >= 0) {
  406. if (!b_single)
  407. *rt_value = get_unaligned_le16(data);
  408. else
  409. *rt_value = data[0];
  410. return 0;
  411. }
  412. }
  413. return err;
  414. }
  415. static void battery_work_func(struct work_struct *work)
  416. {
  417. bool ac_changed,bat_changed;
  418. static int led_flash=1;
  419. ac_changed = false;
  420. bat_changed = false;
  421. polling_count ++;
  422. if(polling_count >= 6){
  423. polling_count = 0;
  424. new_battery_capacity = bq27x00_battery_rsoc(device_info);
  425. }
  426. if((pdata->set_lcd_off)&&(pdata->set_lcd_on)&&(new_battery_capacity>=0))
  427. {
  428. if((new_battery_capacity<=15)&&(pdata->is_ac_online()==0))
  429. {
  430. if(led_flash==1)
  431. {
  432. led_flash=0;
  433. pdata->set_lcd_off();
  434. //printk(KERN_ERR " set_lcd_off\n");
  435. }
  436. else
  437. {
  438. led_flash=1;
  439. pdata->set_lcd_on();
  440. //printk(KERN_ERR " set_lcd_on\n");
  441. }
  442. }
  443. else if(pdata->is_ac_online()==1)
  444. {
  445. led_flash=0;
  446. pdata->set_lcd_on();
  447. //printk(KERN_ERR " set_lcd_on\n");
  448. }
  449. else
  450. {
  451. led_flash=1;
  452. pdata->set_lcd_off();
  453. //printk(KERN_ERR " set_lcd_off\n");
  454. }
  455. }
  456. if(pdata->is_ac_online){
  457. if(ac_status != pdata->is_ac_online()){
  458. ac_status = pdata->is_ac_online();
  459. ac_changed = true;
  460. }
  461. }
  462. if (device_info->chip == BQ27500) {
  463. new_charge_status = bq27x00_battery_status(device_info);
  464. }
  465. else if (pdata->get_charge_status&&pdata->is_ac_online){
  466. if(pdata->is_ac_online())
  467. {
  468. if(pdata->get_charge_status())
  469. new_charge_status = POWER_SUPPLY_STATUS_FULL;
  470. else
  471. new_charge_status = POWER_SUPPLY_STATUS_CHARGING;
  472. }
  473. else
  474. new_charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  475. }
  476. if(new_charge_status != charge_status||battery_capacity != new_battery_capacity){
  477. charge_status = new_charge_status;
  478. battery_capacity = new_battery_capacity;
  479. bat_changed = true;
  480. }
  481. if(bat_changed){
  482. power_supply_changed(&device_info->bat);
  483. }
  484. if(ac_changed){
  485. power_supply_changed(&device_info->ac);
  486. }
  487. }
  488. static void polling_timer_func(unsigned long unused)
  489. {
  490. schedule_work(&battery_work);
  491. mod_timer(&polling_timer,jiffies + msecs_to_jiffies(pdata->polling_interval));
  492. }
  493. #ifdef SUPPORT_DFI_WRITE
  494. static int _bq27x00_read_i2c(struct bq27x00_device_info *di,u16 slave_addr, u8 reg, u8 len, u8 *data)
  495. {
  496. struct i2c_client *client = di->client;
  497. int ret;
  498. u8 msgbuf0[1] = { reg };
  499. u16 slave = slave_addr;
  500. u16 flags = 0;
  501. struct i2c_msg msg[2] = {
  502. { slave, flags, 1, msgbuf0 },
  503. { slave, flags|I2C_M_RD, len, data }
  504. };
  505. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  506. return ret;
  507. }
  508. static int _bq27x00_write_i2c(struct bq27x00_device_info *di,u16 slave_addr, u8 reg, u8 len, u8 *data)
  509. {
  510. struct i2c_client *client = di->client;
  511. u8 msgbuf0[1] = { reg };
  512. u16 slave = slave_addr;
  513. u16 flags = 0;
  514. struct i2c_msg msg[2] = {
  515. { slave, flags, 1, msgbuf0 },
  516. { slave, flags|I2C_M_NOSTART, len, data }
  517. };
  518. return i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  519. }
  520. loff_t bq27x00_file_seek(struct file *filp, loff_t off, int whence)
  521. {
  522. loff_t newpos;
  523. switch(whence) {
  524. case 0: /* SEEK_SET */
  525. newpos = off;
  526. break;
  527. default: /* can't happen */
  528. return -EINVAL;
  529. }
  530. if (newpos < 0) return -EINVAL;
  531. filp->f_pos = newpos;
  532. return newpos;
  533. }
  534. static int bq27x00_file_open(struct inode *inode, struct file *file)
  535. {
  536. int ret = 0;
  537. u8 data[2];
  538. del_timer_sync(&polling_timer);
  539. flush_scheduled_work();
  540. mdelay(1000);
  541. data[0] = 0x02;
  542. data[1] = 0x00;
  543. _bq27x00_write_i2c(device_info,0x55,0x00,2,data);
  544. mdelay(10);
  545. _bq27x00_read_i2c(device_info,0x55,0x00,2,data);
  546. printk("FW version = %x,%x\n", data[1],data[0]);
  547. data[0] = 0x00;
  548. data[1] = 0x0f;
  549. _bq27x00_write_i2c(device_info,0x55,0x00,2,data);
  550. printk("bq27x00_file_open\n");
  551. return ret;
  552. }
  553. static int bq27x00_file_release(struct inode *inode, struct file *file)
  554. {
  555. int ret = 0;
  556. u8 data[2];
  557. mdelay(200);
  558. data[1] = 0x00;
  559. data[0] = 0x02;
  560. _bq27x00_write_i2c(device_info,0x55,0x00,2,data);
  561. mdelay(10);
  562. _bq27x00_read_i2c(device_info,0x55,0x00,2,data);
  563. printk("FW version = %x,%x\n", data[0],data[1]);
  564. mdelay(250);
  565. setup_timer(&polling_timer, polling_timer_func, 0);
  566. mod_timer(&polling_timer,
  567. jiffies + msecs_to_jiffies(pdata->polling_interval));
  568. printk("bq27x00_file_release\n");
  569. return ret;
  570. }
  571. static ssize_t bq27x00_file_read( struct file *file, char __user *buf,
  572. size_t count, loff_t *ppos )
  573. {
  574. uint8_t buffer[20]={0};
  575. u16 addr = 0x0b;
  576. unsigned reg = *ppos;
  577. int ret;
  578. if(count > 128){
  579. printk("%s, buffer exceed 128 bytes\n", __func__);
  580. return -1;
  581. }
  582. _bq27x00_read_i2c(device_info,addr,reg,count,buffer);
  583. ret = copy_to_user(buf,buffer,count);
  584. return count;
  585. }
  586. static ssize_t bq27x00_file_write( struct file *file, const char __user *buf,
  587. size_t count, loff_t *ppos )
  588. {
  589. uint8_t buffer[100]={0};
  590. u16 addr = 0x0b;
  591. unsigned reg = *ppos;
  592. int i;
  593. if (count > 100) {
  594. printk("%s, buffer exceed 100 bytes\n", __func__);
  595. return -1;
  596. }
  597. i = copy_from_user(buffer, buf, count);
  598. for (i = 0; i < count; i++) {
  599. _bq27x00_write_i2c(device_info,addr,reg+i,1,buffer+i);
  600. mdelay(1);
  601. }
  602. return -1;
  603. }
  604. static const struct file_operations bq27x00_fops = {
  605. .owner = THIS_MODULE,
  606. .llseek = bq27x00_file_seek,
  607. .open = bq27x00_file_open,
  608. .release = bq27x00_file_release,
  609. .read = bq27x00_file_read,
  610. .write = bq27x00_file_write,
  611. };
  612. #endif
  613. #ifdef CONFIG_HAS_EARLYSUSPEND
  614. static void early_suspend(struct early_suspend *h)
  615. {
  616. /* Kill the battery timer sync */
  617. del_timer_sync(&polling_timer);
  618. flush_scheduled_work();
  619. if (pdata->set_charge) {
  620. pdata->set_charge(1);
  621. printk("fast charger on early_suspend\n\n");
  622. }
  623. }
  624. static void late_resume(struct early_suspend *h)
  625. {
  626. setup_timer(&polling_timer, polling_timer_func, 0);
  627. mod_timer(&polling_timer,
  628. jiffies + msecs_to_jiffies(pdata->polling_interval));
  629. if (pdata->set_charge) {
  630. pdata->set_charge(0);
  631. printk("set slow charge\n");
  632. }
  633. }
  634. #endif
  635. static int bq27x00_battery_probe(struct i2c_client *client,
  636. const struct i2c_device_id *id)
  637. {
  638. char *name;
  639. struct bq27x00_device_info *di;
  640. struct bq27x00_access_methods *bus;
  641. int num;
  642. int retval = 0;
  643. /* Get new ID for the new battery device */
  644. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  645. if (retval == 0)
  646. return -ENOMEM;
  647. mutex_lock(&battery_mutex);
  648. retval = idr_get_new(&battery_id, client, &num);
  649. mutex_unlock(&battery_mutex);
  650. if (retval < 0)
  651. return retval;
  652. pdata = (struct bq27x00_battery_pdata*)client->dev.platform_data;
  653. #ifdef CONFIG_USB_ANDROID
  654. pdata->is_usb_online = gadget_is_usb_online;
  655. #endif
  656. if (pdata->set_charge) {
  657. pdata->set_charge(0);
  658. printk("set slow charge\n");
  659. }
  660. name = kasprintf(GFP_KERNEL, "bq27x00");
  661. if (!name) {
  662. dev_err(&client->dev, "failed to allocate device name\n");
  663. retval = -ENOMEM;
  664. goto batt_failed_1;
  665. }
  666. di = kzalloc(sizeof(*di), GFP_KERNEL);
  667. if (!di) {
  668. dev_err(&client->dev, "failed to allocate device info data\n");
  669. retval = -ENOMEM;
  670. goto batt_failed_2;
  671. }
  672. di->id = num;
  673. di->chip = pdata->chip;
  674. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  675. if (!bus) {
  676. dev_err(&client->dev, "failed to allocate access method "
  677. "data\n");
  678. retval = -ENOMEM;
  679. goto batt_failed_3;
  680. }
  681. i2c_set_clientdata(client, di);
  682. di->dev = &client->dev;
  683. di->bat.name = name;
  684. bus->read = &bq27x00_read_i2c;
  685. di->bus = bus;
  686. di->client = client;
  687. bq27x00_powersupply_init(di);
  688. retval = power_supply_register(&client->dev, &di->bat);
  689. if (retval) {
  690. dev_err(&client->dev, "failed to register battery\n");
  691. goto batt_failed_4;
  692. }
  693. retval = power_supply_register(&client->dev, &di->ac);
  694. if (retval) {
  695. dev_err(&client->dev, "failed to register ac\n");
  696. goto batt_failed_4;
  697. }
  698. retval = power_supply_register(&client->dev, &di->usb);
  699. if (retval) {
  700. dev_err(&client->dev, "failed to register usb\n");
  701. goto batt_failed_4;
  702. }
  703. if (!pdata->polling_interval)
  704. pdata->polling_interval = 2000;
  705. INIT_WORK(&battery_work, battery_work_func);
  706. setup_timer(&polling_timer, polling_timer_func, 0);
  707. mod_timer(&polling_timer,
  708. jiffies + msecs_to_jiffies(pdata->polling_interval));
  709. #ifdef CONFIG_HAS_EARLYSUSPEND
  710. bq27x00_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
  711. bq27x00_early_suspend.suspend = early_suspend;
  712. bq27x00_early_suspend.resume = late_resume;
  713. bq27x00_early_suspend.param = NULL;
  714. register_early_suspend(&bq27x00_early_suspend);
  715. #endif
  716. dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  717. di->config_major = register_chrdev(0,di->bat.name,&bq27x00_fops);
  718. printk("bq27x00_fops majo = %d\n",di->config_major);
  719. if(di->config_major<=0){
  720. printk("register char device error\n");
  721. }
  722. di->config_class=class_create(THIS_MODULE,di->bat.name);
  723. di->dev=device_create(di->config_class, NULL,
  724. MKDEV(di->config_major,0),NULL,di->bat.name);
  725. bq27x00_battery_get_version(di);
  726. device_info = di;
  727. return 0;
  728. batt_failed_4:
  729. kfree(bus);
  730. batt_failed_3:
  731. kfree(di);
  732. batt_failed_2:
  733. kfree(name);
  734. batt_failed_1:
  735. mutex_lock(&battery_mutex);
  736. idr_remove(&battery_id, num);
  737. mutex_unlock(&battery_mutex);
  738. return retval;
  739. }
  740. static int bq27x00_battery_remove(struct i2c_client *client)
  741. {
  742. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  743. power_supply_unregister(&di->bat);
  744. power_supply_unregister(&di->ac);
  745. power_supply_unregister(&di->usb);
  746. unregister_chrdev(di->config_major,di->bat.name);
  747. if(di->config_class)
  748. {
  749. if(di->dev)
  750. device_destroy(di->config_class,MKDEV(di->config_major,0));
  751. class_destroy(di->config_class);
  752. }
  753. kfree(di->bat.name);
  754. mutex_lock(&battery_mutex);
  755. idr_remove(&battery_id, di->id);
  756. mutex_unlock(&battery_mutex);
  757. #ifdef CONFIG_HAS_EARLYSUSPEND
  758. unregister_early_suspend(&bq27x00_early_suspend);
  759. #endif
  760. kfree(di);
  761. return 0;
  762. }
  763. /*
  764. * Module stuff
  765. */
  766. static const struct i2c_device_id bq27x00_id[] = {
  767. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  768. { "bq27500", BQ27500 },
  769. {},
  770. };
  771. static struct i2c_driver bq27x00_battery_driver = {
  772. .driver = {
  773. .name = "bq27x00-battery",
  774. },
  775. .probe = bq27x00_battery_probe,
  776. .remove = bq27x00_battery_remove,
  777. .id_table = bq27x00_id,
  778. };
  779. static int __init bq27x00_battery_init(void)
  780. {
  781. int ret;
  782. ret = i2c_add_driver(&bq27x00_battery_driver);
  783. if (ret)
  784. printk(KERN_ERR "Unable to register BQ27x00 driver\n");
  785. return ret;
  786. }
  787. module_init(bq27x00_battery_init);
  788. static void __exit bq27x00_battery_exit(void)
  789. {
  790. i2c_del_driver(&bq27x00_battery_driver);
  791. }
  792. module_exit(bq27x00_battery_exit);
  793. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  794. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  795. MODULE_LICENSE("GPL");