1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * Driver for LP8727 Micro/Mini USB IC with integrated charger
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  *			Copyright (C) 2011 Texas Instruments
68c0984e5SSebastian Reichel  *			Copyright (C) 2011 National Semiconductor
78c0984e5SSebastian Reichel  */
88c0984e5SSebastian Reichel 
98c0984e5SSebastian Reichel #include <linux/module.h>
108c0984e5SSebastian Reichel #include <linux/slab.h>
118c0984e5SSebastian Reichel #include <linux/interrupt.h>
128c0984e5SSebastian Reichel #include <linux/i2c.h>
138c0984e5SSebastian Reichel #include <linux/power_supply.h>
148c0984e5SSebastian Reichel #include <linux/platform_data/lp8727.h>
158c0984e5SSebastian Reichel #include <linux/of.h>
168c0984e5SSebastian Reichel 
178c0984e5SSebastian Reichel #define LP8788_NUM_INTREGS	2
188c0984e5SSebastian Reichel #define DEFAULT_DEBOUNCE_MSEC	270
198c0984e5SSebastian Reichel 
208c0984e5SSebastian Reichel /* Registers */
218c0984e5SSebastian Reichel #define LP8727_CTRL1		0x1
228c0984e5SSebastian Reichel #define LP8727_CTRL2		0x2
238c0984e5SSebastian Reichel #define LP8727_SWCTRL		0x3
248c0984e5SSebastian Reichel #define LP8727_INT1		0x4
258c0984e5SSebastian Reichel #define LP8727_INT2		0x5
268c0984e5SSebastian Reichel #define LP8727_STATUS1		0x6
278c0984e5SSebastian Reichel #define LP8727_STATUS2		0x7
288c0984e5SSebastian Reichel #define LP8727_CHGCTRL2		0x9
298c0984e5SSebastian Reichel 
308c0984e5SSebastian Reichel /* CTRL1 register */
318c0984e5SSebastian Reichel #define LP8727_CP_EN		BIT(0)
328c0984e5SSebastian Reichel #define LP8727_ADC_EN		BIT(1)
338c0984e5SSebastian Reichel #define LP8727_ID200_EN		BIT(4)
348c0984e5SSebastian Reichel 
358c0984e5SSebastian Reichel /* CTRL2 register */
368c0984e5SSebastian Reichel #define LP8727_CHGDET_EN	BIT(1)
378c0984e5SSebastian Reichel #define LP8727_INT_EN		BIT(6)
388c0984e5SSebastian Reichel 
398c0984e5SSebastian Reichel /* SWCTRL register */
408c0984e5SSebastian Reichel #define LP8727_SW_DM1_DM	(0x0 << 0)
418c0984e5SSebastian Reichel #define LP8727_SW_DM1_HiZ	(0x7 << 0)
428c0984e5SSebastian Reichel #define LP8727_SW_DP2_DP	(0x0 << 3)
438c0984e5SSebastian Reichel #define LP8727_SW_DP2_HiZ	(0x7 << 3)
448c0984e5SSebastian Reichel 
458c0984e5SSebastian Reichel /* INT1 register */
468c0984e5SSebastian Reichel #define LP8727_IDNO		(0xF << 0)
478c0984e5SSebastian Reichel #define LP8727_VBUS		BIT(4)
488c0984e5SSebastian Reichel 
498c0984e5SSebastian Reichel /* STATUS1 register */
508c0984e5SSebastian Reichel #define LP8727_CHGSTAT		(3 << 4)
518c0984e5SSebastian Reichel #define LP8727_CHPORT		BIT(6)
528c0984e5SSebastian Reichel #define LP8727_DCPORT		BIT(7)
538c0984e5SSebastian Reichel #define LP8727_STAT_EOC		0x30
548c0984e5SSebastian Reichel 
558c0984e5SSebastian Reichel /* STATUS2 register */
568c0984e5SSebastian Reichel #define LP8727_TEMP_STAT	(3 << 5)
578c0984e5SSebastian Reichel #define LP8727_TEMP_SHIFT	5
588c0984e5SSebastian Reichel 
598c0984e5SSebastian Reichel /* CHGCTRL2 register */
608c0984e5SSebastian Reichel #define LP8727_ICHG_SHIFT	4
618c0984e5SSebastian Reichel 
628c0984e5SSebastian Reichel enum lp8727_dev_id {
638c0984e5SSebastian Reichel 	LP8727_ID_NONE,
648c0984e5SSebastian Reichel 	LP8727_ID_TA,
658c0984e5SSebastian Reichel 	LP8727_ID_DEDICATED_CHG,
668c0984e5SSebastian Reichel 	LP8727_ID_USB_CHG,
678c0984e5SSebastian Reichel 	LP8727_ID_USB_DS,
688c0984e5SSebastian Reichel 	LP8727_ID_MAX,
698c0984e5SSebastian Reichel };
708c0984e5SSebastian Reichel 
718c0984e5SSebastian Reichel enum lp8727_die_temp {
728c0984e5SSebastian Reichel 	LP8788_TEMP_75C,
738c0984e5SSebastian Reichel 	LP8788_TEMP_95C,
748c0984e5SSebastian Reichel 	LP8788_TEMP_115C,
758c0984e5SSebastian Reichel 	LP8788_TEMP_135C,
768c0984e5SSebastian Reichel };
778c0984e5SSebastian Reichel 
788c0984e5SSebastian Reichel struct lp8727_psy {
798c0984e5SSebastian Reichel 	struct power_supply *ac;
808c0984e5SSebastian Reichel 	struct power_supply *usb;
818c0984e5SSebastian Reichel 	struct power_supply *batt;
828c0984e5SSebastian Reichel };
838c0984e5SSebastian Reichel 
848c0984e5SSebastian Reichel struct lp8727_chg {
858c0984e5SSebastian Reichel 	struct device *dev;
868c0984e5SSebastian Reichel 	struct i2c_client *client;
878c0984e5SSebastian Reichel 	struct mutex xfer_lock;
888c0984e5SSebastian Reichel 	struct lp8727_psy *psy;
898c0984e5SSebastian Reichel 	struct lp8727_platform_data *pdata;
908c0984e5SSebastian Reichel 
918c0984e5SSebastian Reichel 	/* Charger Data */
928c0984e5SSebastian Reichel 	enum lp8727_dev_id devid;
938c0984e5SSebastian Reichel 	struct lp8727_chg_param *chg_param;
948c0984e5SSebastian Reichel 
958c0984e5SSebastian Reichel 	/* Interrupt Handling */
968c0984e5SSebastian Reichel 	int irq;
978c0984e5SSebastian Reichel 	struct delayed_work work;
988c0984e5SSebastian Reichel 	unsigned long debounce_jiffies;
998c0984e5SSebastian Reichel };
1008c0984e5SSebastian Reichel 
lp8727_read_bytes(struct lp8727_chg * pchg,u8 reg,u8 * data,u8 len)1018c0984e5SSebastian Reichel static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
1028c0984e5SSebastian Reichel {
1038c0984e5SSebastian Reichel 	s32 ret;
1048c0984e5SSebastian Reichel 
1058c0984e5SSebastian Reichel 	mutex_lock(&pchg->xfer_lock);
1068c0984e5SSebastian Reichel 	ret = i2c_smbus_read_i2c_block_data(pchg->client, reg, len, data);
1078c0984e5SSebastian Reichel 	mutex_unlock(&pchg->xfer_lock);
1088c0984e5SSebastian Reichel 
1098c0984e5SSebastian Reichel 	return (ret != len) ? -EIO : 0;
1108c0984e5SSebastian Reichel }
1118c0984e5SSebastian Reichel 
lp8727_read_byte(struct lp8727_chg * pchg,u8 reg,u8 * data)1128c0984e5SSebastian Reichel static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data)
1138c0984e5SSebastian Reichel {
1148c0984e5SSebastian Reichel 	return lp8727_read_bytes(pchg, reg, data, 1);
1158c0984e5SSebastian Reichel }
1168c0984e5SSebastian Reichel 
lp8727_write_byte(struct lp8727_chg * pchg,u8 reg,u8 data)1178c0984e5SSebastian Reichel static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data)
1188c0984e5SSebastian Reichel {
1198c0984e5SSebastian Reichel 	int ret;
1208c0984e5SSebastian Reichel 
1218c0984e5SSebastian Reichel 	mutex_lock(&pchg->xfer_lock);
1228c0984e5SSebastian Reichel 	ret = i2c_smbus_write_byte_data(pchg->client, reg, data);
1238c0984e5SSebastian Reichel 	mutex_unlock(&pchg->xfer_lock);
1248c0984e5SSebastian Reichel 
1258c0984e5SSebastian Reichel 	return ret;
1268c0984e5SSebastian Reichel }
1278c0984e5SSebastian Reichel 
lp8727_is_charger_attached(const char * name,int id)1288c0984e5SSebastian Reichel static bool lp8727_is_charger_attached(const char *name, int id)
1298c0984e5SSebastian Reichel {
1308c0984e5SSebastian Reichel 	if (!strcmp(name, "ac"))
1318c0984e5SSebastian Reichel 		return id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG;
1328c0984e5SSebastian Reichel 	else if (!strcmp(name, "usb"))
1338c0984e5SSebastian Reichel 		return id == LP8727_ID_USB_CHG;
1348c0984e5SSebastian Reichel 
1358c0984e5SSebastian Reichel 	return id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG;
1368c0984e5SSebastian Reichel }
1378c0984e5SSebastian Reichel 
lp8727_init_device(struct lp8727_chg * pchg)1388c0984e5SSebastian Reichel static int lp8727_init_device(struct lp8727_chg *pchg)
1398c0984e5SSebastian Reichel {
1408c0984e5SSebastian Reichel 	u8 val;
1418c0984e5SSebastian Reichel 	int ret;
1428c0984e5SSebastian Reichel 	u8 intstat[LP8788_NUM_INTREGS];
1438c0984e5SSebastian Reichel 
1448c0984e5SSebastian Reichel 	/* clear interrupts */
1458c0984e5SSebastian Reichel 	ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS);
1468c0984e5SSebastian Reichel 	if (ret)
1478c0984e5SSebastian Reichel 		return ret;
1488c0984e5SSebastian Reichel 
1498c0984e5SSebastian Reichel 	val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN;
1508c0984e5SSebastian Reichel 	ret = lp8727_write_byte(pchg, LP8727_CTRL1, val);
1518c0984e5SSebastian Reichel 	if (ret)
1528c0984e5SSebastian Reichel 		return ret;
1538c0984e5SSebastian Reichel 
1548c0984e5SSebastian Reichel 	val = LP8727_INT_EN | LP8727_CHGDET_EN;
1558c0984e5SSebastian Reichel 	return lp8727_write_byte(pchg, LP8727_CTRL2, val);
1568c0984e5SSebastian Reichel }
1578c0984e5SSebastian Reichel 
lp8727_is_dedicated_charger(struct lp8727_chg * pchg)1588c0984e5SSebastian Reichel static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
1598c0984e5SSebastian Reichel {
1608c0984e5SSebastian Reichel 	u8 val;
1618c0984e5SSebastian Reichel 
1628c0984e5SSebastian Reichel 	lp8727_read_byte(pchg, LP8727_STATUS1, &val);
1638c0984e5SSebastian Reichel 	return val & LP8727_DCPORT;
1648c0984e5SSebastian Reichel }
1658c0984e5SSebastian Reichel 
lp8727_is_usb_charger(struct lp8727_chg * pchg)1668c0984e5SSebastian Reichel static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
1678c0984e5SSebastian Reichel {
1688c0984e5SSebastian Reichel 	u8 val;
1698c0984e5SSebastian Reichel 
1708c0984e5SSebastian Reichel 	lp8727_read_byte(pchg, LP8727_STATUS1, &val);
1718c0984e5SSebastian Reichel 	return val & LP8727_CHPORT;
1728c0984e5SSebastian Reichel }
1738c0984e5SSebastian Reichel 
lp8727_ctrl_switch(struct lp8727_chg * pchg,u8 sw)1748c0984e5SSebastian Reichel static inline void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
1758c0984e5SSebastian Reichel {
1768c0984e5SSebastian Reichel 	lp8727_write_byte(pchg, LP8727_SWCTRL, sw);
1778c0984e5SSebastian Reichel }
1788c0984e5SSebastian Reichel 
lp8727_id_detection(struct lp8727_chg * pchg,u8 id,int vbusin)1798c0984e5SSebastian Reichel static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
1808c0984e5SSebastian Reichel {
1818c0984e5SSebastian Reichel 	struct lp8727_platform_data *pdata = pchg->pdata;
1828c0984e5SSebastian Reichel 	u8 devid = LP8727_ID_NONE;
1838c0984e5SSebastian Reichel 	u8 swctrl = LP8727_SW_DM1_HiZ | LP8727_SW_DP2_HiZ;
1848c0984e5SSebastian Reichel 
1858c0984e5SSebastian Reichel 	switch (id) {
1868c0984e5SSebastian Reichel 	case 0x5:
1878c0984e5SSebastian Reichel 		devid = LP8727_ID_TA;
1888c0984e5SSebastian Reichel 		pchg->chg_param = pdata ? pdata->ac : NULL;
1898c0984e5SSebastian Reichel 		break;
1908c0984e5SSebastian Reichel 	case 0xB:
1918c0984e5SSebastian Reichel 		if (lp8727_is_dedicated_charger(pchg)) {
1928c0984e5SSebastian Reichel 			pchg->chg_param = pdata ? pdata->ac : NULL;
1938c0984e5SSebastian Reichel 			devid = LP8727_ID_DEDICATED_CHG;
1948c0984e5SSebastian Reichel 		} else if (lp8727_is_usb_charger(pchg)) {
1958c0984e5SSebastian Reichel 			pchg->chg_param = pdata ? pdata->usb : NULL;
1968c0984e5SSebastian Reichel 			devid = LP8727_ID_USB_CHG;
1978c0984e5SSebastian Reichel 			swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
1988c0984e5SSebastian Reichel 		} else if (vbusin) {
1998c0984e5SSebastian Reichel 			devid = LP8727_ID_USB_DS;
2008c0984e5SSebastian Reichel 			swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
2018c0984e5SSebastian Reichel 		}
2028c0984e5SSebastian Reichel 		break;
2038c0984e5SSebastian Reichel 	default:
2048c0984e5SSebastian Reichel 		devid = LP8727_ID_NONE;
2058c0984e5SSebastian Reichel 		pchg->chg_param = NULL;
2068c0984e5SSebastian Reichel 		break;
2078c0984e5SSebastian Reichel 	}
2088c0984e5SSebastian Reichel 
2098c0984e5SSebastian Reichel 	pchg->devid = devid;
2108c0984e5SSebastian Reichel 	lp8727_ctrl_switch(pchg, swctrl);
2118c0984e5SSebastian Reichel }
2128c0984e5SSebastian Reichel 
lp8727_enable_chgdet(struct lp8727_chg * pchg)2138c0984e5SSebastian Reichel static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
2148c0984e5SSebastian Reichel {
2158c0984e5SSebastian Reichel 	u8 val;
2168c0984e5SSebastian Reichel 
2178c0984e5SSebastian Reichel 	lp8727_read_byte(pchg, LP8727_CTRL2, &val);
2188c0984e5SSebastian Reichel 	val |= LP8727_CHGDET_EN;
2198c0984e5SSebastian Reichel 	lp8727_write_byte(pchg, LP8727_CTRL2, val);
2208c0984e5SSebastian Reichel }
2218c0984e5SSebastian Reichel 
lp8727_delayed_func(struct work_struct * _work)2228c0984e5SSebastian Reichel static void lp8727_delayed_func(struct work_struct *_work)
2238c0984e5SSebastian Reichel {
2248c0984e5SSebastian Reichel 	struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg,
2258c0984e5SSebastian Reichel 						work.work);
2268c0984e5SSebastian Reichel 	u8 intstat[LP8788_NUM_INTREGS];
2278c0984e5SSebastian Reichel 	u8 idno;
2288c0984e5SSebastian Reichel 	u8 vbus;
2298c0984e5SSebastian Reichel 
2308c0984e5SSebastian Reichel 	if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) {
2318c0984e5SSebastian Reichel 		dev_err(pchg->dev, "can not read INT registers\n");
2328c0984e5SSebastian Reichel 		return;
2338c0984e5SSebastian Reichel 	}
2348c0984e5SSebastian Reichel 
2358c0984e5SSebastian Reichel 	idno = intstat[0] & LP8727_IDNO;
2368c0984e5SSebastian Reichel 	vbus = intstat[0] & LP8727_VBUS;
2378c0984e5SSebastian Reichel 
2388c0984e5SSebastian Reichel 	lp8727_id_detection(pchg, idno, vbus);
2398c0984e5SSebastian Reichel 	lp8727_enable_chgdet(pchg);
2408c0984e5SSebastian Reichel 
2418c0984e5SSebastian Reichel 	power_supply_changed(pchg->psy->ac);
2428c0984e5SSebastian Reichel 	power_supply_changed(pchg->psy->usb);
2438c0984e5SSebastian Reichel 	power_supply_changed(pchg->psy->batt);
2448c0984e5SSebastian Reichel }
2458c0984e5SSebastian Reichel 
lp8727_isr_func(int irq,void * ptr)2468c0984e5SSebastian Reichel static irqreturn_t lp8727_isr_func(int irq, void *ptr)
2478c0984e5SSebastian Reichel {
2488c0984e5SSebastian Reichel 	struct lp8727_chg *pchg = ptr;
2498c0984e5SSebastian Reichel 
2508c0984e5SSebastian Reichel 	schedule_delayed_work(&pchg->work, pchg->debounce_jiffies);
2518c0984e5SSebastian Reichel 	return IRQ_HANDLED;
2528c0984e5SSebastian Reichel }
2538c0984e5SSebastian Reichel 
lp8727_setup_irq(struct lp8727_chg * pchg)2548c0984e5SSebastian Reichel static int lp8727_setup_irq(struct lp8727_chg *pchg)
2558c0984e5SSebastian Reichel {
2568c0984e5SSebastian Reichel 	int ret;
2578c0984e5SSebastian Reichel 	int irq = pchg->client->irq;
2588c0984e5SSebastian Reichel 	unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec :
2598c0984e5SSebastian Reichel 						DEFAULT_DEBOUNCE_MSEC;
2608c0984e5SSebastian Reichel 
2618c0984e5SSebastian Reichel 	INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
2628c0984e5SSebastian Reichel 
2638c0984e5SSebastian Reichel 	if (irq <= 0) {
2648c0984e5SSebastian Reichel 		dev_warn(pchg->dev, "invalid irq number: %d\n", irq);
2658c0984e5SSebastian Reichel 		return 0;
2668c0984e5SSebastian Reichel 	}
2678c0984e5SSebastian Reichel 
2688c0984e5SSebastian Reichel 	ret = request_threaded_irq(irq,	NULL, lp8727_isr_func,
2698c0984e5SSebastian Reichel 				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
2708c0984e5SSebastian Reichel 				"lp8727_irq", pchg);
2718c0984e5SSebastian Reichel 
2728c0984e5SSebastian Reichel 	if (ret)
2738c0984e5SSebastian Reichel 		return ret;
2748c0984e5SSebastian Reichel 
2758c0984e5SSebastian Reichel 	pchg->irq = irq;
2768c0984e5SSebastian Reichel 	pchg->debounce_jiffies = msecs_to_jiffies(delay_msec);
2778c0984e5SSebastian Reichel 
2788c0984e5SSebastian Reichel 	return 0;
2798c0984e5SSebastian Reichel }
2808c0984e5SSebastian Reichel 
lp8727_release_irq(struct lp8727_chg * pchg)2818c0984e5SSebastian Reichel static void lp8727_release_irq(struct lp8727_chg *pchg)
2828c0984e5SSebastian Reichel {
2838c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&pchg->work);
2848c0984e5SSebastian Reichel 
2858c0984e5SSebastian Reichel 	if (pchg->irq)
2868c0984e5SSebastian Reichel 		free_irq(pchg->irq, pchg);
2878c0984e5SSebastian Reichel }
2888c0984e5SSebastian Reichel 
2898c0984e5SSebastian Reichel static enum power_supply_property lp8727_charger_prop[] = {
2908c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
2918c0984e5SSebastian Reichel };
2928c0984e5SSebastian Reichel 
2938c0984e5SSebastian Reichel static enum power_supply_property lp8727_battery_prop[] = {
2948c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
2958c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
2968c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
2978c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
2988c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
2998c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
3008c0984e5SSebastian Reichel };
3018c0984e5SSebastian Reichel 
3028c0984e5SSebastian Reichel static char *battery_supplied_to[] = {
3038c0984e5SSebastian Reichel 	"main_batt",
3048c0984e5SSebastian Reichel };
3058c0984e5SSebastian Reichel 
lp8727_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)3068c0984e5SSebastian Reichel static int lp8727_charger_get_property(struct power_supply *psy,
3078c0984e5SSebastian Reichel 				       enum power_supply_property psp,
3088c0984e5SSebastian Reichel 				       union power_supply_propval *val)
3098c0984e5SSebastian Reichel {
3108c0984e5SSebastian Reichel 	struct lp8727_chg *pchg = dev_get_drvdata(psy->dev.parent);
3118c0984e5SSebastian Reichel 
3128c0984e5SSebastian Reichel 	if (psp != POWER_SUPPLY_PROP_ONLINE)
3138c0984e5SSebastian Reichel 		return -EINVAL;
3148c0984e5SSebastian Reichel 
3158c0984e5SSebastian Reichel 	val->intval = lp8727_is_charger_attached(psy->desc->name, pchg->devid);
3168c0984e5SSebastian Reichel 
3178c0984e5SSebastian Reichel 	return 0;
3188c0984e5SSebastian Reichel }
3198c0984e5SSebastian Reichel 
lp8727_is_high_temperature(enum lp8727_die_temp temp)3208c0984e5SSebastian Reichel static bool lp8727_is_high_temperature(enum lp8727_die_temp temp)
3218c0984e5SSebastian Reichel {
3228c0984e5SSebastian Reichel 	switch (temp) {
3238c0984e5SSebastian Reichel 	case LP8788_TEMP_95C:
3248c0984e5SSebastian Reichel 	case LP8788_TEMP_115C:
3258c0984e5SSebastian Reichel 	case LP8788_TEMP_135C:
3268c0984e5SSebastian Reichel 		return true;
3278c0984e5SSebastian Reichel 	default:
3288c0984e5SSebastian Reichel 		return false;
3298c0984e5SSebastian Reichel 	}
3308c0984e5SSebastian Reichel }
3318c0984e5SSebastian Reichel 
lp8727_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)3328c0984e5SSebastian Reichel static int lp8727_battery_get_property(struct power_supply *psy,
3338c0984e5SSebastian Reichel 				       enum power_supply_property psp,
3348c0984e5SSebastian Reichel 				       union power_supply_propval *val)
3358c0984e5SSebastian Reichel {
3368c0984e5SSebastian Reichel 	struct lp8727_chg *pchg = dev_get_drvdata(psy->dev.parent);
3378c0984e5SSebastian Reichel 	struct lp8727_platform_data *pdata = pchg->pdata;
3388c0984e5SSebastian Reichel 	enum lp8727_die_temp temp;
3398c0984e5SSebastian Reichel 	u8 read;
3408c0984e5SSebastian Reichel 
3418c0984e5SSebastian Reichel 	switch (psp) {
3428c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
3438c0984e5SSebastian Reichel 		if (!lp8727_is_charger_attached(psy->desc->name, pchg->devid)) {
3448c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
3458c0984e5SSebastian Reichel 			return 0;
3468c0984e5SSebastian Reichel 		}
3478c0984e5SSebastian Reichel 
3488c0984e5SSebastian Reichel 		lp8727_read_byte(pchg, LP8727_STATUS1, &read);
3498c0984e5SSebastian Reichel 
3508c0984e5SSebastian Reichel 		val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ?
3518c0984e5SSebastian Reichel 				POWER_SUPPLY_STATUS_FULL :
3528c0984e5SSebastian Reichel 				POWER_SUPPLY_STATUS_CHARGING;
3538c0984e5SSebastian Reichel 		break;
3548c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_HEALTH:
3558c0984e5SSebastian Reichel 		lp8727_read_byte(pchg, LP8727_STATUS2, &read);
3568c0984e5SSebastian Reichel 		temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT;
3578c0984e5SSebastian Reichel 
3588c0984e5SSebastian Reichel 		val->intval = lp8727_is_high_temperature(temp) ?
3598c0984e5SSebastian Reichel 			POWER_SUPPLY_HEALTH_OVERHEAT :
3608c0984e5SSebastian Reichel 			POWER_SUPPLY_HEALTH_GOOD;
3618c0984e5SSebastian Reichel 		break;
3628c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
3638c0984e5SSebastian Reichel 		if (!pdata)
3648c0984e5SSebastian Reichel 			return -EINVAL;
3658c0984e5SSebastian Reichel 
3668c0984e5SSebastian Reichel 		if (pdata->get_batt_present)
3678c0984e5SSebastian Reichel 			val->intval = pdata->get_batt_present();
3688c0984e5SSebastian Reichel 		break;
3698c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3708c0984e5SSebastian Reichel 		if (!pdata)
3718c0984e5SSebastian Reichel 			return -EINVAL;
3728c0984e5SSebastian Reichel 
3738c0984e5SSebastian Reichel 		if (pdata->get_batt_level)
3748c0984e5SSebastian Reichel 			val->intval = pdata->get_batt_level();
3758c0984e5SSebastian Reichel 		break;
3768c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
3778c0984e5SSebastian Reichel 		if (!pdata)
3788c0984e5SSebastian Reichel 			return -EINVAL;
3798c0984e5SSebastian Reichel 
3808c0984e5SSebastian Reichel 		if (pdata->get_batt_capacity)
3818c0984e5SSebastian Reichel 			val->intval = pdata->get_batt_capacity();
3828c0984e5SSebastian Reichel 		break;
3838c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
3848c0984e5SSebastian Reichel 		if (!pdata)
3858c0984e5SSebastian Reichel 			return -EINVAL;
3868c0984e5SSebastian Reichel 
3878c0984e5SSebastian Reichel 		if (pdata->get_batt_temp)
3888c0984e5SSebastian Reichel 			val->intval = pdata->get_batt_temp();
3898c0984e5SSebastian Reichel 		break;
3908c0984e5SSebastian Reichel 	default:
3918c0984e5SSebastian Reichel 		break;
3928c0984e5SSebastian Reichel 	}
3938c0984e5SSebastian Reichel 
3948c0984e5SSebastian Reichel 	return 0;
3958c0984e5SSebastian Reichel }
3968c0984e5SSebastian Reichel 
lp8727_charger_changed(struct power_supply * psy)3978c0984e5SSebastian Reichel static void lp8727_charger_changed(struct power_supply *psy)
3988c0984e5SSebastian Reichel {
3998c0984e5SSebastian Reichel 	struct lp8727_chg *pchg = dev_get_drvdata(psy->dev.parent);
4008c0984e5SSebastian Reichel 	u8 eoc_level;
4018c0984e5SSebastian Reichel 	u8 ichg;
4028c0984e5SSebastian Reichel 	u8 val;
4038c0984e5SSebastian Reichel 
4048c0984e5SSebastian Reichel 	/* skip if no charger exists */
4058c0984e5SSebastian Reichel 	if (!lp8727_is_charger_attached(psy->desc->name, pchg->devid))
4068c0984e5SSebastian Reichel 		return;
4078c0984e5SSebastian Reichel 
4088c0984e5SSebastian Reichel 	/* update charging parameters */
4098c0984e5SSebastian Reichel 	if (pchg->chg_param) {
4108c0984e5SSebastian Reichel 		eoc_level = pchg->chg_param->eoc_level;
4118c0984e5SSebastian Reichel 		ichg = pchg->chg_param->ichg;
4128c0984e5SSebastian Reichel 		val = (ichg << LP8727_ICHG_SHIFT) | eoc_level;
4138c0984e5SSebastian Reichel 		lp8727_write_byte(pchg, LP8727_CHGCTRL2, val);
4148c0984e5SSebastian Reichel 	}
4158c0984e5SSebastian Reichel }
4168c0984e5SSebastian Reichel 
4178c0984e5SSebastian Reichel static const struct power_supply_desc lp8727_ac_desc = {
4188c0984e5SSebastian Reichel 	.name			= "ac",
4198c0984e5SSebastian Reichel 	.type			= POWER_SUPPLY_TYPE_MAINS,
4208c0984e5SSebastian Reichel 	.properties		= lp8727_charger_prop,
4218c0984e5SSebastian Reichel 	.num_properties		= ARRAY_SIZE(lp8727_charger_prop),
4228c0984e5SSebastian Reichel 	.get_property		= lp8727_charger_get_property,
4238c0984e5SSebastian Reichel };
4248c0984e5SSebastian Reichel 
4258c0984e5SSebastian Reichel static const struct power_supply_desc lp8727_usb_desc = {
4268c0984e5SSebastian Reichel 	.name			= "usb",
4278c0984e5SSebastian Reichel 	.type			= POWER_SUPPLY_TYPE_USB,
4288c0984e5SSebastian Reichel 	.properties		= lp8727_charger_prop,
4298c0984e5SSebastian Reichel 	.num_properties		= ARRAY_SIZE(lp8727_charger_prop),
4308c0984e5SSebastian Reichel 	.get_property		= lp8727_charger_get_property,
4318c0984e5SSebastian Reichel };
4328c0984e5SSebastian Reichel 
4338c0984e5SSebastian Reichel static const struct power_supply_desc lp8727_batt_desc = {
4348c0984e5SSebastian Reichel 	.name			= "main_batt",
4358c0984e5SSebastian Reichel 	.type			= POWER_SUPPLY_TYPE_BATTERY,
4368c0984e5SSebastian Reichel 	.properties		= lp8727_battery_prop,
4378c0984e5SSebastian Reichel 	.num_properties		= ARRAY_SIZE(lp8727_battery_prop),
4388c0984e5SSebastian Reichel 	.get_property		= lp8727_battery_get_property,
4398c0984e5SSebastian Reichel 	.external_power_changed	= lp8727_charger_changed,
4408c0984e5SSebastian Reichel };
4418c0984e5SSebastian Reichel 
lp8727_register_psy(struct lp8727_chg * pchg)4428c0984e5SSebastian Reichel static int lp8727_register_psy(struct lp8727_chg *pchg)
4438c0984e5SSebastian Reichel {
4448c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {}; /* Only for ac and usb */
4458c0984e5SSebastian Reichel 	struct lp8727_psy *psy;
4468c0984e5SSebastian Reichel 
4478c0984e5SSebastian Reichel 	psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL);
4488c0984e5SSebastian Reichel 	if (!psy)
4498c0984e5SSebastian Reichel 		return -ENOMEM;
4508c0984e5SSebastian Reichel 
4518c0984e5SSebastian Reichel 	pchg->psy = psy;
4528c0984e5SSebastian Reichel 
4538c0984e5SSebastian Reichel 	psy_cfg.supplied_to = battery_supplied_to;
4548c0984e5SSebastian Reichel 	psy_cfg.num_supplicants = ARRAY_SIZE(battery_supplied_to);
4558c0984e5SSebastian Reichel 
4568c0984e5SSebastian Reichel 	psy->ac = power_supply_register(pchg->dev, &lp8727_ac_desc, &psy_cfg);
4578c0984e5SSebastian Reichel 	if (IS_ERR(psy->ac))
4588c0984e5SSebastian Reichel 		goto err_psy_ac;
4598c0984e5SSebastian Reichel 
4608c0984e5SSebastian Reichel 	psy->usb = power_supply_register(pchg->dev, &lp8727_usb_desc,
4618c0984e5SSebastian Reichel 					 &psy_cfg);
4628c0984e5SSebastian Reichel 	if (IS_ERR(psy->usb))
4638c0984e5SSebastian Reichel 		goto err_psy_usb;
4648c0984e5SSebastian Reichel 
4658c0984e5SSebastian Reichel 	psy->batt = power_supply_register(pchg->dev, &lp8727_batt_desc, NULL);
4668c0984e5SSebastian Reichel 	if (IS_ERR(psy->batt))
4678c0984e5SSebastian Reichel 		goto err_psy_batt;
4688c0984e5SSebastian Reichel 
4698c0984e5SSebastian Reichel 	return 0;
4708c0984e5SSebastian Reichel 
4718c0984e5SSebastian Reichel err_psy_batt:
4728c0984e5SSebastian Reichel 	power_supply_unregister(psy->usb);
4738c0984e5SSebastian Reichel err_psy_usb:
4748c0984e5SSebastian Reichel 	power_supply_unregister(psy->ac);
4758c0984e5SSebastian Reichel err_psy_ac:
4768c0984e5SSebastian Reichel 	return -EPERM;
4778c0984e5SSebastian Reichel }
4788c0984e5SSebastian Reichel 
lp8727_unregister_psy(struct lp8727_chg * pchg)4798c0984e5SSebastian Reichel static void lp8727_unregister_psy(struct lp8727_chg *pchg)
4808c0984e5SSebastian Reichel {
4818c0984e5SSebastian Reichel 	struct lp8727_psy *psy = pchg->psy;
4828c0984e5SSebastian Reichel 
4838c0984e5SSebastian Reichel 	if (!psy)
4848c0984e5SSebastian Reichel 		return;
4858c0984e5SSebastian Reichel 
4868c0984e5SSebastian Reichel 	power_supply_unregister(psy->ac);
4878c0984e5SSebastian Reichel 	power_supply_unregister(psy->usb);
4888c0984e5SSebastian Reichel 	power_supply_unregister(psy->batt);
4898c0984e5SSebastian Reichel }
4908c0984e5SSebastian Reichel 
4918c0984e5SSebastian Reichel #ifdef CONFIG_OF
4928c0984e5SSebastian Reichel static struct lp8727_chg_param
lp8727_parse_charge_pdata(struct device * dev,struct device_node * np)4938c0984e5SSebastian Reichel *lp8727_parse_charge_pdata(struct device *dev, struct device_node *np)
4948c0984e5SSebastian Reichel {
4958c0984e5SSebastian Reichel 	struct lp8727_chg_param *param;
4968c0984e5SSebastian Reichel 
4978c0984e5SSebastian Reichel 	param = devm_kzalloc(dev, sizeof(*param), GFP_KERNEL);
4988c0984e5SSebastian Reichel 	if (!param)
4998c0984e5SSebastian Reichel 		goto out;
5008c0984e5SSebastian Reichel 
5018c0984e5SSebastian Reichel 	of_property_read_u8(np, "eoc-level", (u8 *)&param->eoc_level);
5028c0984e5SSebastian Reichel 	of_property_read_u8(np, "charging-current", (u8 *)&param->ichg);
5038c0984e5SSebastian Reichel out:
5048c0984e5SSebastian Reichel 	return param;
5058c0984e5SSebastian Reichel }
5068c0984e5SSebastian Reichel 
lp8727_parse_dt(struct device * dev)5078c0984e5SSebastian Reichel static struct lp8727_platform_data *lp8727_parse_dt(struct device *dev)
5088c0984e5SSebastian Reichel {
5098c0984e5SSebastian Reichel 	struct device_node *np = dev->of_node;
5108c0984e5SSebastian Reichel 	struct device_node *child;
5118c0984e5SSebastian Reichel 	struct lp8727_platform_data *pdata;
5128c0984e5SSebastian Reichel 	const char *type;
5138c0984e5SSebastian Reichel 
5148c0984e5SSebastian Reichel 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
5158c0984e5SSebastian Reichel 	if (!pdata)
5168c0984e5SSebastian Reichel 		return ERR_PTR(-ENOMEM);
5178c0984e5SSebastian Reichel 
5188c0984e5SSebastian Reichel 	of_property_read_u32(np, "debounce-ms", &pdata->debounce_msec);
5198c0984e5SSebastian Reichel 
5208c0984e5SSebastian Reichel 	/* If charging parameter is not defined, just skip parsing the dt */
5218c0984e5SSebastian Reichel 	if (of_get_child_count(np) == 0)
5228c0984e5SSebastian Reichel 		return pdata;
5238c0984e5SSebastian Reichel 
5248c0984e5SSebastian Reichel 	for_each_child_of_node(np, child) {
5258c0984e5SSebastian Reichel 		of_property_read_string(child, "charger-type", &type);
5268c0984e5SSebastian Reichel 
5278c0984e5SSebastian Reichel 		if (!strcmp(type, "ac"))
5288c0984e5SSebastian Reichel 			pdata->ac = lp8727_parse_charge_pdata(dev, child);
5298c0984e5SSebastian Reichel 
5308c0984e5SSebastian Reichel 		if (!strcmp(type, "usb"))
5318c0984e5SSebastian Reichel 			pdata->usb = lp8727_parse_charge_pdata(dev, child);
5328c0984e5SSebastian Reichel 	}
5338c0984e5SSebastian Reichel 
5348c0984e5SSebastian Reichel 	return pdata;
5358c0984e5SSebastian Reichel }
5368c0984e5SSebastian Reichel #else
lp8727_parse_dt(struct device * dev)5378c0984e5SSebastian Reichel static struct lp8727_platform_data *lp8727_parse_dt(struct device *dev)
5388c0984e5SSebastian Reichel {
5398c0984e5SSebastian Reichel 	return NULL;
5408c0984e5SSebastian Reichel }
5418c0984e5SSebastian Reichel #endif
5428c0984e5SSebastian Reichel 
lp8727_probe(struct i2c_client * cl)543d9cafca1SUwe Kleine-König static int lp8727_probe(struct i2c_client *cl)
5448c0984e5SSebastian Reichel {
5458c0984e5SSebastian Reichel 	struct lp8727_chg *pchg;
5468c0984e5SSebastian Reichel 	struct lp8727_platform_data *pdata;
5478c0984e5SSebastian Reichel 	int ret;
5488c0984e5SSebastian Reichel 
5498c0984e5SSebastian Reichel 	if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
5508c0984e5SSebastian Reichel 		return -EIO;
5518c0984e5SSebastian Reichel 
5528c0984e5SSebastian Reichel 	if (cl->dev.of_node) {
5538c0984e5SSebastian Reichel 		pdata = lp8727_parse_dt(&cl->dev);
5548c0984e5SSebastian Reichel 		if (IS_ERR(pdata))
5558c0984e5SSebastian Reichel 			return PTR_ERR(pdata);
5568c0984e5SSebastian Reichel 	} else {
5578c0984e5SSebastian Reichel 		pdata = dev_get_platdata(&cl->dev);
5588c0984e5SSebastian Reichel 	}
5598c0984e5SSebastian Reichel 
5608c0984e5SSebastian Reichel 	pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL);
5618c0984e5SSebastian Reichel 	if (!pchg)
5628c0984e5SSebastian Reichel 		return -ENOMEM;
5638c0984e5SSebastian Reichel 
5648c0984e5SSebastian Reichel 	pchg->client = cl;
5658c0984e5SSebastian Reichel 	pchg->dev = &cl->dev;
5668c0984e5SSebastian Reichel 	pchg->pdata = pdata;
5678c0984e5SSebastian Reichel 	i2c_set_clientdata(cl, pchg);
5688c0984e5SSebastian Reichel 
5698c0984e5SSebastian Reichel 	mutex_init(&pchg->xfer_lock);
5708c0984e5SSebastian Reichel 
5718c0984e5SSebastian Reichel 	ret = lp8727_init_device(pchg);
5728c0984e5SSebastian Reichel 	if (ret) {
5738c0984e5SSebastian Reichel 		dev_err(pchg->dev, "i2c communication err: %d", ret);
5748c0984e5SSebastian Reichel 		return ret;
5758c0984e5SSebastian Reichel 	}
5768c0984e5SSebastian Reichel 
5778c0984e5SSebastian Reichel 	ret = lp8727_register_psy(pchg);
5788c0984e5SSebastian Reichel 	if (ret) {
5798c0984e5SSebastian Reichel 		dev_err(pchg->dev, "power supplies register err: %d", ret);
5808c0984e5SSebastian Reichel 		return ret;
5818c0984e5SSebastian Reichel 	}
5828c0984e5SSebastian Reichel 
5838c0984e5SSebastian Reichel 	ret = lp8727_setup_irq(pchg);
5848c0984e5SSebastian Reichel 	if (ret) {
5858c0984e5SSebastian Reichel 		dev_err(pchg->dev, "irq handler err: %d", ret);
5868c0984e5SSebastian Reichel 		lp8727_unregister_psy(pchg);
5878c0984e5SSebastian Reichel 		return ret;
5888c0984e5SSebastian Reichel 	}
5898c0984e5SSebastian Reichel 
5908c0984e5SSebastian Reichel 	return 0;
5918c0984e5SSebastian Reichel }
5928c0984e5SSebastian Reichel 
lp8727_remove(struct i2c_client * cl)593ed5c2f5fSUwe Kleine-König static void lp8727_remove(struct i2c_client *cl)
5948c0984e5SSebastian Reichel {
5958c0984e5SSebastian Reichel 	struct lp8727_chg *pchg = i2c_get_clientdata(cl);
5968c0984e5SSebastian Reichel 
5978c0984e5SSebastian Reichel 	lp8727_release_irq(pchg);
5988c0984e5SSebastian Reichel 	lp8727_unregister_psy(pchg);
5998c0984e5SSebastian Reichel }
6008c0984e5SSebastian Reichel 
601ea66715dSKrzysztof Kozlowski static const struct of_device_id lp8727_dt_ids[] __maybe_unused = {
6028c0984e5SSebastian Reichel 	{ .compatible = "ti,lp8727", },
6038c0984e5SSebastian Reichel 	{ }
6048c0984e5SSebastian Reichel };
6058c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(of, lp8727_dt_ids);
6068c0984e5SSebastian Reichel 
6078c0984e5SSebastian Reichel static const struct i2c_device_id lp8727_ids[] = {
6088c0984e5SSebastian Reichel 	{"lp8727", 0},
6098c0984e5SSebastian Reichel 	{ }
6108c0984e5SSebastian Reichel };
6118c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(i2c, lp8727_ids);
6128c0984e5SSebastian Reichel 
6138c0984e5SSebastian Reichel static struct i2c_driver lp8727_driver = {
6148c0984e5SSebastian Reichel 	.driver = {
6158c0984e5SSebastian Reichel 		   .name = "lp8727",
6168c0984e5SSebastian Reichel 		   .of_match_table = of_match_ptr(lp8727_dt_ids),
6178c0984e5SSebastian Reichel 		   },
618*fe20b1dcSUwe Kleine-König 	.probe = lp8727_probe,
6198c0984e5SSebastian Reichel 	.remove = lp8727_remove,
6208c0984e5SSebastian Reichel 	.id_table = lp8727_ids,
6218c0984e5SSebastian Reichel };
6228c0984e5SSebastian Reichel module_i2c_driver(lp8727_driver);
6238c0984e5SSebastian Reichel 
6248c0984e5SSebastian Reichel MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
6258c0984e5SSebastian Reichel MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
6268c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
627