Lines Matching +full:avg +full:- +full:samples

11  * Copyright 2010-2011 Analog Devices Inc.
14 * Copyright 2012 Lothar Felten <l-felten@ti.com>
16 * Licensed under the GPL-2 or later.
18 * IIO driver for INA219-220-226-230-231
20 * Configurable 7-bit I2C slave address from 0x40 to 0x4F
51 /* settings - depend on use case */
98 #define SAMPLING_PERIOD(c) ((c->int_time_vbus + c->int_time_vshunt) \
99 * c->avg)
144 int avg; member
190 ret = regmap_read(chip->regmap, chan->address, &regval); in ina2xx_read_raw()
194 if (is_signed_reg(chan->address)) in ina2xx_read_raw()
199 if (chan->address == INA2XX_BUS_VOLTAGE) in ina2xx_read_raw()
200 *val >>= chip->config->bus_voltage_shift; in ina2xx_read_raw()
205 *val = chip->avg; in ina2xx_read_raw()
210 if (chan->address == INA2XX_SHUNT_VOLTAGE) in ina2xx_read_raw()
211 *val2 = chip->int_time_vshunt; in ina2xx_read_raw()
213 *val2 = chip->int_time_vbus; in ina2xx_read_raw()
220 * 1/AVG*(CT_bus+CT_shunt). in ina2xx_read_raw()
227 switch (chan->address) { in ina2xx_read_raw()
230 *val = chip->config->shunt_voltage_lsb; in ina2xx_read_raw()
236 *val = chip->config->bus_voltage_lsb; in ina2xx_read_raw()
246 *val = chip->config->shunt_voltage_lsb; in ina2xx_read_raw()
247 *val2 = chip->shunt_resistor_uohm; in ina2xx_read_raw()
256 *val = chip->config->power_lsb_factor * in ina2xx_read_raw()
257 chip->config->shunt_voltage_lsb; in ina2xx_read_raw()
258 *val2 = chip->shunt_resistor_uohm; in ina2xx_read_raw()
261 return -EINVAL; in ina2xx_read_raw()
264 switch (chan->address) { in ina2xx_read_raw()
266 *val = chip->pga_gain_vshunt; in ina2xx_read_raw()
271 *val = chip->range_vbus == 32 ? 1 : 2; in ina2xx_read_raw()
274 return -EINVAL; in ina2xx_read_raw()
277 return -EINVAL; in ina2xx_read_raw()
283 * table 3 AVG bit settings, found at
294 return -EINVAL; in ina226_set_average()
299 chip->avg = ina226_avg_tab[bits]; in ina226_set_average()
317 return -EINVAL; in ina226_set_int_time_vbus()
322 chip->int_time_vbus = ina226_conv_time_tab[bits]; in ina226_set_int_time_vbus()
336 return -EINVAL; in ina226_set_int_time_vshunt()
341 chip->int_time_vshunt = ina226_conv_time_tab[bits]; in ina226_set_int_time_vshunt()
357 return -EINVAL; in ina219_lookup_int_time()
383 chip->int_time_vbus = val_us_best; in ina219_set_int_time_vbus()
401 chip->int_time_vshunt = val_us_best; in ina219_set_int_time_vshunt()
415 chip->range_vbus = 32; in ina219_set_vbus_range_denom()
417 chip->range_vbus = 16; in ina219_set_vbus_range_denom()
419 return -EINVAL; in ina219_set_vbus_range_denom()
438 return -EINVAL; in ina219_set_vshunt_pga_gain()
443 chip->pga_gain_vshunt = ina219_vshunt_gain_tab[bits]; in ina219_set_vshunt_pga_gain()
444 bits = 3 - bits; in ina219_set_vshunt_pga_gain()
459 switch (chan->address) { in ina2xx_read_avail()
474 return -EINVAL; in ina2xx_read_avail()
486 return -EBUSY; in ina2xx_write_raw()
488 mutex_lock(&chip->state_lock); in ina2xx_write_raw()
490 ret = regmap_read(chip->regmap, INA2XX_CONFIG, &config); in ina2xx_write_raw()
502 if (chip->config->chip_id == ina226) { in ina2xx_write_raw()
503 if (chan->address == INA2XX_SHUNT_VOLTAGE) in ina2xx_write_raw()
510 if (chan->address == INA2XX_SHUNT_VOLTAGE) in ina2xx_write_raw()
520 if (chan->address == INA2XX_SHUNT_VOLTAGE) in ina2xx_write_raw()
528 ret = -EINVAL; in ina2xx_write_raw()
532 ret = regmap_write(chip->regmap, INA2XX_CONFIG, tmp); in ina2xx_write_raw()
534 mutex_unlock(&chip->state_lock); in ina2xx_write_raw()
545 return sysfs_emit(buf, "%d\n", chip->allow_async_readout); in ina2xx_allow_async_readout_show()
560 chip->allow_async_readout = val; in ina2xx_allow_async_readout_store()
574 return regmap_write(chip->regmap, INA2XX_CALIBRATION, in ina2xx_set_calibration()
575 chip->config->calibration_value); in ina2xx_set_calibration()
581 return -EINVAL; in set_shunt_resistor()
583 chip->shunt_resistor_uohm = val; in set_shunt_resistor()
593 int vals[2] = { chip->shunt_resistor_uohm, 1000000 }; in ina2xx_shunt_resistor_show()
671 .realbits = 16 - _shift, \
722 * result in reading V[k-1] again, or skip V[k] at time Tk. in ina2xx_conversion_ready()
730 if (chip->config->chip_id == ina226) { in ina2xx_conversion_ready()
731 ret = regmap_read(chip->regmap, in ina2xx_conversion_ready()
735 ret = regmap_read(chip->regmap, in ina2xx_conversion_ready()
756 * as there is no auto-increment of the register pointer. in ina2xx_work_buffer()
758 for_each_set_bit(bit, indio_dev->active_scan_mask, in ina2xx_work_buffer()
759 indio_dev->masklength) { in ina2xx_work_buffer()
762 ret = regmap_read(chip->regmap, in ina2xx_work_buffer()
767 chip->scan.chan[i++] = val; in ina2xx_work_buffer()
770 iio_push_to_buffers_with_timestamp(indio_dev, &chip->scan, time); in ina2xx_work_buffer()
788 if (!chip->allow_async_readout) in ina2xx_capture_thread()
789 sampling_us -= 200; in ina2xx_capture_thread()
794 while (!chip->allow_async_readout) { in ina2xx_capture_thread()
817 * interval, and sleep for the remainder (next - now) in ina2xx_capture_thread()
819 * multiple times, i.e. samples are dropped. in ina2xx_capture_thread()
840 dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n", in ina2xx_buffer_enable()
841 (unsigned int)(*indio_dev->active_scan_mask), in ina2xx_buffer_enable()
842 1000000 / sampling_us, chip->avg); in ina2xx_buffer_enable()
844 dev_dbg(&indio_dev->dev, "Expected work period: %u us\n", sampling_us); in ina2xx_buffer_enable()
845 dev_dbg(&indio_dev->dev, "Async readout mode: %d\n", in ina2xx_buffer_enable()
846 chip->allow_async_readout); in ina2xx_buffer_enable()
849 "%s:%d-%uus", indio_dev->name, in ina2xx_buffer_enable()
855 chip->task = task; in ina2xx_buffer_enable()
864 if (chip->task) { in ina2xx_buffer_disable()
865 kthread_stop(chip->task); in ina2xx_buffer_disable()
866 chip->task = NULL; in ina2xx_buffer_disable()
883 return regmap_write(chip->regmap, reg, writeval); in ina2xx_debug_reg()
885 return regmap_read(chip->regmap, reg, readval); in ina2xx_debug_reg()
945 int ret = regmap_write(chip->regmap, INA2XX_CONFIG, config); in ina2xx_init()
961 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); in ina2xx_probe()
963 return -ENOMEM; in ina2xx_probe()
970 chip->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config); in ina2xx_probe()
971 if (IS_ERR(chip->regmap)) { in ina2xx_probe()
972 dev_err(&client->dev, "failed to allocate register map\n"); in ina2xx_probe()
973 return PTR_ERR(chip->regmap); in ina2xx_probe()
976 if (client->dev.of_node) in ina2xx_probe()
977 type = (uintptr_t)of_device_get_match_data(&client->dev); in ina2xx_probe()
979 type = id->driver_data; in ina2xx_probe()
980 chip->config = &ina2xx_config[type]; in ina2xx_probe()
982 mutex_init(&chip->state_lock); in ina2xx_probe()
984 if (of_property_read_u32(client->dev.of_node, in ina2xx_probe()
985 "shunt-resistor", &val) < 0) { in ina2xx_probe()
987 dev_get_platdata(&client->dev); in ina2xx_probe()
990 val = pdata->shunt_uohms; in ina2xx_probe()
1000 val = chip->config->config_default; in ina2xx_probe()
1007 chip->avg = 1; in ina2xx_probe()
1016 dev_err(&client->dev, "error configuring the device\n"); in ina2xx_probe()
1020 indio_dev->modes = INDIO_DIRECT_MODE; in ina2xx_probe()
1022 indio_dev->channels = ina226_channels; in ina2xx_probe()
1023 indio_dev->num_channels = ARRAY_SIZE(ina226_channels); in ina2xx_probe()
1024 indio_dev->info = &ina226_info; in ina2xx_probe()
1026 indio_dev->channels = ina219_channels; in ina2xx_probe()
1027 indio_dev->num_channels = ARRAY_SIZE(ina219_channels); in ina2xx_probe()
1028 indio_dev->info = &ina219_info; in ina2xx_probe()
1030 indio_dev->name = id ? id->name : chip->config->name; in ina2xx_probe()
1032 ret = devm_iio_kfifo_buffer_setup(&client->dev, indio_dev, in ina2xx_probe()
1049 ret = regmap_update_bits(chip->regmap, INA2XX_CONFIG, in ina2xx_remove()
1052 dev_warn(&client->dev, "Failed to power down device (%pe)\n", in ina2xx_remove()