blob: 403b670a27b091f812deeedfae0b7581e44d29d0 [file] [log] [blame]
Kiran Gunda1bc78922017-09-19 13:09:44 +05301/* Copyright (c) 2014-2016 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#define pr_fmt(fmt) "SMBCHG: %s: " fmt, __func__
13
14#include <linux/regmap.h>
15#include <linux/spinlock.h>
16#include <linux/gpio.h>
17#include <linux/errno.h>
18#include <linux/delay.h>
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/slab.h>
22#include <linux/sched.h>
23#include <linux/power_supply.h>
24#include <linux/of.h>
25#include <linux/of_gpio.h>
26#include <linux/of_irq.h>
27#include <linux/bitops.h>
28#include <linux/regulator/consumer.h>
29#include <linux/regulator/driver.h>
30#include <linux/regulator/of_regulator.h>
31#include <linux/regulator/machine.h>
32#include <linux/spmi.h>
33#include <linux/platform_device.h>
34#include <linux/printk.h>
35#include <linux/ratelimit.h>
36#include <linux/debugfs.h>
37#include <linux/leds.h>
38#include <linux/rtc.h>
39#include <linux/qpnp/qpnp-adc.h>
40#include <linux/batterydata-lib.h>
41#include <linux/of_batterydata.h>
42#include <linux/msm_bcl.h>
43#include <linux/ktime.h>
44#include <linux/extcon.h>
Kiran Gundaee7cef82017-09-20 17:44:16 +053045#include <linux/pmic-voter.h>
Kiran Gunda1bc78922017-09-19 13:09:44 +053046
47/* Mask/Bit helpers */
48#define _SMB_MASK(BITS, POS) \
49 ((unsigned char)(((1 << (BITS)) - 1) << (POS)))
50#define SMB_MASK(LEFT_BIT_POS, RIGHT_BIT_POS) \
51 _SMB_MASK((LEFT_BIT_POS) - (RIGHT_BIT_POS) + 1, \
52 (RIGHT_BIT_POS))
53/* Config registers */
54struct smbchg_regulator {
55 struct regulator_desc rdesc;
56 struct regulator_dev *rdev;
57};
58
59struct parallel_usb_cfg {
60 struct power_supply *psy;
61 int min_current_thr_ma;
62 int min_9v_current_thr_ma;
63 int allowed_lowering_ma;
64 int current_max_ma;
65 bool avail;
66 struct mutex lock;
67 int initial_aicl_ma;
68 ktime_t last_disabled;
69 bool enabled_once;
70};
71
72struct ilim_entry {
73 int vmin_uv;
74 int vmax_uv;
75 int icl_pt_ma;
76 int icl_lv_ma;
77 int icl_hv_ma;
78};
79
80struct ilim_map {
81 int num;
82 struct ilim_entry *entries;
83};
84
85struct smbchg_version_tables {
86 const int *dc_ilim_ma_table;
87 int dc_ilim_ma_len;
88 const int *usb_ilim_ma_table;
89 int usb_ilim_ma_len;
90 const int *iterm_ma_table;
91 int iterm_ma_len;
92 const int *fcc_comp_table;
93 int fcc_comp_len;
94 const int *aicl_rerun_period_table;
95 int aicl_rerun_period_len;
96 int rchg_thr_mv;
97};
98
99struct smbchg_chip {
100 struct device *dev;
101 struct platform_device *pdev;
102 struct regmap *regmap;
103 int schg_version;
104
105 /* peripheral register address bases */
106 u16 chgr_base;
107 u16 bat_if_base;
108 u16 usb_chgpth_base;
109 u16 dc_chgpth_base;
110 u16 otg_base;
111 u16 misc_base;
112
113 int fake_battery_soc;
114 u8 revision[4];
115
116 /* configuration parameters */
117 int iterm_ma;
118 int usb_max_current_ma;
119 int typec_current_ma;
120 int dc_max_current_ma;
121 int dc_target_current_ma;
122 int cfg_fastchg_current_ma;
123 int fastchg_current_ma;
124 int vfloat_mv;
125 int fastchg_current_comp;
126 int float_voltage_comp;
127 int resume_delta_mv;
128 int safety_time;
129 int prechg_safety_time;
130 int bmd_pin_src;
131 int jeita_temp_hard_limit;
132 int aicl_rerun_period_s;
133 bool use_vfloat_adjustments;
134 bool iterm_disabled;
135 bool bmd_algo_disabled;
136 bool soft_vfloat_comp_disabled;
137 bool chg_enabled;
138 bool charge_unknown_battery;
139 bool chg_inhibit_en;
140 bool chg_inhibit_source_fg;
141 bool low_volt_dcin;
142 bool cfg_chg_led_support;
143 bool cfg_chg_led_sw_ctrl;
144 bool vbat_above_headroom;
145 bool force_aicl_rerun;
146 bool hvdcp3_supported;
Subbaraman Narayanamurthy986bff52016-03-21 15:55:19 -0700147 bool allow_hvdcp3_detection;
Kiran Gunda1bc78922017-09-19 13:09:44 +0530148 bool restricted_charging;
149 bool skip_usb_suspend_for_fake_battery;
150 bool hvdcp_not_supported;
151 bool otg_pinctrl;
152 u8 original_usbin_allowance;
153 struct parallel_usb_cfg parallel;
154 struct delayed_work parallel_en_work;
155 struct dentry *debug_root;
156 struct smbchg_version_tables tables;
157
158 /* wipower params */
159 struct ilim_map wipower_default;
160 struct ilim_map wipower_pt;
161 struct ilim_map wipower_div2;
162 struct qpnp_vadc_chip *vadc_dev;
163 bool wipower_dyn_icl_avail;
164 struct ilim_entry current_ilim;
165 struct mutex wipower_config;
166 bool wipower_configured;
167 struct qpnp_adc_tm_btm_param param;
168
169 /* flash current prediction */
170 int rpara_uohm;
171 int rslow_uohm;
172 int vled_max_uv;
173
174 /* vfloat adjustment */
175 int max_vbat_sample;
176 int n_vbat_samples;
177
178 /* status variables */
179 int wake_reasons;
180 int previous_soc;
181 int usb_online;
182 bool dc_present;
183 bool usb_present;
184 bool batt_present;
185 int otg_retries;
186 ktime_t otg_enable_time;
187 bool aicl_deglitch_short;
188 bool safety_timer_en;
189 bool aicl_complete;
190 bool usb_ov_det;
191 bool otg_pulse_skip_dis;
192 const char *battery_type;
193 enum power_supply_type usb_supply_type;
194 bool very_weak_charger;
195 bool parallel_charger_detected;
196 bool chg_otg_enabled;
197 bool flash_triggered;
198 bool flash_active;
199 bool icl_disabled;
200 u32 wa_flags;
201 int usb_icl_delta;
202 bool typec_dfp;
203 unsigned int usb_current_max;
204 unsigned int usb_health;
205
206 /* jeita and temperature */
207 bool batt_hot;
208 bool batt_cold;
209 bool batt_warm;
210 bool batt_cool;
211 unsigned int thermal_levels;
212 unsigned int therm_lvl_sel;
213 unsigned int *thermal_mitigation;
214
215 /* irqs */
216 int batt_hot_irq;
217 int batt_warm_irq;
218 int batt_cool_irq;
219 int batt_cold_irq;
220 int batt_missing_irq;
221 int vbat_low_irq;
222 int chg_hot_irq;
223 int chg_term_irq;
224 int taper_irq;
225 bool taper_irq_enabled;
226 struct mutex taper_irq_lock;
227 int recharge_irq;
228 int fastchg_irq;
229 int wdog_timeout_irq;
230 int power_ok_irq;
231 int dcin_uv_irq;
232 int usbin_uv_irq;
233 int usbin_ov_irq;
234 int src_detect_irq;
235 int otg_fail_irq;
236 int otg_oc_irq;
237 int aicl_done_irq;
238 int usbid_change_irq;
239 int chg_error_irq;
240 bool enable_aicl_wake;
241
242 /* psy */
243 struct power_supply_desc usb_psy_d;
244 struct power_supply *usb_psy;
245 struct power_supply_desc batt_psy_d;
246 struct power_supply *batt_psy;
247 struct power_supply_desc dc_psy_d;
248 struct power_supply *dc_psy;
249 struct power_supply *bms_psy;
250 struct power_supply *typec_psy;
251 int dc_psy_type;
252 const char *bms_psy_name;
253 const char *battery_psy_name;
254
255 struct regulator *dpdm_reg;
256 struct smbchg_regulator otg_vreg;
257 struct smbchg_regulator ext_otg_vreg;
258 struct work_struct usb_set_online_work;
259 struct delayed_work vfloat_adjust_work;
260 struct delayed_work hvdcp_det_work;
261 spinlock_t sec_access_lock;
262 struct mutex therm_lvl_lock;
263 struct mutex usb_set_online_lock;
264 struct mutex pm_lock;
265 /* aicl deglitch workaround */
266 unsigned long first_aicl_seconds;
267 int aicl_irq_count;
268 struct mutex usb_status_lock;
269 bool hvdcp_3_det_ignore_uv;
270 struct completion src_det_lowered;
271 struct completion src_det_raised;
272 struct completion usbin_uv_lowered;
273 struct completion usbin_uv_raised;
274 int pulse_cnt;
275 struct led_classdev led_cdev;
276 bool skip_usb_notification;
277 u32 vchg_adc_channel;
278 struct qpnp_vadc_chip *vchg_vadc_dev;
279
280 /* voters */
281 struct votable *fcc_votable;
282 struct votable *usb_icl_votable;
283 struct votable *dc_icl_votable;
284 struct votable *usb_suspend_votable;
285 struct votable *dc_suspend_votable;
286 struct votable *battchg_suspend_votable;
287 struct votable *hw_aicl_rerun_disable_votable;
288 struct votable *hw_aicl_rerun_enable_indirect_votable;
289 struct votable *aicl_deglitch_short_votable;
290
291 /* extcon for VBUS / ID notification to USB */
292 struct extcon_dev *extcon;
293};
294
295enum qpnp_schg {
296 QPNP_SCHG,
297 QPNP_SCHG_LITE,
298};
299
300static char *version_str[] = {
301 [QPNP_SCHG] = "SCHG",
302 [QPNP_SCHG_LITE] = "SCHG_LITE",
303};
304
305enum pmic_subtype {
306 PMI8994 = 10,
307 PMI8950 = 17,
308 PMI8996 = 19,
309 PMI8937 = 55,
310};
311
312enum smbchg_wa {
313 SMBCHG_AICL_DEGLITCH_WA = BIT(0),
314 SMBCHG_HVDCP_9V_EN_WA = BIT(1),
315 SMBCHG_USB100_WA = BIT(2),
316 SMBCHG_BATT_OV_WA = BIT(3),
317 SMBCHG_CC_ESR_WA = BIT(4),
318 SMBCHG_FLASH_ICL_DISABLE_WA = BIT(5),
319 SMBCHG_RESTART_WA = BIT(6),
320 SMBCHG_FLASH_BUCK_SWITCH_FREQ_WA = BIT(7),
321};
322
323enum print_reason {
324 PR_REGISTER = BIT(0),
325 PR_INTERRUPT = BIT(1),
326 PR_STATUS = BIT(2),
327 PR_DUMP = BIT(3),
328 PR_PM = BIT(4),
329 PR_MISC = BIT(5),
330 PR_WIPOWER = BIT(6),
331 PR_TYPEC = BIT(7),
332};
333
334enum wake_reason {
335 PM_PARALLEL_CHECK = BIT(0),
336 PM_REASON_VFLOAT_ADJUST = BIT(1),
337 PM_ESR_PULSE = BIT(2),
338 PM_PARALLEL_TAPER = BIT(3),
339 PM_DETECT_HVDCP = BIT(4),
340};
341
342/* fcc_voters */
343#define ESR_PULSE_FCC_VOTER "ESR_PULSE_FCC_VOTER"
344#define BATT_TYPE_FCC_VOTER "BATT_TYPE_FCC_VOTER"
345#define RESTRICTED_CHG_FCC_VOTER "RESTRICTED_CHG_FCC_VOTER"
346
347/* ICL VOTERS */
348#define PSY_ICL_VOTER "PSY_ICL_VOTER"
349#define THERMAL_ICL_VOTER "THERMAL_ICL_VOTER"
350#define HVDCP_ICL_VOTER "HVDCP_ICL_VOTER"
351#define USER_ICL_VOTER "USER_ICL_VOTER"
352#define WEAK_CHARGER_ICL_VOTER "WEAK_CHARGER_ICL_VOTER"
353#define SW_AICL_ICL_VOTER "SW_AICL_ICL_VOTER"
354#define CHG_SUSPEND_WORKAROUND_ICL_VOTER "CHG_SUSPEND_WORKAROUND_ICL_VOTER"
355
356/* USB SUSPEND VOTERS */
357/* userspace has suspended charging altogether */
358#define USER_EN_VOTER "USER_EN_VOTER"
359/*
360 * this specific path has been suspended through the power supply
361 * framework
362 */
363#define POWER_SUPPLY_EN_VOTER "POWER_SUPPLY_EN_VOTER"
364/*
365 * the usb driver has suspended this path by setting a current limit
366 * of < 2MA
367 */
368#define USB_EN_VOTER "USB_EN_VOTER"
369/*
370 * the thermal daemon can suspend a charge path when the system
371 * temperature levels rise
372 */
373#define THERMAL_EN_VOTER "THERMAL_EN_VOTER"
374/*
375 * an external OTG supply is being used, suspend charge path so the
376 * charger does not accidentally try to charge from the external supply.
377 */
378#define OTG_EN_VOTER "OTG_EN_VOTER"
379/*
380 * the charger is very weak, do not draw any current from it
381 */
382#define WEAK_CHARGER_EN_VOTER "WEAK_CHARGER_EN_VOTER"
383/*
384 * fake battery voter, if battery id-resistance around 7.5 Kohm
385 */
386#define FAKE_BATTERY_EN_VOTER "FAKE_BATTERY_EN_VOTER"
387
388/* battchg_enable_voters */
389 /* userspace has disabled battery charging */
390#define BATTCHG_USER_EN_VOTER "BATTCHG_USER_EN_VOTER"
391 /* battery charging disabled while loading battery profiles */
392#define BATTCHG_UNKNOWN_BATTERY_EN_VOTER "BATTCHG_UNKNOWN_BATTERY_EN_VOTER"
393
394/* hw_aicl_rerun_enable_indirect_voters */
395/* enabled via device tree */
396#define DEFAULT_CONFIG_HW_AICL_VOTER "DEFAULT_CONFIG_HW_AICL_VOTER"
397/* Varb workaround voter */
398#define VARB_WORKAROUND_VOTER "VARB_WORKAROUND_VOTER"
399/* SHUTDOWN workaround voter */
400#define SHUTDOWN_WORKAROUND_VOTER "SHUTDOWN_WORKAROUND_VOTER"
401
402/* hw_aicl_rerun_disable_voters */
403/* the results from enabling clients */
404#define HW_AICL_RERUN_ENABLE_INDIRECT_VOTER \
405 "HW_AICL_RERUN_ENABLE_INDIRECT_VOTER"
406/* Weak charger voter */
407#define WEAK_CHARGER_HW_AICL_VOTER "WEAK_CHARGER_HW_AICL_VOTER"
408
409/* aicl_short_deglitch_voters */
410/* Varb workaround voter */
411#define VARB_WORKAROUND_SHORT_DEGLITCH_VOTER \
412 "VARB_WRKARND_SHORT_DEGLITCH_VOTER"
413/* QC 2.0 */
414#define HVDCP_SHORT_DEGLITCH_VOTER "HVDCP_SHORT_DEGLITCH_VOTER"
415
416static const unsigned int smbchg_extcon_cable[] = {
417 EXTCON_USB,
418 EXTCON_USB_HOST,
419 EXTCON_NONE,
420};
421
422static int smbchg_debug_mask;
423module_param_named(
Kiran Gundaee7cef82017-09-20 17:44:16 +0530424 debug_mask, smbchg_debug_mask, int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530425);
426
427static int smbchg_parallel_en = 1;
428module_param_named(
Kiran Gundaee7cef82017-09-20 17:44:16 +0530429 parallel_en, smbchg_parallel_en, int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530430);
431
432static int smbchg_main_chg_fcc_percent = 50;
433module_param_named(
434 main_chg_fcc_percent, smbchg_main_chg_fcc_percent,
Kiran Gundaee7cef82017-09-20 17:44:16 +0530435 int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530436);
437
438static int smbchg_main_chg_icl_percent = 60;
439module_param_named(
440 main_chg_icl_percent, smbchg_main_chg_icl_percent,
Kiran Gundaee7cef82017-09-20 17:44:16 +0530441 int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530442);
443
444static int smbchg_default_hvdcp_icl_ma = 1800;
445module_param_named(
446 default_hvdcp_icl_ma, smbchg_default_hvdcp_icl_ma,
Kiran Gundaee7cef82017-09-20 17:44:16 +0530447 int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530448);
449
450static int smbchg_default_hvdcp3_icl_ma = 3000;
451module_param_named(
452 default_hvdcp3_icl_ma, smbchg_default_hvdcp3_icl_ma,
Kiran Gundaee7cef82017-09-20 17:44:16 +0530453 int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530454);
455
456static int smbchg_default_dcp_icl_ma = 1800;
457module_param_named(
458 default_dcp_icl_ma, smbchg_default_dcp_icl_ma,
Kiran Gundaee7cef82017-09-20 17:44:16 +0530459 int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530460);
461
462static int wipower_dyn_icl_en;
463module_param_named(
464 dynamic_icl_wipower_en, wipower_dyn_icl_en,
Kiran Gundaee7cef82017-09-20 17:44:16 +0530465 int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530466);
467
468static int wipower_dcin_interval = ADC_MEAS1_INTERVAL_2P0MS;
469module_param_named(
470 wipower_dcin_interval, wipower_dcin_interval,
Kiran Gundaee7cef82017-09-20 17:44:16 +0530471 int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530472);
473
474#define WIPOWER_DEFAULT_HYSTERISIS_UV 250000
475static int wipower_dcin_hyst_uv = WIPOWER_DEFAULT_HYSTERISIS_UV;
476module_param_named(
477 wipower_dcin_hyst_uv, wipower_dcin_hyst_uv,
Kiran Gundaee7cef82017-09-20 17:44:16 +0530478 int, 00600
Kiran Gunda1bc78922017-09-19 13:09:44 +0530479);
480
481#define pr_smb(reason, fmt, ...) \
482 do { \
483 if (smbchg_debug_mask & (reason)) \
484 pr_info(fmt, ##__VA_ARGS__); \
485 else \
486 pr_debug(fmt, ##__VA_ARGS__); \
487 } while (0)
488
489#define pr_smb_rt(reason, fmt, ...) \
490 do { \
491 if (smbchg_debug_mask & (reason)) \
492 pr_info_ratelimited(fmt, ##__VA_ARGS__); \
493 else \
494 pr_debug(fmt, ##__VA_ARGS__); \
495 } while (0)
496
497static int smbchg_read(struct smbchg_chip *chip, u8 *val,
498 u16 addr, int count)
499{
500 int rc = 0;
501 struct platform_device *pdev = chip->pdev;
502
503 if (addr == 0) {
504 dev_err(chip->dev, "addr cannot be zero addr=0x%02x sid=0x%02x rc=%d\n",
505 addr, to_spmi_device(pdev->dev.parent)->usid, rc);
506 return -EINVAL;
507 }
508
509 rc = regmap_bulk_read(chip->regmap, addr, val, count);
510 if (rc) {
511 dev_err(chip->dev, "spmi read failed addr=0x%02x sid=0x%02x rc=%d\n",
512 addr, to_spmi_device(pdev->dev.parent)->usid,
513 rc);
514 return rc;
515 }
516 return 0;
517}
518
519/*
520 * Writes a register to the specified by the base and limited by the bit mask
521 *
522 * Do not use this function for register writes if possible. Instead use the
523 * smbchg_masked_write function.
524 *
525 * The sec_access_lock must be held for all register writes and this function
526 * does not do that. If this function is used, please hold the spinlock or
527 * random secure access writes may fail.
528 */
529static int smbchg_masked_write_raw(struct smbchg_chip *chip, u16 base, u8 mask,
530 u8 val)
531{
532 int rc;
533
534 rc = regmap_update_bits(chip->regmap, base, mask, val);
535 if (rc) {
536 dev_err(chip->dev, "spmi write failed: addr=%03X, rc=%d\n",
537 base, rc);
538 return rc;
539 }
540
541 return 0;
542}
543
544/*
545 * Writes a register to the specified by the base and limited by the bit mask
546 *
547 * This function holds a spin lock to ensure secure access register writes goes
548 * through. If the secure access unlock register is armed, any old register
549 * write can unarm the secure access unlock, causing the next write to fail.
550 *
551 * Note: do not use this for sec_access registers. Instead use the function
552 * below: smbchg_sec_masked_write
553 */
554static int smbchg_masked_write(struct smbchg_chip *chip, u16 base, u8 mask,
555 u8 val)
556{
557 unsigned long flags;
558 int rc;
559
560 spin_lock_irqsave(&chip->sec_access_lock, flags);
561 rc = smbchg_masked_write_raw(chip, base, mask, val);
562 spin_unlock_irqrestore(&chip->sec_access_lock, flags);
563
564 return rc;
565}
566
567/*
568 * Unlocks sec access and writes to the register specified.
569 *
570 * This function holds a spin lock to exclude other register writes while
571 * the two writes are taking place.
572 */
573#define SEC_ACCESS_OFFSET 0xD0
574#define SEC_ACCESS_VALUE 0xA5
575#define PERIPHERAL_MASK 0xFF
576static int smbchg_sec_masked_write(struct smbchg_chip *chip, u16 base, u8 mask,
577 u8 val)
578{
579 unsigned long flags;
580 int rc;
581 u16 peripheral_base = base & (~PERIPHERAL_MASK);
582
583 spin_lock_irqsave(&chip->sec_access_lock, flags);
584
585 rc = smbchg_masked_write_raw(chip, peripheral_base + SEC_ACCESS_OFFSET,
586 SEC_ACCESS_VALUE, SEC_ACCESS_VALUE);
587 if (rc) {
588 dev_err(chip->dev, "Unable to unlock sec_access: %d", rc);
589 goto out;
590 }
591
592 rc = smbchg_masked_write_raw(chip, base, mask, val);
593
594out:
595 spin_unlock_irqrestore(&chip->sec_access_lock, flags);
596 return rc;
597}
598
599static void smbchg_stay_awake(struct smbchg_chip *chip, int reason)
600{
601 int reasons;
602
603 mutex_lock(&chip->pm_lock);
604 reasons = chip->wake_reasons | reason;
605 if (reasons != 0 && chip->wake_reasons == 0) {
606 pr_smb(PR_PM, "staying awake: 0x%02x (bit %d)\n",
607 reasons, reason);
608 pm_stay_awake(chip->dev);
609 }
610 chip->wake_reasons = reasons;
611 mutex_unlock(&chip->pm_lock);
612}
613
614static void smbchg_relax(struct smbchg_chip *chip, int reason)
615{
616 int reasons;
617
618 mutex_lock(&chip->pm_lock);
619 reasons = chip->wake_reasons & (~reason);
620 if (reasons == 0 && chip->wake_reasons != 0) {
621 pr_smb(PR_PM, "relaxing: 0x%02x (bit %d)\n",
622 reasons, reason);
623 pm_relax(chip->dev);
624 }
625 chip->wake_reasons = reasons;
626 mutex_unlock(&chip->pm_lock);
627};
628
629enum pwr_path_type {
630 UNKNOWN = 0,
631 PWR_PATH_BATTERY = 1,
632 PWR_PATH_USB = 2,
633 PWR_PATH_DC = 3,
634};
635
636#define PWR_PATH 0x08
637#define PWR_PATH_MASK 0x03
638static enum pwr_path_type smbchg_get_pwr_path(struct smbchg_chip *chip)
639{
640 int rc;
641 u8 reg;
642
643 rc = smbchg_read(chip, &reg, chip->usb_chgpth_base + PWR_PATH, 1);
644 if (rc < 0) {
645 dev_err(chip->dev, "Couldn't read PWR_PATH rc = %d\n", rc);
646 return PWR_PATH_BATTERY;
647 }
648
649 return reg & PWR_PATH_MASK;
650}
651
652#define RID_STS 0xB
653#define RID_MASK 0xF
654#define IDEV_STS 0x8
655#define RT_STS 0x10
656#define USBID_MSB 0xE
657#define USBIN_UV_BIT BIT(0)
658#define USBIN_OV_BIT BIT(1)
659#define USBIN_SRC_DET_BIT BIT(2)
660#define FMB_STS_MASK SMB_MASK(3, 0)
661#define USBID_GND_THRESHOLD 0x495
662static bool is_otg_present_schg(struct smbchg_chip *chip)
663{
664 int rc;
665 u8 reg;
666 u8 usbid_reg[2];
667 u16 usbid_val;
668 /*
669 * After the falling edge of the usbid change interrupt occurs,
670 * there may still be some time before the ADC conversion for USB RID
671 * finishes in the fuel gauge. In the worst case, this could be up to
672 * 15 ms.
673 *
674 * Sleep for 20 ms (minimum msleep time) to wait for the conversion to
675 * finish and the USB RID status register to be updated before trying
676 * to detect OTG insertions.
677 */
678
679 msleep(20);
680
681 /*
682 * There is a problem with USBID conversions on PMI8994 revisions
683 * 2.0.0. As a workaround, check that the cable is not
684 * detected as factory test before enabling OTG.
685 */
686 rc = smbchg_read(chip, &reg, chip->misc_base + IDEV_STS, 1);
687 if (rc < 0) {
688 dev_err(chip->dev, "Couldn't read IDEV_STS rc = %d\n", rc);
689 return false;
690 }
691
692 if ((reg & FMB_STS_MASK) != 0) {
693 pr_smb(PR_STATUS, "IDEV_STS = %02x, not ground\n", reg);
694 return false;
695 }
696
697 rc = smbchg_read(chip, usbid_reg, chip->usb_chgpth_base + USBID_MSB, 2);
698 if (rc < 0) {
699 dev_err(chip->dev, "Couldn't read USBID rc = %d\n", rc);
700 return false;
701 }
702 usbid_val = (usbid_reg[0] << 8) | usbid_reg[1];
703
704 if (usbid_val > USBID_GND_THRESHOLD) {
705 pr_smb(PR_STATUS, "USBID = 0x%04x, too high to be ground\n",
706 usbid_val);
707 return false;
708 }
709
710 rc = smbchg_read(chip, &reg, chip->usb_chgpth_base + RID_STS, 1);
711 if (rc < 0) {
712 dev_err(chip->dev,
713 "Couldn't read usb rid status rc = %d\n", rc);
714 return false;
715 }
716
717 pr_smb(PR_STATUS, "RID_STS = %02x\n", reg);
718
719 return (reg & RID_MASK) == 0;
720}
721
722#define RID_GND_DET_STS BIT(2)
723static bool is_otg_present_schg_lite(struct smbchg_chip *chip)
724{
725 int rc;
726 u8 reg;
727
728 rc = smbchg_read(chip, &reg, chip->otg_base + RT_STS, 1);
729 if (rc < 0) {
730 dev_err(chip->dev,
731 "Couldn't read otg RT status rc = %d\n", rc);
732 return false;
733 }
734
735 return !!(reg & RID_GND_DET_STS);
736}
737
738static bool is_otg_present(struct smbchg_chip *chip)
739{
740 if (chip->schg_version == QPNP_SCHG_LITE)
741 return is_otg_present_schg_lite(chip);
742
743 return is_otg_present_schg(chip);
744}
745
746#define USBIN_9V BIT(5)
747#define USBIN_UNREG BIT(4)
748#define USBIN_LV BIT(3)
749#define DCIN_9V BIT(2)
750#define DCIN_UNREG BIT(1)
751#define DCIN_LV BIT(0)
752#define INPUT_STS 0x0D
753#define DCIN_UV_BIT BIT(0)
754#define DCIN_OV_BIT BIT(1)
755static bool is_dc_present(struct smbchg_chip *chip)
756{
757 int rc;
758 u8 reg;
759
760 rc = smbchg_read(chip, &reg, chip->dc_chgpth_base + RT_STS, 1);
761 if (rc < 0) {
762 dev_err(chip->dev, "Couldn't read dc status rc = %d\n", rc);
763 return false;
764 }
765
766 if ((reg & DCIN_UV_BIT) || (reg & DCIN_OV_BIT))
767 return false;
768
769 return true;
770}
771
772static bool is_usb_present(struct smbchg_chip *chip)
773{
774 int rc;
775 u8 reg;
776
777 rc = smbchg_read(chip, &reg, chip->usb_chgpth_base + RT_STS, 1);
778 if (rc < 0) {
779 dev_err(chip->dev, "Couldn't read usb rt status rc = %d\n", rc);
780 return false;
781 }
782 if (!(reg & USBIN_SRC_DET_BIT) || (reg & USBIN_OV_BIT))
783 return false;
784
785 rc = smbchg_read(chip, &reg, chip->usb_chgpth_base + INPUT_STS, 1);
786 if (rc < 0) {
787 dev_err(chip->dev, "Couldn't read usb status rc = %d\n", rc);
788 return false;
789 }
790
791 return !!(reg & (USBIN_9V | USBIN_UNREG | USBIN_LV));
792}
793
794static char *usb_type_str[] = {
795 "SDP", /* bit 0 */
796 "OTHER", /* bit 1 */
797 "DCP", /* bit 2 */
798 "CDP", /* bit 3 */
799 "NONE", /* bit 4 error case */
800};
801
802#define N_TYPE_BITS 4
803#define TYPE_BITS_OFFSET 4
804
805static int get_type(u8 type_reg)
806{
807 unsigned long type = type_reg;
Kiran Gundaee7cef82017-09-20 17:44:16 +0530808
Kiran Gunda1bc78922017-09-19 13:09:44 +0530809 type >>= TYPE_BITS_OFFSET;
810 return find_first_bit(&type, N_TYPE_BITS);
811}
812
813/* helper to return the string of USB type */
814static inline char *get_usb_type_name(int type)
815{
816 return usb_type_str[type];
817}
818
819static enum power_supply_type usb_type_enum[] = {
820 POWER_SUPPLY_TYPE_USB, /* bit 0 */
821 POWER_SUPPLY_TYPE_USB_DCP, /* bit 1 */
822 POWER_SUPPLY_TYPE_USB_DCP, /* bit 2 */
823 POWER_SUPPLY_TYPE_USB_CDP, /* bit 3 */
824 POWER_SUPPLY_TYPE_USB_DCP, /* bit 4 error case, report DCP */
825};
826
827/* helper to return enum power_supply_type of USB type */
828static inline enum power_supply_type get_usb_supply_type(int type)
829{
830 return usb_type_enum[type];
831}
832
833static bool is_src_detect_high(struct smbchg_chip *chip)
834{
835 int rc;
836 u8 reg;
837
838 rc = smbchg_read(chip, &reg, chip->usb_chgpth_base + RT_STS, 1);
839 if (rc < 0) {
840 dev_err(chip->dev, "Couldn't read usb rt status rc = %d\n", rc);
841 return false;
842 }
843 return reg &= USBIN_SRC_DET_BIT;
844}
845
846static void read_usb_type(struct smbchg_chip *chip, char **usb_type_name,
847 enum power_supply_type *usb_supply_type)
848{
849 int rc, type;
850 u8 reg;
851
852 if (!is_src_detect_high(chip)) {
853 pr_smb(PR_MISC, "src det low\n");
854 *usb_type_name = "Absent";
855 *usb_supply_type = POWER_SUPPLY_TYPE_UNKNOWN;
856 return;
857 }
858
859 rc = smbchg_read(chip, &reg, chip->misc_base + IDEV_STS, 1);
860 if (rc < 0) {
861 dev_err(chip->dev, "Couldn't read status 5 rc = %d\n", rc);
862 *usb_type_name = "Other";
863 *usb_supply_type = POWER_SUPPLY_TYPE_UNKNOWN;
864 return;
865 }
866 type = get_type(reg);
867 *usb_type_name = get_usb_type_name(type);
868 *usb_supply_type = get_usb_supply_type(type);
869}
870
871#define CHGR_STS 0x0E
872#define BATT_LESS_THAN_2V BIT(4)
873#define CHG_HOLD_OFF_BIT BIT(3)
874#define CHG_TYPE_MASK SMB_MASK(2, 1)
875#define CHG_TYPE_SHIFT 1
876#define BATT_NOT_CHG_VAL 0x0
877#define BATT_PRE_CHG_VAL 0x1
878#define BATT_FAST_CHG_VAL 0x2
879#define BATT_TAPER_CHG_VAL 0x3
880#define CHG_INHIBIT_BIT BIT(1)
881#define BAT_TCC_REACHED_BIT BIT(7)
882static int get_prop_batt_status(struct smbchg_chip *chip)
883{
884 int rc, status = POWER_SUPPLY_STATUS_DISCHARGING;
885 u8 reg = 0, chg_type;
886 bool charger_present, chg_inhibit;
887
888 charger_present = is_usb_present(chip) | is_dc_present(chip) |
889 chip->hvdcp_3_det_ignore_uv;
890 if (!charger_present)
891 return POWER_SUPPLY_STATUS_DISCHARGING;
892
893 rc = smbchg_read(chip, &reg, chip->chgr_base + RT_STS, 1);
894 if (rc < 0) {
895 dev_err(chip->dev, "Unable to read RT_STS rc = %d\n", rc);
896 return POWER_SUPPLY_STATUS_UNKNOWN;
897 }
898
899 if (reg & BAT_TCC_REACHED_BIT)
900 return POWER_SUPPLY_STATUS_FULL;
901
902 chg_inhibit = reg & CHG_INHIBIT_BIT;
903 if (chg_inhibit)
904 return POWER_SUPPLY_STATUS_FULL;
905
906 rc = smbchg_read(chip, &reg, chip->chgr_base + CHGR_STS, 1);
907 if (rc < 0) {
908 dev_err(chip->dev, "Unable to read CHGR_STS rc = %d\n", rc);
909 return POWER_SUPPLY_STATUS_UNKNOWN;
910 }
911
912 if (reg & CHG_HOLD_OFF_BIT) {
913 /*
914 * when chg hold off happens the battery is
915 * not charging
916 */
917 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
918 goto out;
919 }
920
921 chg_type = (reg & CHG_TYPE_MASK) >> CHG_TYPE_SHIFT;
922
923 if (chg_type == BATT_NOT_CHG_VAL && !chip->hvdcp_3_det_ignore_uv)
924 status = POWER_SUPPLY_STATUS_DISCHARGING;
925 else
926 status = POWER_SUPPLY_STATUS_CHARGING;
927out:
928 pr_smb_rt(PR_MISC, "CHGR_STS = 0x%02x\n", reg);
929 return status;
930}
931
932#define BAT_PRES_STATUS 0x08
933#define BAT_PRES_BIT BIT(7)
934static int get_prop_batt_present(struct smbchg_chip *chip)
935{
936 int rc;
937 u8 reg;
938
939 rc = smbchg_read(chip, &reg, chip->bat_if_base + BAT_PRES_STATUS, 1);
940 if (rc < 0) {
941 dev_err(chip->dev, "Unable to read CHGR_STS rc = %d\n", rc);
942 return 0;
943 }
944
945 return !!(reg & BAT_PRES_BIT);
946}
947
948static int get_prop_charge_type(struct smbchg_chip *chip)
949{
950 int rc;
951 u8 reg, chg_type;
952
953 rc = smbchg_read(chip, &reg, chip->chgr_base + CHGR_STS, 1);
954 if (rc < 0) {
955 dev_err(chip->dev, "Unable to read CHGR_STS rc = %d\n", rc);
956 return 0;
957 }
958
959 chg_type = (reg & CHG_TYPE_MASK) >> CHG_TYPE_SHIFT;
960 if (chg_type == BATT_NOT_CHG_VAL)
961 return POWER_SUPPLY_CHARGE_TYPE_NONE;
962 else if (chg_type == BATT_TAPER_CHG_VAL)
963 return POWER_SUPPLY_CHARGE_TYPE_TAPER;
964 else if (chg_type == BATT_FAST_CHG_VAL)
965 return POWER_SUPPLY_CHARGE_TYPE_FAST;
966 else if (chg_type == BATT_PRE_CHG_VAL)
967 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
968
969 return POWER_SUPPLY_CHARGE_TYPE_NONE;
970}
971
972static int set_property_on_fg(struct smbchg_chip *chip,
973 enum power_supply_property prop, int val)
974{
975 int rc;
976 union power_supply_propval ret = {0, };
977
978 if (!chip->bms_psy && chip->bms_psy_name)
979 chip->bms_psy =
980 power_supply_get_by_name((char *)chip->bms_psy_name);
981 if (!chip->bms_psy) {
982 pr_smb(PR_STATUS, "no bms psy found\n");
983 return -EINVAL;
984 }
985
986 ret.intval = val;
987 rc = power_supply_set_property(chip->bms_psy, prop, &ret);
988 if (rc)
989 pr_smb(PR_STATUS,
990 "bms psy does not allow updating prop %d rc = %d\n",
991 prop, rc);
992
993 return rc;
994}
995
996static int get_property_from_fg(struct smbchg_chip *chip,
997 enum power_supply_property prop, int *val)
998{
999 int rc;
1000 union power_supply_propval ret = {0, };
1001
1002 if (!chip->bms_psy && chip->bms_psy_name)
1003 chip->bms_psy =
1004 power_supply_get_by_name((char *)chip->bms_psy_name);
1005 if (!chip->bms_psy) {
1006 pr_smb(PR_STATUS, "no bms psy found\n");
1007 return -EINVAL;
1008 }
1009
1010 rc = power_supply_get_property(chip->bms_psy, prop, &ret);
1011 if (rc) {
1012 pr_smb(PR_STATUS,
1013 "bms psy doesn't support reading prop %d rc = %d\n",
1014 prop, rc);
1015 return rc;
1016 }
1017
1018 *val = ret.intval;
1019 return rc;
1020}
1021
1022#define DEFAULT_BATT_CAPACITY 50
1023static int get_prop_batt_capacity(struct smbchg_chip *chip)
1024{
1025 int capacity, rc;
1026
1027 if (chip->fake_battery_soc >= 0)
1028 return chip->fake_battery_soc;
1029
1030 rc = get_property_from_fg(chip, POWER_SUPPLY_PROP_CAPACITY, &capacity);
1031 if (rc) {
1032 pr_smb(PR_STATUS, "Couldn't get capacity rc = %d\n", rc);
1033 capacity = DEFAULT_BATT_CAPACITY;
1034 }
1035 return capacity;
1036}
1037
1038#define DEFAULT_BATT_TEMP 200
1039static int get_prop_batt_temp(struct smbchg_chip *chip)
1040{
1041 int temp, rc;
1042
1043 rc = get_property_from_fg(chip, POWER_SUPPLY_PROP_TEMP, &temp);
1044 if (rc) {
1045 pr_smb(PR_STATUS, "Couldn't get temperature rc = %d\n", rc);
1046 temp = DEFAULT_BATT_TEMP;
1047 }
1048 return temp;
1049}
1050
1051#define DEFAULT_BATT_CURRENT_NOW 0
1052static int get_prop_batt_current_now(struct smbchg_chip *chip)
1053{
1054 int ua, rc;
1055
1056 rc = get_property_from_fg(chip, POWER_SUPPLY_PROP_CURRENT_NOW, &ua);
1057 if (rc) {
1058 pr_smb(PR_STATUS, "Couldn't get current rc = %d\n", rc);
1059 ua = DEFAULT_BATT_CURRENT_NOW;
1060 }
1061 return ua;
1062}
1063
1064#define DEFAULT_BATT_VOLTAGE_NOW 0
1065static int get_prop_batt_voltage_now(struct smbchg_chip *chip)
1066{
1067 int uv, rc;
1068
1069 rc = get_property_from_fg(chip, POWER_SUPPLY_PROP_VOLTAGE_NOW, &uv);
1070 if (rc) {
1071 pr_smb(PR_STATUS, "Couldn't get voltage rc = %d\n", rc);
1072 uv = DEFAULT_BATT_VOLTAGE_NOW;
1073 }
1074 return uv;
1075}
1076
1077#define DEFAULT_BATT_VOLTAGE_MAX_DESIGN 4200000
1078static int get_prop_batt_voltage_max_design(struct smbchg_chip *chip)
1079{
1080 int uv, rc;
1081
1082 rc = get_property_from_fg(chip,
1083 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, &uv);
1084 if (rc) {
1085 pr_smb(PR_STATUS, "Couldn't get voltage rc = %d\n", rc);
1086 uv = DEFAULT_BATT_VOLTAGE_MAX_DESIGN;
1087 }
1088 return uv;
1089}
1090
1091static int get_prop_batt_health(struct smbchg_chip *chip)
1092{
1093 if (chip->batt_hot)
1094 return POWER_SUPPLY_HEALTH_OVERHEAT;
1095 else if (chip->batt_cold)
1096 return POWER_SUPPLY_HEALTH_COLD;
1097 else if (chip->batt_warm)
1098 return POWER_SUPPLY_HEALTH_WARM;
1099 else if (chip->batt_cool)
1100 return POWER_SUPPLY_HEALTH_COOL;
1101 else
1102 return POWER_SUPPLY_HEALTH_GOOD;
1103}
1104
1105static void get_property_from_typec(struct smbchg_chip *chip,
1106 enum power_supply_property property,
1107 union power_supply_propval *prop)
1108{
1109 int rc;
1110
1111 rc = power_supply_get_property(chip->typec_psy,
1112 property, prop);
1113 if (rc)
1114 pr_smb(PR_TYPEC,
1115 "typec psy doesn't support reading prop %d rc = %d\n",
1116 property, rc);
1117}
1118
1119static void update_typec_status(struct smbchg_chip *chip)
1120{
1121 union power_supply_propval type = {0, };
1122 union power_supply_propval capability = {0, };
1123
1124 get_property_from_typec(chip, POWER_SUPPLY_PROP_TYPE, &type);
1125 if (type.intval != POWER_SUPPLY_TYPE_UNKNOWN) {
1126 get_property_from_typec(chip,
1127 POWER_SUPPLY_PROP_CURRENT_CAPABILITY,
1128 &capability);
1129 chip->typec_current_ma = capability.intval;
1130 pr_smb(PR_TYPEC, "SMB Type-C mode = %d, current=%d\n",
1131 type.intval, capability.intval);
1132 } else {
1133 pr_smb(PR_TYPEC,
1134 "typec detection not completed continuing with USB update\n");
1135 }
1136}
1137
1138/*
1139 * finds the index of the closest value in the array. If there are two that
1140 * are equally close, the lower index will be returned
1141 */
1142static int find_closest_in_array(const int *arr, int len, int val)
1143{
1144 int i, closest = 0;
1145
1146 if (len == 0)
1147 return closest;
1148 for (i = 0; i < len; i++)
1149 if (abs(val - arr[i]) < abs(val - arr[closest]))
1150 closest = i;
1151
1152 return closest;
1153}
1154
1155/* finds the index of the closest smaller value in the array. */
1156static int find_smaller_in_array(const int *table, int val, int len)
1157{
1158 int i;
1159
1160 for (i = len - 1; i >= 0; i--) {
1161 if (val >= table[i])
1162 break;
1163 }
1164
1165 return i;
1166}
1167
1168static const int iterm_ma_table_8994[] = {
1169 300,
1170 50,
1171 100,
1172 150,
1173 200,
1174 250,
1175 500,
1176 600
1177};
1178
1179static const int iterm_ma_table_8996[] = {
1180 300,
1181 50,
1182 100,
1183 150,
1184 200,
1185 250,
1186 400,
1187 500
1188};
1189
1190static const int usb_ilim_ma_table_8994[] = {
1191 300,
1192 400,
1193 450,
1194 475,
1195 500,
1196 550,
1197 600,
1198 650,
1199 700,
1200 900,
1201 950,
1202 1000,
1203 1100,
1204 1200,
1205 1400,
1206 1450,
1207 1500,
1208 1600,
1209 1800,
1210 1850,
1211 1880,
1212 1910,
1213 1930,
1214 1950,
1215 1970,
1216 2000,
1217 2050,
1218 2100,
1219 2300,
1220 2400,
1221 2500,
1222 3000
1223};
1224
1225static const int usb_ilim_ma_table_8996[] = {
1226 300,
1227 400,
1228 500,
1229 600,
1230 700,
1231 800,
1232 900,
1233 1000,
1234 1100,
1235 1200,
1236 1300,
1237 1400,
1238 1450,
1239 1500,
1240 1550,
1241 1600,
1242 1700,
1243 1800,
1244 1900,
1245 1950,
1246 2000,
1247 2050,
1248 2100,
1249 2200,
1250 2300,
1251 2400,
1252 2500,
1253 2600,
1254 2700,
1255 2800,
1256 2900,
1257 3000
1258};
1259
1260static int dc_ilim_ma_table_8994[] = {
1261 300,
1262 400,
1263 450,
1264 475,
1265 500,
1266 550,
1267 600,
1268 650,
1269 700,
1270 900,
1271 950,
1272 1000,
1273 1100,
1274 1200,
1275 1400,
1276 1450,
1277 1500,
1278 1600,
1279 1800,
1280 1850,
1281 1880,
1282 1910,
1283 1930,
1284 1950,
1285 1970,
1286 2000,
1287};
1288
1289static int dc_ilim_ma_table_8996[] = {
1290 300,
1291 400,
1292 500,
1293 600,
1294 700,
1295 800,
1296 900,
1297 1000,
1298 1100,
1299 1200,
1300 1300,
1301 1400,
1302 1450,
1303 1500,
1304 1550,
1305 1600,
1306 1700,
1307 1800,
1308 1900,
1309 1950,
1310 2000,
1311 2050,
1312 2100,
1313 2200,
1314 2300,
1315 2400,
1316};
1317
1318static const int fcc_comp_table_8994[] = {
1319 250,
1320 700,
1321 900,
1322 1200,
1323};
1324
1325static const int fcc_comp_table_8996[] = {
1326 250,
1327 1100,
1328 1200,
1329 1500,
1330};
1331
1332static const int aicl_rerun_period[] = {
1333 45,
1334 90,
1335 180,
1336 360,
1337};
1338
1339static const int aicl_rerun_period_schg_lite[] = {
1340 3, /* 2.8s */
1341 6, /* 5.6s */
1342 11, /* 11.3s */
1343 23, /* 22.5s */
1344 45,
1345 90,
1346 180,
1347 360,
1348};
1349
1350static void use_pmi8994_tables(struct smbchg_chip *chip)
1351{
1352 chip->tables.usb_ilim_ma_table = usb_ilim_ma_table_8994;
1353 chip->tables.usb_ilim_ma_len = ARRAY_SIZE(usb_ilim_ma_table_8994);
1354 chip->tables.dc_ilim_ma_table = dc_ilim_ma_table_8994;
1355 chip->tables.dc_ilim_ma_len = ARRAY_SIZE(dc_ilim_ma_table_8994);
1356 chip->tables.iterm_ma_table = iterm_ma_table_8994;
1357 chip->tables.iterm_ma_len = ARRAY_SIZE(iterm_ma_table_8994);
1358 chip->tables.fcc_comp_table = fcc_comp_table_8994;
1359 chip->tables.fcc_comp_len = ARRAY_SIZE(fcc_comp_table_8994);
1360 chip->tables.rchg_thr_mv = 200;
1361 chip->tables.aicl_rerun_period_table = aicl_rerun_period;
1362 chip->tables.aicl_rerun_period_len = ARRAY_SIZE(aicl_rerun_period);
1363}
1364
1365static void use_pmi8996_tables(struct smbchg_chip *chip)
1366{
1367 chip->tables.usb_ilim_ma_table = usb_ilim_ma_table_8996;
1368 chip->tables.usb_ilim_ma_len = ARRAY_SIZE(usb_ilim_ma_table_8996);
1369 chip->tables.dc_ilim_ma_table = dc_ilim_ma_table_8996;
1370 chip->tables.dc_ilim_ma_len = ARRAY_SIZE(dc_ilim_ma_table_8996);
1371 chip->tables.iterm_ma_table = iterm_ma_table_8996;
1372 chip->tables.iterm_ma_len = ARRAY_SIZE(iterm_ma_table_8996);
1373 chip->tables.fcc_comp_table = fcc_comp_table_8996;
1374 chip->tables.fcc_comp_len = ARRAY_SIZE(fcc_comp_table_8996);
1375 chip->tables.rchg_thr_mv = 150;
1376 chip->tables.aicl_rerun_period_table = aicl_rerun_period;
1377 chip->tables.aicl_rerun_period_len = ARRAY_SIZE(aicl_rerun_period);
1378}
1379
1380#define CMD_CHG_REG 0x42
1381#define EN_BAT_CHG_BIT BIT(1)
1382static int smbchg_charging_en(struct smbchg_chip *chip, bool en)
1383{
1384 /* The en bit is configured active low */
1385 return smbchg_masked_write(chip, chip->bat_if_base + CMD_CHG_REG,
1386 EN_BAT_CHG_BIT, en ? 0 : EN_BAT_CHG_BIT);
1387}
1388
1389#define CMD_IL 0x40
1390#define USBIN_SUSPEND_BIT BIT(4)
1391#define CURRENT_100_MA 100
1392#define CURRENT_150_MA 150
1393#define CURRENT_500_MA 500
1394#define CURRENT_900_MA 900
1395#define CURRENT_1500_MA 1500
1396#define SUSPEND_CURRENT_MA 2
1397#define ICL_OVERRIDE_BIT BIT(2)
1398static int smbchg_usb_suspend(struct smbchg_chip *chip, bool suspend)
1399{
1400 int rc;
1401
1402 rc = smbchg_masked_write(chip, chip->usb_chgpth_base + CMD_IL,
1403 USBIN_SUSPEND_BIT, suspend ? USBIN_SUSPEND_BIT : 0);
1404 if (rc < 0)
1405 dev_err(chip->dev, "Couldn't set usb suspend rc = %d\n", rc);
1406 return rc;
1407}
1408
1409#define DCIN_SUSPEND_BIT BIT(3)
1410static int smbchg_dc_suspend(struct smbchg_chip *chip, bool suspend)
1411{
1412 int rc = 0;
1413
1414 rc = smbchg_masked_write(chip, chip->usb_chgpth_base + CMD_IL,
1415 DCIN_SUSPEND_BIT, suspend ? DCIN_SUSPEND_BIT : 0);
1416 if (rc < 0)
1417 dev_err(chip->dev, "Couldn't set dc suspend rc = %d\n", rc);
1418 return rc;
1419}
1420
1421#define IL_CFG 0xF2
1422#define DCIN_INPUT_MASK SMB_MASK(4, 0)
1423static int smbchg_set_dc_current_max(struct smbchg_chip *chip, int current_ma)
1424{
1425 int i;
1426 u8 dc_cur_val;
1427
1428 i = find_smaller_in_array(chip->tables.dc_ilim_ma_table,
1429 current_ma, chip->tables.dc_ilim_ma_len);
1430
1431 if (i < 0) {
1432 dev_err(chip->dev, "Cannot find %dma current_table\n",
1433 current_ma);
1434 return -EINVAL;
1435 }
1436
1437 chip->dc_max_current_ma = chip->tables.dc_ilim_ma_table[i];
1438 dc_cur_val = i & DCIN_INPUT_MASK;
1439
1440 pr_smb(PR_STATUS, "dc current set to %d mA\n",
1441 chip->dc_max_current_ma);
1442 return smbchg_sec_masked_write(chip, chip->dc_chgpth_base + IL_CFG,
1443 DCIN_INPUT_MASK, dc_cur_val);
1444}
1445
1446#define AICL_WL_SEL_CFG 0xF5
1447#define AICL_WL_SEL_MASK SMB_MASK(1, 0)
1448#define AICL_WL_SEL_SCHG_LITE_MASK SMB_MASK(2, 0)
1449static int smbchg_set_aicl_rerun_period_s(struct smbchg_chip *chip,
1450 int period_s)
1451{
1452 int i;
1453 u8 reg, mask;
1454
1455 i = find_smaller_in_array(chip->tables.aicl_rerun_period_table,
1456 period_s, chip->tables.aicl_rerun_period_len);
1457
1458 if (i < 0) {
1459 dev_err(chip->dev, "Cannot find %ds in aicl rerun period\n",
1460 period_s);
1461 return -EINVAL;
1462 }
1463
1464 if (chip->schg_version == QPNP_SCHG_LITE)
1465 mask = AICL_WL_SEL_SCHG_LITE_MASK;
1466 else
1467 mask = AICL_WL_SEL_MASK;
1468
1469 reg = i & mask;
1470
1471 pr_smb(PR_STATUS, "aicl rerun period set to %ds\n",
1472 chip->tables.aicl_rerun_period_table[i]);
1473 return smbchg_sec_masked_write(chip,
1474 chip->dc_chgpth_base + AICL_WL_SEL_CFG,
1475 mask, reg);
1476}
1477
1478static struct power_supply *get_parallel_psy(struct smbchg_chip *chip)
1479{
1480 if (!chip->parallel.avail)
1481 return NULL;
1482 if (chip->parallel.psy)
1483 return chip->parallel.psy;
1484 chip->parallel.psy = power_supply_get_by_name("usb-parallel");
1485 if (!chip->parallel.psy)
1486 pr_smb(PR_STATUS, "parallel charger not found\n");
1487 return chip->parallel.psy;
1488}
1489
1490static void smbchg_usb_update_online_work(struct work_struct *work)
1491{
1492 struct smbchg_chip *chip = container_of(work,
1493 struct smbchg_chip,
1494 usb_set_online_work);
1495 bool user_enabled = !get_client_vote(chip->usb_suspend_votable,
1496 USER_EN_VOTER);
1497 int online;
1498
1499 online = user_enabled && chip->usb_present && !chip->very_weak_charger;
1500
1501 mutex_lock(&chip->usb_set_online_lock);
1502 if (chip->usb_online != online) {
1503 pr_smb(PR_MISC, "setting usb psy online = %d\n", online);
1504 chip->usb_online = online;
1505 power_supply_changed(chip->usb_psy);
1506 }
1507 mutex_unlock(&chip->usb_set_online_lock);
1508}
1509
1510#define CHGPTH_CFG 0xF4
1511#define CFG_USB_2_3_SEL_BIT BIT(7)
1512#define CFG_USB_2 0
1513#define CFG_USB_3 BIT(7)
1514#define USBIN_INPUT_MASK SMB_MASK(4, 0)
1515#define USBIN_MODE_CHG_BIT BIT(0)
1516#define USBIN_LIMITED_MODE 0
1517#define USBIN_HC_MODE BIT(0)
1518#define USB51_MODE_BIT BIT(1)
1519#define USB51_100MA 0
1520#define USB51_500MA BIT(1)
1521static int smbchg_set_high_usb_chg_current(struct smbchg_chip *chip,
1522 int current_ma)
1523{
1524 int i, rc;
1525 u8 usb_cur_val;
1526
1527 if (current_ma == CURRENT_100_MA) {
1528 rc = smbchg_sec_masked_write(chip,
1529 chip->usb_chgpth_base + CHGPTH_CFG,
1530 CFG_USB_2_3_SEL_BIT, CFG_USB_2);
1531 if (rc < 0) {
1532 pr_err("Couldn't set CFG_USB_2 rc=%d\n", rc);
1533 return rc;
1534 }
1535
1536 rc = smbchg_masked_write(chip, chip->usb_chgpth_base + CMD_IL,
1537 USBIN_MODE_CHG_BIT | USB51_MODE_BIT | ICL_OVERRIDE_BIT,
1538 USBIN_LIMITED_MODE | USB51_100MA | ICL_OVERRIDE_BIT);
1539 if (rc < 0) {
1540 pr_err("Couldn't set ICL_OVERRIDE rc=%d\n", rc);
1541 return rc;
1542 }
1543
1544 pr_smb(PR_STATUS,
1545 "Forcing 100mA current limit\n");
1546 chip->usb_max_current_ma = CURRENT_100_MA;
1547 return rc;
1548 }
1549
1550 i = find_smaller_in_array(chip->tables.usb_ilim_ma_table,
1551 current_ma, chip->tables.usb_ilim_ma_len);
1552 if (i < 0) {
1553 dev_err(chip->dev,
1554 "Cannot find %dma current_table using %d\n",
1555 current_ma, CURRENT_150_MA);
1556
1557 rc = smbchg_sec_masked_write(chip,
1558 chip->usb_chgpth_base + CHGPTH_CFG,
1559 CFG_USB_2_3_SEL_BIT, CFG_USB_3);
1560 rc |= smbchg_masked_write(chip, chip->usb_chgpth_base + CMD_IL,
1561 USBIN_MODE_CHG_BIT | USB51_MODE_BIT,
1562 USBIN_LIMITED_MODE | USB51_100MA);
1563 if (rc < 0)
1564 dev_err(chip->dev, "Couldn't set %dmA rc=%d\n",
1565 CURRENT_150_MA, rc);
1566 else
1567 chip->usb_max_current_ma = 150;
1568 return rc;
1569 }
1570
1571 usb_cur_val = i & USBIN_INPUT_MASK;
1572 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + IL_CFG,
1573 USBIN_INPUT_MASK, usb_cur_val);
1574 if (rc < 0) {
1575 dev_err(chip->dev, "cannot write to config c rc = %d\n", rc);
1576 return rc;
1577 }
1578
1579 rc = smbchg_masked_write(chip, chip->usb_chgpth_base + CMD_IL,
1580 USBIN_MODE_CHG_BIT, USBIN_HC_MODE);
1581 if (rc < 0)
1582 dev_err(chip->dev, "Couldn't write cfg 5 rc = %d\n", rc);
1583 chip->usb_max_current_ma = chip->tables.usb_ilim_ma_table[i];
1584 return rc;
1585}
1586
1587/* if APSD results are used
1588 * if SDP is detected it will look at 500mA setting
1589 * if set it will draw 500mA
1590 * if unset it will draw 100mA
1591 * if CDP/DCP it will look at 0x0C setting
1592 * i.e. values in 0x41[1, 0] does not matter
1593 */
1594static int smbchg_set_usb_current_max(struct smbchg_chip *chip,
1595 int current_ma)
1596{
1597 int rc = 0;
1598
1599 /*
1600 * if the battery is not present, do not allow the usb ICL to lower in
1601 * order to avoid browning out the device during a hotswap.
1602 */
1603 if (!chip->batt_present && current_ma < chip->usb_max_current_ma) {
1604 pr_info_ratelimited("Ignoring usb current->%d, battery is absent\n",
1605 current_ma);
1606 return 0;
1607 }
1608 pr_smb(PR_STATUS, "USB current_ma = %d\n", current_ma);
1609
1610 if (current_ma <= SUSPEND_CURRENT_MA) {
1611 /* suspend the usb if current <= 2mA */
1612 rc = vote(chip->usb_suspend_votable, USB_EN_VOTER, true, 0);
1613 chip->usb_max_current_ma = 0;
1614 goto out;
1615 } else {
1616 rc = vote(chip->usb_suspend_votable, USB_EN_VOTER, false, 0);
1617 }
1618
1619 switch (chip->usb_supply_type) {
1620 case POWER_SUPPLY_TYPE_USB:
1621 if ((current_ma < CURRENT_150_MA) &&
1622 (chip->wa_flags & SMBCHG_USB100_WA))
1623 current_ma = CURRENT_150_MA;
1624
1625 if (current_ma < CURRENT_150_MA) {
1626 /* force 100mA */
1627 rc = smbchg_sec_masked_write(chip,
1628 chip->usb_chgpth_base + CHGPTH_CFG,
1629 CFG_USB_2_3_SEL_BIT, CFG_USB_2);
1630 if (rc < 0) {
1631 pr_err("Couldn't set CHGPTH_CFG rc = %d\n", rc);
1632 goto out;
1633 }
1634 rc = smbchg_masked_write(chip,
1635 chip->usb_chgpth_base + CMD_IL,
1636 USBIN_MODE_CHG_BIT | USB51_MODE_BIT,
1637 USBIN_LIMITED_MODE | USB51_100MA);
1638 if (rc < 0) {
1639 pr_err("Couldn't set CMD_IL rc = %d\n", rc);
1640 goto out;
1641 }
1642 chip->usb_max_current_ma = 100;
1643 }
1644 /* specific current values */
1645 if (current_ma == CURRENT_150_MA) {
1646 rc = smbchg_sec_masked_write(chip,
1647 chip->usb_chgpth_base + CHGPTH_CFG,
1648 CFG_USB_2_3_SEL_BIT, CFG_USB_3);
1649 if (rc < 0) {
1650 pr_err("Couldn't set CHGPTH_CFG rc = %d\n", rc);
1651 goto out;
1652 }
1653 rc = smbchg_masked_write(chip,
1654 chip->usb_chgpth_base + CMD_IL,
1655 USBIN_MODE_CHG_BIT | USB51_MODE_BIT,
1656 USBIN_LIMITED_MODE | USB51_100MA);
1657 if (rc < 0) {
1658 pr_err("Couldn't set CMD_IL rc = %d\n", rc);
1659 goto out;
1660 }
1661 chip->usb_max_current_ma = 150;
1662 }
1663 if (current_ma == CURRENT_500_MA) {
1664 rc = smbchg_sec_masked_write(chip,
1665 chip->usb_chgpth_base + CHGPTH_CFG,
1666 CFG_USB_2_3_SEL_BIT, CFG_USB_2);
1667 if (rc < 0) {
1668 pr_err("Couldn't set CHGPTH_CFG rc = %d\n", rc);
1669 goto out;
1670 }
1671 rc = smbchg_masked_write(chip,
1672 chip->usb_chgpth_base + CMD_IL,
1673 USBIN_MODE_CHG_BIT | USB51_MODE_BIT,
1674 USBIN_LIMITED_MODE | USB51_500MA);
1675 if (rc < 0) {
1676 pr_err("Couldn't set CMD_IL rc = %d\n", rc);
1677 goto out;
1678 }
1679 chip->usb_max_current_ma = 500;
1680 }
1681 if (current_ma == CURRENT_900_MA) {
1682 rc = smbchg_sec_masked_write(chip,
1683 chip->usb_chgpth_base + CHGPTH_CFG,
1684 CFG_USB_2_3_SEL_BIT, CFG_USB_3);
1685 if (rc < 0) {
1686 pr_err("Couldn't set CHGPTH_CFG rc = %d\n", rc);
1687 goto out;
1688 }
1689 rc = smbchg_masked_write(chip,
1690 chip->usb_chgpth_base + CMD_IL,
1691 USBIN_MODE_CHG_BIT | USB51_MODE_BIT,
1692 USBIN_LIMITED_MODE | USB51_500MA);
1693 if (rc < 0) {
1694 pr_err("Couldn't set CMD_IL rc = %d\n", rc);
1695 goto out;
1696 }
1697 chip->usb_max_current_ma = 900;
1698 }
1699 break;
1700 case POWER_SUPPLY_TYPE_USB_CDP:
1701 if (current_ma < CURRENT_1500_MA) {
1702 /* use override for CDP */
1703 rc = smbchg_masked_write(chip,
1704 chip->usb_chgpth_base + CMD_IL,
1705 ICL_OVERRIDE_BIT, ICL_OVERRIDE_BIT);
1706 if (rc < 0)
1707 pr_err("Couldn't set override rc = %d\n", rc);
1708 }
1709 /* fall through */
1710 default:
1711 rc = smbchg_set_high_usb_chg_current(chip, current_ma);
1712 if (rc < 0)
1713 pr_err("Couldn't set %dmA rc = %d\n", current_ma, rc);
1714 break;
1715 }
1716
1717out:
1718 pr_smb(PR_STATUS, "usb type = %d current set to %d mA\n",
1719 chip->usb_supply_type, chip->usb_max_current_ma);
1720 return rc;
1721}
1722
1723#define USBIN_HVDCP_STS 0x0C
1724#define USBIN_HVDCP_SEL_BIT BIT(4)
1725#define USBIN_HVDCP_SEL_9V_BIT BIT(1)
1726#define SCHG_LITE_USBIN_HVDCP_SEL_9V_BIT BIT(2)
1727#define SCHG_LITE_USBIN_HVDCP_SEL_BIT BIT(0)
1728static int smbchg_get_min_parallel_current_ma(struct smbchg_chip *chip)
1729{
1730 int rc;
1731 u8 reg, hvdcp_sel, hvdcp_sel_9v;
1732
1733 rc = smbchg_read(chip, &reg,
1734 chip->usb_chgpth_base + USBIN_HVDCP_STS, 1);
1735 if (rc < 0) {
1736 dev_err(chip->dev, "Couldn't read usb status rc = %d\n", rc);
1737 return 0;
1738 }
1739 if (chip->schg_version == QPNP_SCHG_LITE) {
1740 hvdcp_sel = SCHG_LITE_USBIN_HVDCP_SEL_BIT;
1741 hvdcp_sel_9v = SCHG_LITE_USBIN_HVDCP_SEL_9V_BIT;
1742 } else {
1743 hvdcp_sel = USBIN_HVDCP_SEL_BIT;
1744 hvdcp_sel_9v = USBIN_HVDCP_SEL_9V_BIT;
1745 }
1746
1747 if ((reg & hvdcp_sel) && (reg & hvdcp_sel_9v))
1748 return chip->parallel.min_9v_current_thr_ma;
1749 return chip->parallel.min_current_thr_ma;
1750}
1751
1752static bool is_hvdcp_present(struct smbchg_chip *chip)
1753{
1754 int rc;
1755 u8 reg, hvdcp_sel;
1756
1757 rc = smbchg_read(chip, &reg,
1758 chip->usb_chgpth_base + USBIN_HVDCP_STS, 1);
1759 if (rc < 0) {
1760 pr_err("Couldn't read hvdcp status rc = %d\n", rc);
1761 return false;
1762 }
1763
1764 pr_smb(PR_STATUS, "HVDCP_STS = 0x%02x\n", reg);
1765 /*
1766 * If a valid HVDCP is detected, notify it to the usb_psy only
1767 * if USB is still present.
1768 */
1769 if (chip->schg_version == QPNP_SCHG_LITE)
1770 hvdcp_sel = SCHG_LITE_USBIN_HVDCP_SEL_BIT;
1771 else
1772 hvdcp_sel = USBIN_HVDCP_SEL_BIT;
1773
1774 if ((reg & hvdcp_sel) && is_usb_present(chip))
1775 return true;
1776
1777 return false;
1778}
1779
1780#define FCC_CFG 0xF2
1781#define FCC_500MA_VAL 0x4
1782#define FCC_MASK SMB_MASK(4, 0)
1783static int smbchg_set_fastchg_current_raw(struct smbchg_chip *chip,
1784 int current_ma)
1785{
1786 int i, rc;
1787 u8 cur_val;
1788
1789 /* the fcc enumerations are the same as the usb currents */
1790 i = find_smaller_in_array(chip->tables.usb_ilim_ma_table,
1791 current_ma, chip->tables.usb_ilim_ma_len);
1792 if (i < 0) {
1793 dev_err(chip->dev,
1794 "Cannot find %dma current_table using %d\n",
1795 current_ma, CURRENT_500_MA);
1796
1797 rc = smbchg_sec_masked_write(chip, chip->chgr_base + FCC_CFG,
1798 FCC_MASK,
1799 FCC_500MA_VAL);
1800 if (rc < 0)
1801 dev_err(chip->dev, "Couldn't set %dmA rc=%d\n",
1802 CURRENT_500_MA, rc);
1803 else
1804 chip->fastchg_current_ma = 500;
1805 return rc;
1806 }
1807
1808 if (chip->tables.usb_ilim_ma_table[i] == chip->fastchg_current_ma) {
1809 pr_smb(PR_STATUS, "skipping fastchg current request: %d\n",
1810 chip->fastchg_current_ma);
1811 return 0;
1812 }
1813
1814 cur_val = i & FCC_MASK;
1815 rc = smbchg_sec_masked_write(chip, chip->chgr_base + FCC_CFG,
1816 FCC_MASK, cur_val);
1817 if (rc < 0) {
1818 dev_err(chip->dev, "cannot write to fcc cfg rc = %d\n", rc);
1819 return rc;
1820 }
1821 pr_smb(PR_STATUS, "fastcharge current requested %d, set to %d\n",
1822 current_ma, chip->tables.usb_ilim_ma_table[cur_val]);
1823
1824 chip->fastchg_current_ma = chip->tables.usb_ilim_ma_table[cur_val];
1825 return rc;
1826}
1827
1828#define ICL_STS_1_REG 0x7
1829#define ICL_STS_2_REG 0x9
1830#define ICL_STS_MASK 0x1F
1831#define AICL_SUSP_BIT BIT(6)
1832#define AICL_STS_BIT BIT(5)
1833#define USBIN_SUSPEND_STS_BIT BIT(3)
1834#define USBIN_ACTIVE_PWR_SRC_BIT BIT(1)
1835#define DCIN_ACTIVE_PWR_SRC_BIT BIT(0)
1836#define PARALLEL_REENABLE_TIMER_MS 1000
1837#define PARALLEL_CHG_THRESHOLD_CURRENT 1800
1838static bool smbchg_is_usbin_active_pwr_src(struct smbchg_chip *chip)
1839{
1840 int rc;
1841 u8 reg;
1842
1843 rc = smbchg_read(chip, &reg,
1844 chip->usb_chgpth_base + ICL_STS_2_REG, 1);
1845 if (rc < 0) {
1846 dev_err(chip->dev, "Could not read usb icl sts 2: %d\n", rc);
1847 return false;
1848 }
1849
1850 return !(reg & USBIN_SUSPEND_STS_BIT)
1851 && (reg & USBIN_ACTIVE_PWR_SRC_BIT);
1852}
1853
1854static int smbchg_parallel_usb_charging_en(struct smbchg_chip *chip, bool en)
1855{
1856 struct power_supply *parallel_psy = get_parallel_psy(chip);
1857 union power_supply_propval pval = {0, };
1858
1859 if (!parallel_psy || !chip->parallel_charger_detected)
1860 return 0;
1861
1862 pval.intval = en;
1863 return power_supply_set_property(parallel_psy,
1864 POWER_SUPPLY_PROP_CHARGING_ENABLED, &pval);
1865}
1866
1867#define ESR_PULSE_CURRENT_DELTA_MA 200
1868static int smbchg_sw_esr_pulse_en(struct smbchg_chip *chip, bool en)
1869{
1870 int rc, fg_current_now, icl_ma;
1871
1872 rc = get_property_from_fg(chip, POWER_SUPPLY_PROP_CURRENT_NOW,
1873 &fg_current_now);
1874 if (rc) {
1875 pr_smb(PR_STATUS, "bms psy does not support OCV\n");
1876 return 0;
1877 }
1878
1879 icl_ma = max(chip->iterm_ma + ESR_PULSE_CURRENT_DELTA_MA,
1880 fg_current_now - ESR_PULSE_CURRENT_DELTA_MA);
1881 rc = vote(chip->fcc_votable, ESR_PULSE_FCC_VOTER, en, icl_ma);
1882 if (rc < 0) {
1883 pr_err("Couldn't Vote FCC en = %d rc = %d\n", en, rc);
1884 return rc;
1885 }
1886 rc = smbchg_parallel_usb_charging_en(chip, !en);
1887 return rc;
1888}
1889
1890#define USB_AICL_CFG 0xF3
1891#define AICL_EN_BIT BIT(2)
1892static void smbchg_rerun_aicl(struct smbchg_chip *chip)
1893{
1894 pr_smb(PR_STATUS, "Rerunning AICL...\n");
1895 smbchg_sec_masked_write(chip, chip->usb_chgpth_base + USB_AICL_CFG,
1896 AICL_EN_BIT, 0);
1897 /* Add a delay so that AICL successfully clears */
1898 msleep(50);
1899 smbchg_sec_masked_write(chip, chip->usb_chgpth_base + USB_AICL_CFG,
1900 AICL_EN_BIT, AICL_EN_BIT);
1901}
1902
1903static void taper_irq_en(struct smbchg_chip *chip, bool en)
1904{
1905 mutex_lock(&chip->taper_irq_lock);
1906 if (en != chip->taper_irq_enabled) {
1907 if (en) {
1908 enable_irq(chip->taper_irq);
1909 enable_irq_wake(chip->taper_irq);
1910 } else {
1911 disable_irq_wake(chip->taper_irq);
1912 disable_irq_nosync(chip->taper_irq);
1913 }
1914 chip->taper_irq_enabled = en;
1915 }
1916 mutex_unlock(&chip->taper_irq_lock);
1917}
1918
1919static int smbchg_get_aicl_level_ma(struct smbchg_chip *chip)
1920{
1921 int rc;
1922 u8 reg;
1923
1924 rc = smbchg_read(chip, &reg,
1925 chip->usb_chgpth_base + ICL_STS_1_REG, 1);
1926 if (rc < 0) {
1927 dev_err(chip->dev, "Could not read usb icl sts 1: %d\n", rc);
1928 return 0;
1929 }
1930 if (reg & AICL_SUSP_BIT) {
1931 pr_warn("AICL suspended: %02x\n", reg);
1932 return 0;
1933 }
1934 reg &= ICL_STS_MASK;
1935 if (reg >= chip->tables.usb_ilim_ma_len) {
1936 pr_warn("invalid AICL value: %02x\n", reg);
1937 return 0;
1938 }
1939 return chip->tables.usb_ilim_ma_table[reg];
1940}
1941
1942static void smbchg_parallel_usb_disable(struct smbchg_chip *chip)
1943{
1944 struct power_supply *parallel_psy = get_parallel_psy(chip);
1945 union power_supply_propval pval = {0, };
1946 int fcc_ma, usb_icl_ma;
1947
1948 if (!parallel_psy || !chip->parallel_charger_detected)
1949 return;
1950 pr_smb(PR_STATUS, "disabling parallel charger\n");
1951 chip->parallel.last_disabled = ktime_get_boottime();
1952 taper_irq_en(chip, false);
1953 chip->parallel.initial_aicl_ma = 0;
1954 chip->parallel.current_max_ma = 0;
1955 pval.intval = SUSPEND_CURRENT_MA * 1000;
1956 power_supply_set_property(parallel_psy, POWER_SUPPLY_PROP_CURRENT_MAX,
1957 &pval);
1958
1959 pval.intval = false;
1960 power_supply_set_property(parallel_psy, POWER_SUPPLY_PROP_PRESENT,
1961 &pval);
1962
1963 fcc_ma = get_effective_result_locked(chip->fcc_votable);
1964 usb_icl_ma = get_effective_result_locked(chip->usb_icl_votable);
1965 if (fcc_ma < 0)
1966 pr_err("no voters for fcc, skip it\n");
1967 else
1968 smbchg_set_fastchg_current_raw(chip, fcc_ma);
1969
1970 if (usb_icl_ma < 0)
1971 pr_err("no voters for usb_icl, skip it\n");
1972 else
1973 smbchg_set_usb_current_max(chip, usb_icl_ma);
1974
1975 smbchg_rerun_aicl(chip);
1976}
1977
1978#define PARALLEL_TAPER_MAX_TRIES 3
1979#define PARALLEL_FCC_PERCENT_REDUCTION 75
1980#define MINIMUM_PARALLEL_FCC_MA 500
1981#define CHG_ERROR_BIT BIT(0)
1982#define BAT_TAPER_MODE_BIT BIT(6)
1983static void smbchg_parallel_usb_taper(struct smbchg_chip *chip)
1984{
1985 struct power_supply *parallel_psy = get_parallel_psy(chip);
1986 union power_supply_propval pval = {0, };
1987 int parallel_fcc_ma, tries = 0;
1988 u8 reg = 0;
1989
1990 if (!parallel_psy || !chip->parallel_charger_detected)
1991 return;
1992
1993 smbchg_stay_awake(chip, PM_PARALLEL_TAPER);
1994try_again:
1995 mutex_lock(&chip->parallel.lock);
1996 if (chip->parallel.current_max_ma == 0) {
1997 pr_smb(PR_STATUS, "Not parallel charging, skipping\n");
1998 goto done;
1999 }
2000 power_supply_get_property(parallel_psy,
2001 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, &pval);
2002 tries += 1;
2003 parallel_fcc_ma = pval.intval / 1000;
2004 pr_smb(PR_STATUS, "try #%d parallel charger fcc = %d\n",
2005 tries, parallel_fcc_ma);
2006 if (parallel_fcc_ma < MINIMUM_PARALLEL_FCC_MA
2007 || tries > PARALLEL_TAPER_MAX_TRIES) {
2008 smbchg_parallel_usb_disable(chip);
2009 goto done;
2010 }
2011 pval.intval = ((parallel_fcc_ma
2012 * PARALLEL_FCC_PERCENT_REDUCTION) / 100);
2013 pr_smb(PR_STATUS, "reducing FCC of parallel charger to %d\n",
2014 pval.intval);
2015 /* Change it to uA */
2016 pval.intval *= 1000;
2017 power_supply_set_property(parallel_psy,
2018 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, &pval);
2019 /*
2020 * sleep here for 100 ms in order to make sure the charger has a chance
2021 * to go back into constant current charging
2022 */
2023 mutex_unlock(&chip->parallel.lock);
2024 msleep(100);
2025
2026 mutex_lock(&chip->parallel.lock);
2027 if (chip->parallel.current_max_ma == 0) {
2028 pr_smb(PR_STATUS, "Not parallel charging, skipping\n");
2029 goto done;
2030 }
2031 smbchg_read(chip, &reg, chip->chgr_base + RT_STS, 1);
2032 if (reg & BAT_TAPER_MODE_BIT) {
2033 mutex_unlock(&chip->parallel.lock);
2034 goto try_again;
2035 }
2036 taper_irq_en(chip, true);
2037done:
2038 mutex_unlock(&chip->parallel.lock);
2039 smbchg_relax(chip, PM_PARALLEL_TAPER);
2040}
2041
2042static void smbchg_parallel_usb_enable(struct smbchg_chip *chip,
2043 int total_current_ma)
2044{
2045 struct power_supply *parallel_psy = get_parallel_psy(chip);
2046 union power_supply_propval pval = {0, };
2047 int new_parallel_cl_ma, set_parallel_cl_ma, new_pmi_cl_ma, rc;
2048 int current_table_index, target_icl_ma;
2049 int fcc_ma, main_fastchg_current_ma;
2050 int target_parallel_fcc_ma, supplied_parallel_fcc_ma;
2051 int parallel_chg_fcc_percent;
2052
2053 if (!parallel_psy || !chip->parallel_charger_detected)
2054 return;
2055
2056 pr_smb(PR_STATUS, "Attempting to enable parallel charger\n");
2057 pval.intval = chip->vfloat_mv + 50;
2058 rc = power_supply_set_property(parallel_psy,
2059 POWER_SUPPLY_PROP_VOLTAGE_MAX, &pval);
2060 if (rc < 0) {
2061 dev_err(chip->dev,
2062 "Couldn't set Vflt on parallel psy rc: %d\n", rc);
2063 return;
2064 }
2065 /* Set USB ICL */
2066 target_icl_ma = get_effective_result_locked(chip->usb_icl_votable);
2067 if (target_icl_ma < 0) {
2068 pr_err("no voters for usb_icl, skip it\n");
2069 return;
2070 }
2071 new_parallel_cl_ma = total_current_ma
2072 * (100 - smbchg_main_chg_icl_percent) / 100;
2073 taper_irq_en(chip, true);
2074
2075 pval.intval = true;
2076 power_supply_set_property(parallel_psy, POWER_SUPPLY_PROP_PRESENT,
2077 &pval);
2078
2079 pval.intval = new_parallel_cl_ma * 1000;
2080 power_supply_set_property(parallel_psy, POWER_SUPPLY_PROP_CURRENT_MAX,
2081 &pval);
2082
2083 /* read back the real amount of current we are getting */
2084 power_supply_get_property(parallel_psy,
2085 POWER_SUPPLY_PROP_CURRENT_MAX, &pval);
2086 set_parallel_cl_ma = pval.intval / 1000;
2087 chip->parallel.current_max_ma = new_parallel_cl_ma;
2088 pr_smb(PR_MISC, "Requested ICL = %d from parallel, got %d\n",
2089 new_parallel_cl_ma, set_parallel_cl_ma);
2090 new_pmi_cl_ma = max(0, target_icl_ma - set_parallel_cl_ma);
2091 pr_smb(PR_STATUS, "New Total USB current = %d[%d, %d]\n",
2092 total_current_ma, new_pmi_cl_ma,
2093 set_parallel_cl_ma);
2094 smbchg_set_usb_current_max(chip, new_pmi_cl_ma);
2095
2096 /* begin splitting the fast charge current */
2097 fcc_ma = get_effective_result_locked(chip->fcc_votable);
2098 if (fcc_ma < 0) {
2099 pr_err("no voters for fcc, skip it\n");
2100 return;
2101 }
2102 parallel_chg_fcc_percent = 100 - smbchg_main_chg_fcc_percent;
2103 target_parallel_fcc_ma = (fcc_ma * parallel_chg_fcc_percent) / 100;
2104 pval.intval = target_parallel_fcc_ma * 1000;
2105 power_supply_set_property(parallel_psy,
2106 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, &pval);
2107 /* check how much actual current is supplied by the parallel charger */
2108 power_supply_get_property(parallel_psy,
2109 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, &pval);
2110 supplied_parallel_fcc_ma = pval.intval / 1000;
2111 pr_smb(PR_MISC, "Requested FCC = %d from parallel, got %d\n",
2112 target_parallel_fcc_ma, supplied_parallel_fcc_ma);
2113
2114 /* then for the main charger, use the left over FCC */
2115 current_table_index = find_smaller_in_array(
2116 chip->tables.usb_ilim_ma_table,
2117 fcc_ma - supplied_parallel_fcc_ma,
2118 chip->tables.usb_ilim_ma_len);
2119 main_fastchg_current_ma =
2120 chip->tables.usb_ilim_ma_table[current_table_index];
2121 smbchg_set_fastchg_current_raw(chip, main_fastchg_current_ma);
2122 pr_smb(PR_STATUS, "FCC = %d[%d, %d]\n", fcc_ma, main_fastchg_current_ma,
2123 supplied_parallel_fcc_ma);
2124
2125 chip->parallel.enabled_once = true;
Kiran Gunda1bc78922017-09-19 13:09:44 +05302126}
2127
2128static bool smbchg_is_parallel_usb_ok(struct smbchg_chip *chip,
2129 int *ret_total_current_ma)
2130{
2131 struct power_supply *parallel_psy = get_parallel_psy(chip);
2132 union power_supply_propval pval = {0, };
2133 int min_current_thr_ma, rc, type;
2134 int total_current_ma, current_limit_ma, parallel_cl_ma;
2135 ktime_t kt_since_last_disable;
2136 u8 reg;
2137 int fcc_ma = get_effective_result_locked(chip->fcc_votable);
2138 const char *fcc_voter
2139 = get_effective_client_locked(chip->fcc_votable);
2140 int usb_icl_ma = get_effective_result_locked(chip->usb_icl_votable);
2141
2142 if (!parallel_psy || !smbchg_parallel_en
2143 || !chip->parallel_charger_detected) {
2144 pr_smb(PR_STATUS, "Parallel charging not enabled\n");
2145 return false;
2146 }
2147
2148 if (fcc_ma < 0) {
2149 pr_err("no voters for fcc! Can't enable parallel\n");
2150 return false;
2151 }
2152 if (usb_icl_ma < 0) {
2153 pr_err("no voters for usb_icl, Can't enable parallel\n");
2154 return false;
2155 }
2156
2157 kt_since_last_disable = ktime_sub(ktime_get_boottime(),
2158 chip->parallel.last_disabled);
2159 if (chip->parallel.current_max_ma == 0
2160 && chip->parallel.enabled_once
2161 && ktime_to_ms(kt_since_last_disable)
2162 < PARALLEL_REENABLE_TIMER_MS) {
2163 pr_smb(PR_STATUS, "Only been %lld since disable, skipping\n",
2164 ktime_to_ms(kt_since_last_disable));
2165 return false;
2166 }
2167
2168 /*
2169 * If the battery is not present, try not to change parallel charging
2170 * from OFF to ON or from ON to OFF, as it could cause the device to
2171 * brown out in the instant that the USB settings are changed.
2172 *
2173 * Only allow parallel charging check to report false (thereby turnin
2174 * off parallel charging) if the battery is still there, or if parallel
2175 * charging is disabled in the first place.
2176 */
2177 if (get_prop_charge_type(chip) != POWER_SUPPLY_CHARGE_TYPE_FAST
2178 && (get_prop_batt_present(chip)
2179 || chip->parallel.current_max_ma == 0)) {
2180 pr_smb(PR_STATUS, "Not in fast charge, skipping\n");
2181 return false;
2182 }
2183
2184 if (get_prop_batt_health(chip) != POWER_SUPPLY_HEALTH_GOOD) {
2185 pr_smb(PR_STATUS, "JEITA active, skipping\n");
2186 return false;
2187 }
2188
2189 rc = smbchg_read(chip, &reg, chip->misc_base + IDEV_STS, 1);
2190 if (rc < 0) {
2191 dev_err(chip->dev, "Couldn't read status 5 rc = %d\n", rc);
2192 return false;
2193 }
2194
2195 type = get_type(reg);
2196 if (get_usb_supply_type(type) == POWER_SUPPLY_TYPE_USB_CDP) {
2197 pr_smb(PR_STATUS, "CDP adapter, skipping\n");
2198 return false;
2199 }
2200
2201 if (get_usb_supply_type(type) == POWER_SUPPLY_TYPE_USB) {
2202 pr_smb(PR_STATUS, "SDP adapter, skipping\n");
2203 return false;
2204 }
2205
2206 /*
2207 * If USBIN is suspended or not the active power source, do not enable
2208 * parallel charging. The device may be charging off of DCIN.
2209 */
2210 if (!smbchg_is_usbin_active_pwr_src(chip)) {
2211 pr_smb(PR_STATUS, "USB not active power source: %02x\n", reg);
2212 return false;
2213 }
2214
2215 min_current_thr_ma = smbchg_get_min_parallel_current_ma(chip);
2216 if (min_current_thr_ma <= 0) {
2217 pr_smb(PR_STATUS, "parallel charger unavailable for thr: %d\n",
2218 min_current_thr_ma);
2219 return false;
2220 }
2221
2222 if (usb_icl_ma < min_current_thr_ma) {
2223 pr_smb(PR_STATUS, "Weak USB chg skip enable: %d < %d\n",
2224 usb_icl_ma, min_current_thr_ma);
2225 return false;
2226 }
2227
2228 if (!fcc_voter)
2229 return false;
2230 /*
2231 * Suspend the parallel charger if the charging current is < 1800 mA
2232 * and is not because of an ESR pulse.
2233 */
2234 if ((strcmp(fcc_voter, ESR_PULSE_FCC_VOTER) == 0)
2235 && fcc_ma < PARALLEL_CHG_THRESHOLD_CURRENT) {
2236 pr_smb(PR_STATUS, "FCC %d lower than %d\n",
2237 fcc_ma,
2238 PARALLEL_CHG_THRESHOLD_CURRENT);
2239 return false;
2240 }
2241
2242 current_limit_ma = smbchg_get_aicl_level_ma(chip);
2243 if (current_limit_ma <= 0)
2244 return false;
2245
2246 if (chip->parallel.initial_aicl_ma == 0) {
2247 if (current_limit_ma < min_current_thr_ma) {
2248 pr_smb(PR_STATUS, "Initial AICL very low: %d < %d\n",
2249 current_limit_ma, min_current_thr_ma);
2250 return false;
2251 }
2252 chip->parallel.initial_aicl_ma = current_limit_ma;
2253 }
2254
2255 power_supply_get_property(parallel_psy,
2256 POWER_SUPPLY_PROP_CURRENT_MAX, &pval);
2257 parallel_cl_ma = pval.intval / 1000;
2258 /*
2259 * Read back the real amount of current we are getting
2260 * Treat 2mA as 0 because that is the suspend current setting
2261 */
2262 if (parallel_cl_ma <= SUSPEND_CURRENT_MA)
2263 parallel_cl_ma = 0;
2264
2265 /*
2266 * Set the parallel charge path's input current limit (ICL)
2267 * to the total current / 2
2268 */
2269 total_current_ma = min(current_limit_ma + parallel_cl_ma, usb_icl_ma);
2270
2271 if (total_current_ma < chip->parallel.initial_aicl_ma
2272 - chip->parallel.allowed_lowering_ma) {
2273 pr_smb(PR_STATUS,
2274 "Total current reduced a lot: %d (%d + %d) < %d - %d\n",
2275 total_current_ma,
2276 current_limit_ma, parallel_cl_ma,
2277 chip->parallel.initial_aicl_ma,
2278 chip->parallel.allowed_lowering_ma);
2279 return false;
2280 }
2281
2282 *ret_total_current_ma = total_current_ma;
2283 return true;
2284}
2285
2286#define PARALLEL_CHARGER_EN_DELAY_MS 500
2287static void smbchg_parallel_usb_en_work(struct work_struct *work)
2288{
2289 struct smbchg_chip *chip = container_of(work,
2290 struct smbchg_chip,
2291 parallel_en_work.work);
2292 int previous_aicl_ma, total_current_ma, aicl_ma;
2293 bool in_progress;
2294
2295 /* do a check to see if the aicl is stable */
2296 previous_aicl_ma = smbchg_get_aicl_level_ma(chip);
2297 msleep(PARALLEL_CHARGER_EN_DELAY_MS);
2298 aicl_ma = smbchg_get_aicl_level_ma(chip);
2299 if (previous_aicl_ma == aicl_ma) {
2300 pr_smb(PR_STATUS, "AICL at %d\n", aicl_ma);
2301 } else {
2302 pr_smb(PR_STATUS,
2303 "AICL changed [%d -> %d], recheck %d ms\n",
2304 previous_aicl_ma, aicl_ma,
2305 PARALLEL_CHARGER_EN_DELAY_MS);
2306 goto recheck;
2307 }
2308
2309 mutex_lock(&chip->parallel.lock);
2310 in_progress = (chip->parallel.current_max_ma != 0);
2311 if (smbchg_is_parallel_usb_ok(chip, &total_current_ma)) {
2312 smbchg_parallel_usb_enable(chip, total_current_ma);
2313 } else {
2314 if (in_progress) {
2315 pr_smb(PR_STATUS, "parallel charging unavailable\n");
2316 smbchg_parallel_usb_disable(chip);
2317 }
2318 }
2319 mutex_unlock(&chip->parallel.lock);
2320 smbchg_relax(chip, PM_PARALLEL_CHECK);
2321 return;
2322
2323recheck:
2324 schedule_delayed_work(&chip->parallel_en_work, 0);
2325}
2326
2327static void smbchg_parallel_usb_check_ok(struct smbchg_chip *chip)
2328{
2329 struct power_supply *parallel_psy = get_parallel_psy(chip);
2330
2331 if (!parallel_psy || !chip->parallel_charger_detected)
2332 return;
2333
2334 smbchg_stay_awake(chip, PM_PARALLEL_CHECK);
2335 schedule_delayed_work(&chip->parallel_en_work, 0);
2336}
2337
2338static int charging_suspend_vote_cb(struct votable *votable, void *data,
2339 int suspend,
2340 const char *client)
2341{
2342 int rc;
2343 struct smbchg_chip *chip = data;
2344
2345 if (suspend < 0) {
2346 pr_err("No voters\n");
2347 suspend = false;
2348 }
2349
2350 rc = smbchg_charging_en(chip, !suspend);
2351 if (rc < 0) {
2352 dev_err(chip->dev,
2353 "Couldn't configure batt chg: 0x%x rc = %d\n",
2354 !suspend, rc);
2355 }
2356
2357 return rc;
2358}
2359
2360static int usb_suspend_vote_cb(struct votable *votable,
2361 void *data,
2362 int suspend,
2363 const char *client)
2364{
2365 int rc;
2366 struct smbchg_chip *chip = data;
2367
2368 if (suspend < 0) {
2369 pr_err("No voters\n");
2370 suspend = false;
2371 }
2372
2373 rc = smbchg_usb_suspend(chip, suspend);
2374 if (rc < 0)
2375 return rc;
2376
2377 if ((strcmp(client, THERMAL_EN_VOTER) == 0)
2378 || (strcmp(client, POWER_SUPPLY_EN_VOTER) == 0)
2379 || (strcmp(client, USER_EN_VOTER) == 0)
2380 || (strcmp(client, FAKE_BATTERY_EN_VOTER) == 0))
2381 smbchg_parallel_usb_check_ok(chip);
2382
2383 return rc;
2384}
2385
2386static int dc_suspend_vote_cb(struct votable *votable,
2387 void *data,
2388 int suspend,
2389 const char *client)
2390{
2391 int rc;
2392 struct smbchg_chip *chip = data;
2393
2394 if (suspend < 0) {
2395 pr_err("No voters\n");
2396 suspend = false;
2397 }
2398
2399 rc = smbchg_dc_suspend(chip, suspend);
2400 if (rc < 0)
2401 return rc;
2402
2403 if (chip->dc_psy_type != -EINVAL && chip->dc_psy)
2404 power_supply_changed(chip->dc_psy);
2405
2406 return rc;
2407}
2408
2409static int set_fastchg_current_vote_cb(struct votable *votable,
2410 void *data,
2411 int fcc_ma,
2412 const char *client)
2413{
2414 struct smbchg_chip *chip = data;
2415 int rc;
2416
2417 if (fcc_ma < 0) {
2418 pr_err("No voters\n");
2419 return 0;
2420 }
2421
2422 if (chip->parallel.current_max_ma == 0) {
2423 rc = smbchg_set_fastchg_current_raw(chip, fcc_ma);
2424 if (rc < 0) {
2425 pr_err("Can't set FCC fcc_ma=%d rc=%d\n", fcc_ma, rc);
2426 return rc;
2427 }
2428 }
2429 /*
2430 * check if parallel charging can be enabled, and if enabled,
2431 * distribute the fcc
2432 */
2433 smbchg_parallel_usb_check_ok(chip);
2434 return 0;
2435}
2436
2437static int smbchg_set_fastchg_current_user(struct smbchg_chip *chip,
2438 int current_ma)
2439{
2440 int rc = 0;
2441
2442 pr_smb(PR_STATUS, "User setting FCC to %d\n", current_ma);
2443
2444 rc = vote(chip->fcc_votable, BATT_TYPE_FCC_VOTER, true, current_ma);
2445 if (rc < 0)
2446 pr_err("Couldn't vote en rc %d\n", rc);
2447 return rc;
2448}
2449
2450static struct ilim_entry *smbchg_wipower_find_entry(struct smbchg_chip *chip,
2451 struct ilim_map *map, int uv)
2452{
2453 int i;
2454 struct ilim_entry *ret = &(chip->wipower_default.entries[0]);
2455
2456 for (i = 0; i < map->num; i++) {
2457 if (is_between(map->entries[i].vmin_uv, map->entries[i].vmax_uv,
2458 uv))
2459 ret = &map->entries[i];
2460 }
2461 return ret;
2462}
2463
2464#define ZIN_ICL_PT 0xFC
2465#define ZIN_ICL_LV 0xFD
2466#define ZIN_ICL_HV 0xFE
2467#define ZIN_ICL_MASK SMB_MASK(4, 0)
2468static int smbchg_dcin_ilim_config(struct smbchg_chip *chip, int offset, int ma)
2469{
2470 int i, rc;
2471
2472 i = find_smaller_in_array(chip->tables.dc_ilim_ma_table,
2473 ma, chip->tables.dc_ilim_ma_len);
2474
2475 if (i < 0)
2476 i = 0;
2477
2478 rc = smbchg_sec_masked_write(chip, chip->bat_if_base + offset,
2479 ZIN_ICL_MASK, i);
2480 if (rc)
2481 dev_err(chip->dev, "Couldn't write bat if offset %d value = %d rc = %d\n",
2482 offset, i, rc);
2483 return rc;
2484}
2485
2486static int smbchg_wipower_ilim_config(struct smbchg_chip *chip,
2487 struct ilim_entry *ilim)
2488{
2489 int rc = 0;
2490
2491 if (chip->current_ilim.icl_pt_ma != ilim->icl_pt_ma) {
2492 rc = smbchg_dcin_ilim_config(chip, ZIN_ICL_PT, ilim->icl_pt_ma);
2493 if (rc)
2494 dev_err(chip->dev, "failed to write batif offset %d %dma rc = %d\n",
2495 ZIN_ICL_PT, ilim->icl_pt_ma, rc);
2496 else
2497 chip->current_ilim.icl_pt_ma = ilim->icl_pt_ma;
2498 }
2499
2500 if (chip->current_ilim.icl_lv_ma != ilim->icl_lv_ma) {
2501 rc = smbchg_dcin_ilim_config(chip, ZIN_ICL_LV, ilim->icl_lv_ma);
2502 if (rc)
2503 dev_err(chip->dev, "failed to write batif offset %d %dma rc = %d\n",
2504 ZIN_ICL_LV, ilim->icl_lv_ma, rc);
2505 else
2506 chip->current_ilim.icl_lv_ma = ilim->icl_lv_ma;
2507 }
2508
2509 if (chip->current_ilim.icl_hv_ma != ilim->icl_hv_ma) {
2510 rc = smbchg_dcin_ilim_config(chip, ZIN_ICL_HV, ilim->icl_hv_ma);
2511 if (rc)
2512 dev_err(chip->dev, "failed to write batif offset %d %dma rc = %d\n",
2513 ZIN_ICL_HV, ilim->icl_hv_ma, rc);
2514 else
2515 chip->current_ilim.icl_hv_ma = ilim->icl_hv_ma;
2516 }
2517 return rc;
2518}
2519
2520static void btm_notify_dcin(enum qpnp_tm_state state, void *ctx);
2521static int smbchg_wipower_dcin_btm_configure(struct smbchg_chip *chip,
2522 struct ilim_entry *ilim)
2523{
2524 int rc;
2525
2526 if (ilim->vmin_uv == chip->current_ilim.vmin_uv
2527 && ilim->vmax_uv == chip->current_ilim.vmax_uv)
2528 return 0;
2529
2530 chip->param.channel = DCIN;
2531 chip->param.btm_ctx = chip;
2532 if (wipower_dcin_interval < ADC_MEAS1_INTERVAL_0MS)
2533 wipower_dcin_interval = ADC_MEAS1_INTERVAL_0MS;
2534
2535 if (wipower_dcin_interval > ADC_MEAS1_INTERVAL_16S)
2536 wipower_dcin_interval = ADC_MEAS1_INTERVAL_16S;
2537
2538 chip->param.timer_interval = wipower_dcin_interval;
2539 chip->param.threshold_notification = &btm_notify_dcin;
2540 chip->param.high_thr = ilim->vmax_uv + wipower_dcin_hyst_uv;
2541 chip->param.low_thr = ilim->vmin_uv - wipower_dcin_hyst_uv;
2542 chip->param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
2543 rc = qpnp_vadc_channel_monitor(chip->vadc_dev, &chip->param);
2544 if (rc) {
2545 dev_err(chip->dev, "Couldn't configure btm for dcin rc = %d\n",
2546 rc);
2547 } else {
2548 chip->current_ilim.vmin_uv = ilim->vmin_uv;
2549 chip->current_ilim.vmax_uv = ilim->vmax_uv;
2550 pr_smb(PR_STATUS, "btm ilim = (%duV %duV %dmA %dmA %dmA)\n",
2551 ilim->vmin_uv, ilim->vmax_uv,
2552 ilim->icl_pt_ma, ilim->icl_lv_ma, ilim->icl_hv_ma);
2553 }
2554 return rc;
2555}
2556
2557static int smbchg_wipower_icl_configure(struct smbchg_chip *chip,
2558 int dcin_uv, bool div2)
2559{
2560 int rc = 0;
2561 struct ilim_map *map = div2 ? &chip->wipower_div2 : &chip->wipower_pt;
2562 struct ilim_entry *ilim = smbchg_wipower_find_entry(chip, map, dcin_uv);
2563
2564 rc = smbchg_wipower_ilim_config(chip, ilim);
2565 if (rc) {
2566 dev_err(chip->dev, "failed to config ilim rc = %d, dcin_uv = %d , div2 = %d, ilim = (%duV %duV %dmA %dmA %dmA)\n",
2567 rc, dcin_uv, div2,
2568 ilim->vmin_uv, ilim->vmax_uv,
2569 ilim->icl_pt_ma, ilim->icl_lv_ma, ilim->icl_hv_ma);
2570 return rc;
2571 }
2572
2573 rc = smbchg_wipower_dcin_btm_configure(chip, ilim);
2574 if (rc) {
2575 dev_err(chip->dev, "failed to config btm rc = %d, dcin_uv = %d , div2 = %d, ilim = (%duV %duV %dmA %dmA %dmA)\n",
2576 rc, dcin_uv, div2,
2577 ilim->vmin_uv, ilim->vmax_uv,
2578 ilim->icl_pt_ma, ilim->icl_lv_ma, ilim->icl_hv_ma);
2579 return rc;
2580 }
2581 chip->wipower_configured = true;
2582 return 0;
2583}
2584
2585static void smbchg_wipower_icl_deconfigure(struct smbchg_chip *chip)
2586{
2587 int rc;
2588 struct ilim_entry *ilim = &(chip->wipower_default.entries[0]);
2589
2590 if (!chip->wipower_configured)
2591 return;
2592
2593 rc = smbchg_wipower_ilim_config(chip, ilim);
2594 if (rc)
2595 dev_err(chip->dev, "Couldn't config default ilim rc = %d\n",
2596 rc);
2597
2598 rc = qpnp_vadc_end_channel_monitor(chip->vadc_dev);
2599 if (rc)
2600 dev_err(chip->dev, "Couldn't de configure btm for dcin rc = %d\n",
2601 rc);
2602
2603 chip->wipower_configured = false;
2604 chip->current_ilim.vmin_uv = 0;
2605 chip->current_ilim.vmax_uv = 0;
2606 chip->current_ilim.icl_pt_ma = ilim->icl_pt_ma;
2607 chip->current_ilim.icl_lv_ma = ilim->icl_lv_ma;
2608 chip->current_ilim.icl_hv_ma = ilim->icl_hv_ma;
2609 pr_smb(PR_WIPOWER, "De config btm\n");
2610}
2611
2612#define FV_STS 0x0C
2613#define DIV2_ACTIVE BIT(7)
2614static void __smbchg_wipower_check(struct smbchg_chip *chip)
2615{
2616 int chg_type;
2617 bool usb_present, dc_present;
2618 int rc;
2619 int dcin_uv;
2620 bool div2;
2621 struct qpnp_vadc_result adc_result;
2622 u8 reg;
2623
2624 if (!wipower_dyn_icl_en) {
2625 smbchg_wipower_icl_deconfigure(chip);
2626 return;
2627 }
2628
2629 chg_type = get_prop_charge_type(chip);
2630 usb_present = is_usb_present(chip);
2631 dc_present = is_dc_present(chip);
2632 if (chg_type != POWER_SUPPLY_CHARGE_TYPE_NONE
2633 && !usb_present
2634 && dc_present
2635 && chip->dc_psy_type == POWER_SUPPLY_TYPE_WIPOWER) {
2636 rc = qpnp_vadc_read(chip->vadc_dev, DCIN, &adc_result);
2637 if (rc) {
2638 pr_smb(PR_STATUS, "error DCIN read rc = %d\n", rc);
2639 return;
2640 }
2641 dcin_uv = adc_result.physical;
2642
2643 /* check div_by_2 */
2644 rc = smbchg_read(chip, &reg, chip->chgr_base + FV_STS, 1);
2645 if (rc) {
2646 pr_smb(PR_STATUS, "error DCIN read rc = %d\n", rc);
2647 return;
2648 }
2649 div2 = !!(reg & DIV2_ACTIVE);
2650
2651 pr_smb(PR_WIPOWER,
2652 "config ICL chg_type = %d usb = %d dc = %d dcin_uv(adc_code) = %d (0x%x) div2 = %d\n",
2653 chg_type, usb_present, dc_present, dcin_uv,
2654 adc_result.adc_code, div2);
2655 smbchg_wipower_icl_configure(chip, dcin_uv, div2);
2656 } else {
2657 pr_smb(PR_WIPOWER,
2658 "deconfig ICL chg_type = %d usb = %d dc = %d\n",
2659 chg_type, usb_present, dc_present);
2660 smbchg_wipower_icl_deconfigure(chip);
2661 }
2662}
2663
2664static void smbchg_wipower_check(struct smbchg_chip *chip)
2665{
2666 if (!chip->wipower_dyn_icl_avail)
2667 return;
2668
2669 mutex_lock(&chip->wipower_config);
2670 __smbchg_wipower_check(chip);
2671 mutex_unlock(&chip->wipower_config);
2672}
2673
2674static void btm_notify_dcin(enum qpnp_tm_state state, void *ctx)
2675{
2676 struct smbchg_chip *chip = ctx;
2677
2678 mutex_lock(&chip->wipower_config);
2679 pr_smb(PR_WIPOWER, "%s state\n",
2680 state == ADC_TM_LOW_STATE ? "low" : "high");
2681 chip->current_ilim.vmin_uv = 0;
2682 chip->current_ilim.vmax_uv = 0;
2683 __smbchg_wipower_check(chip);
2684 mutex_unlock(&chip->wipower_config);
2685}
2686
2687static int force_dcin_icl_write(void *data, u64 val)
2688{
2689 struct smbchg_chip *chip = data;
2690
2691 smbchg_wipower_check(chip);
2692 return 0;
2693}
2694DEFINE_SIMPLE_ATTRIBUTE(force_dcin_icl_ops, NULL,
2695 force_dcin_icl_write, "0x%02llx\n");
2696
2697/*
2698 * set the dc charge path's maximum allowed current draw
2699 * that may be limited by the system's thermal level
2700 */
2701static int set_dc_current_limit_vote_cb(struct votable *votable,
2702 void *data,
2703 int icl_ma,
2704 const char *client)
2705{
2706 struct smbchg_chip *chip = data;
2707
2708 if (icl_ma < 0) {
2709 pr_err("No voters\n");
2710 return 0;
2711 }
2712
2713 return smbchg_set_dc_current_max(chip, icl_ma);
2714}
2715
2716/*
2717 * set the usb charge path's maximum allowed current draw
2718 * that may be limited by the system's thermal level
2719 */
2720static int set_usb_current_limit_vote_cb(struct votable *votable,
2721 void *data,
2722 int icl_ma,
2723 const char *client)
2724{
2725 struct smbchg_chip *chip = data;
2726 int rc, aicl_ma;
2727 const char *effective_id;
2728
2729 if (icl_ma < 0) {
2730 pr_err("No voters\n");
2731 return 0;
2732 }
2733 effective_id = get_effective_client_locked(chip->usb_icl_votable);
2734
2735 if (!effective_id)
2736 return 0;
2737
2738 /* disable parallel charging if HVDCP is voting for 300mA */
2739 if (strcmp(effective_id, HVDCP_ICL_VOTER) == 0)
2740 smbchg_parallel_usb_disable(chip);
2741
2742 if (chip->parallel.current_max_ma == 0) {
2743 rc = smbchg_set_usb_current_max(chip, icl_ma);
2744 if (rc) {
2745 pr_err("Failed to set usb current max: %d\n", rc);
2746 return rc;
2747 }
2748 }
2749
2750 /* skip the aicl rerun if hvdcp icl voter is active */
2751 if (strcmp(effective_id, HVDCP_ICL_VOTER) == 0)
2752 return 0;
2753
2754 aicl_ma = smbchg_get_aicl_level_ma(chip);
2755 if (icl_ma > aicl_ma)
2756 smbchg_rerun_aicl(chip);
2757 smbchg_parallel_usb_check_ok(chip);
2758 return 0;
2759}
2760
2761static int smbchg_system_temp_level_set(struct smbchg_chip *chip,
2762 int lvl_sel)
2763{
2764 int rc = 0;
2765 int prev_therm_lvl;
2766 int thermal_icl_ma;
2767
2768 if (!chip->thermal_mitigation) {
2769 dev_err(chip->dev, "Thermal mitigation not supported\n");
2770 return -EINVAL;
2771 }
2772
2773 if (lvl_sel < 0) {
2774 dev_err(chip->dev, "Unsupported level selected %d\n", lvl_sel);
2775 return -EINVAL;
2776 }
2777
2778 if (lvl_sel >= chip->thermal_levels) {
2779 dev_err(chip->dev, "Unsupported level selected %d forcing %d\n",
2780 lvl_sel, chip->thermal_levels - 1);
2781 lvl_sel = chip->thermal_levels - 1;
2782 }
2783
2784 if (lvl_sel == chip->therm_lvl_sel)
2785 return 0;
2786
2787 mutex_lock(&chip->therm_lvl_lock);
2788 prev_therm_lvl = chip->therm_lvl_sel;
2789 chip->therm_lvl_sel = lvl_sel;
2790 if (chip->therm_lvl_sel == (chip->thermal_levels - 1)) {
2791 /*
2792 * Disable charging if highest value selected by
2793 * setting the DC and USB path in suspend
2794 */
2795 rc = vote(chip->dc_suspend_votable, THERMAL_EN_VOTER, true, 0);
2796 if (rc < 0) {
2797 dev_err(chip->dev,
2798 "Couldn't set dc suspend rc %d\n", rc);
2799 goto out;
2800 }
2801 rc = vote(chip->usb_suspend_votable, THERMAL_EN_VOTER, true, 0);
2802 if (rc < 0) {
2803 dev_err(chip->dev,
2804 "Couldn't set usb suspend rc %d\n", rc);
2805 goto out;
2806 }
2807 goto out;
2808 }
2809
2810 if (chip->therm_lvl_sel == 0) {
2811 rc = vote(chip->usb_icl_votable, THERMAL_ICL_VOTER, false, 0);
2812 if (rc < 0)
2813 pr_err("Couldn't disable USB thermal ICL vote rc=%d\n",
2814 rc);
2815
2816 rc = vote(chip->dc_icl_votable, THERMAL_ICL_VOTER, false, 0);
2817 if (rc < 0)
2818 pr_err("Couldn't disable DC thermal ICL vote rc=%d\n",
2819 rc);
2820 } else {
2821 thermal_icl_ma =
2822 (int)chip->thermal_mitigation[chip->therm_lvl_sel];
2823 rc = vote(chip->usb_icl_votable, THERMAL_ICL_VOTER, true,
2824 thermal_icl_ma);
2825 if (rc < 0)
2826 pr_err("Couldn't vote for USB thermal ICL rc=%d\n", rc);
2827
2828 rc = vote(chip->dc_icl_votable, THERMAL_ICL_VOTER, true,
2829 thermal_icl_ma);
2830 if (rc < 0)
2831 pr_err("Couldn't vote for DC thermal ICL rc=%d\n", rc);
2832 }
2833
2834 if (prev_therm_lvl == chip->thermal_levels - 1) {
2835 /*
2836 * If previously highest value was selected charging must have
2837 * been disabed. Enable charging by taking the DC and USB path
2838 * out of suspend.
2839 */
2840 rc = vote(chip->dc_suspend_votable, THERMAL_EN_VOTER, false, 0);
2841 if (rc < 0) {
2842 dev_err(chip->dev,
2843 "Couldn't set dc suspend rc %d\n", rc);
2844 goto out;
2845 }
2846 rc = vote(chip->usb_suspend_votable, THERMAL_EN_VOTER,
2847 false, 0);
2848 if (rc < 0) {
2849 dev_err(chip->dev,
2850 "Couldn't set usb suspend rc %d\n", rc);
2851 goto out;
2852 }
2853 }
2854out:
2855 mutex_unlock(&chip->therm_lvl_lock);
2856 return rc;
2857}
2858
2859static int smbchg_ibat_ocp_threshold_ua = 4500000;
2860module_param(smbchg_ibat_ocp_threshold_ua, int, 0644);
2861
2862#define UCONV 1000000LL
2863#define MCONV 1000LL
2864#define FLASH_V_THRESHOLD 3000000
2865#define FLASH_VDIP_MARGIN 100000
2866#define VPH_FLASH_VDIP (FLASH_V_THRESHOLD + FLASH_VDIP_MARGIN)
2867#define BUCK_EFFICIENCY 800LL
2868static int smbchg_calc_max_flash_current(struct smbchg_chip *chip)
2869{
2870 int ocv_uv, esr_uohm, rbatt_uohm, ibat_now, rc;
2871 int64_t ibat_flash_ua, avail_flash_ua, avail_flash_power_fw;
2872 int64_t ibat_safe_ua, vin_flash_uv, vph_flash_uv;
2873
2874 rc = get_property_from_fg(chip, POWER_SUPPLY_PROP_VOLTAGE_OCV, &ocv_uv);
2875 if (rc) {
2876 pr_smb(PR_STATUS, "bms psy does not support OCV\n");
2877 return 0;
2878 }
2879
2880 rc = get_property_from_fg(chip, POWER_SUPPLY_PROP_RESISTANCE,
2881 &esr_uohm);
2882 if (rc) {
2883 pr_smb(PR_STATUS, "bms psy does not support resistance\n");
2884 return 0;
2885 }
2886
2887 rc = msm_bcl_read(BCL_PARAM_CURRENT, &ibat_now);
2888 if (rc) {
2889 pr_smb(PR_STATUS, "BCL current read failed: %d\n", rc);
2890 return 0;
2891 }
2892
2893 rbatt_uohm = esr_uohm + chip->rpara_uohm + chip->rslow_uohm;
2894 /*
2895 * Calculate the maximum current that can pulled out of the battery
2896 * before the battery voltage dips below a safe threshold.
2897 */
2898 ibat_safe_ua = div_s64((ocv_uv - VPH_FLASH_VDIP) * UCONV,
2899 rbatt_uohm);
2900
2901 if (ibat_safe_ua <= smbchg_ibat_ocp_threshold_ua) {
2902 /*
2903 * If the calculated current is below the OCP threshold, then
2904 * use it as the possible flash current.
2905 */
2906 ibat_flash_ua = ibat_safe_ua - ibat_now;
2907 vph_flash_uv = VPH_FLASH_VDIP;
2908 } else {
2909 /*
2910 * If the calculated current is above the OCP threshold, then
2911 * use the ocp threshold instead.
2912 *
2913 * Any higher current will be tripping the battery OCP.
2914 */
2915 ibat_flash_ua = smbchg_ibat_ocp_threshold_ua - ibat_now;
2916 vph_flash_uv = ocv_uv - div64_s64((int64_t)rbatt_uohm
2917 * smbchg_ibat_ocp_threshold_ua, UCONV);
2918 }
2919 /* Calculate the input voltage of the flash module. */
2920 vin_flash_uv = max((chip->vled_max_uv + 500000LL),
2921 div64_s64((vph_flash_uv * 1200), 1000));
2922 /* Calculate the available power for the flash module. */
2923 avail_flash_power_fw = BUCK_EFFICIENCY * vph_flash_uv * ibat_flash_ua;
2924 /*
2925 * Calculate the available amount of current the flash module can draw
2926 * before collapsing the battery. (available power/ flash input voltage)
2927 */
2928 avail_flash_ua = div64_s64(avail_flash_power_fw, vin_flash_uv * MCONV);
2929 pr_smb(PR_MISC,
2930 "avail_iflash=%lld, ocv=%d, ibat=%d, rbatt=%d\n",
2931 avail_flash_ua, ocv_uv, ibat_now, rbatt_uohm);
2932 return (int)avail_flash_ua;
2933}
2934
2935#define FCC_CMP_CFG 0xF3
2936#define FCC_COMP_MASK SMB_MASK(1, 0)
2937static int smbchg_fastchg_current_comp_set(struct smbchg_chip *chip,
2938 int comp_current)
2939{
2940 int rc;
2941 u8 i;
2942
2943 for (i = 0; i < chip->tables.fcc_comp_len; i++)
2944 if (comp_current == chip->tables.fcc_comp_table[i])
2945 break;
2946
2947 if (i >= chip->tables.fcc_comp_len)
2948 return -EINVAL;
2949
2950 rc = smbchg_sec_masked_write(chip, chip->chgr_base + FCC_CMP_CFG,
2951 FCC_COMP_MASK, i);
2952
2953 if (rc)
2954 dev_err(chip->dev, "Couldn't set fastchg current comp rc = %d\n",
2955 rc);
2956
2957 return rc;
2958}
2959
2960#define CFG_TCC_REG 0xF9
2961#define CHG_ITERM_MASK SMB_MASK(2, 0)
2962static int smbchg_iterm_set(struct smbchg_chip *chip, int iterm_ma)
2963{
2964 int rc;
2965 u8 reg;
2966
2967 reg = find_closest_in_array(
2968 chip->tables.iterm_ma_table,
2969 chip->tables.iterm_ma_len,
2970 iterm_ma);
2971
2972 rc = smbchg_sec_masked_write(chip,
2973 chip->chgr_base + CFG_TCC_REG,
2974 CHG_ITERM_MASK, reg);
2975 if (rc) {
2976 dev_err(chip->dev,
2977 "Couldn't set iterm rc = %d\n", rc);
2978 return rc;
2979 }
2980 pr_smb(PR_STATUS, "set tcc (%d) to 0x%02x\n",
2981 iterm_ma, reg);
2982 chip->iterm_ma = iterm_ma;
2983
2984 return 0;
2985}
2986
2987#define FV_CMP_CFG 0xF5
2988#define FV_COMP_MASK SMB_MASK(5, 0)
2989static int smbchg_float_voltage_comp_set(struct smbchg_chip *chip, int code)
2990{
2991 int rc;
2992 u8 val;
2993
2994 val = code & FV_COMP_MASK;
2995 rc = smbchg_sec_masked_write(chip, chip->chgr_base + FV_CMP_CFG,
2996 FV_COMP_MASK, val);
2997
2998 if (rc)
2999 dev_err(chip->dev, "Couldn't set float voltage comp rc = %d\n",
3000 rc);
3001
3002 return rc;
3003}
3004
3005#define VFLOAT_CFG_REG 0xF4
3006#define MIN_FLOAT_MV 3600
3007#define MAX_FLOAT_MV 4500
3008#define VFLOAT_MASK SMB_MASK(5, 0)
3009
3010#define MID_RANGE_FLOAT_MV_MIN 3600
3011#define MID_RANGE_FLOAT_MIN_VAL 0x05
3012#define MID_RANGE_FLOAT_STEP_MV 20
3013
3014#define HIGH_RANGE_FLOAT_MIN_MV 4340
3015#define HIGH_RANGE_FLOAT_MIN_VAL 0x2A
3016#define HIGH_RANGE_FLOAT_STEP_MV 10
3017
3018#define VHIGH_RANGE_FLOAT_MIN_MV 4360
3019#define VHIGH_RANGE_FLOAT_MIN_VAL 0x2C
3020#define VHIGH_RANGE_FLOAT_STEP_MV 20
3021static int smbchg_float_voltage_set(struct smbchg_chip *chip, int vfloat_mv)
3022{
3023 struct power_supply *parallel_psy = get_parallel_psy(chip);
3024 union power_supply_propval prop;
3025 int rc, delta;
3026 u8 temp;
3027
3028 if ((vfloat_mv < MIN_FLOAT_MV) || (vfloat_mv > MAX_FLOAT_MV)) {
3029 dev_err(chip->dev, "bad float voltage mv =%d asked to set\n",
3030 vfloat_mv);
3031 return -EINVAL;
3032 }
3033
3034 if (vfloat_mv <= HIGH_RANGE_FLOAT_MIN_MV) {
3035 /* mid range */
3036 delta = vfloat_mv - MID_RANGE_FLOAT_MV_MIN;
3037 temp = MID_RANGE_FLOAT_MIN_VAL + delta
3038 / MID_RANGE_FLOAT_STEP_MV;
3039 vfloat_mv -= delta % MID_RANGE_FLOAT_STEP_MV;
3040 } else if (vfloat_mv <= VHIGH_RANGE_FLOAT_MIN_MV) {
3041 /* high range */
3042 delta = vfloat_mv - HIGH_RANGE_FLOAT_MIN_MV;
3043 temp = HIGH_RANGE_FLOAT_MIN_VAL + delta
3044 / HIGH_RANGE_FLOAT_STEP_MV;
3045 vfloat_mv -= delta % HIGH_RANGE_FLOAT_STEP_MV;
3046 } else {
3047 /* very high range */
3048 delta = vfloat_mv - VHIGH_RANGE_FLOAT_MIN_MV;
3049 temp = VHIGH_RANGE_FLOAT_MIN_VAL + delta
3050 / VHIGH_RANGE_FLOAT_STEP_MV;
3051 vfloat_mv -= delta % VHIGH_RANGE_FLOAT_STEP_MV;
3052 }
3053
3054 if (parallel_psy) {
3055 prop.intval = vfloat_mv + 50;
3056 rc = power_supply_set_property(parallel_psy,
3057 POWER_SUPPLY_PROP_VOLTAGE_MAX, &prop);
3058 if (rc)
3059 dev_err(chip->dev, "Couldn't set float voltage on parallel psy rc: %d\n",
3060 rc);
3061 }
3062
3063 rc = smbchg_sec_masked_write(chip, chip->chgr_base + VFLOAT_CFG_REG,
3064 VFLOAT_MASK, temp);
3065
3066 if (rc)
3067 dev_err(chip->dev, "Couldn't set float voltage rc = %d\n", rc);
3068 else
3069 chip->vfloat_mv = vfloat_mv;
3070
3071 return rc;
3072}
3073
3074static int smbchg_float_voltage_get(struct smbchg_chip *chip)
3075{
3076 return chip->vfloat_mv;
3077}
3078
3079#define SFT_CFG 0xFD
3080#define SFT_EN_MASK SMB_MASK(5, 4)
3081#define SFT_TO_MASK SMB_MASK(3, 2)
3082#define PRECHG_SFT_TO_MASK SMB_MASK(1, 0)
3083#define SFT_TIMER_DISABLE_BIT BIT(5)
3084#define PRECHG_SFT_TIMER_DISABLE_BIT BIT(4)
3085#define SAFETY_TIME_MINUTES_SHIFT 2
3086static int smbchg_safety_timer_enable(struct smbchg_chip *chip, bool enable)
3087{
3088 int rc;
3089 u8 reg;
3090
3091 if (enable == chip->safety_timer_en)
3092 return 0;
3093
3094 if (enable)
3095 reg = 0;
3096 else
3097 reg = SFT_TIMER_DISABLE_BIT | PRECHG_SFT_TIMER_DISABLE_BIT;
3098
3099 rc = smbchg_sec_masked_write(chip, chip->chgr_base + SFT_CFG,
3100 SFT_EN_MASK, reg);
3101 if (rc < 0) {
3102 dev_err(chip->dev,
3103 "Couldn't %s safety timer rc = %d\n",
3104 enable ? "enable" : "disable", rc);
3105 return rc;
3106 }
3107 chip->safety_timer_en = enable;
3108 return 0;
3109}
3110
3111enum skip_reason {
3112 REASON_OTG_ENABLED = BIT(0),
3113 REASON_FLASH_ENABLED = BIT(1)
3114};
3115
3116#define BAT_IF_TRIM7_REG 0xF7
3117#define CFG_750KHZ_BIT BIT(1)
3118#define MISC_CFG_NTC_VOUT_REG 0xF3
3119#define CFG_NTC_VOUT_FSW_BIT BIT(0)
3120static int smbchg_switch_buck_frequency(struct smbchg_chip *chip,
3121 bool flash_active)
3122{
3123 int rc;
3124
3125 if (!(chip->wa_flags & SMBCHG_FLASH_BUCK_SWITCH_FREQ_WA))
3126 return 0;
3127
3128 if (chip->flash_active == flash_active) {
3129 pr_smb(PR_STATUS, "Fsw not changed, flash_active: %d\n",
3130 flash_active);
3131 return 0;
3132 }
3133
3134 /*
3135 * As per the systems team recommendation, before the flash fires,
3136 * buck switching frequency(Fsw) needs to be increased to 1MHz. Once the
3137 * flash is disabled, Fsw needs to be set back to 750KHz.
3138 */
3139 rc = smbchg_sec_masked_write(chip, chip->misc_base +
3140 MISC_CFG_NTC_VOUT_REG, CFG_NTC_VOUT_FSW_BIT,
3141 flash_active ? CFG_NTC_VOUT_FSW_BIT : 0);
3142 if (rc < 0) {
3143 dev_err(chip->dev, "Couldn't set switching frequency multiplier rc=%d\n",
3144 rc);
3145 return rc;
3146 }
3147
3148 rc = smbchg_sec_masked_write(chip, chip->bat_if_base + BAT_IF_TRIM7_REG,
3149 CFG_750KHZ_BIT, flash_active ? 0 : CFG_750KHZ_BIT);
3150 if (rc < 0) {
3151 dev_err(chip->dev, "Cannot set switching freq: %d\n", rc);
3152 return rc;
3153 }
3154
3155 pr_smb(PR_STATUS, "Fsw @ %sHz\n", flash_active ? "1M" : "750K");
3156 chip->flash_active = flash_active;
3157 return 0;
3158}
3159
3160#define OTG_TRIM6 0xF6
3161#define TR_ENB_SKIP_BIT BIT(2)
3162#define OTG_EN_BIT BIT(0)
3163static int smbchg_otg_pulse_skip_disable(struct smbchg_chip *chip,
3164 enum skip_reason reason, bool disable)
3165{
3166 int rc;
3167 bool disabled;
3168
3169 disabled = !!chip->otg_pulse_skip_dis;
3170 pr_smb(PR_STATUS, "%s pulse skip, reason %d\n",
3171 disable ? "disabling" : "enabling", reason);
3172 if (disable)
3173 chip->otg_pulse_skip_dis |= reason;
3174 else
3175 chip->otg_pulse_skip_dis &= ~reason;
3176 if (disabled == !!chip->otg_pulse_skip_dis)
3177 return 0;
3178 disabled = !!chip->otg_pulse_skip_dis;
3179
3180 rc = smbchg_sec_masked_write(chip, chip->otg_base + OTG_TRIM6,
3181 TR_ENB_SKIP_BIT, disabled ? TR_ENB_SKIP_BIT : 0);
3182 if (rc < 0) {
3183 dev_err(chip->dev,
3184 "Couldn't %s otg pulse skip rc = %d\n",
3185 disabled ? "disable" : "enable", rc);
3186 return rc;
3187 }
3188 pr_smb(PR_STATUS, "%s pulse skip\n", disabled ? "disabled" : "enabled");
3189 return 0;
3190}
3191
3192#define LOW_PWR_OPTIONS_REG 0xFF
3193#define FORCE_TLIM_BIT BIT(4)
3194static int smbchg_force_tlim_en(struct smbchg_chip *chip, bool enable)
3195{
3196 int rc;
3197
3198 rc = smbchg_sec_masked_write(chip, chip->otg_base + LOW_PWR_OPTIONS_REG,
3199 FORCE_TLIM_BIT, enable ? FORCE_TLIM_BIT : 0);
3200 if (rc < 0) {
3201 dev_err(chip->dev,
3202 "Couldn't %s otg force tlim rc = %d\n",
3203 enable ? "enable" : "disable", rc);
3204 return rc;
3205 }
3206 return rc;
3207}
3208
3209static void smbchg_vfloat_adjust_check(struct smbchg_chip *chip)
3210{
3211 if (!chip->use_vfloat_adjustments)
3212 return;
3213
3214 smbchg_stay_awake(chip, PM_REASON_VFLOAT_ADJUST);
3215 pr_smb(PR_STATUS, "Starting vfloat adjustments\n");
3216 schedule_delayed_work(&chip->vfloat_adjust_work, 0);
3217}
3218
3219#define FV_STS_REG 0xC
3220#define AICL_INPUT_STS_BIT BIT(6)
3221static bool smbchg_is_input_current_limited(struct smbchg_chip *chip)
3222{
3223 int rc;
3224 u8 reg;
3225
3226 rc = smbchg_read(chip, &reg, chip->chgr_base + FV_STS_REG, 1);
3227 if (rc < 0) {
3228 dev_err(chip->dev, "Couldn't read FV_STS rc=%d\n", rc);
3229 return false;
3230 }
3231
3232 return !!(reg & AICL_INPUT_STS_BIT);
3233}
3234
3235#define SW_ESR_PULSE_MS 1500
3236static void smbchg_cc_esr_wa_check(struct smbchg_chip *chip)
3237{
3238 int rc, esr_count;
3239
3240 if (!(chip->wa_flags & SMBCHG_CC_ESR_WA))
3241 return;
3242
3243 if (!is_usb_present(chip) && !is_dc_present(chip)) {
3244 pr_smb(PR_STATUS, "No inputs present, skipping\n");
3245 return;
3246 }
3247
3248 if (get_prop_charge_type(chip) != POWER_SUPPLY_CHARGE_TYPE_FAST) {
3249 pr_smb(PR_STATUS, "Not in fast charge, skipping\n");
3250 return;
3251 }
3252
3253 if (!smbchg_is_input_current_limited(chip)) {
3254 pr_smb(PR_STATUS, "Not input current limited, skipping\n");
3255 return;
3256 }
3257
3258 set_property_on_fg(chip, POWER_SUPPLY_PROP_UPDATE_NOW, 1);
3259 rc = get_property_from_fg(chip,
3260 POWER_SUPPLY_PROP_ESR_COUNT, &esr_count);
3261 if (rc) {
3262 pr_smb(PR_STATUS,
3263 "could not read ESR counter rc = %d\n", rc);
3264 return;
3265 }
3266
3267 /*
3268 * The esr_count is counting down the number of fuel gauge cycles
3269 * before a ESR pulse is needed.
3270 *
3271 * After a successful ESR pulse, this count is reset to some
3272 * high number like 28. If this reaches 0, then the fuel gauge
3273 * hardware should force a ESR pulse.
3274 *
3275 * However, if the device is in constant current charge mode while
3276 * being input current limited, the ESR pulse will not affect the
3277 * battery current, so the measurement will fail.
3278 *
3279 * As a failsafe, force a manual ESR pulse if this value is read as
3280 * 0.
3281 */
3282 if (esr_count != 0) {
3283 pr_smb(PR_STATUS, "ESR count is not zero, skipping\n");
3284 return;
3285 }
3286
3287 pr_smb(PR_STATUS, "Lowering charge current for ESR pulse\n");
3288 smbchg_stay_awake(chip, PM_ESR_PULSE);
3289 smbchg_sw_esr_pulse_en(chip, true);
3290 msleep(SW_ESR_PULSE_MS);
3291 pr_smb(PR_STATUS, "Raising charge current for ESR pulse\n");
3292 smbchg_relax(chip, PM_ESR_PULSE);
3293 smbchg_sw_esr_pulse_en(chip, false);
3294}
3295
3296static void smbchg_soc_changed(struct smbchg_chip *chip)
3297{
3298 smbchg_cc_esr_wa_check(chip);
3299}
3300
3301#define DC_AICL_CFG 0xF3
3302#define MISC_TRIM_OPT_15_8 0xF5
3303#define USB_AICL_DEGLITCH_MASK (BIT(5) | BIT(4) | BIT(3))
3304#define USB_AICL_DEGLITCH_SHORT (BIT(5) | BIT(4) | BIT(3))
3305#define USB_AICL_DEGLITCH_LONG 0
3306#define DC_AICL_DEGLITCH_MASK (BIT(5) | BIT(4) | BIT(3))
3307#define DC_AICL_DEGLITCH_SHORT (BIT(5) | BIT(4) | BIT(3))
3308#define DC_AICL_DEGLITCH_LONG 0
3309#define AICL_RERUN_MASK (BIT(5) | BIT(4))
3310#define AICL_RERUN_ON (BIT(5) | BIT(4))
3311#define AICL_RERUN_OFF 0
3312
3313static int smbchg_hw_aicl_rerun_enable_indirect_cb(struct votable *votable,
3314 void *data,
3315 int enable,
3316 const char *client)
3317{
3318 int rc = 0;
3319 struct smbchg_chip *chip = data;
3320
3321 if (enable < 0) {
3322 pr_err("No voters\n");
3323 enable = 0;
3324 }
3325 /*
3326 * If the indirect voting result of all the clients is to enable hw aicl
3327 * rerun, then remove our vote to disable hw aicl rerun
3328 */
3329 rc = vote(chip->hw_aicl_rerun_disable_votable,
3330 HW_AICL_RERUN_ENABLE_INDIRECT_VOTER, !enable, 0);
3331 if (rc < 0) {
3332 pr_err("Couldn't vote for hw rerun rc= %d\n", rc);
3333 return rc;
3334 }
3335
3336 return rc;
3337}
3338
3339static int smbchg_hw_aicl_rerun_disable_cb(struct votable *votable, void *data,
3340 int disable,
3341 const char *client)
3342{
3343 int rc = 0;
3344 struct smbchg_chip *chip = data;
3345
3346 if (disable < 0) {
3347 pr_err("No voters\n");
3348 disable = 0;
3349 }
3350
3351 rc = smbchg_sec_masked_write(chip,
3352 chip->misc_base + MISC_TRIM_OPT_15_8,
3353 AICL_RERUN_MASK, disable ? AICL_RERUN_OFF : AICL_RERUN_ON);
3354 if (rc < 0)
3355 pr_err("Couldn't write to MISC_TRIM_OPTIONS_15_8 rc=%d\n", rc);
3356
3357 return rc;
3358}
3359
3360static int smbchg_aicl_deglitch_config_cb(struct votable *votable, void *data,
3361 int shorter,
3362 const char *client)
3363{
3364 int rc = 0;
3365 struct smbchg_chip *chip = data;
3366
3367 if (shorter < 0) {
3368 pr_err("No voters\n");
3369 shorter = 0;
3370 }
3371
3372 rc = smbchg_sec_masked_write(chip,
3373 chip->usb_chgpth_base + USB_AICL_CFG,
3374 USB_AICL_DEGLITCH_MASK,
3375 shorter ? USB_AICL_DEGLITCH_SHORT : USB_AICL_DEGLITCH_LONG);
3376 if (rc < 0) {
3377 pr_err("Couldn't write to USB_AICL_CFG rc=%d\n", rc);
3378 return rc;
3379 }
3380 rc = smbchg_sec_masked_write(chip,
3381 chip->dc_chgpth_base + DC_AICL_CFG,
3382 DC_AICL_DEGLITCH_MASK,
3383 shorter ? DC_AICL_DEGLITCH_SHORT : DC_AICL_DEGLITCH_LONG);
3384 if (rc < 0) {
3385 pr_err("Couldn't write to DC_AICL_CFG rc=%d\n", rc);
3386 return rc;
3387 }
3388 return rc;
3389}
3390
3391static void smbchg_aicl_deglitch_wa_en(struct smbchg_chip *chip, bool en)
3392{
3393 int rc;
3394
3395 rc = vote(chip->aicl_deglitch_short_votable,
3396 VARB_WORKAROUND_VOTER, en, 0);
3397 if (rc < 0) {
3398 pr_err("Couldn't vote %s deglitch rc=%d\n",
3399 en ? "short" : "long", rc);
3400 return;
3401 }
3402 pr_smb(PR_STATUS, "AICL deglitch set to %s\n", en ? "short" : "long");
3403
3404 rc = vote(chip->hw_aicl_rerun_enable_indirect_votable,
3405 VARB_WORKAROUND_VOTER, en, 0);
3406 if (rc < 0) {
3407 pr_err("Couldn't vote hw aicl rerun rc= %d\n", rc);
3408 return;
3409 }
3410 chip->aicl_deglitch_short = en;
3411}
3412
3413static void smbchg_aicl_deglitch_wa_check(struct smbchg_chip *chip)
3414{
3415 union power_supply_propval prop = {0,};
3416 int rc;
3417 bool low_volt_chgr = true;
3418
3419 if (!(chip->wa_flags & SMBCHG_AICL_DEGLITCH_WA))
3420 return;
3421
3422 if (!is_usb_present(chip) && !is_dc_present(chip)) {
3423 pr_smb(PR_STATUS, "Charger removed\n");
3424 smbchg_aicl_deglitch_wa_en(chip, false);
3425 return;
3426 }
3427
3428 if (!chip->bms_psy)
3429 return;
3430
3431 if (is_usb_present(chip)) {
3432 if (is_hvdcp_present(chip))
3433 low_volt_chgr = false;
3434 } else if (is_dc_present(chip)) {
3435 if (chip->dc_psy_type == POWER_SUPPLY_TYPE_WIPOWER)
3436 low_volt_chgr = false;
3437 else
3438 low_volt_chgr = chip->low_volt_dcin;
3439 }
3440
3441 if (!low_volt_chgr) {
3442 pr_smb(PR_STATUS, "High volt charger! Don't set deglitch\n");
3443 smbchg_aicl_deglitch_wa_en(chip, false);
3444 return;
3445 }
3446
3447 /* It is possible that battery voltage went high above threshold
3448 * when the charger is inserted and can go low because of system
3449 * load. We shouldn't be reconfiguring AICL deglitch when this
3450 * happens as it will lead to oscillation again which is being
3451 * fixed here. Do it once when the battery voltage crosses the
3452 * threshold (e.g. 4.2 V) and clear it only when the charger
3453 * is removed.
3454 */
3455 if (!chip->vbat_above_headroom) {
3456 rc = power_supply_get_property(chip->bms_psy,
3457 POWER_SUPPLY_PROP_VOLTAGE_MIN, &prop);
3458 if (rc < 0) {
3459 pr_err("could not read voltage_min, rc=%d\n", rc);
3460 return;
3461 }
3462 chip->vbat_above_headroom = !prop.intval;
3463 }
3464 smbchg_aicl_deglitch_wa_en(chip, chip->vbat_above_headroom);
3465}
3466
3467#define MISC_TEST_REG 0xE2
3468#define BB_LOOP_DISABLE_ICL BIT(2)
3469static int smbchg_icl_loop_disable_check(struct smbchg_chip *chip)
3470{
3471 bool icl_disabled = !chip->chg_otg_enabled && chip->flash_triggered;
3472 int rc = 0;
3473
3474 if ((chip->wa_flags & SMBCHG_FLASH_ICL_DISABLE_WA)
3475 && icl_disabled != chip->icl_disabled) {
3476 rc = smbchg_sec_masked_write(chip,
3477 chip->misc_base + MISC_TEST_REG,
3478 BB_LOOP_DISABLE_ICL,
3479 icl_disabled ? BB_LOOP_DISABLE_ICL : 0);
3480 chip->icl_disabled = icl_disabled;
3481 }
3482
3483 return rc;
3484}
3485
3486#define UNKNOWN_BATT_TYPE "Unknown Battery"
3487#define LOADING_BATT_TYPE "Loading Battery Data"
3488static int smbchg_config_chg_battery_type(struct smbchg_chip *chip)
3489{
3490 int rc = 0, max_voltage_uv = 0, fastchg_ma = 0, ret = 0, iterm_ua = 0;
3491 struct device_node *batt_node, *profile_node;
3492 struct device_node *node = chip->pdev->dev.of_node;
3493 union power_supply_propval prop = {0,};
3494
3495 rc = power_supply_get_property(chip->bms_psy,
3496 POWER_SUPPLY_PROP_BATTERY_TYPE, &prop);
3497 if (rc) {
3498 pr_smb(PR_STATUS, "Unable to read battery-type rc=%d\n", rc);
3499 return 0;
3500 }
3501 if (!strcmp(prop.strval, UNKNOWN_BATT_TYPE) ||
3502 !strcmp(prop.strval, LOADING_BATT_TYPE)) {
3503 pr_smb(PR_MISC, "Battery-type not identified\n");
3504 return 0;
3505 }
3506 /* quit if there is no change in the battery-type from previous */
3507 if (chip->battery_type && !strcmp(prop.strval, chip->battery_type))
3508 return 0;
3509
3510 chip->battery_type = prop.strval;
3511 batt_node = of_parse_phandle(node, "qcom,battery-data", 0);
3512 if (!batt_node) {
3513 pr_smb(PR_MISC, "No batterydata available\n");
3514 return 0;
3515 }
3516
3517 rc = power_supply_get_property(chip->bms_psy,
3518 POWER_SUPPLY_PROP_RESISTANCE_ID, &prop);
3519 if (rc < 0) {
3520 pr_smb(PR_STATUS, "Unable to read battery-id rc=%d\n", rc);
3521 return 0;
3522 }
3523
3524 profile_node = of_batterydata_get_best_profile(batt_node,
3525 prop.intval / 1000, NULL);
3526 if (IS_ERR_OR_NULL(profile_node)) {
3527 rc = PTR_ERR(profile_node);
3528 pr_err("couldn't find profile handle %d\n", rc);
3529 return rc;
3530 }
3531
3532 /* change vfloat */
3533 rc = of_property_read_u32(profile_node, "qcom,max-voltage-uv",
3534 &max_voltage_uv);
3535 if (rc) {
3536 pr_warn("couldn't find battery max voltage rc=%d\n", rc);
3537 ret = rc;
3538 } else {
3539 if (chip->vfloat_mv != (max_voltage_uv / 1000)) {
3540 pr_info("Vfloat changed from %dmV to %dmV for battery-type %s\n",
3541 chip->vfloat_mv, (max_voltage_uv / 1000),
3542 chip->battery_type);
3543 rc = smbchg_float_voltage_set(chip,
3544 (max_voltage_uv / 1000));
3545 if (rc < 0) {
3546 dev_err(chip->dev,
3547 "Couldn't set float voltage rc = %d\n", rc);
3548 return rc;
3549 }
3550 }
3551 }
3552
3553 /* change chg term */
3554 rc = of_property_read_u32(profile_node, "qcom,chg-term-ua",
3555 &iterm_ua);
3556 if (rc && rc != -EINVAL) {
3557 pr_warn("couldn't read battery term current=%d\n", rc);
3558 ret = rc;
3559 } else if (!rc) {
3560 if (chip->iterm_ma != (iterm_ua / 1000)
3561 && !chip->iterm_disabled) {
3562 pr_info("Term current changed from %dmA to %dmA for battery-type %s\n",
3563 chip->iterm_ma, (iterm_ua / 1000),
3564 chip->battery_type);
3565 rc = smbchg_iterm_set(chip,
3566 (iterm_ua / 1000));
3567 if (rc < 0) {
3568 dev_err(chip->dev,
3569 "Couldn't set iterm rc = %d\n", rc);
3570 return rc;
3571 }
3572 }
3573 chip->iterm_ma = iterm_ua / 1000;
3574 }
3575
3576 /*
3577 * Only configure from profile if fastchg-ma is not defined in the
3578 * charger device node.
3579 */
3580 if (!of_find_property(chip->pdev->dev.of_node,
3581 "qcom,fastchg-current-ma", NULL)) {
3582 rc = of_property_read_u32(profile_node,
3583 "qcom,fastchg-current-ma", &fastchg_ma);
3584 if (rc) {
3585 ret = rc;
3586 } else {
3587 pr_smb(PR_MISC,
3588 "fastchg-ma changed from to %dma for battery-type %s\n",
3589 fastchg_ma, chip->battery_type);
3590 rc = vote(chip->fcc_votable, BATT_TYPE_FCC_VOTER, true,
3591 fastchg_ma);
3592 if (rc < 0) {
3593 dev_err(chip->dev,
3594 "Couldn't vote for fastchg current rc=%d\n",
3595 rc);
3596 return rc;
3597 }
3598 }
3599 }
3600
3601 return ret;
3602}
3603
3604#define MAX_INV_BATT_ID 7700
3605#define MIN_INV_BATT_ID 7300
3606static void check_battery_type(struct smbchg_chip *chip)
3607{
3608 union power_supply_propval prop = {0,};
3609 bool en;
3610
3611 if (!chip->bms_psy && chip->bms_psy_name)
3612 chip->bms_psy =
3613 power_supply_get_by_name((char *)chip->bms_psy_name);
3614 if (chip->bms_psy) {
3615 power_supply_get_property(chip->bms_psy,
3616 POWER_SUPPLY_PROP_BATTERY_TYPE, &prop);
3617 en = (strcmp(prop.strval, UNKNOWN_BATT_TYPE) != 0
3618 || chip->charge_unknown_battery)
3619 && (strcmp(prop.strval, LOADING_BATT_TYPE) != 0);
3620 vote(chip->battchg_suspend_votable,
3621 BATTCHG_UNKNOWN_BATTERY_EN_VOTER, !en, 0);
3622
3623 if (!chip->skip_usb_suspend_for_fake_battery) {
3624 power_supply_get_property(chip->bms_psy,
3625 POWER_SUPPLY_PROP_RESISTANCE_ID, &prop);
3626 /* suspend USB path for invalid battery-id */
3627 en = (prop.intval <= MAX_INV_BATT_ID &&
3628 prop.intval >= MIN_INV_BATT_ID) ? 1 : 0;
3629 vote(chip->usb_suspend_votable, FAKE_BATTERY_EN_VOTER,
3630 en, 0);
3631 }
3632 }
3633}
3634
3635static void smbchg_external_power_changed(struct power_supply *psy)
3636{
3637 struct smbchg_chip *chip = power_supply_get_drvdata(psy);
3638 union power_supply_propval prop = {0,};
3639 int rc, current_limit = 0, soc;
3640 enum power_supply_type usb_supply_type;
3641 char *usb_type_name = "null";
3642
3643 if (chip->bms_psy_name)
3644 chip->bms_psy =
3645 power_supply_get_by_name((char *)chip->bms_psy_name);
3646
3647 smbchg_aicl_deglitch_wa_check(chip);
3648 if (chip->bms_psy) {
3649 check_battery_type(chip);
3650 soc = get_prop_batt_capacity(chip);
3651 if (chip->previous_soc != soc) {
3652 chip->previous_soc = soc;
3653 smbchg_soc_changed(chip);
3654 }
3655
3656 rc = smbchg_config_chg_battery_type(chip);
3657 if (rc)
3658 pr_smb(PR_MISC,
3659 "Couldn't update charger configuration rc=%d\n",
3660 rc);
3661 }
3662
3663 rc = power_supply_get_property(chip->usb_psy,
3664 POWER_SUPPLY_PROP_CHARGING_ENABLED, &prop);
3665 if (rc == 0)
3666 vote(chip->usb_suspend_votable, POWER_SUPPLY_EN_VOTER,
3667 !prop.intval, 0);
3668
3669 current_limit = chip->usb_current_max / 1000;
3670
3671 /* Override if type-c charger used */
3672 if (chip->typec_current_ma > 500 &&
3673 current_limit < chip->typec_current_ma)
3674 current_limit = chip->typec_current_ma;
3675
3676 read_usb_type(chip, &usb_type_name, &usb_supply_type);
3677
3678 if (usb_supply_type != POWER_SUPPLY_TYPE_USB)
3679 goto skip_current_for_non_sdp;
3680
3681 pr_smb(PR_MISC, "usb type = %s current_limit = %d\n",
3682 usb_type_name, current_limit);
3683
3684 rc = vote(chip->usb_icl_votable, PSY_ICL_VOTER, true,
3685 current_limit);
3686 if (rc < 0)
3687 pr_err("Couldn't update USB PSY ICL vote rc=%d\n", rc);
3688
3689skip_current_for_non_sdp:
3690 smbchg_vfloat_adjust_check(chip);
3691
3692 if (chip->batt_psy)
3693 power_supply_changed(chip->batt_psy);
3694}
3695
3696static int smbchg_otg_regulator_enable(struct regulator_dev *rdev)
3697{
3698 int rc = 0;
3699 struct smbchg_chip *chip = rdev_get_drvdata(rdev);
3700
3701 chip->otg_retries = 0;
3702 chip->chg_otg_enabled = true;
3703 smbchg_icl_loop_disable_check(chip);
3704 smbchg_otg_pulse_skip_disable(chip, REASON_OTG_ENABLED, true);
3705
3706 /* If pin control mode then return from here */
3707 if (chip->otg_pinctrl)
3708 return rc;
3709
3710 /* sleep to make sure the pulse skip is actually disabled */
3711 msleep(20);
3712 rc = smbchg_masked_write(chip, chip->bat_if_base + CMD_CHG_REG,
3713 OTG_EN_BIT, OTG_EN_BIT);
3714 if (rc < 0)
3715 dev_err(chip->dev, "Couldn't enable OTG mode rc=%d\n", rc);
3716 else
3717 chip->otg_enable_time = ktime_get();
3718 pr_smb(PR_STATUS, "Enabling OTG Boost\n");
3719 return rc;
3720}
3721
3722static int smbchg_otg_regulator_disable(struct regulator_dev *rdev)
3723{
3724 int rc = 0;
3725 struct smbchg_chip *chip = rdev_get_drvdata(rdev);
3726
3727 if (!chip->otg_pinctrl) {
3728 rc = smbchg_masked_write(chip, chip->bat_if_base + CMD_CHG_REG,
3729 OTG_EN_BIT, 0);
3730 if (rc < 0)
3731 dev_err(chip->dev, "Couldn't disable OTG mode rc=%d\n",
3732 rc);
3733 }
3734
3735 chip->chg_otg_enabled = false;
3736 smbchg_otg_pulse_skip_disable(chip, REASON_OTG_ENABLED, false);
3737 smbchg_icl_loop_disable_check(chip);
3738 pr_smb(PR_STATUS, "Disabling OTG Boost\n");
3739 return rc;
3740}
3741
3742static int smbchg_otg_regulator_is_enable(struct regulator_dev *rdev)
3743{
3744 int rc = 0;
3745 u8 reg = 0;
3746 struct smbchg_chip *chip = rdev_get_drvdata(rdev);
3747
3748 rc = smbchg_read(chip, &reg, chip->bat_if_base + CMD_CHG_REG, 1);
3749 if (rc < 0) {
3750 dev_err(chip->dev,
3751 "Couldn't read OTG enable bit rc=%d\n", rc);
3752 return rc;
3753 }
3754
3755 return (reg & OTG_EN_BIT) ? 1 : 0;
3756}
3757
3758struct regulator_ops smbchg_otg_reg_ops = {
3759 .enable = smbchg_otg_regulator_enable,
3760 .disable = smbchg_otg_regulator_disable,
3761 .is_enabled = smbchg_otg_regulator_is_enable,
3762};
3763
3764#define USBIN_CHGR_CFG 0xF1
3765#define ADAPTER_ALLOWANCE_MASK 0x7
3766#define USBIN_ADAPTER_9V 0x3
3767#define USBIN_ADAPTER_5V_9V_CONT 0x2
3768#define USBIN_ADAPTER_5V_UNREGULATED_9V 0x5
3769#define HVDCP_EN_BIT BIT(3)
3770static int smbchg_external_otg_regulator_enable(struct regulator_dev *rdev)
3771{
3772 int rc = 0;
3773 struct smbchg_chip *chip = rdev_get_drvdata(rdev);
3774
3775 rc = vote(chip->usb_suspend_votable, OTG_EN_VOTER, true, 0);
3776 if (rc < 0) {
3777 dev_err(chip->dev, "Couldn't suspend charger rc=%d\n", rc);
3778 return rc;
3779 }
3780
3781 rc = smbchg_read(chip, &chip->original_usbin_allowance,
3782 chip->usb_chgpth_base + USBIN_CHGR_CFG, 1);
3783 if (rc < 0) {
3784 dev_err(chip->dev, "Couldn't read usb allowance rc=%d\n", rc);
3785 return rc;
3786 }
3787
3788 /*
3789 * To disallow source detect and usbin_uv interrupts, set the adapter
3790 * allowance to 9V, so that the audio boost operating in reverse never
3791 * gets detected as a valid input
3792 */
3793 rc = smbchg_sec_masked_write(chip,
3794 chip->usb_chgpth_base + CHGPTH_CFG,
3795 HVDCP_EN_BIT, 0);
3796 if (rc < 0) {
3797 dev_err(chip->dev, "Couldn't disable HVDCP rc=%d\n", rc);
3798 return rc;
3799 }
3800
3801 rc = smbchg_sec_masked_write(chip,
3802 chip->usb_chgpth_base + USBIN_CHGR_CFG,
3803 0xFF, USBIN_ADAPTER_9V);
3804 if (rc < 0) {
3805 dev_err(chip->dev, "Couldn't write usb allowance rc=%d\n", rc);
3806 return rc;
3807 }
3808
3809 pr_smb(PR_STATUS, "Enabling OTG Boost\n");
3810 return rc;
3811}
3812
3813static int smbchg_external_otg_regulator_disable(struct regulator_dev *rdev)
3814{
3815 int rc = 0;
3816 struct smbchg_chip *chip = rdev_get_drvdata(rdev);
3817
3818 rc = vote(chip->usb_suspend_votable, OTG_EN_VOTER, false, 0);
3819 if (rc < 0) {
3820 dev_err(chip->dev, "Couldn't unsuspend charger rc=%d\n", rc);
3821 return rc;
3822 }
3823
3824 /*
3825 * Reenable HVDCP and set the adapter allowance back to the original
3826 * value in order to allow normal USBs to be recognized as a valid
3827 * input.
3828 */
3829 rc = smbchg_sec_masked_write(chip,
3830 chip->usb_chgpth_base + CHGPTH_CFG,
3831 HVDCP_EN_BIT, HVDCP_EN_BIT);
3832 if (rc < 0) {
3833 dev_err(chip->dev, "Couldn't enable HVDCP rc=%d\n", rc);
3834 return rc;
3835 }
3836
3837 rc = smbchg_sec_masked_write(chip,
3838 chip->usb_chgpth_base + USBIN_CHGR_CFG,
3839 0xFF, chip->original_usbin_allowance);
3840 if (rc < 0) {
3841 dev_err(chip->dev, "Couldn't write usb allowance rc=%d\n", rc);
3842 return rc;
3843 }
3844
3845 pr_smb(PR_STATUS, "Disabling OTG Boost\n");
3846 return rc;
3847}
3848
3849static int smbchg_external_otg_regulator_is_enable(struct regulator_dev *rdev)
3850{
3851 struct smbchg_chip *chip = rdev_get_drvdata(rdev);
3852
3853 return get_client_vote(chip->usb_suspend_votable, OTG_EN_VOTER);
3854}
3855
3856struct regulator_ops smbchg_external_otg_reg_ops = {
3857 .enable = smbchg_external_otg_regulator_enable,
3858 .disable = smbchg_external_otg_regulator_disable,
3859 .is_enabled = smbchg_external_otg_regulator_is_enable,
3860};
3861
3862static int smbchg_regulator_init(struct smbchg_chip *chip)
3863{
3864 int rc = 0;
3865 struct regulator_config cfg = {};
3866 struct device_node *regulator_node;
3867
3868 cfg.dev = chip->dev;
3869 cfg.driver_data = chip;
3870
3871 chip->otg_vreg.rdesc.owner = THIS_MODULE;
3872 chip->otg_vreg.rdesc.type = REGULATOR_VOLTAGE;
3873 chip->otg_vreg.rdesc.ops = &smbchg_otg_reg_ops;
3874 chip->otg_vreg.rdesc.of_match = "qcom,smbcharger-boost-otg";
3875 chip->otg_vreg.rdesc.name = "qcom,smbcharger-boost-otg";
3876
3877 chip->otg_vreg.rdev = devm_regulator_register(chip->dev,
3878 &chip->otg_vreg.rdesc, &cfg);
3879 if (IS_ERR(chip->otg_vreg.rdev)) {
3880 rc = PTR_ERR(chip->otg_vreg.rdev);
3881 chip->otg_vreg.rdev = NULL;
3882 if (rc != -EPROBE_DEFER)
3883 dev_err(chip->dev,
3884 "OTG reg failed, rc=%d\n", rc);
3885 }
3886 if (rc)
3887 return rc;
3888
3889 regulator_node = of_get_child_by_name(chip->dev->of_node,
3890 "qcom,smbcharger-external-otg");
3891 if (!regulator_node) {
3892 dev_dbg(chip->dev, "external-otg node absent\n");
3893 return 0;
3894 }
3895
3896 chip->ext_otg_vreg.rdesc.owner = THIS_MODULE;
3897 chip->ext_otg_vreg.rdesc.type = REGULATOR_VOLTAGE;
3898 chip->ext_otg_vreg.rdesc.ops = &smbchg_external_otg_reg_ops;
3899 chip->ext_otg_vreg.rdesc.of_match = "qcom,smbcharger-external-otg";
3900 chip->ext_otg_vreg.rdesc.name = "qcom,smbcharger-external-otg";
3901 if (of_get_property(chip->dev->of_node, "otg-parent-supply", NULL))
3902 chip->ext_otg_vreg.rdesc.supply_name = "otg-parent";
3903 cfg.dev = chip->dev;
3904 cfg.driver_data = chip;
3905
3906 chip->ext_otg_vreg.rdev = devm_regulator_register(chip->dev,
3907 &chip->ext_otg_vreg.rdesc,
3908 &cfg);
3909 if (IS_ERR(chip->ext_otg_vreg.rdev)) {
3910 rc = PTR_ERR(chip->ext_otg_vreg.rdev);
3911 chip->ext_otg_vreg.rdev = NULL;
3912 if (rc != -EPROBE_DEFER)
3913 dev_err(chip->dev,
3914 "external OTG reg failed, rc=%d\n", rc);
3915 }
3916
3917 return rc;
3918}
3919
3920#define CMD_CHG_LED_REG 0x43
3921#define CHG_LED_CTRL_BIT BIT(0)
3922#define LED_SW_CTRL_BIT 0x1
3923#define LED_CHG_CTRL_BIT 0x0
3924#define CHG_LED_ON 0x03
3925#define CHG_LED_OFF 0x00
3926#define LED_BLINKING_PATTERN1 0x01
3927#define LED_BLINKING_PATTERN2 0x02
3928#define LED_BLINKING_CFG_MASK SMB_MASK(2, 1)
3929#define CHG_LED_SHIFT 1
3930static int smbchg_chg_led_controls(struct smbchg_chip *chip)
3931{
3932 u8 reg, mask;
3933 int rc;
3934
3935 if (chip->cfg_chg_led_sw_ctrl) {
3936 /* turn-off LED by default for software control */
3937 mask = CHG_LED_CTRL_BIT | LED_BLINKING_CFG_MASK;
3938 reg = LED_SW_CTRL_BIT;
3939 } else {
3940 mask = CHG_LED_CTRL_BIT;
3941 reg = LED_CHG_CTRL_BIT;
3942 }
3943
3944 rc = smbchg_masked_write(chip, chip->bat_if_base + CMD_CHG_LED_REG,
3945 mask, reg);
3946 if (rc < 0)
3947 dev_err(chip->dev,
3948 "Couldn't write LED_CTRL_BIT rc=%d\n", rc);
3949 return rc;
3950}
3951
3952static void smbchg_chg_led_brightness_set(struct led_classdev *cdev,
3953 enum led_brightness value)
3954{
3955 struct smbchg_chip *chip = container_of(cdev,
3956 struct smbchg_chip, led_cdev);
3957 union power_supply_propval pval = {0, };
3958 u8 reg;
3959 int rc;
3960
3961 reg = (value > LED_OFF) ? CHG_LED_ON << CHG_LED_SHIFT :
3962 CHG_LED_OFF << CHG_LED_SHIFT;
3963 pval.intval = value > LED_OFF ? 1 : 0;
3964 power_supply_set_property(chip->bms_psy, POWER_SUPPLY_PROP_HI_POWER,
3965 &pval);
3966 pr_smb(PR_STATUS,
3967 "set the charger led brightness to value=%d\n",
3968 value);
3969 rc = smbchg_sec_masked_write(chip,
3970 chip->bat_if_base + CMD_CHG_LED_REG,
3971 LED_BLINKING_CFG_MASK, reg);
3972 if (rc)
3973 dev_err(chip->dev, "Couldn't write CHG_LED rc=%d\n",
3974 rc);
3975}
3976
3977static enum
3978led_brightness smbchg_chg_led_brightness_get(struct led_classdev *cdev)
3979{
3980 struct smbchg_chip *chip = container_of(cdev,
3981 struct smbchg_chip, led_cdev);
3982 u8 reg_val, chg_led_sts;
3983 int rc;
3984
3985 rc = smbchg_read(chip, &reg_val, chip->bat_if_base + CMD_CHG_LED_REG,
3986 1);
3987 if (rc < 0) {
3988 dev_err(chip->dev,
3989 "Couldn't read CHG_LED_REG sts rc=%d\n",
3990 rc);
3991 return rc;
3992 }
3993
3994 chg_led_sts = (reg_val & LED_BLINKING_CFG_MASK) >> CHG_LED_SHIFT;
3995
3996 pr_smb(PR_STATUS, "chg_led_sts = %02x\n", chg_led_sts);
3997
3998 return (chg_led_sts == CHG_LED_OFF) ? LED_OFF : LED_FULL;
3999}
4000
4001static void smbchg_chg_led_blink_set(struct smbchg_chip *chip,
4002 unsigned long blinking)
4003{
4004 union power_supply_propval pval = {0, };
4005 u8 reg;
4006 int rc;
4007
4008 pval.intval = (blinking == 0) ? 0 : 1;
4009 power_supply_set_property(chip->bms_psy, POWER_SUPPLY_PROP_HI_POWER,
4010 &pval);
4011
4012 if (blinking == 0) {
4013 reg = CHG_LED_OFF << CHG_LED_SHIFT;
4014 } else {
4015 if (blinking == 1)
4016 reg = LED_BLINKING_PATTERN1 << CHG_LED_SHIFT;
4017 else if (blinking == 2)
4018 reg = LED_BLINKING_PATTERN2 << CHG_LED_SHIFT;
4019 else
4020 reg = LED_BLINKING_PATTERN1 << CHG_LED_SHIFT;
4021 }
4022
4023 rc = smbchg_sec_masked_write(chip,
4024 chip->bat_if_base + CMD_CHG_LED_REG,
4025 LED_BLINKING_CFG_MASK, reg);
4026 if (rc)
4027 dev_err(chip->dev, "Couldn't write CHG_LED rc=%d\n",
4028 rc);
4029}
4030
4031static ssize_t smbchg_chg_led_blink_store(struct device *dev,
4032 struct device_attribute *attr, const char *buf, size_t len)
4033{
4034 struct led_classdev *cdev = dev_get_drvdata(dev);
4035 struct smbchg_chip *chip = container_of(cdev, struct smbchg_chip,
4036 led_cdev);
4037 unsigned long blinking;
4038 ssize_t rc = -EINVAL;
4039
4040 rc = kstrtoul(buf, 10, &blinking);
4041 if (rc)
4042 return rc;
4043
4044 smbchg_chg_led_blink_set(chip, blinking);
4045
4046 return len;
4047}
4048
4049static DEVICE_ATTR(blink, 0664, NULL, smbchg_chg_led_blink_store);
4050
4051static struct attribute *led_blink_attributes[] = {
4052 &dev_attr_blink.attr,
4053 NULL,
4054};
4055
4056static struct attribute_group smbchg_led_attr_group = {
4057 .attrs = led_blink_attributes
4058};
4059
4060static int smbchg_register_chg_led(struct smbchg_chip *chip)
4061{
4062 int rc;
4063
4064 chip->led_cdev.name = "red";
4065 chip->led_cdev.brightness_set = smbchg_chg_led_brightness_set;
4066 chip->led_cdev.brightness_get = smbchg_chg_led_brightness_get;
4067
4068 rc = led_classdev_register(chip->dev, &chip->led_cdev);
4069 if (rc) {
4070 dev_err(chip->dev, "unable to register charger led, rc=%d\n",
4071 rc);
4072 return rc;
4073 }
4074
4075 rc = sysfs_create_group(&chip->led_cdev.dev->kobj,
4076 &smbchg_led_attr_group);
4077 if (rc) {
4078 dev_err(chip->dev, "led sysfs rc: %d\n", rc);
4079 return rc;
4080 }
4081
4082 return rc;
4083}
4084
4085static int vf_adjust_low_threshold = 5;
4086module_param(vf_adjust_low_threshold, int, 0644);
4087
4088static int vf_adjust_high_threshold = 7;
4089module_param(vf_adjust_high_threshold, int, 0644);
4090
4091static int vf_adjust_n_samples = 10;
4092module_param(vf_adjust_n_samples, int, 0644);
4093
4094static int vf_adjust_max_delta_mv = 40;
4095module_param(vf_adjust_max_delta_mv, int, 0644);
4096
4097static int vf_adjust_trim_steps_per_adjust = 1;
4098module_param(vf_adjust_trim_steps_per_adjust, int, 0644);
4099
4100#define CENTER_TRIM_CODE 7
4101#define MAX_LIN_CODE 14
4102#define MAX_TRIM_CODE 15
4103#define SCALE_SHIFT 4
4104#define VF_TRIM_OFFSET_MASK SMB_MASK(3, 0)
4105#define VF_STEP_SIZE_MV 10
4106#define SCALE_LSB_MV 17
4107static int smbchg_trim_add_steps(int prev_trim, int delta_steps)
4108{
4109 int scale_steps;
4110 int linear_offset, linear_scale;
4111 int offset_code = prev_trim & VF_TRIM_OFFSET_MASK;
4112 int scale_code = (prev_trim & ~VF_TRIM_OFFSET_MASK) >> SCALE_SHIFT;
4113
4114 if (abs(delta_steps) > 1) {
4115 pr_smb(PR_STATUS,
4116 "Cant trim multiple steps delta_steps = %d\n",
4117 delta_steps);
4118 return prev_trim;
4119 }
4120 if (offset_code <= CENTER_TRIM_CODE)
4121 linear_offset = offset_code + CENTER_TRIM_CODE;
4122 else if (offset_code > CENTER_TRIM_CODE)
4123 linear_offset = MAX_TRIM_CODE - offset_code;
4124
4125 if (scale_code <= CENTER_TRIM_CODE)
4126 linear_scale = scale_code + CENTER_TRIM_CODE;
4127 else if (scale_code > CENTER_TRIM_CODE)
4128 linear_scale = scale_code - (CENTER_TRIM_CODE + 1);
4129
Kiran Gundaee7cef82017-09-20 17:44:16 +05304130 /* check if we can accommodate delta steps with just the offset */
Kiran Gunda1bc78922017-09-19 13:09:44 +05304131 if (linear_offset + delta_steps >= 0
4132 && linear_offset + delta_steps <= MAX_LIN_CODE) {
4133 linear_offset += delta_steps;
4134
4135 if (linear_offset > CENTER_TRIM_CODE)
4136 offset_code = linear_offset - CENTER_TRIM_CODE;
4137 else
4138 offset_code = MAX_TRIM_CODE - linear_offset;
4139
4140 return (prev_trim & ~VF_TRIM_OFFSET_MASK) | offset_code;
4141 }
4142
4143 /* changing offset cannot satisfy delta steps, change the scale bits */
4144 scale_steps = delta_steps > 0 ? 1 : -1;
4145
4146 if (linear_scale + scale_steps < 0
4147 || linear_scale + scale_steps > MAX_LIN_CODE) {
4148 pr_smb(PR_STATUS,
4149 "Cant trim scale_steps = %d delta_steps = %d\n",
4150 scale_steps, delta_steps);
4151 return prev_trim;
4152 }
4153
4154 linear_scale += scale_steps;
4155
4156 if (linear_scale > CENTER_TRIM_CODE)
4157 scale_code = linear_scale - CENTER_TRIM_CODE;
4158 else
4159 scale_code = linear_scale + (CENTER_TRIM_CODE + 1);
4160 prev_trim = (prev_trim & VF_TRIM_OFFSET_MASK)
4161 | scale_code << SCALE_SHIFT;
4162
4163 /*
4164 * now that we have changed scale which is a 17mV jump, change the
4165 * offset bits (10mV) too so the effective change is just 7mV
4166 */
4167 delta_steps = -1 * delta_steps;
4168
4169 linear_offset = clamp(linear_offset + delta_steps, 0, MAX_LIN_CODE);
4170 if (linear_offset > CENTER_TRIM_CODE)
4171 offset_code = linear_offset - CENTER_TRIM_CODE;
4172 else
4173 offset_code = MAX_TRIM_CODE - linear_offset;
4174
4175 return (prev_trim & ~VF_TRIM_OFFSET_MASK) | offset_code;
4176}
4177
4178#define TRIM_14 0xFE
4179#define VF_TRIM_MASK 0xFF
4180static int smbchg_adjust_vfloat_mv_trim(struct smbchg_chip *chip,
4181 int delta_mv)
4182{
4183 int sign, delta_steps, rc = 0;
4184 u8 prev_trim, new_trim;
4185 int i;
4186
4187 sign = delta_mv > 0 ? 1 : -1;
4188 delta_steps = (delta_mv + sign * VF_STEP_SIZE_MV / 2)
4189 / VF_STEP_SIZE_MV;
4190
4191 rc = smbchg_read(chip, &prev_trim, chip->misc_base + TRIM_14, 1);
4192 if (rc) {
4193 dev_err(chip->dev, "Unable to read trim 14: %d\n", rc);
4194 return rc;
4195 }
4196
4197 for (i = 1; i <= abs(delta_steps)
4198 && i <= vf_adjust_trim_steps_per_adjust; i++) {
4199 new_trim = (u8)smbchg_trim_add_steps(prev_trim,
4200 delta_steps > 0 ? 1 : -1);
4201 if (new_trim == prev_trim) {
4202 pr_smb(PR_STATUS,
4203 "VFloat trim unchanged from %02x\n", prev_trim);
4204 /* treat no trim change as an error */
4205 return -EINVAL;
4206 }
4207
4208 rc = smbchg_sec_masked_write(chip, chip->misc_base + TRIM_14,
4209 VF_TRIM_MASK, new_trim);
4210 if (rc < 0) {
4211 dev_err(chip->dev,
4212 "Couldn't change vfloat trim rc=%d\n", rc);
4213 }
4214 pr_smb(PR_STATUS,
4215 "VFlt trim %02x to %02x, delta steps: %d\n",
4216 prev_trim, new_trim, delta_steps);
4217 prev_trim = new_trim;
4218 }
4219
4220 return rc;
4221}
4222
4223#define VFLOAT_RESAMPLE_DELAY_MS 10000
4224static void smbchg_vfloat_adjust_work(struct work_struct *work)
4225{
4226 struct smbchg_chip *chip = container_of(work,
4227 struct smbchg_chip,
4228 vfloat_adjust_work.work);
4229 int vbat_uv, vbat_mv, ibat_ua, rc, delta_vfloat_mv;
4230 bool taper, enable;
4231
4232 smbchg_stay_awake(chip, PM_REASON_VFLOAT_ADJUST);
4233 taper = (get_prop_charge_type(chip)
4234 == POWER_SUPPLY_CHARGE_TYPE_TAPER);
4235 enable = taper && (chip->parallel.current_max_ma == 0);
4236
4237 if (!enable) {
4238 pr_smb(PR_MISC,
4239 "Stopping vfloat adj taper=%d parallel_ma = %d\n",
4240 taper, chip->parallel.current_max_ma);
4241 goto stop;
4242 }
4243
4244 if (get_prop_batt_health(chip) != POWER_SUPPLY_HEALTH_GOOD) {
4245 pr_smb(PR_STATUS, "JEITA active, skipping\n");
4246 goto stop;
4247 }
4248
4249 set_property_on_fg(chip, POWER_SUPPLY_PROP_UPDATE_NOW, 1);
4250 rc = get_property_from_fg(chip,
4251 POWER_SUPPLY_PROP_VOLTAGE_NOW, &vbat_uv);
4252 if (rc) {
4253 pr_smb(PR_STATUS,
4254 "bms psy does not support voltage rc = %d\n", rc);
4255 goto stop;
4256 }
4257 vbat_mv = vbat_uv / 1000;
4258
4259 if ((vbat_mv - chip->vfloat_mv) < -1 * vf_adjust_max_delta_mv) {
4260 pr_smb(PR_STATUS, "Skip vbat out of range: %d\n", vbat_mv);
4261 goto reschedule;
4262 }
4263
4264 rc = get_property_from_fg(chip,
4265 POWER_SUPPLY_PROP_CURRENT_NOW, &ibat_ua);
4266 if (rc) {
4267 pr_smb(PR_STATUS,
4268 "bms psy does not support current_now rc = %d\n", rc);
4269 goto stop;
4270 }
4271
4272 if (ibat_ua / 1000 > -chip->iterm_ma) {
4273 pr_smb(PR_STATUS, "Skip ibat too high: %d\n", ibat_ua);
4274 goto reschedule;
4275 }
4276
4277 pr_smb(PR_STATUS, "sample number = %d vbat_mv = %d ibat_ua = %d\n",
4278 chip->n_vbat_samples,
4279 vbat_mv,
4280 ibat_ua);
4281
4282 chip->max_vbat_sample = max(chip->max_vbat_sample, vbat_mv);
4283 chip->n_vbat_samples += 1;
4284 if (chip->n_vbat_samples < vf_adjust_n_samples) {
4285 pr_smb(PR_STATUS, "Skip %d samples; max = %d\n",
4286 chip->n_vbat_samples, chip->max_vbat_sample);
4287 goto reschedule;
4288 }
4289 /* if max vbat > target vfloat, delta_vfloat_mv could be negative */
4290 delta_vfloat_mv = chip->vfloat_mv - chip->max_vbat_sample;
4291 pr_smb(PR_STATUS, "delta_vfloat_mv = %d, samples = %d, mvbat = %d\n",
4292 delta_vfloat_mv, chip->n_vbat_samples, chip->max_vbat_sample);
4293 /*
4294 * enough valid samples has been collected, adjust trim codes
4295 * based on maximum of collected vbat samples if necessary
4296 */
4297 if (delta_vfloat_mv > vf_adjust_high_threshold
4298 || delta_vfloat_mv < -1 * vf_adjust_low_threshold) {
4299 rc = smbchg_adjust_vfloat_mv_trim(chip, delta_vfloat_mv);
4300 if (rc) {
4301 pr_smb(PR_STATUS,
4302 "Stopping vfloat adj after trim adj rc = %d\n",
4303 rc);
4304 goto stop;
4305 }
4306 chip->max_vbat_sample = 0;
4307 chip->n_vbat_samples = 0;
4308 goto reschedule;
4309 }
4310
4311stop:
4312 chip->max_vbat_sample = 0;
4313 chip->n_vbat_samples = 0;
4314 smbchg_relax(chip, PM_REASON_VFLOAT_ADJUST);
4315 return;
4316
4317reschedule:
4318 schedule_delayed_work(&chip->vfloat_adjust_work,
4319 msecs_to_jiffies(VFLOAT_RESAMPLE_DELAY_MS));
Kiran Gunda1bc78922017-09-19 13:09:44 +05304320}
4321
4322static int smbchg_charging_status_change(struct smbchg_chip *chip)
4323{
4324 smbchg_vfloat_adjust_check(chip);
4325 set_property_on_fg(chip, POWER_SUPPLY_PROP_STATUS,
4326 get_prop_batt_status(chip));
4327 return 0;
4328}
4329
4330#define BB_CLMP_SEL 0xF8
4331#define BB_CLMP_MASK SMB_MASK(1, 0)
4332#define BB_CLMP_VFIX_3338MV 0x1
4333#define BB_CLMP_VFIX_3512MV 0x2
4334static int smbchg_set_optimal_charging_mode(struct smbchg_chip *chip, int type)
4335{
4336 int rc;
4337 bool hvdcp2 = (type == POWER_SUPPLY_TYPE_USB_HVDCP
4338 && smbchg_is_usbin_active_pwr_src(chip));
4339
4340 /*
4341 * Set the charger switching freq to 1MHZ if HVDCP 2.0,
4342 * or 750KHZ otherwise
4343 */
4344 rc = smbchg_sec_masked_write(chip,
4345 chip->bat_if_base + BAT_IF_TRIM7_REG,
4346 CFG_750KHZ_BIT, hvdcp2 ? 0 : CFG_750KHZ_BIT);
4347 if (rc) {
4348 dev_err(chip->dev, "Cannot set switching freq: %d\n", rc);
4349 return rc;
4350 }
4351
4352 /*
4353 * Set the charger switch frequency clamp voltage threshold to 3.338V
4354 * if HVDCP 2.0, or 3.512V otherwise.
4355 */
4356 rc = smbchg_sec_masked_write(chip, chip->bat_if_base + BB_CLMP_SEL,
4357 BB_CLMP_MASK,
4358 hvdcp2 ? BB_CLMP_VFIX_3338MV : BB_CLMP_VFIX_3512MV);
4359 if (rc) {
4360 dev_err(chip->dev, "Cannot set switching freq: %d\n", rc);
4361 return rc;
4362 }
4363
4364 return 0;
4365}
4366
4367#define DEFAULT_SDP_MA 100
4368#define DEFAULT_CDP_MA 1500
4369static int smbchg_change_usb_supply_type(struct smbchg_chip *chip,
4370 enum power_supply_type type)
4371{
4372 int rc, current_limit_ma;
4373
4374 /*
4375 * if the type is not unknown, set the type before changing ICL vote
4376 * in order to ensure that the correct current limit registers are
4377 * used
4378 */
4379 if (type != POWER_SUPPLY_TYPE_UNKNOWN)
4380 chip->usb_supply_type = type;
4381
4382 /*
4383 * Type-C only supports STD(900), MEDIUM(1500) and HIGH(3000) current
4384 * modes, skip all BC 1.2 current if external typec is supported.
4385 * Note: for SDP supporting current based on USB notifications.
4386 */
4387 if (chip->typec_psy && (type != POWER_SUPPLY_TYPE_USB))
4388 current_limit_ma = chip->typec_current_ma;
4389 else if (type == POWER_SUPPLY_TYPE_USB)
4390 current_limit_ma = DEFAULT_SDP_MA;
4391 else if (type == POWER_SUPPLY_TYPE_USB_CDP)
4392 current_limit_ma = DEFAULT_CDP_MA;
4393 else if (type == POWER_SUPPLY_TYPE_USB_HVDCP)
4394 current_limit_ma = smbchg_default_hvdcp_icl_ma;
4395 else if (type == POWER_SUPPLY_TYPE_USB_HVDCP_3)
4396 current_limit_ma = smbchg_default_hvdcp3_icl_ma;
4397 else
4398 current_limit_ma = smbchg_default_dcp_icl_ma;
4399
4400 pr_smb(PR_STATUS, "Type %d: setting mA = %d\n",
4401 type, current_limit_ma);
4402 rc = vote(chip->usb_icl_votable, PSY_ICL_VOTER, true,
4403 current_limit_ma);
4404 if (rc < 0) {
4405 pr_err("Couldn't vote for new USB ICL rc=%d\n", rc);
4406 goto out;
4407 }
4408
4409 /* otherwise if it is unknown, set type after the vote */
4410 if (type == POWER_SUPPLY_TYPE_UNKNOWN)
4411 chip->usb_supply_type = type;
4412
4413 if (!chip->skip_usb_notification)
4414 power_supply_changed(chip->usb_psy);
4415
4416 /* set the correct buck switching frequency */
4417 rc = smbchg_set_optimal_charging_mode(chip, type);
4418 if (rc < 0)
4419 pr_err("Couldn't set charger optimal mode rc=%d\n", rc);
4420
4421out:
4422 return rc;
4423}
4424
4425#define HVDCP_ADAPTER_SEL_MASK SMB_MASK(5, 4)
4426#define HVDCP_5V 0x00
4427#define HVDCP_9V 0x10
4428#define USB_CMD_HVDCP_1 0x42
4429#define FORCE_HVDCP_2p0 BIT(3)
4430
4431static int force_9v_hvdcp(struct smbchg_chip *chip)
4432{
4433 int rc;
4434
4435 /* Force 5V HVDCP */
4436 rc = smbchg_sec_masked_write(chip,
4437 chip->usb_chgpth_base + CHGPTH_CFG,
4438 HVDCP_ADAPTER_SEL_MASK, HVDCP_5V);
4439 if (rc) {
4440 pr_err("Couldn't set hvdcp config in chgpath_chg rc=%d\n", rc);
4441 return rc;
4442 }
4443
4444 /* Force QC2.0 */
4445 rc = smbchg_masked_write(chip,
4446 chip->usb_chgpth_base + USB_CMD_HVDCP_1,
4447 FORCE_HVDCP_2p0, FORCE_HVDCP_2p0);
4448 rc |= smbchg_masked_write(chip,
4449 chip->usb_chgpth_base + USB_CMD_HVDCP_1,
4450 FORCE_HVDCP_2p0, 0);
4451 if (rc < 0) {
4452 pr_err("Couldn't force QC2.0 rc=%d\n", rc);
4453 return rc;
4454 }
4455
4456 /* Delay to switch into HVDCP 2.0 and avoid UV */
4457 msleep(500);
4458
4459 /* Force 9V HVDCP */
4460 rc = smbchg_sec_masked_write(chip,
4461 chip->usb_chgpth_base + CHGPTH_CFG,
4462 HVDCP_ADAPTER_SEL_MASK, HVDCP_9V);
4463 if (rc)
4464 pr_err("Couldn't set hvdcp config in chgpath_chg rc=%d\n", rc);
4465
4466 return rc;
4467}
4468
4469static void smbchg_hvdcp_det_work(struct work_struct *work)
4470{
4471 struct smbchg_chip *chip = container_of(work,
4472 struct smbchg_chip,
4473 hvdcp_det_work.work);
4474 int rc;
4475
4476 if (is_hvdcp_present(chip)) {
4477 if (!chip->hvdcp3_supported &&
4478 (chip->wa_flags & SMBCHG_HVDCP_9V_EN_WA)) {
4479 /* force HVDCP 2.0 */
4480 rc = force_9v_hvdcp(chip);
4481 if (rc)
4482 pr_err("could not force 9V HVDCP continuing rc=%d\n",
4483 rc);
4484 }
4485 smbchg_change_usb_supply_type(chip,
4486 POWER_SUPPLY_TYPE_USB_HVDCP);
4487 if (chip->batt_psy)
4488 power_supply_changed(chip->batt_psy);
4489 smbchg_aicl_deglitch_wa_check(chip);
4490 }
4491 smbchg_relax(chip, PM_DETECT_HVDCP);
4492}
4493
4494static int set_usb_psy_dp_dm(struct smbchg_chip *chip, int state)
4495{
4496 int rc;
4497 u8 reg;
4498 union power_supply_propval pval = {0, };
4499
4500 /*
4501 * ensure that we are not in the middle of an insertion where usbin_uv
4502 * is low and src_detect hasnt gone high. If so force dp=F dm=F
4503 * which guarantees proper type detection
4504 */
4505 rc = smbchg_read(chip, &reg, chip->usb_chgpth_base + RT_STS, 1);
4506 if (!rc && !(reg & USBIN_UV_BIT) && !(reg & USBIN_SRC_DET_BIT)) {
4507 pr_smb(PR_MISC, "overwriting state = %d with %d\n",
4508 state, POWER_SUPPLY_DP_DM_DPF_DMF);
4509 if (chip->dpdm_reg && !regulator_is_enabled(chip->dpdm_reg))
4510 return regulator_enable(chip->dpdm_reg);
4511 }
4512 pr_smb(PR_MISC, "setting usb psy dp dm = %d\n", state);
4513 pval.intval = state;
4514 return power_supply_set_property(chip->usb_psy,
4515 POWER_SUPPLY_PROP_DP_DM, &pval);
4516}
4517
4518#define APSD_CFG 0xF5
4519#define AUTO_SRC_DETECT_EN_BIT BIT(0)
4520#define APSD_TIMEOUT_MS 1500
4521static void restore_from_hvdcp_detection(struct smbchg_chip *chip)
4522{
4523 int rc;
4524
4525 pr_smb(PR_MISC, "Retracting HVDCP vote for ICL\n");
4526 rc = vote(chip->usb_icl_votable, HVDCP_ICL_VOTER, false, 0);
4527 if (rc < 0)
4528 pr_err("Couldn't retract HVDCP ICL vote rc=%d\n", rc);
4529
4530 /* switch to 9V HVDCP */
4531 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + CHGPTH_CFG,
4532 HVDCP_ADAPTER_SEL_MASK, HVDCP_9V);
4533 if (rc < 0)
4534 pr_err("Couldn't configure HVDCP 9V rc=%d\n", rc);
4535
4536 /* enable HVDCP */
4537 rc = smbchg_sec_masked_write(chip,
4538 chip->usb_chgpth_base + CHGPTH_CFG,
4539 HVDCP_EN_BIT, HVDCP_EN_BIT);
4540 if (rc < 0)
4541 pr_err("Couldn't enable HVDCP rc=%d\n", rc);
4542
4543 /* enable APSD */
4544 rc = smbchg_sec_masked_write(chip,
4545 chip->usb_chgpth_base + APSD_CFG,
4546 AUTO_SRC_DETECT_EN_BIT, AUTO_SRC_DETECT_EN_BIT);
4547 if (rc < 0)
4548 pr_err("Couldn't enable APSD rc=%d\n", rc);
4549
4550 /* Reset back to 5V unregulated */
4551 rc = smbchg_sec_masked_write(chip,
4552 chip->usb_chgpth_base + USBIN_CHGR_CFG,
4553 ADAPTER_ALLOWANCE_MASK, USBIN_ADAPTER_5V_UNREGULATED_9V);
4554 if (rc < 0)
4555 pr_err("Couldn't write usb allowance rc=%d\n", rc);
4556
4557 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + USB_AICL_CFG,
4558 AICL_EN_BIT, AICL_EN_BIT);
4559 if (rc < 0)
4560 pr_err("Couldn't enable AICL rc=%d\n", rc);
4561
4562 chip->hvdcp_3_det_ignore_uv = false;
4563 chip->pulse_cnt = 0;
4564}
4565
4566#define RESTRICTED_CHG_FCC_PERCENT 50
4567static int smbchg_restricted_charging(struct smbchg_chip *chip, bool enable)
4568{
4569 int current_table_index, fastchg_current;
4570 int rc = 0;
4571
4572 /* If enable, set the fcc to the set point closest
4573 * to 50% of the configured fcc while remaining below it
4574 */
4575 current_table_index = find_smaller_in_array(
4576 chip->tables.usb_ilim_ma_table,
4577 chip->cfg_fastchg_current_ma
4578 * RESTRICTED_CHG_FCC_PERCENT / 100,
4579 chip->tables.usb_ilim_ma_len);
4580 fastchg_current =
4581 chip->tables.usb_ilim_ma_table[current_table_index];
4582 rc = vote(chip->fcc_votable, RESTRICTED_CHG_FCC_VOTER, enable,
4583 fastchg_current);
4584
4585 pr_smb(PR_STATUS, "restricted_charging set to %d\n", enable);
4586 chip->restricted_charging = enable;
4587
4588 return rc;
4589}
4590
4591static void handle_usb_removal(struct smbchg_chip *chip)
4592{
4593 struct power_supply *parallel_psy = get_parallel_psy(chip);
4594 union power_supply_propval pval = {0, };
4595 int rc;
4596
4597 pr_smb(PR_STATUS, "triggered\n");
4598 smbchg_aicl_deglitch_wa_check(chip);
4599 /* Clear the OV detected status set before */
4600 if (chip->usb_ov_det)
4601 chip->usb_ov_det = false;
4602 /* Clear typec current status */
4603 if (chip->typec_psy)
4604 chip->typec_current_ma = 0;
4605 smbchg_change_usb_supply_type(chip, POWER_SUPPLY_TYPE_UNKNOWN);
4606 extcon_set_cable_state_(chip->extcon, EXTCON_USB, chip->usb_present);
4607 if (chip->dpdm_reg)
4608 regulator_disable(chip->dpdm_reg);
4609 schedule_work(&chip->usb_set_online_work);
4610
4611 pr_smb(PR_MISC, "setting usb psy health UNKNOWN\n");
4612 chip->usb_health = POWER_SUPPLY_HEALTH_UNKNOWN;
4613 power_supply_changed(chip->usb_psy);
4614
4615 if (parallel_psy && chip->parallel_charger_detected) {
4616 pval.intval = false;
4617 power_supply_set_property(parallel_psy,
4618 POWER_SUPPLY_PROP_PRESENT, &pval);
4619 }
4620 if (chip->parallel.avail && chip->aicl_done_irq
4621 && chip->enable_aicl_wake) {
4622 disable_irq_wake(chip->aicl_done_irq);
4623 chip->enable_aicl_wake = false;
4624 }
4625 chip->parallel.enabled_once = false;
4626 chip->vbat_above_headroom = false;
4627 rc = smbchg_masked_write(chip, chip->usb_chgpth_base + CMD_IL,
4628 ICL_OVERRIDE_BIT, 0);
4629 if (rc < 0)
4630 pr_err("Couldn't set override rc = %d\n", rc);
4631
4632 vote(chip->usb_icl_votable, WEAK_CHARGER_ICL_VOTER, false, 0);
4633 chip->usb_icl_delta = 0;
4634 vote(chip->usb_icl_votable, SW_AICL_ICL_VOTER, false, 0);
4635 vote(chip->aicl_deglitch_short_votable,
4636 HVDCP_SHORT_DEGLITCH_VOTER, false, 0);
4637 if (!chip->hvdcp_not_supported)
4638 restore_from_hvdcp_detection(chip);
4639}
4640
4641static bool is_usbin_uv_high(struct smbchg_chip *chip)
4642{
4643 int rc;
4644 u8 reg;
4645
4646 rc = smbchg_read(chip, &reg, chip->usb_chgpth_base + RT_STS, 1);
4647 if (rc < 0) {
4648 dev_err(chip->dev, "Couldn't read usb rt status rc = %d\n", rc);
4649 return false;
4650 }
4651 return reg &= USBIN_UV_BIT;
4652}
4653
4654#define HVDCP_NOTIFY_MS 2500
4655static void handle_usb_insertion(struct smbchg_chip *chip)
4656{
4657 struct power_supply *parallel_psy = get_parallel_psy(chip);
4658 union power_supply_propval pval = {0, };
4659 enum power_supply_type usb_supply_type;
4660 int rc;
4661 char *usb_type_name = "null";
4662
4663 pr_smb(PR_STATUS, "triggered\n");
4664 /* usb inserted */
4665 read_usb_type(chip, &usb_type_name, &usb_supply_type);
4666 pr_smb(PR_STATUS,
4667 "inserted type = %d (%s)", usb_supply_type, usb_type_name);
4668
4669 smbchg_aicl_deglitch_wa_check(chip);
4670 if (chip->typec_psy)
4671 update_typec_status(chip);
4672 smbchg_change_usb_supply_type(chip, usb_supply_type);
4673
4674 /* Only notify USB if it's not a charger */
4675 if (usb_supply_type == POWER_SUPPLY_TYPE_USB ||
4676 usb_supply_type == POWER_SUPPLY_TYPE_USB_CDP)
4677 extcon_set_cable_state_(chip->extcon, EXTCON_USB,
4678 chip->usb_present);
4679
4680 /* Notify the USB psy if OV condition is not present */
4681 if (!chip->usb_ov_det) {
4682 /*
4683 * Note that this could still be a very weak charger
4684 * if the handle_usb_insertion was triggered from
4685 * the falling edge of an USBIN_OV interrupt
4686 */
4687 pr_smb(PR_MISC, "setting usb psy health %s\n",
4688 chip->very_weak_charger
4689 ? "UNSPEC_FAILURE" : "GOOD");
4690 chip->usb_health = chip->very_weak_charger
4691 ? POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
4692 : POWER_SUPPLY_HEALTH_GOOD;
4693 power_supply_changed(chip->usb_psy);
4694 }
4695 schedule_work(&chip->usb_set_online_work);
4696
4697 if (!chip->hvdcp_not_supported &&
4698 (usb_supply_type == POWER_SUPPLY_TYPE_USB_DCP)) {
4699 cancel_delayed_work_sync(&chip->hvdcp_det_work);
4700 smbchg_stay_awake(chip, PM_DETECT_HVDCP);
4701 schedule_delayed_work(&chip->hvdcp_det_work,
4702 msecs_to_jiffies(HVDCP_NOTIFY_MS));
4703 }
4704
4705 if (parallel_psy) {
4706 pval.intval = true;
4707 rc = power_supply_set_property(parallel_psy,
4708 POWER_SUPPLY_PROP_PRESENT, &pval);
4709 chip->parallel_charger_detected = rc ? false : true;
4710 if (rc)
4711 pr_debug("parallel-charger absent rc=%d\n", rc);
4712 }
4713
4714 if (chip->parallel.avail && chip->aicl_done_irq
4715 && !chip->enable_aicl_wake) {
4716 rc = enable_irq_wake(chip->aicl_done_irq);
4717 chip->enable_aicl_wake = true;
4718 }
4719}
4720
4721void update_usb_status(struct smbchg_chip *chip, bool usb_present, bool force)
4722{
4723 mutex_lock(&chip->usb_status_lock);
4724 if (force) {
4725 chip->usb_present = usb_present;
4726 chip->usb_present ? handle_usb_insertion(chip)
4727 : handle_usb_removal(chip);
4728 goto unlock;
4729 }
4730 if (!chip->usb_present && usb_present) {
4731 chip->usb_present = usb_present;
4732 handle_usb_insertion(chip);
4733 } else if (chip->usb_present && !usb_present) {
4734 chip->usb_present = usb_present;
4735 handle_usb_removal(chip);
4736 }
4737
4738 /* update FG */
4739 set_property_on_fg(chip, POWER_SUPPLY_PROP_STATUS,
4740 get_prop_batt_status(chip));
4741unlock:
4742 mutex_unlock(&chip->usb_status_lock);
4743}
4744
4745static int otg_oc_reset(struct smbchg_chip *chip)
4746{
4747 int rc;
4748
4749 rc = smbchg_masked_write(chip, chip->bat_if_base + CMD_CHG_REG,
4750 OTG_EN_BIT, 0);
4751 if (rc)
4752 pr_err("Failed to disable OTG rc=%d\n", rc);
4753
4754 msleep(20);
4755
4756 /*
4757 * There is a possibility that an USBID interrupt might have
4758 * occurred notifying USB power supply to disable OTG. We
4759 * should not enable OTG in such cases.
4760 */
4761 if (!is_otg_present(chip)) {
4762 pr_smb(PR_STATUS,
4763 "OTG is not present, not enabling OTG_EN_BIT\n");
4764 goto out;
4765 }
4766
4767 rc = smbchg_masked_write(chip, chip->bat_if_base + CMD_CHG_REG,
4768 OTG_EN_BIT, OTG_EN_BIT);
4769 if (rc)
4770 pr_err("Failed to re-enable OTG rc=%d\n", rc);
4771
4772out:
4773 return rc;
4774}
4775
4776static int get_current_time(unsigned long *now_tm_sec)
4777{
4778 struct rtc_time tm;
4779 struct rtc_device *rtc;
4780 int rc;
4781
4782 rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
4783 if (rtc == NULL) {
4784 pr_err("%s: unable to open rtc device (%s)\n",
4785 __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
4786 return -EINVAL;
4787 }
4788
4789 rc = rtc_read_time(rtc, &tm);
4790 if (rc) {
4791 pr_err("Error reading rtc device (%s) : %d\n",
4792 CONFIG_RTC_HCTOSYS_DEVICE, rc);
4793 goto close_time;
4794 }
4795
4796 rc = rtc_valid_tm(&tm);
4797 if (rc) {
4798 pr_err("Invalid RTC time (%s): %d\n",
4799 CONFIG_RTC_HCTOSYS_DEVICE, rc);
4800 goto close_time;
4801 }
4802 rtc_tm_to_time(&tm, now_tm_sec);
4803
4804close_time:
4805 rtc_class_close(rtc);
4806 return rc;
4807}
4808
4809#define AICL_IRQ_LIMIT_SECONDS 60
4810#define AICL_IRQ_LIMIT_COUNT 25
4811static void increment_aicl_count(struct smbchg_chip *chip)
4812{
4813 bool bad_charger = false;
4814 int max_aicl_count, rc;
4815 u8 reg;
4816 long elapsed_seconds;
4817 unsigned long now_seconds;
4818
4819 pr_smb(PR_INTERRUPT, "aicl count c:%d dgltch:%d first:%ld\n",
4820 chip->aicl_irq_count, chip->aicl_deglitch_short,
4821 chip->first_aicl_seconds);
4822
4823 rc = smbchg_read(chip, &reg,
4824 chip->usb_chgpth_base + ICL_STS_1_REG, 1);
4825 if (!rc)
4826 chip->aicl_complete = reg & AICL_STS_BIT;
4827 else
4828 chip->aicl_complete = false;
4829
4830 if (chip->aicl_deglitch_short || chip->force_aicl_rerun) {
4831 if (!chip->aicl_irq_count)
4832 get_current_time(&chip->first_aicl_seconds);
4833 get_current_time(&now_seconds);
4834 elapsed_seconds = now_seconds
4835 - chip->first_aicl_seconds;
4836
4837 if (elapsed_seconds > AICL_IRQ_LIMIT_SECONDS) {
4838 pr_smb(PR_INTERRUPT,
4839 "resetting: elp:%ld first:%ld now:%ld c=%d\n",
4840 elapsed_seconds, chip->first_aicl_seconds,
4841 now_seconds, chip->aicl_irq_count);
4842 chip->aicl_irq_count = 1;
4843 get_current_time(&chip->first_aicl_seconds);
4844 return;
4845 }
4846 /*
4847 * Double the amount of AICLs allowed if parallel charging is
4848 * enabled.
4849 */
4850 max_aicl_count = AICL_IRQ_LIMIT_COUNT
4851 * (chip->parallel.avail ? 2 : 1);
4852 chip->aicl_irq_count++;
4853
4854 if (chip->aicl_irq_count > max_aicl_count) {
4855 pr_smb(PR_INTERRUPT, "elp:%ld first:%ld now:%ld c=%d\n",
4856 elapsed_seconds, chip->first_aicl_seconds,
4857 now_seconds, chip->aicl_irq_count);
4858 pr_smb(PR_INTERRUPT, "Disable AICL rerun\n");
4859 chip->very_weak_charger = true;
4860 bad_charger = true;
4861
4862 /*
4863 * Disable AICL rerun since many interrupts were
4864 * triggered in a short time
4865 */
4866 /* disable hw aicl */
4867 rc = vote(chip->hw_aicl_rerun_disable_votable,
4868 WEAK_CHARGER_HW_AICL_VOTER, true, 0);
4869 if (rc < 0) {
4870 pr_err("Couldn't disable hw aicl rerun rc=%d\n",
4871 rc);
4872 return;
4873 }
4874
4875 /* Vote 100mA current limit */
4876 rc = vote(chip->usb_icl_votable, WEAK_CHARGER_ICL_VOTER,
4877 true, CURRENT_100_MA);
4878 if (rc < 0) {
4879 pr_err("Can't vote %d current limit rc=%d\n",
4880 CURRENT_100_MA, rc);
4881 }
4882
4883 chip->aicl_irq_count = 0;
4884 } else if ((get_prop_charge_type(chip) ==
4885 POWER_SUPPLY_CHARGE_TYPE_FAST) &&
4886 (reg & AICL_SUSP_BIT)) {
4887 /*
4888 * If the AICL_SUSP_BIT is on, then AICL reruns have
4889 * already been disabled. Set the very weak charger
4890 * flag so that the driver reports a bad charger
4891 * and does not reenable AICL reruns.
4892 */
4893 chip->very_weak_charger = true;
4894 bad_charger = true;
4895 }
4896 if (bad_charger) {
4897 pr_smb(PR_MISC,
4898 "setting usb psy health UNSPEC_FAILURE\n");
4899 chip->usb_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
4900 power_supply_changed(chip->usb_psy);
4901 schedule_work(&chip->usb_set_online_work);
4902 }
4903 }
4904}
4905
4906static int wait_for_usbin_uv(struct smbchg_chip *chip, bool high)
4907{
4908 int rc;
4909 int tries = 3;
4910 struct completion *completion = &chip->usbin_uv_lowered;
4911 bool usbin_uv;
4912
4913 if (high)
4914 completion = &chip->usbin_uv_raised;
4915
4916 while (tries--) {
4917 rc = wait_for_completion_interruptible_timeout(
4918 completion,
4919 msecs_to_jiffies(APSD_TIMEOUT_MS));
4920 if (rc >= 0)
4921 break;
4922 }
4923
4924 usbin_uv = is_usbin_uv_high(chip);
4925
4926 if (high == usbin_uv)
4927 return 0;
4928
4929 pr_err("usbin uv didnt go to a %s state, still at %s, tries = %d, rc = %d\n",
4930 high ? "risen" : "lowered",
4931 usbin_uv ? "high" : "low",
4932 tries, rc);
4933 return -EINVAL;
4934}
4935
4936static int wait_for_src_detect(struct smbchg_chip *chip, bool high)
4937{
4938 int rc;
4939 int tries = 3;
4940 struct completion *completion = &chip->src_det_lowered;
4941 bool src_detect;
4942
4943 if (high)
4944 completion = &chip->src_det_raised;
4945
4946 while (tries--) {
4947 rc = wait_for_completion_interruptible_timeout(
4948 completion,
4949 msecs_to_jiffies(APSD_TIMEOUT_MS));
4950 if (rc >= 0)
4951 break;
4952 }
4953
4954 src_detect = is_src_detect_high(chip);
4955
4956 if (high == src_detect)
4957 return 0;
4958
Kiran Gundaee7cef82017-09-20 17:44:16 +05304959 pr_err("src detect didn't go to a %s state, still at %s, tries = %d, rc = %d\n",
Kiran Gunda1bc78922017-09-19 13:09:44 +05304960 high ? "risen" : "lowered",
4961 src_detect ? "high" : "low",
4962 tries, rc);
4963 return -EINVAL;
4964}
4965
4966static int fake_insertion_removal(struct smbchg_chip *chip, bool insertion)
4967{
4968 int rc;
4969 bool src_detect;
4970 bool usbin_uv;
4971
4972 if (insertion) {
4973 reinit_completion(&chip->src_det_raised);
4974 reinit_completion(&chip->usbin_uv_lowered);
4975 } else {
4976 reinit_completion(&chip->src_det_lowered);
4977 reinit_completion(&chip->usbin_uv_raised);
4978 }
4979
4980 /* ensure that usbin uv real time status is in the right state */
4981 usbin_uv = is_usbin_uv_high(chip);
4982 if (usbin_uv != insertion) {
4983 pr_err("Skip faking, usbin uv is already %d\n", usbin_uv);
4984 return -EINVAL;
4985 }
4986
4987 /* ensure that src_detect real time status is in the right state */
4988 src_detect = is_src_detect_high(chip);
4989 if (src_detect == insertion) {
4990 pr_err("Skip faking, src detect is already %d\n", src_detect);
4991 return -EINVAL;
4992 }
4993
4994 pr_smb(PR_MISC, "Allow only %s charger\n",
4995 insertion ? "5-9V" : "9V only");
4996 rc = smbchg_sec_masked_write(chip,
4997 chip->usb_chgpth_base + USBIN_CHGR_CFG,
4998 ADAPTER_ALLOWANCE_MASK,
4999 insertion ?
5000 USBIN_ADAPTER_5V_9V_CONT : USBIN_ADAPTER_9V);
5001 if (rc < 0) {
5002 pr_err("Couldn't write usb allowance rc=%d\n", rc);
5003 return rc;
5004 }
5005
5006 pr_smb(PR_MISC, "Waiting on %s usbin uv\n",
5007 insertion ? "falling" : "rising");
5008 rc = wait_for_usbin_uv(chip, !insertion);
5009 if (rc < 0) {
5010 pr_err("wait for usbin uv failed rc = %d\n", rc);
5011 return rc;
5012 }
5013
5014 pr_smb(PR_MISC, "Waiting on %s src det\n",
5015 insertion ? "rising" : "falling");
5016 rc = wait_for_src_detect(chip, insertion);
5017 if (rc < 0) {
5018 pr_err("wait for src detect failed rc = %d\n", rc);
5019 return rc;
5020 }
5021
5022 return 0;
5023}
5024
Subbaraman Narayanamurthy986bff52016-03-21 15:55:19 -07005025static void smbchg_handle_hvdcp3_disable(struct smbchg_chip *chip)
5026{
5027 enum power_supply_type usb_supply_type;
5028 char *usb_type_name = "NULL";
5029
5030 if (chip->allow_hvdcp3_detection)
5031 return;
5032
5033 chip->pulse_cnt = 0;
5034
5035 if (is_hvdcp_present(chip)) {
5036 smbchg_change_usb_supply_type(chip,
5037 POWER_SUPPLY_TYPE_USB_HVDCP);
5038 } else if (is_usb_present(chip)) {
5039 read_usb_type(chip, &usb_type_name, &usb_supply_type);
5040 smbchg_change_usb_supply_type(chip, usb_supply_type);
5041 if (usb_supply_type == POWER_SUPPLY_TYPE_USB_DCP)
5042 schedule_delayed_work(&chip->hvdcp_det_work,
5043 msecs_to_jiffies(HVDCP_NOTIFY_MS));
5044 } else {
5045 smbchg_change_usb_supply_type(chip, POWER_SUPPLY_TYPE_UNKNOWN);
5046 }
5047}
5048
Kiran Gunda1bc78922017-09-19 13:09:44 +05305049static int smbchg_prepare_for_pulsing(struct smbchg_chip *chip)
5050{
5051 int rc = 0;
5052 u8 reg;
5053
5054 /* switch to 5V HVDCP */
5055 pr_smb(PR_MISC, "Switch to 5V HVDCP\n");
5056 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + CHGPTH_CFG,
5057 HVDCP_ADAPTER_SEL_MASK, HVDCP_5V);
5058 if (rc < 0) {
5059 pr_err("Couldn't configure HVDCP 5V rc=%d\n", rc);
5060 goto out;
5061 }
5062
5063 /* wait for HVDCP to lower to 5V */
5064 msleep(500);
5065 /*
5066 * Check if the same hvdcp session is in progress. src_det should be
5067 * high and that we are still in 5V hvdcp
5068 */
5069 if (!is_src_detect_high(chip)) {
5070 pr_smb(PR_MISC, "src det low after 500mS sleep\n");
5071 goto out;
5072 }
5073
5074 /* disable HVDCP */
5075 pr_smb(PR_MISC, "Disable HVDCP\n");
5076 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + CHGPTH_CFG,
5077 HVDCP_EN_BIT, 0);
5078 if (rc < 0) {
5079 pr_err("Couldn't disable HVDCP rc=%d\n", rc);
5080 goto out;
5081 }
5082
5083 pr_smb(PR_MISC, "HVDCP voting for 300mA ICL\n");
5084 rc = vote(chip->usb_icl_votable, HVDCP_ICL_VOTER, true, 300);
5085 if (rc < 0) {
5086 pr_err("Couldn't vote for 300mA HVDCP ICL rc=%d\n", rc);
5087 goto out;
5088 }
5089
5090 pr_smb(PR_MISC, "Disable AICL\n");
5091 smbchg_sec_masked_write(chip, chip->usb_chgpth_base + USB_AICL_CFG,
5092 AICL_EN_BIT, 0);
5093
5094 chip->hvdcp_3_det_ignore_uv = true;
5095 /* fake a removal */
5096 pr_smb(PR_MISC, "Faking Removal\n");
5097 rc = fake_insertion_removal(chip, false);
5098 if (rc < 0) {
5099 pr_err("Couldn't fake removal HVDCP Removed rc=%d\n", rc);
5100 goto handle_removal;
5101 }
5102
5103 /* disable APSD */
5104 pr_smb(PR_MISC, "Disabling APSD\n");
5105 rc = smbchg_sec_masked_write(chip,
5106 chip->usb_chgpth_base + APSD_CFG,
5107 AUTO_SRC_DETECT_EN_BIT, 0);
5108 if (rc < 0) {
5109 pr_err("Couldn't disable APSD rc=%d\n", rc);
5110 goto out;
5111 }
5112
5113 /* fake an insertion */
5114 pr_smb(PR_MISC, "Faking Insertion\n");
5115 rc = fake_insertion_removal(chip, true);
5116 if (rc < 0) {
5117 pr_err("Couldn't fake insertion rc=%d\n", rc);
5118 goto handle_removal;
5119 }
5120 chip->hvdcp_3_det_ignore_uv = false;
5121
5122 pr_smb(PR_MISC, "Enable AICL\n");
5123 smbchg_sec_masked_write(chip, chip->usb_chgpth_base + USB_AICL_CFG,
5124 AICL_EN_BIT, AICL_EN_BIT);
5125
5126 set_usb_psy_dp_dm(chip, POWER_SUPPLY_DP_DM_DP0P6_DMF);
5127 /*
5128 * DCP will switch to HVDCP in this time by removing the short
5129 * between DP DM
5130 */
5131 msleep(HVDCP_NOTIFY_MS);
5132 /*
5133 * Check if the same hvdcp session is in progress. src_det should be
5134 * high and the usb type should be none since APSD was disabled
5135 */
5136 if (!is_src_detect_high(chip)) {
5137 pr_smb(PR_MISC, "src det low after 2s sleep\n");
5138 rc = -EINVAL;
5139 goto out;
5140 }
5141
5142 smbchg_read(chip, &reg, chip->misc_base + IDEV_STS, 1);
5143 if ((reg >> TYPE_BITS_OFFSET) != 0) {
5144 pr_smb(PR_MISC, "type bits set after 2s sleep - abort\n");
5145 rc = -EINVAL;
5146 goto out;
5147 }
5148
5149 set_usb_psy_dp_dm(chip, POWER_SUPPLY_DP_DM_DP0P6_DM3P3);
5150 /* Wait 60mS after entering continuous mode */
5151 msleep(60);
5152
5153 return 0;
5154out:
5155 chip->hvdcp_3_det_ignore_uv = false;
5156 restore_from_hvdcp_detection(chip);
5157 return rc;
5158handle_removal:
5159 chip->hvdcp_3_det_ignore_uv = false;
5160 update_usb_status(chip, 0, 0);
5161 return rc;
5162}
5163
5164static int smbchg_unprepare_for_pulsing(struct smbchg_chip *chip)
5165{
5166 int rc = 0;
5167
5168 if (chip->dpdm_reg && !regulator_is_enabled(chip->dpdm_reg))
5169 rc = regulator_enable(chip->dpdm_reg);
5170 if (rc < 0) {
5171 pr_err("Couldn't enable DP/DM for pulsing rc=%d\n", rc);
5172 return rc;
5173 }
5174
5175 /* switch to 9V HVDCP */
5176 pr_smb(PR_MISC, "Switch to 9V HVDCP\n");
5177 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + CHGPTH_CFG,
5178 HVDCP_ADAPTER_SEL_MASK, HVDCP_9V);
5179 if (rc < 0) {
5180 pr_err("Couldn't configure HVDCP 9V rc=%d\n", rc);
5181 return rc;
5182 }
5183
5184 /* enable HVDCP */
5185 pr_smb(PR_MISC, "Enable HVDCP\n");
5186 rc = smbchg_sec_masked_write(chip,
5187 chip->usb_chgpth_base + CHGPTH_CFG,
5188 HVDCP_EN_BIT, HVDCP_EN_BIT);
5189 if (rc < 0) {
5190 pr_err("Couldn't enable HVDCP rc=%d\n", rc);
5191 return rc;
5192 }
5193
5194 /* enable APSD */
5195 pr_smb(PR_MISC, "Enabling APSD\n");
5196 rc = smbchg_sec_masked_write(chip,
5197 chip->usb_chgpth_base + APSD_CFG,
5198 AUTO_SRC_DETECT_EN_BIT, AUTO_SRC_DETECT_EN_BIT);
5199 if (rc < 0) {
5200 pr_err("Couldn't enable APSD rc=%d\n", rc);
5201 return rc;
5202 }
5203
5204 /* Disable AICL */
5205 pr_smb(PR_MISC, "Disable AICL\n");
5206 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + USB_AICL_CFG,
5207 AICL_EN_BIT, 0);
5208 if (rc < 0) {
5209 pr_err("Couldn't disable AICL rc=%d\n", rc);
5210 return rc;
5211 }
5212
5213 /* fake a removal */
5214 chip->hvdcp_3_det_ignore_uv = true;
5215 pr_smb(PR_MISC, "Faking Removal\n");
5216 rc = fake_insertion_removal(chip, false);
5217 if (rc < 0) {
5218 pr_err("Couldn't fake removal rc=%d\n", rc);
5219 goto out;
5220 }
5221
5222 /*
5223 * reset the enabled once flag for parallel charging so
5224 * parallel charging can immediately restart after the HVDCP pulsing
5225 * is complete
5226 */
5227 chip->parallel.enabled_once = false;
5228
5229 /* fake an insertion */
5230 pr_smb(PR_MISC, "Faking Insertion\n");
5231 rc = fake_insertion_removal(chip, true);
5232 if (rc < 0) {
5233 pr_err("Couldn't fake insertion rc=%d\n", rc);
5234 goto out;
5235 }
5236 chip->hvdcp_3_det_ignore_uv = false;
5237
5238 /* Enable AICL */
5239 pr_smb(PR_MISC, "Enable AICL\n");
5240 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + USB_AICL_CFG,
5241 AICL_EN_BIT, 0);
5242 if (rc < 0) {
5243 pr_err("Couldn't enable AICL rc=%d\n", rc);
5244 return rc;
5245 }
5246
5247out:
5248 /*
5249 * There are many QC 2.0 chargers that collapse before the aicl deglitch
5250 * timer can mitigate. Hence set the aicl deglitch time to a shorter
5251 * period.
5252 */
5253
5254 rc = vote(chip->aicl_deglitch_short_votable,
5255 HVDCP_SHORT_DEGLITCH_VOTER, true, 0);
5256 if (rc < 0)
5257 pr_err("Couldn't reduce aicl deglitch rc=%d\n", rc);
5258
5259 pr_smb(PR_MISC, "Retracting HVDCP vote for ICL\n");
5260 rc = vote(chip->usb_icl_votable, HVDCP_ICL_VOTER, false, 0);
5261 if (rc < 0)
5262 pr_err("Couldn't retract HVDCP ICL vote rc=%d\n", rc);
5263
5264 chip->hvdcp_3_det_ignore_uv = false;
5265 if (!is_src_detect_high(chip)) {
5266 pr_smb(PR_MISC, "HVDCP removed\n");
5267 update_usb_status(chip, 0, 0);
5268 }
Subbaraman Narayanamurthy986bff52016-03-21 15:55:19 -07005269
5270 smbchg_handle_hvdcp3_disable(chip);
5271
Kiran Gunda1bc78922017-09-19 13:09:44 +05305272 return rc;
5273}
5274
5275#define USB_CMD_APSD 0x41
5276#define APSD_RERUN BIT(0)
5277static int rerun_apsd(struct smbchg_chip *chip)
5278{
5279 int rc;
5280
5281 reinit_completion(&chip->src_det_raised);
5282 reinit_completion(&chip->usbin_uv_lowered);
5283 reinit_completion(&chip->src_det_lowered);
5284 reinit_completion(&chip->usbin_uv_raised);
5285
5286 /* re-run APSD */
5287 rc = smbchg_masked_write(chip, chip->usb_chgpth_base + USB_CMD_APSD,
5288 APSD_RERUN, APSD_RERUN);
5289 if (rc) {
5290 pr_err("Couldn't re-run APSD rc=%d\n", rc);
5291 return rc;
5292 }
5293
5294 pr_smb(PR_MISC, "Waiting on rising usbin uv\n");
5295 rc = wait_for_usbin_uv(chip, true);
5296 if (rc < 0) {
5297 pr_err("wait for usbin uv failed rc = %d\n", rc);
5298 return rc;
5299 }
5300
5301 pr_smb(PR_MISC, "Waiting on falling src det\n");
5302 rc = wait_for_src_detect(chip, false);
5303 if (rc < 0) {
5304 pr_err("wait for src detect failed rc = %d\n", rc);
5305 return rc;
5306 }
5307
5308 pr_smb(PR_MISC, "Waiting on falling usbin uv\n");
5309 rc = wait_for_usbin_uv(chip, false);
5310 if (rc < 0) {
5311 pr_err("wait for usbin uv failed rc = %d\n", rc);
5312 return rc;
5313 }
5314
5315 pr_smb(PR_MISC, "Waiting on rising src det\n");
5316 rc = wait_for_src_detect(chip, true);
5317 if (rc < 0) {
5318 pr_err("wait for src detect failed rc = %d\n", rc);
5319 return rc;
5320 }
5321
5322 return rc;
5323}
5324
5325#define SCHG_LITE_USBIN_HVDCP_5_9V 0x8
5326#define SCHG_LITE_USBIN_HVDCP_5_9V_SEL_MASK 0x38
5327#define SCHG_LITE_USBIN_HVDCP_SEL_IDLE BIT(3)
5328static bool is_hvdcp_5v_cont_mode(struct smbchg_chip *chip)
5329{
5330 int rc;
5331 u8 reg = 0;
5332
5333 rc = smbchg_read(chip, &reg,
5334 chip->usb_chgpth_base + USBIN_HVDCP_STS, 1);
5335 if (rc) {
5336 pr_err("Unable to read HVDCP status rc=%d\n", rc);
5337 return false;
5338 }
5339
5340 pr_smb(PR_STATUS, "HVDCP status = %x\n", reg);
5341
5342 if (reg & SCHG_LITE_USBIN_HVDCP_SEL_IDLE) {
5343 rc = smbchg_read(chip, &reg,
5344 chip->usb_chgpth_base + INPUT_STS, 1);
5345 if (rc) {
5346 pr_err("Unable to read INPUT status rc=%d\n", rc);
5347 return false;
5348 }
5349 pr_smb(PR_STATUS, "INPUT status = %x\n", reg);
5350 if ((reg & SCHG_LITE_USBIN_HVDCP_5_9V_SEL_MASK) ==
5351 SCHG_LITE_USBIN_HVDCP_5_9V)
5352 return true;
5353 }
5354 return false;
5355}
5356
5357static int smbchg_prepare_for_pulsing_lite(struct smbchg_chip *chip)
5358{
5359 int rc = 0;
5360
5361 /* check if HVDCP is already in 5V continuous mode */
5362 if (is_hvdcp_5v_cont_mode(chip)) {
5363 pr_smb(PR_MISC, "HVDCP by default is in 5V continuous mode\n");
5364 return 0;
5365 }
5366
5367 /* switch to 5V HVDCP */
5368 pr_smb(PR_MISC, "Switch to 5V HVDCP\n");
5369 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + CHGPTH_CFG,
5370 HVDCP_ADAPTER_SEL_MASK, HVDCP_5V);
5371 if (rc < 0) {
5372 pr_err("Couldn't configure HVDCP 5V rc=%d\n", rc);
5373 goto out;
5374 }
5375
5376 /* wait for HVDCP to lower to 5V */
5377 msleep(500);
5378 /*
5379 * Check if the same hvdcp session is in progress. src_det should be
5380 * high and that we are still in 5V hvdcp
5381 */
5382 if (!is_src_detect_high(chip)) {
5383 pr_smb(PR_MISC, "src det low after 500mS sleep\n");
5384 goto out;
5385 }
5386
5387 pr_smb(PR_MISC, "HVDCP voting for 300mA ICL\n");
5388 rc = vote(chip->usb_icl_votable, HVDCP_ICL_VOTER, true, 300);
5389 if (rc < 0) {
5390 pr_err("Couldn't vote for 300mA HVDCP ICL rc=%d\n", rc);
5391 goto out;
5392 }
5393
5394 pr_smb(PR_MISC, "Disable AICL\n");
5395 smbchg_sec_masked_write(chip, chip->usb_chgpth_base + USB_AICL_CFG,
5396 AICL_EN_BIT, 0);
5397
5398 chip->hvdcp_3_det_ignore_uv = true;
5399
5400 /* re-run APSD */
5401 rc = rerun_apsd(chip);
5402 if (rc) {
5403 pr_err("APSD rerun failed\n");
5404 goto out;
5405 }
5406
5407 chip->hvdcp_3_det_ignore_uv = false;
5408
5409 pr_smb(PR_MISC, "Enable AICL\n");
5410 smbchg_sec_masked_write(chip, chip->usb_chgpth_base + USB_AICL_CFG,
5411 AICL_EN_BIT, AICL_EN_BIT);
5412 /*
5413 * DCP will switch to HVDCP in this time by removing the short
5414 * between DP DM
5415 */
5416 msleep(HVDCP_NOTIFY_MS);
5417 /*
5418 * Check if the same hvdcp session is in progress. src_det should be
5419 * high and the usb type should be none since APSD was disabled
5420 */
5421 if (!is_src_detect_high(chip)) {
5422 pr_smb(PR_MISC, "src det low after 2s sleep\n");
5423 rc = -EINVAL;
5424 goto out;
5425 }
5426
5427 /* We are set if HVDCP in 5V continuous mode */
5428 if (!is_hvdcp_5v_cont_mode(chip)) {
5429 pr_err("HVDCP could not be set in 5V continuous mode\n");
5430 goto out;
5431 }
5432
5433 return 0;
5434out:
5435 chip->hvdcp_3_det_ignore_uv = false;
5436 restore_from_hvdcp_detection(chip);
5437 return rc;
5438}
5439
5440static int smbchg_unprepare_for_pulsing_lite(struct smbchg_chip *chip)
5441{
5442 int rc = 0;
5443
5444 pr_smb(PR_MISC, "Forcing 9V HVDCP 2.0\n");
5445 rc = force_9v_hvdcp(chip);
5446 if (rc) {
5447 pr_err("Failed to force 9V HVDCP=%d\n", rc);
5448 return rc;
5449 }
5450
5451 pr_smb(PR_MISC, "Retracting HVDCP vote for ICL\n");
5452 rc = vote(chip->usb_icl_votable, HVDCP_ICL_VOTER, false, 0);
5453 if (rc < 0)
5454 pr_err("Couldn't retract HVDCP ICL vote rc=%d\n", rc);
5455
Subbaraman Narayanamurthy986bff52016-03-21 15:55:19 -07005456 smbchg_handle_hvdcp3_disable(chip);
5457
Kiran Gunda1bc78922017-09-19 13:09:44 +05305458 return rc;
5459}
5460
5461#define CMD_HVDCP_2 0x43
5462#define SINGLE_INCREMENT BIT(0)
5463#define SINGLE_DECREMENT BIT(1)
5464static int smbchg_dp_pulse_lite(struct smbchg_chip *chip)
5465{
5466 int rc = 0;
5467
5468 pr_smb(PR_MISC, "Increment DP\n");
5469 rc = smbchg_masked_write(chip, chip->usb_chgpth_base + CMD_HVDCP_2,
5470 SINGLE_INCREMENT, SINGLE_INCREMENT);
5471 if (rc)
5472 pr_err("Single-increment failed rc=%d\n", rc);
5473
5474 return rc;
5475}
5476
5477static int smbchg_dm_pulse_lite(struct smbchg_chip *chip)
5478{
5479 int rc = 0;
5480
5481 pr_smb(PR_MISC, "Decrement DM\n");
5482 rc = smbchg_masked_write(chip, chip->usb_chgpth_base + CMD_HVDCP_2,
5483 SINGLE_DECREMENT, SINGLE_DECREMENT);
5484 if (rc)
5485 pr_err("Single-decrement failed rc=%d\n", rc);
5486
5487 return rc;
5488}
5489
5490static int smbchg_hvdcp3_confirmed(struct smbchg_chip *chip)
5491{
5492 int rc = 0;
5493
5494 /*
5495 * reset the enabled once flag for parallel charging because this is
5496 * effectively a new insertion.
5497 */
5498 chip->parallel.enabled_once = false;
5499
5500 pr_smb(PR_MISC, "Retracting HVDCP vote for ICL\n");
5501 rc = vote(chip->usb_icl_votable, HVDCP_ICL_VOTER, false, 0);
5502 if (rc < 0)
5503 pr_err("Couldn't retract HVDCP ICL vote rc=%d\n", rc);
5504
5505 smbchg_change_usb_supply_type(chip, POWER_SUPPLY_TYPE_USB_HVDCP_3);
5506
5507 return rc;
5508}
5509
5510static int smbchg_dp_dm(struct smbchg_chip *chip, int val)
5511{
5512 int rc = 0;
5513 int target_icl_vote_ma;
5514
5515 switch (val) {
5516 case POWER_SUPPLY_DP_DM_PREPARE:
5517 if (!is_hvdcp_present(chip)) {
5518 pr_err("No pulsing unless HVDCP\n");
5519 return -ENODEV;
5520 }
5521 if (chip->schg_version == QPNP_SCHG_LITE)
5522 rc = smbchg_prepare_for_pulsing_lite(chip);
5523 else
5524 rc = smbchg_prepare_for_pulsing(chip);
5525 break;
5526 case POWER_SUPPLY_DP_DM_UNPREPARE:
5527 if (chip->schg_version == QPNP_SCHG_LITE)
5528 rc = smbchg_unprepare_for_pulsing_lite(chip);
5529 else
5530 rc = smbchg_unprepare_for_pulsing(chip);
5531 break;
5532 case POWER_SUPPLY_DP_DM_CONFIRMED_HVDCP3:
5533 rc = smbchg_hvdcp3_confirmed(chip);
5534 break;
5535 case POWER_SUPPLY_DP_DM_DP_PULSE:
5536 if (chip->schg_version == QPNP_SCHG)
5537 rc = set_usb_psy_dp_dm(chip,
5538 POWER_SUPPLY_DP_DM_DP_PULSE);
5539 else
5540 rc = smbchg_dp_pulse_lite(chip);
5541 if (!rc)
5542 chip->pulse_cnt++;
5543 pr_smb(PR_MISC, "pulse_cnt = %d\n", chip->pulse_cnt);
5544 break;
5545 case POWER_SUPPLY_DP_DM_DM_PULSE:
5546 if (chip->schg_version == QPNP_SCHG)
5547 rc = set_usb_psy_dp_dm(chip,
5548 POWER_SUPPLY_DP_DM_DM_PULSE);
5549 else
5550 rc = smbchg_dm_pulse_lite(chip);
5551 if (!rc && chip->pulse_cnt)
5552 chip->pulse_cnt--;
5553 pr_smb(PR_MISC, "pulse_cnt = %d\n", chip->pulse_cnt);
5554 break;
5555 case POWER_SUPPLY_DP_DM_HVDCP3_SUPPORTED:
5556 chip->hvdcp3_supported = true;
5557 pr_smb(PR_MISC, "HVDCP3 supported\n");
5558 break;
5559 case POWER_SUPPLY_DP_DM_ICL_DOWN:
5560 chip->usb_icl_delta -= 100;
5561 target_icl_vote_ma = get_client_vote(chip->usb_icl_votable,
5562 PSY_ICL_VOTER);
5563 vote(chip->usb_icl_votable, SW_AICL_ICL_VOTER, true,
5564 target_icl_vote_ma + chip->usb_icl_delta);
5565 break;
5566 case POWER_SUPPLY_DP_DM_ICL_UP:
5567 chip->usb_icl_delta += 100;
5568 target_icl_vote_ma = get_client_vote(chip->usb_icl_votable,
5569 PSY_ICL_VOTER);
5570 vote(chip->usb_icl_votable, SW_AICL_ICL_VOTER, true,
5571 target_icl_vote_ma + chip->usb_icl_delta);
5572 break;
5573 default:
5574 break;
5575 }
5576
5577 return rc;
5578}
5579
5580static void update_typec_capability_status(struct smbchg_chip *chip,
5581 const union power_supply_propval *val)
5582{
5583 pr_smb(PR_TYPEC, "typec capability = %dma\n", val->intval);
5584
5585 pr_debug("changing ICL from %dma to %dma\n", chip->typec_current_ma,
5586 val->intval);
5587 chip->typec_current_ma = val->intval;
5588 smbchg_change_usb_supply_type(chip, chip->usb_supply_type);
5589}
5590
5591static void update_typec_otg_status(struct smbchg_chip *chip, int mode,
5592 bool force)
5593{
5594 union power_supply_propval pval = {0, };
Kiran Gundaee7cef82017-09-20 17:44:16 +05305595
Kiran Gunda1bc78922017-09-19 13:09:44 +05305596 pr_smb(PR_TYPEC, "typec mode = %d\n", mode);
5597
5598 if (mode == POWER_SUPPLY_TYPE_DFP) {
5599 chip->typec_dfp = true;
5600 pval.intval = 1;
5601 extcon_set_cable_state_(chip->extcon, EXTCON_USB_HOST,
5602 chip->typec_dfp);
5603 /* update FG */
5604 set_property_on_fg(chip, POWER_SUPPLY_PROP_STATUS,
5605 get_prop_batt_status(chip));
5606 } else if (force || chip->typec_dfp) {
5607 chip->typec_dfp = false;
5608 pval.intval = 0;
5609 extcon_set_cable_state_(chip->extcon, EXTCON_USB_HOST,
5610 chip->typec_dfp);
5611 /* update FG */
5612 set_property_on_fg(chip, POWER_SUPPLY_PROP_STATUS,
5613 get_prop_batt_status(chip));
5614 }
5615}
5616
5617static int smbchg_usb_get_property(struct power_supply *psy,
5618 enum power_supply_property psp,
5619 union power_supply_propval *val)
5620{
5621 struct smbchg_chip *chip = power_supply_get_drvdata(psy);
5622
5623 switch (psp) {
5624 case POWER_SUPPLY_PROP_CURRENT_MAX:
5625 val->intval = chip->usb_current_max;
5626 break;
5627 case POWER_SUPPLY_PROP_PRESENT:
5628 val->intval = chip->usb_present;
5629 break;
5630 case POWER_SUPPLY_PROP_ONLINE:
5631 val->intval = chip->usb_online;
5632 break;
5633 case POWER_SUPPLY_PROP_TYPE:
5634 val->intval = chip->usb_supply_type;
5635 break;
5636 case POWER_SUPPLY_PROP_HEALTH:
5637 val->intval = chip->usb_health;
5638 break;
5639 default:
5640 return -EINVAL;
5641 }
5642 return 0;
5643}
5644
5645static int smbchg_usb_set_property(struct power_supply *psy,
5646 enum power_supply_property psp,
5647 const union power_supply_propval *val)
5648{
5649 struct smbchg_chip *chip = power_supply_get_drvdata(psy);
5650
5651 switch (psp) {
5652 case POWER_SUPPLY_PROP_CURRENT_MAX:
5653 chip->usb_current_max = val->intval;
5654 break;
5655 case POWER_SUPPLY_PROP_ONLINE:
5656 chip->usb_online = val->intval;
5657 break;
5658 default:
5659 return -EINVAL;
5660 }
5661
5662 power_supply_changed(psy);
5663 return 0;
5664}
5665
5666static int
Kiran Gundaee7cef82017-09-20 17:44:16 +05305667smbchg_usb_is_writeable(struct power_supply *psy,
5668 enum power_supply_property psp)
Kiran Gunda1bc78922017-09-19 13:09:44 +05305669{
5670 switch (psp) {
5671 case POWER_SUPPLY_PROP_CURRENT_MAX:
5672 return 1;
5673 default:
5674 break;
5675 }
5676
5677 return 0;
5678}
5679
5680
5681static char *smbchg_usb_supplicants[] = {
5682 "battery",
5683 "bms",
5684};
5685
5686static enum power_supply_property smbchg_usb_properties[] = {
5687 POWER_SUPPLY_PROP_PRESENT,
5688 POWER_SUPPLY_PROP_ONLINE,
5689 POWER_SUPPLY_PROP_CURRENT_MAX,
5690 POWER_SUPPLY_PROP_TYPE,
5691 POWER_SUPPLY_PROP_HEALTH,
5692};
5693
5694#define CHARGE_OUTPUT_VTG_RATIO 840
5695static int smbchg_get_iusb(struct smbchg_chip *chip)
5696{
5697 int rc, iusb_ua = -EINVAL;
5698 struct qpnp_vadc_result adc_result;
5699
5700 if (!is_usb_present(chip) && !is_dc_present(chip))
5701 return 0;
5702
5703 if (chip->vchg_vadc_dev && chip->vchg_adc_channel != -EINVAL) {
5704 rc = qpnp_vadc_read(chip->vchg_vadc_dev,
5705 chip->vchg_adc_channel, &adc_result);
5706 if (rc) {
5707 pr_smb(PR_STATUS,
5708 "error in VCHG (channel-%d) read rc = %d\n",
5709 chip->vchg_adc_channel, rc);
5710 return 0;
5711 }
5712 iusb_ua = div_s64(adc_result.physical * 1000,
5713 CHARGE_OUTPUT_VTG_RATIO);
5714 }
5715
5716 return iusb_ua;
5717}
5718
5719static enum power_supply_property smbchg_battery_properties[] = {
5720 POWER_SUPPLY_PROP_STATUS,
5721 POWER_SUPPLY_PROP_PRESENT,
5722 POWER_SUPPLY_PROP_BATTERY_CHARGING_ENABLED,
5723 POWER_SUPPLY_PROP_CHARGING_ENABLED,
5724 POWER_SUPPLY_PROP_CHARGE_TYPE,
5725 POWER_SUPPLY_PROP_CAPACITY,
5726 POWER_SUPPLY_PROP_HEALTH,
5727 POWER_SUPPLY_PROP_TECHNOLOGY,
5728 POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL,
5729 POWER_SUPPLY_PROP_FLASH_CURRENT_MAX,
5730 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
5731 POWER_SUPPLY_PROP_VOLTAGE_MAX,
5732 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
5733 POWER_SUPPLY_PROP_CURRENT_NOW,
5734 POWER_SUPPLY_PROP_TEMP,
5735 POWER_SUPPLY_PROP_VOLTAGE_NOW,
5736 POWER_SUPPLY_PROP_SAFETY_TIMER_ENABLE,
5737 POWER_SUPPLY_PROP_INPUT_CURRENT_MAX,
5738 POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED,
5739 POWER_SUPPLY_PROP_INPUT_CURRENT_NOW,
5740 POWER_SUPPLY_PROP_FLASH_ACTIVE,
5741 POWER_SUPPLY_PROP_FLASH_TRIGGER,
5742 POWER_SUPPLY_PROP_DP_DM,
5743 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMITED,
5744 POWER_SUPPLY_PROP_RERUN_AICL,
5745 POWER_SUPPLY_PROP_RESTRICTED_CHARGING,
Subbaraman Narayanamurthy986bff52016-03-21 15:55:19 -07005746 POWER_SUPPLY_PROP_ALLOW_HVDCP3,
Kiran Gunda1bc78922017-09-19 13:09:44 +05305747};
5748
5749static int smbchg_battery_set_property(struct power_supply *psy,
5750 enum power_supply_property prop,
5751 const union power_supply_propval *val)
5752{
5753 int rc = 0;
5754 struct smbchg_chip *chip = power_supply_get_drvdata(psy);
5755
5756 switch (prop) {
5757 case POWER_SUPPLY_PROP_BATTERY_CHARGING_ENABLED:
5758 vote(chip->battchg_suspend_votable, BATTCHG_USER_EN_VOTER,
5759 !val->intval, 0);
5760 break;
5761 case POWER_SUPPLY_PROP_CHARGING_ENABLED:
5762 rc = vote(chip->usb_suspend_votable, USER_EN_VOTER,
5763 !val->intval, 0);
5764 rc = vote(chip->dc_suspend_votable, USER_EN_VOTER,
5765 !val->intval, 0);
5766 chip->chg_enabled = val->intval;
5767 schedule_work(&chip->usb_set_online_work);
5768 break;
5769 case POWER_SUPPLY_PROP_CAPACITY:
5770 chip->fake_battery_soc = val->intval;
5771 if (chip->batt_psy)
5772 power_supply_changed(chip->batt_psy);
5773 break;
5774 case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL:
5775 smbchg_system_temp_level_set(chip, val->intval);
5776 break;
5777 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
5778 rc = smbchg_set_fastchg_current_user(chip, val->intval / 1000);
5779 break;
5780 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
5781 rc = smbchg_float_voltage_set(chip, val->intval);
5782 break;
5783 case POWER_SUPPLY_PROP_SAFETY_TIMER_ENABLE:
5784 rc = smbchg_safety_timer_enable(chip, val->intval);
5785 break;
5786 case POWER_SUPPLY_PROP_FLASH_ACTIVE:
5787 rc = smbchg_switch_buck_frequency(chip, val->intval);
5788 if (rc) {
5789 pr_err("Couldn't switch buck frequency, rc=%d\n", rc);
5790 /*
5791 * Trigger a panic if there is an error while switching
5792 * buck frequency. This will prevent LS FET damage.
5793 */
Kiran Gundaee7cef82017-09-20 17:44:16 +05305794 WARN_ON(1);
Kiran Gunda1bc78922017-09-19 13:09:44 +05305795 }
5796
5797 rc = smbchg_otg_pulse_skip_disable(chip,
5798 REASON_FLASH_ENABLED, val->intval);
5799 break;
5800 case POWER_SUPPLY_PROP_FLASH_TRIGGER:
5801 chip->flash_triggered = !!val->intval;
5802 smbchg_icl_loop_disable_check(chip);
5803 break;
5804 case POWER_SUPPLY_PROP_FORCE_TLIM:
5805 rc = smbchg_force_tlim_en(chip, val->intval);
5806 break;
5807 case POWER_SUPPLY_PROP_DP_DM:
5808 rc = smbchg_dp_dm(chip, val->intval);
5809 break;
5810 case POWER_SUPPLY_PROP_RERUN_AICL:
5811 smbchg_rerun_aicl(chip);
5812 break;
5813 case POWER_SUPPLY_PROP_RESTRICTED_CHARGING:
5814 rc = smbchg_restricted_charging(chip, val->intval);
5815 break;
5816 case POWER_SUPPLY_PROP_CURRENT_CAPABILITY:
5817 if (chip->typec_psy)
5818 update_typec_capability_status(chip, val);
5819 break;
5820 case POWER_SUPPLY_PROP_TYPEC_MODE:
5821 if (chip->typec_psy)
5822 update_typec_otg_status(chip, val->intval, false);
5823 break;
Subbaraman Narayanamurthy986bff52016-03-21 15:55:19 -07005824 case POWER_SUPPLY_PROP_ALLOW_HVDCP3:
5825 if (chip->allow_hvdcp3_detection != val->intval) {
5826 chip->allow_hvdcp3_detection = !!val->intval;
5827 power_supply_changed(chip->batt_psy);
5828 }
5829 break;
Kiran Gunda1bc78922017-09-19 13:09:44 +05305830 default:
5831 return -EINVAL;
5832 }
5833
5834 return rc;
5835}
5836
5837static int smbchg_battery_is_writeable(struct power_supply *psy,
5838 enum power_supply_property prop)
5839{
5840 int rc;
5841
5842 switch (prop) {
5843 case POWER_SUPPLY_PROP_BATTERY_CHARGING_ENABLED:
5844 case POWER_SUPPLY_PROP_CHARGING_ENABLED:
5845 case POWER_SUPPLY_PROP_CAPACITY:
5846 case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL:
5847 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
5848 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
5849 case POWER_SUPPLY_PROP_SAFETY_TIMER_ENABLE:
5850 case POWER_SUPPLY_PROP_DP_DM:
5851 case POWER_SUPPLY_PROP_RERUN_AICL:
5852 case POWER_SUPPLY_PROP_RESTRICTED_CHARGING:
Subbaraman Narayanamurthy986bff52016-03-21 15:55:19 -07005853 case POWER_SUPPLY_PROP_ALLOW_HVDCP3:
Kiran Gunda1bc78922017-09-19 13:09:44 +05305854 rc = 1;
5855 break;
5856 default:
5857 rc = 0;
5858 break;
5859 }
5860 return rc;
5861}
5862
5863static int smbchg_battery_get_property(struct power_supply *psy,
5864 enum power_supply_property prop,
5865 union power_supply_propval *val)
5866{
5867 struct smbchg_chip *chip = power_supply_get_drvdata(psy);
5868
5869 switch (prop) {
5870 case POWER_SUPPLY_PROP_STATUS:
5871 val->intval = get_prop_batt_status(chip);
5872 break;
5873 case POWER_SUPPLY_PROP_PRESENT:
5874 val->intval = get_prop_batt_present(chip);
5875 break;
5876 case POWER_SUPPLY_PROP_BATTERY_CHARGING_ENABLED:
5877 val->intval
5878 = get_effective_result(chip->battchg_suspend_votable);
5879 if (val->intval < 0) /* no votes */
5880 val->intval = 1;
5881 else
5882 val->intval = !val->intval;
5883 break;
5884 case POWER_SUPPLY_PROP_CHARGING_ENABLED:
5885 val->intval = chip->chg_enabled;
5886 break;
5887 case POWER_SUPPLY_PROP_CHARGE_TYPE:
5888 val->intval = get_prop_charge_type(chip);
5889 break;
5890 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
5891 val->intval = smbchg_float_voltage_get(chip);
5892 break;
5893 case POWER_SUPPLY_PROP_HEALTH:
5894 val->intval = get_prop_batt_health(chip);
5895 break;
5896 case POWER_SUPPLY_PROP_TECHNOLOGY:
5897 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
5898 break;
5899 case POWER_SUPPLY_PROP_FLASH_CURRENT_MAX:
5900 val->intval = smbchg_calc_max_flash_current(chip);
5901 break;
5902 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
5903 val->intval = chip->fastchg_current_ma * 1000;
5904 break;
5905 case POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL:
5906 val->intval = chip->therm_lvl_sel;
5907 break;
5908 case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX:
5909 val->intval = smbchg_get_aicl_level_ma(chip) * 1000;
5910 break;
5911 case POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED:
5912 val->intval = (int)chip->aicl_complete;
5913 break;
5914 case POWER_SUPPLY_PROP_RESTRICTED_CHARGING:
5915 val->intval = (int)chip->restricted_charging;
5916 break;
5917 /* properties from fg */
5918 case POWER_SUPPLY_PROP_CAPACITY:
5919 val->intval = get_prop_batt_capacity(chip);
5920 break;
5921 case POWER_SUPPLY_PROP_CURRENT_NOW:
5922 val->intval = get_prop_batt_current_now(chip);
5923 break;
5924 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
5925 val->intval = get_prop_batt_voltage_now(chip);
5926 break;
5927 case POWER_SUPPLY_PROP_TEMP:
5928 val->intval = get_prop_batt_temp(chip);
5929 break;
5930 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
5931 val->intval = get_prop_batt_voltage_max_design(chip);
5932 break;
5933 case POWER_SUPPLY_PROP_SAFETY_TIMER_ENABLE:
5934 val->intval = chip->safety_timer_en;
5935 break;
5936 case POWER_SUPPLY_PROP_FLASH_ACTIVE:
5937 val->intval = chip->otg_pulse_skip_dis;
5938 break;
5939 case POWER_SUPPLY_PROP_FLASH_TRIGGER:
5940 val->intval = chip->flash_triggered;
5941 break;
5942 case POWER_SUPPLY_PROP_DP_DM:
5943 val->intval = chip->pulse_cnt;
5944 break;
5945 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMITED:
5946 val->intval = smbchg_is_input_current_limited(chip);
5947 break;
5948 case POWER_SUPPLY_PROP_RERUN_AICL:
5949 val->intval = 0;
5950 break;
5951 case POWER_SUPPLY_PROP_INPUT_CURRENT_NOW:
5952 val->intval = smbchg_get_iusb(chip);
5953 break;
Subbaraman Narayanamurthy986bff52016-03-21 15:55:19 -07005954 case POWER_SUPPLY_PROP_ALLOW_HVDCP3:
5955 val->intval = chip->allow_hvdcp3_detection;
5956 break;
Kiran Gunda1bc78922017-09-19 13:09:44 +05305957 default:
5958 return -EINVAL;
5959 }
5960 return 0;
5961}
5962
5963static char *smbchg_dc_supplicants[] = {
5964 "bms",
5965};
5966
5967static enum power_supply_property smbchg_dc_properties[] = {
5968 POWER_SUPPLY_PROP_PRESENT,
5969 POWER_SUPPLY_PROP_ONLINE,
5970 POWER_SUPPLY_PROP_CHARGING_ENABLED,
5971 POWER_SUPPLY_PROP_CURRENT_MAX,
5972};
5973
5974static int smbchg_dc_set_property(struct power_supply *psy,
5975 enum power_supply_property prop,
5976 const union power_supply_propval *val)
5977{
5978 int rc = 0;
5979 struct smbchg_chip *chip = power_supply_get_drvdata(psy);
5980
5981 switch (prop) {
5982 case POWER_SUPPLY_PROP_CHARGING_ENABLED:
5983 rc = vote(chip->dc_suspend_votable, POWER_SUPPLY_EN_VOTER,
5984 !val->intval, 0);
5985 break;
5986 case POWER_SUPPLY_PROP_CURRENT_MAX:
5987 rc = vote(chip->dc_icl_votable, USER_ICL_VOTER, true,
5988 val->intval / 1000);
5989 break;
5990 default:
5991 return -EINVAL;
5992 }
5993
5994 return rc;
5995}
5996
5997static int smbchg_dc_get_property(struct power_supply *psy,
5998 enum power_supply_property prop,
5999 union power_supply_propval *val)
6000{
6001 struct smbchg_chip *chip = power_supply_get_drvdata(psy);
6002
6003 switch (prop) {
6004 case POWER_SUPPLY_PROP_PRESENT:
6005 val->intval = is_dc_present(chip);
6006 break;
6007 case POWER_SUPPLY_PROP_CHARGING_ENABLED:
6008 val->intval = get_effective_result(chip->dc_suspend_votable);
6009 if (val->intval < 0) /* no votes */
6010 val->intval = 1;
6011 else
6012 val->intval = !val->intval;
6013 break;
6014 case POWER_SUPPLY_PROP_ONLINE:
6015 /* return if dc is charging the battery */
6016 val->intval = (smbchg_get_pwr_path(chip) == PWR_PATH_DC)
6017 && (get_prop_batt_status(chip)
6018 == POWER_SUPPLY_STATUS_CHARGING);
6019 break;
6020 case POWER_SUPPLY_PROP_CURRENT_MAX:
6021 val->intval = chip->dc_max_current_ma * 1000;
6022 break;
6023 default:
6024 return -EINVAL;
6025 }
6026
6027 return 0;
6028}
6029
6030static int smbchg_dc_is_writeable(struct power_supply *psy,
6031 enum power_supply_property prop)
6032{
6033 int rc;
6034
6035 switch (prop) {
6036 case POWER_SUPPLY_PROP_CHARGING_ENABLED:
6037 case POWER_SUPPLY_PROP_CURRENT_MAX:
6038 rc = 1;
6039 break;
6040 default:
6041 rc = 0;
6042 break;
6043 }
6044 return rc;
6045}
6046
6047#define HOT_BAT_HARD_BIT BIT(0)
6048#define HOT_BAT_SOFT_BIT BIT(1)
6049#define COLD_BAT_HARD_BIT BIT(2)
6050#define COLD_BAT_SOFT_BIT BIT(3)
6051#define BAT_OV_BIT BIT(4)
6052#define BAT_LOW_BIT BIT(5)
6053#define BAT_MISSING_BIT BIT(6)
6054#define BAT_TERM_MISSING_BIT BIT(7)
6055static irqreturn_t batt_hot_handler(int irq, void *_chip)
6056{
6057 struct smbchg_chip *chip = _chip;
6058 u8 reg = 0;
6059
6060 smbchg_read(chip, &reg, chip->bat_if_base + RT_STS, 1);
6061 chip->batt_hot = !!(reg & HOT_BAT_HARD_BIT);
6062 pr_smb(PR_INTERRUPT, "triggered: 0x%02x\n", reg);
6063 smbchg_parallel_usb_check_ok(chip);
6064 if (chip->batt_psy)
6065 power_supply_changed(chip->batt_psy);
6066 smbchg_charging_status_change(chip);
6067 smbchg_wipower_check(chip);
6068 set_property_on_fg(chip, POWER_SUPPLY_PROP_HEALTH,
6069 get_prop_batt_health(chip));
6070 return IRQ_HANDLED;
6071}
6072
6073static irqreturn_t batt_cold_handler(int irq, void *_chip)
6074{
6075 struct smbchg_chip *chip = _chip;
6076 u8 reg = 0;
6077
6078 smbchg_read(chip, &reg, chip->bat_if_base + RT_STS, 1);
6079 chip->batt_cold = !!(reg & COLD_BAT_HARD_BIT);
6080 pr_smb(PR_INTERRUPT, "triggered: 0x%02x\n", reg);
6081 smbchg_parallel_usb_check_ok(chip);
6082 if (chip->batt_psy)
6083 power_supply_changed(chip->batt_psy);
6084 smbchg_charging_status_change(chip);
6085 smbchg_wipower_check(chip);
6086 set_property_on_fg(chip, POWER_SUPPLY_PROP_HEALTH,
6087 get_prop_batt_health(chip));
6088 return IRQ_HANDLED;
6089}
6090
6091static irqreturn_t batt_warm_handler(int irq, void *_chip)
6092{
6093 struct smbchg_chip *chip = _chip;
6094 u8 reg = 0;
6095
6096 smbchg_read(chip, &reg, chip->bat_if_base + RT_STS, 1);
6097 chip->batt_warm = !!(reg & HOT_BAT_SOFT_BIT);
6098 pr_smb(PR_INTERRUPT, "triggered: 0x%02x\n", reg);
6099 smbchg_parallel_usb_check_ok(chip);
6100 if (chip->batt_psy)
6101 power_supply_changed(chip->batt_psy);
6102 set_property_on_fg(chip, POWER_SUPPLY_PROP_HEALTH,
6103 get_prop_batt_health(chip));
6104 return IRQ_HANDLED;
6105}
6106
6107static irqreturn_t batt_cool_handler(int irq, void *_chip)
6108{
6109 struct smbchg_chip *chip = _chip;
6110 u8 reg = 0;
6111
6112 smbchg_read(chip, &reg, chip->bat_if_base + RT_STS, 1);
6113 chip->batt_cool = !!(reg & COLD_BAT_SOFT_BIT);
6114 pr_smb(PR_INTERRUPT, "triggered: 0x%02x\n", reg);
6115 smbchg_parallel_usb_check_ok(chip);
6116 if (chip->batt_psy)
6117 power_supply_changed(chip->batt_psy);
6118 set_property_on_fg(chip, POWER_SUPPLY_PROP_HEALTH,
6119 get_prop_batt_health(chip));
6120 return IRQ_HANDLED;
6121}
6122
6123static irqreturn_t batt_pres_handler(int irq, void *_chip)
6124{
6125 struct smbchg_chip *chip = _chip;
6126 u8 reg = 0;
6127
6128 smbchg_read(chip, &reg, chip->bat_if_base + RT_STS, 1);
6129 chip->batt_present = !(reg & BAT_MISSING_BIT);
6130 pr_smb(PR_INTERRUPT, "triggered: 0x%02x\n", reg);
6131 if (chip->batt_psy)
6132 power_supply_changed(chip->batt_psy);
6133 smbchg_charging_status_change(chip);
6134 set_property_on_fg(chip, POWER_SUPPLY_PROP_HEALTH,
6135 get_prop_batt_health(chip));
6136 return IRQ_HANDLED;
6137}
6138
6139static irqreturn_t vbat_low_handler(int irq, void *_chip)
6140{
6141 pr_warn_ratelimited("vbat low\n");
6142 return IRQ_HANDLED;
6143}
6144
6145#define CHG_COMP_SFT_BIT BIT(3)
6146static irqreturn_t chg_error_handler(int irq, void *_chip)
6147{
6148 struct smbchg_chip *chip = _chip;
6149 int rc = 0;
6150 u8 reg;
6151
6152 pr_smb(PR_INTERRUPT, "chg-error triggered\n");
6153
6154 rc = smbchg_read(chip, &reg, chip->chgr_base + RT_STS, 1);
6155 if (rc < 0) {
6156 dev_err(chip->dev, "Unable to read RT_STS rc = %d\n", rc);
6157 } else {
6158 pr_smb(PR_INTERRUPT, "triggered: 0x%02x\n", reg);
6159 if (reg & CHG_COMP_SFT_BIT)
6160 set_property_on_fg(chip,
6161 POWER_SUPPLY_PROP_SAFETY_TIMER_EXPIRED,
6162 1);
6163 }
6164
6165 smbchg_parallel_usb_check_ok(chip);
6166 if (chip->batt_psy)
6167 power_supply_changed(chip->batt_psy);
6168 smbchg_charging_status_change(chip);
6169 smbchg_wipower_check(chip);
6170 return IRQ_HANDLED;
6171}
6172
6173static irqreturn_t fastchg_handler(int irq, void *_chip)
6174{
6175 struct smbchg_chip *chip = _chip;
6176
6177 pr_smb(PR_INTERRUPT, "p2f triggered\n");
6178 smbchg_parallel_usb_check_ok(chip);
6179 if (chip->batt_psy)
6180 power_supply_changed(chip->batt_psy);
6181 smbchg_charging_status_change(chip);
6182 smbchg_wipower_check(chip);
6183 return IRQ_HANDLED;
6184}
6185
6186static irqreturn_t chg_hot_handler(int irq, void *_chip)
6187{
6188 pr_warn_ratelimited("chg hot\n");
6189 smbchg_wipower_check(_chip);
6190 return IRQ_HANDLED;
6191}
6192
6193static irqreturn_t chg_term_handler(int irq, void *_chip)
6194{
6195 struct smbchg_chip *chip = _chip;
6196
6197 pr_smb(PR_INTERRUPT, "tcc triggered\n");
6198 /*
6199 * Charge termination is a pulse and not level triggered. That means,
6200 * TCC bit in RT_STS can get cleared by the time this interrupt is
6201 * handled. Instead of relying on that to determine whether the
6202 * charge termination had happened, we've to simply notify the FG
6203 * about this as long as the interrupt is handled.
6204 */
6205 set_property_on_fg(chip, POWER_SUPPLY_PROP_CHARGE_DONE, 1);
6206
6207 smbchg_parallel_usb_check_ok(chip);
6208 if (chip->batt_psy)
6209 power_supply_changed(chip->batt_psy);
6210 smbchg_charging_status_change(chip);
6211
6212 return IRQ_HANDLED;
6213}
6214
6215static irqreturn_t taper_handler(int irq, void *_chip)
6216{
6217 struct smbchg_chip *chip = _chip;
6218 u8 reg = 0;
6219
6220 taper_irq_en(chip, false);
6221 smbchg_read(chip, &reg, chip->chgr_base + RT_STS, 1);
6222 pr_smb(PR_INTERRUPT, "triggered: 0x%02x\n", reg);
6223 smbchg_parallel_usb_taper(chip);
6224 if (chip->batt_psy)
6225 power_supply_changed(chip->batt_psy);
6226 smbchg_charging_status_change(chip);
6227 smbchg_wipower_check(chip);
6228 return IRQ_HANDLED;
6229}
6230
6231static irqreturn_t recharge_handler(int irq, void *_chip)
6232{
6233 struct smbchg_chip *chip = _chip;
6234 u8 reg = 0;
6235
6236 smbchg_read(chip, &reg, chip->chgr_base + RT_STS, 1);
6237 pr_smb(PR_INTERRUPT, "triggered: 0x%02x\n", reg);
6238 smbchg_parallel_usb_check_ok(chip);
6239 if (chip->batt_psy)
6240 power_supply_changed(chip->batt_psy);
6241 smbchg_charging_status_change(chip);
6242 return IRQ_HANDLED;
6243}
6244
6245static irqreturn_t wdog_timeout_handler(int irq, void *_chip)
6246{
6247 struct smbchg_chip *chip = _chip;
6248 u8 reg = 0;
6249
6250 smbchg_read(chip, &reg, chip->misc_base + RT_STS, 1);
6251 pr_warn_ratelimited("wdog timeout rt_stat = 0x%02x\n", reg);
6252 if (chip->batt_psy)
6253 power_supply_changed(chip->batt_psy);
6254 smbchg_charging_status_change(chip);
6255 return IRQ_HANDLED;
6256}
6257
6258/**
6259 * power_ok_handler() - called when the switcher turns on or turns off
6260 * @chip: pointer to smbchg_chip
6261 * @rt_stat: the status bit indicating switcher turning on or off
6262 */
6263static irqreturn_t power_ok_handler(int irq, void *_chip)
6264{
6265 struct smbchg_chip *chip = _chip;
6266 u8 reg = 0;
6267
6268 smbchg_read(chip, &reg, chip->misc_base + RT_STS, 1);
6269 pr_smb(PR_INTERRUPT, "triggered: 0x%02x\n", reg);
6270 return IRQ_HANDLED;
6271}
6272
6273/**
6274 * dcin_uv_handler() - called when the dc voltage crosses the uv threshold
6275 * @chip: pointer to smbchg_chip
6276 * @rt_stat: the status bit indicating whether dc voltage is uv
6277 */
6278#define DCIN_UNSUSPEND_DELAY_MS 1000
6279static irqreturn_t dcin_uv_handler(int irq, void *_chip)
6280{
6281 struct smbchg_chip *chip = _chip;
6282 bool dc_present = is_dc_present(chip);
6283
6284 pr_smb(PR_STATUS, "chip->dc_present = %d dc_present = %d\n",
6285 chip->dc_present, dc_present);
6286
6287 if (chip->dc_present != dc_present) {
6288 /* dc changed */
6289 chip->dc_present = dc_present;
6290 if (chip->dc_psy_type != -EINVAL && chip->batt_psy)
6291 power_supply_changed(chip->dc_psy);
6292 smbchg_charging_status_change(chip);
6293 smbchg_aicl_deglitch_wa_check(chip);
6294 chip->vbat_above_headroom = false;
6295 }
6296
6297 smbchg_wipower_check(chip);
6298 return IRQ_HANDLED;
6299}
6300
6301/**
6302 * usbin_ov_handler() - this is called when an overvoltage condition occurs
6303 * @chip: pointer to smbchg_chip chip
6304 */
6305static irqreturn_t usbin_ov_handler(int irq, void *_chip)
6306{
6307 struct smbchg_chip *chip = _chip;
6308 int rc;
6309 u8 reg;
6310 bool usb_present;
6311
6312 rc = smbchg_read(chip, &reg, chip->usb_chgpth_base + RT_STS, 1);
6313 if (rc < 0) {
6314 dev_err(chip->dev, "Couldn't read usb rt status rc = %d\n", rc);
6315 goto out;
6316 }
6317
6318 /* OV condition is detected. Notify it to USB psy */
6319 if (reg & USBIN_OV_BIT) {
6320 chip->usb_ov_det = true;
6321 pr_smb(PR_MISC, "setting usb psy health OV\n");
6322 chip->usb_health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
6323 power_supply_changed(chip->usb_psy);
6324 } else {
6325 chip->usb_ov_det = false;
6326 /* If USB is present, then handle the USB insertion */
6327 usb_present = is_usb_present(chip);
6328 if (usb_present)
6329 update_usb_status(chip, usb_present, false);
6330 }
6331out:
6332 return IRQ_HANDLED;
6333}
6334
6335/**
6336 * usbin_uv_handler() - this is called when USB charger is removed
6337 * @chip: pointer to smbchg_chip chip
6338 * @rt_stat: the status bit indicating chg insertion/removal
6339 */
6340#define ICL_MODE_MASK SMB_MASK(5, 4)
6341#define ICL_MODE_HIGH_CURRENT 0
6342static irqreturn_t usbin_uv_handler(int irq, void *_chip)
6343{
6344 struct smbchg_chip *chip = _chip;
6345 int aicl_level = smbchg_get_aicl_level_ma(chip);
6346 int rc;
6347 u8 reg;
6348
6349 rc = smbchg_read(chip, &reg, chip->usb_chgpth_base + RT_STS, 1);
6350 if (rc) {
6351 pr_err("could not read rt sts: %d", rc);
6352 goto out;
6353 }
6354
6355 pr_smb(PR_STATUS,
6356 "%s chip->usb_present = %d rt_sts = 0x%02x hvdcp_3_det_ignore_uv = %d aicl = %d\n",
6357 chip->hvdcp_3_det_ignore_uv ? "Ignoring":"",
6358 chip->usb_present, reg, chip->hvdcp_3_det_ignore_uv,
6359 aicl_level);
6360
6361 /*
6362 * set usb_psy's dp=f dm=f if this is a new insertion, i.e. it is
6363 * not already src_detected and usbin_uv is seen falling
6364 */
6365 if (!(reg & USBIN_UV_BIT) && !(reg & USBIN_SRC_DET_BIT)) {
6366 pr_smb(PR_MISC, "setting usb dp=f dm=f\n");
6367 if (chip->dpdm_reg && !regulator_is_enabled(chip->dpdm_reg))
6368 rc = regulator_enable(chip->dpdm_reg);
6369 if (rc < 0) {
6370 pr_err("Couldn't enable DP/DM for pulsing rc=%d\n", rc);
6371 return rc;
6372 }
6373 }
6374
6375 if (reg & USBIN_UV_BIT)
6376 complete_all(&chip->usbin_uv_raised);
6377 else
6378 complete_all(&chip->usbin_uv_lowered);
6379
6380 if (chip->hvdcp_3_det_ignore_uv)
6381 goto out;
6382
6383 if ((reg & USBIN_UV_BIT) && (reg & USBIN_SRC_DET_BIT)) {
6384 pr_smb(PR_STATUS, "Very weak charger detected\n");
6385 chip->very_weak_charger = true;
6386 rc = smbchg_read(chip, &reg,
6387 chip->usb_chgpth_base + ICL_STS_2_REG, 1);
6388 if (rc) {
6389 dev_err(chip->dev, "Could not read usb icl sts 2: %d\n",
6390 rc);
6391 goto out;
6392 }
6393 if ((reg & ICL_MODE_MASK) != ICL_MODE_HIGH_CURRENT) {
6394 /*
6395 * If AICL is not even enabled, this is either an
6396 * SDP or a grossly out of spec charger. Do not
6397 * draw any current from it.
6398 */
6399 rc = vote(chip->usb_suspend_votable,
6400 WEAK_CHARGER_EN_VOTER, true, 0);
6401 if (rc < 0)
6402 pr_err("could not disable charger: %d", rc);
6403 } else if (aicl_level == chip->tables.usb_ilim_ma_table[0]) {
6404 /*
6405 * we are in a situation where the adapter is not able
6406 * to supply even 300mA. Disable hw aicl reruns else it
6407 * is only a matter of time when we get back here again
6408 */
6409 rc = vote(chip->hw_aicl_rerun_disable_votable,
6410 WEAK_CHARGER_HW_AICL_VOTER, true, 0);
6411 if (rc < 0)
6412 pr_err("Couldn't disable hw aicl rerun rc=%d\n",
6413 rc);
6414 }
6415 pr_smb(PR_MISC, "setting usb psy health UNSPEC_FAILURE\n");
6416 chip->usb_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
6417 power_supply_changed(chip->usb_psy);
6418 schedule_work(&chip->usb_set_online_work);
6419 }
6420
6421 smbchg_wipower_check(chip);
6422out:
6423 return IRQ_HANDLED;
6424}
6425
6426/**
6427 * src_detect_handler() - this is called on rising edge when USB charger type
6428 * is detected and on falling edge when USB voltage falls
6429 * below the coarse detect voltage(1V), use it for
6430 * handling USB charger insertion and removal.
6431 * @chip: pointer to smbchg_chip
6432 * @rt_stat: the status bit indicating chg insertion/removal
6433 */
6434static irqreturn_t src_detect_handler(int irq, void *_chip)
6435{
6436 struct smbchg_chip *chip = _chip;
6437 bool usb_present = is_usb_present(chip);
6438 bool src_detect = is_src_detect_high(chip);
6439 int rc;
6440
6441 pr_smb(PR_STATUS,
6442 "%s chip->usb_present = %d usb_present = %d src_detect = %d hvdcp_3_det_ignore_uv=%d\n",
6443 chip->hvdcp_3_det_ignore_uv ? "Ignoring":"",
6444 chip->usb_present, usb_present, src_detect,
6445 chip->hvdcp_3_det_ignore_uv);
6446
6447 if (src_detect)
6448 complete_all(&chip->src_det_raised);
6449 else
6450 complete_all(&chip->src_det_lowered);
6451
6452 if (chip->hvdcp_3_det_ignore_uv)
6453 goto out;
6454
6455 /*
6456 * When VBAT is above the AICL threshold (4.25V) - 180mV (4.07V),
6457 * an input collapse due to AICL will actually cause an USBIN_UV
6458 * interrupt to fire as well.
6459 *
6460 * Handle USB insertions and removals in the source detect handler
6461 * instead of the USBIN_UV handler since the latter is untrustworthy
6462 * when the battery voltage is high.
6463 */
6464 chip->very_weak_charger = false;
6465 /*
6466 * a src detect marks a new insertion or a real removal,
6467 * vote for enable aicl hw reruns
6468 */
6469 rc = vote(chip->hw_aicl_rerun_disable_votable,
6470 WEAK_CHARGER_HW_AICL_VOTER, false, 0);
6471 if (rc < 0)
6472 pr_err("Couldn't enable hw aicl rerun rc=%d\n", rc);
6473
6474 rc = vote(chip->usb_suspend_votable, WEAK_CHARGER_EN_VOTER, false, 0);
6475 if (rc < 0)
6476 pr_err("could not enable charger: %d\n", rc);
6477
6478 if (src_detect) {
6479 update_usb_status(chip, usb_present, 0);
6480 } else {
6481 update_usb_status(chip, 0, false);
6482 chip->aicl_irq_count = 0;
6483 }
6484out:
6485 return IRQ_HANDLED;
6486}
6487
6488/**
6489 * otg_oc_handler() - called when the usb otg goes over current
6490 */
6491#define NUM_OTG_RETRIES 5
6492#define OTG_OC_RETRY_DELAY_US 50000
6493static irqreturn_t otg_oc_handler(int irq, void *_chip)
6494{
6495 int rc;
6496 struct smbchg_chip *chip = _chip;
6497 s64 elapsed_us = ktime_us_delta(ktime_get(), chip->otg_enable_time);
6498
6499 pr_smb(PR_INTERRUPT, "triggered\n");
6500
6501 if (chip->schg_version == QPNP_SCHG_LITE) {
6502 pr_warn("OTG OC triggered - OTG disabled\n");
6503 return IRQ_HANDLED;
6504 }
6505
6506 if (elapsed_us > OTG_OC_RETRY_DELAY_US)
6507 chip->otg_retries = 0;
6508
6509 /*
6510 * Due to a HW bug in the PMI8994 charger, the current inrush that
6511 * occurs when connecting certain OTG devices can cause the OTG
6512 * overcurrent protection to trip.
6513 *
6514 * The work around is to try reenabling the OTG when getting an
6515 * overcurrent interrupt once.
6516 */
6517 if (chip->otg_retries < NUM_OTG_RETRIES) {
6518 chip->otg_retries += 1;
6519 pr_smb(PR_STATUS,
6520 "Retrying OTG enable. Try #%d, elapsed_us %lld\n",
6521 chip->otg_retries, elapsed_us);
6522 rc = otg_oc_reset(chip);
6523 if (rc)
6524 pr_err("Failed to reset OTG OC state rc=%d\n", rc);
6525 chip->otg_enable_time = ktime_get();
6526 }
6527 return IRQ_HANDLED;
6528}
6529
6530/**
6531 * otg_fail_handler() - called when the usb otg fails
6532 * (when vbat < OTG UVLO threshold)
6533 */
6534static irqreturn_t otg_fail_handler(int irq, void *_chip)
6535{
6536 pr_smb(PR_INTERRUPT, "triggered\n");
6537 return IRQ_HANDLED;
6538}
6539
6540/**
6541 * aicl_done_handler() - called when the usb AICL algorithm is finished
6542 * and a current is set.
6543 */
6544static irqreturn_t aicl_done_handler(int irq, void *_chip)
6545{
6546 struct smbchg_chip *chip = _chip;
6547 bool usb_present = is_usb_present(chip);
6548 int aicl_level = smbchg_get_aicl_level_ma(chip);
6549
6550 pr_smb(PR_INTERRUPT, "triggered, aicl: %d\n", aicl_level);
6551
6552 increment_aicl_count(chip);
6553
6554 if (usb_present)
6555 smbchg_parallel_usb_check_ok(chip);
6556
6557 if (chip->aicl_complete && chip->batt_psy)
6558 power_supply_changed(chip->batt_psy);
6559
6560 return IRQ_HANDLED;
6561}
6562
6563/**
6564 * usbid_change_handler() - called when the usb RID changes.
6565 * This is used mostly for detecting OTG
6566 */
6567static irqreturn_t usbid_change_handler(int irq, void *_chip)
6568{
6569 struct smbchg_chip *chip = _chip;
6570 bool otg_present;
6571
6572 pr_smb(PR_INTERRUPT, "triggered\n");
6573
6574 otg_present = is_otg_present(chip);
6575 pr_smb(PR_MISC, "setting usb psy OTG = %d\n",
6576 otg_present ? 1 : 0);
6577
6578 extcon_set_cable_state_(chip->extcon, EXTCON_USB_HOST, otg_present);
6579
6580 if (otg_present)
6581 pr_smb(PR_STATUS, "OTG detected\n");
6582
6583 /* update FG */
6584 set_property_on_fg(chip, POWER_SUPPLY_PROP_STATUS,
6585 get_prop_batt_status(chip));
6586
6587 return IRQ_HANDLED;
6588}
6589
6590static int determine_initial_status(struct smbchg_chip *chip)
6591{
6592 union power_supply_propval type = {0, };
6593
6594 /*
6595 * It is okay to read the interrupt status here since
6596 * interrupts aren't requested. reading interrupt status
6597 * clears the interrupt so be careful to read interrupt
6598 * status only in interrupt handling code
6599 */
6600
6601 batt_pres_handler(0, chip);
6602 batt_hot_handler(0, chip);
6603 batt_warm_handler(0, chip);
6604 batt_cool_handler(0, chip);
6605 batt_cold_handler(0, chip);
6606 if (chip->typec_psy) {
6607 get_property_from_typec(chip, POWER_SUPPLY_PROP_TYPE, &type);
6608 update_typec_otg_status(chip, type.intval, true);
6609 } else {
6610 usbid_change_handler(0, chip);
6611 }
6612 src_detect_handler(0, chip);
6613
6614 chip->usb_present = is_usb_present(chip);
6615 chip->dc_present = is_dc_present(chip);
6616
6617 if (chip->usb_present) {
6618 int rc = 0;
Kiran Gundaee7cef82017-09-20 17:44:16 +05306619
Kiran Gunda1bc78922017-09-19 13:09:44 +05306620 pr_smb(PR_MISC, "setting usb dp=f dm=f\n");
6621 if (chip->dpdm_reg && !regulator_is_enabled(chip->dpdm_reg))
6622 rc = regulator_enable(chip->dpdm_reg);
6623 if (rc < 0) {
6624 pr_err("Couldn't enable DP/DM for pulsing rc=%d\n", rc);
6625 return rc;
6626 }
6627 handle_usb_insertion(chip);
6628 } else {
6629 handle_usb_removal(chip);
6630 }
6631
6632 return 0;
6633}
6634
6635static int prechg_time[] = {
6636 24,
6637 48,
6638 96,
6639 192,
6640};
6641static int chg_time[] = {
6642 192,
6643 384,
6644 768,
6645 1536,
6646};
6647
6648enum bpd_type {
6649 BPD_TYPE_BAT_NONE,
6650 BPD_TYPE_BAT_ID,
6651 BPD_TYPE_BAT_THM,
6652 BPD_TYPE_BAT_THM_BAT_ID,
6653 BPD_TYPE_DEFAULT,
6654};
6655
6656static const char * const bpd_label[] = {
6657 [BPD_TYPE_BAT_NONE] = "bpd_none",
6658 [BPD_TYPE_BAT_ID] = "bpd_id",
6659 [BPD_TYPE_BAT_THM] = "bpd_thm",
6660 [BPD_TYPE_BAT_THM_BAT_ID] = "bpd_thm_id",
6661};
6662
6663static inline int get_bpd(const char *name)
6664{
6665 int i = 0;
Kiran Gundaee7cef82017-09-20 17:44:16 +05306666
Kiran Gunda1bc78922017-09-19 13:09:44 +05306667 for (i = 0; i < ARRAY_SIZE(bpd_label); i++) {
6668 if (strcmp(bpd_label[i], name) == 0)
6669 return i;
6670 }
6671 return -EINVAL;
6672}
6673
6674#define REVISION1_REG 0x0
6675#define DIG_MINOR 0
6676#define DIG_MAJOR 1
6677#define ANA_MINOR 2
6678#define ANA_MAJOR 3
6679#define CHGR_CFG1 0xFB
6680#define RECHG_THRESHOLD_SRC_BIT BIT(1)
6681#define TERM_I_SRC_BIT BIT(2)
6682#define TERM_SRC_FG BIT(2)
6683#define CHG_INHIB_CFG_REG 0xF7
6684#define CHG_INHIBIT_50MV_VAL 0x00
6685#define CHG_INHIBIT_100MV_VAL 0x01
6686#define CHG_INHIBIT_200MV_VAL 0x02
6687#define CHG_INHIBIT_300MV_VAL 0x03
6688#define CHG_INHIBIT_MASK 0x03
6689#define USE_REGISTER_FOR_CURRENT BIT(2)
6690#define CHGR_CFG2 0xFC
6691#define CHG_EN_SRC_BIT BIT(7)
6692#define CHG_EN_POLARITY_BIT BIT(6)
6693#define P2F_CHG_TRAN BIT(5)
6694#define CHG_BAT_OV_ECC BIT(4)
6695#define I_TERM_BIT BIT(3)
6696#define AUTO_RECHG_BIT BIT(2)
6697#define CHARGER_INHIBIT_BIT BIT(0)
6698#define USB51_COMMAND_POL BIT(2)
6699#define USB51AC_CTRL BIT(1)
6700#define TR_8OR32B 0xFE
6701#define BUCK_8_16_FREQ_BIT BIT(0)
6702#define BM_CFG 0xF3
6703#define BATT_MISSING_ALGO_BIT BIT(2)
6704#define BMD_PIN_SRC_MASK SMB_MASK(1, 0)
6705#define PIN_SRC_SHIFT 0
6706#define CHGR_CFG 0xFF
6707#define RCHG_LVL_BIT BIT(0)
6708#define VCHG_EN_BIT BIT(1)
6709#define VCHG_INPUT_CURRENT_BIT BIT(3)
6710#define CFG_AFVC 0xF6
6711#define VFLOAT_COMP_ENABLE_MASK SMB_MASK(2, 0)
6712#define TR_RID_REG 0xFA
6713#define FG_INPUT_FET_DELAY_BIT BIT(3)
6714#define TRIM_OPTIONS_7_0 0xF6
6715#define INPUT_MISSING_POLLER_EN_BIT BIT(3)
6716#define CHGR_CCMP_CFG 0xFA
6717#define JEITA_TEMP_HARD_LIMIT_BIT BIT(5)
6718#define HVDCP_ADAPTER_SEL_MASK SMB_MASK(5, 4)
6719#define HVDCP_ADAPTER_SEL_9V_BIT BIT(4)
6720#define HVDCP_AUTH_ALG_EN_BIT BIT(6)
6721#define CMD_APSD 0x41
6722#define APSD_RERUN_BIT BIT(0)
6723#define OTG_CFG 0xF1
6724#define HICCUP_ENABLED_BIT BIT(6)
6725#define OTG_PIN_POLARITY_BIT BIT(4)
6726#define OTG_PIN_ACTIVE_LOW BIT(4)
6727#define OTG_EN_CTRL_MASK SMB_MASK(3, 2)
6728#define OTG_PIN_CTRL_RID_DIS 0x04
6729#define OTG_CMD_CTRL_RID_EN 0x08
6730#define AICL_ADC_BIT BIT(6)
6731static void batt_ov_wa_check(struct smbchg_chip *chip)
6732{
6733 int rc;
6734 u8 reg;
6735
6736 /* disable-'battery OV disables charging' feature */
6737 rc = smbchg_sec_masked_write(chip, chip->chgr_base + CHGR_CFG2,
6738 CHG_BAT_OV_ECC, 0);
6739 if (rc < 0) {
6740 dev_err(chip->dev, "Couldn't set chgr_cfg2 rc=%d\n", rc);
6741 return;
6742 }
6743
6744 /*
6745 * if battery OV is set:
6746 * restart charging by disable/enable charging
6747 */
6748 rc = smbchg_read(chip, &reg, chip->bat_if_base + RT_STS, 1);
6749 if (rc < 0) {
6750 dev_err(chip->dev,
6751 "Couldn't read Battery RT status rc = %d\n", rc);
6752 return;
6753 }
6754
6755 if (reg & BAT_OV_BIT) {
6756 rc = smbchg_charging_en(chip, false);
6757 if (rc < 0) {
6758 dev_err(chip->dev,
6759 "Couldn't disable charging: rc = %d\n", rc);
6760 return;
6761 }
6762
6763 /* delay for charging-disable to take affect */
6764 msleep(200);
6765
6766 rc = smbchg_charging_en(chip, true);
6767 if (rc < 0) {
6768 dev_err(chip->dev,
6769 "Couldn't enable charging: rc = %d\n", rc);
6770 return;
6771 }
6772 }
6773}
6774
6775static int smbchg_hw_init(struct smbchg_chip *chip)
6776{
6777 int rc, i;
6778 u8 reg, mask;
6779
6780 rc = smbchg_read(chip, chip->revision,
6781 chip->misc_base + REVISION1_REG, 4);
6782 if (rc < 0) {
6783 dev_err(chip->dev, "Couldn't read revision rc=%d\n",
6784 rc);
6785 return rc;
6786 }
6787 pr_smb(PR_STATUS, "Charger Revision DIG: %d.%d; ANA: %d.%d\n",
6788 chip->revision[DIG_MAJOR], chip->revision[DIG_MINOR],
6789 chip->revision[ANA_MAJOR], chip->revision[ANA_MINOR]);
6790
6791 /* Setup 9V HVDCP */
6792 if (!chip->hvdcp_not_supported) {
6793 rc = smbchg_sec_masked_write(chip,
6794 chip->usb_chgpth_base + CHGPTH_CFG,
6795 HVDCP_ADAPTER_SEL_MASK, HVDCP_9V);
6796 if (rc < 0) {
6797 pr_err("Couldn't set hvdcp config in chgpath_chg rc=%d\n",
6798 rc);
6799 return rc;
6800 }
6801 }
6802
6803 if (chip->aicl_rerun_period_s > 0) {
6804 rc = smbchg_set_aicl_rerun_period_s(chip,
6805 chip->aicl_rerun_period_s);
6806 if (rc < 0) {
6807 dev_err(chip->dev, "Couldn't set AICL rerun timer rc=%d\n",
6808 rc);
6809 return rc;
6810 }
6811 }
6812
6813 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + TR_RID_REG,
6814 FG_INPUT_FET_DELAY_BIT, FG_INPUT_FET_DELAY_BIT);
6815 if (rc < 0) {
6816 dev_err(chip->dev, "Couldn't disable fg input fet delay rc=%d\n",
6817 rc);
6818 return rc;
6819 }
6820
6821 rc = smbchg_sec_masked_write(chip, chip->misc_base + TRIM_OPTIONS_7_0,
6822 INPUT_MISSING_POLLER_EN_BIT,
6823 INPUT_MISSING_POLLER_EN_BIT);
6824 if (rc < 0) {
6825 dev_err(chip->dev, "Couldn't enable input missing poller rc=%d\n",
6826 rc);
6827 return rc;
6828 }
6829
6830 /*
6831 * Do not force using current from the register i.e. use auto
6832 * power source detect (APSD) mA ratings for the initial current values.
6833 *
6834 * If this is set, AICL will not rerun at 9V for HVDCPs
6835 */
6836 rc = smbchg_masked_write(chip, chip->usb_chgpth_base + CMD_IL,
6837 USE_REGISTER_FOR_CURRENT, 0);
6838
6839 if (rc < 0) {
6840 dev_err(chip->dev, "Couldn't set input limit cmd rc=%d\n", rc);
6841 return rc;
6842 }
6843
6844 /*
6845 * set chg en by cmd register, set chg en by writing bit 1,
6846 * enable auto pre to fast, enable auto recharge by default.
6847 * enable current termination and charge inhibition based on
6848 * the device tree configuration.
6849 */
6850 rc = smbchg_sec_masked_write(chip, chip->chgr_base + CHGR_CFG2,
6851 CHG_EN_SRC_BIT | CHG_EN_POLARITY_BIT | P2F_CHG_TRAN
6852 | I_TERM_BIT | AUTO_RECHG_BIT | CHARGER_INHIBIT_BIT,
6853 CHG_EN_POLARITY_BIT
6854 | (chip->chg_inhibit_en ? CHARGER_INHIBIT_BIT : 0)
6855 | (chip->iterm_disabled ? I_TERM_BIT : 0));
6856 if (rc < 0) {
6857 dev_err(chip->dev, "Couldn't set chgr_cfg2 rc=%d\n", rc);
6858 return rc;
6859 }
6860
6861 /*
6862 * enable battery charging to make sure it hasn't been changed earlier
6863 * by the bootloader.
6864 */
6865 rc = smbchg_charging_en(chip, true);
6866 if (rc < 0) {
6867 dev_err(chip->dev, "Couldn't enable battery charging=%d\n", rc);
6868 return rc;
6869 }
6870
6871 /*
6872 * Based on the configuration, use the analog sensors or the fuelgauge
6873 * adc for recharge threshold source.
6874 */
6875
6876 if (chip->chg_inhibit_source_fg)
6877 rc = smbchg_sec_masked_write(chip, chip->chgr_base + CHGR_CFG1,
6878 TERM_I_SRC_BIT | RECHG_THRESHOLD_SRC_BIT,
6879 TERM_SRC_FG | RECHG_THRESHOLD_SRC_BIT);
6880 else
6881 rc = smbchg_sec_masked_write(chip, chip->chgr_base + CHGR_CFG1,
6882 TERM_I_SRC_BIT | RECHG_THRESHOLD_SRC_BIT, 0);
6883
6884 if (rc < 0) {
6885 dev_err(chip->dev, "Couldn't set chgr_cfg2 rc=%d\n", rc);
6886 return rc;
6887 }
6888
6889 /*
6890 * control USB suspend via command bits and set correct 100/500mA
6891 * polarity on the usb current
6892 */
6893 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + CHGPTH_CFG,
6894 USB51_COMMAND_POL | USB51AC_CTRL, 0);
6895 if (rc < 0) {
6896 dev_err(chip->dev, "Couldn't set usb_chgpth cfg rc=%d\n", rc);
6897 return rc;
6898 }
6899
6900 check_battery_type(chip);
6901
6902 /* set the float voltage */
6903 if (chip->vfloat_mv != -EINVAL) {
6904 rc = smbchg_float_voltage_set(chip, chip->vfloat_mv);
6905 if (rc < 0) {
6906 dev_err(chip->dev,
6907 "Couldn't set float voltage rc = %d\n", rc);
6908 return rc;
6909 }
6910 pr_smb(PR_STATUS, "set vfloat to %d\n", chip->vfloat_mv);
6911 }
6912
6913 /* set the fast charge current compensation */
6914 if (chip->fastchg_current_comp != -EINVAL) {
6915 rc = smbchg_fastchg_current_comp_set(chip,
6916 chip->fastchg_current_comp);
6917 if (rc < 0) {
6918 dev_err(chip->dev, "Couldn't set fastchg current comp rc = %d\n",
6919 rc);
6920 return rc;
6921 }
6922 pr_smb(PR_STATUS, "set fastchg current comp to %d\n",
6923 chip->fastchg_current_comp);
6924 }
6925
6926 /* set the float voltage compensation */
6927 if (chip->float_voltage_comp != -EINVAL) {
6928 rc = smbchg_float_voltage_comp_set(chip,
6929 chip->float_voltage_comp);
6930 if (rc < 0) {
6931 dev_err(chip->dev, "Couldn't set float voltage comp rc = %d\n",
6932 rc);
6933 return rc;
6934 }
6935 pr_smb(PR_STATUS, "set float voltage comp to %d\n",
6936 chip->float_voltage_comp);
6937 }
6938
6939 /* set iterm */
6940 if (chip->iterm_ma != -EINVAL) {
6941 if (chip->iterm_disabled) {
6942 dev_err(chip->dev, "Error: Both iterm_disabled and iterm_ma set\n");
6943 return -EINVAL;
Kiran Gunda1bc78922017-09-19 13:09:44 +05306944 }
Kiran Gundaee7cef82017-09-20 17:44:16 +05306945
6946 smbchg_iterm_set(chip, chip->iterm_ma);
Kiran Gunda1bc78922017-09-19 13:09:44 +05306947 }
6948
6949 /* set the safety time voltage */
6950 if (chip->safety_time != -EINVAL) {
6951 reg = (chip->safety_time > 0 ? 0 : SFT_TIMER_DISABLE_BIT) |
6952 (chip->prechg_safety_time > 0
6953 ? 0 : PRECHG_SFT_TIMER_DISABLE_BIT);
6954
6955 for (i = 0; i < ARRAY_SIZE(chg_time); i++) {
6956 if (chip->safety_time <= chg_time[i]) {
6957 reg |= i << SAFETY_TIME_MINUTES_SHIFT;
6958 break;
6959 }
6960 }
6961 for (i = 0; i < ARRAY_SIZE(prechg_time); i++) {
6962 if (chip->prechg_safety_time <= prechg_time[i]) {
6963 reg |= i;
6964 break;
6965 }
6966 }
6967
6968 rc = smbchg_sec_masked_write(chip,
6969 chip->chgr_base + SFT_CFG,
6970 SFT_EN_MASK | SFT_TO_MASK |
6971 (chip->prechg_safety_time > 0
6972 ? PRECHG_SFT_TO_MASK : 0), reg);
6973 if (rc < 0) {
6974 dev_err(chip->dev,
6975 "Couldn't set safety timer rc = %d\n",
6976 rc);
6977 return rc;
6978 }
6979 chip->safety_timer_en = true;
6980 } else {
6981 rc = smbchg_read(chip, &reg, chip->chgr_base + SFT_CFG, 1);
6982 if (rc < 0)
6983 dev_err(chip->dev, "Unable to read SFT_CFG rc = %d\n",
6984 rc);
6985 else if (!(reg & SFT_EN_MASK))
6986 chip->safety_timer_en = true;
6987 }
6988
6989 /* configure jeita temperature hard limit */
6990 if (chip->jeita_temp_hard_limit >= 0) {
6991 rc = smbchg_sec_masked_write(chip,
6992 chip->chgr_base + CHGR_CCMP_CFG,
6993 JEITA_TEMP_HARD_LIMIT_BIT,
6994 chip->jeita_temp_hard_limit
6995 ? 0 : JEITA_TEMP_HARD_LIMIT_BIT);
6996 if (rc < 0) {
6997 dev_err(chip->dev,
6998 "Couldn't set jeita temp hard limit rc = %d\n",
6999 rc);
7000 return rc;
7001 }
7002 }
7003
7004 /* make the buck switch faster to prevent some vbus oscillation */
7005 rc = smbchg_sec_masked_write(chip,
7006 chip->usb_chgpth_base + TR_8OR32B,
7007 BUCK_8_16_FREQ_BIT, 0);
7008 if (rc < 0) {
7009 dev_err(chip->dev, "Couldn't set buck frequency rc = %d\n", rc);
7010 return rc;
7011 }
7012
7013 /* battery missing detection */
7014 mask = BATT_MISSING_ALGO_BIT;
7015 reg = chip->bmd_algo_disabled ? 0 : BATT_MISSING_ALGO_BIT;
7016 if (chip->bmd_pin_src < BPD_TYPE_DEFAULT) {
7017 mask |= BMD_PIN_SRC_MASK;
7018 reg |= chip->bmd_pin_src << PIN_SRC_SHIFT;
7019 }
7020 rc = smbchg_sec_masked_write(chip,
7021 chip->bat_if_base + BM_CFG, mask, reg);
7022 if (rc < 0) {
7023 dev_err(chip->dev, "Couldn't set batt_missing config = %d\n",
7024 rc);
7025 return rc;
7026 }
7027
7028 if (chip->vchg_adc_channel != -EINVAL) {
7029 /* configure and enable VCHG */
7030 rc = smbchg_sec_masked_write(chip, chip->chgr_base + CHGR_CFG,
7031 VCHG_INPUT_CURRENT_BIT | VCHG_EN_BIT,
7032 VCHG_INPUT_CURRENT_BIT | VCHG_EN_BIT);
7033 if (rc < 0) {
7034 dev_err(chip->dev, "Couldn't set recharge rc = %d\n",
7035 rc);
7036 return rc;
7037 }
7038 }
7039
7040 smbchg_charging_status_change(chip);
7041
7042 vote(chip->usb_suspend_votable, USER_EN_VOTER, !chip->chg_enabled, 0);
7043 vote(chip->dc_suspend_votable, USER_EN_VOTER, !chip->chg_enabled, 0);
7044 /* resume threshold */
7045 if (chip->resume_delta_mv != -EINVAL) {
7046
7047 /*
7048 * Configure only if the recharge threshold source is not
7049 * fuel gauge ADC.
7050 */
7051 if (!chip->chg_inhibit_source_fg) {
7052 if (chip->resume_delta_mv < 100)
7053 reg = CHG_INHIBIT_50MV_VAL;
7054 else if (chip->resume_delta_mv < 200)
7055 reg = CHG_INHIBIT_100MV_VAL;
7056 else if (chip->resume_delta_mv < 300)
7057 reg = CHG_INHIBIT_200MV_VAL;
7058 else
7059 reg = CHG_INHIBIT_300MV_VAL;
7060
7061 rc = smbchg_sec_masked_write(chip,
7062 chip->chgr_base + CHG_INHIB_CFG_REG,
7063 CHG_INHIBIT_MASK, reg);
7064 if (rc < 0) {
7065 dev_err(chip->dev, "Couldn't set inhibit val rc = %d\n",
7066 rc);
7067 return rc;
7068 }
7069 }
7070
7071 rc = smbchg_sec_masked_write(chip,
7072 chip->chgr_base + CHGR_CFG,
7073 RCHG_LVL_BIT,
7074 (chip->resume_delta_mv
7075 < chip->tables.rchg_thr_mv)
7076 ? 0 : RCHG_LVL_BIT);
7077 if (rc < 0) {
7078 dev_err(chip->dev, "Couldn't set recharge rc = %d\n",
7079 rc);
7080 return rc;
7081 }
7082 }
7083
7084 /* DC path current settings */
7085 if (chip->dc_psy_type != -EINVAL) {
7086 rc = vote(chip->dc_icl_votable, PSY_ICL_VOTER, true,
7087 chip->dc_target_current_ma);
7088 if (rc < 0) {
7089 dev_err(chip->dev,
7090 "Couldn't vote for initial DC ICL rc=%d\n", rc);
7091 return rc;
7092 }
7093 }
7094
7095
7096 /*
7097 * on some devices the battery is powered via external sources which
7098 * could raise its voltage above the float voltage. smbchargers go
7099 * in to reverse boost in such a situation and the workaround is to
7100 * disable float voltage compensation (note that the battery will appear
7101 * hot/cold when powered via external source).
7102 */
7103 if (chip->soft_vfloat_comp_disabled) {
7104 rc = smbchg_sec_masked_write(chip, chip->chgr_base + CFG_AFVC,
7105 VFLOAT_COMP_ENABLE_MASK, 0);
7106 if (rc < 0) {
7107 dev_err(chip->dev, "Couldn't disable soft vfloat rc = %d\n",
7108 rc);
7109 return rc;
7110 }
7111 }
7112
7113 rc = vote(chip->fcc_votable, BATT_TYPE_FCC_VOTER, true,
7114 chip->cfg_fastchg_current_ma);
7115 if (rc < 0) {
7116 dev_err(chip->dev, "Couldn't vote fastchg ma rc = %d\n", rc);
7117 return rc;
7118 }
7119
7120 rc = smbchg_read(chip, &chip->original_usbin_allowance,
7121 chip->usb_chgpth_base + USBIN_CHGR_CFG, 1);
7122 if (rc < 0)
7123 dev_err(chip->dev, "Couldn't read usb allowance rc=%d\n", rc);
7124
7125 if (chip->wipower_dyn_icl_avail) {
7126 rc = smbchg_wipower_ilim_config(chip,
7127 &(chip->wipower_default.entries[0]));
7128 if (rc < 0) {
7129 dev_err(chip->dev, "Couldn't set default wipower ilim = %d\n",
7130 rc);
7131 return rc;
7132 }
7133 }
7134 /* unsuspend dc path, it could be suspended by the bootloader */
7135 rc = smbchg_dc_suspend(chip, 0);
7136 if (rc < 0) {
7137 dev_err(chip->dev, "Couldn't unsuspend dc path= %d\n", rc);
7138 return rc;
7139 }
7140
7141 if (chip->force_aicl_rerun) {
7142 /* vote to enable hw aicl */
7143 rc = vote(chip->hw_aicl_rerun_enable_indirect_votable,
7144 DEFAULT_CONFIG_HW_AICL_VOTER, true, 0);
7145 if (rc < 0) {
7146 pr_err("Couldn't vote enable hw aicl rerun rc=%d\n",
7147 rc);
7148 return rc;
7149 }
7150 }
7151
7152 if (chip->schg_version == QPNP_SCHG_LITE) {
7153 /* enable OTG hiccup mode */
7154 rc = smbchg_sec_masked_write(chip, chip->otg_base + OTG_CFG,
7155 HICCUP_ENABLED_BIT, HICCUP_ENABLED_BIT);
7156 if (rc < 0)
7157 dev_err(chip->dev, "Couldn't set OTG OC config rc = %d\n",
7158 rc);
7159 }
7160
7161 if (chip->otg_pinctrl) {
7162 /* configure OTG enable to pin control active low */
7163 rc = smbchg_sec_masked_write(chip, chip->otg_base + OTG_CFG,
7164 OTG_PIN_POLARITY_BIT | OTG_EN_CTRL_MASK,
7165 OTG_PIN_ACTIVE_LOW | OTG_PIN_CTRL_RID_DIS);
7166 if (rc < 0) {
7167 dev_err(chip->dev, "Couldn't set OTG EN config rc = %d\n",
7168 rc);
7169 return rc;
7170 }
7171 }
7172
7173 if (chip->wa_flags & SMBCHG_BATT_OV_WA)
7174 batt_ov_wa_check(chip);
7175
7176 /* turn off AICL adc for improved accuracy */
7177 rc = smbchg_sec_masked_write(chip,
7178 chip->misc_base + MISC_TRIM_OPT_15_8, AICL_ADC_BIT, 0);
7179 if (rc)
7180 pr_err("Couldn't write to MISC_TRIM_OPTIONS_15_8 rc=%d\n",
7181 rc);
7182
7183 return rc;
7184}
7185
Kiran Gundaee7cef82017-09-20 17:44:16 +05307186static const struct of_device_id smbchg_match_table[] = {
Kiran Gunda1bc78922017-09-19 13:09:44 +05307187 {
7188 .compatible = "qcom,qpnp-smbcharger",
7189 },
7190 { },
7191};
7192
7193#define DC_MA_MIN 300
7194#define DC_MA_MAX 2000
7195#define OF_PROP_READ(chip, prop, dt_property, retval, optional) \
7196do { \
7197 if (retval) \
7198 break; \
7199 if (optional) \
7200 prop = -EINVAL; \
7201 \
7202 retval = of_property_read_u32(chip->pdev->dev.of_node, \
Kiran Gundaee7cef82017-09-20 17:44:16 +05307203 "qcom," dt_property, \
Kiran Gunda1bc78922017-09-19 13:09:44 +05307204 &prop); \
7205 \
7206 if ((retval == -EINVAL) && optional) \
7207 retval = 0; \
7208 else if (retval) \
7209 dev_err(chip->dev, "Error reading " #dt_property \
7210 " property rc = %d\n", rc); \
7211} while (0)
7212
7213#define ILIM_ENTRIES 3
7214#define VOLTAGE_RANGE_ENTRIES 2
7215#define RANGE_ENTRY (ILIM_ENTRIES + VOLTAGE_RANGE_ENTRIES)
7216static int smb_parse_wipower_map_dt(struct smbchg_chip *chip,
7217 struct ilim_map *map, char *property)
7218{
7219 struct device_node *node = chip->dev->of_node;
7220 int total_elements, size;
7221 struct property *prop;
7222 const __be32 *data;
7223 int num, i;
7224
7225 prop = of_find_property(node, property, &size);
7226 if (!prop) {
7227 dev_err(chip->dev, "%s missing\n", property);
7228 return -EINVAL;
7229 }
7230
7231 total_elements = size / sizeof(int);
7232 if (total_elements % RANGE_ENTRY) {
7233 dev_err(chip->dev, "%s table not in multiple of %d, total elements = %d\n",
7234 property, RANGE_ENTRY, total_elements);
7235 return -EINVAL;
7236 }
7237
7238 data = prop->value;
7239 num = total_elements / RANGE_ENTRY;
7240 map->entries = devm_kzalloc(chip->dev,
7241 num * sizeof(struct ilim_entry), GFP_KERNEL);
Kiran Gundaee7cef82017-09-20 17:44:16 +05307242 if (!map->entries)
Kiran Gunda1bc78922017-09-19 13:09:44 +05307243 return -ENOMEM;
Kiran Gundaee7cef82017-09-20 17:44:16 +05307244
Kiran Gunda1bc78922017-09-19 13:09:44 +05307245 for (i = 0; i < num; i++) {
7246 map->entries[i].vmin_uv = be32_to_cpup(data++);
7247 map->entries[i].vmax_uv = be32_to_cpup(data++);
7248 map->entries[i].icl_pt_ma = be32_to_cpup(data++);
7249 map->entries[i].icl_lv_ma = be32_to_cpup(data++);
7250 map->entries[i].icl_hv_ma = be32_to_cpup(data++);
7251 }
7252 map->num = num;
7253 return 0;
7254}
7255
7256static int smb_parse_wipower_dt(struct smbchg_chip *chip)
7257{
7258 int rc = 0;
7259
7260 chip->wipower_dyn_icl_avail = false;
7261
7262 if (!chip->vadc_dev)
7263 goto err;
7264
7265 rc = smb_parse_wipower_map_dt(chip, &chip->wipower_default,
7266 "qcom,wipower-default-ilim-map");
7267 if (rc) {
7268 dev_err(chip->dev, "failed to parse wipower-pt-ilim-map rc = %d\n",
7269 rc);
7270 goto err;
7271 }
7272
7273 rc = smb_parse_wipower_map_dt(chip, &chip->wipower_pt,
7274 "qcom,wipower-pt-ilim-map");
7275 if (rc) {
7276 dev_err(chip->dev, "failed to parse wipower-pt-ilim-map rc = %d\n",
7277 rc);
7278 goto err;
7279 }
7280
7281 rc = smb_parse_wipower_map_dt(chip, &chip->wipower_div2,
7282 "qcom,wipower-div2-ilim-map");
7283 if (rc) {
7284 dev_err(chip->dev, "failed to parse wipower-div2-ilim-map rc = %d\n",
7285 rc);
7286 goto err;
7287 }
7288 chip->wipower_dyn_icl_avail = true;
7289 return 0;
7290err:
7291 chip->wipower_default.num = 0;
7292 chip->wipower_pt.num = 0;
7293 chip->wipower_default.num = 0;
7294 if (chip->wipower_default.entries)
7295 devm_kfree(chip->dev, chip->wipower_default.entries);
7296 if (chip->wipower_pt.entries)
7297 devm_kfree(chip->dev, chip->wipower_pt.entries);
7298 if (chip->wipower_div2.entries)
7299 devm_kfree(chip->dev, chip->wipower_div2.entries);
7300 chip->wipower_default.entries = NULL;
7301 chip->wipower_pt.entries = NULL;
7302 chip->wipower_div2.entries = NULL;
7303 chip->vadc_dev = NULL;
7304 return rc;
7305}
7306
7307#define DEFAULT_VLED_MAX_UV 3500000
7308#define DEFAULT_FCC_MA 2000
7309static int smb_parse_dt(struct smbchg_chip *chip)
7310{
7311 int rc = 0, ocp_thresh = -EINVAL;
7312 struct device_node *node = chip->dev->of_node;
7313 const char *dc_psy_type, *bpd;
7314
7315 if (!node) {
7316 dev_err(chip->dev, "device tree info. missing\n");
7317 return -EINVAL;
7318 }
7319
7320 /* read optional u32 properties */
7321 OF_PROP_READ(chip, ocp_thresh,
7322 "ibat-ocp-threshold-ua", rc, 1);
7323 if (ocp_thresh >= 0)
7324 smbchg_ibat_ocp_threshold_ua = ocp_thresh;
7325 OF_PROP_READ(chip, chip->iterm_ma, "iterm-ma", rc, 1);
7326 OF_PROP_READ(chip, chip->cfg_fastchg_current_ma,
7327 "fastchg-current-ma", rc, 1);
7328 if (chip->cfg_fastchg_current_ma == -EINVAL)
7329 chip->cfg_fastchg_current_ma = DEFAULT_FCC_MA;
7330 OF_PROP_READ(chip, chip->vfloat_mv, "float-voltage-mv", rc, 1);
7331 OF_PROP_READ(chip, chip->safety_time, "charging-timeout-mins", rc, 1);
7332 OF_PROP_READ(chip, chip->vled_max_uv, "vled-max-uv", rc, 1);
7333 if (chip->vled_max_uv < 0)
7334 chip->vled_max_uv = DEFAULT_VLED_MAX_UV;
7335 OF_PROP_READ(chip, chip->rpara_uohm, "rparasitic-uohm", rc, 1);
7336 if (chip->rpara_uohm < 0)
7337 chip->rpara_uohm = 0;
7338 OF_PROP_READ(chip, chip->prechg_safety_time, "precharging-timeout-mins",
7339 rc, 1);
7340 OF_PROP_READ(chip, chip->fastchg_current_comp, "fastchg-current-comp",
7341 rc, 1);
7342 OF_PROP_READ(chip, chip->float_voltage_comp, "float-voltage-comp",
7343 rc, 1);
7344 if (chip->safety_time != -EINVAL &&
7345 (chip->safety_time > chg_time[ARRAY_SIZE(chg_time) - 1])) {
7346 dev_err(chip->dev, "Bad charging-timeout-mins %d\n",
7347 chip->safety_time);
7348 return -EINVAL;
7349 }
7350 if (chip->prechg_safety_time != -EINVAL &&
7351 (chip->prechg_safety_time >
7352 prechg_time[ARRAY_SIZE(prechg_time) - 1])) {
7353 dev_err(chip->dev, "Bad precharging-timeout-mins %d\n",
7354 chip->prechg_safety_time);
7355 return -EINVAL;
7356 }
7357 OF_PROP_READ(chip, chip->resume_delta_mv, "resume-delta-mv", rc, 1);
7358 OF_PROP_READ(chip, chip->parallel.min_current_thr_ma,
7359 "parallel-usb-min-current-ma", rc, 1);
7360 OF_PROP_READ(chip, chip->parallel.min_9v_current_thr_ma,
7361 "parallel-usb-9v-min-current-ma", rc, 1);
7362 OF_PROP_READ(chip, chip->parallel.allowed_lowering_ma,
7363 "parallel-allowed-lowering-ma", rc, 1);
7364 if (chip->parallel.min_current_thr_ma != -EINVAL
7365 && chip->parallel.min_9v_current_thr_ma != -EINVAL)
7366 chip->parallel.avail = true;
7367 /*
7368 * use the dt values if they exist, otherwise do not touch the params
7369 */
7370 of_property_read_u32(node, "qcom,parallel-main-chg-fcc-percent",
7371 &smbchg_main_chg_fcc_percent);
7372 of_property_read_u32(node, "qcom,parallel-main-chg-icl-percent",
7373 &smbchg_main_chg_icl_percent);
7374 pr_smb(PR_STATUS, "parallel usb thr: %d, 9v thr: %d\n",
7375 chip->parallel.min_current_thr_ma,
7376 chip->parallel.min_9v_current_thr_ma);
7377 OF_PROP_READ(chip, chip->jeita_temp_hard_limit,
7378 "jeita-temp-hard-limit", rc, 1);
7379 OF_PROP_READ(chip, chip->aicl_rerun_period_s,
7380 "aicl-rerun-period-s", rc, 1);
7381 OF_PROP_READ(chip, chip->vchg_adc_channel,
7382 "vchg-adc-channel-id", rc, 1);
7383
7384 /* read boolean configuration properties */
7385 chip->use_vfloat_adjustments = of_property_read_bool(node,
7386 "qcom,autoadjust-vfloat");
7387 chip->bmd_algo_disabled = of_property_read_bool(node,
7388 "qcom,bmd-algo-disabled");
7389 chip->iterm_disabled = of_property_read_bool(node,
7390 "qcom,iterm-disabled");
7391 chip->soft_vfloat_comp_disabled = of_property_read_bool(node,
7392 "qcom,soft-vfloat-comp-disabled");
7393 chip->chg_enabled = !(of_property_read_bool(node,
7394 "qcom,charging-disabled"));
7395 chip->charge_unknown_battery = of_property_read_bool(node,
7396 "qcom,charge-unknown-battery");
7397 chip->chg_inhibit_en = of_property_read_bool(node,
7398 "qcom,chg-inhibit-en");
7399 chip->chg_inhibit_source_fg = of_property_read_bool(node,
7400 "qcom,chg-inhibit-fg");
7401 chip->low_volt_dcin = of_property_read_bool(node,
7402 "qcom,low-volt-dcin");
7403 chip->force_aicl_rerun = of_property_read_bool(node,
7404 "qcom,force-aicl-rerun");
7405 chip->skip_usb_suspend_for_fake_battery = of_property_read_bool(node,
7406 "qcom,skip-usb-suspend-for-fake-battery");
7407
7408 /* parse the battery missing detection pin source */
7409 rc = of_property_read_string(chip->pdev->dev.of_node,
7410 "qcom,bmd-pin-src", &bpd);
7411 if (rc) {
7412 /* Select BAT_THM as default BPD scheme */
7413 chip->bmd_pin_src = BPD_TYPE_DEFAULT;
7414 rc = 0;
7415 } else {
7416 chip->bmd_pin_src = get_bpd(bpd);
7417 if (chip->bmd_pin_src < 0) {
7418 dev_err(chip->dev,
7419 "failed to determine bpd schema %d\n", rc);
7420 return rc;
7421 }
7422 }
7423
7424 /* parse the dc power supply configuration */
7425 rc = of_property_read_string(node, "qcom,dc-psy-type", &dc_psy_type);
7426 if (rc) {
7427 chip->dc_psy_type = -EINVAL;
7428 rc = 0;
7429 } else {
7430 if (strcmp(dc_psy_type, "Mains") == 0)
7431 chip->dc_psy_type = POWER_SUPPLY_TYPE_MAINS;
7432 else if (strcmp(dc_psy_type, "Wireless") == 0)
7433 chip->dc_psy_type = POWER_SUPPLY_TYPE_WIRELESS;
7434 else if (strcmp(dc_psy_type, "Wipower") == 0)
7435 chip->dc_psy_type = POWER_SUPPLY_TYPE_WIPOWER;
7436 }
7437 if (chip->dc_psy_type != -EINVAL) {
7438 OF_PROP_READ(chip, chip->dc_target_current_ma,
7439 "dc-psy-ma", rc, 0);
7440 if (rc)
7441 return rc;
7442 if (chip->dc_target_current_ma < DC_MA_MIN
7443 || chip->dc_target_current_ma > DC_MA_MAX) {
7444 dev_err(chip->dev, "Bad dc mA %d\n",
7445 chip->dc_target_current_ma);
7446 return -EINVAL;
7447 }
7448 }
7449
7450 if (chip->dc_psy_type == POWER_SUPPLY_TYPE_WIPOWER)
7451 smb_parse_wipower_dt(chip);
7452
7453 /* read the bms power supply name */
7454 rc = of_property_read_string(node, "qcom,bms-psy-name",
7455 &chip->bms_psy_name);
7456 if (rc)
7457 chip->bms_psy_name = NULL;
7458
7459 /* read the battery power supply name */
7460 rc = of_property_read_string(node, "qcom,battery-psy-name",
7461 &chip->battery_psy_name);
7462 if (rc)
7463 chip->battery_psy_name = "battery";
7464
7465 /* Get the charger led support property */
7466 chip->cfg_chg_led_sw_ctrl =
7467 of_property_read_bool(node, "qcom,chg-led-sw-controls");
7468 chip->cfg_chg_led_support =
7469 of_property_read_bool(node, "qcom,chg-led-support");
7470
7471 if (of_find_property(node, "qcom,thermal-mitigation",
7472 &chip->thermal_levels)) {
7473 chip->thermal_mitigation = devm_kzalloc(chip->dev,
7474 chip->thermal_levels,
7475 GFP_KERNEL);
7476
7477 if (chip->thermal_mitigation == NULL) {
7478 dev_err(chip->dev, "thermal mitigation kzalloc() failed.\n");
7479 return -ENOMEM;
7480 }
7481
7482 chip->thermal_levels /= sizeof(int);
7483 rc = of_property_read_u32_array(node,
7484 "qcom,thermal-mitigation",
7485 chip->thermal_mitigation, chip->thermal_levels);
7486 if (rc) {
7487 dev_err(chip->dev,
7488 "Couldn't read threm limits rc = %d\n", rc);
7489 return rc;
7490 }
7491 }
7492
7493 chip->skip_usb_notification
7494 = of_property_read_bool(node,
7495 "qcom,skip-usb-notification");
7496
7497 chip->otg_pinctrl = of_property_read_bool(node, "qcom,otg-pinctrl");
7498
7499 return 0;
7500}
7501
7502#define SUBTYPE_REG 0x5
7503#define SMBCHG_CHGR_SUBTYPE 0x1
7504#define SMBCHG_OTG_SUBTYPE 0x8
7505#define SMBCHG_BAT_IF_SUBTYPE 0x3
7506#define SMBCHG_USB_CHGPTH_SUBTYPE 0x4
7507#define SMBCHG_DC_CHGPTH_SUBTYPE 0x5
7508#define SMBCHG_MISC_SUBTYPE 0x7
7509#define SMBCHG_LITE_CHGR_SUBTYPE 0x51
7510#define SMBCHG_LITE_OTG_SUBTYPE 0x58
7511#define SMBCHG_LITE_BAT_IF_SUBTYPE 0x53
7512#define SMBCHG_LITE_USB_CHGPTH_SUBTYPE 0x54
7513#define SMBCHG_LITE_DC_CHGPTH_SUBTYPE 0x55
7514#define SMBCHG_LITE_MISC_SUBTYPE 0x57
7515static int smbchg_request_irq(struct smbchg_chip *chip,
7516 struct device_node *child,
7517 int irq_num, char *irq_name,
7518 irqreturn_t (irq_handler)(int irq, void *_chip),
7519 int flags)
7520{
7521 int rc;
7522
7523 irq_num = of_irq_get_byname(child, irq_name);
7524 if (irq_num < 0) {
7525 dev_err(chip->dev, "Unable to get %s irqn", irq_name);
7526 rc = -ENXIO;
7527 }
7528 rc = devm_request_threaded_irq(chip->dev,
7529 irq_num, NULL, irq_handler, flags, irq_name,
7530 chip);
7531 if (rc < 0) {
7532 dev_err(chip->dev, "Unable to request %s irq: %dn",
7533 irq_name, rc);
7534 rc = -ENXIO;
7535 }
7536 return 0;
7537}
7538
7539static int smbchg_request_irqs(struct smbchg_chip *chip)
7540{
7541 int rc = 0;
7542 unsigned int base;
7543 struct device_node *child;
7544 u8 subtype;
7545 unsigned long flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
7546 | IRQF_ONESHOT;
7547
7548 if (of_get_available_child_count(chip->pdev->dev.of_node) == 0) {
7549 pr_err("no child nodes\n");
7550 return -ENXIO;
7551 }
7552
7553 for_each_available_child_of_node(chip->pdev->dev.of_node, child) {
7554 rc = of_property_read_u32(child, "reg", &base);
7555 if (rc < 0) {
7556 rc = 0;
7557 continue;
7558 }
7559
7560 rc = smbchg_read(chip, &subtype, base + SUBTYPE_REG, 1);
7561 if (rc) {
7562 dev_err(chip->dev, "Peripheral subtype read failed rc=%d\n",
7563 rc);
7564 return rc;
7565 }
7566
7567 switch (subtype) {
7568 case SMBCHG_CHGR_SUBTYPE:
7569 case SMBCHG_LITE_CHGR_SUBTYPE:
7570 rc = smbchg_request_irq(chip, child,
7571 chip->chg_error_irq, "chg-error",
7572 chg_error_handler, flags);
7573 if (rc < 0)
7574 return rc;
7575 rc = smbchg_request_irq(chip, child, chip->taper_irq,
7576 "chg-taper-thr", taper_handler,
7577 (IRQF_TRIGGER_RISING | IRQF_ONESHOT));
7578 if (rc < 0)
7579 return rc;
7580 disable_irq_nosync(chip->taper_irq);
7581 rc = smbchg_request_irq(chip, child, chip->chg_term_irq,
7582 "chg-tcc-thr", chg_term_handler,
7583 (IRQF_TRIGGER_RISING | IRQF_ONESHOT));
7584 if (rc < 0)
7585 return rc;
7586 rc = smbchg_request_irq(chip, child, chip->recharge_irq,
7587 "chg-rechg-thr", recharge_handler, flags);
7588 if (rc < 0)
7589 return rc;
7590 rc = smbchg_request_irq(chip, child, chip->fastchg_irq,
7591 "chg-p2f-thr", fastchg_handler, flags);
7592 if (rc < 0)
7593 return rc;
7594 enable_irq_wake(chip->chg_term_irq);
7595 enable_irq_wake(chip->chg_error_irq);
7596 enable_irq_wake(chip->fastchg_irq);
7597 break;
7598 case SMBCHG_BAT_IF_SUBTYPE:
7599 case SMBCHG_LITE_BAT_IF_SUBTYPE:
7600 rc = smbchg_request_irq(chip, child, chip->batt_hot_irq,
7601 "batt-hot", batt_hot_handler, flags);
7602 if (rc < 0)
7603 return rc;
7604 rc = smbchg_request_irq(chip, child,
7605 chip->batt_warm_irq,
7606 "batt-warm", batt_warm_handler, flags);
7607 if (rc < 0)
7608 return rc;
7609 rc = smbchg_request_irq(chip, child,
7610 chip->batt_cool_irq,
7611 "batt-cool", batt_cool_handler, flags);
7612 if (rc < 0)
7613 return rc;
7614 rc = smbchg_request_irq(chip, child,
7615 chip->batt_cold_irq,
7616 "batt-cold", batt_cold_handler, flags);
7617 if (rc < 0)
7618 return rc;
7619 rc = smbchg_request_irq(chip, child,
7620 chip->batt_missing_irq,
7621 "batt-missing", batt_pres_handler, flags);
7622 if (rc < 0)
7623 return rc;
7624 rc = smbchg_request_irq(chip, child,
7625 chip->vbat_low_irq,
7626 "batt-low", vbat_low_handler, flags);
7627 if (rc < 0)
7628 return rc;
Kiran Gundaee7cef82017-09-20 17:44:16 +05307629
Kiran Gunda1bc78922017-09-19 13:09:44 +05307630 enable_irq_wake(chip->batt_hot_irq);
7631 enable_irq_wake(chip->batt_warm_irq);
7632 enable_irq_wake(chip->batt_cool_irq);
7633 enable_irq_wake(chip->batt_cold_irq);
7634 enable_irq_wake(chip->batt_missing_irq);
7635 enable_irq_wake(chip->vbat_low_irq);
7636 break;
7637 case SMBCHG_USB_CHGPTH_SUBTYPE:
7638 case SMBCHG_LITE_USB_CHGPTH_SUBTYPE:
7639 rc = smbchg_request_irq(chip, child,
7640 chip->usbin_uv_irq,
7641 "usbin-uv", usbin_uv_handler,
7642 flags | IRQF_EARLY_RESUME);
7643 if (rc < 0)
7644 return rc;
7645 rc = smbchg_request_irq(chip, child,
7646 chip->usbin_ov_irq,
7647 "usbin-ov", usbin_ov_handler, flags);
7648 if (rc < 0)
7649 return rc;
7650 rc = smbchg_request_irq(chip, child,
7651 chip->src_detect_irq,
7652 "usbin-src-det",
7653 src_detect_handler, flags);
7654 if (rc < 0)
7655 return rc;
7656 rc = smbchg_request_irq(chip, child,
7657 chip->aicl_done_irq,
7658 "aicl-done",
7659 aicl_done_handler,
7660 (IRQF_TRIGGER_RISING | IRQF_ONESHOT));
7661 if (rc < 0)
7662 return rc;
7663
7664 if (chip->schg_version != QPNP_SCHG_LITE) {
7665 rc = smbchg_request_irq(chip, child,
7666 chip->otg_fail_irq, "otg-fail",
7667 otg_fail_handler, flags);
7668 if (rc < 0)
7669 return rc;
7670 rc = smbchg_request_irq(chip, child,
7671 chip->otg_oc_irq, "otg-oc",
7672 otg_oc_handler,
7673 (IRQF_TRIGGER_RISING | IRQF_ONESHOT));
7674 if (rc < 0)
7675 return rc;
7676 rc = smbchg_request_irq(chip, child,
7677 chip->usbid_change_irq, "usbid-change",
7678 usbid_change_handler,
7679 (IRQF_TRIGGER_FALLING | IRQF_ONESHOT));
7680 if (rc < 0)
7681 return rc;
7682 enable_irq_wake(chip->otg_oc_irq);
7683 enable_irq_wake(chip->usbid_change_irq);
7684 enable_irq_wake(chip->otg_fail_irq);
7685 }
7686 enable_irq_wake(chip->usbin_uv_irq);
7687 enable_irq_wake(chip->usbin_ov_irq);
7688 enable_irq_wake(chip->src_detect_irq);
7689 if (chip->parallel.avail && chip->usb_present) {
7690 rc = enable_irq_wake(chip->aicl_done_irq);
7691 chip->enable_aicl_wake = true;
7692 }
7693 break;
7694 case SMBCHG_DC_CHGPTH_SUBTYPE:
7695 case SMBCHG_LITE_DC_CHGPTH_SUBTYPE:
7696 rc = smbchg_request_irq(chip, child, chip->dcin_uv_irq,
7697 "dcin-uv", dcin_uv_handler, flags);
7698 if (rc < 0)
7699 return rc;
7700 enable_irq_wake(chip->dcin_uv_irq);
7701 break;
7702 case SMBCHG_MISC_SUBTYPE:
7703 case SMBCHG_LITE_MISC_SUBTYPE:
7704 rc = smbchg_request_irq(chip, child, chip->power_ok_irq,
7705 "power-ok", power_ok_handler, flags);
7706 if (rc < 0)
7707 return rc;
7708 rc = smbchg_request_irq(chip, child, chip->chg_hot_irq,
7709 "temp-shutdown", chg_hot_handler, flags);
7710 if (rc < 0)
7711 return rc;
Kiran Gundaee7cef82017-09-20 17:44:16 +05307712 rc = smbchg_request_irq(chip, child,
7713 chip->wdog_timeout_irq, "wdog-timeout",
Kiran Gunda1bc78922017-09-19 13:09:44 +05307714 wdog_timeout_handler, flags);
7715 if (rc < 0)
7716 return rc;
7717 enable_irq_wake(chip->chg_hot_irq);
7718 enable_irq_wake(chip->wdog_timeout_irq);
7719 break;
7720 case SMBCHG_OTG_SUBTYPE:
7721 break;
7722 case SMBCHG_LITE_OTG_SUBTYPE:
7723 rc = smbchg_request_irq(chip, child,
7724 chip->usbid_change_irq, "usbid-change",
7725 usbid_change_handler,
7726 (IRQF_TRIGGER_FALLING | IRQF_ONESHOT));
7727 if (rc < 0)
7728 return rc;
7729 rc = smbchg_request_irq(chip, child,
7730 chip->otg_oc_irq, "otg-oc",
7731 otg_oc_handler,
7732 (IRQF_TRIGGER_RISING | IRQF_ONESHOT));
7733 if (rc < 0)
7734 return rc;
7735 rc = smbchg_request_irq(chip, child,
7736 chip->otg_fail_irq, "otg-fail",
7737 otg_fail_handler, flags);
7738 if (rc < 0)
7739 return rc;
7740 enable_irq_wake(chip->usbid_change_irq);
7741 enable_irq_wake(chip->otg_oc_irq);
7742 enable_irq_wake(chip->otg_fail_irq);
7743 break;
7744 }
7745 }
7746
7747 return rc;
7748}
7749
7750#define REQUIRE_BASE(chip, base, rc) \
7751do { \
7752 if (!rc && !chip->base) { \
7753 dev_err(chip->dev, "Missing " #base "\n"); \
7754 rc = -EINVAL; \
7755 } \
7756} while (0)
7757
7758static int smbchg_parse_peripherals(struct smbchg_chip *chip)
7759{
7760 int rc = 0;
7761 unsigned int base;
7762 struct device_node *child;
7763 u8 subtype;
7764
7765 if (of_get_available_child_count(chip->pdev->dev.of_node) == 0) {
7766 pr_err("no child nodes\n");
7767 return -ENXIO;
7768 }
7769
7770 for_each_available_child_of_node(chip->pdev->dev.of_node, child) {
7771 rc = of_property_read_u32(child, "reg", &base);
7772 if (rc < 0) {
7773 rc = 0;
7774 continue;
7775 }
7776
7777 rc = smbchg_read(chip, &subtype, base + SUBTYPE_REG, 1);
7778 if (rc) {
7779 dev_err(chip->dev, "Peripheral subtype read failed rc=%d\n",
7780 rc);
7781 return rc;
7782 }
7783
7784 switch (subtype) {
7785 case SMBCHG_CHGR_SUBTYPE:
7786 case SMBCHG_LITE_CHGR_SUBTYPE:
7787 chip->chgr_base = base;
7788 break;
7789 case SMBCHG_BAT_IF_SUBTYPE:
7790 case SMBCHG_LITE_BAT_IF_SUBTYPE:
7791 chip->bat_if_base = base;
7792 break;
7793 case SMBCHG_USB_CHGPTH_SUBTYPE:
7794 case SMBCHG_LITE_USB_CHGPTH_SUBTYPE:
7795 chip->usb_chgpth_base = base;
7796 break;
7797 case SMBCHG_DC_CHGPTH_SUBTYPE:
7798 case SMBCHG_LITE_DC_CHGPTH_SUBTYPE:
7799 chip->dc_chgpth_base = base;
7800 break;
7801 case SMBCHG_MISC_SUBTYPE:
7802 case SMBCHG_LITE_MISC_SUBTYPE:
7803 chip->misc_base = base;
7804 break;
7805 case SMBCHG_OTG_SUBTYPE:
7806 case SMBCHG_LITE_OTG_SUBTYPE:
7807 chip->otg_base = base;
7808 break;
7809 }
7810 }
7811
7812 REQUIRE_BASE(chip, chgr_base, rc);
7813 REQUIRE_BASE(chip, bat_if_base, rc);
7814 REQUIRE_BASE(chip, usb_chgpth_base, rc);
7815 REQUIRE_BASE(chip, dc_chgpth_base, rc);
7816 REQUIRE_BASE(chip, misc_base, rc);
7817
7818 return rc;
7819}
7820
7821static inline void dump_reg(struct smbchg_chip *chip, u16 addr,
7822 const char *name)
7823{
7824 u8 reg;
7825
7826 smbchg_read(chip, &reg, addr, 1);
7827 pr_smb(PR_DUMP, "%s - %04X = %02X\n", name, addr, reg);
7828}
7829
7830/* dumps useful registers for debug */
7831static void dump_regs(struct smbchg_chip *chip)
7832{
7833 u16 addr;
7834
7835 /* charger peripheral */
7836 for (addr = 0xB; addr <= 0x10; addr++)
7837 dump_reg(chip, chip->chgr_base + addr, "CHGR Status");
7838 for (addr = 0xF0; addr <= 0xFF; addr++)
7839 dump_reg(chip, chip->chgr_base + addr, "CHGR Config");
7840 /* battery interface peripheral */
7841 dump_reg(chip, chip->bat_if_base + RT_STS, "BAT_IF Status");
7842 dump_reg(chip, chip->bat_if_base + CMD_CHG_REG, "BAT_IF Command");
7843 for (addr = 0xF0; addr <= 0xFB; addr++)
7844 dump_reg(chip, chip->bat_if_base + addr, "BAT_IF Config");
7845 /* usb charge path peripheral */
7846 for (addr = 0x7; addr <= 0x10; addr++)
7847 dump_reg(chip, chip->usb_chgpth_base + addr, "USB Status");
7848 dump_reg(chip, chip->usb_chgpth_base + CMD_IL, "USB Command");
7849 for (addr = 0xF0; addr <= 0xF5; addr++)
7850 dump_reg(chip, chip->usb_chgpth_base + addr, "USB Config");
7851 /* dc charge path peripheral */
7852 dump_reg(chip, chip->dc_chgpth_base + RT_STS, "DC Status");
7853 for (addr = 0xF0; addr <= 0xF6; addr++)
7854 dump_reg(chip, chip->dc_chgpth_base + addr, "DC Config");
7855 /* misc peripheral */
7856 dump_reg(chip, chip->misc_base + IDEV_STS, "MISC Status");
7857 dump_reg(chip, chip->misc_base + RT_STS, "MISC Status");
7858 for (addr = 0xF0; addr <= 0xF3; addr++)
7859 dump_reg(chip, chip->misc_base + addr, "MISC CFG");
7860}
7861
7862static int create_debugfs_entries(struct smbchg_chip *chip)
7863{
7864 struct dentry *ent;
7865
7866 chip->debug_root = debugfs_create_dir("qpnp-smbcharger", NULL);
7867 if (!chip->debug_root) {
7868 dev_err(chip->dev, "Couldn't create debug dir\n");
7869 return -EINVAL;
7870 }
7871
7872 ent = debugfs_create_file("force_dcin_icl_check",
Kiran Gundaee7cef82017-09-20 17:44:16 +05307873 00100644, chip->debug_root, chip,
Kiran Gunda1bc78922017-09-19 13:09:44 +05307874 &force_dcin_icl_ops);
7875 if (!ent) {
7876 dev_err(chip->dev,
7877 "Couldn't create force dcin icl check file\n");
7878 return -EINVAL;
7879 }
7880 return 0;
7881}
7882
7883static int smbchg_check_chg_version(struct smbchg_chip *chip)
7884{
7885 struct pmic_revid_data *pmic_rev_id;
7886 struct device_node *revid_dev_node;
7887 int rc;
7888
7889 revid_dev_node = of_parse_phandle(chip->pdev->dev.of_node,
7890 "qcom,pmic-revid", 0);
7891 if (!revid_dev_node) {
7892 pr_err("Missing qcom,pmic-revid property - driver failed\n");
7893 return -EINVAL;
7894 }
7895
7896 pmic_rev_id = get_revid_data(revid_dev_node);
7897 if (IS_ERR(pmic_rev_id)) {
7898 rc = PTR_ERR(revid_dev_node);
7899 if (rc != -EPROBE_DEFER)
7900 pr_err("Unable to get pmic_revid rc=%d\n", rc);
7901 return rc;
7902 }
7903
7904 switch (pmic_rev_id->pmic_subtype) {
7905 case PMI8994:
7906 chip->wa_flags |= SMBCHG_AICL_DEGLITCH_WA
7907 | SMBCHG_BATT_OV_WA
7908 | SMBCHG_CC_ESR_WA
7909 | SMBCHG_RESTART_WA;
7910 use_pmi8994_tables(chip);
7911 chip->schg_version = QPNP_SCHG;
7912 break;
7913 case PMI8950:
7914 case PMI8937:
7915 chip->wa_flags |= SMBCHG_BATT_OV_WA;
7916 if (pmic_rev_id->rev4 < 2) /* PMI8950 1.0 */ {
7917 chip->wa_flags |= SMBCHG_AICL_DEGLITCH_WA;
7918 } else { /* rev > PMI8950 v1.0 */
7919 chip->wa_flags |= SMBCHG_HVDCP_9V_EN_WA
7920 | SMBCHG_USB100_WA;
7921 }
7922 use_pmi8994_tables(chip);
7923 chip->tables.aicl_rerun_period_table =
7924 aicl_rerun_period_schg_lite;
7925 chip->tables.aicl_rerun_period_len =
7926 ARRAY_SIZE(aicl_rerun_period_schg_lite);
7927
7928 chip->schg_version = QPNP_SCHG_LITE;
7929 if (pmic_rev_id->pmic_subtype == PMI8937)
7930 chip->hvdcp_not_supported = true;
7931 break;
7932 case PMI8996:
7933 chip->wa_flags |= SMBCHG_CC_ESR_WA
7934 | SMBCHG_FLASH_ICL_DISABLE_WA
7935 | SMBCHG_RESTART_WA
7936 | SMBCHG_FLASH_BUCK_SWITCH_FREQ_WA;
7937 use_pmi8996_tables(chip);
7938 chip->schg_version = QPNP_SCHG;
7939 break;
7940 default:
7941 pr_err("PMIC subtype %d not supported, WA flags not set\n",
7942 pmic_rev_id->pmic_subtype);
7943 }
7944
7945 pr_smb(PR_STATUS, "pmic=%s, wa_flags=0x%x, hvdcp_supported=%s\n",
7946 pmic_rev_id->pmic_name, chip->wa_flags,
7947 chip->hvdcp_not_supported ? "false" : "true");
7948
7949 return 0;
7950}
7951
7952static void rerun_hvdcp_det_if_necessary(struct smbchg_chip *chip)
7953{
7954 enum power_supply_type usb_supply_type;
7955 char *usb_type_name;
7956 int rc;
7957
7958 if (!(chip->wa_flags & SMBCHG_RESTART_WA))
7959 return;
7960
7961 read_usb_type(chip, &usb_type_name, &usb_supply_type);
7962 if (usb_supply_type == POWER_SUPPLY_TYPE_USB_DCP
7963 && !is_hvdcp_present(chip)) {
7964 pr_smb(PR_STATUS, "DCP found rerunning APSD\n");
7965 rc = vote(chip->usb_icl_votable,
7966 CHG_SUSPEND_WORKAROUND_ICL_VOTER, true, 300);
7967 if (rc < 0)
7968 pr_err("Couldn't vote for 300mA for suspend wa, going ahead rc=%d\n",
7969 rc);
7970
7971 pr_smb(PR_STATUS, "Faking Removal\n");
7972 fake_insertion_removal(chip, false);
7973 msleep(500);
7974 pr_smb(PR_STATUS, "Faking Insertion\n");
7975 fake_insertion_removal(chip, true);
7976
7977 read_usb_type(chip, &usb_type_name, &usb_supply_type);
7978 if (usb_supply_type != POWER_SUPPLY_TYPE_USB_DCP) {
7979 msleep(500);
7980 pr_smb(PR_STATUS, "Fake Removal again as type!=DCP\n");
7981 fake_insertion_removal(chip, false);
7982 msleep(500);
7983 pr_smb(PR_STATUS, "Fake Insert again as type!=DCP\n");
7984 fake_insertion_removal(chip, true);
7985 }
7986
7987 rc = vote(chip->usb_icl_votable,
7988 CHG_SUSPEND_WORKAROUND_ICL_VOTER, false, 0);
7989 if (rc < 0)
7990 pr_err("Couldn't vote for 0 for suspend wa, going ahead rc=%d\n",
7991 rc);
7992 }
7993}
7994
7995static int smbchg_probe(struct platform_device *pdev)
7996{
7997 int rc;
7998 struct smbchg_chip *chip;
7999 struct power_supply *typec_psy = NULL;
8000 struct qpnp_vadc_chip *vadc_dev, *vchg_vadc_dev;
8001 const char *typec_psy_name;
8002 struct power_supply_config usb_psy_cfg = {};
8003 struct power_supply_config batt_psy_cfg = {};
8004 struct power_supply_config dc_psy_cfg = {};
8005
8006 if (of_property_read_bool(pdev->dev.of_node, "qcom,external-typec")) {
8007 /* read the type power supply name */
8008 rc = of_property_read_string(pdev->dev.of_node,
8009 "qcom,typec-psy-name", &typec_psy_name);
8010 if (rc) {
8011 pr_err("failed to get prop typec-psy-name rc=%d\n",
8012 rc);
8013 return rc;
8014 }
8015
8016 typec_psy = power_supply_get_by_name(typec_psy_name);
8017 if (!typec_psy) {
8018 pr_smb(PR_STATUS,
8019 "Type-C supply not found, deferring probe\n");
8020 return -EPROBE_DEFER;
8021 }
8022 }
8023
8024 vadc_dev = NULL;
8025 if (of_find_property(pdev->dev.of_node, "qcom,dcin-vadc", NULL)) {
8026 vadc_dev = qpnp_get_vadc(&pdev->dev, "dcin");
8027 if (IS_ERR(vadc_dev)) {
8028 rc = PTR_ERR(vadc_dev);
8029 if (rc != -EPROBE_DEFER)
8030 dev_err(&pdev->dev,
8031 "Couldn't get vadc rc=%d\n",
8032 rc);
8033 return rc;
8034 }
8035 }
8036
8037 vchg_vadc_dev = NULL;
8038 if (of_find_property(pdev->dev.of_node, "qcom,vchg_sns-vadc", NULL)) {
8039 vchg_vadc_dev = qpnp_get_vadc(&pdev->dev, "vchg_sns");
8040 if (IS_ERR(vchg_vadc_dev)) {
8041 rc = PTR_ERR(vchg_vadc_dev);
8042 if (rc != -EPROBE_DEFER)
8043 dev_err(&pdev->dev, "Couldn't get vadc 'vchg' rc=%d\n",
8044 rc);
8045 return rc;
8046 }
8047 }
8048
8049
8050 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
8051 if (!chip)
8052 return -ENOMEM;
8053
8054 chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
8055 if (!chip->regmap) {
8056 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
8057 return -EINVAL;
8058 }
8059
8060 chip->fcc_votable = create_votable("BATT_FCC",
8061 VOTE_MIN,
8062 set_fastchg_current_vote_cb, chip);
8063 if (IS_ERR(chip->fcc_votable)) {
8064 rc = PTR_ERR(chip->fcc_votable);
8065 goto votables_cleanup;
8066 }
8067
8068 chip->usb_icl_votable = create_votable("USB_ICL",
8069 VOTE_MIN,
8070 set_usb_current_limit_vote_cb, chip);
8071 if (IS_ERR(chip->usb_icl_votable)) {
8072 rc = PTR_ERR(chip->usb_icl_votable);
8073 goto votables_cleanup;
8074 }
8075
8076 chip->dc_icl_votable = create_votable("DCIN_ICL",
8077 VOTE_MIN,
8078 set_dc_current_limit_vote_cb, chip);
8079 if (IS_ERR(chip->dc_icl_votable)) {
8080 rc = PTR_ERR(chip->dc_icl_votable);
8081 goto votables_cleanup;
8082 }
8083
8084 chip->usb_suspend_votable = create_votable("USB_SUSPEND",
8085 VOTE_SET_ANY,
8086 usb_suspend_vote_cb, chip);
8087 if (IS_ERR(chip->usb_suspend_votable)) {
8088 rc = PTR_ERR(chip->usb_suspend_votable);
8089 goto votables_cleanup;
8090 }
8091
8092 chip->dc_suspend_votable = create_votable("DC_SUSPEND",
8093 VOTE_SET_ANY,
8094 dc_suspend_vote_cb, chip);
8095 if (IS_ERR(chip->dc_suspend_votable)) {
8096 rc = PTR_ERR(chip->dc_suspend_votable);
8097 goto votables_cleanup;
8098 }
8099
8100 chip->battchg_suspend_votable = create_votable("BATTCHG_SUSPEND",
8101 VOTE_SET_ANY,
8102 charging_suspend_vote_cb, chip);
8103 if (IS_ERR(chip->battchg_suspend_votable)) {
8104 rc = PTR_ERR(chip->battchg_suspend_votable);
8105 goto votables_cleanup;
8106 }
8107
8108 chip->hw_aicl_rerun_disable_votable = create_votable("HWAICL_DISABLE",
8109 VOTE_SET_ANY,
8110 smbchg_hw_aicl_rerun_disable_cb, chip);
8111 if (IS_ERR(chip->hw_aicl_rerun_disable_votable)) {
8112 rc = PTR_ERR(chip->hw_aicl_rerun_disable_votable);
8113 goto votables_cleanup;
8114 }
8115
8116 chip->hw_aicl_rerun_enable_indirect_votable = create_votable(
8117 "HWAICL_ENABLE_INDIRECT",
8118 VOTE_SET_ANY,
8119 smbchg_hw_aicl_rerun_enable_indirect_cb, chip);
8120 if (IS_ERR(chip->hw_aicl_rerun_enable_indirect_votable)) {
8121 rc = PTR_ERR(chip->hw_aicl_rerun_enable_indirect_votable);
8122 goto votables_cleanup;
8123 }
8124
8125 chip->aicl_deglitch_short_votable = create_votable(
8126 "HWAICL_SHORT_DEGLITCH",
8127 VOTE_SET_ANY,
8128 smbchg_aicl_deglitch_config_cb, chip);
8129 if (IS_ERR(chip->aicl_deglitch_short_votable)) {
8130 rc = PTR_ERR(chip->aicl_deglitch_short_votable);
8131 goto votables_cleanup;
8132 }
8133
8134 INIT_WORK(&chip->usb_set_online_work, smbchg_usb_update_online_work);
8135 INIT_DELAYED_WORK(&chip->parallel_en_work,
8136 smbchg_parallel_usb_en_work);
8137 INIT_DELAYED_WORK(&chip->vfloat_adjust_work, smbchg_vfloat_adjust_work);
8138 INIT_DELAYED_WORK(&chip->hvdcp_det_work, smbchg_hvdcp_det_work);
8139 init_completion(&chip->src_det_lowered);
8140 init_completion(&chip->src_det_raised);
8141 init_completion(&chip->usbin_uv_lowered);
8142 init_completion(&chip->usbin_uv_raised);
8143 chip->vadc_dev = vadc_dev;
8144 chip->vchg_vadc_dev = vchg_vadc_dev;
8145 chip->pdev = pdev;
8146 chip->dev = &pdev->dev;
8147
8148 chip->typec_psy = typec_psy;
8149 chip->fake_battery_soc = -EINVAL;
8150 chip->usb_online = -EINVAL;
8151 dev_set_drvdata(&pdev->dev, chip);
8152
8153 spin_lock_init(&chip->sec_access_lock);
8154 mutex_init(&chip->therm_lvl_lock);
8155 mutex_init(&chip->usb_set_online_lock);
8156 mutex_init(&chip->parallel.lock);
8157 mutex_init(&chip->taper_irq_lock);
8158 mutex_init(&chip->pm_lock);
8159 mutex_init(&chip->wipower_config);
8160 mutex_init(&chip->usb_status_lock);
8161 device_init_wakeup(chip->dev, true);
8162
8163 rc = smbchg_parse_peripherals(chip);
8164 if (rc) {
8165 dev_err(chip->dev, "Error parsing DT peripherals: %d\n", rc);
8166 goto votables_cleanup;
8167 }
8168
8169 rc = smbchg_check_chg_version(chip);
8170 if (rc) {
8171 pr_err("Unable to check schg version rc=%d\n", rc);
8172 goto votables_cleanup;
8173 }
8174
8175 rc = smb_parse_dt(chip);
8176 if (rc < 0) {
8177 dev_err(&pdev->dev, "Unable to parse DT nodes: %d\n", rc);
8178 goto votables_cleanup;
8179 }
8180
8181 rc = smbchg_regulator_init(chip);
8182 if (rc) {
8183 dev_err(&pdev->dev,
8184 "Couldn't initialize regulator rc=%d\n", rc);
8185 goto votables_cleanup;
8186 }
8187
8188 chip->extcon = devm_extcon_dev_allocate(chip->dev, smbchg_extcon_cable);
8189 if (IS_ERR(chip->extcon)) {
8190 dev_err(chip->dev, "failed to allocate extcon device\n");
8191 rc = PTR_ERR(chip->extcon);
8192 goto votables_cleanup;
8193 }
8194
8195 rc = devm_extcon_dev_register(chip->dev, chip->extcon);
8196 if (rc) {
8197 dev_err(chip->dev, "failed to register extcon device\n");
8198 goto votables_cleanup;
8199 }
8200
8201 chip->usb_psy_d.name = "usb";
8202 chip->usb_psy_d.type = POWER_SUPPLY_TYPE_USB;
8203 chip->usb_psy_d.get_property = smbchg_usb_get_property;
8204 chip->usb_psy_d.set_property = smbchg_usb_set_property;
8205 chip->usb_psy_d.properties = smbchg_usb_properties;
8206 chip->usb_psy_d.num_properties = ARRAY_SIZE(smbchg_usb_properties);
8207 chip->usb_psy_d.property_is_writeable = smbchg_usb_is_writeable;
8208
8209 usb_psy_cfg.drv_data = chip;
8210 usb_psy_cfg.supplied_to = smbchg_usb_supplicants;
8211 usb_psy_cfg.num_supplicants = ARRAY_SIZE(smbchg_usb_supplicants);
8212
8213 chip->usb_psy = devm_power_supply_register(chip->dev,
8214 &chip->usb_psy_d, &usb_psy_cfg);
8215 if (IS_ERR(chip->usb_psy)) {
8216 dev_err(&pdev->dev, "Unable to register usb_psy rc = %ld\n",
8217 PTR_ERR(chip->usb_psy));
8218 rc = PTR_ERR(chip->usb_psy);
8219 goto votables_cleanup;
8220 }
8221
8222 if (of_find_property(chip->dev->of_node, "dpdm-supply", NULL)) {
8223 chip->dpdm_reg = devm_regulator_get(chip->dev, "dpdm");
8224 if (IS_ERR(chip->dpdm_reg)) {
8225 rc = PTR_ERR(chip->dpdm_reg);
8226 goto votables_cleanup;
8227 }
8228 }
8229
8230 rc = smbchg_hw_init(chip);
8231 if (rc < 0) {
8232 dev_err(&pdev->dev,
Kiran Gundaee7cef82017-09-20 17:44:16 +05308233 "Unable to initialize hardware rc = %d\n", rc);
Kiran Gunda1bc78922017-09-19 13:09:44 +05308234 goto out;
8235 }
8236
8237 rc = determine_initial_status(chip);
8238 if (rc < 0) {
8239 dev_err(&pdev->dev,
8240 "Unable to determine init status rc = %d\n", rc);
8241 goto out;
8242 }
8243
8244 chip->previous_soc = -EINVAL;
8245 chip->batt_psy_d.name = chip->battery_psy_name;
8246 chip->batt_psy_d.type = POWER_SUPPLY_TYPE_BATTERY;
8247 chip->batt_psy_d.get_property = smbchg_battery_get_property;
8248 chip->batt_psy_d.set_property = smbchg_battery_set_property;
8249 chip->batt_psy_d.properties = smbchg_battery_properties;
8250 chip->batt_psy_d.num_properties = ARRAY_SIZE(smbchg_battery_properties);
8251 chip->batt_psy_d.external_power_changed = smbchg_external_power_changed;
8252 chip->batt_psy_d.property_is_writeable = smbchg_battery_is_writeable;
8253
8254 batt_psy_cfg.drv_data = chip;
8255 batt_psy_cfg.num_supplicants = 0;
8256 chip->batt_psy = devm_power_supply_register(chip->dev,
8257 &chip->batt_psy_d,
8258 &batt_psy_cfg);
8259 if (IS_ERR(chip->batt_psy)) {
8260 dev_err(&pdev->dev,
8261 "Unable to register batt_psy rc = %ld\n",
8262 PTR_ERR(chip->batt_psy));
8263 goto out;
8264 }
8265
8266 if (chip->dc_psy_type != -EINVAL) {
8267 chip->dc_psy_d.name = "dc";
8268 chip->dc_psy_d.type = chip->dc_psy_type;
8269 chip->dc_psy_d.get_property = smbchg_dc_get_property;
8270 chip->dc_psy_d.set_property = smbchg_dc_set_property;
8271 chip->dc_psy_d.property_is_writeable = smbchg_dc_is_writeable;
8272 chip->dc_psy_d.properties = smbchg_dc_properties;
8273 chip->dc_psy_d.num_properties
8274 = ARRAY_SIZE(smbchg_dc_properties);
8275
8276 dc_psy_cfg.drv_data = chip;
8277 dc_psy_cfg.num_supplicants
8278 = ARRAY_SIZE(smbchg_dc_supplicants);
8279 dc_psy_cfg.supplied_to = smbchg_dc_supplicants;
8280
8281 chip->dc_psy = devm_power_supply_register(chip->dev,
8282 &chip->dc_psy_d,
8283 &dc_psy_cfg);
8284 if (IS_ERR(chip->dc_psy)) {
8285 dev_err(&pdev->dev,
8286 "Unable to register dc_psy rc = %ld\n",
8287 PTR_ERR(chip->dc_psy));
8288 goto out;
8289 }
8290 }
Subbaraman Narayanamurthy986bff52016-03-21 15:55:19 -07008291 chip->allow_hvdcp3_detection = true;
Kiran Gunda1bc78922017-09-19 13:09:44 +05308292
8293 if (chip->cfg_chg_led_support &&
8294 chip->schg_version == QPNP_SCHG_LITE) {
8295 rc = smbchg_register_chg_led(chip);
8296 if (rc) {
8297 dev_err(chip->dev,
8298 "Unable to register charger led: %d\n",
8299 rc);
8300 goto out;
8301 }
8302
8303 rc = smbchg_chg_led_controls(chip);
8304 if (rc) {
8305 dev_err(chip->dev,
8306 "Failed to set charger led controld bit: %d\n",
8307 rc);
8308 goto unregister_led_class;
8309 }
8310 }
8311
8312 rc = smbchg_request_irqs(chip);
8313 if (rc < 0) {
8314 dev_err(&pdev->dev, "Unable to request irqs rc = %d\n", rc);
8315 goto unregister_led_class;
8316 }
8317
8318 rerun_hvdcp_det_if_necessary(chip);
8319
8320 dump_regs(chip);
8321 create_debugfs_entries(chip);
8322 dev_info(chip->dev,
8323 "SMBCHG successfully probe Charger version=%s Revision DIG:%d.%d ANA:%d.%d batt=%d dc=%d usb=%d\n",
8324 version_str[chip->schg_version],
8325 chip->revision[DIG_MAJOR], chip->revision[DIG_MINOR],
8326 chip->revision[ANA_MAJOR], chip->revision[ANA_MINOR],
8327 get_prop_batt_present(chip),
8328 chip->dc_present, chip->usb_present);
8329 return 0;
8330
8331unregister_led_class:
8332 if (chip->cfg_chg_led_support && chip->schg_version == QPNP_SCHG_LITE)
8333 led_classdev_unregister(&chip->led_cdev);
8334out:
8335 handle_usb_removal(chip);
8336votables_cleanup:
8337 if (chip->aicl_deglitch_short_votable)
8338 destroy_votable(chip->aicl_deglitch_short_votable);
8339 if (chip->hw_aicl_rerun_enable_indirect_votable)
8340 destroy_votable(chip->hw_aicl_rerun_enable_indirect_votable);
8341 if (chip->hw_aicl_rerun_disable_votable)
8342 destroy_votable(chip->hw_aicl_rerun_disable_votable);
8343 if (chip->battchg_suspend_votable)
8344 destroy_votable(chip->battchg_suspend_votable);
8345 if (chip->dc_suspend_votable)
8346 destroy_votable(chip->dc_suspend_votable);
8347 if (chip->usb_suspend_votable)
8348 destroy_votable(chip->usb_suspend_votable);
8349 if (chip->dc_icl_votable)
8350 destroy_votable(chip->dc_icl_votable);
8351 if (chip->usb_icl_votable)
8352 destroy_votable(chip->usb_icl_votable);
8353 if (chip->fcc_votable)
8354 destroy_votable(chip->fcc_votable);
8355 return rc;
8356}
8357
8358static int smbchg_remove(struct platform_device *pdev)
8359{
8360 struct smbchg_chip *chip = dev_get_drvdata(&pdev->dev);
8361
8362 debugfs_remove_recursive(chip->debug_root);
8363
8364 destroy_votable(chip->aicl_deglitch_short_votable);
8365 destroy_votable(chip->hw_aicl_rerun_enable_indirect_votable);
8366 destroy_votable(chip->hw_aicl_rerun_disable_votable);
8367 destroy_votable(chip->battchg_suspend_votable);
8368 destroy_votable(chip->dc_suspend_votable);
8369 destroy_votable(chip->usb_suspend_votable);
8370 destroy_votable(chip->dc_icl_votable);
8371 destroy_votable(chip->usb_icl_votable);
8372 destroy_votable(chip->fcc_votable);
8373
8374 return 0;
8375}
8376
8377static void smbchg_shutdown(struct platform_device *pdev)
8378{
8379 struct smbchg_chip *chip = dev_get_drvdata(&pdev->dev);
8380 int rc;
8381
8382 if (!(chip->wa_flags & SMBCHG_RESTART_WA))
8383 return;
8384
8385 if (!is_hvdcp_present(chip))
8386 return;
8387
8388 pr_smb(PR_MISC, "Disable Parallel\n");
8389 mutex_lock(&chip->parallel.lock);
8390 smbchg_parallel_en = 0;
8391 smbchg_parallel_usb_disable(chip);
8392 mutex_unlock(&chip->parallel.lock);
8393
8394 pr_smb(PR_MISC, "Disable all interrupts\n");
8395 disable_irq(chip->aicl_done_irq);
8396 disable_irq(chip->batt_cold_irq);
8397 disable_irq(chip->batt_cool_irq);
8398 disable_irq(chip->batt_hot_irq);
8399 disable_irq(chip->batt_missing_irq);
8400 disable_irq(chip->batt_warm_irq);
8401 disable_irq(chip->chg_error_irq);
8402 disable_irq(chip->chg_hot_irq);
8403 disable_irq(chip->chg_term_irq);
8404 disable_irq(chip->dcin_uv_irq);
8405 disable_irq(chip->fastchg_irq);
8406 disable_irq(chip->otg_fail_irq);
8407 disable_irq(chip->otg_oc_irq);
8408 disable_irq(chip->power_ok_irq);
8409 disable_irq(chip->recharge_irq);
8410 disable_irq(chip->src_detect_irq);
8411 disable_irq(chip->taper_irq);
8412 disable_irq(chip->usbid_change_irq);
8413 disable_irq(chip->usbin_ov_irq);
8414 disable_irq(chip->usbin_uv_irq);
8415 disable_irq(chip->vbat_low_irq);
8416 disable_irq(chip->wdog_timeout_irq);
8417
8418 /* remove all votes for short deglitch */
8419 vote(chip->aicl_deglitch_short_votable,
8420 VARB_WORKAROUND_SHORT_DEGLITCH_VOTER, false, 0);
8421 vote(chip->aicl_deglitch_short_votable,
8422 HVDCP_SHORT_DEGLITCH_VOTER, false, 0);
8423
8424 /* vote to ensure AICL rerun is enabled */
8425 rc = vote(chip->hw_aicl_rerun_enable_indirect_votable,
8426 SHUTDOWN_WORKAROUND_VOTER, true, 0);
8427 if (rc < 0)
8428 pr_err("Couldn't vote to enable indirect AICL rerun\n");
8429 rc = vote(chip->hw_aicl_rerun_disable_votable,
8430 WEAK_CHARGER_HW_AICL_VOTER, false, 0);
8431 if (rc < 0)
8432 pr_err("Couldn't vote to enable AICL rerun\n");
8433
8434 /* switch to 5V HVDCP */
8435 pr_smb(PR_MISC, "Switch to 5V HVDCP\n");
8436 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + CHGPTH_CFG,
8437 HVDCP_ADAPTER_SEL_MASK, HVDCP_5V);
8438 if (rc < 0) {
8439 pr_err("Couldn't configure HVDCP 5V rc=%d\n", rc);
8440 return;
8441 }
8442
8443 pr_smb(PR_MISC, "Wait 500mS to lower to 5V\n");
8444 /* wait for HVDCP to lower to 5V */
8445 msleep(500);
8446 /*
8447 * Check if the same hvdcp session is in progress. src_det should be
8448 * high and that we are still in 5V hvdcp
8449 */
8450 if (!is_src_detect_high(chip)) {
8451 pr_smb(PR_MISC, "src det low after 500mS sleep\n");
8452 return;
8453 }
8454
8455 /* disable HVDCP */
8456 pr_smb(PR_MISC, "Disable HVDCP\n");
8457 rc = smbchg_sec_masked_write(chip, chip->usb_chgpth_base + CHGPTH_CFG,
8458 HVDCP_EN_BIT, 0);
8459 if (rc < 0)
8460 pr_err("Couldn't disable HVDCP rc=%d\n", rc);
8461
8462 chip->hvdcp_3_det_ignore_uv = true;
8463 /* fake a removal */
8464 pr_smb(PR_MISC, "Faking Removal\n");
8465 rc = fake_insertion_removal(chip, false);
8466 if (rc < 0)
8467 pr_err("Couldn't fake removal HVDCP Removed rc=%d\n", rc);
8468
8469 /* fake an insertion */
8470 pr_smb(PR_MISC, "Faking Insertion\n");
8471 rc = fake_insertion_removal(chip, true);
8472 if (rc < 0)
8473 pr_err("Couldn't fake insertion rc=%d\n", rc);
8474
8475 pr_smb(PR_MISC, "Wait 1S to settle\n");
8476 msleep(1000);
8477 chip->hvdcp_3_det_ignore_uv = false;
8478
8479 pr_smb(PR_STATUS, "wrote power off configurations\n");
8480}
8481
8482static const struct dev_pm_ops smbchg_pm_ops = {
8483};
8484
8485MODULE_DEVICE_TABLE(spmi, smbchg_id);
8486
8487static struct platform_driver smbchg_driver = {
8488 .driver = {
8489 .name = "qpnp-smbcharger",
8490 .owner = THIS_MODULE,
8491 .of_match_table = smbchg_match_table,
8492 .pm = &smbchg_pm_ops,
8493 },
8494 .probe = smbchg_probe,
8495 .remove = smbchg_remove,
8496 .shutdown = smbchg_shutdown,
8497};
8498
8499static int __init smbchg_init(void)
8500{
8501 return platform_driver_register(&smbchg_driver);
8502}
8503
8504static void __exit smbchg_exit(void)
8505{
8506 return platform_driver_unregister(&smbchg_driver);
8507}
8508
8509module_init(smbchg_init);
8510module_exit(smbchg_exit);
8511
8512MODULE_DESCRIPTION("QPNP SMB Charger");
8513MODULE_LICENSE("GPL v2");
8514MODULE_ALIAS("platform:qpnp-smbcharger");