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