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