16 #include <linux/kernel.h>
17 #include <linux/module.h>
20 #include <linux/i2c.h>
31 #define CFG_CHARGE_CURRENT 0x00
32 #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
33 #define CFG_CHARGE_CURRENT_FCC_SHIFT 5
34 #define CFG_CHARGE_CURRENT_PCC_MASK 0x18
35 #define CFG_CHARGE_CURRENT_PCC_SHIFT 3
36 #define CFG_CHARGE_CURRENT_TC_MASK 0x07
37 #define CFG_CURRENT_LIMIT 0x01
38 #define CFG_CURRENT_LIMIT_DC_MASK 0xf0
39 #define CFG_CURRENT_LIMIT_DC_SHIFT 4
40 #define CFG_CURRENT_LIMIT_USB_MASK 0x0f
41 #define CFG_FLOAT_VOLTAGE 0x03
42 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f
43 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
44 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
46 #define CFG_STAT_DISABLED BIT(5)
47 #define CFG_STAT_ACTIVE_HIGH BIT(7)
49 #define CFG_PIN_EN_CTRL_MASK 0x60
50 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
51 #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
52 #define CFG_PIN_EN_APSD_IRQ BIT(1)
53 #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
54 #define CFG_THERM 0x07
55 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
56 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
57 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
58 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
59 #define CFG_THERM_MONITOR_DISABLED BIT(4)
60 #define CFG_SYSOK 0x08
61 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
62 #define CFG_OTHER 0x09
63 #define CFG_OTHER_RID_MASK 0xc0
64 #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
66 #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
67 #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
68 #define CFG_OTG_CC_COMPENSATION_MASK 0xc0
69 #define CFG_OTG_CC_COMPENSATION_SHIFT 6
70 #define CFG_TEMP_LIMIT 0x0b
71 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
72 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
73 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
74 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
75 #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
76 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
77 #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
78 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
79 #define CFG_FAULT_IRQ 0x0c
80 #define CFG_FAULT_IRQ_DCIN_UV BIT(2)
81 #define CFG_STATUS_IRQ 0x0d
82 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
83 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7)
84 #define CFG_ADDRESS 0x0e
88 #define CMD_A_CHG_ENABLED BIT(1)
89 #define CMD_A_SUSPEND_ENABLED BIT(2)
90 #define CMD_A_ALLOW_WRITE BIT(7)
95 #define IRQSTAT_A 0x35
96 #define IRQSTAT_C 0x37
97 #define IRQSTAT_C_TERMINATION_STAT BIT(0)
98 #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
99 #define IRQSTAT_C_TAPER_IRQ BIT(3)
100 #define IRQSTAT_D 0x38
101 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2)
102 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3)
103 #define IRQSTAT_E 0x39
104 #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
105 #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
106 #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
107 #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
108 #define IRQSTAT_F 0x3a
112 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
115 #define STAT_C_CHG_ENABLED BIT(0)
116 #define STAT_C_HOLDOFF_STAT BIT(3)
117 #define STAT_C_CHG_MASK 0x06
118 #define STAT_C_CHG_SHIFT 1
119 #define STAT_C_CHG_TERM BIT(5)
120 #define STAT_C_CHARGER_ERROR BIT(6)
123 #define SMB347_MAX_REGISTER 0x3f
152 static const unsigned int fcc_tbl[] = {
164 static const unsigned int pcc_tbl[] = {
172 static const unsigned int tc_tbl[] = {
184 static const unsigned int icl_tbl[] = {
198 static const unsigned int ccc_tbl[] = {
206 static int hw_to_current(
const unsigned int *tbl,
size_t size,
unsigned int val)
214 static int current_to_hw(
const unsigned int *tbl,
size_t size,
unsigned int val)
218 for (i = 0; i <
size; i++)
221 return i > 0 ? i - 1 : -
EINVAL;
247 if (smb->
pdata->use_mains)
249 if (smb->
pdata->use_usb)
293 if (!smb347_is_ps_online(smb))
308 dev_dbg(smb->
dev,
"charging enable/disable in SW disabled\n");
323 static inline int smb347_charging_enable(
struct smb347_charger *smb)
325 return smb347_charging_set(smb,
true);
328 static inline int smb347_charging_disable(
struct smb347_charger *smb)
330 return smb347_charging_set(smb,
false);
342 if (smb347_is_ps_online(smb)) {
343 ret = smb347_charging_enable(smb);
345 dev_err(smb->
dev,
"failed to enable charging\n");
347 ret = smb347_charging_disable(smb);
349 dev_err(smb->
dev,
"failed to disable charging\n");
359 if (smb->
pdata->max_charge_current) {
360 ret = current_to_hw(fcc_tbl,
ARRAY_SIZE(fcc_tbl),
361 smb->
pdata->max_charge_current);
372 if (smb->
pdata->pre_charge_current) {
373 ret = current_to_hw(pcc_tbl,
ARRAY_SIZE(pcc_tbl),
374 smb->
pdata->pre_charge_current);
385 if (smb->
pdata->termination_current) {
386 ret = current_to_hw(tc_tbl,
ARRAY_SIZE(tc_tbl),
387 smb->
pdata->termination_current);
404 if (smb->
pdata->mains_current_limit) {
405 ret = current_to_hw(icl_tbl,
ARRAY_SIZE(icl_tbl),
406 smb->
pdata->mains_current_limit);
417 if (smb->
pdata->usb_hc_current_limit) {
418 ret = current_to_hw(icl_tbl,
ARRAY_SIZE(icl_tbl),
419 smb->
pdata->usb_hc_current_limit);
436 if (smb->
pdata->pre_to_fast_voltage) {
437 ret = smb->
pdata->pre_to_fast_voltage;
440 ret =
clamp_val(ret, 2400000, 3000000) - 2400000;
450 if (smb->
pdata->max_charge_voltage) {
451 ret = smb->
pdata->max_charge_voltage;
454 ret =
clamp_val(ret, 3500000, 4500000) - 3500000;
468 bool enable_therm_monitor =
false;
472 if (smb->
pdata->chip_temp_threshold) {
473 val = smb->
pdata->chip_temp_threshold;
487 val = smb->
pdata->soft_cold_temp_limit;
500 enable_therm_monitor =
true;
504 val = smb->
pdata->soft_hot_temp_limit;
515 enable_therm_monitor =
true;
519 val = smb->
pdata->hard_cold_temp_limit;
532 enable_therm_monitor =
true;
536 val = smb->
pdata->hard_hot_temp_limit;
547 enable_therm_monitor =
true;
560 if (enable_therm_monitor) {
567 if (smb->
pdata->suspend_on_hard_temp_limit) {
574 if (smb->
pdata->soft_temp_limit_compensation !=
576 val = smb->
pdata->soft_temp_limit_compensation & 0x3;
591 if (smb->
pdata->charge_current_compensation) {
592 val = current_to_hw(ccc_tbl,
ARRAY_SIZE(ccc_tbl),
593 smb->
pdata->charge_current_compensation);
627 ret = smb347_set_writable(smb,
true);
635 ret = smb347_set_charge_current(smb);
639 ret = smb347_set_current_limits(smb);
643 ret = smb347_set_voltage_limits(smb);
647 ret = smb347_set_temp_limits(smb);
652 if (!smb->
pdata->use_usb) {
674 switch (smb->
pdata->enable_control) {
696 ret = smb347_update_ps_status(smb);
700 ret = smb347_start_stop_charging(smb);
703 smb347_set_writable(smb,
false);
710 unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
711 bool handled =
false;
743 dev_err(smb->
dev,
"charging stopped due to charger error\n");
756 dev_dbg(smb->
dev,
"going to HW maintenance mode\n");
765 dev_dbg(smb->
dev,
"total Charge Timeout INT received\n");
768 dev_warn(smb->
dev,
"charging stopped due to timeout\n");
778 if (smb347_update_ps_status(smb) > 0) {
779 smb347_start_stop_charging(smb);
780 if (smb->
pdata->use_mains)
782 if (smb->
pdata->use_usb)
791 static int smb347_irq_set(
struct smb347_charger *smb,
bool enable)
795 ret = smb347_set_writable(smb,
true);
820 smb347_set_writable(smb,
false);
826 return smb347_irq_set(smb,
true);
831 return smb347_irq_set(smb,
false);
849 ret = smb347_set_writable(smb,
true);
863 smb347_set_writable(smb,
false);
868 smb347_set_writable(smb,
false);
887 if (!smb347_is_ps_online(smb))
899 intval = hw_to_current(fcc_tbl,
ARRAY_SIZE(fcc_tbl), v & 7);
902 intval = hw_to_current(pcc_tbl,
ARRAY_SIZE(pcc_tbl), v & 7);
917 if (!smb347_is_ps_online(smb))
928 intval = 3500000 + v * 20000;
933 static int smb347_mains_get_property(
struct power_supply *psy,
947 ret = get_const_charge_voltage(smb);
955 ret = get_const_charge_current(smb);
975 static int smb347_usb_get_property(
struct power_supply *psy,
989 ret = get_const_charge_voltage(smb);
997 ret = get_const_charge_current(smb);
1017 static int smb347_get_charging_status(
struct smb347_charger *smb)
1022 if (!smb347_is_ps_online(smb))
1029 if ((val & STAT_C_CHARGER_ERROR) ||
1062 static int smb347_battery_get_property(
struct power_supply *psy,
1071 ret = smb347_update_ps_status(smb);
1077 ret = smb347_get_charging_status(smb);
1084 if (!smb347_is_ps_online(smb))
1091 switch (smb347_charging_status(smb)) {
1141 static bool smb347_volatile_reg(
struct device *
dev,
unsigned int reg)
1158 static bool smb347_readable_reg(
struct device *dev,
unsigned int reg)
1180 return smb347_volatile_reg(dev, reg);
1183 static const struct regmap_config smb347_regmap = {
1187 .volatile_reg = smb347_volatile_reg,
1188 .readable_reg = smb347_readable_reg,
1191 static int smb347_probe(
struct i2c_client *client,
1194 static char *battery[] = {
"smb347-battery" };
1211 i2c_set_clientdata(client, smb);
1219 return PTR_ERR(smb->
regmap);
1221 ret = smb347_hw_init(smb);
1225 if (smb->
pdata->use_mains) {
1226 smb->
mains.name =
"smb347-mains";
1228 smb->
mains.get_property = smb347_mains_get_property;
1229 smb->
mains.properties = smb347_mains_properties;
1238 if (smb->
pdata->use_usb) {
1239 smb->
usb.name =
"smb347-usb";
1241 smb->
usb.get_property = smb347_usb_get_property;
1242 smb->
usb.properties = smb347_usb_properties;
1243 smb->
usb.num_properties =
ARRAY_SIZE(smb347_usb_properties);
1248 if (smb->
pdata->use_mains)
1254 smb->
battery.name =
"smb347-battery";
1256 smb->
battery.get_property = smb347_battery_get_property;
1257 smb->
battery.properties = smb347_battery_properties;
1263 if (smb->
pdata->use_usb)
1265 if (smb->
pdata->use_mains)
1275 ret = smb347_irq_init(smb, client);
1277 dev_warn(dev,
"failed to initialize IRQ: %d\n", ret);
1278 dev_warn(dev,
"disabling IRQ support\n");
1280 smb347_irq_enable(smb);
1287 static int smb347_remove(
struct i2c_client *client)
1292 smb347_irq_disable(smb);
1298 if (smb->
pdata->use_usb)
1300 if (smb->
pdata->use_mains)
1315 .probe = smb347_probe,
1317 .id_table = smb347_id,