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/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>
23 #include <dt-bindings/power/summit,smb347-charger.h>
25 /* Use the default compensation method */
26 #define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1
28 /* Use default factory programmed value for hard/soft temperature limit */
29 #define SMB3XX_TEMP_USE_DEFAULT -273
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.
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
51 #define CFG_STAT_DISABLED BIT(5)
52 #define CFG_STAT_ACTIVE_HIGH BIT(7)
54 #define CFG_PIN_EN_CTRL_MASK 0x60
55 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
56 #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
57 #define CFG_PIN_EN_APSD_IRQ BIT(1)
58 #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
59 #define CFG_THERM 0x07
60 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
61 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
62 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
63 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
64 #define CFG_THERM_MONITOR_DISABLED BIT(4)
65 #define CFG_SYSOK 0x08
66 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
67 #define CFG_OTHER 0x09
68 #define CFG_OTHER_RID_MASK 0xc0
69 #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
71 #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
72 #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
73 #define CFG_OTG_CC_COMPENSATION_MASK 0xc0
74 #define CFG_OTG_CC_COMPENSATION_SHIFT 6
75 #define CFG_TEMP_LIMIT 0x0b
76 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
77 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
78 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
79 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
80 #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
81 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
82 #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
83 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
84 #define CFG_FAULT_IRQ 0x0c
85 #define CFG_FAULT_IRQ_DCIN_UV BIT(2)
86 #define CFG_STATUS_IRQ 0x0d
87 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
88 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7)
89 #define CFG_ADDRESS 0x0e
91 /* Command registers */
93 #define CMD_A_CHG_ENABLED BIT(1)
94 #define CMD_A_SUSPEND_ENABLED BIT(2)
95 #define CMD_A_ALLOW_WRITE BIT(7)
99 /* Interrupt Status registers */
100 #define IRQSTAT_A 0x35
101 #define IRQSTAT_C 0x37
102 #define IRQSTAT_C_TERMINATION_STAT BIT(0)
103 #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
104 #define IRQSTAT_C_TAPER_IRQ BIT(3)
105 #define IRQSTAT_D 0x38
106 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2)
107 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3)
108 #define IRQSTAT_E 0x39
109 #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
110 #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
111 #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
112 #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
113 #define IRQSTAT_F 0x3a
115 /* Status registers */
117 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
120 #define STAT_C_CHG_ENABLED BIT(0)
121 #define STAT_C_HOLDOFF_STAT BIT(3)
122 #define STAT_C_CHG_MASK 0x06
123 #define STAT_C_CHG_SHIFT 1
124 #define STAT_C_CHG_TERM BIT(5)
125 #define STAT_C_CHARGER_ERROR BIT(6)
128 #define SMB347_MAX_REGISTER 0x3f
131 * struct smb347_charger - smb347 charger instance
132 * @dev: pointer to device
133 * @regmap: pointer to driver regmap
134 * @mains: power_supply instance for AC/DC power
135 * @usb: power_supply instance for USB power
136 * @id: SMB charger ID
137 * @mains_online: is AC/DC input connected
138 * @usb_online: is USB input connected
139 * @charging_enabled: is charging enabled
140 * @max_charge_current: maximum current (in uA) the battery can be charged
141 * @max_charge_voltage: maximum voltage (in uV) the battery can be charged
142 * @pre_charge_current: current (in uA) to use in pre-charging phase
143 * @termination_current: current (in uA) used to determine when the
144 * charging cycle terminates
145 * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to
146 * pre-charge to fast charge mode
147 * @mains_current_limit: maximum input current drawn from AC/DC input (in uA)
148 * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB
150 * @chip_temp_threshold: die temperature where device starts limiting charge
151 * current [%100 - %130] (in degree C)
152 * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C),
153 * granularity is 5 deg C.
154 * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree C),
155 * granularity is 5 deg C.
156 * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C),
157 * granularity is 5 deg C.
158 * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C),
159 * granularity is 5 deg C.
160 * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit
161 * @soft_temp_limit_compensation: compensation method when soft temperature
163 * @charge_current_compensation: current (in uA) for charging compensation
164 * current when temperature hits soft limits
165 * @use_mains: AC/DC input can be used
166 * @use_usb: USB input can be used
167 * @use_usb_otg: USB OTG output can be used (not implemented yet)
168 * @enable_control: how charging enable/disable is controlled
169 * (driver/pin controls)
171 * @use_main, @use_usb, and @use_usb_otg are means to enable/disable
172 * hardware support for these. This is useful when we want to have for
173 * example OTG charging controlled via OTG transceiver driver and not by
174 * the SMB347 hardware.
176 * Hard and soft temperature limit values are given as described in the
177 * device data sheet and assuming NTC beta value is %3750. Even if this is
178 * not the case, these values should be used. They can be mapped to the
179 * corresponding NTC beta values with the help of table %2 in the data
180 * sheet. So for example if NTC beta is %3375 and we want to program hard
181 * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50.
183 * If zero value is given in any of the current and voltage values, the
184 * factory programmed default will be used. For soft/hard temperature
185 * values, pass in %SMB3XX_TEMP_USE_DEFAULT instead.
187 struct smb347_charger
{
189 struct regmap
*regmap
;
190 struct power_supply
*mains
;
191 struct power_supply
*usb
;
195 bool charging_enabled
;
197 unsigned int max_charge_current
;
198 unsigned int max_charge_voltage
;
199 unsigned int pre_charge_current
;
200 unsigned int termination_current
;
201 unsigned int pre_to_fast_voltage
;
202 unsigned int mains_current_limit
;
203 unsigned int usb_hc_current_limit
;
204 unsigned int chip_temp_threshold
;
205 int soft_cold_temp_limit
;
206 int soft_hot_temp_limit
;
207 int hard_cold_temp_limit
;
208 int hard_hot_temp_limit
;
209 bool suspend_on_hard_temp_limit
;
210 unsigned int soft_temp_limit_compensation
;
211 unsigned int charge_current_compensation
;
215 unsigned int enable_control
;
218 enum smb_charger_chipid
{
225 /* Fast charge current in uA */
226 static const unsigned int fcc_tbl
[NUM_CHIP_TYPES
][8] = {
227 [SMB345
] = { 200000, 450000, 600000, 900000,
228 1300000, 1500000, 1800000, 2000000 },
229 [SMB347
] = { 700000, 900000, 1200000, 1500000,
230 1800000, 2000000, 2200000, 2500000 },
231 [SMB358
] = { 200000, 450000, 600000, 900000,
232 1300000, 1500000, 1800000, 2000000 },
234 /* Pre-charge current in uA */
235 static const unsigned int pcc_tbl
[NUM_CHIP_TYPES
][4] = {
236 [SMB345
] = { 150000, 250000, 350000, 450000 },
237 [SMB347
] = { 100000, 150000, 200000, 250000 },
238 [SMB358
] = { 150000, 250000, 350000, 450000 },
241 /* Termination current in uA */
242 static const unsigned int tc_tbl
[NUM_CHIP_TYPES
][8] = {
243 [SMB345
] = { 30000, 40000, 60000, 80000,
244 100000, 125000, 150000, 200000 },
245 [SMB347
] = { 37500, 50000, 100000, 150000,
246 200000, 250000, 500000, 600000 },
247 [SMB358
] = { 30000, 40000, 60000, 80000,
248 100000, 125000, 150000, 200000 },
251 /* Input current limit in uA */
252 static const unsigned int icl_tbl
[NUM_CHIP_TYPES
][10] = {
253 [SMB345
] = { 300000, 500000, 700000, 1000000, 1500000,
254 1800000, 2000000, 2000000, 2000000, 2000000 },
255 [SMB347
] = { 300000, 500000, 700000, 900000, 1200000,
256 1500000, 1800000, 2000000, 2200000, 2500000 },
257 [SMB358
] = { 300000, 500000, 700000, 1000000, 1500000,
258 1800000, 2000000, 2000000, 2000000, 2000000 },
261 /* Charge current compensation in uA */
262 static const unsigned int ccc_tbl
[NUM_CHIP_TYPES
][4] = {
263 [SMB345
] = { 200000, 450000, 600000, 900000 },
264 [SMB347
] = { 250000, 700000, 900000, 1200000 },
265 [SMB358
] = { 200000, 450000, 600000, 900000 },
268 /* Convert register value to current using lookup table */
269 static int hw_to_current(const unsigned int *tbl
, size_t size
, unsigned int val
)
276 /* Convert current to register value using lookup table */
277 static int current_to_hw(const unsigned int *tbl
, size_t size
, unsigned int val
)
281 for (i
= 0; i
< size
; i
++)
284 return i
> 0 ? i
- 1 : -EINVAL
;
288 * smb347_update_ps_status - refreshes the power source status
289 * @smb: pointer to smb347 charger instance
291 * Function checks whether any power source is connected to the charger and
292 * updates internal state accordingly. If there is a change to previous state
293 * function returns %1, otherwise %0 and negative errno in case of errror.
295 static int smb347_update_ps_status(struct smb347_charger
*smb
)
302 ret
= regmap_read(smb
->regmap
, IRQSTAT_E
, &val
);
307 * Dc and usb are set depending on whether they are enabled in
308 * platform data _and_ whether corresponding undervoltage is set.
311 dc
= !(val
& IRQSTAT_E_DCIN_UV_STAT
);
313 usb
= !(val
& IRQSTAT_E_USBIN_UV_STAT
);
315 ret
= smb
->mains_online
!= dc
|| smb
->usb_online
!= usb
;
316 smb
->mains_online
= dc
;
317 smb
->usb_online
= usb
;
323 * smb347_is_ps_online - returns whether input power source is connected
324 * @smb: pointer to smb347 charger instance
326 * Returns %true if input power source is connected. Note that this is
327 * dependent on what platform has configured for usable power sources. For
328 * example if USB is disabled, this will return %false even if the USB cable
331 static bool smb347_is_ps_online(struct smb347_charger
*smb
)
333 return smb
->usb_online
|| smb
->mains_online
;
337 * smb347_charging_status - returns status of charging
338 * @smb: pointer to smb347 charger instance
340 * Function returns charging status. %0 means no charging is in progress,
341 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
343 static int smb347_charging_status(struct smb347_charger
*smb
)
348 if (!smb347_is_ps_online(smb
))
351 ret
= regmap_read(smb
->regmap
, STAT_C
, &val
);
355 return (val
& STAT_C_CHG_MASK
) >> STAT_C_CHG_SHIFT
;
358 static int smb347_charging_set(struct smb347_charger
*smb
, bool enable
)
362 if (smb
->enable_control
!= SMB3XX_CHG_ENABLE_SW
) {
363 dev_dbg(smb
->dev
, "charging enable/disable in SW disabled\n");
367 if (smb
->charging_enabled
!= enable
) {
368 ret
= regmap_update_bits(smb
->regmap
, CMD_A
, CMD_A_CHG_ENABLED
,
369 enable
? CMD_A_CHG_ENABLED
: 0);
371 smb
->charging_enabled
= enable
;
377 static inline int smb347_charging_enable(struct smb347_charger
*smb
)
379 return smb347_charging_set(smb
, true);
382 static inline int smb347_charging_disable(struct smb347_charger
*smb
)
384 return smb347_charging_set(smb
, false);
387 static int smb347_start_stop_charging(struct smb347_charger
*smb
)
392 * Depending on whether valid power source is connected or not, we
393 * disable or enable the charging. We do it manually because it
394 * depends on how the platform has configured the valid inputs.
396 if (smb347_is_ps_online(smb
)) {
397 ret
= smb347_charging_enable(smb
);
399 dev_err(smb
->dev
, "failed to enable charging\n");
401 ret
= smb347_charging_disable(smb
);
403 dev_err(smb
->dev
, "failed to disable charging\n");
409 static int smb347_set_charge_current(struct smb347_charger
*smb
)
411 unsigned int id
= smb
->id
;
414 if (smb
->max_charge_current
) {
415 ret
= current_to_hw(fcc_tbl
[id
], ARRAY_SIZE(fcc_tbl
[id
]),
416 smb
->max_charge_current
);
420 ret
= regmap_update_bits(smb
->regmap
, CFG_CHARGE_CURRENT
,
421 CFG_CHARGE_CURRENT_FCC_MASK
,
422 ret
<< CFG_CHARGE_CURRENT_FCC_SHIFT
);
427 if (smb
->pre_charge_current
) {
428 ret
= current_to_hw(pcc_tbl
[id
], ARRAY_SIZE(pcc_tbl
[id
]),
429 smb
->pre_charge_current
);
433 ret
= regmap_update_bits(smb
->regmap
, CFG_CHARGE_CURRENT
,
434 CFG_CHARGE_CURRENT_PCC_MASK
,
435 ret
<< CFG_CHARGE_CURRENT_PCC_SHIFT
);
440 if (smb
->termination_current
) {
441 ret
= current_to_hw(tc_tbl
[id
], ARRAY_SIZE(tc_tbl
[id
]),
442 smb
->termination_current
);
446 ret
= regmap_update_bits(smb
->regmap
, CFG_CHARGE_CURRENT
,
447 CFG_CHARGE_CURRENT_TC_MASK
, ret
);
455 static int smb347_set_current_limits(struct smb347_charger
*smb
)
457 unsigned int id
= smb
->id
;
460 if (smb
->mains_current_limit
) {
461 ret
= current_to_hw(icl_tbl
[id
], ARRAY_SIZE(icl_tbl
[id
]),
462 smb
->mains_current_limit
);
466 ret
= regmap_update_bits(smb
->regmap
, CFG_CURRENT_LIMIT
,
467 CFG_CURRENT_LIMIT_DC_MASK
,
468 ret
<< CFG_CURRENT_LIMIT_DC_SHIFT
);
473 if (smb
->usb_hc_current_limit
) {
474 ret
= current_to_hw(icl_tbl
[id
], ARRAY_SIZE(icl_tbl
[id
]),
475 smb
->usb_hc_current_limit
);
479 ret
= regmap_update_bits(smb
->regmap
, CFG_CURRENT_LIMIT
,
480 CFG_CURRENT_LIMIT_USB_MASK
, ret
);
488 static int smb347_set_voltage_limits(struct smb347_charger
*smb
)
492 if (smb
->pre_to_fast_voltage
) {
493 ret
= smb
->pre_to_fast_voltage
;
496 ret
= clamp_val(ret
, 2400000, 3000000) - 2400000;
499 ret
= regmap_update_bits(smb
->regmap
, CFG_FLOAT_VOLTAGE
,
500 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK
,
501 ret
<< CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT
);
506 if (smb
->max_charge_voltage
) {
507 ret
= smb
->max_charge_voltage
;
510 ret
= clamp_val(ret
, 3500000, 4500000) - 3500000;
513 ret
= regmap_update_bits(smb
->regmap
, CFG_FLOAT_VOLTAGE
,
514 CFG_FLOAT_VOLTAGE_FLOAT_MASK
, ret
);
522 static int smb347_set_temp_limits(struct smb347_charger
*smb
)
524 unsigned int id
= smb
->id
;
525 bool enable_therm_monitor
= false;
529 if (smb
->chip_temp_threshold
) {
530 val
= smb
->chip_temp_threshold
;
533 val
= clamp_val(val
, 100, 130) - 100;
536 ret
= regmap_update_bits(smb
->regmap
, CFG_OTG
,
537 CFG_OTG_TEMP_THRESHOLD_MASK
,
538 val
<< CFG_OTG_TEMP_THRESHOLD_SHIFT
);
543 if (smb
->soft_cold_temp_limit
!= SMB3XX_TEMP_USE_DEFAULT
) {
544 val
= smb
->soft_cold_temp_limit
;
546 val
= clamp_val(val
, 0, 15);
548 /* this goes from higher to lower so invert the value */
551 ret
= regmap_update_bits(smb
->regmap
, CFG_TEMP_LIMIT
,
552 CFG_TEMP_LIMIT_SOFT_COLD_MASK
,
553 val
<< CFG_TEMP_LIMIT_SOFT_COLD_SHIFT
);
557 enable_therm_monitor
= true;
560 if (smb
->soft_hot_temp_limit
!= SMB3XX_TEMP_USE_DEFAULT
) {
561 val
= smb
->soft_hot_temp_limit
;
563 val
= clamp_val(val
, 40, 55) - 40;
566 ret
= regmap_update_bits(smb
->regmap
, CFG_TEMP_LIMIT
,
567 CFG_TEMP_LIMIT_SOFT_HOT_MASK
,
568 val
<< CFG_TEMP_LIMIT_SOFT_HOT_SHIFT
);
572 enable_therm_monitor
= true;
575 if (smb
->hard_cold_temp_limit
!= SMB3XX_TEMP_USE_DEFAULT
) {
576 val
= smb
->hard_cold_temp_limit
;
578 val
= clamp_val(val
, -5, 10) + 5;
580 /* this goes from higher to lower so invert the value */
583 ret
= regmap_update_bits(smb
->regmap
, CFG_TEMP_LIMIT
,
584 CFG_TEMP_LIMIT_HARD_COLD_MASK
,
585 val
<< CFG_TEMP_LIMIT_HARD_COLD_SHIFT
);
589 enable_therm_monitor
= true;
592 if (smb
->hard_hot_temp_limit
!= SMB3XX_TEMP_USE_DEFAULT
) {
593 val
= smb
->hard_hot_temp_limit
;
595 val
= clamp_val(val
, 50, 65) - 50;
598 ret
= regmap_update_bits(smb
->regmap
, CFG_TEMP_LIMIT
,
599 CFG_TEMP_LIMIT_HARD_HOT_MASK
,
600 val
<< CFG_TEMP_LIMIT_HARD_HOT_SHIFT
);
604 enable_therm_monitor
= true;
608 * If any of the temperature limits are set, we also enable the
609 * thermistor monitoring.
611 * When soft limits are hit, the device will start to compensate
612 * current and/or voltage depending on the configuration.
614 * When hard limit is hit, the device will suspend charging
615 * depending on the configuration.
617 if (enable_therm_monitor
) {
618 ret
= regmap_update_bits(smb
->regmap
, CFG_THERM
,
619 CFG_THERM_MONITOR_DISABLED
, 0);
624 if (smb
->suspend_on_hard_temp_limit
) {
625 ret
= regmap_update_bits(smb
->regmap
, CFG_SYSOK
,
626 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED
, 0);
631 if (smb
->soft_temp_limit_compensation
!=
632 SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT
) {
633 val
= smb
->soft_temp_limit_compensation
& 0x3;
635 ret
= regmap_update_bits(smb
->regmap
, CFG_THERM
,
636 CFG_THERM_SOFT_HOT_COMPENSATION_MASK
,
637 val
<< CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT
);
641 ret
= regmap_update_bits(smb
->regmap
, CFG_THERM
,
642 CFG_THERM_SOFT_COLD_COMPENSATION_MASK
,
643 val
<< CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT
);
648 if (smb
->charge_current_compensation
) {
649 val
= current_to_hw(ccc_tbl
[id
], ARRAY_SIZE(ccc_tbl
[id
]),
650 smb
->charge_current_compensation
);
654 ret
= regmap_update_bits(smb
->regmap
, CFG_OTG
,
655 CFG_OTG_CC_COMPENSATION_MASK
,
656 (val
& 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT
);
665 * smb347_set_writable - enables/disables writing to non-volatile registers
666 * @smb: pointer to smb347 charger instance
668 * You can enable/disable writing to the non-volatile configuration
669 * registers by calling this function.
671 * Returns %0 on success and negative errno in case of failure.
673 static int smb347_set_writable(struct smb347_charger
*smb
, bool writable
)
675 return regmap_update_bits(smb
->regmap
, CMD_A
, CMD_A_ALLOW_WRITE
,
676 writable
? CMD_A_ALLOW_WRITE
: 0);
679 static int smb347_hw_init(struct smb347_charger
*smb
)
684 ret
= smb347_set_writable(smb
, true);
689 * Program the platform specific configuration values to the device
692 ret
= smb347_set_charge_current(smb
);
696 ret
= smb347_set_current_limits(smb
);
700 ret
= smb347_set_voltage_limits(smb
);
704 ret
= smb347_set_temp_limits(smb
);
708 /* If USB charging is disabled we put the USB in suspend mode */
710 ret
= regmap_update_bits(smb
->regmap
, CMD_A
,
711 CMD_A_SUSPEND_ENABLED
,
712 CMD_A_SUSPEND_ENABLED
);
718 * If configured by platform data, we enable hardware Auto-OTG
719 * support for driving VBUS. Otherwise we disable it.
721 ret
= regmap_update_bits(smb
->regmap
, CFG_OTHER
, CFG_OTHER_RID_MASK
,
722 smb
->use_usb_otg
? CFG_OTHER_RID_ENABLED_AUTO_OTG
: 0);
727 * Make the charging functionality controllable by a write to the
728 * command register unless pin control is specified in the platform
731 switch (smb
->enable_control
) {
732 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW
:
733 val
= CFG_PIN_EN_CTRL_ACTIVE_LOW
;
735 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH
:
736 val
= CFG_PIN_EN_CTRL_ACTIVE_HIGH
;
743 ret
= regmap_update_bits(smb
->regmap
, CFG_PIN
, CFG_PIN_EN_CTRL_MASK
,
748 /* Disable Automatic Power Source Detection (APSD) interrupt. */
749 ret
= regmap_update_bits(smb
->regmap
, CFG_PIN
, CFG_PIN_EN_APSD_IRQ
, 0);
753 ret
= smb347_update_ps_status(smb
);
757 ret
= smb347_start_stop_charging(smb
);
760 smb347_set_writable(smb
, false);
764 static irqreturn_t
smb347_interrupt(int irq
, void *data
)
766 struct smb347_charger
*smb
= data
;
767 unsigned int stat_c
, irqstat_c
, irqstat_d
, irqstat_e
;
768 bool handled
= false;
771 /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */
772 usleep_range(25000, 35000);
774 ret
= regmap_read(smb
->regmap
, STAT_C
, &stat_c
);
776 dev_warn(smb
->dev
, "reading STAT_C failed\n");
780 ret
= regmap_read(smb
->regmap
, IRQSTAT_C
, &irqstat_c
);
782 dev_warn(smb
->dev
, "reading IRQSTAT_C failed\n");
786 ret
= regmap_read(smb
->regmap
, IRQSTAT_D
, &irqstat_d
);
788 dev_warn(smb
->dev
, "reading IRQSTAT_D failed\n");
792 ret
= regmap_read(smb
->regmap
, IRQSTAT_E
, &irqstat_e
);
794 dev_warn(smb
->dev
, "reading IRQSTAT_E failed\n");
799 * If we get charger error we report the error back to user.
800 * If the error is recovered charging will resume again.
802 if (stat_c
& STAT_C_CHARGER_ERROR
) {
803 dev_err(smb
->dev
, "charging stopped due to charger error\n");
805 power_supply_changed(smb
->mains
);
807 power_supply_changed(smb
->usb
);
812 * If we reached the termination current the battery is charged and
813 * we can update the status now. Charging is automatically
814 * disabled by the hardware.
816 if (irqstat_c
& (IRQSTAT_C_TERMINATION_IRQ
| IRQSTAT_C_TAPER_IRQ
)) {
817 if (irqstat_c
& IRQSTAT_C_TERMINATION_STAT
) {
819 power_supply_changed(smb
->mains
);
821 power_supply_changed(smb
->usb
);
823 dev_dbg(smb
->dev
, "going to HW maintenance mode\n");
828 * If we got a charger timeout INT that means the charge
829 * full is not detected with in charge timeout value.
831 if (irqstat_d
& IRQSTAT_D_CHARGE_TIMEOUT_IRQ
) {
832 dev_dbg(smb
->dev
, "total Charge Timeout INT received\n");
834 if (irqstat_d
& IRQSTAT_D_CHARGE_TIMEOUT_STAT
)
835 dev_warn(smb
->dev
, "charging stopped due to timeout\n");
837 power_supply_changed(smb
->mains
);
839 power_supply_changed(smb
->usb
);
844 * If we got an under voltage interrupt it means that AC/USB input
845 * was connected or disconnected.
847 if (irqstat_e
& (IRQSTAT_E_USBIN_UV_IRQ
| IRQSTAT_E_DCIN_UV_IRQ
)) {
848 if (smb347_update_ps_status(smb
) > 0) {
849 smb347_start_stop_charging(smb
);
851 power_supply_changed(smb
->mains
);
853 power_supply_changed(smb
->usb
);
858 return handled
? IRQ_HANDLED
: IRQ_NONE
;
861 static int smb347_irq_set(struct smb347_charger
*smb
, bool enable
)
865 ret
= smb347_set_writable(smb
, true);
870 * Enable/disable interrupts for:
872 * - termination current reached
876 ret
= regmap_update_bits(smb
->regmap
, CFG_FAULT_IRQ
, 0xff,
877 enable
? CFG_FAULT_IRQ_DCIN_UV
: 0);
881 ret
= regmap_update_bits(smb
->regmap
, CFG_STATUS_IRQ
, 0xff,
882 enable
? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER
|
883 CFG_STATUS_IRQ_CHARGE_TIMEOUT
) : 0);
887 ret
= regmap_update_bits(smb
->regmap
, CFG_PIN
, CFG_PIN_EN_CHARGER_ERROR
,
888 enable
? CFG_PIN_EN_CHARGER_ERROR
: 0);
890 smb347_set_writable(smb
, false);
894 static inline int smb347_irq_enable(struct smb347_charger
*smb
)
896 return smb347_irq_set(smb
, true);
899 static inline int smb347_irq_disable(struct smb347_charger
*smb
)
901 return smb347_irq_set(smb
, false);
904 static int smb347_irq_init(struct smb347_charger
*smb
,
905 struct i2c_client
*client
)
909 ret
= devm_request_threaded_irq(smb
->dev
, client
->irq
, NULL
,
910 smb347_interrupt
, IRQF_ONESHOT
,
915 ret
= smb347_set_writable(smb
, true);
920 * Configure the STAT output to be suitable for interrupts: disable
921 * all other output (except interrupts) and make it active low.
923 ret
= regmap_update_bits(smb
->regmap
, CFG_STAT
,
924 CFG_STAT_ACTIVE_HIGH
| CFG_STAT_DISABLED
,
929 smb347_set_writable(smb
, false);
935 * Returns the constant charge current programmed
936 * into the charger in uA.
938 static int get_const_charge_current(struct smb347_charger
*smb
)
940 unsigned int id
= smb
->id
;
944 if (!smb347_is_ps_online(smb
))
947 ret
= regmap_read(smb
->regmap
, STAT_B
, &v
);
952 * The current value is composition of FCC and PCC values
953 * and we can detect which table to use from bit 5.
956 intval
= hw_to_current(fcc_tbl
[id
],
957 ARRAY_SIZE(fcc_tbl
[id
]), v
& 7);
960 intval
= hw_to_current(pcc_tbl
[id
],
961 ARRAY_SIZE(pcc_tbl
[id
]), v
& 7);
968 * Returns the constant charge voltage programmed
969 * into the charger in uV.
971 static int get_const_charge_voltage(struct smb347_charger
*smb
)
976 if (!smb347_is_ps_online(smb
))
979 ret
= regmap_read(smb
->regmap
, STAT_A
, &v
);
983 v
&= STAT_A_FLOAT_VOLTAGE_MASK
;
987 intval
= 3500000 + v
* 20000;
992 static int smb347_get_charging_status(struct smb347_charger
*smb
,
993 struct power_supply
*psy
)
998 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
) {
999 if (!smb
->usb_online
)
1000 return POWER_SUPPLY_STATUS_DISCHARGING
;
1002 if (!smb
->mains_online
)
1003 return POWER_SUPPLY_STATUS_DISCHARGING
;
1006 ret
= regmap_read(smb
->regmap
, STAT_C
, &val
);
1010 if ((val
& STAT_C_CHARGER_ERROR
) ||
1011 (val
& STAT_C_HOLDOFF_STAT
)) {
1013 * set to NOT CHARGING upon charger error
1014 * or charging has stopped.
1016 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1018 if ((val
& STAT_C_CHG_MASK
) >> STAT_C_CHG_SHIFT
) {
1020 * set to charging if battery is in pre-charge,
1021 * fast charge or taper charging mode.
1023 status
= POWER_SUPPLY_STATUS_CHARGING
;
1024 } else if (val
& STAT_C_CHG_TERM
) {
1026 * set the status to FULL if battery is not in pre
1027 * charge, fast charge or taper charging mode AND
1028 * charging is terminated at least once.
1030 status
= POWER_SUPPLY_STATUS_FULL
;
1033 * in this case no charger error or termination
1034 * occured but charging is not in progress!!!
1036 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1043 static int smb347_get_property_locked(struct power_supply
*psy
,
1044 enum power_supply_property prop
,
1045 union power_supply_propval
*val
)
1047 struct smb347_charger
*smb
= power_supply_get_drvdata(psy
);
1051 case POWER_SUPPLY_PROP_STATUS
:
1052 ret
= smb347_get_charging_status(smb
, psy
);
1058 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
1059 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
) {
1060 if (!smb
->usb_online
)
1063 if (!smb
->mains_online
)
1068 * We handle trickle and pre-charging the same, and taper
1069 * and none the same.
1071 switch (smb347_charging_status(smb
)) {
1073 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
1076 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
1079 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
1084 case POWER_SUPPLY_PROP_ONLINE
:
1085 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
1086 val
->intval
= smb
->usb_online
;
1088 val
->intval
= smb
->mains_online
;
1091 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
1092 ret
= get_const_charge_voltage(smb
);
1098 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
1099 ret
= get_const_charge_current(smb
);
1112 static int smb347_get_property(struct power_supply
*psy
,
1113 enum power_supply_property prop
,
1114 union power_supply_propval
*val
)
1116 struct smb347_charger
*smb
= power_supply_get_drvdata(psy
);
1117 struct i2c_client
*client
= to_i2c_client(smb
->dev
);
1120 disable_irq(client
->irq
);
1121 ret
= smb347_get_property_locked(psy
, prop
, val
);
1122 enable_irq(client
->irq
);
1127 static enum power_supply_property smb347_properties
[] = {
1128 POWER_SUPPLY_PROP_STATUS
,
1129 POWER_SUPPLY_PROP_CHARGE_TYPE
,
1130 POWER_SUPPLY_PROP_ONLINE
,
1131 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
1132 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
1135 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 void smb347_dt_parse_dev_info(struct smb347_charger
*smb
)
1180 struct device
*dev
= smb
->dev
;
1182 smb
->soft_temp_limit_compensation
=
1183 SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT
;
1185 * These properties come from the battery info, still we need to
1186 * pre-initialize the values. See smb347_get_battery_info() below.
1188 smb
->soft_cold_temp_limit
= SMB3XX_TEMP_USE_DEFAULT
;
1189 smb
->hard_cold_temp_limit
= SMB3XX_TEMP_USE_DEFAULT
;
1190 smb
->soft_hot_temp_limit
= SMB3XX_TEMP_USE_DEFAULT
;
1191 smb
->hard_hot_temp_limit
= SMB3XX_TEMP_USE_DEFAULT
;
1193 /* Charging constraints */
1194 device_property_read_u32(dev
, "summit,fast-voltage-threshold-microvolt",
1195 &smb
->pre_to_fast_voltage
);
1196 device_property_read_u32(dev
, "summit,mains-current-limit-microamp",
1197 &smb
->mains_current_limit
);
1198 device_property_read_u32(dev
, "summit,usb-current-limit-microamp",
1199 &smb
->usb_hc_current_limit
);
1201 /* For thermometer monitoring */
1202 device_property_read_u32(dev
, "summit,chip-temperature-threshold-celsius",
1203 &smb
->chip_temp_threshold
);
1204 device_property_read_u32(dev
, "summit,soft-compensation-method",
1205 &smb
->soft_temp_limit_compensation
);
1206 device_property_read_u32(dev
, "summit,charge-current-compensation-microamp",
1207 &smb
->charge_current_compensation
);
1209 /* Supported charging mode */
1210 smb
->use_mains
= device_property_read_bool(dev
, "summit,enable-mains-charging");
1211 smb
->use_usb
= device_property_read_bool(dev
, "summit,enable-usb-charging");
1212 smb
->use_usb_otg
= device_property_read_bool(dev
, "summit,enable-otg-charging");
1214 /* Select charging control */
1215 device_property_read_u32(dev
, "summit,enable-charge-control",
1216 &smb
->enable_control
);
1219 static int smb347_get_battery_info(struct smb347_charger
*smb
)
1221 struct power_supply_battery_info info
= {};
1222 struct power_supply
*supply
;
1226 supply
= smb
->mains
;
1230 err
= power_supply_get_battery_info(supply
, &info
);
1231 if (err
== -ENXIO
|| err
== -ENODEV
)
1236 if (info
.constant_charge_current_max_ua
!= -EINVAL
)
1237 smb
->max_charge_current
= info
.constant_charge_current_max_ua
;
1239 if (info
.constant_charge_voltage_max_uv
!= -EINVAL
)
1240 smb
->max_charge_voltage
= info
.constant_charge_voltage_max_uv
;
1242 if (info
.precharge_current_ua
!= -EINVAL
)
1243 smb
->pre_charge_current
= info
.precharge_current_ua
;
1245 if (info
.charge_term_current_ua
!= -EINVAL
)
1246 smb
->termination_current
= info
.charge_term_current_ua
;
1248 if (info
.temp_alert_min
!= INT_MIN
)
1249 smb
->soft_cold_temp_limit
= info
.temp_alert_min
;
1251 if (info
.temp_alert_max
!= INT_MAX
)
1252 smb
->soft_hot_temp_limit
= info
.temp_alert_max
;
1254 if (info
.temp_min
!= INT_MIN
)
1255 smb
->hard_cold_temp_limit
= info
.temp_min
;
1257 if (info
.temp_max
!= INT_MAX
)
1258 smb
->hard_hot_temp_limit
= info
.temp_max
;
1260 /* Suspend when battery temperature is outside hard limits */
1261 if (smb
->hard_cold_temp_limit
!= SMB3XX_TEMP_USE_DEFAULT
||
1262 smb
->hard_hot_temp_limit
!= SMB3XX_TEMP_USE_DEFAULT
)
1263 smb
->suspend_on_hard_temp_limit
= true;
1268 static const struct regmap_config smb347_regmap
= {
1271 .max_register
= SMB347_MAX_REGISTER
,
1272 .volatile_reg
= smb347_volatile_reg
,
1273 .readable_reg
= smb347_readable_reg
,
1276 static const struct power_supply_desc smb347_mains_desc
= {
1277 .name
= "smb347-mains",
1278 .type
= POWER_SUPPLY_TYPE_MAINS
,
1279 .get_property
= smb347_get_property
,
1280 .properties
= smb347_properties
,
1281 .num_properties
= ARRAY_SIZE(smb347_properties
),
1284 static const struct power_supply_desc smb347_usb_desc
= {
1285 .name
= "smb347-usb",
1286 .type
= POWER_SUPPLY_TYPE_USB
,
1287 .get_property
= smb347_get_property
,
1288 .properties
= smb347_properties
,
1289 .num_properties
= ARRAY_SIZE(smb347_properties
),
1292 static int smb347_probe(struct i2c_client
*client
,
1293 const struct i2c_device_id
*id
)
1295 struct power_supply_config mains_usb_cfg
= {};
1296 struct device
*dev
= &client
->dev
;
1297 struct smb347_charger
*smb
;
1300 smb
= devm_kzalloc(dev
, sizeof(*smb
), GFP_KERNEL
);
1303 smb
->dev
= &client
->dev
;
1304 smb
->id
= id
->driver_data
;
1305 i2c_set_clientdata(client
, smb
);
1307 smb347_dt_parse_dev_info(smb
);
1308 if (!smb
->use_mains
&& !smb
->use_usb
)
1311 smb
->regmap
= devm_regmap_init_i2c(client
, &smb347_regmap
);
1312 if (IS_ERR(smb
->regmap
))
1313 return PTR_ERR(smb
->regmap
);
1315 mains_usb_cfg
.drv_data
= smb
;
1316 mains_usb_cfg
.of_node
= dev
->of_node
;
1317 if (smb
->use_mains
) {
1318 smb
->mains
= devm_power_supply_register(dev
, &smb347_mains_desc
,
1320 if (IS_ERR(smb
->mains
))
1321 return PTR_ERR(smb
->mains
);
1325 smb
->usb
= devm_power_supply_register(dev
, &smb347_usb_desc
,
1327 if (IS_ERR(smb
->usb
))
1328 return PTR_ERR(smb
->usb
);
1331 ret
= smb347_get_battery_info(smb
);
1335 ret
= smb347_hw_init(smb
);
1340 * Interrupt pin is optional. If it is connected, we setup the
1341 * interrupt support here.
1344 ret
= smb347_irq_init(smb
, client
);
1346 dev_warn(dev
, "failed to initialize IRQ: %d\n", ret
);
1347 dev_warn(dev
, "disabling IRQ support\n");
1349 smb347_irq_enable(smb
);
1356 static int smb347_remove(struct i2c_client
*client
)
1358 struct smb347_charger
*smb
= i2c_get_clientdata(client
);
1361 smb347_irq_disable(smb
);
1365 static const struct i2c_device_id smb347_id
[] = {
1366 { "smb345", SMB345
},
1367 { "smb347", SMB347
},
1368 { "smb358", SMB358
},
1371 MODULE_DEVICE_TABLE(i2c
, smb347_id
);
1373 static const struct of_device_id smb3xx_of_match
[] = {
1374 { .compatible
= "summit,smb345" },
1375 { .compatible
= "summit,smb347" },
1376 { .compatible
= "summit,smb358" },
1379 MODULE_DEVICE_TABLE(of
, smb3xx_of_match
);
1381 static struct i2c_driver smb347_driver
= {
1384 .of_match_table
= smb3xx_of_match
,
1386 .probe
= smb347_probe
,
1387 .remove
= smb347_remove
,
1388 .id_table
= smb347_id
,
1391 module_i2c_driver(smb347_driver
);
1393 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1394 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1395 MODULE_DESCRIPTION("SMB347 battery charger driver");
1396 MODULE_LICENSE("GPL");