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 | 9376184 | 2013-09-11 17:46:54 -0700 | [diff] [blame] | 343 | #define QPNP_IADC_PM8026_2_REV2 4 |
| 344 | #define QPNP_IADC_PM8026_2_REV3 2 |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 345 | |
| 346 | #define QPNP_COEFF_1 969000 |
| 347 | #define QPNP_COEFF_2 32 |
| 348 | #define QPNP_COEFF_3_TYPEA 1700000 |
| 349 | #define QPNP_COEFF_3_TYPEB 1000000 |
| 350 | #define QPNP_COEFF_4 100 |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 351 | #define QPNP_COEFF_5 15 |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 352 | #define QPNP_COEFF_6 100000 |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 353 | #define QPNP_COEFF_7 21 |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 354 | #define QPNP_COEFF_8 100000000 |
| 355 | #define QPNP_COEFF_9 38 |
| 356 | #define QPNP_COEFF_10 40 |
| 357 | #define QPNP_COEFF_11 7 |
| 358 | #define QPNP_COEFF_12 11 |
| 359 | #define QPNP_COEFF_13 37 |
| 360 | #define QPNP_COEFF_14 39 |
| 361 | #define QPNP_COEFF_15 9 |
| 362 | #define QPNP_COEFF_16 11 |
| 363 | #define QPNP_COEFF_17 851200 |
| 364 | #define QPNP_COEFF_18 296500 |
| 365 | #define QPNP_COEFF_19 222400 |
| 366 | #define QPNP_COEFF_20 813800 |
| 367 | #define QPNP_COEFF_21 1059100 |
| 368 | #define QPNP_COEFF_22 5000000 |
| 369 | #define QPNP_COEFF_23 3722500 |
| 370 | #define QPNP_COEFF_24 84 |
Siddartha Mohanadoss | 9376184 | 2013-09-11 17:46:54 -0700 | [diff] [blame] | 371 | #define QPNP_COEFF_25 33 |
| 372 | #define QPNP_COEFF_26 22 |
Xiaozhe Shi | 8075422 | 2013-10-30 14:11:41 -0700 | [diff] [blame] | 373 | #define QPNP_COEFF_27 53 |
| 374 | #define QPNP_COEFF_28 48 |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 375 | |
| 376 | 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] | 377 | int64_t die_temp) |
| 378 | { |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 379 | int64_t temp_var = 0, sys_gain_coeff = 0, old; |
| 380 | int32_t coeff_a = 0, coeff_b = 0; |
Siddartha Mohanadoss | 9376184 | 2013-09-11 17:46:54 -0700 | [diff] [blame] | 381 | int version = 0; |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 382 | |
Siddartha Mohanadoss | 9376184 | 2013-09-11 17:46:54 -0700 | [diff] [blame] | 383 | version = qpnp_adc_get_revid_version(iadc->dev); |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 384 | if (version == -EINVAL) |
| 385 | return 0; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 386 | |
Xiaozhe Shi | 62ad5e1 | 2013-05-13 12:37:41 -0700 | [diff] [blame] | 387 | old = *result; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 388 | *result = *result * 1000000; |
| 389 | |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 390 | if (iadc->iadc_comp.sys_gain > 127) |
| 391 | sys_gain_coeff = -QPNP_COEFF_6 * |
| 392 | (iadc->iadc_comp.sys_gain - 128); |
| 393 | else |
| 394 | sys_gain_coeff = QPNP_COEFF_6 * |
| 395 | iadc->iadc_comp.sys_gain; |
| 396 | |
| 397 | switch (version) { |
Siddartha Mohanadoss | 9376184 | 2013-09-11 17:46:54 -0700 | [diff] [blame] | 398 | case QPNP_REV_ID_8941_3_1: |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 399 | switch (iadc->iadc_comp.id) { |
| 400 | case COMP_ID_GF: |
| 401 | if (!iadc->iadc_comp.ext_rsense) { |
| 402 | /* internal rsense */ |
| 403 | coeff_a = QPNP_COEFF_2; |
| 404 | coeff_b = -QPNP_COEFF_3_TYPEA; |
| 405 | } else { |
| 406 | if (*result < 0) { |
| 407 | /* charge */ |
| 408 | coeff_a = QPNP_COEFF_5; |
| 409 | coeff_b = QPNP_COEFF_6; |
| 410 | } else { |
| 411 | /* discharge */ |
| 412 | coeff_a = -QPNP_COEFF_7; |
| 413 | coeff_b = QPNP_COEFF_6; |
| 414 | } |
| 415 | } |
| 416 | break; |
| 417 | case COMP_ID_TSMC: |
| 418 | default: |
| 419 | if (!iadc->iadc_comp.ext_rsense) { |
| 420 | /* internal rsense */ |
| 421 | coeff_a = QPNP_COEFF_2; |
| 422 | coeff_b = -QPNP_COEFF_3_TYPEB; |
| 423 | } else { |
| 424 | if (*result < 0) { |
| 425 | /* charge */ |
| 426 | coeff_a = QPNP_COEFF_5; |
| 427 | coeff_b = QPNP_COEFF_6; |
| 428 | } else { |
| 429 | /* discharge */ |
| 430 | coeff_a = -QPNP_COEFF_7; |
| 431 | coeff_b = QPNP_COEFF_6; |
| 432 | } |
| 433 | } |
| 434 | break; |
| 435 | } |
| 436 | break; |
Siddartha Mohanadoss | 9376184 | 2013-09-11 17:46:54 -0700 | [diff] [blame] | 437 | case QPNP_REV_ID_8026_2_1: |
| 438 | switch (iadc->iadc_comp.id) { |
| 439 | case COMP_ID_GF: |
| 440 | if (!iadc->iadc_comp.ext_rsense) { |
| 441 | /* internal rsense */ |
| 442 | if (*result < 0) { |
| 443 | /* charge */ |
| 444 | coeff_a = 0; |
| 445 | coeff_b = 0; |
| 446 | } else { |
| 447 | coeff_a = QPNP_COEFF_25; |
| 448 | coeff_b = 0; |
| 449 | } |
| 450 | } else { |
| 451 | if (*result < 0) { |
| 452 | /* charge */ |
| 453 | coeff_a = 0; |
| 454 | coeff_b = 0; |
| 455 | } else { |
| 456 | /* discharge */ |
| 457 | coeff_a = 0; |
| 458 | coeff_b = 0; |
| 459 | } |
| 460 | } |
| 461 | break; |
| 462 | case COMP_ID_TSMC: |
| 463 | default: |
| 464 | if (!iadc->iadc_comp.ext_rsense) { |
| 465 | /* internal rsense */ |
| 466 | if (*result < 0) { |
| 467 | /* charge */ |
| 468 | coeff_a = 0; |
| 469 | coeff_b = 0; |
| 470 | } else { |
| 471 | coeff_a = QPNP_COEFF_26; |
| 472 | coeff_b = 0; |
| 473 | } |
| 474 | } else { |
| 475 | if (*result < 0) { |
| 476 | /* charge */ |
| 477 | coeff_a = 0; |
| 478 | coeff_b = 0; |
| 479 | } else { |
| 480 | /* discharge */ |
| 481 | coeff_a = 0; |
| 482 | coeff_b = 0; |
| 483 | } |
| 484 | } |
| 485 | break; |
| 486 | } |
| 487 | break; |
| 488 | case QPNP_REV_ID_8026_1_0: |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 489 | /* pm8026 rev 1.0 */ |
| 490 | switch (iadc->iadc_comp.id) { |
| 491 | case COMP_ID_GF: |
| 492 | if (!iadc->iadc_comp.ext_rsense) { |
| 493 | /* internal rsense */ |
| 494 | if (*result < 0) { |
| 495 | /* charge */ |
| 496 | coeff_a = QPNP_COEFF_9; |
| 497 | coeff_b = -QPNP_COEFF_17; |
| 498 | } else { |
| 499 | coeff_a = QPNP_COEFF_10; |
| 500 | coeff_b = QPNP_COEFF_18; |
| 501 | } |
| 502 | } else { |
| 503 | if (*result < 0) { |
| 504 | /* charge */ |
| 505 | coeff_a = -QPNP_COEFF_11; |
| 506 | coeff_b = 0; |
| 507 | } else { |
| 508 | /* discharge */ |
| 509 | coeff_a = -QPNP_COEFF_17; |
| 510 | coeff_b = -QPNP_COEFF_19; |
| 511 | } |
| 512 | } |
| 513 | break; |
| 514 | case COMP_ID_TSMC: |
| 515 | default: |
| 516 | if (!iadc->iadc_comp.ext_rsense) { |
| 517 | /* internal rsense */ |
| 518 | if (*result < 0) { |
| 519 | /* charge */ |
| 520 | coeff_a = QPNP_COEFF_13; |
| 521 | coeff_b = -QPNP_COEFF_20; |
| 522 | } else { |
| 523 | coeff_a = QPNP_COEFF_14; |
| 524 | coeff_b = QPNP_COEFF_21; |
| 525 | } |
| 526 | } else { |
| 527 | if (*result < 0) { |
| 528 | /* charge */ |
| 529 | coeff_a = -QPNP_COEFF_15; |
| 530 | coeff_b = 0; |
| 531 | } else { |
| 532 | /* discharge */ |
| 533 | coeff_a = -QPNP_COEFF_12; |
| 534 | coeff_b = -QPNP_COEFF_19; |
| 535 | } |
| 536 | } |
| 537 | break; |
| 538 | } |
| 539 | break; |
Siddartha Mohanadoss | 9376184 | 2013-09-11 17:46:54 -0700 | [diff] [blame] | 540 | case QPNP_REV_ID_8110_1_0: |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 541 | /* pm8110 rev 1.0 */ |
| 542 | switch (iadc->iadc_comp.id) { |
| 543 | case COMP_ID_GF: |
| 544 | if (!iadc->iadc_comp.ext_rsense) { |
| 545 | /* internal rsense */ |
| 546 | if (*result < 0) { |
| 547 | /* charge */ |
| 548 | coeff_a = QPNP_COEFF_24; |
| 549 | coeff_b = -QPNP_COEFF_22; |
| 550 | } else { |
| 551 | coeff_a = QPNP_COEFF_24; |
| 552 | coeff_b = -QPNP_COEFF_23; |
| 553 | } |
| 554 | } |
| 555 | break; |
| 556 | case COMP_ID_SMIC: |
| 557 | default: |
| 558 | if (!iadc->iadc_comp.ext_rsense) { |
| 559 | /* internal rsense */ |
| 560 | if (*result < 0) { |
| 561 | /* charge */ |
| 562 | coeff_a = QPNP_COEFF_24; |
| 563 | coeff_b = -QPNP_COEFF_22; |
| 564 | } else { |
| 565 | coeff_a = QPNP_COEFF_24; |
| 566 | coeff_b = -QPNP_COEFF_23; |
| 567 | } |
| 568 | } |
| 569 | break; |
| 570 | } |
| 571 | break; |
Xiaozhe Shi | 8075422 | 2013-10-30 14:11:41 -0700 | [diff] [blame] | 572 | case QPNP_REV_ID_8110_2_0: |
| 573 | die_temp -= 25000; |
| 574 | /* pm8110 rev 2.0 */ |
| 575 | switch (iadc->iadc_comp.id) { |
| 576 | case COMP_ID_GF: |
| 577 | if (!iadc->iadc_comp.ext_rsense) { |
| 578 | /* internal rsense */ |
| 579 | if (*result < 0) { |
| 580 | /* charge */ |
| 581 | coeff_a = 0; |
| 582 | coeff_b = 0; |
| 583 | } else { |
| 584 | coeff_a = QPNP_COEFF_27; |
| 585 | coeff_b = 0; |
| 586 | } |
| 587 | } |
| 588 | break; |
| 589 | case COMP_ID_SMIC: |
| 590 | default: |
| 591 | if (!iadc->iadc_comp.ext_rsense) { |
| 592 | /* internal rsense */ |
| 593 | if (*result < 0) { |
| 594 | /* charge */ |
| 595 | coeff_a = 0; |
| 596 | coeff_b = 0; |
| 597 | } else { |
| 598 | coeff_a = QPNP_COEFF_28; |
| 599 | coeff_b = 0; |
| 600 | } |
| 601 | } |
| 602 | break; |
| 603 | } |
| 604 | break; |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 605 | default: |
Siddartha Mohanadoss | 9376184 | 2013-09-11 17:46:54 -0700 | [diff] [blame] | 606 | case QPNP_REV_ID_8026_2_0: |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 607 | /* pm8026 rev 1.0 */ |
| 608 | coeff_a = 0; |
| 609 | coeff_b = 0; |
| 610 | break; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 613 | temp_var = (coeff_a * die_temp) + coeff_b; |
| 614 | temp_var = div64_s64(temp_var, QPNP_COEFF_4); |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 615 | temp_var = 1000 * (1000000 - temp_var); |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 616 | |
| 617 | if (!iadc->iadc_comp.ext_rsense) { |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 618 | /* internal rsense */ |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 619 | *result = div64_s64(*result * 1000, temp_var); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 622 | if (iadc->iadc_comp.ext_rsense) { |
| 623 | /* external rsense */ |
| 624 | sys_gain_coeff = (1000000 + |
| 625 | div64_s64(sys_gain_coeff, QPNP_COEFF_4)); |
Siddartha Mohanadoss | 28473be | 2013-09-03 15:46:26 -0700 | [diff] [blame] | 626 | temp_var = div64_s64(temp_var * sys_gain_coeff, 1000000); |
Siddartha Mohanadoss | 54bbfce | 2013-08-21 22:02:51 -0700 | [diff] [blame] | 627 | *result = div64_s64(*result * 1000, temp_var); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 628 | } |
Xiaozhe Shi | 4e3a83c | 2013-12-02 15:30:56 -0800 | [diff] [blame^] | 629 | pr_debug("%lld compensated into %lld, a: %d, b: %d, sys_gain: %lld\n", |
| 630 | old, *result, coeff_a, coeff_b, sys_gain_coeff); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 635 | 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] | 636 | { |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 637 | return qpnp_iadc_comp(result, iadc, iadc->die_temp); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 638 | } |
| 639 | EXPORT_SYMBOL(qpnp_iadc_comp_result); |
| 640 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 641 | static int qpnp_iadc_rds_trim_update_check(struct qpnp_iadc_chip *iadc) |
| 642 | { |
| 643 | int rc = 0; |
| 644 | u8 trim2_val = 0, smbb_batt_trm_data = 0; |
| 645 | |
| 646 | if (!iadc->rds_trim_default_check) { |
| 647 | pr_debug("No internal rds trim check needed\n"); |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_NOMINAL_RSENSE, &trim2_val); |
| 652 | if (rc < 0) { |
| 653 | pr_err("qpnp adc trim2_fullscale1 reg read failed %d\n", rc); |
| 654 | return rc; |
| 655 | } |
| 656 | |
| 657 | rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave, |
| 658 | iadc->batt_id_trim_cnst_rds, &smbb_batt_trm_data, 1); |
| 659 | if (rc < 0) { |
| 660 | pr_err("batt_id trim_cnst rds reg read failed %d\n", rc); |
| 661 | return rc; |
| 662 | } |
| 663 | |
| 664 | pr_debug("n_trim:0x%x smb_trm:0x%x\n", trim2_val, smbb_batt_trm_data); |
| 665 | |
| 666 | if (iadc->rds_trim_default_type == QPNP_IADC_RDS_DEFAULT_TYPEA) { |
| 667 | if (((smbb_batt_trm_data & SMBB_BAT_IF_TRIM_CNST_RDS_MASK) == |
| 668 | SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST) && |
| 669 | (trim2_val == QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) { |
| 670 | iadc->rsense_workaround_value = |
| 671 | QPNP_IADC_RSENSE_DEFAULT_VALUE; |
| 672 | iadc->default_internal_rsense = true; |
| 673 | } |
| 674 | } else if (iadc->rds_trim_default_type == |
| 675 | QPNP_IADC_RDS_DEFAULT_TYPEB) { |
| 676 | if (((smbb_batt_trm_data & SMBB_BAT_IF_TRIM_CNST_RDS_MASK) >= |
| 677 | SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST) && |
| 678 | (trim2_val == QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) { |
| 679 | iadc->rsense_workaround_value = |
| 680 | QPNP_IADC_RSENSE_DEFAULT_VALUE; |
| 681 | iadc->default_internal_rsense = true; |
| 682 | } else if (((smbb_batt_trm_data & |
| 683 | SMBB_BAT_IF_TRIM_CNST_RDS_MASK) |
| 684 | < SMBB_BAT_IF_TRIM_CNST_RDS_MASK_CONST) && |
| 685 | (trim2_val == |
| 686 | QPNP_IADC1_USR_TRIM2_ADC_FULLSCALE1_CONST)) { |
| 687 | if (iadc->iadc_comp.id == COMP_ID_GF) { |
| 688 | iadc->rsense_workaround_value = |
| 689 | QPNP_IADC_RSENSE_DEFAULT_TYPEB_GF; |
| 690 | iadc->default_internal_rsense = true; |
| 691 | } else if (iadc->iadc_comp.id == COMP_ID_SMIC) { |
| 692 | iadc->rsense_workaround_value = |
| 693 | QPNP_IADC_RSENSE_DEFAULT_TYPEB_SMIC; |
| 694 | iadc->default_internal_rsense = true; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 702 | static int32_t qpnp_iadc_comp_info(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 703 | { |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 704 | int rc = 0; |
| 705 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 706 | 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] | 707 | if (rc < 0) { |
| 708 | pr_err("qpnp adc comp id failed with %d\n", rc); |
| 709 | return rc; |
| 710 | } |
| 711 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 712 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION2, |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 713 | &iadc->iadc_comp.revision_dig_major); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 714 | if (rc < 0) { |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 715 | pr_err("qpnp adc revision2 read failed with %d\n", rc); |
| 716 | return rc; |
| 717 | } |
| 718 | |
| 719 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION3, |
| 720 | &iadc->iadc_comp.revision_ana_minor); |
| 721 | if (rc < 0) { |
| 722 | pr_err("qpnp adc revision3 read failed with %d\n", rc); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 723 | return rc; |
| 724 | } |
| 725 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 726 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_ATE_GAIN_CALIB_OFFSET, |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 727 | &iadc->iadc_comp.sys_gain); |
Siddartha Mohanadoss | 7bea844 | 2013-06-17 17:50:22 -0700 | [diff] [blame] | 728 | if (rc < 0) { |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 729 | pr_err("full scale read failed with %d\n", rc); |
Siddartha Mohanadoss | 7bea844 | 2013-06-17 17:50:22 -0700 | [diff] [blame] | 730 | return rc; |
| 731 | } |
| 732 | |
| 733 | if (iadc->external_rsense) |
| 734 | iadc->iadc_comp.ext_rsense = true; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 735 | |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 736 | 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] | 737 | iadc->iadc_comp.id, |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 738 | iadc->iadc_comp.revision_dig_major, |
| 739 | iadc->iadc_comp.revision_ana_minor, |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 740 | iadc->iadc_comp.sys_gain, |
| 741 | iadc->iadc_comp.ext_rsense); |
| 742 | return rc; |
| 743 | } |
| 744 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 745 | static int32_t qpnp_iadc_configure(struct qpnp_iadc_chip *iadc, |
| 746 | enum qpnp_iadc_channels channel, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 747 | uint16_t *raw_code, uint32_t mode_sel) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 748 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 749 | u8 qpnp_iadc_mode_reg = 0, qpnp_iadc_ch_sel_reg = 0; |
| 750 | u8 qpnp_iadc_conv_req = 0, qpnp_iadc_dig_param_reg = 0; |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 751 | u8 status1 = 0; |
| 752 | uint32_t count = 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 753 | int32_t rc = 0; |
| 754 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 755 | qpnp_iadc_ch_sel_reg = channel; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 756 | |
| 757 | qpnp_iadc_dig_param_reg |= iadc->adc->amux_prop->decimation << |
| 758 | QPNP_IADC_DEC_RATIO_SEL; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 759 | if (iadc->iadc_mode_sel) |
| 760 | qpnp_iadc_mode_reg |= (QPNP_ADC_TRIM_EN | QPNP_VADC_SYNCH_EN); |
| 761 | else |
| 762 | qpnp_iadc_mode_reg |= QPNP_ADC_TRIM_EN; |
| 763 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 764 | qpnp_iadc_conv_req = QPNP_IADC_CONV_REQ; |
| 765 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 766 | 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] | 767 | if (rc) { |
| 768 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 769 | return rc; |
| 770 | } |
| 771 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 772 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_ADC_CH_SEL_CTL, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 773 | qpnp_iadc_ch_sel_reg); |
| 774 | if (rc) { |
| 775 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 776 | return rc; |
| 777 | } |
| 778 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 779 | rc = qpnp_iadc_write_reg(iadc, QPNP_ADC_DIG_PARAM, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 780 | qpnp_iadc_dig_param_reg); |
| 781 | if (rc) { |
| 782 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 783 | return rc; |
| 784 | } |
| 785 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 786 | rc = qpnp_iadc_write_reg(iadc, QPNP_HW_SETTLE_DELAY, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 787 | iadc->adc->amux_prop->hw_settle_time); |
| 788 | if (rc < 0) { |
| 789 | pr_err("qpnp adc configure error for hw settling time setup\n"); |
| 790 | return rc; |
| 791 | } |
| 792 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 793 | rc = qpnp_iadc_write_reg(iadc, QPNP_FAST_AVG_CTL, |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 794 | iadc->adc->amux_prop->fast_avg_setup); |
| 795 | if (rc < 0) { |
| 796 | pr_err("qpnp adc fast averaging configure error\n"); |
| 797 | return rc; |
| 798 | } |
| 799 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 800 | if (!iadc->iadc_poll_eoc) |
| 801 | INIT_COMPLETION(iadc->adc->adc_rslt_completion); |
Siddartha Mohanadoss | 3f219c4 | 2013-04-02 11:01:28 -0700 | [diff] [blame] | 802 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 803 | rc = qpnp_iadc_enable(iadc, true); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 804 | if (rc) |
| 805 | return rc; |
| 806 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 807 | 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] | 808 | if (rc) { |
| 809 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 810 | return rc; |
| 811 | } |
| 812 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 813 | if (iadc->iadc_poll_eoc) { |
| 814 | while (status1 != QPNP_STATUS1_EOC) { |
| 815 | rc = qpnp_iadc_read_reg(iadc, QPNP_STATUS1, &status1); |
| 816 | if (rc < 0) |
| 817 | return rc; |
| 818 | status1 &= QPNP_STATUS1_REQ_STS_EOC_MASK; |
| 819 | usleep_range(QPNP_ADC_CONV_TIME_MIN, |
| 820 | QPNP_ADC_CONV_TIME_MAX); |
| 821 | count++; |
| 822 | if (count > QPNP_ADC_ERR_COUNT) { |
| 823 | pr_err("retry error exceeded\n"); |
| 824 | rc = qpnp_iadc_status_debug(iadc); |
| 825 | if (rc < 0) |
| 826 | pr_err("IADC status debug failed\n"); |
| 827 | rc = -EINVAL; |
Siddartha Mohanadoss | d3a3c95 | 2012-12-10 16:55:19 -0800 | [diff] [blame] | 828 | return rc; |
| 829 | } |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 830 | } |
| 831 | } else { |
| 832 | rc = wait_for_completion_timeout( |
| 833 | &iadc->adc->adc_rslt_completion, |
| 834 | QPNP_ADC_COMPLETION_TIMEOUT); |
| 835 | if (!rc) { |
| 836 | rc = qpnp_iadc_read_reg(iadc, QPNP_STATUS1, &status1); |
| 837 | if (rc < 0) |
| 838 | return rc; |
| 839 | status1 &= QPNP_STATUS1_REQ_STS_EOC_MASK; |
| 840 | if (status1 == QPNP_STATUS1_EOC) |
| 841 | pr_debug("End of conversion status set\n"); |
| 842 | else { |
| 843 | rc = qpnp_iadc_status_debug(iadc); |
| 844 | if (rc < 0) { |
| 845 | pr_err("status debug failed %d\n", rc); |
| 846 | return rc; |
| 847 | } |
| 848 | return -EINVAL; |
| 849 | } |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 853 | rc = qpnp_iadc_read_conversion_result(iadc, raw_code); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 854 | if (rc) { |
| 855 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 856 | return rc; |
| 857 | } |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 862 | #define IADC_CENTER 0xC000 |
| 863 | #define IADC_READING_RESOLUTION_N 542535 |
| 864 | #define IADC_READING_RESOLUTION_D 100000 |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 865 | 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] | 866 | { |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 867 | s64 numerator; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 868 | |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 869 | if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) { |
| 870 | pr_err("raw offset errors! raw_gain:0x%x and raw_offset:0x%x\n", |
| 871 | iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw); |
| 872 | return -EINVAL; |
| 873 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 874 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 875 | numerator = iadc->adc->calib.offset_raw - IADC_CENTER; |
| 876 | numerator *= IADC_READING_RESOLUTION_N; |
| 877 | iadc->adc->calib.offset_uv = div_s64(numerator, |
| 878 | IADC_READING_RESOLUTION_D); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 879 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 880 | numerator = iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw; |
| 881 | numerator *= IADC_READING_RESOLUTION_N; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 882 | |
Abhijeet Dharmapurikar | 29005fa | 2013-07-15 17:57:27 -0700 | [diff] [blame] | 883 | iadc->adc->calib.gain_uv = div_s64(numerator, |
| 884 | IADC_READING_RESOLUTION_D); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 885 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 886 | pr_debug("gain_uv:%d offset_uv:%d\n", |
| 887 | iadc->adc->calib.gain_uv, iadc->adc->calib.offset_uv); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 888 | return 0; |
| 889 | } |
| 890 | |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 891 | #define IADC_IDEAL_RAW_GAIN 3291 |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 892 | int32_t qpnp_iadc_calibrate_for_trim(struct qpnp_iadc_chip *iadc, |
| 893 | bool batfet_closed) |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 894 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 895 | uint8_t rslt_lsb, rslt_msb; |
| 896 | int32_t rc = 0; |
| 897 | uint16_t raw_data; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 898 | uint32_t mode_sel = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 899 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 900 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 901 | return -EPROBE_DEFER; |
| 902 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 903 | mutex_lock(&iadc->adc->adc_lock); |
| 904 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 905 | if (iadc->iadc_poll_eoc) { |
| 906 | pr_debug("acquiring iadc eoc wakelock\n"); |
| 907 | pm_stay_awake(iadc->dev); |
| 908 | } |
| 909 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 910 | rc = qpnp_iadc_configure(iadc, GAIN_CALIBRATION_17P857MV, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 911 | &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 912 | if (rc < 0) { |
| 913 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 914 | goto fail; |
| 915 | } |
| 916 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 917 | iadc->adc->calib.gain_raw = raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 918 | |
Abhijeet Dharmapurikar | 0ef9b5c | 2013-07-15 18:24:38 -0700 | [diff] [blame] | 919 | /* |
| 920 | * there is a features in the BMS where if the batfet is opened |
| 921 | * the BMS reads from INTERNAL_RSENSE (channel 0) actually go to |
| 922 | * OFFSET_CALIBRATION_CSP_CSN (channel 5). Hence if batfet is opened |
| 923 | * we have to calibrate based on OFFSET_CALIBRATION_CSP_CSN even for |
| 924 | * internal rsense. |
| 925 | */ |
| 926 | if (!batfet_closed || iadc->external_rsense) { |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 927 | /* external offset calculation */ |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 928 | rc = qpnp_iadc_configure(iadc, OFFSET_CALIBRATION_CSP_CSN, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 929 | &raw_data, mode_sel); |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 930 | if (rc < 0) { |
| 931 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 932 | goto fail; |
| 933 | } |
| 934 | } else { |
| 935 | /* internal offset calculation */ |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 936 | rc = qpnp_iadc_configure(iadc, OFFSET_CALIBRATION_CSP2_CSN2, |
Siddartha Mohanadoss | ce7694c | 2013-07-08 16:47:28 -0700 | [diff] [blame] | 937 | &raw_data, mode_sel); |
| 938 | if (rc < 0) { |
| 939 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 940 | goto fail; |
| 941 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 944 | iadc->adc->calib.offset_raw = raw_data; |
| 945 | if (rc < 0) { |
| 946 | pr_err("qpnp adc offset/gain calculation failed\n"); |
| 947 | goto fail; |
| 948 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 949 | |
Siddartha Mohanadoss | c692168 | 2013-08-07 08:45:41 -0700 | [diff] [blame] | 950 | if (iadc->iadc_comp.revision_dig_major == QPNP_IADC_PM8026_2_REV2 |
| 951 | && iadc->iadc_comp.revision_ana_minor == |
| 952 | QPNP_IADC_PM8026_2_REV3) |
Xiaozhe Shi | f84c8bb | 2013-07-30 12:22:03 -0700 | [diff] [blame] | 953 | iadc->adc->calib.gain_raw = |
| 954 | iadc->adc->calib.offset_raw + IADC_IDEAL_RAW_GAIN; |
| 955 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 956 | pr_debug("raw gain:0x%x, raw offset:0x%x\n", |
| 957 | iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw); |
| 958 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 959 | rc = qpnp_convert_raw_offset_voltage(iadc); |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 960 | if (rc < 0) { |
| 961 | pr_err("qpnp raw_voltage conversion failed\n"); |
| 962 | goto fail; |
| 963 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 964 | |
| 965 | rslt_msb = (raw_data & QPNP_RAW_CODE_16_BIT_MSB_MASK) >> |
| 966 | QPNP_BIT_SHIFT_8; |
| 967 | rslt_lsb = raw_data & QPNP_RAW_CODE_16_BIT_LSB_MASK; |
| 968 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 969 | pr_debug("trim values:lsb:0x%x and msb:0x%x\n", rslt_lsb, rslt_msb); |
| 970 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 971 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_SEC_ACCESS, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 972 | QPNP_IADC_SEC_ACCESS_DATA); |
| 973 | if (rc < 0) { |
| 974 | pr_err("qpnp iadc configure error for sec access\n"); |
| 975 | goto fail; |
| 976 | } |
| 977 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 978 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_MSB_OFFSET, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 979 | rslt_msb); |
| 980 | if (rc < 0) { |
| 981 | pr_err("qpnp iadc configure error for MSB write\n"); |
| 982 | goto fail; |
| 983 | } |
| 984 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 985 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_SEC_ACCESS, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 986 | QPNP_IADC_SEC_ACCESS_DATA); |
| 987 | if (rc < 0) { |
| 988 | pr_err("qpnp iadc configure error for sec access\n"); |
| 989 | goto fail; |
| 990 | } |
| 991 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 992 | rc = qpnp_iadc_write_reg(iadc, QPNP_IADC_LSB_OFFSET, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 993 | rslt_lsb); |
| 994 | if (rc < 0) { |
| 995 | pr_err("qpnp iadc configure error for LSB write\n"); |
| 996 | goto fail; |
| 997 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 998 | fail: |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 999 | if (iadc->iadc_poll_eoc) { |
| 1000 | pr_debug("releasing iadc eoc wakelock\n"); |
| 1001 | pm_relax(iadc->dev); |
| 1002 | } |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1003 | mutex_unlock(&iadc->adc->adc_lock); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1004 | return rc; |
| 1005 | } |
Siddartha Mohanadoss | 0667392 | 2013-03-27 11:14:19 -0700 | [diff] [blame] | 1006 | EXPORT_SYMBOL(qpnp_iadc_calibrate_for_trim); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1007 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1008 | static void qpnp_iadc_work(struct work_struct *work) |
| 1009 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1010 | struct qpnp_iadc_chip *iadc = container_of(work, |
| 1011 | struct qpnp_iadc_chip, iadc_work.work); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1012 | int rc = 0; |
| 1013 | |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1014 | if (!iadc->skip_auto_calibrations) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1015 | rc = qpnp_iadc_calibrate_for_trim(iadc, true); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1016 | if (rc) |
| 1017 | pr_debug("periodic IADC calibration failed\n"); |
| 1018 | } |
| 1019 | |
| 1020 | schedule_delayed_work(&iadc->iadc_work, |
| 1021 | round_jiffies_relative(msecs_to_jiffies |
| 1022 | (QPNP_IADC_CALIB_SECONDS))); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1023 | return; |
| 1024 | } |
| 1025 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1026 | static int32_t qpnp_iadc_version_check(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1027 | { |
| 1028 | uint8_t revision; |
| 1029 | int rc; |
| 1030 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1031 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_REVISION2, &revision); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1032 | if (rc < 0) { |
| 1033 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 1034 | return rc; |
| 1035 | } |
| 1036 | |
| 1037 | if (revision < QPNP_IADC_SUPPORTED_REVISION2) { |
| 1038 | pr_err("IADC Version not supported\n"); |
| 1039 | return -EINVAL; |
| 1040 | } |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1045 | 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] | 1046 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1047 | struct qpnp_iadc_chip *iadc; |
| 1048 | struct device_node *node = NULL; |
| 1049 | char prop_name[QPNP_MAX_PROP_NAME_LEN]; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1050 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1051 | snprintf(prop_name, QPNP_MAX_PROP_NAME_LEN, "qcom,%s-iadc", name); |
| 1052 | |
| 1053 | node = of_parse_phandle(dev->of_node, prop_name, 0); |
| 1054 | if (node == NULL) |
| 1055 | return ERR_PTR(-ENODEV); |
| 1056 | |
| 1057 | list_for_each_entry(iadc, &qpnp_iadc_device_list, list) |
| 1058 | if (iadc->adc->spmi->dev.of_node == node) |
| 1059 | return iadc; |
| 1060 | return ERR_PTR(-EPROBE_DEFER); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1061 | } |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1062 | EXPORT_SYMBOL(qpnp_get_iadc); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1063 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1064 | 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] | 1065 | { |
| 1066 | uint8_t rslt_rsense; |
Siddartha Mohanadoss | 5827954 | 2013-05-28 16:20:46 -0700 | [diff] [blame] | 1067 | int32_t rc = 0, sign_bit = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1068 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1069 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1070 | return -EPROBE_DEFER; |
| 1071 | |
Siddartha Mohanadoss | 5827954 | 2013-05-28 16:20:46 -0700 | [diff] [blame] | 1072 | if (iadc->external_rsense) { |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 1073 | *rsense = iadc->rsense; |
Siddartha Mohanadoss | 5827954 | 2013-05-28 16:20:46 -0700 | [diff] [blame] | 1074 | return rc; |
| 1075 | } |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 1076 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1077 | if (iadc->default_internal_rsense) { |
| 1078 | *rsense = iadc->rsense_workaround_value; |
| 1079 | return rc; |
| 1080 | } |
| 1081 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1082 | rc = qpnp_iadc_read_reg(iadc, QPNP_IADC_NOMINAL_RSENSE, &rslt_rsense); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1083 | if (rc < 0) { |
| 1084 | pr_err("qpnp adc rsense read failed with %d\n", rc); |
| 1085 | return rc; |
| 1086 | } |
| 1087 | |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 1088 | pr_debug("rsense:0%x\n", rslt_rsense); |
| 1089 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1090 | if (rslt_rsense & QPNP_RSENSE_MSB_SIGN_CHECK) |
| 1091 | sign_bit = 1; |
| 1092 | |
| 1093 | rslt_rsense &= ~QPNP_RSENSE_MSB_SIGN_CHECK; |
| 1094 | |
| 1095 | if (sign_bit) |
| 1096 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR - |
| 1097 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 1098 | else |
| 1099 | *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR + |
| 1100 | (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT); |
| 1101 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1102 | pr_debug("rsense value is %d\n", *rsense); |
| 1103 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1104 | return rc; |
| 1105 | } |
Xiaozhe Shi | 767fdb6 | 2013-01-10 15:09:08 -0800 | [diff] [blame] | 1106 | EXPORT_SYMBOL(qpnp_iadc_get_rsense); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1107 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1108 | static int32_t qpnp_check_pmic_temp(struct qpnp_iadc_chip *iadc) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1109 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1110 | struct qpnp_vadc_result result_pmic_therm; |
Siddartha Mohanadoss | d4e9edc | 2013-04-17 14:42:16 -0700 | [diff] [blame] | 1111 | int64_t die_temp_offset; |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 1112 | int rc = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1113 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1114 | rc = qpnp_vadc_read(iadc->vadc_dev, DIE_TEMP, &result_pmic_therm); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1115 | if (rc < 0) |
| 1116 | return rc; |
| 1117 | |
Siddartha Mohanadoss | d4e9edc | 2013-04-17 14:42:16 -0700 | [diff] [blame] | 1118 | die_temp_offset = result_pmic_therm.physical - |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 1119 | iadc->die_temp; |
Siddartha Mohanadoss | d4e9edc | 2013-04-17 14:42:16 -0700 | [diff] [blame] | 1120 | if (die_temp_offset < 0) |
| 1121 | die_temp_offset = -die_temp_offset; |
| 1122 | |
| 1123 | if (die_temp_offset > QPNP_IADC_DIE_TEMP_CALIB_OFFSET) { |
Abhijeet Dharmapurikar | 0ef9b5c | 2013-07-15 18:24:38 -0700 | [diff] [blame] | 1124 | iadc->die_temp = result_pmic_therm.physical; |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1125 | if (!iadc->skip_auto_calibrations) { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1126 | rc = qpnp_iadc_calibrate_for_trim(iadc, true); |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1127 | if (rc) |
| 1128 | pr_err("IADC calibration failed rc = %d\n", rc); |
| 1129 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Siddartha Mohanadoss | d752e47 | 2013-02-26 18:30:14 -0800 | [diff] [blame] | 1132 | return rc; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1135 | int32_t qpnp_iadc_read(struct qpnp_iadc_chip *iadc, |
| 1136 | enum qpnp_iadc_channels channel, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1137 | struct qpnp_iadc_result *result) |
| 1138 | { |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1139 | int32_t rc, rsense_n_ohms, sign = 0, num, mode_sel = 0; |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 1140 | int32_t rsense_u_ohms = 0; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1141 | int64_t result_current; |
| 1142 | uint16_t raw_data; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1143 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1144 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 1145 | return -EPROBE_DEFER; |
| 1146 | |
Dipen Parmar | 454c4b1 | 2013-11-25 14:40:47 +0530 | [diff] [blame] | 1147 | if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) { |
| 1148 | pr_err("raw offset errors! run iadc calibration again\n"); |
| 1149 | return -EINVAL; |
| 1150 | } |
| 1151 | |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1152 | rc = qpnp_check_pmic_temp(iadc); |
| 1153 | if (rc) { |
| 1154 | pr_err("Error checking pmic therm temp\n"); |
| 1155 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1158 | mutex_lock(&iadc->adc->adc_lock); |
| 1159 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1160 | if (iadc->iadc_poll_eoc) { |
| 1161 | pr_debug("acquiring iadc eoc wakelock\n"); |
| 1162 | pm_stay_awake(iadc->dev); |
| 1163 | } |
| 1164 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1165 | rc = qpnp_iadc_configure(iadc, channel, &raw_data, mode_sel); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1166 | if (rc < 0) { |
| 1167 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 1168 | goto fail; |
| 1169 | } |
| 1170 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1171 | rc = qpnp_iadc_get_rsense(iadc, &rsense_n_ohms); |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 1172 | pr_debug("current raw:0%x and rsense:%d\n", |
| 1173 | raw_data, rsense_n_ohms); |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 1174 | rsense_u_ohms = rsense_n_ohms/1000; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1175 | num = raw_data - iadc->adc->calib.offset_raw; |
| 1176 | if (num < 0) { |
| 1177 | sign = 1; |
| 1178 | num = -num; |
| 1179 | } |
| 1180 | |
| 1181 | result->result_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 1182 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 1183 | result_current = result->result_uv; |
| 1184 | result_current *= QPNP_IADC_NANO_VOLTS_FACTOR; |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 1185 | /* Intentional fall through. Process the result w/o comp */ |
Siddartha Mohanadoss | 22618be | 2013-04-02 15:02:19 -0700 | [diff] [blame] | 1186 | do_div(result_current, rsense_u_ohms); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1187 | |
| 1188 | if (sign) { |
| 1189 | result->result_uv = -result->result_uv; |
| 1190 | result_current = -result_current; |
| 1191 | } |
Xiaozhe Shi | e51d554 | 2013-11-01 13:06:14 -0700 | [diff] [blame] | 1192 | result_current *= -1; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1193 | rc = qpnp_iadc_comp_result(iadc, &result_current); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 1194 | if (rc < 0) |
| 1195 | pr_err("Error during compensating the IADC\n"); |
| 1196 | rc = 0; |
Xiaozhe Shi | e51d554 | 2013-11-01 13:06:14 -0700 | [diff] [blame] | 1197 | result_current *= -1; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1198 | |
| 1199 | result->result_ua = (int32_t) result_current; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1200 | fail: |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1201 | if (iadc->iadc_poll_eoc) { |
| 1202 | pr_debug("releasing iadc eoc wakelock\n"); |
| 1203 | pm_relax(iadc->dev); |
| 1204 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1205 | mutex_unlock(&iadc->adc->adc_lock); |
| 1206 | |
| 1207 | return rc; |
| 1208 | } |
| 1209 | EXPORT_SYMBOL(qpnp_iadc_read); |
| 1210 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1211 | int32_t qpnp_iadc_get_gain_and_offset(struct qpnp_iadc_chip *iadc, |
| 1212 | struct qpnp_iadc_calib *result) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1213 | { |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1214 | int rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1215 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1216 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1217 | return -EPROBE_DEFER; |
| 1218 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1219 | rc = qpnp_check_pmic_temp(iadc); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1220 | if (rc) { |
| 1221 | pr_err("Error checking pmic therm temp\n"); |
| 1222 | return rc; |
| 1223 | } |
| 1224 | |
| 1225 | mutex_lock(&iadc->adc->adc_lock); |
| 1226 | result->gain_raw = iadc->adc->calib.gain_raw; |
| 1227 | result->ideal_gain_nv = QPNP_ADC_GAIN_NV; |
| 1228 | result->gain_uv = iadc->adc->calib.gain_uv; |
| 1229 | result->offset_raw = iadc->adc->calib.offset_raw; |
| 1230 | result->ideal_offset_uv = |
| 1231 | QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL; |
| 1232 | result->offset_uv = iadc->adc->calib.offset_uv; |
Siddartha Mohanadoss | b2a4237 | 2013-03-26 15:53:41 -0700 | [diff] [blame] | 1233 | pr_debug("raw gain:0%x, raw offset:0%x\n", |
| 1234 | result->gain_raw, result->offset_raw); |
| 1235 | pr_debug("gain_uv:%d offset_uv:%d\n", |
| 1236 | result->gain_uv, result->offset_uv); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1237 | mutex_unlock(&iadc->adc->adc_lock); |
| 1238 | |
| 1239 | return 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1240 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1241 | EXPORT_SYMBOL(qpnp_iadc_get_gain_and_offset); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1242 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1243 | int qpnp_iadc_skip_calibration(struct qpnp_iadc_chip *iadc) |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1244 | { |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1245 | iadc->skip_auto_calibrations = true; |
| 1246 | return 0; |
| 1247 | } |
| 1248 | EXPORT_SYMBOL(qpnp_iadc_skip_calibration); |
| 1249 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1250 | int qpnp_iadc_resume_calibration(struct qpnp_iadc_chip *iadc) |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1251 | { |
Abhijeet Dharmapurikar | 84b13dd | 2013-07-08 18:43:56 -0700 | [diff] [blame] | 1252 | iadc->skip_auto_calibrations = false; |
| 1253 | return 0; |
| 1254 | } |
| 1255 | EXPORT_SYMBOL(qpnp_iadc_resume_calibration); |
| 1256 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1257 | int32_t qpnp_iadc_vadc_sync_read(struct qpnp_iadc_chip *iadc, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1258 | enum qpnp_iadc_channels i_channel, struct qpnp_iadc_result *i_result, |
| 1259 | enum qpnp_vadc_channels v_channel, struct qpnp_vadc_result *v_result) |
| 1260 | { |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1261 | int rc = 0, mode_sel = 0, num = 0, rsense_n_ohms = 0, sign = 0; |
| 1262 | uint16_t raw_data; |
| 1263 | int32_t rsense_u_ohms = 0; |
| 1264 | int64_t result_current; |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1265 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1266 | if (qpnp_iadc_is_valid(iadc) < 0) |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1267 | return -EPROBE_DEFER; |
| 1268 | |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1269 | mutex_lock(&iadc->adc->adc_lock); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1270 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1271 | if (iadc->iadc_poll_eoc) { |
| 1272 | pr_debug("acquiring iadc eoc wakelock\n"); |
| 1273 | pm_stay_awake(iadc->dev); |
| 1274 | } |
| 1275 | |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1276 | iadc->iadc_mode_sel = true; |
| 1277 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1278 | rc = qpnp_vadc_iadc_sync_request(iadc->vadc_dev, v_channel); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1279 | if (rc) { |
| 1280 | pr_err("Configuring VADC failed\n"); |
| 1281 | goto fail; |
| 1282 | } |
| 1283 | |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1284 | rc = qpnp_iadc_configure(iadc, i_channel, &raw_data, mode_sel); |
| 1285 | if (rc < 0) { |
| 1286 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 1287 | goto fail_release_vadc; |
| 1288 | } |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1289 | |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1290 | rc = qpnp_iadc_get_rsense(iadc, &rsense_n_ohms); |
| 1291 | pr_debug("current raw:0%x and rsense:%d\n", |
| 1292 | raw_data, rsense_n_ohms); |
| 1293 | rsense_u_ohms = rsense_n_ohms/1000; |
| 1294 | num = raw_data - iadc->adc->calib.offset_raw; |
| 1295 | if (num < 0) { |
| 1296 | sign = 1; |
| 1297 | num = -num; |
| 1298 | } |
| 1299 | |
| 1300 | i_result->result_uv = (num * QPNP_ADC_GAIN_NV)/ |
| 1301 | (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw); |
| 1302 | result_current = i_result->result_uv; |
| 1303 | result_current *= QPNP_IADC_NANO_VOLTS_FACTOR; |
| 1304 | /* Intentional fall through. Process the result w/o comp */ |
| 1305 | do_div(result_current, rsense_u_ohms); |
| 1306 | |
| 1307 | if (sign) { |
| 1308 | i_result->result_uv = -i_result->result_uv; |
| 1309 | result_current = -result_current; |
| 1310 | } |
| 1311 | result_current *= -1; |
| 1312 | rc = qpnp_iadc_comp_result(iadc, &result_current); |
| 1313 | if (rc < 0) |
| 1314 | pr_err("Error during compensating the IADC\n"); |
| 1315 | rc = 0; |
| 1316 | result_current *= -1; |
| 1317 | |
| 1318 | i_result->result_ua = (int32_t) result_current; |
| 1319 | |
| 1320 | fail_release_vadc: |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1321 | rc = qpnp_vadc_iadc_sync_complete_request(iadc->vadc_dev, v_channel, |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1322 | v_result); |
| 1323 | if (rc) |
| 1324 | pr_err("Releasing VADC failed\n"); |
| 1325 | fail: |
| 1326 | iadc->iadc_mode_sel = false; |
| 1327 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1328 | if (iadc->iadc_poll_eoc) { |
| 1329 | pr_debug("releasing iadc eoc wakelock\n"); |
| 1330 | pm_relax(iadc->dev); |
| 1331 | } |
Siddartha Mohanadoss | 797508e | 2013-11-22 16:28:30 -0800 | [diff] [blame] | 1332 | mutex_unlock(&iadc->adc->adc_lock); |
Siddartha Mohanadoss | a32ea2a | 2013-02-12 09:58:31 -0800 | [diff] [blame] | 1333 | |
| 1334 | return rc; |
| 1335 | } |
| 1336 | EXPORT_SYMBOL(qpnp_iadc_vadc_sync_read); |
| 1337 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1338 | static ssize_t qpnp_iadc_show(struct device *dev, |
| 1339 | struct device_attribute *devattr, char *buf) |
| 1340 | { |
| 1341 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1342 | struct qpnp_iadc_chip *iadc = dev_get_drvdata(dev); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1343 | struct qpnp_iadc_result result; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1344 | int rc = -1; |
| 1345 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1346 | rc = qpnp_iadc_read(iadc, attr->index, &result); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1347 | |
| 1348 | if (rc) |
| 1349 | return 0; |
| 1350 | |
| 1351 | return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH, |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1352 | "Result:%d\n", result.result_ua); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | static struct sensor_device_attribute qpnp_adc_attr = |
| 1356 | SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0); |
| 1357 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1358 | static int32_t qpnp_iadc_init_hwmon(struct qpnp_iadc_chip *iadc, |
| 1359 | struct spmi_device *spmi) |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1360 | { |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1361 | struct device_node *child; |
| 1362 | struct device_node *node = spmi->dev.of_node; |
| 1363 | int rc = 0, i = 0, channel; |
| 1364 | |
| 1365 | for_each_child_of_node(node, child) { |
| 1366 | channel = iadc->adc->adc_channels[i].channel_num; |
| 1367 | qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num; |
| 1368 | qpnp_adc_attr.dev_attr.attr.name = |
| 1369 | iadc->adc->adc_channels[i].name; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1370 | memcpy(&iadc->sens_attr[i], &qpnp_adc_attr, |
| 1371 | sizeof(qpnp_adc_attr)); |
Stephen Boyd | 8a5c4e4 | 2012-10-30 11:07:22 -0700 | [diff] [blame] | 1372 | sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1373 | rc = device_create_file(&spmi->dev, |
| 1374 | &iadc->sens_attr[i].dev_attr); |
| 1375 | if (rc) { |
| 1376 | dev_err(&spmi->dev, |
| 1377 | "device_create_file failed for dev %s\n", |
| 1378 | iadc->adc->adc_channels[i].name); |
| 1379 | goto hwmon_err_sens; |
| 1380 | } |
| 1381 | i++; |
| 1382 | } |
| 1383 | |
| 1384 | return 0; |
| 1385 | hwmon_err_sens: |
| 1386 | pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc); |
| 1387 | return rc; |
| 1388 | } |
| 1389 | |
| 1390 | static int __devinit qpnp_iadc_probe(struct spmi_device *spmi) |
| 1391 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1392 | struct qpnp_iadc_chip *iadc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1393 | struct qpnp_adc_drv *adc_qpnp; |
| 1394 | struct device_node *node = spmi->dev.of_node; |
| 1395 | struct device_node *child; |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1396 | struct resource *res; |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1397 | int rc, count_adc_channel_list = 0, i = 0; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1398 | |
| 1399 | for_each_child_of_node(node, child) |
| 1400 | count_adc_channel_list++; |
| 1401 | |
| 1402 | if (!count_adc_channel_list) { |
| 1403 | pr_err("No channel listing\n"); |
| 1404 | return -EINVAL; |
| 1405 | } |
| 1406 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1407 | iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_chip) + |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1408 | (sizeof(struct sensor_device_attribute) * |
| 1409 | count_adc_channel_list), GFP_KERNEL); |
| 1410 | if (!iadc) { |
| 1411 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 1412 | return -ENOMEM; |
| 1413 | } |
| 1414 | |
| 1415 | adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv), |
| 1416 | GFP_KERNEL); |
| 1417 | if (!adc_qpnp) { |
| 1418 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
Siddartha Mohanadoss | c61babb | 2013-08-02 13:37:12 -0700 | [diff] [blame] | 1419 | return -ENOMEM; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1422 | iadc->dev = &(spmi->dev); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1423 | iadc->adc = adc_qpnp; |
| 1424 | |
| 1425 | rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc); |
| 1426 | if (rc) { |
| 1427 | dev_err(&spmi->dev, "failed to read device tree\n"); |
Siddartha Mohanadoss | c61babb | 2013-08-02 13:37:12 -0700 | [diff] [blame] | 1428 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1431 | res = spmi_get_resource_byname(spmi, NULL, IORESOURCE_MEM, |
| 1432 | "batt-id-trim-cnst-rds"); |
| 1433 | if (!res) { |
| 1434 | dev_err(&spmi->dev, "failed to read batt_id trim register\n"); |
| 1435 | return -EINVAL; |
| 1436 | } |
| 1437 | iadc->batt_id_trim_cnst_rds = res->start; |
| 1438 | rc = of_property_read_u32(node, "qcom,use-default-rds-trim", |
| 1439 | &iadc->rds_trim_default_type); |
| 1440 | if (rc) |
| 1441 | pr_debug("No trim workaround needed\n"); |
| 1442 | else { |
| 1443 | pr_debug("Use internal RDS trim workaround\n"); |
| 1444 | iadc->rds_trim_default_check = true; |
| 1445 | } |
| 1446 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1447 | iadc->vadc_dev = qpnp_get_vadc(&spmi->dev, "iadc"); |
| 1448 | if (IS_ERR(iadc->vadc_dev)) { |
| 1449 | rc = PTR_ERR(iadc->vadc_dev); |
| 1450 | if (rc != -EPROBE_DEFER) |
| 1451 | pr_err("vadc property missing, rc=%d\n", rc); |
Siddartha Mohanadoss | c61babb | 2013-08-02 13:37:12 -0700 | [diff] [blame] | 1452 | return rc; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 1453 | } |
| 1454 | |
Stephen Boyd | beab450 | 2013-04-25 10:18:17 -0700 | [diff] [blame] | 1455 | mutex_init(&iadc->adc->adc_lock); |
| 1456 | |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1457 | rc = of_property_read_u32(node, "qcom,rsense", |
| 1458 | &iadc->rsense); |
Siddartha Mohanadoss | e70010b | 2013-04-04 14:51:41 -0700 | [diff] [blame] | 1459 | if (rc) |
| 1460 | pr_debug("Defaulting to internal rsense\n"); |
| 1461 | else { |
| 1462 | pr_debug("Use external rsense\n"); |
| 1463 | iadc->external_rsense = true; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1466 | iadc->iadc_poll_eoc = of_property_read_bool(node, |
| 1467 | "qcom,iadc-poll-eoc"); |
| 1468 | if (!iadc->iadc_poll_eoc) { |
| 1469 | rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq_eoc, |
| 1470 | qpnp_iadc_isr, IRQF_TRIGGER_RISING, |
| 1471 | "qpnp_iadc_interrupt", iadc); |
| 1472 | if (rc) { |
| 1473 | dev_err(&spmi->dev, "failed to request adc irq\n"); |
| 1474 | return rc; |
| 1475 | } else |
| 1476 | enable_irq_wake(iadc->adc->adc_irq_eoc); |
| 1477 | } |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1478 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1479 | rc = qpnp_iadc_init_hwmon(iadc, spmi); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1480 | if (rc) { |
| 1481 | dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n"); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1482 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1483 | } |
| 1484 | iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev); |
| 1485 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1486 | rc = qpnp_iadc_version_check(iadc); |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1487 | if (rc) { |
| 1488 | dev_err(&spmi->dev, "IADC version not supported\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1489 | goto fail; |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1492 | INIT_WORK(&iadc->trigger_completion_work, qpnp_iadc_trigger_completion); |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1493 | INIT_DELAYED_WORK(&iadc->iadc_work, qpnp_iadc_work); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1494 | rc = qpnp_iadc_comp_info(iadc); |
Siddartha Mohanadoss | 4e64f8c | 2013-04-08 15:57:32 -0700 | [diff] [blame] | 1495 | if (rc) { |
| 1496 | dev_err(&spmi->dev, "abstracting IADC comp info failed!\n"); |
| 1497 | goto fail; |
| 1498 | } |
Siddartha Mohanadoss | 5e2988d | 2012-09-24 17:03:56 -0700 | [diff] [blame] | 1499 | |
Siddartha Mohanadoss | 18c4b9f | 2014-01-23 16:13:31 -0800 | [diff] [blame] | 1500 | rc = qpnp_iadc_rds_trim_update_check(iadc); |
| 1501 | if (rc) { |
| 1502 | dev_err(&spmi->dev, "Rds trim update failed!\n"); |
| 1503 | goto fail; |
| 1504 | } |
| 1505 | |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1506 | dev_set_drvdata(&spmi->dev, iadc); |
| 1507 | list_add(&iadc->list, &qpnp_iadc_device_list); |
| 1508 | rc = qpnp_iadc_calibrate_for_trim(iadc, true); |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1509 | if (rc) |
| 1510 | dev_err(&spmi->dev, "failed to calibrate for USR trim\n"); |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1511 | |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1512 | if (iadc->iadc_poll_eoc) |
| 1513 | device_init_wakeup(iadc->dev, 1); |
| 1514 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1515 | schedule_delayed_work(&iadc->iadc_work, |
| 1516 | round_jiffies_relative(msecs_to_jiffies |
| 1517 | (QPNP_IADC_CALIB_SECONDS))); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1518 | return 0; |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1519 | fail: |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1520 | for_each_child_of_node(node, child) { |
| 1521 | device_remove_file(&spmi->dev, |
| 1522 | &iadc->sens_attr[i].dev_attr); |
| 1523 | i++; |
| 1524 | } |
| 1525 | hwmon_device_unregister(iadc->iadc_hwmon); |
| 1526 | |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame] | 1527 | return rc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | static int __devexit qpnp_iadc_remove(struct spmi_device *spmi) |
| 1531 | { |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1532 | struct qpnp_iadc_chip *iadc = dev_get_drvdata(&spmi->dev); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1533 | struct device_node *node = spmi->dev.of_node; |
| 1534 | struct device_node *child; |
| 1535 | int i = 0; |
| 1536 | |
Siddartha Mohanadoss | a9b9167 | 2013-02-22 18:32:27 -0800 | [diff] [blame] | 1537 | cancel_delayed_work(&iadc->iadc_work); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1538 | for_each_child_of_node(node, child) { |
| 1539 | device_remove_file(&spmi->dev, |
| 1540 | &iadc->sens_attr[i].dev_attr); |
| 1541 | i++; |
| 1542 | } |
Siddartha Mohanadoss | 55d0bca | 2013-06-24 08:29:34 -0700 | [diff] [blame] | 1543 | hwmon_device_unregister(iadc->iadc_hwmon); |
Siddartha Mohanadoss | f82a6f2 | 2013-07-27 20:47:34 -0700 | [diff] [blame] | 1544 | if (iadc->iadc_poll_eoc) |
| 1545 | pm_relax(iadc->dev); |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 1546 | dev_set_drvdata(&spmi->dev, NULL); |
| 1547 | |
| 1548 | return 0; |
| 1549 | } |
| 1550 | |
| 1551 | static const struct of_device_id qpnp_iadc_match_table[] = { |
| 1552 | { .compatible = "qcom,qpnp-iadc", |
| 1553 | }, |
| 1554 | {} |
| 1555 | }; |
| 1556 | |
| 1557 | static struct spmi_driver qpnp_iadc_driver = { |
| 1558 | .driver = { |
| 1559 | .name = "qcom,qpnp-iadc", |
| 1560 | .of_match_table = qpnp_iadc_match_table, |
| 1561 | }, |
| 1562 | .probe = qpnp_iadc_probe, |
| 1563 | .remove = qpnp_iadc_remove, |
| 1564 | }; |
| 1565 | |
| 1566 | static int __init qpnp_iadc_init(void) |
| 1567 | { |
| 1568 | return spmi_driver_register(&qpnp_iadc_driver); |
| 1569 | } |
| 1570 | module_init(qpnp_iadc_init); |
| 1571 | |
| 1572 | static void __exit qpnp_iadc_exit(void) |
| 1573 | { |
| 1574 | spmi_driver_unregister(&qpnp_iadc_driver); |
| 1575 | } |
| 1576 | module_exit(qpnp_iadc_exit); |
| 1577 | |
| 1578 | MODULE_DESCRIPTION("QPNP PMIC current ADC driver"); |
| 1579 | MODULE_LICENSE("GPL v2"); |