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