Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2014, 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> |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 34 | #include <linux/wakelock.h> |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 35 | |
| 36 | /* QPNP IADC register definition */ |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 37 | #define QPNP_IADC_REVISION1 0x0 |
| 38 | #define QPNP_IADC_REVISION2 0x1 |
| 39 | #define QPNP_IADC_REVISION3 0x2 |
| 40 | #define QPNP_IADC_REVISION4 0x3 |
| 41 | #define QPNP_IADC_PERPH_TYPE 0x4 |
| 42 | #define QPNP_IADC_PERH_SUBTYPE 0x5 |
| 43 | |
| 44 | #define QPNP_IADC_SUPPORTED_REVISION2 1 |
| 45 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 46 | #define QPNP_STATUS1 0x8 |
| 47 | #define QPNP_STATUS1_OP_MODE 4 |
| 48 | #define QPNP_STATUS1_MULTI_MEAS_EN BIT(3) |
| 49 | #define QPNP_STATUS1_MEAS_INTERVAL_EN_STS BIT(2) |
| 50 | #define QPNP_STATUS1_REQ_STS BIT(1) |
| 51 | #define QPNP_STATUS1_EOC BIT(0) |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 52 | #define QPNP_STATUS1_REQ_STS_EOC_MASK 0x3 |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 53 | #define QPNP_STATUS2 0x9 |
| 54 | #define QPNP_STATUS2_CONV_SEQ_STATE_SHIFT 4 |
| 55 | #define QPNP_STATUS2_FIFO_NOT_EMPTY_FLAG BIT(1) |
| 56 | #define QPNP_STATUS2_CONV_SEQ_TIMEOUT_STS BIT(0) |
| 57 | #define QPNP_CONV_TIMEOUT_ERR 2 |
| 58 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 59 | #define QPNP_IADC_MODE_CTL 0x40 |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 60 | #define QPNP_OP_MODE_SHIFT 4 |
| 61 | #define QPNP_USE_BMS_DATA BIT(4) |
| 62 | #define QPNP_VADC_SYNCH_EN BIT(2) |
| 63 | #define QPNP_OFFSET_RMV_EN BIT(1) |
| 64 | #define QPNP_ADC_TRIM_EN BIT(0) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 65 | #define QPNP_IADC_EN_CTL1 0x46 |
| 66 | #define QPNP_IADC_ADC_EN BIT(7) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 67 | #define QPNP_ADC_CH_SEL_CTL 0x48 |
| 68 | #define QPNP_ADC_DIG_PARAM 0x50 |
| 69 | #define QPNP_ADC_CLK_SEL_MASK 0x3 |
| 70 | #define QPNP_ADC_DEC_RATIO_SEL_MASK 0xc |
| 71 | #define QPNP_ADC_DIG_DEC_RATIO_SEL_SHIFT 2 |
| 72 | |
| 73 | #define QPNP_HW_SETTLE_DELAY 0x51 |
| 74 | #define QPNP_CONV_REQ 0x52 |
| 75 | #define QPNP_CONV_REQ_SET BIT(7) |
| 76 | #define QPNP_CONV_SEQ_CTL 0x54 |
| 77 | #define QPNP_CONV_SEQ_HOLDOFF_SHIFT 4 |
| 78 | #define QPNP_CONV_SEQ_TRIG_CTL 0x55 |
| 79 | #define QPNP_FAST_AVG_CTL 0x5a |
| 80 | |
| 81 | #define QPNP_M0_LOW_THR_LSB 0x5c |
| 82 | #define QPNP_M0_LOW_THR_MSB 0x5d |
| 83 | #define QPNP_M0_HIGH_THR_LSB 0x5e |
| 84 | #define QPNP_M0_HIGH_THR_MSB 0x5f |
| 85 | #define QPNP_M1_LOW_THR_LSB 0x69 |
| 86 | #define QPNP_M1_LOW_THR_MSB 0x6a |
| 87 | #define QPNP_M1_HIGH_THR_LSB 0x6b |
| 88 | #define QPNP_M1_HIGH_THR_MSB 0x6c |
| 89 | |
| 90 | #define QPNP_DATA0 0x60 |
| 91 | #define QPNP_DATA1 0x61 |
| 92 | #define QPNP_CONV_TIMEOUT_ERR 2 |
| 93 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 94 | #define QPNP_IADC_SEC_ACCESS 0xD0 |
| 95 | #define QPNP_IADC_SEC_ACCESS_DATA 0xA5 |
| 96 | #define QPNP_IADC_MSB_OFFSET 0xF2 |
| 97 | #define QPNP_IADC_LSB_OFFSET 0xF3 |
| 98 | #define QPNP_IADC_NOMINAL_RSENSE 0xF4 |
| 99 | #define QPNP_IADC_ATE_GAIN_CALIB_OFFSET 0xF5 |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 100 | #define QPNP_INT_TEST_VAL 0xE1 |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 101 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 102 | #define QPNP_IADC_ADC_CH_SEL_CTL 0x48 |
| 103 | #define QPNP_IADC_ADC_CHX_SEL_SHIFT 3 |
| 104 | |
| 105 | #define QPNP_IADC_ADC_DIG_PARAM 0x50 |
| 106 | #define QPNP_IADC_CLK_SEL_SHIFT 1 |
| 107 | #define QPNP_IADC_DEC_RATIO_SEL 3 |
| 108 | |
| 109 | #define QPNP_IADC_CONV_REQUEST 0x52 |
| 110 | #define QPNP_IADC_CONV_REQ BIT(7) |
| 111 | |
| 112 | #define QPNP_IADC_DATA0 0x60 |
| 113 | #define QPNP_IADC_DATA1 0x61 |
| 114 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 115 | #define QPNP_ADC_CONV_TIME_MIN 2000 |
| 116 | #define QPNP_ADC_CONV_TIME_MAX 2100 |
| 117 | #define QPNP_ADC_ERR_COUNT 20 |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 118 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 119 | #define QPNP_ADC_GAIN_NV 17857 |
| 120 | #define QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL 0 |
| 121 | #define QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR 10000000 |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 122 | #define QPNP_IADC_NANO_VOLTS_FACTOR 1000000 |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 123 | #define QPNP_IADC_CALIB_SECONDS 300000 |
| 124 | #define QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT 15625 |
| 125 | #define QPNP_IADC_DIE_TEMP_CALIB_OFFSET 5000 |
| 126 | |
| 127 | #define QPNP_RAW_CODE_16_BIT_MSB_MASK 0xff00 |
| 128 | #define QPNP_RAW_CODE_16_BIT_LSB_MASK 0xff |
| 129 | #define QPNP_BIT_SHIFT_8 8 |
| 130 | #define QPNP_RSENSE_MSB_SIGN_CHECK 0x80 |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 131 | #define QPNP_ADC_COMPLETION_TIMEOUT HZ |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 132 | #define SMBB_BAT_IF_TRIM_CNST_RDS_MASK 0x7 |
| 133 | #define SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST 2 |
| 134 | #define QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST 127 |
| 135 | #define QPNP_IADC_RSENSE_DEFAULT_VALUE 7800000 |
| 136 | #define QPNP_IADC_RSENSE_DEFAULT_TYPEB_GF 9000000 |
| 137 | #define QPNP_IADC_RSENSE_DEFAULT_TYPEB_SMIC 9700000 |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 138 | |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 139 | struct qpnp_iadc_comp { |
| 140 | bool ext_rsense; |
| 141 | u8 id; |
| 142 | u8 sys_gain; |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 143 | u8 revision_dig_major; |
| 144 | u8 revision_ana_minor; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 147 | struct qpnp_iadc_chip { |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 148 | struct device *dev; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 149 | struct qpnp_adc_drv *adc; |
| 150 | int32_t rsense; |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 151 | bool external_rsense; |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 152 | bool default_internal_rsense; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 153 | struct device *iadc_hwmon; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 154 | struct list_head list; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 155 | int64_t die_temp; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 156 | struct delayed_work iadc_work; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 157 | bool iadc_mode_sel; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 158 | struct qpnp_iadc_comp iadc_comp; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 159 | struct qpnp_vadc_chip *vadc_dev; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 160 | struct work_struct trigger_completion_work; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 161 | bool skip_auto_calibrations; |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 162 | bool iadc_poll_eoc; |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 163 | u16 batt_id_trim_cnst_rds; |
| 164 | int rds_trim_default_type; |
| 165 | bool rds_trim_default_check; |
| 166 | int32_t rsense_workaround_value; |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 167 | struct sensor_device_attribute sens_attr[0]; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 170 | LIST_HEAD(qpnp_iadc_device_list); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 171 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 172 | enum qpnp_iadc_rsense_rds_workaround { |
| 173 | QPNP_IADC_RDS_DEFAULT_TYPEA, |
| 174 | QPNP_IADC_RDS_DEFAULT_TYPEB, |
| 175 | }; |
| 176 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 177 | static int32_t qpnp_iadc_read_reg(struct qpnp_iadc_chip *iadc, |
| 178 | uint32_t reg, u8 *data) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 179 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 180 | int rc; |
| 181 | |
| 182 | rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 183 | (iadc->adc->offset + reg), data, 1); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 184 | if (rc < 0) { |
| 185 | pr_err("qpnp iadc read reg %d failed with %d\n", reg, rc); |
| 186 | return rc; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 192 | static int32_t qpnp_iadc_write_reg(struct qpnp_iadc_chip *iadc, |
| 193 | uint32_t reg, u8 data) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 194 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 195 | int rc; |
| 196 | u8 *buf; |
| 197 | |
| 198 | buf = &data; |
| 199 | rc = spmi_ext_register_writel(iadc->adc->spmi->ctrl, iadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 200 | (iadc->adc->offset + reg), buf, 1); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 201 | if (rc < 0) { |
| 202 | pr_err("qpnp iadc write reg %d failed with %d\n", reg, rc); |
| 203 | return rc; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 209 | static int qpnp_iadc_is_valid(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 210 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 211 | struct qpnp_iadc_chip *iadc_chip = NULL; |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 212 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 213 | list_for_each_entry(iadc_chip, &qpnp_iadc_device_list, list) |
| 214 | if (iadc == iadc_chip) |
| 215 | return 0; |
| 216 | |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | static void qpnp_iadc_trigger_completion(struct work_struct *work) |
| 221 | { |
| 222 | struct qpnp_iadc_chip *iadc = container_of(work, |
| 223 | struct qpnp_iadc_chip, trigger_completion_work); |
| 224 | |
| 225 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 226 | return; |
| 227 | |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 228 | complete(&iadc->adc->adc_rslt_completion); |
| 229 | |
| 230 | return; |
| 231 | } |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 232 | |
| 233 | static irqreturn_t qpnp_iadc_isr(int irq, void *dev_id) |
| 234 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 235 | struct qpnp_iadc_chip *iadc = dev_id; |
| 236 | |
| 237 | schedule_work(&iadc->trigger_completion_work); |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 238 | |
| 239 | return IRQ_HANDLED; |
| 240 | } |
| 241 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 242 | 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] | 243 | { |
| 244 | int rc = 0; |
| 245 | u8 data = 0; |
| 246 | |
| 247 | data = QPNP_IADC_ADC_EN; |
| 248 | if (state) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 249 | rc = qpnp_iadc_write_reg(dev, QPNP_IADC_EN_CTL1, |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 250 | data); |
| 251 | if (rc < 0) { |
| 252 | pr_err("IADC enable failed\n"); |
| 253 | return rc; |
| 254 | } |
| 255 | } else { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 256 | rc = qpnp_iadc_write_reg(dev, QPNP_IADC_EN_CTL1, |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 257 | (~data & QPNP_IADC_ADC_EN)); |
| 258 | if (rc < 0) { |
| 259 | pr_err("IADC disable failed\n"); |
| 260 | return rc; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 267 | static int32_t qpnp_iadc_status_debug(struct qpnp_iadc_chip *dev) |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 268 | { |
| 269 | int rc = 0; |
| 270 | u8 mode = 0, status1 = 0, chan = 0, dig = 0, en = 0; |
| 271 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 272 | rc = qpnp_iadc_read_reg(dev, QPNP_IADC_MODE_CTL, &mode); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 273 | if (rc < 0) { |
| 274 | pr_err("mode ctl register read failed with %d\n", rc); |
| 275 | return rc; |
| 276 | } |
| 277 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 278 | rc = qpnp_iadc_read_reg(dev, QPNP_ADC_DIG_PARAM, &dig); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 279 | if (rc < 0) { |
| 280 | pr_err("digital param read failed with %d\n", rc); |
| 281 | return rc; |
| 282 | } |
| 283 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 284 | 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] | 285 | if (rc < 0) { |
| 286 | pr_err("channel read failed with %d\n", rc); |
| 287 | return rc; |
| 288 | } |
| 289 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 290 | rc = qpnp_iadc_read_reg(dev, QPNP_STATUS1, &status1); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 291 | if (rc < 0) { |
| 292 | pr_err("status1 read failed with %d\n", rc); |
| 293 | return rc; |
| 294 | } |
| 295 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 296 | rc = qpnp_iadc_read_reg(dev, QPNP_IADC_EN_CTL1, &en); |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 297 | if (rc < 0) { |
| 298 | pr_err("en read failed with %d\n", rc); |
| 299 | return rc; |
| 300 | } |
| 301 | |
Siddartha Mohanadoss | 73ae69b | 2013-04-03 17:34:03 -0700 | [diff] [blame] | 302 | 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] | 303 | status1, dig, chan, mode, en); |
| 304 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 305 | rc = qpnp_iadc_enable(dev, false); |
Siddartha Mohanadoss | e236359 | 2012-12-14 18:59:01 -0800 | [diff] [blame] | 306 | if (rc < 0) { |
| 307 | pr_err("IADC disable failed with %d\n", rc); |
| 308 | return rc; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 314 | static int32_t qpnp_iadc_read_conversion_result(struct qpnp_iadc_chip *iadc, |
| 315 | int16_t *data) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 316 | { |
| 317 | uint8_t rslt_lsb, rslt_msb; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 318 | uint16_t rslt; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 319 | int32_t rc; |
| 320 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 321 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_DATA0, &rslt_lsb); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 322 | if (rc < 0) { |
| 323 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 324 | return rc; |
| 325 | } |
| 326 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 327 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_DATA1, &rslt_msb); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 328 | if (rc < 0) { |
| 329 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 330 | return rc; |
| 331 | } |
| 332 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 333 | rslt = (rslt_msb << 8) | rslt_lsb; |
| 334 | *data = rslt; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 335 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 336 | rc = qpnp_iadc_enable(iadc, false); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 337 | if (rc) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 338 | return rc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 339 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 343 | #define QPNP_IADC_PM8941_3_1_REV2 3 |
| 344 | #define QPNP_IADC_PM8941_3_1_REV3 2 |
| 345 | #define QPNP_IADC_PM8026_1_REV2 1 |
| 346 | #define QPNP_IADC_PM8026_1_REV3 2 |
| 347 | #define QPNP_IADC_PM8026_2_REV2 4 |
| 348 | #define QPNP_IADC_PM8026_2_REV3 2 |
| 349 | #define QPNP_IADC_PM8110_1_REV2 2 |
| 350 | #define QPNP_IADC_PM8110_1_REV3 2 |
| 351 | |
| 352 | #define QPNP_IADC_REV_ID_8941_3_1 1 |
| 353 | #define QPNP_IADC_REV_ID_8026_1_0 2 |
| 354 | #define QPNP_IADC_REV_ID_8026_2_0 3 |
| 355 | #define QPNP_IADC_REV_ID_8110_1_0 4 |
| 356 | |
| 357 | static void qpnp_temp_comp_version_check(struct qpnp_iadc_chip *iadc, |
| 358 | int32_t *version) |
| 359 | { |
| 360 | if ((iadc->iadc_comp.revision_dig_major == |
| 361 | QPNP_IADC_PM8941_3_1_REV2) && |
| 362 | (iadc->iadc_comp.revision_ana_minor == |
| 363 | QPNP_IADC_PM8941_3_1_REV3)) |
| 364 | *version = QPNP_IADC_REV_ID_8941_3_1; |
| 365 | else if ((iadc->iadc_comp.revision_dig_major == |
| 366 | QPNP_IADC_PM8026_1_REV2) && |
| 367 | (iadc->iadc_comp.revision_ana_minor == |
| 368 | QPNP_IADC_PM8026_1_REV3)) |
| 369 | *version = QPNP_IADC_REV_ID_8026_1_0; |
| 370 | else if ((iadc->iadc_comp.revision_dig_major == |
| 371 | QPNP_IADC_PM8026_2_REV2) && |
| 372 | (iadc->iadc_comp.revision_ana_minor == |
| 373 | QPNP_IADC_PM8026_2_REV3)) |
| 374 | *version = QPNP_IADC_REV_ID_8026_2_0; |
| 375 | else if ((iadc->iadc_comp.revision_dig_major == |
| 376 | QPNP_IADC_PM8110_1_REV2) && |
| 377 | (iadc->iadc_comp.revision_ana_minor == |
| 378 | QPNP_IADC_PM8110_1_REV3)) |
| 379 | *version = QPNP_IADC_REV_ID_8110_1_0; |
| 380 | else |
| 381 | *version = -EINVAL; |
| 382 | |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | #define QPNP_COEFF_1 969000 |
| 387 | #define QPNP_COEFF_2 32 |
| 388 | #define QPNP_COEFF_3_TYPEA 1700000 |
| 389 | #define QPNP_COEFF_3_TYPEB 1000000 |
| 390 | #define QPNP_COEFF_4 100 |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 391 | #define QPNP_COEFF_5 15 |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 392 | #define QPNP_COEFF_6 100000 |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 393 | #define QPNP_COEFF_7 21 |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 394 | #define QPNP_COEFF_8 100000000 |
| 395 | #define QPNP_COEFF_9 38 |
| 396 | #define QPNP_COEFF_10 40 |
| 397 | #define QPNP_COEFF_11 7 |
| 398 | #define QPNP_COEFF_12 11 |
| 399 | #define QPNP_COEFF_13 37 |
| 400 | #define QPNP_COEFF_14 39 |
| 401 | #define QPNP_COEFF_15 9 |
| 402 | #define QPNP_COEFF_16 11 |
| 403 | #define QPNP_COEFF_17 851200 |
| 404 | #define QPNP_COEFF_18 296500 |
| 405 | #define QPNP_COEFF_19 222400 |
| 406 | #define QPNP_COEFF_20 813800 |
| 407 | #define QPNP_COEFF_21 1059100 |
| 408 | #define QPNP_COEFF_22 5000000 |
| 409 | #define QPNP_COEFF_23 3722500 |
| 410 | #define QPNP_COEFF_24 84 |
| 411 | |
| 412 | static int32_t qpnp_iadc_comp(int64_t *result, struct qpnp_iadc_chip *iadc, |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 413 | int64_t die_temp) |
| 414 | { |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 415 | int64_t temp_var = 0, sys_gain_coeff = 0, old; |
| 416 | int32_t coeff_a = 0, coeff_b = 0; |
| 417 | int32_t version; |
| 418 | |
| 419 | qpnp_temp_comp_version_check(iadc, &version); |
| 420 | if (version == -EINVAL) |
| 421 | return 0; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 422 | |
Xiaozhe Shi | 62ad5e1 | 2013-05-13 12:37:41 -0700 | [diff] [blame] | 423 | old = *result; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 424 | *result = *result * 1000000; |
| 425 | |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 426 | if (iadc->iadc_comp.sys_gain > 127) |
| 427 | sys_gain_coeff = -QPNP_COEFF_6 * |
| 428 | (iadc->iadc_comp.sys_gain - 128); |
| 429 | else |
| 430 | sys_gain_coeff = QPNP_COEFF_6 * |
| 431 | iadc->iadc_comp.sys_gain; |
| 432 | |
| 433 | switch (version) { |
| 434 | case QPNP_IADC_REV_ID_8941_3_1: |
| 435 | switch (iadc->iadc_comp.id) { |
| 436 | case COMP_ID_GF: |
| 437 | if (!iadc->iadc_comp.ext_rsense) { |
| 438 | /* internal rsense */ |
| 439 | coeff_a = QPNP_COEFF_2; |
| 440 | coeff_b = -QPNP_COEFF_3_TYPEA; |
| 441 | } else { |
| 442 | if (*result < 0) { |
| 443 | /* charge */ |
| 444 | coeff_a = QPNP_COEFF_5; |
| 445 | coeff_b = QPNP_COEFF_6; |
| 446 | } else { |
| 447 | /* discharge */ |
| 448 | coeff_a = -QPNP_COEFF_7; |
| 449 | coeff_b = QPNP_COEFF_6; |
| 450 | } |
| 451 | } |
| 452 | break; |
| 453 | case COMP_ID_TSMC: |
| 454 | default: |
| 455 | if (!iadc->iadc_comp.ext_rsense) { |
| 456 | /* internal rsense */ |
| 457 | coeff_a = QPNP_COEFF_2; |
| 458 | coeff_b = -QPNP_COEFF_3_TYPEB; |
| 459 | } else { |
| 460 | if (*result < 0) { |
| 461 | /* charge */ |
| 462 | coeff_a = QPNP_COEFF_5; |
| 463 | coeff_b = QPNP_COEFF_6; |
| 464 | } else { |
| 465 | /* discharge */ |
| 466 | coeff_a = -QPNP_COEFF_7; |
| 467 | coeff_b = QPNP_COEFF_6; |
| 468 | } |
| 469 | } |
| 470 | break; |
| 471 | } |
| 472 | break; |
| 473 | case QPNP_IADC_REV_ID_8026_1_0: |
| 474 | /* pm8026 rev 1.0 */ |
| 475 | switch (iadc->iadc_comp.id) { |
| 476 | case COMP_ID_GF: |
| 477 | if (!iadc->iadc_comp.ext_rsense) { |
| 478 | /* internal rsense */ |
| 479 | if (*result < 0) { |
| 480 | /* charge */ |
| 481 | coeff_a = QPNP_COEFF_9; |
| 482 | coeff_b = -QPNP_COEFF_17; |
| 483 | } else { |
| 484 | coeff_a = QPNP_COEFF_10; |
| 485 | coeff_b = QPNP_COEFF_18; |
| 486 | } |
| 487 | } else { |
| 488 | if (*result < 0) { |
| 489 | /* charge */ |
| 490 | coeff_a = -QPNP_COEFF_11; |
| 491 | coeff_b = 0; |
| 492 | } else { |
| 493 | /* discharge */ |
| 494 | coeff_a = -QPNP_COEFF_17; |
| 495 | coeff_b = -QPNP_COEFF_19; |
| 496 | } |
| 497 | } |
| 498 | break; |
| 499 | case COMP_ID_TSMC: |
| 500 | default: |
| 501 | if (!iadc->iadc_comp.ext_rsense) { |
| 502 | /* internal rsense */ |
| 503 | if (*result < 0) { |
| 504 | /* charge */ |
| 505 | coeff_a = QPNP_COEFF_13; |
| 506 | coeff_b = -QPNP_COEFF_20; |
| 507 | } else { |
| 508 | coeff_a = QPNP_COEFF_14; |
| 509 | coeff_b = QPNP_COEFF_21; |
| 510 | } |
| 511 | } else { |
| 512 | if (*result < 0) { |
| 513 | /* charge */ |
| 514 | coeff_a = -QPNP_COEFF_15; |
| 515 | coeff_b = 0; |
| 516 | } else { |
| 517 | /* discharge */ |
| 518 | coeff_a = -QPNP_COEFF_12; |
| 519 | coeff_b = -QPNP_COEFF_19; |
| 520 | } |
| 521 | } |
| 522 | break; |
| 523 | } |
| 524 | break; |
| 525 | case QPNP_IADC_REV_ID_8110_1_0: |
| 526 | /* pm8110 rev 1.0 */ |
| 527 | switch (iadc->iadc_comp.id) { |
| 528 | case COMP_ID_GF: |
| 529 | if (!iadc->iadc_comp.ext_rsense) { |
| 530 | /* internal rsense */ |
| 531 | if (*result < 0) { |
| 532 | /* charge */ |
| 533 | coeff_a = QPNP_COEFF_24; |
| 534 | coeff_b = -QPNP_COEFF_22; |
| 535 | } else { |
| 536 | coeff_a = QPNP_COEFF_24; |
| 537 | coeff_b = -QPNP_COEFF_23; |
| 538 | } |
| 539 | } |
| 540 | break; |
| 541 | case COMP_ID_SMIC: |
| 542 | default: |
| 543 | if (!iadc->iadc_comp.ext_rsense) { |
| 544 | /* internal rsense */ |
| 545 | if (*result < 0) { |
| 546 | /* charge */ |
| 547 | coeff_a = QPNP_COEFF_24; |
| 548 | coeff_b = -QPNP_COEFF_22; |
| 549 | } else { |
| 550 | coeff_a = QPNP_COEFF_24; |
| 551 | coeff_b = -QPNP_COEFF_23; |
| 552 | } |
| 553 | } |
| 554 | break; |
| 555 | } |
| 556 | break; |
| 557 | default: |
| 558 | case QPNP_IADC_REV_ID_8026_2_0: |
| 559 | /* pm8026 rev 1.0 */ |
| 560 | coeff_a = 0; |
| 561 | coeff_b = 0; |
| 562 | break; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 565 | temp_var = (coeff_a * die_temp) + coeff_b; |
| 566 | temp_var = div64_s64(temp_var, QPNP_COEFF_4); |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 567 | temp_var = 1000 * (1000000 - temp_var); |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 568 | |
| 569 | if (!iadc->iadc_comp.ext_rsense) { |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 570 | /* internal rsense */ |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 571 | *result = div64_s64(*result * 1000, temp_var); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 574 | if (iadc->iadc_comp.ext_rsense) { |
| 575 | /* external rsense */ |
| 576 | sys_gain_coeff = (1000000 + |
| 577 | div64_s64(sys_gain_coeff, QPNP_COEFF_4)); |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 578 | temp_var = div64_s64(temp_var * sys_gain_coeff, 1000000); |
Siddartha Mohanadoss | 54bbfce | 2013-08-21 22:02:51 -0700 | [diff] [blame] | 579 | *result = div64_s64(*result * 1000, temp_var); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 580 | } |
Xiaozhe Shi | 62ad5e1 | 2013-05-13 12:37:41 -0700 | [diff] [blame] | 581 | pr_debug("%lld compensated into %lld\n", old, *result); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 586 | 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] | 587 | { |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 588 | return qpnp_iadc_comp(result, iadc, iadc->die_temp); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 589 | } |
| 590 | EXPORT_SYMBOL(qpnp_iadc_comp_result); |
| 591 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 592 | static int qpnp_iadc_rds_trim_update_check(struct qpnp_iadc_chip *iadc) |
| 593 | { |
| 594 | int rc = 0; |
| 595 | u8 trim2_val = 0, smbb_batt_trm_data = 0; |
| 596 | |
| 597 | if (!iadc->rds_trim_default_check) { |
| 598 | pr_debug("No internal rds trim check needed\n"); |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_NOMINAL_RSENSE, &trim2_val); |
| 603 | if (rc < 0) { |
| 604 | pr_err("qpnp adc trim2_fullscale1 reg read failed %d\n", rc); |
| 605 | return rc; |
| 606 | } |
| 607 | |
| 608 | rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave, |
| 609 | iadc->batt_id_trim_cnst_rds, &smbb_batt_trm_data, 1); |
| 610 | if (rc < 0) { |
| 611 | pr_err("batt_id trim_cnst rds reg read failed %d\n", rc); |
| 612 | return rc; |
| 613 | } |
| 614 | |
| 615 | pr_debug("n_trim:0x%x smb_trm:0x%x\n", trim2_val, smbb_batt_trm_data); |
| 616 | |
| 617 | if (iadc->rds_trim_default_type == QPNP_IADC_RDS_DEFAULT_TYPEA) { |
| 618 | if (((smbb_batt_trm_data & SMBB_BAT_IF_TRIM_CNST_RDS_MASK) == |
| 619 | SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST) && |
| 620 | (trim2_val == QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) { |
| 621 | iadc->rsense_workaround_value = |
| 622 | QPNP_IADC_RSENSE_DEFAULT_VALUE; |
| 623 | iadc->default_internal_rsense = true; |
| 624 | } |
| 625 | } else if (iadc->rds_trim_default_type == |
| 626 | QPNP_IADC_RDS_DEFAULT_TYPEB) { |
| 627 | if (((smbb_batt_trm_data & SMBB_BAT_IF_TRIM_CNST_RDS_MASK) >= |
| 628 | SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST) && |
| 629 | (trim2_val == QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) { |
| 630 | iadc->rsense_workaround_value = |
| 631 | QPNP_IADC_RSENSE_DEFAULT_VALUE; |
| 632 | iadc->default_internal_rsense = true; |
| 633 | } else if (((smbb_batt_trm_data & |
| 634 | SMBB_BAT_IF_TRIM_CNST_RDS_MASK) |
| 635 | < SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST) && |
| 636 | (trim2_val == |
| 637 | QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) { |
| 638 | if (iadc->iadc_comp.id == COMP_ID_GF) { |
| 639 | iadc->rsense_workaround_value = |
| 640 | QPNP_IADC_RSENSE_DEFAULT_TYPEB_GF; |
| 641 | iadc->default_internal_rsense = true; |
| 642 | } else if (iadc->iadc_comp.id == COMP_ID_SMIC) { |
| 643 | iadc->rsense_workaround_value = |
| 644 | QPNP_IADC_RSENSE_DEFAULT_TYPEB_SMIC; |
| 645 | iadc->default_internal_rsense = true; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 653 | static int32_t qpnp_iadc_comp_info(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 654 | { |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 655 | int rc = 0; |
| 656 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 657 | 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] | 658 | if (rc < 0) { |
| 659 | pr_err("qpnp adc comp id failed with %d\n", rc); |
| 660 | return rc; |
| 661 | } |
| 662 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 663 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION2, |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 664 | &iadc->iadc_comp.revision_dig_major); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 665 | if (rc < 0) { |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 666 | pr_err("qpnp adc revision2 read failed with %d\n", rc); |
| 667 | return rc; |
| 668 | } |
| 669 | |
| 670 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION3, |
| 671 | &iadc->iadc_comp.revision_ana_minor); |
| 672 | if (rc < 0) { |
| 673 | pr_err("qpnp adc revision3 read failed with %d\n", rc); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 674 | return rc; |
| 675 | } |
| 676 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 677 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_ATE_GAIN_CALIB_OFFSET, |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 678 | &iadc->iadc_comp.sys_gain); |
Siddartha Mohanadoss | 7bea844 | 2013-06-17 17:50:22 -0700 | [diff] [blame] | 679 | if (rc < 0) { |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 680 | pr_err("full scale read failed with %d\n", rc); |
Siddartha Mohanadoss | 7bea844 | 2013-06-17 17:50:22 -0700 | [diff] [blame] | 681 | return rc; |
| 682 | } |
| 683 | |
| 684 | if (iadc->external_rsense) |
| 685 | iadc->iadc_comp.ext_rsense = true; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 686 | |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 687 | pr_debug("fab id = %u, revision_dig_major = %u, revision_ana_minor = %u sys gain = %u, external_rsense = %d\n", |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 688 | iadc->iadc_comp.id, |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 689 | iadc->iadc_comp.revision_dig_major, |
| 690 | iadc->iadc_comp.revision_ana_minor, |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 691 | iadc->iadc_comp.sys_gain, |
| 692 | iadc->iadc_comp.ext_rsense); |
| 693 | return rc; |
| 694 | } |
| 695 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 696 | static int32_t qpnp_iadc_configure(struct qpnp_iadc_chip *iadc, |
| 697 | enum qpnp_iadc_channels channel, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 698 | uint16_t *raw_code, uint32_t mode_sel) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 699 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 700 | u8 qpnp_iadc_mode_reg = 0, qpnp_iadc_ch_sel_reg = 0; |
| 701 | u8 qpnp_iadc_conv_req = 0, qpnp_iadc_dig_param_reg = 0; |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 702 | u8 status1 = 0; |
| 703 | uint32_t count = 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 704 | int32_t rc = 0; |
| 705 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 706 | qpnp_iadc_ch_sel_reg = channel; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 707 | |
| 708 | qpnp_iadc_dig_param_reg |= iadc->adc->amux_prop->decimation << |
| 709 | QPNP_IADC_DEC_RATIO_SEL; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 710 | if (iadc->iadc_mode_sel) |
| 711 | qpnp_iadc_mode_reg |= (QPNP_ADC_TRIM_EN | QPNP_VADC_SYNCH_EN); |
| 712 | else |
| 713 | qpnp_iadc_mode_reg |= QPNP_ADC_TRIM_EN; |
| 714 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 715 | qpnp_iadc_conv_req = QPNP_IADC_CONV_REQ; |
| 716 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 717 | 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] | 718 | if (rc) { |
| 719 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 720 | return rc; |
| 721 | } |
| 722 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 723 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_ADC_CH_SEL_CTL, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 724 | qpnp_iadc_ch_sel_reg); |
| 725 | if (rc) { |
| 726 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 727 | return rc; |
| 728 | } |
| 729 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 730 | rc = qpnp_iadc_write_reg(iadc, QPNP_ADC_DIG_PARAM, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 731 | qpnp_iadc_dig_param_reg); |
| 732 | if (rc) { |
| 733 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 734 | return rc; |
| 735 | } |
| 736 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 737 | rc = qpnp_iadc_write_reg(iadc, QPNP_HW_SETTLE_DELAY, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 738 | iadc->adc->amux_prop->hw_settle_time); |
| 739 | if (rc < 0) { |
| 740 | pr_err("qpnp adc configure error for hw settling time setup\n"); |
| 741 | return rc; |
| 742 | } |
| 743 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 744 | rc = qpnp_iadc_write_reg(iadc, QPNP_FAST_AVG_CTL, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 745 | iadc->adc->amux_prop->fast_avg_setup); |
| 746 | if (rc < 0) { |
| 747 | pr_err("qpnp adc fast averaging configure error\n"); |
| 748 | return rc; |
| 749 | } |
| 750 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 751 | if (!iadc->iadc_poll_eoc) |
| 752 | INIT_COMPLETION(iadc->adc->adc_rslt_completion); |
Siddartha Mohanadoss | 3f219c4 | 2013-04-02 11:01:28 -0700 | [diff] [blame] | 753 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 754 | rc = qpnp_iadc_enable(iadc, true); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 755 | if (rc) |
| 756 | return rc; |
| 757 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 758 | 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] | 759 | if (rc) { |
| 760 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 761 | return rc; |
| 762 | } |
| 763 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 764 | if (iadc->iadc_poll_eoc) { |
| 765 | while (status1 != QPNP_STATUS1_EOC) { |
| 766 | rc = qpnp_iadc_read_reg(iadc, QPNP_STATUS1, &status1); |
| 767 | if (rc < 0) |
| 768 | return rc; |
| 769 | status1 &= QPNP_STATUS1_REQ_STS_EOC_MASK; |
| 770 | usleep_range(QPNP_ADC_CONV_TIME_MIN, |
| 771 | QPNP_ADC_CONV_TIME_MAX); |
| 772 | count++; |
| 773 | if (count > QPNP_ADC_ERR_COUNT) { |
| 774 | pr_err("retry error exceeded\n"); |
| 775 | rc = qpnp_iadc_status_debug(iadc); |
| 776 | if (rc < 0) |
| 777 | pr_err("IADC status debug failed\n"); |
| 778 | rc = -EINVAL; |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 779 | return rc; |
| 780 | } |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 781 | } |
| 782 | } else { |
| 783 | rc = wait_for_completion_timeout( |
| 784 | &iadc->adc->adc_rslt_completion, |
| 785 | QPNP_ADC_COMPLETION_TIMEOUT); |
| 786 | if (!rc) { |
| 787 | rc = qpnp_iadc_read_reg(iadc, QPNP_STATUS1, &status1); |
| 788 | if (rc < 0) |
| 789 | return rc; |
| 790 | status1 &= QPNP_STATUS1_REQ_STS_EOC_MASK; |
| 791 | if (status1 == QPNP_STATUS1_EOC) |
| 792 | pr_debug("End of conversion status set\n"); |
| 793 | else { |
| 794 | rc = qpnp_iadc_status_debug(iadc); |
| 795 | if (rc < 0) { |
| 796 | pr_err("status debug failed %d\n", rc); |
| 797 | return rc; |
| 798 | } |
| 799 | return -EINVAL; |
| 800 | } |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 804 | rc = qpnp_iadc_read_conversion_result(iadc, raw_code); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 805 | if (rc) { |
| 806 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 807 | return rc; |
| 808 | } |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 813 | #define IADC_CENTER 0xC000 |
| 814 | #define IADC_READING_RESOLUTION_N 542535 |
| 815 | #define IADC_READING_RESOLUTION_D 100000 |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 816 | 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] | 817 | { |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 818 | s64 numerator; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 819 | |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 820 | if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) { |
| 821 | pr_err("raw offset errors! raw_gain:0x%x and raw_offset:0x%x\n", |
| 822 | iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw); |
| 823 | return -EINVAL; |
| 824 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 825 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 826 | numerator = iadc->adc->calib.offset_raw - IADC_CENTER; |
| 827 | numerator *= IADC_READING_RESOLUTION_N; |
| 828 | iadc->adc->calib.offset_uv = div_s64(numerator, |
| 829 | IADC_READING_RESOLUTION_D); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 830 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 831 | numerator = iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw; |
| 832 | numerator *= IADC_READING_RESOLUTION_N; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 833 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 834 | iadc->adc->calib.gain_uv = div_s64(numerator, |
| 835 | IADC_READING_RESOLUTION_D); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 836 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 837 | pr_debug("gain_uv:%d offset_uv:%d\n", |
| 838 | iadc->adc->calib.gain_uv, iadc->adc->calib.offset_uv); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 839 | return 0; |
| 840 | } |
| 841 | |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 842 | #define IADC_IDEAL_RAW_GAIN 3291 |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 843 | int32_t qpnp_iadc_calibrate_for_trim(struct qpnp_iadc_chip *iadc, |
| 844 | bool batfet_closed) |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 845 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 846 | uint8_t rslt_lsb, rslt_msb; |
| 847 | int32_t rc = 0; |
| 848 | uint16_t raw_data; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 849 | uint32_t mode_sel = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 850 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 851 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 852 | return -EPROBE_DEFER; |
| 853 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 854 | mutex_lock(&iadc->adc->adc_lock); |
| 855 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 856 | if (iadc->iadc_poll_eoc) { |
| 857 | pr_debug("acquiring iadc eoc wakelock\n"); |
| 858 | pm_stay_awake(iadc->dev); |
| 859 | } |
| 860 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 861 | rc = qpnp_iadc_configure(iadc, GAIN_CALIBRATION_17P857MV, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 862 | &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 863 | if (rc < 0) { |
| 864 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 865 | goto fail; |
| 866 | } |
| 867 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 868 | iadc->adc->calib.gain_raw = raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 869 | |
Abhijeet Dharmapurikar | 0ef9b5c | 2013-07-15 18:24:38 -0700 | [diff] [blame] | 870 | /* |
| 871 | * there is a features in the BMS where if the batfet is opened |
| 872 | * the BMS reads from INTERNAL_RSENSE (channel 0) actually go to |
| 873 | * OFFSET_CALIBRATION_CSP_CSN (channel 5). Hence if batfet is opened |
| 874 | * we have to calibrate based on OFFSET_CALIBRATION_CSP_CSN even for |
| 875 | * internal rsense. |
| 876 | */ |
| 877 | if (!batfet_closed || iadc->external_rsense) { |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 878 | /* external offset calculation */ |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 879 | rc = qpnp_iadc_configure(iadc, OFFSET_CALIBRATION_CSP_CSN, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 880 | &raw_data, mode_sel); |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 881 | if (rc < 0) { |
| 882 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 883 | goto fail; |
| 884 | } |
| 885 | } else { |
| 886 | /* internal offset calculation */ |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 887 | rc = qpnp_iadc_configure(iadc, OFFSET_CALIBRATION_CSP2_CSN2, |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 888 | &raw_data, mode_sel); |
| 889 | if (rc < 0) { |
| 890 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 891 | goto fail; |
| 892 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 895 | iadc->adc->calib.offset_raw = raw_data; |
| 896 | if (rc < 0) { |
| 897 | pr_err("qpnp adc offset/gain calculation failed\n"); |
| 898 | goto fail; |
| 899 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 900 | |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 901 | if (iadc->iadc_comp.revision_dig_major == QPNP_IADC_PM8026_2_REV2 |
| 902 | && iadc->iadc_comp.revision_ana_minor == |
| 903 | QPNP_IADC_PM8026_2_REV3) |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 904 | iadc->adc->calib.gain_raw = |
| 905 | iadc->adc->calib.offset_raw + IADC_IDEAL_RAW_GAIN; |
| 906 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 907 | pr_debug("raw gain:0x%x, raw offset:0x%x\n", |
| 908 | iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw); |
| 909 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 910 | rc = qpnp_convert_raw_offset_voltage(iadc); |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 911 | if (rc < 0) { |
| 912 | pr_err("qpnp raw_voltage conversion failed\n"); |
| 913 | goto fail; |
| 914 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 915 | |
| 916 | rslt_msb = (raw_data & QPNP_RAW_CODE_16_BIT_MSB_MASK) >> |
| 917 | QPNP_BIT_SHIFT_8; |
| 918 | rslt_lsb = raw_data & QPNP_RAW_CODE_16_BIT_LSB_MASK; |
| 919 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 920 | pr_debug("trim values:lsb:0x%x and msb:0x%x\n", rslt_lsb, rslt_msb); |
| 921 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 922 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_SEC_ACCESS, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 923 | QPNP_IADC_SEC_ACCESS_DATA); |
| 924 | if (rc < 0) { |
| 925 | pr_err("qpnp iadc configure error for sec access\n"); |
| 926 | goto fail; |
| 927 | } |
| 928 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 929 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_MSB_OFFSET, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 930 | rslt_msb); |
| 931 | if (rc < 0) { |
| 932 | pr_err("qpnp iadc configure error for MSB write\n"); |
| 933 | goto fail; |
| 934 | } |
| 935 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 936 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_SEC_ACCESS, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 937 | QPNP_IADC_SEC_ACCESS_DATA); |
| 938 | if (rc < 0) { |
| 939 | pr_err("qpnp iadc configure error for sec access\n"); |
| 940 | goto fail; |
| 941 | } |
| 942 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 943 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_LSB_OFFSET, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 944 | rslt_lsb); |
| 945 | if (rc < 0) { |
| 946 | pr_err("qpnp iadc configure error for LSB write\n"); |
| 947 | goto fail; |
| 948 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 949 | fail: |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 950 | if (iadc->iadc_poll_eoc) { |
| 951 | pr_debug("releasing iadc eoc wakelock\n"); |
| 952 | pm_relax(iadc->dev); |
| 953 | } |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 954 | mutex_unlock(&iadc->adc->adc_lock); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 955 | return rc; |
| 956 | } |
Siddartha Mohanadoss | 0667392 | 2013-03-27 11:14:19 -0700 | [diff] [blame] | 957 | EXPORT_SYMBOL(qpnp_iadc_calibrate_for_trim); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 958 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 959 | static void qpnp_iadc_work(struct work_struct *work) |
| 960 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 961 | struct qpnp_iadc_chip *iadc = container_of(work, |
| 962 | struct qpnp_iadc_chip, iadc_work.work); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 963 | int rc = 0; |
| 964 | |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 965 | if (!iadc->skip_auto_calibrations) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 966 | rc = qpnp_iadc_calibrate_for_trim(iadc, true); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 967 | if (rc) |
| 968 | pr_debug("periodic IADC calibration failed\n"); |
| 969 | } |
| 970 | |
| 971 | schedule_delayed_work(&iadc->iadc_work, |
| 972 | round_jiffies_relative(msecs_to_jiffies |
| 973 | (QPNP_IADC_CALIB_SECONDS))); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 974 | return; |
| 975 | } |
| 976 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 977 | static int32_t qpnp_iadc_version_check(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 978 | { |
| 979 | uint8_t revision; |
| 980 | int rc; |
| 981 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 982 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION2, &revision); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 983 | if (rc < 0) { |
| 984 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 985 | return rc; |
| 986 | } |
| 987 | |
| 988 | if (revision < QPNP_IADC_SUPPORTED_REVISION2) { |
| 989 | pr_err("IADC Version not supported\n"); |
| 990 | return -EINVAL; |
| 991 | } |
| 992 | |
| 993 | return 0; |
| 994 | } |
| 995 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 996 | 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] | 997 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 998 | struct qpnp_iadc_chip *iadc; |
| 999 | struct device_node *node = NULL; |
| 1000 | char prop_name[QPNP_MAX_PROP_NAME_LEN]; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1001 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1002 | snprintf(prop_name, QPNP_MAX_PROP_NAME_LEN, "qcom,%s-iadc", name); |
| 1003 | |
| 1004 | node = of_parse_phandle(dev->of_node, prop_name, 0); |
| 1005 | if (node == NULL) |
| 1006 | return ERR_PTR(-ENODEV); |
| 1007 | |
| 1008 | list_for_each_entry(iadc, &qpnp_iadc_device_list, list) |
| 1009 | if (iadc->adc->spmi->dev.of_node == node) |
| 1010 | return iadc; |
| 1011 | return ERR_PTR(-EPROBE_DEFER); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1012 | } |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1013 | EXPORT_SYMBOL(qpnp_get_iadc); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1014 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1015 | 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] | 1016 | { |
| 1017 | uint8_t rslt_rsense; |
Siddartha Mohanadoss | 5827954 | 2013-05-28 16:20:46 -0700 | [diff] [blame] | 1018 | int32_t rc = 0, sign_bit = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1019 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1020 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1021 | return -EPROBE_DEFER; |
| 1022 | |
Siddartha Mohanadoss | 5827954 | 2013-05-28 16:20:46 -0700 | [diff] [blame] | 1023 | if (iadc->external_rsense) { |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 1024 | *rsense = iadc->rsense; |
Siddartha Mohanadoss | 5827954 | 2013-05-28 16:20:46 -0700 | [diff] [blame] | 1025 | return rc; |
| 1026 | } |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 1027 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1028 | if (iadc->default_internal_rsense) { |
| 1029 | *rsense = iadc->rsense_workaround_value; |
| 1030 | return rc; |
| 1031 | } |
| 1032 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1033 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_NOMINAL_RSENSE, &rslt_rsense); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1034 | if (rc < 0) { |
| 1035 | pr_err("qpnp adc rsense read failed with %d\n", rc); |
| 1036 | return rc; |
| 1037 | } |
| 1038 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 1039 | pr_debug("rsense:0%x\n", rslt_rsense); |
| 1040 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1041 | if (rslt_rsense & QPNP_RSENSE_MSB_SIGN_CHECK) |
| 1042 | sign_bit = 1; |
| 1043 | |
| 1044 | rslt_rsense &= ~QPNP_RSENSE_MSB_SIGN_CHECK; |
| 1045 | |
| 1046 | if (sign_bit) |
| 1047 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR - |
| 1048 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 1049 | else |
| 1050 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR + |
| 1051 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 1052 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1053 | pr_debug("rsense value is %d\n", *rsense); |
| 1054 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1055 | return rc; |
| 1056 | } |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 1057 | EXPORT_SYMBOL(qpnp_iadc_get_rsense); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1058 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1059 | static int32_t qpnp_check_pmic_temp(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1060 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1061 | struct qpnp_vadc_result result_pmic_therm; |
Siddartha Mohanadoss | d4e9edc | 2013-04-17 14:42:16 -0700 | [diff] [blame] | 1062 | int64_t die_temp_offset; |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 1063 | int rc = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1064 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1065 | rc = qpnp_vadc_read(iadc->vadc_dev, DIE_TEMP, &result_pmic_therm); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1066 | if (rc < 0) |
| 1067 | return rc; |
| 1068 | |
Siddartha Mohanadoss | d4e9edc | 2013-04-17 14:42:16 -0700 | [diff] [blame] | 1069 | die_temp_offset = result_pmic_therm.physical - |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 1070 | iadc->die_temp; |
Siddartha Mohanadoss | d4e9edc | 2013-04-17 14:42:16 -0700 | [diff] [blame] | 1071 | if (die_temp_offset < 0) |
| 1072 | die_temp_offset = -die_temp_offset; |
| 1073 | |
| 1074 | if (die_temp_offset > QPNP_IADC_DIE_TEMP_CALIB_OFFSET) { |
Abhijeet Dharmapurikar | 0ef9b5c | 2013-07-15 18:24:38 -0700 | [diff] [blame] | 1075 | iadc->die_temp = result_pmic_therm.physical; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1076 | if (!iadc->skip_auto_calibrations) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1077 | rc = qpnp_iadc_calibrate_for_trim(iadc, true); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1078 | if (rc) |
| 1079 | pr_err("IADC calibration failed rc = %d\n", rc); |
| 1080 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 1083 | return rc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1086 | int32_t qpnp_iadc_read(struct qpnp_iadc_chip *iadc, |
| 1087 | enum qpnp_iadc_channels channel, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1088 | struct qpnp_iadc_result *result) |
| 1089 | { |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1090 | int32_t rc, rsense_n_ohms, sign = 0, num, mode_sel = 0; |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 1091 | int32_t rsense_u_ohms = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1092 | int64_t result_current; |
| 1093 | uint16_t raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1094 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1095 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1096 | return -EPROBE_DEFER; |
| 1097 | |
Dipen Parmar | 454c4b1 | 2013-11-25 14:40:47 +0530 | [diff] [blame] | 1098 | if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) { |
| 1099 | pr_err("raw offset errors! run iadc calibration again\n"); |
| 1100 | return -EINVAL; |
| 1101 | } |
| 1102 | |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1103 | rc = qpnp_check_pmic_temp(iadc); |
| 1104 | if (rc) { |
| 1105 | pr_err("Error checking pmic therm temp\n"); |
| 1106 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1109 | mutex_lock(&iadc->adc->adc_lock); |
| 1110 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1111 | if (iadc->iadc_poll_eoc) { |
| 1112 | pr_debug("acquiring iadc eoc wakelock\n"); |
| 1113 | pm_stay_awake(iadc->dev); |
| 1114 | } |
| 1115 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1116 | rc = qpnp_iadc_configure(iadc, channel, &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1117 | if (rc < 0) { |
| 1118 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 1119 | goto fail; |
| 1120 | } |
| 1121 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1122 | rc = qpnp_iadc_get_rsense(iadc, &rsense_n_ohms); |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 1123 | pr_debug("current raw:0%x and rsense:%d\n", |
| 1124 | raw_data, rsense_n_ohms); |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 1125 | rsense_u_ohms = rsense_n_ohms/1000; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1126 | num = raw_data - iadc->adc->calib.offset_raw; |
| 1127 | if (num < 0) { |
| 1128 | sign = 1; |
| 1129 | num = -num; |
| 1130 | } |
| 1131 | |
| 1132 | result->result_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 1133 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 1134 | result_current = result->result_uv; |
| 1135 | result_current *= QPNP_IADC_NANO_VOLTS_FACTOR; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 1136 | /* Intentional fall through. Process the result w/o comp */ |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 1137 | do_div(result_current, rsense_u_ohms); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1138 | |
| 1139 | if (sign) { |
| 1140 | result->result_uv = -result->result_uv; |
| 1141 | result_current = -result_current; |
| 1142 | } |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1143 | rc = qpnp_iadc_comp_result(iadc, &result_current); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 1144 | if (rc < 0) |
| 1145 | pr_err("Error during compensating the IADC\n"); |
| 1146 | rc = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1147 | |
| 1148 | result->result_ua = (int32_t) result_current; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1149 | fail: |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1150 | if (iadc->iadc_poll_eoc) { |
| 1151 | pr_debug("releasing iadc eoc wakelock\n"); |
| 1152 | pm_relax(iadc->dev); |
| 1153 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1154 | mutex_unlock(&iadc->adc->adc_lock); |
| 1155 | |
| 1156 | return rc; |
| 1157 | } |
| 1158 | EXPORT_SYMBOL(qpnp_iadc_read); |
| 1159 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1160 | int32_t qpnp_iadc_get_gain_and_offset(struct qpnp_iadc_chip *iadc, |
| 1161 | struct qpnp_iadc_calib *result) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1162 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1163 | int rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1164 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1165 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1166 | return -EPROBE_DEFER; |
| 1167 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1168 | rc = qpnp_check_pmic_temp(iadc); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1169 | if (rc) { |
| 1170 | pr_err("Error checking pmic therm temp\n"); |
| 1171 | return rc; |
| 1172 | } |
| 1173 | |
| 1174 | mutex_lock(&iadc->adc->adc_lock); |
| 1175 | result->gain_raw = iadc->adc->calib.gain_raw; |
| 1176 | result->ideal_gain_nv = QPNP_ADC_GAIN_NV; |
| 1177 | result->gain_uv = iadc->adc->calib.gain_uv; |
| 1178 | result->offset_raw = iadc->adc->calib.offset_raw; |
| 1179 | result->ideal_offset_uv = |
| 1180 | QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL; |
| 1181 | result->offset_uv = iadc->adc->calib.offset_uv; |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 1182 | pr_debug("raw gain:0%x, raw offset:0%x\n", |
| 1183 | result->gain_raw, result->offset_raw); |
| 1184 | pr_debug("gain_uv:%d offset_uv:%d\n", |
| 1185 | result->gain_uv, result->offset_uv); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1186 | mutex_unlock(&iadc->adc->adc_lock); |
| 1187 | |
| 1188 | return 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1189 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1190 | EXPORT_SYMBOL(qpnp_iadc_get_gain_and_offset); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1191 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1192 | int qpnp_iadc_skip_calibration(struct qpnp_iadc_chip *iadc) |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1193 | { |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1194 | iadc->skip_auto_calibrations = true; |
| 1195 | return 0; |
| 1196 | } |
| 1197 | EXPORT_SYMBOL(qpnp_iadc_skip_calibration); |
| 1198 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1199 | int qpnp_iadc_resume_calibration(struct qpnp_iadc_chip *iadc) |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1200 | { |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1201 | iadc->skip_auto_calibrations = false; |
| 1202 | return 0; |
| 1203 | } |
| 1204 | EXPORT_SYMBOL(qpnp_iadc_resume_calibration); |
| 1205 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1206 | int32_t qpnp_iadc_vadc_sync_read(struct qpnp_iadc_chip *iadc, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1207 | enum qpnp_iadc_channels i_channel, struct qpnp_iadc_result *i_result, |
| 1208 | enum qpnp_vadc_channels v_channel, struct qpnp_vadc_result *v_result) |
| 1209 | { |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1210 | int rc = 0, mode_sel = 0, num = 0, rsense_n_ohms = 0, sign = 0; |
| 1211 | uint16_t raw_data; |
| 1212 | int32_t rsense_u_ohms = 0; |
| 1213 | int64_t result_current; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1214 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1215 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1216 | return -EPROBE_DEFER; |
| 1217 | |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1218 | mutex_lock(&iadc->adc->adc_lock); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1219 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1220 | if (iadc->iadc_poll_eoc) { |
| 1221 | pr_debug("acquiring iadc eoc wakelock\n"); |
| 1222 | pm_stay_awake(iadc->dev); |
| 1223 | } |
| 1224 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1225 | iadc->iadc_mode_sel = true; |
| 1226 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1227 | rc = qpnp_vadc_iadc_sync_request(iadc->vadc_dev, v_channel); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1228 | if (rc) { |
| 1229 | pr_err("Configuring VADC failed\n"); |
| 1230 | goto fail; |
| 1231 | } |
| 1232 | |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1233 | rc = qpnp_iadc_configure(iadc, i_channel, &raw_data, mode_sel); |
| 1234 | if (rc < 0) { |
| 1235 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 1236 | goto fail_release_vadc; |
| 1237 | } |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1238 | |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1239 | rc = qpnp_iadc_get_rsense(iadc, &rsense_n_ohms); |
| 1240 | pr_debug("current raw:0%x and rsense:%d\n", |
| 1241 | raw_data, rsense_n_ohms); |
| 1242 | rsense_u_ohms = rsense_n_ohms/1000; |
| 1243 | num = raw_data - iadc->adc->calib.offset_raw; |
| 1244 | if (num < 0) { |
| 1245 | sign = 1; |
| 1246 | num = -num; |
| 1247 | } |
| 1248 | |
| 1249 | i_result->result_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 1250 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 1251 | result_current = i_result->result_uv; |
| 1252 | result_current *= QPNP_IADC_NANO_VOLTS_FACTOR; |
| 1253 | /* Intentional fall through. Process the result w/o comp */ |
| 1254 | do_div(result_current, rsense_u_ohms); |
| 1255 | |
| 1256 | if (sign) { |
| 1257 | i_result->result_uv = -i_result->result_uv; |
| 1258 | result_current = -result_current; |
| 1259 | } |
| 1260 | result_current *= -1; |
| 1261 | rc = qpnp_iadc_comp_result(iadc, &result_current); |
| 1262 | if (rc < 0) |
| 1263 | pr_err("Error during compensating the IADC\n"); |
| 1264 | rc = 0; |
| 1265 | result_current *= -1; |
| 1266 | |
| 1267 | i_result->result_ua = (int32_t) result_current; |
| 1268 | |
| 1269 | fail_release_vadc: |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1270 | rc = qpnp_vadc_iadc_sync_complete_request(iadc->vadc_dev, v_channel, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1271 | v_result); |
| 1272 | if (rc) |
| 1273 | pr_err("Releasing VADC failed\n"); |
| 1274 | fail: |
| 1275 | iadc->iadc_mode_sel = false; |
| 1276 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1277 | if (iadc->iadc_poll_eoc) { |
| 1278 | pr_debug("releasing iadc eoc wakelock\n"); |
| 1279 | pm_relax(iadc->dev); |
| 1280 | } |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1281 | mutex_unlock(&iadc->adc->adc_lock); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1282 | |
| 1283 | return rc; |
| 1284 | } |
| 1285 | EXPORT_SYMBOL(qpnp_iadc_vadc_sync_read); |
| 1286 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1287 | static ssize_t qpnp_iadc_show(struct device *dev, |
| 1288 | struct device_attribute *devattr, char *buf) |
| 1289 | { |
| 1290 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1291 | struct qpnp_iadc_chip *iadc = dev_get_drvdata(dev); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1292 | struct qpnp_iadc_result result; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1293 | int rc = -1; |
| 1294 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1295 | rc = qpnp_iadc_read(iadc, attr->index, &result); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1296 | |
| 1297 | if (rc) |
| 1298 | return 0; |
| 1299 | |
| 1300 | return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1301 | "Result:%d\n", result.result_ua); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | static struct sensor_device_attribute qpnp_adc_attr = |
| 1305 | SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0); |
| 1306 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1307 | static int32_t qpnp_iadc_init_hwmon(struct qpnp_iadc_chip *iadc, |
| 1308 | struct spmi_device *spmi) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1309 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1310 | struct device_node *child; |
| 1311 | struct device_node *node = spmi->dev.of_node; |
| 1312 | int rc = 0, i = 0, channel; |
| 1313 | |
| 1314 | for_each_child_of_node(node, child) { |
| 1315 | channel = iadc->adc->adc_channels[i].channel_num; |
| 1316 | qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num; |
| 1317 | qpnp_adc_attr.dev_attr.attr.name = |
| 1318 | iadc->adc->adc_channels[i].name; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1319 | memcpy(&iadc->sens_attr[i], &qpnp_adc_attr, |
| 1320 | sizeof(qpnp_adc_attr)); |
Stephen Boyd | 8a5c4e4 | 2012-10-30 11:07:22 -0700 | [diff] [blame] | 1321 | sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1322 | rc = device_create_file(&spmi->dev, |
| 1323 | &iadc->sens_attr[i].dev_attr); |
| 1324 | if (rc) { |
| 1325 | dev_err(&spmi->dev, |
| 1326 | "device_create_file failed for dev %s\n", |
| 1327 | iadc->adc->adc_channels[i].name); |
| 1328 | goto hwmon_err_sens; |
| 1329 | } |
| 1330 | i++; |
| 1331 | } |
| 1332 | |
| 1333 | return 0; |
| 1334 | hwmon_err_sens: |
| 1335 | pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc); |
| 1336 | return rc; |
| 1337 | } |
| 1338 | |
| 1339 | static int __devinit qpnp_iadc_probe(struct spmi_device *spmi) |
| 1340 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1341 | struct qpnp_iadc_chip *iadc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1342 | struct qpnp_adc_drv *adc_qpnp; |
| 1343 | struct device_node *node = spmi->dev.of_node; |
| 1344 | struct device_node *child; |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1345 | struct resource *res; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1346 | int rc, count_adc_channel_list = 0, i = 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1347 | |
| 1348 | for_each_child_of_node(node, child) |
| 1349 | count_adc_channel_list++; |
| 1350 | |
| 1351 | if (!count_adc_channel_list) { |
| 1352 | pr_err("No channel listing\n"); |
| 1353 | return -EINVAL; |
| 1354 | } |
| 1355 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1356 | iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_chip) + |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1357 | (sizeof(struct sensor_device_attribute) * |
| 1358 | count_adc_channel_list), GFP_KERNEL); |
| 1359 | if (!iadc) { |
| 1360 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 1361 | return -ENOMEM; |
| 1362 | } |
| 1363 | |
| 1364 | adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv), |
| 1365 | GFP_KERNEL); |
| 1366 | if (!adc_qpnp) { |
| 1367 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
Siddartha Mohanadoss | c61babb | 2013-08-02 13:37:12 -0700 | [diff] [blame] | 1368 | return -ENOMEM; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1371 | iadc->dev = &(spmi->dev); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1372 | iadc->adc = adc_qpnp; |
| 1373 | |
| 1374 | rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc); |
| 1375 | if (rc) { |
| 1376 | dev_err(&spmi->dev, "failed to read device tree\n"); |
Siddartha Mohanadoss | c61babb | 2013-08-02 13:37:12 -0700 | [diff] [blame] | 1377 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1380 | res = spmi_get_resource_byname(spmi, NULL, IORESOURCE_MEM, |
| 1381 | "batt-id-trim-cnst-rds"); |
| 1382 | if (!res) { |
| 1383 | dev_err(&spmi->dev, "failed to read batt_id trim register\n"); |
| 1384 | return -EINVAL; |
| 1385 | } |
| 1386 | iadc->batt_id_trim_cnst_rds = res->start; |
| 1387 | rc = of_property_read_u32(node, "qcom,use-default-rds-trim", |
| 1388 | &iadc->rds_trim_default_type); |
| 1389 | if (rc) |
| 1390 | pr_debug("No trim workaround needed\n"); |
| 1391 | else { |
| 1392 | pr_debug("Use internal RDS trim workaround\n"); |
| 1393 | iadc->rds_trim_default_check = true; |
| 1394 | } |
| 1395 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1396 | iadc->vadc_dev = qpnp_get_vadc(&spmi->dev, "iadc"); |
| 1397 | if (IS_ERR(iadc->vadc_dev)) { |
| 1398 | rc = PTR_ERR(iadc->vadc_dev); |
| 1399 | if (rc != -EPROBE_DEFER) |
| 1400 | pr_err("vadc property missing, rc=%d\n", rc); |
Siddartha Mohanadoss | c61babb | 2013-08-02 13:37:12 -0700 | [diff] [blame] | 1401 | return rc; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
Stephen Boyd | beab450 | 2013-04-25 10:18:17 -0700 | [diff] [blame] | 1404 | mutex_init(&iadc->adc->adc_lock); |
| 1405 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1406 | rc = of_property_read_u32(node, "qcom,rsense", |
| 1407 | &iadc->rsense); |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 1408 | if (rc) |
| 1409 | pr_debug("Defaulting to internal rsense\n"); |
| 1410 | else { |
| 1411 | pr_debug("Use external rsense\n"); |
| 1412 | iadc->external_rsense = true; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1415 | iadc->iadc_poll_eoc = of_property_read_bool(node, |
| 1416 | "qcom,iadc-poll-eoc"); |
| 1417 | if (!iadc->iadc_poll_eoc) { |
| 1418 | rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq_eoc, |
| 1419 | qpnp_iadc_isr, IRQF_TRIGGER_RISING, |
| 1420 | "qpnp_iadc_interrupt", iadc); |
| 1421 | if (rc) { |
| 1422 | dev_err(&spmi->dev, "failed to request adc irq\n"); |
| 1423 | return rc; |
| 1424 | } else |
| 1425 | enable_irq_wake(iadc->adc->adc_irq_eoc); |
| 1426 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1427 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1428 | rc = qpnp_iadc_init_hwmon(iadc, spmi); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1429 | if (rc) { |
| 1430 | dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n"); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1431 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1432 | } |
| 1433 | iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev); |
| 1434 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1435 | rc = qpnp_iadc_version_check(iadc); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1436 | if (rc) { |
| 1437 | dev_err(&spmi->dev, "IADC version not supported\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1438 | goto fail; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1441 | INIT_WORK(&iadc->trigger_completion_work, qpnp_iadc_trigger_completion); |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1442 | INIT_DELAYED_WORK(&iadc->iadc_work, qpnp_iadc_work); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1443 | rc = qpnp_iadc_comp_info(iadc); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 1444 | if (rc) { |
| 1445 | dev_err(&spmi->dev, "abstracting IADC comp info failed!\n"); |
| 1446 | goto fail; |
| 1447 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1448 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1449 | rc = qpnp_iadc_rds_trim_update_check(iadc); |
| 1450 | if (rc) { |
| 1451 | dev_err(&spmi->dev, "Rds trim update failed!\n"); |
| 1452 | goto fail; |
| 1453 | } |
| 1454 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1455 | dev_set_drvdata(&spmi->dev, iadc); |
| 1456 | list_add(&iadc->list, &qpnp_iadc_device_list); |
| 1457 | rc = qpnp_iadc_calibrate_for_trim(iadc, true); |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1458 | if (rc) |
| 1459 | dev_err(&spmi->dev, "failed to calibrate for USR trim\n"); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1460 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1461 | if (iadc->iadc_poll_eoc) |
| 1462 | device_init_wakeup(iadc->dev, 1); |
| 1463 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1464 | schedule_delayed_work(&iadc->iadc_work, |
| 1465 | round_jiffies_relative(msecs_to_jiffies |
| 1466 | (QPNP_IADC_CALIB_SECONDS))); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1467 | return 0; |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1468 | fail: |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1469 | for_each_child_of_node(node, child) { |
| 1470 | device_remove_file(&spmi->dev, |
| 1471 | &iadc->sens_attr[i].dev_attr); |
| 1472 | i++; |
| 1473 | } |
| 1474 | hwmon_device_unregister(iadc->iadc_hwmon); |
| 1475 | |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1476 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | static int __devexit qpnp_iadc_remove(struct spmi_device *spmi) |
| 1480 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1481 | struct qpnp_iadc_chip *iadc = dev_get_drvdata(&spmi->dev); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1482 | struct device_node *node = spmi->dev.of_node; |
| 1483 | struct device_node *child; |
| 1484 | int i = 0; |
| 1485 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1486 | cancel_delayed_work(&iadc->iadc_work); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1487 | for_each_child_of_node(node, child) { |
| 1488 | device_remove_file(&spmi->dev, |
| 1489 | &iadc->sens_attr[i].dev_attr); |
| 1490 | i++; |
| 1491 | } |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1492 | hwmon_device_unregister(iadc->iadc_hwmon); |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1493 | if (iadc->iadc_poll_eoc) |
| 1494 | pm_relax(iadc->dev); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1495 | dev_set_drvdata(&spmi->dev, NULL); |
| 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | static const struct of_device_id qpnp_iadc_match_table[] = { |
| 1501 | { .compatible = "qcom,qpnp-iadc", |
| 1502 | }, |
| 1503 | {} |
| 1504 | }; |
| 1505 | |
| 1506 | static struct spmi_driver qpnp_iadc_driver = { |
| 1507 | .driver = { |
| 1508 | .name = "qcom,qpnp-iadc", |
| 1509 | .of_match_table = qpnp_iadc_match_table, |
| 1510 | }, |
| 1511 | .probe = qpnp_iadc_probe, |
| 1512 | .remove = qpnp_iadc_remove, |
| 1513 | }; |
| 1514 | |
| 1515 | static int __init qpnp_iadc_init(void) |
| 1516 | { |
| 1517 | return spmi_driver_register(&qpnp_iadc_driver); |
| 1518 | } |
| 1519 | module_init(qpnp_iadc_init); |
| 1520 | |
| 1521 | static void __exit qpnp_iadc_exit(void) |
| 1522 | { |
| 1523 | spmi_driver_unregister(&qpnp_iadc_driver); |
| 1524 | } |
| 1525 | module_exit(qpnp_iadc_exit); |
| 1526 | |
| 1527 | MODULE_DESCRIPTION("QPNP PMIC current ADC driver"); |
| 1528 | MODULE_LICENSE("GPL v2"); |