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