^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) * Battery driver for Maxim MAX8925
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2009-2010 Marvell International Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Haojian Zhuang <haojian.zhuang@marvell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/err.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/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mfd/max8925.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* registers in GPM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define MAX8925_OUT5VEN 0x54
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define MAX8925_OUT3VEN 0x58
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define MAX8925_CHG_CNTL1 0x7c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* bits definition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define MAX8925_CHG_STAT_VSYSLOW (1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define MAX8925_CHG_STAT_MODE_MASK (3 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define MAX8925_CHG_STAT_EN_MASK (1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define MAX8925_CHG_MBDET (1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define MAX8925_CHG_AC_RANGE_MASK (3 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* registers in ADC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define MAX8925_ADC_RES_CNFG1 0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define MAX8925_ADC_AVG_CNFG1 0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define MAX8925_ADC_ACQ_CNFG1 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define MAX8925_ADC_ACQ_CNFG2 0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* 2 bytes registers in below. MSB is 1st, LSB is 2nd. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define MAX8925_ADC_AUX2 0x62
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define MAX8925_ADC_VCHG 0x64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define MAX8925_ADC_VBBATT 0x66
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define MAX8925_ADC_VMBATT 0x68
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define MAX8925_ADC_ISNS 0x6a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define MAX8925_ADC_THM 0x6c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define MAX8925_ADC_TDIE 0x6e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define MAX8925_CMD_AUX2 0xc8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define MAX8925_CMD_VCHG 0xd0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define MAX8925_CMD_VBBATT 0xd8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define MAX8925_CMD_VMBATT 0xe0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define MAX8925_CMD_ISNS 0xe8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define MAX8925_CMD_THM 0xf0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define MAX8925_CMD_TDIE 0xf8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) MEASURE_AUX2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) MEASURE_VCHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) MEASURE_VBBATT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) MEASURE_VMBATT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) MEASURE_ISNS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) MEASURE_THM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) MEASURE_TDIE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) MEASURE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct max8925_power_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct max8925_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct i2c_client *gpm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct i2c_client *adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct power_supply *ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct power_supply *usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct power_supply *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int irq_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned ac_online:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned usb_online:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned bat_online:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned chg_mode:2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) unsigned batt_detect:1; /* detecing MB by ID pin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned topoff_threshold:2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unsigned fast_charge:3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned no_temp_support:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned no_insert_detect:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int (*set_charger) (int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int __set_charger(struct max8925_power_info *info, int enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct max8925_chip *chip = info->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* enable charger in platform */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (info->set_charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) info->set_charger(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* enable charger */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 1 << 7, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* disable charge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 1 << 7, 1 << 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (info->set_charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) info->set_charger(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) dev_dbg(chip->dev, "%s\n", (enable) ? "Enable charger"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) : "Disable charger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static irqreturn_t max8925_charger_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct max8925_power_info *info = (struct max8925_power_info *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct max8925_chip *chip = info->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) switch (irq - chip->irq_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) case MAX8925_IRQ_VCHG_DC_R:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) info->ac_online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) __set_charger(info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) dev_dbg(chip->dev, "Adapter inserted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) case MAX8925_IRQ_VCHG_DC_F:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) info->ac_online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) __set_charger(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) dev_dbg(chip->dev, "Adapter removed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) case MAX8925_IRQ_VCHG_THM_OK_F:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* Battery is not ready yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) dev_dbg(chip->dev, "Battery temperature is out of range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) case MAX8925_IRQ_VCHG_DC_OVP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) dev_dbg(chip->dev, "Error detection\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) __set_charger(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) case MAX8925_IRQ_VCHG_THM_OK_R:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* Battery is ready now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dev_dbg(chip->dev, "Battery temperature is in range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) case MAX8925_IRQ_VCHG_SYSLOW_R:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* VSYS is low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dev_info(chip->dev, "Sys power is too low\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) case MAX8925_IRQ_VCHG_SYSLOW_F:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) dev_dbg(chip->dev, "Sys power is above low threshold\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) case MAX8925_IRQ_VCHG_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) __set_charger(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) dev_dbg(chip->dev, "Charging is done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) case MAX8925_IRQ_VCHG_TOPOFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) dev_dbg(chip->dev, "Charging in top-off mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) case MAX8925_IRQ_VCHG_TMR_FAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) __set_charger(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dev_dbg(chip->dev, "Safe timer is expired\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) case MAX8925_IRQ_VCHG_RST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) __set_charger(info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) dev_dbg(chip->dev, "Charger is reset\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return IRQ_HANDLED;
^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 start_measure(struct max8925_power_info *info, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) unsigned char buf[2] = {0, 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int meas_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int meas_reg = 0, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) case MEASURE_VCHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) meas_cmd = MAX8925_CMD_VCHG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) meas_reg = MAX8925_ADC_VCHG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) case MEASURE_VBBATT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) meas_cmd = MAX8925_CMD_VBBATT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) meas_reg = MAX8925_ADC_VBBATT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) case MEASURE_VMBATT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) meas_cmd = MAX8925_CMD_VMBATT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) meas_reg = MAX8925_ADC_VMBATT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) case MEASURE_ISNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) meas_cmd = MAX8925_CMD_ISNS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) meas_reg = MAX8925_ADC_ISNS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return -EINVAL;
^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) max8925_reg_write(info->adc, meas_cmd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) max8925_bulk_read(info->adc, meas_reg, 2, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ret = ((buf[0]<<8) | buf[1]) >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int max8925_ac_get_prop(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct max8925_power_info *info = dev_get_drvdata(psy->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) val->intval = info->ac_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (info->ac_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) ret = start_measure(info, MEASURE_VCHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) val->intval = ret * 2000; /* unit is uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static enum power_supply_property max8925_ac_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static int max8925_usb_get_prop(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct max8925_power_info *info = dev_get_drvdata(psy->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) val->intval = info->usb_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (info->usb_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) ret = start_measure(info, MEASURE_VCHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) val->intval = ret * 2000; /* unit is uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static enum power_supply_property max8925_usb_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int max8925_bat_get_prop(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct max8925_power_info *info = dev_get_drvdata(psy->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) val->intval = info->bat_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (info->bat_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) ret = start_measure(info, MEASURE_VMBATT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) val->intval = ret * 2000; /* unit is uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (info->bat_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ret = start_measure(info, MEASURE_ISNS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /* assume r_sns is 0.02 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) ret = ((ret * 6250) - 3125) /* uA */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) val->intval = ret; /* unit is mA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) case POWER_SUPPLY_PROP_CHARGE_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!info->bat_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ret = max8925_reg_read(info->gpm, MAX8925_CHG_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ret = (ret & MAX8925_CHG_STAT_MODE_MASK) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (!info->bat_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ret = max8925_reg_read(info->gpm, MAX8925_CHG_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (info->usb_online || info->ac_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (ret & MAX8925_CHG_STAT_EN_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return ret;
^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 enum power_supply_property max8925_battery_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) POWER_SUPPLY_PROP_CHARGE_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static const struct power_supply_desc ac_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) .name = "max8925-ac",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) .type = POWER_SUPPLY_TYPE_MAINS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) .properties = max8925_ac_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .num_properties = ARRAY_SIZE(max8925_ac_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) .get_property = max8925_ac_get_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static const struct power_supply_desc usb_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) .name = "max8925-usb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) .type = POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) .properties = max8925_usb_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) .num_properties = ARRAY_SIZE(max8925_usb_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) .get_property = max8925_usb_get_prop,
^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 const struct power_supply_desc battery_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) .name = "max8925-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) .type = POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) .properties = max8925_battery_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) .num_properties = ARRAY_SIZE(max8925_battery_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) .get_property = max8925_bat_get_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) #define REQUEST_IRQ(_irq, _name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ret = request_threaded_irq(chip->irq_base + _irq, NULL, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) max8925_charger_handler, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) IRQF_ONESHOT, _name, info); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (ret) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) dev_err(chip->dev, "Failed to request IRQ #%d: %d\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) _irq, ret); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static int max8925_init_charger(struct max8925_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct max8925_power_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_OVP, "ac-ovp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (!info->no_insert_detect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_F, "ac-remove");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_R, "ac-insert");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (!info->no_temp_support) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_R, "batt-temp-in-range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_F, "batt-temp-out-range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_F, "vsys-high");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_R, "vsys-low");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) REQUEST_IRQ(MAX8925_IRQ_VCHG_RST, "charger-reset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) REQUEST_IRQ(MAX8925_IRQ_VCHG_DONE, "charger-done");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) REQUEST_IRQ(MAX8925_IRQ_VCHG_TOPOFF, "charger-topoff");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) REQUEST_IRQ(MAX8925_IRQ_VCHG_TMR_FAULT, "charger-timer-expire");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) info->usb_online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) info->bat_online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /* check for power - can miss interrupt at boot time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (start_measure(info, MEASURE_VCHG) * 2000 > 500000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) info->ac_online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) info->ac_online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ret = max8925_reg_read(info->gpm, MAX8925_CHG_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * If battery detection is enabled, ID pin of battery is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * connected to MBDET pin of MAX8925. It could be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * detect battery presence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * Otherwise, we have to assume that battery is always on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (info->batt_detect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) info->bat_online = (ret & MAX8925_CHG_MBDET) ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) info->bat_online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (ret & MAX8925_CHG_AC_RANGE_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) info->ac_online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) info->ac_online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /* disable charge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 1 << 7, 1 << 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /* set charging current in charge topoff mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 3 << 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) info->topoff_threshold << 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /* set charing current in fast charge mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 7, info->fast_charge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static int max8925_deinit_charger(struct max8925_power_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) struct max8925_chip *chip = info->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) irq = chip->irq_base + MAX8925_IRQ_VCHG_DC_OVP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) for (; irq <= chip->irq_base + MAX8925_IRQ_VCHG_TMR_FAULT; irq++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) free_irq(irq, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static struct max8925_power_pdata *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) max8925_power_dt_init(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct device_node *nproot = pdev->dev.parent->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) int batt_detect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) int topoff_threshold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) int fast_charge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int no_temp_support;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) int no_insert_detect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) struct max8925_power_pdata *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (!nproot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) return pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) np = of_get_child_by_name(nproot, "charger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (!np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) dev_err(&pdev->dev, "failed to find charger node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return NULL;
^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) pdata = devm_kzalloc(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) sizeof(struct max8925_power_pdata),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) goto ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) of_property_read_u32(np, "topoff-threshold", &topoff_threshold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) of_property_read_u32(np, "batt-detect", &batt_detect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) of_property_read_u32(np, "fast-charge", &fast_charge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) of_property_read_u32(np, "no-insert-detect", &no_insert_detect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) of_property_read_u32(np, "no-temp-support", &no_temp_support);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) pdata->batt_detect = batt_detect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) pdata->fast_charge = fast_charge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) pdata->topoff_threshold = topoff_threshold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) pdata->no_insert_detect = no_insert_detect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) pdata->no_temp_support = no_temp_support;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static struct max8925_power_pdata *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) max8925_power_dt_init(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) return pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) static int max8925_power_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) struct power_supply_config psy_cfg = {}; /* Only for ac and usb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct max8925_power_pdata *pdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct max8925_power_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) pdata = max8925_power_dt_init(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) dev_err(&pdev->dev, "platform data isn't assigned to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) "power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_power_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) info->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) info->gpm = chip->i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) info->adc = chip->adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) platform_set_drvdata(pdev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) psy_cfg.supplied_to = pdata->supplied_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) psy_cfg.num_supplicants = pdata->num_supplicants;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) info->ac = power_supply_register(&pdev->dev, &ac_desc, &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (IS_ERR(info->ac)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) ret = PTR_ERR(info->ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) info->ac->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) info->usb = power_supply_register(&pdev->dev, &usb_desc, &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (IS_ERR(info->usb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) ret = PTR_ERR(info->usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) goto out_unregister_ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) info->usb->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) info->battery = power_supply_register(&pdev->dev, &battery_desc, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (IS_ERR(info->battery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) ret = PTR_ERR(info->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) goto out_unregister_usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) info->battery->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) info->batt_detect = pdata->batt_detect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) info->topoff_threshold = pdata->topoff_threshold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) info->fast_charge = pdata->fast_charge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) info->set_charger = pdata->set_charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) info->no_temp_support = pdata->no_temp_support;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) info->no_insert_detect = pdata->no_insert_detect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) max8925_init_charger(chip, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) out_unregister_usb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) power_supply_unregister(info->usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) out_unregister_ac:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) power_supply_unregister(info->ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static int max8925_power_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) struct max8925_power_info *info = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) power_supply_unregister(info->ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) power_supply_unregister(info->usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) power_supply_unregister(info->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) max8925_deinit_charger(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static struct platform_driver max8925_power_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) .probe = max8925_power_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) .remove = max8925_power_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) .name = "max8925-power",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) module_platform_driver(max8925_power_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) MODULE_DESCRIPTION("Power supply driver for MAX8925");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) MODULE_ALIAS("platform:max8925-power");