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 
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 
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 
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 
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 
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 
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 
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 #ifdef CONFIG_MACH_H4700
2318c0984e5SSebastian Reichel 	1800,	/* HP iPAQ hx4700 3.7V 1800mAh (359113-001) */
2328c0984e5SSebastian Reichel #else
2338c0984e5SSebastian Reichel 	1440,	/* Lishen */
2348c0984e5SSebastian Reichel #endif
2358c0984e5SSebastian Reichel 	1440,	/* NEC */
2368c0984e5SSebastian Reichel 	2880,	/* Samsung */
2378c0984e5SSebastian Reichel 	2880,	/* BYD */
2388c0984e5SSebastian Reichel 	2880,	/* Lishen */
2398c0984e5SSebastian Reichel 	2880,	/* NEC */
2408c0984e5SSebastian Reichel #ifdef CONFIG_MACH_H4700
2418c0984e5SSebastian Reichel 	0,
2428c0984e5SSebastian Reichel 	3600,	/* HP iPAQ hx4700 3.7V 3600mAh (359114-001) */
2438c0984e5SSebastian Reichel #endif
2448c0984e5SSebastian Reichel };
2458c0984e5SSebastian Reichel 
2468c0984e5SSebastian Reichel /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
2478c0984e5SSebastian Reichel  * temp is in Celsius */
2488c0984e5SSebastian Reichel static int battery_interpolate(int array[], int temp)
2498c0984e5SSebastian Reichel {
2508c0984e5SSebastian Reichel 	int index, dt;
2518c0984e5SSebastian Reichel 
2528c0984e5SSebastian Reichel 	if (temp <= 0)
2538c0984e5SSebastian Reichel 		return array[0];
2548c0984e5SSebastian Reichel 	if (temp >= 40)
2558c0984e5SSebastian Reichel 		return array[4];
2568c0984e5SSebastian Reichel 
2578c0984e5SSebastian Reichel 	index = temp / 10;
2588c0984e5SSebastian Reichel 	dt    = temp % 10;
2598c0984e5SSebastian Reichel 
2608c0984e5SSebastian Reichel 	return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
2618c0984e5SSebastian Reichel }
2628c0984e5SSebastian Reichel 
2638c0984e5SSebastian Reichel static int ds2760_battery_read_status(struct ds2760_device_info *di)
2648c0984e5SSebastian Reichel {
2658c0984e5SSebastian Reichel 	int ret, i, start, count, scale[5];
2668c0984e5SSebastian Reichel 
2678c0984e5SSebastian Reichel 	if (di->update_time && time_before(jiffies, di->update_time +
2688c0984e5SSebastian Reichel 					   msecs_to_jiffies(cache_time)))
2698c0984e5SSebastian Reichel 		return 0;
2708c0984e5SSebastian Reichel 
2718c0984e5SSebastian Reichel 	/* The first time we read the entire contents of SRAM/EEPROM,
2728c0984e5SSebastian Reichel 	 * but after that we just read the interesting bits that change. */
2738c0984e5SSebastian Reichel 	if (di->update_time == 0) {
2748c0984e5SSebastian Reichel 		start = 0;
2758c0984e5SSebastian Reichel 		count = DS2760_DATA_SIZE;
2768c0984e5SSebastian Reichel 	} else {
2778c0984e5SSebastian Reichel 		start = DS2760_VOLTAGE_MSB;
2788c0984e5SSebastian Reichel 		count = DS2760_TEMP_LSB - start + 1;
2798c0984e5SSebastian Reichel 	}
2808c0984e5SSebastian Reichel 
281bf497355SDaniel Mack 	ret = w1_ds2760_read(di->dev, di->raw + start, start, count);
2828c0984e5SSebastian Reichel 	if (ret != count) {
2838c0984e5SSebastian Reichel 		dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
284bf497355SDaniel Mack 			 di->dev);
2858c0984e5SSebastian Reichel 		return 1;
2868c0984e5SSebastian Reichel 	}
2878c0984e5SSebastian Reichel 
2888c0984e5SSebastian Reichel 	di->update_time = jiffies;
2898c0984e5SSebastian Reichel 
2908c0984e5SSebastian Reichel 	/* DS2760 reports voltage in units of 4.88mV, but the battery class
2918c0984e5SSebastian Reichel 	 * reports in units of uV, so convert by multiplying by 4880. */
2928c0984e5SSebastian Reichel 	di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
2938c0984e5SSebastian Reichel 			  (di->raw[DS2760_VOLTAGE_LSB] >> 5);
2948c0984e5SSebastian Reichel 	di->voltage_uV = di->voltage_raw * 4880;
2958c0984e5SSebastian Reichel 
2968c0984e5SSebastian Reichel 	/* DS2760 reports current in signed units of 0.625mA, but the battery
2978c0984e5SSebastian Reichel 	 * class reports in units of µA, so convert by multiplying by 625. */
2988c0984e5SSebastian Reichel 	di->current_raw =
2998c0984e5SSebastian Reichel 	    (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
3008c0984e5SSebastian Reichel 			  (di->raw[DS2760_CURRENT_LSB] >> 3);
3018c0984e5SSebastian Reichel 	di->current_uA = di->current_raw * 625;
3028c0984e5SSebastian Reichel 
3038c0984e5SSebastian Reichel 	/* DS2760 reports accumulated current in signed units of 0.25mAh. */
3048c0984e5SSebastian Reichel 	di->accum_current_raw =
3058c0984e5SSebastian Reichel 	    (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
3068c0984e5SSebastian Reichel 			   di->raw[DS2760_CURRENT_ACCUM_LSB];
3078c0984e5SSebastian Reichel 	di->accum_current_uAh = di->accum_current_raw * 250;
3088c0984e5SSebastian Reichel 
3098c0984e5SSebastian Reichel 	/* DS2760 reports temperature in signed units of 0.125°C, but the
3108c0984e5SSebastian Reichel 	 * battery class reports in units of 1/10 °C, so we convert by
3118c0984e5SSebastian Reichel 	 * multiplying by .125 * 10 = 1.25. */
3128c0984e5SSebastian Reichel 	di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
3138c0984e5SSebastian Reichel 				     (di->raw[DS2760_TEMP_LSB] >> 5);
3148c0984e5SSebastian Reichel 	di->temp_C = di->temp_raw + (di->temp_raw / 4);
3158c0984e5SSebastian Reichel 
3168c0984e5SSebastian Reichel 	/* At least some battery monitors (e.g. HP iPAQ) store the battery's
3178c0984e5SSebastian Reichel 	 * maximum rated capacity. */
3188c0984e5SSebastian Reichel 	if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
3198c0984e5SSebastian Reichel 		di->rated_capacity = rated_capacities[
3208c0984e5SSebastian Reichel 			(unsigned int)di->raw[DS2760_RATED_CAPACITY]];
3218c0984e5SSebastian Reichel 	else
3228c0984e5SSebastian Reichel 		di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
3238c0984e5SSebastian Reichel 
3248c0984e5SSebastian Reichel 	di->rated_capacity *= 1000; /* convert to µAh */
3258c0984e5SSebastian Reichel 
3268c0984e5SSebastian Reichel 	/* Calculate the full level at the present temperature. */
3278c0984e5SSebastian Reichel 	di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
3288c0984e5SSebastian Reichel 			      di->raw[DS2760_ACTIVE_FULL + 1];
3298c0984e5SSebastian Reichel 
3308c0984e5SSebastian Reichel 	/* If the full_active_uAh value is not given, fall back to the rated
3318c0984e5SSebastian Reichel 	 * capacity. This is likely to happen when chips are not part of the
3328c0984e5SSebastian Reichel 	 * battery pack and is therefore not bootstrapped. */
3338c0984e5SSebastian Reichel 	if (di->full_active_uAh == 0)
3348c0984e5SSebastian Reichel 		di->full_active_uAh = di->rated_capacity / 1000L;
3358c0984e5SSebastian Reichel 
3368c0984e5SSebastian Reichel 	scale[0] = di->full_active_uAh;
3378c0984e5SSebastian Reichel 	for (i = 1; i < 5; i++)
3388c0984e5SSebastian Reichel 		scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i];
3398c0984e5SSebastian Reichel 
3408c0984e5SSebastian Reichel 	di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
3418c0984e5SSebastian Reichel 	di->full_active_uAh *= 1000; /* convert to µAh */
3428c0984e5SSebastian Reichel 
3438c0984e5SSebastian Reichel 	/* Calculate the empty level at the present temperature. */
3448c0984e5SSebastian Reichel 	scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
3458c0984e5SSebastian Reichel 	for (i = 3; i >= 0; i--)
3468c0984e5SSebastian Reichel 		scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
3478c0984e5SSebastian Reichel 
3488c0984e5SSebastian Reichel 	di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
3498c0984e5SSebastian Reichel 	di->empty_uAh *= 1000; /* convert to µAh */
3508c0984e5SSebastian Reichel 
3518c0984e5SSebastian Reichel 	if (di->full_active_uAh == di->empty_uAh)
3528c0984e5SSebastian Reichel 		di->rem_capacity = 0;
3538c0984e5SSebastian Reichel 	else
3548c0984e5SSebastian Reichel 		/* From Maxim Application Note 131: remaining capacity =
3558c0984e5SSebastian Reichel 		 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
3568c0984e5SSebastian Reichel 		di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
3578c0984e5SSebastian Reichel 				    (di->full_active_uAh - di->empty_uAh);
3588c0984e5SSebastian Reichel 
3598c0984e5SSebastian Reichel 	if (di->rem_capacity < 0)
3608c0984e5SSebastian Reichel 		di->rem_capacity = 0;
3618c0984e5SSebastian Reichel 	if (di->rem_capacity > 100)
3628c0984e5SSebastian Reichel 		di->rem_capacity = 100;
3638c0984e5SSebastian Reichel 
3648c0984e5SSebastian Reichel 	if (di->current_uA < -100L)
3658c0984e5SSebastian Reichel 		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
3668c0984e5SSebastian Reichel 					/ (di->current_uA / 100L);
3678c0984e5SSebastian Reichel 	else
3688c0984e5SSebastian Reichel 		di->life_sec = 0;
3698c0984e5SSebastian Reichel 
3708c0984e5SSebastian Reichel 	return 0;
3718c0984e5SSebastian Reichel }
3728c0984e5SSebastian Reichel 
3738c0984e5SSebastian Reichel static void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
3748c0984e5SSebastian Reichel 					     unsigned int acr_val)
3758c0984e5SSebastian Reichel {
3768c0984e5SSebastian Reichel 	unsigned char acr[2];
3778c0984e5SSebastian Reichel 
3788c0984e5SSebastian Reichel 	/* acr is in units of 0.25 mAh */
3798c0984e5SSebastian Reichel 	acr_val *= 4L;
3808c0984e5SSebastian Reichel 	acr_val /= 1000;
3818c0984e5SSebastian Reichel 
3828c0984e5SSebastian Reichel 	acr[0] = acr_val >> 8;
3838c0984e5SSebastian Reichel 	acr[1] = acr_val & 0xff;
3848c0984e5SSebastian Reichel 
385bf497355SDaniel Mack 	if (w1_ds2760_write(di->dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
3868c0984e5SSebastian Reichel 		dev_warn(di->dev, "ACR write failed\n");
3878c0984e5SSebastian Reichel }
3888c0984e5SSebastian Reichel 
3898c0984e5SSebastian Reichel static void ds2760_battery_update_status(struct ds2760_device_info *di)
3908c0984e5SSebastian Reichel {
3918c0984e5SSebastian Reichel 	int old_charge_status = di->charge_status;
3928c0984e5SSebastian Reichel 
3938c0984e5SSebastian Reichel 	ds2760_battery_read_status(di);
3948c0984e5SSebastian Reichel 
3958c0984e5SSebastian Reichel 	if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
3968c0984e5SSebastian Reichel 		di->full_counter = 0;
3978c0984e5SSebastian Reichel 
3988c0984e5SSebastian Reichel 	if (power_supply_am_i_supplied(di->bat)) {
3998c0984e5SSebastian Reichel 		if (di->current_uA > 10000) {
4008c0984e5SSebastian Reichel 			di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
4018c0984e5SSebastian Reichel 			di->full_counter = 0;
4028c0984e5SSebastian Reichel 		} else if (di->current_uA < -5000) {
4038c0984e5SSebastian Reichel 			if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
4048c0984e5SSebastian Reichel 				dev_notice(di->dev, "not enough power to "
4058c0984e5SSebastian Reichel 					   "charge\n");
4068c0984e5SSebastian Reichel 			di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
4078c0984e5SSebastian Reichel 			di->full_counter = 0;
4088c0984e5SSebastian Reichel 		} else if (di->current_uA < 10000 &&
4098c0984e5SSebastian Reichel 			    di->charge_status != POWER_SUPPLY_STATUS_FULL) {
4108c0984e5SSebastian Reichel 
4118c0984e5SSebastian Reichel 			/* Don't consider the battery to be full unless
4128c0984e5SSebastian Reichel 			 * we've seen the current < 10 mA at least two
4138c0984e5SSebastian Reichel 			 * consecutive times. */
4148c0984e5SSebastian Reichel 
4158c0984e5SSebastian Reichel 			di->full_counter++;
4168c0984e5SSebastian Reichel 
4178c0984e5SSebastian Reichel 			if (di->full_counter < 2) {
4188c0984e5SSebastian Reichel 				di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
4198c0984e5SSebastian Reichel 			} else {
4208c0984e5SSebastian Reichel 				di->charge_status = POWER_SUPPLY_STATUS_FULL;
4218c0984e5SSebastian Reichel 				ds2760_battery_set_current_accum(di,
4228c0984e5SSebastian Reichel 						di->full_active_uAh);
4238c0984e5SSebastian Reichel 			}
4248c0984e5SSebastian Reichel 		}
4258c0984e5SSebastian Reichel 	} else {
4268c0984e5SSebastian Reichel 		di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
4278c0984e5SSebastian Reichel 		di->full_counter = 0;
4288c0984e5SSebastian Reichel 	}
4298c0984e5SSebastian Reichel 
4308c0984e5SSebastian Reichel 	if (di->charge_status != old_charge_status)
4318c0984e5SSebastian Reichel 		power_supply_changed(di->bat);
4328c0984e5SSebastian Reichel }
4338c0984e5SSebastian Reichel 
4348c0984e5SSebastian Reichel static void ds2760_battery_write_status(struct ds2760_device_info *di,
4358c0984e5SSebastian Reichel 					char status)
4368c0984e5SSebastian Reichel {
4378c0984e5SSebastian Reichel 	if (status == di->raw[DS2760_STATUS_REG])
4388c0984e5SSebastian Reichel 		return;
4398c0984e5SSebastian Reichel 
440bf497355SDaniel Mack 	w1_ds2760_write(di->dev, &status, DS2760_STATUS_WRITE_REG, 1);
441bf497355SDaniel Mack 	w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
442bf497355SDaniel Mack 	w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
4438c0984e5SSebastian Reichel }
4448c0984e5SSebastian Reichel 
4458c0984e5SSebastian Reichel static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
4468c0984e5SSebastian Reichel 						unsigned char rated_capacity)
4478c0984e5SSebastian Reichel {
4488c0984e5SSebastian Reichel 	if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
4498c0984e5SSebastian Reichel 		return;
4508c0984e5SSebastian Reichel 
451bf497355SDaniel Mack 	w1_ds2760_write(di->dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
452bf497355SDaniel Mack 	w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
453bf497355SDaniel Mack 	w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
4548c0984e5SSebastian Reichel }
4558c0984e5SSebastian Reichel 
4568c0984e5SSebastian Reichel static void ds2760_battery_write_active_full(struct ds2760_device_info *di,
4578c0984e5SSebastian Reichel 					     int active_full)
4588c0984e5SSebastian Reichel {
4598c0984e5SSebastian Reichel 	unsigned char tmp[2] = {
4608c0984e5SSebastian Reichel 		active_full >> 8,
4618c0984e5SSebastian Reichel 		active_full & 0xff
4628c0984e5SSebastian Reichel 	};
4638c0984e5SSebastian Reichel 
4648c0984e5SSebastian Reichel 	if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
4658c0984e5SSebastian Reichel 	    tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
4668c0984e5SSebastian Reichel 		return;
4678c0984e5SSebastian Reichel 
468bf497355SDaniel Mack 	w1_ds2760_write(di->dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp));
469bf497355SDaniel Mack 	w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
470bf497355SDaniel Mack 	w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
4718c0984e5SSebastian Reichel 
4728c0984e5SSebastian Reichel 	/* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
4738c0984e5SSebastian Reichel 	 * values won't be read back by ds2760_battery_read_status() */
4748c0984e5SSebastian Reichel 	di->raw[DS2760_ACTIVE_FULL] = tmp[0];
4758c0984e5SSebastian Reichel 	di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1];
4768c0984e5SSebastian Reichel }
4778c0984e5SSebastian Reichel 
4788c0984e5SSebastian Reichel static void ds2760_battery_work(struct work_struct *work)
4798c0984e5SSebastian Reichel {
4808c0984e5SSebastian Reichel 	struct ds2760_device_info *di = container_of(work,
4818c0984e5SSebastian Reichel 		struct ds2760_device_info, monitor_work.work);
4828c0984e5SSebastian Reichel 	const int interval = HZ * 60;
4838c0984e5SSebastian Reichel 
4848c0984e5SSebastian Reichel 	dev_dbg(di->dev, "%s\n", __func__);
4858c0984e5SSebastian Reichel 
4868c0984e5SSebastian Reichel 	ds2760_battery_update_status(di);
4878c0984e5SSebastian Reichel 	queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
4888c0984e5SSebastian Reichel }
4898c0984e5SSebastian Reichel 
4908c0984e5SSebastian Reichel static void ds2760_battery_external_power_changed(struct power_supply *psy)
4918c0984e5SSebastian Reichel {
4928c0984e5SSebastian Reichel 	struct ds2760_device_info *di = power_supply_get_drvdata(psy);
4938c0984e5SSebastian Reichel 
4948c0984e5SSebastian Reichel 	dev_dbg(di->dev, "%s\n", __func__);
4958c0984e5SSebastian Reichel 
4968c0984e5SSebastian Reichel 	mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
4978c0984e5SSebastian Reichel }
4988c0984e5SSebastian Reichel 
4998c0984e5SSebastian Reichel 
5008c0984e5SSebastian Reichel static void ds2760_battery_set_charged_work(struct work_struct *work)
5018c0984e5SSebastian Reichel {
5028c0984e5SSebastian Reichel 	char bias;
5038c0984e5SSebastian Reichel 	struct ds2760_device_info *di = container_of(work,
5048c0984e5SSebastian Reichel 		struct ds2760_device_info, set_charged_work.work);
5058c0984e5SSebastian Reichel 
5068c0984e5SSebastian Reichel 	dev_dbg(di->dev, "%s\n", __func__);
5078c0984e5SSebastian Reichel 
5088c0984e5SSebastian Reichel 	ds2760_battery_read_status(di);
5098c0984e5SSebastian Reichel 
5108c0984e5SSebastian Reichel 	/* When we get notified by external circuitry that the battery is
5118c0984e5SSebastian Reichel 	 * considered fully charged now, we know that there is no current
5128c0984e5SSebastian Reichel 	 * flow any more. However, the ds2760's internal current meter is
5138c0984e5SSebastian Reichel 	 * too inaccurate to rely on - spec say something ~15% failure.
5148c0984e5SSebastian Reichel 	 * Hence, we use the current offset bias register to compensate
5158c0984e5SSebastian Reichel 	 * that error.
5168c0984e5SSebastian Reichel 	 */
5178c0984e5SSebastian Reichel 
5188c0984e5SSebastian Reichel 	if (!power_supply_am_i_supplied(di->bat))
5198c0984e5SSebastian Reichel 		return;
5208c0984e5SSebastian Reichel 
5218c0984e5SSebastian Reichel 	bias = (signed char) di->current_raw +
5228c0984e5SSebastian Reichel 		(signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
5238c0984e5SSebastian Reichel 
5248c0984e5SSebastian Reichel 	dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
5258c0984e5SSebastian Reichel 
526bf497355SDaniel Mack 	w1_ds2760_write(di->dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1);
527bf497355SDaniel Mack 	w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
528bf497355SDaniel Mack 	w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
5298c0984e5SSebastian Reichel 
5308c0984e5SSebastian Reichel 	/* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
5318c0984e5SSebastian Reichel 	 * value won't be read back by ds2760_battery_read_status() */
5328c0984e5SSebastian Reichel 	di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias;
5338c0984e5SSebastian Reichel }
5348c0984e5SSebastian Reichel 
5358c0984e5SSebastian Reichel static void ds2760_battery_set_charged(struct power_supply *psy)
5368c0984e5SSebastian Reichel {
5378c0984e5SSebastian Reichel 	struct ds2760_device_info *di = power_supply_get_drvdata(psy);
5388c0984e5SSebastian Reichel 
5398c0984e5SSebastian Reichel 	/* postpone the actual work by 20 secs. This is for debouncing GPIO
5408c0984e5SSebastian Reichel 	 * signals and to let the current value settle. See AN4188. */
5418c0984e5SSebastian Reichel 	mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20);
5428c0984e5SSebastian Reichel }
5438c0984e5SSebastian Reichel 
5448c0984e5SSebastian Reichel static int ds2760_battery_get_property(struct power_supply *psy,
5458c0984e5SSebastian Reichel 				       enum power_supply_property psp,
5468c0984e5SSebastian Reichel 				       union power_supply_propval *val)
5478c0984e5SSebastian Reichel {
5488c0984e5SSebastian Reichel 	struct ds2760_device_info *di = power_supply_get_drvdata(psy);
5498c0984e5SSebastian Reichel 
5508c0984e5SSebastian Reichel 	switch (psp) {
5518c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
5528c0984e5SSebastian Reichel 		val->intval = di->charge_status;
5538c0984e5SSebastian Reichel 		return 0;
5548c0984e5SSebastian Reichel 	default:
5558c0984e5SSebastian Reichel 		break;
5568c0984e5SSebastian Reichel 	}
5578c0984e5SSebastian Reichel 
5588c0984e5SSebastian Reichel 	ds2760_battery_read_status(di);
5598c0984e5SSebastian Reichel 
5608c0984e5SSebastian Reichel 	switch (psp) {
5618c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
5628c0984e5SSebastian Reichel 		val->intval = di->voltage_uV;
5638c0984e5SSebastian Reichel 		break;
5648c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
5658c0984e5SSebastian Reichel 		val->intval = di->current_uA;
5668c0984e5SSebastian Reichel 		break;
5678c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
5688c0984e5SSebastian Reichel 		val->intval = di->rated_capacity;
5698c0984e5SSebastian Reichel 		break;
5708c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL:
5718c0984e5SSebastian Reichel 		val->intval = di->full_active_uAh;
5728c0984e5SSebastian Reichel 		break;
5738c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_EMPTY:
5748c0984e5SSebastian Reichel 		val->intval = di->empty_uAh;
5758c0984e5SSebastian Reichel 		break;
5768c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
5778c0984e5SSebastian Reichel 		val->intval = di->accum_current_uAh;
5788c0984e5SSebastian Reichel 		break;
5798c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
5808c0984e5SSebastian Reichel 		val->intval = di->temp_C;
5818c0984e5SSebastian Reichel 		break;
5828c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
5838c0984e5SSebastian Reichel 		val->intval = di->life_sec;
5848c0984e5SSebastian Reichel 		break;
5858c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
5868c0984e5SSebastian Reichel 		val->intval = di->rem_capacity;
5878c0984e5SSebastian Reichel 		break;
5888c0984e5SSebastian Reichel 	default:
5898c0984e5SSebastian Reichel 		return -EINVAL;
5908c0984e5SSebastian Reichel 	}
5918c0984e5SSebastian Reichel 
5928c0984e5SSebastian Reichel 	return 0;
5938c0984e5SSebastian Reichel }
5948c0984e5SSebastian Reichel 
5958c0984e5SSebastian Reichel static int ds2760_battery_set_property(struct power_supply *psy,
5968c0984e5SSebastian Reichel 				       enum power_supply_property psp,
5978c0984e5SSebastian Reichel 				       const union power_supply_propval *val)
5988c0984e5SSebastian Reichel {
5998c0984e5SSebastian Reichel 	struct ds2760_device_info *di = power_supply_get_drvdata(psy);
6008c0984e5SSebastian Reichel 
6018c0984e5SSebastian Reichel 	switch (psp) {
6028c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL:
6038c0984e5SSebastian Reichel 		/* the interface counts in uAh, convert the value */
6048c0984e5SSebastian Reichel 		ds2760_battery_write_active_full(di, val->intval / 1000L);
6058c0984e5SSebastian Reichel 		break;
6068c0984e5SSebastian Reichel 
6078c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
6088c0984e5SSebastian Reichel 		/* ds2760_battery_set_current_accum() does the conversion */
6098c0984e5SSebastian Reichel 		ds2760_battery_set_current_accum(di, val->intval);
6108c0984e5SSebastian Reichel 		break;
6118c0984e5SSebastian Reichel 
6128c0984e5SSebastian Reichel 	default:
6138c0984e5SSebastian Reichel 		return -EPERM;
6148c0984e5SSebastian Reichel 	}
6158c0984e5SSebastian Reichel 
6168c0984e5SSebastian Reichel 	return 0;
6178c0984e5SSebastian Reichel }
6188c0984e5SSebastian Reichel 
6198c0984e5SSebastian Reichel static int ds2760_battery_property_is_writeable(struct power_supply *psy,
6208c0984e5SSebastian Reichel 						enum power_supply_property psp)
6218c0984e5SSebastian Reichel {
6228c0984e5SSebastian Reichel 	switch (psp) {
6238c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL:
6248c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
6258c0984e5SSebastian Reichel 		return 1;
6268c0984e5SSebastian Reichel 
6278c0984e5SSebastian Reichel 	default:
6288c0984e5SSebastian Reichel 		break;
6298c0984e5SSebastian Reichel 	}
6308c0984e5SSebastian Reichel 
6318c0984e5SSebastian Reichel 	return 0;
6328c0984e5SSebastian Reichel }
6338c0984e5SSebastian Reichel 
6348c0984e5SSebastian Reichel static enum power_supply_property ds2760_battery_props[] = {
6358c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6368c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
6378c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
6388c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
6398c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
6408c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_EMPTY,
6418c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
6428c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
6438c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
6448c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
6458c0984e5SSebastian Reichel };
6468c0984e5SSebastian Reichel 
647bf497355SDaniel Mack static int ds2760_pm_notifier(struct notifier_block *notifier,
648bf497355SDaniel Mack 			      unsigned long pm_event,
649bf497355SDaniel Mack 			      void *unused)
650bf497355SDaniel Mack {
651bf497355SDaniel Mack 	struct ds2760_device_info *di =
652bf497355SDaniel Mack 		container_of(notifier, struct ds2760_device_info, pm_notifier);
653bf497355SDaniel Mack 
654bf497355SDaniel Mack 	switch (pm_event) {
655bf497355SDaniel Mack 	case PM_HIBERNATION_PREPARE:
656bf497355SDaniel Mack 	case PM_SUSPEND_PREPARE:
657bf497355SDaniel Mack 		di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
658bf497355SDaniel Mack 		break;
659bf497355SDaniel Mack 
660bf497355SDaniel Mack 	case PM_POST_RESTORE:
661bf497355SDaniel Mack 	case PM_POST_HIBERNATION:
662bf497355SDaniel Mack 	case PM_POST_SUSPEND:
663bf497355SDaniel Mack 		di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
664bf497355SDaniel Mack 		power_supply_changed(di->bat);
665bf497355SDaniel Mack 		mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
666bf497355SDaniel Mack 
667bf497355SDaniel Mack 		break;
668bf497355SDaniel Mack 
669bf497355SDaniel Mack 	case PM_RESTORE_PREPARE:
670bf497355SDaniel Mack 	default:
671bf497355SDaniel Mack 		break;
672bf497355SDaniel Mack 	}
673bf497355SDaniel Mack 
674bf497355SDaniel Mack 	return NOTIFY_DONE;
675bf497355SDaniel Mack }
676bf497355SDaniel Mack 
677bf497355SDaniel Mack static int w1_ds2760_add_slave(struct w1_slave *sl)
6788c0984e5SSebastian Reichel {
6798c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
6808c0984e5SSebastian Reichel 	struct ds2760_device_info *di;
681bf497355SDaniel Mack 	struct device *dev = &sl->dev;
682bf497355SDaniel Mack 	int retval = 0;
683bf497355SDaniel Mack 	char name[32];
684bf497355SDaniel Mack 	char status;
6858c0984e5SSebastian Reichel 
686bf497355SDaniel Mack 	di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
6878c0984e5SSebastian Reichel 	if (!di) {
6888c0984e5SSebastian Reichel 		retval = -ENOMEM;
6898c0984e5SSebastian Reichel 		goto di_alloc_failed;
6908c0984e5SSebastian Reichel 	}
6918c0984e5SSebastian Reichel 
692bf497355SDaniel Mack 	snprintf(name, sizeof(name), "ds2760-battery.%d", dev->id);
6938c0984e5SSebastian Reichel 
694bf497355SDaniel Mack 	di->dev				= dev;
695bf497355SDaniel Mack 	di->bat_desc.name		= name;
6968c0984e5SSebastian Reichel 	di->bat_desc.type		= POWER_SUPPLY_TYPE_BATTERY;
6978c0984e5SSebastian Reichel 	di->bat_desc.properties		= ds2760_battery_props;
6988c0984e5SSebastian Reichel 	di->bat_desc.num_properties	= ARRAY_SIZE(ds2760_battery_props);
6998c0984e5SSebastian Reichel 	di->bat_desc.get_property	= ds2760_battery_get_property;
7008c0984e5SSebastian Reichel 	di->bat_desc.set_property	= ds2760_battery_set_property;
7018c0984e5SSebastian Reichel 	di->bat_desc.property_is_writeable =
7028c0984e5SSebastian Reichel 				  ds2760_battery_property_is_writeable;
7038c0984e5SSebastian Reichel 	di->bat_desc.set_charged	= ds2760_battery_set_charged;
7048c0984e5SSebastian Reichel 	di->bat_desc.external_power_changed =
7058c0984e5SSebastian Reichel 				  ds2760_battery_external_power_changed;
7068c0984e5SSebastian Reichel 
7078c0984e5SSebastian Reichel 	psy_cfg.drv_data = di;
7088c0984e5SSebastian Reichel 
709efdafd68SDaniel Mack 	if (dev->of_node) {
710efdafd68SDaniel Mack 		u32 tmp;
711efdafd68SDaniel Mack 
712efdafd68SDaniel Mack 		psy_cfg.of_node = dev->of_node;
713efdafd68SDaniel Mack 
714efdafd68SDaniel Mack 		if (!of_property_read_bool(dev->of_node, "maxim,pmod-enabled"))
715efdafd68SDaniel Mack 			pmod_enabled = true;
716efdafd68SDaniel Mack 
717efdafd68SDaniel Mack 		if (!of_property_read_u32(dev->of_node,
718efdafd68SDaniel Mack 					  "maxim,cache-time-ms", &tmp))
719efdafd68SDaniel Mack 			cache_time = tmp;
720efdafd68SDaniel Mack 
721efdafd68SDaniel Mack 		if (!of_property_read_u32(dev->of_node,
722efdafd68SDaniel Mack 					  "rated-capacity-microamp-hours",
723efdafd68SDaniel Mack 					  &tmp))
724efdafd68SDaniel Mack 			rated_capacity = tmp / 10; /* property is in mAh */
725efdafd68SDaniel Mack 	}
726efdafd68SDaniel Mack 
7278c0984e5SSebastian Reichel 	di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
7288c0984e5SSebastian Reichel 
729bf497355SDaniel Mack 	sl->family_data = di;
730bf497355SDaniel Mack 
7318c0984e5SSebastian Reichel 	/* enable sleep mode feature */
7328c0984e5SSebastian Reichel 	ds2760_battery_read_status(di);
7338c0984e5SSebastian Reichel 	status = di->raw[DS2760_STATUS_REG];
7348c0984e5SSebastian Reichel 	if (pmod_enabled)
7358c0984e5SSebastian Reichel 		status |= DS2760_STATUS_PMOD;
7368c0984e5SSebastian Reichel 	else
7378c0984e5SSebastian Reichel 		status &= ~DS2760_STATUS_PMOD;
7388c0984e5SSebastian Reichel 
7398c0984e5SSebastian Reichel 	ds2760_battery_write_status(di, status);
7408c0984e5SSebastian Reichel 
741efdafd68SDaniel Mack 	/* set rated capacity from module param or device tree */
7428c0984e5SSebastian Reichel 	if (rated_capacity)
7438c0984e5SSebastian Reichel 		ds2760_battery_write_rated_capacity(di, rated_capacity);
7448c0984e5SSebastian Reichel 
7458c0984e5SSebastian Reichel 	/* set current accumulator if given as parameter.
7468c0984e5SSebastian Reichel 	 * this should only be done for bootstrapping the value */
7478c0984e5SSebastian Reichel 	if (current_accum)
7488c0984e5SSebastian Reichel 		ds2760_battery_set_current_accum(di, current_accum);
7498c0984e5SSebastian Reichel 
750bf497355SDaniel Mack 	di->bat = power_supply_register(dev, &di->bat_desc, &psy_cfg);
7518c0984e5SSebastian Reichel 	if (IS_ERR(di->bat)) {
7528c0984e5SSebastian Reichel 		dev_err(di->dev, "failed to register battery\n");
7538c0984e5SSebastian Reichel 		retval = PTR_ERR(di->bat);
7548c0984e5SSebastian Reichel 		goto batt_failed;
7558c0984e5SSebastian Reichel 	}
7568c0984e5SSebastian Reichel 
7578c0984e5SSebastian Reichel 	INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
7588c0984e5SSebastian Reichel 	INIT_DELAYED_WORK(&di->set_charged_work,
7598c0984e5SSebastian Reichel 			  ds2760_battery_set_charged_work);
760bf497355SDaniel Mack 	di->monitor_wqueue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
7618c0984e5SSebastian Reichel 	if (!di->monitor_wqueue) {
7628c0984e5SSebastian Reichel 		retval = -ESRCH;
7638c0984e5SSebastian Reichel 		goto workqueue_failed;
7648c0984e5SSebastian Reichel 	}
7658c0984e5SSebastian Reichel 	queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
7668c0984e5SSebastian Reichel 
767bf497355SDaniel Mack 	di->pm_notifier.notifier_call = ds2760_pm_notifier;
768bf497355SDaniel Mack 	register_pm_notifier(&di->pm_notifier);
769bf497355SDaniel Mack 
7708c0984e5SSebastian Reichel 	goto success;
7718c0984e5SSebastian Reichel 
7728c0984e5SSebastian Reichel workqueue_failed:
7738c0984e5SSebastian Reichel 	power_supply_unregister(di->bat);
7748c0984e5SSebastian Reichel batt_failed:
7758c0984e5SSebastian Reichel di_alloc_failed:
7768c0984e5SSebastian Reichel success:
7778c0984e5SSebastian Reichel 	return retval;
7788c0984e5SSebastian Reichel }
7798c0984e5SSebastian Reichel 
780bf497355SDaniel Mack static void w1_ds2760_remove_slave(struct w1_slave *sl)
7818c0984e5SSebastian Reichel {
782bf497355SDaniel Mack 	struct ds2760_device_info *di = sl->family_data;
7838c0984e5SSebastian Reichel 
784bf497355SDaniel Mack 	unregister_pm_notifier(&di->pm_notifier);
7858c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->monitor_work);
7868c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->set_charged_work);
7878c0984e5SSebastian Reichel 	destroy_workqueue(di->monitor_wqueue);
7888c0984e5SSebastian Reichel 	power_supply_unregister(di->bat);
7898c0984e5SSebastian Reichel }
7908c0984e5SSebastian Reichel 
791efdafd68SDaniel Mack #ifdef CONFIG_OF
792efdafd68SDaniel Mack static const struct of_device_id w1_ds2760_of_ids[] = {
793efdafd68SDaniel Mack 	{ .compatible = "maxim,ds2760" },
794efdafd68SDaniel Mack 	{}
795efdafd68SDaniel Mack };
796efdafd68SDaniel Mack #endif
797efdafd68SDaniel Mack 
7986925478cSRikard Falkeborn static const struct w1_family_ops w1_ds2760_fops = {
799bf497355SDaniel Mack 	.add_slave	= w1_ds2760_add_slave,
800bf497355SDaniel Mack 	.remove_slave	= w1_ds2760_remove_slave,
801bf497355SDaniel Mack 	.groups		= w1_ds2760_groups,
8028c0984e5SSebastian Reichel };
8038c0984e5SSebastian Reichel 
804bf497355SDaniel Mack static struct w1_family w1_ds2760_family = {
805bf497355SDaniel Mack 	.fid		= W1_FAMILY_DS2760,
806bf497355SDaniel Mack 	.fops		= &w1_ds2760_fops,
807efdafd68SDaniel Mack 	.of_match_table	= of_match_ptr(w1_ds2760_of_ids),
808bf497355SDaniel Mack };
809bf497355SDaniel Mack module_w1_family(w1_ds2760_family);
8108c0984e5SSebastian Reichel 
8118c0984e5SSebastian Reichel MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
8128c0984e5SSebastian Reichel 	      "Matt Reimer <mreimer@vpop.net>, "
8138c0984e5SSebastian Reichel 	      "Anton Vorontsov <cbou@mail.ru>");
814bf497355SDaniel Mack MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
815bf497355SDaniel Mack MODULE_LICENSE("GPL");
816bf497355SDaniel Mack MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760));
817