blob: 9461c76df114dcb7101d5d6177728d32f6b8b119 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#ifndef __PM8XXX_BMS_H
14#define __PM8XXX_BMS_H
15
16#include <linux/errno.h>
Xiaozhe Shi5ee95192012-09-12 15:16:34 -070017#include <linux/mfd/pm8xxx/batterydata-lib.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070018
19#define PM8921_BMS_DEV_NAME "pm8921-bms"
20
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -070022struct pm8xxx_bms_core_data {
23 unsigned int batt_temp_channel;
24 unsigned int vbat_channel;
Abhijeet Dharmapurikar76d4db92011-07-19 11:54:52 -070025 unsigned int ref625mv_channel;
26 unsigned int ref1p25v_channel;
Abhijeet Dharmapurikare922bbb2011-09-01 13:05:06 -070027 unsigned int batt_id_channel;
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -070028};
29
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030/**
31 * struct pm8921_bms_platform_data -
David Keitel35e11872012-02-17 17:40:42 -080032 * @batt_type: allows to force chose battery calibration data
Xiaozhe Shid69c91e2012-11-06 10:00:38 -080033 * @r_sense_uohm: sense resistor value in (micro Ohms)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034 * @i_test: current at which the unusable charger cutoff is to be
35 * calculated or the peak system current (mA)
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -070036 * @v_cutoff: the loaded voltage at which the battery
37 * is considered empty(mV)
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -070038 * @enable_fcc_learning: if set the driver will learn full charge
39 * capacity of the battery upon end of charge
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -070040 * @normal_voltage_calc_ms: The period of soc calculation in ms when battery
41 * voltage higher than cutoff voltage
42 * @low_voltage_calc_ms: The period of soc calculation in ms when battery
43 * voltage is near cutoff voltage
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -080044 * @disable_flat_portion_ocv: feature to disable ocv updates while in sleep
45 * @ocv_dis_high_soc: the high soc percent when ocv should be disabled
46 * @ocv_dis_low_soc: the low soc percent when ocv should be enabled
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +053047 * @low_voltage_detect: feature to enable 0 SOC reporting on low volatge
48 * @vbatt_cutoff_retries: number of tries before we report a 0 SOC
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049 */
50struct pm8921_bms_platform_data {
Abhijeet Dharmapurikarc448d982011-06-28 20:48:36 -070051 struct pm8xxx_bms_core_data bms_cdata;
David Keitel35e11872012-02-17 17:40:42 -080052 enum battery_type battery_type;
Xiaozhe Shid69c91e2012-11-06 10:00:38 -080053 int r_sense_uohm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054 unsigned int i_test;
Abhijeet Dharmapurikara51c87b2012-07-13 20:13:14 -070055 unsigned int v_cutoff;
Abhijeet Dharmapurikara93ede82011-11-17 12:20:03 -080056 unsigned int max_voltage_uv;
Abhijeet Dharmapurikarbaffba42012-03-22 14:41:10 -070057 unsigned int rconn_mohm;
Anirudh Ghayal6fe4ffa2013-02-01 11:44:33 +053058 unsigned int alarm_low_mv;
59 unsigned int alarm_high_mv;
Abhijeet Dharmapurikare88f2462012-04-25 18:22:38 -070060 int enable_fcc_learning;
Abhijeet Dharmapurikar602dc272012-07-19 21:34:56 -070061 int shutdown_soc_valid_limit;
Abhijeet Dharmapurikar1f4cc512012-07-21 00:10:39 -070062 int ignore_shutdown_soc;
Abhijeet Dharmapurikar720b00e2012-08-01 21:33:09 -070063 int adjust_soc_low_threshold;
Abhijeet Dharmapurikard6d66c72012-08-06 14:02:51 -070064 int chg_term_ua;
Abhijeet Dharmapurikar683472d2012-10-30 16:21:58 -070065 int normal_voltage_calc_ms;
66 int low_voltage_calc_ms;
Abhijeet Dharmapurikar1939ede2013-01-10 15:59:33 -080067 int disable_flat_portion_ocv;
68 int ocv_dis_high_soc;
69 int ocv_dis_low_soc;
Anirudh Ghayal0c7c2bc2013-03-22 12:57:50 +053070 int low_voltage_detect;
71 int vbatt_cutoff_retries;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072};
73
74#if defined(CONFIG_PM8921_BMS) || defined(CONFIG_PM8921_BMS_MODULE)
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -070075/**
76 * pm8921_bms_get_vsense_avg - return the voltage across the sense
77 * resitor in microvolts
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -070078 * @result: The pointer where the voltage will be updated. A -ve
79 * result means that the current is flowing in
80 * the battery - during battery charging
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -070081 *
82 * RETURNS: Error code if there was a problem reading vsense, Zero otherwise
83 * The result won't be updated in case of an error.
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -070084 *
85 *
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -070086 */
87int pm8921_bms_get_vsense_avg(int *result);
Abhijeet Dharmapurikar52344ac2011-07-05 18:44:54 -070088
89/**
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -070090 * pm8921_bms_get_battery_current - return the battery current based on vsense
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -080091 * resitor in microamperes
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -070092 * @result: The pointer where the voltage will be updated. A -ve
93 * result means that the current is flowing in
94 * the battery - during battery charging
95 *
96 * RETURNS: Error code if there was a problem reading vsense, Zero otherwise
97 * The result won't be updated in case of an error.
98 *
99 */
100int pm8921_bms_get_battery_current(int *result);
101
102/**
Abhijeet Dharmapurikar52344ac2011-07-05 18:44:54 -0700103 * pm8921_bms_get_percent_charge - returns the current battery charge in percent
104 *
105 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106int pm8921_bms_get_percent_charge(void);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -0700107
108/**
109 * pm8921_bms_get_fcc - returns fcc in mAh of the battery depending on its age
110 * and temperature
111 *
112 */
113int pm8921_bms_get_fcc(void);
114
Abhijeet Dharmapurikar52344ac2011-07-05 18:44:54 -0700115/**
116 * pm8921_bms_charging_began - function to notify the bms driver that charging
117 * has started. Used by the bms driver to keep
118 * track of chargecycles
119 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120void pm8921_bms_charging_began(void);
Abhijeet Dharmapurikar52344ac2011-07-05 18:44:54 -0700121/**
122 * pm8921_bms_charging_end - function to notify the bms driver that charging
123 * has stopped. Used by the bms driver to keep
124 * track of chargecycles
125 */
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700126void pm8921_bms_charging_end(int is_battery_full);
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -0800127
128void pm8921_bms_calibrate_hkadc(void);
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800129/**
130 * pm8921_bms_get_simultaneous_battery_voltage_and_current
131 * - function to take simultaneous vbat and vsense readings
132 * this puts the bms in override mode but keeps coulumb couting
133 * on. Useful when ir compensation needs to be implemented
134 */
135int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua,
136 int *vbat_uv);
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800137/**
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -0800138 * pm8921_bms_get_current_max
139 * - function to get the max current that can be drawn from
140 * the battery before it dips below the min allowed voltage
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800141 */
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -0800142int pm8921_bms_get_current_max(void);
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -0700143/**
144 * pm8921_bms_invalidate_shutdown_soc - function to notify the bms driver that
145 * the battery was replaced between reboot
146 * and so it should not use the shutdown
147 * soc stored in a coincell backed register
148 */
149void pm8921_bms_invalidate_shutdown_soc(void);
Abhijeet Dharmapurikar4c78f462012-10-16 15:43:55 -0700150
151/**
152 * pm8921_bms_cc_uah - function to get the coulomb counter based charge. Note
153 * that the coulomb counter are reset when the current
154 * consumption is low (below 8mA for more than 5 minutes),
155 * This will lead in a very low coulomb counter charge
156 * value upon wakeup from sleep.
157 */
158int pm8921_bms_cc_uah(int *cc_uah);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159#else
Abhijeet Dharmapurikar2fb6f082011-06-28 17:10:27 -0700160static inline int pm8921_bms_get_vsense_avg(int *result)
161{
162 return -ENXIO;
163}
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700164static inline int pm8921_bms_get_battery_current(int *result)
165{
166 return -ENXIO;
167}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168static inline int pm8921_bms_get_percent_charge(void)
169{
170 return -ENXIO;
171}
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -0700172static inline int pm8921_bms_get_fcc(void)
173{
174 return -ENXIO;
175}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176static inline void pm8921_bms_charging_began(void)
177{
178}
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700179static inline void pm8921_bms_charging_end(int is_battery_full)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180{
181}
Abhijeet Dharmapurikar1b8e8292012-01-17 11:01:44 -0800182static inline void pm8921_bms_calibrate_hkadc(void)
183{
184}
Abhijeet Dharmapurikarc9857ca2012-02-07 15:56:06 -0800185static inline int pm8921_bms_get_simultaneous_battery_voltage_and_current(
186 int *ibat_ua, int *vbat_uv)
187{
188 return -ENXIO;
189}
Abhijeet Dharmapurikarf6fdcaa02012-02-27 00:10:10 -0800190static inline int pm8921_bms_get_rbatt(void)
191{
192 return -EINVAL;
193}
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -0700194static inline void pm8921_bms_invalidate_shutdown_soc(void)
195{
196}
Abhijeet Dharmapurikar4c78f462012-10-16 15:43:55 -0700197static inline int pm8921_bms_cc_uah(int *cc_uah)
198{
199 return -ENXIO;
200}
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -0800201static inline int pm8921_bms_get_current_max(void)
202{
203 return -ENXIO;
204}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205#endif
206
207#endif