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