intel_mid_battery.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * intel_mid_battery.c - Intel MID PMIC Battery Driver
  3. *
  4. * Copyright (C) 2009 Intel Corporation
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20. *
  21. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. * Author: Nithish Mahalingam <nithish.mahalingam@intel.com>
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/err.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/param.h>
  31. #include <linux/device.h>
  32. #include <linux/spi/spi.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/power_supply.h>
  35. #include <asm/intel_scu_ipc.h>
  36. #define DRIVER_NAME "pmic_battery"
  37. /*********************************************************************
  38. * Generic defines
  39. *********************************************************************/
  40. static int debug;
  41. module_param(debug, int, 0444);
  42. MODULE_PARM_DESC(debug, "Flag to enable PMIC Battery debug messages.");
  43. #define PMIC_BATT_DRV_INFO_UPDATED 1
  44. #define PMIC_BATT_PRESENT 1
  45. #define PMIC_BATT_NOT_PRESENT 0
  46. #define PMIC_USB_PRESENT PMIC_BATT_PRESENT
  47. #define PMIC_USB_NOT_PRESENT PMIC_BATT_NOT_PRESENT
  48. /* pmic battery register related */
  49. #define PMIC_BATT_CHR_SCHRGINT_ADDR 0xD2
  50. #define PMIC_BATT_CHR_SBATOVP_MASK (1 << 1)
  51. #define PMIC_BATT_CHR_STEMP_MASK (1 << 2)
  52. #define PMIC_BATT_CHR_SCOMP_MASK (1 << 3)
  53. #define PMIC_BATT_CHR_SUSBDET_MASK (1 << 4)
  54. #define PMIC_BATT_CHR_SBATDET_MASK (1 << 5)
  55. #define PMIC_BATT_CHR_SDCLMT_MASK (1 << 6)
  56. #define PMIC_BATT_CHR_SUSBOVP_MASK (1 << 7)
  57. #define PMIC_BATT_CHR_EXCPT_MASK 0xC6
  58. #define PMIC_BATT_ADC_ACCCHRG_MASK (1 << 31)
  59. #define PMIC_BATT_ADC_ACCCHRGVAL_MASK 0x7FFFFFFF
  60. /* pmic ipc related */
  61. #define PMIC_BATT_CHR_IPC_FCHRG_SUBID 0x4
  62. #define PMIC_BATT_CHR_IPC_TCHRG_SUBID 0x6
  63. /* types of battery charging */
  64. enum batt_charge_type {
  65. BATT_USBOTG_500MA_CHARGE,
  66. BATT_USBOTG_TRICKLE_CHARGE,
  67. };
  68. /* valid battery events */
  69. enum batt_event {
  70. BATT_EVENT_BATOVP_EXCPT,
  71. BATT_EVENT_USBOVP_EXCPT,
  72. BATT_EVENT_TEMP_EXCPT,
  73. BATT_EVENT_DCLMT_EXCPT,
  74. BATT_EVENT_EXCPT
  75. };
  76. /*********************************************************************
  77. * Battery properties
  78. *********************************************************************/
  79. /*
  80. * pmic battery info
  81. */
  82. struct pmic_power_module_info {
  83. bool is_dev_info_updated;
  84. struct device *dev;
  85. /* pmic battery data */
  86. unsigned long update_time; /* jiffies when data read */
  87. unsigned int usb_is_present;
  88. unsigned int batt_is_present;
  89. unsigned int batt_health;
  90. unsigned int usb_health;
  91. unsigned int batt_status;
  92. unsigned int batt_charge_now; /* in mAS */
  93. unsigned int batt_prev_charge_full; /* in mAS */
  94. unsigned int batt_charge_rate; /* in units per second */
  95. struct power_supply usb;
  96. struct power_supply batt;
  97. int irq; /* GPE_ID or IRQ# */
  98. struct workqueue_struct *monitor_wqueue;
  99. struct delayed_work monitor_battery;
  100. struct work_struct handler;
  101. };
  102. static unsigned int delay_time = 2000; /* in ms */
  103. /*
  104. * pmic ac properties
  105. */
  106. static enum power_supply_property pmic_usb_props[] = {
  107. POWER_SUPPLY_PROP_PRESENT,
  108. POWER_SUPPLY_PROP_HEALTH,
  109. };
  110. /*
  111. * pmic battery properties
  112. */
  113. static enum power_supply_property pmic_battery_props[] = {
  114. POWER_SUPPLY_PROP_STATUS,
  115. POWER_SUPPLY_PROP_HEALTH,
  116. POWER_SUPPLY_PROP_PRESENT,
  117. POWER_SUPPLY_PROP_CHARGE_NOW,
  118. POWER_SUPPLY_PROP_CHARGE_FULL,
  119. };
  120. /*
  121. * Glue functions for talking to the IPC
  122. */
  123. struct battery_property {
  124. u32 capacity; /* Charger capacity */
  125. u8 crnt; /* Quick charge current value*/
  126. u8 volt; /* Fine adjustment of constant charge voltage */
  127. u8 prot; /* CHRGPROT register value */
  128. u8 prot2; /* CHRGPROT1 register value */
  129. u8 timer; /* Charging timer */
  130. };
  131. #define IPCMSG_BATTERY 0xEF
  132. /* Battery coulomb counter accumulator commands */
  133. #define IPC_CMD_CC_WR 0 /* Update coulomb counter value */
  134. #define IPC_CMD_CC_RD 1 /* Read coulomb counter value */
  135. #define IPC_CMD_BATTERY_PROPERTY 2 /* Read Battery property */
  136. /**
  137. * pmic_scu_ipc_battery_cc_read - read battery cc
  138. * @value: battery coulomb counter read
  139. *
  140. * Reads the battery couloumb counter value, returns 0 on success, or
  141. * an error code
  142. *
  143. * This function may sleep. Locking for SCU accesses is handled for
  144. * the caller.
  145. */
  146. static int pmic_scu_ipc_battery_cc_read(u32 *value)
  147. {
  148. return intel_scu_ipc_command(IPCMSG_BATTERY, IPC_CMD_CC_RD,
  149. NULL, 0, value, 1);
  150. }
  151. /**
  152. * pmic_scu_ipc_battery_property_get - fetch properties
  153. * @prop: battery properties
  154. *
  155. * Retrieve the battery properties from the power management
  156. *
  157. * This function may sleep. Locking for SCU accesses is handled for
  158. * the caller.
  159. */
  160. static int pmic_scu_ipc_battery_property_get(struct battery_property *prop)
  161. {
  162. u32 data[3];
  163. u8 *p = (u8 *)&data[1];
  164. int err = intel_scu_ipc_command(IPCMSG_BATTERY,
  165. IPC_CMD_BATTERY_PROPERTY, NULL, 0, data, 3);
  166. prop->capacity = data[0];
  167. prop->crnt = *p++;
  168. prop->volt = *p++;
  169. prop->prot = *p++;
  170. prop->prot2 = *p++;
  171. prop->timer = *p++;
  172. return err;
  173. }
  174. /**
  175. * pmic_scu_ipc_set_charger - set charger
  176. * @charger: charger to select
  177. *
  178. * Switch the charging mode for the SCU
  179. */
  180. static int pmic_scu_ipc_set_charger(int charger)
  181. {
  182. return intel_scu_ipc_simple_command(IPCMSG_BATTERY, charger);
  183. }
  184. /**
  185. * pmic_battery_log_event - log battery events
  186. * @event: battery event to be logged
  187. * Context: can sleep
  188. *
  189. * There are multiple battery events which may be of interest to users;
  190. * this battery function logs the different battery events onto the
  191. * kernel log messages.
  192. */
  193. static void pmic_battery_log_event(enum batt_event event)
  194. {
  195. printk(KERN_WARNING "pmic-battery: ");
  196. switch (event) {
  197. case BATT_EVENT_BATOVP_EXCPT:
  198. printk(KERN_CONT "battery overvoltage condition\n");
  199. break;
  200. case BATT_EVENT_USBOVP_EXCPT:
  201. printk(KERN_CONT "usb charger overvoltage condition\n");
  202. break;
  203. case BATT_EVENT_TEMP_EXCPT:
  204. printk(KERN_CONT "high battery temperature condition\n");
  205. break;
  206. case BATT_EVENT_DCLMT_EXCPT:
  207. printk(KERN_CONT "over battery charge current condition\n");
  208. break;
  209. default:
  210. printk(KERN_CONT "charger/battery exception %d\n", event);
  211. break;
  212. }
  213. }
  214. /**
  215. * pmic_battery_read_status - read battery status information
  216. * @pbi: device info structure to update the read information
  217. * Context: can sleep
  218. *
  219. * PMIC power source information need to be updated based on the data read
  220. * from the PMIC battery registers.
  221. *
  222. */
  223. static void pmic_battery_read_status(struct pmic_power_module_info *pbi)
  224. {
  225. unsigned int update_time_intrvl;
  226. unsigned int chrg_val;
  227. u32 ccval;
  228. u8 r8;
  229. struct battery_property batt_prop;
  230. int batt_present = 0;
  231. int usb_present = 0;
  232. int batt_exception = 0;
  233. /* make sure the last batt_status read happened delay_time before */
  234. if (pbi->update_time && time_before(jiffies, pbi->update_time +
  235. msecs_to_jiffies(delay_time)))
  236. return;
  237. update_time_intrvl = jiffies_to_msecs(jiffies - pbi->update_time);
  238. pbi->update_time = jiffies;
  239. /* read coulomb counter registers and schrgint register */
  240. if (pmic_scu_ipc_battery_cc_read(&ccval)) {
  241. dev_warn(pbi->dev, "%s(): ipc config cmd failed\n",
  242. __func__);
  243. return;
  244. }
  245. if (intel_scu_ipc_ioread8(PMIC_BATT_CHR_SCHRGINT_ADDR, &r8)) {
  246. dev_warn(pbi->dev, "%s(): ipc pmic read failed\n",
  247. __func__);
  248. return;
  249. }
  250. /*
  251. * set pmic_power_module_info members based on pmic register values
  252. * read.
  253. */
  254. /* set batt_is_present */
  255. if (r8 & PMIC_BATT_CHR_SBATDET_MASK) {
  256. pbi->batt_is_present = PMIC_BATT_PRESENT;
  257. batt_present = 1;
  258. } else {
  259. pbi->batt_is_present = PMIC_BATT_NOT_PRESENT;
  260. pbi->batt_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  261. pbi->batt_status = POWER_SUPPLY_STATUS_UNKNOWN;
  262. }
  263. /* set batt_health */
  264. if (batt_present) {
  265. if (r8 & PMIC_BATT_CHR_SBATOVP_MASK) {
  266. pbi->batt_health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  267. pbi->batt_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  268. pmic_battery_log_event(BATT_EVENT_BATOVP_EXCPT);
  269. batt_exception = 1;
  270. } else if (r8 & PMIC_BATT_CHR_SDCLMT_MASK) {
  271. pbi->batt_health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  272. pbi->batt_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  273. pmic_battery_log_event(BATT_EVENT_DCLMT_EXCPT);
  274. batt_exception = 1;
  275. } else if (r8 & PMIC_BATT_CHR_STEMP_MASK) {
  276. pbi->batt_health = POWER_SUPPLY_HEALTH_OVERHEAT;
  277. pbi->batt_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  278. pmic_battery_log_event(BATT_EVENT_TEMP_EXCPT);
  279. batt_exception = 1;
  280. } else {
  281. pbi->batt_health = POWER_SUPPLY_HEALTH_GOOD;
  282. }
  283. }
  284. /* set usb_is_present */
  285. if (r8 & PMIC_BATT_CHR_SUSBDET_MASK) {
  286. pbi->usb_is_present = PMIC_USB_PRESENT;
  287. usb_present = 1;
  288. } else {
  289. pbi->usb_is_present = PMIC_USB_NOT_PRESENT;
  290. pbi->usb_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  291. }
  292. if (usb_present) {
  293. if (r8 & PMIC_BATT_CHR_SUSBOVP_MASK) {
  294. pbi->usb_health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  295. pmic_battery_log_event(BATT_EVENT_USBOVP_EXCPT);
  296. } else {
  297. pbi->usb_health = POWER_SUPPLY_HEALTH_GOOD;
  298. }
  299. }
  300. chrg_val = ccval & PMIC_BATT_ADC_ACCCHRGVAL_MASK;
  301. /* set batt_prev_charge_full to battery capacity the first time */
  302. if (!pbi->is_dev_info_updated) {
  303. if (pmic_scu_ipc_battery_property_get(&batt_prop)) {
  304. dev_warn(pbi->dev, "%s(): ipc config cmd failed\n",
  305. __func__);
  306. return;
  307. }
  308. pbi->batt_prev_charge_full = batt_prop.capacity;
  309. }
  310. /* set batt_status */
  311. if (batt_present && !batt_exception) {
  312. if (r8 & PMIC_BATT_CHR_SCOMP_MASK) {
  313. pbi->batt_status = POWER_SUPPLY_STATUS_FULL;
  314. pbi->batt_prev_charge_full = chrg_val;
  315. } else if (ccval & PMIC_BATT_ADC_ACCCHRG_MASK) {
  316. pbi->batt_status = POWER_SUPPLY_STATUS_DISCHARGING;
  317. } else {
  318. pbi->batt_status = POWER_SUPPLY_STATUS_CHARGING;
  319. }
  320. }
  321. /* set batt_charge_rate */
  322. if (pbi->is_dev_info_updated && batt_present && !batt_exception) {
  323. if (pbi->batt_status == POWER_SUPPLY_STATUS_DISCHARGING) {
  324. if (pbi->batt_charge_now - chrg_val) {
  325. pbi->batt_charge_rate = ((pbi->batt_charge_now -
  326. chrg_val) * 1000 * 60) /
  327. update_time_intrvl;
  328. }
  329. } else if (pbi->batt_status == POWER_SUPPLY_STATUS_CHARGING) {
  330. if (chrg_val - pbi->batt_charge_now) {
  331. pbi->batt_charge_rate = ((chrg_val -
  332. pbi->batt_charge_now) * 1000 * 60) /
  333. update_time_intrvl;
  334. }
  335. } else
  336. pbi->batt_charge_rate = 0;
  337. } else {
  338. pbi->batt_charge_rate = -1;
  339. }
  340. /* batt_charge_now */
  341. if (batt_present && !batt_exception)
  342. pbi->batt_charge_now = chrg_val;
  343. else
  344. pbi->batt_charge_now = -1;
  345. pbi->is_dev_info_updated = PMIC_BATT_DRV_INFO_UPDATED;
  346. }
  347. /**
  348. * pmic_usb_get_property - usb power source get property
  349. * @psy: usb power supply context
  350. * @psp: usb power source property
  351. * @val: usb power source property value
  352. * Context: can sleep
  353. *
  354. * PMIC usb power source property needs to be provided to power_supply
  355. * subsytem for it to provide the information to users.
  356. */
  357. static int pmic_usb_get_property(struct power_supply *psy,
  358. enum power_supply_property psp,
  359. union power_supply_propval *val)
  360. {
  361. struct pmic_power_module_info *pbi = container_of(psy,
  362. struct pmic_power_module_info, usb);
  363. /* update pmic_power_module_info members */
  364. pmic_battery_read_status(pbi);
  365. switch (psp) {
  366. case POWER_SUPPLY_PROP_PRESENT:
  367. val->intval = pbi->usb_is_present;
  368. break;
  369. case POWER_SUPPLY_PROP_HEALTH:
  370. val->intval = pbi->usb_health;
  371. break;
  372. default:
  373. return -EINVAL;
  374. }
  375. return 0;
  376. }
  377. static inline unsigned long mAStouAh(unsigned long v)
  378. {
  379. /* seconds to hours, mA to µA */
  380. return (v * 1000) / 3600;
  381. }
  382. /**
  383. * pmic_battery_get_property - battery power source get property
  384. * @psy: battery power supply context
  385. * @psp: battery power source property
  386. * @val: battery power source property value
  387. * Context: can sleep
  388. *
  389. * PMIC battery power source property needs to be provided to power_supply
  390. * subsytem for it to provide the information to users.
  391. */
  392. static int pmic_battery_get_property(struct power_supply *psy,
  393. enum power_supply_property psp,
  394. union power_supply_propval *val)
  395. {
  396. struct pmic_power_module_info *pbi = container_of(psy,
  397. struct pmic_power_module_info, batt);
  398. /* update pmic_power_module_info members */
  399. pmic_battery_read_status(pbi);
  400. switch (psp) {
  401. case POWER_SUPPLY_PROP_STATUS:
  402. val->intval = pbi->batt_status;
  403. break;
  404. case POWER_SUPPLY_PROP_HEALTH:
  405. val->intval = pbi->batt_health;
  406. break;
  407. case POWER_SUPPLY_PROP_PRESENT:
  408. val->intval = pbi->batt_is_present;
  409. break;
  410. case POWER_SUPPLY_PROP_CHARGE_NOW:
  411. val->intval = mAStouAh(pbi->batt_charge_now);
  412. break;
  413. case POWER_SUPPLY_PROP_CHARGE_FULL:
  414. val->intval = mAStouAh(pbi->batt_prev_charge_full);
  415. break;
  416. default:
  417. return -EINVAL;
  418. }
  419. return 0;
  420. }
  421. /**
  422. * pmic_battery_monitor - monitor battery status
  423. * @work: work structure
  424. * Context: can sleep
  425. *
  426. * PMIC battery status needs to be monitored for any change
  427. * and information needs to be frequently updated.
  428. */
  429. static void pmic_battery_monitor(struct work_struct *work)
  430. {
  431. struct pmic_power_module_info *pbi = container_of(work,
  432. struct pmic_power_module_info, monitor_battery.work);
  433. /* update pmic_power_module_info members */
  434. pmic_battery_read_status(pbi);
  435. queue_delayed_work(pbi->monitor_wqueue, &pbi->monitor_battery, HZ * 10);
  436. }
  437. /**
  438. * pmic_battery_set_charger - set battery charger
  439. * @pbi: device info structure
  440. * @chrg: charge mode to set battery charger in
  441. * Context: can sleep
  442. *
  443. * PMIC battery charger needs to be enabled based on the usb charge
  444. * capabilities connected to the platform.
  445. */
  446. static int pmic_battery_set_charger(struct pmic_power_module_info *pbi,
  447. enum batt_charge_type chrg)
  448. {
  449. int retval;
  450. /* set usblmt bits and chrgcntl register bits appropriately */
  451. switch (chrg) {
  452. case BATT_USBOTG_500MA_CHARGE:
  453. retval = pmic_scu_ipc_set_charger(PMIC_BATT_CHR_IPC_FCHRG_SUBID);
  454. break;
  455. case BATT_USBOTG_TRICKLE_CHARGE:
  456. retval = pmic_scu_ipc_set_charger(PMIC_BATT_CHR_IPC_TCHRG_SUBID);
  457. break;
  458. default:
  459. dev_warn(pbi->dev, "%s(): out of range usb charger "
  460. "charge detected\n", __func__);
  461. return -EINVAL;
  462. }
  463. if (retval) {
  464. dev_warn(pbi->dev, "%s(): ipc pmic read failed\n",
  465. __func__);
  466. return retval;;
  467. }
  468. return 0;
  469. }
  470. /**
  471. * pmic_battery_interrupt_handler - pmic battery interrupt handler
  472. * Context: interrupt context
  473. *
  474. * PMIC battery interrupt handler which will be called with either
  475. * battery full condition occurs or usb otg & battery connect
  476. * condition occurs.
  477. */
  478. static irqreturn_t pmic_battery_interrupt_handler(int id, void *dev)
  479. {
  480. struct pmic_power_module_info *pbi = dev;
  481. schedule_work(&pbi->handler);
  482. return IRQ_HANDLED;
  483. }
  484. /**
  485. * pmic_battery_handle_intrpt - pmic battery service interrupt
  486. * @work: work structure
  487. * Context: can sleep
  488. *
  489. * PMIC battery needs to either update the battery status as full
  490. * if it detects battery full condition caused the interrupt or needs
  491. * to enable battery charger if it detects usb and battery detect
  492. * caused the source of interrupt.
  493. */
  494. static void pmic_battery_handle_intrpt(struct work_struct *work)
  495. {
  496. struct pmic_power_module_info *pbi = container_of(work,
  497. struct pmic_power_module_info, handler);
  498. enum batt_charge_type chrg;
  499. u8 r8;
  500. if (intel_scu_ipc_ioread8(PMIC_BATT_CHR_SCHRGINT_ADDR, &r8)) {
  501. dev_warn(pbi->dev, "%s(): ipc pmic read failed\n",
  502. __func__);
  503. return;
  504. }
  505. /* find the cause of the interrupt */
  506. if (r8 & PMIC_BATT_CHR_SBATDET_MASK) {
  507. pbi->batt_is_present = PMIC_BATT_PRESENT;
  508. } else {
  509. pbi->batt_is_present = PMIC_BATT_NOT_PRESENT;
  510. pbi->batt_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  511. pbi->batt_status = POWER_SUPPLY_STATUS_UNKNOWN;
  512. return;
  513. }
  514. if (r8 & PMIC_BATT_CHR_EXCPT_MASK) {
  515. pbi->batt_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  516. pbi->batt_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  517. pbi->usb_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  518. pmic_battery_log_event(BATT_EVENT_EXCPT);
  519. return;
  520. } else {
  521. pbi->batt_health = POWER_SUPPLY_HEALTH_GOOD;
  522. pbi->usb_health = POWER_SUPPLY_HEALTH_GOOD;
  523. }
  524. if (r8 & PMIC_BATT_CHR_SCOMP_MASK) {
  525. u32 ccval;
  526. pbi->batt_status = POWER_SUPPLY_STATUS_FULL;
  527. if (pmic_scu_ipc_battery_cc_read(&ccval)) {
  528. dev_warn(pbi->dev, "%s(): ipc config cmd "
  529. "failed\n", __func__);
  530. return;
  531. }
  532. pbi->batt_prev_charge_full = ccval &
  533. PMIC_BATT_ADC_ACCCHRGVAL_MASK;
  534. return;
  535. }
  536. if (r8 & PMIC_BATT_CHR_SUSBDET_MASK) {
  537. pbi->usb_is_present = PMIC_USB_PRESENT;
  538. } else {
  539. pbi->usb_is_present = PMIC_USB_NOT_PRESENT;
  540. pbi->usb_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  541. return;
  542. }
  543. /* setup battery charging */
  544. #if 0
  545. /* check usb otg power capability and set charger accordingly */
  546. retval = langwell_udc_maxpower(&power);
  547. if (retval) {
  548. dev_warn(pbi->dev,
  549. "%s(): usb otg power query failed with error code %d\n",
  550. __func__, retval);
  551. return;
  552. }
  553. if (power >= 500)
  554. chrg = BATT_USBOTG_500MA_CHARGE;
  555. else
  556. #endif
  557. chrg = BATT_USBOTG_TRICKLE_CHARGE;
  558. /* enable battery charging */
  559. if (pmic_battery_set_charger(pbi, chrg)) {
  560. dev_warn(pbi->dev,
  561. "%s(): failed to set up battery charging\n", __func__);
  562. return;
  563. }
  564. dev_dbg(pbi->dev,
  565. "pmic-battery: %s() - setting up battery charger successful\n",
  566. __func__);
  567. }
  568. /**
  569. * pmic_battery_probe - pmic battery initialize
  570. * @irq: pmic battery device irq
  571. * @dev: pmic battery device structure
  572. * Context: can sleep
  573. *
  574. * PMIC battery initializes its internal data structue and other
  575. * infrastructure components for it to work as expected.
  576. */
  577. static __devinit int probe(int irq, struct device *dev)
  578. {
  579. int retval = 0;
  580. struct pmic_power_module_info *pbi;
  581. dev_dbg(dev, "pmic-battery: found pmic battery device\n");
  582. pbi = kzalloc(sizeof(*pbi), GFP_KERNEL);
  583. if (!pbi) {
  584. dev_err(dev, "%s(): memory allocation failed\n",
  585. __func__);
  586. return -ENOMEM;
  587. }
  588. pbi->dev = dev;
  589. pbi->irq = irq;
  590. dev_set_drvdata(dev, pbi);
  591. /* initialize all required framework before enabling interrupts */
  592. INIT_WORK(&pbi->handler, pmic_battery_handle_intrpt);
  593. INIT_DELAYED_WORK(&pbi->monitor_battery, pmic_battery_monitor);
  594. pbi->monitor_wqueue =
  595. create_singlethread_workqueue(dev_name(dev));
  596. if (!pbi->monitor_wqueue) {
  597. dev_err(dev, "%s(): wqueue init failed\n", __func__);
  598. retval = -ESRCH;
  599. goto wqueue_failed;
  600. }
  601. /* register interrupt */
  602. retval = request_irq(pbi->irq, pmic_battery_interrupt_handler,
  603. 0, DRIVER_NAME, pbi);
  604. if (retval) {
  605. dev_err(dev, "%s(): cannot get IRQ\n", __func__);
  606. goto requestirq_failed;
  607. }
  608. /* register pmic-batt with power supply subsystem */
  609. pbi->batt.name = "pmic-batt";
  610. pbi->batt.type = POWER_SUPPLY_TYPE_BATTERY;
  611. pbi->batt.properties = pmic_battery_props;
  612. pbi->batt.num_properties = ARRAY_SIZE(pmic_battery_props);
  613. pbi->batt.get_property = pmic_battery_get_property;
  614. retval = power_supply_register(dev, &pbi->batt);
  615. if (retval) {
  616. dev_err(dev,
  617. "%s(): failed to register pmic battery device with power supply subsystem\n",
  618. __func__);
  619. goto power_reg_failed;
  620. }
  621. dev_dbg(dev, "pmic-battery: %s() - pmic battery device "
  622. "registration with power supply subsystem successful\n",
  623. __func__);
  624. queue_delayed_work(pbi->monitor_wqueue, &pbi->monitor_battery, HZ * 1);
  625. /* register pmic-usb with power supply subsystem */
  626. pbi->usb.name = "pmic-usb";
  627. pbi->usb.type = POWER_SUPPLY_TYPE_USB;
  628. pbi->usb.properties = pmic_usb_props;
  629. pbi->usb.num_properties = ARRAY_SIZE(pmic_usb_props);
  630. pbi->usb.get_property = pmic_usb_get_property;
  631. retval = power_supply_register(dev, &pbi->usb);
  632. if (retval) {
  633. dev_err(dev,
  634. "%s(): failed to register pmic usb device with power supply subsystem\n",
  635. __func__);
  636. goto power_reg_failed_1;
  637. }
  638. if (debug)
  639. printk(KERN_INFO "pmic-battery: %s() - pmic usb device "
  640. "registration with power supply subsystem successful\n",
  641. __func__);
  642. return retval;
  643. power_reg_failed_1:
  644. power_supply_unregister(&pbi->batt);
  645. power_reg_failed:
  646. cancel_delayed_work_sync(&pbi->monitor_battery);
  647. requestirq_failed:
  648. destroy_workqueue(pbi->monitor_wqueue);
  649. wqueue_failed:
  650. kfree(pbi);
  651. return retval;
  652. }
  653. static int __devinit platform_pmic_battery_probe(struct platform_device *pdev)
  654. {
  655. return probe(pdev->id, &pdev->dev);
  656. }
  657. /**
  658. * pmic_battery_remove - pmic battery finalize
  659. * @dev: pmic battery device structure
  660. * Context: can sleep
  661. *
  662. * PMIC battery finalizes its internal data structue and other
  663. * infrastructure components that it initialized in
  664. * pmic_battery_probe.
  665. */
  666. static int __devexit platform_pmic_battery_remove(struct platform_device *pdev)
  667. {
  668. struct pmic_power_module_info *pbi = dev_get_drvdata(&pdev->dev);
  669. free_irq(pbi->irq, pbi);
  670. cancel_delayed_work_sync(&pbi->monitor_battery);
  671. destroy_workqueue(pbi->monitor_wqueue);
  672. power_supply_unregister(&pbi->usb);
  673. power_supply_unregister(&pbi->batt);
  674. cancel_work_sync(&pbi->handler);
  675. kfree(pbi);
  676. return 0;
  677. }
  678. static struct platform_driver platform_pmic_battery_driver = {
  679. .driver = {
  680. .name = DRIVER_NAME,
  681. .owner = THIS_MODULE,
  682. },
  683. .probe = platform_pmic_battery_probe,
  684. .remove = __devexit_p(platform_pmic_battery_remove),
  685. };
  686. static int __init platform_pmic_battery_module_init(void)
  687. {
  688. return platform_driver_register(&platform_pmic_battery_driver);
  689. }
  690. static void __exit platform_pmic_battery_module_exit(void)
  691. {
  692. platform_driver_unregister(&platform_pmic_battery_driver);
  693. }
  694. module_init(platform_pmic_battery_module_init);
  695. module_exit(platform_pmic_battery_module_exit);
  696. MODULE_AUTHOR("Nithish Mahalingam <nithish.mahalingam@intel.com>");
  697. MODULE_DESCRIPTION("Intel Moorestown PMIC Battery Driver");
  698. MODULE_LICENSE("GPL");