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