root/drivers/iio/adc/twl6030-gpadc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. twl6030_gpadc_write
  2. twl6030_gpadc_read
  3. twl6030_gpadc_enable_irq
  4. twl6030_gpadc_disable_irq
  5. twl6030_gpadc_irq_handler
  6. twl6030_start_conversion
  7. twl6032_start_conversion
  8. twl6030_channel_to_reg
  9. twl6032_channel_to_reg
  10. twl6030_gpadc_lookup
  11. twl6030_channel_calibrated
  12. twl6030_gpadc_make_correction
  13. twl6030_gpadc_get_raw
  14. twl6030_gpadc_get_processed
  15. twl6030_gpadc_read_raw
  16. twl6030_calibrate_channel
  17. twl6030_gpadc_get_trim_offset
  18. twl6030_calibration
  19. twl6032_get_trim_value
  20. twl6032_calibration
  21. twl6030_gpadc_probe
  22. twl6030_gpadc_remove
  23. twl6030_gpadc_suspend
  24. twl6030_gpadc_resume

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

/* [<][>][^][v][top][bottom][index][help] */