Abhijeet Dharmapurikar | f154bfc | 2013-01-23 00:24:58 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 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 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/moduleparam.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/mfd/pm8xxx/core.h> |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 20 | #include <linux/mfd/pm8xxx/pm8xxx-adc.h> |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 21 | #include <linux/mfd/pm8xxx/ccadc.h> |
| 22 | #include <linux/interrupt.h> |
Anirudh Ghayal | 1d11747 | 2012-12-04 12:41:53 +0530 | [diff] [blame] | 23 | #include <linux/irq.h> |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 24 | #include <linux/ioport.h> |
| 25 | #include <linux/debugfs.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/delay.h> |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 28 | #include <linux/rtc.h> |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 29 | |
| 30 | #define CCADC_ANA_PARAM 0x240 |
| 31 | #define CCADC_DIG_PARAM 0x241 |
| 32 | #define CCADC_RSV 0x242 |
| 33 | #define CCADC_DATA0 0x244 |
| 34 | #define CCADC_DATA1 0x245 |
| 35 | #define CCADC_OFFSET_TRIM1 0x34A |
| 36 | #define CCADC_OFFSET_TRIM0 0x34B |
| 37 | #define CCADC_FULLSCALE_TRIM1 0x34C |
| 38 | #define CCADC_FULLSCALE_TRIM0 0x34D |
| 39 | |
| 40 | /* note : TRIM1 is the msb and TRIM0 is the lsb */ |
| 41 | #define ADC_ARB_SECP_CNTRL 0x190 |
| 42 | #define ADC_ARB_SECP_AMUX_CNTRL 0x191 |
| 43 | #define ADC_ARB_SECP_ANA_PARAM 0x192 |
| 44 | #define ADC_ARB_SECP_DIG_PARAM 0x193 |
| 45 | #define ADC_ARB_SECP_RSV 0x194 |
| 46 | #define ADC_ARB_SECP_DATA1 0x195 |
| 47 | #define ADC_ARB_SECP_DATA0 0x196 |
| 48 | |
| 49 | #define ADC_ARB_BMS_CNTRL 0x18D |
| 50 | |
| 51 | #define START_CONV_BIT BIT(7) |
| 52 | #define EOC_CONV_BIT BIT(6) |
| 53 | #define SEL_CCADC_BIT BIT(1) |
| 54 | #define EN_ARB_BIT BIT(0) |
| 55 | |
| 56 | #define CCADC_CALIB_DIG_PARAM 0xE3 |
| 57 | #define CCADC_CALIB_RSV_GND 0x40 |
| 58 | #define CCADC_CALIB_RSV_25MV 0x80 |
| 59 | #define CCADC_CALIB_ANA_PARAM 0x1B |
| 60 | #define SAMPLE_COUNT 16 |
| 61 | #define ADC_WAIT_COUNT 10 |
| 62 | |
| 63 | #define CCADC_MAX_25MV 30000 |
| 64 | #define CCADC_MIN_25MV 20000 |
| 65 | #define CCADC_MAX_0UV -4000 |
| 66 | #define CCADC_MIN_0UV -7000 |
| 67 | |
| 68 | #define CCADC_INTRINSIC_OFFSET 0xC000 |
| 69 | |
| 70 | struct pm8xxx_ccadc_chip { |
| 71 | struct device *dev; |
| 72 | struct dentry *dent; |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 73 | unsigned int batt_temp_channel; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 74 | u16 ccadc_offset; |
| 75 | int ccadc_gain_uv; |
| 76 | unsigned int revision; |
David Keitel | 3c37882 | 2012-06-07 13:43:22 -0700 | [diff] [blame] | 77 | unsigned int calib_delay_ms; |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 78 | unsigned long last_calib_time; |
| 79 | int last_calib_temp; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 80 | int eoc_irq; |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 81 | int r_sense_uohm; |
David Keitel | 3c37882 | 2012-06-07 13:43:22 -0700 | [diff] [blame] | 82 | struct delayed_work calib_ccadc_work; |
Abhijeet Dharmapurikar | f154bfc | 2013-01-23 00:24:58 -0800 | [diff] [blame] | 83 | struct mutex calib_mutex; |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 84 | bool periodic_wakeup; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static struct pm8xxx_ccadc_chip *the_chip; |
| 88 | |
Abhijeet Dharmapurikar | ed8e5fb | 2011-12-07 14:31:26 -0800 | [diff] [blame] | 89 | #ifdef DEBUG |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 90 | static s64 microvolt_to_ccadc_reading(struct pm8xxx_ccadc_chip *chip, s64 cc) |
| 91 | { |
Abhijeet Dharmapurikar | 305c529 | 2012-06-21 16:15:43 -0700 | [diff] [blame] | 92 | return div_s64(uv * CCADC_READING_RESOLUTION_D, |
| 93 | CCADC_READING_RESOLUTION_N); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 94 | } |
Abhijeet Dharmapurikar | ed8e5fb | 2011-12-07 14:31:26 -0800 | [diff] [blame] | 95 | #endif |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 96 | |
| 97 | static int cc_adjust_for_offset(u16 raw) |
| 98 | { |
| 99 | /* this has the intrinsic offset */ |
| 100 | return (int)raw - the_chip->ccadc_offset; |
| 101 | } |
| 102 | |
| 103 | #define GAIN_REFERENCE_UV 25000 |
| 104 | /* |
| 105 | * gain compensation for ccadc readings - common for vsense based and |
| 106 | * couloumb counter based readings |
| 107 | */ |
| 108 | s64 pm8xxx_cc_adjust_for_gain(s64 uv) |
| 109 | { |
| 110 | if (the_chip == NULL || the_chip->ccadc_gain_uv == 0) |
| 111 | return uv; |
| 112 | |
| 113 | return div_s64(uv * GAIN_REFERENCE_UV, the_chip->ccadc_gain_uv); |
| 114 | } |
| 115 | EXPORT_SYMBOL(pm8xxx_cc_adjust_for_gain); |
| 116 | |
| 117 | static int pm_ccadc_masked_write(struct pm8xxx_ccadc_chip *chip, u16 addr, |
| 118 | u8 mask, u8 val) |
| 119 | { |
| 120 | int rc; |
| 121 | u8 reg; |
| 122 | |
| 123 | rc = pm8xxx_readb(chip->dev->parent, addr, ®); |
| 124 | if (rc) { |
| 125 | pr_err("read failed addr = %03X, rc = %d\n", addr, rc); |
| 126 | return rc; |
| 127 | } |
| 128 | reg &= ~mask; |
| 129 | reg |= val & mask; |
| 130 | rc = pm8xxx_writeb(chip->dev->parent, addr, reg); |
| 131 | if (rc) { |
| 132 | pr_err("write failed addr = %03X, rc = %d\n", addr, rc); |
| 133 | return rc; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | #define REG_SBI_CONFIG 0x04F |
| 139 | #define PAGE3_ENABLE_MASK 0x6 |
| 140 | static int calib_ccadc_enable_trim_access(struct pm8xxx_ccadc_chip *chip, |
| 141 | u8 *sbi_config) |
| 142 | { |
| 143 | u8 reg; |
| 144 | int rc; |
| 145 | |
| 146 | rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, sbi_config); |
| 147 | if (rc) { |
| 148 | pr_err("error = %d reading sbi config reg\n", rc); |
| 149 | return rc; |
| 150 | } |
| 151 | |
| 152 | reg = *sbi_config | PAGE3_ENABLE_MASK; |
| 153 | return pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, reg); |
| 154 | } |
| 155 | |
| 156 | static int calib_ccadc_restore_trim_access(struct pm8xxx_ccadc_chip *chip, |
| 157 | u8 sbi_config) |
| 158 | { |
| 159 | return pm8xxx_writeb(chip->dev->parent, REG_SBI_CONFIG, sbi_config); |
| 160 | } |
| 161 | |
| 162 | static int calib_ccadc_enable_arbiter(struct pm8xxx_ccadc_chip *chip) |
| 163 | { |
| 164 | int rc; |
| 165 | |
| 166 | /* enable Arbiter, must be sent twice */ |
| 167 | rc = pm_ccadc_masked_write(chip, ADC_ARB_SECP_CNTRL, |
| 168 | SEL_CCADC_BIT | EN_ARB_BIT, SEL_CCADC_BIT | EN_ARB_BIT); |
| 169 | if (rc < 0) { |
| 170 | pr_err("error = %d enabling arbiter for offset\n", rc); |
| 171 | return rc; |
| 172 | } |
| 173 | rc = pm_ccadc_masked_write(chip, ADC_ARB_SECP_CNTRL, |
| 174 | SEL_CCADC_BIT | EN_ARB_BIT, SEL_CCADC_BIT | EN_ARB_BIT); |
| 175 | if (rc < 0) { |
| 176 | pr_err("error = %d writing ADC_ARB_SECP_CNTRL\n", rc); |
| 177 | return rc; |
| 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int calib_start_conv(struct pm8xxx_ccadc_chip *chip, |
| 183 | u16 *result) |
| 184 | { |
| 185 | int rc, i; |
| 186 | u8 data_msb, data_lsb, reg; |
| 187 | |
| 188 | /* Start conversion */ |
| 189 | rc = pm_ccadc_masked_write(chip, ADC_ARB_SECP_CNTRL, |
| 190 | START_CONV_BIT, START_CONV_BIT); |
| 191 | if (rc < 0) { |
| 192 | pr_err("error = %d starting offset meas\n", rc); |
| 193 | return rc; |
| 194 | } |
| 195 | |
| 196 | /* Wait for End of conversion */ |
| 197 | for (i = 0; i < ADC_WAIT_COUNT; i++) { |
| 198 | rc = pm8xxx_readb(chip->dev->parent, |
| 199 | ADC_ARB_SECP_CNTRL, ®); |
| 200 | if (rc < 0) { |
| 201 | pr_err("error = %d read eoc for offset\n", rc); |
| 202 | return rc; |
| 203 | } |
| 204 | if ((reg & (START_CONV_BIT | EOC_CONV_BIT)) != EOC_CONV_BIT) |
Abhijeet Dharmapurikar | ccfc4f3 | 2012-01-16 17:35:18 -0800 | [diff] [blame] | 205 | msleep(20); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 206 | else |
| 207 | break; |
| 208 | } |
| 209 | if (i == ADC_WAIT_COUNT) { |
David Keitel | eb38081 | 2012-04-09 18:34:12 -0700 | [diff] [blame] | 210 | pr_err("waited too long for offset eoc returning -EBUSY\n"); |
| 211 | return -EBUSY; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | rc = pm8xxx_readb(chip->dev->parent, ADC_ARB_SECP_DATA0, &data_lsb); |
| 215 | if (rc < 0) { |
| 216 | pr_err("error = %d reading offset lsb\n", rc); |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | rc = pm8xxx_readb(chip->dev->parent, ADC_ARB_SECP_DATA1, &data_msb); |
| 221 | if (rc < 0) { |
| 222 | pr_err("error = %d reading offset msb\n", rc); |
| 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | *result = (data_msb << 8) | data_lsb; |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int calib_ccadc_read_trim(struct pm8xxx_ccadc_chip *chip, |
| 231 | int addr, u8 *data_msb, u8 *data_lsb) |
| 232 | { |
| 233 | int rc; |
| 234 | u8 sbi_config; |
| 235 | |
| 236 | calib_ccadc_enable_trim_access(chip, &sbi_config); |
| 237 | rc = pm8xxx_readb(chip->dev->parent, addr, data_msb); |
| 238 | if (rc < 0) { |
| 239 | pr_err("error = %d read msb\n", rc); |
| 240 | return rc; |
| 241 | } |
| 242 | rc = pm8xxx_readb(chip->dev->parent, addr + 1, data_lsb); |
| 243 | if (rc < 0) { |
| 244 | pr_err("error = %d read lsb\n", rc); |
| 245 | return rc; |
| 246 | } |
| 247 | calib_ccadc_restore_trim_access(chip, sbi_config); |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static void calib_ccadc_read_offset_and_gain(struct pm8xxx_ccadc_chip *chip, |
| 252 | int *gain, u16 *offset) |
| 253 | { |
Abhijeet Dharmapurikar | 034a034 | 2011-12-08 11:12:54 -0800 | [diff] [blame] | 254 | u8 data_msb; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 255 | u8 data_lsb; |
| 256 | int rc; |
| 257 | |
| 258 | rc = calib_ccadc_read_trim(chip, CCADC_FULLSCALE_TRIM1, |
| 259 | &data_msb, &data_lsb); |
| 260 | *gain = (data_msb << 8) | data_lsb; |
| 261 | |
| 262 | rc = calib_ccadc_read_trim(chip, CCADC_OFFSET_TRIM1, |
| 263 | &data_msb, &data_lsb); |
| 264 | *offset = (data_msb << 8) | data_lsb; |
| 265 | |
| 266 | pr_debug("raw gain trim = 0x%x offset trim =0x%x\n", *gain, *offset); |
| 267 | *gain = pm8xxx_ccadc_reading_to_microvolt(chip->revision, |
| 268 | (s64)*gain - *offset); |
| 269 | pr_debug("gain uv = %duV offset=0x%x\n", *gain, *offset); |
| 270 | } |
| 271 | |
| 272 | #define CCADC_PROGRAM_TRIM_COUNT 2 |
| 273 | #define ADC_ARB_BMS_CNTRL_CCADC_SHIFT 4 |
| 274 | #define ADC_ARB_BMS_CNTRL_CONV_MASK 0x03 |
| 275 | #define BMS_CONV_IN_PROGRESS 0x2 |
| 276 | |
| 277 | static int calib_ccadc_program_trim(struct pm8xxx_ccadc_chip *chip, |
| 278 | int addr, u8 data_msb, u8 data_lsb, |
| 279 | int wait) |
| 280 | { |
| 281 | int i, rc, loop; |
| 282 | u8 cntrl, sbi_config; |
| 283 | bool in_progress = 0; |
| 284 | |
| 285 | loop = wait ? CCADC_PROGRAM_TRIM_COUNT : 0; |
| 286 | |
| 287 | calib_ccadc_enable_trim_access(chip, &sbi_config); |
| 288 | |
| 289 | for (i = 0; i < loop; i++) { |
| 290 | rc = pm8xxx_readb(chip->dev->parent, ADC_ARB_BMS_CNTRL, &cntrl); |
| 291 | if (rc < 0) { |
| 292 | pr_err("error = %d reading ADC_ARB_BMS_CNTRL\n", rc); |
| 293 | return rc; |
| 294 | } |
| 295 | |
| 296 | /* break if a ccadc conversion is not happening */ |
| 297 | in_progress = (((cntrl >> ADC_ARB_BMS_CNTRL_CCADC_SHIFT) |
| 298 | & ADC_ARB_BMS_CNTRL_CONV_MASK) == BMS_CONV_IN_PROGRESS); |
| 299 | |
| 300 | if (!in_progress) |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | if (in_progress) { |
| 305 | pr_debug("conv in progress cannot write trim,returing EBUSY\n"); |
| 306 | return -EBUSY; |
| 307 | } |
| 308 | |
| 309 | rc = pm8xxx_writeb(chip->dev->parent, addr, data_msb); |
| 310 | if (rc < 0) { |
| 311 | pr_err("error = %d write msb = 0x%x\n", rc, data_msb); |
| 312 | return rc; |
| 313 | } |
| 314 | rc = pm8xxx_writeb(chip->dev->parent, addr + 1, data_lsb); |
| 315 | if (rc < 0) { |
| 316 | pr_err("error = %d write lsb = 0x%x\n", rc, data_lsb); |
| 317 | return rc; |
| 318 | } |
| 319 | calib_ccadc_restore_trim_access(chip, sbi_config); |
| 320 | return 0; |
| 321 | } |
| 322 | |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 323 | static int get_batt_temp(struct pm8xxx_ccadc_chip *chip, int *batt_temp) |
| 324 | { |
| 325 | int rc; |
| 326 | struct pm8xxx_adc_chan_result result; |
| 327 | |
| 328 | rc = pm8xxx_adc_read(chip->batt_temp_channel, &result); |
| 329 | if (rc) { |
| 330 | pr_err("error reading batt_temp_channel = %d, rc = %d\n", |
| 331 | chip->batt_temp_channel, rc); |
| 332 | return rc; |
| 333 | } |
| 334 | *batt_temp = result.physical; |
| 335 | pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical, |
| 336 | result.measurement); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int get_current_time(unsigned long *now_tm_sec) |
| 341 | { |
| 342 | struct rtc_time tm; |
| 343 | struct rtc_device *rtc; |
| 344 | int rc; |
| 345 | |
| 346 | rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE); |
| 347 | if (rtc == NULL) { |
| 348 | pr_err("%s: unable to open rtc device (%s)\n", |
| 349 | __FILE__, CONFIG_RTC_HCTOSYS_DEVICE); |
| 350 | return -EINVAL; |
| 351 | } |
| 352 | |
| 353 | rc = rtc_read_time(rtc, &tm); |
| 354 | if (rc) { |
| 355 | pr_err("Error reading rtc device (%s) : %d\n", |
| 356 | CONFIG_RTC_HCTOSYS_DEVICE, rc); |
| 357 | return rc; |
| 358 | } |
| 359 | |
| 360 | rc = rtc_valid_tm(&tm); |
| 361 | if (rc) { |
| 362 | pr_err("Invalid RTC time (%s): %d\n", |
| 363 | CONFIG_RTC_HCTOSYS_DEVICE, rc); |
| 364 | return rc; |
| 365 | } |
| 366 | rtc_tm_to_time(&tm, now_tm_sec); |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 371 | static void __pm8xxx_calib_ccadc(int sample_count) |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 372 | { |
| 373 | u8 data_msb, data_lsb, sec_cntrl; |
Abhijeet Dharmapurikar | ed8e5fb | 2011-12-07 14:31:26 -0800 | [diff] [blame] | 374 | int result_offset, result_gain; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 375 | u16 result; |
| 376 | int i, rc; |
| 377 | |
David Keitel | 3c37882 | 2012-06-07 13:43:22 -0700 | [diff] [blame] | 378 | if (!the_chip) { |
| 379 | pr_err("chip not initialized\n"); |
| 380 | return; |
| 381 | } |
| 382 | |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 383 | pr_debug("sample_count = %d\n", sample_count); |
| 384 | |
Abhijeet Dharmapurikar | f154bfc | 2013-01-23 00:24:58 -0800 | [diff] [blame] | 385 | mutex_lock(&the_chip->calib_mutex); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 386 | rc = pm8xxx_readb(the_chip->dev->parent, |
| 387 | ADC_ARB_SECP_CNTRL, &sec_cntrl); |
| 388 | if (rc < 0) { |
| 389 | pr_err("error = %d reading ADC_ARB_SECP_CNTRL\n", rc); |
Abhijeet Dharmapurikar | f154bfc | 2013-01-23 00:24:58 -0800 | [diff] [blame] | 390 | goto calibration_unlock; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | rc = calib_ccadc_enable_arbiter(the_chip); |
| 394 | if (rc < 0) { |
| 395 | pr_err("error = %d enabling arbiter for offset\n", rc); |
| 396 | goto bail; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Set decimation ratio to 4k, lower ratio may be used in order to speed |
| 401 | * up, pending verification through bench |
| 402 | */ |
| 403 | rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_DIG_PARAM, |
| 404 | CCADC_CALIB_DIG_PARAM); |
| 405 | if (rc < 0) { |
| 406 | pr_err("error = %d writing ADC_ARB_SECP_DIG_PARAM\n", rc); |
| 407 | goto bail; |
| 408 | } |
| 409 | |
| 410 | result_offset = 0; |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 411 | for (i = 0; i < sample_count; i++) { |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 412 | /* Short analog inputs to CCADC internally to ground */ |
| 413 | rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_RSV, |
| 414 | CCADC_CALIB_RSV_GND); |
| 415 | if (rc < 0) { |
| 416 | pr_err("error = %d selecting gnd voltage\n", rc); |
| 417 | goto bail; |
| 418 | } |
| 419 | |
| 420 | /* Enable CCADC */ |
| 421 | rc = pm8xxx_writeb(the_chip->dev->parent, |
| 422 | ADC_ARB_SECP_ANA_PARAM, CCADC_CALIB_ANA_PARAM); |
| 423 | if (rc < 0) { |
| 424 | pr_err("error = %d enabling ccadc\n", rc); |
| 425 | goto bail; |
| 426 | } |
| 427 | |
| 428 | rc = calib_start_conv(the_chip, &result); |
| 429 | if (rc < 0) { |
| 430 | pr_err("error = %d for zero volt measurement\n", rc); |
| 431 | goto bail; |
| 432 | } |
| 433 | |
| 434 | result_offset += result; |
| 435 | } |
| 436 | |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 437 | result_offset = result_offset / sample_count; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 438 | |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 439 | |
Abhijeet Dharmapurikar | ed8e5fb | 2011-12-07 14:31:26 -0800 | [diff] [blame] | 440 | pr_debug("offset result_offset = 0x%x, voltage = %llduV\n", |
| 441 | result_offset, |
| 442 | pm8xxx_ccadc_reading_to_microvolt(the_chip->revision, |
| 443 | ((s64)result_offset - CCADC_INTRINSIC_OFFSET))); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 444 | |
| 445 | the_chip->ccadc_offset = result_offset; |
| 446 | data_msb = the_chip->ccadc_offset >> 8; |
| 447 | data_lsb = the_chip->ccadc_offset; |
| 448 | |
| 449 | rc = calib_ccadc_program_trim(the_chip, CCADC_OFFSET_TRIM1, |
| 450 | data_msb, data_lsb, 1); |
| 451 | if (rc) { |
| 452 | pr_debug("error = %d programming offset trim 0x%02x 0x%02x\n", |
| 453 | rc, data_msb, data_lsb); |
| 454 | /* enable the interrupt and write it when it fires */ |
| 455 | enable_irq(the_chip->eoc_irq); |
| 456 | } |
| 457 | |
| 458 | rc = calib_ccadc_enable_arbiter(the_chip); |
| 459 | if (rc < 0) { |
| 460 | pr_err("error = %d enabling arbiter for gain\n", rc); |
| 461 | goto bail; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Set decimation ratio to 4k, lower ratio may be used in order to speed |
| 466 | * up, pending verification through bench |
| 467 | */ |
| 468 | rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_DIG_PARAM, |
| 469 | CCADC_CALIB_DIG_PARAM); |
| 470 | if (rc < 0) { |
| 471 | pr_err("error = %d enabling decimation ration for gain\n", rc); |
| 472 | goto bail; |
| 473 | } |
| 474 | |
| 475 | result_gain = 0; |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 476 | for (i = 0; i < sample_count; i++) { |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 477 | rc = pm8xxx_writeb(the_chip->dev->parent, |
| 478 | ADC_ARB_SECP_RSV, CCADC_CALIB_RSV_25MV); |
| 479 | if (rc < 0) { |
| 480 | pr_err("error = %d selecting 25mV for gain\n", rc); |
| 481 | goto bail; |
| 482 | } |
| 483 | |
| 484 | /* Enable CCADC */ |
| 485 | rc = pm8xxx_writeb(the_chip->dev->parent, |
| 486 | ADC_ARB_SECP_ANA_PARAM, CCADC_CALIB_ANA_PARAM); |
| 487 | if (rc < 0) { |
| 488 | pr_err("error = %d enabling ccadc\n", rc); |
| 489 | goto bail; |
| 490 | } |
| 491 | |
| 492 | rc = calib_start_conv(the_chip, &result); |
| 493 | if (rc < 0) { |
| 494 | pr_err("error = %d for adc reading 25mV\n", rc); |
| 495 | goto bail; |
| 496 | } |
| 497 | |
| 498 | result_gain += result; |
| 499 | } |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 500 | result_gain = result_gain / sample_count; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * result_offset includes INTRINSIC OFFSET |
| 504 | * the_chip->ccadc_gain_uv will be the actual voltage |
| 505 | * measured for 25000UV |
| 506 | */ |
| 507 | the_chip->ccadc_gain_uv = pm8xxx_ccadc_reading_to_microvolt( |
| 508 | the_chip->revision, |
| 509 | ((s64)result_gain - result_offset)); |
| 510 | |
| 511 | pr_debug("gain result_gain = 0x%x, voltage = %d microVolts\n", |
| 512 | result_gain, the_chip->ccadc_gain_uv); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 513 | |
| 514 | data_msb = result_gain >> 8; |
| 515 | data_lsb = result_gain; |
| 516 | rc = calib_ccadc_program_trim(the_chip, CCADC_FULLSCALE_TRIM1, |
| 517 | data_msb, data_lsb, 0); |
| 518 | if (rc) |
| 519 | pr_debug("error = %d programming gain trim\n", rc); |
| 520 | bail: |
| 521 | pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_CNTRL, sec_cntrl); |
Abhijeet Dharmapurikar | f154bfc | 2013-01-23 00:24:58 -0800 | [diff] [blame] | 522 | calibration_unlock: |
| 523 | mutex_unlock(&the_chip->calib_mutex); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 524 | } |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 525 | |
| 526 | static void pm8xxx_calib_ccadc_quick(void) |
| 527 | { |
| 528 | __pm8xxx_calib_ccadc(2); |
| 529 | } |
| 530 | |
| 531 | void pm8xxx_calib_ccadc(void) |
| 532 | { |
| 533 | __pm8xxx_calib_ccadc(SAMPLE_COUNT); |
| 534 | } |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 535 | EXPORT_SYMBOL(pm8xxx_calib_ccadc); |
| 536 | |
David Keitel | 3c37882 | 2012-06-07 13:43:22 -0700 | [diff] [blame] | 537 | static void calibrate_ccadc_work(struct work_struct *work) |
| 538 | { |
| 539 | struct pm8xxx_ccadc_chip *chip = container_of(work, |
| 540 | struct pm8xxx_ccadc_chip, calib_ccadc_work.work); |
| 541 | |
| 542 | pm8xxx_calib_ccadc(); |
| 543 | schedule_delayed_work(&chip->calib_ccadc_work, |
| 544 | round_jiffies_relative(msecs_to_jiffies |
| 545 | (chip->calib_delay_ms))); |
| 546 | } |
| 547 | |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 548 | static irqreturn_t pm8921_bms_ccadc_eoc_handler(int irq, void *data) |
| 549 | { |
| 550 | u8 data_msb, data_lsb; |
| 551 | struct pm8xxx_ccadc_chip *chip = data; |
| 552 | int rc; |
| 553 | |
Abhijeet Dharmapurikar | 1b81734 | 2012-10-11 11:24:50 -0700 | [diff] [blame] | 554 | if (!the_chip) |
| 555 | goto out; |
| 556 | |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 557 | pr_debug("irq = %d triggered\n", irq); |
| 558 | data_msb = chip->ccadc_offset >> 8; |
| 559 | data_lsb = chip->ccadc_offset; |
| 560 | |
| 561 | rc = calib_ccadc_program_trim(chip, CCADC_OFFSET_TRIM1, |
| 562 | data_msb, data_lsb, 0); |
| 563 | disable_irq_nosync(chip->eoc_irq); |
| 564 | |
Abhijeet Dharmapurikar | 1b81734 | 2012-10-11 11:24:50 -0700 | [diff] [blame] | 565 | out: |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 566 | return IRQ_HANDLED; |
| 567 | } |
| 568 | |
| 569 | #define CCADC_IBAT_DIG_PARAM 0xA3 |
| 570 | #define CCADC_IBAT_RSV 0x10 |
| 571 | #define CCADC_IBAT_ANA_PARAM 0x1A |
| 572 | static int ccadc_get_rsense_voltage(int *voltage_uv) |
| 573 | { |
| 574 | u16 raw; |
| 575 | int result; |
| 576 | int rc = 0; |
| 577 | |
| 578 | rc = calib_ccadc_enable_arbiter(the_chip); |
| 579 | if (rc < 0) { |
| 580 | pr_err("error = %d enabling arbiter for offset\n", rc); |
| 581 | return rc; |
| 582 | } |
| 583 | |
| 584 | rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_DIG_PARAM, |
| 585 | CCADC_IBAT_DIG_PARAM); |
| 586 | if (rc < 0) { |
| 587 | pr_err("error = %d writing ADC_ARB_SECP_DIG_PARAM\n", rc); |
| 588 | return rc; |
| 589 | } |
| 590 | |
| 591 | rc = pm8xxx_writeb(the_chip->dev->parent, ADC_ARB_SECP_RSV, |
| 592 | CCADC_IBAT_RSV); |
| 593 | if (rc < 0) { |
| 594 | pr_err("error = %d selecting rsense\n", rc); |
| 595 | return rc; |
| 596 | } |
| 597 | |
| 598 | rc = pm8xxx_writeb(the_chip->dev->parent, |
| 599 | ADC_ARB_SECP_ANA_PARAM, CCADC_IBAT_ANA_PARAM); |
| 600 | if (rc < 0) { |
| 601 | pr_err("error = %d enabling ccadc\n", rc); |
| 602 | return rc; |
| 603 | } |
| 604 | |
| 605 | rc = calib_start_conv(the_chip, &raw); |
| 606 | if (rc < 0) { |
| 607 | pr_err("error = %d for zero volt measurement\n", rc); |
| 608 | return rc; |
| 609 | } |
| 610 | |
| 611 | pr_debug("Vsense raw = 0x%x\n", raw); |
| 612 | result = cc_adjust_for_offset(raw); |
| 613 | pr_debug("Vsense after offset raw = 0x%x offset=0x%x\n", |
| 614 | result, |
| 615 | the_chip->ccadc_offset); |
| 616 | *voltage_uv = pm8xxx_ccadc_reading_to_microvolt(the_chip->revision, |
| 617 | ((s64)result)); |
Abhijeet Dharmapurikar | 034a034 | 2011-12-08 11:12:54 -0800 | [diff] [blame] | 618 | pr_debug("Vsense before gain of %d = %d uV\n", the_chip->ccadc_gain_uv, |
| 619 | *voltage_uv); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 620 | *voltage_uv = pm8xxx_cc_adjust_for_gain(*voltage_uv); |
| 621 | |
| 622 | pr_debug("Vsense = %d uV\n", *voltage_uv); |
| 623 | return 0; |
| 624 | } |
| 625 | |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 626 | int pm8xxx_ccadc_get_battery_current(int *bat_current_ua) |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 627 | { |
Trilok Soni | c6e541e | 2012-03-19 17:14:28 +0530 | [diff] [blame] | 628 | int voltage_uv = 0, rc; |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 629 | |
| 630 | rc = ccadc_get_rsense_voltage(&voltage_uv); |
| 631 | if (rc) { |
| 632 | pr_err("cant get voltage across rsense rc = %d\n", rc); |
| 633 | return rc; |
| 634 | } |
| 635 | |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 636 | *bat_current_ua = div_s64((s64)voltage_uv * 1000000LL, |
| 637 | the_chip->r_sense_uohm); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 638 | /* |
| 639 | * ccadc reads +ve current when the battery is charging |
| 640 | * We need to return -ve if the battery is charging |
| 641 | */ |
Siddartha Mohanadoss | 37e6fc0 | 2011-11-16 16:57:03 -0800 | [diff] [blame] | 642 | *bat_current_ua = -1 * (*bat_current_ua); |
| 643 | pr_debug("bat current = %d ma\n", *bat_current_ua); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | EXPORT_SYMBOL(pm8xxx_ccadc_get_battery_current); |
| 647 | |
| 648 | static int get_reg(void *data, u64 * val) |
| 649 | { |
| 650 | int addr = (int)data; |
| 651 | int ret; |
| 652 | u8 temp; |
| 653 | |
| 654 | ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp); |
| 655 | if (ret) { |
| 656 | pr_err("pm8xxx_readb to %x value = %d errored = %d\n", |
| 657 | addr, temp, ret); |
| 658 | return -EAGAIN; |
| 659 | } |
| 660 | *val = temp; |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | static int set_reg(void *data, u64 val) |
| 665 | { |
| 666 | int addr = (int)data; |
| 667 | int ret; |
| 668 | u8 temp; |
| 669 | |
| 670 | temp = (u8) val; |
| 671 | ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp); |
| 672 | if (ret) { |
| 673 | pr_err("pm8xxx_writeb to %x value = %d errored = %d\n", |
| 674 | addr, temp, ret); |
| 675 | return -EAGAIN; |
| 676 | } |
| 677 | return 0; |
| 678 | } |
| 679 | DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n"); |
| 680 | |
| 681 | static int get_calc(void *data, u64 * val) |
| 682 | { |
| 683 | int ibat, rc; |
| 684 | |
| 685 | rc = pm8xxx_ccadc_get_battery_current(&ibat); |
| 686 | *val = ibat; |
| 687 | return rc; |
| 688 | } |
| 689 | DEFINE_SIMPLE_ATTRIBUTE(calc_fops, get_calc, NULL, "%lld\n"); |
| 690 | |
| 691 | static void create_debugfs_entries(struct pm8xxx_ccadc_chip *chip) |
| 692 | { |
| 693 | chip->dent = debugfs_create_dir("pm8xxx-ccadc", NULL); |
| 694 | |
| 695 | if (IS_ERR(chip->dent)) { |
| 696 | pr_err("ccadc couldnt create debugfs dir\n"); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | debugfs_create_file("CCADC_ANA_PARAM", 0644, chip->dent, |
| 701 | (void *)CCADC_ANA_PARAM, ®_fops); |
| 702 | debugfs_create_file("CCADC_DIG_PARAM", 0644, chip->dent, |
| 703 | (void *)CCADC_DIG_PARAM, ®_fops); |
| 704 | debugfs_create_file("CCADC_RSV", 0644, chip->dent, |
| 705 | (void *)CCADC_RSV, ®_fops); |
| 706 | debugfs_create_file("CCADC_DATA0", 0644, chip->dent, |
| 707 | (void *)CCADC_DATA0, ®_fops); |
| 708 | debugfs_create_file("CCADC_DATA1", 0644, chip->dent, |
| 709 | (void *)CCADC_DATA1, ®_fops); |
| 710 | debugfs_create_file("CCADC_OFFSET_TRIM1", 0644, chip->dent, |
| 711 | (void *)CCADC_OFFSET_TRIM1, ®_fops); |
| 712 | debugfs_create_file("CCADC_OFFSET_TRIM0", 0644, chip->dent, |
| 713 | (void *)CCADC_OFFSET_TRIM0, ®_fops); |
| 714 | debugfs_create_file("CCADC_FULLSCALE_TRIM1", 0644, chip->dent, |
| 715 | (void *)CCADC_FULLSCALE_TRIM1, ®_fops); |
| 716 | debugfs_create_file("CCADC_FULLSCALE_TRIM0", 0644, chip->dent, |
| 717 | (void *)CCADC_FULLSCALE_TRIM0, ®_fops); |
| 718 | |
| 719 | debugfs_create_file("show_ibatt", 0644, chip->dent, |
| 720 | (void *)0, &calc_fops); |
| 721 | } |
| 722 | |
| 723 | static int __devinit pm8xxx_ccadc_probe(struct platform_device *pdev) |
| 724 | { |
| 725 | int rc = 0; |
| 726 | struct pm8xxx_ccadc_chip *chip; |
| 727 | struct resource *res; |
| 728 | const struct pm8xxx_ccadc_platform_data *pdata |
| 729 | = pdev->dev.platform_data; |
| 730 | |
| 731 | if (!pdata) { |
| 732 | pr_err("missing platform data\n"); |
| 733 | return -EINVAL; |
| 734 | } |
| 735 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 736 | "PM8921_BMS_CCADC_EOC"); |
| 737 | if (!res) { |
| 738 | pr_err("failed to get irq\n"); |
| 739 | return -EINVAL; |
| 740 | } |
| 741 | |
| 742 | chip = kzalloc(sizeof(struct pm8xxx_ccadc_chip), GFP_KERNEL); |
| 743 | if (!chip) { |
| 744 | pr_err("Cannot allocate pm_bms_chip\n"); |
| 745 | return -ENOMEM; |
| 746 | } |
| 747 | chip->dev = &pdev->dev; |
| 748 | chip->revision = pm8xxx_get_revision(chip->dev->parent); |
| 749 | chip->eoc_irq = res->start; |
Xiaozhe Shi | d69c91e | 2012-11-06 10:00:38 -0800 | [diff] [blame] | 750 | chip->r_sense_uohm = pdata->r_sense_uohm; |
David Keitel | 3c37882 | 2012-06-07 13:43:22 -0700 | [diff] [blame] | 751 | chip->calib_delay_ms = pdata->calib_delay_ms; |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 752 | chip->batt_temp_channel = pdata->ccadc_cdata.batt_temp_channel; |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 753 | chip->periodic_wakeup = pdata->periodic_wakeup; |
Abhijeet Dharmapurikar | f154bfc | 2013-01-23 00:24:58 -0800 | [diff] [blame] | 754 | mutex_init(&chip->calib_mutex); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 755 | |
Abhijeet Dharmapurikar | 97d4090 | 2011-11-30 12:12:51 -0800 | [diff] [blame] | 756 | calib_ccadc_read_offset_and_gain(chip, |
| 757 | &chip->ccadc_gain_uv, |
| 758 | &chip->ccadc_offset); |
Anirudh Ghayal | 1d11747 | 2012-12-04 12:41:53 +0530 | [diff] [blame] | 759 | irq_set_status_flags(chip->eoc_irq, IRQ_NOAUTOEN); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 760 | rc = request_irq(chip->eoc_irq, |
| 761 | pm8921_bms_ccadc_eoc_handler, IRQF_TRIGGER_RISING, |
| 762 | "bms_eoc_ccadc", chip); |
| 763 | if (rc) { |
| 764 | pr_err("failed to request %d irq rc= %d\n", chip->eoc_irq, rc); |
| 765 | goto free_chip; |
| 766 | } |
David Keitel | 3c37882 | 2012-06-07 13:43:22 -0700 | [diff] [blame] | 767 | |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 768 | platform_set_drvdata(pdev, chip); |
| 769 | the_chip = chip; |
Abhijeet Dharmapurikar | fbefde2 | 2012-07-12 14:39:13 -0700 | [diff] [blame] | 770 | INIT_DELAYED_WORK(&chip->calib_ccadc_work, calibrate_ccadc_work); |
| 771 | schedule_delayed_work(&chip->calib_ccadc_work, 0); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 772 | |
| 773 | create_debugfs_entries(chip); |
| 774 | |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 775 | return 0; |
| 776 | |
| 777 | free_chip: |
Abhijeet Dharmapurikar | f154bfc | 2013-01-23 00:24:58 -0800 | [diff] [blame] | 778 | mutex_destroy(&chip->calib_mutex); |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 779 | kfree(chip); |
| 780 | return rc; |
| 781 | } |
| 782 | |
| 783 | static int __devexit pm8xxx_ccadc_remove(struct platform_device *pdev) |
| 784 | { |
| 785 | struct pm8xxx_ccadc_chip *chip = platform_get_drvdata(pdev); |
| 786 | |
| 787 | debugfs_remove_recursive(chip->dent); |
| 788 | the_chip = NULL; |
| 789 | kfree(chip); |
| 790 | return 0; |
| 791 | } |
| 792 | |
Anirudh Ghayal | 44fde2a | 2013-05-10 11:02:53 +0530 | [diff] [blame] | 793 | static int pm8xxx_ccadc_suspend(struct device *dev) |
| 794 | { |
| 795 | struct pm8xxx_ccadc_chip *chip = dev_get_drvdata(dev); |
| 796 | |
| 797 | cancel_delayed_work_sync(&chip->calib_ccadc_work); |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 802 | #define CCADC_CALIB_TEMP_THRESH 20 |
| 803 | static int pm8xxx_ccadc_resume(struct device *dev) |
| 804 | { |
| 805 | int rc, batt_temp, delta_temp; |
| 806 | unsigned long current_time_sec; |
| 807 | unsigned long time_since_last_calib; |
| 808 | |
| 809 | rc = get_batt_temp(the_chip, &batt_temp); |
| 810 | if (rc) { |
| 811 | pr_err("unable to get batt_temp: %d\n", rc); |
| 812 | return 0; |
| 813 | } |
| 814 | rc = get_current_time(¤t_time_sec); |
| 815 | if (rc) { |
| 816 | pr_err("unable to get current time: %d\n", rc); |
| 817 | return 0; |
| 818 | } |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 819 | |
| 820 | if (the_chip->periodic_wakeup) { |
| 821 | pm8xxx_calib_ccadc_quick(); |
| 822 | return 0; |
| 823 | } |
| 824 | |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 825 | if (current_time_sec > the_chip->last_calib_time) { |
| 826 | time_since_last_calib = current_time_sec - |
| 827 | the_chip->last_calib_time; |
| 828 | delta_temp = abs(batt_temp - the_chip->last_calib_temp); |
| 829 | pr_debug("time since last calib: %lu, delta_temp = %d\n", |
| 830 | time_since_last_calib, delta_temp); |
| 831 | if (time_since_last_calib >= the_chip->calib_delay_ms/1000 |
| 832 | || delta_temp > CCADC_CALIB_TEMP_THRESH) { |
| 833 | the_chip->last_calib_time = current_time_sec; |
| 834 | the_chip->last_calib_temp = batt_temp; |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 835 | schedule_delayed_work(&the_chip->calib_ccadc_work, 0); |
Anirudh Ghayal | 44fde2a | 2013-05-10 11:02:53 +0530 | [diff] [blame] | 836 | } else { |
| 837 | schedule_delayed_work(&the_chip->calib_ccadc_work, |
| 838 | msecs_to_jiffies(the_chip->calib_delay_ms - |
| 839 | (time_since_last_calib * 1000))); |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 840 | } |
| 841 | } |
Abhijeet Dharmapurikar | 5389ebc | 2013-03-10 06:55:43 -0700 | [diff] [blame] | 842 | |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static const struct dev_pm_ops pm8xxx_ccadc_pm_ops = { |
Anirudh Ghayal | 44fde2a | 2013-05-10 11:02:53 +0530 | [diff] [blame] | 847 | .suspend = pm8xxx_ccadc_suspend, |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 848 | .resume = pm8xxx_ccadc_resume, |
| 849 | }; |
| 850 | |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 851 | static struct platform_driver pm8xxx_ccadc_driver = { |
| 852 | .probe = pm8xxx_ccadc_probe, |
| 853 | .remove = __devexit_p(pm8xxx_ccadc_remove), |
| 854 | .driver = { |
| 855 | .name = PM8XXX_CCADC_DEV_NAME, |
| 856 | .owner = THIS_MODULE, |
Xiaozhe Shi | d035f6a | 2012-12-18 16:16:33 -0800 | [diff] [blame] | 857 | .pm = &pm8xxx_ccadc_pm_ops, |
Abhijeet Dharmapurikar | 82d9398 | 2011-11-09 15:52:25 -0800 | [diff] [blame] | 858 | }, |
| 859 | }; |
| 860 | |
| 861 | static int __init pm8xxx_ccadc_init(void) |
| 862 | { |
| 863 | return platform_driver_register(&pm8xxx_ccadc_driver); |
| 864 | } |
| 865 | |
| 866 | static void __exit pm8xxx_ccadc_exit(void) |
| 867 | { |
| 868 | platform_driver_unregister(&pm8xxx_ccadc_driver); |
| 869 | } |
| 870 | |
| 871 | module_init(pm8xxx_ccadc_init); |
| 872 | module_exit(pm8xxx_ccadc_exit); |
| 873 | |
| 874 | MODULE_LICENSE("GPL v2"); |
| 875 | MODULE_DESCRIPTION("PMIC8XXX ccadc driver"); |
| 876 | MODULE_VERSION("1.0"); |
| 877 | MODULE_ALIAS("platform:" PM8XXX_CCADC_DEV_NAME); |