18c0984e5SSebastian Reichel /*
28c0984e5SSebastian Reichel * Driver for batteries with DS2760 chips inside.
38c0984e5SSebastian Reichel *
48c0984e5SSebastian Reichel * Copyright © 2007 Anton Vorontsov
58c0984e5SSebastian Reichel * 2004-2007 Matt Reimer
68c0984e5SSebastian Reichel * 2004 Szabolcs Gyurko
78c0984e5SSebastian Reichel *
88c0984e5SSebastian Reichel * Use consistent with the GNU GPL is permitted,
98c0984e5SSebastian Reichel * provided that this copyright notice is
108c0984e5SSebastian Reichel * preserved in its entirety in all copies and derived works.
118c0984e5SSebastian Reichel *
128c0984e5SSebastian Reichel * Author: Anton Vorontsov <cbou@mail.ru>
138c0984e5SSebastian Reichel * February 2007
148c0984e5SSebastian Reichel *
158c0984e5SSebastian Reichel * Matt Reimer <mreimer@vpop.net>
168c0984e5SSebastian Reichel * April 2004, 2005, 2007
178c0984e5SSebastian Reichel *
188c0984e5SSebastian Reichel * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
198c0984e5SSebastian Reichel * September 2004
208c0984e5SSebastian Reichel */
218c0984e5SSebastian Reichel
228c0984e5SSebastian Reichel #include <linux/module.h>
238c0984e5SSebastian Reichel #include <linux/param.h>
248c0984e5SSebastian Reichel #include <linux/jiffies.h>
258c0984e5SSebastian Reichel #include <linux/workqueue.h>
268c0984e5SSebastian Reichel #include <linux/pm.h>
278c0984e5SSebastian Reichel #include <linux/slab.h>
288c0984e5SSebastian Reichel #include <linux/platform_device.h>
298c0984e5SSebastian Reichel #include <linux/power_supply.h>
30bf497355SDaniel Mack #include <linux/suspend.h>
31de0d6dbdSAndrew F. Davis #include <linux/w1.h>
32efdafd68SDaniel Mack #include <linux/of.h>
33bf497355SDaniel Mack
34bf497355SDaniel Mack static unsigned int cache_time = 1000;
35bf497355SDaniel Mack module_param(cache_time, uint, 0644);
36bf497355SDaniel Mack MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
37bf497355SDaniel Mack
38bf497355SDaniel Mack static bool pmod_enabled;
39bf497355SDaniel Mack module_param(pmod_enabled, bool, 0644);
40bf497355SDaniel Mack MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
41bf497355SDaniel Mack
42bf497355SDaniel Mack static unsigned int rated_capacity;
43bf497355SDaniel Mack module_param(rated_capacity, uint, 0644);
44bf497355SDaniel Mack MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
45bf497355SDaniel Mack
46bf497355SDaniel Mack static unsigned int current_accum;
47bf497355SDaniel Mack module_param(current_accum, uint, 0644);
48bf497355SDaniel Mack MODULE_PARM_DESC(current_accum, "current accumulator value");
49bf497355SDaniel Mack
50bf497355SDaniel Mack #define W1_FAMILY_DS2760 0x30
51bf497355SDaniel Mack
52bf497355SDaniel Mack /* Known commands to the DS2760 chip */
53bf497355SDaniel Mack #define W1_DS2760_SWAP 0xAA
54bf497355SDaniel Mack #define W1_DS2760_READ_DATA 0x69
55bf497355SDaniel Mack #define W1_DS2760_WRITE_DATA 0x6C
56bf497355SDaniel Mack #define W1_DS2760_COPY_DATA 0x48
57bf497355SDaniel Mack #define W1_DS2760_RECALL_DATA 0xB8
58bf497355SDaniel Mack #define W1_DS2760_LOCK 0x6A
59bf497355SDaniel Mack
60bf497355SDaniel Mack /* Number of valid register addresses */
61bf497355SDaniel Mack #define DS2760_DATA_SIZE 0x40
62bf497355SDaniel Mack
63bf497355SDaniel Mack #define DS2760_PROTECTION_REG 0x00
64bf497355SDaniel Mack
65bf497355SDaniel Mack #define DS2760_STATUS_REG 0x01
66bf497355SDaniel Mack #define DS2760_STATUS_IE (1 << 2)
67bf497355SDaniel Mack #define DS2760_STATUS_SWEN (1 << 3)
68bf497355SDaniel Mack #define DS2760_STATUS_RNAOP (1 << 4)
69bf497355SDaniel Mack #define DS2760_STATUS_PMOD (1 << 5)
70bf497355SDaniel Mack
71bf497355SDaniel Mack #define DS2760_EEPROM_REG 0x07
72bf497355SDaniel Mack #define DS2760_SPECIAL_FEATURE_REG 0x08
73bf497355SDaniel Mack #define DS2760_VOLTAGE_MSB 0x0c
74bf497355SDaniel Mack #define DS2760_VOLTAGE_LSB 0x0d
75bf497355SDaniel Mack #define DS2760_CURRENT_MSB 0x0e
76bf497355SDaniel Mack #define DS2760_CURRENT_LSB 0x0f
77bf497355SDaniel Mack #define DS2760_CURRENT_ACCUM_MSB 0x10
78bf497355SDaniel Mack #define DS2760_CURRENT_ACCUM_LSB 0x11
79bf497355SDaniel Mack #define DS2760_TEMP_MSB 0x18
80bf497355SDaniel Mack #define DS2760_TEMP_LSB 0x19
81bf497355SDaniel Mack #define DS2760_EEPROM_BLOCK0 0x20
82bf497355SDaniel Mack #define DS2760_ACTIVE_FULL 0x20
83bf497355SDaniel Mack #define DS2760_EEPROM_BLOCK1 0x30
84bf497355SDaniel Mack #define DS2760_STATUS_WRITE_REG 0x31
85bf497355SDaniel Mack #define DS2760_RATED_CAPACITY 0x32
86bf497355SDaniel Mack #define DS2760_CURRENT_OFFSET_BIAS 0x33
87bf497355SDaniel Mack #define DS2760_ACTIVE_EMPTY 0x3b
888c0984e5SSebastian Reichel
898c0984e5SSebastian Reichel struct ds2760_device_info {
908c0984e5SSebastian Reichel struct device *dev;
918c0984e5SSebastian Reichel
928c0984e5SSebastian Reichel /* DS2760 data, valid after calling ds2760_battery_read_status() */
938c0984e5SSebastian Reichel unsigned long update_time; /* jiffies when data read */
948c0984e5SSebastian Reichel char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */
958c0984e5SSebastian Reichel int voltage_raw; /* units of 4.88 mV */
968c0984e5SSebastian Reichel int voltage_uV; /* units of µV */
978c0984e5SSebastian Reichel int current_raw; /* units of 0.625 mA */
988c0984e5SSebastian Reichel int current_uA; /* units of µA */
998c0984e5SSebastian Reichel int accum_current_raw; /* units of 0.25 mAh */
1008c0984e5SSebastian Reichel int accum_current_uAh; /* units of µAh */
1018c0984e5SSebastian Reichel int temp_raw; /* units of 0.125 °C */
1028c0984e5SSebastian Reichel int temp_C; /* units of 0.1 °C */
1038c0984e5SSebastian Reichel int rated_capacity; /* units of µAh */
1048c0984e5SSebastian Reichel int rem_capacity; /* percentage */
1058c0984e5SSebastian Reichel int full_active_uAh; /* units of µAh */
1068c0984e5SSebastian Reichel int empty_uAh; /* units of µAh */
1078c0984e5SSebastian Reichel int life_sec; /* units of seconds */
1088c0984e5SSebastian Reichel int charge_status; /* POWER_SUPPLY_STATUS_* */
1098c0984e5SSebastian Reichel
1108c0984e5SSebastian Reichel int full_counter;
1118c0984e5SSebastian Reichel struct power_supply *bat;
1128c0984e5SSebastian Reichel struct power_supply_desc bat_desc;
1138c0984e5SSebastian Reichel struct workqueue_struct *monitor_wqueue;
1148c0984e5SSebastian Reichel struct delayed_work monitor_work;
1158c0984e5SSebastian Reichel struct delayed_work set_charged_work;
116bf497355SDaniel Mack struct notifier_block pm_notifier;
1178c0984e5SSebastian Reichel };
1188c0984e5SSebastian Reichel
w1_ds2760_io(struct device * dev,char * buf,int addr,size_t count,int io)119bf497355SDaniel Mack static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
120bf497355SDaniel Mack int io)
121bf497355SDaniel Mack {
122bf497355SDaniel Mack struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
1238c0984e5SSebastian Reichel
124bf497355SDaniel Mack if (!dev)
125bf497355SDaniel Mack return 0;
1268c0984e5SSebastian Reichel
127bf497355SDaniel Mack mutex_lock(&sl->master->bus_mutex);
1288c0984e5SSebastian Reichel
129bf497355SDaniel Mack if (addr > DS2760_DATA_SIZE || addr < 0) {
130bf497355SDaniel Mack count = 0;
131bf497355SDaniel Mack goto out;
132bf497355SDaniel Mack }
133bf497355SDaniel Mack if (addr + count > DS2760_DATA_SIZE)
134bf497355SDaniel Mack count = DS2760_DATA_SIZE - addr;
1358c0984e5SSebastian Reichel
136bf497355SDaniel Mack if (!w1_reset_select_slave(sl)) {
137bf497355SDaniel Mack if (!io) {
138bf497355SDaniel Mack w1_write_8(sl->master, W1_DS2760_READ_DATA);
139bf497355SDaniel Mack w1_write_8(sl->master, addr);
140bf497355SDaniel Mack count = w1_read_block(sl->master, buf, count);
141bf497355SDaniel Mack } else {
142bf497355SDaniel Mack w1_write_8(sl->master, W1_DS2760_WRITE_DATA);
143bf497355SDaniel Mack w1_write_8(sl->master, addr);
144bf497355SDaniel Mack w1_write_block(sl->master, buf, count);
145bf497355SDaniel Mack /* XXX w1_write_block returns void, not n_written */
146bf497355SDaniel Mack }
147bf497355SDaniel Mack }
148bf497355SDaniel Mack
149bf497355SDaniel Mack out:
150bf497355SDaniel Mack mutex_unlock(&sl->master->bus_mutex);
151bf497355SDaniel Mack
152bf497355SDaniel Mack return count;
153bf497355SDaniel Mack }
154bf497355SDaniel Mack
w1_ds2760_read(struct device * dev,char * buf,int addr,size_t count)155bf497355SDaniel Mack static int w1_ds2760_read(struct device *dev,
156bf497355SDaniel Mack char *buf, int addr,
157bf497355SDaniel Mack size_t count)
158bf497355SDaniel Mack {
159bf497355SDaniel Mack return w1_ds2760_io(dev, buf, addr, count, 0);
160bf497355SDaniel Mack }
161bf497355SDaniel Mack
w1_ds2760_write(struct device * dev,char * buf,int addr,size_t count)162bf497355SDaniel Mack static int w1_ds2760_write(struct device *dev,
163bf497355SDaniel Mack char *buf,
164bf497355SDaniel Mack int addr, size_t count)
165bf497355SDaniel Mack {
166bf497355SDaniel Mack return w1_ds2760_io(dev, buf, addr, count, 1);
167bf497355SDaniel Mack }
168bf497355SDaniel Mack
w1_ds2760_eeprom_cmd(struct device * dev,int addr,int cmd)169bf497355SDaniel Mack static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
170bf497355SDaniel Mack {
171bf497355SDaniel Mack struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
172bf497355SDaniel Mack
173bf497355SDaniel Mack if (!dev)
174bf497355SDaniel Mack return -EINVAL;
175bf497355SDaniel Mack
176bf497355SDaniel Mack mutex_lock(&sl->master->bus_mutex);
177bf497355SDaniel Mack
178bf497355SDaniel Mack if (w1_reset_select_slave(sl) == 0) {
179bf497355SDaniel Mack w1_write_8(sl->master, cmd);
180bf497355SDaniel Mack w1_write_8(sl->master, addr);
181bf497355SDaniel Mack }
182bf497355SDaniel Mack
183bf497355SDaniel Mack mutex_unlock(&sl->master->bus_mutex);
184bf497355SDaniel Mack return 0;
185bf497355SDaniel Mack }
186bf497355SDaniel Mack
w1_ds2760_store_eeprom(struct device * dev,int addr)187bf497355SDaniel Mack static int w1_ds2760_store_eeprom(struct device *dev, int addr)
188bf497355SDaniel Mack {
189bf497355SDaniel Mack return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
190bf497355SDaniel Mack }
191bf497355SDaniel Mack
w1_ds2760_recall_eeprom(struct device * dev,int addr)192bf497355SDaniel Mack static int w1_ds2760_recall_eeprom(struct device *dev, int addr)
193bf497355SDaniel Mack {
194bf497355SDaniel Mack return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
195bf497355SDaniel Mack }
196bf497355SDaniel Mack
w1_slave_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)197bf497355SDaniel Mack static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj,
198bf497355SDaniel Mack struct bin_attribute *bin_attr, char *buf,
199bf497355SDaniel Mack loff_t off, size_t count)
200bf497355SDaniel Mack {
201*a72acc56SJian Dong struct device *dev = kobj_to_dev(kobj);
202bf497355SDaniel Mack return w1_ds2760_read(dev, buf, off, count);
203bf497355SDaniel Mack }
204bf497355SDaniel Mack
205bf497355SDaniel Mack static BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE);
206bf497355SDaniel Mack
207bf497355SDaniel Mack static struct bin_attribute *w1_ds2760_bin_attrs[] = {
208bf497355SDaniel Mack &bin_attr_w1_slave,
209bf497355SDaniel Mack NULL,
210bf497355SDaniel Mack };
211bf497355SDaniel Mack
212bf497355SDaniel Mack static const struct attribute_group w1_ds2760_group = {
213bf497355SDaniel Mack .bin_attrs = w1_ds2760_bin_attrs,
214bf497355SDaniel Mack };
215bf497355SDaniel Mack
216bf497355SDaniel Mack static const struct attribute_group *w1_ds2760_groups[] = {
217bf497355SDaniel Mack &w1_ds2760_group,
218bf497355SDaniel Mack NULL,
219bf497355SDaniel Mack };
2208c0984e5SSebastian Reichel /* Some batteries have their rated capacity stored a N * 10 mAh, while
2218c0984e5SSebastian Reichel * others use an index into this table. */
2228c0984e5SSebastian Reichel static int rated_capacities[] = {
2238c0984e5SSebastian Reichel 0,
2248c0984e5SSebastian Reichel 920, /* Samsung */
2258c0984e5SSebastian Reichel 920, /* BYD */
2268c0984e5SSebastian Reichel 920, /* Lishen */
2278c0984e5SSebastian Reichel 920, /* NEC */
2288c0984e5SSebastian Reichel 1440, /* Samsung */
2298c0984e5SSebastian Reichel 1440, /* BYD */
2308c0984e5SSebastian Reichel 1440, /* Lishen */
2318c0984e5SSebastian Reichel 1440, /* NEC */
2328c0984e5SSebastian Reichel 2880, /* Samsung */
2338c0984e5SSebastian Reichel 2880, /* BYD */
2348c0984e5SSebastian Reichel 2880, /* Lishen */
2358c0984e5SSebastian Reichel 2880, /* NEC */
2368c0984e5SSebastian Reichel };
2378c0984e5SSebastian Reichel
2388c0984e5SSebastian Reichel /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
2398c0984e5SSebastian Reichel * temp is in Celsius */
battery_interpolate(int array[],int temp)2408c0984e5SSebastian Reichel static int battery_interpolate(int array[], int temp)
2418c0984e5SSebastian Reichel {
2428c0984e5SSebastian Reichel int index, dt;
2438c0984e5SSebastian Reichel
2448c0984e5SSebastian Reichel if (temp <= 0)
2458c0984e5SSebastian Reichel return array[0];
2468c0984e5SSebastian Reichel if (temp >= 40)
2478c0984e5SSebastian Reichel return array[4];
2488c0984e5SSebastian Reichel
2498c0984e5SSebastian Reichel index = temp / 10;
2508c0984e5SSebastian Reichel dt = temp % 10;
2518c0984e5SSebastian Reichel
2528c0984e5SSebastian Reichel return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
2538c0984e5SSebastian Reichel }
2548c0984e5SSebastian Reichel
ds2760_battery_read_status(struct ds2760_device_info * di)2558c0984e5SSebastian Reichel static int ds2760_battery_read_status(struct ds2760_device_info *di)
2568c0984e5SSebastian Reichel {
2578c0984e5SSebastian Reichel int ret, i, start, count, scale[5];
2588c0984e5SSebastian Reichel
2598c0984e5SSebastian Reichel if (di->update_time && time_before(jiffies, di->update_time +
2608c0984e5SSebastian Reichel msecs_to_jiffies(cache_time)))
2618c0984e5SSebastian Reichel return 0;
2628c0984e5SSebastian Reichel
2638c0984e5SSebastian Reichel /* The first time we read the entire contents of SRAM/EEPROM,
2648c0984e5SSebastian Reichel * but after that we just read the interesting bits that change. */
2658c0984e5SSebastian Reichel if (di->update_time == 0) {
2668c0984e5SSebastian Reichel start = 0;
2678c0984e5SSebastian Reichel count = DS2760_DATA_SIZE;
2688c0984e5SSebastian Reichel } else {
2698c0984e5SSebastian Reichel start = DS2760_VOLTAGE_MSB;
2708c0984e5SSebastian Reichel count = DS2760_TEMP_LSB - start + 1;
2718c0984e5SSebastian Reichel }
2728c0984e5SSebastian Reichel
273bf497355SDaniel Mack ret = w1_ds2760_read(di->dev, di->raw + start, start, count);
2748c0984e5SSebastian Reichel if (ret != count) {
2758c0984e5SSebastian Reichel dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
276bf497355SDaniel Mack di->dev);
2778c0984e5SSebastian Reichel return 1;
2788c0984e5SSebastian Reichel }
2798c0984e5SSebastian Reichel
2808c0984e5SSebastian Reichel di->update_time = jiffies;
2818c0984e5SSebastian Reichel
2828c0984e5SSebastian Reichel /* DS2760 reports voltage in units of 4.88mV, but the battery class
2838c0984e5SSebastian Reichel * reports in units of uV, so convert by multiplying by 4880. */
2848c0984e5SSebastian Reichel di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
2858c0984e5SSebastian Reichel (di->raw[DS2760_VOLTAGE_LSB] >> 5);
2868c0984e5SSebastian Reichel di->voltage_uV = di->voltage_raw * 4880;
2878c0984e5SSebastian Reichel
2888c0984e5SSebastian Reichel /* DS2760 reports current in signed units of 0.625mA, but the battery
2898c0984e5SSebastian Reichel * class reports in units of µA, so convert by multiplying by 625. */
2908c0984e5SSebastian Reichel di->current_raw =
2918c0984e5SSebastian Reichel (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
2928c0984e5SSebastian Reichel (di->raw[DS2760_CURRENT_LSB] >> 3);
2938c0984e5SSebastian Reichel di->current_uA = di->current_raw * 625;
2948c0984e5SSebastian Reichel
2958c0984e5SSebastian Reichel /* DS2760 reports accumulated current in signed units of 0.25mAh. */
2968c0984e5SSebastian Reichel di->accum_current_raw =
2978c0984e5SSebastian Reichel (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
2988c0984e5SSebastian Reichel di->raw[DS2760_CURRENT_ACCUM_LSB];
2998c0984e5SSebastian Reichel di->accum_current_uAh = di->accum_current_raw * 250;
3008c0984e5SSebastian Reichel
3018c0984e5SSebastian Reichel /* DS2760 reports temperature in signed units of 0.125°C, but the
3028c0984e5SSebastian Reichel * battery class reports in units of 1/10 °C, so we convert by
3038c0984e5SSebastian Reichel * multiplying by .125 * 10 = 1.25. */
3048c0984e5SSebastian Reichel di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
3058c0984e5SSebastian Reichel (di->raw[DS2760_TEMP_LSB] >> 5);
3068c0984e5SSebastian Reichel di->temp_C = di->temp_raw + (di->temp_raw / 4);
3078c0984e5SSebastian Reichel
3088c0984e5SSebastian Reichel /* At least some battery monitors (e.g. HP iPAQ) store the battery's
3098c0984e5SSebastian Reichel * maximum rated capacity. */
3108c0984e5SSebastian Reichel if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
3118c0984e5SSebastian Reichel di->rated_capacity = rated_capacities[
3128c0984e5SSebastian Reichel (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
3138c0984e5SSebastian Reichel else
3148c0984e5SSebastian Reichel di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
3158c0984e5SSebastian Reichel
3168c0984e5SSebastian Reichel di->rated_capacity *= 1000; /* convert to µAh */
3178c0984e5SSebastian Reichel
3188c0984e5SSebastian Reichel /* Calculate the full level at the present temperature. */
3198c0984e5SSebastian Reichel di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
3208c0984e5SSebastian Reichel di->raw[DS2760_ACTIVE_FULL + 1];
3218c0984e5SSebastian Reichel
3228c0984e5SSebastian Reichel /* If the full_active_uAh value is not given, fall back to the rated
3238c0984e5SSebastian Reichel * capacity. This is likely to happen when chips are not part of the
3248c0984e5SSebastian Reichel * battery pack and is therefore not bootstrapped. */
3258c0984e5SSebastian Reichel if (di->full_active_uAh == 0)
3268c0984e5SSebastian Reichel di->full_active_uAh = di->rated_capacity / 1000L;
3278c0984e5SSebastian Reichel
3288c0984e5SSebastian Reichel scale[0] = di->full_active_uAh;
3298c0984e5SSebastian Reichel for (i = 1; i < 5; i++)
3308c0984e5SSebastian Reichel scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i];
3318c0984e5SSebastian Reichel
3328c0984e5SSebastian Reichel di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
3338c0984e5SSebastian Reichel di->full_active_uAh *= 1000; /* convert to µAh */
3348c0984e5SSebastian Reichel
3358c0984e5SSebastian Reichel /* Calculate the empty level at the present temperature. */
3368c0984e5SSebastian Reichel scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
3378c0984e5SSebastian Reichel for (i = 3; i >= 0; i--)
3388c0984e5SSebastian Reichel scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
3398c0984e5SSebastian Reichel
3408c0984e5SSebastian Reichel di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
3418c0984e5SSebastian Reichel di->empty_uAh *= 1000; /* convert to µAh */
3428c0984e5SSebastian Reichel
3438c0984e5SSebastian Reichel if (di->full_active_uAh == di->empty_uAh)
3448c0984e5SSebastian Reichel di->rem_capacity = 0;
3458c0984e5SSebastian Reichel else
3468c0984e5SSebastian Reichel /* From Maxim Application Note 131: remaining capacity =
3478c0984e5SSebastian Reichel * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
3488c0984e5SSebastian Reichel di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
3498c0984e5SSebastian Reichel (di->full_active_uAh - di->empty_uAh);
3508c0984e5SSebastian Reichel
3518c0984e5SSebastian Reichel if (di->rem_capacity < 0)
3528c0984e5SSebastian Reichel di->rem_capacity = 0;
3538c0984e5SSebastian Reichel if (di->rem_capacity > 100)
3548c0984e5SSebastian Reichel di->rem_capacity = 100;
3558c0984e5SSebastian Reichel
3568c0984e5SSebastian Reichel if (di->current_uA < -100L)
3578c0984e5SSebastian Reichel di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
3588c0984e5SSebastian Reichel / (di->current_uA / 100L);
3598c0984e5SSebastian Reichel else
3608c0984e5SSebastian Reichel di->life_sec = 0;
3618c0984e5SSebastian Reichel
3628c0984e5SSebastian Reichel return 0;
3638c0984e5SSebastian Reichel }
3648c0984e5SSebastian Reichel
ds2760_battery_set_current_accum(struct ds2760_device_info * di,unsigned int acr_val)3658c0984e5SSebastian Reichel static void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
3668c0984e5SSebastian Reichel unsigned int acr_val)
3678c0984e5SSebastian Reichel {
3688c0984e5SSebastian Reichel unsigned char acr[2];
3698c0984e5SSebastian Reichel
3708c0984e5SSebastian Reichel /* acr is in units of 0.25 mAh */
3718c0984e5SSebastian Reichel acr_val *= 4L;
3728c0984e5SSebastian Reichel acr_val /= 1000;
3738c0984e5SSebastian Reichel
3748c0984e5SSebastian Reichel acr[0] = acr_val >> 8;
3758c0984e5SSebastian Reichel acr[1] = acr_val & 0xff;
3768c0984e5SSebastian Reichel
377bf497355SDaniel Mack if (w1_ds2760_write(di->dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
3788c0984e5SSebastian Reichel dev_warn(di->dev, "ACR write failed\n");
3798c0984e5SSebastian Reichel }
3808c0984e5SSebastian Reichel
ds2760_battery_update_status(struct ds2760_device_info * di)3818c0984e5SSebastian Reichel static void ds2760_battery_update_status(struct ds2760_device_info *di)
3828c0984e5SSebastian Reichel {
3838c0984e5SSebastian Reichel int old_charge_status = di->charge_status;
3848c0984e5SSebastian Reichel
3858c0984e5SSebastian Reichel ds2760_battery_read_status(di);
3868c0984e5SSebastian Reichel
3878c0984e5SSebastian Reichel if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
3888c0984e5SSebastian Reichel di->full_counter = 0;
3898c0984e5SSebastian Reichel
3908c0984e5SSebastian Reichel if (power_supply_am_i_supplied(di->bat)) {
3918c0984e5SSebastian Reichel if (di->current_uA > 10000) {
3928c0984e5SSebastian Reichel di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
3938c0984e5SSebastian Reichel di->full_counter = 0;
3948c0984e5SSebastian Reichel } else if (di->current_uA < -5000) {
3958c0984e5SSebastian Reichel if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
3968c0984e5SSebastian Reichel dev_notice(di->dev, "not enough power to "
3978c0984e5SSebastian Reichel "charge\n");
3988c0984e5SSebastian Reichel di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
3998c0984e5SSebastian Reichel di->full_counter = 0;
4008c0984e5SSebastian Reichel } else if (di->current_uA < 10000 &&
4018c0984e5SSebastian Reichel di->charge_status != POWER_SUPPLY_STATUS_FULL) {
4028c0984e5SSebastian Reichel
4038c0984e5SSebastian Reichel /* Don't consider the battery to be full unless
4048c0984e5SSebastian Reichel * we've seen the current < 10 mA at least two
4058c0984e5SSebastian Reichel * consecutive times. */
4068c0984e5SSebastian Reichel
4078c0984e5SSebastian Reichel di->full_counter++;
4088c0984e5SSebastian Reichel
4098c0984e5SSebastian Reichel if (di->full_counter < 2) {
4108c0984e5SSebastian Reichel di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
4118c0984e5SSebastian Reichel } else {
4128c0984e5SSebastian Reichel di->charge_status = POWER_SUPPLY_STATUS_FULL;
4138c0984e5SSebastian Reichel ds2760_battery_set_current_accum(di,
4148c0984e5SSebastian Reichel di->full_active_uAh);
4158c0984e5SSebastian Reichel }
4168c0984e5SSebastian Reichel }
4178c0984e5SSebastian Reichel } else {
4188c0984e5SSebastian Reichel di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
4198c0984e5SSebastian Reichel di->full_counter = 0;
4208c0984e5SSebastian Reichel }
4218c0984e5SSebastian Reichel
4228c0984e5SSebastian Reichel if (di->charge_status != old_charge_status)
4238c0984e5SSebastian Reichel power_supply_changed(di->bat);
4248c0984e5SSebastian Reichel }
4258c0984e5SSebastian Reichel
ds2760_battery_write_status(struct ds2760_device_info * di,char status)4268c0984e5SSebastian Reichel static void ds2760_battery_write_status(struct ds2760_device_info *di,
4278c0984e5SSebastian Reichel char status)
4288c0984e5SSebastian Reichel {
4298c0984e5SSebastian Reichel if (status == di->raw[DS2760_STATUS_REG])
4308c0984e5SSebastian Reichel return;
4318c0984e5SSebastian Reichel
432bf497355SDaniel Mack w1_ds2760_write(di->dev, &status, DS2760_STATUS_WRITE_REG, 1);
433bf497355SDaniel Mack w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
434bf497355SDaniel Mack w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
4358c0984e5SSebastian Reichel }
4368c0984e5SSebastian Reichel
ds2760_battery_write_rated_capacity(struct ds2760_device_info * di,unsigned char rated_capacity)4378c0984e5SSebastian Reichel static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
4388c0984e5SSebastian Reichel unsigned char rated_capacity)
4398c0984e5SSebastian Reichel {
4408c0984e5SSebastian Reichel if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
4418c0984e5SSebastian Reichel return;
4428c0984e5SSebastian Reichel
443bf497355SDaniel Mack w1_ds2760_write(di->dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
444bf497355SDaniel Mack w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
445bf497355SDaniel Mack w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
4468c0984e5SSebastian Reichel }
4478c0984e5SSebastian Reichel
ds2760_battery_write_active_full(struct ds2760_device_info * di,int active_full)4488c0984e5SSebastian Reichel static void ds2760_battery_write_active_full(struct ds2760_device_info *di,
4498c0984e5SSebastian Reichel int active_full)
4508c0984e5SSebastian Reichel {
4518c0984e5SSebastian Reichel unsigned char tmp[2] = {
4528c0984e5SSebastian Reichel active_full >> 8,
4538c0984e5SSebastian Reichel active_full & 0xff
4548c0984e5SSebastian Reichel };
4558c0984e5SSebastian Reichel
4568c0984e5SSebastian Reichel if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
4578c0984e5SSebastian Reichel tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
4588c0984e5SSebastian Reichel return;
4598c0984e5SSebastian Reichel
460bf497355SDaniel Mack w1_ds2760_write(di->dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp));
461bf497355SDaniel Mack w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
462bf497355SDaniel Mack w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
4638c0984e5SSebastian Reichel
4648c0984e5SSebastian Reichel /* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
4658c0984e5SSebastian Reichel * values won't be read back by ds2760_battery_read_status() */
4668c0984e5SSebastian Reichel di->raw[DS2760_ACTIVE_FULL] = tmp[0];
4678c0984e5SSebastian Reichel di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1];
4688c0984e5SSebastian Reichel }
4698c0984e5SSebastian Reichel
ds2760_battery_work(struct work_struct * work)4708c0984e5SSebastian Reichel static void ds2760_battery_work(struct work_struct *work)
4718c0984e5SSebastian Reichel {
4728c0984e5SSebastian Reichel struct ds2760_device_info *di = container_of(work,
4738c0984e5SSebastian Reichel struct ds2760_device_info, monitor_work.work);
4748c0984e5SSebastian Reichel const int interval = HZ * 60;
4758c0984e5SSebastian Reichel
4768c0984e5SSebastian Reichel dev_dbg(di->dev, "%s\n", __func__);
4778c0984e5SSebastian Reichel
4788c0984e5SSebastian Reichel ds2760_battery_update_status(di);
4798c0984e5SSebastian Reichel queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
4808c0984e5SSebastian Reichel }
4818c0984e5SSebastian Reichel
ds2760_battery_external_power_changed(struct power_supply * psy)4828c0984e5SSebastian Reichel static void ds2760_battery_external_power_changed(struct power_supply *psy)
4838c0984e5SSebastian Reichel {
4848c0984e5SSebastian Reichel struct ds2760_device_info *di = power_supply_get_drvdata(psy);
4858c0984e5SSebastian Reichel
4868c0984e5SSebastian Reichel dev_dbg(di->dev, "%s\n", __func__);
4878c0984e5SSebastian Reichel
4888c0984e5SSebastian Reichel mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
4898c0984e5SSebastian Reichel }
4908c0984e5SSebastian Reichel
4918c0984e5SSebastian Reichel
ds2760_battery_set_charged_work(struct work_struct * work)4928c0984e5SSebastian Reichel static void ds2760_battery_set_charged_work(struct work_struct *work)
4938c0984e5SSebastian Reichel {
4948c0984e5SSebastian Reichel char bias;
4958c0984e5SSebastian Reichel struct ds2760_device_info *di = container_of(work,
4968c0984e5SSebastian Reichel struct ds2760_device_info, set_charged_work.work);
4978c0984e5SSebastian Reichel
4988c0984e5SSebastian Reichel dev_dbg(di->dev, "%s\n", __func__);
4998c0984e5SSebastian Reichel
5008c0984e5SSebastian Reichel ds2760_battery_read_status(di);
5018c0984e5SSebastian Reichel
5028c0984e5SSebastian Reichel /* When we get notified by external circuitry that the battery is
5038c0984e5SSebastian Reichel * considered fully charged now, we know that there is no current
5048c0984e5SSebastian Reichel * flow any more. However, the ds2760's internal current meter is
5058c0984e5SSebastian Reichel * too inaccurate to rely on - spec say something ~15% failure.
5068c0984e5SSebastian Reichel * Hence, we use the current offset bias register to compensate
5078c0984e5SSebastian Reichel * that error.
5088c0984e5SSebastian Reichel */
5098c0984e5SSebastian Reichel
5108c0984e5SSebastian Reichel if (!power_supply_am_i_supplied(di->bat))
5118c0984e5SSebastian Reichel return;
5128c0984e5SSebastian Reichel
5138c0984e5SSebastian Reichel bias = (signed char) di->current_raw +
5148c0984e5SSebastian Reichel (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
5158c0984e5SSebastian Reichel
5168c0984e5SSebastian Reichel dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
5178c0984e5SSebastian Reichel
518bf497355SDaniel Mack w1_ds2760_write(di->dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1);
519bf497355SDaniel Mack w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
520bf497355SDaniel Mack w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
5218c0984e5SSebastian Reichel
5228c0984e5SSebastian Reichel /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
5238c0984e5SSebastian Reichel * value won't be read back by ds2760_battery_read_status() */
5248c0984e5SSebastian Reichel di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias;
5258c0984e5SSebastian Reichel }
5268c0984e5SSebastian Reichel
ds2760_battery_set_charged(struct power_supply * psy)5278c0984e5SSebastian Reichel static void ds2760_battery_set_charged(struct power_supply *psy)
5288c0984e5SSebastian Reichel {
5298c0984e5SSebastian Reichel struct ds2760_device_info *di = power_supply_get_drvdata(psy);
5308c0984e5SSebastian Reichel
5318c0984e5SSebastian Reichel /* postpone the actual work by 20 secs. This is for debouncing GPIO
5328c0984e5SSebastian Reichel * signals and to let the current value settle. See AN4188. */
5338c0984e5SSebastian Reichel mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20);
5348c0984e5SSebastian Reichel }
5358c0984e5SSebastian Reichel
ds2760_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)5368c0984e5SSebastian Reichel static int ds2760_battery_get_property(struct power_supply *psy,
5378c0984e5SSebastian Reichel enum power_supply_property psp,
5388c0984e5SSebastian Reichel union power_supply_propval *val)
5398c0984e5SSebastian Reichel {
5408c0984e5SSebastian Reichel struct ds2760_device_info *di = power_supply_get_drvdata(psy);
5418c0984e5SSebastian Reichel
5428c0984e5SSebastian Reichel switch (psp) {
5438c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_STATUS:
5448c0984e5SSebastian Reichel val->intval = di->charge_status;
5458c0984e5SSebastian Reichel return 0;
5468c0984e5SSebastian Reichel default:
5478c0984e5SSebastian Reichel break;
5488c0984e5SSebastian Reichel }
5498c0984e5SSebastian Reichel
5508c0984e5SSebastian Reichel ds2760_battery_read_status(di);
5518c0984e5SSebastian Reichel
5528c0984e5SSebastian Reichel switch (psp) {
5538c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_VOLTAGE_NOW:
5548c0984e5SSebastian Reichel val->intval = di->voltage_uV;
5558c0984e5SSebastian Reichel break;
5568c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CURRENT_NOW:
5578c0984e5SSebastian Reichel val->intval = di->current_uA;
5588c0984e5SSebastian Reichel break;
5598c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
5608c0984e5SSebastian Reichel val->intval = di->rated_capacity;
5618c0984e5SSebastian Reichel break;
5628c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_FULL:
5638c0984e5SSebastian Reichel val->intval = di->full_active_uAh;
5648c0984e5SSebastian Reichel break;
5658c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_EMPTY:
5668c0984e5SSebastian Reichel val->intval = di->empty_uAh;
5678c0984e5SSebastian Reichel break;
5688c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_NOW:
5698c0984e5SSebastian Reichel val->intval = di->accum_current_uAh;
5708c0984e5SSebastian Reichel break;
5718c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TEMP:
5728c0984e5SSebastian Reichel val->intval = di->temp_C;
5738c0984e5SSebastian Reichel break;
5748c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
5758c0984e5SSebastian Reichel val->intval = di->life_sec;
5768c0984e5SSebastian Reichel break;
5778c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CAPACITY:
5788c0984e5SSebastian Reichel val->intval = di->rem_capacity;
5798c0984e5SSebastian Reichel break;
5808c0984e5SSebastian Reichel default:
5818c0984e5SSebastian Reichel return -EINVAL;
5828c0984e5SSebastian Reichel }
5838c0984e5SSebastian Reichel
5848c0984e5SSebastian Reichel return 0;
5858c0984e5SSebastian Reichel }
5868c0984e5SSebastian Reichel
ds2760_battery_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)5878c0984e5SSebastian Reichel static int ds2760_battery_set_property(struct power_supply *psy,
5888c0984e5SSebastian Reichel enum power_supply_property psp,
5898c0984e5SSebastian Reichel const union power_supply_propval *val)
5908c0984e5SSebastian Reichel {
5918c0984e5SSebastian Reichel struct ds2760_device_info *di = power_supply_get_drvdata(psy);
5928c0984e5SSebastian Reichel
5938c0984e5SSebastian Reichel switch (psp) {
5948c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_FULL:
5958c0984e5SSebastian Reichel /* the interface counts in uAh, convert the value */
5968c0984e5SSebastian Reichel ds2760_battery_write_active_full(di, val->intval / 1000L);
5978c0984e5SSebastian Reichel break;
5988c0984e5SSebastian Reichel
5998c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_NOW:
6008c0984e5SSebastian Reichel /* ds2760_battery_set_current_accum() does the conversion */
6018c0984e5SSebastian Reichel ds2760_battery_set_current_accum(di, val->intval);
6028c0984e5SSebastian Reichel break;
6038c0984e5SSebastian Reichel
6048c0984e5SSebastian Reichel default:
6058c0984e5SSebastian Reichel return -EPERM;
6068c0984e5SSebastian Reichel }
6078c0984e5SSebastian Reichel
6088c0984e5SSebastian Reichel return 0;
6098c0984e5SSebastian Reichel }
6108c0984e5SSebastian Reichel
ds2760_battery_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)6118c0984e5SSebastian Reichel static int ds2760_battery_property_is_writeable(struct power_supply *psy,
6128c0984e5SSebastian Reichel enum power_supply_property psp)
6138c0984e5SSebastian Reichel {
6148c0984e5SSebastian Reichel switch (psp) {
6158c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_FULL:
6168c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_NOW:
6178c0984e5SSebastian Reichel return 1;
6188c0984e5SSebastian Reichel
6198c0984e5SSebastian Reichel default:
6208c0984e5SSebastian Reichel break;
6218c0984e5SSebastian Reichel }
6228c0984e5SSebastian Reichel
6238c0984e5SSebastian Reichel return 0;
6248c0984e5SSebastian Reichel }
6258c0984e5SSebastian Reichel
6268c0984e5SSebastian Reichel static enum power_supply_property ds2760_battery_props[] = {
6278c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS,
6288c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW,
6298c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW,
6308c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
6318c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_FULL,
6328c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_EMPTY,
6338c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW,
6348c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP,
6358c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
6368c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY,
6378c0984e5SSebastian Reichel };
6388c0984e5SSebastian Reichel
ds2760_pm_notifier(struct notifier_block * notifier,unsigned long pm_event,void * unused)639bf497355SDaniel Mack static int ds2760_pm_notifier(struct notifier_block *notifier,
640bf497355SDaniel Mack unsigned long pm_event,
641bf497355SDaniel Mack void *unused)
642bf497355SDaniel Mack {
643bf497355SDaniel Mack struct ds2760_device_info *di =
644bf497355SDaniel Mack container_of(notifier, struct ds2760_device_info, pm_notifier);
645bf497355SDaniel Mack
646bf497355SDaniel Mack switch (pm_event) {
647bf497355SDaniel Mack case PM_HIBERNATION_PREPARE:
648bf497355SDaniel Mack case PM_SUSPEND_PREPARE:
649bf497355SDaniel Mack di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
650bf497355SDaniel Mack break;
651bf497355SDaniel Mack
652bf497355SDaniel Mack case PM_POST_RESTORE:
653bf497355SDaniel Mack case PM_POST_HIBERNATION:
654bf497355SDaniel Mack case PM_POST_SUSPEND:
655bf497355SDaniel Mack di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
656bf497355SDaniel Mack power_supply_changed(di->bat);
657bf497355SDaniel Mack mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
658bf497355SDaniel Mack
659bf497355SDaniel Mack break;
660bf497355SDaniel Mack
661bf497355SDaniel Mack case PM_RESTORE_PREPARE:
662bf497355SDaniel Mack default:
663bf497355SDaniel Mack break;
664bf497355SDaniel Mack }
665bf497355SDaniel Mack
666bf497355SDaniel Mack return NOTIFY_DONE;
667bf497355SDaniel Mack }
668bf497355SDaniel Mack
w1_ds2760_add_slave(struct w1_slave * sl)669bf497355SDaniel Mack static int w1_ds2760_add_slave(struct w1_slave *sl)
6708c0984e5SSebastian Reichel {
6718c0984e5SSebastian Reichel struct power_supply_config psy_cfg = {};
6728c0984e5SSebastian Reichel struct ds2760_device_info *di;
673bf497355SDaniel Mack struct device *dev = &sl->dev;
674bf497355SDaniel Mack int retval = 0;
675bf497355SDaniel Mack char name[32];
676bf497355SDaniel Mack char status;
6778c0984e5SSebastian Reichel
678bf497355SDaniel Mack di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
6798c0984e5SSebastian Reichel if (!di) {
6808c0984e5SSebastian Reichel retval = -ENOMEM;
6818c0984e5SSebastian Reichel goto di_alloc_failed;
6828c0984e5SSebastian Reichel }
6838c0984e5SSebastian Reichel
684bf497355SDaniel Mack snprintf(name, sizeof(name), "ds2760-battery.%d", dev->id);
6858c0984e5SSebastian Reichel
686bf497355SDaniel Mack di->dev = dev;
687bf497355SDaniel Mack di->bat_desc.name = name;
6888c0984e5SSebastian Reichel di->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
6898c0984e5SSebastian Reichel di->bat_desc.properties = ds2760_battery_props;
6908c0984e5SSebastian Reichel di->bat_desc.num_properties = ARRAY_SIZE(ds2760_battery_props);
6918c0984e5SSebastian Reichel di->bat_desc.get_property = ds2760_battery_get_property;
6928c0984e5SSebastian Reichel di->bat_desc.set_property = ds2760_battery_set_property;
6938c0984e5SSebastian Reichel di->bat_desc.property_is_writeable =
6948c0984e5SSebastian Reichel ds2760_battery_property_is_writeable;
6958c0984e5SSebastian Reichel di->bat_desc.set_charged = ds2760_battery_set_charged;
6968c0984e5SSebastian Reichel di->bat_desc.external_power_changed =
6978c0984e5SSebastian Reichel ds2760_battery_external_power_changed;
6988c0984e5SSebastian Reichel
6998c0984e5SSebastian Reichel psy_cfg.drv_data = di;
7008c0984e5SSebastian Reichel
701efdafd68SDaniel Mack if (dev->of_node) {
702efdafd68SDaniel Mack u32 tmp;
703efdafd68SDaniel Mack
704efdafd68SDaniel Mack psy_cfg.of_node = dev->of_node;
705efdafd68SDaniel Mack
706efdafd68SDaniel Mack if (!of_property_read_bool(dev->of_node, "maxim,pmod-enabled"))
707efdafd68SDaniel Mack pmod_enabled = true;
708efdafd68SDaniel Mack
709efdafd68SDaniel Mack if (!of_property_read_u32(dev->of_node,
710efdafd68SDaniel Mack "maxim,cache-time-ms", &tmp))
711efdafd68SDaniel Mack cache_time = tmp;
712efdafd68SDaniel Mack
713efdafd68SDaniel Mack if (!of_property_read_u32(dev->of_node,
714efdafd68SDaniel Mack "rated-capacity-microamp-hours",
715efdafd68SDaniel Mack &tmp))
716efdafd68SDaniel Mack rated_capacity = tmp / 10; /* property is in mAh */
717efdafd68SDaniel Mack }
718efdafd68SDaniel Mack
7198c0984e5SSebastian Reichel di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
7208c0984e5SSebastian Reichel
721bf497355SDaniel Mack sl->family_data = di;
722bf497355SDaniel Mack
7238c0984e5SSebastian Reichel /* enable sleep mode feature */
7248c0984e5SSebastian Reichel ds2760_battery_read_status(di);
7258c0984e5SSebastian Reichel status = di->raw[DS2760_STATUS_REG];
7268c0984e5SSebastian Reichel if (pmod_enabled)
7278c0984e5SSebastian Reichel status |= DS2760_STATUS_PMOD;
7288c0984e5SSebastian Reichel else
7298c0984e5SSebastian Reichel status &= ~DS2760_STATUS_PMOD;
7308c0984e5SSebastian Reichel
7318c0984e5SSebastian Reichel ds2760_battery_write_status(di, status);
7328c0984e5SSebastian Reichel
733efdafd68SDaniel Mack /* set rated capacity from module param or device tree */
7348c0984e5SSebastian Reichel if (rated_capacity)
7358c0984e5SSebastian Reichel ds2760_battery_write_rated_capacity(di, rated_capacity);
7368c0984e5SSebastian Reichel
7378c0984e5SSebastian Reichel /* set current accumulator if given as parameter.
7388c0984e5SSebastian Reichel * this should only be done for bootstrapping the value */
7398c0984e5SSebastian Reichel if (current_accum)
7408c0984e5SSebastian Reichel ds2760_battery_set_current_accum(di, current_accum);
7418c0984e5SSebastian Reichel
742bf497355SDaniel Mack di->bat = power_supply_register(dev, &di->bat_desc, &psy_cfg);
7438c0984e5SSebastian Reichel if (IS_ERR(di->bat)) {
7448c0984e5SSebastian Reichel dev_err(di->dev, "failed to register battery\n");
7458c0984e5SSebastian Reichel retval = PTR_ERR(di->bat);
7468c0984e5SSebastian Reichel goto batt_failed;
7478c0984e5SSebastian Reichel }
7488c0984e5SSebastian Reichel
7498c0984e5SSebastian Reichel INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
7508c0984e5SSebastian Reichel INIT_DELAYED_WORK(&di->set_charged_work,
7518c0984e5SSebastian Reichel ds2760_battery_set_charged_work);
752bf497355SDaniel Mack di->monitor_wqueue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
7538c0984e5SSebastian Reichel if (!di->monitor_wqueue) {
7548c0984e5SSebastian Reichel retval = -ESRCH;
7558c0984e5SSebastian Reichel goto workqueue_failed;
7568c0984e5SSebastian Reichel }
7578c0984e5SSebastian Reichel queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
7588c0984e5SSebastian Reichel
759bf497355SDaniel Mack di->pm_notifier.notifier_call = ds2760_pm_notifier;
760bf497355SDaniel Mack register_pm_notifier(&di->pm_notifier);
761bf497355SDaniel Mack
7628c0984e5SSebastian Reichel goto success;
7638c0984e5SSebastian Reichel
7648c0984e5SSebastian Reichel workqueue_failed:
7658c0984e5SSebastian Reichel power_supply_unregister(di->bat);
7668c0984e5SSebastian Reichel batt_failed:
7678c0984e5SSebastian Reichel di_alloc_failed:
7688c0984e5SSebastian Reichel success:
7698c0984e5SSebastian Reichel return retval;
7708c0984e5SSebastian Reichel }
7718c0984e5SSebastian Reichel
w1_ds2760_remove_slave(struct w1_slave * sl)772bf497355SDaniel Mack static void w1_ds2760_remove_slave(struct w1_slave *sl)
7738c0984e5SSebastian Reichel {
774bf497355SDaniel Mack struct ds2760_device_info *di = sl->family_data;
7758c0984e5SSebastian Reichel
776bf497355SDaniel Mack unregister_pm_notifier(&di->pm_notifier);
7778c0984e5SSebastian Reichel cancel_delayed_work_sync(&di->monitor_work);
7788c0984e5SSebastian Reichel cancel_delayed_work_sync(&di->set_charged_work);
7798c0984e5SSebastian Reichel destroy_workqueue(di->monitor_wqueue);
7808c0984e5SSebastian Reichel power_supply_unregister(di->bat);
7818c0984e5SSebastian Reichel }
7828c0984e5SSebastian Reichel
783efdafd68SDaniel Mack #ifdef CONFIG_OF
784efdafd68SDaniel Mack static const struct of_device_id w1_ds2760_of_ids[] = {
785efdafd68SDaniel Mack { .compatible = "maxim,ds2760" },
786efdafd68SDaniel Mack {}
787efdafd68SDaniel Mack };
788efdafd68SDaniel Mack #endif
789efdafd68SDaniel Mack
7906925478cSRikard Falkeborn static const struct w1_family_ops w1_ds2760_fops = {
791bf497355SDaniel Mack .add_slave = w1_ds2760_add_slave,
792bf497355SDaniel Mack .remove_slave = w1_ds2760_remove_slave,
793bf497355SDaniel Mack .groups = w1_ds2760_groups,
7948c0984e5SSebastian Reichel };
7958c0984e5SSebastian Reichel
796bf497355SDaniel Mack static struct w1_family w1_ds2760_family = {
797bf497355SDaniel Mack .fid = W1_FAMILY_DS2760,
798bf497355SDaniel Mack .fops = &w1_ds2760_fops,
799efdafd68SDaniel Mack .of_match_table = of_match_ptr(w1_ds2760_of_ids),
800bf497355SDaniel Mack };
801bf497355SDaniel Mack module_w1_family(w1_ds2760_family);
8028c0984e5SSebastian Reichel
8038c0984e5SSebastian Reichel MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
8048c0984e5SSebastian Reichel "Matt Reimer <mreimer@vpop.net>, "
8058c0984e5SSebastian Reichel "Anton Vorontsov <cbou@mail.ru>");
806bf497355SDaniel Mack MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
807bf497355SDaniel Mack MODULE_LICENSE("GPL");
808bf497355SDaniel Mack MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760));
809