Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | adm1031.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | monitoring |
| 4 | Based on lm75.c and lm85.c |
| 5 | Supports adm1030 / adm1031 |
| 6 | Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org> |
| 7 | Reworked by Jean Delvare <khali@linux-fr.org> |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 29 | #include <linux/hwmon.h> |
| 30 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | /* Following macros takes channel parameter starting from 0 to 2 */ |
| 34 | #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr)) |
| 35 | #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr)) |
| 36 | #define ADM1031_REG_PWM (0x22) |
| 37 | #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr)) |
| 38 | |
| 39 | #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4*(nr)) |
| 40 | #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4*(nr)) |
| 41 | #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4*(nr)) |
| 42 | |
| 43 | #define ADM1031_REG_TEMP(nr) (0xa + (nr)) |
| 44 | #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr)) |
| 45 | |
| 46 | #define ADM1031_REG_STATUS(nr) (0x2 + (nr)) |
| 47 | |
| 48 | #define ADM1031_REG_CONF1 0x0 |
| 49 | #define ADM1031_REG_CONF2 0x1 |
| 50 | #define ADM1031_REG_EXT_TEMP 0x6 |
| 51 | |
| 52 | #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */ |
| 53 | #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */ |
| 54 | #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */ |
| 55 | |
| 56 | #define ADM1031_CONF2_PWM1_ENABLE 0x01 |
| 57 | #define ADM1031_CONF2_PWM2_ENABLE 0x02 |
| 58 | #define ADM1031_CONF2_TACH1_ENABLE 0x04 |
| 59 | #define ADM1031_CONF2_TACH2_ENABLE 0x08 |
| 60 | #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan)) |
| 61 | |
| 62 | /* Addresses to scan */ |
| 63 | static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 66 | I2C_CLIENT_INSMOD_2(adm1030, adm1031); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | typedef u8 auto_chan_table_t[8][2]; |
| 69 | |
| 70 | /* Each client has this additional data */ |
| 71 | struct adm1031_data { |
| 72 | struct i2c_client client; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 73 | struct class_device *class_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 74 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int chip_type; |
| 76 | char valid; /* !=0 if following fields are valid */ |
| 77 | unsigned long last_updated; /* In jiffies */ |
| 78 | /* The chan_select_table contains the possible configurations for |
| 79 | * auto fan control. |
| 80 | */ |
| 81 | auto_chan_table_t *chan_select_table; |
| 82 | u16 alarm; |
| 83 | u8 conf1; |
| 84 | u8 conf2; |
| 85 | u8 fan[2]; |
| 86 | u8 fan_div[2]; |
| 87 | u8 fan_min[2]; |
| 88 | u8 pwm[2]; |
| 89 | u8 old_pwm[2]; |
| 90 | s8 temp[3]; |
| 91 | u8 ext_temp[3]; |
| 92 | u8 auto_temp[3]; |
| 93 | u8 auto_temp_min[3]; |
| 94 | u8 auto_temp_off[3]; |
| 95 | u8 auto_temp_max[3]; |
| 96 | s8 temp_min[3]; |
| 97 | s8 temp_max[3]; |
| 98 | s8 temp_crit[3]; |
| 99 | }; |
| 100 | |
| 101 | static int adm1031_attach_adapter(struct i2c_adapter *adapter); |
| 102 | static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind); |
| 103 | static void adm1031_init_client(struct i2c_client *client); |
| 104 | static int adm1031_detach_client(struct i2c_client *client); |
| 105 | static struct adm1031_data *adm1031_update_device(struct device *dev); |
| 106 | |
| 107 | /* This is the driver that will be inserted */ |
| 108 | static struct i2c_driver adm1031_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 109 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 110 | .name = "adm1031", |
| 111 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | .attach_adapter = adm1031_attach_adapter, |
| 113 | .detach_client = adm1031_detach_client, |
| 114 | }; |
| 115 | |
| 116 | static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg) |
| 117 | { |
| 118 | return i2c_smbus_read_byte_data(client, reg); |
| 119 | } |
| 120 | |
| 121 | static inline int |
| 122 | adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value) |
| 123 | { |
| 124 | return i2c_smbus_write_byte_data(client, reg, value); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \ |
| 129 | ((val + 500) / 1000))) |
| 130 | |
| 131 | #define TEMP_FROM_REG(val) ((val) * 1000) |
| 132 | |
| 133 | #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125) |
| 134 | |
| 135 | #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0) |
| 136 | |
| 137 | static int FAN_TO_REG(int reg, int div) |
| 138 | { |
| 139 | int tmp; |
| 140 | tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div); |
| 141 | return tmp > 255 ? 255 : tmp; |
| 142 | } |
| 143 | |
| 144 | #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6)) |
| 145 | |
| 146 | #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4) |
| 147 | #define PWM_FROM_REG(val) ((val) << 4) |
| 148 | |
| 149 | #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7) |
| 150 | #define FAN_CHAN_TO_REG(val, reg) \ |
| 151 | (((reg) & 0x1F) | (((val) << 5) & 0xe0)) |
| 152 | |
| 153 | #define AUTO_TEMP_MIN_TO_REG(val, reg) \ |
| 154 | ((((val)/500) & 0xf8)|((reg) & 0x7)) |
| 155 | #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7))) |
| 156 | #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2)) |
| 157 | |
| 158 | #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2) |
| 159 | |
| 160 | #define AUTO_TEMP_OFF_FROM_REG(reg) \ |
| 161 | (AUTO_TEMP_MIN_FROM_REG(reg) - 5000) |
| 162 | |
| 163 | #define AUTO_TEMP_MAX_FROM_REG(reg) \ |
| 164 | (AUTO_TEMP_RANGE_FROM_REG(reg) + \ |
| 165 | AUTO_TEMP_MIN_FROM_REG(reg)) |
| 166 | |
| 167 | static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm) |
| 168 | { |
| 169 | int ret; |
| 170 | int range = val - AUTO_TEMP_MIN_FROM_REG(reg); |
| 171 | |
| 172 | range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm); |
| 173 | ret = ((reg & 0xf8) | |
| 174 | (range < 10000 ? 0 : |
| 175 | range < 20000 ? 1 : |
| 176 | range < 40000 ? 2 : range < 80000 ? 3 : 4)); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | /* FAN auto control */ |
| 181 | #define GET_FAN_AUTO_BITFIELD(data, idx) \ |
| 182 | (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2] |
| 183 | |
| 184 | /* The tables below contains the possible values for the auto fan |
| 185 | * control bitfields. the index in the table is the register value. |
| 186 | * MSb is the auto fan control enable bit, so the four first entries |
| 187 | * in the table disables auto fan control when both bitfields are zero. |
| 188 | */ |
| 189 | static auto_chan_table_t auto_channel_select_table_adm1031 = { |
| 190 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, |
| 191 | {2 /*0b010 */ , 4 /*0b100 */ }, |
| 192 | {2 /*0b010 */ , 2 /*0b010 */ }, |
| 193 | {4 /*0b100 */ , 4 /*0b100 */ }, |
| 194 | {7 /*0b111 */ , 7 /*0b111 */ }, |
| 195 | }; |
| 196 | |
| 197 | static auto_chan_table_t auto_channel_select_table_adm1030 = { |
| 198 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, |
| 199 | {2 /*0b10 */ , 0}, |
| 200 | {0xff /*invalid */ , 0}, |
| 201 | {0xff /*invalid */ , 0}, |
| 202 | {3 /*0b11 */ , 0}, |
| 203 | }; |
| 204 | |
| 205 | /* That function checks if a bitfield is valid and returns the other bitfield |
| 206 | * nearest match if no exact match where found. |
| 207 | */ |
| 208 | static int |
| 209 | get_fan_auto_nearest(struct adm1031_data *data, |
| 210 | int chan, u8 val, u8 reg, u8 * new_reg) |
| 211 | { |
| 212 | int i; |
| 213 | int first_match = -1, exact_match = -1; |
| 214 | u8 other_reg_val = |
| 215 | (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1]; |
| 216 | |
| 217 | if (val == 0) { |
| 218 | *new_reg = 0; |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | for (i = 0; i < 8; i++) { |
| 223 | if ((val == (*data->chan_select_table)[i][chan]) && |
| 224 | ((*data->chan_select_table)[i][chan ? 0 : 1] == |
| 225 | other_reg_val)) { |
| 226 | /* We found an exact match */ |
| 227 | exact_match = i; |
| 228 | break; |
| 229 | } else if (val == (*data->chan_select_table)[i][chan] && |
| 230 | first_match == -1) { |
| 231 | /* Save the first match in case of an exact match has not been |
| 232 | * found |
| 233 | */ |
| 234 | first_match = i; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (exact_match >= 0) { |
| 239 | *new_reg = exact_match; |
| 240 | } else if (first_match >= 0) { |
| 241 | *new_reg = first_match; |
| 242 | } else { |
| 243 | return -EINVAL; |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static ssize_t show_fan_auto_channel(struct device *dev, char *buf, int nr) |
| 249 | { |
| 250 | struct adm1031_data *data = adm1031_update_device(dev); |
| 251 | return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr)); |
| 252 | } |
| 253 | |
| 254 | static ssize_t |
| 255 | set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr) |
| 256 | { |
| 257 | struct i2c_client *client = to_i2c_client(dev); |
| 258 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 259 | int val = simple_strtol(buf, NULL, 10); |
| 260 | u8 reg; |
| 261 | int ret; |
| 262 | u8 old_fan_mode; |
| 263 | |
| 264 | old_fan_mode = data->conf1; |
| 265 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 266 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, ®))) { |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 269 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return ret; |
| 271 | } |
| 272 | if (((data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1)) & ADM1031_CONF1_AUTO_MODE) ^ |
| 273 | (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) { |
| 274 | if (data->conf1 & ADM1031_CONF1_AUTO_MODE){ |
| 275 | /* Switch to Auto Fan Mode |
| 276 | * Save PWM registers |
| 277 | * Set PWM registers to 33% Both */ |
| 278 | data->old_pwm[0] = data->pwm[0]; |
| 279 | data->old_pwm[1] = data->pwm[1]; |
| 280 | adm1031_write_value(client, ADM1031_REG_PWM, 0x55); |
| 281 | } else { |
| 282 | /* Switch to Manual Mode */ |
| 283 | data->pwm[0] = data->old_pwm[0]; |
| 284 | data->pwm[1] = data->old_pwm[1]; |
| 285 | /* Restore PWM registers */ |
| 286 | adm1031_write_value(client, ADM1031_REG_PWM, |
| 287 | data->pwm[0] | (data->pwm[1] << 4)); |
| 288 | } |
| 289 | } |
| 290 | data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1); |
| 291 | adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 292 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return count; |
| 294 | } |
| 295 | |
| 296 | #define fan_auto_channel_offset(offset) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 297 | static ssize_t show_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { \ |
| 299 | return show_fan_auto_channel(dev, buf, offset - 1); \ |
| 300 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 301 | static ssize_t set_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | const char *buf, size_t count) \ |
| 303 | { \ |
| 304 | return set_fan_auto_channel(dev, buf, count, offset - 1); \ |
| 305 | } \ |
| 306 | static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR, \ |
| 307 | show_fan_auto_channel_##offset, \ |
| 308 | set_fan_auto_channel_##offset) |
| 309 | |
| 310 | fan_auto_channel_offset(1); |
| 311 | fan_auto_channel_offset(2); |
| 312 | |
| 313 | /* Auto Temps */ |
| 314 | static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr) |
| 315 | { |
| 316 | struct adm1031_data *data = adm1031_update_device(dev); |
| 317 | return sprintf(buf, "%d\n", |
| 318 | AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr])); |
| 319 | } |
| 320 | static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr) |
| 321 | { |
| 322 | struct adm1031_data *data = adm1031_update_device(dev); |
| 323 | return sprintf(buf, "%d\n", |
| 324 | AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr])); |
| 325 | } |
| 326 | static ssize_t |
| 327 | set_auto_temp_min(struct device *dev, const char *buf, size_t count, int nr) |
| 328 | { |
| 329 | struct i2c_client *client = to_i2c_client(dev); |
| 330 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 331 | int val = simple_strtol(buf, NULL, 10); |
| 332 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 333 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]); |
| 335 | adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr), |
| 336 | data->auto_temp[nr]); |
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 | static ssize_t show_auto_temp_max(struct device *dev, char *buf, int nr) |
| 341 | { |
| 342 | struct adm1031_data *data = adm1031_update_device(dev); |
| 343 | return sprintf(buf, "%d\n", |
| 344 | AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr])); |
| 345 | } |
| 346 | static ssize_t |
| 347 | set_auto_temp_max(struct device *dev, const char *buf, size_t count, int nr) |
| 348 | { |
| 349 | struct i2c_client *client = to_i2c_client(dev); |
| 350 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 351 | int val = simple_strtol(buf, NULL, 10); |
| 352 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 353 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]); |
| 355 | adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr), |
| 356 | data->temp_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 357 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return count; |
| 359 | } |
| 360 | |
| 361 | #define auto_temp_reg(offset) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 362 | static ssize_t show_auto_temp_##offset##_off (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { \ |
| 364 | return show_auto_temp_off(dev, buf, offset - 1); \ |
| 365 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 366 | static ssize_t show_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { \ |
| 368 | return show_auto_temp_min(dev, buf, offset - 1); \ |
| 369 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 370 | static ssize_t show_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | { \ |
| 372 | return show_auto_temp_max(dev, buf, offset - 1); \ |
| 373 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 374 | static ssize_t set_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | const char *buf, size_t count) \ |
| 376 | { \ |
| 377 | return set_auto_temp_min(dev, buf, count, offset - 1); \ |
| 378 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 379 | static ssize_t set_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | const char *buf, size_t count) \ |
| 381 | { \ |
| 382 | return set_auto_temp_max(dev, buf, count, offset - 1); \ |
| 383 | } \ |
| 384 | static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \ |
| 385 | show_auto_temp_##offset##_off, NULL); \ |
| 386 | static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \ |
| 387 | show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\ |
| 388 | static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \ |
| 389 | show_auto_temp_##offset##_max, set_auto_temp_##offset##_max) |
| 390 | |
| 391 | auto_temp_reg(1); |
| 392 | auto_temp_reg(2); |
| 393 | auto_temp_reg(3); |
| 394 | |
| 395 | /* pwm */ |
| 396 | static ssize_t show_pwm(struct device *dev, char *buf, int nr) |
| 397 | { |
| 398 | struct adm1031_data *data = adm1031_update_device(dev); |
| 399 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr])); |
| 400 | } |
| 401 | static ssize_t |
| 402 | set_pwm(struct device *dev, const char *buf, size_t count, int nr) |
| 403 | { |
| 404 | struct i2c_client *client = to_i2c_client(dev); |
| 405 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 406 | int val = simple_strtol(buf, NULL, 10); |
| 407 | int reg; |
| 408 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 409 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) && |
| 411 | (((val>>4) & 0xf) != 5)) { |
| 412 | /* In automatic mode, the only PWM accepted is 33% */ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 413 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | return -EINVAL; |
| 415 | } |
| 416 | data->pwm[nr] = PWM_TO_REG(val); |
| 417 | reg = adm1031_read_value(client, ADM1031_REG_PWM); |
| 418 | adm1031_write_value(client, ADM1031_REG_PWM, |
| 419 | nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf) |
| 420 | : (data->pwm[nr] & 0xf) | (reg & 0xf0)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 421 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | return count; |
| 423 | } |
| 424 | |
| 425 | #define pwm_reg(offset) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 426 | static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { \ |
| 428 | return show_pwm(dev, buf, offset - 1); \ |
| 429 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 430 | static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | const char *buf, size_t count) \ |
| 432 | { \ |
| 433 | return set_pwm(dev, buf, count, offset - 1); \ |
| 434 | } \ |
| 435 | static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ |
| 436 | show_pwm_##offset, set_pwm_##offset) |
| 437 | |
| 438 | pwm_reg(1); |
| 439 | pwm_reg(2); |
| 440 | |
| 441 | /* Fans */ |
| 442 | |
| 443 | /* |
| 444 | * That function checks the cases where the fan reading is not |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 445 | * relevant. It is used to provide 0 as fan reading when the fan is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | * not supposed to run |
| 447 | */ |
| 448 | static int trust_fan_readings(struct adm1031_data *data, int chan) |
| 449 | { |
| 450 | int res = 0; |
| 451 | |
| 452 | if (data->conf1 & ADM1031_CONF1_AUTO_MODE) { |
| 453 | switch (data->conf1 & 0x60) { |
| 454 | case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */ |
| 455 | res = data->temp[chan+1] >= |
| 456 | AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]); |
| 457 | break; |
| 458 | case 0x20: /* remote temp1 controls both fans */ |
| 459 | res = |
| 460 | data->temp[1] >= |
| 461 | AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]); |
| 462 | break; |
| 463 | case 0x40: /* remote temp2 controls both fans */ |
| 464 | res = |
| 465 | data->temp[2] >= |
| 466 | AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]); |
| 467 | break; |
| 468 | case 0x60: /* max controls both fans */ |
| 469 | res = |
| 470 | data->temp[0] >= |
| 471 | AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0]) |
| 472 | || data->temp[1] >= |
| 473 | AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]) |
| 474 | || (data->chip_type == adm1031 |
| 475 | && data->temp[2] >= |
| 476 | AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2])); |
| 477 | break; |
| 478 | } |
| 479 | } else { |
| 480 | res = data->pwm[chan] > 0; |
| 481 | } |
| 482 | return res; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | static ssize_t show_fan(struct device *dev, char *buf, int nr) |
| 487 | { |
| 488 | struct adm1031_data *data = adm1031_update_device(dev); |
| 489 | int value; |
| 490 | |
| 491 | value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr], |
| 492 | FAN_DIV_FROM_REG(data->fan_div[nr])) : 0; |
| 493 | return sprintf(buf, "%d\n", value); |
| 494 | } |
| 495 | |
| 496 | static ssize_t show_fan_div(struct device *dev, char *buf, int nr) |
| 497 | { |
| 498 | struct adm1031_data *data = adm1031_update_device(dev); |
| 499 | return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr])); |
| 500 | } |
| 501 | static ssize_t show_fan_min(struct device *dev, char *buf, int nr) |
| 502 | { |
| 503 | struct adm1031_data *data = adm1031_update_device(dev); |
| 504 | return sprintf(buf, "%d\n", |
| 505 | FAN_FROM_REG(data->fan_min[nr], |
| 506 | FAN_DIV_FROM_REG(data->fan_div[nr]))); |
| 507 | } |
| 508 | static ssize_t |
| 509 | set_fan_min(struct device *dev, const char *buf, size_t count, int nr) |
| 510 | { |
| 511 | struct i2c_client *client = to_i2c_client(dev); |
| 512 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 513 | int val = simple_strtol(buf, NULL, 10); |
| 514 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 515 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | if (val) { |
| 517 | data->fan_min[nr] = |
| 518 | FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr])); |
| 519 | } else { |
| 520 | data->fan_min[nr] = 0xff; |
| 521 | } |
| 522 | adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 523 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | return count; |
| 525 | } |
| 526 | static ssize_t |
| 527 | set_fan_div(struct device *dev, const char *buf, size_t count, int nr) |
| 528 | { |
| 529 | struct i2c_client *client = to_i2c_client(dev); |
| 530 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 531 | int val = simple_strtol(buf, NULL, 10); |
| 532 | u8 tmp; |
| 533 | int old_div; |
| 534 | int new_min; |
| 535 | |
| 536 | tmp = val == 8 ? 0xc0 : |
| 537 | val == 4 ? 0x80 : |
| 538 | val == 2 ? 0x40 : |
| 539 | val == 1 ? 0x00 : |
| 540 | 0xff; |
| 541 | if (tmp == 0xff) |
| 542 | return -EINVAL; |
| 543 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 544 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | old_div = FAN_DIV_FROM_REG(data->fan_div[nr]); |
| 546 | data->fan_div[nr] = (tmp & 0xC0) | (0x3f & data->fan_div[nr]); |
| 547 | new_min = data->fan_min[nr] * old_div / |
| 548 | FAN_DIV_FROM_REG(data->fan_div[nr]); |
| 549 | data->fan_min[nr] = new_min > 0xff ? 0xff : new_min; |
| 550 | data->fan[nr] = data->fan[nr] * old_div / |
| 551 | FAN_DIV_FROM_REG(data->fan_div[nr]); |
| 552 | |
| 553 | adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr), |
| 554 | data->fan_div[nr]); |
| 555 | adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), |
| 556 | data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 557 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | return count; |
| 559 | } |
| 560 | |
| 561 | #define fan_offset(offset) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 562 | static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { \ |
| 564 | return show_fan(dev, buf, offset - 1); \ |
| 565 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 566 | static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | { \ |
| 568 | return show_fan_min(dev, buf, offset - 1); \ |
| 569 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 570 | static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { \ |
| 572 | return show_fan_div(dev, buf, offset - 1); \ |
| 573 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 574 | static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | const char *buf, size_t count) \ |
| 576 | { \ |
| 577 | return set_fan_min(dev, buf, count, offset - 1); \ |
| 578 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 579 | static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | const char *buf, size_t count) \ |
| 581 | { \ |
| 582 | return set_fan_div(dev, buf, count, offset - 1); \ |
| 583 | } \ |
| 584 | static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \ |
| 585 | NULL); \ |
| 586 | static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 587 | show_fan_##offset##_min, set_fan_##offset##_min); \ |
| 588 | static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ |
| 589 | show_fan_##offset##_div, set_fan_##offset##_div); \ |
| 590 | static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR, \ |
| 591 | show_pwm_##offset, set_pwm_##offset) |
| 592 | |
| 593 | fan_offset(1); |
| 594 | fan_offset(2); |
| 595 | |
| 596 | |
| 597 | /* Temps */ |
| 598 | static ssize_t show_temp(struct device *dev, char *buf, int nr) |
| 599 | { |
| 600 | struct adm1031_data *data = adm1031_update_device(dev); |
| 601 | int ext; |
| 602 | ext = nr == 0 ? |
| 603 | ((data->ext_temp[nr] >> 6) & 0x3) * 2 : |
| 604 | (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7)); |
| 605 | return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext)); |
| 606 | } |
| 607 | static ssize_t show_temp_min(struct device *dev, char *buf, int nr) |
| 608 | { |
| 609 | struct adm1031_data *data = adm1031_update_device(dev); |
| 610 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr])); |
| 611 | } |
| 612 | static ssize_t show_temp_max(struct device *dev, char *buf, int nr) |
| 613 | { |
| 614 | struct adm1031_data *data = adm1031_update_device(dev); |
| 615 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr])); |
| 616 | } |
| 617 | static ssize_t show_temp_crit(struct device *dev, char *buf, int nr) |
| 618 | { |
| 619 | struct adm1031_data *data = adm1031_update_device(dev); |
| 620 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr])); |
| 621 | } |
| 622 | static ssize_t |
| 623 | set_temp_min(struct device *dev, const char *buf, size_t count, int nr) |
| 624 | { |
| 625 | struct i2c_client *client = to_i2c_client(dev); |
| 626 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 627 | int val; |
| 628 | |
| 629 | val = simple_strtol(buf, NULL, 10); |
| 630 | val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 631 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | data->temp_min[nr] = TEMP_TO_REG(val); |
| 633 | adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr), |
| 634 | data->temp_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 635 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return count; |
| 637 | } |
| 638 | static ssize_t |
| 639 | set_temp_max(struct device *dev, const char *buf, size_t count, int nr) |
| 640 | { |
| 641 | struct i2c_client *client = to_i2c_client(dev); |
| 642 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 643 | int val; |
| 644 | |
| 645 | val = simple_strtol(buf, NULL, 10); |
| 646 | val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 647 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | data->temp_max[nr] = TEMP_TO_REG(val); |
| 649 | adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr), |
| 650 | data->temp_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 651 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | return count; |
| 653 | } |
| 654 | static ssize_t |
| 655 | set_temp_crit(struct device *dev, const char *buf, size_t count, int nr) |
| 656 | { |
| 657 | struct i2c_client *client = to_i2c_client(dev); |
| 658 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 659 | int val; |
| 660 | |
| 661 | val = simple_strtol(buf, NULL, 10); |
| 662 | val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 663 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | data->temp_crit[nr] = TEMP_TO_REG(val); |
| 665 | adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr), |
| 666 | data->temp_crit[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 667 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return count; |
| 669 | } |
| 670 | |
| 671 | #define temp_reg(offset) \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 672 | static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | { \ |
| 674 | return show_temp(dev, buf, offset - 1); \ |
| 675 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 676 | static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { \ |
| 678 | return show_temp_min(dev, buf, offset - 1); \ |
| 679 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 680 | static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { \ |
| 682 | return show_temp_max(dev, buf, offset - 1); \ |
| 683 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 684 | static ssize_t show_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { \ |
| 686 | return show_temp_crit(dev, buf, offset - 1); \ |
| 687 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 688 | static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | const char *buf, size_t count) \ |
| 690 | { \ |
| 691 | return set_temp_min(dev, buf, count, offset - 1); \ |
| 692 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 693 | static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | const char *buf, size_t count) \ |
| 695 | { \ |
| 696 | return set_temp_max(dev, buf, count, offset - 1); \ |
| 697 | } \ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 698 | static ssize_t set_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | const char *buf, size_t count) \ |
| 700 | { \ |
| 701 | return set_temp_crit(dev, buf, count, offset - 1); \ |
| 702 | } \ |
| 703 | static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \ |
| 704 | NULL); \ |
| 705 | static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ |
| 706 | show_temp_##offset##_min, set_temp_##offset##_min); \ |
| 707 | static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ |
| 708 | show_temp_##offset##_max, set_temp_##offset##_max); \ |
| 709 | static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \ |
| 710 | show_temp_##offset##_crit, set_temp_##offset##_crit) |
| 711 | |
| 712 | temp_reg(1); |
| 713 | temp_reg(2); |
| 714 | temp_reg(3); |
| 715 | |
| 716 | /* Alarms */ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 717 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
| 719 | struct adm1031_data *data = adm1031_update_device(dev); |
| 720 | return sprintf(buf, "%d\n", data->alarm); |
| 721 | } |
| 722 | |
| 723 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 724 | |
| 725 | |
| 726 | static int adm1031_attach_adapter(struct i2c_adapter *adapter) |
| 727 | { |
| 728 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 729 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 730 | return i2c_probe(adapter, &addr_data, adm1031_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 733 | static struct attribute *adm1031_attributes[] = { |
| 734 | &dev_attr_fan1_input.attr, |
| 735 | &dev_attr_fan1_div.attr, |
| 736 | &dev_attr_fan1_min.attr, |
| 737 | &dev_attr_pwm1.attr, |
| 738 | &dev_attr_auto_fan1_channel.attr, |
| 739 | &dev_attr_temp1_input.attr, |
| 740 | &dev_attr_temp1_min.attr, |
| 741 | &dev_attr_temp1_max.attr, |
| 742 | &dev_attr_temp1_crit.attr, |
| 743 | &dev_attr_temp2_input.attr, |
| 744 | &dev_attr_temp2_min.attr, |
| 745 | &dev_attr_temp2_max.attr, |
| 746 | &dev_attr_temp2_crit.attr, |
| 747 | |
| 748 | &dev_attr_auto_temp1_off.attr, |
| 749 | &dev_attr_auto_temp1_min.attr, |
| 750 | &dev_attr_auto_temp1_max.attr, |
| 751 | |
| 752 | &dev_attr_auto_temp2_off.attr, |
| 753 | &dev_attr_auto_temp2_min.attr, |
| 754 | &dev_attr_auto_temp2_max.attr, |
| 755 | |
| 756 | &dev_attr_auto_fan1_min_pwm.attr, |
| 757 | |
| 758 | &dev_attr_alarms.attr, |
| 759 | |
| 760 | NULL |
| 761 | }; |
| 762 | |
| 763 | static const struct attribute_group adm1031_group = { |
| 764 | .attrs = adm1031_attributes, |
| 765 | }; |
| 766 | |
| 767 | static struct attribute *adm1031_attributes_opt[] = { |
| 768 | &dev_attr_fan2_input.attr, |
| 769 | &dev_attr_fan2_div.attr, |
| 770 | &dev_attr_fan2_min.attr, |
| 771 | &dev_attr_pwm2.attr, |
| 772 | &dev_attr_auto_fan2_channel.attr, |
| 773 | &dev_attr_temp3_input.attr, |
| 774 | &dev_attr_temp3_min.attr, |
| 775 | &dev_attr_temp3_max.attr, |
| 776 | &dev_attr_temp3_crit.attr, |
| 777 | &dev_attr_auto_temp3_off.attr, |
| 778 | &dev_attr_auto_temp3_min.attr, |
| 779 | &dev_attr_auto_temp3_max.attr, |
| 780 | &dev_attr_auto_fan2_min_pwm.attr, |
| 781 | NULL |
| 782 | }; |
| 783 | |
| 784 | static const struct attribute_group adm1031_group_opt = { |
| 785 | .attrs = adm1031_attributes_opt, |
| 786 | }; |
| 787 | |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 788 | /* This function is called by i2c_probe */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind) |
| 790 | { |
| 791 | struct i2c_client *new_client; |
| 792 | struct adm1031_data *data; |
| 793 | int err = 0; |
| 794 | const char *name = ""; |
| 795 | |
| 796 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 797 | goto exit; |
| 798 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 799 | if (!(data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | err = -ENOMEM; |
| 801 | goto exit; |
| 802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | new_client = &data->client; |
| 805 | i2c_set_clientdata(new_client, data); |
| 806 | new_client->addr = address; |
| 807 | new_client->adapter = adapter; |
| 808 | new_client->driver = &adm1031_driver; |
| 809 | new_client->flags = 0; |
| 810 | |
| 811 | if (kind < 0) { |
| 812 | int id, co; |
| 813 | id = i2c_smbus_read_byte_data(new_client, 0x3d); |
| 814 | co = i2c_smbus_read_byte_data(new_client, 0x3e); |
| 815 | |
| 816 | if (!((id == 0x31 || id == 0x30) && co == 0x41)) |
| 817 | goto exit_free; |
| 818 | kind = (id == 0x30) ? adm1030 : adm1031; |
| 819 | } |
| 820 | |
| 821 | if (kind <= 0) |
| 822 | kind = adm1031; |
| 823 | |
| 824 | /* Given the detected chip type, set the chip name and the |
| 825 | * auto fan control helper table. */ |
| 826 | if (kind == adm1030) { |
| 827 | name = "adm1030"; |
| 828 | data->chan_select_table = &auto_channel_select_table_adm1030; |
| 829 | } else if (kind == adm1031) { |
| 830 | name = "adm1031"; |
| 831 | data->chan_select_table = &auto_channel_select_table_adm1031; |
| 832 | } |
| 833 | data->chip_type = kind; |
| 834 | |
| 835 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
| 836 | data->valid = 0; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 837 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
| 839 | /* Tell the I2C layer a new client has arrived */ |
| 840 | if ((err = i2c_attach_client(new_client))) |
| 841 | goto exit_free; |
| 842 | |
| 843 | /* Initialize the ADM1031 chip */ |
| 844 | adm1031_init_client(new_client); |
| 845 | |
| 846 | /* Register sysfs hooks */ |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 847 | if ((err = sysfs_create_group(&new_client->dev.kobj, &adm1031_group))) |
| 848 | goto exit_detach; |
| 849 | |
| 850 | if (kind == adm1031) { |
| 851 | if ((err = sysfs_create_group(&new_client->dev.kobj, |
| 852 | &adm1031_group_opt))) |
| 853 | goto exit_remove; |
| 854 | } |
| 855 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 856 | data->class_dev = hwmon_device_register(&new_client->dev); |
| 857 | if (IS_ERR(data->class_dev)) { |
| 858 | err = PTR_ERR(data->class_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 859 | goto exit_remove; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | return 0; |
| 863 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 864 | exit_remove: |
| 865 | sysfs_remove_group(&new_client->dev.kobj, &adm1031_group); |
| 866 | sysfs_remove_group(&new_client->dev.kobj, &adm1031_group_opt); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 867 | exit_detach: |
| 868 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | exit_free: |
Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 870 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | exit: |
| 872 | return err; |
| 873 | } |
| 874 | |
| 875 | static int adm1031_detach_client(struct i2c_client *client) |
| 876 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 877 | struct adm1031_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | int ret; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 879 | |
| 880 | hwmon_device_unregister(data->class_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 881 | sysfs_remove_group(&client->dev.kobj, &adm1031_group); |
| 882 | sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | if ((ret = i2c_detach_client(client)) != 0) { |
| 884 | return ret; |
| 885 | } |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 886 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static void adm1031_init_client(struct i2c_client *client) |
| 891 | { |
| 892 | unsigned int read_val; |
| 893 | unsigned int mask; |
| 894 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 895 | |
| 896 | mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE); |
| 897 | if (data->chip_type == adm1031) { |
| 898 | mask |= (ADM1031_CONF2_PWM2_ENABLE | |
| 899 | ADM1031_CONF2_TACH2_ENABLE); |
| 900 | } |
| 901 | /* Initialize the ADM1031 chip (enables fan speed reading ) */ |
| 902 | read_val = adm1031_read_value(client, ADM1031_REG_CONF2); |
| 903 | if ((read_val | mask) != read_val) { |
| 904 | adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask); |
| 905 | } |
| 906 | |
| 907 | read_val = adm1031_read_value(client, ADM1031_REG_CONF1); |
| 908 | if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) { |
| 909 | adm1031_write_value(client, ADM1031_REG_CONF1, read_val | |
| 910 | ADM1031_CONF1_MONITOR_ENABLE); |
| 911 | } |
| 912 | |
| 913 | } |
| 914 | |
| 915 | static struct adm1031_data *adm1031_update_device(struct device *dev) |
| 916 | { |
| 917 | struct i2c_client *client = to_i2c_client(dev); |
| 918 | struct adm1031_data *data = i2c_get_clientdata(client); |
| 919 | int chan; |
| 920 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 921 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 924 | || !data->valid) { |
| 925 | |
| 926 | dev_dbg(&client->dev, "Starting adm1031 update\n"); |
| 927 | for (chan = 0; |
| 928 | chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) { |
| 929 | u8 oldh, newh; |
| 930 | |
| 931 | oldh = |
| 932 | adm1031_read_value(client, ADM1031_REG_TEMP(chan)); |
| 933 | data->ext_temp[chan] = |
| 934 | adm1031_read_value(client, ADM1031_REG_EXT_TEMP); |
| 935 | newh = |
| 936 | adm1031_read_value(client, ADM1031_REG_TEMP(chan)); |
| 937 | if (newh != oldh) { |
| 938 | data->ext_temp[chan] = |
| 939 | adm1031_read_value(client, |
| 940 | ADM1031_REG_EXT_TEMP); |
| 941 | #ifdef DEBUG |
| 942 | oldh = |
| 943 | adm1031_read_value(client, |
| 944 | ADM1031_REG_TEMP(chan)); |
| 945 | |
| 946 | /* oldh is actually newer */ |
| 947 | if (newh != oldh) |
| 948 | dev_warn(&client->dev, |
| 949 | "Remote temperature may be " |
| 950 | "wrong.\n"); |
| 951 | #endif |
| 952 | } |
| 953 | data->temp[chan] = newh; |
| 954 | |
| 955 | data->temp_min[chan] = |
| 956 | adm1031_read_value(client, |
| 957 | ADM1031_REG_TEMP_MIN(chan)); |
| 958 | data->temp_max[chan] = |
| 959 | adm1031_read_value(client, |
| 960 | ADM1031_REG_TEMP_MAX(chan)); |
| 961 | data->temp_crit[chan] = |
| 962 | adm1031_read_value(client, |
| 963 | ADM1031_REG_TEMP_CRIT(chan)); |
| 964 | data->auto_temp[chan] = |
| 965 | adm1031_read_value(client, |
| 966 | ADM1031_REG_AUTO_TEMP(chan)); |
| 967 | |
| 968 | } |
| 969 | |
| 970 | data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1); |
| 971 | data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2); |
| 972 | |
| 973 | data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0)) |
| 974 | | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) |
| 975 | << 8); |
| 976 | if (data->chip_type == adm1030) { |
| 977 | data->alarm &= 0xc0ff; |
| 978 | } |
| 979 | |
| 980 | for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) { |
| 981 | data->fan_div[chan] = |
| 982 | adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan)); |
| 983 | data->fan_min[chan] = |
| 984 | adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan)); |
| 985 | data->fan[chan] = |
| 986 | adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan)); |
| 987 | data->pwm[chan] = |
| 988 | 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >> |
| 989 | (4*chan)); |
| 990 | } |
| 991 | data->last_updated = jiffies; |
| 992 | data->valid = 1; |
| 993 | } |
| 994 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 995 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
| 997 | return data; |
| 998 | } |
| 999 | |
| 1000 | static int __init sensors_adm1031_init(void) |
| 1001 | { |
| 1002 | return i2c_add_driver(&adm1031_driver); |
| 1003 | } |
| 1004 | |
| 1005 | static void __exit sensors_adm1031_exit(void) |
| 1006 | { |
| 1007 | i2c_del_driver(&adm1031_driver); |
| 1008 | } |
| 1009 | |
| 1010 | MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>"); |
| 1011 | MODULE_DESCRIPTION("ADM1031/ADM1030 driver"); |
| 1012 | MODULE_LICENSE("GPL"); |
| 1013 | |
| 1014 | module_init(sensors_adm1031_init); |
| 1015 | module_exit(sensors_adm1031_exit); |