1 // SPDX-License-Identifier: GPL-2.0-only
3 * Summit Microelectronics SMB347 Battery Charger Driver
5 * Copyright (C) 2011, Intel Corporation
7 * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com>
8 * Mika Westerberg <mika.westerberg@linux.intel.com>
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>
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.
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
43 #define CFG_STAT_DISABLED BIT(5)
44 #define CFG_STAT_ACTIVE_HIGH BIT(7)
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
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
83 /* Command registers */
85 #define CMD_A_CHG_ENABLED BIT(1)
86 #define CMD_A_SUSPEND_ENABLED BIT(2)
87 #define CMD_A_ALLOW_WRITE BIT(7)
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
107 /* Status registers */
109 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
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)
120 #define SMB347_MAX_REGISTER 0x3f
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
135 struct smb347_charger
{
138 struct regmap
*regmap
;
139 struct power_supply
*mains
;
140 struct power_supply
*usb
;
141 struct power_supply
*battery
;
144 bool charging_enabled
;
145 const struct smb347_charger_platform_data
*pdata
;
148 /* Fast charge current in uA */
149 static const unsigned int fcc_tbl
[] = {
160 /* Pre-charge current in uA */
161 static const unsigned int pcc_tbl
[] = {
168 /* Termination current in uA */
169 static const unsigned int tc_tbl
[] = {
180 /* Input current limit in uA */
181 static const unsigned int icl_tbl
[] = {
194 /* Charge current compensation in uA */
195 static const unsigned int ccc_tbl
[] = {
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
)
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
)
215 for (i
= 0; i
< size
; i
++)
218 return i
> 0 ? i
- 1 : -EINVAL
;
222 * smb347_update_ps_status - refreshes the power source status
223 * @smb: pointer to smb347 charger instance
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.
229 static int smb347_update_ps_status(struct smb347_charger
*smb
)
236 ret
= regmap_read(smb
->regmap
, IRQSTAT_E
, &val
);
241 * Dc and usb are set depending on whether they are enabled in
242 * platform data _and_ whether corresponding undervoltage is set.
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
);
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
);
259 * smb347_is_ps_online - returns whether input power source is connected
260 * @smb: pointer to smb347 charger instance
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
267 static bool smb347_is_ps_online(struct smb347_charger
*smb
)
271 mutex_lock(&smb
->lock
);
272 ret
= smb
->usb_online
|| smb
->mains_online
;
273 mutex_unlock(&smb
->lock
);
279 * smb347_charging_status - returns status of charging
280 * @smb: pointer to smb347 charger instance
282 * Function returns charging status. %0 means no charging is in progress,
283 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
285 static int smb347_charging_status(struct smb347_charger
*smb
)
290 if (!smb347_is_ps_online(smb
))
293 ret
= regmap_read(smb
->regmap
, STAT_C
, &val
);
297 return (val
& STAT_C_CHG_MASK
) >> STAT_C_CHG_SHIFT
;
300 static int smb347_charging_set(struct smb347_charger
*smb
, bool enable
)
304 if (smb
->pdata
->enable_control
!= SMB347_CHG_ENABLE_SW
) {
305 dev_dbg(smb
->dev
, "charging enable/disable in SW disabled\n");
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);
314 smb
->charging_enabled
= enable
;
316 mutex_unlock(&smb
->lock
);
320 static inline int smb347_charging_enable(struct smb347_charger
*smb
)
322 return smb347_charging_set(smb
, true);
325 static inline int smb347_charging_disable(struct smb347_charger
*smb
)
327 return smb347_charging_set(smb
, false);
330 static int smb347_start_stop_charging(struct smb347_charger
*smb
)
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.
339 if (smb347_is_ps_online(smb
)) {
340 ret
= smb347_charging_enable(smb
);
342 dev_err(smb
->dev
, "failed to enable charging\n");
344 ret
= smb347_charging_disable(smb
);
346 dev_err(smb
->dev
, "failed to disable charging\n");
352 static int smb347_set_charge_current(struct smb347_charger
*smb
)
356 if (smb
->pdata
->max_charge_current
) {
357 ret
= current_to_hw(fcc_tbl
, ARRAY_SIZE(fcc_tbl
),
358 smb
->pdata
->max_charge_current
);
362 ret
= regmap_update_bits(smb
->regmap
, CFG_CHARGE_CURRENT
,
363 CFG_CHARGE_CURRENT_FCC_MASK
,
364 ret
<< CFG_CHARGE_CURRENT_FCC_SHIFT
);
369 if (smb
->pdata
->pre_charge_current
) {
370 ret
= current_to_hw(pcc_tbl
, ARRAY_SIZE(pcc_tbl
),
371 smb
->pdata
->pre_charge_current
);
375 ret
= regmap_update_bits(smb
->regmap
, CFG_CHARGE_CURRENT
,
376 CFG_CHARGE_CURRENT_PCC_MASK
,
377 ret
<< CFG_CHARGE_CURRENT_PCC_SHIFT
);
382 if (smb
->pdata
->termination_current
) {
383 ret
= current_to_hw(tc_tbl
, ARRAY_SIZE(tc_tbl
),
384 smb
->pdata
->termination_current
);
388 ret
= regmap_update_bits(smb
->regmap
, CFG_CHARGE_CURRENT
,
389 CFG_CHARGE_CURRENT_TC_MASK
, ret
);
397 static int smb347_set_current_limits(struct smb347_charger
*smb
)
401 if (smb
->pdata
->mains_current_limit
) {
402 ret
= current_to_hw(icl_tbl
, ARRAY_SIZE(icl_tbl
),
403 smb
->pdata
->mains_current_limit
);
407 ret
= regmap_update_bits(smb
->regmap
, CFG_CURRENT_LIMIT
,
408 CFG_CURRENT_LIMIT_DC_MASK
,
409 ret
<< CFG_CURRENT_LIMIT_DC_SHIFT
);
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
);
420 ret
= regmap_update_bits(smb
->regmap
, CFG_CURRENT_LIMIT
,
421 CFG_CURRENT_LIMIT_USB_MASK
, ret
);
429 static int smb347_set_voltage_limits(struct smb347_charger
*smb
)
433 if (smb
->pdata
->pre_to_fast_voltage
) {
434 ret
= smb
->pdata
->pre_to_fast_voltage
;
437 ret
= clamp_val(ret
, 2400000, 3000000) - 2400000;
440 ret
= regmap_update_bits(smb
->regmap
, CFG_FLOAT_VOLTAGE
,
441 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK
,
442 ret
<< CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT
);
447 if (smb
->pdata
->max_charge_voltage
) {
448 ret
= smb
->pdata
->max_charge_voltage
;
451 ret
= clamp_val(ret
, 3500000, 4500000) - 3500000;
454 ret
= regmap_update_bits(smb
->regmap
, CFG_FLOAT_VOLTAGE
,
455 CFG_FLOAT_VOLTAGE_FLOAT_MASK
, ret
);
463 static int smb347_set_temp_limits(struct smb347_charger
*smb
)
465 bool enable_therm_monitor
= false;
469 if (smb
->pdata
->chip_temp_threshold
) {
470 val
= smb
->pdata
->chip_temp_threshold
;
473 val
= clamp_val(val
, 100, 130) - 100;
476 ret
= regmap_update_bits(smb
->regmap
, CFG_OTG
,
477 CFG_OTG_TEMP_THRESHOLD_MASK
,
478 val
<< CFG_OTG_TEMP_THRESHOLD_SHIFT
);
483 if (smb
->pdata
->soft_cold_temp_limit
!= SMB347_TEMP_USE_DEFAULT
) {
484 val
= smb
->pdata
->soft_cold_temp_limit
;
486 val
= clamp_val(val
, 0, 15);
488 /* this goes from higher to lower so invert the value */
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
);
497 enable_therm_monitor
= true;
500 if (smb
->pdata
->soft_hot_temp_limit
!= SMB347_TEMP_USE_DEFAULT
) {
501 val
= smb
->pdata
->soft_hot_temp_limit
;
503 val
= clamp_val(val
, 40, 55) - 40;
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
);
512 enable_therm_monitor
= true;
515 if (smb
->pdata
->hard_cold_temp_limit
!= SMB347_TEMP_USE_DEFAULT
) {
516 val
= smb
->pdata
->hard_cold_temp_limit
;
518 val
= clamp_val(val
, -5, 10) + 5;
520 /* this goes from higher to lower so invert the value */
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
);
529 enable_therm_monitor
= true;
532 if (smb
->pdata
->hard_hot_temp_limit
!= SMB347_TEMP_USE_DEFAULT
) {
533 val
= smb
->pdata
->hard_hot_temp_limit
;
535 val
= clamp_val(val
, 50, 65) - 50;
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
);
544 enable_therm_monitor
= true;
548 * If any of the temperature limits are set, we also enable the
549 * thermistor monitoring.
551 * When soft limits are hit, the device will start to compensate
552 * current and/or voltage depending on the configuration.
554 * When hard limit is hit, the device will suspend charging
555 * depending on the configuration.
557 if (enable_therm_monitor
) {
558 ret
= regmap_update_bits(smb
->regmap
, CFG_THERM
,
559 CFG_THERM_MONITOR_DISABLED
, 0);
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);
571 if (smb
->pdata
->soft_temp_limit_compensation
!=
572 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT
) {
573 val
= smb
->pdata
->soft_temp_limit_compensation
& 0x3;
575 ret
= regmap_update_bits(smb
->regmap
, CFG_THERM
,
576 CFG_THERM_SOFT_HOT_COMPENSATION_MASK
,
577 val
<< CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT
);
581 ret
= regmap_update_bits(smb
->regmap
, CFG_THERM
,
582 CFG_THERM_SOFT_COLD_COMPENSATION_MASK
,
583 val
<< CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT
);
588 if (smb
->pdata
->charge_current_compensation
) {
589 val
= current_to_hw(ccc_tbl
, ARRAY_SIZE(ccc_tbl
),
590 smb
->pdata
->charge_current_compensation
);
594 ret
= regmap_update_bits(smb
->regmap
, CFG_OTG
,
595 CFG_OTG_CC_COMPENSATION_MASK
,
596 (val
& 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT
);
605 * smb347_set_writable - enables/disables writing to non-volatile registers
606 * @smb: pointer to smb347 charger instance
608 * You can enable/disable writing to the non-volatile configuration
609 * registers by calling this function.
611 * Returns %0 on success and negative errno in case of failure.
613 static int smb347_set_writable(struct smb347_charger
*smb
, bool writable
)
615 return regmap_update_bits(smb
->regmap
, CMD_A
, CMD_A_ALLOW_WRITE
,
616 writable
? CMD_A_ALLOW_WRITE
: 0);
619 static int smb347_hw_init(struct smb347_charger
*smb
)
624 ret
= smb347_set_writable(smb
, true);
629 * Program the platform specific configuration values to the device
632 ret
= smb347_set_charge_current(smb
);
636 ret
= smb347_set_current_limits(smb
);
640 ret
= smb347_set_voltage_limits(smb
);
644 ret
= smb347_set_temp_limits(smb
);
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
);
658 * If configured by platform data, we enable hardware Auto-OTG
659 * support for driving VBUS. Otherwise we disable it.
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);
667 * Make the charging functionality controllable by a write to the
668 * command register unless pin control is specified in the platform
671 switch (smb
->pdata
->enable_control
) {
672 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW
:
673 val
= CFG_PIN_EN_CTRL_ACTIVE_LOW
;
675 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH
:
676 val
= CFG_PIN_EN_CTRL_ACTIVE_HIGH
;
683 ret
= regmap_update_bits(smb
->regmap
, CFG_PIN
, CFG_PIN_EN_CTRL_MASK
,
688 /* Disable Automatic Power Source Detection (APSD) interrupt. */
689 ret
= regmap_update_bits(smb
->regmap
, CFG_PIN
, CFG_PIN_EN_APSD_IRQ
, 0);
693 ret
= smb347_update_ps_status(smb
);
697 ret
= smb347_start_stop_charging(smb
);
700 smb347_set_writable(smb
, false);
704 static irqreturn_t
smb347_interrupt(int irq
, void *data
)
706 struct smb347_charger
*smb
= data
;
707 unsigned int stat_c
, irqstat_c
, irqstat_d
, irqstat_e
;
708 bool handled
= false;
711 ret
= regmap_read(smb
->regmap
, STAT_C
, &stat_c
);
713 dev_warn(smb
->dev
, "reading STAT_C failed\n");
717 ret
= regmap_read(smb
->regmap
, IRQSTAT_C
, &irqstat_c
);
719 dev_warn(smb
->dev
, "reading IRQSTAT_C failed\n");
723 ret
= regmap_read(smb
->regmap
, IRQSTAT_D
, &irqstat_d
);
725 dev_warn(smb
->dev
, "reading IRQSTAT_D failed\n");
729 ret
= regmap_read(smb
->regmap
, IRQSTAT_E
, &irqstat_e
);
731 dev_warn(smb
->dev
, "reading IRQSTAT_E failed\n");
736 * If we get charger error we report the error back to user.
737 * If the error is recovered charging will resume again.
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
);
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.
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");
758 * If we got a charger timeout INT that means the charge
759 * full is not detected with in charge timeout value.
761 if (irqstat_d
& IRQSTAT_D_CHARGE_TIMEOUT_IRQ
) {
762 dev_dbg(smb
->dev
, "total Charge Timeout INT received\n");
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
);
771 * If we got an under voltage interrupt it means that AC/USB input
772 * was connected or disconnected.
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
);
785 return handled
? IRQ_HANDLED
: IRQ_NONE
;
788 static int smb347_irq_set(struct smb347_charger
*smb
, bool enable
)
792 ret
= smb347_set_writable(smb
, true);
797 * Enable/disable interrupts for:
799 * - termination current reached
803 ret
= regmap_update_bits(smb
->regmap
, CFG_FAULT_IRQ
, 0xff,
804 enable
? CFG_FAULT_IRQ_DCIN_UV
: 0);
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);
814 ret
= regmap_update_bits(smb
->regmap
, CFG_PIN
, CFG_PIN_EN_CHARGER_ERROR
,
815 enable
? CFG_PIN_EN_CHARGER_ERROR
: 0);
817 smb347_set_writable(smb
, false);
821 static inline int smb347_irq_enable(struct smb347_charger
*smb
)
823 return smb347_irq_set(smb
, true);
826 static inline int smb347_irq_disable(struct smb347_charger
*smb
)
828 return smb347_irq_set(smb
, false);
831 static int smb347_irq_init(struct smb347_charger
*smb
,
832 struct i2c_client
*client
)
834 const struct smb347_charger_platform_data
*pdata
= smb
->pdata
;
835 int ret
, irq
= gpio_to_irq(pdata
->irq_gpio
);
837 ret
= gpio_request_one(pdata
->irq_gpio
, GPIOF_IN
, client
->name
);
841 ret
= request_threaded_irq(irq
, NULL
, smb347_interrupt
,
842 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
847 ret
= smb347_set_writable(smb
, true);
852 * Configure the STAT output to be suitable for interrupts: disable
853 * all other output (except interrupts) and make it active low.
855 ret
= regmap_update_bits(smb
->regmap
, CFG_STAT
,
856 CFG_STAT_ACTIVE_HIGH
| CFG_STAT_DISABLED
,
861 smb347_set_writable(smb
, false);
866 smb347_set_writable(smb
, false);
870 gpio_free(pdata
->irq_gpio
);
877 * Returns the constant charge current programmed
878 * into the charger in uA.
880 static int get_const_charge_current(struct smb347_charger
*smb
)
885 if (!smb347_is_ps_online(smb
))
888 ret
= regmap_read(smb
->regmap
, STAT_B
, &v
);
893 * The current value is composition of FCC and PCC values
894 * and we can detect which table to use from bit 5.
897 intval
= hw_to_current(fcc_tbl
, ARRAY_SIZE(fcc_tbl
), v
& 7);
900 intval
= hw_to_current(pcc_tbl
, ARRAY_SIZE(pcc_tbl
), v
& 7);
907 * Returns the constant charge voltage programmed
908 * into the charger in uV.
910 static int get_const_charge_voltage(struct smb347_charger
*smb
)
915 if (!smb347_is_ps_online(smb
))
918 ret
= regmap_read(smb
->regmap
, STAT_A
, &v
);
922 v
&= STAT_A_FLOAT_VOLTAGE_MASK
;
926 intval
= 3500000 + v
* 20000;
931 static int smb347_mains_get_property(struct power_supply
*psy
,
932 enum power_supply_property prop
,
933 union power_supply_propval
*val
)
935 struct smb347_charger
*smb
= power_supply_get_drvdata(psy
);
939 case POWER_SUPPLY_PROP_ONLINE
:
940 val
->intval
= smb
->mains_online
;
943 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
944 ret
= get_const_charge_voltage(smb
);
951 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
952 ret
= get_const_charge_current(smb
);
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
,
972 static int smb347_usb_get_property(struct power_supply
*psy
,
973 enum power_supply_property prop
,
974 union power_supply_propval
*val
)
976 struct smb347_charger
*smb
= power_supply_get_drvdata(psy
);
980 case POWER_SUPPLY_PROP_ONLINE
:
981 val
->intval
= smb
->usb_online
;
984 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
985 ret
= get_const_charge_voltage(smb
);
992 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
993 ret
= get_const_charge_current(smb
);
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
,
1013 static int smb347_get_charging_status(struct smb347_charger
*smb
)
1018 if (!smb347_is_ps_online(smb
))
1019 return POWER_SUPPLY_STATUS_DISCHARGING
;
1021 ret
= regmap_read(smb
->regmap
, STAT_C
, &val
);
1025 if ((val
& STAT_C_CHARGER_ERROR
) ||
1026 (val
& STAT_C_HOLDOFF_STAT
)) {
1028 * set to NOT CHARGING upon charger error
1029 * or charging has stopped.
1031 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1033 if ((val
& STAT_C_CHG_MASK
) >> STAT_C_CHG_SHIFT
) {
1035 * set to charging if battery is in pre-charge,
1036 * fast charge or taper charging mode.
1038 status
= POWER_SUPPLY_STATUS_CHARGING
;
1039 } else if (val
& STAT_C_CHG_TERM
) {
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.
1045 status
= POWER_SUPPLY_STATUS_FULL
;
1048 * in this case no charger error or termination
1049 * occured but charging is not in progress!!!
1051 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1058 static int smb347_battery_get_property(struct power_supply
*psy
,
1059 enum power_supply_property prop
,
1060 union power_supply_propval
*val
)
1062 struct smb347_charger
*smb
= power_supply_get_drvdata(psy
);
1063 const struct smb347_charger_platform_data
*pdata
= smb
->pdata
;
1066 ret
= smb347_update_ps_status(smb
);
1071 case POWER_SUPPLY_PROP_STATUS
:
1072 ret
= smb347_get_charging_status(smb
);
1078 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
1079 if (!smb347_is_ps_online(smb
))
1083 * We handle trickle and pre-charging the same, and taper
1084 * and none the same.
1086 switch (smb347_charging_status(smb
)) {
1088 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
1091 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
1094 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
1099 case POWER_SUPPLY_PROP_TECHNOLOGY
:
1100 val
->intval
= pdata
->battery_info
.technology
;
1103 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
1104 val
->intval
= pdata
->battery_info
.voltage_min_design
;
1107 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
1108 val
->intval
= pdata
->battery_info
.voltage_max_design
;
1111 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
1112 val
->intval
= pdata
->battery_info
.charge_full_design
;
1115 case POWER_SUPPLY_PROP_MODEL_NAME
:
1116 val
->strval
= pdata
->battery_info
.name
;
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
,
1136 static bool smb347_volatile_reg(struct device
*dev
, unsigned int reg
)
1153 static bool smb347_readable_reg(struct device
*dev
, unsigned int reg
)
1156 case CFG_CHARGE_CURRENT
:
1157 case CFG_CURRENT_LIMIT
:
1158 case CFG_FLOAT_VOLTAGE
:
1165 case CFG_TEMP_LIMIT
:
1167 case CFG_STATUS_IRQ
:
1175 return smb347_volatile_reg(dev
, reg
);
1178 static const struct regmap_config smb347_regmap
= {
1181 .max_register
= SMB347_MAX_REGISTER
,
1182 .volatile_reg
= smb347_volatile_reg
,
1183 .readable_reg
= smb347_readable_reg
,
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
),
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
),
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
),
1210 static int smb347_probe(struct i2c_client
*client
,
1211 const struct i2c_device_id
*id
)
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
;
1220 pdata
= dev
->platform_data
;
1224 if (!pdata
->use_mains
&& !pdata
->use_usb
)
1227 smb
= devm_kzalloc(dev
, sizeof(*smb
), GFP_KERNEL
);
1231 i2c_set_clientdata(client
, smb
);
1233 mutex_init(&smb
->lock
);
1234 smb
->dev
= &client
->dev
;
1237 smb
->regmap
= devm_regmap_init_i2c(client
, &smb347_regmap
);
1238 if (IS_ERR(smb
->regmap
))
1239 return PTR_ERR(smb
->regmap
);
1241 ret
= smb347_hw_init(smb
);
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
,
1251 if (IS_ERR(smb
->mains
))
1252 return PTR_ERR(smb
->mains
);
1255 if (smb
->pdata
->use_usb
) {
1256 smb
->usb
= power_supply_register(dev
, &smb347_usb_desc
,
1258 if (IS_ERR(smb
->usb
)) {
1259 if (smb
->pdata
->use_mains
)
1260 power_supply_unregister(smb
->mains
);
1261 return PTR_ERR(smb
->usb
);
1265 battery_cfg
.drv_data
= smb
;
1266 smb
->battery
= power_supply_register(dev
, &smb347_battery_desc
,
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
);
1277 * Interrupt pin is optional. If it is connected, we setup the
1278 * interrupt support here.
1280 if (pdata
->irq_gpio
>= 0) {
1281 ret
= smb347_irq_init(smb
, client
);
1283 dev_warn(dev
, "failed to initialize IRQ: %d\n", ret
);
1284 dev_warn(dev
, "disabling IRQ support\n");
1286 smb347_irq_enable(smb
);
1293 static int smb347_remove(struct i2c_client
*client
)
1295 struct smb347_charger
*smb
= i2c_get_clientdata(client
);
1298 smb347_irq_disable(smb
);
1299 free_irq(client
->irq
, smb
);
1300 gpio_free(smb
->pdata
->irq_gpio
);
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
);
1311 static const struct i2c_device_id smb347_id
[] = {
1315 MODULE_DEVICE_TABLE(i2c
, smb347_id
);
1317 static struct i2c_driver smb347_driver
= {
1321 .probe
= smb347_probe
,
1322 .remove
= smb347_remove
,
1323 .id_table
= smb347_id
,
1326 module_i2c_driver(smb347_driver
);
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");