| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 1 | /* | 
 | 2 |  * Driver for Linear Technology LTC4151 High Voltage I2C Current | 
 | 3 |  * and Voltage Monitor | 
 | 4 |  * | 
 | 5 |  * Copyright (C) 2011 AppearTV AS | 
 | 6 |  * | 
 | 7 |  * Derived from: | 
 | 8 |  * | 
 | 9 |  *  Driver for Linear Technology LTC4261 I2C Negative Voltage Hot | 
 | 10 |  *  Swap Controller | 
 | 11 |  *  Copyright (C) 2010 Ericsson AB. | 
 | 12 |  * | 
 | 13 |  * Datasheet: http://www.linear.com/docs/Datasheet/4151fc.pdf | 
 | 14 |  * | 
 | 15 |  * This program is free software; you can redistribute it and/or modify | 
 | 16 |  * it under the terms of the GNU General Public License as published by | 
 | 17 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 18 |  * (at your option) any later version. | 
 | 19 |  * | 
 | 20 |  * This program is distributed in the hope that it will be useful, | 
 | 21 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 22 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 23 |  * GNU General Public License for more details. | 
 | 24 |  * | 
 | 25 |  * You should have received a copy of the GNU General Public License | 
 | 26 |  * along with this program; if not, write to the Free Software | 
 | 27 |  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
 | 28 |  * | 
 | 29 |  */ | 
 | 30 |  | 
 | 31 | #include <linux/kernel.h> | 
 | 32 | #include <linux/module.h> | 
 | 33 | #include <linux/init.h> | 
 | 34 | #include <linux/err.h> | 
 | 35 | #include <linux/slab.h> | 
 | 36 | #include <linux/i2c.h> | 
 | 37 | #include <linux/hwmon.h> | 
 | 38 | #include <linux/hwmon-sysfs.h> | 
| Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 39 | #include <linux/jiffies.h> | 
| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 40 |  | 
 | 41 | /* chip registers */ | 
 | 42 | #define LTC4151_SENSE_H	0x00 | 
 | 43 | #define LTC4151_SENSE_L	0x01 | 
 | 44 | #define LTC4151_VIN_H	0x02 | 
 | 45 | #define LTC4151_VIN_L	0x03 | 
 | 46 | #define LTC4151_ADIN_H	0x04 | 
 | 47 | #define LTC4151_ADIN_L	0x05 | 
 | 48 |  | 
 | 49 | struct ltc4151_data { | 
 | 50 | 	struct device *hwmon_dev; | 
 | 51 |  | 
 | 52 | 	struct mutex update_lock; | 
 | 53 | 	bool valid; | 
 | 54 | 	unsigned long last_updated; /* in jiffies */ | 
 | 55 |  | 
 | 56 | 	/* Registers */ | 
 | 57 | 	u8 regs[6]; | 
 | 58 | }; | 
 | 59 |  | 
 | 60 | static struct ltc4151_data *ltc4151_update_device(struct device *dev) | 
 | 61 | { | 
 | 62 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 63 | 	struct ltc4151_data *data = i2c_get_clientdata(client); | 
 | 64 | 	struct ltc4151_data *ret = data; | 
 | 65 |  | 
 | 66 | 	mutex_lock(&data->update_lock); | 
 | 67 |  | 
 | 68 | 	/* | 
 | 69 | 	 * The chip's A/D updates 6 times per second | 
 | 70 | 	 * (Conversion Rate 6 - 9 Hz) | 
 | 71 | 	 */ | 
 | 72 | 	if (time_after(jiffies, data->last_updated + HZ / 6) || !data->valid) { | 
 | 73 | 		int i; | 
 | 74 |  | 
 | 75 | 		dev_dbg(&client->dev, "Starting ltc4151 update\n"); | 
 | 76 |  | 
 | 77 | 		/* Read all registers */ | 
 | 78 | 		for (i = 0; i < ARRAY_SIZE(data->regs); i++) { | 
 | 79 | 			int val; | 
 | 80 |  | 
 | 81 | 			val = i2c_smbus_read_byte_data(client, i); | 
 | 82 | 			if (unlikely(val < 0)) { | 
 | 83 | 				dev_dbg(dev, | 
 | 84 | 					"Failed to read ADC value: error %d\n", | 
 | 85 | 					val); | 
 | 86 | 				ret = ERR_PTR(val); | 
 | 87 | 				goto abort; | 
 | 88 | 			} | 
 | 89 | 			data->regs[i] = val; | 
 | 90 | 		} | 
 | 91 | 		data->last_updated = jiffies; | 
 | 92 | 		data->valid = 1; | 
 | 93 | 	} | 
 | 94 | abort: | 
 | 95 | 	mutex_unlock(&data->update_lock); | 
 | 96 | 	return ret; | 
 | 97 | } | 
 | 98 |  | 
 | 99 | /* Return the voltage from the given register in mV */ | 
 | 100 | static int ltc4151_get_value(struct ltc4151_data *data, u8 reg) | 
 | 101 | { | 
 | 102 | 	u32 val; | 
 | 103 |  | 
 | 104 | 	val = (data->regs[reg] << 4) + (data->regs[reg + 1] >> 4); | 
 | 105 |  | 
 | 106 | 	switch (reg) { | 
 | 107 | 	case LTC4151_ADIN_H: | 
 | 108 | 		/* 500uV resolution. Convert to mV. */ | 
 | 109 | 		val = val * 500 / 1000; | 
 | 110 | 		break; | 
 | 111 | 	case LTC4151_SENSE_H: | 
 | 112 | 		/* | 
 | 113 | 		 * 20uV resolution. Convert to current as measured with | 
 | 114 | 		 * an 1 mOhm sense resistor, in mA. | 
 | 115 | 		 */ | 
 | 116 | 		val = val * 20; | 
 | 117 | 		break; | 
 | 118 | 	case LTC4151_VIN_H: | 
 | 119 | 		/* 25 mV per increment */ | 
 | 120 | 		val = val * 25; | 
 | 121 | 		break; | 
 | 122 | 	default: | 
 | 123 | 		/* If we get here, the developer messed up */ | 
 | 124 | 		WARN_ON_ONCE(1); | 
 | 125 | 		val = 0; | 
 | 126 | 		break; | 
 | 127 | 	} | 
 | 128 |  | 
 | 129 | 	return val; | 
 | 130 | } | 
 | 131 |  | 
 | 132 | static ssize_t ltc4151_show_value(struct device *dev, | 
 | 133 | 				  struct device_attribute *da, char *buf) | 
 | 134 | { | 
 | 135 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
 | 136 | 	struct ltc4151_data *data = ltc4151_update_device(dev); | 
 | 137 | 	int value; | 
 | 138 |  | 
 | 139 | 	if (IS_ERR(data)) | 
 | 140 | 		return PTR_ERR(data); | 
 | 141 |  | 
 | 142 | 	value = ltc4151_get_value(data, attr->index); | 
 | 143 | 	return snprintf(buf, PAGE_SIZE, "%d\n", value); | 
 | 144 | } | 
 | 145 |  | 
 | 146 | /* | 
 | 147 |  * Input voltages. | 
 | 148 |  */ | 
| Guenter Roeck | bc0c591 | 2013-01-19 21:18:41 -0800 | [diff] [blame] | 149 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4151_show_value, NULL, | 
 | 150 | 			  LTC4151_VIN_H); | 
 | 151 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4151_show_value, NULL, | 
 | 152 | 			  LTC4151_ADIN_H); | 
| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 153 |  | 
 | 154 | /* Currents (via sense resistor) */ | 
| Guenter Roeck | bc0c591 | 2013-01-19 21:18:41 -0800 | [diff] [blame] | 155 | static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4151_show_value, NULL, | 
 | 156 | 			  LTC4151_SENSE_H); | 
| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 157 |  | 
| Guenter Roeck | 1b05d22 | 2012-01-19 11:02:21 -0800 | [diff] [blame] | 158 | /* | 
 | 159 |  * Finally, construct an array of pointers to members of the above objects, | 
| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 160 |  * as required for sysfs_create_group() | 
 | 161 |  */ | 
 | 162 | static struct attribute *ltc4151_attributes[] = { | 
 | 163 | 	&sensor_dev_attr_in1_input.dev_attr.attr, | 
 | 164 | 	&sensor_dev_attr_in2_input.dev_attr.attr, | 
 | 165 |  | 
 | 166 | 	&sensor_dev_attr_curr1_input.dev_attr.attr, | 
 | 167 |  | 
 | 168 | 	NULL, | 
 | 169 | }; | 
 | 170 |  | 
 | 171 | static const struct attribute_group ltc4151_group = { | 
 | 172 | 	.attrs = ltc4151_attributes, | 
 | 173 | }; | 
 | 174 |  | 
 | 175 | static int ltc4151_probe(struct i2c_client *client, | 
 | 176 | 			 const struct i2c_device_id *id) | 
 | 177 | { | 
 | 178 | 	struct i2c_adapter *adapter = client->adapter; | 
 | 179 | 	struct ltc4151_data *data; | 
 | 180 | 	int ret; | 
 | 181 |  | 
 | 182 | 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
 | 183 | 		return -ENODEV; | 
 | 184 |  | 
| Guenter Roeck | 7896864 | 2012-06-02 10:35:52 -0700 | [diff] [blame] | 185 | 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); | 
 | 186 | 	if (!data) | 
 | 187 | 		return -ENOMEM; | 
| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 188 |  | 
 | 189 | 	i2c_set_clientdata(client, data); | 
 | 190 | 	mutex_init(&data->update_lock); | 
 | 191 |  | 
 | 192 | 	/* Register sysfs hooks */ | 
 | 193 | 	ret = sysfs_create_group(&client->dev.kobj, <c4151_group); | 
 | 194 | 	if (ret) | 
| Guenter Roeck | 7896864 | 2012-06-02 10:35:52 -0700 | [diff] [blame] | 195 | 		return ret; | 
| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 196 |  | 
 | 197 | 	data->hwmon_dev = hwmon_device_register(&client->dev); | 
 | 198 | 	if (IS_ERR(data->hwmon_dev)) { | 
 | 199 | 		ret = PTR_ERR(data->hwmon_dev); | 
 | 200 | 		goto out_hwmon_device_register; | 
 | 201 | 	} | 
 | 202 |  | 
 | 203 | 	return 0; | 
 | 204 |  | 
 | 205 | out_hwmon_device_register: | 
 | 206 | 	sysfs_remove_group(&client->dev.kobj, <c4151_group); | 
| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 207 | 	return ret; | 
 | 208 | } | 
 | 209 |  | 
 | 210 | static int ltc4151_remove(struct i2c_client *client) | 
 | 211 | { | 
 | 212 | 	struct ltc4151_data *data = i2c_get_clientdata(client); | 
 | 213 |  | 
 | 214 | 	hwmon_device_unregister(data->hwmon_dev); | 
 | 215 | 	sysfs_remove_group(&client->dev.kobj, <c4151_group); | 
 | 216 |  | 
| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 217 | 	return 0; | 
 | 218 | } | 
 | 219 |  | 
 | 220 | static const struct i2c_device_id ltc4151_id[] = { | 
 | 221 | 	{ "ltc4151", 0 }, | 
 | 222 | 	{ } | 
 | 223 | }; | 
 | 224 | MODULE_DEVICE_TABLE(i2c, ltc4151_id); | 
 | 225 |  | 
 | 226 | /* This is the driver that will be inserted */ | 
 | 227 | static struct i2c_driver ltc4151_driver = { | 
 | 228 | 	.driver = { | 
 | 229 | 		.name	= "ltc4151", | 
 | 230 | 	}, | 
 | 231 | 	.probe		= ltc4151_probe, | 
 | 232 | 	.remove		= ltc4151_remove, | 
 | 233 | 	.id_table	= ltc4151_id, | 
 | 234 | }; | 
 | 235 |  | 
| Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 236 | module_i2c_driver(ltc4151_driver); | 
| per.dalen@appeartv.com | de77901 | 2011-03-03 14:13:21 -0500 | [diff] [blame] | 237 |  | 
 | 238 | MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>"); | 
 | 239 | MODULE_DESCRIPTION("LTC4151 driver"); | 
 | 240 | MODULE_LICENSE("GPL"); |