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