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