blob: 95da3b993133e9e6b0b7032cf4b25fc86355dde8 [file] [log] [blame]
Siddartha Mohanadossc4a6af12012-07-13 18:50:12 -07001/* 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/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 */
36#define QPNP_STATUS1 0x8
37#define QPNP_STATUS1_OP_MODE 4
38#define QPNP_STATUS1_MULTI_MEAS_EN BIT(3)
39#define QPNP_STATUS1_MEAS_INTERVAL_EN_STS BIT(2)
40#define QPNP_STATUS1_REQ_STS BIT(1)
41#define QPNP_STATUS1_EOC BIT(0)
42#define QPNP_STATUS2 0x9
43#define QPNP_STATUS2_CONV_SEQ_STATE_SHIFT 4
44#define QPNP_STATUS2_FIFO_NOT_EMPTY_FLAG BIT(1)
45#define QPNP_STATUS2_CONV_SEQ_TIMEOUT_STS BIT(0)
46#define QPNP_CONV_TIMEOUT_ERR 2
47
48#define QPNP_INT_RT_ST 0x10
49#define QPNP_INT_SET_TYPE 0x11
50#define QPNP_INT_SET_TYPE_LOW_THR_INT_SET BIT(4)
51#define QPNP_INT_SET_TYPE_HIGH_THR_INT_SET BIT(3)
52#define QPNP_INT_SET_TYPE_CONV_SEQ_TIMEOUT_INT_SET BIT(2)
53#define QPNP_INT_SET_TYPE_FIFO_NOT_EMPTY_INT_SET BIT(1)
54#define QPNP_INT_SET_TYPE_EOC_SET_INT_TYPE BIT(0)
55#define QPNP_INT_POLARITY_HIGH 0x12
56#define QPNP_INT_POLARITY_LOW 0x13
57#define QPNP_INT_EN_SET 0x15
58#define QPNP_INT_EN_SET_LOW_THR_INT_EN_SET BIT(4)
59#define QPNP_INT_EN_SET_HIGH_THR_INT_EN_SET BIT(3)
60#define QPNP_INT_EN_SET_CONV_SEQ_TIMEOUT_INT_EN BIT(2)
61#define QPNP_INT_EN_SET_FIFO_NOT_EMPTY_INT_EN BIT(1)
62#define QPNP_INT_EN_SET_EOC_INT_EN_SET BIT(0)
63#define QPNP_INT_CLR 0x16
64#define QPNP_INT_CLR_LOW_THR_INT_EN_CLR BIT(4)
65#define QPNP_INT_CLR_HIGH_THR_INT_EN_CLKR BIT(3)
66#define QPNP_INT_CLR_CONV_SEQ_TIMEOUT_INT_EN BIT(2)
67#define QPNP_INT_CLR_FIFO_NOT_EMPTY_INT_EN BIT(1)
68#define QPNP_INT_CLR_EOC_INT_EN_CLR BIT(0)
69#define QPNP_INT_CLR_MASK 0x1f
70#define QPNP_MODE_CTL 0x40
71#define QPNP_OP_MODE_SHIFT 4
72#define QPNP_USE_BMS_DATA BIT(4)
73#define QPNP_VADC_SYNCH_EN BIT(2)
74#define QPNP_OFFSET_RMV_EN BIT(1)
75#define QPNP_ADC_TRIM_EN BIT(0)
76#define QPNP_EN_CTL1 0x46
77#define QPNP_ADC_CH_SEL_CTL 0x48
78#define QPNP_ADC_DIG_PARAM 0x50
79#define QPNP_ADC_CLK_SEL_MASK 0x3
80#define QPNP_ADC_DEC_RATIO_SEL_MASK 0xc
81#define QPNP_ADC_DIG_DEC_RATIO_SEL_SHIFT 2
82
83#define QPNP_HW_SETTLE_DELAY 0x51
84#define QPNP_CONV_REQ 0x52
85#define QPNP_CONV_REQ_SET BIT(7)
86#define QPNP_CONV_SEQ_CTL 0x54
87#define QPNP_CONV_SEQ_HOLDOFF_SHIFT 4
88#define QPNP_CONV_SEQ_TRIG_CTL 0x55
89#define QPNP_FAST_AVG_CTL 0x5a
90
91#define QPNP_M0_LOW_THR_LSB 0x5c
92#define QPNP_M0_LOW_THR_MSB 0x5d
93#define QPNP_M0_HIGH_THR_LSB 0x5e
94#define QPNP_M0_HIGH_THR_MSB 0x5f
95#define QPNP_M1_LOW_THR_LSB 0x69
96#define QPNP_M1_LOW_THR_MSB 0x6a
97#define QPNP_M1_HIGH_THR_LSB 0x6b
98#define QPNP_M1_HIGH_THR_MSB 0x6c
99
100#define QPNP_DATA0 0x60
101#define QPNP_DATA1 0x61
102#define QPNP_CONV_TIMEOUT_ERR 2
103
104#define QPNP_IADC_MODE_CTL 0x40
105#define QPNP_IADC_USE_BMS_DATA BIT(4)
106#define QPNP_IADC_RESERVED_BIT3 BIT(3)
107#define QPNP_IADC_VADC_SYNC_EN BIT(2)
108#define QPNP_IADC_OFFSET_RMV_EN BIT(1)
109#define QPNP_IADC_ADC_TRIM_EN BIT(0)
110
111#define QPNP_IADC_ADC_CH_SEL_CTL 0x48
112#define QPNP_IADC_ADC_CHX_SEL_SHIFT 3
113
114#define QPNP_IADC_ADC_DIG_PARAM 0x50
115#define QPNP_IADC_CLK_SEL_SHIFT 1
116#define QPNP_IADC_DEC_RATIO_SEL 3
117
118#define QPNP_IADC_CONV_REQUEST 0x52
119#define QPNP_IADC_CONV_REQ BIT(7)
120
121#define QPNP_IADC_DATA0 0x60
122#define QPNP_IADC_DATA1 0x61
123
124#define QPNP_ADC_CONV_TIME_MIN 2000
125#define QPNP_ADC_CONV_TIME_MAX 2200
126
127#define QPNP_ADC_GAIN_CALCULATION 2500
128
129struct qpnp_iadc_drv {
130 struct qpnp_adc_drv *adc;
131 int32_t rsense;
132 struct device *iadc_hwmon;
133 bool iadc_init_calib;
134 struct sensor_device_attribute sens_attr[0];
135};
136
137struct qpnp_iadc_drv *qpnp_iadc;
138
139static int32_t qpnp_iadc_read_reg(uint32_t reg, u8 *data)
140{
141 struct qpnp_iadc_drv *iadc = qpnp_iadc;
142 int rc;
143
144 rc = spmi_ext_register_readl(iadc->adc->spmi->ctrl, iadc->adc->slave,
145 reg, data, 1);
146 if (rc < 0) {
147 pr_err("qpnp iadc read reg %d failed with %d\n", reg, rc);
148 return rc;
149 }
150
151 return 0;
152}
153
154static int32_t qpnp_iadc_write_reg(uint32_t reg, u8 data)
155{
156 struct qpnp_iadc_drv *iadc = qpnp_iadc;
157 int rc;
158 u8 *buf;
159
160 buf = &data;
161 rc = spmi_ext_register_writel(iadc->adc->spmi->ctrl, iadc->adc->slave,
162 reg, buf, 1);
163 if (rc < 0) {
164 pr_err("qpnp iadc write reg %d failed with %d\n", reg, rc);
165 return rc;
166 }
167
168 return 0;
169}
170
171static int32_t qpnp_iadc_configure_interrupt(void)
172{
173 int rc = 0;
174 u8 data = 0;
175
176 /* Configure interrupt as an Edge trigger */
177 rc = qpnp_iadc_write_reg(QPNP_INT_SET_TYPE,
178 QPNP_INT_CLR_MASK);
179 if (rc < 0) {
180 pr_err("%s Interrupt configure failed\n", __func__);
181 return rc;
182 }
183
184 /* Configure interrupt for rising edge trigger */
185 rc = qpnp_iadc_write_reg(QPNP_INT_POLARITY_HIGH,
186 QPNP_INT_CLR_MASK);
187 if (rc < 0) {
188 pr_err("%s Rising edge trigger configure failed\n", __func__);
189 return rc;
190 }
191
192 /* Disable low level interrupt triggering */
193 data = QPNP_INT_CLR_MASK;
194 rc = qpnp_iadc_write_reg(QPNP_INT_POLARITY_LOW,
195 (~data & QPNP_INT_CLR_MASK));
196 if (rc < 0) {
197 pr_err("%s Setting level low to disable failed\n", __func__);
198 return rc;
199 }
200
201 return 0;
202}
203
204static void trigger_iadc_completion(struct work_struct *work)
205{
206 struct qpnp_iadc_drv *iadc = qpnp_iadc;
207 int rc;
208
209 rc = qpnp_iadc_write_reg(QPNP_INT_CLR, QPNP_INT_CLR_MASK);
210 if (rc < 0)
211 pr_err("qpnp iadc interrupt mask failed with %d\n", rc);
212
213 complete(&iadc->adc->adc_rslt_completion);
214
215 return;
216}
217DECLARE_WORK(trigger_iadc_completion_work, trigger_iadc_completion);
218
219static irqreturn_t qpnp_iadc_isr(int irq, void *dev_id)
220{
221 schedule_work(&trigger_iadc_completion_work);
222
223 return IRQ_HANDLED;
224}
225
226static int32_t qpnp_iadc_read_conversion_result(int32_t *data)
227{
228 uint8_t rslt_lsb, rslt_msb;
229 int32_t rc;
230
231 rc = qpnp_iadc_read_reg(QPNP_IADC_DATA0, &rslt_lsb);
232 if (rc < 0) {
233 pr_err("qpnp adc result read failed with %d\n", rc);
234 return rc;
235 }
236
237 rc = qpnp_iadc_read_reg(QPNP_IADC_DATA1, &rslt_msb);
238 if (rc < 0) {
239 pr_err("qpnp adc result read failed with %d\n", rc);
240 return rc;
241 }
242
243 *data = (rslt_msb << 8) | rslt_lsb;
244
245 rc = qpnp_vadc_check_result(data);
246 if (rc < 0) {
247 pr_err("VADC data check failed\n");
248 return rc;
249 }
250
251 return 0;
252}
253
254static int32_t qpnp_iadc_configure(enum qpnp_iadc_channels channel,
255 int32_t *result)
256{
257 struct qpnp_iadc_drv *iadc = qpnp_iadc;
258 u8 qpnp_iadc_mode_reg = 0, qpnp_iadc_ch_sel_reg = 0;
259 u8 qpnp_iadc_conv_req = 0, qpnp_iadc_dig_param_reg = 0;
260 int32_t rc = 0;
261
262 qpnp_iadc_mode_reg |= (QPNP_IADC_USE_BMS_DATA | QPNP_IADC_USE_BMS_DATA
263 | QPNP_IADC_OFFSET_RMV_EN | QPNP_IADC_ADC_TRIM_EN);
264
265 qpnp_iadc_ch_sel_reg = channel << QPNP_IADC_ADC_CHX_SEL_SHIFT;
266
267 qpnp_iadc_dig_param_reg |= iadc->adc->amux_prop->decimation <<
268 QPNP_IADC_DEC_RATIO_SEL;
269
270 qpnp_iadc_conv_req = QPNP_IADC_CONV_REQ;
271
272 rc = qpnp_iadc_write_reg(QPNP_INT_EN_SET,
273 QPNP_INT_EN_SET_EOC_INT_EN_SET);
274 if (rc < 0) {
275 pr_err("qpnp adc configure error for interrupt setup\n");
276 return rc;
277 }
278
279 rc = qpnp_iadc_write_reg(QPNP_IADC_MODE_CTL, qpnp_iadc_mode_reg);
280 if (rc) {
281 pr_err("qpnp adc read adc failed with %d\n", rc);
282 return rc;
283 }
284
285 rc = qpnp_iadc_write_reg(QPNP_IADC_ADC_CH_SEL_CTL,
286 qpnp_iadc_ch_sel_reg);
287 if (rc) {
288 pr_err("qpnp adc read adc failed with %d\n", rc);
289 return rc;
290 }
291
292 rc = qpnp_iadc_write_reg(QPNP_ADC_DIG_PARAM,
293 qpnp_iadc_dig_param_reg);
294 if (rc) {
295 pr_err("qpnp adc read adc failed with %d\n", rc);
296 return rc;
297 }
298
299 rc = qpnp_iadc_write_reg(QPNP_HW_SETTLE_DELAY,
300 iadc->adc->amux_prop->hw_settle_time);
301 if (rc < 0) {
302 pr_err("qpnp adc configure error for hw settling time setup\n");
303 return rc;
304 }
305
306 rc = qpnp_iadc_write_reg(QPNP_FAST_AVG_CTL,
307 iadc->adc->amux_prop->fast_avg_setup);
308 if (rc < 0) {
309 pr_err("qpnp adc fast averaging configure error\n");
310 return rc;
311 }
312
313 rc = qpnp_iadc_write_reg(QPNP_CONV_REQ, qpnp_iadc_conv_req);
314 if (rc) {
315 pr_err("qpnp adc read adc failed with %d\n", rc);
316 return rc;
317 }
318
319 wait_for_completion(&iadc->adc->adc_rslt_completion);
320
321 rc = qpnp_iadc_read_conversion_result(result);
322 if (rc) {
323 pr_err("qpnp adc read adc failed with %d\n", rc);
324 return rc;
325 }
326
327 return 0;
328}
329
330static int32_t qpnp_iadc_init_calib(void)
331{
332 struct qpnp_iadc_drv *iadc = qpnp_iadc;
333 int32_t rc = 0, result;
334
335 rc = qpnp_iadc_configure(GAIN_CALIBRATION_25MV, &result);
336 if (rc < 0) {
337 pr_err("qpnp adc result read failed with %d\n", rc);
338 goto fail;
339 }
340
341 iadc->adc->calib.gain = result;
342
343 rc = qpnp_iadc_configure(OFFSET_CALIBRATION_SHORT_CADC_LEADS,
344 &result);
345 if (rc < 0) {
346 pr_err("qpnp adc result read failed with %d\n", rc);
347 goto fail;
348 }
349
350 iadc->adc->calib.offset = result;
351
352fail:
353 return rc;
354}
355
356int32_t qpnp_iadc_read(enum qpnp_iadc_channels channel,
357 int32_t *result)
358{
359 struct qpnp_iadc_drv *iadc = qpnp_iadc;
360 int32_t vsense_mv = 0, rc;
361
362 mutex_lock(&iadc->adc->adc_lock);
363
364 if (!iadc->iadc_init_calib) {
365 rc = qpnp_iadc_init_calib();
366 if (!rc) {
367 pr_err("Calibration failed\n");
368 goto fail;
369 } else
370 iadc->iadc_init_calib = true;
371 }
372
373 rc = qpnp_iadc_configure(channel, result);
374 if (rc < 0) {
375 pr_err("qpnp adc result read failed with %d\n", rc);
376 goto fail;
377 }
378
379 vsense_mv = ((*result - iadc->adc->calib.offset)/
380 (iadc->adc->calib.gain - iadc->adc->calib.offset))
381 * QPNP_ADC_GAIN_CALCULATION;
382
383 *result = (vsense_mv/qpnp_iadc->rsense);
384
385fail:
386 mutex_unlock(&iadc->adc->adc_lock);
387
388 return rc;
389}
390EXPORT_SYMBOL(qpnp_iadc_read);
391
392int32_t qpnp_iadc_get_gain(int32_t *result)
393{
394 return qpnp_iadc_read(GAIN_CALIBRATION_25MV, result);
395}
396EXPORT_SYMBOL(qpnp_iadc_get_gain);
397
398int32_t qpnp_iadc_get_offset(enum qpnp_iadc_channels channel,
399 int32_t *result)
400{
401 return qpnp_iadc_read(channel, result);
402}
403EXPORT_SYMBOL(qpnp_iadc_get_offset);
404
405static ssize_t qpnp_iadc_show(struct device *dev,
406 struct device_attribute *devattr, char *buf)
407{
408 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
409 int32_t result;
410 int rc = -1;
411
412 rc = qpnp_iadc_read(attr->index, &result);
413
414 if (rc)
415 return 0;
416
417 return snprintf(buf, QPNP_ADC_HWMON_NAME_LENGTH,
418 "Result:%d\n", result);
419}
420
421static struct sensor_device_attribute qpnp_adc_attr =
422 SENSOR_ATTR(NULL, S_IRUGO, qpnp_iadc_show, NULL, 0);
423
424static int32_t qpnp_iadc_init_hwmon(struct spmi_device *spmi)
425{
426 struct qpnp_iadc_drv *iadc = qpnp_iadc;
427 struct device_node *child;
428 struct device_node *node = spmi->dev.of_node;
429 int rc = 0, i = 0, channel;
430
431 for_each_child_of_node(node, child) {
432 channel = iadc->adc->adc_channels[i].channel_num;
433 qpnp_adc_attr.index = iadc->adc->adc_channels[i].channel_num;
434 qpnp_adc_attr.dev_attr.attr.name =
435 iadc->adc->adc_channels[i].name;
436 sysfs_attr_init(&iadc->sens_attr[i].dev_attr.attr);
437 memcpy(&iadc->sens_attr[i], &qpnp_adc_attr,
438 sizeof(qpnp_adc_attr));
439 rc = device_create_file(&spmi->dev,
440 &iadc->sens_attr[i].dev_attr);
441 if (rc) {
442 dev_err(&spmi->dev,
443 "device_create_file failed for dev %s\n",
444 iadc->adc->adc_channels[i].name);
445 goto hwmon_err_sens;
446 }
447 i++;
448 }
449
450 return 0;
451hwmon_err_sens:
452 pr_err("Init HWMON failed for qpnp_iadc with %d\n", rc);
453 return rc;
454}
455
456static int __devinit qpnp_iadc_probe(struct spmi_device *spmi)
457{
458 struct qpnp_iadc_drv *iadc;
459 struct qpnp_adc_drv *adc_qpnp;
460 struct device_node *node = spmi->dev.of_node;
461 struct device_node *child;
462 int rc, count_adc_channel_list = 0;
463
464 if (!node)
465 return -EINVAL;
466
467 if (qpnp_iadc) {
468 pr_err("IADC already in use\n");
469 return -EBUSY;
470 }
471
472 for_each_child_of_node(node, child)
473 count_adc_channel_list++;
474
475 if (!count_adc_channel_list) {
476 pr_err("No channel listing\n");
477 return -EINVAL;
478 }
479
480 iadc = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_iadc_drv) +
481 (sizeof(struct sensor_device_attribute) *
482 count_adc_channel_list), GFP_KERNEL);
483 if (!iadc) {
484 dev_err(&spmi->dev, "Unable to allocate memory\n");
485 return -ENOMEM;
486 }
487
488 adc_qpnp = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_adc_drv),
489 GFP_KERNEL);
490 if (!adc_qpnp) {
491 dev_err(&spmi->dev, "Unable to allocate memory\n");
492 return -ENOMEM;
493 }
494
495 iadc->adc = adc_qpnp;
496
497 rc = qpnp_adc_get_devicetree_data(spmi, iadc->adc);
498 if (rc) {
499 dev_err(&spmi->dev, "failed to read device tree\n");
500 return rc;
501 }
502
503 rc = of_property_read_u32(node, "qcom,rsense",
504 &iadc->rsense);
505 if (rc) {
506 pr_err("Invalid rsens reference property\n");
507 return -EINVAL;
508 }
509
510 rc = devm_request_irq(&spmi->dev, iadc->adc->adc_irq,
511 qpnp_iadc_isr,
512 IRQF_TRIGGER_RISING, "qpnp_iadc_interrupt", iadc);
513 if (rc) {
514 dev_err(&spmi->dev, "failed to request adc irq\n");
515 return rc;
516 } else
517 enable_irq_wake(iadc->adc->adc_irq);
518
519 iadc->iadc_init_calib = false;
520 dev_set_drvdata(&spmi->dev, iadc);
521 qpnp_iadc = iadc;
522
523 rc = qpnp_iadc_init_hwmon(spmi);
524 if (rc) {
525 dev_err(&spmi->dev, "failed to initialize qpnp hwmon adc\n");
526 return rc;
527 }
528 iadc->iadc_hwmon = hwmon_device_register(&iadc->adc->spmi->dev);
529
530 rc = qpnp_iadc_configure_interrupt();
531 if (rc) {
532 dev_err(&spmi->dev, "failed to configure interrupt");
533 return rc;
534 }
535
536 return 0;
537}
538
539static int __devexit qpnp_iadc_remove(struct spmi_device *spmi)
540{
541 struct qpnp_iadc_drv *iadc = dev_get_drvdata(&spmi->dev);
542 struct device_node *node = spmi->dev.of_node;
543 struct device_node *child;
544 int i = 0;
545
546 for_each_child_of_node(node, child) {
547 device_remove_file(&spmi->dev,
548 &iadc->sens_attr[i].dev_attr);
549 i++;
550 }
551 dev_set_drvdata(&spmi->dev, NULL);
552
553 return 0;
554}
555
556static const struct of_device_id qpnp_iadc_match_table[] = {
557 { .compatible = "qcom,qpnp-iadc",
558 },
559 {}
560};
561
562static struct spmi_driver qpnp_iadc_driver = {
563 .driver = {
564 .name = "qcom,qpnp-iadc",
565 .of_match_table = qpnp_iadc_match_table,
566 },
567 .probe = qpnp_iadc_probe,
568 .remove = qpnp_iadc_remove,
569};
570
571static int __init qpnp_iadc_init(void)
572{
573 return spmi_driver_register(&qpnp_iadc_driver);
574}
575module_init(qpnp_iadc_init);
576
577static void __exit qpnp_iadc_exit(void)
578{
579 spmi_driver_unregister(&qpnp_iadc_driver);
580}
581module_exit(qpnp_iadc_exit);
582
583MODULE_DESCRIPTION("QPNP PMIC current ADC driver");
584MODULE_LICENSE("GPL v2");