treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / power / supply / ds2782_battery.c
blob9ae273fde7a2c53cd9177011f556d86ab63a7635
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC
5 * Copyright (C) 2009 Bluewater Systems Ltd
7 * Author: Ryan Mallon
9 * DS2786 added by Yulia Vilensky <vilensky@compulab.co.il>
11 * UEvent sending added by Evgeny Romanov <romanov@neurosoft.ru>
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>
26 #define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */
28 #define DS278x_REG_VOLT_MSB 0x0c
29 #define DS278x_REG_TEMP_MSB 0x0a
30 #define DS278x_REG_CURRENT_MSB 0x0e
32 /* EEPROM Block */
33 #define DS2782_REG_RSNSP 0x69 /* Sense resistor value */
35 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
36 #define DS2782_CURRENT_UNITS 1563
38 #define DS2786_REG_RARC 0x02 /* Remaining active relative capacity */
40 #define DS2786_CURRENT_UNITS 25
42 #define DS278x_DELAY 1000
44 struct ds278x_info;
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);
52 #define to_ds278x_info(x) power_supply_get_drvdata(x)
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 */
66 static DEFINE_IDR(battery_id);
67 static DEFINE_MUTEX(battery_lock);
69 static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val)
71 int ret;
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;
79 *val = ret;
80 return 0;
83 static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb,
84 s16 *val)
86 int ret;
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;
94 *val = swab16(ret);
95 return 0;
98 static int ds278x_get_temp(struct ds278x_info *info, int *temp)
100 s16 raw;
101 int err;
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.
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;
116 static int ds2782_get_current(struct ds278x_info *info, int *current_uA)
118 int sense_res;
119 int err;
120 u8 sense_res_raw;
121 s16 raw;
124 * The units of measurement for current are dependent on the value of
125 * the sense resistor.
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;
134 sense_res = 1000 / sense_res_raw;
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;
145 static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uV)
147 s16 raw;
148 int err;
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
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;
161 static int ds2782_get_capacity(struct ds278x_info *info, int *capacity)
163 int err;
164 u8 raw;
166 err = ds278x_read_reg(info, DS2782_REG_RARC, &raw);
167 if (err)
168 return err;
169 *capacity = raw;
170 return 0;
173 static int ds2786_get_current(struct ds278x_info *info, int *current_uA)
175 int err;
176 s16 raw;
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;
185 static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uV)
187 s16 raw;
188 int err;
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
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;
201 static int ds2786_get_capacity(struct ds278x_info *info, int *capacity)
203 int err;
204 u8 raw;
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;
214 static int ds278x_get_status(struct ds278x_info *info, int *status)
216 int err;
217 int current_uA;
218 int capacity;
220 err = info->ops->get_battery_current(info, &current_uA);
221 if (err)
222 return err;
224 err = info->ops->get_battery_capacity(info, &capacity);
225 if (err)
226 return err;
228 info->capacity = capacity;
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;
239 return 0;
242 static int ds278x_battery_get_property(struct power_supply *psy,
243 enum power_supply_property prop,
244 union power_supply_propval *val)
246 struct ds278x_info *info = to_ds278x_info(psy);
247 int ret;
249 switch (prop) {
250 case POWER_SUPPLY_PROP_STATUS:
251 ret = ds278x_get_status(info, &val->intval);
252 break;
254 case POWER_SUPPLY_PROP_CAPACITY:
255 ret = info->ops->get_battery_capacity(info, &val->intval);
256 break;
258 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
259 ret = info->ops->get_battery_voltage(info, &val->intval);
260 break;
262 case POWER_SUPPLY_PROP_CURRENT_NOW:
263 ret = info->ops->get_battery_current(info, &val->intval);
264 break;
266 case POWER_SUPPLY_PROP_TEMP:
267 ret = ds278x_get_temp(info, &val->intval);
268 break;
270 default:
271 ret = -EINVAL;
274 return ret;
277 static void ds278x_bat_update(struct ds278x_info *info)
279 int old_status = info->status;
280 int old_capacity = info->capacity;
282 ds278x_get_status(info, &info->status);
284 if ((old_status != info->status) || (old_capacity != info->capacity))
285 power_supply_changed(info->battery);
288 static void ds278x_bat_work(struct work_struct *work)
290 struct ds278x_info *info;
292 info = container_of(work, struct ds278x_info, bat_work.work);
293 ds278x_bat_update(info);
295 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
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,
306 static void ds278x_power_supply_init(struct power_supply_desc *battery)
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;
315 static int ds278x_battery_remove(struct i2c_client *client)
317 struct ds278x_info *info = i2c_get_clientdata(client);
318 int id = info->id;
320 power_supply_unregister(info->battery);
321 cancel_delayed_work_sync(&info->bat_work);
322 kfree(info->battery_desc.name);
323 kfree(info);
325 mutex_lock(&battery_lock);
326 idr_remove(&battery_id, id);
327 mutex_unlock(&battery_lock);
329 return 0;
332 #ifdef CONFIG_PM_SLEEP
334 static int ds278x_suspend(struct device *dev)
336 struct i2c_client *client = to_i2c_client(dev);
337 struct ds278x_info *info = i2c_get_clientdata(client);
339 cancel_delayed_work(&info->bat_work);
340 return 0;
343 static int ds278x_resume(struct device *dev)
345 struct i2c_client *client = to_i2c_client(dev);
346 struct ds278x_info *info = i2c_get_clientdata(client);
348 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
349 return 0;
351 #endif /* CONFIG_PM_SLEEP */
353 static SIMPLE_DEV_PM_OPS(ds278x_battery_pm_ops, ds278x_suspend, ds278x_resume);
355 enum ds278x_num_id {
356 DS2782 = 0,
357 DS2786,
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,
366 [DS2786] = {
367 .get_battery_current = ds2786_get_current,
368 .get_battery_voltage = ds2786_get_voltage,
369 .get_battery_capacity = ds2786_get_capacity,
373 static int ds278x_battery_probe(struct i2c_client *client,
374 const struct i2c_device_id *id)
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;
383 * ds2786 should have the sense resistor value set
384 * in the platform data
386 if (id->driver_data == DS2786 && !pdata) {
387 dev_err(&client->dev, "missing platform data for ds2786\n");
388 return -EINVAL;
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;
399 info = kzalloc(sizeof(*info), GFP_KERNEL);
400 if (!info) {
401 ret = -ENOMEM;
402 goto fail_info;
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;
412 if (id->driver_data == DS2786)
413 info->rsns = pdata->rsns;
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;
422 info->capacity = 100;
423 info->status = POWER_SUPPLY_STATUS_FULL;
425 INIT_DELAYED_WORK(&info->bat_work, ds278x_bat_work);
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);
437 return 0;
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;
451 static const struct i2c_device_id ds278x_id[] = {
452 {"ds2782", DS2782},
453 {"ds2786", DS2786},
456 MODULE_DEVICE_TABLE(i2c, ds278x_id);
458 static struct i2c_driver ds278x_battery_driver = {
459 .driver = {
460 .name = "ds2782-battery",
461 .pm = &ds278x_battery_pm_ops,
463 .probe = ds278x_battery_probe,
464 .remove = ds278x_battery_remove,
465 .id_table = ds278x_id,
467 module_i2c_driver(ds278x_battery_driver);
469 MODULE_AUTHOR("Ryan Mallon");
470 MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC driver");
471 MODULE_LICENSE("GPL");