GNU Linux-libre 4.14.324-gnu1
[releases.git] / drivers / iio / adc / twl6030-gpadc.c
1 /*
2  * TWL6030 GPADC module driver
3  *
4  * Copyright (C) 2009-2013 Texas Instruments Inc.
5  * Nishant Kamat <nskamat@ti.com>
6  * Balaji T K <balajitk@ti.com>
7  * Graeme Gregory <gg@slimlogic.co.uk>
8  * Girish S Ghongdemath <girishsg@ti.com>
9  * Ambresh K <ambresh@ti.com>
10  * Oleksandr Kozaruk <oleksandr.kozaruk@ti.com
11  *
12  * Based on twl4030-madc.c
13  * Copyright (C) 2008 Nokia Corporation
14  * Mikko Ylinen <mikko.k.ylinen@nokia.com>
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * version 2 as published by the Free Software Foundation.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28  * 02110-1301 USA
29  *
30  */
31 #include <linux/interrupt.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/of_platform.h>
36 #include <linux/mfd/twl.h>
37 #include <linux/iio/iio.h>
38 #include <linux/iio/sysfs.h>
39
40 #define DRIVER_NAME             "twl6030_gpadc"
41
42 /*
43  * twl6030 per TRM has 17 channels, and twl6032 has 19 channels
44  * 2 test network channels are not used,
45  * 2 die temperature channels are not used either, as it is not
46  * defined how to convert ADC value to temperature
47  */
48 #define TWL6030_GPADC_USED_CHANNELS             13
49 #define TWL6030_GPADC_MAX_CHANNELS              15
50 #define TWL6032_GPADC_USED_CHANNELS             15
51 #define TWL6032_GPADC_MAX_CHANNELS              19
52 #define TWL6030_GPADC_NUM_TRIM_REGS             16
53
54 #define TWL6030_GPADC_CTRL_P1                   0x05
55
56 #define TWL6032_GPADC_GPSELECT_ISB              0x07
57 #define TWL6032_GPADC_CTRL_P1                   0x08
58
59 #define TWL6032_GPADC_GPCH0_LSB                 0x0d
60 #define TWL6032_GPADC_GPCH0_MSB                 0x0e
61
62 #define TWL6030_GPADC_CTRL_P1_SP1               BIT(3)
63
64 #define TWL6030_GPADC_GPCH0_LSB                 (0x29)
65
66 #define TWL6030_GPADC_RT_SW1_EOC_MASK           BIT(5)
67
68 #define TWL6030_GPADC_TRIM1                     0xCD
69
70 #define TWL6030_REG_TOGGLE1                     0x90
71 #define TWL6030_GPADCS                          BIT(1)
72 #define TWL6030_GPADCR                          BIT(0)
73
74 #define USB_VBUS_CTRL_SET                       0x04
75 #define USB_ID_CTRL_SET                         0x06
76
77 #define TWL6030_MISC1                           0xE4
78 #define VBUS_MEAS                               0x01
79 #define ID_MEAS                                 0x01
80
81 #define VAC_MEAS                0x04
82 #define VBAT_MEAS               0x02
83 #define BB_MEAS                 0x01
84
85
86 /**
87  * struct twl6030_chnl_calib - channel calibration
88  * @gain:               slope coefficient for ideal curve
89  * @gain_error:         gain error
90  * @offset_error:       offset of the real curve
91  */
92 struct twl6030_chnl_calib {
93         s32 gain;
94         s32 gain_error;
95         s32 offset_error;
96 };
97
98 /**
99  * struct twl6030_ideal_code - GPADC calibration parameters
100  * GPADC is calibrated in two points: close to the beginning and
101  * to the and of the measurable input range
102  *
103  * @channel:    channel number
104  * @code1:      ideal code for the input at the beginning
105  * @code2:      ideal code for at the end of the range
106  * @volt1:      voltage input at the beginning(low voltage)
107  * @volt2:      voltage input at the end(high voltage)
108  */
109 struct twl6030_ideal_code {
110         int channel;
111         u16 code1;
112         u16 code2;
113         u16 volt1;
114         u16 volt2;
115 };
116
117 struct twl6030_gpadc_data;
118
119 /**
120  * struct twl6030_gpadc_platform_data - platform specific data
121  * @nchannels:          number of GPADC channels
122  * @iio_channels:       iio channels
123  * @twl6030_ideal:      pointer to calibration parameters
124  * @start_conversion:   pointer to ADC start conversion function
125  * @channel_to_reg      pointer to ADC function to convert channel to
126  *                      register address for reading conversion result
127  * @calibrate:          pointer to calibration function
128  */
129 struct twl6030_gpadc_platform_data {
130         const int nchannels;
131         const struct iio_chan_spec *iio_channels;
132         const struct twl6030_ideal_code *ideal;
133         int (*start_conversion)(int channel);
134         u8 (*channel_to_reg)(int channel);
135         int (*calibrate)(struct twl6030_gpadc_data *gpadc);
136 };
137
138 /**
139  * struct twl6030_gpadc_data - GPADC data
140  * @dev:                device pointer
141  * @lock:               mutual exclusion lock for the structure
142  * @irq_complete:       completion to signal end of conversion
143  * @twl6030_cal_tbl:    pointer to calibration data for each
144  *                      channel with gain error and offset
145  * @pdata:              pointer to device specific data
146  */
147 struct twl6030_gpadc_data {
148         struct device   *dev;
149         struct mutex    lock;
150         struct completion       irq_complete;
151         struct twl6030_chnl_calib       *twl6030_cal_tbl;
152         const struct twl6030_gpadc_platform_data *pdata;
153 };
154
155 /*
156  * channels 11, 12, 13, 15 and 16 have no calibration data
157  * calibration offset is same for channels 1, 3, 4, 5
158  *
159  * The data is taken from GPADC_TRIM registers description.
160  * GPADC_TRIM registers keep difference between the code measured
161  * at volt1 and volt2 input voltages and corresponding code1 and code2
162  */
163 static const struct twl6030_ideal_code
164         twl6030_ideal[TWL6030_GPADC_USED_CHANNELS] = {
165         [0] = { /* ch 0, external, battery type, resistor value */
166                 .channel = 0,
167                 .code1 = 116,
168                 .code2 = 745,
169                 .volt1 = 141,
170                 .volt2 = 910,
171         },
172         [1] = { /* ch 1, external, battery temperature, NTC resistor value */
173                 .channel = 1,
174                 .code1 = 82,
175                 .code2 = 900,
176                 .volt1 = 100,
177                 .volt2 = 1100,
178         },
179         [2] = { /* ch 2, external, audio accessory/general purpose */
180                 .channel = 2,
181                 .code1 = 55,
182                 .code2 = 818,
183                 .volt1 = 101,
184                 .volt2 = 1499,
185         },
186         [3] = { /* ch 3, external, general purpose */
187                 .channel = 3,
188                 .code1 = 82,
189                 .code2 = 900,
190                 .volt1 = 100,
191                 .volt2 = 1100,
192         },
193         [4] = { /* ch 4, external, temperature measurement/general purpose */
194                 .channel = 4,
195                 .code1 = 82,
196                 .code2 = 900,
197                 .volt1 = 100,
198                 .volt2 = 1100,
199         },
200         [5] = { /* ch 5, external, general purpose */
201                 .channel = 5,
202                 .code1 = 82,
203                 .code2 = 900,
204                 .volt1 = 100,
205                 .volt2 = 1100,
206         },
207         [6] = { /* ch 6, external, general purpose */
208                 .channel = 6,
209                 .code1 = 82,
210                 .code2 = 900,
211                 .volt1 = 100,
212                 .volt2 = 1100,
213         },
214         [7] = { /* ch 7, internal, main battery */
215                 .channel = 7,
216                 .code1 = 614,
217                 .code2 = 941,
218                 .volt1 = 3001,
219                 .volt2 = 4599,
220         },
221         [8] = { /* ch 8, internal, backup battery */
222                 .channel = 8,
223                 .code1 = 82,
224                 .code2 = 688,
225                 .volt1 = 501,
226                 .volt2 = 4203,
227         },
228         [9] = { /* ch 9, internal, external charger input */
229                 .channel = 9,
230                 .code1 = 182,
231                 .code2 = 818,
232                 .volt1 = 2001,
233                 .volt2 = 8996,
234         },
235         [10] = { /* ch 10, internal, VBUS */
236                 .channel = 10,
237                 .code1 = 149,
238                 .code2 = 818,
239                 .volt1 = 1001,
240                 .volt2 = 5497,
241         },
242         [11] = { /* ch 11, internal, VBUS charging current */
243                 .channel = 11,
244         },
245                 /* ch 12, internal, Die temperature */
246                 /* ch 13, internal, Die temperature */
247         [12] = { /* ch 14, internal, USB ID line */
248                 .channel = 14,
249                 .code1 = 48,
250                 .code2 = 714,
251                 .volt1 = 323,
252                 .volt2 = 4800,
253         },
254 };
255
256 static const struct twl6030_ideal_code
257                         twl6032_ideal[TWL6032_GPADC_USED_CHANNELS] = {
258         [0] = { /* ch 0, external, battery type, resistor value */
259                 .channel = 0,
260                 .code1 = 1441,
261                 .code2 = 3276,
262                 .volt1 = 440,
263                 .volt2 = 1000,
264         },
265         [1] = { /* ch 1, external, battery temperature, NTC resistor value */
266                 .channel = 1,
267                 .code1 = 1441,
268                 .code2 = 3276,
269                 .volt1 = 440,
270                 .volt2 = 1000,
271         },
272         [2] = { /* ch 2, external, audio accessory/general purpose */
273                 .channel = 2,
274                 .code1 = 1441,
275                 .code2 = 3276,
276                 .volt1 = 660,
277                 .volt2 = 1500,
278         },
279         [3] = { /* ch 3, external, temperature with external diode/general
280                                                                 purpose */
281                 .channel = 3,
282                 .code1 = 1441,
283                 .code2 = 3276,
284                 .volt1 = 440,
285                 .volt2 = 1000,
286         },
287         [4] = { /* ch 4, external, temperature measurement/general purpose */
288                 .channel = 4,
289                 .code1 = 1441,
290                 .code2 = 3276,
291                 .volt1 = 440,
292                 .volt2 = 1000,
293         },
294         [5] = { /* ch 5, external, general purpose */
295                 .channel = 5,
296                 .code1 = 1441,
297                 .code2 = 3276,
298                 .volt1 = 440,
299                 .volt2 = 1000,
300         },
301         [6] = { /* ch 6, external, general purpose */
302                 .channel = 6,
303                 .code1 = 1441,
304                 .code2 = 3276,
305                 .volt1 = 440,
306                 .volt2 = 1000,
307         },
308         [7] = { /* ch7, internal, system supply */
309                 .channel = 7,
310                 .code1 = 1441,
311                 .code2 = 3276,
312                 .volt1 = 2200,
313                 .volt2 = 5000,
314         },
315         [8] = { /* ch8, internal, backup battery */
316                 .channel = 8,
317                 .code1 = 1441,
318                 .code2 = 3276,
319                 .volt1 = 2200,
320                 .volt2 = 5000,
321         },
322         [9] = { /* ch 9, internal, external charger input */
323                 .channel = 9,
324                 .code1 = 1441,
325                 .code2 = 3276,
326                 .volt1 = 3960,
327                 .volt2 = 9000,
328         },
329         [10] = { /* ch10, internal, VBUS */
330                 .channel = 10,
331                 .code1 = 150,
332                 .code2 = 751,
333                 .volt1 = 1000,
334                 .volt2 = 5000,
335         },
336         [11] = { /* ch 11, internal, VBUS DC-DC output current */
337                 .channel = 11,
338                 .code1 = 1441,
339                 .code2 = 3276,
340                 .volt1 = 660,
341                 .volt2 = 1500,
342         },
343                 /* ch 12, internal, Die temperature */
344                 /* ch 13, internal, Die temperature */
345         [12] = { /* ch 14, internal, USB ID line */
346                 .channel = 14,
347                 .code1 = 1441,
348                 .code2 = 3276,
349                 .volt1 = 2420,
350                 .volt2 = 5500,
351         },
352                 /* ch 15, internal, test network */
353                 /* ch 16, internal, test network */
354         [13] = { /* ch 17, internal, battery charging current */
355                 .channel = 17,
356         },
357         [14] = { /* ch 18, internal, battery voltage */
358                 .channel = 18,
359                 .code1 = 1441,
360                 .code2 = 3276,
361                 .volt1 = 2200,
362                 .volt2 = 5000,
363         },
364 };
365
366 static inline int twl6030_gpadc_write(u8 reg, u8 val)
367 {
368         return twl_i2c_write_u8(TWL6030_MODULE_GPADC, val, reg);
369 }
370
371 static inline int twl6030_gpadc_read(u8 reg, u8 *val)
372 {
373
374         return twl_i2c_read(TWL6030_MODULE_GPADC, val, reg, 2);
375 }
376
377 static int twl6030_gpadc_enable_irq(u8 mask)
378 {
379         int ret;
380
381         ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_LINE_B);
382         if (ret < 0)
383                 return ret;
384
385         ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_STS_B);
386
387         return ret;
388 }
389
390 static void twl6030_gpadc_disable_irq(u8 mask)
391 {
392         twl6030_interrupt_mask(mask, REG_INT_MSK_LINE_B);
393         twl6030_interrupt_mask(mask, REG_INT_MSK_STS_B);
394 }
395
396 static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *indio_dev)
397 {
398         struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
399
400         complete(&gpadc->irq_complete);
401
402         return IRQ_HANDLED;
403 }
404
405 static int twl6030_start_conversion(int channel)
406 {
407         return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1,
408                                         TWL6030_GPADC_CTRL_P1_SP1);
409 }
410
411 static int twl6032_start_conversion(int channel)
412 {
413         int ret;
414
415         ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel);
416         if (ret)
417                 return ret;
418
419         return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1,
420                                                 TWL6030_GPADC_CTRL_P1_SP1);
421 }
422
423 static u8 twl6030_channel_to_reg(int channel)
424 {
425         return TWL6030_GPADC_GPCH0_LSB + 2 * channel;
426 }
427
428 static u8 twl6032_channel_to_reg(int channel)
429 {
430         /*
431          * for any prior chosen channel, when the conversion is ready
432          * the result is avalable in GPCH0_LSB, GPCH0_MSB.
433          */
434
435         return TWL6032_GPADC_GPCH0_LSB;
436 }
437
438 static int twl6030_gpadc_lookup(const struct twl6030_ideal_code *ideal,
439                 int channel, int size)
440 {
441         int i;
442
443         for (i = 0; i < size; i++)
444                 if (ideal[i].channel == channel)
445                         break;
446
447         return i;
448 }
449
450 static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
451                 *pdata, int channel)
452 {
453         const struct twl6030_ideal_code *ideal = pdata->ideal;
454         int i;
455
456         i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels);
457         /* not calibrated channels have 0 in all structure members */
458         return pdata->ideal[i].code2;
459 }
460
461 static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data *gpadc,
462                 int channel, int raw_code)
463 {
464         const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
465         int corrected_code;
466         int i;
467
468         i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
469         corrected_code = ((raw_code * 1000) -
470                 gpadc->twl6030_cal_tbl[i].offset_error) /
471                 gpadc->twl6030_cal_tbl[i].gain_error;
472
473         return corrected_code;
474 }
475
476 static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data *gpadc,
477                 int channel, int *res)
478 {
479         u8 reg = gpadc->pdata->channel_to_reg(channel);
480         __le16 val;
481         int raw_code;
482         int ret;
483
484         ret = twl6030_gpadc_read(reg, (u8 *)&val);
485         if (ret) {
486                 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
487                 return ret;
488         }
489
490         raw_code = le16_to_cpu(val);
491         dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code);
492
493         if (twl6030_channel_calibrated(gpadc->pdata, channel))
494                 *res = twl6030_gpadc_make_correction(gpadc, channel, raw_code);
495         else
496                 *res = raw_code;
497
498         return ret;
499 }
500
501 static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data *gpadc,
502                 int channel, int *val)
503 {
504         const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
505         int corrected_code;
506         int channel_value;
507         int i;
508         int ret;
509
510         ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code);
511         if (ret)
512                 return ret;
513
514         i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
515         channel_value = corrected_code *
516                         gpadc->twl6030_cal_tbl[i].gain;
517
518         /* Shift back into mV range */
519         channel_value /= 1000;
520
521         dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code);
522         dev_dbg(gpadc->dev, "GPADC value: %d", channel_value);
523
524         *val = channel_value;
525
526         return ret;
527 }
528
529 static int twl6030_gpadc_read_raw(struct iio_dev *indio_dev,
530                              const struct iio_chan_spec *chan,
531                              int *val, int *val2, long mask)
532 {
533         struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
534         int ret;
535         long timeout;
536
537         mutex_lock(&gpadc->lock);
538
539         ret = gpadc->pdata->start_conversion(chan->channel);
540         if (ret) {
541                 dev_err(gpadc->dev, "failed to start conversion\n");
542                 goto err;
543         }
544         /* wait for conversion to complete */
545         timeout = wait_for_completion_interruptible_timeout(
546                                 &gpadc->irq_complete, msecs_to_jiffies(5000));
547         if (timeout == 0) {
548                 ret = -ETIMEDOUT;
549                 goto err;
550         } else if (timeout < 0) {
551                 ret = -EINTR;
552                 goto err;
553         }
554
555         switch (mask) {
556         case IIO_CHAN_INFO_RAW:
557                 ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val);
558                 ret = ret ? -EIO : IIO_VAL_INT;
559                 break;
560
561         case IIO_CHAN_INFO_PROCESSED:
562                 ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val);
563                 ret = ret ? -EIO : IIO_VAL_INT;
564                 break;
565
566         default:
567                 break;
568         }
569 err:
570         mutex_unlock(&gpadc->lock);
571
572         return ret;
573 }
574
575 /*
576  * The GPADC channels are calibrated using a two point calibration method.
577  * The channels measured with two known values: volt1 and volt2, and
578  * ideal corresponding output codes are known: code1, code2.
579  * The difference(d1, d2) between ideal and measured codes stored in trim
580  * registers.
581  * The goal is to find offset and gain of the real curve for each calibrated
582  * channel.
583  * gain: k = 1 + ((d2 - d1) / (x2 - x1))
584  * offset: b = d1 + (k - 1) * x1
585  */
586 static void twl6030_calibrate_channel(struct twl6030_gpadc_data *gpadc,
587                 int channel, int d1, int d2)
588 {
589         int b, k, gain, x1, x2, i;
590         const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
591
592         i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
593
594         /* Gain */
595         gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) /
596                 (ideal[i].code2 - ideal[i].code1);
597
598         x1 = ideal[i].code1;
599         x2 = ideal[i].code2;
600
601         /* k - real curve gain */
602         k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
603
604         /* b - offset of the real curve gain */
605         b = (d1 * 1000) - (k - 1000) * x1;
606
607         gpadc->twl6030_cal_tbl[i].gain = gain;
608         gpadc->twl6030_cal_tbl[i].gain_error = k;
609         gpadc->twl6030_cal_tbl[i].offset_error = b;
610
611         dev_dbg(gpadc->dev, "GPADC d1   for Chn: %d = %d\n", channel, d1);
612         dev_dbg(gpadc->dev, "GPADC d2   for Chn: %d = %d\n", channel, d2);
613         dev_dbg(gpadc->dev, "GPADC x1   for Chn: %d = %d\n", channel, x1);
614         dev_dbg(gpadc->dev, "GPADC x2   for Chn: %d = %d\n", channel, x2);
615         dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain);
616         dev_dbg(gpadc->dev, "GPADC k    for Chn: %d = %d\n", channel, k);
617         dev_dbg(gpadc->dev, "GPADC b    for Chn: %d = %d\n", channel, b);
618 }
619
620 static inline int twl6030_gpadc_get_trim_offset(s8 d)
621 {
622         /*
623          * XXX NOTE!
624          * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
625          * though, the documentation states that trim value
626          * is absolute value, the correct conversion results are
627          * obtained if the value is interpreted as 2's complement.
628          */
629         __u32 temp = ((d & 0x7f) >> 1) | ((d & 1) << 6);
630
631         return sign_extend32(temp, 6);
632 }
633
634 static int twl6030_calibration(struct twl6030_gpadc_data *gpadc)
635 {
636         int ret;
637         int chn;
638         u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
639         s8 d1, d2;
640
641         /*
642          * for calibration two measurements have been performed at
643          * factory, for some channels, during the production test and
644          * have been stored in registers. This two stored values are
645          * used to correct the measurements. The values represent
646          * offsets for the given input from the output on ideal curve.
647          */
648         ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
649                         TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
650         if (ret < 0) {
651                 dev_err(gpadc->dev, "calibration failed\n");
652                 return ret;
653         }
654
655         for (chn = 0; chn < TWL6030_GPADC_MAX_CHANNELS; chn++) {
656
657                 switch (chn) {
658                 case 0:
659                         d1 = trim_regs[0];
660                         d2 = trim_regs[1];
661                         break;
662                 case 1:
663                 case 3:
664                 case 4:
665                 case 5:
666                 case 6:
667                         d1 = trim_regs[4];
668                         d2 = trim_regs[5];
669                         break;
670                 case 2:
671                         d1 = trim_regs[12];
672                         d2 = trim_regs[13];
673                         break;
674                 case 7:
675                         d1 = trim_regs[6];
676                         d2 = trim_regs[7];
677                         break;
678                 case 8:
679                         d1 = trim_regs[2];
680                         d2 = trim_regs[3];
681                         break;
682                 case 9:
683                         d1 = trim_regs[8];
684                         d2 = trim_regs[9];
685                         break;
686                 case 10:
687                         d1 = trim_regs[10];
688                         d2 = trim_regs[11];
689                         break;
690                 case 14:
691                         d1 = trim_regs[14];
692                         d2 = trim_regs[15];
693                         break;
694                 default:
695                         continue;
696                 }
697
698                 d1 = twl6030_gpadc_get_trim_offset(d1);
699                 d2 = twl6030_gpadc_get_trim_offset(d2);
700
701                 twl6030_calibrate_channel(gpadc, chn, d1, d2);
702         }
703
704         return 0;
705 }
706
707 static int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
708                 unsigned int reg1, unsigned int mask0, unsigned int mask1,
709                 unsigned int shift0)
710 {
711         int val;
712
713         val = (trim_regs[reg0] & mask0) << shift0;
714         val |= (trim_regs[reg1] & mask1) >> 1;
715         if (trim_regs[reg1] & 0x01)
716                 val = -val;
717
718         return val;
719 }
720
721 static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
722 {
723         int chn, d1 = 0, d2 = 0, temp;
724         u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
725         int ret;
726
727         ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
728                         TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
729         if (ret < 0) {
730                 dev_err(gpadc->dev, "calibration failed\n");
731                 return ret;
732         }
733
734         /*
735          * Loop to calculate the value needed for returning voltages from
736          * GPADC not values.
737          *
738          * gain is calculated to 3 decimal places fixed point.
739          */
740         for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
741
742                 switch (chn) {
743                 case 0:
744                 case 1:
745                 case 2:
746                 case 3:
747                 case 4:
748                 case 5:
749                 case 6:
750                 case 11:
751                 case 14:
752                         d1 = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
753                                                                 0x06, 2);
754                         d2 = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
755                                                                 0x06, 2);
756                         break;
757                 case 8:
758                         temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
759                                                                 0x06, 2);
760                         d1 = temp + twl6032_get_trim_value(trim_regs, 7, 6,
761                                                                 0x18, 0x1E, 1);
762
763                         temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3F,
764                                                                 0x06, 2);
765                         d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7,
766                                                                 0x1F, 0x06, 2);
767                         break;
768                 case 9:
769                         temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
770                                                                 0x06, 2);
771                         d1 = temp + twl6032_get_trim_value(trim_regs, 13, 11,
772                                                                 0x18, 0x1E, 1);
773
774                         temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
775                                                                 0x06, 2);
776                         d2 = temp + twl6032_get_trim_value(trim_regs, 15, 13,
777                                                                 0x1F, 0x06, 1);
778                         break;
779                 case 10:
780                         d1 = twl6032_get_trim_value(trim_regs, 10, 8, 0x0f,
781                                                                 0x0E, 3);
782                         d2 = twl6032_get_trim_value(trim_regs, 14, 12, 0x0f,
783                                                                 0x0E, 3);
784                         break;
785                 case 7:
786                 case 18:
787                         temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
788                                                                 0x06, 2);
789
790                         d1 = (trim_regs[4] & 0x7E) >> 1;
791                         if (trim_regs[4] & 0x01)
792                                 d1 = -d1;
793                         d1 += temp;
794
795                         temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
796                                                                 0x06, 2);
797
798                         d2 = (trim_regs[5] & 0xFE) >> 1;
799                         if (trim_regs[5] & 0x01)
800                                 d2 = -d2;
801
802                         d2 += temp;
803                         break;
804                 default:
805                         /* No data for other channels */
806                         continue;
807                 }
808
809                 twl6030_calibrate_channel(gpadc, chn, d1, d2);
810         }
811
812         return 0;
813 }
814
815 #define TWL6030_GPADC_CHAN(chn, _type, chan_info) {     \
816         .type = _type,                                  \
817         .channel = chn,                                 \
818         .info_mask_separate = BIT(chan_info),           \
819         .indexed = 1,                                   \
820 }
821
822 static const struct iio_chan_spec twl6030_gpadc_iio_channels[] = {
823         TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
824         TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
825         TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
826         TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
827         TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
828         TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
829         TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
830         TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
831         TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
832         TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
833         TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
834         TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
835         TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
836 };
837
838 static const struct iio_chan_spec twl6032_gpadc_iio_channels[] = {
839         TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
840         TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
841         TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
842         TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
843         TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
844         TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
845         TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
846         TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
847         TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
848         TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
849         TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
850         TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
851         TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
852         TWL6030_GPADC_CHAN(17, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
853         TWL6030_GPADC_CHAN(18, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
854 };
855
856 static const struct iio_info twl6030_gpadc_iio_info = {
857         .read_raw = &twl6030_gpadc_read_raw,
858         .driver_module = THIS_MODULE,
859 };
860
861 static const struct twl6030_gpadc_platform_data twl6030_pdata = {
862         .iio_channels = twl6030_gpadc_iio_channels,
863         .nchannels = TWL6030_GPADC_USED_CHANNELS,
864         .ideal = twl6030_ideal,
865         .start_conversion = twl6030_start_conversion,
866         .channel_to_reg = twl6030_channel_to_reg,
867         .calibrate = twl6030_calibration,
868 };
869
870 static const struct twl6030_gpadc_platform_data twl6032_pdata = {
871         .iio_channels = twl6032_gpadc_iio_channels,
872         .nchannels = TWL6032_GPADC_USED_CHANNELS,
873         .ideal = twl6032_ideal,
874         .start_conversion = twl6032_start_conversion,
875         .channel_to_reg = twl6032_channel_to_reg,
876         .calibrate = twl6032_calibration,
877 };
878
879 static const struct of_device_id of_twl6030_match_tbl[] = {
880         {
881                 .compatible = "ti,twl6030-gpadc",
882                 .data = &twl6030_pdata,
883         },
884         {
885                 .compatible = "ti,twl6032-gpadc",
886                 .data = &twl6032_pdata,
887         },
888         { /* end */ }
889 };
890 MODULE_DEVICE_TABLE(of, of_twl6030_match_tbl);
891
892 static int twl6030_gpadc_probe(struct platform_device *pdev)
893 {
894         struct device *dev = &pdev->dev;
895         struct twl6030_gpadc_data *gpadc;
896         const struct twl6030_gpadc_platform_data *pdata;
897         const struct of_device_id *match;
898         struct iio_dev *indio_dev;
899         int irq;
900         int ret;
901
902         match = of_match_device(of_twl6030_match_tbl, dev);
903         if (!match)
904                 return -EINVAL;
905
906         pdata = match->data;
907
908         indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
909         if (!indio_dev)
910                 return -ENOMEM;
911
912         gpadc = iio_priv(indio_dev);
913
914         gpadc->twl6030_cal_tbl = devm_kzalloc(dev,
915                                         sizeof(*gpadc->twl6030_cal_tbl) *
916                                         pdata->nchannels, GFP_KERNEL);
917         if (!gpadc->twl6030_cal_tbl)
918                 return -ENOMEM;
919
920         gpadc->dev = dev;
921         gpadc->pdata = pdata;
922
923         platform_set_drvdata(pdev, indio_dev);
924         mutex_init(&gpadc->lock);
925         init_completion(&gpadc->irq_complete);
926
927         ret = pdata->calibrate(gpadc);
928         if (ret < 0) {
929                 dev_err(&pdev->dev, "failed to read calibration registers\n");
930                 return ret;
931         }
932
933         irq = platform_get_irq(pdev, 0);
934         if (irq < 0) {
935                 dev_err(&pdev->dev, "failed to get irq\n");
936                 return irq;
937         }
938
939         ret = devm_request_threaded_irq(dev, irq, NULL,
940                                 twl6030_gpadc_irq_handler,
941                                 IRQF_ONESHOT, "twl6030_gpadc", indio_dev);
942         if (ret)
943                 return ret;
944
945         ret = twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
946         if (ret < 0) {
947                 dev_err(&pdev->dev, "failed to enable GPADC interrupt\n");
948                 return ret;
949         }
950
951         ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
952                                         TWL6030_REG_TOGGLE1);
953         if (ret < 0) {
954                 dev_err(&pdev->dev, "failed to enable GPADC module\n");
955                 return ret;
956         }
957
958         ret = twl_i2c_write_u8(TWL_MODULE_USB, VBUS_MEAS, USB_VBUS_CTRL_SET);
959         if (ret < 0) {
960                 dev_err(dev, "failed to wire up inputs\n");
961                 return ret;
962         }
963
964         ret = twl_i2c_write_u8(TWL_MODULE_USB, ID_MEAS, USB_ID_CTRL_SET);
965         if (ret < 0) {
966                 dev_err(dev, "failed to wire up inputs\n");
967                 return ret;
968         }
969
970         ret = twl_i2c_write_u8(TWL6030_MODULE_ID0,
971                                 VBAT_MEAS | BB_MEAS | BB_MEAS,
972                                 TWL6030_MISC1);
973         if (ret < 0) {
974                 dev_err(dev, "failed to wire up inputs\n");
975                 return ret;
976         }
977
978         indio_dev->name = DRIVER_NAME;
979         indio_dev->dev.parent = dev;
980         indio_dev->info = &twl6030_gpadc_iio_info;
981         indio_dev->modes = INDIO_DIRECT_MODE;
982         indio_dev->channels = pdata->iio_channels;
983         indio_dev->num_channels = pdata->nchannels;
984
985         return iio_device_register(indio_dev);
986 }
987
988 static int twl6030_gpadc_remove(struct platform_device *pdev)
989 {
990         struct iio_dev *indio_dev = platform_get_drvdata(pdev);
991
992         twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
993         iio_device_unregister(indio_dev);
994
995         return 0;
996 }
997
998 #ifdef CONFIG_PM_SLEEP
999 static int twl6030_gpadc_suspend(struct device *pdev)
1000 {
1001         int ret;
1002
1003         ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
1004                                 TWL6030_REG_TOGGLE1);
1005         if (ret)
1006                 dev_err(pdev, "error resetting GPADC (%d)!\n", ret);
1007
1008         return 0;
1009 };
1010
1011 static int twl6030_gpadc_resume(struct device *pdev)
1012 {
1013         int ret;
1014
1015         ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
1016                                 TWL6030_REG_TOGGLE1);
1017         if (ret)
1018                 dev_err(pdev, "error setting GPADC (%d)!\n", ret);
1019
1020         return 0;
1021 };
1022 #endif
1023
1024 static SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops, twl6030_gpadc_suspend,
1025                                         twl6030_gpadc_resume);
1026
1027 static struct platform_driver twl6030_gpadc_driver = {
1028         .probe          = twl6030_gpadc_probe,
1029         .remove         = twl6030_gpadc_remove,
1030         .driver         = {
1031                 .name   = DRIVER_NAME,
1032                 .pm     = &twl6030_gpadc_pm_ops,
1033                 .of_match_table = of_twl6030_match_tbl,
1034         },
1035 };
1036
1037 module_platform_driver(twl6030_gpadc_driver);
1038
1039 MODULE_ALIAS("platform:" DRIVER_NAME);
1040 MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
1041 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1042 MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
1043 MODULE_DESCRIPTION("twl6030 ADC driver");
1044 MODULE_LICENSE("GPL");