blob: 8633ba01a0ee66bb234cc6386173e3ff0e1f96b1 [file] [log] [blame]
Harry Yang4b7db0f2017-11-27 10:50:44 -08001/* Copyright (c) 2018 The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#ifndef __SMB5_CHARGER_H
14#define __SMB5_CHARGER_H
15#include <linux/types.h>
16#include <linux/interrupt.h>
17#include <linux/irqreturn.h>
18#include <linux/regulator/driver.h>
19#include <linux/regulator/consumer.h>
20#include <linux/extcon.h>
21#include "storm-watch.h"
22
23enum print_reason {
24 PR_INTERRUPT = BIT(0),
25 PR_REGISTER = BIT(1),
26 PR_MISC = BIT(2),
27 PR_PARALLEL = BIT(3),
28 PR_OTG = BIT(4),
29};
30
31#define DEFAULT_VOTER "DEFAULT_VOTER"
32#define USER_VOTER "USER_VOTER"
33#define PD_VOTER "PD_VOTER"
34#define DCP_VOTER "DCP_VOTER"
35#define QC_VOTER "QC_VOTER"
36#define PL_USBIN_USBIN_VOTER "PL_USBIN_USBIN_VOTER"
37#define USB_PSY_VOTER "USB_PSY_VOTER"
38#define PL_TAPER_WORK_RUNNING_VOTER "PL_TAPER_WORK_RUNNING_VOTER"
39#define PL_QNOVO_VOTER "PL_QNOVO_VOTER"
40#define USBIN_V_VOTER "USBIN_V_VOTER"
41#define CHG_STATE_VOTER "CHG_STATE_VOTER"
42#define TYPEC_SRC_VOTER "TYPEC_SRC_VOTER"
43#define TAPER_END_VOTER "TAPER_END_VOTER"
44#define THERMAL_DAEMON_VOTER "THERMAL_DAEMON_VOTER"
45#define CC_DETACHED_VOTER "CC_DETACHED_VOTER"
46#define APSD_VOTER "APSD_VOTER"
47#define PD_DISALLOWED_INDIRECT_VOTER "PD_DISALLOWED_INDIRECT_VOTER"
48#define VBUS_CC_SHORT_VOTER "VBUS_CC_SHORT_VOTER"
49#define PD_INACTIVE_VOTER "PD_INACTIVE_VOTER"
50#define BOOST_BACK_VOTER "BOOST_BACK_VOTER"
51#define USBIN_USBIN_BOOST_VOTER "USBIN_USBIN_BOOST_VOTER"
52#define MICRO_USB_VOTER "MICRO_USB_VOTER"
53#define DEBUG_BOARD_VOTER "DEBUG_BOARD_VOTER"
54#define PD_SUSPEND_SUPPORTED_VOTER "PD_SUSPEND_SUPPORTED_VOTER"
55#define PL_DELAY_VOTER "PL_DELAY_VOTER"
56#define CTM_VOTER "CTM_VOTER"
57#define SW_QC3_VOTER "SW_QC3_VOTER"
58#define AICL_RERUN_VOTER "AICL_RERUN_VOTER"
59#define LEGACY_UNKNOWN_VOTER "LEGACY_UNKNOWN_VOTER"
60#define QNOVO_VOTER "QNOVO_VOTER"
61#define BATT_PROFILE_VOTER "BATT_PROFILE_VOTER"
62#define OTG_DELAY_VOTER "OTG_DELAY_VOTER"
63#define USBIN_I_VOTER "USBIN_I_VOTER"
64#define WEAK_CHARGER_VOTER "WEAK_CHARGER_VOTER"
65#define OTG_VOTER "OTG_VOTER"
66#define PL_FCC_LOW_VOTER "PL_FCC_LOW_VOTER"
67#define WBC_VOTER "WBC_VOTER"
68#define HW_LIMIT_VOTER "HW_LIMIT_VOTER"
69
70#define BOOST_BACK_STORM_COUNT 3
71#define WEAK_CHG_STORM_COUNT 8
72
73enum smb_mode {
74 PARALLEL_MASTER = 0,
75 PARALLEL_SLAVE,
76 NUM_MODES,
77};
78
79enum {
80 BOOST_BACK_WA = BIT(0),
81};
82
83enum smb_irq_index {
84 /* CHGR */
85 CHGR_ERROR_IRQ = 0,
86 CHG_STATE_CHANGE_IRQ,
87 STEP_CHG_STATE_CHANGE_IRQ,
88 STEP_CHG_SOC_UPDATE_FAIL_IRQ,
89 STEP_CHG_SOC_UPDATE_REQ_IRQ,
90 FG_FVCAL_QUALIFIED_IRQ,
91 VPH_ALARM_IRQ,
92 VPH_DROP_PRECHG_IRQ,
93 /* DCDC */
94 OTG_FAIL_IRQ,
95 OTG_OC_DISABLE_SW_IRQ,
96 OTG_OC_HICCUP_IRQ,
97 BSM_ACTIVE_IRQ,
98 HIGH_DUTY_CYCLE_IRQ,
99 INPUT_CURRENT_LIMITING_IRQ,
100 CONCURRENT_MODE_DISABLE_IRQ,
101 SWITCHER_POWER_OK_IRQ,
102 /* BATIF */
103 BAT_TEMP_IRQ,
104 ALL_CHNL_CONV_DONE_IRQ,
105 BAT_OV_IRQ,
106 BAT_LOW_IRQ,
107 BAT_THERM_OR_ID_MISSING_IRQ,
108 BAT_TERMINAL_MISSING_IRQ,
109 BUCK_OC_IRQ,
110 VPH_OV_IRQ,
111 /* USB */
112 USBIN_COLLAPSE_IRQ,
113 USBIN_VASHDN_IRQ,
114 USBIN_UV_IRQ,
115 USBIN_OV_IRQ,
116 USBIN_PLUGIN_IRQ,
117 USBIN_REVI_CHANGE_IRQ,
118 USBIN_SRC_CHANGE_IRQ,
119 USBIN_ICL_CHANGE_IRQ,
120 /* DC */
121 DCIN_VASHDN_IRQ,
122 DCIN_UV_IRQ,
123 DCIN_OV_IRQ,
124 DCIN_PLUGIN_IRQ,
125 DCIN_REVI_IRQ,
126 DCIN_PON_IRQ,
127 DCIN_EN_IRQ,
128 /* TYPEC */
129 TYPEC_OR_RID_DETECTION_CHANGE_IRQ,
130 TYPEC_VPD_DETECT_IRQ,
131 TYPEC_CC_STATE_CHANGE_IRQ,
132 TYPEC_VCONN_OC_IRQ,
133 TYPEC_VBUS_CHANGE_IRQ,
134 TYPEC_ATTACH_DETACH_IRQ,
135 TYPEC_LEGACY_CABLE_DETECT_IRQ,
136 TYPEC_TRY_SNK_SRC_DETECT_IRQ,
137 /* MISC */
138 WDOG_SNARL_IRQ,
139 WDOG_BARK_IRQ,
140 AICL_FAIL_IRQ,
141 AICL_DONE_IRQ,
142 SMB_EN_IRQ,
143 IMP_TRIGGER_IRQ,
144 TEMP_CHANGE_IRQ,
145 TEMP_CHANGE_SMB_IRQ,
146 /* END */
147 SMB_IRQ_MAX,
148};
149
150enum float_options {
151 FLOAT_DCP = 1,
152 FLOAT_SDP = 2,
153 DISABLE_CHARGING = 3,
154 SUSPEND_INPUT = 4,
155};
156
157struct smb_irq_info {
158 const char *name;
159 const irq_handler_t handler;
160 const bool wake;
161 const struct storm_watch storm_data;
162 struct smb_irq_data *irq_data;
163 int irq;
164};
165
166static const unsigned int smblib_extcon_cable[] = {
167 EXTCON_USB,
168 EXTCON_USB_HOST,
169 EXTCON_NONE,
170};
171
172/* EXTCON_USB and EXTCON_USB_HOST are mutually exclusive */
173static const u32 smblib_extcon_exclusive[] = {0x3, 0};
174
175struct smb_regulator {
176 struct regulator_dev *rdev;
177 struct regulator_desc rdesc;
178};
179
180struct smb_irq_data {
181 void *parent_data;
182 const char *name;
183 struct storm_watch storm_data;
184};
185
186struct smb_chg_param {
187 const char *name;
188 u16 reg;
189 int min_u;
190 int max_u;
191 int step_u;
192 int (*get_proc)(struct smb_chg_param *param,
193 u8 val_raw);
194 int (*set_proc)(struct smb_chg_param *param,
195 int val_u,
196 u8 *val_raw);
197};
198
199struct smb_chg_freq {
200 unsigned int freq_5V;
201 unsigned int freq_6V_8V;
202 unsigned int freq_9V;
203 unsigned int freq_12V;
204 unsigned int freq_removal;
205 unsigned int freq_below_otg_threshold;
206 unsigned int freq_above_otg_threshold;
207};
208
209struct smb_params {
210 struct smb_chg_param fcc;
211 struct smb_chg_param fv;
212 struct smb_chg_param usb_icl;
213 struct smb_chg_param icl_stat;
214 struct smb_chg_param otg_cl;
215 struct smb_chg_param jeita_cc_comp_hot;
216 struct smb_chg_param jeita_cc_comp_cold;
217 struct smb_chg_param freq_switcher;
218};
219
220struct parallel_params {
221 struct power_supply *psy;
222};
223
224struct smb_iio {
225 struct iio_channel *temp_chan;
226 struct iio_channel *temp_max_chan;
227 struct iio_channel *usbin_i_chan;
228 struct iio_channel *usbin_v_chan;
229 struct iio_channel *batt_i_chan;
230 struct iio_channel *connector_temp_chan;
231 struct iio_channel *connector_temp_thr1_chan;
232 struct iio_channel *connector_temp_thr2_chan;
233 struct iio_channel *connector_temp_thr3_chan;
234};
235
236struct smb_charger {
237 struct device *dev;
238 char *name;
239 struct regmap *regmap;
240 struct smb_irq_info *irq_info;
241 struct smb_params param;
242 struct smb_iio iio;
243 int *debug_mask;
244 enum smb_mode mode;
245 struct smb_chg_freq chg_freq;
246 int smb_version;
247 int otg_delay_ms;
248 int *weak_chg_icl_ua;
249
250 /* locks */
251 struct mutex lock;
252 struct mutex ps_change_lock;
253 struct mutex otg_oc_lock;
254
255 /* power supplies */
256 struct power_supply *batt_psy;
257 struct power_supply *usb_psy;
258 struct power_supply *dc_psy;
259 struct power_supply *bms_psy;
260 struct power_supply *usb_main_psy;
261 struct power_supply *usb_port_psy;
262 enum power_supply_type real_charger_type;
263
264 /* notifiers */
265 struct notifier_block nb;
266
267 /* parallel charging */
268 struct parallel_params pl;
269
270 /* regulators */
271 struct smb_regulator *vbus_vreg;
272 struct smb_regulator *vconn_vreg;
273 struct regulator *dpdm_reg;
274
275 /* votables */
276 struct votable *dc_suspend_votable;
277 struct votable *fcc_votable;
278 struct votable *fv_votable;
279 struct votable *usb_icl_votable;
280 struct votable *pd_disallowed_votable_indirect;
281 struct votable *pd_allowed_votable;
282 struct votable *awake_votable;
283 struct votable *pl_disable_votable;
284 struct votable *chg_disable_votable;
285 struct votable *pl_enable_votable_indirect;
286 struct votable *usb_irq_enable_votable;
287
288 /* work */
289 struct work_struct bms_update_work;
290 struct work_struct pl_update_work;
291 struct delayed_work ps_change_timeout_work;
292 struct delayed_work clear_hdc_work;
293 struct delayed_work icl_change_work;
294 struct delayed_work pl_enable_work;
295 struct delayed_work uusb_otg_work;
296 struct delayed_work bb_removal_work;
297
298 /* cached status */
299 int voltage_min_uv;
300 int voltage_max_uv;
301 int pd_active;
302 bool system_suspend_supported;
303 int boost_threshold_ua;
304 int system_temp_level;
305 int thermal_levels;
306 int *thermal_mitigation;
307 int dcp_icl_ua;
308 int fake_capacity;
309 int fake_batt_status;
310 bool step_chg_enabled;
311 bool sw_jeita_enabled;
312 bool is_hdc;
313 bool chg_done;
314 bool micro_usb_mode;
315 bool otg_en;
316 bool suspend_input_on_debug_batt;
317 int otg_attempts;
318 int vconn_attempts;
319 int default_icl_ua;
320 int otg_cl_ua;
321 bool uusb_apsd_rerun_done;
322 bool pd_hard_reset;
323 bool typec_present;
324 int fake_input_current_limited;
325 bool pr_swap_in_progress;
326 int typec_mode;
327 int usb_icl_change_irq_enabled;
328 u32 jeita_status;
329 u8 float_cfg;
330 bool use_extcon;
331 bool otg_present;
332
333 /* workaround flag */
334 u32 wa_flags;
335 int boost_current_ua;
336
337 /* extcon for VBUS / ID notification to USB for uUSB */
338 struct extcon_dev *extcon;
339
340 /* battery profile */
341 int batt_profile_fcc_ua;
342 int batt_profile_fv_uv;
343
344 int usb_icl_delta_ua;
345 int pulse_cnt;
346
347 int die_health;
348};
349
350int smblib_read(struct smb_charger *chg, u16 addr, u8 *val);
351int smblib_masked_write(struct smb_charger *chg, u16 addr, u8 mask, u8 val);
352int smblib_write(struct smb_charger *chg, u16 addr, u8 val);
353
354int smblib_get_charge_param(struct smb_charger *chg,
355 struct smb_chg_param *param, int *val_u);
356int smblib_get_usb_suspend(struct smb_charger *chg, int *suspend);
357
358int smblib_enable_charging(struct smb_charger *chg, bool enable);
359int smblib_set_charge_param(struct smb_charger *chg,
360 struct smb_chg_param *param, int val_u);
361int smblib_set_usb_suspend(struct smb_charger *chg, bool suspend);
362int smblib_set_dc_suspend(struct smb_charger *chg, bool suspend);
363
364int smblib_mapping_soc_from_field_value(struct smb_chg_param *param,
365 int val_u, u8 *val_raw);
366int smblib_mapping_cc_delta_to_field_value(struct smb_chg_param *param,
367 u8 val_raw);
368int smblib_mapping_cc_delta_from_field_value(struct smb_chg_param *param,
369 int val_u, u8 *val_raw);
370int smblib_set_chg_freq(struct smb_chg_param *param,
371 int val_u, u8 *val_raw);
372int smblib_vbus_regulator_enable(struct regulator_dev *rdev);
373int smblib_vbus_regulator_disable(struct regulator_dev *rdev);
374int smblib_vbus_regulator_is_enabled(struct regulator_dev *rdev);
375
376int smblib_vconn_regulator_enable(struct regulator_dev *rdev);
377int smblib_vconn_regulator_disable(struct regulator_dev *rdev);
378int smblib_vconn_regulator_is_enabled(struct regulator_dev *rdev);
379
380irqreturn_t default_irq_handler(int irq, void *data);
381irqreturn_t chg_state_change_irq_handler(int irq, void *data);
382irqreturn_t batt_temp_changed_irq_handler(int irq, void *data);
383irqreturn_t batt_psy_changed_irq_handler(int irq, void *data);
384irqreturn_t usbin_uv_irq_handler(int irq, void *data);
385irqreturn_t usb_plugin_irq_handler(int irq, void *data);
386irqreturn_t usb_source_change_irq_handler(int irq, void *data);
387irqreturn_t icl_change_irq_handler(int irq, void *data);
388irqreturn_t typec_state_change_irq_handler(int irq, void *data);
389irqreturn_t dc_plugin_irq_handler(int irq, void *data);
390irqreturn_t high_duty_cycle_irq_handler(int irq, void *data);
391irqreturn_t switcher_power_ok_irq_handler(int irq, void *data);
392irqreturn_t wdog_bark_irq_handler(int irq, void *data);
393
394int smblib_get_prop_input_suspend(struct smb_charger *chg,
395 union power_supply_propval *val);
396int smblib_get_prop_batt_present(struct smb_charger *chg,
397 union power_supply_propval *val);
398int smblib_get_prop_batt_capacity(struct smb_charger *chg,
399 union power_supply_propval *val);
400int smblib_get_prop_batt_status(struct smb_charger *chg,
401 union power_supply_propval *val);
402int smblib_get_prop_batt_charge_type(struct smb_charger *chg,
403 union power_supply_propval *val);
404int smblib_get_prop_batt_charge_done(struct smb_charger *chg,
405 union power_supply_propval *val);
406int smblib_get_prop_batt_health(struct smb_charger *chg,
407 union power_supply_propval *val);
408int smblib_get_prop_system_temp_level(struct smb_charger *chg,
409 union power_supply_propval *val);
410int smblib_get_prop_system_temp_level_max(struct smb_charger *chg,
411 union power_supply_propval *val);
412int smblib_get_prop_input_current_limited(struct smb_charger *chg,
413 union power_supply_propval *val);
414int smblib_get_prop_batt_voltage_now(struct smb_charger *chg,
415 union power_supply_propval *val);
416int smblib_get_prop_batt_current_now(struct smb_charger *chg,
417 union power_supply_propval *val);
418int smblib_get_prop_batt_temp(struct smb_charger *chg,
419 union power_supply_propval *val);
420int smblib_get_prop_batt_charge_counter(struct smb_charger *chg,
421 union power_supply_propval *val);
422int smblib_set_prop_input_suspend(struct smb_charger *chg,
423 const union power_supply_propval *val);
424int smblib_set_prop_batt_capacity(struct smb_charger *chg,
425 const union power_supply_propval *val);
426int smblib_set_prop_batt_status(struct smb_charger *chg,
427 const union power_supply_propval *val);
428int smblib_set_prop_system_temp_level(struct smb_charger *chg,
429 const union power_supply_propval *val);
430int smblib_set_prop_input_current_limited(struct smb_charger *chg,
431 const union power_supply_propval *val);
432
433int smblib_get_prop_dc_present(struct smb_charger *chg,
434 union power_supply_propval *val);
435int smblib_get_prop_dc_online(struct smb_charger *chg,
436 union power_supply_propval *val);
437int smblib_get_prop_dc_current_max(struct smb_charger *chg,
438 union power_supply_propval *val);
439int smblib_set_prop_dc_current_max(struct smb_charger *chg,
440 const union power_supply_propval *val);
441int smblib_get_prop_usb_present(struct smb_charger *chg,
442 union power_supply_propval *val);
443int smblib_get_prop_usb_online(struct smb_charger *chg,
444 union power_supply_propval *val);
445int smblib_get_prop_usb_suspend(struct smb_charger *chg,
446 union power_supply_propval *val);
447int smblib_get_prop_usb_voltage_max(struct smb_charger *chg,
448 union power_supply_propval *val);
449int smblib_get_prop_typec_cc_orientation(struct smb_charger *chg,
450 union power_supply_propval *val);
451int smblib_get_prop_typec_power_role(struct smb_charger *chg,
452 union power_supply_propval *val);
453int smblib_get_prop_pd_allowed(struct smb_charger *chg,
454 union power_supply_propval *val);
455int smblib_get_prop_input_current_settled(struct smb_charger *chg,
456 union power_supply_propval *val);
457int smblib_get_prop_input_voltage_settled(struct smb_charger *chg,
458 union power_supply_propval *val);
459int smblib_get_prop_pd_in_hard_reset(struct smb_charger *chg,
460 union power_supply_propval *val);
461int smblib_get_pe_start(struct smb_charger *chg,
462 union power_supply_propval *val);
463int smblib_get_prop_die_health(struct smb_charger *chg,
464 union power_supply_propval *val);
465int smblib_set_prop_pd_current_max(struct smb_charger *chg,
466 const union power_supply_propval *val);
467int smblib_set_prop_sdp_current_max(struct smb_charger *chg,
468 const union power_supply_propval *val);
469int smblib_set_prop_pd_voltage_max(struct smb_charger *chg,
470 const union power_supply_propval *val);
471int smblib_set_prop_pd_voltage_min(struct smb_charger *chg,
472 const union power_supply_propval *val);
473int smblib_set_prop_boost_current(struct smb_charger *chg,
474 const union power_supply_propval *val);
475int smblib_set_prop_typec_power_role(struct smb_charger *chg,
476 const union power_supply_propval *val);
477int smblib_set_prop_pd_active(struct smb_charger *chg,
478 const union power_supply_propval *val);
479int smblib_set_prop_pd_in_hard_reset(struct smb_charger *chg,
480 const union power_supply_propval *val);
481int smblib_set_prop_ship_mode(struct smb_charger *chg,
482 const union power_supply_propval *val);
483void smblib_suspend_on_debug_battery(struct smb_charger *chg);
484int smblib_rerun_apsd_if_required(struct smb_charger *chg);
485int smblib_get_prop_fcc_delta(struct smb_charger *chg,
486 union power_supply_propval *val);
487int smblib_icl_override(struct smb_charger *chg, bool override);
488int smblib_dp_dm(struct smb_charger *chg, int val);
489int smblib_disable_hw_jeita(struct smb_charger *chg, bool disable);
490int smblib_rerun_aicl(struct smb_charger *chg);
491int smblib_set_icl_current(struct smb_charger *chg, int icl_ua);
492int smblib_get_icl_current(struct smb_charger *chg, int *icl_ua);
493int smblib_get_charge_current(struct smb_charger *chg, int *total_current_ua);
494int smblib_get_prop_pr_swap_in_progress(struct smb_charger *chg,
495 union power_supply_propval *val);
496int smblib_set_prop_pr_swap_in_progress(struct smb_charger *chg,
497 const union power_supply_propval *val);
498int smblib_stat_sw_override_cfg(struct smb_charger *chg, bool override);
499
500int smblib_init(struct smb_charger *chg);
501int smblib_deinit(struct smb_charger *chg);
502#endif /* __SMB5_CHARGER_H */