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 * @irq_unsupported: is interrupt unsupported by SMB hardware
141 * @max_charge_current: maximum current (in uA) the battery can be charged
142 * @max_charge_voltage: maximum voltage (in uV) the battery can be charged
143 * @pre_charge_current: current (in uA) to use in pre-charging phase
144 * @termination_current: current (in uA) used to determine when the
145 * charging cycle terminates
146 * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to
147 * pre-charge to fast charge mode
148 * @mains_current_limit: maximum input current drawn from AC/DC input (in uA)
149 * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB
151 * @chip_temp_threshold: die temperature where device starts limiting charge
152 * current [%100 - %130] (in degree C)
153 * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C),
154 * granularity is 5 deg C.
155 * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree C),
156 * granularity is 5 deg C.
157 * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C),
158 * granularity is 5 deg C.
159 * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C),
160 * granularity is 5 deg C.
161 * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit
162 * @soft_temp_limit_compensation: compensation method when soft temperature
164 * @charge_current_compensation: current (in uA) for charging compensation
165 * current when temperature hits soft limits
166 * @use_mains: AC/DC input can be used
167 * @use_usb: USB input can be used
168 * @use_usb_otg: USB OTG output can be used (not implemented yet)
169 * @enable_control: how charging enable/disable is controlled
170 * (driver/pin controls)
172 * @use_main, @use_usb, and @use_usb_otg are means to enable/disable
173 * hardware support for these. This is useful when we want to have for
174 * example OTG charging controlled via OTG transceiver driver and not by
175 * the SMB347 hardware.
177 * Hard and soft temperature limit values are given as described in the
178 * device data sheet and assuming NTC beta value is %3750. Even if this is
179 * not the case, these values should be used. They can be mapped to the
180 * corresponding NTC beta values with the help of table %2 in the data
181 * sheet. So for example if NTC beta is %3375 and we want to program hard
182 * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50.
184 * If zero value is given in any of the current and voltage values, the
185 * factory programmed default will be used. For soft/hard temperature
186 * values, pass in %SMB3XX_TEMP_USE_DEFAULT instead.
188 struct smb347_charger {
190 struct regmap *regmap;
191 struct power_supply *mains;
192 struct power_supply *usb;
196 bool charging_enabled;
197 bool irq_unsupported;
199 unsigned int max_charge_current;
200 unsigned int max_charge_voltage;
201 unsigned int pre_charge_current;
202 unsigned int termination_current;
203 unsigned int pre_to_fast_voltage;
204 unsigned int mains_current_limit;
205 unsigned int usb_hc_current_limit;
206 unsigned int chip_temp_threshold;
207 int soft_cold_temp_limit;
208 int soft_hot_temp_limit;
209 int hard_cold_temp_limit;
210 int hard_hot_temp_limit;
211 bool suspend_on_hard_temp_limit;
212 unsigned int soft_temp_limit_compensation;
213 unsigned int charge_current_compensation;
217 unsigned int enable_control;
220 enum smb_charger_chipid {
227 /* Fast charge current in uA */
228 static const unsigned int fcc_tbl[NUM_CHIP_TYPES][8] = {
229 [SMB345] = { 200000, 450000, 600000, 900000,
230 1300000, 1500000, 1800000, 2000000 },
231 [SMB347] = { 700000, 900000, 1200000, 1500000,
232 1800000, 2000000, 2200000, 2500000 },
233 [SMB358] = { 200000, 450000, 600000, 900000,
234 1300000, 1500000, 1800000, 2000000 },
236 /* Pre-charge current in uA */
237 static const unsigned int pcc_tbl[NUM_CHIP_TYPES][4] = {
238 [SMB345] = { 150000, 250000, 350000, 450000 },
239 [SMB347] = { 100000, 150000, 200000, 250000 },
240 [SMB358] = { 150000, 250000, 350000, 450000 },
243 /* Termination current in uA */
244 static const unsigned int tc_tbl[NUM_CHIP_TYPES][8] = {
245 [SMB345] = { 30000, 40000, 60000, 80000,
246 100000, 125000, 150000, 200000 },
247 [SMB347] = { 37500, 50000, 100000, 150000,
248 200000, 250000, 500000, 600000 },
249 [SMB358] = { 30000, 40000, 60000, 80000,
250 100000, 125000, 150000, 200000 },
253 /* Input current limit in uA */
254 static const unsigned int icl_tbl[NUM_CHIP_TYPES][10] = {
255 [SMB345] = { 300000, 500000, 700000, 1000000, 1500000,
256 1800000, 2000000, 2000000, 2000000, 2000000 },
257 [SMB347] = { 300000, 500000, 700000, 900000, 1200000,
258 1500000, 1800000, 2000000, 2200000, 2500000 },
259 [SMB358] = { 300000, 500000, 700000, 1000000, 1500000,
260 1800000, 2000000, 2000000, 2000000, 2000000 },
263 /* Charge current compensation in uA */
264 static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = {
265 [SMB345] = { 200000, 450000, 600000, 900000 },
266 [SMB347] = { 250000, 700000, 900000, 1200000 },
267 [SMB358] = { 200000, 450000, 600000, 900000 },
270 /* Convert register value to current using lookup table */
271 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
278 /* Convert current to register value using lookup table */
279 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
283 for (i = 0; i < size; i++)
286 return i > 0 ? i - 1 : -EINVAL;
290 * smb347_update_ps_status - refreshes the power source status
291 * @smb: pointer to smb347 charger instance
293 * Function checks whether any power source is connected to the charger and
294 * updates internal state accordingly. If there is a change to previous state
295 * function returns %1, otherwise %0 and negative errno in case of errror.
297 static int smb347_update_ps_status(struct smb347_charger *smb)
304 ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
309 * Dc and usb are set depending on whether they are enabled in
310 * platform data _and_ whether corresponding undervoltage is set.
313 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
315 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
317 ret = smb->mains_online != dc || smb->usb_online != usb;
318 smb->mains_online = dc;
319 smb->usb_online = usb;
325 * smb347_is_ps_online - returns whether input power source is connected
326 * @smb: pointer to smb347 charger instance
328 * Returns %true if input power source is connected. Note that this is
329 * dependent on what platform has configured for usable power sources. For
330 * example if USB is disabled, this will return %false even if the USB cable
333 static bool smb347_is_ps_online(struct smb347_charger *smb)
335 return smb->usb_online || smb->mains_online;
339 * smb347_charging_status - returns status of charging
340 * @smb: pointer to smb347 charger instance
342 * Function returns charging status. %0 means no charging is in progress,
343 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
345 static int smb347_charging_status(struct smb347_charger *smb)
350 if (!smb347_is_ps_online(smb))
353 ret = regmap_read(smb->regmap, STAT_C, &val);
357 return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
360 static int smb347_charging_set(struct smb347_charger *smb, bool enable)
364 if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) {
365 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
369 if (smb->charging_enabled != enable) {
370 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
371 enable ? CMD_A_CHG_ENABLED : 0);
373 smb->charging_enabled = enable;
379 static inline int smb347_charging_enable(struct smb347_charger *smb)
381 return smb347_charging_set(smb, true);
384 static inline int smb347_charging_disable(struct smb347_charger *smb)
386 return smb347_charging_set(smb, false);
389 static int smb347_start_stop_charging(struct smb347_charger *smb)
394 * Depending on whether valid power source is connected or not, we
395 * disable or enable the charging. We do it manually because it
396 * depends on how the platform has configured the valid inputs.
398 if (smb347_is_ps_online(smb)) {
399 ret = smb347_charging_enable(smb);
401 dev_err(smb->dev, "failed to enable charging\n");
403 ret = smb347_charging_disable(smb);
405 dev_err(smb->dev, "failed to disable charging\n");
411 static int smb347_set_charge_current(struct smb347_charger *smb)
413 unsigned int id = smb->id;
416 if (smb->max_charge_current) {
417 ret = current_to_hw(fcc_tbl[id], ARRAY_SIZE(fcc_tbl[id]),
418 smb->max_charge_current);
422 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
423 CFG_CHARGE_CURRENT_FCC_MASK,
424 ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
429 if (smb->pre_charge_current) {
430 ret = current_to_hw(pcc_tbl[id], ARRAY_SIZE(pcc_tbl[id]),
431 smb->pre_charge_current);
435 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
436 CFG_CHARGE_CURRENT_PCC_MASK,
437 ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
442 if (smb->termination_current) {
443 ret = current_to_hw(tc_tbl[id], ARRAY_SIZE(tc_tbl[id]),
444 smb->termination_current);
448 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
449 CFG_CHARGE_CURRENT_TC_MASK, ret);
457 static int smb347_set_current_limits(struct smb347_charger *smb)
459 unsigned int id = smb->id;
462 if (smb->mains_current_limit) {
463 ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
464 smb->mains_current_limit);
468 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
469 CFG_CURRENT_LIMIT_DC_MASK,
470 ret << CFG_CURRENT_LIMIT_DC_SHIFT);
475 if (smb->usb_hc_current_limit) {
476 ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
477 smb->usb_hc_current_limit);
481 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
482 CFG_CURRENT_LIMIT_USB_MASK, ret);
490 static int smb347_set_voltage_limits(struct smb347_charger *smb)
494 if (smb->pre_to_fast_voltage) {
495 ret = smb->pre_to_fast_voltage;
498 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
501 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
502 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
503 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
508 if (smb->max_charge_voltage) {
509 ret = smb->max_charge_voltage;
512 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
515 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
516 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
524 static int smb347_set_temp_limits(struct smb347_charger *smb)
526 unsigned int id = smb->id;
527 bool enable_therm_monitor = false;
531 if (smb->chip_temp_threshold) {
532 val = smb->chip_temp_threshold;
535 val = clamp_val(val, 100, 130) - 100;
538 ret = regmap_update_bits(smb->regmap, CFG_OTG,
539 CFG_OTG_TEMP_THRESHOLD_MASK,
540 val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
545 if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
546 val = smb->soft_cold_temp_limit;
548 val = clamp_val(val, 0, 15);
550 /* this goes from higher to lower so invert the value */
553 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
554 CFG_TEMP_LIMIT_SOFT_COLD_MASK,
555 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
559 enable_therm_monitor = true;
562 if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
563 val = smb->soft_hot_temp_limit;
565 val = clamp_val(val, 40, 55) - 40;
568 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
569 CFG_TEMP_LIMIT_SOFT_HOT_MASK,
570 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
574 enable_therm_monitor = true;
577 if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
578 val = smb->hard_cold_temp_limit;
580 val = clamp_val(val, -5, 10) + 5;
582 /* this goes from higher to lower so invert the value */
585 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
586 CFG_TEMP_LIMIT_HARD_COLD_MASK,
587 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
591 enable_therm_monitor = true;
594 if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
595 val = smb->hard_hot_temp_limit;
597 val = clamp_val(val, 50, 65) - 50;
600 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
601 CFG_TEMP_LIMIT_HARD_HOT_MASK,
602 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
606 enable_therm_monitor = true;
610 * If any of the temperature limits are set, we also enable the
611 * thermistor monitoring.
613 * When soft limits are hit, the device will start to compensate
614 * current and/or voltage depending on the configuration.
616 * When hard limit is hit, the device will suspend charging
617 * depending on the configuration.
619 if (enable_therm_monitor) {
620 ret = regmap_update_bits(smb->regmap, CFG_THERM,
621 CFG_THERM_MONITOR_DISABLED, 0);
626 if (smb->suspend_on_hard_temp_limit) {
627 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
628 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
633 if (smb->soft_temp_limit_compensation !=
634 SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT) {
635 val = smb->soft_temp_limit_compensation & 0x3;
637 ret = regmap_update_bits(smb->regmap, CFG_THERM,
638 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
639 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
643 ret = regmap_update_bits(smb->regmap, CFG_THERM,
644 CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
645 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
650 if (smb->charge_current_compensation) {
651 val = current_to_hw(ccc_tbl[id], ARRAY_SIZE(ccc_tbl[id]),
652 smb->charge_current_compensation);
656 ret = regmap_update_bits(smb->regmap, CFG_OTG,
657 CFG_OTG_CC_COMPENSATION_MASK,
658 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
667 * smb347_set_writable - enables/disables writing to non-volatile registers
668 * @smb: pointer to smb347 charger instance
670 * You can enable/disable writing to the non-volatile configuration
671 * registers by calling this function.
673 * Returns %0 on success and negative errno in case of failure.
675 static int smb347_set_writable(struct smb347_charger *smb, bool writable)
677 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
678 writable ? CMD_A_ALLOW_WRITE : 0);
681 static int smb347_hw_init(struct smb347_charger *smb)
686 ret = smb347_set_writable(smb, true);
691 * Program the platform specific configuration values to the device
694 ret = smb347_set_charge_current(smb);
698 ret = smb347_set_current_limits(smb);
702 ret = smb347_set_voltage_limits(smb);
706 ret = smb347_set_temp_limits(smb);
710 /* If USB charging is disabled we put the USB in suspend mode */
712 ret = regmap_update_bits(smb->regmap, CMD_A,
713 CMD_A_SUSPEND_ENABLED,
714 CMD_A_SUSPEND_ENABLED);
720 * If configured by platform data, we enable hardware Auto-OTG
721 * support for driving VBUS. Otherwise we disable it.
723 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
724 smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
729 * Make the charging functionality controllable by a write to the
730 * command register unless pin control is specified in the platform
733 switch (smb->enable_control) {
734 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
735 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
737 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
738 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
745 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
750 /* Disable Automatic Power Source Detection (APSD) interrupt. */
751 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
755 ret = smb347_update_ps_status(smb);
759 ret = smb347_start_stop_charging(smb);
762 smb347_set_writable(smb, false);
766 static irqreturn_t smb347_interrupt(int irq, void *data)
768 struct smb347_charger *smb = data;
769 unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
770 bool handled = false;
773 /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */
774 usleep_range(25000, 35000);
776 ret = regmap_read(smb->regmap, STAT_C, &stat_c);
778 dev_warn(smb->dev, "reading STAT_C failed\n");
782 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
784 dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
788 ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d);
790 dev_warn(smb->dev, "reading IRQSTAT_D failed\n");
794 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
796 dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
801 * If we get charger error we report the error back to user.
802 * If the error is recovered charging will resume again.
804 if (stat_c & STAT_C_CHARGER_ERROR) {
805 dev_err(smb->dev, "charging stopped due to charger error\n");
807 power_supply_changed(smb->mains);
809 power_supply_changed(smb->usb);
814 * If we reached the termination current the battery is charged and
815 * we can update the status now. Charging is automatically
816 * disabled by the hardware.
818 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
819 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) {
821 power_supply_changed(smb->mains);
823 power_supply_changed(smb->usb);
825 dev_dbg(smb->dev, "going to HW maintenance mode\n");
830 * If we got a charger timeout INT that means the charge
831 * full is not detected with in charge timeout value.
833 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) {
834 dev_dbg(smb->dev, "total Charge Timeout INT received\n");
836 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
837 dev_warn(smb->dev, "charging stopped due to timeout\n");
839 power_supply_changed(smb->mains);
841 power_supply_changed(smb->usb);
846 * If we got an under voltage interrupt it means that AC/USB input
847 * was connected or disconnected.
849 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
850 if (smb347_update_ps_status(smb) > 0) {
851 smb347_start_stop_charging(smb);
853 power_supply_changed(smb->mains);
855 power_supply_changed(smb->usb);
860 return handled ? IRQ_HANDLED : IRQ_NONE;
863 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
867 if (smb->irq_unsupported)
870 ret = smb347_set_writable(smb, true);
875 * Enable/disable interrupts for:
877 * - termination current reached
881 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
882 enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
886 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
887 enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER |
888 CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0);
892 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
893 enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
895 smb347_set_writable(smb, false);
899 static inline int smb347_irq_enable(struct smb347_charger *smb)
901 return smb347_irq_set(smb, true);
904 static inline int smb347_irq_disable(struct smb347_charger *smb)
906 return smb347_irq_set(smb, false);
909 static int smb347_irq_init(struct smb347_charger *smb,
910 struct i2c_client *client)
914 ret = devm_request_threaded_irq(smb->dev, client->irq, NULL,
915 smb347_interrupt, IRQF_ONESHOT,
920 ret = smb347_set_writable(smb, true);
925 * Configure the STAT output to be suitable for interrupts: disable
926 * all other output (except interrupts) and make it active low.
928 ret = regmap_update_bits(smb->regmap, CFG_STAT,
929 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
932 smb347_set_writable(smb, false);
938 * Returns the constant charge current programmed
939 * into the charger in uA.
941 static int get_const_charge_current(struct smb347_charger *smb)
943 unsigned int id = smb->id;
947 if (!smb347_is_ps_online(smb))
950 ret = regmap_read(smb->regmap, STAT_B, &v);
955 * The current value is composition of FCC and PCC values
956 * and we can detect which table to use from bit 5.
959 intval = hw_to_current(fcc_tbl[id],
960 ARRAY_SIZE(fcc_tbl[id]), v & 7);
963 intval = hw_to_current(pcc_tbl[id],
964 ARRAY_SIZE(pcc_tbl[id]), v & 7);
971 * Returns the constant charge voltage programmed
972 * into the charger in uV.
974 static int get_const_charge_voltage(struct smb347_charger *smb)
979 if (!smb347_is_ps_online(smb))
982 ret = regmap_read(smb->regmap, STAT_A, &v);
986 v &= STAT_A_FLOAT_VOLTAGE_MASK;
990 intval = 3500000 + v * 20000;
995 static int smb347_get_charging_status(struct smb347_charger *smb,
996 struct power_supply *psy)
1001 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1002 if (!smb->usb_online)
1003 return POWER_SUPPLY_STATUS_DISCHARGING;
1005 if (!smb->mains_online)
1006 return POWER_SUPPLY_STATUS_DISCHARGING;
1009 ret = regmap_read(smb->regmap, STAT_C, &val);
1013 if ((val & STAT_C_CHARGER_ERROR) ||
1014 (val & STAT_C_HOLDOFF_STAT)) {
1016 * set to NOT CHARGING upon charger error
1017 * or charging has stopped.
1019 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1021 if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
1023 * set to charging if battery is in pre-charge,
1024 * fast charge or taper charging mode.
1026 status = POWER_SUPPLY_STATUS_CHARGING;
1027 } else if (val & STAT_C_CHG_TERM) {
1029 * set the status to FULL if battery is not in pre
1030 * charge, fast charge or taper charging mode AND
1031 * charging is terminated at least once.
1033 status = POWER_SUPPLY_STATUS_FULL;
1036 * in this case no charger error or termination
1037 * occured but charging is not in progress!!!
1039 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1046 static int smb347_get_property_locked(struct power_supply *psy,
1047 enum power_supply_property prop,
1048 union power_supply_propval *val)
1050 struct smb347_charger *smb = power_supply_get_drvdata(psy);
1054 case POWER_SUPPLY_PROP_STATUS:
1055 ret = smb347_get_charging_status(smb, psy);
1061 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1062 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1063 if (!smb->usb_online)
1066 if (!smb->mains_online)
1071 * We handle trickle and pre-charging the same, and taper
1072 * and none the same.
1074 switch (smb347_charging_status(smb)) {
1076 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1079 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1082 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1087 case POWER_SUPPLY_PROP_ONLINE:
1088 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
1089 val->intval = smb->usb_online;
1091 val->intval = smb->mains_online;
1094 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1095 ret = get_const_charge_voltage(smb);
1101 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1102 ret = get_const_charge_current(smb);
1115 static int smb347_get_property(struct power_supply *psy,
1116 enum power_supply_property prop,
1117 union power_supply_propval *val)
1119 struct smb347_charger *smb = power_supply_get_drvdata(psy);
1120 struct i2c_client *client = to_i2c_client(smb->dev);
1123 disable_irq(client->irq);
1124 ret = smb347_get_property_locked(psy, prop, val);
1125 enable_irq(client->irq);
1130 static enum power_supply_property smb347_properties[] = {
1131 POWER_SUPPLY_PROP_STATUS,
1132 POWER_SUPPLY_PROP_CHARGE_TYPE,
1133 POWER_SUPPLY_PROP_ONLINE,
1134 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1135 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1138 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1156 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1159 case CFG_CHARGE_CURRENT:
1160 case CFG_CURRENT_LIMIT:
1161 case CFG_FLOAT_VOLTAGE:
1168 case CFG_TEMP_LIMIT:
1170 case CFG_STATUS_IRQ:
1178 return smb347_volatile_reg(dev, reg);
1181 static void smb347_dt_parse_dev_info(struct smb347_charger *smb)
1183 struct device *dev = smb->dev;
1185 smb->soft_temp_limit_compensation =
1186 SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT;
1188 * These properties come from the battery info, still we need to
1189 * pre-initialize the values. See smb347_get_battery_info() below.
1191 smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1192 smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1193 smb->soft_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1194 smb->hard_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1196 /* Charging constraints */
1197 device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt",
1198 &smb->pre_to_fast_voltage);
1199 device_property_read_u32(dev, "summit,mains-current-limit-microamp",
1200 &smb->mains_current_limit);
1201 device_property_read_u32(dev, "summit,usb-current-limit-microamp",
1202 &smb->usb_hc_current_limit);
1204 /* For thermometer monitoring */
1205 device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius",
1206 &smb->chip_temp_threshold);
1207 device_property_read_u32(dev, "summit,soft-compensation-method",
1208 &smb->soft_temp_limit_compensation);
1209 device_property_read_u32(dev, "summit,charge-current-compensation-microamp",
1210 &smb->charge_current_compensation);
1212 /* Supported charging mode */
1213 smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging");
1214 smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging");
1215 smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging");
1217 /* Select charging control */
1218 device_property_read_u32(dev, "summit,enable-charge-control",
1219 &smb->enable_control);
1222 static int smb347_get_battery_info(struct smb347_charger *smb)
1224 struct power_supply_battery_info info = {};
1225 struct power_supply *supply;
1229 supply = smb->mains;
1233 err = power_supply_get_battery_info(supply, &info);
1234 if (err == -ENXIO || err == -ENODEV)
1239 if (info.constant_charge_current_max_ua != -EINVAL)
1240 smb->max_charge_current = info.constant_charge_current_max_ua;
1242 if (info.constant_charge_voltage_max_uv != -EINVAL)
1243 smb->max_charge_voltage = info.constant_charge_voltage_max_uv;
1245 if (info.precharge_current_ua != -EINVAL)
1246 smb->pre_charge_current = info.precharge_current_ua;
1248 if (info.charge_term_current_ua != -EINVAL)
1249 smb->termination_current = info.charge_term_current_ua;
1251 if (info.temp_alert_min != INT_MIN)
1252 smb->soft_cold_temp_limit = info.temp_alert_min;
1254 if (info.temp_alert_max != INT_MAX)
1255 smb->soft_hot_temp_limit = info.temp_alert_max;
1257 if (info.temp_min != INT_MIN)
1258 smb->hard_cold_temp_limit = info.temp_min;
1260 if (info.temp_max != INT_MAX)
1261 smb->hard_hot_temp_limit = info.temp_max;
1263 /* Suspend when battery temperature is outside hard limits */
1264 if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT ||
1265 smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT)
1266 smb->suspend_on_hard_temp_limit = true;
1271 static const struct regmap_config smb347_regmap = {
1274 .max_register = SMB347_MAX_REGISTER,
1275 .volatile_reg = smb347_volatile_reg,
1276 .readable_reg = smb347_readable_reg,
1279 static const struct power_supply_desc smb347_mains_desc = {
1280 .name = "smb347-mains",
1281 .type = POWER_SUPPLY_TYPE_MAINS,
1282 .get_property = smb347_get_property,
1283 .properties = smb347_properties,
1284 .num_properties = ARRAY_SIZE(smb347_properties),
1287 static const struct power_supply_desc smb347_usb_desc = {
1288 .name = "smb347-usb",
1289 .type = POWER_SUPPLY_TYPE_USB,
1290 .get_property = smb347_get_property,
1291 .properties = smb347_properties,
1292 .num_properties = ARRAY_SIZE(smb347_properties),
1295 static int smb347_probe(struct i2c_client *client,
1296 const struct i2c_device_id *id)
1298 struct power_supply_config mains_usb_cfg = {};
1299 struct device *dev = &client->dev;
1300 struct smb347_charger *smb;
1303 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1306 smb->dev = &client->dev;
1307 smb->id = id->driver_data;
1308 i2c_set_clientdata(client, smb);
1310 smb347_dt_parse_dev_info(smb);
1311 if (!smb->use_mains && !smb->use_usb)
1314 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1315 if (IS_ERR(smb->regmap))
1316 return PTR_ERR(smb->regmap);
1318 mains_usb_cfg.drv_data = smb;
1319 mains_usb_cfg.of_node = dev->of_node;
1320 if (smb->use_mains) {
1321 smb->mains = devm_power_supply_register(dev, &smb347_mains_desc,
1323 if (IS_ERR(smb->mains))
1324 return PTR_ERR(smb->mains);
1328 smb->usb = devm_power_supply_register(dev, &smb347_usb_desc,
1330 if (IS_ERR(smb->usb))
1331 return PTR_ERR(smb->usb);
1334 ret = smb347_get_battery_info(smb);
1338 ret = smb347_hw_init(smb);
1343 * Interrupt pin is optional. If it is connected, we setup the
1344 * interrupt support here.
1347 ret = smb347_irq_init(smb, client);
1349 dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
1350 dev_warn(dev, "disabling IRQ support\n");
1351 smb->irq_unsupported = true;
1353 smb347_irq_enable(smb);
1360 static int smb347_remove(struct i2c_client *client)
1362 struct smb347_charger *smb = i2c_get_clientdata(client);
1364 smb347_irq_disable(smb);
1369 static const struct i2c_device_id smb347_id[] = {
1370 { "smb345", SMB345 },
1371 { "smb347", SMB347 },
1372 { "smb358", SMB358 },
1375 MODULE_DEVICE_TABLE(i2c, smb347_id);
1377 static const struct of_device_id smb3xx_of_match[] = {
1378 { .compatible = "summit,smb345" },
1379 { .compatible = "summit,smb347" },
1380 { .compatible = "summit,smb358" },
1383 MODULE_DEVICE_TABLE(of, smb3xx_of_match);
1385 static struct i2c_driver smb347_driver = {
1388 .of_match_table = smb3xx_of_match,
1390 .probe = smb347_probe,
1391 .remove = smb347_remove,
1392 .id_table = smb347_id,
1395 module_i2c_driver(smb347_driver);
1397 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1398 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1399 MODULE_DESCRIPTION("SMB347 battery charger driver");
1400 MODULE_LICENSE("GPL");