0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <linux/module.h>
0023 #include <linux/param.h>
0024 #include <linux/jiffies.h>
0025 #include <linux/workqueue.h>
0026 #include <linux/pm.h>
0027 #include <linux/slab.h>
0028 #include <linux/platform_device.h>
0029 #include <linux/power_supply.h>
0030 #include <linux/suspend.h>
0031 #include <linux/w1.h>
0032 #include <linux/of.h>
0033
0034 static unsigned int cache_time = 1000;
0035 module_param(cache_time, uint, 0644);
0036 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
0037
0038 static bool pmod_enabled;
0039 module_param(pmod_enabled, bool, 0644);
0040 MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
0041
0042 static unsigned int rated_capacity;
0043 module_param(rated_capacity, uint, 0644);
0044 MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
0045
0046 static unsigned int current_accum;
0047 module_param(current_accum, uint, 0644);
0048 MODULE_PARM_DESC(current_accum, "current accumulator value");
0049
0050 #define W1_FAMILY_DS2760 0x30
0051
0052
0053 #define W1_DS2760_SWAP 0xAA
0054 #define W1_DS2760_READ_DATA 0x69
0055 #define W1_DS2760_WRITE_DATA 0x6C
0056 #define W1_DS2760_COPY_DATA 0x48
0057 #define W1_DS2760_RECALL_DATA 0xB8
0058 #define W1_DS2760_LOCK 0x6A
0059
0060
0061 #define DS2760_DATA_SIZE 0x40
0062
0063 #define DS2760_PROTECTION_REG 0x00
0064
0065 #define DS2760_STATUS_REG 0x01
0066 #define DS2760_STATUS_IE (1 << 2)
0067 #define DS2760_STATUS_SWEN (1 << 3)
0068 #define DS2760_STATUS_RNAOP (1 << 4)
0069 #define DS2760_STATUS_PMOD (1 << 5)
0070
0071 #define DS2760_EEPROM_REG 0x07
0072 #define DS2760_SPECIAL_FEATURE_REG 0x08
0073 #define DS2760_VOLTAGE_MSB 0x0c
0074 #define DS2760_VOLTAGE_LSB 0x0d
0075 #define DS2760_CURRENT_MSB 0x0e
0076 #define DS2760_CURRENT_LSB 0x0f
0077 #define DS2760_CURRENT_ACCUM_MSB 0x10
0078 #define DS2760_CURRENT_ACCUM_LSB 0x11
0079 #define DS2760_TEMP_MSB 0x18
0080 #define DS2760_TEMP_LSB 0x19
0081 #define DS2760_EEPROM_BLOCK0 0x20
0082 #define DS2760_ACTIVE_FULL 0x20
0083 #define DS2760_EEPROM_BLOCK1 0x30
0084 #define DS2760_STATUS_WRITE_REG 0x31
0085 #define DS2760_RATED_CAPACITY 0x32
0086 #define DS2760_CURRENT_OFFSET_BIAS 0x33
0087 #define DS2760_ACTIVE_EMPTY 0x3b
0088
0089 struct ds2760_device_info {
0090 struct device *dev;
0091
0092
0093 unsigned long update_time;
0094 char raw[DS2760_DATA_SIZE];
0095 int voltage_raw;
0096 int voltage_uV;
0097 int current_raw;
0098 int current_uA;
0099 int accum_current_raw;
0100 int accum_current_uAh;
0101 int temp_raw;
0102 int temp_C;
0103 int rated_capacity;
0104 int rem_capacity;
0105 int full_active_uAh;
0106 int empty_uAh;
0107 int life_sec;
0108 int charge_status;
0109
0110 int full_counter;
0111 struct power_supply *bat;
0112 struct power_supply_desc bat_desc;
0113 struct workqueue_struct *monitor_wqueue;
0114 struct delayed_work monitor_work;
0115 struct delayed_work set_charged_work;
0116 struct notifier_block pm_notifier;
0117 };
0118
0119 static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
0120 int io)
0121 {
0122 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
0123
0124 if (!dev)
0125 return 0;
0126
0127 mutex_lock(&sl->master->bus_mutex);
0128
0129 if (addr > DS2760_DATA_SIZE || addr < 0) {
0130 count = 0;
0131 goto out;
0132 }
0133 if (addr + count > DS2760_DATA_SIZE)
0134 count = DS2760_DATA_SIZE - addr;
0135
0136 if (!w1_reset_select_slave(sl)) {
0137 if (!io) {
0138 w1_write_8(sl->master, W1_DS2760_READ_DATA);
0139 w1_write_8(sl->master, addr);
0140 count = w1_read_block(sl->master, buf, count);
0141 } else {
0142 w1_write_8(sl->master, W1_DS2760_WRITE_DATA);
0143 w1_write_8(sl->master, addr);
0144 w1_write_block(sl->master, buf, count);
0145
0146 }
0147 }
0148
0149 out:
0150 mutex_unlock(&sl->master->bus_mutex);
0151
0152 return count;
0153 }
0154
0155 static int w1_ds2760_read(struct device *dev,
0156 char *buf, int addr,
0157 size_t count)
0158 {
0159 return w1_ds2760_io(dev, buf, addr, count, 0);
0160 }
0161
0162 static int w1_ds2760_write(struct device *dev,
0163 char *buf,
0164 int addr, size_t count)
0165 {
0166 return w1_ds2760_io(dev, buf, addr, count, 1);
0167 }
0168
0169 static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
0170 {
0171 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
0172
0173 if (!dev)
0174 return -EINVAL;
0175
0176 mutex_lock(&sl->master->bus_mutex);
0177
0178 if (w1_reset_select_slave(sl) == 0) {
0179 w1_write_8(sl->master, cmd);
0180 w1_write_8(sl->master, addr);
0181 }
0182
0183 mutex_unlock(&sl->master->bus_mutex);
0184 return 0;
0185 }
0186
0187 static int w1_ds2760_store_eeprom(struct device *dev, int addr)
0188 {
0189 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
0190 }
0191
0192 static int w1_ds2760_recall_eeprom(struct device *dev, int addr)
0193 {
0194 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
0195 }
0196
0197 static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj,
0198 struct bin_attribute *bin_attr, char *buf,
0199 loff_t off, size_t count)
0200 {
0201 struct device *dev = kobj_to_dev(kobj);
0202 return w1_ds2760_read(dev, buf, off, count);
0203 }
0204
0205 static BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE);
0206
0207 static struct bin_attribute *w1_ds2760_bin_attrs[] = {
0208 &bin_attr_w1_slave,
0209 NULL,
0210 };
0211
0212 static const struct attribute_group w1_ds2760_group = {
0213 .bin_attrs = w1_ds2760_bin_attrs,
0214 };
0215
0216 static const struct attribute_group *w1_ds2760_groups[] = {
0217 &w1_ds2760_group,
0218 NULL,
0219 };
0220
0221
0222 static int rated_capacities[] = {
0223 0,
0224 920,
0225 920,
0226 920,
0227 920,
0228 1440,
0229 1440,
0230 #ifdef CONFIG_MACH_H4700
0231 1800,
0232 #else
0233 1440,
0234 #endif
0235 1440,
0236 2880,
0237 2880,
0238 2880,
0239 2880,
0240 #ifdef CONFIG_MACH_H4700
0241 0,
0242 3600,
0243 #endif
0244 };
0245
0246
0247
0248 static int battery_interpolate(int array[], int temp)
0249 {
0250 int index, dt;
0251
0252 if (temp <= 0)
0253 return array[0];
0254 if (temp >= 40)
0255 return array[4];
0256
0257 index = temp / 10;
0258 dt = temp % 10;
0259
0260 return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
0261 }
0262
0263 static int ds2760_battery_read_status(struct ds2760_device_info *di)
0264 {
0265 int ret, i, start, count, scale[5];
0266
0267 if (di->update_time && time_before(jiffies, di->update_time +
0268 msecs_to_jiffies(cache_time)))
0269 return 0;
0270
0271
0272
0273 if (di->update_time == 0) {
0274 start = 0;
0275 count = DS2760_DATA_SIZE;
0276 } else {
0277 start = DS2760_VOLTAGE_MSB;
0278 count = DS2760_TEMP_LSB - start + 1;
0279 }
0280
0281 ret = w1_ds2760_read(di->dev, di->raw + start, start, count);
0282 if (ret != count) {
0283 dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
0284 di->dev);
0285 return 1;
0286 }
0287
0288 di->update_time = jiffies;
0289
0290
0291
0292 di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
0293 (di->raw[DS2760_VOLTAGE_LSB] >> 5);
0294 di->voltage_uV = di->voltage_raw * 4880;
0295
0296
0297
0298 di->current_raw =
0299 (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
0300 (di->raw[DS2760_CURRENT_LSB] >> 3);
0301 di->current_uA = di->current_raw * 625;
0302
0303
0304 di->accum_current_raw =
0305 (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
0306 di->raw[DS2760_CURRENT_ACCUM_LSB];
0307 di->accum_current_uAh = di->accum_current_raw * 250;
0308
0309
0310
0311
0312 di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
0313 (di->raw[DS2760_TEMP_LSB] >> 5);
0314 di->temp_C = di->temp_raw + (di->temp_raw / 4);
0315
0316
0317
0318 if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
0319 di->rated_capacity = rated_capacities[
0320 (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
0321 else
0322 di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
0323
0324 di->rated_capacity *= 1000;
0325
0326
0327 di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
0328 di->raw[DS2760_ACTIVE_FULL + 1];
0329
0330
0331
0332
0333 if (di->full_active_uAh == 0)
0334 di->full_active_uAh = di->rated_capacity / 1000L;
0335
0336 scale[0] = di->full_active_uAh;
0337 for (i = 1; i < 5; i++)
0338 scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i];
0339
0340 di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
0341 di->full_active_uAh *= 1000;
0342
0343
0344 scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
0345 for (i = 3; i >= 0; i--)
0346 scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
0347
0348 di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
0349 di->empty_uAh *= 1000;
0350
0351 if (di->full_active_uAh == di->empty_uAh)
0352 di->rem_capacity = 0;
0353 else
0354
0355
0356 di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
0357 (di->full_active_uAh - di->empty_uAh);
0358
0359 if (di->rem_capacity < 0)
0360 di->rem_capacity = 0;
0361 if (di->rem_capacity > 100)
0362 di->rem_capacity = 100;
0363
0364 if (di->current_uA < -100L)
0365 di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
0366 / (di->current_uA / 100L);
0367 else
0368 di->life_sec = 0;
0369
0370 return 0;
0371 }
0372
0373 static void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
0374 unsigned int acr_val)
0375 {
0376 unsigned char acr[2];
0377
0378
0379 acr_val *= 4L;
0380 acr_val /= 1000;
0381
0382 acr[0] = acr_val >> 8;
0383 acr[1] = acr_val & 0xff;
0384
0385 if (w1_ds2760_write(di->dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
0386 dev_warn(di->dev, "ACR write failed\n");
0387 }
0388
0389 static void ds2760_battery_update_status(struct ds2760_device_info *di)
0390 {
0391 int old_charge_status = di->charge_status;
0392
0393 ds2760_battery_read_status(di);
0394
0395 if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
0396 di->full_counter = 0;
0397
0398 if (power_supply_am_i_supplied(di->bat)) {
0399 if (di->current_uA > 10000) {
0400 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
0401 di->full_counter = 0;
0402 } else if (di->current_uA < -5000) {
0403 if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
0404 dev_notice(di->dev, "not enough power to "
0405 "charge\n");
0406 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
0407 di->full_counter = 0;
0408 } else if (di->current_uA < 10000 &&
0409 di->charge_status != POWER_SUPPLY_STATUS_FULL) {
0410
0411
0412
0413
0414
0415 di->full_counter++;
0416
0417 if (di->full_counter < 2) {
0418 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
0419 } else {
0420 di->charge_status = POWER_SUPPLY_STATUS_FULL;
0421 ds2760_battery_set_current_accum(di,
0422 di->full_active_uAh);
0423 }
0424 }
0425 } else {
0426 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
0427 di->full_counter = 0;
0428 }
0429
0430 if (di->charge_status != old_charge_status)
0431 power_supply_changed(di->bat);
0432 }
0433
0434 static void ds2760_battery_write_status(struct ds2760_device_info *di,
0435 char status)
0436 {
0437 if (status == di->raw[DS2760_STATUS_REG])
0438 return;
0439
0440 w1_ds2760_write(di->dev, &status, DS2760_STATUS_WRITE_REG, 1);
0441 w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
0442 w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
0443 }
0444
0445 static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
0446 unsigned char rated_capacity)
0447 {
0448 if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
0449 return;
0450
0451 w1_ds2760_write(di->dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
0452 w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
0453 w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
0454 }
0455
0456 static void ds2760_battery_write_active_full(struct ds2760_device_info *di,
0457 int active_full)
0458 {
0459 unsigned char tmp[2] = {
0460 active_full >> 8,
0461 active_full & 0xff
0462 };
0463
0464 if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
0465 tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
0466 return;
0467
0468 w1_ds2760_write(di->dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp));
0469 w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
0470 w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
0471
0472
0473
0474 di->raw[DS2760_ACTIVE_FULL] = tmp[0];
0475 di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1];
0476 }
0477
0478 static void ds2760_battery_work(struct work_struct *work)
0479 {
0480 struct ds2760_device_info *di = container_of(work,
0481 struct ds2760_device_info, monitor_work.work);
0482 const int interval = HZ * 60;
0483
0484 dev_dbg(di->dev, "%s\n", __func__);
0485
0486 ds2760_battery_update_status(di);
0487 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
0488 }
0489
0490 static void ds2760_battery_external_power_changed(struct power_supply *psy)
0491 {
0492 struct ds2760_device_info *di = power_supply_get_drvdata(psy);
0493
0494 dev_dbg(di->dev, "%s\n", __func__);
0495
0496 mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
0497 }
0498
0499
0500 static void ds2760_battery_set_charged_work(struct work_struct *work)
0501 {
0502 char bias;
0503 struct ds2760_device_info *di = container_of(work,
0504 struct ds2760_device_info, set_charged_work.work);
0505
0506 dev_dbg(di->dev, "%s\n", __func__);
0507
0508 ds2760_battery_read_status(di);
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518 if (!power_supply_am_i_supplied(di->bat))
0519 return;
0520
0521 bias = (signed char) di->current_raw +
0522 (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
0523
0524 dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
0525
0526 w1_ds2760_write(di->dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1);
0527 w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
0528 w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
0529
0530
0531
0532 di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias;
0533 }
0534
0535 static void ds2760_battery_set_charged(struct power_supply *psy)
0536 {
0537 struct ds2760_device_info *di = power_supply_get_drvdata(psy);
0538
0539
0540
0541 mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20);
0542 }
0543
0544 static int ds2760_battery_get_property(struct power_supply *psy,
0545 enum power_supply_property psp,
0546 union power_supply_propval *val)
0547 {
0548 struct ds2760_device_info *di = power_supply_get_drvdata(psy);
0549
0550 switch (psp) {
0551 case POWER_SUPPLY_PROP_STATUS:
0552 val->intval = di->charge_status;
0553 return 0;
0554 default:
0555 break;
0556 }
0557
0558 ds2760_battery_read_status(di);
0559
0560 switch (psp) {
0561 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0562 val->intval = di->voltage_uV;
0563 break;
0564 case POWER_SUPPLY_PROP_CURRENT_NOW:
0565 val->intval = di->current_uA;
0566 break;
0567 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0568 val->intval = di->rated_capacity;
0569 break;
0570 case POWER_SUPPLY_PROP_CHARGE_FULL:
0571 val->intval = di->full_active_uAh;
0572 break;
0573 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
0574 val->intval = di->empty_uAh;
0575 break;
0576 case POWER_SUPPLY_PROP_CHARGE_NOW:
0577 val->intval = di->accum_current_uAh;
0578 break;
0579 case POWER_SUPPLY_PROP_TEMP:
0580 val->intval = di->temp_C;
0581 break;
0582 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
0583 val->intval = di->life_sec;
0584 break;
0585 case POWER_SUPPLY_PROP_CAPACITY:
0586 val->intval = di->rem_capacity;
0587 break;
0588 default:
0589 return -EINVAL;
0590 }
0591
0592 return 0;
0593 }
0594
0595 static int ds2760_battery_set_property(struct power_supply *psy,
0596 enum power_supply_property psp,
0597 const union power_supply_propval *val)
0598 {
0599 struct ds2760_device_info *di = power_supply_get_drvdata(psy);
0600
0601 switch (psp) {
0602 case POWER_SUPPLY_PROP_CHARGE_FULL:
0603
0604 ds2760_battery_write_active_full(di, val->intval / 1000L);
0605 break;
0606
0607 case POWER_SUPPLY_PROP_CHARGE_NOW:
0608
0609 ds2760_battery_set_current_accum(di, val->intval);
0610 break;
0611
0612 default:
0613 return -EPERM;
0614 }
0615
0616 return 0;
0617 }
0618
0619 static int ds2760_battery_property_is_writeable(struct power_supply *psy,
0620 enum power_supply_property psp)
0621 {
0622 switch (psp) {
0623 case POWER_SUPPLY_PROP_CHARGE_FULL:
0624 case POWER_SUPPLY_PROP_CHARGE_NOW:
0625 return 1;
0626
0627 default:
0628 break;
0629 }
0630
0631 return 0;
0632 }
0633
0634 static enum power_supply_property ds2760_battery_props[] = {
0635 POWER_SUPPLY_PROP_STATUS,
0636 POWER_SUPPLY_PROP_VOLTAGE_NOW,
0637 POWER_SUPPLY_PROP_CURRENT_NOW,
0638 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0639 POWER_SUPPLY_PROP_CHARGE_FULL,
0640 POWER_SUPPLY_PROP_CHARGE_EMPTY,
0641 POWER_SUPPLY_PROP_CHARGE_NOW,
0642 POWER_SUPPLY_PROP_TEMP,
0643 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
0644 POWER_SUPPLY_PROP_CAPACITY,
0645 };
0646
0647 static int ds2760_pm_notifier(struct notifier_block *notifier,
0648 unsigned long pm_event,
0649 void *unused)
0650 {
0651 struct ds2760_device_info *di =
0652 container_of(notifier, struct ds2760_device_info, pm_notifier);
0653
0654 switch (pm_event) {
0655 case PM_HIBERNATION_PREPARE:
0656 case PM_SUSPEND_PREPARE:
0657 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
0658 break;
0659
0660 case PM_POST_RESTORE:
0661 case PM_POST_HIBERNATION:
0662 case PM_POST_SUSPEND:
0663 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
0664 power_supply_changed(di->bat);
0665 mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
0666
0667 break;
0668
0669 case PM_RESTORE_PREPARE:
0670 default:
0671 break;
0672 }
0673
0674 return NOTIFY_DONE;
0675 }
0676
0677 static int w1_ds2760_add_slave(struct w1_slave *sl)
0678 {
0679 struct power_supply_config psy_cfg = {};
0680 struct ds2760_device_info *di;
0681 struct device *dev = &sl->dev;
0682 int retval = 0;
0683 char name[32];
0684 char status;
0685
0686 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
0687 if (!di) {
0688 retval = -ENOMEM;
0689 goto di_alloc_failed;
0690 }
0691
0692 snprintf(name, sizeof(name), "ds2760-battery.%d", dev->id);
0693
0694 di->dev = dev;
0695 di->bat_desc.name = name;
0696 di->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
0697 di->bat_desc.properties = ds2760_battery_props;
0698 di->bat_desc.num_properties = ARRAY_SIZE(ds2760_battery_props);
0699 di->bat_desc.get_property = ds2760_battery_get_property;
0700 di->bat_desc.set_property = ds2760_battery_set_property;
0701 di->bat_desc.property_is_writeable =
0702 ds2760_battery_property_is_writeable;
0703 di->bat_desc.set_charged = ds2760_battery_set_charged;
0704 di->bat_desc.external_power_changed =
0705 ds2760_battery_external_power_changed;
0706
0707 psy_cfg.drv_data = di;
0708
0709 if (dev->of_node) {
0710 u32 tmp;
0711
0712 psy_cfg.of_node = dev->of_node;
0713
0714 if (!of_property_read_bool(dev->of_node, "maxim,pmod-enabled"))
0715 pmod_enabled = true;
0716
0717 if (!of_property_read_u32(dev->of_node,
0718 "maxim,cache-time-ms", &tmp))
0719 cache_time = tmp;
0720
0721 if (!of_property_read_u32(dev->of_node,
0722 "rated-capacity-microamp-hours",
0723 &tmp))
0724 rated_capacity = tmp / 10;
0725 }
0726
0727 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
0728
0729 sl->family_data = di;
0730
0731
0732 ds2760_battery_read_status(di);
0733 status = di->raw[DS2760_STATUS_REG];
0734 if (pmod_enabled)
0735 status |= DS2760_STATUS_PMOD;
0736 else
0737 status &= ~DS2760_STATUS_PMOD;
0738
0739 ds2760_battery_write_status(di, status);
0740
0741
0742 if (rated_capacity)
0743 ds2760_battery_write_rated_capacity(di, rated_capacity);
0744
0745
0746
0747 if (current_accum)
0748 ds2760_battery_set_current_accum(di, current_accum);
0749
0750 di->bat = power_supply_register(dev, &di->bat_desc, &psy_cfg);
0751 if (IS_ERR(di->bat)) {
0752 dev_err(di->dev, "failed to register battery\n");
0753 retval = PTR_ERR(di->bat);
0754 goto batt_failed;
0755 }
0756
0757 INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
0758 INIT_DELAYED_WORK(&di->set_charged_work,
0759 ds2760_battery_set_charged_work);
0760 di->monitor_wqueue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
0761 if (!di->monitor_wqueue) {
0762 retval = -ESRCH;
0763 goto workqueue_failed;
0764 }
0765 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
0766
0767 di->pm_notifier.notifier_call = ds2760_pm_notifier;
0768 register_pm_notifier(&di->pm_notifier);
0769
0770 goto success;
0771
0772 workqueue_failed:
0773 power_supply_unregister(di->bat);
0774 batt_failed:
0775 di_alloc_failed:
0776 success:
0777 return retval;
0778 }
0779
0780 static void w1_ds2760_remove_slave(struct w1_slave *sl)
0781 {
0782 struct ds2760_device_info *di = sl->family_data;
0783
0784 unregister_pm_notifier(&di->pm_notifier);
0785 cancel_delayed_work_sync(&di->monitor_work);
0786 cancel_delayed_work_sync(&di->set_charged_work);
0787 destroy_workqueue(di->monitor_wqueue);
0788 power_supply_unregister(di->bat);
0789 }
0790
0791 #ifdef CONFIG_OF
0792 static const struct of_device_id w1_ds2760_of_ids[] = {
0793 { .compatible = "maxim,ds2760" },
0794 {}
0795 };
0796 #endif
0797
0798 static const struct w1_family_ops w1_ds2760_fops = {
0799 .add_slave = w1_ds2760_add_slave,
0800 .remove_slave = w1_ds2760_remove_slave,
0801 .groups = w1_ds2760_groups,
0802 };
0803
0804 static struct w1_family w1_ds2760_family = {
0805 .fid = W1_FAMILY_DS2760,
0806 .fops = &w1_ds2760_fops,
0807 .of_match_table = of_match_ptr(w1_ds2760_of_ids),
0808 };
0809 module_w1_family(w1_ds2760_family);
0810
0811 MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
0812 "Matt Reimer <mreimer@vpop.net>, "
0813 "Anton Vorontsov <cbou@mail.ru>");
0814 MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
0815 MODULE_LICENSE("GPL");
0816 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760));