File Index
Files and directories
Sign in
FILE

pac1921.c

File preview with syntax highlighting.

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