18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Motorola CPCAP PMIC battery charger driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Rewritten for Linux power framework with some parts based on
88c2ecf20Sopenharmony_ci * on earlier driver found in the Motorola Linux kernel:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Copyright (C) 2009-2010 Motorola, Inc.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/atomic.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/err.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/notifier.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
228c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
238c2ecf20Sopenharmony_ci#include <linux/power_supply.h>
248c2ecf20Sopenharmony_ci#include <linux/regmap.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
278c2ecf20Sopenharmony_ci#include <linux/usb/phy_companion.h>
288c2ecf20Sopenharmony_ci#include <linux/phy/omap_usb.h>
298c2ecf20Sopenharmony_ci#include <linux/usb/otg.h>
308c2ecf20Sopenharmony_ci#include <linux/iio/consumer.h>
318c2ecf20Sopenharmony_ci#include <linux/mfd/motorola-cpcap.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
358c2ecf20Sopenharmony_ci * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
368c2ecf20Sopenharmony_ci * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
378c2ecf20Sopenharmony_ci * and values for CPCAP are different, but some of the internal components seem
388c2ecf20Sopenharmony_ci * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
398c2ecf20Sopenharmony_ci * bits that seem to describe the CRM register.
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_UNUSED_641_15	BIT(15)	/* 641 = register number */
428c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_UNUSED_641_14	BIT(14)	/* 641 = register number */
438c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_CHRG_LED_EN	BIT(13)	/* Charger LED */
448c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_RVRSMODE		BIT(12)	/* USB VBUS output enable */
458c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_TR1		BIT(11)	/* Trickle charge current */
468c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_TR0		BIT(10)
478c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_FET_OVRD		BIT(9)	/* 0 = hardware, 1 = FET_CTRL */
488c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_FET_CTRL		BIT(8)	/* BPFET 1 if FET_OVRD set */
498c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG3		BIT(7)	/* Charge voltage bits */
508c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG2		BIT(6)
518c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG1		BIT(5)
528c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG0		BIT(4)
538c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG3		BIT(3)	/* Charge current bits */
548c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG2		BIT(2)
558c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG1		BIT(1)
568c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG0		BIT(0)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* CPCAP_REG_CRM trickle charge voltages */
598c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_TR(val)		(((val) & 0x3) << 10)
608c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_TR_0A00		CPCAP_REG_CRM_TR(0x0)
618c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_TR_0A24		CPCAP_REG_CRM_TR(0x1)
628c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_TR_0A48		CPCAP_REG_CRM_TR(0x2)
638c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_TR_0A72		CPCAP_REG_CRM_TR(0x4)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/*
668c2ecf20Sopenharmony_ci * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
678c2ecf20Sopenharmony_ci * Note that these register bits don't match MC13783UG.pdf VCHRG
688c2ecf20Sopenharmony_ci * register bits.
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG(val)	(((val) & 0xf) << 4)
718c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_3V80	CPCAP_REG_CRM_VCHRG(0x0)
728c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V10	CPCAP_REG_CRM_VCHRG(0x1)
738c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V12	CPCAP_REG_CRM_VCHRG(0x2)
748c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V15	CPCAP_REG_CRM_VCHRG(0x3)
758c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V17	CPCAP_REG_CRM_VCHRG(0x4)
768c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V20	CPCAP_REG_CRM_VCHRG(0x5)
778c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V23	CPCAP_REG_CRM_VCHRG(0x6)
788c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V25	CPCAP_REG_CRM_VCHRG(0x7)
798c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V27	CPCAP_REG_CRM_VCHRG(0x8)
808c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V30	CPCAP_REG_CRM_VCHRG(0x9)
818c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V33	CPCAP_REG_CRM_VCHRG(0xa)
828c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V35	CPCAP_REG_CRM_VCHRG(0xb)
838c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V38	CPCAP_REG_CRM_VCHRG(0xc)
848c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V40	CPCAP_REG_CRM_VCHRG(0xd)
858c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V42	CPCAP_REG_CRM_VCHRG(0xe)
868c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_VCHRG_4V44	CPCAP_REG_CRM_VCHRG(0xf)
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/*
898c2ecf20Sopenharmony_ci * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
908c2ecf20Sopenharmony_ci * values in "Table 8-3. Charge Path Regulator Current Limit
918c2ecf20Sopenharmony_ci * Characteristics" for the nominal values.
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG(val)	(((val) & 0xf) << 0)
948c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A000	CPCAP_REG_CRM_ICHRG(0x0)
958c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A070	CPCAP_REG_CRM_ICHRG(0x1)
968c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A177	CPCAP_REG_CRM_ICHRG(0x2)
978c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A266	CPCAP_REG_CRM_ICHRG(0x3)
988c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A355	CPCAP_REG_CRM_ICHRG(0x4)
998c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A443	CPCAP_REG_CRM_ICHRG(0x5)
1008c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A532	CPCAP_REG_CRM_ICHRG(0x6)
1018c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A621	CPCAP_REG_CRM_ICHRG(0x7)
1028c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A709	CPCAP_REG_CRM_ICHRG(0x8)
1038c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A798	CPCAP_REG_CRM_ICHRG(0x9)
1048c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A886	CPCAP_REG_CRM_ICHRG(0xa)
1058c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_0A975	CPCAP_REG_CRM_ICHRG(0xb)
1068c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_1A064	CPCAP_REG_CRM_ICHRG(0xc)
1078c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_1A152	CPCAP_REG_CRM_ICHRG(0xd)
1088c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_1A596	CPCAP_REG_CRM_ICHRG(0xe)
1098c2ecf20Sopenharmony_ci#define CPCAP_REG_CRM_ICHRG_NO_LIMIT	CPCAP_REG_CRM_ICHRG(0xf)
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/* CPCAP_REG_VUSBC register bits needed for VBUS */
1128c2ecf20Sopenharmony_ci#define CPCAP_BIT_VBUS_SWITCH		BIT(0)	/* VBUS boost to 5V */
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cienum {
1158c2ecf20Sopenharmony_ci	CPCAP_CHARGER_IIO_BATTDET,
1168c2ecf20Sopenharmony_ci	CPCAP_CHARGER_IIO_VOLTAGE,
1178c2ecf20Sopenharmony_ci	CPCAP_CHARGER_IIO_VBUS,
1188c2ecf20Sopenharmony_ci	CPCAP_CHARGER_IIO_CHRG_CURRENT,
1198c2ecf20Sopenharmony_ci	CPCAP_CHARGER_IIO_BATT_CURRENT,
1208c2ecf20Sopenharmony_ci	CPCAP_CHARGER_IIO_NR,
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cienum {
1248c2ecf20Sopenharmony_ci	CPCAP_CHARGER_DISCONNECTED,
1258c2ecf20Sopenharmony_ci	CPCAP_CHARGER_DETECTING,
1268c2ecf20Sopenharmony_ci	CPCAP_CHARGER_CHARGING,
1278c2ecf20Sopenharmony_ci	CPCAP_CHARGER_DONE,
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistruct cpcap_charger_ddata {
1318c2ecf20Sopenharmony_ci	struct device *dev;
1328c2ecf20Sopenharmony_ci	struct regmap *reg;
1338c2ecf20Sopenharmony_ci	struct list_head irq_list;
1348c2ecf20Sopenharmony_ci	struct delayed_work detect_work;
1358c2ecf20Sopenharmony_ci	struct delayed_work vbus_work;
1368c2ecf20Sopenharmony_ci	struct gpio_desc *gpio[2];		/* gpio_reven0 & 1 */
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	struct power_supply *usb;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	struct phy_companion comparator;	/* For USB VBUS */
1438c2ecf20Sopenharmony_ci	unsigned int vbus_enabled:1;
1448c2ecf20Sopenharmony_ci	unsigned int feeding_vbus:1;
1458c2ecf20Sopenharmony_ci	atomic_t active;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	int status;
1488c2ecf20Sopenharmony_ci	int state;
1498c2ecf20Sopenharmony_ci	int voltage;
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistruct cpcap_interrupt_desc {
1538c2ecf20Sopenharmony_ci	int irq;
1548c2ecf20Sopenharmony_ci	struct list_head node;
1558c2ecf20Sopenharmony_ci	const char *name;
1568c2ecf20Sopenharmony_ci};
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistruct cpcap_charger_ints_state {
1598c2ecf20Sopenharmony_ci	bool chrg_det;
1608c2ecf20Sopenharmony_ci	bool rvrs_chrg;
1618c2ecf20Sopenharmony_ci	bool vbusov;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	bool chrg_se1b;
1648c2ecf20Sopenharmony_ci	bool rvrs_mode;
1658c2ecf20Sopenharmony_ci	bool chrgcurr2;
1668c2ecf20Sopenharmony_ci	bool chrgcurr1;
1678c2ecf20Sopenharmony_ci	bool vbusvld;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	bool battdetb;
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic enum power_supply_property cpcap_charger_props[] = {
1738c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_STATUS,
1748c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_ONLINE,
1758c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1768c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_VOLTAGE_NOW,
1778c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_CURRENT_NOW,
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/* No battery always shows temperature of -40000 */
1818c2ecf20Sopenharmony_cistatic bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	struct iio_channel *channel;
1848c2ecf20Sopenharmony_ci	int error, temperature;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
1878c2ecf20Sopenharmony_ci	error = iio_read_channel_processed(channel, &temperature);
1888c2ecf20Sopenharmony_ci	if (error < 0) {
1898c2ecf20Sopenharmony_ci		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci		return false;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return temperature > -20000 && temperature < 60000;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct iio_channel *channel;
2008c2ecf20Sopenharmony_ci	int error, value = 0;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
2038c2ecf20Sopenharmony_ci	error = iio_read_channel_processed(channel, &value);
2048c2ecf20Sopenharmony_ci	if (error < 0) {
2058c2ecf20Sopenharmony_ci		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci		return 0;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return value;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct iio_channel *channel;
2168c2ecf20Sopenharmony_ci	int error, value = 0;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
2198c2ecf20Sopenharmony_ci	error = iio_read_channel_processed(channel, &value);
2208c2ecf20Sopenharmony_ci	if (error < 0) {
2218c2ecf20Sopenharmony_ci		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci		return 0;
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	return value;
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic int cpcap_charger_get_property(struct power_supply *psy,
2308c2ecf20Sopenharmony_ci				      enum power_supply_property psp,
2318c2ecf20Sopenharmony_ci				      union power_supply_propval *val)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	switch (psp) {
2368c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_STATUS:
2378c2ecf20Sopenharmony_ci		val->intval = ddata->status;
2388c2ecf20Sopenharmony_ci		break;
2398c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
2408c2ecf20Sopenharmony_ci		val->intval = ddata->voltage;
2418c2ecf20Sopenharmony_ci		break;
2428c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2438c2ecf20Sopenharmony_ci		if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
2448c2ecf20Sopenharmony_ci			val->intval = cpcap_charger_get_charge_voltage(ddata) *
2458c2ecf20Sopenharmony_ci				1000;
2468c2ecf20Sopenharmony_ci		else
2478c2ecf20Sopenharmony_ci			val->intval = 0;
2488c2ecf20Sopenharmony_ci		break;
2498c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_CURRENT_NOW:
2508c2ecf20Sopenharmony_ci		if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
2518c2ecf20Sopenharmony_ci			val->intval = cpcap_charger_get_charge_current(ddata) *
2528c2ecf20Sopenharmony_ci				1000;
2538c2ecf20Sopenharmony_ci		else
2548c2ecf20Sopenharmony_ci			val->intval = 0;
2558c2ecf20Sopenharmony_ci		break;
2568c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_ONLINE:
2578c2ecf20Sopenharmony_ci		val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
2588c2ecf20Sopenharmony_ci		break;
2598c2ecf20Sopenharmony_ci	default:
2608c2ecf20Sopenharmony_ci		return -EINVAL;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return 0;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int cpcap_charger_match_voltage(int voltage)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	switch (voltage) {
2698c2ecf20Sopenharmony_ci	case 0 ... 4100000 - 1: return 3800000;
2708c2ecf20Sopenharmony_ci	case 4100000 ... 4120000 - 1: return 4100000;
2718c2ecf20Sopenharmony_ci	case 4120000 ... 4150000 - 1: return 4120000;
2728c2ecf20Sopenharmony_ci	case 4150000 ... 4170000 - 1: return 4150000;
2738c2ecf20Sopenharmony_ci	case 4170000 ... 4200000 - 1: return 4170000;
2748c2ecf20Sopenharmony_ci	case 4200000 ... 4230000 - 1: return 4200000;
2758c2ecf20Sopenharmony_ci	case 4230000 ... 4250000 - 1: return 4230000;
2768c2ecf20Sopenharmony_ci	case 4250000 ... 4270000 - 1: return 4250000;
2778c2ecf20Sopenharmony_ci	case 4270000 ... 4300000 - 1: return 4270000;
2788c2ecf20Sopenharmony_ci	case 4300000 ... 4330000 - 1: return 4300000;
2798c2ecf20Sopenharmony_ci	case 4330000 ... 4350000 - 1: return 4330000;
2808c2ecf20Sopenharmony_ci	case 4350000 ... 4380000 - 1: return 4350000;
2818c2ecf20Sopenharmony_ci	case 4380000 ... 4400000 - 1: return 4380000;
2828c2ecf20Sopenharmony_ci	case 4400000 ... 4420000 - 1: return 4400000;
2838c2ecf20Sopenharmony_ci	case 4420000 ... 4440000 - 1: return 4420000;
2848c2ecf20Sopenharmony_ci	case 4440000: return 4440000;
2858c2ecf20Sopenharmony_ci	default: return 0;
2868c2ecf20Sopenharmony_ci	}
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic int
2908c2ecf20Sopenharmony_cicpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata *ddata)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	union power_supply_propval prop;
2938c2ecf20Sopenharmony_ci	struct power_supply *battery;
2948c2ecf20Sopenharmony_ci	int voltage = ddata->voltage;
2958c2ecf20Sopenharmony_ci	int error;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	battery = power_supply_get_by_name("battery");
2988c2ecf20Sopenharmony_ci	if (battery) {
2998c2ecf20Sopenharmony_ci		error = power_supply_get_property(battery,
3008c2ecf20Sopenharmony_ci				POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
3018c2ecf20Sopenharmony_ci				&prop);
3028c2ecf20Sopenharmony_ci		if (!error)
3038c2ecf20Sopenharmony_ci			voltage = prop.intval;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci		power_supply_put(battery);
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	return voltage;
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic int cpcap_charger_set_property(struct power_supply *psy,
3128c2ecf20Sopenharmony_ci				      enum power_supply_property psp,
3138c2ecf20Sopenharmony_ci				      const union power_supply_propval *val)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
3168c2ecf20Sopenharmony_ci	int voltage, batvolt;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	switch (psp) {
3198c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
3208c2ecf20Sopenharmony_ci		voltage = cpcap_charger_match_voltage(val->intval);
3218c2ecf20Sopenharmony_ci		batvolt = cpcap_charger_get_bat_const_charge_voltage(ddata);
3228c2ecf20Sopenharmony_ci		if (voltage > batvolt)
3238c2ecf20Sopenharmony_ci			voltage = batvolt;
3248c2ecf20Sopenharmony_ci		ddata->voltage = voltage;
3258c2ecf20Sopenharmony_ci		schedule_delayed_work(&ddata->detect_work, 0);
3268c2ecf20Sopenharmony_ci		break;
3278c2ecf20Sopenharmony_ci	default:
3288c2ecf20Sopenharmony_ci		return -EINVAL;
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	return 0;
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic int cpcap_charger_property_is_writeable(struct power_supply *psy,
3358c2ecf20Sopenharmony_ci					       enum power_supply_property psp)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	switch (psp) {
3388c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
3398c2ecf20Sopenharmony_ci		return 1;
3408c2ecf20Sopenharmony_ci	default:
3418c2ecf20Sopenharmony_ci		return 0;
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
3468c2ecf20Sopenharmony_ci					 bool enabled)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	if (!ddata->gpio[0])
3498c2ecf20Sopenharmony_ci		return;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	gpiod_set_value(ddata->gpio[0], enabled);
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
3558c2ecf20Sopenharmony_ci					     bool enabled)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	if (!ddata->gpio[1])
3588c2ecf20Sopenharmony_ci		return;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	gpiod_set_value(ddata->gpio[1], enabled);
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
3648c2ecf20Sopenharmony_ci				   int max_voltage, int charge_current,
3658c2ecf20Sopenharmony_ci				   int trickle_current)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	bool enable;
3688c2ecf20Sopenharmony_ci	int error;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	enable = (charge_current || trickle_current);
3718c2ecf20Sopenharmony_ci	dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (!enable) {
3748c2ecf20Sopenharmony_ci		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
3758c2ecf20Sopenharmony_ci					   0x3fff,
3768c2ecf20Sopenharmony_ci					   CPCAP_REG_CRM_FET_OVRD |
3778c2ecf20Sopenharmony_ci					   CPCAP_REG_CRM_FET_CTRL);
3788c2ecf20Sopenharmony_ci		if (error) {
3798c2ecf20Sopenharmony_ci			ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
3808c2ecf20Sopenharmony_ci			goto out_err;
3818c2ecf20Sopenharmony_ci		}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci		ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci		return 0;
3868c2ecf20Sopenharmony_ci	}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
3898c2ecf20Sopenharmony_ci				   CPCAP_REG_CRM_CHRG_LED_EN |
3908c2ecf20Sopenharmony_ci				   trickle_current |
3918c2ecf20Sopenharmony_ci				   CPCAP_REG_CRM_FET_OVRD |
3928c2ecf20Sopenharmony_ci				   CPCAP_REG_CRM_FET_CTRL |
3938c2ecf20Sopenharmony_ci				   max_voltage |
3948c2ecf20Sopenharmony_ci				   charge_current);
3958c2ecf20Sopenharmony_ci	if (error) {
3968c2ecf20Sopenharmony_ci		ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
3978c2ecf20Sopenharmony_ci		goto out_err;
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	ddata->status = POWER_SUPPLY_STATUS_CHARGING;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	return 0;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ciout_err:
4058c2ecf20Sopenharmony_ci	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	return error;
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	int error, value = 0;
4138c2ecf20Sopenharmony_ci	struct iio_channel *channel =
4148c2ecf20Sopenharmony_ci		ddata->channels[CPCAP_CHARGER_IIO_VBUS];
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	error = iio_read_channel_processed(channel, &value);
4178c2ecf20Sopenharmony_ci	if (error >= 0)
4188c2ecf20Sopenharmony_ci		return value > 3900 ? true : false;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	dev_err(ddata->dev, "error reading VBUS: %i\n", error);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	return false;
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci/* VBUS control functions for the USB PHY companion */
4268c2ecf20Sopenharmony_cistatic void cpcap_charger_vbus_work(struct work_struct *work)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	struct cpcap_charger_ddata *ddata;
4298c2ecf20Sopenharmony_ci	bool vbus = false;
4308c2ecf20Sopenharmony_ci	int error;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	ddata = container_of(work, struct cpcap_charger_ddata,
4338c2ecf20Sopenharmony_ci			     vbus_work.work);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	if (ddata->vbus_enabled) {
4368c2ecf20Sopenharmony_ci		vbus = cpcap_charger_vbus_valid(ddata);
4378c2ecf20Sopenharmony_ci		if (vbus) {
4388c2ecf20Sopenharmony_ci			dev_info(ddata->dev, "VBUS already provided\n");
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci			return;
4418c2ecf20Sopenharmony_ci		}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci		ddata->feeding_vbus = true;
4448c2ecf20Sopenharmony_ci		cpcap_charger_set_cable_path(ddata, false);
4458c2ecf20Sopenharmony_ci		cpcap_charger_set_inductive_path(ddata, false);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci		error = cpcap_charger_set_state(ddata, 0, 0, 0);
4488c2ecf20Sopenharmony_ci		if (error)
4498c2ecf20Sopenharmony_ci			goto out_err;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci		error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
4528c2ecf20Sopenharmony_ci					   CPCAP_BIT_VBUS_SWITCH,
4538c2ecf20Sopenharmony_ci					   CPCAP_BIT_VBUS_SWITCH);
4548c2ecf20Sopenharmony_ci		if (error)
4558c2ecf20Sopenharmony_ci			goto out_err;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
4588c2ecf20Sopenharmony_ci					   CPCAP_REG_CRM_RVRSMODE,
4598c2ecf20Sopenharmony_ci					   CPCAP_REG_CRM_RVRSMODE);
4608c2ecf20Sopenharmony_ci		if (error)
4618c2ecf20Sopenharmony_ci			goto out_err;
4628c2ecf20Sopenharmony_ci	} else {
4638c2ecf20Sopenharmony_ci		error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
4648c2ecf20Sopenharmony_ci					   CPCAP_BIT_VBUS_SWITCH, 0);
4658c2ecf20Sopenharmony_ci		if (error)
4668c2ecf20Sopenharmony_ci			goto out_err;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
4698c2ecf20Sopenharmony_ci					   CPCAP_REG_CRM_RVRSMODE, 0);
4708c2ecf20Sopenharmony_ci		if (error)
4718c2ecf20Sopenharmony_ci			goto out_err;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci		cpcap_charger_set_cable_path(ddata, true);
4748c2ecf20Sopenharmony_ci		cpcap_charger_set_inductive_path(ddata, true);
4758c2ecf20Sopenharmony_ci		ddata->feeding_vbus = false;
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	return;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ciout_err:
4818c2ecf20Sopenharmony_ci	dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
4828c2ecf20Sopenharmony_ci		ddata->vbus_enabled ? "enable" : "disable", error);
4838c2ecf20Sopenharmony_ci}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_cistatic int cpcap_charger_set_vbus(struct phy_companion *comparator,
4868c2ecf20Sopenharmony_ci				  bool enabled)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	struct cpcap_charger_ddata *ddata =
4898c2ecf20Sopenharmony_ci		container_of(comparator, struct cpcap_charger_ddata,
4908c2ecf20Sopenharmony_ci			     comparator);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	ddata->vbus_enabled = enabled;
4938c2ecf20Sopenharmony_ci	schedule_delayed_work(&ddata->vbus_work, 0);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	return 0;
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci/* Charger interrupt handling functions */
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
5018c2ecf20Sopenharmony_ci					struct cpcap_charger_ints_state *s)
5028c2ecf20Sopenharmony_ci{
5038c2ecf20Sopenharmony_ci	int val, error;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
5068c2ecf20Sopenharmony_ci	if (error)
5078c2ecf20Sopenharmony_ci		return error;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	s->chrg_det = val & BIT(13);
5108c2ecf20Sopenharmony_ci	s->rvrs_chrg = val & BIT(12);
5118c2ecf20Sopenharmony_ci	s->vbusov = val & BIT(11);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
5148c2ecf20Sopenharmony_ci	if (error)
5158c2ecf20Sopenharmony_ci		return error;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	s->chrg_se1b = val & BIT(13);
5188c2ecf20Sopenharmony_ci	s->rvrs_mode = val & BIT(6);
5198c2ecf20Sopenharmony_ci	s->chrgcurr2 = val & BIT(5);
5208c2ecf20Sopenharmony_ci	s->chrgcurr1 = val & BIT(4);
5218c2ecf20Sopenharmony_ci	s->vbusvld = val & BIT(3);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
5248c2ecf20Sopenharmony_ci	if (error)
5258c2ecf20Sopenharmony_ci		return error;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	s->battdetb = val & BIT(6);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	return 0;
5308c2ecf20Sopenharmony_ci}
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_cistatic void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata,
5338c2ecf20Sopenharmony_ci				       int state)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	const char *status;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	if (state > CPCAP_CHARGER_DONE) {
5388c2ecf20Sopenharmony_ci		dev_warn(ddata->dev, "unknown state: %i\n", state);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci		return;
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	ddata->state = state;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	switch (state) {
5468c2ecf20Sopenharmony_ci	case CPCAP_CHARGER_DISCONNECTED:
5478c2ecf20Sopenharmony_ci		status = "DISCONNECTED";
5488c2ecf20Sopenharmony_ci		break;
5498c2ecf20Sopenharmony_ci	case CPCAP_CHARGER_DETECTING:
5508c2ecf20Sopenharmony_ci		status = "DETECTING";
5518c2ecf20Sopenharmony_ci		break;
5528c2ecf20Sopenharmony_ci	case CPCAP_CHARGER_CHARGING:
5538c2ecf20Sopenharmony_ci		status = "CHARGING";
5548c2ecf20Sopenharmony_ci		break;
5558c2ecf20Sopenharmony_ci	case CPCAP_CHARGER_DONE:
5568c2ecf20Sopenharmony_ci		status = "DONE";
5578c2ecf20Sopenharmony_ci		break;
5588c2ecf20Sopenharmony_ci	default:
5598c2ecf20Sopenharmony_ci		return;
5608c2ecf20Sopenharmony_ci	}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	dev_dbg(ddata->dev, "state: %s\n", status);
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic int cpcap_charger_voltage_to_regval(int voltage)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	int offset;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	switch (voltage) {
5708c2ecf20Sopenharmony_ci	case 0 ... 4100000 - 1:
5718c2ecf20Sopenharmony_ci		return 0;
5728c2ecf20Sopenharmony_ci	case 4100000 ... 4200000 - 1:
5738c2ecf20Sopenharmony_ci		offset = 1;
5748c2ecf20Sopenharmony_ci		break;
5758c2ecf20Sopenharmony_ci	case 4200000 ... 4300000 - 1:
5768c2ecf20Sopenharmony_ci		offset = 0;
5778c2ecf20Sopenharmony_ci		break;
5788c2ecf20Sopenharmony_ci	case 4300000 ... 4380000 - 1:
5798c2ecf20Sopenharmony_ci		offset = -1;
5808c2ecf20Sopenharmony_ci		break;
5818c2ecf20Sopenharmony_ci	case 4380000 ... 4440000:
5828c2ecf20Sopenharmony_ci		offset = -2;
5838c2ecf20Sopenharmony_ci		break;
5848c2ecf20Sopenharmony_ci	default:
5858c2ecf20Sopenharmony_ci		return 0;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	return ((voltage - 4100000) / 20000) + offset;
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_cistatic void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata,
5928c2ecf20Sopenharmony_ci				     int state, unsigned long delay)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	int error;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	error = cpcap_charger_set_state(ddata, 0, 0, 0);
5978c2ecf20Sopenharmony_ci	if (error)
5988c2ecf20Sopenharmony_ci		return;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	cpcap_charger_update_state(ddata, state);
6018c2ecf20Sopenharmony_ci	power_supply_changed(ddata->usb);
6028c2ecf20Sopenharmony_ci	schedule_delayed_work(&ddata->detect_work, delay);
6038c2ecf20Sopenharmony_ci}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_cistatic void cpcap_usb_detect(struct work_struct *work)
6068c2ecf20Sopenharmony_ci{
6078c2ecf20Sopenharmony_ci	struct cpcap_charger_ddata *ddata;
6088c2ecf20Sopenharmony_ci	struct cpcap_charger_ints_state s;
6098c2ecf20Sopenharmony_ci	int error;
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	ddata = container_of(work, struct cpcap_charger_ddata,
6128c2ecf20Sopenharmony_ci			     detect_work.work);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	error = cpcap_charger_get_ints_state(ddata, &s);
6158c2ecf20Sopenharmony_ci	if (error)
6168c2ecf20Sopenharmony_ci		return;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	/* Just init the state if a charger is connected with no chrg_det set */
6198c2ecf20Sopenharmony_ci	if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) {
6208c2ecf20Sopenharmony_ci		cpcap_charger_update_state(ddata, CPCAP_CHARGER_DETECTING);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci		return;
6238c2ecf20Sopenharmony_ci	}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	/*
6268c2ecf20Sopenharmony_ci	 * If battery voltage is higher than charge voltage, it may have been
6278c2ecf20Sopenharmony_ci	 * charged to 4.35V by Android. Try again in 10 minutes.
6288c2ecf20Sopenharmony_ci	 */
6298c2ecf20Sopenharmony_ci	if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) {
6308c2ecf20Sopenharmony_ci		cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
6318c2ecf20Sopenharmony_ci					 HZ * 60 * 10);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci		return;
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	/* Delay for 80ms to avoid vbus bouncing when usb cable is plugged in */
6378c2ecf20Sopenharmony_ci	usleep_range(80000, 120000);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	/* Throttle chrgcurr2 interrupt for charger done and retry */
6408c2ecf20Sopenharmony_ci	switch (ddata->state) {
6418c2ecf20Sopenharmony_ci	case CPCAP_CHARGER_CHARGING:
6428c2ecf20Sopenharmony_ci		if (s.chrgcurr2)
6438c2ecf20Sopenharmony_ci			break;
6448c2ecf20Sopenharmony_ci		if (s.chrgcurr1 && s.vbusvld) {
6458c2ecf20Sopenharmony_ci			cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DONE,
6468c2ecf20Sopenharmony_ci						 HZ * 5);
6478c2ecf20Sopenharmony_ci			return;
6488c2ecf20Sopenharmony_ci		}
6498c2ecf20Sopenharmony_ci		break;
6508c2ecf20Sopenharmony_ci	case CPCAP_CHARGER_DONE:
6518c2ecf20Sopenharmony_ci		if (!s.chrgcurr2)
6528c2ecf20Sopenharmony_ci			break;
6538c2ecf20Sopenharmony_ci		cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
6548c2ecf20Sopenharmony_ci					 HZ * 5);
6558c2ecf20Sopenharmony_ci		return;
6568c2ecf20Sopenharmony_ci	default:
6578c2ecf20Sopenharmony_ci		break;
6588c2ecf20Sopenharmony_ci	}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) &&
6618c2ecf20Sopenharmony_ci	    s.chrgcurr1) {
6628c2ecf20Sopenharmony_ci		int max_current;
6638c2ecf20Sopenharmony_ci		int vchrg;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci		if (cpcap_charger_battery_found(ddata))
6668c2ecf20Sopenharmony_ci			max_current = CPCAP_REG_CRM_ICHRG_1A596;
6678c2ecf20Sopenharmony_ci		else
6688c2ecf20Sopenharmony_ci			max_current = CPCAP_REG_CRM_ICHRG_0A532;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci		vchrg = cpcap_charger_voltage_to_regval(ddata->voltage);
6718c2ecf20Sopenharmony_ci		error = cpcap_charger_set_state(ddata,
6728c2ecf20Sopenharmony_ci						CPCAP_REG_CRM_VCHRG(vchrg),
6738c2ecf20Sopenharmony_ci						max_current, 0);
6748c2ecf20Sopenharmony_ci		if (error)
6758c2ecf20Sopenharmony_ci			goto out_err;
6768c2ecf20Sopenharmony_ci		cpcap_charger_update_state(ddata, CPCAP_CHARGER_CHARGING);
6778c2ecf20Sopenharmony_ci	} else {
6788c2ecf20Sopenharmony_ci		error = cpcap_charger_set_state(ddata, 0, 0, 0);
6798c2ecf20Sopenharmony_ci		if (error)
6808c2ecf20Sopenharmony_ci			goto out_err;
6818c2ecf20Sopenharmony_ci		cpcap_charger_update_state(ddata, CPCAP_CHARGER_DISCONNECTED);
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	power_supply_changed(ddata->usb);
6858c2ecf20Sopenharmony_ci	return;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ciout_err:
6888c2ecf20Sopenharmony_ci	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_cistatic irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	struct cpcap_charger_ddata *ddata = data;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	if (!atomic_read(&ddata->active))
6968c2ecf20Sopenharmony_ci		return IRQ_NONE;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	schedule_delayed_work(&ddata->detect_work, 0);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
7018c2ecf20Sopenharmony_ci}
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_cistatic int cpcap_usb_init_irq(struct platform_device *pdev,
7048c2ecf20Sopenharmony_ci			      struct cpcap_charger_ddata *ddata,
7058c2ecf20Sopenharmony_ci			      const char *name)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	struct cpcap_interrupt_desc *d;
7088c2ecf20Sopenharmony_ci	int irq, error;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	irq = platform_get_irq_byname(pdev, name);
7118c2ecf20Sopenharmony_ci	if (irq < 0)
7128c2ecf20Sopenharmony_ci		return -ENODEV;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	error = devm_request_threaded_irq(ddata->dev, irq, NULL,
7158c2ecf20Sopenharmony_ci					  cpcap_charger_irq_thread,
7168c2ecf20Sopenharmony_ci					  IRQF_SHARED | IRQF_ONESHOT,
7178c2ecf20Sopenharmony_ci					  name, ddata);
7188c2ecf20Sopenharmony_ci	if (error) {
7198c2ecf20Sopenharmony_ci		dev_err(ddata->dev, "could not get irq %s: %i\n",
7208c2ecf20Sopenharmony_ci			name, error);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci		return error;
7238c2ecf20Sopenharmony_ci	}
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
7268c2ecf20Sopenharmony_ci	if (!d)
7278c2ecf20Sopenharmony_ci		return -ENOMEM;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	d->name = name;
7308c2ecf20Sopenharmony_ci	d->irq = irq;
7318c2ecf20Sopenharmony_ci	list_add(&d->node, &ddata->irq_list);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	return 0;
7348c2ecf20Sopenharmony_ci}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cistatic const char * const cpcap_charger_irqs[] = {
7378c2ecf20Sopenharmony_ci	/* REG_INT_0 */
7388c2ecf20Sopenharmony_ci	"chrg_det", "rvrs_chrg",
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	/* REG_INT1 */
7418c2ecf20Sopenharmony_ci	"chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr2", "chrgcurr1", "vbusvld",
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	/* REG_INT_3 */
7448c2ecf20Sopenharmony_ci	"battdetb",
7458c2ecf20Sopenharmony_ci};
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_cistatic int cpcap_usb_init_interrupts(struct platform_device *pdev,
7488c2ecf20Sopenharmony_ci				     struct cpcap_charger_ddata *ddata)
7498c2ecf20Sopenharmony_ci{
7508c2ecf20Sopenharmony_ci	int i, error;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
7538c2ecf20Sopenharmony_ci		error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
7548c2ecf20Sopenharmony_ci		if (error)
7558c2ecf20Sopenharmony_ci			return error;
7568c2ecf20Sopenharmony_ci	}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	return 0;
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_cistatic void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
7628c2ecf20Sopenharmony_ci{
7638c2ecf20Sopenharmony_ci	int i;
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
7668c2ecf20Sopenharmony_ci		ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
7678c2ecf20Sopenharmony_ci						      i, GPIOD_OUT_HIGH);
7688c2ecf20Sopenharmony_ci		if (IS_ERR(ddata->gpio[i])) {
7698c2ecf20Sopenharmony_ci			dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
7708c2ecf20Sopenharmony_ci				 i, PTR_ERR(ddata->gpio[i]));
7718c2ecf20Sopenharmony_ci			ddata->gpio[i] = NULL;
7728c2ecf20Sopenharmony_ci		}
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	const char * const names[CPCAP_CHARGER_IIO_NR] = {
7798c2ecf20Sopenharmony_ci		"battdetb", "battp", "vbus", "chg_isense", "batti",
7808c2ecf20Sopenharmony_ci	};
7818c2ecf20Sopenharmony_ci	int error, i;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
7848c2ecf20Sopenharmony_ci		ddata->channels[i] = devm_iio_channel_get(ddata->dev,
7858c2ecf20Sopenharmony_ci							  names[i]);
7868c2ecf20Sopenharmony_ci		if (IS_ERR(ddata->channels[i])) {
7878c2ecf20Sopenharmony_ci			error = PTR_ERR(ddata->channels[i]);
7888c2ecf20Sopenharmony_ci			goto out_err;
7898c2ecf20Sopenharmony_ci		}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci		if (!ddata->channels[i]->indio_dev) {
7928c2ecf20Sopenharmony_ci			error = -ENXIO;
7938c2ecf20Sopenharmony_ci			goto out_err;
7948c2ecf20Sopenharmony_ci		}
7958c2ecf20Sopenharmony_ci	}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	return 0;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ciout_err:
8008c2ecf20Sopenharmony_ci	if (error != -EPROBE_DEFER)
8018c2ecf20Sopenharmony_ci		dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
8028c2ecf20Sopenharmony_ci			error);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	return error;
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_cistatic const struct power_supply_desc cpcap_charger_usb_desc = {
8088c2ecf20Sopenharmony_ci	.name		= "usb",
8098c2ecf20Sopenharmony_ci	.type		= POWER_SUPPLY_TYPE_USB,
8108c2ecf20Sopenharmony_ci	.properties	= cpcap_charger_props,
8118c2ecf20Sopenharmony_ci	.num_properties	= ARRAY_SIZE(cpcap_charger_props),
8128c2ecf20Sopenharmony_ci	.get_property	= cpcap_charger_get_property,
8138c2ecf20Sopenharmony_ci	.set_property	= cpcap_charger_set_property,
8148c2ecf20Sopenharmony_ci	.property_is_writeable = cpcap_charger_property_is_writeable,
8158c2ecf20Sopenharmony_ci};
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
8188c2ecf20Sopenharmony_cistatic const struct of_device_id cpcap_charger_id_table[] = {
8198c2ecf20Sopenharmony_ci	{
8208c2ecf20Sopenharmony_ci		.compatible = "motorola,mapphone-cpcap-charger",
8218c2ecf20Sopenharmony_ci	},
8228c2ecf20Sopenharmony_ci	{},
8238c2ecf20Sopenharmony_ci};
8248c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
8258c2ecf20Sopenharmony_ci#endif
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_cistatic int cpcap_charger_probe(struct platform_device *pdev)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	struct cpcap_charger_ddata *ddata;
8308c2ecf20Sopenharmony_ci	const struct of_device_id *of_id;
8318c2ecf20Sopenharmony_ci	struct power_supply_config psy_cfg = {};
8328c2ecf20Sopenharmony_ci	int error;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
8358c2ecf20Sopenharmony_ci				&pdev->dev);
8368c2ecf20Sopenharmony_ci	if (!of_id)
8378c2ecf20Sopenharmony_ci		return -EINVAL;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
8408c2ecf20Sopenharmony_ci	if (!ddata)
8418c2ecf20Sopenharmony_ci		return -ENOMEM;
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	ddata->dev = &pdev->dev;
8448c2ecf20Sopenharmony_ci	ddata->voltage = 4200000;
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
8478c2ecf20Sopenharmony_ci	if (!ddata->reg)
8488c2ecf20Sopenharmony_ci		return -ENODEV;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ddata->irq_list);
8518c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
8528c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
8538c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ddata);
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	error = cpcap_charger_init_iio(ddata);
8568c2ecf20Sopenharmony_ci	if (error)
8578c2ecf20Sopenharmony_ci		return error;
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	atomic_set(&ddata->active, 1);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	psy_cfg.of_node = pdev->dev.of_node;
8628c2ecf20Sopenharmony_ci	psy_cfg.drv_data = ddata;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	ddata->usb = devm_power_supply_register(ddata->dev,
8658c2ecf20Sopenharmony_ci						&cpcap_charger_usb_desc,
8668c2ecf20Sopenharmony_ci						&psy_cfg);
8678c2ecf20Sopenharmony_ci	if (IS_ERR(ddata->usb)) {
8688c2ecf20Sopenharmony_ci		error = PTR_ERR(ddata->usb);
8698c2ecf20Sopenharmony_ci		dev_err(ddata->dev, "failed to register USB charger: %i\n",
8708c2ecf20Sopenharmony_ci			error);
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci		return error;
8738c2ecf20Sopenharmony_ci	}
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	error = cpcap_usb_init_interrupts(pdev, ddata);
8768c2ecf20Sopenharmony_ci	if (error)
8778c2ecf20Sopenharmony_ci		return error;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	ddata->comparator.set_vbus = cpcap_charger_set_vbus;
8808c2ecf20Sopenharmony_ci	error = omap_usb2_set_comparator(&ddata->comparator);
8818c2ecf20Sopenharmony_ci	if (error == -ENODEV) {
8828c2ecf20Sopenharmony_ci		dev_info(ddata->dev, "charger needs phy, deferring probe\n");
8838c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
8848c2ecf20Sopenharmony_ci	}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	cpcap_charger_init_optional_gpios(ddata);
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	schedule_delayed_work(&ddata->detect_work, 0);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	return 0;
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_cistatic int cpcap_charger_remove(struct platform_device *pdev)
8948c2ecf20Sopenharmony_ci{
8958c2ecf20Sopenharmony_ci	struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
8968c2ecf20Sopenharmony_ci	int error;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	atomic_set(&ddata->active, 0);
8998c2ecf20Sopenharmony_ci	error = omap_usb2_set_comparator(NULL);
9008c2ecf20Sopenharmony_ci	if (error)
9018c2ecf20Sopenharmony_ci		dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
9028c2ecf20Sopenharmony_ci			 error);
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	error = cpcap_charger_set_state(ddata, 0, 0, 0);
9058c2ecf20Sopenharmony_ci	if (error)
9068c2ecf20Sopenharmony_ci		dev_warn(ddata->dev, "could not clear charger: %i\n",
9078c2ecf20Sopenharmony_ci			 error);
9088c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&ddata->vbus_work);
9098c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&ddata->detect_work);
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	return 0;
9128c2ecf20Sopenharmony_ci}
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_cistatic struct platform_driver cpcap_charger_driver = {
9158c2ecf20Sopenharmony_ci	.probe = cpcap_charger_probe,
9168c2ecf20Sopenharmony_ci	.driver	= {
9178c2ecf20Sopenharmony_ci		.name	= "cpcap-charger",
9188c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(cpcap_charger_id_table),
9198c2ecf20Sopenharmony_ci	},
9208c2ecf20Sopenharmony_ci	.remove	= cpcap_charger_remove,
9218c2ecf20Sopenharmony_ci};
9228c2ecf20Sopenharmony_cimodule_platform_driver(cpcap_charger_driver);
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
9258c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
9268c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
9278c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:cpcap-charger");
928