Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 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 | |
| 173 | int32_t qpnp_vadc_configure( |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 174 | struct qpnp_adc_amux_properties *chan_prop) |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 175 | { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 176 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 177 | u8 decimation = 0, conv_sequence = 0, conv_sequence_trig = 0; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 178 | u8 mode_ctrl = 0; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 179 | int rc = 0; |
| 180 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 181 | /* Mode selection */ |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 182 | mode_ctrl = chan_prop->mode_sel << QPNP_VADC_OP_MODE_SHIFT; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 183 | rc = qpnp_vadc_write_reg(QPNP_VADC_MODE_CTL, mode_ctrl); |
| 184 | if (rc < 0) { |
| 185 | pr_err("Mode configure write error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 186 | return rc; |
| 187 | } |
| 188 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 189 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 190 | /* Channel selection */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 191 | rc = qpnp_vadc_write_reg(QPNP_VADC_ADC_CH_SEL_CTL, |
| 192 | chan_prop->amux_channel); |
| 193 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 194 | pr_err("Channel configure error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 195 | return rc; |
| 196 | } |
| 197 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 198 | /* Digital parameter setup */ |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 199 | decimation = chan_prop->decimation << |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 200 | QPNP_VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT; |
| 201 | rc = qpnp_vadc_write_reg(QPNP_VADC_ADC_DIG_PARAM, decimation); |
| 202 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 203 | pr_err("Digital parameter configure write error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 204 | return rc; |
| 205 | } |
| 206 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 207 | /* HW settling time delay */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 208 | rc = qpnp_vadc_write_reg(QPNP_VADC_HW_SETTLE_DELAY, |
| 209 | chan_prop->hw_settle_time); |
| 210 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 211 | pr_err("HW settling time setup error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 212 | return rc; |
| 213 | } |
| 214 | |
| 215 | if (chan_prop->mode_sel == (ADC_OP_NORMAL_MODE << |
| 216 | QPNP_VADC_OP_MODE_SHIFT)) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 217 | /* Normal measurement mode */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 218 | rc = qpnp_vadc_write_reg(QPNP_VADC_FAST_AVG_CTL, |
| 219 | chan_prop->fast_avg_setup); |
| 220 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 221 | pr_err("Fast averaging configure error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 222 | return rc; |
| 223 | } |
| 224 | } else if (chan_prop->mode_sel == (ADC_OP_CONVERSION_SEQUENCER << |
| 225 | QPNP_VADC_OP_MODE_SHIFT)) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 226 | /* Conversion sequence mode */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 227 | conv_sequence = ((ADC_SEQ_HOLD_100US << |
| 228 | QPNP_VADC_CONV_SEQ_HOLDOFF_SHIFT) | |
| 229 | ADC_CONV_SEQ_TIMEOUT_5MS); |
| 230 | rc = qpnp_vadc_write_reg(QPNP_VADC_CONV_SEQ_CTL, |
| 231 | conv_sequence); |
| 232 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 233 | pr_err("Conversion sequence error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 234 | return rc; |
| 235 | } |
| 236 | |
| 237 | conv_sequence_trig = ((QPNP_VADC_CONV_SEQ_RISING_EDGE << |
| 238 | QPNP_VADC_CONV_SEQ_EDGE_SHIFT) | |
| 239 | chan_prop->trigger_channel); |
| 240 | rc = qpnp_vadc_write_reg(QPNP_VADC_CONV_SEQ_TRIG_CTL, |
| 241 | conv_sequence_trig); |
| 242 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 243 | pr_err("Conversion trigger error\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 244 | return rc; |
| 245 | } |
| 246 | } |
| 247 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 248 | INIT_COMPLETION(vadc->adc->adc_rslt_completion); |
| 249 | |
| 250 | rc = qpnp_vadc_enable(true); |
| 251 | if (rc) |
| 252 | return rc; |
| 253 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 254 | /* Request conversion */ |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 255 | rc = qpnp_vadc_write_reg(QPNP_VADC_CONV_REQ, QPNP_VADC_CONV_REQ_SET); |
| 256 | if (rc < 0) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 257 | pr_err("Request conversion failed\n"); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 258 | return rc; |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | EXPORT_SYMBOL(qpnp_vadc_configure); |
| 264 | |
| 265 | static int32_t qpnp_vadc_read_conversion_result(int32_t *data) |
| 266 | { |
| 267 | uint8_t rslt_lsb, rslt_msb; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 268 | int rc = 0, status = 0; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 269 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 270 | status = qpnp_vadc_read_reg(QPNP_VADC_DATA0, &rslt_lsb); |
| 271 | if (status < 0) { |
| 272 | pr_err("qpnp adc result read failed for data0\n"); |
| 273 | goto fail; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 276 | status = qpnp_vadc_read_reg(QPNP_VADC_DATA1, &rslt_msb); |
| 277 | if (status < 0) { |
| 278 | pr_err("qpnp adc result read failed for data1\n"); |
| 279 | goto fail; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | *data = (rslt_msb << 8) | rslt_lsb; |
| 283 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 284 | status = qpnp_vadc_check_result(data); |
| 285 | if (status < 0) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 286 | pr_err("VADC data check failed\n"); |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 287 | goto fail; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 290 | fail: |
| 291 | rc = qpnp_vadc_enable(false); |
| 292 | if (rc) |
| 293 | return rc; |
| 294 | |
| 295 | return status; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static int32_t qpnp_vadc_read_status(int mode_sel) |
| 299 | { |
| 300 | u8 status1, status2, status2_conv_seq_state; |
| 301 | u8 status_err = QPNP_VADC_CONV_TIMEOUT_ERR; |
| 302 | int rc; |
| 303 | |
| 304 | switch (mode_sel) { |
| 305 | case (ADC_OP_CONVERSION_SEQUENCER << QPNP_VADC_OP_MODE_SHIFT): |
| 306 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 307 | if (rc) { |
| 308 | pr_err("qpnp_vadc read mask interrupt failed\n"); |
| 309 | return rc; |
| 310 | } |
| 311 | |
| 312 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS2, &status2); |
| 313 | if (rc) { |
| 314 | pr_err("qpnp_vadc read mask interrupt failed\n"); |
| 315 | return rc; |
| 316 | } |
| 317 | |
| 318 | if (!(status2 & ~QPNP_VADC_STATUS2_CONV_SEQ_TIMEOUT_STS) && |
| 319 | (status1 & (~QPNP_VADC_STATUS1_REQ_STS | |
| 320 | QPNP_VADC_STATUS1_EOC))) { |
| 321 | rc = status_err; |
| 322 | return rc; |
| 323 | } |
| 324 | |
| 325 | status2_conv_seq_state = status2 >> |
| 326 | QPNP_VADC_STATUS2_CONV_SEQ_STATE_SHIFT; |
| 327 | if (status2_conv_seq_state != ADC_CONV_SEQ_IDLE) { |
| 328 | pr_err("qpnp vadc seq error with status %d\n", |
| 329 | status2); |
| 330 | rc = -EINVAL; |
| 331 | return rc; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static void qpnp_vadc_work(struct work_struct *work) |
| 339 | { |
| 340 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 341 | |
| 342 | complete(&vadc->adc->adc_rslt_completion); |
| 343 | |
| 344 | return; |
| 345 | } |
| 346 | DECLARE_WORK(trigger_completion_work, qpnp_vadc_work); |
| 347 | |
| 348 | static irqreturn_t qpnp_vadc_isr(int irq, void *dev_id) |
| 349 | { |
| 350 | schedule_work(&trigger_completion_work); |
| 351 | |
| 352 | return IRQ_HANDLED; |
| 353 | } |
| 354 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 355 | static int32_t qpnp_vadc_version_check(void) |
| 356 | { |
| 357 | uint8_t revision; |
| 358 | int rc; |
| 359 | |
| 360 | rc = qpnp_vadc_read_reg(QPNP_VADC_REVISION2, &revision); |
| 361 | if (rc < 0) { |
| 362 | pr_err("qpnp adc result read failed with %d\n", rc); |
| 363 | return rc; |
| 364 | } |
| 365 | |
| 366 | if (revision < QPNP_VADC_SUPPORTED_REVISION2) { |
| 367 | pr_err("VADC Version not supported\n"); |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 374 | static uint32_t qpnp_vadc_calib_device(void) |
| 375 | { |
| 376 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
Siddartha Mohanadoss | c4a6af1 | 2012-07-13 18:50:12 -0700 | [diff] [blame] | 377 | struct qpnp_adc_amux_properties conv; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 378 | int rc, calib_read_1, calib_read_2; |
| 379 | u8 status1 = 0; |
| 380 | |
| 381 | conv.amux_channel = REF_125V; |
| 382 | conv.decimation = DECIMATION_TYPE2; |
| 383 | conv.mode_sel = ADC_OP_NORMAL_MODE << QPNP_VADC_OP_MODE_SHIFT; |
| 384 | conv.hw_settle_time = ADC_CHANNEL_HW_SETTLE_DELAY_0US; |
| 385 | conv.fast_avg_setup = ADC_FAST_AVG_SAMPLE_1; |
| 386 | |
| 387 | rc = qpnp_vadc_configure(&conv); |
| 388 | if (rc) { |
| 389 | pr_err("qpnp_vadc configure failed with %d\n", rc); |
| 390 | goto calib_fail; |
| 391 | } |
| 392 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 393 | while (status1 != QPNP_VADC_STATUS1_EOC) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 394 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 395 | if (rc < 0) |
| 396 | return rc; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 397 | status1 &= QPNP_VADC_STATUS1_REQ_STS_EOC_MASK; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 398 | usleep_range(QPNP_VADC_CONV_TIME_MIN, |
| 399 | QPNP_VADC_CONV_TIME_MAX); |
| 400 | } |
| 401 | |
| 402 | rc = qpnp_vadc_read_conversion_result(&calib_read_1); |
| 403 | if (rc) { |
| 404 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 405 | goto calib_fail; |
| 406 | } |
| 407 | |
| 408 | conv.amux_channel = REF_625MV; |
| 409 | conv.decimation = DECIMATION_TYPE2; |
| 410 | conv.mode_sel = ADC_OP_NORMAL_MODE << QPNP_VADC_OP_MODE_SHIFT; |
| 411 | conv.hw_settle_time = ADC_CHANNEL_HW_SETTLE_DELAY_0US; |
| 412 | conv.fast_avg_setup = ADC_FAST_AVG_SAMPLE_1; |
| 413 | rc = qpnp_vadc_configure(&conv); |
| 414 | if (rc) { |
| 415 | pr_err("qpnp adc configure failed with %d\n", rc); |
| 416 | goto calib_fail; |
| 417 | } |
| 418 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 419 | status1 = 0; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 420 | while (status1 != QPNP_VADC_STATUS1_EOC) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 421 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 422 | if (rc < 0) |
| 423 | return rc; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 424 | status1 &= QPNP_VADC_STATUS1_REQ_STS_EOC_MASK; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 425 | usleep_range(QPNP_VADC_CONV_TIME_MIN, |
| 426 | QPNP_VADC_CONV_TIME_MAX); |
| 427 | } |
| 428 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 429 | rc = qpnp_vadc_read_conversion_result(&calib_read_2); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 430 | if (rc) { |
| 431 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 432 | goto calib_fail; |
| 433 | } |
| 434 | |
| 435 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].dy = |
| 436 | (calib_read_1 - calib_read_2); |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 437 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 438 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].dx |
| 439 | = QPNP_ADC_625_UV; |
| 440 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].adc_vref = |
| 441 | calib_read_1; |
| 442 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_ABSOLUTE].adc_gnd = |
| 443 | calib_read_2; |
| 444 | /* Ratiometric Calibration */ |
| 445 | conv.amux_channel = VDD_VADC; |
| 446 | conv.decimation = DECIMATION_TYPE2; |
| 447 | conv.mode_sel = ADC_OP_NORMAL_MODE << QPNP_VADC_OP_MODE_SHIFT; |
| 448 | conv.hw_settle_time = ADC_CHANNEL_HW_SETTLE_DELAY_0US; |
| 449 | conv.fast_avg_setup = ADC_FAST_AVG_SAMPLE_1; |
| 450 | rc = qpnp_vadc_configure(&conv); |
| 451 | if (rc) { |
| 452 | pr_err("qpnp adc configure failed with %d\n", rc); |
| 453 | goto calib_fail; |
| 454 | } |
| 455 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 456 | status1 = 0; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 457 | while (status1 != QPNP_VADC_STATUS1_EOC) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 458 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 459 | if (rc < 0) |
| 460 | return rc; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 461 | status1 &= QPNP_VADC_STATUS1_REQ_STS_EOC_MASK; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 462 | usleep_range(QPNP_VADC_CONV_TIME_MIN, |
| 463 | QPNP_VADC_CONV_TIME_MAX); |
| 464 | } |
| 465 | |
| 466 | rc = qpnp_vadc_read_conversion_result(&calib_read_1); |
| 467 | if (rc) { |
| 468 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 469 | goto calib_fail; |
| 470 | } |
| 471 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 472 | conv.amux_channel = GND_REF; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 473 | conv.decimation = DECIMATION_TYPE2; |
| 474 | conv.mode_sel = ADC_OP_NORMAL_MODE << QPNP_VADC_OP_MODE_SHIFT; |
| 475 | conv.hw_settle_time = ADC_CHANNEL_HW_SETTLE_DELAY_0US; |
| 476 | conv.fast_avg_setup = ADC_FAST_AVG_SAMPLE_1; |
| 477 | rc = qpnp_vadc_configure(&conv); |
| 478 | if (rc) { |
| 479 | pr_err("qpnp adc configure failed with %d\n", rc); |
| 480 | goto calib_fail; |
| 481 | } |
| 482 | |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 483 | status1 = 0; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 484 | while (status1 != QPNP_VADC_STATUS1_EOC) { |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 485 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 486 | if (rc < 0) |
| 487 | return rc; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 488 | status1 &= QPNP_VADC_STATUS1_REQ_STS_EOC_MASK; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 489 | usleep_range(QPNP_VADC_CONV_TIME_MIN, |
| 490 | QPNP_VADC_CONV_TIME_MAX); |
| 491 | } |
| 492 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 493 | rc = qpnp_vadc_read_conversion_result(&calib_read_2); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 494 | if (rc) { |
| 495 | pr_err("qpnp adc read adc failed with %d\n", rc); |
| 496 | goto calib_fail; |
| 497 | } |
| 498 | |
| 499 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].dy = |
| 500 | (calib_read_1 - calib_read_2); |
| 501 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].dx = |
| 502 | vadc->adc->adc_prop->adc_vdd_reference; |
| 503 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].adc_vref = |
| 504 | calib_read_1; |
| 505 | vadc->adc->amux_prop->chan_prop->adc_graph[CALIB_RATIOMETRIC].adc_gnd = |
| 506 | calib_read_2; |
| 507 | |
| 508 | calib_fail: |
| 509 | return rc; |
| 510 | } |
| 511 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 512 | int32_t qpnp_vadc_is_ready(void) |
| 513 | { |
| 514 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
| 515 | |
| 516 | if (!vadc || !vadc->vadc_initialized) |
| 517 | return -EPROBE_DEFER; |
| 518 | else |
| 519 | return 0; |
| 520 | } |
| 521 | EXPORT_SYMBOL(qpnp_vadc_is_ready); |
| 522 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 523 | int32_t qpnp_vadc_conv_seq_request(enum qpnp_vadc_trigger trigger_channel, |
| 524 | enum qpnp_vadc_channels channel, |
| 525 | struct qpnp_vadc_result *result) |
| 526 | { |
| 527 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 528 | int rc = 0, scale_type, amux_prescaling, dt_index = 0; |
| 529 | |
| 530 | if (!vadc || !vadc->vadc_initialized) |
| 531 | return -EPROBE_DEFER; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 532 | |
| 533 | if (!vadc->vadc_init_calib) { |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 534 | rc = qpnp_vadc_version_check(); |
| 535 | if (rc) |
| 536 | return rc; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 537 | rc = qpnp_vadc_calib_device(); |
| 538 | if (rc) { |
| 539 | pr_err("Calibration failed\n"); |
| 540 | return rc; |
| 541 | } else |
| 542 | vadc->vadc_init_calib = true; |
| 543 | } |
| 544 | |
| 545 | mutex_lock(&vadc->adc->adc_lock); |
| 546 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 547 | vadc->adc->amux_prop->amux_channel = channel; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 548 | |
| 549 | while (vadc->adc->adc_channels[dt_index].channel_num |
| 550 | != channel || dt_index > vadc->max_channels_available) |
| 551 | dt_index++; |
| 552 | |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame^] | 553 | if (dt_index > vadc->max_channels_available) { |
| 554 | pr_err("not a valid VADC channel\n"); |
| 555 | rc = -EINVAL; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 556 | goto fail_unlock; |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame^] | 557 | } |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 558 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 559 | vadc->adc->amux_prop->decimation = |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 560 | vadc->adc->adc_channels[dt_index].adc_decimation; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 561 | vadc->adc->amux_prop->hw_settle_time = |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 562 | vadc->adc->adc_channels[dt_index].hw_settle_time; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 563 | vadc->adc->amux_prop->fast_avg_setup = |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 564 | vadc->adc->adc_channels[dt_index].fast_avg_setup; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 565 | |
| 566 | if (trigger_channel < ADC_SEQ_NONE) |
| 567 | vadc->adc->amux_prop->mode_sel = (ADC_OP_CONVERSION_SEQUENCER |
| 568 | << QPNP_VADC_OP_MODE_SHIFT); |
| 569 | else if (trigger_channel == ADC_SEQ_NONE) |
| 570 | vadc->adc->amux_prop->mode_sel = (ADC_OP_NORMAL_MODE |
| 571 | << QPNP_VADC_OP_MODE_SHIFT); |
| 572 | else { |
| 573 | pr_err("Invalid trigger channel:%d\n", trigger_channel); |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 574 | goto fail_unlock; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | vadc->adc->amux_prop->trigger_channel = trigger_channel; |
| 578 | |
| 579 | rc = qpnp_vadc_configure(vadc->adc->amux_prop); |
| 580 | if (rc) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 581 | pr_err("qpnp vadc configure failed with %d\n", rc); |
| 582 | goto fail_unlock; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Siddartha Mohanadoss | 1a0d203 | 2012-11-01 11:22:29 -0700 | [diff] [blame^] | 585 | rc = wait_for_completion_timeout(&vadc->adc->adc_rslt_completion, |
| 586 | QPNP_ADC_COMPLETION_TIMEOUT); |
| 587 | if (!rc) { |
| 588 | u8 status1 = 0; |
| 589 | rc = qpnp_vadc_read_reg(QPNP_VADC_STATUS1, &status1); |
| 590 | if (rc < 0) |
| 591 | goto fail_unlock; |
| 592 | status1 &= (QPNP_VADC_STATUS1_REQ_STS | QPNP_VADC_STATUS1_EOC); |
| 593 | if (status1 == QPNP_VADC_STATUS1_EOC) |
| 594 | pr_debug("End of conversion status set\n"); |
| 595 | else { |
| 596 | pr_err("EOC interrupt not received\n"); |
| 597 | rc = -EINVAL; |
| 598 | goto fail_unlock; |
| 599 | } |
| 600 | } |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 601 | |
| 602 | if (trigger_channel < ADC_SEQ_NONE) { |
| 603 | rc = qpnp_vadc_read_status(vadc->adc->amux_prop->mode_sel); |
| 604 | if (rc) |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 605 | pr_debug("Conversion sequence timed out - %d\n", rc); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | rc = qpnp_vadc_read_conversion_result(&result->adc_code); |
| 609 | if (rc) { |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 610 | pr_err("qpnp vadc read adc code failed with %d\n", rc); |
| 611 | goto fail_unlock; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 614 | amux_prescaling = |
| 615 | vadc->adc->adc_channels[dt_index].chan_path_prescaling; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 616 | |
| 617 | vadc->adc->amux_prop->chan_prop->offset_gain_numerator = |
| 618 | qpnp_vadc_amux_scaling_ratio[amux_prescaling].num; |
| 619 | vadc->adc->amux_prop->chan_prop->offset_gain_denominator = |
| 620 | qpnp_vadc_amux_scaling_ratio[amux_prescaling].den; |
| 621 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 622 | scale_type = vadc->adc->adc_channels[dt_index].adc_scale_fn; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 623 | if (scale_type >= SCALE_NONE) { |
| 624 | rc = -EBADF; |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 625 | goto fail_unlock; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | vadc_scale_fn[scale_type].chan(result->adc_code, |
| 629 | vadc->adc->adc_prop, vadc->adc->amux_prop->chan_prop, result); |
| 630 | |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 631 | fail_unlock: |
| 632 | mutex_unlock(&vadc->adc->adc_lock); |
| 633 | |
| 634 | return rc; |
| 635 | } |
| 636 | EXPORT_SYMBOL(qpnp_vadc_conv_seq_request); |
| 637 | |
| 638 | int32_t qpnp_vadc_read(enum qpnp_vadc_channels channel, |
| 639 | struct qpnp_vadc_result *result) |
| 640 | { |
| 641 | return qpnp_vadc_conv_seq_request(ADC_SEQ_NONE, |
| 642 | channel, result); |
| 643 | } |
| 644 | EXPORT_SYMBOL_GPL(qpnp_vadc_read); |
| 645 | |
| 646 | static ssize_t qpnp_adc_show(struct device *dev, |
| 647 | struct device_attribute *devattr, char *buf) |
| 648 | { |
| 649 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 650 | struct qpnp_vadc_result result; |
| 651 | int rc = -1; |
| 652 | |
| 653 | rc = qpnp_vadc_read(attr->index, &result); |
| 654 | |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 655 | if (rc) { |
| 656 | pr_err("VADC read error with %d\n", rc); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 657 | return 0; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 658 | } |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 659 | |
| 660 | return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH, |
| 661 | "Result:%lld Raw:%d\n", result.physical, result.adc_code); |
| 662 | } |
| 663 | |
| 664 | static struct sensor_device_attribute qpnp_adc_attr = |
| 665 | SENSOR_ATTR(NULL, S_IRUGO, qpnp_adc_show, NULL, 0); |
| 666 | |
| 667 | static int32_t qpnp_vadc_init_hwmon(struct spmi_device *spmi) |
| 668 | { |
| 669 | struct qpnp_vadc_drv *vadc = qpnp_vadc; |
| 670 | struct device_node *child; |
| 671 | struct device_node *node = spmi->dev.of_node; |
| 672 | int rc = 0, i = 0, channel; |
| 673 | |
| 674 | for_each_child_of_node(node, child) { |
| 675 | channel = vadc->adc->adc_channels[i].channel_num; |
| 676 | qpnp_adc_attr.index = vadc->adc->adc_channels[i].channel_num; |
| 677 | qpnp_adc_attr.dev_attr.attr.name = |
| 678 | vadc->adc->adc_channels[i].name; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 679 | memcpy(&vadc->sens_attr[i], &qpnp_adc_attr, |
| 680 | sizeof(qpnp_adc_attr)); |
Stephen Boyd | d733796 | 2012-10-30 11:10:46 -0700 | [diff] [blame] | 681 | sysfs_attr_init(&vadc->sens_attr[i].dev_attr.attr); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 682 | rc = device_create_file(&spmi->dev, |
| 683 | &vadc->sens_attr[i].dev_attr); |
| 684 | if (rc) { |
| 685 | dev_err(&spmi->dev, |
| 686 | "device_create_file failed for dev %s\n", |
| 687 | vadc->adc->adc_channels[i].name); |
| 688 | goto hwmon_err_sens; |
| 689 | } |
| 690 | i++; |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | hwmon_err_sens: |
Siddartha Mohanadoss | ae1da73 | 2012-08-08 16:39:02 -0700 | [diff] [blame] | 695 | pr_err("Init HWMON failed for qpnp_adc with %d\n", rc); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 696 | return rc; |
| 697 | } |
| 698 | |
| 699 | static int __devinit qpnp_vadc_probe(struct spmi_device *spmi) |
| 700 | { |
| 701 | struct qpnp_vadc_drv *vadc; |
| 702 | struct qpnp_adc_drv *adc_qpnp; |
| 703 | struct device_node *node = spmi->dev.of_node; |
| 704 | struct device_node *child; |
| 705 | int rc, count_adc_channel_list = 0; |
| 706 | |
| 707 | if (!node) |
| 708 | return -EINVAL; |
| 709 | |
| 710 | if (qpnp_vadc) { |
| 711 | pr_err("VADC already in use\n"); |
| 712 | return -EBUSY; |
| 713 | } |
| 714 | |
| 715 | for_each_child_of_node(node, child) |
| 716 | count_adc_channel_list++; |
| 717 | |
| 718 | if (!count_adc_channel_list) { |
| 719 | pr_err("No channel listing\n"); |
| 720 | return -EINVAL; |
| 721 | } |
| 722 | |
| 723 | vadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_vadc_drv) + |
| 724 | (sizeof(struct sensor_device_attribute) * |
| 725 | count_adc_channel_list), GFP_KERNEL); |
| 726 | if (!vadc) { |
| 727 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 728 | return -ENOMEM; |
| 729 | } |
| 730 | |
| 731 | adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv), |
| 732 | GFP_KERNEL); |
| 733 | if (!adc_qpnp) { |
| 734 | dev_err(&spmi->dev, "Unable to allocate memory\n"); |
| 735 | return -ENOMEM; |
| 736 | } |
| 737 | |
| 738 | vadc->adc = adc_qpnp; |
| 739 | |
| 740 | rc = qpnp_adc_get_devicetree_data(spmi, vadc->adc); |
| 741 | if (rc) { |
| 742 | dev_err(&spmi->dev, "failed to read device tree\n"); |
| 743 | return rc; |
| 744 | } |
| 745 | |
| 746 | rc = devm_request_irq(&spmi->dev, vadc->adc->adc_irq, |
| 747 | qpnp_vadc_isr, IRQF_TRIGGER_RISING, |
| 748 | "qpnp_vadc_interrupt", vadc); |
| 749 | if (rc) { |
| 750 | dev_err(&spmi->dev, |
| 751 | "failed to request adc irq with error %d\n", rc); |
| 752 | return rc; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 753 | } else { |
| 754 | enable_irq_wake(vadc->adc->adc_irq); |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | qpnp_vadc = vadc; |
| 758 | dev_set_drvdata(&spmi->dev, vadc); |
| 759 | rc = qpnp_vadc_init_hwmon(spmi); |
| 760 | if (rc) { |
| 761 | dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n"); |
Siddartha Mohanadoss | 0ee2812 | 2012-11-01 10:35:01 -0700 | [diff] [blame] | 762 | return rc; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 763 | } |
| 764 | vadc->vadc_hwmon = hwmon_device_register(&vadc->adc->spmi->dev); |
| 765 | vadc->vadc_init_calib = false; |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 766 | vadc->max_channels_available = count_adc_channel_list; |
Siddartha Mohanadoss | 0ee2812 | 2012-11-01 10:35:01 -0700 | [diff] [blame] | 767 | vadc->vadc_initialized = true; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 768 | |
| 769 | return 0; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | static int __devexit qpnp_vadc_remove(struct spmi_device *spmi) |
| 773 | { |
| 774 | struct qpnp_vadc_drv *vadc = dev_get_drvdata(&spmi->dev); |
| 775 | struct device_node *node = spmi->dev.of_node; |
| 776 | struct device_node *child; |
| 777 | int i = 0; |
| 778 | |
| 779 | for_each_child_of_node(node, child) { |
| 780 | device_remove_file(&spmi->dev, |
| 781 | &vadc->sens_attr[i].dev_attr); |
| 782 | i++; |
| 783 | } |
Siddartha Mohanadoss | 5ace110 | 2012-08-20 23:18:10 -0700 | [diff] [blame] | 784 | vadc->vadc_initialized = false; |
Siddartha Mohanadoss | 7b116e1 | 2012-06-05 23:27:46 -0700 | [diff] [blame] | 785 | dev_set_drvdata(&spmi->dev, NULL); |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static const struct of_device_id qpnp_vadc_match_table[] = { |
| 791 | { .compatible = "qcom,qpnp-vadc", |
| 792 | }, |
| 793 | {} |
| 794 | }; |
| 795 | |
| 796 | static struct spmi_driver qpnp_vadc_driver = { |
| 797 | .driver = { |
| 798 | .name = "qcom,qpnp-vadc", |
| 799 | .of_match_table = qpnp_vadc_match_table, |
| 800 | }, |
| 801 | .probe = qpnp_vadc_probe, |
| 802 | .remove = qpnp_vadc_remove, |
| 803 | }; |
| 804 | |
| 805 | static int __init qpnp_vadc_init(void) |
| 806 | { |
| 807 | return spmi_driver_register(&qpnp_vadc_driver); |
| 808 | } |
| 809 | module_init(qpnp_vadc_init); |
| 810 | |
| 811 | static void __exit qpnp_vadc_exit(void) |
| 812 | { |
| 813 | spmi_driver_unregister(&qpnp_vadc_driver); |
| 814 | } |
| 815 | module_exit(qpnp_vadc_exit); |
| 816 | |
| 817 | MODULE_DESCRIPTION("QPNP PMIC Voltage ADC driver"); |
| 818 | MODULE_LICENSE("GPL v2"); |