msm_charger.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. /* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/err.h>
  16. #include <linux/mfd/pmic8058.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/power_supply.h>
  19. #include <linux/delay.h>
  20. #include <linux/bitops.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/msm-charger.h>
  23. #include <linux/time.h>
  24. #include <linux/slab.h>
  25. #include <linux/wakelock.h>
  26. #include <asm/atomic.h>
  27. #include <mach/msm_hsusb.h>
  28. #define MSM_CHG_MAX_EVENTS 16
  29. #define CHARGING_TEOC_MS 9000000
  30. #define UPDATE_TIME_MS 60000
  31. #define RESUME_CHECK_PERIOD_MS 60000
  32. #define DEFAULT_BATT_MAX_V 4200
  33. #define DEFAULT_BATT_MIN_V 3200
  34. #define MSM_CHARGER_GAUGE_MISSING_VOLTS 3500
  35. #define MSM_CHARGER_GAUGE_MISSING_TEMP 35
  36. /**
  37. * enum msm_battery_status
  38. * @BATT_STATUS_ABSENT: battery not present
  39. * @BATT_STATUS_ID_INVALID: battery present but the id is invalid
  40. * @BATT_STATUS_DISCHARGING: battery is present and is discharging
  41. * @BATT_STATUS_TRKL_CHARGING: battery is being trickle charged
  42. * @BATT_STATUS_FAST_CHARGING: battery is being fast charged
  43. * @BATT_STATUS_JUST_FINISHED_CHARGING: just finished charging,
  44. * battery is fully charged. Do not begin charging untill the
  45. * voltage falls below a threshold to avoid overcharging
  46. * @BATT_STATUS_TEMPERATURE_OUT_OF_RANGE: battery present,
  47. no charging, temp is hot/cold
  48. */
  49. enum msm_battery_status {
  50. BATT_STATUS_ABSENT,
  51. BATT_STATUS_ID_INVALID,
  52. BATT_STATUS_DISCHARGING,
  53. BATT_STATUS_TRKL_CHARGING,
  54. BATT_STATUS_FAST_CHARGING,
  55. BATT_STATUS_JUST_FINISHED_CHARGING,
  56. BATT_STATUS_TEMPERATURE_OUT_OF_RANGE,
  57. };
  58. struct msm_hardware_charger_priv {
  59. struct list_head list;
  60. struct msm_hardware_charger *hw_chg;
  61. enum msm_hardware_charger_state hw_chg_state;
  62. unsigned int max_source_current;
  63. struct power_supply psy;
  64. };
  65. struct msm_charger_event {
  66. enum msm_hardware_charger_event event;
  67. struct msm_hardware_charger *hw_chg;
  68. };
  69. struct msm_charger_mux {
  70. int inited;
  71. struct list_head msm_hardware_chargers;
  72. int count_chargers;
  73. struct mutex msm_hardware_chargers_lock;
  74. struct device *dev;
  75. unsigned int max_voltage;
  76. unsigned int min_voltage;
  77. unsigned int safety_time;
  78. struct delayed_work teoc_work;
  79. unsigned int update_time;
  80. int stop_update;
  81. struct delayed_work update_heartbeat_work;
  82. struct mutex status_lock;
  83. enum msm_battery_status batt_status;
  84. struct msm_hardware_charger_priv *current_chg_priv;
  85. struct msm_hardware_charger_priv *current_mon_priv;
  86. unsigned int (*get_batt_capacity_percent) (void);
  87. struct msm_charger_event *queue;
  88. int tail;
  89. int head;
  90. spinlock_t queue_lock;
  91. int queue_count;
  92. struct work_struct queue_work;
  93. struct workqueue_struct *event_wq_thread;
  94. struct wake_lock wl;
  95. };
  96. static struct msm_charger_mux msm_chg;
  97. static struct msm_battery_gauge *msm_batt_gauge;
  98. static int is_chg_capable_of_charging(struct msm_hardware_charger_priv *priv)
  99. {
  100. if (priv->hw_chg_state == CHG_READY_STATE
  101. || priv->hw_chg_state == CHG_CHARGING_STATE)
  102. return 1;
  103. return 0;
  104. }
  105. static int is_batt_status_capable_of_charging(void)
  106. {
  107. if (msm_chg.batt_status == BATT_STATUS_ABSENT
  108. || msm_chg.batt_status == BATT_STATUS_TEMPERATURE_OUT_OF_RANGE
  109. || msm_chg.batt_status == BATT_STATUS_ID_INVALID
  110. || msm_chg.batt_status == BATT_STATUS_JUST_FINISHED_CHARGING)
  111. return 0;
  112. return 1;
  113. }
  114. static int is_batt_status_charging(void)
  115. {
  116. if (msm_chg.batt_status == BATT_STATUS_TRKL_CHARGING
  117. || msm_chg.batt_status == BATT_STATUS_FAST_CHARGING)
  118. return 1;
  119. return 0;
  120. }
  121. static int is_battery_present(void)
  122. {
  123. if (msm_batt_gauge && msm_batt_gauge->is_battery_present)
  124. return msm_batt_gauge->is_battery_present();
  125. else {
  126. pr_err("msm-charger: no batt gauge batt=absent\n");
  127. return 0;
  128. }
  129. }
  130. static int is_battery_temp_within_range(void)
  131. {
  132. if (msm_batt_gauge && msm_batt_gauge->is_battery_temp_within_range)
  133. return msm_batt_gauge->is_battery_temp_within_range();
  134. else {
  135. pr_err("msm-charger no batt gauge batt=out_of_temperatur\n");
  136. return 0;
  137. }
  138. }
  139. static int is_battery_id_valid(void)
  140. {
  141. if (msm_batt_gauge && msm_batt_gauge->is_battery_id_valid)
  142. return msm_batt_gauge->is_battery_id_valid();
  143. else {
  144. pr_err("msm-charger no batt gauge batt=id_invalid\n");
  145. return 0;
  146. }
  147. }
  148. static int get_prop_battery_mvolts(void)
  149. {
  150. if (msm_batt_gauge && msm_batt_gauge->get_battery_mvolts)
  151. return msm_batt_gauge->get_battery_mvolts();
  152. else {
  153. pr_err("msm-charger no batt gauge assuming 3.5V\n");
  154. return MSM_CHARGER_GAUGE_MISSING_VOLTS;
  155. }
  156. }
  157. static int get_battery_temperature(void)
  158. {
  159. if (msm_batt_gauge && msm_batt_gauge->get_battery_temperature)
  160. return msm_batt_gauge->get_battery_temperature();
  161. else {
  162. pr_err("msm-charger no batt gauge assuming 35 deg G\n");
  163. return MSM_CHARGER_GAUGE_MISSING_TEMP;
  164. }
  165. }
  166. static int get_prop_batt_capacity(void)
  167. {
  168. int capacity;
  169. if (msm_batt_gauge && msm_batt_gauge->get_batt_remaining_capacity)
  170. capacity = msm_batt_gauge->get_batt_remaining_capacity();
  171. else
  172. capacity = msm_chg.get_batt_capacity_percent();
  173. if (capacity <= 10)
  174. pr_err("battery capacity very low = %d\n", capacity);
  175. return capacity;
  176. }
  177. static int get_prop_batt_health(void)
  178. {
  179. int status = 0;
  180. if (msm_chg.batt_status == BATT_STATUS_TEMPERATURE_OUT_OF_RANGE)
  181. status = POWER_SUPPLY_HEALTH_OVERHEAT;
  182. else
  183. status = POWER_SUPPLY_HEALTH_GOOD;
  184. return status;
  185. }
  186. static int get_prop_charge_type(void)
  187. {
  188. int status = 0;
  189. if (msm_chg.batt_status == BATT_STATUS_TRKL_CHARGING)
  190. status = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  191. else if (msm_chg.batt_status == BATT_STATUS_FAST_CHARGING)
  192. status = POWER_SUPPLY_CHARGE_TYPE_FAST;
  193. else
  194. status = POWER_SUPPLY_CHARGE_TYPE_NONE;
  195. return status;
  196. }
  197. static int get_prop_batt_status(void)
  198. {
  199. int status = 0;
  200. if (msm_batt_gauge && msm_batt_gauge->get_battery_status) {
  201. status = msm_batt_gauge->get_battery_status();
  202. if (status == POWER_SUPPLY_STATUS_CHARGING ||
  203. status == POWER_SUPPLY_STATUS_FULL ||
  204. status == POWER_SUPPLY_STATUS_DISCHARGING)
  205. return status;
  206. }
  207. if (is_batt_status_charging())
  208. status = POWER_SUPPLY_STATUS_CHARGING;
  209. else if (msm_chg.batt_status ==
  210. BATT_STATUS_JUST_FINISHED_CHARGING
  211. && msm_chg.current_chg_priv != NULL)
  212. status = POWER_SUPPLY_STATUS_FULL;
  213. else
  214. status = POWER_SUPPLY_STATUS_DISCHARGING;
  215. return status;
  216. }
  217. /* This function should only be called within handle_event or resume */
  218. static void update_batt_status(void)
  219. {
  220. if (is_battery_present()) {
  221. if (is_battery_id_valid()) {
  222. if (msm_chg.batt_status == BATT_STATUS_ABSENT
  223. || msm_chg.batt_status
  224. == BATT_STATUS_ID_INVALID) {
  225. msm_chg.batt_status = BATT_STATUS_DISCHARGING;
  226. }
  227. } else
  228. msm_chg.batt_status = BATT_STATUS_ID_INVALID;
  229. } else
  230. msm_chg.batt_status = BATT_STATUS_ABSENT;
  231. }
  232. static enum power_supply_property msm_power_props[] = {
  233. POWER_SUPPLY_PROP_PRESENT,
  234. POWER_SUPPLY_PROP_ONLINE,
  235. };
  236. static char *msm_power_supplied_to[] = {
  237. "battery",
  238. };
  239. static int msm_power_get_property(struct power_supply *psy,
  240. enum power_supply_property psp,
  241. union power_supply_propval *val)
  242. {
  243. struct msm_hardware_charger_priv *priv;
  244. priv = container_of(psy, struct msm_hardware_charger_priv, psy);
  245. switch (psp) {
  246. case POWER_SUPPLY_PROP_PRESENT:
  247. val->intval = !(priv->hw_chg_state == CHG_ABSENT_STATE);
  248. break;
  249. case POWER_SUPPLY_PROP_ONLINE:
  250. val->intval = (priv->hw_chg_state == CHG_READY_STATE)
  251. || (priv->hw_chg_state == CHG_CHARGING_STATE);
  252. break;
  253. default:
  254. return -EINVAL;
  255. }
  256. return 0;
  257. }
  258. static enum power_supply_property msm_batt_power_props[] = {
  259. POWER_SUPPLY_PROP_STATUS,
  260. POWER_SUPPLY_PROP_CHARGE_TYPE,
  261. POWER_SUPPLY_PROP_HEALTH,
  262. POWER_SUPPLY_PROP_PRESENT,
  263. POWER_SUPPLY_PROP_TECHNOLOGY,
  264. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  265. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  266. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  267. POWER_SUPPLY_PROP_CAPACITY,
  268. };
  269. static int msm_batt_power_get_property(struct power_supply *psy,
  270. enum power_supply_property psp,
  271. union power_supply_propval *val)
  272. {
  273. switch (psp) {
  274. case POWER_SUPPLY_PROP_STATUS:
  275. val->intval = get_prop_batt_status();
  276. break;
  277. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  278. val->intval = get_prop_charge_type();
  279. break;
  280. case POWER_SUPPLY_PROP_HEALTH:
  281. val->intval = get_prop_batt_health();
  282. break;
  283. case POWER_SUPPLY_PROP_PRESENT:
  284. val->intval = !(msm_chg.batt_status == BATT_STATUS_ABSENT);
  285. break;
  286. case POWER_SUPPLY_PROP_TECHNOLOGY:
  287. val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
  288. break;
  289. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  290. val->intval = msm_chg.max_voltage * 1000;
  291. break;
  292. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  293. val->intval = msm_chg.min_voltage * 1000;
  294. break;
  295. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  296. val->intval = get_prop_battery_mvolts();
  297. break;
  298. case POWER_SUPPLY_PROP_CAPACITY:
  299. val->intval = get_prop_batt_capacity();
  300. break;
  301. default:
  302. return -EINVAL;
  303. }
  304. return 0;
  305. }
  306. static struct power_supply msm_psy_batt = {
  307. .name = "battery",
  308. .type = POWER_SUPPLY_TYPE_BATTERY,
  309. .properties = msm_batt_power_props,
  310. .num_properties = ARRAY_SIZE(msm_batt_power_props),
  311. .get_property = msm_batt_power_get_property,
  312. };
  313. static int usb_chg_current;
  314. static struct msm_hardware_charger_priv *usb_hw_chg_priv;
  315. static void (*notify_vbus_state_func_ptr)(int);
  316. static int usb_notified_of_insertion;
  317. /* this is passed to the hsusb via platform_data msm_otg_pdata */
  318. int msm_charger_register_vbus_sn(void (*callback)(int))
  319. {
  320. pr_debug(KERN_INFO "%s\n", __func__);
  321. notify_vbus_state_func_ptr = callback;
  322. return 0;
  323. }
  324. /* this is passed to the hsusb via platform_data msm_otg_pdata */
  325. void msm_charger_unregister_vbus_sn(void (*callback)(int))
  326. {
  327. pr_debug(KERN_INFO "%s\n", __func__);
  328. notify_vbus_state_func_ptr = NULL;
  329. }
  330. static void notify_usb_of_the_plugin_event(struct msm_hardware_charger_priv
  331. *hw_chg, int plugin)
  332. {
  333. plugin = !!plugin;
  334. if (plugin == 1 && usb_notified_of_insertion == 0) {
  335. usb_notified_of_insertion = 1;
  336. if (notify_vbus_state_func_ptr) {
  337. dev_dbg(msm_chg.dev, "%s notifying plugin\n", __func__);
  338. (*notify_vbus_state_func_ptr) (plugin);
  339. } else
  340. dev_dbg(msm_chg.dev, "%s unable to notify plugin\n",
  341. __func__);
  342. usb_hw_chg_priv = hw_chg;
  343. }
  344. if (plugin == 0 && usb_notified_of_insertion == 1) {
  345. if (notify_vbus_state_func_ptr) {
  346. dev_dbg(msm_chg.dev, "%s notifying unplugin\n",
  347. __func__);
  348. (*notify_vbus_state_func_ptr) (plugin);
  349. } else
  350. dev_dbg(msm_chg.dev, "%s unable to notify unplugin\n",
  351. __func__);
  352. usb_notified_of_insertion = 0;
  353. usb_hw_chg_priv = NULL;
  354. }
  355. }
  356. static unsigned int msm_chg_get_batt_capacity_percent(void)
  357. {
  358. unsigned int current_voltage = get_prop_battery_mvolts();
  359. unsigned int low_voltage = msm_chg.min_voltage;
  360. unsigned int high_voltage = msm_chg.max_voltage;
  361. if (current_voltage <= low_voltage)
  362. return 0;
  363. else if (current_voltage >= high_voltage)
  364. return 100;
  365. else
  366. return (current_voltage - low_voltage) * 100
  367. / (high_voltage - low_voltage);
  368. }
  369. #ifdef DEBUG
  370. static inline void debug_print(const char *func,
  371. struct msm_hardware_charger_priv *hw_chg_priv)
  372. {
  373. dev_dbg(msm_chg.dev,
  374. "%s current=(%s)(s=%d)(r=%d) new=(%s)(s=%d)(r=%d) batt=%d En\n",
  375. func,
  376. msm_chg.current_chg_priv ? msm_chg.current_chg_priv->
  377. hw_chg->name : "none",
  378. msm_chg.current_chg_priv ? msm_chg.
  379. current_chg_priv->hw_chg_state : -1,
  380. msm_chg.current_chg_priv ? msm_chg.current_chg_priv->
  381. hw_chg->rating : -1,
  382. hw_chg_priv ? hw_chg_priv->hw_chg->name : "none",
  383. hw_chg_priv ? hw_chg_priv->hw_chg_state : -1,
  384. hw_chg_priv ? hw_chg_priv->hw_chg->rating : -1,
  385. msm_chg.batt_status);
  386. }
  387. #else
  388. static inline void debug_print(const char *func,
  389. struct msm_hardware_charger_priv *hw_chg_priv)
  390. {
  391. }
  392. #endif
  393. static struct msm_hardware_charger_priv *find_best_charger(void)
  394. {
  395. struct msm_hardware_charger_priv *hw_chg_priv;
  396. struct msm_hardware_charger_priv *better;
  397. int rating;
  398. better = NULL;
  399. rating = 0;
  400. list_for_each_entry(hw_chg_priv, &msm_chg.msm_hardware_chargers, list) {
  401. if (is_chg_capable_of_charging(hw_chg_priv)) {
  402. if (hw_chg_priv->hw_chg->rating > rating) {
  403. rating = hw_chg_priv->hw_chg->rating;
  404. better = hw_chg_priv;
  405. }
  406. }
  407. }
  408. return better;
  409. }
  410. static int msm_charging_switched(struct msm_hardware_charger_priv *priv)
  411. {
  412. int ret = 0;
  413. if (priv->hw_chg->charging_switched)
  414. ret = priv->hw_chg->charging_switched(priv->hw_chg);
  415. return ret;
  416. }
  417. static int msm_stop_charging(struct msm_hardware_charger_priv *priv)
  418. {
  419. int ret;
  420. ret = priv->hw_chg->stop_charging(priv->hw_chg);
  421. if (!ret)
  422. wake_unlock(&msm_chg.wl);
  423. return ret;
  424. }
  425. static void msm_enable_system_current(struct msm_hardware_charger_priv *priv)
  426. {
  427. if (priv->hw_chg->start_system_current)
  428. priv->hw_chg->start_system_current(priv->hw_chg,
  429. priv->max_source_current);
  430. }
  431. static void msm_disable_system_current(struct msm_hardware_charger_priv *priv)
  432. {
  433. if (priv->hw_chg->stop_system_current)
  434. priv->hw_chg->stop_system_current(priv->hw_chg);
  435. }
  436. /* the best charger has been selected -start charging from current_chg_priv */
  437. static int msm_start_charging(void)
  438. {
  439. int ret;
  440. struct msm_hardware_charger_priv *priv;
  441. priv = msm_chg.current_chg_priv;
  442. wake_lock(&msm_chg.wl);
  443. ret = priv->hw_chg->start_charging(priv->hw_chg, msm_chg.max_voltage,
  444. priv->max_source_current);
  445. if (ret) {
  446. wake_unlock(&msm_chg.wl);
  447. dev_err(msm_chg.dev, "%s couldnt start chg error = %d\n",
  448. priv->hw_chg->name, ret);
  449. } else
  450. priv->hw_chg_state = CHG_CHARGING_STATE;
  451. return ret;
  452. }
  453. static void handle_charging_done(struct msm_hardware_charger_priv *priv)
  454. {
  455. if (msm_chg.current_chg_priv == priv) {
  456. if (msm_chg.current_chg_priv->hw_chg_state ==
  457. CHG_CHARGING_STATE)
  458. if (msm_stop_charging(msm_chg.current_chg_priv)) {
  459. dev_err(msm_chg.dev, "%s couldnt stop chg\n",
  460. msm_chg.current_chg_priv->hw_chg->name);
  461. }
  462. msm_chg.current_chg_priv->hw_chg_state = CHG_READY_STATE;
  463. msm_chg.batt_status = BATT_STATUS_JUST_FINISHED_CHARGING;
  464. dev_info(msm_chg.dev, "%s: stopping safety timer work\n",
  465. __func__);
  466. cancel_delayed_work(&msm_chg.teoc_work);
  467. if (msm_batt_gauge && msm_batt_gauge->monitor_for_recharging)
  468. msm_batt_gauge->monitor_for_recharging();
  469. else
  470. dev_err(msm_chg.dev,
  471. "%s: no batt gauge recharge monitor\n", __func__);
  472. }
  473. }
  474. static void teoc(struct work_struct *work)
  475. {
  476. /* we have been charging too long - stop charging */
  477. dev_info(msm_chg.dev, "%s: safety timer work expired\n", __func__);
  478. mutex_lock(&msm_chg.status_lock);
  479. if (msm_chg.current_chg_priv != NULL
  480. && msm_chg.current_chg_priv->hw_chg_state == CHG_CHARGING_STATE) {
  481. handle_charging_done(msm_chg.current_chg_priv);
  482. }
  483. mutex_unlock(&msm_chg.status_lock);
  484. }
  485. static void handle_battery_inserted(void)
  486. {
  487. /* if a charger is already present start charging */
  488. if (msm_chg.current_chg_priv != NULL &&
  489. is_batt_status_capable_of_charging() &&
  490. !is_batt_status_charging()) {
  491. if (msm_start_charging()) {
  492. dev_err(msm_chg.dev, "%s couldnt start chg\n",
  493. msm_chg.current_chg_priv->hw_chg->name);
  494. return;
  495. }
  496. msm_chg.batt_status = BATT_STATUS_TRKL_CHARGING;
  497. dev_info(msm_chg.dev, "%s: starting safety timer work\n",
  498. __func__);
  499. queue_delayed_work(msm_chg.event_wq_thread,
  500. &msm_chg.teoc_work,
  501. round_jiffies_relative(msecs_to_jiffies
  502. (msm_chg.
  503. safety_time)));
  504. }
  505. }
  506. static void handle_battery_removed(void)
  507. {
  508. /* if a charger is charging the battery stop it */
  509. if (msm_chg.current_chg_priv != NULL
  510. && msm_chg.current_chg_priv->hw_chg_state == CHG_CHARGING_STATE) {
  511. if (msm_stop_charging(msm_chg.current_chg_priv)) {
  512. dev_err(msm_chg.dev, "%s couldnt stop chg\n",
  513. msm_chg.current_chg_priv->hw_chg->name);
  514. }
  515. msm_chg.current_chg_priv->hw_chg_state = CHG_READY_STATE;
  516. dev_info(msm_chg.dev, "%s: stopping safety timer work\n",
  517. __func__);
  518. cancel_delayed_work(&msm_chg.teoc_work);
  519. }
  520. }
  521. static void update_heartbeat(struct work_struct *work)
  522. {
  523. int temperature;
  524. if (msm_chg.batt_status == BATT_STATUS_ABSENT
  525. || msm_chg.batt_status == BATT_STATUS_ID_INVALID) {
  526. if (is_battery_present())
  527. if (is_battery_id_valid()) {
  528. msm_chg.batt_status = BATT_STATUS_DISCHARGING;
  529. handle_battery_inserted();
  530. }
  531. } else {
  532. if (!is_battery_present()) {
  533. msm_chg.batt_status = BATT_STATUS_ABSENT;
  534. handle_battery_removed();
  535. }
  536. /*
  537. * check battery id because a good battery could be removed
  538. * and replaced with a invalid battery.
  539. */
  540. if (!is_battery_id_valid()) {
  541. msm_chg.batt_status = BATT_STATUS_ID_INVALID;
  542. handle_battery_removed();
  543. }
  544. }
  545. pr_debug("msm-charger %s batt_status= %d\n",
  546. __func__, msm_chg.batt_status);
  547. if (msm_chg.current_chg_priv
  548. && msm_chg.current_chg_priv->hw_chg_state
  549. == CHG_CHARGING_STATE) {
  550. temperature = get_battery_temperature();
  551. /* TODO implement JEITA SPEC*/
  552. }
  553. /* notify that the voltage has changed
  554. * the read of the capacity will trigger a
  555. * voltage read*/
  556. power_supply_changed(&msm_psy_batt);
  557. if (msm_chg.stop_update) {
  558. msm_chg.stop_update = 0;
  559. return;
  560. }
  561. queue_delayed_work(msm_chg.event_wq_thread,
  562. &msm_chg.update_heartbeat_work,
  563. round_jiffies_relative(msecs_to_jiffies
  564. (msm_chg.update_time)));
  565. }
  566. /* set the charger state to READY before calling this */
  567. static void handle_charger_ready(struct msm_hardware_charger_priv *hw_chg_priv)
  568. {
  569. struct msm_hardware_charger_priv *old_chg_priv = NULL;
  570. debug_print(__func__, hw_chg_priv);
  571. if (msm_chg.current_chg_priv != NULL
  572. && hw_chg_priv->hw_chg->rating >
  573. msm_chg.current_chg_priv->hw_chg->rating) {
  574. /*
  575. * a better charger was found, ask the current charger
  576. * to stop charging if it was charging
  577. */
  578. if (msm_chg.current_chg_priv->hw_chg_state ==
  579. CHG_CHARGING_STATE) {
  580. if (msm_stop_charging(msm_chg.current_chg_priv)) {
  581. dev_err(msm_chg.dev, "%s couldnt stop chg\n",
  582. msm_chg.current_chg_priv->hw_chg->name);
  583. return;
  584. }
  585. if (msm_charging_switched(msm_chg.current_chg_priv)) {
  586. dev_err(msm_chg.dev, "%s couldnt stop chg\n",
  587. msm_chg.current_chg_priv->hw_chg->name);
  588. return;
  589. }
  590. }
  591. msm_chg.current_chg_priv->hw_chg_state = CHG_READY_STATE;
  592. old_chg_priv = msm_chg.current_chg_priv;
  593. msm_chg.current_chg_priv = NULL;
  594. }
  595. if (msm_chg.current_chg_priv == NULL) {
  596. msm_chg.current_chg_priv = hw_chg_priv;
  597. dev_info(msm_chg.dev,
  598. "%s: best charger = %s\n", __func__,
  599. msm_chg.current_chg_priv->hw_chg->name);
  600. msm_enable_system_current(msm_chg.current_chg_priv);
  601. /*
  602. * since a better charger was chosen, ask the old
  603. * charger to stop providing system current
  604. */
  605. if (old_chg_priv != NULL)
  606. msm_disable_system_current(old_chg_priv);
  607. if (!is_batt_status_capable_of_charging())
  608. return;
  609. /* start charging from the new charger */
  610. if (!msm_start_charging()) {
  611. /* if we simply switched chg continue with teoc timer
  612. * else we update the batt state and set the teoc
  613. * timer */
  614. if (!is_batt_status_charging()) {
  615. dev_info(msm_chg.dev,
  616. "%s: starting safety timer\n", __func__);
  617. queue_delayed_work(msm_chg.event_wq_thread,
  618. &msm_chg.teoc_work,
  619. round_jiffies_relative
  620. (msecs_to_jiffies
  621. (msm_chg.safety_time)));
  622. msm_chg.batt_status = BATT_STATUS_TRKL_CHARGING;
  623. }
  624. } else {
  625. /* we couldnt start charging from the new readied
  626. * charger */
  627. if (is_batt_status_charging())
  628. msm_chg.batt_status = BATT_STATUS_DISCHARGING;
  629. }
  630. }
  631. }
  632. static void handle_charger_removed(struct msm_hardware_charger_priv
  633. *hw_chg_removed, int new_state)
  634. {
  635. struct msm_hardware_charger_priv *hw_chg_priv;
  636. debug_print(__func__, hw_chg_removed);
  637. if (msm_chg.current_chg_priv == hw_chg_removed) {
  638. msm_disable_system_current(hw_chg_removed);
  639. if (msm_chg.current_chg_priv->hw_chg_state
  640. == CHG_CHARGING_STATE) {
  641. if (msm_stop_charging(hw_chg_removed)) {
  642. dev_err(msm_chg.dev, "%s couldnt stop chg\n",
  643. msm_chg.current_chg_priv->hw_chg->name);
  644. }
  645. }
  646. msm_chg.current_chg_priv = NULL;
  647. }
  648. hw_chg_removed->hw_chg_state = new_state;
  649. if (msm_chg.current_chg_priv == NULL) {
  650. hw_chg_priv = find_best_charger();
  651. if (hw_chg_priv == NULL) {
  652. dev_info(msm_chg.dev, "%s: no chargers\n", __func__);
  653. /* if the battery was Just finished charging
  654. * we keep that state as is so that we dont rush
  655. * in to charging the battery when a charger is
  656. * plugged in shortly. */
  657. if (is_batt_status_charging())
  658. msm_chg.batt_status = BATT_STATUS_DISCHARGING;
  659. } else {
  660. msm_chg.current_chg_priv = hw_chg_priv;
  661. msm_enable_system_current(hw_chg_priv);
  662. dev_info(msm_chg.dev,
  663. "%s: best charger = %s\n", __func__,
  664. msm_chg.current_chg_priv->hw_chg->name);
  665. if (!is_batt_status_capable_of_charging())
  666. return;
  667. if (msm_start_charging()) {
  668. /* we couldnt start charging for some reason */
  669. msm_chg.batt_status = BATT_STATUS_DISCHARGING;
  670. }
  671. }
  672. }
  673. /* if we arent charging stop the safety timer */
  674. if (!is_batt_status_charging()) {
  675. dev_info(msm_chg.dev, "%s: stopping safety timer work\n",
  676. __func__);
  677. cancel_delayed_work(&msm_chg.teoc_work);
  678. }
  679. }
  680. static void handle_event(struct msm_hardware_charger *hw_chg, int event)
  681. {
  682. struct msm_hardware_charger_priv *priv = NULL;
  683. /*
  684. * if hw_chg is NULL then this event comes from non-charger
  685. * parties like battery gauge
  686. */
  687. if (hw_chg)
  688. priv = hw_chg->charger_private;
  689. mutex_lock(&msm_chg.status_lock);
  690. switch (event) {
  691. case CHG_INSERTED_EVENT:
  692. if (priv->hw_chg_state != CHG_ABSENT_STATE) {
  693. dev_info(msm_chg.dev,
  694. "%s insertion detected when cbl present",
  695. hw_chg->name);
  696. break;
  697. }
  698. update_batt_status();
  699. if (hw_chg->type == CHG_TYPE_USB) {
  700. priv->hw_chg_state = CHG_PRESENT_STATE;
  701. notify_usb_of_the_plugin_event(priv, 1);
  702. if (usb_chg_current) {
  703. priv->max_source_current = usb_chg_current;
  704. usb_chg_current = 0;
  705. /* usb has already indicated us to charge */
  706. priv->hw_chg_state = CHG_READY_STATE;
  707. handle_charger_ready(priv);
  708. }
  709. } else {
  710. priv->hw_chg_state = CHG_READY_STATE;
  711. handle_charger_ready(priv);
  712. }
  713. break;
  714. case CHG_ENUMERATED_EVENT: /* only in USB types */
  715. if (priv->hw_chg_state == CHG_ABSENT_STATE) {
  716. dev_info(msm_chg.dev, "%s enum withuot presence\n",
  717. hw_chg->name);
  718. break;
  719. }
  720. update_batt_status();
  721. dev_dbg(msm_chg.dev, "%s enum with %dmA to draw\n",
  722. hw_chg->name, priv->max_source_current);
  723. if (priv->max_source_current == 0) {
  724. /* usb subsystem doesnt want us to draw
  725. * charging current */
  726. /* act as if the charge is removed */
  727. if (priv->hw_chg_state != CHG_PRESENT_STATE)
  728. handle_charger_removed(priv, CHG_PRESENT_STATE);
  729. } else {
  730. if (priv->hw_chg_state != CHG_READY_STATE) {
  731. priv->hw_chg_state = CHG_READY_STATE;
  732. handle_charger_ready(priv);
  733. }
  734. }
  735. break;
  736. case CHG_REMOVED_EVENT:
  737. if (priv->hw_chg_state == CHG_ABSENT_STATE) {
  738. dev_info(msm_chg.dev, "%s cable already removed\n",
  739. hw_chg->name);
  740. break;
  741. }
  742. update_batt_status();
  743. if (hw_chg->type == CHG_TYPE_USB) {
  744. usb_chg_current = 0;
  745. notify_usb_of_the_plugin_event(priv, 0);
  746. }
  747. handle_charger_removed(priv, CHG_ABSENT_STATE);
  748. break;
  749. case CHG_DONE_EVENT:
  750. if (priv->hw_chg_state == CHG_CHARGING_STATE)
  751. handle_charging_done(priv);
  752. break;
  753. case CHG_BATT_BEGIN_FAST_CHARGING:
  754. /* only update if we are TRKL charging */
  755. if (msm_chg.batt_status == BATT_STATUS_TRKL_CHARGING)
  756. msm_chg.batt_status = BATT_STATUS_FAST_CHARGING;
  757. break;
  758. case CHG_BATT_NEEDS_RECHARGING:
  759. msm_chg.batt_status = BATT_STATUS_DISCHARGING;
  760. handle_battery_inserted();
  761. priv = msm_chg.current_chg_priv;
  762. break;
  763. case CHG_BATT_TEMP_OUTOFRANGE:
  764. /* the batt_temp out of range can trigger
  765. * when the battery is absent */
  766. if (!is_battery_present()
  767. && msm_chg.batt_status != BATT_STATUS_ABSENT) {
  768. msm_chg.batt_status = BATT_STATUS_ABSENT;
  769. handle_battery_removed();
  770. break;
  771. }
  772. if (msm_chg.batt_status == BATT_STATUS_TEMPERATURE_OUT_OF_RANGE)
  773. break;
  774. msm_chg.batt_status = BATT_STATUS_TEMPERATURE_OUT_OF_RANGE;
  775. handle_battery_removed();
  776. break;
  777. case CHG_BATT_TEMP_INRANGE:
  778. if (msm_chg.batt_status != BATT_STATUS_TEMPERATURE_OUT_OF_RANGE)
  779. break;
  780. msm_chg.batt_status = BATT_STATUS_ID_INVALID;
  781. /* check id */
  782. if (!is_battery_id_valid())
  783. break;
  784. /* assume that we are discharging from the battery
  785. * and act as if the battery was inserted
  786. * if a charger is present charging will be resumed */
  787. msm_chg.batt_status = BATT_STATUS_DISCHARGING;
  788. handle_battery_inserted();
  789. break;
  790. case CHG_BATT_INSERTED:
  791. if (msm_chg.batt_status != BATT_STATUS_ABSENT)
  792. break;
  793. /* debounce */
  794. if (!is_battery_present())
  795. break;
  796. msm_chg.batt_status = BATT_STATUS_ID_INVALID;
  797. if (!is_battery_id_valid())
  798. break;
  799. /* assume that we are discharging from the battery */
  800. msm_chg.batt_status = BATT_STATUS_DISCHARGING;
  801. /* check if a charger is present */
  802. handle_battery_inserted();
  803. break;
  804. case CHG_BATT_REMOVED:
  805. if (msm_chg.batt_status == BATT_STATUS_ABSENT)
  806. break;
  807. /* debounce */
  808. if (is_battery_present())
  809. break;
  810. msm_chg.batt_status = BATT_STATUS_ABSENT;
  811. handle_battery_removed();
  812. break;
  813. case CHG_BATT_STATUS_CHANGE:
  814. /* TODO battery SOC like battery-alarm/charging-full features
  815. can be added here for future improvement */
  816. break;
  817. }
  818. dev_dbg(msm_chg.dev, "%s %d done batt_status=%d\n", __func__,
  819. event, msm_chg.batt_status);
  820. /* update userspace */
  821. if (msm_batt_gauge)
  822. power_supply_changed(&msm_psy_batt);
  823. if (priv)
  824. power_supply_changed(&priv->psy);
  825. mutex_unlock(&msm_chg.status_lock);
  826. }
  827. static int msm_chg_dequeue_event(struct msm_charger_event **event)
  828. {
  829. unsigned long flags;
  830. spin_lock_irqsave(&msm_chg.queue_lock, flags);
  831. if (msm_chg.queue_count == 0) {
  832. spin_unlock_irqrestore(&msm_chg.queue_lock, flags);
  833. return -EINVAL;
  834. }
  835. *event = &msm_chg.queue[msm_chg.head];
  836. msm_chg.head = (msm_chg.head + 1) % MSM_CHG_MAX_EVENTS;
  837. pr_debug("%s dequeueing %d\n", __func__, (*event)->event);
  838. msm_chg.queue_count--;
  839. spin_unlock_irqrestore(&msm_chg.queue_lock, flags);
  840. return 0;
  841. }
  842. static int msm_chg_enqueue_event(struct msm_hardware_charger *hw_chg,
  843. enum msm_hardware_charger_event event)
  844. {
  845. unsigned long flags;
  846. spin_lock_irqsave(&msm_chg.queue_lock, flags);
  847. if (msm_chg.queue_count == MSM_CHG_MAX_EVENTS) {
  848. spin_unlock_irqrestore(&msm_chg.queue_lock, flags);
  849. pr_err("%s: queue full cannot enqueue %d\n",
  850. __func__, event);
  851. return -EAGAIN;
  852. }
  853. pr_debug("%s queueing %d\n", __func__, event);
  854. msm_chg.queue[msm_chg.tail].event = event;
  855. msm_chg.queue[msm_chg.tail].hw_chg = hw_chg;
  856. msm_chg.tail = (msm_chg.tail + 1)%MSM_CHG_MAX_EVENTS;
  857. msm_chg.queue_count++;
  858. spin_unlock_irqrestore(&msm_chg.queue_lock, flags);
  859. return 0;
  860. }
  861. static void process_events(struct work_struct *work)
  862. {
  863. struct msm_charger_event *event;
  864. int rc;
  865. do {
  866. rc = msm_chg_dequeue_event(&event);
  867. if (!rc)
  868. handle_event(event->hw_chg, event->event);
  869. } while (!rc);
  870. }
  871. /* USB calls these to tell us how much charging current we should draw */
  872. void msm_charger_vbus_draw(unsigned int mA)
  873. {
  874. if (usb_hw_chg_priv) {
  875. usb_hw_chg_priv->max_source_current = mA;
  876. msm_charger_notify_event(usb_hw_chg_priv->hw_chg,
  877. CHG_ENUMERATED_EVENT);
  878. } else
  879. /* remember the current, to be used when charger is ready */
  880. usb_chg_current = mA;
  881. }
  882. static int determine_initial_batt_status(void)
  883. {
  884. if (is_battery_present())
  885. if (is_battery_id_valid())
  886. if (is_battery_temp_within_range())
  887. msm_chg.batt_status = BATT_STATUS_DISCHARGING;
  888. else
  889. msm_chg.batt_status
  890. = BATT_STATUS_TEMPERATURE_OUT_OF_RANGE;
  891. else
  892. msm_chg.batt_status = BATT_STATUS_ID_INVALID;
  893. else
  894. msm_chg.batt_status = BATT_STATUS_ABSENT;
  895. if (is_batt_status_capable_of_charging())
  896. handle_battery_inserted();
  897. /* start updaing the battery powersupply every msm_chg.update_time
  898. * milliseconds */
  899. queue_delayed_work(msm_chg.event_wq_thread,
  900. &msm_chg.update_heartbeat_work,
  901. round_jiffies_relative(msecs_to_jiffies
  902. (msm_chg.update_time)));
  903. pr_debug("%s:OK batt_status=%d\n", __func__, msm_chg.batt_status);
  904. return 0;
  905. }
  906. static int __devinit msm_charger_probe(struct platform_device *pdev)
  907. {
  908. msm_chg.dev = &pdev->dev;
  909. if (pdev->dev.platform_data) {
  910. unsigned int milli_secs;
  911. struct msm_charger_platform_data *pdata
  912. =
  913. (struct msm_charger_platform_data *)pdev->dev.platform_data;
  914. milli_secs = pdata->safety_time * 60 * MSEC_PER_SEC;
  915. if (milli_secs > jiffies_to_msecs(MAX_JIFFY_OFFSET)) {
  916. dev_warn(&pdev->dev, "%s: safety time too large"
  917. "%dms\n", __func__, milli_secs);
  918. milli_secs = jiffies_to_msecs(MAX_JIFFY_OFFSET);
  919. }
  920. msm_chg.safety_time = milli_secs;
  921. milli_secs = pdata->update_time * 60 * MSEC_PER_SEC;
  922. if (milli_secs > jiffies_to_msecs(MAX_JIFFY_OFFSET)) {
  923. dev_warn(&pdev->dev, "%s: safety time too large"
  924. "%dms\n", __func__, milli_secs);
  925. milli_secs = jiffies_to_msecs(MAX_JIFFY_OFFSET);
  926. }
  927. msm_chg.update_time = milli_secs;
  928. msm_chg.max_voltage = pdata->max_voltage;
  929. msm_chg.min_voltage = pdata->min_voltage;
  930. msm_chg.get_batt_capacity_percent =
  931. pdata->get_batt_capacity_percent;
  932. }
  933. if (msm_chg.safety_time == 0)
  934. msm_chg.safety_time = CHARGING_TEOC_MS;
  935. if (msm_chg.update_time == 0)
  936. msm_chg.update_time = UPDATE_TIME_MS;
  937. if (msm_chg.max_voltage == 0)
  938. msm_chg.max_voltage = DEFAULT_BATT_MAX_V;
  939. if (msm_chg.min_voltage == 0)
  940. msm_chg.min_voltage = DEFAULT_BATT_MIN_V;
  941. if (msm_chg.get_batt_capacity_percent == NULL)
  942. msm_chg.get_batt_capacity_percent =
  943. msm_chg_get_batt_capacity_percent;
  944. mutex_init(&msm_chg.status_lock);
  945. INIT_DELAYED_WORK(&msm_chg.teoc_work, teoc);
  946. INIT_DELAYED_WORK(&msm_chg.update_heartbeat_work, update_heartbeat);
  947. wake_lock_init(&msm_chg.wl, WAKE_LOCK_SUSPEND, "msm_charger");
  948. return 0;
  949. }
  950. static int __devexit msm_charger_remove(struct platform_device *pdev)
  951. {
  952. wake_lock_destroy(&msm_chg.wl);
  953. mutex_destroy(&msm_chg.status_lock);
  954. power_supply_unregister(&msm_psy_batt);
  955. return 0;
  956. }
  957. int msm_charger_notify_event(struct msm_hardware_charger *hw_chg,
  958. enum msm_hardware_charger_event event)
  959. {
  960. msm_chg_enqueue_event(hw_chg, event);
  961. queue_work(msm_chg.event_wq_thread, &msm_chg.queue_work);
  962. return 0;
  963. }
  964. EXPORT_SYMBOL(msm_charger_notify_event);
  965. int msm_charger_register(struct msm_hardware_charger *hw_chg)
  966. {
  967. struct msm_hardware_charger_priv *priv;
  968. int rc = 0;
  969. if (!msm_chg.inited) {
  970. pr_err("%s: msm_chg is NULL,Too early to register\n", __func__);
  971. return -EAGAIN;
  972. }
  973. if (hw_chg->start_charging == NULL
  974. || hw_chg->stop_charging == NULL
  975. || hw_chg->name == NULL
  976. || hw_chg->rating == 0) {
  977. pr_err("%s: invalid hw_chg\n", __func__);
  978. return -EINVAL;
  979. }
  980. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  981. if (priv == NULL) {
  982. dev_err(msm_chg.dev, "%s kzalloc failed\n", __func__);
  983. return -ENOMEM;
  984. }
  985. priv->psy.name = hw_chg->name;
  986. if (hw_chg->type == CHG_TYPE_USB)
  987. priv->psy.type = POWER_SUPPLY_TYPE_USB;
  988. else
  989. priv->psy.type = POWER_SUPPLY_TYPE_MAINS;
  990. priv->psy.supplied_to = msm_power_supplied_to;
  991. priv->psy.num_supplicants = ARRAY_SIZE(msm_power_supplied_to);
  992. priv->psy.properties = msm_power_props;
  993. priv->psy.num_properties = ARRAY_SIZE(msm_power_props);
  994. priv->psy.get_property = msm_power_get_property;
  995. rc = power_supply_register(NULL, &priv->psy);
  996. if (rc) {
  997. dev_err(msm_chg.dev, "%s power_supply_register failed\n",
  998. __func__);
  999. goto out;
  1000. }
  1001. priv->hw_chg = hw_chg;
  1002. priv->hw_chg_state = CHG_ABSENT_STATE;
  1003. INIT_LIST_HEAD(&priv->list);
  1004. mutex_lock(&msm_chg.msm_hardware_chargers_lock);
  1005. list_add_tail(&priv->list, &msm_chg.msm_hardware_chargers);
  1006. mutex_unlock(&msm_chg.msm_hardware_chargers_lock);
  1007. hw_chg->charger_private = (void *)priv;
  1008. return 0;
  1009. out:
  1010. kfree(priv);
  1011. return rc;
  1012. }
  1013. EXPORT_SYMBOL(msm_charger_register);
  1014. void msm_battery_gauge_register(struct msm_battery_gauge *batt_gauge)
  1015. {
  1016. int rc;
  1017. if (msm_batt_gauge) {
  1018. msm_batt_gauge = batt_gauge;
  1019. pr_err("msm-charger %s multiple battery gauge called\n",
  1020. __func__);
  1021. } else {
  1022. rc = power_supply_register(msm_chg.dev, &msm_psy_batt);
  1023. if (rc < 0) {
  1024. dev_err(msm_chg.dev, "%s: power_supply_register failed"
  1025. " rc=%d\n", __func__, rc);
  1026. return;
  1027. }
  1028. msm_batt_gauge = batt_gauge;
  1029. determine_initial_batt_status();
  1030. }
  1031. }
  1032. EXPORT_SYMBOL(msm_battery_gauge_register);
  1033. void msm_battery_gauge_unregister(struct msm_battery_gauge *batt_gauge)
  1034. {
  1035. msm_batt_gauge = NULL;
  1036. }
  1037. EXPORT_SYMBOL(msm_battery_gauge_unregister);
  1038. int msm_charger_unregister(struct msm_hardware_charger *hw_chg)
  1039. {
  1040. struct msm_hardware_charger_priv *priv;
  1041. priv = (struct msm_hardware_charger_priv *)(hw_chg->charger_private);
  1042. mutex_lock(&msm_chg.msm_hardware_chargers_lock);
  1043. list_del(&priv->list);
  1044. mutex_unlock(&msm_chg.msm_hardware_chargers_lock);
  1045. power_supply_unregister(&priv->psy);
  1046. kfree(priv);
  1047. return 0;
  1048. }
  1049. EXPORT_SYMBOL(msm_charger_unregister);
  1050. static int msm_charger_suspend(struct device *dev)
  1051. {
  1052. dev_dbg(msm_chg.dev, "%s suspended\n", __func__);
  1053. msm_chg.stop_update = 1;
  1054. cancel_delayed_work(&msm_chg.update_heartbeat_work);
  1055. mutex_lock(&msm_chg.status_lock);
  1056. handle_battery_removed();
  1057. mutex_unlock(&msm_chg.status_lock);
  1058. return 0;
  1059. }
  1060. static int msm_charger_resume(struct device *dev)
  1061. {
  1062. dev_dbg(msm_chg.dev, "%s resumed\n", __func__);
  1063. msm_chg.stop_update = 0;
  1064. /* start updaing the battery powersupply every msm_chg.update_time
  1065. * milliseconds */
  1066. queue_delayed_work(msm_chg.event_wq_thread,
  1067. &msm_chg.update_heartbeat_work,
  1068. round_jiffies_relative(msecs_to_jiffies
  1069. (msm_chg.update_time)));
  1070. mutex_lock(&msm_chg.status_lock);
  1071. handle_battery_inserted();
  1072. mutex_unlock(&msm_chg.status_lock);
  1073. return 0;
  1074. }
  1075. static SIMPLE_DEV_PM_OPS(msm_charger_pm_ops,
  1076. msm_charger_suspend, msm_charger_resume);
  1077. static struct platform_driver msm_charger_driver = {
  1078. .probe = msm_charger_probe,
  1079. .remove = __devexit_p(msm_charger_remove),
  1080. .driver = {
  1081. .name = "msm-charger",
  1082. .owner = THIS_MODULE,
  1083. .pm = &msm_charger_pm_ops,
  1084. },
  1085. };
  1086. static int __init msm_charger_init(void)
  1087. {
  1088. int rc;
  1089. INIT_LIST_HEAD(&msm_chg.msm_hardware_chargers);
  1090. msm_chg.count_chargers = 0;
  1091. mutex_init(&msm_chg.msm_hardware_chargers_lock);
  1092. msm_chg.queue = kzalloc(sizeof(struct msm_charger_event)
  1093. * MSM_CHG_MAX_EVENTS,
  1094. GFP_KERNEL);
  1095. if (!msm_chg.queue) {
  1096. rc = -ENOMEM;
  1097. goto out;
  1098. }
  1099. msm_chg.tail = 0;
  1100. msm_chg.head = 0;
  1101. spin_lock_init(&msm_chg.queue_lock);
  1102. msm_chg.queue_count = 0;
  1103. INIT_WORK(&msm_chg.queue_work, process_events);
  1104. msm_chg.event_wq_thread = create_workqueue("msm_charger_eventd");
  1105. if (!msm_chg.event_wq_thread) {
  1106. rc = -ENOMEM;
  1107. goto free_queue;
  1108. }
  1109. rc = platform_driver_register(&msm_charger_driver);
  1110. if (rc < 0) {
  1111. pr_err("%s: FAIL: platform_driver_register. rc = %d\n",
  1112. __func__, rc);
  1113. goto destroy_wq_thread;
  1114. }
  1115. msm_chg.inited = 1;
  1116. return 0;
  1117. destroy_wq_thread:
  1118. destroy_workqueue(msm_chg.event_wq_thread);
  1119. free_queue:
  1120. kfree(msm_chg.queue);
  1121. out:
  1122. return rc;
  1123. }
  1124. static void __exit msm_charger_exit(void)
  1125. {
  1126. flush_workqueue(msm_chg.event_wq_thread);
  1127. destroy_workqueue(msm_chg.event_wq_thread);
  1128. kfree(msm_chg.queue);
  1129. platform_driver_unregister(&msm_charger_driver);
  1130. }
  1131. module_init(msm_charger_init);
  1132. module_exit(msm_charger_exit);
  1133. MODULE_LICENSE("GPL v2");
  1134. MODULE_AUTHOR("Abhijeet Dharmapurikar <adharmap@codeaurora.org>");
  1135. MODULE_DESCRIPTION("Battery driver for Qualcomm MSM chipsets.");
  1136. MODULE_VERSION("1.0");