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