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/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/power_supply.h>
19 #include <linux/property.h>
20 #include <linux/regmap.h>
22 #include <dt-bindings/power/summit,smb347-charger.h>
24 /* Use the default compensation method */
25 #define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1
27 /* Use default factory programmed value for hard/soft temperature limit */
28 #define SMB3XX_TEMP_USE_DEFAULT -273
31 * Configuration registers. These are mirrored to volatile RAM and can be
32 * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
33 * reloaded from non-volatile registers after POR.
35 #define CFG_CHARGE_CURRENT 0x00
36 #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
37 #define CFG_CHARGE_CURRENT_FCC_SHIFT 5
38 #define CFG_CHARGE_CURRENT_PCC_MASK 0x18
39 #define CFG_CHARGE_CURRENT_PCC_SHIFT 3
40 #define CFG_CHARGE_CURRENT_TC_MASK 0x07
41 #define CFG_CURRENT_LIMIT 0x01
42 #define CFG_CURRENT_LIMIT_DC_MASK 0xf0
43 #define CFG_CURRENT_LIMIT_DC_SHIFT 4
44 #define CFG_CURRENT_LIMIT_USB_MASK 0x0f
45 #define CFG_FLOAT_VOLTAGE 0x03
46 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f
47 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
48 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
50 #define CFG_STAT_DISABLED BIT(5)
51 #define CFG_STAT_ACTIVE_HIGH BIT(7)
53 #define CFG_PIN_EN_CTRL_MASK 0x60
54 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
55 #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
56 #define CFG_PIN_EN_APSD_IRQ BIT(1)
57 #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
58 #define CFG_THERM 0x07
59 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
60 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
61 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
62 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
63 #define CFG_THERM_MONITOR_DISABLED BIT(4)
64 #define CFG_SYSOK 0x08
65 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
66 #define CFG_OTHER 0x09
67 #define CFG_OTHER_RID_MASK 0xc0
68 #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
70 #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
71 #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
72 #define CFG_OTG_CC_COMPENSATION_MASK 0xc0
73 #define CFG_OTG_CC_COMPENSATION_SHIFT 6
74 #define CFG_TEMP_LIMIT 0x0b
75 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
76 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
77 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
78 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
79 #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
80 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
81 #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
82 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
83 #define CFG_FAULT_IRQ 0x0c
84 #define CFG_FAULT_IRQ_DCIN_UV BIT(2)
85 #define CFG_STATUS_IRQ 0x0d
86 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
87 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7)
88 #define CFG_ADDRESS 0x0e
90 /* Command registers */
92 #define CMD_A_CHG_ENABLED BIT(1)
93 #define CMD_A_SUSPEND_ENABLED BIT(2)
94 #define CMD_A_ALLOW_WRITE BIT(7)
98 /* Interrupt Status registers */
99 #define IRQSTAT_A 0x35
100 #define IRQSTAT_C 0x37
101 #define IRQSTAT_C_TERMINATION_STAT BIT(0)
102 #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
103 #define IRQSTAT_C_TAPER_IRQ BIT(3)
104 #define IRQSTAT_D 0x38
105 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2)
106 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3)
107 #define IRQSTAT_E 0x39
108 #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
109 #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
110 #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
111 #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
112 #define IRQSTAT_F 0x3a
114 /* Status registers */
116 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
119 #define STAT_C_CHG_ENABLED BIT(0)
120 #define STAT_C_HOLDOFF_STAT BIT(3)
121 #define STAT_C_CHG_MASK 0x06
122 #define STAT_C_CHG_SHIFT 1
123 #define STAT_C_CHG_TERM BIT(5)
124 #define STAT_C_CHARGER_ERROR BIT(6)
127 #define SMB347_MAX_REGISTER 0x3f
130 * struct smb347_charger - smb347 charger instance
131 * @dev: pointer to device
132 * @regmap: pointer to driver regmap
133 * @mains: power_supply instance for AC/DC power
134 * @usb: power_supply instance for USB power
135 * @id: SMB charger ID
136 * @mains_online: is AC/DC input connected
137 * @usb_online: is USB input connected
138 * @charging_enabled: is charging enabled
139 * @irq_unsupported: is interrupt unsupported by SMB hardware
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;
196 bool irq_unsupported;
198 unsigned int max_charge_current;
199 unsigned int max_charge_voltage;
200 unsigned int pre_charge_current;
201 unsigned int termination_current;
202 unsigned int pre_to_fast_voltage;
203 unsigned int mains_current_limit;
204 unsigned int usb_hc_current_limit;
205 unsigned int chip_temp_threshold;
206 int soft_cold_temp_limit;
207 int soft_hot_temp_limit;
208 int hard_cold_temp_limit;
209 int hard_hot_temp_limit;
210 bool suspend_on_hard_temp_limit;
211 unsigned int soft_temp_limit_compensation;
212 unsigned int charge_current_compensation;
216 unsigned int enable_control;
219 enum smb_charger_chipid {
226 /* Fast charge current in uA */
227 static const unsigned int fcc_tbl[NUM_CHIP_TYPES][8] = {
228 [SMB345] = { 200000, 450000, 600000, 900000,
229 1300000, 1500000, 1800000, 2000000 },
230 [SMB347] = { 700000, 900000, 1200000, 1500000,
231 1800000, 2000000, 2200000, 2500000 },
232 [SMB358] = { 200000, 450000, 600000, 900000,
233 1300000, 1500000, 1800000, 2000000 },
235 /* Pre-charge current in uA */
236 static const unsigned int pcc_tbl[NUM_CHIP_TYPES][4] = {
237 [SMB345] = { 150000, 250000, 350000, 450000 },
238 [SMB347] = { 100000, 150000, 200000, 250000 },
239 [SMB358] = { 150000, 250000, 350000, 450000 },
242 /* Termination current in uA */
243 static const unsigned int tc_tbl[NUM_CHIP_TYPES][8] = {
244 [SMB345] = { 30000, 40000, 60000, 80000,
245 100000, 125000, 150000, 200000 },
246 [SMB347] = { 37500, 50000, 100000, 150000,
247 200000, 250000, 500000, 600000 },
248 [SMB358] = { 30000, 40000, 60000, 80000,
249 100000, 125000, 150000, 200000 },
252 /* Input current limit in uA */
253 static const unsigned int icl_tbl[NUM_CHIP_TYPES][10] = {
254 [SMB345] = { 300000, 500000, 700000, 1000000, 1500000,
255 1800000, 2000000, 2000000, 2000000, 2000000 },
256 [SMB347] = { 300000, 500000, 700000, 900000, 1200000,
257 1500000, 1800000, 2000000, 2200000, 2500000 },
258 [SMB358] = { 300000, 500000, 700000, 1000000, 1500000,
259 1800000, 2000000, 2000000, 2000000, 2000000 },
262 /* Charge current compensation in uA */
263 static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = {
264 [SMB345] = { 200000, 450000, 600000, 900000 },
265 [SMB347] = { 250000, 700000, 900000, 1200000 },
266 [SMB358] = { 200000, 450000, 600000, 900000 },
269 /* Convert register value to current using lookup table */
270 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
277 /* Convert current to register value using lookup table */
278 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
282 for (i = 0; i < size; i++)
285 return i > 0 ? i - 1 : -EINVAL;
289 * smb347_update_ps_status - refreshes the power source status
290 * @smb: pointer to smb347 charger instance
292 * Function checks whether any power source is connected to the charger and
293 * updates internal state accordingly. If there is a change to previous state
294 * function returns %1, otherwise %0 and negative errno in case of errror.
296 static int smb347_update_ps_status(struct smb347_charger *smb)
303 ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
308 * Dc and usb are set depending on whether they are enabled in
309 * platform data _and_ whether corresponding undervoltage is set.
312 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
314 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
316 ret = smb->mains_online != dc || smb->usb_online != usb;
317 smb->mains_online = dc;
318 smb->usb_online = usb;
324 * smb347_is_ps_online - returns whether input power source is connected
325 * @smb: pointer to smb347 charger instance
327 * Returns %true if input power source is connected. Note that this is
328 * dependent on what platform has configured for usable power sources. For
329 * example if USB is disabled, this will return %false even if the USB cable
332 static bool smb347_is_ps_online(struct smb347_charger *smb)
334 return smb->usb_online || smb->mains_online;
338 * smb347_charging_status - returns status of charging
339 * @smb: pointer to smb347 charger instance
341 * Function returns charging status. %0 means no charging is in progress,
342 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
344 static int smb347_charging_status(struct smb347_charger *smb)
349 if (!smb347_is_ps_online(smb))
352 ret = regmap_read(smb->regmap, STAT_C, &val);
356 return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
359 static int smb347_charging_set(struct smb347_charger *smb, bool enable)
363 if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) {
364 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
368 if (smb->charging_enabled != enable) {
369 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
370 enable ? CMD_A_CHG_ENABLED : 0);
372 smb->charging_enabled = enable;
378 static inline int smb347_charging_enable(struct smb347_charger *smb)
380 return smb347_charging_set(smb, true);
383 static inline int smb347_charging_disable(struct smb347_charger *smb)
385 return smb347_charging_set(smb, false);
388 static int smb347_start_stop_charging(struct smb347_charger *smb)
393 * Depending on whether valid power source is connected or not, we
394 * disable or enable the charging. We do it manually because it
395 * depends on how the platform has configured the valid inputs.
397 if (smb347_is_ps_online(smb)) {
398 ret = smb347_charging_enable(smb);
400 dev_err(smb->dev, "failed to enable charging\n");
402 ret = smb347_charging_disable(smb);
404 dev_err(smb->dev, "failed to disable charging\n");
410 static int smb347_set_charge_current(struct smb347_charger *smb)
412 unsigned int id = smb->id;
415 if (smb->max_charge_current) {
416 ret = current_to_hw(fcc_tbl[id], ARRAY_SIZE(fcc_tbl[id]),
417 smb->max_charge_current);
421 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
422 CFG_CHARGE_CURRENT_FCC_MASK,
423 ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
428 if (smb->pre_charge_current) {
429 ret = current_to_hw(pcc_tbl[id], ARRAY_SIZE(pcc_tbl[id]),
430 smb->pre_charge_current);
434 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
435 CFG_CHARGE_CURRENT_PCC_MASK,
436 ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
441 if (smb->termination_current) {
442 ret = current_to_hw(tc_tbl[id], ARRAY_SIZE(tc_tbl[id]),
443 smb->termination_current);
447 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
448 CFG_CHARGE_CURRENT_TC_MASK, ret);
456 static int smb347_set_current_limits(struct smb347_charger *smb)
458 unsigned int id = smb->id;
461 if (smb->mains_current_limit) {
462 ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
463 smb->mains_current_limit);
467 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
468 CFG_CURRENT_LIMIT_DC_MASK,
469 ret << CFG_CURRENT_LIMIT_DC_SHIFT);
474 if (smb->usb_hc_current_limit) {
475 ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
476 smb->usb_hc_current_limit);
480 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
481 CFG_CURRENT_LIMIT_USB_MASK, ret);
489 static int smb347_set_voltage_limits(struct smb347_charger *smb)
493 if (smb->pre_to_fast_voltage) {
494 ret = smb->pre_to_fast_voltage;
497 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
500 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
501 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
502 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
507 if (smb->max_charge_voltage) {
508 ret = smb->max_charge_voltage;
511 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
514 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
515 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
523 static int smb347_set_temp_limits(struct smb347_charger *smb)
525 unsigned int id = smb->id;
526 bool enable_therm_monitor = false;
530 if (smb->chip_temp_threshold) {
531 val = smb->chip_temp_threshold;
534 val = clamp_val(val, 100, 130) - 100;
537 ret = regmap_update_bits(smb->regmap, CFG_OTG,
538 CFG_OTG_TEMP_THRESHOLD_MASK,
539 val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
544 if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
545 val = smb->soft_cold_temp_limit;
547 val = clamp_val(val, 0, 15);
549 /* this goes from higher to lower so invert the value */
552 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
553 CFG_TEMP_LIMIT_SOFT_COLD_MASK,
554 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
558 enable_therm_monitor = true;
561 if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
562 val = smb->soft_hot_temp_limit;
564 val = clamp_val(val, 40, 55) - 40;
567 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
568 CFG_TEMP_LIMIT_SOFT_HOT_MASK,
569 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
573 enable_therm_monitor = true;
576 if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
577 val = smb->hard_cold_temp_limit;
579 val = clamp_val(val, -5, 10) + 5;
581 /* this goes from higher to lower so invert the value */
584 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
585 CFG_TEMP_LIMIT_HARD_COLD_MASK,
586 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
590 enable_therm_monitor = true;
593 if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
594 val = smb->hard_hot_temp_limit;
596 val = clamp_val(val, 50, 65) - 50;
599 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
600 CFG_TEMP_LIMIT_HARD_HOT_MASK,
601 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
605 enable_therm_monitor = true;
609 * If any of the temperature limits are set, we also enable the
610 * thermistor monitoring.
612 * When soft limits are hit, the device will start to compensate
613 * current and/or voltage depending on the configuration.
615 * When hard limit is hit, the device will suspend charging
616 * depending on the configuration.
618 if (enable_therm_monitor) {
619 ret = regmap_update_bits(smb->regmap, CFG_THERM,
620 CFG_THERM_MONITOR_DISABLED, 0);
625 if (smb->suspend_on_hard_temp_limit) {
626 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
627 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
632 if (smb->soft_temp_limit_compensation !=
633 SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT) {
634 val = smb->soft_temp_limit_compensation & 0x3;
636 ret = regmap_update_bits(smb->regmap, CFG_THERM,
637 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
638 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
642 ret = regmap_update_bits(smb->regmap, CFG_THERM,
643 CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
644 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
649 if (smb->charge_current_compensation) {
650 val = current_to_hw(ccc_tbl[id], ARRAY_SIZE(ccc_tbl[id]),
651 smb->charge_current_compensation);
655 ret = regmap_update_bits(smb->regmap, CFG_OTG,
656 CFG_OTG_CC_COMPENSATION_MASK,
657 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
666 * smb347_set_writable - enables/disables writing to non-volatile registers
667 * @smb: pointer to smb347 charger instance
669 * You can enable/disable writing to the non-volatile configuration
670 * registers by calling this function.
672 * Returns %0 on success and negative errno in case of failure.
674 static int smb347_set_writable(struct smb347_charger *smb, bool writable)
676 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
677 writable ? CMD_A_ALLOW_WRITE : 0);
680 static int smb347_hw_init(struct smb347_charger *smb)
685 ret = smb347_set_writable(smb, true);
690 * Program the platform specific configuration values to the device
693 ret = smb347_set_charge_current(smb);
697 ret = smb347_set_current_limits(smb);
701 ret = smb347_set_voltage_limits(smb);
705 ret = smb347_set_temp_limits(smb);
709 /* If USB charging is disabled we put the USB in suspend mode */
711 ret = regmap_update_bits(smb->regmap, CMD_A,
712 CMD_A_SUSPEND_ENABLED,
713 CMD_A_SUSPEND_ENABLED);
719 * If configured by platform data, we enable hardware Auto-OTG
720 * support for driving VBUS. Otherwise we disable it.
722 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
723 smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
728 * Make the charging functionality controllable by a write to the
729 * command register unless pin control is specified in the platform
732 switch (smb->enable_control) {
733 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
734 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
736 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
737 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
744 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
749 /* Disable Automatic Power Source Detection (APSD) interrupt. */
750 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
754 ret = smb347_update_ps_status(smb);
758 ret = smb347_start_stop_charging(smb);
761 smb347_set_writable(smb, false);
765 static irqreturn_t smb347_interrupt(int irq, void *data)
767 struct smb347_charger *smb = data;
768 unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
769 bool handled = false;
772 /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */
773 usleep_range(25000, 35000);
775 ret = regmap_read(smb->regmap, STAT_C, &stat_c);
777 dev_warn(smb->dev, "reading STAT_C failed\n");
781 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
783 dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
787 ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d);
789 dev_warn(smb->dev, "reading IRQSTAT_D failed\n");
793 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
795 dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
800 * If we get charger error we report the error back to user.
801 * If the error is recovered charging will resume again.
803 if (stat_c & STAT_C_CHARGER_ERROR) {
804 dev_err(smb->dev, "charging stopped due to charger error\n");
806 power_supply_changed(smb->mains);
808 power_supply_changed(smb->usb);
813 * If we reached the termination current the battery is charged and
814 * we can update the status now. Charging is automatically
815 * disabled by the hardware.
817 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
818 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) {
820 power_supply_changed(smb->mains);
822 power_supply_changed(smb->usb);
824 dev_dbg(smb->dev, "going to HW maintenance mode\n");
829 * If we got a charger timeout INT that means the charge
830 * full is not detected with in charge timeout value.
832 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) {
833 dev_dbg(smb->dev, "total Charge Timeout INT received\n");
835 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
836 dev_warn(smb->dev, "charging stopped due to timeout\n");
838 power_supply_changed(smb->mains);
840 power_supply_changed(smb->usb);
845 * If we got an under voltage interrupt it means that AC/USB input
846 * was connected or disconnected.
848 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
849 if (smb347_update_ps_status(smb) > 0) {
850 smb347_start_stop_charging(smb);
852 power_supply_changed(smb->mains);
854 power_supply_changed(smb->usb);
859 return handled ? IRQ_HANDLED : IRQ_NONE;
862 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
866 if (smb->irq_unsupported)
869 ret = smb347_set_writable(smb, true);
874 * Enable/disable interrupts for:
876 * - termination current reached
880 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
881 enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
885 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
886 enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER |
887 CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0);
891 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
892 enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
894 smb347_set_writable(smb, false);
898 static inline int smb347_irq_enable(struct smb347_charger *smb)
900 return smb347_irq_set(smb, true);
903 static inline int smb347_irq_disable(struct smb347_charger *smb)
905 return smb347_irq_set(smb, false);
908 static int smb347_irq_init(struct smb347_charger *smb,
909 struct i2c_client *client)
913 smb->irq_unsupported = true;
916 * Interrupt pin is optional. If it is connected, we setup the
917 * interrupt support here.
922 ret = smb347_set_writable(smb, true);
927 * Configure the STAT output to be suitable for interrupts: disable
928 * all other output (except interrupts) and make it active low.
930 ret = regmap_update_bits(smb->regmap, CFG_STAT,
931 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
934 smb347_set_writable(smb, false);
937 dev_warn(smb->dev, "failed to initialize IRQ: %d\n", ret);
938 dev_warn(smb->dev, "disabling IRQ support\n");
942 ret = devm_request_threaded_irq(smb->dev, client->irq, NULL,
943 smb347_interrupt, IRQF_ONESHOT,
948 smb->irq_unsupported = false;
950 ret = smb347_irq_enable(smb);
958 * Returns the constant charge current programmed
959 * into the charger in uA.
961 static int get_const_charge_current(struct smb347_charger *smb)
963 unsigned int id = smb->id;
967 if (!smb347_is_ps_online(smb))
970 ret = regmap_read(smb->regmap, STAT_B, &v);
975 * The current value is composition of FCC and PCC values
976 * and we can detect which table to use from bit 5.
979 intval = hw_to_current(fcc_tbl[id],
980 ARRAY_SIZE(fcc_tbl[id]), v & 7);
983 intval = hw_to_current(pcc_tbl[id],
984 ARRAY_SIZE(pcc_tbl[id]), v & 7);
991 * Returns the constant charge voltage programmed
992 * into the charger in uV.
994 static int get_const_charge_voltage(struct smb347_charger *smb)
999 if (!smb347_is_ps_online(smb))
1002 ret = regmap_read(smb->regmap, STAT_A, &v);
1006 v &= STAT_A_FLOAT_VOLTAGE_MASK;
1010 intval = 3500000 + v * 20000;
1015 static int smb347_get_charging_status(struct smb347_charger *smb,
1016 struct power_supply *psy)
1021 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1022 if (!smb->usb_online)
1023 return POWER_SUPPLY_STATUS_DISCHARGING;
1025 if (!smb->mains_online)
1026 return POWER_SUPPLY_STATUS_DISCHARGING;
1029 ret = regmap_read(smb->regmap, STAT_C, &val);
1033 if ((val & STAT_C_CHARGER_ERROR) ||
1034 (val & STAT_C_HOLDOFF_STAT)) {
1036 * set to NOT CHARGING upon charger error
1037 * or charging has stopped.
1039 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1041 if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
1043 * set to charging if battery is in pre-charge,
1044 * fast charge or taper charging mode.
1046 status = POWER_SUPPLY_STATUS_CHARGING;
1047 } else if (val & STAT_C_CHG_TERM) {
1049 * set the status to FULL if battery is not in pre
1050 * charge, fast charge or taper charging mode AND
1051 * charging is terminated at least once.
1053 status = POWER_SUPPLY_STATUS_FULL;
1056 * in this case no charger error or termination
1057 * occured but charging is not in progress!!!
1059 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1066 static int smb347_get_property_locked(struct power_supply *psy,
1067 enum power_supply_property prop,
1068 union power_supply_propval *val)
1070 struct smb347_charger *smb = power_supply_get_drvdata(psy);
1074 case POWER_SUPPLY_PROP_STATUS:
1075 ret = smb347_get_charging_status(smb, psy);
1081 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1082 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1083 if (!smb->usb_online)
1086 if (!smb->mains_online)
1091 * We handle trickle and pre-charging the same, and taper
1092 * and none the same.
1094 switch (smb347_charging_status(smb)) {
1096 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1099 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1102 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1107 case POWER_SUPPLY_PROP_ONLINE:
1108 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
1109 val->intval = smb->usb_online;
1111 val->intval = smb->mains_online;
1114 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1115 ret = get_const_charge_voltage(smb);
1121 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1122 ret = get_const_charge_current(smb);
1135 static int smb347_get_property(struct power_supply *psy,
1136 enum power_supply_property prop,
1137 union power_supply_propval *val)
1139 struct smb347_charger *smb = power_supply_get_drvdata(psy);
1140 struct i2c_client *client = to_i2c_client(smb->dev);
1143 if (!smb->irq_unsupported)
1144 disable_irq(client->irq);
1146 ret = smb347_get_property_locked(psy, prop, val);
1148 if (!smb->irq_unsupported)
1149 enable_irq(client->irq);
1154 static enum power_supply_property smb347_properties[] = {
1155 POWER_SUPPLY_PROP_STATUS,
1156 POWER_SUPPLY_PROP_CHARGE_TYPE,
1157 POWER_SUPPLY_PROP_ONLINE,
1158 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1159 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1162 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1180 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1183 case CFG_CHARGE_CURRENT:
1184 case CFG_CURRENT_LIMIT:
1185 case CFG_FLOAT_VOLTAGE:
1192 case CFG_TEMP_LIMIT:
1194 case CFG_STATUS_IRQ:
1202 return smb347_volatile_reg(dev, reg);
1205 static void smb347_dt_parse_dev_info(struct smb347_charger *smb)
1207 struct device *dev = smb->dev;
1209 smb->soft_temp_limit_compensation =
1210 SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT;
1212 * These properties come from the battery info, still we need to
1213 * pre-initialize the values. See smb347_get_battery_info() below.
1215 smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1216 smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1217 smb->soft_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1218 smb->hard_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1220 /* Charging constraints */
1221 device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt",
1222 &smb->pre_to_fast_voltage);
1223 device_property_read_u32(dev, "summit,mains-current-limit-microamp",
1224 &smb->mains_current_limit);
1225 device_property_read_u32(dev, "summit,usb-current-limit-microamp",
1226 &smb->usb_hc_current_limit);
1228 /* For thermometer monitoring */
1229 device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius",
1230 &smb->chip_temp_threshold);
1231 device_property_read_u32(dev, "summit,soft-compensation-method",
1232 &smb->soft_temp_limit_compensation);
1233 device_property_read_u32(dev, "summit,charge-current-compensation-microamp",
1234 &smb->charge_current_compensation);
1236 /* Supported charging mode */
1237 smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging");
1238 smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging");
1239 smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging");
1241 /* Select charging control */
1242 device_property_read_u32(dev, "summit,enable-charge-control",
1243 &smb->enable_control);
1246 static int smb347_get_battery_info(struct smb347_charger *smb)
1248 struct power_supply_battery_info info = {};
1249 struct power_supply *supply;
1253 supply = smb->mains;
1257 err = power_supply_get_battery_info(supply, &info);
1258 if (err == -ENXIO || err == -ENODEV)
1263 if (info.constant_charge_current_max_ua != -EINVAL)
1264 smb->max_charge_current = info.constant_charge_current_max_ua;
1266 if (info.constant_charge_voltage_max_uv != -EINVAL)
1267 smb->max_charge_voltage = info.constant_charge_voltage_max_uv;
1269 if (info.precharge_current_ua != -EINVAL)
1270 smb->pre_charge_current = info.precharge_current_ua;
1272 if (info.charge_term_current_ua != -EINVAL)
1273 smb->termination_current = info.charge_term_current_ua;
1275 if (info.temp_alert_min != INT_MIN)
1276 smb->soft_cold_temp_limit = info.temp_alert_min;
1278 if (info.temp_alert_max != INT_MAX)
1279 smb->soft_hot_temp_limit = info.temp_alert_max;
1281 if (info.temp_min != INT_MIN)
1282 smb->hard_cold_temp_limit = info.temp_min;
1284 if (info.temp_max != INT_MAX)
1285 smb->hard_hot_temp_limit = info.temp_max;
1287 /* Suspend when battery temperature is outside hard limits */
1288 if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT ||
1289 smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT)
1290 smb->suspend_on_hard_temp_limit = true;
1295 static const struct regmap_config smb347_regmap = {
1298 .max_register = SMB347_MAX_REGISTER,
1299 .volatile_reg = smb347_volatile_reg,
1300 .readable_reg = smb347_readable_reg,
1303 static const struct power_supply_desc smb347_mains_desc = {
1304 .name = "smb347-mains",
1305 .type = POWER_SUPPLY_TYPE_MAINS,
1306 .get_property = smb347_get_property,
1307 .properties = smb347_properties,
1308 .num_properties = ARRAY_SIZE(smb347_properties),
1311 static const struct power_supply_desc smb347_usb_desc = {
1312 .name = "smb347-usb",
1313 .type = POWER_SUPPLY_TYPE_USB,
1314 .get_property = smb347_get_property,
1315 .properties = smb347_properties,
1316 .num_properties = ARRAY_SIZE(smb347_properties),
1319 static int smb347_probe(struct i2c_client *client,
1320 const struct i2c_device_id *id)
1322 struct power_supply_config mains_usb_cfg = {};
1323 struct device *dev = &client->dev;
1324 struct smb347_charger *smb;
1327 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1330 smb->dev = &client->dev;
1331 smb->id = id->driver_data;
1332 i2c_set_clientdata(client, smb);
1334 smb347_dt_parse_dev_info(smb);
1335 if (!smb->use_mains && !smb->use_usb)
1338 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1339 if (IS_ERR(smb->regmap))
1340 return PTR_ERR(smb->regmap);
1342 mains_usb_cfg.drv_data = smb;
1343 mains_usb_cfg.of_node = dev->of_node;
1344 if (smb->use_mains) {
1345 smb->mains = devm_power_supply_register(dev, &smb347_mains_desc,
1347 if (IS_ERR(smb->mains))
1348 return PTR_ERR(smb->mains);
1352 smb->usb = devm_power_supply_register(dev, &smb347_usb_desc,
1354 if (IS_ERR(smb->usb))
1355 return PTR_ERR(smb->usb);
1358 ret = smb347_get_battery_info(smb);
1362 ret = smb347_hw_init(smb);
1366 ret = smb347_irq_init(smb, client);
1373 static int smb347_remove(struct i2c_client *client)
1375 struct smb347_charger *smb = i2c_get_clientdata(client);
1377 smb347_irq_disable(smb);
1382 static const struct i2c_device_id smb347_id[] = {
1383 { "smb345", SMB345 },
1384 { "smb347", SMB347 },
1385 { "smb358", SMB358 },
1388 MODULE_DEVICE_TABLE(i2c, smb347_id);
1390 static const struct of_device_id smb3xx_of_match[] = {
1391 { .compatible = "summit,smb345" },
1392 { .compatible = "summit,smb347" },
1393 { .compatible = "summit,smb358" },
1396 MODULE_DEVICE_TABLE(of, smb3xx_of_match);
1398 static struct i2c_driver smb347_driver = {
1401 .of_match_table = smb3xx_of_match,
1403 .probe = smb347_probe,
1404 .remove = smb347_remove,
1405 .id_table = smb347_id,
1407 module_i2c_driver(smb347_driver);
1409 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1410 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1411 MODULE_DESCRIPTION("SMB347 battery charger driver");
1412 MODULE_LICENSE("GPL");