blob: f6ab3c21ebf6fcd0523edd6e986bd510ba7e5465 [file] [log] [blame]
Xiaozhe Shi767fdb62013-01-10 15:09:08 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/kernel.h>
16#include <linux/of.h>
17#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/delay.h>
21#include <linux/mutex.h>
22#include <linux/types.h>
23#include <linux/hwmon.h>
24#include <linux/module.h>
25#include <linux/debugfs.h>
26#include <linux/spmi.h>
27#include <linux/of_irq.h>
28#include <linux/wakelock.h>
29#include <linux/interrupt.h>
30#include <linux/completion.h>
31#include <linux/hwmon-sysfs.h>
32#include <linux/qpnp/qpnp-adc.h>
33#include <linux/platform_device.h>
34
35/* QPNP IADC register definition */
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -070036#define QPNP_IADC_REVISION1 0x0
37#define QPNP_IADC_REVISION2 0x1
38#define QPNP_IADC_REVISION3 0x2
39#define QPNP_IADC_REVISION4 0x3
40#define QPNP_IADC_PERPH_TYPE 0x4
41#define QPNP_IADC_PERH_SUBTYPE 0x5
42
43#define QPNP_IADC_SUPPORTED_REVISION2 1
44
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -070045#define QPNP_STATUS1 0x8
46#define QPNP_STATUS1_OP_MODE 4
47#define QPNP_STATUS1_MULTI_MEAS_EN BIT(3)
48#define QPNP_STATUS1_MEAS_INTERVAL_EN_STS BIT(2)
49#define QPNP_STATUS1_REQ_STS BIT(1)
50#define QPNP_STATUS1_EOC BIT(0)
51#define QPNP_STATUS2 0x9
52#define QPNP_STATUS2_CONV_SEQ_STATE_SHIFT 4
53#define QPNP_STATUS2_FIFO_NOT_EMPTY_FLAG BIT(1)
54#define QPNP_STATUS2_CONV_SEQ_TIMEOUT_STS BIT(0)
55#define QPNP_CONV_TIMEOUT_ERR 2
56
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -070057#define QPNP_IADC_MODE_CTL 0x40
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -070058#define QPNP_OP_MODE_SHIFT 4
59#define QPNP_USE_BMS_DATA BIT(4)
60#define QPNP_VADC_SYNCH_EN BIT(2)
61#define QPNP_OFFSET_RMV_EN BIT(1)
62#define QPNP_ADC_TRIM_EN BIT(0)
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -070063#define QPNP_IADC_EN_CTL1 0x46
64#define QPNP_IADC_ADC_EN BIT(7)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -070065#define QPNP_ADC_CH_SEL_CTL 0x48
66#define QPNP_ADC_DIG_PARAM 0x50
67#define QPNP_ADC_CLK_SEL_MASK 0x3
68#define QPNP_ADC_DEC_RATIO_SEL_MASK 0xc
69#define QPNP_ADC_DIG_DEC_RATIO_SEL_SHIFT 2
70
71#define QPNP_HW_SETTLE_DELAY 0x51
72#define QPNP_CONV_REQ 0x52
73#define QPNP_CONV_REQ_SET BIT(7)
74#define QPNP_CONV_SEQ_CTL 0x54
75#define QPNP_CONV_SEQ_HOLDOFF_SHIFT 4
76#define QPNP_CONV_SEQ_TRIG_CTL 0x55
77#define QPNP_FAST_AVG_CTL 0x5a
78
79#define QPNP_M0_LOW_THR_LSB 0x5c
80#define QPNP_M0_LOW_THR_MSB 0x5d
81#define QPNP_M0_HIGH_THR_LSB 0x5e
82#define QPNP_M0_HIGH_THR_MSB 0x5f
83#define QPNP_M1_LOW_THR_LSB 0x69
84#define QPNP_M1_LOW_THR_MSB 0x6a
85#define QPNP_M1_HIGH_THR_LSB 0x6b
86#define QPNP_M1_HIGH_THR_MSB 0x6c
87
88#define QPNP_DATA0 0x60
89#define QPNP_DATA1 0x61
90#define QPNP_CONV_TIMEOUT_ERR 2
91
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -070092#define QPNP_IADC_SEC_ACCESS 0xD0
93#define QPNP_IADC_SEC_ACCESS_DATA 0xA5
94#define QPNP_IADC_MSB_OFFSET 0xF2
95#define QPNP_IADC_LSB_OFFSET 0xF3
96#define QPNP_IADC_NOMINAL_RSENSE 0xF4
97#define QPNP_IADC_ATE_GAIN_CALIB_OFFSET 0xF5
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -070098#define QPNP_INT_TEST_VAL 0xE1
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -070099
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700100#define QPNP_IADC_ADC_CH_SEL_CTL 0x48
101#define QPNP_IADC_ADC_CHX_SEL_SHIFT 3
102
103#define QPNP_IADC_ADC_DIG_PARAM 0x50
104#define QPNP_IADC_CLK_SEL_SHIFT 1
105#define QPNP_IADC_DEC_RATIO_SEL 3
106
107#define QPNP_IADC_CONV_REQUEST 0x52
108#define QPNP_IADC_CONV_REQ BIT(7)
109
110#define QPNP_IADC_DATA0 0x60
111#define QPNP_IADC_DATA1 0x61
112
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700113#define QPNP_ADC_CONV_TIME_MIN 8000
114#define QPNP_ADC_CONV_TIME_MAX 8200
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700115
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700116#define QPNP_ADC_GAIN_NV 17857
117#define QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL 0
118#define QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR 10000000
Siddartha Mohanadoss22618be2013-04-02 15:02:19 -0700119#define QPNP_IADC_NANO_VOLTS_FACTOR 1000000
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700120#define QPNP_IADC_CALIB_SECONDS 300000
121#define QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT 15625
122#define QPNP_IADC_DIE_TEMP_CALIB_OFFSET 5000
123
124#define QPNP_RAW_CODE_16_BIT_MSB_MASK 0xff00
125#define QPNP_RAW_CODE_16_BIT_LSB_MASK 0xff
126#define QPNP_BIT_SHIFT_8 8
127#define QPNP_RSENSE_MSB_SIGN_CHECK 0x80
Siddartha Mohanadoss1a0d2032012-11-01 11:22:29 -0700128#define QPNP_ADC_COMPLETION_TIMEOUT HZ
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700129
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700130struct qpnp_iadc_comp {
131 bool ext_rsense;
132 u8 id;
133 u8 sys_gain;
134 u8 revision;
135};
136
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700137struct qpnp_iadc_drv {
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700138 struct qpnp_adc_drv *adc;
139 int32_t rsense;
Siddartha Mohanadosse70010b2013-04-04 14:51:41 -0700140 bool external_rsense;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700141 struct device *iadc_hwmon;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700142 bool iadc_initialized;
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700143 int64_t die_temp;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700144 struct delayed_work iadc_work;
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800145 struct mutex iadc_vadc_lock;
146 bool iadc_mode_sel;
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700147 struct qpnp_iadc_comp iadc_comp;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700148 struct sensor_device_attribute sens_attr[0];
149};
150
151struct qpnp_iadc_drv *qpnp_iadc;
152
153static int32_t qpnp_iadc_read_reg(uint32_t reg, u8 *data)
154{
155 struct qpnp_iadc_drv *iadc = qpnp_iadc;
156 int rc;
157
158 rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave,
Siddartha Mohanadossae1da732012-08-08 16:39:02 -0700159 (iadc->adc->offset + reg), data, 1);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700160 if (rc < 0) {
161 pr_err("qpnp iadc read reg %d failed with %d\n", reg, rc);
162 return rc;
163 }
164
165 return 0;
166}
167
168static int32_t qpnp_iadc_write_reg(uint32_t reg, u8 data)
169{
170 struct qpnp_iadc_drv *iadc = qpnp_iadc;
171 int rc;
172 u8 *buf;
173
174 buf = &data;
175 rc = spmi_ext_register_writel(iadc->adc->spmi->ctrl, iadc->adc->slave,
Siddartha Mohanadossae1da732012-08-08 16:39:02 -0700176 (iadc->adc->offset + reg), buf, 1);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700177 if (rc < 0) {
178 pr_err("qpnp iadc write reg %d failed with %d\n", reg, rc);
179 return rc;
180 }
181
182 return 0;
183}
184
Siddartha Mohanadosse2363592012-12-14 18:59:01 -0800185static void trigger_iadc_completion(struct work_struct *work)
186{
187 struct qpnp_iadc_drv *iadc = qpnp_iadc;
188
Siddartha Mohanadossb60f6462012-11-20 18:06:51 -0800189 if (!iadc || !iadc->iadc_initialized)
190 return;
191
Siddartha Mohanadosse2363592012-12-14 18:59:01 -0800192 complete(&iadc->adc->adc_rslt_completion);
193
194 return;
195}
196DECLARE_WORK(trigger_iadc_completion_work, trigger_iadc_completion);
197
198static irqreturn_t qpnp_iadc_isr(int irq, void *dev_id)
199{
200 schedule_work(&trigger_iadc_completion_work);
201
202 return IRQ_HANDLED;
203}
204
205static int32_t qpnp_iadc_enable(bool state)
206{
207 int rc = 0;
208 u8 data = 0;
209
210 data = QPNP_IADC_ADC_EN;
211 if (state) {
212 rc = qpnp_iadc_write_reg(QPNP_IADC_EN_CTL1,
213 data);
214 if (rc < 0) {
215 pr_err("IADC enable failed\n");
216 return rc;
217 }
218 } else {
219 rc = qpnp_iadc_write_reg(QPNP_IADC_EN_CTL1,
220 (~data & QPNP_IADC_ADC_EN));
221 if (rc < 0) {
222 pr_err("IADC disable failed\n");
223 return rc;
224 }
225 }
226
227 return 0;
228}
229
Siddartha Mohanadossd3a3c952012-12-10 16:55:19 -0800230static int32_t qpnp_iadc_status_debug(void)
231{
232 int rc = 0;
233 u8 mode = 0, status1 = 0, chan = 0, dig = 0, en = 0;
234
235 rc = qpnp_iadc_read_reg(QPNP_IADC_MODE_CTL, &mode);
236 if (rc < 0) {
237 pr_err("mode ctl register read failed with %d\n", rc);
238 return rc;
239 }
240
241 rc = qpnp_iadc_read_reg(QPNP_ADC_DIG_PARAM, &dig);
242 if (rc < 0) {
243 pr_err("digital param read failed with %d\n", rc);
244 return rc;
245 }
246
247 rc = qpnp_iadc_read_reg(QPNP_IADC_ADC_CH_SEL_CTL, &chan);
248 if (rc < 0) {
249 pr_err("channel read failed with %d\n", rc);
250 return rc;
251 }
252
253 rc = qpnp_iadc_read_reg(QPNP_STATUS1, &status1);
254 if (rc < 0) {
255 pr_err("status1 read failed with %d\n", rc);
256 return rc;
257 }
258
259 rc = qpnp_iadc_read_reg(QPNP_IADC_EN_CTL1, &en);
260 if (rc < 0) {
261 pr_err("en read failed with %d\n", rc);
262 return rc;
263 }
264
Siddartha Mohanadoss73ae69b2013-04-03 17:34:03 -0700265 pr_debug("EOC not set with status:%x, dig:%x, ch:%x, mode:%x, en:%x\n",
Siddartha Mohanadossd3a3c952012-12-10 16:55:19 -0800266 status1, dig, chan, mode, en);
267
Siddartha Mohanadosse2363592012-12-14 18:59:01 -0800268 rc = qpnp_iadc_enable(false);
269 if (rc < 0) {
270 pr_err("IADC disable failed with %d\n", rc);
271 return rc;
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700272 }
273
274 return 0;
275}
276
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700277static int32_t qpnp_iadc_read_conversion_result(uint16_t *data)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700278{
279 uint8_t rslt_lsb, rslt_msb;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700280 uint16_t rslt;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700281 int32_t rc;
282
283 rc = qpnp_iadc_read_reg(QPNP_IADC_DATA0, &rslt_lsb);
284 if (rc < 0) {
285 pr_err("qpnp adc result read failed with %d\n", rc);
286 return rc;
287 }
288
289 rc = qpnp_iadc_read_reg(QPNP_IADC_DATA1, &rslt_msb);
290 if (rc < 0) {
291 pr_err("qpnp adc result read failed with %d\n", rc);
292 return rc;
293 }
294
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700295 rslt = (rslt_msb << 8) | rslt_lsb;
296 *data = rslt;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700297
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700298 rc = qpnp_iadc_enable(false);
299 if (rc)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700300 return rc;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700301
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700302 return 0;
303}
304
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700305static int32_t qpnp_iadc_comp(int64_t *result, struct qpnp_iadc_comp comp,
306 int64_t die_temp)
307{
Xiaozhe Shi62ad5e12013-05-13 12:37:41 -0700308 int64_t temp_var = 0, sign_coeff = 0, sys_gain_coeff = 0, old;
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700309
Xiaozhe Shi62ad5e12013-05-13 12:37:41 -0700310 old = *result;
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700311 *result = *result * 1000000;
312
313 if (comp.revision == QPNP_IADC_VER_3_1) {
314 /* revision 3.1 */
315 if (comp.sys_gain > 127)
316 sys_gain_coeff = -QPNP_COEFF_6 * (comp.sys_gain - 128);
317 else
318 sys_gain_coeff = QPNP_COEFF_6 * comp.sys_gain;
Xiaozhe Shi62ad5e12013-05-13 12:37:41 -0700319 } else if (comp.revision != QPNP_IADC_VER_3_0) {
320 /* unsupported revision, do not compensate */
321 *result = old;
322 return 0;
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700323 }
324
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700325 if (!comp.ext_rsense) {
326 /* internal rsense */
327 switch (comp.id) {
328 case COMP_ID_TSMC:
329 temp_var = ((QPNP_COEFF_2 * die_temp) -
330 QPNP_COEFF_3_TYPEB);
331 break;
332 case COMP_ID_GF:
333 default:
334 temp_var = ((QPNP_COEFF_2 * die_temp) -
335 QPNP_COEFF_3_TYPEA);
336 break;
337 }
338 temp_var = div64_s64(temp_var, QPNP_COEFF_4);
339 if (comp.revision == QPNP_IADC_VER_3_0)
340 temp_var = QPNP_COEFF_1 * (1000000 - temp_var);
341 else if (comp.revision == QPNP_IADC_VER_3_1)
Xiaozhe Shi62ad5e12013-05-13 12:37:41 -0700342 temp_var = 1000000 * (1000000 - temp_var);
343 *result = div64_s64(*result * 1000000, temp_var);
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700344 }
345
346 sign_coeff = *result < 0 ? QPNP_COEFF_7 : QPNP_COEFF_5;
347 if (comp.ext_rsense) {
348 /* external rsense and current charging */
349 temp_var = div64_s64((-sign_coeff * die_temp) + QPNP_COEFF_8,
350 QPNP_COEFF_4);
351 temp_var = 1000000000 - temp_var;
352 if (comp.revision == QPNP_IADC_VER_3_1) {
353 sys_gain_coeff = (1000000 +
354 div64_s64(sys_gain_coeff, QPNP_COEFF_4));
355 temp_var = div64_s64(temp_var * sys_gain_coeff,
356 1000000000);
357 }
358 *result = div64_s64(*result, temp_var);
359 }
Xiaozhe Shi62ad5e12013-05-13 12:37:41 -0700360 pr_debug("%lld compensated into %lld\n", old, *result);
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700361
362 return 0;
363}
364
365int32_t qpnp_iadc_comp_result(int64_t *result)
366{
367 struct qpnp_iadc_drv *iadc = qpnp_iadc;
368
369 return qpnp_iadc_comp(result, iadc->iadc_comp, iadc->die_temp);
370}
371EXPORT_SYMBOL(qpnp_iadc_comp_result);
372
373static int32_t qpnp_iadc_comp_info(void)
374{
375 struct qpnp_iadc_drv *iadc = qpnp_iadc;
376 int rc = 0;
377
378 rc = qpnp_iadc_read_reg(QPNP_INT_TEST_VAL, &iadc->iadc_comp.id);
379 if (rc < 0) {
380 pr_err("qpnp adc comp id failed with %d\n", rc);
381 return rc;
382 }
383
384 rc = qpnp_iadc_read_reg(QPNP_IADC_REVISION2, &iadc->iadc_comp.revision);
385 if (rc < 0) {
386 pr_err("qpnp adc revision read failed with %d\n", rc);
387 return rc;
388 }
389
390 rc = qpnp_iadc_read_reg(QPNP_IADC_ATE_GAIN_CALIB_OFFSET,
391 &iadc->iadc_comp.sys_gain);
392 if (rc < 0)
393 pr_err("full scale read failed with %d\n", rc);
394
395 pr_debug("fab id = %u, revision = %u, sys gain = %u, external_rsense = %d\n",
396 iadc->iadc_comp.id,
397 iadc->iadc_comp.revision,
398 iadc->iadc_comp.sys_gain,
399 iadc->iadc_comp.ext_rsense);
400 return rc;
401}
402
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700403static int32_t qpnp_iadc_configure(enum qpnp_iadc_channels channel,
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800404 uint16_t *raw_code, uint32_t mode_sel)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700405{
406 struct qpnp_iadc_drv *iadc = qpnp_iadc;
407 u8 qpnp_iadc_mode_reg = 0, qpnp_iadc_ch_sel_reg = 0;
408 u8 qpnp_iadc_conv_req = 0, qpnp_iadc_dig_param_reg = 0;
409 int32_t rc = 0;
410
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700411 qpnp_iadc_ch_sel_reg = channel;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700412
413 qpnp_iadc_dig_param_reg |= iadc->adc->amux_prop->decimation <<
414 QPNP_IADC_DEC_RATIO_SEL;
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800415 if (iadc->iadc_mode_sel)
416 qpnp_iadc_mode_reg |= (QPNP_ADC_TRIM_EN | QPNP_VADC_SYNCH_EN);
417 else
418 qpnp_iadc_mode_reg |= QPNP_ADC_TRIM_EN;
419
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700420 qpnp_iadc_conv_req = QPNP_IADC_CONV_REQ;
421
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700422 rc = qpnp_iadc_write_reg(QPNP_IADC_MODE_CTL, qpnp_iadc_mode_reg);
423 if (rc) {
424 pr_err("qpnp adc read adc failed with %d\n", rc);
425 return rc;
426 }
427
428 rc = qpnp_iadc_write_reg(QPNP_IADC_ADC_CH_SEL_CTL,
429 qpnp_iadc_ch_sel_reg);
430 if (rc) {
431 pr_err("qpnp adc read adc failed with %d\n", rc);
432 return rc;
433 }
434
435 rc = qpnp_iadc_write_reg(QPNP_ADC_DIG_PARAM,
436 qpnp_iadc_dig_param_reg);
437 if (rc) {
438 pr_err("qpnp adc read adc failed with %d\n", rc);
439 return rc;
440 }
441
442 rc = qpnp_iadc_write_reg(QPNP_HW_SETTLE_DELAY,
443 iadc->adc->amux_prop->hw_settle_time);
444 if (rc < 0) {
445 pr_err("qpnp adc configure error for hw settling time setup\n");
446 return rc;
447 }
448
449 rc = qpnp_iadc_write_reg(QPNP_FAST_AVG_CTL,
450 iadc->adc->amux_prop->fast_avg_setup);
451 if (rc < 0) {
452 pr_err("qpnp adc fast averaging configure error\n");
453 return rc;
454 }
455
Siddartha Mohanadoss3f219c42013-04-02 11:01:28 -0700456 INIT_COMPLETION(iadc->adc->adc_rslt_completion);
457
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700458 rc = qpnp_iadc_enable(true);
459 if (rc)
460 return rc;
461
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700462 rc = qpnp_iadc_write_reg(QPNP_CONV_REQ, qpnp_iadc_conv_req);
463 if (rc) {
464 pr_err("qpnp adc read adc failed with %d\n", rc);
465 return rc;
466 }
467
Siddartha Mohanadoss1a0d2032012-11-01 11:22:29 -0700468 rc = wait_for_completion_timeout(&iadc->adc->adc_rslt_completion,
469 QPNP_ADC_COMPLETION_TIMEOUT);
470 if (!rc) {
471 u8 status1 = 0;
472 rc = qpnp_iadc_read_reg(QPNP_STATUS1, &status1);
473 if (rc < 0)
474 return rc;
475 status1 &= (QPNP_STATUS1_REQ_STS | QPNP_STATUS1_EOC);
476 if (status1 == QPNP_STATUS1_EOC)
477 pr_debug("End of conversion status set\n");
478 else {
Siddartha Mohanadossd3a3c952012-12-10 16:55:19 -0800479 rc = qpnp_iadc_status_debug();
480 if (rc < 0) {
481 pr_err("status1 read failed with %d\n", rc);
482 return rc;
483 }
Siddartha Mohanadoss1a0d2032012-11-01 11:22:29 -0700484 return -EINVAL;
485 }
486 }
487
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700488 rc = qpnp_iadc_read_conversion_result(raw_code);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700489 if (rc) {
490 pr_err("qpnp adc read adc failed with %d\n", rc);
491 return rc;
492 }
493
494 return 0;
495}
496
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700497static int32_t qpnp_convert_raw_offset_voltage(void)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700498{
499 struct qpnp_iadc_drv *iadc = qpnp_iadc;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700500 uint32_t num = 0;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700501
Siddartha Mohanadossd752e472013-02-26 18:30:14 -0800502 if ((iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw) == 0) {
503 pr_err("raw offset errors! raw_gain:0x%x and raw_offset:0x%x\n",
504 iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw);
505 return -EINVAL;
506 }
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700507
Siddartha Mohanadossd752e472013-02-26 18:30:14 -0800508 iadc->adc->calib.offset_uv = 0;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700509
510 num = iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw;
511
512 iadc->adc->calib.gain_uv = (num * QPNP_ADC_GAIN_NV)/
513 (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw);
514
Siddartha Mohanadossb2a42372013-03-26 15:53:41 -0700515 pr_debug("gain_uv:%d offset_uv:%d\n",
516 iadc->adc->calib.gain_uv, iadc->adc->calib.offset_uv);
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700517 return 0;
518}
519
Siddartha Mohanadoss06673922013-03-27 11:14:19 -0700520int32_t qpnp_iadc_calibrate_for_trim(void)
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700521{
522 struct qpnp_iadc_drv *iadc = qpnp_iadc;
523 uint8_t rslt_lsb, rslt_msb;
524 int32_t rc = 0;
525 uint16_t raw_data;
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800526 uint32_t mode_sel = 0;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700527
Siddartha Mohanadossa9b91672013-02-22 18:32:27 -0800528 mutex_lock(&iadc->adc->adc_lock);
529
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800530 rc = qpnp_iadc_configure(GAIN_CALIBRATION_17P857MV,
531 &raw_data, mode_sel);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700532 if (rc < 0) {
533 pr_err("qpnp adc result read failed with %d\n", rc);
534 goto fail;
535 }
536
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700537 iadc->adc->calib.gain_raw = raw_data;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700538
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800539 rc = qpnp_iadc_configure(OFFSET_CALIBRATION_CSP2_CSN2,
540 &raw_data, mode_sel);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700541 if (rc < 0) {
542 pr_err("qpnp adc result read failed with %d\n", rc);
543 goto fail;
544 }
545
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700546 iadc->adc->calib.offset_raw = raw_data;
547 if (rc < 0) {
548 pr_err("qpnp adc offset/gain calculation failed\n");
549 goto fail;
550 }
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700551
Siddartha Mohanadossb2a42372013-03-26 15:53:41 -0700552 pr_debug("raw gain:0x%x, raw offset:0x%x\n",
553 iadc->adc->calib.gain_raw, iadc->adc->calib.offset_raw);
554
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700555 rc = qpnp_convert_raw_offset_voltage();
Siddartha Mohanadossd752e472013-02-26 18:30:14 -0800556 if (rc < 0) {
557 pr_err("qpnp raw_voltage conversion failed\n");
558 goto fail;
559 }
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700560
561 rslt_msb = (raw_data & QPNP_RAW_CODE_16_BIT_MSB_MASK) >>
562 QPNP_BIT_SHIFT_8;
563 rslt_lsb = raw_data & QPNP_RAW_CODE_16_BIT_LSB_MASK;
564
Siddartha Mohanadossb2a42372013-03-26 15:53:41 -0700565 pr_debug("trim values:lsb:0x%x and msb:0x%x\n", rslt_lsb, rslt_msb);
566
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700567 rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS,
568 QPNP_IADC_SEC_ACCESS_DATA);
569 if (rc < 0) {
570 pr_err("qpnp iadc configure error for sec access\n");
571 goto fail;
572 }
573
574 rc = qpnp_iadc_write_reg(QPNP_IADC_MSB_OFFSET,
575 rslt_msb);
576 if (rc < 0) {
577 pr_err("qpnp iadc configure error for MSB write\n");
578 goto fail;
579 }
580
581 rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS,
582 QPNP_IADC_SEC_ACCESS_DATA);
583 if (rc < 0) {
584 pr_err("qpnp iadc configure error for sec access\n");
585 goto fail;
586 }
587
588 rc = qpnp_iadc_write_reg(QPNP_IADC_LSB_OFFSET,
589 rslt_lsb);
590 if (rc < 0) {
591 pr_err("qpnp iadc configure error for LSB write\n");
592 goto fail;
593 }
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700594fail:
Siddartha Mohanadossa9b91672013-02-22 18:32:27 -0800595 mutex_unlock(&iadc->adc->adc_lock);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700596 return rc;
597}
Siddartha Mohanadoss06673922013-03-27 11:14:19 -0700598EXPORT_SYMBOL(qpnp_iadc_calibrate_for_trim);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700599
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700600static void qpnp_iadc_work(struct work_struct *work)
601{
602 struct qpnp_iadc_drv *iadc = qpnp_iadc;
603 int rc = 0;
604
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700605 rc = qpnp_iadc_calibrate_for_trim();
Siddartha Mohanadoss73ae69b2013-04-03 17:34:03 -0700606 if (rc)
607 pr_debug("periodic IADC calibration failed\n");
608 else
Siddartha Mohanadoss12a15ea2013-02-05 19:13:41 -0800609 schedule_delayed_work(&iadc->iadc_work,
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700610 round_jiffies_relative(msecs_to_jiffies
611 (QPNP_IADC_CALIB_SECONDS)));
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700612 return;
613}
614
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700615static int32_t qpnp_iadc_version_check(void)
616{
617 uint8_t revision;
618 int rc;
619
620 rc = qpnp_iadc_read_reg(QPNP_IADC_REVISION2, &revision);
621 if (rc < 0) {
622 pr_err("qpnp adc result read failed with %d\n", rc);
623 return rc;
624 }
625
626 if (revision < QPNP_IADC_SUPPORTED_REVISION2) {
627 pr_err("IADC Version not supported\n");
628 return -EINVAL;
629 }
630
631 return 0;
632}
633
634int32_t qpnp_iadc_is_ready(void)
635{
636 struct qpnp_iadc_drv *iadc = qpnp_iadc;
637
638 if (!iadc || !iadc->iadc_initialized)
639 return -EPROBE_DEFER;
640 else
641 return 0;
642}
643EXPORT_SYMBOL(qpnp_iadc_is_ready);
644
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700645int32_t qpnp_iadc_get_rsense(int32_t *rsense)
646{
Siddartha Mohanadossa9b91672013-02-22 18:32:27 -0800647 struct qpnp_iadc_drv *iadc = qpnp_iadc;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700648 uint8_t rslt_rsense;
649 int32_t rc, sign_bit = 0;
650
Siddartha Mohanadossa9b91672013-02-22 18:32:27 -0800651 if (!iadc || !iadc->iadc_initialized)
652 return -EPROBE_DEFER;
653
Siddartha Mohanadosse70010b2013-04-04 14:51:41 -0700654 if (iadc->external_rsense)
655 *rsense = iadc->rsense;
656
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700657 rc = qpnp_iadc_read_reg(QPNP_IADC_NOMINAL_RSENSE, &rslt_rsense);
658 if (rc < 0) {
659 pr_err("qpnp adc rsense read failed with %d\n", rc);
660 return rc;
661 }
662
Siddartha Mohanadossb2a42372013-03-26 15:53:41 -0700663 pr_debug("rsense:0%x\n", rslt_rsense);
664
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700665 if (rslt_rsense & QPNP_RSENSE_MSB_SIGN_CHECK)
666 sign_bit = 1;
667
668 rslt_rsense &= ~QPNP_RSENSE_MSB_SIGN_CHECK;
669
670 if (sign_bit)
671 *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR -
672 (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT);
673 else
674 *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR +
675 (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT);
676
677 return rc;
678}
Xiaozhe Shi767fdb62013-01-10 15:09:08 -0800679EXPORT_SYMBOL(qpnp_iadc_get_rsense);
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700680
Siddartha Mohanadossa9b91672013-02-22 18:32:27 -0800681static int32_t qpnp_check_pmic_temp(void)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700682{
683 struct qpnp_iadc_drv *iadc = qpnp_iadc;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700684 struct qpnp_vadc_result result_pmic_therm;
Siddartha Mohanadossd4e9edc2013-04-17 14:42:16 -0700685 int64_t die_temp_offset;
Siddartha Mohanadossd752e472013-02-26 18:30:14 -0800686 int rc = 0;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700687
688 rc = qpnp_vadc_read(DIE_TEMP, &result_pmic_therm);
689 if (rc < 0)
690 return rc;
691
Siddartha Mohanadossd4e9edc2013-04-17 14:42:16 -0700692 die_temp_offset = result_pmic_therm.physical -
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700693 iadc->die_temp;
Siddartha Mohanadossd4e9edc2013-04-17 14:42:16 -0700694 if (die_temp_offset < 0)
695 die_temp_offset = -die_temp_offset;
696
697 if (die_temp_offset > QPNP_IADC_DIE_TEMP_CALIB_OFFSET) {
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700698 iadc->die_temp =
Siddartha Mohanadossd4e9edc2013-04-17 14:42:16 -0700699 result_pmic_therm.physical;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700700 rc = qpnp_iadc_calibrate_for_trim();
701 if (rc)
702 pr_err("periodic IADC calibration failed\n");
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700703 }
704
Siddartha Mohanadossd752e472013-02-26 18:30:14 -0800705 return rc;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700706}
707
708int32_t qpnp_iadc_read(enum qpnp_iadc_channels channel,
709 struct qpnp_iadc_result *result)
710{
711 struct qpnp_iadc_drv *iadc = qpnp_iadc;
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800712 int32_t rc, rsense_n_ohms, sign = 0, num, mode_sel = 0;
Siddartha Mohanadoss22618be2013-04-02 15:02:19 -0700713 int32_t rsense_u_ohms = 0;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700714 int64_t result_current;
715 uint16_t raw_data;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700716
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700717 if (!iadc || !iadc->iadc_initialized)
718 return -EPROBE_DEFER;
719
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800720 if (!iadc->iadc_mode_sel) {
721 rc = qpnp_check_pmic_temp();
722 if (rc) {
723 pr_err("Error checking pmic therm temp\n");
724 return rc;
725 }
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700726 }
727
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700728 mutex_lock(&iadc->adc->adc_lock);
729
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800730 rc = qpnp_iadc_configure(channel, &raw_data, mode_sel);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700731 if (rc < 0) {
732 pr_err("qpnp adc result read failed with %d\n", rc);
733 goto fail;
734 }
735
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700736 rc = qpnp_iadc_get_rsense(&rsense_n_ohms);
Siddartha Mohanadossb2a42372013-03-26 15:53:41 -0700737 pr_debug("current raw:0%x and rsense:%d\n",
738 raw_data, rsense_n_ohms);
Siddartha Mohanadoss22618be2013-04-02 15:02:19 -0700739 rsense_u_ohms = rsense_n_ohms/1000;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700740 num = raw_data - iadc->adc->calib.offset_raw;
741 if (num < 0) {
742 sign = 1;
743 num = -num;
744 }
745
746 result->result_uv = (num * QPNP_ADC_GAIN_NV)/
747 (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw);
748 result_current = result->result_uv;
749 result_current *= QPNP_IADC_NANO_VOLTS_FACTOR;
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700750 /* Intentional fall through. Process the result w/o comp */
Siddartha Mohanadoss22618be2013-04-02 15:02:19 -0700751 do_div(result_current, rsense_u_ohms);
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700752
753 if (sign) {
754 result->result_uv = -result->result_uv;
755 result_current = -result_current;
756 }
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700757 rc = qpnp_iadc_comp_result(&result_current);
758 if (rc < 0)
759 pr_err("Error during compensating the IADC\n");
760 rc = 0;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700761
762 result->result_ua = (int32_t) result_current;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700763fail:
764 mutex_unlock(&iadc->adc->adc_lock);
765
766 return rc;
767}
768EXPORT_SYMBOL(qpnp_iadc_read);
769
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700770int32_t qpnp_iadc_get_gain_and_offset(struct qpnp_iadc_calib *result)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700771{
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700772 struct qpnp_iadc_drv *iadc = qpnp_iadc;
773 int rc;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700774
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700775 if (!iadc || !iadc->iadc_initialized)
776 return -EPROBE_DEFER;
777
778 rc = qpnp_check_pmic_temp();
779 if (rc) {
780 pr_err("Error checking pmic therm temp\n");
781 return rc;
782 }
783
784 mutex_lock(&iadc->adc->adc_lock);
785 result->gain_raw = iadc->adc->calib.gain_raw;
786 result->ideal_gain_nv = QPNP_ADC_GAIN_NV;
787 result->gain_uv = iadc->adc->calib.gain_uv;
788 result->offset_raw = iadc->adc->calib.offset_raw;
789 result->ideal_offset_uv =
790 QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL;
791 result->offset_uv = iadc->adc->calib.offset_uv;
Siddartha Mohanadossb2a42372013-03-26 15:53:41 -0700792 pr_debug("raw gain:0%x, raw offset:0%x\n",
793 result->gain_raw, result->offset_raw);
794 pr_debug("gain_uv:%d offset_uv:%d\n",
795 result->gain_uv, result->offset_uv);
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700796 mutex_unlock(&iadc->adc->adc_lock);
797
798 return 0;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700799}
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700800EXPORT_SYMBOL(qpnp_iadc_get_gain_and_offset);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700801
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800802int32_t qpnp_iadc_vadc_sync_read(
803 enum qpnp_iadc_channels i_channel, struct qpnp_iadc_result *i_result,
804 enum qpnp_vadc_channels v_channel, struct qpnp_vadc_result *v_result)
805{
806 struct qpnp_iadc_drv *iadc = qpnp_iadc;
807 int rc = 0;
808
809 if (!iadc || !iadc->iadc_initialized)
810 return -EPROBE_DEFER;
811
812 mutex_lock(&iadc->iadc_vadc_lock);
813
814 rc = qpnp_check_pmic_temp();
815 if (rc) {
816 pr_err("PMIC die temp check failed\n");
817 goto fail;
818 }
819
820 iadc->iadc_mode_sel = true;
821
822 rc = qpnp_vadc_iadc_sync_request(v_channel);
823 if (rc) {
824 pr_err("Configuring VADC failed\n");
825 goto fail;
826 }
827
828 rc = qpnp_iadc_read(i_channel, i_result);
829 if (rc)
830 pr_err("Configuring IADC failed\n");
831 /* Intentional fall through to release VADC */
832
833 rc = qpnp_vadc_iadc_sync_complete_request(v_channel,
834 v_result);
835 if (rc)
836 pr_err("Releasing VADC failed\n");
837fail:
838 iadc->iadc_mode_sel = false;
839
840 mutex_unlock(&iadc->iadc_vadc_lock);
841
842 return rc;
843}
844EXPORT_SYMBOL(qpnp_iadc_vadc_sync_read);
845
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700846static ssize_t qpnp_iadc_show(struct device *dev,
847 struct device_attribute *devattr, char *buf)
848{
849 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700850 struct qpnp_iadc_result result;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700851 int rc = -1;
852
853 rc = qpnp_iadc_read(attr->index, &result);
854
855 if (rc)
856 return 0;
857
858 return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH,
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700859 "Result:%d\n", result.result_ua);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700860}
861
862static struct sensor_device_attribute qpnp_adc_attr =
863 SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0);
864
865static int32_t qpnp_iadc_init_hwmon(struct spmi_device *spmi)
866{
867 struct qpnp_iadc_drv *iadc = qpnp_iadc;
868 struct device_node *child;
869 struct device_node *node = spmi->dev.of_node;
870 int rc = 0, i = 0, channel;
871
872 for_each_child_of_node(node, child) {
873 channel = iadc->adc->adc_channels[i].channel_num;
874 qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num;
875 qpnp_adc_attr.dev_attr.attr.name =
876 iadc->adc->adc_channels[i].name;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700877 memcpy(&iadc->sens_attr[i], &qpnp_adc_attr,
878 sizeof(qpnp_adc_attr));
Stephen Boyd8a5c4e42012-10-30 11:07:22 -0700879 sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700880 rc = device_create_file(&spmi->dev,
881 &iadc->sens_attr[i].dev_attr);
882 if (rc) {
883 dev_err(&spmi->dev,
884 "device_create_file failed for dev %s\n",
885 iadc->adc->adc_channels[i].name);
886 goto hwmon_err_sens;
887 }
888 i++;
889 }
890
891 return 0;
892hwmon_err_sens:
893 pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc);
894 return rc;
895}
896
897static int __devinit qpnp_iadc_probe(struct spmi_device *spmi)
898{
899 struct qpnp_iadc_drv *iadc;
900 struct qpnp_adc_drv *adc_qpnp;
901 struct device_node *node = spmi->dev.of_node;
902 struct device_node *child;
903 int rc, count_adc_channel_list = 0;
904
905 if (!node)
906 return -EINVAL;
907
908 if (qpnp_iadc) {
909 pr_err("IADC already in use\n");
910 return -EBUSY;
911 }
912
913 for_each_child_of_node(node, child)
914 count_adc_channel_list++;
915
916 if (!count_adc_channel_list) {
917 pr_err("No channel listing\n");
918 return -EINVAL;
919 }
920
921 iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_drv) +
922 (sizeof(struct sensor_device_attribute) *
923 count_adc_channel_list), GFP_KERNEL);
924 if (!iadc) {
925 dev_err(&spmi->dev, "Unable to allocate memory\n");
926 return -ENOMEM;
927 }
928
929 adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv),
930 GFP_KERNEL);
931 if (!adc_qpnp) {
932 dev_err(&spmi->dev, "Unable to allocate memory\n");
Siddartha Mohanadossb60f6462012-11-20 18:06:51 -0800933 rc = -ENOMEM;
934 goto fail;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700935 }
936
937 iadc->adc = adc_qpnp;
938
939 rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc);
940 if (rc) {
941 dev_err(&spmi->dev, "failed to read device tree\n");
Siddartha Mohanadossb60f6462012-11-20 18:06:51 -0800942 goto fail;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700943 }
944
Stephen Boydbeab4502013-04-25 10:18:17 -0700945 mutex_init(&iadc->adc->adc_lock);
946
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700947 rc = of_property_read_u32(node, "qcom,rsense",
948 &iadc->rsense);
Siddartha Mohanadosse70010b2013-04-04 14:51:41 -0700949 if (rc)
950 pr_debug("Defaulting to internal rsense\n");
951 else {
952 pr_debug("Use external rsense\n");
953 iadc->external_rsense = true;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700954 }
955
Siddartha Mohanadoss12109952012-11-20 14:57:51 -0800956 rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq_eoc,
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700957 qpnp_iadc_isr,
958 IRQF_TRIGGER_RISING, "qpnp_iadc_interrupt", iadc);
959 if (rc) {
960 dev_err(&spmi->dev, "failed to request adc irq\n");
Siddartha Mohanadossb60f6462012-11-20 18:06:51 -0800961 goto fail;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700962 } else
Siddartha Mohanadoss12109952012-11-20 14:57:51 -0800963 enable_irq_wake(iadc->adc->adc_irq_eoc);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700964
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700965 dev_set_drvdata(&spmi->dev, iadc);
966 qpnp_iadc = iadc;
967
968 rc = qpnp_iadc_init_hwmon(spmi);
969 if (rc) {
970 dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n");
Siddartha Mohanadossb60f6462012-11-20 18:06:51 -0800971 goto fail;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700972 }
973 iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev);
974
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700975 rc = qpnp_iadc_version_check();
976 if (rc) {
977 dev_err(&spmi->dev, "IADC version not supported\n");
Siddartha Mohanadossb60f6462012-11-20 18:06:51 -0800978 goto fail;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700979 }
980
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -0800981 mutex_init(&iadc->iadc_vadc_lock);
Siddartha Mohanadossa9b91672013-02-22 18:32:27 -0800982 INIT_DELAYED_WORK(&iadc->iadc_work, qpnp_iadc_work);
Siddartha Mohanadoss4e64f8c2013-04-08 15:57:32 -0700983 rc = qpnp_iadc_comp_info();
984 if (rc) {
985 dev_err(&spmi->dev, "abstracting IADC comp info failed!\n");
986 goto fail;
987 }
Siddartha Mohanadoss12a15ea2013-02-05 19:13:41 -0800988 iadc->iadc_initialized = true;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700989
Siddartha Mohanadossa9b91672013-02-22 18:32:27 -0800990 rc = qpnp_iadc_calibrate_for_trim();
991 if (rc)
992 dev_err(&spmi->dev, "failed to calibrate for USR trim\n");
993 schedule_delayed_work(&iadc->iadc_work,
994 round_jiffies_relative(msecs_to_jiffies
995 (QPNP_IADC_CALIB_SECONDS)));
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700996 return 0;
Siddartha Mohanadossb60f6462012-11-20 18:06:51 -0800997fail:
Siddartha Mohanadoss32019b52012-12-23 17:05:45 -0800998 qpnp_iadc = NULL;
Siddartha Mohanadossb60f6462012-11-20 18:06:51 -0800999 return rc;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -07001000}
1001
1002static int __devexit qpnp_iadc_remove(struct spmi_device *spmi)
1003{
1004 struct qpnp_iadc_drv *iadc = dev_get_drvdata(&spmi->dev);
1005 struct device_node *node = spmi->dev.of_node;
1006 struct device_node *child;
1007 int i = 0;
1008
Siddartha Mohanadossa9b91672013-02-22 18:32:27 -08001009 cancel_delayed_work(&iadc->iadc_work);
Siddartha Mohanadossa32ea2a2013-02-12 09:58:31 -08001010 mutex_destroy(&iadc->iadc_vadc_lock);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -07001011 for_each_child_of_node(node, child) {
1012 device_remove_file(&spmi->dev,
1013 &iadc->sens_attr[i].dev_attr);
1014 i++;
1015 }
1016 dev_set_drvdata(&spmi->dev, NULL);
1017
1018 return 0;
1019}
1020
1021static const struct of_device_id qpnp_iadc_match_table[] = {
1022 { .compatible = "qcom,qpnp-iadc",
1023 },
1024 {}
1025};
1026
1027static struct spmi_driver qpnp_iadc_driver = {
1028 .driver = {
1029 .name = "qcom,qpnp-iadc",
1030 .of_match_table = qpnp_iadc_match_table,
1031 },
1032 .probe = qpnp_iadc_probe,
1033 .remove = qpnp_iadc_remove,
1034};
1035
1036static int __init qpnp_iadc_init(void)
1037{
1038 return spmi_driver_register(&qpnp_iadc_driver);
1039}
1040module_init(qpnp_iadc_init);
1041
1042static void __exit qpnp_iadc_exit(void)
1043{
1044 spmi_driver_unregister(&qpnp_iadc_driver);
1045}
1046module_exit(qpnp_iadc_exit);
1047
1048MODULE_DESCRIPTION("QPNP PMIC current ADC driver");
1049MODULE_LICENSE("GPL v2");