18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * bq2415x charger driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2011-2013  Pali Rohár <pali@kernel.org>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Datasheets:
88c2ecf20Sopenharmony_ci * https://www.ti.com/product/bq24150
98c2ecf20Sopenharmony_ci * https://www.ti.com/product/bq24150a
108c2ecf20Sopenharmony_ci * https://www.ti.com/product/bq24152
118c2ecf20Sopenharmony_ci * https://www.ti.com/product/bq24153
128c2ecf20Sopenharmony_ci * https://www.ti.com/product/bq24153a
138c2ecf20Sopenharmony_ci * https://www.ti.com/product/bq24155
148c2ecf20Sopenharmony_ci * https://www.ti.com/product/bq24157s
158c2ecf20Sopenharmony_ci * https://www.ti.com/product/bq24158
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/kernel.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/param.h>
218c2ecf20Sopenharmony_ci#include <linux/err.h>
228c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
238c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
248c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
258c2ecf20Sopenharmony_ci#include <linux/power_supply.h>
268c2ecf20Sopenharmony_ci#include <linux/idr.h>
278c2ecf20Sopenharmony_ci#include <linux/i2c.h>
288c2ecf20Sopenharmony_ci#include <linux/slab.h>
298c2ecf20Sopenharmony_ci#include <linux/acpi.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/power/bq2415x_charger.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* timeout for resetting chip timer */
348c2ecf20Sopenharmony_ci#define BQ2415X_TIMER_TIMEOUT		10
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define BQ2415X_REG_STATUS		0x00
378c2ecf20Sopenharmony_ci#define BQ2415X_REG_CONTROL		0x01
388c2ecf20Sopenharmony_ci#define BQ2415X_REG_VOLTAGE		0x02
398c2ecf20Sopenharmony_ci#define BQ2415X_REG_VENDER		0x03
408c2ecf20Sopenharmony_ci#define BQ2415X_REG_CURRENT		0x04
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* reset state for all registers */
438c2ecf20Sopenharmony_ci#define BQ2415X_RESET_STATUS		BIT(6)
448c2ecf20Sopenharmony_ci#define BQ2415X_RESET_CONTROL		(BIT(4)|BIT(5))
458c2ecf20Sopenharmony_ci#define BQ2415X_RESET_VOLTAGE		(BIT(1)|BIT(3))
468c2ecf20Sopenharmony_ci#define BQ2415X_RESET_CURRENT		(BIT(0)|BIT(3)|BIT(7))
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* status register */
498c2ecf20Sopenharmony_ci#define BQ2415X_BIT_TMR_RST		7
508c2ecf20Sopenharmony_ci#define BQ2415X_BIT_OTG			7
518c2ecf20Sopenharmony_ci#define BQ2415X_BIT_EN_STAT		6
528c2ecf20Sopenharmony_ci#define BQ2415X_MASK_STAT		(BIT(4)|BIT(5))
538c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_STAT		4
548c2ecf20Sopenharmony_ci#define BQ2415X_BIT_BOOST		3
558c2ecf20Sopenharmony_ci#define BQ2415X_MASK_FAULT		(BIT(0)|BIT(1)|BIT(2))
568c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_FAULT		0
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* control register */
598c2ecf20Sopenharmony_ci#define BQ2415X_MASK_LIMIT		(BIT(6)|BIT(7))
608c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_LIMIT		6
618c2ecf20Sopenharmony_ci#define BQ2415X_MASK_VLOWV		(BIT(4)|BIT(5))
628c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_VLOWV		4
638c2ecf20Sopenharmony_ci#define BQ2415X_BIT_TE			3
648c2ecf20Sopenharmony_ci#define BQ2415X_BIT_CE			2
658c2ecf20Sopenharmony_ci#define BQ2415X_BIT_HZ_MODE		1
668c2ecf20Sopenharmony_ci#define BQ2415X_BIT_OPA_MODE		0
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* voltage register */
698c2ecf20Sopenharmony_ci#define BQ2415X_MASK_VO		(BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
708c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_VO		2
718c2ecf20Sopenharmony_ci#define BQ2415X_BIT_OTG_PL		1
728c2ecf20Sopenharmony_ci#define BQ2415X_BIT_OTG_EN		0
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* vender register */
758c2ecf20Sopenharmony_ci#define BQ2415X_MASK_VENDER		(BIT(5)|BIT(6)|BIT(7))
768c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_VENDER		5
778c2ecf20Sopenharmony_ci#define BQ2415X_MASK_PN			(BIT(3)|BIT(4))
788c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_PN		3
798c2ecf20Sopenharmony_ci#define BQ2415X_MASK_REVISION		(BIT(0)|BIT(1)|BIT(2))
808c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_REVISION		0
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* current register */
838c2ecf20Sopenharmony_ci#define BQ2415X_MASK_RESET		BIT(7)
848c2ecf20Sopenharmony_ci#define BQ2415X_MASK_VI_CHRG		(BIT(4)|BIT(5)|BIT(6))
858c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_VI_CHRG		4
868c2ecf20Sopenharmony_ci/* N/A					BIT(3) */
878c2ecf20Sopenharmony_ci#define BQ2415X_MASK_VI_TERM		(BIT(0)|BIT(1)|BIT(2))
888c2ecf20Sopenharmony_ci#define BQ2415X_SHIFT_VI_TERM		0
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cienum bq2415x_command {
928c2ecf20Sopenharmony_ci	BQ2415X_TIMER_RESET,
938c2ecf20Sopenharmony_ci	BQ2415X_OTG_STATUS,
948c2ecf20Sopenharmony_ci	BQ2415X_STAT_PIN_STATUS,
958c2ecf20Sopenharmony_ci	BQ2415X_STAT_PIN_ENABLE,
968c2ecf20Sopenharmony_ci	BQ2415X_STAT_PIN_DISABLE,
978c2ecf20Sopenharmony_ci	BQ2415X_CHARGE_STATUS,
988c2ecf20Sopenharmony_ci	BQ2415X_BOOST_STATUS,
998c2ecf20Sopenharmony_ci	BQ2415X_FAULT_STATUS,
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	BQ2415X_CHARGE_TERMINATION_STATUS,
1028c2ecf20Sopenharmony_ci	BQ2415X_CHARGE_TERMINATION_ENABLE,
1038c2ecf20Sopenharmony_ci	BQ2415X_CHARGE_TERMINATION_DISABLE,
1048c2ecf20Sopenharmony_ci	BQ2415X_CHARGER_STATUS,
1058c2ecf20Sopenharmony_ci	BQ2415X_CHARGER_ENABLE,
1068c2ecf20Sopenharmony_ci	BQ2415X_CHARGER_DISABLE,
1078c2ecf20Sopenharmony_ci	BQ2415X_HIGH_IMPEDANCE_STATUS,
1088c2ecf20Sopenharmony_ci	BQ2415X_HIGH_IMPEDANCE_ENABLE,
1098c2ecf20Sopenharmony_ci	BQ2415X_HIGH_IMPEDANCE_DISABLE,
1108c2ecf20Sopenharmony_ci	BQ2415X_BOOST_MODE_STATUS,
1118c2ecf20Sopenharmony_ci	BQ2415X_BOOST_MODE_ENABLE,
1128c2ecf20Sopenharmony_ci	BQ2415X_BOOST_MODE_DISABLE,
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	BQ2415X_OTG_LEVEL,
1158c2ecf20Sopenharmony_ci	BQ2415X_OTG_ACTIVATE_HIGH,
1168c2ecf20Sopenharmony_ci	BQ2415X_OTG_ACTIVATE_LOW,
1178c2ecf20Sopenharmony_ci	BQ2415X_OTG_PIN_STATUS,
1188c2ecf20Sopenharmony_ci	BQ2415X_OTG_PIN_ENABLE,
1198c2ecf20Sopenharmony_ci	BQ2415X_OTG_PIN_DISABLE,
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	BQ2415X_VENDER_CODE,
1228c2ecf20Sopenharmony_ci	BQ2415X_PART_NUMBER,
1238c2ecf20Sopenharmony_ci	BQ2415X_REVISION,
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cienum bq2415x_chip {
1278c2ecf20Sopenharmony_ci	BQUNKNOWN,
1288c2ecf20Sopenharmony_ci	BQ24150,
1298c2ecf20Sopenharmony_ci	BQ24150A,
1308c2ecf20Sopenharmony_ci	BQ24151,
1318c2ecf20Sopenharmony_ci	BQ24151A,
1328c2ecf20Sopenharmony_ci	BQ24152,
1338c2ecf20Sopenharmony_ci	BQ24153,
1348c2ecf20Sopenharmony_ci	BQ24153A,
1358c2ecf20Sopenharmony_ci	BQ24155,
1368c2ecf20Sopenharmony_ci	BQ24156,
1378c2ecf20Sopenharmony_ci	BQ24156A,
1388c2ecf20Sopenharmony_ci	BQ24157S,
1398c2ecf20Sopenharmony_ci	BQ24158,
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic char *bq2415x_chip_name[] = {
1438c2ecf20Sopenharmony_ci	"unknown",
1448c2ecf20Sopenharmony_ci	"bq24150",
1458c2ecf20Sopenharmony_ci	"bq24150a",
1468c2ecf20Sopenharmony_ci	"bq24151",
1478c2ecf20Sopenharmony_ci	"bq24151a",
1488c2ecf20Sopenharmony_ci	"bq24152",
1498c2ecf20Sopenharmony_ci	"bq24153",
1508c2ecf20Sopenharmony_ci	"bq24153a",
1518c2ecf20Sopenharmony_ci	"bq24155",
1528c2ecf20Sopenharmony_ci	"bq24156",
1538c2ecf20Sopenharmony_ci	"bq24156a",
1548c2ecf20Sopenharmony_ci	"bq24157s",
1558c2ecf20Sopenharmony_ci	"bq24158",
1568c2ecf20Sopenharmony_ci};
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistruct bq2415x_device {
1598c2ecf20Sopenharmony_ci	struct device *dev;
1608c2ecf20Sopenharmony_ci	struct bq2415x_platform_data init_data;
1618c2ecf20Sopenharmony_ci	struct power_supply *charger;
1628c2ecf20Sopenharmony_ci	struct power_supply_desc charger_desc;
1638c2ecf20Sopenharmony_ci	struct delayed_work work;
1648c2ecf20Sopenharmony_ci	struct device_node *notify_node;
1658c2ecf20Sopenharmony_ci	struct notifier_block nb;
1668c2ecf20Sopenharmony_ci	enum bq2415x_mode reported_mode;/* mode reported by hook function */
1678c2ecf20Sopenharmony_ci	enum bq2415x_mode mode;		/* currently configured mode */
1688c2ecf20Sopenharmony_ci	enum bq2415x_chip chip;
1698c2ecf20Sopenharmony_ci	const char *timer_error;
1708c2ecf20Sopenharmony_ci	char *model;
1718c2ecf20Sopenharmony_ci	char *name;
1728c2ecf20Sopenharmony_ci	int autotimer;	/* 1 - if driver automatically reset timer, 0 - not */
1738c2ecf20Sopenharmony_ci	int automode;	/* 1 - enabled, 0 - disabled; -1 - not supported */
1748c2ecf20Sopenharmony_ci	int id;
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/* each registered chip must have unique id */
1788c2ecf20Sopenharmony_cistatic DEFINE_IDR(bq2415x_id);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(bq2415x_id_mutex);
1818c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(bq2415x_timer_mutex);
1828c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(bq2415x_i2c_mutex);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/**** i2c read functions ****/
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/* read value from register */
1878c2ecf20Sopenharmony_cistatic int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(bq->dev);
1908c2ecf20Sopenharmony_ci	struct i2c_msg msg[2];
1918c2ecf20Sopenharmony_ci	u8 val;
1928c2ecf20Sopenharmony_ci	int ret;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	if (!client->adapter)
1958c2ecf20Sopenharmony_ci		return -ENODEV;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	msg[0].addr = client->addr;
1988c2ecf20Sopenharmony_ci	msg[0].flags = 0;
1998c2ecf20Sopenharmony_ci	msg[0].buf = &reg;
2008c2ecf20Sopenharmony_ci	msg[0].len = sizeof(reg);
2018c2ecf20Sopenharmony_ci	msg[1].addr = client->addr;
2028c2ecf20Sopenharmony_ci	msg[1].flags = I2C_M_RD;
2038c2ecf20Sopenharmony_ci	msg[1].buf = &val;
2048c2ecf20Sopenharmony_ci	msg[1].len = sizeof(val);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	mutex_lock(&bq2415x_i2c_mutex);
2078c2ecf20Sopenharmony_ci	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
2088c2ecf20Sopenharmony_ci	mutex_unlock(&bq2415x_i2c_mutex);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if (ret < 0)
2118c2ecf20Sopenharmony_ci		return ret;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	return val;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/* read value from register, apply mask and right shift it */
2178c2ecf20Sopenharmony_cistatic int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
2188c2ecf20Sopenharmony_ci				 u8 mask, u8 shift)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	int ret;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	if (shift > 8)
2238c2ecf20Sopenharmony_ci		return -EINVAL;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	ret = bq2415x_i2c_read(bq, reg);
2268c2ecf20Sopenharmony_ci	if (ret < 0)
2278c2ecf20Sopenharmony_ci		return ret;
2288c2ecf20Sopenharmony_ci	return (ret & mask) >> shift;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci/* read value from register and return one specified bit */
2328c2ecf20Sopenharmony_cistatic int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	if (bit > 8)
2358c2ecf20Sopenharmony_ci		return -EINVAL;
2368c2ecf20Sopenharmony_ci	return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/**** i2c write functions ****/
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci/* write value to register */
2428c2ecf20Sopenharmony_cistatic int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(bq->dev);
2458c2ecf20Sopenharmony_ci	struct i2c_msg msg[1];
2468c2ecf20Sopenharmony_ci	u8 data[2];
2478c2ecf20Sopenharmony_ci	int ret;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	data[0] = reg;
2508c2ecf20Sopenharmony_ci	data[1] = val;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	msg[0].addr = client->addr;
2538c2ecf20Sopenharmony_ci	msg[0].flags = 0;
2548c2ecf20Sopenharmony_ci	msg[0].buf = data;
2558c2ecf20Sopenharmony_ci	msg[0].len = ARRAY_SIZE(data);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	mutex_lock(&bq2415x_i2c_mutex);
2588c2ecf20Sopenharmony_ci	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
2598c2ecf20Sopenharmony_ci	mutex_unlock(&bq2415x_i2c_mutex);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* i2c_transfer returns number of messages transferred */
2628c2ecf20Sopenharmony_ci	if (ret < 0)
2638c2ecf20Sopenharmony_ci		return ret;
2648c2ecf20Sopenharmony_ci	else if (ret != 1)
2658c2ecf20Sopenharmony_ci		return -EIO;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return 0;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/* read value from register, change it with mask left shifted and write back */
2718c2ecf20Sopenharmony_cistatic int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
2728c2ecf20Sopenharmony_ci				  u8 mask, u8 shift)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	int ret;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (shift > 8)
2778c2ecf20Sopenharmony_ci		return -EINVAL;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	ret = bq2415x_i2c_read(bq, reg);
2808c2ecf20Sopenharmony_ci	if (ret < 0)
2818c2ecf20Sopenharmony_ci		return ret;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	ret &= ~mask;
2848c2ecf20Sopenharmony_ci	ret |= val << shift;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return bq2415x_i2c_write(bq, reg, ret);
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci/* change only one bit in register */
2908c2ecf20Sopenharmony_cistatic int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
2918c2ecf20Sopenharmony_ci				 bool val, u8 bit)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	if (bit > 8)
2948c2ecf20Sopenharmony_ci		return -EINVAL;
2958c2ecf20Sopenharmony_ci	return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/**** global functions ****/
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci/* exec command function */
3018c2ecf20Sopenharmony_cistatic int bq2415x_exec_command(struct bq2415x_device *bq,
3028c2ecf20Sopenharmony_ci				enum bq2415x_command command)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	int ret;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	switch (command) {
3078c2ecf20Sopenharmony_ci	case BQ2415X_TIMER_RESET:
3088c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
3098c2ecf20Sopenharmony_ci				1, BQ2415X_BIT_TMR_RST);
3108c2ecf20Sopenharmony_ci	case BQ2415X_OTG_STATUS:
3118c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
3128c2ecf20Sopenharmony_ci				BQ2415X_BIT_OTG);
3138c2ecf20Sopenharmony_ci	case BQ2415X_STAT_PIN_STATUS:
3148c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
3158c2ecf20Sopenharmony_ci				BQ2415X_BIT_EN_STAT);
3168c2ecf20Sopenharmony_ci	case BQ2415X_STAT_PIN_ENABLE:
3178c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
3188c2ecf20Sopenharmony_ci				BQ2415X_BIT_EN_STAT);
3198c2ecf20Sopenharmony_ci	case BQ2415X_STAT_PIN_DISABLE:
3208c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
3218c2ecf20Sopenharmony_ci				BQ2415X_BIT_EN_STAT);
3228c2ecf20Sopenharmony_ci	case BQ2415X_CHARGE_STATUS:
3238c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
3248c2ecf20Sopenharmony_ci				BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
3258c2ecf20Sopenharmony_ci	case BQ2415X_BOOST_STATUS:
3268c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
3278c2ecf20Sopenharmony_ci				BQ2415X_BIT_BOOST);
3288c2ecf20Sopenharmony_ci	case BQ2415X_FAULT_STATUS:
3298c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
3308c2ecf20Sopenharmony_ci			BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	case BQ2415X_CHARGE_TERMINATION_STATUS:
3338c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
3348c2ecf20Sopenharmony_ci				BQ2415X_BIT_TE);
3358c2ecf20Sopenharmony_ci	case BQ2415X_CHARGE_TERMINATION_ENABLE:
3368c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
3378c2ecf20Sopenharmony_ci				1, BQ2415X_BIT_TE);
3388c2ecf20Sopenharmony_ci	case BQ2415X_CHARGE_TERMINATION_DISABLE:
3398c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
3408c2ecf20Sopenharmony_ci				0, BQ2415X_BIT_TE);
3418c2ecf20Sopenharmony_ci	case BQ2415X_CHARGER_STATUS:
3428c2ecf20Sopenharmony_ci		ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
3438c2ecf20Sopenharmony_ci			BQ2415X_BIT_CE);
3448c2ecf20Sopenharmony_ci		if (ret < 0)
3458c2ecf20Sopenharmony_ci			return ret;
3468c2ecf20Sopenharmony_ci		return ret > 0 ? 0 : 1;
3478c2ecf20Sopenharmony_ci	case BQ2415X_CHARGER_ENABLE:
3488c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
3498c2ecf20Sopenharmony_ci				0, BQ2415X_BIT_CE);
3508c2ecf20Sopenharmony_ci	case BQ2415X_CHARGER_DISABLE:
3518c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
3528c2ecf20Sopenharmony_ci				1, BQ2415X_BIT_CE);
3538c2ecf20Sopenharmony_ci	case BQ2415X_HIGH_IMPEDANCE_STATUS:
3548c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
3558c2ecf20Sopenharmony_ci				BQ2415X_BIT_HZ_MODE);
3568c2ecf20Sopenharmony_ci	case BQ2415X_HIGH_IMPEDANCE_ENABLE:
3578c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
3588c2ecf20Sopenharmony_ci				1, BQ2415X_BIT_HZ_MODE);
3598c2ecf20Sopenharmony_ci	case BQ2415X_HIGH_IMPEDANCE_DISABLE:
3608c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
3618c2ecf20Sopenharmony_ci				0, BQ2415X_BIT_HZ_MODE);
3628c2ecf20Sopenharmony_ci	case BQ2415X_BOOST_MODE_STATUS:
3638c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
3648c2ecf20Sopenharmony_ci				BQ2415X_BIT_OPA_MODE);
3658c2ecf20Sopenharmony_ci	case BQ2415X_BOOST_MODE_ENABLE:
3668c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
3678c2ecf20Sopenharmony_ci				1, BQ2415X_BIT_OPA_MODE);
3688c2ecf20Sopenharmony_ci	case BQ2415X_BOOST_MODE_DISABLE:
3698c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
3708c2ecf20Sopenharmony_ci				0, BQ2415X_BIT_OPA_MODE);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	case BQ2415X_OTG_LEVEL:
3738c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
3748c2ecf20Sopenharmony_ci				BQ2415X_BIT_OTG_PL);
3758c2ecf20Sopenharmony_ci	case BQ2415X_OTG_ACTIVATE_HIGH:
3768c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
3778c2ecf20Sopenharmony_ci				1, BQ2415X_BIT_OTG_PL);
3788c2ecf20Sopenharmony_ci	case BQ2415X_OTG_ACTIVATE_LOW:
3798c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
3808c2ecf20Sopenharmony_ci				0, BQ2415X_BIT_OTG_PL);
3818c2ecf20Sopenharmony_ci	case BQ2415X_OTG_PIN_STATUS:
3828c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
3838c2ecf20Sopenharmony_ci				BQ2415X_BIT_OTG_EN);
3848c2ecf20Sopenharmony_ci	case BQ2415X_OTG_PIN_ENABLE:
3858c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
3868c2ecf20Sopenharmony_ci				1, BQ2415X_BIT_OTG_EN);
3878c2ecf20Sopenharmony_ci	case BQ2415X_OTG_PIN_DISABLE:
3888c2ecf20Sopenharmony_ci		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
3898c2ecf20Sopenharmony_ci				0, BQ2415X_BIT_OTG_EN);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	case BQ2415X_VENDER_CODE:
3928c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
3938c2ecf20Sopenharmony_ci			BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER);
3948c2ecf20Sopenharmony_ci	case BQ2415X_PART_NUMBER:
3958c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
3968c2ecf20Sopenharmony_ci				BQ2415X_MASK_PN, BQ2415X_SHIFT_PN);
3978c2ecf20Sopenharmony_ci	case BQ2415X_REVISION:
3988c2ecf20Sopenharmony_ci		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
3998c2ecf20Sopenharmony_ci			BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION);
4008c2ecf20Sopenharmony_ci	}
4018c2ecf20Sopenharmony_ci	return -EINVAL;
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci/* detect chip type */
4058c2ecf20Sopenharmony_cistatic enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(bq->dev);
4088c2ecf20Sopenharmony_ci	int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (ret < 0)
4118c2ecf20Sopenharmony_ci		return ret;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	switch (client->addr) {
4148c2ecf20Sopenharmony_ci	case 0x6b:
4158c2ecf20Sopenharmony_ci		switch (ret) {
4168c2ecf20Sopenharmony_ci		case 0:
4178c2ecf20Sopenharmony_ci			if (bq->chip == BQ24151A)
4188c2ecf20Sopenharmony_ci				return bq->chip;
4198c2ecf20Sopenharmony_ci			return BQ24151;
4208c2ecf20Sopenharmony_ci		case 1:
4218c2ecf20Sopenharmony_ci			if (bq->chip == BQ24150A ||
4228c2ecf20Sopenharmony_ci				bq->chip == BQ24152 ||
4238c2ecf20Sopenharmony_ci				bq->chip == BQ24155)
4248c2ecf20Sopenharmony_ci				return bq->chip;
4258c2ecf20Sopenharmony_ci			return BQ24150;
4268c2ecf20Sopenharmony_ci		case 2:
4278c2ecf20Sopenharmony_ci			if (bq->chip == BQ24153A)
4288c2ecf20Sopenharmony_ci				return bq->chip;
4298c2ecf20Sopenharmony_ci			return BQ24153;
4308c2ecf20Sopenharmony_ci		default:
4318c2ecf20Sopenharmony_ci			return BQUNKNOWN;
4328c2ecf20Sopenharmony_ci		}
4338c2ecf20Sopenharmony_ci		break;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	case 0x6a:
4368c2ecf20Sopenharmony_ci		switch (ret) {
4378c2ecf20Sopenharmony_ci		case 0:
4388c2ecf20Sopenharmony_ci			if (bq->chip == BQ24156A)
4398c2ecf20Sopenharmony_ci				return bq->chip;
4408c2ecf20Sopenharmony_ci			return BQ24156;
4418c2ecf20Sopenharmony_ci		case 2:
4428c2ecf20Sopenharmony_ci			if (bq->chip == BQ24157S)
4438c2ecf20Sopenharmony_ci				return bq->chip;
4448c2ecf20Sopenharmony_ci			return BQ24158;
4458c2ecf20Sopenharmony_ci		default:
4468c2ecf20Sopenharmony_ci			return BQUNKNOWN;
4478c2ecf20Sopenharmony_ci		}
4488c2ecf20Sopenharmony_ci		break;
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return BQUNKNOWN;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci/* detect chip revision */
4558c2ecf20Sopenharmony_cistatic int bq2415x_detect_revision(struct bq2415x_device *bq)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
4588c2ecf20Sopenharmony_ci	int chip = bq2415x_detect_chip(bq);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	if (ret < 0 || chip < 0)
4618c2ecf20Sopenharmony_ci		return -1;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	switch (chip) {
4648c2ecf20Sopenharmony_ci	case BQ24150:
4658c2ecf20Sopenharmony_ci	case BQ24150A:
4668c2ecf20Sopenharmony_ci	case BQ24151:
4678c2ecf20Sopenharmony_ci	case BQ24151A:
4688c2ecf20Sopenharmony_ci	case BQ24152:
4698c2ecf20Sopenharmony_ci		if (ret >= 0 && ret <= 3)
4708c2ecf20Sopenharmony_ci			return ret;
4718c2ecf20Sopenharmony_ci		return -1;
4728c2ecf20Sopenharmony_ci	case BQ24153:
4738c2ecf20Sopenharmony_ci	case BQ24153A:
4748c2ecf20Sopenharmony_ci	case BQ24156:
4758c2ecf20Sopenharmony_ci	case BQ24156A:
4768c2ecf20Sopenharmony_ci	case BQ24157S:
4778c2ecf20Sopenharmony_ci	case BQ24158:
4788c2ecf20Sopenharmony_ci		if (ret == 3)
4798c2ecf20Sopenharmony_ci			return 0;
4808c2ecf20Sopenharmony_ci		else if (ret == 1)
4818c2ecf20Sopenharmony_ci			return 1;
4828c2ecf20Sopenharmony_ci		return -1;
4838c2ecf20Sopenharmony_ci	case BQ24155:
4848c2ecf20Sopenharmony_ci		if (ret == 3)
4858c2ecf20Sopenharmony_ci			return 3;
4868c2ecf20Sopenharmony_ci		return -1;
4878c2ecf20Sopenharmony_ci	case BQUNKNOWN:
4888c2ecf20Sopenharmony_ci		return -1;
4898c2ecf20Sopenharmony_ci	}
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	return -1;
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci/* return chip vender code */
4958c2ecf20Sopenharmony_cistatic int bq2415x_get_vender_code(struct bq2415x_device *bq)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	int ret;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
5008c2ecf20Sopenharmony_ci	if (ret < 0)
5018c2ecf20Sopenharmony_ci		return 0;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	/* convert to binary */
5048c2ecf20Sopenharmony_ci	return (ret & 0x1) +
5058c2ecf20Sopenharmony_ci	       ((ret >> 1) & 0x1) * 10 +
5068c2ecf20Sopenharmony_ci	       ((ret >> 2) & 0x1) * 100;
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci/* reset all chip registers to default state */
5108c2ecf20Sopenharmony_cistatic void bq2415x_reset_chip(struct bq2415x_device *bq)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci	bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
5138c2ecf20Sopenharmony_ci	bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
5148c2ecf20Sopenharmony_ci	bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
5158c2ecf20Sopenharmony_ci	bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
5168c2ecf20Sopenharmony_ci	bq->timer_error = NULL;
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci/**** properties functions ****/
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci/* set current limit in mA */
5228c2ecf20Sopenharmony_cistatic int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	int val;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	if (mA <= 100)
5278c2ecf20Sopenharmony_ci		val = 0;
5288c2ecf20Sopenharmony_ci	else if (mA <= 500)
5298c2ecf20Sopenharmony_ci		val = 1;
5308c2ecf20Sopenharmony_ci	else if (mA <= 800)
5318c2ecf20Sopenharmony_ci		val = 2;
5328c2ecf20Sopenharmony_ci	else
5338c2ecf20Sopenharmony_ci		val = 3;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
5368c2ecf20Sopenharmony_ci			BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci/* get current limit in mA */
5408c2ecf20Sopenharmony_cistatic int bq2415x_get_current_limit(struct bq2415x_device *bq)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	int ret;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
5458c2ecf20Sopenharmony_ci			BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
5468c2ecf20Sopenharmony_ci	if (ret < 0)
5478c2ecf20Sopenharmony_ci		return ret;
5488c2ecf20Sopenharmony_ci	else if (ret == 0)
5498c2ecf20Sopenharmony_ci		return 100;
5508c2ecf20Sopenharmony_ci	else if (ret == 1)
5518c2ecf20Sopenharmony_ci		return 500;
5528c2ecf20Sopenharmony_ci	else if (ret == 2)
5538c2ecf20Sopenharmony_ci		return 800;
5548c2ecf20Sopenharmony_ci	else if (ret == 3)
5558c2ecf20Sopenharmony_ci		return 1800;
5568c2ecf20Sopenharmony_ci	return -EINVAL;
5578c2ecf20Sopenharmony_ci}
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci/* set weak battery voltage in mV */
5608c2ecf20Sopenharmony_cistatic int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	int val;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	/* round to 100mV */
5658c2ecf20Sopenharmony_ci	if (mV <= 3400 + 50)
5668c2ecf20Sopenharmony_ci		val = 0;
5678c2ecf20Sopenharmony_ci	else if (mV <= 3500 + 50)
5688c2ecf20Sopenharmony_ci		val = 1;
5698c2ecf20Sopenharmony_ci	else if (mV <= 3600 + 50)
5708c2ecf20Sopenharmony_ci		val = 2;
5718c2ecf20Sopenharmony_ci	else
5728c2ecf20Sopenharmony_ci		val = 3;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
5758c2ecf20Sopenharmony_ci			BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci/* get weak battery voltage in mV */
5798c2ecf20Sopenharmony_cistatic int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	int ret;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
5848c2ecf20Sopenharmony_ci			BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
5858c2ecf20Sopenharmony_ci	if (ret < 0)
5868c2ecf20Sopenharmony_ci		return ret;
5878c2ecf20Sopenharmony_ci	return 100 * (34 + ret);
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci/* set battery regulation voltage in mV */
5918c2ecf20Sopenharmony_cistatic int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
5928c2ecf20Sopenharmony_ci						  int mV)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	int val = (mV/10 - 350) / 2;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	/*
5978c2ecf20Sopenharmony_ci	 * According to datasheet, maximum battery regulation voltage is
5988c2ecf20Sopenharmony_ci	 * 4440mV which is b101111 = 47.
5998c2ecf20Sopenharmony_ci	 */
6008c2ecf20Sopenharmony_ci	if (val < 0)
6018c2ecf20Sopenharmony_ci		val = 0;
6028c2ecf20Sopenharmony_ci	else if (val > 47)
6038c2ecf20Sopenharmony_ci		return -EINVAL;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
6068c2ecf20Sopenharmony_ci			BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
6078c2ecf20Sopenharmony_ci}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci/* get battery regulation voltage in mV */
6108c2ecf20Sopenharmony_cistatic int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
6138c2ecf20Sopenharmony_ci			BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	if (ret < 0)
6168c2ecf20Sopenharmony_ci		return ret;
6178c2ecf20Sopenharmony_ci	return 10 * (350 + 2*ret);
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci/* set charge current in mA (platform data must provide resistor sense) */
6218c2ecf20Sopenharmony_cistatic int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	int val;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	if (bq->init_data.resistor_sense <= 0)
6268c2ecf20Sopenharmony_ci		return -EINVAL;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
6298c2ecf20Sopenharmony_ci	if (val < 0)
6308c2ecf20Sopenharmony_ci		val = 0;
6318c2ecf20Sopenharmony_ci	else if (val > 7)
6328c2ecf20Sopenharmony_ci		val = 7;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
6358c2ecf20Sopenharmony_ci			BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
6368c2ecf20Sopenharmony_ci			BQ2415X_SHIFT_VI_CHRG);
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci/* get charge current in mA (platform data must provide resistor sense) */
6408c2ecf20Sopenharmony_cistatic int bq2415x_get_charge_current(struct bq2415x_device *bq)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	int ret;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	if (bq->init_data.resistor_sense <= 0)
6458c2ecf20Sopenharmony_ci		return -EINVAL;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
6488c2ecf20Sopenharmony_ci			BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
6498c2ecf20Sopenharmony_ci	if (ret < 0)
6508c2ecf20Sopenharmony_ci		return ret;
6518c2ecf20Sopenharmony_ci	return (37400 + 6800*ret) / bq->init_data.resistor_sense;
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci/* set termination current in mA (platform data must provide resistor sense) */
6558c2ecf20Sopenharmony_cistatic int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
6568c2ecf20Sopenharmony_ci{
6578c2ecf20Sopenharmony_ci	int val;
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	if (bq->init_data.resistor_sense <= 0)
6608c2ecf20Sopenharmony_ci		return -EINVAL;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
6638c2ecf20Sopenharmony_ci	if (val < 0)
6648c2ecf20Sopenharmony_ci		val = 0;
6658c2ecf20Sopenharmony_ci	else if (val > 7)
6668c2ecf20Sopenharmony_ci		val = 7;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
6698c2ecf20Sopenharmony_ci			BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
6708c2ecf20Sopenharmony_ci			BQ2415X_SHIFT_VI_TERM);
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci/* get termination current in mA (platform data must provide resistor sense) */
6748c2ecf20Sopenharmony_cistatic int bq2415x_get_termination_current(struct bq2415x_device *bq)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	int ret;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if (bq->init_data.resistor_sense <= 0)
6798c2ecf20Sopenharmony_ci		return -EINVAL;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
6828c2ecf20Sopenharmony_ci			BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
6838c2ecf20Sopenharmony_ci	if (ret < 0)
6848c2ecf20Sopenharmony_ci		return ret;
6858c2ecf20Sopenharmony_ci	return (3400 + 3400*ret) / bq->init_data.resistor_sense;
6868c2ecf20Sopenharmony_ci}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci/* set default value of property */
6898c2ecf20Sopenharmony_ci#define bq2415x_set_default_value(bq, prop) \
6908c2ecf20Sopenharmony_ci	do { \
6918c2ecf20Sopenharmony_ci		int ret = 0; \
6928c2ecf20Sopenharmony_ci		if (bq->init_data.prop != -1) \
6938c2ecf20Sopenharmony_ci			ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
6948c2ecf20Sopenharmony_ci		if (ret < 0) \
6958c2ecf20Sopenharmony_ci			return ret; \
6968c2ecf20Sopenharmony_ci	} while (0)
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci/* set default values of all properties */
6998c2ecf20Sopenharmony_cistatic int bq2415x_set_defaults(struct bq2415x_device *bq)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
7028c2ecf20Sopenharmony_ci	bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
7038c2ecf20Sopenharmony_ci	bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	bq2415x_set_default_value(bq, current_limit);
7068c2ecf20Sopenharmony_ci	bq2415x_set_default_value(bq, weak_battery_voltage);
7078c2ecf20Sopenharmony_ci	bq2415x_set_default_value(bq, battery_regulation_voltage);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	if (bq->init_data.resistor_sense > 0) {
7108c2ecf20Sopenharmony_ci		bq2415x_set_default_value(bq, charge_current);
7118c2ecf20Sopenharmony_ci		bq2415x_set_default_value(bq, termination_current);
7128c2ecf20Sopenharmony_ci		bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
7138c2ecf20Sopenharmony_ci	}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
7168c2ecf20Sopenharmony_ci	return 0;
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci/**** charger mode functions ****/
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci/* set charger mode */
7228c2ecf20Sopenharmony_cistatic int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	int ret = 0;
7258c2ecf20Sopenharmony_ci	int charger = 0;
7268c2ecf20Sopenharmony_ci	int boost = 0;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	if (mode == BQ2415X_MODE_BOOST)
7298c2ecf20Sopenharmony_ci		boost = 1;
7308c2ecf20Sopenharmony_ci	else if (mode != BQ2415X_MODE_OFF)
7318c2ecf20Sopenharmony_ci		charger = 1;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	if (!charger)
7348c2ecf20Sopenharmony_ci		ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	if (!boost)
7378c2ecf20Sopenharmony_ci		ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	if (ret < 0)
7408c2ecf20Sopenharmony_ci		return ret;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	switch (mode) {
7438c2ecf20Sopenharmony_ci	case BQ2415X_MODE_OFF:
7448c2ecf20Sopenharmony_ci		dev_dbg(bq->dev, "changing mode to: Offline\n");
7458c2ecf20Sopenharmony_ci		ret = bq2415x_set_current_limit(bq, 100);
7468c2ecf20Sopenharmony_ci		break;
7478c2ecf20Sopenharmony_ci	case BQ2415X_MODE_NONE:
7488c2ecf20Sopenharmony_ci		dev_dbg(bq->dev, "changing mode to: N/A\n");
7498c2ecf20Sopenharmony_ci		ret = bq2415x_set_current_limit(bq, 100);
7508c2ecf20Sopenharmony_ci		break;
7518c2ecf20Sopenharmony_ci	case BQ2415X_MODE_HOST_CHARGER:
7528c2ecf20Sopenharmony_ci		dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
7538c2ecf20Sopenharmony_ci		ret = bq2415x_set_current_limit(bq, 500);
7548c2ecf20Sopenharmony_ci		break;
7558c2ecf20Sopenharmony_ci	case BQ2415X_MODE_DEDICATED_CHARGER:
7568c2ecf20Sopenharmony_ci		dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
7578c2ecf20Sopenharmony_ci		ret = bq2415x_set_current_limit(bq, 1800);
7588c2ecf20Sopenharmony_ci		break;
7598c2ecf20Sopenharmony_ci	case BQ2415X_MODE_BOOST: /* Boost mode */
7608c2ecf20Sopenharmony_ci		dev_dbg(bq->dev, "changing mode to: Boost\n");
7618c2ecf20Sopenharmony_ci		ret = bq2415x_set_current_limit(bq, 100);
7628c2ecf20Sopenharmony_ci		break;
7638c2ecf20Sopenharmony_ci	}
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	if (ret < 0)
7668c2ecf20Sopenharmony_ci		return ret;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	if (charger)
7698c2ecf20Sopenharmony_ci		ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
7708c2ecf20Sopenharmony_ci	else if (boost)
7718c2ecf20Sopenharmony_ci		ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	if (ret < 0)
7748c2ecf20Sopenharmony_ci		return ret;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	bq2415x_set_default_value(bq, weak_battery_voltage);
7778c2ecf20Sopenharmony_ci	bq2415x_set_default_value(bq, battery_regulation_voltage);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	bq->mode = mode;
7808c2ecf20Sopenharmony_ci	sysfs_notify(&bq->charger->dev.kobj, NULL, "mode");
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	return 0;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci}
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_cistatic bool bq2415x_update_reported_mode(struct bq2415x_device *bq, int mA)
7878c2ecf20Sopenharmony_ci{
7888c2ecf20Sopenharmony_ci	enum bq2415x_mode mode;
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	if (mA == 0)
7918c2ecf20Sopenharmony_ci		mode = BQ2415X_MODE_OFF;
7928c2ecf20Sopenharmony_ci	else if (mA < 500)
7938c2ecf20Sopenharmony_ci		mode = BQ2415X_MODE_NONE;
7948c2ecf20Sopenharmony_ci	else if (mA < 1800)
7958c2ecf20Sopenharmony_ci		mode = BQ2415X_MODE_HOST_CHARGER;
7968c2ecf20Sopenharmony_ci	else
7978c2ecf20Sopenharmony_ci		mode = BQ2415X_MODE_DEDICATED_CHARGER;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	if (bq->reported_mode == mode)
8008c2ecf20Sopenharmony_ci		return false;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	bq->reported_mode = mode;
8038c2ecf20Sopenharmony_ci	return true;
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_cistatic int bq2415x_notifier_call(struct notifier_block *nb,
8078c2ecf20Sopenharmony_ci		unsigned long val, void *v)
8088c2ecf20Sopenharmony_ci{
8098c2ecf20Sopenharmony_ci	struct bq2415x_device *bq =
8108c2ecf20Sopenharmony_ci		container_of(nb, struct bq2415x_device, nb);
8118c2ecf20Sopenharmony_ci	struct power_supply *psy = v;
8128c2ecf20Sopenharmony_ci	union power_supply_propval prop;
8138c2ecf20Sopenharmony_ci	int ret;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	if (val != PSY_EVENT_PROP_CHANGED)
8168c2ecf20Sopenharmony_ci		return NOTIFY_OK;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	/* Ignore event if it was not send by notify_node/notify_device */
8198c2ecf20Sopenharmony_ci	if (bq->notify_node) {
8208c2ecf20Sopenharmony_ci		if (!psy->dev.parent ||
8218c2ecf20Sopenharmony_ci		    psy->dev.parent->of_node != bq->notify_node)
8228c2ecf20Sopenharmony_ci			return NOTIFY_OK;
8238c2ecf20Sopenharmony_ci	} else if (bq->init_data.notify_device) {
8248c2ecf20Sopenharmony_ci		if (strcmp(psy->desc->name, bq->init_data.notify_device) != 0)
8258c2ecf20Sopenharmony_ci			return NOTIFY_OK;
8268c2ecf20Sopenharmony_ci	}
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	dev_dbg(bq->dev, "notifier call was called\n");
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX,
8318c2ecf20Sopenharmony_ci			&prop);
8328c2ecf20Sopenharmony_ci	if (ret != 0)
8338c2ecf20Sopenharmony_ci		return NOTIFY_OK;
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	if (!bq2415x_update_reported_mode(bq, prop.intval))
8368c2ecf20Sopenharmony_ci		return NOTIFY_OK;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	/* if automode is not enabled do not tell about reported_mode */
8398c2ecf20Sopenharmony_ci	if (bq->automode < 1)
8408c2ecf20Sopenharmony_ci		return NOTIFY_OK;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	schedule_delayed_work(&bq->work, 0);
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	return NOTIFY_OK;
8458c2ecf20Sopenharmony_ci}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci/**** timer functions ****/
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci/* enable/disable auto resetting chip timer */
8508c2ecf20Sopenharmony_cistatic void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
8518c2ecf20Sopenharmony_ci{
8528c2ecf20Sopenharmony_ci	mutex_lock(&bq2415x_timer_mutex);
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	if (bq->autotimer == state) {
8558c2ecf20Sopenharmony_ci		mutex_unlock(&bq2415x_timer_mutex);
8568c2ecf20Sopenharmony_ci		return;
8578c2ecf20Sopenharmony_ci	}
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	bq->autotimer = state;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	if (state) {
8628c2ecf20Sopenharmony_ci		schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
8638c2ecf20Sopenharmony_ci		bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
8648c2ecf20Sopenharmony_ci		bq->timer_error = NULL;
8658c2ecf20Sopenharmony_ci	} else {
8668c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&bq->work);
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	mutex_unlock(&bq2415x_timer_mutex);
8708c2ecf20Sopenharmony_ci}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci/* called by bq2415x_timer_work on timer error */
8738c2ecf20Sopenharmony_cistatic void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
8748c2ecf20Sopenharmony_ci{
8758c2ecf20Sopenharmony_ci	bq->timer_error = msg;
8768c2ecf20Sopenharmony_ci	sysfs_notify(&bq->charger->dev.kobj, NULL, "timer");
8778c2ecf20Sopenharmony_ci	dev_err(bq->dev, "%s\n", msg);
8788c2ecf20Sopenharmony_ci	if (bq->automode > 0)
8798c2ecf20Sopenharmony_ci		bq->automode = 0;
8808c2ecf20Sopenharmony_ci	bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
8818c2ecf20Sopenharmony_ci	bq2415x_set_autotimer(bq, 0);
8828c2ecf20Sopenharmony_ci}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci/* delayed work function for auto resetting chip timer */
8858c2ecf20Sopenharmony_cistatic void bq2415x_timer_work(struct work_struct *work)
8868c2ecf20Sopenharmony_ci{
8878c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
8888c2ecf20Sopenharmony_ci						 work.work);
8898c2ecf20Sopenharmony_ci	int ret;
8908c2ecf20Sopenharmony_ci	int error;
8918c2ecf20Sopenharmony_ci	int boost;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	if (bq->automode > 0 && (bq->reported_mode != bq->mode)) {
8948c2ecf20Sopenharmony_ci		sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode");
8958c2ecf20Sopenharmony_ci		bq2415x_set_mode(bq, bq->reported_mode);
8968c2ecf20Sopenharmony_ci	}
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	if (!bq->autotimer)
8998c2ecf20Sopenharmony_ci		return;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
9028c2ecf20Sopenharmony_ci	if (ret < 0) {
9038c2ecf20Sopenharmony_ci		bq2415x_timer_error(bq, "Resetting timer failed");
9048c2ecf20Sopenharmony_ci		return;
9058c2ecf20Sopenharmony_ci	}
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
9088c2ecf20Sopenharmony_ci	if (boost < 0) {
9098c2ecf20Sopenharmony_ci		bq2415x_timer_error(bq, "Unknown error");
9108c2ecf20Sopenharmony_ci		return;
9118c2ecf20Sopenharmony_ci	}
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
9148c2ecf20Sopenharmony_ci	if (error < 0) {
9158c2ecf20Sopenharmony_ci		bq2415x_timer_error(bq, "Unknown error");
9168c2ecf20Sopenharmony_ci		return;
9178c2ecf20Sopenharmony_ci	}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	if (boost) {
9208c2ecf20Sopenharmony_ci		switch (error) {
9218c2ecf20Sopenharmony_ci		/* Non fatal errors, chip is OK */
9228c2ecf20Sopenharmony_ci		case 0: /* No error */
9238c2ecf20Sopenharmony_ci			break;
9248c2ecf20Sopenharmony_ci		case 6: /* Timer expired */
9258c2ecf20Sopenharmony_ci			dev_err(bq->dev, "Timer expired\n");
9268c2ecf20Sopenharmony_ci			break;
9278c2ecf20Sopenharmony_ci		case 3: /* Battery voltage too low */
9288c2ecf20Sopenharmony_ci			dev_err(bq->dev, "Battery voltage to low\n");
9298c2ecf20Sopenharmony_ci			break;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci		/* Fatal errors, disable and reset chip */
9328c2ecf20Sopenharmony_ci		case 1: /* Overvoltage protection (chip fried) */
9338c2ecf20Sopenharmony_ci			bq2415x_timer_error(bq,
9348c2ecf20Sopenharmony_ci				"Overvoltage protection (chip fried)");
9358c2ecf20Sopenharmony_ci			return;
9368c2ecf20Sopenharmony_ci		case 2: /* Overload */
9378c2ecf20Sopenharmony_ci			bq2415x_timer_error(bq, "Overload");
9388c2ecf20Sopenharmony_ci			return;
9398c2ecf20Sopenharmony_ci		case 4: /* Battery overvoltage protection */
9408c2ecf20Sopenharmony_ci			bq2415x_timer_error(bq,
9418c2ecf20Sopenharmony_ci				"Battery overvoltage protection");
9428c2ecf20Sopenharmony_ci			return;
9438c2ecf20Sopenharmony_ci		case 5: /* Thermal shutdown (too hot) */
9448c2ecf20Sopenharmony_ci			bq2415x_timer_error(bq,
9458c2ecf20Sopenharmony_ci					"Thermal shutdown (too hot)");
9468c2ecf20Sopenharmony_ci			return;
9478c2ecf20Sopenharmony_ci		case 7: /* N/A */
9488c2ecf20Sopenharmony_ci			bq2415x_timer_error(bq, "Unknown error");
9498c2ecf20Sopenharmony_ci			return;
9508c2ecf20Sopenharmony_ci		}
9518c2ecf20Sopenharmony_ci	} else {
9528c2ecf20Sopenharmony_ci		switch (error) {
9538c2ecf20Sopenharmony_ci		/* Non fatal errors, chip is OK */
9548c2ecf20Sopenharmony_ci		case 0: /* No error */
9558c2ecf20Sopenharmony_ci			break;
9568c2ecf20Sopenharmony_ci		case 2: /* Sleep mode */
9578c2ecf20Sopenharmony_ci			dev_err(bq->dev, "Sleep mode\n");
9588c2ecf20Sopenharmony_ci			break;
9598c2ecf20Sopenharmony_ci		case 3: /* Poor input source */
9608c2ecf20Sopenharmony_ci			dev_err(bq->dev, "Poor input source\n");
9618c2ecf20Sopenharmony_ci			break;
9628c2ecf20Sopenharmony_ci		case 6: /* Timer expired */
9638c2ecf20Sopenharmony_ci			dev_err(bq->dev, "Timer expired\n");
9648c2ecf20Sopenharmony_ci			break;
9658c2ecf20Sopenharmony_ci		case 7: /* No battery */
9668c2ecf20Sopenharmony_ci			dev_err(bq->dev, "No battery\n");
9678c2ecf20Sopenharmony_ci			break;
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci		/* Fatal errors, disable and reset chip */
9708c2ecf20Sopenharmony_ci		case 1: /* Overvoltage protection (chip fried) */
9718c2ecf20Sopenharmony_ci			bq2415x_timer_error(bq,
9728c2ecf20Sopenharmony_ci				"Overvoltage protection (chip fried)");
9738c2ecf20Sopenharmony_ci			return;
9748c2ecf20Sopenharmony_ci		case 4: /* Battery overvoltage protection */
9758c2ecf20Sopenharmony_ci			bq2415x_timer_error(bq,
9768c2ecf20Sopenharmony_ci				"Battery overvoltage protection");
9778c2ecf20Sopenharmony_ci			return;
9788c2ecf20Sopenharmony_ci		case 5: /* Thermal shutdown (too hot) */
9798c2ecf20Sopenharmony_ci			bq2415x_timer_error(bq,
9808c2ecf20Sopenharmony_ci				"Thermal shutdown (too hot)");
9818c2ecf20Sopenharmony_ci			return;
9828c2ecf20Sopenharmony_ci		}
9838c2ecf20Sopenharmony_ci	}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
9868c2ecf20Sopenharmony_ci}
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci/**** power supply interface code ****/
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_cistatic enum power_supply_property bq2415x_power_supply_props[] = {
9918c2ecf20Sopenharmony_ci	/* TODO: maybe add more power supply properties */
9928c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_STATUS,
9938c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_MODEL_NAME,
9948c2ecf20Sopenharmony_ci};
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_cistatic int bq2415x_power_supply_get_property(struct power_supply *psy,
9978c2ecf20Sopenharmony_ci					     enum power_supply_property psp,
9988c2ecf20Sopenharmony_ci					     union power_supply_propval *val)
9998c2ecf20Sopenharmony_ci{
10008c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
10018c2ecf20Sopenharmony_ci	int ret;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	switch (psp) {
10048c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_STATUS:
10058c2ecf20Sopenharmony_ci		ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
10068c2ecf20Sopenharmony_ci		if (ret < 0)
10078c2ecf20Sopenharmony_ci			return ret;
10088c2ecf20Sopenharmony_ci		else if (ret == 0) /* Ready */
10098c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
10108c2ecf20Sopenharmony_ci		else if (ret == 1) /* Charge in progress */
10118c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_STATUS_CHARGING;
10128c2ecf20Sopenharmony_ci		else if (ret == 2) /* Charge done */
10138c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_STATUS_FULL;
10148c2ecf20Sopenharmony_ci		else
10158c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
10168c2ecf20Sopenharmony_ci		break;
10178c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_MODEL_NAME:
10188c2ecf20Sopenharmony_ci		val->strval = bq->model;
10198c2ecf20Sopenharmony_ci		break;
10208c2ecf20Sopenharmony_ci	default:
10218c2ecf20Sopenharmony_ci		return -EINVAL;
10228c2ecf20Sopenharmony_ci	}
10238c2ecf20Sopenharmony_ci	return 0;
10248c2ecf20Sopenharmony_ci}
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_cistatic void bq2415x_power_supply_exit(struct bq2415x_device *bq)
10278c2ecf20Sopenharmony_ci{
10288c2ecf20Sopenharmony_ci	bq->autotimer = 0;
10298c2ecf20Sopenharmony_ci	if (bq->automode > 0)
10308c2ecf20Sopenharmony_ci		bq->automode = 0;
10318c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&bq->work);
10328c2ecf20Sopenharmony_ci	power_supply_unregister(bq->charger);
10338c2ecf20Sopenharmony_ci	kfree(bq->model);
10348c2ecf20Sopenharmony_ci}
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci/**** additional sysfs entries for power supply interface ****/
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci/* show *_status entries */
10398c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_show_status(struct device *dev,
10408c2ecf20Sopenharmony_ci					 struct device_attribute *attr,
10418c2ecf20Sopenharmony_ci					 char *buf)
10428c2ecf20Sopenharmony_ci{
10438c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
10448c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
10458c2ecf20Sopenharmony_ci	enum bq2415x_command command;
10468c2ecf20Sopenharmony_ci	int ret;
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	if (strcmp(attr->attr.name, "otg_status") == 0)
10498c2ecf20Sopenharmony_ci		command = BQ2415X_OTG_STATUS;
10508c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "charge_status") == 0)
10518c2ecf20Sopenharmony_ci		command = BQ2415X_CHARGE_STATUS;
10528c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "boost_status") == 0)
10538c2ecf20Sopenharmony_ci		command = BQ2415X_BOOST_STATUS;
10548c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "fault_status") == 0)
10558c2ecf20Sopenharmony_ci		command = BQ2415X_FAULT_STATUS;
10568c2ecf20Sopenharmony_ci	else
10578c2ecf20Sopenharmony_ci		return -EINVAL;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	ret = bq2415x_exec_command(bq, command);
10608c2ecf20Sopenharmony_ci	if (ret < 0)
10618c2ecf20Sopenharmony_ci		return ret;
10628c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", ret);
10638c2ecf20Sopenharmony_ci}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci/*
10668c2ecf20Sopenharmony_ci * set timer entry:
10678c2ecf20Sopenharmony_ci *    auto - enable auto mode
10688c2ecf20Sopenharmony_ci *    off - disable auto mode
10698c2ecf20Sopenharmony_ci *    (other values) - reset chip timer
10708c2ecf20Sopenharmony_ci */
10718c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_set_timer(struct device *dev,
10728c2ecf20Sopenharmony_ci				       struct device_attribute *attr,
10738c2ecf20Sopenharmony_ci				       const char *buf,
10748c2ecf20Sopenharmony_ci				       size_t count)
10758c2ecf20Sopenharmony_ci{
10768c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
10778c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
10788c2ecf20Sopenharmony_ci	int ret = 0;
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	if (strncmp(buf, "auto", 4) == 0)
10818c2ecf20Sopenharmony_ci		bq2415x_set_autotimer(bq, 1);
10828c2ecf20Sopenharmony_ci	else if (strncmp(buf, "off", 3) == 0)
10838c2ecf20Sopenharmony_ci		bq2415x_set_autotimer(bq, 0);
10848c2ecf20Sopenharmony_ci	else
10858c2ecf20Sopenharmony_ci		ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	if (ret < 0)
10888c2ecf20Sopenharmony_ci		return ret;
10898c2ecf20Sopenharmony_ci	return count;
10908c2ecf20Sopenharmony_ci}
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci/* show timer entry (auto or off) */
10938c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_show_timer(struct device *dev,
10948c2ecf20Sopenharmony_ci					struct device_attribute *attr,
10958c2ecf20Sopenharmony_ci					char *buf)
10968c2ecf20Sopenharmony_ci{
10978c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
10988c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	if (bq->timer_error)
11018c2ecf20Sopenharmony_ci		return sprintf(buf, "%s\n", bq->timer_error);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	if (bq->autotimer)
11048c2ecf20Sopenharmony_ci		return sprintf(buf, "auto\n");
11058c2ecf20Sopenharmony_ci	return sprintf(buf, "off\n");
11068c2ecf20Sopenharmony_ci}
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci/*
11098c2ecf20Sopenharmony_ci * set mode entry:
11108c2ecf20Sopenharmony_ci *    auto - if automode is supported, enable it and set mode to reported
11118c2ecf20Sopenharmony_ci *    none - disable charger and boost mode
11128c2ecf20Sopenharmony_ci *    host - charging mode for host/hub chargers (current limit 500mA)
11138c2ecf20Sopenharmony_ci *    dedicated - charging mode for dedicated chargers (unlimited current limit)
11148c2ecf20Sopenharmony_ci *    boost - disable charger and enable boost mode
11158c2ecf20Sopenharmony_ci */
11168c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_set_mode(struct device *dev,
11178c2ecf20Sopenharmony_ci				      struct device_attribute *attr,
11188c2ecf20Sopenharmony_ci				      const char *buf,
11198c2ecf20Sopenharmony_ci				      size_t count)
11208c2ecf20Sopenharmony_ci{
11218c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
11228c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
11238c2ecf20Sopenharmony_ci	enum bq2415x_mode mode;
11248c2ecf20Sopenharmony_ci	int ret = 0;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	if (strncmp(buf, "auto", 4) == 0) {
11278c2ecf20Sopenharmony_ci		if (bq->automode < 0)
11288c2ecf20Sopenharmony_ci			return -EINVAL;
11298c2ecf20Sopenharmony_ci		bq->automode = 1;
11308c2ecf20Sopenharmony_ci		mode = bq->reported_mode;
11318c2ecf20Sopenharmony_ci	} else if (strncmp(buf, "off", 3) == 0) {
11328c2ecf20Sopenharmony_ci		if (bq->automode > 0)
11338c2ecf20Sopenharmony_ci			bq->automode = 0;
11348c2ecf20Sopenharmony_ci		mode = BQ2415X_MODE_OFF;
11358c2ecf20Sopenharmony_ci	} else if (strncmp(buf, "none", 4) == 0) {
11368c2ecf20Sopenharmony_ci		if (bq->automode > 0)
11378c2ecf20Sopenharmony_ci			bq->automode = 0;
11388c2ecf20Sopenharmony_ci		mode = BQ2415X_MODE_NONE;
11398c2ecf20Sopenharmony_ci	} else if (strncmp(buf, "host", 4) == 0) {
11408c2ecf20Sopenharmony_ci		if (bq->automode > 0)
11418c2ecf20Sopenharmony_ci			bq->automode = 0;
11428c2ecf20Sopenharmony_ci		mode = BQ2415X_MODE_HOST_CHARGER;
11438c2ecf20Sopenharmony_ci	} else if (strncmp(buf, "dedicated", 9) == 0) {
11448c2ecf20Sopenharmony_ci		if (bq->automode > 0)
11458c2ecf20Sopenharmony_ci			bq->automode = 0;
11468c2ecf20Sopenharmony_ci		mode = BQ2415X_MODE_DEDICATED_CHARGER;
11478c2ecf20Sopenharmony_ci	} else if (strncmp(buf, "boost", 5) == 0) {
11488c2ecf20Sopenharmony_ci		if (bq->automode > 0)
11498c2ecf20Sopenharmony_ci			bq->automode = 0;
11508c2ecf20Sopenharmony_ci		mode = BQ2415X_MODE_BOOST;
11518c2ecf20Sopenharmony_ci	} else if (strncmp(buf, "reset", 5) == 0) {
11528c2ecf20Sopenharmony_ci		bq2415x_reset_chip(bq);
11538c2ecf20Sopenharmony_ci		bq2415x_set_defaults(bq);
11548c2ecf20Sopenharmony_ci		if (bq->automode <= 0)
11558c2ecf20Sopenharmony_ci			return count;
11568c2ecf20Sopenharmony_ci		bq->automode = 1;
11578c2ecf20Sopenharmony_ci		mode = bq->reported_mode;
11588c2ecf20Sopenharmony_ci	} else {
11598c2ecf20Sopenharmony_ci		return -EINVAL;
11608c2ecf20Sopenharmony_ci	}
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	ret = bq2415x_set_mode(bq, mode);
11638c2ecf20Sopenharmony_ci	if (ret < 0)
11648c2ecf20Sopenharmony_ci		return ret;
11658c2ecf20Sopenharmony_ci	return count;
11668c2ecf20Sopenharmony_ci}
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci/* show mode entry (auto, none, host, dedicated or boost) */
11698c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_show_mode(struct device *dev,
11708c2ecf20Sopenharmony_ci				       struct device_attribute *attr,
11718c2ecf20Sopenharmony_ci				       char *buf)
11728c2ecf20Sopenharmony_ci{
11738c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
11748c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
11758c2ecf20Sopenharmony_ci	ssize_t ret = 0;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	if (bq->automode > 0)
11788c2ecf20Sopenharmony_ci		ret += sprintf(buf+ret, "auto (");
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci	switch (bq->mode) {
11818c2ecf20Sopenharmony_ci	case BQ2415X_MODE_OFF:
11828c2ecf20Sopenharmony_ci		ret += sprintf(buf+ret, "off");
11838c2ecf20Sopenharmony_ci		break;
11848c2ecf20Sopenharmony_ci	case BQ2415X_MODE_NONE:
11858c2ecf20Sopenharmony_ci		ret += sprintf(buf+ret, "none");
11868c2ecf20Sopenharmony_ci		break;
11878c2ecf20Sopenharmony_ci	case BQ2415X_MODE_HOST_CHARGER:
11888c2ecf20Sopenharmony_ci		ret += sprintf(buf+ret, "host");
11898c2ecf20Sopenharmony_ci		break;
11908c2ecf20Sopenharmony_ci	case BQ2415X_MODE_DEDICATED_CHARGER:
11918c2ecf20Sopenharmony_ci		ret += sprintf(buf+ret, "dedicated");
11928c2ecf20Sopenharmony_ci		break;
11938c2ecf20Sopenharmony_ci	case BQ2415X_MODE_BOOST:
11948c2ecf20Sopenharmony_ci		ret += sprintf(buf+ret, "boost");
11958c2ecf20Sopenharmony_ci		break;
11968c2ecf20Sopenharmony_ci	}
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci	if (bq->automode > 0)
11998c2ecf20Sopenharmony_ci		ret += sprintf(buf+ret, ")");
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	ret += sprintf(buf+ret, "\n");
12028c2ecf20Sopenharmony_ci	return ret;
12038c2ecf20Sopenharmony_ci}
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci/* show reported_mode entry (none, host, dedicated or boost) */
12068c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
12078c2ecf20Sopenharmony_ci						struct device_attribute *attr,
12088c2ecf20Sopenharmony_ci						char *buf)
12098c2ecf20Sopenharmony_ci{
12108c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
12118c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	if (bq->automode < 0)
12148c2ecf20Sopenharmony_ci		return -EINVAL;
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	switch (bq->reported_mode) {
12178c2ecf20Sopenharmony_ci	case BQ2415X_MODE_OFF:
12188c2ecf20Sopenharmony_ci		return sprintf(buf, "off\n");
12198c2ecf20Sopenharmony_ci	case BQ2415X_MODE_NONE:
12208c2ecf20Sopenharmony_ci		return sprintf(buf, "none\n");
12218c2ecf20Sopenharmony_ci	case BQ2415X_MODE_HOST_CHARGER:
12228c2ecf20Sopenharmony_ci		return sprintf(buf, "host\n");
12238c2ecf20Sopenharmony_ci	case BQ2415X_MODE_DEDICATED_CHARGER:
12248c2ecf20Sopenharmony_ci		return sprintf(buf, "dedicated\n");
12258c2ecf20Sopenharmony_ci	case BQ2415X_MODE_BOOST:
12268c2ecf20Sopenharmony_ci		return sprintf(buf, "boost\n");
12278c2ecf20Sopenharmony_ci	}
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	return -EINVAL;
12308c2ecf20Sopenharmony_ci}
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci/* directly set raw value to chip register, format: 'register value' */
12338c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_set_registers(struct device *dev,
12348c2ecf20Sopenharmony_ci					   struct device_attribute *attr,
12358c2ecf20Sopenharmony_ci					   const char *buf,
12368c2ecf20Sopenharmony_ci					   size_t count)
12378c2ecf20Sopenharmony_ci{
12388c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
12398c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
12408c2ecf20Sopenharmony_ci	ssize_t ret = 0;
12418c2ecf20Sopenharmony_ci	unsigned int reg;
12428c2ecf20Sopenharmony_ci	unsigned int val;
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	if (sscanf(buf, "%x %x", &reg, &val) != 2)
12458c2ecf20Sopenharmony_ci		return -EINVAL;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	if (reg > 4 || val > 255)
12488c2ecf20Sopenharmony_ci		return -EINVAL;
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	ret = bq2415x_i2c_write(bq, reg, val);
12518c2ecf20Sopenharmony_ci	if (ret < 0)
12528c2ecf20Sopenharmony_ci		return ret;
12538c2ecf20Sopenharmony_ci	return count;
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci/* print value of chip register, format: 'register=value' */
12578c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
12588c2ecf20Sopenharmony_ci				       u8 reg,
12598c2ecf20Sopenharmony_ci				       char *buf)
12608c2ecf20Sopenharmony_ci{
12618c2ecf20Sopenharmony_ci	int ret = bq2415x_i2c_read(bq, reg);
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	if (ret < 0)
12648c2ecf20Sopenharmony_ci		return sprintf(buf, "%#.2x=error %d\n", reg, ret);
12658c2ecf20Sopenharmony_ci	return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
12668c2ecf20Sopenharmony_ci}
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci/* show all raw values of chip register, format per line: 'register=value' */
12698c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_show_registers(struct device *dev,
12708c2ecf20Sopenharmony_ci					    struct device_attribute *attr,
12718c2ecf20Sopenharmony_ci					    char *buf)
12728c2ecf20Sopenharmony_ci{
12738c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
12748c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
12758c2ecf20Sopenharmony_ci	ssize_t ret = 0;
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
12788c2ecf20Sopenharmony_ci	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
12798c2ecf20Sopenharmony_ci	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
12808c2ecf20Sopenharmony_ci	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
12818c2ecf20Sopenharmony_ci	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
12828c2ecf20Sopenharmony_ci	return ret;
12838c2ecf20Sopenharmony_ci}
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci/* set current and voltage limit entries (in mA or mV) */
12868c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_set_limit(struct device *dev,
12878c2ecf20Sopenharmony_ci				       struct device_attribute *attr,
12888c2ecf20Sopenharmony_ci				       const char *buf,
12898c2ecf20Sopenharmony_ci				       size_t count)
12908c2ecf20Sopenharmony_ci{
12918c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
12928c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
12938c2ecf20Sopenharmony_ci	long val;
12948c2ecf20Sopenharmony_ci	int ret;
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	if (kstrtol(buf, 10, &val) < 0)
12978c2ecf20Sopenharmony_ci		return -EINVAL;
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	if (strcmp(attr->attr.name, "current_limit") == 0)
13008c2ecf20Sopenharmony_ci		ret = bq2415x_set_current_limit(bq, val);
13018c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
13028c2ecf20Sopenharmony_ci		ret = bq2415x_set_weak_battery_voltage(bq, val);
13038c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
13048c2ecf20Sopenharmony_ci		ret = bq2415x_set_battery_regulation_voltage(bq, val);
13058c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "charge_current") == 0)
13068c2ecf20Sopenharmony_ci		ret = bq2415x_set_charge_current(bq, val);
13078c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "termination_current") == 0)
13088c2ecf20Sopenharmony_ci		ret = bq2415x_set_termination_current(bq, val);
13098c2ecf20Sopenharmony_ci	else
13108c2ecf20Sopenharmony_ci		return -EINVAL;
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci	if (ret < 0)
13138c2ecf20Sopenharmony_ci		return ret;
13148c2ecf20Sopenharmony_ci	return count;
13158c2ecf20Sopenharmony_ci}
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci/* show current and voltage limit entries (in mA or mV) */
13188c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_show_limit(struct device *dev,
13198c2ecf20Sopenharmony_ci					struct device_attribute *attr,
13208c2ecf20Sopenharmony_ci					char *buf)
13218c2ecf20Sopenharmony_ci{
13228c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
13238c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
13248c2ecf20Sopenharmony_ci	int ret;
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	if (strcmp(attr->attr.name, "current_limit") == 0)
13278c2ecf20Sopenharmony_ci		ret = bq2415x_get_current_limit(bq);
13288c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
13298c2ecf20Sopenharmony_ci		ret = bq2415x_get_weak_battery_voltage(bq);
13308c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
13318c2ecf20Sopenharmony_ci		ret = bq2415x_get_battery_regulation_voltage(bq);
13328c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "charge_current") == 0)
13338c2ecf20Sopenharmony_ci		ret = bq2415x_get_charge_current(bq);
13348c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "termination_current") == 0)
13358c2ecf20Sopenharmony_ci		ret = bq2415x_get_termination_current(bq);
13368c2ecf20Sopenharmony_ci	else
13378c2ecf20Sopenharmony_ci		return -EINVAL;
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	if (ret < 0)
13408c2ecf20Sopenharmony_ci		return ret;
13418c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", ret);
13428c2ecf20Sopenharmony_ci}
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci/* set *_enable entries */
13458c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_set_enable(struct device *dev,
13468c2ecf20Sopenharmony_ci					struct device_attribute *attr,
13478c2ecf20Sopenharmony_ci					const char *buf,
13488c2ecf20Sopenharmony_ci					size_t count)
13498c2ecf20Sopenharmony_ci{
13508c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
13518c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
13528c2ecf20Sopenharmony_ci	enum bq2415x_command command;
13538c2ecf20Sopenharmony_ci	long val;
13548c2ecf20Sopenharmony_ci	int ret;
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	if (kstrtol(buf, 10, &val) < 0)
13578c2ecf20Sopenharmony_ci		return -EINVAL;
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
13608c2ecf20Sopenharmony_ci		command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
13618c2ecf20Sopenharmony_ci			BQ2415X_CHARGE_TERMINATION_DISABLE;
13628c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
13638c2ecf20Sopenharmony_ci		command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
13648c2ecf20Sopenharmony_ci			BQ2415X_HIGH_IMPEDANCE_DISABLE;
13658c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
13668c2ecf20Sopenharmony_ci		command = val ? BQ2415X_OTG_PIN_ENABLE :
13678c2ecf20Sopenharmony_ci			BQ2415X_OTG_PIN_DISABLE;
13688c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
13698c2ecf20Sopenharmony_ci		command = val ? BQ2415X_STAT_PIN_ENABLE :
13708c2ecf20Sopenharmony_ci			BQ2415X_STAT_PIN_DISABLE;
13718c2ecf20Sopenharmony_ci	else
13728c2ecf20Sopenharmony_ci		return -EINVAL;
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	ret = bq2415x_exec_command(bq, command);
13758c2ecf20Sopenharmony_ci	if (ret < 0)
13768c2ecf20Sopenharmony_ci		return ret;
13778c2ecf20Sopenharmony_ci	return count;
13788c2ecf20Sopenharmony_ci}
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci/* show *_enable entries */
13818c2ecf20Sopenharmony_cistatic ssize_t bq2415x_sysfs_show_enable(struct device *dev,
13828c2ecf20Sopenharmony_ci					 struct device_attribute *attr,
13838c2ecf20Sopenharmony_ci					 char *buf)
13848c2ecf20Sopenharmony_ci{
13858c2ecf20Sopenharmony_ci	struct power_supply *psy = dev_get_drvdata(dev);
13868c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
13878c2ecf20Sopenharmony_ci	enum bq2415x_command command;
13888c2ecf20Sopenharmony_ci	int ret;
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci	if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
13918c2ecf20Sopenharmony_ci		command = BQ2415X_CHARGE_TERMINATION_STATUS;
13928c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
13938c2ecf20Sopenharmony_ci		command = BQ2415X_HIGH_IMPEDANCE_STATUS;
13948c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
13958c2ecf20Sopenharmony_ci		command = BQ2415X_OTG_PIN_STATUS;
13968c2ecf20Sopenharmony_ci	else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
13978c2ecf20Sopenharmony_ci		command = BQ2415X_STAT_PIN_STATUS;
13988c2ecf20Sopenharmony_ci	else
13998c2ecf20Sopenharmony_ci		return -EINVAL;
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	ret = bq2415x_exec_command(bq, command);
14028c2ecf20Sopenharmony_ci	if (ret < 0)
14038c2ecf20Sopenharmony_ci		return ret;
14048c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", ret);
14058c2ecf20Sopenharmony_ci}
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_cistatic DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
14088c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
14098c2ecf20Sopenharmony_cistatic DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
14108c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
14118c2ecf20Sopenharmony_cistatic DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
14128c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
14138c2ecf20Sopenharmony_cistatic DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
14148c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
14158c2ecf20Sopenharmony_cistatic DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
14168c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_cistatic DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
14198c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
14208c2ecf20Sopenharmony_cistatic DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
14218c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
14228c2ecf20Sopenharmony_cistatic DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
14238c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
14248c2ecf20Sopenharmony_cistatic DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
14258c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_cistatic DEVICE_ATTR(reported_mode, S_IRUGO,
14288c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_reported_mode, NULL);
14298c2ecf20Sopenharmony_cistatic DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
14308c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
14318c2ecf20Sopenharmony_cistatic DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
14328c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_cistatic DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
14358c2ecf20Sopenharmony_ci		bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_cistatic DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
14388c2ecf20Sopenharmony_cistatic DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
14398c2ecf20Sopenharmony_cistatic DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
14408c2ecf20Sopenharmony_cistatic DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_cistatic struct attribute *bq2415x_sysfs_attrs[] = {
14438c2ecf20Sopenharmony_ci	/*
14448c2ecf20Sopenharmony_ci	 * TODO: some (appropriate) of these attrs should be switched to
14458c2ecf20Sopenharmony_ci	 * use power supply class props.
14468c2ecf20Sopenharmony_ci	 */
14478c2ecf20Sopenharmony_ci	&dev_attr_current_limit.attr,
14488c2ecf20Sopenharmony_ci	&dev_attr_weak_battery_voltage.attr,
14498c2ecf20Sopenharmony_ci	&dev_attr_battery_regulation_voltage.attr,
14508c2ecf20Sopenharmony_ci	&dev_attr_charge_current.attr,
14518c2ecf20Sopenharmony_ci	&dev_attr_termination_current.attr,
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	&dev_attr_charge_termination_enable.attr,
14548c2ecf20Sopenharmony_ci	&dev_attr_high_impedance_enable.attr,
14558c2ecf20Sopenharmony_ci	&dev_attr_otg_pin_enable.attr,
14568c2ecf20Sopenharmony_ci	&dev_attr_stat_pin_enable.attr,
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	&dev_attr_reported_mode.attr,
14598c2ecf20Sopenharmony_ci	&dev_attr_mode.attr,
14608c2ecf20Sopenharmony_ci	&dev_attr_timer.attr,
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci	&dev_attr_registers.attr,
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	&dev_attr_otg_status.attr,
14658c2ecf20Sopenharmony_ci	&dev_attr_charge_status.attr,
14668c2ecf20Sopenharmony_ci	&dev_attr_boost_status.attr,
14678c2ecf20Sopenharmony_ci	&dev_attr_fault_status.attr,
14688c2ecf20Sopenharmony_ci	NULL,
14698c2ecf20Sopenharmony_ci};
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(bq2415x_sysfs);
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_cistatic int bq2415x_power_supply_init(struct bq2415x_device *bq)
14748c2ecf20Sopenharmony_ci{
14758c2ecf20Sopenharmony_ci	int ret;
14768c2ecf20Sopenharmony_ci	int chip;
14778c2ecf20Sopenharmony_ci	char revstr[8];
14788c2ecf20Sopenharmony_ci	struct power_supply_config psy_cfg = {
14798c2ecf20Sopenharmony_ci		.drv_data = bq,
14808c2ecf20Sopenharmony_ci		.of_node = bq->dev->of_node,
14818c2ecf20Sopenharmony_ci		.attr_grp = bq2415x_sysfs_groups,
14828c2ecf20Sopenharmony_ci	};
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci	bq->charger_desc.name = bq->name;
14858c2ecf20Sopenharmony_ci	bq->charger_desc.type = POWER_SUPPLY_TYPE_USB;
14868c2ecf20Sopenharmony_ci	bq->charger_desc.properties = bq2415x_power_supply_props;
14878c2ecf20Sopenharmony_ci	bq->charger_desc.num_properties =
14888c2ecf20Sopenharmony_ci			ARRAY_SIZE(bq2415x_power_supply_props);
14898c2ecf20Sopenharmony_ci	bq->charger_desc.get_property = bq2415x_power_supply_get_property;
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	ret = bq2415x_detect_chip(bq);
14928c2ecf20Sopenharmony_ci	if (ret < 0)
14938c2ecf20Sopenharmony_ci		chip = BQUNKNOWN;
14948c2ecf20Sopenharmony_ci	else
14958c2ecf20Sopenharmony_ci		chip = ret;
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci	ret = bq2415x_detect_revision(bq);
14988c2ecf20Sopenharmony_ci	if (ret < 0)
14998c2ecf20Sopenharmony_ci		strcpy(revstr, "unknown");
15008c2ecf20Sopenharmony_ci	else
15018c2ecf20Sopenharmony_ci		sprintf(revstr, "1.%d", ret);
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	bq->model = kasprintf(GFP_KERNEL,
15048c2ecf20Sopenharmony_ci				"chip %s, revision %s, vender code %.3d",
15058c2ecf20Sopenharmony_ci				bq2415x_chip_name[chip], revstr,
15068c2ecf20Sopenharmony_ci				bq2415x_get_vender_code(bq));
15078c2ecf20Sopenharmony_ci	if (!bq->model) {
15088c2ecf20Sopenharmony_ci		dev_err(bq->dev, "failed to allocate model name\n");
15098c2ecf20Sopenharmony_ci		return -ENOMEM;
15108c2ecf20Sopenharmony_ci	}
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	bq->charger = power_supply_register(bq->dev, &bq->charger_desc,
15138c2ecf20Sopenharmony_ci					    &psy_cfg);
15148c2ecf20Sopenharmony_ci	if (IS_ERR(bq->charger)) {
15158c2ecf20Sopenharmony_ci		kfree(bq->model);
15168c2ecf20Sopenharmony_ci		return PTR_ERR(bq->charger);
15178c2ecf20Sopenharmony_ci	}
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_ci	return 0;
15208c2ecf20Sopenharmony_ci}
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_ci/* main bq2415x probe function */
15238c2ecf20Sopenharmony_cistatic int bq2415x_probe(struct i2c_client *client,
15248c2ecf20Sopenharmony_ci			 const struct i2c_device_id *id)
15258c2ecf20Sopenharmony_ci{
15268c2ecf20Sopenharmony_ci	int ret;
15278c2ecf20Sopenharmony_ci	int num;
15288c2ecf20Sopenharmony_ci	char *name = NULL;
15298c2ecf20Sopenharmony_ci	struct bq2415x_device *bq;
15308c2ecf20Sopenharmony_ci	struct device_node *np = client->dev.of_node;
15318c2ecf20Sopenharmony_ci	struct bq2415x_platform_data *pdata = client->dev.platform_data;
15328c2ecf20Sopenharmony_ci	const struct acpi_device_id *acpi_id = NULL;
15338c2ecf20Sopenharmony_ci	struct power_supply *notify_psy = NULL;
15348c2ecf20Sopenharmony_ci	union power_supply_propval prop;
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci	if (!np && !pdata && !ACPI_HANDLE(&client->dev)) {
15378c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Neither devicetree, nor platform data, nor ACPI support\n");
15388c2ecf20Sopenharmony_ci		return -ENODEV;
15398c2ecf20Sopenharmony_ci	}
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci	/* Get new ID for the new device */
15428c2ecf20Sopenharmony_ci	mutex_lock(&bq2415x_id_mutex);
15438c2ecf20Sopenharmony_ci	num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
15448c2ecf20Sopenharmony_ci	mutex_unlock(&bq2415x_id_mutex);
15458c2ecf20Sopenharmony_ci	if (num < 0)
15468c2ecf20Sopenharmony_ci		return num;
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	if (id) {
15498c2ecf20Sopenharmony_ci		name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
15508c2ecf20Sopenharmony_ci	} else if (ACPI_HANDLE(&client->dev)) {
15518c2ecf20Sopenharmony_ci		acpi_id =
15528c2ecf20Sopenharmony_ci			acpi_match_device(client->dev.driver->acpi_match_table,
15538c2ecf20Sopenharmony_ci					  &client->dev);
15548c2ecf20Sopenharmony_ci		if (!acpi_id) {
15558c2ecf20Sopenharmony_ci			dev_err(&client->dev, "failed to match device name\n");
15568c2ecf20Sopenharmony_ci			ret = -ENODEV;
15578c2ecf20Sopenharmony_ci			goto error_1;
15588c2ecf20Sopenharmony_ci		}
15598c2ecf20Sopenharmony_ci		name = kasprintf(GFP_KERNEL, "%s-%d", acpi_id->id, num);
15608c2ecf20Sopenharmony_ci	}
15618c2ecf20Sopenharmony_ci	if (!name) {
15628c2ecf20Sopenharmony_ci		dev_err(&client->dev, "failed to allocate device name\n");
15638c2ecf20Sopenharmony_ci		ret = -ENOMEM;
15648c2ecf20Sopenharmony_ci		goto error_1;
15658c2ecf20Sopenharmony_ci	}
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci	bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
15688c2ecf20Sopenharmony_ci	if (!bq) {
15698c2ecf20Sopenharmony_ci		ret = -ENOMEM;
15708c2ecf20Sopenharmony_ci		goto error_2;
15718c2ecf20Sopenharmony_ci	}
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, bq);
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci	bq->id = num;
15768c2ecf20Sopenharmony_ci	bq->dev = &client->dev;
15778c2ecf20Sopenharmony_ci	if (id)
15788c2ecf20Sopenharmony_ci		bq->chip = id->driver_data;
15798c2ecf20Sopenharmony_ci	else if (ACPI_HANDLE(bq->dev))
15808c2ecf20Sopenharmony_ci		bq->chip = acpi_id->driver_data;
15818c2ecf20Sopenharmony_ci	bq->name = name;
15828c2ecf20Sopenharmony_ci	bq->mode = BQ2415X_MODE_OFF;
15838c2ecf20Sopenharmony_ci	bq->reported_mode = BQ2415X_MODE_OFF;
15848c2ecf20Sopenharmony_ci	bq->autotimer = 0;
15858c2ecf20Sopenharmony_ci	bq->automode = 0;
15868c2ecf20Sopenharmony_ci
15878c2ecf20Sopenharmony_ci	if (np || ACPI_HANDLE(bq->dev)) {
15888c2ecf20Sopenharmony_ci		ret = device_property_read_u32(bq->dev,
15898c2ecf20Sopenharmony_ci					       "ti,current-limit",
15908c2ecf20Sopenharmony_ci					       &bq->init_data.current_limit);
15918c2ecf20Sopenharmony_ci		if (ret)
15928c2ecf20Sopenharmony_ci			goto error_2;
15938c2ecf20Sopenharmony_ci		ret = device_property_read_u32(bq->dev,
15948c2ecf20Sopenharmony_ci					"ti,weak-battery-voltage",
15958c2ecf20Sopenharmony_ci					&bq->init_data.weak_battery_voltage);
15968c2ecf20Sopenharmony_ci		if (ret)
15978c2ecf20Sopenharmony_ci			goto error_2;
15988c2ecf20Sopenharmony_ci		ret = device_property_read_u32(bq->dev,
15998c2ecf20Sopenharmony_ci				"ti,battery-regulation-voltage",
16008c2ecf20Sopenharmony_ci				&bq->init_data.battery_regulation_voltage);
16018c2ecf20Sopenharmony_ci		if (ret)
16028c2ecf20Sopenharmony_ci			goto error_2;
16038c2ecf20Sopenharmony_ci		ret = device_property_read_u32(bq->dev,
16048c2ecf20Sopenharmony_ci					       "ti,charge-current",
16058c2ecf20Sopenharmony_ci					       &bq->init_data.charge_current);
16068c2ecf20Sopenharmony_ci		if (ret)
16078c2ecf20Sopenharmony_ci			goto error_2;
16088c2ecf20Sopenharmony_ci		ret = device_property_read_u32(bq->dev,
16098c2ecf20Sopenharmony_ci				"ti,termination-current",
16108c2ecf20Sopenharmony_ci				&bq->init_data.termination_current);
16118c2ecf20Sopenharmony_ci		if (ret)
16128c2ecf20Sopenharmony_ci			goto error_2;
16138c2ecf20Sopenharmony_ci		ret = device_property_read_u32(bq->dev,
16148c2ecf20Sopenharmony_ci					       "ti,resistor-sense",
16158c2ecf20Sopenharmony_ci					       &bq->init_data.resistor_sense);
16168c2ecf20Sopenharmony_ci		if (ret)
16178c2ecf20Sopenharmony_ci			goto error_2;
16188c2ecf20Sopenharmony_ci		if (np)
16198c2ecf20Sopenharmony_ci			bq->notify_node = of_parse_phandle(np,
16208c2ecf20Sopenharmony_ci						"ti,usb-charger-detection", 0);
16218c2ecf20Sopenharmony_ci	} else {
16228c2ecf20Sopenharmony_ci		memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
16238c2ecf20Sopenharmony_ci	}
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	bq2415x_reset_chip(bq);
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	ret = bq2415x_power_supply_init(bq);
16288c2ecf20Sopenharmony_ci	if (ret) {
16298c2ecf20Sopenharmony_ci		dev_err(bq->dev, "failed to register power supply: %d\n", ret);
16308c2ecf20Sopenharmony_ci		goto error_2;
16318c2ecf20Sopenharmony_ci	}
16328c2ecf20Sopenharmony_ci
16338c2ecf20Sopenharmony_ci	ret = bq2415x_set_defaults(bq);
16348c2ecf20Sopenharmony_ci	if (ret) {
16358c2ecf20Sopenharmony_ci		dev_err(bq->dev, "failed to set default values: %d\n", ret);
16368c2ecf20Sopenharmony_ci		goto error_3;
16378c2ecf20Sopenharmony_ci	}
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci	if (bq->notify_node || bq->init_data.notify_device) {
16408c2ecf20Sopenharmony_ci		bq->nb.notifier_call = bq2415x_notifier_call;
16418c2ecf20Sopenharmony_ci		ret = power_supply_reg_notifier(&bq->nb);
16428c2ecf20Sopenharmony_ci		if (ret) {
16438c2ecf20Sopenharmony_ci			dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
16448c2ecf20Sopenharmony_ci			goto error_3;
16458c2ecf20Sopenharmony_ci		}
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci		bq->automode = 1;
16488c2ecf20Sopenharmony_ci		dev_info(bq->dev, "automode supported, waiting for events\n");
16498c2ecf20Sopenharmony_ci	} else {
16508c2ecf20Sopenharmony_ci		bq->automode = -1;
16518c2ecf20Sopenharmony_ci		dev_info(bq->dev, "automode not supported\n");
16528c2ecf20Sopenharmony_ci	}
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci	/* Query for initial reported_mode and set it */
16558c2ecf20Sopenharmony_ci	if (bq->nb.notifier_call) {
16568c2ecf20Sopenharmony_ci		if (np) {
16578c2ecf20Sopenharmony_ci			notify_psy = power_supply_get_by_phandle(np,
16588c2ecf20Sopenharmony_ci						"ti,usb-charger-detection");
16598c2ecf20Sopenharmony_ci			if (IS_ERR(notify_psy))
16608c2ecf20Sopenharmony_ci				notify_psy = NULL;
16618c2ecf20Sopenharmony_ci		} else if (bq->init_data.notify_device) {
16628c2ecf20Sopenharmony_ci			notify_psy = power_supply_get_by_name(
16638c2ecf20Sopenharmony_ci						bq->init_data.notify_device);
16648c2ecf20Sopenharmony_ci		}
16658c2ecf20Sopenharmony_ci	}
16668c2ecf20Sopenharmony_ci	if (notify_psy) {
16678c2ecf20Sopenharmony_ci		ret = power_supply_get_property(notify_psy,
16688c2ecf20Sopenharmony_ci					POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
16698c2ecf20Sopenharmony_ci		power_supply_put(notify_psy);
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci		if (ret == 0) {
16728c2ecf20Sopenharmony_ci			bq2415x_update_reported_mode(bq, prop.intval);
16738c2ecf20Sopenharmony_ci			bq2415x_set_mode(bq, bq->reported_mode);
16748c2ecf20Sopenharmony_ci		}
16758c2ecf20Sopenharmony_ci	}
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
16788c2ecf20Sopenharmony_ci	bq2415x_set_autotimer(bq, 1);
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci	dev_info(bq->dev, "driver registered\n");
16818c2ecf20Sopenharmony_ci	return 0;
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_cierror_3:
16848c2ecf20Sopenharmony_ci	bq2415x_power_supply_exit(bq);
16858c2ecf20Sopenharmony_cierror_2:
16868c2ecf20Sopenharmony_ci	if (bq)
16878c2ecf20Sopenharmony_ci		of_node_put(bq->notify_node);
16888c2ecf20Sopenharmony_ci	kfree(name);
16898c2ecf20Sopenharmony_cierror_1:
16908c2ecf20Sopenharmony_ci	mutex_lock(&bq2415x_id_mutex);
16918c2ecf20Sopenharmony_ci	idr_remove(&bq2415x_id, num);
16928c2ecf20Sopenharmony_ci	mutex_unlock(&bq2415x_id_mutex);
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	return ret;
16958c2ecf20Sopenharmony_ci}
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_ci/* main bq2415x remove function */
16988c2ecf20Sopenharmony_ci
16998c2ecf20Sopenharmony_cistatic int bq2415x_remove(struct i2c_client *client)
17008c2ecf20Sopenharmony_ci{
17018c2ecf20Sopenharmony_ci	struct bq2415x_device *bq = i2c_get_clientdata(client);
17028c2ecf20Sopenharmony_ci
17038c2ecf20Sopenharmony_ci	if (bq->nb.notifier_call)
17048c2ecf20Sopenharmony_ci		power_supply_unreg_notifier(&bq->nb);
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci	of_node_put(bq->notify_node);
17078c2ecf20Sopenharmony_ci	bq2415x_power_supply_exit(bq);
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	bq2415x_reset_chip(bq);
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_ci	mutex_lock(&bq2415x_id_mutex);
17128c2ecf20Sopenharmony_ci	idr_remove(&bq2415x_id, bq->id);
17138c2ecf20Sopenharmony_ci	mutex_unlock(&bq2415x_id_mutex);
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ci	dev_info(bq->dev, "driver unregistered\n");
17168c2ecf20Sopenharmony_ci
17178c2ecf20Sopenharmony_ci	kfree(bq->name);
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci	return 0;
17208c2ecf20Sopenharmony_ci}
17218c2ecf20Sopenharmony_ci
17228c2ecf20Sopenharmony_cistatic const struct i2c_device_id bq2415x_i2c_id_table[] = {
17238c2ecf20Sopenharmony_ci	{ "bq2415x", BQUNKNOWN },
17248c2ecf20Sopenharmony_ci	{ "bq24150", BQ24150 },
17258c2ecf20Sopenharmony_ci	{ "bq24150a", BQ24150A },
17268c2ecf20Sopenharmony_ci	{ "bq24151", BQ24151 },
17278c2ecf20Sopenharmony_ci	{ "bq24151a", BQ24151A },
17288c2ecf20Sopenharmony_ci	{ "bq24152", BQ24152 },
17298c2ecf20Sopenharmony_ci	{ "bq24153", BQ24153 },
17308c2ecf20Sopenharmony_ci	{ "bq24153a", BQ24153A },
17318c2ecf20Sopenharmony_ci	{ "bq24155", BQ24155 },
17328c2ecf20Sopenharmony_ci	{ "bq24156", BQ24156 },
17338c2ecf20Sopenharmony_ci	{ "bq24156a", BQ24156A },
17348c2ecf20Sopenharmony_ci	{ "bq24157s", BQ24157S },
17358c2ecf20Sopenharmony_ci	{ "bq24158", BQ24158 },
17368c2ecf20Sopenharmony_ci	{},
17378c2ecf20Sopenharmony_ci};
17388c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
17398c2ecf20Sopenharmony_ci
17408c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI
17418c2ecf20Sopenharmony_cistatic const struct acpi_device_id bq2415x_i2c_acpi_match[] = {
17428c2ecf20Sopenharmony_ci	{ "BQ2415X", BQUNKNOWN },
17438c2ecf20Sopenharmony_ci	{ "BQ241500", BQ24150 },
17448c2ecf20Sopenharmony_ci	{ "BQA24150", BQ24150A },
17458c2ecf20Sopenharmony_ci	{ "BQ241510", BQ24151 },
17468c2ecf20Sopenharmony_ci	{ "BQA24151", BQ24151A },
17478c2ecf20Sopenharmony_ci	{ "BQ241520", BQ24152 },
17488c2ecf20Sopenharmony_ci	{ "BQ241530", BQ24153 },
17498c2ecf20Sopenharmony_ci	{ "BQA24153", BQ24153A },
17508c2ecf20Sopenharmony_ci	{ "BQ241550", BQ24155 },
17518c2ecf20Sopenharmony_ci	{ "BQ241560", BQ24156 },
17528c2ecf20Sopenharmony_ci	{ "BQA24156", BQ24156A },
17538c2ecf20Sopenharmony_ci	{ "BQS24157", BQ24157S },
17548c2ecf20Sopenharmony_ci	{ "BQ241580", BQ24158 },
17558c2ecf20Sopenharmony_ci	{},
17568c2ecf20Sopenharmony_ci};
17578c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, bq2415x_i2c_acpi_match);
17588c2ecf20Sopenharmony_ci#endif
17598c2ecf20Sopenharmony_ci
17608c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
17618c2ecf20Sopenharmony_cistatic const struct of_device_id bq2415x_of_match_table[] = {
17628c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24150" },
17638c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24150a" },
17648c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24151" },
17658c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24151a" },
17668c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24152" },
17678c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24153" },
17688c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24153a" },
17698c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24155" },
17708c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24156" },
17718c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24156a" },
17728c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24157s" },
17738c2ecf20Sopenharmony_ci	{ .compatible = "ti,bq24158" },
17748c2ecf20Sopenharmony_ci	{},
17758c2ecf20Sopenharmony_ci};
17768c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, bq2415x_of_match_table);
17778c2ecf20Sopenharmony_ci#endif
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_cistatic struct i2c_driver bq2415x_driver = {
17808c2ecf20Sopenharmony_ci	.driver = {
17818c2ecf20Sopenharmony_ci		.name = "bq2415x-charger",
17828c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(bq2415x_of_match_table),
17838c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(bq2415x_i2c_acpi_match),
17848c2ecf20Sopenharmony_ci	},
17858c2ecf20Sopenharmony_ci	.probe = bq2415x_probe,
17868c2ecf20Sopenharmony_ci	.remove = bq2415x_remove,
17878c2ecf20Sopenharmony_ci	.id_table = bq2415x_i2c_id_table,
17888c2ecf20Sopenharmony_ci};
17898c2ecf20Sopenharmony_cimodule_i2c_driver(bq2415x_driver);
17908c2ecf20Sopenharmony_ci
17918c2ecf20Sopenharmony_ciMODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
17928c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("bq2415x charger driver");
17938c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1794