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)
143 int avg; member
187 ret = regmap_read(chip->regmap, chan->address, &regval); in ina2xx_read_raw()
191 if (is_signed_reg(chan->address)) in ina2xx_read_raw()
196 if (chan->address == INA2XX_BUS_VOLTAGE) in ina2xx_read_raw()
197 *val >>= chip->config->bus_voltage_shift; in ina2xx_read_raw()
202 *val = chip->avg; in ina2xx_read_raw()
207 if (chan->address == INA2XX_SHUNT_VOLTAGE) in ina2xx_read_raw()
208 *val2 = chip->int_time_vshunt; in ina2xx_read_raw()
210 *val2 = chip->int_time_vbus; in ina2xx_read_raw()
217 * 1/AVG*(CT_bus+CT_shunt). in ina2xx_read_raw()
224 switch (chan->address) { in ina2xx_read_raw()
227 *val = chip->config->shunt_voltage_lsb; in ina2xx_read_raw()
233 *val = chip->config->bus_voltage_lsb; in ina2xx_read_raw()
243 *val = chip->config->shunt_voltage_lsb; in ina2xx_read_raw()
244 *val2 = chip->shunt_resistor_uohm; in ina2xx_read_raw()
253 *val = chip->config->power_lsb_factor * in ina2xx_read_raw()
254 chip->config->shunt_voltage_lsb; in ina2xx_read_raw()
255 *val2 = chip->shunt_resistor_uohm; in ina2xx_read_raw()
258 return -EINVAL; in ina2xx_read_raw()
261 switch (chan->address) { in ina2xx_read_raw()
263 *val = chip->pga_gain_vshunt; in ina2xx_read_raw()
268 *val = chip->range_vbus == 32 ? 1 : 2; in ina2xx_read_raw()
271 return -EINVAL; in ina2xx_read_raw()
274 return -EINVAL; in ina2xx_read_raw()
280 * table 3 AVG bit settings, found at
291 return -EINVAL; in ina226_set_average()
296 chip->avg = ina226_avg_tab[bits]; in ina226_set_average()
314 return -EINVAL; in ina226_set_int_time_vbus()
319 chip->int_time_vbus = ina226_conv_time_tab[bits]; in ina226_set_int_time_vbus()
333 return -EINVAL; in ina226_set_int_time_vshunt()
338 chip->int_time_vshunt = ina226_conv_time_tab[bits]; in ina226_set_int_time_vshunt()
354 return -EINVAL; in ina219_lookup_int_time()
380 chip->int_time_vbus = val_us_best; in ina219_set_int_time_vbus()
398 chip->int_time_vshunt = val_us_best; in ina219_set_int_time_vshunt()
412 chip->range_vbus = 32; in ina219_set_vbus_range_denom()
414 chip->range_vbus = 16; in ina219_set_vbus_range_denom()
416 return -EINVAL; in ina219_set_vbus_range_denom()
435 return -EINVAL; in ina219_set_vshunt_pga_gain()
440 chip->pga_gain_vshunt = ina219_vshunt_gain_tab[bits]; in ina219_set_vshunt_pga_gain()
441 bits = 3 - bits; in ina219_set_vshunt_pga_gain()
456 switch (chan->address) { in ina2xx_read_avail()
471 return -EINVAL; in ina2xx_read_avail()
483 return -EBUSY; in ina2xx_write_raw()
485 mutex_lock(&chip->state_lock); in ina2xx_write_raw()
487 ret = regmap_read(chip->regmap, INA2XX_CONFIG, &config); in ina2xx_write_raw()
499 if (chip->config->chip_id == ina226) { in ina2xx_write_raw()
500 if (chan->address == INA2XX_SHUNT_VOLTAGE) in ina2xx_write_raw()
507 if (chan->address == INA2XX_SHUNT_VOLTAGE) in ina2xx_write_raw()
517 if (chan->address == INA2XX_SHUNT_VOLTAGE) in ina2xx_write_raw()
525 ret = -EINVAL; in ina2xx_write_raw()
529 ret = regmap_write(chip->regmap, INA2XX_CONFIG, tmp); in ina2xx_write_raw()
531 mutex_unlock(&chip->state_lock); in ina2xx_write_raw()
542 return sprintf(buf, "%d\n", chip->allow_async_readout); in ina2xx_allow_async_readout_show()
557 chip->allow_async_readout = val; in ina2xx_allow_async_readout_store()
571 return regmap_write(chip->regmap, INA2XX_CALIBRATION, in ina2xx_set_calibration()
572 chip->config->calibration_value); in ina2xx_set_calibration()
578 return -EINVAL; in set_shunt_resistor()
580 chip->shunt_resistor_uohm = val; in set_shunt_resistor()
590 int vals[2] = { chip->shunt_resistor_uohm, 1000000 }; in ina2xx_shunt_resistor_show()
668 .realbits = 16 - _shift, \
719 * result in reading V[k-1] again, or skip V[k] at time Tk. in ina2xx_conversion_ready()
727 if (chip->config->chip_id == ina226) { in ina2xx_conversion_ready()
728 ret = regmap_read(chip->regmap, in ina2xx_conversion_ready()
732 ret = regmap_read(chip->regmap, in ina2xx_conversion_ready()
753 * as there is no auto-increment of the register pointer. in ina2xx_work_buffer()
755 for_each_set_bit(bit, indio_dev->active_scan_mask, in ina2xx_work_buffer()
756 indio_dev->masklength) { in ina2xx_work_buffer()
759 ret = regmap_read(chip->regmap, in ina2xx_work_buffer()
764 chip->scan.chan[i++] = val; in ina2xx_work_buffer()
767 iio_push_to_buffers_with_timestamp(indio_dev, &chip->scan, time); in ina2xx_work_buffer()
785 if (!chip->allow_async_readout) in ina2xx_capture_thread()
786 sampling_us -= 200; in ina2xx_capture_thread()
791 while (!chip->allow_async_readout) { in ina2xx_capture_thread()
814 * interval, and sleep for the remainder (next - now) in ina2xx_capture_thread()
816 * multiple times, i.e. samples are dropped. in ina2xx_capture_thread()
837 dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n", in ina2xx_buffer_enable()
838 (unsigned int)(*indio_dev->active_scan_mask), in ina2xx_buffer_enable()
839 1000000 / sampling_us, chip->avg); in ina2xx_buffer_enable()
841 dev_dbg(&indio_dev->dev, "Expected work period: %u us\n", sampling_us); in ina2xx_buffer_enable()
842 dev_dbg(&indio_dev->dev, "Async readout mode: %d\n", in ina2xx_buffer_enable()
843 chip->allow_async_readout); in ina2xx_buffer_enable()
846 "%s:%d-%uus", indio_dev->name, indio_dev->id, in ina2xx_buffer_enable()
853 chip->task = task; in ina2xx_buffer_enable()
862 if (chip->task) { in ina2xx_buffer_disable()
863 kthread_stop(chip->task); in ina2xx_buffer_disable()
864 put_task_struct(chip->task); in ina2xx_buffer_disable()
865 chip->task = NULL; in ina2xx_buffer_disable()
882 return regmap_write(chip->regmap, reg, writeval); in ina2xx_debug_reg()
884 return regmap_read(chip->regmap, reg, readval); in ina2xx_debug_reg()
944 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 = (enum ina2xx_ids)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()
1002 if (id->driver_data == ina226) { 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 | INDIO_BUFFER_SOFTWARE; in ina2xx_probe()
1021 if (id->driver_data == ina226) { 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->name; in ina2xx_probe()
1031 indio_dev->setup_ops = &ina2xx_setup_ops; in ina2xx_probe()
1033 buffer = devm_iio_kfifo_allocate(&indio_dev->dev); in ina2xx_probe()
1035 return -ENOMEM; in ina2xx_probe()
1050 return regmap_update_bits(chip->regmap, INA2XX_CONFIG, in ina2xx_remove()