11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
225ec2496STony Lindgren /*
325ec2496STony Lindgren * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
425ec2496STony Lindgren *
525ec2496STony Lindgren * Rewritten for Linux IIO framework with some code based on
625ec2496STony Lindgren * earlier driver found in the Motorola Linux kernel:
725ec2496STony Lindgren *
825ec2496STony Lindgren * Copyright (C) 2009-2010 Motorola, Inc.
925ec2496STony Lindgren */
1025ec2496STony Lindgren
1125ec2496STony Lindgren #include <linux/delay.h>
1225ec2496STony Lindgren #include <linux/device.h>
1325ec2496STony Lindgren #include <linux/err.h>
1425ec2496STony Lindgren #include <linux/init.h>
1525ec2496STony Lindgren #include <linux/interrupt.h>
1625ec2496STony Lindgren #include <linux/kernel.h>
1725ec2496STony Lindgren #include <linux/module.h>
1829788fd6SJonathan Cameron #include <linux/mod_devicetable.h>
1925ec2496STony Lindgren #include <linux/platform_device.h>
2029788fd6SJonathan Cameron #include <linux/property.h>
2125ec2496STony Lindgren #include <linux/regmap.h>
2225ec2496STony Lindgren
2325ec2496STony Lindgren #include <linux/iio/buffer.h>
2425ec2496STony Lindgren #include <linux/iio/driver.h>
2525ec2496STony Lindgren #include <linux/iio/iio.h>
2625ec2496STony Lindgren #include <linux/iio/kfifo_buf.h>
2725ec2496STony Lindgren #include <linux/mfd/motorola-cpcap.h>
2825ec2496STony Lindgren
2925ec2496STony Lindgren /* Register CPCAP_REG_ADCC1 bits */
3025ec2496STony Lindgren #define CPCAP_BIT_ADEN_AUTO_CLR BIT(15) /* Currently unused */
3125ec2496STony Lindgren #define CPCAP_BIT_CAL_MODE BIT(14) /* Set with BIT_RAND0 */
3225ec2496STony Lindgren #define CPCAP_BIT_ADC_CLK_SEL1 BIT(13) /* Currently unused */
3325ec2496STony Lindgren #define CPCAP_BIT_ADC_CLK_SEL0 BIT(12) /* Currently unused */
3425ec2496STony Lindgren #define CPCAP_BIT_ATOX BIT(11)
3525ec2496STony Lindgren #define CPCAP_BIT_ATO3 BIT(10)
3625ec2496STony Lindgren #define CPCAP_BIT_ATO2 BIT(9)
3725ec2496STony Lindgren #define CPCAP_BIT_ATO1 BIT(8)
3825ec2496STony Lindgren #define CPCAP_BIT_ATO0 BIT(7)
3925ec2496STony Lindgren #define CPCAP_BIT_ADA2 BIT(6)
4025ec2496STony Lindgren #define CPCAP_BIT_ADA1 BIT(5)
4125ec2496STony Lindgren #define CPCAP_BIT_ADA0 BIT(4)
4225ec2496STony Lindgren #define CPCAP_BIT_AD_SEL1 BIT(3) /* Set for bank1 */
4325ec2496STony Lindgren #define CPCAP_BIT_RAND1 BIT(2) /* Set for channel 16 & 17 */
4425ec2496STony Lindgren #define CPCAP_BIT_RAND0 BIT(1) /* Set with CAL_MODE */
4525ec2496STony Lindgren #define CPCAP_BIT_ADEN BIT(0) /* Currently unused */
4625ec2496STony Lindgren
473f9f3a1aSTony Lindgren #define CPCAP_REG_ADCC1_DEFAULTS (CPCAP_BIT_ADEN_AUTO_CLR | \
483f9f3a1aSTony Lindgren CPCAP_BIT_ADC_CLK_SEL0 | \
493f9f3a1aSTony Lindgren CPCAP_BIT_RAND1)
503f9f3a1aSTony Lindgren
5125ec2496STony Lindgren /* Register CPCAP_REG_ADCC2 bits */
5225ec2496STony Lindgren #define CPCAP_BIT_CAL_FACTOR_ENABLE BIT(15) /* Currently unused */
5325ec2496STony Lindgren #define CPCAP_BIT_BATDETB_EN BIT(14) /* Currently unused */
5425ec2496STony Lindgren #define CPCAP_BIT_ADTRIG_ONESHOT BIT(13) /* Set for !TIMING_IMM */
5525ec2496STony Lindgren #define CPCAP_BIT_ASC BIT(12) /* Set for TIMING_IMM */
5625ec2496STony Lindgren #define CPCAP_BIT_ATOX_PS_FACTOR BIT(11)
5725ec2496STony Lindgren #define CPCAP_BIT_ADC_PS_FACTOR1 BIT(10)
5825ec2496STony Lindgren #define CPCAP_BIT_ADC_PS_FACTOR0 BIT(9)
5925ec2496STony Lindgren #define CPCAP_BIT_AD4_SELECT BIT(8) /* Currently unused */
6025ec2496STony Lindgren #define CPCAP_BIT_ADC_BUSY BIT(7) /* Currently unused */
613f9f3a1aSTony Lindgren #define CPCAP_BIT_THERMBIAS_EN BIT(6) /* Bias for AD0_BATTDETB */
6225ec2496STony Lindgren #define CPCAP_BIT_ADTRIG_DIS BIT(5) /* Disable interrupt */
6325ec2496STony Lindgren #define CPCAP_BIT_LIADC BIT(4) /* Currently unused */
6425ec2496STony Lindgren #define CPCAP_BIT_TS_REFEN BIT(3) /* Currently unused */
6525ec2496STony Lindgren #define CPCAP_BIT_TS_M2 BIT(2) /* Currently unused */
6625ec2496STony Lindgren #define CPCAP_BIT_TS_M1 BIT(1) /* Currently unused */
6725ec2496STony Lindgren #define CPCAP_BIT_TS_M0 BIT(0) /* Currently unused */
6825ec2496STony Lindgren
693f9f3a1aSTony Lindgren #define CPCAP_REG_ADCC2_DEFAULTS (CPCAP_BIT_AD4_SELECT | \
703f9f3a1aSTony Lindgren CPCAP_BIT_ADTRIG_DIS | \
713f9f3a1aSTony Lindgren CPCAP_BIT_LIADC | \
723f9f3a1aSTony Lindgren CPCAP_BIT_TS_M2 | \
733f9f3a1aSTony Lindgren CPCAP_BIT_TS_M1)
743f9f3a1aSTony Lindgren
7525ec2496STony Lindgren #define CPCAP_MAX_TEMP_LVL 27
7625ec2496STony Lindgren #define CPCAP_FOUR_POINT_TWO_ADC 801
7725ec2496STony Lindgren #define ST_ADC_CAL_CHRGI_HIGH_THRESHOLD 530
7825ec2496STony Lindgren #define ST_ADC_CAL_CHRGI_LOW_THRESHOLD 494
7925ec2496STony Lindgren #define ST_ADC_CAL_BATTI_HIGH_THRESHOLD 530
8025ec2496STony Lindgren #define ST_ADC_CAL_BATTI_LOW_THRESHOLD 494
8125ec2496STony Lindgren #define ST_ADC_CALIBRATE_DIFF_THRESHOLD 3
8225ec2496STony Lindgren
839d965236STony Lindgren #define CPCAP_ADC_MAX_RETRIES 5 /* Calibration */
8425ec2496STony Lindgren
8595d61a67SLee Jones /*
8625ec2496STony Lindgren * struct cpcap_adc_ato - timing settings for cpcap adc
8725ec2496STony Lindgren *
8825ec2496STony Lindgren * Unfortunately no cpcap documentation available, please document when
8925ec2496STony Lindgren * using these.
9025ec2496STony Lindgren */
9125ec2496STony Lindgren struct cpcap_adc_ato {
9225ec2496STony Lindgren unsigned short ato_in;
9325ec2496STony Lindgren unsigned short atox_in;
9425ec2496STony Lindgren unsigned short adc_ps_factor_in;
9525ec2496STony Lindgren unsigned short atox_ps_factor_in;
9625ec2496STony Lindgren unsigned short ato_out;
9725ec2496STony Lindgren unsigned short atox_out;
9825ec2496STony Lindgren unsigned short adc_ps_factor_out;
9925ec2496STony Lindgren unsigned short atox_ps_factor_out;
10025ec2496STony Lindgren };
10125ec2496STony Lindgren
10225ec2496STony Lindgren /**
103a80aeec0SJonathan Cameron * struct cpcap_adc - cpcap adc device driver data
10425ec2496STony Lindgren * @reg: cpcap regmap
10525ec2496STony Lindgren * @dev: struct device
10625ec2496STony Lindgren * @vendor: cpcap vendor
10725ec2496STony Lindgren * @irq: interrupt
10825ec2496STony Lindgren * @lock: mutex
10925ec2496STony Lindgren * @ato: request timings
11025ec2496STony Lindgren * @wq_data_avail: work queue
11125ec2496STony Lindgren * @done: work done
11225ec2496STony Lindgren */
11325ec2496STony Lindgren struct cpcap_adc {
11425ec2496STony Lindgren struct regmap *reg;
11525ec2496STony Lindgren struct device *dev;
11625ec2496STony Lindgren u16 vendor;
11725ec2496STony Lindgren int irq;
11825ec2496STony Lindgren struct mutex lock; /* ADC register access lock */
11925ec2496STony Lindgren const struct cpcap_adc_ato *ato;
12025ec2496STony Lindgren wait_queue_head_t wq_data_avail;
12125ec2496STony Lindgren bool done;
12225ec2496STony Lindgren };
12325ec2496STony Lindgren
12495d61a67SLee Jones /*
12525ec2496STony Lindgren * enum cpcap_adc_channel - cpcap adc channels
12625ec2496STony Lindgren */
12725ec2496STony Lindgren enum cpcap_adc_channel {
12825ec2496STony Lindgren /* Bank0 channels */
1293f9f3a1aSTony Lindgren CPCAP_ADC_AD0, /* Battery temperature */
13025ec2496STony Lindgren CPCAP_ADC_BATTP, /* Battery voltage */
13125ec2496STony Lindgren CPCAP_ADC_VBUS, /* USB VBUS voltage */
1323f9f3a1aSTony Lindgren CPCAP_ADC_AD3, /* Die temperature when charging */
13325ec2496STony Lindgren CPCAP_ADC_BPLUS_AD4, /* Another battery or system voltage */
13425ec2496STony Lindgren CPCAP_ADC_CHG_ISENSE, /* Calibrated charge current */
13525ec2496STony Lindgren CPCAP_ADC_BATTI, /* Calibrated system current */
13625ec2496STony Lindgren CPCAP_ADC_USB_ID, /* USB OTG ID, unused on droid 4? */
13725ec2496STony Lindgren
13825ec2496STony Lindgren /* Bank1 channels */
13925ec2496STony Lindgren CPCAP_ADC_AD8, /* Seems unused */
14025ec2496STony Lindgren CPCAP_ADC_AD9, /* Seems unused */
14125ec2496STony Lindgren CPCAP_ADC_LICELL, /* Maybe system voltage? Always 3V */
14225ec2496STony Lindgren CPCAP_ADC_HV_BATTP, /* Another battery detection? */
14325ec2496STony Lindgren CPCAP_ADC_TSX1_AD12, /* Seems unused, for touchscreen? */
14425ec2496STony Lindgren CPCAP_ADC_TSX2_AD13, /* Seems unused, for touchscreen? */
14525ec2496STony Lindgren CPCAP_ADC_TSY1_AD14, /* Seems unused, for touchscreen? */
14625ec2496STony Lindgren CPCAP_ADC_TSY2_AD15, /* Seems unused, for touchscreen? */
14725ec2496STony Lindgren
14825ec2496STony Lindgren /* Remuxed channels using bank0 entries */
14925ec2496STony Lindgren CPCAP_ADC_BATTP_PI16, /* Alternative mux mode for BATTP */
15025ec2496STony Lindgren CPCAP_ADC_BATTI_PI17, /* Alternative mux mode for BATTI */
15125ec2496STony Lindgren
15225ec2496STony Lindgren CPCAP_ADC_CHANNEL_NUM,
15325ec2496STony Lindgren };
15425ec2496STony Lindgren
15595d61a67SLee Jones /*
15625ec2496STony Lindgren * enum cpcap_adc_timing - cpcap adc timing options
15725ec2496STony Lindgren *
15825ec2496STony Lindgren * CPCAP_ADC_TIMING_IMM seems to be immediate with no timings.
15925ec2496STony Lindgren * Please document when using.
16025ec2496STony Lindgren */
16125ec2496STony Lindgren enum cpcap_adc_timing {
16225ec2496STony Lindgren CPCAP_ADC_TIMING_IMM,
16325ec2496STony Lindgren CPCAP_ADC_TIMING_IN,
16425ec2496STony Lindgren CPCAP_ADC_TIMING_OUT,
16525ec2496STony Lindgren };
16625ec2496STony Lindgren
16725ec2496STony Lindgren /**
16825ec2496STony Lindgren * struct cpcap_adc_phasing_tbl - cpcap phasing table
16925ec2496STony Lindgren * @offset: offset in the phasing table
17025ec2496STony Lindgren * @multiplier: multiplier in the phasing table
17125ec2496STony Lindgren * @divider: divider in the phasing table
17225ec2496STony Lindgren * @min: minimum value
17325ec2496STony Lindgren * @max: maximum value
17425ec2496STony Lindgren */
17525ec2496STony Lindgren struct cpcap_adc_phasing_tbl {
17625ec2496STony Lindgren short offset;
17725ec2496STony Lindgren unsigned short multiplier;
17825ec2496STony Lindgren unsigned short divider;
17925ec2496STony Lindgren short min;
18025ec2496STony Lindgren short max;
18125ec2496STony Lindgren };
18225ec2496STony Lindgren
18325ec2496STony Lindgren /**
18425ec2496STony Lindgren * struct cpcap_adc_conversion_tbl - cpcap conversion table
18525ec2496STony Lindgren * @conv_type: conversion type
18625ec2496STony Lindgren * @align_offset: align offset
18725ec2496STony Lindgren * @conv_offset: conversion offset
18825ec2496STony Lindgren * @cal_offset: calibration offset
18925ec2496STony Lindgren * @multiplier: conversion multiplier
19025ec2496STony Lindgren * @divider: conversion divider
19125ec2496STony Lindgren */
19225ec2496STony Lindgren struct cpcap_adc_conversion_tbl {
19325ec2496STony Lindgren enum iio_chan_info_enum conv_type;
19425ec2496STony Lindgren int align_offset;
19525ec2496STony Lindgren int conv_offset;
19625ec2496STony Lindgren int cal_offset;
19725ec2496STony Lindgren int multiplier;
19825ec2496STony Lindgren int divider;
19925ec2496STony Lindgren };
20025ec2496STony Lindgren
20125ec2496STony Lindgren /**
20225ec2496STony Lindgren * struct cpcap_adc_request - cpcap adc request
20325ec2496STony Lindgren * @channel: request channel
20425ec2496STony Lindgren * @phase_tbl: channel phasing table
20525ec2496STony Lindgren * @conv_tbl: channel conversion table
20625ec2496STony Lindgren * @bank_index: channel index within the bank
20725ec2496STony Lindgren * @timing: timing settings
20825ec2496STony Lindgren * @result: result
20925ec2496STony Lindgren */
21025ec2496STony Lindgren struct cpcap_adc_request {
21125ec2496STony Lindgren int channel;
21225ec2496STony Lindgren const struct cpcap_adc_phasing_tbl *phase_tbl;
21325ec2496STony Lindgren const struct cpcap_adc_conversion_tbl *conv_tbl;
21425ec2496STony Lindgren int bank_index;
21525ec2496STony Lindgren enum cpcap_adc_timing timing;
21625ec2496STony Lindgren int result;
21725ec2496STony Lindgren };
21825ec2496STony Lindgren
21925ec2496STony Lindgren /* Phasing table for channels. Note that channels 16 & 17 use BATTP and BATTI */
22025ec2496STony Lindgren static const struct cpcap_adc_phasing_tbl bank_phasing[] = {
22125ec2496STony Lindgren /* Bank0 */
2223f9f3a1aSTony Lindgren [CPCAP_ADC_AD0] = {0, 0x80, 0x80, 0, 1023},
22325ec2496STony Lindgren [CPCAP_ADC_BATTP] = {0, 0x80, 0x80, 0, 1023},
22425ec2496STony Lindgren [CPCAP_ADC_VBUS] = {0, 0x80, 0x80, 0, 1023},
22525ec2496STony Lindgren [CPCAP_ADC_AD3] = {0, 0x80, 0x80, 0, 1023},
22625ec2496STony Lindgren [CPCAP_ADC_BPLUS_AD4] = {0, 0x80, 0x80, 0, 1023},
22725ec2496STony Lindgren [CPCAP_ADC_CHG_ISENSE] = {0, 0x80, 0x80, -512, 511},
22825ec2496STony Lindgren [CPCAP_ADC_BATTI] = {0, 0x80, 0x80, -512, 511},
22925ec2496STony Lindgren [CPCAP_ADC_USB_ID] = {0, 0x80, 0x80, 0, 1023},
23025ec2496STony Lindgren
23125ec2496STony Lindgren /* Bank1 */
23225ec2496STony Lindgren [CPCAP_ADC_AD8] = {0, 0x80, 0x80, 0, 1023},
23325ec2496STony Lindgren [CPCAP_ADC_AD9] = {0, 0x80, 0x80, 0, 1023},
23425ec2496STony Lindgren [CPCAP_ADC_LICELL] = {0, 0x80, 0x80, 0, 1023},
23525ec2496STony Lindgren [CPCAP_ADC_HV_BATTP] = {0, 0x80, 0x80, 0, 1023},
23625ec2496STony Lindgren [CPCAP_ADC_TSX1_AD12] = {0, 0x80, 0x80, 0, 1023},
23725ec2496STony Lindgren [CPCAP_ADC_TSX2_AD13] = {0, 0x80, 0x80, 0, 1023},
23825ec2496STony Lindgren [CPCAP_ADC_TSY1_AD14] = {0, 0x80, 0x80, 0, 1023},
23925ec2496STony Lindgren [CPCAP_ADC_TSY2_AD15] = {0, 0x80, 0x80, 0, 1023},
24025ec2496STony Lindgren };
24125ec2496STony Lindgren
24225ec2496STony Lindgren /*
24325ec2496STony Lindgren * Conversion table for channels. Updated during init based on calibration.
24425ec2496STony Lindgren * Here too channels 16 & 17 use BATTP and BATTI.
24525ec2496STony Lindgren */
24625ec2496STony Lindgren static struct cpcap_adc_conversion_tbl bank_conversion[] = {
24725ec2496STony Lindgren /* Bank0 */
2483f9f3a1aSTony Lindgren [CPCAP_ADC_AD0] = {
24925ec2496STony Lindgren IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 1, 1,
25025ec2496STony Lindgren },
25125ec2496STony Lindgren [CPCAP_ADC_BATTP] = {
25225ec2496STony Lindgren IIO_CHAN_INFO_PROCESSED, 0, 2400, 0, 2300, 1023,
25325ec2496STony Lindgren },
25425ec2496STony Lindgren [CPCAP_ADC_VBUS] = {
25525ec2496STony Lindgren IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 10000, 1023,
25625ec2496STony Lindgren },
25725ec2496STony Lindgren [CPCAP_ADC_AD3] = {
25825ec2496STony Lindgren IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 1, 1,
25925ec2496STony Lindgren },
26025ec2496STony Lindgren [CPCAP_ADC_BPLUS_AD4] = {
26125ec2496STony Lindgren IIO_CHAN_INFO_PROCESSED, 0, 2400, 0, 2300, 1023,
26225ec2496STony Lindgren },
26325ec2496STony Lindgren [CPCAP_ADC_CHG_ISENSE] = {
26425ec2496STony Lindgren IIO_CHAN_INFO_PROCESSED, -512, 2, 0, 5000, 1023,
26525ec2496STony Lindgren },
26625ec2496STony Lindgren [CPCAP_ADC_BATTI] = {
26725ec2496STony Lindgren IIO_CHAN_INFO_PROCESSED, -512, 2, 0, 5000, 1023,
26825ec2496STony Lindgren },
26925ec2496STony Lindgren [CPCAP_ADC_USB_ID] = {
27025ec2496STony Lindgren IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
27125ec2496STony Lindgren },
27225ec2496STony Lindgren
27325ec2496STony Lindgren /* Bank1 */
27425ec2496STony Lindgren [CPCAP_ADC_AD8] = {
27525ec2496STony Lindgren IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
27625ec2496STony Lindgren },
27725ec2496STony Lindgren [CPCAP_ADC_AD9] = {
27825ec2496STony Lindgren IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
27925ec2496STony Lindgren },
28025ec2496STony Lindgren [CPCAP_ADC_LICELL] = {
28125ec2496STony Lindgren IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 3400, 1023,
28225ec2496STony Lindgren },
28325ec2496STony Lindgren [CPCAP_ADC_HV_BATTP] = {
28425ec2496STony Lindgren IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
28525ec2496STony Lindgren },
28625ec2496STony Lindgren [CPCAP_ADC_TSX1_AD12] = {
28725ec2496STony Lindgren IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
28825ec2496STony Lindgren },
28925ec2496STony Lindgren [CPCAP_ADC_TSX2_AD13] = {
29025ec2496STony Lindgren IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
29125ec2496STony Lindgren },
29225ec2496STony Lindgren [CPCAP_ADC_TSY1_AD14] = {
29325ec2496STony Lindgren IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
29425ec2496STony Lindgren },
29525ec2496STony Lindgren [CPCAP_ADC_TSY2_AD15] = {
29625ec2496STony Lindgren IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
29725ec2496STony Lindgren },
29825ec2496STony Lindgren };
29925ec2496STony Lindgren
30025ec2496STony Lindgren /*
30125ec2496STony Lindgren * Temperature lookup table of register values to milliCelcius.
30225ec2496STony Lindgren * REVISIT: Check the duplicate 0x3ff entry in a freezer
30325ec2496STony Lindgren */
30425ec2496STony Lindgren static const int temp_map[CPCAP_MAX_TEMP_LVL][2] = {
30525ec2496STony Lindgren { 0x03ff, -40000 },
30625ec2496STony Lindgren { 0x03ff, -35000 },
30725ec2496STony Lindgren { 0x03ef, -30000 },
30825ec2496STony Lindgren { 0x03b2, -25000 },
30925ec2496STony Lindgren { 0x036c, -20000 },
31025ec2496STony Lindgren { 0x0320, -15000 },
31125ec2496STony Lindgren { 0x02d0, -10000 },
31225ec2496STony Lindgren { 0x027f, -5000 },
31325ec2496STony Lindgren { 0x022f, 0 },
31425ec2496STony Lindgren { 0x01e4, 5000 },
31525ec2496STony Lindgren { 0x019f, 10000 },
31625ec2496STony Lindgren { 0x0161, 15000 },
31725ec2496STony Lindgren { 0x012b, 20000 },
31825ec2496STony Lindgren { 0x00fc, 25000 },
31925ec2496STony Lindgren { 0x00d4, 30000 },
32025ec2496STony Lindgren { 0x00b2, 35000 },
32125ec2496STony Lindgren { 0x0095, 40000 },
32225ec2496STony Lindgren { 0x007d, 45000 },
32325ec2496STony Lindgren { 0x0069, 50000 },
32425ec2496STony Lindgren { 0x0059, 55000 },
32525ec2496STony Lindgren { 0x004b, 60000 },
32625ec2496STony Lindgren { 0x003f, 65000 },
32725ec2496STony Lindgren { 0x0036, 70000 },
32825ec2496STony Lindgren { 0x002e, 75000 },
32925ec2496STony Lindgren { 0x0027, 80000 },
33025ec2496STony Lindgren { 0x0022, 85000 },
33125ec2496STony Lindgren { 0x001d, 90000 },
33225ec2496STony Lindgren };
33325ec2496STony Lindgren
33425ec2496STony Lindgren #define CPCAP_CHAN(_type, _index, _address, _datasheet_name) { \
33525ec2496STony Lindgren .type = (_type), \
33625ec2496STony Lindgren .address = (_address), \
33725ec2496STony Lindgren .indexed = 1, \
33825ec2496STony Lindgren .channel = (_index), \
33925ec2496STony Lindgren .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
34025ec2496STony Lindgren BIT(IIO_CHAN_INFO_PROCESSED), \
34125ec2496STony Lindgren .scan_index = (_index), \
34225ec2496STony Lindgren .scan_type = { \
34325ec2496STony Lindgren .sign = 'u', \
34425ec2496STony Lindgren .realbits = 10, \
34525ec2496STony Lindgren .storagebits = 16, \
34625ec2496STony Lindgren .endianness = IIO_CPU, \
34725ec2496STony Lindgren }, \
34825ec2496STony Lindgren .datasheet_name = (_datasheet_name), \
34925ec2496STony Lindgren }
35025ec2496STony Lindgren
35125ec2496STony Lindgren /*
35225ec2496STony Lindgren * The datasheet names are from Motorola mapphone Linux kernel except
35325ec2496STony Lindgren * for the last two which might be uncalibrated charge voltage and
35425ec2496STony Lindgren * current.
35525ec2496STony Lindgren */
35625ec2496STony Lindgren static const struct iio_chan_spec cpcap_adc_channels[] = {
35725ec2496STony Lindgren /* Bank0 */
35825ec2496STony Lindgren CPCAP_CHAN(IIO_TEMP, 0, CPCAP_REG_ADCD0, "battdetb"),
35925ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 1, CPCAP_REG_ADCD1, "battp"),
36025ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 2, CPCAP_REG_ADCD2, "vbus"),
36125ec2496STony Lindgren CPCAP_CHAN(IIO_TEMP, 3, CPCAP_REG_ADCD3, "ad3"),
36225ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 4, CPCAP_REG_ADCD4, "ad4"),
36325ec2496STony Lindgren CPCAP_CHAN(IIO_CURRENT, 5, CPCAP_REG_ADCD5, "chg_isense"),
36425ec2496STony Lindgren CPCAP_CHAN(IIO_CURRENT, 6, CPCAP_REG_ADCD6, "batti"),
36525ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 7, CPCAP_REG_ADCD7, "usb_id"),
36625ec2496STony Lindgren
36725ec2496STony Lindgren /* Bank1 */
36825ec2496STony Lindgren CPCAP_CHAN(IIO_CURRENT, 8, CPCAP_REG_ADCD0, "ad8"),
36925ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 9, CPCAP_REG_ADCD1, "ad9"),
37025ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 10, CPCAP_REG_ADCD2, "licell"),
37125ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 11, CPCAP_REG_ADCD3, "hv_battp"),
37225ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 12, CPCAP_REG_ADCD4, "tsx1_ad12"),
37325ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 13, CPCAP_REG_ADCD5, "tsx2_ad13"),
37425ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 14, CPCAP_REG_ADCD6, "tsy1_ad14"),
37525ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 15, CPCAP_REG_ADCD7, "tsy2_ad15"),
37625ec2496STony Lindgren
37725ec2496STony Lindgren /* There are two registers with multiplexed functionality */
37825ec2496STony Lindgren CPCAP_CHAN(IIO_VOLTAGE, 16, CPCAP_REG_ADCD0, "chg_vsense"),
37925ec2496STony Lindgren CPCAP_CHAN(IIO_CURRENT, 17, CPCAP_REG_ADCD1, "batti2"),
38025ec2496STony Lindgren };
38125ec2496STony Lindgren
cpcap_adc_irq_thread(int irq,void * data)38225ec2496STony Lindgren static irqreturn_t cpcap_adc_irq_thread(int irq, void *data)
38325ec2496STony Lindgren {
38425ec2496STony Lindgren struct iio_dev *indio_dev = data;
38525ec2496STony Lindgren struct cpcap_adc *ddata = iio_priv(indio_dev);
38625ec2496STony Lindgren int error;
38725ec2496STony Lindgren
38825ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
38925ec2496STony Lindgren CPCAP_BIT_ADTRIG_DIS,
39025ec2496STony Lindgren CPCAP_BIT_ADTRIG_DIS);
39125ec2496STony Lindgren if (error)
39225ec2496STony Lindgren return IRQ_NONE;
39325ec2496STony Lindgren
39425ec2496STony Lindgren ddata->done = true;
39525ec2496STony Lindgren wake_up_interruptible(&ddata->wq_data_avail);
39625ec2496STony Lindgren
39725ec2496STony Lindgren return IRQ_HANDLED;
39825ec2496STony Lindgren }
39925ec2496STony Lindgren
40025ec2496STony Lindgren /* ADC calibration functions */
cpcap_adc_setup_calibrate(struct cpcap_adc * ddata,enum cpcap_adc_channel chan)40125ec2496STony Lindgren static void cpcap_adc_setup_calibrate(struct cpcap_adc *ddata,
40225ec2496STony Lindgren enum cpcap_adc_channel chan)
40325ec2496STony Lindgren {
40425ec2496STony Lindgren unsigned int value = 0;
40525ec2496STony Lindgren unsigned long timeout = jiffies + msecs_to_jiffies(3000);
40625ec2496STony Lindgren int error;
40725ec2496STony Lindgren
40825ec2496STony Lindgren if ((chan != CPCAP_ADC_CHG_ISENSE) &&
40925ec2496STony Lindgren (chan != CPCAP_ADC_BATTI))
41025ec2496STony Lindgren return;
41125ec2496STony Lindgren
41225ec2496STony Lindgren value |= CPCAP_BIT_CAL_MODE | CPCAP_BIT_RAND0;
41325ec2496STony Lindgren value |= ((chan << 4) &
41425ec2496STony Lindgren (CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 | CPCAP_BIT_ADA0));
41525ec2496STony Lindgren
41625ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
41725ec2496STony Lindgren CPCAP_BIT_CAL_MODE | CPCAP_BIT_ATOX |
41825ec2496STony Lindgren CPCAP_BIT_ATO3 | CPCAP_BIT_ATO2 |
41925ec2496STony Lindgren CPCAP_BIT_ATO1 | CPCAP_BIT_ATO0 |
42025ec2496STony Lindgren CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 |
42125ec2496STony Lindgren CPCAP_BIT_ADA0 | CPCAP_BIT_AD_SEL1 |
42225ec2496STony Lindgren CPCAP_BIT_RAND1 | CPCAP_BIT_RAND0,
42325ec2496STony Lindgren value);
42425ec2496STony Lindgren if (error)
42525ec2496STony Lindgren return;
42625ec2496STony Lindgren
42725ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
42825ec2496STony Lindgren CPCAP_BIT_ATOX_PS_FACTOR |
42925ec2496STony Lindgren CPCAP_BIT_ADC_PS_FACTOR1 |
43025ec2496STony Lindgren CPCAP_BIT_ADC_PS_FACTOR0,
43125ec2496STony Lindgren 0);
43225ec2496STony Lindgren if (error)
43325ec2496STony Lindgren return;
43425ec2496STony Lindgren
43525ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
43625ec2496STony Lindgren CPCAP_BIT_ADTRIG_DIS,
43725ec2496STony Lindgren CPCAP_BIT_ADTRIG_DIS);
43825ec2496STony Lindgren if (error)
43925ec2496STony Lindgren return;
44025ec2496STony Lindgren
44125ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
44225ec2496STony Lindgren CPCAP_BIT_ASC,
44325ec2496STony Lindgren CPCAP_BIT_ASC);
44425ec2496STony Lindgren if (error)
44525ec2496STony Lindgren return;
44625ec2496STony Lindgren
44725ec2496STony Lindgren do {
44825ec2496STony Lindgren schedule_timeout_uninterruptible(1);
44925ec2496STony Lindgren error = regmap_read(ddata->reg, CPCAP_REG_ADCC2, &value);
45025ec2496STony Lindgren if (error)
45125ec2496STony Lindgren return;
45225ec2496STony Lindgren } while ((value & CPCAP_BIT_ASC) && time_before(jiffies, timeout));
45325ec2496STony Lindgren
45425ec2496STony Lindgren if (value & CPCAP_BIT_ASC)
45525ec2496STony Lindgren dev_err(ddata->dev,
45625ec2496STony Lindgren "Timeout waiting for calibration to complete\n");
45725ec2496STony Lindgren
45825ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
45925ec2496STony Lindgren CPCAP_BIT_CAL_MODE, 0);
46025ec2496STony Lindgren if (error)
46125ec2496STony Lindgren return;
46225ec2496STony Lindgren }
46325ec2496STony Lindgren
cpcap_adc_calibrate_one(struct cpcap_adc * ddata,int channel,u16 calibration_register,int lower_threshold,int upper_threshold)46425ec2496STony Lindgren static int cpcap_adc_calibrate_one(struct cpcap_adc *ddata,
46525ec2496STony Lindgren int channel,
46625ec2496STony Lindgren u16 calibration_register,
46725ec2496STony Lindgren int lower_threshold,
46825ec2496STony Lindgren int upper_threshold)
46925ec2496STony Lindgren {
47025ec2496STony Lindgren unsigned int calibration_data[2];
47125ec2496STony Lindgren unsigned short cal_data_diff;
47225ec2496STony Lindgren int i, error;
47325ec2496STony Lindgren
47425ec2496STony Lindgren for (i = 0; i < CPCAP_ADC_MAX_RETRIES; i++) {
47525ec2496STony Lindgren calibration_data[0] = 0;
47625ec2496STony Lindgren calibration_data[1] = 0;
477*342c6c5eSColin Ian King
47825ec2496STony Lindgren cpcap_adc_setup_calibrate(ddata, channel);
47925ec2496STony Lindgren error = regmap_read(ddata->reg, calibration_register,
48025ec2496STony Lindgren &calibration_data[0]);
48125ec2496STony Lindgren if (error)
48225ec2496STony Lindgren return error;
48325ec2496STony Lindgren cpcap_adc_setup_calibrate(ddata, channel);
48425ec2496STony Lindgren error = regmap_read(ddata->reg, calibration_register,
48525ec2496STony Lindgren &calibration_data[1]);
48625ec2496STony Lindgren if (error)
48725ec2496STony Lindgren return error;
48825ec2496STony Lindgren
48925ec2496STony Lindgren if (calibration_data[0] > calibration_data[1])
49025ec2496STony Lindgren cal_data_diff =
49125ec2496STony Lindgren calibration_data[0] - calibration_data[1];
49225ec2496STony Lindgren else
49325ec2496STony Lindgren cal_data_diff =
49425ec2496STony Lindgren calibration_data[1] - calibration_data[0];
49525ec2496STony Lindgren
49625ec2496STony Lindgren if (((calibration_data[1] >= lower_threshold) &&
49725ec2496STony Lindgren (calibration_data[1] <= upper_threshold) &&
49825ec2496STony Lindgren (cal_data_diff <= ST_ADC_CALIBRATE_DIFF_THRESHOLD)) ||
49925ec2496STony Lindgren (ddata->vendor == CPCAP_VENDOR_TI)) {
50025ec2496STony Lindgren bank_conversion[channel].cal_offset =
50125ec2496STony Lindgren ((short)calibration_data[1] * -1) + 512;
50225ec2496STony Lindgren dev_dbg(ddata->dev, "ch%i calibration complete: %i\n",
50325ec2496STony Lindgren channel, bank_conversion[channel].cal_offset);
50425ec2496STony Lindgren break;
50525ec2496STony Lindgren }
50625ec2496STony Lindgren usleep_range(5000, 10000);
50725ec2496STony Lindgren }
50825ec2496STony Lindgren
50925ec2496STony Lindgren return 0;
51025ec2496STony Lindgren }
51125ec2496STony Lindgren
cpcap_adc_calibrate(struct cpcap_adc * ddata)51225ec2496STony Lindgren static int cpcap_adc_calibrate(struct cpcap_adc *ddata)
51325ec2496STony Lindgren {
51425ec2496STony Lindgren int error;
51525ec2496STony Lindgren
51625ec2496STony Lindgren error = cpcap_adc_calibrate_one(ddata, CPCAP_ADC_CHG_ISENSE,
51725ec2496STony Lindgren CPCAP_REG_ADCAL1,
51825ec2496STony Lindgren ST_ADC_CAL_CHRGI_LOW_THRESHOLD,
51925ec2496STony Lindgren ST_ADC_CAL_CHRGI_HIGH_THRESHOLD);
52025ec2496STony Lindgren if (error)
52125ec2496STony Lindgren return error;
52225ec2496STony Lindgren
52325ec2496STony Lindgren error = cpcap_adc_calibrate_one(ddata, CPCAP_ADC_BATTI,
52425ec2496STony Lindgren CPCAP_REG_ADCAL2,
52525ec2496STony Lindgren ST_ADC_CAL_BATTI_LOW_THRESHOLD,
52625ec2496STony Lindgren ST_ADC_CAL_BATTI_HIGH_THRESHOLD);
52725ec2496STony Lindgren if (error)
52825ec2496STony Lindgren return error;
52925ec2496STony Lindgren
53025ec2496STony Lindgren return 0;
53125ec2496STony Lindgren }
53225ec2496STony Lindgren
53325ec2496STony Lindgren /* ADC setup, read and scale functions */
cpcap_adc_setup_bank(struct cpcap_adc * ddata,struct cpcap_adc_request * req)53425ec2496STony Lindgren static void cpcap_adc_setup_bank(struct cpcap_adc *ddata,
53525ec2496STony Lindgren struct cpcap_adc_request *req)
53625ec2496STony Lindgren {
53725ec2496STony Lindgren const struct cpcap_adc_ato *ato = ddata->ato;
53825ec2496STony Lindgren unsigned short value1 = 0;
53925ec2496STony Lindgren unsigned short value2 = 0;
54025ec2496STony Lindgren int error;
54125ec2496STony Lindgren
54225ec2496STony Lindgren if (!ato)
54325ec2496STony Lindgren return;
54425ec2496STony Lindgren
54525ec2496STony Lindgren switch (req->channel) {
5463f9f3a1aSTony Lindgren case CPCAP_ADC_AD0:
5473f9f3a1aSTony Lindgren value2 |= CPCAP_BIT_THERMBIAS_EN;
5483f9f3a1aSTony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
5493f9f3a1aSTony Lindgren CPCAP_BIT_THERMBIAS_EN,
5503f9f3a1aSTony Lindgren value2);
5513f9f3a1aSTony Lindgren if (error)
5523f9f3a1aSTony Lindgren return;
5533f9f3a1aSTony Lindgren usleep_range(800, 1000);
5543f9f3a1aSTony Lindgren break;
55525ec2496STony Lindgren case CPCAP_ADC_AD8 ... CPCAP_ADC_TSY2_AD15:
55625ec2496STony Lindgren value1 |= CPCAP_BIT_AD_SEL1;
55725ec2496STony Lindgren break;
55825ec2496STony Lindgren case CPCAP_ADC_BATTP_PI16 ... CPCAP_ADC_BATTI_PI17:
55925ec2496STony Lindgren value1 |= CPCAP_BIT_RAND1;
56039996252SGustavo A. R. Silva break;
56125ec2496STony Lindgren default:
56225ec2496STony Lindgren break;
56325ec2496STony Lindgren }
56425ec2496STony Lindgren
56525ec2496STony Lindgren switch (req->timing) {
56625ec2496STony Lindgren case CPCAP_ADC_TIMING_IN:
56725ec2496STony Lindgren value1 |= ato->ato_in;
56825ec2496STony Lindgren value1 |= ato->atox_in;
56925ec2496STony Lindgren value2 |= ato->adc_ps_factor_in;
57025ec2496STony Lindgren value2 |= ato->atox_ps_factor_in;
57125ec2496STony Lindgren break;
57225ec2496STony Lindgren case CPCAP_ADC_TIMING_OUT:
57325ec2496STony Lindgren value1 |= ato->ato_out;
57425ec2496STony Lindgren value1 |= ato->atox_out;
57525ec2496STony Lindgren value2 |= ato->adc_ps_factor_out;
57625ec2496STony Lindgren value2 |= ato->atox_ps_factor_out;
57725ec2496STony Lindgren break;
57825ec2496STony Lindgren
57925ec2496STony Lindgren case CPCAP_ADC_TIMING_IMM:
58025ec2496STony Lindgren default:
58125ec2496STony Lindgren break;
58225ec2496STony Lindgren }
58325ec2496STony Lindgren
58425ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
58525ec2496STony Lindgren CPCAP_BIT_CAL_MODE | CPCAP_BIT_ATOX |
58625ec2496STony Lindgren CPCAP_BIT_ATO3 | CPCAP_BIT_ATO2 |
58725ec2496STony Lindgren CPCAP_BIT_ATO1 | CPCAP_BIT_ATO0 |
58825ec2496STony Lindgren CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 |
58925ec2496STony Lindgren CPCAP_BIT_ADA0 | CPCAP_BIT_AD_SEL1 |
59025ec2496STony Lindgren CPCAP_BIT_RAND1 | CPCAP_BIT_RAND0,
59125ec2496STony Lindgren value1);
59225ec2496STony Lindgren if (error)
59325ec2496STony Lindgren return;
59425ec2496STony Lindgren
59525ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
59625ec2496STony Lindgren CPCAP_BIT_ATOX_PS_FACTOR |
59725ec2496STony Lindgren CPCAP_BIT_ADC_PS_FACTOR1 |
5983f9f3a1aSTony Lindgren CPCAP_BIT_ADC_PS_FACTOR0 |
5993f9f3a1aSTony Lindgren CPCAP_BIT_THERMBIAS_EN,
60025ec2496STony Lindgren value2);
60125ec2496STony Lindgren if (error)
60225ec2496STony Lindgren return;
60325ec2496STony Lindgren
60425ec2496STony Lindgren if (req->timing == CPCAP_ADC_TIMING_IMM) {
60525ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
60625ec2496STony Lindgren CPCAP_BIT_ADTRIG_DIS,
60725ec2496STony Lindgren CPCAP_BIT_ADTRIG_DIS);
60825ec2496STony Lindgren if (error)
60925ec2496STony Lindgren return;
61025ec2496STony Lindgren
61125ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
61225ec2496STony Lindgren CPCAP_BIT_ASC,
61325ec2496STony Lindgren CPCAP_BIT_ASC);
61425ec2496STony Lindgren if (error)
61525ec2496STony Lindgren return;
61625ec2496STony Lindgren } else {
61725ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
61825ec2496STony Lindgren CPCAP_BIT_ADTRIG_ONESHOT,
61925ec2496STony Lindgren CPCAP_BIT_ADTRIG_ONESHOT);
62025ec2496STony Lindgren if (error)
62125ec2496STony Lindgren return;
62225ec2496STony Lindgren
62325ec2496STony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
62425ec2496STony Lindgren CPCAP_BIT_ADTRIG_DIS, 0);
62525ec2496STony Lindgren if (error)
62625ec2496STony Lindgren return;
62725ec2496STony Lindgren }
62825ec2496STony Lindgren }
62925ec2496STony Lindgren
cpcap_adc_start_bank(struct cpcap_adc * ddata,struct cpcap_adc_request * req)63025ec2496STony Lindgren static int cpcap_adc_start_bank(struct cpcap_adc *ddata,
63125ec2496STony Lindgren struct cpcap_adc_request *req)
63225ec2496STony Lindgren {
63325ec2496STony Lindgren int i, error;
63425ec2496STony Lindgren
63525ec2496STony Lindgren req->timing = CPCAP_ADC_TIMING_IMM;
63625ec2496STony Lindgren ddata->done = false;
63725ec2496STony Lindgren
63825ec2496STony Lindgren for (i = 0; i < CPCAP_ADC_MAX_RETRIES; i++) {
63925ec2496STony Lindgren cpcap_adc_setup_bank(ddata, req);
64025ec2496STony Lindgren error = wait_event_interruptible_timeout(ddata->wq_data_avail,
64125ec2496STony Lindgren ddata->done,
64225ec2496STony Lindgren msecs_to_jiffies(50));
64325ec2496STony Lindgren if (error > 0)
64425ec2496STony Lindgren return 0;
64525ec2496STony Lindgren
64625ec2496STony Lindgren if (error == 0) {
64725ec2496STony Lindgren error = -ETIMEDOUT;
64825ec2496STony Lindgren continue;
64925ec2496STony Lindgren }
65025ec2496STony Lindgren
65125ec2496STony Lindgren if (error < 0)
65225ec2496STony Lindgren return error;
65325ec2496STony Lindgren }
65425ec2496STony Lindgren
65525ec2496STony Lindgren return error;
65625ec2496STony Lindgren }
65725ec2496STony Lindgren
cpcap_adc_stop_bank(struct cpcap_adc * ddata)6583f9f3a1aSTony Lindgren static int cpcap_adc_stop_bank(struct cpcap_adc *ddata)
6593f9f3a1aSTony Lindgren {
6603f9f3a1aSTony Lindgren int error;
6613f9f3a1aSTony Lindgren
6623f9f3a1aSTony Lindgren error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
6633f9f3a1aSTony Lindgren 0xffff,
6643f9f3a1aSTony Lindgren CPCAP_REG_ADCC1_DEFAULTS);
6653f9f3a1aSTony Lindgren if (error)
6663f9f3a1aSTony Lindgren return error;
6673f9f3a1aSTony Lindgren
6683f9f3a1aSTony Lindgren return regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
6693f9f3a1aSTony Lindgren 0xffff,
6703f9f3a1aSTony Lindgren CPCAP_REG_ADCC2_DEFAULTS);
6713f9f3a1aSTony Lindgren }
6723f9f3a1aSTony Lindgren
cpcap_adc_phase(struct cpcap_adc_request * req)67325ec2496STony Lindgren static void cpcap_adc_phase(struct cpcap_adc_request *req)
67425ec2496STony Lindgren {
67525ec2496STony Lindgren const struct cpcap_adc_conversion_tbl *conv_tbl = req->conv_tbl;
67625ec2496STony Lindgren const struct cpcap_adc_phasing_tbl *phase_tbl = req->phase_tbl;
67725ec2496STony Lindgren int index = req->channel;
67825ec2496STony Lindgren
67925ec2496STony Lindgren /* Remuxed channels 16 and 17 use BATTP and BATTI entries */
68025ec2496STony Lindgren switch (req->channel) {
68125ec2496STony Lindgren case CPCAP_ADC_BATTP:
68225ec2496STony Lindgren case CPCAP_ADC_BATTP_PI16:
68325ec2496STony Lindgren index = req->bank_index;
68425ec2496STony Lindgren req->result -= phase_tbl[index].offset;
68525ec2496STony Lindgren req->result -= CPCAP_FOUR_POINT_TWO_ADC;
68625ec2496STony Lindgren req->result *= phase_tbl[index].multiplier;
68725ec2496STony Lindgren if (phase_tbl[index].divider == 0)
68825ec2496STony Lindgren return;
68925ec2496STony Lindgren req->result /= phase_tbl[index].divider;
69025ec2496STony Lindgren req->result += CPCAP_FOUR_POINT_TWO_ADC;
69125ec2496STony Lindgren break;
69225ec2496STony Lindgren case CPCAP_ADC_BATTI_PI17:
69325ec2496STony Lindgren index = req->bank_index;
694df561f66SGustavo A. R. Silva fallthrough;
69525ec2496STony Lindgren default:
69625ec2496STony Lindgren req->result += conv_tbl[index].cal_offset;
69725ec2496STony Lindgren req->result += conv_tbl[index].align_offset;
69825ec2496STony Lindgren req->result *= phase_tbl[index].multiplier;
69925ec2496STony Lindgren if (phase_tbl[index].divider == 0)
70025ec2496STony Lindgren return;
70125ec2496STony Lindgren req->result /= phase_tbl[index].divider;
70225ec2496STony Lindgren req->result += phase_tbl[index].offset;
70325ec2496STony Lindgren break;
70425ec2496STony Lindgren }
70525ec2496STony Lindgren
70625ec2496STony Lindgren if (req->result < phase_tbl[index].min)
70725ec2496STony Lindgren req->result = phase_tbl[index].min;
70825ec2496STony Lindgren else if (req->result > phase_tbl[index].max)
70925ec2496STony Lindgren req->result = phase_tbl[index].max;
71025ec2496STony Lindgren }
71125ec2496STony Lindgren
71225ec2496STony Lindgren /* Looks up temperatures in a table and calculates averages if needed */
cpcap_adc_table_to_millicelcius(unsigned short value)71325ec2496STony Lindgren static int cpcap_adc_table_to_millicelcius(unsigned short value)
71425ec2496STony Lindgren {
71525ec2496STony Lindgren int i, result = 0, alpha;
71625ec2496STony Lindgren
71725ec2496STony Lindgren if (value <= temp_map[CPCAP_MAX_TEMP_LVL - 1][0])
71825ec2496STony Lindgren return temp_map[CPCAP_MAX_TEMP_LVL - 1][1];
71925ec2496STony Lindgren
72025ec2496STony Lindgren if (value >= temp_map[0][0])
72125ec2496STony Lindgren return temp_map[0][1];
72225ec2496STony Lindgren
72325ec2496STony Lindgren for (i = 0; i < CPCAP_MAX_TEMP_LVL - 1; i++) {
72425ec2496STony Lindgren if ((value <= temp_map[i][0]) &&
72525ec2496STony Lindgren (value >= temp_map[i + 1][0])) {
72625ec2496STony Lindgren if (value == temp_map[i][0]) {
72725ec2496STony Lindgren result = temp_map[i][1];
72825ec2496STony Lindgren } else if (value == temp_map[i + 1][0]) {
72925ec2496STony Lindgren result = temp_map[i + 1][1];
73025ec2496STony Lindgren } else {
73125ec2496STony Lindgren alpha = ((value - temp_map[i][0]) * 1000) /
73225ec2496STony Lindgren (temp_map[i + 1][0] - temp_map[i][0]);
73325ec2496STony Lindgren
73425ec2496STony Lindgren result = temp_map[i][1] +
73525ec2496STony Lindgren ((alpha * (temp_map[i + 1][1] -
73625ec2496STony Lindgren temp_map[i][1])) / 1000);
73725ec2496STony Lindgren }
73825ec2496STony Lindgren break;
73925ec2496STony Lindgren }
74025ec2496STony Lindgren }
74125ec2496STony Lindgren
74225ec2496STony Lindgren return result;
74325ec2496STony Lindgren }
74425ec2496STony Lindgren
cpcap_adc_convert(struct cpcap_adc_request * req)74525ec2496STony Lindgren static void cpcap_adc_convert(struct cpcap_adc_request *req)
74625ec2496STony Lindgren {
74725ec2496STony Lindgren const struct cpcap_adc_conversion_tbl *conv_tbl = req->conv_tbl;
74825ec2496STony Lindgren int index = req->channel;
74925ec2496STony Lindgren
75025ec2496STony Lindgren /* Remuxed channels 16 and 17 use BATTP and BATTI entries */
75125ec2496STony Lindgren switch (req->channel) {
75225ec2496STony Lindgren case CPCAP_ADC_BATTP_PI16:
75325ec2496STony Lindgren index = CPCAP_ADC_BATTP;
75425ec2496STony Lindgren break;
75525ec2496STony Lindgren case CPCAP_ADC_BATTI_PI17:
75625ec2496STony Lindgren index = CPCAP_ADC_BATTI;
75725ec2496STony Lindgren break;
75825ec2496STony Lindgren default:
75925ec2496STony Lindgren break;
76025ec2496STony Lindgren }
76125ec2496STony Lindgren
76225ec2496STony Lindgren /* No conversion for raw channels */
76325ec2496STony Lindgren if (conv_tbl[index].conv_type == IIO_CHAN_INFO_RAW)
76425ec2496STony Lindgren return;
76525ec2496STony Lindgren
76625ec2496STony Lindgren /* Temperatures use a lookup table instead of conversion table */
7673f9f3a1aSTony Lindgren if ((req->channel == CPCAP_ADC_AD0) ||
76825ec2496STony Lindgren (req->channel == CPCAP_ADC_AD3)) {
76925ec2496STony Lindgren req->result =
77025ec2496STony Lindgren cpcap_adc_table_to_millicelcius(req->result);
77125ec2496STony Lindgren
77225ec2496STony Lindgren return;
77325ec2496STony Lindgren }
77425ec2496STony Lindgren
77525ec2496STony Lindgren /* All processed channels use a conversion table */
77625ec2496STony Lindgren req->result *= conv_tbl[index].multiplier;
77725ec2496STony Lindgren if (conv_tbl[index].divider == 0)
77825ec2496STony Lindgren return;
77925ec2496STony Lindgren req->result /= conv_tbl[index].divider;
78025ec2496STony Lindgren req->result += conv_tbl[index].conv_offset;
78125ec2496STony Lindgren }
78225ec2496STony Lindgren
78325ec2496STony Lindgren /*
78425ec2496STony Lindgren * REVISIT: Check if timed sampling can use multiple channels at the
78525ec2496STony Lindgren * same time. If not, replace channel_mask with just channel.
78625ec2496STony Lindgren */
cpcap_adc_read_bank_scaled(struct cpcap_adc * ddata,struct cpcap_adc_request * req)78725ec2496STony Lindgren static int cpcap_adc_read_bank_scaled(struct cpcap_adc *ddata,
78825ec2496STony Lindgren struct cpcap_adc_request *req)
78925ec2496STony Lindgren {
79025ec2496STony Lindgren int calibration_data, error, addr;
79125ec2496STony Lindgren
79225ec2496STony Lindgren if (ddata->vendor == CPCAP_VENDOR_TI) {
79325ec2496STony Lindgren error = regmap_read(ddata->reg, CPCAP_REG_ADCAL1,
79425ec2496STony Lindgren &calibration_data);
79525ec2496STony Lindgren if (error)
79625ec2496STony Lindgren return error;
79725ec2496STony Lindgren bank_conversion[CPCAP_ADC_CHG_ISENSE].cal_offset =
79825ec2496STony Lindgren ((short)calibration_data * -1) + 512;
79925ec2496STony Lindgren
80025ec2496STony Lindgren error = regmap_read(ddata->reg, CPCAP_REG_ADCAL2,
80125ec2496STony Lindgren &calibration_data);
80225ec2496STony Lindgren if (error)
80325ec2496STony Lindgren return error;
80425ec2496STony Lindgren bank_conversion[CPCAP_ADC_BATTI].cal_offset =
80525ec2496STony Lindgren ((short)calibration_data * -1) + 512;
80625ec2496STony Lindgren }
80725ec2496STony Lindgren
80825ec2496STony Lindgren addr = CPCAP_REG_ADCD0 + req->bank_index * 4;
80925ec2496STony Lindgren
81025ec2496STony Lindgren error = regmap_read(ddata->reg, addr, &req->result);
81125ec2496STony Lindgren if (error)
81225ec2496STony Lindgren return error;
81325ec2496STony Lindgren
81425ec2496STony Lindgren req->result &= 0x3ff;
81525ec2496STony Lindgren cpcap_adc_phase(req);
81625ec2496STony Lindgren cpcap_adc_convert(req);
81725ec2496STony Lindgren
81825ec2496STony Lindgren return 0;
81925ec2496STony Lindgren }
82025ec2496STony Lindgren
cpcap_adc_init_request(struct cpcap_adc_request * req,int channel)82125ec2496STony Lindgren static int cpcap_adc_init_request(struct cpcap_adc_request *req,
82225ec2496STony Lindgren int channel)
82325ec2496STony Lindgren {
82425ec2496STony Lindgren req->channel = channel;
82525ec2496STony Lindgren req->phase_tbl = bank_phasing;
82625ec2496STony Lindgren req->conv_tbl = bank_conversion;
82725ec2496STony Lindgren
82825ec2496STony Lindgren switch (channel) {
8293f9f3a1aSTony Lindgren case CPCAP_ADC_AD0 ... CPCAP_ADC_USB_ID:
83025ec2496STony Lindgren req->bank_index = channel;
83125ec2496STony Lindgren break;
83225ec2496STony Lindgren case CPCAP_ADC_AD8 ... CPCAP_ADC_TSY2_AD15:
83325ec2496STony Lindgren req->bank_index = channel - 8;
83425ec2496STony Lindgren break;
83525ec2496STony Lindgren case CPCAP_ADC_BATTP_PI16:
83625ec2496STony Lindgren req->bank_index = CPCAP_ADC_BATTP;
83725ec2496STony Lindgren break;
83825ec2496STony Lindgren case CPCAP_ADC_BATTI_PI17:
83925ec2496STony Lindgren req->bank_index = CPCAP_ADC_BATTI;
84025ec2496STony Lindgren break;
84125ec2496STony Lindgren default:
84225ec2496STony Lindgren return -EINVAL;
84325ec2496STony Lindgren }
84425ec2496STony Lindgren
84525ec2496STony Lindgren return 0;
84625ec2496STony Lindgren }
84725ec2496STony Lindgren
cpcap_adc_read_st_die_temp(struct cpcap_adc * ddata,int addr,int * val)848951d21deSTony Lindgren static int cpcap_adc_read_st_die_temp(struct cpcap_adc *ddata,
849951d21deSTony Lindgren int addr, int *val)
850951d21deSTony Lindgren {
851951d21deSTony Lindgren int error;
852951d21deSTony Lindgren
853951d21deSTony Lindgren error = regmap_read(ddata->reg, addr, val);
854951d21deSTony Lindgren if (error)
855951d21deSTony Lindgren return error;
856951d21deSTony Lindgren
857951d21deSTony Lindgren *val -= 282;
858951d21deSTony Lindgren *val *= 114;
859951d21deSTony Lindgren *val += 25000;
860951d21deSTony Lindgren
861951d21deSTony Lindgren return 0;
862951d21deSTony Lindgren }
863951d21deSTony Lindgren
cpcap_adc_read(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)86425ec2496STony Lindgren static int cpcap_adc_read(struct iio_dev *indio_dev,
86525ec2496STony Lindgren struct iio_chan_spec const *chan,
86625ec2496STony Lindgren int *val, int *val2, long mask)
86725ec2496STony Lindgren {
86825ec2496STony Lindgren struct cpcap_adc *ddata = iio_priv(indio_dev);
86925ec2496STony Lindgren struct cpcap_adc_request req;
87025ec2496STony Lindgren int error;
87125ec2496STony Lindgren
87225ec2496STony Lindgren error = cpcap_adc_init_request(&req, chan->channel);
87325ec2496STony Lindgren if (error)
87425ec2496STony Lindgren return error;
87525ec2496STony Lindgren
87625ec2496STony Lindgren switch (mask) {
87725ec2496STony Lindgren case IIO_CHAN_INFO_RAW:
87825ec2496STony Lindgren mutex_lock(&ddata->lock);
87925ec2496STony Lindgren error = cpcap_adc_start_bank(ddata, &req);
88025ec2496STony Lindgren if (error)
88125ec2496STony Lindgren goto err_unlock;
88225ec2496STony Lindgren error = regmap_read(ddata->reg, chan->address, val);
88325ec2496STony Lindgren if (error)
88425ec2496STony Lindgren goto err_unlock;
8853f9f3a1aSTony Lindgren error = cpcap_adc_stop_bank(ddata);
8863f9f3a1aSTony Lindgren if (error)
8873f9f3a1aSTony Lindgren goto err_unlock;
88825ec2496STony Lindgren mutex_unlock(&ddata->lock);
88925ec2496STony Lindgren break;
89025ec2496STony Lindgren case IIO_CHAN_INFO_PROCESSED:
89125ec2496STony Lindgren mutex_lock(&ddata->lock);
89225ec2496STony Lindgren error = cpcap_adc_start_bank(ddata, &req);
89325ec2496STony Lindgren if (error)
89425ec2496STony Lindgren goto err_unlock;
895951d21deSTony Lindgren if ((ddata->vendor == CPCAP_VENDOR_ST) &&
896951d21deSTony Lindgren (chan->channel == CPCAP_ADC_AD3)) {
897951d21deSTony Lindgren error = cpcap_adc_read_st_die_temp(ddata,
898951d21deSTony Lindgren chan->address,
899951d21deSTony Lindgren &req.result);
900951d21deSTony Lindgren if (error)
901951d21deSTony Lindgren goto err_unlock;
902951d21deSTony Lindgren } else {
90325ec2496STony Lindgren error = cpcap_adc_read_bank_scaled(ddata, &req);
90425ec2496STony Lindgren if (error)
90525ec2496STony Lindgren goto err_unlock;
906951d21deSTony Lindgren }
9073f9f3a1aSTony Lindgren error = cpcap_adc_stop_bank(ddata);
9083f9f3a1aSTony Lindgren if (error)
9093f9f3a1aSTony Lindgren goto err_unlock;
91025ec2496STony Lindgren mutex_unlock(&ddata->lock);
91125ec2496STony Lindgren *val = req.result;
91225ec2496STony Lindgren break;
91325ec2496STony Lindgren default:
91425ec2496STony Lindgren return -EINVAL;
91525ec2496STony Lindgren }
91625ec2496STony Lindgren
91725ec2496STony Lindgren return IIO_VAL_INT;
91825ec2496STony Lindgren
91925ec2496STony Lindgren err_unlock:
92025ec2496STony Lindgren mutex_unlock(&ddata->lock);
92125ec2496STony Lindgren dev_err(ddata->dev, "error reading ADC: %i\n", error);
92225ec2496STony Lindgren
92325ec2496STony Lindgren return error;
92425ec2496STony Lindgren }
92525ec2496STony Lindgren
92625ec2496STony Lindgren static const struct iio_info cpcap_adc_info = {
92725ec2496STony Lindgren .read_raw = &cpcap_adc_read,
92825ec2496STony Lindgren };
92925ec2496STony Lindgren
93025ec2496STony Lindgren /*
93125ec2496STony Lindgren * Configuration for Motorola mapphone series such as droid 4.
93225ec2496STony Lindgren * Copied from the Motorola mapphone kernel tree.
93325ec2496STony Lindgren */
93425ec2496STony Lindgren static const struct cpcap_adc_ato mapphone_adc = {
93525ec2496STony Lindgren .ato_in = 0x0480,
93625ec2496STony Lindgren .atox_in = 0,
93725ec2496STony Lindgren .adc_ps_factor_in = 0x0200,
93825ec2496STony Lindgren .atox_ps_factor_in = 0,
93925ec2496STony Lindgren .ato_out = 0,
94025ec2496STony Lindgren .atox_out = 0,
94125ec2496STony Lindgren .adc_ps_factor_out = 0,
94225ec2496STony Lindgren .atox_ps_factor_out = 0,
94325ec2496STony Lindgren };
94425ec2496STony Lindgren
94525ec2496STony Lindgren static const struct of_device_id cpcap_adc_id_table[] = {
94625ec2496STony Lindgren {
94725ec2496STony Lindgren .compatible = "motorola,cpcap-adc",
94825ec2496STony Lindgren },
94925ec2496STony Lindgren {
95025ec2496STony Lindgren .compatible = "motorola,mapphone-cpcap-adc",
95125ec2496STony Lindgren .data = &mapphone_adc,
95225ec2496STony Lindgren },
95325ec2496STony Lindgren { /* sentinel */ },
95425ec2496STony Lindgren };
95525ec2496STony Lindgren MODULE_DEVICE_TABLE(of, cpcap_adc_id_table);
95625ec2496STony Lindgren
cpcap_adc_probe(struct platform_device * pdev)95725ec2496STony Lindgren static int cpcap_adc_probe(struct platform_device *pdev)
95825ec2496STony Lindgren {
95925ec2496STony Lindgren struct cpcap_adc *ddata;
96025ec2496STony Lindgren struct iio_dev *indio_dev;
96125ec2496STony Lindgren int error;
96225ec2496STony Lindgren
96325ec2496STony Lindgren indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*ddata));
96425ec2496STony Lindgren if (!indio_dev) {
96525ec2496STony Lindgren dev_err(&pdev->dev, "failed to allocate iio device\n");
96625ec2496STony Lindgren
96725ec2496STony Lindgren return -ENOMEM;
96825ec2496STony Lindgren }
96925ec2496STony Lindgren ddata = iio_priv(indio_dev);
97029788fd6SJonathan Cameron ddata->ato = device_get_match_data(&pdev->dev);
97129788fd6SJonathan Cameron if (!ddata->ato)
97229788fd6SJonathan Cameron return -ENODEV;
97325ec2496STony Lindgren ddata->dev = &pdev->dev;
97425ec2496STony Lindgren
97525ec2496STony Lindgren mutex_init(&ddata->lock);
97625ec2496STony Lindgren init_waitqueue_head(&ddata->wq_data_avail);
97725ec2496STony Lindgren
97825ec2496STony Lindgren indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
97925ec2496STony Lindgren indio_dev->channels = cpcap_adc_channels;
98025ec2496STony Lindgren indio_dev->num_channels = ARRAY_SIZE(cpcap_adc_channels);
98125ec2496STony Lindgren indio_dev->name = dev_name(&pdev->dev);
98225ec2496STony Lindgren indio_dev->info = &cpcap_adc_info;
98325ec2496STony Lindgren
98425ec2496STony Lindgren ddata->reg = dev_get_regmap(pdev->dev.parent, NULL);
98525ec2496STony Lindgren if (!ddata->reg)
98625ec2496STony Lindgren return -ENODEV;
98725ec2496STony Lindgren
98825ec2496STony Lindgren error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor);
98925ec2496STony Lindgren if (error)
99025ec2496STony Lindgren return error;
99125ec2496STony Lindgren
99225ec2496STony Lindgren platform_set_drvdata(pdev, indio_dev);
99325ec2496STony Lindgren
99425ec2496STony Lindgren ddata->irq = platform_get_irq_byname(pdev, "adcdone");
99581b039ecSPan Bian if (ddata->irq < 0)
99625ec2496STony Lindgren return -ENODEV;
99725ec2496STony Lindgren
99825ec2496STony Lindgren error = devm_request_threaded_irq(&pdev->dev, ddata->irq, NULL,
99925ec2496STony Lindgren cpcap_adc_irq_thread,
10000e643753SJonathan Cameron IRQF_TRIGGER_NONE | IRQF_ONESHOT,
100125ec2496STony Lindgren "cpcap-adc", indio_dev);
100225ec2496STony Lindgren if (error) {
100325ec2496STony Lindgren dev_err(&pdev->dev, "could not get irq: %i\n",
100425ec2496STony Lindgren error);
100525ec2496STony Lindgren
100625ec2496STony Lindgren return error;
100725ec2496STony Lindgren }
100825ec2496STony Lindgren
100925ec2496STony Lindgren error = cpcap_adc_calibrate(ddata);
101025ec2496STony Lindgren if (error)
101125ec2496STony Lindgren return error;
101225ec2496STony Lindgren
101325ec2496STony Lindgren dev_info(&pdev->dev, "CPCAP ADC device probed\n");
101425ec2496STony Lindgren
101525ec2496STony Lindgren return devm_iio_device_register(&pdev->dev, indio_dev);
101625ec2496STony Lindgren }
101725ec2496STony Lindgren
101825ec2496STony Lindgren static struct platform_driver cpcap_adc_driver = {
101925ec2496STony Lindgren .driver = {
102025ec2496STony Lindgren .name = "cpcap_adc",
102129788fd6SJonathan Cameron .of_match_table = cpcap_adc_id_table,
102225ec2496STony Lindgren },
102325ec2496STony Lindgren .probe = cpcap_adc_probe,
102425ec2496STony Lindgren };
102525ec2496STony Lindgren
102625ec2496STony Lindgren module_platform_driver(cpcap_adc_driver);
102725ec2496STony Lindgren
102825ec2496STony Lindgren MODULE_ALIAS("platform:cpcap_adc");
102925ec2496STony Lindgren MODULE_DESCRIPTION("CPCAP ADC driver");
103025ec2496STony Lindgren MODULE_AUTHOR("Tony Lindgren <tony@atomide.com");
103125ec2496STony Lindgren MODULE_LICENSE("GPL v2");
1032