linux/drivers/hwmon/ltc4215.c
<<
>>
Prefs
   1/*
   2 * Driver for Linear Technology LTC4215 I2C Hot Swap Controller
   3 *
   4 * Copyright (C) 2009 Ira W. Snyder <iws@ovro.caltech.edu>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; version 2 of the License.
   9 *
  10 * Datasheet:
  11 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
  12 */
  13
  14#include <linux/kernel.h>
  15#include <linux/module.h>
  16#include <linux/init.h>
  17#include <linux/err.h>
  18#include <linux/slab.h>
  19#include <linux/i2c.h>
  20#include <linux/hwmon.h>
  21#include <linux/hwmon-sysfs.h>
  22#include <linux/jiffies.h>
  23
  24/* Here are names of the chip's registers (a.k.a. commands) */
  25enum ltc4215_cmd {
  26        LTC4215_CONTROL                 = 0x00, /* rw */
  27        LTC4215_ALERT                   = 0x01, /* rw */
  28        LTC4215_STATUS                  = 0x02, /* ro */
  29        LTC4215_FAULT                   = 0x03, /* rw */
  30        LTC4215_SENSE                   = 0x04, /* rw */
  31        LTC4215_SOURCE                  = 0x05, /* rw */
  32        LTC4215_ADIN                    = 0x06, /* rw */
  33};
  34
  35struct ltc4215_data {
  36        struct device *hwmon_dev;
  37
  38        struct mutex update_lock;
  39        bool valid;
  40        unsigned long last_updated; /* in jiffies */
  41
  42        /* Registers */
  43        u8 regs[7];
  44};
  45
  46static struct ltc4215_data *ltc4215_update_device(struct device *dev)
  47{
  48        struct i2c_client *client = to_i2c_client(dev);
  49        struct ltc4215_data *data = i2c_get_clientdata(client);
  50        s32 val;
  51        int i;
  52
  53        mutex_lock(&data->update_lock);
  54
  55        /* The chip's A/D updates 10 times per second */
  56        if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
  57
  58                dev_dbg(&client->dev, "Starting ltc4215 update\n");
  59
  60                /* Read all registers */
  61                for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  62                        val = i2c_smbus_read_byte_data(client, i);
  63                        if (unlikely(val < 0))
  64                                data->regs[i] = 0;
  65                        else
  66                                data->regs[i] = val;
  67                }
  68
  69                data->last_updated = jiffies;
  70                data->valid = 1;
  71        }
  72
  73        mutex_unlock(&data->update_lock);
  74
  75        return data;
  76}
  77
  78/* Return the voltage from the given register in millivolts */
  79static int ltc4215_get_voltage(struct device *dev, u8 reg)
  80{
  81        struct ltc4215_data *data = ltc4215_update_device(dev);
  82        const u8 regval = data->regs[reg];
  83        u32 voltage = 0;
  84
  85        switch (reg) {
  86        case LTC4215_SENSE:
  87                /* 151 uV per increment */
  88                voltage = regval * 151 / 1000;
  89                break;
  90        case LTC4215_SOURCE:
  91                /* 60.5 mV per increment */
  92                voltage = regval * 605 / 10;
  93                break;
  94        case LTC4215_ADIN:
  95                /*
  96                 * The ADIN input is divided by 12.5, and has 4.82 mV
  97                 * per increment, so we have the additional multiply
  98                 */
  99                voltage = regval * 482 * 125 / 1000;
 100                break;
 101        default:
 102                /* If we get here, the developer messed up */
 103                WARN_ON_ONCE(1);
 104                break;
 105        }
 106
 107        return voltage;
 108}
 109
 110/* Return the current from the sense resistor in mA */
 111static unsigned int ltc4215_get_current(struct device *dev)
 112{
 113        struct ltc4215_data *data = ltc4215_update_device(dev);
 114
 115        /*
 116         * The strange looking conversions that follow are fixed-point
 117         * math, since we cannot do floating point in the kernel.
 118         *
 119         * Step 1: convert sense register to microVolts
 120         * Step 2: convert voltage to milliAmperes
 121         *
 122         * If you play around with the V=IR equation, you come up with
 123         * the following: X uV / Y mOhm == Z mA
 124         *
 125         * With the resistors that are fractions of a milliOhm, we multiply
 126         * the voltage and resistance by 10, to shift the decimal point.
 127         * Now we can use the normal division operator again.
 128         */
 129
 130        /* Calculate voltage in microVolts (151 uV per increment) */
 131        const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
 132
 133        /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
 134        const unsigned int curr = voltage / 4;
 135
 136        return curr;
 137}
 138
 139static ssize_t ltc4215_show_voltage(struct device *dev,
 140                                    struct device_attribute *da,
 141                                    char *buf)
 142{
 143        struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
 144        const int voltage = ltc4215_get_voltage(dev, attr->index);
 145
 146        return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
 147}
 148
 149static ssize_t ltc4215_show_current(struct device *dev,
 150                                    struct device_attribute *da,
 151                                    char *buf)
 152{
 153        const unsigned int curr = ltc4215_get_current(dev);
 154
 155        return snprintf(buf, PAGE_SIZE, "%u\n", curr);
 156}
 157
 158static ssize_t ltc4215_show_power(struct device *dev,
 159                                  struct device_attribute *da,
 160                                  char *buf)
 161{
 162        const unsigned int curr = ltc4215_get_current(dev);
 163        const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
 164
 165        /* current in mA * voltage in mV == power in uW */
 166        const unsigned int power = abs(output_voltage * curr);
 167
 168        return snprintf(buf, PAGE_SIZE, "%u\n", power);
 169}
 170
 171static ssize_t ltc4215_show_alarm(struct device *dev,
 172                                          struct device_attribute *da,
 173                                          char *buf)
 174{
 175        struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
 176        struct ltc4215_data *data = ltc4215_update_device(dev);
 177        const u8 reg = data->regs[attr->index];
 178        const u32 mask = attr->nr;
 179
 180        return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
 181}
 182
 183/*
 184 * These macros are used below in constructing device attribute objects
 185 * for use with sysfs_create_group() to make a sysfs device file
 186 * for each register.
 187 */
 188
 189#define LTC4215_VOLTAGE(name, ltc4215_cmd_idx) \
 190        static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
 191        ltc4215_show_voltage, NULL, ltc4215_cmd_idx)
 192
 193#define LTC4215_CURRENT(name) \
 194        static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
 195        ltc4215_show_current, NULL, 0);
 196
 197#define LTC4215_POWER(name) \
 198        static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
 199        ltc4215_show_power, NULL, 0);
 200
 201#define LTC4215_ALARM(name, mask, reg) \
 202        static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
 203        ltc4215_show_alarm, NULL, (mask), reg)
 204
 205/* Construct a sensor_device_attribute structure for each register */
 206
 207/* Current */
 208LTC4215_CURRENT(curr1_input);
 209LTC4215_ALARM(curr1_max_alarm,  (1 << 2),       LTC4215_STATUS);
 210
 211/* Power (virtual) */
 212LTC4215_POWER(power1_input);
 213
 214/* Input Voltage */
 215LTC4215_VOLTAGE(in1_input,                      LTC4215_ADIN);
 216LTC4215_ALARM(in1_max_alarm,    (1 << 0),       LTC4215_STATUS);
 217LTC4215_ALARM(in1_min_alarm,    (1 << 1),       LTC4215_STATUS);
 218
 219/* Output Voltage */
 220LTC4215_VOLTAGE(in2_input,                      LTC4215_SOURCE);
 221LTC4215_ALARM(in2_min_alarm,    (1 << 3),       LTC4215_STATUS);
 222
 223/*
 224 * Finally, construct an array of pointers to members of the above objects,
 225 * as required for sysfs_create_group()
 226 */
 227static struct attribute *ltc4215_attributes[] = {
 228        &sensor_dev_attr_curr1_input.dev_attr.attr,
 229        &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
 230
 231        &sensor_dev_attr_power1_input.dev_attr.attr,
 232
 233        &sensor_dev_attr_in1_input.dev_attr.attr,
 234        &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
 235        &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
 236
 237        &sensor_dev_attr_in2_input.dev_attr.attr,
 238        &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
 239
 240        NULL,
 241};
 242
 243static const struct attribute_group ltc4215_group = {
 244        .attrs = ltc4215_attributes,
 245};
 246
 247static int ltc4215_probe(struct i2c_client *client,
 248                         const struct i2c_device_id *id)
 249{
 250        struct i2c_adapter *adapter = client->adapter;
 251        struct ltc4215_data *data;
 252        int ret;
 253
 254        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 255                return -ENODEV;
 256
 257        data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
 258        if (!data)
 259                return -ENOMEM;
 260
 261        i2c_set_clientdata(client, data);
 262        mutex_init(&data->update_lock);
 263
 264        /* Initialize the LTC4215 chip */
 265        i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
 266
 267        /* Register sysfs hooks */
 268        ret = sysfs_create_group(&client->dev.kobj, &ltc4215_group);
 269        if (ret)
 270                return ret;
 271
 272        data->hwmon_dev = hwmon_device_register(&client->dev);
 273        if (IS_ERR(data->hwmon_dev)) {
 274                ret = PTR_ERR+hwmon05rn )
 272         272        href="drivers/hwmon/ltc4215.c#L270" id="L2client" cr_dev_attr_2" class="sre2">to_2ensor_dev_attr; 2c4215_upd2te_device" class="sref">2tc42127t_voltage * [2a href="+code=attr" clas2="sre27/ltc4215.c#L167" id= 99                mask2/a> = attr->nr;
 219 272         272        href:f">attr-> 268        ret = sysfs_create_group(&client->dev.ret)
}
2ivers/hwm2n/ltc4215.c#L183" id="L123" cl28="sref">mask),  * These m2cros 2re use215.c#L246" id="L246" class="line"removnref">client-> 247static int );
 * 2or us2 with ode=IS_ERR" class="sref">IS_ERR(<.c#L186" 2d="L186" class="line" na2e="L128/ltc4215.c#L2_2(da);
 176 260
dev. 1872*/
 272un         274                ret = PTR_ERR+hwm+code=nam2" class="sref">name,2 268        ret = sysfs_create_group(&client->PTR_ERR+hwm+70" classhref="+code=name" class=2sref"29            return NULL                2"+code=LT24215_CURRENT" class="sre2">LTC29="sref">mask), (client,
ltc4225_sho29nsor_dev_attr{"+code=snprintf" class="srefttributtf(ltc4215_probe(struct NUL2, 0);
2"egs[2=LTC4215_POWER" class="s2ef">L2C4215_tc4215_attributes" class="sref">ltc421TR((LMODULEs/hwmon/TABL"" class="line" name=c42rivers/hwmon/ltc42/a>        PTR_ERR+hw3c4215_sho3_power" class="sref">ltc3215_s3ow_power, NUL3, 0);


name3/a>,  226(, ( 2=hw="+code=snprintf" class="srefttributtf((ltc4215_probe(struct 3/* Const3uct a306me,  246 246
ltc4215_probe(struct 3<7a>, 0);
clientremovniver"+code=hw{
client->ltc4215_probe(struct 3<8a>, clientid_tablnme, ltc4215_probe(struct 3<9a>(ltc423max_alarm3/a>,  (1 << 2),   3   3TC4215_ST3TUS);
PTR_ERR+hw3.c#L211" 3d="L211" class="line" na3e="L231tc4215.c#L241" id="L241" class="line" na3215_POWER3 class="sref">LTC4215_PO3ER(PTR_ERR+hw3.4();
(PTR_ERR+hw3.5( 214LMODULEsLmonNS"" clascode=snprintf" class="srefGPLtf(PTR_ERR+hw3./a>,              3     3 


e/div>

, foo   <>
Tn/lorigpan  LXR softwommebymon/le=PTR_ERRhttp://sourceforge.net/pro24" i/lx <>LXR d="Lun.c#L254,monismexperiL210n  lassionebyme=PTR_ERRmailto:lx @   ux.noltex @   ux.note_gr
e/div>
, subfoo   <>
ex .   ux.no kindly hostedabyme=PTR_ERRhttp://www.redpill-   pro.noltRedpill L  pro ASL254,mprovidf=pof L  uxLientul"drivand operatc42s serc#L2s since">na5.
e/div>