Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 1 | /* Copyright (c) 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 | |
| 13 | #ifndef __SMB2_CHARGER_H |
| 14 | #define __SMB2_CHARGER_H |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/irqreturn.h> |
| 17 | #include <linux/regulator/driver.h> |
| 18 | #include <linux/regulator/consumer.h> |
Nicholas Troast | 47ae461 | 2016-08-03 09:49:36 -0700 | [diff] [blame] | 19 | #include "storm-watch.h" |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 20 | |
| 21 | enum print_reason { |
| 22 | PR_INTERRUPT = BIT(0), |
| 23 | PR_REGISTER = BIT(1), |
| 24 | PR_MISC = BIT(2), |
| 25 | }; |
| 26 | |
Harry Yang | 995b742 | 2016-08-29 16:06:50 -0700 | [diff] [blame^] | 27 | #define DEFAULT_VOTER "DEFAULT_VOTER" |
| 28 | #define USER_VOTER "USER_VOTER" |
| 29 | #define PD_VOTER "PD_VOTER" |
| 30 | #define PL_TAPER_WORK_RUNNING_VOTER "PL_TAPER_WORK_RUNNING_VOTER" |
| 31 | #define PARALLEL_PSY_VOTER "PARALLEL_PSY_VOTER" |
| 32 | #define USBIN_ICL_VOTER "USBIN_ICL_VOTER" |
| 33 | #define CHG_STATE_VOTER "CHG_STATE_VOTER" |
| 34 | #define TYPEC_SRC_VOTER "TYPEC_SRC_VOTER" |
| 35 | #define TAPER_END_VOTER "TAPER_END_VOTER" |
| 36 | #define FCC_MAX_RESULT "FCC_MAX_RESULT" |
| 37 | #define THERMAL_DAEMON "THERMAL_DAEMON" |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 38 | |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 39 | enum smb_mode { |
| 40 | PARALLEL_MASTER = 0, |
| 41 | PARALLEL_SLAVE, |
| 42 | NUM_MODES, |
| 43 | }; |
| 44 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 45 | struct smb_regulator { |
| 46 | struct regulator_dev *rdev; |
| 47 | struct regulator_desc rdesc; |
| 48 | }; |
| 49 | |
| 50 | struct smb_irq_data { |
Nicholas Troast | 47ae461 | 2016-08-03 09:49:36 -0700 | [diff] [blame] | 51 | void *parent_data; |
| 52 | const char *name; |
| 53 | struct storm_watch storm_data; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct smb_chg_param { |
| 57 | const char *name; |
| 58 | u16 reg; |
| 59 | int min_u; |
| 60 | int max_u; |
| 61 | int step_u; |
Harry Yang | f8b4125 | 2016-08-10 14:21:10 -0700 | [diff] [blame] | 62 | int (*get_proc)(struct smb_chg_param *param, |
| 63 | u8 val_raw); |
| 64 | int (*set_proc)(struct smb_chg_param *param, |
| 65 | int val_u, |
| 66 | u8 *val_raw); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct smb_params { |
| 70 | struct smb_chg_param fcc; |
| 71 | struct smb_chg_param fv; |
| 72 | struct smb_chg_param usb_icl; |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 73 | struct smb_chg_param icl_stat; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 74 | struct smb_chg_param dc_icl; |
Abhijeet Dharmapurikar | 5cf5faf | 2016-06-21 14:20:24 -0700 | [diff] [blame] | 75 | struct smb_chg_param dc_icl_pt_lv; |
| 76 | struct smb_chg_param dc_icl_pt_hv; |
| 77 | struct smb_chg_param dc_icl_div2_lv; |
| 78 | struct smb_chg_param dc_icl_div2_mid_lv; |
| 79 | struct smb_chg_param dc_icl_div2_mid_hv; |
| 80 | struct smb_chg_param dc_icl_div2_hv; |
Abhijeet Dharmapurikar | 2644cd6 | 2016-07-20 16:54:55 -0700 | [diff] [blame] | 81 | struct smb_chg_param jeita_cc_comp; |
Harry Yang | fe91384 | 2016-08-10 12:27:28 -0700 | [diff] [blame] | 82 | struct smb_chg_param step_soc_threshold[4]; |
| 83 | struct smb_chg_param step_soc; |
| 84 | struct smb_chg_param step_cc_delta[5]; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 87 | struct parallel_params { |
| 88 | struct power_supply *psy; |
| 89 | int *master_percent; |
| 90 | int taper_percent; |
| 91 | int slave_fcc; |
| 92 | }; |
| 93 | |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 94 | struct smb_iio { |
| 95 | struct iio_channel *temp_chan; |
| 96 | struct iio_channel *temp_max_chan; |
| 97 | struct iio_channel *usbin_i_chan; |
| 98 | struct iio_channel *usbin_v_chan; |
| 99 | }; |
| 100 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 101 | struct smb_charger { |
| 102 | struct device *dev; |
| 103 | struct regmap *regmap; |
| 104 | struct smb_params param; |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 105 | struct smb_iio iio; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 106 | int *debug_mask; |
Nicholas Troast | 320839e | 2016-06-03 10:18:00 -0700 | [diff] [blame] | 107 | enum smb_mode mode; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 108 | |
| 109 | /* locks */ |
Harry Yang | bacd222 | 2016-05-11 16:43:51 -0700 | [diff] [blame] | 110 | struct mutex write_lock; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 111 | struct mutex ps_change_lock; |
| 112 | |
| 113 | /* power supplies */ |
| 114 | struct power_supply *batt_psy; |
| 115 | struct power_supply *usb_psy; |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 116 | struct power_supply *dc_psy; |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 117 | struct power_supply *bms_psy; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 118 | struct power_supply_desc usb_psy_desc; |
| 119 | |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 120 | /* notifiers */ |
| 121 | struct notifier_block nb; |
| 122 | |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 123 | /* parallel charging */ |
| 124 | struct parallel_params pl; |
| 125 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 126 | /* regulators */ |
| 127 | struct smb_regulator *vbus_vreg; |
| 128 | struct smb_regulator *vconn_vreg; |
| 129 | struct regulator *dpdm_reg; |
| 130 | |
| 131 | /* votables */ |
| 132 | struct votable *usb_suspend_votable; |
| 133 | struct votable *dc_suspend_votable; |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 134 | struct votable *fcc_max_votable; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 135 | struct votable *fcc_votable; |
Abhijeet Dharmapurikar | f9805d4 | 2016-05-17 18:38:42 -0700 | [diff] [blame] | 136 | struct votable *fv_votable; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 137 | struct votable *usb_icl_votable; |
| 138 | struct votable *dc_icl_votable; |
| 139 | struct votable *pd_allowed_votable; |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 140 | struct votable *awake_votable; |
| 141 | struct votable *pl_disable_votable; |
Abhijeet Dharmapurikar | ee54de0 | 2016-07-08 14:51:47 -0700 | [diff] [blame] | 142 | struct votable *chg_disable_votable; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 143 | |
| 144 | /* work */ |
Harry Yang | 5e1a522 | 2016-07-26 15:16:04 -0700 | [diff] [blame] | 145 | struct work_struct bms_update_work; |
Harry Yang | 58a9e7a | 2016-06-23 14:54:43 -0700 | [diff] [blame] | 146 | struct work_struct pl_detect_work; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 147 | struct delayed_work hvdcp_detect_work; |
| 148 | struct delayed_work ps_change_timeout_work; |
Harry Yang | 1d1034c | 2016-06-15 12:09:42 -0700 | [diff] [blame] | 149 | struct delayed_work pl_taper_work; |
Harry Yang | fe91384 | 2016-08-10 12:27:28 -0700 | [diff] [blame] | 150 | struct delayed_work step_soc_req_work; |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 151 | struct delayed_work clear_hdc_work; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 152 | |
| 153 | /* cached status */ |
| 154 | int voltage_min_uv; |
| 155 | int voltage_max_uv; |
| 156 | bool pd_active; |
| 157 | bool vbus_present; |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 158 | |
| 159 | int system_temp_level; |
| 160 | int thermal_levels; |
| 161 | int *thermal_mitigation; |
Abhijeet Dharmapurikar | 9e7e48c | 2016-08-23 12:55:49 -0700 | [diff] [blame] | 162 | |
| 163 | int fake_capacity; |
Harry Yang | fe91384 | 2016-08-10 12:27:28 -0700 | [diff] [blame] | 164 | |
| 165 | bool step_chg_enabled; |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 166 | bool is_hdc; |
Subbaraman Narayanamurthy | 270a265 | 2016-10-04 17:08:42 -0700 | [diff] [blame] | 167 | bool chg_done; |
Harry Yang | f725198 | 2016-09-16 11:14:49 -0700 | [diff] [blame] | 168 | |
| 169 | /* workaround flag */ |
| 170 | u32 wa_flags; |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | int smblib_read(struct smb_charger *chg, u16 addr, u8 *val); |
| 174 | int smblib_masked_write(struct smb_charger *chg, u16 addr, u8 mask, u8 val); |
| 175 | int smblib_write(struct smb_charger *chg, u16 addr, u8 val); |
| 176 | |
Nicholas Troast | 4c31049 | 2016-05-12 17:56:35 -0700 | [diff] [blame] | 177 | int smblib_get_charge_param(struct smb_charger *chg, |
| 178 | struct smb_chg_param *param, int *val_u); |
| 179 | int smblib_get_usb_suspend(struct smb_charger *chg, int *suspend); |
| 180 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 181 | int smblib_enable_charging(struct smb_charger *chg, bool enable); |
Nicholas Troast | 4c31049 | 2016-05-12 17:56:35 -0700 | [diff] [blame] | 182 | int smblib_set_charge_param(struct smb_charger *chg, |
| 183 | struct smb_chg_param *param, int val_u); |
| 184 | int smblib_set_usb_suspend(struct smb_charger *chg, bool suspend); |
| 185 | int smblib_set_dc_suspend(struct smb_charger *chg, bool suspend); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 186 | |
Harry Yang | fe91384 | 2016-08-10 12:27:28 -0700 | [diff] [blame] | 187 | int smblib_mapping_soc_from_field_value(struct smb_chg_param *param, |
| 188 | int val_u, u8 *val_raw); |
| 189 | int smblib_mapping_cc_delta_to_field_value(struct smb_chg_param *param, |
| 190 | u8 val_raw); |
| 191 | int smblib_mapping_cc_delta_from_field_value(struct smb_chg_param *param, |
| 192 | int val_u, u8 *val_raw); |
| 193 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 194 | int smblib_vbus_regulator_enable(struct regulator_dev *rdev); |
| 195 | int smblib_vbus_regulator_disable(struct regulator_dev *rdev); |
| 196 | int smblib_vbus_regulator_is_enabled(struct regulator_dev *rdev); |
| 197 | |
| 198 | int smblib_vconn_regulator_enable(struct regulator_dev *rdev); |
| 199 | int smblib_vconn_regulator_disable(struct regulator_dev *rdev); |
| 200 | int smblib_vconn_regulator_is_enabled(struct regulator_dev *rdev); |
| 201 | |
| 202 | irqreturn_t smblib_handle_debug(int irq, void *data); |
Harry Yang | 6fe72ab | 2016-06-14 16:21:39 -0700 | [diff] [blame] | 203 | irqreturn_t smblib_handle_chg_state_change(int irq, void *data); |
Harry Yang | fe91384 | 2016-08-10 12:27:28 -0700 | [diff] [blame] | 204 | irqreturn_t smblib_handle_step_chg_state_change(int irq, void *data); |
| 205 | irqreturn_t smblib_handle_step_chg_soc_update_fail(int irq, void *data); |
| 206 | irqreturn_t smblib_handle_step_chg_soc_update_request(int irq, void *data); |
Abhijeet Dharmapurikar | 2644cd6 | 2016-07-20 16:54:55 -0700 | [diff] [blame] | 207 | irqreturn_t smblib_handle_batt_temp_changed(int irq, void *data); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 208 | irqreturn_t smblib_handle_batt_psy_changed(int irq, void *data); |
| 209 | irqreturn_t smblib_handle_usb_psy_changed(int irq, void *data); |
| 210 | irqreturn_t smblib_handle_usb_plugin(int irq, void *data); |
| 211 | irqreturn_t smblib_handle_usb_source_change(int irq, void *data); |
Harry Yang | 6fe72ab | 2016-06-14 16:21:39 -0700 | [diff] [blame] | 212 | irqreturn_t smblib_handle_icl_change(int irq, void *data); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 213 | irqreturn_t smblib_handle_usb_typec_change(int irq, void *data); |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 214 | irqreturn_t smblib_handle_high_duty_cycle(int irq, void *data); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 215 | |
| 216 | int smblib_get_prop_input_suspend(struct smb_charger *chg, |
| 217 | union power_supply_propval *val); |
| 218 | int smblib_get_prop_batt_present(struct smb_charger *chg, |
| 219 | union power_supply_propval *val); |
| 220 | int smblib_get_prop_batt_capacity(struct smb_charger *chg, |
| 221 | union power_supply_propval *val); |
| 222 | int smblib_get_prop_batt_status(struct smb_charger *chg, |
| 223 | union power_supply_propval *val); |
| 224 | int smblib_get_prop_batt_charge_type(struct smb_charger *chg, |
| 225 | union power_supply_propval *val); |
| 226 | int smblib_get_prop_batt_health(struct smb_charger *chg, |
| 227 | union power_supply_propval *val); |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 228 | int smblib_get_prop_system_temp_level(struct smb_charger *chg, |
| 229 | union power_supply_propval *val); |
Abhijeet Dharmapurikar | 0e36900 | 2016-09-07 17:25:36 -0700 | [diff] [blame] | 230 | int smblib_get_prop_input_current_limited(struct smb_charger *chg, |
| 231 | union power_supply_propval *val); |
Nicholas Troast | 66b21d7 | 2016-09-20 15:33:20 -0700 | [diff] [blame] | 232 | int smblib_get_prop_batt_voltage_now(struct smb_charger *chg, |
| 233 | union power_supply_propval *val); |
| 234 | int smblib_get_prop_batt_current_now(struct smb_charger *chg, |
| 235 | union power_supply_propval *val); |
| 236 | int smblib_get_prop_batt_temp(struct smb_charger *chg, |
| 237 | union power_supply_propval *val); |
Harry Yang | bedee33 | 2016-08-31 16:14:29 -0700 | [diff] [blame] | 238 | int smblib_get_prop_step_chg_step(struct smb_charger *chg, |
| 239 | union power_supply_propval *val); |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 240 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 241 | int smblib_set_prop_input_suspend(struct smb_charger *chg, |
| 242 | const union power_supply_propval *val); |
Abhijeet Dharmapurikar | 9e7e48c | 2016-08-23 12:55:49 -0700 | [diff] [blame] | 243 | int smblib_set_prop_batt_capacity(struct smb_charger *chg, |
| 244 | const union power_supply_propval *val); |
Abhijeet Dharmapurikar | 99fb894 | 2016-07-08 11:39:23 -0700 | [diff] [blame] | 245 | int smblib_set_prop_system_temp_level(struct smb_charger *chg, |
| 246 | const union power_supply_propval *val); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 247 | |
Harry Yang | f302359 | 2016-07-20 14:56:41 -0700 | [diff] [blame] | 248 | int smblib_get_prop_dc_present(struct smb_charger *chg, |
| 249 | union power_supply_propval *val); |
| 250 | int smblib_get_prop_dc_online(struct smb_charger *chg, |
| 251 | union power_supply_propval *val); |
| 252 | int smblib_get_prop_dc_current_max(struct smb_charger *chg, |
| 253 | union power_supply_propval *val); |
| 254 | int smblib_set_prop_dc_current_max(struct smb_charger *chg, |
| 255 | const union power_supply_propval *val); |
| 256 | |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 257 | int smblib_get_prop_usb_present(struct smb_charger *chg, |
| 258 | union power_supply_propval *val); |
| 259 | int smblib_get_prop_usb_online(struct smb_charger *chg, |
| 260 | union power_supply_propval *val); |
| 261 | int smblib_get_prop_usb_suspend(struct smb_charger *chg, |
| 262 | union power_supply_propval *val); |
| 263 | int smblib_get_prop_usb_voltage_now(struct smb_charger *chg, |
| 264 | union power_supply_propval *val); |
| 265 | int smblib_get_prop_usb_current_max(struct smb_charger *chg, |
| 266 | union power_supply_propval *val); |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 267 | int smblib_get_prop_usb_current_now(struct smb_charger *chg, |
| 268 | union power_supply_propval *val); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 269 | int smblib_get_prop_typec_cc_orientation(struct smb_charger *chg, |
| 270 | union power_supply_propval *val); |
| 271 | int smblib_get_prop_typec_mode(struct smb_charger *chg, |
| 272 | union power_supply_propval *val); |
| 273 | int smblib_get_prop_typec_power_role(struct smb_charger *chg, |
| 274 | union power_supply_propval *val); |
| 275 | int smblib_get_prop_pd_allowed(struct smb_charger *chg, |
| 276 | union power_supply_propval *val); |
Nicholas Troast | 133a7f5 | 2016-06-29 13:48:20 -0700 | [diff] [blame] | 277 | int smblib_get_prop_input_current_settled(struct smb_charger *chg, |
| 278 | union power_supply_propval *val); |
Harry Yang | ba874ce | 2016-08-19 14:17:01 -0700 | [diff] [blame] | 279 | int smblib_get_prop_charger_temp(struct smb_charger *chg, |
| 280 | union power_supply_propval *val); |
| 281 | int smblib_get_prop_charger_temp_max(struct smb_charger *chg, |
| 282 | union power_supply_propval *val); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 283 | int smblib_set_prop_usb_current_max(struct smb_charger *chg, |
| 284 | const union power_supply_propval *val); |
| 285 | int smblib_set_prop_usb_voltage_min(struct smb_charger *chg, |
| 286 | const union power_supply_propval *val); |
| 287 | int smblib_set_prop_usb_voltage_max(struct smb_charger *chg, |
| 288 | const union power_supply_propval *val); |
| 289 | int smblib_set_prop_typec_power_role(struct smb_charger *chg, |
| 290 | const union power_supply_propval *val); |
| 291 | int smblib_set_prop_pd_active(struct smb_charger *chg, |
| 292 | const union power_supply_propval *val); |
| 293 | |
| 294 | int smblib_init(struct smb_charger *chg); |
Abhijeet Dharmapurikar | 8e9e757 | 2016-06-06 16:13:14 -0700 | [diff] [blame] | 295 | int smblib_deinit(struct smb_charger *chg); |
Nicholas Troast | 34db503 | 2016-03-28 12:26:44 -0700 | [diff] [blame] | 296 | #endif /* __SMB2_CHARGER_H */ |
| 297 | |