1 /* 2 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 and 6 * only version 2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 14 #include <linux/bitops.h> 15 #include <linux/completion.h> 16 #include <linux/delay.h> 17 #include <linux/err.h> 18 #include <linux/iio/iio.h> 19 #include <linux/interrupt.h> 20 #include <linux/kernel.h> 21 #include <linux/math64.h> 22 #include <linux/module.h> 23 #include <linux/of.h> 24 #include <linux/platform_device.h> 25 #include <linux/regmap.h> 26 #include <linux/slab.h> 27 #include <linux/log2.h> 28 29 #include <dt-bindings/iio/qcom,spmi-vadc.h> 30 31 #include "qcom-vadc-common.h" 32 33 /* VADC register and bit definitions */ 34 #define VADC_REVISION2 0x1 35 #define VADC_REVISION2_SUPPORTED_VADC 1 36 37 #define VADC_PERPH_TYPE 0x4 38 #define VADC_PERPH_TYPE_ADC 8 39 40 #define VADC_PERPH_SUBTYPE 0x5 41 #define VADC_PERPH_SUBTYPE_VADC 1 42 43 #define VADC_STATUS1 0x8 44 #define VADC_STATUS1_OP_MODE 4 45 #define VADC_STATUS1_REQ_STS BIT(1) 46 #define VADC_STATUS1_EOC BIT(0) 47 #define VADC_STATUS1_REQ_STS_EOC_MASK 0x3 48 49 #define VADC_MODE_CTL 0x40 50 #define VADC_OP_MODE_SHIFT 3 51 #define VADC_OP_MODE_NORMAL 0 52 #define VADC_AMUX_TRIM_EN BIT(1) 53 #define VADC_ADC_TRIM_EN BIT(0) 54 55 #define VADC_EN_CTL1 0x46 56 #define VADC_EN_CTL1_SET BIT(7) 57 58 #define VADC_ADC_CH_SEL_CTL 0x48 59 60 #define VADC_ADC_DIG_PARAM 0x50 61 #define VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT 2 62 63 #define VADC_HW_SETTLE_DELAY 0x51 64 65 #define VADC_CONV_REQ 0x52 66 #define VADC_CONV_REQ_SET BIT(7) 67 68 #define VADC_FAST_AVG_CTL 0x5a 69 #define VADC_FAST_AVG_EN 0x5b 70 #define VADC_FAST_AVG_EN_SET BIT(7) 71 72 #define VADC_ACCESS 0xd0 73 #define VADC_ACCESS_DATA 0xa5 74 75 #define VADC_PERH_RESET_CTL3 0xda 76 #define VADC_FOLLOW_WARM_RB BIT(2) 77 78 #define VADC_DATA 0x60 /* 16 bits */ 79 80 #define VADC_CHAN_MIN VADC_USBIN 81 #define VADC_CHAN_MAX VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM 82 83 /** 84 * struct vadc_channel_prop - VADC channel property. 85 * @channel: channel number, refer to the channel list. 86 * @calibration: calibration type. 87 * @decimation: sampling rate supported for the channel. 88 * @prescale: channel scaling performed on the input signal. 89 * @hw_settle_time: the time between AMUX being configured and the 90 * start of conversion. 91 * @avg_samples: ability to provide single result from the ADC 92 * that is an average of multiple measurements. 93 * @scale_fn_type: Represents the scaling function to convert voltage 94 * physical units desired by the client for the channel. 95 */ 96 struct vadc_channel_prop { 97 unsigned int channel; 98 enum vadc_calibration calibration; 99 unsigned int decimation; 100 unsigned int prescale; 101 unsigned int hw_settle_time; 102 unsigned int avg_samples; 103 enum vadc_scale_fn_type scale_fn_type; 104 }; 105 106 /** 107 * struct vadc_priv - VADC private structure. 108 * @regmap: pointer to struct regmap. 109 * @dev: pointer to struct device. 110 * @base: base address for the ADC peripheral. 111 * @nchannels: number of VADC channels. 112 * @chan_props: array of VADC channel properties. 113 * @iio_chans: array of IIO channels specification. 114 * @are_ref_measured: are reference points measured. 115 * @poll_eoc: use polling instead of interrupt. 116 * @complete: VADC result notification after interrupt is received. 117 * @graph: store parameters for calibration. 118 * @lock: ADC lock for access to the peripheral. 119 */ 120 struct vadc_priv { 121 struct regmap *regmap; 122 struct device *dev; 123 u16 base; 124 unsigned int nchannels; 125 struct vadc_channel_prop *chan_props; 126 struct iio_chan_spec *iio_chans; 127 bool are_ref_measured; 128 bool poll_eoc; 129 struct completion complete; 130 struct vadc_linear_graph graph[2]; 131 struct mutex lock; 132 }; 133 134 static const struct vadc_prescale_ratio vadc_prescale_ratios[] = { 135 {.num = 1, .den = 1}, 136 {.num = 1, .den = 3}, 137 {.num = 1, .den = 4}, 138 {.num = 1, .den = 6}, 139 {.num = 1, .den = 20}, 140 {.num = 1, .den = 8}, 141 {.num = 10, .den = 81}, 142 {.num = 1, .den = 10} 143 }; 144 145 static int vadc_read(struct vadc_priv *vadc, u16 offset, u8 *data) 146 { 147 return regmap_bulk_read(vadc->regmap, vadc->base + offset, data, 1); 148 } 149 150 static int vadc_write(struct vadc_priv *vadc, u16 offset, u8 data) 151 { 152 return regmap_write(vadc->regmap, vadc->base + offset, data); 153 } 154 155 static int vadc_reset(struct vadc_priv *vadc) 156 { 157 u8 data; 158 int ret; 159 160 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA); 161 if (ret) 162 return ret; 163 164 ret = vadc_read(vadc, VADC_PERH_RESET_CTL3, &data); 165 if (ret) 166 return ret; 167 168 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA); 169 if (ret) 170 return ret; 171 172 data |= VADC_FOLLOW_WARM_RB; 173 174 return vadc_write(vadc, VADC_PERH_RESET_CTL3, data); 175 } 176 177 static int vadc_set_state(struct vadc_priv *vadc, bool state) 178 { 179 return vadc_write(vadc, VADC_EN_CTL1, state ? VADC_EN_CTL1_SET : 0); 180 } 181 182 static void vadc_show_status(struct vadc_priv *vadc) 183 { 184 u8 mode, sta1, chan, dig, en, req; 185 int ret; 186 187 ret = vadc_read(vadc, VADC_MODE_CTL, &mode); 188 if (ret) 189 return; 190 191 ret = vadc_read(vadc, VADC_ADC_DIG_PARAM, &dig); 192 if (ret) 193 return; 194 195 ret = vadc_read(vadc, VADC_ADC_CH_SEL_CTL, &chan); 196 if (ret) 197 return; 198 199 ret = vadc_read(vadc, VADC_CONV_REQ, &req); 200 if (ret) 201 return; 202 203 ret = vadc_read(vadc, VADC_STATUS1, &sta1); 204 if (ret) 205 return; 206 207 ret = vadc_read(vadc, VADC_EN_CTL1, &en); 208 if (ret) 209 return; 210 211 dev_err(vadc->dev, 212 "mode:%02x en:%02x chan:%02x dig:%02x req:%02x sta1:%02x\n", 213 mode, en, chan, dig, req, sta1); 214 } 215 216 static int vadc_configure(struct vadc_priv *vadc, 217 struct vadc_channel_prop *prop) 218 { 219 u8 decimation, mode_ctrl; 220 int ret; 221 222 /* Mode selection */ 223 mode_ctrl = (VADC_OP_MODE_NORMAL << VADC_OP_MODE_SHIFT) | 224 VADC_ADC_TRIM_EN | VADC_AMUX_TRIM_EN; 225 ret = vadc_write(vadc, VADC_MODE_CTL, mode_ctrl); 226 if (ret) 227 return ret; 228 229 /* Channel selection */ 230 ret = vadc_write(vadc, VADC_ADC_CH_SEL_CTL, prop->channel); 231 if (ret) 232 return ret; 233 234 /* Digital parameter setup */ 235 decimation = prop->decimation << VADC_ADC_DIG_DEC_RATIO_SEL_SHIFT; 236 ret = vadc_write(vadc, VADC_ADC_DIG_PARAM, decimation); 237 if (ret) 238 return ret; 239 240 /* HW settle time delay */ 241 ret = vadc_write(vadc, VADC_HW_SETTLE_DELAY, prop->hw_settle_time); 242 if (ret) 243 return ret; 244 245 ret = vadc_write(vadc, VADC_FAST_AVG_CTL, prop->avg_samples); 246 if (ret) 247 return ret; 248 249 if (prop->avg_samples) 250 ret = vadc_write(vadc, VADC_FAST_AVG_EN, VADC_FAST_AVG_EN_SET); 251 else 252 ret = vadc_write(vadc, VADC_FAST_AVG_EN, 0); 253 254 return ret; 255 } 256 257 static int vadc_poll_wait_eoc(struct vadc_priv *vadc, unsigned int interval_us) 258 { 259 unsigned int count, retry; 260 u8 sta1; 261 int ret; 262 263 retry = interval_us / VADC_CONV_TIME_MIN_US; 264 265 for (count = 0; count < retry; count++) { 266 ret = vadc_read(vadc, VADC_STATUS1, &sta1); 267 if (ret) 268 return ret; 269 270 sta1 &= VADC_STATUS1_REQ_STS_EOC_MASK; 271 if (sta1 == VADC_STATUS1_EOC) 272 return 0; 273 274 usleep_range(VADC_CONV_TIME_MIN_US, VADC_CONV_TIME_MAX_US); 275 } 276 277 vadc_show_status(vadc); 278 279 return -ETIMEDOUT; 280 } 281 282 static int vadc_read_result(struct vadc_priv *vadc, u16 *data) 283 { 284 int ret; 285 286 ret = regmap_bulk_read(vadc->regmap, vadc->base + VADC_DATA, data, 2); 287 if (ret) 288 return ret; 289 290 *data = clamp_t(u16, *data, VADC_MIN_ADC_CODE, VADC_MAX_ADC_CODE); 291 292 return 0; 293 } 294 295 static struct vadc_channel_prop *vadc_get_channel(struct vadc_priv *vadc, 296 unsigned int num) 297 { 298 unsigned int i; 299 300 for (i = 0; i < vadc->nchannels; i++) 301 if (vadc->chan_props[i].channel == num) 302 return &vadc->chan_props[i]; 303 304 dev_dbg(vadc->dev, "no such channel %02x\n", num); 305 306 return NULL; 307 } 308 309 static int vadc_do_conversion(struct vadc_priv *vadc, 310 struct vadc_channel_prop *prop, u16 *data) 311 { 312 unsigned int timeout; 313 int ret; 314 315 mutex_lock(&vadc->lock); 316 317 ret = vadc_configure(vadc, prop); 318 if (ret) 319 goto unlock; 320 321 if (!vadc->poll_eoc) 322 reinit_completion(&vadc->complete); 323 324 ret = vadc_set_state(vadc, true); 325 if (ret) 326 goto unlock; 327 328 ret = vadc_write(vadc, VADC_CONV_REQ, VADC_CONV_REQ_SET); 329 if (ret) 330 goto err_disable; 331 332 timeout = BIT(prop->avg_samples) * VADC_CONV_TIME_MIN_US * 2; 333 334 if (vadc->poll_eoc) { 335 ret = vadc_poll_wait_eoc(vadc, timeout); 336 } else { 337 ret = wait_for_completion_timeout(&vadc->complete, timeout); 338 if (!ret) { 339 ret = -ETIMEDOUT; 340 goto err_disable; 341 } 342 343 /* Double check conversion status */ 344 ret = vadc_poll_wait_eoc(vadc, VADC_CONV_TIME_MIN_US); 345 if (ret) 346 goto err_disable; 347 } 348 349 ret = vadc_read_result(vadc, data); 350 351 err_disable: 352 vadc_set_state(vadc, false); 353 if (ret) 354 dev_err(vadc->dev, "conversion failed\n"); 355 unlock: 356 mutex_unlock(&vadc->lock); 357 return ret; 358 } 359 360 static int vadc_measure_ref_points(struct vadc_priv *vadc) 361 { 362 struct vadc_channel_prop *prop; 363 u16 read_1, read_2; 364 int ret; 365 366 vadc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE; 367 vadc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV; 368 369 prop = vadc_get_channel(vadc, VADC_REF_1250MV); 370 ret = vadc_do_conversion(vadc, prop, &read_1); 371 if (ret) 372 goto err; 373 374 /* Try with buffered 625mV channel first */ 375 prop = vadc_get_channel(vadc, VADC_SPARE1); 376 if (!prop) 377 prop = vadc_get_channel(vadc, VADC_REF_625MV); 378 379 ret = vadc_do_conversion(vadc, prop, &read_2); 380 if (ret) 381 goto err; 382 383 if (read_1 == read_2) { 384 ret = -EINVAL; 385 goto err; 386 } 387 388 vadc->graph[VADC_CALIB_ABSOLUTE].dy = read_1 - read_2; 389 vadc->graph[VADC_CALIB_ABSOLUTE].gnd = read_2; 390 391 /* Ratiometric calibration */ 392 prop = vadc_get_channel(vadc, VADC_VDD_VADC); 393 ret = vadc_do_conversion(vadc, prop, &read_1); 394 if (ret) 395 goto err; 396 397 prop = vadc_get_channel(vadc, VADC_GND_REF); 398 ret = vadc_do_conversion(vadc, prop, &read_2); 399 if (ret) 400 goto err; 401 402 if (read_1 == read_2) { 403 ret = -EINVAL; 404 goto err; 405 } 406 407 vadc->graph[VADC_CALIB_RATIOMETRIC].dy = read_1 - read_2; 408 vadc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_2; 409 err: 410 if (ret) 411 dev_err(vadc->dev, "measure reference points failed\n"); 412 413 return ret; 414 } 415 416 static int vadc_prescaling_from_dt(u32 num, u32 den) 417 { 418 unsigned int pre; 419 420 for (pre = 0; pre < ARRAY_SIZE(vadc_prescale_ratios); pre++) 421 if (vadc_prescale_ratios[pre].num == num && 422 vadc_prescale_ratios[pre].den == den) 423 break; 424 425 if (pre == ARRAY_SIZE(vadc_prescale_ratios)) 426 return -EINVAL; 427 428 return pre; 429 } 430 431 static int vadc_hw_settle_time_from_dt(u32 value) 432 { 433 if ((value <= 1000 && value % 100) || (value > 1000 && value % 2000)) 434 return -EINVAL; 435 436 if (value <= 1000) 437 value /= 100; 438 else 439 value = value / 2000 + 10; 440 441 return value; 442 } 443 444 static int vadc_avg_samples_from_dt(u32 value) 445 { 446 if (!is_power_of_2(value) || value > VADC_AVG_SAMPLES_MAX) 447 return -EINVAL; 448 449 return __ffs64(value); 450 } 451 452 static int vadc_read_raw(struct iio_dev *indio_dev, 453 struct iio_chan_spec const *chan, int *val, int *val2, 454 long mask) 455 { 456 struct vadc_priv *vadc = iio_priv(indio_dev); 457 struct vadc_channel_prop *prop; 458 u16 adc_code; 459 int ret; 460 461 switch (mask) { 462 case IIO_CHAN_INFO_PROCESSED: 463 prop = &vadc->chan_props[chan->address]; 464 ret = vadc_do_conversion(vadc, prop, &adc_code); 465 if (ret) 466 break; 467 468 ret = qcom_vadc_scale(prop->scale_fn_type, 469 &vadc->graph[prop->calibration], 470 &vadc_prescale_ratios[prop->prescale], 471 (prop->calibration == VADC_CALIB_ABSOLUTE), 472 adc_code, val); 473 if (ret) 474 break; 475 476 return IIO_VAL_INT; 477 case IIO_CHAN_INFO_RAW: 478 prop = &vadc->chan_props[chan->address]; 479 ret = vadc_do_conversion(vadc, prop, &adc_code); 480 if (ret) 481 break; 482 483 *val = (int)adc_code; 484 return IIO_VAL_INT; 485 default: 486 ret = -EINVAL; 487 break; 488 } 489 490 return ret; 491 } 492 493 static int vadc_of_xlate(struct iio_dev *indio_dev, 494 const struct of_phandle_args *iiospec) 495 { 496 struct vadc_priv *vadc = iio_priv(indio_dev); 497 unsigned int i; 498 499 for (i = 0; i < vadc->nchannels; i++) 500 if (vadc->iio_chans[i].channel == iiospec->args[0]) 501 return i; 502 503 return -EINVAL; 504 } 505 506 static const struct iio_info vadc_info = { 507 .read_raw = vadc_read_raw, 508 .of_xlate = vadc_of_xlate, 509 .driver_module = THIS_MODULE, 510 }; 511 512 struct vadc_channels { 513 const char *datasheet_name; 514 unsigned int prescale_index; 515 enum iio_chan_type type; 516 long info_mask; 517 enum vadc_scale_fn_type scale_fn_type; 518 }; 519 520 #define VADC_CHAN(_dname, _type, _mask, _pre, _scale) \ 521 [VADC_##_dname] = { \ 522 .datasheet_name = __stringify(_dname), \ 523 .prescale_index = _pre, \ 524 .type = _type, \ 525 .info_mask = _mask, \ 526 .scale_fn_type = _scale \ 527 }, \ 528 529 #define VADC_NO_CHAN(_dname, _type, _mask, _pre) \ 530 [VADC_##_dname] = { \ 531 .datasheet_name = __stringify(_dname), \ 532 .prescale_index = _pre, \ 533 .type = _type, \ 534 .info_mask = _mask \ 535 }, 536 537 #define VADC_CHAN_TEMP(_dname, _pre, _scale) \ 538 VADC_CHAN(_dname, IIO_TEMP, \ 539 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED), \ 540 _pre, _scale) \ 541 542 #define VADC_CHAN_VOLT(_dname, _pre, _scale) \ 543 VADC_CHAN(_dname, IIO_VOLTAGE, \ 544 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_PROCESSED),\ 545 _pre, _scale) \ 546 547 #define VADC_CHAN_NO_SCALE(_dname, _pre) \ 548 VADC_NO_CHAN(_dname, IIO_VOLTAGE, \ 549 BIT(IIO_CHAN_INFO_RAW), \ 550 _pre) \ 551 552 /* 553 * The array represents all possible ADC channels found in the supported PMICs. 554 * Every index in the array is equal to the channel number per datasheet. The 555 * gaps in the array should be treated as reserved channels. 556 */ 557 static const struct vadc_channels vadc_chans[] = { 558 VADC_CHAN_VOLT(USBIN, 4, SCALE_DEFAULT) 559 VADC_CHAN_VOLT(DCIN, 4, SCALE_DEFAULT) 560 VADC_CHAN_NO_SCALE(VCHG_SNS, 3) 561 VADC_CHAN_NO_SCALE(SPARE1_03, 1) 562 VADC_CHAN_NO_SCALE(USB_ID_MV, 1) 563 VADC_CHAN_VOLT(VCOIN, 1, SCALE_DEFAULT) 564 VADC_CHAN_NO_SCALE(VBAT_SNS, 1) 565 VADC_CHAN_VOLT(VSYS, 1, SCALE_DEFAULT) 566 VADC_CHAN_TEMP(DIE_TEMP, 0, SCALE_PMIC_THERM) 567 VADC_CHAN_VOLT(REF_625MV, 0, SCALE_DEFAULT) 568 VADC_CHAN_VOLT(REF_1250MV, 0, SCALE_DEFAULT) 569 VADC_CHAN_NO_SCALE(CHG_TEMP, 0) 570 VADC_CHAN_NO_SCALE(SPARE1, 0) 571 VADC_CHAN_TEMP(SPARE2, 0, SCALE_PMI_CHG_TEMP) 572 VADC_CHAN_VOLT(GND_REF, 0, SCALE_DEFAULT) 573 VADC_CHAN_VOLT(VDD_VADC, 0, SCALE_DEFAULT) 574 575 VADC_CHAN_NO_SCALE(P_MUX1_1_1, 0) 576 VADC_CHAN_NO_SCALE(P_MUX2_1_1, 0) 577 VADC_CHAN_NO_SCALE(P_MUX3_1_1, 0) 578 VADC_CHAN_NO_SCALE(P_MUX4_1_1, 0) 579 VADC_CHAN_NO_SCALE(P_MUX5_1_1, 0) 580 VADC_CHAN_NO_SCALE(P_MUX6_1_1, 0) 581 VADC_CHAN_NO_SCALE(P_MUX7_1_1, 0) 582 VADC_CHAN_NO_SCALE(P_MUX8_1_1, 0) 583 VADC_CHAN_NO_SCALE(P_MUX9_1_1, 0) 584 VADC_CHAN_NO_SCALE(P_MUX10_1_1, 0) 585 VADC_CHAN_NO_SCALE(P_MUX11_1_1, 0) 586 VADC_CHAN_NO_SCALE(P_MUX12_1_1, 0) 587 VADC_CHAN_NO_SCALE(P_MUX13_1_1, 0) 588 VADC_CHAN_NO_SCALE(P_MUX14_1_1, 0) 589 VADC_CHAN_NO_SCALE(P_MUX15_1_1, 0) 590 VADC_CHAN_NO_SCALE(P_MUX16_1_1, 0) 591 592 VADC_CHAN_NO_SCALE(P_MUX1_1_3, 1) 593 VADC_CHAN_NO_SCALE(P_MUX2_1_3, 1) 594 VADC_CHAN_NO_SCALE(P_MUX3_1_3, 1) 595 VADC_CHAN_NO_SCALE(P_MUX4_1_3, 1) 596 VADC_CHAN_NO_SCALE(P_MUX5_1_3, 1) 597 VADC_CHAN_NO_SCALE(P_MUX6_1_3, 1) 598 VADC_CHAN_NO_SCALE(P_MUX7_1_3, 1) 599 VADC_CHAN_NO_SCALE(P_MUX8_1_3, 1) 600 VADC_CHAN_NO_SCALE(P_MUX9_1_3, 1) 601 VADC_CHAN_NO_SCALE(P_MUX10_1_3, 1) 602 VADC_CHAN_NO_SCALE(P_MUX11_1_3, 1) 603 VADC_CHAN_NO_SCALE(P_MUX12_1_3, 1) 604 VADC_CHAN_NO_SCALE(P_MUX13_1_3, 1) 605 VADC_CHAN_NO_SCALE(P_MUX14_1_3, 1) 606 VADC_CHAN_NO_SCALE(P_MUX15_1_3, 1) 607 VADC_CHAN_NO_SCALE(P_MUX16_1_3, 1) 608 609 VADC_CHAN_NO_SCALE(LR_MUX1_BAT_THERM, 0) 610 VADC_CHAN_NO_SCALE(LR_MUX2_BAT_ID, 0) 611 VADC_CHAN_NO_SCALE(LR_MUX3_XO_THERM, 0) 612 VADC_CHAN_NO_SCALE(LR_MUX4_AMUX_THM1, 0) 613 VADC_CHAN_NO_SCALE(LR_MUX5_AMUX_THM2, 0) 614 VADC_CHAN_NO_SCALE(LR_MUX6_AMUX_THM3, 0) 615 VADC_CHAN_NO_SCALE(LR_MUX7_HW_ID, 0) 616 VADC_CHAN_NO_SCALE(LR_MUX8_AMUX_THM4, 0) 617 VADC_CHAN_NO_SCALE(LR_MUX9_AMUX_THM5, 0) 618 VADC_CHAN_NO_SCALE(LR_MUX10_USB_ID, 0) 619 VADC_CHAN_NO_SCALE(AMUX_PU1, 0) 620 VADC_CHAN_NO_SCALE(AMUX_PU2, 0) 621 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_XO_THERM, 0) 622 623 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_BAT_THERM, 0) 624 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_BAT_ID, 0) 625 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_XO_THERM, 0) 626 VADC_CHAN_TEMP(LR_MUX4_PU1_AMUX_THM1, 0, SCALE_THERM_100K_PULLUP) 627 VADC_CHAN_TEMP(LR_MUX5_PU1_AMUX_THM2, 0, SCALE_THERM_100K_PULLUP) 628 VADC_CHAN_TEMP(LR_MUX6_PU1_AMUX_THM3, 0, SCALE_THERM_100K_PULLUP) 629 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_AMUX_HW_ID, 0) 630 VADC_CHAN_TEMP(LR_MUX8_PU1_AMUX_THM4, 0, SCALE_THERM_100K_PULLUP) 631 VADC_CHAN_TEMP(LR_MUX9_PU1_AMUX_THM5, 0, SCALE_THERM_100K_PULLUP) 632 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_AMUX_USB_ID, 0) 633 VADC_CHAN_TEMP(LR_MUX3_BUF_PU1_XO_THERM, 0, SCALE_XOTHERM) 634 635 VADC_CHAN_NO_SCALE(LR_MUX1_PU2_BAT_THERM, 0) 636 VADC_CHAN_NO_SCALE(LR_MUX2_PU2_BAT_ID, 0) 637 VADC_CHAN_NO_SCALE(LR_MUX3_PU2_XO_THERM, 0) 638 VADC_CHAN_NO_SCALE(LR_MUX4_PU2_AMUX_THM1, 0) 639 VADC_CHAN_NO_SCALE(LR_MUX5_PU2_AMUX_THM2, 0) 640 VADC_CHAN_NO_SCALE(LR_MUX6_PU2_AMUX_THM3, 0) 641 VADC_CHAN_NO_SCALE(LR_MUX7_PU2_AMUX_HW_ID, 0) 642 VADC_CHAN_NO_SCALE(LR_MUX8_PU2_AMUX_THM4, 0) 643 VADC_CHAN_NO_SCALE(LR_MUX9_PU2_AMUX_THM5, 0) 644 VADC_CHAN_NO_SCALE(LR_MUX10_PU2_AMUX_USB_ID, 0) 645 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU2_XO_THERM, 0) 646 647 VADC_CHAN_NO_SCALE(LR_MUX1_PU1_PU2_BAT_THERM, 0) 648 VADC_CHAN_NO_SCALE(LR_MUX2_PU1_PU2_BAT_ID, 0) 649 VADC_CHAN_NO_SCALE(LR_MUX3_PU1_PU2_XO_THERM, 0) 650 VADC_CHAN_NO_SCALE(LR_MUX4_PU1_PU2_AMUX_THM1, 0) 651 VADC_CHAN_NO_SCALE(LR_MUX5_PU1_PU2_AMUX_THM2, 0) 652 VADC_CHAN_NO_SCALE(LR_MUX6_PU1_PU2_AMUX_THM3, 0) 653 VADC_CHAN_NO_SCALE(LR_MUX7_PU1_PU2_AMUX_HW_ID, 0) 654 VADC_CHAN_NO_SCALE(LR_MUX8_PU1_PU2_AMUX_THM4, 0) 655 VADC_CHAN_NO_SCALE(LR_MUX9_PU1_PU2_AMUX_THM5, 0) 656 VADC_CHAN_NO_SCALE(LR_MUX10_PU1_PU2_AMUX_USB_ID, 0) 657 VADC_CHAN_NO_SCALE(LR_MUX3_BUF_PU1_PU2_XO_THERM, 0) 658 }; 659 660 static int vadc_get_dt_channel_data(struct device *dev, 661 struct vadc_channel_prop *prop, 662 struct device_node *node) 663 { 664 const char *name = node->name; 665 u32 chan, value, varr[2]; 666 int ret; 667 668 ret = of_property_read_u32(node, "reg", &chan); 669 if (ret) { 670 dev_err(dev, "invalid channel number %s\n", name); 671 return ret; 672 } 673 674 if (chan > VADC_CHAN_MAX || chan < VADC_CHAN_MIN) { 675 dev_err(dev, "%s invalid channel number %d\n", name, chan); 676 return -EINVAL; 677 } 678 679 /* the channel has DT description */ 680 prop->channel = chan; 681 682 ret = of_property_read_u32(node, "qcom,decimation", &value); 683 if (!ret) { 684 ret = qcom_vadc_decimation_from_dt(value); 685 if (ret < 0) { 686 dev_err(dev, "%02x invalid decimation %d\n", 687 chan, value); 688 return ret; 689 } 690 prop->decimation = ret; 691 } else { 692 prop->decimation = VADC_DEF_DECIMATION; 693 } 694 695 ret = of_property_read_u32_array(node, "qcom,pre-scaling", varr, 2); 696 if (!ret) { 697 ret = vadc_prescaling_from_dt(varr[0], varr[1]); 698 if (ret < 0) { 699 dev_err(dev, "%02x invalid pre-scaling <%d %d>\n", 700 chan, varr[0], varr[1]); 701 return ret; 702 } 703 prop->prescale = ret; 704 } else { 705 prop->prescale = vadc_chans[prop->channel].prescale_index; 706 } 707 708 ret = of_property_read_u32(node, "qcom,hw-settle-time", &value); 709 if (!ret) { 710 ret = vadc_hw_settle_time_from_dt(value); 711 if (ret < 0) { 712 dev_err(dev, "%02x invalid hw-settle-time %d us\n", 713 chan, value); 714 return ret; 715 } 716 prop->hw_settle_time = ret; 717 } else { 718 prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME; 719 } 720 721 ret = of_property_read_u32(node, "qcom,avg-samples", &value); 722 if (!ret) { 723 ret = vadc_avg_samples_from_dt(value); 724 if (ret < 0) { 725 dev_err(dev, "%02x invalid avg-samples %d\n", 726 chan, value); 727 return ret; 728 } 729 prop->avg_samples = ret; 730 } else { 731 prop->avg_samples = VADC_DEF_AVG_SAMPLES; 732 } 733 734 if (of_property_read_bool(node, "qcom,ratiometric")) 735 prop->calibration = VADC_CALIB_RATIOMETRIC; 736 else 737 prop->calibration = VADC_CALIB_ABSOLUTE; 738 739 dev_dbg(dev, "%02x name %s\n", chan, name); 740 741 return 0; 742 } 743 744 static int vadc_get_dt_data(struct vadc_priv *vadc, struct device_node *node) 745 { 746 const struct vadc_channels *vadc_chan; 747 struct iio_chan_spec *iio_chan; 748 struct vadc_channel_prop prop; 749 struct device_node *child; 750 unsigned int index = 0; 751 int ret; 752 753 vadc->nchannels = of_get_available_child_count(node); 754 if (!vadc->nchannels) 755 return -EINVAL; 756 757 vadc->iio_chans = devm_kcalloc(vadc->dev, vadc->nchannels, 758 sizeof(*vadc->iio_chans), GFP_KERNEL); 759 if (!vadc->iio_chans) 760 return -ENOMEM; 761 762 vadc->chan_props = devm_kcalloc(vadc->dev, vadc->nchannels, 763 sizeof(*vadc->chan_props), GFP_KERNEL); 764 if (!vadc->chan_props) 765 return -ENOMEM; 766 767 iio_chan = vadc->iio_chans; 768 769 for_each_available_child_of_node(node, child) { 770 ret = vadc_get_dt_channel_data(vadc->dev, &prop, child); 771 if (ret) { 772 of_node_put(child); 773 return ret; 774 } 775 776 prop.scale_fn_type = vadc_chans[prop.channel].scale_fn_type; 777 vadc->chan_props[index] = prop; 778 779 vadc_chan = &vadc_chans[prop.channel]; 780 781 iio_chan->channel = prop.channel; 782 iio_chan->datasheet_name = vadc_chan->datasheet_name; 783 iio_chan->info_mask_separate = vadc_chan->info_mask; 784 iio_chan->type = vadc_chan->type; 785 iio_chan->indexed = 1; 786 iio_chan->address = index++; 787 788 iio_chan++; 789 } 790 791 /* These channels are mandatory, they are used as reference points */ 792 if (!vadc_get_channel(vadc, VADC_REF_1250MV)) { 793 dev_err(vadc->dev, "Please define 1.25V channel\n"); 794 return -ENODEV; 795 } 796 797 if (!vadc_get_channel(vadc, VADC_REF_625MV)) { 798 dev_err(vadc->dev, "Please define 0.625V channel\n"); 799 return -ENODEV; 800 } 801 802 if (!vadc_get_channel(vadc, VADC_VDD_VADC)) { 803 dev_err(vadc->dev, "Please define VDD channel\n"); 804 return -ENODEV; 805 } 806 807 if (!vadc_get_channel(vadc, VADC_GND_REF)) { 808 dev_err(vadc->dev, "Please define GND channel\n"); 809 return -ENODEV; 810 } 811 812 return 0; 813 } 814 815 static irqreturn_t vadc_isr(int irq, void *dev_id) 816 { 817 struct vadc_priv *vadc = dev_id; 818 819 complete(&vadc->complete); 820 821 return IRQ_HANDLED; 822 } 823 824 static int vadc_check_revision(struct vadc_priv *vadc) 825 { 826 u8 val; 827 int ret; 828 829 ret = vadc_read(vadc, VADC_PERPH_TYPE, &val); 830 if (ret) 831 return ret; 832 833 if (val < VADC_PERPH_TYPE_ADC) { 834 dev_err(vadc->dev, "%d is not ADC\n", val); 835 return -ENODEV; 836 } 837 838 ret = vadc_read(vadc, VADC_PERPH_SUBTYPE, &val); 839 if (ret) 840 return ret; 841 842 if (val < VADC_PERPH_SUBTYPE_VADC) { 843 dev_err(vadc->dev, "%d is not VADC\n", val); 844 return -ENODEV; 845 } 846 847 ret = vadc_read(vadc, VADC_REVISION2, &val); 848 if (ret) 849 return ret; 850 851 if (val < VADC_REVISION2_SUPPORTED_VADC) { 852 dev_err(vadc->dev, "revision %d not supported\n", val); 853 return -ENODEV; 854 } 855 856 return 0; 857 } 858 859 static int vadc_probe(struct platform_device *pdev) 860 { 861 struct device_node *node = pdev->dev.of_node; 862 struct device *dev = &pdev->dev; 863 struct iio_dev *indio_dev; 864 struct vadc_priv *vadc; 865 struct regmap *regmap; 866 int ret, irq_eoc; 867 u32 reg; 868 869 regmap = dev_get_regmap(dev->parent, NULL); 870 if (!regmap) 871 return -ENODEV; 872 873 ret = of_property_read_u32(node, "reg", ®); 874 if (ret < 0) 875 return ret; 876 877 indio_dev = devm_iio_device_alloc(dev, sizeof(*vadc)); 878 if (!indio_dev) 879 return -ENOMEM; 880 881 vadc = iio_priv(indio_dev); 882 vadc->regmap = regmap; 883 vadc->dev = dev; 884 vadc->base = reg; 885 vadc->are_ref_measured = false; 886 init_completion(&vadc->complete); 887 mutex_init(&vadc->lock); 888 889 ret = vadc_check_revision(vadc); 890 if (ret) 891 return ret; 892 893 ret = vadc_get_dt_data(vadc, node); 894 if (ret) 895 return ret; 896 897 irq_eoc = platform_get_irq(pdev, 0); 898 if (irq_eoc < 0) { 899 if (irq_eoc == -EPROBE_DEFER || irq_eoc == -EINVAL) 900 return irq_eoc; 901 vadc->poll_eoc = true; 902 } else { 903 ret = devm_request_irq(dev, irq_eoc, vadc_isr, 0, 904 "spmi-vadc", vadc); 905 if (ret) 906 return ret; 907 } 908 909 ret = vadc_reset(vadc); 910 if (ret) { 911 dev_err(dev, "reset failed\n"); 912 return ret; 913 } 914 915 ret = vadc_measure_ref_points(vadc); 916 if (ret) 917 return ret; 918 919 indio_dev->dev.parent = dev; 920 indio_dev->dev.of_node = node; 921 indio_dev->name = pdev->name; 922 indio_dev->modes = INDIO_DIRECT_MODE; 923 indio_dev->info = &vadc_info; 924 indio_dev->channels = vadc->iio_chans; 925 indio_dev->num_channels = vadc->nchannels; 926 927 return devm_iio_device_register(dev, indio_dev); 928 } 929 930 static const struct of_device_id vadc_match_table[] = { 931 { .compatible = "qcom,spmi-vadc" }, 932 { } 933 }; 934 MODULE_DEVICE_TABLE(of, vadc_match_table); 935 936 static struct platform_driver vadc_driver = { 937 .driver = { 938 .name = "qcom-spmi-vadc", 939 .of_match_table = vadc_match_table, 940 }, 941 .probe = vadc_probe, 942 }; 943 module_platform_driver(vadc_driver); 944 945 MODULE_ALIAS("platform:qcom-spmi-vadc"); 946 MODULE_DESCRIPTION("Qualcomm SPMI PMIC voltage ADC driver"); 947 MODULE_LICENSE("GPL v2"); 948 MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>"); 949 MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>"); 950