xref: /linux/drivers/power/supply/bd99954-charger.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
10902f836SMatti Vaittinen // SPDX-License-Identifier: GPL-2.0-only
20902f836SMatti Vaittinen /*
30902f836SMatti Vaittinen  * ROHM BD99954 charger driver
40902f836SMatti Vaittinen  *
50902f836SMatti Vaittinen  * Copyright (C) 2020 Rohm Semiconductors
60902f836SMatti Vaittinen  *	Originally written by:
70902f836SMatti Vaittinen  *		Mikko Mutanen <mikko.mutanen@fi.rohmeurope.com>
80902f836SMatti Vaittinen  *		Markus Laine <markus.laine@fi.rohmeurope.com>
90902f836SMatti Vaittinen  *	Bugs added by:
100902f836SMatti Vaittinen  *		Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
110902f836SMatti Vaittinen  */
120902f836SMatti Vaittinen 
130902f836SMatti Vaittinen /*
140902f836SMatti Vaittinen  *   The battery charging profile of BD99954.
150902f836SMatti Vaittinen  *
160902f836SMatti Vaittinen  *   Curve (1) represents charging current.
170902f836SMatti Vaittinen  *   Curve (2) represents battery voltage.
180902f836SMatti Vaittinen  *
190902f836SMatti Vaittinen  *   The BD99954 data sheet divides charging to three phases.
200902f836SMatti Vaittinen  *   a) Trickle-charge with constant current (8).
210902f836SMatti Vaittinen  *   b) pre-charge with constant current (6)
220902f836SMatti Vaittinen  *   c) fast-charge, first with constant current (5) phase. After
230902f836SMatti Vaittinen  *      the battery voltage has reached target level (4) we have constant
240902f836SMatti Vaittinen  *      voltage phase until charging current has dropped to termination
250902f836SMatti Vaittinen  *      level (7)
260902f836SMatti Vaittinen  *
270902f836SMatti Vaittinen  *    V ^                                                        ^ I
280902f836SMatti Vaittinen  *      .                                                        .
290902f836SMatti Vaittinen  *      .                                                        .
300902f836SMatti Vaittinen  *(4)` `.` ` ` ` ` ` ` ` ` ` ` ` ` ` ----------------------------.
310902f836SMatti Vaittinen  *      .                           :/                           .
320902f836SMatti Vaittinen  *      .                     o----+/:/ ` ` ` ` ` ` ` ` ` ` ` ` `.` ` (5)
330902f836SMatti Vaittinen  *      .                     +   ::  +                          .
340902f836SMatti Vaittinen  *      .                     +  /-   --                         .
350902f836SMatti Vaittinen  *      .                     +`/-     +                         .
360902f836SMatti Vaittinen  *      .                     o/-      -:                        .
370902f836SMatti Vaittinen  *      .                    .s.        +`                       .
380902f836SMatti Vaittinen  *      .                  .--+         `/                       .
390902f836SMatti Vaittinen  *      .               ..``  +          .:                      .
400902f836SMatti Vaittinen  *      .             -`      +           --                     .
410902f836SMatti Vaittinen  *      .    (2)  ...``       +            :-                    .
420902f836SMatti Vaittinen  *      .    ...``            +             -:                   .
430902f836SMatti Vaittinen  *(3)` `.`.""  ` ` ` `+-------- ` ` ` ` ` ` `.:` ` ` ` ` ` ` ` ` .` ` (6)
440902f836SMatti Vaittinen  *      .             +                       `:.                .
450902f836SMatti Vaittinen  *      .             +                         -:               .
460902f836SMatti Vaittinen  *      .             +                           -:.            .
470902f836SMatti Vaittinen  *      .             +                             .--.         .
480902f836SMatti Vaittinen  *      .   (1)       +                                `.+` ` ` `.` ` (7)
490902f836SMatti Vaittinen  *      -..............` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` + ` ` ` .` ` (8)
500902f836SMatti Vaittinen  *      .                                                +       -
510902f836SMatti Vaittinen  *      -------------------------------------------------+++++++++-->
520902f836SMatti Vaittinen  *      |   trickle   |  pre  |          fast            |
530902f836SMatti Vaittinen  *
540902f836SMatti Vaittinen  * Details of DT properties for different limits can be found from BD99954
550902f836SMatti Vaittinen  * device tree binding documentation.
560902f836SMatti Vaittinen  */
570902f836SMatti Vaittinen 
580902f836SMatti Vaittinen #include <linux/delay.h>
590902f836SMatti Vaittinen #include <linux/gpio/consumer.h>
600902f836SMatti Vaittinen #include <linux/interrupt.h>
610902f836SMatti Vaittinen #include <linux/i2c.h>
620902f836SMatti Vaittinen #include <linux/kernel.h>
630902f836SMatti Vaittinen #include <linux/linear_range.h>
640902f836SMatti Vaittinen #include <linux/module.h>
650902f836SMatti Vaittinen #include <linux/mod_devicetable.h>
660902f836SMatti Vaittinen #include <linux/power_supply.h>
670902f836SMatti Vaittinen #include <linux/property.h>
680902f836SMatti Vaittinen #include <linux/regmap.h>
690902f836SMatti Vaittinen #include <linux/types.h>
700902f836SMatti Vaittinen 
710902f836SMatti Vaittinen #include "bd99954-charger.h"
720902f836SMatti Vaittinen 
730902f836SMatti Vaittinen /* Initial field values, converted to initial register values */
740902f836SMatti Vaittinen struct bd9995x_init_data {
750902f836SMatti Vaittinen 	u16 vsysreg_set;	/* VSYS Regulation Setting */
760902f836SMatti Vaittinen 	u16 ibus_lim_set;	/* VBUS input current limitation */
770902f836SMatti Vaittinen 	u16 icc_lim_set;	/* VCC/VACP Input Current Limit Setting */
780902f836SMatti Vaittinen 	u16 itrich_set;		/* Trickle-charge Current Setting */
790902f836SMatti Vaittinen 	u16 iprech_set;		/* Pre-Charge Current Setting */
800902f836SMatti Vaittinen 	u16 ichg_set;		/* Fast-Charge constant current */
810902f836SMatti Vaittinen 	u16 vfastchg_reg_set1;	/* Fast Charging Regulation Voltage */
820902f836SMatti Vaittinen 	u16 vprechg_th_set;	/* Pre-charge Voltage Threshold Setting */
830902f836SMatti Vaittinen 	u16 vrechg_set;		/* Re-charge Battery Voltage Setting */
840902f836SMatti Vaittinen 	u16 vbatovp_set;	/* Battery Over Voltage Threshold Setting */
850902f836SMatti Vaittinen 	u16 iterm_set;		/* Charging termination current */
860902f836SMatti Vaittinen };
870902f836SMatti Vaittinen 
880902f836SMatti Vaittinen struct bd9995x_state {
890902f836SMatti Vaittinen 	u8 online;
900902f836SMatti Vaittinen 	u16 chgstm_status;
910902f836SMatti Vaittinen 	u16 vbat_vsys_status;
920902f836SMatti Vaittinen 	u16 vbus_vcc_status;
930902f836SMatti Vaittinen };
940902f836SMatti Vaittinen 
950902f836SMatti Vaittinen struct bd9995x_device {
960902f836SMatti Vaittinen 	struct i2c_client *client;
970902f836SMatti Vaittinen 	struct device *dev;
980902f836SMatti Vaittinen 	struct power_supply *charger;
990902f836SMatti Vaittinen 
1000902f836SMatti Vaittinen 	struct regmap *rmap;
1010902f836SMatti Vaittinen 	struct regmap_field *rmap_fields[F_MAX_FIELDS];
1020902f836SMatti Vaittinen 
1030902f836SMatti Vaittinen 	int chip_id;
1040902f836SMatti Vaittinen 	int chip_rev;
1050902f836SMatti Vaittinen 	struct bd9995x_init_data init_data;
1060902f836SMatti Vaittinen 	struct bd9995x_state state;
1070902f836SMatti Vaittinen 
1080902f836SMatti Vaittinen 	struct mutex lock; /* Protect state data */
1090902f836SMatti Vaittinen };
1100902f836SMatti Vaittinen 
1110902f836SMatti Vaittinen static const struct regmap_range bd9995x_readonly_reg_ranges[] = {
1120902f836SMatti Vaittinen 	regmap_reg_range(CHGSTM_STATUS, SEL_ILIM_VAL),
1130902f836SMatti Vaittinen 	regmap_reg_range(IOUT_DACIN_VAL, IOUT_DACIN_VAL),
1140902f836SMatti Vaittinen 	regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
1150902f836SMatti Vaittinen 	regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
1160902f836SMatti Vaittinen 	regmap_reg_range(CHIP_ID, CHIP_REV),
1170902f836SMatti Vaittinen 	regmap_reg_range(SYSTEM_STATUS, SYSTEM_STATUS),
1180902f836SMatti Vaittinen 	regmap_reg_range(IBATP_VAL, VBAT_AVE_VAL),
1190902f836SMatti Vaittinen 	regmap_reg_range(VTH_VAL, EXTIADP_AVE_VAL),
1200902f836SMatti Vaittinen };
1210902f836SMatti Vaittinen 
1220902f836SMatti Vaittinen static const struct regmap_access_table bd9995x_writeable_regs = {
1230902f836SMatti Vaittinen 	.no_ranges = bd9995x_readonly_reg_ranges,
1240902f836SMatti Vaittinen 	.n_no_ranges = ARRAY_SIZE(bd9995x_readonly_reg_ranges),
1250902f836SMatti Vaittinen };
1260902f836SMatti Vaittinen 
1270902f836SMatti Vaittinen static const struct regmap_range bd9995x_volatile_reg_ranges[] = {
1280902f836SMatti Vaittinen 	regmap_reg_range(CHGSTM_STATUS, WDT_STATUS),
1290902f836SMatti Vaittinen 	regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
1300902f836SMatti Vaittinen 	regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
1310902f836SMatti Vaittinen 	regmap_reg_range(INT0_STATUS, INT7_STATUS),
1320902f836SMatti Vaittinen 	regmap_reg_range(SYSTEM_STATUS, SYSTEM_CTRL_SET),
1330902f836SMatti Vaittinen 	regmap_reg_range(IBATP_VAL, EXTIADP_AVE_VAL), /* Measurement regs */
1340902f836SMatti Vaittinen };
1350902f836SMatti Vaittinen 
1360902f836SMatti Vaittinen static const struct regmap_access_table bd9995x_volatile_regs = {
1370902f836SMatti Vaittinen 	.yes_ranges = bd9995x_volatile_reg_ranges,
1380902f836SMatti Vaittinen 	.n_yes_ranges = ARRAY_SIZE(bd9995x_volatile_reg_ranges),
1390902f836SMatti Vaittinen };
1400902f836SMatti Vaittinen 
1410902f836SMatti Vaittinen static const struct regmap_range_cfg regmap_range_cfg[] = {
1420902f836SMatti Vaittinen 	{
1430902f836SMatti Vaittinen 	.selector_reg     = MAP_SET,
1440902f836SMatti Vaittinen 	.selector_mask    = 0xFFFF,
1450902f836SMatti Vaittinen 	.selector_shift   = 0,
1460902f836SMatti Vaittinen 	.window_start     = 0,
1470902f836SMatti Vaittinen 	.window_len       = 0x100,
1480902f836SMatti Vaittinen 	.range_min        = 0 * 0x100,
1490902f836SMatti Vaittinen 	.range_max        = 3 * 0x100,
1500902f836SMatti Vaittinen 	},
1510902f836SMatti Vaittinen };
1520902f836SMatti Vaittinen 
1530902f836SMatti Vaittinen static const struct regmap_config bd9995x_regmap_config = {
1540902f836SMatti Vaittinen 	.reg_bits = 8,
1550902f836SMatti Vaittinen 	.val_bits = 16,
1560902f836SMatti Vaittinen 	.reg_stride = 1,
1570902f836SMatti Vaittinen 
1580902f836SMatti Vaittinen 	.max_register = 3 * 0x100,
1590902f836SMatti Vaittinen 	.cache_type = REGCACHE_RBTREE,
1600902f836SMatti Vaittinen 
1610902f836SMatti Vaittinen 	.ranges = regmap_range_cfg,
1620902f836SMatti Vaittinen 	.num_ranges = ARRAY_SIZE(regmap_range_cfg),
1630902f836SMatti Vaittinen 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
1640902f836SMatti Vaittinen 	.wr_table = &bd9995x_writeable_regs,
1650902f836SMatti Vaittinen 	.volatile_table = &bd9995x_volatile_regs,
1660902f836SMatti Vaittinen };
1670902f836SMatti Vaittinen 
1680902f836SMatti Vaittinen enum bd9995x_chrg_fault {
1690902f836SMatti Vaittinen 	CHRG_FAULT_NORMAL,
1700902f836SMatti Vaittinen 	CHRG_FAULT_INPUT,
1710902f836SMatti Vaittinen 	CHRG_FAULT_THERMAL_SHUTDOWN,
1720902f836SMatti Vaittinen 	CHRG_FAULT_TIMER_EXPIRED,
1730902f836SMatti Vaittinen };
1740902f836SMatti Vaittinen 
bd9995x_get_prop_batt_health(struct bd9995x_device * bd)1750902f836SMatti Vaittinen static int bd9995x_get_prop_batt_health(struct bd9995x_device *bd)
1760902f836SMatti Vaittinen {
1770902f836SMatti Vaittinen 	int ret, tmp;
1780902f836SMatti Vaittinen 
1790902f836SMatti Vaittinen 	ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
1800902f836SMatti Vaittinen 	if (ret)
1810902f836SMatti Vaittinen 		return POWER_SUPPLY_HEALTH_UNKNOWN;
1820902f836SMatti Vaittinen 
1830902f836SMatti Vaittinen 	/* TODO: Check these against datasheet page 34 */
1840902f836SMatti Vaittinen 
1850902f836SMatti Vaittinen 	switch (tmp) {
1860902f836SMatti Vaittinen 	case ROOM:
1870902f836SMatti Vaittinen 		return POWER_SUPPLY_HEALTH_GOOD;
1880902f836SMatti Vaittinen 	case HOT1:
1890902f836SMatti Vaittinen 	case HOT2:
1900902f836SMatti Vaittinen 	case HOT3:
1910902f836SMatti Vaittinen 		return POWER_SUPPLY_HEALTH_OVERHEAT;
1920902f836SMatti Vaittinen 	case COLD1:
1930902f836SMatti Vaittinen 	case COLD2:
1940902f836SMatti Vaittinen 		return POWER_SUPPLY_HEALTH_COLD;
1950902f836SMatti Vaittinen 	case TEMP_DIS:
1960902f836SMatti Vaittinen 	case BATT_OPEN:
1970902f836SMatti Vaittinen 	default:
1980902f836SMatti Vaittinen 		return POWER_SUPPLY_HEALTH_UNKNOWN;
1990902f836SMatti Vaittinen 	}
2000902f836SMatti Vaittinen }
2010902f836SMatti Vaittinen 
bd9995x_get_prop_charge_type(struct bd9995x_device * bd)2020902f836SMatti Vaittinen static int bd9995x_get_prop_charge_type(struct bd9995x_device *bd)
2030902f836SMatti Vaittinen {
2040902f836SMatti Vaittinen 	int ret, tmp;
2050902f836SMatti Vaittinen 
2060902f836SMatti Vaittinen 	ret = regmap_field_read(bd->rmap_fields[F_CHGSTM_STATE], &tmp);
2070902f836SMatti Vaittinen 	if (ret)
2080902f836SMatti Vaittinen 		return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
2090902f836SMatti Vaittinen 
2100902f836SMatti Vaittinen 	switch (tmp) {
2110902f836SMatti Vaittinen 	case CHGSTM_TRICKLE_CHARGE:
2120902f836SMatti Vaittinen 	case CHGSTM_PRE_CHARGE:
2130902f836SMatti Vaittinen 		return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
2140902f836SMatti Vaittinen 	case CHGSTM_FAST_CHARGE:
2150902f836SMatti Vaittinen 		return POWER_SUPPLY_CHARGE_TYPE_FAST;
2160902f836SMatti Vaittinen 	case CHGSTM_TOP_OFF:
2170902f836SMatti Vaittinen 	case CHGSTM_DONE:
2180902f836SMatti Vaittinen 	case CHGSTM_SUSPEND:
2190902f836SMatti Vaittinen 		return POWER_SUPPLY_CHARGE_TYPE_NONE;
2200902f836SMatti Vaittinen 	default: /* Rest of the states are error related, no charging */
2210902f836SMatti Vaittinen 		return POWER_SUPPLY_CHARGE_TYPE_NONE;
2220902f836SMatti Vaittinen 	}
2230902f836SMatti Vaittinen }
2240902f836SMatti Vaittinen 
bd9995x_get_prop_batt_present(struct bd9995x_device * bd)2250902f836SMatti Vaittinen static bool bd9995x_get_prop_batt_present(struct bd9995x_device *bd)
2260902f836SMatti Vaittinen {
2270902f836SMatti Vaittinen 	int ret, tmp;
2280902f836SMatti Vaittinen 
2290902f836SMatti Vaittinen 	ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
2300902f836SMatti Vaittinen 	if (ret)
2310902f836SMatti Vaittinen 		return false;
2320902f836SMatti Vaittinen 
2330902f836SMatti Vaittinen 	return tmp != BATT_OPEN;
2340902f836SMatti Vaittinen }
2350902f836SMatti Vaittinen 
bd9995x_get_prop_batt_voltage(struct bd9995x_device * bd)2360902f836SMatti Vaittinen static int bd9995x_get_prop_batt_voltage(struct bd9995x_device *bd)
2370902f836SMatti Vaittinen {
2380902f836SMatti Vaittinen 	int ret, tmp;
2390902f836SMatti Vaittinen 
2400902f836SMatti Vaittinen 	ret = regmap_field_read(bd->rmap_fields[F_VBAT_VAL], &tmp);
2410902f836SMatti Vaittinen 	if (ret)
2420902f836SMatti Vaittinen 		return 0;
2430902f836SMatti Vaittinen 
2440902f836SMatti Vaittinen 	tmp = min(tmp, 19200);
2450902f836SMatti Vaittinen 
2460902f836SMatti Vaittinen 	return tmp * 1000;
2470902f836SMatti Vaittinen }
2480902f836SMatti Vaittinen 
bd9995x_get_prop_batt_current(struct bd9995x_device * bd)2490902f836SMatti Vaittinen static int bd9995x_get_prop_batt_current(struct bd9995x_device *bd)
2500902f836SMatti Vaittinen {
2510902f836SMatti Vaittinen 	int ret, tmp;
2520902f836SMatti Vaittinen 
2530902f836SMatti Vaittinen 	ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
2540902f836SMatti Vaittinen 	if (ret)
2550902f836SMatti Vaittinen 		return 0;
2560902f836SMatti Vaittinen 
2570902f836SMatti Vaittinen 	return tmp * 1000;
2580902f836SMatti Vaittinen }
2590902f836SMatti Vaittinen 
2600902f836SMatti Vaittinen #define DEFAULT_BATTERY_TEMPERATURE 250
2610902f836SMatti Vaittinen 
bd9995x_get_prop_batt_temp(struct bd9995x_device * bd)2620902f836SMatti Vaittinen static int bd9995x_get_prop_batt_temp(struct bd9995x_device *bd)
2630902f836SMatti Vaittinen {
2640902f836SMatti Vaittinen 	int ret, tmp;
2650902f836SMatti Vaittinen 
2660902f836SMatti Vaittinen 	ret = regmap_field_read(bd->rmap_fields[F_THERM_VAL], &tmp);
2670902f836SMatti Vaittinen 	if (ret)
2680902f836SMatti Vaittinen 		return DEFAULT_BATTERY_TEMPERATURE;
2690902f836SMatti Vaittinen 
2700902f836SMatti Vaittinen 	return (200 - tmp) * 10;
2710902f836SMatti Vaittinen }
2720902f836SMatti Vaittinen 
bd9995x_power_supply_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)2730902f836SMatti Vaittinen static int bd9995x_power_supply_get_property(struct power_supply *psy,
2740902f836SMatti Vaittinen 					     enum power_supply_property psp,
2750902f836SMatti Vaittinen 					     union power_supply_propval *val)
2760902f836SMatti Vaittinen {
2770902f836SMatti Vaittinen 	int ret, tmp;
2780902f836SMatti Vaittinen 	struct bd9995x_device *bd = power_supply_get_drvdata(psy);
2790902f836SMatti Vaittinen 	struct bd9995x_state state;
2800902f836SMatti Vaittinen 
2810902f836SMatti Vaittinen 	mutex_lock(&bd->lock);
2820902f836SMatti Vaittinen 	state = bd->state;
2830902f836SMatti Vaittinen 	mutex_unlock(&bd->lock);
2840902f836SMatti Vaittinen 
2850902f836SMatti Vaittinen 	switch (psp) {
2860902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_STATUS:
2870902f836SMatti Vaittinen 		switch (state.chgstm_status) {
2880902f836SMatti Vaittinen 		case CHGSTM_TRICKLE_CHARGE:
2890902f836SMatti Vaittinen 		case CHGSTM_PRE_CHARGE:
2900902f836SMatti Vaittinen 		case CHGSTM_FAST_CHARGE:
2910902f836SMatti Vaittinen 		case CHGSTM_TOP_OFF:
2920902f836SMatti Vaittinen 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
2930902f836SMatti Vaittinen 			break;
2940902f836SMatti Vaittinen 
2950902f836SMatti Vaittinen 		case CHGSTM_DONE:
2960902f836SMatti Vaittinen 			val->intval = POWER_SUPPLY_STATUS_FULL;
2970902f836SMatti Vaittinen 			break;
2980902f836SMatti Vaittinen 
2990902f836SMatti Vaittinen 		case CHGSTM_SUSPEND:
3000902f836SMatti Vaittinen 		case CHGSTM_TEMPERATURE_ERROR_1:
3010902f836SMatti Vaittinen 		case CHGSTM_TEMPERATURE_ERROR_2:
3020902f836SMatti Vaittinen 		case CHGSTM_TEMPERATURE_ERROR_3:
3030902f836SMatti Vaittinen 		case CHGSTM_TEMPERATURE_ERROR_4:
3040902f836SMatti Vaittinen 		case CHGSTM_TEMPERATURE_ERROR_5:
3050902f836SMatti Vaittinen 		case CHGSTM_TEMPERATURE_ERROR_6:
3060902f836SMatti Vaittinen 		case CHGSTM_TEMPERATURE_ERROR_7:
3070902f836SMatti Vaittinen 		case CHGSTM_THERMAL_SHUT_DOWN_1:
3080902f836SMatti Vaittinen 		case CHGSTM_THERMAL_SHUT_DOWN_2:
3090902f836SMatti Vaittinen 		case CHGSTM_THERMAL_SHUT_DOWN_3:
3100902f836SMatti Vaittinen 		case CHGSTM_THERMAL_SHUT_DOWN_4:
3110902f836SMatti Vaittinen 		case CHGSTM_THERMAL_SHUT_DOWN_5:
3120902f836SMatti Vaittinen 		case CHGSTM_THERMAL_SHUT_DOWN_6:
3130902f836SMatti Vaittinen 		case CHGSTM_THERMAL_SHUT_DOWN_7:
3140902f836SMatti Vaittinen 		case CHGSTM_BATTERY_ERROR:
3150902f836SMatti Vaittinen 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
3160902f836SMatti Vaittinen 			break;
3170902f836SMatti Vaittinen 
3180902f836SMatti Vaittinen 		default:
3190902f836SMatti Vaittinen 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
3200902f836SMatti Vaittinen 			break;
3210902f836SMatti Vaittinen 		}
3220902f836SMatti Vaittinen 		break;
3230902f836SMatti Vaittinen 
3240902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_MANUFACTURER:
3250902f836SMatti Vaittinen 		val->strval = BD9995X_MANUFACTURER;
3260902f836SMatti Vaittinen 		break;
3270902f836SMatti Vaittinen 
3280902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_ONLINE:
3290902f836SMatti Vaittinen 		val->intval = state.online;
3300902f836SMatti Vaittinen 		break;
3310902f836SMatti Vaittinen 
3320902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
3330902f836SMatti Vaittinen 		ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
3340902f836SMatti Vaittinen 		if (ret)
3350902f836SMatti Vaittinen 			return ret;
3360902f836SMatti Vaittinen 		val->intval = tmp * 1000;
3370902f836SMatti Vaittinen 		break;
3380902f836SMatti Vaittinen 
3390902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_CHARGE_AVG:
3400902f836SMatti Vaittinen 		ret = regmap_field_read(bd->rmap_fields[F_IBATP_AVE_VAL], &tmp);
3410902f836SMatti Vaittinen 		if (ret)
3420902f836SMatti Vaittinen 			return ret;
3430902f836SMatti Vaittinen 		val->intval = tmp * 1000;
3440902f836SMatti Vaittinen 		break;
3450902f836SMatti Vaittinen 
3460902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
3470902f836SMatti Vaittinen 		/*
3480902f836SMatti Vaittinen 		 * Currently the DT uses this property to give the
3490902f836SMatti Vaittinen 		 * target current for fast-charging constant current phase.
3500902f836SMatti Vaittinen 		 * I think it is correct in a sense.
3510902f836SMatti Vaittinen 		 *
3520902f836SMatti Vaittinen 		 * Yet, this prop we read and return here is the programmed
3530902f836SMatti Vaittinen 		 * safety limit for combined input currents. This feels
3540902f836SMatti Vaittinen 		 * also correct in a sense.
3550902f836SMatti Vaittinen 		 *
3560902f836SMatti Vaittinen 		 * However, this results a mismatch to DT value and value
3570902f836SMatti Vaittinen 		 * read from sysfs.
3580902f836SMatti Vaittinen 		 */
3590902f836SMatti Vaittinen 		ret = regmap_field_read(bd->rmap_fields[F_SEL_ILIM_VAL], &tmp);
3600902f836SMatti Vaittinen 		if (ret)
3610902f836SMatti Vaittinen 			return ret;
3620902f836SMatti Vaittinen 		val->intval = tmp * 1000;
3630902f836SMatti Vaittinen 		break;
3640902f836SMatti Vaittinen 
3650902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
3660902f836SMatti Vaittinen 		if (!state.online) {
3670902f836SMatti Vaittinen 			val->intval = 0;
3680902f836SMatti Vaittinen 			break;
3690902f836SMatti Vaittinen 		}
3700902f836SMatti Vaittinen 
3710902f836SMatti Vaittinen 		ret = regmap_field_read(bd->rmap_fields[F_VFASTCHG_REG_SET1],
3720902f836SMatti Vaittinen 					&tmp);
3730902f836SMatti Vaittinen 		if (ret)
3740902f836SMatti Vaittinen 			return ret;
3750902f836SMatti Vaittinen 
3760902f836SMatti Vaittinen 		/*
3770902f836SMatti Vaittinen 		 * The actual range : 2560 to 19200 mV. No matter what the
3780902f836SMatti Vaittinen 		 * register says
3790902f836SMatti Vaittinen 		 */
3800902f836SMatti Vaittinen 		val->intval = clamp_val(tmp << 4, 2560, 19200);
3810902f836SMatti Vaittinen 		val->intval *= 1000;
3820902f836SMatti Vaittinen 		break;
3830902f836SMatti Vaittinen 
3840902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
3850902f836SMatti Vaittinen 		ret = regmap_field_read(bd->rmap_fields[F_ITERM_SET], &tmp);
3860902f836SMatti Vaittinen 		if (ret)
3870902f836SMatti Vaittinen 			return ret;
3880902f836SMatti Vaittinen 		/* Start step is 64 mA */
3890902f836SMatti Vaittinen 		val->intval = tmp << 6;
3900902f836SMatti Vaittinen 		/* Maximum is 1024 mA - no matter what register says */
3910902f836SMatti Vaittinen 		val->intval = min(val->intval, 1024);
3920902f836SMatti Vaittinen 		val->intval *= 1000;
3930902f836SMatti Vaittinen 		break;
3940902f836SMatti Vaittinen 
3950902f836SMatti Vaittinen 	/* Battery properties which we access through charger */
3960902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_PRESENT:
3970902f836SMatti Vaittinen 		val->intval = bd9995x_get_prop_batt_present(bd);
3980902f836SMatti Vaittinen 		break;
3990902f836SMatti Vaittinen 
4000902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4010902f836SMatti Vaittinen 		val->intval = bd9995x_get_prop_batt_voltage(bd);
4020902f836SMatti Vaittinen 		break;
4030902f836SMatti Vaittinen 
4040902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_CURRENT_NOW:
4050902f836SMatti Vaittinen 		val->intval = bd9995x_get_prop_batt_current(bd);
4060902f836SMatti Vaittinen 		break;
4070902f836SMatti Vaittinen 
4080902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
4090902f836SMatti Vaittinen 		val->intval = bd9995x_get_prop_charge_type(bd);
4100902f836SMatti Vaittinen 		break;
4110902f836SMatti Vaittinen 
4120902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_HEALTH:
4130902f836SMatti Vaittinen 		val->intval = bd9995x_get_prop_batt_health(bd);
4140902f836SMatti Vaittinen 		break;
4150902f836SMatti Vaittinen 
4160902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_TEMP:
4170902f836SMatti Vaittinen 		val->intval = bd9995x_get_prop_batt_temp(bd);
4180902f836SMatti Vaittinen 		break;
4190902f836SMatti Vaittinen 
4200902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_TECHNOLOGY:
4210902f836SMatti Vaittinen 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
4220902f836SMatti Vaittinen 		break;
4230902f836SMatti Vaittinen 
4240902f836SMatti Vaittinen 	case POWER_SUPPLY_PROP_MODEL_NAME:
4250902f836SMatti Vaittinen 		val->strval = "bd99954";
4260902f836SMatti Vaittinen 		break;
4270902f836SMatti Vaittinen 
4280902f836SMatti Vaittinen 	default:
4290902f836SMatti Vaittinen 		return -EINVAL;
4300902f836SMatti Vaittinen 
4310902f836SMatti Vaittinen 	}
4320902f836SMatti Vaittinen 
4330902f836SMatti Vaittinen 	return 0;
4340902f836SMatti Vaittinen }
4350902f836SMatti Vaittinen 
bd9995x_get_chip_state(struct bd9995x_device * bd,struct bd9995x_state * state)4360902f836SMatti Vaittinen static int bd9995x_get_chip_state(struct bd9995x_device *bd,
4370902f836SMatti Vaittinen 				  struct bd9995x_state *state)
4380902f836SMatti Vaittinen {
4390902f836SMatti Vaittinen 	int i, ret, tmp;
4400902f836SMatti Vaittinen 	struct {
4410902f836SMatti Vaittinen 		struct regmap_field *id;
4420902f836SMatti Vaittinen 		u16 *data;
4430902f836SMatti Vaittinen 	} state_fields[] = {
4440902f836SMatti Vaittinen 		{
4450902f836SMatti Vaittinen 			bd->rmap_fields[F_CHGSTM_STATE], &state->chgstm_status,
4460902f836SMatti Vaittinen 		}, {
4470902f836SMatti Vaittinen 			bd->rmap_fields[F_VBAT_VSYS_STATUS],
4480902f836SMatti Vaittinen 			&state->vbat_vsys_status,
4490902f836SMatti Vaittinen 		}, {
4500902f836SMatti Vaittinen 			bd->rmap_fields[F_VBUS_VCC_STATUS],
4510902f836SMatti Vaittinen 			&state->vbus_vcc_status,
4520902f836SMatti Vaittinen 		},
4530902f836SMatti Vaittinen 	};
4540902f836SMatti Vaittinen 
4550902f836SMatti Vaittinen 
4560902f836SMatti Vaittinen 	for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
4570902f836SMatti Vaittinen 		ret = regmap_field_read(state_fields[i].id, &tmp);
4580902f836SMatti Vaittinen 		if (ret)
4590902f836SMatti Vaittinen 			return ret;
4600902f836SMatti Vaittinen 
4610902f836SMatti Vaittinen 		*state_fields[i].data = tmp;
4620902f836SMatti Vaittinen 	}
4630902f836SMatti Vaittinen 
4640902f836SMatti Vaittinen 	if (state->vbus_vcc_status & STATUS_VCC_DET ||
4650902f836SMatti Vaittinen 	    state->vbus_vcc_status & STATUS_VBUS_DET)
4660902f836SMatti Vaittinen 		state->online = 1;
4670902f836SMatti Vaittinen 	else
4680902f836SMatti Vaittinen 		state->online = 0;
4690902f836SMatti Vaittinen 
4700902f836SMatti Vaittinen 	return 0;
4710902f836SMatti Vaittinen }
4720902f836SMatti Vaittinen 
bd9995x_irq_handler_thread(int irq,void * private)4730902f836SMatti Vaittinen static irqreturn_t bd9995x_irq_handler_thread(int irq, void *private)
4740902f836SMatti Vaittinen {
4750902f836SMatti Vaittinen 	struct bd9995x_device *bd = private;
4760902f836SMatti Vaittinen 	int ret, status, mask, i;
4770902f836SMatti Vaittinen 	unsigned long tmp;
4780902f836SMatti Vaittinen 	struct bd9995x_state state;
4790902f836SMatti Vaittinen 
4800902f836SMatti Vaittinen 	/*
4810902f836SMatti Vaittinen 	 * The bd9995x does not seem to generate big amount of interrupts.
4820902f836SMatti Vaittinen 	 * The logic regarding which interrupts can cause relevant
4830902f836SMatti Vaittinen 	 * status changes seem to be pretty complex.
4840902f836SMatti Vaittinen 	 *
4850902f836SMatti Vaittinen 	 * So lets implement really simple and hopefully bullet-proof handler:
4860902f836SMatti Vaittinen 	 * It does not really matter which IRQ we handle, we just go and
4870902f836SMatti Vaittinen 	 * re-read all interesting statuses + give the framework a nudge.
4880902f836SMatti Vaittinen 	 *
4890902f836SMatti Vaittinen 	 * Other option would be building a _complex_ and error prone logic
4900902f836SMatti Vaittinen 	 * trying to decide what could have been changed (resulting this IRQ
4910902f836SMatti Vaittinen 	 * we are now handling). During the normal operation the BD99954 does
4920902f836SMatti Vaittinen 	 * not seem to be generating much of interrupts so benefit from such
4930902f836SMatti Vaittinen 	 * logic would probably be minimal.
4940902f836SMatti Vaittinen 	 */
4950902f836SMatti Vaittinen 
4960902f836SMatti Vaittinen 	ret = regmap_read(bd->rmap, INT0_STATUS, &status);
4970902f836SMatti Vaittinen 	if (ret) {
4980902f836SMatti Vaittinen 		dev_err(bd->dev, "Failed to read IRQ status\n");
4990902f836SMatti Vaittinen 		return IRQ_NONE;
5000902f836SMatti Vaittinen 	}
5010902f836SMatti Vaittinen 
5020902f836SMatti Vaittinen 	ret = regmap_field_read(bd->rmap_fields[F_INT0_SET], &mask);
5030902f836SMatti Vaittinen 	if (ret) {
5040902f836SMatti Vaittinen 		dev_err(bd->dev, "Failed to read IRQ mask\n");
5050902f836SMatti Vaittinen 		return IRQ_NONE;
5060902f836SMatti Vaittinen 	}
5070902f836SMatti Vaittinen 
5080902f836SMatti Vaittinen 	/* Handle only IRQs that are not masked */
5090902f836SMatti Vaittinen 	status &= mask;
5100902f836SMatti Vaittinen 	tmp = status;
5110902f836SMatti Vaittinen 
5120902f836SMatti Vaittinen 	/* Lowest bit does not represent any sub-registers */
5130902f836SMatti Vaittinen 	tmp >>= 1;
5140902f836SMatti Vaittinen 
5150902f836SMatti Vaittinen 	/*
5160902f836SMatti Vaittinen 	 * Mask and ack IRQs we will handle (+ the idiot bit)
5170902f836SMatti Vaittinen 	 */
5180902f836SMatti Vaittinen 	ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], 0);
5190902f836SMatti Vaittinen 	if (ret) {
5200902f836SMatti Vaittinen 		dev_err(bd->dev, "Failed to mask F_INT0\n");
5210902f836SMatti Vaittinen 		return IRQ_NONE;
5220902f836SMatti Vaittinen 	}
5230902f836SMatti Vaittinen 
5240902f836SMatti Vaittinen 	ret = regmap_write(bd->rmap, INT0_STATUS, status);
5250902f836SMatti Vaittinen 	if (ret) {
5260902f836SMatti Vaittinen 		dev_err(bd->dev, "Failed to ack F_INT0\n");
5270902f836SMatti Vaittinen 		goto err_umask;
5280902f836SMatti Vaittinen 	}
5290902f836SMatti Vaittinen 
5300902f836SMatti Vaittinen 	for_each_set_bit(i, &tmp, 7) {
5310902f836SMatti Vaittinen 		int sub_status, sub_mask;
532*7b9fc309SColin Ian King 		static const int sub_status_reg[] = {
5330902f836SMatti Vaittinen 			INT1_STATUS, INT2_STATUS, INT3_STATUS, INT4_STATUS,
5340902f836SMatti Vaittinen 			INT5_STATUS, INT6_STATUS, INT7_STATUS,
5350902f836SMatti Vaittinen 		};
5360902f836SMatti Vaittinen 		struct regmap_field *sub_mask_f[] = {
5370902f836SMatti Vaittinen 			bd->rmap_fields[F_INT1_SET],
5380902f836SMatti Vaittinen 			bd->rmap_fields[F_INT2_SET],
5390902f836SMatti Vaittinen 			bd->rmap_fields[F_INT3_SET],
5400902f836SMatti Vaittinen 			bd->rmap_fields[F_INT4_SET],
5410902f836SMatti Vaittinen 			bd->rmap_fields[F_INT5_SET],
5420902f836SMatti Vaittinen 			bd->rmap_fields[F_INT6_SET],
5430902f836SMatti Vaittinen 			bd->rmap_fields[F_INT7_SET],
5440902f836SMatti Vaittinen 		};
5450902f836SMatti Vaittinen 
5460902f836SMatti Vaittinen 		/* Clear sub IRQs */
5470902f836SMatti Vaittinen 		ret = regmap_read(bd->rmap, sub_status_reg[i], &sub_status);
5480902f836SMatti Vaittinen 		if (ret) {
5490902f836SMatti Vaittinen 			dev_err(bd->dev, "Failed to read IRQ sub-status\n");
5500902f836SMatti Vaittinen 			goto err_umask;
5510902f836SMatti Vaittinen 		}
5520902f836SMatti Vaittinen 
5530902f836SMatti Vaittinen 		ret = regmap_field_read(sub_mask_f[i], &sub_mask);
5540902f836SMatti Vaittinen 		if (ret) {
5550902f836SMatti Vaittinen 			dev_err(bd->dev, "Failed to read IRQ sub-mask\n");
5560902f836SMatti Vaittinen 			goto err_umask;
5570902f836SMatti Vaittinen 		}
5580902f836SMatti Vaittinen 
5590902f836SMatti Vaittinen 		/* Ack active sub-statuses */
5600902f836SMatti Vaittinen 		sub_status &= sub_mask;
5610902f836SMatti Vaittinen 
5620902f836SMatti Vaittinen 		ret = regmap_write(bd->rmap, sub_status_reg[i], sub_status);
5630902f836SMatti Vaittinen 		if (ret) {
5640902f836SMatti Vaittinen 			dev_err(bd->dev, "Failed to ack sub-IRQ\n");
5650902f836SMatti Vaittinen 			goto err_umask;
5660902f836SMatti Vaittinen 		}
5670902f836SMatti Vaittinen 	}
5680902f836SMatti Vaittinen 
5690902f836SMatti Vaittinen 	ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
5700902f836SMatti Vaittinen 	if (ret)
5710902f836SMatti Vaittinen 		/* May as well retry once */
5720902f836SMatti Vaittinen 		goto err_umask;
5730902f836SMatti Vaittinen 
5740902f836SMatti Vaittinen 	/* Read whole chip state */
5750902f836SMatti Vaittinen 	ret = bd9995x_get_chip_state(bd, &state);
5760902f836SMatti Vaittinen 	if (ret < 0) {
5770902f836SMatti Vaittinen 		dev_err(bd->dev, "Failed to read chip state\n");
5780902f836SMatti Vaittinen 	} else {
5790902f836SMatti Vaittinen 		mutex_lock(&bd->lock);
5800902f836SMatti Vaittinen 		bd->state = state;
5810902f836SMatti Vaittinen 		mutex_unlock(&bd->lock);
5820902f836SMatti Vaittinen 
5830902f836SMatti Vaittinen 		power_supply_changed(bd->charger);
5840902f836SMatti Vaittinen 	}
5850902f836SMatti Vaittinen 
5860902f836SMatti Vaittinen 	return IRQ_HANDLED;
5870902f836SMatti Vaittinen 
5880902f836SMatti Vaittinen err_umask:
5890902f836SMatti Vaittinen 	ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
5900902f836SMatti Vaittinen 	if (ret)
5910902f836SMatti Vaittinen 		dev_err(bd->dev,
5920902f836SMatti Vaittinen 		"Failed to un-mask F_INT0 - IRQ permanently disabled\n");
5930902f836SMatti Vaittinen 
5940902f836SMatti Vaittinen 	return IRQ_NONE;
5950902f836SMatti Vaittinen }
5960902f836SMatti Vaittinen 
__bd9995x_chip_reset(struct bd9995x_device * bd)5970902f836SMatti Vaittinen static int __bd9995x_chip_reset(struct bd9995x_device *bd)
5980902f836SMatti Vaittinen {
5990902f836SMatti Vaittinen 	int ret, state;
6000902f836SMatti Vaittinen 	int rst_check_counter = 10;
6010902f836SMatti Vaittinen 	u16 tmp = ALLRST | OTPLD;
6020902f836SMatti Vaittinen 
6030902f836SMatti Vaittinen 	ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
6040902f836SMatti Vaittinen 	if (ret < 0)
6050902f836SMatti Vaittinen 		return ret;
6060902f836SMatti Vaittinen 
6070902f836SMatti Vaittinen 	do {
6080902f836SMatti Vaittinen 		ret = regmap_field_read(bd->rmap_fields[F_OTPLD_STATE], &state);
6090902f836SMatti Vaittinen 		if (ret)
6100902f836SMatti Vaittinen 			return ret;
6110902f836SMatti Vaittinen 
6120902f836SMatti Vaittinen 		msleep(10);
6130902f836SMatti Vaittinen 	} while (state == 0 && --rst_check_counter);
6140902f836SMatti Vaittinen 
6150902f836SMatti Vaittinen 	if (!rst_check_counter) {
6160902f836SMatti Vaittinen 		dev_err(bd->dev, "chip reset not completed\n");
6170902f836SMatti Vaittinen 		return -ETIMEDOUT;
6180902f836SMatti Vaittinen 	}
6190902f836SMatti Vaittinen 
6200902f836SMatti Vaittinen 	tmp = 0;
6210902f836SMatti Vaittinen 	ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
6220902f836SMatti Vaittinen 
6230902f836SMatti Vaittinen 	return ret;
6240902f836SMatti Vaittinen }
6250902f836SMatti Vaittinen 
bd9995x_hw_init(struct bd9995x_device * bd)6260902f836SMatti Vaittinen static int bd9995x_hw_init(struct bd9995x_device *bd)
6270902f836SMatti Vaittinen {
6280902f836SMatti Vaittinen 	int ret;
6290902f836SMatti Vaittinen 	int i;
6300902f836SMatti Vaittinen 	struct bd9995x_state state;
6310902f836SMatti Vaittinen 	struct bd9995x_init_data *id = &bd->init_data;
6320902f836SMatti Vaittinen 
6330902f836SMatti Vaittinen 	const struct {
6340902f836SMatti Vaittinen 		enum bd9995x_fields id;
6350902f836SMatti Vaittinen 		u16 value;
6360902f836SMatti Vaittinen 	} init_data[] = {
6370902f836SMatti Vaittinen 		/* Enable the charging trigger after SDP charger attached */
6380902f836SMatti Vaittinen 		{F_SDP_CHG_TRIG_EN,	1},
6390902f836SMatti Vaittinen 		/* Enable charging trigger after SDP charger attached */
6400902f836SMatti Vaittinen 		{F_SDP_CHG_TRIG,	1},
6410902f836SMatti Vaittinen 		/* Disable charging trigger by BC1.2 detection */
6420902f836SMatti Vaittinen 		{F_VBUS_BC_DISEN,	1},
6430902f836SMatti Vaittinen 		/* Disable charging trigger by BC1.2 detection */
6440902f836SMatti Vaittinen 		{F_VCC_BC_DISEN,	1},
6450902f836SMatti Vaittinen 		/* Disable automatic limitation of the input current */
6460902f836SMatti Vaittinen 		{F_ILIM_AUTO_DISEN,	1},
6470902f836SMatti Vaittinen 		/* Select current limitation when SDP charger attached*/
6480902f836SMatti Vaittinen 		{F_SDP_500_SEL,		1},
6490902f836SMatti Vaittinen 		/* Select current limitation when DCP charger attached */
6500902f836SMatti Vaittinen 		{F_DCP_2500_SEL,	1},
6510902f836SMatti Vaittinen 		{F_VSYSREG_SET,		id->vsysreg_set},
6520902f836SMatti Vaittinen 		/* Activate USB charging and DC/DC converter */
6530902f836SMatti Vaittinen 		{F_USB_SUS,		0},
6540902f836SMatti Vaittinen 		/* DCDC clock: 1200 kHz*/
6550902f836SMatti Vaittinen 		{F_DCDC_CLK_SEL,	3},
6560902f836SMatti Vaittinen 		/* Enable charging */
6570902f836SMatti Vaittinen 		{F_CHG_EN,		1},
6580902f836SMatti Vaittinen 		/* Disable Input current Limit setting voltage measurement */
6590902f836SMatti Vaittinen 		{F_EXTIADPEN,		0},
6600902f836SMatti Vaittinen 		/* Disable input current limiting */
6610902f836SMatti Vaittinen 		{F_VSYS_PRIORITY,	1},
6620902f836SMatti Vaittinen 		{F_IBUS_LIM_SET,	id->ibus_lim_set},
6630902f836SMatti Vaittinen 		{F_ICC_LIM_SET,		id->icc_lim_set},
6640902f836SMatti Vaittinen 		/* Charge Termination Current Setting to 0*/
6650902f836SMatti Vaittinen 		{F_ITERM_SET,		id->iterm_set},
6660902f836SMatti Vaittinen 		/* Trickle-charge Current Setting */
6670902f836SMatti Vaittinen 		{F_ITRICH_SET,		id->itrich_set},
6680902f836SMatti Vaittinen 		/* Pre-charge Current setting */
6690902f836SMatti Vaittinen 		{F_IPRECH_SET,		id->iprech_set},
6700902f836SMatti Vaittinen 		/* Fast Charge Current for constant current phase */
6710902f836SMatti Vaittinen 		{F_ICHG_SET,		id->ichg_set},
6720902f836SMatti Vaittinen 		/* Fast Charge Voltage Regulation Setting */
6730902f836SMatti Vaittinen 		{F_VFASTCHG_REG_SET1,	id->vfastchg_reg_set1},
6740902f836SMatti Vaittinen 		/* Set Pre-charge Voltage Threshold for trickle charging. */
6750902f836SMatti Vaittinen 		{F_VPRECHG_TH_SET,	id->vprechg_th_set},
6760902f836SMatti Vaittinen 		{F_VRECHG_SET,		id->vrechg_set},
6770902f836SMatti Vaittinen 		{F_VBATOVP_SET,		id->vbatovp_set},
6780902f836SMatti Vaittinen 		/* Reverse buck boost voltage Setting */
6790902f836SMatti Vaittinen 		{F_VRBOOST_SET,		0},
6800902f836SMatti Vaittinen 		/* Disable fast-charging watchdog */
6810902f836SMatti Vaittinen 		{F_WDT_FST,		0},
6820902f836SMatti Vaittinen 		/* Disable pre-charging watchdog */
6830902f836SMatti Vaittinen 		{F_WDT_PRE,		0},
6840902f836SMatti Vaittinen 		/* Power save off */
6850902f836SMatti Vaittinen 		{F_POWER_SAVE_MODE,	0},
6860902f836SMatti Vaittinen 		{F_INT1_SET,		INT1_ALL},
6870902f836SMatti Vaittinen 		{F_INT2_SET,		INT2_ALL},
6880902f836SMatti Vaittinen 		{F_INT3_SET,		INT3_ALL},
6890902f836SMatti Vaittinen 		{F_INT4_SET,		INT4_ALL},
6900902f836SMatti Vaittinen 		{F_INT5_SET,		INT5_ALL},
6910902f836SMatti Vaittinen 		{F_INT6_SET,		INT6_ALL},
6920902f836SMatti Vaittinen 		{F_INT7_SET,		INT7_ALL},
6930902f836SMatti Vaittinen 	};
6940902f836SMatti Vaittinen 
6950902f836SMatti Vaittinen 	/*
6960902f836SMatti Vaittinen 	 * Currently we initialize charger to a known state at startup.
6970902f836SMatti Vaittinen 	 * If we want to allow for example the boot code to initialize
6980902f836SMatti Vaittinen 	 * charger we should get rid of this.
6990902f836SMatti Vaittinen 	 */
7000902f836SMatti Vaittinen 	ret = __bd9995x_chip_reset(bd);
7010902f836SMatti Vaittinen 	if (ret < 0)
7020902f836SMatti Vaittinen 		return ret;
7030902f836SMatti Vaittinen 
7040902f836SMatti Vaittinen 	/* Initialize currents/voltages and other parameters */
7050902f836SMatti Vaittinen 	for (i = 0; i < ARRAY_SIZE(init_data); i++) {
7060902f836SMatti Vaittinen 		ret = regmap_field_write(bd->rmap_fields[init_data[i].id],
7070902f836SMatti Vaittinen 					 init_data[i].value);
7080902f836SMatti Vaittinen 		if (ret) {
7090902f836SMatti Vaittinen 			dev_err(bd->dev, "failed to initialize charger (%d)\n",
7100902f836SMatti Vaittinen 				ret);
7110902f836SMatti Vaittinen 			return ret;
7120902f836SMatti Vaittinen 		}
7130902f836SMatti Vaittinen 	}
7140902f836SMatti Vaittinen 
7150902f836SMatti Vaittinen 	ret = bd9995x_get_chip_state(bd, &state);
7160902f836SMatti Vaittinen 	if (ret < 0)
7170902f836SMatti Vaittinen 		return ret;
7180902f836SMatti Vaittinen 
7190902f836SMatti Vaittinen 	mutex_lock(&bd->lock);
7200902f836SMatti Vaittinen 	bd->state = state;
7210902f836SMatti Vaittinen 	mutex_unlock(&bd->lock);
7220902f836SMatti Vaittinen 
7230902f836SMatti Vaittinen 	return 0;
7240902f836SMatti Vaittinen }
7250902f836SMatti Vaittinen 
7260902f836SMatti Vaittinen static enum power_supply_property bd9995x_power_supply_props[] = {
7270902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_MANUFACTURER,
7280902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_STATUS,
7290902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_ONLINE,
7300902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
7310902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_CHARGE_AVG,
7320902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
7330902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
7340902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
7350902f836SMatti Vaittinen 	/* Battery props we access through charger */
7360902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_PRESENT,
7370902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
7380902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_CURRENT_NOW,
7390902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_CHARGE_TYPE,
7400902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_HEALTH,
7410902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_TEMP,
7420902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_TECHNOLOGY,
7430902f836SMatti Vaittinen 	POWER_SUPPLY_PROP_MODEL_NAME,
7440902f836SMatti Vaittinen };
7450902f836SMatti Vaittinen 
7460902f836SMatti Vaittinen static const struct power_supply_desc bd9995x_power_supply_desc = {
7470902f836SMatti Vaittinen 	.name = "bd9995x-charger",
7480902f836SMatti Vaittinen 	.type = POWER_SUPPLY_TYPE_USB,
7490902f836SMatti Vaittinen 	.properties = bd9995x_power_supply_props,
7500902f836SMatti Vaittinen 	.num_properties = ARRAY_SIZE(bd9995x_power_supply_props),
7510902f836SMatti Vaittinen 	.get_property = bd9995x_power_supply_get_property,
7520902f836SMatti Vaittinen };
7530902f836SMatti Vaittinen 
7540902f836SMatti Vaittinen /*
7550902f836SMatti Vaittinen  * Limit configurations for vbus-input-current and vcc-vacp-input-current
7560902f836SMatti Vaittinen  * Minimum limit is 0 uA. Max is 511 * 32000 uA = 16352000 uA. This is
7570902f836SMatti Vaittinen  * configured by writing a register so that each increment in register
7580902f836SMatti Vaittinen  * value equals to 32000 uA limit increment.
7590902f836SMatti Vaittinen  *
7600902f836SMatti Vaittinen  * Eg, value 0x0 is limit 0, value 0x1 is limit 32000, ...
7610902f836SMatti Vaittinen  * Describe the setting in linear_range table.
7620902f836SMatti Vaittinen  */
7630902f836SMatti Vaittinen static const struct linear_range input_current_limit_ranges[] = {
7649a265e04SMatti Vaittinen 	LINEAR_RANGE(0, 0x0, 0x1ff, 32000),
7650902f836SMatti Vaittinen };
7660902f836SMatti Vaittinen 
7670902f836SMatti Vaittinen /* Possible trickle, pre-charging and termination current values */
7680902f836SMatti Vaittinen static const struct linear_range charging_current_ranges[] = {
7699a265e04SMatti Vaittinen 	LINEAR_RANGE(0, 0x0, 0x10, 64000),
7709a265e04SMatti Vaittinen 	LINEAR_RANGE(1024000, 0x11, 0x1f, 0),
7710902f836SMatti Vaittinen };
7720902f836SMatti Vaittinen 
7730902f836SMatti Vaittinen /*
7740902f836SMatti Vaittinen  * Fast charging voltage regulation, starting re-charging limit
7750902f836SMatti Vaittinen  * and battery over voltage protection have same possible values
7760902f836SMatti Vaittinen  */
7770902f836SMatti Vaittinen static const struct linear_range charge_voltage_regulation_ranges[] = {
7789a265e04SMatti Vaittinen 	LINEAR_RANGE(2560000, 0, 0xA0, 0),
7799a265e04SMatti Vaittinen 	LINEAR_RANGE(2560000, 0xA0, 0x4B0, 16000),
7809a265e04SMatti Vaittinen 	LINEAR_RANGE(19200000, 0x4B0, 0x7FF, 0),
7810902f836SMatti Vaittinen };
7820902f836SMatti Vaittinen 
7830902f836SMatti Vaittinen /* Possible VSYS voltage regulation values */
7840902f836SMatti Vaittinen static const struct linear_range vsys_voltage_regulation_ranges[] = {
7859a265e04SMatti Vaittinen 	LINEAR_RANGE(2560000, 0, 0x28, 0),
7869a265e04SMatti Vaittinen 	LINEAR_RANGE(2560000, 0x28, 0x12C, 64000),
7879a265e04SMatti Vaittinen 	LINEAR_RANGE(19200000, 0x12C, 0x1FF, 0),
7880902f836SMatti Vaittinen };
7890902f836SMatti Vaittinen 
7900902f836SMatti Vaittinen /* Possible settings for switching from trickle to pre-charging limits */
7910902f836SMatti Vaittinen static const struct linear_range trickle_to_pre_threshold_ranges[] = {
7929a265e04SMatti Vaittinen 	LINEAR_RANGE(2048000, 0, 0x20, 0),
7939a265e04SMatti Vaittinen 	LINEAR_RANGE(2048000, 0x20, 0x12C, 64000),
7949a265e04SMatti Vaittinen 	LINEAR_RANGE(19200000, 0x12C, 0x1FF, 0),
7950902f836SMatti Vaittinen };
7960902f836SMatti Vaittinen 
7970902f836SMatti Vaittinen /* Possible current values for fast-charging constant current phase */
7980902f836SMatti Vaittinen static const struct linear_range fast_charge_current_ranges[] = {
7999a265e04SMatti Vaittinen 	LINEAR_RANGE(0, 0, 0xFF, 64000),
8000902f836SMatti Vaittinen };
8010902f836SMatti Vaittinen 
8020902f836SMatti Vaittinen struct battery_init {
8030902f836SMatti Vaittinen 	const char *name;
8040902f836SMatti Vaittinen 	int *info_data;
8050902f836SMatti Vaittinen 	const struct linear_range *range;
8060902f836SMatti Vaittinen 	int ranges;
8070902f836SMatti Vaittinen 	u16 *data;
8080902f836SMatti Vaittinen };
8090902f836SMatti Vaittinen 
8100902f836SMatti Vaittinen struct dt_init {
8110902f836SMatti Vaittinen 	char *prop;
8120902f836SMatti Vaittinen 	const struct linear_range *range;
8130902f836SMatti Vaittinen 	int ranges;
8140902f836SMatti Vaittinen 	u16 *data;
8150902f836SMatti Vaittinen };
8160902f836SMatti Vaittinen 
bd9995x_fw_probe(struct bd9995x_device * bd)8170902f836SMatti Vaittinen static int bd9995x_fw_probe(struct bd9995x_device *bd)
8180902f836SMatti Vaittinen {
8190902f836SMatti Vaittinen 	int ret;
82025fd3303SLinus Walleij 	struct power_supply_battery_info *info;
8210902f836SMatti Vaittinen 	u32 property;
8220902f836SMatti Vaittinen 	int i;
8230902f836SMatti Vaittinen 	int regval;
8240902f836SMatti Vaittinen 	bool found;
8250902f836SMatti Vaittinen 	struct bd9995x_init_data *init = &bd->init_data;
8260902f836SMatti Vaittinen 	struct battery_init battery_inits[] = {
8270902f836SMatti Vaittinen 		{
8280902f836SMatti Vaittinen 			.name = "trickle-charging current",
8290902f836SMatti Vaittinen 			.range = &charging_current_ranges[0],
8300902f836SMatti Vaittinen 			.ranges = 2,
8310902f836SMatti Vaittinen 			.data = &init->itrich_set,
8320902f836SMatti Vaittinen 		}, {
8330902f836SMatti Vaittinen 			.name = "pre-charging current",
8340902f836SMatti Vaittinen 			.range = &charging_current_ranges[0],
8350902f836SMatti Vaittinen 			.ranges = 2,
8360902f836SMatti Vaittinen 			.data = &init->iprech_set,
8370902f836SMatti Vaittinen 		}, {
8380902f836SMatti Vaittinen 			.name = "pre-to-trickle charge voltage threshold",
8390902f836SMatti Vaittinen 			.range = &trickle_to_pre_threshold_ranges[0],
8400902f836SMatti Vaittinen 			.ranges = 2,
8410902f836SMatti Vaittinen 			.data = &init->vprechg_th_set,
8420902f836SMatti Vaittinen 		}, {
8430902f836SMatti Vaittinen 			.name = "charging termination current",
8440902f836SMatti Vaittinen 			.range = &charging_current_ranges[0],
8450902f836SMatti Vaittinen 			.ranges = 2,
8460902f836SMatti Vaittinen 			.data = &init->iterm_set,
8470902f836SMatti Vaittinen 		}, {
8480902f836SMatti Vaittinen 			.name = "charging re-start voltage",
8490902f836SMatti Vaittinen 			.range = &charge_voltage_regulation_ranges[0],
8500902f836SMatti Vaittinen 			.ranges = 2,
8510902f836SMatti Vaittinen 			.data = &init->vrechg_set,
8520902f836SMatti Vaittinen 		}, {
8530902f836SMatti Vaittinen 			.name = "battery overvoltage limit",
8540902f836SMatti Vaittinen 			.range = &charge_voltage_regulation_ranges[0],
8550902f836SMatti Vaittinen 			.ranges = 2,
8560902f836SMatti Vaittinen 			.data = &init->vbatovp_set,
8570902f836SMatti Vaittinen 		}, {
8580902f836SMatti Vaittinen 			.name = "fast-charging max current",
8590902f836SMatti Vaittinen 			.range = &fast_charge_current_ranges[0],
8600902f836SMatti Vaittinen 			.ranges = 1,
8610902f836SMatti Vaittinen 			.data = &init->ichg_set,
8620902f836SMatti Vaittinen 		}, {
8630902f836SMatti Vaittinen 			.name = "fast-charging voltage",
8640902f836SMatti Vaittinen 			.range = &charge_voltage_regulation_ranges[0],
8650902f836SMatti Vaittinen 			.ranges = 2,
8660902f836SMatti Vaittinen 			.data = &init->vfastchg_reg_set1,
8670902f836SMatti Vaittinen 		},
8680902f836SMatti Vaittinen 	};
8690902f836SMatti Vaittinen 	struct dt_init props[] = {
8700902f836SMatti Vaittinen 		{
8710902f836SMatti Vaittinen 			.prop = "rohm,vsys-regulation-microvolt",
8720902f836SMatti Vaittinen 			.range = &vsys_voltage_regulation_ranges[0],
8730902f836SMatti Vaittinen 			.ranges = 2,
8740902f836SMatti Vaittinen 			.data = &init->vsysreg_set,
8750902f836SMatti Vaittinen 		}, {
8760902f836SMatti Vaittinen 			.prop = "rohm,vbus-input-current-limit-microamp",
8770902f836SMatti Vaittinen 			.range = &input_current_limit_ranges[0],
8780902f836SMatti Vaittinen 			.ranges = 1,
8790902f836SMatti Vaittinen 			.data = &init->ibus_lim_set,
8800902f836SMatti Vaittinen 		}, {
8810902f836SMatti Vaittinen 			.prop = "rohm,vcc-input-current-limit-microamp",
8820902f836SMatti Vaittinen 			.range = &input_current_limit_ranges[0],
8830902f836SMatti Vaittinen 			.ranges = 1,
8840902f836SMatti Vaittinen 			.data = &init->icc_lim_set,
8850902f836SMatti Vaittinen 		},
8860902f836SMatti Vaittinen 	};
8870902f836SMatti Vaittinen 
8880902f836SMatti Vaittinen 	/*
8890902f836SMatti Vaittinen 	 * The power_supply_get_battery_info() does not support getting values
8900902f836SMatti Vaittinen 	 * from ACPI. Let's fix it if ACPI is required here.
8910902f836SMatti Vaittinen 	 */
8920902f836SMatti Vaittinen 	ret = power_supply_get_battery_info(bd->charger, &info);
8930902f836SMatti Vaittinen 	if (ret < 0)
8940902f836SMatti Vaittinen 		return ret;
8950902f836SMatti Vaittinen 
89625fd3303SLinus Walleij 	/* Put pointers to the generic battery info */
89725fd3303SLinus Walleij 	battery_inits[0].info_data = &info->tricklecharge_current_ua;
89825fd3303SLinus Walleij 	battery_inits[1].info_data = &info->precharge_current_ua;
89925fd3303SLinus Walleij 	battery_inits[2].info_data = &info->precharge_voltage_max_uv;
90025fd3303SLinus Walleij 	battery_inits[3].info_data = &info->charge_term_current_ua;
90125fd3303SLinus Walleij 	battery_inits[4].info_data = &info->charge_restart_voltage_uv;
90225fd3303SLinus Walleij 	battery_inits[5].info_data = &info->overvoltage_limit_uv;
90325fd3303SLinus Walleij 	battery_inits[6].info_data = &info->constant_charge_current_max_ua;
90425fd3303SLinus Walleij 	battery_inits[7].info_data = &info->constant_charge_voltage_max_uv;
90525fd3303SLinus Walleij 
9060902f836SMatti Vaittinen 	for (i = 0; i < ARRAY_SIZE(battery_inits); i++) {
9070902f836SMatti Vaittinen 		int val = *battery_inits[i].info_data;
9080902f836SMatti Vaittinen 		const struct linear_range *range = battery_inits[i].range;
9090902f836SMatti Vaittinen 		int ranges = battery_inits[i].ranges;
9100902f836SMatti Vaittinen 
9110902f836SMatti Vaittinen 		if (val == -EINVAL)
9120902f836SMatti Vaittinen 			continue;
9130902f836SMatti Vaittinen 
9140902f836SMatti Vaittinen 		ret = linear_range_get_selector_low_array(range, ranges, val,
9150902f836SMatti Vaittinen 							  &regval, &found);
9160902f836SMatti Vaittinen 		if (ret) {
9170902f836SMatti Vaittinen 			dev_err(bd->dev, "Unsupported value for %s\n",
9180902f836SMatti Vaittinen 				battery_inits[i].name);
9190902f836SMatti Vaittinen 
92025fd3303SLinus Walleij 			power_supply_put_battery_info(bd->charger, info);
9210902f836SMatti Vaittinen 			return -EINVAL;
9220902f836SMatti Vaittinen 		}
9230902f836SMatti Vaittinen 		if (!found) {
9240902f836SMatti Vaittinen 			dev_warn(bd->dev,
9250902f836SMatti Vaittinen 				 "Unsupported value for %s - using smaller\n",
9260902f836SMatti Vaittinen 				 battery_inits[i].name);
9270902f836SMatti Vaittinen 		}
9280902f836SMatti Vaittinen 		*(battery_inits[i].data) = regval;
9290902f836SMatti Vaittinen 	}
9300902f836SMatti Vaittinen 
93125fd3303SLinus Walleij 	power_supply_put_battery_info(bd->charger, info);
9320902f836SMatti Vaittinen 
9330902f836SMatti Vaittinen 	for (i = 0; i < ARRAY_SIZE(props); i++) {
9340902f836SMatti Vaittinen 		ret = device_property_read_u32(bd->dev, props[i].prop,
9350902f836SMatti Vaittinen 					       &property);
9360902f836SMatti Vaittinen 		if (ret < 0) {
9370902f836SMatti Vaittinen 			dev_err(bd->dev, "failed to read %s", props[i].prop);
9380902f836SMatti Vaittinen 
9390902f836SMatti Vaittinen 			return ret;
9400902f836SMatti Vaittinen 		}
9410902f836SMatti Vaittinen 
9420902f836SMatti Vaittinen 		ret = linear_range_get_selector_low_array(props[i].range,
9430902f836SMatti Vaittinen 							  props[i].ranges,
9440902f836SMatti Vaittinen 							  property, &regval,
9450902f836SMatti Vaittinen 							  &found);
9460902f836SMatti Vaittinen 		if (ret) {
9470902f836SMatti Vaittinen 			dev_err(bd->dev, "Unsupported value for '%s'\n",
9480902f836SMatti Vaittinen 				props[i].prop);
9490902f836SMatti Vaittinen 
9500902f836SMatti Vaittinen 			return -EINVAL;
9510902f836SMatti Vaittinen 		}
9520902f836SMatti Vaittinen 
9530902f836SMatti Vaittinen 		if (!found) {
9540902f836SMatti Vaittinen 			dev_warn(bd->dev,
9550902f836SMatti Vaittinen 				 "Unsupported value for '%s' - using smaller\n",
9560902f836SMatti Vaittinen 				 props[i].prop);
9570902f836SMatti Vaittinen 		}
9580902f836SMatti Vaittinen 
9590902f836SMatti Vaittinen 		*(props[i].data) = regval;
9600902f836SMatti Vaittinen 	}
9610902f836SMatti Vaittinen 
9620902f836SMatti Vaittinen 	return 0;
9630902f836SMatti Vaittinen }
9640902f836SMatti Vaittinen 
bd9995x_chip_reset(void * bd)9652e9bcaccSSamuel Zou static void bd9995x_chip_reset(void *bd)
9660902f836SMatti Vaittinen {
9670902f836SMatti Vaittinen 	__bd9995x_chip_reset(bd);
9680902f836SMatti Vaittinen }
9690902f836SMatti Vaittinen 
bd9995x_probe(struct i2c_client * client)9700902f836SMatti Vaittinen static int bd9995x_probe(struct i2c_client *client)
9710902f836SMatti Vaittinen {
9720902f836SMatti Vaittinen 	struct device *dev = &client->dev;
9730902f836SMatti Vaittinen 	struct bd9995x_device *bd;
9740902f836SMatti Vaittinen 	struct power_supply_config psy_cfg = {};
9750902f836SMatti Vaittinen 	int ret;
9760902f836SMatti Vaittinen 	int i;
9770902f836SMatti Vaittinen 
9780902f836SMatti Vaittinen 	bd = devm_kzalloc(dev, sizeof(*bd), GFP_KERNEL);
9790902f836SMatti Vaittinen 	if (!bd)
9800902f836SMatti Vaittinen 		return -ENOMEM;
9810902f836SMatti Vaittinen 
9820902f836SMatti Vaittinen 	bd->client = client;
9830902f836SMatti Vaittinen 	bd->dev = dev;
9840902f836SMatti Vaittinen 	psy_cfg.drv_data = bd;
9850902f836SMatti Vaittinen 	psy_cfg.of_node = dev->of_node;
9860902f836SMatti Vaittinen 
9870902f836SMatti Vaittinen 	mutex_init(&bd->lock);
9880902f836SMatti Vaittinen 
9890902f836SMatti Vaittinen 	bd->rmap = devm_regmap_init_i2c(client, &bd9995x_regmap_config);
9900902f836SMatti Vaittinen 	if (IS_ERR(bd->rmap)) {
9910902f836SMatti Vaittinen 		dev_err(dev, "Failed to setup register access via i2c\n");
9920902f836SMatti Vaittinen 		return PTR_ERR(bd->rmap);
9930902f836SMatti Vaittinen 	}
9940902f836SMatti Vaittinen 
9950902f836SMatti Vaittinen 	for (i = 0; i < ARRAY_SIZE(bd9995x_reg_fields); i++) {
9960902f836SMatti Vaittinen 		const struct reg_field *reg_fields = bd9995x_reg_fields;
9970902f836SMatti Vaittinen 
9980902f836SMatti Vaittinen 		bd->rmap_fields[i] = devm_regmap_field_alloc(dev, bd->rmap,
9990902f836SMatti Vaittinen 							     reg_fields[i]);
10000902f836SMatti Vaittinen 		if (IS_ERR(bd->rmap_fields[i])) {
10010902f836SMatti Vaittinen 			dev_err(dev, "cannot allocate regmap field\n");
10020902f836SMatti Vaittinen 			return PTR_ERR(bd->rmap_fields[i]);
10030902f836SMatti Vaittinen 		}
10040902f836SMatti Vaittinen 	}
10050902f836SMatti Vaittinen 
10060902f836SMatti Vaittinen 	i2c_set_clientdata(client, bd);
10070902f836SMatti Vaittinen 
10080902f836SMatti Vaittinen 	ret = regmap_field_read(bd->rmap_fields[F_CHIP_ID], &bd->chip_id);
10090902f836SMatti Vaittinen 	if (ret) {
10100902f836SMatti Vaittinen 		dev_err(dev, "Cannot read chip ID.\n");
10110902f836SMatti Vaittinen 		return ret;
10120902f836SMatti Vaittinen 	}
10130902f836SMatti Vaittinen 
10140902f836SMatti Vaittinen 	if (bd->chip_id != BD99954_ID) {
10150902f836SMatti Vaittinen 		dev_err(dev, "Chip with ID=0x%x, not supported!\n",
10160902f836SMatti Vaittinen 			bd->chip_id);
10170902f836SMatti Vaittinen 		return -ENODEV;
10180902f836SMatti Vaittinen 	}
10190902f836SMatti Vaittinen 
10200902f836SMatti Vaittinen 	ret = regmap_field_read(bd->rmap_fields[F_CHIP_REV], &bd->chip_rev);
10210902f836SMatti Vaittinen 	if (ret) {
10220902f836SMatti Vaittinen 		dev_err(dev, "Cannot read revision.\n");
10230902f836SMatti Vaittinen 		return ret;
10240902f836SMatti Vaittinen 	}
10250902f836SMatti Vaittinen 
10260902f836SMatti Vaittinen 	dev_info(bd->dev, "Found BD99954 chip rev %d\n", bd->chip_rev);
10270902f836SMatti Vaittinen 
10280902f836SMatti Vaittinen 	/*
10290902f836SMatti Vaittinen 	 * We need to init the psy before we can call
10300902f836SMatti Vaittinen 	 * power_supply_get_battery_info() for it
10310902f836SMatti Vaittinen 	 */
10320902f836SMatti Vaittinen 	bd->charger = devm_power_supply_register(bd->dev,
10330902f836SMatti Vaittinen 						 &bd9995x_power_supply_desc,
10340902f836SMatti Vaittinen 						&psy_cfg);
10350902f836SMatti Vaittinen 	if (IS_ERR(bd->charger)) {
10360902f836SMatti Vaittinen 		dev_err(dev, "Failed to register power supply\n");
10370902f836SMatti Vaittinen 		return PTR_ERR(bd->charger);
10380902f836SMatti Vaittinen 	}
10390902f836SMatti Vaittinen 
10400902f836SMatti Vaittinen 	ret = bd9995x_fw_probe(bd);
10410902f836SMatti Vaittinen 	if (ret < 0) {
10420902f836SMatti Vaittinen 		dev_err(dev, "Cannot read device properties.\n");
10430902f836SMatti Vaittinen 		return ret;
10440902f836SMatti Vaittinen 	}
10450902f836SMatti Vaittinen 
10460902f836SMatti Vaittinen 	ret = bd9995x_hw_init(bd);
10470902f836SMatti Vaittinen 	if (ret < 0) {
10480902f836SMatti Vaittinen 		dev_err(dev, "Cannot initialize the chip.\n");
10490902f836SMatti Vaittinen 		return ret;
10500902f836SMatti Vaittinen 	}
10510902f836SMatti Vaittinen 
10520902f836SMatti Vaittinen 	ret = devm_add_action_or_reset(dev, bd9995x_chip_reset, bd);
10530902f836SMatti Vaittinen 	if (ret)
10540902f836SMatti Vaittinen 		return ret;
10550902f836SMatti Vaittinen 
10560902f836SMatti Vaittinen 	return devm_request_threaded_irq(dev, client->irq, NULL,
10570902f836SMatti Vaittinen 					 bd9995x_irq_handler_thread,
10580902f836SMatti Vaittinen 					 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
10590902f836SMatti Vaittinen 					 BD9995X_IRQ_PIN, bd);
10600902f836SMatti Vaittinen }
10610902f836SMatti Vaittinen 
10620902f836SMatti Vaittinen static const struct of_device_id bd9995x_of_match[] = {
10630902f836SMatti Vaittinen 	{ .compatible = "rohm,bd99954", },
10640902f836SMatti Vaittinen 	{ }
10650902f836SMatti Vaittinen };
10660902f836SMatti Vaittinen MODULE_DEVICE_TABLE(of, bd9995x_of_match);
10670902f836SMatti Vaittinen 
10680902f836SMatti Vaittinen static struct i2c_driver bd9995x_driver = {
10690902f836SMatti Vaittinen 	.driver = {
10700902f836SMatti Vaittinen 		.name = "bd9995x-charger",
10710902f836SMatti Vaittinen 		.of_match_table = bd9995x_of_match,
10720902f836SMatti Vaittinen 	},
1073fe20b1dcSUwe Kleine-König 	.probe = bd9995x_probe,
10740902f836SMatti Vaittinen };
10750902f836SMatti Vaittinen module_i2c_driver(bd9995x_driver);
10760902f836SMatti Vaittinen 
10770902f836SMatti Vaittinen MODULE_AUTHOR("Laine Markus <markus.laine@fi.rohmeurope.com>");
10780902f836SMatti Vaittinen MODULE_DESCRIPTION("ROHM BD99954 charger driver");
10790902f836SMatti Vaittinen MODULE_LICENSE("GPL");
1080