Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
09770b26 GR |
2 | * lm85.c - Part of lm_sensors, Linux kernel modules for hardware |
3 | * monitoring | |
4 | * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> | |
5 | * Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com> | |
6 | * Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de> | |
7 | * Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com> | |
590e8534 | 8 | * Copyright (C) 2007--2014 Jean Delvare <jdelvare@suse.de> |
09770b26 GR |
9 | * |
10 | * Chip details at <http://www.national.com/ds/LM/LM85.pdf> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; if not, write to the Free Software | |
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | */ | |
1da177e4 | 26 | |
1da177e4 LT |
27 | #include <linux/module.h> |
28 | #include <linux/init.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/jiffies.h> | |
31 | #include <linux/i2c.h> | |
943b0830 | 32 | #include <linux/hwmon.h> |
303760b4 | 33 | #include <linux/hwmon-vid.h> |
b353a487 | 34 | #include <linux/hwmon-sysfs.h> |
943b0830 | 35 | #include <linux/err.h> |
9a61bf63 | 36 | #include <linux/mutex.h> |
1da177e4 LT |
37 | |
38 | /* Addresses to scan */ | |
25e9c86d | 39 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
1da177e4 | 40 | |
e5e9f44c | 41 | enum chips { |
590e8534 | 42 | lm85, |
e5e9f44c | 43 | adm1027, adt7463, adt7468, |
06923f84 | 44 | emc6d100, emc6d102, emc6d103, emc6d103s |
e5e9f44c | 45 | }; |
1da177e4 LT |
46 | |
47 | /* The LM85 registers */ | |
48 | ||
09770b26 GR |
49 | #define LM85_REG_IN(nr) (0x20 + (nr)) |
50 | #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2) | |
51 | #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2) | |
1da177e4 | 52 | |
09770b26 GR |
53 | #define LM85_REG_TEMP(nr) (0x25 + (nr)) |
54 | #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2) | |
55 | #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2) | |
1da177e4 LT |
56 | |
57 | /* Fan speeds are LSB, MSB (2 bytes) */ | |
09770b26 GR |
58 | #define LM85_REG_FAN(nr) (0x28 + (nr) * 2) |
59 | #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2) | |
1da177e4 | 60 | |
09770b26 | 61 | #define LM85_REG_PWM(nr) (0x30 + (nr)) |
1da177e4 | 62 | |
09770b26 GR |
63 | #define LM85_REG_COMPANY 0x3e |
64 | #define LM85_REG_VERSTEP 0x3f | |
79b92f2b | 65 | |
09770b26 GR |
66 | #define ADT7468_REG_CFG5 0x7c |
67 | #define ADT7468_OFF64 (1 << 0) | |
68 | #define ADT7468_HFPWM (1 << 1) | |
69 | #define IS_ADT7468_OFF64(data) \ | |
79b92f2b | 70 | ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64)) |
09770b26 | 71 | #define IS_ADT7468_HFPWM(data) \ |
f6c61cff | 72 | ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM)) |
79b92f2b | 73 | |
1da177e4 | 74 | /* These are the recognized values for the above regs */ |
09770b26 GR |
75 | #define LM85_COMPANY_NATIONAL 0x01 |
76 | #define LM85_COMPANY_ANALOG_DEV 0x41 | |
77 | #define LM85_COMPANY_SMSC 0x5c | |
09770b26 GR |
78 | #define LM85_VERSTEP_LM85C 0x60 |
79 | #define LM85_VERSTEP_LM85B 0x62 | |
80 | #define LM85_VERSTEP_LM96000_1 0x68 | |
81 | #define LM85_VERSTEP_LM96000_2 0x69 | |
82 | #define LM85_VERSTEP_ADM1027 0x60 | |
83 | #define LM85_VERSTEP_ADT7463 0x62 | |
84 | #define LM85_VERSTEP_ADT7463C 0x6A | |
85 | #define LM85_VERSTEP_ADT7468_1 0x71 | |
86 | #define LM85_VERSTEP_ADT7468_2 0x72 | |
87 | #define LM85_VERSTEP_EMC6D100_A0 0x60 | |
88 | #define LM85_VERSTEP_EMC6D100_A1 0x61 | |
89 | #define LM85_VERSTEP_EMC6D102 0x65 | |
90 | #define LM85_VERSTEP_EMC6D103_A0 0x68 | |
91 | #define LM85_VERSTEP_EMC6D103_A1 0x69 | |
92 | #define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */ | |
93 | ||
94 | #define LM85_REG_CONFIG 0x40 | |
95 | ||
96 | #define LM85_REG_ALARM1 0x41 | |
97 | #define LM85_REG_ALARM2 0x42 | |
98 | ||
99 | #define LM85_REG_VID 0x43 | |
1da177e4 LT |
100 | |
101 | /* Automated FAN control */ | |
09770b26 GR |
102 | #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr)) |
103 | #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr)) | |
104 | #define LM85_REG_AFAN_SPIKE1 0x62 | |
105 | #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr)) | |
106 | #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr)) | |
107 | #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr)) | |
108 | #define LM85_REG_AFAN_HYST1 0x6d | |
109 | #define LM85_REG_AFAN_HYST2 0x6e | |
110 | ||
111 | #define ADM1027_REG_EXTEND_ADC1 0x76 | |
112 | #define ADM1027_REG_EXTEND_ADC2 0x77 | |
1da177e4 LT |
113 | |
114 | #define EMC6D100_REG_ALARM3 0x7d | |
115 | /* IN5, IN6 and IN7 */ | |
09770b26 GR |
116 | #define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5)) |
117 | #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2) | |
118 | #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2) | |
119 | #define EMC6D102_REG_EXTEND_ADC1 0x85 | |
120 | #define EMC6D102_REG_EXTEND_ADC2 0x86 | |
121 | #define EMC6D102_REG_EXTEND_ADC3 0x87 | |
122 | #define EMC6D102_REG_EXTEND_ADC4 0x88 | |
123 | ||
09770b26 GR |
124 | /* |
125 | * Conversions. Rounding and limit checking is only done on the TO_REG | |
126 | * variants. Note that you should be a bit careful with which arguments | |
127 | * these macros are called: arguments may be evaluated more than once. | |
1da177e4 LT |
128 | */ |
129 | ||
25985edc | 130 | /* IN are scaled according to built-in resistors */ |
e89e22b2 | 131 | static const int lm85_scaling[] = { /* .001 Volts */ |
1f44809a JD |
132 | 2500, 2250, 3300, 5000, 12000, |
133 | 3300, 1500, 1800 /*EMC6D100*/ | |
134 | }; | |
135 | #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from)) | |
1da177e4 | 136 | |
1f44809a | 137 | #define INS_TO_REG(n, val) \ |
2a844c14 | 138 | clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255) |
1da177e4 | 139 | |
1f44809a | 140 | #define INSEXT_FROM_REG(n, val, ext) \ |
5a4d3ef3 | 141 | SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n]) |
1da177e4 | 142 | |
1f44809a | 143 | #define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n]) |
1da177e4 LT |
144 | |
145 | /* FAN speed is measured using 90kHz clock */ | |
63f281a6 JD |
146 | static inline u16 FAN_TO_REG(unsigned long val) |
147 | { | |
148 | if (!val) | |
149 | return 0xffff; | |
2a844c14 | 150 | return clamp_val(5400000 / val, 1, 0xfffe); |
63f281a6 | 151 | } |
1f44809a JD |
152 | #define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \ |
153 | 5400000 / (val)) | |
1da177e4 LT |
154 | |
155 | /* Temperature is reported in .001 degC increments */ | |
156 | #define TEMP_TO_REG(val) \ | |
3248c3b7 | 157 | DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000) |
1f44809a | 158 | #define TEMPEXT_FROM_REG(val, ext) \ |
5a4d3ef3 JD |
159 | SCALE(((val) << 4) + (ext), 16, 1000) |
160 | #define TEMP_FROM_REG(val) ((val) * 1000) | |
1da177e4 | 161 | |
2a844c14 | 162 | #define PWM_TO_REG(val) clamp_val(val, 0, 255) |
1da177e4 LT |
163 | #define PWM_FROM_REG(val) (val) |
164 | ||
165 | ||
09770b26 GR |
166 | /* |
167 | * ZONEs have the following parameters: | |
1da177e4 LT |
168 | * Limit (low) temp, 1. degC |
169 | * Hysteresis (below limit), 1. degC (0-15) | |
170 | * Range of speed control, .1 degC (2-80) | |
171 | * Critical (high) temp, 1. degC | |
172 | * | |
173 | * FAN PWMs have the following parameters: | |
174 | * Reference Zone, 1, 2, 3, etc. | |
175 | * Spinup time, .05 sec | |
176 | * PWM value at limit/low temp, 1 count | |
177 | * PWM Frequency, 1. Hz | |
178 | * PWM is Min or OFF below limit, flag | |
179 | * Invert PWM output, flag | |
180 | * | |
181 | * Some chips filter the temp, others the fan. | |
182 | * Filter constant (or disabled) .1 seconds | |
183 | */ | |
184 | ||
185 | /* These are the zone temperature range encodings in .001 degree C */ | |
e89e22b2 | 186 | static const int lm85_range_map[] = { |
1f44809a JD |
187 | 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000, |
188 | 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000 | |
189 | }; | |
190 | ||
3248c3b7 | 191 | static int RANGE_TO_REG(long range) |
1da177e4 LT |
192 | { |
193 | int i; | |
194 | ||
d38b1497 | 195 | /* Find the closest match */ |
1b92adad JD |
196 | for (i = 0; i < 15; ++i) { |
197 | if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2) | |
198 | break; | |
1da177e4 | 199 | } |
d38b1497 | 200 | |
1b92adad | 201 | return i; |
1da177e4 | 202 | } |
1f44809a | 203 | #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f] |
1da177e4 | 204 | |
1da177e4 | 205 | /* These are the PWM frequency encodings */ |
34e7dc6c | 206 | static const int lm85_freq_map[8] = { /* 1 Hz */ |
8a0795d9 JD |
207 | 10, 15, 23, 30, 38, 47, 61, 94 |
208 | }; | |
209 | static const int adm1027_freq_map[8] = { /* 1 Hz */ | |
210 | 11, 15, 22, 29, 35, 44, 59, 88 | |
1f44809a JD |
211 | }; |
212 | ||
3248c3b7 | 213 | static int FREQ_TO_REG(const int *map, unsigned long freq) |
1da177e4 LT |
214 | { |
215 | int i; | |
216 | ||
86010c98 | 217 | /* Find the closest match */ |
1f44809a | 218 | for (i = 0; i < 7; ++i) |
8a0795d9 | 219 | if (freq <= (map[i] + map[i + 1]) / 2) |
1f44809a | 220 | break; |
e89e22b2 | 221 | return i; |
1da177e4 | 222 | } |
8a0795d9 JD |
223 | |
224 | static int FREQ_FROM_REG(const int *map, u8 reg) | |
225 | { | |
226 | return map[reg & 0x07]; | |
227 | } | |
1da177e4 | 228 | |
09770b26 GR |
229 | /* |
230 | * Since we can't use strings, I'm abusing these numbers | |
1da177e4 LT |
231 | * to stand in for the following meanings: |
232 | * 1 -- PWM responds to Zone 1 | |
233 | * 2 -- PWM responds to Zone 2 | |
234 | * 3 -- PWM responds to Zone 3 | |
235 | * 23 -- PWM responds to the higher temp of Zone 2 or 3 | |
236 | * 123 -- PWM responds to highest of Zone 1, 2, or 3 | |
237 | * 0 -- PWM is always at 0% (ie, off) | |
238 | * -1 -- PWM is always at 100% | |
239 | * -2 -- PWM responds to manual control | |
240 | */ | |
241 | ||
e89e22b2 JD |
242 | static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 }; |
243 | #define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5] | |
1da177e4 | 244 | |
1f44809a | 245 | static int ZONE_TO_REG(int zone) |
1da177e4 LT |
246 | { |
247 | int i; | |
248 | ||
1f44809a JD |
249 | for (i = 0; i <= 7; ++i) |
250 | if (zone == lm85_zone_map[i]) | |
251 | break; | |
252 | if (i > 7) /* Not found. */ | |
1da177e4 | 253 | i = 3; /* Always 100% */ |
e89e22b2 | 254 | return i << 5; |
1da177e4 LT |
255 | } |
256 | ||
2a844c14 | 257 | #define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15) |
1f44809a | 258 | #define HYST_FROM_REG(val) ((val) * 1000) |
1da177e4 | 259 | |
09770b26 GR |
260 | /* |
261 | * Chip sampling rates | |
1da177e4 LT |
262 | * |
263 | * Some sensors are not updated more frequently than once per second | |
264 | * so it doesn't make sense to read them more often than that. | |
265 | * We cache the results and return the saved data if the driver | |
266 | * is called again before a second has elapsed. | |
267 | * | |
268 | * Also, there is significant configuration data for this chip | |
269 | * given the automatic PWM fan control that is possible. There | |
270 | * are about 47 bytes of config data to only 22 bytes of actual | |
271 | * readings. So, we keep the config data up to date in the cache | |
272 | * when it is written and only sample it once every 1 *minute* | |
273 | */ | |
274 | #define LM85_DATA_INTERVAL (HZ + HZ / 2) | |
275 | #define LM85_CONFIG_INTERVAL (1 * 60 * HZ) | |
276 | ||
09770b26 GR |
277 | /* |
278 | * LM85 can automatically adjust fan speeds based on temperature | |
1da177e4 LT |
279 | * This structure encapsulates an entire Zone config. There are |
280 | * three zones (one for each temperature input) on the lm85 | |
281 | */ | |
282 | struct lm85_zone { | |
283 | s8 limit; /* Low temp limit */ | |
284 | u8 hyst; /* Low limit hysteresis. (0-15) */ | |
285 | u8 range; /* Temp range, encoded */ | |
286 | s8 critical; /* "All fans ON" temp limit */ | |
09770b26 GR |
287 | u8 max_desired; /* |
288 | * Actual "max" temperature specified. Preserved | |
1da177e4 LT |
289 | * to prevent "drift" as other autofan control |
290 | * values change. | |
291 | */ | |
292 | }; | |
293 | ||
294 | struct lm85_autofan { | |
295 | u8 config; /* Register value */ | |
1da177e4 LT |
296 | u8 min_pwm; /* Minimum PWM value, encoded */ |
297 | u8 min_off; /* Min PWM or OFF below "limit", flag */ | |
298 | }; | |
299 | ||
09770b26 GR |
300 | /* |
301 | * For each registered chip, we need to keep some data in memory. | |
302 | * The structure is dynamically allocated. | |
303 | */ | |
1da177e4 | 304 | struct lm85_data { |
746f6884 AL |
305 | struct i2c_client *client; |
306 | const struct attribute_group *groups[6]; | |
8a0795d9 | 307 | const int *freq_map; |
1da177e4 LT |
308 | enum chips type; |
309 | ||
de248805 GR |
310 | bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */ |
311 | ||
9a61bf63 | 312 | struct mutex update_lock; |
1da177e4 LT |
313 | int valid; /* !=0 if following fields are valid */ |
314 | unsigned long last_reading; /* In jiffies */ | |
315 | unsigned long last_config; /* In jiffies */ | |
316 | ||
317 | u8 in[8]; /* Register value */ | |
318 | u8 in_max[8]; /* Register value */ | |
319 | u8 in_min[8]; /* Register value */ | |
320 | s8 temp[3]; /* Register value */ | |
321 | s8 temp_min[3]; /* Register value */ | |
322 | s8 temp_max[3]; /* Register value */ | |
1da177e4 LT |
323 | u16 fan[4]; /* Register value */ |
324 | u16 fan_min[4]; /* Register value */ | |
325 | u8 pwm[3]; /* Register value */ | |
34e7dc6c | 326 | u8 pwm_freq[3]; /* Register encoding */ |
1da177e4 LT |
327 | u8 temp_ext[3]; /* Decoded values */ |
328 | u8 in_ext[8]; /* Decoded values */ | |
1da177e4 LT |
329 | u8 vid; /* Register value */ |
330 | u8 vrm; /* VRM version */ | |
1da177e4 | 331 | u32 alarms; /* Register encoding, combined */ |
79b92f2b | 332 | u8 cfg5; /* Config Register 5 on ADT7468 */ |
1da177e4 LT |
333 | struct lm85_autofan autofan[3]; |
334 | struct lm85_zone zone[3]; | |
335 | }; | |
336 | ||
6fd5dd58 AL |
337 | static int lm85_read_value(struct i2c_client *client, u8 reg) |
338 | { | |
339 | int res; | |
1da177e4 | 340 | |
6fd5dd58 AL |
341 | /* What size location is it? */ |
342 | switch (reg) { | |
343 | case LM85_REG_FAN(0): /* Read WORD data */ | |
344 | case LM85_REG_FAN(1): | |
345 | case LM85_REG_FAN(2): | |
346 | case LM85_REG_FAN(3): | |
347 | case LM85_REG_FAN_MIN(0): | |
348 | case LM85_REG_FAN_MIN(1): | |
349 | case LM85_REG_FAN_MIN(2): | |
350 | case LM85_REG_FAN_MIN(3): | |
351 | case LM85_REG_ALARM1: /* Read both bytes at once */ | |
352 | res = i2c_smbus_read_byte_data(client, reg) & 0xff; | |
353 | res |= i2c_smbus_read_byte_data(client, reg + 1) << 8; | |
354 | break; | |
355 | default: /* Read BYTE data */ | |
356 | res = i2c_smbus_read_byte_data(client, reg); | |
357 | break; | |
358 | } | |
1da177e4 | 359 | |
6fd5dd58 AL |
360 | return res; |
361 | } | |
1da177e4 | 362 | |
6fd5dd58 AL |
363 | static void lm85_write_value(struct i2c_client *client, u8 reg, int value) |
364 | { | |
365 | switch (reg) { | |
366 | case LM85_REG_FAN(0): /* Write WORD data */ | |
367 | case LM85_REG_FAN(1): | |
368 | case LM85_REG_FAN(2): | |
369 | case LM85_REG_FAN(3): | |
370 | case LM85_REG_FAN_MIN(0): | |
371 | case LM85_REG_FAN_MIN(1): | |
372 | case LM85_REG_FAN_MIN(2): | |
373 | case LM85_REG_FAN_MIN(3): | |
374 | /* NOTE: ALARM is read only, so not included here */ | |
375 | i2c_smbus_write_byte_data(client, reg, value & 0xff); | |
376 | i2c_smbus_write_byte_data(client, reg + 1, value >> 8); | |
377 | break; | |
378 | default: /* Write BYTE data */ | |
379 | i2c_smbus_write_byte_data(client, reg, value); | |
380 | break; | |
381 | } | |
382 | } | |
67712d01 | 383 | |
6fd5dd58 AL |
384 | static struct lm85_data *lm85_update_device(struct device *dev) |
385 | { | |
746f6884 AL |
386 | struct lm85_data *data = dev_get_drvdata(dev); |
387 | struct i2c_client *client = data->client; | |
6fd5dd58 AL |
388 | int i; |
389 | ||
390 | mutex_lock(&data->update_lock); | |
391 | ||
392 | if (!data->valid || | |
393 | time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) { | |
394 | /* Things that change quickly */ | |
395 | dev_dbg(&client->dev, "Reading sensor values\n"); | |
396 | ||
397 | /* | |
398 | * Have to read extended bits first to "freeze" the | |
399 | * more significant bits that are read later. | |
400 | * There are 2 additional resolution bits per channel and we | |
401 | * have room for 4, so we shift them to the left. | |
402 | */ | |
403 | if (data->type == adm1027 || data->type == adt7463 || | |
404 | data->type == adt7468) { | |
405 | int ext1 = lm85_read_value(client, | |
406 | ADM1027_REG_EXTEND_ADC1); | |
407 | int ext2 = lm85_read_value(client, | |
408 | ADM1027_REG_EXTEND_ADC2); | |
409 | int val = (ext1 << 8) + ext2; | |
410 | ||
411 | for (i = 0; i <= 4; i++) | |
412 | data->in_ext[i] = | |
413 | ((val >> (i * 2)) & 0x03) << 2; | |
414 | ||
415 | for (i = 0; i <= 2; i++) | |
416 | data->temp_ext[i] = | |
417 | (val >> ((i + 4) * 2)) & 0x0c; | |
418 | } | |
419 | ||
420 | data->vid = lm85_read_value(client, LM85_REG_VID); | |
421 | ||
422 | for (i = 0; i <= 3; ++i) { | |
423 | data->in[i] = | |
424 | lm85_read_value(client, LM85_REG_IN(i)); | |
425 | data->fan[i] = | |
426 | lm85_read_value(client, LM85_REG_FAN(i)); | |
427 | } | |
428 | ||
429 | if (!data->has_vid5) | |
430 | data->in[4] = lm85_read_value(client, LM85_REG_IN(4)); | |
431 | ||
432 | if (data->type == adt7468) | |
433 | data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5); | |
434 | ||
435 | for (i = 0; i <= 2; ++i) { | |
436 | data->temp[i] = | |
437 | lm85_read_value(client, LM85_REG_TEMP(i)); | |
438 | data->pwm[i] = | |
439 | lm85_read_value(client, LM85_REG_PWM(i)); | |
1da177e4 | 440 | |
6fd5dd58 AL |
441 | if (IS_ADT7468_OFF64(data)) |
442 | data->temp[i] -= 64; | |
443 | } | |
444 | ||
445 | data->alarms = lm85_read_value(client, LM85_REG_ALARM1); | |
446 | ||
447 | if (data->type == emc6d100) { | |
448 | /* Three more voltage sensors */ | |
449 | for (i = 5; i <= 7; ++i) { | |
450 | data->in[i] = lm85_read_value(client, | |
451 | EMC6D100_REG_IN(i)); | |
452 | } | |
453 | /* More alarm bits */ | |
454 | data->alarms |= lm85_read_value(client, | |
455 | EMC6D100_REG_ALARM3) << 16; | |
456 | } else if (data->type == emc6d102 || data->type == emc6d103 || | |
457 | data->type == emc6d103s) { | |
458 | /* | |
459 | * Have to read LSB bits after the MSB ones because | |
460 | * the reading of the MSB bits has frozen the | |
461 | * LSBs (backward from the ADM1027). | |
462 | */ | |
463 | int ext1 = lm85_read_value(client, | |
464 | EMC6D102_REG_EXTEND_ADC1); | |
465 | int ext2 = lm85_read_value(client, | |
466 | EMC6D102_REG_EXTEND_ADC2); | |
467 | int ext3 = lm85_read_value(client, | |
468 | EMC6D102_REG_EXTEND_ADC3); | |
469 | int ext4 = lm85_read_value(client, | |
470 | EMC6D102_REG_EXTEND_ADC4); | |
471 | data->in_ext[0] = ext3 & 0x0f; | |
472 | data->in_ext[1] = ext4 & 0x0f; | |
473 | data->in_ext[2] = ext4 >> 4; | |
474 | data->in_ext[3] = ext3 >> 4; | |
475 | data->in_ext[4] = ext2 >> 4; | |
476 | ||
477 | data->temp_ext[0] = ext1 & 0x0f; | |
478 | data->temp_ext[1] = ext2 & 0x0f; | |
479 | data->temp_ext[2] = ext1 >> 4; | |
480 | } | |
481 | ||
482 | data->last_reading = jiffies; | |
483 | } /* last_reading */ | |
484 | ||
485 | if (!data->valid || | |
486 | time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) { | |
487 | /* Things that don't change often */ | |
488 | dev_dbg(&client->dev, "Reading config values\n"); | |
489 | ||
490 | for (i = 0; i <= 3; ++i) { | |
491 | data->in_min[i] = | |
492 | lm85_read_value(client, LM85_REG_IN_MIN(i)); | |
493 | data->in_max[i] = | |
494 | lm85_read_value(client, LM85_REG_IN_MAX(i)); | |
495 | data->fan_min[i] = | |
496 | lm85_read_value(client, LM85_REG_FAN_MIN(i)); | |
497 | } | |
498 | ||
499 | if (!data->has_vid5) { | |
500 | data->in_min[4] = lm85_read_value(client, | |
501 | LM85_REG_IN_MIN(4)); | |
502 | data->in_max[4] = lm85_read_value(client, | |
503 | LM85_REG_IN_MAX(4)); | |
504 | } | |
505 | ||
506 | if (data->type == emc6d100) { | |
507 | for (i = 5; i <= 7; ++i) { | |
508 | data->in_min[i] = lm85_read_value(client, | |
509 | EMC6D100_REG_IN_MIN(i)); | |
510 | data->in_max[i] = lm85_read_value(client, | |
511 | EMC6D100_REG_IN_MAX(i)); | |
512 | } | |
513 | } | |
514 | ||
515 | for (i = 0; i <= 2; ++i) { | |
516 | int val; | |
517 | ||
518 | data->temp_min[i] = | |
519 | lm85_read_value(client, LM85_REG_TEMP_MIN(i)); | |
520 | data->temp_max[i] = | |
521 | lm85_read_value(client, LM85_REG_TEMP_MAX(i)); | |
522 | ||
523 | data->autofan[i].config = | |
524 | lm85_read_value(client, LM85_REG_AFAN_CONFIG(i)); | |
525 | val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i)); | |
526 | data->pwm_freq[i] = val & 0x07; | |
527 | data->zone[i].range = val >> 4; | |
528 | data->autofan[i].min_pwm = | |
529 | lm85_read_value(client, LM85_REG_AFAN_MINPWM(i)); | |
530 | data->zone[i].limit = | |
531 | lm85_read_value(client, LM85_REG_AFAN_LIMIT(i)); | |
532 | data->zone[i].critical = | |
533 | lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i)); | |
534 | ||
535 | if (IS_ADT7468_OFF64(data)) { | |
536 | data->temp_min[i] -= 64; | |
537 | data->temp_max[i] -= 64; | |
538 | data->zone[i].limit -= 64; | |
539 | data->zone[i].critical -= 64; | |
540 | } | |
541 | } | |
542 | ||
543 | if (data->type != emc6d103s) { | |
544 | i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1); | |
545 | data->autofan[0].min_off = (i & 0x20) != 0; | |
546 | data->autofan[1].min_off = (i & 0x40) != 0; | |
547 | data->autofan[2].min_off = (i & 0x80) != 0; | |
548 | ||
549 | i = lm85_read_value(client, LM85_REG_AFAN_HYST1); | |
550 | data->zone[0].hyst = i >> 4; | |
551 | data->zone[1].hyst = i & 0x0f; | |
552 | ||
553 | i = lm85_read_value(client, LM85_REG_AFAN_HYST2); | |
554 | data->zone[2].hyst = i >> 4; | |
555 | } | |
556 | ||
557 | data->last_config = jiffies; | |
558 | } /* last_config */ | |
559 | ||
560 | data->valid = 1; | |
561 | ||
562 | mutex_unlock(&data->update_lock); | |
563 | ||
564 | return data; | |
565 | } | |
1da177e4 LT |
566 | |
567 | /* 4 Fans */ | |
b353a487 JD |
568 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
569 | char *buf) | |
1da177e4 | 570 | { |
b353a487 | 571 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 572 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 573 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr])); |
1da177e4 | 574 | } |
b353a487 JD |
575 | |
576 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, | |
577 | char *buf) | |
1da177e4 | 578 | { |
b353a487 | 579 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 580 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 581 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr])); |
1da177e4 | 582 | } |
b353a487 JD |
583 | |
584 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, | |
585 | const char *buf, size_t count) | |
1da177e4 | 586 | { |
b353a487 | 587 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
588 | struct lm85_data *data = dev_get_drvdata(dev); |
589 | struct i2c_client *client = data->client; | |
09770b26 GR |
590 | unsigned long val; |
591 | int err; | |
592 | ||
593 | err = kstrtoul(buf, 10, &val); | |
594 | if (err) | |
595 | return err; | |
1da177e4 | 596 | |
9a61bf63 | 597 | mutex_lock(&data->update_lock); |
1da177e4 LT |
598 | data->fan_min[nr] = FAN_TO_REG(val); |
599 | lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]); | |
9a61bf63 | 600 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
601 | return count; |
602 | } | |
603 | ||
604 | #define show_fan_offset(offset) \ | |
b353a487 JD |
605 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
606 | show_fan, NULL, offset - 1); \ | |
607 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
608 | show_fan_min, set_fan_min, offset - 1) | |
1da177e4 LT |
609 | |
610 | show_fan_offset(1); | |
611 | show_fan_offset(2); | |
612 | show_fan_offset(3); | |
613 | show_fan_offset(4); | |
614 | ||
615 | /* vid, vrm, alarms */ | |
616 | ||
1f44809a JD |
617 | static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, |
618 | char *buf) | |
1da177e4 LT |
619 | { |
620 | struct lm85_data *data = lm85_update_device(dev); | |
9c516ef4 JD |
621 | int vid; |
622 | ||
de248805 | 623 | if (data->has_vid5) { |
9c516ef4 JD |
624 | /* 6-pin VID (VRM 10) */ |
625 | vid = vid_from_reg(data->vid & 0x3f, data->vrm); | |
626 | } else { | |
627 | /* 5-pin VID (VRM 9) */ | |
628 | vid = vid_from_reg(data->vid & 0x1f, data->vrm); | |
629 | } | |
630 | ||
631 | return sprintf(buf, "%d\n", vid); | |
1da177e4 LT |
632 | } |
633 | ||
634 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL); | |
635 | ||
1f44809a JD |
636 | static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, |
637 | char *buf) | |
1da177e4 | 638 | { |
90d6619a | 639 | struct lm85_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
640 | return sprintf(buf, "%ld\n", (long) data->vrm); |
641 | } | |
642 | ||
1f44809a JD |
643 | static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, |
644 | const char *buf, size_t count) | |
1da177e4 | 645 | { |
8f74efe8 | 646 | struct lm85_data *data = dev_get_drvdata(dev); |
09770b26 GR |
647 | unsigned long val; |
648 | int err; | |
649 | ||
650 | err = kstrtoul(buf, 10, &val); | |
651 | if (err) | |
652 | return err; | |
653 | ||
3248c3b7 GR |
654 | if (val > 255) |
655 | return -EINVAL; | |
656 | ||
09770b26 | 657 | data->vrm = val; |
1da177e4 LT |
658 | return count; |
659 | } | |
660 | ||
661 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); | |
662 | ||
1f44809a JD |
663 | static ssize_t show_alarms_reg(struct device *dev, struct device_attribute |
664 | *attr, char *buf) | |
1da177e4 LT |
665 | { |
666 | struct lm85_data *data = lm85_update_device(dev); | |
68188ba7 | 667 | return sprintf(buf, "%u\n", data->alarms); |
1da177e4 LT |
668 | } |
669 | ||
670 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL); | |
671 | ||
bf76e9d3 JD |
672 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
673 | char *buf) | |
674 | { | |
675 | int nr = to_sensor_dev_attr(attr)->index; | |
676 | struct lm85_data *data = lm85_update_device(dev); | |
677 | return sprintf(buf, "%u\n", (data->alarms >> nr) & 1); | |
678 | } | |
679 | ||
680 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); | |
681 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); | |
682 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); | |
683 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); | |
684 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); | |
685 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18); | |
686 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16); | |
687 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17); | |
688 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); | |
689 | static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14); | |
690 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); | |
691 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6); | |
692 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15); | |
693 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10); | |
694 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11); | |
695 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12); | |
696 | static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13); | |
697 | ||
1da177e4 LT |
698 | /* pwm */ |
699 | ||
b353a487 JD |
700 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
701 | char *buf) | |
1da177e4 | 702 | { |
b353a487 | 703 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 704 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 705 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr])); |
1da177e4 | 706 | } |
b353a487 JD |
707 | |
708 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, | |
709 | const char *buf, size_t count) | |
1da177e4 | 710 | { |
b353a487 | 711 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
712 | struct lm85_data *data = dev_get_drvdata(dev); |
713 | struct i2c_client *client = data->client; | |
09770b26 GR |
714 | unsigned long val; |
715 | int err; | |
716 | ||
717 | err = kstrtoul(buf, 10, &val); | |
718 | if (err) | |
719 | return err; | |
1da177e4 | 720 | |
9a61bf63 | 721 | mutex_lock(&data->update_lock); |
1da177e4 LT |
722 | data->pwm[nr] = PWM_TO_REG(val); |
723 | lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]); | |
9a61bf63 | 724 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
725 | return count; |
726 | } | |
b353a487 JD |
727 | |
728 | static ssize_t show_pwm_enable(struct device *dev, struct device_attribute | |
729 | *attr, char *buf) | |
1da177e4 | 730 | { |
b353a487 | 731 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 732 | struct lm85_data *data = lm85_update_device(dev); |
4b4df95d | 733 | int pwm_zone, enable; |
1da177e4 LT |
734 | |
735 | pwm_zone = ZONE_FROM_REG(data->autofan[nr].config); | |
4b4df95d JD |
736 | switch (pwm_zone) { |
737 | case -1: /* PWM is always at 100% */ | |
738 | enable = 0; | |
739 | break; | |
740 | case 0: /* PWM is always at 0% */ | |
741 | case -2: /* PWM responds to manual control */ | |
742 | enable = 1; | |
743 | break; | |
744 | default: /* PWM in automatic mode */ | |
745 | enable = 2; | |
746 | } | |
747 | return sprintf(buf, "%d\n", enable); | |
1da177e4 LT |
748 | } |
749 | ||
455f791e JD |
750 | static ssize_t set_pwm_enable(struct device *dev, struct device_attribute |
751 | *attr, const char *buf, size_t count) | |
752 | { | |
753 | int nr = to_sensor_dev_attr(attr)->index; | |
746f6884 AL |
754 | struct lm85_data *data = dev_get_drvdata(dev); |
755 | struct i2c_client *client = data->client; | |
455f791e | 756 | u8 config; |
09770b26 GR |
757 | unsigned long val; |
758 | int err; | |
759 | ||
760 | err = kstrtoul(buf, 10, &val); | |
761 | if (err) | |
762 | return err; | |
455f791e JD |
763 | |
764 | switch (val) { | |
765 | case 0: | |
766 | config = 3; | |
767 | break; | |
768 | case 1: | |
769 | config = 7; | |
770 | break; | |
771 | case 2: | |
09770b26 GR |
772 | /* |
773 | * Here we have to choose arbitrarily one of the 5 possible | |
774 | * configurations; I go for the safest | |
775 | */ | |
455f791e JD |
776 | config = 6; |
777 | break; | |
778 | default: | |
779 | return -EINVAL; | |
780 | } | |
781 | ||
782 | mutex_lock(&data->update_lock); | |
783 | data->autofan[nr].config = lm85_read_value(client, | |
784 | LM85_REG_AFAN_CONFIG(nr)); | |
785 | data->autofan[nr].config = (data->autofan[nr].config & ~0xe0) | |
786 | | (config << 5); | |
787 | lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr), | |
788 | data->autofan[nr].config); | |
789 | mutex_unlock(&data->update_lock); | |
790 | return count; | |
791 | } | |
792 | ||
34e7dc6c JD |
793 | static ssize_t show_pwm_freq(struct device *dev, |
794 | struct device_attribute *attr, char *buf) | |
795 | { | |
796 | int nr = to_sensor_dev_attr(attr)->index; | |
797 | struct lm85_data *data = lm85_update_device(dev); | |
f6c61cff JD |
798 | int freq; |
799 | ||
800 | if (IS_ADT7468_HFPWM(data)) | |
801 | freq = 22500; | |
802 | else | |
803 | freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]); | |
804 | ||
805 | return sprintf(buf, "%d\n", freq); | |
34e7dc6c JD |
806 | } |
807 | ||
808 | static ssize_t set_pwm_freq(struct device *dev, | |
809 | struct device_attribute *attr, const char *buf, size_t count) | |
810 | { | |
811 | int nr = to_sensor_dev_attr(attr)->index; | |
746f6884 AL |
812 | struct lm85_data *data = dev_get_drvdata(dev); |
813 | struct i2c_client *client = data->client; | |
09770b26 GR |
814 | unsigned long val; |
815 | int err; | |
816 | ||
817 | err = kstrtoul(buf, 10, &val); | |
818 | if (err) | |
819 | return err; | |
34e7dc6c JD |
820 | |
821 | mutex_lock(&data->update_lock); | |
09770b26 GR |
822 | /* |
823 | * The ADT7468 has a special high-frequency PWM output mode, | |
f6c61cff | 824 | * where all PWM outputs are driven by a 22.5 kHz clock. |
09770b26 GR |
825 | * This might confuse the user, but there's not much we can do. |
826 | */ | |
f6c61cff JD |
827 | if (data->type == adt7468 && val >= 11300) { /* High freq. mode */ |
828 | data->cfg5 &= ~ADT7468_HFPWM; | |
829 | lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5); | |
830 | } else { /* Low freq. mode */ | |
831 | data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val); | |
832 | lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), | |
833 | (data->zone[nr].range << 4) | |
834 | | data->pwm_freq[nr]); | |
835 | if (data->type == adt7468) { | |
836 | data->cfg5 |= ADT7468_HFPWM; | |
837 | lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5); | |
838 | } | |
839 | } | |
34e7dc6c JD |
840 | mutex_unlock(&data->update_lock); |
841 | return count; | |
842 | } | |
843 | ||
1da177e4 | 844 | #define show_pwm_reg(offset) \ |
b353a487 JD |
845 | static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ |
846 | show_pwm, set_pwm, offset - 1); \ | |
455f791e | 847 | static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \ |
34e7dc6c JD |
848 | show_pwm_enable, set_pwm_enable, offset - 1); \ |
849 | static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \ | |
850 | show_pwm_freq, set_pwm_freq, offset - 1) | |
1da177e4 LT |
851 | |
852 | show_pwm_reg(1); | |
853 | show_pwm_reg(2); | |
854 | show_pwm_reg(3); | |
855 | ||
856 | /* Voltages */ | |
857 | ||
b353a487 JD |
858 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
859 | char *buf) | |
1da177e4 | 860 | { |
b353a487 | 861 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 862 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a JD |
863 | return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr], |
864 | data->in_ext[nr])); | |
1da177e4 | 865 | } |
b353a487 | 866 | |
1f44809a | 867 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
b353a487 | 868 | char *buf) |
1da177e4 | 869 | { |
b353a487 | 870 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 871 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 872 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr])); |
1da177e4 | 873 | } |
b353a487 JD |
874 | |
875 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, | |
876 | const char *buf, size_t count) | |
1da177e4 | 877 | { |
b353a487 | 878 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
879 | struct lm85_data *data = dev_get_drvdata(dev); |
880 | struct i2c_client *client = data->client; | |
09770b26 GR |
881 | long val; |
882 | int err; | |
883 | ||
884 | err = kstrtol(buf, 10, &val); | |
885 | if (err) | |
886 | return err; | |
1da177e4 | 887 | |
9a61bf63 | 888 | mutex_lock(&data->update_lock); |
1da177e4 LT |
889 | data->in_min[nr] = INS_TO_REG(nr, val); |
890 | lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]); | |
9a61bf63 | 891 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
892 | return count; |
893 | } | |
b353a487 JD |
894 | |
895 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, | |
896 | char *buf) | |
1da177e4 | 897 | { |
b353a487 | 898 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 899 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 900 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr])); |
1da177e4 | 901 | } |
b353a487 JD |
902 | |
903 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, | |
904 | const char *buf, size_t count) | |
1da177e4 | 905 | { |
b353a487 | 906 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
907 | struct lm85_data *data = dev_get_drvdata(dev); |
908 | struct i2c_client *client = data->client; | |
09770b26 GR |
909 | long val; |
910 | int err; | |
911 | ||
912 | err = kstrtol(buf, 10, &val); | |
913 | if (err) | |
914 | return err; | |
1da177e4 | 915 | |
9a61bf63 | 916 | mutex_lock(&data->update_lock); |
1da177e4 LT |
917 | data->in_max[nr] = INS_TO_REG(nr, val); |
918 | lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]); | |
9a61bf63 | 919 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
920 | return count; |
921 | } | |
b353a487 | 922 | |
1da177e4 | 923 | #define show_in_reg(offset) \ |
b353a487 JD |
924 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
925 | show_in, NULL, offset); \ | |
926 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ | |
927 | show_in_min, set_in_min, offset); \ | |
928 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
929 | show_in_max, set_in_max, offset) | |
1da177e4 LT |
930 | |
931 | show_in_reg(0); | |
932 | show_in_reg(1); | |
933 | show_in_reg(2); | |
934 | show_in_reg(3); | |
935 | show_in_reg(4); | |
6b9aad2d JD |
936 | show_in_reg(5); |
937 | show_in_reg(6); | |
938 | show_in_reg(7); | |
1da177e4 LT |
939 | |
940 | /* Temps */ | |
941 | ||
b353a487 JD |
942 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
943 | char *buf) | |
1da177e4 | 944 | { |
b353a487 | 945 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 946 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a JD |
947 | return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr], |
948 | data->temp_ext[nr])); | |
1da177e4 | 949 | } |
b353a487 JD |
950 | |
951 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, | |
952 | char *buf) | |
1da177e4 | 953 | { |
b353a487 | 954 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 955 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 956 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr])); |
1da177e4 | 957 | } |
b353a487 JD |
958 | |
959 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, | |
960 | const char *buf, size_t count) | |
1da177e4 | 961 | { |
b353a487 | 962 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
963 | struct lm85_data *data = dev_get_drvdata(dev); |
964 | struct i2c_client *client = data->client; | |
09770b26 GR |
965 | long val; |
966 | int err; | |
967 | ||
968 | err = kstrtol(buf, 10, &val); | |
969 | if (err) | |
970 | return err; | |
1da177e4 | 971 | |
79b92f2b DW |
972 | if (IS_ADT7468_OFF64(data)) |
973 | val += 64; | |
974 | ||
9a61bf63 | 975 | mutex_lock(&data->update_lock); |
1da177e4 LT |
976 | data->temp_min[nr] = TEMP_TO_REG(val); |
977 | lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]); | |
9a61bf63 | 978 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
979 | return count; |
980 | } | |
b353a487 JD |
981 | |
982 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, | |
983 | char *buf) | |
1da177e4 | 984 | { |
b353a487 | 985 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 986 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 987 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr])); |
1da177e4 | 988 | } |
b353a487 JD |
989 | |
990 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, | |
991 | const char *buf, size_t count) | |
1da177e4 | 992 | { |
b353a487 | 993 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
994 | struct lm85_data *data = dev_get_drvdata(dev); |
995 | struct i2c_client *client = data->client; | |
09770b26 GR |
996 | long val; |
997 | int err; | |
998 | ||
999 | err = kstrtol(buf, 10, &val); | |
1000 | if (err) | |
1001 | return err; | |
1da177e4 | 1002 | |
79b92f2b DW |
1003 | if (IS_ADT7468_OFF64(data)) |
1004 | val += 64; | |
1005 | ||
9a61bf63 | 1006 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1007 | data->temp_max[nr] = TEMP_TO_REG(val); |
1008 | lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]); | |
9a61bf63 | 1009 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1010 | return count; |
1011 | } | |
b353a487 | 1012 | |
1da177e4 | 1013 | #define show_temp_reg(offset) \ |
b353a487 JD |
1014 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
1015 | show_temp, NULL, offset - 1); \ | |
1016 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ | |
1017 | show_temp_min, set_temp_min, offset - 1); \ | |
1018 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ | |
1019 | show_temp_max, set_temp_max, offset - 1); | |
1da177e4 LT |
1020 | |
1021 | show_temp_reg(1); | |
1022 | show_temp_reg(2); | |
1023 | show_temp_reg(3); | |
1024 | ||
1025 | ||
1026 | /* Automatic PWM control */ | |
1027 | ||
b353a487 JD |
1028 | static ssize_t show_pwm_auto_channels(struct device *dev, |
1029 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1030 | { |
b353a487 | 1031 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 1032 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 1033 | return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config)); |
1da177e4 | 1034 | } |
b353a487 JD |
1035 | |
1036 | static ssize_t set_pwm_auto_channels(struct device *dev, | |
1037 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 1038 | { |
b353a487 | 1039 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
1040 | struct lm85_data *data = dev_get_drvdata(dev); |
1041 | struct i2c_client *client = data->client; | |
09770b26 GR |
1042 | long val; |
1043 | int err; | |
1044 | ||
1045 | err = kstrtol(buf, 10, &val); | |
1046 | if (err) | |
1047 | return err; | |
1da177e4 | 1048 | |
9a61bf63 | 1049 | mutex_lock(&data->update_lock); |
1da177e4 | 1050 | data->autofan[nr].config = (data->autofan[nr].config & (~0xe0)) |
1f44809a | 1051 | | ZONE_TO_REG(val); |
1da177e4 LT |
1052 | lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr), |
1053 | data->autofan[nr].config); | |
9a61bf63 | 1054 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1055 | return count; |
1056 | } | |
b353a487 JD |
1057 | |
1058 | static ssize_t show_pwm_auto_pwm_min(struct device *dev, | |
1059 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1060 | { |
b353a487 | 1061 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 1062 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 1063 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm)); |
1da177e4 | 1064 | } |
b353a487 JD |
1065 | |
1066 | static ssize_t set_pwm_auto_pwm_min(struct device *dev, | |
1067 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 1068 | { |
b353a487 | 1069 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
1070 | struct lm85_data *data = dev_get_drvdata(dev); |
1071 | struct i2c_client *client = data->client; | |
09770b26 GR |
1072 | unsigned long val; |
1073 | int err; | |
1074 | ||
1075 | err = kstrtoul(buf, 10, &val); | |
1076 | if (err) | |
1077 | return err; | |
1da177e4 | 1078 | |
9a61bf63 | 1079 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1080 | data->autofan[nr].min_pwm = PWM_TO_REG(val); |
1081 | lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr), | |
1082 | data->autofan[nr].min_pwm); | |
9a61bf63 | 1083 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1084 | return count; |
1085 | } | |
b353a487 JD |
1086 | |
1087 | static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, | |
1088 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1089 | { |
b353a487 | 1090 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 1091 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 1092 | return sprintf(buf, "%d\n", data->autofan[nr].min_off); |
1da177e4 | 1093 | } |
b353a487 JD |
1094 | |
1095 | static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, | |
1096 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 1097 | { |
b353a487 | 1098 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
1099 | struct lm85_data *data = dev_get_drvdata(dev); |
1100 | struct i2c_client *client = data->client; | |
7133e56f | 1101 | u8 tmp; |
09770b26 GR |
1102 | long val; |
1103 | int err; | |
1104 | ||
1105 | err = kstrtol(buf, 10, &val); | |
1106 | if (err) | |
1107 | return err; | |
1da177e4 | 1108 | |
9a61bf63 | 1109 | mutex_lock(&data->update_lock); |
1da177e4 | 1110 | data->autofan[nr].min_off = val; |
7133e56f JD |
1111 | tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1); |
1112 | tmp &= ~(0x20 << nr); | |
1113 | if (data->autofan[nr].min_off) | |
1114 | tmp |= 0x20 << nr; | |
1115 | lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp); | |
9a61bf63 | 1116 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1117 | return count; |
1118 | } | |
b353a487 | 1119 | |
1da177e4 | 1120 | #define pwm_auto(offset) \ |
b353a487 JD |
1121 | static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \ |
1122 | S_IRUGO | S_IWUSR, show_pwm_auto_channels, \ | |
1123 | set_pwm_auto_channels, offset - 1); \ | |
1124 | static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \ | |
1125 | S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \ | |
1126 | set_pwm_auto_pwm_min, offset - 1); \ | |
1127 | static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \ | |
1128 | S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \ | |
34e7dc6c | 1129 | set_pwm_auto_pwm_minctl, offset - 1) |
b353a487 | 1130 | |
1da177e4 LT |
1131 | pwm_auto(1); |
1132 | pwm_auto(2); | |
1133 | pwm_auto(3); | |
1134 | ||
1135 | /* Temperature settings for automatic PWM control */ | |
1136 | ||
b353a487 JD |
1137 | static ssize_t show_temp_auto_temp_off(struct device *dev, |
1138 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1139 | { |
b353a487 | 1140 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 1141 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 1142 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) - |
1da177e4 LT |
1143 | HYST_FROM_REG(data->zone[nr].hyst)); |
1144 | } | |
b353a487 JD |
1145 | |
1146 | static ssize_t set_temp_auto_temp_off(struct device *dev, | |
1147 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 1148 | { |
b353a487 | 1149 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
1150 | struct lm85_data *data = dev_get_drvdata(dev); |
1151 | struct i2c_client *client = data->client; | |
1da177e4 | 1152 | int min; |
09770b26 GR |
1153 | long val; |
1154 | int err; | |
1155 | ||
1156 | err = kstrtol(buf, 10, &val); | |
1157 | if (err) | |
1158 | return err; | |
1da177e4 | 1159 | |
9a61bf63 | 1160 | mutex_lock(&data->update_lock); |
1da177e4 | 1161 | min = TEMP_FROM_REG(data->zone[nr].limit); |
1da177e4 | 1162 | data->zone[nr].hyst = HYST_TO_REG(min - val); |
1f44809a | 1163 | if (nr == 0 || nr == 1) { |
1da177e4 LT |
1164 | lm85_write_value(client, LM85_REG_AFAN_HYST1, |
1165 | (data->zone[0].hyst << 4) | |
1f44809a | 1166 | | data->zone[1].hyst); |
1da177e4 LT |
1167 | } else { |
1168 | lm85_write_value(client, LM85_REG_AFAN_HYST2, | |
1f44809a | 1169 | (data->zone[2].hyst << 4)); |
1da177e4 | 1170 | } |
9a61bf63 | 1171 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1172 | return count; |
1173 | } | |
b353a487 JD |
1174 | |
1175 | static ssize_t show_temp_auto_temp_min(struct device *dev, | |
1176 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1177 | { |
b353a487 | 1178 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 1179 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 1180 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit)); |
1da177e4 | 1181 | } |
b353a487 JD |
1182 | |
1183 | static ssize_t set_temp_auto_temp_min(struct device *dev, | |
1184 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 1185 | { |
b353a487 | 1186 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
1187 | struct lm85_data *data = dev_get_drvdata(dev); |
1188 | struct i2c_client *client = data->client; | |
09770b26 GR |
1189 | long val; |
1190 | int err; | |
1191 | ||
1192 | err = kstrtol(buf, 10, &val); | |
1193 | if (err) | |
1194 | return err; | |
1da177e4 | 1195 | |
9a61bf63 | 1196 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1197 | data->zone[nr].limit = TEMP_TO_REG(val); |
1198 | lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr), | |
1199 | data->zone[nr].limit); | |
1200 | ||
1201 | /* Update temp_auto_max and temp_auto_range */ | |
1202 | data->zone[nr].range = RANGE_TO_REG( | |
1203 | TEMP_FROM_REG(data->zone[nr].max_desired) - | |
1204 | TEMP_FROM_REG(data->zone[nr].limit)); | |
1205 | lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), | |
1206 | ((data->zone[nr].range & 0x0f) << 4) | |
34e7dc6c | 1207 | | (data->pwm_freq[nr] & 0x07)); |
1da177e4 | 1208 | |
9a61bf63 | 1209 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1210 | return count; |
1211 | } | |
b353a487 JD |
1212 | |
1213 | static ssize_t show_temp_auto_temp_max(struct device *dev, | |
1214 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1215 | { |
b353a487 | 1216 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 1217 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 1218 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) + |
1da177e4 LT |
1219 | RANGE_FROM_REG(data->zone[nr].range)); |
1220 | } | |
b353a487 JD |
1221 | |
1222 | static ssize_t set_temp_auto_temp_max(struct device *dev, | |
1223 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 1224 | { |
b353a487 | 1225 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
1226 | struct lm85_data *data = dev_get_drvdata(dev); |
1227 | struct i2c_client *client = data->client; | |
1da177e4 | 1228 | int min; |
09770b26 GR |
1229 | long val; |
1230 | int err; | |
1231 | ||
1232 | err = kstrtol(buf, 10, &val); | |
1233 | if (err) | |
1234 | return err; | |
1da177e4 | 1235 | |
9a61bf63 | 1236 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1237 | min = TEMP_FROM_REG(data->zone[nr].limit); |
1238 | data->zone[nr].max_desired = TEMP_TO_REG(val); | |
1239 | data->zone[nr].range = RANGE_TO_REG( | |
1240 | val - min); | |
1241 | lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), | |
1242 | ((data->zone[nr].range & 0x0f) << 4) | |
34e7dc6c | 1243 | | (data->pwm_freq[nr] & 0x07)); |
9a61bf63 | 1244 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1245 | return count; |
1246 | } | |
b353a487 JD |
1247 | |
1248 | static ssize_t show_temp_auto_temp_crit(struct device *dev, | |
1249 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1250 | { |
b353a487 | 1251 | int nr = to_sensor_dev_attr(attr)->index; |
1da177e4 | 1252 | struct lm85_data *data = lm85_update_device(dev); |
1f44809a | 1253 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical)); |
1da177e4 | 1254 | } |
b353a487 JD |
1255 | |
1256 | static ssize_t set_temp_auto_temp_crit(struct device *dev, | |
1f44809a | 1257 | struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 | 1258 | { |
b353a487 | 1259 | int nr = to_sensor_dev_attr(attr)->index; |
746f6884 AL |
1260 | struct lm85_data *data = dev_get_drvdata(dev); |
1261 | struct i2c_client *client = data->client; | |
09770b26 GR |
1262 | long val; |
1263 | int err; | |
1264 | ||
1265 | err = kstrtol(buf, 10, &val); | |
1266 | if (err) | |
1267 | return err; | |
1da177e4 | 1268 | |
9a61bf63 | 1269 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1270 | data->zone[nr].critical = TEMP_TO_REG(val); |
1271 | lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr), | |
1272 | data->zone[nr].critical); | |
9a61bf63 | 1273 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1274 | return count; |
1275 | } | |
b353a487 | 1276 | |
1da177e4 | 1277 | #define temp_auto(offset) \ |
b353a487 JD |
1278 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \ |
1279 | S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \ | |
1280 | set_temp_auto_temp_off, offset - 1); \ | |
1281 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \ | |
1282 | S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \ | |
1283 | set_temp_auto_temp_min, offset - 1); \ | |
1284 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \ | |
1285 | S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \ | |
1286 | set_temp_auto_temp_max, offset - 1); \ | |
1287 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \ | |
1288 | S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \ | |
1289 | set_temp_auto_temp_crit, offset - 1); | |
1290 | ||
1da177e4 LT |
1291 | temp_auto(1); |
1292 | temp_auto(2); | |
1293 | temp_auto(3); | |
1294 | ||
0501a381 | 1295 | static struct attribute *lm85_attributes[] = { |
b353a487 JD |
1296 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
1297 | &sensor_dev_attr_fan2_input.dev_attr.attr, | |
1298 | &sensor_dev_attr_fan3_input.dev_attr.attr, | |
1299 | &sensor_dev_attr_fan4_input.dev_attr.attr, | |
1300 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
1301 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
1302 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
1303 | &sensor_dev_attr_fan4_min.dev_attr.attr, | |
bf76e9d3 JD |
1304 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
1305 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | |
1306 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, | |
1307 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, | |
b353a487 JD |
1308 | |
1309 | &sensor_dev_attr_pwm1.dev_attr.attr, | |
1310 | &sensor_dev_attr_pwm2.dev_attr.attr, | |
1311 | &sensor_dev_attr_pwm3.dev_attr.attr, | |
1312 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, | |
1313 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, | |
1314 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, | |
34e7dc6c JD |
1315 | &sensor_dev_attr_pwm1_freq.dev_attr.attr, |
1316 | &sensor_dev_attr_pwm2_freq.dev_attr.attr, | |
1317 | &sensor_dev_attr_pwm3_freq.dev_attr.attr, | |
b353a487 JD |
1318 | |
1319 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
1320 | &sensor_dev_attr_in1_input.dev_attr.attr, | |
1321 | &sensor_dev_attr_in2_input.dev_attr.attr, | |
1322 | &sensor_dev_attr_in3_input.dev_attr.attr, | |
1323 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
1324 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
1325 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
1326 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
1327 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
1328 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
1329 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
1330 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
bf76e9d3 JD |
1331 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
1332 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | |
1333 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | |
1334 | &sensor_dev_attr_in3_alarm.dev_attr.attr, | |
b353a487 JD |
1335 | |
1336 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
1337 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
1338 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
1339 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
1340 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
1341 | &sensor_dev_attr_temp3_min.dev_attr.attr, | |
1342 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
1343 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
1344 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
bf76e9d3 JD |
1345 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
1346 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, | |
1347 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, | |
1348 | &sensor_dev_attr_temp1_fault.dev_attr.attr, | |
1349 | &sensor_dev_attr_temp3_fault.dev_attr.attr, | |
b353a487 JD |
1350 | |
1351 | &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr, | |
1352 | &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr, | |
1353 | &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr, | |
1354 | &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr, | |
1355 | &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr, | |
1356 | &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr, | |
b353a487 | 1357 | |
b353a487 JD |
1358 | &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr, |
1359 | &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr, | |
1360 | &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr, | |
1361 | &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr, | |
1362 | &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr, | |
1363 | &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr, | |
1364 | &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr, | |
1365 | &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr, | |
1366 | &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr, | |
1367 | ||
0501a381 MH |
1368 | &dev_attr_vrm.attr, |
1369 | &dev_attr_cpu0_vid.attr, | |
1370 | &dev_attr_alarms.attr, | |
0501a381 MH |
1371 | NULL |
1372 | }; | |
1373 | ||
1374 | static const struct attribute_group lm85_group = { | |
1375 | .attrs = lm85_attributes, | |
1376 | }; | |
1377 | ||
06923f84 GR |
1378 | static struct attribute *lm85_attributes_minctl[] = { |
1379 | &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr, | |
1380 | &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr, | |
1381 | &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr, | |
5f441e22 | 1382 | NULL |
06923f84 GR |
1383 | }; |
1384 | ||
1385 | static const struct attribute_group lm85_group_minctl = { | |
1386 | .attrs = lm85_attributes_minctl, | |
1387 | }; | |
1388 | ||
1389 | static struct attribute *lm85_attributes_temp_off[] = { | |
1390 | &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr, | |
1391 | &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr, | |
1392 | &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr, | |
5f441e22 | 1393 | NULL |
06923f84 GR |
1394 | }; |
1395 | ||
1396 | static const struct attribute_group lm85_group_temp_off = { | |
1397 | .attrs = lm85_attributes_temp_off, | |
1398 | }; | |
1399 | ||
6b9aad2d | 1400 | static struct attribute *lm85_attributes_in4[] = { |
b353a487 JD |
1401 | &sensor_dev_attr_in4_input.dev_attr.attr, |
1402 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
1403 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
bf76e9d3 | 1404 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
0501a381 MH |
1405 | NULL |
1406 | }; | |
1407 | ||
6b9aad2d JD |
1408 | static const struct attribute_group lm85_group_in4 = { |
1409 | .attrs = lm85_attributes_in4, | |
1410 | }; | |
1411 | ||
1412 | static struct attribute *lm85_attributes_in567[] = { | |
1413 | &sensor_dev_attr_in5_input.dev_attr.attr, | |
1414 | &sensor_dev_attr_in6_input.dev_attr.attr, | |
1415 | &sensor_dev_attr_in7_input.dev_attr.attr, | |
1416 | &sensor_dev_attr_in5_min.dev_attr.attr, | |
1417 | &sensor_dev_attr_in6_min.dev_attr.attr, | |
1418 | &sensor_dev_attr_in7_min.dev_attr.attr, | |
1419 | &sensor_dev_attr_in5_max.dev_attr.attr, | |
1420 | &sensor_dev_attr_in6_max.dev_attr.attr, | |
1421 | &sensor_dev_attr_in7_max.dev_attr.attr, | |
bf76e9d3 JD |
1422 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
1423 | &sensor_dev_attr_in6_alarm.dev_attr.attr, | |
1424 | &sensor_dev_attr_in7_alarm.dev_attr.attr, | |
6b9aad2d JD |
1425 | NULL |
1426 | }; | |
1427 | ||
1428 | static const struct attribute_group lm85_group_in567 = { | |
1429 | .attrs = lm85_attributes_in567, | |
0501a381 MH |
1430 | }; |
1431 | ||
5f447594 JD |
1432 | static void lm85_init_client(struct i2c_client *client) |
1433 | { | |
1434 | int value; | |
1435 | ||
1436 | /* Start monitoring if needed */ | |
1437 | value = lm85_read_value(client, LM85_REG_CONFIG); | |
1438 | if (!(value & 0x01)) { | |
1439 | dev_info(&client->dev, "Starting monitoring\n"); | |
1440 | lm85_write_value(client, LM85_REG_CONFIG, value | 0x01); | |
1441 | } | |
1442 | ||
1443 | /* Warn about unusual configuration bits */ | |
1444 | if (value & 0x02) | |
1445 | dev_warn(&client->dev, "Device configuration is locked\n"); | |
1446 | if (!(value & 0x04)) | |
1447 | dev_warn(&client->dev, "Device is not ready\n"); | |
1448 | } | |
1449 | ||
5cfaf338 JD |
1450 | static int lm85_is_fake(struct i2c_client *client) |
1451 | { | |
1452 | /* | |
1453 | * Differenciate between real LM96000 and Winbond WPCD377I. The latter | |
1454 | * emulate the former except that it has no hardware monitoring function | |
1455 | * so the readings are always 0. | |
1456 | */ | |
1457 | int i; | |
1458 | u8 in_temp, fan; | |
1459 | ||
1460 | for (i = 0; i < 8; i++) { | |
1461 | in_temp = i2c_smbus_read_byte_data(client, 0x20 + i); | |
1462 | fan = i2c_smbus_read_byte_data(client, 0x28 + i); | |
1463 | if (in_temp != 0x00 || fan != 0xff) | |
1464 | return 0; | |
1465 | } | |
1466 | ||
1467 | return 1; | |
1468 | } | |
1469 | ||
67712d01 | 1470 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 1471 | static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info) |
1da177e4 | 1472 | { |
67712d01 JD |
1473 | struct i2c_adapter *adapter = client->adapter; |
1474 | int address = client->addr; | |
590e8534 | 1475 | const char *type_name = NULL; |
d42a2eb5 | 1476 | int company, verstep; |
1da177e4 | 1477 | |
e89e22b2 | 1478 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
1da177e4 | 1479 | /* We need to be able to do byte I/O */ |
67712d01 | 1480 | return -ENODEV; |
1f44809a | 1481 | } |
1da177e4 | 1482 | |
d42a2eb5 JD |
1483 | /* Determine the chip type */ |
1484 | company = lm85_read_value(client, LM85_REG_COMPANY); | |
1485 | verstep = lm85_read_value(client, LM85_REG_VERSTEP); | |
1486 | ||
b55f3757 GR |
1487 | dev_dbg(&adapter->dev, |
1488 | "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n", | |
d42a2eb5 JD |
1489 | address, company, verstep); |
1490 | ||
d42a2eb5 JD |
1491 | if (company == LM85_COMPANY_NATIONAL) { |
1492 | switch (verstep) { | |
1493 | case LM85_VERSTEP_LM85C: | |
1494 | type_name = "lm85c"; | |
1495 | break; | |
1496 | case LM85_VERSTEP_LM85B: | |
1497 | type_name = "lm85b"; | |
1498 | break; | |
1499 | case LM85_VERSTEP_LM96000_1: | |
1500 | case LM85_VERSTEP_LM96000_2: | |
1501 | /* Check for Winbond WPCD377I */ | |
1502 | if (lm85_is_fake(client)) { | |
1503 | dev_dbg(&adapter->dev, | |
1504 | "Found Winbond WPCD377I, ignoring\n"); | |
1505 | return -ENODEV; | |
69fc1feb | 1506 | } |
590e8534 | 1507 | type_name = "lm85"; |
d42a2eb5 JD |
1508 | break; |
1509 | } | |
1510 | } else if (company == LM85_COMPANY_ANALOG_DEV) { | |
1511 | switch (verstep) { | |
1512 | case LM85_VERSTEP_ADM1027: | |
1513 | type_name = "adm1027"; | |
1514 | break; | |
1515 | case LM85_VERSTEP_ADT7463: | |
1516 | case LM85_VERSTEP_ADT7463C: | |
1517 | type_name = "adt7463"; | |
1518 | break; | |
1519 | case LM85_VERSTEP_ADT7468_1: | |
1520 | case LM85_VERSTEP_ADT7468_2: | |
1521 | type_name = "adt7468"; | |
1522 | break; | |
1da177e4 | 1523 | } |
d42a2eb5 JD |
1524 | } else if (company == LM85_COMPANY_SMSC) { |
1525 | switch (verstep) { | |
1526 | case LM85_VERSTEP_EMC6D100_A0: | |
1527 | case LM85_VERSTEP_EMC6D100_A1: | |
1528 | /* Note: we can't tell a '100 from a '101 */ | |
1529 | type_name = "emc6d100"; | |
1530 | break; | |
1531 | case LM85_VERSTEP_EMC6D102: | |
1532 | type_name = "emc6d102"; | |
1533 | break; | |
f065a93e JB |
1534 | case LM85_VERSTEP_EMC6D103_A0: |
1535 | case LM85_VERSTEP_EMC6D103_A1: | |
1536 | type_name = "emc6d103"; | |
1537 | break; | |
f065a93e JB |
1538 | case LM85_VERSTEP_EMC6D103S: |
1539 | type_name = "emc6d103s"; | |
1540 | break; | |
d42a2eb5 | 1541 | } |
1da177e4 LT |
1542 | } |
1543 | ||
590e8534 JD |
1544 | if (!type_name) |
1545 | return -ENODEV; | |
1546 | ||
67712d01 JD |
1547 | strlcpy(info->type, type_name, I2C_NAME_SIZE); |
1548 | ||
1549 | return 0; | |
1550 | } | |
1da177e4 | 1551 | |
746f6884 | 1552 | static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id) |
67712d01 | 1553 | { |
746f6884 AL |
1554 | struct device *dev = &client->dev; |
1555 | struct device *hwmon_dev; | |
67712d01 | 1556 | struct lm85_data *data; |
746f6884 | 1557 | int idx = 0; |
67712d01 | 1558 | |
746f6884 | 1559 | data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL); |
67712d01 JD |
1560 | if (!data) |
1561 | return -ENOMEM; | |
1562 | ||
746f6884 | 1563 | data->client = client; |
67712d01 | 1564 | data->type = id->driver_data; |
9a61bf63 | 1565 | mutex_init(&data->update_lock); |
1da177e4 | 1566 | |
67712d01 JD |
1567 | /* Fill in the chip specific driver values */ |
1568 | switch (data->type) { | |
1569 | case adm1027: | |
1570 | case adt7463: | |
fa7a5797 | 1571 | case adt7468: |
67712d01 JD |
1572 | case emc6d100: |
1573 | case emc6d102: | |
f065a93e | 1574 | case emc6d103: |
06923f84 | 1575 | case emc6d103s: |
67712d01 JD |
1576 | data->freq_map = adm1027_freq_map; |
1577 | break; | |
1578 | default: | |
1579 | data->freq_map = lm85_freq_map; | |
1580 | } | |
1da177e4 LT |
1581 | |
1582 | /* Set the VRM version */ | |
303760b4 | 1583 | data->vrm = vid_which_vrm(); |
1da177e4 LT |
1584 | |
1585 | /* Initialize the LM85 chip */ | |
e89e22b2 | 1586 | lm85_init_client(client); |
1da177e4 | 1587 | |
746f6884 AL |
1588 | /* sysfs hooks */ |
1589 | data->groups[idx++] = &lm85_group; | |
1da177e4 | 1590 | |
06923f84 GR |
1591 | /* minctl and temp_off exist on all chips except emc6d103s */ |
1592 | if (data->type != emc6d103s) { | |
746f6884 AL |
1593 | data->groups[idx++] = &lm85_group_minctl; |
1594 | data->groups[idx++] = &lm85_group_temp_off; | |
06923f84 GR |
1595 | } |
1596 | ||
09770b26 GR |
1597 | /* |
1598 | * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used | |
1599 | * as a sixth digital VID input rather than an analog input. | |
1600 | */ | |
de248805 GR |
1601 | if (data->type == adt7463 || data->type == adt7468) { |
1602 | u8 vid = lm85_read_value(client, LM85_REG_VID); | |
1603 | if (vid & 0x80) | |
1604 | data->has_vid5 = true; | |
1605 | } | |
1606 | ||
746f6884 AL |
1607 | if (!data->has_vid5) |
1608 | data->groups[idx++] = &lm85_group_in4; | |
6b9aad2d JD |
1609 | |
1610 | /* The EMC6D100 has 3 additional voltage inputs */ | |
746f6884 AL |
1611 | if (data->type == emc6d100) |
1612 | data->groups[idx++] = &lm85_group_in567; | |
1da177e4 | 1613 | |
746f6884 AL |
1614 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
1615 | data, data->groups); | |
1616 | return PTR_ERR_OR_ZERO(hwmon_dev); | |
1da177e4 LT |
1617 | } |
1618 | ||
6fd5dd58 AL |
1619 | static const struct i2c_device_id lm85_id[] = { |
1620 | { "adm1027", adm1027 }, | |
1621 | { "adt7463", adt7463 }, | |
1622 | { "adt7468", adt7468 }, | |
1623 | { "lm85", lm85 }, | |
1624 | { "lm85b", lm85 }, | |
1625 | { "lm85c", lm85 }, | |
1626 | { "emc6d100", emc6d100 }, | |
1627 | { "emc6d101", emc6d100 }, | |
1628 | { "emc6d102", emc6d102 }, | |
1629 | { "emc6d103", emc6d103 }, | |
1630 | { "emc6d103s", emc6d103s }, | |
1631 | { } | |
1632 | }; | |
1633 | MODULE_DEVICE_TABLE(i2c, lm85_id); | |
1da177e4 | 1634 | |
6fd5dd58 AL |
1635 | static struct i2c_driver lm85_driver = { |
1636 | .class = I2C_CLASS_HWMON, | |
1637 | .driver = { | |
1638 | .name = "lm85", | |
1639 | }, | |
1640 | .probe = lm85_probe, | |
6fd5dd58 AL |
1641 | .id_table = lm85_id, |
1642 | .detect = lm85_detect, | |
1643 | .address_list = normal_i2c, | |
1644 | }; | |
1da177e4 | 1645 | |
f0967eea | 1646 | module_i2c_driver(lm85_driver); |
1da177e4 | 1647 | |
1da177e4 | 1648 | MODULE_LICENSE("GPL"); |
1f44809a JD |
1649 | MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, " |
1650 | "Margit Schubert-While <margitsw@t-online.de>, " | |
e89e22b2 | 1651 | "Justin Thiessen <jthiessen@penguincomputing.com>"); |
1da177e4 | 1652 | MODULE_DESCRIPTION("LM85-B, LM85-C driver"); |