ltc4215.c 7.4 KB
Newer Older
I
Ira Snyder 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Driver for Linear Technology LTC4215 I2C Hot Swap Controller
 *
 * Copyright (C) 2009 Ira W. Snyder <iws@ovro.caltech.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * Datasheet:
 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
22
#include <linux/jiffies.h>
I
Ira Snyder 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35

/* Here are names of the chip's registers (a.k.a. commands) */
enum ltc4215_cmd {
	LTC4215_CONTROL			= 0x00, /* rw */
	LTC4215_ALERT			= 0x01, /* rw */
	LTC4215_STATUS			= 0x02, /* ro */
	LTC4215_FAULT			= 0x03, /* rw */
	LTC4215_SENSE			= 0x04, /* rw */
	LTC4215_SOURCE			= 0x05, /* rw */
	LTC4215_ADIN			= 0x06, /* rw */
};

struct ltc4215_data {
36
	struct i2c_client *client;
I
Ira Snyder 已提交
37 38 39 40 41 42 43 44 45 46 47

	struct mutex update_lock;
	bool valid;
	unsigned long last_updated; /* in jiffies */

	/* Registers */
	u8 regs[7];
};

static struct ltc4215_data *ltc4215_update_device(struct device *dev)
{
48 49
	struct ltc4215_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
I
Ira Snyder 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	s32 val;
	int i;

	mutex_lock(&data->update_lock);

	/* The chip's A/D updates 10 times per second */
	if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {

		dev_dbg(&client->dev, "Starting ltc4215 update\n");

		/* Read all registers */
		for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
			val = i2c_smbus_read_byte_data(client, i);
			if (unlikely(val < 0))
				data->regs[i] = 0;
			else
				data->regs[i] = val;
		}

		data->last_updated = jiffies;
		data->valid = 1;
	}

	mutex_unlock(&data->update_lock);

	return data;
}

/* Return the voltage from the given register in millivolts */
static int ltc4215_get_voltage(struct device *dev, u8 reg)
{
	struct ltc4215_data *data = ltc4215_update_device(dev);
	const u8 regval = data->regs[reg];
	u32 voltage = 0;

	switch (reg) {
	case LTC4215_SENSE:
		/* 151 uV per increment */
		voltage = regval * 151 / 1000;
		break;
	case LTC4215_SOURCE:
		/* 60.5 mV per increment */
		voltage = regval * 605 / 10;
		break;
	case LTC4215_ADIN:
95 96 97 98
		/*
		 * The ADIN input is divided by 12.5, and has 4.82 mV
		 * per increment, so we have the additional multiply
		 */
I
Ira Snyder 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		voltage = regval * 482 * 125 / 1000;
		break;
	default:
		/* If we get here, the developer messed up */
		WARN_ON_ONCE(1);
		break;
	}

	return voltage;
}

/* Return the current from the sense resistor in mA */
static unsigned int ltc4215_get_current(struct device *dev)
{
	struct ltc4215_data *data = ltc4215_update_device(dev);

115 116
	/*
	 * The strange looking conversions that follow are fixed-point
I
Ira Snyder 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	 * math, since we cannot do floating point in the kernel.
	 *
	 * Step 1: convert sense register to microVolts
	 * Step 2: convert voltage to milliAmperes
	 *
	 * If you play around with the V=IR equation, you come up with
	 * the following: X uV / Y mOhm == Z mA
	 *
	 * With the resistors that are fractions of a milliOhm, we multiply
	 * the voltage and resistance by 10, to shift the decimal point.
	 * Now we can use the normal division operator again.
	 */

	/* Calculate voltage in microVolts (151 uV per increment) */
	const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;

	/* Calculate current in milliAmperes (4 milliOhm sense resistor) */
	const unsigned int curr = voltage / 4;

	return curr;
}

static ssize_t ltc4215_show_voltage(struct device *dev,
				    struct device_attribute *da,
				    char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
	const int voltage = ltc4215_get_voltage(dev, attr->index);

	return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
}

static ssize_t ltc4215_show_current(struct device *dev,
				    struct device_attribute *da,
				    char *buf)
{
	const unsigned int curr = ltc4215_get_current(dev);

	return snprintf(buf, PAGE_SIZE, "%u\n", curr);
}

