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