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 | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 350 | rc = qpnp_iadc_enable(true); |
| 351 | if (rc) |
| 352 | return rc; |
| 353 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 354 | rc = qpnp_iadc_write_reg(QPNP_CONV_REQ, qpnp_iadc_conv_req); |
| 355 | if (rc) { |
| 356 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 357 | return rc; |
| 358 | } |
| 359 | |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 360 | rc = wait_for_completion_timeout(&iadc->adc->adc_rslt_completion, |
| 361 | QPNP_ADC_COMPLETION_TIMEOUT); |
| 362 | if (!rc) { |
| 363 | u8 status1 = 0; |
| 364 | rc = qpnp_iadc_read_reg(QPNP_STATUS1, &status1); |
| 365 | if (rc < 0) |
| 366 | return rc; |
| 367 | status1 &= (QPNP_STATUS1_REQ_STS | QPNP_STATUS1_EOC); |
| 368 | if (status1 == QPNP_STATUS1_EOC) |
| 369 | pr_debug("End of conversion status set\n"); |
| 370 | else { |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 371 | rc = qpnp_iadc_status_debug(); |
| 372 | if (rc < 0) { |
| 373 | pr_err("status1 read failed with %d\n", rc); |
| 374 | return rc; |
| 375 | } |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 376 | return -EINVAL; |
| 377 | } |
| 378 | } |
| 379 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 380 | rc = qpnp_iadc_read_conversion_result(raw_code); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 381 | if (rc) { |
| 382 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 383 | return rc; |
| 384 | } |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 389 | static int32_t qpnp_convert_raw_offset_voltage(void) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 390 | { |
| 391 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 392 | uint32_t num = 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 393 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 394 | num = iadc->adc->calib.offset_raw - iadc->adc->calib.offset_raw; |
| 395 | |
| 396 | iadc->adc->calib.offset_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 397 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 398 | |
| 399 | num = iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw; |
| 400 | |
| 401 | iadc->adc->calib.gain_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 402 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int32_t qpnp_iadc_calibrate_for_trim(void) |
| 408 | { |
| 409 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 410 | uint8_t rslt_lsb, rslt_msb; |
| 411 | int32_t rc = 0; |
| 412 | uint16_t raw_data; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 413 | uint32_t mode_sel = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 414 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame^] | 415 | mutex_lock(&iadc->adc->adc_lock); |
| 416 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 417 | rc = qpnp_iadc_configure(GAIN_CALIBRATION_17P857MV, |
| 418 | &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 419 | if (rc < 0) { |
| 420 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 421 | goto fail; |
| 422 | } |
| 423 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 424 | iadc->adc->calib.gain_raw = raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 425 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 426 | rc = qpnp_iadc_configure(OFFSET_CALIBRATION_CSP2_CSN2, |
| 427 | &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 428 | if (rc < 0) { |
| 429 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 430 | goto fail; |
| 431 | } |
| 432 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 433 | iadc->adc->calib.offset_raw = raw_data; |
| 434 | if (rc < 0) { |
| 435 | pr_err("qpnp adc offset/gain calculation failed\n"); |
| 436 | goto fail; |
| 437 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 438 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 439 | rc = qpnp_convert_raw_offset_voltage(); |
| 440 | |
| 441 | rslt_msb = (raw_data & QPNP_RAW_CODE_16_BIT_MSB_MASK) >> |
| 442 | QPNP_BIT_SHIFT_8; |
| 443 | rslt_lsb = raw_data & QPNP_RAW_CODE_16_BIT_LSB_MASK; |
| 444 | |
| 445 | rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS, |
| 446 | QPNP_IADC_SEC_ACCESS_DATA); |
| 447 | if (rc < 0) { |
| 448 | pr_err("qpnp iadc configure error for sec access\n"); |
| 449 | goto fail; |
| 450 | } |
| 451 | |
| 452 | rc = qpnp_iadc_write_reg(QPNP_IADC_MSB_OFFSET, |
| 453 | rslt_msb); |
| 454 | if (rc < 0) { |
| 455 | pr_err("qpnp iadc configure error for MSB write\n"); |
| 456 | goto fail; |
| 457 | } |
| 458 | |
| 459 | rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS, |
| 460 | QPNP_IADC_SEC_ACCESS_DATA); |
| 461 | if (rc < 0) { |
| 462 | pr_err("qpnp iadc configure error for sec access\n"); |
| 463 | goto fail; |
| 464 | } |
| 465 | |
| 466 | rc = qpnp_iadc_write_reg(QPNP_IADC_LSB_OFFSET, |
| 467 | rslt_lsb); |
| 468 | if (rc < 0) { |
| 469 | pr_err("qpnp iadc configure error for LSB write\n"); |
| 470 | goto fail; |
| 471 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 472 | fail: |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame^] | 473 | mutex_unlock(&iadc->adc->adc_lock); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 474 | return rc; |
| 475 | } |
| 476 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 477 | static void qpnp_iadc_work(struct work_struct *work) |
| 478 | { |
| 479 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 480 | int rc = 0; |
| 481 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 482 | rc = qpnp_iadc_calibrate_for_trim(); |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 483 | if (rc) { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 484 | pr_err("periodic IADC calibration failed\n"); |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 485 | iadc->iadc_err_cnt++; |
| 486 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 487 | |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 488 | if (iadc->iadc_err_cnt < QPNP_IADC_ERR_CHK_RATELIMIT) |
| 489 | schedule_delayed_work(&iadc->iadc_work, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 490 | round_jiffies_relative(msecs_to_jiffies |
| 491 | (QPNP_IADC_CALIB_SECONDS))); |
| 492 | |
| 493 | return; |
| 494 | } |
| 495 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 496 | static int32_t qpnp_iadc_version_check(void) |
| 497 | { |
| 498 | uint8_t revision; |
| 499 | int rc; |
| 500 | |
| 501 | rc = qpnp_iadc_read_reg(QPNP_IADC_REVISION2, &revision); |
| 502 | if (rc < 0) { |
| 503 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 504 | return rc; |
| 505 | } |
| 506 | |
| 507 | if (revision < QPNP_IADC_SUPPORTED_REVISION2) { |
| 508 | pr_err("IADC Version not supported\n"); |
| 509 | return -EINVAL; |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | int32_t qpnp_iadc_is_ready(void) |
| 516 | { |
| 517 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 518 | |
| 519 | if (!iadc || !iadc->iadc_initialized) |
| 520 | return -EPROBE_DEFER; |
| 521 | else |
| 522 | return 0; |
| 523 | } |
| 524 | EXPORT_SYMBOL(qpnp_iadc_is_ready); |
| 525 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 526 | int32_t qpnp_iadc_get_rsense(int32_t *rsense) |
| 527 | { |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame^] | 528 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 529 | uint8_t rslt_rsense; |
| 530 | int32_t rc, sign_bit = 0; |
| 531 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame^] | 532 | if (!iadc || !iadc->iadc_initialized) |
| 533 | return -EPROBE_DEFER; |
| 534 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 535 | rc = qpnp_iadc_read_reg(QPNP_IADC_NOMINAL_RSENSE, &rslt_rsense); |
| 536 | if (rc < 0) { |
| 537 | pr_err("qpnp adc rsense read failed with %d\n", rc); |
| 538 | return rc; |
| 539 | } |
| 540 | |
| 541 | if (rslt_rsense & QPNP_RSENSE_MSB_SIGN_CHECK) |
| 542 | sign_bit = 1; |
| 543 | |
| 544 | rslt_rsense &= ~QPNP_RSENSE_MSB_SIGN_CHECK; |
| 545 | |
| 546 | if (sign_bit) |
| 547 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR - |
| 548 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 549 | else |
| 550 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR + |
| 551 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 552 | |
| 553 | return rc; |
| 554 | } |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 555 | EXPORT_SYMBOL(qpnp_iadc_get_rsense); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 556 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame^] | 557 | static int32_t qpnp_check_pmic_temp(void) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 558 | { |
| 559 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 560 | struct qpnp_vadc_result result_pmic_therm; |
| 561 | int rc; |
| 562 | |
| 563 | rc = qpnp_vadc_read(DIE_TEMP, &result_pmic_therm); |
| 564 | if (rc < 0) |
| 565 | return rc; |
| 566 | |
| 567 | if (((uint64_t) (result_pmic_therm.physical - |
| 568 | iadc->die_temp_calib_offset)) |
| 569 | > QPNP_IADC_DIE_TEMP_CALIB_OFFSET) { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 570 | rc = qpnp_iadc_calibrate_for_trim(); |
| 571 | if (rc) |
| 572 | pr_err("periodic IADC calibration failed\n"); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | int32_t qpnp_iadc_read(enum qpnp_iadc_channels channel, |
| 579 | struct qpnp_iadc_result *result) |
| 580 | { |
| 581 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 582 | int32_t rc, rsense_n_ohms, sign = 0, num, mode_sel = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 583 | int64_t result_current; |
| 584 | uint16_t raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 585 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 586 | if (!iadc || !iadc->iadc_initialized) |
| 587 | return -EPROBE_DEFER; |
| 588 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 589 | if (!iadc->iadc_mode_sel) { |
| 590 | rc = qpnp_check_pmic_temp(); |
| 591 | if (rc) { |
| 592 | pr_err("Error checking pmic therm temp\n"); |
| 593 | return rc; |
| 594 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 597 | mutex_lock(&iadc->adc->adc_lock); |
| 598 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 599 | rc = qpnp_iadc_configure(channel, &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 600 | if (rc < 0) { |
| 601 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 602 | goto fail; |
| 603 | } |
| 604 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 605 | rc = qpnp_iadc_get_rsense(&rsense_n_ohms); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 606 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 607 | num = raw_data - iadc->adc->calib.offset_raw; |
| 608 | if (num < 0) { |
| 609 | sign = 1; |
| 610 | num = -num; |
| 611 | } |
| 612 | |
| 613 | result->result_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 614 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 615 | result_current = result->result_uv; |
| 616 | result_current *= QPNP_IADC_NANO_VOLTS_FACTOR; |
| 617 | do_div(result_current, rsense_n_ohms); |
| 618 | |
| 619 | if (sign) { |
| 620 | result->result_uv = -result->result_uv; |
| 621 | result_current = -result_current; |
| 622 | } |
| 623 | |
| 624 | result->result_ua = (int32_t) result_current; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 625 | fail: |
| 626 | mutex_unlock(&iadc->adc->adc_lock); |
| 627 | |
| 628 | return rc; |
| 629 | } |
| 630 | EXPORT_SYMBOL(qpnp_iadc_read); |
| 631 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 632 | 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] | 633 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 634 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 635 | int rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 636 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 637 | if (!iadc || !iadc->iadc_initialized) |
| 638 | return -EPROBE_DEFER; |
| 639 | |
| 640 | rc = qpnp_check_pmic_temp(); |
| 641 | if (rc) { |
| 642 | pr_err("Error checking pmic therm temp\n"); |
| 643 | return rc; |
| 644 | } |
| 645 | |
| 646 | mutex_lock(&iadc->adc->adc_lock); |
| 647 | result->gain_raw = iadc->adc->calib.gain_raw; |
| 648 | result->ideal_gain_nv = QPNP_ADC_GAIN_NV; |
| 649 | result->gain_uv = iadc->adc->calib.gain_uv; |
| 650 | result->offset_raw = iadc->adc->calib.offset_raw; |
| 651 | result->ideal_offset_uv = |
| 652 | QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL; |
| 653 | result->offset_uv = iadc->adc->calib.offset_uv; |
| 654 | mutex_unlock(&iadc->adc->adc_lock); |
| 655 | |
| 656 | return 0; |
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 | EXPORT_SYMBOL(qpnp_iadc_get_gain_and_offset); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 659 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 660 | int32_t qpnp_iadc_vadc_sync_read( |
| 661 | enum qpnp_iadc_channels i_channel, struct qpnp_iadc_result *i_result, |
| 662 | enum qpnp_vadc_channels v_channel, struct qpnp_vadc_result *v_result) |
| 663 | { |
| 664 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 665 | int rc = 0; |
| 666 | |
| 667 | if (!iadc || !iadc->iadc_initialized) |
| 668 | return -EPROBE_DEFER; |
| 669 | |
| 670 | mutex_lock(&iadc->iadc_vadc_lock); |
| 671 | |
| 672 | rc = qpnp_check_pmic_temp(); |
| 673 | if (rc) { |
| 674 | pr_err("PMIC die temp check failed\n"); |
| 675 | goto fail; |
| 676 | } |
| 677 | |
| 678 | iadc->iadc_mode_sel = true; |
| 679 | |
| 680 | rc = qpnp_vadc_iadc_sync_request(v_channel); |
| 681 | if (rc) { |
| 682 | pr_err("Configuring VADC failed\n"); |
| 683 | goto fail; |
| 684 | } |
| 685 | |
| 686 | rc = qpnp_iadc_read(i_channel, i_result); |
| 687 | if (rc) |
| 688 | pr_err("Configuring IADC failed\n"); |
| 689 | /* Intentional fall through to release VADC */ |
| 690 | |
| 691 | rc = qpnp_vadc_iadc_sync_complete_request(v_channel, |
| 692 | v_result); |
| 693 | if (rc) |
| 694 | pr_err("Releasing VADC failed\n"); |
| 695 | fail: |
| 696 | iadc->iadc_mode_sel = false; |
| 697 | |
| 698 | mutex_unlock(&iadc->iadc_vadc_lock); |
| 699 | |
| 700 | return rc; |
| 701 | } |
| 702 | EXPORT_SYMBOL(qpnp_iadc_vadc_sync_read); |
| 703 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 704 | static ssize_t qpnp_iadc_show(struct device *dev, |
| 705 | struct device_attribute *devattr, char *buf) |
| 706 | { |
| 707 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 708 | struct qpnp_iadc_result result; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 709 | int rc = -1; |
| 710 | |
| 711 | rc = qpnp_iadc_read(attr->index, &result); |
| 712 | |
| 713 | if (rc) |
| 714 | return 0; |
| 715 | |
| 716 | return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 717 | "Result:%d\n", result.result_ua); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static struct sensor_device_attribute qpnp_adc_attr = |
| 721 | SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0); |
| 722 | |
| 723 | static int32_t qpnp_iadc_init_hwmon(struct spmi_device *spmi) |
| 724 | { |
| 725 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 726 | struct device_node *child; |
| 727 | struct device_node *node = spmi->dev.of_node; |
| 728 | int rc = 0, i = 0, channel; |
| 729 | |
| 730 | for_each_child_of_node(node, child) { |
| 731 | channel = iadc->adc->adc_channels[i].channel_num; |
| 732 | qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num; |
| 733 | qpnp_adc_attr.dev_attr.attr.name = |
| 734 | iadc->adc->adc_channels[i].name; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 735 | memcpy(&iadc->sens_attr[i], &qpnp_adc_attr, |
| 736 | sizeof(qpnp_adc_attr)); |
Stephen Boyd | 8a5c4e4 | 2012-10-30 11:07:22 -0700 | [diff] [blame] | 737 | sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 738 | rc = device_create_file(&spmi->dev, |
| 739 | &iadc->sens_attr[i].dev_attr); |
| 740 | if (rc) { |
| 741 | dev_err(&spmi->dev, |
| 742 | "device_create_file failed for dev %s\n", |
| 743 | iadc->adc->adc_channels[i].name); |
| 744 | goto hwmon_err_sens; |
| 745 | } |
| 746 | i++; |
| 747 | } |
| 748 | |
| 749 | return 0; |
| 750 | hwmon_err_sens: |
| 751 | pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc); |
| 752 | return rc; |
| 753 | } |
| 754 | |
| 755 | static int __devinit qpnp_iadc_probe(struct spmi_device *spmi) |
| 756 | { |
| 757 | struct qpnp_iadc_drv *iadc; |
| 758 | struct qpnp_adc_drv *adc_qpnp; |
| 759 | struct device_node *node = spmi->dev.of_node; |
| 760 | struct device_node *child; |
| 761 | int rc, count_adc_channel_list = 0; |
| 762 | |
| 763 | if (!node) |
| 764 | return -EINVAL; |
| 765 | |
| 766 | if (qpnp_iadc) { |
| 767 | pr_err("IADC already in use\n"); |
| 768 | return -EBUSY; |
| 769 | } |
| 770 | |
| 771 | for_each_child_of_node(node, child) |
| 772 | count_adc_channel_list++; |
| 773 | |
| 774 | if (!count_adc_channel_list) { |
| 775 | pr_err("No channel listing\n"); |
| 776 | return -EINVAL; |
| 777 | } |
| 778 | |
| 779 | iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_drv) + |
| 780 | (sizeof(struct sensor_device_attribute) * |
| 781 | count_adc_channel_list), GFP_KERNEL); |
| 782 | if (!iadc) { |
| 783 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 784 | return -ENOMEM; |
| 785 | } |
| 786 | |
| 787 | adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv), |
| 788 | GFP_KERNEL); |
| 789 | if (!adc_qpnp) { |
| 790 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 791 | rc = -ENOMEM; |
| 792 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | iadc->adc = adc_qpnp; |
| 796 | |
| 797 | rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc); |
| 798 | if (rc) { |
| 799 | dev_err(&spmi->dev, "failed to read device tree\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 800 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | rc = of_property_read_u32(node, "qcom,rsense", |
| 804 | &iadc->rsense); |
| 805 | if (rc) { |
| 806 | pr_err("Invalid rsens reference property\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 807 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame] | 810 | rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq_eoc, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 811 | qpnp_iadc_isr, |
| 812 | IRQF_TRIGGER_RISING, "qpnp_iadc_interrupt", iadc); |
| 813 | if (rc) { |
| 814 | dev_err(&spmi->dev, "failed to request adc irq\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 815 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 816 | } else |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame] | 817 | enable_irq_wake(iadc->adc->adc_irq_eoc); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 818 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 819 | dev_set_drvdata(&spmi->dev, iadc); |
| 820 | qpnp_iadc = iadc; |
| 821 | |
| 822 | rc = qpnp_iadc_init_hwmon(spmi); |
| 823 | if (rc) { |
| 824 | dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\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 | iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev); |
| 828 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 829 | rc = qpnp_iadc_version_check(); |
| 830 | if (rc) { |
| 831 | dev_err(&spmi->dev, "IADC version not supported\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 832 | goto fail; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 835 | mutex_init(&iadc->iadc_vadc_lock); |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame^] | 836 | INIT_DELAYED_WORK(&iadc->iadc_work, qpnp_iadc_work); |
Siddartha Mohanadoss | 12a15ea | 2013-02-05 19:13:41 -0800 | [diff] [blame] | 837 | iadc->iadc_err_cnt = 0; |
| 838 | iadc->iadc_initialized = true; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 839 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame^] | 840 | rc = qpnp_iadc_calibrate_for_trim(); |
| 841 | if (rc) |
| 842 | dev_err(&spmi->dev, "failed to calibrate for USR trim\n"); |
| 843 | schedule_delayed_work(&iadc->iadc_work, |
| 844 | round_jiffies_relative(msecs_to_jiffies |
| 845 | (QPNP_IADC_CALIB_SECONDS))); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 846 | return 0; |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 847 | fail: |
Siddartha Mohanadoss | 32019b5 | 2012-12-23 17:05:45 -0800 | [diff] [blame] | 848 | qpnp_iadc = NULL; |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 849 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | static int __devexit qpnp_iadc_remove(struct spmi_device *spmi) |
| 853 | { |
| 854 | struct qpnp_iadc_drv *iadc = dev_get_drvdata(&spmi->dev); |
| 855 | struct device_node *node = spmi->dev.of_node; |
| 856 | struct device_node *child; |
| 857 | int i = 0; |
| 858 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame^] | 859 | cancel_delayed_work(&iadc->iadc_work); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 860 | mutex_destroy(&iadc->iadc_vadc_lock); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 861 | for_each_child_of_node(node, child) { |
| 862 | device_remove_file(&spmi->dev, |
| 863 | &iadc->sens_attr[i].dev_attr); |
| 864 | i++; |
| 865 | } |
| 866 | dev_set_drvdata(&spmi->dev, NULL); |
| 867 | |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static const struct of_device_id qpnp_iadc_match_table[] = { |
| 872 | { .compatible = "qcom,qpnp-iadc", |
| 873 | }, |
| 874 | {} |
| 875 | }; |
| 876 | |
| 877 | static struct spmi_driver qpnp_iadc_driver = { |
| 878 | .driver = { |
| 879 | .name = "qcom,qpnp-iadc", |
| 880 | .of_match_table = qpnp_iadc_match_table, |
| 881 | }, |
| 882 | .probe = qpnp_iadc_probe, |
| 883 | .remove = qpnp_iadc_remove, |
| 884 | }; |
| 885 | |
| 886 | static int __init qpnp_iadc_init(void) |
| 887 | { |
| 888 | return spmi_driver_register(&qpnp_iadc_driver); |
| 889 | } |
| 890 | module_init(qpnp_iadc_init); |
| 891 | |
| 892 | static void __exit qpnp_iadc_exit(void) |
| 893 | { |
| 894 | spmi_driver_unregister(&qpnp_iadc_driver); |
| 895 | } |
| 896 | module_exit(qpnp_iadc_exit); |
| 897 | |
| 898 | MODULE_DESCRIPTION("QPNP PMIC current ADC driver"); |
| 899 | MODULE_LICENSE("GPL v2"); |