blob: b5ee104189bb1bf1c8efa4ee402a5e1980de2da0 [file] [log] [blame]
Siddartha Mohanadoss12109952012-11-20 14:57:51 -08001/* Copyright (c) 2012, 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
98
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -070099#define QPNP_IADC_ADC_CH_SEL_CTL 0x48
100#define QPNP_IADC_ADC_CHX_SEL_SHIFT 3
101
102#define QPNP_IADC_ADC_DIG_PARAM 0x50
103#define QPNP_IADC_CLK_SEL_SHIFT 1
104#define QPNP_IADC_DEC_RATIO_SEL 3
105
106#define QPNP_IADC_CONV_REQUEST 0x52
107#define QPNP_IADC_CONV_REQ BIT(7)
108
109#define QPNP_IADC_DATA0 0x60
110#define QPNP_IADC_DATA1 0x61
111
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700112#define QPNP_ADC_CONV_TIME_MIN 8000
113#define QPNP_ADC_CONV_TIME_MAX 8200
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700114
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700115#define QPNP_ADC_GAIN_NV 17857
116#define QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL 0
117#define QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR 10000000
118#define QPNP_IADC_NANO_VOLTS_FACTOR 1000000000
119#define QPNP_IADC_CALIB_SECONDS 300000
120#define QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT 15625
121#define QPNP_IADC_DIE_TEMP_CALIB_OFFSET 5000
122
123#define QPNP_RAW_CODE_16_BIT_MSB_MASK 0xff00
124#define QPNP_RAW_CODE_16_BIT_LSB_MASK 0xff
125#define QPNP_BIT_SHIFT_8 8
126#define QPNP_RSENSE_MSB_SIGN_CHECK 0x80
Siddartha Mohanadoss1a0d2032012-11-01 11:22:29 -0700127#define QPNP_ADC_COMPLETION_TIMEOUT HZ
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700128
129struct qpnp_iadc_drv {
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700130 struct qpnp_adc_drv *adc;
131 int32_t rsense;
132 struct device *iadc_hwmon;
133 bool iadc_init_calib;
134 bool iadc_initialized;
135 int64_t die_temp_calib_offset;
136 struct delayed_work iadc_work;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700137 struct sensor_device_attribute sens_attr[0];
138};
139
140struct qpnp_iadc_drv *qpnp_iadc;
141
142static int32_t qpnp_iadc_read_reg(uint32_t reg, u8 *data)
143{
144 struct qpnp_iadc_drv *iadc = qpnp_iadc;
145 int rc;
146
147 rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave,
Siddartha Mohanadossae1da732012-08-08 16:39:02 -0700148 (iadc->adc->offset + reg), data, 1);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700149 if (rc < 0) {
150 pr_err("qpnp iadc read reg %d failed with %d\n", reg, rc);
151 return rc;
152 }
153
154 return 0;
155}
156
157static int32_t qpnp_iadc_write_reg(uint32_t reg, u8 data)
158{
159 struct qpnp_iadc_drv *iadc = qpnp_iadc;
160 int rc;
161 u8 *buf;
162
163 buf = &data;
164 rc = spmi_ext_register_writel(iadc->adc->spmi->ctrl, iadc->adc->slave,
Siddartha Mohanadossae1da732012-08-08 16:39:02 -0700165 (iadc->adc->offset + reg), buf, 1);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700166 if (rc < 0) {
167 pr_err("qpnp iadc write reg %d failed with %d\n", reg, rc);
168 return rc;
169 }
170
171 return 0;
172}
173
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700174static void trigger_iadc_completion(struct work_struct *work)
175{
176 struct qpnp_iadc_drv *iadc = qpnp_iadc;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700177
178 complete(&iadc->adc->adc_rslt_completion);
179
180 return;
181}
182DECLARE_WORK(trigger_iadc_completion_work, trigger_iadc_completion);
183
184static irqreturn_t qpnp_iadc_isr(int irq, void *dev_id)
185{
186 schedule_work(&trigger_iadc_completion_work);
187
188 return IRQ_HANDLED;
189}
190
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700191static int32_t qpnp_iadc_enable(bool state)
192{
193 int rc = 0;
194 u8 data = 0;
195
196 data = QPNP_IADC_ADC_EN;
197 if (state) {
198 rc = qpnp_iadc_write_reg(QPNP_IADC_EN_CTL1,
199 data);
200 if (rc < 0) {
201 pr_err("IADC enable failed\n");
202 return rc;
203 }
204 } else {
205 rc = qpnp_iadc_write_reg(QPNP_IADC_EN_CTL1,
206 (~data & QPNP_IADC_ADC_EN));
207 if (rc < 0) {
208 pr_err("IADC disable failed\n");
209 return rc;
210 }
211 }
212
213 return 0;
214}
215
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700216static int32_t qpnp_iadc_read_conversion_result(uint16_t *data)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700217{
218 uint8_t rslt_lsb, rslt_msb;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700219 uint16_t rslt;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700220 int32_t rc;
221
222 rc = qpnp_iadc_read_reg(QPNP_IADC_DATA0, &rslt_lsb);
223 if (rc < 0) {
224 pr_err("qpnp adc result read failed with %d\n", rc);
225 return rc;
226 }
227
228 rc = qpnp_iadc_read_reg(QPNP_IADC_DATA1, &rslt_msb);
229 if (rc < 0) {
230 pr_err("qpnp adc result read failed with %d\n", rc);
231 return rc;
232 }
233
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700234 rslt = (rslt_msb << 8) | rslt_lsb;
235 *data = rslt;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700236
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700237 rc = qpnp_iadc_enable(false);
238 if (rc)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700239 return rc;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700240
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700241 return 0;
242}
243
244static int32_t qpnp_iadc_configure(enum qpnp_iadc_channels channel,
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700245 uint16_t *raw_code)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700246{
247 struct qpnp_iadc_drv *iadc = qpnp_iadc;
248 u8 qpnp_iadc_mode_reg = 0, qpnp_iadc_ch_sel_reg = 0;
249 u8 qpnp_iadc_conv_req = 0, qpnp_iadc_dig_param_reg = 0;
250 int32_t rc = 0;
251
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700252 qpnp_iadc_ch_sel_reg = channel;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700253
254 qpnp_iadc_dig_param_reg |= iadc->adc->amux_prop->decimation <<
255 QPNP_IADC_DEC_RATIO_SEL;
256
257 qpnp_iadc_conv_req = QPNP_IADC_CONV_REQ;
258
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700259 rc = qpnp_iadc_write_reg(QPNP_IADC_MODE_CTL, qpnp_iadc_mode_reg);
260 if (rc) {
261 pr_err("qpnp adc read adc failed with %d\n", rc);
262 return rc;
263 }
264
265 rc = qpnp_iadc_write_reg(QPNP_IADC_ADC_CH_SEL_CTL,
266 qpnp_iadc_ch_sel_reg);
267 if (rc) {
268 pr_err("qpnp adc read adc failed with %d\n", rc);
269 return rc;
270 }
271
272 rc = qpnp_iadc_write_reg(QPNP_ADC_DIG_PARAM,
273 qpnp_iadc_dig_param_reg);
274 if (rc) {
275 pr_err("qpnp adc read adc failed with %d\n", rc);
276 return rc;
277 }
278
279 rc = qpnp_iadc_write_reg(QPNP_HW_SETTLE_DELAY,
280 iadc->adc->amux_prop->hw_settle_time);
281 if (rc < 0) {
282 pr_err("qpnp adc configure error for hw settling time setup\n");
283 return rc;
284 }
285
286 rc = qpnp_iadc_write_reg(QPNP_FAST_AVG_CTL,
287 iadc->adc->amux_prop->fast_avg_setup);
288 if (rc < 0) {
289 pr_err("qpnp adc fast averaging configure error\n");
290 return rc;
291 }
292
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700293 rc = qpnp_iadc_enable(true);
294 if (rc)
295 return rc;
296
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700297 rc = qpnp_iadc_write_reg(QPNP_CONV_REQ, qpnp_iadc_conv_req);
298 if (rc) {
299 pr_err("qpnp adc read adc failed with %d\n", rc);
300 return rc;
301 }
302
Siddartha Mohanadoss1a0d2032012-11-01 11:22:29 -0700303 rc = wait_for_completion_timeout(&iadc->adc->adc_rslt_completion,
304 QPNP_ADC_COMPLETION_TIMEOUT);
305 if (!rc) {
306 u8 status1 = 0;
307 rc = qpnp_iadc_read_reg(QPNP_STATUS1, &status1);
308 if (rc < 0)
309 return rc;
310 status1 &= (QPNP_STATUS1_REQ_STS | QPNP_STATUS1_EOC);
311 if (status1 == QPNP_STATUS1_EOC)
312 pr_debug("End of conversion status set\n");
313 else {
314 pr_err("EOC interrupt not received\n");
315 return -EINVAL;
316 }
317 }
318
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700319
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700320 rc = qpnp_iadc_read_conversion_result(raw_code);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700321 if (rc) {
322 pr_err("qpnp adc read adc failed with %d\n", rc);
323 return rc;
324 }
325
326 return 0;
327}
328
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700329static int32_t qpnp_convert_raw_offset_voltage(void)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700330{
331 struct qpnp_iadc_drv *iadc = qpnp_iadc;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700332 uint32_t num = 0;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700333
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700334 num = iadc->adc->calib.offset_raw - iadc->adc->calib.offset_raw;
335
336 iadc->adc->calib.offset_uv = (num * QPNP_ADC_GAIN_NV)/
337 (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw);
338
339 num = iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw;
340
341 iadc->adc->calib.gain_uv = (num * QPNP_ADC_GAIN_NV)/
342 (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw);
343
344 return 0;
345}
346
347static int32_t qpnp_iadc_calibrate_for_trim(void)
348{
349 struct qpnp_iadc_drv *iadc = qpnp_iadc;
350 uint8_t rslt_lsb, rslt_msb;
351 int32_t rc = 0;
352 uint16_t raw_data;
353
354 rc = qpnp_iadc_configure(GAIN_CALIBRATION_17P857MV, &raw_data);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700355 if (rc < 0) {
356 pr_err("qpnp adc result read failed with %d\n", rc);
357 goto fail;
358 }
359
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700360 iadc->adc->calib.gain_raw = raw_data;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700361
362 rc = qpnp_iadc_configure(OFFSET_CALIBRATION_SHORT_CADC_LEADS,
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700363 &raw_data);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700364 if (rc < 0) {
365 pr_err("qpnp adc result read failed with %d\n", rc);
366 goto fail;
367 }
368
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700369 iadc->adc->calib.offset_raw = raw_data;
370 if (rc < 0) {
371 pr_err("qpnp adc offset/gain calculation failed\n");
372 goto fail;
373 }
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700374
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700375 rc = qpnp_convert_raw_offset_voltage();
376
377 rslt_msb = (raw_data & QPNP_RAW_CODE_16_BIT_MSB_MASK) >>
378 QPNP_BIT_SHIFT_8;
379 rslt_lsb = raw_data & QPNP_RAW_CODE_16_BIT_LSB_MASK;
380
381 rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS,
382 QPNP_IADC_SEC_ACCESS_DATA);
383 if (rc < 0) {
384 pr_err("qpnp iadc configure error for sec access\n");
385 goto fail;
386 }
387
388 rc = qpnp_iadc_write_reg(QPNP_IADC_MSB_OFFSET,
389 rslt_msb);
390 if (rc < 0) {
391 pr_err("qpnp iadc configure error for MSB write\n");
392 goto fail;
393 }
394
395 rc = qpnp_iadc_write_reg(QPNP_IADC_SEC_ACCESS,
396 QPNP_IADC_SEC_ACCESS_DATA);
397 if (rc < 0) {
398 pr_err("qpnp iadc configure error for sec access\n");
399 goto fail;
400 }
401
402 rc = qpnp_iadc_write_reg(QPNP_IADC_LSB_OFFSET,
403 rslt_lsb);
404 if (rc < 0) {
405 pr_err("qpnp iadc configure error for LSB write\n");
406 goto fail;
407 }
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700408fail:
409 return rc;
410}
411
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700412static void qpnp_iadc_work(struct work_struct *work)
413{
414 struct qpnp_iadc_drv *iadc = qpnp_iadc;
415 int rc = 0;
416
417 mutex_lock(&iadc->adc->adc_lock);
418
419 rc = qpnp_iadc_calibrate_for_trim();
420 if (rc)
421 pr_err("periodic IADC calibration failed\n");
422
423 mutex_unlock(&iadc->adc->adc_lock);
424
425 schedule_delayed_work(&iadc->iadc_work,
426 round_jiffies_relative(msecs_to_jiffies
427 (QPNP_IADC_CALIB_SECONDS)));
428
429 return;
430}
431
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700432static int32_t qpnp_iadc_version_check(void)
433{
434 uint8_t revision;
435 int rc;
436
437 rc = qpnp_iadc_read_reg(QPNP_IADC_REVISION2, &revision);
438 if (rc < 0) {
439 pr_err("qpnp adc result read failed with %d\n", rc);
440 return rc;
441 }
442
443 if (revision < QPNP_IADC_SUPPORTED_REVISION2) {
444 pr_err("IADC Version not supported\n");
445 return -EINVAL;
446 }
447
448 return 0;
449}
450
451int32_t qpnp_iadc_is_ready(void)
452{
453 struct qpnp_iadc_drv *iadc = qpnp_iadc;
454
455 if (!iadc || !iadc->iadc_initialized)
456 return -EPROBE_DEFER;
457 else
458 return 0;
459}
460EXPORT_SYMBOL(qpnp_iadc_is_ready);
461
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700462int32_t qpnp_iadc_get_rsense(int32_t *rsense)
463{
464 uint8_t rslt_rsense;
465 int32_t rc, sign_bit = 0;
466
467 rc = qpnp_iadc_read_reg(QPNP_IADC_NOMINAL_RSENSE, &rslt_rsense);
468 if (rc < 0) {
469 pr_err("qpnp adc rsense read failed with %d\n", rc);
470 return rc;
471 }
472
473 if (rslt_rsense & QPNP_RSENSE_MSB_SIGN_CHECK)
474 sign_bit = 1;
475
476 rslt_rsense &= ~QPNP_RSENSE_MSB_SIGN_CHECK;
477
478 if (sign_bit)
479 *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR -
480 (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT);
481 else
482 *rsense = QPNP_IADC_INTERNAL_RSENSE_N_OHMS_FACTOR +
483 (rslt_rsense * QPNP_IADC_RSENSE_LSB_N_OHMS_PER_BIT);
484
485 return rc;
486}
487
488int32_t qpnp_check_pmic_temp(void)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700489{
490 struct qpnp_iadc_drv *iadc = qpnp_iadc;
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700491 struct qpnp_vadc_result result_pmic_therm;
492 int rc;
493
494 rc = qpnp_vadc_read(DIE_TEMP, &result_pmic_therm);
495 if (rc < 0)
496 return rc;
497
498 if (((uint64_t) (result_pmic_therm.physical -
499 iadc->die_temp_calib_offset))
500 > QPNP_IADC_DIE_TEMP_CALIB_OFFSET) {
501 mutex_lock(&iadc->adc->adc_lock);
502
503 rc = qpnp_iadc_calibrate_for_trim();
504 if (rc)
505 pr_err("periodic IADC calibration failed\n");
506
507 mutex_unlock(&iadc->adc->adc_lock);
508 }
509
510 return 0;
511}
512
513int32_t qpnp_iadc_read(enum qpnp_iadc_channels channel,
514 struct qpnp_iadc_result *result)
515{
516 struct qpnp_iadc_drv *iadc = qpnp_iadc;
517 int32_t rc, rsense_n_ohms, sign = 0, num;
518 int64_t result_current;
519 uint16_t raw_data;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700520
Siddartha Mohanadoss5ace1102012-08-20 23:18:10 -0700521 if (!iadc || !iadc->iadc_initialized)
522 return -EPROBE_DEFER;
523
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700524 rc = qpnp_check_pmic_temp();
525 if (rc) {
526 pr_err("Error checking pmic therm temp\n");
527 return rc;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700528 }
529
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700530 mutex_lock(&iadc->adc->adc_lock);
531
532 rc = qpnp_iadc_configure(channel, &raw_data);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700533 if (rc < 0) {
534 pr_err("qpnp adc result read failed with %d\n", rc);
535 goto fail;
536 }
537
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700538 rc = qpnp_iadc_get_rsense(&rsense_n_ohms);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700539
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700540 num = raw_data - iadc->adc->calib.offset_raw;
541 if (num < 0) {
542 sign = 1;
543 num = -num;
544 }
545
546 result->result_uv = (num * QPNP_ADC_GAIN_NV)/
547 (iadc->adc->calib.gain_raw - iadc->adc->calib.offset_raw);
548 result_current = result->result_uv;
549 result_current *= QPNP_IADC_NANO_VOLTS_FACTOR;
550 do_div(result_current, rsense_n_ohms);
551
552 if (sign) {
553 result->result_uv = -result->result_uv;
554 result_current = -result_current;
555 }
556
557 result->result_ua = (int32_t) result_current;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700558fail:
559 mutex_unlock(&iadc->adc->adc_lock);
560
561 return rc;
562}
563EXPORT_SYMBOL(qpnp_iadc_read);
564
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700565int32_t qpnp_iadc_get_gain_and_offset(struct qpnp_iadc_calib *result)
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700566{
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700567 struct qpnp_iadc_drv *iadc = qpnp_iadc;
568 int rc;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700569
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700570 if (!iadc || !iadc->iadc_initialized)
571 return -EPROBE_DEFER;
572
573 rc = qpnp_check_pmic_temp();
574 if (rc) {
575 pr_err("Error checking pmic therm temp\n");
576 return rc;
577 }
578
579 mutex_lock(&iadc->adc->adc_lock);
580 result->gain_raw = iadc->adc->calib.gain_raw;
581 result->ideal_gain_nv = QPNP_ADC_GAIN_NV;
582 result->gain_uv = iadc->adc->calib.gain_uv;
583 result->offset_raw = iadc->adc->calib.offset_raw;
584 result->ideal_offset_uv =
585 QPNP_OFFSET_CALIBRATION_SHORT_CADC_LEADS_IDEAL;
586 result->offset_uv = iadc->adc->calib.offset_uv;
587 mutex_unlock(&iadc->adc->adc_lock);
588
589 return 0;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700590}
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700591EXPORT_SYMBOL(qpnp_iadc_get_gain_and_offset);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700592
593static ssize_t qpnp_iadc_show(struct device *dev,
594 struct device_attribute *devattr, char *buf)
595{
596 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700597 struct qpnp_iadc_result result;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700598 int rc = -1;
599
600 rc = qpnp_iadc_read(attr->index, &result);
601
602 if (rc)
603 return 0;
604
605 return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH,
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700606 "Result:%d\n", result.result_ua);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700607}
608
609static struct sensor_device_attribute qpnp_adc_attr =
610 SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0);
611
612static int32_t qpnp_iadc_init_hwmon(struct spmi_device *spmi)
613{
614 struct qpnp_iadc_drv *iadc = qpnp_iadc;
615 struct device_node *child;
616 struct device_node *node = spmi->dev.of_node;
617 int rc = 0, i = 0, channel;
618
619 for_each_child_of_node(node, child) {
620 channel = iadc->adc->adc_channels[i].channel_num;
621 qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num;
622 qpnp_adc_attr.dev_attr.attr.name =
623 iadc->adc->adc_channels[i].name;
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700624 memcpy(&iadc->sens_attr[i], &qpnp_adc_attr,
625 sizeof(qpnp_adc_attr));
Stephen Boyd8a5c4e42012-10-30 11:07:22 -0700626 sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700627 rc = device_create_file(&spmi->dev,
628 &iadc->sens_attr[i].dev_attr);
629 if (rc) {
630 dev_err(&spmi->dev,
631 "device_create_file failed for dev %s\n",
632 iadc->adc->adc_channels[i].name);
633 goto hwmon_err_sens;
634 }
635 i++;
636 }
637
638 return 0;
639hwmon_err_sens:
640 pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc);
641 return rc;
642}
643
644static int __devinit qpnp_iadc_probe(struct spmi_device *spmi)
645{
646 struct qpnp_iadc_drv *iadc;
647 struct qpnp_adc_drv *adc_qpnp;
648 struct device_node *node = spmi->dev.of_node;
649 struct device_node *child;
650 int rc, count_adc_channel_list = 0;
651
652 if (!node)
653 return -EINVAL;
654
655 if (qpnp_iadc) {
656 pr_err("IADC already in use\n");
657 return -EBUSY;
658 }
659
660 for_each_child_of_node(node, child)
661 count_adc_channel_list++;
662
663 if (!count_adc_channel_list) {
664 pr_err("No channel listing\n");
665 return -EINVAL;
666 }
667
668 iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_drv) +
669 (sizeof(struct sensor_device_attribute) *
670 count_adc_channel_list), GFP_KERNEL);
671 if (!iadc) {
672 dev_err(&spmi->dev, "Unable to allocate memory\n");
673 return -ENOMEM;
674 }
675
676 adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv),
677 GFP_KERNEL);
678 if (!adc_qpnp) {
679 dev_err(&spmi->dev, "Unable to allocate memory\n");
680 return -ENOMEM;
681 }
682
683 iadc->adc = adc_qpnp;
684
685 rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc);
686 if (rc) {
687 dev_err(&spmi->dev, "failed to read device tree\n");
688 return rc;
689 }
690
691 rc = of_property_read_u32(node, "qcom,rsense",
692 &iadc->rsense);
693 if (rc) {
694 pr_err("Invalid rsens reference property\n");
695 return -EINVAL;
696 }
697
Siddartha Mohanadoss12109952012-11-20 14:57:51 -0800698 rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq_eoc,
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700699 qpnp_iadc_isr,
700 IRQF_TRIGGER_RISING, "qpnp_iadc_interrupt", iadc);
701 if (rc) {
702 dev_err(&spmi->dev, "failed to request adc irq\n");
703 return rc;
704 } else
Siddartha Mohanadoss12109952012-11-20 14:57:51 -0800705 enable_irq_wake(iadc->adc->adc_irq_eoc);
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700706
707 iadc->iadc_init_calib = false;
708 dev_set_drvdata(&spmi->dev, iadc);
709 qpnp_iadc = iadc;
710
711 rc = qpnp_iadc_init_hwmon(spmi);
712 if (rc) {
713 dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n");
714 return rc;
715 }
716 iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev);
717
Siddartha Mohanadoss5e2988d2012-09-24 17:03:56 -0700718 rc = qpnp_iadc_version_check();
719 if (rc) {
720 dev_err(&spmi->dev, "IADC version not supported\n");
721 return rc;
722 }
723
724 rc = qpnp_iadc_calibrate_for_trim();
725 if (rc) {
726 dev_err(&spmi->dev, "failed to calibrate for USR trim\n");
727 return rc;
728 }
729 iadc->iadc_init_calib = true;
730 INIT_DELAYED_WORK(&iadc->iadc_work, qpnp_iadc_work);
731 schedule_delayed_work(&iadc->iadc_work,
732 round_jiffies_relative(msecs_to_jiffies
733 (QPNP_IADC_CALIB_SECONDS)));
734 iadc->iadc_initialized = true;
735
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -0700736 return 0;
737}
738
739static int __devexit qpnp_iadc_remove(struct spmi_device *spmi)
740{
741 struct qpnp_iadc_drv *iadc = dev_get_drvdata(&spmi->dev);
742 struct device_node *node = spmi->dev.of_node;
743 struct device_node *child;
744 int i = 0;
745
746 for_each_child_of_node(node, child) {
747 device_remove_file(&spmi->dev,
748 &iadc->sens_attr[i].dev_attr);
749 i++;
750 }
751 dev_set_drvdata(&spmi->dev, NULL);
752
753 return 0;
754}
755
756static const struct of_device_id qpnp_iadc_match_table[] = {
757 { .compatible = "qcom,qpnp-iadc",
758 },
759 {}
760};
761
762static struct spmi_driver qpnp_iadc_driver = {
763 .driver = {
764 .name = "qcom,qpnp-iadc",
765 .of_match_table = qpnp_iadc_match_table,
766 },
767 .probe = qpnp_iadc_probe,
768 .remove = qpnp_iadc_remove,
769};
770
771static int __init qpnp_iadc_init(void)
772{
773 return spmi_driver_register(&qpnp_iadc_driver);
774}
775module_init(qpnp_iadc_init);
776
777static void __exit qpnp_iadc_exit(void)
778{
779 spmi_driver_unregister(&qpnp_iadc_driver);
780}
781module_exit(qpnp_iadc_exit);
782
783MODULE_DESCRIPTION("QPNP PMIC current ADC driver");
784MODULE_LICENSE("GPL v2");