Lines Matching +full:i2c +full:- +full:retry +full:- +full:count
1 // SPDX-License-Identifier: GPL-2.0-only
6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
10 * Bi-Directional Current/Power Monitor with I2C Interface
14 * Bi-Directional Current/Power Monitor with I2C Interface
18 * Bi-directional Current/Power Monitor with I2C Interface
30 #include <linux/i2c.h>
32 #include <linux/hwmon-sysfs.h>
55 /* register count */
61 /* settings - depend on use case */
190 return regmap_write(data->regmap, INA2XX_CALIBRATION, in ina2xx_calibrate()
191 data->config->calibration_value); in ina2xx_calibrate()
199 int ret = regmap_write(data->regmap, INA2XX_CONFIG, in ina2xx_init()
200 data->config->config_default); in ina2xx_init()
210 int ret, retry; in ina2xx_read_reg() local
214 for (retry = 5; retry; retry--) { in ina2xx_read_reg()
216 ret = regmap_read(data->regmap, reg, regval); in ina2xx_read_reg()
233 ret = regmap_read(data->regmap, INA2XX_CALIBRATION, in ina2xx_read_reg()
262 return -ENODEV; in ina2xx_read_reg()
273 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div); in ina2xx_get_value()
276 val = (regval >> data->config->bus_voltage_shift) in ina2xx_get_value()
277 * data->config->bus_voltage_lsb; in ina2xx_get_value()
281 val = regval * data->power_lsb_uW; in ina2xx_get_value()
285 val = (s16)regval * data->current_lsb_uA; in ina2xx_get_value()
308 int err = ina2xx_read_reg(dev, attr->index, ®val); in ina2xx_value_show()
314 ina2xx_get_value(data, attr->index, regval)); in ina2xx_value_show()
351 val *= data->config->shunt_div; in ina226_alert_to_reg()
355 val = (val * 1000) << data->config->bus_voltage_shift; in ina226_alert_to_reg()
356 val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb); in ina226_alert_to_reg()
359 val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW); in ina226_alert_to_reg()
377 mutex_lock(&data->config_lock); in ina226_alert_show()
378 ret = regmap_read(data->regmap, INA226_MASK_ENABLE, ®val); in ina226_alert_show()
382 if (regval & BIT(attr->index)) { in ina226_alert_show()
383 ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, ®val); in ina226_alert_show()
386 val = ina226_reg_to_alert(data, attr->index, regval); in ina226_alert_show()
391 mutex_unlock(&data->config_lock); in ina226_alert_show()
397 const char *buf, size_t count) in ina226_alert_store() argument
411 * if the value is non-zero. in ina226_alert_store()
413 mutex_lock(&data->config_lock); in ina226_alert_store()
414 ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE, in ina226_alert_store()
419 ret = regmap_write(data->regmap, INA226_ALERT_LIMIT, in ina226_alert_store()
420 ina226_alert_to_reg(data, attr->index, val)); in ina226_alert_store()
425 ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE, in ina226_alert_store()
427 BIT(attr->index)); in ina226_alert_store()
432 ret = count; in ina226_alert_store()
434 mutex_unlock(&data->config_lock); in ina226_alert_store()
447 ret = regmap_read(data->regmap, INA226_MASK_ENABLE, ®val); in ina226_alarm_show()
451 alarm = (regval & BIT(attr->index)) && in ina226_alarm_show()
465 data->config->shunt_div); in ina2xx_set_shunt()
467 return -EINVAL; in ina2xx_set_shunt()
469 mutex_lock(&data->config_lock); in ina2xx_set_shunt()
470 data->rshunt = val; in ina2xx_set_shunt()
471 data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val); in ina2xx_set_shunt()
472 data->power_lsb_uW = data->config->power_lsb_factor * in ina2xx_set_shunt()
473 data->current_lsb_uA; in ina2xx_set_shunt()
474 mutex_unlock(&data->config_lock); in ina2xx_set_shunt()
484 return snprintf(buf, PAGE_SIZE, "%li\n", data->rshunt); in ina2xx_shunt_show()
489 const char *buf, size_t count) in ina2xx_shunt_store() argument
502 return count; in ina2xx_shunt_store()
507 const char *buf, size_t count) in ina226_interval_store() argument
518 return -EINVAL; in ina226_interval_store()
520 status = regmap_update_bits(data->regmap, INA2XX_CONFIG, in ina226_interval_store()
526 return count; in ina226_interval_store()
536 status = regmap_read(data->regmap, INA2XX_CONFIG, ®val); in ina226_interval_show()
572 /* over-limit power alert setting and alarm */
621 struct device *dev = &client->dev; in ina2xx_probe()
628 if (client->dev.of_node) in ina2xx_probe()
629 chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev); in ina2xx_probe()
631 chip = i2c_match_id(ina2xx_id, client)->driver_data; in ina2xx_probe()
635 return -ENOMEM; in ina2xx_probe()
638 data->config = &ina2xx_config[chip]; in ina2xx_probe()
639 mutex_init(&data->config_lock); in ina2xx_probe()
641 if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) { in ina2xx_probe()
645 val = pdata->shunt_uohms; in ina2xx_probe()
652 ina2xx_regmap_config.max_register = data->config->registers; in ina2xx_probe()
654 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config); in ina2xx_probe()
655 if (IS_ERR(data->regmap)) { in ina2xx_probe()
657 return PTR_ERR(data->regmap); in ina2xx_probe()
663 return -ENODEV; in ina2xx_probe()
666 data->groups[group++] = &ina2xx_group; in ina2xx_probe()
668 data->groups[group++] = &ina226_group; in ina2xx_probe()
670 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, in ina2xx_probe()
671 data, data->groups); in ina2xx_probe()
676 client->name, data->rshunt); in ina2xx_probe()
689 MODULE_DEVICE_TABLE(i2c, ina2xx_id);
727 MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");