Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2012, 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 | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 128 | |
| 129 | struct qpnp_iadc_drv { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 130 | struct qpnp_adc_drv *adc; |
| 131 | int32_t rsense; |
| 132 | struct device *iadc_hwmon; |
| 133 | bool iadc_init_calib; |
| 134 | bool iadc_initialized; |
| 135 | int64_t die_temp_calib_offset; |
| 136 | struct delayed_work iadc_work; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 137 | struct sensor_device_attribute sens_attr[0]; |
| 138 | }; |
| 139 | |
| 140 | struct qpnp_iadc_drv *qpnp_iadc; |
| 141 | |
| 142 | static int32_t qpnp_iadc_read_reg(uint32_t reg, u8 *data) |
| 143 | { |
| 144 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 145 | int rc; |
| 146 | |
| 147 | rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 148 | (iadc->adc->offset + reg), data, 1); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 149 | if (rc < 0) { |
| 150 | pr_err("qpnp iadc read reg %d failed with %d\n", reg, rc); |
| 151 | return rc; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int32_t qpnp_iadc_write_reg(uint32_t reg, u8 data) |
| 158 | { |
| 159 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 160 | int rc; |
| 161 | u8 *buf; |
| 162 | |
| 163 | buf = &data; |
| 164 | rc = spmi_ext_register_writel(iadc->adc->spmi->ctrl, iadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 165 | (iadc->adc->offset + reg), buf, 1); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 166 | if (rc < 0) { |
| 167 | pr_err("qpnp iadc write reg %d failed with %d\n", reg, rc); |
| 168 | return rc; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 174 | static void trigger_iadc_completion(struct work_struct *work) |
| 175 | { |
| 176 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 177 | |
| 178 | complete(&iadc->adc->adc_rslt_completion); |
| 179 | |
| 180 | return; |
| 181 | } |
| 182 | DECLARE_WORK(trigger_iadc_completion_work, trigger_iadc_completion); |
| 183 | |
| 184 | static irqreturn_t qpnp_iadc_isr(int irq, void *dev_id) |
| 185 | { |
| 186 | schedule_work(&trigger_iadc_completion_work); |
| 187 | |
| 188 | return IRQ_HANDLED; |
| 189 | } |
| 190 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 191 | static int32_t qpnp_iadc_enable(bool state) |
| 192 | { |
| 193 | int rc = 0; |
| 194 | u8 data = 0; |
| 195 | |
| 196 | data = QPNP_IADC_ADC_EN; |
| 197 | if (state) { |
| 198 | rc = qpnp_iadc_write_reg(QPNP_IADC_EN_CTL1, |
| 199 | data); |
| 200 | if (rc < 0) { |
| 201 | pr_err("IADC enable failed\n"); |
| 202 | return rc; |
| 203 | } |
| 204 | } else { |
| 205 | rc = qpnp_iadc_write_reg(QPNP_IADC_EN_CTL1, |
| 206 | (~data & QPNP_IADC_ADC_EN)); |
| 207 | if (rc < 0) { |
| 208 | pr_err("IADC disable failed\n"); |
| 209 | return rc; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 216 | static int32_t qpnp_iadc_read_conversion_result(uint16_t *data) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 217 | { |
| 218 | uint8_t rslt_lsb, rslt_msb; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 219 | uint16_t rslt; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 220 | int32_t rc; |
| 221 | |
| 222 | rc = qpnp_iadc_read_reg(QPNP_IADC_DATA0, &rslt_lsb); |
| 223 | if (rc < 0) { |
| 224 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 225 | return rc; |
| 226 | } |
| 227 | |
| 228 | rc = qpnp_iadc_read_reg(QPNP_IADC_DATA1, &rslt_msb); |
| 229 | if (rc < 0) { |
| 230 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 231 | return rc; |
| 232 | } |
| 233 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 234 | rslt = (rslt_msb << 8) | rslt_lsb; |
| 235 | *data = rslt; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 236 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 237 | rc = qpnp_iadc_enable(false); |
| 238 | if (rc) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 239 | return rc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 240 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static int32_t qpnp_iadc_configure(enum qpnp_iadc_channels channel, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 245 | uint16_t *raw_code) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 246 | { |
| 247 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 248 | u8 qpnp_iadc_mode_reg = 0, qpnp_iadc_ch_sel_reg = 0; |
| 249 | u8 qpnp_iadc_conv_req = 0, qpnp_iadc_dig_param_reg = 0; |
| 250 | int32_t rc = 0; |
| 251 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 252 | qpnp_iadc_ch_sel_reg = channel; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 253 | |
| 254 | qpnp_iadc_dig_param_reg |= iadc->adc->amux_prop->decimation << |
| 255 | QPNP_IADC_DEC_RATIO_SEL; |
| 256 | |
| 257 | qpnp_iadc_conv_req = QPNP_IADC_CONV_REQ; |
| 258 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 259 | rc = qpnp_iadc_write_reg(QPNP_IADC_MODE_CTL, qpnp_iadc_mode_reg); |
| 260 | if (rc) { |
| 261 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 262 | return rc; |
| 263 | } |
| 264 | |
| 265 | rc = qpnp_iadc_write_reg(QPNP_IADC_ADC_CH_SEL_CTL, |
| 266 | qpnp_iadc_ch_sel_reg); |
| 267 | if (rc) { |
| 268 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 269 | return rc; |
| 270 | } |
| 271 | |
| 272 | rc = qpnp_iadc_write_reg(QPNP_ADC_DIG_PARAM, |
| 273 | qpnp_iadc_dig_param_reg); |
| 274 | if (rc) { |
| 275 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 276 | return rc; |
| 277 | } |
| 278 | |
| 279 | rc = qpnp_iadc_write_reg(QPNP_HW_SETTLE_DELAY, |
| 280 | iadc->adc->amux_prop->hw_settle_time); |
| 281 | if (rc < 0) { |
| 282 | pr_err("qpnp adc configure error for hw settling time setup\n"); |
| 283 | return rc; |
| 284 | } |
| 285 | |
| 286 | rc = qpnp_iadc_write_reg(QPNP_FAST_AVG_CTL, |
| 287 | iadc->adc->amux_prop->fast_avg_setup); |
| 288 | if (rc < 0) { |
| 289 | pr_err("qpnp adc fast averaging configure error\n"); |
| 290 | return rc; |
| 291 | } |
| 292 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 293 | rc = qpnp_iadc_enable(true); |
| 294 | if (rc) |
| 295 | return rc; |
| 296 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 297 | rc = qpnp_iadc_write_reg(QPNP_CONV_REQ, qpnp_iadc_conv_req); |
| 298 | if (rc) { |
| 299 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 300 | return rc; |
| 301 | } |
| 302 | |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 303 | rc = wait_for_completion_timeout(&iadc->adc->adc_rslt_completion, |
| 304 | QPNP_ADC_COMPLETION_TIMEOUT); |
| 305 | if (!rc) { |
| 306 | u8 status1 = 0; |
| 307 | rc = qpnp_iadc_read_reg(QPNP_STATUS1, &status1); |
| 308 | if (rc < 0) |
| 309 | return rc; |
| 310 | status1 &= (QPNP_STATUS1_REQ_STS | QPNP_STATUS1_EOC); |
| 311 | if (status1 == QPNP_STATUS1_EOC) |
| 312 | pr_debug("End of conversion status set\n"); |
| 313 | else { |
| 314 | pr_err("EOC interrupt not received\n"); |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | } |
| 318 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 319 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 320 | rc = qpnp_iadc_read_conversion_result(raw_code); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 321 | if (rc) { |
| 322 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 323 | return rc; |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 329 | static int32_t qpnp_convert_raw_offset_voltage(void) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 330 | { |
| 331 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 332 | uint32_t num = 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 333 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 334 | num = iadc->adc->calib.offset_raw - iadc->adc->calib.offset_raw; |
| 335 | |
| 336 | iadc->adc->calib.offset_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 337 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 338 | |
| 339 | num = iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw; |
| 340 | |
| 341 | iadc->adc->calib.gain_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 342 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static int32_t qpnp_iadc_calibrate_for_trim(void) |
| 348 | { |
| 349 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 350 | uint8_t rslt_lsb, rslt_msb; |
| 351 | int32_t rc = 0; |
| 352 | uint16_t raw_data; |
| 353 | |
| 354 | rc = qpnp_iadc_configure(GAIN_CALIBRATION_17P857MV, &raw_data); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 355 | if (rc < 0) { |
| 356 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 357 | goto fail; |
| 358 | } |
| 359 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 360 | iadc->adc->calib.gain_raw = raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 361 | |
| 362 | rc = qpnp_iadc_configure(OFFSET_CALIBRATION_SHORT_CADC_LEADS, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 363 | &raw_data); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 364 | if (rc < 0) { |
| 365 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 366 | goto fail; |
| 367 | } |
| 368 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 369 | iadc->adc->calib.offset_raw = raw_data; |
| 370 | if (rc < 0) { |
| 371 | pr_err("qpnp adc offset/gain calculation failed\n"); |
| 372 | goto fail; |
| 373 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 374 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 375 | rc = qpnp_convert_raw_offset_voltage(); |
| 376 | |
| 377 | rslt_msb = (raw_data & QPNP_RAW_CODE_16_BIT_MSB_MASK) >> |
| 378 | QPNP_BIT_SHIFT_8; |
| 379 | rslt_lsb = raw_data & QPNP_RAW_CODE_16_BIT_LSB_MASK; |
| 380 | |
| 381 | rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS, |
| 382 | QPNP_IADC_SEC_ACCESS_DATA); |
| 383 | if (rc < 0) { |
| 384 | pr_err("qpnp iadc configure error for sec access\n"); |
| 385 | goto fail; |
| 386 | } |
| 387 | |
| 388 | rc = qpnp_iadc_write_reg(QPNP_IADC_MSB_OFFSET, |
| 389 | rslt_msb); |
| 390 | if (rc < 0) { |
| 391 | pr_err("qpnp iadc configure error for MSB write\n"); |
| 392 | goto fail; |
| 393 | } |
| 394 | |
| 395 | rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS, |
| 396 | QPNP_IADC_SEC_ACCESS_DATA); |
| 397 | if (rc < 0) { |
| 398 | pr_err("qpnp iadc configure error for sec access\n"); |
| 399 | goto fail; |
| 400 | } |
| 401 | |
| 402 | rc = qpnp_iadc_write_reg(QPNP_IADC_LSB_OFFSET, |
| 403 | rslt_lsb); |
| 404 | if (rc < 0) { |
| 405 | pr_err("qpnp iadc configure error for LSB write\n"); |
| 406 | goto fail; |
| 407 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 408 | fail: |
| 409 | return rc; |
| 410 | } |
| 411 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 412 | static void qpnp_iadc_work(struct work_struct *work) |
| 413 | { |
| 414 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 415 | int rc = 0; |
| 416 | |
| 417 | mutex_lock(&iadc->adc->adc_lock); |
| 418 | |
| 419 | rc = qpnp_iadc_calibrate_for_trim(); |
| 420 | if (rc) |
| 421 | pr_err("periodic IADC calibration failed\n"); |
| 422 | |
| 423 | mutex_unlock(&iadc->adc->adc_lock); |
| 424 | |
| 425 | schedule_delayed_work(&iadc->iadc_work, |
| 426 | round_jiffies_relative(msecs_to_jiffies |
| 427 | (QPNP_IADC_CALIB_SECONDS))); |
| 428 | |
| 429 | return; |
| 430 | } |
| 431 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 432 | static int32_t qpnp_iadc_version_check(void) |
| 433 | { |
| 434 | uint8_t revision; |
| 435 | int rc; |
| 436 | |
| 437 | rc = qpnp_iadc_read_reg(QPNP_IADC_REVISION2, &revision); |
| 438 | if (rc < 0) { |
| 439 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 440 | return rc; |
| 441 | } |
| 442 | |
| 443 | if (revision < QPNP_IADC_SUPPORTED_REVISION2) { |
| 444 | pr_err("IADC Version not supported\n"); |
| 445 | return -EINVAL; |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | int32_t qpnp_iadc_is_ready(void) |
| 452 | { |
| 453 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 454 | |
| 455 | if (!iadc || !iadc->iadc_initialized) |
| 456 | return -EPROBE_DEFER; |
| 457 | else |
| 458 | return 0; |
| 459 | } |
| 460 | EXPORT_SYMBOL(qpnp_iadc_is_ready); |
| 461 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 462 | int32_t qpnp_iadc_get_rsense(int32_t *rsense) |
| 463 | { |
| 464 | uint8_t rslt_rsense; |
| 465 | int32_t rc, sign_bit = 0; |
| 466 | |
| 467 | rc = qpnp_iadc_read_reg(QPNP_IADC_NOMINAL_RSENSE, &rslt_rsense); |
| 468 | if (rc < 0) { |
| 469 | pr_err("qpnp adc rsense read failed with %d\n", rc); |
| 470 | return rc; |
| 471 | } |
| 472 | |
| 473 | if (rslt_rsense & QPNP_RSENSE_MSB_SIGN_CHECK) |
| 474 | sign_bit = 1; |
| 475 | |
| 476 | rslt_rsense &= ~QPNP_RSENSE_MSB_SIGN_CHECK; |
| 477 | |
| 478 | if (sign_bit) |
| 479 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR - |
| 480 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 481 | else |
| 482 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR + |
| 483 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 484 | |
| 485 | return rc; |
| 486 | } |
| 487 | |
| 488 | int32_t qpnp_check_pmic_temp(void) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 489 | { |
| 490 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 491 | struct qpnp_vadc_result result_pmic_therm; |
| 492 | int rc; |
| 493 | |
| 494 | rc = qpnp_vadc_read(DIE_TEMP, &result_pmic_therm); |
| 495 | if (rc < 0) |
| 496 | return rc; |
| 497 | |
| 498 | if (((uint64_t) (result_pmic_therm.physical - |
| 499 | iadc->die_temp_calib_offset)) |
| 500 | > QPNP_IADC_DIE_TEMP_CALIB_OFFSET) { |
| 501 | mutex_lock(&iadc->adc->adc_lock); |
| 502 | |
| 503 | rc = qpnp_iadc_calibrate_for_trim(); |
| 504 | if (rc) |
| 505 | pr_err("periodic IADC calibration failed\n"); |
| 506 | |
| 507 | mutex_unlock(&iadc->adc->adc_lock); |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | int32_t qpnp_iadc_read(enum qpnp_iadc_channels channel, |
| 514 | struct qpnp_iadc_result *result) |
| 515 | { |
| 516 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 517 | int32_t rc, rsense_n_ohms, sign = 0, num; |
| 518 | int64_t result_current; |
| 519 | uint16_t raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 520 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 521 | if (!iadc || !iadc->iadc_initialized) |
| 522 | return -EPROBE_DEFER; |
| 523 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 524 | rc = qpnp_check_pmic_temp(); |
| 525 | if (rc) { |
| 526 | pr_err("Error checking pmic therm temp\n"); |
| 527 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 530 | mutex_lock(&iadc->adc->adc_lock); |
| 531 | |
| 532 | rc = qpnp_iadc_configure(channel, &raw_data); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 533 | if (rc < 0) { |
| 534 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 535 | goto fail; |
| 536 | } |
| 537 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 538 | rc = qpnp_iadc_get_rsense(&rsense_n_ohms); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 539 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 540 | num = raw_data - iadc->adc->calib.offset_raw; |
| 541 | if (num < 0) { |
| 542 | sign = 1; |
| 543 | num = -num; |
| 544 | } |
| 545 | |
| 546 | result->result_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 547 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 548 | result_current = result->result_uv; |
| 549 | result_current *= QPNP_IADC_NANO_VOLTS_FACTOR; |
| 550 | do_div(result_current, rsense_n_ohms); |
| 551 | |
| 552 | if (sign) { |
| 553 | result->result_uv = -result->result_uv; |
| 554 | result_current = -result_current; |
| 555 | } |
| 556 | |
| 557 | result->result_ua = (int32_t) result_current; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 558 | fail: |
| 559 | mutex_unlock(&iadc->adc->adc_lock); |
| 560 | |
| 561 | return rc; |
| 562 | } |
| 563 | EXPORT_SYMBOL(qpnp_iadc_read); |
| 564 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 565 | 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] | 566 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 567 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 568 | int rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 569 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 570 | if (!iadc || !iadc->iadc_initialized) |
| 571 | return -EPROBE_DEFER; |
| 572 | |
| 573 | rc = qpnp_check_pmic_temp(); |
| 574 | if (rc) { |
| 575 | pr_err("Error checking pmic therm temp\n"); |
| 576 | return rc; |
| 577 | } |
| 578 | |
| 579 | mutex_lock(&iadc->adc->adc_lock); |
| 580 | result->gain_raw = iadc->adc->calib.gain_raw; |
| 581 | result->ideal_gain_nv = QPNP_ADC_GAIN_NV; |
| 582 | result->gain_uv = iadc->adc->calib.gain_uv; |
| 583 | result->offset_raw = iadc->adc->calib.offset_raw; |
| 584 | result->ideal_offset_uv = |
| 585 | QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL; |
| 586 | result->offset_uv = iadc->adc->calib.offset_uv; |
| 587 | mutex_unlock(&iadc->adc->adc_lock); |
| 588 | |
| 589 | return 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 590 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 591 | EXPORT_SYMBOL(qpnp_iadc_get_gain_and_offset); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 592 | |
| 593 | static ssize_t qpnp_iadc_show(struct device *dev, |
| 594 | struct device_attribute *devattr, char *buf) |
| 595 | { |
| 596 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 597 | struct qpnp_iadc_result result; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 598 | int rc = -1; |
| 599 | |
| 600 | rc = qpnp_iadc_read(attr->index, &result); |
| 601 | |
| 602 | if (rc) |
| 603 | return 0; |
| 604 | |
| 605 | return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 606 | "Result:%d\n", result.result_ua); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static struct sensor_device_attribute qpnp_adc_attr = |
| 610 | SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0); |
| 611 | |
| 612 | static int32_t qpnp_iadc_init_hwmon(struct spmi_device *spmi) |
| 613 | { |
| 614 | struct qpnp_iadc_drv *iadc = qpnp_iadc; |
| 615 | struct device_node *child; |
| 616 | struct device_node *node = spmi->dev.of_node; |
| 617 | int rc = 0, i = 0, channel; |
| 618 | |
| 619 | for_each_child_of_node(node, child) { |
| 620 | channel = iadc->adc->adc_channels[i].channel_num; |
| 621 | qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num; |
| 622 | qpnp_adc_attr.dev_attr.attr.name = |
| 623 | iadc->adc->adc_channels[i].name; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 624 | memcpy(&iadc->sens_attr[i], &qpnp_adc_attr, |
| 625 | sizeof(qpnp_adc_attr)); |
Stephen Boyd | 8a5c4e4 | 2012-10-30 11:07:22 -0700 | [diff] [blame] | 626 | sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 627 | rc = device_create_file(&spmi->dev, |
| 628 | &iadc->sens_attr[i].dev_attr); |
| 629 | if (rc) { |
| 630 | dev_err(&spmi->dev, |
| 631 | "device_create_file failed for dev %s\n", |
| 632 | iadc->adc->adc_channels[i].name); |
| 633 | goto hwmon_err_sens; |
| 634 | } |
| 635 | i++; |
| 636 | } |
| 637 | |
| 638 | return 0; |
| 639 | hwmon_err_sens: |
| 640 | pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc); |
| 641 | return rc; |
| 642 | } |
| 643 | |
| 644 | static int __devinit qpnp_iadc_probe(struct spmi_device *spmi) |
| 645 | { |
| 646 | struct qpnp_iadc_drv *iadc; |
| 647 | struct qpnp_adc_drv *adc_qpnp; |
| 648 | struct device_node *node = spmi->dev.of_node; |
| 649 | struct device_node *child; |
| 650 | int rc, count_adc_channel_list = 0; |
| 651 | |
| 652 | if (!node) |
| 653 | return -EINVAL; |
| 654 | |
| 655 | if (qpnp_iadc) { |
| 656 | pr_err("IADC already in use\n"); |
| 657 | return -EBUSY; |
| 658 | } |
| 659 | |
| 660 | for_each_child_of_node(node, child) |
| 661 | count_adc_channel_list++; |
| 662 | |
| 663 | if (!count_adc_channel_list) { |
| 664 | pr_err("No channel listing\n"); |
| 665 | return -EINVAL; |
| 666 | } |
| 667 | |
| 668 | iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_drv) + |
| 669 | (sizeof(struct sensor_device_attribute) * |
| 670 | count_adc_channel_list), GFP_KERNEL); |
| 671 | if (!iadc) { |
| 672 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 673 | return -ENOMEM; |
| 674 | } |
| 675 | |
| 676 | adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv), |
| 677 | GFP_KERNEL); |
| 678 | if (!adc_qpnp) { |
| 679 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 680 | return -ENOMEM; |
| 681 | } |
| 682 | |
| 683 | iadc->adc = adc_qpnp; |
| 684 | |
| 685 | rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc); |
| 686 | if (rc) { |
| 687 | dev_err(&spmi->dev, "failed to read device tree\n"); |
| 688 | return rc; |
| 689 | } |
| 690 | |
| 691 | rc = of_property_read_u32(node, "qcom,rsense", |
| 692 | &iadc->rsense); |
| 693 | if (rc) { |
| 694 | pr_err("Invalid rsens reference property\n"); |
| 695 | return -EINVAL; |
| 696 | } |
| 697 | |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame^] | 698 | rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq_eoc, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 699 | qpnp_iadc_isr, |
| 700 | IRQF_TRIGGER_RISING, "qpnp_iadc_interrupt", iadc); |
| 701 | if (rc) { |
| 702 | dev_err(&spmi->dev, "failed to request adc irq\n"); |
| 703 | return rc; |
| 704 | } else |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame^] | 705 | enable_irq_wake(iadc->adc->adc_irq_eoc); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 706 | |
| 707 | iadc->iadc_init_calib = false; |
| 708 | dev_set_drvdata(&spmi->dev, iadc); |
| 709 | qpnp_iadc = iadc; |
| 710 | |
| 711 | rc = qpnp_iadc_init_hwmon(spmi); |
| 712 | if (rc) { |
| 713 | dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n"); |
| 714 | return rc; |
| 715 | } |
| 716 | iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev); |
| 717 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 718 | rc = qpnp_iadc_version_check(); |
| 719 | if (rc) { |
| 720 | dev_err(&spmi->dev, "IADC version not supported\n"); |
| 721 | return rc; |
| 722 | } |
| 723 | |
| 724 | rc = qpnp_iadc_calibrate_for_trim(); |
| 725 | if (rc) { |
| 726 | dev_err(&spmi->dev, "failed to calibrate for USR trim\n"); |
| 727 | return rc; |
| 728 | } |
| 729 | iadc->iadc_init_calib = true; |
| 730 | INIT_DELAYED_WORK(&iadc->iadc_work, qpnp_iadc_work); |
| 731 | schedule_delayed_work(&iadc->iadc_work, |
| 732 | round_jiffies_relative(msecs_to_jiffies |
| 733 | (QPNP_IADC_CALIB_SECONDS))); |
| 734 | iadc->iadc_initialized = true; |
| 735 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static int __devexit qpnp_iadc_remove(struct spmi_device *spmi) |
| 740 | { |
| 741 | struct qpnp_iadc_drv *iadc = dev_get_drvdata(&spmi->dev); |
| 742 | struct device_node *node = spmi->dev.of_node; |
| 743 | struct device_node *child; |
| 744 | int i = 0; |
| 745 | |
| 746 | for_each_child_of_node(node, child) { |
| 747 | device_remove_file(&spmi->dev, |
| 748 | &iadc->sens_attr[i].dev_attr); |
| 749 | i++; |
| 750 | } |
| 751 | dev_set_drvdata(&spmi->dev, NULL); |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | static const struct of_device_id qpnp_iadc_match_table[] = { |
| 757 | { .compatible = "qcom,qpnp-iadc", |
| 758 | }, |
| 759 | {} |
| 760 | }; |
| 761 | |
| 762 | static struct spmi_driver qpnp_iadc_driver = { |
| 763 | .driver = { |
| 764 | .name = "qcom,qpnp-iadc", |
| 765 | .of_match_table = qpnp_iadc_match_table, |
| 766 | }, |
| 767 | .probe = qpnp_iadc_probe, |
| 768 | .remove = qpnp_iadc_remove, |
| 769 | }; |
| 770 | |
| 771 | static int __init qpnp_iadc_init(void) |
| 772 | { |
| 773 | return spmi_driver_register(&qpnp_iadc_driver); |
| 774 | } |
| 775 | module_init(qpnp_iadc_init); |
| 776 | |
| 777 | static void __exit qpnp_iadc_exit(void) |
| 778 | { |
| 779 | spmi_driver_unregister(&qpnp_iadc_driver); |
| 780 | } |
| 781 | module_exit(qpnp_iadc_exit); |
| 782 | |
| 783 | MODULE_DESCRIPTION("QPNP PMIC current ADC driver"); |
| 784 | MODULE_LICENSE("GPL v2"); |