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