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