blob: 0bd4cc38b33c46cf0a52e575761747512978148f [file] [log] [blame]
David Keitel3c378822012-06-07 13:43:22 -07001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -08002 *
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 __PMIC8XXX_CCADC_H__
14#define __PMIC8XXX_CCADC_H__
15
16#include <linux/mfd/pm8xxx/core.h>
17
18#define PM8XXX_CCADC_DEV_NAME "pm8xxx-ccadc"
19
20/**
21 * struct pm8xxx_ccadc_platform_data -
22 * @r_sense: sense resistor value in (mOhms)
David Keitel3c378822012-06-07 13:43:22 -070023 * @calib_delay_ms: how often should the adc calculate gain and offset
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -080024 */
25struct pm8xxx_ccadc_platform_data {
David Keitel3c378822012-06-07 13:43:22 -070026 int r_sense;
27 unsigned int calib_delay_ms;
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -080028};
29
30#define CCADC_READING_RESOLUTION_N_V1 1085069
31#define CCADC_READING_RESOLUTION_D_V1 100000
32#define CCADC_READING_RESOLUTION_N_V2 542535
33#define CCADC_READING_RESOLUTION_D_V2 100000
34
35static s64 pm8xxx_ccadc_reading_to_microvolt_v1(s64 cc)
36{
37 return div_s64(cc * CCADC_READING_RESOLUTION_N_V1,
38 CCADC_READING_RESOLUTION_D_V1);
39}
40
41static s64 pm8xxx_ccadc_reading_to_microvolt_v2(s64 cc)
42{
43 return div_s64(cc * CCADC_READING_RESOLUTION_N_V2,
44 CCADC_READING_RESOLUTION_D_V2);
45}
46
47static inline s64 pm8xxx_ccadc_reading_to_microvolt(int revision, s64 cc)
48{
49 /*
50 * resolution (the value of a single bit) was changed after revision 2.0
51 * for more accurate readings
52 */
53 return (revision < PM8XXX_REVISION_8921_2p0) ?
54 pm8xxx_ccadc_reading_to_microvolt_v1((s64)cc) :
55 pm8xxx_ccadc_reading_to_microvolt_v2((s64)cc);
56}
57
58#if defined(CONFIG_PM8XXX_CCADC) || defined(CONFIG_PM8XXX_CCADC_MODULE)
59/**
60 * pm8xxx_cc_adjust_for_gain - the function to adjust the voltage read from
61 * ccadc for gain compensation
62 * @v: the voltage which needs to be gain compensated in microVolts
63 *
64 *
65 * RETURNS: gain compensated voltage
66 */
67s64 pm8xxx_cc_adjust_for_gain(s64 uv);
68
69/**
70 * pm8xxx_calib_ccadc - calibration for ccadc. This will calculate gain
71 * and offset and reprogram them in the appropriate
72 * registers
73 */
74void pm8xxx_calib_ccadc(void);
75
76/**
77 * pm8xxx_ccadc_get_battery_current - return the battery current based on vsense
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -080078 * resitor in microamperes
Abhijeet Dharmapurikar82d93982011-11-09 15:52:25 -080079 * @result: The pointer where the voltage will be updated. A -ve
80 * result means that the current is flowing in
81 * the battery - during battery charging
82 *
83 * RETURNS: Error code if there was a problem reading vsense, Zero otherwise
84 * The result won't be updated in case of an error.
85 *
86 */
87int pm8xxx_ccadc_get_battery_current(int *bat_current);
88#else
89static inline s64 pm8xxx_cc_adjust_for_gain(s64 uv)
90{
91 return -ENXIO;
92}
93static inline void pm8xxx_calib_ccadc(void)
94{
95}
96static inline int pm8xxx_ccadc_get_battery_current(int *bat_current)
97{
98 return -ENXIO;
99}
100#endif
101
102#endif /* __PMIC8XXX_CCADC_H__ */