battery.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "QCOM-BATT: %s: " fmt, __func__
  7. #include <linux/debugfs.h>
  8. #include <linux/device.h>
  9. #include <linux/delay.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/power_supply.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/of.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/printk.h>
  18. #include <linux/pm_wakeup.h>
  19. #include <linux/slab.h>
  20. #include <linux/pmic-voter.h>
  21. #include <linux/qti_power_supply.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/iio/consumer.h>
  24. #include <dt-bindings/iio/qti_power_supply_iio.h>
  25. #include "battery.h"
  26. #include "smb5-iio.h"
  27. #define DRV_MAJOR_VERSION 1
  28. #define DRV_MINOR_VERSION 0
  29. #define BATT_PROFILE_VOTER "BATT_PROFILE_VOTER"
  30. #define CHG_STATE_VOTER "CHG_STATE_VOTER"
  31. #define TAPER_STEPPER_VOTER "TAPER_STEPPER_VOTER"
  32. #define TAPER_END_VOTER "TAPER_END_VOTER"
  33. #define PL_TAPER_EARLY_BAD_VOTER "PL_TAPER_EARLY_BAD_VOTER"
  34. #define PARALLEL_PSY_VOTER "PARALLEL_PSY_VOTER"
  35. #define PL_HW_ABSENT_VOTER "PL_HW_ABSENT_VOTER"
  36. #define PL_VOTER "PL_VOTER"
  37. #define RESTRICT_CHG_VOTER "RESTRICT_CHG_VOTER"
  38. #define ICL_CHANGE_VOTER "ICL_CHANGE_VOTER"
  39. #define PL_INDIRECT_VOTER "PL_INDIRECT_VOTER"
  40. #define USBIN_I_VOTER "USBIN_I_VOTER"
  41. #define PL_FCC_LOW_VOTER "PL_FCC_LOW_VOTER"
  42. #define ICL_LIMIT_VOTER "ICL_LIMIT_VOTER"
  43. #define FCC_STEPPER_VOTER "FCC_STEPPER_VOTER"
  44. #define FCC_VOTER "FCC_VOTER"
  45. #define MAIN_FCC_VOTER "MAIN_FCC_VOTER"
  46. #define PD_VOTER "PD_VOTER"
  47. /* PMI8998 */
  48. #define PMI8998_SUBTYPE 0x15
  49. /* PM660 */
  50. #define PM660_SUBTYPE 0x1B
  51. struct pl_data {
  52. struct device *dev;
  53. int pl_mode;
  54. int pl_batfet_mode;
  55. int pl_min_icl_ua;
  56. int slave_pct;
  57. int slave_fcc_ua;
  58. int main_fcc_ua;
  59. int restricted_current;
  60. bool restricted_charging_enabled;
  61. struct votable *fcc_votable;
  62. struct votable *fv_votable;
  63. struct votable *pl_disable_votable;
  64. struct votable *pl_awake_votable;
  65. struct votable *hvdcp_hw_inov_dis_votable;
  66. struct votable *usb_icl_votable;
  67. struct votable *pl_enable_votable_indirect;
  68. struct votable *cp_ilim_votable;
  69. struct votable *cp_disable_votable;
  70. struct votable *fcc_main_votable;
  71. struct votable *cp_slave_disable_votable;
  72. struct delayed_work status_change_work;
  73. struct work_struct pl_disable_forever_work;
  74. struct work_struct pl_taper_work;
  75. struct delayed_work fcc_stepper_work;
  76. bool taper_work_running;
  77. struct power_supply *pl_psy;
  78. struct power_supply *batt_psy;
  79. struct power_supply *usb_psy;
  80. struct power_supply *dc_psy;
  81. struct power_supply *cp_master_psy;
  82. int charge_type;
  83. int total_settled_ua;
  84. int pl_settled_ua;
  85. int pl_fcc_max;
  86. int fcc_stepper_enable;
  87. int main_step_fcc_dir;
  88. int main_step_fcc_count;
  89. int main_step_fcc_residual;
  90. int parallel_step_fcc_dir;
  91. int parallel_step_fcc_count;
  92. int parallel_step_fcc_residual;
  93. int step_fcc;
  94. int override_main_fcc_ua;
  95. int total_fcc_ua;
  96. u32 wa_flags;
  97. struct class qcom_batt_class;
  98. struct wakeup_source *pl_ws;
  99. struct notifier_block nb;
  100. struct charger_param *chg_param;
  101. bool pl_disable;
  102. bool cp_disabled;
  103. int taper_entry_fv;
  104. int main_fcc_max;
  105. int charger_type;
  106. /* debugfs directory */
  107. struct dentry *dfs_root;
  108. u32 float_voltage_uv;
  109. struct iio_channel **iio_chan_list_cp;
  110. struct iio_channel *iio_chan_list_cp_slave;
  111. struct iio_channel **iio_chan_list_smb_parallel;
  112. };
  113. static struct pl_data *the_chip;
  114. enum print_reason {
  115. PR_PARALLEL = BIT(0),
  116. };
  117. enum {
  118. AICL_RERUN_WA_BIT = BIT(0),
  119. FORCE_INOV_DISABLE_BIT = BIT(1),
  120. };
  121. static int debug_mask;
  122. #define pl_dbg(chip, reason, fmt, ...) \
  123. do { \
  124. if (debug_mask & (reason)) \
  125. pr_info(fmt, ##__VA_ARGS__); \
  126. else \
  127. pr_debug(fmt, ##__VA_ARGS__); \
  128. } while (0)
  129. #define IS_USBIN(mode) ((mode == QTI_POWER_SUPPLY_PL_USBIN_USBIN) \
  130. || (mode == QTI_POWER_SUPPLY_PL_USBIN_USBIN_EXT))
  131. enum {
  132. VER = 0,
  133. SLAVE_PCT,
  134. RESTRICT_CHG_ENABLE,
  135. RESTRICT_CHG_CURRENT,
  136. FCC_STEPPING_IN_PROGRESS,
  137. };
  138. enum {
  139. PARALLEL_INPUT_MODE,
  140. PARALLEL_OUTPUT_MODE,
  141. };
  142. /* CP Channels */
  143. static const char * const bat_cp_ext_iio_chan[] = {
  144. [BAT_CP_PARALLEL_MODE] = "cp_parallel_mode",
  145. [BAT_CP_PARALLEL_OUTPUT_MODE] = "cp_parallel_output_mode",
  146. [BAT_CP_MIN_ICL] = "cp_min_icl",
  147. [BAT_CP_SWITCHER_EN] = "cp_switcher_en",
  148. };
  149. /* SMB1355 Channels */
  150. static const char * const bat_smb_parallel_ext_iio_chan[] = {
  151. [BAT_SMB_PARALLEL_INPUT_SUSPEND] = "pl_input_suspend",
  152. [BAT_SMB_PARALLEL_MODE] = "pl_mode",
  153. [BAT_SMB_PARALLEL_BATFET_MODE] = "pl_batfet_mode",
  154. [BAT_SMB_PARALLEL_MIN_ICL] = "pl_min_icl",
  155. [BAT_SMB_PARALLEL_FCC_MAX] = "pl_fcc_max",
  156. [BAT_SMB_PARALLEL_CURRENT_MAX] = "pl_current_max",
  157. [BAT_SMB_PARALLEL_CONSTANT_CHARGE_CURRENT_MAX] =
  158. "pl_constant_charge_current_max",
  159. [BAT_SMB_PARALLEL_VOLTAGE_MAX] = "pl_voltage_max",
  160. [BAT_SMB_PARALLEL_CHARGE_TYPE] = "pl_charge_type",
  161. };
  162. /*********
  163. * HELPER*
  164. *********/
  165. static bool is_usb_available(struct pl_data *chip)
  166. {
  167. if (!chip->usb_psy)
  168. chip->usb_psy =
  169. power_supply_get_by_name("usb");
  170. return !!chip->usb_psy;
  171. }
  172. static bool is_cp_available(struct pl_data *chip)
  173. {
  174. int rc;
  175. struct iio_channel **iio_list;
  176. if (IS_ERR(chip->iio_chan_list_cp))
  177. return false;
  178. if (!chip->iio_chan_list_cp) {
  179. iio_list = get_ext_channels(chip->dev,
  180. bat_cp_ext_iio_chan,
  181. ARRAY_SIZE(bat_cp_ext_iio_chan));
  182. if (IS_ERR(iio_list)) {
  183. rc = PTR_ERR(iio_list);
  184. if (rc != -EPROBE_DEFER) {
  185. dev_err(chip->dev, "Failed to get channels, %d\n",
  186. rc);
  187. chip->iio_chan_list_cp = ERR_PTR(-EINVAL);
  188. }
  189. return false;
  190. }
  191. chip->iio_chan_list_cp = iio_list;
  192. }
  193. if (!chip->cp_master_psy) {
  194. chip->cp_master_psy =
  195. power_supply_get_by_name("charge_pump_master");
  196. if (!chip->cp_master_psy)
  197. return false;
  198. }
  199. return true;
  200. }
  201. static int battery_read_iio_prop(struct pl_data *chip,
  202. enum iio_type type, int iio_chan, int *val)
  203. {
  204. struct iio_channel *iio_chan_list;
  205. int rc;
  206. switch (type) {
  207. case CP:
  208. if (IS_ERR_OR_NULL(chip->iio_chan_list_cp))
  209. return -ENODEV;
  210. iio_chan_list = chip->iio_chan_list_cp[iio_chan];
  211. break;
  212. case SMB_PARALLEL:
  213. if (IS_ERR_OR_NULL(chip->iio_chan_list_smb_parallel))
  214. return -ENODEV;
  215. iio_chan_list = chip->iio_chan_list_smb_parallel[iio_chan];
  216. break;
  217. default:
  218. pr_err_ratelimited("iio_type %d is not supported\n", type);
  219. return -EINVAL;
  220. }
  221. rc = iio_read_channel_processed(iio_chan_list, val);
  222. return (rc < 0) ? rc : 0;
  223. }
  224. static int battery_write_iio_prop(struct pl_data *chip,
  225. enum iio_type type, int iio_chan, int val)
  226. {
  227. struct iio_channel *iio_chan_list;
  228. switch (type) {
  229. case CP:
  230. if (IS_ERR_OR_NULL(chip->iio_chan_list_cp))
  231. return -ENODEV;
  232. iio_chan_list = chip->iio_chan_list_cp[iio_chan];
  233. break;
  234. case SMB_PARALLEL:
  235. if (IS_ERR_OR_NULL(chip->iio_chan_list_smb_parallel))
  236. return -ENODEV;
  237. iio_chan_list = chip->iio_chan_list_smb_parallel[iio_chan];
  238. break;
  239. default:
  240. pr_err_ratelimited("iio_type %d is not supported\n", type);
  241. return -EINVAL;
  242. }
  243. return iio_write_channel_raw(iio_chan_list, val);
  244. }
  245. static int cp_get_parallel_mode(struct pl_data *chip, int mode)
  246. {
  247. int rc = -EINVAL, val = -EINVAL;
  248. if (!is_cp_available(chip))
  249. return -EINVAL;
  250. switch (mode) {
  251. case PARALLEL_INPUT_MODE:
  252. rc = battery_read_iio_prop(chip, CP,
  253. BAT_CP_PARALLEL_MODE, &val);
  254. break;
  255. case PARALLEL_OUTPUT_MODE:
  256. rc = battery_read_iio_prop(chip, CP,
  257. BAT_CP_PARALLEL_OUTPUT_MODE,
  258. &val);
  259. break;
  260. default:
  261. pr_err("Invalid mode request %d\n", mode);
  262. break;
  263. }
  264. if (rc < 0)
  265. pr_err("Failed to read CP topology for mode=%d rc=%d\n",
  266. mode, rc);
  267. return val;
  268. }
  269. static int get_adapter_icl_based_ilim(struct pl_data *chip)
  270. {
  271. int main_icl = -EINVAL, adapter_icl = -EINVAL, final_icl = -EINVAL;
  272. int rc = -EINVAL, pval = 0;
  273. rc = chip->chg_param->iio_read(chip->dev,
  274. PSY_IIO_PD_ACTIVE, &pval);
  275. if (rc < 0)
  276. pr_err("Failed to read PD_ACTIVE status rc=%d\n",
  277. rc);
  278. /* Check for QC 3, 3.5 and PPS adapters, return if its none of them */
  279. if (chip->charger_type != QTI_POWER_SUPPLY_TYPE_USB_HVDCP_3 &&
  280. chip->charger_type != QTI_POWER_SUPPLY_TYPE_USB_HVDCP_3P5 &&
  281. pval != QTI_POWER_SUPPLY_PD_PPS_ACTIVE)
  282. return final_icl;
  283. /*
  284. * For HVDCP3/HVDCP_3P5 adapters, limit max. ILIM as:
  285. * HVDCP3_ICL: Maximum ICL of HVDCP3 adapter(from DT
  286. * configuration).
  287. *
  288. * For PPS adapters, limit max. ILIM to
  289. * MIN(qc4_max_icl, PD_CURRENT_MAX)
  290. */
  291. if (pval == QTI_POWER_SUPPLY_PD_PPS_ACTIVE) {
  292. adapter_icl = min_t(int, chip->chg_param->qc4_max_icl_ua,
  293. get_client_vote_locked(chip->usb_icl_votable,
  294. PD_VOTER));
  295. if (adapter_icl <= 0)
  296. adapter_icl = chip->chg_param->qc4_max_icl_ua;
  297. } else {
  298. adapter_icl = chip->chg_param->hvdcp3_max_icl_ua;
  299. }
  300. /*
  301. * For Parallel input configurations:
  302. * VBUS: final_icl = adapter_icl - main_ICL
  303. * VMID: final_icl = adapter_icl
  304. */
  305. final_icl = adapter_icl;
  306. if (cp_get_parallel_mode(chip, PARALLEL_INPUT_MODE)
  307. == QTI_POWER_SUPPLY_PL_USBIN_USBIN) {
  308. main_icl = get_effective_result_locked(chip->usb_icl_votable);
  309. if ((main_icl >= 0) && (main_icl < adapter_icl))
  310. final_icl = adapter_icl - main_icl;
  311. }
  312. pr_debug("charger_type=%d final_icl=%d adapter_icl=%d main_icl=%d\n",
  313. chip->charger_type, final_icl, adapter_icl, main_icl);
  314. return final_icl;
  315. }
  316. /*
  317. * Adapter CC Mode: ILIM over-ridden explicitly, below takes no effect.
  318. *
  319. * Adapter CV mode: Configuration of ILIM for different topology is as below:
  320. * MID-VPH:
  321. * SMB1390 ILIM: independent of FCC and based on the AICL result or
  322. * PD advertised current, handled directly in SMB1390
  323. * driver.
  324. * MID-VBAT:
  325. * SMB1390 ILIM: based on minimum of FCC portion of SMB1390 or ICL.
  326. * USBIN-VBAT:
  327. * SMB1390 ILIM: based on FCC portion of SMB1390 and independent of ICL.
  328. */
  329. static void cp_configure_ilim(struct pl_data *chip, const char *voter, int ilim)
  330. {
  331. int rc = 0, fcc, target_icl, val;
  332. if (!is_usb_available(chip))
  333. return;
  334. if (!is_cp_available(chip))
  335. return;
  336. if (cp_get_parallel_mode(chip, PARALLEL_OUTPUT_MODE)
  337. == QTI_POWER_SUPPLY_PL_OUTPUT_VPH)
  338. return;
  339. target_icl = get_adapter_icl_based_ilim(chip);
  340. ilim = (target_icl > 0) ? min(ilim, target_icl) : ilim;
  341. rc = battery_read_iio_prop(chip, CP, BAT_CP_MIN_ICL, &val);
  342. if (rc < 0) {
  343. pr_err("Failed to read min_icl rc=%d\n", rc);
  344. return;
  345. }
  346. if (!chip->cp_ilim_votable)
  347. chip->cp_ilim_votable = find_votable("CP_ILIM");
  348. if (chip->cp_ilim_votable) {
  349. fcc = get_effective_result_locked(chip->fcc_votable);
  350. /*
  351. * If FCC >= (2 * MIN_ICL) then it is safe to enable CP
  352. * with MIN_ICL.
  353. * Configure ILIM as follows:
  354. * if request_ilim < MIN_ICL cofigure ILIM to MIN_ICL.
  355. * otherwise configure ILIM to requested_ilim.
  356. */
  357. if ((fcc >= (val * 2)) && (ilim < val))
  358. vote(chip->cp_ilim_votable, voter, true, val);
  359. else
  360. vote(chip->cp_ilim_votable, voter, true, ilim);
  361. /*
  362. * Rerun FCC votable to ensure offset for ILIM compensation is
  363. * recalculated based on new ILIM.
  364. */
  365. if (!chip->fcc_main_votable)
  366. chip->fcc_main_votable = find_votable("FCC_MAIN");
  367. if ((chip->charger_type == QTI_POWER_SUPPLY_TYPE_USB_HVDCP_3)
  368. && chip->fcc_main_votable)
  369. rerun_election(chip->fcc_main_votable);
  370. pl_dbg(chip, PR_PARALLEL,
  371. "ILIM: vote: %d voter:%s min_ilim=%d fcc = %d\n",
  372. ilim, voter, val, fcc);
  373. }
  374. }
  375. /*******
  376. * ICL *
  377. ********/
  378. static int get_settled_split(struct pl_data *chip, int *main_icl_ua,
  379. int *slave_icl_ua, int *total_settled_icl_ua)
  380. {
  381. int slave_icl_pct, total_current_ua;
  382. int slave_ua = 0, main_settled_ua = 0;
  383. int rc, total_settled_ua = 0;
  384. int val;
  385. if (!IS_USBIN(chip->pl_mode))
  386. return -EINVAL;
  387. if (!get_effective_result_locked(chip->pl_disable_votable)) {
  388. /* read the aicl settled value */
  389. rc = chip->chg_param->iio_read(chip->dev,
  390. PSY_IIO_MAIN_INPUT_CURRENT_SETTLED, &val);
  391. if (rc < 0) {
  392. pr_err("Couldn't get aicl settled value rc=%d\n", rc);
  393. return rc;
  394. }
  395. main_settled_ua = val;
  396. slave_icl_pct = max(0, chip->slave_pct);
  397. slave_ua = ((main_settled_ua + chip->pl_settled_ua)
  398. * slave_icl_pct) / 100;
  399. total_settled_ua = main_settled_ua + chip->pl_settled_ua;
  400. }
  401. total_current_ua = get_effective_result_locked(chip->usb_icl_votable);
  402. if (total_current_ua < 0) {
  403. if (!chip->usb_psy)
  404. chip->usb_psy = power_supply_get_by_name("usb");
  405. if (!chip->usb_psy) {
  406. pr_err("Couldn't get usbpsy while splitting settled\n");
  407. return -ENOENT;
  408. }
  409. /* no client is voting, so get the total current from charger */
  410. rc = chip->chg_param->iio_read(chip->dev,
  411. PSY_IIO_HW_CURRENT_MAX, &val);
  412. if (rc < 0) {
  413. pr_err("Couldn't get max current rc=%d\n", rc);
  414. return rc;
  415. }
  416. total_current_ua = val;
  417. }
  418. *main_icl_ua = total_current_ua - slave_ua;
  419. *slave_icl_ua = slave_ua;
  420. *total_settled_icl_ua = total_settled_ua;
  421. pl_dbg(chip, PR_PARALLEL,
  422. "Split total_current_ua=%d total_settled_ua=%d main_settled_ua=%d slave_ua=%d\n",
  423. total_current_ua, total_settled_ua, main_settled_ua, slave_ua);
  424. return 0;
  425. }
  426. static int validate_parallel_icl(struct pl_data *chip, bool *disable)
  427. {
  428. int rc = 0;
  429. int main_ua = 0, slave_ua = 0, total_settled_ua = 0;
  430. if (!IS_USBIN(chip->pl_mode)
  431. || get_effective_result_locked(chip->pl_disable_votable))
  432. return 0;
  433. rc = get_settled_split(chip, &main_ua, &slave_ua, &total_settled_ua);
  434. if (rc < 0) {
  435. pr_err("Couldn't get split current rc=%d\n", rc);
  436. return rc;
  437. }
  438. if (slave_ua < chip->pl_min_icl_ua)
  439. *disable = true;
  440. else
  441. *disable = false;
  442. return 0;
  443. }
  444. static void split_settled(struct pl_data *chip)
  445. {
  446. int rc, main_ua, slave_ua, total_settled_ua;
  447. rc = get_settled_split(chip, &main_ua, &slave_ua, &total_settled_ua);
  448. if (rc < 0) {
  449. pr_err("Couldn't get split current rc=%d\n", rc);
  450. return;
  451. }
  452. /*
  453. * If there is an increase in slave share
  454. * (Also handles parallel enable case)
  455. * Set Main ICL then slave ICL
  456. * else
  457. * (Also handles parallel disable case)
  458. * Set slave ICL then main ICL.
  459. */
  460. if (slave_ua > chip->pl_settled_ua) {
  461. /* Set ICL on main charger */
  462. rc = chip->chg_param->iio_write(chip->dev, PSY_IIO_CURRENT_MAX,
  463. main_ua);
  464. if (rc < 0) {
  465. pr_err("Couldn't change slave suspend state rc=%d\n",
  466. rc);
  467. return;
  468. }
  469. /* set parallel's ICL could be 0mA when pl is disabled */
  470. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  471. BAT_SMB_PARALLEL_CURRENT_MAX, slave_ua);
  472. if (rc < 0) {
  473. pr_err("Couldn't set parallel icl, rc=%d\n", rc);
  474. return;
  475. }
  476. } else {
  477. /* set parallel's ICL could be 0mA when pl is disabled */
  478. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  479. BAT_SMB_PARALLEL_CURRENT_MAX, slave_ua);
  480. if (rc < 0) {
  481. pr_err("Couldn't set parallel icl, rc=%d\n", rc);
  482. return;
  483. }
  484. /* Set ICL on main charger */
  485. rc = chip->chg_param->iio_write(chip->dev, PSY_IIO_CURRENT_MAX,
  486. main_ua);
  487. if (rc < 0) {
  488. pr_err("Couldn't change slave suspend state rc=%d\n",
  489. rc);
  490. return;
  491. }
  492. }
  493. chip->total_settled_ua = total_settled_ua;
  494. chip->pl_settled_ua = slave_ua;
  495. }
  496. static ssize_t version_show(struct class *c, struct class_attribute *attr,
  497. char *buf)
  498. {
  499. return scnprintf(buf, PAGE_SIZE, "%d.%d\n",
  500. DRV_MAJOR_VERSION, DRV_MINOR_VERSION);
  501. }
  502. static CLASS_ATTR_RO(version);
  503. /*************
  504. * SLAVE PCT *
  505. **************/
  506. static ssize_t slave_pct_show(struct class *c, struct class_attribute *attr,
  507. char *ubuf)
  508. {
  509. struct pl_data *chip = container_of(c, struct pl_data,
  510. qcom_batt_class);
  511. return scnprintf(ubuf, PAGE_SIZE, "%d\n", chip->slave_pct);
  512. }
  513. static ssize_t slave_pct_store(struct class *c, struct class_attribute *attr,
  514. const char *ubuf, size_t count)
  515. {
  516. struct pl_data *chip = container_of(c, struct pl_data, qcom_batt_class);
  517. int rc;
  518. unsigned long val;
  519. bool disable = false;
  520. if (kstrtoul(ubuf, 10, &val))
  521. return -EINVAL;
  522. chip->slave_pct = val;
  523. rc = validate_parallel_icl(chip, &disable);
  524. if (rc < 0)
  525. return rc;
  526. vote(chip->pl_disable_votable, ICL_LIMIT_VOTER, disable, 0);
  527. rerun_election(chip->fcc_votable);
  528. rerun_election(chip->fv_votable);
  529. if (IS_USBIN(chip->pl_mode))
  530. split_settled(chip);
  531. return count;
  532. }
  533. static struct class_attribute class_attr_slave_pct =
  534. __ATTR(parallel_pct, 0644, slave_pct_show, slave_pct_store);
  535. /************************
  536. * RESTRICTED CHARGIGNG *
  537. ************************/
  538. static ssize_t restrict_chg_show(struct class *c, struct class_attribute *attr,
  539. char *ubuf)
  540. {
  541. struct pl_data *chip = container_of(c, struct pl_data,
  542. qcom_batt_class);
  543. return scnprintf(ubuf, PAGE_SIZE, "%d\n",
  544. chip->restricted_charging_enabled);
  545. }
  546. static ssize_t restrict_chg_store(struct class *c, struct class_attribute *attr,
  547. const char *ubuf, size_t count)
  548. {
  549. struct pl_data *chip = container_of(c, struct pl_data,
  550. qcom_batt_class);
  551. unsigned long val;
  552. if (kstrtoul(ubuf, 10, &val))
  553. return -EINVAL;
  554. if (chip->restricted_charging_enabled == !!val)
  555. goto no_change;
  556. chip->restricted_charging_enabled = !!val;
  557. /* disable parallel charger in case of restricted charging */
  558. vote(chip->pl_disable_votable, RESTRICT_CHG_VOTER,
  559. chip->restricted_charging_enabled, 0);
  560. vote(chip->fcc_votable, RESTRICT_CHG_VOTER,
  561. chip->restricted_charging_enabled,
  562. chip->restricted_current);
  563. no_change:
  564. return count;
  565. }
  566. static CLASS_ATTR_RW(restrict_chg);
  567. static ssize_t restrict_cur_show(struct class *c, struct class_attribute *attr,
  568. char *ubuf)
  569. {
  570. struct pl_data *chip = container_of(c, struct pl_data,
  571. qcom_batt_class);
  572. return scnprintf(ubuf, PAGE_SIZE, "%d\n", chip->restricted_current);
  573. }
  574. static ssize_t restrict_cur_store(struct class *c, struct class_attribute *attr,
  575. const char *ubuf, size_t count)
  576. {
  577. struct pl_data *chip = container_of(c, struct pl_data,
  578. qcom_batt_class);
  579. unsigned long val;
  580. if (kstrtoul(ubuf, 10, &val))
  581. return -EINVAL;
  582. chip->restricted_current = val;
  583. vote(chip->fcc_votable, RESTRICT_CHG_VOTER,
  584. chip->restricted_charging_enabled,
  585. chip->restricted_current);
  586. return count;
  587. }
  588. static CLASS_ATTR_RW(restrict_cur);
  589. /****************************
  590. * FCC STEPPING IN PROGRESS *
  591. ****************************/
  592. static ssize_t fcc_stepping_in_progress_show(struct class *c,
  593. struct class_attribute *attr, char *ubuf)
  594. {
  595. struct pl_data *chip = container_of(c, struct pl_data,
  596. qcom_batt_class);
  597. return scnprintf(ubuf, PAGE_SIZE, "%d\n", chip->step_fcc);
  598. }
  599. static CLASS_ATTR_RO(fcc_stepping_in_progress);
  600. static struct attribute *batt_class_attrs[] = {
  601. [VER] = &class_attr_version.attr,
  602. [SLAVE_PCT] = &class_attr_slave_pct.attr,
  603. [RESTRICT_CHG_ENABLE] = &class_attr_restrict_chg.attr,
  604. [RESTRICT_CHG_CURRENT] = &class_attr_restrict_cur.attr,
  605. [FCC_STEPPING_IN_PROGRESS]
  606. = &class_attr_fcc_stepping_in_progress.attr,
  607. NULL,
  608. };
  609. ATTRIBUTE_GROUPS(batt_class);
  610. /*********
  611. * FCC *
  612. **********/
  613. #define EFFICIENCY_PCT 80
  614. #define STEP_UP 1
  615. #define STEP_DOWN -1
  616. static void get_fcc_split(struct pl_data *chip, int total_ua,
  617. int *master_ua, int *slave_ua)
  618. {
  619. int rc, effective_total_ua, slave_limited_ua, hw_cc_delta_ua = 0,
  620. icl_ua, adapter_uv, bcl_ua, val;
  621. rc = chip->chg_param->iio_read(chip->dev, PSY_IIO_FCC_DELTA, &val);
  622. if (rc < 0) {
  623. pr_err("Couldn't get fcc_delta rc=%d\n", rc);
  624. hw_cc_delta_ua = 0;
  625. } else {
  626. hw_cc_delta_ua = val;
  627. }
  628. bcl_ua = INT_MAX;
  629. if (chip->pl_mode == QTI_POWER_SUPPLY_PL_USBMID_USBMID) {
  630. rc = chip->chg_param->iio_read(chip->dev,
  631. PSY_IIO_MAIN_INPUT_CURRENT_SETTLED, &val);
  632. if (rc < 0) {
  633. pr_err("Couldn't get aicl settled value rc=%d\n", rc);
  634. return;
  635. }
  636. icl_ua = val;
  637. rc = chip->chg_param->iio_read(chip->dev,
  638. PSY_IIO_MAIN_INPUT_VOLTAGE_SETTLED, &val);
  639. if (rc < 0) {
  640. pr_err("Couldn't get adaptive voltage rc=%d\n", rc);
  641. return;
  642. }
  643. adapter_uv = val;
  644. bcl_ua = div64_s64((s64)icl_ua * adapter_uv * EFFICIENCY_PCT,
  645. (s64)get_effective_result(chip->fv_votable) * 100);
  646. }
  647. effective_total_ua = max(0, total_ua + hw_cc_delta_ua);
  648. slave_limited_ua = min(effective_total_ua, bcl_ua);
  649. *slave_ua = (slave_limited_ua * chip->slave_pct) / 100;
  650. *slave_ua = min(*slave_ua, chip->pl_fcc_max);
  651. /*
  652. * In stacked BATFET configuration charger's current goes
  653. * through main charger's BATFET, keep the main charger's FCC
  654. * to the votable result.
  655. */
  656. if (chip->pl_batfet_mode == QTI_POWER_SUPPLY_PL_STACKED_BATFET) {
  657. *master_ua = max(0, total_ua);
  658. if (chip->main_fcc_max)
  659. *master_ua = min(*master_ua,
  660. chip->main_fcc_max + *slave_ua);
  661. } else {
  662. *master_ua = max(0, total_ua - *slave_ua);
  663. if (chip->main_fcc_max)
  664. *master_ua = min(*master_ua, chip->main_fcc_max);
  665. }
  666. }
  667. static void get_main_fcc_config(struct pl_data *chip, int *total_fcc)
  668. {
  669. int rc = 0, val;
  670. if (!is_cp_available(chip))
  671. goto out;
  672. rc = battery_read_iio_prop(chip, CP, BAT_CP_SWITCHER_EN, &val);
  673. if (rc < 0) {
  674. pr_err("Couldn't get switcher enable status, rc=%d\n", rc);
  675. goto out;
  676. }
  677. if (!val) {
  678. /*
  679. * To honor main charger upper FCC limit, on CP switcher
  680. * disable, skip fcc slewing as it will cause delay in limiting
  681. * the charge current flowing through main charger.
  682. */
  683. if (!chip->cp_disabled) {
  684. chip->fcc_stepper_enable = false;
  685. pl_dbg(chip, PR_PARALLEL,
  686. "Disabling FCC slewing on CP Switcher disable\n");
  687. }
  688. chip->cp_disabled = true;
  689. } else {
  690. chip->cp_disabled = false;
  691. pl_dbg(chip, PR_PARALLEL,
  692. "CP Switcher is enabled, don't limit main fcc\n");
  693. return;
  694. }
  695. out:
  696. *total_fcc = min(*total_fcc, chip->main_fcc_max);
  697. }
  698. static void get_fcc_stepper_params(struct pl_data *chip, int main_fcc_ua,
  699. int parallel_fcc_ua)
  700. {
  701. int main_set_fcc_ua, total_fcc_ua, target_icl;
  702. bool override;
  703. if (!chip->chg_param->fcc_step_size_ua) {
  704. pr_err("Invalid fcc stepper step size, value 0\n");
  705. return;
  706. }
  707. total_fcc_ua = main_fcc_ua + parallel_fcc_ua;
  708. override = is_override_vote_enabled_locked(chip->fcc_main_votable);
  709. if (override) {
  710. /*
  711. * FCC stepper params need re-calculation in override mode
  712. * only if there is change in Main or total FCC
  713. */
  714. main_set_fcc_ua = get_effective_result_locked(
  715. chip->fcc_main_votable);
  716. if ((main_set_fcc_ua != chip->override_main_fcc_ua)
  717. || (total_fcc_ua != chip->total_fcc_ua)) {
  718. chip->override_main_fcc_ua = main_set_fcc_ua;
  719. chip->total_fcc_ua = total_fcc_ua;
  720. } else {
  721. goto skip_fcc_step_update;
  722. }
  723. }
  724. /*
  725. * If override vote is removed then start main FCC from the
  726. * last overridden value.
  727. * Clear slave_fcc if requested parallel current is 0 i.e.
  728. * parallel is disabled.
  729. */
  730. if (chip->override_main_fcc_ua && !override) {
  731. chip->main_fcc_ua = chip->override_main_fcc_ua;
  732. chip->override_main_fcc_ua = 0;
  733. if (!parallel_fcc_ua)
  734. chip->slave_fcc_ua = 0;
  735. } else {
  736. chip->main_fcc_ua = get_effective_result_locked(
  737. chip->fcc_main_votable);
  738. }
  739. /* Skip stepping if override vote is applied on main */
  740. if (override) {
  741. chip->main_step_fcc_count = 0;
  742. chip->main_step_fcc_residual = 0;
  743. } else {
  744. chip->main_step_fcc_dir =
  745. (main_fcc_ua > chip->main_fcc_ua) ?
  746. STEP_UP : STEP_DOWN;
  747. chip->main_step_fcc_count =
  748. abs(main_fcc_ua - chip->main_fcc_ua) /
  749. chip->chg_param->fcc_step_size_ua;
  750. chip->main_step_fcc_residual =
  751. abs(main_fcc_ua - chip->main_fcc_ua) %
  752. chip->chg_param->fcc_step_size_ua;
  753. }
  754. /* Calculate CP_ILIM based on adapter limit and max. FCC */
  755. if (!parallel_fcc_ua && is_cp_available(chip) && override) {
  756. if (!chip->cp_ilim_votable)
  757. chip->cp_ilim_votable = find_votable("CP_ILIM");
  758. target_icl = get_adapter_icl_based_ilim(chip) * 2;
  759. total_fcc_ua -= chip->main_fcc_ua;
  760. /*
  761. * CP_ILIM = parallel_fcc_ua / 2.
  762. * Calculate parallel_fcc_ua as follows:
  763. * parallel_fcc_ua is based minimum of total FCC
  764. * or adapter's maximum allowed ICL limitation(if adapter
  765. * has max. ICL limitations).
  766. */
  767. parallel_fcc_ua = (target_icl > 0) ?
  768. min(target_icl, total_fcc_ua) : total_fcc_ua;
  769. }
  770. /* Skip stepping if override vote is applied on CP */
  771. if (chip->cp_ilim_votable
  772. && is_override_vote_enabled(chip->cp_ilim_votable)) {
  773. chip->parallel_step_fcc_count = 0;
  774. chip->parallel_step_fcc_residual = 0;
  775. } else {
  776. chip->parallel_step_fcc_dir =
  777. (parallel_fcc_ua > chip->slave_fcc_ua) ?
  778. STEP_UP : STEP_DOWN;
  779. chip->parallel_step_fcc_count =
  780. abs(parallel_fcc_ua - chip->slave_fcc_ua) /
  781. chip->chg_param->fcc_step_size_ua;
  782. chip->parallel_step_fcc_residual =
  783. abs(parallel_fcc_ua - chip->slave_fcc_ua) %
  784. chip->chg_param->fcc_step_size_ua;
  785. }
  786. skip_fcc_step_update:
  787. if (chip->parallel_step_fcc_count || chip->parallel_step_fcc_residual
  788. || chip->main_step_fcc_count || chip->main_step_fcc_residual)
  789. chip->step_fcc = 1;
  790. pl_dbg(chip, PR_PARALLEL,
  791. "Main FCC Stepper parameters: target_main_fcc: %d, current_main_fcc: %d main_step_direction: %d, main_step_count: %d, main_residual_fcc: %d override_main_fcc_ua: %d override: %d\n",
  792. main_fcc_ua, chip->main_fcc_ua, chip->main_step_fcc_dir,
  793. chip->main_step_fcc_count, chip->main_step_fcc_residual,
  794. chip->override_main_fcc_ua, override);
  795. pl_dbg(chip, PR_PARALLEL,
  796. "Parallel FCC Stepper parameters: target_pl_fcc: %d current_pl_fcc: %d parallel_step_direction: %d, parallel_step_count: %d, parallel_residual_fcc: %d\n",
  797. parallel_fcc_ua, chip->slave_fcc_ua,
  798. chip->parallel_step_fcc_dir, chip->parallel_step_fcc_count,
  799. chip->parallel_step_fcc_residual);
  800. pl_dbg(chip, PR_PARALLEL, "FCC Stepper parameters: step_fcc=%d\n",
  801. chip->step_fcc);
  802. }
  803. #define MINIMUM_PARALLEL_FCC_UA 500000
  804. #define PL_TAPER_WORK_DELAY_MS 500
  805. #define TAPER_RESIDUAL_PCT 90
  806. #define TAPER_REDUCTION_UA 200000
  807. static void pl_taper_work(struct work_struct *work)
  808. {
  809. struct pl_data *chip = container_of(work, struct pl_data,
  810. pl_taper_work);
  811. union power_supply_propval pval = {0, };
  812. int rc;
  813. int fcc_ua, total_fcc_ua, master_fcc_ua, slave_fcc_ua = 0;
  814. chip->taper_entry_fv = get_effective_result(chip->fv_votable);
  815. chip->taper_work_running = true;
  816. fcc_ua = get_client_vote(chip->fcc_votable, BATT_PROFILE_VOTER);
  817. vote(chip->fcc_votable, TAPER_STEPPER_VOTER, true, fcc_ua);
  818. while (true) {
  819. if (get_effective_result(chip->pl_disable_votable)) {
  820. /*
  821. * if parallel's FCC share is low, simply disable
  822. * parallel with TAPER_END_VOTER
  823. */
  824. total_fcc_ua = get_effective_result_locked(
  825. chip->fcc_votable);
  826. get_fcc_split(chip, total_fcc_ua, &master_fcc_ua,
  827. &slave_fcc_ua);
  828. if (slave_fcc_ua <= MINIMUM_PARALLEL_FCC_UA) {
  829. pl_dbg(chip, PR_PARALLEL, "terminating: parallel's share is low\n");
  830. vote(chip->pl_disable_votable, TAPER_END_VOTER,
  831. true, 0);
  832. } else {
  833. pl_dbg(chip, PR_PARALLEL, "terminating: parallel disabled\n");
  834. }
  835. goto done;
  836. }
  837. /*
  838. * Due to reduction of float voltage in JEITA condition taper
  839. * charging can be initiated at a lower FV. On removal of JEITA
  840. * condition, FV readjusts itself. However, once taper charging
  841. * is initiated, it doesn't exits until parallel chaging is
  842. * disabled due to which FCC doesn't scale back to its original
  843. * value, leading to slow charging thereafter.
  844. * Check if FV increases in comparison to FV at which taper
  845. * charging was initiated, and if yes, exit taper charging.
  846. */
  847. if (get_effective_result(chip->fv_votable) >
  848. chip->taper_entry_fv) {
  849. pl_dbg(chip, PR_PARALLEL, "Float voltage increased. Exiting taper\n");
  850. goto done;
  851. } else {
  852. chip->taper_entry_fv =
  853. get_effective_result(chip->fv_votable);
  854. }
  855. rc = power_supply_get_property(chip->batt_psy,
  856. POWER_SUPPLY_PROP_CHARGE_TYPE, &pval);
  857. if (rc < 0) {
  858. pr_err("Couldn't get batt charge type rc=%d\n", rc);
  859. goto done;
  860. }
  861. chip->charge_type = pval.intval;
  862. if (pval.intval == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE) {
  863. fcc_ua = get_client_vote(chip->fcc_votable,
  864. TAPER_STEPPER_VOTER);
  865. if (fcc_ua < 0) {
  866. pr_err("Couldn't get fcc, exiting taper work\n");
  867. goto done;
  868. }
  869. fcc_ua -= TAPER_REDUCTION_UA;
  870. if (fcc_ua < 0) {
  871. pr_err("Can't reduce FCC any more\n");
  872. goto done;
  873. }
  874. pl_dbg(chip, PR_PARALLEL, "master is taper charging; reducing FCC to %dua\n",
  875. fcc_ua);
  876. vote(chip->fcc_votable, TAPER_STEPPER_VOTER,
  877. true, fcc_ua);
  878. } else {
  879. pl_dbg(chip, PR_PARALLEL, "master is fast charging; waiting for next taper\n");
  880. }
  881. /* wait for the charger state to deglitch after FCC change */
  882. msleep(PL_TAPER_WORK_DELAY_MS);
  883. }
  884. done:
  885. chip->taper_work_running = false;
  886. vote(chip->fcc_votable, TAPER_STEPPER_VOTER, false, 0);
  887. vote(chip->pl_awake_votable, TAPER_END_VOTER, false, 0);
  888. }
  889. static int pl_fcc_main_vote_callback(struct votable *votable, void *data,
  890. int fcc_main_ua, const char *client)
  891. {
  892. struct pl_data *chip = data;
  893. int rc;
  894. rc = chip->chg_param->iio_write(chip->dev,
  895. PSY_IIO_CONSTANT_CHARGE_CURRENT_MAX, fcc_main_ua);
  896. if (rc < 0)
  897. pr_err("Couldn't set constant_charge_current_max, rc=%d\n", rc);
  898. return rc;
  899. }
  900. static int pl_fcc_vote_callback(struct votable *votable, void *data,
  901. int total_fcc_ua, const char *client)
  902. {
  903. struct pl_data *chip = data;
  904. int master_fcc_ua = total_fcc_ua, slave_fcc_ua = 0;
  905. int cp_fcc_ua = 0, rc, val = 0;
  906. if (total_fcc_ua < 0)
  907. return 0;
  908. if (!chip->cp_disable_votable)
  909. chip->cp_disable_votable = find_votable("CP_DISABLE");
  910. if (IS_ERR_OR_NULL(chip->iio_chan_list_cp))
  911. is_cp_available(chip);
  912. /*
  913. * Search for a slave charger channel specifically,
  914. * which would be available only if slave got probed.
  915. */
  916. if (!chip->iio_chan_list_cp_slave) {
  917. chip->iio_chan_list_cp_slave = devm_iio_channel_get(chip->dev,
  918. "current_capability");
  919. if (PTR_ERR(chip->iio_chan_list_cp_slave) == -EPROBE_DEFER)
  920. chip->iio_chan_list_cp_slave = NULL;
  921. }
  922. if (!chip->cp_slave_disable_votable)
  923. chip->cp_slave_disable_votable =
  924. find_votable("CP_SLAVE_DISABLE");
  925. /*
  926. * CP charger current = Total FCC - Main charger's FCC.
  927. * Main charger FCC is userspace's override vote on main.
  928. */
  929. cp_fcc_ua = total_fcc_ua - chip->chg_param->forced_main_fcc;
  930. pl_dbg(chip, PR_PARALLEL,
  931. "cp_fcc_ua=%d total_fcc_ua=%d forced_main_fcc=%d\n",
  932. cp_fcc_ua, total_fcc_ua, chip->chg_param->forced_main_fcc);
  933. if (cp_fcc_ua > 0) {
  934. if (!IS_ERR_OR_NULL(chip->iio_chan_list_cp)) {
  935. rc = battery_read_iio_prop(chip, CP, BAT_CP_MIN_ICL,
  936. &val);
  937. if (rc < 0)
  938. pr_err("Couldn't get MIN ICL threshold rc=%d\n",
  939. rc);
  940. }
  941. if (!IS_ERR_OR_NULL(chip->iio_chan_list_cp_slave) &&
  942. chip->cp_slave_disable_votable) {
  943. /*
  944. * Disable Slave CP if FCC share
  945. * falls below 3 * min ICL threshold.
  946. */
  947. vote(chip->cp_slave_disable_votable, FCC_VOTER,
  948. (cp_fcc_ua < (3 * val)), 0);
  949. }
  950. if (chip->cp_disable_votable) {
  951. /*
  952. * Disable Master CP if FCC share
  953. * falls below 2 * min ICL threshold.
  954. */
  955. vote(chip->cp_disable_votable, FCC_VOTER,
  956. (cp_fcc_ua < (2 * val)), 0);
  957. }
  958. }
  959. if (chip->pl_mode != QTI_POWER_SUPPLY_PL_NONE) {
  960. get_fcc_split(chip, total_fcc_ua, &master_fcc_ua,
  961. &slave_fcc_ua);
  962. if (slave_fcc_ua > MINIMUM_PARALLEL_FCC_UA) {
  963. vote(chip->pl_disable_votable, PL_FCC_LOW_VOTER,
  964. false, 0);
  965. } else {
  966. vote(chip->pl_disable_votable, PL_FCC_LOW_VOTER,
  967. true, 0);
  968. }
  969. }
  970. rerun_election(chip->pl_disable_votable);
  971. /* When FCC changes, trigger psy changed event for CC mode */
  972. if (!IS_ERR_OR_NULL(chip->iio_chan_list_cp))
  973. power_supply_changed(chip->cp_master_psy);
  974. return 0;
  975. }
  976. static void fcc_stepper_work(struct work_struct *work)
  977. {
  978. struct pl_data *chip = container_of(work, struct pl_data,
  979. fcc_stepper_work.work);
  980. union power_supply_propval pval = {0, };
  981. int reschedule_ms = 0, rc = 0, charger_present = 0;
  982. int main_fcc = chip->main_fcc_ua;
  983. int parallel_fcc = chip->slave_fcc_ua;
  984. /* Check whether USB is present or not */
  985. rc = power_supply_get_property(chip->usb_psy,
  986. POWER_SUPPLY_PROP_PRESENT, &pval);
  987. if (rc < 0)
  988. pr_err("Couldn't get USB Present status, rc=%d\n", rc);
  989. charger_present = pval.intval;
  990. /*Check whether DC charger is present or not */
  991. if (!chip->dc_psy)
  992. chip->dc_psy = power_supply_get_by_name("dc");
  993. if (chip->dc_psy) {
  994. rc = power_supply_get_property(chip->dc_psy,
  995. POWER_SUPPLY_PROP_PRESENT, &pval);
  996. if (rc < 0)
  997. pr_err("Couldn't get DC Present status, rc=%d\n", rc);
  998. charger_present |= pval.intval;
  999. }
  1000. /*
  1001. * If USB is not present, then set parallel FCC to min value and
  1002. * main FCC to the effective value of FCC votable and exit.
  1003. */
  1004. if (!charger_present) {
  1005. /* Disable parallel */
  1006. parallel_fcc = 0;
  1007. if (!IS_ERR_OR_NULL(chip->iio_chan_list_smb_parallel)) {
  1008. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1009. BAT_SMB_PARALLEL_INPUT_SUSPEND, 1);
  1010. if (rc < 0) {
  1011. pr_err("Couldn't change slave suspend state rc=%d\n",
  1012. rc);
  1013. goto out;
  1014. }
  1015. chip->pl_disable = true;
  1016. power_supply_changed(chip->pl_psy);
  1017. }
  1018. main_fcc = get_effective_result_locked(chip->fcc_votable);
  1019. vote(chip->fcc_main_votable, FCC_STEPPER_VOTER, true, main_fcc);
  1020. goto stepper_exit;
  1021. }
  1022. if (chip->main_step_fcc_count) {
  1023. main_fcc += (chip->chg_param->fcc_step_size_ua
  1024. * chip->main_step_fcc_dir);
  1025. chip->main_step_fcc_count--;
  1026. reschedule_ms = chip->chg_param->fcc_step_delay_ms;
  1027. } else if (chip->main_step_fcc_residual) {
  1028. main_fcc += chip->main_step_fcc_residual
  1029. * chip->main_step_fcc_dir;
  1030. chip->main_step_fcc_residual = 0;
  1031. }
  1032. if (chip->parallel_step_fcc_count) {
  1033. parallel_fcc += (chip->chg_param->fcc_step_size_ua
  1034. * chip->parallel_step_fcc_dir);
  1035. chip->parallel_step_fcc_count--;
  1036. reschedule_ms = chip->chg_param->fcc_step_delay_ms;
  1037. } else if (chip->parallel_step_fcc_residual) {
  1038. parallel_fcc += chip->parallel_step_fcc_residual;
  1039. chip->parallel_step_fcc_residual = 0;
  1040. }
  1041. if (parallel_fcc < chip->slave_fcc_ua) {
  1042. /* Set parallel FCC */
  1043. if (!IS_ERR_OR_NULL(chip->iio_chan_list_smb_parallel) &&
  1044. !chip->pl_disable) {
  1045. if (parallel_fcc < MINIMUM_PARALLEL_FCC_UA) {
  1046. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1047. BAT_SMB_PARALLEL_INPUT_SUSPEND, 1);
  1048. if (rc < 0) {
  1049. pr_err("Couldn't change slave suspend state rc=%d\n",
  1050. rc);
  1051. goto out;
  1052. }
  1053. if (IS_USBIN(chip->pl_mode))
  1054. split_settled(chip);
  1055. parallel_fcc = 0;
  1056. chip->parallel_step_fcc_count = 0;
  1057. chip->parallel_step_fcc_residual = 0;
  1058. chip->total_settled_ua = 0;
  1059. chip->pl_settled_ua = 0;
  1060. chip->pl_disable = true;
  1061. power_supply_changed(chip->pl_psy);
  1062. } else {
  1063. /* Set Parallel FCC */
  1064. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1065. BAT_SMB_PARALLEL_CONSTANT_CHARGE_CURRENT_MAX,
  1066. parallel_fcc);
  1067. if (rc < 0) {
  1068. pr_err("Couldn't set parallel charger fcc, rc=%d\n",
  1069. rc);
  1070. goto out;
  1071. }
  1072. }
  1073. }
  1074. /* Set main FCC */
  1075. vote(chip->fcc_main_votable, FCC_STEPPER_VOTER, true, main_fcc);
  1076. } else {
  1077. /* Set main FCC */
  1078. vote(chip->fcc_main_votable, FCC_STEPPER_VOTER, true, main_fcc);
  1079. /* Set parallel FCC */
  1080. if (!IS_ERR_OR_NULL(chip->iio_chan_list_smb_parallel)) {
  1081. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1082. BAT_SMB_PARALLEL_CONSTANT_CHARGE_CURRENT_MAX,
  1083. parallel_fcc);
  1084. if (rc < 0) {
  1085. pr_err("Couldn't set parallel charger fcc, rc=%d\n",
  1086. rc);
  1087. goto out;
  1088. }
  1089. /*
  1090. * Enable parallel charger only if it was disabled
  1091. * earlier and configured slave fcc is greater than or
  1092. * equal to minimum parallel FCC value.
  1093. */
  1094. if (chip->pl_disable && parallel_fcc
  1095. >= MINIMUM_PARALLEL_FCC_UA) {
  1096. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1097. BAT_SMB_PARALLEL_INPUT_SUSPEND, 0);
  1098. if (rc < 0) {
  1099. pr_err("Couldn't change slave suspend state rc=%d\n",
  1100. rc);
  1101. goto out;
  1102. }
  1103. if (IS_USBIN(chip->pl_mode))
  1104. split_settled(chip);
  1105. chip->pl_disable = false;
  1106. power_supply_changed(chip->pl_psy);
  1107. }
  1108. }
  1109. }
  1110. stepper_exit:
  1111. chip->main_fcc_ua = main_fcc;
  1112. chip->slave_fcc_ua = parallel_fcc;
  1113. cp_configure_ilim(chip, FCC_VOTER, chip->slave_fcc_ua / 2);
  1114. if (reschedule_ms) {
  1115. schedule_delayed_work(&chip->fcc_stepper_work,
  1116. msecs_to_jiffies(reschedule_ms));
  1117. pr_debug("Rescheduling FCC_STEPPER work\n");
  1118. return;
  1119. }
  1120. out:
  1121. chip->step_fcc = 0;
  1122. vote(chip->pl_awake_votable, FCC_STEPPER_VOTER, false, 0);
  1123. }
  1124. static bool is_batt_available(struct pl_data *chip)
  1125. {
  1126. if (!chip->batt_psy)
  1127. chip->batt_psy = power_supply_get_by_name("battery");
  1128. if (!chip->batt_psy)
  1129. return false;
  1130. return true;
  1131. }
  1132. #define PARALLEL_FLOAT_VOLTAGE_DELTA_UV 50000
  1133. static int pl_fv_vote_callback(struct votable *votable, void *data,
  1134. int fv_uv, const char *client)
  1135. {
  1136. struct pl_data *chip = data;
  1137. union power_supply_propval pval = {0, };
  1138. int rc = 0, val;
  1139. if (fv_uv < 0)
  1140. return 0;
  1141. val = fv_uv;
  1142. rc = chip->chg_param->iio_write(chip->dev, PSY_IIO_VOLTAGE_MAX, val);
  1143. if (rc < 0) {
  1144. pr_err("Couldn't set main fv, rc=%d\n", rc);
  1145. return rc;
  1146. }
  1147. if (chip->pl_mode != QTI_POWER_SUPPLY_PL_NONE) {
  1148. val += PARALLEL_FLOAT_VOLTAGE_DELTA_UV;
  1149. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1150. BAT_SMB_PARALLEL_VOLTAGE_MAX, val);
  1151. if (rc < 0) {
  1152. pr_err("Couldn't set float on parallel rc=%d\n", rc);
  1153. return rc;
  1154. }
  1155. }
  1156. /*
  1157. * check for termination at reduced float voltage and re-trigger
  1158. * charging if new float voltage is above last FV.
  1159. */
  1160. if ((chip->float_voltage_uv < fv_uv) && is_batt_available(chip)) {
  1161. rc = power_supply_get_property(chip->batt_psy,
  1162. POWER_SUPPLY_PROP_STATUS, &pval);
  1163. if (rc < 0) {
  1164. pr_err("Couldn't get battery status rc=%d\n", rc);
  1165. } else {
  1166. if (pval.intval == POWER_SUPPLY_STATUS_FULL) {
  1167. pr_debug("re-triggering charging\n");
  1168. val = 1;
  1169. rc = chip->chg_param->iio_write(chip->dev,
  1170. PSY_IIO_FORCE_RECHARGE, val);
  1171. if (rc < 0)
  1172. pr_err("Couldn't set force recharge rc=%d\n",
  1173. rc);
  1174. }
  1175. }
  1176. }
  1177. chip->float_voltage_uv = fv_uv;
  1178. return 0;
  1179. }
  1180. #define ICL_STEP_UA 25000
  1181. #define PL_DELAY_MS 3000
  1182. static int usb_icl_vote_callback(struct votable *votable, void *data,
  1183. int icl_ua, const char *client)
  1184. {
  1185. int rc, val;
  1186. struct pl_data *chip = data;
  1187. union power_supply_propval pval = {0, };
  1188. bool rerun_aicl = false, dc_present = false;
  1189. if (client == NULL)
  1190. icl_ua = INT_MAX;
  1191. /*
  1192. * Disable parallel for new ICL vote - the call to split_settled will
  1193. * ensure that all the input current limit gets assigned to the main
  1194. * charger.
  1195. */
  1196. vote(chip->pl_disable_votable, ICL_CHANGE_VOTER, true, 0);
  1197. /*
  1198. * if (ICL < 1400)
  1199. * disable parallel charger using USBIN_I_VOTER
  1200. * else
  1201. * instead of re-enabling here rely on status_changed_work
  1202. * (triggered via AICL completed or scheduled from here to
  1203. * unvote USBIN_I_VOTER) the status_changed_work enables
  1204. * USBIN_I_VOTER based on settled current.
  1205. */
  1206. if (icl_ua <= 1400000)
  1207. vote(chip->pl_enable_votable_indirect, USBIN_I_VOTER, false, 0);
  1208. else
  1209. schedule_delayed_work(&chip->status_change_work,
  1210. msecs_to_jiffies(PL_DELAY_MS));
  1211. /* rerun AICL */
  1212. /* get the settled current */
  1213. rc = chip->chg_param->iio_read(chip->dev,
  1214. PSY_IIO_MAIN_INPUT_CURRENT_SETTLED, &val);
  1215. if (rc < 0) {
  1216. pr_err("Couldn't get aicl settled value rc=%d\n", rc);
  1217. return rc;
  1218. }
  1219. /* rerun AICL if new ICL is above settled ICL */
  1220. if (icl_ua > val)
  1221. rerun_aicl = true;
  1222. if (rerun_aicl && (chip->wa_flags & AICL_RERUN_WA_BIT)) {
  1223. /* set a lower ICL */
  1224. val = max(val - ICL_STEP_UA, ICL_STEP_UA);
  1225. rc = chip->chg_param->iio_write(chip->dev, PSY_IIO_CURRENT_MAX,
  1226. val);
  1227. if (rc < 0)
  1228. pr_err("Couldn't set main current_max, rc=%d\n", rc);
  1229. }
  1230. /* set the effective ICL */
  1231. val = icl_ua;
  1232. rc = chip->chg_param->iio_write(chip->dev, PSY_IIO_CURRENT_MAX, val);
  1233. if (rc < 0)
  1234. pr_err("Couldn't set main current_max, rc=%d\n", rc);
  1235. vote(chip->pl_disable_votable, ICL_CHANGE_VOTER, false, 0);
  1236. /* Configure ILIM based on AICL result only if input mode is USBMID */
  1237. if (cp_get_parallel_mode(chip, PARALLEL_INPUT_MODE)
  1238. == QTI_POWER_SUPPLY_PL_USBMID_USBMID) {
  1239. if (chip->dc_psy) {
  1240. rc = power_supply_get_property(chip->dc_psy,
  1241. POWER_SUPPLY_PROP_PRESENT, &pval);
  1242. if (rc < 0) {
  1243. pr_err("Couldn't get DC PRESENT rc=%d\n", rc);
  1244. return rc;
  1245. }
  1246. dc_present = pval.intval;
  1247. }
  1248. /* Don't configure ILIM if DC is present */
  1249. if (!dc_present)
  1250. cp_configure_ilim(chip, ICL_CHANGE_VOTER, icl_ua);
  1251. }
  1252. return 0;
  1253. }
  1254. static void pl_disable_forever_work(struct work_struct *work)
  1255. {
  1256. struct pl_data *chip = container_of(work,
  1257. struct pl_data, pl_disable_forever_work);
  1258. /* Disable Parallel charger forever */
  1259. vote(chip->pl_disable_votable, PL_HW_ABSENT_VOTER, true, 0);
  1260. /* Re-enable autonomous mode */
  1261. if (chip->hvdcp_hw_inov_dis_votable)
  1262. vote(chip->hvdcp_hw_inov_dis_votable, PL_VOTER, false, 0);
  1263. }
  1264. static int pl_disable_vote_callback(struct votable *votable,
  1265. void *data, int pl_disable, const char *client)
  1266. {
  1267. struct pl_data *chip = data;
  1268. union power_supply_propval pval = {0, };
  1269. int master_fcc_ua = 0, total_fcc_ua = 0, slave_fcc_ua = 0;
  1270. int rc = 0, cp_ilim;
  1271. bool disable = false;
  1272. if (!is_batt_available(chip))
  1273. return -ENODEV;
  1274. if (!chip->usb_psy)
  1275. chip->usb_psy = power_supply_get_by_name("usb");
  1276. if (!chip->usb_psy) {
  1277. pr_err("Couldn't get usb psy\n");
  1278. return -ENODEV;
  1279. }
  1280. rc = chip->chg_param->iio_read(chip->dev, PSY_IIO_FCC_STEPPER_ENABLE,
  1281. &pval.intval);
  1282. if (rc < 0) {
  1283. pr_err("Couldn't read FCC step update status, rc=%d\n", rc);
  1284. return rc;
  1285. }
  1286. chip->fcc_stepper_enable = pval.intval;
  1287. pr_debug("FCC Stepper %s\n", pval.intval ? "enabled" : "disabled");
  1288. rc = chip->chg_param->iio_read(chip->dev, PSY_IIO_MAIN_FCC_MAX,
  1289. &pval.intval);
  1290. if (rc < 0) {
  1291. pl_dbg(chip, PR_PARALLEL,
  1292. "Couldn't read primary charger FCC upper limit, rc=%d\n",
  1293. rc);
  1294. } else if (pval.intval > 0) {
  1295. chip->main_fcc_max = pval.intval;
  1296. }
  1297. if (chip->fcc_stepper_enable) {
  1298. cancel_delayed_work_sync(&chip->fcc_stepper_work);
  1299. vote(chip->pl_awake_votable, FCC_STEPPER_VOTER, false, 0);
  1300. }
  1301. total_fcc_ua = get_effective_result_locked(chip->fcc_votable);
  1302. if (chip->pl_mode != QTI_POWER_SUPPLY_PL_NONE && !pl_disable) {
  1303. rc = validate_parallel_icl(chip, &disable);
  1304. if (rc < 0)
  1305. return rc;
  1306. if (disable) {
  1307. pr_info("Parallel ICL is less than min ICL(%d), skipping parallel enable\n",
  1308. chip->pl_min_icl_ua);
  1309. return 0;
  1310. }
  1311. /* enable parallel charging */
  1312. rc = battery_read_iio_prop(chip, SMB_PARALLEL,
  1313. BAT_SMB_PARALLEL_CHARGE_TYPE, &pval.intval);
  1314. if (rc == -ENODEV) {
  1315. /*
  1316. * -ENODEV is returned only if parallel chip
  1317. * is not present in the system.
  1318. * Disable parallel charger forever.
  1319. */
  1320. schedule_work(&chip->pl_disable_forever_work);
  1321. return rc;
  1322. }
  1323. rerun_election(chip->fv_votable);
  1324. get_fcc_split(chip, total_fcc_ua, &master_fcc_ua,
  1325. &slave_fcc_ua);
  1326. if (chip->fcc_stepper_enable) {
  1327. get_fcc_stepper_params(chip, master_fcc_ua,
  1328. slave_fcc_ua);
  1329. if (chip->step_fcc) {
  1330. vote(chip->pl_awake_votable, FCC_STEPPER_VOTER,
  1331. true, 0);
  1332. schedule_delayed_work(&chip->fcc_stepper_work,
  1333. 0);
  1334. }
  1335. } else {
  1336. /*
  1337. * If there is an increase in slave share
  1338. * (Also handles parallel enable case)
  1339. * Set Main ICL then slave FCC
  1340. * else
  1341. * (Also handles parallel disable case)
  1342. * Set slave ICL then main FCC.
  1343. */
  1344. if (slave_fcc_ua > chip->slave_fcc_ua) {
  1345. vote(chip->fcc_main_votable, MAIN_FCC_VOTER,
  1346. true, master_fcc_ua);
  1347. pval.intval = slave_fcc_ua;
  1348. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1349. BAT_SMB_PARALLEL_CONSTANT_CHARGE_CURRENT_MAX,
  1350. pval.intval);
  1351. if (rc < 0) {
  1352. pr_err("Couldn't set parallel fcc, rc=%d\n",
  1353. rc);
  1354. return rc;
  1355. }
  1356. chip->slave_fcc_ua = slave_fcc_ua;
  1357. } else {
  1358. pval.intval = slave_fcc_ua;
  1359. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1360. BAT_SMB_PARALLEL_CONSTANT_CHARGE_CURRENT_MAX,
  1361. pval.intval);
  1362. if (rc < 0) {
  1363. pr_err("Couldn't set parallel fcc, rc=%d\n",
  1364. rc);
  1365. return rc;
  1366. }
  1367. chip->slave_fcc_ua = slave_fcc_ua;
  1368. vote(chip->fcc_main_votable, MAIN_FCC_VOTER,
  1369. true, master_fcc_ua);
  1370. }
  1371. /*
  1372. * Enable will be called with a valid pl_psy always. The
  1373. * PARALLEL_PSY_VOTER keeps it disabled unless a pl_psy
  1374. * is seen.
  1375. */
  1376. pval.intval = 0;
  1377. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1378. BAT_SMB_PARALLEL_INPUT_SUSPEND,
  1379. pval.intval);
  1380. if (rc < 0)
  1381. pr_err("Couldn't change slave suspend state rc=%d\n",
  1382. rc);
  1383. if (IS_USBIN(chip->pl_mode))
  1384. split_settled(chip);
  1385. }
  1386. /*
  1387. * we could have been enabled while in taper mode,
  1388. * start the taper work if so
  1389. */
  1390. rc = power_supply_get_property(chip->batt_psy,
  1391. POWER_SUPPLY_PROP_CHARGE_TYPE, &pval);
  1392. if (rc < 0) {
  1393. pr_err("Couldn't get batt charge type rc=%d\n", rc);
  1394. } else {
  1395. if (pval.intval == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE
  1396. && !chip->taper_work_running) {
  1397. pl_dbg(chip, PR_PARALLEL,
  1398. "pl enabled in Taper scheduing work\n");
  1399. vote(chip->pl_awake_votable, TAPER_END_VOTER,
  1400. true, 0);
  1401. queue_work(system_long_wq,
  1402. &chip->pl_taper_work);
  1403. }
  1404. }
  1405. pl_dbg(chip, PR_PARALLEL, "master_fcc=%d slave_fcc=%d distribution=(%d/%d)\n",
  1406. master_fcc_ua, slave_fcc_ua,
  1407. (master_fcc_ua * 100) / total_fcc_ua,
  1408. (slave_fcc_ua * 100) / total_fcc_ua);
  1409. } else {
  1410. if (chip->main_fcc_max)
  1411. get_main_fcc_config(chip, &total_fcc_ua);
  1412. if (!chip->fcc_stepper_enable) {
  1413. if (IS_USBIN(chip->pl_mode))
  1414. split_settled(chip);
  1415. /* pl_psy may be NULL while in the disable branch */
  1416. if (!IS_ERR_OR_NULL(chip->iio_chan_list_smb_parallel)) {
  1417. pval.intval = 1;
  1418. rc = battery_write_iio_prop(chip, SMB_PARALLEL,
  1419. BAT_SMB_PARALLEL_INPUT_SUSPEND,
  1420. pval.intval);
  1421. if (rc < 0)
  1422. pr_err("Couldn't change slave suspend state rc=%d\n",
  1423. rc);
  1424. }
  1425. /* main psy gets all share */
  1426. vote(chip->fcc_main_votable, MAIN_FCC_VOTER, true,
  1427. total_fcc_ua);
  1428. cp_ilim = total_fcc_ua - get_effective_result_locked(
  1429. chip->fcc_main_votable);
  1430. if (cp_ilim > 0)
  1431. cp_configure_ilim(chip, FCC_VOTER, cp_ilim / 2);
  1432. /* reset parallel FCC */
  1433. chip->slave_fcc_ua = 0;
  1434. chip->total_settled_ua = 0;
  1435. chip->pl_settled_ua = 0;
  1436. } else {
  1437. get_fcc_stepper_params(chip, total_fcc_ua, 0);
  1438. if (chip->step_fcc) {
  1439. vote(chip->pl_awake_votable, FCC_STEPPER_VOTER,
  1440. true, 0);
  1441. schedule_delayed_work(&chip->fcc_stepper_work,
  1442. 0);
  1443. }
  1444. }
  1445. rerun_election(chip->fv_votable);
  1446. }
  1447. /* notify parallel state change */
  1448. if (!IS_ERR_OR_NULL(chip->iio_chan_list_smb_parallel) &&
  1449. (chip->pl_disable != pl_disable)
  1450. && !chip->fcc_stepper_enable) {
  1451. power_supply_changed(chip->pl_psy);
  1452. chip->pl_disable = (bool)pl_disable;
  1453. }
  1454. pl_dbg(chip, PR_PARALLEL, "parallel charging %s\n",
  1455. pl_disable ? "disabled" : "enabled");
  1456. return 0;
  1457. }
  1458. static int pl_enable_indirect_vote_callback(struct votable *votable,
  1459. void *data, int pl_enable, const char *client)
  1460. {
  1461. struct pl_data *chip = data;
  1462. vote(chip->pl_disable_votable, PL_INDIRECT_VOTER, !pl_enable, 0);
  1463. return 0;
  1464. }
  1465. static int pl_awake_vote_callback(struct votable *votable,
  1466. void *data, int awake, const char *client)
  1467. {
  1468. struct pl_data *chip = data;
  1469. if (awake)
  1470. __pm_stay_awake(chip->pl_ws);
  1471. else
  1472. __pm_relax(chip->pl_ws);
  1473. pr_debug("client: %s awake: %d\n", client, awake);
  1474. return 0;
  1475. }
  1476. static bool is_parallel_available(struct pl_data *chip)
  1477. {
  1478. union power_supply_propval pval = {0, };
  1479. int rc = 0;
  1480. struct iio_channel **iio_list;
  1481. if (IS_ERR(chip->iio_chan_list_smb_parallel))
  1482. return false;
  1483. if (!chip->iio_chan_list_smb_parallel) {
  1484. iio_list = get_ext_channels(chip->dev,
  1485. bat_smb_parallel_ext_iio_chan,
  1486. ARRAY_SIZE(bat_smb_parallel_ext_iio_chan));
  1487. if (IS_ERR(iio_list)) {
  1488. rc = PTR_ERR(iio_list);
  1489. if (rc != -EPROBE_DEFER) {
  1490. dev_err(chip->dev, "Failed to get channels, %d\n",
  1491. rc);
  1492. chip->iio_chan_list_smb_parallel =
  1493. ERR_PTR(-EINVAL);
  1494. }
  1495. return false;
  1496. }
  1497. chip->iio_chan_list_smb_parallel = iio_list;
  1498. }
  1499. if (!chip->pl_psy) {
  1500. chip->pl_psy = power_supply_get_by_name("parallel");
  1501. if (!chip->pl_psy)
  1502. return false;
  1503. }
  1504. vote(chip->pl_disable_votable, PARALLEL_PSY_VOTER, false, 0);
  1505. rc = battery_read_iio_prop(chip, SMB_PARALLEL, BAT_SMB_PARALLEL_MODE,
  1506. &pval.intval);
  1507. if (rc < 0) {
  1508. pr_err("Couldn't get parallel mode from parallel rc=%d\n",
  1509. rc);
  1510. return false;
  1511. }
  1512. /*
  1513. * Note that pl_mode will be updated to anything other than a _NONE
  1514. * only after pl_psy is found. IOW pl_mode != _NONE implies that
  1515. * pl_psy is present and valid.
  1516. */
  1517. chip->pl_mode = pval.intval;
  1518. /* Disable autonomous votage increments for USBIN-USBIN */
  1519. if (IS_USBIN(chip->pl_mode)
  1520. && (chip->wa_flags & FORCE_INOV_DISABLE_BIT)) {
  1521. if (!chip->hvdcp_hw_inov_dis_votable)
  1522. chip->hvdcp_hw_inov_dis_votable =
  1523. find_votable("HVDCP_HW_INOV_DIS");
  1524. if (chip->hvdcp_hw_inov_dis_votable)
  1525. /* Read current pulse count */
  1526. vote(chip->hvdcp_hw_inov_dis_votable, PL_VOTER,
  1527. true, 0);
  1528. else
  1529. return false;
  1530. }
  1531. rc = battery_read_iio_prop(chip, SMB_PARALLEL,
  1532. BAT_SMB_PARALLEL_BATFET_MODE, &pval.intval);
  1533. if (rc < 0) {
  1534. pr_err("Couldn't get parallel batfet mode rc=%d\n",
  1535. rc);
  1536. return false;
  1537. }
  1538. chip->pl_batfet_mode = pval.intval;
  1539. pval.intval = 0;
  1540. rc = battery_read_iio_prop(chip, SMB_PARALLEL, BAT_SMB_PARALLEL_MIN_ICL,
  1541. &pval.intval);
  1542. if (rc < 0)
  1543. pr_err("Couldn't get min_icl rc=%d\n", rc);
  1544. else
  1545. chip->pl_min_icl_ua = pval.intval;
  1546. chip->pl_fcc_max = INT_MAX;
  1547. rc = battery_read_iio_prop(chip, SMB_PARALLEL, BAT_SMB_PARALLEL_FCC_MAX,
  1548. &pval.intval);
  1549. if (!rc)
  1550. chip->pl_fcc_max = pval.intval;
  1551. else
  1552. pr_err("Couldn't get fcc_max rc=%d\n", rc);
  1553. return true;
  1554. }
  1555. static void handle_main_charge_type(struct pl_data *chip)
  1556. {
  1557. union power_supply_propval pval = {0, };
  1558. int rc;
  1559. rc = power_supply_get_property(chip->batt_psy,
  1560. POWER_SUPPLY_PROP_CHARGE_TYPE, &pval);
  1561. if (rc < 0) {
  1562. pr_err("Couldn't get batt charge type rc=%d\n", rc);
  1563. return;
  1564. }
  1565. /* not fast/not taper state to disables parallel */
  1566. if ((pval.intval != POWER_SUPPLY_CHARGE_TYPE_FAST)
  1567. && (pval.intval != POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE)) {
  1568. vote(chip->pl_disable_votable, CHG_STATE_VOTER, true, 0);
  1569. chip->charge_type = pval.intval;
  1570. return;
  1571. }
  1572. /* handle taper charge entry */
  1573. if (chip->charge_type == POWER_SUPPLY_CHARGE_TYPE_FAST
  1574. && (pval.intval == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE)) {
  1575. chip->charge_type = pval.intval;
  1576. if (!chip->taper_work_running) {
  1577. pl_dbg(chip, PR_PARALLEL, "taper entry scheduling work\n");
  1578. vote(chip->pl_awake_votable, TAPER_END_VOTER, true, 0);
  1579. queue_work(system_long_wq, &chip->pl_taper_work);
  1580. }
  1581. return;
  1582. }
  1583. /* handle fast/taper charge entry */
  1584. if (pval.intval == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE
  1585. || pval.intval == POWER_SUPPLY_CHARGE_TYPE_FAST) {
  1586. /*
  1587. * Undo parallel charging termination if entered taper in
  1588. * reduced float voltage condition due to jeita mitigation.
  1589. */
  1590. if (pval.intval == POWER_SUPPLY_CHARGE_TYPE_FAST &&
  1591. (chip->taper_entry_fv <
  1592. get_effective_result(chip->fv_votable))) {
  1593. vote(chip->pl_disable_votable, TAPER_END_VOTER,
  1594. false, 0);
  1595. }
  1596. pl_dbg(chip, PR_PARALLEL, "chg_state enabling parallel\n");
  1597. vote(chip->pl_disable_votable, CHG_STATE_VOTER, false, 0);
  1598. chip->charge_type = pval.intval;
  1599. return;
  1600. }
  1601. /* remember the new state only if it isn't any of the above */
  1602. chip->charge_type = pval.intval;
  1603. }
  1604. #define MIN_ICL_CHANGE_DELTA_UA 300000
  1605. static void handle_settled_icl_change(struct pl_data *chip)
  1606. {
  1607. union power_supply_propval pval = {0, };
  1608. int new_total_settled_ua;
  1609. int rc, val;
  1610. int main_settled_ua;
  1611. int main_limited;
  1612. int total_current_ua;
  1613. bool disable = false;
  1614. total_current_ua = get_effective_result_locked(chip->usb_icl_votable);
  1615. /*
  1616. * call aicl split only when USBIN_USBIN and enabled
  1617. * and if aicl changed
  1618. */
  1619. rc = chip->chg_param->iio_read(chip->dev,
  1620. PSY_IIO_MAIN_INPUT_CURRENT_SETTLED, &val);
  1621. if (rc < 0) {
  1622. pr_err("Couldn't get aicl settled value rc=%d\n", rc);
  1623. return;
  1624. }
  1625. main_settled_ua = val;
  1626. rc = chip->chg_param->iio_read(chip->dev, PSY_IIO_INPUT_CURRENT_LIMITED,
  1627. &val);
  1628. if (rc < 0) {
  1629. pr_err("Couldn't get aicl settled value rc=%d\n", rc);
  1630. return;
  1631. }
  1632. main_limited = val;
  1633. if ((main_limited && (main_settled_ua + chip->pl_settled_ua) < 1400000)
  1634. || (main_settled_ua == 0)
  1635. || ((total_current_ua >= 0) &&
  1636. (total_current_ua <= 1400000)))
  1637. vote(chip->pl_enable_votable_indirect, USBIN_I_VOTER, false, 0);
  1638. else
  1639. vote(chip->pl_enable_votable_indirect, USBIN_I_VOTER, true, 0);
  1640. rerun_election(chip->fcc_votable);
  1641. if (IS_USBIN(chip->pl_mode)) {
  1642. /*
  1643. * call aicl split only when USBIN_USBIN and enabled
  1644. * and if settled current has changed by more than 300mA
  1645. */
  1646. new_total_settled_ua = main_settled_ua + chip->pl_settled_ua;
  1647. pl_dbg(chip, PR_PARALLEL,
  1648. "total_settled_ua=%d settled_ua=%d new_total_settled_ua=%d\n",
  1649. chip->total_settled_ua, pval.intval,
  1650. new_total_settled_ua);
  1651. /* If ICL change is small skip splitting */
  1652. if (abs(new_total_settled_ua - chip->total_settled_ua)
  1653. > MIN_ICL_CHANGE_DELTA_UA) {
  1654. rc = validate_parallel_icl(chip, &disable);
  1655. if (rc < 0)
  1656. return;
  1657. vote(chip->pl_disable_votable, ICL_LIMIT_VOTER,
  1658. disable, 0);
  1659. if (!get_effective_result_locked(
  1660. chip->pl_disable_votable))
  1661. split_settled(chip);
  1662. }
  1663. }
  1664. }
  1665. static void handle_parallel_in_taper(struct pl_data *chip)
  1666. {
  1667. union power_supply_propval pval = {0, };
  1668. int rc;
  1669. if (get_effective_result_locked(chip->pl_disable_votable))
  1670. return;
  1671. if (IS_ERR_OR_NULL(chip->iio_chan_list_smb_parallel))
  1672. return;
  1673. rc = battery_read_iio_prop(chip, SMB_PARALLEL,
  1674. BAT_SMB_PARALLEL_CHARGE_TYPE, &pval.intval);
  1675. if (rc < 0) {
  1676. pr_err("Couldn't get pl charge type rc=%d\n", rc);
  1677. return;
  1678. }
  1679. /*
  1680. * if parallel is seen in taper mode ever, that is an anomaly and
  1681. * we disable parallel charger
  1682. */
  1683. if (pval.intval == POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE) {
  1684. vote(chip->pl_disable_votable, PL_TAPER_EARLY_BAD_VOTER,
  1685. true, 0);
  1686. return;
  1687. }
  1688. }
  1689. static void handle_usb_change(struct pl_data *chip)
  1690. {
  1691. int rc, val;
  1692. union power_supply_propval pval = {0, };
  1693. if (!chip->usb_psy)
  1694. chip->usb_psy = power_supply_get_by_name("usb");
  1695. if (!chip->usb_psy) {
  1696. pr_err("Couldn't get usbpsy\n");
  1697. return;
  1698. }
  1699. rc = power_supply_get_property(chip->usb_psy,
  1700. POWER_SUPPLY_PROP_PRESENT, &pval);
  1701. if (rc < 0) {
  1702. pr_err("Couldn't get present from USB rc=%d\n", rc);
  1703. return;
  1704. }
  1705. if (!pval.intval) {
  1706. /* USB removed: remove all stale votes */
  1707. vote(chip->pl_disable_votable, TAPER_END_VOTER, false, 0);
  1708. vote(chip->pl_disable_votable, PL_TAPER_EARLY_BAD_VOTER,
  1709. false, 0);
  1710. vote(chip->pl_disable_votable, ICL_LIMIT_VOTER, false, 0);
  1711. chip->override_main_fcc_ua = 0;
  1712. chip->total_fcc_ua = 0;
  1713. chip->slave_fcc_ua = 0;
  1714. chip->main_fcc_ua = 0;
  1715. chip->charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
  1716. } else {
  1717. rc = chip->chg_param->iio_read(chip->dev, PSY_IIO_USB_REAL_TYPE,
  1718. &val);
  1719. if (rc < 0)
  1720. pr_err("Couldn't get USB real type rc=%d\n", rc);
  1721. else
  1722. chip->charger_type = val;
  1723. }
  1724. }
  1725. static void status_change_work(struct work_struct *work)
  1726. {
  1727. struct pl_data *chip = container_of(work,
  1728. struct pl_data, status_change_work.work);
  1729. /*
  1730. * re-run election for FCC/FV/ICL to ensure all
  1731. * votes are reflected on hardware
  1732. */
  1733. rerun_election(chip->usb_icl_votable);
  1734. rerun_election(chip->fcc_votable);
  1735. rerun_election(chip->fv_votable);
  1736. if (!is_batt_available(chip))
  1737. return;
  1738. is_parallel_available(chip);
  1739. handle_usb_change(chip);
  1740. handle_main_charge_type(chip);
  1741. handle_settled_icl_change(chip);
  1742. handle_parallel_in_taper(chip);
  1743. }
  1744. static int pl_notifier_call(struct notifier_block *nb,
  1745. unsigned long ev, void *v)
  1746. {
  1747. struct power_supply *psy = v;
  1748. struct pl_data *chip = container_of(nb, struct pl_data, nb);
  1749. if (ev != PSY_EVENT_PROP_CHANGED)
  1750. return NOTIFY_OK;
  1751. if ((strcmp(psy->desc->name, "parallel") == 0)
  1752. || (strcmp(psy->desc->name, "battery") == 0))
  1753. schedule_delayed_work(&chip->status_change_work, 0);
  1754. return NOTIFY_OK;
  1755. }
  1756. static int pl_register_notifier(struct pl_data *chip)
  1757. {
  1758. int rc;
  1759. chip->nb.notifier_call = pl_notifier_call;
  1760. rc = power_supply_reg_notifier(&chip->nb);
  1761. if (rc < 0) {
  1762. pr_err("Couldn't register psy notifier rc = %d\n", rc);
  1763. return rc;
  1764. }
  1765. return 0;
  1766. }
  1767. static int pl_determine_initial_status(struct pl_data *chip)
  1768. {
  1769. status_change_work(&chip->status_change_work.work);
  1770. return 0;
  1771. }
  1772. static void pl_config_init(struct pl_data *chip, int smb_version)
  1773. {
  1774. switch (smb_version) {
  1775. case PMI8998_SUBTYPE:
  1776. case PM660_SUBTYPE:
  1777. chip->wa_flags = AICL_RERUN_WA_BIT | FORCE_INOV_DISABLE_BIT;
  1778. break;
  1779. default:
  1780. break;
  1781. }
  1782. }
  1783. static void qcom_batt_create_debugfs(struct pl_data *chip)
  1784. {
  1785. chip->dfs_root = debugfs_create_dir("battery", NULL);
  1786. if (IS_ERR_OR_NULL(chip->dfs_root)) {
  1787. pr_err("Couldn't create battery debugfs rc=%ld\n",
  1788. (long)chip->dfs_root);
  1789. return;
  1790. }
  1791. debugfs_create_u32("debug_mask", 0600, chip->dfs_root,
  1792. &debug_mask);
  1793. }
  1794. #define DEFAULT_RESTRICTED_CURRENT_UA 1000000
  1795. int qcom_batt_init(struct device *dev, struct charger_param *chg_param)
  1796. {
  1797. struct pl_data *chip;
  1798. int rc = 0;
  1799. if (!chg_param) {
  1800. pr_err("invalid charger parameter\n");
  1801. return -EINVAL;
  1802. }
  1803. if (!chg_param->iio_read || !chg_param->iio_write) {
  1804. pr_err("Invalid iio read/write pointers\n");
  1805. return -EINVAL;
  1806. }
  1807. /* initialize just once */
  1808. if (the_chip) {
  1809. pr_err("was initialized earlier. Failing now\n");
  1810. return -EINVAL;
  1811. }
  1812. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1813. if (!chip)
  1814. return -ENOMEM;
  1815. chip->dev = dev;
  1816. qcom_batt_create_debugfs(chip);
  1817. chip->slave_pct = 50;
  1818. chip->chg_param = chg_param;
  1819. pl_config_init(chip, chg_param->smb_version);
  1820. chip->restricted_current = DEFAULT_RESTRICTED_CURRENT_UA;
  1821. chip->pl_ws = wakeup_source_register(NULL, "qcom-battery");
  1822. if (!chip->pl_ws)
  1823. goto cleanup;
  1824. INIT_DELAYED_WORK(&chip->status_change_work, status_change_work);
  1825. INIT_WORK(&chip->pl_taper_work, pl_taper_work);
  1826. INIT_WORK(&chip->pl_disable_forever_work, pl_disable_forever_work);
  1827. INIT_DELAYED_WORK(&chip->fcc_stepper_work, fcc_stepper_work);
  1828. chip->fcc_main_votable = create_votable("FCC_MAIN", VOTE_MIN,
  1829. pl_fcc_main_vote_callback,
  1830. chip);
  1831. if (IS_ERR(chip->fcc_main_votable)) {
  1832. rc = PTR_ERR(chip->fcc_main_votable);
  1833. chip->fcc_main_votable = NULL;
  1834. goto release_wakeup_source;
  1835. }
  1836. chip->fcc_votable = create_votable("FCC", VOTE_MIN,
  1837. pl_fcc_vote_callback,
  1838. chip);
  1839. if (IS_ERR(chip->fcc_votable)) {
  1840. rc = PTR_ERR(chip->fcc_votable);
  1841. chip->fcc_votable = NULL;
  1842. goto destroy_votable;
  1843. }
  1844. chip->fv_votable = create_votable("FV", VOTE_MIN,
  1845. pl_fv_vote_callback,
  1846. chip);
  1847. if (IS_ERR(chip->fv_votable)) {
  1848. rc = PTR_ERR(chip->fv_votable);
  1849. chip->fv_votable = NULL;
  1850. goto destroy_votable;
  1851. }
  1852. chip->usb_icl_votable = create_votable("USB_ICL", VOTE_MIN,
  1853. usb_icl_vote_callback,
  1854. chip);
  1855. if (IS_ERR(chip->usb_icl_votable)) {
  1856. rc = PTR_ERR(chip->usb_icl_votable);
  1857. chip->usb_icl_votable = NULL;
  1858. goto destroy_votable;
  1859. }
  1860. chip->pl_disable_votable = create_votable("PL_DISABLE", VOTE_SET_ANY,
  1861. pl_disable_vote_callback,
  1862. chip);
  1863. if (IS_ERR(chip->pl_disable_votable)) {
  1864. rc = PTR_ERR(chip->pl_disable_votable);
  1865. chip->pl_disable_votable = NULL;
  1866. goto destroy_votable;
  1867. }
  1868. vote(chip->pl_disable_votable, CHG_STATE_VOTER, true, 0);
  1869. vote(chip->pl_disable_votable, TAPER_END_VOTER, false, 0);
  1870. vote(chip->pl_disable_votable, PARALLEL_PSY_VOTER, true, 0);
  1871. chip->pl_awake_votable = create_votable("PL_AWAKE", VOTE_SET_ANY,
  1872. pl_awake_vote_callback,
  1873. chip);
  1874. if (IS_ERR(chip->pl_awake_votable)) {
  1875. rc = PTR_ERR(chip->pl_awake_votable);
  1876. chip->pl_awake_votable = NULL;
  1877. goto destroy_votable;
  1878. }
  1879. chip->pl_enable_votable_indirect = create_votable("PL_ENABLE_INDIRECT",
  1880. VOTE_SET_ANY,
  1881. pl_enable_indirect_vote_callback,
  1882. chip);
  1883. if (IS_ERR(chip->pl_enable_votable_indirect)) {
  1884. rc = PTR_ERR(chip->pl_enable_votable_indirect);
  1885. chip->pl_enable_votable_indirect = NULL;
  1886. goto destroy_votable;
  1887. }
  1888. vote(chip->pl_disable_votable, PL_INDIRECT_VOTER, true, 0);
  1889. rc = pl_register_notifier(chip);
  1890. if (rc < 0) {
  1891. pr_err("Couldn't register psy notifier rc = %d\n", rc);
  1892. goto unreg_notifier;
  1893. }
  1894. rc = pl_determine_initial_status(chip);
  1895. if (rc < 0) {
  1896. pr_err("Couldn't determine initial status rc=%d\n", rc);
  1897. goto unreg_notifier;
  1898. }
  1899. chip->pl_disable = true;
  1900. chip->cp_disabled = true;
  1901. chip->qcom_batt_class.name = "qcom-battery",
  1902. chip->qcom_batt_class.owner = THIS_MODULE,
  1903. chip->qcom_batt_class.class_groups = batt_class_groups;
  1904. rc = class_register(&chip->qcom_batt_class);
  1905. if (rc < 0) {
  1906. pr_err("couldn't register pl_data sysfs class rc = %d\n", rc);
  1907. goto unreg_notifier;
  1908. }
  1909. the_chip = chip;
  1910. return 0;
  1911. unreg_notifier:
  1912. power_supply_unreg_notifier(&chip->nb);
  1913. destroy_votable:
  1914. destroy_votable(chip->pl_enable_votable_indirect);
  1915. destroy_votable(chip->pl_awake_votable);
  1916. destroy_votable(chip->pl_disable_votable);
  1917. destroy_votable(chip->fv_votable);
  1918. destroy_votable(chip->fcc_votable);
  1919. destroy_votable(chip->fcc_main_votable);
  1920. destroy_votable(chip->usb_icl_votable);
  1921. release_wakeup_source:
  1922. wakeup_source_unregister(chip->pl_ws);
  1923. cleanup:
  1924. kfree(chip);
  1925. return rc;
  1926. }
  1927. void qcom_batt_deinit(void)
  1928. {
  1929. struct pl_data *chip = the_chip;
  1930. if (chip == NULL)
  1931. return;
  1932. cancel_delayed_work_sync(&chip->status_change_work);
  1933. cancel_work_sync(&chip->pl_taper_work);
  1934. cancel_work_sync(&chip->pl_disable_forever_work);
  1935. cancel_delayed_work_sync(&chip->fcc_stepper_work);
  1936. power_supply_unreg_notifier(&chip->nb);
  1937. destroy_votable(chip->pl_enable_votable_indirect);
  1938. destroy_votable(chip->pl_awake_votable);
  1939. destroy_votable(chip->pl_disable_votable);
  1940. destroy_votable(chip->fv_votable);
  1941. destroy_votable(chip->fcc_votable);
  1942. destroy_votable(chip->fcc_main_votable);
  1943. wakeup_source_unregister(chip->pl_ws);
  1944. the_chip = NULL;
  1945. kfree(chip);
  1946. }