1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016-2019 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2023, Linaro Ltd.
5  * Author: Caleb Connolly <caleb.connolly@linaro.org>
6  *
7  * This driver is for the switch-mode battery charger and boost
8  * hardware found in pmi8998 and related PMICs.
9  */
10 
11 #include <linux/bits.h>
12 #include <linux/devm-helpers.h>
13 #include <linux/iio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/minmax.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_wakeirq.h>
20 #include <linux/of.h>
21 #include <linux/power_supply.h>
22 #include <linux/regmap.h>
23 #include <linux/types.h>
24 #include <linux/workqueue.h>
25 
26 /* clang-format off */
27 #define BATTERY_CHARGER_STATUS_1			0x06
28 #define BVR_INITIAL_RAMP_BIT				BIT(7)
29 #define CC_SOFT_TERMINATE_BIT				BIT(6)
30 #define STEP_CHARGING_STATUS_SHIFT			3
31 #define STEP_CHARGING_STATUS_MASK			GENMASK(5, 3)
32 #define BATTERY_CHARGER_STATUS_MASK			GENMASK(2, 0)
33 
34 #define BATTERY_CHARGER_STATUS_2			0x07
35 #define INPUT_CURRENT_LIMITED_BIT			BIT(7)
36 #define CHARGER_ERROR_STATUS_SFT_EXPIRE_BIT		BIT(6)
37 #define CHARGER_ERROR_STATUS_BAT_OV_BIT			BIT(5)
38 #define CHARGER_ERROR_STATUS_BAT_TERM_MISSING_BIT	BIT(4)
39 #define BAT_TEMP_STATUS_MASK				GENMASK(3, 0)
40 #define BAT_TEMP_STATUS_SOFT_LIMIT_MASK			GENMASK(3, 2)
41 #define BAT_TEMP_STATUS_HOT_SOFT_LIMIT_BIT		BIT(3)
42 #define BAT_TEMP_STATUS_COLD_SOFT_LIMIT_BIT		BIT(2)
43 #define BAT_TEMP_STATUS_TOO_HOT_BIT			BIT(1)
44 #define BAT_TEMP_STATUS_TOO_COLD_BIT			BIT(0)
45 
46 #define BATTERY_CHARGER_STATUS_4			0x0A
47 #define CHARGE_CURRENT_POST_JEITA_MASK			GENMASK(7, 0)
48 
49 #define BATTERY_CHARGER_STATUS_7			0x0D
50 #define ENABLE_TRICKLE_BIT				BIT(7)
51 #define ENABLE_PRE_CHARGING_BIT				BIT(6)
52 #define ENABLE_FAST_CHARGING_BIT			BIT(5)
53 #define ENABLE_FULLON_MODE_BIT				BIT(4)
54 #define TOO_COLD_ADC_BIT				BIT(3)
55 #define TOO_HOT_ADC_BIT					BIT(2)
56 #define HOT_SL_ADC_BIT					BIT(1)
57 #define COLD_SL_ADC_BIT					BIT(0)
58 
59 #define CHARGING_ENABLE_CMD				0x42
60 #define CHARGING_ENABLE_CMD_BIT				BIT(0)
61 
62 #define CHGR_CFG2					0x51
63 #define CHG_EN_SRC_BIT					BIT(7)
64 #define CHG_EN_POLARITY_BIT				BIT(6)
65 #define PRETOFAST_TRANSITION_CFG_BIT			BIT(5)
66 #define BAT_OV_ECC_BIT					BIT(4)
67 #define I_TERM_BIT					BIT(3)
68 #define AUTO_RECHG_BIT					BIT(2)
69 #define EN_ANALOG_DROP_IN_VBATT_BIT			BIT(1)
70 #define CHARGER_INHIBIT_BIT				BIT(0)
71 
72 #define PRE_CHARGE_CURRENT_CFG				0x60
73 #define PRE_CHARGE_CURRENT_SETTING_MASK			GENMASK(5, 0)
74 
75 #define FAST_CHARGE_CURRENT_CFG				0x61
76 #define FAST_CHARGE_CURRENT_SETTING_MASK		GENMASK(7, 0)
77 
78 #define FLOAT_VOLTAGE_CFG				0x70
79 #define FLOAT_VOLTAGE_SETTING_MASK			GENMASK(7, 0)
80 
81 #define FG_UPDATE_CFG_2_SEL				0x7D
82 #define SOC_LT_OTG_THRESH_SEL_BIT			BIT(3)
83 #define SOC_LT_CHG_RECHARGE_THRESH_SEL_BIT		BIT(2)
84 #define VBT_LT_CHG_RECHARGE_THRESH_SEL_BIT		BIT(1)
85 #define IBT_LT_CHG_TERM_THRESH_SEL_BIT			BIT(0)
86 
87 #define JEITA_EN_CFG					0x90
88 #define JEITA_EN_HARDLIMIT_BIT				BIT(4)
89 #define JEITA_EN_HOT_SL_FCV_BIT				BIT(3)
90 #define JEITA_EN_COLD_SL_FCV_BIT			BIT(2)
91 #define JEITA_EN_HOT_SL_CCC_BIT				BIT(1)
92 #define JEITA_EN_COLD_SL_CCC_BIT			BIT(0)
93 
94 #define INT_RT_STS					0x310
95 #define TYPE_C_CHANGE_RT_STS_BIT			BIT(7)
96 #define USBIN_ICL_CHANGE_RT_STS_BIT			BIT(6)
97 #define USBIN_SOURCE_CHANGE_RT_STS_BIT			BIT(5)
98 #define USBIN_PLUGIN_RT_STS_BIT				BIT(4)
99 #define USBIN_OV_RT_STS_BIT				BIT(3)
100 #define USBIN_UV_RT_STS_BIT				BIT(2)
101 #define USBIN_LT_3P6V_RT_STS_BIT			BIT(1)
102 #define USBIN_COLLAPSE_RT_STS_BIT			BIT(0)
103 
104 #define OTG_CFG						0x153
105 #define OTG_RESERVED_MASK				GENMASK(7, 6)
106 #define DIS_OTG_ON_TLIM_BIT				BIT(5)
107 #define QUICKSTART_OTG_FASTROLESWAP_BIT			BIT(4)
108 #define INCREASE_DFP_TIME_BIT				BIT(3)
109 #define ENABLE_OTG_IN_DEBUG_MODE_BIT			BIT(2)
110 #define OTG_EN_SRC_CFG_BIT				BIT(1)
111 #define CONCURRENT_MODE_CFG_BIT				BIT(0)
112 
113 #define OTG_ENG_OTG_CFG					0x1C0
114 #define ENG_BUCKBOOST_HALT1_8_MODE_BIT			BIT(0)
115 
116 #define APSD_STATUS					0x307
117 #define APSD_STATUS_7_BIT				BIT(7)
118 #define HVDCP_CHECK_TIMEOUT_BIT				BIT(6)
119 #define SLOW_PLUGIN_TIMEOUT_BIT				BIT(5)
120 #define ENUMERATION_DONE_BIT				BIT(4)
121 #define VADP_CHANGE_DONE_AFTER_AUTH_BIT			BIT(3)
122 #define QC_AUTH_DONE_STATUS_BIT				BIT(2)
123 #define QC_CHARGER_BIT					BIT(1)
124 #define APSD_DTC_STATUS_DONE_BIT			BIT(0)
125 
126 #define APSD_RESULT_STATUS				0x308
127 #define ICL_OVERRIDE_LATCH_BIT				BIT(7)
128 #define APSD_RESULT_STATUS_MASK				GENMASK(6, 0)
129 #define QC_3P0_BIT					BIT(6)
130 #define QC_2P0_BIT					BIT(5)
131 #define FLOAT_CHARGER_BIT				BIT(4)
132 #define DCP_CHARGER_BIT					BIT(3)
133 #define CDP_CHARGER_BIT					BIT(2)
134 #define OCP_CHARGER_BIT					BIT(1)
135 #define SDP_CHARGER_BIT					BIT(0)
136 
137 #define TYPE_C_STATUS_1					0x30B
138 #define UFP_TYPEC_MASK					GENMASK(7, 5)
139 #define UFP_TYPEC_RDSTD_BIT				BIT(7)
140 #define UFP_TYPEC_RD1P5_BIT				BIT(6)
141 #define UFP_TYPEC_RD3P0_BIT				BIT(5)
142 #define UFP_TYPEC_FMB_255K_BIT				BIT(4)
143 #define UFP_TYPEC_FMB_301K_BIT				BIT(3)
144 #define UFP_TYPEC_FMB_523K_BIT				BIT(2)
145 #define UFP_TYPEC_FMB_619K_BIT				BIT(1)
146 #define UFP_TYPEC_OPEN_OPEN_BIT				BIT(0)
147 
148 #define TYPE_C_STATUS_2					0x30C
149 #define DFP_RA_OPEN_BIT					BIT(7)
150 #define TIMER_STAGE_BIT					BIT(6)
151 #define EXIT_UFP_MODE_BIT				BIT(5)
152 #define EXIT_DFP_MODE_BIT				BIT(4)
153 #define DFP_TYPEC_MASK					GENMASK(3, 0)
154 #define DFP_RD_OPEN_BIT					BIT(3)
155 #define DFP_RD_RA_VCONN_BIT				BIT(2)
156 #define DFP_RD_RD_BIT					BIT(1)
157 #define DFP_RA_RA_BIT					BIT(0)
158 
159 #define TYPE_C_STATUS_3					0x30D
160 #define ENABLE_BANDGAP_BIT				BIT(7)
161 #define U_USB_GND_NOVBUS_BIT				BIT(6)
162 #define U_USB_FLOAT_NOVBUS_BIT				BIT(5)
163 #define U_USB_GND_BIT					BIT(4)
164 #define U_USB_FMB1_BIT					BIT(3)
165 #define U_USB_FLOAT1_BIT				BIT(2)
166 #define U_USB_FMB2_BIT					BIT(1)
167 #define U_USB_FLOAT2_BIT				BIT(0)
168 
169 #define TYPE_C_STATUS_4					0x30E
170 #define UFP_DFP_MODE_STATUS_BIT				BIT(7)
171 #define TYPEC_VBUS_STATUS_BIT				BIT(6)
172 #define TYPEC_VBUS_ERROR_STATUS_BIT			BIT(5)
173 #define TYPEC_DEBOUNCE_DONE_STATUS_BIT			BIT(4)
174 #define TYPEC_UFP_AUDIO_ADAPT_STATUS_BIT		BIT(3)
175 #define TYPEC_VCONN_OVERCURR_STATUS_BIT			BIT(2)
176 #define CC_ORIENTATION_BIT				BIT(1)
177 #define CC_ATTACHED_BIT					BIT(0)
178 
179 #define TYPE_C_STATUS_5					0x30F
180 #define TRY_SOURCE_FAILED_BIT				BIT(6)
181 #define TRY_SINK_FAILED_BIT				BIT(5)
182 #define TIMER_STAGE_2_BIT				BIT(4)
183 #define TYPEC_LEGACY_CABLE_STATUS_BIT			BIT(3)
184 #define TYPEC_NONCOMP_LEGACY_CABLE_STATUS_BIT		BIT(2)
185 #define TYPEC_TRYSOURCE_DETECT_STATUS_BIT		BIT(1)
186 #define TYPEC_TRYSINK_DETECT_STATUS_BIT			BIT(0)
187 
188 #define CMD_APSD					0x341
189 #define ICL_OVERRIDE_BIT				BIT(1)
190 #define APSD_RERUN_BIT					BIT(0)
191 
192 #define TYPE_C_CFG					0x358
193 #define APSD_START_ON_CC_BIT				BIT(7)
194 #define WAIT_FOR_APSD_BIT				BIT(6)
195 #define FACTORY_MODE_DETECTION_EN_BIT			BIT(5)
196 #define FACTORY_MODE_ICL_3A_4A_BIT			BIT(4)
197 #define FACTORY_MODE_DIS_CHGING_CFG_BIT			BIT(3)
198 #define SUSPEND_NON_COMPLIANT_CFG_BIT			BIT(2)
199 #define VCONN_OC_CFG_BIT				BIT(1)
200 #define TYPE_C_OR_U_USB_BIT				BIT(0)
201 
202 #define TYPE_C_CFG_2					0x359
203 #define TYPE_C_DFP_CURRSRC_MODE_BIT			BIT(7)
204 #define DFP_CC_1P4V_OR_1P6V_BIT				BIT(6)
205 #define VCONN_SOFTSTART_CFG_MASK			GENMASK(5, 4)
206 #define EN_TRY_SOURCE_MODE_BIT				BIT(3)
207 #define USB_FACTORY_MODE_ENABLE_BIT			BIT(2)
208 #define TYPE_C_UFP_MODE_BIT				BIT(1)
209 #define EN_80UA_180UA_CUR_SOURCE_BIT			BIT(0)
210 
211 #define TYPE_C_CFG_3					0x35A
212 #define TVBUS_DEBOUNCE_BIT				BIT(7)
213 #define TYPEC_LEGACY_CABLE_INT_EN_BIT			BIT(6)
214 #define TYPEC_NONCOMPLIANT_LEGACY_CABLE_INT_EN_B	BIT(5)
215 #define TYPEC_TRYSOURCE_DETECT_INT_EN_BIT		BIT(4)
216 #define TYPEC_TRYSINK_DETECT_INT_EN_BIT			BIT(3)
217 #define EN_TRYSINK_MODE_BIT				BIT(2)
218 #define EN_LEGACY_CABLE_DETECTION_BIT			BIT(1)
219 #define ALLOW_PD_DRING_UFP_TCCDB_BIT			BIT(0)
220 
221 #define USBIN_OPTIONS_1_CFG				0x362
222 #define CABLE_R_SEL_BIT					BIT(7)
223 #define HVDCP_AUTH_ALG_EN_CFG_BIT			BIT(6)
224 #define HVDCP_AUTONOMOUS_MODE_EN_CFG_BIT		BIT(5)
225 #define INPUT_PRIORITY_BIT				BIT(4)
226 #define AUTO_SRC_DETECT_BIT				BIT(3)
227 #define HVDCP_EN_BIT					BIT(2)
228 #define VADP_INCREMENT_VOLTAGE_LIMIT_BIT		BIT(1)
229 #define VADP_TAPER_TIMER_EN_BIT				BIT(0)
230 
231 #define USBIN_OPTIONS_2_CFG				0x363
232 #define WIPWR_RST_EUD_CFG_BIT				BIT(7)
233 #define SWITCHER_START_CFG_BIT				BIT(6)
234 #define DCD_TIMEOUT_SEL_BIT				BIT(5)
235 #define OCD_CURRENT_SEL_BIT				BIT(4)
236 #define SLOW_PLUGIN_TIMER_EN_CFG_BIT			BIT(3)
237 #define FLOAT_OPTIONS_MASK				GENMASK(2, 0)
238 #define FLOAT_DIS_CHGING_CFG_BIT			BIT(2)
239 #define SUSPEND_FLOAT_CFG_BIT				BIT(1)
240 #define FORCE_FLOAT_SDP_CFG_BIT				BIT(0)
241 
242 #define TAPER_TIMER_SEL_CFG				0x364
243 #define TYPEC_SPARE_CFG_BIT				BIT(7)
244 #define TYPEC_DRP_DFP_TIME_CFG_BIT			BIT(5)
245 #define TAPER_TIMER_SEL_MASK				GENMASK(1, 0)
246 
247 #define USBIN_LOAD_CFG					0x365
248 #define USBIN_OV_CH_LOAD_OPTION_BIT			BIT(7)
249 #define ICL_OVERRIDE_AFTER_APSD_BIT			BIT(4)
250 
251 #define USBIN_ICL_OPTIONS				0x366
252 #define CFG_USB3P0_SEL_BIT				BIT(2)
253 #define USB51_MODE_BIT					BIT(1)
254 #define USBIN_MODE_CHG_BIT				BIT(0)
255 
256 #define TYPE_C_INTRPT_ENB_SOFTWARE_CTRL			0x368
257 #define EXIT_SNK_BASED_ON_CC_BIT			BIT(7)
258 #define VCONN_EN_ORIENTATION_BIT			BIT(6)
259 #define TYPEC_VCONN_OVERCURR_INT_EN_BIT			BIT(5)
260 #define VCONN_EN_SRC_BIT				BIT(4)
261 #define VCONN_EN_VALUE_BIT				BIT(3)
262 #define TYPEC_POWER_ROLE_CMD_MASK			GENMASK(2, 0)
263 #define UFP_EN_CMD_BIT					BIT(2)
264 #define DFP_EN_CMD_BIT					BIT(1)
265 #define TYPEC_DISABLE_CMD_BIT				BIT(0)
266 
267 #define USBIN_CURRENT_LIMIT_CFG				0x370
268 #define USBIN_CURRENT_LIMIT_MASK			GENMASK(7, 0)
269 
270 #define USBIN_AICL_OPTIONS_CFG				0x380
271 #define SUSPEND_ON_COLLAPSE_USBIN_BIT			BIT(7)
272 #define USBIN_AICL_HDC_EN_BIT				BIT(6)
273 #define USBIN_AICL_START_AT_MAX_BIT			BIT(5)
274 #define USBIN_AICL_RERUN_EN_BIT				BIT(4)
275 #define USBIN_AICL_ADC_EN_BIT				BIT(3)
276 #define USBIN_AICL_EN_BIT				BIT(2)
277 #define USBIN_HV_COLLAPSE_RESPONSE_BIT			BIT(1)
278 #define USBIN_LV_COLLAPSE_RESPONSE_BIT			BIT(0)
279 
280 #define USBIN_5V_AICL_THRESHOLD_CFG			0x381
281 #define USBIN_5V_AICL_THRESHOLD_CFG_MASK		GENMASK(2, 0)
282 
283 #define USBIN_CONT_AICL_THRESHOLD_CFG			0x384
284 #define USBIN_CONT_AICL_THRESHOLD_CFG_MASK		GENMASK(5, 0)
285 
286 #define DC_ENG_SSUPPLY_CFG2				0x4C1
287 #define ENG_SSUPPLY_IVREF_OTG_SS_MASK			GENMASK(2, 0)
288 #define OTG_SS_SLOW					0x3
289 
290 #define DCIN_AICL_REF_SEL_CFG				0x481
291 #define DCIN_CONT_AICL_THRESHOLD_CFG_MASK		GENMASK(5, 0)
292 
293 #define WI_PWR_OPTIONS					0x495
294 #define CHG_OK_BIT					BIT(7)
295 #define WIPWR_UVLO_IRQ_OPT_BIT				BIT(6)
296 #define BUCK_HOLDOFF_ENABLE_BIT				BIT(5)
297 #define CHG_OK_HW_SW_SELECT_BIT				BIT(4)
298 #define WIPWR_RST_ENABLE_BIT				BIT(3)
299 #define DCIN_WIPWR_IRQ_SELECT_BIT			BIT(2)
300 #define AICL_SWITCH_ENABLE_BIT				BIT(1)
301 #define ZIN_ICL_ENABLE_BIT				BIT(0)
302 
303 #define ICL_STATUS					0x607
304 #define INPUT_CURRENT_LIMIT_MASK			GENMASK(7, 0)
305 
306 #define POWER_PATH_STATUS				0x60B
307 #define P_PATH_INPUT_SS_DONE_BIT			BIT(7)
308 #define P_PATH_USBIN_SUSPEND_STS_BIT			BIT(6)
309 #define P_PATH_DCIN_SUSPEND_STS_BIT			BIT(5)
310 #define P_PATH_USE_USBIN_BIT				BIT(4)
311 #define P_PATH_USE_DCIN_BIT				BIT(3)
312 #define P_PATH_POWER_PATH_MASK				GENMASK(2, 1)
313 #define P_PATH_VALID_INPUT_POWER_SOURCE_STS_BIT		BIT(0)
314 
315 #define BARK_BITE_WDOG_PET				0x643
316 #define BARK_BITE_WDOG_PET_BIT				BIT(0)
317 
318 #define WD_CFG						0x651
319 #define WATCHDOG_TRIGGER_AFP_EN_BIT			BIT(7)
320 #define BARK_WDOG_INT_EN_BIT				BIT(6)
321 #define BITE_WDOG_INT_EN_BIT				BIT(5)
322 #define SFT_AFTER_WDOG_IRQ_MASK				GENMASK(4, 3)
323 #define WDOG_IRQ_SFT_BIT				BIT(2)
324 #define WDOG_TIMER_EN_ON_PLUGIN_BIT			BIT(1)
325 #define WDOG_TIMER_EN_BIT				BIT(0)
326 
327 #define SNARL_BARK_BITE_WD_CFG				0x653
328 #define BITE_WDOG_DISABLE_CHARGING_CFG_BIT		BIT(7)
329 #define SNARL_WDOG_TIMEOUT_MASK				GENMASK(6, 4)
330 #define BARK_WDOG_TIMEOUT_MASK				GENMASK(3, 2)
331 #define BITE_WDOG_TIMEOUT_MASK				GENMASK(1, 0)
332 
333 #define AICL_RERUN_TIME_CFG				0x661
334 #define AICL_RERUN_TIME_MASK				GENMASK(1, 0)
335 
336 #define STAT_CFG					0x690
337 #define STAT_SW_OVERRIDE_VALUE_BIT			BIT(7)
338 #define STAT_SW_OVERRIDE_CFG_BIT			BIT(6)
339 #define STAT_PARALLEL_OFF_DG_CFG_MASK			GENMASK(5, 4)
340 #define STAT_POLARITY_CFG_BIT				BIT(3)
341 #define STAT_PARALLEL_CFG_BIT				BIT(2)
342 #define STAT_FUNCTION_CFG_BIT				BIT(1)
343 #define STAT_IRQ_PULSING_EN_BIT				BIT(0)
344 
345 #define SDP_CURRENT_UA					500000
346 #define CDP_CURRENT_UA					1500000
347 #define DCP_CURRENT_UA					1500000
348 #define CURRENT_MAX_UA					DCP_CURRENT_UA
349 
350 /* pmi8998 registers represent current in increments of 1/40th of an amp */
351 #define CURRENT_SCALE_FACTOR				25000
352 /* clang-format on */
353 
354 enum charger_status {
355 	TRICKLE_CHARGE = 0,
356 	PRE_CHARGE,
357 	FAST_CHARGE,
358 	FULLON_CHARGE,
359 	TAPER_CHARGE,
360 	TERMINATE_CHARGE,
361 	INHIBIT_CHARGE,
362 	DISABLE_CHARGE,
363 };
364 
365 struct smb2_register {
366 	u16 addr;
367 	u8 mask;
368 	u8 val;
369 };
370 
371 /**
372  * struct smb2_chip - smb2 chip structure
373  * @dev:		Device reference for power_supply
374  * @name:		The platform device name
375  * @base:		Base address for smb2 registers
376  * @regmap:		Register map
377  * @batt_info:		Battery data from DT
378  * @status_change_work: Worker to handle plug/unplug events
379  * @cable_irq:		USB plugin IRQ
380  * @wakeup_enabled:	If the cable IRQ will cause a wakeup
381  * @usb_in_i_chan:	USB_IN current measurement channel
382  * @usb_in_v_chan:	USB_IN voltage measurement channel
383  * @chg_psy:		Charger power supply instance
384  */
385 struct smb2_chip {
386 	struct device *dev;
387 	const char *name;
388 	unsigned int base;
389 	struct regmap *regmap;
390 	struct power_supply_battery_info *batt_info;
391 
392 	struct delayed_work status_change_work;
393 	int cable_irq;
394 	bool wakeup_enabled;
395 
396 	struct iio_channel *usb_in_i_chan;
397 	struct iio_channel *usb_in_v_chan;
398 
399 	struct power_supply *chg_psy;
400 };
401 
402 static enum power_supply_property smb2_properties[] = {
403 	POWER_SUPPLY_PROP_MANUFACTURER,
404 	POWER_SUPPLY_PROP_MODEL_NAME,
405 	POWER_SUPPLY_PROP_CURRENT_MAX,
406 	POWER_SUPPLY_PROP_CURRENT_NOW,
407 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
408 	POWER_SUPPLY_PROP_STATUS,
409 	POWER_SUPPLY_PROP_HEALTH,
410 	POWER_SUPPLY_PROP_ONLINE,
411 	POWER_SUPPLY_PROP_USB_TYPE,
412 	POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
413 	POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
414 };
415 
416 static enum power_supply_usb_type smb2_usb_types[] = {
417 	POWER_SUPPLY_USB_TYPE_UNKNOWN,
418 	POWER_SUPPLY_USB_TYPE_SDP,
419 	POWER_SUPPLY_USB_TYPE_DCP,
420 	POWER_SUPPLY_USB_TYPE_CDP,
421 };
422 
423 static int smb2_get_prop_usb_online(struct smb2_chip *chip, int *val)
424 {
425 	unsigned int stat;
426 	int rc;
427 
428 	rc = regmap_read(chip->regmap, chip->base + POWER_PATH_STATUS, &stat);
429 	if (rc < 0) {
430 		dev_err(chip->dev, "Couldn't read power path status: %d\n", rc);
431 		return rc;
432 	}
433 
434 	*val = (stat & P_PATH_USE_USBIN_BIT) &&
435 	       (stat & P_PATH_VALID_INPUT_POWER_SOURCE_STS_BIT);
436 	return 0;
437 }
438 
439 /*
440  * Qualcomm "automatic power source detection" aka APSD
441  * tells us what type of charger we're connected to.
442  */
443 static int smb2_apsd_get_charger_type(struct smb2_chip *chip, int *val)
444 {
445 	unsigned int apsd_stat, stat;
446 	int usb_online = 0;
447 	int rc;
448 
449 	rc = smb2_get_prop_usb_online(chip, &usb_online);
450 	if (!usb_online) {
451 		*val = POWER_SUPPLY_USB_TYPE_UNKNOWN;
452 		return rc;
453 	}
454 
455 	rc = regmap_read(chip->regmap, chip->base + APSD_STATUS, &apsd_stat);
456 	if (rc < 0) {
457 		dev_err(chip->dev, "Failed to read apsd status, rc = %d", rc);
458 		return rc;
459 	}
460 	if (!(apsd_stat & APSD_DTC_STATUS_DONE_BIT)) {
461 		dev_dbg(chip->dev, "Apsd not ready");
462 		return -EAGAIN;
463 	}
464 
465 	rc = regmap_read(chip->regmap, chip->base + APSD_RESULT_STATUS, &stat);
466 	if (rc < 0) {
467 		dev_err(chip->dev, "Failed to read apsd result, rc = %d", rc);
468 		return rc;
469 	}
470 
471 	stat &= APSD_RESULT_STATUS_MASK;
472 
473 	if (stat & CDP_CHARGER_BIT)
474 		*val = POWER_SUPPLY_USB_TYPE_CDP;
475 	else if (stat & (DCP_CHARGER_BIT | OCP_CHARGER_BIT | FLOAT_CHARGER_BIT))
476 		*val = POWER_SUPPLY_USB_TYPE_DCP;
477 	else /* SDP_CHARGER_BIT (or others) */
478 		*val = POWER_SUPPLY_USB_TYPE_SDP;
479 
480 	return 0;
481 }
482 
483 static int smb2_get_prop_status(struct smb2_chip *chip, int *val)
484 {
485 	unsigned char stat[2];
486 	int usb_online = 0;
487 	int rc;
488 
489 	rc = smb2_get_prop_usb_online(chip, &usb_online);
490 	if (!usb_online) {
491 		*val = POWER_SUPPLY_STATUS_DISCHARGING;
492 		return rc;
493 	}
494 
495 	rc = regmap_bulk_read(chip->regmap,
496 			      chip->base + BATTERY_CHARGER_STATUS_1, &stat, 2);
497 	if (rc < 0) {
498 		dev_err(chip->dev, "Failed to read charging status ret=%d\n",
499 			rc);
500 		return rc;
501 	}
502 
503 	if (stat[1] & CHARGER_ERROR_STATUS_BAT_OV_BIT) {
504 		*val = POWER_SUPPLY_STATUS_NOT_CHARGING;
505 		return 0;
506 	}
507 
508 	stat[0] = stat[0] & BATTERY_CHARGER_STATUS_MASK;
509 
510 	switch (stat[0]) {
511 	case TRICKLE_CHARGE:
512 	case PRE_CHARGE:
513 	case FAST_CHARGE:
514 	case FULLON_CHARGE:
515 	case TAPER_CHARGE:
516 		*val = POWER_SUPPLY_STATUS_CHARGING;
517 		return rc;
518 	case DISABLE_CHARGE:
519 		*val = POWER_SUPPLY_STATUS_NOT_CHARGING;
520 		return rc;
521 	case TERMINATE_CHARGE:
522 		*val = POWER_SUPPLY_STATUS_FULL;
523 		return rc;
524 	case INHIBIT_CHARGE:
525 	default:
526 		*val = POWER_SUPPLY_STATUS_UNKNOWN;
527 		return rc;
528 	}
529 }
530 
531 static inline int smb2_get_current_limit(struct smb2_chip *chip,
532 					 unsigned int *val)
533 {
534 	int rc = regmap_read(chip->regmap, chip->base + ICL_STATUS, val);
535 
536 	if (rc >= 0)
537 		*val *= CURRENT_SCALE_FACTOR;
538 	return rc;
539 }
540 
541 static int smb2_set_current_limit(struct smb2_chip *chip, unsigned int val)
542 {
543 	unsigned char val_raw;
544 
545 	if (val > 4800000) {
546 		dev_err(chip->dev,
547 			"Can't set current limit higher than 4800000uA");
548 		return -EINVAL;
549 	}
550 	val_raw = val / CURRENT_SCALE_FACTOR;
551 
552 	return regmap_write(chip->regmap, chip->base + USBIN_CURRENT_LIMIT_CFG,
553 			    val_raw);
554 }
555 
556 static void smb2_status_change_work(struct work_struct *work)
557 {
558 	unsigned int charger_type, current_ua;
559 	int usb_online, count, rc;
560 	struct smb2_chip *chip;
561 
562 	chip = container_of(work, struct smb2_chip, status_change_work.work);
563 
564 	smb2_get_prop_usb_online(chip, &usb_online);
565 	if (!usb_online)
566 		return;
567 
568 	for (count = 0; count < 3; count++) {
569 		dev_dbg(chip->dev, "get charger type retry %d\n", count);
570 		rc = smb2_apsd_get_charger_type(chip, &charger_type);
571 		if (rc != -EAGAIN)
572 			break;
573 		msleep(100);
574 	}
575 
576 	if (rc < 0 && rc != -EAGAIN) {
577 		dev_err(chip->dev, "get charger type failed: %d\n", rc);
578 		return;
579 	}
580 
581 	if (rc < 0) {
582 		rc = regmap_update_bits(chip->regmap, chip->base + CMD_APSD,
583 					APSD_RERUN_BIT, APSD_RERUN_BIT);
584 		schedule_delayed_work(&chip->status_change_work,
585 				      msecs_to_jiffies(1000));
586 		dev_dbg(chip->dev, "get charger type failed, rerun apsd\n");
587 		return;
588 	}
589 
590 	switch (charger_type) {
591 	case POWER_SUPPLY_USB_TYPE_CDP:
592 		current_ua = CDP_CURRENT_UA;
593 		break;
594 	case POWER_SUPPLY_USB_TYPE_DCP:
595 		current_ua = DCP_CURRENT_UA;
596 		break;
597 	case POWER_SUPPLY_USB_TYPE_SDP:
598 	default:
599 		current_ua = SDP_CURRENT_UA;
600 		break;
601 	}
602 
603 	smb2_set_current_limit(chip, current_ua);
604 	power_supply_changed(chip->chg_psy);
605 }
606 
607 static int smb2_get_iio_chan(struct smb2_chip *chip, struct iio_channel *chan,
608 			     int *val)
609 {
610 	int rc;
611 	union power_supply_propval status;
612 
613 	rc = power_supply_get_property(chip->chg_psy, POWER_SUPPLY_PROP_STATUS,
614 				       &status);
615 	if (rc < 0 || status.intval != POWER_SUPPLY_STATUS_CHARGING) {
616 		*val = 0;
617 		return 0;
618 	}
619 
620 	if (IS_ERR(chan)) {
621 		dev_err(chip->dev, "Failed to chan, err = %li", PTR_ERR(chan));
622 		return PTR_ERR(chan);
623 	}
624 
625 	return iio_read_channel_processed(chan, val);
626 }
627 
628 static int smb2_get_prop_health(struct smb2_chip *chip, int *val)
629 {
630 	int rc;
631 	unsigned int stat;
632 
633 	rc = regmap_read(chip->regmap, chip->base + BATTERY_CHARGER_STATUS_2,
634 			 &stat);
635 	if (rc < 0) {
636 		dev_err(chip->dev, "Couldn't read charger status rc=%d\n", rc);
637 		return rc;
638 	}
639 
640 	switch (stat) {
641 	case CHARGER_ERROR_STATUS_BAT_OV_BIT:
642 		*val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
643 		return 0;
644 	case BAT_TEMP_STATUS_TOO_COLD_BIT:
645 		*val = POWER_SUPPLY_HEALTH_COLD;
646 		return 0;
647 	case BAT_TEMP_STATUS_TOO_HOT_BIT:
648 		*val = POWER_SUPPLY_HEALTH_OVERHEAT;
649 		return 0;
650 	case BAT_TEMP_STATUS_COLD_SOFT_LIMIT_BIT:
651 		*val = POWER_SUPPLY_HEALTH_COOL;
652 		return 0;
653 	case BAT_TEMP_STATUS_HOT_SOFT_LIMIT_BIT:
654 		*val = POWER_SUPPLY_HEALTH_WARM;
655 		return 0;
656 	default:
657 		*val = POWER_SUPPLY_HEALTH_GOOD;
658 		return 0;
659 	}
660 }
661 
662 static int smb2_get_property(struct power_supply *psy,
663 			     enum power_supply_property psp,
664 			     union power_supply_propval *val)
665 {
666 	struct smb2_chip *chip = power_supply_get_drvdata(psy);
667 
668 	switch (psp) {
669 	case POWER_SUPPLY_PROP_MANUFACTURER:
670 		val->strval = "Qualcomm";
671 		return 0;
672 	case POWER_SUPPLY_PROP_MODEL_NAME:
673 		val->strval = chip->name;
674 		return 0;
675 	case POWER_SUPPLY_PROP_CURRENT_MAX:
676 	case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
677 		return smb2_get_current_limit(chip, &val->intval);
678 	case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
679 		val->intval = DCP_CURRENT_UA;
680 		return 0;
681 	case POWER_SUPPLY_PROP_CURRENT_NOW:
682 		return smb2_get_iio_chan(chip, chip->usb_in_i_chan,
683 					 &val->intval);
684 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
685 		return smb2_get_iio_chan(chip, chip->usb_in_v_chan,
686 					 &val->intval);
687 	case POWER_SUPPLY_PROP_ONLINE:
688 		return smb2_get_prop_usb_online(chip, &val->intval);
689 	case POWER_SUPPLY_PROP_STATUS:
690 		return smb2_get_prop_status(chip, &val->intval);
691 	case POWER_SUPPLY_PROP_HEALTH:
692 		return smb2_get_prop_health(chip, &val->intval);
693 	case POWER_SUPPLY_PROP_USB_TYPE:
694 		return smb2_apsd_get_charger_type(chip, &val->intval);
695 	default:
696 		dev_err(chip->dev, "invalid property: %d\n", psp);
697 		return -EINVAL;
698 	}
699 }
700 
701 static int smb2_set_property(struct power_supply *psy,
702 			     enum power_supply_property psp,
703 			     const union power_supply_propval *val)
704 {
705 	struct smb2_chip *chip = power_supply_get_drvdata(psy);
706 
707 	switch (psp) {
708 	case POWER_SUPPLY_PROP_CURRENT_MAX:
709 	case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
710 		return smb2_set_current_limit(chip, val->intval);
711 	default:
712 		dev_err(chip->dev, "No setter for property: %d\n", psp);
713 		return -EINVAL;
714 	}
715 }
716 
717 static int smb2_property_is_writable(struct power_supply *psy,
718 				     enum power_supply_property psp)
719 {
720 	switch (psp) {
721 	case POWER_SUPPLY_PROP_CURRENT_MAX:
722 	case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
723 		return 1;
724 	default:
725 		return 0;
726 	}
727 }
728 
729 static irqreturn_t smb2_handle_batt_overvoltage(int irq, void *data)
730 {
731 	struct smb2_chip *chip = data;
732 	unsigned int status;
733 
734 	regmap_read(chip->regmap, chip->base + BATTERY_CHARGER_STATUS_2,
735 		    &status);
736 
737 	if (status & CHARGER_ERROR_STATUS_BAT_OV_BIT) {
738 		/* The hardware stops charging automatically */
739 		dev_err(chip->dev, "battery overvoltage detected\n");
740 		power_supply_changed(chip->chg_psy);
741 	}
742 
743 	return IRQ_HANDLED;
744 }
745 
746 static irqreturn_t smb2_handle_usb_plugin(int irq, void *data)
747 {
748 	struct smb2_chip *chip = data;
749 
750 	power_supply_changed(chip->chg_psy);
751 
752 	schedule_delayed_work(&chip->status_change_work,
753 			      msecs_to_jiffies(1500));
754 
755 	return IRQ_HANDLED;
756 }
757 
758 static irqreturn_t smb2_handle_usb_icl_change(int irq, void *data)
759 {
760 	struct smb2_chip *chip = data;
761 
762 	power_supply_changed(chip->chg_psy);
763 
764 	return IRQ_HANDLED;
765 }
766 
767 static irqreturn_t smb2_handle_wdog_bark(int irq, void *data)
768 {
769 	struct smb2_chip *chip = data;
770 	int rc;
771 
772 	power_supply_changed(chip->chg_psy);
773 
774 	rc = regmap_write(chip->regmap, BARK_BITE_WDOG_PET,
775 			  BARK_BITE_WDOG_PET_BIT);
776 	if (rc < 0)
777 		dev_err(chip->dev, "Couldn't pet the dog rc=%d\n", rc);
778 
779 	return IRQ_HANDLED;
780 }
781 
782 static const struct power_supply_desc smb2_psy_desc = {
783 	.name = "pmi8998_charger",
784 	.type = POWER_SUPPLY_TYPE_USB,
785 	.usb_types = smb2_usb_types,
786 	.num_usb_types = ARRAY_SIZE(smb2_usb_types),
787 	.properties = smb2_properties,
788 	.num_properties = ARRAY_SIZE(smb2_properties),
789 	.get_property = smb2_get_property,
790 	.set_property = smb2_set_property,
791 	.property_is_writeable = smb2_property_is_writable,
792 };
793 
794 /* Init sequence derived from vendor downstream driver */
795 static const struct smb2_register smb2_init_seq[] = {
796 	{ .addr = AICL_RERUN_TIME_CFG, .mask = AICL_RERUN_TIME_MASK, .val = 0 },
797 	/*
798 	 * By default configure us as an upstream facing port
799 	 * FIXME: This will be handled by the type-c driver
800 	 */
801 	{ .addr = TYPE_C_INTRPT_ENB_SOFTWARE_CTRL,
802 	  .mask = TYPEC_POWER_ROLE_CMD_MASK | VCONN_EN_SRC_BIT |
803 		  VCONN_EN_VALUE_BIT,
804 	  .val = VCONN_EN_SRC_BIT },
805 	/*
806 	 * Disable Type-C factory mode and stay in Attached.SRC state when VCONN
807 	 * over-current happens
808 	 */
809 	{ .addr = TYPE_C_CFG,
810 	  .mask = FACTORY_MODE_DETECTION_EN_BIT | VCONN_OC_CFG_BIT,
811 	  .val = 0 },
812 	/* Configure VBUS for software control */
813 	{ .addr = OTG_CFG, .mask = OTG_EN_SRC_CFG_BIT, .val = 0 },
814 	/*
815 	 * Use VBAT to determine the recharge threshold when battery is full
816 	 * rather than the state of charge.
817 	 */
818 	{ .addr = FG_UPDATE_CFG_2_SEL,
819 	  .mask = SOC_LT_CHG_RECHARGE_THRESH_SEL_BIT |
820 		  VBT_LT_CHG_RECHARGE_THRESH_SEL_BIT,
821 	  .val = VBT_LT_CHG_RECHARGE_THRESH_SEL_BIT },
822 	/* Enable charging */
823 	{ .addr = USBIN_OPTIONS_1_CFG, .mask = HVDCP_EN_BIT, .val = 0 },
824 	{ .addr = CHARGING_ENABLE_CMD,
825 	  .mask = CHARGING_ENABLE_CMD_BIT,
826 	  .val = CHARGING_ENABLE_CMD_BIT },
827 	/*
828 	 * Match downstream defaults
829 	 * CHG_EN_SRC_BIT - charger enable is controlled by software
830 	 * CHG_EN_POLARITY_BIT - polarity of charge enable pin when in HW control
831 	 *                       pulled low on OnePlus 6 and SHIFT6mq
832 	 * PRETOFAST_TRANSITION_CFG_BIT -
833 	 * BAT_OV_ECC_BIT -
834 	 * I_TERM_BIT - Current termination ?? 0 = enabled
835 	 * AUTO_RECHG_BIT - Enable automatic recharge when battery is full
836 	 *                  0 = enabled
837 	 * EN_ANALOG_DROP_IN_VBATT_BIT
838 	 * CHARGER_INHIBIT_BIT - Inhibit charging based on battery voltage
839 	 *                       instead of ??
840 	 */
841 	{ .addr = CHGR_CFG2,
842 	  .mask = CHG_EN_SRC_BIT | CHG_EN_POLARITY_BIT |
843 		  PRETOFAST_TRANSITION_CFG_BIT | BAT_OV_ECC_BIT | I_TERM_BIT |
844 		  AUTO_RECHG_BIT | EN_ANALOG_DROP_IN_VBATT_BIT |
845 		  CHARGER_INHIBIT_BIT,
846 	  .val = CHARGER_INHIBIT_BIT },
847 	/* STAT pin software override, match downstream. Parallell charging? */
848 	{ .addr = STAT_CFG,
849 	  .mask = STAT_SW_OVERRIDE_CFG_BIT,
850 	  .val = STAT_SW_OVERRIDE_CFG_BIT },
851 	/* Set the default SDP charger type to a 500ma USB 2.0 port */
852 	{ .addr = USBIN_ICL_OPTIONS,
853 	  .mask = USB51_MODE_BIT | USBIN_MODE_CHG_BIT,
854 	  .val = USB51_MODE_BIT },
855 	/* Disable watchdog */
856 	{ .addr = SNARL_BARK_BITE_WD_CFG, .mask = 0xff, .val = 0 },
857 	{ .addr = WD_CFG,
858 	  .mask = WATCHDOG_TRIGGER_AFP_EN_BIT | WDOG_TIMER_EN_ON_PLUGIN_BIT |
859 		  BARK_WDOG_INT_EN_BIT,
860 	  .val = 0 },
861 	/* These bits aren't documented anywhere */
862 	{ .addr = USBIN_5V_AICL_THRESHOLD_CFG,
863 	  .mask = USBIN_5V_AICL_THRESHOLD_CFG_MASK,
864 	  .val = 0x3 },
865 	{ .addr = USBIN_CONT_AICL_THRESHOLD_CFG,
866 	  .mask = USBIN_CONT_AICL_THRESHOLD_CFG_MASK,
867 	  .val = 0x3 },
868 	/*
869 	 * Enable Automatic Input Current Limit, this will slowly ramp up the current
870 	 * When connected to a wall charger, and automatically stop when it detects
871 	 * the charger current limit (voltage drop?) or it reaches the programmed limit.
872 	 */
873 	{ .addr = USBIN_AICL_OPTIONS_CFG,
874 	  .mask = USBIN_AICL_START_AT_MAX_BIT | USBIN_AICL_ADC_EN_BIT |
875 		  USBIN_AICL_EN_BIT | SUSPEND_ON_COLLAPSE_USBIN_BIT |
876 		  USBIN_HV_COLLAPSE_RESPONSE_BIT |
877 		  USBIN_LV_COLLAPSE_RESPONSE_BIT,
878 	  .val = USBIN_HV_COLLAPSE_RESPONSE_BIT |
879 		 USBIN_LV_COLLAPSE_RESPONSE_BIT | USBIN_AICL_EN_BIT },
880 	/*
881 	 * Set pre charge current to default, the OnePlus 6 bootloader
882 	 * sets this very conservatively.
883 	 */
884 	{ .addr = PRE_CHARGE_CURRENT_CFG,
885 	  .mask = PRE_CHARGE_CURRENT_SETTING_MASK,
886 	  .val = 500000 / CURRENT_SCALE_FACTOR },
887 	/*
888 	 * This overrides all of the current limit options exposed to userspace
889 	 * and prevents the device from pulling more than ~1A. This is done
890 	 * to minimise potential fire hazard risks.
891 	 */
892 	{ .addr = FAST_CHARGE_CURRENT_CFG,
893 	  .mask = FAST_CHARGE_CURRENT_SETTING_MASK,
894 	  .val = 1000000 / CURRENT_SCALE_FACTOR },
895 };
896 
897 static int smb2_init_hw(struct smb2_chip *chip)
898 {
899 	int rc, i;
900 
901 	for (i = 0; i < ARRAY_SIZE(smb2_init_seq); i++) {
902 		dev_dbg(chip->dev, "%d: Writing 0x%02x to 0x%02x\n", i,
903 			smb2_init_seq[i].val, smb2_init_seq[i].addr);
904 		rc = regmap_update_bits(chip->regmap,
905 					chip->base + smb2_init_seq[i].addr,
906 					smb2_init_seq[i].mask,
907 					smb2_init_seq[i].val);
908 		if (rc < 0)
909 			return dev_err_probe(chip->dev, rc,
910 					     "%s: init command %d failed\n",
911 					     __func__, i);
912 	}
913 
914 	return 0;
915 }
916 
917 static int smb2_init_irq(struct smb2_chip *chip, int *irq, const char *name,
918 			 irqreturn_t (*handler)(int irq, void *data))
919 {
920 	int irqnum;
921 	int rc;
922 
923 	irqnum = platform_get_irq_byname(to_platform_device(chip->dev), name);
924 	if (irqnum < 0)
925 		return dev_err_probe(chip->dev, irqnum,
926 				     "Couldn't get irq %s byname\n", name);
927 
928 	rc = devm_request_threaded_irq(chip->dev, irqnum, NULL, handler,
929 				       IRQF_ONESHOT, name, chip);
930 	if (rc < 0)
931 		return dev_err_probe(chip->dev, rc, "Couldn't request irq %s\n",
932 				     name);
933 
934 	if (irq)
935 		*irq = irqnum;
936 
937 	return 0;
938 }
939 
940 static int smb2_probe(struct platform_device *pdev)
941 {
942 	struct power_supply_config supply_config = {};
943 	struct power_supply_desc *desc;
944 	struct smb2_chip *chip;
945 	int rc, irq;
946 
947 	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
948 	if (!chip)
949 		return -ENOMEM;
950 
951 	chip->dev = &pdev->dev;
952 	chip->name = pdev->name;
953 
954 	chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
955 	if (!chip->regmap)
956 		return dev_err_probe(chip->dev, -ENODEV,
957 				     "failed to locate the regmap\n");
958 
959 	rc = device_property_read_u32(chip->dev, "reg", &chip->base);
960 	if (rc < 0)
961 		return dev_err_probe(chip->dev, rc,
962 				     "Couldn't read base address\n");
963 
964 	chip->usb_in_v_chan = devm_iio_channel_get(chip->dev, "usbin_v");
965 	if (IS_ERR(chip->usb_in_v_chan))
966 		return dev_err_probe(chip->dev, PTR_ERR(chip->usb_in_v_chan),
967 				     "Couldn't get usbin_v IIO channel\n");
968 
969 	chip->usb_in_i_chan = devm_iio_channel_get(chip->dev, "usbin_i");
970 	if (IS_ERR(chip->usb_in_i_chan)) {
971 		return dev_err_probe(chip->dev, PTR_ERR(chip->usb_in_i_chan),
972 				     "Couldn't get usbin_i IIO channel\n");
973 	}
974 
975 	rc = smb2_init_hw(chip);
976 	if (rc < 0)
977 		return rc;
978 
979 	supply_config.drv_data = chip;
980 	supply_config.of_node = pdev->dev.of_node;
981 
982 	desc = devm_kzalloc(chip->dev, sizeof(smb2_psy_desc), GFP_KERNEL);
983 	memcpy(desc, &smb2_psy_desc, sizeof(smb2_psy_desc));
984 	desc->name =
985 		devm_kasprintf(chip->dev, GFP_KERNEL, "%s-charger",
986 			       (const char *)device_get_match_data(chip->dev));
987 
988 	chip->chg_psy =
989 		devm_power_supply_register(chip->dev, desc, &supply_config);
990 	if (IS_ERR(chip->chg_psy))
991 		return dev_err_probe(chip->dev, PTR_ERR(chip->chg_psy),
992 				     "failed to register power supply\n");
993 
994 	rc = power_supply_get_battery_info(chip->chg_psy, &chip->batt_info);
995 	if (rc)
996 		return dev_err_probe(chip->dev, rc,
997 				     "Failed to get battery info\n");
998 
999 	rc = devm_delayed_work_autocancel(chip->dev, &chip->status_change_work,
1000 					  smb2_status_change_work);
1001 	if (rc)
1002 		return dev_err_probe(chip->dev, rc,
1003 				     "Failed to init status change work\n");
1004 
1005 	rc = (chip->batt_info->voltage_max_design_uv - 3487500) / 7500 + 1;
1006 	rc = regmap_update_bits(chip->regmap, chip->base + FLOAT_VOLTAGE_CFG,
1007 				FLOAT_VOLTAGE_SETTING_MASK, rc);
1008 	if (rc < 0)
1009 		return dev_err_probe(chip->dev, rc, "Couldn't set vbat max\n");
1010 
1011 	rc = smb2_init_irq(chip, &irq, "bat-ov", smb2_handle_batt_overvoltage);
1012 	if (rc < 0)
1013 		return rc;
1014 
1015 	rc = smb2_init_irq(chip, &chip->cable_irq, "usb-plugin",
1016 			   smb2_handle_usb_plugin);
1017 	if (rc < 0)
1018 		return rc;
1019 
1020 	rc = smb2_init_irq(chip, &irq, "usbin-icl-change",
1021 			   smb2_handle_usb_icl_change);
1022 	if (rc < 0)
1023 		return rc;
1024 	rc = smb2_init_irq(chip, &irq, "wdog-bark", smb2_handle_wdog_bark);
1025 	if (rc < 0)
1026 		return rc;
1027 
1028 	rc = dev_pm_set_wake_irq(chip->dev, chip->cable_irq);
1029 	if (rc < 0)
1030 		return dev_err_probe(chip->dev, rc, "Couldn't set wake irq\n");
1031 
1032 	platform_set_drvdata(pdev, chip);
1033 
1034 	/* Initialise charger state */
1035 	schedule_delayed_work(&chip->status_change_work, 0);
1036 
1037 	return 0;
1038 }
1039 
1040 static const struct of_device_id smb2_match_id_table[] = {
1041 	{ .compatible = "qcom,pmi8998-charger", .data = "pmi8998" },
1042 	{ .compatible = "qcom,pm660-charger", .data = "pm660" },
1043 	{ /* sentinal */ }
1044 };
1045 MODULE_DEVICE_TABLE(of, smb2_match_id_table);
1046 
1047 static struct platform_driver qcom_spmi_smb2 = {
1048 	.probe = smb2_probe,
1049 	.driver = {
1050 		.name = "qcom-pmi8998/pm660-charger",
1051 		.of_match_table = smb2_match_id_table,
1052 		},
1053 };
1054 
1055 module_platform_driver(qcom_spmi_smb2);
1056 
1057 MODULE_AUTHOR("Caleb Connolly <caleb.connolly@linaro.org>");
1058 MODULE_DESCRIPTION("Qualcomm SMB2 Charger Driver");
1059 MODULE_LICENSE("GPL");
1060