1fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov/*
2fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov * Driver for batteries with DS2760 chips inside.
3fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *
4fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov * Copyright © 2007 Anton Vorontsov
5fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *	       2004-2007 Matt Reimer
6fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *	       2004 Szabolcs Gyurko
7fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *
8fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov * Use consistent with the GNU GPL is permitted,
9fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov * provided that this copyright notice is
10fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov * preserved in its entirety in all copies and derived works.
11fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *
12fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov * Author:  Anton Vorontsov <cbou@mail.ru>
13fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *	    February 2007
14fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *
15fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *	    Matt Reimer <mreimer@vpop.net>
16fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *	    April 2004, 2005, 2007
17fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *
18fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *	    Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
19fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov *	    September 2004
20fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov */
21fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
22fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#include <linux/module.h>
23fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#include <linux/param.h>
24fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#include <linux/jiffies.h>
25fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#include <linux/workqueue.h>
26fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#include <linux/pm.h>
275a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
28fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#include <linux/platform_device.h>
29fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#include <linux/power_supply.h>
30fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
31fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#include "../w1/w1.h"
32fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#include "../w1/slaves/w1_ds2760.h"
33fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
34fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstruct ds2760_device_info {
35fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct device *dev;
36fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
37fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	/* DS2760 data, valid after calling ds2760_battery_read_status() */
38fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	unsigned long update_time;	/* jiffies when data read */
39fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	char raw[DS2760_DATA_SIZE];	/* raw DS2760 data */
40fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int voltage_raw;		/* units of 4.88 mV */
41fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int voltage_uV;			/* units of µV */
42fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int current_raw;		/* units of 0.625 mA */
43fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int current_uA;			/* units of µA */
44fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int accum_current_raw;		/* units of 0.25 mAh */
45fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int accum_current_uAh;		/* units of µAh */
46fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int temp_raw;			/* units of 0.125 °C */
47fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int temp_C;			/* units of 0.1 °C */
48fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int rated_capacity;		/* units of µAh */
49fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int rem_capacity;		/* percentage */
50fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int full_active_uAh;		/* units of µAh */
51fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int empty_uAh;			/* units of µAh */
52fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int life_sec;			/* units of seconds */
53fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int charge_status;		/* POWER_SUPPLY_STATUS_* */
54fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
55fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int full_counter;
56fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct power_supply bat;
57fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct device *w1_dev;
58fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct workqueue_struct *monitor_wqueue;
59fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct delayed_work monitor_work;
608d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	struct delayed_work set_charged_work;
61fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov};
62fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
63fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic unsigned int cache_time = 1000;
64fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovmodule_param(cache_time, uint, 0644);
65fe0e3153acfef4864b69932cf116eb5f38f7500cAnton VorontsovMODULE_PARM_DESC(cache_time, "cache time in milliseconds");
66fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
6790ab5ee94171b3e28de6bb42ee30b527014e0be7Rusty Russellstatic bool pmod_enabled;
68cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mackmodule_param(pmod_enabled, bool, 0644);
69cef437e3a9b6d229d4ed3730cde047007267df6dDaniel MackMODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
70cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack
71c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mackstatic unsigned int rated_capacity;
72c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mackmodule_param(rated_capacity, uint, 0644);
73c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel MackMODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
74c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack
7502d0d2758821c38b2601d34dac544140af09e651Daniel Mackstatic unsigned int current_accum;
7602d0d2758821c38b2601d34dac544140af09e651Daniel Mackmodule_param(current_accum, uint, 0644);
7702d0d2758821c38b2601d34dac544140af09e651Daniel MackMODULE_PARM_DESC(current_accum, "current accumulator value");
7802d0d2758821c38b2601d34dac544140af09e651Daniel Mack
79fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov/* Some batteries have their rated capacity stored a N * 10 mAh, while
80fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov * others use an index into this table. */
81fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic int rated_capacities[] = {
82fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	0,
83fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	920,	/* Samsung */
84fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	920,	/* BYD */
85fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	920,	/* Lishen */
86fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	920,	/* NEC */
87fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	1440,	/* Samsung */
88fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	1440,	/* BYD */
899c6f8740d0c8d2b4de78511257f38384f4cbb292Paul Parsons#ifdef CONFIG_MACH_H4700
909c6f8740d0c8d2b4de78511257f38384f4cbb292Paul Parsons	1800,	/* HP iPAQ hx4700 3.7V 1800mAh (359113-001) */
919c6f8740d0c8d2b4de78511257f38384f4cbb292Paul Parsons#else
92fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	1440,	/* Lishen */
939c6f8740d0c8d2b4de78511257f38384f4cbb292Paul Parsons#endif
94fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	1440,	/* NEC */
95fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	2880,	/* Samsung */
96fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	2880,	/* BYD */
97fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	2880,	/* Lishen */
9886e6c6bd9b70260fc5bc9fdfa2e5b4dfd8fe545cPaul Parsons	2880,	/* NEC */
9986e6c6bd9b70260fc5bc9fdfa2e5b4dfd8fe545cPaul Parsons#ifdef CONFIG_MACH_H4700
10086e6c6bd9b70260fc5bc9fdfa2e5b4dfd8fe545cPaul Parsons	0,
10186e6c6bd9b70260fc5bc9fdfa2e5b4dfd8fe545cPaul Parsons	3600,	/* HP iPAQ hx4700 3.7V 3600mAh (359114-001) */
10286e6c6bd9b70260fc5bc9fdfa2e5b4dfd8fe545cPaul Parsons#endif
103fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov};
104fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
105fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov/* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
106fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov * temp is in Celsius */
107fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic int battery_interpolate(int array[], int temp)
108fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov{
109fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int index, dt;
110fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
111fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (temp <= 0)
112fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		return array[0];
113fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (temp >= 40)
114fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		return array[4];
115fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
116fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	index = temp / 10;
117fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	dt    = temp % 10;
118fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
119fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
120fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov}
121fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
122fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic int ds2760_battery_read_status(struct ds2760_device_info *di)
123fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov{
124fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int ret, i, start, count, scale[5];
125fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
126fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (di->update_time && time_before(jiffies, di->update_time +
127fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov					   msecs_to_jiffies(cache_time)))
128fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		return 0;
129fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
130fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	/* The first time we read the entire contents of SRAM/EEPROM,
131fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	 * but after that we just read the interesting bits that change. */
132fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (di->update_time == 0) {
133fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		start = 0;
134fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		count = DS2760_DATA_SIZE;
135fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	} else {
136fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		start = DS2760_VOLTAGE_MSB;
137fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		count = DS2760_TEMP_LSB - start + 1;
138fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	}
139fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
140fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
141fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (ret != count) {
142fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
143fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			 di->w1_dev);
144fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		return 1;
145fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	}
146fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
147fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->update_time = jiffies;
148fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
149fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	/* DS2760 reports voltage in units of 4.88mV, but the battery class
150fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	 * reports in units of uV, so convert by multiplying by 4880. */
151fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
152fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			  (di->raw[DS2760_VOLTAGE_LSB] >> 5);
153fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->voltage_uV = di->voltage_raw * 4880;
154fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
155fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	/* DS2760 reports current in signed units of 0.625mA, but the battery
156fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	 * class reports in units of µA, so convert by multiplying by 625. */
157fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->current_raw =
158fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	    (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
159fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			  (di->raw[DS2760_CURRENT_LSB] >> 3);
160fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->current_uA = di->current_raw * 625;
161fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
162fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	/* DS2760 reports accumulated current in signed units of 0.25mAh. */
163fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->accum_current_raw =
164fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	    (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
165fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			   di->raw[DS2760_CURRENT_ACCUM_LSB];
166fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->accum_current_uAh = di->accum_current_raw * 250;
167fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
168fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	/* DS2760 reports temperature in signed units of 0.125°C, but the
169fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	 * battery class reports in units of 1/10 °C, so we convert by
170fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	 * multiplying by .125 * 10 = 1.25. */
171fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
172fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov				     (di->raw[DS2760_TEMP_LSB] >> 5);
173fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->temp_C = di->temp_raw + (di->temp_raw / 4);
174fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
175fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	/* At least some battery monitors (e.g. HP iPAQ) store the battery's
176fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	 * maximum rated capacity. */
177fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
178fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		di->rated_capacity = rated_capacities[
179fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			(unsigned int)di->raw[DS2760_RATED_CAPACITY]];
180fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	else
181fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
182fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
183fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->rated_capacity *= 1000; /* convert to µAh */
184fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
185fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	/* Calculate the full level at the present temperature. */
186fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
187fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			      di->raw[DS2760_ACTIVE_FULL + 1];
188fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
18925f2bfa62ae77820a8185734c4a2ab7f3971a2fcDaniel Mack	/* If the full_active_uAh value is not given, fall back to the rated
19025f2bfa62ae77820a8185734c4a2ab7f3971a2fcDaniel Mack	 * capacity. This is likely to happen when chips are not part of the
19125f2bfa62ae77820a8185734c4a2ab7f3971a2fcDaniel Mack	 * battery pack and is therefore not bootstrapped. */
19225f2bfa62ae77820a8185734c4a2ab7f3971a2fcDaniel Mack	if (di->full_active_uAh == 0)
19325f2bfa62ae77820a8185734c4a2ab7f3971a2fcDaniel Mack		di->full_active_uAh = di->rated_capacity / 1000L;
19425f2bfa62ae77820a8185734c4a2ab7f3971a2fcDaniel Mack
19525f2bfa62ae77820a8185734c4a2ab7f3971a2fcDaniel Mack	scale[0] = di->full_active_uAh;
196fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	for (i = 1; i < 5; i++)
1973af98a8f39dee8ffb93a93e4145276d58e66e48fPaul Parsons		scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i];
198fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
199fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
200fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->full_active_uAh *= 1000; /* convert to µAh */
201fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
202fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	/* Calculate the empty level at the present temperature. */
203fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
204fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	for (i = 3; i >= 0; i--)
205fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
206fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
207fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
208fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->empty_uAh *= 1000; /* convert to µAh */
209fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
210a4e3f91b98d86ae0b5c816fe45190bb29ac32f71Daniel Mack	if (di->full_active_uAh == di->empty_uAh)
211a4e3f91b98d86ae0b5c816fe45190bb29ac32f71Daniel Mack		di->rem_capacity = 0;
212a4e3f91b98d86ae0b5c816fe45190bb29ac32f71Daniel Mack	else
213a4e3f91b98d86ae0b5c816fe45190bb29ac32f71Daniel Mack		/* From Maxim Application Note 131: remaining capacity =
214a4e3f91b98d86ae0b5c816fe45190bb29ac32f71Daniel Mack		 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
215a4e3f91b98d86ae0b5c816fe45190bb29ac32f71Daniel Mack		di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
216a4e3f91b98d86ae0b5c816fe45190bb29ac32f71Daniel Mack				    (di->full_active_uAh - di->empty_uAh);
217fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
218fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (di->rem_capacity < 0)
219fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		di->rem_capacity = 0;
220fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (di->rem_capacity > 100)
221fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		di->rem_capacity = 100;
222fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
22386af95039b69a90db15294eb1f9c147f1df0a8eaSven Neumann	if (di->current_uA < -100L)
224b0525b48f06714e8d5cf6a3266261b71de8d6dd4Daniel Mack		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
225b0525b48f06714e8d5cf6a3266261b71de8d6dd4Daniel Mack					/ (di->current_uA / 100L);
226fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	else
227fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		di->life_sec = 0;
228fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
229fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	return 0;
230fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov}
231fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
23202d0d2758821c38b2601d34dac544140af09e651Daniel Mackstatic void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
23302d0d2758821c38b2601d34dac544140af09e651Daniel Mack					     unsigned int acr_val)
23402d0d2758821c38b2601d34dac544140af09e651Daniel Mack{
23502d0d2758821c38b2601d34dac544140af09e651Daniel Mack	unsigned char acr[2];
23602d0d2758821c38b2601d34dac544140af09e651Daniel Mack
23702d0d2758821c38b2601d34dac544140af09e651Daniel Mack	/* acr is in units of 0.25 mAh */
23802d0d2758821c38b2601d34dac544140af09e651Daniel Mack	acr_val *= 4L;
23902d0d2758821c38b2601d34dac544140af09e651Daniel Mack	acr_val /= 1000;
24002d0d2758821c38b2601d34dac544140af09e651Daniel Mack
24102d0d2758821c38b2601d34dac544140af09e651Daniel Mack	acr[0] = acr_val >> 8;
24202d0d2758821c38b2601d34dac544140af09e651Daniel Mack	acr[1] = acr_val & 0xff;
24302d0d2758821c38b2601d34dac544140af09e651Daniel Mack
24402d0d2758821c38b2601d34dac544140af09e651Daniel Mack	if (w1_ds2760_write(di->w1_dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
24502d0d2758821c38b2601d34dac544140af09e651Daniel Mack		dev_warn(di->dev, "ACR write failed\n");
24602d0d2758821c38b2601d34dac544140af09e651Daniel Mack}
24702d0d2758821c38b2601d34dac544140af09e651Daniel Mack
248fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic void ds2760_battery_update_status(struct ds2760_device_info *di)
249fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov{
250fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int old_charge_status = di->charge_status;
251fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
252fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	ds2760_battery_read_status(di);
253fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
254fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
255fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		di->full_counter = 0;
256fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
257fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (power_supply_am_i_supplied(&di->bat)) {
258fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		if (di->current_uA > 10000) {
259fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
260fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			di->full_counter = 0;
261fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		} else if (di->current_uA < -5000) {
262fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
263fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov				dev_notice(di->dev, "not enough power to "
264fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov					   "charge\n");
265fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
266fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			di->full_counter = 0;
267fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		} else if (di->current_uA < 10000 &&
268fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			    di->charge_status != POWER_SUPPLY_STATUS_FULL) {
269fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
270fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			/* Don't consider the battery to be full unless
271fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			 * we've seen the current < 10 mA at least two
272fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			 * consecutive times. */
273fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
274fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			di->full_counter++;
275fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
276fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			if (di->full_counter < 2) {
277fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov				di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
278fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			} else {
279fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov				di->charge_status = POWER_SUPPLY_STATUS_FULL;
28002d0d2758821c38b2601d34dac544140af09e651Daniel Mack				ds2760_battery_set_current_accum(di,
28102d0d2758821c38b2601d34dac544140af09e651Daniel Mack						di->full_active_uAh);
282fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov			}
283fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		}
284fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	} else {
285fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
286fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		di->full_counter = 0;
287fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	}
288fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
289fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (di->charge_status != old_charge_status)
290fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		power_supply_changed(&di->bat);
291fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov}
292fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
293cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mackstatic void ds2760_battery_write_status(struct ds2760_device_info *di,
294cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack					char status)
295cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack{
296cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	if (status == di->raw[DS2760_STATUS_REG])
297cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack		return;
298cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack
299cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	w1_ds2760_write(di->w1_dev, &status, DS2760_STATUS_WRITE_REG, 1);
300cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
301cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
302cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack}
303cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack
304c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mackstatic void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
305c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack						unsigned char rated_capacity)
306c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack{
307c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack	if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
308c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack		return;
309c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack
310c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack	w1_ds2760_write(di->w1_dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
311c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack	w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
312c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack	w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
313c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack}
314c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack
315bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mackstatic void ds2760_battery_write_active_full(struct ds2760_device_info *di,
316bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack					     int active_full)
317bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack{
318bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	unsigned char tmp[2] = {
319bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		active_full >> 8,
320bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		active_full & 0xff
321bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	};
322bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
323bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
324bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	    tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
325bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		return;
326bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
327bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	w1_ds2760_write(di->w1_dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp));
328bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK0);
329bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK0);
330bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
331bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	/* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
332bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	 * values won't be read back by ds2760_battery_read_status() */
333bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	di->raw[DS2760_ACTIVE_FULL] = tmp[0];
334bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1];
335bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack}
336bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
337fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic void ds2760_battery_work(struct work_struct *work)
338fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov{
339fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct ds2760_device_info *di = container_of(work,
340fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		struct ds2760_device_info, monitor_work.work);
341fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	const int interval = HZ * 60;
342fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
3430cddc0a906ee3e47ce3e09107d385ff89f87cd6dHarvey Harrison	dev_dbg(di->dev, "%s\n", __func__);
344fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
345fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	ds2760_battery_update_status(di);
346fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
347fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov}
348fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
349fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
350fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov					      bat);
351fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
352fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic void ds2760_battery_external_power_changed(struct power_supply *psy)
353fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov{
354fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct ds2760_device_info *di = to_ds2760_device_info(psy);
355fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
3560cddc0a906ee3e47ce3e09107d385ff89f87cd6dHarvey Harrison	dev_dbg(di->dev, "%s\n", __func__);
357fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
35841f63c5359d14ca995172b8f6eaffd93f60fec54Tejun Heo	mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
359fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov}
360fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
3618d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3628d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mackstatic void ds2760_battery_set_charged_work(struct work_struct *work)
3638d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack{
3648d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	char bias;
3658d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	struct ds2760_device_info *di = container_of(work,
3668d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack		struct ds2760_device_info, set_charged_work.work);
3678d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3688d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	dev_dbg(di->dev, "%s\n", __func__);
3698d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3708d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	ds2760_battery_read_status(di);
3718d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3728d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	/* When we get notified by external circuitry that the battery is
3738d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	 * considered fully charged now, we know that there is no current
3748d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	 * flow any more. However, the ds2760's internal current meter is
3758d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	 * too inaccurate to rely on - spec say something ~15% failure.
3768d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	 * Hence, we use the current offset bias register to compensate
3778d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	 * that error.
3788d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	 */
3798d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3808d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	if (!power_supply_am_i_supplied(&di->bat))
3818d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack		return;
3828d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3838d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	bias = (signed char) di->current_raw +
3848d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack		(signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
3858d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3868d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
3878d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3888d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	w1_ds2760_write(di->w1_dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1);
3898d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
3908d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
3918d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3928d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	/* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
3938d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	 * value won't be read back by ds2760_battery_read_status() */
3948d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias;
3958d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack}
3968d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
3978d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mackstatic void ds2760_battery_set_charged(struct power_supply *psy)
3988d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack{
3998d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	struct ds2760_device_info *di = to_ds2760_device_info(psy);
4008d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
4018d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	/* postpone the actual work by 20 secs. This is for debouncing GPIO
4028d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	 * signals and to let the current value settle. See AN4188. */
40341f63c5359d14ca995172b8f6eaffd93f60fec54Tejun Heo	mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20);
4048d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack}
4058d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack
406fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic int ds2760_battery_get_property(struct power_supply *psy,
407fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov				       enum power_supply_property psp,
408fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov				       union power_supply_propval *val)
409fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov{
410fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct ds2760_device_info *di = to_ds2760_device_info(psy);
411fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
412fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	switch (psp) {
413fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	case POWER_SUPPLY_PROP_STATUS:
414fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		val->intval = di->charge_status;
415fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		return 0;
416fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	default:
417fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		break;
418fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	}
419fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
420fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	ds2760_battery_read_status(di);
421fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
422fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	switch (psp) {
423fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
424fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		val->intval = di->voltage_uV;
425fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		break;
426fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	case POWER_SUPPLY_PROP_CURRENT_NOW:
427fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		val->intval = di->current_uA;
428fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		break;
429fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
430fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		val->intval = di->rated_capacity;
431fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		break;
432fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	case POWER_SUPPLY_PROP_CHARGE_FULL:
433fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		val->intval = di->full_active_uAh;
434fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		break;
435fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	case POWER_SUPPLY_PROP_CHARGE_EMPTY:
436fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		val->intval = di->empty_uAh;
437fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		break;
438fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	case POWER_SUPPLY_PROP_CHARGE_NOW:
439fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		val->intval = di->accum_current_uAh;
440fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		break;
441fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	case POWER_SUPPLY_PROP_TEMP:
442fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		val->intval = di->temp_C;
443fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		break;
4445c6e9bf2c96e746237516bc8897add67682ee452Daniel Mack	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
4455c6e9bf2c96e746237516bc8897add67682ee452Daniel Mack		val->intval = di->life_sec;
4465c6e9bf2c96e746237516bc8897add67682ee452Daniel Mack		break;
4475c6e9bf2c96e746237516bc8897add67682ee452Daniel Mack	case POWER_SUPPLY_PROP_CAPACITY:
4485c6e9bf2c96e746237516bc8897add67682ee452Daniel Mack		val->intval = di->rem_capacity;
4495c6e9bf2c96e746237516bc8897add67682ee452Daniel Mack		break;
450fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	default:
451fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		return -EINVAL;
452fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	}
453fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
454fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	return 0;
455fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov}
456fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
457bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mackstatic int ds2760_battery_set_property(struct power_supply *psy,
458bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack				       enum power_supply_property psp,
459bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack				       const union power_supply_propval *val)
460bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack{
461bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	struct ds2760_device_info *di = to_ds2760_device_info(psy);
462bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
463bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	switch (psp) {
464bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	case POWER_SUPPLY_PROP_CHARGE_FULL:
465bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		/* the interface counts in uAh, convert the value */
466bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		ds2760_battery_write_active_full(di, val->intval / 1000L);
467bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		break;
468bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
469bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	case POWER_SUPPLY_PROP_CHARGE_NOW:
470bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		/* ds2760_battery_set_current_accum() does the conversion */
471bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		ds2760_battery_set_current_accum(di, val->intval);
472bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		break;
473bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
474bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	default:
475bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		return -EPERM;
476bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	}
477bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
478bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	return 0;
479bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack}
480bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
481bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mackstatic int ds2760_battery_property_is_writeable(struct power_supply *psy,
482bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack						enum power_supply_property psp)
483bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack{
484bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	switch (psp) {
485bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	case POWER_SUPPLY_PROP_CHARGE_FULL:
486bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	case POWER_SUPPLY_PROP_CHARGE_NOW:
487bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		return 1;
488bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
489bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	default:
490bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack		break;
491bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	}
492bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
493bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	return 0;
494bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack}
495bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack
496fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic enum power_supply_property ds2760_battery_props[] = {
497fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	POWER_SUPPLY_PROP_STATUS,
498fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	POWER_SUPPLY_PROP_VOLTAGE_NOW,
499fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	POWER_SUPPLY_PROP_CURRENT_NOW,
500fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
501fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	POWER_SUPPLY_PROP_CHARGE_FULL,
502fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	POWER_SUPPLY_PROP_CHARGE_EMPTY,
503fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	POWER_SUPPLY_PROP_CHARGE_NOW,
504fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	POWER_SUPPLY_PROP_TEMP,
5055c6e9bf2c96e746237516bc8897add67682ee452Daniel Mack	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
5065c6e9bf2c96e746237516bc8897add67682ee452Daniel Mack	POWER_SUPPLY_PROP_CAPACITY,
507fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov};
508fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
509fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic int ds2760_battery_probe(struct platform_device *pdev)
510fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov{
511cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	char status;
512fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	int retval = 0;
513fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct ds2760_device_info *di;
514fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
5154ec730dc8c697d991b9ba8910cb3f8d985016becJingoo Han	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
516fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (!di) {
517fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		retval = -ENOMEM;
518fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		goto di_alloc_failed;
519fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	}
520fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
521fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	platform_set_drvdata(pdev, di);
522fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
523ae9fb6e814ecede683bcd404910085cea3ab1260Daniel Mack	di->dev			= &pdev->dev;
524ae9fb6e814ecede683bcd404910085cea3ab1260Daniel Mack	di->w1_dev		= pdev->dev.parent;
525ae9fb6e814ecede683bcd404910085cea3ab1260Daniel Mack	di->bat.name		= dev_name(&pdev->dev);
526ae9fb6e814ecede683bcd404910085cea3ab1260Daniel Mack	di->bat.type		= POWER_SUPPLY_TYPE_BATTERY;
527ae9fb6e814ecede683bcd404910085cea3ab1260Daniel Mack	di->bat.properties	= ds2760_battery_props;
528ae9fb6e814ecede683bcd404910085cea3ab1260Daniel Mack	di->bat.num_properties	= ARRAY_SIZE(ds2760_battery_props);
529ae9fb6e814ecede683bcd404910085cea3ab1260Daniel Mack	di->bat.get_property	= ds2760_battery_get_property;
530bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	di->bat.set_property	= ds2760_battery_set_property;
531bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack	di->bat.property_is_writeable =
532bd52ca555ef36bf5b790178cfe8b7d42b5c16ca6Daniel Mack				  ds2760_battery_property_is_writeable;
5338d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	di->bat.set_charged	= ds2760_battery_set_charged;
534fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->bat.external_power_changed =
535fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov				  ds2760_battery_external_power_changed;
536fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
537fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
538fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
539cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	/* enable sleep mode feature */
540cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	ds2760_battery_read_status(di);
541cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	status = di->raw[DS2760_STATUS_REG];
542cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	if (pmod_enabled)
543cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack		status |= DS2760_STATUS_PMOD;
544cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	else
545cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack		status &= ~DS2760_STATUS_PMOD;
546cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack
547cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack	ds2760_battery_write_status(di, status);
548cef437e3a9b6d229d4ed3730cde047007267df6dDaniel Mack
549c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack	/* set rated capacity from module param */
550c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack	if (rated_capacity)
551c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack		ds2760_battery_write_rated_capacity(di, rated_capacity);
552c1e72193ea3fa02e96bf3aa66006e18d107d0266Daniel Mack
55302d0d2758821c38b2601d34dac544140af09e651Daniel Mack	/* set current accumulator if given as parameter.
55402d0d2758821c38b2601d34dac544140af09e651Daniel Mack	 * this should only be done for bootstrapping the value */
55502d0d2758821c38b2601d34dac544140af09e651Daniel Mack	if (current_accum)
55602d0d2758821c38b2601d34dac544140af09e651Daniel Mack		ds2760_battery_set_current_accum(di, current_accum);
55702d0d2758821c38b2601d34dac544140af09e651Daniel Mack
5582e83a5c5d2317c386de2880eb43ef0bef8eb1fa9Daniel Mack	retval = power_supply_register(&pdev->dev, &di->bat);
5592e83a5c5d2317c386de2880eb43ef0bef8eb1fa9Daniel Mack	if (retval) {
5602e83a5c5d2317c386de2880eb43ef0bef8eb1fa9Daniel Mack		dev_err(di->dev, "failed to register battery\n");
5612e83a5c5d2317c386de2880eb43ef0bef8eb1fa9Daniel Mack		goto batt_failed;
5622e83a5c5d2317c386de2880eb43ef0bef8eb1fa9Daniel Mack	}
5632e83a5c5d2317c386de2880eb43ef0bef8eb1fa9Daniel Mack
564fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
5658d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack	INIT_DELAYED_WORK(&di->set_charged_work,
5668d631ccff8d90fce77b42f01b3872595c599cbf9Daniel Mack			  ds2760_battery_set_charged_work);
567ba88b0029fcdc39d23acd4d0e64b38145ffa4d5fKay Sievers	di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
568fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	if (!di->monitor_wqueue) {
569fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		retval = -ESRCH;
570fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov		goto workqueue_failed;
571fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	}
572fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
573fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
574fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	goto success;
575fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
576fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovworkqueue_failed:
577fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	power_supply_unregister(&di->bat);
578fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovbatt_failed:
579fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovdi_alloc_failed:
580fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovsuccess:
581fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	return retval;
582fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov}
583fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
584fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic int ds2760_battery_remove(struct platform_device *pdev)
585fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov{
586fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct ds2760_device_info *di = platform_get_drvdata(pdev);
587fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
588afe2c511fb2d75f1515081ff1be15bd79cfe722dTejun Heo	cancel_delayed_work_sync(&di->monitor_work);
589afe2c511fb2d75f1515081ff1be15bd79cfe722dTejun Heo	cancel_delayed_work_sync(&di->set_charged_work);
590fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	destroy_workqueue(di->monitor_wqueue);
591fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	power_supply_unregister(&di->bat);
592fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
593fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	return 0;
594fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov}
595fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
596fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov#ifdef CONFIG_PM
597fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov
598fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsovstatic int ds2760_battery_suspend(struct platform_device *pdev,
599fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov				  pm_message_t state)
600fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov{
601fe0e3153acfef4864b69932cf116eb5f38f7500cAnton Vorontsov	struct ds2760_device_info *