1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2016, Prodys S.L.
5 * This adds support for sbs-charger compilant chips as defined here:
6 * http://sbs-forum.org/specs/sbc110.pdf
8 * Implemetation based on sbs-battery.c
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/err.h>
15 #include <linux/power_supply.h>
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/gpio.h>
20 #include <linux/regmap.h>
21 #include <linux/of_gpio.h>
22 #include <linux/bitops.h>
24 #define SBS_CHARGER_REG_SPEC_INFO 0x11
25 #define SBS_CHARGER_REG_STATUS 0x13
26 #define SBS_CHARGER_REG_ALARM_WARNING 0x16
28 #define SBS_CHARGER_STATUS_CHARGE_INHIBITED BIT(1)
29 #define SBS_CHARGER_STATUS_RES_COLD BIT(9)
30 #define SBS_CHARGER_STATUS_RES_HOT BIT(10)
31 #define SBS_CHARGER_STATUS_BATTERY_PRESENT BIT(14)
32 #define SBS_CHARGER_STATUS_AC_PRESENT BIT(15)
34 #define SBS_CHARGER_POLL_TIME 500
37 struct i2c_client
*client
;
38 struct power_supply
*power_supply
;
39 struct regmap
*regmap
;
40 struct delayed_work work
;
41 unsigned int last_state
;
44 static int sbs_get_property(struct power_supply
*psy
,
45 enum power_supply_property psp
,
46 union power_supply_propval
*val
)
48 struct sbs_info
*chip
= power_supply_get_drvdata(psy
);
51 reg
= chip
->last_state
;
54 case POWER_SUPPLY_PROP_PRESENT
:
55 val
->intval
= !!(reg
& SBS_CHARGER_STATUS_BATTERY_PRESENT
);
58 case POWER_SUPPLY_PROP_ONLINE
:
59 val
->intval
= !!(reg
& SBS_CHARGER_STATUS_AC_PRESENT
);
62 case POWER_SUPPLY_PROP_STATUS
:
63 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
65 if (!(reg
& SBS_CHARGER_STATUS_BATTERY_PRESENT
))
66 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
67 else if (reg
& SBS_CHARGER_STATUS_AC_PRESENT
&&
68 !(reg
& SBS_CHARGER_STATUS_CHARGE_INHIBITED
))
69 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
71 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
75 case POWER_SUPPLY_PROP_HEALTH
:
76 if (reg
& SBS_CHARGER_STATUS_RES_COLD
)
77 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
78 if (reg
& SBS_CHARGER_STATUS_RES_HOT
)
79 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
81 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
92 static int sbs_check_state(struct sbs_info
*chip
)
97 ret
= regmap_read(chip
->regmap
, SBS_CHARGER_REG_STATUS
, ®
);
98 if (!ret
&& reg
!= chip
->last_state
) {
99 chip
->last_state
= reg
;
100 power_supply_changed(chip
->power_supply
);
107 static void sbs_delayed_work(struct work_struct
*work
)
109 struct sbs_info
*chip
= container_of(work
, struct sbs_info
, work
.work
);
111 sbs_check_state(chip
);
113 schedule_delayed_work(&chip
->work
,
114 msecs_to_jiffies(SBS_CHARGER_POLL_TIME
));
117 static irqreturn_t
sbs_irq_thread(int irq
, void *data
)
119 struct sbs_info
*chip
= data
;
122 ret
= sbs_check_state(chip
);
124 return ret
? IRQ_HANDLED
: IRQ_NONE
;
127 static enum power_supply_property sbs_properties
[] = {
128 POWER_SUPPLY_PROP_STATUS
,
129 POWER_SUPPLY_PROP_PRESENT
,
130 POWER_SUPPLY_PROP_ONLINE
,
131 POWER_SUPPLY_PROP_HEALTH
,
134 static bool sbs_readable_reg(struct device
*dev
, unsigned int reg
)
136 return reg
>= SBS_CHARGER_REG_SPEC_INFO
;
139 static bool sbs_volatile_reg(struct device
*dev
, unsigned int reg
)
142 case SBS_CHARGER_REG_STATUS
:
149 static const struct regmap_config sbs_regmap
= {
152 .max_register
= SBS_CHARGER_REG_ALARM_WARNING
,
153 .readable_reg
= sbs_readable_reg
,
154 .volatile_reg
= sbs_volatile_reg
,
155 .val_format_endian
= REGMAP_ENDIAN_LITTLE
, /* since based on SMBus */
158 static const struct power_supply_desc sbs_desc
= {
159 .name
= "sbs-charger",
160 .type
= POWER_SUPPLY_TYPE_MAINS
,
161 .properties
= sbs_properties
,
162 .num_properties
= ARRAY_SIZE(sbs_properties
),
163 .get_property
= sbs_get_property
,
166 static int sbs_probe(struct i2c_client
*client
,
167 const struct i2c_device_id
*id
)
169 struct power_supply_config psy_cfg
= {};
170 struct sbs_info
*chip
;
173 chip
= devm_kzalloc(&client
->dev
, sizeof(struct sbs_info
), GFP_KERNEL
);
177 chip
->client
= client
;
178 psy_cfg
.of_node
= client
->dev
.of_node
;
179 psy_cfg
.drv_data
= chip
;
181 i2c_set_clientdata(client
, chip
);
183 chip
->regmap
= devm_regmap_init_i2c(client
, &sbs_regmap
);
184 if (IS_ERR(chip
->regmap
))
185 return PTR_ERR(chip
->regmap
);
188 * Before we register, we need to make sure we can actually talk
191 ret
= regmap_read(chip
->regmap
, SBS_CHARGER_REG_STATUS
, &val
);
193 dev_err(&client
->dev
, "Failed to get device status\n");
196 chip
->last_state
= val
;
198 chip
->power_supply
= devm_power_supply_register(&client
->dev
, &sbs_desc
,
200 if (IS_ERR(chip
->power_supply
)) {
201 dev_err(&client
->dev
, "Failed to register power supply\n");
202 return PTR_ERR(chip
->power_supply
);
206 * The sbs-charger spec doesn't impose the use of an interrupt. So in
207 * the case it wasn't provided we use polling in order get the charger's
211 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
212 NULL
, sbs_irq_thread
,
213 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
214 dev_name(&client
->dev
), chip
);
216 dev_err(&client
->dev
, "Failed to request irq, %d\n", ret
);
220 INIT_DELAYED_WORK(&chip
->work
, sbs_delayed_work
);
221 schedule_delayed_work(&chip
->work
,
222 msecs_to_jiffies(SBS_CHARGER_POLL_TIME
));
225 dev_info(&client
->dev
,
226 "%s: smart charger device registered\n", client
->name
);
231 static int sbs_remove(struct i2c_client
*client
)
233 struct sbs_info
*chip
= i2c_get_clientdata(client
);
235 cancel_delayed_work_sync(&chip
->work
);
241 static const struct of_device_id sbs_dt_ids
[] = {
242 { .compatible
= "sbs,sbs-charger" },
245 MODULE_DEVICE_TABLE(of
, sbs_dt_ids
);
248 static const struct i2c_device_id sbs_id
[] = {
249 { "sbs-charger", 0 },
252 MODULE_DEVICE_TABLE(i2c
, sbs_id
);
254 static struct i2c_driver sbs_driver
= {
256 .remove
= sbs_remove
,
259 .name
= "sbs-charger",
260 .of_match_table
= of_match_ptr(sbs_dt_ids
),
263 module_i2c_driver(sbs_driver
);
265 MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
266 MODULE_DESCRIPTION("SBS smart charger driver");
267 MODULE_LICENSE("GPL v2");