1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 Richtek Technology Corp.
5 * ChiYuan Huang <cy_huang@richtek.com>
8 #include <linux/bitops.h>
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/kernel.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
17 #include <linux/sysfs.h>
18 #include <linux/types.h>
19 #include <linux/util_macros.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
27 #define RTQ6056_REG_CONFIG 0x00
28 #define RTQ6056_REG_SHUNTVOLT 0x01
29 #define RTQ6056_REG_BUSVOLT 0x02
30 #define RTQ6056_REG_POWER 0x03
31 #define RTQ6056_REG_CURRENT 0x04
32 #define RTQ6056_REG_CALIBRATION 0x05
33 #define RTQ6056_REG_MASKENABLE 0x06
34 #define RTQ6056_REG_ALERTLIMIT 0x07
35 #define RTQ6056_REG_MANUFACTID 0xFE
36 #define RTQ6056_REG_DIEID 0xFF
38 #define RTQ6056_VENDOR_ID 0x1214
39 #define RTQ6056_DEFAULT_CONFIG 0x4127
40 #define RTQ6056_CONT_ALLON 7
42 #define RTQ6059_DEFAULT_CONFIG 0x3C47
43 #define RTQ6059_VBUS_LSB_OFFSET 3
44 #define RTQ6059_AVG_BASE 8
47 RTQ6056_CH_VSHUNT
= 0,
55 * The enum is to present the 0x00 CONFIG RG bitfield for the 16bit RG value
56 * field value order from LSB to MSB
57 * RTQ6053/6 is OPMODE->VSHUNTCT->VBUSCT->AVG->RESET
58 * RTQ6059 is OPMODE->SADC->BADC->PGA->RESET
63 F_RTQ6059_SADC
= F_VSHUNTCT
,
65 F_RTQ6059_BADC
= F_VBUSCT
,
67 F_RTQ6059_PGA
= F_AVG
,
74 struct richtek_dev_data
{
77 int default_conv_time_us
;
78 unsigned int default_config
;
79 unsigned int calib_coefficient
;
80 const int *avg_sample_list
;
81 int avg_sample_list_length
;
82 const struct reg_field
*reg_fields
;
83 const struct iio_chan_spec
*channels
;
85 int (*read_scale
)(struct iio_chan_spec
const *ch
, int *val
, int *val2
);
86 int (*set_average
)(struct rtq6056_priv
*priv
, int val
);
91 struct regmap
*regmap
;
92 struct regmap_field
*rm_fields
[F_MAX_FIELDS
];
93 const struct richtek_dev_data
*devdata
;
94 u32 shunt_resistor_uohm
;
100 static const struct reg_field rtq6056_reg_fields
[F_MAX_FIELDS
] = {
101 [F_OPMODE
] = REG_FIELD(RTQ6056_REG_CONFIG
, 0, 2),
102 [F_VSHUNTCT
] = REG_FIELD(RTQ6056_REG_CONFIG
, 3, 5),
103 [F_VBUSCT
] = REG_FIELD(RTQ6056_REG_CONFIG
, 6, 8),
104 [F_AVG
] = REG_FIELD(RTQ6056_REG_CONFIG
, 9, 11),
105 [F_RESET
] = REG_FIELD(RTQ6056_REG_CONFIG
, 15, 15),
108 static const struct reg_field rtq6059_reg_fields
[F_MAX_FIELDS
] = {
109 [F_OPMODE
] = REG_FIELD(RTQ6056_REG_CONFIG
, 0, 2),
110 [F_RTQ6059_SADC
] = REG_FIELD(RTQ6056_REG_CONFIG
, 3, 6),
111 [F_RTQ6059_BADC
] = REG_FIELD(RTQ6056_REG_CONFIG
, 7, 10),
112 [F_RTQ6059_PGA
] = REG_FIELD(RTQ6056_REG_CONFIG
, 11, 12),
113 [F_RESET
] = REG_FIELD(RTQ6056_REG_CONFIG
, 15, 15),
116 static const struct iio_chan_spec rtq6056_channels
[RTQ6056_MAX_CHANNEL
+ 1] = {
121 .address
= RTQ6056_REG_SHUNTVOLT
,
122 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
123 BIT(IIO_CHAN_INFO_SCALE
) |
124 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
125 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
126 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
127 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
133 .endianness
= IIO_CPU
,
140 .address
= RTQ6056_REG_BUSVOLT
,
141 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
142 BIT(IIO_CHAN_INFO_SCALE
) |
143 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
144 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
145 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
146 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
152 .endianness
= IIO_CPU
,
159 .address
= RTQ6056_REG_POWER
,
160 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
161 BIT(IIO_CHAN_INFO_SCALE
) |
162 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
163 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
164 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
170 .endianness
= IIO_CPU
,
177 .address
= RTQ6056_REG_CURRENT
,
178 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
179 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
180 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
181 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
187 .endianness
= IIO_CPU
,
190 IIO_CHAN_SOFT_TIMESTAMP(RTQ6056_MAX_CHANNEL
),
194 * Difference between RTQ6056 and RTQ6059
195 * - Fixed sampling conversion time
196 * - Average sample numbers
198 * - calibration coefficient
200 static const struct iio_chan_spec rtq6059_channels
[RTQ6056_MAX_CHANNEL
+ 1] = {
205 .address
= RTQ6056_REG_SHUNTVOLT
,
206 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
207 BIT(IIO_CHAN_INFO_SCALE
) |
208 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
209 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
210 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
216 .endianness
= IIO_CPU
,
223 .address
= RTQ6056_REG_BUSVOLT
,
224 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
225 BIT(IIO_CHAN_INFO_SCALE
) |
226 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
227 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
228 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
234 .endianness
= IIO_CPU
,
241 .address
= RTQ6056_REG_POWER
,
242 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
243 BIT(IIO_CHAN_INFO_SCALE
) |
244 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
245 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
246 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
252 .endianness
= IIO_CPU
,
259 .address
= RTQ6056_REG_CURRENT
,
260 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
261 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
262 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
263 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
269 .endianness
= IIO_CPU
,
272 IIO_CHAN_SOFT_TIMESTAMP(RTQ6056_MAX_CHANNEL
),
275 static int rtq6056_adc_read_channel(struct rtq6056_priv
*priv
,
276 struct iio_chan_spec
const *ch
,
279 const struct richtek_dev_data
*devdata
= priv
->devdata
;
280 struct device
*dev
= priv
->dev
;
281 unsigned int addr
= ch
->address
;
285 pm_runtime_get_sync(dev
);
286 ret
= regmap_read(priv
->regmap
, addr
, ®val
);
287 pm_runtime_mark_last_busy(dev
);
292 /* Power and VBUS is unsigned 16-bit, others are signed 16-bit */
294 case RTQ6056_REG_BUSVOLT
:
295 regval
>>= devdata
->vbus_offset
;
298 case RTQ6056_REG_POWER
:
301 case RTQ6056_REG_SHUNTVOLT
:
302 case RTQ6056_REG_CURRENT
:
303 *val
= sign_extend32(regval
, 16);
310 static int rtq6056_adc_read_scale(struct iio_chan_spec
const *ch
, int *val
,
313 switch (ch
->address
) {
314 case RTQ6056_REG_SHUNTVOLT
:
315 /* VSHUNT lsb 2.5uV */
318 return IIO_VAL_FRACTIONAL
;
319 case RTQ6056_REG_BUSVOLT
:
320 /* VBUS lsb 1.25mV */
323 return IIO_VAL_FRACTIONAL
;
324 case RTQ6056_REG_POWER
:
333 static int rtq6059_adc_read_scale(struct iio_chan_spec
const *ch
, int *val
,
336 switch (ch
->address
) {
337 case RTQ6056_REG_SHUNTVOLT
:
338 /* VSHUNT lsb 10uV */
341 return IIO_VAL_FRACTIONAL
;
342 case RTQ6056_REG_BUSVOLT
:
346 case RTQ6056_REG_POWER
:
356 * Sample frequency for channel VSHUNT and VBUS. The indices correspond
357 * with the bit value expected by the chip. And it can be found at
358 * https://www.richtek.com/assets/product_file/RTQ6056/DSQ6056-00.pdf
360 static const int rtq6056_samp_freq_list
[] = {
361 7194, 4926, 3717, 1904, 964, 485, 243, 122,
364 static int rtq6056_adc_set_samp_freq(struct rtq6056_priv
*priv
,
365 struct iio_chan_spec
const *ch
, int val
)
367 struct regmap_field
*rm_field
;
368 unsigned int selector
;
371 if (val
> 7194 || val
< 122)
374 if (ch
->address
== RTQ6056_REG_SHUNTVOLT
) {
375 rm_field
= priv
->rm_fields
[F_VSHUNTCT
];
376 ct
= &priv
->vshuntct_us
;
377 } else if (ch
->address
== RTQ6056_REG_BUSVOLT
) {
378 rm_field
= priv
->rm_fields
[F_VBUSCT
];
379 ct
= &priv
->vbusct_us
;
383 selector
= find_closest_descending(val
, rtq6056_samp_freq_list
,
384 ARRAY_SIZE(rtq6056_samp_freq_list
));
386 ret
= regmap_field_write(rm_field
, selector
);
390 *ct
= 1000000 / rtq6056_samp_freq_list
[selector
];
396 * Available averaging rate for rtq6056. The indices correspond with the bit
397 * value expected by the chip. And it can be found at
398 * https://www.richtek.com/assets/product_file/RTQ6056/DSQ6056-00.pdf
400 static const int rtq6056_avg_sample_list
[] = {
401 1, 4, 16, 64, 128, 256, 512, 1024,
404 static const int rtq6059_avg_sample_list
[] = {
405 1, 2, 4, 8, 16, 32, 64, 128,
408 static int rtq6056_adc_set_average(struct rtq6056_priv
*priv
, int val
)
410 unsigned int selector
;
413 if (val
> 1024 || val
< 1)
416 selector
= find_closest(val
, rtq6056_avg_sample_list
,
417 ARRAY_SIZE(rtq6056_avg_sample_list
));
419 ret
= regmap_field_write(priv
->rm_fields
[F_AVG
], selector
);
423 priv
->avg_sample
= rtq6056_avg_sample_list
[selector
];
428 static int rtq6059_adc_set_average(struct rtq6056_priv
*priv
, int val
)
430 unsigned int selector
;
433 if (val
> 128 || val
< 1)
436 /* The supported average sample is 2^x (x from 0 to 7) */
437 selector
= fls(val
) - 1;
439 ret
= regmap_field_write(priv
->rm_fields
[F_RTQ6059_BADC
],
440 RTQ6059_AVG_BASE
+ selector
);
444 ret
= regmap_field_write(priv
->rm_fields
[F_RTQ6059_SADC
],
445 RTQ6059_AVG_BASE
+ selector
);
447 priv
->avg_sample
= BIT(selector
);
452 static int rtq6056_adc_get_sample_freq(struct rtq6056_priv
*priv
,
453 struct iio_chan_spec
const *ch
, int *val
)
457 if (ch
->address
== RTQ6056_REG_SHUNTVOLT
)
458 sample_time
= priv
->vshuntct_us
;
459 else if (ch
->address
== RTQ6056_REG_BUSVOLT
)
460 sample_time
= priv
->vbusct_us
;
462 sample_time
= priv
->vshuntct_us
+ priv
->vbusct_us
;
463 sample_time
*= priv
->avg_sample
;
466 *val
= 1000000 / sample_time
;
471 static int rtq6056_adc_read_raw(struct iio_dev
*indio_dev
,
472 struct iio_chan_spec
const *chan
, int *val
,
473 int *val2
, long mask
)
475 struct rtq6056_priv
*priv
= iio_priv(indio_dev
);
476 const struct richtek_dev_data
*devdata
= priv
->devdata
;
479 case IIO_CHAN_INFO_RAW
:
480 return rtq6056_adc_read_channel(priv
, chan
, val
);
481 case IIO_CHAN_INFO_SCALE
:
482 return devdata
->read_scale(chan
, val
, val2
);
483 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
484 *val
= priv
->avg_sample
;
486 case IIO_CHAN_INFO_SAMP_FREQ
:
487 return rtq6056_adc_get_sample_freq(priv
, chan
, val
);
493 static int rtq6056_adc_read_avail(struct iio_dev
*indio_dev
,
494 struct iio_chan_spec
const *chan
,
495 const int **vals
, int *type
, int *length
,
498 struct rtq6056_priv
*priv
= iio_priv(indio_dev
);
499 const struct richtek_dev_data
*devdata
= priv
->devdata
;
502 case IIO_CHAN_INFO_SAMP_FREQ
:
503 *vals
= rtq6056_samp_freq_list
;
505 *length
= ARRAY_SIZE(rtq6056_samp_freq_list
);
506 return IIO_AVAIL_LIST
;
507 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
508 *vals
= devdata
->avg_sample_list
;
509 *length
= devdata
->avg_sample_list_length
;
511 return IIO_AVAIL_LIST
;
517 static int rtq6056_adc_write_raw(struct iio_dev
*indio_dev
,
518 struct iio_chan_spec
const *chan
, int val
,
521 struct rtq6056_priv
*priv
= iio_priv(indio_dev
);
522 const struct richtek_dev_data
*devdata
= priv
->devdata
;
524 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
) {
526 case IIO_CHAN_INFO_SAMP_FREQ
:
527 if (devdata
->fixed_samp_freq
)
529 return rtq6056_adc_set_samp_freq(priv
, chan
, val
);
530 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
531 return devdata
->set_average(priv
, val
);
539 static const char *rtq6056_channel_labels
[RTQ6056_MAX_CHANNEL
] = {
540 [RTQ6056_CH_VSHUNT
] = "Vshunt",
541 [RTQ6056_CH_VBUS
] = "Vbus",
542 [RTQ6056_CH_POWER
] = "Power",
543 [RTQ6056_CH_CURRENT
] = "Current",
546 static int rtq6056_adc_read_label(struct iio_dev
*indio_dev
,
547 struct iio_chan_spec
const *chan
,
550 return sysfs_emit(label
, "%s\n", rtq6056_channel_labels
[chan
->channel
]);
553 static int rtq6056_set_shunt_resistor(struct rtq6056_priv
*priv
,
556 const struct richtek_dev_data
*devdata
= priv
->devdata
;
557 unsigned int calib_val
;
560 if (resistor_uohm
<= 0) {
561 dev_err(priv
->dev
, "Invalid resistor [%d]\n", resistor_uohm
);
565 /* calibration = coefficient / (Rshunt (uOhm) * current lsb (1mA)) */
566 calib_val
= devdata
->calib_coefficient
/ resistor_uohm
;
567 ret
= regmap_write(priv
->regmap
, RTQ6056_REG_CALIBRATION
, calib_val
);
571 priv
->shunt_resistor_uohm
= resistor_uohm
;
576 static ssize_t
shunt_resistor_show(struct device
*dev
,
577 struct device_attribute
*attr
, char *buf
)
579 struct rtq6056_priv
*priv
= iio_priv(dev_to_iio_dev(dev
));
580 int vals
[2] = { priv
->shunt_resistor_uohm
, 1000000 };
582 return iio_format_value(buf
, IIO_VAL_FRACTIONAL
, 1, vals
);
585 static ssize_t
shunt_resistor_store(struct device
*dev
,
586 struct device_attribute
*attr
,
587 const char *buf
, size_t len
)
589 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
590 struct rtq6056_priv
*priv
= iio_priv(indio_dev
);
591 int val
, val_fract
, ret
;
593 ret
= iio_device_claim_direct_mode(indio_dev
);
597 ret
= iio_str_to_fixpoint(buf
, 100000, &val
, &val_fract
);
601 ret
= rtq6056_set_shunt_resistor(priv
, val
* 1000000 + val_fract
);
604 iio_device_release_direct_mode(indio_dev
);
609 static IIO_DEVICE_ATTR_RW(shunt_resistor
, 0);
611 static struct attribute
*rtq6056_attributes
[] = {
612 &iio_dev_attr_shunt_resistor
.dev_attr
.attr
,
616 static const struct attribute_group rtq6056_attribute_group
= {
617 .attrs
= rtq6056_attributes
,
620 static const struct iio_info rtq6056_info
= {
621 .attrs
= &rtq6056_attribute_group
,
622 .read_raw
= rtq6056_adc_read_raw
,
623 .read_avail
= rtq6056_adc_read_avail
,
624 .write_raw
= rtq6056_adc_write_raw
,
625 .read_label
= rtq6056_adc_read_label
,
628 static irqreturn_t
rtq6056_buffer_trigger_handler(int irq
, void *p
)
630 struct iio_poll_func
*pf
= p
;
631 struct iio_dev
*indio_dev
= pf
->indio_dev
;
632 struct rtq6056_priv
*priv
= iio_priv(indio_dev
);
633 const struct richtek_dev_data
*devdata
= priv
->devdata
;
634 struct device
*dev
= priv
->dev
;
636 u16 vals
[RTQ6056_MAX_CHANNEL
];
637 s64 timestamp
__aligned(8);
642 memset(&data
, 0, sizeof(data
));
644 pm_runtime_get_sync(dev
);
646 iio_for_each_active_channel(indio_dev
, bit
) {
647 unsigned int addr
= rtq6056_channels
[bit
].address
;
649 ret
= regmap_read(priv
->regmap
, addr
, &raw
);
653 if (addr
== RTQ6056_REG_BUSVOLT
)
654 raw
>>= devdata
->vbus_offset
;
656 data
.vals
[i
++] = raw
;
659 iio_push_to_buffers_with_timestamp(indio_dev
, &data
, iio_get_time_ns(indio_dev
));
662 pm_runtime_mark_last_busy(dev
);
665 iio_trigger_notify_done(indio_dev
->trig
);
670 static void rtq6056_enter_shutdown_state(void *dev
)
672 struct rtq6056_priv
*priv
= dev_get_drvdata(dev
);
674 /* Enter shutdown state */
675 regmap_field_write(priv
->rm_fields
[F_OPMODE
], 0);
678 static bool rtq6056_is_readable_reg(struct device
*dev
, unsigned int reg
)
681 case RTQ6056_REG_CONFIG
... RTQ6056_REG_ALERTLIMIT
:
682 case RTQ6056_REG_MANUFACTID
... RTQ6056_REG_DIEID
:
689 static bool rtq6056_is_writeable_reg(struct device
*dev
, unsigned int reg
)
692 case RTQ6056_REG_CONFIG
:
693 case RTQ6056_REG_CALIBRATION
... RTQ6056_REG_ALERTLIMIT
:
700 static const struct regmap_config rtq6056_regmap_config
= {
703 .val_format_endian
= REGMAP_ENDIAN_BIG
,
704 .max_register
= RTQ6056_REG_DIEID
,
705 .readable_reg
= rtq6056_is_readable_reg
,
706 .writeable_reg
= rtq6056_is_writeable_reg
,
709 static int rtq6056_probe(struct i2c_client
*i2c
)
711 struct iio_dev
*indio_dev
;
712 struct rtq6056_priv
*priv
;
713 struct device
*dev
= &i2c
->dev
;
714 struct regmap
*regmap
;
715 const struct richtek_dev_data
*devdata
;
716 unsigned int vendor_id
, shunt_resistor_uohm
;
719 if (!i2c_check_functionality(i2c
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
722 devdata
= device_get_match_data(dev
);
724 return dev_err_probe(dev
, -EINVAL
, "Invalid dev data\n");
726 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
730 priv
= iio_priv(indio_dev
);
732 priv
->vshuntct_us
= priv
->vbusct_us
= devdata
->default_conv_time_us
;
733 priv
->avg_sample
= 1;
734 priv
->devdata
= devdata
;
735 i2c_set_clientdata(i2c
, priv
);
737 regmap
= devm_regmap_init_i2c(i2c
, &rtq6056_regmap_config
);
739 return dev_err_probe(dev
, PTR_ERR(regmap
),
740 "Failed to init regmap\n");
742 priv
->regmap
= regmap
;
744 ret
= regmap_read(regmap
, RTQ6056_REG_MANUFACTID
, &vendor_id
);
746 return dev_err_probe(dev
, ret
,
747 "Failed to get manufacturer info\n");
749 if (vendor_id
!= RTQ6056_VENDOR_ID
)
750 return dev_err_probe(dev
, -ENODEV
,
751 "Invalid vendor id 0x%04x\n", vendor_id
);
753 ret
= devm_regmap_field_bulk_alloc(dev
, regmap
, priv
->rm_fields
,
754 devdata
->reg_fields
, F_MAX_FIELDS
);
756 return dev_err_probe(dev
, ret
, "Failed to init regmap field\n");
758 ret
= regmap_write(regmap
, RTQ6056_REG_CONFIG
, devdata
->default_config
);
760 return dev_err_probe(dev
, ret
,
761 "Failed to enable continuous sensing\n");
763 ret
= devm_add_action_or_reset(dev
, rtq6056_enter_shutdown_state
, dev
);
767 pm_runtime_set_autosuspend_delay(dev
, MSEC_PER_SEC
);
768 pm_runtime_use_autosuspend(dev
);
769 pm_runtime_set_active(dev
);
770 pm_runtime_mark_last_busy(dev
);
771 ret
= devm_pm_runtime_enable(dev
);
773 return dev_err_probe(dev
, ret
, "Failed to enable pm_runtime\n");
775 /* By default, use 2000 micro-Ohm resistor */
776 shunt_resistor_uohm
= 2000;
777 device_property_read_u32(dev
, "shunt-resistor-micro-ohms",
778 &shunt_resistor_uohm
);
780 ret
= rtq6056_set_shunt_resistor(priv
, shunt_resistor_uohm
);
782 return dev_err_probe(dev
, ret
,
783 "Failed to init shunt resistor\n");
785 indio_dev
->name
= "rtq6056";
786 indio_dev
->modes
= INDIO_DIRECT_MODE
;
787 indio_dev
->channels
= devdata
->channels
;
788 indio_dev
->num_channels
= devdata
->num_channels
;
789 indio_dev
->info
= &rtq6056_info
;
791 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
,
792 rtq6056_buffer_trigger_handler
,
795 return dev_err_probe(dev
, ret
,
796 "Failed to allocate iio trigger buffer\n");
798 return devm_iio_device_register(dev
, indio_dev
);
801 static int rtq6056_runtime_suspend(struct device
*dev
)
803 struct rtq6056_priv
*priv
= dev_get_drvdata(dev
);
805 /* Configure to shutdown mode */
806 return regmap_field_write(priv
->rm_fields
[F_OPMODE
], 0);
809 static int rtq6056_runtime_resume(struct device
*dev
)
811 struct rtq6056_priv
*priv
= dev_get_drvdata(dev
);
812 int sample_rdy_time_us
, ret
;
814 ret
= regmap_field_write(priv
->rm_fields
[F_OPMODE
], RTQ6056_CONT_ALLON
);
818 sample_rdy_time_us
= priv
->vbusct_us
+ priv
->vshuntct_us
;
819 sample_rdy_time_us
*= priv
->avg_sample
;
821 usleep_range(sample_rdy_time_us
, sample_rdy_time_us
+ 100);
826 static DEFINE_RUNTIME_DEV_PM_OPS(rtq6056_pm_ops
, rtq6056_runtime_suspend
,
827 rtq6056_runtime_resume
, NULL
);
829 static const struct richtek_dev_data rtq6056_devdata
= {
830 .default_conv_time_us
= 1037,
831 .calib_coefficient
= 5120000,
833 * By default, configure average sample as 1, bus and shunt conversion
834 * time as 1037 microsecond, and operating mode to all on.
836 .default_config
= RTQ6056_DEFAULT_CONFIG
,
837 .avg_sample_list
= rtq6056_avg_sample_list
,
838 .avg_sample_list_length
= ARRAY_SIZE(rtq6056_avg_sample_list
),
839 .reg_fields
= rtq6056_reg_fields
,
840 .channels
= rtq6056_channels
,
841 .num_channels
= ARRAY_SIZE(rtq6056_channels
),
842 .read_scale
= rtq6056_adc_read_scale
,
843 .set_average
= rtq6056_adc_set_average
,
846 static const struct richtek_dev_data rtq6059_devdata
= {
847 .fixed_samp_freq
= true,
848 .vbus_offset
= RTQ6059_VBUS_LSB_OFFSET
,
849 .default_conv_time_us
= 532,
850 .calib_coefficient
= 40960000,
852 * By default, configure average sample as 1, bus and shunt conversion
853 * time as 532 microsecond, and operating mode to all on.
855 .default_config
= RTQ6059_DEFAULT_CONFIG
,
856 .avg_sample_list
= rtq6059_avg_sample_list
,
857 .avg_sample_list_length
= ARRAY_SIZE(rtq6059_avg_sample_list
),
858 .reg_fields
= rtq6059_reg_fields
,
859 .channels
= rtq6059_channels
,
860 .num_channels
= ARRAY_SIZE(rtq6059_channels
),
861 .read_scale
= rtq6059_adc_read_scale
,
862 .set_average
= rtq6059_adc_set_average
,
865 static const struct of_device_id rtq6056_device_match
[] = {
866 { .compatible
= "richtek,rtq6056", .data
= &rtq6056_devdata
},
867 { .compatible
= "richtek,rtq6059", .data
= &rtq6059_devdata
},
870 MODULE_DEVICE_TABLE(of
, rtq6056_device_match
);
872 static struct i2c_driver rtq6056_driver
= {
875 .of_match_table
= rtq6056_device_match
,
876 .pm
= pm_ptr(&rtq6056_pm_ops
),
878 .probe
= rtq6056_probe
,
880 module_i2c_driver(rtq6056_driver
);
882 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
883 MODULE_DESCRIPTION("Richtek RTQ6056 Driver");
884 MODULE_LICENSE("GPL v2");