Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * ROHM BD99954 charger driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2020 Rohm Semiconductors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *	Originally written by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *		Mikko Mutanen <mikko.mutanen@fi.rohmeurope.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *		Markus Laine <markus.laine@fi.rohmeurope.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *	Bugs added by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *		Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *   The battery charging profile of BD99954.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *   Curve (1) represents charging current.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *   Curve (2) represents battery voltage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *   The BD99954 data sheet divides charging to three phases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *   a) Trickle-charge with constant current (8).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *   b) pre-charge with constant current (6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *   c) fast-charge, first with constant current (5) phase. After
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *      the battery voltage has reached target level (4) we have constant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  *      voltage phase until charging current has dropped to termination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *      level (7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  *    V ^                                                        ^ I
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *      .                                                        .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  *      .                                                        .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  *(4)` `.` ` ` ` ` ` ` ` ` ` ` ` ` ` ----------------------------.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  *      .                           :/                           .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  *      .                     o----+/:/ ` ` ` ` ` ` ` ` ` ` ` ` `.` ` (5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  *      .                     +   ::  +                          .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  *      .                     +  /-   --                         .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  *      .                     +`/-     +                         .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  *      .                     o/-      -:                        .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  *      .                    .s.        +`                       .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  *      .                  .--+         `/                       .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  *      .               ..``  +          .:                      .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  *      .             -`      +           --                     .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  *      .    (2)  ...``       +            :-                    .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  *      .    ...``            +             -:                   .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  *(3)` `.`.""  ` ` ` `+-------- ` ` ` ` ` ` `.:` ` ` ` ` ` ` ` ` .` ` (6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  *      .             +                       `:.                .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  *      .             +                         -:               .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  *      .             +                           -:.            .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  *      .             +                             .--.         .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  *      .   (1)       +                                `.+` ` ` `.` ` (7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  *      -..............` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` + ` ` ` .` ` (8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  *      .                                                +       -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  *      -------------------------------------------------+++++++++-->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  *      |   trickle   |  pre  |          fast            |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * Details of DT properties for different limits can be found from BD99954
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * device tree binding documentation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #include <linux/linear_range.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #include "bd99954-charger.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) struct battery_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	u16 precharge_current;	/* Trickle-charge Current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	u16 fc_reg_voltage;	/* Fast Charging Regulation Voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	u16 voltage_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	u16 voltage_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) /* Initial field values, converted to initial register values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) struct bd9995x_init_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	u16 vsysreg_set;	/* VSYS Regulation Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	u16 ibus_lim_set;	/* VBUS input current limitation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	u16 icc_lim_set;	/* VCC/VACP Input Current Limit Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	u16 itrich_set;		/* Trickle-charge Current Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	u16 iprech_set;		/* Pre-Charge Current Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	u16 ichg_set;		/* Fast-Charge constant current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	u16 vfastchg_reg_set1;	/* Fast Charging Regulation Voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	u16 vprechg_th_set;	/* Pre-charge Voltage Threshold Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	u16 vrechg_set;		/* Re-charge Battery Voltage Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	u16 vbatovp_set;	/* Battery Over Voltage Threshold Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	u16 iterm_set;		/* Charging termination current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) struct bd9995x_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	u8 online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	u16 chgstm_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	u16 vbat_vsys_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	u16 vbus_vcc_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) struct bd9995x_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct power_supply *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	struct regmap *rmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	struct regmap_field *rmap_fields[F_MAX_FIELDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	int chip_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	int chip_rev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	struct bd9995x_init_data init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	struct bd9995x_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	struct mutex lock; /* Protect state data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) static const struct regmap_range bd9995x_readonly_reg_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	regmap_reg_range(CHGSTM_STATUS, SEL_ILIM_VAL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	regmap_reg_range(IOUT_DACIN_VAL, IOUT_DACIN_VAL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	regmap_reg_range(CHIP_ID, CHIP_REV),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	regmap_reg_range(SYSTEM_STATUS, SYSTEM_STATUS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	regmap_reg_range(IBATP_VAL, VBAT_AVE_VAL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	regmap_reg_range(VTH_VAL, EXTIADP_AVE_VAL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) static const struct regmap_access_table bd9995x_writeable_regs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	.no_ranges = bd9995x_readonly_reg_ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	.n_no_ranges = ARRAY_SIZE(bd9995x_readonly_reg_ranges),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) static const struct regmap_range bd9995x_volatile_reg_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	regmap_reg_range(CHGSTM_STATUS, WDT_STATUS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	regmap_reg_range(INT0_STATUS, INT7_STATUS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	regmap_reg_range(SYSTEM_STATUS, SYSTEM_CTRL_SET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	regmap_reg_range(IBATP_VAL, EXTIADP_AVE_VAL), /* Measurement regs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) static const struct regmap_access_table bd9995x_volatile_regs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	.yes_ranges = bd9995x_volatile_reg_ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	.n_yes_ranges = ARRAY_SIZE(bd9995x_volatile_reg_ranges),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) static const struct regmap_range_cfg regmap_range_cfg[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	.selector_reg     = MAP_SET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	.selector_mask    = 0xFFFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	.selector_shift   = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	.window_start     = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	.window_len       = 0x100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	.range_min        = 0 * 0x100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	.range_max        = 3 * 0x100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) static const struct regmap_config bd9995x_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	.val_bits = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	.reg_stride = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	.max_register = 3 * 0x100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	.cache_type = REGCACHE_RBTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	.ranges = regmap_range_cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	.num_ranges = ARRAY_SIZE(regmap_range_cfg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	.wr_table = &bd9995x_writeable_regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	.volatile_table = &bd9995x_volatile_regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) enum bd9995x_chrg_fault {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	CHRG_FAULT_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	CHRG_FAULT_INPUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	CHRG_FAULT_THERMAL_SHUTDOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	CHRG_FAULT_TIMER_EXPIRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) static int bd9995x_get_prop_batt_health(struct bd9995x_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	int ret, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		return POWER_SUPPLY_HEALTH_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	/* TODO: Check these against datasheet page 34 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	switch (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	case ROOM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		return POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	case HOT1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	case HOT2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	case HOT3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		return POWER_SUPPLY_HEALTH_OVERHEAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	case COLD1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	case COLD2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		return POWER_SUPPLY_HEALTH_COLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	case TEMP_DIS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	case BATT_OPEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		return POWER_SUPPLY_HEALTH_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) static int bd9995x_get_prop_charge_type(struct bd9995x_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	int ret, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	ret = regmap_field_read(bd->rmap_fields[F_CHGSTM_STATE], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	switch (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	case CHGSTM_TRICKLE_CHARGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	case CHGSTM_PRE_CHARGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	case CHGSTM_FAST_CHARGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		return POWER_SUPPLY_CHARGE_TYPE_FAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	case CHGSTM_TOP_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	case CHGSTM_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	case CHGSTM_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		return POWER_SUPPLY_CHARGE_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	default: /* Rest of the states are error related, no charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		return POWER_SUPPLY_CHARGE_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) static bool bd9995x_get_prop_batt_present(struct bd9995x_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	int ret, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	return tmp != BATT_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) static int bd9995x_get_prop_batt_voltage(struct bd9995x_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	int ret, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	ret = regmap_field_read(bd->rmap_fields[F_VBAT_VAL], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	tmp = min(tmp, 19200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	return tmp * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) static int bd9995x_get_prop_batt_current(struct bd9995x_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	int ret, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	return tmp * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) #define DEFAULT_BATTERY_TEMPERATURE 250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) static int bd9995x_get_prop_batt_temp(struct bd9995x_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	int ret, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	ret = regmap_field_read(bd->rmap_fields[F_THERM_VAL], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		return DEFAULT_BATTERY_TEMPERATURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	return (200 - tmp) * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) static int bd9995x_power_supply_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 					     enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 					     union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	int ret, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	struct bd9995x_device *bd = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	struct bd9995x_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	mutex_lock(&bd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	state = bd->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	mutex_unlock(&bd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		switch (state.chgstm_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		case CHGSTM_TRICKLE_CHARGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		case CHGSTM_PRE_CHARGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		case CHGSTM_FAST_CHARGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		case CHGSTM_TOP_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		case CHGSTM_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 			val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		case CHGSTM_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		case CHGSTM_TEMPERATURE_ERROR_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		case CHGSTM_TEMPERATURE_ERROR_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		case CHGSTM_TEMPERATURE_ERROR_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		case CHGSTM_TEMPERATURE_ERROR_4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		case CHGSTM_TEMPERATURE_ERROR_5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		case CHGSTM_TEMPERATURE_ERROR_6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		case CHGSTM_TEMPERATURE_ERROR_7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		case CHGSTM_THERMAL_SHUT_DOWN_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		case CHGSTM_THERMAL_SHUT_DOWN_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		case CHGSTM_THERMAL_SHUT_DOWN_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		case CHGSTM_THERMAL_SHUT_DOWN_4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		case CHGSTM_THERMAL_SHUT_DOWN_5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		case CHGSTM_THERMAL_SHUT_DOWN_6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		case CHGSTM_THERMAL_SHUT_DOWN_7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		case CHGSTM_BATTERY_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	case POWER_SUPPLY_PROP_MANUFACTURER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		val->strval = BD9995X_MANUFACTURER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		val->intval = state.online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		val->intval = tmp * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	case POWER_SUPPLY_PROP_CHARGE_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		ret = regmap_field_read(bd->rmap_fields[F_IBATP_AVE_VAL], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		val->intval = tmp * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		 * Currently the DT uses this property to give the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		 * target current for fast-charging constant current phase.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		 * I think it is correct in a sense.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		 * Yet, this prop we read and return here is the programmed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		 * safety limit for combined input currents. This feels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		 * also correct in a sense.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		 * However, this results a mismatch to DT value and value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		 * read from sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		ret = regmap_field_read(bd->rmap_fields[F_SEL_ILIM_VAL], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		val->intval = tmp * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		if (!state.online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 			val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		ret = regmap_field_read(bd->rmap_fields[F_VFASTCHG_REG_SET1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 					&tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		 * The actual range : 2560 to 19200 mV. No matter what the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		 * register says
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		val->intval = clamp_val(tmp << 4, 2560, 19200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		val->intval *= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		ret = regmap_field_read(bd->rmap_fields[F_ITERM_SET], &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		/* Start step is 64 mA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		val->intval = tmp << 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		/* Maximum is 1024 mA - no matter what register says */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		val->intval = min(val->intval, 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		val->intval *= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	/* Battery properties which we access through charger */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		val->intval = bd9995x_get_prop_batt_present(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		val->intval = bd9995x_get_prop_batt_voltage(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		val->intval = bd9995x_get_prop_batt_current(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		val->intval = bd9995x_get_prop_charge_type(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		val->intval = bd9995x_get_prop_batt_health(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		val->intval = bd9995x_get_prop_batt_temp(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	case POWER_SUPPLY_PROP_TECHNOLOGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	case POWER_SUPPLY_PROP_MODEL_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		val->strval = "bd99954";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) static int bd9995x_get_chip_state(struct bd9995x_device *bd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 				  struct bd9995x_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	int i, ret, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		struct regmap_field *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		u16 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	} state_fields[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 			bd->rmap_fields[F_CHGSTM_STATE], &state->chgstm_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			bd->rmap_fields[F_VBAT_VSYS_STATUS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 			&state->vbat_vsys_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 			bd->rmap_fields[F_VBUS_VCC_STATUS],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 			&state->vbus_vcc_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		ret = regmap_field_read(state_fields[i].id, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		*state_fields[i].data = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	if (state->vbus_vcc_status & STATUS_VCC_DET ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	    state->vbus_vcc_status & STATUS_VBUS_DET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		state->online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		state->online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) static irqreturn_t bd9995x_irq_handler_thread(int irq, void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	struct bd9995x_device *bd = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	int ret, status, mask, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	unsigned long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	struct bd9995x_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	 * The bd9995x does not seem to generate big amount of interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	 * The logic regarding which interrupts can cause relevant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	 * status changes seem to be pretty complex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	 * So lets implement really simple and hopefully bullet-proof handler:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	 * It does not really matter which IRQ we handle, we just go and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	 * re-read all interesting statuses + give the framework a nudge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	 * Other option would be building a _complex_ and error prone logic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	 * trying to decide what could have been changed (resulting this IRQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	 * we are now handling). During the normal operation the BD99954 does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	 * not seem to be generating much of interrupts so benefit from such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	 * logic would probably be minimal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	ret = regmap_read(bd->rmap, INT0_STATUS, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		dev_err(bd->dev, "Failed to read IRQ status\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	ret = regmap_field_read(bd->rmap_fields[F_INT0_SET], &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		dev_err(bd->dev, "Failed to read IRQ mask\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	/* Handle only IRQs that are not masked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	status &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	tmp = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	/* Lowest bit does not represent any sub-registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	tmp >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	 * Mask and ack IRQs we will handle (+ the idiot bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		dev_err(bd->dev, "Failed to mask F_INT0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	ret = regmap_write(bd->rmap, INT0_STATUS, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		dev_err(bd->dev, "Failed to ack F_INT0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		goto err_umask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	for_each_set_bit(i, &tmp, 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		int sub_status, sub_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		int sub_status_reg[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 			INT1_STATUS, INT2_STATUS, INT3_STATUS, INT4_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			INT5_STATUS, INT6_STATUS, INT7_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		struct regmap_field *sub_mask_f[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 			bd->rmap_fields[F_INT1_SET],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 			bd->rmap_fields[F_INT2_SET],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 			bd->rmap_fields[F_INT3_SET],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			bd->rmap_fields[F_INT4_SET],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			bd->rmap_fields[F_INT5_SET],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			bd->rmap_fields[F_INT6_SET],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 			bd->rmap_fields[F_INT7_SET],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		/* Clear sub IRQs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		ret = regmap_read(bd->rmap, sub_status_reg[i], &sub_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 			dev_err(bd->dev, "Failed to read IRQ sub-status\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 			goto err_umask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		ret = regmap_field_read(sub_mask_f[i], &sub_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 			dev_err(bd->dev, "Failed to read IRQ sub-mask\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			goto err_umask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		/* Ack active sub-statuses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		sub_status &= sub_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		ret = regmap_write(bd->rmap, sub_status_reg[i], sub_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			dev_err(bd->dev, "Failed to ack sub-IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			goto err_umask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		/* May as well retry once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		goto err_umask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	/* Read whole chip state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	ret = bd9995x_get_chip_state(bd, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		dev_err(bd->dev, "Failed to read chip state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		mutex_lock(&bd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		bd->state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		mutex_unlock(&bd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		power_supply_changed(bd->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) err_umask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		dev_err(bd->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		"Failed to un-mask F_INT0 - IRQ permanently disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) static int __bd9995x_chip_reset(struct bd9995x_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	int ret, state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	int rst_check_counter = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	u16 tmp = ALLRST | OTPLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		ret = regmap_field_read(bd->rmap_fields[F_OTPLD_STATE], &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		msleep(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	} while (state == 0 && --rst_check_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (!rst_check_counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		dev_err(bd->dev, "chip reset not completed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	tmp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) static int bd9995x_hw_init(struct bd9995x_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	struct bd9995x_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	struct bd9995x_init_data *id = &bd->init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		enum bd9995x_fields id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		u16 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	} init_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		/* Enable the charging trigger after SDP charger attached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		{F_SDP_CHG_TRIG_EN,	1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		/* Enable charging trigger after SDP charger attached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		{F_SDP_CHG_TRIG,	1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		/* Disable charging trigger by BC1.2 detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		{F_VBUS_BC_DISEN,	1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		/* Disable charging trigger by BC1.2 detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		{F_VCC_BC_DISEN,	1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		/* Disable automatic limitation of the input current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		{F_ILIM_AUTO_DISEN,	1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		/* Select current limitation when SDP charger attached*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		{F_SDP_500_SEL,		1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		/* Select current limitation when DCP charger attached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		{F_DCP_2500_SEL,	1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		{F_VSYSREG_SET,		id->vsysreg_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		/* Activate USB charging and DC/DC converter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		{F_USB_SUS,		0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		/* DCDC clock: 1200 kHz*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		{F_DCDC_CLK_SEL,	3},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		/* Enable charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		{F_CHG_EN,		1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		/* Disable Input current Limit setting voltage measurement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		{F_EXTIADPEN,		0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		/* Disable input current limiting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		{F_VSYS_PRIORITY,	1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		{F_IBUS_LIM_SET,	id->ibus_lim_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		{F_ICC_LIM_SET,		id->icc_lim_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		/* Charge Termination Current Setting to 0*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		{F_ITERM_SET,		id->iterm_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		/* Trickle-charge Current Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		{F_ITRICH_SET,		id->itrich_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		/* Pre-charge Current setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		{F_IPRECH_SET,		id->iprech_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		/* Fast Charge Current for constant current phase */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		{F_ICHG_SET,		id->ichg_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		/* Fast Charge Voltage Regulation Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		{F_VFASTCHG_REG_SET1,	id->vfastchg_reg_set1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		/* Set Pre-charge Voltage Threshold for trickle charging. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		{F_VPRECHG_TH_SET,	id->vprechg_th_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		{F_VRECHG_SET,		id->vrechg_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		{F_VBATOVP_SET,		id->vbatovp_set},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		/* Reverse buck boost voltage Setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		{F_VRBOOST_SET,		0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		/* Disable fast-charging watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		{F_WDT_FST,		0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		/* Disable pre-charging watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		{F_WDT_PRE,		0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		/* Power save off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		{F_POWER_SAVE_MODE,	0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		{F_INT1_SET,		INT1_ALL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		{F_INT2_SET,		INT2_ALL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		{F_INT3_SET,		INT3_ALL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		{F_INT4_SET,		INT4_ALL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		{F_INT5_SET,		INT5_ALL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		{F_INT6_SET,		INT6_ALL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		{F_INT7_SET,		INT7_ALL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	 * Currently we initialize charger to a known state at startup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	 * If we want to allow for example the boot code to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	 * charger we should get rid of this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	ret = __bd9995x_chip_reset(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	/* Initialize currents/voltages and other parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	for (i = 0; i < ARRAY_SIZE(init_data); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		ret = regmap_field_write(bd->rmap_fields[init_data[i].id],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 					 init_data[i].value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			dev_err(bd->dev, "failed to initialize charger (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	ret = bd9995x_get_chip_state(bd, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	mutex_lock(&bd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	bd->state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	mutex_unlock(&bd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) static enum power_supply_property bd9995x_power_supply_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	POWER_SUPPLY_PROP_MANUFACTURER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	POWER_SUPPLY_PROP_CHARGE_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	/* Battery props we access through charger */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	POWER_SUPPLY_PROP_CHARGE_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	POWER_SUPPLY_PROP_TECHNOLOGY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	POWER_SUPPLY_PROP_MODEL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) static const struct power_supply_desc bd9995x_power_supply_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	.name = "bd9995x-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	.type = POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	.properties = bd9995x_power_supply_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	.num_properties = ARRAY_SIZE(bd9995x_power_supply_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	.get_property = bd9995x_power_supply_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  * Limit configurations for vbus-input-current and vcc-vacp-input-current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763)  * Minimum limit is 0 uA. Max is 511 * 32000 uA = 16352000 uA. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764)  * configured by writing a register so that each increment in register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765)  * value equals to 32000 uA limit increment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767)  * Eg, value 0x0 is limit 0, value 0x1 is limit 32000, ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768)  * Describe the setting in linear_range table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) static const struct linear_range input_current_limit_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		.min = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		.step = 32000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		.min_sel = 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		.max_sel = 0x1ff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) /* Possible trickle, pre-charging and termination current values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) static const struct linear_range charging_current_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		.min = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		.step = 64000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		.min_sel = 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		.max_sel = 0x10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		.min = 1024000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		.step = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		.min_sel = 0x11,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		.max_sel = 0x1f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795)  * Fast charging voltage regulation, starting re-charging limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796)  * and battery over voltage protection have same possible values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) static const struct linear_range charge_voltage_regulation_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		.min = 2560000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		.step = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		.min_sel = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		.max_sel = 0xA0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		.min = 2560000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		.step = 16000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		.min_sel = 0xA0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		.max_sel = 0x4B0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		.min = 19200000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		.step = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		.min_sel = 0x4B0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		.max_sel = 0x7FF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) /* Possible VSYS voltage regulation values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) static const struct linear_range vsys_voltage_regulation_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		.min = 2560000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		.step = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		.min_sel = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		.max_sel = 0x28,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		.min = 2560000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		.step = 64000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		.min_sel = 0x28,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		.max_sel = 0x12C,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		.min = 19200000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		.step = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		.min_sel = 0x12C,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		.max_sel = 0x1FF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) /* Possible settings for switching from trickle to pre-charging limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) static const struct linear_range trickle_to_pre_threshold_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		.min = 2048000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		.step = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		.min_sel = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		.max_sel = 0x20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		.min = 2048000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		.step = 64000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		.min_sel = 0x20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		.max_sel = 0x12C,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		.min = 19200000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		.step = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		.min_sel = 0x12C,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		.max_sel = 0x1FF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) /* Possible current values for fast-charging constant current phase */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) static const struct linear_range fast_charge_current_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		.min = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		.step = 64000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		.min_sel = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		.max_sel = 0xFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) struct battery_init {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	int *info_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	const struct linear_range *range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	int ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	u16 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) struct dt_init {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	char *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	const struct linear_range *range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	int ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	u16 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) static int bd9995x_fw_probe(struct bd9995x_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	struct power_supply_battery_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	u32 property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	int regval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	struct bd9995x_init_data *init = &bd->init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	struct battery_init battery_inits[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			.name = "trickle-charging current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			.info_data = &info.tricklecharge_current_ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			.range = &charging_current_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			.ranges = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 			.data = &init->itrich_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			.name = "pre-charging current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			.info_data = &info.precharge_current_ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 			.range = &charging_current_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 			.ranges = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 			.data = &init->iprech_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			.name = "pre-to-trickle charge voltage threshold",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			.info_data = &info.precharge_voltage_max_uv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			.range = &trickle_to_pre_threshold_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 			.ranges = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			.data = &init->vprechg_th_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			.name = "charging termination current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 			.info_data = &info.charge_term_current_ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			.range = &charging_current_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 			.ranges = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 			.data = &init->iterm_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 			.name = "charging re-start voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			.info_data = &info.charge_restart_voltage_uv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			.range = &charge_voltage_regulation_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			.ranges = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 			.data = &init->vrechg_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			.name = "battery overvoltage limit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 			.info_data = &info.overvoltage_limit_uv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			.range = &charge_voltage_regulation_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			.ranges = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 			.data = &init->vbatovp_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			.name = "fast-charging max current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			.info_data = &info.constant_charge_current_max_ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			.range = &fast_charge_current_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			.ranges = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			.data = &init->ichg_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			.name = "fast-charging voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			.info_data = &info.constant_charge_voltage_max_uv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 			.range = &charge_voltage_regulation_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			.ranges = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			.data = &init->vfastchg_reg_set1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	struct dt_init props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			.prop = "rohm,vsys-regulation-microvolt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 			.range = &vsys_voltage_regulation_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			.ranges = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 			.data = &init->vsysreg_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 			.prop = "rohm,vbus-input-current-limit-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			.range = &input_current_limit_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			.ranges = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			.data = &init->ibus_lim_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			.prop = "rohm,vcc-input-current-limit-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			.range = &input_current_limit_ranges[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			.ranges = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			.data = &init->icc_lim_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	 * The power_supply_get_battery_info() does not support getting values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	 * from ACPI. Let's fix it if ACPI is required here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	ret = power_supply_get_battery_info(bd->charger, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	for (i = 0; i < ARRAY_SIZE(battery_inits); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		int val = *battery_inits[i].info_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		const struct linear_range *range = battery_inits[i].range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		int ranges = battery_inits[i].ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		if (val == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		ret = linear_range_get_selector_low_array(range, ranges, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 							  &regval, &found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			dev_err(bd->dev, "Unsupported value for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 				battery_inits[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			power_supply_put_battery_info(bd->charger, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			dev_warn(bd->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 				 "Unsupported value for %s - using smaller\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 				 battery_inits[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		*(battery_inits[i].data) = regval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	power_supply_put_battery_info(bd->charger, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	for (i = 0; i < ARRAY_SIZE(props); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		ret = device_property_read_u32(bd->dev, props[i].prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 					       &property);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			dev_err(bd->dev, "failed to read %s", props[i].prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		ret = linear_range_get_selector_low_array(props[i].range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 							  props[i].ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 							  property, &regval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 							  &found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 			dev_err(bd->dev, "Unsupported value for '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 				props[i].prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 			dev_warn(bd->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 				 "Unsupported value for '%s' - using smaller\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 				 props[i].prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		*(props[i].data) = regval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) static void bd9995x_chip_reset(void *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	__bd9995x_chip_reset(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) static int bd9995x_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	struct bd9995x_device *bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	bd = devm_kzalloc(dev, sizeof(*bd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	if (!bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	bd->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	bd->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	psy_cfg.drv_data = bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	psy_cfg.of_node = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	mutex_init(&bd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	bd->rmap = devm_regmap_init_i2c(client, &bd9995x_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	if (IS_ERR(bd->rmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		dev_err(dev, "Failed to setup register access via i2c\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		return PTR_ERR(bd->rmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	for (i = 0; i < ARRAY_SIZE(bd9995x_reg_fields); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		const struct reg_field *reg_fields = bd9995x_reg_fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		bd->rmap_fields[i] = devm_regmap_field_alloc(dev, bd->rmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 							     reg_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		if (IS_ERR(bd->rmap_fields[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			dev_err(dev, "cannot allocate regmap field\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			return PTR_ERR(bd->rmap_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	i2c_set_clientdata(client, bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	ret = regmap_field_read(bd->rmap_fields[F_CHIP_ID], &bd->chip_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		dev_err(dev, "Cannot read chip ID.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	if (bd->chip_id != BD99954_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		dev_err(dev, "Chip with ID=0x%x, not supported!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			bd->chip_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	ret = regmap_field_read(bd->rmap_fields[F_CHIP_REV], &bd->chip_rev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		dev_err(dev, "Cannot read revision.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	dev_info(bd->dev, "Found BD99954 chip rev %d\n", bd->chip_rev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	 * We need to init the psy before we can call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	 * power_supply_get_battery_info() for it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	bd->charger = devm_power_supply_register(bd->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 						 &bd9995x_power_supply_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 						&psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	if (IS_ERR(bd->charger)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		dev_err(dev, "Failed to register power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		return PTR_ERR(bd->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	ret = bd9995x_fw_probe(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		dev_err(dev, "Cannot read device properties.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	ret = bd9995x_hw_init(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		dev_err(dev, "Cannot initialize the chip.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	ret = devm_add_action_or_reset(dev, bd9995x_chip_reset, bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	return devm_request_threaded_irq(dev, client->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 					 bd9995x_irq_handler_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 					 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 					 BD9995X_IRQ_PIN, bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) static const struct of_device_id bd9995x_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	{ .compatible = "rohm,bd99954", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) MODULE_DEVICE_TABLE(of, bd9995x_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) static struct i2c_driver bd9995x_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		.name = "bd9995x-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		.of_match_table = bd9995x_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	.probe_new = bd9995x_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) module_i2c_driver(bd9995x_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) MODULE_AUTHOR("Laine Markus <markus.laine@fi.rohmeurope.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) MODULE_DESCRIPTION("ROHM BD99954 charger driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) MODULE_LICENSE("GPL");