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; |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 180 | unsigned int auto_update_interval; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 183 | /* |
| 184 | * 16-bit registers on the ADT7470 are low-byte first. The data sheet says |
| 185 | * that the low byte must be read before the high byte. |
| 186 | */ |
| 187 | static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg) |
| 188 | { |
| 189 | u16 foo; |
| 190 | foo = i2c_smbus_read_byte_data(client, reg); |
| 191 | foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8); |
| 192 | return foo; |
| 193 | } |
| 194 | |
| 195 | static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg, |
| 196 | u16 value) |
| 197 | { |
| 198 | return i2c_smbus_write_byte_data(client, reg, value & 0xFF) |
Curt Brune | 93d783b | 2013-08-08 12:11:03 -0700 | [diff] [blame] | 199 | || i2c_smbus_write_byte_data(client, reg + 1, value >> 8); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 202 | /* Probe for temperature sensors. Assumes lock is held */ |
| 203 | static int adt7470_read_temperatures(struct i2c_client *client, |
| 204 | struct adt7470_data *data) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 205 | { |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 206 | unsigned long res; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 207 | int i; |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 208 | u8 cfg, pwm[4], pwm_cfg[2]; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 209 | |
Darrick J. Wong | 2e75a4b | 2009-01-06 14:41:32 -0800 | [diff] [blame] | 210 | /* save pwm[1-4] config register */ |
| 211 | pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0)); |
| 212 | pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2)); |
| 213 | |
| 214 | /* set manual pwm to whatever it is set to now */ |
| 215 | for (i = 0; i < ADT7470_FAN_COUNT; i++) |
| 216 | pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i)); |
| 217 | |
| 218 | /* put pwm in manual mode */ |
| 219 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), |
| 220 | pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK)); |
| 221 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), |
| 222 | pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK)); |
| 223 | |
| 224 | /* write pwm control to whatever it was */ |
| 225 | for (i = 0; i < ADT7470_FAN_COUNT; i++) |
| 226 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]); |
| 227 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 228 | /* start reading temperature sensors */ |
| 229 | cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); |
| 230 | cfg |= 0x80; |
| 231 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg); |
| 232 | |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 233 | /* Delay is 200ms * number of temp sensors. */ |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 234 | res = msleep_interruptible((data->num_temp_sensors >= 0 ? |
| 235 | data->num_temp_sensors * 200 : |
| 236 | TEMP_COLLECTION_TIME)); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 237 | |
| 238 | /* done reading temperature sensors */ |
| 239 | cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); |
| 240 | cfg &= ~0x80; |
| 241 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg); |
| 242 | |
Darrick J. Wong | 2e75a4b | 2009-01-06 14:41:32 -0800 | [diff] [blame] | 243 | /* restore pwm[1-4] config registers */ |
| 244 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]); |
| 245 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]); |
| 246 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 247 | if (res) { |
Joe Perches | 2e99120 | 2010-10-20 06:51:27 +0000 | [diff] [blame] | 248 | pr_err("ha ha, interrupted\n"); |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 249 | return -EAGAIN; |
| 250 | } |
| 251 | |
| 252 | /* Only count fans if we have to */ |
| 253 | if (data->num_temp_sensors >= 0) |
| 254 | return 0; |
| 255 | |
| 256 | for (i = 0; i < ADT7470_TEMP_COUNT; i++) { |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 257 | data->temp[i] = i2c_smbus_read_byte_data(client, |
| 258 | ADT7470_TEMP_REG(i)); |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 259 | if (data->temp[i]) |
| 260 | data->num_temp_sensors = i + 1; |
| 261 | } |
| 262 | data->temperatures_probed = 1; |
| 263 | return 0; |
| 264 | } |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 265 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 266 | static int adt7470_update_thread(void *p) |
| 267 | { |
| 268 | struct i2c_client *client = p; |
| 269 | struct adt7470_data *data = i2c_get_clientdata(client); |
| 270 | |
| 271 | while (!kthread_should_stop()) { |
| 272 | mutex_lock(&data->lock); |
| 273 | adt7470_read_temperatures(client, data); |
| 274 | mutex_unlock(&data->lock); |
Joshua Scott | 93cacfd | 2016-09-09 17:19:26 +1200 | [diff] [blame] | 275 | |
| 276 | set_current_state(TASK_INTERRUPTIBLE); |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 277 | if (kthread_should_stop()) |
| 278 | break; |
Joshua Scott | 93cacfd | 2016-09-09 17:19:26 +1200 | [diff] [blame] | 279 | |
| 280 | schedule_timeout(msecs_to_jiffies(data->auto_update_interval)); |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static struct adt7470_data *adt7470_update_device(struct device *dev) |
| 287 | { |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 288 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 289 | struct i2c_client *client = data->client; |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 290 | unsigned long local_jiffies = jiffies; |
| 291 | u8 cfg; |
| 292 | int i; |
| 293 | int need_sensors = 1; |
| 294 | int need_limits = 1; |
| 295 | |
| 296 | /* |
| 297 | * Figure out if we need to update the shadow registers. |
| 298 | * Lockless means that we may occasionally report out of |
| 299 | * date data. |
| 300 | */ |
| 301 | if (time_before(local_jiffies, data->sensors_last_updated + |
| 302 | SENSOR_REFRESH_INTERVAL) && |
| 303 | data->sensors_valid) |
| 304 | need_sensors = 0; |
| 305 | |
| 306 | if (time_before(local_jiffies, data->limits_last_updated + |
| 307 | LIMIT_REFRESH_INTERVAL) && |
| 308 | data->limits_valid) |
| 309 | need_limits = 0; |
| 310 | |
| 311 | if (!need_sensors && !need_limits) |
| 312 | return data; |
| 313 | |
| 314 | mutex_lock(&data->lock); |
| 315 | if (!need_sensors) |
| 316 | goto no_sensor_update; |
| 317 | |
| 318 | if (!data->temperatures_probed) |
| 319 | adt7470_read_temperatures(client, data); |
| 320 | else |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 321 | for (i = 0; i < ADT7470_TEMP_COUNT; i++) |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 322 | data->temp[i] = i2c_smbus_read_byte_data(client, |
| 323 | ADT7470_TEMP_REG(i)); |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 324 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 325 | for (i = 0; i < ADT7470_FAN_COUNT; i++) |
| 326 | data->fan[i] = adt7470_read_word_data(client, |
| 327 | ADT7470_REG_FAN(i)); |
| 328 | |
| 329 | for (i = 0; i < ADT7470_PWM_COUNT; i++) { |
| 330 | int reg; |
| 331 | int reg_mask; |
| 332 | |
| 333 | data->pwm[i] = i2c_smbus_read_byte_data(client, |
| 334 | ADT7470_REG_PWM(i)); |
| 335 | |
| 336 | if (i % 2) |
| 337 | reg_mask = ADT7470_PWM2_AUTO_MASK; |
| 338 | else |
| 339 | reg_mask = ADT7470_PWM1_AUTO_MASK; |
| 340 | |
| 341 | reg = ADT7470_REG_PWM_CFG(i); |
| 342 | if (i2c_smbus_read_byte_data(client, reg) & reg_mask) |
| 343 | data->pwm_automatic[i] = 1; |
| 344 | else |
| 345 | data->pwm_automatic[i] = 0; |
| 346 | |
| 347 | reg = ADT7470_REG_PWM_AUTO_TEMP(i); |
| 348 | cfg = i2c_smbus_read_byte_data(client, reg); |
| 349 | if (!(i % 2)) |
| 350 | data->pwm_auto_temp[i] = cfg >> 4; |
| 351 | else |
| 352 | data->pwm_auto_temp[i] = cfg & 0xF; |
| 353 | } |
| 354 | |
| 355 | if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) & |
| 356 | ADT7470_FSPD_MASK) |
| 357 | data->force_pwm_max = 1; |
| 358 | else |
| 359 | data->force_pwm_max = 0; |
| 360 | |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 361 | data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1); |
| 362 | if (data->alarm & ADT7470_OOL_ALARM) |
| 363 | data->alarm |= ALARM2(i2c_smbus_read_byte_data(client, |
| 364 | ADT7470_REG_ALARM2)); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 365 | data->alarms_mask = adt7470_read_word_data(client, |
| 366 | ADT7470_REG_ALARM1_MASK); |
| 367 | |
| 368 | data->sensors_last_updated = local_jiffies; |
| 369 | data->sensors_valid = 1; |
| 370 | |
| 371 | no_sensor_update: |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 372 | if (!need_limits) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 373 | goto out; |
| 374 | |
| 375 | for (i = 0; i < ADT7470_TEMP_COUNT; i++) { |
| 376 | data->temp_min[i] = i2c_smbus_read_byte_data(client, |
| 377 | ADT7470_TEMP_MIN_REG(i)); |
| 378 | data->temp_max[i] = i2c_smbus_read_byte_data(client, |
| 379 | ADT7470_TEMP_MAX_REG(i)); |
| 380 | } |
| 381 | |
| 382 | for (i = 0; i < ADT7470_FAN_COUNT; i++) { |
| 383 | data->fan_min[i] = adt7470_read_word_data(client, |
| 384 | ADT7470_REG_FAN_MIN(i)); |
| 385 | data->fan_max[i] = adt7470_read_word_data(client, |
| 386 | ADT7470_REG_FAN_MAX(i)); |
| 387 | } |
| 388 | |
| 389 | for (i = 0; i < ADT7470_PWM_COUNT; i++) { |
| 390 | data->pwm_max[i] = i2c_smbus_read_byte_data(client, |
| 391 | ADT7470_REG_PWM_MAX(i)); |
| 392 | data->pwm_min[i] = i2c_smbus_read_byte_data(client, |
| 393 | ADT7470_REG_PWM_MIN(i)); |
| 394 | data->pwm_tmin[i] = i2c_smbus_read_byte_data(client, |
| 395 | ADT7470_REG_PWM_TMIN(i)); |
| 396 | } |
| 397 | |
| 398 | data->limits_last_updated = local_jiffies; |
| 399 | data->limits_valid = 1; |
| 400 | |
| 401 | out: |
| 402 | mutex_unlock(&data->lock); |
| 403 | return data; |
| 404 | } |
| 405 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 406 | static ssize_t auto_update_interval_show(struct device *dev, |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 407 | struct device_attribute *devattr, |
| 408 | char *buf) |
| 409 | { |
| 410 | struct adt7470_data *data = adt7470_update_device(dev); |
| 411 | return sprintf(buf, "%d\n", data->auto_update_interval); |
| 412 | } |
| 413 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 414 | static ssize_t auto_update_interval_store(struct device *dev, |
| 415 | struct device_attribute *devattr, |
| 416 | const char *buf, size_t count) |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 417 | { |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 418 | struct adt7470_data *data = dev_get_drvdata(dev); |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 419 | long temp; |
| 420 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 421 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 422 | return -EINVAL; |
| 423 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 424 | temp = clamp_val(temp, 0, 60000); |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 425 | |
| 426 | mutex_lock(&data->lock); |
| 427 | data->auto_update_interval = temp; |
| 428 | mutex_unlock(&data->lock); |
| 429 | |
| 430 | return count; |
| 431 | } |
| 432 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 433 | static ssize_t num_temp_sensors_show(struct device *dev, |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 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->num_temp_sensors); |
| 439 | } |
| 440 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 441 | static ssize_t num_temp_sensors_store(struct device *dev, |
| 442 | struct device_attribute *devattr, |
| 443 | const char *buf, size_t count) |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 444 | { |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 445 | struct adt7470_data *data = dev_get_drvdata(dev); |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 446 | long temp; |
| 447 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 448 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 449 | return -EINVAL; |
| 450 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 451 | temp = clamp_val(temp, -1, 10); |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 452 | |
| 453 | mutex_lock(&data->lock); |
| 454 | data->num_temp_sensors = temp; |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 455 | if (temp < 0) |
| 456 | data->temperatures_probed = 0; |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 457 | mutex_unlock(&data->lock); |
| 458 | |
| 459 | return count; |
| 460 | } |
| 461 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 462 | static ssize_t temp_min_show(struct device *dev, |
| 463 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 464 | { |
| 465 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 466 | struct adt7470_data *data = adt7470_update_device(dev); |
| 467 | return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]); |
| 468 | } |
| 469 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 470 | static ssize_t temp_min_store(struct device *dev, |
| 471 | struct device_attribute *devattr, |
| 472 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 473 | { |
| 474 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 475 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 476 | struct i2c_client *client = data->client; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 477 | long temp; |
| 478 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 479 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 480 | return -EINVAL; |
| 481 | |
Guenter Roeck | 64bd708 | 2016-12-03 11:10:34 -0800 | [diff] [blame] | 482 | temp = clamp_val(temp, -128000, 127000); |
Darrick J. Wong | 8f8c1fb | 2009-01-06 14:41:31 -0800 | [diff] [blame] | 483 | temp = DIV_ROUND_CLOSEST(temp, 1000); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 484 | |
| 485 | mutex_lock(&data->lock); |
| 486 | data->temp_min[attr->index] = temp; |
| 487 | i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index), |
| 488 | temp); |
| 489 | mutex_unlock(&data->lock); |
| 490 | |
| 491 | return count; |
| 492 | } |
| 493 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 494 | static ssize_t temp_max_show(struct device *dev, |
| 495 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 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_max[attr->index]); |
| 500 | } |
| 501 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 502 | static ssize_t temp_max_store(struct device *dev, |
| 503 | struct device_attribute *devattr, |
| 504 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 505 | { |
| 506 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 507 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 508 | struct i2c_client *client = data->client; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 509 | long temp; |
| 510 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 511 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 512 | return -EINVAL; |
| 513 | |
Guenter Roeck | 64bd708 | 2016-12-03 11:10:34 -0800 | [diff] [blame] | 514 | temp = clamp_val(temp, -128000, 127000); |
Darrick J. Wong | 8f8c1fb | 2009-01-06 14:41:31 -0800 | [diff] [blame] | 515 | temp = DIV_ROUND_CLOSEST(temp, 1000); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 516 | |
| 517 | mutex_lock(&data->lock); |
| 518 | data->temp_max[attr->index] = temp; |
| 519 | i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index), |
| 520 | temp); |
| 521 | mutex_unlock(&data->lock); |
| 522 | |
| 523 | return count; |
| 524 | } |
| 525 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 526 | static ssize_t temp_show(struct device *dev, struct device_attribute *devattr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 527 | char *buf) |
| 528 | { |
| 529 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 530 | struct adt7470_data *data = adt7470_update_device(dev); |
| 531 | return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]); |
| 532 | } |
| 533 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 534 | static ssize_t alarm_mask_show(struct device *dev, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 535 | struct device_attribute *devattr, |
| 536 | char *buf) |
| 537 | { |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 538 | struct adt7470_data *data = adt7470_update_device(dev); |
| 539 | |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 540 | return sprintf(buf, "%x\n", data->alarms_mask); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 543 | static ssize_t alarm_mask_store(struct device *dev, |
| 544 | struct device_attribute *devattr, |
| 545 | const char *buf, size_t count) |
Joshua Scott | feca313 | 2016-09-09 17:19:25 +1200 | [diff] [blame] | 546 | { |
| 547 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 548 | long mask; |
| 549 | |
| 550 | if (kstrtoul(buf, 0, &mask)) |
| 551 | return -EINVAL; |
| 552 | |
| 553 | if (mask & ~0xffff) |
| 554 | return -EINVAL; |
| 555 | |
| 556 | mutex_lock(&data->lock); |
| 557 | data->alarms_mask = mask; |
| 558 | adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask); |
| 559 | mutex_unlock(&data->lock); |
| 560 | |
| 561 | return count; |
| 562 | } |
| 563 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 564 | static ssize_t fan_max_show(struct device *dev, |
| 565 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 566 | { |
| 567 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 568 | struct adt7470_data *data = adt7470_update_device(dev); |
| 569 | |
| 570 | if (FAN_DATA_VALID(data->fan_max[attr->index])) |
| 571 | return sprintf(buf, "%d\n", |
| 572 | FAN_PERIOD_TO_RPM(data->fan_max[attr->index])); |
| 573 | else |
| 574 | return sprintf(buf, "0\n"); |
| 575 | } |
| 576 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 577 | static ssize_t fan_max_store(struct device *dev, |
| 578 | struct device_attribute *devattr, |
| 579 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 580 | { |
| 581 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 582 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 583 | struct i2c_client *client = data->client; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 584 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 585 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 586 | if (kstrtol(buf, 10, &temp) || !temp) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 587 | return -EINVAL; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 588 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 589 | temp = FAN_RPM_TO_PERIOD(temp); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 590 | temp = clamp_val(temp, 1, 65534); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 591 | |
| 592 | mutex_lock(&data->lock); |
| 593 | data->fan_max[attr->index] = temp; |
| 594 | adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp); |
| 595 | mutex_unlock(&data->lock); |
| 596 | |
| 597 | return count; |
| 598 | } |
| 599 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 600 | static ssize_t fan_min_show(struct device *dev, |
| 601 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 602 | { |
| 603 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 604 | struct adt7470_data *data = adt7470_update_device(dev); |
| 605 | |
| 606 | if (FAN_DATA_VALID(data->fan_min[attr->index])) |
| 607 | return sprintf(buf, "%d\n", |
| 608 | FAN_PERIOD_TO_RPM(data->fan_min[attr->index])); |
| 609 | else |
| 610 | return sprintf(buf, "0\n"); |
| 611 | } |
| 612 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 613 | static ssize_t fan_min_store(struct device *dev, |
| 614 | struct device_attribute *devattr, |
| 615 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 616 | { |
| 617 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 618 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 619 | struct i2c_client *client = data->client; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 620 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 621 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 622 | if (kstrtol(buf, 10, &temp) || !temp) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 623 | return -EINVAL; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 624 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 625 | temp = FAN_RPM_TO_PERIOD(temp); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 626 | temp = clamp_val(temp, 1, 65534); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 627 | |
| 628 | mutex_lock(&data->lock); |
| 629 | data->fan_min[attr->index] = temp; |
| 630 | adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp); |
| 631 | mutex_unlock(&data->lock); |
| 632 | |
| 633 | return count; |
| 634 | } |
| 635 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 636 | static ssize_t fan_show(struct device *dev, struct device_attribute *devattr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 637 | char *buf) |
| 638 | { |
| 639 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 640 | struct adt7470_data *data = adt7470_update_device(dev); |
| 641 | |
| 642 | if (FAN_DATA_VALID(data->fan[attr->index])) |
| 643 | return sprintf(buf, "%d\n", |
| 644 | FAN_PERIOD_TO_RPM(data->fan[attr->index])); |
| 645 | else |
| 646 | return sprintf(buf, "0\n"); |
| 647 | } |
| 648 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 649 | static ssize_t force_pwm_max_show(struct device *dev, |
| 650 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 651 | { |
| 652 | struct adt7470_data *data = adt7470_update_device(dev); |
| 653 | return sprintf(buf, "%d\n", data->force_pwm_max); |
| 654 | } |
| 655 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 656 | static ssize_t force_pwm_max_store(struct device *dev, |
| 657 | struct device_attribute *devattr, |
| 658 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 659 | { |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 660 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 661 | struct i2c_client *client = data->client; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 662 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 663 | u8 reg; |
| 664 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 665 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 666 | return -EINVAL; |
| 667 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 668 | mutex_lock(&data->lock); |
| 669 | data->force_pwm_max = temp; |
| 670 | reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); |
| 671 | if (temp) |
| 672 | reg |= ADT7470_FSPD_MASK; |
| 673 | else |
| 674 | reg &= ~ADT7470_FSPD_MASK; |
| 675 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg); |
| 676 | mutex_unlock(&data->lock); |
| 677 | |
| 678 | return count; |
| 679 | } |
| 680 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 681 | static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 682 | char *buf) |
| 683 | { |
| 684 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 685 | struct adt7470_data *data = adt7470_update_device(dev); |
| 686 | return sprintf(buf, "%d\n", data->pwm[attr->index]); |
| 687 | } |
| 688 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 689 | static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr, |
| 690 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 691 | { |
| 692 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 693 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 694 | struct i2c_client *client = data->client; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 695 | long temp; |
| 696 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 697 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 698 | return -EINVAL; |
| 699 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 700 | temp = clamp_val(temp, 0, 255); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 701 | |
| 702 | mutex_lock(&data->lock); |
| 703 | data->pwm[attr->index] = temp; |
| 704 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp); |
| 705 | mutex_unlock(&data->lock); |
| 706 | |
| 707 | return count; |
| 708 | } |
| 709 | |
Joshua Scott | aa18cc9 | 2016-08-08 13:35:45 +1200 | [diff] [blame] | 710 | /* These are the valid PWM frequencies to the nearest Hz */ |
| 711 | static const int adt7470_freq_map[] = { |
| 712 | 11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500 |
| 713 | }; |
| 714 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 715 | static ssize_t pwm1_freq_show(struct device *dev, |
| 716 | struct device_attribute *devattr, char *buf) |
Joshua Scott | aa18cc9 | 2016-08-08 13:35:45 +1200 | [diff] [blame] | 717 | { |
| 718 | struct adt7470_data *data = adt7470_update_device(dev); |
| 719 | unsigned char cfg_reg_1; |
| 720 | unsigned char cfg_reg_2; |
| 721 | int index; |
| 722 | |
| 723 | mutex_lock(&data->lock); |
| 724 | cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG); |
| 725 | cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2); |
| 726 | mutex_unlock(&data->lock); |
| 727 | |
| 728 | index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT; |
| 729 | if (!(cfg_reg_1 & ADT7470_CFG_LF)) |
| 730 | index += 8; |
| 731 | if (index >= ARRAY_SIZE(adt7470_freq_map)) |
| 732 | index = ARRAY_SIZE(adt7470_freq_map) - 1; |
| 733 | |
| 734 | return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]); |
| 735 | } |
| 736 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 737 | static ssize_t pwm1_freq_store(struct device *dev, |
| 738 | struct device_attribute *devattr, |
| 739 | const char *buf, size_t count) |
Joshua Scott | aa18cc9 | 2016-08-08 13:35:45 +1200 | [diff] [blame] | 740 | { |
| 741 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 742 | struct i2c_client *client = data->client; |
| 743 | long freq; |
| 744 | int index; |
| 745 | int low_freq = ADT7470_CFG_LF; |
| 746 | unsigned char val; |
| 747 | |
| 748 | if (kstrtol(buf, 10, &freq)) |
| 749 | return -EINVAL; |
| 750 | |
| 751 | /* Round the user value given to the closest available frequency */ |
| 752 | index = find_closest(freq, adt7470_freq_map, |
| 753 | ARRAY_SIZE(adt7470_freq_map)); |
| 754 | |
| 755 | if (index >= 8) { |
| 756 | index -= 8; |
| 757 | low_freq = 0; |
| 758 | } |
| 759 | |
| 760 | mutex_lock(&data->lock); |
| 761 | /* Configuration Register 1 */ |
| 762 | val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); |
| 763 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, |
| 764 | (val & ~ADT7470_CFG_LF) | low_freq); |
| 765 | /* Configuration Register 2 */ |
| 766 | val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2); |
| 767 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2, |
| 768 | (val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT)); |
| 769 | mutex_unlock(&data->lock); |
| 770 | |
| 771 | return count; |
| 772 | } |
| 773 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 774 | static ssize_t pwm_max_show(struct device *dev, |
| 775 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 776 | { |
| 777 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 778 | struct adt7470_data *data = adt7470_update_device(dev); |
| 779 | return sprintf(buf, "%d\n", data->pwm_max[attr->index]); |
| 780 | } |
| 781 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 782 | static ssize_t pwm_max_store(struct device *dev, |
| 783 | struct device_attribute *devattr, |
| 784 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 785 | { |
| 786 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 787 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 788 | struct i2c_client *client = data->client; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 789 | long temp; |
| 790 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 791 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 792 | return -EINVAL; |
| 793 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 794 | temp = clamp_val(temp, 0, 255); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 795 | |
| 796 | mutex_lock(&data->lock); |
| 797 | data->pwm_max[attr->index] = temp; |
| 798 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index), |
| 799 | temp); |
| 800 | mutex_unlock(&data->lock); |
| 801 | |
| 802 | return count; |
| 803 | } |
| 804 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 805 | static ssize_t pwm_min_show(struct device *dev, |
| 806 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 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", data->pwm_min[attr->index]); |
| 811 | } |
| 812 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 813 | static ssize_t pwm_min_store(struct device *dev, |
| 814 | struct device_attribute *devattr, |
| 815 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 816 | { |
| 817 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 818 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 819 | struct i2c_client *client = data->client; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 820 | long temp; |
| 821 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 822 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 823 | return -EINVAL; |
| 824 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 825 | temp = clamp_val(temp, 0, 255); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 826 | |
| 827 | mutex_lock(&data->lock); |
| 828 | data->pwm_min[attr->index] = temp; |
| 829 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index), |
| 830 | temp); |
| 831 | mutex_unlock(&data->lock); |
| 832 | |
| 833 | return count; |
| 834 | } |
| 835 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 836 | static ssize_t pwm_tmax_show(struct device *dev, |
| 837 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 838 | { |
| 839 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 840 | struct adt7470_data *data = adt7470_update_device(dev); |
| 841 | /* the datasheet says that tmax = tmin + 20C */ |
| 842 | return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index])); |
| 843 | } |
| 844 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 845 | static ssize_t pwm_tmin_show(struct device *dev, |
| 846 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 847 | { |
| 848 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 849 | struct adt7470_data *data = adt7470_update_device(dev); |
| 850 | return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]); |
| 851 | } |
| 852 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 853 | static ssize_t pwm_tmin_store(struct device *dev, |
| 854 | struct device_attribute *devattr, |
| 855 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 856 | { |
| 857 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 858 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 859 | struct i2c_client *client = data->client; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 860 | long temp; |
| 861 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 862 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 863 | return -EINVAL; |
| 864 | |
Guenter Roeck | 64bd708 | 2016-12-03 11:10:34 -0800 | [diff] [blame] | 865 | temp = clamp_val(temp, -128000, 127000); |
Darrick J. Wong | 8f8c1fb | 2009-01-06 14:41:31 -0800 | [diff] [blame] | 866 | temp = DIV_ROUND_CLOSEST(temp, 1000); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 867 | |
| 868 | mutex_lock(&data->lock); |
| 869 | data->pwm_tmin[attr->index] = temp; |
| 870 | i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index), |
| 871 | temp); |
| 872 | mutex_unlock(&data->lock); |
| 873 | |
| 874 | return count; |
| 875 | } |
| 876 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 877 | static ssize_t pwm_auto_show(struct device *dev, |
| 878 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 879 | { |
| 880 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 881 | struct adt7470_data *data = adt7470_update_device(dev); |
| 882 | return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]); |
| 883 | } |
| 884 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 885 | static ssize_t pwm_auto_store(struct device *dev, |
| 886 | struct device_attribute *devattr, |
| 887 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 888 | { |
| 889 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 890 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 891 | struct i2c_client *client = data->client; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 892 | int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index); |
| 893 | int pwm_auto_reg_mask; |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 894 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 895 | u8 reg; |
| 896 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 897 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 898 | return -EINVAL; |
| 899 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 900 | if (attr->index % 2) |
| 901 | pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK; |
| 902 | else |
| 903 | pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK; |
| 904 | |
| 905 | if (temp != 2 && temp != 1) |
| 906 | return -EINVAL; |
| 907 | temp--; |
| 908 | |
| 909 | mutex_lock(&data->lock); |
| 910 | data->pwm_automatic[attr->index] = temp; |
| 911 | reg = i2c_smbus_read_byte_data(client, pwm_auto_reg); |
| 912 | if (temp) |
| 913 | reg |= pwm_auto_reg_mask; |
| 914 | else |
| 915 | reg &= ~pwm_auto_reg_mask; |
| 916 | i2c_smbus_write_byte_data(client, pwm_auto_reg, reg); |
| 917 | mutex_unlock(&data->lock); |
| 918 | |
| 919 | return count; |
| 920 | } |
| 921 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 922 | static ssize_t pwm_auto_temp_show(struct device *dev, |
| 923 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 924 | { |
| 925 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 926 | struct adt7470_data *data = adt7470_update_device(dev); |
| 927 | u8 ctrl = data->pwm_auto_temp[attr->index]; |
| 928 | |
| 929 | if (ctrl) |
| 930 | return sprintf(buf, "%d\n", 1 << (ctrl - 1)); |
| 931 | else |
| 932 | return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS); |
| 933 | } |
| 934 | |
| 935 | static int cvt_auto_temp(int input) |
| 936 | { |
| 937 | if (input == ADT7470_PWM_ALL_TEMPS) |
| 938 | return 0; |
Robert P. J. Day | ce9c2f4 | 2007-11-06 03:21:42 -0500 | [diff] [blame] | 939 | if (input < 1 || !is_power_of_2(input)) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 940 | return -EINVAL; |
| 941 | return ilog2(input) + 1; |
| 942 | } |
| 943 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 944 | static ssize_t pwm_auto_temp_store(struct device *dev, |
| 945 | struct device_attribute *devattr, |
| 946 | const char *buf, size_t count) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 947 | { |
| 948 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 949 | struct adt7470_data *data = dev_get_drvdata(dev); |
| 950 | struct i2c_client *client = data->client; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 951 | int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index); |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 952 | long temp; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 953 | u8 reg; |
| 954 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 955 | if (kstrtol(buf, 10, &temp)) |
Darrick J. Wong | 05a9bd4 | 2008-11-12 13:26:57 -0800 | [diff] [blame] | 956 | return -EINVAL; |
| 957 | |
| 958 | temp = cvt_auto_temp(temp); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 959 | if (temp < 0) |
| 960 | return temp; |
| 961 | |
| 962 | mutex_lock(&data->lock); |
| 963 | data->pwm_automatic[attr->index] = temp; |
| 964 | reg = i2c_smbus_read_byte_data(client, pwm_auto_reg); |
| 965 | |
| 966 | if (!(attr->index % 2)) { |
| 967 | reg &= 0xF; |
| 968 | reg |= (temp << 4) & 0xF0; |
| 969 | } else { |
| 970 | reg &= 0xF0; |
| 971 | reg |= temp & 0xF; |
| 972 | } |
| 973 | |
| 974 | i2c_smbus_write_byte_data(client, pwm_auto_reg, reg); |
| 975 | mutex_unlock(&data->lock); |
| 976 | |
| 977 | return count; |
| 978 | } |
| 979 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 980 | static ssize_t alarm_show(struct device *dev, |
| 981 | struct device_attribute *devattr, char *buf) |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 982 | { |
| 983 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 984 | struct adt7470_data *data = adt7470_update_device(dev); |
| 985 | |
| 986 | if (data->alarm & attr->index) |
| 987 | return sprintf(buf, "1\n"); |
| 988 | else |
| 989 | return sprintf(buf, "0\n"); |
| 990 | } |
| 991 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 992 | static DEVICE_ATTR_RW(alarm_mask); |
| 993 | static DEVICE_ATTR_RW(num_temp_sensors); |
| 994 | static DEVICE_ATTR_RW(auto_update_interval); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 995 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 996 | static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); |
| 997 | static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); |
| 998 | static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2); |
| 999 | static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3); |
| 1000 | static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4); |
| 1001 | static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5); |
| 1002 | static SENSOR_DEVICE_ATTR_RW(temp7_max, temp_max, 6); |
| 1003 | static SENSOR_DEVICE_ATTR_RW(temp8_max, temp_max, 7); |
| 1004 | static SENSOR_DEVICE_ATTR_RW(temp9_max, temp_max, 8); |
| 1005 | static SENSOR_DEVICE_ATTR_RW(temp10_max, temp_max, 9); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1006 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1007 | static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0); |
| 1008 | static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1); |
| 1009 | static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2); |
| 1010 | static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3); |
| 1011 | static SENSOR_DEVICE_ATTR_RW(temp5_min, temp_min, 4); |
| 1012 | static SENSOR_DEVICE_ATTR_RW(temp6_min, temp_min, 5); |
| 1013 | static SENSOR_DEVICE_ATTR_RW(temp7_min, temp_min, 6); |
| 1014 | static SENSOR_DEVICE_ATTR_RW(temp8_min, temp_min, 7); |
| 1015 | static SENSOR_DEVICE_ATTR_RW(temp9_min, temp_min, 8); |
| 1016 | static SENSOR_DEVICE_ATTR_RW(temp10_min, temp_min, 9); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1017 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1018 | static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0); |
| 1019 | static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1); |
| 1020 | static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2); |
| 1021 | static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3); |
| 1022 | static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4); |
| 1023 | static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5); |
| 1024 | static SENSOR_DEVICE_ATTR_RO(temp7_input, temp, 6); |
| 1025 | static SENSOR_DEVICE_ATTR_RO(temp8_input, temp, 7); |
| 1026 | static SENSOR_DEVICE_ATTR_RO(temp9_input, temp, 8); |
| 1027 | static SENSOR_DEVICE_ATTR_RO(temp10_input, temp, 9); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1028 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1029 | static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, ADT7470_R1T_ALARM); |
| 1030 | static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, ADT7470_R2T_ALARM); |
| 1031 | static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, ADT7470_R3T_ALARM); |
| 1032 | static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, ADT7470_R4T_ALARM); |
| 1033 | static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, ADT7470_R5T_ALARM); |
| 1034 | static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, ADT7470_R6T_ALARM); |
| 1035 | static SENSOR_DEVICE_ATTR_RO(temp7_alarm, alarm, ADT7470_R7T_ALARM); |
| 1036 | static SENSOR_DEVICE_ATTR_RO(temp8_alarm, alarm, ALARM2(ADT7470_R8T_ALARM)); |
| 1037 | static SENSOR_DEVICE_ATTR_RO(temp9_alarm, alarm, ALARM2(ADT7470_R9T_ALARM)); |
| 1038 | static SENSOR_DEVICE_ATTR_RO(temp10_alarm, alarm, ALARM2(ADT7470_R10T_ALARM)); |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1039 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1040 | static SENSOR_DEVICE_ATTR_RW(fan1_max, fan_max, 0); |
| 1041 | static SENSOR_DEVICE_ATTR_RW(fan2_max, fan_max, 1); |
| 1042 | static SENSOR_DEVICE_ATTR_RW(fan3_max, fan_max, 2); |
| 1043 | static SENSOR_DEVICE_ATTR_RW(fan4_max, fan_max, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1044 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1045 | static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0); |
| 1046 | static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1); |
| 1047 | static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2); |
| 1048 | static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1049 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1050 | static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0); |
| 1051 | static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1); |
| 1052 | static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2); |
| 1053 | static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1054 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1055 | static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, ALARM2(ADT7470_FAN1_ALARM)); |
| 1056 | static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, ALARM2(ADT7470_FAN2_ALARM)); |
| 1057 | static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, ALARM2(ADT7470_FAN3_ALARM)); |
| 1058 | static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm, ALARM2(ADT7470_FAN4_ALARM)); |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1059 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1060 | static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1061 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1062 | static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); |
| 1063 | static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1); |
| 1064 | static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2); |
| 1065 | static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1066 | |
Julia Lawall | 808fc6c | 2016-12-22 13:04:34 +0100 | [diff] [blame] | 1067 | static DEVICE_ATTR_RW(pwm1_freq); |
Joshua Scott | aa18cc9 | 2016-08-08 13:35:45 +1200 | [diff] [blame] | 1068 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1069 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0); |
| 1070 | static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1); |
| 1071 | static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2); |
| 1072 | static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1073 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1074 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0); |
| 1075 | static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1); |
| 1076 | static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2); |
| 1077 | static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1078 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1079 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_tmin, 0); |
| 1080 | static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_temp, pwm_tmin, 1); |
| 1081 | static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_temp, pwm_tmin, 2); |
| 1082 | static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_temp, pwm_tmin, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1083 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1084 | static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_temp, pwm_tmax, 0); |
| 1085 | static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point2_temp, pwm_tmax, 1); |
| 1086 | static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point2_temp, pwm_tmax, 2); |
| 1087 | static SENSOR_DEVICE_ATTR_RO(pwm4_auto_point2_temp, pwm_tmax, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1088 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1089 | static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_auto, 0); |
| 1090 | static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_auto, 1); |
| 1091 | static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_auto, 2); |
| 1092 | static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_auto, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1093 | |
Guenter Roeck | 42291a5 | 2018-12-10 14:02:02 -0800 | [diff] [blame^] | 1094 | static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0); |
| 1095 | static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1); |
| 1096 | static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2); |
| 1097 | static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1098 | |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1099 | static struct attribute *adt7470_attrs[] = { |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1100 | &dev_attr_alarm_mask.attr, |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 1101 | &dev_attr_num_temp_sensors.attr, |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 1102 | &dev_attr_auto_update_interval.attr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1103 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 1104 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 1105 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 1106 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 1107 | &sensor_dev_attr_temp5_max.dev_attr.attr, |
| 1108 | &sensor_dev_attr_temp6_max.dev_attr.attr, |
| 1109 | &sensor_dev_attr_temp7_max.dev_attr.attr, |
| 1110 | &sensor_dev_attr_temp8_max.dev_attr.attr, |
| 1111 | &sensor_dev_attr_temp9_max.dev_attr.attr, |
| 1112 | &sensor_dev_attr_temp10_max.dev_attr.attr, |
| 1113 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 1114 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 1115 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 1116 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
| 1117 | &sensor_dev_attr_temp5_min.dev_attr.attr, |
| 1118 | &sensor_dev_attr_temp6_min.dev_attr.attr, |
| 1119 | &sensor_dev_attr_temp7_min.dev_attr.attr, |
| 1120 | &sensor_dev_attr_temp8_min.dev_attr.attr, |
| 1121 | &sensor_dev_attr_temp9_min.dev_attr.attr, |
| 1122 | &sensor_dev_attr_temp10_min.dev_attr.attr, |
| 1123 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 1124 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 1125 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 1126 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 1127 | &sensor_dev_attr_temp5_input.dev_attr.attr, |
| 1128 | &sensor_dev_attr_temp6_input.dev_attr.attr, |
| 1129 | &sensor_dev_attr_temp7_input.dev_attr.attr, |
| 1130 | &sensor_dev_attr_temp8_input.dev_attr.attr, |
| 1131 | &sensor_dev_attr_temp9_input.dev_attr.attr, |
| 1132 | &sensor_dev_attr_temp10_input.dev_attr.attr, |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1133 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 1134 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 1135 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 1136 | &sensor_dev_attr_temp4_alarm.dev_attr.attr, |
| 1137 | &sensor_dev_attr_temp5_alarm.dev_attr.attr, |
| 1138 | &sensor_dev_attr_temp6_alarm.dev_attr.attr, |
| 1139 | &sensor_dev_attr_temp7_alarm.dev_attr.attr, |
| 1140 | &sensor_dev_attr_temp8_alarm.dev_attr.attr, |
| 1141 | &sensor_dev_attr_temp9_alarm.dev_attr.attr, |
| 1142 | &sensor_dev_attr_temp10_alarm.dev_attr.attr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1143 | &sensor_dev_attr_fan1_max.dev_attr.attr, |
| 1144 | &sensor_dev_attr_fan2_max.dev_attr.attr, |
| 1145 | &sensor_dev_attr_fan3_max.dev_attr.attr, |
| 1146 | &sensor_dev_attr_fan4_max.dev_attr.attr, |
| 1147 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 1148 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 1149 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 1150 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
| 1151 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 1152 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 1153 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 1154 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
Darrick J. Wong | fe03f28 | 2007-12-19 14:11:25 -0800 | [diff] [blame] | 1155 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 1156 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 1157 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
| 1158 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1159 | &sensor_dev_attr_force_pwm_max.dev_attr.attr, |
| 1160 | &sensor_dev_attr_pwm1.dev_attr.attr, |
Joshua Scott | aa18cc9 | 2016-08-08 13:35:45 +1200 | [diff] [blame] | 1161 | &dev_attr_pwm1_freq.attr, |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1162 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 1163 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1164 | &sensor_dev_attr_pwm4.dev_attr.attr, |
| 1165 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
| 1166 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, |
| 1167 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, |
| 1168 | &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr, |
| 1169 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, |
| 1170 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, |
| 1171 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, |
| 1172 | &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr, |
| 1173 | &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, |
| 1174 | &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr, |
| 1175 | &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr, |
| 1176 | &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, |
| 1177 | &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, |
| 1178 | &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr, |
| 1179 | &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr, |
| 1180 | &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr, |
| 1181 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 1182 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 1183 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
| 1184 | &sensor_dev_attr_pwm4_enable.dev_attr.attr, |
| 1185 | &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, |
| 1186 | &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, |
| 1187 | &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, |
| 1188 | &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr, |
| 1189 | NULL |
| 1190 | }; |
| 1191 | |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1192 | ATTRIBUTE_GROUPS(adt7470); |
| 1193 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1194 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 1195 | static int adt7470_detect(struct i2c_client *client, |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1196 | struct i2c_board_info *info) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1197 | { |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1198 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1199 | int vendor, device, revision; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1200 | |
| 1201 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1202 | return -ENODEV; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1203 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1204 | vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR); |
| 1205 | if (vendor != ADT7470_VENDOR) |
| 1206 | return -ENODEV; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1207 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1208 | device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE); |
| 1209 | if (device != ADT7470_DEVICE) |
| 1210 | return -ENODEV; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1211 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1212 | revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION); |
| 1213 | if (revision != ADT7470_REVISION) |
| 1214 | return -ENODEV; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1215 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1216 | strlcpy(info->type, "adt7470", I2C_NAME_SIZE); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1217 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1218 | return 0; |
| 1219 | } |
| 1220 | |
Axel Lin | 9027d93 | 2014-07-16 23:12:05 +0800 | [diff] [blame] | 1221 | static void adt7470_init_client(struct i2c_client *client) |
| 1222 | { |
| 1223 | int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG); |
| 1224 | |
| 1225 | if (reg < 0) { |
| 1226 | dev_err(&client->dev, "cannot read configuration register\n"); |
| 1227 | } else { |
| 1228 | /* start monitoring (and do a self-test) */ |
| 1229 | i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3); |
| 1230 | } |
| 1231 | } |
| 1232 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1233 | static int adt7470_probe(struct i2c_client *client, |
| 1234 | const struct i2c_device_id *id) |
| 1235 | { |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1236 | struct device *dev = &client->dev; |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1237 | struct adt7470_data *data; |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1238 | struct device *hwmon_dev; |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1239 | |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1240 | data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL); |
Guenter Roeck | 9cc7dcc | 2012-06-02 09:58:01 -0700 | [diff] [blame] | 1241 | if (!data) |
| 1242 | return -ENOMEM; |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1243 | |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 1244 | data->num_temp_sensors = -1; |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 1245 | data->auto_update_interval = AUTO_UPDATE_INTERVAL; |
Darrick J. Wong | 2f22d5d | 2009-01-06 14:41:33 -0800 | [diff] [blame] | 1246 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1247 | i2c_set_clientdata(client, data); |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1248 | data->client = client; |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1249 | mutex_init(&data->lock); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1250 | |
| 1251 | dev_info(&client->dev, "%s chip found\n", client->name); |
| 1252 | |
| 1253 | /* Initialize the ADT7470 chip */ |
| 1254 | adt7470_init_client(client); |
| 1255 | |
| 1256 | /* Register sysfs hooks */ |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1257 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 1258 | data, |
| 1259 | adt7470_groups); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1260 | |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1261 | if (IS_ERR(hwmon_dev)) |
| 1262 | return PTR_ERR(hwmon_dev); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1263 | |
Kees Cook | f170168 | 2013-07-03 15:04:58 -0700 | [diff] [blame] | 1264 | data->auto_update = kthread_run(adt7470_update_thread, client, "%s", |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1265 | dev_name(hwmon_dev)); |
Axel Lin | f7334b4 | 2010-11-08 00:11:33 -0500 | [diff] [blame] | 1266 | if (IS_ERR(data->auto_update)) { |
Axel Lin | 3048577 | 2014-07-16 23:12:54 +0800 | [diff] [blame] | 1267 | return PTR_ERR(data->auto_update); |
Axel Lin | f7334b4 | 2010-11-08 00:11:33 -0500 | [diff] [blame] | 1268 | } |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 1269 | |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1270 | return 0; |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Jean Delvare | 008f1ca | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1273 | static int adt7470_remove(struct i2c_client *client) |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1274 | { |
| 1275 | struct adt7470_data *data = i2c_get_clientdata(client); |
| 1276 | |
Darrick J. Wong | 89fac11 | 2009-01-06 14:41:34 -0800 | [diff] [blame] | 1277 | kthread_stop(data->auto_update); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1278 | return 0; |
| 1279 | } |
| 1280 | |
Axel Lin | 9027d93 | 2014-07-16 23:12:05 +0800 | [diff] [blame] | 1281 | static const struct i2c_device_id adt7470_id[] = { |
| 1282 | { "adt7470", 0 }, |
| 1283 | { } |
| 1284 | }; |
| 1285 | MODULE_DEVICE_TABLE(i2c, adt7470_id); |
| 1286 | |
| 1287 | static struct i2c_driver adt7470_driver = { |
| 1288 | .class = I2C_CLASS_HWMON, |
| 1289 | .driver = { |
| 1290 | .name = "adt7470", |
| 1291 | }, |
| 1292 | .probe = adt7470_probe, |
| 1293 | .remove = adt7470_remove, |
| 1294 | .id_table = adt7470_id, |
| 1295 | .detect = adt7470_detect, |
| 1296 | .address_list = normal_i2c, |
| 1297 | }; |
| 1298 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 1299 | module_i2c_driver(adt7470_driver); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1300 | |
Darrick J. Wong | 5407e051 | 2013-08-26 15:42:27 -0700 | [diff] [blame] | 1301 | MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>"); |
Darrick J. Wong | 6f9703d | 2007-07-31 11:06:52 -0700 | [diff] [blame] | 1302 | MODULE_DESCRIPTION("ADT7470 driver"); |
| 1303 | MODULE_LICENSE("GPL"); |