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 |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 98 | #define QPNP_INT_TEST_VAL 0xE1 |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 99 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 100 | #define QPNP_IADC_ADC_CH_SEL_CTL 0x48 |
| 101 | #define QPNP_IADC_ADC_CHX_SEL_SHIFT 3 |
| 102 | |
| 103 | #define QPNP_IADC_ADC_DIG_PARAM 0x50 |
| 104 | #define QPNP_IADC_CLK_SEL_SHIFT 1 |
| 105 | #define QPNP_IADC_DEC_RATIO_SEL 3 |
| 106 | |
| 107 | #define QPNP_IADC_CONV_REQUEST 0x52 |
| 108 | #define QPNP_IADC_CONV_REQ BIT(7) |
| 109 | |
| 110 | #define QPNP_IADC_DATA0 0x60 |
| 111 | #define QPNP_IADC_DATA1 0x61 |
| 112 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 113 | #define QPNP_ADC_CONV_TIME_MIN 8000 |
| 114 | #define QPNP_ADC_CONV_TIME_MAX 8200 |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 115 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 116 | #define QPNP_ADC_GAIN_NV 17857 |
| 117 | #define QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL 0 |
| 118 | #define QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR 10000000 |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 119 | #define QPNP_IADC_NANO_VOLTS_FACTOR 1000000 |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 120 | #define QPNP_IADC_CALIB_SECONDS 300000 |
| 121 | #define QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT 15625 |
| 122 | #define QPNP_IADC_DIE_TEMP_CALIB_OFFSET 5000 |
| 123 | |
| 124 | #define QPNP_RAW_CODE_16_BIT_MSB_MASK 0xff00 |
| 125 | #define QPNP_RAW_CODE_16_BIT_LSB_MASK 0xff |
| 126 | #define QPNP_BIT_SHIFT_8 8 |
| 127 | #define QPNP_RSENSE_MSB_SIGN_CHECK 0x80 |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 128 | #define QPNP_ADC_COMPLETION_TIMEOUT HZ |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 129 | |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 130 | struct qpnp_iadc_comp { |
| 131 | bool ext_rsense; |
| 132 | u8 id; |
| 133 | u8 sys_gain; |
| 134 | u8 revision; |
| 135 | }; |
| 136 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 137 | struct qpnp_iadc_chip { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 138 | struct qpnp_adc_drv *adc; |
| 139 | int32_t rsense; |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 140 | bool external_rsense; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 141 | struct device *iadc_hwmon; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 142 | struct list_head list; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 143 | int64_t die_temp; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 144 | struct delayed_work iadc_work; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 145 | struct mutex iadc_vadc_lock; |
| 146 | bool iadc_mode_sel; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 147 | struct qpnp_iadc_comp iadc_comp; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 148 | struct qpnp_vadc_chip *vadc_dev; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 149 | struct work_struct trigger_completion_work; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 150 | struct sensor_device_attribute sens_attr[0]; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 151 | bool skip_auto_calibrations; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 154 | LIST_HEAD(qpnp_iadc_device_list); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 155 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 156 | static int32_t qpnp_iadc_read_reg(struct qpnp_iadc_chip *iadc, |
| 157 | uint32_t reg, u8 *data) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 158 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 159 | int rc; |
| 160 | |
| 161 | rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 162 | (iadc->adc->offset + reg), data, 1); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 163 | if (rc < 0) { |
| 164 | pr_err("qpnp iadc read reg %d failed with %d\n", reg, rc); |
| 165 | return rc; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 171 | static int32_t qpnp_iadc_write_reg(struct qpnp_iadc_chip *iadc, |
| 172 | uint32_t reg, u8 data) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 173 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 174 | int rc; |
| 175 | u8 *buf; |
| 176 | |
| 177 | buf = &data; |
| 178 | rc = spmi_ext_register_writel(iadc->adc->spmi->ctrl, iadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 179 | (iadc->adc->offset + reg), buf, 1); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 180 | if (rc < 0) { |
| 181 | pr_err("qpnp iadc write reg %d failed with %d\n", reg, rc); |
| 182 | return rc; |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 188 | static int qpnp_iadc_is_valid(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 189 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 190 | struct qpnp_iadc_chip *iadc_chip = NULL; |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 191 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 192 | list_for_each_entry(iadc_chip, &qpnp_iadc_device_list, list) |
| 193 | if (iadc == iadc_chip) |
| 194 | return 0; |
| 195 | |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
| 199 | static void qpnp_iadc_trigger_completion(struct work_struct *work) |
| 200 | { |
| 201 | struct qpnp_iadc_chip *iadc = container_of(work, |
| 202 | struct qpnp_iadc_chip, trigger_completion_work); |
| 203 | |
| 204 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 205 | return; |
| 206 | |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 207 | complete(&iadc->adc->adc_rslt_completion); |
| 208 | |
| 209 | return; |
| 210 | } |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 211 | |
| 212 | static irqreturn_t qpnp_iadc_isr(int irq, void *dev_id) |
| 213 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 214 | struct qpnp_iadc_chip *iadc = dev_id; |
| 215 | |
| 216 | schedule_work(&iadc->trigger_completion_work); |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 217 | |
| 218 | return IRQ_HANDLED; |
| 219 | } |
| 220 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 221 | static int32_t qpnp_iadc_enable(struct qpnp_iadc_chip *dev, bool state) |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 222 | { |
| 223 | int rc = 0; |
| 224 | u8 data = 0; |
| 225 | |
| 226 | data = QPNP_IADC_ADC_EN; |
| 227 | if (state) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 228 | rc = qpnp_iadc_write_reg(dev, QPNP_IADC_EN_CTL1, |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 229 | data); |
| 230 | if (rc < 0) { |
| 231 | pr_err("IADC enable failed\n"); |
| 232 | return rc; |
| 233 | } |
| 234 | } else { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 235 | rc = qpnp_iadc_write_reg(dev, QPNP_IADC_EN_CTL1, |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 236 | (~data & QPNP_IADC_ADC_EN)); |
| 237 | if (rc < 0) { |
| 238 | pr_err("IADC disable failed\n"); |
| 239 | return rc; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 246 | static int32_t qpnp_iadc_status_debug(struct qpnp_iadc_chip *dev) |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 247 | { |
| 248 | int rc = 0; |
| 249 | u8 mode = 0, status1 = 0, chan = 0, dig = 0, en = 0; |
| 250 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 251 | rc = qpnp_iadc_read_reg(dev, QPNP_IADC_MODE_CTL, &mode); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 252 | if (rc < 0) { |
| 253 | pr_err("mode ctl register read failed with %d\n", rc); |
| 254 | return rc; |
| 255 | } |
| 256 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 257 | rc = qpnp_iadc_read_reg(dev, QPNP_ADC_DIG_PARAM, &dig); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 258 | if (rc < 0) { |
| 259 | pr_err("digital param read failed with %d\n", rc); |
| 260 | return rc; |
| 261 | } |
| 262 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 263 | rc = qpnp_iadc_read_reg(dev, QPNP_IADC_ADC_CH_SEL_CTL, &chan); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 264 | if (rc < 0) { |
| 265 | pr_err("channel read failed with %d\n", rc); |
| 266 | return rc; |
| 267 | } |
| 268 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 269 | rc = qpnp_iadc_read_reg(dev, QPNP_STATUS1, &status1); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 270 | if (rc < 0) { |
| 271 | pr_err("status1 read failed with %d\n", rc); |
| 272 | return rc; |
| 273 | } |
| 274 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 275 | rc = qpnp_iadc_read_reg(dev, QPNP_IADC_EN_CTL1, &en); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 276 | if (rc < 0) { |
| 277 | pr_err("en read failed with %d\n", rc); |
| 278 | return rc; |
| 279 | } |
| 280 | |
Siddartha Mohanadoss | 73ae69b | 2013-04-03 17:34:03 -0700 | [diff] [blame] | 281 | pr_debug("EOC not set with status:%x, dig:%x, ch:%x, mode:%x, en:%x\n", |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 282 | status1, dig, chan, mode, en); |
| 283 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 284 | rc = qpnp_iadc_enable(dev, false); |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 285 | if (rc < 0) { |
| 286 | pr_err("IADC disable failed with %d\n", rc); |
| 287 | return rc; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 293 | static int32_t qpnp_iadc_read_conversion_result(struct qpnp_iadc_chip *iadc, |
| 294 | int16_t *data) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 295 | { |
| 296 | uint8_t rslt_lsb, rslt_msb; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 297 | uint16_t rslt; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 298 | int32_t rc; |
| 299 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 300 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_DATA0, &rslt_lsb); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 301 | if (rc < 0) { |
| 302 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 303 | return rc; |
| 304 | } |
| 305 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 306 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_DATA1, &rslt_msb); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 307 | if (rc < 0) { |
| 308 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 309 | return rc; |
| 310 | } |
| 311 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 312 | rslt = (rslt_msb << 8) | rslt_lsb; |
| 313 | *data = rslt; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 314 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 315 | rc = qpnp_iadc_enable(iadc, false); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 316 | if (rc) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 317 | return rc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 318 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 322 | static int32_t qpnp_iadc_comp(int64_t *result, struct qpnp_iadc_comp comp, |
| 323 | int64_t die_temp) |
| 324 | { |
Xiaozhe Shi | 62ad5e1 | 2013-05-13 12:37:41 -0700 | [diff] [blame] | 325 | int64_t temp_var = 0, sign_coeff = 0, sys_gain_coeff = 0, old; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 326 | |
Xiaozhe Shi | 62ad5e1 | 2013-05-13 12:37:41 -0700 | [diff] [blame] | 327 | old = *result; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 328 | *result = *result * 1000000; |
| 329 | |
| 330 | if (comp.revision == QPNP_IADC_VER_3_1) { |
| 331 | /* revision 3.1 */ |
| 332 | if (comp.sys_gain > 127) |
| 333 | sys_gain_coeff = -QPNP_COEFF_6 * (comp.sys_gain - 128); |
| 334 | else |
| 335 | sys_gain_coeff = QPNP_COEFF_6 * comp.sys_gain; |
Xiaozhe Shi | 62ad5e1 | 2013-05-13 12:37:41 -0700 | [diff] [blame] | 336 | } else if (comp.revision != QPNP_IADC_VER_3_0) { |
| 337 | /* unsupported revision, do not compensate */ |
| 338 | *result = old; |
| 339 | return 0; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 342 | if (!comp.ext_rsense) { |
| 343 | /* internal rsense */ |
| 344 | switch (comp.id) { |
| 345 | case COMP_ID_TSMC: |
| 346 | temp_var = ((QPNP_COEFF_2 * die_temp) - |
| 347 | QPNP_COEFF_3_TYPEB); |
| 348 | break; |
| 349 | case COMP_ID_GF: |
| 350 | default: |
| 351 | temp_var = ((QPNP_COEFF_2 * die_temp) - |
| 352 | QPNP_COEFF_3_TYPEA); |
| 353 | break; |
| 354 | } |
| 355 | temp_var = div64_s64(temp_var, QPNP_COEFF_4); |
| 356 | if (comp.revision == QPNP_IADC_VER_3_0) |
| 357 | temp_var = QPNP_COEFF_1 * (1000000 - temp_var); |
| 358 | else if (comp.revision == QPNP_IADC_VER_3_1) |
Xiaozhe Shi | 62ad5e1 | 2013-05-13 12:37:41 -0700 | [diff] [blame] | 359 | temp_var = 1000000 * (1000000 - temp_var); |
| 360 | *result = div64_s64(*result * 1000000, temp_var); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | sign_coeff = *result < 0 ? QPNP_COEFF_7 : QPNP_COEFF_5; |
| 364 | if (comp.ext_rsense) { |
| 365 | /* external rsense and current charging */ |
| 366 | temp_var = div64_s64((-sign_coeff * die_temp) + QPNP_COEFF_8, |
| 367 | QPNP_COEFF_4); |
| 368 | temp_var = 1000000000 - temp_var; |
| 369 | if (comp.revision == QPNP_IADC_VER_3_1) { |
| 370 | sys_gain_coeff = (1000000 + |
| 371 | div64_s64(sys_gain_coeff, QPNP_COEFF_4)); |
| 372 | temp_var = div64_s64(temp_var * sys_gain_coeff, |
| 373 | 1000000000); |
| 374 | } |
| 375 | *result = div64_s64(*result, temp_var); |
| 376 | } |
Xiaozhe Shi | 62ad5e1 | 2013-05-13 12:37:41 -0700 | [diff] [blame] | 377 | pr_debug("%lld compensated into %lld\n", old, *result); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 382 | int32_t qpnp_iadc_comp_result(struct qpnp_iadc_chip *iadc, int64_t *result) |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 383 | { |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 384 | return qpnp_iadc_comp(result, iadc->iadc_comp, iadc->die_temp); |
| 385 | } |
| 386 | EXPORT_SYMBOL(qpnp_iadc_comp_result); |
| 387 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 388 | static int32_t qpnp_iadc_comp_info(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 389 | { |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 390 | int rc = 0; |
| 391 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 392 | rc = qpnp_iadc_read_reg(iadc, QPNP_INT_TEST_VAL, &iadc->iadc_comp.id); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 393 | if (rc < 0) { |
| 394 | pr_err("qpnp adc comp id failed with %d\n", rc); |
| 395 | return rc; |
| 396 | } |
| 397 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 398 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION2, |
| 399 | &iadc->iadc_comp.revision); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 400 | if (rc < 0) { |
| 401 | pr_err("qpnp adc revision read failed with %d\n", rc); |
| 402 | return rc; |
| 403 | } |
| 404 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 405 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_ATE_GAIN_CALIB_OFFSET, |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 406 | &iadc->iadc_comp.sys_gain); |
Siddartha Mohanadoss | 7bea844 | 2013-06-17 17:50:22 -0700 | [diff] [blame] | 407 | if (rc < 0) { |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 408 | pr_err("full scale read failed with %d\n", rc); |
Siddartha Mohanadoss | 7bea844 | 2013-06-17 17:50:22 -0700 | [diff] [blame] | 409 | return rc; |
| 410 | } |
| 411 | |
| 412 | if (iadc->external_rsense) |
| 413 | iadc->iadc_comp.ext_rsense = true; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 414 | |
| 415 | pr_debug("fab id = %u, revision = %u, sys gain = %u, external_rsense = %d\n", |
| 416 | iadc->iadc_comp.id, |
| 417 | iadc->iadc_comp.revision, |
| 418 | iadc->iadc_comp.sys_gain, |
| 419 | iadc->iadc_comp.ext_rsense); |
| 420 | return rc; |
| 421 | } |
| 422 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 423 | static int32_t qpnp_iadc_configure(struct qpnp_iadc_chip *iadc, |
| 424 | enum qpnp_iadc_channels channel, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 425 | uint16_t *raw_code, uint32_t mode_sel) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 426 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 427 | u8 qpnp_iadc_mode_reg = 0, qpnp_iadc_ch_sel_reg = 0; |
| 428 | u8 qpnp_iadc_conv_req = 0, qpnp_iadc_dig_param_reg = 0; |
| 429 | int32_t rc = 0; |
| 430 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 431 | qpnp_iadc_ch_sel_reg = channel; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 432 | |
| 433 | qpnp_iadc_dig_param_reg |= iadc->adc->amux_prop->decimation << |
| 434 | QPNP_IADC_DEC_RATIO_SEL; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 435 | if (iadc->iadc_mode_sel) |
| 436 | qpnp_iadc_mode_reg |= (QPNP_ADC_TRIM_EN | QPNP_VADC_SYNCH_EN); |
| 437 | else |
| 438 | qpnp_iadc_mode_reg |= QPNP_ADC_TRIM_EN; |
| 439 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 440 | qpnp_iadc_conv_req = QPNP_IADC_CONV_REQ; |
| 441 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 442 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_MODE_CTL, qpnp_iadc_mode_reg); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 443 | if (rc) { |
| 444 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 445 | return rc; |
| 446 | } |
| 447 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 448 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_ADC_CH_SEL_CTL, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 449 | qpnp_iadc_ch_sel_reg); |
| 450 | if (rc) { |
| 451 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 452 | return rc; |
| 453 | } |
| 454 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 455 | rc = qpnp_iadc_write_reg(iadc, QPNP_ADC_DIG_PARAM, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 456 | qpnp_iadc_dig_param_reg); |
| 457 | if (rc) { |
| 458 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 459 | return rc; |
| 460 | } |
| 461 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 462 | rc = qpnp_iadc_write_reg(iadc, QPNP_HW_SETTLE_DELAY, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 463 | iadc->adc->amux_prop->hw_settle_time); |
| 464 | if (rc < 0) { |
| 465 | pr_err("qpnp adc configure error for hw settling time setup\n"); |
| 466 | return rc; |
| 467 | } |
| 468 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 469 | rc = qpnp_iadc_write_reg(iadc, QPNP_FAST_AVG_CTL, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 470 | iadc->adc->amux_prop->fast_avg_setup); |
| 471 | if (rc < 0) { |
| 472 | pr_err("qpnp adc fast averaging configure error\n"); |
| 473 | return rc; |
| 474 | } |
| 475 | |
Siddartha Mohanadoss | 3f219c4 | 2013-04-02 11:01:28 -0700 | [diff] [blame] | 476 | INIT_COMPLETION(iadc->adc->adc_rslt_completion); |
| 477 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 478 | rc = qpnp_iadc_enable(iadc, true); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 479 | if (rc) |
| 480 | return rc; |
| 481 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 482 | rc = qpnp_iadc_write_reg(iadc, QPNP_CONV_REQ, qpnp_iadc_conv_req); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 483 | if (rc) { |
| 484 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 485 | return rc; |
| 486 | } |
| 487 | |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 488 | rc = wait_for_completion_timeout(&iadc->adc->adc_rslt_completion, |
| 489 | QPNP_ADC_COMPLETION_TIMEOUT); |
| 490 | if (!rc) { |
| 491 | u8 status1 = 0; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 492 | rc = qpnp_iadc_read_reg(iadc, QPNP_STATUS1, &status1); |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 493 | if (rc < 0) |
| 494 | return rc; |
| 495 | status1 &= (QPNP_STATUS1_REQ_STS | QPNP_STATUS1_EOC); |
| 496 | if (status1 == QPNP_STATUS1_EOC) |
| 497 | pr_debug("End of conversion status set\n"); |
| 498 | else { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 499 | rc = qpnp_iadc_status_debug(iadc); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 500 | if (rc < 0) { |
| 501 | pr_err("status1 read failed with %d\n", rc); |
| 502 | return rc; |
| 503 | } |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 504 | return -EINVAL; |
| 505 | } |
| 506 | } |
| 507 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 508 | rc = qpnp_iadc_read_conversion_result(iadc, raw_code); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 509 | if (rc) { |
| 510 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 511 | return rc; |
| 512 | } |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 517 | #define IADC_CENTER 0xC000 |
| 518 | #define IADC_READING_RESOLUTION_N 542535 |
| 519 | #define IADC_READING_RESOLUTION_D 100000 |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 520 | static int32_t qpnp_convert_raw_offset_voltage(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 521 | { |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 522 | s64 numerator; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 523 | |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 524 | if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) { |
| 525 | pr_err("raw offset errors! raw_gain:0x%x and raw_offset:0x%x\n", |
| 526 | iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw); |
| 527 | return -EINVAL; |
| 528 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 529 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 530 | numerator = iadc->adc->calib.offset_raw - IADC_CENTER; |
| 531 | numerator *= IADC_READING_RESOLUTION_N; |
| 532 | iadc->adc->calib.offset_uv = div_s64(numerator, |
| 533 | IADC_READING_RESOLUTION_D); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 534 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 535 | numerator = iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw; |
| 536 | numerator *= IADC_READING_RESOLUTION_N; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 537 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 538 | iadc->adc->calib.gain_uv = div_s64(numerator, |
| 539 | IADC_READING_RESOLUTION_D); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 540 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 541 | pr_debug("gain_uv:%d offset_uv:%d\n", |
| 542 | iadc->adc->calib.gain_uv, iadc->adc->calib.offset_uv); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 546 | int32_t qpnp_iadc_calibrate_for_trim(struct qpnp_iadc_chip *iadc, |
| 547 | bool batfet_closed) |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 548 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 549 | uint8_t rslt_lsb, rslt_msb; |
| 550 | int32_t rc = 0; |
| 551 | uint16_t raw_data; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 552 | uint32_t mode_sel = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 553 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 554 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 555 | return -EPROBE_DEFER; |
| 556 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 557 | mutex_lock(&iadc->adc->adc_lock); |
| 558 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 559 | rc = qpnp_iadc_configure(iadc, GAIN_CALIBRATION_17P857MV, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 560 | &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 561 | if (rc < 0) { |
| 562 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 563 | goto fail; |
| 564 | } |
| 565 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 566 | iadc->adc->calib.gain_raw = raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 567 | |
Abhijeet Dharmapurikar | 0ef9b5c | 2013-07-15 18:24:38 -0700 | [diff] [blame] | 568 | /* |
| 569 | * there is a features in the BMS where if the batfet is opened |
| 570 | * the BMS reads from INTERNAL_RSENSE (channel 0) actually go to |
| 571 | * OFFSET_CALIBRATION_CSP_CSN (channel 5). Hence if batfet is opened |
| 572 | * we have to calibrate based on OFFSET_CALIBRATION_CSP_CSN even for |
| 573 | * internal rsense. |
| 574 | */ |
| 575 | if (!batfet_closed || iadc->external_rsense) { |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 576 | /* external offset calculation */ |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 577 | rc = qpnp_iadc_configure(iadc, OFFSET_CALIBRATION_CSP_CSN, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 578 | &raw_data, mode_sel); |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 579 | if (rc < 0) { |
| 580 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 581 | goto fail; |
| 582 | } |
| 583 | } else { |
| 584 | /* internal offset calculation */ |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 585 | rc = qpnp_iadc_configure(iadc, OFFSET_CALIBRATION_CSP2_CSN2, |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 586 | &raw_data, mode_sel); |
| 587 | if (rc < 0) { |
| 588 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 589 | goto fail; |
| 590 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 593 | iadc->adc->calib.offset_raw = raw_data; |
| 594 | if (rc < 0) { |
| 595 | pr_err("qpnp adc offset/gain calculation failed\n"); |
| 596 | goto fail; |
| 597 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 598 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 599 | pr_debug("raw gain:0x%x, raw offset:0x%x\n", |
| 600 | iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw); |
| 601 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 602 | rc = qpnp_convert_raw_offset_voltage(iadc); |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 603 | if (rc < 0) { |
| 604 | pr_err("qpnp raw_voltage conversion failed\n"); |
| 605 | goto fail; |
| 606 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 607 | |
| 608 | rslt_msb = (raw_data & QPNP_RAW_CODE_16_BIT_MSB_MASK) >> |
| 609 | QPNP_BIT_SHIFT_8; |
| 610 | rslt_lsb = raw_data & QPNP_RAW_CODE_16_BIT_LSB_MASK; |
| 611 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 612 | pr_debug("trim values:lsb:0x%x and msb:0x%x\n", rslt_lsb, rslt_msb); |
| 613 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 614 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_SEC_ACCESS, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 615 | QPNP_IADC_SEC_ACCESS_DATA); |
| 616 | if (rc < 0) { |
| 617 | pr_err("qpnp iadc configure error for sec access\n"); |
| 618 | goto fail; |
| 619 | } |
| 620 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 621 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_MSB_OFFSET, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 622 | rslt_msb); |
| 623 | if (rc < 0) { |
| 624 | pr_err("qpnp iadc configure error for MSB write\n"); |
| 625 | goto fail; |
| 626 | } |
| 627 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 628 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_SEC_ACCESS, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 629 | QPNP_IADC_SEC_ACCESS_DATA); |
| 630 | if (rc < 0) { |
| 631 | pr_err("qpnp iadc configure error for sec access\n"); |
| 632 | goto fail; |
| 633 | } |
| 634 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 635 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_LSB_OFFSET, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 636 | rslt_lsb); |
| 637 | if (rc < 0) { |
| 638 | pr_err("qpnp iadc configure error for LSB write\n"); |
| 639 | goto fail; |
| 640 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 641 | fail: |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 642 | mutex_unlock(&iadc->adc->adc_lock); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 643 | return rc; |
| 644 | } |
Siddartha Mohanadoss | 0667392 | 2013-03-27 11:14:19 -0700 | [diff] [blame] | 645 | EXPORT_SYMBOL(qpnp_iadc_calibrate_for_trim); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 646 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 647 | static void qpnp_iadc_work(struct work_struct *work) |
| 648 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 649 | struct qpnp_iadc_chip *iadc = container_of(work, |
| 650 | struct qpnp_iadc_chip, iadc_work.work); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 651 | int rc = 0; |
| 652 | |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 653 | if (!iadc->skip_auto_calibrations) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 654 | rc = qpnp_iadc_calibrate_for_trim(iadc, true); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 655 | if (rc) |
| 656 | pr_debug("periodic IADC calibration failed\n"); |
| 657 | } |
| 658 | |
| 659 | schedule_delayed_work(&iadc->iadc_work, |
| 660 | round_jiffies_relative(msecs_to_jiffies |
| 661 | (QPNP_IADC_CALIB_SECONDS))); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 662 | return; |
| 663 | } |
| 664 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 665 | static int32_t qpnp_iadc_version_check(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 666 | { |
| 667 | uint8_t revision; |
| 668 | int rc; |
| 669 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 670 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION2, &revision); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 671 | if (rc < 0) { |
| 672 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 673 | return rc; |
| 674 | } |
| 675 | |
| 676 | if (revision < QPNP_IADC_SUPPORTED_REVISION2) { |
| 677 | pr_err("IADC Version not supported\n"); |
| 678 | return -EINVAL; |
| 679 | } |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 684 | struct qpnp_iadc_chip *qpnp_get_iadc(struct device *dev, const char *name) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 685 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 686 | struct qpnp_iadc_chip *iadc; |
| 687 | struct device_node *node = NULL; |
| 688 | char prop_name[QPNP_MAX_PROP_NAME_LEN]; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 689 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 690 | snprintf(prop_name, QPNP_MAX_PROP_NAME_LEN, "qcom,%s-iadc", name); |
| 691 | |
| 692 | node = of_parse_phandle(dev->of_node, prop_name, 0); |
| 693 | if (node == NULL) |
| 694 | return ERR_PTR(-ENODEV); |
| 695 | |
| 696 | list_for_each_entry(iadc, &qpnp_iadc_device_list, list) |
| 697 | if (iadc->adc->spmi->dev.of_node == node) |
| 698 | return iadc; |
| 699 | return ERR_PTR(-EPROBE_DEFER); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 700 | } |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 701 | EXPORT_SYMBOL(qpnp_get_iadc); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 702 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 703 | int32_t qpnp_iadc_get_rsense(struct qpnp_iadc_chip *iadc, int32_t *rsense) |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 704 | { |
| 705 | uint8_t rslt_rsense; |
Siddartha Mohanadoss | 5827954 | 2013-05-28 16:20:46 -0700 | [diff] [blame] | 706 | int32_t rc = 0, sign_bit = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 707 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 708 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 709 | return -EPROBE_DEFER; |
| 710 | |
Siddartha Mohanadoss | 5827954 | 2013-05-28 16:20:46 -0700 | [diff] [blame] | 711 | if (iadc->external_rsense) { |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 712 | *rsense = iadc->rsense; |
Siddartha Mohanadoss | 5827954 | 2013-05-28 16:20:46 -0700 | [diff] [blame] | 713 | return rc; |
| 714 | } |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 715 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 716 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_NOMINAL_RSENSE, &rslt_rsense); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 717 | if (rc < 0) { |
| 718 | pr_err("qpnp adc rsense read failed with %d\n", rc); |
| 719 | return rc; |
| 720 | } |
| 721 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 722 | pr_debug("rsense:0%x\n", rslt_rsense); |
| 723 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 724 | if (rslt_rsense & QPNP_RSENSE_MSB_SIGN_CHECK) |
| 725 | sign_bit = 1; |
| 726 | |
| 727 | rslt_rsense &= ~QPNP_RSENSE_MSB_SIGN_CHECK; |
| 728 | |
| 729 | if (sign_bit) |
| 730 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR - |
| 731 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 732 | else |
| 733 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR + |
| 734 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 735 | |
| 736 | return rc; |
| 737 | } |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 738 | EXPORT_SYMBOL(qpnp_iadc_get_rsense); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 739 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 740 | static int32_t qpnp_check_pmic_temp(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 741 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 742 | struct qpnp_vadc_result result_pmic_therm; |
Siddartha Mohanadoss | d4e9edc | 2013-04-17 14:42:16 -0700 | [diff] [blame] | 743 | int64_t die_temp_offset; |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 744 | int rc = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 745 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 746 | rc = qpnp_vadc_read(iadc->vadc_dev, DIE_TEMP, &result_pmic_therm); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 747 | if (rc < 0) |
| 748 | return rc; |
| 749 | |
Siddartha Mohanadoss | d4e9edc | 2013-04-17 14:42:16 -0700 | [diff] [blame] | 750 | die_temp_offset = result_pmic_therm.physical - |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 751 | iadc->die_temp; |
Siddartha Mohanadoss | d4e9edc | 2013-04-17 14:42:16 -0700 | [diff] [blame] | 752 | if (die_temp_offset < 0) |
| 753 | die_temp_offset = -die_temp_offset; |
| 754 | |
| 755 | if (die_temp_offset > QPNP_IADC_DIE_TEMP_CALIB_OFFSET) { |
Abhijeet Dharmapurikar | 0ef9b5c | 2013-07-15 18:24:38 -0700 | [diff] [blame] | 756 | iadc->die_temp = result_pmic_therm.physical; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 757 | if (!iadc->skip_auto_calibrations) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 758 | rc = qpnp_iadc_calibrate_for_trim(iadc, true); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 759 | if (rc) |
| 760 | pr_err("IADC calibration failed rc = %d\n", rc); |
| 761 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 764 | return rc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 767 | int32_t qpnp_iadc_read(struct qpnp_iadc_chip *iadc, |
| 768 | enum qpnp_iadc_channels channel, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 769 | struct qpnp_iadc_result *result) |
| 770 | { |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 771 | int32_t rc, rsense_n_ohms, sign = 0, num, mode_sel = 0; |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 772 | int32_t rsense_u_ohms = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 773 | int64_t result_current; |
| 774 | uint16_t raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 775 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 776 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 777 | return -EPROBE_DEFER; |
| 778 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 779 | if (!iadc->iadc_mode_sel) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 780 | rc = qpnp_check_pmic_temp(iadc); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 781 | if (rc) { |
| 782 | pr_err("Error checking pmic therm temp\n"); |
| 783 | return rc; |
| 784 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 787 | mutex_lock(&iadc->adc->adc_lock); |
| 788 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 789 | rc = qpnp_iadc_configure(iadc, channel, &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 790 | if (rc < 0) { |
| 791 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 792 | goto fail; |
| 793 | } |
| 794 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 795 | rc = qpnp_iadc_get_rsense(iadc, &rsense_n_ohms); |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 796 | pr_debug("current raw:0%x and rsense:%d\n", |
| 797 | raw_data, rsense_n_ohms); |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 798 | rsense_u_ohms = rsense_n_ohms/1000; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 799 | num = raw_data - iadc->adc->calib.offset_raw; |
| 800 | if (num < 0) { |
| 801 | sign = 1; |
| 802 | num = -num; |
| 803 | } |
| 804 | |
| 805 | result->result_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 806 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 807 | result_current = result->result_uv; |
| 808 | result_current *= QPNP_IADC_NANO_VOLTS_FACTOR; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 809 | /* Intentional fall through. Process the result w/o comp */ |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 810 | do_div(result_current, rsense_u_ohms); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 811 | |
| 812 | if (sign) { |
| 813 | result->result_uv = -result->result_uv; |
| 814 | result_current = -result_current; |
| 815 | } |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 816 | rc = qpnp_iadc_comp_result(iadc, &result_current); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 817 | if (rc < 0) |
| 818 | pr_err("Error during compensating the IADC\n"); |
| 819 | rc = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 820 | |
| 821 | result->result_ua = (int32_t) result_current; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 822 | fail: |
| 823 | mutex_unlock(&iadc->adc->adc_lock); |
| 824 | |
| 825 | return rc; |
| 826 | } |
| 827 | EXPORT_SYMBOL(qpnp_iadc_read); |
| 828 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 829 | int32_t qpnp_iadc_get_gain_and_offset(struct qpnp_iadc_chip *iadc, |
| 830 | struct qpnp_iadc_calib *result) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 831 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 832 | int rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 833 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 834 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 835 | return -EPROBE_DEFER; |
| 836 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 837 | rc = qpnp_check_pmic_temp(iadc); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 838 | if (rc) { |
| 839 | pr_err("Error checking pmic therm temp\n"); |
| 840 | return rc; |
| 841 | } |
| 842 | |
| 843 | mutex_lock(&iadc->adc->adc_lock); |
| 844 | result->gain_raw = iadc->adc->calib.gain_raw; |
| 845 | result->ideal_gain_nv = QPNP_ADC_GAIN_NV; |
| 846 | result->gain_uv = iadc->adc->calib.gain_uv; |
| 847 | result->offset_raw = iadc->adc->calib.offset_raw; |
| 848 | result->ideal_offset_uv = |
| 849 | QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL; |
| 850 | result->offset_uv = iadc->adc->calib.offset_uv; |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 851 | pr_debug("raw gain:0%x, raw offset:0%x\n", |
| 852 | result->gain_raw, result->offset_raw); |
| 853 | pr_debug("gain_uv:%d offset_uv:%d\n", |
| 854 | result->gain_uv, result->offset_uv); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 855 | mutex_unlock(&iadc->adc->adc_lock); |
| 856 | |
| 857 | return 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 858 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 859 | EXPORT_SYMBOL(qpnp_iadc_get_gain_and_offset); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 860 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 861 | int qpnp_iadc_skip_calibration(struct qpnp_iadc_chip *iadc) |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 862 | { |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 863 | iadc->skip_auto_calibrations = true; |
| 864 | return 0; |
| 865 | } |
| 866 | EXPORT_SYMBOL(qpnp_iadc_skip_calibration); |
| 867 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 868 | int qpnp_iadc_resume_calibration(struct qpnp_iadc_chip *iadc) |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 869 | { |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 870 | iadc->skip_auto_calibrations = false; |
| 871 | return 0; |
| 872 | } |
| 873 | EXPORT_SYMBOL(qpnp_iadc_resume_calibration); |
| 874 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 875 | int32_t qpnp_iadc_vadc_sync_read(struct qpnp_iadc_chip *iadc, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 876 | enum qpnp_iadc_channels i_channel, struct qpnp_iadc_result *i_result, |
| 877 | enum qpnp_vadc_channels v_channel, struct qpnp_vadc_result *v_result) |
| 878 | { |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 879 | int rc = 0; |
| 880 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 881 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 882 | return -EPROBE_DEFER; |
| 883 | |
| 884 | mutex_lock(&iadc->iadc_vadc_lock); |
| 885 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 886 | rc = qpnp_check_pmic_temp(iadc); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 887 | if (rc) { |
| 888 | pr_err("PMIC die temp check failed\n"); |
| 889 | goto fail; |
| 890 | } |
| 891 | |
| 892 | iadc->iadc_mode_sel = true; |
| 893 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 894 | rc = qpnp_vadc_iadc_sync_request(iadc->vadc_dev, v_channel); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 895 | if (rc) { |
| 896 | pr_err("Configuring VADC failed\n"); |
| 897 | goto fail; |
| 898 | } |
| 899 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 900 | rc = qpnp_iadc_read(iadc, i_channel, i_result); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 901 | if (rc) |
| 902 | pr_err("Configuring IADC failed\n"); |
| 903 | /* Intentional fall through to release VADC */ |
| 904 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 905 | rc = qpnp_vadc_iadc_sync_complete_request(iadc->vadc_dev, v_channel, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 906 | v_result); |
| 907 | if (rc) |
| 908 | pr_err("Releasing VADC failed\n"); |
| 909 | fail: |
| 910 | iadc->iadc_mode_sel = false; |
| 911 | |
| 912 | mutex_unlock(&iadc->iadc_vadc_lock); |
| 913 | |
| 914 | return rc; |
| 915 | } |
| 916 | EXPORT_SYMBOL(qpnp_iadc_vadc_sync_read); |
| 917 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 918 | static ssize_t qpnp_iadc_show(struct device *dev, |
| 919 | struct device_attribute *devattr, char *buf) |
| 920 | { |
| 921 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 922 | struct qpnp_iadc_chip *iadc = dev_get_drvdata(dev); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 923 | struct qpnp_iadc_result result; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 924 | int rc = -1; |
| 925 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 926 | rc = qpnp_iadc_read(iadc, attr->index, &result); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 927 | |
| 928 | if (rc) |
| 929 | return 0; |
| 930 | |
| 931 | return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 932 | "Result:%d\n", result.result_ua); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | static struct sensor_device_attribute qpnp_adc_attr = |
| 936 | SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0); |
| 937 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 938 | static int32_t qpnp_iadc_init_hwmon(struct qpnp_iadc_chip *iadc, |
| 939 | struct spmi_device *spmi) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 940 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 941 | struct device_node *child; |
| 942 | struct device_node *node = spmi->dev.of_node; |
| 943 | int rc = 0, i = 0, channel; |
| 944 | |
| 945 | for_each_child_of_node(node, child) { |
| 946 | channel = iadc->adc->adc_channels[i].channel_num; |
| 947 | qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num; |
| 948 | qpnp_adc_attr.dev_attr.attr.name = |
| 949 | iadc->adc->adc_channels[i].name; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 950 | memcpy(&iadc->sens_attr[i], &qpnp_adc_attr, |
| 951 | sizeof(qpnp_adc_attr)); |
Stephen Boyd | 8a5c4e4 | 2012-10-30 11:07:22 -0700 | [diff] [blame] | 952 | sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 953 | rc = device_create_file(&spmi->dev, |
| 954 | &iadc->sens_attr[i].dev_attr); |
| 955 | if (rc) { |
| 956 | dev_err(&spmi->dev, |
| 957 | "device_create_file failed for dev %s\n", |
| 958 | iadc->adc->adc_channels[i].name); |
| 959 | goto hwmon_err_sens; |
| 960 | } |
| 961 | i++; |
| 962 | } |
| 963 | |
| 964 | return 0; |
| 965 | hwmon_err_sens: |
| 966 | pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc); |
| 967 | return rc; |
| 968 | } |
| 969 | |
| 970 | static int __devinit qpnp_iadc_probe(struct spmi_device *spmi) |
| 971 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 972 | struct qpnp_iadc_chip *iadc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 973 | struct qpnp_adc_drv *adc_qpnp; |
| 974 | struct device_node *node = spmi->dev.of_node; |
| 975 | struct device_node *child; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 976 | int rc, count_adc_channel_list = 0, i = 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 977 | |
| 978 | for_each_child_of_node(node, child) |
| 979 | count_adc_channel_list++; |
| 980 | |
| 981 | if (!count_adc_channel_list) { |
| 982 | pr_err("No channel listing\n"); |
| 983 | return -EINVAL; |
| 984 | } |
| 985 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 986 | iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_chip) + |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 987 | (sizeof(struct sensor_device_attribute) * |
| 988 | count_adc_channel_list), GFP_KERNEL); |
| 989 | if (!iadc) { |
| 990 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 991 | return -ENOMEM; |
| 992 | } |
| 993 | |
| 994 | adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv), |
| 995 | GFP_KERNEL); |
| 996 | if (!adc_qpnp) { |
| 997 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 998 | rc = -ENOMEM; |
| 999 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | iadc->adc = adc_qpnp; |
| 1003 | |
| 1004 | rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc); |
| 1005 | if (rc) { |
| 1006 | dev_err(&spmi->dev, "failed to read device tree\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1007 | goto fail; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1010 | iadc->vadc_dev = qpnp_get_vadc(&spmi->dev, "iadc"); |
| 1011 | if (IS_ERR(iadc->vadc_dev)) { |
| 1012 | rc = PTR_ERR(iadc->vadc_dev); |
| 1013 | if (rc != -EPROBE_DEFER) |
| 1014 | pr_err("vadc property missing, rc=%d\n", rc); |
| 1015 | goto fail; |
| 1016 | } |
| 1017 | |
Stephen Boyd | beab450 | 2013-04-25 10:18:17 -0700 | [diff] [blame] | 1018 | mutex_init(&iadc->adc->adc_lock); |
| 1019 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1020 | rc = of_property_read_u32(node, "qcom,rsense", |
| 1021 | &iadc->rsense); |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 1022 | if (rc) |
| 1023 | pr_debug("Defaulting to internal rsense\n"); |
| 1024 | else { |
| 1025 | pr_debug("Use external rsense\n"); |
| 1026 | iadc->external_rsense = true; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame] | 1029 | rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq_eoc, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1030 | qpnp_iadc_isr, |
| 1031 | IRQF_TRIGGER_RISING, "qpnp_iadc_interrupt", iadc); |
| 1032 | if (rc) { |
| 1033 | dev_err(&spmi->dev, "failed to request adc irq\n"); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1034 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1035 | } else |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame] | 1036 | enable_irq_wake(iadc->adc->adc_irq_eoc); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1037 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1038 | rc = qpnp_iadc_init_hwmon(iadc, spmi); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1039 | if (rc) { |
| 1040 | dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n"); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1041 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1042 | } |
| 1043 | iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev); |
| 1044 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1045 | rc = qpnp_iadc_version_check(iadc); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1046 | if (rc) { |
| 1047 | dev_err(&spmi->dev, "IADC version not supported\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1048 | goto fail; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1051 | mutex_init(&iadc->iadc_vadc_lock); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1052 | INIT_WORK(&iadc->trigger_completion_work, qpnp_iadc_trigger_completion); |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1053 | INIT_DELAYED_WORK(&iadc->iadc_work, qpnp_iadc_work); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1054 | rc = qpnp_iadc_comp_info(iadc); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 1055 | if (rc) { |
| 1056 | dev_err(&spmi->dev, "abstracting IADC comp info failed!\n"); |
| 1057 | goto fail; |
| 1058 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1059 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1060 | dev_set_drvdata(&spmi->dev, iadc); |
| 1061 | list_add(&iadc->list, &qpnp_iadc_device_list); |
| 1062 | rc = qpnp_iadc_calibrate_for_trim(iadc, true); |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1063 | if (rc) |
| 1064 | dev_err(&spmi->dev, "failed to calibrate for USR trim\n"); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1065 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1066 | schedule_delayed_work(&iadc->iadc_work, |
| 1067 | round_jiffies_relative(msecs_to_jiffies |
| 1068 | (QPNP_IADC_CALIB_SECONDS))); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1069 | return 0; |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1070 | fail: |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1071 | for_each_child_of_node(node, child) { |
| 1072 | device_remove_file(&spmi->dev, |
| 1073 | &iadc->sens_attr[i].dev_attr); |
| 1074 | i++; |
| 1075 | } |
| 1076 | hwmon_device_unregister(iadc->iadc_hwmon); |
| 1077 | |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1078 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static int __devexit qpnp_iadc_remove(struct spmi_device *spmi) |
| 1082 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1083 | struct qpnp_iadc_chip *iadc = dev_get_drvdata(&spmi->dev); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1084 | struct device_node *node = spmi->dev.of_node; |
| 1085 | struct device_node *child; |
| 1086 | int i = 0; |
| 1087 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1088 | cancel_delayed_work(&iadc->iadc_work); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1089 | mutex_destroy(&iadc->iadc_vadc_lock); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1090 | for_each_child_of_node(node, child) { |
| 1091 | device_remove_file(&spmi->dev, |
| 1092 | &iadc->sens_attr[i].dev_attr); |
| 1093 | i++; |
| 1094 | } |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame^] | 1095 | hwmon_device_unregister(iadc->iadc_hwmon); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1096 | dev_set_drvdata(&spmi->dev, NULL); |
| 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | static const struct of_device_id qpnp_iadc_match_table[] = { |
| 1102 | { .compatible = "qcom,qpnp-iadc", |
| 1103 | }, |
| 1104 | {} |
| 1105 | }; |
| 1106 | |
| 1107 | static struct spmi_driver qpnp_iadc_driver = { |
| 1108 | .driver = { |
| 1109 | .name = "qcom,qpnp-iadc", |
| 1110 | .of_match_table = qpnp_iadc_match_table, |
| 1111 | }, |
| 1112 | .probe = qpnp_iadc_probe, |
| 1113 | .remove = qpnp_iadc_remove, |
| 1114 | }; |
| 1115 | |
| 1116 | static int __init qpnp_iadc_init(void) |
| 1117 | { |
| 1118 | return spmi_driver_register(&qpnp_iadc_driver); |
| 1119 | } |
| 1120 | module_init(qpnp_iadc_init); |
| 1121 | |
| 1122 | static void __exit qpnp_iadc_exit(void) |
| 1123 | { |
| 1124 | spmi_driver_unregister(&qpnp_iadc_driver); |
| 1125 | } |
| 1126 | module_exit(qpnp_iadc_exit); |
| 1127 | |
| 1128 | MODULE_DESCRIPTION("QPNP PMIC current ADC driver"); |
| 1129 | MODULE_LICENSE("GPL v2"); |