blob: 73cf7964a5fe44ebc376f3e7bb74f2786a0bcbfb [file] [log] [blame]
Tony Lindgren25ec2492017-03-23 20:38:42 -07001/*
2 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
3 *
4 * Rewritten for Linux IIO framework with some code based on
5 * earlier driver found in the Motorola Linux kernel:
6 *
7 * Copyright (C) 2009-2010 Motorola, Inc.
8 *
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.
12 *
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.
17 */
18
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>
26#include <linux/of.h>
27#include <linux/of_platform.h>
28#include <linux/platform_device.h>
29#include <linux/regmap.h>
30
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>
36
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 */
54
Tony Lindgren3f9f3a12017-05-22 17:51:44 -070055#define CPCAP_REG_ADCC1_DEFAULTS (CPCAP_BIT_ADEN_AUTO_CLR | \
56 CPCAP_BIT_ADC_CLK_SEL0 | \
57 CPCAP_BIT_RAND1)
58
Tony Lindgren25ec2492017-03-23 20:38:42 -070059/* 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 */
Tony Lindgren3f9f3a12017-05-22 17:51:44 -070069#define CPCAP_BIT_THERMBIAS_EN BIT(6) /* Bias for AD0_BATTDETB */
Tony Lindgren25ec2492017-03-23 20:38:42 -070070#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 */
76
Tony Lindgren3f9f3a12017-05-22 17:51:44 -070077#define CPCAP_REG_ADCC2_DEFAULTS (CPCAP_BIT_AD4_SELECT | \
78 CPCAP_BIT_ADTRIG_DIS | \
79 CPCAP_BIT_LIADC | \
80 CPCAP_BIT_TS_M2 | \
81 CPCAP_BIT_TS_M1)
82
Tony Lindgren25ec2492017-03-23 20:38:42 -070083#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
90
91#define CPCAP_ADC_MAX_RETRIES 5 /* Calibration and quirk */
92
93/**
94 * struct cpcap_adc_ato - timing settings for cpcap adc
95 *
96 * Unfortunately no cpcap documentation available, please document when
97 * using these.
98 */
99struct 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;
108};
109
110/**
111 * struct cpcap-adc - cpcap adc device driver data
112 * @reg: cpcap regmap
113 * @dev: struct device
114 * @vendor: cpcap vendor
115 * @irq: interrupt
116 * @lock: mutex
117 * @ato: request timings
118 * @wq_data_avail: work queue
119 * @done: work done
120 */
121struct cpcap_adc {
122 struct regmap *reg;
123 struct device *dev;
124 u16 vendor;
125 int irq;
126 struct mutex lock; /* ADC register access lock */
127 const struct cpcap_adc_ato *ato;
128 wait_queue_head_t wq_data_avail;
129 bool done;
130};
131
132/**
133 * enum cpcap_adc_channel - cpcap adc channels
134 */
135enum cpcap_adc_channel {
136 /* Bank0 channels */
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700137 CPCAP_ADC_AD0, /* Battery temperature */
Tony Lindgren25ec2492017-03-23 20:38:42 -0700138 CPCAP_ADC_BATTP, /* Battery voltage */
139 CPCAP_ADC_VBUS, /* USB VBUS voltage */
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700140 CPCAP_ADC_AD3, /* Die temperature when charging */
Tony Lindgren25ec2492017-03-23 20:38:42 -0700141 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? */
145
146 /* Bank1 channels */
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? */
155
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 */
159
160 CPCAP_ADC_CHANNEL_NUM,
161};
162
163/**
164 * enum cpcap_adc_timing - cpcap adc timing options
165 *
166 * CPCAP_ADC_TIMING_IMM seems to be immediate with no timings.
167 * Please document when using.
168 */
169enum cpcap_adc_timing {
170 CPCAP_ADC_TIMING_IMM,
171 CPCAP_ADC_TIMING_IN,
172 CPCAP_ADC_TIMING_OUT,
173};
174
175/**
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
182 */
183struct cpcap_adc_phasing_tbl {
184 short offset;
185 unsigned short multiplier;
186 unsigned short divider;
187 short min;
188 short max;
189};
190
191/**
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
199 */
200struct cpcap_adc_conversion_tbl {
201 enum iio_chan_info_enum conv_type;
202 int align_offset;
203 int conv_offset;
204 int cal_offset;
205 int multiplier;
206 int divider;
207};
208
209/**
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
216 * @result: result
217 */
218struct cpcap_adc_request {
219 int channel;
220 const struct cpcap_adc_phasing_tbl *phase_tbl;
221 const struct cpcap_adc_conversion_tbl *conv_tbl;
222 int bank_index;
223 enum cpcap_adc_timing timing;
224 int result;
225};
226
227/* Phasing table for channels. Note that channels 16 & 17 use BATTP and BATTI */
228static const struct cpcap_adc_phasing_tbl bank_phasing[] = {
229 /* Bank0 */
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700230 [CPCAP_ADC_AD0] = {0, 0x80, 0x80, 0, 1023},
Tony Lindgren25ec2492017-03-23 20:38:42 -0700231 [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},
238
239 /* Bank1 */
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},
248};
249
250/*
251 * Conversion table for channels. Updated during init based on calibration.
252 * Here too channels 16 & 17 use BATTP and BATTI.
253 */
254static struct cpcap_adc_conversion_tbl bank_conversion[] = {
255 /* Bank0 */
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700256 [CPCAP_ADC_AD0] = {
Tony Lindgren25ec2492017-03-23 20:38:42 -0700257 IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 1, 1,
258 },
259 [CPCAP_ADC_BATTP] = {
260 IIO_CHAN_INFO_PROCESSED, 0, 2400, 0, 2300, 1023,
261 },
262 [CPCAP_ADC_VBUS] = {
263 IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 10000, 1023,
264 },
265 [CPCAP_ADC_AD3] = {
266 IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 1, 1,
267 },
268 [CPCAP_ADC_BPLUS_AD4] = {
269 IIO_CHAN_INFO_PROCESSED, 0, 2400, 0, 2300, 1023,
270 },
271 [CPCAP_ADC_CHG_ISENSE] = {
272 IIO_CHAN_INFO_PROCESSED, -512, 2, 0, 5000, 1023,
273 },
274 [CPCAP_ADC_BATTI] = {
275 IIO_CHAN_INFO_PROCESSED, -512, 2, 0, 5000, 1023,
276 },
277 [CPCAP_ADC_USB_ID] = {
278 IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
279 },
280
281 /* Bank1 */
282 [CPCAP_ADC_AD8] = {
283 IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
284 },
285 [CPCAP_ADC_AD9] = {
286 IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
287 },
288 [CPCAP_ADC_LICELL] = {
289 IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 3400, 1023,
290 },
291 [CPCAP_ADC_HV_BATTP] = {
292 IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
293 },
294 [CPCAP_ADC_TSX1_AD12] = {
295 IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
296 },
297 [CPCAP_ADC_TSX2_AD13] = {
298 IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
299 },
300 [CPCAP_ADC_TSY1_AD14] = {
301 IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
302 },
303 [CPCAP_ADC_TSY2_AD15] = {
304 IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1,
305 },
306};
307
308/*
309 * Temperature lookup table of register values to milliCelcius.
310 * REVISIT: Check the duplicate 0x3ff entry in a freezer
311 */
312static const int temp_map[CPCAP_MAX_TEMP_LVL][2] = {
313 { 0x03ff, -40000 },
314 { 0x03ff, -35000 },
315 { 0x03ef, -30000 },
316 { 0x03b2, -25000 },
317 { 0x036c, -20000 },
318 { 0x0320, -15000 },
319 { 0x02d0, -10000 },
320 { 0x027f, -5000 },
321 { 0x022f, 0 },
322 { 0x01e4, 5000 },
323 { 0x019f, 10000 },
324 { 0x0161, 15000 },
325 { 0x012b, 20000 },
326 { 0x00fc, 25000 },
327 { 0x00d4, 30000 },
328 { 0x00b2, 35000 },
329 { 0x0095, 40000 },
330 { 0x007d, 45000 },
331 { 0x0069, 50000 },
332 { 0x0059, 55000 },
333 { 0x004b, 60000 },
334 { 0x003f, 65000 },
335 { 0x0036, 70000 },
336 { 0x002e, 75000 },
337 { 0x0027, 80000 },
338 { 0x0022, 85000 },
339 { 0x001d, 90000 },
340};
341
342#define CPCAP_CHAN(_type, _index, _address, _datasheet_name) { \
343 .type = (_type), \
344 .address = (_address), \
345 .indexed = 1, \
346 .channel = (_index), \
347 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
348 BIT(IIO_CHAN_INFO_PROCESSED), \
349 .scan_index = (_index), \
350 .scan_type = { \
351 .sign = 'u', \
352 .realbits = 10, \
353 .storagebits = 16, \
354 .endianness = IIO_CPU, \
355 }, \
356 .datasheet_name = (_datasheet_name), \
357}
358
359/*
360 * The datasheet names are from Motorola mapphone Linux kernel except
361 * for the last two which might be uncalibrated charge voltage and
362 * current.
363 */
364static const struct iio_chan_spec cpcap_adc_channels[] = {
365 /* Bank0 */
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"),
374
375 /* Bank1 */
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"),
384
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"),
388};
389
390static irqreturn_t cpcap_adc_irq_thread(int irq, void *data)
391{
392 struct iio_dev *indio_dev = data;
393 struct cpcap_adc *ddata = iio_priv(indio_dev);
394 int error;
395
396 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
397 CPCAP_BIT_ADTRIG_DIS,
398 CPCAP_BIT_ADTRIG_DIS);
399 if (error)
400 return IRQ_NONE;
401
402 ddata->done = true;
403 wake_up_interruptible(&ddata->wq_data_avail);
404
405 return IRQ_HANDLED;
406}
407
408/* ADC calibration functions */
409static void cpcap_adc_setup_calibrate(struct cpcap_adc *ddata,
410 enum cpcap_adc_channel chan)
411{
412 unsigned int value = 0;
413 unsigned long timeout = jiffies + msecs_to_jiffies(3000);
414 int error;
415
416 if ((chan != CPCAP_ADC_CHG_ISENSE) &&
417 (chan != CPCAP_ADC_BATTI))
418 return;
419
420 value |= CPCAP_BIT_CAL_MODE | CPCAP_BIT_RAND0;
421 value |= ((chan << 4) &
422 (CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 | CPCAP_BIT_ADA0));
423
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,
431 value);
432 if (error)
433 return;
434
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,
439 0);
440 if (error)
441 return;
442
443 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
444 CPCAP_BIT_ADTRIG_DIS,
445 CPCAP_BIT_ADTRIG_DIS);
446 if (error)
447 return;
448
449 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
450 CPCAP_BIT_ASC,
451 CPCAP_BIT_ASC);
452 if (error)
453 return;
454
455 do {
456 schedule_timeout_uninterruptible(1);
457 error = regmap_read(ddata->reg, CPCAP_REG_ADCC2, &value);
458 if (error)
459 return;
460 } while ((value & CPCAP_BIT_ASC) && time_before(jiffies, timeout));
461
462 if (value & CPCAP_BIT_ASC)
463 dev_err(ddata->dev,
464 "Timeout waiting for calibration to complete\n");
465
466 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
467 CPCAP_BIT_CAL_MODE, 0);
468 if (error)
469 return;
470}
471
472static int cpcap_adc_calibrate_one(struct cpcap_adc *ddata,
473 int channel,
474 u16 calibration_register,
475 int lower_threshold,
476 int upper_threshold)
477{
478 unsigned int calibration_data[2];
479 unsigned short cal_data_diff;
480 int i, error;
481
482 for (i = 0; i < CPCAP_ADC_MAX_RETRIES; i++) {
483 calibration_data[0] = 0;
484 calibration_data[1] = 0;
485 cal_data_diff = 0;
486 cpcap_adc_setup_calibrate(ddata, channel);
487 error = regmap_read(ddata->reg, calibration_register,
488 &calibration_data[0]);
489 if (error)
490 return error;
491 cpcap_adc_setup_calibrate(ddata, channel);
492 error = regmap_read(ddata->reg, calibration_register,
493 &calibration_data[1]);
494 if (error)
495 return error;
496
497 if (calibration_data[0] > calibration_data[1])
498 cal_data_diff =
499 calibration_data[0] - calibration_data[1];
500 else
501 cal_data_diff =
502 calibration_data[1] - calibration_data[0];
503
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);
512 break;
513 }
514 usleep_range(5000, 10000);
515 }
516
517 return 0;
518}
519
520static int cpcap_adc_calibrate(struct cpcap_adc *ddata)
521{
522 int error;
523
524 error = cpcap_adc_calibrate_one(ddata, CPCAP_ADC_CHG_ISENSE,
525 CPCAP_REG_ADCAL1,
526 ST_ADC_CAL_CHRGI_LOW_THRESHOLD,
527 ST_ADC_CAL_CHRGI_HIGH_THRESHOLD);
528 if (error)
529 return error;
530
531 error = cpcap_adc_calibrate_one(ddata, CPCAP_ADC_BATTI,
532 CPCAP_REG_ADCAL2,
533 ST_ADC_CAL_BATTI_LOW_THRESHOLD,
534 ST_ADC_CAL_BATTI_HIGH_THRESHOLD);
535 if (error)
536 return error;
537
538 return 0;
539}
540
541/* ADC setup, read and scale functions */
542static void cpcap_adc_setup_bank(struct cpcap_adc *ddata,
543 struct cpcap_adc_request *req)
544{
545 const struct cpcap_adc_ato *ato = ddata->ato;
546 unsigned short value1 = 0;
547 unsigned short value2 = 0;
548 int error;
549
550 if (!ato)
551 return;
552
553 switch (req->channel) {
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700554 case CPCAP_ADC_AD0:
555 value2 |= CPCAP_BIT_THERMBIAS_EN;
556 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
557 CPCAP_BIT_THERMBIAS_EN,
558 value2);
559 if (error)
560 return;
561 usleep_range(800, 1000);
562 break;
Tony Lindgren25ec2492017-03-23 20:38:42 -0700563 case CPCAP_ADC_AD8 ... CPCAP_ADC_TSY2_AD15:
564 value1 |= CPCAP_BIT_AD_SEL1;
565 break;
566 case CPCAP_ADC_BATTP_PI16 ... CPCAP_ADC_BATTI_PI17:
567 value1 |= CPCAP_BIT_RAND1;
568 default:
569 break;
570 }
571
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;
578 break;
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;
584 break;
585
586 case CPCAP_ADC_TIMING_IMM:
587 default:
588 break;
589 }
590
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,
598 value1);
599 if (error)
600 return;
601
602 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
603 CPCAP_BIT_ATOX_PS_FACTOR |
604 CPCAP_BIT_ADC_PS_FACTOR1 |
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700605 CPCAP_BIT_ADC_PS_FACTOR0 |
606 CPCAP_BIT_THERMBIAS_EN,
Tony Lindgren25ec2492017-03-23 20:38:42 -0700607 value2);
608 if (error)
609 return;
610
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);
615 if (error)
616 return;
617
618 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
619 CPCAP_BIT_ASC,
620 CPCAP_BIT_ASC);
621 if (error)
622 return;
623 } else {
624 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
625 CPCAP_BIT_ADTRIG_ONESHOT,
626 CPCAP_BIT_ADTRIG_ONESHOT);
627 if (error)
628 return;
629
630 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
631 CPCAP_BIT_ADTRIG_DIS, 0);
632 if (error)
633 return;
634 }
635}
636
637/*
638 * Occasionally the ADC does not seem to start and there will be no
639 * interrupt. Let's re-init interrupt to prevent the ADC from hanging
640 * for the next request. It is unclear why this happens, but the next
641 * request will usually work after doing this.
642 */
643static void cpcap_adc_quirk_reset_lost_irq(struct cpcap_adc *ddata)
644{
645 int error;
646
647 dev_info(ddata->dev, "lost ADC irq, attempting to reinit\n");
648 disable_irq(ddata->irq);
649 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
650 CPCAP_BIT_ADTRIG_DIS,
651 CPCAP_BIT_ADTRIG_DIS);
652 if (error)
653 dev_warn(ddata->dev, "%s reset failed: %i\n",
654 __func__, error);
655 enable_irq(ddata->irq);
656}
657
658static int cpcap_adc_start_bank(struct cpcap_adc *ddata,
659 struct cpcap_adc_request *req)
660{
661 int i, error;
662
663 req->timing = CPCAP_ADC_TIMING_IMM;
664 ddata->done = false;
665
666 for (i = 0; i < CPCAP_ADC_MAX_RETRIES; i++) {
667 cpcap_adc_setup_bank(ddata, req);
668 error = wait_event_interruptible_timeout(ddata->wq_data_avail,
669 ddata->done,
670 msecs_to_jiffies(50));
671 if (error > 0)
672 return 0;
673
674 if (error == 0) {
675 cpcap_adc_quirk_reset_lost_irq(ddata);
676 error = -ETIMEDOUT;
677 continue;
678 }
679
680 if (error < 0)
681 return error;
682 }
683
684 return error;
685}
686
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700687static int cpcap_adc_stop_bank(struct cpcap_adc *ddata)
688{
689 int error;
690
691 error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1,
692 0xffff,
693 CPCAP_REG_ADCC1_DEFAULTS);
694 if (error)
695 return error;
696
697 return regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2,
698 0xffff,
699 CPCAP_REG_ADCC2_DEFAULTS);
700}
701
Tony Lindgren25ec2492017-03-23 20:38:42 -0700702static void cpcap_adc_phase(struct cpcap_adc_request *req)
703{
704 const struct cpcap_adc_conversion_tbl *conv_tbl = req->conv_tbl;
705 const struct cpcap_adc_phasing_tbl *phase_tbl = req->phase_tbl;
706 int index = req->channel;
707
708 /* Remuxed channels 16 and 17 use BATTP and BATTI entries */
709 switch (req->channel) {
710 case CPCAP_ADC_BATTP:
711 case CPCAP_ADC_BATTP_PI16:
712 index = req->bank_index;
713 req->result -= phase_tbl[index].offset;
714 req->result -= CPCAP_FOUR_POINT_TWO_ADC;
715 req->result *= phase_tbl[index].multiplier;
716 if (phase_tbl[index].divider == 0)
717 return;
718 req->result /= phase_tbl[index].divider;
719 req->result += CPCAP_FOUR_POINT_TWO_ADC;
720 break;
721 case CPCAP_ADC_BATTI_PI17:
722 index = req->bank_index;
723 /* fallthrough */
724 default:
725 req->result += conv_tbl[index].cal_offset;
726 req->result += conv_tbl[index].align_offset;
727 req->result *= phase_tbl[index].multiplier;
728 if (phase_tbl[index].divider == 0)
729 return;
730 req->result /= phase_tbl[index].divider;
731 req->result += phase_tbl[index].offset;
732 break;
733 }
734
735 if (req->result < phase_tbl[index].min)
736 req->result = phase_tbl[index].min;
737 else if (req->result > phase_tbl[index].max)
738 req->result = phase_tbl[index].max;
739}
740
741/* Looks up temperatures in a table and calculates averages if needed */
742static int cpcap_adc_table_to_millicelcius(unsigned short value)
743{
744 int i, result = 0, alpha;
745
746 if (value <= temp_map[CPCAP_MAX_TEMP_LVL - 1][0])
747 return temp_map[CPCAP_MAX_TEMP_LVL - 1][1];
748
749 if (value >= temp_map[0][0])
750 return temp_map[0][1];
751
752 for (i = 0; i < CPCAP_MAX_TEMP_LVL - 1; i++) {
753 if ((value <= temp_map[i][0]) &&
754 (value >= temp_map[i + 1][0])) {
755 if (value == temp_map[i][0]) {
756 result = temp_map[i][1];
757 } else if (value == temp_map[i + 1][0]) {
758 result = temp_map[i + 1][1];
759 } else {
760 alpha = ((value - temp_map[i][0]) * 1000) /
761 (temp_map[i + 1][0] - temp_map[i][0]);
762
763 result = temp_map[i][1] +
764 ((alpha * (temp_map[i + 1][1] -
765 temp_map[i][1])) / 1000);
766 }
767 break;
768 }
769 }
770
771 return result;
772}
773
774static void cpcap_adc_convert(struct cpcap_adc_request *req)
775{
776 const struct cpcap_adc_conversion_tbl *conv_tbl = req->conv_tbl;
777 int index = req->channel;
778
779 /* Remuxed channels 16 and 17 use BATTP and BATTI entries */
780 switch (req->channel) {
781 case CPCAP_ADC_BATTP_PI16:
782 index = CPCAP_ADC_BATTP;
783 break;
784 case CPCAP_ADC_BATTI_PI17:
785 index = CPCAP_ADC_BATTI;
786 break;
787 default:
788 break;
789 }
790
791 /* No conversion for raw channels */
792 if (conv_tbl[index].conv_type == IIO_CHAN_INFO_RAW)
793 return;
794
795 /* Temperatures use a lookup table instead of conversion table */
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700796 if ((req->channel == CPCAP_ADC_AD0) ||
Tony Lindgren25ec2492017-03-23 20:38:42 -0700797 (req->channel == CPCAP_ADC_AD3)) {
798 req->result =
799 cpcap_adc_table_to_millicelcius(req->result);
800
801 return;
802 }
803
804 /* All processed channels use a conversion table */
805 req->result *= conv_tbl[index].multiplier;
806 if (conv_tbl[index].divider == 0)
807 return;
808 req->result /= conv_tbl[index].divider;
809 req->result += conv_tbl[index].conv_offset;
810}
811
812/*
813 * REVISIT: Check if timed sampling can use multiple channels at the
814 * same time. If not, replace channel_mask with just channel.
815 */
816static int cpcap_adc_read_bank_scaled(struct cpcap_adc *ddata,
817 struct cpcap_adc_request *req)
818{
819 int calibration_data, error, addr;
820
821 if (ddata->vendor == CPCAP_VENDOR_TI) {
822 error = regmap_read(ddata->reg, CPCAP_REG_ADCAL1,
823 &calibration_data);
824 if (error)
825 return error;
826 bank_conversion[CPCAP_ADC_CHG_ISENSE].cal_offset =
827 ((short)calibration_data * -1) + 512;
828
829 error = regmap_read(ddata->reg, CPCAP_REG_ADCAL2,
830 &calibration_data);
831 if (error)
832 return error;
833 bank_conversion[CPCAP_ADC_BATTI].cal_offset =
834 ((short)calibration_data * -1) + 512;
835 }
836
837 addr = CPCAP_REG_ADCD0 + req->bank_index * 4;
838
839 error = regmap_read(ddata->reg, addr, &req->result);
840 if (error)
841 return error;
842
843 req->result &= 0x3ff;
844 cpcap_adc_phase(req);
845 cpcap_adc_convert(req);
846
847 return 0;
848}
849
850static int cpcap_adc_init_request(struct cpcap_adc_request *req,
851 int channel)
852{
853 req->channel = channel;
854 req->phase_tbl = bank_phasing;
855 req->conv_tbl = bank_conversion;
856
857 switch (channel) {
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700858 case CPCAP_ADC_AD0 ... CPCAP_ADC_USB_ID:
Tony Lindgren25ec2492017-03-23 20:38:42 -0700859 req->bank_index = channel;
860 break;
861 case CPCAP_ADC_AD8 ... CPCAP_ADC_TSY2_AD15:
862 req->bank_index = channel - 8;
863 break;
864 case CPCAP_ADC_BATTP_PI16:
865 req->bank_index = CPCAP_ADC_BATTP;
866 break;
867 case CPCAP_ADC_BATTI_PI17:
868 req->bank_index = CPCAP_ADC_BATTI;
869 break;
870 default:
871 return -EINVAL;
872 }
873
874 return 0;
875}
876
877static int cpcap_adc_read(struct iio_dev *indio_dev,
878 struct iio_chan_spec const *chan,
879 int *val, int *val2, long mask)
880{
881 struct cpcap_adc *ddata = iio_priv(indio_dev);
882 struct cpcap_adc_request req;
883 int error;
884
885 error = cpcap_adc_init_request(&req, chan->channel);
886 if (error)
887 return error;
888
889 switch (mask) {
890 case IIO_CHAN_INFO_RAW:
891 mutex_lock(&ddata->lock);
892 error = cpcap_adc_start_bank(ddata, &req);
893 if (error)
894 goto err_unlock;
895 error = regmap_read(ddata->reg, chan->address, val);
896 if (error)
897 goto err_unlock;
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700898 error = cpcap_adc_stop_bank(ddata);
899 if (error)
900 goto err_unlock;
Tony Lindgren25ec2492017-03-23 20:38:42 -0700901 mutex_unlock(&ddata->lock);
902 break;
903 case IIO_CHAN_INFO_PROCESSED:
904 mutex_lock(&ddata->lock);
905 error = cpcap_adc_start_bank(ddata, &req);
906 if (error)
907 goto err_unlock;
908 error = cpcap_adc_read_bank_scaled(ddata, &req);
909 if (error)
910 goto err_unlock;
Tony Lindgren3f9f3a12017-05-22 17:51:44 -0700911 error = cpcap_adc_stop_bank(ddata);
912 if (error)
913 goto err_unlock;
Tony Lindgren25ec2492017-03-23 20:38:42 -0700914 mutex_unlock(&ddata->lock);
915 *val = req.result;
916 break;
917 default:
918 return -EINVAL;
919 }
920
921 return IIO_VAL_INT;
922
923err_unlock:
924 mutex_unlock(&ddata->lock);
925 dev_err(ddata->dev, "error reading ADC: %i\n", error);
926
927 return error;
928}
929
930static const struct iio_info cpcap_adc_info = {
931 .read_raw = &cpcap_adc_read,
932 .driver_module = THIS_MODULE,
933};
934
935/*
936 * Configuration for Motorola mapphone series such as droid 4.
937 * Copied from the Motorola mapphone kernel tree.
938 */
939static const struct cpcap_adc_ato mapphone_adc = {
940 .ato_in = 0x0480,
941 .atox_in = 0,
942 .adc_ps_factor_in = 0x0200,
943 .atox_ps_factor_in = 0,
944 .ato_out = 0,
945 .atox_out = 0,
946 .adc_ps_factor_out = 0,
947 .atox_ps_factor_out = 0,
948};
949
950static const struct of_device_id cpcap_adc_id_table[] = {
951 {
952 .compatible = "motorola,cpcap-adc",
953 },
954 {
955 .compatible = "motorola,mapphone-cpcap-adc",
956 .data = &mapphone_adc,
957 },
958 { /* sentinel */ },
959};
960MODULE_DEVICE_TABLE(of, cpcap_adc_id_table);
961
962static int cpcap_adc_probe(struct platform_device *pdev)
963{
964 const struct of_device_id *match;
965 struct cpcap_adc *ddata;
966 struct iio_dev *indio_dev;
967 int error;
968
969 match = of_match_device(of_match_ptr(cpcap_adc_id_table),
970 &pdev->dev);
971 if (!match)
972 return -EINVAL;
973
974 if (!match->data) {
975 dev_err(&pdev->dev, "no configuration data found\n");
976
977 return -ENODEV;
978 }
979
980 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*ddata));
981 if (!indio_dev) {
982 dev_err(&pdev->dev, "failed to allocate iio device\n");
983
984 return -ENOMEM;
985 }
986 ddata = iio_priv(indio_dev);
987 ddata->ato = match->data;
988 ddata->dev = &pdev->dev;
989
990 mutex_init(&ddata->lock);
991 init_waitqueue_head(&ddata->wq_data_avail);
992
993 indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
994 indio_dev->dev.parent = &pdev->dev;
995 indio_dev->dev.of_node = pdev->dev.of_node;
996 indio_dev->channels = cpcap_adc_channels;
997 indio_dev->num_channels = ARRAY_SIZE(cpcap_adc_channels);
998 indio_dev->name = dev_name(&pdev->dev);
999 indio_dev->info = &cpcap_adc_info;
1000
1001 ddata->reg = dev_get_regmap(pdev->dev.parent, NULL);
1002 if (!ddata->reg)
1003 return -ENODEV;
1004
1005 error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor);
1006 if (error)
1007 return error;
1008
1009 platform_set_drvdata(pdev, indio_dev);
1010
1011 ddata->irq = platform_get_irq_byname(pdev, "adcdone");
1012 if (!ddata->irq)
1013 return -ENODEV;
1014
1015 error = devm_request_threaded_irq(&pdev->dev, ddata->irq, NULL,
1016 cpcap_adc_irq_thread,
1017 IRQF_TRIGGER_NONE,
1018 "cpcap-adc", indio_dev);
1019 if (error) {
1020 dev_err(&pdev->dev, "could not get irq: %i\n",
1021 error);
1022
1023 return error;
1024 }
1025
1026 error = cpcap_adc_calibrate(ddata);
1027 if (error)
1028 return error;
1029
1030 dev_info(&pdev->dev, "CPCAP ADC device probed\n");
1031
1032 return devm_iio_device_register(&pdev->dev, indio_dev);
1033}
1034
1035static struct platform_driver cpcap_adc_driver = {
1036 .driver = {
1037 .name = "cpcap_adc",
1038 .of_match_table = of_match_ptr(cpcap_adc_id_table),
1039 },
1040 .probe = cpcap_adc_probe,
1041};
1042
1043module_platform_driver(cpcap_adc_driver);
1044
1045MODULE_ALIAS("platform:cpcap_adc");
1046MODULE_DESCRIPTION("CPCAP ADC driver");
1047MODULE_AUTHOR("Tony Lindgren <tony@atomide.com");
1048MODULE_LICENSE("GPL v2");