aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/adc/ina2xx-adc.c52
1 files changed, 46 insertions, 6 deletions
diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
index ff31f5b49a41..dd4ff08d7b62 100644
--- a/drivers/iio/adc/ina2xx-adc.c
+++ b/drivers/iio/adc/ina2xx-adc.c
@@ -111,7 +111,7 @@ struct ina2xx_chip_info {
111 struct task_struct *task; 111 struct task_struct *task;
112 const struct ina2xx_config *config; 112 const struct ina2xx_config *config;
113 struct mutex state_lock; 113 struct mutex state_lock;
114 long rshunt; 114 unsigned int shunt_resistor;
115 int avg; 115 int avg;
116 s64 prev_ns; /* track buffer capture time, check for underruns*/ 116 s64 prev_ns; /* track buffer capture time, check for underruns*/
117 int int_time_vbus; /* Bus voltage integration time uS */ 117 int int_time_vbus; /* Bus voltage integration time uS */
@@ -349,6 +349,42 @@ static ssize_t ina2xx_allow_async_readout_store(struct device *dev,
349 return len; 349 return len;
350} 350}
351 351
352static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val)
353{
354 if (val <= 0 || val > chip->config->calibration_factor)
355 return -EINVAL;
356
357 chip->shunt_resistor = val;
358 return 0;
359}
360
361static ssize_t ina2xx_shunt_resistor_show(struct device *dev,
362 struct device_attribute *attr,
363 char *buf)
364{
365 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
366
367 return sprintf(buf, "%d\n", chip->shunt_resistor);
368}
369
370static ssize_t ina2xx_shunt_resistor_store(struct device *dev,
371 struct device_attribute *attr,
372 const char *buf, size_t len)
373{
374 struct ina2xx_chip_info *chip = iio_priv(dev_to_iio_dev(dev));
375 unsigned long val;
376 int ret;
377
378 ret = kstrtoul((const char *) buf, 10, &val);
379 if (ret)
380 return ret;
381
382 ret = set_shunt_resistor(chip, val);
383 if (ret)
384 return ret;
385
386 return len;
387}
352 388
353#define INA2XX_CHAN(_type, _index, _address) { \ 389#define INA2XX_CHAN(_type, _index, _address) { \
354 .type = (_type), \ 390 .type = (_type), \
@@ -545,9 +581,14 @@ static IIO_DEVICE_ATTR(in_allow_async_readout, S_IRUGO | S_IWUSR,
545 ina2xx_allow_async_readout_show, 581 ina2xx_allow_async_readout_show,
546 ina2xx_allow_async_readout_store, 0); 582 ina2xx_allow_async_readout_store, 0);
547 583
584static IIO_DEVICE_ATTR(in_shunt_resistor, S_IRUGO | S_IWUSR,
585 ina2xx_shunt_resistor_show,
586 ina2xx_shunt_resistor_store, 0);
587
548static struct attribute *ina2xx_attributes[] = { 588static struct attribute *ina2xx_attributes[] = {
549 &iio_dev_attr_in_allow_async_readout.dev_attr.attr, 589 &iio_dev_attr_in_allow_async_readout.dev_attr.attr,
550 &iio_const_attr_integration_time_available.dev_attr.attr, 590 &iio_const_attr_integration_time_available.dev_attr.attr,
591 &iio_dev_attr_in_shunt_resistor.dev_attr.attr,
551 NULL, 592 NULL,
552}; 593};
553 594
@@ -579,7 +620,7 @@ static int ina2xx_init(struct ina2xx_chip_info *chip, unsigned int config)
579 * to the user for now. 620 * to the user for now.
580 */ 621 */
581 regval = DIV_ROUND_CLOSEST(chip->config->calibration_factor, 622 regval = DIV_ROUND_CLOSEST(chip->config->calibration_factor,
582 chip->rshunt); 623 chip->shunt_resistor);
583 624
584 return regmap_write(chip->regmap, INA2XX_CALIBRATION, regval); 625 return regmap_write(chip->regmap, INA2XX_CALIBRATION, regval);
585} 626}
@@ -612,10 +653,9 @@ static int ina2xx_probe(struct i2c_client *client,
612 val = INA2XX_RSHUNT_DEFAULT; 653 val = INA2XX_RSHUNT_DEFAULT;
613 } 654 }
614 655
615 if (val <= 0 || val > chip->config->calibration_factor) 656 ret = set_shunt_resistor(chip, val);
616 return -ENODEV; 657 if (ret)
617 658 return ret;
618 chip->rshunt = val;
619 659
620 mutex_init(&chip->state_lock); 660 mutex_init(&chip->state_lock);
621 661