Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [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/kernel.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/hwmon.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/debugfs.h> |
| 26 | #include <linux/spmi.h> |
| 27 | #include <linux/of_irq.h> |
| 28 | #include <linux/wakelock.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/completion.h> |
| 31 | #include <linux/hwmon-sysfs.h> |
| 32 | #include <linux/qpnp/qpnp-adc.h> |
| 33 | #include <linux/platform_device.h> |
| 34 | |
| 35 | /* QPNP IADC register definition */ |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 36 | #define QPNP_IADC_REVISION1 0x0 |
| 37 | #define QPNP_IADC_REVISION2 0x1 |
| 38 | #define QPNP_IADC_REVISION3 0x2 |
| 39 | #define QPNP_IADC_REVISION4 0x3 |
| 40 | #define QPNP_IADC_PERPH_TYPE 0x4 |
| 41 | #define QPNP_IADC_PERH_SUBTYPE 0x5 |
| 42 | |
| 43 | #define QPNP_IADC_SUPPORTED_REVISION2 1 |
| 44 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 45 | #define QPNP_STATUS1 0x8 |
| 46 | #define QPNP_STATUS1_OP_MODE 4 |
| 47 | #define QPNP_STATUS1_MULTI_MEAS_EN BIT(3) |
| 48 | #define QPNP_STATUS1_MEAS_INTERVAL_EN_STS BIT(2) |
| 49 | #define QPNP_STATUS1_REQ_STS BIT(1) |
| 50 | #define QPNP_STATUS1_EOC BIT(0) |
| 51 | #define QPNP_STATUS2 0x9 |
| 52 | #define QPNP_STATUS2_CONV_SEQ_STATE_SHIFT 4 |
| 53 | #define QPNP_STATUS2_FIFO_NOT_EMPTY_FLAG BIT(1) |
| 54 | #define QPNP_STATUS2_CONV_SEQ_TIMEOUT_STS BIT(0) |
| 55 | #define QPNP_CONV_TIMEOUT_ERR 2 |
| 56 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 57 | #define QPNP_IADC_MODE_CTL 0x40 |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 58 | #define QPNP_OP_MODE_SHIFT 4 |
| 59 | #define QPNP_USE_BMS_DATA BIT(4) |
| 60 | #define QPNP_VADC_SYNCH_EN BIT(2) |
| 61 | #define QPNP_OFFSET_RMV_EN BIT(1) |
| 62 | #define QPNP_ADC_TRIM_EN BIT(0) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 63 | #define QPNP_IADC_EN_CTL1 0x46 |
| 64 | #define QPNP_IADC_ADC_EN BIT(7) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 65 | #define QPNP_ADC_CH_SEL_CTL 0x48 |
| 66 | #define QPNP_ADC_DIG_PARAM 0x50 |
| 67 | #define QPNP_ADC_CLK_SEL_MASK 0x3 |
| 68 | #define QPNP_ADC_DEC_RATIO_SEL_MASK 0xc |
| 69 | #define QPNP_ADC_DIG_DEC_RATIO_SEL_SHIFT 2 |
| 70 | |
| 71 | #define QPNP_HW_SETTLE_DELAY 0x51 |
| 72 | #define QPNP_CONV_REQ 0x52 |
| 73 | #define QPNP_CONV_REQ_SET BIT(7) |
| 74 | #define QPNP_CONV_SEQ_CTL 0x54 |
| 75 | #define QPNP_CONV_SEQ_HOLDOFF_SHIFT 4 |
| 76 | #define QPNP_CONV_SEQ_TRIG_CTL 0x55 |
| 77 | #define QPNP_FAST_AVG_CTL 0x5a |
| 78 | |
| 79 | #define QPNP_M0_LOW_THR_LSB 0x5c |
| 80 | #define QPNP_M0_LOW_THR_MSB 0x5d |
| 81 | #define QPNP_M0_HIGH_THR_LSB 0x5e |
| 82 | #define QPNP_M0_HIGH_THR_MSB 0x5f |
| 83 | #define QPNP_M1_LOW_THR_LSB 0x69 |
| 84 | #define QPNP_M1_LOW_THR_MSB 0x6a |
| 85 | #define QPNP_M1_HIGH_THR_LSB 0x6b |
| 86 | #define QPNP_M1_HIGH_THR_MSB 0x6c |
| 87 | |
| 88 | #define QPNP_DATA0 0x60 |
| 89 | #define QPNP_DATA1 0x61 |
| 90 | #define QPNP_CONV_TIMEOUT_ERR 2 |
| 91 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 92 | #define QPNP_IADC_SEC_ACCESS 0xD0 |
| 93 | #define QPNP_IADC_SEC_ACCESS_DATA 0xA5 |
| 94 | #define QPNP_IADC_MSB_OFFSET 0xF2 |
| 95 | #define QPNP_IADC_LSB_OFFSET 0xF3 |
| 96 | #define QPNP_IADC_NOMINAL_RSENSE 0xF4 |
| 97 | #define QPNP_IADC_ATE_GAIN_CALIB_OFFSET 0xF5 |
| 98 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 99 | #define QPNP_IADC_ADC_CH_SEL_CTL 0x48 |
| 100 | #define QPNP_IADC_ADC_CHX_SEL_SHIFT 3 |
| 101 | |
| 102 | #define QPNP_IADC_ADC_DIG_PARAM 0x50 |
| 103 | #define QPNP_IADC_CLK_SEL_SHIFT 1 |
| 104 | #define QPNP_IADC_DEC_RATIO_SEL 3 |
| 105 | |
| 106 | #define QPNP_IADC_CONV_REQUEST 0x52 |
| 107 | #define QPNP_IADC_CONV_REQ BIT(7) |
| 108 | |
| 109 | #define QPNP_IADC_DATA0 0x60 |
| 110 | #define QPNP_IADC_DATA1 0x61 |
| 111 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 112 | #define QPNP_ADC_CONV_TIME_MIN 8000 |
| 113 | #define QPNP_ADC_CONV_TIME_MAX 8200 |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 114 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 115 | #define QPNP_ADC_GAIN_NV 17857 |
| 116 | #define QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL 0 |
| 117 | #define QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR 10000000 |
| 118 | #define QPNP_IADC_NANO_VOLTS_FACTOR 1000000000 |
| 119 | #define QPNP_IADC_CALIB_SECONDS 300000 |
| 120 | #define QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT 15625 |
| 121 | #define QPNP_IADC_DIE_TEMP_CALIB_OFFSET 5000 |
| 122 | |
| 123 | #define QPNP_RAW_CODE_16_BIT_MSB_MASK 0xff00 |
| 124 | #define QPNP_RAW_CODE_16_BIT_LSB_MASK 0xff |
| 125 | #define QPNP_BIT_SHIFT_8 8 |
| 126 | #define QPNP_RSENSE_MSB_SIGN_CHECK 0x80 |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 127 | #define QPNP_ADC_COMPLETION_TIMEOUT HZ |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 128 | #define QPNP_IADC_ERR_CHK_RATELIMIT 3 |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 129 | |
| 130 | struct qpnp_iadc_drv { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 131 | struct qpnp_adc_drv *adc; |
| 132 | int32_t rsense; |
| 133 | struct device *iadc_hwmon; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 134 | bool iadc_initialized; |
| 135 | int64_t die_temp_calib_offset; |
| 136 | struct delayed_work iadc_work; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 137 | struct mutex iadc_vadc_lock; |
| 138 | bool iadc_mode_sel; |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 139 | uint32_t iadc_err_cnt; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 140 | struct sensor_device_attribute sens_attr[0]; |
| 141 | }; |
| 142 | |
| 143 | struct qpnp_iadc_drv *qpnp_iadc; |
| 144 | |
| 145 | static int32_t qpnp_iadc_read_reg(uint32_t reg, u8 *data) |
| 146 | { |
| 147 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 148 | int rc; |
| 149 | |
| 150 | rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 151 | (iadc->adc->offset + reg), data, 1); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 152 | if (rc < 0) { |
| 153 | pr_err("qpnp iadc read reg %d failed with %d\n", reg, rc); |
| 154 | return rc; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int32_t qpnp_iadc_write_reg(uint32_t reg, u8 data) |
| 161 | { |
| 162 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 163 | int rc; |
| 164 | u8 *buf; |
| 165 | |
| 166 | buf = &data; |
| 167 | rc = spmi_ext_register_writel(iadc->adc->spmi->ctrl, iadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 168 | (iadc->adc->offset + reg), buf, 1); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 169 | if (rc < 0) { |
| 170 | pr_err("qpnp iadc write reg %d failed with %d\n", reg, rc); |
| 171 | return rc; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 177 | static void trigger_iadc_completion(struct work_struct *work) |
| 178 | { |
| 179 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 180 | |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 181 | if (!iadc || !iadc->iadc_initialized) |
| 182 | return; |
| 183 | |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 184 | complete(&iadc->adc->adc_rslt_completion); |
| 185 | |
| 186 | return; |
| 187 | } |
| 188 | DECLARE_WORK(trigger_iadc_completion_work, trigger_iadc_completion); |
| 189 | |
| 190 | static irqreturn_t qpnp_iadc_isr(int irq, void *dev_id) |
| 191 | { |
| 192 | schedule_work(&trigger_iadc_completion_work); |
| 193 | |
| 194 | return IRQ_HANDLED; |
| 195 | } |
| 196 | |
| 197 | static int32_t qpnp_iadc_enable(bool state) |
| 198 | { |
| 199 | int rc = 0; |
| 200 | u8 data = 0; |
| 201 | |
| 202 | data = QPNP_IADC_ADC_EN; |
| 203 | if (state) { |
| 204 | rc = qpnp_iadc_write_reg(QPNP_IADC_EN_CTL1, |
| 205 | data); |
| 206 | if (rc < 0) { |
| 207 | pr_err("IADC enable failed\n"); |
| 208 | return rc; |
| 209 | } |
| 210 | } else { |
| 211 | rc = qpnp_iadc_write_reg(QPNP_IADC_EN_CTL1, |
| 212 | (~data & QPNP_IADC_ADC_EN)); |
| 213 | if (rc < 0) { |
| 214 | pr_err("IADC disable failed\n"); |
| 215 | return rc; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 222 | static int32_t qpnp_iadc_status_debug(void) |
| 223 | { |
| 224 | int rc = 0; |
| 225 | u8 mode = 0, status1 = 0, chan = 0, dig = 0, en = 0; |
| 226 | |
| 227 | rc = qpnp_iadc_read_reg(QPNP_IADC_MODE_CTL, &mode); |
| 228 | if (rc < 0) { |
| 229 | pr_err("mode ctl register read failed with %d\n", rc); |
| 230 | return rc; |
| 231 | } |
| 232 | |
| 233 | rc = qpnp_iadc_read_reg(QPNP_ADC_DIG_PARAM, &dig); |
| 234 | if (rc < 0) { |
| 235 | pr_err("digital param read failed with %d\n", rc); |
| 236 | return rc; |
| 237 | } |
| 238 | |
| 239 | rc = qpnp_iadc_read_reg(QPNP_IADC_ADC_CH_SEL_CTL, &chan); |
| 240 | if (rc < 0) { |
| 241 | pr_err("channel read failed with %d\n", rc); |
| 242 | return rc; |
| 243 | } |
| 244 | |
| 245 | rc = qpnp_iadc_read_reg(QPNP_STATUS1, &status1); |
| 246 | if (rc < 0) { |
| 247 | pr_err("status1 read failed with %d\n", rc); |
| 248 | return rc; |
| 249 | } |
| 250 | |
| 251 | rc = qpnp_iadc_read_reg(QPNP_IADC_EN_CTL1, &en); |
| 252 | if (rc < 0) { |
| 253 | pr_err("en read failed with %d\n", rc); |
| 254 | return rc; |
| 255 | } |
| 256 | |
| 257 | pr_err("EOC not set with status:%x, dig:%x, ch:%x, mode:%x, en:%x\n", |
| 258 | status1, dig, chan, mode, en); |
| 259 | |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 260 | rc = qpnp_iadc_enable(false); |
| 261 | if (rc < 0) { |
| 262 | pr_err("IADC disable failed with %d\n", rc); |
| 263 | return rc; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 269 | static int32_t qpnp_iadc_read_conversion_result(uint16_t *data) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 270 | { |
| 271 | uint8_t rslt_lsb, rslt_msb; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 272 | uint16_t rslt; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 273 | int32_t rc; |
| 274 | |
| 275 | rc = qpnp_iadc_read_reg(QPNP_IADC_DATA0, &rslt_lsb); |
| 276 | if (rc < 0) { |
| 277 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 278 | return rc; |
| 279 | } |
| 280 | |
| 281 | rc = qpnp_iadc_read_reg(QPNP_IADC_DATA1, &rslt_msb); |
| 282 | if (rc < 0) { |
| 283 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 284 | return rc; |
| 285 | } |
| 286 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 287 | rslt = (rslt_msb << 8) | rslt_lsb; |
| 288 | *data = rslt; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 289 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 290 | rc = qpnp_iadc_enable(false); |
| 291 | if (rc) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 292 | return rc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 293 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int32_t qpnp_iadc_configure(enum qpnp_iadc_channels channel, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 298 | uint16_t *raw_code, uint32_t mode_sel) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 299 | { |
| 300 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 301 | u8 qpnp_iadc_mode_reg = 0, qpnp_iadc_ch_sel_reg = 0; |
| 302 | u8 qpnp_iadc_conv_req = 0, qpnp_iadc_dig_param_reg = 0; |
| 303 | int32_t rc = 0; |
| 304 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 305 | qpnp_iadc_ch_sel_reg = channel; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 306 | |
| 307 | qpnp_iadc_dig_param_reg |= iadc->adc->amux_prop->decimation << |
| 308 | QPNP_IADC_DEC_RATIO_SEL; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 309 | if (iadc->iadc_mode_sel) |
| 310 | qpnp_iadc_mode_reg |= (QPNP_ADC_TRIM_EN | QPNP_VADC_SYNCH_EN); |
| 311 | else |
| 312 | qpnp_iadc_mode_reg |= QPNP_ADC_TRIM_EN; |
| 313 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 314 | qpnp_iadc_conv_req = QPNP_IADC_CONV_REQ; |
| 315 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 316 | rc = qpnp_iadc_write_reg(QPNP_IADC_MODE_CTL, qpnp_iadc_mode_reg); |
| 317 | if (rc) { |
| 318 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 319 | return rc; |
| 320 | } |
| 321 | |
| 322 | rc = qpnp_iadc_write_reg(QPNP_IADC_ADC_CH_SEL_CTL, |
| 323 | qpnp_iadc_ch_sel_reg); |
| 324 | if (rc) { |
| 325 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 326 | return rc; |
| 327 | } |
| 328 | |
| 329 | rc = qpnp_iadc_write_reg(QPNP_ADC_DIG_PARAM, |
| 330 | qpnp_iadc_dig_param_reg); |
| 331 | if (rc) { |
| 332 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 333 | return rc; |
| 334 | } |
| 335 | |
| 336 | rc = qpnp_iadc_write_reg(QPNP_HW_SETTLE_DELAY, |
| 337 | iadc->adc->amux_prop->hw_settle_time); |
| 338 | if (rc < 0) { |
| 339 | pr_err("qpnp adc configure error for hw settling time setup\n"); |
| 340 | return rc; |
| 341 | } |
| 342 | |
| 343 | rc = qpnp_iadc_write_reg(QPNP_FAST_AVG_CTL, |
| 344 | iadc->adc->amux_prop->fast_avg_setup); |
| 345 | if (rc < 0) { |
| 346 | pr_err("qpnp adc fast averaging configure error\n"); |
| 347 | return rc; |
| 348 | } |
| 349 | |
Siddartha Mohanadoss | 3f219c4 | 2013-04-02 11:01:28 -0700 | [diff] [blame^] | 350 | INIT_COMPLETION(iadc->adc->adc_rslt_completion); |
| 351 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 352 | rc = qpnp_iadc_enable(true); |
| 353 | if (rc) |
| 354 | return rc; |
| 355 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 356 | rc = qpnp_iadc_write_reg(QPNP_CONV_REQ, qpnp_iadc_conv_req); |
| 357 | if (rc) { |
| 358 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 359 | return rc; |
| 360 | } |
| 361 | |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 362 | rc = wait_for_completion_timeout(&iadc->adc->adc_rslt_completion, |
| 363 | QPNP_ADC_COMPLETION_TIMEOUT); |
| 364 | if (!rc) { |
| 365 | u8 status1 = 0; |
| 366 | rc = qpnp_iadc_read_reg(QPNP_STATUS1, &status1); |
| 367 | if (rc < 0) |
| 368 | return rc; |
| 369 | status1 &= (QPNP_STATUS1_REQ_STS | QPNP_STATUS1_EOC); |
| 370 | if (status1 == QPNP_STATUS1_EOC) |
| 371 | pr_debug("End of conversion status set\n"); |
| 372 | else { |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 373 | rc = qpnp_iadc_status_debug(); |
| 374 | if (rc < 0) { |
| 375 | pr_err("status1 read failed with %d\n", rc); |
| 376 | return rc; |
| 377 | } |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 378 | return -EINVAL; |
| 379 | } |
| 380 | } |
| 381 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 382 | rc = qpnp_iadc_read_conversion_result(raw_code); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 383 | if (rc) { |
| 384 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 385 | return rc; |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 391 | static int32_t qpnp_convert_raw_offset_voltage(void) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 392 | { |
| 393 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 394 | uint32_t num = 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 395 | |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 396 | if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) { |
| 397 | pr_err("raw offset errors! raw_gain:0x%x and raw_offset:0x%x\n", |
| 398 | iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw); |
| 399 | return -EINVAL; |
| 400 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 401 | |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 402 | iadc->adc->calib.offset_uv = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 403 | |
| 404 | num = iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw; |
| 405 | |
| 406 | iadc->adc->calib.gain_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 407 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 408 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 409 | pr_debug("gain_uv:%d offset_uv:%d\n", |
| 410 | iadc->adc->calib.gain_uv, iadc->adc->calib.offset_uv); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Siddartha Mohanadoss | 0667392 | 2013-03-27 11:14:19 -0700 | [diff] [blame] | 414 | int32_t qpnp_iadc_calibrate_for_trim(void) |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 415 | { |
| 416 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 417 | uint8_t rslt_lsb, rslt_msb; |
| 418 | int32_t rc = 0; |
| 419 | uint16_t raw_data; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 420 | uint32_t mode_sel = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 421 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 422 | mutex_lock(&iadc->adc->adc_lock); |
| 423 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 424 | rc = qpnp_iadc_configure(GAIN_CALIBRATION_17P857MV, |
| 425 | &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 426 | if (rc < 0) { |
| 427 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 428 | goto fail; |
| 429 | } |
| 430 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 431 | iadc->adc->calib.gain_raw = raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 432 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 433 | rc = qpnp_iadc_configure(OFFSET_CALIBRATION_CSP2_CSN2, |
| 434 | &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 435 | if (rc < 0) { |
| 436 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 437 | goto fail; |
| 438 | } |
| 439 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 440 | iadc->adc->calib.offset_raw = raw_data; |
| 441 | if (rc < 0) { |
| 442 | pr_err("qpnp adc offset/gain calculation failed\n"); |
| 443 | goto fail; |
| 444 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 445 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 446 | pr_debug("raw gain:0x%x, raw offset:0x%x\n", |
| 447 | iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw); |
| 448 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 449 | rc = qpnp_convert_raw_offset_voltage(); |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 450 | if (rc < 0) { |
| 451 | pr_err("qpnp raw_voltage conversion failed\n"); |
| 452 | goto fail; |
| 453 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 454 | |
| 455 | rslt_msb = (raw_data & QPNP_RAW_CODE_16_BIT_MSB_MASK) >> |
| 456 | QPNP_BIT_SHIFT_8; |
| 457 | rslt_lsb = raw_data & QPNP_RAW_CODE_16_BIT_LSB_MASK; |
| 458 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 459 | pr_debug("trim values:lsb:0x%x and msb:0x%x\n", rslt_lsb, rslt_msb); |
| 460 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 461 | rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS, |
| 462 | QPNP_IADC_SEC_ACCESS_DATA); |
| 463 | if (rc < 0) { |
| 464 | pr_err("qpnp iadc configure error for sec access\n"); |
| 465 | goto fail; |
| 466 | } |
| 467 | |
| 468 | rc = qpnp_iadc_write_reg(QPNP_IADC_MSB_OFFSET, |
| 469 | rslt_msb); |
| 470 | if (rc < 0) { |
| 471 | pr_err("qpnp iadc configure error for MSB write\n"); |
| 472 | goto fail; |
| 473 | } |
| 474 | |
| 475 | rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS, |
| 476 | QPNP_IADC_SEC_ACCESS_DATA); |
| 477 | if (rc < 0) { |
| 478 | pr_err("qpnp iadc configure error for sec access\n"); |
| 479 | goto fail; |
| 480 | } |
| 481 | |
| 482 | rc = qpnp_iadc_write_reg(QPNP_IADC_LSB_OFFSET, |
| 483 | rslt_lsb); |
| 484 | if (rc < 0) { |
| 485 | pr_err("qpnp iadc configure error for LSB write\n"); |
| 486 | goto fail; |
| 487 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 488 | fail: |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 489 | mutex_unlock(&iadc->adc->adc_lock); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 490 | return rc; |
| 491 | } |
Siddartha Mohanadoss | 0667392 | 2013-03-27 11:14:19 -0700 | [diff] [blame] | 492 | EXPORT_SYMBOL(qpnp_iadc_calibrate_for_trim); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 493 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 494 | static void qpnp_iadc_work(struct work_struct *work) |
| 495 | { |
| 496 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 497 | int rc = 0; |
| 498 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 499 | rc = qpnp_iadc_calibrate_for_trim(); |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 500 | if (rc) { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 501 | pr_err("periodic IADC calibration failed\n"); |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 502 | iadc->iadc_err_cnt++; |
| 503 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 504 | |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 505 | if (iadc->iadc_err_cnt < QPNP_IADC_ERR_CHK_RATELIMIT) |
| 506 | schedule_delayed_work(&iadc->iadc_work, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 507 | round_jiffies_relative(msecs_to_jiffies |
| 508 | (QPNP_IADC_CALIB_SECONDS))); |
| 509 | |
| 510 | return; |
| 511 | } |
| 512 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 513 | static int32_t qpnp_iadc_version_check(void) |
| 514 | { |
| 515 | uint8_t revision; |
| 516 | int rc; |
| 517 | |
| 518 | rc = qpnp_iadc_read_reg(QPNP_IADC_REVISION2, &revision); |
| 519 | if (rc < 0) { |
| 520 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 521 | return rc; |
| 522 | } |
| 523 | |
| 524 | if (revision < QPNP_IADC_SUPPORTED_REVISION2) { |
| 525 | pr_err("IADC Version not supported\n"); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | int32_t qpnp_iadc_is_ready(void) |
| 533 | { |
| 534 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 535 | |
| 536 | if (!iadc || !iadc->iadc_initialized) |
| 537 | return -EPROBE_DEFER; |
| 538 | else |
| 539 | return 0; |
| 540 | } |
| 541 | EXPORT_SYMBOL(qpnp_iadc_is_ready); |
| 542 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 543 | int32_t qpnp_iadc_get_rsense(int32_t *rsense) |
| 544 | { |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 545 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 546 | uint8_t rslt_rsense; |
| 547 | int32_t rc, sign_bit = 0; |
| 548 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 549 | if (!iadc || !iadc->iadc_initialized) |
| 550 | return -EPROBE_DEFER; |
| 551 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 552 | rc = qpnp_iadc_read_reg(QPNP_IADC_NOMINAL_RSENSE, &rslt_rsense); |
| 553 | if (rc < 0) { |
| 554 | pr_err("qpnp adc rsense read failed with %d\n", rc); |
| 555 | return rc; |
| 556 | } |
| 557 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 558 | pr_debug("rsense:0%x\n", rslt_rsense); |
| 559 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 560 | if (rslt_rsense & QPNP_RSENSE_MSB_SIGN_CHECK) |
| 561 | sign_bit = 1; |
| 562 | |
| 563 | rslt_rsense &= ~QPNP_RSENSE_MSB_SIGN_CHECK; |
| 564 | |
| 565 | if (sign_bit) |
| 566 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR - |
| 567 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 568 | else |
| 569 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR + |
| 570 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 571 | |
| 572 | return rc; |
| 573 | } |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 574 | EXPORT_SYMBOL(qpnp_iadc_get_rsense); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 575 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 576 | static int32_t qpnp_check_pmic_temp(void) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 577 | { |
| 578 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 579 | struct qpnp_vadc_result result_pmic_therm; |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 580 | int rc = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 581 | |
| 582 | rc = qpnp_vadc_read(DIE_TEMP, &result_pmic_therm); |
| 583 | if (rc < 0) |
| 584 | return rc; |
| 585 | |
| 586 | if (((uint64_t) (result_pmic_therm.physical - |
| 587 | iadc->die_temp_calib_offset)) |
| 588 | > QPNP_IADC_DIE_TEMP_CALIB_OFFSET) { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 589 | rc = qpnp_iadc_calibrate_for_trim(); |
| 590 | if (rc) |
| 591 | pr_err("periodic IADC calibration failed\n"); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 594 | return rc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | int32_t qpnp_iadc_read(enum qpnp_iadc_channels channel, |
| 598 | struct qpnp_iadc_result *result) |
| 599 | { |
| 600 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 601 | int32_t rc, rsense_n_ohms, sign = 0, num, mode_sel = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 602 | int64_t result_current; |
| 603 | uint16_t raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 604 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 605 | if (!iadc || !iadc->iadc_initialized) |
| 606 | return -EPROBE_DEFER; |
| 607 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 608 | if (!iadc->iadc_mode_sel) { |
| 609 | rc = qpnp_check_pmic_temp(); |
| 610 | if (rc) { |
| 611 | pr_err("Error checking pmic therm temp\n"); |
| 612 | return rc; |
| 613 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 616 | mutex_lock(&iadc->adc->adc_lock); |
| 617 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 618 | rc = qpnp_iadc_configure(channel, &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 619 | if (rc < 0) { |
| 620 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 621 | goto fail; |
| 622 | } |
| 623 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 624 | rc = qpnp_iadc_get_rsense(&rsense_n_ohms); |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 625 | pr_debug("current raw:0%x and rsense:%d\n", |
| 626 | raw_data, rsense_n_ohms); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 627 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 628 | num = raw_data - iadc->adc->calib.offset_raw; |
| 629 | if (num < 0) { |
| 630 | sign = 1; |
| 631 | num = -num; |
| 632 | } |
| 633 | |
| 634 | result->result_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 635 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 636 | result_current = result->result_uv; |
| 637 | result_current *= QPNP_IADC_NANO_VOLTS_FACTOR; |
| 638 | do_div(result_current, rsense_n_ohms); |
| 639 | |
| 640 | if (sign) { |
| 641 | result->result_uv = -result->result_uv; |
| 642 | result_current = -result_current; |
| 643 | } |
| 644 | |
| 645 | result->result_ua = (int32_t) result_current; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 646 | fail: |
| 647 | mutex_unlock(&iadc->adc->adc_lock); |
| 648 | |
| 649 | return rc; |
| 650 | } |
| 651 | EXPORT_SYMBOL(qpnp_iadc_read); |
| 652 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 653 | int32_t qpnp_iadc_get_gain_and_offset(struct qpnp_iadc_calib *result) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 654 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 655 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 656 | int rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 657 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 658 | if (!iadc || !iadc->iadc_initialized) |
| 659 | return -EPROBE_DEFER; |
| 660 | |
| 661 | rc = qpnp_check_pmic_temp(); |
| 662 | if (rc) { |
| 663 | pr_err("Error checking pmic therm temp\n"); |
| 664 | return rc; |
| 665 | } |
| 666 | |
| 667 | mutex_lock(&iadc->adc->adc_lock); |
| 668 | result->gain_raw = iadc->adc->calib.gain_raw; |
| 669 | result->ideal_gain_nv = QPNP_ADC_GAIN_NV; |
| 670 | result->gain_uv = iadc->adc->calib.gain_uv; |
| 671 | result->offset_raw = iadc->adc->calib.offset_raw; |
| 672 | result->ideal_offset_uv = |
| 673 | QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL; |
| 674 | result->offset_uv = iadc->adc->calib.offset_uv; |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 675 | pr_debug("raw gain:0%x, raw offset:0%x\n", |
| 676 | result->gain_raw, result->offset_raw); |
| 677 | pr_debug("gain_uv:%d offset_uv:%d\n", |
| 678 | result->gain_uv, result->offset_uv); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 679 | mutex_unlock(&iadc->adc->adc_lock); |
| 680 | |
| 681 | return 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 682 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 683 | EXPORT_SYMBOL(qpnp_iadc_get_gain_and_offset); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 684 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 685 | int32_t qpnp_iadc_vadc_sync_read( |
| 686 | enum qpnp_iadc_channels i_channel, struct qpnp_iadc_result *i_result, |
| 687 | enum qpnp_vadc_channels v_channel, struct qpnp_vadc_result *v_result) |
| 688 | { |
| 689 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 690 | int rc = 0; |
| 691 | |
| 692 | if (!iadc || !iadc->iadc_initialized) |
| 693 | return -EPROBE_DEFER; |
| 694 | |
| 695 | mutex_lock(&iadc->iadc_vadc_lock); |
| 696 | |
| 697 | rc = qpnp_check_pmic_temp(); |
| 698 | if (rc) { |
| 699 | pr_err("PMIC die temp check failed\n"); |
| 700 | goto fail; |
| 701 | } |
| 702 | |
| 703 | iadc->iadc_mode_sel = true; |
| 704 | |
| 705 | rc = qpnp_vadc_iadc_sync_request(v_channel); |
| 706 | if (rc) { |
| 707 | pr_err("Configuring VADC failed\n"); |
| 708 | goto fail; |
| 709 | } |
| 710 | |
| 711 | rc = qpnp_iadc_read(i_channel, i_result); |
| 712 | if (rc) |
| 713 | pr_err("Configuring IADC failed\n"); |
| 714 | /* Intentional fall through to release VADC */ |
| 715 | |
| 716 | rc = qpnp_vadc_iadc_sync_complete_request(v_channel, |
| 717 | v_result); |
| 718 | if (rc) |
| 719 | pr_err("Releasing VADC failed\n"); |
| 720 | fail: |
| 721 | iadc->iadc_mode_sel = false; |
| 722 | |
| 723 | mutex_unlock(&iadc->iadc_vadc_lock); |
| 724 | |
| 725 | return rc; |
| 726 | } |
| 727 | EXPORT_SYMBOL(qpnp_iadc_vadc_sync_read); |
| 728 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 729 | static ssize_t qpnp_iadc_show(struct device *dev, |
| 730 | struct device_attribute *devattr, char *buf) |
| 731 | { |
| 732 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 733 | struct qpnp_iadc_result result; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 734 | int rc = -1; |
| 735 | |
| 736 | rc = qpnp_iadc_read(attr->index, &result); |
| 737 | |
| 738 | if (rc) |
| 739 | return 0; |
| 740 | |
| 741 | return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 742 | "Result:%d\n", result.result_ua); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static struct sensor_device_attribute qpnp_adc_attr = |
| 746 | SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0); |
| 747 | |
| 748 | static int32_t qpnp_iadc_init_hwmon(struct spmi_device *spmi) |
| 749 | { |
| 750 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 751 | struct device_node *child; |
| 752 | struct device_node *node = spmi->dev.of_node; |
| 753 | int rc = 0, i = 0, channel; |
| 754 | |
| 755 | for_each_child_of_node(node, child) { |
| 756 | channel = iadc->adc->adc_channels[i].channel_num; |
| 757 | qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num; |
| 758 | qpnp_adc_attr.dev_attr.attr.name = |
| 759 | iadc->adc->adc_channels[i].name; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 760 | memcpy(&iadc->sens_attr[i], &qpnp_adc_attr, |
| 761 | sizeof(qpnp_adc_attr)); |
Stephen Boyd | 8a5c4e4 | 2012-10-30 11:07:22 -0700 | [diff] [blame] | 762 | sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 763 | rc = device_create_file(&spmi->dev, |
| 764 | &iadc->sens_attr[i].dev_attr); |
| 765 | if (rc) { |
| 766 | dev_err(&spmi->dev, |
| 767 | "device_create_file failed for dev %s\n", |
| 768 | iadc->adc->adc_channels[i].name); |
| 769 | goto hwmon_err_sens; |
| 770 | } |
| 771 | i++; |
| 772 | } |
| 773 | |
| 774 | return 0; |
| 775 | hwmon_err_sens: |
| 776 | pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc); |
| 777 | return rc; |
| 778 | } |
| 779 | |
| 780 | static int __devinit qpnp_iadc_probe(struct spmi_device *spmi) |
| 781 | { |
| 782 | struct qpnp_iadc_drv *iadc; |
| 783 | struct qpnp_adc_drv *adc_qpnp; |
| 784 | struct device_node *node = spmi->dev.of_node; |
| 785 | struct device_node *child; |
| 786 | int rc, count_adc_channel_list = 0; |
| 787 | |
| 788 | if (!node) |
| 789 | return -EINVAL; |
| 790 | |
| 791 | if (qpnp_iadc) { |
| 792 | pr_err("IADC already in use\n"); |
| 793 | return -EBUSY; |
| 794 | } |
| 795 | |
| 796 | for_each_child_of_node(node, child) |
| 797 | count_adc_channel_list++; |
| 798 | |
| 799 | if (!count_adc_channel_list) { |
| 800 | pr_err("No channel listing\n"); |
| 801 | return -EINVAL; |
| 802 | } |
| 803 | |
| 804 | iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_drv) + |
| 805 | (sizeof(struct sensor_device_attribute) * |
| 806 | count_adc_channel_list), GFP_KERNEL); |
| 807 | if (!iadc) { |
| 808 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 809 | return -ENOMEM; |
| 810 | } |
| 811 | |
| 812 | adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv), |
| 813 | GFP_KERNEL); |
| 814 | if (!adc_qpnp) { |
| 815 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 816 | rc = -ENOMEM; |
| 817 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | iadc->adc = adc_qpnp; |
| 821 | |
| 822 | rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc); |
| 823 | if (rc) { |
| 824 | dev_err(&spmi->dev, "failed to read device tree\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 825 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | rc = of_property_read_u32(node, "qcom,rsense", |
| 829 | &iadc->rsense); |
| 830 | if (rc) { |
| 831 | pr_err("Invalid rsens reference property\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 832 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame] | 835 | rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq_eoc, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 836 | qpnp_iadc_isr, |
| 837 | IRQF_TRIGGER_RISING, "qpnp_iadc_interrupt", iadc); |
| 838 | if (rc) { |
| 839 | dev_err(&spmi->dev, "failed to request adc irq\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 840 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 841 | } else |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame] | 842 | enable_irq_wake(iadc->adc->adc_irq_eoc); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 843 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 844 | dev_set_drvdata(&spmi->dev, iadc); |
| 845 | qpnp_iadc = iadc; |
| 846 | |
| 847 | rc = qpnp_iadc_init_hwmon(spmi); |
| 848 | if (rc) { |
| 849 | dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 850 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 851 | } |
| 852 | iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev); |
| 853 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 854 | rc = qpnp_iadc_version_check(); |
| 855 | if (rc) { |
| 856 | dev_err(&spmi->dev, "IADC version not supported\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 857 | goto fail; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 860 | mutex_init(&iadc->iadc_vadc_lock); |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 861 | INIT_DELAYED_WORK(&iadc->iadc_work, qpnp_iadc_work); |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 862 | iadc->iadc_err_cnt = 0; |
| 863 | iadc->iadc_initialized = true; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 864 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 865 | rc = qpnp_iadc_calibrate_for_trim(); |
| 866 | if (rc) |
| 867 | dev_err(&spmi->dev, "failed to calibrate for USR trim\n"); |
| 868 | schedule_delayed_work(&iadc->iadc_work, |
| 869 | round_jiffies_relative(msecs_to_jiffies |
| 870 | (QPNP_IADC_CALIB_SECONDS))); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 871 | return 0; |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 872 | fail: |
Siddartha Mohanadoss | 32019b5 | 2012-12-23 17:05:45 -0800 | [diff] [blame] | 873 | qpnp_iadc = NULL; |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 874 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static int __devexit qpnp_iadc_remove(struct spmi_device *spmi) |
| 878 | { |
| 879 | struct qpnp_iadc_drv *iadc = dev_get_drvdata(&spmi->dev); |
| 880 | struct device_node *node = spmi->dev.of_node; |
| 881 | struct device_node *child; |
| 882 | int i = 0; |
| 883 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 884 | cancel_delayed_work(&iadc->iadc_work); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 885 | mutex_destroy(&iadc->iadc_vadc_lock); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 886 | for_each_child_of_node(node, child) { |
| 887 | device_remove_file(&spmi->dev, |
| 888 | &iadc->sens_attr[i].dev_attr); |
| 889 | i++; |
| 890 | } |
| 891 | dev_set_drvdata(&spmi->dev, NULL); |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static const struct of_device_id qpnp_iadc_match_table[] = { |
| 897 | { .compatible = "qcom,qpnp-iadc", |
| 898 | }, |
| 899 | {} |
| 900 | }; |
| 901 | |
| 902 | static struct spmi_driver qpnp_iadc_driver = { |
| 903 | .driver = { |
| 904 | .name = "qcom,qpnp-iadc", |
| 905 | .of_match_table = qpnp_iadc_match_table, |
| 906 | }, |
| 907 | .probe = qpnp_iadc_probe, |
| 908 | .remove = qpnp_iadc_remove, |
| 909 | }; |
| 910 | |
| 911 | static int __init qpnp_iadc_init(void) |
| 912 | { |
| 913 | return spmi_driver_register(&qpnp_iadc_driver); |
| 914 | } |
| 915 | module_init(qpnp_iadc_init); |
| 916 | |
| 917 | static void __exit qpnp_iadc_exit(void) |
| 918 | { |
| 919 | spmi_driver_unregister(&qpnp_iadc_driver); |
| 920 | } |
| 921 | module_exit(qpnp_iadc_exit); |
| 922 | |
| 923 | MODULE_DESCRIPTION("QPNP PMIC current ADC driver"); |
| 924 | MODULE_LICENSE("GPL v2"); |