Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 2 | /* |
| 3 | * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives |
| 4 | * Copyright (C) 2007-2008, Advanced Micro Devices, Inc. |
| 5 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> |
| 6 | * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com> |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 7 | * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de> |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 8 | * |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 9 | * Derived from the lm83 driver by Jean Delvare |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
Javier Martinez Canillas | 4e2496e | 2017-02-24 10:12:58 -0300 | [diff] [blame] | 13 | #include <linux/of_device.h> |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/hwmon.h> |
| 18 | #include <linux/hwmon-sysfs.h> |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 19 | #include <linux/hwmon-vid.h> |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 20 | #include <linux/err.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 21 | #include <linux/jiffies.h> |
Chris Packham | e465164 | 2017-05-11 15:45:21 +1200 | [diff] [blame] | 22 | #include <linux/util_macros.h> |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 23 | |
| 24 | /* Indexes for the sysfs hooks */ |
| 25 | |
| 26 | #define INPUT 0 |
| 27 | #define MIN 1 |
| 28 | #define MAX 2 |
| 29 | #define CONTROL 3 |
| 30 | #define OFFSET 3 |
| 31 | #define AUTOMIN 4 |
| 32 | #define THERM 5 |
| 33 | #define HYSTERSIS 6 |
| 34 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 35 | /* |
| 36 | * These are unique identifiers for the sysfs functions - unlike the |
| 37 | * numbers above, these are not also indexes into an array |
| 38 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 39 | |
| 40 | #define ALARM 9 |
| 41 | #define FAULT 10 |
| 42 | |
| 43 | /* 7475 Common Registers */ |
| 44 | |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 45 | #define REG_DEVREV2 0x12 /* ADT7490 only */ |
| 46 | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 47 | #define REG_VTT 0x1E /* ADT7490 only */ |
| 48 | #define REG_EXTEND3 0x1F /* ADT7490 only */ |
| 49 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 50 | #define REG_VOLTAGE_BASE 0x20 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 51 | #define REG_TEMP_BASE 0x25 |
| 52 | #define REG_TACH_BASE 0x28 |
| 53 | #define REG_PWM_BASE 0x30 |
| 54 | #define REG_PWM_MAX_BASE 0x38 |
| 55 | |
| 56 | #define REG_DEVID 0x3D |
| 57 | #define REG_VENDID 0x3E |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 58 | #define REG_DEVID2 0x3F |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 59 | |
Chris Packham | 4abdf38 | 2017-04-22 07:08:09 +1200 | [diff] [blame] | 60 | #define REG_CONFIG1 0x40 |
| 61 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 62 | #define REG_STATUS1 0x41 |
| 63 | #define REG_STATUS2 0x42 |
| 64 | |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 65 | #define REG_VID 0x43 /* ADT7476 only */ |
| 66 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 67 | #define REG_VOLTAGE_MIN_BASE 0x44 |
| 68 | #define REG_VOLTAGE_MAX_BASE 0x45 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 69 | |
| 70 | #define REG_TEMP_MIN_BASE 0x4E |
| 71 | #define REG_TEMP_MAX_BASE 0x4F |
| 72 | |
| 73 | #define REG_TACH_MIN_BASE 0x54 |
| 74 | |
| 75 | #define REG_PWM_CONFIG_BASE 0x5C |
| 76 | |
| 77 | #define REG_TEMP_TRANGE_BASE 0x5F |
| 78 | |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 79 | #define REG_ENHANCE_ACOUSTICS1 0x62 |
| 80 | #define REG_ENHANCE_ACOUSTICS2 0x63 |
| 81 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 82 | #define REG_PWM_MIN_BASE 0x64 |
| 83 | |
| 84 | #define REG_TEMP_TMIN_BASE 0x67 |
| 85 | #define REG_TEMP_THERM_BASE 0x6A |
| 86 | |
| 87 | #define REG_REMOTE1_HYSTERSIS 0x6D |
| 88 | #define REG_REMOTE2_HYSTERSIS 0x6E |
| 89 | |
| 90 | #define REG_TEMP_OFFSET_BASE 0x70 |
| 91 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 92 | #define REG_CONFIG2 0x73 |
| 93 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 94 | #define REG_EXTEND1 0x76 |
| 95 | #define REG_EXTEND2 0x77 |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 96 | |
| 97 | #define REG_CONFIG3 0x78 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 98 | #define REG_CONFIG5 0x7C |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 99 | #define REG_CONFIG4 0x7D |
| 100 | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 101 | #define REG_STATUS4 0x81 /* ADT7490 only */ |
| 102 | |
| 103 | #define REG_VTT_MIN 0x84 /* ADT7490 only */ |
| 104 | #define REG_VTT_MAX 0x86 /* ADT7490 only */ |
| 105 | |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 106 | #define VID_VIDSEL 0x80 /* ADT7476 only */ |
| 107 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 108 | #define CONFIG2_ATTN 0x20 |
| 109 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 110 | #define CONFIG3_SMBALERT 0x01 |
| 111 | #define CONFIG3_THERM 0x02 |
| 112 | |
| 113 | #define CONFIG4_PINFUNC 0x03 |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 114 | #define CONFIG4_MAXDUTY 0x08 |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 115 | #define CONFIG4_ATTN_IN10 0x30 |
| 116 | #define CONFIG4_ATTN_IN43 0xC0 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 117 | |
| 118 | #define CONFIG5_TWOSCOMP 0x01 |
| 119 | #define CONFIG5_TEMPOFFSET 0x02 |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 120 | #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 121 | |
| 122 | /* ADT7475 Settings */ |
| 123 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 124 | #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 125 | #define ADT7475_TEMP_COUNT 3 |
| 126 | #define ADT7475_TACH_COUNT 4 |
| 127 | #define ADT7475_PWM_COUNT 3 |
| 128 | |
| 129 | /* Macro to read the registers */ |
| 130 | |
| 131 | #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg)) |
| 132 | |
| 133 | /* Macros to easily index the registers */ |
| 134 | |
| 135 | #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2)) |
| 136 | #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2)) |
| 137 | |
| 138 | #define PWM_REG(idx) (REG_PWM_BASE + (idx)) |
| 139 | #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx)) |
| 140 | #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx)) |
| 141 | #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx)) |
| 142 | |
| 143 | #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx)) |
| 144 | #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2)) |
| 145 | #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2)) |
| 146 | |
| 147 | #define TEMP_REG(idx) (REG_TEMP_BASE + (idx)) |
| 148 | #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2)) |
| 149 | #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2)) |
| 150 | #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx)) |
| 151 | #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx)) |
| 152 | #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx)) |
| 153 | #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx)) |
| 154 | |
Jean Delvare | 918ee91 | 2010-10-28 20:31:50 +0200 | [diff] [blame] | 155 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 156 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 157 | enum chips { adt7473, adt7475, adt7476, adt7490 }; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 158 | |
| 159 | static const struct i2c_device_id adt7475_id[] = { |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 160 | { "adt7473", adt7473 }, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 161 | { "adt7475", adt7475 }, |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 162 | { "adt7476", adt7476 }, |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 163 | { "adt7490", adt7490 }, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 164 | { } |
| 165 | }; |
| 166 | MODULE_DEVICE_TABLE(i2c, adt7475_id); |
| 167 | |
Guenter Roeck | fe339db | 2019-04-04 06:33:42 -0700 | [diff] [blame] | 168 | static const struct of_device_id __maybe_unused adt7475_of_match[] = { |
Javier Martinez Canillas | 4e2496e | 2017-02-24 10:12:58 -0300 | [diff] [blame] | 169 | { |
| 170 | .compatible = "adi,adt7473", |
| 171 | .data = (void *)adt7473 |
| 172 | }, |
| 173 | { |
| 174 | .compatible = "adi,adt7475", |
| 175 | .data = (void *)adt7475 |
| 176 | }, |
| 177 | { |
| 178 | .compatible = "adi,adt7476", |
| 179 | .data = (void *)adt7476 |
| 180 | }, |
| 181 | { |
| 182 | .compatible = "adi,adt7490", |
| 183 | .data = (void *)adt7490 |
| 184 | }, |
| 185 | { }, |
| 186 | }; |
| 187 | MODULE_DEVICE_TABLE(of, adt7475_of_match); |
| 188 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 189 | struct adt7475_data { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 190 | struct i2c_client *client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 191 | struct mutex lock; |
| 192 | |
| 193 | unsigned long measure_updated; |
Tokunori Ikegami | b36fb17 | 2018-08-08 10:32:17 +0900 | [diff] [blame] | 194 | bool valid; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 195 | |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 196 | u8 config4; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 197 | u8 config5; |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 198 | u8 has_voltage; |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 199 | u8 bypass_attn; /* Bypass voltage attenuator */ |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 200 | u8 has_pwm2:1; |
| 201 | u8 has_fan4:1; |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 202 | u8 has_vid:1; |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 203 | u32 alarms; |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 204 | u16 voltage[3][6]; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 205 | u16 temp[7][3]; |
| 206 | u16 tach[2][4]; |
| 207 | u8 pwm[4][3]; |
| 208 | u8 range[3]; |
| 209 | u8 pwmctl[3]; |
| 210 | u8 pwmchan[3]; |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 211 | u8 enh_acoustics[2]; |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 212 | |
| 213 | u8 vid; |
| 214 | u8 vrm; |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 215 | const struct attribute_group *groups[9]; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | static struct i2c_driver adt7475_driver; |
| 219 | static struct adt7475_data *adt7475_update_device(struct device *dev); |
| 220 | static void adt7475_read_hystersis(struct i2c_client *client); |
| 221 | static void adt7475_read_pwm(struct i2c_client *client, int index); |
| 222 | |
| 223 | /* Given a temp value, convert it to register value */ |
| 224 | |
| 225 | static inline u16 temp2reg(struct adt7475_data *data, long val) |
| 226 | { |
| 227 | u16 ret; |
| 228 | |
| 229 | if (!(data->config5 & CONFIG5_TWOSCOMP)) { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 230 | val = clamp_val(val, -64000, 191000); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 231 | ret = (val + 64500) / 1000; |
| 232 | } else { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 233 | val = clamp_val(val, -128000, 127000); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 234 | if (val < -500) |
| 235 | ret = (256500 + val) / 1000; |
| 236 | else |
| 237 | ret = (val + 500) / 1000; |
| 238 | } |
| 239 | |
| 240 | return ret << 2; |
| 241 | } |
| 242 | |
| 243 | /* Given a register value, convert it to a real temp value */ |
| 244 | |
| 245 | static inline int reg2temp(struct adt7475_data *data, u16 reg) |
| 246 | { |
| 247 | if (data->config5 & CONFIG5_TWOSCOMP) { |
| 248 | if (reg >= 512) |
| 249 | return (reg - 1024) * 250; |
| 250 | else |
| 251 | return reg * 250; |
| 252 | } else |
| 253 | return (reg - 256) * 250; |
| 254 | } |
| 255 | |
| 256 | static inline int tach2rpm(u16 tach) |
| 257 | { |
| 258 | if (tach == 0 || tach == 0xFFFF) |
| 259 | return 0; |
| 260 | |
| 261 | return (90000 * 60) / tach; |
| 262 | } |
| 263 | |
| 264 | static inline u16 rpm2tach(unsigned long rpm) |
| 265 | { |
| 266 | if (rpm == 0) |
| 267 | return 0; |
| 268 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 269 | return clamp_val((90000 * 60) / rpm, 1, 0xFFFF); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 272 | /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */ |
| 273 | static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = { |
| 274 | { 45, 94 }, /* +2.5V */ |
| 275 | { 175, 525 }, /* Vccp */ |
| 276 | { 68, 71 }, /* Vcc */ |
| 277 | { 93, 47 }, /* +5V */ |
| 278 | { 120, 20 }, /* +12V */ |
| 279 | { 45, 45 }, /* Vtt */ |
| 280 | }; |
| 281 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 282 | static inline int reg2volt(int channel, u16 reg, u8 bypass_attn) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 283 | { |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 284 | const int *r = adt7473_in_scaling[channel]; |
| 285 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 286 | if (bypass_attn & (1 << channel)) |
| 287 | return DIV_ROUND_CLOSEST(reg * 2250, 1024); |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 288 | return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 291 | static inline u16 volt2reg(int channel, long volt, u8 bypass_attn) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 292 | { |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 293 | const int *r = adt7473_in_scaling[channel]; |
| 294 | long reg; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 295 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 296 | if (bypass_attn & (1 << channel)) |
| 297 | reg = (volt * 1024) / 2250; |
| 298 | else |
| 299 | reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 300 | return clamp_val(reg, 0, 1023) & (0xff << 2); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Dan Carpenter | f196dec | 2018-08-14 13:07:47 +0300 | [diff] [blame] | 303 | static int adt7475_read_word(struct i2c_client *client, int reg) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 304 | { |
Dan Carpenter | f196dec | 2018-08-14 13:07:47 +0300 | [diff] [blame] | 305 | int val1, val2; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 306 | |
Dan Carpenter | f196dec | 2018-08-14 13:07:47 +0300 | [diff] [blame] | 307 | val1 = i2c_smbus_read_byte_data(client, reg); |
| 308 | if (val1 < 0) |
| 309 | return val1; |
| 310 | val2 = i2c_smbus_read_byte_data(client, reg + 1); |
| 311 | if (val2 < 0) |
| 312 | return val2; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 313 | |
Dan Carpenter | f196dec | 2018-08-14 13:07:47 +0300 | [diff] [blame] | 314 | return val1 | (val2 << 8); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void adt7475_write_word(struct i2c_client *client, int reg, u16 val) |
| 318 | { |
| 319 | i2c_smbus_write_byte_data(client, reg + 1, val >> 8); |
| 320 | i2c_smbus_write_byte_data(client, reg, val & 0xFF); |
| 321 | } |
| 322 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 323 | static ssize_t voltage_show(struct device *dev, struct device_attribute *attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 324 | char *buf) |
| 325 | { |
| 326 | struct adt7475_data *data = adt7475_update_device(dev); |
| 327 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 328 | unsigned short val; |
| 329 | |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 330 | if (IS_ERR(data)) |
| 331 | return PTR_ERR(data); |
| 332 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 333 | switch (sattr->nr) { |
| 334 | case ALARM: |
| 335 | return sprintf(buf, "%d\n", |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 336 | (data->alarms >> sattr->index) & 1); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 337 | default: |
| 338 | val = data->voltage[sattr->nr][sattr->index]; |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 339 | return sprintf(buf, "%d\n", |
| 340 | reg2volt(sattr->index, val, data->bypass_attn)); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 344 | static ssize_t voltage_store(struct device *dev, |
| 345 | struct device_attribute *attr, const char *buf, |
| 346 | size_t count) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 347 | { |
| 348 | |
| 349 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 350 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 351 | struct i2c_client *client = data->client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 352 | unsigned char reg; |
| 353 | long val; |
| 354 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 355 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 356 | return -EINVAL; |
| 357 | |
| 358 | mutex_lock(&data->lock); |
| 359 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 360 | data->voltage[sattr->nr][sattr->index] = |
| 361 | volt2reg(sattr->index, val, data->bypass_attn); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 362 | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 363 | if (sattr->index < ADT7475_VOLTAGE_COUNT) { |
| 364 | if (sattr->nr == MIN) |
| 365 | reg = VOLTAGE_MIN_REG(sattr->index); |
| 366 | else |
| 367 | reg = VOLTAGE_MAX_REG(sattr->index); |
| 368 | } else { |
| 369 | if (sattr->nr == MIN) |
| 370 | reg = REG_VTT_MIN; |
| 371 | else |
| 372 | reg = REG_VTT_MAX; |
| 373 | } |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 374 | |
| 375 | i2c_smbus_write_byte_data(client, reg, |
| 376 | data->voltage[sattr->nr][sattr->index] >> 2); |
| 377 | mutex_unlock(&data->lock); |
| 378 | |
| 379 | return count; |
| 380 | } |
| 381 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 382 | static ssize_t temp_show(struct device *dev, struct device_attribute *attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 383 | char *buf) |
| 384 | { |
| 385 | struct adt7475_data *data = adt7475_update_device(dev); |
| 386 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 387 | int out; |
| 388 | |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 389 | if (IS_ERR(data)) |
| 390 | return PTR_ERR(data); |
| 391 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 392 | switch (sattr->nr) { |
| 393 | case HYSTERSIS: |
| 394 | mutex_lock(&data->lock); |
| 395 | out = data->temp[sattr->nr][sattr->index]; |
| 396 | if (sattr->index != 1) |
| 397 | out = (out >> 4) & 0xF; |
| 398 | else |
| 399 | out = (out & 0xF); |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 400 | /* |
| 401 | * Show the value as an absolute number tied to |
| 402 | * THERM |
| 403 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 404 | out = reg2temp(data, data->temp[THERM][sattr->index]) - |
| 405 | out * 1000; |
| 406 | mutex_unlock(&data->lock); |
| 407 | break; |
| 408 | |
| 409 | case OFFSET: |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 410 | /* |
| 411 | * Offset is always 2's complement, regardless of the |
| 412 | * setting in CONFIG5 |
| 413 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 414 | mutex_lock(&data->lock); |
| 415 | out = (s8)data->temp[sattr->nr][sattr->index]; |
| 416 | if (data->config5 & CONFIG5_TEMPOFFSET) |
| 417 | out *= 1000; |
| 418 | else |
| 419 | out *= 500; |
| 420 | mutex_unlock(&data->lock); |
| 421 | break; |
| 422 | |
| 423 | case ALARM: |
| 424 | out = (data->alarms >> (sattr->index + 4)) & 1; |
| 425 | break; |
| 426 | |
| 427 | case FAULT: |
| 428 | /* Note - only for remote1 and remote2 */ |
Jean Delvare | cf312e0 | 2009-11-16 12:45:39 +0100 | [diff] [blame] | 429 | out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000)); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 430 | break; |
| 431 | |
| 432 | default: |
| 433 | /* All other temp values are in the configured format */ |
| 434 | out = reg2temp(data, data->temp[sattr->nr][sattr->index]); |
| 435 | } |
| 436 | |
| 437 | return sprintf(buf, "%d\n", out); |
| 438 | } |
| 439 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 440 | static ssize_t temp_store(struct device *dev, struct device_attribute *attr, |
| 441 | const char *buf, size_t count) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 442 | { |
| 443 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 444 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 445 | struct i2c_client *client = data->client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 446 | unsigned char reg = 0; |
| 447 | u8 out; |
| 448 | int temp; |
| 449 | long val; |
| 450 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 451 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 452 | return -EINVAL; |
| 453 | |
| 454 | mutex_lock(&data->lock); |
| 455 | |
| 456 | /* We need the config register in all cases for temp <-> reg conv. */ |
| 457 | data->config5 = adt7475_read(REG_CONFIG5); |
| 458 | |
| 459 | switch (sattr->nr) { |
| 460 | case OFFSET: |
| 461 | if (data->config5 & CONFIG5_TEMPOFFSET) { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 462 | val = clamp_val(val, -63000, 127000); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 463 | out = data->temp[OFFSET][sattr->index] = val / 1000; |
| 464 | } else { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 465 | val = clamp_val(val, -63000, 64000); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 466 | out = data->temp[OFFSET][sattr->index] = val / 500; |
| 467 | } |
| 468 | break; |
| 469 | |
| 470 | case HYSTERSIS: |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 471 | /* |
| 472 | * The value will be given as an absolute value, turn it |
| 473 | * into an offset based on THERM |
| 474 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 475 | |
| 476 | /* Read fresh THERM and HYSTERSIS values from the chip */ |
| 477 | data->temp[THERM][sattr->index] = |
| 478 | adt7475_read(TEMP_THERM_REG(sattr->index)) << 2; |
| 479 | adt7475_read_hystersis(client); |
| 480 | |
| 481 | temp = reg2temp(data, data->temp[THERM][sattr->index]); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 482 | val = clamp_val(val, temp - 15000, temp); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 483 | val = (temp - val) / 1000; |
| 484 | |
| 485 | if (sattr->index != 1) { |
| 486 | data->temp[HYSTERSIS][sattr->index] &= 0xF0; |
| 487 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4; |
| 488 | } else { |
| 489 | data->temp[HYSTERSIS][sattr->index] &= 0x0F; |
| 490 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF); |
| 491 | } |
| 492 | |
| 493 | out = data->temp[HYSTERSIS][sattr->index]; |
| 494 | break; |
| 495 | |
| 496 | default: |
| 497 | data->temp[sattr->nr][sattr->index] = temp2reg(data, val); |
| 498 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 499 | /* |
| 500 | * We maintain an extra 2 digits of precision for simplicity |
| 501 | * - shift those back off before writing the value |
| 502 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 503 | out = (u8) (data->temp[sattr->nr][sattr->index] >> 2); |
| 504 | } |
| 505 | |
| 506 | switch (sattr->nr) { |
| 507 | case MIN: |
| 508 | reg = TEMP_MIN_REG(sattr->index); |
| 509 | break; |
| 510 | case MAX: |
| 511 | reg = TEMP_MAX_REG(sattr->index); |
| 512 | break; |
| 513 | case OFFSET: |
| 514 | reg = TEMP_OFFSET_REG(sattr->index); |
| 515 | break; |
| 516 | case AUTOMIN: |
| 517 | reg = TEMP_TMIN_REG(sattr->index); |
| 518 | break; |
| 519 | case THERM: |
| 520 | reg = TEMP_THERM_REG(sattr->index); |
| 521 | break; |
| 522 | case HYSTERSIS: |
| 523 | if (sattr->index != 2) |
| 524 | reg = REG_REMOTE1_HYSTERSIS; |
| 525 | else |
| 526 | reg = REG_REMOTE2_HYSTERSIS; |
| 527 | |
| 528 | break; |
| 529 | } |
| 530 | |
| 531 | i2c_smbus_write_byte_data(client, reg, out); |
| 532 | |
| 533 | mutex_unlock(&data->lock); |
| 534 | return count; |
| 535 | } |
| 536 | |
Chris Packham | 8f05bcc | 2017-05-15 13:30:28 +1200 | [diff] [blame] | 537 | /* Assuming CONFIG6[SLOW] is 0 */ |
| 538 | static const int ad7475_st_map[] = { |
| 539 | 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800, |
| 540 | }; |
| 541 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 542 | static ssize_t temp_st_show(struct device *dev, struct device_attribute *attr, |
| 543 | char *buf) |
Chris Packham | 8f05bcc | 2017-05-15 13:30:28 +1200 | [diff] [blame] | 544 | { |
| 545 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 546 | struct adt7475_data *data = dev_get_drvdata(dev); |
Chris Packham | 8f05bcc | 2017-05-15 13:30:28 +1200 | [diff] [blame] | 547 | long val; |
| 548 | |
| 549 | switch (sattr->index) { |
| 550 | case 0: |
| 551 | val = data->enh_acoustics[0] & 0xf; |
| 552 | break; |
| 553 | case 1: |
| 554 | val = (data->enh_acoustics[1] >> 4) & 0xf; |
| 555 | break; |
| 556 | case 2: |
| 557 | default: |
| 558 | val = data->enh_acoustics[1] & 0xf; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | if (val & 0x8) |
| 563 | return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]); |
| 564 | else |
| 565 | return sprintf(buf, "0\n"); |
| 566 | } |
| 567 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 568 | static ssize_t temp_st_store(struct device *dev, |
| 569 | struct device_attribute *attr, const char *buf, |
| 570 | size_t count) |
Chris Packham | 8f05bcc | 2017-05-15 13:30:28 +1200 | [diff] [blame] | 571 | { |
| 572 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 573 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 574 | struct i2c_client *client = data->client; |
Chris Packham | 8f05bcc | 2017-05-15 13:30:28 +1200 | [diff] [blame] | 575 | unsigned char reg; |
| 576 | int shift, idx; |
| 577 | ulong val; |
| 578 | |
| 579 | if (kstrtoul(buf, 10, &val)) |
| 580 | return -EINVAL; |
| 581 | |
| 582 | switch (sattr->index) { |
| 583 | case 0: |
| 584 | reg = REG_ENHANCE_ACOUSTICS1; |
| 585 | shift = 0; |
| 586 | idx = 0; |
| 587 | break; |
| 588 | case 1: |
| 589 | reg = REG_ENHANCE_ACOUSTICS2; |
| 590 | shift = 0; |
| 591 | idx = 1; |
| 592 | break; |
| 593 | case 2: |
| 594 | default: |
| 595 | reg = REG_ENHANCE_ACOUSTICS2; |
| 596 | shift = 4; |
| 597 | idx = 1; |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | if (val > 0) { |
| 602 | val = find_closest_descending(val, ad7475_st_map, |
| 603 | ARRAY_SIZE(ad7475_st_map)); |
| 604 | val |= 0x8; |
| 605 | } |
| 606 | |
| 607 | mutex_lock(&data->lock); |
| 608 | |
| 609 | data->enh_acoustics[idx] &= ~(0xf << shift); |
| 610 | data->enh_acoustics[idx] |= (val << shift); |
| 611 | |
| 612 | i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]); |
| 613 | |
| 614 | mutex_unlock(&data->lock); |
| 615 | |
| 616 | return count; |
| 617 | } |
| 618 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 619 | /* |
| 620 | * Table of autorange values - the user will write the value in millidegrees, |
| 621 | * and we'll convert it |
| 622 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 623 | static const int autorange_table[] = { |
| 624 | 2000, 2500, 3330, 4000, 5000, 6670, 8000, |
| 625 | 10000, 13330, 16000, 20000, 26670, 32000, 40000, |
| 626 | 53330, 80000 |
| 627 | }; |
| 628 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 629 | static ssize_t point2_show(struct device *dev, struct device_attribute *attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 630 | char *buf) |
| 631 | { |
| 632 | struct adt7475_data *data = adt7475_update_device(dev); |
| 633 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 634 | int out, val; |
| 635 | |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 636 | if (IS_ERR(data)) |
| 637 | return PTR_ERR(data); |
| 638 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 639 | mutex_lock(&data->lock); |
| 640 | out = (data->range[sattr->index] >> 4) & 0x0F; |
| 641 | val = reg2temp(data, data->temp[AUTOMIN][sattr->index]); |
| 642 | mutex_unlock(&data->lock); |
| 643 | |
| 644 | return sprintf(buf, "%d\n", val + autorange_table[out]); |
| 645 | } |
| 646 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 647 | static ssize_t point2_store(struct device *dev, struct device_attribute *attr, |
| 648 | const char *buf, size_t count) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 649 | { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 650 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 651 | struct i2c_client *client = data->client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 652 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 653 | int temp; |
| 654 | long val; |
| 655 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 656 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 657 | return -EINVAL; |
| 658 | |
| 659 | mutex_lock(&data->lock); |
| 660 | |
| 661 | /* Get a fresh copy of the needed registers */ |
| 662 | data->config5 = adt7475_read(REG_CONFIG5); |
| 663 | data->temp[AUTOMIN][sattr->index] = |
| 664 | adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2; |
| 665 | data->range[sattr->index] = |
| 666 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); |
| 667 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 668 | /* |
| 669 | * The user will write an absolute value, so subtract the start point |
| 670 | * to figure the range |
| 671 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 672 | temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 673 | val = clamp_val(val, temp + autorange_table[0], |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 674 | temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]); |
| 675 | val -= temp; |
| 676 | |
| 677 | /* Find the nearest table entry to what the user wrote */ |
Chris Packham | e465164 | 2017-05-11 15:45:21 +1200 | [diff] [blame] | 678 | val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table)); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 679 | |
| 680 | data->range[sattr->index] &= ~0xF0; |
| 681 | data->range[sattr->index] |= val << 4; |
| 682 | |
| 683 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), |
| 684 | data->range[sattr->index]); |
| 685 | |
| 686 | mutex_unlock(&data->lock); |
| 687 | return count; |
| 688 | } |
| 689 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 690 | static ssize_t tach_show(struct device *dev, struct device_attribute *attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 691 | char *buf) |
| 692 | { |
| 693 | struct adt7475_data *data = adt7475_update_device(dev); |
| 694 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 695 | int out; |
| 696 | |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 697 | if (IS_ERR(data)) |
| 698 | return PTR_ERR(data); |
| 699 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 700 | if (sattr->nr == ALARM) |
| 701 | out = (data->alarms >> (sattr->index + 10)) & 1; |
| 702 | else |
| 703 | out = tach2rpm(data->tach[sattr->nr][sattr->index]); |
| 704 | |
| 705 | return sprintf(buf, "%d\n", out); |
| 706 | } |
| 707 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 708 | static ssize_t tach_store(struct device *dev, struct device_attribute *attr, |
| 709 | const char *buf, size_t count) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 710 | { |
| 711 | |
| 712 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 713 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 714 | struct i2c_client *client = data->client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 715 | unsigned long val; |
| 716 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 717 | if (kstrtoul(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 718 | return -EINVAL; |
| 719 | |
| 720 | mutex_lock(&data->lock); |
| 721 | |
| 722 | data->tach[MIN][sattr->index] = rpm2tach(val); |
| 723 | |
| 724 | adt7475_write_word(client, TACH_MIN_REG(sattr->index), |
| 725 | data->tach[MIN][sattr->index]); |
| 726 | |
| 727 | mutex_unlock(&data->lock); |
| 728 | return count; |
| 729 | } |
| 730 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 731 | static ssize_t pwm_show(struct device *dev, struct device_attribute *attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 732 | char *buf) |
| 733 | { |
| 734 | struct adt7475_data *data = adt7475_update_device(dev); |
| 735 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 736 | |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 737 | if (IS_ERR(data)) |
| 738 | return PTR_ERR(data); |
| 739 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 740 | return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]); |
| 741 | } |
| 742 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 743 | static ssize_t pwmchan_show(struct device *dev, struct device_attribute *attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 744 | char *buf) |
| 745 | { |
| 746 | struct adt7475_data *data = adt7475_update_device(dev); |
| 747 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 748 | |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 749 | if (IS_ERR(data)) |
| 750 | return PTR_ERR(data); |
| 751 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 752 | return sprintf(buf, "%d\n", data->pwmchan[sattr->index]); |
| 753 | } |
| 754 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 755 | static ssize_t pwmctrl_show(struct device *dev, struct device_attribute *attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 756 | char *buf) |
| 757 | { |
| 758 | struct adt7475_data *data = adt7475_update_device(dev); |
| 759 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 760 | |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 761 | if (IS_ERR(data)) |
| 762 | return PTR_ERR(data); |
| 763 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 764 | return sprintf(buf, "%d\n", data->pwmctl[sattr->index]); |
| 765 | } |
| 766 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 767 | static ssize_t pwm_store(struct device *dev, struct device_attribute *attr, |
| 768 | const char *buf, size_t count) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 769 | { |
| 770 | |
| 771 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 772 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 773 | struct i2c_client *client = data->client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 774 | unsigned char reg = 0; |
| 775 | long val; |
| 776 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 777 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 778 | return -EINVAL; |
| 779 | |
| 780 | mutex_lock(&data->lock); |
| 781 | |
| 782 | switch (sattr->nr) { |
| 783 | case INPUT: |
| 784 | /* Get a fresh value for CONTROL */ |
| 785 | data->pwm[CONTROL][sattr->index] = |
| 786 | adt7475_read(PWM_CONFIG_REG(sattr->index)); |
| 787 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 788 | /* |
| 789 | * If we are not in manual mode, then we shouldn't allow |
| 790 | * the user to set the pwm speed |
| 791 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 792 | if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) { |
| 793 | mutex_unlock(&data->lock); |
| 794 | return count; |
| 795 | } |
| 796 | |
| 797 | reg = PWM_REG(sattr->index); |
| 798 | break; |
| 799 | |
| 800 | case MIN: |
| 801 | reg = PWM_MIN_REG(sattr->index); |
| 802 | break; |
| 803 | |
| 804 | case MAX: |
| 805 | reg = PWM_MAX_REG(sattr->index); |
| 806 | break; |
| 807 | } |
| 808 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 809 | data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 810 | i2c_smbus_write_byte_data(client, reg, |
| 811 | data->pwm[sattr->nr][sattr->index]); |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 812 | mutex_unlock(&data->lock); |
| 813 | |
| 814 | return count; |
| 815 | } |
| 816 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 817 | static ssize_t stall_disable_show(struct device *dev, |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 818 | struct device_attribute *attr, char *buf) |
| 819 | { |
| 820 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 821 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 822 | |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 823 | u8 mask = BIT(5 + sattr->index); |
| 824 | |
| 825 | return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask)); |
| 826 | } |
| 827 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 828 | static ssize_t stall_disable_store(struct device *dev, |
| 829 | struct device_attribute *attr, |
| 830 | const char *buf, size_t count) |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 831 | { |
| 832 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 833 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 834 | struct i2c_client *client = data->client; |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 835 | long val; |
| 836 | u8 mask = BIT(5 + sattr->index); |
| 837 | |
| 838 | if (kstrtol(buf, 10, &val)) |
| 839 | return -EINVAL; |
| 840 | |
| 841 | mutex_lock(&data->lock); |
| 842 | |
| 843 | data->enh_acoustics[0] &= ~mask; |
| 844 | if (val) |
| 845 | data->enh_acoustics[0] |= mask; |
| 846 | |
| 847 | i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1, |
| 848 | data->enh_acoustics[0]); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 849 | |
| 850 | mutex_unlock(&data->lock); |
| 851 | |
| 852 | return count; |
| 853 | } |
| 854 | |
| 855 | /* Called by set_pwmctrl and set_pwmchan */ |
| 856 | |
| 857 | static int hw_set_pwm(struct i2c_client *client, int index, |
| 858 | unsigned int pwmctl, unsigned int pwmchan) |
| 859 | { |
| 860 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 861 | long val = 0; |
| 862 | |
| 863 | switch (pwmctl) { |
| 864 | case 0: |
| 865 | val = 0x03; /* Run at full speed */ |
| 866 | break; |
| 867 | case 1: |
| 868 | val = 0x07; /* Manual mode */ |
| 869 | break; |
| 870 | case 2: |
| 871 | switch (pwmchan) { |
| 872 | case 1: |
| 873 | /* Remote1 controls PWM */ |
| 874 | val = 0x00; |
| 875 | break; |
| 876 | case 2: |
| 877 | /* local controls PWM */ |
| 878 | val = 0x01; |
| 879 | break; |
| 880 | case 4: |
| 881 | /* remote2 controls PWM */ |
| 882 | val = 0x02; |
| 883 | break; |
| 884 | case 6: |
| 885 | /* local/remote2 control PWM */ |
| 886 | val = 0x05; |
| 887 | break; |
| 888 | case 7: |
| 889 | /* All three control PWM */ |
| 890 | val = 0x06; |
| 891 | break; |
| 892 | default: |
| 893 | return -EINVAL; |
| 894 | } |
| 895 | break; |
| 896 | default: |
| 897 | return -EINVAL; |
| 898 | } |
| 899 | |
| 900 | data->pwmctl[index] = pwmctl; |
| 901 | data->pwmchan[index] = pwmchan; |
| 902 | |
| 903 | data->pwm[CONTROL][index] &= ~0xE0; |
| 904 | data->pwm[CONTROL][index] |= (val & 7) << 5; |
| 905 | |
| 906 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 907 | data->pwm[CONTROL][index]); |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 912 | static ssize_t pwmchan_store(struct device *dev, |
| 913 | struct device_attribute *attr, const char *buf, |
| 914 | size_t count) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 915 | { |
| 916 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 917 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 918 | struct i2c_client *client = data->client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 919 | int r; |
| 920 | long val; |
| 921 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 922 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 923 | return -EINVAL; |
| 924 | |
| 925 | mutex_lock(&data->lock); |
| 926 | /* Read Modify Write PWM values */ |
| 927 | adt7475_read_pwm(client, sattr->index); |
| 928 | r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val); |
| 929 | if (r) |
| 930 | count = r; |
| 931 | mutex_unlock(&data->lock); |
| 932 | |
| 933 | return count; |
| 934 | } |
| 935 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 936 | static ssize_t pwmctrl_store(struct device *dev, |
| 937 | struct device_attribute *attr, const char *buf, |
| 938 | size_t count) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 939 | { |
| 940 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 941 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 942 | struct i2c_client *client = data->client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 943 | int r; |
| 944 | long val; |
| 945 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 946 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 947 | return -EINVAL; |
| 948 | |
| 949 | mutex_lock(&data->lock); |
| 950 | /* Read Modify Write PWM values */ |
| 951 | adt7475_read_pwm(client, sattr->index); |
| 952 | r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]); |
| 953 | if (r) |
| 954 | count = r; |
| 955 | mutex_unlock(&data->lock); |
| 956 | |
| 957 | return count; |
| 958 | } |
| 959 | |
| 960 | /* List of frequencies for the PWM */ |
| 961 | static const int pwmfreq_table[] = { |
Chris Packham | 3490c92 | 2017-05-15 13:30:29 +1200 | [diff] [blame] | 962 | 11, 14, 22, 29, 35, 44, 58, 88, 22500 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 963 | }; |
| 964 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 965 | static ssize_t pwmfreq_show(struct device *dev, struct device_attribute *attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 966 | char *buf) |
| 967 | { |
| 968 | struct adt7475_data *data = adt7475_update_device(dev); |
| 969 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Dan Carpenter | 9d19371 | 2018-08-14 12:12:36 +0300 | [diff] [blame] | 970 | int idx; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 971 | |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 972 | if (IS_ERR(data)) |
| 973 | return PTR_ERR(data); |
Dan Carpenter | 9d19371 | 2018-08-14 12:12:36 +0300 | [diff] [blame] | 974 | idx = clamp_val(data->range[sattr->index] & 0xf, 0, |
| 975 | ARRAY_SIZE(pwmfreq_table) - 1); |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 976 | |
Dan Carpenter | 9d19371 | 2018-08-14 12:12:36 +0300 | [diff] [blame] | 977 | return sprintf(buf, "%d\n", pwmfreq_table[idx]); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 978 | } |
| 979 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 980 | static ssize_t pwmfreq_store(struct device *dev, |
| 981 | struct device_attribute *attr, const char *buf, |
| 982 | size_t count) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 983 | { |
| 984 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 985 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 986 | struct i2c_client *client = data->client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 987 | int out; |
| 988 | long val; |
| 989 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 990 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 991 | return -EINVAL; |
| 992 | |
Chris Packham | e465164 | 2017-05-11 15:45:21 +1200 | [diff] [blame] | 993 | out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table)); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 994 | |
| 995 | mutex_lock(&data->lock); |
| 996 | |
| 997 | data->range[sattr->index] = |
| 998 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); |
Chris Packham | 3490c92 | 2017-05-15 13:30:29 +1200 | [diff] [blame] | 999 | data->range[sattr->index] &= ~0xf; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1000 | data->range[sattr->index] |= out; |
| 1001 | |
| 1002 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), |
| 1003 | data->range[sattr->index]); |
| 1004 | |
| 1005 | mutex_unlock(&data->lock); |
| 1006 | return count; |
| 1007 | } |
| 1008 | |
Julia Lawall | 1d05303c | 2016-12-22 13:05:33 +0100 | [diff] [blame] | 1009 | static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev, |
| 1010 | struct device_attribute *devattr, |
| 1011 | char *buf) |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1012 | { |
| 1013 | struct adt7475_data *data = adt7475_update_device(dev); |
Dan Carpenter | 9d19371 | 2018-08-14 12:12:36 +0300 | [diff] [blame] | 1014 | |
| 1015 | if (IS_ERR(data)) |
| 1016 | return PTR_ERR(data); |
| 1017 | |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1018 | return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY)); |
| 1019 | } |
| 1020 | |
Julia Lawall | 1d05303c | 2016-12-22 13:05:33 +0100 | [diff] [blame] | 1021 | static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev, |
| 1022 | struct device_attribute *devattr, |
| 1023 | const char *buf, size_t count) |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1024 | { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1025 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 1026 | struct i2c_client *client = data->client; |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1027 | long val; |
| 1028 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 1029 | if (kstrtol(buf, 10, &val)) |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1030 | return -EINVAL; |
| 1031 | if (val != 0 && val != 1) |
| 1032 | return -EINVAL; |
| 1033 | |
| 1034 | mutex_lock(&data->lock); |
| 1035 | data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4); |
| 1036 | if (val) |
| 1037 | data->config4 |= CONFIG4_MAXDUTY; |
| 1038 | else |
| 1039 | data->config4 &= ~CONFIG4_MAXDUTY; |
| 1040 | i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4); |
| 1041 | mutex_unlock(&data->lock); |
| 1042 | |
| 1043 | return count; |
| 1044 | } |
| 1045 | |
Julia Lawall | 1d05303c | 2016-12-22 13:05:33 +0100 | [diff] [blame] | 1046 | static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr, |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1047 | char *buf) |
| 1048 | { |
| 1049 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 1050 | return sprintf(buf, "%d\n", (int)data->vrm); |
| 1051 | } |
| 1052 | |
Julia Lawall | 1d05303c | 2016-12-22 13:05:33 +0100 | [diff] [blame] | 1053 | static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr, |
| 1054 | const char *buf, size_t count) |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1055 | { |
| 1056 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 1057 | long val; |
| 1058 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 1059 | if (kstrtol(buf, 10, &val)) |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1060 | return -EINVAL; |
| 1061 | if (val < 0 || val > 255) |
| 1062 | return -EINVAL; |
| 1063 | data->vrm = val; |
| 1064 | |
| 1065 | return count; |
| 1066 | } |
| 1067 | |
Julia Lawall | 1d05303c | 2016-12-22 13:05:33 +0100 | [diff] [blame] | 1068 | static ssize_t cpu0_vid_show(struct device *dev, |
| 1069 | struct device_attribute *devattr, char *buf) |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1070 | { |
| 1071 | struct adt7475_data *data = adt7475_update_device(dev); |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 1072 | |
| 1073 | if (IS_ERR(data)) |
| 1074 | return PTR_ERR(data); |
| 1075 | |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1076 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 1077 | } |
| 1078 | |
Guenter Roeck | c24f9ba | 2018-12-10 14:02:02 -0800 | [diff] [blame] | 1079 | static SENSOR_DEVICE_ATTR_2_RO(in0_input, voltage, INPUT, 0); |
| 1080 | static SENSOR_DEVICE_ATTR_2_RW(in0_max, voltage, MAX, 0); |
| 1081 | static SENSOR_DEVICE_ATTR_2_RW(in0_min, voltage, MIN, 0); |
| 1082 | static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, voltage, ALARM, 0); |
| 1083 | static SENSOR_DEVICE_ATTR_2_RO(in1_input, voltage, INPUT, 1); |
| 1084 | static SENSOR_DEVICE_ATTR_2_RW(in1_max, voltage, MAX, 1); |
| 1085 | static SENSOR_DEVICE_ATTR_2_RW(in1_min, voltage, MIN, 1); |
| 1086 | static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, voltage, ALARM, 1); |
| 1087 | static SENSOR_DEVICE_ATTR_2_RO(in2_input, voltage, INPUT, 2); |
| 1088 | static SENSOR_DEVICE_ATTR_2_RW(in2_max, voltage, MAX, 2); |
| 1089 | static SENSOR_DEVICE_ATTR_2_RW(in2_min, voltage, MIN, 2); |
| 1090 | static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, voltage, ALARM, 2); |
| 1091 | static SENSOR_DEVICE_ATTR_2_RO(in3_input, voltage, INPUT, 3); |
| 1092 | static SENSOR_DEVICE_ATTR_2_RW(in3_max, voltage, MAX, 3); |
| 1093 | static SENSOR_DEVICE_ATTR_2_RW(in3_min, voltage, MIN, 3); |
| 1094 | static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, voltage, ALARM, 3); |
| 1095 | static SENSOR_DEVICE_ATTR_2_RO(in4_input, voltage, INPUT, 4); |
| 1096 | static SENSOR_DEVICE_ATTR_2_RW(in4_max, voltage, MAX, 4); |
| 1097 | static SENSOR_DEVICE_ATTR_2_RW(in4_min, voltage, MIN, 4); |
| 1098 | static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, voltage, ALARM, 8); |
| 1099 | static SENSOR_DEVICE_ATTR_2_RO(in5_input, voltage, INPUT, 5); |
| 1100 | static SENSOR_DEVICE_ATTR_2_RW(in5_max, voltage, MAX, 5); |
| 1101 | static SENSOR_DEVICE_ATTR_2_RW(in5_min, voltage, MIN, 5); |
| 1102 | static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, voltage, ALARM, 31); |
| 1103 | static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, INPUT, 0); |
| 1104 | static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, temp, ALARM, 0); |
| 1105 | static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, temp, FAULT, 0); |
| 1106 | static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, MAX, 0); |
| 1107 | static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, MIN, 0); |
| 1108 | static SENSOR_DEVICE_ATTR_2_RW(temp1_offset, temp, OFFSET, 0); |
| 1109 | static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point1_temp, temp, AUTOMIN, 0); |
| 1110 | static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, point2, 0, 0); |
| 1111 | static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, THERM, 0); |
| 1112 | static SENSOR_DEVICE_ATTR_2_RW(temp1_crit_hyst, temp, HYSTERSIS, 0); |
| 1113 | static SENSOR_DEVICE_ATTR_2_RW(temp1_smoothing, temp_st, 0, 0); |
| 1114 | static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, INPUT, 1); |
| 1115 | static SENSOR_DEVICE_ATTR_2_RO(temp2_alarm, temp, ALARM, 1); |
| 1116 | static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, MAX, 1); |
| 1117 | static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, MIN, 1); |
| 1118 | static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, OFFSET, 1); |
| 1119 | static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp, AUTOMIN, 1); |
| 1120 | static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, point2, 0, 1); |
| 1121 | static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, THERM, 1); |
| 1122 | static SENSOR_DEVICE_ATTR_2_RW(temp2_crit_hyst, temp, HYSTERSIS, 1); |
| 1123 | static SENSOR_DEVICE_ATTR_2_RW(temp2_smoothing, temp_st, 0, 1); |
| 1124 | static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, INPUT, 2); |
| 1125 | static SENSOR_DEVICE_ATTR_2_RO(temp3_alarm, temp, ALARM, 2); |
| 1126 | static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, temp, FAULT, 2); |
| 1127 | static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, MAX, 2); |
| 1128 | static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, MIN, 2); |
| 1129 | static SENSOR_DEVICE_ATTR_2_RW(temp3_offset, temp, OFFSET, 2); |
| 1130 | static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point1_temp, temp, AUTOMIN, 2); |
| 1131 | static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point2_temp, point2, 0, 2); |
| 1132 | static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, THERM, 2); |
| 1133 | static SENSOR_DEVICE_ATTR_2_RW(temp3_crit_hyst, temp, HYSTERSIS, 2); |
| 1134 | static SENSOR_DEVICE_ATTR_2_RW(temp3_smoothing, temp_st, 0, 2); |
| 1135 | static SENSOR_DEVICE_ATTR_2_RO(fan1_input, tach, INPUT, 0); |
| 1136 | static SENSOR_DEVICE_ATTR_2_RW(fan1_min, tach, MIN, 0); |
| 1137 | static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, tach, ALARM, 0); |
| 1138 | static SENSOR_DEVICE_ATTR_2_RO(fan2_input, tach, INPUT, 1); |
| 1139 | static SENSOR_DEVICE_ATTR_2_RW(fan2_min, tach, MIN, 1); |
| 1140 | static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, tach, ALARM, 1); |
| 1141 | static SENSOR_DEVICE_ATTR_2_RO(fan3_input, tach, INPUT, 2); |
| 1142 | static SENSOR_DEVICE_ATTR_2_RW(fan3_min, tach, MIN, 2); |
| 1143 | static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, tach, ALARM, 2); |
| 1144 | static SENSOR_DEVICE_ATTR_2_RO(fan4_input, tach, INPUT, 3); |
| 1145 | static SENSOR_DEVICE_ATTR_2_RW(fan4_min, tach, MIN, 3); |
| 1146 | static SENSOR_DEVICE_ATTR_2_RO(fan4_alarm, tach, ALARM, 3); |
| 1147 | static SENSOR_DEVICE_ATTR_2_RW(pwm1, pwm, INPUT, 0); |
| 1148 | static SENSOR_DEVICE_ATTR_2_RW(pwm1_freq, pwmfreq, INPUT, 0); |
| 1149 | static SENSOR_DEVICE_ATTR_2_RW(pwm1_enable, pwmctrl, INPUT, 0); |
| 1150 | static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_channels_temp, pwmchan, INPUT, 0); |
| 1151 | static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, pwm, MIN, 0); |
| 1152 | static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, pwm, MAX, 0); |
| 1153 | static SENSOR_DEVICE_ATTR_2_RW(pwm1_stall_disable, stall_disable, 0, 0); |
| 1154 | static SENSOR_DEVICE_ATTR_2_RW(pwm2, pwm, INPUT, 1); |
| 1155 | static SENSOR_DEVICE_ATTR_2_RW(pwm2_freq, pwmfreq, INPUT, 1); |
| 1156 | static SENSOR_DEVICE_ATTR_2_RW(pwm2_enable, pwmctrl, INPUT, 1); |
| 1157 | static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_channels_temp, pwmchan, INPUT, 1); |
| 1158 | static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, pwm, MIN, 1); |
| 1159 | static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, pwm, MAX, 1); |
| 1160 | static SENSOR_DEVICE_ATTR_2_RW(pwm2_stall_disable, stall_disable, 0, 1); |
| 1161 | static SENSOR_DEVICE_ATTR_2_RW(pwm3, pwm, INPUT, 2); |
| 1162 | static SENSOR_DEVICE_ATTR_2_RW(pwm3_freq, pwmfreq, INPUT, 2); |
| 1163 | static SENSOR_DEVICE_ATTR_2_RW(pwm3_enable, pwmctrl, INPUT, 2); |
| 1164 | static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_channels_temp, pwmchan, INPUT, 2); |
| 1165 | static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, pwm, MIN, 2); |
| 1166 | static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, pwm, MAX, 2); |
| 1167 | static SENSOR_DEVICE_ATTR_2_RW(pwm3_stall_disable, stall_disable, 0, 2); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1168 | |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1169 | /* Non-standard name, might need revisiting */ |
Julia Lawall | 1d05303c | 2016-12-22 13:05:33 +0100 | [diff] [blame] | 1170 | static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit); |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1171 | |
Julia Lawall | 1d05303c | 2016-12-22 13:05:33 +0100 | [diff] [blame] | 1172 | static DEVICE_ATTR_RW(vrm); |
| 1173 | static DEVICE_ATTR_RO(cpu0_vid); |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1174 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1175 | static struct attribute *adt7475_attrs[] = { |
| 1176 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 1177 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 1178 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 1179 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 1180 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 1181 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 1182 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 1183 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 1184 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 1185 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 1186 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
| 1187 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 1188 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 1189 | &sensor_dev_attr_temp1_offset.dev_attr.attr, |
| 1190 | &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, |
| 1191 | &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, |
| 1192 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 1193 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
Chris Packham | 8f05bcc | 2017-05-15 13:30:28 +1200 | [diff] [blame] | 1194 | &sensor_dev_attr_temp1_smoothing.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1195 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 1196 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 1197 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 1198 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 1199 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
| 1200 | &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, |
| 1201 | &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, |
| 1202 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 1203 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
Chris Packham | 8f05bcc | 2017-05-15 13:30:28 +1200 | [diff] [blame] | 1204 | &sensor_dev_attr_temp2_smoothing.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1205 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 1206 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
| 1207 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 1208 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 1209 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 1210 | &sensor_dev_attr_temp3_offset.dev_attr.attr, |
| 1211 | &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, |
| 1212 | &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, |
| 1213 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 1214 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
Chris Packham | 8f05bcc | 2017-05-15 13:30:28 +1200 | [diff] [blame] | 1215 | &sensor_dev_attr_temp3_smoothing.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1216 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 1217 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 1218 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 1219 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 1220 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 1221 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 1222 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 1223 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 1224 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1225 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 1226 | &sensor_dev_attr_pwm1_freq.dev_attr.attr, |
| 1227 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1228 | &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1229 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
| 1230 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 1231 | &sensor_dev_attr_pwm1_stall_disable.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1232 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1233 | &sensor_dev_attr_pwm3_freq.dev_attr.attr, |
| 1234 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1235 | &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1236 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, |
| 1237 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 1238 | &sensor_dev_attr_pwm3_stall_disable.dev_attr.attr, |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1239 | &dev_attr_pwm_use_point2_pwm_at_crit.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1240 | NULL, |
| 1241 | }; |
| 1242 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1243 | static struct attribute *fan4_attrs[] = { |
| 1244 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
| 1245 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
| 1246 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
| 1247 | NULL |
| 1248 | }; |
| 1249 | |
| 1250 | static struct attribute *pwm2_attrs[] = { |
| 1251 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 1252 | &sensor_dev_attr_pwm2_freq.dev_attr.attr, |
| 1253 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 1254 | &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, |
| 1255 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, |
| 1256 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, |
Chris Packham | 1d58f5e | 2017-05-15 13:30:27 +1200 | [diff] [blame] | 1257 | &sensor_dev_attr_pwm2_stall_disable.dev_attr.attr, |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1258 | NULL |
| 1259 | }; |
| 1260 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1261 | static struct attribute *in0_attrs[] = { |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1262 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 1263 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 1264 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 1265 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1266 | NULL |
| 1267 | }; |
| 1268 | |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1269 | static struct attribute *in3_attrs[] = { |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1270 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 1271 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 1272 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 1273 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1274 | NULL |
| 1275 | }; |
| 1276 | |
| 1277 | static struct attribute *in4_attrs[] = { |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1278 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 1279 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 1280 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 1281 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1282 | NULL |
| 1283 | }; |
| 1284 | |
| 1285 | static struct attribute *in5_attrs[] = { |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1286 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 1287 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 1288 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 1289 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 1290 | NULL |
| 1291 | }; |
| 1292 | |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1293 | static struct attribute *vid_attrs[] = { |
| 1294 | &dev_attr_cpu0_vid.attr, |
| 1295 | &dev_attr_vrm.attr, |
| 1296 | NULL |
| 1297 | }; |
| 1298 | |
Arvind Yadav | f5397be | 2017-07-05 11:04:04 +0530 | [diff] [blame] | 1299 | static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs }; |
| 1300 | static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs }; |
| 1301 | static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs }; |
| 1302 | static const struct attribute_group in0_attr_group = { .attrs = in0_attrs }; |
| 1303 | static const struct attribute_group in3_attr_group = { .attrs = in3_attrs }; |
| 1304 | static const struct attribute_group in4_attr_group = { .attrs = in4_attrs }; |
| 1305 | static const struct attribute_group in5_attr_group = { .attrs = in5_attrs }; |
| 1306 | static const struct attribute_group vid_attr_group = { .attrs = vid_attrs }; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1307 | |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 1308 | static int adt7475_detect(struct i2c_client *client, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1309 | struct i2c_board_info *info) |
| 1310 | { |
| 1311 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 1312 | int vendid, devid, devid2; |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1313 | const char *name; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1314 | |
| 1315 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 1316 | return -ENODEV; |
| 1317 | |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1318 | vendid = adt7475_read(REG_VENDID); |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 1319 | devid2 = adt7475_read(REG_DEVID2); |
| 1320 | if (vendid != 0x41 || /* Analog Devices */ |
| 1321 | (devid2 & 0xf8) != 0x68) |
| 1322 | return -ENODEV; |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1323 | |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 1324 | devid = adt7475_read(REG_DEVID); |
| 1325 | if (devid == 0x73) |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1326 | name = "adt7473"; |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 1327 | else if (devid == 0x75 && client->addr == 0x2e) |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1328 | name = "adt7475"; |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1329 | else if (devid == 0x76) |
| 1330 | name = "adt7476"; |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1331 | else if ((devid2 & 0xfc) == 0x6c) |
| 1332 | name = "adt7490"; |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1333 | else { |
| 1334 | dev_dbg(&adapter->dev, |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1335 | "Couldn't detect an ADT7473/75/76/90 part at " |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1336 | "0x%02x\n", (unsigned int)client->addr); |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1337 | return -ENODEV; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1340 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1341 | |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1345 | static int adt7475_update_limits(struct i2c_client *client) |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1346 | { |
| 1347 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1348 | int i; |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1349 | int ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1350 | |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1351 | ret = adt7475_read(REG_CONFIG4); |
| 1352 | if (ret < 0) |
| 1353 | return ret; |
| 1354 | data->config4 = ret; |
| 1355 | |
| 1356 | ret = adt7475_read(REG_CONFIG5); |
| 1357 | if (ret < 0) |
| 1358 | return ret; |
| 1359 | data->config5 = ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1360 | |
| 1361 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { |
| 1362 | if (!(data->has_voltage & (1 << i))) |
| 1363 | continue; |
| 1364 | /* Adjust values so they match the input precision */ |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1365 | ret = adt7475_read(VOLTAGE_MIN_REG(i)); |
| 1366 | if (ret < 0) |
| 1367 | return ret; |
| 1368 | data->voltage[MIN][i] = ret << 2; |
| 1369 | |
| 1370 | ret = adt7475_read(VOLTAGE_MAX_REG(i)); |
| 1371 | if (ret < 0) |
| 1372 | return ret; |
| 1373 | data->voltage[MAX][i] = ret << 2; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | if (data->has_voltage & (1 << 5)) { |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1377 | ret = adt7475_read(REG_VTT_MIN); |
| 1378 | if (ret < 0) |
| 1379 | return ret; |
| 1380 | data->voltage[MIN][5] = ret << 2; |
| 1381 | |
| 1382 | ret = adt7475_read(REG_VTT_MAX); |
| 1383 | if (ret < 0) |
| 1384 | return ret; |
| 1385 | data->voltage[MAX][5] = ret << 2; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) { |
| 1389 | /* Adjust values so they match the input precision */ |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1390 | ret = adt7475_read(TEMP_MIN_REG(i)); |
| 1391 | if (ret < 0) |
| 1392 | return ret; |
| 1393 | data->temp[MIN][i] = ret << 2; |
| 1394 | |
| 1395 | ret = adt7475_read(TEMP_MAX_REG(i)); |
| 1396 | if (ret < 0) |
| 1397 | return ret; |
| 1398 | data->temp[MAX][i] = ret << 2; |
| 1399 | |
| 1400 | ret = adt7475_read(TEMP_TMIN_REG(i)); |
| 1401 | if (ret < 0) |
| 1402 | return ret; |
| 1403 | data->temp[AUTOMIN][i] = ret << 2; |
| 1404 | |
| 1405 | ret = adt7475_read(TEMP_THERM_REG(i)); |
| 1406 | if (ret < 0) |
| 1407 | return ret; |
| 1408 | data->temp[THERM][i] = ret << 2; |
| 1409 | |
| 1410 | ret = adt7475_read(TEMP_OFFSET_REG(i)); |
| 1411 | if (ret < 0) |
| 1412 | return ret; |
| 1413 | data->temp[OFFSET][i] = ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1414 | } |
| 1415 | adt7475_read_hystersis(client); |
| 1416 | |
| 1417 | for (i = 0; i < ADT7475_TACH_COUNT; i++) { |
| 1418 | if (i == 3 && !data->has_fan4) |
| 1419 | continue; |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1420 | ret = adt7475_read_word(client, TACH_MIN_REG(i)); |
| 1421 | if (ret < 0) |
| 1422 | return ret; |
| 1423 | data->tach[MIN][i] = ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | for (i = 0; i < ADT7475_PWM_COUNT; i++) { |
| 1427 | if (i == 1 && !data->has_pwm2) |
| 1428 | continue; |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1429 | ret = adt7475_read(PWM_MAX_REG(i)); |
| 1430 | if (ret < 0) |
| 1431 | return ret; |
| 1432 | data->pwm[MAX][i] = ret; |
| 1433 | |
| 1434 | ret = adt7475_read(PWM_MIN_REG(i)); |
| 1435 | if (ret < 0) |
| 1436 | return ret; |
| 1437 | data->pwm[MIN][i] = ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1438 | /* Set the channel and control information */ |
| 1439 | adt7475_read_pwm(client, i); |
| 1440 | } |
| 1441 | |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1442 | ret = adt7475_read(TEMP_TRANGE_REG(0)); |
| 1443 | if (ret < 0) |
| 1444 | return ret; |
| 1445 | data->range[0] = ret; |
| 1446 | |
| 1447 | ret = adt7475_read(TEMP_TRANGE_REG(1)); |
| 1448 | if (ret < 0) |
| 1449 | return ret; |
| 1450 | data->range[1] = ret; |
| 1451 | |
| 1452 | ret = adt7475_read(TEMP_TRANGE_REG(2)); |
| 1453 | if (ret < 0) |
| 1454 | return ret; |
| 1455 | data->range[2] = ret; |
| 1456 | |
| 1457 | return 0; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1458 | } |
| 1459 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1460 | static int adt7475_probe(struct i2c_client *client, |
| 1461 | const struct i2c_device_id *id) |
| 1462 | { |
Javier Martinez Canillas | 4e2496e | 2017-02-24 10:12:58 -0300 | [diff] [blame] | 1463 | enum chips chip; |
Frans Meulenbroeks | 99b8c83 | 2012-01-08 19:34:08 +0100 | [diff] [blame] | 1464 | static const char * const names[] = { |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1465 | [adt7473] = "ADT7473", |
| 1466 | [adt7475] = "ADT7475", |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1467 | [adt7476] = "ADT7476", |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1468 | [adt7490] = "ADT7490", |
| 1469 | }; |
| 1470 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1471 | struct adt7475_data *data; |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1472 | struct device *hwmon_dev; |
| 1473 | int i, ret = 0, revision, group_num = 0; |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1474 | u8 config2, config3; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1475 | |
Guenter Roeck | e3ecb2e | 2012-06-02 09:58:01 -0700 | [diff] [blame] | 1476 | data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1477 | if (data == NULL) |
| 1478 | return -ENOMEM; |
| 1479 | |
| 1480 | mutex_init(&data->lock); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1481 | data->client = client; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1482 | i2c_set_clientdata(client, data); |
| 1483 | |
Javier Martinez Canillas | 4e2496e | 2017-02-24 10:12:58 -0300 | [diff] [blame] | 1484 | if (client->dev.of_node) |
| 1485 | chip = (enum chips)of_device_get_match_data(&client->dev); |
| 1486 | else |
| 1487 | chip = id->driver_data; |
| 1488 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1489 | /* Initialize device-specific values */ |
Javier Martinez Canillas | 4e2496e | 2017-02-24 10:12:58 -0300 | [diff] [blame] | 1490 | switch (chip) { |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1491 | case adt7476: |
| 1492 | data->has_voltage = 0x0e; /* in1 to in3 */ |
| 1493 | revision = adt7475_read(REG_DEVID2) & 0x07; |
| 1494 | break; |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1495 | case adt7490: |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1496 | data->has_voltage = 0x3e; /* in1 to in5 */ |
| 1497 | revision = adt7475_read(REG_DEVID2) & 0x03; |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1498 | if (revision == 0x03) |
| 1499 | revision += adt7475_read(REG_DEVREV2); |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1500 | break; |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1501 | default: |
| 1502 | data->has_voltage = 0x06; /* in1, in2 */ |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1503 | revision = adt7475_read(REG_DEVID2) & 0x07; |
| 1504 | } |
| 1505 | |
| 1506 | config3 = adt7475_read(REG_CONFIG3); |
| 1507 | /* Pin PWM2 may alternatively be used for ALERT output */ |
| 1508 | if (!(config3 & CONFIG3_SMBALERT)) |
| 1509 | data->has_pwm2 = 1; |
| 1510 | /* Meaning of this bit is inverted for the ADT7473-1 */ |
| 1511 | if (id->driver_data == adt7473 && revision >= 1) |
| 1512 | data->has_pwm2 = !data->has_pwm2; |
| 1513 | |
| 1514 | data->config4 = adt7475_read(REG_CONFIG4); |
| 1515 | /* Pin TACH4 may alternatively be used for THERM */ |
| 1516 | if ((data->config4 & CONFIG4_PINFUNC) == 0x0) |
| 1517 | data->has_fan4 = 1; |
| 1518 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1519 | /* |
| 1520 | * THERM configuration is more complex on the ADT7476 and ADT7490, |
| 1521 | * because 2 different pins (TACH4 and +2.5 Vin) can be used for |
| 1522 | * this function |
| 1523 | */ |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1524 | if (id->driver_data == adt7490) { |
| 1525 | if ((data->config4 & CONFIG4_PINFUNC) == 0x1 && |
| 1526 | !(config3 & CONFIG3_THERM)) |
| 1527 | data->has_fan4 = 1; |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1528 | } |
| 1529 | if (id->driver_data == adt7476 || id->driver_data == adt7490) { |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1530 | if (!(config3 & CONFIG3_THERM) || |
| 1531 | (data->config4 & CONFIG4_PINFUNC) == 0x1) |
| 1532 | data->has_voltage |= (1 << 0); /* in0 */ |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1533 | } |
| 1534 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1535 | /* |
| 1536 | * On the ADT7476, the +12V input pin may instead be used as VID5, |
| 1537 | * and VID pins may alternatively be used as GPIO |
| 1538 | */ |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1539 | if (id->driver_data == adt7476) { |
| 1540 | u8 vid = adt7475_read(REG_VID); |
| 1541 | if (!(vid & VID_VIDSEL)) |
| 1542 | data->has_voltage |= (1 << 4); /* in4 */ |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1543 | |
| 1544 | data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO); |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1545 | } |
| 1546 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1547 | /* Voltage attenuators can be bypassed, globally or individually */ |
| 1548 | config2 = adt7475_read(REG_CONFIG2); |
| 1549 | if (config2 & CONFIG2_ATTN) { |
| 1550 | data->bypass_attn = (0x3 << 3) | 0x3; |
| 1551 | } else { |
| 1552 | data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) | |
| 1553 | ((data->config4 & CONFIG4_ATTN_IN43) >> 3); |
| 1554 | } |
| 1555 | data->bypass_attn &= data->has_voltage; |
| 1556 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1557 | /* |
| 1558 | * Call adt7475_read_pwm for all pwm's as this will reprogram any |
| 1559 | * pwm's which are disabled to manual mode with 0% duty cycle |
| 1560 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1561 | for (i = 0; i < ADT7475_PWM_COUNT; i++) |
| 1562 | adt7475_read_pwm(client, i); |
| 1563 | |
Chris Packham | 4abdf38 | 2017-04-22 07:08:09 +1200 | [diff] [blame] | 1564 | /* Start monitoring */ |
| 1565 | switch (chip) { |
| 1566 | case adt7475: |
| 1567 | case adt7476: |
| 1568 | i2c_smbus_write_byte_data(client, REG_CONFIG1, |
| 1569 | adt7475_read(REG_CONFIG1) | 0x01); |
| 1570 | break; |
| 1571 | default: |
| 1572 | break; |
| 1573 | } |
| 1574 | |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1575 | data->groups[group_num++] = &adt7475_attr_group; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1576 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1577 | /* Features that can be disabled individually */ |
| 1578 | if (data->has_fan4) { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1579 | data->groups[group_num++] = &fan4_attr_group; |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1580 | } |
| 1581 | if (data->has_pwm2) { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1582 | data->groups[group_num++] = &pwm2_attr_group; |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1583 | } |
| 1584 | if (data->has_voltage & (1 << 0)) { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1585 | data->groups[group_num++] = &in0_attr_group; |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1586 | } |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1587 | if (data->has_voltage & (1 << 3)) { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1588 | data->groups[group_num++] = &in3_attr_group; |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1589 | } |
| 1590 | if (data->has_voltage & (1 << 4)) { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1591 | data->groups[group_num++] = &in4_attr_group; |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1592 | } |
| 1593 | if (data->has_voltage & (1 << 5)) { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1594 | data->groups[group_num++] = &in5_attr_group; |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1595 | } |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1596 | if (data->has_vid) { |
| 1597 | data->vrm = vid_which_vrm(); |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1598 | data->groups[group_num] = &vid_attr_group; |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1599 | } |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1600 | |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1601 | /* register device with all the acquired attributes */ |
| 1602 | hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, |
| 1603 | client->name, data, |
| 1604 | data->groups); |
| 1605 | |
| 1606 | if (IS_ERR(hwmon_dev)) { |
| 1607 | ret = PTR_ERR(hwmon_dev); |
| 1608 | return ret; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1609 | } |
| 1610 | |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1611 | dev_info(&client->dev, "%s device, revision %d\n", |
| 1612 | names[id->driver_data], revision); |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1613 | if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2) |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1614 | dev_info(&client->dev, "Optional features:%s%s%s%s%s\n", |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1615 | (data->has_voltage & (1 << 0)) ? " in0" : "", |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1616 | (data->has_voltage & (1 << 4)) ? " in4" : "", |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1617 | data->has_fan4 ? " fan4" : "", |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1618 | data->has_pwm2 ? " pwm2" : "", |
| 1619 | data->has_vid ? " vid" : ""); |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1620 | if (data->bypass_attn) |
| 1621 | dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n", |
| 1622 | (data->bypass_attn & (1 << 0)) ? " in0" : "", |
| 1623 | (data->bypass_attn & (1 << 1)) ? " in1" : "", |
| 1624 | (data->bypass_attn & (1 << 3)) ? " in3" : "", |
| 1625 | (data->bypass_attn & (1 << 4)) ? " in4" : ""); |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1626 | |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1627 | /* Limits and settings, should never change update more than once */ |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 1628 | ret = adt7475_update_limits(client); |
| 1629 | if (ret) |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1630 | return ret; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1631 | |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | static struct i2c_driver adt7475_driver = { |
| 1636 | .class = I2C_CLASS_HWMON, |
| 1637 | .driver = { |
| 1638 | .name = "adt7475", |
Javier Martinez Canillas | 4e2496e | 2017-02-24 10:12:58 -0300 | [diff] [blame] | 1639 | .of_match_table = of_match_ptr(adt7475_of_match), |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1640 | }, |
| 1641 | .probe = adt7475_probe, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1642 | .id_table = adt7475_id, |
| 1643 | .detect = adt7475_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 1644 | .address_list = normal_i2c, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1645 | }; |
| 1646 | |
| 1647 | static void adt7475_read_hystersis(struct i2c_client *client) |
| 1648 | { |
| 1649 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1650 | |
| 1651 | data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS); |
| 1652 | data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0]; |
| 1653 | data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS); |
| 1654 | } |
| 1655 | |
| 1656 | static void adt7475_read_pwm(struct i2c_client *client, int index) |
| 1657 | { |
| 1658 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1659 | unsigned int v; |
| 1660 | |
| 1661 | data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index)); |
| 1662 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1663 | /* |
| 1664 | * Figure out the internal value for pwmctrl and pwmchan |
| 1665 | * based on the current settings |
| 1666 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1667 | v = (data->pwm[CONTROL][index] >> 5) & 7; |
| 1668 | |
| 1669 | if (v == 3) |
| 1670 | data->pwmctl[index] = 0; |
| 1671 | else if (v == 7) |
| 1672 | data->pwmctl[index] = 1; |
| 1673 | else if (v == 4) { |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1674 | /* |
| 1675 | * The fan is disabled - we don't want to |
| 1676 | * support that, so change to manual mode and |
| 1677 | * set the duty cycle to 0 instead |
| 1678 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1679 | data->pwm[INPUT][index] = 0; |
| 1680 | data->pwm[CONTROL][index] &= ~0xE0; |
| 1681 | data->pwm[CONTROL][index] |= (7 << 5); |
| 1682 | |
| 1683 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 1684 | data->pwm[INPUT][index]); |
| 1685 | |
| 1686 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 1687 | data->pwm[CONTROL][index]); |
| 1688 | |
| 1689 | data->pwmctl[index] = 1; |
| 1690 | } else { |
| 1691 | data->pwmctl[index] = 2; |
| 1692 | |
| 1693 | switch (v) { |
| 1694 | case 0: |
| 1695 | data->pwmchan[index] = 1; |
| 1696 | break; |
| 1697 | case 1: |
| 1698 | data->pwmchan[index] = 2; |
| 1699 | break; |
| 1700 | case 2: |
| 1701 | data->pwmchan[index] = 4; |
| 1702 | break; |
| 1703 | case 5: |
| 1704 | data->pwmchan[index] = 6; |
| 1705 | break; |
| 1706 | case 6: |
| 1707 | data->pwmchan[index] = 7; |
| 1708 | break; |
| 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1713 | static int adt7475_update_measure(struct device *dev) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1714 | { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1715 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 1716 | struct i2c_client *client = data->client; |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1717 | u16 ext; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1718 | int i; |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1719 | int ret; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1720 | |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1721 | ret = adt7475_read(REG_STATUS2); |
| 1722 | if (ret < 0) |
| 1723 | return ret; |
| 1724 | data->alarms = ret << 8; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1725 | |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1726 | ret = adt7475_read(REG_STATUS1); |
| 1727 | if (ret < 0) |
| 1728 | return ret; |
| 1729 | data->alarms |= ret; |
| 1730 | |
| 1731 | ret = adt7475_read(REG_EXTEND2); |
| 1732 | if (ret < 0) |
| 1733 | return ret; |
| 1734 | |
| 1735 | ext = (ret << 8); |
| 1736 | |
| 1737 | ret = adt7475_read(REG_EXTEND1); |
| 1738 | if (ret < 0) |
| 1739 | return ret; |
| 1740 | |
| 1741 | ext |= ret; |
| 1742 | |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1743 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { |
| 1744 | if (!(data->has_voltage & (1 << i))) |
| 1745 | continue; |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1746 | ret = adt7475_read(VOLTAGE_REG(i)); |
| 1747 | if (ret < 0) |
| 1748 | return ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1749 | data->voltage[INPUT][i] = |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1750 | (ret << 2) | |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1751 | ((ext >> (i * 2)) & 3); |
| 1752 | } |
| 1753 | |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1754 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) { |
| 1755 | ret = adt7475_read(TEMP_REG(i)); |
| 1756 | if (ret < 0) |
| 1757 | return ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1758 | data->temp[INPUT][i] = |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1759 | (ret << 2) | |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1760 | ((ext >> ((i + 5) * 2)) & 3); |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1761 | } |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1762 | |
| 1763 | if (data->has_voltage & (1 << 5)) { |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1764 | ret = adt7475_read(REG_STATUS4); |
| 1765 | if (ret < 0) |
| 1766 | return ret; |
| 1767 | data->alarms |= ret << 24; |
| 1768 | |
| 1769 | ret = adt7475_read(REG_EXTEND3); |
| 1770 | if (ret < 0) |
| 1771 | return ret; |
| 1772 | ext = ret; |
| 1773 | |
| 1774 | ret = adt7475_read(REG_VTT); |
| 1775 | if (ret < 0) |
| 1776 | return ret; |
| 1777 | data->voltage[INPUT][5] = ret << 2 | |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1778 | ((ext >> 4) & 3); |
| 1779 | } |
| 1780 | |
| 1781 | for (i = 0; i < ADT7475_TACH_COUNT; i++) { |
| 1782 | if (i == 3 && !data->has_fan4) |
| 1783 | continue; |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1784 | ret = adt7475_read_word(client, TACH_REG(i)); |
| 1785 | if (ret < 0) |
| 1786 | return ret; |
| 1787 | data->tach[INPUT][i] = ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | /* Updated by hw when in auto mode */ |
| 1791 | for (i = 0; i < ADT7475_PWM_COUNT; i++) { |
| 1792 | if (i == 1 && !data->has_pwm2) |
| 1793 | continue; |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1794 | ret = adt7475_read(PWM_REG(i)); |
| 1795 | if (ret < 0) |
| 1796 | return ret; |
| 1797 | data->pwm[INPUT][i] = ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1798 | } |
| 1799 | |
Tokunori Ikegami | 702afea | 2018-08-08 10:32:18 +0900 | [diff] [blame] | 1800 | if (data->has_vid) { |
| 1801 | ret = adt7475_read(REG_VID); |
| 1802 | if (ret < 0) |
| 1803 | return ret; |
| 1804 | data->vid = ret & 0x3f; |
| 1805 | } |
| 1806 | |
| 1807 | return 0; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | static struct adt7475_data *adt7475_update_device(struct device *dev) |
| 1811 | { |
Grant McEwan | c64fce7 | 2019-07-22 10:55:30 +1200 | [diff] [blame] | 1812 | struct adt7475_data *data = dev_get_drvdata(dev); |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 1813 | int ret; |
Tokunori Ikegami | 5cf943e | 2018-08-08 10:32:16 +0900 | [diff] [blame] | 1814 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1815 | mutex_lock(&data->lock); |
| 1816 | |
| 1817 | /* Measurement values update every 2 seconds */ |
| 1818 | if (time_after(jiffies, data->measure_updated + HZ * 2) || |
| 1819 | !data->valid) { |
Tokunori Ikegami | 4afec79 | 2018-08-08 10:32:19 +0900 | [diff] [blame] | 1820 | ret = adt7475_update_measure(dev); |
| 1821 | if (ret) { |
| 1822 | data->valid = false; |
| 1823 | mutex_unlock(&data->lock); |
| 1824 | return ERR_PTR(ret); |
| 1825 | } |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1826 | data->measure_updated = jiffies; |
Tokunori Ikegami | b36fb17 | 2018-08-08 10:32:17 +0900 | [diff] [blame] | 1827 | data->valid = true; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1830 | mutex_unlock(&data->lock); |
| 1831 | |
| 1832 | return data; |
| 1833 | } |
| 1834 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 1835 | module_i2c_driver(adt7475_driver); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1836 | |
| 1837 | MODULE_AUTHOR("Advanced Micro Devices, Inc"); |
| 1838 | MODULE_DESCRIPTION("adt7475 driver"); |
| 1839 | MODULE_LICENSE("GPL"); |