^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // max77693_charger.c - Battery charger driver for the Maxim 77693
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright (C) 2014 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // Krzysztof Kozlowski <krzk@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/mfd/max77693.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mfd/max77693-common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mfd/max77693-private.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define MAX77693_CHARGER_NAME "max77693-charger"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static const char *max77693_charger_model = "MAX77693";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static const char *max77693_charger_manufacturer = "Maxim Integrated";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct max77693_charger {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct max77693_dev *max77693;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct power_supply *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) u32 constant_volt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u32 min_system_volt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u32 thermal_regulation_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u32 batttery_overcurrent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 charge_input_threshold_volt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int max77693_get_charger_state(struct regmap *regmap, int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) data &= CHG_DETAILS_01_CHG_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) data >>= CHG_DETAILS_01_CHG_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) switch (data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) case MAX77693_CHARGING_PREQUALIFICATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) case MAX77693_CHARGING_FAST_CONST_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) case MAX77693_CHARGING_TOP_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* In high temp the charging current is reduced, but still charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) case MAX77693_CHARGING_HIGH_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *val = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) case MAX77693_CHARGING_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *val = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) case MAX77693_CHARGING_TIMER_EXPIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) case MAX77693_CHARGING_THERMISTOR_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) *val = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) case MAX77693_CHARGING_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) case MAX77693_CHARGING_OVER_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) case MAX77693_CHARGING_WATCHDOG_EXPIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *val = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) case MAX77693_CHARGING_RESERVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *val = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static int max77693_get_charge_type(struct regmap *regmap, int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) data &= CHG_DETAILS_01_CHG_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) data >>= CHG_DETAILS_01_CHG_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) switch (data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) case MAX77693_CHARGING_PREQUALIFICATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * Top-off: trickle or fast? In top-off the current varies between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * 100 and 250 mA. It is higher than prequalification current.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) case MAX77693_CHARGING_TOP_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) case MAX77693_CHARGING_FAST_CONST_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* In high temp the charging current is reduced, but still charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case MAX77693_CHARGING_HIGH_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *val = POWER_SUPPLY_CHARGE_TYPE_FAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) case MAX77693_CHARGING_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case MAX77693_CHARGING_TIMER_EXPIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) case MAX77693_CHARGING_THERMISTOR_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) case MAX77693_CHARGING_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) case MAX77693_CHARGING_OVER_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) case MAX77693_CHARGING_WATCHDOG_EXPIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) case MAX77693_CHARGING_RESERVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) *val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Supported health statuses:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * - POWER_SUPPLY_HEALTH_DEAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * - POWER_SUPPLY_HEALTH_GOOD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * - POWER_SUPPLY_HEALTH_UNKNOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int max77693_get_battery_health(struct regmap *regmap, int *val)
^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) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
^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) data &= CHG_DETAILS_01_BAT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) data >>= CHG_DETAILS_01_BAT_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) switch (data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) case MAX77693_BATTERY_NOBAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) *val = POWER_SUPPLY_HEALTH_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) case MAX77693_BATTERY_PREQUALIFICATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) case MAX77693_BATTERY_GOOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) case MAX77693_BATTERY_LOWVOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *val = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) case MAX77693_BATTERY_TIMER_EXPIRED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Took longer to charge than expected, charging suspended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Damaged battery?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) *val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) case MAX77693_BATTERY_OVERVOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) case MAX77693_BATTERY_OVERCURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *val = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) case MAX77693_BATTERY_RESERVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *val = POWER_SUPPLY_HEALTH_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static int max77693_get_present(struct regmap *regmap, int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * Read CHG_INT_OK register. High DETBAT bit here should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * equal to value 0x0 in CHG_DETAILS_01/BAT field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *val = (data & CHG_INT_OK_DETBAT_MASK) ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int max77693_get_online(struct regmap *regmap, int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) *val = (data & CHG_INT_OK_CHGIN_MASK) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static enum power_supply_property max77693_charger_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) POWER_SUPPLY_PROP_CHARGE_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) POWER_SUPPLY_PROP_MODEL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) POWER_SUPPLY_PROP_MANUFACTURER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int max77693_charger_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct max77693_charger *chg = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct regmap *regmap = chg->max77693->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ret = max77693_get_charger_state(regmap, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) case POWER_SUPPLY_PROP_CHARGE_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ret = max77693_get_charge_type(regmap, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) ret = max77693_get_battery_health(regmap, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ret = max77693_get_present(regmap, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ret = max77693_get_online(regmap, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) case POWER_SUPPLY_PROP_MODEL_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) val->strval = max77693_charger_model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) case POWER_SUPPLY_PROP_MANUFACTURER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) val->strval = max77693_charger_manufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) default:
^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) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static const struct power_supply_desc max77693_charger_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) .name = MAX77693_CHARGER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) .type = POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) .properties = max77693_charger_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .num_properties = ARRAY_SIZE(max77693_charger_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .get_property = max77693_charger_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static ssize_t device_attr_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct device_attribute *attr, const char *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int (*fn)(struct max77693_charger *, unsigned long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct max77693_charger *chg = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) ret = fn(chg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static ssize_t fast_charge_timer_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct max77693_charger *chg = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) unsigned int data, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) data &= CHG_CNFG_01_FCHGTIME_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) data >>= CHG_CNFG_01_FCHGTIME_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) switch (data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) case 0x1 ... 0x7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /* Starting from 4 hours, step by 2 hours */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) val = 4 + (data - 1) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) case 0x0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return scnprintf(buf, PAGE_SIZE, "%u\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int max77693_set_fast_charge_timer(struct max77693_charger *chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) unsigned long hours)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * 0x00 - disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * 0x01 - 4h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * 0x02 - 6h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * 0x07 - 16h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * Round down odd values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) switch (hours) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) case 4 ... 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) data = (hours - 4) / 2 + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* Disable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) data <<= CHG_CNFG_01_FCHGTIME_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return regmap_update_bits(chg->max77693->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) MAX77693_CHG_REG_CHG_CNFG_01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) CHG_CNFG_01_FCHGTIME_MASK, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static ssize_t fast_charge_timer_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return device_attr_store(dev, attr, buf, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) max77693_set_fast_charge_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static ssize_t top_off_threshold_current_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct max77693_charger *chg = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) unsigned int data, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) data &= CHG_CNFG_03_TOITH_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) data >>= CHG_CNFG_03_TOITH_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (data <= 0x04)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) val = 100000 + data * 25000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) val = data * 50000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return scnprintf(buf, PAGE_SIZE, "%u\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static int max77693_set_top_off_threshold_current(struct max77693_charger *chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) unsigned long uamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (uamp < 100000 || uamp > 350000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (uamp <= 200000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) data = (uamp - 100000) / 25000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /* (200000, 350000> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) data = uamp / 50000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) data <<= CHG_CNFG_03_TOITH_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return regmap_update_bits(chg->max77693->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) MAX77693_CHG_REG_CHG_CNFG_03,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) CHG_CNFG_03_TOITH_MASK, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) static ssize_t top_off_threshold_current_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return device_attr_store(dev, attr, buf, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) max77693_set_top_off_threshold_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static ssize_t top_off_timer_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct max77693_charger *chg = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) unsigned int data, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) data &= CHG_CNFG_03_TOTIME_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) data >>= CHG_CNFG_03_TOTIME_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) val = data * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return scnprintf(buf, PAGE_SIZE, "%u\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static int max77693_set_top_off_timer(struct max77693_charger *chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) unsigned long minutes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (minutes > 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) data = minutes / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) data <<= CHG_CNFG_03_TOTIME_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return regmap_update_bits(chg->max77693->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) MAX77693_CHG_REG_CHG_CNFG_03,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) CHG_CNFG_03_TOTIME_MASK, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static ssize_t top_off_timer_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return device_attr_store(dev, attr, buf, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) max77693_set_top_off_timer);
^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 DEVICE_ATTR_RW(fast_charge_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static DEVICE_ATTR_RW(top_off_threshold_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static DEVICE_ATTR_RW(top_off_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static int max77693_set_constant_volt(struct max77693_charger *chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) unsigned int uvolt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * 0x00 - 3.650 V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * 0x01 - 3.675 V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * 0x1b - 4.325 V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * 0x1c - 4.340 V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * 0x1d - 4.350 V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * 0x1e - 4.375 V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * 0x1f - 4.400 V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (uvolt >= 3650000 && uvolt < 4340000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) data = (uvolt - 3650000) / 25000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) else if (uvolt >= 4340000 && uvolt < 4350000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) data = 0x1c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) else if (uvolt >= 4350000 && uvolt <= 4400000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) data = 0x1d + (uvolt - 4350000) / 25000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) dev_err(chg->dev, "Wrong value for charging constant voltage\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return -EINVAL;
^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) data <<= CHG_CNFG_04_CHGCVPRM_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return regmap_update_bits(chg->max77693->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) MAX77693_CHG_REG_CHG_CNFG_04,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) CHG_CNFG_04_CHGCVPRM_MASK, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static int max77693_set_min_system_volt(struct max77693_charger *chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) unsigned int uvolt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (uvolt < 3000000 || uvolt > 3700000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) data = (uvolt - 3000000) / 100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) data <<= CHG_CNFG_04_MINVSYS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) uvolt, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return regmap_update_bits(chg->max77693->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) MAX77693_CHG_REG_CHG_CNFG_04,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) CHG_CNFG_04_MINVSYS_MASK, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) unsigned int cels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) switch (cels) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) case 70:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) case 85:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) case 100:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) case 115:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) data = (cels - 70) / 15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) data <<= CHG_CNFG_07_REGTEMP_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) cels, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) return regmap_update_bits(chg->max77693->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) MAX77693_CHG_REG_CHG_CNFG_07,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) CHG_CNFG_07_REGTEMP_MASK, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) unsigned int uamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (uamp && (uamp < 2000000 || uamp > 3500000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) dev_err(chg->dev, "Wrong value for battery overcurrent\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (uamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) data = ((uamp - 2000000) / 250000) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) data = 0; /* disable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) data <<= CHG_CNFG_12_B2SOVRC_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) return regmap_update_bits(chg->max77693->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) MAX77693_CHG_REG_CHG_CNFG_12,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) CHG_CNFG_12_B2SOVRC_MASK, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) unsigned int uvolt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) switch (uvolt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) case 4300000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) data = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) case 4700000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) case 4800000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) case 4900000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) data = (uvolt - 4700000) / 100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) data <<= CHG_CNFG_12_VCHGINREG_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) uvolt, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return regmap_update_bits(chg->max77693->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) MAX77693_CHG_REG_CHG_CNFG_12,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) CHG_CNFG_12_VCHGINREG_MASK, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * Sets charger registers to proper and safe default values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static int max77693_reg_init(struct max77693_charger *chg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) /* Unlock charger register protection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) data = (0x3 << CHG_CNFG_06_CHGPROT_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) ret = regmap_update_bits(chg->max77693->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) MAX77693_CHG_REG_CHG_CNFG_06,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) CHG_CNFG_06_CHGPROT_MASK, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) dev_err(chg->dev, "Error unlocking registers: %d\n", ret);
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) ret = max77693_set_top_off_threshold_current(chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) DEFAULT_TOP_OFF_THRESHOLD_CURRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) ret = max77693_set_constant_volt(chg, chg->constant_volt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (ret)
^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) ret = max77693_set_min_system_volt(chg, chg->min_system_volt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) ret = max77693_set_thermal_regulation_temp(chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) chg->thermal_regulation_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) return max77693_set_charge_input_threshold_volt(chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) chg->charge_input_threshold_volt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (!np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) dev_err(dev, "no charger OF node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (of_property_read_u32(np, "maxim,constant-microvolt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) &chg->constant_volt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) chg->constant_volt = DEFAULT_CONSTANT_VOLT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (of_property_read_u32(np, "maxim,min-system-microvolt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) &chg->min_system_volt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (of_property_read_u32(np, "maxim,thermal-regulation-celsius",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) &chg->thermal_regulation_temp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) &chg->batttery_overcurrent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) &chg->charge_input_threshold_volt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) chg->charge_input_threshold_volt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) #else /* CONFIG_OF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) #endif /* CONFIG_OF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static int max77693_charger_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) struct max77693_charger *chg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (!chg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) platform_set_drvdata(pdev, chg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) chg->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) chg->max77693 = max77693;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) ret = max77693_dt_init(&pdev->dev, chg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) ret = max77693_reg_init(chg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) psy_cfg.drv_data = chg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) ret = device_create_file(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) &dev_attr_top_off_threshold_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) dev_err(&pdev->dev, "failed: create top off current sysfs entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) chg->charger = power_supply_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) &max77693_charger_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (IS_ERR(chg->charger)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) dev_err(&pdev->dev, "failed: power supply register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) ret = PTR_ERR(chg->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) static int max77693_charger_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) struct max77693_charger *chg = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) power_supply_unregister(chg->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) return 0;
^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 platform_device_id max77693_charger_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) { "max77693-charger", 0, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) MODULE_DEVICE_TABLE(platform, max77693_charger_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) static struct platform_driver max77693_charger_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) .name = "max77693-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) .probe = max77693_charger_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) .remove = max77693_charger_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) .id_table = max77693_charger_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) module_platform_driver(max77693_charger_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) MODULE_DESCRIPTION("Maxim 77693 charger driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) MODULE_LICENSE("GPL");