2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License v2
5 * Author: Arun R Murthy <arun.murthy@stericsson.com>
6 * Author: Daniel Willerud <daniel.willerud@stericsson.com>
7 * Author: Johan Palsson <johan.palsson@stericsson.com>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/interrupt.h>
13 #include <linux/spinlock.h>
14 #include <linux/delay.h>
15 #include <linux/platform_device.h>
16 #include <linux/completion.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
21 #include <linux/mfd/ab8500.h>
22 #include <linux/mfd/abx500.h>
23 #include <linux/mfd/ab8500/gpadc.h>
26 * GPADC register offsets
29 #define AB8500_GPADC_CTRL1_REG 0x00
30 #define AB8500_GPADC_CTRL2_REG 0x01
31 #define AB8500_GPADC_CTRL3_REG 0x02
32 #define AB8500_GPADC_AUTO_TIMER_REG 0x03
33 #define AB8500_GPADC_STAT_REG 0x04
34 #define AB8500_GPADC_MANDATAL_REG 0x05
35 #define AB8500_GPADC_MANDATAH_REG 0x06
36 #define AB8500_GPADC_AUTODATAL_REG 0x07
37 #define AB8500_GPADC_AUTODATAH_REG 0x08
38 #define AB8500_GPADC_MUX_CTRL_REG 0x09
41 * OTP register offsets
44 #define AB8500_GPADC_CAL_1 0x0F
45 #define AB8500_GPADC_CAL_2 0x10
46 #define AB8500_GPADC_CAL_3 0x11
47 #define AB8500_GPADC_CAL_4 0x12
48 #define AB8500_GPADC_CAL_5 0x13
49 #define AB8500_GPADC_CAL_6 0x14
50 #define AB8500_GPADC_CAL_7 0x15
53 #define EN_VINTCORE12 0x04
54 #define EN_VTVOUT 0x02
56 #define DIS_GPADC 0x00
57 #define SW_AVG_16 0x60
58 #define ADC_SW_CONV 0x04
60 #define BTEMP_PULL_UP 0x08
63 #define GPADC_BUSY 0x01
65 /* GPADC constants from AB8500 spec, UM0836 */
66 #define ADC_RESOLUTION 1024
67 #define ADC_CH_BTEMP_MIN 0
68 #define ADC_CH_BTEMP_MAX 1350
69 #define ADC_CH_DIETEMP_MIN 0
70 #define ADC_CH_DIETEMP_MAX 1350
71 #define ADC_CH_CHG_V_MIN 0
72 #define ADC_CH_CHG_V_MAX 20030
73 #define ADC_CH_ACCDET2_MIN 0
74 #define ADC_CH_ACCDET2_MAX 2500
75 #define ADC_CH_VBAT_MIN 2300
76 #define ADC_CH_VBAT_MAX 4800
77 #define ADC_CH_CHG_I_MIN 0
78 #define ADC_CH_CHG_I_MAX 1500
79 #define ADC_CH_BKBAT_MIN 0
80 #define ADC_CH_BKBAT_MAX 3200
82 /* This is used to not lose precision when dividing to get gain and offset */
83 #define CALIB_SCALE 1000
93 * struct adc_cal_data - Table for storing gain and offset for the calibrated
95 * @gain: Gain of the ADC channel
96 * @offset: Offset of the ADC channel
104 * struct ab8500_gpadc - AB8500 GPADC device information
105 * @chip_id ABB chip id
106 * @dev: pointer to the struct device
107 * @node: a list of AB8500 GPADCs, hence prepared for
109 * @ab8500_gpadc_complete: pointer to the struct completion, to indicate
110 * the completion of gpadc conversion
111 * @ab8500_gpadc_lock: structure of type mutex
112 * @regu: pointer to the struct regulator
113 * @irq: interrupt number that is used by gpadc
114 * @cal_data array of ADC calibration data structs
116 struct ab8500_gpadc
{
119 struct list_head node
;
120 struct completion ab8500_gpadc_complete
;
121 struct mutex ab8500_gpadc_lock
;
122 struct regulator
*regu
;
124 struct adc_cal_data cal_data
[NBR_CAL_INPUTS
];
127 static LIST_HEAD(ab8500_gpadc_list
);
130 * ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC
131 * (i.e. the first GPADC in the instance list)
133 struct ab8500_gpadc
*ab8500_gpadc_get(char *name
)
135 struct ab8500_gpadc
*gpadc
;
137 list_for_each_entry(gpadc
, &ab8500_gpadc_list
, node
) {
138 if (!strcmp(name
, dev_name(gpadc
->dev
)))
142 return ERR_PTR(-ENOENT
);
144 EXPORT_SYMBOL(ab8500_gpadc_get
);
146 static int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc
*gpadc
, u8 input
,
153 /* For some reason we don't have calibrated data */
154 if (!gpadc
->cal_data
[ADC_INPUT_VMAIN
].gain
) {
155 res
= ADC_CH_CHG_V_MIN
+ (ADC_CH_CHG_V_MAX
-
156 ADC_CH_CHG_V_MIN
) * ad_value
/
160 /* Here we can use the calibrated data */
161 res
= (int) (ad_value
* gpadc
->cal_data
[ADC_INPUT_VMAIN
].gain
+
162 gpadc
->cal_data
[ADC_INPUT_VMAIN
].offset
) / CALIB_SCALE
;
170 /* For some reason we don't have calibrated data */
171 if (!gpadc
->cal_data
[ADC_INPUT_BTEMP
].gain
) {
172 res
= ADC_CH_BTEMP_MIN
+ (ADC_CH_BTEMP_MAX
-
173 ADC_CH_BTEMP_MIN
) * ad_value
/
177 /* Here we can use the calibrated data */
178 res
= (int) (ad_value
* gpadc
->cal_data
[ADC_INPUT_BTEMP
].gain
+
179 gpadc
->cal_data
[ADC_INPUT_BTEMP
].offset
) / CALIB_SCALE
;
183 /* For some reason we don't have calibrated data */
184 if (!gpadc
->cal_data
[ADC_INPUT_VBAT
].gain
) {
185 res
= ADC_CH_VBAT_MIN
+ (ADC_CH_VBAT_MAX
-
186 ADC_CH_VBAT_MIN
) * ad_value
/
190 /* Here we can use the calibrated data */
191 res
= (int) (ad_value
* gpadc
->cal_data
[ADC_INPUT_VBAT
].gain
+
192 gpadc
->cal_data
[ADC_INPUT_VBAT
].offset
) / CALIB_SCALE
;
196 res
= ADC_CH_DIETEMP_MIN
+
197 (ADC_CH_DIETEMP_MAX
- ADC_CH_DIETEMP_MIN
) * ad_value
/
202 res
= ADC_CH_ACCDET2_MIN
+
203 (ADC_CH_ACCDET2_MAX
- ADC_CH_ACCDET2_MIN
) * ad_value
/
208 res
= ADC_CH_CHG_V_MIN
+
209 (ADC_CH_CHG_V_MAX
- ADC_CH_CHG_V_MIN
) * ad_value
/
215 res
= ADC_CH_CHG_I_MIN
+
216 (ADC_CH_CHG_I_MAX
- ADC_CH_CHG_I_MIN
) * ad_value
/
221 res
= ADC_CH_BKBAT_MIN
+
222 (ADC_CH_BKBAT_MAX
- ADC_CH_BKBAT_MIN
) * ad_value
/
228 "unknown channel, not possible to convert\n");
237 * ab8500_gpadc_convert() - gpadc conversion
238 * @input: analog input to be converted to digital data
240 * This function converts the selected analog i/p to digital
243 int ab8500_gpadc_convert(struct ab8500_gpadc
*gpadc
, u8 input
)
248 u8 val
, low_data
, high_data
;
253 mutex_lock(&gpadc
->ab8500_gpadc_lock
);
254 /* Enable VTVout LDO this is required for GPADC */
255 regulator_enable(gpadc
->regu
);
257 /* Check if ADC is not busy, lock and proceed */
259 ret
= abx500_get_register_interruptible(gpadc
->dev
,
260 AB8500_GPADC
, AB8500_GPADC_STAT_REG
, &val
);
263 if (!(val
& GPADC_BUSY
))
266 } while (++looplimit
< 10);
267 if (looplimit
>= 10 && (val
& GPADC_BUSY
)) {
268 dev_err(gpadc
->dev
, "gpadc_conversion: GPADC busy");
274 ret
= abx500_mask_and_set_register_interruptible(gpadc
->dev
,
275 AB8500_GPADC
, AB8500_GPADC_CTRL1_REG
, EN_GPADC
, EN_GPADC
);
277 dev_err(gpadc
->dev
, "gpadc_conversion: enable gpadc failed\n");
281 /* Select the input source and set average samples to 16 */
282 ret
= abx500_set_register_interruptible(gpadc
->dev
, AB8500_GPADC
,
283 AB8500_GPADC_CTRL2_REG
, (input
| SW_AVG_16
));
286 "gpadc_conversion: set avg samples failed\n");
291 * Enable ADC, buffering, select rising edge and enable ADC path
292 * charging current sense if it needed, ABB 3.0 needs some special
298 ret
= abx500_mask_and_set_register_interruptible(gpadc
->dev
,
299 AB8500_GPADC
, AB8500_GPADC_CTRL1_REG
,
304 if (gpadc
->chip_id
>= AB8500_CUT3P0
) {
305 /* Turn on btemp pull-up on ABB 3.0 */
306 ret
= abx500_mask_and_set_register_interruptible(
308 AB8500_GPADC
, AB8500_GPADC_CTRL1_REG
,
309 EN_BUF
| BTEMP_PULL_UP
,
310 EN_BUF
| BTEMP_PULL_UP
);
313 * Delay might be needed for ABB8500 cut 3.0, if not, remove
314 * when hardware will be availible
319 /* Intentional fallthrough */
321 ret
= abx500_mask_and_set_register_interruptible(gpadc
->dev
,
322 AB8500_GPADC
, AB8500_GPADC_CTRL1_REG
, EN_BUF
, EN_BUF
);
327 "gpadc_conversion: select falling edge failed\n");
331 ret
= abx500_mask_and_set_register_interruptible(gpadc
->dev
,
332 AB8500_GPADC
, AB8500_GPADC_CTRL1_REG
, ADC_SW_CONV
, ADC_SW_CONV
);
335 "gpadc_conversion: start s/w conversion failed\n");
338 /* wait for completion of conversion */
339 if (!wait_for_completion_timeout(&gpadc
->ab8500_gpadc_complete
, 2*HZ
)) {
341 "timeout: didn't receive GPADC conversion interrupt\n");
346 /* Read the converted RAW data */
347 ret
= abx500_get_register_interruptible(gpadc
->dev
, AB8500_GPADC
,
348 AB8500_GPADC_MANDATAL_REG
, &low_data
);
350 dev_err(gpadc
->dev
, "gpadc_conversion: read low data failed\n");
354 ret
= abx500_get_register_interruptible(gpadc
->dev
, AB8500_GPADC
,
355 AB8500_GPADC_MANDATAH_REG
, &high_data
);
358 "gpadc_conversion: read high data failed\n");
362 data
= (high_data
<< 8) | low_data
;
364 ret
= abx500_set_register_interruptible(gpadc
->dev
, AB8500_GPADC
,
365 AB8500_GPADC_CTRL1_REG
, DIS_GPADC
);
367 dev_err(gpadc
->dev
, "gpadc_conversion: disable gpadc failed\n");
370 /* Disable VTVout LDO this is required for GPADC */
371 regulator_disable(gpadc
->regu
);
372 mutex_unlock(&gpadc
->ab8500_gpadc_lock
);
373 ret
= ab8500_gpadc_ad_to_voltage(gpadc
, input
, data
);
378 * It has shown to be needed to turn off the GPADC if an error occurs,
379 * otherwise we might have problem when waiting for the busy bit in the
380 * GPADC status register to go low. In V1.1 there wait_for_completion
381 * seems to timeout when waiting for an interrupt.. Not seen in V2.0
383 (void) abx500_set_register_interruptible(gpadc
->dev
, AB8500_GPADC
,
384 AB8500_GPADC_CTRL1_REG
, DIS_GPADC
);
385 regulator_disable(gpadc
->regu
);
386 mutex_unlock(&gpadc
->ab8500_gpadc_lock
);
388 "gpadc_conversion: Failed to AD convert channel %d\n", input
);
391 EXPORT_SYMBOL(ab8500_gpadc_convert
);
394 * ab8500_bm_gpswadcconvend_handler() - isr for s/w gpadc conversion completion
396 * @data: pointer to the data passed during request irq
398 * This is a interrupt service routine for s/w gpadc conversion completion.
399 * Notifies the gpadc completion is completed and the converted raw value
400 * can be read from the registers.
401 * Returns IRQ status(IRQ_HANDLED)
403 static irqreturn_t
ab8500_bm_gpswadcconvend_handler(int irq
, void *_gpadc
)
405 struct ab8500_gpadc
*gpadc
= _gpadc
;
407 complete(&gpadc
->ab8500_gpadc_complete
);
412 static int otp_cal_regs
[] = {
422 static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc
*gpadc
)
425 int ret
[ARRAY_SIZE(otp_cal_regs
)];
426 u8 gpadc_cal
[ARRAY_SIZE(otp_cal_regs
)];
428 int vmain_high
, vmain_low
;
429 int btemp_high
, btemp_low
;
430 int vbat_high
, vbat_low
;
432 /* First we read all OTP registers and store the error code */
433 for (i
= 0; i
< ARRAY_SIZE(otp_cal_regs
); i
++) {
434 ret
[i
] = abx500_get_register_interruptible(gpadc
->dev
,
435 AB8500_OTP_EMUL
, otp_cal_regs
[i
], &gpadc_cal
[i
]);
437 dev_err(gpadc
->dev
, "%s: read otp reg 0x%02x failed\n",
438 __func__
, otp_cal_regs
[i
]);
442 * The ADC calibration data is stored in OTP registers.
443 * The layout of the calibration data is outlined below and a more
444 * detailed description can be found in UM0836
446 * vm_h/l = vmain_high/low
447 * bt_h/l = btemp_high/low
448 * vb_h/l = vbat_high/low
451 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
452 * |.......|.......|.......|.......|.......|.......|.......|.......
454 * |.......|.......|.......|.......|.......|.......|.......|.......
455 * | | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
456 * |.......|.......|.......|.......|.......|.......|.......|.......
457 * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
458 * |.......|.......|.......|.......|.......|.......|.......|.......
459 * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
460 * |.......|.......|.......|.......|.......|.......|.......|.......
461 * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
462 * |.......|.......|.......|.......|.......|.......|.......|.......
463 * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
464 * |.......|.......|.......|.......|.......|.......|.......|.......
465 * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
466 * |.......|.......|.......|.......|.......|.......|.......|.......
469 * Ideal output ADC codes corresponding to injected input voltages
470 * during manufacturing is:
472 * vmain_high: Vin = 19500mV / ADC ideal code = 997
473 * vmain_low: Vin = 315mV / ADC ideal code = 16
474 * btemp_high: Vin = 1300mV / ADC ideal code = 985
475 * btemp_low: Vin = 21mV / ADC ideal code = 16
476 * vbat_high: Vin = 4700mV / ADC ideal code = 982
477 * vbat_low: Vin = 2380mV / ADC ideal code = 33
480 /* Calculate gain and offset for VMAIN if all reads succeeded */
481 if (!(ret
[0] < 0 || ret
[1] < 0 || ret
[2] < 0)) {
482 vmain_high
= (((gpadc_cal
[0] & 0x03) << 8) |
483 ((gpadc_cal
[1] & 0x3F) << 2) |
484 ((gpadc_cal
[2] & 0xC0) >> 6));
486 vmain_low
= ((gpadc_cal
[2] & 0x3E) >> 1);
488 gpadc
->cal_data
[ADC_INPUT_VMAIN
].gain
= CALIB_SCALE
*
489 (19500 - 315) / (vmain_high
- vmain_low
);
491 gpadc
->cal_data
[ADC_INPUT_VMAIN
].offset
= CALIB_SCALE
* 19500 -
492 (CALIB_SCALE
* (19500 - 315) /
493 (vmain_high
- vmain_low
)) * vmain_high
;
495 gpadc
->cal_data
[ADC_INPUT_VMAIN
].gain
= 0;
498 /* Calculate gain and offset for BTEMP if all reads succeeded */
499 if (!(ret
[2] < 0 || ret
[3] < 0 || ret
[4] < 0)) {
500 btemp_high
= (((gpadc_cal
[2] & 0x01) << 9) |
501 (gpadc_cal
[3] << 1) |
502 ((gpadc_cal
[4] & 0x80) >> 7));
504 btemp_low
= ((gpadc_cal
[4] & 0x7C) >> 2);
506 gpadc
->cal_data
[ADC_INPUT_BTEMP
].gain
=
507 CALIB_SCALE
* (1300 - 21) / (btemp_high
- btemp_low
);
509 gpadc
->cal_data
[ADC_INPUT_BTEMP
].offset
= CALIB_SCALE
* 1300 -
510 (CALIB_SCALE
* (1300 - 21) /
511 (btemp_high
- btemp_low
)) * btemp_high
;
513 gpadc
->cal_data
[ADC_INPUT_BTEMP
].gain
= 0;
516 /* Calculate gain and offset for VBAT if all reads succeeded */
517 if (!(ret
[4] < 0 || ret
[5] < 0 || ret
[6] < 0)) {
518 vbat_high
= (((gpadc_cal
[4] & 0x03) << 8) | gpadc_cal
[5]);
519 vbat_low
= ((gpadc_cal
[6] & 0xFC) >> 2);
521 gpadc
->cal_data
[ADC_INPUT_VBAT
].gain
= CALIB_SCALE
*
522 (4700 - 2380) / (vbat_high
- vbat_low
);
524 gpadc
->cal_data
[ADC_INPUT_VBAT
].offset
= CALIB_SCALE
* 4700 -
525 (CALIB_SCALE
* (4700 - 2380) /
526 (vbat_high
- vbat_low
)) * vbat_high
;
528 gpadc
->cal_data
[ADC_INPUT_VBAT
].gain
= 0;
531 dev_dbg(gpadc
->dev
, "VMAIN gain %llu offset %llu\n",
532 gpadc
->cal_data
[ADC_INPUT_VMAIN
].gain
,
533 gpadc
->cal_data
[ADC_INPUT_VMAIN
].offset
);
535 dev_dbg(gpadc
->dev
, "BTEMP gain %llu offset %llu\n",
536 gpadc
->cal_data
[ADC_INPUT_BTEMP
].gain
,
537 gpadc
->cal_data
[ADC_INPUT_BTEMP
].offset
);
539 dev_dbg(gpadc
->dev
, "VBAT gain %llu offset %llu\n",
540 gpadc
->cal_data
[ADC_INPUT_VBAT
].gain
,
541 gpadc
->cal_data
[ADC_INPUT_VBAT
].offset
);
544 static int __devinit
ab8500_gpadc_probe(struct platform_device
*pdev
)
547 struct ab8500_gpadc
*gpadc
;
549 gpadc
= kzalloc(sizeof(struct ab8500_gpadc
), GFP_KERNEL
);
551 dev_err(&pdev
->dev
, "Error: No memory\n");
555 gpadc
->irq
= platform_get_irq_byname(pdev
, "SW_CONV_END");
556 if (gpadc
->irq
< 0) {
557 dev_err(gpadc
->dev
, "failed to get platform irq-%d\n",
563 gpadc
->dev
= &pdev
->dev
;
564 mutex_init(&gpadc
->ab8500_gpadc_lock
);
566 /* Initialize completion used to notify completion of conversion */
567 init_completion(&gpadc
->ab8500_gpadc_complete
);
569 /* Register interrupt - SwAdcComplete */
570 ret
= request_threaded_irq(gpadc
->irq
, NULL
,
571 ab8500_bm_gpswadcconvend_handler
,
572 IRQF_NO_SUSPEND
| IRQF_SHARED
, "ab8500-gpadc", gpadc
);
574 dev_err(gpadc
->dev
, "Failed to register interrupt, irq: %d\n",
579 /* Get Chip ID of the ABB ASIC */
580 ret
= abx500_get_chip_id(gpadc
->dev
);
582 dev_err(gpadc
->dev
, "failed to get chip ID\n");
585 gpadc
->chip_id
= (u8
) ret
;
587 /* VTVout LDO used to power up ab8500-GPADC */
588 gpadc
->regu
= regulator_get(&pdev
->dev
, "vddadc");
589 if (IS_ERR(gpadc
->regu
)) {
590 ret
= PTR_ERR(gpadc
->regu
);
591 dev_err(gpadc
->dev
, "failed to get vtvout LDO\n");
594 ab8500_gpadc_read_calibration_data(gpadc
);
595 list_add_tail(&gpadc
->node
, &ab8500_gpadc_list
);
596 dev_dbg(gpadc
->dev
, "probe success\n");
599 free_irq(gpadc
->irq
, gpadc
);
606 static int __devexit
ab8500_gpadc_remove(struct platform_device
*pdev
)
608 struct ab8500_gpadc
*gpadc
= platform_get_drvdata(pdev
);
610 /* remove this gpadc entry from the list */
611 list_del(&gpadc
->node
);
612 /* remove interrupt - completion of Sw ADC conversion */
613 free_irq(gpadc
->irq
, gpadc
);
614 /* disable VTVout LDO that is being used by GPADC */
615 regulator_put(gpadc
->regu
);
621 static struct platform_driver ab8500_gpadc_driver
= {
622 .probe
= ab8500_gpadc_probe
,
623 .remove
= __devexit_p(ab8500_gpadc_remove
),
625 .name
= "ab8500-gpadc",
626 .owner
= THIS_MODULE
,
630 static int __init
ab8500_gpadc_init(void)
632 return platform_driver_register(&ab8500_gpadc_driver
);
635 static void __exit
ab8500_gpadc_exit(void)
637 platform_driver_unregister(&ab8500_gpadc_driver
);
640 subsys_initcall_sync(ab8500_gpadc_init
);
641 module_exit(ab8500_gpadc_exit
);
643 MODULE_LICENSE("GPL v2");
644 MODULE_AUTHOR("Arun R Murthy, Daniel Willerud, Johan Palsson");
645 MODULE_ALIAS("platform:ab8500_gpadc");
646 MODULE_DESCRIPTION("AB8500 GPADC driver");