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 {
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 		ret = devm_delayed_work_autocancel(&client->dev, &chip->work,
213 						   sbs_delayed_work);
214 		if (ret)
215 			return dev_err_probe(&client->dev, ret,
216 					     "Failed to init work for polling\n");
217 
218 		schedule_delayed_work(&chip->work,
219 				      msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
220 	}
221 
222 	dev_info(&client->dev,
223 		 "%s: smart charger device registered\n", client->name);
224 
225 	return 0;
226 }
227 
228 #ifdef CONFIG_OF
229 static const struct of_device_id sbs_dt_ids[] = {
230 	{ .compatible = "sbs,sbs-charger" },
231 	{ },
232 };
233 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
234 #endif
235 
236 static const struct i2c_device_id sbs_id[] = {
237 	{ "sbs-charger", 0 },
238 	{ }
239 };
240 MODULE_DEVICE_TABLE(i2c, sbs_id);
241 
242 static struct i2c_driver sbs_driver = {
243 	.probe_new	= sbs_probe,
244 	.id_table	= sbs_id,
245 	.driver = {
246 		.name	= "sbs-charger",
247 		.of_match_table = of_match_ptr(sbs_dt_ids),
248 	},
249 };
250 module_i2c_driver(sbs_driver);
251 
252 MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
253 MODULE_DESCRIPTION("SBS smart charger driver");
254 MODULE_LICENSE("GPL v2");
255