1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * IIO driver for PAC1921 High-Side Power/Current Monitor 4 * 5 * Copyright (C) 2024 Matteo Martelli <matteomartelli3@gmail.com> 6 */ 7 8 #include <linux/unaligned.h> 9 #include <linux/bitfield.h> 10 #include <linux/i2c.h> 11 #include <linux/iio/events.h> 12 #include <linux/iio/iio.h> 13 #include <linux/iio/trigger_consumer.h> 14 #include <linux/iio/triggered_buffer.h> 15 #include <linux/regmap.h> 16 #include <linux/units.h> 17 18 /* pac1921 registers */ 19 #define PAC1921_REG_GAIN_CFG 0x00 20 #define PAC1921_REG_INT_CFG 0x01 21 #define PAC1921_REG_CONTROL 0x02 22 #define PAC1921_REG_VBUS 0x10 23 #define PAC1921_REG_VSENSE 0x12 24 #define PAC1921_REG_OVERFLOW_STS 0x1C 25 #define PAC1921_REG_VPOWER 0x1D 26 27 /* pac1921 gain configuration bits */ 28 #define PAC1921_GAIN_DI_GAIN_MASK GENMASK(5, 3) 29 #define PAC1921_GAIN_DV_GAIN_MASK GENMASK(2, 0) 30 31 /* pac1921 integration configuration bits */ 32 #define PAC1921_INT_CFG_SMPL_MASK GENMASK(7, 4) 33 #define PAC1921_INT_CFG_VSFEN BIT(3) 34 #define PAC1921_INT_CFG_VBFEN BIT(2) 35 #define PAC1921_INT_CFG_RIOV BIT(1) 36 #define PAC1921_INT_CFG_INTEN BIT(0) 37 38 /* pac1921 control bits */ 39 #define PAC1921_CONTROL_MXSL_MASK GENMASK(7, 6) 40 enum pac1921_mxsl { 41 PAC1921_MXSL_VPOWER_PIN = 0, 42 PAC1921_MXSL_VSENSE_FREE_RUN = 1, 43 PAC1921_MXSL_VBUS_FREE_RUN = 2, 44 PAC1921_MXSL_VPOWER_FREE_RUN = 3, 45 }; 46 #define PAC1921_CONTROL_SLEEP BIT(2) 47 48 /* pac1921 result registers mask and resolution */ 49 #define PAC1921_RES_MASK GENMASK(15, 6) 50 #define PAC1921_RES_RESOLUTION 1023 51 52 /* pac1921 overflow status bits */ 53 #define PAC1921_OVERFLOW_VSOV BIT(2) 54 #define PAC1921_OVERFLOW_VBOV BIT(1) 55 #define PAC1921_OVERFLOW_VPOV BIT(0) 56 57 /* pac1921 constants */ 58 #define PAC1921_MAX_VSENSE_MV 100 59 #define PAC1921_MAX_VBUS_V 32 60 /* Time to first communication after power up (tINT_T) */ 61 #define PAC1921_POWERUP_TIME_MS 20 62 /* Time from Sleep State to Start of Integration Period (tSLEEP_TO_INT) */ 63 #define PAC1921_SLEEP_TO_INT_TIME_US 86 64 65 /* pac1921 defaults */ 66 #define PAC1921_DEFAULT_DV_GAIN 0 /* 2^(value): 1x gain (HW default) */ 67 #define PAC1921_DEFAULT_DI_GAIN 0 /* 2^(value): 1x gain (HW default) */ 68 #define PAC1921_DEFAULT_NUM_SAMPLES 0 /* 2^(value): 1 sample (HW default) */ 69 70 /* 71 * Pre-computed scale factors for BUS voltage 72 * format: IIO_VAL_INT_PLUS_NANO 73 * unit: mV 74 * 75 * Vbus scale (mV) = max_vbus (mV) / dv_gain / resolution 76 */ 77 static const int pac1921_vbus_scales[][2] = { 78 { 31, 280547409 }, /* dv_gain x1 */ 79 { 15, 640273704 }, /* dv_gain x2 */ 80 { 7, 820136852 }, /* dv_gain x4 */ 81 { 3, 910068426 }, /* dv_gain x8 */ 82 { 1, 955034213 }, /* dv_gain x16 */ 83 { 0, 977517106 }, /* dv_gain x32 */ 84 }; 85 86 /* 87 * Pre-computed scales for SENSE voltage 88 * format: IIO_VAL_INT_PLUS_NANO 89 * unit: mV 90 * 91 * Vsense scale (mV) = max_vsense (mV) / di_gain / resolution 92 */ 93 static const int pac1921_vsense_scales[][2] = { 94 { 0, 97751710 }, /* di_gain x1 */ 95 { 0, 48875855 }, /* di_gain x2 */ 96 { 0, 24437927 }, /* di_gain x4 */ 97 { 0, 12218963 }, /* di_gain x8 */ 98 { 0, 6109481 }, /* di_gain x16 */ 99 { 0, 3054740 }, /* di_gain x32 */ 100 { 0, 1527370 }, /* di_gain x64 */ 101 { 0, 763685 }, /* di_gain x128 */ 102 }; 103 104 /* 105 * Numbers of samples used to integrate measurements at the end of an 106 * integration period. 107 * 108 * Changing the number of samples affects the integration period: higher the 109 * number of samples, longer the integration period. 110 * 111 * These correspond to the oversampling ratios available exposed to userspace. 112 */ 113 static const int pac1921_int_num_samples[] = { 114 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 115 }; 116 117 /* 118 * The integration period depends on the configuration of number of integration 119 * samples, measurement resolution and post filters. The following array 120 * contains integration periods, in microsecs unit, based on table 4-5 from 121 * datasheet considering power integration mode, 14-Bit resolution and post 122 * filters on. Each index corresponds to a specific number of samples from 1 123 * to 2048. 124 */ 125 static const unsigned int pac1921_int_periods_usecs[] = { 126 2720, /* 1 sample */ 127 4050, /* 2 samples */ 128 6790, /* 4 samples */ 129 12200, /* 8 samples */ 130 23000, /* 16 samples */ 131 46000, /* 32 samples */ 132 92000, /* 64 samples */ 133 184000, /* 128 samples */ 134 368000, /* 256 samples */ 135 736000, /* 512 samples */ 136 1471000, /* 1024 samples */ 137 2941000 /* 2048 samples */ 138 }; 139 140 /* pac1921 regmap configuration */ 141 static const struct regmap_range pac1921_regmap_wr_ranges[] = { 142 regmap_reg_range(PAC1921_REG_GAIN_CFG, PAC1921_REG_CONTROL), 143 }; 144 145 static const struct regmap_access_table pac1921_regmap_wr_table = { 146 .yes_ranges = pac1921_regmap_wr_ranges, 147 .n_yes_ranges = ARRAY_SIZE(pac1921_regmap_wr_ranges), 148 }; 149 150 static const struct regmap_range pac1921_regmap_rd_ranges[] = { 151 regmap_reg_range(PAC1921_REG_GAIN_CFG, PAC1921_REG_CONTROL), 152 regmap_reg_range(PAC1921_REG_VBUS, PAC1921_REG_VPOWER + 1), 153 }; 154 155 static const struct regmap_access_table pac1921_regmap_rd_table = { 156 .yes_ranges = pac1921_regmap_rd_ranges, 157 .n_yes_ranges = ARRAY_SIZE(pac1921_regmap_rd_ranges), 158 }; 159 160 static const struct regmap_config pac1921_regmap_config = { 161 .reg_bits = 8, 162 .val_bits = 8, 163 .rd_table = &pac1921_regmap_rd_table, 164 .wr_table = &pac1921_regmap_wr_table, 165 }; 166 167 enum pac1921_channels { 168 PAC1921_CHAN_VBUS = 0, 169 PAC1921_CHAN_VSENSE = 1, 170 PAC1921_CHAN_CURRENT = 2, 171 PAC1921_CHAN_POWER = 3, 172 }; 173 #define PAC1921_NUM_MEAS_CHANS 4 174 175 struct pac1921_priv { 176 struct i2c_client *client; 177 struct regmap *regmap; 178 struct regulator *vdd; 179 struct iio_info iio_info; 180 181 /* 182 * Synchronize access to private members, and ensure atomicity of 183 * consecutive regmap operations. 184 */ 185 struct mutex lock; 186 187 u32 rshunt_uohm; /* uOhm */ 188 u8 dv_gain; 189 u8 di_gain; 190 u8 n_samples; 191 u8 prev_ovf_flags; 192 u8 ovf_enabled_events; 193 194 bool first_integr_started; 195 bool first_integr_done; 196 unsigned long integr_started_time_jiffies; 197 unsigned int integr_period_usecs; 198 199 int current_scales[ARRAY_SIZE(pac1921_vsense_scales)][2]; 200 201 struct { 202 u16 chan[PAC1921_NUM_MEAS_CHANS]; 203 s64 timestamp __aligned(8); 204 } scan; 205 }; 206 207 /* 208 * Check if first integration after configuration update has completed. 209 * 210 * Must be called with lock held. 211 */ 212 static bool pac1921_data_ready(struct pac1921_priv *priv) 213 { 214 if (!priv->first_integr_started) 215 return false; 216 217 if (!priv->first_integr_done) { 218 unsigned long t_ready; 219 220 /* 221 * Data valid after the device entered into integration state, 222 * considering worst case where the device was in sleep state, 223 * and completed the first integration period. 224 */ 225 t_ready = priv->integr_started_time_jiffies + 226 usecs_to_jiffies(PAC1921_SLEEP_TO_INT_TIME_US) + 227 usecs_to_jiffies(priv->integr_period_usecs); 228 229 if (time_before(jiffies, t_ready)) 230 return false; 231 232 priv->first_integr_done = true; 233 } 234 235 return true; 236 } 237 238 static inline void pac1921_calc_scale(int dividend, int divisor, int *val, 239 int *val2) 240 { 241 s64 tmp; 242 243 tmp = div_s64(dividend * (s64)NANO, divisor); 244 *val = div_s64_rem(tmp, NANO, val2); 245 } 246 247 /* 248 * Fill the table of scale factors for current 249 * format: IIO_VAL_INT_PLUS_NANO 250 * unit: mA 251 * 252 * Vsense LSB (nV) = max_vsense (nV) * di_gain / resolution 253 * Current scale (mA) = Vsense LSB (nV) / shunt (uOhm) 254 * 255 * Must be called with held lock when updating after first initialization. 256 */ 257 static void pac1921_calc_current_scales(struct pac1921_priv *priv) 258 { 259 for (unsigned int i = 0; i < ARRAY_SIZE(priv->current_scales); i++) { 260 int max = (PAC1921_MAX_VSENSE_MV * MICRO) >> i; 261 int vsense_lsb = DIV_ROUND_CLOSEST(max, PAC1921_RES_RESOLUTION); 262 263 pac1921_calc_scale(vsense_lsb, priv->rshunt_uohm, 264 &priv->current_scales[i][0], 265 &priv->current_scales[i][1]); 266 } 267 } 268 269 /* 270 * Check if overflow occurred and if so, push the corresponding events. 271 * 272 * Must be called with lock held. 273 */ 274 static int pac1921_check_push_overflow(struct iio_dev *indio_dev, s64 timestamp) 275 { 276 struct pac1921_priv *priv = iio_priv(indio_dev); 277 unsigned int flags; 278 int ret; 279 280 ret = regmap_read(priv->regmap, PAC1921_REG_OVERFLOW_STS, &flags); 281 if (ret) 282 return ret; 283 284 if (flags & PAC1921_OVERFLOW_VBOV && 285 !(priv->prev_ovf_flags & PAC1921_OVERFLOW_VBOV) && 286 priv->ovf_enabled_events & PAC1921_OVERFLOW_VBOV) { 287 iio_push_event(indio_dev, 288 IIO_UNMOD_EVENT_CODE( 289 IIO_VOLTAGE, PAC1921_CHAN_VBUS, 290 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 291 timestamp); 292 } 293 if (flags & PAC1921_OVERFLOW_VSOV && 294 !(priv->prev_ovf_flags & PAC1921_OVERFLOW_VSOV) && 295 priv->ovf_enabled_events & PAC1921_OVERFLOW_VSOV) { 296 iio_push_event(indio_dev, 297 IIO_UNMOD_EVENT_CODE( 298 IIO_VOLTAGE, PAC1921_CHAN_VSENSE, 299 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 300 timestamp); 301 iio_push_event(indio_dev, 302 IIO_UNMOD_EVENT_CODE( 303 IIO_CURRENT, PAC1921_CHAN_CURRENT, 304 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 305 timestamp); 306 } 307 if (flags & PAC1921_OVERFLOW_VPOV && 308 !(priv->prev_ovf_flags & PAC1921_OVERFLOW_VPOV) && 309 priv->ovf_enabled_events & PAC1921_OVERFLOW_VPOV) { 310 iio_push_event(indio_dev, 311 IIO_UNMOD_EVENT_CODE( 312 IIO_POWER, PAC1921_CHAN_POWER, 313 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 314 timestamp); 315 } 316 317 priv->prev_ovf_flags = flags; 318 319 return 0; 320 } 321 322 /* 323 * Read the value from a result register 324 * 325 * Result registers contain the most recent averaged values of Vbus, Vsense and 326 * Vpower. Each value is 10 bits wide and spread across two consecutive 8 bit 327 * registers, with 6 bit LSB zero padding. 328 */ 329 static int pac1921_read_res(struct pac1921_priv *priv, unsigned long reg, 330 u16 *val) 331 { 332 int ret = regmap_bulk_read(priv->regmap, reg, val, sizeof(*val)); 333 if (ret) 334 return ret; 335 336 *val = FIELD_GET(PAC1921_RES_MASK, get_unaligned_be16(val)); 337 338 return 0; 339 } 340 341 static int pac1921_read_raw(struct iio_dev *indio_dev, 342 struct iio_chan_spec const *chan, int *val, 343 int *val2, long mask) 344 { 345 struct pac1921_priv *priv = iio_priv(indio_dev); 346 347 guard(mutex)(&priv->lock); 348 349 switch (mask) { 350 case IIO_CHAN_INFO_RAW: { 351 s64 ts; 352 u16 res_val; 353 int ret; 354 355 if (!pac1921_data_ready(priv)) 356 return -EBUSY; 357 358 ts = iio_get_time_ns(indio_dev); 359 360 ret = pac1921_check_push_overflow(indio_dev, ts); 361 if (ret) 362 return ret; 363 364 ret = pac1921_read_res(priv, chan->address, &res_val); 365 if (ret) 366 return ret; 367 368 *val = res_val; 369 370 return IIO_VAL_INT; 371 } 372 case IIO_CHAN_INFO_SCALE: 373 switch (chan->channel) { 374 case PAC1921_CHAN_VBUS: 375 *val = pac1921_vbus_scales[priv->dv_gain][0]; 376 *val2 = pac1921_vbus_scales[priv->dv_gain][1]; 377 return IIO_VAL_INT_PLUS_NANO; 378 379 case PAC1921_CHAN_VSENSE: 380 *val = pac1921_vsense_scales[priv->di_gain][0]; 381 *val2 = pac1921_vsense_scales[priv->di_gain][1]; 382 return IIO_VAL_INT_PLUS_NANO; 383 384 case PAC1921_CHAN_CURRENT: 385 *val = priv->current_scales[priv->di_gain][0]; 386 *val2 = priv->current_scales[priv->di_gain][1]; 387 return IIO_VAL_INT_PLUS_NANO; 388 389 case PAC1921_CHAN_POWER: { 390 /* 391 * Power scale factor in mW: 392 * Current scale (mA) * max_vbus (V) / dv_gain 393 */ 394 395 /* Get current scale based on di_gain */ 396 int *curr_scale = priv->current_scales[priv->di_gain]; 397 398 /* Convert current_scale from INT_PLUS_NANO to INT */ 399 s64 tmp = curr_scale[0] * (s64)NANO + curr_scale[1]; 400 401 /* Multiply by max_vbus (V) / dv_gain */ 402 tmp *= PAC1921_MAX_VBUS_V >> priv->dv_gain; 403 404 /* Convert back to INT_PLUS_NANO */ 405 *val = div_s64_rem(tmp, NANO, val2); 406 407 return IIO_VAL_INT_PLUS_NANO; 408 } 409 default: 410 return -EINVAL; 411 } 412 413 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 414 *val = pac1921_int_num_samples[priv->n_samples]; 415 return IIO_VAL_INT; 416 417 case IIO_CHAN_INFO_SAMP_FREQ: 418 /* 419 * The sampling frequency (Hz) is read-only and corresponds to 420 * how often the device provides integrated measurements into 421 * the result registers, thus it's 1/integration_period. 422 * The integration period depends on the number of integration 423 * samples, measurement resolution and post filters. 424 * 425 * 1/(integr_period_usecs/MICRO) = MICRO/integr_period_usecs 426 */ 427 *val = MICRO; 428 *val2 = priv->integr_period_usecs; 429 return IIO_VAL_FRACTIONAL; 430 431 default: 432 return -EINVAL; 433 } 434 } 435 436 static int pac1921_read_avail(struct iio_dev *indio_dev, 437 struct iio_chan_spec const *chan, 438 const int **vals, int *type, int *length, 439 long mask) 440 { 441 switch (mask) { 442 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 443 *type = IIO_VAL_INT; 444 *vals = pac1921_int_num_samples; 445 *length = ARRAY_SIZE(pac1921_int_num_samples); 446 return IIO_AVAIL_LIST; 447 default: 448 return -EINVAL; 449 } 450 } 451 452 /* 453 * Perform configuration update sequence: set the device into read state, then 454 * write the config register and set the device back into integration state. 455 * Also reset integration start time and mark first integration to be yet 456 * completed. 457 * 458 * Must be called with lock held. 459 */ 460 static int pac1921_update_cfg_reg(struct pac1921_priv *priv, unsigned int reg, 461 unsigned int mask, unsigned int val) 462 { 463 /* Enter READ state before configuration */ 464 int ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 465 PAC1921_INT_CFG_INTEN, 0); 466 if (ret) 467 return ret; 468 469 /* Update configuration value */ 470 ret = regmap_update_bits(priv->regmap, reg, mask, val); 471 if (ret) 472 return ret; 473 474 /* Re-enable integration */ 475 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 476 PAC1921_INT_CFG_INTEN, PAC1921_INT_CFG_INTEN); 477 if (ret) 478 return ret; 479 480 /* 481 * Reset integration started time and mark this integration period as 482 * the first one so that new measurements will be considered as valid 483 * only at the end of this integration period. 484 */ 485 priv->integr_started_time_jiffies = jiffies; 486 priv->first_integr_done = false; 487 488 return 0; 489 } 490 491 /* 492 * Retrieve the index of the given scale (represented by scale_val and 493 * scale_val2) from scales_tbl. The returned index (if found) is the log2 of 494 * the gain corresponding to the given scale. 495 * 496 * Must be called with lock held if the scales_tbl can change runtime (e.g. for 497 * the current scales table) 498 */ 499 static int pac1921_lookup_scale(const int (*const scales_tbl)[2], size_t size, 500 int scale_val, int scale_val2) 501 { 502 for (unsigned int i = 0; i < size; i++) 503 if (scales_tbl[i][0] == scale_val && 504 scales_tbl[i][1] == scale_val2) 505 return i; 506 507 return -EINVAL; 508 } 509 510 /* 511 * Configure device with the given gain (only if changed) 512 * 513 * Must be called with lock held. 514 */ 515 static int pac1921_update_gain(struct pac1921_priv *priv, u8 *priv_val, u8 gain, 516 unsigned int mask) 517 { 518 unsigned int reg_val; 519 int ret; 520 521 if (*priv_val == gain) 522 return 0; 523 524 reg_val = (gain << __ffs(mask)) & mask; 525 ret = pac1921_update_cfg_reg(priv, PAC1921_REG_GAIN_CFG, mask, reg_val); 526 if (ret) 527 return ret; 528 529 *priv_val = gain; 530 531 return 0; 532 } 533 534 /* 535 * Given a scale factor represented by scale_val and scale_val2 with format 536 * IIO_VAL_INT_PLUS_NANO, find the corresponding gain value and write it to the 537 * device. 538 * 539 * Must be called with lock held. 540 */ 541 static int pac1921_update_gain_from_scale(struct pac1921_priv *priv, 542 struct iio_chan_spec const *chan, 543 int scale_val, int scale_val2) 544 { 545 int ret; 546 547 switch (chan->channel) { 548 case PAC1921_CHAN_VBUS: 549 ret = pac1921_lookup_scale(pac1921_vbus_scales, 550 ARRAY_SIZE(pac1921_vbus_scales), 551 scale_val, scale_val2); 552 if (ret < 0) 553 return ret; 554 555 return pac1921_update_gain(priv, &priv->dv_gain, ret, 556 PAC1921_GAIN_DV_GAIN_MASK); 557 case PAC1921_CHAN_VSENSE: 558 ret = pac1921_lookup_scale(pac1921_vsense_scales, 559 ARRAY_SIZE(pac1921_vsense_scales), 560 scale_val, scale_val2); 561 if (ret < 0) 562 return ret; 563 564 return pac1921_update_gain(priv, &priv->di_gain, ret, 565 PAC1921_GAIN_DI_GAIN_MASK); 566 case PAC1921_CHAN_CURRENT: 567 ret = pac1921_lookup_scale(priv->current_scales, 568 ARRAY_SIZE(priv->current_scales), 569 scale_val, scale_val2); 570 if (ret < 0) 571 return ret; 572 573 return pac1921_update_gain(priv, &priv->di_gain, ret, 574 PAC1921_GAIN_DI_GAIN_MASK); 575 default: 576 return -EINVAL; 577 } 578 } 579 580 /* 581 * Retrieve the index of the given number of samples from the constant table. 582 * The returned index (if found) is the log2 of the given num_samples. 583 */ 584 static int pac1921_lookup_int_num_samples(int num_samples) 585 { 586 for (unsigned int i = 0; i < ARRAY_SIZE(pac1921_int_num_samples); i++) 587 if (pac1921_int_num_samples[i] == num_samples) 588 return i; 589 590 return -EINVAL; 591 } 592 593 /* 594 * Update the device with the given number of integration samples. 595 * 596 * Must be called with lock held. 597 */ 598 static int pac1921_update_int_num_samples(struct pac1921_priv *priv, 599 int num_samples) 600 { 601 unsigned int reg_val; 602 u8 n_samples; 603 int ret; 604 605 ret = pac1921_lookup_int_num_samples(num_samples); 606 if (ret < 0) 607 return ret; 608 609 n_samples = ret; 610 611 if (priv->n_samples == n_samples) 612 return 0; 613 614 reg_val = FIELD_PREP(PAC1921_INT_CFG_SMPL_MASK, n_samples); 615 616 ret = pac1921_update_cfg_reg(priv, PAC1921_REG_INT_CFG, 617 PAC1921_INT_CFG_SMPL_MASK, reg_val); 618 if (ret) 619 return ret; 620 621 priv->n_samples = n_samples; 622 623 priv->integr_period_usecs = pac1921_int_periods_usecs[priv->n_samples]; 624 625 return 0; 626 } 627 628 static int pac1921_write_raw_get_fmt(struct iio_dev *indio_dev, 629 struct iio_chan_spec const *chan, 630 long info) 631 { 632 switch (info) { 633 case IIO_CHAN_INFO_SCALE: 634 return IIO_VAL_INT_PLUS_NANO; 635 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 636 return IIO_VAL_INT; 637 default: 638 return -EINVAL; 639 } 640 } 641 642 static int pac1921_write_raw(struct iio_dev *indio_dev, 643 struct iio_chan_spec const *chan, int val, 644 int val2, long mask) 645 { 646 struct pac1921_priv *priv = iio_priv(indio_dev); 647 648 guard(mutex)(&priv->lock); 649 650 switch (mask) { 651 case IIO_CHAN_INFO_SCALE: 652 return pac1921_update_gain_from_scale(priv, chan, val, val2); 653 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 654 return pac1921_update_int_num_samples(priv, val); 655 default: 656 return -EINVAL; 657 } 658 } 659 660 static int pac1921_read_label(struct iio_dev *indio_dev, 661 struct iio_chan_spec const *chan, char *label) 662 { 663 switch (chan->channel) { 664 case PAC1921_CHAN_VBUS: 665 return sprintf(label, "vbus\n"); 666 case PAC1921_CHAN_VSENSE: 667 return sprintf(label, "vsense\n"); 668 case PAC1921_CHAN_CURRENT: 669 return sprintf(label, "current\n"); 670 case PAC1921_CHAN_POWER: 671 return sprintf(label, "power\n"); 672 default: 673 return -EINVAL; 674 } 675 } 676 677 static int pac1921_read_event_config(struct iio_dev *indio_dev, 678 const struct iio_chan_spec *chan, 679 enum iio_event_type type, 680 enum iio_event_direction dir) 681 { 682 struct pac1921_priv *priv = iio_priv(indio_dev); 683 684 guard(mutex)(&priv->lock); 685 686 switch (chan->channel) { 687 case PAC1921_CHAN_VBUS: 688 return !!(priv->ovf_enabled_events & PAC1921_OVERFLOW_VBOV); 689 case PAC1921_CHAN_VSENSE: 690 case PAC1921_CHAN_CURRENT: 691 return !!(priv->ovf_enabled_events & PAC1921_OVERFLOW_VSOV); 692 case PAC1921_CHAN_POWER: 693 return !!(priv->ovf_enabled_events & PAC1921_OVERFLOW_VPOV); 694 default: 695 return -EINVAL; 696 } 697 } 698 699 static int pac1921_write_event_config(struct iio_dev *indio_dev, 700 const struct iio_chan_spec *chan, 701 enum iio_event_type type, 702 enum iio_event_direction dir, int state) 703 { 704 struct pac1921_priv *priv = iio_priv(indio_dev); 705 u8 ovf_bit; 706 707 guard(mutex)(&priv->lock); 708 709 switch (chan->channel) { 710 case PAC1921_CHAN_VBUS: 711 ovf_bit = PAC1921_OVERFLOW_VBOV; 712 break; 713 case PAC1921_CHAN_VSENSE: 714 case PAC1921_CHAN_CURRENT: 715 ovf_bit = PAC1921_OVERFLOW_VSOV; 716 break; 717 case PAC1921_CHAN_POWER: 718 ovf_bit = PAC1921_OVERFLOW_VPOV; 719 break; 720 default: 721 return -EINVAL; 722 } 723 724 if (state) 725 priv->ovf_enabled_events |= ovf_bit; 726 else 727 priv->ovf_enabled_events &= ~ovf_bit; 728 729 return 0; 730 } 731 732 static int pac1921_read_event_value(struct iio_dev *indio_dev, 733 const struct iio_chan_spec *chan, 734 enum iio_event_type type, 735 enum iio_event_direction dir, 736 enum iio_event_info info, int *val, 737 int *val2) 738 { 739 switch (info) { 740 case IIO_EV_INFO_VALUE: 741 *val = PAC1921_RES_RESOLUTION; 742 return IIO_VAL_INT; 743 default: 744 return -EINVAL; 745 } 746 } 747 748 static const struct iio_info pac1921_iio = { 749 .read_raw = pac1921_read_raw, 750 .read_avail = pac1921_read_avail, 751 .write_raw = pac1921_write_raw, 752 .write_raw_get_fmt = pac1921_write_raw_get_fmt, 753 .read_label = pac1921_read_label, 754 .read_event_config = pac1921_read_event_config, 755 .write_event_config = pac1921_write_event_config, 756 .read_event_value = pac1921_read_event_value, 757 }; 758 759 static ssize_t pac1921_read_shunt_resistor(struct iio_dev *indio_dev, 760 uintptr_t private, 761 const struct iio_chan_spec *chan, 762 char *buf) 763 { 764 struct pac1921_priv *priv = iio_priv(indio_dev); 765 int vals[2]; 766 767 if (chan->channel != PAC1921_CHAN_CURRENT) 768 return -EINVAL; 769 770 guard(mutex)(&priv->lock); 771 772 vals[0] = priv->rshunt_uohm; 773 vals[1] = MICRO; 774 775 return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals); 776 } 777 778 static ssize_t pac1921_write_shunt_resistor(struct iio_dev *indio_dev, 779 uintptr_t private, 780 const struct iio_chan_spec *chan, 781 const char *buf, size_t len) 782 { 783 struct pac1921_priv *priv = iio_priv(indio_dev); 784 u64 rshunt_uohm; 785 int val, val_fract; 786 int ret; 787 788 if (chan->channel != PAC1921_CHAN_CURRENT) 789 return -EINVAL; 790 791 ret = iio_str_to_fixpoint(buf, 100000, &val, &val_fract); 792 if (ret) 793 return ret; 794 795 rshunt_uohm = val * MICRO + val_fract; 796 if (rshunt_uohm == 0 || rshunt_uohm > INT_MAX) 797 return -EINVAL; 798 799 guard(mutex)(&priv->lock); 800 801 priv->rshunt_uohm = rshunt_uohm; 802 803 pac1921_calc_current_scales(priv); 804 805 return len; 806 } 807 808 /* 809 * Emit on sysfs the list of available scales contained in scales_tbl 810 * 811 * TODO:: this function can be replaced with iio_format_avail_list() if the 812 * latter will ever be exported. 813 * 814 * Must be called with lock held if the scales_tbl can change runtime (e.g. for 815 * the current scales table) 816 */ 817 static ssize_t pac1921_format_scale_avail(const int (*const scales_tbl)[2], 818 size_t size, char *buf) 819 { 820 ssize_t len = 0; 821 822 for (unsigned int i = 0; i < size; i++) { 823 if (i != 0) { 824 len += sysfs_emit_at(buf, len, " "); 825 if (len >= PAGE_SIZE) 826 return -EFBIG; 827 } 828 len += sysfs_emit_at(buf, len, "%d.%09d", scales_tbl[i][0], 829 scales_tbl[i][1]); 830 if (len >= PAGE_SIZE) 831 return -EFBIG; 832 } 833 834 len += sysfs_emit_at(buf, len, "\n"); 835 return len; 836 } 837 838 /* 839 * Read available scales for a specific channel 840 * 841 * NOTE: using extended info insted of iio.read_avail() because access to 842 * current scales must be locked as they depend on shunt resistor which may 843 * change runtime. Caller of iio.read_avail() would access the table unlocked 844 * instead. 845 */ 846 static ssize_t pac1921_read_scale_avail(struct iio_dev *indio_dev, 847 uintptr_t private, 848 const struct iio_chan_spec *chan, 849 char *buf) 850 { 851 struct pac1921_priv *priv = iio_priv(indio_dev); 852 const int (*scales_tbl)[2]; 853 size_t size; 854 855 switch (chan->channel) { 856 case PAC1921_CHAN_VBUS: 857 scales_tbl = pac1921_vbus_scales; 858 size = ARRAY_SIZE(pac1921_vbus_scales); 859 return pac1921_format_scale_avail(scales_tbl, size, buf); 860 861 case PAC1921_CHAN_VSENSE: 862 scales_tbl = pac1921_vsense_scales; 863 size = ARRAY_SIZE(pac1921_vsense_scales); 864 return pac1921_format_scale_avail(scales_tbl, size, buf); 865 866 case PAC1921_CHAN_CURRENT: { 867 guard(mutex)(&priv->lock); 868 scales_tbl = priv->current_scales; 869 size = ARRAY_SIZE(priv->current_scales); 870 return pac1921_format_scale_avail(scales_tbl, size, buf); 871 } 872 default: 873 return -EINVAL; 874 } 875 } 876 877 #define PAC1921_EXT_INFO_SCALE_AVAIL { \ 878 .name = "scale_available", \ 879 .read = pac1921_read_scale_avail, \ 880 .shared = IIO_SEPARATE, \ 881 } 882 883 static const struct iio_chan_spec_ext_info pac1921_ext_info_voltage[] = { 884 PAC1921_EXT_INFO_SCALE_AVAIL, 885 {} 886 }; 887 888 static const struct iio_chan_spec_ext_info pac1921_ext_info_current[] = { 889 PAC1921_EXT_INFO_SCALE_AVAIL, 890 { 891 .name = "shunt_resistor", 892 .read = pac1921_read_shunt_resistor, 893 .write = pac1921_write_shunt_resistor, 894 .shared = IIO_SEPARATE, 895 }, 896 {} 897 }; 898 899 static const struct iio_event_spec pac1921_overflow_event[] = { 900 { 901 .type = IIO_EV_TYPE_THRESH, 902 .dir = IIO_EV_DIR_RISING, 903 .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE), 904 .mask_separate = BIT(IIO_EV_INFO_ENABLE), 905 }, 906 }; 907 908 static const struct iio_chan_spec pac1921_channels[] = { 909 { 910 .type = IIO_VOLTAGE, 911 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 912 BIT(IIO_CHAN_INFO_SCALE), 913 .info_mask_shared_by_all = 914 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | 915 BIT(IIO_CHAN_INFO_SAMP_FREQ), 916 .info_mask_shared_by_all_available = 917 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 918 .channel = PAC1921_CHAN_VBUS, 919 .address = PAC1921_REG_VBUS, 920 .scan_index = PAC1921_CHAN_VBUS, 921 .scan_type = { 922 .sign = 'u', 923 .realbits = 10, 924 .storagebits = 16, 925 .endianness = IIO_CPU 926 }, 927 .indexed = 1, 928 .event_spec = pac1921_overflow_event, 929 .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), 930 .ext_info = pac1921_ext_info_voltage, 931 }, 932 { 933 .type = IIO_VOLTAGE, 934 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 935 BIT(IIO_CHAN_INFO_SCALE), 936 .info_mask_shared_by_all = 937 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | 938 BIT(IIO_CHAN_INFO_SAMP_FREQ), 939 .info_mask_shared_by_all_available = 940 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 941 .channel = PAC1921_CHAN_VSENSE, 942 .address = PAC1921_REG_VSENSE, 943 .scan_index = PAC1921_CHAN_VSENSE, 944 .scan_type = { 945 .sign = 'u', 946 .realbits = 10, 947 .storagebits = 16, 948 .endianness = IIO_CPU 949 }, 950 .indexed = 1, 951 .event_spec = pac1921_overflow_event, 952 .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), 953 .ext_info = pac1921_ext_info_voltage, 954 }, 955 { 956 .type = IIO_CURRENT, 957 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 958 BIT(IIO_CHAN_INFO_SCALE), 959 .info_mask_shared_by_all = 960 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | 961 BIT(IIO_CHAN_INFO_SAMP_FREQ), 962 .info_mask_shared_by_all_available = 963 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 964 .channel = PAC1921_CHAN_CURRENT, 965 .address = PAC1921_REG_VSENSE, 966 .scan_index = PAC1921_CHAN_CURRENT, 967 .scan_type = { 968 .sign = 'u', 969 .realbits = 10, 970 .storagebits = 16, 971 .endianness = IIO_CPU 972 }, 973 .event_spec = pac1921_overflow_event, 974 .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), 975 .ext_info = pac1921_ext_info_current, 976 }, 977 { 978 .type = IIO_POWER, 979 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 980 BIT(IIO_CHAN_INFO_SCALE), 981 .info_mask_shared_by_all = 982 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | 983 BIT(IIO_CHAN_INFO_SAMP_FREQ), 984 .info_mask_shared_by_all_available = 985 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 986 .channel = PAC1921_CHAN_POWER, 987 .address = PAC1921_REG_VPOWER, 988 .scan_index = PAC1921_CHAN_POWER, 989 .scan_type = { 990 .sign = 'u', 991 .realbits = 10, 992 .storagebits = 16, 993 .endianness = IIO_CPU 994 }, 995 .event_spec = pac1921_overflow_event, 996 .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), 997 }, 998 IIO_CHAN_SOFT_TIMESTAMP(PAC1921_NUM_MEAS_CHANS), 999 }; 1000 1001 static irqreturn_t pac1921_trigger_handler(int irq, void *p) 1002 { 1003 struct iio_poll_func *pf = p; 1004 struct iio_dev *idev = pf->indio_dev; 1005 struct pac1921_priv *priv = iio_priv(idev); 1006 int ret; 1007 int bit; 1008 int ch = 0; 1009 1010 guard(mutex)(&priv->lock); 1011 1012 if (!pac1921_data_ready(priv)) 1013 goto done; 1014 1015 ret = pac1921_check_push_overflow(idev, pf->timestamp); 1016 if (ret) 1017 goto done; 1018 1019 iio_for_each_active_channel(idev, bit) { 1020 u16 val; 1021 1022 ret = pac1921_read_res(priv, idev->channels[ch].address, &val); 1023 if (ret) 1024 goto done; 1025 1026 priv->scan.chan[ch++] = val; 1027 } 1028 1029 iio_push_to_buffers_with_timestamp(idev, &priv->scan, pf->timestamp); 1030 1031 done: 1032 iio_trigger_notify_done(idev->trig); 1033 1034 return IRQ_HANDLED; 1035 } 1036 1037 /* 1038 * Initialize device by writing initial configuration and putting it into 1039 * integration state. 1040 * 1041 * Must be called with lock held when called after first initialization 1042 * (e.g. from pm resume) 1043 */ 1044 static int pac1921_init(struct pac1921_priv *priv) 1045 { 1046 unsigned int val; 1047 int ret; 1048 1049 /* Enter READ state before configuration */ 1050 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 1051 PAC1921_INT_CFG_INTEN, 0); 1052 if (ret) 1053 return ret; 1054 1055 /* Configure gains, use 14-bits measurement resolution (HW default) */ 1056 val = FIELD_PREP(PAC1921_GAIN_DI_GAIN_MASK, priv->di_gain) | 1057 FIELD_PREP(PAC1921_GAIN_DV_GAIN_MASK, priv->dv_gain); 1058 ret = regmap_write(priv->regmap, PAC1921_REG_GAIN_CFG, val); 1059 if (ret) 1060 return ret; 1061 1062 /* 1063 * Configure integration: 1064 * - num of integration samples 1065 * - filters enabled (HW default) 1066 * - set READ/INT pin override (RIOV) to control operation mode via 1067 * register instead of pin 1068 */ 1069 val = FIELD_PREP(PAC1921_INT_CFG_SMPL_MASK, priv->n_samples) | 1070 PAC1921_INT_CFG_VSFEN | PAC1921_INT_CFG_VBFEN | 1071 PAC1921_INT_CFG_RIOV; 1072 ret = regmap_write(priv->regmap, PAC1921_REG_INT_CFG, val); 1073 if (ret) 1074 return ret; 1075 1076 /* 1077 * Init control register: 1078 * - VPower free run integration mode 1079 * - OUT pin full scale range: 3V (HW default) 1080 * - no timeout, no sleep, no sleep override, no recalc (HW defaults) 1081 */ 1082 val = FIELD_PREP(PAC1921_CONTROL_MXSL_MASK, 1083 PAC1921_MXSL_VPOWER_FREE_RUN); 1084 ret = regmap_write(priv->regmap, PAC1921_REG_CONTROL, val); 1085 if (ret) 1086 return ret; 1087 1088 /* Enable integration */ 1089 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 1090 PAC1921_INT_CFG_INTEN, PAC1921_INT_CFG_INTEN); 1091 if (ret) 1092 return ret; 1093 1094 priv->first_integr_started = true; 1095 priv->integr_started_time_jiffies = jiffies; 1096 priv->integr_period_usecs = pac1921_int_periods_usecs[priv->n_samples]; 1097 1098 return 0; 1099 } 1100 1101 static int pac1921_suspend(struct device *dev) 1102 { 1103 struct iio_dev *indio_dev = dev_get_drvdata(dev); 1104 struct pac1921_priv *priv = iio_priv(indio_dev); 1105 int ret; 1106 1107 guard(mutex)(&priv->lock); 1108 1109 priv->first_integr_started = false; 1110 priv->first_integr_done = false; 1111 1112 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 1113 PAC1921_INT_CFG_INTEN, 0); 1114 if (ret) 1115 return ret; 1116 1117 ret = regmap_update_bits(priv->regmap, PAC1921_REG_CONTROL, 1118 PAC1921_CONTROL_SLEEP, PAC1921_CONTROL_SLEEP); 1119 if (ret) 1120 return ret; 1121 1122 return regulator_disable(priv->vdd); 1123 1124 } 1125 1126 static int pac1921_resume(struct device *dev) 1127 { 1128 struct iio_dev *indio_dev = dev_get_drvdata(dev); 1129 struct pac1921_priv *priv = iio_priv(indio_dev); 1130 int ret; 1131 1132 guard(mutex)(&priv->lock); 1133 1134 ret = regulator_enable(priv->vdd); 1135 if (ret) 1136 return ret; 1137 1138 msleep(PAC1921_POWERUP_TIME_MS); 1139 1140 return pac1921_init(priv); 1141 } 1142 1143 static DEFINE_SIMPLE_DEV_PM_OPS(pac1921_pm_ops, pac1921_suspend, 1144 pac1921_resume); 1145 1146 static void pac1921_regulator_disable(void *data) 1147 { 1148 struct regulator *regulator = data; 1149 1150 regulator_disable(regulator); 1151 } 1152 1153 static int pac1921_probe(struct i2c_client *client) 1154 { 1155 struct device *dev = &client->dev; 1156 struct pac1921_priv *priv; 1157 struct iio_dev *indio_dev; 1158 int ret; 1159 1160 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv)); 1161 if (!indio_dev) 1162 return -ENOMEM; 1163 1164 priv = iio_priv(indio_dev); 1165 priv->client = client; 1166 i2c_set_clientdata(client, indio_dev); 1167 1168 priv->regmap = devm_regmap_init_i2c(client, &pac1921_regmap_config); 1169 if (IS_ERR(priv->regmap)) 1170 return dev_err_probe(dev, PTR_ERR(priv->regmap), 1171 "Cannot initialize register map\n"); 1172 1173 devm_mutex_init(dev, &priv->lock); 1174 1175 priv->dv_gain = PAC1921_DEFAULT_DV_GAIN; 1176 priv->di_gain = PAC1921_DEFAULT_DI_GAIN; 1177 priv->n_samples = PAC1921_DEFAULT_NUM_SAMPLES; 1178 1179 ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms", 1180 &priv->rshunt_uohm); 1181 if (ret) 1182 return dev_err_probe(dev, ret, 1183 "Cannot read shunt resistor property\n"); 1184 if (priv->rshunt_uohm == 0 || priv->rshunt_uohm > INT_MAX) 1185 return dev_err_probe(dev, -EINVAL, 1186 "Invalid shunt resistor: %u\n", 1187 priv->rshunt_uohm); 1188 1189 pac1921_calc_current_scales(priv); 1190 1191 priv->vdd = devm_regulator_get(dev, "vdd"); 1192 if (IS_ERR(priv->vdd)) 1193 return dev_err_probe(dev, PTR_ERR(priv->vdd), 1194 "Cannot get vdd regulator\n"); 1195 1196 ret = regulator_enable(priv->vdd); 1197 if (ret) 1198 return dev_err_probe(dev, ret, "Cannot enable vdd regulator\n"); 1199 1200 ret = devm_add_action_or_reset(dev, pac1921_regulator_disable, 1201 priv->vdd); 1202 if (ret) 1203 return dev_err_probe(dev, ret, 1204 "Cannot add action for vdd regulator disposal\n"); 1205 1206 msleep(PAC1921_POWERUP_TIME_MS); 1207 1208 ret = pac1921_init(priv); 1209 if (ret) 1210 return dev_err_probe(dev, ret, "Cannot initialize device\n"); 1211 1212 priv->iio_info = pac1921_iio; 1213 1214 indio_dev->name = "pac1921"; 1215 indio_dev->info = &priv->iio_info; 1216 indio_dev->modes = INDIO_DIRECT_MODE; 1217 indio_dev->channels = pac1921_channels; 1218 indio_dev->num_channels = ARRAY_SIZE(pac1921_channels); 1219 1220 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 1221 &iio_pollfunc_store_time, 1222 &pac1921_trigger_handler, NULL); 1223 if (ret) 1224 return dev_err_probe(dev, ret, 1225 "Cannot setup IIO triggered buffer\n"); 1226 1227 ret = devm_iio_device_register(dev, indio_dev); 1228 if (ret) 1229 return dev_err_probe(dev, ret, "Cannot register IIO device\n"); 1230 1231 return 0; 1232 } 1233 1234 static const struct i2c_device_id pac1921_id[] = { 1235 { .name = "pac1921", 0 }, 1236 { } 1237 }; 1238 MODULE_DEVICE_TABLE(i2c, pac1921_id); 1239 1240 static const struct of_device_id pac1921_of_match[] = { 1241 { .compatible = "microchip,pac1921" }, 1242 { } 1243 }; 1244 MODULE_DEVICE_TABLE(of, pac1921_of_match); 1245 1246 static struct i2c_driver pac1921_driver = { 1247 .driver = { 1248 .name = "pac1921", 1249 .pm = pm_sleep_ptr(&pac1921_pm_ops), 1250 .of_match_table = pac1921_of_match, 1251 }, 1252 .probe = pac1921_probe, 1253 .id_table = pac1921_id, 1254 }; 1255 1256 module_i2c_driver(pac1921_driver); 1257 1258 MODULE_AUTHOR("Matteo Martelli <matteomartelli3@gmail.com>"); 1259 MODULE_DESCRIPTION("IIO driver for PAC1921 High-Side Power/Current Monitor"); 1260 MODULE_LICENSE("GPL"); 1261