Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 1 | /* |
| 2 | * adm9240.c Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * |
| 5 | * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl> |
| 6 | * Philip Edelbrock <phil@netroedge.com> |
| 7 | * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl> |
| 8 | * Copyright (C) 2005 Grant Coady <gcoady@gmail.com> with valuable |
| 9 | * guidance from Jean Delvare |
| 10 | * |
| 11 | * Driver supports Analog Devices ADM9240 |
| 12 | * Dallas Semiconductor DS1780 |
| 13 | * National Semiconductor LM81 |
| 14 | * |
| 15 | * ADM9240 is the reference, DS1780 and LM81 are register compatibles |
| 16 | * |
| 17 | * Voltage Six inputs are scaled by chip, VID also reported |
| 18 | * Temperature Chip temperature to 0.5'C, maximum and max_hysteris |
| 19 | * Fans 2 fans, low speed alarm, automatic fan clock divider |
| 20 | * Alarms 16-bit map of active alarms |
| 21 | * Analog Out 0..1250 mV output |
| 22 | * |
| 23 | * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear' |
| 24 | * |
| 25 | * Test hardware: Intel SE440BX-2 desktop motherboard --Grant |
| 26 | * |
| 27 | * LM81 extended temp reading not implemented |
| 28 | * |
| 29 | * This program is free software; you can redistribute it and/or modify |
| 30 | * it under the terms of the GNU General Public License as published by |
| 31 | * the Free Software Foundation; either version 2 of the License, or |
| 32 | * (at your option) any later version. |
| 33 | * |
| 34 | * This program is distributed in the hope that it will be useful, |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | * GNU General Public License for more details. |
| 38 | * |
| 39 | * You should have received a copy of the GNU General Public License |
| 40 | * along with this program; if not, write to the Free Software |
| 41 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 42 | */ |
| 43 | |
| 44 | #include <linux/init.h> |
| 45 | #include <linux/module.h> |
| 46 | #include <linux/slab.h> |
| 47 | #include <linux/i2c.h> |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 48 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 49 | #include <linux/hwmon.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 50 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 51 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 52 | #include <linux/mutex.h> |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 53 | |
| 54 | /* Addresses to scan */ |
| 55 | static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, |
| 56 | I2C_CLIENT_END }; |
| 57 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 58 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 59 | I2C_CLIENT_INSMOD_3(adm9240, ds1780, lm81); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 60 | |
| 61 | /* ADM9240 registers */ |
| 62 | #define ADM9240_REG_MAN_ID 0x3e |
| 63 | #define ADM9240_REG_DIE_REV 0x3f |
| 64 | #define ADM9240_REG_CONFIG 0x40 |
| 65 | |
| 66 | #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */ |
| 67 | #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2) |
| 68 | #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2) |
| 69 | #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */ |
| 70 | #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 71 | #define ADM9240_REG_INT(nr) (0x41 + (nr)) |
| 72 | #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr)) |
| 73 | #define ADM9240_REG_TEMP 0x27 |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 74 | #define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 75 | #define ADM9240_REG_ANALOG_OUT 0x19 |
| 76 | #define ADM9240_REG_CHASSIS_CLEAR 0x46 |
| 77 | #define ADM9240_REG_VID_FAN_DIV 0x47 |
| 78 | #define ADM9240_REG_I2C_ADDR 0x48 |
| 79 | #define ADM9240_REG_VID4 0x49 |
| 80 | #define ADM9240_REG_TEMP_CONF 0x4b |
| 81 | |
| 82 | /* generalised scaling with integer rounding */ |
| 83 | static inline int SCALE(long val, int mul, int div) |
| 84 | { |
| 85 | if (val < 0) |
| 86 | return (val * mul - div / 2) / div; |
| 87 | else |
| 88 | return (val * mul + div / 2) / div; |
| 89 | } |
| 90 | |
| 91 | /* adm9240 internally scales voltage measurements */ |
| 92 | static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 }; |
| 93 | |
| 94 | static inline unsigned int IN_FROM_REG(u8 reg, int n) |
| 95 | { |
| 96 | return SCALE(reg, nom_mv[n], 192); |
| 97 | } |
| 98 | |
| 99 | static inline u8 IN_TO_REG(unsigned long val, int n) |
| 100 | { |
| 101 | return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255); |
| 102 | } |
| 103 | |
| 104 | /* temperature range: -40..125, 127 disables temperature alarm */ |
| 105 | static inline s8 TEMP_TO_REG(long val) |
| 106 | { |
| 107 | return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127); |
| 108 | } |
| 109 | |
| 110 | /* two fans, each with low fan speed limit */ |
| 111 | static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) |
| 112 | { |
| 113 | if (!reg) /* error */ |
| 114 | return -1; |
| 115 | |
| 116 | if (reg == 255) |
| 117 | return 0; |
| 118 | |
| 119 | return SCALE(1350000, 1, reg * div); |
| 120 | } |
| 121 | |
| 122 | /* analog out 0..1250mV */ |
| 123 | static inline u8 AOUT_TO_REG(unsigned long val) |
| 124 | { |
| 125 | return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255); |
| 126 | } |
| 127 | |
| 128 | static inline unsigned int AOUT_FROM_REG(u8 reg) |
| 129 | { |
| 130 | return SCALE(reg, 1250, 255); |
| 131 | } |
| 132 | |
| 133 | static int adm9240_attach_adapter(struct i2c_adapter *adapter); |
| 134 | static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind); |
| 135 | static void adm9240_init_client(struct i2c_client *client); |
| 136 | static int adm9240_detach_client(struct i2c_client *client); |
| 137 | static struct adm9240_data *adm9240_update_device(struct device *dev); |
| 138 | |
| 139 | /* driver data */ |
| 140 | static struct i2c_driver adm9240_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 141 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 142 | .name = "adm9240", |
| 143 | }, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 144 | .id = I2C_DRIVERID_ADM9240, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 145 | .attach_adapter = adm9240_attach_adapter, |
| 146 | .detach_client = adm9240_detach_client, |
| 147 | }; |
| 148 | |
| 149 | /* per client data */ |
| 150 | struct adm9240_data { |
| 151 | enum chips type; |
| 152 | struct i2c_client client; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 153 | struct class_device *class_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 154 | struct mutex update_lock; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 155 | char valid; |
| 156 | unsigned long last_updated_measure; |
| 157 | unsigned long last_updated_config; |
| 158 | |
| 159 | u8 in[6]; /* ro in0_input */ |
| 160 | u8 in_max[6]; /* rw in0_max */ |
| 161 | u8 in_min[6]; /* rw in0_min */ |
| 162 | u8 fan[2]; /* ro fan1_input */ |
| 163 | u8 fan_min[2]; /* rw fan1_min */ |
| 164 | u8 fan_div[2]; /* rw fan1_div, read-only accessor */ |
| 165 | s16 temp; /* ro temp1_input, 9-bit sign-extended */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 166 | s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 167 | u16 alarms; /* ro alarms */ |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 168 | u8 aout; /* rw aout_output */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 169 | u8 vid; /* ro vid */ |
| 170 | u8 vrm; /* -- vrm set on startup, no accessor */ |
| 171 | }; |
| 172 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 173 | /*** sysfs accessors ***/ |
| 174 | |
| 175 | /* temperature */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 176 | static ssize_t show_temp(struct device *dev, struct device_attribute *dummy, |
| 177 | char *buf) |
| 178 | { |
| 179 | struct adm9240_data *data = adm9240_update_device(dev); |
| 180 | return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 181 | } |
| 182 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 183 | static ssize_t show_max(struct device *dev, struct device_attribute *devattr, |
| 184 | char *buf) |
| 185 | { |
| 186 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 187 | struct adm9240_data *data = adm9240_update_device(dev); |
| 188 | return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000); |
| 189 | } |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 190 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 191 | static ssize_t set_max(struct device *dev, struct device_attribute *devattr, |
| 192 | const char *buf, size_t count) |
| 193 | { |
| 194 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 195 | struct i2c_client *client = to_i2c_client(dev); |
| 196 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 197 | long val = simple_strtol(buf, NULL, 10); |
| 198 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 199 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 200 | data->temp_max[attr->index] = TEMP_TO_REG(val); |
| 201 | i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index), |
| 202 | data->temp_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 203 | mutex_unlock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 204 | return count; |
| 205 | } |
| 206 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 207 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 208 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 209 | show_max, set_max, 0); |
| 210 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, |
| 211 | show_max, set_max, 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 212 | |
| 213 | /* voltage */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 214 | static ssize_t show_in(struct device *dev, struct device_attribute *devattr, |
| 215 | char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 216 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 217 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 218 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 219 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index], |
| 220 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 221 | } |
| 222 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 223 | static ssize_t show_in_min(struct device *dev, |
| 224 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 225 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 226 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 227 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 228 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index], |
| 229 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 230 | } |
| 231 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 232 | static ssize_t show_in_max(struct device *dev, |
| 233 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 234 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 235 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 236 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 237 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index], |
| 238 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 239 | } |
| 240 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 241 | static ssize_t set_in_min(struct device *dev, |
| 242 | struct device_attribute *devattr, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 243 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 244 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 245 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 246 | struct i2c_client *client = to_i2c_client(dev); |
| 247 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 248 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 249 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 250 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 251 | data->in_min[attr->index] = IN_TO_REG(val, attr->index); |
| 252 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index), |
| 253 | data->in_min[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 254 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 255 | return count; |
| 256 | } |
| 257 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 258 | static ssize_t set_in_max(struct device *dev, |
| 259 | struct device_attribute *devattr, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 260 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 261 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 262 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 263 | struct i2c_client *client = to_i2c_client(dev); |
| 264 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 265 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 266 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 267 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 268 | data->in_max[attr->index] = IN_TO_REG(val, attr->index); |
| 269 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index), |
| 270 | data->in_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 271 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 272 | return count; |
| 273 | } |
| 274 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 275 | #define vin(nr) \ |
| 276 | static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \ |
| 277 | show_in, NULL, nr); \ |
| 278 | static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \ |
| 279 | show_in_min, set_in_min, nr); \ |
| 280 | static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \ |
| 281 | show_in_max, set_in_max, nr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 282 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 283 | vin(0); |
| 284 | vin(1); |
| 285 | vin(2); |
| 286 | vin(3); |
| 287 | vin(4); |
| 288 | vin(5); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 289 | |
| 290 | /* fans */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 291 | static ssize_t show_fan(struct device *dev, |
| 292 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 293 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 294 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 295 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 296 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], |
| 297 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 298 | } |
| 299 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 300 | static ssize_t show_fan_min(struct device *dev, |
| 301 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 302 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 303 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 304 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 305 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index], |
| 306 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 307 | } |
| 308 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 309 | static ssize_t show_fan_div(struct device *dev, |
| 310 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 311 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 312 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 313 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 314 | return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* write new fan div, callers must hold data->update_lock */ |
| 318 | static void adm9240_write_fan_div(struct i2c_client *client, int nr, |
| 319 | u8 fan_div) |
| 320 | { |
| 321 | u8 reg, old, shift = (nr + 2) * 2; |
| 322 | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 323 | reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 324 | old = (reg >> shift) & 3; |
| 325 | reg &= ~(3 << shift); |
| 326 | reg |= (fan_div << shift); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 327 | i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 328 | dev_dbg(&client->dev, "fan%d clock divider changed from %u " |
| 329 | "to %u\n", nr + 1, 1 << old, 1 << fan_div); |
| 330 | } |
| 331 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 332 | /* |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 333 | * set fan speed low limit: |
| 334 | * |
| 335 | * - value is zero: disable fan speed low limit alarm |
| 336 | * |
| 337 | * - value is below fan speed measurement range: enable fan speed low |
| 338 | * limit alarm to be asserted while fan speed too slow to measure |
| 339 | * |
| 340 | * - otherwise: select fan clock divider to suit fan speed low limit, |
| 341 | * measurement code may adjust registers to ensure fan speed reading |
| 342 | */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 343 | static ssize_t set_fan_min(struct device *dev, |
| 344 | struct device_attribute *devattr, |
| 345 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 346 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 347 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 348 | struct i2c_client *client = to_i2c_client(dev); |
| 349 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 350 | unsigned long val = simple_strtoul(buf, NULL, 10); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 351 | int nr = attr->index; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 352 | u8 new_div; |
| 353 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 354 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 355 | |
| 356 | if (!val) { |
| 357 | data->fan_min[nr] = 255; |
| 358 | new_div = data->fan_div[nr]; |
| 359 | |
| 360 | dev_dbg(&client->dev, "fan%u low limit set disabled\n", |
| 361 | nr + 1); |
| 362 | |
| 363 | } else if (val < 1350000 / (8 * 254)) { |
| 364 | new_div = 3; |
| 365 | data->fan_min[nr] = 254; |
| 366 | |
| 367 | dev_dbg(&client->dev, "fan%u low limit set minimum %u\n", |
| 368 | nr + 1, FAN_FROM_REG(254, 1 << new_div)); |
| 369 | |
| 370 | } else { |
| 371 | unsigned int new_min = 1350000 / val; |
| 372 | |
| 373 | new_div = 0; |
| 374 | while (new_min > 192 && new_div < 3) { |
| 375 | new_div++; |
| 376 | new_min /= 2; |
| 377 | } |
| 378 | if (!new_min) /* keep > 0 */ |
| 379 | new_min++; |
| 380 | |
| 381 | data->fan_min[nr] = new_min; |
| 382 | |
| 383 | dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n", |
| 384 | nr + 1, FAN_FROM_REG(new_min, 1 << new_div)); |
| 385 | } |
| 386 | |
| 387 | if (new_div != data->fan_div[nr]) { |
| 388 | data->fan_div[nr] = new_div; |
| 389 | adm9240_write_fan_div(client, nr, new_div); |
| 390 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 391 | i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr), |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 392 | data->fan_min[nr]); |
| 393 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 394 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 395 | return count; |
| 396 | } |
| 397 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 398 | #define fan(nr) \ |
| 399 | static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \ |
| 400 | show_fan, NULL, nr - 1); \ |
| 401 | static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \ |
| 402 | show_fan_div, NULL, nr - 1); \ |
| 403 | static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \ |
| 404 | show_fan_min, set_fan_min, nr - 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 405 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 406 | fan(1); |
| 407 | fan(2); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 408 | |
| 409 | /* alarms */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 410 | static ssize_t show_alarms(struct device *dev, |
| 411 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 412 | { |
| 413 | struct adm9240_data *data = adm9240_update_device(dev); |
| 414 | return sprintf(buf, "%u\n", data->alarms); |
| 415 | } |
| 416 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 417 | |
| 418 | /* vid */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 419 | static ssize_t show_vid(struct device *dev, |
| 420 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 421 | { |
| 422 | struct adm9240_data *data = adm9240_update_device(dev); |
| 423 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 424 | } |
| 425 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 426 | |
| 427 | /* analog output */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 428 | static ssize_t show_aout(struct device *dev, |
| 429 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 430 | { |
| 431 | struct adm9240_data *data = adm9240_update_device(dev); |
| 432 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); |
| 433 | } |
| 434 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 435 | static ssize_t set_aout(struct device *dev, |
| 436 | struct device_attribute *attr, |
| 437 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 438 | { |
| 439 | struct i2c_client *client = to_i2c_client(dev); |
| 440 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 441 | unsigned long val = simple_strtol(buf, NULL, 10); |
| 442 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 443 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 444 | data->aout = AOUT_TO_REG(val); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 445 | i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 446 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 447 | return count; |
| 448 | } |
| 449 | static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout); |
| 450 | |
| 451 | /* chassis_clear */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 452 | static ssize_t chassis_clear(struct device *dev, |
| 453 | struct device_attribute *attr, |
| 454 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 455 | { |
| 456 | struct i2c_client *client = to_i2c_client(dev); |
| 457 | unsigned long val = simple_strtol(buf, NULL, 10); |
| 458 | |
| 459 | if (val == 1) { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 460 | i2c_smbus_write_byte_data(client, |
| 461 | ADM9240_REG_CHASSIS_CLEAR, 0x80); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 462 | dev_dbg(&client->dev, "chassis intrusion latch cleared\n"); |
| 463 | } |
| 464 | return count; |
| 465 | } |
| 466 | static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear); |
| 467 | |
| 468 | |
| 469 | /*** sensor chip detect and driver install ***/ |
| 470 | |
| 471 | static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind) |
| 472 | { |
| 473 | struct i2c_client *new_client; |
| 474 | struct adm9240_data *data; |
| 475 | int err = 0; |
| 476 | const char *name = ""; |
| 477 | u8 man_id, die_rev; |
| 478 | |
| 479 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 480 | goto exit; |
| 481 | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 482 | if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 483 | err = -ENOMEM; |
| 484 | goto exit; |
| 485 | } |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 486 | |
| 487 | new_client = &data->client; |
| 488 | i2c_set_clientdata(new_client, data); |
| 489 | new_client->addr = address; |
| 490 | new_client->adapter = adapter; |
| 491 | new_client->driver = &adm9240_driver; |
| 492 | new_client->flags = 0; |
| 493 | |
| 494 | if (kind == 0) { |
| 495 | kind = adm9240; |
| 496 | } |
| 497 | |
| 498 | if (kind < 0) { |
| 499 | |
| 500 | /* verify chip: reg address should match i2c address */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 501 | if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 502 | != address) { |
| 503 | dev_err(&adapter->dev, "detect fail: address match, " |
| 504 | "0x%02x\n", address); |
| 505 | goto exit_free; |
| 506 | } |
| 507 | |
| 508 | /* check known chip manufacturer */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 509 | man_id = i2c_smbus_read_byte_data(new_client, |
| 510 | ADM9240_REG_MAN_ID); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 511 | if (man_id == 0x23) { |
| 512 | kind = adm9240; |
| 513 | } else if (man_id == 0xda) { |
| 514 | kind = ds1780; |
| 515 | } else if (man_id == 0x01) { |
| 516 | kind = lm81; |
| 517 | } else { |
| 518 | dev_err(&adapter->dev, "detect fail: unknown manuf, " |
| 519 | "0x%02x\n", man_id); |
| 520 | goto exit_free; |
| 521 | } |
| 522 | |
| 523 | /* successful detect, print chip info */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 524 | die_rev = i2c_smbus_read_byte_data(new_client, |
| 525 | ADM9240_REG_DIE_REV); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 526 | dev_info(&adapter->dev, "found %s revision %u\n", |
| 527 | man_id == 0x23 ? "ADM9240" : |
| 528 | man_id == 0xda ? "DS1780" : "LM81", die_rev); |
| 529 | } |
| 530 | |
| 531 | /* either forced or detected chip kind */ |
| 532 | if (kind == adm9240) { |
| 533 | name = "adm9240"; |
| 534 | } else if (kind == ds1780) { |
| 535 | name = "ds1780"; |
| 536 | } else if (kind == lm81) { |
| 537 | name = "lm81"; |
| 538 | } |
| 539 | |
| 540 | /* fill in the remaining client fields and attach */ |
| 541 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
| 542 | data->type = kind; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 543 | mutex_init(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 544 | |
| 545 | if ((err = i2c_attach_client(new_client))) |
| 546 | goto exit_free; |
| 547 | |
| 548 | adm9240_init_client(new_client); |
| 549 | |
| 550 | /* populate sysfs filesystem */ |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 551 | data->class_dev = hwmon_device_register(&new_client->dev); |
| 552 | if (IS_ERR(data->class_dev)) { |
| 553 | err = PTR_ERR(data->class_dev); |
| 554 | goto exit_detach; |
| 555 | } |
| 556 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 557 | device_create_file(&new_client->dev, |
| 558 | &sensor_dev_attr_in0_input.dev_attr); |
| 559 | device_create_file(&new_client->dev, |
| 560 | &sensor_dev_attr_in0_min.dev_attr); |
| 561 | device_create_file(&new_client->dev, |
| 562 | &sensor_dev_attr_in0_max.dev_attr); |
| 563 | device_create_file(&new_client->dev, |
| 564 | &sensor_dev_attr_in1_input.dev_attr); |
| 565 | device_create_file(&new_client->dev, |
| 566 | &sensor_dev_attr_in1_min.dev_attr); |
| 567 | device_create_file(&new_client->dev, |
| 568 | &sensor_dev_attr_in1_max.dev_attr); |
| 569 | device_create_file(&new_client->dev, |
| 570 | &sensor_dev_attr_in2_input.dev_attr); |
| 571 | device_create_file(&new_client->dev, |
| 572 | &sensor_dev_attr_in2_min.dev_attr); |
| 573 | device_create_file(&new_client->dev, |
| 574 | &sensor_dev_attr_in2_max.dev_attr); |
| 575 | device_create_file(&new_client->dev, |
| 576 | &sensor_dev_attr_in3_input.dev_attr); |
| 577 | device_create_file(&new_client->dev, |
| 578 | &sensor_dev_attr_in3_min.dev_attr); |
| 579 | device_create_file(&new_client->dev, |
| 580 | &sensor_dev_attr_in3_max.dev_attr); |
| 581 | device_create_file(&new_client->dev, |
| 582 | &sensor_dev_attr_in4_input.dev_attr); |
| 583 | device_create_file(&new_client->dev, |
| 584 | &sensor_dev_attr_in4_min.dev_attr); |
| 585 | device_create_file(&new_client->dev, |
| 586 | &sensor_dev_attr_in4_max.dev_attr); |
| 587 | device_create_file(&new_client->dev, |
| 588 | &sensor_dev_attr_in5_input.dev_attr); |
| 589 | device_create_file(&new_client->dev, |
| 590 | &sensor_dev_attr_in5_min.dev_attr); |
| 591 | device_create_file(&new_client->dev, |
| 592 | &sensor_dev_attr_in5_max.dev_attr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 593 | device_create_file(&new_client->dev, &dev_attr_temp1_input); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 594 | device_create_file(&new_client->dev, |
| 595 | &sensor_dev_attr_temp1_max.dev_attr); |
| 596 | device_create_file(&new_client->dev, |
| 597 | &sensor_dev_attr_temp1_max_hyst.dev_attr); |
| 598 | device_create_file(&new_client->dev, |
| 599 | &sensor_dev_attr_fan1_input.dev_attr); |
| 600 | device_create_file(&new_client->dev, |
| 601 | &sensor_dev_attr_fan1_div.dev_attr); |
| 602 | device_create_file(&new_client->dev, |
| 603 | &sensor_dev_attr_fan1_min.dev_attr); |
| 604 | device_create_file(&new_client->dev, |
| 605 | &sensor_dev_attr_fan2_input.dev_attr); |
| 606 | device_create_file(&new_client->dev, |
| 607 | &sensor_dev_attr_fan2_div.dev_attr); |
| 608 | device_create_file(&new_client->dev, |
| 609 | &sensor_dev_attr_fan2_min.dev_attr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 610 | device_create_file(&new_client->dev, &dev_attr_alarms); |
| 611 | device_create_file(&new_client->dev, &dev_attr_aout_output); |
| 612 | device_create_file(&new_client->dev, &dev_attr_chassis_clear); |
| 613 | device_create_file(&new_client->dev, &dev_attr_cpu0_vid); |
| 614 | |
| 615 | return 0; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 616 | |
| 617 | exit_detach: |
| 618 | i2c_detach_client(new_client); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 619 | exit_free: |
Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 620 | kfree(data); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 621 | exit: |
| 622 | return err; |
| 623 | } |
| 624 | |
| 625 | static int adm9240_attach_adapter(struct i2c_adapter *adapter) |
| 626 | { |
| 627 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 628 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 629 | return i2c_probe(adapter, &addr_data, adm9240_detect); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static int adm9240_detach_client(struct i2c_client *client) |
| 633 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 634 | struct adm9240_data *data = i2c_get_clientdata(client); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 635 | int err; |
| 636 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 637 | hwmon_device_unregister(data->class_dev); |
| 638 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 639 | if ((err = i2c_detach_client(client))) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 640 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 641 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 642 | kfree(data); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static void adm9240_init_client(struct i2c_client *client) |
| 647 | { |
| 648 | struct adm9240_data *data = i2c_get_clientdata(client); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 649 | u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG); |
| 650 | u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 651 | |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 652 | data->vrm = vid_which_vrm(); /* need this to report vid as mV */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 653 | |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 654 | dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10, |
| 655 | data->vrm % 10); |
| 656 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 657 | if (conf & 1) { /* measurement cycle running: report state */ |
| 658 | |
| 659 | dev_info(&client->dev, "status: config 0x%02x mode %u\n", |
| 660 | conf, mode); |
| 661 | |
| 662 | } else { /* cold start: open limits before starting chip */ |
| 663 | int i; |
| 664 | |
| 665 | for (i = 0; i < 6; i++) |
| 666 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 667 | i2c_smbus_write_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 668 | ADM9240_REG_IN_MIN(i), 0); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 669 | i2c_smbus_write_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 670 | ADM9240_REG_IN_MAX(i), 255); |
| 671 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 672 | i2c_smbus_write_byte_data(client, |
| 673 | ADM9240_REG_FAN_MIN(0), 255); |
| 674 | i2c_smbus_write_byte_data(client, |
| 675 | ADM9240_REG_FAN_MIN(1), 255); |
| 676 | i2c_smbus_write_byte_data(client, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 677 | ADM9240_REG_TEMP_MAX(0), 127); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 678 | i2c_smbus_write_byte_data(client, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 679 | ADM9240_REG_TEMP_MAX(1), 127); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 680 | |
| 681 | /* start measurement cycle */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 682 | i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 683 | |
| 684 | dev_info(&client->dev, "cold start: config was 0x%02x " |
| 685 | "mode %u\n", conf, mode); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | static struct adm9240_data *adm9240_update_device(struct device *dev) |
| 690 | { |
| 691 | struct i2c_client *client = to_i2c_client(dev); |
| 692 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 693 | int i; |
| 694 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 695 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 696 | |
| 697 | /* minimum measurement cycle: 1.75 seconds */ |
| 698 | if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4)) |
| 699 | || !data->valid) { |
| 700 | |
| 701 | for (i = 0; i < 6; i++) /* read voltages */ |
| 702 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 703 | data->in[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 704 | ADM9240_REG_IN(i)); |
| 705 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 706 | data->alarms = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 707 | ADM9240_REG_INT(0)) | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 708 | i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 709 | ADM9240_REG_INT(1)) << 8; |
| 710 | |
| 711 | /* read temperature: assume temperature changes less than |
| 712 | * 0.5'C per two measurement cycles thus ignore possible |
| 713 | * but unlikely aliasing error on lsb reading. --Grant */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 714 | data->temp = ((i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 715 | ADM9240_REG_TEMP) << 8) | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 716 | i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 717 | ADM9240_REG_TEMP_CONF)) / 128; |
| 718 | |
| 719 | for (i = 0; i < 2; i++) /* read fans */ |
| 720 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 721 | data->fan[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 722 | ADM9240_REG_FAN(i)); |
| 723 | |
| 724 | /* adjust fan clock divider on overflow */ |
| 725 | if (data->valid && data->fan[i] == 255 && |
| 726 | data->fan_div[i] < 3) { |
| 727 | |
| 728 | adm9240_write_fan_div(client, i, |
| 729 | ++data->fan_div[i]); |
| 730 | |
| 731 | /* adjust fan_min if active, but not to 0 */ |
| 732 | if (data->fan_min[i] < 255 && |
| 733 | data->fan_min[i] >= 2) |
| 734 | data->fan_min[i] /= 2; |
| 735 | } |
| 736 | } |
| 737 | data->last_updated_measure = jiffies; |
| 738 | } |
| 739 | |
| 740 | /* minimum config reading cycle: 300 seconds */ |
| 741 | if (time_after(jiffies, data->last_updated_config + (HZ * 300)) |
| 742 | || !data->valid) { |
| 743 | |
| 744 | for (i = 0; i < 6; i++) |
| 745 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 746 | data->in_min[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 747 | ADM9240_REG_IN_MIN(i)); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 748 | data->in_max[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 749 | ADM9240_REG_IN_MAX(i)); |
| 750 | } |
| 751 | for (i = 0; i < 2; i++) |
| 752 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 753 | data->fan_min[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 754 | ADM9240_REG_FAN_MIN(i)); |
| 755 | } |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 756 | data->temp_max[0] = i2c_smbus_read_byte_data(client, |
| 757 | ADM9240_REG_TEMP_MAX(0)); |
| 758 | data->temp_max[1] = i2c_smbus_read_byte_data(client, |
| 759 | ADM9240_REG_TEMP_MAX(1)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 760 | |
| 761 | /* read fan divs and 5-bit VID */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 762 | i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 763 | data->fan_div[0] = (i >> 4) & 3; |
| 764 | data->fan_div[1] = (i >> 6) & 3; |
| 765 | data->vid = i & 0x0f; |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 766 | data->vid |= (i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 767 | ADM9240_REG_VID4) & 1) << 4; |
| 768 | /* read analog out */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 769 | data->aout = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 770 | ADM9240_REG_ANALOG_OUT); |
| 771 | |
| 772 | data->last_updated_config = jiffies; |
| 773 | data->valid = 1; |
| 774 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame^] | 775 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 776 | return data; |
| 777 | } |
| 778 | |
| 779 | static int __init sensors_adm9240_init(void) |
| 780 | { |
| 781 | return i2c_add_driver(&adm9240_driver); |
| 782 | } |
| 783 | |
| 784 | static void __exit sensors_adm9240_exit(void) |
| 785 | { |
| 786 | i2c_del_driver(&adm9240_driver); |
| 787 | } |
| 788 | |
| 789 | MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, " |
| 790 | "Grant Coady <gcoady@gmail.com> and others"); |
| 791 | MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver"); |
| 792 | MODULE_LICENSE("GPL"); |
| 793 | |
| 794 | module_init(sensors_adm9240_init); |
| 795 | module_exit(sensors_adm9240_exit); |
| 796 | |