e1000e: cleanup PARENTHESIS_ALIGNMENT checkpatch checks
[linux/fpc-iii.git] / drivers / power / ds2782_battery.c
blobc09e7726c96cc25c6b890872db9b64f4f7903b52
1 /*
2 * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC
4 * Copyright (C) 2009 Bluewater Systems Ltd
6 * Author: Ryan Mallon
8 * DS2786 added by Yulia Vilensky <vilensky@compulab.co.il>
10 * UEvent sending added by Evgeny Romanov <romanov@neurosoft.ru>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/swab.h>
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <linux/idr.h>
26 #include <linux/power_supply.h>
27 #include <linux/slab.h>
28 #include <linux/ds2782_battery.h>
30 #define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */
32 #define DS278x_REG_VOLT_MSB 0x0c
33 #define DS278x_REG_TEMP_MSB 0x0a
34 #define DS278x_REG_CURRENT_MSB 0x0e
36 /* EEPROM Block */
37 #define DS2782_REG_RSNSP 0x69 /* Sense resistor value */
39 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
40 #define DS2782_CURRENT_UNITS 1563
42 #define DS2786_REG_RARC 0x02 /* Remaining active relative capacity */
44 #define DS2786_CURRENT_UNITS 25
46 #define DS278x_DELAY 1000
48 struct ds278x_info;
50 struct ds278x_battery_ops {
51 int (*get_battery_current)(struct ds278x_info *info, int *current_uA);
52 int (*get_battery_voltage)(struct ds278x_info *info, int *voltage_uV);
53 int (*get_battery_capacity)(struct ds278x_info *info, int *capacity);
56 #define to_ds278x_info(x) container_of(x, struct ds278x_info, battery)
58 struct ds278x_info {
59 struct i2c_client *client;
60 struct power_supply battery;
61 struct ds278x_battery_ops *ops;
62 struct delayed_work bat_work;
63 int id;
64 int rsns;
65 int capacity;
66 int status; /* State Of Charge */
69 static DEFINE_IDR(battery_id);
70 static DEFINE_MUTEX(battery_lock);
72 static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val)
74 int ret;
76 ret = i2c_smbus_read_byte_data(info->client, reg);
77 if (ret < 0) {
78 dev_err(&info->client->dev, "register read failed\n");
79 return ret;
82 *val = ret;
83 return 0;
86 static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb,
87 s16 *val)
89 int ret;
91 ret = i2c_smbus_read_word_data(info->client, reg_msb);
92 if (ret < 0) {
93 dev_err(&info->client->dev, "register read failed\n");
94 return ret;
97 *val = swab16(ret);
98 return 0;
101 static int ds278x_get_temp(struct ds278x_info *info, int *temp)
103 s16 raw;
104 int err;
107 * Temperature is measured in units of 0.125 degrees celcius, the
108 * power_supply class measures temperature in tenths of degrees
109 * celsius. The temperature value is stored as a 10 bit number, plus
110 * sign in the upper bits of a 16 bit register.
112 err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw);
113 if (err)
114 return err;
115 *temp = ((raw / 32) * 125) / 100;
116 return 0;
119 static int ds2782_get_current(struct ds278x_info *info, int *current_uA)
121 int sense_res;
122 int err;
123 u8 sense_res_raw;
124 s16 raw;
127 * The units of measurement for current are dependent on the value of
128 * the sense resistor.
130 err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw);
131 if (err)
132 return err;
133 if (sense_res_raw == 0) {
134 dev_err(&info->client->dev, "sense resistor value is 0\n");
135 return -ENXIO;
137 sense_res = 1000 / sense_res_raw;
139 dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n",
140 sense_res);
141 err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
142 if (err)
143 return err;
144 *current_uA = raw * (DS2782_CURRENT_UNITS / sense_res);
145 return 0;
148 static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uV)
150 s16 raw;
151 int err;
154 * Voltage is measured in units of 4.88mV. The voltage is stored as
155 * a 10-bit number plus sign, in the upper bits of a 16-bit register
157 err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
158 if (err)
159 return err;
160 *voltage_uV = (raw / 32) * 4800;
161 return 0;
164 static int ds2782_get_capacity(struct ds278x_info *info, int *capacity)
166 int err;
167 u8 raw;
169 err = ds278x_read_reg(info, DS2782_REG_RARC, &raw);
170 if (err)
171 return err;
172 *capacity = raw;
173 return 0;
176 static int ds2786_get_current(struct ds278x_info *info, int *current_uA)
178 int err;
179 s16 raw;
181 err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
182 if (err)
183 return err;
184 *current_uA = (raw / 16) * (DS2786_CURRENT_UNITS / info->rsns);
185 return 0;
188 static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uV)
190 s16 raw;
191 int err;
194 * Voltage is measured in units of 1.22mV. The voltage is stored as
195 * a 10-bit number plus sign, in the upper bits of a 16-bit register
197 err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
198 if (err)
199 return err;
200 *voltage_uV = (raw / 8) * 1220;
201 return 0;
204 static int ds2786_get_capacity(struct ds278x_info *info, int *capacity)
206 int err;
207 u8 raw;
209 err = ds278x_read_reg(info, DS2786_REG_RARC, &raw);
210 if (err)
211 return err;
212 /* Relative capacity is displayed with resolution 0.5 % */
213 *capacity = raw/2 ;
214 return 0;
217 static int ds278x_get_status(struct ds278x_info *info, int *status)
219 int err;
220 int current_uA;
221 int capacity;
223 err = info->ops->get_battery_current(info, &current_uA);
224 if (err)
225 return err;
227 err = info->ops->get_battery_capacity(info, &capacity);
228 if (err)
229 return err;
231 info->capacity = capacity;
233 if (capacity == 100)
234 *status = POWER_SUPPLY_STATUS_FULL;
235 else if (current_uA == 0)
236 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
237 else if (current_uA < 0)
238 *status = POWER_SUPPLY_STATUS_DISCHARGING;
239 else
240 *status = POWER_SUPPLY_STATUS_CHARGING;
242 return 0;
245 static int ds278x_battery_get_property(struct power_supply *psy,
246 enum power_supply_property prop,
247 union power_supply_propval *val)
249 struct ds278x_info *info = to_ds278x_info(psy);
250 int ret;
252 switch (prop) {
253 case POWER_SUPPLY_PROP_STATUS:
254 ret = ds278x_get_status(info, &val->intval);
255 break;
257 case POWER_SUPPLY_PROP_CAPACITY:
258 ret = info->ops->get_battery_capacity(info, &val->intval);
259 break;
261 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
262 ret = info->ops->get_battery_voltage(info, &val->intval);
263 break;
265 case POWER_SUPPLY_PROP_CURRENT_NOW:
266 ret = info->ops->get_battery_current(info, &val->intval);
267 break;
269 case POWER_SUPPLY_PROP_TEMP:
270 ret = ds278x_get_temp(info, &val->intval);
271 break;
273 default:
274 ret = -EINVAL;
277 return ret;
280 static void ds278x_bat_update(struct ds278x_info *info)
282 int old_status = info->status;
283 int old_capacity = info->capacity;
285 ds278x_get_status(info, &info->status);
287 if ((old_status != info->status) || (old_capacity != info->capacity))
288 power_supply_changed(&info->battery);
291 static void ds278x_bat_work(struct work_struct *work)
293 struct ds278x_info *info;
295 info = container_of(work, struct ds278x_info, bat_work.work);
296 ds278x_bat_update(info);
298 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
301 static enum power_supply_property ds278x_battery_props[] = {
302 POWER_SUPPLY_PROP_STATUS,
303 POWER_SUPPLY_PROP_CAPACITY,
304 POWER_SUPPLY_PROP_VOLTAGE_NOW,
305 POWER_SUPPLY_PROP_CURRENT_NOW,
306 POWER_SUPPLY_PROP_TEMP,
309 static void ds278x_power_supply_init(struct power_supply *battery)
311 battery->type = POWER_SUPPLY_TYPE_BATTERY;
312 battery->properties = ds278x_battery_props;
313 battery->num_properties = ARRAY_SIZE(ds278x_battery_props);
314 battery->get_property = ds278x_battery_get_property;
315 battery->external_power_changed = NULL;
318 static int ds278x_battery_remove(struct i2c_client *client)
320 struct ds278x_info *info = i2c_get_clientdata(client);
322 power_supply_unregister(&info->battery);
323 kfree(info->battery.name);
325 mutex_lock(&battery_lock);
326 idr_remove(&battery_id, info->id);
327 mutex_unlock(&battery_lock);
329 cancel_delayed_work(&info->bat_work);
331 kfree(info);
332 return 0;
335 #ifdef CONFIG_PM
337 static int ds278x_suspend(struct i2c_client *client,
338 pm_message_t state)
340 struct ds278x_info *info = i2c_get_clientdata(client);
342 cancel_delayed_work(&info->bat_work);
343 return 0;
346 static int ds278x_resume(struct i2c_client *client)
348 struct ds278x_info *info = i2c_get_clientdata(client);
350 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
351 return 0;
354 #else
356 #define ds278x_suspend NULL
357 #define ds278x_resume NULL
359 #endif /* CONFIG_PM */
362 enum ds278x_num_id {
363 DS2782 = 0,
364 DS2786,
367 static struct ds278x_battery_ops ds278x_ops[] = {
368 [DS2782] = {
369 .get_battery_current = ds2782_get_current,
370 .get_battery_voltage = ds2782_get_voltage,
371 .get_battery_capacity = ds2782_get_capacity,
373 [DS2786] = {
374 .get_battery_current = ds2786_get_current,
375 .get_battery_voltage = ds2786_get_voltage,
376 .get_battery_capacity = ds2786_get_capacity,
380 static int ds278x_battery_probe(struct i2c_client *client,
381 const struct i2c_device_id *id)
383 struct ds278x_platform_data *pdata = client->dev.platform_data;
384 struct ds278x_info *info;
385 int ret;
386 int num;
389 * ds2786 should have the sense resistor value set
390 * in the platform data
392 if (id->driver_data == DS2786 && !pdata) {
393 dev_err(&client->dev, "missing platform data for ds2786\n");
394 return -EINVAL;
397 /* Get an ID for this battery */
398 mutex_lock(&battery_lock);
399 ret = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
400 mutex_unlock(&battery_lock);
401 if (ret < 0)
402 goto fail_id;
403 num = ret;
405 info = kzalloc(sizeof(*info), GFP_KERNEL);
406 if (!info) {
407 ret = -ENOMEM;
408 goto fail_info;
411 info->battery.name = kasprintf(GFP_KERNEL, "%s-%d", client->name, num);
412 if (!info->battery.name) {
413 ret = -ENOMEM;
414 goto fail_name;
417 if (id->driver_data == DS2786)
418 info->rsns = pdata->rsns;
420 i2c_set_clientdata(client, info);
421 info->client = client;
422 info->id = num;
423 info->ops = &ds278x_ops[id->driver_data];
424 ds278x_power_supply_init(&info->battery);
426 info->capacity = 100;
427 info->status = POWER_SUPPLY_STATUS_FULL;
429 INIT_DELAYED_WORK(&info->bat_work, ds278x_bat_work);
431 ret = power_supply_register(&client->dev, &info->battery);
432 if (ret) {
433 dev_err(&client->dev, "failed to register battery\n");
434 goto fail_register;
435 } else {
436 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
439 return 0;
441 fail_register:
442 kfree(info->battery.name);
443 fail_name:
444 kfree(info);
445 fail_info:
446 mutex_lock(&battery_lock);
447 idr_remove(&battery_id, num);
448 mutex_unlock(&battery_lock);
449 fail_id:
450 return ret;
453 static const struct i2c_device_id ds278x_id[] = {
454 {"ds2782", DS2782},
455 {"ds2786", DS2786},
458 MODULE_DEVICE_TABLE(i2c, ds278x_id);
460 static struct i2c_driver ds278x_battery_driver = {
461 .driver = {
462 .name = "ds2782-battery",
464 .probe = ds278x_battery_probe,
465 .remove = ds278x_battery_remove,
466 .suspend = ds278x_suspend,
467 .resume = ds278x_resume,
468 .id_table = ds278x_id,
470 module_i2c_driver(ds278x_battery_driver);
472 MODULE_AUTHOR("Ryan Mallon");
473 MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver");
474 MODULE_LICENSE("GPL");