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