1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC
4  *
5  * Copyright (C) 2009 Bluewater Systems Ltd
6  *
7  * Author: Ryan Mallon
8  *
9  * DS2786 added by Yulia Vilensky <vilensky@compulab.co.il>
10  *
11  * UEvent sending added by Evgeny Romanov <romanov@neurosoft.ru>
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/errno.h>
18 #include <linux/swab.h>
19 #include <linux/i2c.h>
20 #include <linux/delay.h>
21 #include <linux/idr.h>
22 #include <linux/power_supply.h>
23 #include <linux/slab.h>
24 #include <linux/ds2782_battery.h>
25 
26 #define DS2782_REG_RARC		0x06	/* Remaining active relative capacity */
27 
28 #define DS278x_REG_VOLT_MSB	0x0c
29 #define DS278x_REG_TEMP_MSB	0x0a
30 #define DS278x_REG_CURRENT_MSB	0x0e
31 
32 /* EEPROM Block */
33 #define DS2782_REG_RSNSP	0x69	/* Sense resistor value */
34 
35 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
36 #define DS2782_CURRENT_UNITS	1563
37 
38 #define DS2786_REG_RARC		0x02	/* Remaining active relative capacity */
39 
40 #define DS2786_CURRENT_UNITS	25
41 
42 #define DS278x_DELAY		1000
43 
44 struct ds278x_info;
45 
46 struct ds278x_battery_ops {
47 	int (*get_battery_current)(struct ds278x_info *info, int *current_uA);
48 	int (*get_battery_voltage)(struct ds278x_info *info, int *voltage_uV);
49 	int (*get_battery_capacity)(struct ds278x_info *info, int *capacity);
50 };
51 
52 #define to_ds278x_info(x) power_supply_get_drvdata(x)
53 
54 struct ds278x_info {
55 	struct i2c_client	*client;
56 	struct power_supply	*battery;
57 	struct power_supply_desc	battery_desc;
58 	const struct ds278x_battery_ops *ops;
59 	struct delayed_work	bat_work;
60 	int			id;
61 	int                     rsns;
62 	int			capacity;
63 	int			status;		/* State Of Charge */
64 };
65 
66 static DEFINE_IDR(battery_id);
67 static DEFINE_MUTEX(battery_lock);
68 
69 static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val)
70 {
71 	int ret;
72 
73 	ret = i2c_smbus_read_byte_data(info->client, reg);
74 	if (ret < 0) {
75 		dev_err(&info->client->dev, "register read failed\n");
76 		return ret;
77 	}
78 
79 	*val = ret;
80 	return 0;
81 }
82 
83 static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb,
84 				    s16 *val)
85 {
86 	int ret;
87 
88 	ret = i2c_smbus_read_word_data(info->client, reg_msb);
89 	if (ret < 0) {
90 		dev_err(&info->client->dev, "register read failed\n");
91 		return ret;
92 	}
93 
94 	*val = swab16(ret);
95 	return 0;
96 }
97 
98 static int ds278x_get_temp(struct ds278x_info *info, int *temp)
99 {
100 	s16 raw;
101 	int err;
102 
103 	/*
104 	 * Temperature is measured in units of 0.125 degrees celcius, the
105 	 * power_supply class measures temperature in tenths of degrees
106 	 * celsius. The temperature value is stored as a 10 bit number, plus
107 	 * sign in the upper bits of a 16 bit register.
108 	 */
109 	err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw);
110 	if (err)
111 		return err;
112 	*temp = ((raw / 32) * 125) / 100;
113 	return 0;
114 }
115 
116 static int ds2782_get_current(struct ds278x_info *info, int *current_uA)
117 {
118 	int sense_res;
119 	int err;
120 	u8 sense_res_raw;
121 	s16 raw;
122 
123 	/*
124 	 * The units of measurement for current are dependent on the value of
125 	 * the sense resistor.
126 	 */
127 	err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw);
128 	if (err)
129 		return err;
130 	if (sense_res_raw == 0) {
131 		dev_err(&info->client->dev, "sense resistor value is 0\n");
132 		return -ENXIO;
133 	}
134 	sense_res = 1000 / sense_res_raw;
135 
136 	dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n",
137 		sense_res);
138 	err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
139 	if (err)
140 		return err;
141 	*current_uA = raw * (DS2782_CURRENT_UNITS / sense_res);
142 	return 0;
143 }
144 
145 static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uV)
146 {
147 	s16 raw;
148 	int err;
149 
150 	/*
151 	 * Voltage is measured in units of 4.88mV. The voltage is stored as
152 	 * a 10-bit number plus sign, in the upper bits of a 16-bit register
153 	 */
154 	err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
155 	if (err)
156 		return err;
157 	*voltage_uV = (raw / 32) * 4800;
158 	return 0;
159 }
160 
161 static int ds2782_get_capacity(struct ds278x_info *info, int *capacity)
162 {
163 	int err;
164 	u8 raw;
165 
166 	err = ds278x_read_reg(info, DS2782_REG_RARC, &raw);
167 	if (err)
168 		return err;
169 	*capacity = raw;
170 	return 0;
171 }
172 
173 static int ds2786_get_current(struct ds278x_info *info, int *current_uA)
174 {
175 	int err;
176 	s16 raw;
177 
178 	err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
179 	if (err)
180 		return err;
181 	*current_uA = (raw / 16) * (DS2786_CURRENT_UNITS / info->rsns);
182 	return 0;
183 }
184 
185 static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uV)
186 {
187 	s16 raw;
188 	int err;
189 
190 	/*
191 	 * Voltage is measured in units of 1.22mV. The voltage is stored as
192 	 * a 12-bit number plus sign, in the upper bits of a 16-bit register
193 	 */
194 	err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
195 	if (err)
196 		return err;
197 	*voltage_uV = (raw / 8) * 1220;
198 	return 0;
199 }
200 
201 static int ds2786_get_capacity(struct ds278x_info *info, int *capacity)
202 {
203 	int err;
204 	u8 raw;
205 
206 	err = ds278x_read_reg(info, DS2786_REG_RARC, &raw);
207 	if (err)
208 		return err;
209 	/* Relative capacity is displayed with resolution 0.5 % */
210 	*capacity = raw/2 ;
211 	return 0;
212 }
213 
214 static int ds278x_get_status(struct ds278x_info *info, int *status)
215 {
216 	int err;
217 	int current_uA;
218 	int capacity;
219 
220 	err = info->ops->get_battery_current(info, &current_uA);
221 	if (err)
222 		return err;
223 
224 	err = info->ops->get_battery_capacity(info, &capacity);
225 	if (err)
226 		return err;
227 
228 	info->capacity = capacity;
229 
230 	if (capacity == 100)
231 		*status = POWER_SUPPLY_STATUS_FULL;
232 	else if (current_uA == 0)
233 		*status = POWER_SUPPLY_STATUS_NOT_CHARGING;
234 	else if (current_uA < 0)
235 		*status = POWER_SUPPLY_STATUS_DISCHARGING;
236 	else
237 		*status = POWER_SUPPLY_STATUS_CHARGING;
238 
239 	return 0;
240 }
241 
242 static int ds278x_battery_get_property(struct power_supply *psy,
243 				       enum power_supply_property prop,
244 				       union power_supply_propval *val)
245 {
246 	struct ds278x_info *info = to_ds278x_info(psy);
247 	int ret;
248 
249 	switch (prop) {
250 	case POWER_SUPPLY_PROP_STATUS:
251 		ret = ds278x_get_status(info, &val->intval);
252 		break;
253 
254 	case POWER_SUPPLY_PROP_CAPACITY:
255 		ret = info->ops->get_battery_capacity(info, &val->intval);
256 		break;
257 
258 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
259 		ret = info->ops->get_battery_voltage(info, &val->intval);
260 		break;
261 
262 	case POWER_SUPPLY_PROP_CURRENT_NOW:
263 		ret = info->ops->get_battery_current(info, &val->intval);
264 		break;
265 
266 	case POWER_SUPPLY_PROP_TEMP:
267 		ret = ds278x_get_temp(info, &val->intval);
268 		break;
269 
270 	default:
271 		ret = -EINVAL;
272 	}
273 
274 	return ret;
275 }
276 
277 static void ds278x_bat_update(struct ds278x_info *info)
278 {
279 	int old_status = info->status;
280 	int old_capacity = info->capacity;
281 
282 	ds278x_get_status(info, &info->status);
283 
284 	if ((old_status != info->status) || (old_capacity != info->capacity))
285 		power_supply_changed(info->battery);
286 }
287 
288 static void ds278x_bat_work(struct work_struct *work)
289 {
290 	struct ds278x_info *info;
291 
292 	info = container_of(work, struct ds278x_info, bat_work.work);
293 	ds278x_bat_update(info);
294 
295 	schedule_delayed_work(&info->bat_work, DS278x_DELAY);
296 }
297 
298 static enum power_supply_property ds278x_battery_props[] = {
299 	POWER_SUPPLY_PROP_STATUS,
300 	POWER_SUPPLY_PROP_CAPACITY,
301 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
302 	POWER_SUPPLY_PROP_CURRENT_NOW,
303 	POWER_SUPPLY_PROP_TEMP,
304 };
305 
306 static void ds278x_power_supply_init(struct power_supply_desc *battery)
307 {
308 	battery->type			= POWER_SUPPLY_TYPE_BATTERY;
309 	battery->properties		= ds278x_battery_props;
310 	battery->num_properties		= ARRAY_SIZE(ds278x_battery_props);
311 	battery->get_property		= ds278x_battery_get_property;
312 	battery->external_power_changed	= NULL;
313 }
314 
315 static int ds278x_battery_remove(struct i2c_client *client)
316 {
317 	struct ds278x_info *info = i2c_get_clientdata(client);
318 	int id = info->id;
319 
320 	power_supply_unregister(info->battery);
321 	cancel_delayed_work_sync(&info->bat_work);
322 	kfree(info->battery_desc.name);
323 	kfree(info);
324 
325 	mutex_lock(&battery_lock);
326 	idr_remove(&battery_id, id);
327 	mutex_unlock(&battery_lock);
328 
329 	return 0;
330 }
331 
332 #ifdef CONFIG_PM_SLEEP
333 
334 static int ds278x_suspend(struct device *dev)
335 {
336 	struct i2c_client *client = to_i2c_client(dev);
337 	struct ds278x_info *info = i2c_get_clientdata(client);
338 
339 	cancel_delayed_work(&info->bat_work);
340 	return 0;
341 }
342 
343 static int ds278x_resume(struct device *dev)
344 {
345 	struct i2c_client *client = to_i2c_client(dev);
346 	struct ds278x_info *info = i2c_get_clientdata(client);
347 
348 	schedule_delayed_work(&info->bat_work, DS278x_DELAY);
349 	return 0;
350 }
351 #endif /* CONFIG_PM_SLEEP */
352 
353 static SIMPLE_DEV_PM_OPS(ds278x_battery_pm_ops, ds278x_suspend, ds278x_resume);
354 
355 enum ds278x_num_id {
356 	DS2782 = 0,
357 	DS2786,
358 };
359 
360 static const struct ds278x_battery_ops ds278x_ops[] = {
361 	[DS2782] = {
362 		.get_battery_current  = ds2782_get_current,
363 		.get_battery_voltage  = ds2782_get_voltage,
364 		.get_battery_capacity = ds2782_get_capacity,
365 	},
366 	[DS2786] = {
367 		.get_battery_current  = ds2786_get_current,
368 		.get_battery_voltage  = ds2786_get_voltage,
369 		.get_battery_capacity = ds2786_get_capacity,
370 	}
371 };
372 
373 static int ds278x_battery_probe(struct i2c_client *client,
374 				const struct i2c_device_id *id)
375 {
376 	struct ds278x_platform_data *pdata = client->dev.platform_data;
377 	struct power_supply_config psy_cfg = {};
378 	struct ds278x_info *info;
379 	int ret;
380 	int num;
381 
382 	/*
383 	 * ds2786 should have the sense resistor value set
384 	 * in the platform data
385 	 */
386 	if (id->driver_data == DS2786 && !pdata) {
387 		dev_err(&client->dev, "missing platform data for ds2786\n");
388 		return -EINVAL;
389 	}
390 
391 	/* Get an ID for this battery */
392 	mutex_lock(&battery_lock);
393 	ret = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
394 	mutex_unlock(&battery_lock);
395 	if (ret < 0)
396 		goto fail_id;
397 	num = ret;
398 
399 	info = kzalloc(sizeof(*info), GFP_KERNEL);
400 	if (!info) {
401 		ret = -ENOMEM;
402 		goto fail_info;
403 	}
404 
405 	info->battery_desc.name = kasprintf(GFP_KERNEL, "%s-%d",
406 					    client->name, num);
407 	if (!info->battery_desc.name) {
408 		ret = -ENOMEM;
409 		goto fail_name;
410 	}
411 
412 	if (id->driver_data == DS2786)
413 		info->rsns = pdata->rsns;
414 
415 	i2c_set_clientdata(client, info);
416 	info->client = client;
417 	info->id = num;
418 	info->ops  = &ds278x_ops[id->driver_data];
419 	ds278x_power_supply_init(&info->battery_desc);
420 	psy_cfg.drv_data = info;
421 
422 	info->capacity = 100;
423 	info->status = POWER_SUPPLY_STATUS_FULL;
424 
425 	INIT_DELAYED_WORK(&info->bat_work, ds278x_bat_work);
426 
427 	info->battery = power_supply_register(&client->dev,
428 					      &info->battery_desc, &psy_cfg);
429 	if (IS_ERR(info->battery)) {
430 		dev_err(&client->dev, "failed to register battery\n");
431 		ret = PTR_ERR(info->battery);
432 		goto fail_register;
433 	} else {
434 		schedule_delayed_work(&info->bat_work, DS278x_DELAY);
435 	}
436 
437 	return 0;
438 
439 fail_register:
440 	kfree(info->battery_desc.name);
441 fail_name:
442 	kfree(info);
443 fail_info:
444 	mutex_lock(&battery_lock);
445 	idr_remove(&battery_id, num);
446 	mutex_unlock(&battery_lock);
447 fail_id:
448 	return ret;
449 }
450 
451 static const struct i2c_device_id ds278x_id[] = {
452 	{"ds2782", DS2782},
453 	{"ds2786", DS2786},
454 	{},
455 };
456 MODULE_DEVICE_TABLE(i2c, ds278x_id);
457 
458 static struct i2c_driver ds278x_battery_driver = {
459 	.driver 	= {
460 		.name	= "ds2782-battery",
461 		.pm	= &ds278x_battery_pm_ops,
462 	},
463 	.probe		= ds278x_battery_probe,
464 	.remove		= ds278x_battery_remove,
465 	.id_table	= ds278x_id,
466 };
467 module_i2c_driver(ds278x_battery_driver);
468 
469 MODULE_AUTHOR("Ryan Mallon");
470 MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC driver");
471 MODULE_LICENSE("GPL");
472