drm/panel: simple: add Multi-Inno Technology MI0700A2T-30
[drm/drm-misc.git] / drivers / iio / adc / pac1921.c
blobb0f6727cfe3835de5ac962e6221f39fe1d3acfb8
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * IIO driver for PAC1921 High-Side Power/Current Monitor
5 * Copyright (C) 2024 Matteo Martelli <matteomartelli3@gmail.com>
6 */
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>
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
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)
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)
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,
46 #define PAC1921_CONTROL_SLEEP BIT(2)
48 /* pac1921 result registers mask and resolution */
49 #define PAC1921_RES_MASK GENMASK(15, 6)
50 #define PAC1921_RES_RESOLUTION 1023
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)
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
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) */
71 * Pre-computed scale factors for BUS voltage
72 * format: IIO_VAL_INT_PLUS_NANO
73 * unit: mV
75 * Vbus scale (mV) = max_vbus (mV) / dv_gain / resolution
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 */
87 * Pre-computed scales for SENSE voltage
88 * format: IIO_VAL_INT_PLUS_NANO
89 * unit: mV
91 * Vsense scale (mV) = max_vsense (mV) / di_gain / resolution
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 */
105 * Numbers of samples used to integrate measurements at the end of an
106 * integration period.
108 * Changing the number of samples affects the integration period: higher the
109 * number of samples, longer the integration period.
111 * These correspond to the oversampling ratios available exposed to userspace.
113 static const int pac1921_int_num_samples[] = {
114 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048
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.
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 */
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),
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),
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),
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),
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,
167 enum pac1921_channels {
168 PAC1921_CHAN_VBUS = 0,
169 PAC1921_CHAN_VSENSE = 1,
170 PAC1921_CHAN_CURRENT = 2,
171 PAC1921_CHAN_POWER = 3,
173 #define PAC1921_NUM_MEAS_CHANS 4
175 struct pac1921_priv {
176 struct i2c_client *client;
177 struct regmap *regmap;
178 struct regulator *vdd;
179 struct iio_info iio_info;
182 * Synchronize access to private members, and ensure atomicity of
183 * consecutive regmap operations.
185 struct mutex lock;
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;
194 bool first_integr_started;
195 bool first_integr_done;
196 unsigned long integr_started_time_jiffies;
197 unsigned int integr_period_usecs;
199 int current_scales[ARRAY_SIZE(pac1921_vsense_scales)][2];
201 struct {
202 u16 chan[PAC1921_NUM_MEAS_CHANS];
203 s64 timestamp __aligned(8);
204 } scan;
208 * Check if first integration after configuration update has completed.
210 * Must be called with lock held.
212 static bool pac1921_data_ready(struct pac1921_priv *priv)
214 if (!priv->first_integr_started)
215 return false;
217 if (!priv->first_integr_done) {
218 unsigned long t_ready;
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.
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);
229 if (time_before(jiffies, t_ready))
230 return false;
232 priv->first_integr_done = true;
235 return true;
238 static inline void pac1921_calc_scale(int dividend, int divisor, int *val,
239 int *val2)
241 s64 tmp;
243 tmp = div_s64(dividend * (s64)NANO, divisor);
244 *val = div_s64_rem(tmp, NANO, val2);
248 * Fill the table of scale factors for current
249 * format: IIO_VAL_INT_PLUS_NANO
250 * unit: mA
252 * Vsense LSB (nV) = max_vsense (nV) * di_gain / resolution
253 * Current scale (mA) = Vsense LSB (nV) / shunt (uOhm)
255 * Must be called with held lock when updating after first initialization.
257 static void pac1921_calc_current_scales(struct pac1921_priv *priv)
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);
263 pac1921_calc_scale(vsense_lsb, priv->rshunt_uohm,
264 &priv->current_scales[i][0],
265 &priv->current_scales[i][1]);
270 * Check if overflow occurred and if so, push the corresponding events.
272 * Must be called with lock held.
274 static int pac1921_check_push_overflow(struct iio_dev *indio_dev, s64 timestamp)
276 struct pac1921_priv *priv = iio_priv(indio_dev);
277 unsigned int flags;
278 int ret;
280 ret = regmap_read(priv->regmap, PAC1921_REG_OVERFLOW_STS, &flags);
281 if (ret)
282 return ret;
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);
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);
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);
317 priv->prev_ovf_flags = flags;
319 return 0;
323 * Read the value from a result register
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.
329 static int pac1921_read_res(struct pac1921_priv *priv, unsigned long reg,
330 u16 *val)
332 int ret = regmap_bulk_read(priv->regmap, reg, val, sizeof(*val));
333 if (ret)
334 return ret;
336 *val = FIELD_GET(PAC1921_RES_MASK, get_unaligned_be16(val));
338 return 0;
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)
345 struct pac1921_priv *priv = iio_priv(indio_dev);
347 guard(mutex)(&priv->lock);
349 switch (mask) {
350 case IIO_CHAN_INFO_RAW: {
351 s64 ts;
352 u16 res_val;
353 int ret;
355 if (!pac1921_data_ready(priv))
356 return -EBUSY;
358 ts = iio_get_time_ns(indio_dev);
360 ret = pac1921_check_push_overflow(indio_dev, ts);
361 if (ret)
362 return ret;
364 ret = pac1921_read_res(priv, chan->address, &res_val);
365 if (ret)
366 return ret;
368 *val = res_val;
370 return IIO_VAL_INT;
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;
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;
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;
389 case PAC1921_CHAN_POWER: {
391 * Power scale factor in mW:
392 * Current scale (mA) * max_vbus (V) / dv_gain
395 /* Get current scale based on di_gain */
396 int *curr_scale = priv->current_scales[priv->di_gain];
398 /* Convert current_scale from INT_PLUS_NANO to INT */
399 s64 tmp = curr_scale[0] * (s64)NANO + curr_scale[1];
401 /* Multiply by max_vbus (V) / dv_gain */
402 tmp *= PAC1921_MAX_VBUS_V >> priv->dv_gain;
404 /* Convert back to INT_PLUS_NANO */
405 *val = div_s64_rem(tmp, NANO, val2);
407 return IIO_VAL_INT_PLUS_NANO;
409 default:
410 return -EINVAL;
413 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
414 *val = pac1921_int_num_samples[priv->n_samples];
415 return IIO_VAL_INT;
417 case IIO_CHAN_INFO_SAMP_FREQ:
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.
425 * 1/(integr_period_usecs/MICRO) = MICRO/integr_period_usecs
427 *val = MICRO;
428 *val2 = priv->integr_period_usecs;
429 return IIO_VAL_FRACTIONAL;
431 default:
432 return -EINVAL;
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)
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;
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.
458 * Must be called with lock held.
460 static int pac1921_update_cfg_reg(struct pac1921_priv *priv, unsigned int reg,
461 unsigned int mask, unsigned int val)
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;
469 /* Update configuration value */
470 ret = regmap_update_bits(priv->regmap, reg, mask, val);
471 if (ret)
472 return ret;
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;
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.
485 priv->integr_started_time_jiffies = jiffies;
486 priv->first_integr_done = false;
488 return 0;
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.
496 * Must be called with lock held if the scales_tbl can change runtime (e.g. for
497 * the current scales table)
499 static int pac1921_lookup_scale(const int (*const scales_tbl)[2], size_t size,
500 int scale_val, int scale_val2)
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;
507 return -EINVAL;
511 * Configure device with the given gain (only if changed)
513 * Must be called with lock held.
515 static int pac1921_update_gain(struct pac1921_priv *priv, u8 *priv_val, u8 gain,
516 unsigned int mask)
518 unsigned int reg_val;
519 int ret;
521 if (*priv_val == gain)
522 return 0;
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;
529 *priv_val = gain;
531 return 0;
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.
539 * Must be called with lock held.
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)
545 int ret;
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;
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;
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;
573 return pac1921_update_gain(priv, &priv->di_gain, ret,
574 PAC1921_GAIN_DI_GAIN_MASK);
575 default:
576 return -EINVAL;
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.
584 static int pac1921_lookup_int_num_samples(int num_samples)
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;
590 return -EINVAL;
594 * Update the device with the given number of integration samples.
596 * Must be called with lock held.
598 static int pac1921_update_int_num_samples(struct pac1921_priv *priv,
599 int num_samples)
601 unsigned int reg_val;
602 u8 n_samples;
603 int ret;
605 ret = pac1921_lookup_int_num_samples(num_samples);
606 if (ret < 0)
607 return ret;
609 n_samples = ret;
611 if (priv->n_samples == n_samples)
612 return 0;
614 reg_val = FIELD_PREP(PAC1921_INT_CFG_SMPL_MASK, n_samples);
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;
621 priv->n_samples = n_samples;
623 priv->integr_period_usecs = pac1921_int_periods_usecs[priv->n_samples];
625 return 0;
628 static int pac1921_write_raw_get_fmt(struct iio_dev *indio_dev,
629 struct iio_chan_spec const *chan,
630 long info)
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;
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)
646 struct pac1921_priv *priv = iio_priv(indio_dev);
648 guard(mutex)(&priv->lock);
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;
660 static int pac1921_read_label(struct iio_dev *indio_dev,
661 struct iio_chan_spec const *chan, char *label)
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;
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)
682 struct pac1921_priv *priv = iio_priv(indio_dev);
684 guard(mutex)(&priv->lock);
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;
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,
703 bool state)
705 struct pac1921_priv *priv = iio_priv(indio_dev);
706 u8 ovf_bit;
708 guard(mutex)(&priv->lock);
710 switch (chan->channel) {
711 case PAC1921_CHAN_VBUS:
712 ovf_bit = PAC1921_OVERFLOW_VBOV;
713 break;
714 case PAC1921_CHAN_VSENSE:
715 case PAC1921_CHAN_CURRENT:
716 ovf_bit = PAC1921_OVERFLOW_VSOV;
717 break;
718 case PAC1921_CHAN_POWER:
719 ovf_bit = PAC1921_OVERFLOW_VPOV;
720 break;
721 default:
722 return -EINVAL;
725 if (state)
726 priv->ovf_enabled_events |= ovf_bit;
727 else
728 priv->ovf_enabled_events &= ~ovf_bit;
730 return 0;
733 static int pac1921_read_event_value(struct iio_dev *indio_dev,
734 const struct iio_chan_spec *chan,
735 enum iio_event_type type,
736 enum iio_event_direction dir,
737 enum iio_event_info info, int *val,
738 int *val2)
740 switch (info) {
741 case IIO_EV_INFO_VALUE:
742 *val = PAC1921_RES_RESOLUTION;
743 return IIO_VAL_INT;
744 default:
745 return -EINVAL;
749 static const struct iio_info pac1921_iio = {
750 .read_raw = pac1921_read_raw,
751 .read_avail = pac1921_read_avail,
752 .write_raw = pac1921_write_raw,
753 .write_raw_get_fmt = pac1921_write_raw_get_fmt,
754 .read_label = pac1921_read_label,
755 .read_event_config = pac1921_read_event_config,
756 .write_event_config = pac1921_write_event_config,
757 .read_event_value = pac1921_read_event_value,
760 static ssize_t pac1921_read_shunt_resistor(struct iio_dev *indio_dev,
761 uintptr_t private,
762 const struct iio_chan_spec *chan,
763 char *buf)
765 struct pac1921_priv *priv = iio_priv(indio_dev);
766 int vals[2];
768 if (chan->channel != PAC1921_CHAN_CURRENT)
769 return -EINVAL;
771 guard(mutex)(&priv->lock);
773 vals[0] = priv->rshunt_uohm;
774 vals[1] = MICRO;
776 return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals);
779 static ssize_t pac1921_write_shunt_resistor(struct iio_dev *indio_dev,
780 uintptr_t private,
781 const struct iio_chan_spec *chan,
782 const char *buf, size_t len)
784 struct pac1921_priv *priv = iio_priv(indio_dev);
785 u64 rshunt_uohm;
786 int val, val_fract;
787 int ret;
789 if (chan->channel != PAC1921_CHAN_CURRENT)
790 return -EINVAL;
792 ret = iio_str_to_fixpoint(buf, 100000, &val, &val_fract);
793 if (ret)
794 return ret;
796 rshunt_uohm = val * MICRO + val_fract;
797 if (rshunt_uohm == 0 || rshunt_uohm > INT_MAX)
798 return -EINVAL;
800 guard(mutex)(&priv->lock);
802 priv->rshunt_uohm = rshunt_uohm;
804 pac1921_calc_current_scales(priv);
806 return len;
810 * Emit on sysfs the list of available scales contained in scales_tbl
812 * TODO:: this function can be replaced with iio_format_avail_list() if the
813 * latter will ever be exported.
815 * Must be called with lock held if the scales_tbl can change runtime (e.g. for
816 * the current scales table)
818 static ssize_t pac1921_format_scale_avail(const int (*const scales_tbl)[2],
819 size_t size, char *buf)
821 ssize_t len = 0;
823 for (unsigned int i = 0; i < size; i++) {
824 if (i != 0) {
825 len += sysfs_emit_at(buf, len, " ");
826 if (len >= PAGE_SIZE)
827 return -EFBIG;
829 len += sysfs_emit_at(buf, len, "%d.%09d", scales_tbl[i][0],
830 scales_tbl[i][1]);
831 if (len >= PAGE_SIZE)
832 return -EFBIG;
835 len += sysfs_emit_at(buf, len, "\n");
836 return len;
840 * Read available scales for a specific channel
842 * NOTE: using extended info insted of iio.read_avail() because access to
843 * current scales must be locked as they depend on shunt resistor which may
844 * change runtime. Caller of iio.read_avail() would access the table unlocked
845 * instead.
847 static ssize_t pac1921_read_scale_avail(struct iio_dev *indio_dev,
848 uintptr_t private,
849 const struct iio_chan_spec *chan,
850 char *buf)
852 struct pac1921_priv *priv = iio_priv(indio_dev);
853 const int (*scales_tbl)[2];
854 size_t size;
856 switch (chan->channel) {
857 case PAC1921_CHAN_VBUS:
858 scales_tbl = pac1921_vbus_scales;
859 size = ARRAY_SIZE(pac1921_vbus_scales);
860 return pac1921_format_scale_avail(scales_tbl, size, buf);
862 case PAC1921_CHAN_VSENSE:
863 scales_tbl = pac1921_vsense_scales;
864 size = ARRAY_SIZE(pac1921_vsense_scales);
865 return pac1921_format_scale_avail(scales_tbl, size, buf);
867 case PAC1921_CHAN_CURRENT: {
868 guard(mutex)(&priv->lock);
869 scales_tbl = priv->current_scales;
870 size = ARRAY_SIZE(priv->current_scales);
871 return pac1921_format_scale_avail(scales_tbl, size, buf);
873 default:
874 return -EINVAL;
878 #define PAC1921_EXT_INFO_SCALE_AVAIL { \
879 .name = "scale_available", \
880 .read = pac1921_read_scale_avail, \
881 .shared = IIO_SEPARATE, \
884 static const struct iio_chan_spec_ext_info pac1921_ext_info_voltage[] = {
885 PAC1921_EXT_INFO_SCALE_AVAIL,
889 static const struct iio_chan_spec_ext_info pac1921_ext_info_current[] = {
890 PAC1921_EXT_INFO_SCALE_AVAIL,
892 .name = "shunt_resistor",
893 .read = pac1921_read_shunt_resistor,
894 .write = pac1921_write_shunt_resistor,
895 .shared = IIO_SEPARATE,
900 static const struct iio_event_spec pac1921_overflow_event[] = {
902 .type = IIO_EV_TYPE_THRESH,
903 .dir = IIO_EV_DIR_RISING,
904 .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE),
905 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
909 static const struct iio_chan_spec pac1921_channels[] = {
911 .type = IIO_VOLTAGE,
912 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
913 BIT(IIO_CHAN_INFO_SCALE),
914 .info_mask_shared_by_all =
915 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
916 BIT(IIO_CHAN_INFO_SAMP_FREQ),
917 .info_mask_shared_by_all_available =
918 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
919 .channel = PAC1921_CHAN_VBUS,
920 .address = PAC1921_REG_VBUS,
921 .scan_index = PAC1921_CHAN_VBUS,
922 .scan_type = {
923 .sign = 'u',
924 .realbits = 10,
925 .storagebits = 16,
926 .endianness = IIO_CPU
928 .indexed = 1,
929 .event_spec = pac1921_overflow_event,
930 .num_event_specs = ARRAY_SIZE(pac1921_overflow_event),
931 .ext_info = pac1921_ext_info_voltage,
934 .type = IIO_VOLTAGE,
935 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
936 BIT(IIO_CHAN_INFO_SCALE),
937 .info_mask_shared_by_all =
938 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
939 BIT(IIO_CHAN_INFO_SAMP_FREQ),
940 .info_mask_shared_by_all_available =
941 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
942 .channel = PAC1921_CHAN_VSENSE,
943 .address = PAC1921_REG_VSENSE,
944 .scan_index = PAC1921_CHAN_VSENSE,
945 .scan_type = {
946 .sign = 'u',
947 .realbits = 10,
948 .storagebits = 16,
949 .endianness = IIO_CPU
951 .indexed = 1,
952 .event_spec = pac1921_overflow_event,
953 .num_event_specs = ARRAY_SIZE(pac1921_overflow_event),
954 .ext_info = pac1921_ext_info_voltage,
957 .type = IIO_CURRENT,
958 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
959 BIT(IIO_CHAN_INFO_SCALE),
960 .info_mask_shared_by_all =
961 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
962 BIT(IIO_CHAN_INFO_SAMP_FREQ),
963 .info_mask_shared_by_all_available =
964 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
965 .channel = PAC1921_CHAN_CURRENT,
966 .address = PAC1921_REG_VSENSE,
967 .scan_index = PAC1921_CHAN_CURRENT,
968 .scan_type = {
969 .sign = 'u',
970 .realbits = 10,
971 .storagebits = 16,
972 .endianness = IIO_CPU
974 .event_spec = pac1921_overflow_event,
975 .num_event_specs = ARRAY_SIZE(pac1921_overflow_event),
976 .ext_info = pac1921_ext_info_current,
979 .type = IIO_POWER,
980 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
981 BIT(IIO_CHAN_INFO_SCALE),
982 .info_mask_shared_by_all =
983 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
984 BIT(IIO_CHAN_INFO_SAMP_FREQ),
985 .info_mask_shared_by_all_available =
986 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
987 .channel = PAC1921_CHAN_POWER,
988 .address = PAC1921_REG_VPOWER,
989 .scan_index = PAC1921_CHAN_POWER,
990 .scan_type = {
991 .sign = 'u',
992 .realbits = 10,
993 .storagebits = 16,
994 .endianness = IIO_CPU
996 .event_spec = pac1921_overflow_event,
997 .num_event_specs = ARRAY_SIZE(pac1921_overflow_event),
999 IIO_CHAN_SOFT_TIMESTAMP(PAC1921_NUM_MEAS_CHANS),
1002 static irqreturn_t pac1921_trigger_handler(int irq, void *p)
1004 struct iio_poll_func *pf = p;
1005 struct iio_dev *idev = pf->indio_dev;
1006 struct pac1921_priv *priv = iio_priv(idev);
1007 int ret;
1008 int bit;
1009 int ch = 0;
1011 guard(mutex)(&priv->lock);
1013 if (!pac1921_data_ready(priv))
1014 goto done;
1016 ret = pac1921_check_push_overflow(idev, pf->timestamp);
1017 if (ret)
1018 goto done;
1020 iio_for_each_active_channel(idev, bit) {
1021 u16 val;
1023 ret = pac1921_read_res(priv, idev->channels[ch].address, &val);
1024 if (ret)
1025 goto done;
1027 priv->scan.chan[ch++] = val;
1030 iio_push_to_buffers_with_timestamp(idev, &priv->scan, pf->timestamp);
1032 done:
1033 iio_trigger_notify_done(idev->trig);
1035 return IRQ_HANDLED;
1039 * Initialize device by writing initial configuration and putting it into
1040 * integration state.
1042 * Must be called with lock held when called after first initialization
1043 * (e.g. from pm resume)
1045 static int pac1921_init(struct pac1921_priv *priv)
1047 unsigned int val;
1048 int ret;
1050 /* Enter READ state before configuration */
1051 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
1052 PAC1921_INT_CFG_INTEN, 0);
1053 if (ret)
1054 return ret;
1056 /* Configure gains, use 14-bits measurement resolution (HW default) */
1057 val = FIELD_PREP(PAC1921_GAIN_DI_GAIN_MASK, priv->di_gain) |
1058 FIELD_PREP(PAC1921_GAIN_DV_GAIN_MASK, priv->dv_gain);
1059 ret = regmap_write(priv->regmap, PAC1921_REG_GAIN_CFG, val);
1060 if (ret)
1061 return ret;
1064 * Configure integration:
1065 * - num of integration samples
1066 * - filters enabled (HW default)
1067 * - set READ/INT pin override (RIOV) to control operation mode via
1068 * register instead of pin
1070 val = FIELD_PREP(PAC1921_INT_CFG_SMPL_MASK, priv->n_samples) |
1071 PAC1921_INT_CFG_VSFEN | PAC1921_INT_CFG_VBFEN |
1072 PAC1921_INT_CFG_RIOV;
1073 ret = regmap_write(priv->regmap, PAC1921_REG_INT_CFG, val);
1074 if (ret)
1075 return ret;
1078 * Init control register:
1079 * - VPower free run integration mode
1080 * - OUT pin full scale range: 3V (HW default)
1081 * - no timeout, no sleep, no sleep override, no recalc (HW defaults)
1083 val = FIELD_PREP(PAC1921_CONTROL_MXSL_MASK,
1084 PAC1921_MXSL_VPOWER_FREE_RUN);
1085 ret = regmap_write(priv->regmap, PAC1921_REG_CONTROL, val);
1086 if (ret)
1087 return ret;
1089 /* Enable integration */
1090 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
1091 PAC1921_INT_CFG_INTEN, PAC1921_INT_CFG_INTEN);
1092 if (ret)
1093 return ret;
1095 priv->first_integr_started = true;
1096 priv->integr_started_time_jiffies = jiffies;
1097 priv->integr_period_usecs = pac1921_int_periods_usecs[priv->n_samples];
1099 return 0;
1102 static int pac1921_suspend(struct device *dev)
1104 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1105 struct pac1921_priv *priv = iio_priv(indio_dev);
1106 int ret;
1108 guard(mutex)(&priv->lock);
1110 priv->first_integr_started = false;
1111 priv->first_integr_done = false;
1113 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
1114 PAC1921_INT_CFG_INTEN, 0);
1115 if (ret)
1116 return ret;
1118 ret = regmap_update_bits(priv->regmap, PAC1921_REG_CONTROL,
1119 PAC1921_CONTROL_SLEEP, PAC1921_CONTROL_SLEEP);
1120 if (ret)
1121 return ret;
1123 return regulator_disable(priv->vdd);
1127 static int pac1921_resume(struct device *dev)
1129 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1130 struct pac1921_priv *priv = iio_priv(indio_dev);
1131 int ret;
1133 guard(mutex)(&priv->lock);
1135 ret = regulator_enable(priv->vdd);
1136 if (ret)
1137 return ret;
1139 msleep(PAC1921_POWERUP_TIME_MS);
1141 return pac1921_init(priv);
1144 static DEFINE_SIMPLE_DEV_PM_OPS(pac1921_pm_ops, pac1921_suspend,
1145 pac1921_resume);
1147 static void pac1921_regulator_disable(void *data)
1149 struct regulator *regulator = data;
1151 regulator_disable(regulator);
1154 static int pac1921_probe(struct i2c_client *client)
1156 struct device *dev = &client->dev;
1157 struct pac1921_priv *priv;
1158 struct iio_dev *indio_dev;
1159 int ret;
1161 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
1162 if (!indio_dev)
1163 return -ENOMEM;
1165 priv = iio_priv(indio_dev);
1166 priv->client = client;
1167 i2c_set_clientdata(client, indio_dev);
1169 priv->regmap = devm_regmap_init_i2c(client, &pac1921_regmap_config);
1170 if (IS_ERR(priv->regmap))
1171 return dev_err_probe(dev, PTR_ERR(priv->regmap),
1172 "Cannot initialize register map\n");
1174 ret = devm_mutex_init(dev, &priv->lock);
1175 if (ret)
1176 return ret;
1178 priv->dv_gain = PAC1921_DEFAULT_DV_GAIN;
1179 priv->di_gain = PAC1921_DEFAULT_DI_GAIN;
1180 priv->n_samples = PAC1921_DEFAULT_NUM_SAMPLES;
1182 ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms",
1183 &priv->rshunt_uohm);
1184 if (ret)
1185 return dev_err_probe(dev, ret,
1186 "Cannot read shunt resistor property\n");
1187 if (priv->rshunt_uohm == 0 || priv->rshunt_uohm > INT_MAX)
1188 return dev_err_probe(dev, -EINVAL,
1189 "Invalid shunt resistor: %u\n",
1190 priv->rshunt_uohm);
1192 pac1921_calc_current_scales(priv);
1194 priv->vdd = devm_regulator_get(dev, "vdd");
1195 if (IS_ERR(priv->vdd))
1196 return dev_err_probe(dev, PTR_ERR(priv->vdd),
1197 "Cannot get vdd regulator\n");
1199 ret = regulator_enable(priv->vdd);
1200 if (ret)
1201 return dev_err_probe(dev, ret, "Cannot enable vdd regulator\n");
1203 ret = devm_add_action_or_reset(dev, pac1921_regulator_disable,
1204 priv->vdd);
1205 if (ret)
1206 return dev_err_probe(dev, ret,
1207 "Cannot add action for vdd regulator disposal\n");
1209 msleep(PAC1921_POWERUP_TIME_MS);
1211 ret = pac1921_init(priv);
1212 if (ret)
1213 return dev_err_probe(dev, ret, "Cannot initialize device\n");
1215 priv->iio_info = pac1921_iio;
1217 indio_dev->name = "pac1921";
1218 indio_dev->info = &priv->iio_info;
1219 indio_dev->modes = INDIO_DIRECT_MODE;
1220 indio_dev->channels = pac1921_channels;
1221 indio_dev->num_channels = ARRAY_SIZE(pac1921_channels);
1223 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
1224 &iio_pollfunc_store_time,
1225 &pac1921_trigger_handler, NULL);
1226 if (ret)
1227 return dev_err_probe(dev, ret,
1228 "Cannot setup IIO triggered buffer\n");
1230 ret = devm_iio_device_register(dev, indio_dev);
1231 if (ret)
1232 return dev_err_probe(dev, ret, "Cannot register IIO device\n");
1234 return 0;
1237 static const struct i2c_device_id pac1921_id[] = {
1238 { .name = "pac1921", 0 },
1241 MODULE_DEVICE_TABLE(i2c, pac1921_id);
1243 static const struct of_device_id pac1921_of_match[] = {
1244 { .compatible = "microchip,pac1921" },
1247 MODULE_DEVICE_TABLE(of, pac1921_of_match);
1249 static struct i2c_driver pac1921_driver = {
1250 .driver = {
1251 .name = "pac1921",
1252 .pm = pm_sleep_ptr(&pac1921_pm_ops),
1253 .of_match_table = pac1921_of_match,
1255 .probe = pac1921_probe,
1256 .id_table = pac1921_id,
1259 module_i2c_driver(pac1921_driver);
1261 MODULE_AUTHOR("Matteo Martelli <matteomartelli3@gmail.com>");
1262 MODULE_DESCRIPTION("IIO driver for PAC1921 High-Side Power/Current Monitor");
1263 MODULE_LICENSE("GPL");