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> |
Grant Coady | 2ca7b96 | 2006-10-08 21:57:41 +0200 | [diff] [blame] | 8 | * Copyright (C) 2005 Grant Coady <gcoady.lk@gmail.com> with valuable |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 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 */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 55 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 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 | .attach_adapter = adm9240_attach_adapter, |
| 145 | .detach_client = adm9240_detach_client, |
| 146 | }; |
| 147 | |
| 148 | /* per client data */ |
| 149 | struct adm9240_data { |
| 150 | enum chips type; |
| 151 | struct i2c_client client; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 152 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 153 | struct mutex update_lock; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 154 | char valid; |
| 155 | unsigned long last_updated_measure; |
| 156 | unsigned long last_updated_config; |
| 157 | |
| 158 | u8 in[6]; /* ro in0_input */ |
| 159 | u8 in_max[6]; /* rw in0_max */ |
| 160 | u8 in_min[6]; /* rw in0_min */ |
| 161 | u8 fan[2]; /* ro fan1_input */ |
| 162 | u8 fan_min[2]; /* rw fan1_min */ |
| 163 | u8 fan_div[2]; /* rw fan1_div, read-only accessor */ |
| 164 | s16 temp; /* ro temp1_input, 9-bit sign-extended */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 165 | s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 166 | u16 alarms; /* ro alarms */ |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 167 | u8 aout; /* rw aout_output */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 168 | u8 vid; /* ro vid */ |
| 169 | u8 vrm; /* -- vrm set on startup, no accessor */ |
| 170 | }; |
| 171 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 172 | /*** sysfs accessors ***/ |
| 173 | |
| 174 | /* temperature */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 175 | static ssize_t show_temp(struct device *dev, struct device_attribute *dummy, |
| 176 | char *buf) |
| 177 | { |
| 178 | struct adm9240_data *data = adm9240_update_device(dev); |
| 179 | return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 180 | } |
| 181 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 182 | static ssize_t show_max(struct device *dev, struct device_attribute *devattr, |
| 183 | char *buf) |
| 184 | { |
| 185 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 186 | struct adm9240_data *data = adm9240_update_device(dev); |
| 187 | return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000); |
| 188 | } |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 189 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 190 | static ssize_t set_max(struct device *dev, struct device_attribute *devattr, |
| 191 | const char *buf, size_t count) |
| 192 | { |
| 193 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 194 | struct i2c_client *client = to_i2c_client(dev); |
| 195 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 196 | long val = simple_strtol(buf, NULL, 10); |
| 197 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 198 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 199 | data->temp_max[attr->index] = TEMP_TO_REG(val); |
| 200 | i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index), |
| 201 | data->temp_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 202 | mutex_unlock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 203 | return count; |
| 204 | } |
| 205 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 206 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 207 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 208 | show_max, set_max, 0); |
| 209 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, |
| 210 | show_max, set_max, 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 211 | |
| 212 | /* voltage */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 213 | static ssize_t show_in(struct device *dev, struct device_attribute *devattr, |
| 214 | char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 215 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 216 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 217 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 218 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index], |
| 219 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 220 | } |
| 221 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 222 | static ssize_t show_in_min(struct device *dev, |
| 223 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 224 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 225 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 226 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 227 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index], |
| 228 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 229 | } |
| 230 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 231 | static ssize_t show_in_max(struct device *dev, |
| 232 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 233 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 234 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 235 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 236 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index], |
| 237 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 238 | } |
| 239 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 240 | static ssize_t set_in_min(struct device *dev, |
| 241 | struct device_attribute *devattr, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 242 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 243 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 244 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 245 | struct i2c_client *client = to_i2c_client(dev); |
| 246 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 247 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 248 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 249 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 250 | data->in_min[attr->index] = IN_TO_REG(val, attr->index); |
| 251 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index), |
| 252 | data->in_min[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 253 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 254 | return count; |
| 255 | } |
| 256 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 257 | static ssize_t set_in_max(struct device *dev, |
| 258 | struct device_attribute *devattr, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 259 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 260 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 261 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 262 | struct i2c_client *client = to_i2c_client(dev); |
| 263 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 264 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 265 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 266 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 267 | data->in_max[attr->index] = IN_TO_REG(val, attr->index); |
| 268 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index), |
| 269 | data->in_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 270 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 271 | return count; |
| 272 | } |
| 273 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 274 | #define vin(nr) \ |
| 275 | static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \ |
| 276 | show_in, NULL, nr); \ |
| 277 | static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \ |
| 278 | show_in_min, set_in_min, nr); \ |
| 279 | static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \ |
| 280 | show_in_max, set_in_max, nr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 281 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 282 | vin(0); |
| 283 | vin(1); |
| 284 | vin(2); |
| 285 | vin(3); |
| 286 | vin(4); |
| 287 | vin(5); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 288 | |
| 289 | /* fans */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 290 | static ssize_t show_fan(struct device *dev, |
| 291 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 292 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 293 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 294 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 295 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], |
| 296 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 297 | } |
| 298 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 299 | static ssize_t show_fan_min(struct device *dev, |
| 300 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 301 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 302 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 303 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 304 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index], |
| 305 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 306 | } |
| 307 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 308 | static ssize_t show_fan_div(struct device *dev, |
| 309 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 310 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 311 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 312 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 313 | return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* write new fan div, callers must hold data->update_lock */ |
| 317 | static void adm9240_write_fan_div(struct i2c_client *client, int nr, |
| 318 | u8 fan_div) |
| 319 | { |
| 320 | u8 reg, old, shift = (nr + 2) * 2; |
| 321 | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 322 | reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 323 | old = (reg >> shift) & 3; |
| 324 | reg &= ~(3 << shift); |
| 325 | reg |= (fan_div << shift); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 326 | i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 327 | dev_dbg(&client->dev, "fan%d clock divider changed from %u " |
| 328 | "to %u\n", nr + 1, 1 << old, 1 << fan_div); |
| 329 | } |
| 330 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 331 | /* |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 332 | * set fan speed low limit: |
| 333 | * |
| 334 | * - value is zero: disable fan speed low limit alarm |
| 335 | * |
| 336 | * - value is below fan speed measurement range: enable fan speed low |
| 337 | * limit alarm to be asserted while fan speed too slow to measure |
| 338 | * |
| 339 | * - otherwise: select fan clock divider to suit fan speed low limit, |
| 340 | * measurement code may adjust registers to ensure fan speed reading |
| 341 | */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 342 | static ssize_t set_fan_min(struct device *dev, |
| 343 | struct device_attribute *devattr, |
| 344 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 345 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 346 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 347 | struct i2c_client *client = to_i2c_client(dev); |
| 348 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 349 | unsigned long val = simple_strtoul(buf, NULL, 10); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 350 | int nr = attr->index; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 351 | u8 new_div; |
| 352 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 353 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 354 | |
| 355 | if (!val) { |
| 356 | data->fan_min[nr] = 255; |
| 357 | new_div = data->fan_div[nr]; |
| 358 | |
| 359 | dev_dbg(&client->dev, "fan%u low limit set disabled\n", |
| 360 | nr + 1); |
| 361 | |
| 362 | } else if (val < 1350000 / (8 * 254)) { |
| 363 | new_div = 3; |
| 364 | data->fan_min[nr] = 254; |
| 365 | |
| 366 | dev_dbg(&client->dev, "fan%u low limit set minimum %u\n", |
| 367 | nr + 1, FAN_FROM_REG(254, 1 << new_div)); |
| 368 | |
| 369 | } else { |
| 370 | unsigned int new_min = 1350000 / val; |
| 371 | |
| 372 | new_div = 0; |
| 373 | while (new_min > 192 && new_div < 3) { |
| 374 | new_div++; |
| 375 | new_min /= 2; |
| 376 | } |
| 377 | if (!new_min) /* keep > 0 */ |
| 378 | new_min++; |
| 379 | |
| 380 | data->fan_min[nr] = new_min; |
| 381 | |
| 382 | dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n", |
| 383 | nr + 1, FAN_FROM_REG(new_min, 1 << new_div)); |
| 384 | } |
| 385 | |
| 386 | if (new_div != data->fan_div[nr]) { |
| 387 | data->fan_div[nr] = new_div; |
| 388 | adm9240_write_fan_div(client, nr, new_div); |
| 389 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 390 | i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr), |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 391 | data->fan_min[nr]); |
| 392 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 393 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 394 | return count; |
| 395 | } |
| 396 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 397 | #define fan(nr) \ |
| 398 | static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \ |
| 399 | show_fan, NULL, nr - 1); \ |
| 400 | static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \ |
| 401 | show_fan_div, NULL, nr - 1); \ |
| 402 | static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \ |
| 403 | show_fan_min, set_fan_min, nr - 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 404 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 405 | fan(1); |
| 406 | fan(2); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 407 | |
| 408 | /* alarms */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 409 | static ssize_t show_alarms(struct device *dev, |
| 410 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 411 | { |
| 412 | struct adm9240_data *data = adm9240_update_device(dev); |
| 413 | return sprintf(buf, "%u\n", data->alarms); |
| 414 | } |
| 415 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 416 | |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 417 | static ssize_t show_alarm(struct device *dev, |
| 418 | struct device_attribute *attr, char *buf) |
| 419 | { |
| 420 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 421 | struct adm9240_data *data = adm9240_update_device(dev); |
| 422 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 423 | } |
| 424 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 425 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 426 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 427 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 428 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 429 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); |
| 430 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 431 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 432 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 433 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 434 | /* vid */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 435 | static ssize_t show_vid(struct device *dev, |
| 436 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 437 | { |
| 438 | struct adm9240_data *data = adm9240_update_device(dev); |
| 439 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 440 | } |
| 441 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 442 | |
| 443 | /* analog output */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 444 | static ssize_t show_aout(struct device *dev, |
| 445 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 446 | { |
| 447 | struct adm9240_data *data = adm9240_update_device(dev); |
| 448 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); |
| 449 | } |
| 450 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 451 | static ssize_t set_aout(struct device *dev, |
| 452 | struct device_attribute *attr, |
| 453 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 454 | { |
| 455 | struct i2c_client *client = to_i2c_client(dev); |
| 456 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 457 | unsigned long val = simple_strtol(buf, NULL, 10); |
| 458 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 459 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 460 | data->aout = AOUT_TO_REG(val); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 461 | i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 462 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 463 | return count; |
| 464 | } |
| 465 | static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout); |
| 466 | |
| 467 | /* chassis_clear */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 468 | static ssize_t chassis_clear(struct device *dev, |
| 469 | struct device_attribute *attr, |
| 470 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 471 | { |
| 472 | struct i2c_client *client = to_i2c_client(dev); |
| 473 | unsigned long val = simple_strtol(buf, NULL, 10); |
| 474 | |
| 475 | if (val == 1) { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 476 | i2c_smbus_write_byte_data(client, |
| 477 | ADM9240_REG_CHASSIS_CLEAR, 0x80); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 478 | dev_dbg(&client->dev, "chassis intrusion latch cleared\n"); |
| 479 | } |
| 480 | return count; |
| 481 | } |
| 482 | static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear); |
| 483 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 484 | static struct attribute *adm9240_attributes[] = { |
| 485 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 486 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 487 | &sensor_dev_attr_in0_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 488 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 489 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 490 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 491 | &sensor_dev_attr_in1_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 492 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 493 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 494 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 495 | &sensor_dev_attr_in2_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 496 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 497 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 498 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 499 | &sensor_dev_attr_in3_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 500 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 501 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 502 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 503 | &sensor_dev_attr_in4_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 504 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 505 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 506 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 507 | &sensor_dev_attr_in5_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 508 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 509 | &dev_attr_temp1_input.attr, |
| 510 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 511 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 512 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 513 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 514 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 515 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 516 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 517 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 518 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
| 519 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 520 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 521 | &dev_attr_alarms.attr, |
| 522 | &dev_attr_aout_output.attr, |
| 523 | &dev_attr_chassis_clear.attr, |
| 524 | &dev_attr_cpu0_vid.attr, |
| 525 | NULL |
| 526 | }; |
| 527 | |
| 528 | static const struct attribute_group adm9240_group = { |
| 529 | .attrs = adm9240_attributes, |
| 530 | }; |
| 531 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 532 | |
| 533 | /*** sensor chip detect and driver install ***/ |
| 534 | |
| 535 | static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind) |
| 536 | { |
| 537 | struct i2c_client *new_client; |
| 538 | struct adm9240_data *data; |
| 539 | int err = 0; |
| 540 | const char *name = ""; |
| 541 | u8 man_id, die_rev; |
| 542 | |
| 543 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 544 | goto exit; |
| 545 | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 546 | if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 547 | err = -ENOMEM; |
| 548 | goto exit; |
| 549 | } |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 550 | |
| 551 | new_client = &data->client; |
| 552 | i2c_set_clientdata(new_client, data); |
| 553 | new_client->addr = address; |
| 554 | new_client->adapter = adapter; |
| 555 | new_client->driver = &adm9240_driver; |
| 556 | new_client->flags = 0; |
| 557 | |
| 558 | if (kind == 0) { |
| 559 | kind = adm9240; |
| 560 | } |
| 561 | |
| 562 | if (kind < 0) { |
| 563 | |
| 564 | /* verify chip: reg address should match i2c address */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 565 | if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 566 | != address) { |
| 567 | dev_err(&adapter->dev, "detect fail: address match, " |
| 568 | "0x%02x\n", address); |
| 569 | goto exit_free; |
| 570 | } |
| 571 | |
| 572 | /* check known chip manufacturer */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 573 | man_id = i2c_smbus_read_byte_data(new_client, |
| 574 | ADM9240_REG_MAN_ID); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 575 | if (man_id == 0x23) { |
| 576 | kind = adm9240; |
| 577 | } else if (man_id == 0xda) { |
| 578 | kind = ds1780; |
| 579 | } else if (man_id == 0x01) { |
| 580 | kind = lm81; |
| 581 | } else { |
| 582 | dev_err(&adapter->dev, "detect fail: unknown manuf, " |
| 583 | "0x%02x\n", man_id); |
| 584 | goto exit_free; |
| 585 | } |
| 586 | |
| 587 | /* successful detect, print chip info */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 588 | die_rev = i2c_smbus_read_byte_data(new_client, |
| 589 | ADM9240_REG_DIE_REV); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 590 | dev_info(&adapter->dev, "found %s revision %u\n", |
| 591 | man_id == 0x23 ? "ADM9240" : |
| 592 | man_id == 0xda ? "DS1780" : "LM81", die_rev); |
| 593 | } |
| 594 | |
| 595 | /* either forced or detected chip kind */ |
| 596 | if (kind == adm9240) { |
| 597 | name = "adm9240"; |
| 598 | } else if (kind == ds1780) { |
| 599 | name = "ds1780"; |
| 600 | } else if (kind == lm81) { |
| 601 | name = "lm81"; |
| 602 | } |
| 603 | |
| 604 | /* fill in the remaining client fields and attach */ |
| 605 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
| 606 | data->type = kind; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 607 | mutex_init(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 608 | |
| 609 | if ((err = i2c_attach_client(new_client))) |
| 610 | goto exit_free; |
| 611 | |
| 612 | adm9240_init_client(new_client); |
| 613 | |
| 614 | /* populate sysfs filesystem */ |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 615 | if ((err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group))) |
| 616 | goto exit_detach; |
| 617 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 618 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 619 | if (IS_ERR(data->hwmon_dev)) { |
| 620 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 621 | goto exit_remove; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 622 | } |
| 623 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 624 | return 0; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 625 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 626 | exit_remove: |
| 627 | sysfs_remove_group(&new_client->dev.kobj, &adm9240_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 628 | exit_detach: |
| 629 | i2c_detach_client(new_client); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 630 | exit_free: |
Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 631 | kfree(data); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 632 | exit: |
| 633 | return err; |
| 634 | } |
| 635 | |
| 636 | static int adm9240_attach_adapter(struct i2c_adapter *adapter) |
| 637 | { |
| 638 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 639 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 640 | return i2c_probe(adapter, &addr_data, adm9240_detect); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static int adm9240_detach_client(struct i2c_client *client) |
| 644 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 645 | struct adm9240_data *data = i2c_get_clientdata(client); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 646 | int err; |
| 647 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 648 | hwmon_device_unregister(data->hwmon_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 649 | sysfs_remove_group(&client->dev.kobj, &adm9240_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 650 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 651 | if ((err = i2c_detach_client(client))) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 652 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 653 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 654 | kfree(data); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static void adm9240_init_client(struct i2c_client *client) |
| 659 | { |
| 660 | struct adm9240_data *data = i2c_get_clientdata(client); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 661 | u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG); |
| 662 | 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] | 663 | |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 664 | data->vrm = vid_which_vrm(); /* need this to report vid as mV */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 665 | |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 666 | dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10, |
| 667 | data->vrm % 10); |
| 668 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 669 | if (conf & 1) { /* measurement cycle running: report state */ |
| 670 | |
| 671 | dev_info(&client->dev, "status: config 0x%02x mode %u\n", |
| 672 | conf, mode); |
| 673 | |
| 674 | } else { /* cold start: open limits before starting chip */ |
| 675 | int i; |
| 676 | |
| 677 | for (i = 0; i < 6; i++) |
| 678 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 679 | i2c_smbus_write_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 680 | ADM9240_REG_IN_MIN(i), 0); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 681 | i2c_smbus_write_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 682 | ADM9240_REG_IN_MAX(i), 255); |
| 683 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 684 | i2c_smbus_write_byte_data(client, |
| 685 | ADM9240_REG_FAN_MIN(0), 255); |
| 686 | i2c_smbus_write_byte_data(client, |
| 687 | ADM9240_REG_FAN_MIN(1), 255); |
| 688 | i2c_smbus_write_byte_data(client, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 689 | ADM9240_REG_TEMP_MAX(0), 127); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 690 | i2c_smbus_write_byte_data(client, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 691 | ADM9240_REG_TEMP_MAX(1), 127); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 692 | |
| 693 | /* start measurement cycle */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 694 | i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 695 | |
| 696 | dev_info(&client->dev, "cold start: config was 0x%02x " |
| 697 | "mode %u\n", conf, mode); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | static struct adm9240_data *adm9240_update_device(struct device *dev) |
| 702 | { |
| 703 | struct i2c_client *client = to_i2c_client(dev); |
| 704 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 705 | int i; |
| 706 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 707 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 708 | |
| 709 | /* minimum measurement cycle: 1.75 seconds */ |
| 710 | if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4)) |
| 711 | || !data->valid) { |
| 712 | |
| 713 | for (i = 0; i < 6; i++) /* read voltages */ |
| 714 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 715 | data->in[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 716 | ADM9240_REG_IN(i)); |
| 717 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 718 | data->alarms = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 719 | ADM9240_REG_INT(0)) | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 720 | i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 721 | ADM9240_REG_INT(1)) << 8; |
| 722 | |
| 723 | /* read temperature: assume temperature changes less than |
| 724 | * 0.5'C per two measurement cycles thus ignore possible |
| 725 | * but unlikely aliasing error on lsb reading. --Grant */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 726 | data->temp = ((i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 727 | ADM9240_REG_TEMP) << 8) | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 728 | i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 729 | ADM9240_REG_TEMP_CONF)) / 128; |
| 730 | |
| 731 | for (i = 0; i < 2; i++) /* read fans */ |
| 732 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 733 | data->fan[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 734 | ADM9240_REG_FAN(i)); |
| 735 | |
| 736 | /* adjust fan clock divider on overflow */ |
| 737 | if (data->valid && data->fan[i] == 255 && |
| 738 | data->fan_div[i] < 3) { |
| 739 | |
| 740 | adm9240_write_fan_div(client, i, |
| 741 | ++data->fan_div[i]); |
| 742 | |
| 743 | /* adjust fan_min if active, but not to 0 */ |
| 744 | if (data->fan_min[i] < 255 && |
| 745 | data->fan_min[i] >= 2) |
| 746 | data->fan_min[i] /= 2; |
| 747 | } |
| 748 | } |
| 749 | data->last_updated_measure = jiffies; |
| 750 | } |
| 751 | |
| 752 | /* minimum config reading cycle: 300 seconds */ |
| 753 | if (time_after(jiffies, data->last_updated_config + (HZ * 300)) |
| 754 | || !data->valid) { |
| 755 | |
| 756 | for (i = 0; i < 6; i++) |
| 757 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 758 | data->in_min[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 759 | ADM9240_REG_IN_MIN(i)); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 760 | data->in_max[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 761 | ADM9240_REG_IN_MAX(i)); |
| 762 | } |
| 763 | for (i = 0; i < 2; i++) |
| 764 | { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 765 | data->fan_min[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 766 | ADM9240_REG_FAN_MIN(i)); |
| 767 | } |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 768 | data->temp_max[0] = i2c_smbus_read_byte_data(client, |
| 769 | ADM9240_REG_TEMP_MAX(0)); |
| 770 | data->temp_max[1] = i2c_smbus_read_byte_data(client, |
| 771 | ADM9240_REG_TEMP_MAX(1)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 772 | |
| 773 | /* read fan divs and 5-bit VID */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 774 | i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 775 | data->fan_div[0] = (i >> 4) & 3; |
| 776 | data->fan_div[1] = (i >> 6) & 3; |
| 777 | data->vid = i & 0x0f; |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 778 | data->vid |= (i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 779 | ADM9240_REG_VID4) & 1) << 4; |
| 780 | /* read analog out */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 781 | data->aout = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 782 | ADM9240_REG_ANALOG_OUT); |
| 783 | |
| 784 | data->last_updated_config = jiffies; |
| 785 | data->valid = 1; |
| 786 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 787 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 788 | return data; |
| 789 | } |
| 790 | |
| 791 | static int __init sensors_adm9240_init(void) |
| 792 | { |
| 793 | return i2c_add_driver(&adm9240_driver); |
| 794 | } |
| 795 | |
| 796 | static void __exit sensors_adm9240_exit(void) |
| 797 | { |
| 798 | i2c_del_driver(&adm9240_driver); |
| 799 | } |
| 800 | |
| 801 | MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, " |
Grant Coady | 2ca7b96 | 2006-10-08 21:57:41 +0200 | [diff] [blame] | 802 | "Grant Coady <gcoady.lk@gmail.com> and others"); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 803 | MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver"); |
| 804 | MODULE_LICENSE("GPL"); |
| 805 | |
| 806 | module_init(sensors_adm9240_init); |
| 807 | module_exit(sensors_adm9240_exit); |
| 808 | |