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