static ssize_t ltc4215_show_power(struct device *dev,
				  struct device_attribute *da,
				  char *buf)
{
	const unsigned int curr = ltc4215_get_current(dev);
	const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);

	/* current in mA * voltage in mV == power in uW */
	const unsigned int power = abs(output_voltage * curr);

	return snprintf(buf, PAGE_SIZE, "%u\n", power);
}

static ssize_t ltc4215_show_alarm(struct device *dev,
					  struct device_attribute *da,
					  char *buf)
{
175
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
I
Ira Snyder 已提交
176
	struct ltc4215_data *data = ltc4215_update_device(dev);
177 178
	const u8 reg = data->regs[LTC4215_STATUS];
	const u32 mask = attr->index;
I
Ira Snyder 已提交
179

180
	return snprintf(buf, PAGE_SIZE, "%u\n", !!(reg & mask));
I
Ira Snyder 已提交
181 182
}

183 184
/*
 * These macros are used below in constructing device attribute objects
I
Ira Snyder 已提交
185 186 187 188 189 190 191
 * for use with sysfs_create_group() to make a sysfs device file
 * for each register.
 */

/* Construct a sensor_device_attribute structure for each register */

/* Current */
192 193 194
static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4215_show_current, NULL, 0);
static SENSOR_DEVICE_ATTR(curr1_max_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
			  1 << 2);
I
Ira Snyder 已提交
195 196

/* Power (virtual) */
197
static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc4215_show_power, NULL, 0);
I
Ira Snyder 已提交
198 199

/* Input Voltage */
200 201 202 203 204 205
static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4215_show_voltage, NULL,
			  LTC4215_ADIN);
static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
			  1 << 0);
static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
			  1 << 1);
I
Ira Snyder 已提交
206 207

/* Output Voltage */
208 209 210 211
static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4215_show_voltage, NULL,
			  LTC4215_SOURCE);
static SENSOR_DEVICE_ATTR(in2_min_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
			  1 << 3);
I
Ira Snyder 已提交
212

213 214
/*
 * Finally, construct an array of pointers to members of the above objects,
I
Ira Snyder 已提交
215 216
 * as required for sysfs_create_group()
 */
217
static struct attribute *ltc4215_attrs[] = {
I
Ira Snyder 已提交
218 219 220 221 222 223 224 225 226 227
	&sensor_dev_attr_curr1_input.dev_attr.attr,
	&sensor_dev_attr_curr1_max_alarm.dev_attr.attr,

	&sensor_dev_attr_power1_input.dev_attr.attr,

	&sensor_dev_attr_in1_input.dev_attr.attr,
	&sensor_dev_attr_in1_max_alarm.dev_attr.attr,
	&sensor_dev_attr_in1_min_alarm.dev_attr.attr,

	&sensor_dev_attr_in2_input.dev_attr.attr,
228
	&sensor_dev_attr_in2_min_alarm.dev_attr.attr,
I
Ira Snyder 已提交
229 230 231

	NULL,
};
232
ATTRIBUTE_GROUPS(ltc4215);
I
Ira Snyder 已提交
233 234 235 236

static int ltc4215_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
237
	struct i2c_adapter *adapter = client->adapter;
238
	struct device *dev = &client->dev;
I
Ira Snyder 已提交
239
	struct ltc4215_data *data;
240
	struct device *hwmon_dev;
I
Ira Snyder 已提交
241

242 243 244
	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -ENODEV;

245
	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
246 247
	if (!data)
		return -ENOMEM;
I
Ira Snyder 已提交
248

249
	data->client = client;
I
Ira Snyder 已提交
250 251 252
	mutex_init(&data->update_lock);

	/* Initialize the LTC4215 chip */
253
	i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
I
Ira Snyder 已提交
254

255 256 257 258
	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
							   data,
							   ltc4215_groups);
	return PTR_ERR_OR_ZERO(hwmon_dev);
I
Ira Snyder 已提交
259 260 261
}

static const struct i2c_device_id ltc4215_id[] = {
262
	{ "ltc4215", 0 },
I
Ira Snyder 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275
	{ }
};
MODULE_DEVICE_TABLE(i2c, ltc4215_id);

/* This is the driver that will be inserted */
static struct i2c_driver ltc4215_driver = {
	.driver = {
		.name	= "ltc4215",
	},
	.probe		= ltc4215_probe,
	.id_table	= ltc4215_id,
};

276
module_i2c_driver(ltc4215_driver);
I
Ira Snyder 已提交
277 278 279 280

MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
MODULE_DESCRIPTION("LTC4215 driver");
MODULE_LICENSE("GPL");