intel_mid_battery.c 21 KB

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