blob: ff7da6c2d7f20f7d81cfd9683b82b33bddd212f6 [file] [log] [blame]
Harry Yang4b7db0f2017-11-27 10:50:44 -08001/* Copyright (c) 2018 The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/device.h>
14#include <linux/regmap.h>
15#include <linux/delay.h>
16#include <linux/power_supply.h>
17#include <linux/regulator/driver.h>
18#include <linux/qpnp/qpnp-revid.h>
19#include <linux/irq.h>
20#include <linux/pmic-voter.h>
21#include "smb5-lib.h"
22#include "smb5-reg.h"
23#include "battery.h"
24#include "step-chg-jeita.h"
25#include "storm-watch.h"
26
27#define smblib_err(chg, fmt, ...) \
28 pr_err("%s: %s: " fmt, chg->name, \
29 __func__, ##__VA_ARGS__) \
30
31#define smblib_dbg(chg, reason, fmt, ...) \
32 do { \
33 if (*chg->debug_mask & (reason)) \
34 pr_info("%s: %s: " fmt, chg->name, \
35 __func__, ##__VA_ARGS__); \
36 else \
37 pr_debug("%s: %s: " fmt, chg->name, \
38 __func__, ##__VA_ARGS__); \
39 } while (0)
40
41
42int smblib_read(struct smb_charger *chg, u16 addr, u8 *val)
43{
44 unsigned int value;
45 int rc = 0;
46
47 rc = regmap_read(chg->regmap, addr, &value);
48 if (rc >= 0)
49 *val = (u8)value;
50
51 return rc;
52}
53
54int smblib_batch_read(struct smb_charger *chg, u16 addr, u8 *val,
55 int count)
56{
57 return regmap_bulk_read(chg->regmap, addr, val, count);
58}
59
60int smblib_write(struct smb_charger *chg, u16 addr, u8 val)
61{
62 return regmap_write(chg->regmap, addr, val);
63}
64
65int smblib_masked_write(struct smb_charger *chg, u16 addr, u8 mask, u8 val)
66{
67
68 return regmap_update_bits(chg->regmap, addr, mask, val);
69
70}
71
72int smblib_get_jeita_cc_delta(struct smb_charger *chg, int *cc_delta_ua)
73{
74 int rc, cc_minus_ua;
75 u8 stat;
76
77 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_7_REG, &stat);
78 if (rc < 0) {
79 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n",
80 rc);
81 return rc;
82 }
83
84 if (stat & BAT_TEMP_STATUS_HOT_SOFT_BIT) {
85 rc = smblib_get_charge_param(chg, &chg->param.jeita_cc_comp_hot,
86 &cc_minus_ua);
87 if (rc < 0) {
88 smblib_err(chg, "Couldn't get jeita cc minus rc=%d\n",
89 rc);
90 return rc;
91 }
92 } else if (stat & BAT_TEMP_STATUS_COLD_SOFT_BIT) {
93 rc = smblib_get_charge_param(chg,
94 &chg->param.jeita_cc_comp_cold,
95 &cc_minus_ua);
96 if (rc < 0) {
97 smblib_err(chg, "Couldn't get jeita cc minus rc=%d\n",
98 rc);
99 return rc;
100 }
101 } else {
102 cc_minus_ua = 0;
103 }
104
105 *cc_delta_ua = -cc_minus_ua;
106
107 return 0;
108}
109
110int smblib_icl_override(struct smb_charger *chg, bool override)
111{
112 int rc;
113
114 rc = smblib_masked_write(chg, USBIN_LOAD_CFG_REG,
115 ICL_OVERRIDE_AFTER_APSD_BIT,
116 override ? ICL_OVERRIDE_AFTER_APSD_BIT : 0);
117 if (rc < 0)
118 smblib_err(chg, "Couldn't override ICL rc=%d\n", rc);
119
120 return rc;
121}
122
123int smblib_stat_sw_override_cfg(struct smb_charger *chg, bool override)
124{
125 int rc = 0;
126
127 /* override = 1, SW STAT override; override = 0, HW auto mode */
128 rc = smblib_masked_write(chg, MISC_SMB_EN_CMD_REG,
129 SMB_EN_OVERRIDE_BIT,
130 override ? SMB_EN_OVERRIDE_BIT : 0);
131 if (rc < 0) {
132 dev_err(chg->dev, "Couldn't configure SW STAT override rc=%d\n",
133 rc);
134 return rc;
135 }
136
137 return rc;
138}
139
140/********************
141 * REGISTER GETTERS *
142 ********************/
143
144int smblib_get_charge_param(struct smb_charger *chg,
145 struct smb_chg_param *param, int *val_u)
146{
147 int rc = 0;
148 u8 val_raw;
149
150 rc = smblib_read(chg, param->reg, &val_raw);
151 if (rc < 0) {
152 smblib_err(chg, "%s: Couldn't read from 0x%04x rc=%d\n",
153 param->name, param->reg, rc);
154 return rc;
155 }
156
157 if (param->get_proc)
158 *val_u = param->get_proc(param, val_raw);
159 else
160 *val_u = val_raw * param->step_u + param->min_u;
161 smblib_dbg(chg, PR_REGISTER, "%s = %d (0x%02x)\n",
162 param->name, *val_u, val_raw);
163
164 return rc;
165}
166
167int smblib_get_usb_suspend(struct smb_charger *chg, int *suspend)
168{
169 int rc = 0;
170 u8 temp;
171
172 rc = smblib_read(chg, USBIN_CMD_IL_REG, &temp);
173 if (rc < 0) {
174 smblib_err(chg, "Couldn't read USBIN_CMD_IL rc=%d\n", rc);
175 return rc;
176 }
177 *suspend = temp & USBIN_SUSPEND_BIT;
178
179 return rc;
180}
181
182struct apsd_result {
183 const char * const name;
184 const u8 bit;
185 const enum power_supply_type pst;
186};
187
188enum {
189 UNKNOWN,
190 SDP,
191 CDP,
192 DCP,
193 OCP,
194 FLOAT,
195 HVDCP2,
196 HVDCP3,
197 MAX_TYPES
198};
199
200static const struct apsd_result smblib_apsd_results[] = {
201 [UNKNOWN] = {
202 .name = "UNKNOWN",
203 .bit = 0,
204 .pst = POWER_SUPPLY_TYPE_UNKNOWN
205 },
206 [SDP] = {
207 .name = "SDP",
208 .bit = SDP_CHARGER_BIT,
209 .pst = POWER_SUPPLY_TYPE_USB
210 },
211 [CDP] = {
212 .name = "CDP",
213 .bit = CDP_CHARGER_BIT,
214 .pst = POWER_SUPPLY_TYPE_USB_CDP
215 },
216 [DCP] = {
217 .name = "DCP",
218 .bit = DCP_CHARGER_BIT,
219 .pst = POWER_SUPPLY_TYPE_USB_DCP
220 },
221 [OCP] = {
222 .name = "OCP",
223 .bit = OCP_CHARGER_BIT,
224 .pst = POWER_SUPPLY_TYPE_USB_DCP
225 },
226 [FLOAT] = {
227 .name = "FLOAT",
228 .bit = FLOAT_CHARGER_BIT,
229 .pst = POWER_SUPPLY_TYPE_USB_FLOAT
230 },
231 [HVDCP2] = {
232 .name = "HVDCP2",
233 .bit = DCP_CHARGER_BIT | QC_2P0_BIT,
234 .pst = POWER_SUPPLY_TYPE_USB_HVDCP
235 },
236 [HVDCP3] = {
237 .name = "HVDCP3",
238 .bit = DCP_CHARGER_BIT | QC_3P0_BIT,
239 .pst = POWER_SUPPLY_TYPE_USB_HVDCP_3,
240 },
241};
242
243static const struct apsd_result *smblib_get_apsd_result(struct smb_charger *chg)
244{
245 int rc, i;
246 u8 apsd_stat, stat;
247 const struct apsd_result *result = &smblib_apsd_results[UNKNOWN];
248
249 rc = smblib_read(chg, APSD_STATUS_REG, &apsd_stat);
250 if (rc < 0) {
251 smblib_err(chg, "Couldn't read APSD_STATUS rc=%d\n", rc);
252 return result;
253 }
254 smblib_dbg(chg, PR_REGISTER, "APSD_STATUS = 0x%02x\n", apsd_stat);
255
256 if (!(apsd_stat & APSD_DTC_STATUS_DONE_BIT))
257 return result;
258
259 rc = smblib_read(chg, APSD_RESULT_STATUS_REG, &stat);
260 if (rc < 0) {
261 smblib_err(chg, "Couldn't read APSD_RESULT_STATUS rc=%d\n",
262 rc);
263 return result;
264 }
265 stat &= APSD_RESULT_STATUS_MASK;
266
267 for (i = 0; i < ARRAY_SIZE(smblib_apsd_results); i++) {
268 if (smblib_apsd_results[i].bit == stat)
269 result = &smblib_apsd_results[i];
270 }
271
272 if (apsd_stat & QC_CHARGER_BIT) {
273 /* since its a qc_charger, either return HVDCP3 or HVDCP2 */
274 if (result != &smblib_apsd_results[HVDCP3])
275 result = &smblib_apsd_results[HVDCP2];
276 }
277
278 return result;
279}
280
281/********************
282 * REGISTER SETTERS *
283 ********************/
284
285int smblib_set_opt_switcher_freq(struct smb_charger *chg, int fsw_khz)
286{
287 union power_supply_propval pval = {0, };
288 int rc = 0;
289
290 rc = smblib_set_charge_param(chg, &chg->param.freq_switcher, fsw_khz);
291 if (rc < 0)
292 dev_err(chg->dev, "Error in setting freq_buck rc=%d\n", rc);
293
294 if (chg->mode == PARALLEL_MASTER && chg->pl.psy) {
295 pval.intval = fsw_khz;
296 /*
297 * Some parallel charging implementations may not have
298 * PROP_BUCK_FREQ property - they could be running
299 * with a fixed frequency
300 */
301 power_supply_set_property(chg->pl.psy,
302 POWER_SUPPLY_PROP_BUCK_FREQ, &pval);
303 }
304
305 return rc;
306}
307
308int smblib_set_charge_param(struct smb_charger *chg,
309 struct smb_chg_param *param, int val_u)
310{
311 int rc = 0;
312 u8 val_raw;
313
314 if (param->set_proc) {
315 rc = param->set_proc(param, val_u, &val_raw);
316 if (rc < 0)
317 return -EINVAL;
318 } else {
319 if (val_u > param->max_u || val_u < param->min_u) {
320 smblib_err(chg, "%s: %d is out of range [%d, %d]\n",
321 param->name, val_u, param->min_u, param->max_u);
322 return -EINVAL;
323 }
324
325 val_raw = (val_u - param->min_u) / param->step_u;
326 }
327
328 rc = smblib_write(chg, param->reg, val_raw);
329 if (rc < 0) {
330 smblib_err(chg, "%s: Couldn't write 0x%02x to 0x%04x rc=%d\n",
331 param->name, val_raw, param->reg, rc);
332 return rc;
333 }
334
335 smblib_dbg(chg, PR_REGISTER, "%s = %d (0x%02x)\n",
336 param->name, val_u, val_raw);
337
338 return rc;
339}
340
341int smblib_set_usb_suspend(struct smb_charger *chg, bool suspend)
342{
343 int rc = 0;
344 int irq = chg->irq_info[USBIN_ICL_CHANGE_IRQ].irq;
345
346 if (suspend && irq) {
347 if (chg->usb_icl_change_irq_enabled) {
348 disable_irq_nosync(irq);
349 chg->usb_icl_change_irq_enabled = false;
350 }
351 }
352
353 rc = smblib_masked_write(chg, USBIN_CMD_IL_REG, USBIN_SUSPEND_BIT,
354 suspend ? USBIN_SUSPEND_BIT : 0);
355 if (rc < 0)
356 smblib_err(chg, "Couldn't write %s to USBIN_SUSPEND_BIT rc=%d\n",
357 suspend ? "suspend" : "resume", rc);
358
359 if (!suspend && irq) {
360 if (!chg->usb_icl_change_irq_enabled) {
361 enable_irq(irq);
362 chg->usb_icl_change_irq_enabled = true;
363 }
364 }
365
366 return rc;
367}
368
369int smblib_set_dc_suspend(struct smb_charger *chg, bool suspend)
370{
371 int rc = 0;
372
373 rc = smblib_masked_write(chg, DCIN_CMD_IL_REG, DCIN_SUSPEND_BIT,
374 suspend ? DCIN_SUSPEND_BIT : 0);
375 if (rc < 0)
376 smblib_err(chg, "Couldn't write %s to DCIN_SUSPEND_BIT rc=%d\n",
377 suspend ? "suspend" : "resume", rc);
378
379 return rc;
380}
381
382static int smblib_set_adapter_allowance(struct smb_charger *chg,
383 u8 allowed_voltage)
384{
385 int rc = 0;
386
387 rc = smblib_write(chg, USBIN_ADAPTER_ALLOW_CFG_REG, allowed_voltage);
388 if (rc < 0) {
389 smblib_err(chg, "Couldn't write 0x%02x to USBIN_ADAPTER_ALLOW_CFG rc=%d\n",
390 allowed_voltage, rc);
391 return rc;
392 }
393
394 return rc;
395}
396
397#define MICRO_5V 5000000
398#define MICRO_9V 9000000
399#define MICRO_12V 12000000
400static int smblib_set_usb_pd_allowed_voltage(struct smb_charger *chg,
401 int min_allowed_uv, int max_allowed_uv)
402{
403 int rc;
404 u8 allowed_voltage;
405
406 if (min_allowed_uv == MICRO_5V && max_allowed_uv == MICRO_5V) {
407 allowed_voltage = USBIN_ADAPTER_ALLOW_5V;
408 smblib_set_opt_switcher_freq(chg, chg->chg_freq.freq_5V);
409 } else if (min_allowed_uv == MICRO_9V && max_allowed_uv == MICRO_9V) {
410 allowed_voltage = USBIN_ADAPTER_ALLOW_9V;
411 smblib_set_opt_switcher_freq(chg, chg->chg_freq.freq_9V);
412 } else if (min_allowed_uv == MICRO_12V && max_allowed_uv == MICRO_12V) {
413 allowed_voltage = USBIN_ADAPTER_ALLOW_12V;
414 smblib_set_opt_switcher_freq(chg, chg->chg_freq.freq_12V);
415 } else if (min_allowed_uv < MICRO_9V && max_allowed_uv <= MICRO_9V) {
416 allowed_voltage = USBIN_ADAPTER_ALLOW_5V_TO_9V;
417 } else if (min_allowed_uv < MICRO_9V && max_allowed_uv <= MICRO_12V) {
418 allowed_voltage = USBIN_ADAPTER_ALLOW_5V_TO_12V;
419 } else if (min_allowed_uv < MICRO_12V && max_allowed_uv <= MICRO_12V) {
420 allowed_voltage = USBIN_ADAPTER_ALLOW_9V_TO_12V;
421 } else {
422 smblib_err(chg, "invalid allowed voltage [%d, %d]\n",
423 min_allowed_uv, max_allowed_uv);
424 return -EINVAL;
425 }
426
427 rc = smblib_set_adapter_allowance(chg, allowed_voltage);
428 if (rc < 0) {
429 smblib_err(chg, "Couldn't configure adapter allowance rc=%d\n",
430 rc);
431 return rc;
432 }
433
434 return rc;
435}
436
437/********************
438 * HELPER FUNCTIONS *
439 ********************/
440static int smblib_request_dpdm(struct smb_charger *chg, bool enable)
441{
442 int rc = 0;
443
444 /* fetch the DPDM regulator */
445 if (!chg->dpdm_reg && of_get_property(chg->dev->of_node,
446 "dpdm-supply", NULL)) {
447 chg->dpdm_reg = devm_regulator_get(chg->dev, "dpdm");
448 if (IS_ERR(chg->dpdm_reg)) {
449 rc = PTR_ERR(chg->dpdm_reg);
450 smblib_err(chg, "Couldn't get dpdm regulator rc=%d\n",
451 rc);
452 chg->dpdm_reg = NULL;
453 return rc;
454 }
455 }
456
457 if (enable) {
458 if (chg->dpdm_reg && !regulator_is_enabled(chg->dpdm_reg)) {
459 smblib_dbg(chg, PR_MISC, "enabling DPDM regulator\n");
460 rc = regulator_enable(chg->dpdm_reg);
461 if (rc < 0)
462 smblib_err(chg,
463 "Couldn't enable dpdm regulator rc=%d\n",
464 rc);
465 }
466 } else {
467 if (chg->dpdm_reg && regulator_is_enabled(chg->dpdm_reg)) {
468 smblib_dbg(chg, PR_MISC, "disabling DPDM regulator\n");
469 rc = regulator_disable(chg->dpdm_reg);
470 if (rc < 0)
471 smblib_err(chg,
472 "Couldn't disable dpdm regulator rc=%d\n",
473 rc);
474 }
475 }
476
477 return rc;
478}
479
480static void smblib_rerun_apsd(struct smb_charger *chg)
481{
482 int rc;
483
484 smblib_dbg(chg, PR_MISC, "re-running APSD\n");
485
486 rc = smblib_masked_write(chg, CMD_APSD_REG,
487 APSD_RERUN_BIT, APSD_RERUN_BIT);
488 if (rc < 0)
489 smblib_err(chg, "Couldn't re-run APSD rc=%d\n", rc);
490}
491
492static const struct apsd_result *smblib_update_usb_type(struct smb_charger *chg)
493{
494 const struct apsd_result *apsd_result = smblib_get_apsd_result(chg);
495
496 /* if PD is active, APSD is disabled so won't have a valid result */
497 if (chg->pd_active) {
498 chg->real_charger_type = POWER_SUPPLY_TYPE_USB_PD;
499 } else {
500 /*
501 * Update real charger type only if its not FLOAT
502 * detected as as SDP
503 */
504 if (!(apsd_result->pst == POWER_SUPPLY_TYPE_USB_FLOAT &&
505 chg->real_charger_type == POWER_SUPPLY_TYPE_USB))
506 chg->real_charger_type = apsd_result->pst;
507 }
508
509 smblib_dbg(chg, PR_MISC, "APSD=%s PD=%d\n",
510 apsd_result->name, chg->pd_active);
511 return apsd_result;
512}
513
514static int smblib_notifier_call(struct notifier_block *nb,
515 unsigned long ev, void *v)
516{
517 struct power_supply *psy = v;
518 struct smb_charger *chg = container_of(nb, struct smb_charger, nb);
519
520 if (!strcmp(psy->desc->name, "bms")) {
521 if (!chg->bms_psy)
522 chg->bms_psy = psy;
523 if (ev == PSY_EVENT_PROP_CHANGED)
524 schedule_work(&chg->bms_update_work);
525 }
526
527 if (!chg->pl.psy && !strcmp(psy->desc->name, "parallel")) {
528 chg->pl.psy = psy;
529 schedule_work(&chg->pl_update_work);
530 }
531
532 return NOTIFY_OK;
533}
534
535static int smblib_register_notifier(struct smb_charger *chg)
536{
537 int rc;
538
539 chg->nb.notifier_call = smblib_notifier_call;
540 rc = power_supply_reg_notifier(&chg->nb);
541 if (rc < 0) {
542 smblib_err(chg, "Couldn't register psy notifier rc = %d\n", rc);
543 return rc;
544 }
545
546 return 0;
547}
548
549int smblib_mapping_soc_from_field_value(struct smb_chg_param *param,
550 int val_u, u8 *val_raw)
551{
552 if (val_u > param->max_u || val_u < param->min_u)
553 return -EINVAL;
554
555 *val_raw = val_u << 1;
556
557 return 0;
558}
559
560int smblib_mapping_cc_delta_to_field_value(struct smb_chg_param *param,
561 u8 val_raw)
562{
563 int val_u = val_raw * param->step_u + param->min_u;
564
565 if (val_u > param->max_u)
566 val_u -= param->max_u * 2;
567
568 return val_u;
569}
570
571int smblib_mapping_cc_delta_from_field_value(struct smb_chg_param *param,
572 int val_u, u8 *val_raw)
573{
574 if (val_u > param->max_u || val_u < param->min_u - param->max_u)
575 return -EINVAL;
576
577 val_u += param->max_u * 2 - param->min_u;
578 val_u %= param->max_u * 2;
579 *val_raw = val_u / param->step_u;
580
581 return 0;
582}
583
584static void smblib_uusb_removal(struct smb_charger *chg)
585{
586 int rc;
587 struct smb_irq_data *data;
588 struct storm_watch *wdata;
589
590 cancel_delayed_work_sync(&chg->pl_enable_work);
591
592 rc = smblib_request_dpdm(chg, false);
593 if (rc < 0)
594 smblib_err(chg, "Couldn't to disable DPDM rc=%d\n", rc);
595
596 if (chg->wa_flags & BOOST_BACK_WA) {
597 data = chg->irq_info[SWITCHER_POWER_OK_IRQ].irq_data;
598 if (data) {
599 wdata = &data->storm_data;
600 update_storm_count(wdata, WEAK_CHG_STORM_COUNT);
601 vote(chg->usb_icl_votable, BOOST_BACK_VOTER, false, 0);
602 vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
603 false, 0);
604 }
605 }
606 vote(chg->pl_disable_votable, PL_DELAY_VOTER, true, 0);
607 vote(chg->awake_votable, PL_DELAY_VOTER, false, 0);
608
609 /* reset both usbin current and voltage votes */
610 vote(chg->pl_enable_votable_indirect, USBIN_I_VOTER, false, 0);
611 vote(chg->pl_enable_votable_indirect, USBIN_V_VOTER, false, 0);
612 vote(chg->usb_icl_votable, SW_QC3_VOTER, false, 0);
613
614 /* reconfigure allowed voltage for HVDCP */
615 rc = smblib_set_adapter_allowance(chg,
616 USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V);
617 if (rc < 0)
618 smblib_err(chg, "Couldn't set USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V rc=%d\n",
619 rc);
620
621 chg->voltage_min_uv = MICRO_5V;
622 chg->voltage_max_uv = MICRO_5V;
623 chg->usb_icl_delta_ua = 0;
624 chg->pulse_cnt = 0;
625 chg->uusb_apsd_rerun_done = false;
626
627 /* clear USB ICL vote for USB_PSY_VOTER */
628 rc = vote(chg->usb_icl_votable, USB_PSY_VOTER, false, 0);
629 if (rc < 0)
630 smblib_err(chg, "Couldn't un-vote for USB ICL rc=%d\n", rc);
631
632 /* clear USB ICL vote for DCP_VOTER */
633 rc = vote(chg->usb_icl_votable, DCP_VOTER, false, 0);
634 if (rc < 0)
635 smblib_err(chg,
636 "Couldn't un-vote DCP from USB ICL rc=%d\n", rc);
637}
638
639void smblib_suspend_on_debug_battery(struct smb_charger *chg)
640{
641 int rc;
642 union power_supply_propval val;
643
644 if (!chg->suspend_input_on_debug_batt)
645 return;
646
647 rc = power_supply_get_property(chg->bms_psy,
648 POWER_SUPPLY_PROP_DEBUG_BATTERY, &val);
649 if (rc < 0) {
650 smblib_err(chg, "Couldn't get debug battery prop rc=%d\n", rc);
651 return;
652 }
653
654 vote(chg->usb_icl_votable, DEBUG_BOARD_VOTER, val.intval, 0);
655 vote(chg->dc_suspend_votable, DEBUG_BOARD_VOTER, val.intval, 0);
656 if (val.intval)
657 pr_info("Input suspended: Fake battery\n");
658}
659
660int smblib_rerun_apsd_if_required(struct smb_charger *chg)
661{
662 union power_supply_propval val;
663 int rc;
664
665 rc = smblib_get_prop_usb_present(chg, &val);
666 if (rc < 0) {
667 smblib_err(chg, "Couldn't get usb present rc = %d\n", rc);
668 return rc;
669 }
670
671 if (!val.intval)
672 return 0;
673
674 rc = smblib_request_dpdm(chg, true);
675 if (rc < 0)
676 smblib_err(chg, "Couldn't to enable DPDM rc=%d\n", rc);
677
678 chg->uusb_apsd_rerun_done = true;
679 smblib_rerun_apsd(chg);
680
681 return 0;
682}
683
684static int smblib_get_pulse_cnt(struct smb_charger *chg, int *count)
685{
686 *count = chg->pulse_cnt;
687 return 0;
688}
689
690#define USBIN_25MA 25000
691#define USBIN_100MA 100000
692#define USBIN_150MA 150000
693#define USBIN_500MA 500000
694#define USBIN_900MA 900000
695static int set_sdp_current(struct smb_charger *chg, int icl_ua)
696{
697 int rc;
698 u8 icl_options;
699 const struct apsd_result *apsd_result = smblib_get_apsd_result(chg);
700
701 /* power source is SDP */
702 switch (icl_ua) {
703 case USBIN_100MA:
704 /* USB 2.0 100mA */
705 icl_options = 0;
706 break;
707 case USBIN_150MA:
708 /* USB 3.0 150mA */
709 icl_options = CFG_USB3P0_SEL_BIT;
710 break;
711 case USBIN_500MA:
712 /* USB 2.0 500mA */
713 icl_options = USB51_MODE_BIT;
714 break;
715 case USBIN_900MA:
716 /* USB 3.0 900mA */
717 icl_options = CFG_USB3P0_SEL_BIT | USB51_MODE_BIT;
718 break;
719 default:
720 smblib_err(chg, "ICL %duA isn't supported for SDP\n", icl_ua);
721 return -EINVAL;
722 }
723
724 if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB &&
725 apsd_result->pst == POWER_SUPPLY_TYPE_USB_FLOAT) {
726 /*
727 * change the float charger configuration to SDP, if this
728 * is the case of SDP being detected as FLOAT
729 */
730 rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG,
731 FORCE_FLOAT_SDP_CFG_BIT, FORCE_FLOAT_SDP_CFG_BIT);
732 if (rc < 0) {
733 smblib_err(chg, "Couldn't set float ICL options rc=%d\n",
734 rc);
735 return rc;
736 }
737 }
738
739 rc = smblib_masked_write(chg, USBIN_ICL_OPTIONS_REG,
740 CFG_USB3P0_SEL_BIT | USB51_MODE_BIT, icl_options);
741 if (rc < 0) {
742 smblib_err(chg, "Couldn't set ICL options rc=%d\n", rc);
743 return rc;
744 }
745
746 return rc;
747}
748
749static int get_sdp_current(struct smb_charger *chg, int *icl_ua)
750{
751 int rc;
752 u8 icl_options;
753 bool usb3 = false;
754
755 rc = smblib_read(chg, USBIN_ICL_OPTIONS_REG, &icl_options);
756 if (rc < 0) {
757 smblib_err(chg, "Couldn't get ICL options rc=%d\n", rc);
758 return rc;
759 }
760
761 usb3 = (icl_options & CFG_USB3P0_SEL_BIT);
762
763 if (icl_options & USB51_MODE_BIT)
764 *icl_ua = usb3 ? USBIN_900MA : USBIN_500MA;
765 else
766 *icl_ua = usb3 ? USBIN_150MA : USBIN_100MA;
767
768 return rc;
769}
770
771int smblib_set_icl_current(struct smb_charger *chg, int icl_ua)
772{
773 int rc = 0;
774 bool override;
775
776 /* suspend and return if 25mA or less is requested */
777 if (icl_ua <= USBIN_25MA)
778 return smblib_set_usb_suspend(chg, true);
779
780 if (icl_ua == INT_MAX)
781 goto override_suspend_config;
782
783 /* configure current */
784 if (chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT
785 && (chg->real_charger_type == POWER_SUPPLY_TYPE_USB)) {
786 rc = set_sdp_current(chg, icl_ua);
787 if (rc < 0) {
788 smblib_err(chg, "Couldn't set SDP ICL rc=%d\n", rc);
789 goto enable_icl_changed_interrupt;
790 }
791 } else {
792 set_sdp_current(chg, 100000);
793 rc = smblib_set_charge_param(chg, &chg->param.usb_icl, icl_ua);
794 if (rc < 0) {
795 smblib_err(chg, "Couldn't set HC ICL rc=%d\n", rc);
796 goto enable_icl_changed_interrupt;
797 }
798 }
799
800override_suspend_config:
801 /* determine if override needs to be enforced */
802 override = true;
803 if (icl_ua == INT_MAX) {
804 /* remove override if no voters - hw defaults is desired */
805 override = false;
806 } else if (chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT) {
807 if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB)
808 /* For std cable with type = SDP never override */
809 override = false;
810 else if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_CDP
811 && icl_ua == 1500000)
812 /*
813 * For std cable with type = CDP override only if
814 * current is not 1500mA
815 */
816 override = false;
817 }
818
819 /* enforce override */
820 rc = smblib_masked_write(chg, USBIN_ICL_OPTIONS_REG,
821 USBIN_MODE_CHG_BIT, override ? USBIN_MODE_CHG_BIT : 0);
822
823 rc = smblib_icl_override(chg, override);
824 if (rc < 0) {
825 smblib_err(chg, "Couldn't set ICL override rc=%d\n", rc);
826 goto enable_icl_changed_interrupt;
827 }
828
829 /* unsuspend after configuring current and override */
830 rc = smblib_set_usb_suspend(chg, false);
831 if (rc < 0) {
832 smblib_err(chg, "Couldn't resume input rc=%d\n", rc);
833 goto enable_icl_changed_interrupt;
834 }
835
836enable_icl_changed_interrupt:
837 return rc;
838}
839
840int smblib_get_icl_current(struct smb_charger *chg, int *icl_ua)
841{
842 int rc = 0;
843 u8 load_cfg;
844 bool override;
845
846 if ((chg->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_DEFAULT
847 || chg->micro_usb_mode)
848 && (chg->usb_psy->desc->type == POWER_SUPPLY_TYPE_USB)) {
849 rc = get_sdp_current(chg, icl_ua);
850 if (rc < 0) {
851 smblib_err(chg, "Couldn't get SDP ICL rc=%d\n", rc);
852 return rc;
853 }
854 } else {
855 rc = smblib_read(chg, USBIN_LOAD_CFG_REG, &load_cfg);
856 if (rc < 0) {
857 smblib_err(chg, "Couldn't get load cfg rc=%d\n", rc);
858 return rc;
859 }
860 override = load_cfg & ICL_OVERRIDE_AFTER_APSD_BIT;
861 if (!override)
862 return INT_MAX;
863
864 /* override is set */
865 rc = smblib_get_charge_param(chg, &chg->param.usb_icl, icl_ua);
866 if (rc < 0) {
867 smblib_err(chg, "Couldn't get HC ICL rc=%d\n", rc);
868 return rc;
869 }
870 }
871
872 return 0;
873}
874
875/*********************
876 * VOTABLE CALLBACKS *
877 *********************/
878
879static int smblib_dc_suspend_vote_callback(struct votable *votable, void *data,
880 int suspend, const char *client)
881{
882 struct smb_charger *chg = data;
883
884 /* resume input if suspend is invalid */
885 if (suspend < 0)
886 suspend = 0;
887
888 return smblib_set_dc_suspend(chg, (bool)suspend);
889}
890
891static int smblib_pd_disallowed_votable_indirect_callback(
892 struct votable *votable, void *data, int disallowed, const char *client)
893{
894 struct smb_charger *chg = data;
895 int rc;
896
897 rc = vote(chg->pd_allowed_votable, PD_DISALLOWED_INDIRECT_VOTER,
898 !disallowed, 0);
899
900 return rc;
901}
902
903static int smblib_awake_vote_callback(struct votable *votable, void *data,
904 int awake, const char *client)
905{
906 struct smb_charger *chg = data;
907
908 if (awake)
909 pm_stay_awake(chg->dev);
910 else
911 pm_relax(chg->dev);
912
913 return 0;
914}
915
916static int smblib_chg_disable_vote_callback(struct votable *votable, void *data,
917 int chg_disable, const char *client)
918{
919 struct smb_charger *chg = data;
920 int rc;
921
922 rc = smblib_masked_write(chg, CHARGING_ENABLE_CMD_REG,
923 CHARGING_ENABLE_CMD_BIT,
924 chg_disable ? 0 : CHARGING_ENABLE_CMD_BIT);
925 if (rc < 0) {
926 smblib_err(chg, "Couldn't %s charging rc=%d\n",
927 chg_disable ? "disable" : "enable", rc);
928 return rc;
929 }
930
931 return 0;
932}
933
934static int smblib_usb_irq_enable_vote_callback(struct votable *votable,
935 void *data, int enable, const char *client)
936{
937 struct smb_charger *chg = data;
938
939 if (!chg->irq_info[INPUT_CURRENT_LIMITING_IRQ].irq ||
940 !chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq)
941 return 0;
942
943 if (enable) {
944 enable_irq(chg->irq_info[INPUT_CURRENT_LIMITING_IRQ].irq);
945 enable_irq(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq);
946 } else {
947 disable_irq_nosync(
948 chg->irq_info[INPUT_CURRENT_LIMITING_IRQ].irq);
949 disable_irq_nosync(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq);
950 }
951
952 return 0;
953}
954
955/*******************
956 * VCONN REGULATOR *
957 * *****************/
958
959int smblib_vconn_regulator_enable(struct regulator_dev *rdev)
960{
961 struct smb_charger *chg = rdev_get_drvdata(rdev);
962 int rc = 0;
963
964 smblib_dbg(chg, PR_OTG, "enabling VCONN\n");
965
966 rc = smblib_masked_write(chg, TYPE_C_VCONN_CONTROL_REG,
967 VCONN_EN_VALUE_BIT, VCONN_EN_VALUE_BIT);
968 if (rc < 0) {
969 smblib_err(chg, "Couldn't enable vconn setting rc=%d\n", rc);
970 return rc;
971 }
972
973 return 0;
974}
975
976int smblib_vconn_regulator_disable(struct regulator_dev *rdev)
977{
978 struct smb_charger *chg = rdev_get_drvdata(rdev);
979 int rc = 0;
980
981 smblib_dbg(chg, PR_OTG, "disabling VCONN\n");
982 rc = smblib_masked_write(chg, TYPE_C_VCONN_CONTROL_REG,
983 VCONN_EN_VALUE_BIT, 0);
984 if (rc < 0)
985 smblib_err(chg, "Couldn't disable vconn regulator rc=%d\n", rc);
986
987 return 0;
988}
989
990int smblib_vconn_regulator_is_enabled(struct regulator_dev *rdev)
991{
992 struct smb_charger *chg = rdev_get_drvdata(rdev);
993 int rc;
994 u8 cmd;
995
996 rc = smblib_read(chg, TYPE_C_VCONN_CONTROL_REG, &cmd);
997 if (rc < 0) {
998 smblib_err(chg, "Couldn't read TYPE_C_INTRPT_ENB_SOFTWARE_CTRL rc=%d\n",
999 rc);
1000 return rc;
1001 }
1002
1003 return (cmd & VCONN_EN_VALUE_BIT) ? 1 : 0;
1004}
1005
1006/*****************
1007 * OTG REGULATOR *
1008 *****************/
1009
1010int smblib_vbus_regulator_enable(struct regulator_dev *rdev)
1011{
1012 struct smb_charger *chg = rdev_get_drvdata(rdev);
1013 int rc;
1014
1015 smblib_dbg(chg, PR_OTG, "enabling OTG\n");
1016
1017 rc = smblib_masked_write(chg, DCDC_CMD_OTG_REG, OTG_EN_BIT, OTG_EN_BIT);
1018 if (rc < 0) {
1019 smblib_err(chg, "Couldn't enable OTG rc=%d\n", rc);
1020 return rc;
1021 }
1022
1023 return 0;
1024}
1025
1026int smblib_vbus_regulator_disable(struct regulator_dev *rdev)
1027{
1028 struct smb_charger *chg = rdev_get_drvdata(rdev);
1029 int rc;
1030
1031 smblib_dbg(chg, PR_OTG, "disabling OTG\n");
1032
1033 rc = smblib_masked_write(chg, DCDC_CMD_OTG_REG, OTG_EN_BIT, 0);
1034 if (rc < 0) {
1035 smblib_err(chg, "Couldn't disable OTG regulator rc=%d\n", rc);
1036 return rc;
1037 }
1038
1039 return 0;
1040}
1041
1042int smblib_vbus_regulator_is_enabled(struct regulator_dev *rdev)
1043{
1044 struct smb_charger *chg = rdev_get_drvdata(rdev);
1045 int rc = 0;
1046 u8 cmd;
1047
1048 rc = smblib_read(chg, DCDC_CMD_OTG_REG, &cmd);
1049 if (rc < 0) {
1050 smblib_err(chg, "Couldn't read CMD_OTG rc=%d", rc);
1051 return rc;
1052 }
1053
1054 return (cmd & OTG_EN_BIT) ? 1 : 0;
1055}
1056
1057/********************
1058 * BATT PSY GETTERS *
1059 ********************/
1060
1061int smblib_get_prop_input_suspend(struct smb_charger *chg,
1062 union power_supply_propval *val)
1063{
1064 val->intval
1065 = (get_client_vote(chg->usb_icl_votable, USER_VOTER) == 0)
1066 && get_client_vote(chg->dc_suspend_votable, USER_VOTER);
1067 return 0;
1068}
1069
1070int smblib_get_prop_batt_present(struct smb_charger *chg,
1071 union power_supply_propval *val)
1072{
1073 int rc;
1074 u8 stat;
1075
1076 rc = smblib_read(chg, BATIF_BASE + INT_RT_STS_OFFSET, &stat);
1077 if (rc < 0) {
1078 smblib_err(chg, "Couldn't read BATIF_INT_RT_STS rc=%d\n", rc);
1079 return rc;
1080 }
1081
1082 val->intval = !(stat & (BAT_THERM_OR_ID_MISSING_RT_STS_BIT
1083 | BAT_TERMINAL_MISSING_RT_STS_BIT));
1084
1085 return rc;
1086}
1087
1088int smblib_get_prop_batt_capacity(struct smb_charger *chg,
1089 union power_supply_propval *val)
1090{
1091 int rc = -EINVAL;
1092
1093 if (chg->fake_capacity >= 0) {
1094 val->intval = chg->fake_capacity;
1095 return 0;
1096 }
1097
1098 if (chg->bms_psy)
1099 rc = power_supply_get_property(chg->bms_psy,
1100 POWER_SUPPLY_PROP_CAPACITY, val);
1101 return rc;
1102}
1103
1104int smblib_get_prop_batt_status(struct smb_charger *chg,
1105 union power_supply_propval *val)
1106{
1107 union power_supply_propval pval = {0, };
1108 bool usb_online, dc_online;
1109 u8 stat;
1110 int rc;
1111
1112 rc = smblib_get_prop_usb_online(chg, &pval);
1113 if (rc < 0) {
1114 smblib_err(chg, "Couldn't get usb online property rc=%d\n",
1115 rc);
1116 return rc;
1117 }
1118 usb_online = (bool)pval.intval;
1119
1120 rc = smblib_get_prop_dc_online(chg, &pval);
1121 if (rc < 0) {
1122 smblib_err(chg, "Couldn't get dc online property rc=%d\n",
1123 rc);
1124 return rc;
1125 }
1126 dc_online = (bool)pval.intval;
1127
1128 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat);
1129 if (rc < 0) {
1130 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
1131 rc);
1132 return rc;
1133 }
1134 stat = stat & BATTERY_CHARGER_STATUS_MASK;
1135
1136 if (!usb_online && !dc_online) {
1137 switch (stat) {
1138 case TERMINATE_CHARGE:
1139 case INHIBIT_CHARGE:
1140 val->intval = POWER_SUPPLY_STATUS_FULL;
1141 break;
1142 default:
1143 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1144 break;
1145 }
1146 return rc;
1147 }
1148
1149 switch (stat) {
1150 case TRICKLE_CHARGE:
1151 case PRE_CHARGE:
1152 case FULLON_CHARGE:
1153 case TAPER_CHARGE:
1154 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1155 break;
1156 case TERMINATE_CHARGE:
1157 case INHIBIT_CHARGE:
1158 val->intval = POWER_SUPPLY_STATUS_FULL;
1159 break;
1160 case DISABLE_CHARGE:
1161 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1162 break;
1163 default:
1164 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
1165 break;
1166 }
1167
1168 if (val->intval != POWER_SUPPLY_STATUS_CHARGING)
1169 return 0;
1170
1171 if (!usb_online && dc_online
1172 && chg->fake_batt_status == POWER_SUPPLY_STATUS_FULL) {
1173 val->intval = POWER_SUPPLY_STATUS_FULL;
1174 return 0;
1175 }
1176
1177 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_5_REG, &stat);
1178 if (rc < 0) {
1179 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n",
1180 rc);
1181 return rc;
1182 }
1183
1184 stat &= ENABLE_TRICKLE_BIT | ENABLE_PRE_CHARGING_BIT |
1185 ENABLE_FULLON_MODE_BIT;
1186
1187 if (!stat)
1188 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1189
1190 return 0;
1191}
1192
1193int smblib_get_prop_batt_charge_type(struct smb_charger *chg,
1194 union power_supply_propval *val)
1195{
1196 int rc;
1197 u8 stat;
1198
1199 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat);
1200 if (rc < 0) {
1201 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
1202 rc);
1203 return rc;
1204 }
1205
1206 switch (stat & BATTERY_CHARGER_STATUS_MASK) {
1207 case TRICKLE_CHARGE:
1208 case PRE_CHARGE:
1209 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1210 break;
1211 case FULLON_CHARGE:
1212 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1213 break;
1214 case TAPER_CHARGE:
1215 val->intval = POWER_SUPPLY_CHARGE_TYPE_TAPER;
1216 break;
1217 default:
1218 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1219 }
1220
1221 return rc;
1222}
1223
1224int smblib_get_prop_batt_health(struct smb_charger *chg,
1225 union power_supply_propval *val)
1226{
1227 union power_supply_propval pval;
1228 int rc;
1229 int effective_fv_uv;
1230 u8 stat;
1231
1232 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_2_REG, &stat);
1233 if (rc < 0) {
1234 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n",
1235 rc);
1236 return rc;
1237 }
1238 smblib_dbg(chg, PR_REGISTER, "BATTERY_CHARGER_STATUS_2 = 0x%02x\n",
1239 stat);
1240
1241 if (stat & CHARGER_ERROR_STATUS_BAT_OV_BIT) {
1242 rc = smblib_get_prop_batt_voltage_now(chg, &pval);
1243 if (!rc) {
1244 /*
1245 * If Vbatt is within 40mV above Vfloat, then don't
1246 * treat it as overvoltage.
1247 */
1248 effective_fv_uv = get_effective_result(chg->fv_votable);
1249 if (pval.intval >= effective_fv_uv + 40000) {
1250 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1251 smblib_err(chg, "battery over-voltage vbat_fg = %duV, fv = %duV\n",
1252 pval.intval, effective_fv_uv);
1253 goto done;
1254 }
1255 }
1256 }
1257
1258 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_7_REG, &stat);
1259 if (rc < 0) {
1260 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n",
1261 rc);
1262 return rc;
1263 }
1264 if (stat & BAT_TEMP_STATUS_TOO_COLD_BIT)
1265 val->intval = POWER_SUPPLY_HEALTH_COLD;
1266 else if (stat & BAT_TEMP_STATUS_TOO_HOT_BIT)
1267 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1268 else if (stat & BAT_TEMP_STATUS_COLD_SOFT_BIT)
1269 val->intval = POWER_SUPPLY_HEALTH_COOL;
1270 else if (stat & BAT_TEMP_STATUS_HOT_SOFT_BIT)
1271 val->intval = POWER_SUPPLY_HEALTH_WARM;
1272 else
1273 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1274
1275done:
1276 return rc;
1277}
1278
1279int smblib_get_prop_system_temp_level(struct smb_charger *chg,
1280 union power_supply_propval *val)
1281{
1282 val->intval = chg->system_temp_level;
1283 return 0;
1284}
1285
1286int smblib_get_prop_system_temp_level_max(struct smb_charger *chg,
1287 union power_supply_propval *val)
1288{
1289 val->intval = chg->thermal_levels;
1290 return 0;
1291}
1292
1293int smblib_get_prop_input_current_limited(struct smb_charger *chg,
1294 union power_supply_propval *val)
1295{
1296 u8 stat;
1297 int rc;
1298
1299 if (chg->fake_input_current_limited >= 0) {
1300 val->intval = chg->fake_input_current_limited;
1301 return 0;
1302 }
1303
1304 rc = smblib_read(chg, AICL_STATUS_REG, &stat);
1305 if (rc < 0) {
1306 smblib_err(chg, "Couldn't read AICL_STATUS rc=%d\n", rc);
1307 return rc;
1308 }
1309 val->intval = (stat & SOFT_ILIMIT_BIT) || chg->is_hdc;
1310 return 0;
1311}
1312
1313int smblib_get_prop_batt_voltage_now(struct smb_charger *chg,
1314 union power_supply_propval *val)
1315{
1316 int rc;
1317
1318 if (!chg->bms_psy)
1319 return -EINVAL;
1320
1321 rc = power_supply_get_property(chg->bms_psy,
1322 POWER_SUPPLY_PROP_VOLTAGE_NOW, val);
1323 return rc;
1324}
1325
1326int smblib_get_prop_batt_current_now(struct smb_charger *chg,
1327 union power_supply_propval *val)
1328{
1329 int rc;
1330
1331 if (!chg->bms_psy)
1332 return -EINVAL;
1333
1334 rc = power_supply_get_property(chg->bms_psy,
1335 POWER_SUPPLY_PROP_CURRENT_NOW, val);
1336 return rc;
1337}
1338
1339int smblib_get_prop_batt_temp(struct smb_charger *chg,
1340 union power_supply_propval *val)
1341{
1342 int rc;
1343
1344 if (!chg->bms_psy)
1345 return -EINVAL;
1346
1347 rc = power_supply_get_property(chg->bms_psy,
1348 POWER_SUPPLY_PROP_TEMP, val);
1349 return rc;
1350}
1351
1352int smblib_get_prop_batt_charge_done(struct smb_charger *chg,
1353 union power_supply_propval *val)
1354{
1355 int rc;
1356 u8 stat;
1357
1358 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat);
1359 if (rc < 0) {
1360 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
1361 rc);
1362 return rc;
1363 }
1364
1365 stat = stat & BATTERY_CHARGER_STATUS_MASK;
1366 val->intval = (stat == TERMINATE_CHARGE);
1367 return 0;
1368}
1369
1370int smblib_get_prop_batt_charge_counter(struct smb_charger *chg,
1371 union power_supply_propval *val)
1372{
1373 int rc;
1374
1375 if (!chg->bms_psy)
1376 return -EINVAL;
1377
1378 rc = power_supply_get_property(chg->bms_psy,
1379 POWER_SUPPLY_PROP_CHARGE_COUNTER, val);
1380 return rc;
1381}
1382
1383/***********************
1384 * BATTERY PSY SETTERS *
1385 ***********************/
1386
1387int smblib_set_prop_input_suspend(struct smb_charger *chg,
1388 const union power_supply_propval *val)
1389{
1390 int rc;
1391
1392 /* vote 0mA when suspended */
1393 rc = vote(chg->usb_icl_votable, USER_VOTER, (bool)val->intval, 0);
1394 if (rc < 0) {
1395 smblib_err(chg, "Couldn't vote to %s USB rc=%d\n",
1396 (bool)val->intval ? "suspend" : "resume", rc);
1397 return rc;
1398 }
1399
1400 rc = vote(chg->dc_suspend_votable, USER_VOTER, (bool)val->intval, 0);
1401 if (rc < 0) {
1402 smblib_err(chg, "Couldn't vote to %s DC rc=%d\n",
1403 (bool)val->intval ? "suspend" : "resume", rc);
1404 return rc;
1405 }
1406
1407 power_supply_changed(chg->batt_psy);
1408 return rc;
1409}
1410
1411int smblib_set_prop_batt_capacity(struct smb_charger *chg,
1412 const union power_supply_propval *val)
1413{
1414 chg->fake_capacity = val->intval;
1415
1416 power_supply_changed(chg->batt_psy);
1417
1418 return 0;
1419}
1420
1421int smblib_set_prop_batt_status(struct smb_charger *chg,
1422 const union power_supply_propval *val)
1423{
1424 /* Faking battery full */
1425 if (val->intval == POWER_SUPPLY_STATUS_FULL)
1426 chg->fake_batt_status = val->intval;
1427 else
1428 chg->fake_batt_status = -EINVAL;
1429
1430 power_supply_changed(chg->batt_psy);
1431
1432 return 0;
1433}
1434
1435int smblib_set_prop_system_temp_level(struct smb_charger *chg,
1436 const union power_supply_propval *val)
1437{
1438 if (val->intval < 0)
1439 return -EINVAL;
1440
1441 if (chg->thermal_levels <= 0)
1442 return -EINVAL;
1443
1444 if (val->intval > chg->thermal_levels)
1445 return -EINVAL;
1446
1447 chg->system_temp_level = val->intval;
1448 /* disable parallel charge in case of system temp level */
1449 vote(chg->pl_disable_votable, THERMAL_DAEMON_VOTER,
1450 chg->system_temp_level ? true : false, 0);
1451
1452 if (chg->system_temp_level == chg->thermal_levels)
1453 return vote(chg->chg_disable_votable,
1454 THERMAL_DAEMON_VOTER, true, 0);
1455
1456 vote(chg->chg_disable_votable, THERMAL_DAEMON_VOTER, false, 0);
1457 if (chg->system_temp_level == 0)
1458 return vote(chg->fcc_votable, THERMAL_DAEMON_VOTER, false, 0);
1459
1460 vote(chg->fcc_votable, THERMAL_DAEMON_VOTER, true,
1461 chg->thermal_mitigation[chg->system_temp_level]);
1462 return 0;
1463}
1464
1465int smblib_set_prop_input_current_limited(struct smb_charger *chg,
1466 const union power_supply_propval *val)
1467{
1468 chg->fake_input_current_limited = val->intval;
1469 return 0;
1470}
1471
1472int smblib_rerun_aicl(struct smb_charger *chg)
1473{
1474 int rc;
1475 u8 stat;
1476
1477 rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat);
1478 if (rc < 0) {
1479 smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n",
1480 rc);
1481 return rc;
1482 }
1483
1484 /* USB is suspended so skip re-running AICL */
1485 if (stat & USBIN_SUSPEND_STS_BIT)
1486 return rc;
1487
1488 smblib_dbg(chg, PR_MISC, "re-running AICL\n");
1489
1490 rc = smblib_masked_write(chg, AICL_CMD_REG, RERUN_AICL_BIT,
1491 RERUN_AICL_BIT);
1492 if (rc < 0)
1493 smblib_err(chg, "Couldn't write to AICL_CMD_REG rc=%d\n",
1494 rc);
1495 return 0;
1496}
1497
1498static int smblib_dp_pulse(struct smb_charger *chg)
1499{
1500 int rc;
1501
1502 /* QC 3.0 increment */
1503 rc = smblib_masked_write(chg, CMD_HVDCP_2_REG, SINGLE_INCREMENT_BIT,
1504 SINGLE_INCREMENT_BIT);
1505 if (rc < 0)
1506 smblib_err(chg, "Couldn't write to CMD_HVDCP_2_REG rc=%d\n",
1507 rc);
1508
1509 return rc;
1510}
1511
1512static int smblib_dm_pulse(struct smb_charger *chg)
1513{
1514 int rc;
1515
1516 /* QC 3.0 decrement */
1517 rc = smblib_masked_write(chg, CMD_HVDCP_2_REG, SINGLE_DECREMENT_BIT,
1518 SINGLE_DECREMENT_BIT);
1519 if (rc < 0)
1520 smblib_err(chg, "Couldn't write to CMD_HVDCP_2_REG rc=%d\n",
1521 rc);
1522
1523 return rc;
1524}
1525
1526int smblib_dp_dm(struct smb_charger *chg, int val)
1527{
1528 int target_icl_ua, rc = 0;
1529 union power_supply_propval pval;
1530
1531 switch (val) {
1532 case POWER_SUPPLY_DP_DM_DP_PULSE:
1533 rc = smblib_dp_pulse(chg);
1534 if (!rc)
1535 chg->pulse_cnt++;
1536 smblib_dbg(chg, PR_PARALLEL, "DP_DM_DP_PULSE rc=%d cnt=%d\n",
1537 rc, chg->pulse_cnt);
1538 break;
1539 case POWER_SUPPLY_DP_DM_DM_PULSE:
1540 rc = smblib_dm_pulse(chg);
1541 if (!rc && chg->pulse_cnt)
1542 chg->pulse_cnt--;
1543 smblib_dbg(chg, PR_PARALLEL, "DP_DM_DM_PULSE rc=%d cnt=%d\n",
1544 rc, chg->pulse_cnt);
1545 break;
1546 case POWER_SUPPLY_DP_DM_ICL_DOWN:
1547 target_icl_ua = get_effective_result(chg->usb_icl_votable);
1548 if (target_icl_ua < 0) {
1549 /* no client vote, get the ICL from charger */
1550 rc = power_supply_get_property(chg->usb_psy,
1551 POWER_SUPPLY_PROP_HW_CURRENT_MAX,
1552 &pval);
1553 if (rc < 0) {
1554 smblib_err(chg, "Couldn't get max curr rc=%d\n",
1555 rc);
1556 return rc;
1557 }
1558 target_icl_ua = pval.intval;
1559 }
1560
1561 /*
1562 * Check if any other voter voted on USB_ICL in case of
1563 * voter other than SW_QC3_VOTER reset and restart reduction
1564 * again.
1565 */
1566 if (target_icl_ua != get_client_vote(chg->usb_icl_votable,
1567 SW_QC3_VOTER))
1568 chg->usb_icl_delta_ua = 0;
1569
1570 chg->usb_icl_delta_ua += 100000;
1571 vote(chg->usb_icl_votable, SW_QC3_VOTER, true,
1572 target_icl_ua - 100000);
1573 smblib_dbg(chg, PR_PARALLEL, "ICL DOWN ICL=%d reduction=%d\n",
1574 target_icl_ua, chg->usb_icl_delta_ua);
1575 break;
1576 case POWER_SUPPLY_DP_DM_ICL_UP:
1577 default:
1578 break;
1579 }
1580
1581 return rc;
1582}
1583
1584int smblib_disable_hw_jeita(struct smb_charger *chg, bool disable)
1585{
1586 int rc;
1587 u8 mask;
1588
1589 /*
1590 * Disable h/w base JEITA compensation if s/w JEITA is enabled
1591 */
1592 mask = JEITA_EN_COLD_SL_FCV_BIT
1593 | JEITA_EN_HOT_SL_FCV_BIT
1594 | JEITA_EN_HOT_SL_CCC_BIT
1595 | JEITA_EN_COLD_SL_CCC_BIT,
1596 rc = smblib_masked_write(chg, JEITA_EN_CFG_REG, mask,
1597 disable ? 0 : mask);
1598 if (rc < 0) {
1599 dev_err(chg->dev, "Couldn't configure s/w jeita rc=%d\n",
1600 rc);
1601 return rc;
1602 }
1603 return 0;
1604}
1605
1606/*******************
1607 * DC PSY GETTERS *
1608 *******************/
1609
1610int smblib_get_prop_dc_present(struct smb_charger *chg,
1611 union power_supply_propval *val)
1612{
1613 int rc;
1614 u8 stat;
1615
1616 rc = smblib_read(chg, DCIN_BASE + INT_RT_STS_OFFSET, &stat);
1617 if (rc < 0) {
1618 smblib_err(chg, "Couldn't read DCIN_RT_STS rc=%d\n", rc);
1619 return rc;
1620 }
1621
1622 val->intval = (bool)(stat & DCIN_PLUGIN_RT_STS_BIT);
1623 return 0;
1624}
1625
1626int smblib_get_prop_dc_online(struct smb_charger *chg,
1627 union power_supply_propval *val)
1628{
1629 int rc = 0;
1630 u8 stat;
1631
1632 if (get_client_vote(chg->dc_suspend_votable, USER_VOTER)) {
1633 val->intval = false;
1634 return rc;
1635 }
1636
1637 rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat);
1638 if (rc < 0) {
1639 smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n",
1640 rc);
1641 return rc;
1642 }
1643 smblib_dbg(chg, PR_REGISTER, "POWER_PATH_STATUS = 0x%02x\n",
1644 stat);
1645
1646 val->intval = (stat & USE_DCIN_BIT) &&
1647 (stat & VALID_INPUT_POWER_SOURCE_STS_BIT);
1648
1649 return rc;
1650}
1651
1652/*******************
1653 * USB PSY GETTERS *
1654 *******************/
1655
1656int smblib_get_prop_usb_present(struct smb_charger *chg,
1657 union power_supply_propval *val)
1658{
1659 int rc;
1660 u8 stat;
1661
1662 rc = smblib_read(chg, USBIN_BASE + INT_RT_STS_OFFSET, &stat);
1663 if (rc < 0) {
1664 smblib_err(chg, "Couldn't read USBIN_RT_STS rc=%d\n", rc);
1665 return rc;
1666 }
1667
1668 val->intval = (bool)(stat & USBIN_PLUGIN_RT_STS_BIT);
1669 return 0;
1670}
1671
1672int smblib_get_prop_usb_online(struct smb_charger *chg,
1673 union power_supply_propval *val)
1674{
1675 int rc = 0;
1676 u8 stat;
1677
1678 if (get_client_vote_locked(chg->usb_icl_votable, USER_VOTER) == 0) {
1679 val->intval = false;
1680 return rc;
1681 }
1682
1683 rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat);
1684 if (rc < 0) {
1685 smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n",
1686 rc);
1687 return rc;
1688 }
1689 smblib_dbg(chg, PR_REGISTER, "POWER_PATH_STATUS = 0x%02x\n",
1690 stat);
1691
1692 val->intval = (stat & USE_USBIN_BIT) &&
1693 (stat & VALID_INPUT_POWER_SOURCE_STS_BIT);
1694 return rc;
1695}
1696
1697int smblib_get_prop_usb_voltage_max(struct smb_charger *chg,
1698 union power_supply_propval *val)
1699{
1700 switch (chg->real_charger_type) {
1701 case POWER_SUPPLY_TYPE_USB_HVDCP:
1702 case POWER_SUPPLY_TYPE_USB_PD:
1703 val->intval = MICRO_12V;
1704 default:
1705 val->intval = MICRO_5V;
1706 break;
1707 }
1708
1709 return 0;
1710}
1711
1712int smblib_get_prop_typec_cc_orientation(struct smb_charger *chg,
1713 union power_supply_propval *val)
1714{
1715 int rc = 0;
1716 u8 stat;
1717
1718 rc = smblib_read(chg, TYPE_C_MISC_STATUS_REG, &stat);
1719 if (rc < 0) {
1720 smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n", rc);
1721 return rc;
1722 }
1723 smblib_dbg(chg, PR_REGISTER, "TYPE_C_STATUS_4 = 0x%02x\n", stat);
1724
1725 if (stat & CC_ATTACHED_BIT)
1726 val->intval = (bool)(stat & CC_ORIENTATION_BIT) + 1;
1727 else
1728 val->intval = 0;
1729
1730 return rc;
1731}
1732
1733static const char * const smblib_typec_mode_name[] = {
1734 [POWER_SUPPLY_TYPEC_NONE] = "NONE",
1735 [POWER_SUPPLY_TYPEC_SOURCE_DEFAULT] = "SOURCE_DEFAULT",
1736 [POWER_SUPPLY_TYPEC_SOURCE_MEDIUM] = "SOURCE_MEDIUM",
1737 [POWER_SUPPLY_TYPEC_SOURCE_HIGH] = "SOURCE_HIGH",
1738 [POWER_SUPPLY_TYPEC_NON_COMPLIANT] = "NON_COMPLIANT",
1739 [POWER_SUPPLY_TYPEC_SINK] = "SINK",
1740 [POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE] = "SINK_POWERED_CABLE",
1741 [POWER_SUPPLY_TYPEC_SINK_DEBUG_ACCESSORY] = "SINK_DEBUG_ACCESSORY",
1742 [POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER] = "SINK_AUDIO_ADAPTER",
1743 [POWER_SUPPLY_TYPEC_POWERED_CABLE_ONLY] = "POWERED_CABLE_ONLY",
1744};
1745
1746static int smblib_get_prop_ufp_mode(struct smb_charger *chg)
1747{
1748 int rc;
1749 u8 stat;
1750
1751 rc = smblib_read(chg, TYPE_C_SNK_STATUS_REG, &stat);
1752 if (rc < 0) {
1753 smblib_err(chg, "Couldn't read TYPE_C_STATUS_1 rc=%d\n", rc);
1754 return POWER_SUPPLY_TYPEC_NONE;
1755 }
1756 smblib_dbg(chg, PR_REGISTER, "TYPE_C_STATUS_1 = 0x%02x\n", stat);
1757
1758 switch (stat & DETECTED_SRC_TYPE_MASK) {
1759 case SNK_RP_STD_BIT:
1760 return POWER_SUPPLY_TYPEC_SOURCE_DEFAULT;
1761 case SNK_RP_1P5_BIT:
1762 return POWER_SUPPLY_TYPEC_SOURCE_MEDIUM;
1763 case SNK_RP_3P0_BIT:
1764 return POWER_SUPPLY_TYPEC_SOURCE_HIGH;
1765 default:
1766 break;
1767 }
1768
1769 return POWER_SUPPLY_TYPEC_NONE;
1770}
1771
1772static int smblib_get_prop_dfp_mode(struct smb_charger *chg)
1773{
1774 int rc;
1775 u8 stat;
1776
1777 rc = smblib_read(chg, TYPE_C_SRC_STATUS_REG, &stat);
1778 if (rc < 0) {
1779 smblib_err(chg, "Couldn't read TYPE_C_SRC_STATUS_REG rc=%d\n",
1780 rc);
1781 return POWER_SUPPLY_TYPEC_NONE;
1782 }
1783 smblib_dbg(chg, PR_REGISTER, "TYPE_C_SRC_STATUS_REG = 0x%02x\n", stat);
1784
1785 switch (stat & DETECTED_SNK_TYPE_MASK) {
1786 case AUDIO_ACCESS_RA_RA_BIT:
1787 return POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER;
1788 case SRC_DEBUG_ACCESS_BIT:
1789 return POWER_SUPPLY_TYPEC_SINK_DEBUG_ACCESSORY;
1790 case SRC_RD_RA_VCONN_BIT:
1791 return POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE;
1792 case SRC_RD_OPEN_BIT:
1793 return POWER_SUPPLY_TYPEC_SINK;
1794 default:
1795 break;
1796 }
1797
1798 return POWER_SUPPLY_TYPEC_NONE;
1799}
1800
1801static int smblib_get_prop_typec_mode(struct smb_charger *chg)
1802{
1803 int rc;
1804 u8 stat;
1805
1806 rc = smblib_read(chg, TYPE_C_MISC_STATUS_REG, &stat);
1807 if (rc < 0) {
1808 smblib_err(chg, "Couldn't read TYPE_C_MISC_STATUS_REG rc=%d\n",
1809 rc);
1810 return 0;
1811 }
1812 smblib_dbg(chg, PR_REGISTER, "TYPE_C_MISC_STATUS_REG = 0x%02x\n", stat);
1813
1814 if (stat & SNK_SRC_MODE_BIT)
1815 return smblib_get_prop_dfp_mode(chg);
1816 else
1817 return smblib_get_prop_ufp_mode(chg);
1818}
1819
1820int smblib_get_prop_typec_power_role(struct smb_charger *chg,
1821 union power_supply_propval *val)
1822{
1823 int rc = 0;
1824 u8 ctrl;
1825
1826 rc = smblib_read(chg, TYPE_C_MODE_CFG_REG, &ctrl);
1827 if (rc < 0) {
1828 smblib_err(chg, "Couldn't read TYPE_C_MODE_CFG_REG rc=%d\n",
1829 rc);
1830 return rc;
1831 }
1832 smblib_dbg(chg, PR_REGISTER, "TYPE_C_MODE_CFG_REG = 0x%02x\n",
1833 ctrl);
1834
1835 if (ctrl & TYPEC_DISABLE_CMD_BIT) {
1836 val->intval = POWER_SUPPLY_TYPEC_PR_NONE;
1837 return rc;
1838 }
1839
1840 switch (ctrl & (EN_SRC_ONLY_BIT | EN_SNK_ONLY_BIT)) {
1841 case 0:
1842 val->intval = POWER_SUPPLY_TYPEC_PR_DUAL;
1843 break;
1844 case EN_SRC_ONLY_BIT:
1845 val->intval = POWER_SUPPLY_TYPEC_PR_SOURCE;
1846 break;
1847 case EN_SNK_ONLY_BIT:
1848 val->intval = POWER_SUPPLY_TYPEC_PR_SINK;
1849 break;
1850 default:
1851 val->intval = POWER_SUPPLY_TYPEC_PR_NONE;
1852 smblib_err(chg, "unsupported power role 0x%02lx\n",
1853 ctrl & (EN_SRC_ONLY_BIT | EN_SNK_ONLY_BIT));
1854 return -EINVAL;
1855 }
1856
1857 return rc;
1858}
1859
1860int smblib_get_prop_pd_allowed(struct smb_charger *chg,
1861 union power_supply_propval *val)
1862{
1863 val->intval = get_effective_result(chg->pd_allowed_votable);
1864 return 0;
1865}
1866
1867int smblib_get_prop_input_current_settled(struct smb_charger *chg,
1868 union power_supply_propval *val)
1869{
1870 return smblib_get_charge_param(chg, &chg->param.icl_stat, &val->intval);
1871}
1872
1873#define HVDCP3_STEP_UV 200000
1874int smblib_get_prop_input_voltage_settled(struct smb_charger *chg,
1875 union power_supply_propval *val)
1876{
1877 int rc, pulses;
1878
1879 switch (chg->real_charger_type) {
1880 case POWER_SUPPLY_TYPE_USB_HVDCP_3:
1881 rc = smblib_get_pulse_cnt(chg, &pulses);
1882 if (rc < 0) {
1883 smblib_err(chg,
1884 "Couldn't read QC_PULSE_COUNT rc=%d\n", rc);
1885 return 0;
1886 }
1887 val->intval = MICRO_5V + HVDCP3_STEP_UV * pulses;
1888 break;
1889 case POWER_SUPPLY_TYPE_USB_PD:
1890 val->intval = chg->voltage_min_uv;
1891 break;
1892 default:
1893 val->intval = MICRO_5V;
1894 break;
1895 }
1896
1897 return 0;
1898}
1899
1900int smblib_get_prop_pd_in_hard_reset(struct smb_charger *chg,
1901 union power_supply_propval *val)
1902{
1903 val->intval = chg->pd_hard_reset;
1904 return 0;
1905}
1906
1907int smblib_get_pe_start(struct smb_charger *chg,
1908 union power_supply_propval *val)
1909{
1910 /*
1911 * hvdcp timeout voter is the last one to allow pd. Use its vote
1912 * to indicate start of pe engine
1913 */
1914 val->intval
1915 = !get_client_vote_locked(chg->pd_disallowed_votable_indirect,
1916 APSD_VOTER);
1917 return 0;
1918}
1919
1920int smblib_get_prop_die_health(struct smb_charger *chg,
1921 union power_supply_propval *val)
1922{
1923 int rc;
1924 u8 stat;
1925
1926 rc = smblib_read(chg, TEMP_RANGE_STATUS_REG, &stat);
1927 if (rc < 0) {
1928 smblib_err(chg, "Couldn't read TEMP_RANGE_STATUS_REG rc=%d\n",
1929 rc);
1930 return rc;
1931 }
1932
1933 /* TEMP_RANGE bits are mutually exclusive */
1934 switch (stat & TEMP_RANGE_MASK) {
1935 case TEMP_BELOW_RANGE_BIT:
1936 val->intval = POWER_SUPPLY_HEALTH_COOL;
1937 break;
1938 case TEMP_WITHIN_RANGE_BIT:
1939 val->intval = POWER_SUPPLY_HEALTH_WARM;
1940 break;
1941 case TEMP_ABOVE_RANGE_BIT:
1942 val->intval = POWER_SUPPLY_HEALTH_HOT;
1943 break;
1944 case ALERT_LEVEL_BIT:
1945 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1946 break;
1947 default:
1948 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
1949 }
1950
1951 return 0;
1952}
1953
1954#define SDP_CURRENT_UA 500000
1955#define CDP_CURRENT_UA 1500000
1956#define DCP_CURRENT_UA 1500000
1957#define HVDCP_CURRENT_UA 3000000
1958#define TYPEC_DEFAULT_CURRENT_UA 900000
1959#define TYPEC_MEDIUM_CURRENT_UA 1500000
1960#define TYPEC_HIGH_CURRENT_UA 3000000
1961static int get_rp_based_dcp_current(struct smb_charger *chg, int typec_mode)
1962{
1963 int rp_ua;
1964
1965 switch (typec_mode) {
1966 case POWER_SUPPLY_TYPEC_SOURCE_HIGH:
1967 rp_ua = TYPEC_HIGH_CURRENT_UA;
1968 break;
1969 case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM:
1970 case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT:
1971 /* fall through */
1972 default:
1973 rp_ua = DCP_CURRENT_UA;
1974 }
1975
1976 return rp_ua;
1977}
1978
1979/*******************
1980 * USB PSY SETTERS *
1981 * *****************/
1982
1983int smblib_set_prop_pd_current_max(struct smb_charger *chg,
1984 const union power_supply_propval *val)
1985{
1986 int rc;
1987
1988 if (chg->pd_active)
1989 rc = vote(chg->usb_icl_votable, PD_VOTER, true, val->intval);
1990 else
1991 rc = -EPERM;
1992
1993 return rc;
1994}
1995
1996static int smblib_handle_usb_current(struct smb_charger *chg,
1997 int usb_current)
1998{
1999 int rc = 0, rp_ua, typec_mode;
2000
2001 if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_FLOAT) {
2002 if (usb_current == -ETIMEDOUT) {
2003 /*
2004 * Valid FLOAT charger, report the current based
2005 * of Rp
2006 */
2007 typec_mode = smblib_get_prop_typec_mode(chg);
2008 rp_ua = get_rp_based_dcp_current(chg, typec_mode);
2009 rc = vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER,
2010 true, rp_ua);
2011 if (rc < 0)
2012 return rc;
2013 } else {
2014 /*
2015 * FLOAT charger detected as SDP by USB driver,
2016 * charge with the requested current and update the
2017 * real_charger_type
2018 */
2019 chg->real_charger_type = POWER_SUPPLY_TYPE_USB;
2020 rc = vote(chg->usb_icl_votable, USB_PSY_VOTER,
2021 true, usb_current);
2022 if (rc < 0)
2023 return rc;
2024 rc = vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER,
2025 false, 0);
2026 if (rc < 0)
2027 return rc;
2028 }
2029 } else {
2030 rc = vote(chg->usb_icl_votable, USB_PSY_VOTER,
2031 true, usb_current);
2032 }
2033
2034 return rc;
2035}
2036
2037int smblib_set_prop_sdp_current_max(struct smb_charger *chg,
2038 const union power_supply_propval *val)
2039{
2040 int rc = 0;
2041
2042 if (!chg->pd_active) {
2043 rc = smblib_handle_usb_current(chg, val->intval);
2044 } else if (chg->system_suspend_supported) {
2045 if (val->intval <= USBIN_25MA)
2046 rc = vote(chg->usb_icl_votable,
2047 PD_SUSPEND_SUPPORTED_VOTER, true, val->intval);
2048 else
2049 rc = vote(chg->usb_icl_votable,
2050 PD_SUSPEND_SUPPORTED_VOTER, false, 0);
2051 }
2052 return rc;
2053}
2054
2055int smblib_set_prop_boost_current(struct smb_charger *chg,
2056 const union power_supply_propval *val)
2057{
2058 int rc = 0;
2059
2060 rc = smblib_set_charge_param(chg, &chg->param.freq_switcher,
2061 val->intval <= chg->boost_threshold_ua ?
2062 chg->chg_freq.freq_below_otg_threshold :
2063 chg->chg_freq.freq_above_otg_threshold);
2064 if (rc < 0) {
2065 dev_err(chg->dev, "Error in setting freq_boost rc=%d\n", rc);
2066 return rc;
2067 }
2068
2069 chg->boost_current_ua = val->intval;
2070 return rc;
2071}
2072
2073int smblib_set_prop_typec_power_role(struct smb_charger *chg,
2074 const union power_supply_propval *val)
2075{
2076 int rc = 0;
2077 u8 power_role;
2078
2079 switch (val->intval) {
2080 case POWER_SUPPLY_TYPEC_PR_NONE:
2081 power_role = TYPEC_DISABLE_CMD_BIT;
2082 break;
2083 case POWER_SUPPLY_TYPEC_PR_DUAL:
2084 power_role = 0;
2085 break;
2086 case POWER_SUPPLY_TYPEC_PR_SINK:
2087 power_role = EN_SNK_ONLY_BIT;
2088 break;
2089 case POWER_SUPPLY_TYPEC_PR_SOURCE:
2090 power_role = EN_SRC_ONLY_BIT;
2091 break;
2092 default:
2093 smblib_err(chg, "power role %d not supported\n", val->intval);
2094 return -EINVAL;
2095 }
2096
2097 rc = smblib_masked_write(chg, TYPE_C_MODE_CFG_REG,
2098 TYPEC_POWER_ROLE_CMD_MASK, power_role);
2099 if (rc < 0) {
2100 smblib_err(chg, "Couldn't write 0x%02x to TYPE_C_INTRPT_ENB_SOFTWARE_CTRL rc=%d\n",
2101 power_role, rc);
2102 return rc;
2103 }
2104
2105 return rc;
2106}
2107
2108int smblib_set_prop_pd_voltage_min(struct smb_charger *chg,
2109 const union power_supply_propval *val)
2110{
2111 int rc, min_uv;
2112
2113 min_uv = min(val->intval, chg->voltage_max_uv);
2114 rc = smblib_set_usb_pd_allowed_voltage(chg, min_uv,
2115 chg->voltage_max_uv);
2116 if (rc < 0) {
2117 smblib_err(chg, "invalid max voltage %duV rc=%d\n",
2118 val->intval, rc);
2119 return rc;
2120 }
2121
2122 chg->voltage_min_uv = min_uv;
2123 power_supply_changed(chg->usb_main_psy);
2124
2125 return rc;
2126}
2127
2128int smblib_set_prop_pd_voltage_max(struct smb_charger *chg,
2129 const union power_supply_propval *val)
2130{
2131 int rc, max_uv;
2132
2133 max_uv = max(val->intval, chg->voltage_min_uv);
2134 rc = smblib_set_usb_pd_allowed_voltage(chg, chg->voltage_min_uv,
2135 max_uv);
2136 if (rc < 0) {
2137 smblib_err(chg, "invalid min voltage %duV rc=%d\n",
2138 val->intval, rc);
2139 return rc;
2140 }
2141
2142 chg->voltage_max_uv = max_uv;
2143 power_supply_changed(chg->usb_main_psy);
2144
2145 return rc;
2146}
2147
2148static int __smblib_set_prop_pd_active(struct smb_charger *chg, bool pd_active)
2149{
2150 int rc = 0;
2151
2152 chg->pd_active = pd_active;
2153
2154 if (chg->pd_active) {
2155 vote(chg->pd_allowed_votable, PD_VOTER, true, 0);
2156 vote(chg->usb_irq_enable_votable, PD_VOTER, true, 0);
2157
2158 /*
2159 * Enforce 500mA for PD until the real vote comes in later.
2160 * It is guaranteed that pd_active is set prior to
2161 * pd_current_max
2162 */
2163 rc = vote(chg->usb_icl_votable, PD_VOTER, true, USBIN_500MA);
2164 if (rc < 0)
2165 smblib_err(chg, "Couldn't vote for USB ICL rc=%d\n",
2166 rc);
2167
2168 /* since PD was found the cable must be non-legacy */
2169 vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, false, 0);
2170
2171 /* clear USB ICL vote for DCP_VOTER */
2172 rc = vote(chg->usb_icl_votable, DCP_VOTER, false, 0);
2173 if (rc < 0)
2174 smblib_err(chg, "Couldn't un-vote DCP from USB ICL rc=%d\n",
2175 rc);
2176
2177 /* remove USB_PSY_VOTER */
2178 rc = vote(chg->usb_icl_votable, USB_PSY_VOTER, false, 0);
2179 if (rc < 0)
2180 smblib_err(chg, "Couldn't unvote USB_PSY rc=%d\n", rc);
2181 } else {
2182 vote(chg->pd_allowed_votable, PD_VOTER, true, 0);
2183 vote(chg->usb_irq_enable_votable, PD_VOTER, false, 0);
2184 }
2185
2186 smblib_update_usb_type(chg);
2187 power_supply_changed(chg->usb_psy);
2188 return rc;
2189}
2190
2191int smblib_set_prop_pd_active(struct smb_charger *chg,
2192 const union power_supply_propval *val)
2193{
2194 if (!get_effective_result(chg->pd_allowed_votable))
2195 return -EINVAL;
2196
2197 return __smblib_set_prop_pd_active(chg, val->intval);
2198}
2199
2200int smblib_set_prop_ship_mode(struct smb_charger *chg,
2201 const union power_supply_propval *val)
2202{
2203 int rc;
2204
2205 smblib_dbg(chg, PR_MISC, "Set ship mode: %d!!\n", !!val->intval);
2206
2207 rc = smblib_masked_write(chg, SHIP_MODE_REG, SHIP_MODE_EN_BIT,
2208 !!val->intval ? SHIP_MODE_EN_BIT : 0);
2209 if (rc < 0)
2210 dev_err(chg->dev, "Couldn't %s ship mode, rc=%d\n",
2211 !!val->intval ? "enable" : "disable", rc);
2212
2213 return rc;
2214}
2215
2216int smblib_set_prop_pd_in_hard_reset(struct smb_charger *chg,
2217 const union power_supply_propval *val)
2218{
2219 int rc = 0;
2220
2221 if (chg->pd_hard_reset == val->intval)
2222 return rc;
2223
2224 chg->pd_hard_reset = val->intval;
2225 rc = smblib_masked_write(chg, TYPE_C_EXIT_STATE_CFG_REG,
2226 EXIT_SNK_BASED_ON_CC_BIT,
2227 (chg->pd_hard_reset) ? EXIT_SNK_BASED_ON_CC_BIT : 0);
2228 if (rc < 0)
2229 smblib_err(chg, "Couldn't set EXIT_SNK_BASED_ON_CC rc=%d\n",
2230 rc);
2231
2232 return rc;
2233}
2234
2235static int smblib_recover_from_soft_jeita(struct smb_charger *chg)
2236{
2237 u8 stat1, stat7;
2238 int rc;
2239
2240 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat1);
2241 if (rc < 0) {
2242 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
2243 rc);
2244 return rc;
2245 }
2246
2247 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_7_REG, &stat7);
2248 if (rc < 0) {
2249 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_2 rc=%d\n",
2250 rc);
2251 return rc;
2252 }
2253
2254 if ((chg->jeita_status && !(stat7 & BAT_TEMP_STATUS_SOFT_LIMIT_MASK) &&
2255 ((stat1 & BATTERY_CHARGER_STATUS_MASK) == TERMINATE_CHARGE))) {
2256 /*
2257 * We are moving from JEITA soft -> Normal and charging
2258 * is terminated
2259 */
2260 rc = smblib_write(chg, CHARGING_ENABLE_CMD_REG, 0);
2261 if (rc < 0) {
2262 smblib_err(chg, "Couldn't disable charging rc=%d\n",
2263 rc);
2264 return rc;
2265 }
2266 rc = smblib_write(chg, CHARGING_ENABLE_CMD_REG,
2267 CHARGING_ENABLE_CMD_BIT);
2268 if (rc < 0) {
2269 smblib_err(chg, "Couldn't enable charging rc=%d\n",
2270 rc);
2271 return rc;
2272 }
2273 }
2274
2275 chg->jeita_status = stat7 & BAT_TEMP_STATUS_SOFT_LIMIT_MASK;
2276
2277 return 0;
2278}
2279
2280/************************
2281 * USB MAIN PSY GETTERS *
2282 ************************/
2283int smblib_get_prop_fcc_delta(struct smb_charger *chg,
2284 union power_supply_propval *val)
2285{
2286 int rc, jeita_cc_delta_ua = 0;
2287
2288 if (chg->sw_jeita_enabled) {
2289 val->intval = 0;
2290 return 0;
2291 }
2292
2293 rc = smblib_get_jeita_cc_delta(chg, &jeita_cc_delta_ua);
2294 if (rc < 0) {
2295 smblib_err(chg, "Couldn't get jeita cc delta rc=%d\n", rc);
2296 jeita_cc_delta_ua = 0;
2297 }
2298
2299 val->intval = jeita_cc_delta_ua;
2300 return 0;
2301}
2302
2303/************************
2304 * USB MAIN PSY SETTERS *
2305 ************************/
2306int smblib_get_charge_current(struct smb_charger *chg,
2307 int *total_current_ua)
2308{
2309 const struct apsd_result *apsd_result = smblib_get_apsd_result(chg);
2310 union power_supply_propval val = {0, };
2311 int rc = 0, typec_source_rd, current_ua;
2312 bool non_compliant;
2313 u8 stat;
2314
2315 if (chg->pd_active) {
2316 *total_current_ua =
2317 get_client_vote_locked(chg->usb_icl_votable, PD_VOTER);
2318 return rc;
2319 }
2320
2321 rc = smblib_read(chg, LEGACY_CABLE_STATUS_REG, &stat);
2322 if (rc < 0) {
2323 smblib_err(chg, "Couldn't read TYPE_C_STATUS_5 rc=%d\n", rc);
2324 return rc;
2325 }
2326 non_compliant = stat & TYPEC_NONCOMP_LEGACY_CABLE_STATUS_BIT;
2327
2328 /* get settled ICL */
2329 rc = smblib_get_prop_input_current_settled(chg, &val);
2330 if (rc < 0) {
2331 smblib_err(chg, "Couldn't get settled ICL rc=%d\n", rc);
2332 return rc;
2333 }
2334
2335 typec_source_rd = smblib_get_prop_ufp_mode(chg);
2336
2337 /* QC 2.0/3.0 adapter */
2338 if (apsd_result->bit & (QC_3P0_BIT | QC_2P0_BIT)) {
2339 *total_current_ua = HVDCP_CURRENT_UA;
2340 return 0;
2341 }
2342
2343 if (non_compliant) {
2344 switch (apsd_result->bit) {
2345 case CDP_CHARGER_BIT:
2346 current_ua = CDP_CURRENT_UA;
2347 break;
2348 case DCP_CHARGER_BIT:
2349 case OCP_CHARGER_BIT:
2350 case FLOAT_CHARGER_BIT:
2351 current_ua = DCP_CURRENT_UA;
2352 break;
2353 default:
2354 current_ua = 0;
2355 break;
2356 }
2357
2358 *total_current_ua = max(current_ua, val.intval);
2359 return 0;
2360 }
2361
2362 switch (typec_source_rd) {
2363 case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT:
2364 switch (apsd_result->bit) {
2365 case CDP_CHARGER_BIT:
2366 current_ua = CDP_CURRENT_UA;
2367 break;
2368 case DCP_CHARGER_BIT:
2369 case OCP_CHARGER_BIT:
2370 case FLOAT_CHARGER_BIT:
2371 current_ua = chg->default_icl_ua;
2372 break;
2373 default:
2374 current_ua = 0;
2375 break;
2376 }
2377 break;
2378 case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM:
2379 current_ua = TYPEC_MEDIUM_CURRENT_UA;
2380 break;
2381 case POWER_SUPPLY_TYPEC_SOURCE_HIGH:
2382 current_ua = TYPEC_HIGH_CURRENT_UA;
2383 break;
2384 case POWER_SUPPLY_TYPEC_NON_COMPLIANT:
2385 case POWER_SUPPLY_TYPEC_NONE:
2386 default:
2387 current_ua = 0;
2388 break;
2389 }
2390
2391 *total_current_ua = max(current_ua, val.intval);
2392 return 0;
2393}
2394
2395/**********************
2396 * INTERRUPT HANDLERS *
2397 **********************/
2398
2399irqreturn_t default_irq_handler(int irq, void *data)
2400{
2401 struct smb_irq_data *irq_data = data;
2402 struct smb_charger *chg = irq_data->parent_data;
2403
2404 smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
2405 return IRQ_HANDLED;
2406}
2407
2408irqreturn_t chg_state_change_irq_handler(int irq, void *data)
2409{
2410 struct smb_irq_data *irq_data = data;
2411 struct smb_charger *chg = irq_data->parent_data;
2412 u8 stat;
2413 int rc;
2414
2415 smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
2416
2417 rc = smblib_read(chg, BATTERY_CHARGER_STATUS_1_REG, &stat);
2418 if (rc < 0) {
2419 smblib_err(chg, "Couldn't read BATTERY_CHARGER_STATUS_1 rc=%d\n",
2420 rc);
2421 return IRQ_HANDLED;
2422 }
2423
2424 stat = stat & BATTERY_CHARGER_STATUS_MASK;
2425 power_supply_changed(chg->batt_psy);
2426 return IRQ_HANDLED;
2427}
2428
2429irqreturn_t batt_temp_changed_irq_handler(int irq, void *data)
2430{
2431 struct smb_irq_data *irq_data = data;
2432 struct smb_charger *chg = irq_data->parent_data;
2433 int rc;
2434
2435 rc = smblib_recover_from_soft_jeita(chg);
2436 if (rc < 0) {
2437 smblib_err(chg, "Couldn't recover chg from soft jeita rc=%d\n",
2438 rc);
2439 return IRQ_HANDLED;
2440 }
2441
2442 rerun_election(chg->fcc_votable);
2443 power_supply_changed(chg->batt_psy);
2444 return IRQ_HANDLED;
2445}
2446
2447irqreturn_t batt_psy_changed_irq_handler(int irq, void *data)
2448{
2449 struct smb_irq_data *irq_data = data;
2450 struct smb_charger *chg = irq_data->parent_data;
2451
2452 smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
2453 power_supply_changed(chg->batt_psy);
2454 return IRQ_HANDLED;
2455}
2456
2457irqreturn_t usbin_uv_irq_handler(int irq, void *data)
2458{
2459 struct smb_irq_data *irq_data = data;
2460 struct smb_charger *chg = irq_data->parent_data;
2461 struct storm_watch *wdata;
2462
2463 smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
2464 if (!chg->irq_info[SWITCHER_POWER_OK_IRQ].irq_data)
2465 return IRQ_HANDLED;
2466
2467 wdata = &chg->irq_info[SWITCHER_POWER_OK_IRQ].irq_data->storm_data;
2468 reset_storm_count(wdata);
2469 return IRQ_HANDLED;
2470}
2471
2472#define USB_WEAK_INPUT_UA 1400000
2473#define ICL_CHANGE_DELAY_MS 1000
2474irqreturn_t icl_change_irq_handler(int irq, void *data)
2475{
2476 u8 stat;
2477 int rc, settled_ua, delay = ICL_CHANGE_DELAY_MS;
2478 struct smb_irq_data *irq_data = data;
2479 struct smb_charger *chg = irq_data->parent_data;
2480
2481 if (chg->mode == PARALLEL_MASTER) {
2482 rc = smblib_read(chg, AICL_STATUS_REG, &stat);
2483 if (rc < 0) {
2484 smblib_err(chg, "Couldn't read AICL_STATUS rc=%d\n",
2485 rc);
2486 return IRQ_HANDLED;
2487 }
2488
2489 rc = smblib_get_charge_param(chg, &chg->param.icl_stat,
2490 &settled_ua);
2491 if (rc < 0) {
2492 smblib_err(chg, "Couldn't get ICL status rc=%d\n", rc);
2493 return IRQ_HANDLED;
2494 }
2495
2496 /* If AICL settled then schedule work now */
2497 if (settled_ua == get_effective_result(chg->usb_icl_votable))
2498 delay = 0;
2499
2500 cancel_delayed_work_sync(&chg->icl_change_work);
2501 schedule_delayed_work(&chg->icl_change_work,
2502 msecs_to_jiffies(delay));
2503 }
2504
2505 return IRQ_HANDLED;
2506}
2507
2508static void smblib_micro_usb_plugin(struct smb_charger *chg, bool vbus_rising)
2509{
2510 if (vbus_rising) {
2511 /* use the typec flag even though its not typec */
2512 chg->typec_present = 1;
2513 } else {
2514 chg->typec_present = 0;
2515 smblib_update_usb_type(chg);
2516 extcon_set_state_sync(chg->extcon, EXTCON_USB, false);
2517 smblib_uusb_removal(chg);
2518 }
2519}
2520
2521void smblib_usb_plugin_hard_reset_locked(struct smb_charger *chg)
2522{
2523 int rc;
2524 u8 stat;
2525 bool vbus_rising;
2526 struct smb_irq_data *data;
2527 struct storm_watch *wdata;
2528
2529 rc = smblib_read(chg, USBIN_BASE + INT_RT_STS_OFFSET, &stat);
2530 if (rc < 0) {
2531 smblib_err(chg, "Couldn't read USB_INT_RT_STS rc=%d\n", rc);
2532 return;
2533 }
2534
2535 vbus_rising = (bool)(stat & USBIN_PLUGIN_RT_STS_BIT);
2536
2537 if (!vbus_rising) {
2538 if (chg->wa_flags & BOOST_BACK_WA) {
2539 data = chg->irq_info[SWITCHER_POWER_OK_IRQ].irq_data;
2540 if (data) {
2541 wdata = &data->storm_data;
2542 update_storm_count(wdata,
2543 WEAK_CHG_STORM_COUNT);
2544 vote(chg->usb_icl_votable, BOOST_BACK_VOTER,
2545 false, 0);
2546 vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
2547 false, 0);
2548 }
2549 }
2550 }
2551
2552 power_supply_changed(chg->usb_psy);
2553 smblib_dbg(chg, PR_INTERRUPT, "IRQ: usbin-plugin %s\n",
2554 vbus_rising ? "attached" : "detached");
2555}
2556
2557#define PL_DELAY_MS 30000
2558void smblib_usb_plugin_locked(struct smb_charger *chg)
2559{
2560 int rc;
2561 u8 stat;
2562 bool vbus_rising;
2563 struct smb_irq_data *data;
2564 struct storm_watch *wdata;
2565
2566 rc = smblib_read(chg, USBIN_BASE + INT_RT_STS_OFFSET, &stat);
2567 if (rc < 0) {
2568 smblib_err(chg, "Couldn't read USB_INT_RT_STS rc=%d\n", rc);
2569 return;
2570 }
2571
2572 vbus_rising = (bool)(stat & USBIN_PLUGIN_RT_STS_BIT);
2573 smblib_set_opt_switcher_freq(chg, vbus_rising ? chg->chg_freq.freq_5V :
2574 chg->chg_freq.freq_removal);
2575
2576 if (vbus_rising) {
2577 rc = smblib_request_dpdm(chg, true);
2578 if (rc < 0)
2579 smblib_err(chg, "Couldn't to enable DPDM rc=%d\n", rc);
2580
2581 /* Schedule work to enable parallel charger */
2582 vote(chg->awake_votable, PL_DELAY_VOTER, true, 0);
2583 schedule_delayed_work(&chg->pl_enable_work,
2584 msecs_to_jiffies(PL_DELAY_MS));
2585 } else {
2586 if (chg->wa_flags & BOOST_BACK_WA) {
2587 data = chg->irq_info[SWITCHER_POWER_OK_IRQ].irq_data;
2588 if (data) {
2589 wdata = &data->storm_data;
2590 update_storm_count(wdata,
2591 WEAK_CHG_STORM_COUNT);
2592 vote(chg->usb_icl_votable, BOOST_BACK_VOTER,
2593 false, 0);
2594 vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
2595 false, 0);
2596 }
2597 }
2598
2599 rc = smblib_request_dpdm(chg, false);
2600 if (rc < 0)
2601 smblib_err(chg, "Couldn't disable DPDM rc=%d\n", rc);
2602 }
2603
2604 if (chg->micro_usb_mode)
2605 smblib_micro_usb_plugin(chg, vbus_rising);
2606
2607 power_supply_changed(chg->usb_psy);
2608 smblib_dbg(chg, PR_INTERRUPT, "IRQ: usbin-plugin %s\n",
2609 vbus_rising ? "attached" : "detached");
2610}
2611
2612irqreturn_t usb_plugin_irq_handler(int irq, void *data)
2613{
2614 struct smb_irq_data *irq_data = data;
2615 struct smb_charger *chg = irq_data->parent_data;
2616
2617 mutex_lock(&chg->lock);
2618 if (chg->pd_hard_reset)
2619 smblib_usb_plugin_hard_reset_locked(chg);
2620 else
2621 smblib_usb_plugin_locked(chg);
2622 mutex_unlock(&chg->lock);
2623
2624 return IRQ_HANDLED;
2625}
2626
2627static void smblib_handle_slow_plugin_timeout(struct smb_charger *chg,
2628 bool rising)
2629{
2630 smblib_dbg(chg, PR_INTERRUPT, "IRQ: slow-plugin-timeout %s\n",
2631 rising ? "rising" : "falling");
2632}
2633
2634static void smblib_handle_sdp_enumeration_done(struct smb_charger *chg,
2635 bool rising)
2636{
2637 smblib_dbg(chg, PR_INTERRUPT, "IRQ: sdp-enumeration-done %s\n",
2638 rising ? "rising" : "falling");
2639}
2640
2641#define QC3_PULSES_FOR_6V 5
2642#define QC3_PULSES_FOR_9V 20
2643#define QC3_PULSES_FOR_12V 35
2644static void smblib_hvdcp_adaptive_voltage_change(struct smb_charger *chg)
2645{
2646 int rc;
2647 u8 stat;
2648 int pulses;
2649
2650 power_supply_changed(chg->usb_main_psy);
2651 if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_HVDCP) {
2652 rc = smblib_read(chg, QC_CHANGE_STATUS_REG, &stat);
2653 if (rc < 0) {
2654 smblib_err(chg,
2655 "Couldn't read QC_CHANGE_STATUS rc=%d\n", rc);
2656 return;
2657 }
2658
2659 switch (stat & QC_2P0_STATUS_MASK) {
2660 case QC_5V_BIT:
2661 smblib_set_opt_switcher_freq(chg,
2662 chg->chg_freq.freq_5V);
2663 break;
2664 case QC_9V_BIT:
2665 smblib_set_opt_switcher_freq(chg,
2666 chg->chg_freq.freq_9V);
2667 break;
2668 case QC_12V_BIT:
2669 smblib_set_opt_switcher_freq(chg,
2670 chg->chg_freq.freq_12V);
2671 break;
2672 default:
2673 smblib_set_opt_switcher_freq(chg,
2674 chg->chg_freq.freq_removal);
2675 break;
2676 }
2677 }
2678
2679 if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB_HVDCP_3) {
2680 rc = smblib_get_pulse_cnt(chg, &pulses);
2681 if (rc < 0) {
2682 smblib_err(chg,
2683 "Couldn't read QC_PULSE_COUNT rc=%d\n", rc);
2684 return;
2685 }
2686
2687 if (pulses < QC3_PULSES_FOR_6V)
2688 smblib_set_opt_switcher_freq(chg,
2689 chg->chg_freq.freq_5V);
2690 else if (pulses < QC3_PULSES_FOR_9V)
2691 smblib_set_opt_switcher_freq(chg,
2692 chg->chg_freq.freq_6V_8V);
2693 else if (pulses < QC3_PULSES_FOR_12V)
2694 smblib_set_opt_switcher_freq(chg,
2695 chg->chg_freq.freq_9V);
2696 else
2697 smblib_set_opt_switcher_freq(chg,
2698 chg->chg_freq.freq_12V);
2699 }
2700}
2701
2702/* triggers when HVDCP 3.0 authentication has finished */
2703static void smblib_handle_hvdcp_3p0_auth_done(struct smb_charger *chg,
2704 bool rising)
2705{
2706 const struct apsd_result *apsd_result;
2707
2708 if (!rising)
2709 return;
2710
2711 vote(chg->pd_disallowed_votable_indirect, APSD_VOTER, false, 0);
2712
2713 if (chg->mode == PARALLEL_MASTER)
2714 vote(chg->pl_enable_votable_indirect, USBIN_V_VOTER, true, 0);
2715
2716 /* the APSD done handler will set the USB supply type */
2717 apsd_result = smblib_get_apsd_result(chg);
2718 smblib_dbg(chg, PR_INTERRUPT, "IRQ: hvdcp-3p0-auth-done rising; %s detected\n",
2719 apsd_result->name);
2720}
2721
2722static void smblib_handle_hvdcp_check_timeout(struct smb_charger *chg,
2723 bool rising, bool qc_charger)
2724{
2725 const struct apsd_result *apsd_result = smblib_get_apsd_result(chg);
2726
2727 /* Hold off PD only until hvdcp 2.0 detection timeout */
2728 if (rising) {
2729
2730 /* enable HDC and ICL irq for QC2/3 charger */
2731 if (qc_charger)
2732 vote(chg->usb_irq_enable_votable, QC_VOTER, true, 0);
2733 else
2734 vote(chg->pd_disallowed_votable_indirect, APSD_VOTER,
2735 false, 0);
2736
2737 /*
2738 * HVDCP detection timeout done
2739 * If adapter is not QC2.0/QC3.0 - it is a plain old DCP.
2740 */
2741 if (!qc_charger && (apsd_result->bit & DCP_CHARGER_BIT))
2742 /* enforce DCP ICL if specified */
2743 vote(chg->usb_icl_votable, DCP_VOTER,
2744 chg->dcp_icl_ua != -EINVAL, chg->dcp_icl_ua);
2745
2746 /*
2747 * if pd is not allowed, then set pd_active = false right here,
2748 * so that it starts the hvdcp engine
2749 */
2750 if (!get_effective_result(chg->pd_allowed_votable))
2751 __smblib_set_prop_pd_active(chg, 0);
2752 }
2753
2754 smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s %s\n", __func__,
2755 rising ? "rising" : "falling");
2756}
2757
2758/* triggers when HVDCP is detected */
2759static void smblib_handle_hvdcp_detect_done(struct smb_charger *chg,
2760 bool rising)
2761{
2762 smblib_dbg(chg, PR_INTERRUPT, "IRQ: hvdcp-detect-done %s\n",
2763 rising ? "rising" : "falling");
2764}
2765
2766static void smblib_notify_extcon_props(struct smb_charger *chg, int id)
2767{
2768 union extcon_property_value val;
2769 union power_supply_propval prop_val;
2770
2771 smblib_get_prop_typec_cc_orientation(chg, &prop_val);
2772 val.intval = ((prop_val.intval == 2) ? 1 : 0);
2773 extcon_set_property(chg->extcon, id,
2774 EXTCON_PROP_USB_TYPEC_POLARITY, val);
2775
2776 val.intval = true;
2777 extcon_set_property(chg->extcon, id,
2778 EXTCON_PROP_USB_SS, val);
2779}
2780
2781static void smblib_notify_device_mode(struct smb_charger *chg, bool enable)
2782{
2783 if (enable)
2784 smblib_notify_extcon_props(chg, EXTCON_USB);
2785
2786 extcon_set_state_sync(chg->extcon, EXTCON_USB, enable);
2787}
2788
2789static void smblib_notify_usb_host(struct smb_charger *chg, bool enable)
2790{
2791 if (enable)
2792 smblib_notify_extcon_props(chg, EXTCON_USB_HOST);
2793
2794 extcon_set_state_sync(chg->extcon, EXTCON_USB_HOST, enable);
2795}
2796
2797static void smblib_handle_apsd_done(struct smb_charger *chg, bool rising)
2798{
2799 const struct apsd_result *apsd_result;
2800
2801 if (!rising)
2802 return;
2803
2804 apsd_result = smblib_update_usb_type(chg);
2805
2806 switch (apsd_result->bit) {
2807 case SDP_CHARGER_BIT:
2808 case CDP_CHARGER_BIT:
2809 if (chg->micro_usb_mode)
2810 extcon_set_state_sync(chg->extcon, EXTCON_USB,
2811 true);
2812 /* if not DCP then no hvdcp timeout happens. Enable pd here */
2813 vote(chg->pd_disallowed_votable_indirect, APSD_VOTER,
2814 false, 0);
2815 if (chg->use_extcon)
2816 smblib_notify_device_mode(chg, true);
2817 break;
2818 case OCP_CHARGER_BIT:
2819 case FLOAT_CHARGER_BIT:
2820 /* if not DCP then no hvdcp timeout happens, Enable pd here. */
2821 vote(chg->pd_disallowed_votable_indirect, APSD_VOTER,
2822 false, 0);
2823 break;
2824 case DCP_CHARGER_BIT:
2825 break;
2826 default:
2827 break;
2828 }
2829
2830 smblib_dbg(chg, PR_INTERRUPT, "IRQ: apsd-done rising; %s detected\n",
2831 apsd_result->name);
2832}
2833
2834irqreturn_t usb_source_change_irq_handler(int irq, void *data)
2835{
2836 struct smb_irq_data *irq_data = data;
2837 struct smb_charger *chg = irq_data->parent_data;
2838 int rc = 0;
2839 u8 stat;
2840
2841 rc = smblib_read(chg, APSD_STATUS_REG, &stat);
2842 if (rc < 0) {
2843 smblib_err(chg, "Couldn't read APSD_STATUS rc=%d\n", rc);
2844 return IRQ_HANDLED;
2845 }
2846 smblib_dbg(chg, PR_REGISTER, "APSD_STATUS = 0x%02x\n", stat);
2847
2848 if (chg->micro_usb_mode && (stat & APSD_DTC_STATUS_DONE_BIT)
2849 && !chg->uusb_apsd_rerun_done) {
2850 /*
2851 * Force re-run APSD to handle slow insertion related
2852 * charger-mis-detection.
2853 */
2854 chg->uusb_apsd_rerun_done = true;
2855 smblib_rerun_apsd(chg);
2856 return IRQ_HANDLED;
2857 }
2858
2859 smblib_handle_apsd_done(chg,
2860 (bool)(stat & APSD_DTC_STATUS_DONE_BIT));
2861
2862 smblib_handle_hvdcp_detect_done(chg,
2863 (bool)(stat & QC_CHARGER_BIT));
2864
2865 smblib_handle_hvdcp_check_timeout(chg,
2866 (bool)(stat & HVDCP_CHECK_TIMEOUT_BIT),
2867 (bool)(stat & QC_CHARGER_BIT));
2868
2869 smblib_handle_hvdcp_3p0_auth_done(chg,
2870 (bool)(stat & QC_AUTH_DONE_STATUS_BIT));
2871
2872 smblib_handle_sdp_enumeration_done(chg,
2873 (bool)(stat & ENUMERATION_DONE_BIT));
2874
2875 smblib_handle_slow_plugin_timeout(chg,
2876 (bool)(stat & SLOW_PLUGIN_TIMEOUT_BIT));
2877
2878 smblib_hvdcp_adaptive_voltage_change(chg);
2879
2880 power_supply_changed(chg->usb_psy);
2881
2882 rc = smblib_read(chg, APSD_STATUS_REG, &stat);
2883 if (rc < 0) {
2884 smblib_err(chg, "Couldn't read APSD_STATUS rc=%d\n", rc);
2885 return IRQ_HANDLED;
2886 }
2887 smblib_dbg(chg, PR_REGISTER, "APSD_STATUS = 0x%02x\n", stat);
2888
2889 return IRQ_HANDLED;
2890}
2891
2892static void typec_sink_insertion(struct smb_charger *chg)
2893{
2894 /* when a sink is inserted we should not wait on hvdcp timeout to
2895 * enable pd
2896 */
2897 vote(chg->pd_disallowed_votable_indirect, APSD_VOTER, false, 0);
2898 if (chg->use_extcon) {
2899 smblib_notify_usb_host(chg, true);
2900 chg->otg_present = true;
2901 }
2902}
2903
2904static void typec_sink_removal(struct smb_charger *chg)
2905{
2906 smblib_set_charge_param(chg, &chg->param.freq_switcher,
2907 chg->chg_freq.freq_above_otg_threshold);
2908 chg->boost_current_ua = 0;
2909}
2910
2911static void smblib_handle_typec_removal(struct smb_charger *chg)
2912{
2913 int rc;
2914 struct smb_irq_data *data;
2915 struct storm_watch *wdata;
2916
2917 rc = smblib_request_dpdm(chg, false);
2918 if (rc < 0)
2919 smblib_err(chg, "Couldn't disable DPDM rc=%d\n", rc);
2920
2921 if (chg->wa_flags & BOOST_BACK_WA) {
2922 data = chg->irq_info[SWITCHER_POWER_OK_IRQ].irq_data;
2923 if (data) {
2924 wdata = &data->storm_data;
2925 update_storm_count(wdata, WEAK_CHG_STORM_COUNT);
2926 vote(chg->usb_icl_votable, BOOST_BACK_VOTER, false, 0);
2927 vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
2928 false, 0);
2929 }
2930 }
2931
2932 cancel_delayed_work_sync(&chg->pl_enable_work);
2933
2934 /* reset input current limit voters */
2935 vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, 100000);
2936 vote(chg->usb_icl_votable, PD_VOTER, false, 0);
2937 vote(chg->usb_icl_votable, USB_PSY_VOTER, false, 0);
2938 vote(chg->usb_icl_votable, DCP_VOTER, false, 0);
2939 vote(chg->usb_icl_votable, PL_USBIN_USBIN_VOTER, false, 0);
2940 vote(chg->usb_icl_votable, SW_QC3_VOTER, false, 0);
2941 vote(chg->usb_icl_votable, OTG_VOTER, false, 0);
2942 vote(chg->usb_icl_votable, CTM_VOTER, false, 0);
2943
2944 /* reset power delivery voters */
2945 vote(chg->pd_allowed_votable, PD_VOTER, false, 0);
2946 vote(chg->pd_disallowed_votable_indirect, CC_DETACHED_VOTER, true, 0);
2947 vote(chg->pd_disallowed_votable_indirect, APSD_VOTER, true, 0);
2948
2949 /* reset usb irq voters */
2950 vote(chg->usb_irq_enable_votable, PD_VOTER, false, 0);
2951 vote(chg->usb_irq_enable_votable, QC_VOTER, false, 0);
2952
2953 /* reset parallel voters */
2954 vote(chg->pl_disable_votable, PL_DELAY_VOTER, true, 0);
2955 vote(chg->pl_disable_votable, PL_FCC_LOW_VOTER, false, 0);
2956 vote(chg->pl_enable_votable_indirect, USBIN_I_VOTER, false, 0);
2957 vote(chg->pl_enable_votable_indirect, USBIN_V_VOTER, false, 0);
2958 vote(chg->awake_votable, PL_DELAY_VOTER, false, 0);
2959
2960 chg->vconn_attempts = 0;
2961 chg->otg_attempts = 0;
2962 chg->pulse_cnt = 0;
2963 chg->usb_icl_delta_ua = 0;
2964 chg->voltage_min_uv = MICRO_5V;
2965 chg->voltage_max_uv = MICRO_5V;
2966 chg->pd_active = 0;
2967 chg->pd_hard_reset = 0;
2968
2969 /* write back the default FLOAT charger configuration */
2970 rc = smblib_masked_write(chg, USBIN_OPTIONS_2_CFG_REG,
2971 (u8)FLOAT_OPTIONS_MASK, chg->float_cfg);
2972 if (rc < 0)
2973 smblib_err(chg, "Couldn't write float charger options rc=%d\n",
2974 rc);
2975
2976 /* reset back to 103mS tCC debounce */
2977 rc = smblib_masked_write(chg, TYPE_C_DEBOUNCE_OPTION_REG,
2978 REDUCE_TCCDEBOUNCE_TO_2MS_BIT, 0);
2979 if (rc < 0)
2980 smblib_err(chg, "Couldn't set 120mS tCC debounce rc=%d\n", rc);
2981
2982 /* reconfigure allowed voltage for HVDCP */
2983 rc = smblib_set_adapter_allowance(chg,
2984 USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V);
2985 if (rc < 0)
2986 smblib_err(chg, "Couldn't set USBIN_ADAPTER_ALLOW_5V_OR_9V_TO_12V rc=%d\n",
2987 rc);
2988
2989 /* enable DRP */
2990 rc = smblib_masked_write(chg, TYPE_C_MODE_CFG_REG,
2991 TYPEC_POWER_ROLE_CMD_MASK, 0);
2992 if (rc < 0)
2993 smblib_err(chg, "Couldn't enable DRP rc=%d\n", rc);
2994
2995 /* HW controlled CC_OUT */
2996 rc = smblib_masked_write(chg, TYPE_C_CCOUT_CONTROL_REG,
2997 TYPEC_CCOUT_SRC_BIT, 0);
2998 if (rc < 0)
2999 smblib_err(chg, "Couldn't enable HW cc_out rc=%d\n", rc);
3000
3001
3002 rc = smblib_masked_write(chg, TYPE_C_VCONN_CONTROL_REG,
3003 VCONN_EN_SRC_BIT, 0);
3004 if (rc < 0)
3005 smblib_err(chg, "Couldn't set TYPE_C_VCONN_CONTROL_REG rc=%d\n",
3006 rc);
3007
3008 /* clear exit sink based on cc */
3009 rc = smblib_masked_write(chg, TYPE_C_EXIT_STATE_CFG_REG,
3010 EXIT_SNK_BASED_ON_CC_BIT, 0);
3011 if (rc < 0)
3012 smblib_err(chg, "Couldn't clear exit_sink_based_on_cc rc=%d\n",
3013 rc);
3014
3015 typec_sink_removal(chg);
3016 smblib_update_usb_type(chg);
3017
3018 if (chg->use_extcon) {
3019 if (chg->otg_present)
3020 smblib_notify_usb_host(chg, false);
3021 else
3022 smblib_notify_device_mode(chg, false);
3023 }
3024 chg->otg_present = false;
3025}
3026
3027static void smblib_handle_typec_insertion(struct smb_charger *chg)
3028{
3029 int rc;
3030 u8 stat;
3031
3032 vote(chg->pd_disallowed_votable_indirect, CC_DETACHED_VOTER, false, 0);
3033
3034 rc = smblib_read(chg, TYPE_C_MISC_STATUS_REG, &stat);
3035 if (rc < 0) {
3036 smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n", rc);
3037 return;
3038 }
3039
3040 if (stat & SNK_SRC_MODE_BIT) {
3041 typec_sink_insertion(chg);
3042 } else {
3043 rc = smblib_request_dpdm(chg, true);
3044 if (rc < 0)
3045 smblib_err(chg, "Couldn't to enable DPDM rc=%d\n", rc);
3046 typec_sink_removal(chg);
3047 }
3048}
3049
3050static void smblib_handle_rp_change(struct smb_charger *chg, int typec_mode)
3051{
3052 int rp_ua;
3053 const struct apsd_result *apsd = smblib_get_apsd_result(chg);
3054
3055 if ((apsd->pst != POWER_SUPPLY_TYPE_USB_DCP)
3056 && (apsd->pst != POWER_SUPPLY_TYPE_USB_FLOAT))
3057 return;
3058
3059 /*
3060 * if APSD indicates FLOAT and the USB stack had detected SDP,
3061 * do not respond to Rp changes as we do not confirm that its
3062 * a legacy cable
3063 */
3064 if (chg->real_charger_type == POWER_SUPPLY_TYPE_USB)
3065 return;
3066 /*
3067 * We want the ICL vote @ 100mA for a FLOAT charger
3068 * until the detection by the USB stack is complete.
3069 * Ignore the Rp changes unless there is a
3070 * pre-existing valid vote.
3071 */
3072 if (apsd->pst == POWER_SUPPLY_TYPE_USB_FLOAT &&
3073 get_client_vote(chg->usb_icl_votable,
3074 LEGACY_UNKNOWN_VOTER) <= 100000)
3075 return;
3076
3077 /*
3078 * handle Rp change for DCP/FLOAT/OCP.
3079 * Update the current only if the Rp is different from
3080 * the last Rp value.
3081 */
3082 smblib_dbg(chg, PR_MISC, "CC change old_mode=%d new_mode=%d\n",
3083 chg->typec_mode, typec_mode);
3084
3085 rp_ua = get_rp_based_dcp_current(chg, typec_mode);
3086 vote(chg->usb_icl_votable, LEGACY_UNKNOWN_VOTER, true, rp_ua);
3087}
3088
3089static void smblib_handle_typec_cc_state_change(struct smb_charger *chg)
3090{
3091 u8 stat;
3092 int typec_mode, rc;
3093
3094 if (chg->pr_swap_in_progress)
3095 return;
3096
3097 typec_mode = smblib_get_prop_typec_mode(chg);
3098 if (chg->typec_present && (typec_mode != chg->typec_mode))
3099 smblib_handle_rp_change(chg, typec_mode);
3100
3101 chg->typec_mode = typec_mode;
3102
3103 if (!chg->typec_present && chg->typec_mode != POWER_SUPPLY_TYPEC_NONE) {
3104 chg->typec_present = true;
3105 smblib_dbg(chg, PR_MISC, "TypeC %s insertion\n",
3106 smblib_typec_mode_name[chg->typec_mode]);
3107 smblib_handle_typec_insertion(chg);
3108 } else if (chg->typec_present &&
3109 chg->typec_mode == POWER_SUPPLY_TYPEC_NONE) {
3110 chg->typec_present = false;
3111 smblib_dbg(chg, PR_MISC, "TypeC removal\n");
3112 smblib_handle_typec_removal(chg);
3113 }
3114
3115 rc = smblib_read(chg, TYPE_C_MISC_STATUS_REG, &stat);
3116 if (rc < 0) {
3117 smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n", rc);
3118 return;
3119 }
3120 /* suspend usb if sink */
3121 if ((stat & SNK_SRC_MODE_BIT) && chg->typec_present)
3122 vote(chg->usb_icl_votable, OTG_VOTER, true, 0);
3123 else
3124 vote(chg->usb_icl_votable, OTG_VOTER, false, 0);
3125
3126 smblib_dbg(chg, PR_INTERRUPT, "IRQ: cc-state-change; Type-C %s detected\n",
3127 smblib_typec_mode_name[chg->typec_mode]);
3128}
3129
3130static void smblib_usb_typec_change(struct smb_charger *chg)
3131{
3132 int rc;
3133 u8 stat;
3134
3135 smblib_handle_typec_cc_state_change(chg);
3136
3137 rc = smblib_read(chg, TYPE_C_MISC_STATUS_REG, &stat);
3138 if (rc < 0) {
3139 smblib_err(chg, "Couldn't read TYPE_C_STATUS_4 rc=%d\n", rc);
3140 return;
3141 }
3142
3143 if (stat & TYPEC_VBUS_ERROR_STATUS_BIT)
3144 smblib_dbg(chg, PR_INTERRUPT, "IRQ: vbus-error\n");
3145
3146 power_supply_changed(chg->usb_psy);
3147}
3148
3149irqreturn_t typec_state_change_irq_handler(int irq, void *data)
3150{
3151 struct smb_irq_data *irq_data = data;
3152 struct smb_charger *chg = irq_data->parent_data;
3153
3154 if (chg->micro_usb_mode) {
3155 cancel_delayed_work_sync(&chg->uusb_otg_work);
3156 vote(chg->awake_votable, OTG_DELAY_VOTER, true, 0);
3157 smblib_dbg(chg, PR_INTERRUPT, "Scheduling OTG work\n");
3158 schedule_delayed_work(&chg->uusb_otg_work,
3159 msecs_to_jiffies(chg->otg_delay_ms));
3160 return IRQ_HANDLED;
3161 }
3162
3163 if (chg->pr_swap_in_progress) {
3164 smblib_dbg(chg, PR_INTERRUPT,
3165 "Ignoring since pr_swap_in_progress\n");
3166 return IRQ_HANDLED;
3167 }
3168
3169 mutex_lock(&chg->lock);
3170 smblib_usb_typec_change(chg);
3171 mutex_unlock(&chg->lock);
3172 return IRQ_HANDLED;
3173}
3174
3175irqreturn_t dc_plugin_irq_handler(int irq, void *data)
3176{
3177 struct smb_irq_data *irq_data = data;
3178 struct smb_charger *chg = irq_data->parent_data;
3179
3180 power_supply_changed(chg->dc_psy);
3181 return IRQ_HANDLED;
3182}
3183
3184irqreturn_t high_duty_cycle_irq_handler(int irq, void *data)
3185{
3186 struct smb_irq_data *irq_data = data;
3187 struct smb_charger *chg = irq_data->parent_data;
3188
3189 chg->is_hdc = true;
3190 /*
3191 * Disable usb IRQs after the flag set and re-enable IRQs after
3192 * the flag cleared in the delayed work queue, to avoid any IRQ
3193 * storming during the delays
3194 */
3195 if (chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq)
3196 disable_irq_nosync(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq);
3197
3198 schedule_delayed_work(&chg->clear_hdc_work, msecs_to_jiffies(60));
3199
3200 return IRQ_HANDLED;
3201}
3202
3203static void smblib_bb_removal_work(struct work_struct *work)
3204{
3205 struct smb_charger *chg = container_of(work, struct smb_charger,
3206 bb_removal_work.work);
3207
3208 vote(chg->usb_icl_votable, BOOST_BACK_VOTER, false, 0);
3209 vote(chg->awake_votable, BOOST_BACK_VOTER, false, 0);
3210}
3211
3212#define BOOST_BACK_UNVOTE_DELAY_MS 750
3213#define BOOST_BACK_STORM_COUNT 3
3214#define WEAK_CHG_STORM_COUNT 8
3215irqreturn_t switcher_power_ok_irq_handler(int irq, void *data)
3216{
3217 struct smb_irq_data *irq_data = data;
3218 struct smb_charger *chg = irq_data->parent_data;
3219 struct storm_watch *wdata = &irq_data->storm_data;
3220 int rc, usb_icl;
3221 u8 stat;
3222
3223 if (!(chg->wa_flags & BOOST_BACK_WA))
3224 return IRQ_HANDLED;
3225
3226 rc = smblib_read(chg, POWER_PATH_STATUS_REG, &stat);
3227 if (rc < 0) {
3228 smblib_err(chg, "Couldn't read POWER_PATH_STATUS rc=%d\n", rc);
3229 return IRQ_HANDLED;
3230 }
3231
3232 /* skip suspending input if its already suspended by some other voter */
3233 usb_icl = get_effective_result(chg->usb_icl_votable);
3234 if ((stat & USE_USBIN_BIT) && usb_icl >= 0 && usb_icl <= USBIN_25MA)
3235 return IRQ_HANDLED;
3236
3237 if (stat & USE_DCIN_BIT)
3238 return IRQ_HANDLED;
3239
3240 if (is_storming(&irq_data->storm_data)) {
3241 /* This could be a weak charger reduce ICL */
3242 if (!is_client_vote_enabled(chg->usb_icl_votable,
3243 WEAK_CHARGER_VOTER)) {
3244 smblib_err(chg,
3245 "Weak charger detected: voting %dmA ICL\n",
3246 *chg->weak_chg_icl_ua / 1000);
3247 vote(chg->usb_icl_votable, WEAK_CHARGER_VOTER,
3248 true, *chg->weak_chg_icl_ua);
3249 /*
3250 * reset storm data and set the storm threshold
3251 * to 3 for reverse boost detection.
3252 */
3253 update_storm_count(wdata, BOOST_BACK_STORM_COUNT);
3254 } else {
3255 smblib_err(chg,
3256 "Reverse boost detected: voting 0mA to suspend input\n");
3257 vote(chg->usb_icl_votable, BOOST_BACK_VOTER, true, 0);
3258 vote(chg->awake_votable, BOOST_BACK_VOTER, true, 0);
3259 /*
3260 * Remove the boost-back vote after a delay, to avoid
3261 * permanently suspending the input if the boost-back
3262 * condition is unintentionally hit.
3263 */
3264 schedule_delayed_work(&chg->bb_removal_work,
3265 msecs_to_jiffies(BOOST_BACK_UNVOTE_DELAY_MS));
3266 }
3267 }
3268
3269 return IRQ_HANDLED;
3270}
3271
3272irqreturn_t wdog_bark_irq_handler(int irq, void *data)
3273{
3274 struct smb_irq_data *irq_data = data;
3275 struct smb_charger *chg = irq_data->parent_data;
3276 int rc;
3277
3278 smblib_dbg(chg, PR_INTERRUPT, "IRQ: %s\n", irq_data->name);
3279
3280 rc = smblib_write(chg, BARK_BITE_WDOG_PET_REG, BARK_BITE_WDOG_PET_BIT);
3281 if (rc < 0)
3282 smblib_err(chg, "Couldn't pet the dog rc=%d\n", rc);
3283
3284 if (chg->step_chg_enabled || chg->sw_jeita_enabled)
3285 power_supply_changed(chg->batt_psy);
3286
3287 return IRQ_HANDLED;
3288}
3289
3290/**************
3291 * Additional USB PSY getters/setters
3292 * that call interrupt functions
3293 ***************/
3294
3295int smblib_get_prop_pr_swap_in_progress(struct smb_charger *chg,
3296 union power_supply_propval *val)
3297{
3298 val->intval = chg->pr_swap_in_progress;
3299 return 0;
3300}
3301
3302int smblib_set_prop_pr_swap_in_progress(struct smb_charger *chg,
3303 const union power_supply_propval *val)
3304{
3305 int rc;
3306
3307 chg->pr_swap_in_progress = val->intval;
3308
3309 /*
3310 * call the cc changed irq to handle real removals while
3311 * PR_SWAP was in progress
3312 */
3313 smblib_usb_typec_change(chg);
3314 rc = smblib_masked_write(chg, TYPE_C_DEBOUNCE_OPTION_REG,
3315 REDUCE_TCCDEBOUNCE_TO_2MS_BIT,
3316 val->intval ? REDUCE_TCCDEBOUNCE_TO_2MS_BIT : 0);
3317 if (rc < 0)
3318 smblib_err(chg, "Couldn't set tCC debounce rc=%d\n", rc);
3319 return 0;
3320}
3321
3322/***************
3323 * Work Queues *
3324 ***************/
3325static void smblib_uusb_otg_work(struct work_struct *work)
3326{
3327 struct smb_charger *chg = container_of(work, struct smb_charger,
3328 uusb_otg_work.work);
3329 int rc;
3330 u8 stat;
3331 bool otg;
3332
3333 rc = smblib_read(chg, TYPEC_U_USB_STATUS_REG, &stat);
3334 if (rc < 0) {
3335 smblib_err(chg, "Couldn't read TYPE_C_STATUS_3 rc=%d\n", rc);
3336 goto out;
3337 }
3338
3339 otg = !!(stat & (U_USB_GROUND_NOVBUS_BIT | U_USB_GROUND_BIT));
3340 extcon_set_state_sync(chg->extcon, EXTCON_USB_HOST, otg);
3341 smblib_dbg(chg, PR_REGISTER, "TYPE_C_STATUS_3 = 0x%02x OTG=%d\n",
3342 stat, otg);
3343 power_supply_changed(chg->usb_psy);
3344
3345out:
3346 vote(chg->awake_votable, OTG_DELAY_VOTER, false, 0);
3347}
3348
3349static void bms_update_work(struct work_struct *work)
3350{
3351 struct smb_charger *chg = container_of(work, struct smb_charger,
3352 bms_update_work);
3353
3354 smblib_suspend_on_debug_battery(chg);
3355
3356 if (chg->batt_psy)
3357 power_supply_changed(chg->batt_psy);
3358}
3359
3360static void pl_update_work(struct work_struct *work)
3361{
3362 struct smb_charger *chg = container_of(work, struct smb_charger,
3363 pl_update_work);
3364
3365 smblib_stat_sw_override_cfg(chg, false);
3366}
3367
3368static void clear_hdc_work(struct work_struct *work)
3369{
3370 struct smb_charger *chg = container_of(work, struct smb_charger,
3371 clear_hdc_work.work);
3372
3373 chg->is_hdc = 0;
3374 if (chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq)
3375 enable_irq(chg->irq_info[HIGH_DUTY_CYCLE_IRQ].irq);
3376}
3377
3378static void smblib_icl_change_work(struct work_struct *work)
3379{
3380 struct smb_charger *chg = container_of(work, struct smb_charger,
3381 icl_change_work.work);
3382 int rc, settled_ua;
3383
3384 rc = smblib_get_charge_param(chg, &chg->param.icl_stat, &settled_ua);
3385 if (rc < 0) {
3386 smblib_err(chg, "Couldn't get ICL status rc=%d\n", rc);
3387 return;
3388 }
3389
3390 power_supply_changed(chg->usb_main_psy);
3391
3392 smblib_dbg(chg, PR_INTERRUPT, "icl_settled=%d\n", settled_ua);
3393}
3394
3395static void smblib_pl_enable_work(struct work_struct *work)
3396{
3397 struct smb_charger *chg = container_of(work, struct smb_charger,
3398 pl_enable_work.work);
3399
3400 smblib_dbg(chg, PR_PARALLEL, "timer expired, enabling parallel\n");
3401 vote(chg->pl_disable_votable, PL_DELAY_VOTER, false, 0);
3402 vote(chg->awake_votable, PL_DELAY_VOTER, false, 0);
3403}
3404
3405static int smblib_create_votables(struct smb_charger *chg)
3406{
3407 int rc = 0;
3408
3409 chg->fcc_votable = find_votable("FCC");
3410 if (chg->fcc_votable == NULL) {
3411 rc = -EINVAL;
3412 smblib_err(chg, "Couldn't find FCC votable rc=%d\n", rc);
3413 return rc;
3414 }
3415
3416 chg->fv_votable = find_votable("FV");
3417 if (chg->fv_votable == NULL) {
3418 rc = -EINVAL;
3419 smblib_err(chg, "Couldn't find FV votable rc=%d\n", rc);
3420 return rc;
3421 }
3422
3423 chg->usb_icl_votable = find_votable("USB_ICL");
3424 if (chg->usb_icl_votable == NULL) {
3425 rc = -EINVAL;
3426 smblib_err(chg, "Couldn't find USB_ICL votable rc=%d\n", rc);
3427 return rc;
3428 }
3429
3430 chg->pl_disable_votable = find_votable("PL_DISABLE");
3431 if (chg->pl_disable_votable == NULL) {
3432 rc = -EINVAL;
3433 smblib_err(chg, "Couldn't find votable PL_DISABLE rc=%d\n", rc);
3434 return rc;
3435 }
3436
3437 chg->pl_enable_votable_indirect = find_votable("PL_ENABLE_INDIRECT");
3438 if (chg->pl_enable_votable_indirect == NULL) {
3439 rc = -EINVAL;
3440 smblib_err(chg,
3441 "Couldn't find votable PL_ENABLE_INDIRECT rc=%d\n",
3442 rc);
3443 return rc;
3444 }
3445
3446 vote(chg->pl_disable_votable, PL_DELAY_VOTER, true, 0);
3447
3448 chg->dc_suspend_votable = create_votable("DC_SUSPEND", VOTE_SET_ANY,
3449 smblib_dc_suspend_vote_callback,
3450 chg);
3451 if (IS_ERR(chg->dc_suspend_votable)) {
3452 rc = PTR_ERR(chg->dc_suspend_votable);
3453 chg->dc_suspend_votable = NULL;
3454 return rc;
3455 }
3456
3457 chg->pd_disallowed_votable_indirect
3458 = create_votable("PD_DISALLOWED_INDIRECT", VOTE_SET_ANY,
3459 smblib_pd_disallowed_votable_indirect_callback, chg);
3460 if (IS_ERR(chg->pd_disallowed_votable_indirect)) {
3461 rc = PTR_ERR(chg->pd_disallowed_votable_indirect);
3462 chg->pd_disallowed_votable_indirect = NULL;
3463 return rc;
3464 }
3465
3466 chg->pd_allowed_votable = create_votable("PD_ALLOWED",
3467 VOTE_SET_ANY, NULL, NULL);
3468 if (IS_ERR(chg->pd_allowed_votable)) {
3469 rc = PTR_ERR(chg->pd_allowed_votable);
3470 chg->pd_allowed_votable = NULL;
3471 return rc;
3472 }
3473
3474 chg->awake_votable = create_votable("AWAKE", VOTE_SET_ANY,
3475 smblib_awake_vote_callback,
3476 chg);
3477 if (IS_ERR(chg->awake_votable)) {
3478 rc = PTR_ERR(chg->awake_votable);
3479 chg->awake_votable = NULL;
3480 return rc;
3481 }
3482
3483 chg->chg_disable_votable = create_votable("CHG_DISABLE", VOTE_SET_ANY,
3484 smblib_chg_disable_vote_callback,
3485 chg);
3486 if (IS_ERR(chg->chg_disable_votable)) {
3487 rc = PTR_ERR(chg->chg_disable_votable);
3488 chg->chg_disable_votable = NULL;
3489 return rc;
3490 }
3491
3492 chg->usb_irq_enable_votable = create_votable("USB_IRQ_DISABLE",
3493 VOTE_SET_ANY,
3494 smblib_usb_irq_enable_vote_callback,
3495 chg);
3496 if (IS_ERR(chg->usb_irq_enable_votable)) {
3497 rc = PTR_ERR(chg->usb_irq_enable_votable);
3498 chg->usb_irq_enable_votable = NULL;
3499 return rc;
3500 }
3501
3502 return rc;
3503}
3504
3505static void smblib_destroy_votables(struct smb_charger *chg)
3506{
3507 if (chg->dc_suspend_votable)
3508 destroy_votable(chg->dc_suspend_votable);
3509 if (chg->usb_icl_votable)
3510 destroy_votable(chg->usb_icl_votable);
3511 if (chg->pd_disallowed_votable_indirect)
3512 destroy_votable(chg->pd_disallowed_votable_indirect);
3513 if (chg->pd_allowed_votable)
3514 destroy_votable(chg->pd_allowed_votable);
3515 if (chg->awake_votable)
3516 destroy_votable(chg->awake_votable);
3517 if (chg->chg_disable_votable)
3518 destroy_votable(chg->chg_disable_votable);
3519}
3520
3521int smblib_init(struct smb_charger *chg)
3522{
3523 int rc = 0;
3524
3525 mutex_init(&chg->lock);
3526 mutex_init(&chg->otg_oc_lock);
3527 INIT_WORK(&chg->bms_update_work, bms_update_work);
3528 INIT_WORK(&chg->pl_update_work, pl_update_work);
3529 INIT_DELAYED_WORK(&chg->clear_hdc_work, clear_hdc_work);
3530 INIT_DELAYED_WORK(&chg->icl_change_work, smblib_icl_change_work);
3531 INIT_DELAYED_WORK(&chg->pl_enable_work, smblib_pl_enable_work);
3532 INIT_DELAYED_WORK(&chg->uusb_otg_work, smblib_uusb_otg_work);
3533 INIT_DELAYED_WORK(&chg->bb_removal_work, smblib_bb_removal_work);
3534 chg->fake_capacity = -EINVAL;
3535 chg->fake_input_current_limited = -EINVAL;
3536 chg->fake_batt_status = -EINVAL;
3537
3538 switch (chg->mode) {
3539 case PARALLEL_MASTER:
3540 rc = qcom_batt_init();
3541 if (rc < 0) {
3542 smblib_err(chg, "Couldn't init qcom_batt_init rc=%d\n",
3543 rc);
3544 return rc;
3545 }
3546
3547 rc = qcom_step_chg_init(chg->dev, chg->step_chg_enabled,
3548 chg->sw_jeita_enabled);
3549 if (rc < 0) {
3550 smblib_err(chg, "Couldn't init qcom_step_chg_init rc=%d\n",
3551 rc);
3552 return rc;
3553 }
3554
3555 rc = smblib_create_votables(chg);
3556 if (rc < 0) {
3557 smblib_err(chg, "Couldn't create votables rc=%d\n",
3558 rc);
3559 return rc;
3560 }
3561
3562 rc = smblib_register_notifier(chg);
3563 if (rc < 0) {
3564 smblib_err(chg,
3565 "Couldn't register notifier rc=%d\n", rc);
3566 return rc;
3567 }
3568
3569 chg->bms_psy = power_supply_get_by_name("bms");
3570 chg->pl.psy = power_supply_get_by_name("parallel");
3571 if (chg->pl.psy) {
3572 rc = smblib_stat_sw_override_cfg(chg, false);
3573 if (rc < 0) {
3574 smblib_err(chg,
3575 "Couldn't config stat sw rc=%d\n", rc);
3576 return rc;
3577 }
3578 }
3579 break;
3580 case PARALLEL_SLAVE:
3581 break;
3582 default:
3583 smblib_err(chg, "Unsupported mode %d\n", chg->mode);
3584 return -EINVAL;
3585 }
3586
3587 return rc;
3588}
3589
3590int smblib_deinit(struct smb_charger *chg)
3591{
3592 switch (chg->mode) {
3593 case PARALLEL_MASTER:
3594 cancel_work_sync(&chg->bms_update_work);
3595 cancel_work_sync(&chg->pl_update_work);
3596 cancel_delayed_work_sync(&chg->clear_hdc_work);
3597 cancel_delayed_work_sync(&chg->icl_change_work);
3598 cancel_delayed_work_sync(&chg->pl_enable_work);
3599 cancel_delayed_work_sync(&chg->uusb_otg_work);
3600 cancel_delayed_work_sync(&chg->bb_removal_work);
3601 power_supply_unreg_notifier(&chg->nb);
3602 smblib_destroy_votables(chg);
3603 qcom_step_chg_deinit();
3604 qcom_batt_deinit();
3605 break;
3606 case PARALLEL_SLAVE:
3607 break;
3608 default:
3609 smblib_err(chg, "Unsupported mode %d\n", chg->mode);
3610 return -EINVAL;
3611 }
3612
3613 return 0;
3614}