Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | monitoring |
| 4 | Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>, |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 5 | Kyösti Mälkki <kmalkki@cc.hut.fi> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net> |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
Jean Delvare | 0cacdf2 | 2005-07-29 12:15:12 -0700 | [diff] [blame] | 27 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 29 | #include <linux/hwmon.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 30 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 31 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 33 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* Type of the extra sensor */ |
| 36 | static unsigned short extra_sensor_type; |
| 37 | module_param(extra_sensor_type, ushort, 0); |
| 38 | MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)"); |
| 39 | |
| 40 | /* Addresses to scan */ |
| 41 | static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 44 | I2C_CLIENT_INSMOD_1(gl520sm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 46 | /* Many GL520 constants specified below |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | One of the inputs can be configured as either temp or voltage. |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 48 | That's why _TEMP2 and _IN4 access the same register |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | /* The GL520 registers */ |
| 52 | #define GL520_REG_CHIP_ID 0x00 |
| 53 | #define GL520_REG_REVISION 0x01 |
| 54 | #define GL520_REG_CONF 0x03 |
| 55 | #define GL520_REG_MASK 0x11 |
| 56 | |
| 57 | #define GL520_REG_VID_INPUT 0x02 |
| 58 | |
Jean Delvare | 8b4b0ab | 2007-11-04 23:44:52 +0100 | [diff] [blame^] | 59 | static const u8 GL520_REG_IN_INPUT[] = { 0x15, 0x14, 0x13, 0x0d, 0x0e }; |
| 60 | static const u8 GL520_REG_IN_LIMIT[] = { 0x0c, 0x09, 0x0a, 0x0b }; |
| 61 | static const u8 GL520_REG_IN_MIN[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x18 }; |
| 62 | static const u8 GL520_REG_IN_MAX[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x17 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Jean Delvare | 8b4b0ab | 2007-11-04 23:44:52 +0100 | [diff] [blame^] | 64 | static const u8 GL520_REG_TEMP_INPUT[] = { 0x04, 0x0e }; |
| 65 | static const u8 GL520_REG_TEMP_MAX[] = { 0x05, 0x17 }; |
| 66 | static const u8 GL520_REG_TEMP_MAX_HYST[] = { 0x06, 0x18 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | #define GL520_REG_FAN_INPUT 0x07 |
| 69 | #define GL520_REG_FAN_MIN 0x08 |
| 70 | #define GL520_REG_FAN_DIV 0x0f |
| 71 | #define GL520_REG_FAN_OFF GL520_REG_FAN_DIV |
| 72 | |
| 73 | #define GL520_REG_ALARMS 0x12 |
| 74 | #define GL520_REG_BEEP_MASK 0x10 |
| 75 | #define GL520_REG_BEEP_ENABLE GL520_REG_CONF |
| 76 | |
| 77 | /* |
| 78 | * Function declarations |
| 79 | */ |
| 80 | |
| 81 | static int gl520_attach_adapter(struct i2c_adapter *adapter); |
| 82 | static int gl520_detect(struct i2c_adapter *adapter, int address, int kind); |
| 83 | static void gl520_init_client(struct i2c_client *client); |
| 84 | static int gl520_detach_client(struct i2c_client *client); |
| 85 | static int gl520_read_value(struct i2c_client *client, u8 reg); |
| 86 | static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value); |
| 87 | static struct gl520_data *gl520_update_device(struct device *dev); |
| 88 | |
| 89 | /* Driver data */ |
| 90 | static struct i2c_driver gl520_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 91 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 92 | .name = "gl520sm", |
| 93 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | .attach_adapter = gl520_attach_adapter, |
| 95 | .detach_client = gl520_detach_client, |
| 96 | }; |
| 97 | |
| 98 | /* Client data */ |
| 99 | struct gl520_data { |
| 100 | struct i2c_client client; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 101 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 102 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | char valid; /* zero until the following fields are valid */ |
| 104 | unsigned long last_updated; /* in jiffies */ |
| 105 | |
| 106 | u8 vid; |
| 107 | u8 vrm; |
| 108 | u8 in_input[5]; /* [0] = VVD */ |
| 109 | u8 in_min[5]; /* [0] = VDD */ |
| 110 | u8 in_max[5]; /* [0] = VDD */ |
| 111 | u8 fan_input[2]; |
| 112 | u8 fan_min[2]; |
| 113 | u8 fan_div[2]; |
| 114 | u8 fan_off; |
| 115 | u8 temp_input[2]; |
| 116 | u8 temp_max[2]; |
| 117 | u8 temp_max_hyst[2]; |
| 118 | u8 alarms; |
| 119 | u8 beep_enable; |
| 120 | u8 beep_mask; |
| 121 | u8 alarm_mask; |
| 122 | u8 two_temps; |
| 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * Sysfs stuff |
| 127 | */ |
| 128 | |
| 129 | #define sysfs_r(type, n, item, reg) \ |
| 130 | static ssize_t get_##type##item (struct gl520_data *, char *, int); \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 131 | static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \ |
| 132 | static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { \ |
| 134 | struct gl520_data *data = gl520_update_device(dev); \ |
| 135 | return get_##type##item(data, buf, (n)); \ |
| 136 | } |
| 137 | |
| 138 | #define sysfs_w(type, n, item, reg) \ |
| 139 | static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 140 | static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \ |
| 141 | static ssize_t set_##type##n##item (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { \ |
| 143 | struct i2c_client *client = to_i2c_client(dev); \ |
| 144 | struct gl520_data *data = i2c_get_clientdata(client); \ |
| 145 | return set_##type##item(client, data, buf, count, (n), reg); \ |
| 146 | } |
| 147 | |
| 148 | #define sysfs_rw_n(type, n, item, reg) \ |
| 149 | sysfs_r(type, n, item, reg) \ |
| 150 | sysfs_w(type, n, item, reg) \ |
| 151 | static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item); |
| 152 | |
| 153 | #define sysfs_ro_n(type, n, item, reg) \ |
| 154 | sysfs_r(type, n, item, reg) \ |
| 155 | static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL); |
| 156 | |
| 157 | #define sysfs_rw(type, item, reg) \ |
| 158 | sysfs_r(type, 0, item, reg) \ |
| 159 | sysfs_w(type, 0, item, reg) \ |
| 160 | static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item); |
| 161 | |
| 162 | #define sysfs_ro(type, item, reg) \ |
| 163 | sysfs_r(type, 0, item, reg) \ |
| 164 | static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL); |
| 165 | |
| 166 | |
| 167 | #define sysfs_vid(n) \ |
| 168 | sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT) |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | #define sysfs_in(n) \ |
Jean Delvare | 8b4b0ab | 2007-11-04 23:44:52 +0100 | [diff] [blame^] | 171 | sysfs_ro_n(in, n, _input, GL520_REG_IN_INPUT[n]) \ |
| 172 | sysfs_rw_n(in, n, _min, GL520_REG_IN_MIN[n]) \ |
| 173 | sysfs_rw_n(in, n, _max, GL520_REG_IN_MAX[n]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | #define sysfs_fan(n) \ |
| 176 | sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \ |
| 177 | sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \ |
| 178 | sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV) |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | #define sysfs_fan_off(n) \ |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 181 | sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | #define sysfs_temp(n) \ |
Jean Delvare | 8b4b0ab | 2007-11-04 23:44:52 +0100 | [diff] [blame^] | 184 | sysfs_ro_n(temp, n, _input, GL520_REG_TEMP_INPUT[(n) - 1]) \ |
| 185 | sysfs_rw_n(temp, n, _max, GL520_REG_TEMP_MAX[(n) - 1]) \ |
| 186 | sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP_MAX_HYST[(n) - 1]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | #define sysfs_alarms() \ |
| 189 | sysfs_ro(alarms, , GL520_REG_ALARMS) \ |
| 190 | sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \ |
| 191 | sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK) |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | sysfs_vid(0) |
| 195 | |
| 196 | sysfs_in(0) |
| 197 | sysfs_in(1) |
| 198 | sysfs_in(2) |
| 199 | sysfs_in(3) |
| 200 | sysfs_in(4) |
| 201 | |
| 202 | sysfs_fan(1) |
| 203 | sysfs_fan(2) |
| 204 | sysfs_fan_off(1) |
| 205 | |
| 206 | sysfs_temp(1) |
| 207 | sysfs_temp(2) |
| 208 | |
| 209 | sysfs_alarms() |
| 210 | |
| 211 | |
| 212 | static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n) |
| 213 | { |
| 214 | return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm)); |
| 215 | } |
| 216 | |
| 217 | #define VDD_FROM_REG(val) (((val)*95+2)/4) |
| 218 | #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255)) |
| 219 | |
| 220 | #define IN_FROM_REG(val) ((val)*19) |
| 221 | #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255)) |
| 222 | |
| 223 | static ssize_t get_in_input(struct gl520_data *data, char *buf, int n) |
| 224 | { |
| 225 | u8 r = data->in_input[n]; |
| 226 | |
| 227 | if (n == 0) |
| 228 | return sprintf(buf, "%d\n", VDD_FROM_REG(r)); |
| 229 | else |
| 230 | return sprintf(buf, "%d\n", IN_FROM_REG(r)); |
| 231 | } |
| 232 | |
| 233 | static ssize_t get_in_min(struct gl520_data *data, char *buf, int n) |
| 234 | { |
| 235 | u8 r = data->in_min[n]; |
| 236 | |
| 237 | if (n == 0) |
| 238 | return sprintf(buf, "%d\n", VDD_FROM_REG(r)); |
| 239 | else |
| 240 | return sprintf(buf, "%d\n", IN_FROM_REG(r)); |
| 241 | } |
| 242 | |
| 243 | static ssize_t get_in_max(struct gl520_data *data, char *buf, int n) |
| 244 | { |
| 245 | u8 r = data->in_max[n]; |
| 246 | |
| 247 | if (n == 0) |
| 248 | return sprintf(buf, "%d\n", VDD_FROM_REG(r)); |
| 249 | else |
| 250 | return sprintf(buf, "%d\n", IN_FROM_REG(r)); |
| 251 | } |
| 252 | |
| 253 | static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) |
| 254 | { |
| 255 | long v = simple_strtol(buf, NULL, 10); |
| 256 | u8 r; |
| 257 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 258 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | if (n == 0) |
| 261 | r = VDD_TO_REG(v); |
| 262 | else |
| 263 | r = IN_TO_REG(v); |
| 264 | |
| 265 | data->in_min[n] = r; |
| 266 | |
| 267 | if (n < 4) |
| 268 | gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r); |
| 269 | else |
| 270 | gl520_write_value(client, reg, r); |
| 271 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 272 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return count; |
| 274 | } |
| 275 | |
| 276 | static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) |
| 277 | { |
| 278 | long v = simple_strtol(buf, NULL, 10); |
| 279 | u8 r; |
| 280 | |
| 281 | if (n == 0) |
| 282 | r = VDD_TO_REG(v); |
| 283 | else |
| 284 | r = IN_TO_REG(v); |
| 285 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 286 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | data->in_max[n] = r; |
| 289 | |
| 290 | if (n < 4) |
| 291 | gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8)); |
| 292 | else |
| 293 | gl520_write_value(client, reg, r); |
| 294 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 295 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | return count; |
| 297 | } |
| 298 | |
| 299 | #define DIV_FROM_REG(val) (1 << (val)) |
| 300 | #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div)))) |
| 301 | #define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255)); |
| 302 | |
| 303 | static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n) |
| 304 | { |
| 305 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1])); |
| 306 | } |
| 307 | |
| 308 | static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n) |
| 309 | { |
| 310 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1])); |
| 311 | } |
| 312 | |
| 313 | static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n) |
| 314 | { |
| 315 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1])); |
| 316 | } |
| 317 | |
| 318 | static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n) |
| 319 | { |
| 320 | return sprintf(buf, "%d\n", data->fan_off); |
| 321 | } |
| 322 | |
| 323 | static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) |
| 324 | { |
| 325 | unsigned long v = simple_strtoul(buf, NULL, 10); |
| 326 | u8 r; |
| 327 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 328 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | r = FAN_TO_REG(v, data->fan_div[n - 1]); |
| 330 | data->fan_min[n - 1] = r; |
| 331 | |
| 332 | if (n == 1) |
| 333 | gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8)); |
| 334 | else |
| 335 | gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r); |
| 336 | |
| 337 | data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); |
| 338 | if (data->fan_min[n - 1] == 0) |
| 339 | data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40; |
| 340 | else |
| 341 | data->alarm_mask |= (n == 1) ? 0x20 : 0x40; |
| 342 | data->beep_mask &= data->alarm_mask; |
| 343 | gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); |
| 344 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 345 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | return count; |
| 347 | } |
| 348 | |
| 349 | static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) |
| 350 | { |
| 351 | unsigned long v = simple_strtoul(buf, NULL, 10); |
| 352 | u8 r; |
| 353 | |
| 354 | switch (v) { |
| 355 | case 1: r = 0; break; |
| 356 | case 2: r = 1; break; |
| 357 | case 4: r = 2; break; |
| 358 | case 8: r = 3; break; |
| 359 | default: |
| 360 | dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v); |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 364 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | data->fan_div[n - 1] = r; |
| 366 | |
| 367 | if (n == 1) |
| 368 | gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6)); |
| 369 | else |
| 370 | gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4)); |
| 371 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 372 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return count; |
| 374 | } |
| 375 | |
| 376 | static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) |
| 377 | { |
| 378 | u8 r = simple_strtoul(buf, NULL, 10)?1:0; |
| 379 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 380 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | data->fan_off = r; |
| 382 | gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 383 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return count; |
| 385 | } |
| 386 | |
| 387 | #define TEMP_FROM_REG(val) (((val) - 130) * 1000) |
| 388 | #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255)) |
| 389 | |
| 390 | static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n) |
| 391 | { |
| 392 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1])); |
| 393 | } |
| 394 | |
| 395 | static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n) |
| 396 | { |
| 397 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1])); |
| 398 | } |
| 399 | |
| 400 | static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n) |
| 401 | { |
| 402 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1])); |
| 403 | } |
| 404 | |
| 405 | static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) |
| 406 | { |
| 407 | long v = simple_strtol(buf, NULL, 10); |
| 408 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 409 | mutex_lock(&data->update_lock); |
Alexey Dobriyan | 53b3531 | 2006-03-24 03:16:13 -0800 | [diff] [blame] | 410 | data->temp_max[n - 1] = TEMP_TO_REG(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | gl520_write_value(client, reg, data->temp_max[n - 1]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 412 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | return count; |
| 414 | } |
| 415 | |
| 416 | static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) |
| 417 | { |
| 418 | long v = simple_strtol(buf, NULL, 10); |
| 419 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 420 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | data->temp_max_hyst[n - 1] = TEMP_TO_REG(v); |
| 422 | gl520_write_value(client, reg, data->temp_max_hyst[n - 1]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 423 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return count; |
| 425 | } |
| 426 | |
| 427 | static ssize_t get_alarms(struct gl520_data *data, char *buf, int n) |
| 428 | { |
| 429 | return sprintf(buf, "%d\n", data->alarms); |
| 430 | } |
| 431 | |
| 432 | static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n) |
| 433 | { |
| 434 | return sprintf(buf, "%d\n", data->beep_enable); |
| 435 | } |
| 436 | |
| 437 | static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n) |
| 438 | { |
| 439 | return sprintf(buf, "%d\n", data->beep_mask); |
| 440 | } |
| 441 | |
| 442 | static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) |
| 443 | { |
| 444 | u8 r = simple_strtoul(buf, NULL, 10)?0:1; |
| 445 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 446 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | data->beep_enable = !r; |
| 448 | gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 449 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | return count; |
| 451 | } |
| 452 | |
| 453 | static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg) |
| 454 | { |
| 455 | u8 r = simple_strtoul(buf, NULL, 10); |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 456 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 457 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | r &= data->alarm_mask; |
| 459 | data->beep_mask = r; |
| 460 | gl520_write_value(client, reg, r); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 461 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return count; |
| 463 | } |
| 464 | |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 465 | static struct attribute *gl520_attributes[] = { |
| 466 | &dev_attr_cpu0_vid.attr, |
| 467 | |
| 468 | &dev_attr_in0_input.attr, |
| 469 | &dev_attr_in0_min.attr, |
| 470 | &dev_attr_in0_max.attr, |
| 471 | &dev_attr_in1_input.attr, |
| 472 | &dev_attr_in1_min.attr, |
| 473 | &dev_attr_in1_max.attr, |
| 474 | &dev_attr_in2_input.attr, |
| 475 | &dev_attr_in2_min.attr, |
| 476 | &dev_attr_in2_max.attr, |
| 477 | &dev_attr_in3_input.attr, |
| 478 | &dev_attr_in3_min.attr, |
| 479 | &dev_attr_in3_max.attr, |
| 480 | |
| 481 | &dev_attr_fan1_input.attr, |
| 482 | &dev_attr_fan1_min.attr, |
| 483 | &dev_attr_fan1_div.attr, |
| 484 | &dev_attr_fan1_off.attr, |
| 485 | &dev_attr_fan2_input.attr, |
| 486 | &dev_attr_fan2_min.attr, |
| 487 | &dev_attr_fan2_div.attr, |
| 488 | |
| 489 | &dev_attr_temp1_input.attr, |
| 490 | &dev_attr_temp1_max.attr, |
| 491 | &dev_attr_temp1_max_hyst.attr, |
| 492 | |
| 493 | &dev_attr_alarms.attr, |
| 494 | &dev_attr_beep_enable.attr, |
| 495 | &dev_attr_beep_mask.attr, |
| 496 | NULL |
| 497 | }; |
| 498 | |
| 499 | static const struct attribute_group gl520_group = { |
| 500 | .attrs = gl520_attributes, |
| 501 | }; |
| 502 | |
| 503 | static struct attribute *gl520_attributes_opt[] = { |
| 504 | &dev_attr_in4_input.attr, |
| 505 | &dev_attr_in4_min.attr, |
| 506 | &dev_attr_in4_max.attr, |
| 507 | |
| 508 | &dev_attr_temp2_input.attr, |
| 509 | &dev_attr_temp2_max.attr, |
| 510 | &dev_attr_temp2_max_hyst.attr, |
| 511 | NULL |
| 512 | }; |
| 513 | |
| 514 | static const struct attribute_group gl520_group_opt = { |
| 515 | .attrs = gl520_attributes_opt, |
| 516 | }; |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * Real code |
| 521 | */ |
| 522 | |
| 523 | static int gl520_attach_adapter(struct i2c_adapter *adapter) |
| 524 | { |
| 525 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 526 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 527 | return i2c_probe(adapter, &addr_data, gl520_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static int gl520_detect(struct i2c_adapter *adapter, int address, int kind) |
| 531 | { |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 532 | struct i2c_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | struct gl520_data *data; |
| 534 | int err = 0; |
| 535 | |
| 536 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 537 | I2C_FUNC_SMBUS_WORD_DATA)) |
| 538 | goto exit; |
| 539 | |
| 540 | /* OK. For now, we presume we have a valid client. We now create the |
| 541 | client structure, even though we cannot fill it completely yet. |
| 542 | But it allows us to access gl520_{read,write}_value. */ |
| 543 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 544 | if (!(data = kzalloc(sizeof(struct gl520_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | err = -ENOMEM; |
| 546 | goto exit; |
| 547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 549 | client = &data->client; |
| 550 | i2c_set_clientdata(client, data); |
| 551 | client->addr = address; |
| 552 | client->adapter = adapter; |
| 553 | client->driver = &gl520_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | /* Determine the chip type. */ |
| 556 | if (kind < 0) { |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 557 | if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) || |
| 558 | ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) || |
| 559 | ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) { |
| 560 | dev_dbg(&client->dev, "Unknown chip type, skipping\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | goto exit_free; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* Fill in the remaining client fields */ |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 566 | strlcpy(client->name, "gl520sm", I2C_NAME_SIZE); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 567 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | /* Tell the I2C layer a new client has arrived */ |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 570 | if ((err = i2c_attach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | goto exit_free; |
| 572 | |
| 573 | /* Initialize the GL520SM chip */ |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 574 | gl520_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
| 576 | /* Register sysfs hooks */ |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 577 | if ((err = sysfs_create_group(&client->dev.kobj, &gl520_group))) |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 578 | goto exit_detach; |
| 579 | |
| 580 | if (data->two_temps) { |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 581 | if ((err = device_create_file(&client->dev, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 582 | &dev_attr_temp2_input)) |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 583 | || (err = device_create_file(&client->dev, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 584 | &dev_attr_temp2_max)) |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 585 | || (err = device_create_file(&client->dev, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 586 | &dev_attr_temp2_max_hyst))) |
| 587 | goto exit_remove_files; |
| 588 | } else { |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 589 | if ((err = device_create_file(&client->dev, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 590 | &dev_attr_in4_input)) |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 591 | || (err = device_create_file(&client->dev, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 592 | &dev_attr_in4_min)) |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 593 | || (err = device_create_file(&client->dev, |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 594 | &dev_attr_in4_max))) |
| 595 | goto exit_remove_files; |
| 596 | } |
| 597 | |
| 598 | |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 599 | data->hwmon_dev = hwmon_device_register(&client->dev); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 600 | if (IS_ERR(data->hwmon_dev)) { |
| 601 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 602 | goto exit_remove_files; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | return 0; |
| 606 | |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 607 | exit_remove_files: |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 608 | sysfs_remove_group(&client->dev.kobj, &gl520_group); |
| 609 | sysfs_remove_group(&client->dev.kobj, &gl520_group_opt); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 610 | exit_detach: |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 611 | i2c_detach_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | exit_free: |
| 613 | kfree(data); |
| 614 | exit: |
| 615 | return err; |
| 616 | } |
| 617 | |
| 618 | |
| 619 | /* Called when we have found a new GL520SM. */ |
| 620 | static void gl520_init_client(struct i2c_client *client) |
| 621 | { |
| 622 | struct gl520_data *data = i2c_get_clientdata(client); |
| 623 | u8 oldconf, conf; |
| 624 | |
| 625 | conf = oldconf = gl520_read_value(client, GL520_REG_CONF); |
| 626 | |
| 627 | data->alarm_mask = 0xff; |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 628 | data->vrm = vid_which_vrm(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | if (extra_sensor_type == 1) |
| 631 | conf &= ~0x10; |
| 632 | else if (extra_sensor_type == 2) |
| 633 | conf |= 0x10; |
| 634 | data->two_temps = !(conf & 0x10); |
| 635 | |
| 636 | /* If IRQ# is disabled, we can safely force comparator mode */ |
| 637 | if (!(conf & 0x20)) |
| 638 | conf &= 0xf7; |
| 639 | |
| 640 | /* Enable monitoring if needed */ |
| 641 | conf |= 0x40; |
| 642 | |
| 643 | if (conf != oldconf) |
| 644 | gl520_write_value(client, GL520_REG_CONF, conf); |
| 645 | |
| 646 | gl520_update_device(&(client->dev)); |
| 647 | |
| 648 | if (data->fan_min[0] == 0) |
| 649 | data->alarm_mask &= ~0x20; |
| 650 | if (data->fan_min[1] == 0) |
| 651 | data->alarm_mask &= ~0x40; |
| 652 | |
| 653 | data->beep_mask &= data->alarm_mask; |
| 654 | gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); |
| 655 | } |
| 656 | |
| 657 | static int gl520_detach_client(struct i2c_client *client) |
| 658 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 659 | struct gl520_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | int err; |
| 661 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 662 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 663 | sysfs_remove_group(&client->dev.kobj, &gl520_group); |
| 664 | sysfs_remove_group(&client->dev.kobj, &gl520_group_opt); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 665 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 666 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 669 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | |
Jean Delvare | f28dc2f | 2007-11-04 23:44:23 +0100 | [diff] [blame] | 674 | /* Registers 0x07 to 0x0c are word-sized, others are byte-sized |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | GL520 uses a high-byte first convention */ |
| 676 | static int gl520_read_value(struct i2c_client *client, u8 reg) |
| 677 | { |
| 678 | if ((reg >= 0x07) && (reg <= 0x0c)) |
| 679 | return swab16(i2c_smbus_read_word_data(client, reg)); |
| 680 | else |
| 681 | return i2c_smbus_read_byte_data(client, reg); |
| 682 | } |
| 683 | |
| 684 | static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value) |
| 685 | { |
| 686 | if ((reg >= 0x07) && (reg <= 0x0c)) |
| 687 | return i2c_smbus_write_word_data(client, reg, swab16(value)); |
| 688 | else |
| 689 | return i2c_smbus_write_byte_data(client, reg, value); |
| 690 | } |
| 691 | |
| 692 | |
| 693 | static struct gl520_data *gl520_update_device(struct device *dev) |
| 694 | { |
| 695 | struct i2c_client *client = to_i2c_client(dev); |
| 696 | struct gl520_data *data = i2c_get_clientdata(client); |
Jean Delvare | 8b4b0ab | 2007-11-04 23:44:52 +0100 | [diff] [blame^] | 697 | int val, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 699 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
Jean Delvare | 0cacdf2 | 2005-07-29 12:15:12 -0700 | [diff] [blame] | 701 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
| 703 | dev_dbg(&client->dev, "Starting gl520sm update\n"); |
| 704 | |
| 705 | data->alarms = gl520_read_value(client, GL520_REG_ALARMS); |
| 706 | data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); |
| 707 | data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f; |
| 708 | |
Jean Delvare | 8b4b0ab | 2007-11-04 23:44:52 +0100 | [diff] [blame^] | 709 | for (i = 0; i < 4; i++) { |
| 710 | data->in_input[i] = gl520_read_value(client, |
| 711 | GL520_REG_IN_INPUT[i]); |
| 712 | val = gl520_read_value(client, GL520_REG_IN_LIMIT[i]); |
| 713 | data->in_min[i] = val & 0xff; |
| 714 | data->in_max[i] = (val >> 8) & 0xff; |
| 715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
| 717 | val = gl520_read_value(client, GL520_REG_FAN_INPUT); |
| 718 | data->fan_input[0] = (val >> 8) & 0xff; |
| 719 | data->fan_input[1] = val & 0xff; |
| 720 | |
| 721 | val = gl520_read_value(client, GL520_REG_FAN_MIN); |
| 722 | data->fan_min[0] = (val >> 8) & 0xff; |
| 723 | data->fan_min[1] = val & 0xff; |
| 724 | |
Jean Delvare | 8b4b0ab | 2007-11-04 23:44:52 +0100 | [diff] [blame^] | 725 | data->temp_input[0] = gl520_read_value(client, |
| 726 | GL520_REG_TEMP_INPUT[0]); |
| 727 | data->temp_max[0] = gl520_read_value(client, |
| 728 | GL520_REG_TEMP_MAX[0]); |
| 729 | data->temp_max_hyst[0] = gl520_read_value(client, |
| 730 | GL520_REG_TEMP_MAX_HYST[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
| 732 | val = gl520_read_value(client, GL520_REG_FAN_DIV); |
| 733 | data->fan_div[0] = (val >> 6) & 0x03; |
| 734 | data->fan_div[1] = (val >> 4) & 0x03; |
| 735 | data->fan_off = (val >> 2) & 0x01; |
| 736 | |
| 737 | data->alarms &= data->alarm_mask; |
| 738 | |
| 739 | val = gl520_read_value(client, GL520_REG_CONF); |
| 740 | data->beep_enable = !((val >> 2) & 1); |
| 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | /* Temp1 and Vin4 are the same input */ |
| 743 | if (data->two_temps) { |
Jean Delvare | 8b4b0ab | 2007-11-04 23:44:52 +0100 | [diff] [blame^] | 744 | data->temp_input[1] = gl520_read_value(client, |
| 745 | GL520_REG_TEMP_INPUT[1]); |
| 746 | data->temp_max[1] = gl520_read_value(client, |
| 747 | GL520_REG_TEMP_MAX[1]); |
| 748 | data->temp_max_hyst[1] = gl520_read_value(client, |
| 749 | GL520_REG_TEMP_MAX_HYST[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | } else { |
Jean Delvare | 8b4b0ab | 2007-11-04 23:44:52 +0100 | [diff] [blame^] | 751 | data->in_input[4] = gl520_read_value(client, |
| 752 | GL520_REG_IN_INPUT[4]); |
| 753 | data->in_min[4] = gl520_read_value(client, |
| 754 | GL520_REG_IN_MIN[4]); |
| 755 | data->in_max[4] = gl520_read_value(client, |
| 756 | GL520_REG_IN_MAX[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | data->last_updated = jiffies; |
| 760 | data->valid = 1; |
| 761 | } |
| 762 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 763 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | return data; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | static int __init sensors_gl520sm_init(void) |
| 770 | { |
| 771 | return i2c_add_driver(&gl520_driver); |
| 772 | } |
| 773 | |
| 774 | static void __exit sensors_gl520sm_exit(void) |
| 775 | { |
| 776 | i2c_del_driver(&gl520_driver); |
| 777 | } |
| 778 | |
| 779 | |
| 780 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 781 | "Kyösti Mälkki <kmalkki@cc.hut.fi>, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | "Maarten Deprez <maartendeprez@users.sourceforge.net>"); |
| 783 | MODULE_DESCRIPTION("GL520SM driver"); |
| 784 | MODULE_LICENSE("GPL"); |
| 785 | |
| 786 | module_init(sensors_gl520sm_init); |
| 787 | module_exit(sensors_gl520sm_exit); |