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