Version:  2.0.40 2.2.26 2.4.37 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us