18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Battery charger driver for Dialog Semiconductor DA9030
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008 Compulab, Ltd.
68c2ecf20Sopenharmony_ci * 	Mike Rapoport <mike@compulab.co.il>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci#include <linux/device.h>
148c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
178c2ecf20Sopenharmony_ci#include <linux/power_supply.h>
188c2ecf20Sopenharmony_ci#include <linux/mfd/da903x.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
218c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
228c2ecf20Sopenharmony_ci#include <linux/notifier.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define DA9030_FAULT_LOG		0x0a
258c2ecf20Sopenharmony_ci#define DA9030_FAULT_LOG_OVER_TEMP	(1 << 7)
268c2ecf20Sopenharmony_ci#define DA9030_FAULT_LOG_VBAT_OVER	(1 << 4)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define DA9030_CHARGE_CONTROL		0x28
298c2ecf20Sopenharmony_ci#define DA9030_CHRG_CHARGER_ENABLE	(1 << 7)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define DA9030_ADC_MAN_CONTROL		0x30
328c2ecf20Sopenharmony_ci#define DA9030_ADC_TBATREF_ENABLE	(1 << 5)
338c2ecf20Sopenharmony_ci#define DA9030_ADC_LDO_INT_ENABLE	(1 << 4)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define DA9030_ADC_AUTO_CONTROL		0x31
368c2ecf20Sopenharmony_ci#define DA9030_ADC_TBAT_ENABLE		(1 << 5)
378c2ecf20Sopenharmony_ci#define DA9030_ADC_VBAT_IN_TXON		(1 << 4)
388c2ecf20Sopenharmony_ci#define DA9030_ADC_VCH_ENABLE		(1 << 3)
398c2ecf20Sopenharmony_ci#define DA9030_ADC_ICH_ENABLE		(1 << 2)
408c2ecf20Sopenharmony_ci#define DA9030_ADC_VBAT_ENABLE		(1 << 1)
418c2ecf20Sopenharmony_ci#define DA9030_ADC_AUTO_SLEEP_ENABLE	(1 << 0)
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define DA9030_VBATMON		0x32
448c2ecf20Sopenharmony_ci#define DA9030_VBATMONTXON	0x33
458c2ecf20Sopenharmony_ci#define DA9030_TBATHIGHP	0x34
468c2ecf20Sopenharmony_ci#define DA9030_TBATHIGHN	0x35
478c2ecf20Sopenharmony_ci#define DA9030_TBATLOW		0x36
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define DA9030_VBAT_RES		0x41
508c2ecf20Sopenharmony_ci#define DA9030_VBATMIN_RES	0x42
518c2ecf20Sopenharmony_ci#define DA9030_VBATMINTXON_RES	0x43
528c2ecf20Sopenharmony_ci#define DA9030_ICHMAX_RES	0x44
538c2ecf20Sopenharmony_ci#define DA9030_ICHMIN_RES	0x45
548c2ecf20Sopenharmony_ci#define DA9030_ICHAVERAGE_RES	0x46
558c2ecf20Sopenharmony_ci#define DA9030_VCHMAX_RES	0x47
568c2ecf20Sopenharmony_ci#define DA9030_VCHMIN_RES	0x48
578c2ecf20Sopenharmony_ci#define DA9030_TBAT_RES		0x49
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistruct da9030_adc_res {
608c2ecf20Sopenharmony_ci	uint8_t vbat_res;
618c2ecf20Sopenharmony_ci	uint8_t vbatmin_res;
628c2ecf20Sopenharmony_ci	uint8_t vbatmintxon;
638c2ecf20Sopenharmony_ci	uint8_t ichmax_res;
648c2ecf20Sopenharmony_ci	uint8_t ichmin_res;
658c2ecf20Sopenharmony_ci	uint8_t ichaverage_res;
668c2ecf20Sopenharmony_ci	uint8_t vchmax_res;
678c2ecf20Sopenharmony_ci	uint8_t vchmin_res;
688c2ecf20Sopenharmony_ci	uint8_t tbat_res;
698c2ecf20Sopenharmony_ci	uint8_t adc_in4_res;
708c2ecf20Sopenharmony_ci	uint8_t adc_in5_res;
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistruct da9030_battery_thresholds {
748c2ecf20Sopenharmony_ci	int tbat_low;
758c2ecf20Sopenharmony_ci	int tbat_high;
768c2ecf20Sopenharmony_ci	int tbat_restart;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	int vbat_low;
798c2ecf20Sopenharmony_ci	int vbat_crit;
808c2ecf20Sopenharmony_ci	int vbat_charge_start;
818c2ecf20Sopenharmony_ci	int vbat_charge_stop;
828c2ecf20Sopenharmony_ci	int vbat_charge_restart;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	int vcharge_min;
858c2ecf20Sopenharmony_ci	int vcharge_max;
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistruct da9030_charger {
898c2ecf20Sopenharmony_ci	struct power_supply *psy;
908c2ecf20Sopenharmony_ci	struct power_supply_desc psy_desc;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	struct device *master;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	struct da9030_adc_res adc;
958c2ecf20Sopenharmony_ci	struct delayed_work work;
968c2ecf20Sopenharmony_ci	unsigned int interval;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	struct power_supply_info *battery_info;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	struct da9030_battery_thresholds thresholds;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	unsigned int charge_milliamp;
1038c2ecf20Sopenharmony_ci	unsigned int charge_millivolt;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* charger status */
1068c2ecf20Sopenharmony_ci	bool chdet;
1078c2ecf20Sopenharmony_ci	uint8_t fault;
1088c2ecf20Sopenharmony_ci	int mA;
1098c2ecf20Sopenharmony_ci	int mV;
1108c2ecf20Sopenharmony_ci	bool is_on;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	struct notifier_block nb;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	/* platform callbacks for battery low and critical events */
1158c2ecf20Sopenharmony_ci	void (*battery_low)(void);
1168c2ecf20Sopenharmony_ci	void (*battery_critical)(void);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	struct dentry *debug_file;
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic inline int da9030_reg_to_mV(int reg)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	return ((reg * 2650) >> 8) + 2650;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic inline int da9030_millivolt_to_reg(int mV)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	return ((mV - 2650) << 8) / 2650;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline int da9030_reg_to_mA(int reg)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	return ((reg * 24000) >> 8) / 15;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
1378c2ecf20Sopenharmony_cistatic int bat_debug_show(struct seq_file *s, void *data)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	struct da9030_charger *charger = s->private;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	seq_printf(s, "charger is %s\n", charger->is_on ? "on" : "off");
1428c2ecf20Sopenharmony_ci	if (charger->chdet) {
1438c2ecf20Sopenharmony_ci		seq_printf(s, "iset = %dmA, vset = %dmV\n",
1448c2ecf20Sopenharmony_ci			   charger->mA, charger->mV);
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	seq_printf(s, "vbat_res = %d (%dmV)\n",
1488c2ecf20Sopenharmony_ci		   charger->adc.vbat_res,
1498c2ecf20Sopenharmony_ci		   da9030_reg_to_mV(charger->adc.vbat_res));
1508c2ecf20Sopenharmony_ci	seq_printf(s, "vbatmin_res = %d (%dmV)\n",
1518c2ecf20Sopenharmony_ci		   charger->adc.vbatmin_res,
1528c2ecf20Sopenharmony_ci		   da9030_reg_to_mV(charger->adc.vbatmin_res));
1538c2ecf20Sopenharmony_ci	seq_printf(s, "vbatmintxon = %d (%dmV)\n",
1548c2ecf20Sopenharmony_ci		   charger->adc.vbatmintxon,
1558c2ecf20Sopenharmony_ci		   da9030_reg_to_mV(charger->adc.vbatmintxon));
1568c2ecf20Sopenharmony_ci	seq_printf(s, "ichmax_res = %d (%dmA)\n",
1578c2ecf20Sopenharmony_ci		   charger->adc.ichmax_res,
1588c2ecf20Sopenharmony_ci		   da9030_reg_to_mV(charger->adc.ichmax_res));
1598c2ecf20Sopenharmony_ci	seq_printf(s, "ichmin_res = %d (%dmA)\n",
1608c2ecf20Sopenharmony_ci		   charger->adc.ichmin_res,
1618c2ecf20Sopenharmony_ci		   da9030_reg_to_mA(charger->adc.ichmin_res));
1628c2ecf20Sopenharmony_ci	seq_printf(s, "ichaverage_res = %d (%dmA)\n",
1638c2ecf20Sopenharmony_ci		   charger->adc.ichaverage_res,
1648c2ecf20Sopenharmony_ci		   da9030_reg_to_mA(charger->adc.ichaverage_res));
1658c2ecf20Sopenharmony_ci	seq_printf(s, "vchmax_res = %d (%dmV)\n",
1668c2ecf20Sopenharmony_ci		   charger->adc.vchmax_res,
1678c2ecf20Sopenharmony_ci		   da9030_reg_to_mA(charger->adc.vchmax_res));
1688c2ecf20Sopenharmony_ci	seq_printf(s, "vchmin_res = %d (%dmV)\n",
1698c2ecf20Sopenharmony_ci		   charger->adc.vchmin_res,
1708c2ecf20Sopenharmony_ci		   da9030_reg_to_mV(charger->adc.vchmin_res));
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	return 0;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(bat_debug);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic struct dentry *da9030_bat_create_debugfs(struct da9030_charger *charger)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	charger->debug_file = debugfs_create_file("charger", 0666, NULL,
1808c2ecf20Sopenharmony_ci						  charger, &bat_debug_fops);
1818c2ecf20Sopenharmony_ci	return charger->debug_file;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic void da9030_bat_remove_debugfs(struct da9030_charger *charger)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	debugfs_remove(charger->debug_file);
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci#else
1898c2ecf20Sopenharmony_cistatic inline struct dentry *da9030_bat_create_debugfs(struct da9030_charger *charger)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	return NULL;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_cistatic inline void da9030_bat_remove_debugfs(struct da9030_charger *charger)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci#endif
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic inline void da9030_read_adc(struct da9030_charger *charger,
1998c2ecf20Sopenharmony_ci				   struct da9030_adc_res *adc)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	da903x_reads(charger->master, DA9030_VBAT_RES,
2028c2ecf20Sopenharmony_ci		     sizeof(*adc), (uint8_t *)adc);
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic void da9030_charger_update_state(struct da9030_charger *charger)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	uint8_t val;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	da903x_read(charger->master, DA9030_CHARGE_CONTROL, &val);
2108c2ecf20Sopenharmony_ci	charger->is_on = (val & DA9030_CHRG_CHARGER_ENABLE) ? 1 : 0;
2118c2ecf20Sopenharmony_ci	charger->mA = ((val >> 3) & 0xf) * 100;
2128c2ecf20Sopenharmony_ci	charger->mV = (val & 0x7) * 50 + 4000;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	da9030_read_adc(charger, &charger->adc);
2158c2ecf20Sopenharmony_ci	da903x_read(charger->master, DA9030_FAULT_LOG, &charger->fault);
2168c2ecf20Sopenharmony_ci	charger->chdet = da903x_query_status(charger->master,
2178c2ecf20Sopenharmony_ci						     DA9030_STATUS_CHDET);
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic void da9030_set_charge(struct da9030_charger *charger, int on)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	uint8_t val;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	if (on) {
2258c2ecf20Sopenharmony_ci		val = DA9030_CHRG_CHARGER_ENABLE;
2268c2ecf20Sopenharmony_ci		val |= (charger->charge_milliamp / 100) << 3;
2278c2ecf20Sopenharmony_ci		val |= (charger->charge_millivolt - 4000) / 50;
2288c2ecf20Sopenharmony_ci		charger->is_on = 1;
2298c2ecf20Sopenharmony_ci	} else {
2308c2ecf20Sopenharmony_ci		val = 0;
2318c2ecf20Sopenharmony_ci		charger->is_on = 0;
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	da903x_write(charger->master, DA9030_CHARGE_CONTROL, val);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	power_supply_changed(charger->psy);
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic void da9030_charger_check_state(struct da9030_charger *charger)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	da9030_charger_update_state(charger);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/* we wake or boot with external power on */
2448c2ecf20Sopenharmony_ci	if (!charger->is_on) {
2458c2ecf20Sopenharmony_ci		if ((charger->chdet) &&
2468c2ecf20Sopenharmony_ci		    (charger->adc.vbat_res <
2478c2ecf20Sopenharmony_ci		     charger->thresholds.vbat_charge_start)) {
2488c2ecf20Sopenharmony_ci			da9030_set_charge(charger, 1);
2498c2ecf20Sopenharmony_ci		}
2508c2ecf20Sopenharmony_ci	} else {
2518c2ecf20Sopenharmony_ci		/* Charger has been pulled out */
2528c2ecf20Sopenharmony_ci		if (!charger->chdet) {
2538c2ecf20Sopenharmony_ci			da9030_set_charge(charger, 0);
2548c2ecf20Sopenharmony_ci			return;
2558c2ecf20Sopenharmony_ci		}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci		if (charger->adc.vbat_res >=
2588c2ecf20Sopenharmony_ci		    charger->thresholds.vbat_charge_stop) {
2598c2ecf20Sopenharmony_ci			da9030_set_charge(charger, 0);
2608c2ecf20Sopenharmony_ci			da903x_write(charger->master, DA9030_VBATMON,
2618c2ecf20Sopenharmony_ci				       charger->thresholds.vbat_charge_restart);
2628c2ecf20Sopenharmony_ci		} else if (charger->adc.vbat_res >
2638c2ecf20Sopenharmony_ci			   charger->thresholds.vbat_low) {
2648c2ecf20Sopenharmony_ci			/* we are charging and passed LOW_THRESH,
2658c2ecf20Sopenharmony_ci			   so upate DA9030 VBAT threshold
2668c2ecf20Sopenharmony_ci			 */
2678c2ecf20Sopenharmony_ci			da903x_write(charger->master, DA9030_VBATMON,
2688c2ecf20Sopenharmony_ci				     charger->thresholds.vbat_low);
2698c2ecf20Sopenharmony_ci		}
2708c2ecf20Sopenharmony_ci		if (charger->adc.vchmax_res > charger->thresholds.vcharge_max ||
2718c2ecf20Sopenharmony_ci		    charger->adc.vchmin_res < charger->thresholds.vcharge_min ||
2728c2ecf20Sopenharmony_ci		    /* Tempreture readings are negative */
2738c2ecf20Sopenharmony_ci		    charger->adc.tbat_res < charger->thresholds.tbat_high ||
2748c2ecf20Sopenharmony_ci		    charger->adc.tbat_res > charger->thresholds.tbat_low) {
2758c2ecf20Sopenharmony_ci			/* disable charger */
2768c2ecf20Sopenharmony_ci			da9030_set_charge(charger, 0);
2778c2ecf20Sopenharmony_ci		}
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic void da9030_charging_monitor(struct work_struct *work)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct da9030_charger *charger;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	charger = container_of(work, struct da9030_charger, work.work);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	da9030_charger_check_state(charger);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	/* reschedule for the next time */
2908c2ecf20Sopenharmony_ci	schedule_delayed_work(&charger->work, charger->interval);
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic enum power_supply_property da9030_battery_props[] = {
2948c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_MODEL_NAME,
2958c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_STATUS,
2968c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_HEALTH,
2978c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_TECHNOLOGY,
2988c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
2998c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
3008c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3018c2ecf20Sopenharmony_ci	POWER_SUPPLY_PROP_CURRENT_AVG,
3028c2ecf20Sopenharmony_ci};
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic void da9030_battery_check_status(struct da9030_charger *charger,
3058c2ecf20Sopenharmony_ci				    union power_supply_propval *val)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	if (charger->chdet) {
3088c2ecf20Sopenharmony_ci		if (charger->is_on)
3098c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_STATUS_CHARGING;
3108c2ecf20Sopenharmony_ci		else
3118c2ecf20Sopenharmony_ci			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
3128c2ecf20Sopenharmony_ci	} else {
3138c2ecf20Sopenharmony_ci		val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic void da9030_battery_check_health(struct da9030_charger *charger,
3188c2ecf20Sopenharmony_ci				    union power_supply_propval *val)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	if (charger->fault & DA9030_FAULT_LOG_OVER_TEMP)
3218c2ecf20Sopenharmony_ci		val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
3228c2ecf20Sopenharmony_ci	else if (charger->fault & DA9030_FAULT_LOG_VBAT_OVER)
3238c2ecf20Sopenharmony_ci		val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3248c2ecf20Sopenharmony_ci	else
3258c2ecf20Sopenharmony_ci		val->intval = POWER_SUPPLY_HEALTH_GOOD;
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic int da9030_battery_get_property(struct power_supply *psy,
3298c2ecf20Sopenharmony_ci				   enum power_supply_property psp,
3308c2ecf20Sopenharmony_ci				   union power_supply_propval *val)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	struct da9030_charger *charger = power_supply_get_drvdata(psy);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	switch (psp) {
3358c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_STATUS:
3368c2ecf20Sopenharmony_ci		da9030_battery_check_status(charger, val);
3378c2ecf20Sopenharmony_ci		break;
3388c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_HEALTH:
3398c2ecf20Sopenharmony_ci		da9030_battery_check_health(charger, val);
3408c2ecf20Sopenharmony_ci		break;
3418c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_TECHNOLOGY:
3428c2ecf20Sopenharmony_ci		val->intval = charger->battery_info->technology;
3438c2ecf20Sopenharmony_ci		break;
3448c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
3458c2ecf20Sopenharmony_ci		val->intval = charger->battery_info->voltage_max_design;
3468c2ecf20Sopenharmony_ci		break;
3478c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
3488c2ecf20Sopenharmony_ci		val->intval = charger->battery_info->voltage_min_design;
3498c2ecf20Sopenharmony_ci		break;
3508c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3518c2ecf20Sopenharmony_ci		val->intval = da9030_reg_to_mV(charger->adc.vbat_res) * 1000;
3528c2ecf20Sopenharmony_ci		break;
3538c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_CURRENT_AVG:
3548c2ecf20Sopenharmony_ci		val->intval =
3558c2ecf20Sopenharmony_ci			da9030_reg_to_mA(charger->adc.ichaverage_res) * 1000;
3568c2ecf20Sopenharmony_ci		break;
3578c2ecf20Sopenharmony_ci	case POWER_SUPPLY_PROP_MODEL_NAME:
3588c2ecf20Sopenharmony_ci		val->strval = charger->battery_info->name;
3598c2ecf20Sopenharmony_ci		break;
3608c2ecf20Sopenharmony_ci	default:
3618c2ecf20Sopenharmony_ci		break;
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	return 0;
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cistatic void da9030_battery_vbat_event(struct da9030_charger *charger)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	da9030_read_adc(charger, &charger->adc);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	if (charger->is_on)
3728c2ecf20Sopenharmony_ci		return;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (charger->adc.vbat_res < charger->thresholds.vbat_low) {
3758c2ecf20Sopenharmony_ci		/* set VBAT threshold for critical */
3768c2ecf20Sopenharmony_ci		da903x_write(charger->master, DA9030_VBATMON,
3778c2ecf20Sopenharmony_ci			     charger->thresholds.vbat_crit);
3788c2ecf20Sopenharmony_ci		if (charger->battery_low)
3798c2ecf20Sopenharmony_ci			charger->battery_low();
3808c2ecf20Sopenharmony_ci	} else if (charger->adc.vbat_res <
3818c2ecf20Sopenharmony_ci		   charger->thresholds.vbat_crit) {
3828c2ecf20Sopenharmony_ci		/* notify the system of battery critical */
3838c2ecf20Sopenharmony_ci		if (charger->battery_critical)
3848c2ecf20Sopenharmony_ci			charger->battery_critical();
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic int da9030_battery_event(struct notifier_block *nb, unsigned long event,
3898c2ecf20Sopenharmony_ci				void *data)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct da9030_charger *charger =
3928c2ecf20Sopenharmony_ci		container_of(nb, struct da9030_charger, nb);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	switch (event) {
3958c2ecf20Sopenharmony_ci	case DA9030_EVENT_CHDET:
3968c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&charger->work);
3978c2ecf20Sopenharmony_ci		schedule_work(&charger->work.work);
3988c2ecf20Sopenharmony_ci		break;
3998c2ecf20Sopenharmony_ci	case DA9030_EVENT_VBATMON:
4008c2ecf20Sopenharmony_ci		da9030_battery_vbat_event(charger);
4018c2ecf20Sopenharmony_ci		break;
4028c2ecf20Sopenharmony_ci	case DA9030_EVENT_CHIOVER:
4038c2ecf20Sopenharmony_ci	case DA9030_EVENT_TBAT:
4048c2ecf20Sopenharmony_ci		da9030_set_charge(charger, 0);
4058c2ecf20Sopenharmony_ci		break;
4068c2ecf20Sopenharmony_ci	}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	return 0;
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic void da9030_battery_convert_thresholds(struct da9030_charger *charger,
4128c2ecf20Sopenharmony_ci					      struct da9030_battery_info *pdata)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	charger->thresholds.tbat_low = pdata->tbat_low;
4158c2ecf20Sopenharmony_ci	charger->thresholds.tbat_high = pdata->tbat_high;
4168c2ecf20Sopenharmony_ci	charger->thresholds.tbat_restart  = pdata->tbat_restart;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	charger->thresholds.vbat_low =
4198c2ecf20Sopenharmony_ci		da9030_millivolt_to_reg(pdata->vbat_low);
4208c2ecf20Sopenharmony_ci	charger->thresholds.vbat_crit =
4218c2ecf20Sopenharmony_ci		da9030_millivolt_to_reg(pdata->vbat_crit);
4228c2ecf20Sopenharmony_ci	charger->thresholds.vbat_charge_start =
4238c2ecf20Sopenharmony_ci		da9030_millivolt_to_reg(pdata->vbat_charge_start);
4248c2ecf20Sopenharmony_ci	charger->thresholds.vbat_charge_stop =
4258c2ecf20Sopenharmony_ci		da9030_millivolt_to_reg(pdata->vbat_charge_stop);
4268c2ecf20Sopenharmony_ci	charger->thresholds.vbat_charge_restart =
4278c2ecf20Sopenharmony_ci		da9030_millivolt_to_reg(pdata->vbat_charge_restart);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	charger->thresholds.vcharge_min =
4308c2ecf20Sopenharmony_ci		da9030_millivolt_to_reg(pdata->vcharge_min);
4318c2ecf20Sopenharmony_ci	charger->thresholds.vcharge_max =
4328c2ecf20Sopenharmony_ci		da9030_millivolt_to_reg(pdata->vcharge_max);
4338c2ecf20Sopenharmony_ci}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic void da9030_battery_setup_psy(struct da9030_charger *charger)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct power_supply_desc *psy_desc = &charger->psy_desc;
4388c2ecf20Sopenharmony_ci	struct power_supply_info *info = charger->battery_info;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	psy_desc->name = info->name;
4418c2ecf20Sopenharmony_ci	psy_desc->use_for_apm = info->use_for_apm;
4428c2ecf20Sopenharmony_ci	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
4438c2ecf20Sopenharmony_ci	psy_desc->get_property = da9030_battery_get_property;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	psy_desc->properties = da9030_battery_props;
4468c2ecf20Sopenharmony_ci	psy_desc->num_properties = ARRAY_SIZE(da9030_battery_props);
4478c2ecf20Sopenharmony_ci};
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_cistatic int da9030_battery_charger_init(struct da9030_charger *charger)
4508c2ecf20Sopenharmony_ci{
4518c2ecf20Sopenharmony_ci	char v[5];
4528c2ecf20Sopenharmony_ci	int ret;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	v[0] = v[1] = charger->thresholds.vbat_low;
4558c2ecf20Sopenharmony_ci	v[2] = charger->thresholds.tbat_high;
4568c2ecf20Sopenharmony_ci	v[3] = charger->thresholds.tbat_restart;
4578c2ecf20Sopenharmony_ci	v[4] = charger->thresholds.tbat_low;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	ret = da903x_writes(charger->master, DA9030_VBATMON, 5, v);
4608c2ecf20Sopenharmony_ci	if (ret)
4618c2ecf20Sopenharmony_ci		return ret;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	/*
4648c2ecf20Sopenharmony_ci	 * Enable reference voltage supply for ADC from the LDO_INTERNAL
4658c2ecf20Sopenharmony_ci	 * regulator. Must be set before ADC measurements can be made.
4668c2ecf20Sopenharmony_ci	 */
4678c2ecf20Sopenharmony_ci	ret = da903x_write(charger->master, DA9030_ADC_MAN_CONTROL,
4688c2ecf20Sopenharmony_ci			   DA9030_ADC_LDO_INT_ENABLE |
4698c2ecf20Sopenharmony_ci			   DA9030_ADC_TBATREF_ENABLE);
4708c2ecf20Sopenharmony_ci	if (ret)
4718c2ecf20Sopenharmony_ci		return ret;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	/* enable auto ADC measuremnts */
4748c2ecf20Sopenharmony_ci	return da903x_write(charger->master, DA9030_ADC_AUTO_CONTROL,
4758c2ecf20Sopenharmony_ci			    DA9030_ADC_TBAT_ENABLE | DA9030_ADC_VBAT_IN_TXON |
4768c2ecf20Sopenharmony_ci			    DA9030_ADC_VCH_ENABLE | DA9030_ADC_ICH_ENABLE |
4778c2ecf20Sopenharmony_ci			    DA9030_ADC_VBAT_ENABLE |
4788c2ecf20Sopenharmony_ci			    DA9030_ADC_AUTO_SLEEP_ENABLE);
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic int da9030_battery_probe(struct platform_device *pdev)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	struct da9030_charger *charger;
4848c2ecf20Sopenharmony_ci	struct power_supply_config psy_cfg = {};
4858c2ecf20Sopenharmony_ci	struct da9030_battery_info *pdata = pdev->dev.platform_data;
4868c2ecf20Sopenharmony_ci	int ret;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	if (pdata == NULL)
4898c2ecf20Sopenharmony_ci		return -EINVAL;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	if (pdata->charge_milliamp >= 1500 ||
4928c2ecf20Sopenharmony_ci	    pdata->charge_millivolt < 4000 ||
4938c2ecf20Sopenharmony_ci	    pdata->charge_millivolt > 4350)
4948c2ecf20Sopenharmony_ci		return -EINVAL;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
4978c2ecf20Sopenharmony_ci	if (charger == NULL)
4988c2ecf20Sopenharmony_ci		return -ENOMEM;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	charger->master = pdev->dev.parent;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	/* 10 seconds between monitor runs unless platform defines other
5038c2ecf20Sopenharmony_ci	   interval */
5048c2ecf20Sopenharmony_ci	charger->interval = msecs_to_jiffies(
5058c2ecf20Sopenharmony_ci		(pdata->batmon_interval ? : 10) * 1000);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	charger->charge_milliamp = pdata->charge_milliamp;
5088c2ecf20Sopenharmony_ci	charger->charge_millivolt = pdata->charge_millivolt;
5098c2ecf20Sopenharmony_ci	charger->battery_info = pdata->battery_info;
5108c2ecf20Sopenharmony_ci	charger->battery_low = pdata->battery_low;
5118c2ecf20Sopenharmony_ci	charger->battery_critical = pdata->battery_critical;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	da9030_battery_convert_thresholds(charger, pdata);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	ret = da9030_battery_charger_init(charger);
5168c2ecf20Sopenharmony_ci	if (ret)
5178c2ecf20Sopenharmony_ci		goto err_charger_init;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&charger->work, da9030_charging_monitor);
5208c2ecf20Sopenharmony_ci	schedule_delayed_work(&charger->work, charger->interval);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	charger->nb.notifier_call = da9030_battery_event;
5238c2ecf20Sopenharmony_ci	ret = da903x_register_notifier(charger->master, &charger->nb,
5248c2ecf20Sopenharmony_ci				       DA9030_EVENT_CHDET |
5258c2ecf20Sopenharmony_ci				       DA9030_EVENT_VBATMON |
5268c2ecf20Sopenharmony_ci				       DA9030_EVENT_CHIOVER |
5278c2ecf20Sopenharmony_ci				       DA9030_EVENT_TBAT);
5288c2ecf20Sopenharmony_ci	if (ret)
5298c2ecf20Sopenharmony_ci		goto err_notifier;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	da9030_battery_setup_psy(charger);
5328c2ecf20Sopenharmony_ci	psy_cfg.drv_data = charger;
5338c2ecf20Sopenharmony_ci	charger->psy = power_supply_register(&pdev->dev, &charger->psy_desc,
5348c2ecf20Sopenharmony_ci					     &psy_cfg);
5358c2ecf20Sopenharmony_ci	if (IS_ERR(charger->psy)) {
5368c2ecf20Sopenharmony_ci		ret = PTR_ERR(charger->psy);
5378c2ecf20Sopenharmony_ci		goto err_ps_register;
5388c2ecf20Sopenharmony_ci	}
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	charger->debug_file = da9030_bat_create_debugfs(charger);
5418c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, charger);
5428c2ecf20Sopenharmony_ci	return 0;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_cierr_ps_register:
5458c2ecf20Sopenharmony_ci	da903x_unregister_notifier(charger->master, &charger->nb,
5468c2ecf20Sopenharmony_ci				   DA9030_EVENT_CHDET | DA9030_EVENT_VBATMON |
5478c2ecf20Sopenharmony_ci				   DA9030_EVENT_CHIOVER | DA9030_EVENT_TBAT);
5488c2ecf20Sopenharmony_cierr_notifier:
5498c2ecf20Sopenharmony_ci	cancel_delayed_work(&charger->work);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_cierr_charger_init:
5528c2ecf20Sopenharmony_ci	return ret;
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic int da9030_battery_remove(struct platform_device *dev)
5568c2ecf20Sopenharmony_ci{
5578c2ecf20Sopenharmony_ci	struct da9030_charger *charger = platform_get_drvdata(dev);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	da9030_bat_remove_debugfs(charger);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	da903x_unregister_notifier(charger->master, &charger->nb,
5628c2ecf20Sopenharmony_ci				   DA9030_EVENT_CHDET | DA9030_EVENT_VBATMON |
5638c2ecf20Sopenharmony_ci				   DA9030_EVENT_CHIOVER | DA9030_EVENT_TBAT);
5648c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&charger->work);
5658c2ecf20Sopenharmony_ci	da9030_set_charge(charger, 0);
5668c2ecf20Sopenharmony_ci	power_supply_unregister(charger->psy);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	return 0;
5698c2ecf20Sopenharmony_ci}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_cistatic struct platform_driver da903x_battery_driver = {
5728c2ecf20Sopenharmony_ci	.driver	= {
5738c2ecf20Sopenharmony_ci		.name	= "da903x-battery",
5748c2ecf20Sopenharmony_ci	},
5758c2ecf20Sopenharmony_ci	.probe = da9030_battery_probe,
5768c2ecf20Sopenharmony_ci	.remove = da9030_battery_remove,
5778c2ecf20Sopenharmony_ci};
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cimodule_platform_driver(da903x_battery_driver);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("DA9030 battery charger driver");
5828c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mike Rapoport, CompuLab");
5838c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
584