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