10 #include <linux/module.h>
11 #include <linux/device.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
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
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
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
83 #define CALIB_SCALE 1000
231 "unknown channel, not possible to convert\n");
254 dev_err(gpadc->
dev,
"GPADC raw value failed ch: %d\n", channel);
261 dev_err(gpadc->
dev,
"GPADC to voltage conversion failed ch:"
262 " %d AD: 0x%x\n", channel, ad_value);
279 u8 val, low_data, high_data;
297 }
while (++looplimit < 10);
299 dev_err(gpadc->
dev,
"gpadc_conversion: GPADC busy");
308 dev_err(gpadc->
dev,
"gpadc_conversion: enable gpadc failed\n");
317 "gpadc_conversion: set avg samples failed\n");
358 "gpadc_conversion: select falling edge failed\n");
366 "gpadc_conversion: start s/w conversion failed\n");
372 "timeout: didn't receive GPADC conversion interrupt\n");
381 dev_err(gpadc->
dev,
"gpadc_conversion: read low data failed\n");
389 "gpadc_conversion: read high data failed\n");
397 dev_err(gpadc->
dev,
"gpadc_conversion: disable gpadc failed\n");
404 return (high_data << 8) | low_data;
418 "gpadc_conversion: Failed to AD convert channel %d\n", channel);
433 static irqreturn_t ab8500_bm_gpswadcconvend_handler(
int irq,
void *_gpadc)
442 static int otp_cal_regs[] = {
452 static void ab8500_gpadc_read_calibration_data(
struct ab8500_gpadc *gpadc)
458 int vmain_high, vmain_low;
459 int btemp_high, btemp_low;
460 int vbat_high, vbat_low;
463 for (i = 0; i <
ARRAY_SIZE(otp_cal_regs); i++) {
467 dev_err(gpadc->
dev,
"%s: read otp reg 0x%02x failed\n",
468 __func__, otp_cal_regs[i]);
511 if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
512 vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
513 ((gpadc_cal[1] & 0x3F) << 2) |
514 ((gpadc_cal[2] & 0xC0) >> 6));
516 vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);
519 (19500 - 315) / (vmain_high - vmain_low);
523 (vmain_high - vmain_low)) * vmain_high;
529 if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
530 btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
531 (gpadc_cal[3] << 1) |
532 ((gpadc_cal[4] & 0x80) >> 7));
534 btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);
537 CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);
541 (btemp_high - btemp_low)) * btemp_high;
547 if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
548 vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
549 vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);
552 (4700 - 2380) / (vbat_high - vbat_low);
556 (vbat_high - vbat_low)) * vbat_high;
561 dev_dbg(gpadc->
dev,
"VMAIN gain %llu offset %llu\n",
565 dev_dbg(gpadc->
dev,
"BTEMP gain %llu offset %llu\n",
569 dev_dbg(gpadc->
dev,
"VBAT gain %llu offset %llu\n",
586 if (gpadc->
irq < 0) {
587 dev_err(&pdev->
dev,
"failed to get platform irq-%d\n",
601 ab8500_bm_gpswadcconvend_handler,
603 "ab8500-gpadc", gpadc);
605 dev_err(gpadc->
dev,
"Failed to register interrupt, irq: %d\n",
613 dev_err(gpadc->
dev,
"failed to get chip ID\n");
620 if (IS_ERR(gpadc->
regu)) {
621 ret = PTR_ERR(gpadc->
regu);
622 dev_err(gpadc->
dev,
"failed to get vtvout LDO\n");
625 ab8500_gpadc_read_calibration_data(gpadc);
639 struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);
653 .probe = ab8500_gpadc_probe,
656 .name =
"ab8500-gpadc",
661 static int __init ab8500_gpadc_init(
void)
666 static void __exit ab8500_gpadc_exit(
void)
675 MODULE_AUTHOR(
"Arun R Murthy, Daniel Willerud, Johan Palsson");