Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for Linear Technology LTC4151 High Voltage I2C Current
0004  * and Voltage Monitor
0005  *
0006  * Copyright (C) 2011 AppearTV AS
0007  *
0008  * Derived from:
0009  *
0010  *  Driver for Linear Technology LTC4261 I2C Negative Voltage Hot
0011  *  Swap Controller
0012  *  Copyright (C) 2010 Ericsson AB.
0013  *
0014  * Datasheet: http://www.linear.com/docs/Datasheet/4151fc.pdf
0015  */
0016 
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019 #include <linux/of.h>
0020 #include <linux/init.h>
0021 #include <linux/err.h>
0022 #include <linux/slab.h>
0023 #include <linux/i2c.h>
0024 #include <linux/hwmon.h>
0025 #include <linux/hwmon-sysfs.h>
0026 #include <linux/jiffies.h>
0027 
0028 /* chip registers */
0029 #define LTC4151_SENSE_H 0x00
0030 #define LTC4151_SENSE_L 0x01
0031 #define LTC4151_VIN_H   0x02
0032 #define LTC4151_VIN_L   0x03
0033 #define LTC4151_ADIN_H  0x04
0034 #define LTC4151_ADIN_L  0x05
0035 
0036 struct ltc4151_data {
0037     struct i2c_client *client;
0038 
0039     struct mutex update_lock;
0040     bool valid;
0041     unsigned long last_updated; /* in jiffies */
0042     unsigned int shunt; /* in micro ohms */
0043 
0044     /* Registers */
0045     u8 regs[6];
0046 };
0047 
0048 static struct ltc4151_data *ltc4151_update_device(struct device *dev)
0049 {
0050     struct ltc4151_data *data = dev_get_drvdata(dev);
0051     struct i2c_client *client = data->client;
0052     struct ltc4151_data *ret = data;
0053 
0054     mutex_lock(&data->update_lock);
0055 
0056     /*
0057      * The chip's A/D updates 6 times per second
0058      * (Conversion Rate 6 - 9 Hz)
0059      */
0060     if (time_after(jiffies, data->last_updated + HZ / 6) || !data->valid) {
0061         int i;
0062 
0063         dev_dbg(&client->dev, "Starting ltc4151 update\n");
0064 
0065         /* Read all registers */
0066         for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
0067             int val;
0068 
0069             val = i2c_smbus_read_byte_data(client, i);
0070             if (unlikely(val < 0)) {
0071                 dev_dbg(dev,
0072                     "Failed to read ADC value: error %d\n",
0073                     val);
0074                 ret = ERR_PTR(val);
0075                 goto abort;
0076             }
0077             data->regs[i] = val;
0078         }
0079         data->last_updated = jiffies;
0080         data->valid = true;
0081     }
0082 abort:
0083     mutex_unlock(&data->update_lock);
0084     return ret;
0085 }
0086 
0087 /* Return the voltage from the given register in mV */
0088 static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
0089 {
0090     u32 val;
0091 
0092     val = (data->regs[reg] << 4) + (data->regs[reg + 1] >> 4);
0093 
0094     switch (reg) {
0095     case LTC4151_ADIN_H:
0096         /* 500uV resolution. Convert to mV. */
0097         val = val * 500 / 1000;
0098         break;
0099     case LTC4151_SENSE_H:
0100         /*
0101          * 20uV resolution. Convert to current as measured with
0102          * a given sense resistor, in mA.
0103          */
0104         val = val * 20 * 1000 / data->shunt;
0105         break;
0106     case LTC4151_VIN_H:
0107         /* 25 mV per increment */
0108         val = val * 25;
0109         break;
0110     default:
0111         /* If we get here, the developer messed up */
0112         WARN_ON_ONCE(1);
0113         val = 0;
0114         break;
0115     }
0116 
0117     return val;
0118 }
0119 
0120 static ssize_t ltc4151_value_show(struct device *dev,
0121                   struct device_attribute *da, char *buf)
0122 {
0123     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0124     struct ltc4151_data *data = ltc4151_update_device(dev);
0125     int value;
0126 
0127     if (IS_ERR(data))
0128         return PTR_ERR(data);
0129 
0130     value = ltc4151_get_value(data, attr->index);
0131     return sysfs_emit(buf, "%d\n", value);
0132 }
0133 
0134 /*
0135  * Input voltages.
0136  */
0137 static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4151_value, LTC4151_VIN_H);
0138 static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4151_value, LTC4151_ADIN_H);
0139 
0140 /* Currents (via sense resistor) */
0141 static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4151_value, LTC4151_SENSE_H);
0142 
0143 /*
0144  * Finally, construct an array of pointers to members of the above objects,
0145  * as required for sysfs_create_group()
0146  */
0147 static struct attribute *ltc4151_attrs[] = {
0148     &sensor_dev_attr_in1_input.dev_attr.attr,
0149     &sensor_dev_attr_in2_input.dev_attr.attr,
0150 
0151     &sensor_dev_attr_curr1_input.dev_attr.attr,
0152 
0153     NULL,
0154 };
0155 ATTRIBUTE_GROUPS(ltc4151);
0156 
0157 static int ltc4151_probe(struct i2c_client *client)
0158 {
0159     struct i2c_adapter *adapter = client->adapter;
0160     struct device *dev = &client->dev;
0161     struct ltc4151_data *data;
0162     struct device *hwmon_dev;
0163     u32 shunt;
0164 
0165     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0166         return -ENODEV;
0167 
0168     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0169     if (!data)
0170         return -ENOMEM;
0171 
0172     if (of_property_read_u32(client->dev.of_node,
0173                  "shunt-resistor-micro-ohms", &shunt))
0174         shunt = 1000; /* 1 mOhm if not set via DT */
0175 
0176     if (shunt == 0)
0177         return -EINVAL;
0178 
0179     data->shunt = shunt;
0180 
0181     data->client = client;
0182     mutex_init(&data->update_lock);
0183 
0184     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0185                                data,
0186                                ltc4151_groups);
0187     return PTR_ERR_OR_ZERO(hwmon_dev);
0188 }
0189 
0190 static const struct i2c_device_id ltc4151_id[] = {
0191     { "ltc4151", 0 },
0192     { }
0193 };
0194 MODULE_DEVICE_TABLE(i2c, ltc4151_id);
0195 
0196 static const struct of_device_id __maybe_unused ltc4151_match[] = {
0197     { .compatible = "lltc,ltc4151" },
0198     {},
0199 };
0200 MODULE_DEVICE_TABLE(of, ltc4151_match);
0201 
0202 /* This is the driver that will be inserted */
0203 static struct i2c_driver ltc4151_driver = {
0204     .driver = {
0205         .name   = "ltc4151",
0206         .of_match_table = of_match_ptr(ltc4151_match),
0207     },
0208     .probe_new  = ltc4151_probe,
0209     .id_table   = ltc4151_id,
0210 };
0211 
0212 module_i2c_driver(ltc4151_driver);
0213 
0214 MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
0215 MODULE_DESCRIPTION("LTC4151 driver");
0216 MODULE_LICENSE("GPL");