1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Driver for batteries with DS2760 chips inside.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright © 2007 Anton Vorontsov
5*4882a593Smuzhiyun * 2004-2007 Matt Reimer
6*4882a593Smuzhiyun * 2004 Szabolcs Gyurko
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Use consistent with the GNU GPL is permitted,
9*4882a593Smuzhiyun * provided that this copyright notice is
10*4882a593Smuzhiyun * preserved in its entirety in all copies and derived works.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Author: Anton Vorontsov <cbou@mail.ru>
13*4882a593Smuzhiyun * February 2007
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Matt Reimer <mreimer@vpop.net>
16*4882a593Smuzhiyun * April 2004, 2005, 2007
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
19*4882a593Smuzhiyun * September 2004
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <linux/module.h>
23*4882a593Smuzhiyun #include <linux/param.h>
24*4882a593Smuzhiyun #include <linux/jiffies.h>
25*4882a593Smuzhiyun #include <linux/workqueue.h>
26*4882a593Smuzhiyun #include <linux/pm.h>
27*4882a593Smuzhiyun #include <linux/slab.h>
28*4882a593Smuzhiyun #include <linux/platform_device.h>
29*4882a593Smuzhiyun #include <linux/power_supply.h>
30*4882a593Smuzhiyun #include <linux/suspend.h>
31*4882a593Smuzhiyun #include <linux/w1.h>
32*4882a593Smuzhiyun #include <linux/of.h>
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun static unsigned int cache_time = 1000;
35*4882a593Smuzhiyun module_param(cache_time, uint, 0644);
36*4882a593Smuzhiyun MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun static bool pmod_enabled;
39*4882a593Smuzhiyun module_param(pmod_enabled, bool, 0644);
40*4882a593Smuzhiyun MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun static unsigned int rated_capacity;
43*4882a593Smuzhiyun module_param(rated_capacity, uint, 0644);
44*4882a593Smuzhiyun MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun static unsigned int current_accum;
47*4882a593Smuzhiyun module_param(current_accum, uint, 0644);
48*4882a593Smuzhiyun MODULE_PARM_DESC(current_accum, "current accumulator value");
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #define W1_FAMILY_DS2760 0x30
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* Known commands to the DS2760 chip */
53*4882a593Smuzhiyun #define W1_DS2760_SWAP 0xAA
54*4882a593Smuzhiyun #define W1_DS2760_READ_DATA 0x69
55*4882a593Smuzhiyun #define W1_DS2760_WRITE_DATA 0x6C
56*4882a593Smuzhiyun #define W1_DS2760_COPY_DATA 0x48
57*4882a593Smuzhiyun #define W1_DS2760_RECALL_DATA 0xB8
58*4882a593Smuzhiyun #define W1_DS2760_LOCK 0x6A
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /* Number of valid register addresses */
61*4882a593Smuzhiyun #define DS2760_DATA_SIZE 0x40
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun #define DS2760_PROTECTION_REG 0x00
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun #define DS2760_STATUS_REG 0x01
66*4882a593Smuzhiyun #define DS2760_STATUS_IE (1 << 2)
67*4882a593Smuzhiyun #define DS2760_STATUS_SWEN (1 << 3)
68*4882a593Smuzhiyun #define DS2760_STATUS_RNAOP (1 << 4)
69*4882a593Smuzhiyun #define DS2760_STATUS_PMOD (1 << 5)
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #define DS2760_EEPROM_REG 0x07
72*4882a593Smuzhiyun #define DS2760_SPECIAL_FEATURE_REG 0x08
73*4882a593Smuzhiyun #define DS2760_VOLTAGE_MSB 0x0c
74*4882a593Smuzhiyun #define DS2760_VOLTAGE_LSB 0x0d
75*4882a593Smuzhiyun #define DS2760_CURRENT_MSB 0x0e
76*4882a593Smuzhiyun #define DS2760_CURRENT_LSB 0x0f
77*4882a593Smuzhiyun #define DS2760_CURRENT_ACCUM_MSB 0x10
78*4882a593Smuzhiyun #define DS2760_CURRENT_ACCUM_LSB 0x11
79*4882a593Smuzhiyun #define DS2760_TEMP_MSB 0x18
80*4882a593Smuzhiyun #define DS2760_TEMP_LSB 0x19
81*4882a593Smuzhiyun #define DS2760_EEPROM_BLOCK0 0x20
82*4882a593Smuzhiyun #define DS2760_ACTIVE_FULL 0x20
83*4882a593Smuzhiyun #define DS2760_EEPROM_BLOCK1 0x30
84*4882a593Smuzhiyun #define DS2760_STATUS_WRITE_REG 0x31
85*4882a593Smuzhiyun #define DS2760_RATED_CAPACITY 0x32
86*4882a593Smuzhiyun #define DS2760_CURRENT_OFFSET_BIAS 0x33
87*4882a593Smuzhiyun #define DS2760_ACTIVE_EMPTY 0x3b
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun struct ds2760_device_info {
90*4882a593Smuzhiyun struct device *dev;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* DS2760 data, valid after calling ds2760_battery_read_status() */
93*4882a593Smuzhiyun unsigned long update_time; /* jiffies when data read */
94*4882a593Smuzhiyun char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */
95*4882a593Smuzhiyun int voltage_raw; /* units of 4.88 mV */
96*4882a593Smuzhiyun int voltage_uV; /* units of µV */
97*4882a593Smuzhiyun int current_raw; /* units of 0.625 mA */
98*4882a593Smuzhiyun int current_uA; /* units of µA */
99*4882a593Smuzhiyun int accum_current_raw; /* units of 0.25 mAh */
100*4882a593Smuzhiyun int accum_current_uAh; /* units of µAh */
101*4882a593Smuzhiyun int temp_raw; /* units of 0.125 °C */
102*4882a593Smuzhiyun int temp_C; /* units of 0.1 °C */
103*4882a593Smuzhiyun int rated_capacity; /* units of µAh */
104*4882a593Smuzhiyun int rem_capacity; /* percentage */
105*4882a593Smuzhiyun int full_active_uAh; /* units of µAh */
106*4882a593Smuzhiyun int empty_uAh; /* units of µAh */
107*4882a593Smuzhiyun int life_sec; /* units of seconds */
108*4882a593Smuzhiyun int charge_status; /* POWER_SUPPLY_STATUS_* */
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun int full_counter;
111*4882a593Smuzhiyun struct power_supply *bat;
112*4882a593Smuzhiyun struct power_supply_desc bat_desc;
113*4882a593Smuzhiyun struct workqueue_struct *monitor_wqueue;
114*4882a593Smuzhiyun struct delayed_work monitor_work;
115*4882a593Smuzhiyun struct delayed_work set_charged_work;
116*4882a593Smuzhiyun struct notifier_block pm_notifier;
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun
w1_ds2760_io(struct device * dev,char * buf,int addr,size_t count,int io)119*4882a593Smuzhiyun static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
120*4882a593Smuzhiyun int io)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun if (!dev)
125*4882a593Smuzhiyun return 0;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun mutex_lock(&sl->master->bus_mutex);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun if (addr > DS2760_DATA_SIZE || addr < 0) {
130*4882a593Smuzhiyun count = 0;
131*4882a593Smuzhiyun goto out;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun if (addr + count > DS2760_DATA_SIZE)
134*4882a593Smuzhiyun count = DS2760_DATA_SIZE - addr;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun if (!w1_reset_select_slave(sl)) {
137*4882a593Smuzhiyun if (!io) {
138*4882a593Smuzhiyun w1_write_8(sl->master, W1_DS2760_READ_DATA);
139*4882a593Smuzhiyun w1_write_8(sl->master, addr);
140*4882a593Smuzhiyun count = w1_read_block(sl->master, buf, count);
141*4882a593Smuzhiyun } else {
142*4882a593Smuzhiyun w1_write_8(sl->master, W1_DS2760_WRITE_DATA);
143*4882a593Smuzhiyun w1_write_8(sl->master, addr);
144*4882a593Smuzhiyun w1_write_block(sl->master, buf, count);
145*4882a593Smuzhiyun /* XXX w1_write_block returns void, not n_written */
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun out:
150*4882a593Smuzhiyun mutex_unlock(&sl->master->bus_mutex);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun return count;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
w1_ds2760_read(struct device * dev,char * buf,int addr,size_t count)155*4882a593Smuzhiyun static int w1_ds2760_read(struct device *dev,
156*4882a593Smuzhiyun char *buf, int addr,
157*4882a593Smuzhiyun size_t count)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun return w1_ds2760_io(dev, buf, addr, count, 0);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
w1_ds2760_write(struct device * dev,char * buf,int addr,size_t count)162*4882a593Smuzhiyun static int w1_ds2760_write(struct device *dev,
163*4882a593Smuzhiyun char *buf,
164*4882a593Smuzhiyun int addr, size_t count)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun return w1_ds2760_io(dev, buf, addr, count, 1);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
w1_ds2760_eeprom_cmd(struct device * dev,int addr,int cmd)169*4882a593Smuzhiyun static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun if (!dev)
174*4882a593Smuzhiyun return -EINVAL;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun mutex_lock(&sl->master->bus_mutex);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun if (w1_reset_select_slave(sl) == 0) {
179*4882a593Smuzhiyun w1_write_8(sl->master, cmd);
180*4882a593Smuzhiyun w1_write_8(sl->master, addr);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun mutex_unlock(&sl->master->bus_mutex);
184*4882a593Smuzhiyun return 0;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
w1_ds2760_store_eeprom(struct device * dev,int addr)187*4882a593Smuzhiyun static int w1_ds2760_store_eeprom(struct device *dev, int addr)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
w1_ds2760_recall_eeprom(struct device * dev,int addr)192*4882a593Smuzhiyun static int w1_ds2760_recall_eeprom(struct device *dev, int addr)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
w1_slave_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)197*4882a593Smuzhiyun static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj,
198*4882a593Smuzhiyun struct bin_attribute *bin_attr, char *buf,
199*4882a593Smuzhiyun loff_t off, size_t count)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun struct device *dev = container_of(kobj, struct device, kobj);
202*4882a593Smuzhiyun return w1_ds2760_read(dev, buf, off, count);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun static BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun static struct bin_attribute *w1_ds2760_bin_attrs[] = {
208*4882a593Smuzhiyun &bin_attr_w1_slave,
209*4882a593Smuzhiyun NULL,
210*4882a593Smuzhiyun };
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun static const struct attribute_group w1_ds2760_group = {
213*4882a593Smuzhiyun .bin_attrs = w1_ds2760_bin_attrs,
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun static const struct attribute_group *w1_ds2760_groups[] = {
217*4882a593Smuzhiyun &w1_ds2760_group,
218*4882a593Smuzhiyun NULL,
219*4882a593Smuzhiyun };
220*4882a593Smuzhiyun /* Some batteries have their rated capacity stored a N * 10 mAh, while
221*4882a593Smuzhiyun * others use an index into this table. */
222*4882a593Smuzhiyun static int rated_capacities[] = {
223*4882a593Smuzhiyun 0,
224*4882a593Smuzhiyun 920, /* Samsung */
225*4882a593Smuzhiyun 920, /* BYD */
226*4882a593Smuzhiyun 920, /* Lishen */
227*4882a593Smuzhiyun 920, /* NEC */
228*4882a593Smuzhiyun 1440, /* Samsung */
229*4882a593Smuzhiyun 1440, /* BYD */
230*4882a593Smuzhiyun #ifdef CONFIG_MACH_H4700
231*4882a593Smuzhiyun 1800, /* HP iPAQ hx4700 3.7V 1800mAh (359113-001) */
232*4882a593Smuzhiyun #else
233*4882a593Smuzhiyun 1440, /* Lishen */
234*4882a593Smuzhiyun #endif
235*4882a593Smuzhiyun 1440, /* NEC */
236*4882a593Smuzhiyun 2880, /* Samsung */
237*4882a593Smuzhiyun 2880, /* BYD */
238*4882a593Smuzhiyun 2880, /* Lishen */
239*4882a593Smuzhiyun 2880, /* NEC */
240*4882a593Smuzhiyun #ifdef CONFIG_MACH_H4700
241*4882a593Smuzhiyun 0,
242*4882a593Smuzhiyun 3600, /* HP iPAQ hx4700 3.7V 3600mAh (359114-001) */
243*4882a593Smuzhiyun #endif
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
247*4882a593Smuzhiyun * temp is in Celsius */
battery_interpolate(int array[],int temp)248*4882a593Smuzhiyun static int battery_interpolate(int array[], int temp)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun int index, dt;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun if (temp <= 0)
253*4882a593Smuzhiyun return array[0];
254*4882a593Smuzhiyun if (temp >= 40)
255*4882a593Smuzhiyun return array[4];
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun index = temp / 10;
258*4882a593Smuzhiyun dt = temp % 10;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
ds2760_battery_read_status(struct ds2760_device_info * di)263*4882a593Smuzhiyun static int ds2760_battery_read_status(struct ds2760_device_info *di)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun int ret, i, start, count, scale[5];
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun if (di->update_time && time_before(jiffies, di->update_time +
268*4882a593Smuzhiyun msecs_to_jiffies(cache_time)))
269*4882a593Smuzhiyun return 0;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* The first time we read the entire contents of SRAM/EEPROM,
272*4882a593Smuzhiyun * but after that we just read the interesting bits that change. */
273*4882a593Smuzhiyun if (di->update_time == 0) {
274*4882a593Smuzhiyun start = 0;
275*4882a593Smuzhiyun count = DS2760_DATA_SIZE;
276*4882a593Smuzhiyun } else {
277*4882a593Smuzhiyun start = DS2760_VOLTAGE_MSB;
278*4882a593Smuzhiyun count = DS2760_TEMP_LSB - start + 1;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun ret = w1_ds2760_read(di->dev, di->raw + start, start, count);
282*4882a593Smuzhiyun if (ret != count) {
283*4882a593Smuzhiyun dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
284*4882a593Smuzhiyun di->dev);
285*4882a593Smuzhiyun return 1;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun di->update_time = jiffies;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun /* DS2760 reports voltage in units of 4.88mV, but the battery class
291*4882a593Smuzhiyun * reports in units of uV, so convert by multiplying by 4880. */
292*4882a593Smuzhiyun di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
293*4882a593Smuzhiyun (di->raw[DS2760_VOLTAGE_LSB] >> 5);
294*4882a593Smuzhiyun di->voltage_uV = di->voltage_raw * 4880;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun /* DS2760 reports current in signed units of 0.625mA, but the battery
297*4882a593Smuzhiyun * class reports in units of µA, so convert by multiplying by 625. */
298*4882a593Smuzhiyun di->current_raw =
299*4882a593Smuzhiyun (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
300*4882a593Smuzhiyun (di->raw[DS2760_CURRENT_LSB] >> 3);
301*4882a593Smuzhiyun di->current_uA = di->current_raw * 625;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /* DS2760 reports accumulated current in signed units of 0.25mAh. */
304*4882a593Smuzhiyun di->accum_current_raw =
305*4882a593Smuzhiyun (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
306*4882a593Smuzhiyun di->raw[DS2760_CURRENT_ACCUM_LSB];
307*4882a593Smuzhiyun di->accum_current_uAh = di->accum_current_raw * 250;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /* DS2760 reports temperature in signed units of 0.125°C, but the
310*4882a593Smuzhiyun * battery class reports in units of 1/10 °C, so we convert by
311*4882a593Smuzhiyun * multiplying by .125 * 10 = 1.25. */
312*4882a593Smuzhiyun di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
313*4882a593Smuzhiyun (di->raw[DS2760_TEMP_LSB] >> 5);
314*4882a593Smuzhiyun di->temp_C = di->temp_raw + (di->temp_raw / 4);
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun /* At least some battery monitors (e.g. HP iPAQ) store the battery's
317*4882a593Smuzhiyun * maximum rated capacity. */
318*4882a593Smuzhiyun if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
319*4882a593Smuzhiyun di->rated_capacity = rated_capacities[
320*4882a593Smuzhiyun (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
321*4882a593Smuzhiyun else
322*4882a593Smuzhiyun di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun di->rated_capacity *= 1000; /* convert to µAh */
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun /* Calculate the full level at the present temperature. */
327*4882a593Smuzhiyun di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
328*4882a593Smuzhiyun di->raw[DS2760_ACTIVE_FULL + 1];
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /* If the full_active_uAh value is not given, fall back to the rated
331*4882a593Smuzhiyun * capacity. This is likely to happen when chips are not part of the
332*4882a593Smuzhiyun * battery pack and is therefore not bootstrapped. */
333*4882a593Smuzhiyun if (di->full_active_uAh == 0)
334*4882a593Smuzhiyun di->full_active_uAh = di->rated_capacity / 1000L;
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun scale[0] = di->full_active_uAh;
337*4882a593Smuzhiyun for (i = 1; i < 5; i++)
338*4882a593Smuzhiyun scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i];
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
341*4882a593Smuzhiyun di->full_active_uAh *= 1000; /* convert to µAh */
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun /* Calculate the empty level at the present temperature. */
344*4882a593Smuzhiyun scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
345*4882a593Smuzhiyun for (i = 3; i >= 0; i--)
346*4882a593Smuzhiyun scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
349*4882a593Smuzhiyun di->empty_uAh *= 1000; /* convert to µAh */
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun if (di->full_active_uAh == di->empty_uAh)
352*4882a593Smuzhiyun di->rem_capacity = 0;
353*4882a593Smuzhiyun else
354*4882a593Smuzhiyun /* From Maxim Application Note 131: remaining capacity =
355*4882a593Smuzhiyun * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
356*4882a593Smuzhiyun di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
357*4882a593Smuzhiyun (di->full_active_uAh - di->empty_uAh);
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun if (di->rem_capacity < 0)
360*4882a593Smuzhiyun di->rem_capacity = 0;
361*4882a593Smuzhiyun if (di->rem_capacity > 100)
362*4882a593Smuzhiyun di->rem_capacity = 100;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun if (di->current_uA < -100L)
365*4882a593Smuzhiyun di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
366*4882a593Smuzhiyun / (di->current_uA / 100L);
367*4882a593Smuzhiyun else
368*4882a593Smuzhiyun di->life_sec = 0;
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun return 0;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
ds2760_battery_set_current_accum(struct ds2760_device_info * di,unsigned int acr_val)373*4882a593Smuzhiyun static void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
374*4882a593Smuzhiyun unsigned int acr_val)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun unsigned char acr[2];
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun /* acr is in units of 0.25 mAh */
379*4882a593Smuzhiyun acr_val *= 4L;
380*4882a593Smuzhiyun acr_val /= 1000;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun acr[0] = acr_val >> 8;
383*4882a593Smuzhiyun acr[1] = acr_val & 0xff;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun if (w1_ds2760_write(di->dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
386*4882a593Smuzhiyun dev_warn(di->dev, "ACR write failed\n");
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
ds2760_battery_update_status(struct ds2760_device_info * di)389*4882a593Smuzhiyun static void ds2760_battery_update_status(struct ds2760_device_info *di)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun int old_charge_status = di->charge_status;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun ds2760_battery_read_status(di);
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
396*4882a593Smuzhiyun di->full_counter = 0;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun if (power_supply_am_i_supplied(di->bat)) {
399*4882a593Smuzhiyun if (di->current_uA > 10000) {
400*4882a593Smuzhiyun di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
401*4882a593Smuzhiyun di->full_counter = 0;
402*4882a593Smuzhiyun } else if (di->current_uA < -5000) {
403*4882a593Smuzhiyun if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
404*4882a593Smuzhiyun dev_notice(di->dev, "not enough power to "
405*4882a593Smuzhiyun "charge\n");
406*4882a593Smuzhiyun di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
407*4882a593Smuzhiyun di->full_counter = 0;
408*4882a593Smuzhiyun } else if (di->current_uA < 10000 &&
409*4882a593Smuzhiyun di->charge_status != POWER_SUPPLY_STATUS_FULL) {
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun /* Don't consider the battery to be full unless
412*4882a593Smuzhiyun * we've seen the current < 10 mA at least two
413*4882a593Smuzhiyun * consecutive times. */
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun di->full_counter++;
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun if (di->full_counter < 2) {
418*4882a593Smuzhiyun di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
419*4882a593Smuzhiyun } else {
420*4882a593Smuzhiyun di->charge_status = POWER_SUPPLY_STATUS_FULL;
421*4882a593Smuzhiyun ds2760_battery_set_current_accum(di,
422*4882a593Smuzhiyun di->full_active_uAh);
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun } else {
426*4882a593Smuzhiyun di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
427*4882a593Smuzhiyun di->full_counter = 0;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun if (di->charge_status != old_charge_status)
431*4882a593Smuzhiyun power_supply_changed(di->bat);
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun
ds2760_battery_write_status(struct ds2760_device_info * di,char status)434*4882a593Smuzhiyun static void ds2760_battery_write_status(struct ds2760_device_info *di,
435*4882a593Smuzhiyun char status)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun if (status == di->raw[DS2760_STATUS_REG])
438*4882a593Smuzhiyun return;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun w1_ds2760_write(di->dev, &status, DS2760_STATUS_WRITE_REG, 1);
441*4882a593Smuzhiyun w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
442*4882a593Smuzhiyun w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
ds2760_battery_write_rated_capacity(struct ds2760_device_info * di,unsigned char rated_capacity)445*4882a593Smuzhiyun static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
446*4882a593Smuzhiyun unsigned char rated_capacity)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
449*4882a593Smuzhiyun return;
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun w1_ds2760_write(di->dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
452*4882a593Smuzhiyun w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
453*4882a593Smuzhiyun w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
ds2760_battery_write_active_full(struct ds2760_device_info * di,int active_full)456*4882a593Smuzhiyun static void ds2760_battery_write_active_full(struct ds2760_device_info *di,
457*4882a593Smuzhiyun int active_full)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun unsigned char tmp[2] = {
460*4882a593Smuzhiyun active_full >> 8,
461*4882a593Smuzhiyun active_full & 0xff
462*4882a593Smuzhiyun };
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
465*4882a593Smuzhiyun tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
466*4882a593Smuzhiyun return;
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun w1_ds2760_write(di->dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp));
469*4882a593Smuzhiyun w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
470*4882a593Smuzhiyun w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun /* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
473*4882a593Smuzhiyun * values won't be read back by ds2760_battery_read_status() */
474*4882a593Smuzhiyun di->raw[DS2760_ACTIVE_FULL] = tmp[0];
475*4882a593Smuzhiyun di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1];
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun
ds2760_battery_work(struct work_struct * work)478*4882a593Smuzhiyun static void ds2760_battery_work(struct work_struct *work)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun struct ds2760_device_info *di = container_of(work,
481*4882a593Smuzhiyun struct ds2760_device_info, monitor_work.work);
482*4882a593Smuzhiyun const int interval = HZ * 60;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun dev_dbg(di->dev, "%s\n", __func__);
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun ds2760_battery_update_status(di);
487*4882a593Smuzhiyun queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun
ds2760_battery_external_power_changed(struct power_supply * psy)490*4882a593Smuzhiyun static void ds2760_battery_external_power_changed(struct power_supply *psy)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun struct ds2760_device_info *di = power_supply_get_drvdata(psy);
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun dev_dbg(di->dev, "%s\n", __func__);
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun
ds2760_battery_set_charged_work(struct work_struct * work)500*4882a593Smuzhiyun static void ds2760_battery_set_charged_work(struct work_struct *work)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun char bias;
503*4882a593Smuzhiyun struct ds2760_device_info *di = container_of(work,
504*4882a593Smuzhiyun struct ds2760_device_info, set_charged_work.work);
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun dev_dbg(di->dev, "%s\n", __func__);
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun ds2760_battery_read_status(di);
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /* When we get notified by external circuitry that the battery is
511*4882a593Smuzhiyun * considered fully charged now, we know that there is no current
512*4882a593Smuzhiyun * flow any more. However, the ds2760's internal current meter is
513*4882a593Smuzhiyun * too inaccurate to rely on - spec say something ~15% failure.
514*4882a593Smuzhiyun * Hence, we use the current offset bias register to compensate
515*4882a593Smuzhiyun * that error.
516*4882a593Smuzhiyun */
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun if (!power_supply_am_i_supplied(di->bat))
519*4882a593Smuzhiyun return;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun bias = (signed char) di->current_raw +
522*4882a593Smuzhiyun (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun w1_ds2760_write(di->dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1);
527*4882a593Smuzhiyun w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
528*4882a593Smuzhiyun w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
531*4882a593Smuzhiyun * value won't be read back by ds2760_battery_read_status() */
532*4882a593Smuzhiyun di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
ds2760_battery_set_charged(struct power_supply * psy)535*4882a593Smuzhiyun static void ds2760_battery_set_charged(struct power_supply *psy)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun struct ds2760_device_info *di = power_supply_get_drvdata(psy);
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun /* postpone the actual work by 20 secs. This is for debouncing GPIO
540*4882a593Smuzhiyun * signals and to let the current value settle. See AN4188. */
541*4882a593Smuzhiyun mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20);
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun
ds2760_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)544*4882a593Smuzhiyun static int ds2760_battery_get_property(struct power_supply *psy,
545*4882a593Smuzhiyun enum power_supply_property psp,
546*4882a593Smuzhiyun union power_supply_propval *val)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun struct ds2760_device_info *di = power_supply_get_drvdata(psy);
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun switch (psp) {
551*4882a593Smuzhiyun case POWER_SUPPLY_PROP_STATUS:
552*4882a593Smuzhiyun val->intval = di->charge_status;
553*4882a593Smuzhiyun return 0;
554*4882a593Smuzhiyun default:
555*4882a593Smuzhiyun break;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun ds2760_battery_read_status(di);
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun switch (psp) {
561*4882a593Smuzhiyun case POWER_SUPPLY_PROP_VOLTAGE_NOW:
562*4882a593Smuzhiyun val->intval = di->voltage_uV;
563*4882a593Smuzhiyun break;
564*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CURRENT_NOW:
565*4882a593Smuzhiyun val->intval = di->current_uA;
566*4882a593Smuzhiyun break;
567*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
568*4882a593Smuzhiyun val->intval = di->rated_capacity;
569*4882a593Smuzhiyun break;
570*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL:
571*4882a593Smuzhiyun val->intval = di->full_active_uAh;
572*4882a593Smuzhiyun break;
573*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_EMPTY:
574*4882a593Smuzhiyun val->intval = di->empty_uAh;
575*4882a593Smuzhiyun break;
576*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_NOW:
577*4882a593Smuzhiyun val->intval = di->accum_current_uAh;
578*4882a593Smuzhiyun break;
579*4882a593Smuzhiyun case POWER_SUPPLY_PROP_TEMP:
580*4882a593Smuzhiyun val->intval = di->temp_C;
581*4882a593Smuzhiyun break;
582*4882a593Smuzhiyun case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
583*4882a593Smuzhiyun val->intval = di->life_sec;
584*4882a593Smuzhiyun break;
585*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CAPACITY:
586*4882a593Smuzhiyun val->intval = di->rem_capacity;
587*4882a593Smuzhiyun break;
588*4882a593Smuzhiyun default:
589*4882a593Smuzhiyun return -EINVAL;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun return 0;
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun
ds2760_battery_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)595*4882a593Smuzhiyun static int ds2760_battery_set_property(struct power_supply *psy,
596*4882a593Smuzhiyun enum power_supply_property psp,
597*4882a593Smuzhiyun const union power_supply_propval *val)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun struct ds2760_device_info *di = power_supply_get_drvdata(psy);
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun switch (psp) {
602*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL:
603*4882a593Smuzhiyun /* the interface counts in uAh, convert the value */
604*4882a593Smuzhiyun ds2760_battery_write_active_full(di, val->intval / 1000L);
605*4882a593Smuzhiyun break;
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_NOW:
608*4882a593Smuzhiyun /* ds2760_battery_set_current_accum() does the conversion */
609*4882a593Smuzhiyun ds2760_battery_set_current_accum(di, val->intval);
610*4882a593Smuzhiyun break;
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun default:
613*4882a593Smuzhiyun return -EPERM;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun return 0;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
ds2760_battery_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)619*4882a593Smuzhiyun static int ds2760_battery_property_is_writeable(struct power_supply *psy,
620*4882a593Smuzhiyun enum power_supply_property psp)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun switch (psp) {
623*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL:
624*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_NOW:
625*4882a593Smuzhiyun return 1;
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun default:
628*4882a593Smuzhiyun break;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun return 0;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun static enum power_supply_property ds2760_battery_props[] = {
635*4882a593Smuzhiyun POWER_SUPPLY_PROP_STATUS,
636*4882a593Smuzhiyun POWER_SUPPLY_PROP_VOLTAGE_NOW,
637*4882a593Smuzhiyun POWER_SUPPLY_PROP_CURRENT_NOW,
638*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
639*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_FULL,
640*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_EMPTY,
641*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_NOW,
642*4882a593Smuzhiyun POWER_SUPPLY_PROP_TEMP,
643*4882a593Smuzhiyun POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
644*4882a593Smuzhiyun POWER_SUPPLY_PROP_CAPACITY,
645*4882a593Smuzhiyun };
646*4882a593Smuzhiyun
ds2760_pm_notifier(struct notifier_block * notifier,unsigned long pm_event,void * unused)647*4882a593Smuzhiyun static int ds2760_pm_notifier(struct notifier_block *notifier,
648*4882a593Smuzhiyun unsigned long pm_event,
649*4882a593Smuzhiyun void *unused)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun struct ds2760_device_info *di =
652*4882a593Smuzhiyun container_of(notifier, struct ds2760_device_info, pm_notifier);
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun switch (pm_event) {
655*4882a593Smuzhiyun case PM_HIBERNATION_PREPARE:
656*4882a593Smuzhiyun case PM_SUSPEND_PREPARE:
657*4882a593Smuzhiyun di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
658*4882a593Smuzhiyun break;
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun case PM_POST_RESTORE:
661*4882a593Smuzhiyun case PM_POST_HIBERNATION:
662*4882a593Smuzhiyun case PM_POST_SUSPEND:
663*4882a593Smuzhiyun di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
664*4882a593Smuzhiyun power_supply_changed(di->bat);
665*4882a593Smuzhiyun mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun break;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun case PM_RESTORE_PREPARE:
670*4882a593Smuzhiyun default:
671*4882a593Smuzhiyun break;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun return NOTIFY_DONE;
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun
w1_ds2760_add_slave(struct w1_slave * sl)677*4882a593Smuzhiyun static int w1_ds2760_add_slave(struct w1_slave *sl)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun struct power_supply_config psy_cfg = {};
680*4882a593Smuzhiyun struct ds2760_device_info *di;
681*4882a593Smuzhiyun struct device *dev = &sl->dev;
682*4882a593Smuzhiyun int retval = 0;
683*4882a593Smuzhiyun char name[32];
684*4882a593Smuzhiyun char status;
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
687*4882a593Smuzhiyun if (!di) {
688*4882a593Smuzhiyun retval = -ENOMEM;
689*4882a593Smuzhiyun goto di_alloc_failed;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun snprintf(name, sizeof(name), "ds2760-battery.%d", dev->id);
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun di->dev = dev;
695*4882a593Smuzhiyun di->bat_desc.name = name;
696*4882a593Smuzhiyun di->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
697*4882a593Smuzhiyun di->bat_desc.properties = ds2760_battery_props;
698*4882a593Smuzhiyun di->bat_desc.num_properties = ARRAY_SIZE(ds2760_battery_props);
699*4882a593Smuzhiyun di->bat_desc.get_property = ds2760_battery_get_property;
700*4882a593Smuzhiyun di->bat_desc.set_property = ds2760_battery_set_property;
701*4882a593Smuzhiyun di->bat_desc.property_is_writeable =
702*4882a593Smuzhiyun ds2760_battery_property_is_writeable;
703*4882a593Smuzhiyun di->bat_desc.set_charged = ds2760_battery_set_charged;
704*4882a593Smuzhiyun di->bat_desc.external_power_changed =
705*4882a593Smuzhiyun ds2760_battery_external_power_changed;
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun psy_cfg.drv_data = di;
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun if (dev->of_node) {
710*4882a593Smuzhiyun u32 tmp;
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun psy_cfg.of_node = dev->of_node;
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun if (!of_property_read_bool(dev->of_node, "maxim,pmod-enabled"))
715*4882a593Smuzhiyun pmod_enabled = true;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun if (!of_property_read_u32(dev->of_node,
718*4882a593Smuzhiyun "maxim,cache-time-ms", &tmp))
719*4882a593Smuzhiyun cache_time = tmp;
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun if (!of_property_read_u32(dev->of_node,
722*4882a593Smuzhiyun "rated-capacity-microamp-hours",
723*4882a593Smuzhiyun &tmp))
724*4882a593Smuzhiyun rated_capacity = tmp / 10; /* property is in mAh */
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun sl->family_data = di;
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun /* enable sleep mode feature */
732*4882a593Smuzhiyun ds2760_battery_read_status(di);
733*4882a593Smuzhiyun status = di->raw[DS2760_STATUS_REG];
734*4882a593Smuzhiyun if (pmod_enabled)
735*4882a593Smuzhiyun status |= DS2760_STATUS_PMOD;
736*4882a593Smuzhiyun else
737*4882a593Smuzhiyun status &= ~DS2760_STATUS_PMOD;
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun ds2760_battery_write_status(di, status);
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun /* set rated capacity from module param or device tree */
742*4882a593Smuzhiyun if (rated_capacity)
743*4882a593Smuzhiyun ds2760_battery_write_rated_capacity(di, rated_capacity);
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun /* set current accumulator if given as parameter.
746*4882a593Smuzhiyun * this should only be done for bootstrapping the value */
747*4882a593Smuzhiyun if (current_accum)
748*4882a593Smuzhiyun ds2760_battery_set_current_accum(di, current_accum);
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun di->bat = power_supply_register(dev, &di->bat_desc, &psy_cfg);
751*4882a593Smuzhiyun if (IS_ERR(di->bat)) {
752*4882a593Smuzhiyun dev_err(di->dev, "failed to register battery\n");
753*4882a593Smuzhiyun retval = PTR_ERR(di->bat);
754*4882a593Smuzhiyun goto batt_failed;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
758*4882a593Smuzhiyun INIT_DELAYED_WORK(&di->set_charged_work,
759*4882a593Smuzhiyun ds2760_battery_set_charged_work);
760*4882a593Smuzhiyun di->monitor_wqueue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
761*4882a593Smuzhiyun if (!di->monitor_wqueue) {
762*4882a593Smuzhiyun retval = -ESRCH;
763*4882a593Smuzhiyun goto workqueue_failed;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun di->pm_notifier.notifier_call = ds2760_pm_notifier;
768*4882a593Smuzhiyun register_pm_notifier(&di->pm_notifier);
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun goto success;
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun workqueue_failed:
773*4882a593Smuzhiyun power_supply_unregister(di->bat);
774*4882a593Smuzhiyun batt_failed:
775*4882a593Smuzhiyun di_alloc_failed:
776*4882a593Smuzhiyun success:
777*4882a593Smuzhiyun return retval;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
w1_ds2760_remove_slave(struct w1_slave * sl)780*4882a593Smuzhiyun static void w1_ds2760_remove_slave(struct w1_slave *sl)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun struct ds2760_device_info *di = sl->family_data;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun unregister_pm_notifier(&di->pm_notifier);
785*4882a593Smuzhiyun cancel_delayed_work_sync(&di->monitor_work);
786*4882a593Smuzhiyun cancel_delayed_work_sync(&di->set_charged_work);
787*4882a593Smuzhiyun destroy_workqueue(di->monitor_wqueue);
788*4882a593Smuzhiyun power_supply_unregister(di->bat);
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun #ifdef CONFIG_OF
792*4882a593Smuzhiyun static const struct of_device_id w1_ds2760_of_ids[] = {
793*4882a593Smuzhiyun { .compatible = "maxim,ds2760" },
794*4882a593Smuzhiyun {}
795*4882a593Smuzhiyun };
796*4882a593Smuzhiyun #endif
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun static const struct w1_family_ops w1_ds2760_fops = {
799*4882a593Smuzhiyun .add_slave = w1_ds2760_add_slave,
800*4882a593Smuzhiyun .remove_slave = w1_ds2760_remove_slave,
801*4882a593Smuzhiyun .groups = w1_ds2760_groups,
802*4882a593Smuzhiyun };
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun static struct w1_family w1_ds2760_family = {
805*4882a593Smuzhiyun .fid = W1_FAMILY_DS2760,
806*4882a593Smuzhiyun .fops = &w1_ds2760_fops,
807*4882a593Smuzhiyun .of_match_table = of_match_ptr(w1_ds2760_of_ids),
808*4882a593Smuzhiyun };
809*4882a593Smuzhiyun module_w1_family(w1_ds2760_family);
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
812*4882a593Smuzhiyun "Matt Reimer <mreimer@vpop.net>, "
813*4882a593Smuzhiyun "Anton Vorontsov <cbou@mail.ru>");
814*4882a593Smuzhiyun MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
815*4882a593Smuzhiyun MODULE_LICENSE("GPL");
816*4882a593Smuzhiyun MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760));
817