Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
0004  * MyungJoo.Ham <myungjoo.ham@samsung.com>
0005  *
0006  * Charger Manager.
0007  * This framework enables to control and multiple chargers and to
0008  * monitor charging even in the context of suspend-to-RAM with
0009  * an interface combining the chargers.
0010  *
0011 **/
0012 
0013 #ifndef _CHARGER_MANAGER_H
0014 #define _CHARGER_MANAGER_H
0015 
0016 #include <linux/power_supply.h>
0017 #include <linux/extcon.h>
0018 #include <linux/alarmtimer.h>
0019 
0020 enum data_source {
0021     CM_BATTERY_PRESENT,
0022     CM_NO_BATTERY,
0023     CM_FUEL_GAUGE,
0024     CM_CHARGER_STAT,
0025 };
0026 
0027 enum polling_modes {
0028     CM_POLL_DISABLE = 0,
0029     CM_POLL_ALWAYS,
0030     CM_POLL_EXTERNAL_POWER_ONLY,
0031     CM_POLL_CHARGING_ONLY,
0032 };
0033 
0034 enum cm_batt_temp {
0035     CM_BATT_OK = 0,
0036     CM_BATT_OVERHEAT,
0037     CM_BATT_COLD,
0038 };
0039 
0040 /**
0041  * struct charger_cable
0042  * @extcon_name: the name of extcon device.
0043  * @name: the name of the cable connector
0044  * @extcon_dev: the extcon device.
0045  * @wq: the workqueue to control charger according to the state of
0046  *  charger cable. If charger cable is attached, enable charger.
0047  *  But if charger cable is detached, disable charger.
0048  * @nb: the notifier block to receive changed state from EXTCON
0049  *  (External Connector) when charger cable is attached/detached.
0050  * @attached: the state of charger cable.
0051  *  true: the charger cable is attached
0052  *  false: the charger cable is detached
0053  * @charger: the instance of struct charger_regulator.
0054  * @cm: the Charger Manager representing the battery.
0055  */
0056 struct charger_cable {
0057     const char *extcon_name;
0058     const char *name;
0059     struct extcon_dev *extcon_dev;
0060     u64 extcon_type;
0061 
0062     /* The charger-manager use Extcon framework */
0063     struct work_struct wq;
0064     struct notifier_block nb;
0065 
0066     /* The state of charger cable */
0067     bool attached;
0068 
0069     struct charger_regulator *charger;
0070 
0071     /*
0072      * Set min/max current of regulator to protect over-current issue
0073      * according to a kind of charger cable when cable is attached.
0074      */
0075     int min_uA;
0076     int max_uA;
0077 
0078     struct charger_manager *cm;
0079 };
0080 
0081 /**
0082  * struct charger_regulator
0083  * @regulator_name: the name of regulator for using charger.
0084  * @consumer: the regulator consumer for the charger.
0085  * @externally_control:
0086  *  Set if the charger-manager cannot control charger,
0087  *  the charger will be maintained with disabled state.
0088  * @cables:
0089  *  the array of charger cables to enable/disable charger
0090  *  and set current limit according to constraint data of
0091  *  struct charger_cable if only charger cable included
0092  *  in the array of charger cables is attached/detached.
0093  * @num_cables: the number of charger cables.
0094  * @attr_g: Attribute group for the charger(regulator)
0095  * @attr_name: "name" sysfs entry
0096  * @attr_state: "state" sysfs entry
0097  * @attr_externally_control: "externally_control" sysfs entry
0098  * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
0099  */
0100 struct charger_regulator {
0101     /* The name of regulator for charging */
0102     const char *regulator_name;
0103     struct regulator *consumer;
0104 
0105     /* charger never on when system is on */
0106     int externally_control;
0107 
0108     /*
0109      * Store constraint information related to current limit,
0110      * each cable have different condition for charging.
0111      */
0112     struct charger_cable *cables;
0113     int num_cables;
0114 
0115     struct attribute_group attr_grp;
0116     struct device_attribute attr_name;
0117     struct device_attribute attr_state;
0118     struct device_attribute attr_externally_control;
0119     struct attribute *attrs[4];
0120 
0121     struct charger_manager *cm;
0122 };
0123 
0124 /**
0125  * struct charger_desc
0126  * @psy_name: the name of power-supply-class for charger manager
0127  * @polling_mode:
0128  *  Determine which polling mode will be used
0129  * @fullbatt_vchkdrop_uV:
0130  *  Check voltage drop after the battery is fully charged.
0131  *  If it has dropped more than fullbatt_vchkdrop_uV
0132  *  CM will restart charging.
0133  * @fullbatt_uV: voltage in microvolt
0134  *  If VBATT >= fullbatt_uV, it is assumed to be full.
0135  * @fullbatt_soc: state of Charge in %
0136  *  If state of Charge >= fullbatt_soc, it is assumed to be full.
0137  * @fullbatt_full_capacity: full capacity measure
0138  *  If full capacity of battery >= fullbatt_full_capacity,
0139  *  it is assumed to be full.
0140  * @polling_interval_ms: interval in millisecond at which
0141  *  charger manager will monitor battery health
0142  * @battery_present:
0143  *  Specify where information for existence of battery can be obtained
0144  * @psy_charger_stat: the names of power-supply for chargers
0145  * @num_charger_regulator: the number of entries in charger_regulators
0146  * @charger_regulators: array of charger regulators
0147  * @psy_fuel_gauge: the name of power-supply for fuel gauge
0148  * @thermal_zone : the name of thermal zone for battery
0149  * @temp_min : Minimum battery temperature for charging.
0150  * @temp_max : Maximum battery temperature for charging.
0151  * @temp_diff : Temperature difference to restart charging.
0152  * @measure_battery_temp:
0153  *  true: measure battery temperature
0154  *  false: measure ambient temperature
0155  * @charging_max_duration_ms: Maximum possible duration for charging
0156  *  If whole charging duration exceed 'charging_max_duration_ms',
0157  *  cm stop charging.
0158  * @discharging_max_duration_ms:
0159  *  Maximum possible duration for discharging with charger cable
0160  *  after full-batt. If discharging duration exceed 'discharging
0161  *  max_duration_ms', cm start charging.
0162  */
0163 struct charger_desc {
0164     const char *psy_name;
0165 
0166     enum polling_modes polling_mode;
0167     unsigned int polling_interval_ms;
0168 
0169     unsigned int fullbatt_vchkdrop_uV;
0170     unsigned int fullbatt_uV;
0171     unsigned int fullbatt_soc;
0172     unsigned int fullbatt_full_capacity;
0173 
0174     enum data_source battery_present;
0175 
0176     const char **psy_charger_stat;
0177 
0178     int num_charger_regulators;
0179     struct charger_regulator *charger_regulators;
0180     const struct attribute_group **sysfs_groups;
0181 
0182     const char *psy_fuel_gauge;
0183 
0184     const char *thermal_zone;
0185 
0186     int temp_min;
0187     int temp_max;
0188     int temp_diff;
0189 
0190     bool measure_battery_temp;
0191 
0192     u32 charging_max_duration_ms;
0193     u32 discharging_max_duration_ms;
0194 };
0195 
0196 #define PSY_NAME_MAX    30
0197 
0198 /**
0199  * struct charger_manager
0200  * @entry: entry for list
0201  * @dev: device pointer
0202  * @desc: instance of charger_desc
0203  * @fuel_gauge: power_supply for fuel gauge
0204  * @charger_stat: array of power_supply for chargers
0205  * @tzd_batt : thermal zone device for battery
0206  * @charger_enabled: the state of charger
0207  * @emergency_stop:
0208  *  When setting true, stop charging
0209  * @psy_name_buf: the name of power-supply-class for charger manager
0210  * @charger_psy: power_supply for charger manager
0211  * @status_save_ext_pwr_inserted:
0212  *  saved status of external power before entering suspend-to-RAM
0213  * @status_save_batt:
0214  *  saved status of battery before entering suspend-to-RAM
0215  * @charging_start_time: saved start time of enabling charging
0216  * @charging_end_time: saved end time of disabling charging
0217  * @battery_status: Current battery status
0218  */
0219 struct charger_manager {
0220     struct list_head entry;
0221     struct device *dev;
0222     struct charger_desc *desc;
0223 
0224 #ifdef CONFIG_THERMAL
0225     struct thermal_zone_device *tzd_batt;
0226 #endif
0227     bool charger_enabled;
0228 
0229     int emergency_stop;
0230 
0231     char psy_name_buf[PSY_NAME_MAX + 1];
0232     struct power_supply_desc charger_psy_desc;
0233     struct power_supply *charger_psy;
0234 
0235     u64 charging_start_time;
0236     u64 charging_end_time;
0237 
0238     int battery_status;
0239 };
0240 
0241 #endif /* _CHARGER_MANAGER_H */