1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016, Prodys S.L.
4  *
5  * This adds support for sbs-charger compilant chips as defined here:
6  * http://sbs-forum.org/specs/sbc110.pdf
7  *
8  * Implemetation based on sbs-battery.c
9  */
10 
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/regmap.h>
20 #include <linux/bitops.h>
21 
22 #define SBS_CHARGER_REG_SPEC_INFO		0x11
23 #define SBS_CHARGER_REG_STATUS			0x13
24 #define SBS_CHARGER_REG_ALARM_WARNING		0x16
25 
26 #define SBS_CHARGER_STATUS_CHARGE_INHIBITED	BIT(1)
27 #define SBS_CHARGER_STATUS_RES_COLD		BIT(9)
28 #define SBS_CHARGER_STATUS_RES_HOT		BIT(10)
29 #define SBS_CHARGER_STATUS_BATTERY_PRESENT	BIT(14)
30 #define SBS_CHARGER_STATUS_AC_PRESENT		BIT(15)
31 
32 #define SBS_CHARGER_POLL_TIME			500
33 
34 struct sbs_info {
35 	struct i2c_client		*client;
36 	struct power_supply		*power_supply;
37 	struct regmap			*regmap;
38 	struct delayed_work		work;
39 	unsigned int			last_state;
40 };
41 
42 static int sbs_get_property(struct power_supply *psy,
43 			    enum power_supply_property psp,
44 			    union power_supply_propval *val)
45 {
46 	struct sbs_info *chip = power_supply_get_drvdata(psy);
47 	unsigned int reg;
48 
49 	reg = chip->last_state;
50 
51 	switch (psp) {
52 	case POWER_SUPPLY_PROP_PRESENT:
53 		val->intval = !!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT);
54 		break;
55 
56 	case POWER_SUPPLY_PROP_ONLINE:
57 		val->intval = !!(reg & SBS_CHARGER_STATUS_AC_PRESENT);
58 		break;
59 
60 	case POWER_SUPPLY_PROP_STATUS:
61 		val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
62 
63 		if (!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT))
64 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
65 		else if (reg & SBS_CHARGER_STATUS_AC_PRESENT &&
66 			 !(reg & SBS_CHARGER_STATUS_CHARGE_INHIBITED))
67 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
68 		else
69 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
70 
71 		break;
72 
73 	case POWER_SUPPLY_PROP_HEALTH:
74 		if (reg & SBS_CHARGER_STATUS_RES_COLD)
75 			val->intval = POWER_SUPPLY_HEALTH_COLD;
76 		if (reg & SBS_CHARGER_STATUS_RES_HOT)
77 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
78 		else
79 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
80 
81 		break;
82 
83 	default:
84 		return -EINVAL;
85 	}
86 
87 	return 0;
88 }
89 
90 static int sbs_check_state(struct sbs_info *chip)
91 {
92 	unsigned int reg;
93 	int ret;
94 
95 	ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &reg);
96 	if (!ret && reg != chip->last_state) {
97 		chip->last_state = reg;
98 		power_supply_changed(chip->power_supply);
99 		return 1;
100 	}
101 
102 	return 0;
103 }
104 
105 static void sbs_delayed_work(struct work_struct *work)
106 {
107 	struct sbs_info *chip = container_of(work, struct sbs_info, work.work);
108 
109 	sbs_check_state(chip);
110 
111 	schedule_delayed_work(&chip->work,
112 			      msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
113 }
114 
115 static irqreturn_t sbs_irq_thread(int irq, void *data)
116 {
117 	struct sbs_info *chip = data;
118 	int ret;
119 
120 	ret = sbs_check_state(chip);
121 
122 	return ret ? IRQ_HANDLED : IRQ_NONE;
123 }
124 
125 static enum power_supply_property sbs_properties[] = {
126 	POWER_SUPPLY_PROP_STATUS,
127 	POWER_SUPPLY_PROP_PRESENT,
128 	POWER_SUPPLY_PROP_ONLINE,
129 	POWER_SUPPLY_PROP_HEALTH,
130 };
131 
132 static bool sbs_readable_reg(struct device *dev, unsigned int reg)
133 {
134 	return reg >= SBS_CHARGER_REG_SPEC_INFO;
135 }
136 
137 static bool sbs_volatile_reg(struct device *dev, unsigned int reg)
138 {
139 	switch (reg) {
140 	case SBS_CHARGER_REG_STATUS:
141 		return true;
142 	}
143 
144 	return false;
145 }
146 
147 static const struct regmap_config sbs_regmap = {
148 	.reg_bits	= 8,
149 	.val_bits	= 16,
150 	.max_register	= SBS_CHARGER_REG_ALARM_WARNING,
151 	.readable_reg	= sbs_readable_reg,
152 	.volatile_reg	= sbs_volatile_reg,
153 	.val_format_endian = REGMAP_ENDIAN_LITTLE, /* since based on SMBus */
154 };
155 
156 static const struct power_supply_desc sbs_desc = {
157 	.name = "sbs-charger",
158 	.type = POWER_SUPPLY_TYPE_MAINS,
159 	.properties = sbs_properties,
160 	.num_properties = ARRAY_SIZE(sbs_properties),
161 	.get_property = sbs_get_property,
162 };
163 
164 static int sbs_probe(struct i2c_client *client,
165 		     const struct i2c_device_id *id)
166 {
167 	struct power_supply_config psy_cfg = {};
168 	struct sbs_info *chip;
169 	int ret, val;
170 
171 	chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
172 	if (!chip)
173 		return -ENOMEM;
174 
175 	chip->client = client;
176 	psy_cfg.of_node = client->dev.of_node;
177 	psy_cfg.drv_data = chip;
178 
179 	i2c_set_clientdata(client, chip);
180 
181 	chip->regmap = devm_regmap_init_i2c(client, &sbs_regmap);
182 	if (IS_ERR(chip->regmap))
183 		return PTR_ERR(chip->regmap);
184 
185 	/*
186 	 * Before we register, we need to make sure we can actually talk
187 	 * to the battery.
188 	 */
189 	ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &val);
190 	if (ret)
191 		return dev_err_probe(&client->dev, ret, "Failed to get device status\n");
192 	chip->last_state = val;
193 
194 	chip->power_supply = devm_power_supply_register(&client->dev, &sbs_desc, &psy_cfg);
195 	if (IS_ERR(chip->power_supply))
196 		return dev_err_probe(&client->dev, PTR_ERR(chip->power_supply),
197 				     "Failed to register power supply\n");
198 
199 	/*
200 	 * The sbs-charger spec doesn't impose the use of an interrupt. So in
201 	 * the case it wasn't provided we use polling in order get the charger's
202 	 * status.
203 	 */
204 	if (client->irq) {
205 		ret = devm_request_threaded_irq(&client->dev, client->irq,
206 					NULL, sbs_irq_thread,
207 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
208 					dev_name(&client->dev), chip);
209 		if (ret)
210 			return dev_err_probe(&client->dev, ret, "Failed to request irq\n");
211 	} else {
212 		INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
213 		schedule_delayed_work(&chip->work,
214 				      msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
215 	}
216 
217 	dev_info(&client->dev,
218 		 "%s: smart charger device registered\n", client->name);
219 
220 	return 0;
221 }
222 
223 static int sbs_remove(struct i2c_client *client)
224 {
225 	struct sbs_info *chip = i2c_get_clientdata(client);
226 
227 	cancel_delayed_work_sync(&chip->work);
228 
229 	return 0;
230 }
231 
232 #ifdef CONFIG_OF
233 static const struct of_device_id sbs_dt_ids[] = {
234 	{ .compatible = "sbs,sbs-charger" },
235 	{ },
236 };
237 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
238 #endif
239 
240 static const struct i2c_device_id sbs_id[] = {
241 	{ "sbs-charger", 0 },
242 	{ }
243 };
244 MODULE_DEVICE_TABLE(i2c, sbs_id);
245 
246 static struct i2c_driver sbs_driver = {
247 	.probe		= sbs_probe,
248 	.remove		= sbs_remove,
249 	.id_table	= sbs_id,
250 	.driver = {
251 		.name	= "sbs-charger",
252 		.of_match_table = of_match_ptr(sbs_dt_ids),
253 	},
254 };
255 module_i2c_driver(sbs_driver);
256 
257 MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
258 MODULE_DESCRIPTION("SBS smart charger driver");
259 MODULE_LICENSE("GPL v2");
260