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 charger driver for TI BQ24735
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * the Free Software Foundation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * This program is distributed in the hope that it will be useful, but WITHOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * You should have received a copy of the GNU General Public License along
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * with this program; if not, write to the Free Software Foundation, Inc.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/power/bq24735-charger.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define BQ24735_CHG_OPT			0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define BQ24735_CHG_OPT_CHARGE_DISABLE	(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define BQ24735_CHG_OPT_AC_PRESENT	(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define BQ24735_CHARGE_CURRENT		0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define BQ24735_CHARGE_CURRENT_MASK	0x1fc0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define BQ24735_CHARGE_VOLTAGE		0x15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define BQ24735_CHARGE_VOLTAGE_MASK	0x7ff0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define BQ24735_INPUT_CURRENT		0x3f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define BQ24735_INPUT_CURRENT_MASK	0x1f80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define BQ24735_MANUFACTURER_ID		0xfe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define BQ24735_DEVICE_ID		0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) struct bq24735 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct power_supply		*charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct power_supply_desc	charger_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct i2c_client		*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct bq24735_platform		*pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct mutex			lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct gpio_desc		*status_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct delayed_work		poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u32				poll_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	bool				charging;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static inline struct bq24735 *to_bq24735(struct power_supply *psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	return power_supply_get_drvdata(psy);
^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) static enum power_supply_property bq24735_charger_properties[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static int bq24735_charger_property_is_writeable(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 						 enum power_supply_property psp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static inline int bq24735_write_word(struct i2c_client *client, u8 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 				     u16 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return i2c_smbus_write_word_data(client, reg, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static inline int bq24735_read_word(struct i2c_client *client, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return i2c_smbus_read_word_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static int bq24735_update_word(struct i2c_client *client, u8 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			       u16 mask, u16 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	unsigned int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	ret = bq24735_read_word(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	tmp = ret & ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	tmp |= value & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return bq24735_write_word(client, reg, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int bq24735_config_charger(struct bq24735 *charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct bq24735_platform *pdata = charger->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	u16 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (pdata->ext_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (pdata->charge_current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		value = pdata->charge_current & BQ24735_CHARGE_CURRENT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		ret = bq24735_write_word(charger->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 					 BQ24735_CHARGE_CURRENT, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			dev_err(&charger->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 				"Failed to write charger current : %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (pdata->charge_voltage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		value = pdata->charge_voltage & BQ24735_CHARGE_VOLTAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		ret = bq24735_write_word(charger->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 					 BQ24735_CHARGE_VOLTAGE, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			dev_err(&charger->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				"Failed to write charger voltage : %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (pdata->input_current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		value = pdata->input_current & BQ24735_INPUT_CURRENT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		ret = bq24735_write_word(charger->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 					 BQ24735_INPUT_CURRENT, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			dev_err(&charger->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				"Failed to write input current : %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return 0;
^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 inline int bq24735_enable_charging(struct bq24735 *charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (charger->pdata->ext_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	ret = bq24735_config_charger(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				   BQ24735_CHG_OPT_CHARGE_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static inline int bq24735_disable_charging(struct bq24735 *charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (charger->pdata->ext_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				   BQ24735_CHG_OPT_CHARGE_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				   BQ24735_CHG_OPT_CHARGE_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static bool bq24735_charger_is_present(struct bq24735 *charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (charger->status_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return !gpiod_get_value_cansleep(charger->status_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		int ac = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		ac = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		if (ac < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			dev_dbg(&charger->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 				"Failed to read charger options : %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return (ac & BQ24735_CHG_OPT_AC_PRESENT) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int bq24735_charger_is_charging(struct bq24735 *charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (!bq24735_charger_is_present(charger))
^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) 	ret  = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return !(ret & BQ24735_CHG_OPT_CHARGE_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void bq24735_update(struct bq24735 *charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	mutex_lock(&charger->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (charger->charging && bq24735_charger_is_present(charger))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		bq24735_enable_charging(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		bq24735_disable_charging(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	mutex_unlock(&charger->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	power_supply_changed(charger->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static irqreturn_t bq24735_charger_isr(int irq, void *devid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct power_supply *psy = devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	struct bq24735 *charger = to_bq24735(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	bq24735_update(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static void bq24735_poll(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	struct bq24735 *charger = container_of(work, struct bq24735, poll.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	bq24735_update(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	schedule_delayed_work(&charger->poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			      msecs_to_jiffies(charger->poll_interval));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static int bq24735_charger_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 					enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 					union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct bq24735 *charger = to_bq24735(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		val->intval = bq24735_charger_is_present(charger) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		switch (bq24735_charger_is_charging(charger)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int bq24735_charger_set_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 					enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 					const union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct bq24735 *charger = to_bq24735(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		switch (val->intval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		case POWER_SUPPLY_STATUS_CHARGING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			mutex_lock(&charger->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			charger->charging = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			ret = bq24735_enable_charging(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			mutex_unlock(&charger->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		case POWER_SUPPLY_STATUS_DISCHARGING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		case POWER_SUPPLY_STATUS_NOT_CHARGING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			mutex_lock(&charger->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			charger->charging = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			ret = bq24735_disable_charging(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			mutex_unlock(&charger->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		power_supply_changed(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return -EPERM;
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static struct bq24735_platform *bq24735_parse_dt_data(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	struct bq24735_platform *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	struct device_node *np = client->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			"Memory alloc for bq24735 pdata failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	ret = of_property_read_u32(np, "ti,charge-current", &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		pdata->charge_current = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	ret = of_property_read_u32(np, "ti,charge-voltage", &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		pdata->charge_voltage = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	ret = of_property_read_u32(np, "ti,input-current", &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		pdata->input_current = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	pdata->ext_control = of_property_read_bool(np, "ti,external-control");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int bq24735_charger_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 				 const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	struct bq24735 *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct power_supply_desc *supply_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	charger = devm_kzalloc(&client->dev, sizeof(*charger), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (!charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	mutex_init(&charger->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	charger->charging = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	charger->pdata = client->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (IS_ENABLED(CONFIG_OF) && !charger->pdata && client->dev.of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		charger->pdata = bq24735_parse_dt_data(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (!charger->pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		dev_err(&client->dev, "no platform data provided\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	name = (char *)charger->pdata->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		name = devm_kasprintf(&client->dev, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 				      "bq24735@%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 				      dev_name(&client->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			dev_err(&client->dev, "Failed to alloc device name\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	charger->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	supply_desc = &charger->charger_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	supply_desc->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	supply_desc->type = POWER_SUPPLY_TYPE_MAINS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	supply_desc->properties = bq24735_charger_properties;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	supply_desc->num_properties = ARRAY_SIZE(bq24735_charger_properties);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	supply_desc->get_property = bq24735_charger_get_property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	supply_desc->set_property = bq24735_charger_set_property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	supply_desc->property_is_writeable =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 				bq24735_charger_property_is_writeable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	psy_cfg.supplied_to = charger->pdata->supplied_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	psy_cfg.num_supplicants = charger->pdata->num_supplicants;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	psy_cfg.of_node = client->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	psy_cfg.drv_data = charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	i2c_set_clientdata(client, charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	charger->status_gpio = devm_gpiod_get_optional(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 						       "ti,ac-detect",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 						       GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	if (IS_ERR(charger->status_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		ret = PTR_ERR(charger->status_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		dev_err(&client->dev, "Getting gpio failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (bq24735_charger_is_present(charger)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		ret = bq24735_read_word(client, BQ24735_MANUFACTURER_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			dev_err(&client->dev, "Failed to read manufacturer id : %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		} else if (ret != 0x0040) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 				"manufacturer id mismatch. 0x0040 != 0x%04x\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		ret = bq24735_read_word(client, BQ24735_DEVICE_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			dev_err(&client->dev, "Failed to read device id : %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		} else if (ret != 0x000B) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 				"device id mismatch. 0x000b != 0x%04x\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		ret = bq24735_enable_charging(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			dev_err(&client->dev, "Failed to enable charging\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	charger->charger = devm_power_supply_register(&client->dev, supply_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 						      &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	if (IS_ERR(charger->charger)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		ret = PTR_ERR(charger->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		dev_err(&client->dev, "Failed to register power supply: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (client->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		ret = devm_request_threaded_irq(&client->dev, client->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 						NULL, bq24735_charger_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 						IRQF_TRIGGER_RISING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 						IRQF_TRIGGER_FALLING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 						IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 						supply_desc->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 						charger->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 				"Unable to register IRQ %d err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 				client->irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		ret = device_property_read_u32(&client->dev, "poll-interval",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 					       &charger->poll_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		if (!charger->poll_interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		INIT_DELAYED_WORK(&charger->poll, bq24735_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		schedule_delayed_work(&charger->poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 				      msecs_to_jiffies(charger->poll_interval));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static int bq24735_charger_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	struct bq24735 *charger = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	if (charger->poll_interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		cancel_delayed_work_sync(&charger->poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) static const struct i2c_device_id bq24735_charger_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	{ "bq24735-charger", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) MODULE_DEVICE_TABLE(i2c, bq24735_charger_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static const struct of_device_id bq24735_match_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	{ .compatible = "ti,bq24735", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	{ /* end */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) MODULE_DEVICE_TABLE(of, bq24735_match_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static struct i2c_driver bq24735_charger_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		.name = "bq24735-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		.of_match_table = bq24735_match_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	.probe = bq24735_charger_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	.remove = bq24735_charger_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	.id_table = bq24735_charger_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) module_i2c_driver(bq24735_charger_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) MODULE_DESCRIPTION("bq24735 battery charging driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) MODULE_LICENSE("GPL v2");