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)
42 enum 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 */
76 static 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 */
87 static 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 */
103 static 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 */
123 static 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 */
135 static 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 */
151 static const struct regmap_range pac1921_regmap_wr_ranges[] = {
152 regmap_reg_range(PAC1921_REG_GAIN_CFG, PAC1921_REG_CONTROL),
153 };
154
155 static 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
160 static 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
165 static 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
170 static 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
177 enum 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
185 struct 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 */
pac1921_data_ready(struct pac1921_priv * priv)222 static 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
pac1921_calc_scale(int dividend,int divisor,int * val,int * val2)248 static 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 */
pac1921_calc_current_scales(struct pac1921_priv * priv)267 static 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 */
pac1921_check_push_overflow(struct iio_dev * indio_dev,s64 timestamp)284 static 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 */
pac1921_read_res(struct pac1921_priv * priv,unsigned long reg,u16 * val)339 static 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
pac1921_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)351 static 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
pac1921_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)446 static 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 */
pac1921_update_cfg_reg(struct pac1921_priv * priv,unsigned int reg,unsigned int mask,unsigned int val)470 static 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 */
pac1921_lookup_scale(const int (* const scales_tbl)[2],size_t size,int scale_val,int scale_val2)509 static 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 */
pac1921_update_gain(struct pac1921_priv * priv,u8 * priv_val,u8 gain,unsigned int mask)525 static 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 */
pac1921_update_gain_from_scale(struct pac1921_priv * priv,struct iio_chan_spec const * chan,int scale_val,int scale_val2)551 static 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 */
pac1921_lookup_int_num_samples(int num_samples)594 static 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 */
pac1921_update_int_num_samples(struct pac1921_priv * priv,int num_samples)608 static 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
pac1921_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long info)638 static 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
pac1921_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)652 static 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
pac1921_read_label(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,char * label)670 static 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 sprintf(label, "vbus\n");
676 case PAC1921_CHAN_VSENSE:
677 return sprintf(label, "vsense\n");
678 case PAC1921_CHAN_CURRENT:
679 return sprintf(label, "current\n");
680 case PAC1921_CHAN_POWER:
681 return sprintf(label, "power\n");
682 default:
683 return -EINVAL;
684 }
685 }
686
pac1921_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)687 static 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
pac1921_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,bool state)709 static 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
pac1921_read_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)743 static 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
759 static 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
pac1921_read_shunt_resistor(struct iio_dev * indio_dev,uintptr_t private,const struct iio_chan_spec * chan,char * buf)770 static 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
pac1921_write_shunt_resistor(struct iio_dev * indio_dev,uintptr_t private,const struct iio_chan_spec * chan,const char * buf,size_t len)789 static 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 */
pac1921_format_scale_avail(const int (* const scales_tbl)[2],size_t size,char * buf)835 static 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 */
pac1921_read_scale_avail(struct iio_dev * indio_dev,uintptr_t private,const struct iio_chan_spec * chan,char * buf)864 static 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
901 static const struct iio_chan_spec_ext_info pac1921_ext_info_voltage[] = {
902 PAC1921_EXT_INFO_SCALE_AVAIL,
903 {}
904 };
905
906 static 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
917 static 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
926 static 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
pac1921_trigger_handler(int irq,void * p)1019 static 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_timestamp(idev, &priv->scan, pf->timestamp);
1048
1049 done:
1050 iio_trigger_notify_done(idev->trig);
1051
1052 return IRQ_HANDLED;
1053 }
1054
1055 /*
1056 * Initialize device by writing initial configuration and putting it into
1057 * integration state.
1058 *
1059 * Must be called with lock held when called after first initialization
1060 * (e.g. from pm resume)
1061 */
pac1921_init(struct pac1921_priv * priv)1062 static int pac1921_init(struct pac1921_priv *priv)
1063 {
1064 unsigned int val;
1065 int ret;
1066
1067 /* Enter READ state before configuration */
1068 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
1069 PAC1921_INT_CFG_INTEN, 0);
1070 if (ret)
1071 return ret;
1072
1073 /* Configure gains, use 14-bits measurement resolution (HW default) */
1074 val = FIELD_PREP(PAC1921_GAIN_DI_GAIN_MASK, priv->di_gain) |
1075 FIELD_PREP(PAC1921_GAIN_DV_GAIN_MASK, priv->dv_gain);
1076 ret = regmap_write(priv->regmap, PAC1921_REG_GAIN_CFG, val);
1077 if (ret)
1078 return ret;
1079
1080 /*
1081 * Configure integration:
1082 * - num of integration samples
1083 * - filters enabled (HW default)
1084 * - set READ/INT pin override (RIOV) to control operation mode via
1085 * register instead of pin
1086 */
1087 val = FIELD_PREP(PAC1921_INT_CFG_SMPL_MASK, priv->n_samples) |
1088 PAC1921_INT_CFG_VSFEN | PAC1921_INT_CFG_VBFEN |
1089 PAC1921_INT_CFG_RIOV;
1090 ret = regmap_write(priv->regmap, PAC1921_REG_INT_CFG, val);
1091 if (ret)
1092 return ret;
1093
1094 /*
1095 * Init control register:
1096 * - VPower free run integration mode
1097 * - OUT pin full scale range: 3V (HW default)
1098 * - no timeout, no sleep, no sleep override, no recalc (HW defaults)
1099 */
1100 val = FIELD_PREP(PAC1921_CONTROL_MXSL_MASK,
1101 PAC1921_MXSL_VPOWER_FREE_RUN);
1102 ret = regmap_write(priv->regmap, PAC1921_REG_CONTROL, val);
1103 if (ret)
1104 return ret;
1105
1106 /* Enable integration */
1107 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
1108 PAC1921_INT_CFG_INTEN, PAC1921_INT_CFG_INTEN);
1109 if (ret)
1110 return ret;
1111
1112 priv->first_integr_started = true;
1113 priv->integr_started_time_jiffies = jiffies;
1114 priv->integr_period_usecs = pac1921_int_periods_usecs[priv->n_samples];
1115
1116 return 0;
1117 }
1118
pac1921_suspend(struct device * dev)1119 static int pac1921_suspend(struct device *dev)
1120 {
1121 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1122 struct pac1921_priv *priv = iio_priv(indio_dev);
1123 int ret;
1124
1125 guard(mutex)(&priv->lock);
1126
1127 priv->first_integr_started = false;
1128 priv->first_integr_done = false;
1129
1130 ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG,
1131 PAC1921_INT_CFG_INTEN, 0);
1132 if (ret)
1133 return ret;
1134
1135 ret = regmap_update_bits(priv->regmap, PAC1921_REG_CONTROL,
1136 PAC1921_CONTROL_SLEEP, PAC1921_CONTROL_SLEEP);
1137 if (ret)
1138 return ret;
1139
1140 return regulator_disable(priv->vdd);
1141
1142 }
1143
pac1921_resume(struct device * dev)1144 static int pac1921_resume(struct device *dev)
1145 {
1146 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1147 struct pac1921_priv *priv = iio_priv(indio_dev);
1148 int ret;
1149
1150 guard(mutex)(&priv->lock);
1151
1152 ret = regulator_enable(priv->vdd);
1153 if (ret)
1154 return ret;
1155
1156 msleep(PAC1921_POWERUP_TIME_MS);
1157
1158 return pac1921_init(priv);
1159 }
1160
1161 static DEFINE_SIMPLE_DEV_PM_OPS(pac1921_pm_ops, pac1921_suspend,
1162 pac1921_resume);
1163
pac1921_regulator_disable(void * data)1164 static void pac1921_regulator_disable(void *data)
1165 {
1166 struct regulator *regulator = data;
1167
1168 regulator_disable(regulator);
1169 }
1170
1171 /*
1172 * Documentation related to the ACPI device definition
1173 * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC193X-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf
1174 */
pac1921_match_acpi_device(struct iio_dev * indio_dev)1175 static int pac1921_match_acpi_device(struct iio_dev *indio_dev)
1176 {
1177 acpi_handle handle;
1178 union acpi_object *status;
1179 char *label;
1180 struct pac1921_priv *priv = iio_priv(indio_dev);
1181 struct device *dev = &priv->client->dev;
1182
1183 handle = ACPI_HANDLE(dev);
1184
1185 status = acpi_evaluate_dsm(handle, &pac1921_guid, 1,
1186 PAC1921_ACPI_GET_uOHMS_VALS, NULL);
1187 if (!status)
1188 return dev_err_probe(dev, -EINVAL,
1189 "Could not read shunt from ACPI table\n");
1190
1191 priv->rshunt_uohm = status->package.elements[0].integer.value;
1192 ACPI_FREE(status);
1193
1194 status = acpi_evaluate_dsm(handle, &pac1921_guid, 1,
1195 PAC1921_ACPI_GET_LABEL, NULL);
1196 if (!status)
1197 return dev_err_probe(dev, -EINVAL,
1198 "Could not read label from ACPI table\n");
1199
1200 label = devm_kstrdup(dev, status->package.elements[0].string.pointer,
1201 GFP_KERNEL);
1202 ACPI_FREE(status);
1203 if (!label)
1204 return -ENOMEM;
1205
1206 indio_dev->label = label;
1207
1208 return 0;
1209 }
1210
pac1921_parse_of_fw(struct iio_dev * indio_dev)1211 static int pac1921_parse_of_fw(struct iio_dev *indio_dev)
1212 {
1213 int ret;
1214 struct pac1921_priv *priv = iio_priv(indio_dev);
1215 struct device *dev = &priv->client->dev;
1216
1217 ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms",
1218 &priv->rshunt_uohm);
1219 if (ret)
1220 return dev_err_probe(dev, ret,
1221 "Cannot read shunt resistor property\n");
1222
1223 return 0;
1224 }
1225
pac1921_probe(struct i2c_client * client)1226 static int pac1921_probe(struct i2c_client *client)
1227 {
1228 struct device *dev = &client->dev;
1229 struct pac1921_priv *priv;
1230 struct iio_dev *indio_dev;
1231 int ret;
1232
1233 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
1234 if (!indio_dev)
1235 return -ENOMEM;
1236
1237 priv = iio_priv(indio_dev);
1238 priv->client = client;
1239 i2c_set_clientdata(client, indio_dev);
1240
1241 priv->regmap = devm_regmap_init_i2c(client, &pac1921_regmap_config);
1242 if (IS_ERR(priv->regmap))
1243 return dev_err_probe(dev, PTR_ERR(priv->regmap),
1244 "Cannot initialize register map\n");
1245
1246 ret = devm_mutex_init(dev, &priv->lock);
1247 if (ret)
1248 return ret;
1249
1250 priv->dv_gain = PAC1921_DEFAULT_DV_GAIN;
1251 priv->di_gain = PAC1921_DEFAULT_DI_GAIN;
1252 priv->n_samples = PAC1921_DEFAULT_NUM_SAMPLES;
1253
1254 if (is_acpi_device_node(dev->fwnode))
1255 ret = pac1921_match_acpi_device(indio_dev);
1256 else
1257 ret = pac1921_parse_of_fw(indio_dev);
1258 if (ret)
1259 return dev_err_probe(dev, ret,
1260 "Parameter parsing error\n");
1261
1262 if (priv->rshunt_uohm == 0 || priv->rshunt_uohm > INT_MAX)
1263 return dev_err_probe(dev, -EINVAL,
1264 "Invalid shunt resistor: %u\n",
1265 priv->rshunt_uohm);
1266
1267 pac1921_calc_current_scales(priv);
1268
1269 priv->vdd = devm_regulator_get(dev, "vdd");
1270 if (IS_ERR(priv->vdd))
1271 return dev_err_probe(dev, PTR_ERR(priv->vdd),
1272 "Cannot get vdd regulator\n");
1273
1274 ret = regulator_enable(priv->vdd);
1275 if (ret)
1276 return dev_err_probe(dev, ret, "Cannot enable vdd regulator\n");
1277
1278 ret = devm_add_action_or_reset(dev, pac1921_regulator_disable,
1279 priv->vdd);
1280 if (ret)
1281 return dev_err_probe(dev, ret,
1282 "Cannot add action for vdd regulator disposal\n");
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
1312 static const struct i2c_device_id pac1921_id[] = {
1313 { .name = "pac1921", 0 },
1314 { }
1315 };
1316 MODULE_DEVICE_TABLE(i2c, pac1921_id);
1317
1318 static const struct of_device_id pac1921_of_match[] = {
1319 { .compatible = "microchip,pac1921" },
1320 { }
1321 };
1322 MODULE_DEVICE_TABLE(of, pac1921_of_match);
1323
1324 static const struct acpi_device_id pac1921_acpi_match[] = {
1325 { "MCHP1921" },
1326 { }
1327 };
1328 MODULE_DEVICE_TABLE(acpi, pac1921_acpi_match);
1329
1330 static 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
1341 module_i2c_driver(pac1921_driver);
1342
1343 MODULE_AUTHOR("Matteo Martelli <matteomartelli3@gmail.com>");
1344 MODULE_DESCRIPTION("IIO driver for PAC1921 High-Side Power/Current Monitor");
1345 MODULE_LICENSE("GPL");
1346