Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * A hwmon driver for the Analog Devices ADT7470 |
| 3 | * Copyright (C) 2007 IBM |
| 4 | * |
| 5 | * Author: Darrick J. Wong <djwong@us.ibm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/hwmon.h> |
| 26 | #include <linux/hwmon-sysfs.h> |
| 27 | #include <linux/err.h> |
| 28 | #include <linux/mutex.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/log2.h> |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 31 | #include <linux/kthread.h> |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 32 | |
| 33 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 34 | static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END }; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 35 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 36 | /* ADT7470 registers */ |
| 37 | #define ADT7470_REG_BASE_ADDR 0x20 |
| 38 | #define ADT7470_REG_TEMP_BASE_ADDR 0x20 |
| 39 | #define ADT7470_REG_TEMP_MAX_ADDR 0x29 |
| 40 | #define ADT7470_REG_FAN_BASE_ADDR 0x2A |
| 41 | #define ADT7470_REG_FAN_MAX_ADDR 0x31 |
| 42 | #define ADT7470_REG_PWM_BASE_ADDR 0x32 |
| 43 | #define ADT7470_REG_PWM_MAX_ADDR 0x35 |
| 44 | #define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38 |
| 45 | #define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B |
| 46 | #define ADT7470_REG_CFG 0x40 |
| 47 | #define ADT7470_FSPD_MASK 0x04 |
| 48 | #define ADT7470_REG_ALARM1 0x41 |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 49 | #define ADT7470_R1T_ALARM 0x01 |
| 50 | #define ADT7470_R2T_ALARM 0x02 |
| 51 | #define ADT7470_R3T_ALARM 0x04 |
| 52 | #define ADT7470_R4T_ALARM 0x08 |
| 53 | #define ADT7470_R5T_ALARM 0x10 |
| 54 | #define ADT7470_R6T_ALARM 0x20 |
| 55 | #define ADT7470_R7T_ALARM 0x40 |
| 56 | #define ADT7470_OOL_ALARM 0x80 |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 57 | #define ADT7470_REG_ALARM2 0x42 |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 58 | #define ADT7470_R8T_ALARM 0x01 |
| 59 | #define ADT7470_R9T_ALARM 0x02 |
| 60 | #define ADT7470_R10T_ALARM 0x04 |
| 61 | #define ADT7470_FAN1_ALARM 0x10 |
| 62 | #define ADT7470_FAN2_ALARM 0x20 |
| 63 | #define ADT7470_FAN3_ALARM 0x40 |
| 64 | #define ADT7470_FAN4_ALARM 0x80 |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 65 | #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44 |
| 66 | #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57 |
| 67 | #define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58 |
| 68 | #define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F |
| 69 | #define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60 |
| 70 | #define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67 |
| 71 | #define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68 |
| 72 | #define ADT7470_REG_PWM12_CFG 0x68 |
| 73 | #define ADT7470_PWM2_AUTO_MASK 0x40 |
| 74 | #define ADT7470_PWM1_AUTO_MASK 0x80 |
Darrick J. Wong | 2e75a4b | 2009-01-06 14:41:32 -0800 | [diff] [blame] | 75 | #define ADT7470_PWM_AUTO_MASK 0xC0 |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 76 | #define ADT7470_REG_PWM34_CFG 0x69 |
| 77 | #define ADT7470_PWM3_AUTO_MASK 0x40 |
| 78 | #define ADT7470_PWM4_AUTO_MASK 0x80 |
| 79 | #define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A |
| 80 | #define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D |
| 81 | #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E |
| 82 | #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71 |
| 83 | #define ADT7470_REG_ACOUSTICS12 0x75 |
| 84 | #define ADT7470_REG_ACOUSTICS34 0x76 |
| 85 | #define ADT7470_REG_DEVICE 0x3D |
| 86 | #define ADT7470_REG_VENDOR 0x3E |
| 87 | #define ADT7470_REG_REVISION 0x3F |
| 88 | #define ADT7470_REG_ALARM1_MASK 0x72 |
| 89 | #define ADT7470_REG_ALARM2_MASK 0x73 |
| 90 | #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C |
| 91 | #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D |
| 92 | #define ADT7470_REG_MAX_ADDR 0x81 |
| 93 | |
| 94 | #define ADT7470_TEMP_COUNT 10 |
| 95 | #define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x)) |
| 96 | #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2)) |
| 97 | #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \ |
| 98 | ((x) * 2) + 1) |
| 99 | |
| 100 | #define ADT7470_FAN_COUNT 4 |
| 101 | #define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2)) |
| 102 | #define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2)) |
| 103 | #define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2)) |
| 104 | |
| 105 | #define ADT7470_PWM_COUNT 4 |
| 106 | #define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x)) |
| 107 | #define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x)) |
| 108 | #define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x)) |
| 109 | #define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x)) |
| 110 | #define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2)) |
| 111 | #define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \ |
| 112 | ((x) / 2)) |
| 113 | |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 114 | #define ALARM2(x) ((x) << 8) |
| 115 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 116 | #define ADT7470_VENDOR 0x41 |
| 117 | #define ADT7470_DEVICE 0x70 |
| 118 | /* datasheet only mentions a revision 2 */ |
| 119 | #define ADT7470_REVISION 0x02 |
| 120 | |
| 121 | /* "all temps" according to hwmon sysfs interface spec */ |
| 122 | #define ADT7470_PWM_ALL_TEMPS 0x3FF |
| 123 | |
| 124 | /* How often do we reread sensors values? (In jiffies) */ |
| 125 | #define SENSOR_REFRESH_INTERVAL (5 * HZ) |
| 126 | |
| 127 | /* How often do we reread sensor limit values? (In jiffies) */ |
| 128 | #define LIMIT_REFRESH_INTERVAL (60 * HZ) |
| 129 | |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 130 | /* Wait at least 200ms per sensor for 10 sensors */ |
| 131 | #define TEMP_COLLECTION_TIME 2000 |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 132 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 133 | /* auto update thing won't fire more than every 2s */ |
| 134 | #define AUTO_UPDATE_INTERVAL 2000 |
| 135 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 136 | /* datasheet says to divide this number by the fan reading to get fan rpm */ |
| 137 | #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x)) |
| 138 | #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM |
| 139 | #define FAN_PERIOD_INVALID 65535 |
| 140 | #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID) |
| 141 | |
| 142 | struct adt7470_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 143 | struct device *hwmon_dev; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 144 | struct attribute_group attrs; |
| 145 | struct mutex lock; |
| 146 | char sensors_valid; |
| 147 | char limits_valid; |
| 148 | unsigned long sensors_last_updated; /* In jiffies */ |
| 149 | unsigned long limits_last_updated; /* In jiffies */ |
| 150 | |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 151 | int num_temp_sensors; /* -1 = probe */ |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 152 | int temperatures_probed; |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 153 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 154 | s8 temp[ADT7470_TEMP_COUNT]; |
| 155 | s8 temp_min[ADT7470_TEMP_COUNT]; |
| 156 | s8 temp_max[ADT7470_TEMP_COUNT]; |
| 157 | u16 fan[ADT7470_FAN_COUNT]; |
| 158 | u16 fan_min[ADT7470_FAN_COUNT]; |
| 159 | u16 fan_max[ADT7470_FAN_COUNT]; |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 160 | u16 alarm; |
| 161 | u16 alarms_mask; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 162 | u8 force_pwm_max; |
| 163 | u8 pwm[ADT7470_PWM_COUNT]; |
| 164 | u8 pwm_max[ADT7470_PWM_COUNT]; |
| 165 | u8 pwm_automatic[ADT7470_PWM_COUNT]; |
| 166 | u8 pwm_min[ADT7470_PWM_COUNT]; |
| 167 | s8 pwm_tmin[ADT7470_PWM_COUNT]; |
| 168 | u8 pwm_auto_temp[ADT7470_PWM_COUNT]; |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 169 | |
| 170 | struct task_struct *auto_update; |
| 171 | struct completion auto_update_stop; |
| 172 | unsigned int auto_update_interval; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 175 | static int adt7470_probe(struct i2c_client *client, |
| 176 | const struct i2c_device_id *id); |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 177 | static int adt7470_detect(struct i2c_client *client, |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 178 | struct i2c_board_info *info); |
| 179 | static int adt7470_remove(struct i2c_client *client); |
| 180 | |
| 181 | static const struct i2c_device_id adt7470_id[] = { |
Jean Delvare | 1f86df4 | 2009-12-14 21:17:26 +0100 | [diff] [blame] | 182 | { "adt7470", 0 }, |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 183 | { } |
| 184 | }; |
| 185 | MODULE_DEVICE_TABLE(i2c, adt7470_id); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 186 | |
| 187 | static struct i2c_driver adt7470_driver = { |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 188 | .class = I2C_CLASS_HWMON, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 189 | .driver = { |
| 190 | .name = "adt7470", |
| 191 | }, |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 192 | .probe = adt7470_probe, |
| 193 | .remove = adt7470_remove, |
| 194 | .id_table = adt7470_id, |
| 195 | .detect = adt7470_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 196 | .address_list = normal_i2c, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | /* |
| 200 | * 16-bit registers on the ADT7470 are low-byte first. The data sheet says |
| 201 | * that the low byte must be read before the high byte. |
| 202 | */ |
| 203 | static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg) |
| 204 | { |
| 205 | u16 foo; |
| 206 | foo = i2c_smbus_read_byte_data(client, reg); |
| 207 | foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8); |
| 208 | return foo; |
| 209 | } |
| 210 | |
| 211 | static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg, |
| 212 | u16 value) |
| 213 | { |
| 214 | return i2c_smbus_write_byte_data(client, reg, value & 0xFF) |
| 215 | && i2c_smbus_write_byte_data(client, reg + 1, value >> 8); |
| 216 | } |
| 217 | |
| 218 | static void adt7470_init_client(struct i2c_client *client) |
| 219 | { |
| 220 | int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); |
| 221 | |
| 222 | if (reg < 0) { |
| 223 | dev_err(&client->dev, "cannot read configuration register\n"); |
| 224 | } else { |
| 225 | /* start monitoring (and do a self-test) */ |
| 226 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3); |
| 227 | } |
| 228 | } |
| 229 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 230 | /* Probe for temperature sensors. Assumes lock is held */ |
| 231 | static int adt7470_read_temperatures(struct i2c_client *client, |
| 232 | struct adt7470_data *data) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 233 | { |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 234 | unsigned long res; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 235 | int i; |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 236 | u8 cfg, pwm[4], pwm_cfg[2]; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 237 | |
Darrick J. Wong | 2e75a4b | 2009-01-06 14:41:32 -0800 | [diff] [blame] | 238 | /* save pwm[1-4] config register */ |
| 239 | pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0)); |
| 240 | pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2)); |
| 241 | |
| 242 | /* set manual pwm to whatever it is set to now */ |
| 243 | for (i = 0; i < ADT7470_FAN_COUNT; i++) |
| 244 | pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i)); |
| 245 | |
| 246 | /* put pwm in manual mode */ |
| 247 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), |
| 248 | pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK)); |
| 249 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), |
| 250 | pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK)); |
| 251 | |
| 252 | /* write pwm control to whatever it was */ |
| 253 | for (i = 0; i < ADT7470_FAN_COUNT; i++) |
| 254 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]); |
| 255 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 256 | /* start reading temperature sensors */ |
| 257 | cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); |
| 258 | cfg |= 0x80; |
| 259 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg); |
| 260 | |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 261 | /* Delay is 200ms * number of temp sensors. */ |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 262 | res = msleep_interruptible((data->num_temp_sensors >= 0 ? |
| 263 | data->num_temp_sensors * 200 : |
| 264 | TEMP_COLLECTION_TIME)); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 265 | |
| 266 | /* done reading temperature sensors */ |
| 267 | cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); |
| 268 | cfg &= ~0x80; |
| 269 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg); |
| 270 | |
Darrick J. Wong | 2e75a4b | 2009-01-06 14:41:32 -0800 | [diff] [blame] | 271 | /* restore pwm[1-4] config registers */ |
| 272 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]); |
| 273 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]); |
| 274 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 275 | if (res) { |
| 276 | printk(KERN_ERR "ha ha, interrupted"); |
| 277 | return -EAGAIN; |
| 278 | } |
| 279 | |
| 280 | /* Only count fans if we have to */ |
| 281 | if (data->num_temp_sensors >= 0) |
| 282 | return 0; |
| 283 | |
| 284 | for (i = 0; i < ADT7470_TEMP_COUNT; i++) { |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 285 | data->temp[i] = i2c_smbus_read_byte_data(client, |
| 286 | ADT7470_TEMP_REG(i)); |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 287 | if (data->temp[i]) |
| 288 | data->num_temp_sensors = i + 1; |
| 289 | } |
| 290 | data->temperatures_probed = 1; |
| 291 | return 0; |
| 292 | } |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 293 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 294 | static int adt7470_update_thread(void *p) |
| 295 | { |
| 296 | struct i2c_client *client = p; |
| 297 | struct adt7470_data *data = i2c_get_clientdata(client); |
| 298 | |
| 299 | while (!kthread_should_stop()) { |
| 300 | mutex_lock(&data->lock); |
| 301 | adt7470_read_temperatures(client, data); |
| 302 | mutex_unlock(&data->lock); |
| 303 | if (kthread_should_stop()) |
| 304 | break; |
| 305 | msleep_interruptible(data->auto_update_interval); |
| 306 | } |
| 307 | |
| 308 | complete_all(&data->auto_update_stop); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static struct adt7470_data *adt7470_update_device(struct device *dev) |
| 313 | { |
| 314 | struct i2c_client *client = to_i2c_client(dev); |
| 315 | struct adt7470_data *data = i2c_get_clientdata(client); |
| 316 | unsigned long local_jiffies = jiffies; |
| 317 | u8 cfg; |
| 318 | int i; |
| 319 | int need_sensors = 1; |
| 320 | int need_limits = 1; |
| 321 | |
| 322 | /* |
| 323 | * Figure out if we need to update the shadow registers. |
| 324 | * Lockless means that we may occasionally report out of |
| 325 | * date data. |
| 326 | */ |
| 327 | if (time_before(local_jiffies, data->sensors_last_updated + |
| 328 | SENSOR_REFRESH_INTERVAL) && |
| 329 | data->sensors_valid) |
| 330 | need_sensors = 0; |
| 331 | |
| 332 | if (time_before(local_jiffies, data->limits_last_updated + |
| 333 | LIMIT_REFRESH_INTERVAL) && |
| 334 | data->limits_valid) |
| 335 | need_limits = 0; |
| 336 | |
| 337 | if (!need_sensors && !need_limits) |
| 338 | return data; |
| 339 | |
| 340 | mutex_lock(&data->lock); |
| 341 | if (!need_sensors) |
| 342 | goto no_sensor_update; |
| 343 | |
| 344 | if (!data->temperatures_probed) |
| 345 | adt7470_read_temperatures(client, data); |
| 346 | else |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 347 | for (i = 0; i < ADT7470_TEMP_COUNT; i++) |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 348 | data->temp[i] = i2c_smbus_read_byte_data(client, |
| 349 | ADT7470_TEMP_REG(i)); |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 350 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 351 | for (i = 0; i < ADT7470_FAN_COUNT; i++) |
| 352 | data->fan[i] = adt7470_read_word_data(client, |
| 353 | ADT7470_REG_FAN(i)); |
| 354 | |
| 355 | for (i = 0; i < ADT7470_PWM_COUNT; i++) { |
| 356 | int reg; |
| 357 | int reg_mask; |
| 358 | |
| 359 | data->pwm[i] = i2c_smbus_read_byte_data(client, |
| 360 | ADT7470_REG_PWM(i)); |
| 361 | |
| 362 | if (i % 2) |
| 363 | reg_mask = ADT7470_PWM2_AUTO_MASK; |
| 364 | else |
| 365 | reg_mask = ADT7470_PWM1_AUTO_MASK; |
| 366 | |
| 367 | reg = ADT7470_REG_PWM_CFG(i); |
| 368 | if (i2c_smbus_read_byte_data(client, reg) & reg_mask) |
| 369 | data->pwm_automatic[i] = 1; |
| 370 | else |
| 371 | data->pwm_automatic[i] = 0; |
| 372 | |
| 373 | reg = ADT7470_REG_PWM_AUTO_TEMP(i); |
| 374 | cfg = i2c_smbus_read_byte_data(client, reg); |
| 375 | if (!(i % 2)) |
| 376 | data->pwm_auto_temp[i] = cfg >> 4; |
| 377 | else |
| 378 | data->pwm_auto_temp[i] = cfg & 0xF; |
| 379 | } |
| 380 | |
| 381 | if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) & |
| 382 | ADT7470_FSPD_MASK) |
| 383 | data->force_pwm_max = 1; |
| 384 | else |
| 385 | data->force_pwm_max = 0; |
| 386 | |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 387 | data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1); |
| 388 | if (data->alarm & ADT7470_OOL_ALARM) |
| 389 | data->alarm |= ALARM2(i2c_smbus_read_byte_data(client, |
| 390 | ADT7470_REG_ALARM2)); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 391 | data->alarms_mask = adt7470_read_word_data(client, |
| 392 | ADT7470_REG_ALARM1_MASK); |
| 393 | |
| 394 | data->sensors_last_updated = local_jiffies; |
| 395 | data->sensors_valid = 1; |
| 396 | |
| 397 | no_sensor_update: |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 398 | if (!need_limits) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 399 | goto out; |
| 400 | |
| 401 | for (i = 0; i < ADT7470_TEMP_COUNT; i++) { |
| 402 | data->temp_min[i] = i2c_smbus_read_byte_data(client, |
| 403 | ADT7470_TEMP_MIN_REG(i)); |
| 404 | data->temp_max[i] = i2c_smbus_read_byte_data(client, |
| 405 | ADT7470_TEMP_MAX_REG(i)); |
| 406 | } |
| 407 | |
| 408 | for (i = 0; i < ADT7470_FAN_COUNT; i++) { |
| 409 | data->fan_min[i] = adt7470_read_word_data(client, |
| 410 | ADT7470_REG_FAN_MIN(i)); |
| 411 | data->fan_max[i] = adt7470_read_word_data(client, |
| 412 | ADT7470_REG_FAN_MAX(i)); |
| 413 | } |
| 414 | |
| 415 | for (i = 0; i < ADT7470_PWM_COUNT; i++) { |
| 416 | data->pwm_max[i] = i2c_smbus_read_byte_data(client, |
| 417 | ADT7470_REG_PWM_MAX(i)); |
| 418 | data->pwm_min[i] = i2c_smbus_read_byte_data(client, |
| 419 | ADT7470_REG_PWM_MIN(i)); |
| 420 | data->pwm_tmin[i] = i2c_smbus_read_byte_data(client, |
| 421 | ADT7470_REG_PWM_TMIN(i)); |
| 422 | } |
| 423 | |
| 424 | data->limits_last_updated = local_jiffies; |
| 425 | data->limits_valid = 1; |
| 426 | |
| 427 | out: |
| 428 | mutex_unlock(&data->lock); |
| 429 | return data; |
| 430 | } |
| 431 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 432 | static ssize_t show_auto_update_interval(struct device *dev, |
| 433 | struct device_attribute *devattr, |
| 434 | char *buf) |
| 435 | { |
| 436 | struct adt7470_data *data = adt7470_update_device(dev); |
| 437 | return sprintf(buf, "%d\n", data->auto_update_interval); |
| 438 | } |
| 439 | |
| 440 | static ssize_t set_auto_update_interval(struct device *dev, |
| 441 | struct device_attribute *devattr, |
| 442 | const char *buf, |
| 443 | size_t count) |
| 444 | { |
| 445 | struct i2c_client *client = to_i2c_client(dev); |
| 446 | struct adt7470_data *data = i2c_get_clientdata(client); |
| 447 | long temp; |
| 448 | |
| 449 | if (strict_strtol(buf, 10, &temp)) |
| 450 | return -EINVAL; |
| 451 | |
| 452 | temp = SENSORS_LIMIT(temp, 0, 60000); |
| 453 | |
| 454 | mutex_lock(&data->lock); |
| 455 | data->auto_update_interval = temp; |
| 456 | mutex_unlock(&data->lock); |
| 457 | |
| 458 | return count; |
| 459 | } |
| 460 | |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 461 | static ssize_t show_num_temp_sensors(struct device *dev, |
| 462 | struct device_attribute *devattr, |
| 463 | char *buf) |
| 464 | { |
| 465 | struct adt7470_data *data = adt7470_update_device(dev); |
| 466 | return sprintf(buf, "%d\n", data->num_temp_sensors); |
| 467 | } |
| 468 | |
| 469 | static ssize_t set_num_temp_sensors(struct device *dev, |
| 470 | struct device_attribute *devattr, |
| 471 | const char *buf, |
| 472 | size_t count) |
| 473 | { |
| 474 | struct i2c_client *client = to_i2c_client(dev); |
| 475 | struct adt7470_data *data = i2c_get_clientdata(client); |
| 476 | long temp; |
| 477 | |
| 478 | if (strict_strtol(buf, 10, &temp)) |
| 479 | return -EINVAL; |
| 480 | |
| 481 | temp = SENSORS_LIMIT(temp, -1, 10); |
| 482 | |
| 483 | mutex_lock(&data->lock); |
| 484 | data->num_temp_sensors = temp; |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 485 | if (temp < 0) |
| 486 | data->temperatures_probed = 0; |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 487 | mutex_unlock(&data->lock); |
| 488 | |
| 489 | return count; |
| 490 | } |
| 491 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 492 | static ssize_t show_temp_min(struct device *dev, |
| 493 | struct device_attribute *devattr, |
| 494 | char *buf) |
| 495 | { |
| 496 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 497 | struct adt7470_data *data = adt7470_update_device(dev); |
| 498 | return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]); |
| 499 | } |
| 500 | |
| 501 | static ssize_t set_temp_min(struct device *dev, |
| 502 | struct device_attribute *devattr, |
| 503 | const char *buf, |
| 504 | size_t count) |
| 505 | { |
| 506 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 507 | struct i2c_client *client = to_i2c_client(dev); |
| 508 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 509 | long temp; |
| 510 | |
| 511 | if (strict_strtol(buf, 10, &temp)) |
| 512 | return -EINVAL; |
| 513 | |
Darrick J. Wong | 8f8c1fb | 2009-01-06 14:41:31 -0800 | [diff] [blame] | 514 | temp = DIV_ROUND_CLOSEST(temp, 1000); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 515 | temp = SENSORS_LIMIT(temp, 0, 255); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 516 | |
| 517 | mutex_lock(&data->lock); |
| 518 | data->temp_min[attr->index] = temp; |
| 519 | i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index), |
| 520 | temp); |
| 521 | mutex_unlock(&data->lock); |
| 522 | |
| 523 | return count; |
| 524 | } |
| 525 | |
| 526 | static ssize_t show_temp_max(struct device *dev, |
| 527 | struct device_attribute *devattr, |
| 528 | char *buf) |
| 529 | { |
| 530 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 531 | struct adt7470_data *data = adt7470_update_device(dev); |
| 532 | return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]); |
| 533 | } |
| 534 | |
| 535 | static ssize_t set_temp_max(struct device *dev, |
| 536 | struct device_attribute *devattr, |
| 537 | const char *buf, |
| 538 | size_t count) |
| 539 | { |
| 540 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 541 | struct i2c_client *client = to_i2c_client(dev); |
| 542 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 543 | long temp; |
| 544 | |
| 545 | if (strict_strtol(buf, 10, &temp)) |
| 546 | return -EINVAL; |
| 547 | |
Darrick J. Wong | 8f8c1fb | 2009-01-06 14:41:31 -0800 | [diff] [blame] | 548 | temp = DIV_ROUND_CLOSEST(temp, 1000); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 549 | temp = SENSORS_LIMIT(temp, 0, 255); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 550 | |
| 551 | mutex_lock(&data->lock); |
| 552 | data->temp_max[attr->index] = temp; |
| 553 | i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index), |
| 554 | temp); |
| 555 | mutex_unlock(&data->lock); |
| 556 | |
| 557 | return count; |
| 558 | } |
| 559 | |
| 560 | static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, |
| 561 | char *buf) |
| 562 | { |
| 563 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 564 | struct adt7470_data *data = adt7470_update_device(dev); |
| 565 | return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]); |
| 566 | } |
| 567 | |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 568 | static ssize_t show_alarm_mask(struct device *dev, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 569 | struct device_attribute *devattr, |
| 570 | char *buf) |
| 571 | { |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 572 | struct adt7470_data *data = adt7470_update_device(dev); |
| 573 | |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 574 | return sprintf(buf, "%x\n", data->alarms_mask); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static ssize_t show_fan_max(struct device *dev, |
| 578 | struct device_attribute *devattr, |
| 579 | char *buf) |
| 580 | { |
| 581 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 582 | struct adt7470_data *data = adt7470_update_device(dev); |
| 583 | |
| 584 | if (FAN_DATA_VALID(data->fan_max[attr->index])) |
| 585 | return sprintf(buf, "%d\n", |
| 586 | FAN_PERIOD_TO_RPM(data->fan_max[attr->index])); |
| 587 | else |
| 588 | return sprintf(buf, "0\n"); |
| 589 | } |
| 590 | |
| 591 | static ssize_t set_fan_max(struct device *dev, |
| 592 | struct device_attribute *devattr, |
| 593 | const char *buf, size_t count) |
| 594 | { |
| 595 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 596 | struct i2c_client *client = to_i2c_client(dev); |
| 597 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 598 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 599 | |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 600 | if (strict_strtol(buf, 10, &temp) || !temp) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 601 | return -EINVAL; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 602 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 603 | temp = FAN_RPM_TO_PERIOD(temp); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 604 | temp = SENSORS_LIMIT(temp, 1, 65534); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 605 | |
| 606 | mutex_lock(&data->lock); |
| 607 | data->fan_max[attr->index] = temp; |
| 608 | adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp); |
| 609 | mutex_unlock(&data->lock); |
| 610 | |
| 611 | return count; |
| 612 | } |
| 613 | |
| 614 | static ssize_t show_fan_min(struct device *dev, |
| 615 | struct device_attribute *devattr, |
| 616 | char *buf) |
| 617 | { |
| 618 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 619 | struct adt7470_data *data = adt7470_update_device(dev); |
| 620 | |
| 621 | if (FAN_DATA_VALID(data->fan_min[attr->index])) |
| 622 | return sprintf(buf, "%d\n", |
| 623 | FAN_PERIOD_TO_RPM(data->fan_min[attr->index])); |
| 624 | else |
| 625 | return sprintf(buf, "0\n"); |
| 626 | } |
| 627 | |
| 628 | static ssize_t set_fan_min(struct device *dev, |
| 629 | struct device_attribute *devattr, |
| 630 | const char *buf, size_t count) |
| 631 | { |
| 632 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 633 | struct i2c_client *client = to_i2c_client(dev); |
| 634 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 635 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 636 | |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 637 | if (strict_strtol(buf, 10, &temp) || !temp) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 638 | return -EINVAL; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 639 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 640 | temp = FAN_RPM_TO_PERIOD(temp); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 641 | temp = SENSORS_LIMIT(temp, 1, 65534); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 642 | |
| 643 | mutex_lock(&data->lock); |
| 644 | data->fan_min[attr->index] = temp; |
| 645 | adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp); |
| 646 | mutex_unlock(&data->lock); |
| 647 | |
| 648 | return count; |
| 649 | } |
| 650 | |
| 651 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, |
| 652 | char *buf) |
| 653 | { |
| 654 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 655 | struct adt7470_data *data = adt7470_update_device(dev); |
| 656 | |
| 657 | if (FAN_DATA_VALID(data->fan[attr->index])) |
| 658 | return sprintf(buf, "%d\n", |
| 659 | FAN_PERIOD_TO_RPM(data->fan[attr->index])); |
| 660 | else |
| 661 | return sprintf(buf, "0\n"); |
| 662 | } |
| 663 | |
| 664 | static ssize_t show_force_pwm_max(struct device *dev, |
| 665 | struct device_attribute *devattr, |
| 666 | char *buf) |
| 667 | { |
| 668 | struct adt7470_data *data = adt7470_update_device(dev); |
| 669 | return sprintf(buf, "%d\n", data->force_pwm_max); |
| 670 | } |
| 671 | |
| 672 | static ssize_t set_force_pwm_max(struct device *dev, |
| 673 | struct device_attribute *devattr, |
| 674 | const char *buf, |
| 675 | size_t count) |
| 676 | { |
| 677 | struct i2c_client *client = to_i2c_client(dev); |
| 678 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 679 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 680 | u8 reg; |
| 681 | |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 682 | if (strict_strtol(buf, 10, &temp)) |
| 683 | return -EINVAL; |
| 684 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 685 | mutex_lock(&data->lock); |
| 686 | data->force_pwm_max = temp; |
| 687 | reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); |
| 688 | if (temp) |
| 689 | reg |= ADT7470_FSPD_MASK; |
| 690 | else |
| 691 | reg &= ~ADT7470_FSPD_MASK; |
| 692 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg); |
| 693 | mutex_unlock(&data->lock); |
| 694 | |
| 695 | return count; |
| 696 | } |
| 697 | |
| 698 | static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, |
| 699 | char *buf) |
| 700 | { |
| 701 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 702 | struct adt7470_data *data = adt7470_update_device(dev); |
| 703 | return sprintf(buf, "%d\n", data->pwm[attr->index]); |
| 704 | } |
| 705 | |
| 706 | static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, |
| 707 | const char *buf, size_t count) |
| 708 | { |
| 709 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 710 | struct i2c_client *client = to_i2c_client(dev); |
| 711 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 712 | long temp; |
| 713 | |
| 714 | if (strict_strtol(buf, 10, &temp)) |
| 715 | return -EINVAL; |
| 716 | |
| 717 | temp = SENSORS_LIMIT(temp, 0, 255); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 718 | |
| 719 | mutex_lock(&data->lock); |
| 720 | data->pwm[attr->index] = temp; |
| 721 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp); |
| 722 | mutex_unlock(&data->lock); |
| 723 | |
| 724 | return count; |
| 725 | } |
| 726 | |
| 727 | static ssize_t show_pwm_max(struct device *dev, |
| 728 | struct device_attribute *devattr, |
| 729 | char *buf) |
| 730 | { |
| 731 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 732 | struct adt7470_data *data = adt7470_update_device(dev); |
| 733 | return sprintf(buf, "%d\n", data->pwm_max[attr->index]); |
| 734 | } |
| 735 | |
| 736 | static ssize_t set_pwm_max(struct device *dev, |
| 737 | struct device_attribute *devattr, |
| 738 | const char *buf, |
| 739 | size_t count) |
| 740 | { |
| 741 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 742 | struct i2c_client *client = to_i2c_client(dev); |
| 743 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 744 | long temp; |
| 745 | |
| 746 | if (strict_strtol(buf, 10, &temp)) |
| 747 | return -EINVAL; |
| 748 | |
| 749 | temp = SENSORS_LIMIT(temp, 0, 255); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 750 | |
| 751 | mutex_lock(&data->lock); |
| 752 | data->pwm_max[attr->index] = temp; |
| 753 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index), |
| 754 | temp); |
| 755 | mutex_unlock(&data->lock); |
| 756 | |
| 757 | return count; |
| 758 | } |
| 759 | |
| 760 | static ssize_t show_pwm_min(struct device *dev, |
| 761 | struct device_attribute *devattr, |
| 762 | char *buf) |
| 763 | { |
| 764 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 765 | struct adt7470_data *data = adt7470_update_device(dev); |
| 766 | return sprintf(buf, "%d\n", data->pwm_min[attr->index]); |
| 767 | } |
| 768 | |
| 769 | static ssize_t set_pwm_min(struct device *dev, |
| 770 | struct device_attribute *devattr, |
| 771 | const char *buf, |
| 772 | size_t count) |
| 773 | { |
| 774 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 775 | struct i2c_client *client = to_i2c_client(dev); |
| 776 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 777 | long temp; |
| 778 | |
| 779 | if (strict_strtol(buf, 10, &temp)) |
| 780 | return -EINVAL; |
| 781 | |
| 782 | temp = SENSORS_LIMIT(temp, 0, 255); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 783 | |
| 784 | mutex_lock(&data->lock); |
| 785 | data->pwm_min[attr->index] = temp; |
| 786 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index), |
| 787 | temp); |
| 788 | mutex_unlock(&data->lock); |
| 789 | |
| 790 | return count; |
| 791 | } |
| 792 | |
| 793 | static ssize_t show_pwm_tmax(struct device *dev, |
| 794 | struct device_attribute *devattr, |
| 795 | char *buf) |
| 796 | { |
| 797 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 798 | struct adt7470_data *data = adt7470_update_device(dev); |
| 799 | /* the datasheet says that tmax = tmin + 20C */ |
| 800 | return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index])); |
| 801 | } |
| 802 | |
| 803 | static ssize_t show_pwm_tmin(struct device *dev, |
| 804 | struct device_attribute *devattr, |
| 805 | char *buf) |
| 806 | { |
| 807 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 808 | struct adt7470_data *data = adt7470_update_device(dev); |
| 809 | return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]); |
| 810 | } |
| 811 | |
| 812 | static ssize_t set_pwm_tmin(struct device *dev, |
| 813 | struct device_attribute *devattr, |
| 814 | const char *buf, |
| 815 | size_t count) |
| 816 | { |
| 817 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 818 | struct i2c_client *client = to_i2c_client(dev); |
| 819 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 820 | long temp; |
| 821 | |
| 822 | if (strict_strtol(buf, 10, &temp)) |
| 823 | return -EINVAL; |
| 824 | |
Darrick J. Wong | 8f8c1fb | 2009-01-06 14:41:31 -0800 | [diff] [blame] | 825 | temp = DIV_ROUND_CLOSEST(temp, 1000); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 826 | temp = SENSORS_LIMIT(temp, 0, 255); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 827 | |
| 828 | mutex_lock(&data->lock); |
| 829 | data->pwm_tmin[attr->index] = temp; |
| 830 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index), |
| 831 | temp); |
| 832 | mutex_unlock(&data->lock); |
| 833 | |
| 834 | return count; |
| 835 | } |
| 836 | |
| 837 | static ssize_t show_pwm_auto(struct device *dev, |
| 838 | struct device_attribute *devattr, |
| 839 | char *buf) |
| 840 | { |
| 841 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 842 | struct adt7470_data *data = adt7470_update_device(dev); |
| 843 | return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]); |
| 844 | } |
| 845 | |
| 846 | static ssize_t set_pwm_auto(struct device *dev, |
| 847 | struct device_attribute *devattr, |
| 848 | const char *buf, |
| 849 | size_t count) |
| 850 | { |
| 851 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 852 | struct i2c_client *client = to_i2c_client(dev); |
| 853 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 854 | int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index); |
| 855 | int pwm_auto_reg_mask; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 856 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 857 | u8 reg; |
| 858 | |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 859 | if (strict_strtol(buf, 10, &temp)) |
| 860 | return -EINVAL; |
| 861 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 862 | if (attr->index % 2) |
| 863 | pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK; |
| 864 | else |
| 865 | pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK; |
| 866 | |
| 867 | if (temp != 2 && temp != 1) |
| 868 | return -EINVAL; |
| 869 | temp--; |
| 870 | |
| 871 | mutex_lock(&data->lock); |
| 872 | data->pwm_automatic[attr->index] = temp; |
| 873 | reg = i2c_smbus_read_byte_data(client, pwm_auto_reg); |
| 874 | if (temp) |
| 875 | reg |= pwm_auto_reg_mask; |
| 876 | else |
| 877 | reg &= ~pwm_auto_reg_mask; |
| 878 | i2c_smbus_write_byte_data(client, pwm_auto_reg, reg); |
| 879 | mutex_unlock(&data->lock); |
| 880 | |
| 881 | return count; |
| 882 | } |
| 883 | |
| 884 | static ssize_t show_pwm_auto_temp(struct device *dev, |
| 885 | struct device_attribute *devattr, |
| 886 | char *buf) |
| 887 | { |
| 888 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 889 | struct adt7470_data *data = adt7470_update_device(dev); |
| 890 | u8 ctrl = data->pwm_auto_temp[attr->index]; |
| 891 | |
| 892 | if (ctrl) |
| 893 | return sprintf(buf, "%d\n", 1 << (ctrl - 1)); |
| 894 | else |
| 895 | return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS); |
| 896 | } |
| 897 | |
| 898 | static int cvt_auto_temp(int input) |
| 899 | { |
| 900 | if (input == ADT7470_PWM_ALL_TEMPS) |
| 901 | return 0; |
Robert P. J. Day | ce9c2f4 | 2007-11-06 03:21:42 -0500 | [diff] [blame] | 902 | if (input < 1 || !is_power_of_2(input)) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 903 | return -EINVAL; |
| 904 | return ilog2(input) + 1; |
| 905 | } |
| 906 | |
| 907 | static ssize_t set_pwm_auto_temp(struct device *dev, |
| 908 | struct device_attribute *devattr, |
| 909 | const char *buf, |
| 910 | size_t count) |
| 911 | { |
| 912 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 913 | struct i2c_client *client = to_i2c_client(dev); |
| 914 | struct adt7470_data *data = i2c_get_clientdata(client); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 915 | int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 916 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 917 | u8 reg; |
| 918 | |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 919 | if (strict_strtol(buf, 10, &temp)) |
| 920 | return -EINVAL; |
| 921 | |
| 922 | temp = cvt_auto_temp(temp); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 923 | if (temp < 0) |
| 924 | return temp; |
| 925 | |
| 926 | mutex_lock(&data->lock); |
| 927 | data->pwm_automatic[attr->index] = temp; |
| 928 | reg = i2c_smbus_read_byte_data(client, pwm_auto_reg); |
| 929 | |
| 930 | if (!(attr->index % 2)) { |
| 931 | reg &= 0xF; |
| 932 | reg |= (temp << 4) & 0xF0; |
| 933 | } else { |
| 934 | reg &= 0xF0; |
| 935 | reg |= temp & 0xF; |
| 936 | } |
| 937 | |
| 938 | i2c_smbus_write_byte_data(client, pwm_auto_reg, reg); |
| 939 | mutex_unlock(&data->lock); |
| 940 | |
| 941 | return count; |
| 942 | } |
| 943 | |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 944 | static ssize_t show_alarm(struct device *dev, |
| 945 | struct device_attribute *devattr, |
| 946 | char *buf) |
| 947 | { |
| 948 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 949 | struct adt7470_data *data = adt7470_update_device(dev); |
| 950 | |
| 951 | if (data->alarm & attr->index) |
| 952 | return sprintf(buf, "1\n"); |
| 953 | else |
| 954 | return sprintf(buf, "0\n"); |
| 955 | } |
| 956 | |
| 957 | static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL); |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 958 | static DEVICE_ATTR(num_temp_sensors, S_IWUSR | S_IRUGO, show_num_temp_sensors, |
| 959 | set_num_temp_sensors); |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 960 | static DEVICE_ATTR(auto_update_interval, S_IWUSR | S_IRUGO, |
| 961 | show_auto_update_interval, set_auto_update_interval); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 962 | |
| 963 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 964 | set_temp_max, 0); |
| 965 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 966 | set_temp_max, 1); |
| 967 | static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 968 | set_temp_max, 2); |
| 969 | static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 970 | set_temp_max, 3); |
| 971 | static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 972 | set_temp_max, 4); |
| 973 | static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 974 | set_temp_max, 5); |
| 975 | static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 976 | set_temp_max, 6); |
| 977 | static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 978 | set_temp_max, 7); |
| 979 | static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 980 | set_temp_max, 8); |
| 981 | static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 982 | set_temp_max, 9); |
| 983 | |
| 984 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 985 | set_temp_min, 0); |
| 986 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 987 | set_temp_min, 1); |
| 988 | static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 989 | set_temp_min, 2); |
| 990 | static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 991 | set_temp_min, 3); |
| 992 | static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 993 | set_temp_min, 4); |
| 994 | static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 995 | set_temp_min, 5); |
| 996 | static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 997 | set_temp_min, 6); |
| 998 | static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 999 | set_temp_min, 7); |
| 1000 | static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 1001 | set_temp_min, 8); |
| 1002 | static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 1003 | set_temp_min, 9); |
| 1004 | |
| 1005 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 1006 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); |
| 1007 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); |
| 1008 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); |
| 1009 | static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4); |
| 1010 | static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5); |
| 1011 | static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6); |
| 1012 | static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7); |
| 1013 | static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8); |
| 1014 | static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9); |
| 1015 | |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1016 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, |
| 1017 | ADT7470_R1T_ALARM); |
| 1018 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, |
| 1019 | ADT7470_R2T_ALARM); |
| 1020 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, |
| 1021 | ADT7470_R3T_ALARM); |
| 1022 | static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, |
| 1023 | ADT7470_R4T_ALARM); |
| 1024 | static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, |
| 1025 | ADT7470_R5T_ALARM); |
| 1026 | static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, |
| 1027 | ADT7470_R6T_ALARM); |
| 1028 | static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL, |
| 1029 | ADT7470_R7T_ALARM); |
| 1030 | static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL, |
| 1031 | ALARM2(ADT7470_R8T_ALARM)); |
| 1032 | static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL, |
| 1033 | ALARM2(ADT7470_R9T_ALARM)); |
| 1034 | static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL, |
| 1035 | ALARM2(ADT7470_R10T_ALARM)); |
| 1036 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1037 | static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max, |
| 1038 | set_fan_max, 0); |
| 1039 | static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max, |
| 1040 | set_fan_max, 1); |
| 1041 | static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max, |
| 1042 | set_fan_max, 2); |
| 1043 | static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max, |
| 1044 | set_fan_max, 3); |
| 1045 | |
| 1046 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, |
| 1047 | set_fan_min, 0); |
| 1048 | static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, |
| 1049 | set_fan_min, 1); |
| 1050 | static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, |
| 1051 | set_fan_min, 2); |
| 1052 | static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min, |
| 1053 | set_fan_min, 3); |
| 1054 | |
| 1055 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
| 1056 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); |
| 1057 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2); |
| 1058 | static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3); |
| 1059 | |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1060 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, |
| 1061 | ALARM2(ADT7470_FAN1_ALARM)); |
| 1062 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, |
| 1063 | ALARM2(ADT7470_FAN2_ALARM)); |
| 1064 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, |
| 1065 | ALARM2(ADT7470_FAN3_ALARM)); |
| 1066 | static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, |
| 1067 | ALARM2(ADT7470_FAN4_ALARM)); |
| 1068 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1069 | static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO, |
| 1070 | show_force_pwm_max, set_force_pwm_max, 0); |
| 1071 | |
| 1072 | static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0); |
| 1073 | static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1); |
| 1074 | static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2); |
| 1075 | static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3); |
| 1076 | |
| 1077 | static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO, |
| 1078 | show_pwm_min, set_pwm_min, 0); |
| 1079 | static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO, |
| 1080 | show_pwm_min, set_pwm_min, 1); |
| 1081 | static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO, |
| 1082 | show_pwm_min, set_pwm_min, 2); |
| 1083 | static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO, |
| 1084 | show_pwm_min, set_pwm_min, 3); |
| 1085 | |
| 1086 | static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO, |
| 1087 | show_pwm_max, set_pwm_max, 0); |
| 1088 | static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO, |
| 1089 | show_pwm_max, set_pwm_max, 1); |
| 1090 | static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO, |
| 1091 | show_pwm_max, set_pwm_max, 2); |
| 1092 | static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO, |
| 1093 | show_pwm_max, set_pwm_max, 3); |
| 1094 | |
| 1095 | static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO, |
| 1096 | show_pwm_tmin, set_pwm_tmin, 0); |
| 1097 | static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO, |
| 1098 | show_pwm_tmin, set_pwm_tmin, 1); |
| 1099 | static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO, |
| 1100 | show_pwm_tmin, set_pwm_tmin, 2); |
| 1101 | static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO, |
| 1102 | show_pwm_tmin, set_pwm_tmin, 3); |
| 1103 | |
| 1104 | static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax, |
| 1105 | NULL, 0); |
| 1106 | static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax, |
| 1107 | NULL, 1); |
| 1108 | static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax, |
| 1109 | NULL, 2); |
| 1110 | static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax, |
| 1111 | NULL, 3); |
| 1112 | |
| 1113 | static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto, |
| 1114 | set_pwm_auto, 0); |
| 1115 | static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto, |
| 1116 | set_pwm_auto, 1); |
| 1117 | static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto, |
| 1118 | set_pwm_auto, 2); |
| 1119 | static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto, |
| 1120 | set_pwm_auto, 3); |
| 1121 | |
| 1122 | static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO, |
| 1123 | show_pwm_auto_temp, set_pwm_auto_temp, 0); |
| 1124 | static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO, |
| 1125 | show_pwm_auto_temp, set_pwm_auto_temp, 1); |
| 1126 | static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO, |
| 1127 | show_pwm_auto_temp, set_pwm_auto_temp, 2); |
| 1128 | static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO, |
| 1129 | show_pwm_auto_temp, set_pwm_auto_temp, 3); |
| 1130 | |
| 1131 | static struct attribute *adt7470_attr[] = |
| 1132 | { |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1133 | &dev_attr_alarm_mask.attr, |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 1134 | &dev_attr_num_temp_sensors.attr, |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 1135 | &dev_attr_auto_update_interval.attr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1136 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 1137 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 1138 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 1139 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 1140 | &sensor_dev_attr_temp5_max.dev_attr.attr, |
| 1141 | &sensor_dev_attr_temp6_max.dev_attr.attr, |
| 1142 | &sensor_dev_attr_temp7_max.dev_attr.attr, |
| 1143 | &sensor_dev_attr_temp8_max.dev_attr.attr, |
| 1144 | &sensor_dev_attr_temp9_max.dev_attr.attr, |
| 1145 | &sensor_dev_attr_temp10_max.dev_attr.attr, |
| 1146 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 1147 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 1148 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 1149 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
| 1150 | &sensor_dev_attr_temp5_min.dev_attr.attr, |
| 1151 | &sensor_dev_attr_temp6_min.dev_attr.attr, |
| 1152 | &sensor_dev_attr_temp7_min.dev_attr.attr, |
| 1153 | &sensor_dev_attr_temp8_min.dev_attr.attr, |
| 1154 | &sensor_dev_attr_temp9_min.dev_attr.attr, |
| 1155 | &sensor_dev_attr_temp10_min.dev_attr.attr, |
| 1156 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 1157 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 1158 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 1159 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 1160 | &sensor_dev_attr_temp5_input.dev_attr.attr, |
| 1161 | &sensor_dev_attr_temp6_input.dev_attr.attr, |
| 1162 | &sensor_dev_attr_temp7_input.dev_attr.attr, |
| 1163 | &sensor_dev_attr_temp8_input.dev_attr.attr, |
| 1164 | &sensor_dev_attr_temp9_input.dev_attr.attr, |
| 1165 | &sensor_dev_attr_temp10_input.dev_attr.attr, |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1166 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 1167 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 1168 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 1169 | &sensor_dev_attr_temp4_alarm.dev_attr.attr, |
| 1170 | &sensor_dev_attr_temp5_alarm.dev_attr.attr, |
| 1171 | &sensor_dev_attr_temp6_alarm.dev_attr.attr, |
| 1172 | &sensor_dev_attr_temp7_alarm.dev_attr.attr, |
| 1173 | &sensor_dev_attr_temp8_alarm.dev_attr.attr, |
| 1174 | &sensor_dev_attr_temp9_alarm.dev_attr.attr, |
| 1175 | &sensor_dev_attr_temp10_alarm.dev_attr.attr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1176 | &sensor_dev_attr_fan1_max.dev_attr.attr, |
| 1177 | &sensor_dev_attr_fan2_max.dev_attr.attr, |
| 1178 | &sensor_dev_attr_fan3_max.dev_attr.attr, |
| 1179 | &sensor_dev_attr_fan4_max.dev_attr.attr, |
| 1180 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 1181 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 1182 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 1183 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
| 1184 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 1185 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 1186 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 1187 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1188 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 1189 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 1190 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
| 1191 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1192 | &sensor_dev_attr_force_pwm_max.dev_attr.attr, |
| 1193 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 1194 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 1195 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1196 | &sensor_dev_attr_pwm4.dev_attr.attr, |
| 1197 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
| 1198 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, |
| 1199 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, |
| 1200 | &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr, |
| 1201 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, |
| 1202 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, |
| 1203 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, |
| 1204 | &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr, |
| 1205 | &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, |
| 1206 | &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr, |
| 1207 | &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr, |
| 1208 | &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, |
| 1209 | &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, |
| 1210 | &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr, |
| 1211 | &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr, |
| 1212 | &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr, |
| 1213 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 1214 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 1215 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
| 1216 | &sensor_dev_attr_pwm4_enable.dev_attr.attr, |
| 1217 | &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, |
| 1218 | &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, |
| 1219 | &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, |
| 1220 | &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr, |
| 1221 | NULL |
| 1222 | }; |
| 1223 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1224 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 1225 | static int adt7470_detect(struct i2c_client *client, |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1226 | struct i2c_board_info *info) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1227 | { |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1228 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1229 | int vendor, device, revision; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1230 | |
| 1231 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1232 | return -ENODEV; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1233 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1234 | vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR); |
| 1235 | if (vendor != ADT7470_VENDOR) |
| 1236 | return -ENODEV; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1237 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1238 | device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE); |
| 1239 | if (device != ADT7470_DEVICE) |
| 1240 | return -ENODEV; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1241 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1242 | revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION); |
| 1243 | if (revision != ADT7470_REVISION) |
| 1244 | return -ENODEV; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1245 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1246 | strlcpy(info->type, "adt7470", I2C_NAME_SIZE); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1247 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | static int adt7470_probe(struct i2c_client *client, |
| 1252 | const struct i2c_device_id *id) |
| 1253 | { |
| 1254 | struct adt7470_data *data; |
| 1255 | int err; |
| 1256 | |
| 1257 | data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL); |
| 1258 | if (!data) { |
| 1259 | err = -ENOMEM; |
| 1260 | goto exit; |
| 1261 | } |
| 1262 | |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 1263 | data->num_temp_sensors = -1; |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 1264 | data->auto_update_interval = AUTO_UPDATE_INTERVAL; |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 1265 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1266 | i2c_set_clientdata(client, data); |
| 1267 | mutex_init(&data->lock); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1268 | |
| 1269 | dev_info(&client->dev, "%s chip found\n", client->name); |
| 1270 | |
| 1271 | /* Initialize the ADT7470 chip */ |
| 1272 | adt7470_init_client(client); |
| 1273 | |
| 1274 | /* Register sysfs hooks */ |
| 1275 | data->attrs.attrs = adt7470_attr; |
| 1276 | if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs))) |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1277 | goto exit_free; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1278 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1279 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 1280 | if (IS_ERR(data->hwmon_dev)) { |
| 1281 | err = PTR_ERR(data->hwmon_dev); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1282 | goto exit_remove; |
| 1283 | } |
| 1284 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 1285 | init_completion(&data->auto_update_stop); |
| 1286 | data->auto_update = kthread_run(adt7470_update_thread, client, |
| 1287 | dev_name(data->hwmon_dev)); |
| 1288 | if (IS_ERR(data->auto_update)) |
| 1289 | goto exit_unregister; |
| 1290 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1291 | return 0; |
| 1292 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 1293 | exit_unregister: |
| 1294 | hwmon_device_unregister(data->hwmon_dev); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1295 | exit_remove: |
| 1296 | sysfs_remove_group(&client->dev.kobj, &data->attrs); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1297 | exit_free: |
| 1298 | kfree(data); |
| 1299 | exit: |
| 1300 | return err; |
| 1301 | } |
| 1302 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1303 | static int adt7470_remove(struct i2c_client *client) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1304 | { |
| 1305 | struct adt7470_data *data = i2c_get_clientdata(client); |
| 1306 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 1307 | kthread_stop(data->auto_update); |
| 1308 | wait_for_completion(&data->auto_update_stop); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1309 | hwmon_device_unregister(data->hwmon_dev); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1310 | sysfs_remove_group(&client->dev.kobj, &data->attrs); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1311 | kfree(data); |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | static int __init adt7470_init(void) |
| 1316 | { |
| 1317 | return i2c_add_driver(&adt7470_driver); |
| 1318 | } |
| 1319 | |
| 1320 | static void __exit adt7470_exit(void) |
| 1321 | { |
| 1322 | i2c_del_driver(&adt7470_driver); |
| 1323 | } |
| 1324 | |
| 1325 | MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>"); |
| 1326 | MODULE_DESCRIPTION("ADT7470 driver"); |
| 1327 | MODULE_LICENSE("GPL"); |
| 1328 | |
| 1329 | module_init(adt7470_init); |
| 1330 | module_exit(adt7470_exit); |