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