msm_battery.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. /* Copyright (c) 2009-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. /*
  14. * this needs to be before <linux/kernel.h> is loaded,
  15. * and <linux/sched.h> loads <linux/kernel.h>
  16. */
  17. #define DEBUG 0
  18. #include <linux/slab.h>
  19. #include <linux/earlysuspend.h>
  20. #include <linux/err.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/sched.h>
  25. #include <linux/signal.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/wait.h>
  28. #include <linux/workqueue.h>
  29. #include <asm/atomic.h>
  30. #include <mach/msm_rpcrouter.h>
  31. #include <mach/msm_battery.h>
  32. #define BATTERY_RPC_PROG 0x30000089
  33. #define BATTERY_RPC_VER_1_1 0x00010001
  34. #define BATTERY_RPC_VER_2_1 0x00020001
  35. #define BATTERY_RPC_VER_4_1 0x00040001
  36. #define BATTERY_RPC_VER_5_1 0x00050001
  37. #define BATTERY_RPC_CB_PROG (BATTERY_RPC_PROG | 0x01000000)
  38. #define CHG_RPC_PROG 0x3000001a
  39. #define CHG_RPC_VER_1_1 0x00010001
  40. #define CHG_RPC_VER_1_3 0x00010003
  41. #define CHG_RPC_VER_2_2 0x00020002
  42. #define CHG_RPC_VER_3_1 0x00030001
  43. #define CHG_RPC_VER_4_1 0x00040001
  44. #define BATTERY_REGISTER_PROC 2
  45. #define BATTERY_MODIFY_CLIENT_PROC 4
  46. #define BATTERY_DEREGISTER_CLIENT_PROC 5
  47. #define BATTERY_READ_MV_PROC 12
  48. #define BATTERY_ENABLE_DISABLE_FILTER_PROC 14
  49. #define VBATT_FILTER 2
  50. #define BATTERY_CB_TYPE_PROC 1
  51. #define BATTERY_CB_ID_ALL_ACTIV 1
  52. #define BATTERY_CB_ID_LOW_VOL 2
  53. #define BATTERY_LOW 3200
  54. #define BATTERY_HIGH 4300
  55. #define ONCRPC_CHG_GET_GENERAL_STATUS_PROC 12
  56. #define ONCRPC_CHARGER_API_VERSIONS_PROC 0xffffffff
  57. #define BATT_RPC_TIMEOUT 5000 /* 5 sec */
  58. #define INVALID_BATT_HANDLE -1
  59. #define RPC_TYPE_REQ 0
  60. #define RPC_TYPE_REPLY 1
  61. #define RPC_REQ_REPLY_COMMON_HEADER_SIZE (3 * sizeof(uint32_t))
  62. #if DEBUG
  63. #define DBG_LIMIT(x...) do {if (printk_ratelimit()) pr_debug(x); } while (0)
  64. #else
  65. #define DBG_LIMIT(x...) do {} while (0)
  66. #endif
  67. enum {
  68. BATTERY_REGISTRATION_SUCCESSFUL = 0,
  69. BATTERY_DEREGISTRATION_SUCCESSFUL = BATTERY_REGISTRATION_SUCCESSFUL,
  70. BATTERY_MODIFICATION_SUCCESSFUL = BATTERY_REGISTRATION_SUCCESSFUL,
  71. BATTERY_INTERROGATION_SUCCESSFUL = BATTERY_REGISTRATION_SUCCESSFUL,
  72. BATTERY_CLIENT_TABLE_FULL = 1,
  73. BATTERY_REG_PARAMS_WRONG = 2,
  74. BATTERY_DEREGISTRATION_FAILED = 4,
  75. BATTERY_MODIFICATION_FAILED = 8,
  76. BATTERY_INTERROGATION_FAILED = 16,
  77. /* Client's filter could not be set because perhaps it does not exist */
  78. BATTERY_SET_FILTER_FAILED = 32,
  79. /* Client's could not be found for enabling or disabling the individual
  80. * client */
  81. BATTERY_ENABLE_DISABLE_INDIVIDUAL_CLIENT_FAILED = 64,
  82. BATTERY_LAST_ERROR = 128,
  83. };
  84. enum {
  85. BATTERY_VOLTAGE_UP = 0,
  86. BATTERY_VOLTAGE_DOWN,
  87. BATTERY_VOLTAGE_ABOVE_THIS_LEVEL,
  88. BATTERY_VOLTAGE_BELOW_THIS_LEVEL,
  89. BATTERY_VOLTAGE_LEVEL,
  90. BATTERY_ALL_ACTIVITY,
  91. VBATT_CHG_EVENTS,
  92. BATTERY_VOLTAGE_UNKNOWN,
  93. };
  94. /*
  95. * This enum contains defintions of the charger hardware status
  96. */
  97. enum chg_charger_status_type {
  98. /* The charger is good */
  99. CHARGER_STATUS_GOOD,
  100. /* The charger is bad */
  101. CHARGER_STATUS_BAD,
  102. /* The charger is weak */
  103. CHARGER_STATUS_WEAK,
  104. /* Invalid charger status. */
  105. CHARGER_STATUS_INVALID
  106. };
  107. /*
  108. *This enum contains defintions of the charger hardware type
  109. */
  110. enum chg_charger_hardware_type {
  111. /* The charger is removed */
  112. CHARGER_TYPE_NONE,
  113. /* The charger is a regular wall charger */
  114. CHARGER_TYPE_WALL,
  115. /* The charger is a PC USB */
  116. CHARGER_TYPE_USB_PC,
  117. /* The charger is a wall USB charger */
  118. CHARGER_TYPE_USB_WALL,
  119. /* The charger is a USB carkit */
  120. CHARGER_TYPE_USB_CARKIT,
  121. /* Invalid charger hardware status. */
  122. CHARGER_TYPE_INVALID
  123. };
  124. /*
  125. * This enum contains defintions of the battery status
  126. */
  127. enum chg_battery_status_type {
  128. /* The battery is good */
  129. BATTERY_STATUS_GOOD,
  130. /* The battery is cold/hot */
  131. BATTERY_STATUS_BAD_TEMP,
  132. /* The battery is bad */
  133. BATTERY_STATUS_BAD,
  134. /* The battery is removed */
  135. BATTERY_STATUS_REMOVED, /* on v2.2 only */
  136. BATTERY_STATUS_INVALID_v1 = BATTERY_STATUS_REMOVED,
  137. /* Invalid battery status. */
  138. BATTERY_STATUS_INVALID
  139. };
  140. /*
  141. *This enum contains defintions of the battery voltage level
  142. */
  143. enum chg_battery_level_type {
  144. /* The battery voltage is dead/very low (less than 3.2V) */
  145. BATTERY_LEVEL_DEAD,
  146. /* The battery voltage is weak/low (between 3.2V and 3.4V) */
  147. BATTERY_LEVEL_WEAK,
  148. /* The battery voltage is good/normal(between 3.4V and 4.2V) */
  149. BATTERY_LEVEL_GOOD,
  150. /* The battery voltage is up to full (close to 4.2V) */
  151. BATTERY_LEVEL_FULL,
  152. /* Invalid battery voltage level. */
  153. BATTERY_LEVEL_INVALID
  154. };
  155. #ifndef CONFIG_BATTERY_MSM_FAKE
  156. struct rpc_reply_batt_chg_v1 {
  157. struct rpc_reply_hdr hdr;
  158. u32 more_data;
  159. u32 charger_status;
  160. u32 charger_type;
  161. u32 battery_status;
  162. u32 battery_level;
  163. u32 battery_voltage;
  164. u32 battery_temp;
  165. };
  166. struct rpc_reply_batt_chg_v2 {
  167. struct rpc_reply_batt_chg_v1 v1;
  168. u32 is_charger_valid;
  169. u32 is_charging;
  170. u32 is_battery_valid;
  171. u32 ui_event;
  172. };
  173. union rpc_reply_batt_chg {
  174. struct rpc_reply_batt_chg_v1 v1;
  175. struct rpc_reply_batt_chg_v2 v2;
  176. };
  177. static union rpc_reply_batt_chg rep_batt_chg;
  178. #endif
  179. struct msm_battery_info {
  180. u32 voltage_max_design;
  181. u32 voltage_min_design;
  182. u32 voltage_fail_safe;
  183. u32 chg_api_version;
  184. u32 batt_technology;
  185. u32 batt_api_version;
  186. u32 avail_chg_sources;
  187. u32 current_chg_source;
  188. u32 batt_status;
  189. u32 batt_health;
  190. u32 charger_valid;
  191. u32 batt_valid;
  192. u32 batt_capacity; /* in percentage */
  193. u32 charger_status;
  194. u32 charger_type;
  195. u32 battery_status;
  196. u32 battery_level;
  197. u32 battery_voltage; /* in millie volts */
  198. u32 battery_temp; /* in celsius */
  199. u32(*calculate_capacity) (u32 voltage);
  200. s32 batt_handle;
  201. struct power_supply *msm_psy_ac;
  202. struct power_supply *msm_psy_usb;
  203. struct power_supply *msm_psy_batt;
  204. struct power_supply *current_ps;
  205. struct msm_rpc_client *batt_client;
  206. struct msm_rpc_endpoint *chg_ep;
  207. wait_queue_head_t wait_q;
  208. u32 vbatt_modify_reply_avail;
  209. struct early_suspend early_suspend;
  210. };
  211. static struct msm_battery_info msm_batt_info = {
  212. .batt_handle = INVALID_BATT_HANDLE,
  213. .charger_status = CHARGER_STATUS_BAD,
  214. .charger_type = CHARGER_TYPE_INVALID,
  215. .battery_status = BATTERY_STATUS_GOOD,
  216. .battery_level = BATTERY_LEVEL_FULL,
  217. .battery_voltage = BATTERY_HIGH,
  218. .batt_capacity = 100,
  219. .batt_status = POWER_SUPPLY_STATUS_DISCHARGING,
  220. .batt_health = POWER_SUPPLY_HEALTH_GOOD,
  221. .batt_valid = 1,
  222. .battery_temp = 23,
  223. .vbatt_modify_reply_avail = 0,
  224. };
  225. static enum power_supply_property msm_power_props[] = {
  226. POWER_SUPPLY_PROP_ONLINE,
  227. };
  228. static char *msm_power_supplied_to[] = {
  229. "battery",
  230. };
  231. static int msm_power_get_property(struct power_supply *psy,
  232. enum power_supply_property psp,
  233. union power_supply_propval *val)
  234. {
  235. switch (psp) {
  236. case POWER_SUPPLY_PROP_ONLINE:
  237. if (psy->type == POWER_SUPPLY_TYPE_MAINS) {
  238. val->intval = msm_batt_info.current_chg_source & AC_CHG
  239. ? 1 : 0;
  240. }
  241. if (psy->type == POWER_SUPPLY_TYPE_USB) {
  242. val->intval = msm_batt_info.current_chg_source & USB_CHG
  243. ? 1 : 0;
  244. }
  245. break;
  246. default:
  247. return -EINVAL;
  248. }
  249. return 0;
  250. }
  251. static struct power_supply msm_psy_ac = {
  252. .name = "ac",
  253. .type = POWER_SUPPLY_TYPE_MAINS,
  254. .supplied_to = msm_power_supplied_to,
  255. .num_supplicants = ARRAY_SIZE(msm_power_supplied_to),
  256. .properties = msm_power_props,
  257. .num_properties = ARRAY_SIZE(msm_power_props),
  258. .get_property = msm_power_get_property,
  259. };
  260. static struct power_supply msm_psy_usb = {
  261. .name = "usb",
  262. .type = POWER_SUPPLY_TYPE_USB,
  263. .supplied_to = msm_power_supplied_to,
  264. .num_supplicants = ARRAY_SIZE(msm_power_supplied_to),
  265. .properties = msm_power_props,
  266. .num_properties = ARRAY_SIZE(msm_power_props),
  267. .get_property = msm_power_get_property,
  268. };
  269. static enum power_supply_property msm_batt_power_props[] = {
  270. POWER_SUPPLY_PROP_STATUS,
  271. POWER_SUPPLY_PROP_HEALTH,
  272. POWER_SUPPLY_PROP_PRESENT,
  273. POWER_SUPPLY_PROP_TECHNOLOGY,
  274. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  275. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  276. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  277. POWER_SUPPLY_PROP_CAPACITY,
  278. };
  279. static int msm_batt_power_get_property(struct power_supply *psy,
  280. enum power_supply_property psp,
  281. union power_supply_propval *val)
  282. {
  283. switch (psp) {
  284. case POWER_SUPPLY_PROP_STATUS:
  285. val->intval = msm_batt_info.batt_status;
  286. break;
  287. case POWER_SUPPLY_PROP_HEALTH:
  288. val->intval = msm_batt_info.batt_health;
  289. break;
  290. case POWER_SUPPLY_PROP_PRESENT:
  291. val->intval = msm_batt_info.batt_valid;
  292. break;
  293. case POWER_SUPPLY_PROP_TECHNOLOGY:
  294. val->intval = msm_batt_info.batt_technology;
  295. break;
  296. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  297. val->intval = msm_batt_info.voltage_max_design;
  298. break;
  299. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  300. val->intval = msm_batt_info.voltage_min_design;
  301. break;
  302. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  303. val->intval = msm_batt_info.battery_voltage;
  304. break;
  305. case POWER_SUPPLY_PROP_CAPACITY:
  306. val->intval = msm_batt_info.batt_capacity;
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. return 0;
  312. }
  313. static struct power_supply msm_psy_batt = {
  314. .name = "battery",
  315. .type = POWER_SUPPLY_TYPE_BATTERY,
  316. .properties = msm_batt_power_props,
  317. .num_properties = ARRAY_SIZE(msm_batt_power_props),
  318. .get_property = msm_batt_power_get_property,
  319. };
  320. #ifndef CONFIG_BATTERY_MSM_FAKE
  321. struct msm_batt_get_volt_ret_data {
  322. u32 battery_voltage;
  323. };
  324. static int msm_batt_get_volt_ret_func(struct msm_rpc_client *batt_client,
  325. void *buf, void *data)
  326. {
  327. struct msm_batt_get_volt_ret_data *data_ptr, *buf_ptr;
  328. data_ptr = (struct msm_batt_get_volt_ret_data *)data;
  329. buf_ptr = (struct msm_batt_get_volt_ret_data *)buf;
  330. data_ptr->battery_voltage = be32_to_cpu(buf_ptr->battery_voltage);
  331. return 0;
  332. }
  333. static u32 msm_batt_get_vbatt_voltage(void)
  334. {
  335. int rc;
  336. struct msm_batt_get_volt_ret_data rep;
  337. rc = msm_rpc_client_req(msm_batt_info.batt_client,
  338. BATTERY_READ_MV_PROC,
  339. NULL, NULL,
  340. msm_batt_get_volt_ret_func, &rep,
  341. msecs_to_jiffies(BATT_RPC_TIMEOUT));
  342. if (rc < 0) {
  343. pr_err("%s: FAIL: vbatt get volt. rc=%d\n", __func__, rc);
  344. return 0;
  345. }
  346. return rep.battery_voltage;
  347. }
  348. #define be32_to_cpu_self(v) (v = be32_to_cpu(v))
  349. static int msm_batt_get_batt_chg_status(void)
  350. {
  351. int rc;
  352. struct rpc_req_batt_chg {
  353. struct rpc_request_hdr hdr;
  354. u32 more_data;
  355. } req_batt_chg;
  356. struct rpc_reply_batt_chg_v1 *v1p;
  357. req_batt_chg.more_data = cpu_to_be32(1);
  358. memset(&rep_batt_chg, 0, sizeof(rep_batt_chg));
  359. v1p = &rep_batt_chg.v1;
  360. rc = msm_rpc_call_reply(msm_batt_info.chg_ep,
  361. ONCRPC_CHG_GET_GENERAL_STATUS_PROC,
  362. &req_batt_chg, sizeof(req_batt_chg),
  363. &rep_batt_chg, sizeof(rep_batt_chg),
  364. msecs_to_jiffies(BATT_RPC_TIMEOUT));
  365. if (rc < 0) {
  366. pr_err("%s: ERROR. msm_rpc_call_reply failed! proc=%d rc=%d\n",
  367. __func__, ONCRPC_CHG_GET_GENERAL_STATUS_PROC, rc);
  368. return rc;
  369. } else if (be32_to_cpu(v1p->more_data)) {
  370. be32_to_cpu_self(v1p->charger_status);
  371. be32_to_cpu_self(v1p->charger_type);
  372. be32_to_cpu_self(v1p->battery_status);
  373. be32_to_cpu_self(v1p->battery_level);
  374. be32_to_cpu_self(v1p->battery_voltage);
  375. be32_to_cpu_self(v1p->battery_temp);
  376. } else {
  377. pr_err("%s: No battery/charger data in RPC reply\n", __func__);
  378. return -EIO;
  379. }
  380. return 0;
  381. }
  382. static void msm_batt_update_psy_status(void)
  383. {
  384. static u32 unnecessary_event_count;
  385. u32 charger_status;
  386. u32 charger_type;
  387. u32 battery_status;
  388. u32 battery_level;
  389. u32 battery_voltage;
  390. u32 battery_temp;
  391. struct power_supply *supp;
  392. if (msm_batt_get_batt_chg_status())
  393. return;
  394. charger_status = rep_batt_chg.v1.charger_status;
  395. charger_type = rep_batt_chg.v1.charger_type;
  396. battery_status = rep_batt_chg.v1.battery_status;
  397. battery_level = rep_batt_chg.v1.battery_level;
  398. battery_voltage = rep_batt_chg.v1.battery_voltage;
  399. battery_temp = rep_batt_chg.v1.battery_temp;
  400. /* Make correction for battery status */
  401. if (battery_status == BATTERY_STATUS_INVALID_v1) {
  402. if (msm_batt_info.chg_api_version < CHG_RPC_VER_3_1)
  403. battery_status = BATTERY_STATUS_INVALID;
  404. }
  405. if (charger_status == msm_batt_info.charger_status &&
  406. charger_type == msm_batt_info.charger_type &&
  407. battery_status == msm_batt_info.battery_status &&
  408. battery_level == msm_batt_info.battery_level &&
  409. battery_voltage == msm_batt_info.battery_voltage &&
  410. battery_temp == msm_batt_info.battery_temp) {
  411. /* Got unnecessary event from Modem PMIC VBATT driver.
  412. * Nothing changed in Battery or charger status.
  413. */
  414. unnecessary_event_count++;
  415. if ((unnecessary_event_count % 20) == 1)
  416. DBG_LIMIT("BATT: same event count = %u\n",
  417. unnecessary_event_count);
  418. return;
  419. }
  420. unnecessary_event_count = 0;
  421. DBG_LIMIT("BATT: rcvd: %d, %d, %d, %d; %d, %d\n",
  422. charger_status, charger_type, battery_status,
  423. battery_level, battery_voltage, battery_temp);
  424. if (battery_status == BATTERY_STATUS_INVALID &&
  425. battery_level != BATTERY_LEVEL_INVALID) {
  426. DBG_LIMIT("BATT: change status(%d) to (%d) for level=%d\n",
  427. battery_status, BATTERY_STATUS_GOOD, battery_level);
  428. battery_status = BATTERY_STATUS_GOOD;
  429. }
  430. if (msm_batt_info.charger_type != charger_type) {
  431. if (charger_type == CHARGER_TYPE_USB_PC ||
  432. charger_type == CHARGER_TYPE_USB_CARKIT) {
  433. DBG_LIMIT("BATT: USB charger plugged in\n");
  434. msm_batt_info.current_chg_source = USB_CHG;
  435. supp = &msm_psy_usb;
  436. } else if (charger_type == CHARGER_TYPE_WALL ||
  437. charger_type == CHARGER_TYPE_USB_WALL) {
  438. DBG_LIMIT("BATT: AC Wall changer plugged in\n");
  439. msm_batt_info.current_chg_source = AC_CHG;
  440. supp = &msm_psy_ac;
  441. } else {
  442. if (msm_batt_info.current_chg_source & AC_CHG)
  443. DBG_LIMIT("BATT: AC Wall charger removed\n");
  444. else if (msm_batt_info.current_chg_source & USB_CHG)
  445. DBG_LIMIT("BATT: USB charger removed\n");
  446. else
  447. DBG_LIMIT("BATT: No charger present\n");
  448. msm_batt_info.current_chg_source = 0;
  449. supp = &msm_psy_batt;
  450. /* Correct charger status */
  451. if (charger_status != CHARGER_STATUS_INVALID) {
  452. DBG_LIMIT("BATT: No charging!\n");
  453. charger_status = CHARGER_STATUS_INVALID;
  454. msm_batt_info.batt_status =
  455. POWER_SUPPLY_STATUS_NOT_CHARGING;
  456. }
  457. }
  458. } else
  459. supp = NULL;
  460. if (msm_batt_info.charger_status != charger_status) {
  461. if (charger_status == CHARGER_STATUS_GOOD ||
  462. charger_status == CHARGER_STATUS_WEAK) {
  463. if (msm_batt_info.current_chg_source) {
  464. DBG_LIMIT("BATT: Charging.\n");
  465. msm_batt_info.batt_status =
  466. POWER_SUPPLY_STATUS_CHARGING;
  467. /* Correct when supp==NULL */
  468. if (msm_batt_info.current_chg_source & AC_CHG)
  469. supp = &msm_psy_ac;
  470. else
  471. supp = &msm_psy_usb;
  472. }
  473. } else {
  474. DBG_LIMIT("BATT: No charging.\n");
  475. msm_batt_info.batt_status =
  476. POWER_SUPPLY_STATUS_NOT_CHARGING;
  477. supp = &msm_psy_batt;
  478. }
  479. } else {
  480. /* Correct charger status */
  481. if (charger_type != CHARGER_TYPE_INVALID &&
  482. charger_status == CHARGER_STATUS_GOOD) {
  483. DBG_LIMIT("BATT: In charging\n");
  484. msm_batt_info.batt_status =
  485. POWER_SUPPLY_STATUS_CHARGING;
  486. }
  487. }
  488. /* Correct battery voltage and status */
  489. if (!battery_voltage) {
  490. if (charger_status == CHARGER_STATUS_INVALID) {
  491. DBG_LIMIT("BATT: Read VBATT\n");
  492. battery_voltage = msm_batt_get_vbatt_voltage();
  493. } else
  494. /* Use previous */
  495. battery_voltage = msm_batt_info.battery_voltage;
  496. }
  497. if (battery_status == BATTERY_STATUS_INVALID) {
  498. if (battery_voltage >= msm_batt_info.voltage_min_design &&
  499. battery_voltage <= msm_batt_info.voltage_max_design) {
  500. DBG_LIMIT("BATT: Battery valid\n");
  501. msm_batt_info.batt_valid = 1;
  502. battery_status = BATTERY_STATUS_GOOD;
  503. }
  504. }
  505. if (msm_batt_info.battery_status != battery_status) {
  506. if (battery_status != BATTERY_STATUS_INVALID) {
  507. msm_batt_info.batt_valid = 1;
  508. if (battery_status == BATTERY_STATUS_BAD) {
  509. DBG_LIMIT("BATT: Battery bad.\n");
  510. msm_batt_info.batt_health =
  511. POWER_SUPPLY_HEALTH_DEAD;
  512. } else if (battery_status == BATTERY_STATUS_BAD_TEMP) {
  513. DBG_LIMIT("BATT: Battery overheat.\n");
  514. msm_batt_info.batt_health =
  515. POWER_SUPPLY_HEALTH_OVERHEAT;
  516. } else {
  517. DBG_LIMIT("BATT: Battery good.\n");
  518. msm_batt_info.batt_health =
  519. POWER_SUPPLY_HEALTH_GOOD;
  520. }
  521. } else {
  522. msm_batt_info.batt_valid = 0;
  523. DBG_LIMIT("BATT: Battery invalid.\n");
  524. msm_batt_info.batt_health = POWER_SUPPLY_HEALTH_UNKNOWN;
  525. }
  526. if (msm_batt_info.batt_status != POWER_SUPPLY_STATUS_CHARGING) {
  527. if (battery_status == BATTERY_STATUS_INVALID) {
  528. DBG_LIMIT("BATT: Battery -> unknown\n");
  529. msm_batt_info.batt_status =
  530. POWER_SUPPLY_STATUS_UNKNOWN;
  531. } else {
  532. DBG_LIMIT("BATT: Battery -> discharging\n");
  533. msm_batt_info.batt_status =
  534. POWER_SUPPLY_STATUS_DISCHARGING;
  535. }
  536. }
  537. if (!supp) {
  538. if (msm_batt_info.current_chg_source) {
  539. if (msm_batt_info.current_chg_source & AC_CHG)
  540. supp = &msm_psy_ac;
  541. else
  542. supp = &msm_psy_usb;
  543. } else
  544. supp = &msm_psy_batt;
  545. }
  546. }
  547. msm_batt_info.charger_status = charger_status;
  548. msm_batt_info.charger_type = charger_type;
  549. msm_batt_info.battery_status = battery_status;
  550. msm_batt_info.battery_level = battery_level;
  551. msm_batt_info.battery_temp = battery_temp;
  552. if (msm_batt_info.battery_voltage != battery_voltage) {
  553. msm_batt_info.battery_voltage = battery_voltage;
  554. msm_batt_info.batt_capacity =
  555. msm_batt_info.calculate_capacity(battery_voltage);
  556. DBG_LIMIT("BATT: voltage = %u mV [capacity = %d%%]\n",
  557. battery_voltage, msm_batt_info.batt_capacity);
  558. if (!supp)
  559. supp = msm_batt_info.current_ps;
  560. }
  561. if (supp) {
  562. msm_batt_info.current_ps = supp;
  563. DBG_LIMIT("BATT: Supply = %s\n", supp->name);
  564. power_supply_changed(supp);
  565. }
  566. }
  567. #ifdef CONFIG_HAS_EARLYSUSPEND
  568. struct batt_modify_client_req {
  569. u32 client_handle;
  570. /* The voltage at which callback (CB) should be called. */
  571. u32 desired_batt_voltage;
  572. /* The direction when the CB should be called. */
  573. u32 voltage_direction;
  574. /* The registered callback to be called when voltage and
  575. * direction specs are met. */
  576. u32 batt_cb_id;
  577. /* The call back data */
  578. u32 cb_data;
  579. };
  580. struct batt_modify_client_rep {
  581. u32 result;
  582. };
  583. static int msm_batt_modify_client_arg_func(struct msm_rpc_client *batt_client,
  584. void *buf, void *data)
  585. {
  586. struct batt_modify_client_req *batt_modify_client_req =
  587. (struct batt_modify_client_req *)data;
  588. u32 *req = (u32 *)buf;
  589. int size = 0;
  590. *req = cpu_to_be32(batt_modify_client_req->client_handle);
  591. size += sizeof(u32);
  592. req++;
  593. *req = cpu_to_be32(batt_modify_client_req->desired_batt_voltage);
  594. size += sizeof(u32);
  595. req++;
  596. *req = cpu_to_be32(batt_modify_client_req->voltage_direction);
  597. size += sizeof(u32);
  598. req++;
  599. *req = cpu_to_be32(batt_modify_client_req->batt_cb_id);
  600. size += sizeof(u32);
  601. req++;
  602. *req = cpu_to_be32(batt_modify_client_req->cb_data);
  603. size += sizeof(u32);
  604. return size;
  605. }
  606. static int msm_batt_modify_client_ret_func(struct msm_rpc_client *batt_client,
  607. void *buf, void *data)
  608. {
  609. struct batt_modify_client_rep *data_ptr, *buf_ptr;
  610. data_ptr = (struct batt_modify_client_rep *)data;
  611. buf_ptr = (struct batt_modify_client_rep *)buf;
  612. data_ptr->result = be32_to_cpu(buf_ptr->result);
  613. return 0;
  614. }
  615. static int msm_batt_modify_client(u32 client_handle, u32 desired_batt_voltage,
  616. u32 voltage_direction, u32 batt_cb_id, u32 cb_data)
  617. {
  618. int rc;
  619. struct batt_modify_client_req req;
  620. struct batt_modify_client_rep rep;
  621. req.client_handle = client_handle;
  622. req.desired_batt_voltage = desired_batt_voltage;
  623. req.voltage_direction = voltage_direction;
  624. req.batt_cb_id = batt_cb_id;
  625. req.cb_data = cb_data;
  626. rc = msm_rpc_client_req(msm_batt_info.batt_client,
  627. BATTERY_MODIFY_CLIENT_PROC,
  628. msm_batt_modify_client_arg_func, &req,
  629. msm_batt_modify_client_ret_func, &rep,
  630. msecs_to_jiffies(BATT_RPC_TIMEOUT));
  631. if (rc < 0) {
  632. pr_err("%s: ERROR. failed to modify Vbatt client\n",
  633. __func__);
  634. return rc;
  635. }
  636. if (rep.result != BATTERY_MODIFICATION_SUCCESSFUL) {
  637. pr_err("%s: ERROR. modify client failed. result = %u\n",
  638. __func__, rep.result);
  639. return -EIO;
  640. }
  641. return 0;
  642. }
  643. void msm_batt_early_suspend(struct early_suspend *h)
  644. {
  645. int rc;
  646. pr_debug("%s: enter\n", __func__);
  647. if (msm_batt_info.batt_handle != INVALID_BATT_HANDLE) {
  648. rc = msm_batt_modify_client(msm_batt_info.batt_handle,
  649. msm_batt_info.voltage_fail_safe,
  650. BATTERY_VOLTAGE_BELOW_THIS_LEVEL,
  651. BATTERY_CB_ID_LOW_VOL,
  652. msm_batt_info.voltage_fail_safe);
  653. if (rc < 0) {
  654. pr_err("%s: msm_batt_modify_client. rc=%d\n",
  655. __func__, rc);
  656. return;
  657. }
  658. } else {
  659. pr_err("%s: ERROR. invalid batt_handle\n", __func__);
  660. return;
  661. }
  662. pr_debug("%s: exit\n", __func__);
  663. }
  664. void msm_batt_late_resume(struct early_suspend *h)
  665. {
  666. int rc;
  667. pr_debug("%s: enter\n", __func__);
  668. if (msm_batt_info.batt_handle != INVALID_BATT_HANDLE) {
  669. rc = msm_batt_modify_client(msm_batt_info.batt_handle,
  670. msm_batt_info.voltage_fail_safe,
  671. BATTERY_ALL_ACTIVITY,
  672. BATTERY_CB_ID_ALL_ACTIV, BATTERY_ALL_ACTIVITY);
  673. if (rc < 0) {
  674. pr_err("%s: msm_batt_modify_client FAIL rc=%d\n",
  675. __func__, rc);
  676. return;
  677. }
  678. } else {
  679. pr_err("%s: ERROR. invalid batt_handle\n", __func__);
  680. return;
  681. }
  682. msm_batt_update_psy_status();
  683. pr_debug("%s: exit\n", __func__);
  684. }
  685. #endif
  686. struct msm_batt_vbatt_filter_req {
  687. u32 batt_handle;
  688. u32 enable_filter;
  689. u32 vbatt_filter;
  690. };
  691. struct msm_batt_vbatt_filter_rep {
  692. u32 result;
  693. };
  694. static int msm_batt_filter_arg_func(struct msm_rpc_client *batt_client,
  695. void *buf, void *data)
  696. {
  697. struct msm_batt_vbatt_filter_req *vbatt_filter_req =
  698. (struct msm_batt_vbatt_filter_req *)data;
  699. u32 *req = (u32 *)buf;
  700. int size = 0;
  701. *req = cpu_to_be32(vbatt_filter_req->batt_handle);
  702. size += sizeof(u32);
  703. req++;
  704. *req = cpu_to_be32(vbatt_filter_req->enable_filter);
  705. size += sizeof(u32);
  706. req++;
  707. *req = cpu_to_be32(vbatt_filter_req->vbatt_filter);
  708. size += sizeof(u32);
  709. return size;
  710. }
  711. static int msm_batt_filter_ret_func(struct msm_rpc_client *batt_client,
  712. void *buf, void *data)
  713. {
  714. struct msm_batt_vbatt_filter_rep *data_ptr, *buf_ptr;
  715. data_ptr = (struct msm_batt_vbatt_filter_rep *)data;
  716. buf_ptr = (struct msm_batt_vbatt_filter_rep *)buf;
  717. data_ptr->result = be32_to_cpu(buf_ptr->result);
  718. return 0;
  719. }
  720. static int msm_batt_enable_filter(u32 vbatt_filter)
  721. {
  722. int rc;
  723. struct msm_batt_vbatt_filter_req vbatt_filter_req;
  724. struct msm_batt_vbatt_filter_rep vbatt_filter_rep;
  725. vbatt_filter_req.batt_handle = msm_batt_info.batt_handle;
  726. vbatt_filter_req.enable_filter = 1;
  727. vbatt_filter_req.vbatt_filter = vbatt_filter;
  728. rc = msm_rpc_client_req(msm_batt_info.batt_client,
  729. BATTERY_ENABLE_DISABLE_FILTER_PROC,
  730. msm_batt_filter_arg_func, &vbatt_filter_req,
  731. msm_batt_filter_ret_func, &vbatt_filter_rep,
  732. msecs_to_jiffies(BATT_RPC_TIMEOUT));
  733. if (rc < 0) {
  734. pr_err("%s: FAIL: enable vbatt filter. rc=%d\n",
  735. __func__, rc);
  736. return rc;
  737. }
  738. if (vbatt_filter_rep.result != BATTERY_DEREGISTRATION_SUCCESSFUL) {
  739. pr_err("%s: FAIL: enable vbatt filter: result=%d\n",
  740. __func__, vbatt_filter_rep.result);
  741. return -EIO;
  742. }
  743. pr_debug("%s: enable vbatt filter: OK\n", __func__);
  744. return rc;
  745. }
  746. struct batt_client_registration_req {
  747. /* The voltage at which callback (CB) should be called. */
  748. u32 desired_batt_voltage;
  749. /* The direction when the CB should be called. */
  750. u32 voltage_direction;
  751. /* The registered callback to be called when voltage and
  752. * direction specs are met. */
  753. u32 batt_cb_id;
  754. /* The call back data */
  755. u32 cb_data;
  756. u32 more_data;
  757. u32 batt_error;
  758. };
  759. struct batt_client_registration_req_4_1 {
  760. /* The voltage at which callback (CB) should be called. */
  761. u32 desired_batt_voltage;
  762. /* The direction when the CB should be called. */
  763. u32 voltage_direction;
  764. /* The registered callback to be called when voltage and
  765. * direction specs are met. */
  766. u32 batt_cb_id;
  767. /* The call back data */
  768. u32 cb_data;
  769. u32 batt_error;
  770. };
  771. struct batt_client_registration_rep {
  772. u32 batt_handle;
  773. };
  774. struct batt_client_registration_rep_4_1 {
  775. u32 batt_handle;
  776. u32 more_data;
  777. u32 err;
  778. };
  779. static int msm_batt_register_arg_func(struct msm_rpc_client *batt_client,
  780. void *buf, void *data)
  781. {
  782. struct batt_client_registration_req *batt_reg_req =
  783. (struct batt_client_registration_req *)data;
  784. u32 *req = (u32 *)buf;
  785. int size = 0;
  786. if (msm_batt_info.batt_api_version == BATTERY_RPC_VER_4_1) {
  787. *req = cpu_to_be32(batt_reg_req->desired_batt_voltage);
  788. size += sizeof(u32);
  789. req++;
  790. *req = cpu_to_be32(batt_reg_req->voltage_direction);
  791. size += sizeof(u32);
  792. req++;
  793. *req = cpu_to_be32(batt_reg_req->batt_cb_id);
  794. size += sizeof(u32);
  795. req++;
  796. *req = cpu_to_be32(batt_reg_req->cb_data);
  797. size += sizeof(u32);
  798. req++;
  799. *req = cpu_to_be32(batt_reg_req->batt_error);
  800. size += sizeof(u32);
  801. return size;
  802. } else {
  803. *req = cpu_to_be32(batt_reg_req->desired_batt_voltage);
  804. size += sizeof(u32);
  805. req++;
  806. *req = cpu_to_be32(batt_reg_req->voltage_direction);
  807. size += sizeof(u32);
  808. req++;
  809. *req = cpu_to_be32(batt_reg_req->batt_cb_id);
  810. size += sizeof(u32);
  811. req++;
  812. *req = cpu_to_be32(batt_reg_req->cb_data);
  813. size += sizeof(u32);
  814. req++;
  815. *req = cpu_to_be32(batt_reg_req->more_data);
  816. size += sizeof(u32);
  817. req++;
  818. *req = cpu_to_be32(batt_reg_req->batt_error);
  819. size += sizeof(u32);
  820. return size;
  821. }
  822. }
  823. static int msm_batt_register_ret_func(struct msm_rpc_client *batt_client,
  824. void *buf, void *data)
  825. {
  826. struct batt_client_registration_rep *data_ptr, *buf_ptr;
  827. struct batt_client_registration_rep_4_1 *data_ptr_4_1, *buf_ptr_4_1;
  828. if (msm_batt_info.batt_api_version == BATTERY_RPC_VER_4_1) {
  829. data_ptr_4_1 = (struct batt_client_registration_rep_4_1 *)data;
  830. buf_ptr_4_1 = (struct batt_client_registration_rep_4_1 *)buf;
  831. data_ptr_4_1->batt_handle
  832. = be32_to_cpu(buf_ptr_4_1->batt_handle);
  833. data_ptr_4_1->more_data
  834. = be32_to_cpu(buf_ptr_4_1->more_data);
  835. data_ptr_4_1->err = be32_to_cpu(buf_ptr_4_1->err);
  836. return 0;
  837. } else {
  838. data_ptr = (struct batt_client_registration_rep *)data;
  839. buf_ptr = (struct batt_client_registration_rep *)buf;
  840. data_ptr->batt_handle = be32_to_cpu(buf_ptr->batt_handle);
  841. return 0;
  842. }
  843. }
  844. static int msm_batt_register(u32 desired_batt_voltage,
  845. u32 voltage_direction, u32 batt_cb_id, u32 cb_data)
  846. {
  847. struct batt_client_registration_req batt_reg_req;
  848. struct batt_client_registration_req_4_1 batt_reg_req_4_1;
  849. struct batt_client_registration_rep batt_reg_rep;
  850. struct batt_client_registration_rep_4_1 batt_reg_rep_4_1;
  851. void *request;
  852. void *reply;
  853. int rc;
  854. if (msm_batt_info.batt_api_version == BATTERY_RPC_VER_4_1) {
  855. batt_reg_req_4_1.desired_batt_voltage = desired_batt_voltage;
  856. batt_reg_req_4_1.voltage_direction = voltage_direction;
  857. batt_reg_req_4_1.batt_cb_id = batt_cb_id;
  858. batt_reg_req_4_1.cb_data = cb_data;
  859. batt_reg_req_4_1.batt_error = 1;
  860. request = &batt_reg_req_4_1;
  861. } else {
  862. batt_reg_req.desired_batt_voltage = desired_batt_voltage;
  863. batt_reg_req.voltage_direction = voltage_direction;
  864. batt_reg_req.batt_cb_id = batt_cb_id;
  865. batt_reg_req.cb_data = cb_data;
  866. batt_reg_req.more_data = 1;
  867. batt_reg_req.batt_error = 0;
  868. request = &batt_reg_req;
  869. }
  870. if (msm_batt_info.batt_api_version == BATTERY_RPC_VER_4_1)
  871. reply = &batt_reg_rep_4_1;
  872. else
  873. reply = &batt_reg_rep;
  874. rc = msm_rpc_client_req(msm_batt_info.batt_client,
  875. BATTERY_REGISTER_PROC,
  876. msm_batt_register_arg_func, request,
  877. msm_batt_register_ret_func, reply,
  878. msecs_to_jiffies(BATT_RPC_TIMEOUT));
  879. if (rc < 0) {
  880. pr_err("%s: FAIL: vbatt register. rc=%d\n", __func__, rc);
  881. return rc;
  882. }
  883. if (msm_batt_info.batt_api_version == BATTERY_RPC_VER_4_1) {
  884. if (batt_reg_rep_4_1.more_data != 0
  885. && batt_reg_rep_4_1.err
  886. != BATTERY_REGISTRATION_SUCCESSFUL) {
  887. pr_err("%s: vBatt Registration Failed proc_num=%d\n"
  888. , __func__, BATTERY_REGISTER_PROC);
  889. return -EIO;
  890. }
  891. msm_batt_info.batt_handle = batt_reg_rep_4_1.batt_handle;
  892. } else
  893. msm_batt_info.batt_handle = batt_reg_rep.batt_handle;
  894. return 0;
  895. }
  896. struct batt_client_deregister_req {
  897. u32 batt_handle;
  898. };
  899. struct batt_client_deregister_rep {
  900. u32 batt_error;
  901. };
  902. static int msm_batt_deregister_arg_func(struct msm_rpc_client *batt_client,
  903. void *buf, void *data)
  904. {
  905. struct batt_client_deregister_req *deregister_req =
  906. (struct batt_client_deregister_req *)data;
  907. u32 *req = (u32 *)buf;
  908. int size = 0;
  909. *req = cpu_to_be32(deregister_req->batt_handle);
  910. size += sizeof(u32);
  911. return size;
  912. }
  913. static int msm_batt_deregister_ret_func(struct msm_rpc_client *batt_client,
  914. void *buf, void *data)
  915. {
  916. struct batt_client_deregister_rep *data_ptr, *buf_ptr;
  917. data_ptr = (struct batt_client_deregister_rep *)data;
  918. buf_ptr = (struct batt_client_deregister_rep *)buf;
  919. data_ptr->batt_error = be32_to_cpu(buf_ptr->batt_error);
  920. return 0;
  921. }
  922. static int msm_batt_deregister(u32 batt_handle)
  923. {
  924. int rc;
  925. struct batt_client_deregister_req req;
  926. struct batt_client_deregister_rep rep;
  927. req.batt_handle = batt_handle;
  928. rc = msm_rpc_client_req(msm_batt_info.batt_client,
  929. BATTERY_DEREGISTER_CLIENT_PROC,
  930. msm_batt_deregister_arg_func, &req,
  931. msm_batt_deregister_ret_func, &rep,
  932. msecs_to_jiffies(BATT_RPC_TIMEOUT));
  933. if (rc < 0) {
  934. pr_err("%s: FAIL: vbatt deregister. rc=%d\n", __func__, rc);
  935. return rc;
  936. }
  937. if (rep.batt_error != BATTERY_DEREGISTRATION_SUCCESSFUL) {
  938. pr_err("%s: vbatt deregistration FAIL. error=%d, handle=%d\n",
  939. __func__, rep.batt_error, batt_handle);
  940. return -EIO;
  941. }
  942. return 0;
  943. }
  944. #endif /* CONFIG_BATTERY_MSM_FAKE */
  945. static int msm_batt_cleanup(void)
  946. {
  947. int rc = 0;
  948. #ifndef CONFIG_BATTERY_MSM_FAKE
  949. if (msm_batt_info.batt_handle != INVALID_BATT_HANDLE) {
  950. rc = msm_batt_deregister(msm_batt_info.batt_handle);
  951. if (rc < 0)
  952. pr_err("%s: FAIL: msm_batt_deregister. rc=%d\n",
  953. __func__, rc);
  954. }
  955. msm_batt_info.batt_handle = INVALID_BATT_HANDLE;
  956. if (msm_batt_info.batt_client)
  957. msm_rpc_unregister_client(msm_batt_info.batt_client);
  958. #endif /* CONFIG_BATTERY_MSM_FAKE */
  959. if (msm_batt_info.msm_psy_ac)
  960. power_supply_unregister(msm_batt_info.msm_psy_ac);
  961. if (msm_batt_info.msm_psy_usb)
  962. power_supply_unregister(msm_batt_info.msm_psy_usb);
  963. if (msm_batt_info.msm_psy_batt)
  964. power_supply_unregister(msm_batt_info.msm_psy_batt);
  965. #ifndef CONFIG_BATTERY_MSM_FAKE
  966. if (msm_batt_info.chg_ep) {
  967. rc = msm_rpc_close(msm_batt_info.chg_ep);
  968. if (rc < 0) {
  969. pr_err("%s: FAIL. msm_rpc_close(chg_ep). rc=%d\n",
  970. __func__, rc);
  971. }
  972. }
  973. #ifdef CONFIG_HAS_EARLYSUSPEND
  974. if (msm_batt_info.early_suspend.suspend == msm_batt_early_suspend)
  975. unregister_early_suspend(&msm_batt_info.early_suspend);
  976. #endif
  977. #endif
  978. return rc;
  979. }
  980. static u32 msm_batt_capacity(u32 current_voltage)
  981. {
  982. u32 low_voltage = msm_batt_info.voltage_min_design;
  983. u32 high_voltage = msm_batt_info.voltage_max_design;
  984. if (current_voltage <= low_voltage)
  985. return 0;
  986. else if (current_voltage >= high_voltage)
  987. return 100;
  988. else
  989. return (current_voltage - low_voltage) * 100
  990. / (high_voltage - low_voltage);
  991. }
  992. #ifndef CONFIG_BATTERY_MSM_FAKE
  993. int msm_batt_get_charger_api_version(void)
  994. {
  995. int rc ;
  996. struct rpc_reply_hdr *reply;
  997. struct rpc_req_chg_api_ver {
  998. struct rpc_request_hdr hdr;
  999. u32 more_data;
  1000. } req_chg_api_ver;
  1001. struct rpc_rep_chg_api_ver {
  1002. struct rpc_reply_hdr hdr;
  1003. u32 num_of_chg_api_versions;
  1004. u32 *chg_api_versions;
  1005. };
  1006. u32 num_of_versions;
  1007. struct rpc_rep_chg_api_ver *rep_chg_api_ver;
  1008. req_chg_api_ver.more_data = cpu_to_be32(1);
  1009. msm_rpc_setup_req(&req_chg_api_ver.hdr, CHG_RPC_PROG, CHG_RPC_VER_1_1,
  1010. ONCRPC_CHARGER_API_VERSIONS_PROC);
  1011. rc = msm_rpc_write(msm_batt_info.chg_ep, &req_chg_api_ver,
  1012. sizeof(req_chg_api_ver));
  1013. if (rc < 0) {
  1014. pr_err("%s: FAIL: msm_rpc_write. proc=0x%08x, rc=%d\n",
  1015. __func__, ONCRPC_CHARGER_API_VERSIONS_PROC, rc);
  1016. return rc;
  1017. }
  1018. for (;;) {
  1019. rc = msm_rpc_read(msm_batt_info.chg_ep, (void *) &reply, -1,
  1020. BATT_RPC_TIMEOUT);
  1021. if (rc < 0)
  1022. return rc;
  1023. if (rc < RPC_REQ_REPLY_COMMON_HEADER_SIZE) {
  1024. pr_err("%s: LENGTH ERR: msm_rpc_read. rc=%d (<%d)\n",
  1025. __func__, rc, RPC_REQ_REPLY_COMMON_HEADER_SIZE);
  1026. rc = -EIO;
  1027. break;
  1028. }
  1029. /* we should not get RPC REQ or call packets -- ignore them */
  1030. if (reply->type == RPC_TYPE_REQ) {
  1031. pr_err("%s: TYPE ERR: type=%d (!=%d)\n",
  1032. __func__, reply->type, RPC_TYPE_REQ);
  1033. kfree(reply);
  1034. continue;
  1035. }
  1036. /* If an earlier call timed out, we could get the (no
  1037. * longer wanted) reply for it. Ignore replies that
  1038. * we don't expect
  1039. */
  1040. if (reply->xid != req_chg_api_ver.hdr.xid) {
  1041. pr_err("%s: XID ERR: xid=%d (!=%d)\n", __func__,
  1042. reply->xid, req_chg_api_ver.hdr.xid);
  1043. kfree(reply);
  1044. continue;
  1045. }
  1046. if (reply->reply_stat != RPCMSG_REPLYSTAT_ACCEPTED) {
  1047. rc = -EPERM;
  1048. break;
  1049. }
  1050. if (reply->data.acc_hdr.accept_stat !=
  1051. RPC_ACCEPTSTAT_SUCCESS) {
  1052. rc = -EINVAL;
  1053. break;
  1054. }
  1055. rep_chg_api_ver = (struct rpc_rep_chg_api_ver *)reply;
  1056. num_of_versions =
  1057. be32_to_cpu(rep_chg_api_ver->num_of_chg_api_versions);
  1058. rep_chg_api_ver->chg_api_versions = (u32 *)
  1059. ((u8 *) reply + sizeof(struct rpc_reply_hdr) +
  1060. sizeof(rep_chg_api_ver->num_of_chg_api_versions));
  1061. rc = be32_to_cpu(
  1062. rep_chg_api_ver->chg_api_versions[num_of_versions - 1]);
  1063. pr_debug("%s: num_of_chg_api_versions = %u. "
  1064. "The chg api version = 0x%08x\n", __func__,
  1065. num_of_versions, rc);
  1066. break;
  1067. }
  1068. kfree(reply);
  1069. return rc;
  1070. }
  1071. static int msm_batt_cb_func(struct msm_rpc_client *client,
  1072. void *buffer, int in_size)
  1073. {
  1074. int rc = 0;
  1075. struct rpc_request_hdr *req;
  1076. u32 procedure;
  1077. u32 accept_status;
  1078. req = (struct rpc_request_hdr *)buffer;
  1079. procedure = be32_to_cpu(req->procedure);
  1080. switch (procedure) {
  1081. case BATTERY_CB_TYPE_PROC:
  1082. accept_status = RPC_ACCEPTSTAT_SUCCESS;
  1083. break;
  1084. default:
  1085. accept_status = RPC_ACCEPTSTAT_PROC_UNAVAIL;
  1086. pr_err("%s: ERROR. procedure (%d) not supported\n",
  1087. __func__, procedure);
  1088. break;
  1089. }
  1090. msm_rpc_start_accepted_reply(msm_batt_info.batt_client,
  1091. be32_to_cpu(req->xid), accept_status);
  1092. rc = msm_rpc_send_accepted_reply(msm_batt_info.batt_client, 0);
  1093. if (rc)
  1094. pr_err("%s: FAIL: sending reply. rc=%d\n", __func__, rc);
  1095. if (accept_status == RPC_ACCEPTSTAT_SUCCESS)
  1096. msm_batt_update_psy_status();
  1097. return rc;
  1098. }
  1099. #endif /* CONFIG_BATTERY_MSM_FAKE */
  1100. static int __devinit msm_batt_probe(struct platform_device *pdev)
  1101. {
  1102. int rc;
  1103. struct msm_psy_batt_pdata *pdata = pdev->dev.platform_data;
  1104. if (pdev->id != -1) {
  1105. dev_err(&pdev->dev,
  1106. "%s: MSM chipsets Can only support one"
  1107. " battery ", __func__);
  1108. return -EINVAL;
  1109. }
  1110. #ifndef CONFIG_BATTERY_MSM_FAKE
  1111. if (pdata->avail_chg_sources & AC_CHG) {
  1112. #else
  1113. {
  1114. #endif
  1115. rc = power_supply_register(&pdev->dev, &msm_psy_ac);
  1116. if (rc < 0) {
  1117. dev_err(&pdev->dev,
  1118. "%s: power_supply_register failed"
  1119. " rc = %d\n", __func__, rc);
  1120. msm_batt_cleanup();
  1121. return rc;
  1122. }
  1123. msm_batt_info.msm_psy_ac = &msm_psy_ac;
  1124. msm_batt_info.avail_chg_sources |= AC_CHG;
  1125. }
  1126. if (pdata->avail_chg_sources & USB_CHG) {
  1127. rc = power_supply_register(&pdev->dev, &msm_psy_usb);
  1128. if (rc < 0) {
  1129. dev_err(&pdev->dev,
  1130. "%s: power_supply_register failed"
  1131. " rc = %d\n", __func__, rc);
  1132. msm_batt_cleanup();
  1133. return rc;
  1134. }
  1135. msm_batt_info.msm_psy_usb = &msm_psy_usb;
  1136. msm_batt_info.avail_chg_sources |= USB_CHG;
  1137. }
  1138. if (!msm_batt_info.msm_psy_ac && !msm_batt_info.msm_psy_usb) {
  1139. dev_err(&pdev->dev,
  1140. "%s: No external Power supply(AC or USB)"
  1141. "is avilable\n", __func__);
  1142. msm_batt_cleanup();
  1143. return -ENODEV;
  1144. }
  1145. msm_batt_info.voltage_max_design = pdata->voltage_max_design;
  1146. msm_batt_info.voltage_min_design = pdata->voltage_min_design;
  1147. msm_batt_info.voltage_fail_safe = pdata->voltage_fail_safe;
  1148. msm_batt_info.batt_technology = pdata->batt_technology;
  1149. msm_batt_info.calculate_capacity = pdata->calculate_capacity;
  1150. if (!msm_batt_info.voltage_min_design)
  1151. msm_batt_info.voltage_min_design = BATTERY_LOW;
  1152. if (!msm_batt_info.voltage_max_design)
  1153. msm_batt_info.voltage_max_design = BATTERY_HIGH;
  1154. if (!msm_batt_info.voltage_fail_safe)
  1155. msm_batt_info.voltage_fail_safe = BATTERY_LOW;
  1156. if (msm_batt_info.batt_technology == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
  1157. msm_batt_info.batt_technology = POWER_SUPPLY_TECHNOLOGY_LION;
  1158. if (!msm_batt_info.calculate_capacity)
  1159. msm_batt_info.calculate_capacity = msm_batt_capacity;
  1160. rc = power_supply_register(&pdev->dev, &msm_psy_batt);
  1161. if (rc < 0) {
  1162. dev_err(&pdev->dev, "%s: power_supply_register failed"
  1163. " rc=%d\n", __func__, rc);
  1164. msm_batt_cleanup();
  1165. return rc;
  1166. }
  1167. msm_batt_info.msm_psy_batt = &msm_psy_batt;
  1168. #ifndef CONFIG_BATTERY_MSM_FAKE
  1169. rc = msm_batt_register(msm_batt_info.voltage_fail_safe,
  1170. BATTERY_ALL_ACTIVITY,
  1171. BATTERY_CB_ID_ALL_ACTIV,
  1172. BATTERY_ALL_ACTIVITY);
  1173. if (rc < 0) {
  1174. dev_err(&pdev->dev,
  1175. "%s: msm_batt_register failed rc = %d\n", __func__, rc);
  1176. msm_batt_cleanup();
  1177. return rc;
  1178. }
  1179. rc = msm_batt_enable_filter(VBATT_FILTER);
  1180. if (rc < 0) {
  1181. dev_err(&pdev->dev,
  1182. "%s: msm_batt_enable_filter failed rc = %d\n",
  1183. __func__, rc);
  1184. msm_batt_cleanup();
  1185. return rc;
  1186. }
  1187. #ifdef CONFIG_HAS_EARLYSUSPEND
  1188. msm_batt_info.early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
  1189. msm_batt_info.early_suspend.suspend = msm_batt_early_suspend;
  1190. msm_batt_info.early_suspend.resume = msm_batt_late_resume;
  1191. register_early_suspend(&msm_batt_info.early_suspend);
  1192. #endif
  1193. msm_batt_update_psy_status();
  1194. #else
  1195. power_supply_changed(&msm_psy_ac);
  1196. #endif /* CONFIG_BATTERY_MSM_FAKE */
  1197. return 0;
  1198. }
  1199. static int __devexit msm_batt_remove(struct platform_device *pdev)
  1200. {
  1201. int rc;
  1202. rc = msm_batt_cleanup();
  1203. if (rc < 0) {
  1204. dev_err(&pdev->dev,
  1205. "%s: msm_batt_cleanup failed rc=%d\n", __func__, rc);
  1206. return rc;
  1207. }
  1208. return 0;
  1209. }
  1210. static struct platform_driver msm_batt_driver = {
  1211. .probe = msm_batt_probe,
  1212. .remove = __devexit_p(msm_batt_remove),
  1213. .driver = {
  1214. .name = "msm-battery",
  1215. .owner = THIS_MODULE,
  1216. },
  1217. };
  1218. static int __devinit msm_batt_init_rpc(void)
  1219. {
  1220. int rc;
  1221. #ifdef CONFIG_BATTERY_MSM_FAKE
  1222. pr_info("Faking MSM battery\n");
  1223. #else
  1224. msm_batt_info.chg_ep =
  1225. msm_rpc_connect_compatible(CHG_RPC_PROG, CHG_RPC_VER_4_1, 0);
  1226. msm_batt_info.chg_api_version = CHG_RPC_VER_4_1;
  1227. if (msm_batt_info.chg_ep == NULL) {
  1228. pr_err("%s: rpc connect CHG_RPC_PROG = NULL\n", __func__);
  1229. return -ENODEV;
  1230. }
  1231. if (IS_ERR(msm_batt_info.chg_ep)) {
  1232. msm_batt_info.chg_ep = msm_rpc_connect_compatible(
  1233. CHG_RPC_PROG, CHG_RPC_VER_3_1, 0);
  1234. msm_batt_info.chg_api_version = CHG_RPC_VER_3_1;
  1235. }
  1236. if (IS_ERR(msm_batt_info.chg_ep)) {
  1237. msm_batt_info.chg_ep = msm_rpc_connect_compatible(
  1238. CHG_RPC_PROG, CHG_RPC_VER_1_1, 0);
  1239. msm_batt_info.chg_api_version = CHG_RPC_VER_1_1;
  1240. }
  1241. if (IS_ERR(msm_batt_info.chg_ep)) {
  1242. msm_batt_info.chg_ep = msm_rpc_connect_compatible(
  1243. CHG_RPC_PROG, CHG_RPC_VER_1_3, 0);
  1244. msm_batt_info.chg_api_version = CHG_RPC_VER_1_3;
  1245. }
  1246. if (IS_ERR(msm_batt_info.chg_ep)) {
  1247. msm_batt_info.chg_ep = msm_rpc_connect_compatible(
  1248. CHG_RPC_PROG, CHG_RPC_VER_2_2, 0);
  1249. msm_batt_info.chg_api_version = CHG_RPC_VER_2_2;
  1250. }
  1251. if (IS_ERR(msm_batt_info.chg_ep)) {
  1252. rc = PTR_ERR(msm_batt_info.chg_ep);
  1253. pr_err("%s: FAIL: rpc connect for CHG_RPC_PROG. rc=%d\n",
  1254. __func__, rc);
  1255. msm_batt_info.chg_ep = NULL;
  1256. return rc;
  1257. }
  1258. /* Get the real 1.x version */
  1259. if (msm_batt_info.chg_api_version == CHG_RPC_VER_1_1)
  1260. msm_batt_info.chg_api_version =
  1261. msm_batt_get_charger_api_version();
  1262. /* Fall back to 1.1 for default */
  1263. if (msm_batt_info.chg_api_version < 0)
  1264. msm_batt_info.chg_api_version = CHG_RPC_VER_1_1;
  1265. msm_batt_info.batt_api_version = BATTERY_RPC_VER_4_1;
  1266. msm_batt_info.batt_client =
  1267. msm_rpc_register_client("battery", BATTERY_RPC_PROG,
  1268. BATTERY_RPC_VER_4_1,
  1269. 1, msm_batt_cb_func);
  1270. if (msm_batt_info.batt_client == NULL) {
  1271. pr_err("%s: FAIL: rpc_register_client. batt_client=NULL\n",
  1272. __func__);
  1273. return -ENODEV;
  1274. }
  1275. if (IS_ERR(msm_batt_info.batt_client)) {
  1276. msm_batt_info.batt_client =
  1277. msm_rpc_register_client("battery", BATTERY_RPC_PROG,
  1278. BATTERY_RPC_VER_1_1,
  1279. 1, msm_batt_cb_func);
  1280. msm_batt_info.batt_api_version = BATTERY_RPC_VER_1_1;
  1281. }
  1282. if (IS_ERR(msm_batt_info.batt_client)) {
  1283. msm_batt_info.batt_client =
  1284. msm_rpc_register_client("battery", BATTERY_RPC_PROG,
  1285. BATTERY_RPC_VER_2_1,
  1286. 1, msm_batt_cb_func);
  1287. msm_batt_info.batt_api_version = BATTERY_RPC_VER_2_1;
  1288. }
  1289. if (IS_ERR(msm_batt_info.batt_client)) {
  1290. msm_batt_info.batt_client =
  1291. msm_rpc_register_client("battery", BATTERY_RPC_PROG,
  1292. BATTERY_RPC_VER_5_1,
  1293. 1, msm_batt_cb_func);
  1294. msm_batt_info.batt_api_version = BATTERY_RPC_VER_5_1;
  1295. }
  1296. if (IS_ERR(msm_batt_info.batt_client)) {
  1297. rc = PTR_ERR(msm_batt_info.batt_client);
  1298. pr_err("%s: ERROR: rpc_register_client: rc = %d\n ",
  1299. __func__, rc);
  1300. msm_batt_info.batt_client = NULL;
  1301. return rc;
  1302. }
  1303. #endif /* CONFIG_BATTERY_MSM_FAKE */
  1304. rc = platform_driver_register(&msm_batt_driver);
  1305. if (rc < 0)
  1306. pr_err("%s: FAIL: platform_driver_register. rc = %d\n",
  1307. __func__, rc);
  1308. return rc;
  1309. }
  1310. static int __init msm_batt_init(void)
  1311. {
  1312. int rc;
  1313. pr_debug("%s: enter\n", __func__);
  1314. rc = msm_batt_init_rpc();
  1315. if (rc < 0) {
  1316. pr_err("%s: FAIL: msm_batt_init_rpc. rc=%d\n", __func__, rc);
  1317. msm_batt_cleanup();
  1318. return rc;
  1319. }
  1320. pr_info("%s: Charger/Battery = 0x%08x/0x%08x (RPC version)\n",
  1321. __func__, msm_batt_info.chg_api_version,
  1322. msm_batt_info.batt_api_version);
  1323. return 0;
  1324. }
  1325. static void __exit msm_batt_exit(void)
  1326. {
  1327. platform_driver_unregister(&msm_batt_driver);
  1328. }
  1329. module_init(msm_batt_init);
  1330. module_exit(msm_batt_exit);
  1331. MODULE_LICENSE("GPL v2");
  1332. MODULE_AUTHOR("Kiran Kandi, Qualcomm Innovation Center, Inc.");
  1333. MODULE_DESCRIPTION("Battery driver for Qualcomm MSM chipsets.");
  1334. MODULE_VERSION("1.0");
  1335. MODULE_ALIAS("platform:msm_battery");