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 */
hw_to_current(const unsigned int * tbl,size_t size,unsigned int val)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 */
current_to_hw(const unsigned int * tbl,size_t size,unsigned int val)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  */
smb347_update_ps_status(struct smb347_charger * smb)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  */
smb347_is_ps_online(struct smb347_charger * smb)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  */
smb347_charging_status(struct smb347_charger * smb)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 
smb347_charging_set(struct smb347_charger * smb,bool enable)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 
smb347_charging_enable(struct smb347_charger * smb)379 static inline int smb347_charging_enable(struct smb347_charger *smb)
380 {
381 	return smb347_charging_set(smb, true);
382 }
383 
smb347_charging_disable(struct smb347_charger * smb)384 static inline int smb347_charging_disable(struct smb347_charger *smb)
385 {
386 	return smb347_charging_set(smb, false);
387 }
388 
smb347_start_stop_charging(struct smb347_charger * smb)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 
smb347_set_charge_current(struct smb347_charger * smb)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 
smb347_set_current_limits(struct smb347_charger * smb)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 
smb347_set_voltage_limits(struct smb347_charger * smb)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 
smb347_set_temp_limits(struct smb347_charger * smb)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  */
smb347_set_writable(struct smb347_charger * smb,bool writable)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 
smb347_hw_init(struct smb347_charger * smb)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 
smb347_interrupt(int irq,void * data)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 
smb347_irq_set(struct smb347_charger * smb,bool enable)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 
smb347_irq_enable(struct smb347_charger * smb)899 static inline int smb347_irq_enable(struct smb347_charger *smb)
900 {
901 	return smb347_irq_set(smb, true);
902 }
903 
smb347_irq_disable(struct smb347_charger * smb)904 static inline int smb347_irq_disable(struct smb347_charger *smb)
905 {
906 	return smb347_irq_set(smb, false);
907 }
908 
smb347_irq_init(struct smb347_charger * smb,struct i2c_client * client)909 static int smb347_irq_init(struct smb347_charger *smb,
910 			   struct i2c_client *client)
911 {
912 	int ret;
913 
914 	smb->irq_unsupported = true;
915 
916 	/*
917 	 * Interrupt pin is optional. If it is connected, we setup the
918 	 * interrupt support here.
919 	 */
920 	if (!client->irq)
921 		return 0;
922 
923 	ret = smb347_set_writable(smb, true);
924 	if (ret < 0)
925 		return ret;
926 
927 	/*
928 	 * Configure the STAT output to be suitable for interrupts: disable
929 	 * all other output (except interrupts) and make it active low.
930 	 */
931 	ret = regmap_update_bits(smb->regmap, CFG_STAT,
932 				 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
933 				 CFG_STAT_DISABLED);
934 
935 	smb347_set_writable(smb, false);
936 
937 	if (ret < 0) {
938 		dev_warn(smb->dev, "failed to initialize IRQ: %d\n", ret);
939 		dev_warn(smb->dev, "disabling IRQ support\n");
940 		return 0;
941 	}
942 
943 	ret = devm_request_threaded_irq(smb->dev, client->irq, NULL,
944 					smb347_interrupt, IRQF_ONESHOT,
945 					client->name, smb);
946 	if (ret)
947 		return ret;
948 
949 	smb->irq_unsupported = false;
950 
951 	ret = smb347_irq_enable(smb);
952 	if (ret < 0)
953 		return ret;
954 
955 	return 0;
956 }
957 
958 /*
959  * Returns the constant charge current programmed
960  * into the charger in uA.
961  */
get_const_charge_current(struct smb347_charger * smb)962 static int get_const_charge_current(struct smb347_charger *smb)
963 {
964 	unsigned int id = smb->id;
965 	int ret, intval;
966 	unsigned int v;
967 
968 	if (!smb347_is_ps_online(smb))
969 		return -ENODATA;
970 
971 	ret = regmap_read(smb->regmap, STAT_B, &v);
972 	if (ret < 0)
973 		return ret;
974 
975 	/*
976 	 * The current value is composition of FCC and PCC values
977 	 * and we can detect which table to use from bit 5.
978 	 */
979 	if (v & 0x20) {
980 		intval = hw_to_current(fcc_tbl[id],
981 				       ARRAY_SIZE(fcc_tbl[id]), v & 7);
982 	} else {
983 		v >>= 3;
984 		intval = hw_to_current(pcc_tbl[id],
985 				       ARRAY_SIZE(pcc_tbl[id]), v & 7);
986 	}
987 
988 	return intval;
989 }
990 
991 /*
992  * Returns the constant charge voltage programmed
993  * into the charger in uV.
994  */
get_const_charge_voltage(struct smb347_charger * smb)995 static int get_const_charge_voltage(struct smb347_charger *smb)
996 {
997 	int ret, intval;
998 	unsigned int v;
999 
1000 	if (!smb347_is_ps_online(smb))
1001 		return -ENODATA;
1002 
1003 	ret = regmap_read(smb->regmap, STAT_A, &v);
1004 	if (ret < 0)
1005 		return ret;
1006 
1007 	v &= STAT_A_FLOAT_VOLTAGE_MASK;
1008 	if (v > 0x3d)
1009 		v = 0x3d;
1010 
1011 	intval = 3500000 + v * 20000;
1012 
1013 	return intval;
1014 }
1015 
smb347_get_charging_status(struct smb347_charger * smb,struct power_supply * psy)1016 static int smb347_get_charging_status(struct smb347_charger *smb,
1017 				      struct power_supply *psy)
1018 {
1019 	int ret, status;
1020 	unsigned int val;
1021 
1022 	if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1023 		if (!smb->usb_online)
1024 			return POWER_SUPPLY_STATUS_DISCHARGING;
1025 	} else {
1026 		if (!smb->mains_online)
1027 			return POWER_SUPPLY_STATUS_DISCHARGING;
1028 	}
1029 
1030 	ret = regmap_read(smb->regmap, STAT_C, &val);
1031 	if (ret < 0)
1032 		return ret;
1033 
1034 	if ((val & STAT_C_CHARGER_ERROR) ||
1035 			(val & STAT_C_HOLDOFF_STAT)) {
1036 		/*
1037 		 * set to NOT CHARGING upon charger error
1038 		 * or charging has stopped.
1039 		 */
1040 		status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1041 	} else {
1042 		if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
1043 			/*
1044 			 * set to charging if battery is in pre-charge,
1045 			 * fast charge or taper charging mode.
1046 			 */
1047 			status = POWER_SUPPLY_STATUS_CHARGING;
1048 		} else if (val & STAT_C_CHG_TERM) {
1049 			/*
1050 			 * set the status to FULL if battery is not in pre
1051 			 * charge, fast charge or taper charging mode AND
1052 			 * charging is terminated at least once.
1053 			 */
1054 			status = POWER_SUPPLY_STATUS_FULL;
1055 		} else {
1056 			/*
1057 			 * in this case no charger error or termination
1058 			 * occured but charging is not in progress!!!
1059 			 */
1060 			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1061 		}
1062 	}
1063 
1064 	return status;
1065 }
1066 
smb347_get_property_locked(struct power_supply * psy,enum power_supply_property prop,union power_supply_propval * val)1067 static int smb347_get_property_locked(struct power_supply *psy,
1068 				      enum power_supply_property prop,
1069 				      union power_supply_propval *val)
1070 {
1071 	struct smb347_charger *smb = power_supply_get_drvdata(psy);
1072 	int ret;
1073 
1074 	switch (prop) {
1075 	case POWER_SUPPLY_PROP_STATUS:
1076 		ret = smb347_get_charging_status(smb, psy);
1077 		if (ret < 0)
1078 			return ret;
1079 		val->intval = ret;
1080 		break;
1081 
1082 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
1083 		if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1084 			if (!smb->usb_online)
1085 				return -ENODATA;
1086 		} else {
1087 			if (!smb->mains_online)
1088 				return -ENODATA;
1089 		}
1090 
1091 		/*
1092 		 * We handle trickle and pre-charging the same, and taper
1093 		 * and none the same.
1094 		 */
1095 		switch (smb347_charging_status(smb)) {
1096 		case 1:
1097 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1098 			break;
1099 		case 2:
1100 			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1101 			break;
1102 		default:
1103 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1104 			break;
1105 		}
1106 		break;
1107 
1108 	case POWER_SUPPLY_PROP_ONLINE:
1109 		if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
1110 			val->intval = smb->usb_online;
1111 		else
1112 			val->intval = smb->mains_online;
1113 		break;
1114 
1115 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1116 		ret = get_const_charge_voltage(smb);
1117 		if (ret < 0)
1118 			return ret;
1119 		val->intval = ret;
1120 		break;
1121 
1122 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1123 		ret = get_const_charge_current(smb);
1124 		if (ret < 0)
1125 			return ret;
1126 		val->intval = ret;
1127 		break;
1128 
1129 	default:
1130 		return -EINVAL;
1131 	}
1132 
1133 	return 0;
1134 }
1135 
smb347_get_property(struct power_supply * psy,enum power_supply_property prop,union power_supply_propval * val)1136 static int smb347_get_property(struct power_supply *psy,
1137 			       enum power_supply_property prop,
1138 			       union power_supply_propval *val)
1139 {
1140 	struct smb347_charger *smb = power_supply_get_drvdata(psy);
1141 	struct i2c_client *client = to_i2c_client(smb->dev);
1142 	int ret;
1143 
1144 	if (!smb->irq_unsupported)
1145 		disable_irq(client->irq);
1146 
1147 	ret = smb347_get_property_locked(psy, prop, val);
1148 
1149 	if (!smb->irq_unsupported)
1150 		enable_irq(client->irq);
1151 
1152 	return ret;
1153 }
1154 
1155 static enum power_supply_property smb347_properties[] = {
1156 	POWER_SUPPLY_PROP_STATUS,
1157 	POWER_SUPPLY_PROP_CHARGE_TYPE,
1158 	POWER_SUPPLY_PROP_ONLINE,
1159 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1160 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1161 };
1162 
smb347_volatile_reg(struct device * dev,unsigned int reg)1163 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1164 {
1165 	switch (reg) {
1166 	case IRQSTAT_A:
1167 	case IRQSTAT_C:
1168 	case IRQSTAT_D:
1169 	case IRQSTAT_E:
1170 	case IRQSTAT_F:
1171 	case STAT_A:
1172 	case STAT_B:
1173 	case STAT_C:
1174 	case STAT_E:
1175 		return true;
1176 	}
1177 
1178 	return false;
1179 }
1180 
smb347_readable_reg(struct device * dev,unsigned int reg)1181 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1182 {
1183 	switch (reg) {
1184 	case CFG_CHARGE_CURRENT:
1185 	case CFG_CURRENT_LIMIT:
1186 	case CFG_FLOAT_VOLTAGE:
1187 	case CFG_STAT:
1188 	case CFG_PIN:
1189 	case CFG_THERM:
1190 	case CFG_SYSOK:
1191 	case CFG_OTHER:
1192 	case CFG_OTG:
1193 	case CFG_TEMP_LIMIT:
1194 	case CFG_FAULT_IRQ:
1195 	case CFG_STATUS_IRQ:
1196 	case CFG_ADDRESS:
1197 	case CMD_A:
1198 	case CMD_B:
1199 	case CMD_C:
1200 		return true;
1201 	}
1202 
1203 	return smb347_volatile_reg(dev, reg);
1204 }
1205 
smb347_dt_parse_dev_info(struct smb347_charger * smb)1206 static void smb347_dt_parse_dev_info(struct smb347_charger *smb)
1207 {
1208 	struct device *dev = smb->dev;
1209 
1210 	smb->soft_temp_limit_compensation =
1211 					SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT;
1212 	/*
1213 	 * These properties come from the battery info, still we need to
1214 	 * pre-initialize the values. See smb347_get_battery_info() below.
1215 	 */
1216 	smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1217 	smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1218 	smb->soft_hot_temp_limit  = SMB3XX_TEMP_USE_DEFAULT;
1219 	smb->hard_hot_temp_limit  = SMB3XX_TEMP_USE_DEFAULT;
1220 
1221 	/* Charging constraints */
1222 	device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt",
1223 				 &smb->pre_to_fast_voltage);
1224 	device_property_read_u32(dev, "summit,mains-current-limit-microamp",
1225 				 &smb->mains_current_limit);
1226 	device_property_read_u32(dev, "summit,usb-current-limit-microamp",
1227 				 &smb->usb_hc_current_limit);
1228 
1229 	/* For thermometer monitoring */
1230 	device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius",
1231 				 &smb->chip_temp_threshold);
1232 	device_property_read_u32(dev, "summit,soft-compensation-method",
1233 				 &smb->soft_temp_limit_compensation);
1234 	device_property_read_u32(dev, "summit,charge-current-compensation-microamp",
1235 				 &smb->charge_current_compensation);
1236 
1237 	/* Supported charging mode */
1238 	smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging");
1239 	smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging");
1240 	smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging");
1241 
1242 	/* Select charging control */
1243 	device_property_read_u32(dev, "summit,enable-charge-control",
1244 				 &smb->enable_control);
1245 }
1246 
smb347_get_battery_info(struct smb347_charger * smb)1247 static int smb347_get_battery_info(struct smb347_charger *smb)
1248 {
1249 	struct power_supply_battery_info info = {};
1250 	struct power_supply *supply;
1251 	int err;
1252 
1253 	if (smb->mains)
1254 		supply = smb->mains;
1255 	else
1256 		supply = smb->usb;
1257 
1258 	err = power_supply_get_battery_info(supply, &info);
1259 	if (err == -ENXIO || err == -ENODEV)
1260 		return 0;
1261 	if (err)
1262 		return err;
1263 
1264 	if (info.constant_charge_current_max_ua != -EINVAL)
1265 		smb->max_charge_current = info.constant_charge_current_max_ua;
1266 
1267 	if (info.constant_charge_voltage_max_uv != -EINVAL)
1268 		smb->max_charge_voltage = info.constant_charge_voltage_max_uv;
1269 
1270 	if (info.precharge_current_ua != -EINVAL)
1271 		smb->pre_charge_current = info.precharge_current_ua;
1272 
1273 	if (info.charge_term_current_ua != -EINVAL)
1274 		smb->termination_current = info.charge_term_current_ua;
1275 
1276 	if (info.temp_alert_min != INT_MIN)
1277 		smb->soft_cold_temp_limit = info.temp_alert_min;
1278 
1279 	if (info.temp_alert_max != INT_MAX)
1280 		smb->soft_hot_temp_limit = info.temp_alert_max;
1281 
1282 	if (info.temp_min != INT_MIN)
1283 		smb->hard_cold_temp_limit = info.temp_min;
1284 
1285 	if (info.temp_max != INT_MAX)
1286 		smb->hard_hot_temp_limit = info.temp_max;
1287 
1288 	/* Suspend when battery temperature is outside hard limits */
1289 	if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT ||
1290 	    smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT)
1291 		smb->suspend_on_hard_temp_limit = true;
1292 
1293 	return 0;
1294 }
1295 
1296 static const struct regmap_config smb347_regmap = {
1297 	.reg_bits	= 8,
1298 	.val_bits	= 8,
1299 	.max_register	= SMB347_MAX_REGISTER,
1300 	.volatile_reg	= smb347_volatile_reg,
1301 	.readable_reg	= smb347_readable_reg,
1302 };
1303 
1304 static const struct power_supply_desc smb347_mains_desc = {
1305 	.name		= "smb347-mains",
1306 	.type		= POWER_SUPPLY_TYPE_MAINS,
1307 	.get_property	= smb347_get_property,
1308 	.properties	= smb347_properties,
1309 	.num_properties	= ARRAY_SIZE(smb347_properties),
1310 };
1311 
1312 static const struct power_supply_desc smb347_usb_desc = {
1313 	.name		= "smb347-usb",
1314 	.type		= POWER_SUPPLY_TYPE_USB,
1315 	.get_property	= smb347_get_property,
1316 	.properties	= smb347_properties,
1317 	.num_properties	= ARRAY_SIZE(smb347_properties),
1318 };
1319 
smb347_probe(struct i2c_client * client,const struct i2c_device_id * id)1320 static int smb347_probe(struct i2c_client *client,
1321 			const struct i2c_device_id *id)
1322 {
1323 	struct power_supply_config mains_usb_cfg = {};
1324 	struct device *dev = &client->dev;
1325 	struct smb347_charger *smb;
1326 	int ret;
1327 
1328 	smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1329 	if (!smb)
1330 		return -ENOMEM;
1331 	smb->dev = &client->dev;
1332 	smb->id = id->driver_data;
1333 	i2c_set_clientdata(client, smb);
1334 
1335 	smb347_dt_parse_dev_info(smb);
1336 	if (!smb->use_mains && !smb->use_usb)
1337 		return -EINVAL;
1338 
1339 	smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1340 	if (IS_ERR(smb->regmap))
1341 		return PTR_ERR(smb->regmap);
1342 
1343 	mains_usb_cfg.drv_data = smb;
1344 	mains_usb_cfg.of_node = dev->of_node;
1345 	if (smb->use_mains) {
1346 		smb->mains = devm_power_supply_register(dev, &smb347_mains_desc,
1347 							&mains_usb_cfg);
1348 		if (IS_ERR(smb->mains))
1349 			return PTR_ERR(smb->mains);
1350 	}
1351 
1352 	if (smb->use_usb) {
1353 		smb->usb = devm_power_supply_register(dev, &smb347_usb_desc,
1354 						      &mains_usb_cfg);
1355 		if (IS_ERR(smb->usb))
1356 			return PTR_ERR(smb->usb);
1357 	}
1358 
1359 	ret = smb347_get_battery_info(smb);
1360 	if (ret)
1361 		return ret;
1362 
1363 	ret = smb347_hw_init(smb);
1364 	if (ret < 0)
1365 		return ret;
1366 
1367 	ret = smb347_irq_init(smb, client);
1368 	if (ret)
1369 		return ret;
1370 
1371 	return 0;
1372 }
1373 
smb347_remove(struct i2c_client * client)1374 static int smb347_remove(struct i2c_client *client)
1375 {
1376 	struct smb347_charger *smb = i2c_get_clientdata(client);
1377 
1378 	smb347_irq_disable(smb);
1379 
1380 	return 0;
1381 }
1382 
1383 static const struct i2c_device_id smb347_id[] = {
1384 	{ "smb345", SMB345 },
1385 	{ "smb347", SMB347 },
1386 	{ "smb358", SMB358 },
1387 	{ },
1388 };
1389 MODULE_DEVICE_TABLE(i2c, smb347_id);
1390 
1391 static const struct of_device_id smb3xx_of_match[] = {
1392 	{ .compatible = "summit,smb345" },
1393 	{ .compatible = "summit,smb347" },
1394 	{ .compatible = "summit,smb358" },
1395 	{ },
1396 };
1397 MODULE_DEVICE_TABLE(of, smb3xx_of_match);
1398 
1399 static struct i2c_driver smb347_driver = {
1400 	.driver = {
1401 		.name = "smb347",
1402 		.of_match_table = smb3xx_of_match,
1403 	},
1404 	.probe = smb347_probe,
1405 	.remove = smb347_remove,
1406 	.id_table = smb347_id,
1407 };
1408 module_i2c_driver(smb347_driver);
1409 
1410 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1411 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1412 MODULE_DESCRIPTION("SMB347 battery charger driver");
1413 MODULE_LICENSE("GPL");
1414