2 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
4 * Rewritten for Linux IIO framework with some code based on
5 * earlier driver found in the Motorola Linux kernel:
7 * Copyright (C) 2009-2010 Motorola, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
27 #include <linux/of_platform.h>
28 #include <linux/platform_device.h>
29 #include <linux/regmap.h>
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/driver.h>
33 #include <linux/iio/iio.h>
34 #include <linux/iio/kfifo_buf.h>
35 #include <linux/mfd/motorola-cpcap.h>
37 /* Register CPCAP_REG_ADCC1 bits */
38 #define CPCAP_BIT_ADEN_AUTO_CLR BIT(15) /* Currently unused */
39 #define CPCAP_BIT_CAL_MODE BIT(14) /* Set with BIT_RAND0 */
40 #define CPCAP_BIT_ADC_CLK_SEL1 BIT(13) /* Currently unused */
41 #define CPCAP_BIT_ADC_CLK_SEL0 BIT(12) /* Currently unused */
42 #define CPCAP_BIT_ATOX BIT(11)
43 #define CPCAP_BIT_ATO3 BIT(10)
44 #define CPCAP_BIT_ATO2 BIT(9)
45 #define CPCAP_BIT_ATO1 BIT(8)
46 #define CPCAP_BIT_ATO0 BIT(7)
47 #define CPCAP_BIT_ADA2 BIT(6)
48 #define CPCAP_BIT_ADA1 BIT(5)
49 #define CPCAP_BIT_ADA0 BIT(4)
50 #define CPCAP_BIT_AD_SEL1 BIT(3) /* Set for bank1 */
51 #define CPCAP_BIT_RAND1 BIT(2) /* Set for channel 16 & 17 */
52 #define CPCAP_BIT_RAND0 BIT(1) /* Set with CAL_MODE */
53 #define CPCAP_BIT_ADEN BIT(0) /* Currently unused */
55 #define CPCAP_REG_ADCC1_DEFAULTS (CPCAP_BIT_ADEN_AUTO_CLR | \
56 CPCAP_BIT_ADC_CLK_SEL0 | \
59 /* Register CPCAP_REG_ADCC2 bits */
60 #define CPCAP_BIT_CAL_FACTOR_ENABLE BIT(15) /* Currently unused */
61 #define CPCAP_BIT_BATDETB_EN BIT(14) /* Currently unused */
62 #define CPCAP_BIT_ADTRIG_ONESHOT BIT(13) /* Set for !TIMING_IMM */
63 #define CPCAP_BIT_ASC BIT(12) /* Set for TIMING_IMM */
64 #define CPCAP_BIT_ATOX_PS_FACTOR BIT(11)
65 #define CPCAP_BIT_ADC_PS_FACTOR1 BIT(10)
66 #define CPCAP_BIT_ADC_PS_FACTOR0 BIT(9)
67 #define CPCAP_BIT_AD4_SELECT BIT(8) /* Currently unused */
68 #define CPCAP_BIT_ADC_BUSY BIT(7) /* Currently unused */
69 #define CPCAP_BIT_THERMBIAS_EN BIT(6) /* Bias for AD0_BATTDETB */
70 #define CPCAP_BIT_ADTRIG_DIS BIT(5) /* Disable interrupt */
71 #define CPCAP_BIT_LIADC BIT(4) /* Currently unused */
72 #define CPCAP_BIT_TS_REFEN BIT(3) /* Currently unused */
73 #define CPCAP_BIT_TS_M2 BIT(2) /* Currently unused */
74 #define CPCAP_BIT_TS_M1 BIT(1) /* Currently unused */
75 #define CPCAP_BIT_TS_M0 BIT(0) /* Currently unused */
77 #define CPCAP_REG_ADCC2_DEFAULTS (CPCAP_BIT_AD4_SELECT | \
78 CPCAP_BIT_ADTRIG_DIS | \
83 #define CPCAP_MAX_TEMP_LVL 27
84 #define CPCAP_FOUR_POINT_TWO_ADC 801
85 #define ST_ADC_CAL_CHRGI_HIGH_THRESHOLD 530
86 #define ST_ADC_CAL_CHRGI_LOW_THRESHOLD 494
87 #define ST_ADC_CAL_BATTI_HIGH_THRESHOLD 530
88 #define ST_ADC_CAL_BATTI_LOW_THRESHOLD 494
89 #define ST_ADC_CALIBRATE_DIFF_THRESHOLD 3
91 #define CPCAP_ADC_MAX_RETRIES 5 /* Calibration */
94 * struct cpcap_adc_ato - timing settings for cpcap adc
96 * Unfortunately no cpcap documentation available, please document when
99 struct cpcap_adc_ato
{
100 unsigned short ato_in
;
101 unsigned short atox_in
;
102 unsigned short adc_ps_factor_in
;
103 unsigned short atox_ps_factor_in
;
104 unsigned short ato_out
;
105 unsigned short atox_out
;
106 unsigned short adc_ps_factor_out
;
107 unsigned short atox_ps_factor_out
;
111 * struct cpcap-adc - cpcap adc device driver data
113 * @dev: struct device
114 * @vendor: cpcap vendor
117 * @ato: request timings
118 * @wq_data_avail: work queue
126 struct mutex lock
; /* ADC register access lock */
127 const struct cpcap_adc_ato
*ato
;
128 wait_queue_head_t wq_data_avail
;
133 * enum cpcap_adc_channel - cpcap adc channels
135 enum cpcap_adc_channel
{
137 CPCAP_ADC_AD0
, /* Battery temperature */
138 CPCAP_ADC_BATTP
, /* Battery voltage */
139 CPCAP_ADC_VBUS
, /* USB VBUS voltage */
140 CPCAP_ADC_AD3
, /* Die temperature when charging */
141 CPCAP_ADC_BPLUS_AD4
, /* Another battery or system voltage */
142 CPCAP_ADC_CHG_ISENSE
, /* Calibrated charge current */
143 CPCAP_ADC_BATTI
, /* Calibrated system current */
144 CPCAP_ADC_USB_ID
, /* USB OTG ID, unused on droid 4? */
147 CPCAP_ADC_AD8
, /* Seems unused */
148 CPCAP_ADC_AD9
, /* Seems unused */
149 CPCAP_ADC_LICELL
, /* Maybe system voltage? Always 3V */
150 CPCAP_ADC_HV_BATTP
, /* Another battery detection? */
151 CPCAP_ADC_TSX1_AD12
, /* Seems unused, for touchscreen? */
152 CPCAP_ADC_TSX2_AD13
, /* Seems unused, for touchscreen? */
153 CPCAP_ADC_TSY1_AD14
, /* Seems unused, for touchscreen? */
154 CPCAP_ADC_TSY2_AD15
, /* Seems unused, for touchscreen? */
156 /* Remuxed channels using bank0 entries */
157 CPCAP_ADC_BATTP_PI16
, /* Alternative mux mode for BATTP */
158 CPCAP_ADC_BATTI_PI17
, /* Alternative mux mode for BATTI */
160 CPCAP_ADC_CHANNEL_NUM
,
164 * enum cpcap_adc_timing - cpcap adc timing options
166 * CPCAP_ADC_TIMING_IMM seems to be immediate with no timings.
167 * Please document when using.
169 enum cpcap_adc_timing
{
170 CPCAP_ADC_TIMING_IMM
,
172 CPCAP_ADC_TIMING_OUT
,
176 * struct cpcap_adc_phasing_tbl - cpcap phasing table
177 * @offset: offset in the phasing table
178 * @multiplier: multiplier in the phasing table
179 * @divider: divider in the phasing table
180 * @min: minimum value
181 * @max: maximum value
183 struct cpcap_adc_phasing_tbl
{
185 unsigned short multiplier
;
186 unsigned short divider
;
192 * struct cpcap_adc_conversion_tbl - cpcap conversion table
193 * @conv_type: conversion type
194 * @align_offset: align offset
195 * @conv_offset: conversion offset
196 * @cal_offset: calibration offset
197 * @multiplier: conversion multiplier
198 * @divider: conversion divider
200 struct cpcap_adc_conversion_tbl
{
201 enum iio_chan_info_enum conv_type
;
210 * struct cpcap_adc_request - cpcap adc request
211 * @channel: request channel
212 * @phase_tbl: channel phasing table
213 * @conv_tbl: channel conversion table
214 * @bank_index: channel index within the bank
215 * @timing: timing settings
218 struct cpcap_adc_request
{
220 const struct cpcap_adc_phasing_tbl
*phase_tbl
;
221 const struct cpcap_adc_conversion_tbl
*conv_tbl
;
223 enum cpcap_adc_timing timing
;
227 /* Phasing table for channels. Note that channels 16 & 17 use BATTP and BATTI */
228 static const struct cpcap_adc_phasing_tbl bank_phasing
[] = {
230 [CPCAP_ADC_AD0
] = {0, 0x80, 0x80, 0, 1023},
231 [CPCAP_ADC_BATTP
] = {0, 0x80, 0x80, 0, 1023},
232 [CPCAP_ADC_VBUS
] = {0, 0x80, 0x80, 0, 1023},
233 [CPCAP_ADC_AD3
] = {0, 0x80, 0x80, 0, 1023},
234 [CPCAP_ADC_BPLUS_AD4
] = {0, 0x80, 0x80, 0, 1023},
235 [CPCAP_ADC_CHG_ISENSE
] = {0, 0x80, 0x80, -512, 511},
236 [CPCAP_ADC_BATTI
] = {0, 0x80, 0x80, -512, 511},
237 [CPCAP_ADC_USB_ID
] = {0, 0x80, 0x80, 0, 1023},
240 [CPCAP_ADC_AD8
] = {0, 0x80, 0x80, 0, 1023},
241 [CPCAP_ADC_AD9
] = {0, 0x80, 0x80, 0, 1023},
242 [CPCAP_ADC_LICELL
] = {0, 0x80, 0x80, 0, 1023},
243 [CPCAP_ADC_HV_BATTP
] = {0, 0x80, 0x80, 0, 1023},
244 [CPCAP_ADC_TSX1_AD12
] = {0, 0x80, 0x80, 0, 1023},
245 [CPCAP_ADC_TSX2_AD13
] = {0, 0x80, 0x80, 0, 1023},
246 [CPCAP_ADC_TSY1_AD14
] = {0, 0x80, 0x80, 0, 1023},
247 [CPCAP_ADC_TSY2_AD15
] = {0, 0x80, 0x80, 0, 1023},
251 * Conversion table for channels. Updated during init based on calibration.
252 * Here too channels 16 & 17 use BATTP and BATTI.
254 static struct cpcap_adc_conversion_tbl bank_conversion
[] = {
257 IIO_CHAN_INFO_PROCESSED
, 0, 0, 0, 1, 1,
259 [CPCAP_ADC_BATTP
] = {
260 IIO_CHAN_INFO_PROCESSED
, 0, 2400, 0, 2300, 1023,
263 IIO_CHAN_INFO_PROCESSED
, 0, 0, 0, 10000, 1023,
266 IIO_CHAN_INFO_PROCESSED
, 0, 0, 0, 1, 1,
268 [CPCAP_ADC_BPLUS_AD4
] = {
269 IIO_CHAN_INFO_PROCESSED
, 0, 2400, 0, 2300, 1023,
271 [CPCAP_ADC_CHG_ISENSE
] = {
272 IIO_CHAN_INFO_PROCESSED
, -512, 2, 0, 5000, 1023,
274 [CPCAP_ADC_BATTI
] = {
275 IIO_CHAN_INFO_PROCESSED
, -512, 2, 0, 5000, 1023,
277 [CPCAP_ADC_USB_ID
] = {
278 IIO_CHAN_INFO_RAW
, 0, 0, 0, 1, 1,
283 IIO_CHAN_INFO_RAW
, 0, 0, 0, 1, 1,
286 IIO_CHAN_INFO_RAW
, 0, 0, 0, 1, 1,
288 [CPCAP_ADC_LICELL
] = {
289 IIO_CHAN_INFO_PROCESSED
, 0, 0, 0, 3400, 1023,
291 [CPCAP_ADC_HV_BATTP
] = {
292 IIO_CHAN_INFO_RAW
, 0, 0, 0, 1, 1,
294 [CPCAP_ADC_TSX1_AD12
] = {
295 IIO_CHAN_INFO_RAW
, 0, 0, 0, 1, 1,
297 [CPCAP_ADC_TSX2_AD13
] = {
298 IIO_CHAN_INFO_RAW
, 0, 0, 0, 1, 1,
300 [CPCAP_ADC_TSY1_AD14
] = {
301 IIO_CHAN_INFO_RAW
, 0, 0, 0, 1, 1,
303 [CPCAP_ADC_TSY2_AD15
] = {
304 IIO_CHAN_INFO_RAW
, 0, 0, 0, 1, 1,
309 * Temperature lookup table of register values to milliCelcius.
310 * REVISIT: Check the duplicate 0x3ff entry in a freezer
312 static const int temp_map
[CPCAP_MAX_TEMP_LVL
][2] = {
342 #define CPCAP_CHAN(_type, _index, _address, _datasheet_name) { \
344 .address = (_address), \
346 .channel = (_index), \
347 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
348 BIT(IIO_CHAN_INFO_PROCESSED), \
349 .scan_index = (_index), \
354 .endianness = IIO_CPU, \
356 .datasheet_name = (_datasheet_name), \
360 * The datasheet names are from Motorola mapphone Linux kernel except
361 * for the last two which might be uncalibrated charge voltage and
364 static const struct iio_chan_spec cpcap_adc_channels
[] = {
366 CPCAP_CHAN(IIO_TEMP
, 0, CPCAP_REG_ADCD0
, "battdetb"),
367 CPCAP_CHAN(IIO_VOLTAGE
, 1, CPCAP_REG_ADCD1
, "battp"),
368 CPCAP_CHAN(IIO_VOLTAGE
, 2, CPCAP_REG_ADCD2
, "vbus"),
369 CPCAP_CHAN(IIO_TEMP
, 3, CPCAP_REG_ADCD3
, "ad3"),
370 CPCAP_CHAN(IIO_VOLTAGE
, 4, CPCAP_REG_ADCD4
, "ad4"),
371 CPCAP_CHAN(IIO_CURRENT
, 5, CPCAP_REG_ADCD5
, "chg_isense"),
372 CPCAP_CHAN(IIO_CURRENT
, 6, CPCAP_REG_ADCD6
, "batti"),
373 CPCAP_CHAN(IIO_VOLTAGE
, 7, CPCAP_REG_ADCD7
, "usb_id"),
376 CPCAP_CHAN(IIO_CURRENT
, 8, CPCAP_REG_ADCD0
, "ad8"),
377 CPCAP_CHAN(IIO_VOLTAGE
, 9, CPCAP_REG_ADCD1
, "ad9"),
378 CPCAP_CHAN(IIO_VOLTAGE
, 10, CPCAP_REG_ADCD2
, "licell"),
379 CPCAP_CHAN(IIO_VOLTAGE
, 11, CPCAP_REG_ADCD3
, "hv_battp"),
380 CPCAP_CHAN(IIO_VOLTAGE
, 12, CPCAP_REG_ADCD4
, "tsx1_ad12"),
381 CPCAP_CHAN(IIO_VOLTAGE
, 13, CPCAP_REG_ADCD5
, "tsx2_ad13"),
382 CPCAP_CHAN(IIO_VOLTAGE
, 14, CPCAP_REG_ADCD6
, "tsy1_ad14"),
383 CPCAP_CHAN(IIO_VOLTAGE
, 15, CPCAP_REG_ADCD7
, "tsy2_ad15"),
385 /* There are two registers with multiplexed functionality */
386 CPCAP_CHAN(IIO_VOLTAGE
, 16, CPCAP_REG_ADCD0
, "chg_vsense"),
387 CPCAP_CHAN(IIO_CURRENT
, 17, CPCAP_REG_ADCD1
, "batti2"),
390 static irqreturn_t
cpcap_adc_irq_thread(int irq
, void *data
)
392 struct iio_dev
*indio_dev
= data
;
393 struct cpcap_adc
*ddata
= iio_priv(indio_dev
);
396 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
397 CPCAP_BIT_ADTRIG_DIS
,
398 CPCAP_BIT_ADTRIG_DIS
);
403 wake_up_interruptible(&ddata
->wq_data_avail
);
408 /* ADC calibration functions */
409 static void cpcap_adc_setup_calibrate(struct cpcap_adc
*ddata
,
410 enum cpcap_adc_channel chan
)
412 unsigned int value
= 0;
413 unsigned long timeout
= jiffies
+ msecs_to_jiffies(3000);
416 if ((chan
!= CPCAP_ADC_CHG_ISENSE
) &&
417 (chan
!= CPCAP_ADC_BATTI
))
420 value
|= CPCAP_BIT_CAL_MODE
| CPCAP_BIT_RAND0
;
421 value
|= ((chan
<< 4) &
422 (CPCAP_BIT_ADA2
| CPCAP_BIT_ADA1
| CPCAP_BIT_ADA0
));
424 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC1
,
425 CPCAP_BIT_CAL_MODE
| CPCAP_BIT_ATOX
|
426 CPCAP_BIT_ATO3
| CPCAP_BIT_ATO2
|
427 CPCAP_BIT_ATO1
| CPCAP_BIT_ATO0
|
428 CPCAP_BIT_ADA2
| CPCAP_BIT_ADA1
|
429 CPCAP_BIT_ADA0
| CPCAP_BIT_AD_SEL1
|
430 CPCAP_BIT_RAND1
| CPCAP_BIT_RAND0
,
435 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
436 CPCAP_BIT_ATOX_PS_FACTOR
|
437 CPCAP_BIT_ADC_PS_FACTOR1
|
438 CPCAP_BIT_ADC_PS_FACTOR0
,
443 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
444 CPCAP_BIT_ADTRIG_DIS
,
445 CPCAP_BIT_ADTRIG_DIS
);
449 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
456 schedule_timeout_uninterruptible(1);
457 error
= regmap_read(ddata
->reg
, CPCAP_REG_ADCC2
, &value
);
460 } while ((value
& CPCAP_BIT_ASC
) && time_before(jiffies
, timeout
));
462 if (value
& CPCAP_BIT_ASC
)
464 "Timeout waiting for calibration to complete\n");
466 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC1
,
467 CPCAP_BIT_CAL_MODE
, 0);
472 static int cpcap_adc_calibrate_one(struct cpcap_adc
*ddata
,
474 u16 calibration_register
,
478 unsigned int calibration_data
[2];
479 unsigned short cal_data_diff
;
482 for (i
= 0; i
< CPCAP_ADC_MAX_RETRIES
; i
++) {
483 calibration_data
[0] = 0;
484 calibration_data
[1] = 0;
486 cpcap_adc_setup_calibrate(ddata
, channel
);
487 error
= regmap_read(ddata
->reg
, calibration_register
,
488 &calibration_data
[0]);
491 cpcap_adc_setup_calibrate(ddata
, channel
);
492 error
= regmap_read(ddata
->reg
, calibration_register
,
493 &calibration_data
[1]);
497 if (calibration_data
[0] > calibration_data
[1])
499 calibration_data
[0] - calibration_data
[1];
502 calibration_data
[1] - calibration_data
[0];
504 if (((calibration_data
[1] >= lower_threshold
) &&
505 (calibration_data
[1] <= upper_threshold
) &&
506 (cal_data_diff
<= ST_ADC_CALIBRATE_DIFF_THRESHOLD
)) ||
507 (ddata
->vendor
== CPCAP_VENDOR_TI
)) {
508 bank_conversion
[channel
].cal_offset
=
509 ((short)calibration_data
[1] * -1) + 512;
510 dev_dbg(ddata
->dev
, "ch%i calibration complete: %i\n",
511 channel
, bank_conversion
[channel
].cal_offset
);
514 usleep_range(5000, 10000);
520 static int cpcap_adc_calibrate(struct cpcap_adc
*ddata
)
524 error
= cpcap_adc_calibrate_one(ddata
, CPCAP_ADC_CHG_ISENSE
,
526 ST_ADC_CAL_CHRGI_LOW_THRESHOLD
,
527 ST_ADC_CAL_CHRGI_HIGH_THRESHOLD
);
531 error
= cpcap_adc_calibrate_one(ddata
, CPCAP_ADC_BATTI
,
533 ST_ADC_CAL_BATTI_LOW_THRESHOLD
,
534 ST_ADC_CAL_BATTI_HIGH_THRESHOLD
);
541 /* ADC setup, read and scale functions */
542 static void cpcap_adc_setup_bank(struct cpcap_adc
*ddata
,
543 struct cpcap_adc_request
*req
)
545 const struct cpcap_adc_ato
*ato
= ddata
->ato
;
546 unsigned short value1
= 0;
547 unsigned short value2
= 0;
553 switch (req
->channel
) {
555 value2
|= CPCAP_BIT_THERMBIAS_EN
;
556 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
557 CPCAP_BIT_THERMBIAS_EN
,
561 usleep_range(800, 1000);
563 case CPCAP_ADC_AD8
... CPCAP_ADC_TSY2_AD15
:
564 value1
|= CPCAP_BIT_AD_SEL1
;
566 case CPCAP_ADC_BATTP_PI16
... CPCAP_ADC_BATTI_PI17
:
567 value1
|= CPCAP_BIT_RAND1
;
572 switch (req
->timing
) {
573 case CPCAP_ADC_TIMING_IN
:
574 value1
|= ato
->ato_in
;
575 value1
|= ato
->atox_in
;
576 value2
|= ato
->adc_ps_factor_in
;
577 value2
|= ato
->atox_ps_factor_in
;
579 case CPCAP_ADC_TIMING_OUT
:
580 value1
|= ato
->ato_out
;
581 value1
|= ato
->atox_out
;
582 value2
|= ato
->adc_ps_factor_out
;
583 value2
|= ato
->atox_ps_factor_out
;
586 case CPCAP_ADC_TIMING_IMM
:
591 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC1
,
592 CPCAP_BIT_CAL_MODE
| CPCAP_BIT_ATOX
|
593 CPCAP_BIT_ATO3
| CPCAP_BIT_ATO2
|
594 CPCAP_BIT_ATO1
| CPCAP_BIT_ATO0
|
595 CPCAP_BIT_ADA2
| CPCAP_BIT_ADA1
|
596 CPCAP_BIT_ADA0
| CPCAP_BIT_AD_SEL1
|
597 CPCAP_BIT_RAND1
| CPCAP_BIT_RAND0
,
602 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
603 CPCAP_BIT_ATOX_PS_FACTOR
|
604 CPCAP_BIT_ADC_PS_FACTOR1
|
605 CPCAP_BIT_ADC_PS_FACTOR0
|
606 CPCAP_BIT_THERMBIAS_EN
,
611 if (req
->timing
== CPCAP_ADC_TIMING_IMM
) {
612 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
613 CPCAP_BIT_ADTRIG_DIS
,
614 CPCAP_BIT_ADTRIG_DIS
);
618 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
624 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
625 CPCAP_BIT_ADTRIG_ONESHOT
,
626 CPCAP_BIT_ADTRIG_ONESHOT
);
630 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
631 CPCAP_BIT_ADTRIG_DIS
, 0);
637 static int cpcap_adc_start_bank(struct cpcap_adc
*ddata
,
638 struct cpcap_adc_request
*req
)
642 req
->timing
= CPCAP_ADC_TIMING_IMM
;
645 for (i
= 0; i
< CPCAP_ADC_MAX_RETRIES
; i
++) {
646 cpcap_adc_setup_bank(ddata
, req
);
647 error
= wait_event_interruptible_timeout(ddata
->wq_data_avail
,
649 msecs_to_jiffies(50));
665 static int cpcap_adc_stop_bank(struct cpcap_adc
*ddata
)
669 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC1
,
671 CPCAP_REG_ADCC1_DEFAULTS
);
675 return regmap_update_bits(ddata
->reg
, CPCAP_REG_ADCC2
,
677 CPCAP_REG_ADCC2_DEFAULTS
);
680 static void cpcap_adc_phase(struct cpcap_adc_request
*req
)
682 const struct cpcap_adc_conversion_tbl
*conv_tbl
= req
->conv_tbl
;
683 const struct cpcap_adc_phasing_tbl
*phase_tbl
= req
->phase_tbl
;
684 int index
= req
->channel
;
686 /* Remuxed channels 16 and 17 use BATTP and BATTI entries */
687 switch (req
->channel
) {
688 case CPCAP_ADC_BATTP
:
689 case CPCAP_ADC_BATTP_PI16
:
690 index
= req
->bank_index
;
691 req
->result
-= phase_tbl
[index
].offset
;
692 req
->result
-= CPCAP_FOUR_POINT_TWO_ADC
;
693 req
->result
*= phase_tbl
[index
].multiplier
;
694 if (phase_tbl
[index
].divider
== 0)
696 req
->result
/= phase_tbl
[index
].divider
;
697 req
->result
+= CPCAP_FOUR_POINT_TWO_ADC
;
699 case CPCAP_ADC_BATTI_PI17
:
700 index
= req
->bank_index
;
703 req
->result
+= conv_tbl
[index
].cal_offset
;
704 req
->result
+= conv_tbl
[index
].align_offset
;
705 req
->result
*= phase_tbl
[index
].multiplier
;
706 if (phase_tbl
[index
].divider
== 0)
708 req
->result
/= phase_tbl
[index
].divider
;
709 req
->result
+= phase_tbl
[index
].offset
;
713 if (req
->result
< phase_tbl
[index
].min
)
714 req
->result
= phase_tbl
[index
].min
;
715 else if (req
->result
> phase_tbl
[index
].max
)
716 req
->result
= phase_tbl
[index
].max
;
719 /* Looks up temperatures in a table and calculates averages if needed */
720 static int cpcap_adc_table_to_millicelcius(unsigned short value
)
722 int i
, result
= 0, alpha
;
724 if (value
<= temp_map
[CPCAP_MAX_TEMP_LVL
- 1][0])
725 return temp_map
[CPCAP_MAX_TEMP_LVL
- 1][1];
727 if (value
>= temp_map
[0][0])
728 return temp_map
[0][1];
730 for (i
= 0; i
< CPCAP_MAX_TEMP_LVL
- 1; i
++) {
731 if ((value
<= temp_map
[i
][0]) &&
732 (value
>= temp_map
[i
+ 1][0])) {
733 if (value
== temp_map
[i
][0]) {
734 result
= temp_map
[i
][1];
735 } else if (value
== temp_map
[i
+ 1][0]) {
736 result
= temp_map
[i
+ 1][1];
738 alpha
= ((value
- temp_map
[i
][0]) * 1000) /
739 (temp_map
[i
+ 1][0] - temp_map
[i
][0]);
741 result
= temp_map
[i
][1] +
742 ((alpha
* (temp_map
[i
+ 1][1] -
743 temp_map
[i
][1])) / 1000);
752 static void cpcap_adc_convert(struct cpcap_adc_request
*req
)
754 const struct cpcap_adc_conversion_tbl
*conv_tbl
= req
->conv_tbl
;
755 int index
= req
->channel
;
757 /* Remuxed channels 16 and 17 use BATTP and BATTI entries */
758 switch (req
->channel
) {
759 case CPCAP_ADC_BATTP_PI16
:
760 index
= CPCAP_ADC_BATTP
;
762 case CPCAP_ADC_BATTI_PI17
:
763 index
= CPCAP_ADC_BATTI
;
769 /* No conversion for raw channels */
770 if (conv_tbl
[index
].conv_type
== IIO_CHAN_INFO_RAW
)
773 /* Temperatures use a lookup table instead of conversion table */
774 if ((req
->channel
== CPCAP_ADC_AD0
) ||
775 (req
->channel
== CPCAP_ADC_AD3
)) {
777 cpcap_adc_table_to_millicelcius(req
->result
);
782 /* All processed channels use a conversion table */
783 req
->result
*= conv_tbl
[index
].multiplier
;
784 if (conv_tbl
[index
].divider
== 0)
786 req
->result
/= conv_tbl
[index
].divider
;
787 req
->result
+= conv_tbl
[index
].conv_offset
;
791 * REVISIT: Check if timed sampling can use multiple channels at the
792 * same time. If not, replace channel_mask with just channel.
794 static int cpcap_adc_read_bank_scaled(struct cpcap_adc
*ddata
,
795 struct cpcap_adc_request
*req
)
797 int calibration_data
, error
, addr
;
799 if (ddata
->vendor
== CPCAP_VENDOR_TI
) {
800 error
= regmap_read(ddata
->reg
, CPCAP_REG_ADCAL1
,
804 bank_conversion
[CPCAP_ADC_CHG_ISENSE
].cal_offset
=
805 ((short)calibration_data
* -1) + 512;
807 error
= regmap_read(ddata
->reg
, CPCAP_REG_ADCAL2
,
811 bank_conversion
[CPCAP_ADC_BATTI
].cal_offset
=
812 ((short)calibration_data
* -1) + 512;
815 addr
= CPCAP_REG_ADCD0
+ req
->bank_index
* 4;
817 error
= regmap_read(ddata
->reg
, addr
, &req
->result
);
821 req
->result
&= 0x3ff;
822 cpcap_adc_phase(req
);
823 cpcap_adc_convert(req
);
828 static int cpcap_adc_init_request(struct cpcap_adc_request
*req
,
831 req
->channel
= channel
;
832 req
->phase_tbl
= bank_phasing
;
833 req
->conv_tbl
= bank_conversion
;
836 case CPCAP_ADC_AD0
... CPCAP_ADC_USB_ID
:
837 req
->bank_index
= channel
;
839 case CPCAP_ADC_AD8
... CPCAP_ADC_TSY2_AD15
:
840 req
->bank_index
= channel
- 8;
842 case CPCAP_ADC_BATTP_PI16
:
843 req
->bank_index
= CPCAP_ADC_BATTP
;
845 case CPCAP_ADC_BATTI_PI17
:
846 req
->bank_index
= CPCAP_ADC_BATTI
;
855 static int cpcap_adc_read_st_die_temp(struct cpcap_adc
*ddata
,
860 error
= regmap_read(ddata
->reg
, addr
, val
);
871 static int cpcap_adc_read(struct iio_dev
*indio_dev
,
872 struct iio_chan_spec
const *chan
,
873 int *val
, int *val2
, long mask
)
875 struct cpcap_adc
*ddata
= iio_priv(indio_dev
);
876 struct cpcap_adc_request req
;
879 error
= cpcap_adc_init_request(&req
, chan
->channel
);
884 case IIO_CHAN_INFO_RAW
:
885 mutex_lock(&ddata
->lock
);
886 error
= cpcap_adc_start_bank(ddata
, &req
);
889 error
= regmap_read(ddata
->reg
, chan
->address
, val
);
892 error
= cpcap_adc_stop_bank(ddata
);
895 mutex_unlock(&ddata
->lock
);
897 case IIO_CHAN_INFO_PROCESSED
:
898 mutex_lock(&ddata
->lock
);
899 error
= cpcap_adc_start_bank(ddata
, &req
);
902 if ((ddata
->vendor
== CPCAP_VENDOR_ST
) &&
903 (chan
->channel
== CPCAP_ADC_AD3
)) {
904 error
= cpcap_adc_read_st_die_temp(ddata
,
910 error
= cpcap_adc_read_bank_scaled(ddata
, &req
);
914 error
= cpcap_adc_stop_bank(ddata
);
917 mutex_unlock(&ddata
->lock
);
927 mutex_unlock(&ddata
->lock
);
928 dev_err(ddata
->dev
, "error reading ADC: %i\n", error
);
933 static const struct iio_info cpcap_adc_info
= {
934 .read_raw
= &cpcap_adc_read
,
938 * Configuration for Motorola mapphone series such as droid 4.
939 * Copied from the Motorola mapphone kernel tree.
941 static const struct cpcap_adc_ato mapphone_adc
= {
944 .adc_ps_factor_in
= 0x0200,
945 .atox_ps_factor_in
= 0,
948 .adc_ps_factor_out
= 0,
949 .atox_ps_factor_out
= 0,
952 static const struct of_device_id cpcap_adc_id_table
[] = {
954 .compatible
= "motorola,cpcap-adc",
957 .compatible
= "motorola,mapphone-cpcap-adc",
958 .data
= &mapphone_adc
,
962 MODULE_DEVICE_TABLE(of
, cpcap_adc_id_table
);
964 static int cpcap_adc_probe(struct platform_device
*pdev
)
966 const struct of_device_id
*match
;
967 struct cpcap_adc
*ddata
;
968 struct iio_dev
*indio_dev
;
971 match
= of_match_device(of_match_ptr(cpcap_adc_id_table
),
977 dev_err(&pdev
->dev
, "no configuration data found\n");
982 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*ddata
));
984 dev_err(&pdev
->dev
, "failed to allocate iio device\n");
988 ddata
= iio_priv(indio_dev
);
989 ddata
->ato
= match
->data
;
990 ddata
->dev
= &pdev
->dev
;
992 mutex_init(&ddata
->lock
);
993 init_waitqueue_head(&ddata
->wq_data_avail
);
995 indio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
996 indio_dev
->dev
.parent
= &pdev
->dev
;
997 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
998 indio_dev
->channels
= cpcap_adc_channels
;
999 indio_dev
->num_channels
= ARRAY_SIZE(cpcap_adc_channels
);
1000 indio_dev
->name
= dev_name(&pdev
->dev
);
1001 indio_dev
->info
= &cpcap_adc_info
;
1003 ddata
->reg
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
1007 error
= cpcap_get_vendor(ddata
->dev
, ddata
->reg
, &ddata
->vendor
);
1011 platform_set_drvdata(pdev
, indio_dev
);
1013 ddata
->irq
= platform_get_irq_byname(pdev
, "adcdone");
1017 error
= devm_request_threaded_irq(&pdev
->dev
, ddata
->irq
, NULL
,
1018 cpcap_adc_irq_thread
,
1020 "cpcap-adc", indio_dev
);
1022 dev_err(&pdev
->dev
, "could not get irq: %i\n",
1028 error
= cpcap_adc_calibrate(ddata
);
1032 dev_info(&pdev
->dev
, "CPCAP ADC device probed\n");
1034 return devm_iio_device_register(&pdev
->dev
, indio_dev
);
1037 static struct platform_driver cpcap_adc_driver
= {
1039 .name
= "cpcap_adc",
1040 .of_match_table
= of_match_ptr(cpcap_adc_id_table
),
1042 .probe
= cpcap_adc_probe
,
1045 module_platform_driver(cpcap_adc_driver
);
1047 MODULE_ALIAS("platform:cpcap_adc");
1048 MODULE_DESCRIPTION("CPCAP ADC driver");
1049 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com");
1050 MODULE_LICENSE("GPL v2");