1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Summit Microelectronics SMB347 Battery Charger Driver
4  *
5  * Copyright (C) 2011, Intel Corporation
6  *
7  * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com>
8  *          Mika Westerberg <mika.westerberg@linux.intel.com>
9  */
10 
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/gpio.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/power_supply.h>
20 #include <linux/property.h>
21 #include <linux/regmap.h>
22 
23 #include <dt-bindings/power/summit,smb347-charger.h>
24 
25 /* Use the default compensation method */
26 #define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1
27 
28 /* Use default factory programmed value for hard/soft temperature limit */
29 #define SMB3XX_TEMP_USE_DEFAULT		-273
30 
31 /*
32  * Configuration registers. These are mirrored to volatile RAM and can be
33  * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
34  * reloaded from non-volatile registers after POR.
35  */
36 #define CFG_CHARGE_CURRENT			0x00
37 #define CFG_CHARGE_CURRENT_FCC_MASK		0xe0
38 #define CFG_CHARGE_CURRENT_FCC_SHIFT		5
39 #define CFG_CHARGE_CURRENT_PCC_MASK		0x18
40 #define CFG_CHARGE_CURRENT_PCC_SHIFT		3
41 #define CFG_CHARGE_CURRENT_TC_MASK		0x07
42 #define CFG_CURRENT_LIMIT			0x01
43 #define CFG_CURRENT_LIMIT_DC_MASK		0xf0
44 #define CFG_CURRENT_LIMIT_DC_SHIFT		4
45 #define CFG_CURRENT_LIMIT_USB_MASK		0x0f
46 #define CFG_FLOAT_VOLTAGE			0x03
47 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK		0x3f
48 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK	0xc0
49 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT	6
50 #define CFG_STAT				0x05
51 #define CFG_STAT_DISABLED			BIT(5)
52 #define CFG_STAT_ACTIVE_HIGH			BIT(7)
53 #define CFG_PIN					0x06
54 #define CFG_PIN_EN_CTRL_MASK			0x60
55 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH		0x40
56 #define CFG_PIN_EN_CTRL_ACTIVE_LOW		0x60
57 #define CFG_PIN_EN_APSD_IRQ			BIT(1)
58 #define CFG_PIN_EN_CHARGER_ERROR		BIT(2)
59 #define CFG_THERM				0x07
60 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK	0x03
61 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT	0
62 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK	0x0c
63 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT	2
64 #define CFG_THERM_MONITOR_DISABLED		BIT(4)
65 #define CFG_SYSOK				0x08
66 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED	BIT(2)
67 #define CFG_OTHER				0x09
68 #define CFG_OTHER_RID_MASK			0xc0
69 #define CFG_OTHER_RID_ENABLED_AUTO_OTG		0xc0
70 #define CFG_OTG					0x0a
71 #define CFG_OTG_TEMP_THRESHOLD_MASK		0x30
72 #define CFG_OTG_TEMP_THRESHOLD_SHIFT		4
73 #define CFG_OTG_CC_COMPENSATION_MASK		0xc0
74 #define CFG_OTG_CC_COMPENSATION_SHIFT		6
75 #define CFG_TEMP_LIMIT				0x0b
76 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK		0x03
77 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT		0
78 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK		0x0c
79 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT		2
80 #define CFG_TEMP_LIMIT_HARD_HOT_MASK		0x30
81 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT		4
82 #define CFG_TEMP_LIMIT_HARD_COLD_MASK		0xc0
83 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT		6
84 #define CFG_FAULT_IRQ				0x0c
85 #define CFG_FAULT_IRQ_DCIN_UV			BIT(2)
86 #define CFG_STATUS_IRQ				0x0d
87 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER	BIT(4)
88 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT		BIT(7)
89 #define CFG_ADDRESS				0x0e
90 
91 /* Command registers */
92 #define CMD_A					0x30
93 #define CMD_A_CHG_ENABLED			BIT(1)
94 #define CMD_A_SUSPEND_ENABLED			BIT(2)
95 #define CMD_A_ALLOW_WRITE			BIT(7)
96 #define CMD_B					0x31
97 #define CMD_C					0x33
98 
99 /* Interrupt Status registers */
100 #define IRQSTAT_A				0x35
101 #define IRQSTAT_C				0x37
102 #define IRQSTAT_C_TERMINATION_STAT		BIT(0)
103 #define IRQSTAT_C_TERMINATION_IRQ		BIT(1)
104 #define IRQSTAT_C_TAPER_IRQ			BIT(3)
105 #define IRQSTAT_D				0x38
106 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT		BIT(2)
107 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ		BIT(3)
108 #define IRQSTAT_E				0x39
109 #define IRQSTAT_E_USBIN_UV_STAT			BIT(0)
110 #define IRQSTAT_E_USBIN_UV_IRQ			BIT(1)
111 #define IRQSTAT_E_DCIN_UV_STAT			BIT(4)
112 #define IRQSTAT_E_DCIN_UV_IRQ			BIT(5)
113 #define IRQSTAT_F				0x3a
114 
115 /* Status registers */
116 #define STAT_A					0x3b
117 #define STAT_A_FLOAT_VOLTAGE_MASK		0x3f
118 #define STAT_B					0x3c
119 #define STAT_C					0x3d
120 #define STAT_C_CHG_ENABLED			BIT(0)
121 #define STAT_C_HOLDOFF_STAT			BIT(3)
122 #define STAT_C_CHG_MASK				0x06
123 #define STAT_C_CHG_SHIFT			1
124 #define STAT_C_CHG_TERM				BIT(5)
125 #define STAT_C_CHARGER_ERROR			BIT(6)
126 #define STAT_E					0x3f
127 
128 #define SMB347_MAX_REGISTER			0x3f
129 
130 /**
131  * struct smb347_charger - smb347 charger instance
132  * @dev: pointer to device
133  * @regmap: pointer to driver regmap
134  * @mains: power_supply instance for AC/DC power
135  * @usb: power_supply instance for USB power
136  * @id: SMB charger ID
137  * @mains_online: is AC/DC input connected
138  * @usb_online: is USB input connected
139  * @charging_enabled: is charging enabled
140  * @irq_unsupported: is interrupt unsupported by SMB hardware
141  * @max_charge_current: maximum current (in uA) the battery can be charged
142  * @max_charge_voltage: maximum voltage (in uV) the battery can be charged
143  * @pre_charge_current: current (in uA) to use in pre-charging phase
144  * @termination_current: current (in uA) used to determine when the
145  *			 charging cycle terminates
146  * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to
147  *			 pre-charge to fast charge mode
148  * @mains_current_limit: maximum input current drawn from AC/DC input (in uA)
149  * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB
150  *			  input
151  * @chip_temp_threshold: die temperature where device starts limiting charge
152  *			 current [%100 - %130] (in degree C)
153  * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C),
154  *			  granularity is 5 deg C.
155  * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree  C),
156  *			 granularity is 5 deg C.
157  * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C),
158  *			  granularity is 5 deg C.
159  * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C),
160  *			 granularity is 5 deg C.
161  * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit
162  * @soft_temp_limit_compensation: compensation method when soft temperature
163  *				  limit is hit
164  * @charge_current_compensation: current (in uA) for charging compensation
165  *				 current when temperature hits soft limits
166  * @use_mains: AC/DC input can be used
167  * @use_usb: USB input can be used
168  * @use_usb_otg: USB OTG output can be used (not implemented yet)
169  * @enable_control: how charging enable/disable is controlled
170  *		    (driver/pin controls)
171  *
172  * @use_main, @use_usb, and @use_usb_otg are means to enable/disable
173  * hardware support for these. This is useful when we want to have for
174  * example OTG charging controlled via OTG transceiver driver and not by
175  * the SMB347 hardware.
176  *
177  * Hard and soft temperature limit values are given as described in the
178  * device data sheet and assuming NTC beta value is %3750. Even if this is
179  * not the case, these values should be used. They can be mapped to the
180  * corresponding NTC beta values with the help of table %2 in the data
181  * sheet. So for example if NTC beta is %3375 and we want to program hard
182  * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50.
183  *
184  * If zero value is given in any of the current and voltage values, the
185  * factory programmed default will be used. For soft/hard temperature
186  * values, pass in %SMB3XX_TEMP_USE_DEFAULT instead.
187  */
188 struct smb347_charger {
189 	struct device		*dev;
190 	struct regmap		*regmap;
191 	struct power_supply	*mains;
192 	struct power_supply	*usb;
193 	unsigned int		id;
194 	bool			mains_online;
195 	bool			usb_online;
196 	bool			charging_enabled;
197 	bool			irq_unsupported;
198 
199 	unsigned int		max_charge_current;
200 	unsigned int		max_charge_voltage;
201 	unsigned int		pre_charge_current;
202 	unsigned int		termination_current;
203 	unsigned int		pre_to_fast_voltage;
204 	unsigned int		mains_current_limit;
205 	unsigned int		usb_hc_current_limit;
206 	unsigned int		chip_temp_threshold;
207 	int			soft_cold_temp_limit;
208 	int			soft_hot_temp_limit;
209 	int			hard_cold_temp_limit;
210 	int			hard_hot_temp_limit;
211 	bool			suspend_on_hard_temp_limit;
212 	unsigned int		soft_temp_limit_compensation;
213 	unsigned int		charge_current_compensation;
214 	bool			use_mains;
215 	bool			use_usb;
216 	bool			use_usb_otg;
217 	unsigned int		enable_control;
218 };
219 
220 enum smb_charger_chipid {
221 	SMB345,
222 	SMB347,
223 	SMB358,
224 	NUM_CHIP_TYPES,
225 };
226 
227 /* Fast charge current in uA */
228 static const unsigned int fcc_tbl[NUM_CHIP_TYPES][8] = {
229 	[SMB345] = {  200000,  450000,  600000,  900000,
230 		     1300000, 1500000, 1800000, 2000000 },
231 	[SMB347] = {  700000,  900000, 1200000, 1500000,
232 		     1800000, 2000000, 2200000, 2500000 },
233 	[SMB358] = {  200000,  450000,  600000,  900000,
234 		     1300000, 1500000, 1800000, 2000000 },
235 };
236 /* Pre-charge current in uA */
237 static const unsigned int pcc_tbl[NUM_CHIP_TYPES][4] = {
238 	[SMB345] = { 150000, 250000, 350000, 450000 },
239 	[SMB347] = { 100000, 150000, 200000, 250000 },
240 	[SMB358] = { 150000, 250000, 350000, 450000 },
241 };
242 
243 /* Termination current in uA */
244 static const unsigned int tc_tbl[NUM_CHIP_TYPES][8] = {
245 	[SMB345] = {  30000,  40000,  60000,  80000,
246 		     100000, 125000, 150000, 200000 },
247 	[SMB347] = {  37500,  50000, 100000, 150000,
248 		     200000, 250000, 500000, 600000 },
249 	[SMB358] = {  30000,  40000,  60000,  80000,
250 		     100000, 125000, 150000, 200000 },
251 };
252 
253 /* Input current limit in uA */
254 static const unsigned int icl_tbl[NUM_CHIP_TYPES][10] = {
255 	[SMB345] = {  300000,  500000,  700000, 1000000, 1500000,
256 		     1800000, 2000000, 2000000, 2000000, 2000000 },
257 	[SMB347] = {  300000,  500000,  700000,  900000, 1200000,
258 		     1500000, 1800000, 2000000, 2200000, 2500000 },
259 	[SMB358] = {  300000,  500000,  700000, 1000000, 1500000,
260 		     1800000, 2000000, 2000000, 2000000, 2000000 },
261 };
262 
263 /* Charge current compensation in uA */
264 static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = {
265 	[SMB345] = {  200000,  450000,  600000,  900000 },
266 	[SMB347] = {  250000,  700000,  900000, 1200000 },
267 	[SMB358] = {  200000,  450000,  600000,  900000 },
268 };
269 
270 /* Convert register value to current using lookup table */
271 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
272 {
273 	if (val >= size)
274 		return -EINVAL;
275 	return tbl[val];
276 }
277 
278 /* Convert current to register value using lookup table */
279 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
280 {
281 	size_t i;
282 
283 	for (i = 0; i < size; i++)
284 		if (val < tbl[i])
285 			break;
286 	return i > 0 ? i - 1 : -EINVAL;
287 }
288 
289 /**
290  * smb347_update_ps_status - refreshes the power source status
291  * @smb: pointer to smb347 charger instance
292  *
293  * Function checks whether any power source is connected to the charger and
294  * updates internal state accordingly. If there is a change to previous state
295  * function returns %1, otherwise %0 and negative errno in case of errror.
296  */
297 static int smb347_update_ps_status(struct smb347_charger *smb)
298 {
299 	bool usb = false;
300 	bool dc = false;
301 	unsigned int val;
302 	int ret;
303 
304 	ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
305 	if (ret < 0)
306 		return ret;
307 
308 	/*
309 	 * Dc and usb are set depending on whether they are enabled in
310 	 * platform data _and_ whether corresponding undervoltage is set.
311 	 */
312 	if (smb->use_mains)
313 		dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
314 	if (smb->use_usb)
315 		usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
316 
317 	ret = smb->mains_online != dc || smb->usb_online != usb;
318 	smb->mains_online = dc;
319 	smb->usb_online = usb;
320 
321 	return ret;
322 }
323 
324 /*
325  * smb347_is_ps_online - returns whether input power source is connected
326  * @smb: pointer to smb347 charger instance
327  *
328  * Returns %true if input power source is connected. Note that this is
329  * dependent on what platform has configured for usable power sources. For
330  * example if USB is disabled, this will return %false even if the USB cable
331  * is connected.
332  */
333 static bool smb347_is_ps_online(struct smb347_charger *smb)
334 {
335 	return smb->usb_online || smb->mains_online;
336 }
337 
338 /**
339  * smb347_charging_status - returns status of charging
340  * @smb: pointer to smb347 charger instance
341  *
342  * Function returns charging status. %0 means no charging is in progress,
343  * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
344  */
345 static int smb347_charging_status(struct smb347_charger *smb)
346 {
347 	unsigned int val;
348 	int ret;
349 
350 	if (!smb347_is_ps_online(smb))
351 		return 0;
352 
353 	ret = regmap_read(smb->regmap, STAT_C, &val);
354 	if (ret < 0)
355 		return 0;
356 
357 	return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
358 }
359 
360 static int smb347_charging_set(struct smb347_charger *smb, bool enable)
361 {
362 	int ret = 0;
363 
364 	if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) {
365 		dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
366 		return 0;
367 	}
368 
369 	if (smb->charging_enabled != enable) {
370 		ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
371 					 enable ? CMD_A_CHG_ENABLED : 0);
372 		if (!ret)
373 			smb->charging_enabled = enable;
374 	}
375 
376 	return ret;
377 }
378 
379 static inline int smb347_charging_enable(struct smb347_charger *smb)
380 {
381 	return smb347_charging_set(smb, true);
382 }
383 
384 static inline int smb347_charging_disable(struct smb347_charger *smb)
385 {
386 	return smb347_charging_set(smb, false);
387 }
388 
389 static int smb347_start_stop_charging(struct smb347_charger *smb)
390 {
391 	int ret;
392 
393 	/*
394 	 * Depending on whether valid power source is connected or not, we
395 	 * disable or enable the charging. We do it manually because it
396 	 * depends on how the platform has configured the valid inputs.
397 	 */
398 	if (smb347_is_ps_online(smb)) {
399 		ret = smb347_charging_enable(smb);
400 		if (ret < 0)
401 			dev_err(smb->dev, "failed to enable charging\n");
402 	} else {
403 		ret = smb347_charging_disable(smb);
404 		if (ret < 0)
405 			dev_err(smb->dev, "failed to disable charging\n");
406 	}
407 
408 	return ret;
409 }
410 
411 static int smb347_set_charge_current(struct smb347_charger *smb)
412 {
413 	unsigned int id = smb->id;
414 	int ret;
415 
416 	if (smb->max_charge_current) {
417 		ret = current_to_hw(fcc_tbl[id], ARRAY_SIZE(fcc_tbl[id]),
418 				    smb->max_charge_current);
419 		if (ret < 0)
420 			return ret;
421 
422 		ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
423 					 CFG_CHARGE_CURRENT_FCC_MASK,
424 					 ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
425 		if (ret < 0)
426 			return ret;
427 	}
428 
429 	if (smb->pre_charge_current) {
430 		ret = current_to_hw(pcc_tbl[id], ARRAY_SIZE(pcc_tbl[id]),
431 				    smb->pre_charge_current);
432 		if (ret < 0)
433 			return ret;
434 
435 		ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
436 					 CFG_CHARGE_CURRENT_PCC_MASK,
437 					 ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
438 		if (ret < 0)
439 			return ret;
440 	}
441 
442 	if (smb->termination_current) {
443 		ret = current_to_hw(tc_tbl[id], ARRAY_SIZE(tc_tbl[id]),
444 				    smb->termination_current);
445 		if (ret < 0)
446 			return ret;
447 
448 		ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
449 					 CFG_CHARGE_CURRENT_TC_MASK, ret);
450 		if (ret < 0)
451 			return ret;
452 	}
453 
454 	return 0;
455 }
456 
457 static int smb347_set_current_limits(struct smb347_charger *smb)
458 {
459 	unsigned int id = smb->id;
460 	int ret;
461 
462 	if (smb->mains_current_limit) {
463 		ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
464 				    smb->mains_current_limit);
465 		if (ret < 0)
466 			return ret;
467 
468 		ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
469 					 CFG_CURRENT_LIMIT_DC_MASK,
470 					 ret << CFG_CURRENT_LIMIT_DC_SHIFT);
471 		if (ret < 0)
472 			return ret;
473 	}
474 
475 	if (smb->usb_hc_current_limit) {
476 		ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
477 				    smb->usb_hc_current_limit);
478 		if (ret < 0)
479 			return ret;
480 
481 		ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
482 					 CFG_CURRENT_LIMIT_USB_MASK, ret);
483 		if (ret < 0)
484 			return ret;
485 	}
486 
487 	return 0;
488 }
489 
490 static int smb347_set_voltage_limits(struct smb347_charger *smb)
491 {
492 	int ret;
493 
494 	if (smb->pre_to_fast_voltage) {
495 		ret = smb->pre_to_fast_voltage;
496 
497 		/* uV */
498 		ret = clamp_val(ret, 2400000, 3000000) - 2400000;
499 		ret /= 200000;
500 
501 		ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
502 				CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
503 				ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
504 		if (ret < 0)
505 			return ret;
506 	}
507 
508 	if (smb->max_charge_voltage) {
509 		ret = smb->max_charge_voltage;
510 
511 		/* uV */
512 		ret = clamp_val(ret, 3500000, 4500000) - 3500000;
513 		ret /= 20000;
514 
515 		ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
516 					 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
517 		if (ret < 0)
518 			return ret;
519 	}
520 
521 	return 0;
522 }
523 
524 static int smb347_set_temp_limits(struct smb347_charger *smb)
525 {
526 	unsigned int id = smb->id;
527 	bool enable_therm_monitor = false;
528 	int ret = 0;
529 	int val;
530 
531 	if (smb->chip_temp_threshold) {
532 		val = smb->chip_temp_threshold;
533 
534 		/* degree C */
535 		val = clamp_val(val, 100, 130) - 100;
536 		val /= 10;
537 
538 		ret = regmap_update_bits(smb->regmap, CFG_OTG,
539 					 CFG_OTG_TEMP_THRESHOLD_MASK,
540 					 val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
541 		if (ret < 0)
542 			return ret;
543 	}
544 
545 	if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
546 		val = smb->soft_cold_temp_limit;
547 
548 		val = clamp_val(val, 0, 15);
549 		val /= 5;
550 		/* this goes from higher to lower so invert the value */
551 		val = ~val & 0x3;
552 
553 		ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
554 					 CFG_TEMP_LIMIT_SOFT_COLD_MASK,
555 					 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
556 		if (ret < 0)
557 			return ret;
558 
559 		enable_therm_monitor = true;
560 	}
561 
562 	if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
563 		val = smb->soft_hot_temp_limit;
564 
565 		val = clamp_val(val, 40, 55) - 40;
566 		val /= 5;
567 
568 		ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
569 					 CFG_TEMP_LIMIT_SOFT_HOT_MASK,
570 					 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
571 		if (ret < 0)
572 			return ret;
573 
574 		enable_therm_monitor = true;
575 	}
576 
577 	if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
578 		val = smb->hard_cold_temp_limit;
579 
580 		val = clamp_val(val, -5, 10) + 5;
581 		val /= 5;
582 		/* this goes from higher to lower so invert the value */
583 		val = ~val & 0x3;
584 
585 		ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
586 					 CFG_TEMP_LIMIT_HARD_COLD_MASK,
587 					 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
588 		if (ret < 0)
589 			return ret;
590 
591 		enable_therm_monitor = true;
592 	}
593 
594 	if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
595 		val = smb->hard_hot_temp_limit;
596 
597 		val = clamp_val(val, 50, 65) - 50;
598 		val /= 5;
599 
600 		ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
601 					 CFG_TEMP_LIMIT_HARD_HOT_MASK,
602 					 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
603 		if (ret < 0)
604 			return ret;
605 
606 		enable_therm_monitor = true;
607 	}
608 
609 	/*
610 	 * If any of the temperature limits are set, we also enable the
611 	 * thermistor monitoring.
612 	 *
613 	 * When soft limits are hit, the device will start to compensate
614 	 * current and/or voltage depending on the configuration.
615 	 *
616 	 * When hard limit is hit, the device will suspend charging
617 	 * depending on the configuration.
618 	 */
619 	if (enable_therm_monitor) {
620 		ret = regmap_update_bits(smb->regmap, CFG_THERM,
621 					 CFG_THERM_MONITOR_DISABLED, 0);
622 		if (ret < 0)
623 			return ret;
624 	}
625 
626 	if (smb->suspend_on_hard_temp_limit) {
627 		ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
628 				 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
629 		if (ret < 0)
630 			return ret;
631 	}
632 
633 	if (smb->soft_temp_limit_compensation !=
634 	    SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT) {
635 		val = smb->soft_temp_limit_compensation & 0x3;
636 
637 		ret = regmap_update_bits(smb->regmap, CFG_THERM,
638 				 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
639 				 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
640 		if (ret < 0)
641 			return ret;
642 
643 		ret = regmap_update_bits(smb->regmap, CFG_THERM,
644 				 CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
645 				 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
646 		if (ret < 0)
647 			return ret;
648 	}
649 
650 	if (smb->charge_current_compensation) {
651 		val = current_to_hw(ccc_tbl[id], ARRAY_SIZE(ccc_tbl[id]),
652 				    smb->charge_current_compensation);
653 		if (val < 0)
654 			return val;
655 
656 		ret = regmap_update_bits(smb->regmap, CFG_OTG,
657 				CFG_OTG_CC_COMPENSATION_MASK,
658 				(val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
659 		if (ret < 0)
660 			return ret;
661 	}
662 
663 	return ret;
664 }
665 
666 /*
667  * smb347_set_writable - enables/disables writing to non-volatile registers
668  * @smb: pointer to smb347 charger instance
669  *
670  * You can enable/disable writing to the non-volatile configuration
671  * registers by calling this function.
672  *
673  * Returns %0 on success and negative errno in case of failure.
674  */
675 static int smb347_set_writable(struct smb347_charger *smb, bool writable)
676 {
677 	return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
678 				  writable ? CMD_A_ALLOW_WRITE : 0);
679 }
680 
681 static int smb347_hw_init(struct smb347_charger *smb)
682 {
683 	unsigned int val;
684 	int ret;
685 
686 	ret = smb347_set_writable(smb, true);
687 	if (ret < 0)
688 		return ret;
689 
690 	/*
691 	 * Program the platform specific configuration values to the device
692 	 * first.
693 	 */
694 	ret = smb347_set_charge_current(smb);
695 	if (ret < 0)
696 		goto fail;
697 
698 	ret = smb347_set_current_limits(smb);
699 	if (ret < 0)
700 		goto fail;
701 
702 	ret = smb347_set_voltage_limits(smb);
703 	if (ret < 0)
704 		goto fail;
705 
706 	ret = smb347_set_temp_limits(smb);
707 	if (ret < 0)
708 		goto fail;
709 
710 	/* If USB charging is disabled we put the USB in suspend mode */
711 	if (!smb->use_usb) {
712 		ret = regmap_update_bits(smb->regmap, CMD_A,
713 					 CMD_A_SUSPEND_ENABLED,
714 					 CMD_A_SUSPEND_ENABLED);
715 		if (ret < 0)
716 			goto fail;
717 	}
718 
719 	/*
720 	 * If configured by platform data, we enable hardware Auto-OTG
721 	 * support for driving VBUS. Otherwise we disable it.
722 	 */
723 	ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
724 		smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
725 	if (ret < 0)
726 		goto fail;
727 
728 	/*
729 	 * Make the charging functionality controllable by a write to the
730 	 * command register unless pin control is specified in the platform
731 	 * data.
732 	 */
733 	switch (smb->enable_control) {
734 	case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
735 		val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
736 		break;
737 	case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
738 		val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
739 		break;
740 	default:
741 		val = 0;
742 		break;
743 	}
744 
745 	ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
746 				 val);
747 	if (ret < 0)
748 		goto fail;
749 
750 	/* Disable Automatic Power Source Detection (APSD) interrupt. */
751 	ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
752 	if (ret < 0)
753 		goto fail;
754 
755 	ret = smb347_update_ps_status(smb);
756 	if (ret < 0)
757 		goto fail;
758 
759 	ret = smb347_start_stop_charging(smb);
760 
761 fail:
762 	smb347_set_writable(smb, false);
763 	return ret;
764 }
765 
766 static irqreturn_t smb347_interrupt(int irq, void *data)
767 {
768 	struct smb347_charger *smb = data;
769 	unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
770 	bool handled = false;
771 	int ret;
772 
773 	/* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */
774 	usleep_range(25000, 35000);
775 
776 	ret = regmap_read(smb->regmap, STAT_C, &stat_c);
777 	if (ret < 0) {
778 		dev_warn(smb->dev, "reading STAT_C failed\n");
779 		return IRQ_NONE;
780 	}
781 
782 	ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
783 	if (ret < 0) {
784 		dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
785 		return IRQ_NONE;
786 	}
787 
788 	ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d);
789 	if (ret < 0) {
790 		dev_warn(smb->dev, "reading IRQSTAT_D failed\n");
791 		return IRQ_NONE;
792 	}
793 
794 	ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
795 	if (ret < 0) {
796 		dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
797 		return IRQ_NONE;
798 	}
799 
800 	/*
801 	 * If we get charger error we report the error back to user.
802 	 * If the error is recovered charging will resume again.
803 	 */
804 	if (stat_c & STAT_C_CHARGER_ERROR) {
805 		dev_err(smb->dev, "charging stopped due to charger error\n");
806 		if (smb->use_mains)
807 			power_supply_changed(smb->mains);
808 		if (smb->use_usb)
809 			power_supply_changed(smb->usb);
810 		handled = true;
811 	}
812 
813 	/*
814 	 * If we reached the termination current the battery is charged and
815 	 * we can update the status now. Charging is automatically
816 	 * disabled by the hardware.
817 	 */
818 	if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
819 		if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) {
820 			if (smb->use_mains)
821 				power_supply_changed(smb->mains);
822 			if (smb->use_usb)
823 				power_supply_changed(smb->usb);
824 		}
825 		dev_dbg(smb->dev, "going to HW maintenance mode\n");
826 		handled = true;
827 	}
828 
829 	/*
830 	 * If we got a charger timeout INT that means the charge
831 	 * full is not detected with in charge timeout value.
832 	 */
833 	if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) {
834 		dev_dbg(smb->dev, "total Charge Timeout INT received\n");
835 
836 		if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
837 			dev_warn(smb->dev, "charging stopped due to timeout\n");
838 		if (smb->use_mains)
839 			power_supply_changed(smb->mains);
840 		if (smb->use_usb)
841 			power_supply_changed(smb->usb);
842 		handled = true;
843 	}
844 
845 	/*
846 	 * If we got an under voltage interrupt it means that AC/USB input
847 	 * was connected or disconnected.
848 	 */
849 	if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
850 		if (smb347_update_ps_status(smb) > 0) {
851 			smb347_start_stop_charging(smb);
852 			if (smb->use_mains)
853 				power_supply_changed(smb->mains);
854 			if (smb->use_usb)
855 				power_supply_changed(smb->usb);
856 		}
857 		handled = true;
858 	}
859 
860 	return handled ? IRQ_HANDLED : IRQ_NONE;
861 }
862 
863 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
864 {
865 	int ret;
866 
867 	if (smb->irq_unsupported)
868 		return 0;
869 
870 	ret = smb347_set_writable(smb, true);
871 	if (ret < 0)
872 		return ret;
873 
874 	/*
875 	 * Enable/disable interrupts for:
876 	 *	- under voltage
877 	 *	- termination current reached
878 	 *	- charger timeout
879 	 *	- charger error
880 	 */
881 	ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
882 				 enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
883 	if (ret < 0)
884 		goto fail;
885 
886 	ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
887 			enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER |
888 					CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0);
889 	if (ret < 0)
890 		goto fail;
891 
892 	ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
893 				 enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
894 fail:
895 	smb347_set_writable(smb, false);
896 	return ret;
897 }
898 
899 static inline int smb347_irq_enable(struct smb347_charger *smb)
900 {
901 	return smb347_irq_set(smb, true);
902 }
903 
904 static inline int smb347_irq_disable(struct smb347_charger *smb)
905 {
906 	return smb347_irq_set(smb, false);
907 }
908 
909 static int smb347_irq_init(struct smb347_charger *smb,
910 			   struct i2c_client *client)
911 {
912 	int ret;
913 
914 	ret = devm_request_threaded_irq(smb->dev, client->irq, NULL,
915 					smb347_interrupt, IRQF_ONESHOT,
916 					client->name, smb);
917 	if (ret < 0)
918 		return ret;
919 
920 	ret = smb347_set_writable(smb, true);
921 	if (ret < 0)
922 		return ret;
923 
924 	/*
925 	 * Configure the STAT output to be suitable for interrupts: disable
926 	 * all other output (except interrupts) and make it active low.
927 	 */
928 	ret = regmap_update_bits(smb->regmap, CFG_STAT,
929 				 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
930 				 CFG_STAT_DISABLED);
931 
932 	smb347_set_writable(smb, false);
933 
934 	return ret;
935 }
936 
937 /*
938  * Returns the constant charge current programmed
939  * into the charger in uA.
940  */
941 static int get_const_charge_current(struct smb347_charger *smb)
942 {
943 	unsigned int id = smb->id;
944 	int ret, intval;
945 	unsigned int v;
946 
947 	if (!smb347_is_ps_online(smb))
948 		return -ENODATA;
949 
950 	ret = regmap_read(smb->regmap, STAT_B, &v);
951 	if (ret < 0)
952 		return ret;
953 
954 	/*
955 	 * The current value is composition of FCC and PCC values
956 	 * and we can detect which table to use from bit 5.
957 	 */
958 	if (v & 0x20) {
959 		intval = hw_to_current(fcc_tbl[id],
960 				       ARRAY_SIZE(fcc_tbl[id]), v & 7);
961 	} else {
962 		v >>= 3;
963 		intval = hw_to_current(pcc_tbl[id],
964 				       ARRAY_SIZE(pcc_tbl[id]), v & 7);
965 	}
966 
967 	return intval;
968 }
969 
970 /*
971  * Returns the constant charge voltage programmed
972  * into the charger in uV.
973  */
974 static int get_const_charge_voltage(struct smb347_charger *smb)
975 {
976 	int ret, intval;
977 	unsigned int v;
978 
979 	if (!smb347_is_ps_online(smb))
980 		return -ENODATA;
981 
982 	ret = regmap_read(smb->regmap, STAT_A, &v);
983 	if (ret < 0)
984 		return ret;
985 
986 	v &= STAT_A_FLOAT_VOLTAGE_MASK;
987 	if (v > 0x3d)
988 		v = 0x3d;
989 
990 	intval = 3500000 + v * 20000;
991 
992 	return intval;
993 }
994 
995 static int smb347_get_charging_status(struct smb347_charger *smb,
996 				      struct power_supply *psy)
997 {
998 	int ret, status;
999 	unsigned int val;
1000 
1001 	if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1002 		if (!smb->usb_online)
1003 			return POWER_SUPPLY_STATUS_DISCHARGING;
1004 	} else {
1005 		if (!smb->mains_online)
1006 			return POWER_SUPPLY_STATUS_DISCHARGING;
1007 	}
1008 
1009 	ret = regmap_read(smb->regmap, STAT_C, &val);
1010 	if (ret < 0)
1011 		return ret;
1012 
1013 	if ((val & STAT_C_CHARGER_ERROR) ||
1014 			(val & STAT_C_HOLDOFF_STAT)) {
1015 		/*
1016 		 * set to NOT CHARGING upon charger error
1017 		 * or charging has stopped.
1018 		 */
1019 		status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1020 	} else {
1021 		if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
1022 			/*
1023 			 * set to charging if battery is in pre-charge,
1024 			 * fast charge or taper charging mode.
1025 			 */
1026 			status = POWER_SUPPLY_STATUS_CHARGING;
1027 		} else if (val & STAT_C_CHG_TERM) {
1028 			/*
1029 			 * set the status to FULL if battery is not in pre
1030 			 * charge, fast charge or taper charging mode AND
1031 			 * charging is terminated at least once.
1032 			 */
1033 			status = POWER_SUPPLY_STATUS_FULL;
1034 		} else {
1035 			/*
1036 			 * in this case no charger error or termination
1037 			 * occured but charging is not in progress!!!
1038 			 */
1039 			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1040 		}
1041 	}
1042 
1043 	return status;
1044 }
1045 
1046 static int smb347_get_property_locked(struct power_supply *psy,
1047 				      enum power_supply_property prop,
1048 				      union power_supply_propval *val)
1049 {
1050 	struct smb347_charger *smb = power_supply_get_drvdata(psy);
1051 	int ret;
1052 
1053 	switch (prop) {
1054 	case POWER_SUPPLY_PROP_STATUS:
1055 		ret = smb347_get_charging_status(smb, psy);
1056 		if (ret < 0)
1057 			return ret;
1058 		val->intval = ret;
1059 		break;
1060 
1061 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
1062 		if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1063 			if (!smb->usb_online)
1064 				return -ENODATA;
1065 		} else {
1066 			if (!smb->mains_online)
1067 				return -ENODATA;
1068 		}
1069 
1070 		/*
1071 		 * We handle trickle and pre-charging the same, and taper
1072 		 * and none the same.
1073 		 */
1074 		switch (smb347_charging_status(smb)) {
1075 		case 1:
1076 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1077 			break;
1078 		case 2:
1079 			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1080 			break;
1081 		default:
1082 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1083 			break;
1084 		}
1085 		break;
1086 
1087 	case POWER_SUPPLY_PROP_ONLINE:
1088 		if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
1089 			val->intval = smb->usb_online;
1090 		else
1091 			val->intval = smb->mains_online;
1092 		break;
1093 
1094 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1095 		ret = get_const_charge_voltage(smb);
1096 		if (ret < 0)
1097 			return ret;
1098 		val->intval = ret;
1099 		break;
1100 
1101 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1102 		ret = get_const_charge_current(smb);
1103 		if (ret < 0)
1104 			return ret;
1105 		val->intval = ret;
1106 		break;
1107 
1108 	default:
1109 		return -EINVAL;
1110 	}
1111 
1112 	return 0;
1113 }
1114 
1115 static int smb347_get_property(struct power_supply *psy,
1116 			       enum power_supply_property prop,
1117 			       union power_supply_propval *val)
1118 {
1119 	struct smb347_charger *smb = power_supply_get_drvdata(psy);
1120 	struct i2c_client *client = to_i2c_client(smb->dev);
1121 	int ret;
1122 
1123 	disable_irq(client->irq);
1124 	ret = smb347_get_property_locked(psy, prop, val);
1125 	enable_irq(client->irq);
1126 
1127 	return ret;
1128 }
1129 
1130 static enum power_supply_property smb347_properties[] = {
1131 	POWER_SUPPLY_PROP_STATUS,
1132 	POWER_SUPPLY_PROP_CHARGE_TYPE,
1133 	POWER_SUPPLY_PROP_ONLINE,
1134 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1135 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1136 };
1137 
1138 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1139 {
1140 	switch (reg) {
1141 	case IRQSTAT_A:
1142 	case IRQSTAT_C:
1143 	case IRQSTAT_D:
1144 	case IRQSTAT_E:
1145 	case IRQSTAT_F:
1146 	case STAT_A:
1147 	case STAT_B:
1148 	case STAT_C:
1149 	case STAT_E:
1150 		return true;
1151 	}
1152 
1153 	return false;
1154 }
1155 
1156 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1157 {
1158 	switch (reg) {
1159 	case CFG_CHARGE_CURRENT:
1160 	case CFG_CURRENT_LIMIT:
1161 	case CFG_FLOAT_VOLTAGE:
1162 	case CFG_STAT:
1163 	case CFG_PIN:
1164 	case CFG_THERM:
1165 	case CFG_SYSOK:
1166 	case CFG_OTHER:
1167 	case CFG_OTG:
1168 	case CFG_TEMP_LIMIT:
1169 	case CFG_FAULT_IRQ:
1170 	case CFG_STATUS_IRQ:
1171 	case CFG_ADDRESS:
1172 	case CMD_A:
1173 	case CMD_B:
1174 	case CMD_C:
1175 		return true;
1176 	}
1177 
1178 	return smb347_volatile_reg(dev, reg);
1179 }
1180 
1181 static void smb347_dt_parse_dev_info(struct smb347_charger *smb)
1182 {
1183 	struct device *dev = smb->dev;
1184 
1185 	smb->soft_temp_limit_compensation =
1186 					SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT;
1187 	/*
1188 	 * These properties come from the battery info, still we need to
1189 	 * pre-initialize the values. See smb347_get_battery_info() below.
1190 	 */
1191 	smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1192 	smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1193 	smb->soft_hot_temp_limit  = SMB3XX_TEMP_USE_DEFAULT;
1194 	smb->hard_hot_temp_limit  = SMB3XX_TEMP_USE_DEFAULT;
1195 
1196 	/* Charging constraints */
1197 	device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt",
1198 				 &smb->pre_to_fast_voltage);
1199 	device_property_read_u32(dev, "summit,mains-current-limit-microamp",
1200 				 &smb->mains_current_limit);
1201 	device_property_read_u32(dev, "summit,usb-current-limit-microamp",
1202 				 &smb->usb_hc_current_limit);
1203 
1204 	/* For thermometer monitoring */
1205 	device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius",
1206 				 &smb->chip_temp_threshold);
1207 	device_property_read_u32(dev, "summit,soft-compensation-method",
1208 				 &smb->soft_temp_limit_compensation);
1209 	device_property_read_u32(dev, "summit,charge-current-compensation-microamp",
1210 				 &smb->charge_current_compensation);
1211 
1212 	/* Supported charging mode */
1213 	smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging");
1214 	smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging");
1215 	smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging");
1216 
1217 	/* Select charging control */
1218 	device_property_read_u32(dev, "summit,enable-charge-control",
1219 				 &smb->enable_control);
1220 }
1221 
1222 static int smb347_get_battery_info(struct smb347_charger *smb)
1223 {
1224 	struct power_supply_battery_info info = {};
1225 	struct power_supply *supply;
1226 	int err;
1227 
1228 	if (smb->mains)
1229 		supply = smb->mains;
1230 	else
1231 		supply = smb->usb;
1232 
1233 	err = power_supply_get_battery_info(supply, &info);
1234 	if (err == -ENXIO || err == -ENODEV)
1235 		return 0;
1236 	if (err)
1237 		return err;
1238 
1239 	if (info.constant_charge_current_max_ua != -EINVAL)
1240 		smb->max_charge_current = info.constant_charge_current_max_ua;
1241 
1242 	if (info.constant_charge_voltage_max_uv != -EINVAL)
1243 		smb->max_charge_voltage = info.constant_charge_voltage_max_uv;
1244 
1245 	if (info.precharge_current_ua != -EINVAL)
1246 		smb->pre_charge_current = info.precharge_current_ua;
1247 
1248 	if (info.charge_term_current_ua != -EINVAL)
1249 		smb->termination_current = info.charge_term_current_ua;
1250 
1251 	if (info.temp_alert_min != INT_MIN)
1252 		smb->soft_cold_temp_limit = info.temp_alert_min;
1253 
1254 	if (info.temp_alert_max != INT_MAX)
1255 		smb->soft_hot_temp_limit = info.temp_alert_max;
1256 
1257 	if (info.temp_min != INT_MIN)
1258 		smb->hard_cold_temp_limit = info.temp_min;
1259 
1260 	if (info.temp_max != INT_MAX)
1261 		smb->hard_hot_temp_limit = info.temp_max;
1262 
1263 	/* Suspend when battery temperature is outside hard limits */
1264 	if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT ||
1265 	    smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT)
1266 		smb->suspend_on_hard_temp_limit = true;
1267 
1268 	return 0;
1269 }
1270 
1271 static const struct regmap_config smb347_regmap = {
1272 	.reg_bits	= 8,
1273 	.val_bits	= 8,
1274 	.max_register	= SMB347_MAX_REGISTER,
1275 	.volatile_reg	= smb347_volatile_reg,
1276 	.readable_reg	= smb347_readable_reg,
1277 };
1278 
1279 static const struct power_supply_desc smb347_mains_desc = {
1280 	.name		= "smb347-mains",
1281 	.type		= POWER_SUPPLY_TYPE_MAINS,
1282 	.get_property	= smb347_get_property,
1283 	.properties	= smb347_properties,
1284 	.num_properties	= ARRAY_SIZE(smb347_properties),
1285 };
1286 
1287 static const struct power_supply_desc smb347_usb_desc = {
1288 	.name		= "smb347-usb",
1289 	.type		= POWER_SUPPLY_TYPE_USB,
1290 	.get_property	= smb347_get_property,
1291 	.properties	= smb347_properties,
1292 	.num_properties	= ARRAY_SIZE(smb347_properties),
1293 };
1294 
1295 static int smb347_probe(struct i2c_client *client,
1296 			const struct i2c_device_id *id)
1297 {
1298 	struct power_supply_config mains_usb_cfg = {};
1299 	struct device *dev = &client->dev;
1300 	struct smb347_charger *smb;
1301 	int ret;
1302 
1303 	smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1304 	if (!smb)
1305 		return -ENOMEM;
1306 	smb->dev = &client->dev;
1307 	smb->id = id->driver_data;
1308 	i2c_set_clientdata(client, smb);
1309 
1310 	smb347_dt_parse_dev_info(smb);
1311 	if (!smb->use_mains && !smb->use_usb)
1312 		return -EINVAL;
1313 
1314 	smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1315 	if (IS_ERR(smb->regmap))
1316 		return PTR_ERR(smb->regmap);
1317 
1318 	mains_usb_cfg.drv_data = smb;
1319 	mains_usb_cfg.of_node = dev->of_node;
1320 	if (smb->use_mains) {
1321 		smb->mains = devm_power_supply_register(dev, &smb347_mains_desc,
1322 							&mains_usb_cfg);
1323 		if (IS_ERR(smb->mains))
1324 			return PTR_ERR(smb->mains);
1325 	}
1326 
1327 	if (smb->use_usb) {
1328 		smb->usb = devm_power_supply_register(dev, &smb347_usb_desc,
1329 						      &mains_usb_cfg);
1330 		if (IS_ERR(smb->usb))
1331 			return PTR_ERR(smb->usb);
1332 	}
1333 
1334 	ret = smb347_get_battery_info(smb);
1335 	if (ret)
1336 		return ret;
1337 
1338 	ret = smb347_hw_init(smb);
1339 	if (ret < 0)
1340 		return ret;
1341 
1342 	/*
1343 	 * Interrupt pin is optional. If it is connected, we setup the
1344 	 * interrupt support here.
1345 	 */
1346 	if (client->irq) {
1347 		ret = smb347_irq_init(smb, client);
1348 		if (ret < 0) {
1349 			dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
1350 			dev_warn(dev, "disabling IRQ support\n");
1351 			smb->irq_unsupported = true;
1352 		} else {
1353 			smb347_irq_enable(smb);
1354 		}
1355 	}
1356 
1357 	return 0;
1358 }
1359 
1360 static int smb347_remove(struct i2c_client *client)
1361 {
1362 	struct smb347_charger *smb = i2c_get_clientdata(client);
1363 
1364 	smb347_irq_disable(smb);
1365 
1366 	return 0;
1367 }
1368 
1369 static const struct i2c_device_id smb347_id[] = {
1370 	{ "smb345", SMB345 },
1371 	{ "smb347", SMB347 },
1372 	{ "smb358", SMB358 },
1373 	{ },
1374 };
1375 MODULE_DEVICE_TABLE(i2c, smb347_id);
1376 
1377 static const struct of_device_id smb3xx_of_match[] = {
1378 	{ .compatible = "summit,smb345" },
1379 	{ .compatible = "summit,smb347" },
1380 	{ .compatible = "summit,smb358" },
1381 	{ },
1382 };
1383 MODULE_DEVICE_TABLE(of, smb3xx_of_match);
1384 
1385 static struct i2c_driver smb347_driver = {
1386 	.driver = {
1387 		.name = "smb347",
1388 		.of_match_table = smb3xx_of_match,
1389 	},
1390 	.probe        = smb347_probe,
1391 	.remove       = smb347_remove,
1392 	.id_table     = smb347_id,
1393 };
1394 
1395 module_i2c_driver(smb347_driver);
1396 
1397 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1398 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1399 MODULE_DESCRIPTION("SMB347 battery charger driver");
1400 MODULE_LICENSE("GPL");
1401