Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Battery driver for CPCAP PMIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Some parts of the code based on earlie Motorola mapphone Linux kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * drivers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 2009-2010 Motorola, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * it under the terms of the GNU General Public License version 2 as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * kind, whether express or implied; without even the implied warranty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/iio/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/mfd/motorola-cpcap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * Register bit defines for CPCAP_REG_BPEOL. Some of these seem to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * map to MC13783UG.pdf "Table 5-19. Register 13, Power Control 0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * to enable BATTDETEN, LOBAT and EOL features. We currently use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * LOBAT interrupts instead of EOL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define CPCAP_REG_BPEOL_BIT_EOL9	BIT(9)	/* Set for EOL irq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define CPCAP_REG_BPEOL_BIT_EOL8	BIT(8)	/* Set for EOL irq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define CPCAP_REG_BPEOL_BIT_UNKNOWN7	BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define CPCAP_REG_BPEOL_BIT_UNKNOWN6	BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define CPCAP_REG_BPEOL_BIT_UNKNOWN5	BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define CPCAP_REG_BPEOL_BIT_EOL_MULTI	BIT(4)	/* Set for multiple EOL irqs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define CPCAP_REG_BPEOL_BIT_UNKNOWN3	BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define CPCAP_REG_BPEOL_BIT_UNKNOWN2	BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define CPCAP_REG_BPEOL_BIT_BATTDETEN	BIT(1)	/* Enable battery detect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define CPCAP_REG_BPEOL_BIT_EOLSEL	BIT(0)	/* BPDET = 0, EOL = 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * Register bit defines for CPCAP_REG_CCC1. These seem similar to the twl6030
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * coulomb counter registers rather than the mc13892 registers. Both twl6030
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * and mc13892 set bits 2 and 1 to reset and clear registers. But mc13892
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * sets bit 0 to start the coulomb counter while twl6030 sets bit 0 to stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * the coulomb counter like cpcap does. So for now, we use the twl6030 style
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * naming for the registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define CPCAP_REG_CCC1_ACTIVE_MODE1	BIT(4)	/* Update rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define CPCAP_REG_CCC1_ACTIVE_MODE0	BIT(3)	/* Update rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define CPCAP_REG_CCC1_AUTOCLEAR	BIT(2)	/* Resets sample registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define CPCAP_REG_CCC1_CAL_EN		BIT(1)	/* Clears after write in 1s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define CPCAP_REG_CCC1_PAUSE		BIT(0)	/* Stop counters, allow write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define CPCAP_REG_CCC1_RESET_MASK	(CPCAP_REG_CCC1_AUTOCLEAR | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 					 CPCAP_REG_CCC1_CAL_EN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define CPCAP_REG_CCCC2_RATE1		BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define CPCAP_REG_CCCC2_RATE0		BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define CPCAP_REG_CCCC2_ENABLE		BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS	250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	CPCAP_BATTERY_IIO_BATTDET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	CPCAP_BATTERY_IIO_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	CPCAP_BATTERY_IIO_CHRG_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	CPCAP_BATTERY_IIO_BATT_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	CPCAP_BATTERY_IIO_NR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) enum cpcap_battery_irq_action {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	CPCAP_BATTERY_IRQ_ACTION_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	CPCAP_BATTERY_IRQ_ACTION_CC_CAL_DONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	CPCAP_BATTERY_IRQ_ACTION_POWEROFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) struct cpcap_interrupt_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	enum cpcap_battery_irq_action action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) struct cpcap_battery_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	int cd_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct power_supply_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct power_supply_battery_info bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct cpcap_coulomb_counter_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	s32 sample;		/* 24 or 32 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	s32 accumulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	s16 offset;		/* 9 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	s16 integrator;		/* 13 or 16 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) enum cpcap_battery_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	CPCAP_BATTERY_STATE_PREVIOUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	CPCAP_BATTERY_STATE_LATEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	CPCAP_BATTERY_STATE_NR,
^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) struct cpcap_battery_state_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int current_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int counter_uah;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	ktime_t time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	struct cpcap_coulomb_counter_data cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct cpcap_battery_ddata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct regmap *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct list_head irq_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct iio_channel *channels[CPCAP_BATTERY_IIO_NR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct power_supply *psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct cpcap_battery_config config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct cpcap_battery_state_data state[CPCAP_BATTERY_STATE_NR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	u32 cc_lsb;		/* μAms per LSB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	atomic_t active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	u16 vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #define CPCAP_NO_BATTERY	-400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static struct cpcap_battery_state_data *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) cpcap_battery_get_state(struct cpcap_battery_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			enum cpcap_battery_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (state >= CPCAP_BATTERY_STATE_NR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return &ddata->state[state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static struct cpcap_battery_state_data *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) cpcap_battery_latest(struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_LATEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static struct cpcap_battery_state_data *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) cpcap_battery_previous(struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_PREVIOUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int cpcap_charger_battery_temperature(struct cpcap_battery_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 					     int *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct iio_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	channel = ddata->channels[CPCAP_BATTERY_IIO_BATTDET];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	error = iio_read_channel_processed(channel, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		*value = CPCAP_NO_BATTERY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	*value /= 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int cpcap_battery_get_voltage(struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct iio_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int error, value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	channel = ddata->channels[CPCAP_BATTERY_IIO_VOLTAGE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	error = iio_read_channel_processed(channel, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return value * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int cpcap_battery_get_current(struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct iio_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	int error, value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	channel = ddata->channels[CPCAP_BATTERY_IIO_BATT_CURRENT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	error = iio_read_channel_processed(channel, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		return 0;
^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) 	return value * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  * cpcap_battery_cc_raw_div - calculate and divide coulomb counter μAms values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  * @ddata: device driver data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  * @sample: coulomb counter sample value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * @accumulator: coulomb counter integrator value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  * @offset: coulomb counter offset value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * @divider: conversion divider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * Note that cc_lsb and cc_dur values are from Motorola Linux kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  * function data_get_avg_curr_ua() and seem to be based on measured test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  * results. It also has the following comment:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * Adjustment factors are applied here as a temp solution per the test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * results. Need to work out a formal solution for this adjustment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * A coulomb counter for similar hardware seems to be documented in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * "TWL6030 Gas Gauging Basics (Rev. A)" swca095a.pdf in chapter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  * "10 Calculating Accumulated Current". We however follow what the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * Motorola mapphone Linux kernel is doing as there may be either a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * TI or ST coulomb counter in the PMIC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				    s32 sample, s32 accumulator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 				    s16 offset, u32 divider)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	s64 acc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (!divider)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	acc = accumulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	acc -= (s64)sample * offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	acc *= ddata->cc_lsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	acc *= -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	acc = div_s64(acc, divider);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return acc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /* 3600000μAms = 1μAh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int cpcap_battery_cc_to_uah(struct cpcap_battery_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 				   s32 sample, s32 accumulator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 				   s16 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	return cpcap_battery_cc_raw_div(ddata, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 					accumulator, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 					3600000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				  s32 sample, s32 accumulator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				  s16 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return cpcap_battery_cc_raw_div(ddata, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 					accumulator, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 					sample *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 					CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS);
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * cpcap_battery_read_accumulated - reads cpcap coulomb counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  * @ddata: device driver data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  * @ccd: coulomb counter values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)  * Based on Motorola mapphone kernel function data_read_regs().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  * Looking at the registers, the coulomb counter seems similar to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  * the coulomb counter in TWL6030. See "TWL6030 Gas Gauging Basics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  * (Rev. A) swca095a.pdf for "10 Calculating Accumulated Current".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  * Note that swca095a.pdf instructs to stop the coulomb counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)  * before reading to avoid values changing. Motorola mapphone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)  * Linux kernel does not do it, so let's assume they've verified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * the data produced is correct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) cpcap_battery_read_accumulated(struct cpcap_battery_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			       struct cpcap_coulomb_counter_data *ccd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	u16 buf[7];	/* CPCAP_REG_CCS1 to CCI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	ccd->sample = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	ccd->accumulator = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	ccd->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	ccd->integrator = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	/* Read coulomb counter register range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	error = regmap_bulk_read(ddata->reg, CPCAP_REG_CCS1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				 buf, ARRAY_SIZE(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	/* Sample value CPCAP_REG_CCS1 & 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	ccd->sample = (buf[1] & 0x0fff) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	ccd->sample |= buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (ddata->vendor == CPCAP_VENDOR_TI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		ccd->sample = sign_extend32(24, ccd->sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	/* Accumulator value CPCAP_REG_CCA1 & 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	ccd->accumulator = ((s16)buf[3]) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	ccd->accumulator |= buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	 * Coulomb counter calibration offset is CPCAP_REG_CCM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	 * REG_CCO seems unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	ccd->offset = buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	ccd->offset = sign_extend32(ccd->offset, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	/* Integrator register CPCAP_REG_CCI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (ddata->vendor == CPCAP_VENDOR_TI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		ccd->integrator = sign_extend32(buf[6], 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		ccd->integrator = (s16)buf[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	return cpcap_battery_cc_to_uah(ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				       ccd->sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 				       ccd->accumulator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 				       ccd->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  * cpcap_battery_cc_get_avg_current - read cpcap coulumb counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  * @ddata: cpcap battery driver device data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static int cpcap_battery_cc_get_avg_current(struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	int value, acc, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	s32 sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	s16 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	/* Coulomb counter integrator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	error = regmap_read(ddata->reg, CPCAP_REG_CCI, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	if (ddata->vendor == CPCAP_VENDOR_TI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		acc = sign_extend32(value, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		sample = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		acc = (s16)value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		sample = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	/* Coulomb counter calibration offset  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	offset = sign_extend32(value, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	return cpcap_battery_cc_to_ua(ddata, sample, acc, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (state->voltage >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	    (ddata->config.bat.constant_charge_voltage_max_uv - 18000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	struct cpcap_battery_state_data state, *latest, *previous;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	memset(&state, 0, sizeof(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	now = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	latest = cpcap_battery_latest(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (latest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		s64 delta_ms = ktime_to_ms(ktime_sub(now, latest->time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (delta_ms < CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			return delta_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	state.time = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	state.voltage = cpcap_battery_get_voltage(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	state.current_ua = cpcap_battery_get_current(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	state.counter_uah = cpcap_battery_read_accumulated(ddata, &state.cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	error = cpcap_charger_battery_temperature(ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 						  &state.temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	previous = cpcap_battery_previous(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	memcpy(previous, latest, sizeof(*previous));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	memcpy(latest, &state, sizeof(*latest));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static enum power_supply_property cpcap_battery_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	POWER_SUPPLY_PROP_TECHNOLOGY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	POWER_SUPPLY_PROP_CURRENT_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	POWER_SUPPLY_PROP_POWER_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	POWER_SUPPLY_PROP_POWER_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	POWER_SUPPLY_PROP_SCOPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static int cpcap_battery_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 				      enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 				      union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	struct cpcap_battery_state_data *latest, *previous;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	u32 sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	s32 accumulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	int cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	s64 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	cached = cpcap_battery_update_status(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (cached < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	latest = cpcap_battery_latest(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	previous = cpcap_battery_previous(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		if (latest->temperature > CPCAP_NO_BATTERY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			val->intval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		if (cpcap_battery_full(ddata)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		if (cpcap_battery_cc_get_avg_current(ddata) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	case POWER_SUPPLY_PROP_TECHNOLOGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		val->intval = ddata->config.info.technology;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		val->intval = cpcap_battery_get_voltage(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		val->intval = ddata->config.info.voltage_max_design;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		val->intval = ddata->config.info.voltage_min_design;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		val->intval = ddata->config.bat.constant_charge_voltage_max_uv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	case POWER_SUPPLY_PROP_CURRENT_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		sample = latest->cc.sample - previous->cc.sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		if (!sample) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			val->intval = cpcap_battery_cc_get_avg_current(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		accumulator = latest->cc.accumulator - previous->cc.accumulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		val->intval = cpcap_battery_cc_to_ua(ddata, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 						     accumulator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 						     latest->cc.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		val->intval = latest->current_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		val->intval = latest->counter_uah;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	case POWER_SUPPLY_PROP_POWER_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		tmp = (latest->voltage / 10000) * latest->current_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		val->intval = div64_s64(tmp, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	case POWER_SUPPLY_PROP_POWER_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		sample = latest->cc.sample - previous->cc.sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		if (!sample) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			tmp = cpcap_battery_cc_get_avg_current(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			tmp *= (latest->voltage / 10000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 			val->intval = div64_s64(tmp, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		accumulator = latest->cc.accumulator - previous->cc.accumulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		tmp = cpcap_battery_cc_to_ua(ddata, sample, accumulator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 					     latest->cc.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		tmp *= ((latest->voltage + previous->voltage) / 20000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		val->intval = div64_s64(tmp, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		if (cpcap_battery_full(ddata))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		else if (latest->voltage >= 3750000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		else if (latest->voltage >= 3300000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		else if (latest->voltage > 3100000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		else if (latest->voltage <= 3100000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		val->intval = ddata->config.info.charge_full_design;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	case POWER_SUPPLY_PROP_SCOPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		val->intval = latest->temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) static int cpcap_battery_update_charger(struct cpcap_battery_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 					int const_charge_voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	union power_supply_propval prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	struct power_supply *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	charger = power_supply_get_by_name("usb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	if (!charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	error = power_supply_get_property(charger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 				POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 				&prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	/* Allow charger const voltage lower than battery const voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if (const_charge_voltage > prop.intval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	val.intval = const_charge_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	error = power_supply_set_property(charger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			&val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	power_supply_put(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) static int cpcap_battery_set_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 				      enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 				      const union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		if (val->intval < ddata->config.info.voltage_min_design)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		if (val->intval > ddata->config.info.voltage_max_design)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		ddata->config.bat.constant_charge_voltage_max_uv = val->intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		return cpcap_battery_update_charger(ddata, val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) static int cpcap_battery_property_is_writeable(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 					       enum power_supply_property psp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	struct cpcap_battery_ddata *ddata = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	struct cpcap_battery_state_data *latest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	struct cpcap_interrupt_desc *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	if (!atomic_read(&ddata->active))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	list_for_each_entry(d, &ddata->irq_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		if (irq == d->irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	if (list_entry_is_head(d, &ddata->irq_list, node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	latest = cpcap_battery_latest(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	switch (d->action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	case CPCAP_BATTERY_IRQ_ACTION_CC_CAL_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		dev_info(ddata->dev, "Coulomb counter calibration done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		if (latest->current_ua >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 			dev_warn(ddata->dev, "Battery low at %imV!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 				latest->voltage / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	case CPCAP_BATTERY_IRQ_ACTION_POWEROFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		if (latest->current_ua >= 0 && latest->voltage <= 3200000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			dev_emerg(ddata->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 				  "Battery empty at %imV, powering off\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 				  latest->voltage / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 			orderly_poweroff(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	power_supply_changed(ddata->psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static int cpcap_battery_init_irq(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 				  struct cpcap_battery_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 				  const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	struct cpcap_interrupt_desc *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	int irq, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	irq = platform_get_irq_byname(pdev, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		return irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	error = devm_request_threaded_irq(ddata->dev, irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 					  cpcap_battery_irq_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 					  IRQF_SHARED | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 					  name, ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		dev_err(ddata->dev, "could not get irq %s: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 			name, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	d->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	d->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	if (!strncmp(name, "cccal", 5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		d->action = CPCAP_BATTERY_IRQ_ACTION_CC_CAL_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	else if (!strncmp(name, "lowbph", 6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		d->action = CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	else if (!strncmp(name, "lowbpl", 6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		d->action = CPCAP_BATTERY_IRQ_ACTION_POWEROFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	list_add(&d->node, &ddata->irq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) static int cpcap_battery_init_interrupts(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 					 struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	static const char * const cpcap_battery_irqs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		"eol", "lowbph", "lowbpl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		"chrgcurr1", "battdetb"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	int i, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	for (i = 0; i < ARRAY_SIZE(cpcap_battery_irqs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		error = cpcap_battery_init_irq(pdev, ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 					       cpcap_battery_irqs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	/* Enable calibration interrupt if already available in dts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	cpcap_battery_init_irq(pdev, ddata, "cccal");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	/* Enable low battery interrupts for 3.3V high and 3.1V low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 				   0xffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 				   CPCAP_REG_BPEOL_BIT_BATTDETEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) static int cpcap_battery_init_iio(struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	const char * const names[CPCAP_BATTERY_IIO_NR] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		"battdetb", "battp", "chg_isense", "batti",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	int error, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	for (i = 0; i < CPCAP_BATTERY_IIO_NR; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		ddata->channels[i] = devm_iio_channel_get(ddata->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 							  names[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		if (IS_ERR(ddata->channels[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 			error = PTR_ERR(ddata->channels[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		if (!ddata->channels[i]->indio_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 			error = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	return dev_err_probe(ddata->dev, error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 			     "could not initialize VBUS or ID IIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) /* Calibrate coulomb counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) static int cpcap_battery_calibrate(struct cpcap_battery_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	int error, ccc1, value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	error = regmap_read(ddata->reg, CPCAP_REG_CCC1, &ccc1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	timeout = jiffies + msecs_to_jiffies(6000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	/* Start calibration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	error = regmap_update_bits(ddata->reg, CPCAP_REG_CCC1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 				   0xffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 				   CPCAP_REG_CCC1_CAL_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 		goto restore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	while (time_before(jiffies, timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 		error = regmap_read(ddata->reg, CPCAP_REG_CCC1, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 			goto restore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 		if (!(value & CPCAP_REG_CCC1_CAL_EN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 		error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 			goto restore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 		msleep(300);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	/* Read calibration offset from CCM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 		goto restore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	dev_info(ddata->dev, "calibration done: 0x%04x\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) restore:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 		dev_err(ddata->dev, "%s: error %i\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	error = regmap_update_bits(ddata->reg, CPCAP_REG_CCC1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 				   0xffff, ccc1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 		dev_err(ddata->dev, "%s: restore error %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 			__func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)  * Based on the values from Motorola mapphone Linux kernel. In the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)  * the Motorola mapphone Linux kernel tree the value for pm_cd_factor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)  * is passed to the kernel via device tree. If it turns out to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)  * something device specific we can consider that too later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)  * And looking at the battery full and shutdown values for the stock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)  * kernel on droid 4, full is 4351000 and software initiates shutdown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)  * at 3078000. The device will die around 2743000.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) static const struct cpcap_battery_config cpcap_battery_default_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	.cd_factor = 0x3cc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	.info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	.info.voltage_max_design = 4351000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	.info.voltage_min_design = 3100000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	.info.charge_full_design = 1740000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	.bat.constant_charge_voltage_max_uv = 4200000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) static const struct of_device_id cpcap_battery_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 		.compatible = "motorola,cpcap-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 		.data = &cpcap_battery_default_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) MODULE_DEVICE_TABLE(of, cpcap_battery_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) static int cpcap_battery_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	struct power_supply_desc *psy_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	struct cpcap_battery_ddata *ddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	const struct of_device_id *match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	match = of_match_device(of_match_ptr(cpcap_battery_id_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 				&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	if (!match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	if (!match->data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 		dev_err(&pdev->dev, "no configuration data found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	if (!ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	INIT_LIST_HEAD(&ddata->irq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	ddata->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	memcpy(&ddata->config, match->data, sizeof(ddata->config));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	if (!ddata->reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 	switch (ddata->vendor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	case CPCAP_VENDOR_ST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 		ddata->cc_lsb = 95374;	/* μAms per LSB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 	case CPCAP_VENDOR_TI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 		ddata->cc_lsb = 91501;	/* μAms per LSB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 	ddata->cc_lsb = (ddata->cc_lsb * ddata->config.cd_factor) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	platform_set_drvdata(pdev, ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	error = cpcap_battery_init_interrupts(pdev, ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 	error = cpcap_battery_init_iio(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	psy_desc = devm_kzalloc(ddata->dev, sizeof(*psy_desc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	if (!psy_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	psy_desc->name = "battery";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	psy_desc->properties = cpcap_battery_props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	psy_desc->num_properties = ARRAY_SIZE(cpcap_battery_props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 	psy_desc->get_property = cpcap_battery_get_property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 	psy_desc->set_property = cpcap_battery_set_property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	psy_desc->property_is_writeable = cpcap_battery_property_is_writeable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	psy_cfg.of_node = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 	psy_cfg.drv_data = ddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	ddata->psy = devm_power_supply_register(ddata->dev, psy_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 						&psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 	error = PTR_ERR_OR_ZERO(ddata->psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 		dev_err(ddata->dev, "failed to register power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	atomic_set(&ddata->active, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	error = cpcap_battery_calibrate(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) static int cpcap_battery_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 	struct cpcap_battery_ddata *ddata = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 	atomic_set(&ddata->active, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 				   0xffff, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 		dev_err(&pdev->dev, "could not disable: %i\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) static struct platform_driver cpcap_battery_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 	.driver	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 		.name		= "cpcap_battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 		.of_match_table = of_match_ptr(cpcap_battery_id_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) 	.probe	= cpcap_battery_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 	.remove = cpcap_battery_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) module_platform_driver(cpcap_battery_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) MODULE_DESCRIPTION("CPCAP PMIC Battery Driver");