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