| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2008 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 | * This driver is based on the ds1621 and ina209 drivers. | 
|  | 11 | * | 
|  | 12 | * Datasheet: | 
|  | 13 | * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1140,P19392,D13517 | 
|  | 14 | */ | 
|  | 15 |  | 
|  | 16 | #include <linux/kernel.h> | 
|  | 17 | #include <linux/module.h> | 
|  | 18 | #include <linux/init.h> | 
|  | 19 | #include <linux/err.h> | 
|  | 20 | #include <linux/slab.h> | 
|  | 21 | #include <linux/i2c.h> | 
|  | 22 | #include <linux/hwmon.h> | 
|  | 23 | #include <linux/hwmon-sysfs.h> | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 24 | #include <linux/i2c/ltc4245.h> | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 25 |  | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 26 | /* Here are names of the chip's registers (a.k.a. commands) */ | 
|  | 27 | enum ltc4245_cmd { | 
|  | 28 | LTC4245_STATUS			= 0x00, /* readonly */ | 
|  | 29 | LTC4245_ALERT			= 0x01, | 
|  | 30 | LTC4245_CONTROL			= 0x02, | 
|  | 31 | LTC4245_ON			= 0x03, | 
|  | 32 | LTC4245_FAULT1			= 0x04, | 
|  | 33 | LTC4245_FAULT2			= 0x05, | 
|  | 34 | LTC4245_GPIO			= 0x06, | 
|  | 35 | LTC4245_ADCADR			= 0x07, | 
|  | 36 |  | 
|  | 37 | LTC4245_12VIN			= 0x10, | 
|  | 38 | LTC4245_12VSENSE		= 0x11, | 
|  | 39 | LTC4245_12VOUT			= 0x12, | 
|  | 40 | LTC4245_5VIN			= 0x13, | 
|  | 41 | LTC4245_5VSENSE			= 0x14, | 
|  | 42 | LTC4245_5VOUT			= 0x15, | 
|  | 43 | LTC4245_3VIN			= 0x16, | 
|  | 44 | LTC4245_3VSENSE			= 0x17, | 
|  | 45 | LTC4245_3VOUT			= 0x18, | 
|  | 46 | LTC4245_VEEIN			= 0x19, | 
|  | 47 | LTC4245_VEESENSE		= 0x1a, | 
|  | 48 | LTC4245_VEEOUT			= 0x1b, | 
| Ira W. Snyder | df16dd5 | 2010-05-27 19:59:02 +0200 | [diff] [blame] | 49 | LTC4245_GPIOADC			= 0x1c, | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 50 | }; | 
|  | 51 |  | 
|  | 52 | struct ltc4245_data { | 
|  | 53 | struct device *hwmon_dev; | 
|  | 54 |  | 
|  | 55 | struct mutex update_lock; | 
|  | 56 | bool valid; | 
|  | 57 | unsigned long last_updated; /* in jiffies */ | 
|  | 58 |  | 
|  | 59 | /* Control registers */ | 
|  | 60 | u8 cregs[0x08]; | 
|  | 61 |  | 
|  | 62 | /* Voltage registers */ | 
| Ira W. Snyder | df16dd5 | 2010-05-27 19:59:02 +0200 | [diff] [blame] | 63 | u8 vregs[0x0d]; | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 64 |  | 
|  | 65 | /* GPIO ADC registers */ | 
|  | 66 | bool use_extra_gpios; | 
|  | 67 | int gpios[3]; | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 68 | }; | 
|  | 69 |  | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 70 | /* | 
|  | 71 | * Update the readings from the GPIO pins. If the driver has been configured to | 
|  | 72 | * sample all GPIO's as analog voltages, a round-robin sampling method is used. | 
|  | 73 | * Otherwise, only the configured GPIO pin is sampled. | 
|  | 74 | * | 
|  | 75 | * LOCKING: must hold data->update_lock | 
|  | 76 | */ | 
|  | 77 | static void ltc4245_update_gpios(struct device *dev) | 
|  | 78 | { | 
|  | 79 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 80 | struct ltc4245_data *data = i2c_get_clientdata(client); | 
|  | 81 | u8 gpio_curr, gpio_next, gpio_reg; | 
|  | 82 | int i; | 
|  | 83 |  | 
|  | 84 | /* no extra gpio support, we're basically done */ | 
|  | 85 | if (!data->use_extra_gpios) { | 
|  | 86 | data->gpios[0] = data->vregs[LTC4245_GPIOADC - 0x10]; | 
|  | 87 | return; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | /* | 
|  | 91 | * If the last reading was too long ago, then we mark all old GPIO | 
|  | 92 | * readings as stale by setting them to -EAGAIN | 
|  | 93 | */ | 
|  | 94 | if (time_after(jiffies, data->last_updated + 5 * HZ)) { | 
|  | 95 | dev_dbg(&client->dev, "Marking GPIOs invalid\n"); | 
|  | 96 | for (i = 0; i < ARRAY_SIZE(data->gpios); i++) | 
|  | 97 | data->gpios[i] = -EAGAIN; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | /* | 
|  | 101 | * Get the current GPIO pin | 
|  | 102 | * | 
|  | 103 | * The datasheet calls these GPIO[1-3], but we'll calculate the zero | 
|  | 104 | * based array index instead, and call them GPIO[0-2]. This is much | 
|  | 105 | * easier to think about. | 
|  | 106 | */ | 
|  | 107 | gpio_curr = (data->cregs[LTC4245_GPIO] & 0xc0) >> 6; | 
|  | 108 | if (gpio_curr > 0) | 
|  | 109 | gpio_curr -= 1; | 
|  | 110 |  | 
|  | 111 | /* Read the GPIO voltage from the GPIOADC register */ | 
|  | 112 | data->gpios[gpio_curr] = data->vregs[LTC4245_GPIOADC - 0x10]; | 
|  | 113 |  | 
|  | 114 | /* Find the next GPIO pin to read */ | 
|  | 115 | gpio_next = (gpio_curr + 1) % ARRAY_SIZE(data->gpios); | 
|  | 116 |  | 
|  | 117 | /* | 
|  | 118 | * Calculate the correct setting for the GPIO register so it will | 
|  | 119 | * sample the next GPIO pin | 
|  | 120 | */ | 
|  | 121 | gpio_reg = (data->cregs[LTC4245_GPIO] & 0x3f) | ((gpio_next + 1) << 6); | 
|  | 122 |  | 
|  | 123 | /* Update the GPIO register */ | 
|  | 124 | i2c_smbus_write_byte_data(client, LTC4245_GPIO, gpio_reg); | 
|  | 125 |  | 
|  | 126 | /* Update saved data */ | 
|  | 127 | data->cregs[LTC4245_GPIO] = gpio_reg; | 
|  | 128 | } | 
|  | 129 |  | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 130 | static struct ltc4245_data *ltc4245_update_device(struct device *dev) | 
|  | 131 | { | 
|  | 132 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 133 | struct ltc4245_data *data = i2c_get_clientdata(client); | 
|  | 134 | s32 val; | 
|  | 135 | int i; | 
|  | 136 |  | 
|  | 137 | mutex_lock(&data->update_lock); | 
|  | 138 |  | 
|  | 139 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | 
|  | 140 |  | 
|  | 141 | dev_dbg(&client->dev, "Starting ltc4245 update\n"); | 
|  | 142 |  | 
|  | 143 | /* Read control registers -- 0x00 to 0x07 */ | 
|  | 144 | for (i = 0; i < ARRAY_SIZE(data->cregs); i++) { | 
|  | 145 | val = i2c_smbus_read_byte_data(client, i); | 
|  | 146 | if (unlikely(val < 0)) | 
|  | 147 | data->cregs[i] = 0; | 
|  | 148 | else | 
|  | 149 | data->cregs[i] = val; | 
|  | 150 | } | 
|  | 151 |  | 
| Ira W. Snyder | df16dd5 | 2010-05-27 19:59:02 +0200 | [diff] [blame] | 152 | /* Read voltage registers -- 0x10 to 0x1c */ | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 153 | for (i = 0; i < ARRAY_SIZE(data->vregs); i++) { | 
|  | 154 | val = i2c_smbus_read_byte_data(client, i+0x10); | 
|  | 155 | if (unlikely(val < 0)) | 
|  | 156 | data->vregs[i] = 0; | 
|  | 157 | else | 
|  | 158 | data->vregs[i] = val; | 
|  | 159 | } | 
|  | 160 |  | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 161 | /* Update GPIO readings */ | 
|  | 162 | ltc4245_update_gpios(dev); | 
|  | 163 |  | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 164 | data->last_updated = jiffies; | 
|  | 165 | data->valid = 1; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | mutex_unlock(&data->update_lock); | 
|  | 169 |  | 
|  | 170 | return data; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | /* Return the voltage from the given register in millivolts */ | 
|  | 174 | static int ltc4245_get_voltage(struct device *dev, u8 reg) | 
|  | 175 | { | 
|  | 176 | struct ltc4245_data *data = ltc4245_update_device(dev); | 
|  | 177 | const u8 regval = data->vregs[reg - 0x10]; | 
|  | 178 | u32 voltage = 0; | 
|  | 179 |  | 
|  | 180 | switch (reg) { | 
|  | 181 | case LTC4245_12VIN: | 
|  | 182 | case LTC4245_12VOUT: | 
|  | 183 | voltage = regval * 55; | 
|  | 184 | break; | 
|  | 185 | case LTC4245_5VIN: | 
|  | 186 | case LTC4245_5VOUT: | 
|  | 187 | voltage = regval * 22; | 
|  | 188 | break; | 
|  | 189 | case LTC4245_3VIN: | 
|  | 190 | case LTC4245_3VOUT: | 
|  | 191 | voltage = regval * 15; | 
|  | 192 | break; | 
|  | 193 | case LTC4245_VEEIN: | 
|  | 194 | case LTC4245_VEEOUT: | 
|  | 195 | voltage = regval * -55; | 
|  | 196 | break; | 
| Ira W. Snyder | df16dd5 | 2010-05-27 19:59:02 +0200 | [diff] [blame] | 197 | case LTC4245_GPIOADC: | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 198 | voltage = regval * 10; | 
|  | 199 | break; | 
|  | 200 | default: | 
|  | 201 | /* If we get here, the developer messed up */ | 
|  | 202 | WARN_ON_ONCE(1); | 
|  | 203 | break; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | return voltage; | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | /* Return the current in the given sense register in milliAmperes */ | 
|  | 210 | static unsigned int ltc4245_get_current(struct device *dev, u8 reg) | 
|  | 211 | { | 
|  | 212 | struct ltc4245_data *data = ltc4245_update_device(dev); | 
|  | 213 | const u8 regval = data->vregs[reg - 0x10]; | 
|  | 214 | unsigned int voltage; | 
|  | 215 | unsigned int curr; | 
|  | 216 |  | 
|  | 217 | /* The strange looking conversions that follow are fixed-point | 
|  | 218 | * math, since we cannot do floating point in the kernel. | 
|  | 219 | * | 
|  | 220 | * Step 1: convert sense register to microVolts | 
|  | 221 | * Step 2: convert voltage to milliAmperes | 
|  | 222 | * | 
|  | 223 | * If you play around with the V=IR equation, you come up with | 
|  | 224 | * the following: X uV / Y mOhm == Z mA | 
|  | 225 | * | 
|  | 226 | * With the resistors that are fractions of a milliOhm, we multiply | 
|  | 227 | * the voltage and resistance by 10, to shift the decimal point. | 
|  | 228 | * Now we can use the normal division operator again. | 
|  | 229 | */ | 
|  | 230 |  | 
|  | 231 | switch (reg) { | 
|  | 232 | case LTC4245_12VSENSE: | 
|  | 233 | voltage = regval * 250; /* voltage in uV */ | 
|  | 234 | curr = voltage / 50; /* sense resistor 50 mOhm */ | 
|  | 235 | break; | 
|  | 236 | case LTC4245_5VSENSE: | 
|  | 237 | voltage = regval * 125; /* voltage in uV */ | 
|  | 238 | curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */ | 
|  | 239 | break; | 
|  | 240 | case LTC4245_3VSENSE: | 
|  | 241 | voltage = regval * 125; /* voltage in uV */ | 
|  | 242 | curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */ | 
|  | 243 | break; | 
|  | 244 | case LTC4245_VEESENSE: | 
|  | 245 | voltage = regval * 250; /* voltage in uV */ | 
|  | 246 | curr = voltage / 100; /* sense resistor 100 mOhm */ | 
|  | 247 | break; | 
|  | 248 | default: | 
|  | 249 | /* If we get here, the developer messed up */ | 
|  | 250 | WARN_ON_ONCE(1); | 
|  | 251 | curr = 0; | 
|  | 252 | break; | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | return curr; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | static ssize_t ltc4245_show_voltage(struct device *dev, | 
|  | 259 | struct device_attribute *da, | 
|  | 260 | char *buf) | 
|  | 261 | { | 
|  | 262 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
|  | 263 | const int voltage = ltc4245_get_voltage(dev, attr->index); | 
|  | 264 |  | 
|  | 265 | return snprintf(buf, PAGE_SIZE, "%d\n", voltage); | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | static ssize_t ltc4245_show_current(struct device *dev, | 
|  | 269 | struct device_attribute *da, | 
|  | 270 | char *buf) | 
|  | 271 | { | 
|  | 272 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
|  | 273 | const unsigned int curr = ltc4245_get_current(dev, attr->index); | 
|  | 274 |  | 
|  | 275 | return snprintf(buf, PAGE_SIZE, "%u\n", curr); | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | static ssize_t ltc4245_show_power(struct device *dev, | 
|  | 279 | struct device_attribute *da, | 
|  | 280 | char *buf) | 
|  | 281 | { | 
|  | 282 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
|  | 283 | const unsigned int curr = ltc4245_get_current(dev, attr->index); | 
|  | 284 | const int output_voltage = ltc4245_get_voltage(dev, attr->index+1); | 
|  | 285 |  | 
|  | 286 | /* current in mA * voltage in mV == power in uW */ | 
|  | 287 | const unsigned int power = abs(output_voltage * curr); | 
|  | 288 |  | 
|  | 289 | return snprintf(buf, PAGE_SIZE, "%u\n", power); | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | static ssize_t ltc4245_show_alarm(struct device *dev, | 
|  | 293 | struct device_attribute *da, | 
|  | 294 | char *buf) | 
|  | 295 | { | 
|  | 296 | struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da); | 
|  | 297 | struct ltc4245_data *data = ltc4245_update_device(dev); | 
|  | 298 | const u8 reg = data->cregs[attr->index]; | 
|  | 299 | const u32 mask = attr->nr; | 
|  | 300 |  | 
|  | 301 | return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0); | 
|  | 302 | } | 
|  | 303 |  | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 304 | static ssize_t ltc4245_show_gpio(struct device *dev, | 
|  | 305 | struct device_attribute *da, | 
|  | 306 | char *buf) | 
|  | 307 | { | 
|  | 308 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
|  | 309 | struct ltc4245_data *data = ltc4245_update_device(dev); | 
|  | 310 | int val = data->gpios[attr->index]; | 
|  | 311 |  | 
|  | 312 | /* handle stale GPIO's */ | 
|  | 313 | if (val < 0) | 
|  | 314 | return val; | 
|  | 315 |  | 
|  | 316 | /* Convert to millivolts and print */ | 
|  | 317 | return snprintf(buf, PAGE_SIZE, "%u\n", val * 10); | 
|  | 318 | } | 
|  | 319 |  | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 320 | /* These macros are used below in constructing device attribute objects | 
|  | 321 | * for use with sysfs_create_group() to make a sysfs device file | 
|  | 322 | * for each register. | 
|  | 323 | */ | 
|  | 324 |  | 
|  | 325 | #define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \ | 
|  | 326 | static SENSOR_DEVICE_ATTR(name, S_IRUGO, \ | 
|  | 327 | ltc4245_show_voltage, NULL, ltc4245_cmd_idx) | 
|  | 328 |  | 
|  | 329 | #define LTC4245_CURRENT(name, ltc4245_cmd_idx) \ | 
|  | 330 | static SENSOR_DEVICE_ATTR(name, S_IRUGO, \ | 
|  | 331 | ltc4245_show_current, NULL, ltc4245_cmd_idx) | 
|  | 332 |  | 
|  | 333 | #define LTC4245_POWER(name, ltc4245_cmd_idx) \ | 
|  | 334 | static SENSOR_DEVICE_ATTR(name, S_IRUGO, \ | 
|  | 335 | ltc4245_show_power, NULL, ltc4245_cmd_idx) | 
|  | 336 |  | 
|  | 337 | #define LTC4245_ALARM(name, mask, reg) \ | 
|  | 338 | static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \ | 
|  | 339 | ltc4245_show_alarm, NULL, (mask), reg) | 
|  | 340 |  | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 341 | #define LTC4245_GPIO_VOLTAGE(name, gpio_num) \ | 
|  | 342 | static SENSOR_DEVICE_ATTR(name, S_IRUGO, \ | 
|  | 343 | ltc4245_show_gpio, NULL, gpio_num) | 
|  | 344 |  | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 345 | /* Construct a sensor_device_attribute structure for each register */ | 
|  | 346 |  | 
|  | 347 | /* Input voltages */ | 
|  | 348 | LTC4245_VOLTAGE(in1_input,			LTC4245_12VIN); | 
|  | 349 | LTC4245_VOLTAGE(in2_input,			LTC4245_5VIN); | 
|  | 350 | LTC4245_VOLTAGE(in3_input,			LTC4245_3VIN); | 
|  | 351 | LTC4245_VOLTAGE(in4_input,			LTC4245_VEEIN); | 
|  | 352 |  | 
|  | 353 | /* Input undervoltage alarms */ | 
|  | 354 | LTC4245_ALARM(in1_min_alarm,	(1 << 0),	LTC4245_FAULT1); | 
|  | 355 | LTC4245_ALARM(in2_min_alarm,	(1 << 1),	LTC4245_FAULT1); | 
|  | 356 | LTC4245_ALARM(in3_min_alarm,	(1 << 2),	LTC4245_FAULT1); | 
|  | 357 | LTC4245_ALARM(in4_min_alarm,	(1 << 3),	LTC4245_FAULT1); | 
|  | 358 |  | 
|  | 359 | /* Currents (via sense resistor) */ | 
|  | 360 | LTC4245_CURRENT(curr1_input,			LTC4245_12VSENSE); | 
|  | 361 | LTC4245_CURRENT(curr2_input,			LTC4245_5VSENSE); | 
|  | 362 | LTC4245_CURRENT(curr3_input,			LTC4245_3VSENSE); | 
|  | 363 | LTC4245_CURRENT(curr4_input,			LTC4245_VEESENSE); | 
|  | 364 |  | 
|  | 365 | /* Overcurrent alarms */ | 
|  | 366 | LTC4245_ALARM(curr1_max_alarm,	(1 << 4),	LTC4245_FAULT1); | 
|  | 367 | LTC4245_ALARM(curr2_max_alarm,	(1 << 5),	LTC4245_FAULT1); | 
|  | 368 | LTC4245_ALARM(curr3_max_alarm,	(1 << 6),	LTC4245_FAULT1); | 
|  | 369 | LTC4245_ALARM(curr4_max_alarm,	(1 << 7),	LTC4245_FAULT1); | 
|  | 370 |  | 
|  | 371 | /* Output voltages */ | 
|  | 372 | LTC4245_VOLTAGE(in5_input,			LTC4245_12VOUT); | 
|  | 373 | LTC4245_VOLTAGE(in6_input,			LTC4245_5VOUT); | 
|  | 374 | LTC4245_VOLTAGE(in7_input,			LTC4245_3VOUT); | 
|  | 375 | LTC4245_VOLTAGE(in8_input,			LTC4245_VEEOUT); | 
|  | 376 |  | 
|  | 377 | /* Power Bad alarms */ | 
|  | 378 | LTC4245_ALARM(in5_min_alarm,	(1 << 0),	LTC4245_FAULT2); | 
|  | 379 | LTC4245_ALARM(in6_min_alarm,	(1 << 1),	LTC4245_FAULT2); | 
|  | 380 | LTC4245_ALARM(in7_min_alarm,	(1 << 2),	LTC4245_FAULT2); | 
|  | 381 | LTC4245_ALARM(in8_min_alarm,	(1 << 3),	LTC4245_FAULT2); | 
|  | 382 |  | 
|  | 383 | /* GPIO voltages */ | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 384 | LTC4245_GPIO_VOLTAGE(in9_input,			0); | 
|  | 385 | LTC4245_GPIO_VOLTAGE(in10_input,		1); | 
|  | 386 | LTC4245_GPIO_VOLTAGE(in11_input,		2); | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 387 |  | 
|  | 388 | /* Power Consumption (virtual) */ | 
|  | 389 | LTC4245_POWER(power1_input,			LTC4245_12VSENSE); | 
|  | 390 | LTC4245_POWER(power2_input,			LTC4245_5VSENSE); | 
|  | 391 | LTC4245_POWER(power3_input,			LTC4245_3VSENSE); | 
|  | 392 | LTC4245_POWER(power4_input,			LTC4245_VEESENSE); | 
|  | 393 |  | 
|  | 394 | /* Finally, construct an array of pointers to members of the above objects, | 
|  | 395 | * as required for sysfs_create_group() | 
|  | 396 | */ | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 397 | static struct attribute *ltc4245_std_attributes[] = { | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 398 | &sensor_dev_attr_in1_input.dev_attr.attr, | 
|  | 399 | &sensor_dev_attr_in2_input.dev_attr.attr, | 
|  | 400 | &sensor_dev_attr_in3_input.dev_attr.attr, | 
|  | 401 | &sensor_dev_attr_in4_input.dev_attr.attr, | 
|  | 402 |  | 
|  | 403 | &sensor_dev_attr_in1_min_alarm.dev_attr.attr, | 
|  | 404 | &sensor_dev_attr_in2_min_alarm.dev_attr.attr, | 
|  | 405 | &sensor_dev_attr_in3_min_alarm.dev_attr.attr, | 
|  | 406 | &sensor_dev_attr_in4_min_alarm.dev_attr.attr, | 
|  | 407 |  | 
|  | 408 | &sensor_dev_attr_curr1_input.dev_attr.attr, | 
|  | 409 | &sensor_dev_attr_curr2_input.dev_attr.attr, | 
|  | 410 | &sensor_dev_attr_curr3_input.dev_attr.attr, | 
|  | 411 | &sensor_dev_attr_curr4_input.dev_attr.attr, | 
|  | 412 |  | 
|  | 413 | &sensor_dev_attr_curr1_max_alarm.dev_attr.attr, | 
|  | 414 | &sensor_dev_attr_curr2_max_alarm.dev_attr.attr, | 
|  | 415 | &sensor_dev_attr_curr3_max_alarm.dev_attr.attr, | 
|  | 416 | &sensor_dev_attr_curr4_max_alarm.dev_attr.attr, | 
|  | 417 |  | 
|  | 418 | &sensor_dev_attr_in5_input.dev_attr.attr, | 
|  | 419 | &sensor_dev_attr_in6_input.dev_attr.attr, | 
|  | 420 | &sensor_dev_attr_in7_input.dev_attr.attr, | 
|  | 421 | &sensor_dev_attr_in8_input.dev_attr.attr, | 
|  | 422 |  | 
|  | 423 | &sensor_dev_attr_in5_min_alarm.dev_attr.attr, | 
|  | 424 | &sensor_dev_attr_in6_min_alarm.dev_attr.attr, | 
|  | 425 | &sensor_dev_attr_in7_min_alarm.dev_attr.attr, | 
|  | 426 | &sensor_dev_attr_in8_min_alarm.dev_attr.attr, | 
|  | 427 |  | 
|  | 428 | &sensor_dev_attr_in9_input.dev_attr.attr, | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 429 |  | 
|  | 430 | &sensor_dev_attr_power1_input.dev_attr.attr, | 
|  | 431 | &sensor_dev_attr_power2_input.dev_attr.attr, | 
|  | 432 | &sensor_dev_attr_power3_input.dev_attr.attr, | 
|  | 433 | &sensor_dev_attr_power4_input.dev_attr.attr, | 
|  | 434 |  | 
|  | 435 | NULL, | 
|  | 436 | }; | 
|  | 437 |  | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 438 | static struct attribute *ltc4245_gpio_attributes[] = { | 
|  | 439 | &sensor_dev_attr_in10_input.dev_attr.attr, | 
|  | 440 | &sensor_dev_attr_in11_input.dev_attr.attr, | 
|  | 441 | NULL, | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 442 | }; | 
|  | 443 |  | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 444 | static const struct attribute_group ltc4245_std_group = { | 
|  | 445 | .attrs = ltc4245_std_attributes, | 
|  | 446 | }; | 
|  | 447 |  | 
|  | 448 | static const struct attribute_group ltc4245_gpio_group = { | 
|  | 449 | .attrs = ltc4245_gpio_attributes, | 
|  | 450 | }; | 
|  | 451 |  | 
|  | 452 | static int ltc4245_sysfs_create_groups(struct i2c_client *client) | 
|  | 453 | { | 
|  | 454 | struct ltc4245_data *data = i2c_get_clientdata(client); | 
|  | 455 | struct device *dev = &client->dev; | 
|  | 456 | int ret; | 
|  | 457 |  | 
|  | 458 | /* register the standard sysfs attributes */ | 
|  | 459 | ret = sysfs_create_group(&dev->kobj, <c4245_std_group); | 
|  | 460 | if (ret) { | 
|  | 461 | dev_err(dev, "unable to register standard attributes\n"); | 
|  | 462 | return ret; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | /* if we're using the extra gpio support, register it's attributes */ | 
|  | 466 | if (data->use_extra_gpios) { | 
|  | 467 | ret = sysfs_create_group(&dev->kobj, <c4245_gpio_group); | 
|  | 468 | if (ret) { | 
|  | 469 | dev_err(dev, "unable to register gpio attributes\n"); | 
|  | 470 | sysfs_remove_group(&dev->kobj, <c4245_std_group); | 
|  | 471 | return ret; | 
|  | 472 | } | 
|  | 473 | } | 
|  | 474 |  | 
|  | 475 | return 0; | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | static void ltc4245_sysfs_remove_groups(struct i2c_client *client) | 
|  | 479 | { | 
|  | 480 | struct ltc4245_data *data = i2c_get_clientdata(client); | 
|  | 481 | struct device *dev = &client->dev; | 
|  | 482 |  | 
|  | 483 | if (data->use_extra_gpios) | 
|  | 484 | sysfs_remove_group(&dev->kobj, <c4245_gpio_group); | 
|  | 485 |  | 
|  | 486 | sysfs_remove_group(&dev->kobj, <c4245_std_group); | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | static bool ltc4245_use_extra_gpios(struct i2c_client *client) | 
|  | 490 | { | 
|  | 491 | struct ltc4245_platform_data *pdata = dev_get_platdata(&client->dev); | 
|  | 492 | #ifdef CONFIG_OF | 
|  | 493 | struct device_node *np = client->dev.of_node; | 
|  | 494 | #endif | 
|  | 495 |  | 
|  | 496 | /* prefer platform data */ | 
|  | 497 | if (pdata) | 
|  | 498 | return pdata->use_extra_gpios; | 
|  | 499 |  | 
|  | 500 | #ifdef CONFIG_OF | 
|  | 501 | /* fallback on OF */ | 
|  | 502 | if (of_find_property(np, "ltc4245,use-extra-gpios", NULL)) | 
|  | 503 | return true; | 
|  | 504 | #endif | 
|  | 505 |  | 
|  | 506 | return false; | 
|  | 507 | } | 
|  | 508 |  | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 509 | static int ltc4245_probe(struct i2c_client *client, | 
|  | 510 | const struct i2c_device_id *id) | 
|  | 511 | { | 
| Jean Delvare | 2d2a7cf | 2009-10-04 22:53:42 +0200 | [diff] [blame] | 512 | struct i2c_adapter *adapter = client->adapter; | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 513 | struct ltc4245_data *data; | 
|  | 514 | int ret; | 
|  | 515 |  | 
| Jean Delvare | 2d2a7cf | 2009-10-04 22:53:42 +0200 | [diff] [blame] | 516 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
|  | 517 | return -ENODEV; | 
|  | 518 |  | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 519 | data = kzalloc(sizeof(*data), GFP_KERNEL); | 
|  | 520 | if (!data) { | 
|  | 521 | ret = -ENOMEM; | 
|  | 522 | goto out_kzalloc; | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | i2c_set_clientdata(client, data); | 
|  | 526 | mutex_init(&data->update_lock); | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 527 | data->use_extra_gpios = ltc4245_use_extra_gpios(client); | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 528 |  | 
|  | 529 | /* Initialize the LTC4245 chip */ | 
| Ira W. Snyder | 58f055e | 2009-09-23 22:59:44 +0200 | [diff] [blame] | 530 | i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00); | 
|  | 531 | i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00); | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 532 |  | 
|  | 533 | /* Register sysfs hooks */ | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 534 | ret = ltc4245_sysfs_create_groups(client); | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 535 | if (ret) | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 536 | goto out_sysfs_create_groups; | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 537 |  | 
|  | 538 | data->hwmon_dev = hwmon_device_register(&client->dev); | 
|  | 539 | if (IS_ERR(data->hwmon_dev)) { | 
|  | 540 | ret = PTR_ERR(data->hwmon_dev); | 
|  | 541 | goto out_hwmon_device_register; | 
|  | 542 | } | 
|  | 543 |  | 
|  | 544 | return 0; | 
|  | 545 |  | 
|  | 546 | out_hwmon_device_register: | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 547 | ltc4245_sysfs_remove_groups(client); | 
|  | 548 | out_sysfs_create_groups: | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 549 | kfree(data); | 
|  | 550 | out_kzalloc: | 
|  | 551 | return ret; | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | static int ltc4245_remove(struct i2c_client *client) | 
|  | 555 | { | 
|  | 556 | struct ltc4245_data *data = i2c_get_clientdata(client); | 
|  | 557 |  | 
|  | 558 | hwmon_device_unregister(data->hwmon_dev); | 
| Ira W. Snyder | 5950ec8 | 2010-08-14 21:08:49 +0200 | [diff] [blame] | 559 | ltc4245_sysfs_remove_groups(client); | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 560 | kfree(data); | 
|  | 561 |  | 
|  | 562 | return 0; | 
|  | 563 | } | 
|  | 564 |  | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 565 | static const struct i2c_device_id ltc4245_id[] = { | 
| Jean Delvare | 2d2a7cf | 2009-10-04 22:53:42 +0200 | [diff] [blame] | 566 | { "ltc4245", 0 }, | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 567 | { } | 
|  | 568 | }; | 
|  | 569 | MODULE_DEVICE_TABLE(i2c, ltc4245_id); | 
|  | 570 |  | 
|  | 571 | /* This is the driver that will be inserted */ | 
|  | 572 | static struct i2c_driver ltc4245_driver = { | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 573 | .driver = { | 
|  | 574 | .name	= "ltc4245", | 
|  | 575 | }, | 
|  | 576 | .probe		= ltc4245_probe, | 
|  | 577 | .remove		= ltc4245_remove, | 
|  | 578 | .id_table	= ltc4245_id, | 
| Ira Snyder | 6e34b18 | 2009-01-07 16:37:32 +0100 | [diff] [blame] | 579 | }; | 
|  | 580 |  | 
|  | 581 | static int __init ltc4245_init(void) | 
|  | 582 | { | 
|  | 583 | return i2c_add_driver(<c4245_driver); | 
|  | 584 | } | 
|  | 585 |  | 
|  | 586 | static void __exit ltc4245_exit(void) | 
|  | 587 | { | 
|  | 588 | i2c_del_driver(<c4245_driver); | 
|  | 589 | } | 
|  | 590 |  | 
|  | 591 | MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>"); | 
|  | 592 | MODULE_DESCRIPTION("LTC4245 driver"); | 
|  | 593 | MODULE_LICENSE("GPL"); | 
|  | 594 |  | 
|  | 595 | module_init(ltc4245_init); | 
|  | 596 | module_exit(ltc4245_exit); |