blob: eac24c1361bca358add0f8fe7329ecfffedeb0ec [file] [log] [blame]
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001/*
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 Delvare3d849982009-12-09 20:36:05 +01006 * Copyright (C) 2009 Jean Delvare <khali@linux-fr.org>
7 *
Jordan Crouse1c301fc2009-01-15 22:27:47 +01008 * 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>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/i2c.h>
19#include <linux/hwmon.h>
20#include <linux/hwmon-sysfs.h>
21#include <linux/err.h>
22
23/* Indexes for the sysfs hooks */
24
25#define INPUT 0
26#define MIN 1
27#define MAX 2
28#define CONTROL 3
29#define OFFSET 3
30#define AUTOMIN 4
31#define THERM 5
32#define HYSTERSIS 6
33
34/* These are unique identifiers for the sysfs functions - unlike the
35 numbers above, these are not also indexes into an array
36*/
37
38#define ALARM 9
39#define FAULT 10
40
41/* 7475 Common Registers */
42
Jean Delvared07ca4a2009-12-09 20:36:07 +010043#define REG_DEVREV2 0x12 /* ADT7490 only */
44
Jean Delvare3d849982009-12-09 20:36:05 +010045#define REG_VTT 0x1E /* ADT7490 only */
46#define REG_EXTEND3 0x1F /* ADT7490 only */
47
Jean Delvarecffb9dd2009-12-09 20:36:03 +010048#define REG_VOLTAGE_BASE 0x20
Jordan Crouse1c301fc2009-01-15 22:27:47 +010049#define REG_TEMP_BASE 0x25
50#define REG_TACH_BASE 0x28
51#define REG_PWM_BASE 0x30
52#define REG_PWM_MAX_BASE 0x38
53
54#define REG_DEVID 0x3D
55#define REG_VENDID 0x3E
Jean Delvared656b6f2009-12-09 20:36:04 +010056#define REG_DEVID2 0x3F
Jordan Crouse1c301fc2009-01-15 22:27:47 +010057
58#define REG_STATUS1 0x41
59#define REG_STATUS2 0x42
60
Jean Delvarecffb9dd2009-12-09 20:36:03 +010061#define REG_VOLTAGE_MIN_BASE 0x44
62#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010063
64#define REG_TEMP_MIN_BASE 0x4E
65#define REG_TEMP_MAX_BASE 0x4F
66
67#define REG_TACH_MIN_BASE 0x54
68
69#define REG_PWM_CONFIG_BASE 0x5C
70
71#define REG_TEMP_TRANGE_BASE 0x5F
72
73#define REG_PWM_MIN_BASE 0x64
74
75#define REG_TEMP_TMIN_BASE 0x67
76#define REG_TEMP_THERM_BASE 0x6A
77
78#define REG_REMOTE1_HYSTERSIS 0x6D
79#define REG_REMOTE2_HYSTERSIS 0x6E
80
81#define REG_TEMP_OFFSET_BASE 0x70
82
Jean Delvareebfaf1f2009-12-09 20:36:07 +010083#define REG_CONFIG2 0x73
84
Jordan Crouse1c301fc2009-01-15 22:27:47 +010085#define REG_EXTEND1 0x76
86#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010087
88#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +010089#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +010090#define REG_CONFIG4 0x7D
91
Jean Delvare3d849982009-12-09 20:36:05 +010092#define REG_STATUS4 0x81 /* ADT7490 only */
93
94#define REG_VTT_MIN 0x84 /* ADT7490 only */
95#define REG_VTT_MAX 0x86 /* ADT7490 only */
96
Jean Delvareebfaf1f2009-12-09 20:36:07 +010097#define CONFIG2_ATTN 0x20
98
Jean Delvare378933c2009-12-09 20:36:06 +010099#define CONFIG3_SMBALERT 0x01
100#define CONFIG3_THERM 0x02
101
102#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100103#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100104#define CONFIG4_ATTN_IN10 0x30
105#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100106
107#define CONFIG5_TWOSCOMP 0x01
108#define CONFIG5_TEMPOFFSET 0x02
109
110/* ADT7475 Settings */
111
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100112#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100113#define ADT7475_TEMP_COUNT 3
114#define ADT7475_TACH_COUNT 4
115#define ADT7475_PWM_COUNT 3
116
117/* Macro to read the registers */
118
119#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
120
121/* Macros to easily index the registers */
122
123#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
124#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
125
126#define PWM_REG(idx) (REG_PWM_BASE + (idx))
127#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
128#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
129#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
130
131#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
132#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
133#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
134
135#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
136#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
137#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
138#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
139#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
140#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
141#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
142
Jean Delvareb180d052009-12-09 20:36:02 +0100143static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100144
Jean Delvare3d849982009-12-09 20:36:05 +0100145I2C_CLIENT_INSMOD_3(adt7473, adt7475, adt7490);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100146
147static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100148 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100149 { "adt7475", adt7475 },
Jean Delvare3d849982009-12-09 20:36:05 +0100150 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100151 { }
152};
153MODULE_DEVICE_TABLE(i2c, adt7475_id);
154
155struct adt7475_data {
156 struct device *hwmon_dev;
157 struct mutex lock;
158
159 unsigned long measure_updated;
160 unsigned long limits_updated;
161 char valid;
162
Jean Delvaref99318b2009-12-09 20:36:03 +0100163 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100164 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100165 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100166 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100167 u8 has_pwm2:1;
168 u8 has_fan4:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100169 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100170 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100171 u16 temp[7][3];
172 u16 tach[2][4];
173 u8 pwm[4][3];
174 u8 range[3];
175 u8 pwmctl[3];
176 u8 pwmchan[3];
177};
178
179static struct i2c_driver adt7475_driver;
180static struct adt7475_data *adt7475_update_device(struct device *dev);
181static void adt7475_read_hystersis(struct i2c_client *client);
182static void adt7475_read_pwm(struct i2c_client *client, int index);
183
184/* Given a temp value, convert it to register value */
185
186static inline u16 temp2reg(struct adt7475_data *data, long val)
187{
188 u16 ret;
189
190 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
191 val = SENSORS_LIMIT(val, -64000, 191000);
192 ret = (val + 64500) / 1000;
193 } else {
194 val = SENSORS_LIMIT(val, -128000, 127000);
195 if (val < -500)
196 ret = (256500 + val) / 1000;
197 else
198 ret = (val + 500) / 1000;
199 }
200
201 return ret << 2;
202}
203
204/* Given a register value, convert it to a real temp value */
205
206static inline int reg2temp(struct adt7475_data *data, u16 reg)
207{
208 if (data->config5 & CONFIG5_TWOSCOMP) {
209 if (reg >= 512)
210 return (reg - 1024) * 250;
211 else
212 return reg * 250;
213 } else
214 return (reg - 256) * 250;
215}
216
217static inline int tach2rpm(u16 tach)
218{
219 if (tach == 0 || tach == 0xFFFF)
220 return 0;
221
222 return (90000 * 60) / tach;
223}
224
225static inline u16 rpm2tach(unsigned long rpm)
226{
227 if (rpm == 0)
228 return 0;
229
230 return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF);
231}
232
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100233/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
234static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
235 { 45, 94 }, /* +2.5V */
236 { 175, 525 }, /* Vccp */
237 { 68, 71 }, /* Vcc */
238 { 93, 47 }, /* +5V */
239 { 120, 20 }, /* +12V */
240 { 45, 45 }, /* Vtt */
241};
242
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100243static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100244{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100245 const int *r = adt7473_in_scaling[channel];
246
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100247 if (bypass_attn & (1 << channel))
248 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100249 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100250}
251
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100252static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100253{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100254 const int *r = adt7473_in_scaling[channel];
255 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100256
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100257 if (bypass_attn & (1 << channel))
258 reg = (volt * 1024) / 2250;
259 else
260 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100261 return SENSORS_LIMIT(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100262}
263
264static u16 adt7475_read_word(struct i2c_client *client, int reg)
265{
266 u16 val;
267
268 val = i2c_smbus_read_byte_data(client, reg);
269 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
270
271 return val;
272}
273
274static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
275{
276 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
277 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
278}
279
280/* Find the nearest value in a table - used for pwm frequency and
281 auto temp range */
282static int find_nearest(long val, const int *array, int size)
283{
284 int i;
285
286 if (val < array[0])
287 return 0;
288
289 if (val > array[size - 1])
290 return size - 1;
291
292 for (i = 0; i < size - 1; i++) {
293 int a, b;
294
295 if (val > array[i + 1])
296 continue;
297
298 a = val - array[i];
299 b = array[i + 1] - val;
300
301 return (a <= b) ? i : i + 1;
302 }
303
304 return 0;
305}
306
307static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
308 char *buf)
309{
310 struct adt7475_data *data = adt7475_update_device(dev);
311 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
312 unsigned short val;
313
314 switch (sattr->nr) {
315 case ALARM:
316 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100317 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100318 default:
319 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100320 return sprintf(buf, "%d\n",
321 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100322 }
323}
324
325static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
326 const char *buf, size_t count)
327{
328
329 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
330 struct i2c_client *client = to_i2c_client(dev);
331 struct adt7475_data *data = i2c_get_clientdata(client);
332 unsigned char reg;
333 long val;
334
335 if (strict_strtol(buf, 10, &val))
336 return -EINVAL;
337
338 mutex_lock(&data->lock);
339
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100340 data->voltage[sattr->nr][sattr->index] =
341 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100342
Jean Delvare3d849982009-12-09 20:36:05 +0100343 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
344 if (sattr->nr == MIN)
345 reg = VOLTAGE_MIN_REG(sattr->index);
346 else
347 reg = VOLTAGE_MAX_REG(sattr->index);
348 } else {
349 if (sattr->nr == MIN)
350 reg = REG_VTT_MIN;
351 else
352 reg = REG_VTT_MAX;
353 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100354
355 i2c_smbus_write_byte_data(client, reg,
356 data->voltage[sattr->nr][sattr->index] >> 2);
357 mutex_unlock(&data->lock);
358
359 return count;
360}
361
362static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
363 char *buf)
364{
365 struct adt7475_data *data = adt7475_update_device(dev);
366 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
367 int out;
368
369 switch (sattr->nr) {
370 case HYSTERSIS:
371 mutex_lock(&data->lock);
372 out = data->temp[sattr->nr][sattr->index];
373 if (sattr->index != 1)
374 out = (out >> 4) & 0xF;
375 else
376 out = (out & 0xF);
377 /* Show the value as an absolute number tied to
378 * THERM */
379 out = reg2temp(data, data->temp[THERM][sattr->index]) -
380 out * 1000;
381 mutex_unlock(&data->lock);
382 break;
383
384 case OFFSET:
385 /* Offset is always 2's complement, regardless of the
386 * setting in CONFIG5 */
387 mutex_lock(&data->lock);
388 out = (s8)data->temp[sattr->nr][sattr->index];
389 if (data->config5 & CONFIG5_TEMPOFFSET)
390 out *= 1000;
391 else
392 out *= 500;
393 mutex_unlock(&data->lock);
394 break;
395
396 case ALARM:
397 out = (data->alarms >> (sattr->index + 4)) & 1;
398 break;
399
400 case FAULT:
401 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100402 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100403 break;
404
405 default:
406 /* All other temp values are in the configured format */
407 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
408 }
409
410 return sprintf(buf, "%d\n", out);
411}
412
413static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
414 const char *buf, size_t count)
415{
416 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
417 struct i2c_client *client = to_i2c_client(dev);
418 struct adt7475_data *data = i2c_get_clientdata(client);
419 unsigned char reg = 0;
420 u8 out;
421 int temp;
422 long val;
423
424 if (strict_strtol(buf, 10, &val))
425 return -EINVAL;
426
427 mutex_lock(&data->lock);
428
429 /* We need the config register in all cases for temp <-> reg conv. */
430 data->config5 = adt7475_read(REG_CONFIG5);
431
432 switch (sattr->nr) {
433 case OFFSET:
434 if (data->config5 & CONFIG5_TEMPOFFSET) {
435 val = SENSORS_LIMIT(val, -63000, 127000);
436 out = data->temp[OFFSET][sattr->index] = val / 1000;
437 } else {
438 val = SENSORS_LIMIT(val, -63000, 64000);
439 out = data->temp[OFFSET][sattr->index] = val / 500;
440 }
441 break;
442
443 case HYSTERSIS:
444 /* The value will be given as an absolute value, turn it
445 into an offset based on THERM */
446
447 /* Read fresh THERM and HYSTERSIS values from the chip */
448 data->temp[THERM][sattr->index] =
449 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
450 adt7475_read_hystersis(client);
451
452 temp = reg2temp(data, data->temp[THERM][sattr->index]);
453 val = SENSORS_LIMIT(val, temp - 15000, temp);
454 val = (temp - val) / 1000;
455
456 if (sattr->index != 1) {
457 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
458 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
459 } else {
460 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
461 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
462 }
463
464 out = data->temp[HYSTERSIS][sattr->index];
465 break;
466
467 default:
468 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
469
470 /* We maintain an extra 2 digits of precision for simplicity
471 * - shift those back off before writing the value */
472 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
473 }
474
475 switch (sattr->nr) {
476 case MIN:
477 reg = TEMP_MIN_REG(sattr->index);
478 break;
479 case MAX:
480 reg = TEMP_MAX_REG(sattr->index);
481 break;
482 case OFFSET:
483 reg = TEMP_OFFSET_REG(sattr->index);
484 break;
485 case AUTOMIN:
486 reg = TEMP_TMIN_REG(sattr->index);
487 break;
488 case THERM:
489 reg = TEMP_THERM_REG(sattr->index);
490 break;
491 case HYSTERSIS:
492 if (sattr->index != 2)
493 reg = REG_REMOTE1_HYSTERSIS;
494 else
495 reg = REG_REMOTE2_HYSTERSIS;
496
497 break;
498 }
499
500 i2c_smbus_write_byte_data(client, reg, out);
501
502 mutex_unlock(&data->lock);
503 return count;
504}
505
506/* Table of autorange values - the user will write the value in millidegrees,
507 and we'll convert it */
508static const int autorange_table[] = {
509 2000, 2500, 3330, 4000, 5000, 6670, 8000,
510 10000, 13330, 16000, 20000, 26670, 32000, 40000,
511 53330, 80000
512};
513
514static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
515 char *buf)
516{
517 struct adt7475_data *data = adt7475_update_device(dev);
518 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
519 int out, val;
520
521 mutex_lock(&data->lock);
522 out = (data->range[sattr->index] >> 4) & 0x0F;
523 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
524 mutex_unlock(&data->lock);
525
526 return sprintf(buf, "%d\n", val + autorange_table[out]);
527}
528
529static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
530 const char *buf, size_t count)
531{
532 struct i2c_client *client = to_i2c_client(dev);
533 struct adt7475_data *data = i2c_get_clientdata(client);
534 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
535 int temp;
536 long val;
537
538 if (strict_strtol(buf, 10, &val))
539 return -EINVAL;
540
541 mutex_lock(&data->lock);
542
543 /* Get a fresh copy of the needed registers */
544 data->config5 = adt7475_read(REG_CONFIG5);
545 data->temp[AUTOMIN][sattr->index] =
546 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
547 data->range[sattr->index] =
548 adt7475_read(TEMP_TRANGE_REG(sattr->index));
549
550 /* The user will write an absolute value, so subtract the start point
551 to figure the range */
552 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
553 val = SENSORS_LIMIT(val, temp + autorange_table[0],
554 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
555 val -= temp;
556
557 /* Find the nearest table entry to what the user wrote */
558 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
559
560 data->range[sattr->index] &= ~0xF0;
561 data->range[sattr->index] |= val << 4;
562
563 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
564 data->range[sattr->index]);
565
566 mutex_unlock(&data->lock);
567 return count;
568}
569
570static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
571 char *buf)
572{
573 struct adt7475_data *data = adt7475_update_device(dev);
574 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
575 int out;
576
577 if (sattr->nr == ALARM)
578 out = (data->alarms >> (sattr->index + 10)) & 1;
579 else
580 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
581
582 return sprintf(buf, "%d\n", out);
583}
584
585static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
586 const char *buf, size_t count)
587{
588
589 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
590 struct i2c_client *client = to_i2c_client(dev);
591 struct adt7475_data *data = i2c_get_clientdata(client);
592 unsigned long val;
593
594 if (strict_strtoul(buf, 10, &val))
595 return -EINVAL;
596
597 mutex_lock(&data->lock);
598
599 data->tach[MIN][sattr->index] = rpm2tach(val);
600
601 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
602 data->tach[MIN][sattr->index]);
603
604 mutex_unlock(&data->lock);
605 return count;
606}
607
608static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
609 char *buf)
610{
611 struct adt7475_data *data = adt7475_update_device(dev);
612 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
613
614 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
615}
616
617static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
618 char *buf)
619{
620 struct adt7475_data *data = adt7475_update_device(dev);
621 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
622
623 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
624}
625
626static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
627 char *buf)
628{
629 struct adt7475_data *data = adt7475_update_device(dev);
630 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
631
632 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
633}
634
635static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
636 const char *buf, size_t count)
637{
638
639 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
640 struct i2c_client *client = to_i2c_client(dev);
641 struct adt7475_data *data = i2c_get_clientdata(client);
642 unsigned char reg = 0;
643 long val;
644
645 if (strict_strtol(buf, 10, &val))
646 return -EINVAL;
647
648 mutex_lock(&data->lock);
649
650 switch (sattr->nr) {
651 case INPUT:
652 /* Get a fresh value for CONTROL */
653 data->pwm[CONTROL][sattr->index] =
654 adt7475_read(PWM_CONFIG_REG(sattr->index));
655
656 /* If we are not in manual mode, then we shouldn't allow
657 * the user to set the pwm speed */
658 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
659 mutex_unlock(&data->lock);
660 return count;
661 }
662
663 reg = PWM_REG(sattr->index);
664 break;
665
666 case MIN:
667 reg = PWM_MIN_REG(sattr->index);
668 break;
669
670 case MAX:
671 reg = PWM_MAX_REG(sattr->index);
672 break;
673 }
674
675 data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF);
676 i2c_smbus_write_byte_data(client, reg,
677 data->pwm[sattr->nr][sattr->index]);
678
679 mutex_unlock(&data->lock);
680
681 return count;
682}
683
684/* Called by set_pwmctrl and set_pwmchan */
685
686static int hw_set_pwm(struct i2c_client *client, int index,
687 unsigned int pwmctl, unsigned int pwmchan)
688{
689 struct adt7475_data *data = i2c_get_clientdata(client);
690 long val = 0;
691
692 switch (pwmctl) {
693 case 0:
694 val = 0x03; /* Run at full speed */
695 break;
696 case 1:
697 val = 0x07; /* Manual mode */
698 break;
699 case 2:
700 switch (pwmchan) {
701 case 1:
702 /* Remote1 controls PWM */
703 val = 0x00;
704 break;
705 case 2:
706 /* local controls PWM */
707 val = 0x01;
708 break;
709 case 4:
710 /* remote2 controls PWM */
711 val = 0x02;
712 break;
713 case 6:
714 /* local/remote2 control PWM */
715 val = 0x05;
716 break;
717 case 7:
718 /* All three control PWM */
719 val = 0x06;
720 break;
721 default:
722 return -EINVAL;
723 }
724 break;
725 default:
726 return -EINVAL;
727 }
728
729 data->pwmctl[index] = pwmctl;
730 data->pwmchan[index] = pwmchan;
731
732 data->pwm[CONTROL][index] &= ~0xE0;
733 data->pwm[CONTROL][index] |= (val & 7) << 5;
734
735 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
736 data->pwm[CONTROL][index]);
737
738 return 0;
739}
740
741static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
742 const char *buf, size_t count)
743{
744 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
745 struct i2c_client *client = to_i2c_client(dev);
746 struct adt7475_data *data = i2c_get_clientdata(client);
747 int r;
748 long val;
749
750 if (strict_strtol(buf, 10, &val))
751 return -EINVAL;
752
753 mutex_lock(&data->lock);
754 /* Read Modify Write PWM values */
755 adt7475_read_pwm(client, sattr->index);
756 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
757 if (r)
758 count = r;
759 mutex_unlock(&data->lock);
760
761 return count;
762}
763
764static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
765 const char *buf, size_t count)
766{
767 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
768 struct i2c_client *client = to_i2c_client(dev);
769 struct adt7475_data *data = i2c_get_clientdata(client);
770 int r;
771 long val;
772
773 if (strict_strtol(buf, 10, &val))
774 return -EINVAL;
775
776 mutex_lock(&data->lock);
777 /* Read Modify Write PWM values */
778 adt7475_read_pwm(client, sattr->index);
779 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
780 if (r)
781 count = r;
782 mutex_unlock(&data->lock);
783
784 return count;
785}
786
787/* List of frequencies for the PWM */
788static const int pwmfreq_table[] = {
789 11, 14, 22, 29, 35, 44, 58, 88
790};
791
792static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
793 char *buf)
794{
795 struct adt7475_data *data = adt7475_update_device(dev);
796 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
797
798 return sprintf(buf, "%d\n",
799 pwmfreq_table[data->range[sattr->index] & 7]);
800}
801
802static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
803 const char *buf, size_t count)
804{
805 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
806 struct i2c_client *client = to_i2c_client(dev);
807 struct adt7475_data *data = i2c_get_clientdata(client);
808 int out;
809 long val;
810
811 if (strict_strtol(buf, 10, &val))
812 return -EINVAL;
813
814 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
815
816 mutex_lock(&data->lock);
817
818 data->range[sattr->index] =
819 adt7475_read(TEMP_TRANGE_REG(sattr->index));
820 data->range[sattr->index] &= ~7;
821 data->range[sattr->index] |= out;
822
823 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
824 data->range[sattr->index]);
825
826 mutex_unlock(&data->lock);
827 return count;
828}
829
Jean Delvaref99318b2009-12-09 20:36:03 +0100830static ssize_t show_pwm_at_crit(struct device *dev,
831 struct device_attribute *devattr, char *buf)
832{
833 struct adt7475_data *data = adt7475_update_device(dev);
834 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
835}
836
837static ssize_t set_pwm_at_crit(struct device *dev,
838 struct device_attribute *devattr,
839 const char *buf, size_t count)
840{
841 struct i2c_client *client = to_i2c_client(dev);
842 struct adt7475_data *data = i2c_get_clientdata(client);
843 long val;
844
845 if (strict_strtol(buf, 10, &val))
846 return -EINVAL;
847 if (val != 0 && val != 1)
848 return -EINVAL;
849
850 mutex_lock(&data->lock);
851 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
852 if (val)
853 data->config4 |= CONFIG4_MAXDUTY;
854 else
855 data->config4 &= ~CONFIG4_MAXDUTY;
856 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
857 mutex_unlock(&data->lock);
858
859 return count;
860}
861
Jean Delvare3d849982009-12-09 20:36:05 +0100862static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
863static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
864 set_voltage, MAX, 0);
865static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
866 set_voltage, MIN, 0);
867static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100868static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100869static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100870 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100871static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100872 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100873static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
874static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
875static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
876 set_voltage, MAX, 2);
877static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
878 set_voltage, MIN, 2);
879static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +0100880static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
881static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
882 set_voltage, MAX, 3);
883static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
884 set_voltage, MIN, 3);
885static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
886static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
887static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
888 set_voltage, MAX, 4);
889static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
890 set_voltage, MIN, 4);
891static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
892static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
893static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
894 set_voltage, MAX, 5);
895static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
896 set_voltage, MIN, 5);
897static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100898static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
899static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
900static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
901static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
902 MAX, 0);
903static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
904 MIN, 0);
905static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
906 set_temp, OFFSET, 0);
907static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
908 show_temp, set_temp, AUTOMIN, 0);
909static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
910 show_point2, set_point2, 0, 0);
911static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
912 THERM, 0);
913static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
914 set_temp, HYSTERSIS, 0);
915static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
916static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
917static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
918 MAX, 1);
919static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
920 MIN, 1);
921static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
922 set_temp, OFFSET, 1);
923static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
924 show_temp, set_temp, AUTOMIN, 1);
925static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
926 show_point2, set_point2, 0, 1);
927static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
928 THERM, 1);
929static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
930 set_temp, HYSTERSIS, 1);
931static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
932static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
933static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
934static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
935 MAX, 2);
936static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
937 MIN, 2);
938static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
939 set_temp, OFFSET, 2);
940static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
941 show_temp, set_temp, AUTOMIN, 2);
942static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
943 show_point2, set_point2, 0, 2);
944static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
945 THERM, 2);
946static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
947 set_temp, HYSTERSIS, 2);
948static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
949static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
950 MIN, 0);
951static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
952static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
953static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
954 MIN, 1);
955static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
956static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
957static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
958 MIN, 2);
959static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
960static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
961static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
962 MIN, 3);
963static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
964static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
965 0);
966static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
967 set_pwmfreq, INPUT, 0);
968static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
969 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +0100970static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100971 show_pwmchan, set_pwmchan, INPUT, 0);
972static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
973 set_pwm, MIN, 0);
974static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
975 set_pwm, MAX, 0);
976static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
977 1);
978static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
979 set_pwmfreq, INPUT, 1);
980static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
981 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +0100982static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100983 show_pwmchan, set_pwmchan, INPUT, 1);
984static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
985 set_pwm, MIN, 1);
986static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
987 set_pwm, MAX, 1);
988static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
989 2);
990static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
991 set_pwmfreq, INPUT, 2);
992static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
993 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +0100994static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100995 show_pwmchan, set_pwmchan, INPUT, 2);
996static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
997 set_pwm, MIN, 2);
998static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
999 set_pwm, MAX, 2);
1000
Jean Delvaref99318b2009-12-09 20:36:03 +01001001/* Non-standard name, might need revisiting */
1002static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
1003 show_pwm_at_crit, set_pwm_at_crit);
1004
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001005static struct attribute *adt7475_attrs[] = {
1006 &sensor_dev_attr_in1_input.dev_attr.attr,
1007 &sensor_dev_attr_in1_max.dev_attr.attr,
1008 &sensor_dev_attr_in1_min.dev_attr.attr,
1009 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1010 &sensor_dev_attr_in2_input.dev_attr.attr,
1011 &sensor_dev_attr_in2_max.dev_attr.attr,
1012 &sensor_dev_attr_in2_min.dev_attr.attr,
1013 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1014 &sensor_dev_attr_temp1_input.dev_attr.attr,
1015 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1016 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1017 &sensor_dev_attr_temp1_max.dev_attr.attr,
1018 &sensor_dev_attr_temp1_min.dev_attr.attr,
1019 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1020 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1021 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1022 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1023 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1024 &sensor_dev_attr_temp2_input.dev_attr.attr,
1025 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1026 &sensor_dev_attr_temp2_max.dev_attr.attr,
1027 &sensor_dev_attr_temp2_min.dev_attr.attr,
1028 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1029 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1030 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1031 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1032 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1033 &sensor_dev_attr_temp3_input.dev_attr.attr,
1034 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1035 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1036 &sensor_dev_attr_temp3_max.dev_attr.attr,
1037 &sensor_dev_attr_temp3_min.dev_attr.attr,
1038 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1039 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1040 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1041 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1042 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1043 &sensor_dev_attr_fan1_input.dev_attr.attr,
1044 &sensor_dev_attr_fan1_min.dev_attr.attr,
1045 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1046 &sensor_dev_attr_fan2_input.dev_attr.attr,
1047 &sensor_dev_attr_fan2_min.dev_attr.attr,
1048 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1049 &sensor_dev_attr_fan3_input.dev_attr.attr,
1050 &sensor_dev_attr_fan3_min.dev_attr.attr,
1051 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001052 &sensor_dev_attr_pwm1.dev_attr.attr,
1053 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1054 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001055 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001056 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1057 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001058 &sensor_dev_attr_pwm3.dev_attr.attr,
1059 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1060 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001061 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001062 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1063 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001064 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001065 NULL,
1066};
1067
Jean Delvare378933c2009-12-09 20:36:06 +01001068static struct attribute *fan4_attrs[] = {
1069 &sensor_dev_attr_fan4_input.dev_attr.attr,
1070 &sensor_dev_attr_fan4_min.dev_attr.attr,
1071 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1072 NULL
1073};
1074
1075static struct attribute *pwm2_attrs[] = {
1076 &sensor_dev_attr_pwm2.dev_attr.attr,
1077 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1078 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1079 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1080 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1081 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1082 NULL
1083};
1084
Jean Delvare3d849982009-12-09 20:36:05 +01001085/* Attributes specific to the ADT7490 */
Jean Delvare378933c2009-12-09 20:36:06 +01001086static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001087 &sensor_dev_attr_in0_input.dev_attr.attr,
1088 &sensor_dev_attr_in0_max.dev_attr.attr,
1089 &sensor_dev_attr_in0_min.dev_attr.attr,
1090 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001091 NULL
1092};
1093
1094static struct attribute *adt7490_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001095 &sensor_dev_attr_in3_input.dev_attr.attr,
1096 &sensor_dev_attr_in3_max.dev_attr.attr,
1097 &sensor_dev_attr_in3_min.dev_attr.attr,
1098 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1099 &sensor_dev_attr_in4_input.dev_attr.attr,
1100 &sensor_dev_attr_in4_max.dev_attr.attr,
1101 &sensor_dev_attr_in4_min.dev_attr.attr,
1102 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1103 &sensor_dev_attr_in5_input.dev_attr.attr,
1104 &sensor_dev_attr_in5_max.dev_attr.attr,
1105 &sensor_dev_attr_in5_min.dev_attr.attr,
1106 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1107 NULL
1108};
1109
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001110static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jean Delvare378933c2009-12-09 20:36:06 +01001111static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1112static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1113static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
Jean Delvare3d849982009-12-09 20:36:05 +01001114static struct attribute_group adt7490_attr_group = { .attrs = adt7490_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001115
1116static int adt7475_detect(struct i2c_client *client, int kind,
1117 struct i2c_board_info *info)
1118{
1119 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001120 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001121 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001122
1123 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1124 return -ENODEV;
1125
Jean Delvareb180d052009-12-09 20:36:02 +01001126 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001127 devid2 = adt7475_read(REG_DEVID2);
1128 if (vendid != 0x41 || /* Analog Devices */
1129 (devid2 & 0xf8) != 0x68)
1130 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001131
Jean Delvared656b6f2009-12-09 20:36:04 +01001132 devid = adt7475_read(REG_DEVID);
1133 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001134 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001135 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001136 name = "adt7475";
Jean Delvare3d849982009-12-09 20:36:05 +01001137 else if ((devid2 & 0xfc) == 0x6c)
1138 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001139 else {
1140 dev_dbg(&adapter->dev,
Jean Delvare3d849982009-12-09 20:36:05 +01001141 "Couldn't detect an ADT7473/75/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001142 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001143 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001144 }
1145
Jean Delvareb180d052009-12-09 20:36:02 +01001146 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001147
1148 return 0;
1149}
1150
Jean Delvare0f144802009-12-09 20:36:06 +01001151static void adt7475_remove_files(struct i2c_client *client,
1152 struct adt7475_data *data)
1153{
1154 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1155 if (data->has_voltage & 0x39)
1156 sysfs_remove_group(&client->dev.kobj, &adt7490_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001157 if (data->has_fan4)
1158 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1159 if (data->has_pwm2)
1160 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1161 if (data->has_voltage & (1 << 0))
1162 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001163}
1164
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001165static int adt7475_probe(struct i2c_client *client,
1166 const struct i2c_device_id *id)
1167{
Jean Delvared07ca4a2009-12-09 20:36:07 +01001168 static const char *names[] = {
1169 [adt7473] = "ADT7473",
1170 [adt7475] = "ADT7475",
1171 [adt7490] = "ADT7490",
1172 };
1173
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001174 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001175 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001176 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001177
1178 data = kzalloc(sizeof(*data), GFP_KERNEL);
1179 if (data == NULL)
1180 return -ENOMEM;
1181
1182 mutex_init(&data->lock);
1183 i2c_set_clientdata(client, data);
1184
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001185 /* Initialize device-specific values */
1186 switch (id->driver_data) {
Jean Delvare3d849982009-12-09 20:36:05 +01001187 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001188 data->has_voltage = 0x3e; /* in1 to in5 */
1189 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001190 if (revision == 0x03)
1191 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001192 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001193 default:
1194 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001195 revision = adt7475_read(REG_DEVID2) & 0x07;
1196 }
1197
1198 config3 = adt7475_read(REG_CONFIG3);
1199 /* Pin PWM2 may alternatively be used for ALERT output */
1200 if (!(config3 & CONFIG3_SMBALERT))
1201 data->has_pwm2 = 1;
1202 /* Meaning of this bit is inverted for the ADT7473-1 */
1203 if (id->driver_data == adt7473 && revision >= 1)
1204 data->has_pwm2 = !data->has_pwm2;
1205
1206 data->config4 = adt7475_read(REG_CONFIG4);
1207 /* Pin TACH4 may alternatively be used for THERM */
1208 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1209 data->has_fan4 = 1;
1210
1211 /* THERM configuration is more complex on the ADT7490, because 2
1212 different pins (TACH4 and +2.5 Vin) can be used for this function */
1213 if (id->driver_data == adt7490) {
1214 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1215 !(config3 & CONFIG3_THERM))
1216 data->has_fan4 = 1;
1217 if (!(config3 & CONFIG3_THERM) ||
1218 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1219 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001220 }
1221
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001222 /* Voltage attenuators can be bypassed, globally or individually */
1223 config2 = adt7475_read(REG_CONFIG2);
1224 if (config2 & CONFIG2_ATTN) {
1225 data->bypass_attn = (0x3 << 3) | 0x3;
1226 } else {
1227 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1228 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1229 }
1230 data->bypass_attn &= data->has_voltage;
1231
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001232 /* Call adt7475_read_pwm for all pwm's as this will reprogram any
1233 pwm's which are disabled to manual mode with 0% duty cycle */
1234 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1235 adt7475_read_pwm(client, i);
1236
1237 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1238 if (ret)
1239 goto efree;
1240
Jean Delvare3d849982009-12-09 20:36:05 +01001241 if (id->driver_data == adt7490) {
1242 ret = sysfs_create_group(&client->dev.kobj,
1243 &adt7490_attr_group);
1244 if (ret)
1245 goto eremove;
1246 }
1247
Jean Delvare378933c2009-12-09 20:36:06 +01001248 /* Features that can be disabled individually */
1249 if (data->has_fan4) {
1250 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1251 if (ret)
1252 goto eremove;
1253 }
1254 if (data->has_pwm2) {
1255 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1256 if (ret)
1257 goto eremove;
1258 }
1259 if (data->has_voltage & (1 << 0)) {
1260 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1261 if (ret)
1262 goto eremove;
1263 }
1264
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001265 data->hwmon_dev = hwmon_device_register(&client->dev);
1266 if (IS_ERR(data->hwmon_dev)) {
1267 ret = PTR_ERR(data->hwmon_dev);
1268 goto eremove;
1269 }
1270
Jean Delvared07ca4a2009-12-09 20:36:07 +01001271 dev_info(&client->dev, "%s device, revision %d\n",
1272 names[id->driver_data], revision);
1273 if ((data->has_voltage & (1 << 0)) || data->has_fan4 || data->has_pwm2)
1274 dev_info(&client->dev, "Optional features:%s%s%s\n",
1275 (data->has_voltage & (1 << 0)) ? " in0" : "",
1276 data->has_fan4 ? " fan4" : "",
1277 data->has_pwm2 ? " pwm2" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001278 if (data->bypass_attn)
1279 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1280 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1281 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1282 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1283 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001284
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001285 return 0;
1286
1287eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001288 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001289efree:
1290 kfree(data);
1291 return ret;
1292}
1293
1294static int adt7475_remove(struct i2c_client *client)
1295{
1296 struct adt7475_data *data = i2c_get_clientdata(client);
1297
1298 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001299 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001300 kfree(data);
1301
1302 return 0;
1303}
1304
1305static struct i2c_driver adt7475_driver = {
1306 .class = I2C_CLASS_HWMON,
1307 .driver = {
1308 .name = "adt7475",
1309 },
1310 .probe = adt7475_probe,
1311 .remove = adt7475_remove,
1312 .id_table = adt7475_id,
1313 .detect = adt7475_detect,
1314 .address_data = &addr_data,
1315};
1316
1317static void adt7475_read_hystersis(struct i2c_client *client)
1318{
1319 struct adt7475_data *data = i2c_get_clientdata(client);
1320
1321 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1322 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1323 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1324}
1325
1326static void adt7475_read_pwm(struct i2c_client *client, int index)
1327{
1328 struct adt7475_data *data = i2c_get_clientdata(client);
1329 unsigned int v;
1330
1331 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1332
1333 /* Figure out the internal value for pwmctrl and pwmchan
1334 based on the current settings */
1335 v = (data->pwm[CONTROL][index] >> 5) & 7;
1336
1337 if (v == 3)
1338 data->pwmctl[index] = 0;
1339 else if (v == 7)
1340 data->pwmctl[index] = 1;
1341 else if (v == 4) {
1342 /* The fan is disabled - we don't want to
1343 support that, so change to manual mode and
1344 set the duty cycle to 0 instead
1345 */
1346 data->pwm[INPUT][index] = 0;
1347 data->pwm[CONTROL][index] &= ~0xE0;
1348 data->pwm[CONTROL][index] |= (7 << 5);
1349
1350 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1351 data->pwm[INPUT][index]);
1352
1353 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1354 data->pwm[CONTROL][index]);
1355
1356 data->pwmctl[index] = 1;
1357 } else {
1358 data->pwmctl[index] = 2;
1359
1360 switch (v) {
1361 case 0:
1362 data->pwmchan[index] = 1;
1363 break;
1364 case 1:
1365 data->pwmchan[index] = 2;
1366 break;
1367 case 2:
1368 data->pwmchan[index] = 4;
1369 break;
1370 case 5:
1371 data->pwmchan[index] = 6;
1372 break;
1373 case 6:
1374 data->pwmchan[index] = 7;
1375 break;
1376 }
1377 }
1378}
1379
1380static struct adt7475_data *adt7475_update_device(struct device *dev)
1381{
1382 struct i2c_client *client = to_i2c_client(dev);
1383 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001384 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001385 int i;
1386
1387 mutex_lock(&data->lock);
1388
1389 /* Measurement values update every 2 seconds */
1390 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1391 !data->valid) {
1392 data->alarms = adt7475_read(REG_STATUS2) << 8;
1393 data->alarms |= adt7475_read(REG_STATUS1);
1394
Jean Delvare3d849982009-12-09 20:36:05 +01001395 ext = (adt7475_read(REG_EXTEND2) << 8) |
1396 adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001397 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1398 if (!(data->has_voltage & (1 << i)))
1399 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001400 data->voltage[INPUT][i] =
1401 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001402 ((ext >> (i * 2)) & 3);
1403 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001404
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001405 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1406 data->temp[INPUT][i] =
1407 (adt7475_read(TEMP_REG(i)) << 2) |
Jean Delvare3d849982009-12-09 20:36:05 +01001408 ((ext >> ((i + 5) * 2)) & 3);
1409
1410 if (data->has_voltage & (1 << 5)) {
1411 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1412 ext = adt7475_read(REG_EXTEND3);
1413 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1414 ((ext >> 4) & 3);
1415 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001416
Jean Delvare378933c2009-12-09 20:36:06 +01001417 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1418 if (i == 3 && !data->has_fan4)
1419 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001420 data->tach[INPUT][i] =
1421 adt7475_read_word(client, TACH_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001422 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001423
1424 /* Updated by hw when in auto mode */
Jean Delvare378933c2009-12-09 20:36:06 +01001425 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1426 if (i == 1 && !data->has_pwm2)
1427 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001428 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001429 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001430
1431 data->measure_updated = jiffies;
1432 }
1433
1434 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001435 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001436 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001437 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001438 data->config5 = adt7475_read(REG_CONFIG5);
1439
1440 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001441 if (!(data->has_voltage & (1 << i)))
1442 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001443 /* Adjust values so they match the input precision */
1444 data->voltage[MIN][i] =
1445 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1446 data->voltage[MAX][i] =
1447 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1448 }
1449
Jean Delvare3d849982009-12-09 20:36:05 +01001450 if (data->has_voltage & (1 << 5)) {
1451 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1452 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1453 }
1454
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001455 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1456 /* Adjust values so they match the input precision */
1457 data->temp[MIN][i] =
1458 adt7475_read(TEMP_MIN_REG(i)) << 2;
1459 data->temp[MAX][i] =
1460 adt7475_read(TEMP_MAX_REG(i)) << 2;
1461 data->temp[AUTOMIN][i] =
1462 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1463 data->temp[THERM][i] =
1464 adt7475_read(TEMP_THERM_REG(i)) << 2;
1465 data->temp[OFFSET][i] =
1466 adt7475_read(TEMP_OFFSET_REG(i));
1467 }
1468 adt7475_read_hystersis(client);
1469
Jean Delvare378933c2009-12-09 20:36:06 +01001470 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1471 if (i == 3 && !data->has_fan4)
1472 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001473 data->tach[MIN][i] =
1474 adt7475_read_word(client, TACH_MIN_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001475 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001476
1477 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
Jean Delvare378933c2009-12-09 20:36:06 +01001478 if (i == 1 && !data->has_pwm2)
1479 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001480 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1481 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1482 /* Set the channel and control information */
1483 adt7475_read_pwm(client, i);
1484 }
1485
1486 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1487 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1488 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1489
1490 data->limits_updated = jiffies;
1491 data->valid = 1;
1492 }
1493
1494 mutex_unlock(&data->lock);
1495
1496 return data;
1497}
1498
1499static int __init sensors_adt7475_init(void)
1500{
1501 return i2c_add_driver(&adt7475_driver);
1502}
1503
1504static void __exit sensors_adt7475_exit(void)
1505{
1506 i2c_del_driver(&adt7475_driver);
1507}
1508
1509MODULE_AUTHOR("Advanced Micro Devices, Inc");
1510MODULE_DESCRIPTION("adt7475 driver");
1511MODULE_LICENSE("GPL");
1512
1513module_init(sensors_adt7475_init);
1514module_exit(sensors_adt7475_exit);