18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2016, Prodys S.L.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This adds support for sbs-charger compilant chips as defined here:
68c2ecf20Sopenharmony_ci * http://sbs-forum.org/specs/sbc110.pdf
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Implemetation based on sbs-battery.c
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/err.h>
158c2ecf20Sopenharmony_ci#include <linux/power_supply.h>
168c2ecf20Sopenharmony_ci#include <linux/i2c.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/gpio.h>
208c2ecf20Sopenharmony_ci#include <linux/regmap.h>
218c2ecf20Sopenharmony_ci#include <linux/of_gpio.h>
228c2ecf20Sopenharmony_ci#include <linux/bitops.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define SBS_CHARGER_REG_SPEC_INFO		0x11
258c2ecf20Sopenharmony_ci#define SBS_CHARGER_REG_STATUS			0x13
268c2ecf20Sopenharmony_ci#define SBS_CHARGER_REG_ALARM_WARNING		0x16
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define SBS_CHARGER_STATUS_CHARGE_INHIBITED	BIT(0)
298c2ecf20Sopenharmony_ci#define SBS_CHARGER_STATUS_RES_COLD		BIT(9)
308c2ecf20Sopenharmony_ci#define SBS_CHARGER_STATUS_RES_HOT		BIT(10)
318c2ecf20Sopenharmony_ci#define SBS_CHARGER_STATUS_BATTERY_PRESENT	BIT(14)
328c2ecf20Sopenharmony_ci#define SBS_CHARGER_STATUS_AC_PRESENT		BIT(15)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define SBS_CHARGER_POLL_TIME			500
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistruct sbs_info {
378c2ecf20Sopenharmony_ci	struct i2c_client		*client;
388c2ecf20Sopenharmony_ci	struct power_supply		*power_supply;
398c2ecf20Sopenharmony_ci	struct regmap			*regmap;
408c2ecf20Sopenharmony_ci	struct delayed_work		work;
418c2ecf20Sopenharmony_ci	unsigned int			last_state;
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic int sbs_get_property(struct power_supply *psy,
458c2ecf20Sopenharmony_ci			    enum power_supply_property psp,
468c2ecf20Sopenharmony_ci			    union power_supply_propval *val)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct sbs_info *chip = power_supply_get_drvdata(psy);
498c2ecf20Sopenharmony_ci	unsigned int reg;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	reg = chip->last_state;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	switch (psp) {
548c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_PRESENT:
558c2ecf20Sopenharmony_ci		val->intval = !!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT);
568c2ecf20Sopenharmony_ci		break;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_ONLINE:
598c2ecf20Sopenharmony_ci		val->intval = !!(reg & SBS_CHARGER_STATUS_AC_PRESENT);
608c2ecf20Sopenharmony_ci		break;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_STATUS:
638c2ecf20Sopenharmony_ci		val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci		if (!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT))
668c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
678c2ecf20Sopenharmony_ci		else if (reg & SBS_CHARGER_STATUS_AC_PRESENT &&
688c2ecf20Sopenharmony_ci			 !(reg & SBS_CHARGER_STATUS_CHARGE_INHIBITED))
698c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_STATUS_CHARGING;
708c2ecf20Sopenharmony_ci		else
718c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci		break;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_HEALTH:
768c2ecf20Sopenharmony_ci		if (reg & SBS_CHARGER_STATUS_RES_COLD)
778c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_HEALTH_COLD;
788c2ecf20Sopenharmony_ci		if (reg & SBS_CHARGER_STATUS_RES_HOT)
798c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
808c2ecf20Sopenharmony_ci		else
818c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_HEALTH_GOOD;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		break;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	default:
868c2ecf20Sopenharmony_ci		return -EINVAL;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	return 0;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic int sbs_check_state(struct sbs_info *chip)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	unsigned int reg;
958c2ecf20Sopenharmony_ci	int ret;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &reg);
988c2ecf20Sopenharmony_ci	if (!ret && reg != chip->last_state) {
998c2ecf20Sopenharmony_ci		chip->last_state = reg;
1008c2ecf20Sopenharmony_ci		power_supply_changed(chip->power_supply);
1018c2ecf20Sopenharmony_ci		return 1;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return 0;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic void sbs_delayed_work(struct work_struct *work)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct sbs_info *chip = container_of(work, struct sbs_info, work.work);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	sbs_check_state(chip);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	schedule_delayed_work(&chip->work,
1148c2ecf20Sopenharmony_ci			      msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic irqreturn_t sbs_irq_thread(int irq, void *data)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct sbs_info *chip = data;
1208c2ecf20Sopenharmony_ci	int ret;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	ret = sbs_check_state(chip);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	return ret ? IRQ_HANDLED : IRQ_NONE;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic enum power_supply_property sbs_properties[] = {
1288c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_STATUS,
1298c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_PRESENT,
1308c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_ONLINE,
1318c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_HEALTH,
1328c2ecf20Sopenharmony_ci};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic bool sbs_readable_reg(struct device *dev, unsigned int reg)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	return reg >= SBS_CHARGER_REG_SPEC_INFO;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic bool sbs_volatile_reg(struct device *dev, unsigned int reg)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	switch (reg) {
1428c2ecf20Sopenharmony_ci	case SBS_CHARGER_REG_STATUS:
1438c2ecf20Sopenharmony_ci		return true;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	return false;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic const struct regmap_config sbs_regmap = {
1508c2ecf20Sopenharmony_ci	.reg_bits	= 8,
1518c2ecf20Sopenharmony_ci	.val_bits	= 16,
1528c2ecf20Sopenharmony_ci	.max_register	= SBS_CHARGER_REG_ALARM_WARNING,
1538c2ecf20Sopenharmony_ci	.readable_reg	= sbs_readable_reg,
1548c2ecf20Sopenharmony_ci	.volatile_reg	= sbs_volatile_reg,
1558c2ecf20Sopenharmony_ci	.val_format_endian = REGMAP_ENDIAN_LITTLE, /* since based on SMBus */
1568c2ecf20Sopenharmony_ci};
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic const struct power_supply_desc sbs_desc = {
1598c2ecf20Sopenharmony_ci	.name = "sbs-charger",
1608c2ecf20Sopenharmony_ci	.type = POWER_SUPPLY_TYPE_MAINS,
1618c2ecf20Sopenharmony_ci	.properties = sbs_properties,
1628c2ecf20Sopenharmony_ci	.num_properties = ARRAY_SIZE(sbs_properties),
1638c2ecf20Sopenharmony_ci	.get_property = sbs_get_property,
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic int sbs_probe(struct i2c_client *client,
1678c2ecf20Sopenharmony_ci		     const struct i2c_device_id *id)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct power_supply_config psy_cfg = {};
1708c2ecf20Sopenharmony_ci	struct sbs_info *chip;
1718c2ecf20Sopenharmony_ci	int ret, val;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
1748c2ecf20Sopenharmony_ci	if (!chip)
1758c2ecf20Sopenharmony_ci		return -ENOMEM;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	chip->client = client;
1788c2ecf20Sopenharmony_ci	psy_cfg.of_node = client->dev.of_node;
1798c2ecf20Sopenharmony_ci	psy_cfg.drv_data = chip;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, chip);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	chip->regmap = devm_regmap_init_i2c(client, &sbs_regmap);
1848c2ecf20Sopenharmony_ci	if (IS_ERR(chip->regmap))
1858c2ecf20Sopenharmony_ci		return PTR_ERR(chip->regmap);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/*
1888c2ecf20Sopenharmony_ci	 * Before we register, we need to make sure we can actually talk
1898c2ecf20Sopenharmony_ci	 * to the battery.
1908c2ecf20Sopenharmony_ci	 */
1918c2ecf20Sopenharmony_ci	ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &val);
1928c2ecf20Sopenharmony_ci	if (ret) {
1938c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to get device status\n");
1948c2ecf20Sopenharmony_ci		return ret;
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci	chip->last_state = val;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	chip->power_supply = devm_power_supply_register(&client->dev, &sbs_desc,
1998c2ecf20Sopenharmony_ci							&psy_cfg);
2008c2ecf20Sopenharmony_ci	if (IS_ERR(chip->power_supply)) {
2018c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to register power supply\n");
2028c2ecf20Sopenharmony_ci		return PTR_ERR(chip->power_supply);
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	/*
2068c2ecf20Sopenharmony_ci	 * The sbs-charger spec doesn't impose the use of an interrupt. So in
2078c2ecf20Sopenharmony_ci	 * the case it wasn't provided we use polling in order get the charger's
2088c2ecf20Sopenharmony_ci	 * status.
2098c2ecf20Sopenharmony_ci	 */
2108c2ecf20Sopenharmony_ci	if (client->irq) {
2118c2ecf20Sopenharmony_ci		ret = devm_request_threaded_irq(&client->dev, client->irq,
2128c2ecf20Sopenharmony_ci					NULL, sbs_irq_thread,
2138c2ecf20Sopenharmony_ci					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
2148c2ecf20Sopenharmony_ci					dev_name(&client->dev), chip);
2158c2ecf20Sopenharmony_ci		if (ret) {
2168c2ecf20Sopenharmony_ci			dev_err(&client->dev, "Failed to request irq, %d\n", ret);
2178c2ecf20Sopenharmony_ci			return ret;
2188c2ecf20Sopenharmony_ci		}
2198c2ecf20Sopenharmony_ci	} else {
2208c2ecf20Sopenharmony_ci		INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
2218c2ecf20Sopenharmony_ci		schedule_delayed_work(&chip->work,
2228c2ecf20Sopenharmony_ci				      msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
2238c2ecf20Sopenharmony_ci	}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	dev_info(&client->dev,
2268c2ecf20Sopenharmony_ci		 "%s: smart charger device registered\n", client->name);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	return 0;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic int sbs_remove(struct i2c_client *client)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	struct sbs_info *chip = i2c_get_clientdata(client);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&chip->work);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	return 0;
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
2418c2ecf20Sopenharmony_cistatic const struct of_device_id sbs_dt_ids[] = {
2428c2ecf20Sopenharmony_ci	{ .compatible = "sbs,sbs-charger" },
2438c2ecf20Sopenharmony_ci	{ },
2448c2ecf20Sopenharmony_ci};
2458c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sbs_dt_ids);
2468c2ecf20Sopenharmony_ci#endif
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic const struct i2c_device_id sbs_id[] = {
2498c2ecf20Sopenharmony_ci	{ "sbs-charger", 0 },
2508c2ecf20Sopenharmony_ci	{ }
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, sbs_id);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic struct i2c_driver sbs_driver = {
2558c2ecf20Sopenharmony_ci	.probe		= sbs_probe,
2568c2ecf20Sopenharmony_ci	.remove		= sbs_remove,
2578c2ecf20Sopenharmony_ci	.id_table	= sbs_id,
2588c2ecf20Sopenharmony_ci	.driver = {
2598c2ecf20Sopenharmony_ci		.name	= "sbs-charger",
2608c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(sbs_dt_ids),
2618c2ecf20Sopenharmony_ci	},
2628c2ecf20Sopenharmony_ci};
2638c2ecf20Sopenharmony_cimodule_i2c_driver(sbs_driver);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ciMODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
2668c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SBS smart charger driver");
2678c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
268