* git://git.infradead.org/users/cbou/battery-2.6.31: Add ds2782 battery gas gauge driver olpc_battery: Ensure that the TRICKLE bit is checked olpc_battery: Fix up eeprom read function
···4343 help4444 Say Y here to enable support for batteries with ds2760 chip.45454646+config BATTERY_DS27824747+ tristate "DS2782 standalone gas-gauge"4848+ depends on I2C4949+ help5050+ Say Y here to enable support for the DS2782 standalone battery5151+ gas-gauge.5252+4653config BATTERY_PMU4754 tristate "Apple PMU battery"4855 depends on PPC32 && ADB_PMU
···11+/*22+ * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC33+ *44+ * Copyright (C) 2009 Bluewater Systems Ltd55+ *66+ * Author: Ryan Mallon <ryan@bluewatersys.com>77+ *88+ * This program is free software; you can redistribute it and/or modify99+ * it under the terms of the GNU General Public License version 2 as1010+ * published by the Free Software Foundation.1111+ *1212+ */1313+1414+#include <linux/kernel.h>1515+#include <linux/module.h>1616+#include <linux/types.h>1717+#include <linux/errno.h>1818+#include <linux/swab.h>1919+#include <linux/i2c.h>2020+#include <linux/idr.h>2121+#include <linux/power_supply.h>2222+2323+#define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */2424+2525+#define DS2782_REG_VOLT_MSB 0x0c2626+#define DS2782_REG_TEMP_MSB 0x0a2727+#define DS2782_REG_CURRENT_MSB 0x0e2828+2929+/* EEPROM Block */3030+#define DS2782_REG_RSNSP 0x69 /* Sense resistor value */3131+3232+/* Current unit measurement in uA for a 1 milli-ohm sense resistor */3333+#define DS2782_CURRENT_UNITS 15633434+3535+#define to_ds2782_info(x) container_of(x, struct ds2782_info, battery)3636+3737+struct ds2782_info {3838+ struct i2c_client *client;3939+ struct power_supply battery;4040+ int id;4141+};4242+4343+static DEFINE_IDR(battery_id);4444+static DEFINE_MUTEX(battery_lock);4545+4646+static inline int ds2782_read_reg(struct ds2782_info *info, int reg, u8 *val)4747+{4848+ int ret;4949+5050+ ret = i2c_smbus_read_byte_data(info->client, reg);5151+ if (ret < 0) {5252+ dev_err(&info->client->dev, "register read failed\n");5353+ return ret;5454+ }5555+5656+ *val = ret;5757+ return 0;5858+}5959+6060+static inline int ds2782_read_reg16(struct ds2782_info *info, int reg_msb,6161+ s16 *val)6262+{6363+ int ret;6464+6565+ ret = swab16(i2c_smbus_read_word_data(info->client, reg_msb));6666+ if (ret < 0) {6767+ dev_err(&info->client->dev, "register read failed\n");6868+ return ret;6969+ }7070+7171+ *val = ret;7272+ return 0;7373+}7474+7575+static int ds2782_get_temp(struct ds2782_info *info, int *temp)7676+{7777+ s16 raw;7878+ int err;7979+8080+ /*8181+ * Temperature is measured in units of 0.125 degrees celcius, the8282+ * power_supply class measures temperature in tenths of degrees8383+ * celsius. The temperature value is stored as a 10 bit number, plus8484+ * sign in the upper bits of a 16 bit register.8585+ */8686+ err = ds2782_read_reg16(info, DS2782_REG_TEMP_MSB, &raw);8787+ if (err)8888+ return err;8989+ *temp = ((raw / 32) * 125) / 100;9090+ return 0;9191+}9292+9393+static int ds2782_get_current(struct ds2782_info *info, int *current_uA)9494+{9595+ int sense_res;9696+ int err;9797+ u8 sense_res_raw;9898+ s16 raw;9999+100100+ /*101101+ * The units of measurement for current are dependent on the value of102102+ * the sense resistor.103103+ */104104+ err = ds2782_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw);105105+ if (err)106106+ return err;107107+ if (sense_res_raw == 0) {108108+ dev_err(&info->client->dev, "sense resistor value is 0\n");109109+ return -ENXIO;110110+ }111111+ sense_res = 1000 / sense_res_raw;112112+113113+ dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n",114114+ sense_res);115115+ err = ds2782_read_reg16(info, DS2782_REG_CURRENT_MSB, &raw);116116+ if (err)117117+ return err;118118+ *current_uA = raw * (DS2782_CURRENT_UNITS / sense_res);119119+ return 0;120120+}121121+122122+static int ds2782_get_voltage(struct ds2782_info *info, int *voltage_uA)123123+{124124+ s16 raw;125125+ int err;126126+127127+ /*128128+ * Voltage is measured in units of 4.88mV. The voltage is stored as129129+ * a 10-bit number plus sign, in the upper bits of a 16-bit register130130+ */131131+ err = ds2782_read_reg16(info, DS2782_REG_VOLT_MSB, &raw);132132+ if (err)133133+ return err;134134+ *voltage_uA = (raw / 32) * 4800;135135+ return 0;136136+}137137+138138+static int ds2782_get_capacity(struct ds2782_info *info, int *capacity)139139+{140140+ int err;141141+ u8 raw;142142+143143+ err = ds2782_read_reg(info, DS2782_REG_RARC, &raw);144144+ if (err)145145+ return err;146146+ *capacity = raw;147147+ return raw;148148+}149149+150150+static int ds2782_get_status(struct ds2782_info *info, int *status)151151+{152152+ int err;153153+ int current_uA;154154+ int capacity;155155+156156+ err = ds2782_get_current(info, ¤t_uA);157157+ if (err)158158+ return err;159159+160160+ err = ds2782_get_capacity(info, &capacity);161161+ if (err)162162+ return err;163163+164164+ if (capacity == 100)165165+ *status = POWER_SUPPLY_STATUS_FULL;166166+ else if (current_uA == 0)167167+ *status = POWER_SUPPLY_STATUS_NOT_CHARGING;168168+ else if (current_uA < 0)169169+ *status = POWER_SUPPLY_STATUS_DISCHARGING;170170+ else171171+ *status = POWER_SUPPLY_STATUS_CHARGING;172172+173173+ return 0;174174+}175175+176176+static int ds2782_battery_get_property(struct power_supply *psy,177177+ enum power_supply_property prop,178178+ union power_supply_propval *val)179179+{180180+ struct ds2782_info *info = to_ds2782_info(psy);181181+ int ret;182182+183183+ switch (prop) {184184+ case POWER_SUPPLY_PROP_STATUS:185185+ ret = ds2782_get_status(info, &val->intval);186186+ break;187187+188188+ case POWER_SUPPLY_PROP_CAPACITY:189189+ ret = ds2782_get_capacity(info, &val->intval);190190+ break;191191+192192+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:193193+ ret = ds2782_get_voltage(info, &val->intval);194194+ break;195195+196196+ case POWER_SUPPLY_PROP_CURRENT_NOW:197197+ ret = ds2782_get_current(info, &val->intval);198198+ break;199199+200200+ case POWER_SUPPLY_PROP_TEMP:201201+ ret = ds2782_get_temp(info, &val->intval);202202+ break;203203+204204+ default:205205+ ret = -EINVAL;206206+ }207207+208208+ return ret;209209+}210210+211211+static enum power_supply_property ds2782_battery_props[] = {212212+ POWER_SUPPLY_PROP_STATUS,213213+ POWER_SUPPLY_PROP_CAPACITY,214214+ POWER_SUPPLY_PROP_VOLTAGE_NOW,215215+ POWER_SUPPLY_PROP_CURRENT_NOW,216216+ POWER_SUPPLY_PROP_TEMP,217217+};218218+219219+static void ds2782_power_supply_init(struct power_supply *battery)220220+{221221+ battery->type = POWER_SUPPLY_TYPE_BATTERY;222222+ battery->properties = ds2782_battery_props;223223+ battery->num_properties = ARRAY_SIZE(ds2782_battery_props);224224+ battery->get_property = ds2782_battery_get_property;225225+ battery->external_power_changed = NULL;226226+}227227+228228+static int ds2782_battery_remove(struct i2c_client *client)229229+{230230+ struct ds2782_info *info = i2c_get_clientdata(client);231231+232232+ power_supply_unregister(&info->battery);233233+ kfree(info->battery.name);234234+235235+ mutex_lock(&battery_lock);236236+ idr_remove(&battery_id, info->id);237237+ mutex_unlock(&battery_lock);238238+239239+ i2c_set_clientdata(client, info);240240+241241+ kfree(info);242242+ return 0;243243+}244244+245245+static int ds2782_battery_probe(struct i2c_client *client,246246+ const struct i2c_device_id *id)247247+{248248+ struct ds2782_info *info;249249+ int ret;250250+ int num;251251+252252+ /* Get an ID for this battery */253253+ ret = idr_pre_get(&battery_id, GFP_KERNEL);254254+ if (ret == 0) {255255+ ret = -ENOMEM;256256+ goto fail_id;257257+ }258258+259259+ mutex_lock(&battery_lock);260260+ ret = idr_get_new(&battery_id, client, &num);261261+ mutex_unlock(&battery_lock);262262+ if (ret < 0)263263+ goto fail_id;264264+265265+ info = kzalloc(sizeof(*info), GFP_KERNEL);266266+ if (!info) {267267+ ret = -ENOMEM;268268+ goto fail_info;269269+ }270270+271271+ info->battery.name = kasprintf(GFP_KERNEL, "ds2782-%d", num);272272+ if (!info->battery.name) {273273+ ret = -ENOMEM;274274+ goto fail_name;275275+ }276276+277277+ i2c_set_clientdata(client, info);278278+ info->client = client;279279+ ds2782_power_supply_init(&info->battery);280280+281281+ ret = power_supply_register(&client->dev, &info->battery);282282+ if (ret) {283283+ dev_err(&client->dev, "failed to register battery\n");284284+ goto fail_register;285285+ }286286+287287+ return 0;288288+289289+fail_register:290290+ kfree(info->battery.name);291291+fail_name:292292+ i2c_set_clientdata(client, info);293293+ kfree(info);294294+fail_info:295295+ mutex_lock(&battery_lock);296296+ idr_remove(&battery_id, num);297297+ mutex_unlock(&battery_lock);298298+fail_id:299299+ return ret;300300+}301301+302302+static const struct i2c_device_id ds2782_id[] = {303303+ {"ds2782", 0},304304+ {},305305+};306306+307307+static struct i2c_driver ds2782_battery_driver = {308308+ .driver = {309309+ .name = "ds2782-battery",310310+ },311311+ .probe = ds2782_battery_probe,312312+ .remove = ds2782_battery_remove,313313+ .id_table = ds2782_id,314314+};315315+316316+static int __init ds2782_init(void)317317+{318318+ return i2c_add_driver(&ds2782_battery_driver);319319+}320320+module_init(ds2782_init);321321+322322+static void __exit ds2782_exit(void)323323+{324324+ i2c_del_driver(&ds2782_battery_driver);325325+}326326+module_exit(ds2782_exit);327327+328328+MODULE_AUTHOR("Ryan Mallon <ryan@bluewatersys.com>");329329+MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver");330330+MODULE_LICENSE("GPL");
+15-11
drivers/power/olpc_battery.c
···88 * published by the Free Software Foundation.99 */10101111+#include <linux/kernel.h>1112#include <linux/module.h>1213#include <linux/err.h>1314#include <linux/platform_device.h>···3635#define BAT_STAT_AC 0x103736#define BAT_STAT_CHARGING 0x203837#define BAT_STAT_DISCHARGING 0x403838+#define BAT_STAT_TRICKLE 0x8039394040#define BAT_ERR_INFOFAIL 0x024141#define BAT_ERR_OVERVOLTAGE 0x04···9189static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte)9290{9391 if (olpc_platform_info.ecver > 0x44) {9494- if (ec_byte & BAT_STAT_CHARGING)9292+ if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))9593 val->intval = POWER_SUPPLY_STATUS_CHARGING;9694 else if (ec_byte & BAT_STAT_DISCHARGING)9795 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;···221219 It doesn't matter though -- the EC will return the last-known222220 information, and it's as if we just ran that _little_ bit faster223221 and managed to read it out before the battery went away. */224224- if (!(ec_byte & BAT_STAT_PRESENT) && psp != POWER_SUPPLY_PROP_PRESENT)222222+ if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&223223+ psp != POWER_SUPPLY_PROP_PRESENT)225224 return -ENODEV;226225227226 switch (psp) {···232229 return ret;233230 break;234231 case POWER_SUPPLY_PROP_PRESENT:235235- val->intval = !!(ec_byte & BAT_STAT_PRESENT);232232+ val->intval = !!(ec_byte & (BAT_STAT_PRESENT |233233+ BAT_STAT_TRICKLE));236234 break;237235238236 case POWER_SUPPLY_PROP_HEALTH:···338334 struct bin_attribute *attr, char *buf, loff_t off, size_t count)339335{340336 uint8_t ec_byte;341341- int ret, end;337337+ int ret;338338+ int i;342339343340 if (off >= EEPROM_SIZE)344341 return 0;345342 if (off + count > EEPROM_SIZE)346343 count = EEPROM_SIZE - off;347344348348- end = EEPROM_START + off + count;349349- for (ec_byte = EEPROM_START + off; ec_byte < end; ec_byte++) {350350- ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1,351351- &buf[ec_byte - EEPROM_START], 1);345345+ for (i = 0; i < count; i++) {346346+ ec_byte = EEPROM_START + off + i;347347+ ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);352348 if (ret) {353353- printk(KERN_ERR "olpc-battery: EC command "354354- "EC_BAT_EEPROM @ 0x%x failed -"355355- " %d!\n", ec_byte, ret);349349+ pr_err("olpc-battery: "350350+ "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",351351+ ec_byte, ret);356352 return -EIO;357353 }358354 }