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