qci_battery.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /* Quanta I2C Battery Driver
  2. *
  3. * Copyright (C) 2009 Quanta Computer Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. /*
  16. *
  17. * The Driver with I/O communications via the I2C Interface for ST15 platform.
  18. * And it is only working on the nuvoTon WPCE775x Embedded Controller.
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/err.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/power_supply.h>
  25. #include <linux/sched.h>
  26. #include <linux/gpio.h>
  27. #include <linux/i2c.h>
  28. #include <linux/wpce775x.h>
  29. #include <linux/delay.h>
  30. #include "qci_battery.h"
  31. #define QCIBAT_DEFAULT_CHARGE_FULL_CAPACITY 2200 /* 2200 mAh */
  32. #define QCIBAT_DEFAULT_CHARGE_FULL_DESIGN 2200
  33. #define QCIBAT_DEFAULT_VOLTAGE_DESIGN 10800 /* 10.8 V */
  34. #define QCIBAT_STRING_SIZE 16
  35. /* General structure to hold the driver data */
  36. struct i2cbat_drv_data {
  37. struct i2c_client *bi2c_client;
  38. struct work_struct work;
  39. unsigned int qcibat_irq;
  40. unsigned int qcibat_gpio;
  41. u8 battery_state;
  42. u8 battery_dev_name[QCIBAT_STRING_SIZE];
  43. u8 serial_number[QCIBAT_STRING_SIZE];
  44. u8 manufacturer_name[QCIBAT_STRING_SIZE];
  45. unsigned int charge_full;
  46. unsigned int charge_full_design;
  47. unsigned int voltage_full_design;
  48. unsigned int energy_full;
  49. };
  50. static struct i2cbat_drv_data context;
  51. static struct mutex qci_i2c_lock;
  52. static struct mutex qci_transaction_lock;
  53. /*********************************************************************
  54. * Power
  55. *********************************************************************/
  56. static int get_bat_info(u8 ec_data)
  57. {
  58. u8 byte_read;
  59. mutex_lock(&qci_i2c_lock);
  60. i2c_smbus_write_byte(context.bi2c_client, ec_data);
  61. byte_read = i2c_smbus_read_byte(context.bi2c_client);
  62. mutex_unlock(&qci_i2c_lock);
  63. return byte_read;
  64. }
  65. static int qci_ac_get_prop(struct power_supply *psy,
  66. enum power_supply_property psp,
  67. union power_supply_propval *val)
  68. {
  69. int ret = 0;
  70. switch (psp) {
  71. case POWER_SUPPLY_PROP_ONLINE:
  72. if (get_bat_info(ECRAM_POWER_SOURCE) & EC_FLAG_ADAPTER_IN)
  73. val->intval = EC_ADAPTER_PRESENT;
  74. else
  75. val->intval = EC_ADAPTER_NOT_PRESENT;
  76. break;
  77. default:
  78. ret = -EINVAL;
  79. break;
  80. }
  81. return ret;
  82. }
  83. static enum power_supply_property qci_ac_props[] = {
  84. POWER_SUPPLY_PROP_ONLINE,
  85. };
  86. static enum power_supply_property qci_bat_props[] = {
  87. POWER_SUPPLY_PROP_STATUS,
  88. POWER_SUPPLY_PROP_PRESENT,
  89. POWER_SUPPLY_PROP_HEALTH,
  90. POWER_SUPPLY_PROP_TECHNOLOGY,
  91. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  92. POWER_SUPPLY_PROP_CURRENT_AVG,
  93. POWER_SUPPLY_PROP_CAPACITY,
  94. POWER_SUPPLY_PROP_TEMP,
  95. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  96. POWER_SUPPLY_PROP_CHARGE_FULL,
  97. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  98. POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  99. POWER_SUPPLY_PROP_MODEL_NAME,
  100. POWER_SUPPLY_PROP_MANUFACTURER,
  101. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  102. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  103. POWER_SUPPLY_PROP_ENERGY_NOW,
  104. POWER_SUPPLY_PROP_ENERGY_FULL,
  105. POWER_SUPPLY_PROP_ENERGY_EMPTY,
  106. };
  107. static int read_data_from_battery(u8 smb_cmd, u8 smb_prtcl)
  108. {
  109. if (context.battery_state & MAIN_BATTERY_STATUS_BAT_IN) {
  110. mutex_lock(&qci_i2c_lock);
  111. i2c_smbus_write_byte_data(context.bi2c_client,
  112. ECRAM_SMB_STS, 0);
  113. i2c_smbus_write_byte_data(context.bi2c_client, ECRAM_SMB_ADDR,
  114. BATTERY_SLAVE_ADDRESS);
  115. i2c_smbus_write_byte_data(context.bi2c_client,
  116. ECRAM_SMB_CMD, smb_cmd);
  117. i2c_smbus_write_byte_data(context.bi2c_client,
  118. ECRAM_SMB_PRTCL, smb_prtcl);
  119. mutex_unlock(&qci_i2c_lock);
  120. msleep(100);
  121. return get_bat_info(ECRAM_SMB_STS);
  122. } else
  123. return SMBUS_DEVICE_NOACK;
  124. }
  125. static int qbat_get_status(union power_supply_propval *val)
  126. {
  127. int status;
  128. status = get_bat_info(ECRAM_BATTERY_STATUS);
  129. if ((status & MAIN_BATTERY_STATUS_BAT_IN) == 0x0)
  130. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  131. else if (status & MAIN_BATTERY_STATUS_BAT_CHARGING)
  132. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  133. else if (status & MAIN_BATTERY_STATUS_BAT_FULL)
  134. val->intval = POWER_SUPPLY_STATUS_FULL;
  135. else if (status & MAIN_BATTERY_STATUS_BAT_DISCHRG)
  136. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  137. else
  138. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  139. return 0;
  140. }
  141. static int qbat_get_present(union power_supply_propval *val)
  142. {
  143. if (context.battery_state & MAIN_BATTERY_STATUS_BAT_IN)
  144. val->intval = EC_BAT_PRESENT;
  145. else
  146. val->intval = EC_BAT_NOT_PRESENT;
  147. return 0;
  148. }
  149. static int qbat_get_health(union power_supply_propval *val)
  150. {
  151. u8 health;
  152. health = get_bat_info(ECRAM_CHARGER_ALARM);
  153. if (!(context.battery_state & MAIN_BATTERY_STATUS_BAT_IN))
  154. val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
  155. else if (health & ALARM_OVER_TEMP)
  156. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  157. else if (health & ALARM_REMAIN_CAPACITY)
  158. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  159. else if (health & ALARM_OVER_CHARGE)
  160. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  161. else
  162. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  163. return 0;
  164. }
  165. static int qbat_get_voltage_avg(union power_supply_propval *val)
  166. {
  167. val->intval = (get_bat_info(ECRAM_BATTERY_VOLTAGE_MSB) << 8 |
  168. get_bat_info(ECRAM_BATTERY_VOLTAGE_LSB)) * 1000;
  169. return 0;
  170. }
  171. static int qbat_get_current_avg(union power_supply_propval *val)
  172. {
  173. val->intval = (get_bat_info(ECRAM_BATTERY_CURRENT_MSB) << 8 |
  174. get_bat_info(ECRAM_BATTERY_CURRENT_LSB));
  175. return 0;
  176. }
  177. static int qbat_get_capacity(union power_supply_propval *val)
  178. {
  179. if (!(context.battery_state & MAIN_BATTERY_STATUS_BAT_IN))
  180. val->intval = 0xFF;
  181. else
  182. val->intval = get_bat_info(ECRAM_BATTERY_CAPACITY);
  183. return 0;
  184. }
  185. static int qbat_get_temp_avg(union power_supply_propval *val)
  186. {
  187. int temp;
  188. int rc = 0;
  189. if (!(context.battery_state & MAIN_BATTERY_STATUS_BAT_IN)) {
  190. val->intval = 0xFFFF;
  191. rc = -ENODATA;
  192. } else {
  193. temp = (get_bat_info(ECRAM_BATTERY_TEMP_MSB) << 8) |
  194. get_bat_info(ECRAM_BATTERY_TEMP_LSB);
  195. val->intval = (temp - 2730) / 10;
  196. }
  197. return rc;
  198. }
  199. static int qbat_get_charge_full_design(union power_supply_propval *val)
  200. {
  201. val->intval = context.charge_full_design;
  202. return 0;
  203. }
  204. static int qbat_get_charge_full(union power_supply_propval *val)
  205. {
  206. val->intval = context.charge_full;
  207. return 0;
  208. }
  209. static int qbat_get_charge_counter(union power_supply_propval *val)
  210. {
  211. u16 charge = 0;
  212. int rc = 0;
  213. mutex_lock(&qci_transaction_lock);
  214. if (read_data_from_battery(BATTERY_CYCLE_COUNT,
  215. SMBUS_READ_WORD_PRTCL) == SMBUS_DONE) {
  216. charge = get_bat_info(ECRAM_SMB_DATA1);
  217. charge = charge << 8;
  218. charge |= get_bat_info(ECRAM_SMB_DATA0);
  219. } else
  220. rc = -ENODATA;
  221. mutex_unlock(&qci_transaction_lock);
  222. val->intval = charge;
  223. return rc;
  224. }
  225. static int qbat_get_time_empty_avg(union power_supply_propval *val)
  226. {
  227. u16 avg = 0;
  228. int rc = 0;
  229. mutex_lock(&qci_transaction_lock);
  230. if (read_data_from_battery(BATTERY_AVERAGE_TIME_TO_EMPTY,
  231. SMBUS_READ_WORD_PRTCL) == SMBUS_DONE) {
  232. avg = get_bat_info(ECRAM_SMB_DATA1);
  233. avg = avg << 8;
  234. avg |= get_bat_info(ECRAM_SMB_DATA0);
  235. } else
  236. rc = -ENODATA;
  237. mutex_unlock(&qci_transaction_lock);
  238. val->intval = avg;
  239. return rc;
  240. }
  241. static int qbat_get_time_full_avg(union power_supply_propval *val)
  242. {
  243. u16 avg = 0;
  244. int rc = 0;
  245. mutex_lock(&qci_transaction_lock);
  246. if (read_data_from_battery(BATTERY_AVERAGE_TIME_TO_FULL,
  247. SMBUS_READ_WORD_PRTCL) == SMBUS_DONE) {
  248. avg = get_bat_info(ECRAM_SMB_DATA1);
  249. avg = avg << 8;
  250. avg |= get_bat_info(ECRAM_SMB_DATA0);
  251. } else
  252. rc = -ENODATA;
  253. mutex_unlock(&qci_transaction_lock);
  254. val->intval = avg;
  255. return rc;
  256. }
  257. static int qbat_get_model_name(union power_supply_propval *val)
  258. {
  259. unsigned char i, size;
  260. mutex_lock(&qci_transaction_lock);
  261. if (read_data_from_battery(BATTERY_DEVICE_NAME,
  262. SMBUS_READ_BLOCK_PRTCL) == SMBUS_DONE) {
  263. size = min(get_bat_info(ECRAM_SMB_BCNT), QCIBAT_STRING_SIZE);
  264. for (i = 0; i < size; i++) {
  265. context.battery_dev_name[i] =
  266. get_bat_info(ECRAM_SMB_DATA_START + i);
  267. }
  268. val->strval = context.battery_dev_name;
  269. } else
  270. val->strval = "Unknown";
  271. mutex_unlock(&qci_transaction_lock);
  272. return 0;
  273. }
  274. static int qbat_get_manufacturer_name(union power_supply_propval *val)
  275. {
  276. val->strval = context.manufacturer_name;
  277. return 0;
  278. }
  279. static int qbat_get_serial_number(union power_supply_propval *val)
  280. {
  281. val->strval = context.serial_number;
  282. return 0;
  283. }
  284. static int qbat_get_technology(union power_supply_propval *val)
  285. {
  286. val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  287. return 0;
  288. }
  289. static int qbat_get_energy_now(union power_supply_propval *val)
  290. {
  291. if (!(get_bat_info(ECRAM_BATTERY_STATUS) & MAIN_BATTERY_STATUS_BAT_IN))
  292. val->intval = 0;
  293. else
  294. val->intval = (get_bat_info(ECRAM_BATTERY_CAPACITY) *
  295. context.energy_full) / 100;
  296. return 0;
  297. }
  298. static int qbat_get_energy_full(union power_supply_propval *val)
  299. {
  300. val->intval = context.energy_full;
  301. return 0;
  302. }
  303. static int qbat_get_energy_empty(union power_supply_propval *val)
  304. {
  305. val->intval = 0;
  306. return 0;
  307. }
  308. static void qbat_init_get_charge_full(void)
  309. {
  310. u16 charge = QCIBAT_DEFAULT_CHARGE_FULL_CAPACITY;
  311. mutex_lock(&qci_transaction_lock);
  312. if (read_data_from_battery(BATTERY_FULL_CAPACITY,
  313. SMBUS_READ_WORD_PRTCL) == SMBUS_DONE) {
  314. charge = get_bat_info(ECRAM_SMB_DATA1);
  315. charge = charge << 8;
  316. charge |= get_bat_info(ECRAM_SMB_DATA0);
  317. }
  318. mutex_unlock(&qci_transaction_lock);
  319. context.charge_full = charge;
  320. }
  321. static void qbat_init_get_charge_full_design(void)
  322. {
  323. u16 charge = QCIBAT_DEFAULT_CHARGE_FULL_DESIGN;
  324. mutex_lock(&qci_transaction_lock);
  325. if (read_data_from_battery(BATTERY_DESIGN_CAPACITY,
  326. SMBUS_READ_WORD_PRTCL) == SMBUS_DONE) {
  327. charge = get_bat_info(ECRAM_SMB_DATA1);
  328. charge = charge << 8;
  329. charge |= get_bat_info(ECRAM_SMB_DATA0);
  330. }
  331. mutex_unlock(&qci_transaction_lock);
  332. context.charge_full_design = charge;
  333. }
  334. static void qbat_init_get_voltage_full_design(void)
  335. {
  336. u16 voltage = QCIBAT_DEFAULT_VOLTAGE_DESIGN;
  337. mutex_lock(&qci_transaction_lock);
  338. if (read_data_from_battery(BATTERY_DESIGN_VOLTAGE,
  339. SMBUS_READ_WORD_PRTCL) == SMBUS_DONE) {
  340. voltage = get_bat_info(ECRAM_SMB_DATA1);
  341. voltage = voltage << 8;
  342. voltage |= get_bat_info(ECRAM_SMB_DATA0);
  343. }
  344. mutex_unlock(&qci_transaction_lock);
  345. context.voltage_full_design = voltage;
  346. }
  347. static void qbat_init_get_manufacturer_name(void)
  348. {
  349. u8 size;
  350. u8 i;
  351. int rc;
  352. mutex_lock(&qci_transaction_lock);
  353. rc = read_data_from_battery(BATTERY_MANUFACTURE_NAME,
  354. SMBUS_READ_BLOCK_PRTCL);
  355. if (rc == SMBUS_DONE) {
  356. size = min(get_bat_info(ECRAM_SMB_BCNT), QCIBAT_STRING_SIZE);
  357. for (i = 0; i < size; i++) {
  358. context.manufacturer_name[i] =
  359. get_bat_info(ECRAM_SMB_DATA_START + i);
  360. }
  361. } else
  362. strcpy(context.manufacturer_name, "Unknown");
  363. mutex_unlock(&qci_transaction_lock);
  364. }
  365. static void qbat_init_get_serial_number(void)
  366. {
  367. u8 size;
  368. u8 i;
  369. int rc;
  370. mutex_lock(&qci_transaction_lock);
  371. rc = read_data_from_battery(BATTERY_SERIAL_NUMBER,
  372. SMBUS_READ_BLOCK_PRTCL);
  373. if (rc == SMBUS_DONE) {
  374. size = min(get_bat_info(ECRAM_SMB_BCNT), QCIBAT_STRING_SIZE);
  375. for (i = 0; i < size; i++) {
  376. context.serial_number[i] =
  377. get_bat_info(ECRAM_SMB_DATA_START + i);
  378. }
  379. } else
  380. strcpy(context.serial_number, "Unknown");
  381. mutex_unlock(&qci_transaction_lock);
  382. }
  383. static void init_battery_stats(void)
  384. {
  385. int i;
  386. context.battery_state = get_bat_info(ECRAM_BATTERY_STATUS);
  387. if (!(context.battery_state & MAIN_BATTERY_STATUS_BAT_IN))
  388. return;
  389. /* EC bug? needs some initial priming */
  390. for (i = 0; i < 5; i++) {
  391. read_data_from_battery(BATTERY_DESIGN_CAPACITY,
  392. SMBUS_READ_WORD_PRTCL);
  393. }
  394. qbat_init_get_charge_full_design();
  395. qbat_init_get_charge_full();
  396. qbat_init_get_voltage_full_design();
  397. context.energy_full = context.voltage_full_design *
  398. context.charge_full;
  399. qbat_init_get_serial_number();
  400. qbat_init_get_manufacturer_name();
  401. }
  402. /*********************************************************************
  403. * Battery properties
  404. *********************************************************************/
  405. static int qbat_get_property(struct power_supply *psy,
  406. enum power_supply_property psp,
  407. union power_supply_propval *val)
  408. {
  409. int ret = 0;
  410. switch (psp) {
  411. case POWER_SUPPLY_PROP_STATUS:
  412. ret = qbat_get_status(val);
  413. break;
  414. case POWER_SUPPLY_PROP_PRESENT:
  415. ret = qbat_get_present(val);
  416. break;
  417. case POWER_SUPPLY_PROP_HEALTH:
  418. ret = qbat_get_health(val);
  419. break;
  420. case POWER_SUPPLY_PROP_MANUFACTURER:
  421. ret = qbat_get_manufacturer_name(val);
  422. break;
  423. case POWER_SUPPLY_PROP_TECHNOLOGY:
  424. ret = qbat_get_technology(val);
  425. break;
  426. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  427. ret = qbat_get_voltage_avg(val);
  428. break;
  429. case POWER_SUPPLY_PROP_CURRENT_AVG:
  430. ret = qbat_get_current_avg(val);
  431. break;
  432. case POWER_SUPPLY_PROP_CAPACITY:
  433. ret = qbat_get_capacity(val);
  434. break;
  435. case POWER_SUPPLY_PROP_TEMP:
  436. ret = qbat_get_temp_avg(val);
  437. break;
  438. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  439. ret = qbat_get_charge_full_design(val);
  440. break;
  441. case POWER_SUPPLY_PROP_CHARGE_FULL:
  442. ret = qbat_get_charge_full(val);
  443. break;
  444. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  445. ret = qbat_get_charge_counter(val);
  446. break;
  447. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  448. ret = qbat_get_time_empty_avg(val);
  449. break;
  450. case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
  451. ret = qbat_get_time_full_avg(val);
  452. break;
  453. case POWER_SUPPLY_PROP_MODEL_NAME:
  454. ret = qbat_get_model_name(val);
  455. break;
  456. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  457. ret = qbat_get_serial_number(val);
  458. break;
  459. case POWER_SUPPLY_PROP_ENERGY_NOW:
  460. ret = qbat_get_energy_now(val);
  461. break;
  462. case POWER_SUPPLY_PROP_ENERGY_FULL:
  463. ret = qbat_get_energy_full(val);
  464. break;
  465. case POWER_SUPPLY_PROP_ENERGY_EMPTY:
  466. ret = qbat_get_energy_empty(val);
  467. break;
  468. default:
  469. ret = -EINVAL;
  470. break;
  471. }
  472. return ret;
  473. }
  474. /*********************************************************************
  475. * Initialisation
  476. *********************************************************************/
  477. static struct power_supply qci_ac = {
  478. .name = "ac",
  479. .type = POWER_SUPPLY_TYPE_MAINS,
  480. .properties = qci_ac_props,
  481. .num_properties = ARRAY_SIZE(qci_ac_props),
  482. .get_property = qci_ac_get_prop,
  483. };
  484. static struct power_supply qci_bat = {
  485. .name = "battery",
  486. .type = POWER_SUPPLY_TYPE_BATTERY,
  487. .properties = qci_bat_props,
  488. .num_properties = ARRAY_SIZE(qci_bat_props),
  489. .get_property = qbat_get_property,
  490. .use_for_apm = 1,
  491. };
  492. static irqreturn_t qbat_interrupt(int irq, void *dev_id)
  493. {
  494. struct i2cbat_drv_data *ibat_drv_data = dev_id;
  495. schedule_work(&ibat_drv_data->work);
  496. return IRQ_HANDLED;
  497. }
  498. static void qbat_work(struct work_struct *_work)
  499. {
  500. u8 status;
  501. status = get_bat_info(ECRAM_BATTERY_EVENTS);
  502. if (status & EC_EVENT_AC) {
  503. context.battery_state = get_bat_info(ECRAM_BATTERY_STATUS);
  504. power_supply_changed(&qci_ac);
  505. }
  506. if (status & (EC_EVENT_BATTERY | EC_EVENT_CHARGER | EC_EVENT_TIMER)) {
  507. context.battery_state = get_bat_info(ECRAM_BATTERY_STATUS);
  508. power_supply_changed(&qci_bat);
  509. if (status & EC_EVENT_BATTERY)
  510. init_battery_stats();
  511. }
  512. }
  513. static struct platform_device *bat_pdev;
  514. static int __init qbat_init(void)
  515. {
  516. int err = 0;
  517. mutex_init(&qci_i2c_lock);
  518. mutex_init(&qci_transaction_lock);
  519. context.bi2c_client = wpce_get_i2c_client();
  520. if (context.bi2c_client == NULL)
  521. return -1;
  522. i2c_set_clientdata(context.bi2c_client, &context);
  523. context.qcibat_gpio = context.bi2c_client->irq;
  524. /*battery device register*/
  525. bat_pdev = platform_device_register_simple("battery", 0, NULL, 0);
  526. if (IS_ERR(bat_pdev))
  527. return PTR_ERR(bat_pdev);
  528. err = power_supply_register(&bat_pdev->dev, &qci_ac);
  529. if (err)
  530. goto ac_failed;
  531. qci_bat.name = bat_pdev->name;
  532. err = power_supply_register(&bat_pdev->dev, &qci_bat);
  533. if (err)
  534. goto battery_failed;
  535. /*battery irq configure*/
  536. INIT_WORK(&context.work, qbat_work);
  537. err = gpio_request(context.qcibat_gpio, "qci-bat");
  538. if (err) {
  539. dev_err(&context.bi2c_client->dev,
  540. "[BAT] err gpio request\n");
  541. goto gpio_request_fail;
  542. }
  543. context.qcibat_irq = gpio_to_irq(context.qcibat_gpio);
  544. err = request_irq(context.qcibat_irq, qbat_interrupt,
  545. IRQF_TRIGGER_FALLING, BATTERY_ID_NAME, &context);
  546. if (err) {
  547. dev_err(&context.bi2c_client->dev,
  548. "[BAT] unable to get IRQ\n");
  549. goto request_irq_fail;
  550. }
  551. init_battery_stats();
  552. goto success;
  553. request_irq_fail:
  554. gpio_free(context.qcibat_gpio);
  555. gpio_request_fail:
  556. power_supply_unregister(&qci_bat);
  557. battery_failed:
  558. power_supply_unregister(&qci_ac);
  559. ac_failed:
  560. platform_device_unregister(bat_pdev);
  561. i2c_set_clientdata(context.bi2c_client, NULL);
  562. success:
  563. return err;
  564. }
  565. static void __exit qbat_exit(void)
  566. {
  567. free_irq(context.qcibat_irq, &context);
  568. gpio_free(context.qcibat_gpio);
  569. power_supply_unregister(&qci_bat);
  570. power_supply_unregister(&qci_ac);
  571. platform_device_unregister(bat_pdev);
  572. i2c_set_clientdata(context.bi2c_client, NULL);
  573. }
  574. late_initcall(qbat_init);
  575. module_exit(qbat_exit);
  576. MODULE_AUTHOR("Quanta Computer Inc.");
  577. MODULE_DESCRIPTION("Quanta Embedded Controller I2C Battery Driver");
  578. MODULE_LICENSE("GPL v2");