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