intel_mid_battery.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 0x86
  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_STEMP_MASK) {
  271. pbi->batt_health = POWER_SUPPLY_HEALTH_OVERHEAT;
  272. pbi->batt_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  273. pmic_battery_log_event(BATT_EVENT_TEMP_EXCPT);
  274. batt_exception = 1;
  275. } else {
  276. pbi->batt_health = POWER_SUPPLY_HEALTH_GOOD;
  277. if (r8 & PMIC_BATT_CHR_SDCLMT_MASK) {
  278. /* PMIC will change charging current automatically */
  279. pmic_battery_log_event(BATT_EVENT_DCLMT_EXCPT);
  280. }
  281. }
  282. }
  283. /* set usb_is_present */
  284. if (r8 & PMIC_BATT_CHR_SUSBDET_MASK) {
  285. pbi->usb_is_present = PMIC_USB_PRESENT;
  286. usb_present = 1;
  287. } else {
  288. pbi->usb_is_present = PMIC_USB_NOT_PRESENT;
  289. pbi->usb_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  290. }
  291. if (usb_present) {
  292. if (r8 & PMIC_BATT_CHR_SUSBOVP_MASK) {
  293. pbi->usb_health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  294. pmic_battery_log_event(BATT_EVENT_USBOVP_EXCPT);
  295. } else {
  296. pbi->usb_health = POWER_SUPPLY_HEALTH_GOOD;
  297. }
  298. }
  299. chrg_val = ccval & PMIC_BATT_ADC_ACCCHRGVAL_MASK;
  300. /* set batt_prev_charge_full to battery capacity the first time */
  301. if (!pbi->is_dev_info_updated) {
  302. if (pmic_scu_ipc_battery_property_get(&batt_prop)) {
  303. dev_warn(pbi->dev, "%s(): ipc config cmd failed\n",
  304. __func__);
  305. return;
  306. }
  307. pbi->batt_prev_charge_full = batt_prop.capacity;
  308. }
  309. /* set batt_status */
  310. if (batt_present && !batt_exception) {
  311. if (r8 & PMIC_BATT_CHR_SCOMP_MASK) {
  312. pbi->batt_status = POWER_SUPPLY_STATUS_FULL;
  313. pbi->batt_prev_charge_full = chrg_val;
  314. } else if (ccval & PMIC_BATT_ADC_ACCCHRG_MASK) {
  315. pbi->batt_status = POWER_SUPPLY_STATUS_DISCHARGING;
  316. } else {
  317. pbi->batt_status = POWER_SUPPLY_STATUS_CHARGING;
  318. }
  319. }
  320. /* set batt_charge_rate */
  321. if (pbi->is_dev_info_updated && batt_present && !batt_exception) {
  322. if (pbi->batt_status == POWER_SUPPLY_STATUS_DISCHARGING) {
  323. if (pbi->batt_charge_now - chrg_val) {
  324. pbi->batt_charge_rate = ((pbi->batt_charge_now -
  325. chrg_val) * 1000 * 60) /
  326. update_time_intrvl;
  327. }
  328. } else if (pbi->batt_status == POWER_SUPPLY_STATUS_CHARGING) {
  329. if (chrg_val - pbi->batt_charge_now) {
  330. pbi->batt_charge_rate = ((chrg_val -
  331. pbi->batt_charge_now) * 1000 * 60) /
  332. update_time_intrvl;
  333. }
  334. } else
  335. pbi->batt_charge_rate = 0;
  336. } else {
  337. pbi->batt_charge_rate = -1;
  338. }
  339. /* batt_charge_now */
  340. if (batt_present && !batt_exception)
  341. pbi->batt_charge_now = chrg_val;
  342. else
  343. pbi->batt_charge_now = -1;
  344. pbi->is_dev_info_updated = PMIC_BATT_DRV_INFO_UPDATED;
  345. }
  346. /**
  347. * pmic_usb_get_property - usb power source get property
  348. * @psy: usb power supply context
  349. * @psp: usb power source property
  350. * @val: usb power source property value
  351. * Context: can sleep
  352. *
  353. * PMIC usb power source property needs to be provided to power_supply
  354. * subsytem for it to provide the information to users.
  355. */
  356. static int pmic_usb_get_property(struct power_supply *psy,
  357. enum power_supply_property psp,
  358. union power_supply_propval *val)
  359. {
  360. struct pmic_power_module_info *pbi = container_of(psy,
  361. struct pmic_power_module_info, usb);
  362. /* update pmic_power_module_info members */
  363. pmic_battery_read_status(pbi);
  364. switch (psp) {
  365. case POWER_SUPPLY_PROP_PRESENT:
  366. val->intval = pbi->usb_is_present;
  367. break;
  368. case POWER_SUPPLY_PROP_HEALTH:
  369. val->intval = pbi->usb_health;
  370. break;
  371. default:
  372. return -EINVAL;
  373. }
  374. return 0;
  375. }
  376. static inline unsigned long mAStouAh(unsigned long v)
  377. {
  378. /* seconds to hours, mA to µA */
  379. return (v * 1000) / 3600;
  380. }
  381. /**
  382. * pmic_battery_get_property - battery power source get property
  383. * @psy: battery power supply context
  384. * @psp: battery power source property
  385. * @val: battery power source property value
  386. * Context: can sleep
  387. *
  388. * PMIC battery power source property needs to be provided to power_supply
  389. * subsytem for it to provide the information to users.
  390. */
  391. static int pmic_battery_get_property(struct power_supply *psy,
  392. enum power_supply_property psp,
  393. union power_supply_propval *val)
  394. {
  395. struct pmic_power_module_info *pbi = container_of(psy,
  396. struct pmic_power_module_info, batt);
  397. /* update pmic_power_module_info members */
  398. pmic_battery_read_status(pbi);
  399. switch (psp) {
  400. case POWER_SUPPLY_PROP_STATUS:
  401. val->intval = pbi->batt_status;
  402. break;
  403. case POWER_SUPPLY_PROP_HEALTH:
  404. val->intval = pbi->batt_health;
  405. break;
  406. case POWER_SUPPLY_PROP_PRESENT:
  407. val->intval = pbi->batt_is_present;
  408. break;
  409. case POWER_SUPPLY_PROP_CHARGE_NOW:
  410. val->intval = mAStouAh(pbi->batt_charge_now);
  411. break;
  412. case POWER_SUPPLY_PROP_CHARGE_FULL:
  413. val->intval = mAStouAh(pbi->batt_prev_charge_full);
  414. break;
  415. default:
  416. return -EINVAL;
  417. }
  418. return 0;
  419. }
  420. /**
  421. * pmic_battery_monitor - monitor battery status
  422. * @work: work structure
  423. * Context: can sleep
  424. *
  425. * PMIC battery status needs to be monitored for any change
  426. * and information needs to be frequently updated.
  427. */
  428. static void pmic_battery_monitor(struct work_struct *work)
  429. {
  430. struct pmic_power_module_info *pbi = container_of(work,
  431. struct pmic_power_module_info, monitor_battery.work);
  432. /* update pmic_power_module_info members */
  433. pmic_battery_read_status(pbi);
  434. queue_delayed_work(pbi->monitor_wqueue, &pbi->monitor_battery, HZ * 10);
  435. }
  436. /**
  437. * pmic_battery_set_charger - set battery charger
  438. * @pbi: device info structure
  439. * @chrg: charge mode to set battery charger in
  440. * Context: can sleep
  441. *
  442. * PMIC battery charger needs to be enabled based on the usb charge
  443. * capabilities connected to the platform.
  444. */
  445. static int pmic_battery_set_charger(struct pmic_power_module_info *pbi,
  446. enum batt_charge_type chrg)
  447. {
  448. int retval;
  449. /* set usblmt bits and chrgcntl register bits appropriately */
  450. switch (chrg) {
  451. case BATT_USBOTG_500MA_CHARGE:
  452. retval = pmic_scu_ipc_set_charger(PMIC_BATT_CHR_IPC_FCHRG_SUBID);
  453. break;
  454. case BATT_USBOTG_TRICKLE_CHARGE:
  455. retval = pmic_scu_ipc_set_charger(PMIC_BATT_CHR_IPC_TCHRG_SUBID);
  456. break;
  457. default:
  458. dev_warn(pbi->dev, "%s(): out of range usb charger "
  459. "charge detected\n", __func__);
  460. return -EINVAL;
  461. }
  462. if (retval) {
  463. dev_warn(pbi->dev, "%s(): ipc pmic read failed\n",
  464. __func__);
  465. return retval;
  466. }
  467. return 0;
  468. }
  469. /**
  470. * pmic_battery_interrupt_handler - pmic battery interrupt handler
  471. * Context: interrupt context
  472. *
  473. * PMIC battery interrupt handler which will be called with either
  474. * battery full condition occurs or usb otg & battery connect
  475. * condition occurs.
  476. */
  477. static irqreturn_t pmic_battery_interrupt_handler(int id, void *dev)
  478. {
  479. struct pmic_power_module_info *pbi = dev;
  480. schedule_work(&pbi->handler);
  481. return IRQ_HANDLED;
  482. }
  483. /**
  484. * pmic_battery_handle_intrpt - pmic battery service interrupt
  485. * @work: work structure
  486. * Context: can sleep
  487. *
  488. * PMIC battery needs to either update the battery status as full
  489. * if it detects battery full condition caused the interrupt or needs
  490. * to enable battery charger if it detects usb and battery detect
  491. * caused the source of interrupt.
  492. */
  493. static void pmic_battery_handle_intrpt(struct work_struct *work)
  494. {
  495. struct pmic_power_module_info *pbi = container_of(work,
  496. struct pmic_power_module_info, handler);
  497. enum batt_charge_type chrg;
  498. u8 r8;
  499. if (intel_scu_ipc_ioread8(PMIC_BATT_CHR_SCHRGINT_ADDR, &r8)) {
  500. dev_warn(pbi->dev, "%s(): ipc pmic read failed\n",
  501. __func__);
  502. return;
  503. }
  504. /* find the cause of the interrupt */
  505. if (r8 & PMIC_BATT_CHR_SBATDET_MASK) {
  506. pbi->batt_is_present = PMIC_BATT_PRESENT;
  507. } else {
  508. pbi->batt_is_present = PMIC_BATT_NOT_PRESENT;
  509. pbi->batt_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  510. pbi->batt_status = POWER_SUPPLY_STATUS_UNKNOWN;
  511. return;
  512. }
  513. if (r8 & PMIC_BATT_CHR_EXCPT_MASK) {
  514. pbi->batt_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  515. pbi->batt_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  516. pbi->usb_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  517. pmic_battery_log_event(BATT_EVENT_EXCPT);
  518. return;
  519. } else {
  520. pbi->batt_health = POWER_SUPPLY_HEALTH_GOOD;
  521. pbi->usb_health = POWER_SUPPLY_HEALTH_GOOD;
  522. }
  523. if (r8 & PMIC_BATT_CHR_SCOMP_MASK) {
  524. u32 ccval;
  525. pbi->batt_status = POWER_SUPPLY_STATUS_FULL;
  526. if (pmic_scu_ipc_battery_cc_read(&ccval)) {
  527. dev_warn(pbi->dev, "%s(): ipc config cmd "
  528. "failed\n", __func__);
  529. return;
  530. }
  531. pbi->batt_prev_charge_full = ccval &
  532. PMIC_BATT_ADC_ACCCHRGVAL_MASK;
  533. return;
  534. }
  535. if (r8 & PMIC_BATT_CHR_SUSBDET_MASK) {
  536. pbi->usb_is_present = PMIC_USB_PRESENT;
  537. } else {
  538. pbi->usb_is_present = PMIC_USB_NOT_PRESENT;
  539. pbi->usb_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  540. return;
  541. }
  542. /* setup battery charging */
  543. #if 0
  544. /* check usb otg power capability and set charger accordingly */
  545. retval = langwell_udc_maxpower(&power);
  546. if (retval) {
  547. dev_warn(pbi->dev,
  548. "%s(): usb otg power query failed with error code %d\n",
  549. __func__, retval);
  550. return;
  551. }
  552. if (power >= 500)
  553. chrg = BATT_USBOTG_500MA_CHARGE;
  554. else
  555. #endif
  556. chrg = BATT_USBOTG_TRICKLE_CHARGE;
  557. /* enable battery charging */
  558. if (pmic_battery_set_charger(pbi, chrg)) {
  559. dev_warn(pbi->dev,
  560. "%s(): failed to set up battery charging\n", __func__);
  561. return;
  562. }
  563. dev_dbg(pbi->dev,
  564. "pmic-battery: %s() - setting up battery charger successful\n",
  565. __func__);
  566. }
  567. /**
  568. * pmic_battery_probe - pmic battery initialize
  569. * @irq: pmic battery device irq
  570. * @dev: pmic battery device structure
  571. * Context: can sleep
  572. *
  573. * PMIC battery initializes its internal data structue and other
  574. * infrastructure components for it to work as expected.
  575. */
  576. static __devinit int probe(int irq, struct device *dev)
  577. {
  578. int retval = 0;
  579. struct pmic_power_module_info *pbi;
  580. dev_dbg(dev, "pmic-battery: found pmic battery device\n");
  581. pbi = kzalloc(sizeof(*pbi), GFP_KERNEL);
  582. if (!pbi) {
  583. dev_err(dev, "%s(): memory allocation failed\n",
  584. __func__);
  585. return -ENOMEM;
  586. }
  587. pbi->dev = dev;
  588. pbi->irq = irq;
  589. dev_set_drvdata(dev, pbi);
  590. /* initialize all required framework before enabling interrupts */
  591. INIT_WORK(&pbi->handler, pmic_battery_handle_intrpt);
  592. INIT_DELAYED_WORK(&pbi->monitor_battery, pmic_battery_monitor);
  593. pbi->monitor_wqueue =
  594. create_singlethread_workqueue(dev_name(dev));
  595. if (!pbi->monitor_wqueue) {
  596. dev_err(dev, "%s(): wqueue init failed\n", __func__);
  597. retval = -ESRCH;
  598. goto wqueue_failed;
  599. }
  600. /* register interrupt */
  601. retval = request_irq(pbi->irq, pmic_battery_interrupt_handler,
  602. 0, DRIVER_NAME, pbi);
  603. if (retval) {
  604. dev_err(dev, "%s(): cannot get IRQ\n", __func__);
  605. goto requestirq_failed;
  606. }
  607. /* register pmic-batt with power supply subsystem */
  608. pbi->batt.name = "pmic-batt";
  609. pbi->batt.type = POWER_SUPPLY_TYPE_BATTERY;
  610. pbi->batt.properties = pmic_battery_props;
  611. pbi->batt.num_properties = ARRAY_SIZE(pmic_battery_props);
  612. pbi->batt.get_property = pmic_battery_get_property;
  613. retval = power_supply_register(dev, &pbi->batt);
  614. if (retval) {
  615. dev_err(dev,
  616. "%s(): failed to register pmic battery device with power supply subsystem\n",
  617. __func__);
  618. goto power_reg_failed;
  619. }
  620. dev_dbg(dev, "pmic-battery: %s() - pmic battery device "
  621. "registration with power supply subsystem successful\n",
  622. __func__);
  623. queue_delayed_work(pbi->monitor_wqueue, &pbi->monitor_battery, HZ * 1);
  624. /* register pmic-usb with power supply subsystem */
  625. pbi->usb.name = "pmic-usb";
  626. pbi->usb.type = POWER_SUPPLY_TYPE_USB;
  627. pbi->usb.properties = pmic_usb_props;
  628. pbi->usb.num_properties = ARRAY_SIZE(pmic_usb_props);
  629. pbi->usb.get_property = pmic_usb_get_property;
  630. retval = power_supply_register(dev, &pbi->usb);
  631. if (retval) {
  632. dev_err(dev,
  633. "%s(): failed to register pmic usb device with power supply subsystem\n",
  634. __func__);
  635. goto power_reg_failed_1;
  636. }
  637. if (debug)
  638. printk(KERN_INFO "pmic-battery: %s() - pmic usb device "
  639. "registration with power supply subsystem successful\n",
  640. __func__);
  641. return retval;
  642. power_reg_failed_1:
  643. power_supply_unregister(&pbi->batt);
  644. power_reg_failed:
  645. cancel_delayed_work_sync(&pbi->monitor_battery);
  646. requestirq_failed:
  647. destroy_workqueue(pbi->monitor_wqueue);
  648. wqueue_failed:
  649. kfree(pbi);
  650. return retval;
  651. }
  652. static int __devinit platform_pmic_battery_probe(struct platform_device *pdev)
  653. {
  654. return probe(pdev->id, &pdev->dev);
  655. }
  656. /**
  657. * pmic_battery_remove - pmic battery finalize
  658. * @dev: pmic battery device structure
  659. * Context: can sleep
  660. *
  661. * PMIC battery finalizes its internal data structue and other
  662. * infrastructure components that it initialized in
  663. * pmic_battery_probe.
  664. */
  665. static int __devexit platform_pmic_battery_remove(struct platform_device *pdev)
  666. {
  667. struct pmic_power_module_info *pbi = dev_get_drvdata(&pdev->dev);
  668. free_irq(pbi->irq, pbi);
  669. cancel_delayed_work_sync(&pbi->monitor_battery);
  670. destroy_workqueue(pbi->monitor_wqueue);
  671. power_supply_unregister(&pbi->usb);
  672. power_supply_unregister(&pbi->batt);
  673. cancel_work_sync(&pbi->handler);
  674. kfree(pbi);
  675. return 0;
  676. }
  677. static struct platform_driver platform_pmic_battery_driver = {
  678. .driver = {
  679. .name = DRIVER_NAME,
  680. .owner = THIS_MODULE,
  681. },
  682. .probe = platform_pmic_battery_probe,
  683. .remove = __devexit_p(platform_pmic_battery_remove),
  684. };
  685. module_platform_driver(platform_pmic_battery_driver);
  686. MODULE_AUTHOR("Nithish Mahalingam <nithish.mahalingam@intel.com>");
  687. MODULE_DESCRIPTION("Intel Moorestown PMIC Battery Driver");
  688. MODULE_LICENSE("GPL");