Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012, The Linux Foundation. All rights reserved. |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -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/interrupt.h> |
| 29 | #include <linux/completion.h> |
| 30 | #include <linux/hwmon-sysfs.h> |
| 31 | #include <linux/qpnp/qpnp-adc.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | |
| 34 | /* QPNP VADC register definition */ |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 35 | #define QPNP_VADC_REVISION1 0x0 |
| 36 | #define QPNP_VADC_REVISION2 0x1 |
| 37 | #define QPNP_VADC_REVISION3 0x2 |
| 38 | #define QPNP_VADC_REVISION4 0x3 |
| 39 | #define QPNP_VADC_PERPH_TYPE 0x4 |
| 40 | #define QPNP_VADC_PERH_SUBTYPE 0x5 |
| 41 | |
| 42 | #define QPNP_VADC_SUPPORTED_REVISION2 1 |
| 43 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 44 | #define QPNP_VADC_STATUS1 0x8 |
| 45 | #define QPNP_VADC_STATUS1_OP_MODE 4 |
| 46 | #define QPNP_VADC_STATUS1_MEAS_INTERVAL_EN_STS BIT(2) |
| 47 | #define QPNP_VADC_STATUS1_REQ_STS BIT(1) |
| 48 | #define QPNP_VADC_STATUS1_EOC BIT(0) |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 49 | #define QPNP_VADC_STATUS1_REQ_STS_EOC_MASK 0x3 |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 50 | #define QPNP_VADC_STATUS2 0x9 |
| 51 | #define QPNP_VADC_STATUS2_CONV_SEQ_STATE 6 |
| 52 | #define QPNP_VADC_STATUS2_FIFO_NOT_EMPTY_FLAG BIT(1) |
| 53 | #define QPNP_VADC_STATUS2_CONV_SEQ_TIMEOUT_STS BIT(0) |
| 54 | #define QPNP_VADC_STATUS2_CONV_SEQ_STATE_SHIFT 4 |
| 55 | #define QPNP_VADC_CONV_TIMEOUT_ERR 2 |
| 56 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 57 | #define QPNP_VADC_MODE_CTL 0x40 |
| 58 | #define QPNP_VADC_OP_MODE_SHIFT 4 |
| 59 | #define QPNP_VADC_VREF_XO_THM_FORCE BIT(2) |
| 60 | #define QPNP_VADC_AMUX_TRIM_EN BIT(1) |
| 61 | #define QPNP_VADC_ADC_TRIM_EN BIT(0) |
| 62 | #define QPNP_VADC_EN_CTL1 0x46 |
| 63 | #define QPNP_VADC_ADC_EN BIT(7) |
| 64 | #define QPNP_VADC_ADC_CH_SEL_CTL 0x48 |
| 65 | #define QPNP_VADC_ADC_DIG_PARAM 0x50 |
| 66 | #define QPNP_VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT 3 |
| 67 | #define QPNP_VADC_HW_SETTLE_DELAY 0x51 |
| 68 | #define QPNP_VADC_CONV_REQ 0x52 |
| 69 | #define QPNP_VADC_CONV_REQ_SET BIT(7) |
| 70 | #define QPNP_VADC_CONV_SEQ_CTL 0x54 |
| 71 | #define QPNP_VADC_CONV_SEQ_HOLDOFF_SHIFT 4 |
| 72 | #define QPNP_VADC_CONV_SEQ_TRIG_CTL 0x55 |
| 73 | #define QPNP_VADC_CONV_SEQ_FALLING_EDGE 0x0 |
| 74 | #define QPNP_VADC_CONV_SEQ_RISING_EDGE 0x1 |
| 75 | #define QPNP_VADC_CONV_SEQ_EDGE_SHIFT 7 |
| 76 | #define QPNP_VADC_FAST_AVG_CTL 0x5a |
| 77 | |
| 78 | #define QPNP_VADC_M0_LOW_THR_LSB 0x5c |
| 79 | #define QPNP_VADC_M0_LOW_THR_MSB 0x5d |
| 80 | #define QPNP_VADC_M0_HIGH_THR_LSB 0x5e |
| 81 | #define QPNP_VADC_M0_HIGH_THR_MSB 0x5f |
| 82 | #define QPNP_VADC_M1_LOW_THR_LSB 0x69 |
| 83 | #define QPNP_VADC_M1_LOW_THR_MSB 0x6a |
| 84 | #define QPNP_VADC_M1_HIGH_THR_LSB 0x6b |
| 85 | #define QPNP_VADC_M1_HIGH_THR_MSB 0x6c |
| 86 | |
| 87 | #define QPNP_VADC_DATA0 0x60 |
| 88 | #define QPNP_VADC_DATA1 0x61 |
| 89 | #define QPNP_VADC_CONV_TIMEOUT_ERR 2 |
| 90 | #define QPNP_VADC_CONV_TIME_MIN 2000 |
| 91 | #define QPNP_VADC_CONV_TIME_MAX 2100 |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 92 | #define QPNP_ADC_COMPLETION_TIMEOUT HZ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 93 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 94 | struct qpnp_vadc_drv { |
| 95 | struct qpnp_adc_drv *adc; |
| 96 | struct dentry *dent; |
| 97 | struct device *vadc_hwmon; |
| 98 | bool vadc_init_calib; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 99 | bool vadc_initialized; |
| 100 | int max_channels_available; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 101 | struct sensor_device_attribute sens_attr[0]; |
| 102 | }; |
| 103 | |
| 104 | struct qpnp_vadc_drv *qpnp_vadc; |
| 105 | |
| 106 | static struct qpnp_vadc_scale_fn vadc_scale_fn[] = { |
| 107 | [SCALE_DEFAULT] = {qpnp_adc_scale_default}, |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 108 | [SCALE_BATT_THERM] = {qpnp_adc_scale_batt_therm}, |
| 109 | [SCALE_PMIC_THERM] = {qpnp_adc_scale_pmic_therm}, |
| 110 | [SCALE_XOTHERM] = {qpnp_adc_tdkntcg_therm}, |
Siddartha Mohanadoss | e77edf1 | 2012-09-13 14:26:32 -0700 | [diff] [blame] | 111 | [SCALE_THERM_100K_PULLUP] = {qpnp_adc_scale_therm_pu2}, |
| 112 | [SCALE_THERM_150K_PULLUP] = {qpnp_adc_scale_therm_pu1}, |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static int32_t qpnp_vadc_read_reg(int16_t reg, u8 *data) |
| 116 | { |
| 117 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
| 118 | int rc; |
| 119 | |
| 120 | rc = spmi_ext_register_readl(vadc->adc->spmi->ctrl, vadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 121 | (vadc->adc->offset + reg), data, 1); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 122 | if (rc < 0) { |
| 123 | pr_err("qpnp adc read reg %d failed with %d\n", reg, rc); |
| 124 | return rc; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int32_t qpnp_vadc_write_reg(int16_t reg, u8 data) |
| 131 | { |
| 132 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
| 133 | int rc; |
| 134 | u8 *buf; |
| 135 | |
| 136 | buf = &data; |
| 137 | |
| 138 | rc = spmi_ext_register_writel(vadc->adc->spmi->ctrl, vadc->adc->slave, |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 139 | (vadc->adc->offset + reg), buf, 1); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 140 | if (rc < 0) { |
| 141 | pr_err("qpnp adc write reg %d failed with %d\n", reg, rc); |
| 142 | return rc; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 148 | static int32_t qpnp_vadc_enable(bool state) |
| 149 | { |
| 150 | int rc = 0; |
| 151 | u8 data = 0; |
| 152 | |
| 153 | data = QPNP_VADC_ADC_EN; |
| 154 | if (state) { |
| 155 | rc = qpnp_vadc_write_reg(QPNP_VADC_EN_CTL1, |
| 156 | data); |
| 157 | if (rc < 0) { |
| 158 | pr_err("VADC enable failed\n"); |
| 159 | return rc; |
| 160 | } |
| 161 | } else { |
| 162 | rc = qpnp_vadc_write_reg(QPNP_VADC_EN_CTL1, |
| 163 | (~data & QPNP_VADC_ADC_EN)); |
| 164 | if (rc < 0) { |
| 165 | pr_err("VADC disable failed\n"); |
| 166 | return rc; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Siddartha Mohanadoss | 9cb2c65 | 2012-12-14 19:18:18 -0800 | [diff] [blame] | 173 | static int32_t qpnp_vadc_status_debug(void) |
| 174 | { |
| 175 | int rc = 0; |
| 176 | u8 mode = 0, status1 = 0, chan = 0, dig = 0, en = 0, status2 = 0; |
| 177 | |
| 178 | rc = qpnp_vadc_read_reg(QPNP_VADC_MODE_CTL, &mode); |
| 179 | if (rc < 0) { |
| 180 | pr_err("mode ctl register read failed with %d\n", rc); |
| 181 | return rc; |
| 182 | } |
| 183 | |
| 184 | rc = qpnp_vadc_read_reg(QPNP_VADC_ADC_DIG_PARAM, &dig); |
| 185 | if (rc < 0) { |
| 186 | pr_err("digital param read failed with %d\n", rc); |
| 187 | return rc; |
| 188 | } |
| 189 | |
| 190 | rc = qpnp_vadc_read_reg(QPNP_VADC_ADC_CH_SEL_CTL, &chan); |
| 191 | if (rc < 0) { |
| 192 | pr_err("channel read failed with %d\n", rc); |
| 193 | return rc; |
| 194 | } |
| 195 | |
| 196 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 197 | if (rc < 0) { |
| 198 | pr_err("status1 read failed with %d\n", rc); |
| 199 | return rc; |
| 200 | } |
| 201 | |
| 202 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS2, &status2); |
| 203 | if (rc < 0) { |
| 204 | pr_err("status2 read failed with %d\n", rc); |
| 205 | return rc; |
| 206 | } |
| 207 | |
| 208 | rc = qpnp_vadc_read_reg(QPNP_VADC_EN_CTL1, &en); |
| 209 | if (rc < 0) { |
| 210 | pr_err("en read failed with %d\n", rc); |
| 211 | return rc; |
| 212 | } |
| 213 | |
| 214 | pr_err("EOC not set - status1/2:%x/%x, dig:%x, ch:%x, mode:%x, en:%x\n", |
| 215 | status1, status2, dig, chan, mode, en); |
| 216 | |
| 217 | rc = qpnp_vadc_enable(false); |
| 218 | if (rc < 0) { |
| 219 | pr_err("VADC disable failed with %d\n", rc); |
| 220 | return rc; |
| 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |
Siddartha Mohanadoss | 8dbb5c2 | 2012-12-11 14:50:45 -0800 | [diff] [blame] | 225 | static int32_t qpnp_vadc_configure( |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 226 | struct qpnp_adc_amux_properties *chan_prop) |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 227 | { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 228 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 229 | u8 decimation = 0, conv_sequence = 0, conv_sequence_trig = 0; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 230 | u8 mode_ctrl = 0; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 231 | int rc = 0; |
| 232 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 233 | /* Mode selection */ |
Siddartha Mohanadoss | 429b449 | 2012-12-11 13:29:58 -0800 | [diff] [blame] | 234 | mode_ctrl |= ((chan_prop->mode_sel << QPNP_VADC_OP_MODE_SHIFT) | |
| 235 | (QPNP_VADC_ADC_TRIM_EN | QPNP_VADC_AMUX_TRIM_EN)); |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 236 | rc = qpnp_vadc_write_reg(QPNP_VADC_MODE_CTL, mode_ctrl); |
| 237 | if (rc < 0) { |
| 238 | pr_err("Mode configure write error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 239 | return rc; |
| 240 | } |
| 241 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 242 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 243 | /* Channel selection */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 244 | rc = qpnp_vadc_write_reg(QPNP_VADC_ADC_CH_SEL_CTL, |
| 245 | chan_prop->amux_channel); |
| 246 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 247 | pr_err("Channel configure error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 248 | return rc; |
| 249 | } |
| 250 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 251 | /* Digital parameter setup */ |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 252 | decimation = chan_prop->decimation << |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 253 | QPNP_VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT; |
| 254 | rc = qpnp_vadc_write_reg(QPNP_VADC_ADC_DIG_PARAM, decimation); |
| 255 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 256 | pr_err("Digital parameter configure write error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 257 | return rc; |
| 258 | } |
| 259 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 260 | /* HW settling time delay */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 261 | rc = qpnp_vadc_write_reg(QPNP_VADC_HW_SETTLE_DELAY, |
| 262 | chan_prop->hw_settle_time); |
| 263 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 264 | pr_err("HW settling time setup error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 265 | return rc; |
| 266 | } |
| 267 | |
| 268 | if (chan_prop->mode_sel == (ADC_OP_NORMAL_MODE << |
| 269 | QPNP_VADC_OP_MODE_SHIFT)) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 270 | /* Normal measurement mode */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 271 | rc = qpnp_vadc_write_reg(QPNP_VADC_FAST_AVG_CTL, |
| 272 | chan_prop->fast_avg_setup); |
| 273 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 274 | pr_err("Fast averaging configure error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 275 | return rc; |
| 276 | } |
| 277 | } else if (chan_prop->mode_sel == (ADC_OP_CONVERSION_SEQUENCER << |
| 278 | QPNP_VADC_OP_MODE_SHIFT)) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 279 | /* Conversion sequence mode */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 280 | conv_sequence = ((ADC_SEQ_HOLD_100US << |
| 281 | QPNP_VADC_CONV_SEQ_HOLDOFF_SHIFT) | |
| 282 | ADC_CONV_SEQ_TIMEOUT_5MS); |
| 283 | rc = qpnp_vadc_write_reg(QPNP_VADC_CONV_SEQ_CTL, |
| 284 | conv_sequence); |
| 285 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 286 | pr_err("Conversion sequence error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 287 | return rc; |
| 288 | } |
| 289 | |
| 290 | conv_sequence_trig = ((QPNP_VADC_CONV_SEQ_RISING_EDGE << |
| 291 | QPNP_VADC_CONV_SEQ_EDGE_SHIFT) | |
| 292 | chan_prop->trigger_channel); |
| 293 | rc = qpnp_vadc_write_reg(QPNP_VADC_CONV_SEQ_TRIG_CTL, |
| 294 | conv_sequence_trig); |
| 295 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 296 | pr_err("Conversion trigger error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 297 | return rc; |
| 298 | } |
| 299 | } |
| 300 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 301 | INIT_COMPLETION(vadc->adc->adc_rslt_completion); |
| 302 | |
| 303 | rc = qpnp_vadc_enable(true); |
| 304 | if (rc) |
| 305 | return rc; |
| 306 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 307 | /* Request conversion */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 308 | rc = qpnp_vadc_write_reg(QPNP_VADC_CONV_REQ, QPNP_VADC_CONV_REQ_SET); |
| 309 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 310 | pr_err("Request conversion failed\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 311 | return rc; |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 316 | |
| 317 | static int32_t qpnp_vadc_read_conversion_result(int32_t *data) |
| 318 | { |
| 319 | uint8_t rslt_lsb, rslt_msb; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 320 | int rc = 0, status = 0; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 321 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 322 | status = qpnp_vadc_read_reg(QPNP_VADC_DATA0, &rslt_lsb); |
| 323 | if (status < 0) { |
| 324 | pr_err("qpnp adc result read failed for data0\n"); |
| 325 | goto fail; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 328 | status = qpnp_vadc_read_reg(QPNP_VADC_DATA1, &rslt_msb); |
| 329 | if (status < 0) { |
| 330 | pr_err("qpnp adc result read failed for data1\n"); |
| 331 | goto fail; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | *data = (rslt_msb << 8) | rslt_lsb; |
| 335 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 336 | status = qpnp_vadc_check_result(data); |
| 337 | if (status < 0) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 338 | pr_err("VADC data check failed\n"); |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 339 | goto fail; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 342 | fail: |
| 343 | rc = qpnp_vadc_enable(false); |
| 344 | if (rc) |
| 345 | return rc; |
| 346 | |
| 347 | return status; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static int32_t qpnp_vadc_read_status(int mode_sel) |
| 351 | { |
| 352 | u8 status1, status2, status2_conv_seq_state; |
| 353 | u8 status_err = QPNP_VADC_CONV_TIMEOUT_ERR; |
| 354 | int rc; |
| 355 | |
| 356 | switch (mode_sel) { |
| 357 | case (ADC_OP_CONVERSION_SEQUENCER << QPNP_VADC_OP_MODE_SHIFT): |
| 358 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 359 | if (rc) { |
| 360 | pr_err("qpnp_vadc read mask interrupt failed\n"); |
| 361 | return rc; |
| 362 | } |
| 363 | |
| 364 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS2, &status2); |
| 365 | if (rc) { |
| 366 | pr_err("qpnp_vadc read mask interrupt failed\n"); |
| 367 | return rc; |
| 368 | } |
| 369 | |
| 370 | if (!(status2 & ~QPNP_VADC_STATUS2_CONV_SEQ_TIMEOUT_STS) && |
| 371 | (status1 & (~QPNP_VADC_STATUS1_REQ_STS | |
| 372 | QPNP_VADC_STATUS1_EOC))) { |
| 373 | rc = status_err; |
| 374 | return rc; |
| 375 | } |
| 376 | |
| 377 | status2_conv_seq_state = status2 >> |
| 378 | QPNP_VADC_STATUS2_CONV_SEQ_STATE_SHIFT; |
| 379 | if (status2_conv_seq_state != ADC_CONV_SEQ_IDLE) { |
| 380 | pr_err("qpnp vadc seq error with status %d\n", |
| 381 | status2); |
| 382 | rc = -EINVAL; |
| 383 | return rc; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static void qpnp_vadc_work(struct work_struct *work) |
| 391 | { |
| 392 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 393 | |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame^] | 394 | if (!vadc || !vadc->vadc_initialized) |
| 395 | return; |
| 396 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 397 | complete(&vadc->adc->adc_rslt_completion); |
| 398 | |
| 399 | return; |
| 400 | } |
| 401 | DECLARE_WORK(trigger_completion_work, qpnp_vadc_work); |
| 402 | |
| 403 | static irqreturn_t qpnp_vadc_isr(int irq, void *dev_id) |
| 404 | { |
| 405 | schedule_work(&trigger_completion_work); |
| 406 | |
| 407 | return IRQ_HANDLED; |
| 408 | } |
| 409 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 410 | static int32_t qpnp_vadc_version_check(void) |
| 411 | { |
| 412 | uint8_t revision; |
| 413 | int rc; |
| 414 | |
| 415 | rc = qpnp_vadc_read_reg(QPNP_VADC_REVISION2, &revision); |
| 416 | if (rc < 0) { |
| 417 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 418 | return rc; |
| 419 | } |
| 420 | |
| 421 | if (revision < QPNP_VADC_SUPPORTED_REVISION2) { |
| 422 | pr_err("VADC Version not supported\n"); |
| 423 | return -EINVAL; |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 429 | static uint32_t qpnp_vadc_calib_device(void) |
| 430 | { |
| 431 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 432 | struct qpnp_adc_amux_properties conv; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 433 | int rc, calib_read_1, calib_read_2; |
| 434 | u8 status1 = 0; |
| 435 | |
| 436 | conv.amux_channel = REF_125V; |
| 437 | conv.decimation = DECIMATION_TYPE2; |
| 438 | conv.mode_sel = ADC_OP_NORMAL_MODE << QPNP_VADC_OP_MODE_SHIFT; |
| 439 | conv.hw_settle_time = ADC_CHANNEL_HW_SETTLE_DELAY_0US; |
| 440 | conv.fast_avg_setup = ADC_FAST_AVG_SAMPLE_1; |
| 441 | |
| 442 | rc = qpnp_vadc_configure(&conv); |
| 443 | if (rc) { |
| 444 | pr_err("qpnp_vadc configure failed with %d\n", rc); |
| 445 | goto calib_fail; |
| 446 | } |
| 447 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 448 | while (status1 != QPNP_VADC_STATUS1_EOC) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 449 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 450 | if (rc < 0) |
| 451 | return rc; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 452 | status1 &= QPNP_VADC_STATUS1_REQ_STS_EOC_MASK; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 453 | usleep_range(QPNP_VADC_CONV_TIME_MIN, |
| 454 | QPNP_VADC_CONV_TIME_MAX); |
| 455 | } |
| 456 | |
| 457 | rc = qpnp_vadc_read_conversion_result(&calib_read_1); |
| 458 | if (rc) { |
| 459 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 460 | goto calib_fail; |
| 461 | } |
| 462 | |
| 463 | conv.amux_channel = REF_625MV; |
| 464 | conv.decimation = DECIMATION_TYPE2; |
| 465 | conv.mode_sel = ADC_OP_NORMAL_MODE << QPNP_VADC_OP_MODE_SHIFT; |
| 466 | conv.hw_settle_time = ADC_CHANNEL_HW_SETTLE_DELAY_0US; |
| 467 | conv.fast_avg_setup = ADC_FAST_AVG_SAMPLE_1; |
| 468 | rc = qpnp_vadc_configure(&conv); |
| 469 | if (rc) { |
| 470 | pr_err("qpnp adc configure failed with %d\n", rc); |
| 471 | goto calib_fail; |
| 472 | } |
| 473 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 474 | status1 = 0; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 475 | while (status1 != QPNP_VADC_STATUS1_EOC) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 476 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 477 | if (rc < 0) |
| 478 | return rc; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 479 | status1 &= QPNP_VADC_STATUS1_REQ_STS_EOC_MASK; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 480 | usleep_range(QPNP_VADC_CONV_TIME_MIN, |
| 481 | QPNP_VADC_CONV_TIME_MAX); |
| 482 | } |
| 483 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 484 | rc = qpnp_vadc_read_conversion_result(&calib_read_2); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 485 | if (rc) { |
| 486 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 487 | goto calib_fail; |
| 488 | } |
| 489 | |
| 490 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].dy = |
| 491 | (calib_read_1 - calib_read_2); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 492 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 493 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].dx |
| 494 | = QPNP_ADC_625_UV; |
| 495 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].adc_vref = |
| 496 | calib_read_1; |
| 497 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].adc_gnd = |
| 498 | calib_read_2; |
| 499 | /* Ratiometric Calibration */ |
| 500 | conv.amux_channel = VDD_VADC; |
| 501 | conv.decimation = DECIMATION_TYPE2; |
| 502 | conv.mode_sel = ADC_OP_NORMAL_MODE << QPNP_VADC_OP_MODE_SHIFT; |
| 503 | conv.hw_settle_time = ADC_CHANNEL_HW_SETTLE_DELAY_0US; |
| 504 | conv.fast_avg_setup = ADC_FAST_AVG_SAMPLE_1; |
| 505 | rc = qpnp_vadc_configure(&conv); |
| 506 | if (rc) { |
| 507 | pr_err("qpnp adc configure failed with %d\n", rc); |
| 508 | goto calib_fail; |
| 509 | } |
| 510 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 511 | status1 = 0; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 512 | while (status1 != QPNP_VADC_STATUS1_EOC) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 513 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 514 | if (rc < 0) |
| 515 | return rc; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 516 | status1 &= QPNP_VADC_STATUS1_REQ_STS_EOC_MASK; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 517 | usleep_range(QPNP_VADC_CONV_TIME_MIN, |
| 518 | QPNP_VADC_CONV_TIME_MAX); |
| 519 | } |
| 520 | |
| 521 | rc = qpnp_vadc_read_conversion_result(&calib_read_1); |
| 522 | if (rc) { |
| 523 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 524 | goto calib_fail; |
| 525 | } |
| 526 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 527 | conv.amux_channel = GND_REF; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 528 | conv.decimation = DECIMATION_TYPE2; |
| 529 | conv.mode_sel = ADC_OP_NORMAL_MODE << QPNP_VADC_OP_MODE_SHIFT; |
| 530 | conv.hw_settle_time = ADC_CHANNEL_HW_SETTLE_DELAY_0US; |
| 531 | conv.fast_avg_setup = ADC_FAST_AVG_SAMPLE_1; |
| 532 | rc = qpnp_vadc_configure(&conv); |
| 533 | if (rc) { |
| 534 | pr_err("qpnp adc configure failed with %d\n", rc); |
| 535 | goto calib_fail; |
| 536 | } |
| 537 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 538 | status1 = 0; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 539 | while (status1 != QPNP_VADC_STATUS1_EOC) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 540 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 541 | if (rc < 0) |
| 542 | return rc; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 543 | status1 &= QPNP_VADC_STATUS1_REQ_STS_EOC_MASK; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 544 | usleep_range(QPNP_VADC_CONV_TIME_MIN, |
| 545 | QPNP_VADC_CONV_TIME_MAX); |
| 546 | } |
| 547 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 548 | rc = qpnp_vadc_read_conversion_result(&calib_read_2); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 549 | if (rc) { |
| 550 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 551 | goto calib_fail; |
| 552 | } |
| 553 | |
| 554 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].dy = |
| 555 | (calib_read_1 - calib_read_2); |
| 556 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].dx = |
| 557 | vadc->adc->adc_prop->adc_vdd_reference; |
| 558 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].adc_vref = |
| 559 | calib_read_1; |
| 560 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].adc_gnd = |
| 561 | calib_read_2; |
| 562 | |
| 563 | calib_fail: |
| 564 | return rc; |
| 565 | } |
| 566 | |
Siddartha Mohanadoss | d0f4fd1 | 2012-11-20 16:28:40 -0800 | [diff] [blame] | 567 | int32_t qpnp_get_vadc_gain_and_offset(struct qpnp_vadc_linear_graph *param, |
| 568 | enum qpnp_adc_calib_type calib_type) |
| 569 | { |
| 570 | |
| 571 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
| 572 | |
| 573 | switch (calib_type) { |
| 574 | case CALIB_RATIOMETRIC: |
| 575 | param->dy = |
| 576 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].dy; |
| 577 | param->dx = |
| 578 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].dx; |
| 579 | param->adc_vref = vadc->adc->adc_prop->adc_vdd_reference; |
| 580 | param->adc_gnd = |
| 581 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].adc_gnd; |
| 582 | break; |
| 583 | case CALIB_ABSOLUTE: |
| 584 | param->dy = |
| 585 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].dy; |
| 586 | param->dx = |
| 587 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].dx; |
| 588 | param->adc_vref = vadc->adc->adc_prop->adc_vdd_reference; |
| 589 | param->adc_gnd = |
| 590 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].adc_gnd; |
| 591 | break; |
| 592 | default: |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | EXPORT_SYMBOL(qpnp_get_vadc_gain_and_offset); |
| 599 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 600 | int32_t qpnp_vadc_is_ready(void) |
| 601 | { |
| 602 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
| 603 | |
| 604 | if (!vadc || !vadc->vadc_initialized) |
| 605 | return -EPROBE_DEFER; |
| 606 | else |
| 607 | return 0; |
| 608 | } |
| 609 | EXPORT_SYMBOL(qpnp_vadc_is_ready); |
| 610 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 611 | int32_t qpnp_vadc_conv_seq_request(enum qpnp_vadc_trigger trigger_channel, |
| 612 | enum qpnp_vadc_channels channel, |
| 613 | struct qpnp_vadc_result *result) |
| 614 | { |
| 615 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 616 | int rc = 0, scale_type, amux_prescaling, dt_index = 0; |
| 617 | |
| 618 | if (!vadc || !vadc->vadc_initialized) |
| 619 | return -EPROBE_DEFER; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 620 | |
| 621 | if (!vadc->vadc_init_calib) { |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 622 | rc = qpnp_vadc_version_check(); |
| 623 | if (rc) |
| 624 | return rc; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 625 | rc = qpnp_vadc_calib_device(); |
| 626 | if (rc) { |
| 627 | pr_err("Calibration failed\n"); |
| 628 | return rc; |
| 629 | } else |
| 630 | vadc->vadc_init_calib = true; |
| 631 | } |
| 632 | |
| 633 | mutex_lock(&vadc->adc->adc_lock); |
| 634 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 635 | vadc->adc->amux_prop->amux_channel = channel; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 636 | |
Siddartha Mohanadoss | 7126ce8 | 2012-12-11 14:33:11 -0800 | [diff] [blame] | 637 | while ((vadc->adc->adc_channels[dt_index].channel_num |
| 638 | != channel) && (dt_index < vadc->max_channels_available)) |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 639 | dt_index++; |
| 640 | |
Siddartha Mohanadoss | 7126ce8 | 2012-12-11 14:33:11 -0800 | [diff] [blame] | 641 | if (dt_index >= vadc->max_channels_available) { |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 642 | pr_err("not a valid VADC channel\n"); |
| 643 | rc = -EINVAL; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 644 | goto fail_unlock; |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 645 | } |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 646 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 647 | vadc->adc->amux_prop->decimation = |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 648 | vadc->adc->adc_channels[dt_index].adc_decimation; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 649 | vadc->adc->amux_prop->hw_settle_time = |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 650 | vadc->adc->adc_channels[dt_index].hw_settle_time; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 651 | vadc->adc->amux_prop->fast_avg_setup = |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 652 | vadc->adc->adc_channels[dt_index].fast_avg_setup; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 653 | |
| 654 | if (trigger_channel < ADC_SEQ_NONE) |
| 655 | vadc->adc->amux_prop->mode_sel = (ADC_OP_CONVERSION_SEQUENCER |
| 656 | << QPNP_VADC_OP_MODE_SHIFT); |
| 657 | else if (trigger_channel == ADC_SEQ_NONE) |
| 658 | vadc->adc->amux_prop->mode_sel = (ADC_OP_NORMAL_MODE |
| 659 | << QPNP_VADC_OP_MODE_SHIFT); |
| 660 | else { |
| 661 | pr_err("Invalid trigger channel:%d\n", trigger_channel); |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 662 | goto fail_unlock; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | vadc->adc->amux_prop->trigger_channel = trigger_channel; |
| 666 | |
| 667 | rc = qpnp_vadc_configure(vadc->adc->amux_prop); |
| 668 | if (rc) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 669 | pr_err("qpnp vadc configure failed with %d\n", rc); |
| 670 | goto fail_unlock; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 673 | rc = wait_for_completion_timeout(&vadc->adc->adc_rslt_completion, |
| 674 | QPNP_ADC_COMPLETION_TIMEOUT); |
| 675 | if (!rc) { |
| 676 | u8 status1 = 0; |
| 677 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 678 | if (rc < 0) |
| 679 | goto fail_unlock; |
| 680 | status1 &= (QPNP_VADC_STATUS1_REQ_STS | QPNP_VADC_STATUS1_EOC); |
| 681 | if (status1 == QPNP_VADC_STATUS1_EOC) |
| 682 | pr_debug("End of conversion status set\n"); |
| 683 | else { |
Siddartha Mohanadoss | 9cb2c65 | 2012-12-14 19:18:18 -0800 | [diff] [blame] | 684 | rc = qpnp_vadc_status_debug(); |
| 685 | if (rc < 0) |
| 686 | pr_err("VADC disable failed\n"); |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame] | 687 | rc = -EINVAL; |
| 688 | goto fail_unlock; |
| 689 | } |
| 690 | } |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 691 | |
| 692 | if (trigger_channel < ADC_SEQ_NONE) { |
| 693 | rc = qpnp_vadc_read_status(vadc->adc->amux_prop->mode_sel); |
| 694 | if (rc) |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 695 | pr_debug("Conversion sequence timed out - %d\n", rc); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | rc = qpnp_vadc_read_conversion_result(&result->adc_code); |
| 699 | if (rc) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 700 | pr_err("qpnp vadc read adc code failed with %d\n", rc); |
| 701 | goto fail_unlock; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 704 | amux_prescaling = |
| 705 | vadc->adc->adc_channels[dt_index].chan_path_prescaling; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 706 | |
| 707 | vadc->adc->amux_prop->chan_prop->offset_gain_numerator = |
| 708 | qpnp_vadc_amux_scaling_ratio[amux_prescaling].num; |
| 709 | vadc->adc->amux_prop->chan_prop->offset_gain_denominator = |
| 710 | qpnp_vadc_amux_scaling_ratio[amux_prescaling].den; |
| 711 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 712 | scale_type = vadc->adc->adc_channels[dt_index].adc_scale_fn; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 713 | if (scale_type >= SCALE_NONE) { |
| 714 | rc = -EBADF; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 715 | goto fail_unlock; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | vadc_scale_fn[scale_type].chan(result->adc_code, |
| 719 | vadc->adc->adc_prop, vadc->adc->amux_prop->chan_prop, result); |
| 720 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 721 | fail_unlock: |
| 722 | mutex_unlock(&vadc->adc->adc_lock); |
| 723 | |
| 724 | return rc; |
| 725 | } |
| 726 | EXPORT_SYMBOL(qpnp_vadc_conv_seq_request); |
| 727 | |
| 728 | int32_t qpnp_vadc_read(enum qpnp_vadc_channels channel, |
| 729 | struct qpnp_vadc_result *result) |
| 730 | { |
| 731 | return qpnp_vadc_conv_seq_request(ADC_SEQ_NONE, |
| 732 | channel, result); |
| 733 | } |
| 734 | EXPORT_SYMBOL_GPL(qpnp_vadc_read); |
| 735 | |
| 736 | static ssize_t qpnp_adc_show(struct device *dev, |
| 737 | struct device_attribute *devattr, char *buf) |
| 738 | { |
| 739 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 740 | struct qpnp_vadc_result result; |
| 741 | int rc = -1; |
| 742 | |
| 743 | rc = qpnp_vadc_read(attr->index, &result); |
| 744 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 745 | if (rc) { |
| 746 | pr_err("VADC read error with %d\n", rc); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 747 | return 0; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 748 | } |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 749 | |
| 750 | return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH, |
| 751 | "Result:%lld Raw:%d\n", result.physical, result.adc_code); |
| 752 | } |
| 753 | |
| 754 | static struct sensor_device_attribute qpnp_adc_attr = |
| 755 | SENSOR_ATTR(NULL, S_IRUGO, qpnp_adc_show, NULL, 0); |
| 756 | |
| 757 | static int32_t qpnp_vadc_init_hwmon(struct spmi_device *spmi) |
| 758 | { |
| 759 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
| 760 | struct device_node *child; |
| 761 | struct device_node *node = spmi->dev.of_node; |
| 762 | int rc = 0, i = 0, channel; |
| 763 | |
| 764 | for_each_child_of_node(node, child) { |
| 765 | channel = vadc->adc->adc_channels[i].channel_num; |
| 766 | qpnp_adc_attr.index = vadc->adc->adc_channels[i].channel_num; |
| 767 | qpnp_adc_attr.dev_attr.attr.name = |
| 768 | vadc->adc->adc_channels[i].name; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 769 | memcpy(&vadc->sens_attr[i], &qpnp_adc_attr, |
| 770 | sizeof(qpnp_adc_attr)); |
Stephen Boyd | d733796 | 2012-10-30 11:10:46 -0700 | [diff] [blame] | 771 | sysfs_attr_init(&vadc->sens_attr[i].dev_attr.attr); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 772 | rc = device_create_file(&spmi->dev, |
| 773 | &vadc->sens_attr[i].dev_attr); |
| 774 | if (rc) { |
| 775 | dev_err(&spmi->dev, |
| 776 | "device_create_file failed for dev %s\n", |
| 777 | vadc->adc->adc_channels[i].name); |
| 778 | goto hwmon_err_sens; |
| 779 | } |
| 780 | i++; |
| 781 | } |
| 782 | |
| 783 | return 0; |
| 784 | hwmon_err_sens: |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 785 | pr_err("Init HWMON failed for qpnp_adc with %d\n", rc); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 786 | return rc; |
| 787 | } |
| 788 | |
| 789 | static int __devinit qpnp_vadc_probe(struct spmi_device *spmi) |
| 790 | { |
| 791 | struct qpnp_vadc_drv *vadc; |
| 792 | struct qpnp_adc_drv *adc_qpnp; |
| 793 | struct device_node *node = spmi->dev.of_node; |
| 794 | struct device_node *child; |
| 795 | int rc, count_adc_channel_list = 0; |
| 796 | |
| 797 | if (!node) |
| 798 | return -EINVAL; |
| 799 | |
| 800 | if (qpnp_vadc) { |
| 801 | pr_err("VADC already in use\n"); |
| 802 | return -EBUSY; |
| 803 | } |
| 804 | |
| 805 | for_each_child_of_node(node, child) |
| 806 | count_adc_channel_list++; |
| 807 | |
| 808 | if (!count_adc_channel_list) { |
| 809 | pr_err("No channel listing\n"); |
| 810 | return -EINVAL; |
| 811 | } |
| 812 | |
| 813 | vadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_vadc_drv) + |
| 814 | (sizeof(struct sensor_device_attribute) * |
| 815 | count_adc_channel_list), GFP_KERNEL); |
| 816 | if (!vadc) { |
| 817 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 818 | return -ENOMEM; |
| 819 | } |
| 820 | |
| 821 | adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv), |
| 822 | GFP_KERNEL); |
| 823 | if (!adc_qpnp) { |
| 824 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame^] | 825 | rc = -ENOMEM; |
| 826 | goto fail; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | vadc->adc = adc_qpnp; |
| 830 | |
| 831 | rc = qpnp_adc_get_devicetree_data(spmi, vadc->adc); |
| 832 | if (rc) { |
| 833 | dev_err(&spmi->dev, "failed to read device tree\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame^] | 834 | goto fail; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame] | 837 | rc = devm_request_irq(&spmi->dev, vadc->adc->adc_irq_eoc, |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 838 | qpnp_vadc_isr, IRQF_TRIGGER_RISING, |
| 839 | "qpnp_vadc_interrupt", vadc); |
| 840 | if (rc) { |
| 841 | dev_err(&spmi->dev, |
| 842 | "failed to request adc irq with error %d\n", rc); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame^] | 843 | goto fail; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 844 | } else { |
Siddartha Mohanadoss | 1210995 | 2012-11-20 14:57:51 -0800 | [diff] [blame] | 845 | enable_irq_wake(vadc->adc->adc_irq_eoc); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | qpnp_vadc = vadc; |
| 849 | dev_set_drvdata(&spmi->dev, vadc); |
| 850 | rc = qpnp_vadc_init_hwmon(spmi); |
| 851 | if (rc) { |
| 852 | dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n"); |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame^] | 853 | goto fail; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 854 | } |
| 855 | vadc->vadc_hwmon = hwmon_device_register(&vadc->adc->spmi->dev); |
| 856 | vadc->vadc_init_calib = false; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 857 | vadc->max_channels_available = count_adc_channel_list; |
Siddartha Mohanadoss | 0ee2812 | 2012-11-01 10:35:01 -0700 | [diff] [blame] | 858 | vadc->vadc_initialized = true; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 859 | |
| 860 | return 0; |
Siddartha Mohanadoss | b60f646 | 2012-11-20 18:06:51 -0800 | [diff] [blame^] | 861 | fail: |
| 862 | qpnp_vadc = NULL; |
| 863 | return rc; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static int __devexit qpnp_vadc_remove(struct spmi_device *spmi) |
| 867 | { |
| 868 | struct qpnp_vadc_drv *vadc = dev_get_drvdata(&spmi->dev); |
| 869 | struct device_node *node = spmi->dev.of_node; |
| 870 | struct device_node *child; |
| 871 | int i = 0; |
| 872 | |
| 873 | for_each_child_of_node(node, child) { |
| 874 | device_remove_file(&spmi->dev, |
| 875 | &vadc->sens_attr[i].dev_attr); |
| 876 | i++; |
| 877 | } |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 878 | vadc->vadc_initialized = false; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 879 | dev_set_drvdata(&spmi->dev, NULL); |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static const struct of_device_id qpnp_vadc_match_table[] = { |
| 885 | { .compatible = "qcom,qpnp-vadc", |
| 886 | }, |
| 887 | {} |
| 888 | }; |
| 889 | |
| 890 | static struct spmi_driver qpnp_vadc_driver = { |
| 891 | .driver = { |
| 892 | .name = "qcom,qpnp-vadc", |
| 893 | .of_match_table = qpnp_vadc_match_table, |
| 894 | }, |
| 895 | .probe = qpnp_vadc_probe, |
| 896 | .remove = qpnp_vadc_remove, |
| 897 | }; |
| 898 | |
| 899 | static int __init qpnp_vadc_init(void) |
| 900 | { |
| 901 | return spmi_driver_register(&qpnp_vadc_driver); |
| 902 | } |
| 903 | module_init(qpnp_vadc_init); |
| 904 | |
| 905 | static void __exit qpnp_vadc_exit(void) |
| 906 | { |
| 907 | spmi_driver_unregister(&qpnp_vadc_driver); |
| 908 | } |
| 909 | module_exit(qpnp_vadc_exit); |
| 910 | |
| 911 | MODULE_DESCRIPTION("QPNP PMIC Voltage ADC driver"); |
| 912 | MODULE_LICENSE("GPL v2"); |