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 | /* |
| 153 | * Functions declaration |
| 154 | */ |
| 155 | |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 156 | static int lm87_probe(struct i2c_client *client, |
| 157 | const struct i2c_device_id *id); |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 158 | static int lm87_detect(struct i2c_client *new_client, |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 159 | struct i2c_board_info *info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | static void lm87_init_client(struct i2c_client *client); |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 161 | static int lm87_remove(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | static struct lm87_data *lm87_update_device(struct device *dev); |
| 163 | |
| 164 | /* |
| 165 | * Driver data (common to all clients) |
| 166 | */ |
| 167 | |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 168 | static const struct i2c_device_id lm87_id[] = { |
| 169 | { "lm87", lm87 }, |
| 170 | { "adm1024", adm1024 }, |
| 171 | { } |
| 172 | }; |
| 173 | MODULE_DEVICE_TABLE(i2c, lm87_id); |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | static struct i2c_driver lm87_driver = { |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 176 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 177 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 178 | .name = "lm87", |
| 179 | }, |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 180 | .probe = lm87_probe, |
| 181 | .remove = lm87_remove, |
| 182 | .id_table = lm87_id, |
| 183 | .detect = lm87_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 184 | .address_list = normal_i2c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /* |
| 188 | * Client data (each client gets its own) |
| 189 | */ |
| 190 | |
| 191 | struct lm87_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 192 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 193 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | char valid; /* zero until following fields are valid */ |
| 195 | unsigned long last_updated; /* In jiffies */ |
| 196 | |
| 197 | u8 channel; /* register value */ |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 198 | u8 config; /* original register value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | u8 in[8]; /* register value */ |
| 201 | u8 in_max[8]; /* register value */ |
| 202 | u8 in_min[8]; /* register value */ |
| 203 | u16 in_scale[8]; |
| 204 | |
| 205 | s8 temp[3]; /* register value */ |
| 206 | s8 temp_high[3]; /* register value */ |
| 207 | s8 temp_low[3]; /* register value */ |
| 208 | s8 temp_crit_int; /* min of two register values */ |
| 209 | s8 temp_crit_ext; /* min of two register values */ |
| 210 | |
| 211 | u8 fan[2]; /* register value */ |
| 212 | u8 fan_min[2]; /* register value */ |
| 213 | u8 fan_div[2]; /* register value, shifted right */ |
| 214 | u8 aout; /* register value */ |
| 215 | |
| 216 | u16 alarms; /* register values, combined */ |
| 217 | u8 vid; /* register values, combined */ |
| 218 | u8 vrm; |
| 219 | }; |
| 220 | |
| 221 | /* |
| 222 | * Sysfs stuff |
| 223 | */ |
| 224 | |
| 225 | static inline int lm87_read_value(struct i2c_client *client, u8 reg) |
| 226 | { |
| 227 | return i2c_smbus_read_byte_data(client, reg); |
| 228 | } |
| 229 | |
| 230 | static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 231 | { |
| 232 | return i2c_smbus_write_byte_data(client, reg, value); |
| 233 | } |
| 234 | |
| 235 | #define show_in(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 236 | static ssize_t show_in##offset##_input(struct device *dev, \ |
| 237 | struct device_attribute *attr, \ |
| 238 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { \ |
| 240 | struct lm87_data *data = lm87_update_device(dev); \ |
| 241 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \ |
| 242 | data->in_scale[offset])); \ |
| 243 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 244 | static ssize_t show_in##offset##_min(struct device *dev, \ |
| 245 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { \ |
| 247 | struct lm87_data *data = lm87_update_device(dev); \ |
| 248 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \ |
| 249 | data->in_scale[offset])); \ |
| 250 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 251 | static ssize_t show_in##offset##_max(struct device *dev, \ |
| 252 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { \ |
| 254 | struct lm87_data *data = lm87_update_device(dev); \ |
| 255 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \ |
| 256 | data->in_scale[offset])); \ |
| 257 | } \ |
| 258 | static DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 259 | show_in##offset##_input, NULL); |
| 260 | show_in(0); |
| 261 | show_in(1); |
| 262 | show_in(2); |
| 263 | show_in(3); |
| 264 | show_in(4); |
| 265 | show_in(5); |
| 266 | show_in(6); |
| 267 | show_in(7); |
| 268 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 269 | static ssize_t set_in_min(struct device *dev, const char *buf, int nr, |
| 270 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | struct i2c_client *client = to_i2c_client(dev); |
| 273 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 274 | long val; |
| 275 | int err; |
| 276 | |
| 277 | err = kstrtol(buf, 10, &val); |
| 278 | if (err) |
| 279 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 281 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 283 | lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) : |
| 284 | LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 285 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 286 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 289 | static ssize_t set_in_max(struct device *dev, const char *buf, int nr, |
| 290 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | struct i2c_client *client = to_i2c_client(dev); |
| 293 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 294 | long val; |
| 295 | int err; |
| 296 | |
| 297 | err = kstrtol(buf, 10, &val); |
| 298 | if (err) |
| 299 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 301 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 303 | lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) : |
| 304 | LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 305 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 306 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | #define set_in(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 310 | static ssize_t set_in##offset##_min(struct device *dev, \ |
| 311 | struct device_attribute *attr, \ |
| 312 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 314 | return set_in_min(dev, buf, offset, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 316 | static ssize_t set_in##offset##_max(struct device *dev, \ |
| 317 | struct device_attribute *attr, \ |
| 318 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 320 | return set_in_max(dev, buf, offset, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } \ |
| 322 | static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 323 | show_in##offset##_min, set_in##offset##_min); \ |
| 324 | static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 325 | show_in##offset##_max, set_in##offset##_max); |
| 326 | set_in(0); |
| 327 | set_in(1); |
| 328 | set_in(2); |
| 329 | set_in(3); |
| 330 | set_in(4); |
| 331 | set_in(5); |
| 332 | set_in(6); |
| 333 | set_in(7); |
| 334 | |
| 335 | #define show_temp(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 336 | static ssize_t show_temp##offset##_input(struct device *dev, \ |
| 337 | struct device_attribute *attr, \ |
| 338 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { \ |
| 340 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 341 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset - 1])); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 343 | static ssize_t show_temp##offset##_low(struct device *dev, \ |
| 344 | struct device_attribute *attr, \ |
| 345 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { \ |
| 347 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 348 | return sprintf(buf, "%d\n", \ |
| 349 | TEMP_FROM_REG(data->temp_low[offset - 1])); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 351 | static ssize_t show_temp##offset##_high(struct device *dev, \ |
| 352 | struct device_attribute *attr, \ |
| 353 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { \ |
| 355 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 356 | return sprintf(buf, "%d\n", \ |
| 357 | TEMP_FROM_REG(data->temp_high[offset - 1])); \ |
| 358 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
| 360 | show_temp##offset##_input, NULL); |
| 361 | show_temp(1); |
| 362 | show_temp(2); |
| 363 | show_temp(3); |
| 364 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 365 | static ssize_t set_temp_low(struct device *dev, const char *buf, int nr, |
| 366 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
| 368 | struct i2c_client *client = to_i2c_client(dev); |
| 369 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 370 | long val; |
| 371 | int err; |
| 372 | |
| 373 | err = kstrtol(buf, 10, &val); |
| 374 | if (err) |
| 375 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 377 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | data->temp_low[nr] = TEMP_TO_REG(val); |
| 379 | 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] | 380 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 381 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 384 | static ssize_t set_temp_high(struct device *dev, const char *buf, int nr, |
| 385 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | struct i2c_client *client = to_i2c_client(dev); |
| 388 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 389 | long val; |
| 390 | int err; |
| 391 | |
| 392 | err = kstrtol(buf, 10, &val); |
| 393 | if (err) |
| 394 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 396 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | data->temp_high[nr] = TEMP_TO_REG(val); |
| 398 | 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] | 399 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 400 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | #define set_temp(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 404 | static ssize_t set_temp##offset##_low(struct device *dev, \ |
| 405 | struct device_attribute *attr, \ |
| 406 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 408 | return set_temp_low(dev, buf, offset - 1, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 410 | static ssize_t set_temp##offset##_high(struct device *dev, \ |
| 411 | struct device_attribute *attr, \ |
| 412 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 414 | return set_temp_high(dev, buf, offset - 1, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } \ |
| 416 | static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ |
| 417 | show_temp##offset##_high, set_temp##offset##_high); \ |
| 418 | static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ |
| 419 | show_temp##offset##_low, set_temp##offset##_low); |
| 420 | set_temp(1); |
| 421 | set_temp(2); |
| 422 | set_temp(3); |
| 423 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 424 | static ssize_t show_temp_crit_int(struct device *dev, |
| 425 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | struct lm87_data *data = lm87_update_device(dev); |
| 428 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int)); |
| 429 | } |
| 430 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 431 | static ssize_t show_temp_crit_ext(struct device *dev, |
| 432 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
| 434 | struct lm87_data *data = lm87_update_device(dev); |
| 435 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext)); |
| 436 | } |
| 437 | |
| 438 | static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL); |
| 439 | static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL); |
| 440 | static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL); |
| 441 | |
| 442 | #define show_fan(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 443 | static ssize_t show_fan##offset##_input(struct device *dev, \ |
| 444 | struct device_attribute *attr, \ |
| 445 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { \ |
| 447 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 448 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset - 1], \ |
| 449 | FAN_DIV_FROM_REG(data->fan_div[offset - 1]))); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 451 | static ssize_t show_fan##offset##_min(struct device *dev, \ |
| 452 | struct device_attribute *attr, \ |
| 453 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { \ |
| 455 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 456 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset - 1], \ |
| 457 | FAN_DIV_FROM_REG(data->fan_div[offset - 1]))); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 459 | static ssize_t show_fan##offset##_div(struct device *dev, \ |
| 460 | struct device_attribute *attr, \ |
| 461 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { \ |
| 463 | struct lm87_data *data = lm87_update_device(dev); \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 464 | return sprintf(buf, "%d\n", \ |
| 465 | FAN_DIV_FROM_REG(data->fan_div[offset - 1])); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } \ |
| 467 | static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
| 468 | show_fan##offset##_input, NULL); |
| 469 | show_fan(1); |
| 470 | show_fan(2); |
| 471 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 472 | static ssize_t set_fan_min(struct device *dev, const char *buf, int nr, |
| 473 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
| 475 | struct i2c_client *client = to_i2c_client(dev); |
| 476 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 477 | long val; |
| 478 | int err; |
| 479 | |
| 480 | err = kstrtol(buf, 10, &val); |
| 481 | if (err) |
| 482 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 484 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | data->fan_min[nr] = FAN_TO_REG(val, |
| 486 | FAN_DIV_FROM_REG(data->fan_div[nr])); |
| 487 | 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] | 488 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 489 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 492 | /* |
| 493 | * Note: we save and restore the fan minimum here, because its value is |
| 494 | * determined in part by the fan clock divider. This follows the principle |
| 495 | * of least surprise; the user doesn't expect the fan minimum to change just |
| 496 | * because the divider changed. |
| 497 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | static ssize_t set_fan_div(struct device *dev, const char *buf, |
| 499 | size_t count, int nr) |
| 500 | { |
| 501 | struct i2c_client *client = to_i2c_client(dev); |
| 502 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 503 | long val; |
| 504 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | unsigned long min; |
| 506 | u8 reg; |
| 507 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 508 | err = kstrtol(buf, 10, &val); |
| 509 | if (err) |
| 510 | return err; |
| 511 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 512 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | min = FAN_FROM_REG(data->fan_min[nr], |
| 514 | FAN_DIV_FROM_REG(data->fan_div[nr])); |
| 515 | |
| 516 | switch (val) { |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 517 | case 1: |
| 518 | data->fan_div[nr] = 0; |
| 519 | break; |
| 520 | case 2: |
| 521 | data->fan_div[nr] = 1; |
| 522 | break; |
| 523 | case 4: |
| 524 | data->fan_div[nr] = 2; |
| 525 | break; |
| 526 | case 8: |
| 527 | data->fan_div[nr] = 3; |
| 528 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | default: |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 530 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | return -EINVAL; |
| 532 | } |
| 533 | |
| 534 | reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV); |
| 535 | switch (nr) { |
| 536 | case 0: |
| 537 | reg = (reg & 0xCF) | (data->fan_div[0] << 4); |
| 538 | break; |
| 539 | case 1: |
| 540 | reg = (reg & 0x3F) | (data->fan_div[1] << 6); |
| 541 | break; |
| 542 | } |
| 543 | lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg); |
| 544 | |
| 545 | data->fan_min[nr] = FAN_TO_REG(min, val); |
| 546 | lm87_write_value(client, LM87_REG_FAN_MIN(nr), |
| 547 | data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 548 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | return count; |
| 551 | } |
| 552 | |
| 553 | #define set_fan(offset) \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 554 | static ssize_t set_fan##offset##_min(struct device *dev, \ |
| 555 | struct device_attribute *attr, \ |
| 556 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 558 | return set_fan_min(dev, buf, offset - 1, count); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 560 | static ssize_t set_fan##offset##_div(struct device *dev, \ |
| 561 | struct device_attribute *attr, \ |
| 562 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 564 | return set_fan_div(dev, buf, count, offset - 1); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } \ |
| 566 | static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 567 | show_fan##offset##_min, set_fan##offset##_min); \ |
| 568 | static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ |
| 569 | show_fan##offset##_div, set_fan##offset##_div); |
| 570 | set_fan(1); |
| 571 | set_fan(2); |
| 572 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 573 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, |
| 574 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { |
| 576 | struct lm87_data *data = lm87_update_device(dev); |
| 577 | return sprintf(buf, "%d\n", data->alarms); |
| 578 | } |
| 579 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 580 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 581 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, |
| 582 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { |
| 584 | struct lm87_data *data = lm87_update_device(dev); |
| 585 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 586 | } |
| 587 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 588 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 589 | static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, |
| 590 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
Jean Delvare | 90d6619 | 2007-10-08 18:24:35 +0200 | [diff] [blame] | 592 | struct lm87_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | return sprintf(buf, "%d\n", data->vrm); |
| 594 | } |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 595 | static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, |
| 596 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { |
Jean Delvare | 8f74efe | 2007-12-01 11:25:33 +0100 | [diff] [blame] | 598 | struct lm87_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 599 | unsigned long val; |
| 600 | int err; |
| 601 | |
| 602 | err = kstrtoul(buf, 10, &val); |
| 603 | if (err) |
| 604 | return err; |
| 605 | data->vrm = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | return count; |
| 607 | } |
| 608 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm); |
| 609 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 610 | static ssize_t show_aout(struct device *dev, struct device_attribute *attr, |
| 611 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | { |
| 613 | struct lm87_data *data = lm87_update_device(dev); |
| 614 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); |
| 615 | } |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 616 | static ssize_t set_aout(struct device *dev, struct device_attribute *attr, |
| 617 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
| 619 | struct i2c_client *client = to_i2c_client(dev); |
| 620 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 621 | long val; |
| 622 | int err; |
| 623 | |
| 624 | err = kstrtol(buf, 10, &val); |
| 625 | if (err) |
| 626 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 628 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | data->aout = AOUT_TO_REG(val); |
| 630 | lm87_write_value(client, LM87_REG_AOUT, data->aout); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 631 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | return count; |
| 633 | } |
| 634 | static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout); |
| 635 | |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 636 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 637 | char *buf) |
| 638 | { |
| 639 | struct lm87_data *data = lm87_update_device(dev); |
| 640 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 641 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 642 | } |
| 643 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 644 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 645 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 646 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 647 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 648 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); |
| 649 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 650 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 651 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 652 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 653 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 654 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 655 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 656 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14); |
| 657 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15); |
| 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | /* |
| 660 | * Real code |
| 661 | */ |
| 662 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 663 | static struct attribute *lm87_attributes[] = { |
| 664 | &dev_attr_in1_input.attr, |
| 665 | &dev_attr_in1_min.attr, |
| 666 | &dev_attr_in1_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 667 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 668 | &dev_attr_in2_input.attr, |
| 669 | &dev_attr_in2_min.attr, |
| 670 | &dev_attr_in2_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 671 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 672 | &dev_attr_in3_input.attr, |
| 673 | &dev_attr_in3_min.attr, |
| 674 | &dev_attr_in3_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 675 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 676 | &dev_attr_in4_input.attr, |
| 677 | &dev_attr_in4_min.attr, |
| 678 | &dev_attr_in4_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 679 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 680 | |
| 681 | &dev_attr_temp1_input.attr, |
| 682 | &dev_attr_temp1_max.attr, |
| 683 | &dev_attr_temp1_min.attr, |
| 684 | &dev_attr_temp1_crit.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 685 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 686 | &dev_attr_temp2_input.attr, |
| 687 | &dev_attr_temp2_max.attr, |
| 688 | &dev_attr_temp2_min.attr, |
| 689 | &dev_attr_temp2_crit.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 690 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 691 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 692 | |
| 693 | &dev_attr_alarms.attr, |
| 694 | &dev_attr_aout_output.attr, |
| 695 | |
| 696 | NULL |
| 697 | }; |
| 698 | |
| 699 | static const struct attribute_group lm87_group = { |
| 700 | .attrs = lm87_attributes, |
| 701 | }; |
| 702 | |
| 703 | static struct attribute *lm87_attributes_opt[] = { |
| 704 | &dev_attr_in6_input.attr, |
| 705 | &dev_attr_in6_min.attr, |
| 706 | &dev_attr_in6_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 707 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 708 | |
| 709 | &dev_attr_fan1_input.attr, |
| 710 | &dev_attr_fan1_min.attr, |
| 711 | &dev_attr_fan1_div.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 712 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 713 | |
| 714 | &dev_attr_in7_input.attr, |
| 715 | &dev_attr_in7_min.attr, |
| 716 | &dev_attr_in7_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 717 | &sensor_dev_attr_in7_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 718 | |
| 719 | &dev_attr_fan2_input.attr, |
| 720 | &dev_attr_fan2_min.attr, |
| 721 | &dev_attr_fan2_div.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 722 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 723 | |
| 724 | &dev_attr_temp3_input.attr, |
| 725 | &dev_attr_temp3_max.attr, |
| 726 | &dev_attr_temp3_min.attr, |
| 727 | &dev_attr_temp3_crit.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 728 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 729 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 730 | |
| 731 | &dev_attr_in0_input.attr, |
| 732 | &dev_attr_in0_min.attr, |
| 733 | &dev_attr_in0_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 734 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 735 | &dev_attr_in5_input.attr, |
| 736 | &dev_attr_in5_min.attr, |
| 737 | &dev_attr_in5_max.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 738 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 739 | |
| 740 | &dev_attr_cpu0_vid.attr, |
| 741 | &dev_attr_vrm.attr, |
| 742 | |
| 743 | NULL |
| 744 | }; |
| 745 | |
| 746 | static const struct attribute_group lm87_group_opt = { |
| 747 | .attrs = lm87_attributes_opt, |
| 748 | }; |
| 749 | |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 750 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 751 | static int lm87_detect(struct i2c_client *new_client, |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 752 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | { |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 754 | struct i2c_adapter *adapter = new_client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 755 | const char *name; |
| 756 | u8 cid, rev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 759 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 761 | if (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80) |
| 762 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
| 764 | /* Now, we do the remaining detection. */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 765 | cid = lm87_read_value(new_client, LM87_REG_COMPANY_ID); |
| 766 | rev = lm87_read_value(new_client, LM87_REG_REVISION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 768 | if (cid == 0x02 /* National Semiconductor */ |
| 769 | && (rev >= 0x01 && rev <= 0x08)) |
| 770 | name = "lm87"; |
| 771 | else if (cid == 0x41 /* Analog Devices */ |
| 772 | && (rev & 0xf0) == 0x10) |
| 773 | name = "adm1024"; |
| 774 | else { |
| 775 | dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n", |
| 776 | new_client->addr); |
| 777 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 780 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static int lm87_probe(struct i2c_client *new_client, |
| 786 | const struct i2c_device_id *id) |
| 787 | { |
| 788 | struct lm87_data *data; |
| 789 | int err; |
| 790 | |
| 791 | data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL); |
| 792 | if (!data) { |
| 793 | err = -ENOMEM; |
| 794 | goto exit; |
| 795 | } |
| 796 | |
| 797 | i2c_set_clientdata(new_client, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | data->valid = 0; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 799 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | /* Initialize the LM87 chip */ |
| 802 | lm87_init_client(new_client); |
| 803 | |
| 804 | data->in_scale[0] = 2500; |
| 805 | data->in_scale[1] = 2700; |
| 806 | data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300; |
| 807 | data->in_scale[3] = 5000; |
| 808 | data->in_scale[4] = 12000; |
| 809 | data->in_scale[5] = 2700; |
| 810 | data->in_scale[6] = 1875; |
| 811 | data->in_scale[7] = 1875; |
| 812 | |
| 813 | /* Register sysfs hooks */ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame^] | 814 | err = sysfs_create_group(&new_client->dev.kobj, &lm87_group); |
| 815 | if (err) |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 816 | goto exit_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | if (data->channel & CHAN_NO_FAN(0)) { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 819 | if ((err = device_create_file(&new_client->dev, |
| 820 | &dev_attr_in6_input)) |
| 821 | || (err = device_create_file(&new_client->dev, |
| 822 | &dev_attr_in6_min)) |
| 823 | || (err = device_create_file(&new_client->dev, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 824 | &dev_attr_in6_max)) |
| 825 | || (err = device_create_file(&new_client->dev, |
| 826 | &sensor_dev_attr_in6_alarm.dev_attr))) |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 827 | goto exit_remove; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } else { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 829 | if ((err = device_create_file(&new_client->dev, |
| 830 | &dev_attr_fan1_input)) |
| 831 | || (err = device_create_file(&new_client->dev, |
| 832 | &dev_attr_fan1_min)) |
| 833 | || (err = device_create_file(&new_client->dev, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 834 | &dev_attr_fan1_div)) |
| 835 | || (err = device_create_file(&new_client->dev, |
| 836 | &sensor_dev_attr_fan1_alarm.dev_attr))) |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 837 | goto exit_remove; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 840 | if (data->channel & CHAN_NO_FAN(1)) { |
| 841 | if ((err = device_create_file(&new_client->dev, |
| 842 | &dev_attr_in7_input)) |
| 843 | || (err = device_create_file(&new_client->dev, |
| 844 | &dev_attr_in7_min)) |
| 845 | || (err = device_create_file(&new_client->dev, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 846 | &dev_attr_in7_max)) |
| 847 | || (err = device_create_file(&new_client->dev, |
| 848 | &sensor_dev_attr_in7_alarm.dev_attr))) |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 849 | goto exit_remove; |
| 850 | } else { |
| 851 | if ((err = device_create_file(&new_client->dev, |
| 852 | &dev_attr_fan2_input)) |
| 853 | || (err = device_create_file(&new_client->dev, |
| 854 | &dev_attr_fan2_min)) |
| 855 | || (err = device_create_file(&new_client->dev, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 856 | &dev_attr_fan2_div)) |
| 857 | || (err = device_create_file(&new_client->dev, |
| 858 | &sensor_dev_attr_fan2_alarm.dev_attr))) |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 859 | goto exit_remove; |
| 860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
| 862 | if (data->channel & CHAN_TEMP3) { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 863 | if ((err = device_create_file(&new_client->dev, |
| 864 | &dev_attr_temp3_input)) |
| 865 | || (err = device_create_file(&new_client->dev, |
| 866 | &dev_attr_temp3_max)) |
| 867 | || (err = device_create_file(&new_client->dev, |
| 868 | &dev_attr_temp3_min)) |
| 869 | || (err = device_create_file(&new_client->dev, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 870 | &dev_attr_temp3_crit)) |
| 871 | || (err = device_create_file(&new_client->dev, |
| 872 | &sensor_dev_attr_temp3_alarm.dev_attr)) |
| 873 | || (err = device_create_file(&new_client->dev, |
| 874 | &sensor_dev_attr_temp3_fault.dev_attr))) |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 875 | goto exit_remove; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } else { |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 877 | if ((err = device_create_file(&new_client->dev, |
| 878 | &dev_attr_in0_input)) |
| 879 | || (err = device_create_file(&new_client->dev, |
| 880 | &dev_attr_in0_min)) |
| 881 | || (err = device_create_file(&new_client->dev, |
| 882 | &dev_attr_in0_max)) |
| 883 | || (err = device_create_file(&new_client->dev, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 884 | &sensor_dev_attr_in0_alarm.dev_attr)) |
| 885 | || (err = device_create_file(&new_client->dev, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 886 | &dev_attr_in5_input)) |
| 887 | || (err = device_create_file(&new_client->dev, |
| 888 | &dev_attr_in5_min)) |
| 889 | || (err = device_create_file(&new_client->dev, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 890 | &dev_attr_in5_max)) |
| 891 | || (err = device_create_file(&new_client->dev, |
| 892 | &sensor_dev_attr_in5_alarm.dev_attr))) |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 893 | goto exit_remove; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | if (!(data->channel & CHAN_NO_VID)) { |
Jean Delvare | 8a665a0 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 897 | data->vrm = vid_which_vrm(); |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 898 | if ((err = device_create_file(&new_client->dev, |
| 899 | &dev_attr_cpu0_vid)) |
| 900 | || (err = device_create_file(&new_client->dev, |
| 901 | &dev_attr_vrm))) |
| 902 | goto exit_remove; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 905 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 906 | if (IS_ERR(data->hwmon_dev)) { |
| 907 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 908 | goto exit_remove; |
| 909 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | return 0; |
| 912 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 913 | exit_remove: |
| 914 | sysfs_remove_group(&new_client->dev.kobj, &lm87_group); |
| 915 | sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | exit_free: |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 917 | lm87_write_value(new_client, LM87_REG_CONFIG, data->config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | kfree(data); |
| 919 | exit: |
| 920 | return err; |
| 921 | } |
| 922 | |
| 923 | static void lm87_init_client(struct i2c_client *client) |
| 924 | { |
| 925 | struct lm87_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
Ben Hutchings | 47064d6 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 927 | if (client->dev.platform_data) { |
| 928 | data->channel = *(u8 *)client->dev.platform_data; |
| 929 | lm87_write_value(client, |
| 930 | LM87_REG_CHANNEL_MODE, data->channel); |
| 931 | } else { |
| 932 | data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE); |
| 933 | } |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 934 | data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 936 | if (!(data->config & 0x01)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | int i; |
| 938 | |
| 939 | /* Limits are left uninitialized after power-up */ |
| 940 | for (i = 1; i < 6; i++) { |
| 941 | lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00); |
| 942 | lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF); |
| 943 | } |
| 944 | for (i = 0; i < 2; i++) { |
| 945 | lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F); |
| 946 | lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00); |
| 947 | lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00); |
| 948 | lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF); |
| 949 | } |
| 950 | if (data->channel & CHAN_TEMP3) { |
| 951 | lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F); |
| 952 | lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00); |
| 953 | } else { |
| 954 | lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00); |
| 955 | lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF); |
| 956 | } |
| 957 | } |
Ben Hutchings | 49ae6cc | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 958 | |
| 959 | /* Make sure Start is set and INT#_Clear is clear */ |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 960 | if ((data->config & 0x09) != 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | lm87_write_value(client, LM87_REG_CONFIG, |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 962 | (data->config & 0x77) | 0x01); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 965 | static int lm87_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 967 | struct lm87_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 969 | hwmon_device_unregister(data->hwmon_dev); |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 970 | sysfs_remove_group(&client->dev.kobj, &lm87_group); |
| 971 | sysfs_remove_group(&client->dev.kobj, &lm87_group_opt); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 972 | |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 973 | lm87_write_value(client, LM87_REG_CONFIG, data->config); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 974 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static struct lm87_data *lm87_update_device(struct device *dev) |
| 979 | { |
| 980 | struct i2c_client *client = to_i2c_client(dev); |
| 981 | struct lm87_data *data = i2c_get_clientdata(client); |
| 982 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 983 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
| 985 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 986 | int i, j; |
| 987 | |
| 988 | dev_dbg(&client->dev, "Updating data.\n"); |
| 989 | |
| 990 | i = (data->channel & CHAN_TEMP3) ? 1 : 0; |
| 991 | j = (data->channel & CHAN_TEMP3) ? 5 : 6; |
| 992 | for (; i < j; i++) { |
| 993 | data->in[i] = lm87_read_value(client, |
| 994 | LM87_REG_IN(i)); |
| 995 | data->in_min[i] = lm87_read_value(client, |
| 996 | LM87_REG_IN_MIN(i)); |
| 997 | data->in_max[i] = lm87_read_value(client, |
| 998 | LM87_REG_IN_MAX(i)); |
| 999 | } |
| 1000 | |
| 1001 | for (i = 0; i < 2; i++) { |
| 1002 | if (data->channel & CHAN_NO_FAN(i)) { |
| 1003 | data->in[6+i] = lm87_read_value(client, |
| 1004 | LM87_REG_AIN(i)); |
| 1005 | data->in_max[6+i] = lm87_read_value(client, |
| 1006 | LM87_REG_AIN_MAX(i)); |
| 1007 | data->in_min[6+i] = lm87_read_value(client, |
| 1008 | LM87_REG_AIN_MIN(i)); |
| 1009 | |
| 1010 | } else { |
| 1011 | data->fan[i] = lm87_read_value(client, |
| 1012 | LM87_REG_FAN(i)); |
| 1013 | data->fan_min[i] = lm87_read_value(client, |
| 1014 | LM87_REG_FAN_MIN(i)); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | j = (data->channel & CHAN_TEMP3) ? 3 : 2; |
| 1019 | for (i = 0 ; i < j; i++) { |
| 1020 | data->temp[i] = lm87_read_value(client, |
| 1021 | LM87_REG_TEMP[i]); |
| 1022 | data->temp_high[i] = lm87_read_value(client, |
| 1023 | LM87_REG_TEMP_HIGH[i]); |
| 1024 | data->temp_low[i] = lm87_read_value(client, |
| 1025 | LM87_REG_TEMP_LOW[i]); |
| 1026 | } |
| 1027 | |
| 1028 | i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK); |
| 1029 | j = lm87_read_value(client, LM87_REG_TEMP_HW_INT); |
| 1030 | data->temp_crit_int = min(i, j); |
| 1031 | |
| 1032 | i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK); |
| 1033 | j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT); |
| 1034 | data->temp_crit_ext = min(i, j); |
| 1035 | |
| 1036 | i = lm87_read_value(client, LM87_REG_VID_FAN_DIV); |
| 1037 | data->fan_div[0] = (i >> 4) & 0x03; |
| 1038 | data->fan_div[1] = (i >> 6) & 0x03; |
| 1039 | data->vid = (i & 0x0F) |
| 1040 | | (lm87_read_value(client, LM87_REG_VID4) & 0x01) |
| 1041 | << 4; |
| 1042 | |
| 1043 | data->alarms = lm87_read_value(client, LM87_REG_ALARMS1) |
| 1044 | | (lm87_read_value(client, LM87_REG_ALARMS2) |
| 1045 | << 8); |
| 1046 | data->aout = lm87_read_value(client, LM87_REG_AOUT); |
| 1047 | |
| 1048 | data->last_updated = jiffies; |
| 1049 | data->valid = 1; |
| 1050 | } |
| 1051 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1052 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | return data; |
| 1055 | } |
| 1056 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 1057 | module_i2c_driver(lm87_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
| 1059 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others"); |
| 1060 | MODULE_DESCRIPTION("LM87 driver"); |
| 1061 | MODULE_LICENSE("GPL"); |