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