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