pm8921-bms.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  2. *
  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. #ifndef __PM8XXX_BMS_H
  13. #define __PM8XXX_BMS_H
  14. #include <linux/errno.h>
  15. #include <linux/batterydata-lib.h>
  16. #define PM8921_BMS_DEV_NAME "pm8921-bms"
  17. struct pm8xxx_bms_core_data {
  18. unsigned int batt_temp_channel;
  19. unsigned int vbat_channel;
  20. unsigned int ref625mv_channel;
  21. unsigned int ref1p25v_channel;
  22. unsigned int batt_id_channel;
  23. };
  24. /**
  25. * struct pm8921_bms_platform_data -
  26. * @batt_type: allows to force chose battery calibration data
  27. * @r_sense_uohm: sense resistor value in (micro Ohms)
  28. * @i_test: current at which the unusable charger cutoff is to be
  29. * calculated or the peak system current (mA)
  30. * @v_cutoff: the loaded voltage at which the battery
  31. * is considered empty(mV)
  32. * @enable_fcc_learning: if set the driver will learn full charge
  33. * capacity of the battery upon end of charge
  34. * @min_fcc_learning_soc: minimum SOC as which CC counting for FCC
  35. * learning can start
  36. * @min_fcc_ocv_pc: minimum PC (lookup(OCV)) at which CC counting
  37. * for FCC learning can start
  38. * @min_fcc_learning_samples: Minimum number of FCC measurement cycles to be
  39. * used for FCC update
  40. * @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
  44. * @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
  47. * @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
  49. * @high_ocv_correction_limit_uv: the max amount of OCV corrections
  50. * allowed when ocv is high
  51. * (higher than 3.8V)
  52. * @low_ocv_correction_limit_uv: the max amount of OCV corrections
  53. * allowed when ocv is low
  54. * (lower or equal to 3.8V)
  55. * @hold_soc_est: the min est soc below which the calculated soc
  56. * is allowed to go to 0%
  57. */
  58. struct pm8921_bms_platform_data {
  59. struct pm8xxx_bms_core_data bms_cdata;
  60. enum battery_type battery_type;
  61. int r_sense_uohm;
  62. unsigned int i_test;
  63. unsigned int v_cutoff;
  64. unsigned int max_voltage_uv;
  65. unsigned int rconn_mohm;
  66. unsigned int alarm_low_mv;
  67. unsigned int alarm_high_mv;
  68. int enable_fcc_learning;
  69. int min_fcc_learning_soc;
  70. int min_fcc_ocv_pc;
  71. int min_fcc_learning_samples;
  72. int shutdown_soc_valid_limit;
  73. int ignore_shutdown_soc;
  74. int adjust_soc_low_threshold;
  75. int chg_term_ua;
  76. int normal_voltage_calc_ms;
  77. int low_voltage_calc_ms;
  78. int disable_flat_portion_ocv;
  79. int ocv_dis_high_soc;
  80. int ocv_dis_low_soc;
  81. int low_voltage_detect;
  82. int vbatt_cutoff_retries;
  83. int high_ocv_correction_limit_uv;
  84. int low_ocv_correction_limit_uv;
  85. int hold_soc_est;
  86. };
  87. #if defined(CONFIG_PM8921_BMS) || defined(CONFIG_PM8921_BMS_MODULE)
  88. /**
  89. * pm8921_bms_get_vsense_avg - return the voltage across the sense
  90. * resitor in microvolts
  91. * @result: The pointer where the voltage will be updated. A -ve
  92. * result means that the current is flowing in
  93. * the battery - during battery charging
  94. *
  95. * RETURNS: Error code if there was a problem reading vsense, Zero otherwise
  96. * The result won't be updated in case of an error.
  97. *
  98. *
  99. */
  100. int pm8921_bms_get_vsense_avg(int *result);
  101. /**
  102. * pm8921_bms_get_battery_current - return the battery current based on vsense
  103. * resitor in microamperes
  104. * @result: The pointer where the voltage will be updated. A -ve
  105. * result means that the current is flowing in
  106. * the battery - during battery charging
  107. *
  108. * RETURNS: Error code if there was a problem reading vsense, Zero otherwise
  109. * The result won't be updated in case of an error.
  110. *
  111. */
  112. int pm8921_bms_get_battery_current(int *result);
  113. /**
  114. * pm8921_bms_get_percent_charge - returns the current battery charge in percent
  115. *
  116. */
  117. int pm8921_bms_get_percent_charge(void);
  118. /**
  119. * pm8921_bms_get_fcc - returns fcc in mAh of the battery depending on its age
  120. * and temperature
  121. *
  122. */
  123. int pm8921_bms_get_fcc(void);
  124. /**
  125. * pm8921_bms_charging_began - function to notify the bms driver that charging
  126. * has started. Used by the bms driver to keep
  127. * track of chargecycles
  128. */
  129. void pm8921_bms_charging_began(void);
  130. /**
  131. * pm8921_bms_charging_end - function to notify the bms driver that charging
  132. * has stopped. Used by the bms driver to keep
  133. * track of chargecycles
  134. */
  135. void pm8921_bms_charging_end(int is_battery_full);
  136. void pm8921_bms_calibrate_hkadc(void);
  137. /**
  138. * pm8921_bms_get_simultaneous_battery_voltage_and_current
  139. * - function to take simultaneous vbat and vsense readings
  140. * this puts the bms in override mode but keeps coulumb couting
  141. * on. Useful when ir compensation needs to be implemented
  142. */
  143. int pm8921_bms_get_simultaneous_battery_voltage_and_current(int *ibat_ua,
  144. int *vbat_uv);
  145. /**
  146. * pm8921_bms_get_current_max
  147. * - function to get the max current that can be drawn from
  148. * the battery before it dips below the min allowed voltage
  149. */
  150. int pm8921_bms_get_current_max(void);
  151. /**
  152. * pm8921_bms_invalidate_shutdown_soc - function to notify the bms driver that
  153. * the battery was replaced between reboot
  154. * and so it should not use the shutdown
  155. * soc stored in a coincell backed register
  156. */
  157. void pm8921_bms_invalidate_shutdown_soc(void);
  158. /**
  159. * pm8921_bms_cc_uah - function to get the coulomb counter based charge. Note
  160. * that the coulomb counter are reset when the current
  161. * consumption is low (below 8mA for more than 5 minutes),
  162. * This will lead in a very low coulomb counter charge
  163. * value upon wakeup from sleep.
  164. */
  165. int pm8921_bms_cc_uah(int *cc_uah);
  166. /**
  167. * pm8921_bms_battery_removed - function to be called to tell the bms that
  168. * the battery is removed. The bms resets its internal
  169. * history data used to report soc.
  170. */
  171. void pm8921_bms_battery_removed(void);
  172. /**
  173. * pm8921_bms_battery_inseted - function to be called to tell the bms that
  174. * the battery was inserted. The bms initiates calculations
  175. * for reporting soc.
  176. */
  177. void pm8921_bms_battery_inserted(void);
  178. #else
  179. static inline int pm8921_bms_get_vsense_avg(int *result)
  180. {
  181. return -ENXIO;
  182. }
  183. static inline int pm8921_bms_get_battery_current(int *result)
  184. {
  185. return -ENXIO;
  186. }
  187. static inline int pm8921_bms_get_percent_charge(void)
  188. {
  189. return -ENXIO;
  190. }
  191. static inline int pm8921_bms_get_fcc(void)
  192. {
  193. return -ENXIO;
  194. }
  195. static inline void pm8921_bms_charging_began(void)
  196. {
  197. }
  198. static inline void pm8921_bms_charging_end(int is_battery_full)
  199. {
  200. }
  201. static inline void pm8921_bms_calibrate_hkadc(void)
  202. {
  203. }
  204. static inline int pm8921_bms_get_simultaneous_battery_voltage_and_current(
  205. int *ibat_ua, int *vbat_uv)
  206. {
  207. return -ENXIO;
  208. }
  209. static inline int pm8921_bms_get_rbatt(void)
  210. {
  211. return -EINVAL;
  212. }
  213. static inline void pm8921_bms_invalidate_shutdown_soc(void)
  214. {
  215. }
  216. static inline int pm8921_bms_cc_uah(int *cc_uah)
  217. {
  218. return -ENXIO;
  219. }
  220. static inline int pm8921_bms_get_current_max(void)
  221. {
  222. return -ENXIO;
  223. }
  224. static inline void pm8921_bms_battery_removed(void) {}
  225. static inline void pm8921_bms_battery_inserted(void) {}
  226. #endif
  227. #endif