Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. 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 | |
| 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) |
| 23 | */ |
| 24 | struct pm8xxx_ccadc_platform_data { |
| 25 | int r_sense; |
| 26 | }; |
| 27 | |
| 28 | #define CCADC_READING_RESOLUTION_N_V1 1085069 |
| 29 | #define CCADC_READING_RESOLUTION_D_V1 100000 |
| 30 | #define CCADC_READING_RESOLUTION_N_V2 542535 |
| 31 | #define CCADC_READING_RESOLUTION_D_V2 100000 |
| 32 | |
| 33 | static s64 pm8xxx_ccadc_reading_to_microvolt_v1(s64 cc) |
| 34 | { |
| 35 | return div_s64(cc * CCADC_READING_RESOLUTION_N_V1, |
| 36 | CCADC_READING_RESOLUTION_D_V1); |
| 37 | } |
| 38 | |
| 39 | static s64 pm8xxx_ccadc_reading_to_microvolt_v2(s64 cc) |
| 40 | { |
| 41 | return div_s64(cc * CCADC_READING_RESOLUTION_N_V2, |
| 42 | CCADC_READING_RESOLUTION_D_V2); |
| 43 | } |
| 44 | |
| 45 | static inline s64 pm8xxx_ccadc_reading_to_microvolt(int revision, s64 cc) |
| 46 | { |
| 47 | /* |
| 48 | * resolution (the value of a single bit) was changed after revision 2.0 |
| 49 | * for more accurate readings |
| 50 | */ |
| 51 | return (revision < PM8XXX_REVISION_8921_2p0) ? |
| 52 | pm8xxx_ccadc_reading_to_microvolt_v1((s64)cc) : |
| 53 | pm8xxx_ccadc_reading_to_microvolt_v2((s64)cc); |
| 54 | } |
| 55 | |
| 56 | #if defined(CONFIG_PM8XXX_CCADC) || defined(CONFIG_PM8XXX_CCADC_MODULE) |
| 57 | /** |
| 58 | * pm8xxx_cc_adjust_for_gain - the function to adjust the voltage read from |
| 59 | * ccadc for gain compensation |
| 60 | * @v: the voltage which needs to be gain compensated in microVolts |
| 61 | * |
| 62 | * |
| 63 | * RETURNS: gain compensated voltage |
| 64 | */ |
| 65 | s64 pm8xxx_cc_adjust_for_gain(s64 uv); |
| 66 | |
| 67 | /** |
| 68 | * pm8xxx_calib_ccadc - calibration for ccadc. This will calculate gain |
| 69 | * and offset and reprogram them in the appropriate |
| 70 | * registers |
| 71 | */ |
| 72 | void pm8xxx_calib_ccadc(void); |
| 73 | |
| 74 | /** |
| 75 | * pm8xxx_ccadc_get_battery_current - return the battery current based on vsense |
| 76 | * resitor in milliamperes |
| 77 | * @result: The pointer where the voltage will be updated. A -ve |
| 78 | * result means that the current is flowing in |
| 79 | * the battery - during battery charging |
| 80 | * |
| 81 | * RETURNS: Error code if there was a problem reading vsense, Zero otherwise |
| 82 | * The result won't be updated in case of an error. |
| 83 | * |
| 84 | */ |
| 85 | int pm8xxx_ccadc_get_battery_current(int *bat_current); |
| 86 | #else |
| 87 | static inline s64 pm8xxx_cc_adjust_for_gain(s64 uv) |
| 88 | { |
| 89 | return -ENXIO; |
| 90 | } |
| 91 | static inline void pm8xxx_calib_ccadc(void) |
| 92 | { |
| 93 | } |
| 94 | static inline int pm8xxx_ccadc_get_battery_current(int *bat_current) |
| 95 | { |
| 96 | return -ENXIO; |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | #endif /* __PMIC8XXX_CCADC_H__ */ |