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