mfd: twl-core: regulator configuration for twl6030 V1V8, V2V1 SMPS
[linux-2.6.git] / drivers / power / ds2780_battery.c
1 /*
2  * 1-wire client/driver for the Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC
3  *
4  * Copyright (C) 2010 Indesign, LLC
5  *
6  * Author: Clifton Barnes <cabarnes@indesign-llc.com>
7  *
8  * Based on ds2760_battery and ds2782_battery drivers
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/param.h>
19 #include <linux/pm.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/idr.h>
23
24 #include "../w1/w1.h"
25 #include "../w1/slaves/w1_ds2780.h"
26
27 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
28 #define DS2780_CURRENT_UNITS    1563
29 /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
30 #define DS2780_CHARGE_UNITS             6250
31 /* Number of bytes in user EEPROM space */
32 #define DS2780_USER_EEPROM_SIZE         (DS2780_EEPROM_BLOCK0_END - \
33                                         DS2780_EEPROM_BLOCK0_START + 1)
34 /* Number of bytes in parameter EEPROM space */
35 #define DS2780_PARAM_EEPROM_SIZE        (DS2780_EEPROM_BLOCK1_END - \
36                                         DS2780_EEPROM_BLOCK1_START + 1)
37
38 struct ds2780_device_info {
39         struct device *dev;
40         struct power_supply bat;
41         struct device *w1_dev;
42         struct task_struct *mutex_holder;
43 };
44
45 enum current_types {
46         CURRENT_NOW,
47         CURRENT_AVG,
48 };
49
50 static const char model[] = "DS2780";
51 static const char manufacturer[] = "Maxim/Dallas";
52
53 static inline struct ds2780_device_info *
54 to_ds2780_device_info(struct power_supply *psy)
55 {
56         return container_of(psy, struct ds2780_device_info, bat);
57 }
58
59 static inline struct power_supply *to_power_supply(struct device *dev)
60 {
61         return dev_get_drvdata(dev);
62 }
63
64 static inline int ds2780_battery_io(struct ds2780_device_info *dev_info,
65         char *buf, int addr, size_t count, int io)
66 {
67         if (dev_info->mutex_holder == current)
68                 return w1_ds2780_io_nolock(dev_info->w1_dev, buf, addr, count, io);
69         else
70                 return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
71 }
72
73 static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
74         int addr)
75 {
76         return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
77 }
78
79 static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
80         int addr)
81 {
82         int ret;
83         u8 raw[2];
84
85         ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
86         if (ret < 0)
87                 return ret;
88
89         *val = (raw[0] << 8) | raw[1];
90
91         return 0;
92 }
93
94 static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
95         u8 *val, int addr, size_t count)
96 {
97         return ds2780_battery_io(dev_info, val, addr, count, 0);
98 }
99
100 static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
101         int addr, size_t count)
102 {
103         return ds2780_battery_io(dev_info, val, addr, count, 1);
104 }
105
106 static inline int ds2780_store_eeprom(struct device *dev, int addr)
107 {
108         return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
109 }
110
111 static inline int ds2780_recall_eeprom(struct device *dev, int addr)
112 {
113         return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
114 }
115
116 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
117 {
118         int ret;
119
120         ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
121         if (ret < 0)
122                 return ret;
123
124         ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
125         if (ret < 0)
126                 return ret;
127
128         return 0;
129 }
130
131 /* Set sense resistor value in mhos */
132 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
133         u8 conductance)
134 {
135         int ret;
136
137         ret = ds2780_write(dev_info, &conductance,
138                                 DS2780_RSNSP_REG, sizeof(u8));
139         if (ret < 0)
140                 return ret;
141
142         return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
143 }
144
145 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
146 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
147         u16 *rsgain)
148 {
149         return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
150 }
151
152 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
153 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
154         u16 rsgain)
155 {
156         int ret;
157         u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
158
159         ret = ds2780_write(dev_info, raw,
160                                 DS2780_RSGAIN_MSB_REG, sizeof(raw));
161         if (ret < 0)
162                 return ret;
163
164         return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
165 }
166
167 static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
168         int *voltage_uV)
169 {
170         int ret;
171         s16 voltage_raw;
172
173         /*
174          * The voltage value is located in 10 bits across the voltage MSB
175          * and LSB registers in two's compliment form
176          * Sign bit of the voltage value is in bit 7 of the voltage MSB register
177          * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
178          * voltage MSB register
179          * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
180          * voltage LSB register
181          */
182         ret = ds2780_read16(dev_info, &voltage_raw,
183                                 DS2780_VOLT_MSB_REG);
184         if (ret < 0)
185                 return ret;
186
187         /*
188          * DS2780 reports voltage in units of 4.88mV, but the battery class
189          * reports in units of uV, so convert by multiplying by 4880.
190          */
191         *voltage_uV = (voltage_raw / 32) * 4880;
192         return 0;
193 }
194
195 static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
196         int *temperature)
197 {
198         int ret;
199         s16 temperature_raw;
200
201         /*
202          * The temperature value is located in 10 bits across the temperature
203          * MSB and LSB registers in two's compliment form
204          * Sign bit of the temperature value is in bit 7 of the temperature
205          * MSB register
206          * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
207          * temperature MSB register
208          * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
209          * temperature LSB register
210          */
211         ret = ds2780_read16(dev_info, &temperature_raw,
212                                 DS2780_TEMP_MSB_REG);
213         if (ret < 0)
214                 return ret;
215
216         /*
217          * Temperature is measured in units of 0.125 degrees celcius, the
218          * power_supply class measures temperature in tenths of degrees
219          * celsius. The temperature value is stored as a 10 bit number, plus
220          * sign in the upper bits of a 16 bit register.
221          */
222         *temperature = ((temperature_raw / 32) * 125) / 100;
223         return 0;
224 }
225
226 static int ds2780_get_current(struct ds2780_device_info *dev_info,
227         enum current_types type, int *current_uA)
228 {
229         int ret, sense_res;
230         s16 current_raw;
231         u8 sense_res_raw, reg_msb;
232
233         /*
234          * The units of measurement for current are dependent on the value of
235          * the sense resistor.
236          */
237         ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
238         if (ret < 0)
239                 return ret;
240
241         if (sense_res_raw == 0) {
242                 dev_err(dev_info->dev, "sense resistor value is 0\n");
243                 return -EINVAL;
244         }
245         sense_res = 1000 / sense_res_raw;
246
247         if (type == CURRENT_NOW)
248                 reg_msb = DS2780_CURRENT_MSB_REG;
249         else if (type == CURRENT_AVG)
250                 reg_msb = DS2780_IAVG_MSB_REG;
251         else
252                 return -EINVAL;
253
254         /*
255          * The current value is located in 16 bits across the current MSB
256          * and LSB registers in two's compliment form
257          * Sign bit of the current value is in bit 7 of the current MSB register
258          * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
259          * MSB register
260          * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
261          * LSB register
262          */
263         ret = ds2780_read16(dev_info, &current_raw, reg_msb);
264         if (ret < 0)
265                 return ret;
266
267         *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
268         return 0;
269 }
270
271 static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
272         int *accumulated_current)
273 {
274         int ret, sense_res;
275         s16 current_raw;
276         u8 sense_res_raw;
277
278         /*
279          * The units of measurement for accumulated current are dependent on
280          * the value of the sense resistor.
281          */
282         ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
283         if (ret < 0)
284                 return ret;
285
286         if (sense_res_raw == 0) {
287                 dev_err(dev_info->dev, "sense resistor value is 0\n");
288                 return -ENXIO;
289         }
290         sense_res = 1000 / sense_res_raw;
291
292         /*
293          * The ACR value is located in 16 bits across the ACR MSB and
294          * LSB registers
295          * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
296          * MSB register
297          * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
298          * LSB register
299          */
300         ret = ds2780_read16(dev_info, &current_raw, DS2780_ACR_MSB_REG);
301         if (ret < 0)
302                 return ret;
303
304         *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
305         return 0;
306 }
307
308 static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
309         int *capacity)
310 {
311         int ret;
312         u8 raw;
313
314         ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
315         if (ret < 0)
316                 return ret;
317
318         *capacity = raw;
319         return raw;
320 }
321
322 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
323 {
324         int ret, current_uA, capacity;
325
326         ret = ds2780_get_current(dev_info, CURRENT_NOW, &current_uA);
327         if (ret < 0)
328                 return ret;
329
330         ret = ds2780_get_capacity(dev_info, &capacity);
331         if (ret < 0)
332                 return ret;
333
334         if (capacity == 100)
335                 *status = POWER_SUPPLY_STATUS_FULL;
336         else if (current_uA == 0)
337                 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
338         else if (current_uA < 0)
339                 *status = POWER_SUPPLY_STATUS_DISCHARGING;
340         else
341                 *status = POWER_SUPPLY_STATUS_CHARGING;
342
343         return 0;
344 }
345
346 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
347         int *charge_now)
348 {
349         int ret;
350         u16 charge_raw;
351
352         /*
353          * The RAAC value is located in 16 bits across the RAAC MSB and
354          * LSB registers
355          * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
356          * MSB register
357          * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
358          * LSB register
359          */
360         ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
361         if (ret < 0)
362                 return ret;
363
364         *charge_now = charge_raw * 1600;
365         return 0;
366 }
367
368 static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
369         u8 *control_reg)
370 {
371         return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
372 }
373
374 static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
375         u8 control_reg)
376 {
377         int ret;
378
379         ret = ds2780_write(dev_info, &control_reg,
380                                 DS2780_CONTROL_REG, sizeof(u8));
381         if (ret < 0)
382                 return ret;
383
384         return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
385 }
386
387 static int ds2780_battery_get_property(struct power_supply *psy,
388         enum power_supply_property psp,
389         union power_supply_propval *val)
390 {
391         int ret = 0;
392         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
393
394         switch (psp) {
395         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
396                 ret = ds2780_get_voltage(dev_info, &val->intval);
397                 break;
398
399         case POWER_SUPPLY_PROP_TEMP:
400                 ret = ds2780_get_temperature(dev_info, &val->intval);
401                 break;
402
403         case POWER_SUPPLY_PROP_MODEL_NAME:
404                 val->strval = model;
405                 break;
406
407         case POWER_SUPPLY_PROP_MANUFACTURER:
408                 val->strval = manufacturer;
409                 break;
410
411         case POWER_SUPPLY_PROP_CURRENT_NOW:
412                 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
413                 break;
414
415         case POWER_SUPPLY_PROP_CURRENT_AVG:
416                 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
417                 break;
418
419         case POWER_SUPPLY_PROP_STATUS:
420                 ret = ds2780_get_status(dev_info, &val->intval);
421                 break;
422
423         case POWER_SUPPLY_PROP_CAPACITY:
424                 ret = ds2780_get_capacity(dev_info, &val->intval);
425                 break;
426
427         case POWER_SUPPLY_PROP_CHARGE_COUNTER:
428                 ret = ds2780_get_accumulated_current(dev_info, &val->intval);
429                 break;
430
431         case POWER_SUPPLY_PROP_CHARGE_NOW:
432                 ret = ds2780_get_charge_now(dev_info, &val->intval);
433                 break;
434
435         default:
436                 ret = -EINVAL;
437         }
438
439         return ret;
440 }
441
442 static enum power_supply_property ds2780_battery_props[] = {
443         POWER_SUPPLY_PROP_STATUS,
444         POWER_SUPPLY_PROP_VOLTAGE_NOW,
445         POWER_SUPPLY_PROP_TEMP,
446         POWER_SUPPLY_PROP_MODEL_NAME,
447         POWER_SUPPLY_PROP_MANUFACTURER,
448         POWER_SUPPLY_PROP_CURRENT_NOW,
449         POWER_SUPPLY_PROP_CURRENT_AVG,
450         POWER_SUPPLY_PROP_CAPACITY,
451         POWER_SUPPLY_PROP_CHARGE_COUNTER,
452         POWER_SUPPLY_PROP_CHARGE_NOW,
453 };
454
455 static ssize_t ds2780_get_pmod_enabled(struct device *dev,
456         struct device_attribute *attr,
457         char *buf)
458 {
459         int ret;
460         u8 control_reg;
461         struct power_supply *psy = to_power_supply(dev);
462         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
463
464         /* Get power mode */
465         ret = ds2780_get_control_register(dev_info, &control_reg);
466         if (ret < 0)
467                 return ret;
468
469         return sprintf(buf, "%d\n",
470                  !!(control_reg & DS2780_CONTROL_REG_PMOD));
471 }
472
473 static ssize_t ds2780_set_pmod_enabled(struct device *dev,
474         struct device_attribute *attr,
475         const char *buf,
476         size_t count)
477 {
478         int ret;
479         u8 control_reg, new_setting;
480         struct power_supply *psy = to_power_supply(dev);
481         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
482
483         /* Set power mode */
484         ret = ds2780_get_control_register(dev_info, &control_reg);
485         if (ret < 0)
486                 return ret;
487
488         ret = kstrtou8(buf, 0, &new_setting);
489         if (ret < 0)
490                 return ret;
491
492         if ((new_setting != 0) && (new_setting != 1)) {
493                 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
494                 return -EINVAL;
495         }
496
497         if (new_setting)
498                 control_reg |= DS2780_CONTROL_REG_PMOD;
499         else
500                 control_reg &= ~DS2780_CONTROL_REG_PMOD;
501
502         ret = ds2780_set_control_register(dev_info, control_reg);
503         if (ret < 0)
504                 return ret;
505
506         return count;
507 }
508
509 static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
510         struct device_attribute *attr,
511         char *buf)
512 {
513         int ret;
514         u8 sense_resistor;
515         struct power_supply *psy = to_power_supply(dev);
516         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
517
518         ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
519         if (ret < 0)
520                 return ret;
521
522         ret = sprintf(buf, "%d\n", sense_resistor);
523         return ret;
524 }
525
526 static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
527         struct device_attribute *attr,
528         const char *buf,
529         size_t count)
530 {
531         int ret;
532         u8 new_setting;
533         struct power_supply *psy = to_power_supply(dev);
534         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
535
536         ret = kstrtou8(buf, 0, &new_setting);
537         if (ret < 0)
538                 return ret;
539
540         ret = ds2780_set_sense_register(dev_info, new_setting);
541         if (ret < 0)
542                 return ret;
543
544         return count;
545 }
546
547 static ssize_t ds2780_get_rsgain_setting(struct device *dev,
548         struct device_attribute *attr,
549         char *buf)
550 {
551         int ret;
552         u16 rsgain;
553         struct power_supply *psy = to_power_supply(dev);
554         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
555
556         ret = ds2780_get_rsgain_register(dev_info, &rsgain);
557         if (ret < 0)
558                 return ret;
559
560         return sprintf(buf, "%d\n", rsgain);
561 }
562
563 static ssize_t ds2780_set_rsgain_setting(struct device *dev,
564         struct device_attribute *attr,
565         const char *buf,
566         size_t count)
567 {
568         int ret;
569         u16 new_setting;
570         struct power_supply *psy = to_power_supply(dev);
571         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
572
573         ret = kstrtou16(buf, 0, &new_setting);
574         if (ret < 0)
575                 return ret;
576
577         /* Gain can only be from 0 to 1.999 in steps of .001 */
578         if (new_setting > 1999) {
579                 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
580                 return -EINVAL;
581         }
582
583         ret = ds2780_set_rsgain_register(dev_info, new_setting);
584         if (ret < 0)
585                 return ret;
586
587         return count;
588 }
589
590 static ssize_t ds2780_get_pio_pin(struct device *dev,
591         struct device_attribute *attr,
592         char *buf)
593 {
594         int ret;
595         u8 sfr;
596         struct power_supply *psy = to_power_supply(dev);
597         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
598
599         ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
600         if (ret < 0)
601                 return ret;
602
603         ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
604         return ret;
605 }
606
607 static ssize_t ds2780_set_pio_pin(struct device *dev,
608         struct device_attribute *attr,
609         const char *buf,
610         size_t count)
611 {
612         int ret;
613         u8 new_setting;
614         struct power_supply *psy = to_power_supply(dev);
615         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
616
617         ret = kstrtou8(buf, 0, &new_setting);
618         if (ret < 0)
619                 return ret;
620
621         if ((new_setting != 0) && (new_setting != 1)) {
622                 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
623                 return -EINVAL;
624         }
625
626         ret = ds2780_write(dev_info, &new_setting,
627                                 DS2780_SFR_REG, sizeof(u8));
628         if (ret < 0)
629                 return ret;
630
631         return count;
632 }
633
634 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
635                                 struct kobject *kobj,
636                                 struct bin_attribute *bin_attr,
637                                 char *buf, loff_t off, size_t count)
638 {
639         struct device *dev = container_of(kobj, struct device, kobj);
640         struct power_supply *psy = to_power_supply(dev);
641         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
642
643         count = min_t(loff_t, count,
644                 DS2780_EEPROM_BLOCK1_END -
645                 DS2780_EEPROM_BLOCK1_START + 1 - off);
646
647         return ds2780_read_block(dev_info, buf,
648                                 DS2780_EEPROM_BLOCK1_START + off, count);
649 }
650
651 static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
652                                 struct kobject *kobj,
653                                 struct bin_attribute *bin_attr,
654                                 char *buf, loff_t off, size_t count)
655 {
656         struct device *dev = container_of(kobj, struct device, kobj);
657         struct power_supply *psy = to_power_supply(dev);
658         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
659         int ret;
660
661         count = min_t(loff_t, count,
662                 DS2780_EEPROM_BLOCK1_END -
663                 DS2780_EEPROM_BLOCK1_START + 1 - off);
664
665         ret = ds2780_write(dev_info, buf,
666                                 DS2780_EEPROM_BLOCK1_START + off, count);
667         if (ret < 0)
668                 return ret;
669
670         ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
671         if (ret < 0)
672                 return ret;
673
674         return count;
675 }
676
677 static struct bin_attribute ds2780_param_eeprom_bin_attr = {
678         .attr = {
679                 .name = "param_eeprom",
680                 .mode = S_IRUGO | S_IWUSR,
681         },
682         .size = DS2780_EEPROM_BLOCK1_END - DS2780_EEPROM_BLOCK1_START + 1,
683         .read = ds2780_read_param_eeprom_bin,
684         .write = ds2780_write_param_eeprom_bin,
685 };
686
687 static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
688                                 struct kobject *kobj,
689                                 struct bin_attribute *bin_attr,
690                                 char *buf, loff_t off, size_t count)
691 {
692         struct device *dev = container_of(kobj, struct device, kobj);
693         struct power_supply *psy = to_power_supply(dev);
694         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
695
696         count = min_t(loff_t, count,
697                 DS2780_EEPROM_BLOCK0_END -
698                 DS2780_EEPROM_BLOCK0_START + 1 - off);
699
700         return ds2780_read_block(dev_info, buf,
701                                 DS2780_EEPROM_BLOCK0_START + off, count);
702 }
703
704 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
705                                 struct kobject *kobj,
706                                 struct bin_attribute *bin_attr,
707                                 char *buf, loff_t off, size_t count)
708 {
709         struct device *dev = container_of(kobj, struct device, kobj);
710         struct power_supply *psy = to_power_supply(dev);
711         struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
712         int ret;
713
714         count = min_t(loff_t, count,
715                 DS2780_EEPROM_BLOCK0_END -
716                 DS2780_EEPROM_BLOCK0_START + 1 - off);
717
718         ret = ds2780_write(dev_info, buf,
719                                 DS2780_EEPROM_BLOCK0_START + off, count);
720         if (ret < 0)
721                 return ret;
722
723         ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
724         if (ret < 0)
725                 return ret;
726
727         return count;
728 }
729
730 static struct bin_attribute ds2780_user_eeprom_bin_attr = {
731         .attr = {
732                 .name = "user_eeprom",
733                 .mode = S_IRUGO | S_IWUSR,
734         },
735         .size = DS2780_EEPROM_BLOCK0_END - DS2780_EEPROM_BLOCK0_START + 1,
736         .read = ds2780_read_user_eeprom_bin,
737         .write = ds2780_write_user_eeprom_bin,
738 };
739
740 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
741         ds2780_set_pmod_enabled);
742 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
743         ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
744 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
745         ds2780_set_rsgain_setting);
746 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
747         ds2780_set_pio_pin);
748
749
750 static struct attribute *ds2780_attributes[] = {
751         &dev_attr_pmod_enabled.attr,
752         &dev_attr_sense_resistor_value.attr,
753         &dev_attr_rsgain_setting.attr,
754         &dev_attr_pio_pin.attr,
755         NULL
756 };
757
758 static const struct attribute_group ds2780_attr_group = {
759         .attrs = ds2780_attributes,
760 };
761
762 static int __devinit ds2780_battery_probe(struct platform_device *pdev)
763 {
764         int ret = 0;
765         struct ds2780_device_info *dev_info;
766
767         dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
768         if (!dev_info) {
769                 ret = -ENOMEM;
770                 goto fail;
771         }
772
773         platform_set_drvdata(pdev, dev_info);
774
775         dev_info->dev                   = &pdev->dev;
776         dev_info->w1_dev                = pdev->dev.parent;
777         dev_info->bat.name              = dev_name(&pdev->dev);
778         dev_info->bat.type              = POWER_SUPPLY_TYPE_BATTERY;
779         dev_info->bat.properties        = ds2780_battery_props;
780         dev_info->bat.num_properties    = ARRAY_SIZE(ds2780_battery_props);
781         dev_info->bat.get_property      = ds2780_battery_get_property;
782         dev_info->mutex_holder          = current;
783
784         ret = power_supply_register(&pdev->dev, &dev_info->bat);
785         if (ret) {
786                 dev_err(dev_info->dev, "failed to register battery\n");
787                 goto fail_free_info;
788         }
789
790         ret = sysfs_create_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
791         if (ret) {
792                 dev_err(dev_info->dev, "failed to create sysfs group\n");
793                 goto fail_unregister;
794         }
795
796         ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
797                                         &ds2780_param_eeprom_bin_attr);
798         if (ret) {
799                 dev_err(dev_info->dev,
800                                 "failed to create param eeprom bin file");
801                 goto fail_remove_group;
802         }
803
804         ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
805                                         &ds2780_user_eeprom_bin_attr);
806         if (ret) {
807                 dev_err(dev_info->dev,
808                                 "failed to create user eeprom bin file");
809                 goto fail_remove_bin_file;
810         }
811
812         dev_info->mutex_holder = NULL;
813
814         return 0;
815
816 fail_remove_bin_file:
817         sysfs_remove_bin_file(&dev_info->bat.dev->kobj,
818                                 &ds2780_param_eeprom_bin_attr);
819 fail_remove_group:
820         sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
821 fail_unregister:
822         power_supply_unregister(&dev_info->bat);
823 fail_free_info:
824         kfree(dev_info);
825 fail:
826         return ret;
827 }
828
829 static int __devexit ds2780_battery_remove(struct platform_device *pdev)
830 {
831         struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
832
833         dev_info->mutex_holder = current;
834
835         /* remove attributes */
836         sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
837
838         power_supply_unregister(&dev_info->bat);
839
840         kfree(dev_info);
841         return 0;
842 }
843
844 static struct platform_driver ds2780_battery_driver = {
845         .driver = {
846                 .name = "ds2780-battery",
847         },
848         .probe    = ds2780_battery_probe,
849         .remove   = __devexit_p(ds2780_battery_remove),
850 };
851
852 module_platform_driver(ds2780_battery_driver);
853
854 MODULE_LICENSE("GPL");
855 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
856 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver");
857 MODULE_ALIAS("platform:ds2780-battery");