Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lm87.c |
| 3 | * |
| 4 | * Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl> |
| 5 | * Philip Edelbrock <phil@netroedge.com> |
| 6 | * Stephen Rousset <stephen.rousset@rocketlogix.com> |
| 7 | * Dan Eaton <dan.eaton@rocketlogix.com> |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 8 | * Copyright (C) 2004-2008 Jean Delvare <khali@linux-fr.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * Original port to Linux 2.6 by Jeff Oliver. |
| 11 | * |
| 12 | * The LM87 is a sensor chip made by National Semiconductor. It monitors up |
| 13 | * to 8 voltages (including its own power source), up to three temperatures |
| 14 | * (its own plus up to two external ones) and up to two fans. The default |
| 15 | * configuration is 6 voltages, two temperatures and two fans (see below). |
| 16 | * Voltages are scaled internally with ratios such that the nominal value of |
| 17 | * each voltage correspond to a register value of 192 (which means a |
| 18 | * resolution of about 0.5% of the nominal value). Temperature values are |
| 19 | * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete |
| 20 | * datasheet can be obtained from National's website at: |
| 21 | * http://www.national.com/pf/LM/LM87.html |
| 22 | * |
| 23 | * Some functions share pins, so not all functions are available at the same |
Ben Hutchings | 47064d6 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 24 | * time. Which are depends on the hardware setup. This driver normally |
| 25 | * assumes that firmware configured the chip correctly. Where this is not |
| 26 | * the case, platform code must set the I2C client's platform_data to point |
| 27 | * to a u8 value to be written to the channel register. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * For reference, here is the list of exclusive functions: |
| 29 | * - in0+in5 (default) or temp3 |
| 30 | * - fan1 (default) or in6 |
| 31 | * - fan2 (default) or in7 |
| 32 | * - VID lines (default) or IRQ lines (not handled by this driver) |
| 33 | * |
| 34 | * The LM87 additionally features an analog output, supposedly usable to |
| 35 | * control the speed of a fan. All new chips use pulse width modulation |
| 36 | * instead. The LM87 is the only hardware monitoring chipset I know of |
| 37 | * which uses amplitude modulation. Be careful when using this feature. |
| 38 | * |
Jean Delvare | c7fa373 | 2007-10-09 15:22:22 +0200 | [diff] [blame] | 39 | * This driver also supports the ADM1024, a sensor chip made by Analog |
| 40 | * Devices. That chip is fully compatible with the LM87. Complete |
| 41 | * datasheet can be obtained from Analog's website at: |
| 42 | * http://www.analog.com/en/prod/0,2877,ADM1024,00.html |
| 43 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * This program is free software; you can redistribute it and/or modify |
| 45 | * it under the terms of the GNU General Public License as published by |
| 46 | * the Free Software Foundation; either version 2 of the License, or |
| 47 | * (at your option) any later version. |
| 48 | * |
| 49 | * This program is distributed in the hope that it will be useful, |
| 50 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 51 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 52 | * GNU General Public License for more details. |
| 53 | * |
| 54 | * You should have received a copy of the GNU General Public License |
| 55 | * along with this program; if not, write to the Free Software |
| 56 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 57 | */ |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/module.h> |
| 60 | #include <linux/init.h> |
| 61 | #include <linux/slab.h> |
| 62 | #include <linux/jiffies.h> |
| 63 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 64 | #include <linux/hwmon.h> |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 65 | #include <linux/hwmon-sysfs.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 66 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 67 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 68 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Addresses to scan |
| 72 | * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e. |
| 73 | */ |
| 74 | |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 75 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 77 | enum chips { lm87, adm1024 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * The LM87 registers |
| 81 | */ |
| 82 | |
| 83 | /* nr in 0..5 */ |
| 84 | #define LM87_REG_IN(nr) (0x20 + (nr)) |
| 85 | #define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2) |
| 86 | #define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2) |
| 87 | /* nr in 0..1 */ |
| 88 | #define LM87_REG_AIN(nr) (0x28 + (nr)) |
| 89 | #define LM87_REG_AIN_MIN(nr) (0x1A + (nr)) |
| 90 | #define LM87_REG_AIN_MAX(nr) (0x3B + (nr)) |
| 91 | |
| 92 | static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 }; |
| 93 | static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B }; |
| 94 | static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C }; |
| 95 | |
| 96 | #define LM87_REG_TEMP_HW_INT_LOCK 0x13 |
| 97 | #define LM87_REG_TEMP_HW_EXT_LOCK 0x14 |
| 98 | #define LM87_REG_TEMP_HW_INT 0x17 |
| 99 | #define LM87_REG_TEMP_HW_EXT 0x18 |
| 100 | |
| 101 | /* nr in 0..1 */ |
| 102 | #define LM87_REG_FAN(nr) (0x28 + (nr)) |
| 103 | #define LM87_REG_FAN_MIN(nr) (0x3B + (nr)) |
| 104 | #define LM87_REG_AOUT 0x19 |
| 105 | |
| 106 | #define LM87_REG_CONFIG 0x40 |
| 107 | #define LM87_REG_CHANNEL_MODE 0x16 |
| 108 | #define LM87_REG_VID_FAN_DIV 0x47 |
| 109 | #define LM87_REG_VID4 0x49 |
| 110 | |
| 111 | #define LM87_REG_ALARMS1 0x41 |
| 112 | #define LM87_REG_ALARMS2 0x42 |
| 113 | |
| 114 | #define LM87_REG_COMPANY_ID 0x3E |
| 115 | #define LM87_REG_REVISION 0x3F |
| 116 | |
| 117 | /* |
| 118 | * Conversions and various macros |
| 119 | * The LM87 uses signed 8-bit values for temperatures. |
| 120 | */ |
| 121 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 122 | #define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192) |
| 123 | #define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | (val) * 192 >= (scale) * 255 ? 255 : \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 125 | ((val) * 192 + (scale) / 2) / (scale)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | #define TEMP_FROM_REG(reg) ((reg) * 1000) |
| 128 | #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \ |
| 129 | (val) >= 126500 ? 127 : \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 130 | (((val) < 0 ? (val) - 500 : \ |
| 131 | (val) + 500) / 1000)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 133 | #define FAN_FROM_REG(reg, div) ((reg) == 255 || (reg) == 0 ? 0 : \ |
| 134 | (1350000 + (reg)*(div) / 2) / ((reg) * (div))) |
| 135 | #define FAN_TO_REG(val, div) ((val) * (div) * 255 <= 1350000 ? 255 : \ |
| 136 | (1350000 + (val)*(div) / 2) / ((val) * (div))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | #define FAN_DIV_FROM_REG(reg) (1 << (reg)) |
| 139 | |
| 140 | /* analog out is 9.80mV/LSB */ |
| 141 | #define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10) |
| 142 | #define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \ |
| 143 | (val) >= 2500 ? 255 : \ |
| 144 | ((val) * 10 + 49) / 98) |
| 145 | |
| 146 | /* nr in 0..1 */ |
| 147 | #define CHAN_NO_FAN(nr) (1 << (nr)) |
| 148 | #define CHAN_TEMP3 (1 << 2) |
| 149 | #define CHAN_VCC_5V (1 << 3) |
Jean Delvare | 889af3d | 2007-10-08 22:48:10 +0200 | [diff] [blame] | 150 | #define CHAN_NO_VID (1 << 7) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * Client data (each client gets its own) |
| 154 | */ |
| 155 | |
| 156 | struct lm87_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 157 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 158 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | char valid; /* zero until following fields are valid */ |
| 160 | unsigned long last_updated; /* In jiffies */ |
| 161 | |
| 162 | u8 channel; /* register value */ |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 163 | u8 config; /* original register value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | u8 in[8]; /* register value */ |
| 166 | u8 in_max[8]; /* register value */ |
| 167 | u8 in_min[8]; /* register value */ |
| 168 | u16 in_scale[8]; |
| 169 | |
| 170 | s8 temp[3]; /* register value */ |
| 171 | s8 temp_high[3]; /* register value */ |
| 172 | s8 temp_low[3]; /* register value */ |
| 173 | s8 temp_crit_int; /* min of two register values */ |
| 174 | s8 temp_crit_ext; /* min of two register values */ |
| 175 | |
| 176 | u8 fan[2]; /* register value */ |
| 177 | u8 fan_min[2]; /* register value */ |
| 178 | u8 fan_div[2]; /* register value, shifted right */ |
| 179 | u8 aout; /* register value */ |
| 180 | |
| 181 | u16 alarms; /* register values, combined */ |
| 182 | u8 vid; /* register values, combined */ |
| 183 | u8 vrm; |
| 184 | }; |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | static inline int lm87_read_value(struct i2c_client *client, u8 reg) |
| 187 | { |
| 188 | return i2c_smbus_read_byte_data(client, reg); |
| 189 | } |
| 190 | |
| 191 | static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 192 | { |
| 193 | return i2c_smbus_write_byte_data(client, reg, value); |
| 194 | } |
| 195 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 196 | static struct lm87_data *lm87_update_device(struct device *dev) |
| 197 | { |
| 198 | struct i2c_client *client = to_i2c_client(dev); |
| 199 | struct lm87_data *data = i2c_get_clientdata(client); |
| 200 | |
| 201 | mutex_lock(&data->update_lock); |
| 202 | |
| 203 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 204 | int i, j; |
| 205 | |
| 206 | dev_dbg(&client->dev, "Updating data.\n"); |
| 207 | |
| 208 | i = (data->channel & CHAN_TEMP3) ? 1 : 0; |
| 209 | j = (data->channel & CHAN_TEMP3) ? 5 : 6; |
| 210 | for (; i < j; i++) { |
| 211 | data->in[i] = lm87_read_value(client, |
| 212 | LM87_REG_IN(i)); |
| 213 | data->in_min[i] = lm87_read_value(client, |
| 214 | LM87_REG_IN_MIN(i)); |
| 215 | data->in_max[i] = lm87_read_value(client, |
| 216 | LM87_REG_IN_MAX(i)); |
| 217 | } |
| 218 | |
| 219 | for (i = 0; i < 2; i++) { |
| 220 | if (data->channel & CHAN_NO_FAN(i)) { |
| 221 | data->in[6+i] = lm87_read_value(client, |
| 222 | LM87_REG_AIN(i)); |
| 223 | data->in_max[6+i] = lm87_read_value(client, |
| 224 | LM87_REG_AIN_MAX(i)); |
| 225 | data->in_min[6+i] = lm87_read_value(client, |
| 226 | LM87_REG_AIN_MIN(i)); |
| 227 | |
| 228 | } else { |
| 229 | data->fan[i] = lm87_read_value(client, |
| 230 | LM87_REG_FAN(i)); |
| 231 | data->fan_min[i] = lm87_read_value(client, |
| 232 | LM87_REG_FAN_MIN(i)); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | j = (data->channel & CHAN_TEMP3) ? 3 : 2; |
| 237 | for (i = 0 ; i < j; i++) { |
| 238 | data->temp[i] = lm87_read_value(client, |
| 239 | LM87_REG_TEMP[i]); |
| 240 | data->temp_high[i] = lm87_read_value(client, |
| 241 | LM87_REG_TEMP_HIGH[i]); |
| 242 | data->temp_low[i] = lm87_read_value(client, |
| 243 | LM87_REG_TEMP_LOW[i]); |
| 244 | } |
| 245 | |
| 246 | i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK); |
| 247 | j = lm87_read_value(client, LM87_REG_TEMP_HW_INT); |
| 248 | data->temp_crit_int = min(i, j); |
| 249 | |
| 250 | i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK); |
| 251 | j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT); |
| 252 | data->temp_crit_ext = min(i, j); |
| 253 | |
| 254 | i = lm87_read_value(client, LM87_REG_VID_FAN_DIV); |
| 255 | data->fan_div[0] = (i >> 4) & 0x03; |
| 256 | data->fan_div[1] = (i >> 6) & 0x03; |
| 257 | data->vid = (i & 0x0F) |
| 258 | | (lm87_read_value(client, LM87_REG_VID4) & 0x01) |
| 259 | << 4; |
| 260 | |
| 261 | data->alarms = lm87_read_value(client, LM87_REG_ALARMS1) |
| 262 | | (lm87_read_value(client, LM87_REG_ALARMS2) |
| 263 | << 8); |
| 264 | data->aout = lm87_read_value(client, LM87_REG_AOUT); |
| 265 | |
| 266 | data->last_updated = jiffies; |
| 267 | data->valid = 1; |
| 268 | } |
| 269 | |
| 270 | mutex_unlock(&data->update_lock); |
| 271 | |
| 272 | return data; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Sysfs stuff |
| 277 | */ |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | #define show_in(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 280 | static ssize_t show_in##offset##_input(struct device *dev, \ |
| 281 | struct device_attribute *attr, \ |
| 282 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { \ |
| 284 | struct lm87_data *data = lm87_update_device(dev); \ |
| 285 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \ |
| 286 | data->in_scale[offset])); \ |
| 287 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 288 | static ssize_t show_in##offset##_min(struct device *dev, \ |
| 289 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { \ |
| 291 | struct lm87_data *data = lm87_update_device(dev); \ |
| 292 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \ |
| 293 | data->in_scale[offset])); \ |
| 294 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 295 | static ssize_t show_in##offset##_max(struct device *dev, \ |
| 296 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { \ |
| 298 | struct lm87_data *data = lm87_update_device(dev); \ |
| 299 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \ |
| 300 | data->in_scale[offset])); \ |
| 301 | } \ |
| 302 | static DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 303 | show_in##offset##_input, NULL); |
| 304 | show_in(0); |
| 305 | show_in(1); |
| 306 | show_in(2); |
| 307 | show_in(3); |
| 308 | show_in(4); |
| 309 | show_in(5); |
| 310 | show_in(6); |
| 311 | show_in(7); |
| 312 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 313 | static ssize_t set_in_min(struct device *dev, const char *buf, int nr, |
| 314 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
| 316 | struct i2c_client *client = to_i2c_client(dev); |
| 317 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 318 | long val; |
| 319 | int err; |
| 320 | |
| 321 | err = kstrtol(buf, 10, &val); |
| 322 | if (err) |
| 323 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 325 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 327 | lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) : |
| 328 | LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 329 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 330 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 333 | static ssize_t set_in_max(struct device *dev, const char *buf, int nr, |
| 334 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
| 336 | struct i2c_client *client = to_i2c_client(dev); |
| 337 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 338 | long val; |
| 339 | int err; |
| 340 | |
| 341 | err = kstrtol(buf, 10, &val); |
| 342 | if (err) |
| 343 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 345 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 347 | lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) : |
| 348 | LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 349 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 350 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | #define set_in(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 354 | static ssize_t set_in##offset##_min(struct device *dev, \ |
| 355 | struct device_attribute *attr, \ |
| 356 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 358 | return set_in_min(dev, buf, offset, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 360 | static ssize_t set_in##offset##_max(struct device *dev, \ |
| 361 | struct device_attribute *attr, \ |
| 362 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 364 | return set_in_max(dev, buf, offset, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } \ |
| 366 | static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 367 | show_in##offset##_min, set_in##offset##_min); \ |
| 368 | static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 369 | show_in##offset##_max, set_in##offset##_max); |
| 370 | set_in(0); |
| 371 | set_in(1); |
| 372 | set_in(2); |
| 373 | set_in(3); |
| 374 | set_in(4); |
| 375 | set_in(5); |
| 376 | set_in(6); |
| 377 | set_in(7); |
| 378 | |
| 379 | #define show_temp(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 380 | static ssize_t show_temp##offset##_input(struct device *dev, \ |
| 381 | struct device_attribute *attr, \ |
| 382 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { \ |
| 384 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 385 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset - 1])); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 387 | static ssize_t show_temp##offset##_low(struct device *dev, \ |
| 388 | struct device_attribute *attr, \ |
| 389 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { \ |
| 391 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 392 | return sprintf(buf, "%d\n", \ |
| 393 | TEMP_FROM_REG(data->temp_low[offset - 1])); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 395 | static ssize_t show_temp##offset##_high(struct device *dev, \ |
| 396 | struct device_attribute *attr, \ |
| 397 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { \ |
| 399 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 400 | return sprintf(buf, "%d\n", \ |
| 401 | TEMP_FROM_REG(data->temp_high[offset - 1])); \ |
| 402 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
| 404 | show_temp##offset##_input, NULL); |
| 405 | show_temp(1); |
| 406 | show_temp(2); |
| 407 | show_temp(3); |
| 408 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 409 | static ssize_t set_temp_low(struct device *dev, const char *buf, int nr, |
| 410 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
| 412 | struct i2c_client *client = to_i2c_client(dev); |
| 413 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 414 | long val; |
| 415 | int err; |
| 416 | |
| 417 | err = kstrtol(buf, 10, &val); |
| 418 | if (err) |
| 419 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 421 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | data->temp_low[nr] = TEMP_TO_REG(val); |
| 423 | lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 424 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 425 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 428 | static ssize_t set_temp_high(struct device *dev, const char *buf, int nr, |
| 429 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
| 431 | struct i2c_client *client = to_i2c_client(dev); |
| 432 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 433 | long val; |
| 434 | int err; |
| 435 | |
| 436 | err = kstrtol(buf, 10, &val); |
| 437 | if (err) |
| 438 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 440 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | data->temp_high[nr] = TEMP_TO_REG(val); |
| 442 | lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 443 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 444 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | #define set_temp(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 448 | static ssize_t set_temp##offset##_low(struct device *dev, \ |
| 449 | struct device_attribute *attr, \ |
| 450 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 452 | return set_temp_low(dev, buf, offset - 1, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 454 | static ssize_t set_temp##offset##_high(struct device *dev, \ |
| 455 | struct device_attribute *attr, \ |
| 456 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 458 | return set_temp_high(dev, buf, offset - 1, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } \ |
| 460 | static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ |
| 461 | show_temp##offset##_high, set_temp##offset##_high); \ |
| 462 | static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ |
| 463 | show_temp##offset##_low, set_temp##offset##_low); |
| 464 | set_temp(1); |
| 465 | set_temp(2); |
| 466 | set_temp(3); |
| 467 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 468 | static ssize_t show_temp_crit_int(struct device *dev, |
| 469 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
| 471 | struct lm87_data *data = lm87_update_device(dev); |
| 472 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int)); |
| 473 | } |
| 474 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 475 | static ssize_t show_temp_crit_ext(struct device *dev, |
| 476 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
| 478 | struct lm87_data *data = lm87_update_device(dev); |
| 479 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext)); |
| 480 | } |
| 481 | |
| 482 | static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL); |
| 483 | static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL); |
| 484 | static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL); |
| 485 | |
| 486 | #define show_fan(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 487 | static ssize_t show_fan##offset##_input(struct device *dev, \ |
| 488 | struct device_attribute *attr, \ |
| 489 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { \ |
| 491 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 492 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset - 1], \ |
| 493 | FAN_DIV_FROM_REG(data->fan_div[offset - 1]))); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 495 | static ssize_t show_fan##offset##_min(struct device *dev, \ |
| 496 | struct device_attribute *attr, \ |
| 497 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { \ |
| 499 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 500 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset - 1], \ |
| 501 | FAN_DIV_FROM_REG(data->fan_div[offset - 1]))); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 503 | static ssize_t show_fan##offset##_div(struct device *dev, \ |
| 504 | struct device_attribute *attr, \ |
| 505 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { \ |
| 507 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 508 | return sprintf(buf, "%d\n", \ |
| 509 | FAN_DIV_FROM_REG(data->fan_div[offset - 1])); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } \ |
| 511 | static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
| 512 | show_fan##offset##_input, NULL); |
| 513 | show_fan(1); |
| 514 | show_fan(2); |
| 515 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 516 | static ssize_t set_fan_min(struct device *dev, const char *buf, int nr, |
| 517 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
| 519 | struct i2c_client *client = to_i2c_client(dev); |
| 520 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 521 | long val; |
| 522 | int err; |
| 523 | |
| 524 | err = kstrtol(buf, 10, &val); |
| 525 | if (err) |
| 526 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 528 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | data->fan_min[nr] = FAN_TO_REG(val, |
| 530 | FAN_DIV_FROM_REG(data->fan_div[nr])); |
| 531 | lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 532 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 533 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 536 | /* |
| 537 | * Note: we save and restore the fan minimum here, because its value is |
| 538 | * determined in part by the fan clock divider. This follows the principle |
| 539 | * of least surprise; the user doesn't expect the fan minimum to change just |
| 540 | * because the divider changed. |
| 541 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | static ssize_t set_fan_div(struct device *dev, const char *buf, |
| 543 | size_t count, int nr) |
| 544 | { |
| 545 | struct i2c_client *client = to_i2c_client(dev); |
| 546 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 547 | long val; |
| 548 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | unsigned long min; |
| 550 | u8 reg; |
| 551 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 552 | err = kstrtol(buf, 10, &val); |
| 553 | if (err) |
| 554 | return err; |
| 555 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 556 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | min = FAN_FROM_REG(data->fan_min[nr], |
| 558 | FAN_DIV_FROM_REG(data->fan_div[nr])); |
| 559 | |
| 560 | switch (val) { |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 561 | case 1: |
| 562 | data->fan_div[nr] = 0; |
| 563 | break; |
| 564 | case 2: |
| 565 | data->fan_div[nr] = 1; |
| 566 | break; |
| 567 | case 4: |
| 568 | data->fan_div[nr] = 2; |
| 569 | break; |
| 570 | case 8: |
| 571 | data->fan_div[nr] = 3; |
| 572 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | default: |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 574 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | return -EINVAL; |
| 576 | } |
| 577 | |
| 578 | reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV); |
| 579 | switch (nr) { |
| 580 | case 0: |
| 581 | reg = (reg & 0xCF) | (data->fan_div[0] << 4); |
| 582 | break; |
| 583 | case 1: |
| 584 | reg = (reg & 0x3F) | (data->fan_div[1] << 6); |
| 585 | break; |
| 586 | } |
| 587 | lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg); |
| 588 | |
| 589 | data->fan_min[nr] = FAN_TO_REG(min, val); |
| 590 | lm87_write_value(client, LM87_REG_FAN_MIN(nr), |
| 591 | data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 592 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
| 594 | return count; |
| 595 | } |
| 596 | |
| 597 | #define set_fan(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 598 | static ssize_t set_fan##offset##_min(struct device *dev, \ |
| 599 | struct device_attribute *attr, \ |
| 600 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 602 | return set_fan_min(dev, buf, offset - 1, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 604 | static ssize_t set_fan##offset##_div(struct device *dev, \ |
| 605 | struct device_attribute *attr, \ |
| 606 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 608 | return set_fan_div(dev, buf, count, offset - 1); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } \ |
| 610 | static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 611 | show_fan##offset##_min, set_fan##offset##_min); \ |
| 612 | static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ |
| 613 | show_fan##offset##_div, set_fan##offset##_div); |
| 614 | set_fan(1); |
| 615 | set_fan(2); |
| 616 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 617 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, |
| 618 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { |
| 620 | struct lm87_data *data = lm87_update_device(dev); |
| 621 | return sprintf(buf, "%d\n", data->alarms); |
| 622 | } |
| 623 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 624 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 625 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, |
| 626 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
| 628 | struct lm87_data *data = lm87_update_device(dev); |
| 629 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 630 | } |
| 631 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 632 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 633 | static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, |
| 634 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
Jean Delvare | 90d6619 | 2007-10-08 18:24:35 +0200 | [diff] [blame] | 636 | struct lm87_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | return sprintf(buf, "%d\n", data->vrm); |
| 638 | } |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 639 | static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, |
| 640 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
Jean Delvare | 8f74efe | 2007-12-01 11:25:33 +0100 | [diff] [blame] | 642 | struct lm87_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 643 | unsigned long val; |
| 644 | int err; |
| 645 | |
| 646 | err = kstrtoul(buf, 10, &val); |
| 647 | if (err) |
| 648 | return err; |
| 649 | data->vrm = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | return count; |
| 651 | } |
| 652 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm); |
| 653 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 654 | static ssize_t show_aout(struct device *dev, struct device_attribute *attr, |
| 655 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | struct lm87_data *data = lm87_update_device(dev); |
| 658 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); |
| 659 | } |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 660 | static ssize_t set_aout(struct device *dev, struct device_attribute *attr, |
| 661 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
| 663 | struct i2c_client *client = to_i2c_client(dev); |
| 664 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 665 | long val; |
| 666 | int err; |
| 667 | |
| 668 | err = kstrtol(buf, 10, &val); |
| 669 | if (err) |
| 670 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 672 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | data->aout = AOUT_TO_REG(val); |
| 674 | lm87_write_value(client, LM87_REG_AOUT, data->aout); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 675 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | return count; |
| 677 | } |
| 678 | static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout); |
| 679 | |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 680 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 681 | char *buf) |
| 682 | { |
| 683 | struct lm87_data *data = lm87_update_device(dev); |
| 684 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 685 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 686 | } |
| 687 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 688 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 689 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 690 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 691 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 692 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); |
| 693 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 694 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 695 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 696 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 697 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 698 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 699 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 700 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14); |
| 701 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15); |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | /* |
| 704 | * Real code |
| 705 | */ |
| 706 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 707 | static struct attribute *lm87_attributes[] = { |
| 708 | &dev_attr_in1_input.attr, |
| 709 | &dev_attr_in1_min.attr, |
| 710 | &dev_attr_in1_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 711 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 712 | &dev_attr_in2_input.attr, |
| 713 | &dev_attr_in2_min.attr, |
| 714 | &dev_attr_in2_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 715 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 716 | &dev_attr_in3_input.attr, |
| 717 | &dev_attr_in3_min.attr, |
| 718 | &dev_attr_in3_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 719 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 720 | &dev_attr_in4_input.attr, |
| 721 | &dev_attr_in4_min.attr, |
| 722 | &dev_attr_in4_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 723 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 724 | |
| 725 | &dev_attr_temp1_input.attr, |
| 726 | &dev_attr_temp1_max.attr, |
| 727 | &dev_attr_temp1_min.attr, |
| 728 | &dev_attr_temp1_crit.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 729 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 730 | &dev_attr_temp2_input.attr, |
| 731 | &dev_attr_temp2_max.attr, |
| 732 | &dev_attr_temp2_min.attr, |
| 733 | &dev_attr_temp2_crit.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 734 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 735 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 736 | |
| 737 | &dev_attr_alarms.attr, |
| 738 | &dev_attr_aout_output.attr, |
| 739 | |
| 740 | NULL |
| 741 | }; |
| 742 | |
| 743 | static const struct attribute_group lm87_group = { |
| 744 | .attrs = lm87_attributes, |
| 745 | }; |
| 746 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 747 | static struct attribute *lm87_attributes_in6[] = { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 748 | &dev_attr_in6_input.attr, |
| 749 | &dev_attr_in6_min.attr, |
| 750 | &dev_attr_in6_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 751 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 752 | NULL |
| 753 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 754 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 755 | static const struct attribute_group lm87_group_in6 = { |
| 756 | .attrs = lm87_attributes_in6, |
| 757 | }; |
| 758 | |
| 759 | static struct attribute *lm87_attributes_fan1[] = { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 760 | &dev_attr_fan1_input.attr, |
| 761 | &dev_attr_fan1_min.attr, |
| 762 | &dev_attr_fan1_div.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 763 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 764 | NULL |
| 765 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 766 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 767 | static const struct attribute_group lm87_group_fan1 = { |
| 768 | .attrs = lm87_attributes_fan1, |
| 769 | }; |
| 770 | |
| 771 | static struct attribute *lm87_attributes_in7[] = { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 772 | &dev_attr_in7_input.attr, |
| 773 | &dev_attr_in7_min.attr, |
| 774 | &dev_attr_in7_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 775 | &sensor_dev_attr_in7_alarm.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 776 | NULL |
| 777 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 778 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 779 | static const struct attribute_group lm87_group_in7 = { |
| 780 | .attrs = lm87_attributes_in7, |
| 781 | }; |
| 782 | |
| 783 | static struct attribute *lm87_attributes_fan2[] = { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 784 | &dev_attr_fan2_input.attr, |
| 785 | &dev_attr_fan2_min.attr, |
| 786 | &dev_attr_fan2_div.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 787 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 788 | NULL |
| 789 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 790 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 791 | static const struct attribute_group lm87_group_fan2 = { |
| 792 | .attrs = lm87_attributes_fan2, |
| 793 | }; |
| 794 | |
| 795 | static struct attribute *lm87_attributes_temp3[] = { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 796 | &dev_attr_temp3_input.attr, |
| 797 | &dev_attr_temp3_max.attr, |
| 798 | &dev_attr_temp3_min.attr, |
| 799 | &dev_attr_temp3_crit.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 800 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 801 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 802 | NULL |
| 803 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 804 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 805 | static const struct attribute_group lm87_group_temp3 = { |
| 806 | .attrs = lm87_attributes_temp3, |
| 807 | }; |
| 808 | |
| 809 | static struct attribute *lm87_attributes_in0_5[] = { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 810 | &dev_attr_in0_input.attr, |
| 811 | &dev_attr_in0_min.attr, |
| 812 | &dev_attr_in0_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 813 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 814 | &dev_attr_in5_input.attr, |
| 815 | &dev_attr_in5_min.attr, |
| 816 | &dev_attr_in5_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 817 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 818 | NULL |
| 819 | }; |
| 820 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 821 | static const struct attribute_group lm87_group_in0_5 = { |
| 822 | .attrs = lm87_attributes_in0_5, |
| 823 | }; |
| 824 | |
| 825 | static struct attribute *lm87_attributes_vid[] = { |
| 826 | &dev_attr_cpu0_vid.attr, |
| 827 | &dev_attr_vrm.attr, |
| 828 | NULL |
| 829 | }; |
| 830 | |
| 831 | static const struct attribute_group lm87_group_vid = { |
| 832 | .attrs = lm87_attributes_vid, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 833 | }; |
| 834 | |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 835 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 836 | static int lm87_detect(struct i2c_client *client, struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | { |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 838 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 839 | const char *name; |
| 840 | u8 cid, rev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
| 842 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 843 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 845 | if (lm87_read_value(client, LM87_REG_CONFIG) & 0x80) |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 846 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
| 848 | /* Now, we do the remaining detection. */ |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 849 | cid = lm87_read_value(client, LM87_REG_COMPANY_ID); |
| 850 | rev = lm87_read_value(client, LM87_REG_REVISION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 852 | if (cid == 0x02 /* National Semiconductor */ |
| 853 | && (rev >= 0x01 && rev <= 0x08)) |
| 854 | name = "lm87"; |
| 855 | else if (cid == 0x41 /* Analog Devices */ |
| 856 | && (rev & 0xf0) == 0x10) |
| 857 | name = "adm1024"; |
| 858 | else { |
| 859 | dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n", |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 860 | client->addr); |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 861 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 864 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 869 | static void lm87_remove_files(struct i2c_client *client) |
| 870 | { |
| 871 | struct device *dev = &client->dev; |
| 872 | |
| 873 | sysfs_remove_group(&dev->kobj, &lm87_group); |
| 874 | sysfs_remove_group(&dev->kobj, &lm87_group_in6); |
| 875 | sysfs_remove_group(&dev->kobj, &lm87_group_fan1); |
| 876 | sysfs_remove_group(&dev->kobj, &lm87_group_in7); |
| 877 | sysfs_remove_group(&dev->kobj, &lm87_group_fan2); |
| 878 | sysfs_remove_group(&dev->kobj, &lm87_group_temp3); |
| 879 | sysfs_remove_group(&dev->kobj, &lm87_group_in0_5); |
| 880 | sysfs_remove_group(&dev->kobj, &lm87_group_vid); |
| 881 | } |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | static void lm87_init_client(struct i2c_client *client) |
| 884 | { |
| 885 | struct lm87_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Ben Hutchings | 47064d6 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 887 | if (client->dev.platform_data) { |
| 888 | data->channel = *(u8 *)client->dev.platform_data; |
| 889 | lm87_write_value(client, |
| 890 | LM87_REG_CHANNEL_MODE, data->channel); |
| 891 | } else { |
| 892 | data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE); |
| 893 | } |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 894 | data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 896 | if (!(data->config & 0x01)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | int i; |
| 898 | |
| 899 | /* Limits are left uninitialized after power-up */ |
| 900 | for (i = 1; i < 6; i++) { |
| 901 | lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00); |
| 902 | lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF); |
| 903 | } |
| 904 | for (i = 0; i < 2; i++) { |
| 905 | lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F); |
| 906 | lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00); |
| 907 | lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00); |
| 908 | lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF); |
| 909 | } |
| 910 | if (data->channel & CHAN_TEMP3) { |
| 911 | lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F); |
| 912 | lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00); |
| 913 | } else { |
| 914 | lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00); |
| 915 | lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF); |
| 916 | } |
| 917 | } |
Ben Hutchings | 49ae6cc | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 918 | |
| 919 | /* Make sure Start is set and INT#_Clear is clear */ |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 920 | if ((data->config & 0x09) != 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | lm87_write_value(client, LM87_REG_CONFIG, |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 922 | (data->config & 0x77) | 0x01); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 925 | static int lm87_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 926 | { |
| 927 | struct lm87_data *data; |
| 928 | int err; |
| 929 | |
| 930 | data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL); |
| 931 | if (!data) { |
| 932 | err = -ENOMEM; |
| 933 | goto exit; |
| 934 | } |
| 935 | |
| 936 | i2c_set_clientdata(client, data); |
| 937 | data->valid = 0; |
| 938 | mutex_init(&data->update_lock); |
| 939 | |
| 940 | /* Initialize the LM87 chip */ |
| 941 | lm87_init_client(client); |
| 942 | |
| 943 | data->in_scale[0] = 2500; |
| 944 | data->in_scale[1] = 2700; |
| 945 | data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300; |
| 946 | data->in_scale[3] = 5000; |
| 947 | data->in_scale[4] = 12000; |
| 948 | data->in_scale[5] = 2700; |
| 949 | data->in_scale[6] = 1875; |
| 950 | data->in_scale[7] = 1875; |
| 951 | |
| 952 | /* Register sysfs hooks */ |
| 953 | err = sysfs_create_group(&client->dev.kobj, &lm87_group); |
| 954 | if (err) |
| 955 | goto exit_free; |
| 956 | |
| 957 | if (data->channel & CHAN_NO_FAN(0)) { |
| 958 | err = sysfs_create_group(&client->dev.kobj, &lm87_group_in6); |
| 959 | if (err) |
| 960 | goto exit_remove; |
| 961 | } else { |
| 962 | err = sysfs_create_group(&client->dev.kobj, &lm87_group_fan1); |
| 963 | if (err) |
| 964 | goto exit_remove; |
| 965 | } |
| 966 | |
| 967 | if (data->channel & CHAN_NO_FAN(1)) { |
| 968 | err = sysfs_create_group(&client->dev.kobj, &lm87_group_in7); |
| 969 | if (err) |
| 970 | goto exit_remove; |
| 971 | } else { |
| 972 | err = sysfs_create_group(&client->dev.kobj, &lm87_group_fan2); |
| 973 | if (err) |
| 974 | goto exit_remove; |
| 975 | } |
| 976 | |
| 977 | if (data->channel & CHAN_TEMP3) { |
| 978 | err = sysfs_create_group(&client->dev.kobj, &lm87_group_temp3); |
| 979 | if (err) |
| 980 | goto exit_remove; |
| 981 | } else { |
| 982 | err = sysfs_create_group(&client->dev.kobj, &lm87_group_in0_5); |
| 983 | if (err) |
| 984 | goto exit_remove; |
| 985 | } |
| 986 | |
| 987 | if (!(data->channel & CHAN_NO_VID)) { |
| 988 | data->vrm = vid_which_vrm(); |
| 989 | err = sysfs_create_group(&client->dev.kobj, &lm87_group_vid); |
| 990 | if (err) |
| 991 | goto exit_remove; |
| 992 | } |
| 993 | |
| 994 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 995 | if (IS_ERR(data->hwmon_dev)) { |
| 996 | err = PTR_ERR(data->hwmon_dev); |
| 997 | goto exit_remove; |
| 998 | } |
| 999 | |
| 1000 | return 0; |
| 1001 | |
| 1002 | exit_remove: |
| 1003 | lm87_remove_files(client); |
| 1004 | exit_free: |
| 1005 | lm87_write_value(client, LM87_REG_CONFIG, data->config); |
| 1006 | kfree(data); |
| 1007 | exit: |
| 1008 | return err; |
| 1009 | } |
| 1010 | |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 1011 | static int lm87_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1013 | struct lm87_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1015 | hwmon_device_unregister(data->hwmon_dev); |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 1016 | lm87_remove_files(client); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1017 | |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 1018 | lm87_write_value(client, LM87_REG_CONFIG, data->config); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1019 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | return 0; |
| 1021 | } |
| 1022 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 1023 | /* |
| 1024 | * Driver data (common to all clients) |
| 1025 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 1027 | static const struct i2c_device_id lm87_id[] = { |
| 1028 | { "lm87", lm87 }, |
| 1029 | { "adm1024", adm1024 }, |
| 1030 | { } |
| 1031 | }; |
| 1032 | MODULE_DEVICE_TABLE(i2c, lm87_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame^] | 1034 | static struct i2c_driver lm87_driver = { |
| 1035 | .class = I2C_CLASS_HWMON, |
| 1036 | .driver = { |
| 1037 | .name = "lm87", |
| 1038 | }, |
| 1039 | .probe = lm87_probe, |
| 1040 | .remove = lm87_remove, |
| 1041 | .id_table = lm87_id, |
| 1042 | .detect = lm87_detect, |
| 1043 | .address_list = normal_i2c, |
| 1044 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 1046 | module_i2c_driver(lm87_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
| 1048 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others"); |
| 1049 | MODULE_DESCRIPTION("LM87 driver"); |
| 1050 | MODULE_LICENSE("GPL"); |