blob: 861c756e95366ceb6a3fcc4e7a5dfa5b838feb9a [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>
Jean Delvare54fe4672009-12-09 20:36:08 +010021#include <linux/hwmon-vid.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010022#include <linux/err.h>
23
24/* Indexes for the sysfs hooks */
25
26#define INPUT 0
27#define MIN 1
28#define MAX 2
29#define CONTROL 3
30#define OFFSET 3
31#define AUTOMIN 4
32#define THERM 5
33#define HYSTERSIS 6
34
Guenter Roeck9ed5bc22012-01-19 11:02:15 -080035/*
36 * These are unique identifiers for the sysfs functions - unlike the
37 * numbers above, these are not also indexes into an array
38 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +010039
40#define ALARM 9
41#define FAULT 10
42
43/* 7475 Common Registers */
44
Jean Delvared07ca4a2009-12-09 20:36:07 +010045#define REG_DEVREV2 0x12 /* ADT7490 only */
46
Jean Delvare3d849982009-12-09 20:36:05 +010047#define REG_VTT 0x1E /* ADT7490 only */
48#define REG_EXTEND3 0x1F /* ADT7490 only */
49
Jean Delvarecffb9dd2009-12-09 20:36:03 +010050#define REG_VOLTAGE_BASE 0x20
Jordan Crouse1c301fc2009-01-15 22:27:47 +010051#define REG_TEMP_BASE 0x25
52#define REG_TACH_BASE 0x28
53#define REG_PWM_BASE 0x30
54#define REG_PWM_MAX_BASE 0x38
55
56#define REG_DEVID 0x3D
57#define REG_VENDID 0x3E
Jean Delvared656b6f2009-12-09 20:36:04 +010058#define REG_DEVID2 0x3F
Jordan Crouse1c301fc2009-01-15 22:27:47 +010059
60#define REG_STATUS1 0x41
61#define REG_STATUS2 0x42
62
Jean Delvared8d2ee02009-12-09 20:36:08 +010063#define REG_VID 0x43 /* ADT7476 only */
64
Jean Delvarecffb9dd2009-12-09 20:36:03 +010065#define REG_VOLTAGE_MIN_BASE 0x44
66#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010067
68#define REG_TEMP_MIN_BASE 0x4E
69#define REG_TEMP_MAX_BASE 0x4F
70
71#define REG_TACH_MIN_BASE 0x54
72
73#define REG_PWM_CONFIG_BASE 0x5C
74
75#define REG_TEMP_TRANGE_BASE 0x5F
76
77#define REG_PWM_MIN_BASE 0x64
78
79#define REG_TEMP_TMIN_BASE 0x67
80#define REG_TEMP_THERM_BASE 0x6A
81
82#define REG_REMOTE1_HYSTERSIS 0x6D
83#define REG_REMOTE2_HYSTERSIS 0x6E
84
85#define REG_TEMP_OFFSET_BASE 0x70
86
Jean Delvareebfaf1f2009-12-09 20:36:07 +010087#define REG_CONFIG2 0x73
88
Jordan Crouse1c301fc2009-01-15 22:27:47 +010089#define REG_EXTEND1 0x76
90#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010091
92#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +010093#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +010094#define REG_CONFIG4 0x7D
95
Jean Delvare3d849982009-12-09 20:36:05 +010096#define REG_STATUS4 0x81 /* ADT7490 only */
97
98#define REG_VTT_MIN 0x84 /* ADT7490 only */
99#define REG_VTT_MAX 0x86 /* ADT7490 only */
100
Jean Delvared8d2ee02009-12-09 20:36:08 +0100101#define VID_VIDSEL 0x80 /* ADT7476 only */
102
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100103#define CONFIG2_ATTN 0x20
104
Jean Delvare378933c2009-12-09 20:36:06 +0100105#define CONFIG3_SMBALERT 0x01
106#define CONFIG3_THERM 0x02
107
108#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100109#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100110#define CONFIG4_ATTN_IN10 0x30
111#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100112
113#define CONFIG5_TWOSCOMP 0x01
114#define CONFIG5_TEMPOFFSET 0x02
Jean Delvare54fe4672009-12-09 20:36:08 +0100115#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100116
117/* ADT7475 Settings */
118
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100119#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100120#define ADT7475_TEMP_COUNT 3
121#define ADT7475_TACH_COUNT 4
122#define ADT7475_PWM_COUNT 3
123
124/* Macro to read the registers */
125
126#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
127
128/* Macros to easily index the registers */
129
130#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
131#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
132
133#define PWM_REG(idx) (REG_PWM_BASE + (idx))
134#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
135#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
136#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
137
138#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
139#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
140#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
141
142#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
143#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
144#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
145#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
146#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
147#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
148#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
149
Jean Delvare918ee912010-10-28 20:31:50 +0200150static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100151
Jean Delvaree5e9f442009-12-14 21:17:27 +0100152enum chips { adt7473, adt7475, adt7476, adt7490 };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100153
154static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100155 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100156 { "adt7475", adt7475 },
Jean Delvared8d2ee02009-12-09 20:36:08 +0100157 { "adt7476", adt7476 },
Jean Delvare3d849982009-12-09 20:36:05 +0100158 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100159 { }
160};
161MODULE_DEVICE_TABLE(i2c, adt7475_id);
162
163struct adt7475_data {
164 struct device *hwmon_dev;
165 struct mutex lock;
166
167 unsigned long measure_updated;
168 unsigned long limits_updated;
169 char valid;
170
Jean Delvaref99318b2009-12-09 20:36:03 +0100171 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100172 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100173 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100174 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100175 u8 has_pwm2:1;
176 u8 has_fan4:1;
Jean Delvare54fe4672009-12-09 20:36:08 +0100177 u8 has_vid:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100178 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100179 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100180 u16 temp[7][3];
181 u16 tach[2][4];
182 u8 pwm[4][3];
183 u8 range[3];
184 u8 pwmctl[3];
185 u8 pwmchan[3];
Jean Delvare54fe4672009-12-09 20:36:08 +0100186
187 u8 vid;
188 u8 vrm;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100189};
190
191static struct i2c_driver adt7475_driver;
192static struct adt7475_data *adt7475_update_device(struct device *dev);
193static void adt7475_read_hystersis(struct i2c_client *client);
194static void adt7475_read_pwm(struct i2c_client *client, int index);
195
196/* Given a temp value, convert it to register value */
197
198static inline u16 temp2reg(struct adt7475_data *data, long val)
199{
200 u16 ret;
201
202 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
203 val = SENSORS_LIMIT(val, -64000, 191000);
204 ret = (val + 64500) / 1000;
205 } else {
206 val = SENSORS_LIMIT(val, -128000, 127000);
207 if (val < -500)
208 ret = (256500 + val) / 1000;
209 else
210 ret = (val + 500) / 1000;
211 }
212
213 return ret << 2;
214}
215
216/* Given a register value, convert it to a real temp value */
217
218static inline int reg2temp(struct adt7475_data *data, u16 reg)
219{
220 if (data->config5 & CONFIG5_TWOSCOMP) {
221 if (reg >= 512)
222 return (reg - 1024) * 250;
223 else
224 return reg * 250;
225 } else
226 return (reg - 256) * 250;
227}
228
229static inline int tach2rpm(u16 tach)
230{
231 if (tach == 0 || tach == 0xFFFF)
232 return 0;
233
234 return (90000 * 60) / tach;
235}
236
237static inline u16 rpm2tach(unsigned long rpm)
238{
239 if (rpm == 0)
240 return 0;
241
242 return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF);
243}
244
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100245/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
246static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
247 { 45, 94 }, /* +2.5V */
248 { 175, 525 }, /* Vccp */
249 { 68, 71 }, /* Vcc */
250 { 93, 47 }, /* +5V */
251 { 120, 20 }, /* +12V */
252 { 45, 45 }, /* Vtt */
253};
254
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100255static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100256{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100257 const int *r = adt7473_in_scaling[channel];
258
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100259 if (bypass_attn & (1 << channel))
260 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100261 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100262}
263
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100264static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100265{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100266 const int *r = adt7473_in_scaling[channel];
267 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100268
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100269 if (bypass_attn & (1 << channel))
270 reg = (volt * 1024) / 2250;
271 else
272 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100273 return SENSORS_LIMIT(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100274}
275
276static u16 adt7475_read_word(struct i2c_client *client, int reg)
277{
278 u16 val;
279
280 val = i2c_smbus_read_byte_data(client, reg);
281 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
282
283 return val;
284}
285
286static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
287{
288 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
289 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
290}
291
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800292/*
293 * Find the nearest value in a table - used for pwm frequency and
294 * auto temp range
295 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100296static int find_nearest(long val, const int *array, int size)
297{
298 int i;
299
300 if (val < array[0])
301 return 0;
302
303 if (val > array[size - 1])
304 return size - 1;
305
306 for (i = 0; i < size - 1; i++) {
307 int a, b;
308
309 if (val > array[i + 1])
310 continue;
311
312 a = val - array[i];
313 b = array[i + 1] - val;
314
315 return (a <= b) ? i : i + 1;
316 }
317
318 return 0;
319}
320
321static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
322 char *buf)
323{
324 struct adt7475_data *data = adt7475_update_device(dev);
325 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
326 unsigned short val;
327
328 switch (sattr->nr) {
329 case ALARM:
330 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100331 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100332 default:
333 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100334 return sprintf(buf, "%d\n",
335 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100336 }
337}
338
339static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
340 const char *buf, size_t count)
341{
342
343 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
344 struct i2c_client *client = to_i2c_client(dev);
345 struct adt7475_data *data = i2c_get_clientdata(client);
346 unsigned char reg;
347 long val;
348
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100349 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100350 return -EINVAL;
351
352 mutex_lock(&data->lock);
353
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100354 data->voltage[sattr->nr][sattr->index] =
355 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100356
Jean Delvare3d849982009-12-09 20:36:05 +0100357 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
358 if (sattr->nr == MIN)
359 reg = VOLTAGE_MIN_REG(sattr->index);
360 else
361 reg = VOLTAGE_MAX_REG(sattr->index);
362 } else {
363 if (sattr->nr == MIN)
364 reg = REG_VTT_MIN;
365 else
366 reg = REG_VTT_MAX;
367 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100368
369 i2c_smbus_write_byte_data(client, reg,
370 data->voltage[sattr->nr][sattr->index] >> 2);
371 mutex_unlock(&data->lock);
372
373 return count;
374}
375
376static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
377 char *buf)
378{
379 struct adt7475_data *data = adt7475_update_device(dev);
380 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
381 int out;
382
383 switch (sattr->nr) {
384 case HYSTERSIS:
385 mutex_lock(&data->lock);
386 out = data->temp[sattr->nr][sattr->index];
387 if (sattr->index != 1)
388 out = (out >> 4) & 0xF;
389 else
390 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800391 /*
392 * Show the value as an absolute number tied to
393 * THERM
394 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100395 out = reg2temp(data, data->temp[THERM][sattr->index]) -
396 out * 1000;
397 mutex_unlock(&data->lock);
398 break;
399
400 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800401 /*
402 * Offset is always 2's complement, regardless of the
403 * setting in CONFIG5
404 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100405 mutex_lock(&data->lock);
406 out = (s8)data->temp[sattr->nr][sattr->index];
407 if (data->config5 & CONFIG5_TEMPOFFSET)
408 out *= 1000;
409 else
410 out *= 500;
411 mutex_unlock(&data->lock);
412 break;
413
414 case ALARM:
415 out = (data->alarms >> (sattr->index + 4)) & 1;
416 break;
417
418 case FAULT:
419 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100420 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100421 break;
422
423 default:
424 /* All other temp values are in the configured format */
425 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
426 }
427
428 return sprintf(buf, "%d\n", out);
429}
430
431static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
432 const char *buf, size_t count)
433{
434 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
435 struct i2c_client *client = to_i2c_client(dev);
436 struct adt7475_data *data = i2c_get_clientdata(client);
437 unsigned char reg = 0;
438 u8 out;
439 int temp;
440 long val;
441
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100442 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100443 return -EINVAL;
444
445 mutex_lock(&data->lock);
446
447 /* We need the config register in all cases for temp <-> reg conv. */
448 data->config5 = adt7475_read(REG_CONFIG5);
449
450 switch (sattr->nr) {
451 case OFFSET:
452 if (data->config5 & CONFIG5_TEMPOFFSET) {
453 val = SENSORS_LIMIT(val, -63000, 127000);
454 out = data->temp[OFFSET][sattr->index] = val / 1000;
455 } else {
456 val = SENSORS_LIMIT(val, -63000, 64000);
457 out = data->temp[OFFSET][sattr->index] = val / 500;
458 }
459 break;
460
461 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800462 /*
463 * The value will be given as an absolute value, turn it
464 * into an offset based on THERM
465 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100466
467 /* Read fresh THERM and HYSTERSIS values from the chip */
468 data->temp[THERM][sattr->index] =
469 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
470 adt7475_read_hystersis(client);
471
472 temp = reg2temp(data, data->temp[THERM][sattr->index]);
473 val = SENSORS_LIMIT(val, temp - 15000, temp);
474 val = (temp - val) / 1000;
475
476 if (sattr->index != 1) {
477 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
478 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
479 } else {
480 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
481 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
482 }
483
484 out = data->temp[HYSTERSIS][sattr->index];
485 break;
486
487 default:
488 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
489
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800490 /*
491 * We maintain an extra 2 digits of precision for simplicity
492 * - shift those back off before writing the value
493 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100494 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
495 }
496
497 switch (sattr->nr) {
498 case MIN:
499 reg = TEMP_MIN_REG(sattr->index);
500 break;
501 case MAX:
502 reg = TEMP_MAX_REG(sattr->index);
503 break;
504 case OFFSET:
505 reg = TEMP_OFFSET_REG(sattr->index);
506 break;
507 case AUTOMIN:
508 reg = TEMP_TMIN_REG(sattr->index);
509 break;
510 case THERM:
511 reg = TEMP_THERM_REG(sattr->index);
512 break;
513 case HYSTERSIS:
514 if (sattr->index != 2)
515 reg = REG_REMOTE1_HYSTERSIS;
516 else
517 reg = REG_REMOTE2_HYSTERSIS;
518
519 break;
520 }
521
522 i2c_smbus_write_byte_data(client, reg, out);
523
524 mutex_unlock(&data->lock);
525 return count;
526}
527
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800528/*
529 * Table of autorange values - the user will write the value in millidegrees,
530 * and we'll convert it
531 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100532static const int autorange_table[] = {
533 2000, 2500, 3330, 4000, 5000, 6670, 8000,
534 10000, 13330, 16000, 20000, 26670, 32000, 40000,
535 53330, 80000
536};
537
538static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
539 char *buf)
540{
541 struct adt7475_data *data = adt7475_update_device(dev);
542 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
543 int out, val;
544
545 mutex_lock(&data->lock);
546 out = (data->range[sattr->index] >> 4) & 0x0F;
547 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
548 mutex_unlock(&data->lock);
549
550 return sprintf(buf, "%d\n", val + autorange_table[out]);
551}
552
553static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
554 const char *buf, size_t count)
555{
556 struct i2c_client *client = to_i2c_client(dev);
557 struct adt7475_data *data = i2c_get_clientdata(client);
558 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
559 int temp;
560 long val;
561
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100562 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100563 return -EINVAL;
564
565 mutex_lock(&data->lock);
566
567 /* Get a fresh copy of the needed registers */
568 data->config5 = adt7475_read(REG_CONFIG5);
569 data->temp[AUTOMIN][sattr->index] =
570 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
571 data->range[sattr->index] =
572 adt7475_read(TEMP_TRANGE_REG(sattr->index));
573
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800574 /*
575 * The user will write an absolute value, so subtract the start point
576 * to figure the range
577 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100578 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
579 val = SENSORS_LIMIT(val, temp + autorange_table[0],
580 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
581 val -= temp;
582
583 /* Find the nearest table entry to what the user wrote */
584 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
585
586 data->range[sattr->index] &= ~0xF0;
587 data->range[sattr->index] |= val << 4;
588
589 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
590 data->range[sattr->index]);
591
592 mutex_unlock(&data->lock);
593 return count;
594}
595
596static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
597 char *buf)
598{
599 struct adt7475_data *data = adt7475_update_device(dev);
600 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
601 int out;
602
603 if (sattr->nr == ALARM)
604 out = (data->alarms >> (sattr->index + 10)) & 1;
605 else
606 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
607
608 return sprintf(buf, "%d\n", out);
609}
610
611static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
612 const char *buf, size_t count)
613{
614
615 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
616 struct i2c_client *client = to_i2c_client(dev);
617 struct adt7475_data *data = i2c_get_clientdata(client);
618 unsigned long val;
619
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100620 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100621 return -EINVAL;
622
623 mutex_lock(&data->lock);
624
625 data->tach[MIN][sattr->index] = rpm2tach(val);
626
627 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
628 data->tach[MIN][sattr->index]);
629
630 mutex_unlock(&data->lock);
631 return count;
632}
633
634static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
635 char *buf)
636{
637 struct adt7475_data *data = adt7475_update_device(dev);
638 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
639
640 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
641}
642
643static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
644 char *buf)
645{
646 struct adt7475_data *data = adt7475_update_device(dev);
647 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
648
649 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
650}
651
652static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
653 char *buf)
654{
655 struct adt7475_data *data = adt7475_update_device(dev);
656 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
657
658 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
659}
660
661static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
662 const char *buf, size_t count)
663{
664
665 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
666 struct i2c_client *client = to_i2c_client(dev);
667 struct adt7475_data *data = i2c_get_clientdata(client);
668 unsigned char reg = 0;
669 long val;
670
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100671 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100672 return -EINVAL;
673
674 mutex_lock(&data->lock);
675
676 switch (sattr->nr) {
677 case INPUT:
678 /* Get a fresh value for CONTROL */
679 data->pwm[CONTROL][sattr->index] =
680 adt7475_read(PWM_CONFIG_REG(sattr->index));
681
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800682 /*
683 * If we are not in manual mode, then we shouldn't allow
684 * the user to set the pwm speed
685 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100686 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
687 mutex_unlock(&data->lock);
688 return count;
689 }
690
691 reg = PWM_REG(sattr->index);
692 break;
693
694 case MIN:
695 reg = PWM_MIN_REG(sattr->index);
696 break;
697
698 case MAX:
699 reg = PWM_MAX_REG(sattr->index);
700 break;
701 }
702
703 data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF);
704 i2c_smbus_write_byte_data(client, reg,
705 data->pwm[sattr->nr][sattr->index]);
706
707 mutex_unlock(&data->lock);
708
709 return count;
710}
711
712/* Called by set_pwmctrl and set_pwmchan */
713
714static int hw_set_pwm(struct i2c_client *client, int index,
715 unsigned int pwmctl, unsigned int pwmchan)
716{
717 struct adt7475_data *data = i2c_get_clientdata(client);
718 long val = 0;
719
720 switch (pwmctl) {
721 case 0:
722 val = 0x03; /* Run at full speed */
723 break;
724 case 1:
725 val = 0x07; /* Manual mode */
726 break;
727 case 2:
728 switch (pwmchan) {
729 case 1:
730 /* Remote1 controls PWM */
731 val = 0x00;
732 break;
733 case 2:
734 /* local controls PWM */
735 val = 0x01;
736 break;
737 case 4:
738 /* remote2 controls PWM */
739 val = 0x02;
740 break;
741 case 6:
742 /* local/remote2 control PWM */
743 val = 0x05;
744 break;
745 case 7:
746 /* All three control PWM */
747 val = 0x06;
748 break;
749 default:
750 return -EINVAL;
751 }
752 break;
753 default:
754 return -EINVAL;
755 }
756
757 data->pwmctl[index] = pwmctl;
758 data->pwmchan[index] = pwmchan;
759
760 data->pwm[CONTROL][index] &= ~0xE0;
761 data->pwm[CONTROL][index] |= (val & 7) << 5;
762
763 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
764 data->pwm[CONTROL][index]);
765
766 return 0;
767}
768
769static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
770 const char *buf, size_t count)
771{
772 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
773 struct i2c_client *client = to_i2c_client(dev);
774 struct adt7475_data *data = i2c_get_clientdata(client);
775 int r;
776 long val;
777
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100778 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100779 return -EINVAL;
780
781 mutex_lock(&data->lock);
782 /* Read Modify Write PWM values */
783 adt7475_read_pwm(client, sattr->index);
784 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
785 if (r)
786 count = r;
787 mutex_unlock(&data->lock);
788
789 return count;
790}
791
792static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
793 const char *buf, size_t count)
794{
795 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
796 struct i2c_client *client = to_i2c_client(dev);
797 struct adt7475_data *data = i2c_get_clientdata(client);
798 int r;
799 long val;
800
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100801 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100802 return -EINVAL;
803
804 mutex_lock(&data->lock);
805 /* Read Modify Write PWM values */
806 adt7475_read_pwm(client, sattr->index);
807 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
808 if (r)
809 count = r;
810 mutex_unlock(&data->lock);
811
812 return count;
813}
814
815/* List of frequencies for the PWM */
816static const int pwmfreq_table[] = {
817 11, 14, 22, 29, 35, 44, 58, 88
818};
819
820static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
821 char *buf)
822{
823 struct adt7475_data *data = adt7475_update_device(dev);
824 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
825
826 return sprintf(buf, "%d\n",
827 pwmfreq_table[data->range[sattr->index] & 7]);
828}
829
830static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
831 const char *buf, size_t count)
832{
833 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
834 struct i2c_client *client = to_i2c_client(dev);
835 struct adt7475_data *data = i2c_get_clientdata(client);
836 int out;
837 long val;
838
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100839 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100840 return -EINVAL;
841
842 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
843
844 mutex_lock(&data->lock);
845
846 data->range[sattr->index] =
847 adt7475_read(TEMP_TRANGE_REG(sattr->index));
848 data->range[sattr->index] &= ~7;
849 data->range[sattr->index] |= out;
850
851 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
852 data->range[sattr->index]);
853
854 mutex_unlock(&data->lock);
855 return count;
856}
857
Jean Delvaref99318b2009-12-09 20:36:03 +0100858static ssize_t show_pwm_at_crit(struct device *dev,
859 struct device_attribute *devattr, char *buf)
860{
861 struct adt7475_data *data = adt7475_update_device(dev);
862 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
863}
864
865static ssize_t set_pwm_at_crit(struct device *dev,
866 struct device_attribute *devattr,
867 const char *buf, size_t count)
868{
869 struct i2c_client *client = to_i2c_client(dev);
870 struct adt7475_data *data = i2c_get_clientdata(client);
871 long val;
872
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100873 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +0100874 return -EINVAL;
875 if (val != 0 && val != 1)
876 return -EINVAL;
877
878 mutex_lock(&data->lock);
879 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
880 if (val)
881 data->config4 |= CONFIG4_MAXDUTY;
882 else
883 data->config4 &= ~CONFIG4_MAXDUTY;
884 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
885 mutex_unlock(&data->lock);
886
887 return count;
888}
889
Jean Delvare54fe4672009-12-09 20:36:08 +0100890static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr,
891 char *buf)
892{
893 struct adt7475_data *data = dev_get_drvdata(dev);
894 return sprintf(buf, "%d\n", (int)data->vrm);
895}
896
897static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr,
898 const char *buf, size_t count)
899{
900 struct adt7475_data *data = dev_get_drvdata(dev);
901 long val;
902
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100903 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +0100904 return -EINVAL;
905 if (val < 0 || val > 255)
906 return -EINVAL;
907 data->vrm = val;
908
909 return count;
910}
911
912static ssize_t show_vid(struct device *dev, struct device_attribute *devattr,
913 char *buf)
914{
915 struct adt7475_data *data = adt7475_update_device(dev);
916 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
917}
918
Jean Delvare3d849982009-12-09 20:36:05 +0100919static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
920static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
921 set_voltage, MAX, 0);
922static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
923 set_voltage, MIN, 0);
924static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100925static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100926static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100927 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100928static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100929 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100930static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
931static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
932static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
933 set_voltage, MAX, 2);
934static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
935 set_voltage, MIN, 2);
936static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +0100937static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
938static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
939 set_voltage, MAX, 3);
940static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
941 set_voltage, MIN, 3);
942static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
943static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
944static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
945 set_voltage, MAX, 4);
946static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
947 set_voltage, MIN, 4);
948static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
949static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
950static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
951 set_voltage, MAX, 5);
952static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
953 set_voltage, MIN, 5);
954static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100955static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
956static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
957static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
958static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
959 MAX, 0);
960static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
961 MIN, 0);
962static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
963 set_temp, OFFSET, 0);
964static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
965 show_temp, set_temp, AUTOMIN, 0);
966static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
967 show_point2, set_point2, 0, 0);
968static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
969 THERM, 0);
970static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
971 set_temp, HYSTERSIS, 0);
972static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
973static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
974static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
975 MAX, 1);
976static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
977 MIN, 1);
978static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
979 set_temp, OFFSET, 1);
980static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
981 show_temp, set_temp, AUTOMIN, 1);
982static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
983 show_point2, set_point2, 0, 1);
984static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
985 THERM, 1);
986static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
987 set_temp, HYSTERSIS, 1);
988static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
989static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
990static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
991static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
992 MAX, 2);
993static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
994 MIN, 2);
995static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
996 set_temp, OFFSET, 2);
997static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
998 show_temp, set_temp, AUTOMIN, 2);
999static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
1000 show_point2, set_point2, 0, 2);
1001static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1002 THERM, 2);
1003static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1004 set_temp, HYSTERSIS, 2);
1005static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1006static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1007 MIN, 0);
1008static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1009static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1010static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1011 MIN, 1);
1012static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1013static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1014static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1015 MIN, 2);
1016static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1017static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1018static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1019 MIN, 3);
1020static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1021static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1022 0);
1023static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1024 set_pwmfreq, INPUT, 0);
1025static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1026 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +01001027static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001028 show_pwmchan, set_pwmchan, INPUT, 0);
1029static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1030 set_pwm, MIN, 0);
1031static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1032 set_pwm, MAX, 0);
1033static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1034 1);
1035static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1036 set_pwmfreq, INPUT, 1);
1037static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1038 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +01001039static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001040 show_pwmchan, set_pwmchan, INPUT, 1);
1041static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1042 set_pwm, MIN, 1);
1043static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1044 set_pwm, MAX, 1);
1045static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1046 2);
1047static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1048 set_pwmfreq, INPUT, 2);
1049static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1050 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +01001051static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001052 show_pwmchan, set_pwmchan, INPUT, 2);
1053static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1054 set_pwm, MIN, 2);
1055static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1056 set_pwm, MAX, 2);
1057
Jean Delvaref99318b2009-12-09 20:36:03 +01001058/* Non-standard name, might need revisiting */
1059static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
1060 show_pwm_at_crit, set_pwm_at_crit);
1061
Jean Delvare54fe4672009-12-09 20:36:08 +01001062static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm);
1063static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1064
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001065static struct attribute *adt7475_attrs[] = {
1066 &sensor_dev_attr_in1_input.dev_attr.attr,
1067 &sensor_dev_attr_in1_max.dev_attr.attr,
1068 &sensor_dev_attr_in1_min.dev_attr.attr,
1069 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1070 &sensor_dev_attr_in2_input.dev_attr.attr,
1071 &sensor_dev_attr_in2_max.dev_attr.attr,
1072 &sensor_dev_attr_in2_min.dev_attr.attr,
1073 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1074 &sensor_dev_attr_temp1_input.dev_attr.attr,
1075 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1076 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1077 &sensor_dev_attr_temp1_max.dev_attr.attr,
1078 &sensor_dev_attr_temp1_min.dev_attr.attr,
1079 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1080 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1081 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1082 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1083 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1084 &sensor_dev_attr_temp2_input.dev_attr.attr,
1085 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1086 &sensor_dev_attr_temp2_max.dev_attr.attr,
1087 &sensor_dev_attr_temp2_min.dev_attr.attr,
1088 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1089 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1090 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1091 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1092 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1093 &sensor_dev_attr_temp3_input.dev_attr.attr,
1094 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1095 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1096 &sensor_dev_attr_temp3_max.dev_attr.attr,
1097 &sensor_dev_attr_temp3_min.dev_attr.attr,
1098 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1099 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1100 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1101 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1102 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1103 &sensor_dev_attr_fan1_input.dev_attr.attr,
1104 &sensor_dev_attr_fan1_min.dev_attr.attr,
1105 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1106 &sensor_dev_attr_fan2_input.dev_attr.attr,
1107 &sensor_dev_attr_fan2_min.dev_attr.attr,
1108 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1109 &sensor_dev_attr_fan3_input.dev_attr.attr,
1110 &sensor_dev_attr_fan3_min.dev_attr.attr,
1111 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001112 &sensor_dev_attr_pwm1.dev_attr.attr,
1113 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1114 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001115 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001116 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1117 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001118 &sensor_dev_attr_pwm3.dev_attr.attr,
1119 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1120 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001121 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001122 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1123 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001124 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001125 NULL,
1126};
1127
Jean Delvare378933c2009-12-09 20:36:06 +01001128static struct attribute *fan4_attrs[] = {
1129 &sensor_dev_attr_fan4_input.dev_attr.attr,
1130 &sensor_dev_attr_fan4_min.dev_attr.attr,
1131 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1132 NULL
1133};
1134
1135static struct attribute *pwm2_attrs[] = {
1136 &sensor_dev_attr_pwm2.dev_attr.attr,
1137 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1138 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1139 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1140 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1141 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1142 NULL
1143};
1144
Jean Delvare378933c2009-12-09 20:36:06 +01001145static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001146 &sensor_dev_attr_in0_input.dev_attr.attr,
1147 &sensor_dev_attr_in0_max.dev_attr.attr,
1148 &sensor_dev_attr_in0_min.dev_attr.attr,
1149 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001150 NULL
1151};
1152
Jean Delvared8d2ee02009-12-09 20:36:08 +01001153static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001154 &sensor_dev_attr_in3_input.dev_attr.attr,
1155 &sensor_dev_attr_in3_max.dev_attr.attr,
1156 &sensor_dev_attr_in3_min.dev_attr.attr,
1157 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001158 NULL
1159};
1160
1161static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001162 &sensor_dev_attr_in4_input.dev_attr.attr,
1163 &sensor_dev_attr_in4_max.dev_attr.attr,
1164 &sensor_dev_attr_in4_min.dev_attr.attr,
1165 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001166 NULL
1167};
1168
1169static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001170 &sensor_dev_attr_in5_input.dev_attr.attr,
1171 &sensor_dev_attr_in5_max.dev_attr.attr,
1172 &sensor_dev_attr_in5_min.dev_attr.attr,
1173 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1174 NULL
1175};
1176
Jean Delvare54fe4672009-12-09 20:36:08 +01001177static struct attribute *vid_attrs[] = {
1178 &dev_attr_cpu0_vid.attr,
1179 &dev_attr_vrm.attr,
1180 NULL
1181};
1182
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001183static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jean Delvare378933c2009-12-09 20:36:06 +01001184static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1185static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1186static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
Jean Delvared8d2ee02009-12-09 20:36:08 +01001187static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1188static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1189static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
Jean Delvare54fe4672009-12-09 20:36:08 +01001190static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001191
Jean Delvare310ec792009-12-14 21:17:23 +01001192static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001193 struct i2c_board_info *info)
1194{
1195 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001196 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001197 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001198
1199 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1200 return -ENODEV;
1201
Jean Delvareb180d052009-12-09 20:36:02 +01001202 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001203 devid2 = adt7475_read(REG_DEVID2);
1204 if (vendid != 0x41 || /* Analog Devices */
1205 (devid2 & 0xf8) != 0x68)
1206 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001207
Jean Delvared656b6f2009-12-09 20:36:04 +01001208 devid = adt7475_read(REG_DEVID);
1209 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001210 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001211 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001212 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001213 else if (devid == 0x76)
1214 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001215 else if ((devid2 & 0xfc) == 0x6c)
1216 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001217 else {
1218 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001219 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001220 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001221 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001222 }
1223
Jean Delvareb180d052009-12-09 20:36:02 +01001224 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001225
1226 return 0;
1227}
1228
Jean Delvare0f144802009-12-09 20:36:06 +01001229static void adt7475_remove_files(struct i2c_client *client,
1230 struct adt7475_data *data)
1231{
1232 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001233 if (data->has_fan4)
1234 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1235 if (data->has_pwm2)
1236 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1237 if (data->has_voltage & (1 << 0))
1238 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001239 if (data->has_voltage & (1 << 3))
1240 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1241 if (data->has_voltage & (1 << 4))
1242 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1243 if (data->has_voltage & (1 << 5))
1244 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001245 if (data->has_vid)
1246 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001247}
1248
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001249static int adt7475_probe(struct i2c_client *client,
1250 const struct i2c_device_id *id)
1251{
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001252 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001253 [adt7473] = "ADT7473",
1254 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001255 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001256 [adt7490] = "ADT7490",
1257 };
1258
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001259 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001260 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001261 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001262
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001263 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001264 if (data == NULL)
1265 return -ENOMEM;
1266
1267 mutex_init(&data->lock);
1268 i2c_set_clientdata(client, data);
1269
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001270 /* Initialize device-specific values */
1271 switch (id->driver_data) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001272 case adt7476:
1273 data->has_voltage = 0x0e; /* in1 to in3 */
1274 revision = adt7475_read(REG_DEVID2) & 0x07;
1275 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001276 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001277 data->has_voltage = 0x3e; /* in1 to in5 */
1278 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001279 if (revision == 0x03)
1280 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001281 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001282 default:
1283 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001284 revision = adt7475_read(REG_DEVID2) & 0x07;
1285 }
1286
1287 config3 = adt7475_read(REG_CONFIG3);
1288 /* Pin PWM2 may alternatively be used for ALERT output */
1289 if (!(config3 & CONFIG3_SMBALERT))
1290 data->has_pwm2 = 1;
1291 /* Meaning of this bit is inverted for the ADT7473-1 */
1292 if (id->driver_data == adt7473 && revision >= 1)
1293 data->has_pwm2 = !data->has_pwm2;
1294
1295 data->config4 = adt7475_read(REG_CONFIG4);
1296 /* Pin TACH4 may alternatively be used for THERM */
1297 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1298 data->has_fan4 = 1;
1299
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001300 /*
1301 * THERM configuration is more complex on the ADT7476 and ADT7490,
1302 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1303 * this function
1304 */
Jean Delvare378933c2009-12-09 20:36:06 +01001305 if (id->driver_data == adt7490) {
1306 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1307 !(config3 & CONFIG3_THERM))
1308 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001309 }
1310 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001311 if (!(config3 & CONFIG3_THERM) ||
1312 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1313 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001314 }
1315
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001316 /*
1317 * On the ADT7476, the +12V input pin may instead be used as VID5,
1318 * and VID pins may alternatively be used as GPIO
1319 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001320 if (id->driver_data == adt7476) {
1321 u8 vid = adt7475_read(REG_VID);
1322 if (!(vid & VID_VIDSEL))
1323 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001324
1325 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001326 }
1327
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001328 /* Voltage attenuators can be bypassed, globally or individually */
1329 config2 = adt7475_read(REG_CONFIG2);
1330 if (config2 & CONFIG2_ATTN) {
1331 data->bypass_attn = (0x3 << 3) | 0x3;
1332 } else {
1333 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1334 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1335 }
1336 data->bypass_attn &= data->has_voltage;
1337
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001338 /*
1339 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1340 * pwm's which are disabled to manual mode with 0% duty cycle
1341 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001342 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1343 adt7475_read_pwm(client, i);
1344
1345 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1346 if (ret)
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001347 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001348
Jean Delvare378933c2009-12-09 20:36:06 +01001349 /* Features that can be disabled individually */
1350 if (data->has_fan4) {
1351 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1352 if (ret)
1353 goto eremove;
1354 }
1355 if (data->has_pwm2) {
1356 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1357 if (ret)
1358 goto eremove;
1359 }
1360 if (data->has_voltage & (1 << 0)) {
1361 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1362 if (ret)
1363 goto eremove;
1364 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001365 if (data->has_voltage & (1 << 3)) {
1366 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1367 if (ret)
1368 goto eremove;
1369 }
1370 if (data->has_voltage & (1 << 4)) {
1371 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1372 if (ret)
1373 goto eremove;
1374 }
1375 if (data->has_voltage & (1 << 5)) {
1376 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1377 if (ret)
1378 goto eremove;
1379 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001380 if (data->has_vid) {
1381 data->vrm = vid_which_vrm();
1382 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1383 if (ret)
1384 goto eremove;
1385 }
Jean Delvare378933c2009-12-09 20:36:06 +01001386
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001387 data->hwmon_dev = hwmon_device_register(&client->dev);
1388 if (IS_ERR(data->hwmon_dev)) {
1389 ret = PTR_ERR(data->hwmon_dev);
1390 goto eremove;
1391 }
1392
Jean Delvared07ca4a2009-12-09 20:36:07 +01001393 dev_info(&client->dev, "%s device, revision %d\n",
1394 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001395 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001396 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001397 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001398 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001399 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001400 data->has_pwm2 ? " pwm2" : "",
1401 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001402 if (data->bypass_attn)
1403 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1404 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1405 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1406 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1407 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001408
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001409 return 0;
1410
1411eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001412 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001413 return ret;
1414}
1415
1416static int adt7475_remove(struct i2c_client *client)
1417{
1418 struct adt7475_data *data = i2c_get_clientdata(client);
1419
1420 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001421 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001422
1423 return 0;
1424}
1425
1426static struct i2c_driver adt7475_driver = {
1427 .class = I2C_CLASS_HWMON,
1428 .driver = {
1429 .name = "adt7475",
1430 },
1431 .probe = adt7475_probe,
1432 .remove = adt7475_remove,
1433 .id_table = adt7475_id,
1434 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001435 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001436};
1437
1438static void adt7475_read_hystersis(struct i2c_client *client)
1439{
1440 struct adt7475_data *data = i2c_get_clientdata(client);
1441
1442 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1443 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1444 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1445}
1446
1447static void adt7475_read_pwm(struct i2c_client *client, int index)
1448{
1449 struct adt7475_data *data = i2c_get_clientdata(client);
1450 unsigned int v;
1451
1452 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1453
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001454 /*
1455 * Figure out the internal value for pwmctrl and pwmchan
1456 * based on the current settings
1457 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001458 v = (data->pwm[CONTROL][index] >> 5) & 7;
1459
1460 if (v == 3)
1461 data->pwmctl[index] = 0;
1462 else if (v == 7)
1463 data->pwmctl[index] = 1;
1464 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001465 /*
1466 * The fan is disabled - we don't want to
1467 * support that, so change to manual mode and
1468 * set the duty cycle to 0 instead
1469 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001470 data->pwm[INPUT][index] = 0;
1471 data->pwm[CONTROL][index] &= ~0xE0;
1472 data->pwm[CONTROL][index] |= (7 << 5);
1473
1474 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1475 data->pwm[INPUT][index]);
1476
1477 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1478 data->pwm[CONTROL][index]);
1479
1480 data->pwmctl[index] = 1;
1481 } else {
1482 data->pwmctl[index] = 2;
1483
1484 switch (v) {
1485 case 0:
1486 data->pwmchan[index] = 1;
1487 break;
1488 case 1:
1489 data->pwmchan[index] = 2;
1490 break;
1491 case 2:
1492 data->pwmchan[index] = 4;
1493 break;
1494 case 5:
1495 data->pwmchan[index] = 6;
1496 break;
1497 case 6:
1498 data->pwmchan[index] = 7;
1499 break;
1500 }
1501 }
1502}
1503
1504static struct adt7475_data *adt7475_update_device(struct device *dev)
1505{
1506 struct i2c_client *client = to_i2c_client(dev);
1507 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001508 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001509 int i;
1510
1511 mutex_lock(&data->lock);
1512
1513 /* Measurement values update every 2 seconds */
1514 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1515 !data->valid) {
1516 data->alarms = adt7475_read(REG_STATUS2) << 8;
1517 data->alarms |= adt7475_read(REG_STATUS1);
1518
Jean Delvare3d849982009-12-09 20:36:05 +01001519 ext = (adt7475_read(REG_EXTEND2) << 8) |
1520 adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001521 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1522 if (!(data->has_voltage & (1 << i)))
1523 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001524 data->voltage[INPUT][i] =
1525 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001526 ((ext >> (i * 2)) & 3);
1527 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001528
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001529 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1530 data->temp[INPUT][i] =
1531 (adt7475_read(TEMP_REG(i)) << 2) |
Jean Delvare3d849982009-12-09 20:36:05 +01001532 ((ext >> ((i + 5) * 2)) & 3);
1533
1534 if (data->has_voltage & (1 << 5)) {
1535 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1536 ext = adt7475_read(REG_EXTEND3);
1537 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1538 ((ext >> 4) & 3);
1539 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001540
Jean Delvare378933c2009-12-09 20:36:06 +01001541 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1542 if (i == 3 && !data->has_fan4)
1543 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001544 data->tach[INPUT][i] =
1545 adt7475_read_word(client, TACH_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001546 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001547
1548 /* Updated by hw when in auto mode */
Jean Delvare378933c2009-12-09 20:36:06 +01001549 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1550 if (i == 1 && !data->has_pwm2)
1551 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001552 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001553 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001554
Jean Delvare54fe4672009-12-09 20:36:08 +01001555 if (data->has_vid)
1556 data->vid = adt7475_read(REG_VID) & 0x3f;
1557
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001558 data->measure_updated = jiffies;
1559 }
1560
1561 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001562 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001563 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001564 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001565 data->config5 = adt7475_read(REG_CONFIG5);
1566
1567 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001568 if (!(data->has_voltage & (1 << i)))
1569 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001570 /* Adjust values so they match the input precision */
1571 data->voltage[MIN][i] =
1572 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1573 data->voltage[MAX][i] =
1574 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1575 }
1576
Jean Delvare3d849982009-12-09 20:36:05 +01001577 if (data->has_voltage & (1 << 5)) {
1578 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1579 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1580 }
1581
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001582 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1583 /* Adjust values so they match the input precision */
1584 data->temp[MIN][i] =
1585 adt7475_read(TEMP_MIN_REG(i)) << 2;
1586 data->temp[MAX][i] =
1587 adt7475_read(TEMP_MAX_REG(i)) << 2;
1588 data->temp[AUTOMIN][i] =
1589 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1590 data->temp[THERM][i] =
1591 adt7475_read(TEMP_THERM_REG(i)) << 2;
1592 data->temp[OFFSET][i] =
1593 adt7475_read(TEMP_OFFSET_REG(i));
1594 }
1595 adt7475_read_hystersis(client);
1596
Jean Delvare378933c2009-12-09 20:36:06 +01001597 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1598 if (i == 3 && !data->has_fan4)
1599 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001600 data->tach[MIN][i] =
1601 adt7475_read_word(client, TACH_MIN_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001602 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001603
1604 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
Jean Delvare378933c2009-12-09 20:36:06 +01001605 if (i == 1 && !data->has_pwm2)
1606 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001607 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1608 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1609 /* Set the channel and control information */
1610 adt7475_read_pwm(client, i);
1611 }
1612
1613 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1614 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1615 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1616
1617 data->limits_updated = jiffies;
1618 data->valid = 1;
1619 }
1620
1621 mutex_unlock(&data->lock);
1622
1623 return data;
1624}
1625
Axel Linf0967ee2012-01-20 15:38:18 +08001626module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001627
1628MODULE_AUTHOR("Advanced Micro Devices, Inc");
1629MODULE_DESCRIPTION("adt7475 driver");
1630MODULE_LICENSE("GPL");