blob: a0c385145686c3d068a9453ce80f8023ecf9cc7c [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
35/* These are unique identifiers for the sysfs functions - unlike the
36 numbers above, these are not also indexes into an array
37*/
38
39#define ALARM 9
40#define FAULT 10
41
42/* 7475 Common Registers */
43
Jean Delvared07ca4a2009-12-09 20:36:07 +010044#define REG_DEVREV2 0x12 /* ADT7490 only */
45
Jean Delvare3d849982009-12-09 20:36:05 +010046#define REG_VTT 0x1E /* ADT7490 only */
47#define REG_EXTEND3 0x1F /* ADT7490 only */
48
Jean Delvarecffb9dd2009-12-09 20:36:03 +010049#define REG_VOLTAGE_BASE 0x20
Jordan Crouse1c301fc2009-01-15 22:27:47 +010050#define REG_TEMP_BASE 0x25
51#define REG_TACH_BASE 0x28
52#define REG_PWM_BASE 0x30
53#define REG_PWM_MAX_BASE 0x38
54
55#define REG_DEVID 0x3D
56#define REG_VENDID 0x3E
Jean Delvared656b6f2009-12-09 20:36:04 +010057#define REG_DEVID2 0x3F
Jordan Crouse1c301fc2009-01-15 22:27:47 +010058
59#define REG_STATUS1 0x41
60#define REG_STATUS2 0x42
61
Jean Delvared8d2ee02009-12-09 20:36:08 +010062#define REG_VID 0x43 /* ADT7476 only */
63
Jean Delvarecffb9dd2009-12-09 20:36:03 +010064#define REG_VOLTAGE_MIN_BASE 0x44
65#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010066
67#define REG_TEMP_MIN_BASE 0x4E
68#define REG_TEMP_MAX_BASE 0x4F
69
70#define REG_TACH_MIN_BASE 0x54
71
72#define REG_PWM_CONFIG_BASE 0x5C
73
74#define REG_TEMP_TRANGE_BASE 0x5F
75
76#define REG_PWM_MIN_BASE 0x64
77
78#define REG_TEMP_TMIN_BASE 0x67
79#define REG_TEMP_THERM_BASE 0x6A
80
81#define REG_REMOTE1_HYSTERSIS 0x6D
82#define REG_REMOTE2_HYSTERSIS 0x6E
83
84#define REG_TEMP_OFFSET_BASE 0x70
85
Jean Delvareebfaf1f2009-12-09 20:36:07 +010086#define REG_CONFIG2 0x73
87
Jordan Crouse1c301fc2009-01-15 22:27:47 +010088#define REG_EXTEND1 0x76
89#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010090
91#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +010092#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +010093#define REG_CONFIG4 0x7D
94
Jean Delvare3d849982009-12-09 20:36:05 +010095#define REG_STATUS4 0x81 /* ADT7490 only */
96
97#define REG_VTT_MIN 0x84 /* ADT7490 only */
98#define REG_VTT_MAX 0x86 /* ADT7490 only */
99
Jean Delvared8d2ee02009-12-09 20:36:08 +0100100#define VID_VIDSEL 0x80 /* ADT7476 only */
101
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100102#define CONFIG2_ATTN 0x20
103
Jean Delvare378933c2009-12-09 20:36:06 +0100104#define CONFIG3_SMBALERT 0x01
105#define CONFIG3_THERM 0x02
106
107#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100108#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100109#define CONFIG4_ATTN_IN10 0x30
110#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100111
112#define CONFIG5_TWOSCOMP 0x01
113#define CONFIG5_TEMPOFFSET 0x02
Jean Delvare54fe4672009-12-09 20:36:08 +0100114#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100115
116/* ADT7475 Settings */
117
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100118#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100119#define ADT7475_TEMP_COUNT 3
120#define ADT7475_TACH_COUNT 4
121#define ADT7475_PWM_COUNT 3
122
123/* Macro to read the registers */
124
125#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
126
127/* Macros to easily index the registers */
128
129#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
130#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
131
132#define PWM_REG(idx) (REG_PWM_BASE + (idx))
133#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
134#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
135#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
136
137#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
138#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
139#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
140
141#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
142#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
143#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
144#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
145#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
146#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
147#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
148
Jean Delvareb180d052009-12-09 20:36:02 +0100149static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100150
Jean Delvaree5e9f442009-12-14 21:17:27 +0100151enum chips { adt7473, adt7475, adt7476, adt7490 };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100152
153static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100154 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100155 { "adt7475", adt7475 },
Jean Delvared8d2ee02009-12-09 20:36:08 +0100156 { "adt7476", adt7476 },
Jean Delvare3d849982009-12-09 20:36:05 +0100157 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100158 { }
159};
160MODULE_DEVICE_TABLE(i2c, adt7475_id);
161
162struct adt7475_data {
163 struct device *hwmon_dev;
164 struct mutex lock;
165
166 unsigned long measure_updated;
167 unsigned long limits_updated;
168 char valid;
169
Jean Delvaref99318b2009-12-09 20:36:03 +0100170 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100171 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100172 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100173 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100174 u8 has_pwm2:1;
175 u8 has_fan4:1;
Jean Delvare54fe4672009-12-09 20:36:08 +0100176 u8 has_vid:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100177 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100178 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100179 u16 temp[7][3];
180 u16 tach[2][4];
181 u8 pwm[4][3];
182 u8 range[3];
183 u8 pwmctl[3];
184 u8 pwmchan[3];
Jean Delvare54fe4672009-12-09 20:36:08 +0100185
186 u8 vid;
187 u8 vrm;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100188};
189
190static struct i2c_driver adt7475_driver;
191static struct adt7475_data *adt7475_update_device(struct device *dev);
192static void adt7475_read_hystersis(struct i2c_client *client);
193static void adt7475_read_pwm(struct i2c_client *client, int index);
194
195/* Given a temp value, convert it to register value */
196
197static inline u16 temp2reg(struct adt7475_data *data, long val)
198{
199 u16 ret;
200
201 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
202 val = SENSORS_LIMIT(val, -64000, 191000);
203 ret = (val + 64500) / 1000;
204 } else {
205 val = SENSORS_LIMIT(val, -128000, 127000);
206 if (val < -500)
207 ret = (256500 + val) / 1000;
208 else
209 ret = (val + 500) / 1000;
210 }
211
212 return ret << 2;
213}
214
215/* Given a register value, convert it to a real temp value */
216
217static inline int reg2temp(struct adt7475_data *data, u16 reg)
218{
219 if (data->config5 & CONFIG5_TWOSCOMP) {
220 if (reg >= 512)
221 return (reg - 1024) * 250;
222 else
223 return reg * 250;
224 } else
225 return (reg - 256) * 250;
226}
227
228static inline int tach2rpm(u16 tach)
229{
230 if (tach == 0 || tach == 0xFFFF)
231 return 0;
232
233 return (90000 * 60) / tach;
234}
235
236static inline u16 rpm2tach(unsigned long rpm)
237{
238 if (rpm == 0)
239 return 0;
240
241 return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF);
242}
243
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100244/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
245static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
246 { 45, 94 }, /* +2.5V */
247 { 175, 525 }, /* Vccp */
248 { 68, 71 }, /* Vcc */
249 { 93, 47 }, /* +5V */
250 { 120, 20 }, /* +12V */
251 { 45, 45 }, /* Vtt */
252};
253
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100254static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100255{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100256 const int *r = adt7473_in_scaling[channel];
257
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100258 if (bypass_attn & (1 << channel))
259 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100260 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100261}
262
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100263static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100264{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100265 const int *r = adt7473_in_scaling[channel];
266 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100267
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100268 if (bypass_attn & (1 << channel))
269 reg = (volt * 1024) / 2250;
270 else
271 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100272 return SENSORS_LIMIT(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100273}
274
275static u16 adt7475_read_word(struct i2c_client *client, int reg)
276{
277 u16 val;
278
279 val = i2c_smbus_read_byte_data(client, reg);
280 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
281
282 return val;
283}
284
285static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
286{
287 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
288 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
289}
290
291/* Find the nearest value in a table - used for pwm frequency and
292 auto temp range */
293static int find_nearest(long val, const int *array, int size)
294{
295 int i;
296
297 if (val < array[0])
298 return 0;
299
300 if (val > array[size - 1])
301 return size - 1;
302
303 for (i = 0; i < size - 1; i++) {
304 int a, b;
305
306 if (val > array[i + 1])
307 continue;
308
309 a = val - array[i];
310 b = array[i + 1] - val;
311
312 return (a <= b) ? i : i + 1;
313 }
314
315 return 0;
316}
317
318static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
319 char *buf)
320{
321 struct adt7475_data *data = adt7475_update_device(dev);
322 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
323 unsigned short val;
324
325 switch (sattr->nr) {
326 case ALARM:
327 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100328 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100329 default:
330 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100331 return sprintf(buf, "%d\n",
332 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100333 }
334}
335
336static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
337 const char *buf, size_t count)
338{
339
340 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
341 struct i2c_client *client = to_i2c_client(dev);
342 struct adt7475_data *data = i2c_get_clientdata(client);
343 unsigned char reg;
344 long val;
345
346 if (strict_strtol(buf, 10, &val))
347 return -EINVAL;
348
349 mutex_lock(&data->lock);
350
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100351 data->voltage[sattr->nr][sattr->index] =
352 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100353
Jean Delvare3d849982009-12-09 20:36:05 +0100354 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
355 if (sattr->nr == MIN)
356 reg = VOLTAGE_MIN_REG(sattr->index);
357 else
358 reg = VOLTAGE_MAX_REG(sattr->index);
359 } else {
360 if (sattr->nr == MIN)
361 reg = REG_VTT_MIN;
362 else
363 reg = REG_VTT_MAX;
364 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100365
366 i2c_smbus_write_byte_data(client, reg,
367 data->voltage[sattr->nr][sattr->index] >> 2);
368 mutex_unlock(&data->lock);
369
370 return count;
371}
372
373static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
374 char *buf)
375{
376 struct adt7475_data *data = adt7475_update_device(dev);
377 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
378 int out;
379
380 switch (sattr->nr) {
381 case HYSTERSIS:
382 mutex_lock(&data->lock);
383 out = data->temp[sattr->nr][sattr->index];
384 if (sattr->index != 1)
385 out = (out >> 4) & 0xF;
386 else
387 out = (out & 0xF);
388 /* Show the value as an absolute number tied to
389 * THERM */
390 out = reg2temp(data, data->temp[THERM][sattr->index]) -
391 out * 1000;
392 mutex_unlock(&data->lock);
393 break;
394
395 case OFFSET:
396 /* Offset is always 2's complement, regardless of the
397 * setting in CONFIG5 */
398 mutex_lock(&data->lock);
399 out = (s8)data->temp[sattr->nr][sattr->index];
400 if (data->config5 & CONFIG5_TEMPOFFSET)
401 out *= 1000;
402 else
403 out *= 500;
404 mutex_unlock(&data->lock);
405 break;
406
407 case ALARM:
408 out = (data->alarms >> (sattr->index + 4)) & 1;
409 break;
410
411 case FAULT:
412 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100413 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100414 break;
415
416 default:
417 /* All other temp values are in the configured format */
418 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
419 }
420
421 return sprintf(buf, "%d\n", out);
422}
423
424static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
425 const char *buf, size_t count)
426{
427 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
428 struct i2c_client *client = to_i2c_client(dev);
429 struct adt7475_data *data = i2c_get_clientdata(client);
430 unsigned char reg = 0;
431 u8 out;
432 int temp;
433 long val;
434
435 if (strict_strtol(buf, 10, &val))
436 return -EINVAL;
437
438 mutex_lock(&data->lock);
439
440 /* We need the config register in all cases for temp <-> reg conv. */
441 data->config5 = adt7475_read(REG_CONFIG5);
442
443 switch (sattr->nr) {
444 case OFFSET:
445 if (data->config5 & CONFIG5_TEMPOFFSET) {
446 val = SENSORS_LIMIT(val, -63000, 127000);
447 out = data->temp[OFFSET][sattr->index] = val / 1000;
448 } else {
449 val = SENSORS_LIMIT(val, -63000, 64000);
450 out = data->temp[OFFSET][sattr->index] = val / 500;
451 }
452 break;
453
454 case HYSTERSIS:
455 /* The value will be given as an absolute value, turn it
456 into an offset based on THERM */
457
458 /* Read fresh THERM and HYSTERSIS values from the chip */
459 data->temp[THERM][sattr->index] =
460 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
461 adt7475_read_hystersis(client);
462
463 temp = reg2temp(data, data->temp[THERM][sattr->index]);
464 val = SENSORS_LIMIT(val, temp - 15000, temp);
465 val = (temp - val) / 1000;
466
467 if (sattr->index != 1) {
468 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
469 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
470 } else {
471 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
472 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
473 }
474
475 out = data->temp[HYSTERSIS][sattr->index];
476 break;
477
478 default:
479 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
480
481 /* We maintain an extra 2 digits of precision for simplicity
482 * - shift those back off before writing the value */
483 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
484 }
485
486 switch (sattr->nr) {
487 case MIN:
488 reg = TEMP_MIN_REG(sattr->index);
489 break;
490 case MAX:
491 reg = TEMP_MAX_REG(sattr->index);
492 break;
493 case OFFSET:
494 reg = TEMP_OFFSET_REG(sattr->index);
495 break;
496 case AUTOMIN:
497 reg = TEMP_TMIN_REG(sattr->index);
498 break;
499 case THERM:
500 reg = TEMP_THERM_REG(sattr->index);
501 break;
502 case HYSTERSIS:
503 if (sattr->index != 2)
504 reg = REG_REMOTE1_HYSTERSIS;
505 else
506 reg = REG_REMOTE2_HYSTERSIS;
507
508 break;
509 }
510
511 i2c_smbus_write_byte_data(client, reg, out);
512
513 mutex_unlock(&data->lock);
514 return count;
515}
516
517/* Table of autorange values - the user will write the value in millidegrees,
518 and we'll convert it */
519static const int autorange_table[] = {
520 2000, 2500, 3330, 4000, 5000, 6670, 8000,
521 10000, 13330, 16000, 20000, 26670, 32000, 40000,
522 53330, 80000
523};
524
525static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
526 char *buf)
527{
528 struct adt7475_data *data = adt7475_update_device(dev);
529 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
530 int out, val;
531
532 mutex_lock(&data->lock);
533 out = (data->range[sattr->index] >> 4) & 0x0F;
534 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
535 mutex_unlock(&data->lock);
536
537 return sprintf(buf, "%d\n", val + autorange_table[out]);
538}
539
540static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
541 const char *buf, size_t count)
542{
543 struct i2c_client *client = to_i2c_client(dev);
544 struct adt7475_data *data = i2c_get_clientdata(client);
545 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
546 int temp;
547 long val;
548
549 if (strict_strtol(buf, 10, &val))
550 return -EINVAL;
551
552 mutex_lock(&data->lock);
553
554 /* Get a fresh copy of the needed registers */
555 data->config5 = adt7475_read(REG_CONFIG5);
556 data->temp[AUTOMIN][sattr->index] =
557 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
558 data->range[sattr->index] =
559 adt7475_read(TEMP_TRANGE_REG(sattr->index));
560
561 /* The user will write an absolute value, so subtract the start point
562 to figure the range */
563 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
564 val = SENSORS_LIMIT(val, temp + autorange_table[0],
565 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
566 val -= temp;
567
568 /* Find the nearest table entry to what the user wrote */
569 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
570
571 data->range[sattr->index] &= ~0xF0;
572 data->range[sattr->index] |= val << 4;
573
574 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
575 data->range[sattr->index]);
576
577 mutex_unlock(&data->lock);
578 return count;
579}
580
581static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
582 char *buf)
583{
584 struct adt7475_data *data = adt7475_update_device(dev);
585 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
586 int out;
587
588 if (sattr->nr == ALARM)
589 out = (data->alarms >> (sattr->index + 10)) & 1;
590 else
591 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
592
593 return sprintf(buf, "%d\n", out);
594}
595
596static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
598{
599
600 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
601 struct i2c_client *client = to_i2c_client(dev);
602 struct adt7475_data *data = i2c_get_clientdata(client);
603 unsigned long val;
604
605 if (strict_strtoul(buf, 10, &val))
606 return -EINVAL;
607
608 mutex_lock(&data->lock);
609
610 data->tach[MIN][sattr->index] = rpm2tach(val);
611
612 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
613 data->tach[MIN][sattr->index]);
614
615 mutex_unlock(&data->lock);
616 return count;
617}
618
619static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
620 char *buf)
621{
622 struct adt7475_data *data = adt7475_update_device(dev);
623 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
624
625 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
626}
627
628static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
629 char *buf)
630{
631 struct adt7475_data *data = adt7475_update_device(dev);
632 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
633
634 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
635}
636
637static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
638 char *buf)
639{
640 struct adt7475_data *data = adt7475_update_device(dev);
641 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
642
643 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
644}
645
646static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
647 const char *buf, size_t count)
648{
649
650 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
651 struct i2c_client *client = to_i2c_client(dev);
652 struct adt7475_data *data = i2c_get_clientdata(client);
653 unsigned char reg = 0;
654 long val;
655
656 if (strict_strtol(buf, 10, &val))
657 return -EINVAL;
658
659 mutex_lock(&data->lock);
660
661 switch (sattr->nr) {
662 case INPUT:
663 /* Get a fresh value for CONTROL */
664 data->pwm[CONTROL][sattr->index] =
665 adt7475_read(PWM_CONFIG_REG(sattr->index));
666
667 /* If we are not in manual mode, then we shouldn't allow
668 * the user to set the pwm speed */
669 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
670 mutex_unlock(&data->lock);
671 return count;
672 }
673
674 reg = PWM_REG(sattr->index);
675 break;
676
677 case MIN:
678 reg = PWM_MIN_REG(sattr->index);
679 break;
680
681 case MAX:
682 reg = PWM_MAX_REG(sattr->index);
683 break;
684 }
685
686 data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF);
687 i2c_smbus_write_byte_data(client, reg,
688 data->pwm[sattr->nr][sattr->index]);
689
690 mutex_unlock(&data->lock);
691
692 return count;
693}
694
695/* Called by set_pwmctrl and set_pwmchan */
696
697static int hw_set_pwm(struct i2c_client *client, int index,
698 unsigned int pwmctl, unsigned int pwmchan)
699{
700 struct adt7475_data *data = i2c_get_clientdata(client);
701 long val = 0;
702
703 switch (pwmctl) {
704 case 0:
705 val = 0x03; /* Run at full speed */
706 break;
707 case 1:
708 val = 0x07; /* Manual mode */
709 break;
710 case 2:
711 switch (pwmchan) {
712 case 1:
713 /* Remote1 controls PWM */
714 val = 0x00;
715 break;
716 case 2:
717 /* local controls PWM */
718 val = 0x01;
719 break;
720 case 4:
721 /* remote2 controls PWM */
722 val = 0x02;
723 break;
724 case 6:
725 /* local/remote2 control PWM */
726 val = 0x05;
727 break;
728 case 7:
729 /* All three control PWM */
730 val = 0x06;
731 break;
732 default:
733 return -EINVAL;
734 }
735 break;
736 default:
737 return -EINVAL;
738 }
739
740 data->pwmctl[index] = pwmctl;
741 data->pwmchan[index] = pwmchan;
742
743 data->pwm[CONTROL][index] &= ~0xE0;
744 data->pwm[CONTROL][index] |= (val & 7) << 5;
745
746 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
747 data->pwm[CONTROL][index]);
748
749 return 0;
750}
751
752static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
753 const char *buf, size_t count)
754{
755 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
756 struct i2c_client *client = to_i2c_client(dev);
757 struct adt7475_data *data = i2c_get_clientdata(client);
758 int r;
759 long val;
760
761 if (strict_strtol(buf, 10, &val))
762 return -EINVAL;
763
764 mutex_lock(&data->lock);
765 /* Read Modify Write PWM values */
766 adt7475_read_pwm(client, sattr->index);
767 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
768 if (r)
769 count = r;
770 mutex_unlock(&data->lock);
771
772 return count;
773}
774
775static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
776 const char *buf, size_t count)
777{
778 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
779 struct i2c_client *client = to_i2c_client(dev);
780 struct adt7475_data *data = i2c_get_clientdata(client);
781 int r;
782 long val;
783
784 if (strict_strtol(buf, 10, &val))
785 return -EINVAL;
786
787 mutex_lock(&data->lock);
788 /* Read Modify Write PWM values */
789 adt7475_read_pwm(client, sattr->index);
790 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
791 if (r)
792 count = r;
793 mutex_unlock(&data->lock);
794
795 return count;
796}
797
798/* List of frequencies for the PWM */
799static const int pwmfreq_table[] = {
800 11, 14, 22, 29, 35, 44, 58, 88
801};
802
803static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
804 char *buf)
805{
806 struct adt7475_data *data = adt7475_update_device(dev);
807 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
808
809 return sprintf(buf, "%d\n",
810 pwmfreq_table[data->range[sattr->index] & 7]);
811}
812
813static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
814 const char *buf, size_t count)
815{
816 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
817 struct i2c_client *client = to_i2c_client(dev);
818 struct adt7475_data *data = i2c_get_clientdata(client);
819 int out;
820 long val;
821
822 if (strict_strtol(buf, 10, &val))
823 return -EINVAL;
824
825 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
826
827 mutex_lock(&data->lock);
828
829 data->range[sattr->index] =
830 adt7475_read(TEMP_TRANGE_REG(sattr->index));
831 data->range[sattr->index] &= ~7;
832 data->range[sattr->index] |= out;
833
834 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
835 data->range[sattr->index]);
836
837 mutex_unlock(&data->lock);
838 return count;
839}
840
Jean Delvaref99318b2009-12-09 20:36:03 +0100841static ssize_t show_pwm_at_crit(struct device *dev,
842 struct device_attribute *devattr, char *buf)
843{
844 struct adt7475_data *data = adt7475_update_device(dev);
845 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
846}
847
848static ssize_t set_pwm_at_crit(struct device *dev,
849 struct device_attribute *devattr,
850 const char *buf, size_t count)
851{
852 struct i2c_client *client = to_i2c_client(dev);
853 struct adt7475_data *data = i2c_get_clientdata(client);
854 long val;
855
856 if (strict_strtol(buf, 10, &val))
857 return -EINVAL;
858 if (val != 0 && val != 1)
859 return -EINVAL;
860
861 mutex_lock(&data->lock);
862 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
863 if (val)
864 data->config4 |= CONFIG4_MAXDUTY;
865 else
866 data->config4 &= ~CONFIG4_MAXDUTY;
867 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
868 mutex_unlock(&data->lock);
869
870 return count;
871}
872
Jean Delvare54fe4672009-12-09 20:36:08 +0100873static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr,
874 char *buf)
875{
876 struct adt7475_data *data = dev_get_drvdata(dev);
877 return sprintf(buf, "%d\n", (int)data->vrm);
878}
879
880static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr,
881 const char *buf, size_t count)
882{
883 struct adt7475_data *data = dev_get_drvdata(dev);
884 long val;
885
886 if (strict_strtol(buf, 10, &val))
887 return -EINVAL;
888 if (val < 0 || val > 255)
889 return -EINVAL;
890 data->vrm = val;
891
892 return count;
893}
894
895static ssize_t show_vid(struct device *dev, struct device_attribute *devattr,
896 char *buf)
897{
898 struct adt7475_data *data = adt7475_update_device(dev);
899 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
900}
901
Jean Delvare3d849982009-12-09 20:36:05 +0100902static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
903static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
904 set_voltage, MAX, 0);
905static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
906 set_voltage, MIN, 0);
907static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100908static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100909static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100910 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100911static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100912 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100913static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
914static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
915static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
916 set_voltage, MAX, 2);
917static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
918 set_voltage, MIN, 2);
919static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +0100920static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
921static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
922 set_voltage, MAX, 3);
923static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
924 set_voltage, MIN, 3);
925static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
926static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
927static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
928 set_voltage, MAX, 4);
929static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
930 set_voltage, MIN, 4);
931static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
932static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
933static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
934 set_voltage, MAX, 5);
935static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
936 set_voltage, MIN, 5);
937static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100938static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
939static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
940static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
941static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
942 MAX, 0);
943static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
944 MIN, 0);
945static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
946 set_temp, OFFSET, 0);
947static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
948 show_temp, set_temp, AUTOMIN, 0);
949static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
950 show_point2, set_point2, 0, 0);
951static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
952 THERM, 0);
953static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
954 set_temp, HYSTERSIS, 0);
955static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
956static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
957static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
958 MAX, 1);
959static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
960 MIN, 1);
961static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
962 set_temp, OFFSET, 1);
963static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
964 show_temp, set_temp, AUTOMIN, 1);
965static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
966 show_point2, set_point2, 0, 1);
967static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
968 THERM, 1);
969static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
970 set_temp, HYSTERSIS, 1);
971static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
972static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
973static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
974static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
975 MAX, 2);
976static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
977 MIN, 2);
978static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
979 set_temp, OFFSET, 2);
980static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
981 show_temp, set_temp, AUTOMIN, 2);
982static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
983 show_point2, set_point2, 0, 2);
984static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
985 THERM, 2);
986static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
987 set_temp, HYSTERSIS, 2);
988static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
989static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
990 MIN, 0);
991static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
992static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
993static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
994 MIN, 1);
995static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
996static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
997static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
998 MIN, 2);
999static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1000static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1001static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1002 MIN, 3);
1003static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1004static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1005 0);
1006static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1007 set_pwmfreq, INPUT, 0);
1008static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1009 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +01001010static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001011 show_pwmchan, set_pwmchan, INPUT, 0);
1012static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1013 set_pwm, MIN, 0);
1014static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1015 set_pwm, MAX, 0);
1016static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1017 1);
1018static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1019 set_pwmfreq, INPUT, 1);
1020static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1021 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +01001022static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001023 show_pwmchan, set_pwmchan, INPUT, 1);
1024static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1025 set_pwm, MIN, 1);
1026static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1027 set_pwm, MAX, 1);
1028static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1029 2);
1030static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1031 set_pwmfreq, INPUT, 2);
1032static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1033 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +01001034static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001035 show_pwmchan, set_pwmchan, INPUT, 2);
1036static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1037 set_pwm, MIN, 2);
1038static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1039 set_pwm, MAX, 2);
1040
Jean Delvaref99318b2009-12-09 20:36:03 +01001041/* Non-standard name, might need revisiting */
1042static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
1043 show_pwm_at_crit, set_pwm_at_crit);
1044
Jean Delvare54fe4672009-12-09 20:36:08 +01001045static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm);
1046static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1047
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001048static struct attribute *adt7475_attrs[] = {
1049 &sensor_dev_attr_in1_input.dev_attr.attr,
1050 &sensor_dev_attr_in1_max.dev_attr.attr,
1051 &sensor_dev_attr_in1_min.dev_attr.attr,
1052 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1053 &sensor_dev_attr_in2_input.dev_attr.attr,
1054 &sensor_dev_attr_in2_max.dev_attr.attr,
1055 &sensor_dev_attr_in2_min.dev_attr.attr,
1056 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1057 &sensor_dev_attr_temp1_input.dev_attr.attr,
1058 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1059 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1060 &sensor_dev_attr_temp1_max.dev_attr.attr,
1061 &sensor_dev_attr_temp1_min.dev_attr.attr,
1062 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1063 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1064 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1065 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1066 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1067 &sensor_dev_attr_temp2_input.dev_attr.attr,
1068 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1069 &sensor_dev_attr_temp2_max.dev_attr.attr,
1070 &sensor_dev_attr_temp2_min.dev_attr.attr,
1071 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1072 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1073 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1074 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1075 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1076 &sensor_dev_attr_temp3_input.dev_attr.attr,
1077 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1078 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1079 &sensor_dev_attr_temp3_max.dev_attr.attr,
1080 &sensor_dev_attr_temp3_min.dev_attr.attr,
1081 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1082 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1083 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1084 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1085 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1086 &sensor_dev_attr_fan1_input.dev_attr.attr,
1087 &sensor_dev_attr_fan1_min.dev_attr.attr,
1088 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1089 &sensor_dev_attr_fan2_input.dev_attr.attr,
1090 &sensor_dev_attr_fan2_min.dev_attr.attr,
1091 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1092 &sensor_dev_attr_fan3_input.dev_attr.attr,
1093 &sensor_dev_attr_fan3_min.dev_attr.attr,
1094 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001095 &sensor_dev_attr_pwm1.dev_attr.attr,
1096 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1097 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001098 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001099 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1100 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001101 &sensor_dev_attr_pwm3.dev_attr.attr,
1102 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1103 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001104 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001105 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1106 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001107 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001108 NULL,
1109};
1110
Jean Delvare378933c2009-12-09 20:36:06 +01001111static struct attribute *fan4_attrs[] = {
1112 &sensor_dev_attr_fan4_input.dev_attr.attr,
1113 &sensor_dev_attr_fan4_min.dev_attr.attr,
1114 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1115 NULL
1116};
1117
1118static struct attribute *pwm2_attrs[] = {
1119 &sensor_dev_attr_pwm2.dev_attr.attr,
1120 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1121 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1122 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1123 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1124 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1125 NULL
1126};
1127
Jean Delvare378933c2009-12-09 20:36:06 +01001128static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001129 &sensor_dev_attr_in0_input.dev_attr.attr,
1130 &sensor_dev_attr_in0_max.dev_attr.attr,
1131 &sensor_dev_attr_in0_min.dev_attr.attr,
1132 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001133 NULL
1134};
1135
Jean Delvared8d2ee02009-12-09 20:36:08 +01001136static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001137 &sensor_dev_attr_in3_input.dev_attr.attr,
1138 &sensor_dev_attr_in3_max.dev_attr.attr,
1139 &sensor_dev_attr_in3_min.dev_attr.attr,
1140 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001141 NULL
1142};
1143
1144static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001145 &sensor_dev_attr_in4_input.dev_attr.attr,
1146 &sensor_dev_attr_in4_max.dev_attr.attr,
1147 &sensor_dev_attr_in4_min.dev_attr.attr,
1148 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001149 NULL
1150};
1151
1152static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001153 &sensor_dev_attr_in5_input.dev_attr.attr,
1154 &sensor_dev_attr_in5_max.dev_attr.attr,
1155 &sensor_dev_attr_in5_min.dev_attr.attr,
1156 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1157 NULL
1158};
1159
Jean Delvare54fe4672009-12-09 20:36:08 +01001160static struct attribute *vid_attrs[] = {
1161 &dev_attr_cpu0_vid.attr,
1162 &dev_attr_vrm.attr,
1163 NULL
1164};
1165
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001166static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jean Delvare378933c2009-12-09 20:36:06 +01001167static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1168static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1169static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
Jean Delvared8d2ee02009-12-09 20:36:08 +01001170static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1171static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1172static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
Jean Delvare54fe4672009-12-09 20:36:08 +01001173static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001174
Jean Delvare310ec792009-12-14 21:17:23 +01001175static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001176 struct i2c_board_info *info)
1177{
1178 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001179 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001180 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001181
1182 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1183 return -ENODEV;
1184
Jean Delvareb180d052009-12-09 20:36:02 +01001185 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001186 devid2 = adt7475_read(REG_DEVID2);
1187 if (vendid != 0x41 || /* Analog Devices */
1188 (devid2 & 0xf8) != 0x68)
1189 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001190
Jean Delvared656b6f2009-12-09 20:36:04 +01001191 devid = adt7475_read(REG_DEVID);
1192 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001193 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001194 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001195 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001196 else if (devid == 0x76)
1197 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001198 else if ((devid2 & 0xfc) == 0x6c)
1199 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001200 else {
1201 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001202 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001203 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001204 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001205 }
1206
Jean Delvareb180d052009-12-09 20:36:02 +01001207 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001208
1209 return 0;
1210}
1211
Jean Delvare0f144802009-12-09 20:36:06 +01001212static void adt7475_remove_files(struct i2c_client *client,
1213 struct adt7475_data *data)
1214{
1215 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001216 if (data->has_fan4)
1217 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1218 if (data->has_pwm2)
1219 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1220 if (data->has_voltage & (1 << 0))
1221 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001222 if (data->has_voltage & (1 << 3))
1223 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1224 if (data->has_voltage & (1 << 4))
1225 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1226 if (data->has_voltage & (1 << 5))
1227 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001228 if (data->has_vid)
1229 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001230}
1231
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001232static int adt7475_probe(struct i2c_client *client,
1233 const struct i2c_device_id *id)
1234{
Jean Delvared07ca4a2009-12-09 20:36:07 +01001235 static const char *names[] = {
1236 [adt7473] = "ADT7473",
1237 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001238 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001239 [adt7490] = "ADT7490",
1240 };
1241
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001242 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001243 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001244 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001245
1246 data = kzalloc(sizeof(*data), GFP_KERNEL);
1247 if (data == NULL)
1248 return -ENOMEM;
1249
1250 mutex_init(&data->lock);
1251 i2c_set_clientdata(client, data);
1252
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001253 /* Initialize device-specific values */
1254 switch (id->driver_data) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001255 case adt7476:
1256 data->has_voltage = 0x0e; /* in1 to in3 */
1257 revision = adt7475_read(REG_DEVID2) & 0x07;
1258 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001259 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001260 data->has_voltage = 0x3e; /* in1 to in5 */
1261 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001262 if (revision == 0x03)
1263 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001264 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001265 default:
1266 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001267 revision = adt7475_read(REG_DEVID2) & 0x07;
1268 }
1269
1270 config3 = adt7475_read(REG_CONFIG3);
1271 /* Pin PWM2 may alternatively be used for ALERT output */
1272 if (!(config3 & CONFIG3_SMBALERT))
1273 data->has_pwm2 = 1;
1274 /* Meaning of this bit is inverted for the ADT7473-1 */
1275 if (id->driver_data == adt7473 && revision >= 1)
1276 data->has_pwm2 = !data->has_pwm2;
1277
1278 data->config4 = adt7475_read(REG_CONFIG4);
1279 /* Pin TACH4 may alternatively be used for THERM */
1280 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1281 data->has_fan4 = 1;
1282
Jean Delvared8d2ee02009-12-09 20:36:08 +01001283 /* THERM configuration is more complex on the ADT7476 and ADT7490,
1284 because 2 different pins (TACH4 and +2.5 Vin) can be used for
1285 this function */
Jean Delvare378933c2009-12-09 20:36:06 +01001286 if (id->driver_data == adt7490) {
1287 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1288 !(config3 & CONFIG3_THERM))
1289 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001290 }
1291 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001292 if (!(config3 & CONFIG3_THERM) ||
1293 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1294 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001295 }
1296
Jean Delvare54fe4672009-12-09 20:36:08 +01001297 /* On the ADT7476, the +12V input pin may instead be used as VID5,
1298 and VID pins may alternatively be used as GPIO */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001299 if (id->driver_data == adt7476) {
1300 u8 vid = adt7475_read(REG_VID);
1301 if (!(vid & VID_VIDSEL))
1302 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001303
1304 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001305 }
1306
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001307 /* Voltage attenuators can be bypassed, globally or individually */
1308 config2 = adt7475_read(REG_CONFIG2);
1309 if (config2 & CONFIG2_ATTN) {
1310 data->bypass_attn = (0x3 << 3) | 0x3;
1311 } else {
1312 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1313 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1314 }
1315 data->bypass_attn &= data->has_voltage;
1316
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001317 /* Call adt7475_read_pwm for all pwm's as this will reprogram any
1318 pwm's which are disabled to manual mode with 0% duty cycle */
1319 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1320 adt7475_read_pwm(client, i);
1321
1322 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1323 if (ret)
1324 goto efree;
1325
Jean Delvare378933c2009-12-09 20:36:06 +01001326 /* Features that can be disabled individually */
1327 if (data->has_fan4) {
1328 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1329 if (ret)
1330 goto eremove;
1331 }
1332 if (data->has_pwm2) {
1333 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1334 if (ret)
1335 goto eremove;
1336 }
1337 if (data->has_voltage & (1 << 0)) {
1338 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1339 if (ret)
1340 goto eremove;
1341 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001342 if (data->has_voltage & (1 << 3)) {
1343 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1344 if (ret)
1345 goto eremove;
1346 }
1347 if (data->has_voltage & (1 << 4)) {
1348 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1349 if (ret)
1350 goto eremove;
1351 }
1352 if (data->has_voltage & (1 << 5)) {
1353 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1354 if (ret)
1355 goto eremove;
1356 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001357 if (data->has_vid) {
1358 data->vrm = vid_which_vrm();
1359 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1360 if (ret)
1361 goto eremove;
1362 }
Jean Delvare378933c2009-12-09 20:36:06 +01001363
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001364 data->hwmon_dev = hwmon_device_register(&client->dev);
1365 if (IS_ERR(data->hwmon_dev)) {
1366 ret = PTR_ERR(data->hwmon_dev);
1367 goto eremove;
1368 }
1369
Jean Delvared07ca4a2009-12-09 20:36:07 +01001370 dev_info(&client->dev, "%s device, revision %d\n",
1371 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001372 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001373 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001374 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001375 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001376 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001377 data->has_pwm2 ? " pwm2" : "",
1378 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001379 if (data->bypass_attn)
1380 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1381 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1382 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1383 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1384 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001385
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001386 return 0;
1387
1388eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001389 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001390efree:
1391 kfree(data);
1392 return ret;
1393}
1394
1395static int adt7475_remove(struct i2c_client *client)
1396{
1397 struct adt7475_data *data = i2c_get_clientdata(client);
1398
1399 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001400 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001401 kfree(data);
1402
1403 return 0;
1404}
1405
1406static struct i2c_driver adt7475_driver = {
1407 .class = I2C_CLASS_HWMON,
1408 .driver = {
1409 .name = "adt7475",
1410 },
1411 .probe = adt7475_probe,
1412 .remove = adt7475_remove,
1413 .id_table = adt7475_id,
1414 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001415 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001416};
1417
1418static void adt7475_read_hystersis(struct i2c_client *client)
1419{
1420 struct adt7475_data *data = i2c_get_clientdata(client);
1421
1422 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1423 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1424 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1425}
1426
1427static void adt7475_read_pwm(struct i2c_client *client, int index)
1428{
1429 struct adt7475_data *data = i2c_get_clientdata(client);
1430 unsigned int v;
1431
1432 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1433
1434 /* Figure out the internal value for pwmctrl and pwmchan
1435 based on the current settings */
1436 v = (data->pwm[CONTROL][index] >> 5) & 7;
1437
1438 if (v == 3)
1439 data->pwmctl[index] = 0;
1440 else if (v == 7)
1441 data->pwmctl[index] = 1;
1442 else if (v == 4) {
1443 /* The fan is disabled - we don't want to
1444 support that, so change to manual mode and
1445 set the duty cycle to 0 instead
1446 */
1447 data->pwm[INPUT][index] = 0;
1448 data->pwm[CONTROL][index] &= ~0xE0;
1449 data->pwm[CONTROL][index] |= (7 << 5);
1450
1451 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1452 data->pwm[INPUT][index]);
1453
1454 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1455 data->pwm[CONTROL][index]);
1456
1457 data->pwmctl[index] = 1;
1458 } else {
1459 data->pwmctl[index] = 2;
1460
1461 switch (v) {
1462 case 0:
1463 data->pwmchan[index] = 1;
1464 break;
1465 case 1:
1466 data->pwmchan[index] = 2;
1467 break;
1468 case 2:
1469 data->pwmchan[index] = 4;
1470 break;
1471 case 5:
1472 data->pwmchan[index] = 6;
1473 break;
1474 case 6:
1475 data->pwmchan[index] = 7;
1476 break;
1477 }
1478 }
1479}
1480
1481static struct adt7475_data *adt7475_update_device(struct device *dev)
1482{
1483 struct i2c_client *client = to_i2c_client(dev);
1484 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001485 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001486 int i;
1487
1488 mutex_lock(&data->lock);
1489
1490 /* Measurement values update every 2 seconds */
1491 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1492 !data->valid) {
1493 data->alarms = adt7475_read(REG_STATUS2) << 8;
1494 data->alarms |= adt7475_read(REG_STATUS1);
1495
Jean Delvare3d849982009-12-09 20:36:05 +01001496 ext = (adt7475_read(REG_EXTEND2) << 8) |
1497 adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001498 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1499 if (!(data->has_voltage & (1 << i)))
1500 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001501 data->voltage[INPUT][i] =
1502 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001503 ((ext >> (i * 2)) & 3);
1504 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001505
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001506 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1507 data->temp[INPUT][i] =
1508 (adt7475_read(TEMP_REG(i)) << 2) |
Jean Delvare3d849982009-12-09 20:36:05 +01001509 ((ext >> ((i + 5) * 2)) & 3);
1510
1511 if (data->has_voltage & (1 << 5)) {
1512 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1513 ext = adt7475_read(REG_EXTEND3);
1514 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1515 ((ext >> 4) & 3);
1516 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001517
Jean Delvare378933c2009-12-09 20:36:06 +01001518 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1519 if (i == 3 && !data->has_fan4)
1520 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001521 data->tach[INPUT][i] =
1522 adt7475_read_word(client, TACH_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001523 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001524
1525 /* Updated by hw when in auto mode */
Jean Delvare378933c2009-12-09 20:36:06 +01001526 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1527 if (i == 1 && !data->has_pwm2)
1528 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001529 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001530 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001531
Jean Delvare54fe4672009-12-09 20:36:08 +01001532 if (data->has_vid)
1533 data->vid = adt7475_read(REG_VID) & 0x3f;
1534
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001535 data->measure_updated = jiffies;
1536 }
1537
1538 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001539 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001540 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001541 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001542 data->config5 = adt7475_read(REG_CONFIG5);
1543
1544 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001545 if (!(data->has_voltage & (1 << i)))
1546 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001547 /* Adjust values so they match the input precision */
1548 data->voltage[MIN][i] =
1549 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1550 data->voltage[MAX][i] =
1551 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1552 }
1553
Jean Delvare3d849982009-12-09 20:36:05 +01001554 if (data->has_voltage & (1 << 5)) {
1555 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1556 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1557 }
1558
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001559 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1560 /* Adjust values so they match the input precision */
1561 data->temp[MIN][i] =
1562 adt7475_read(TEMP_MIN_REG(i)) << 2;
1563 data->temp[MAX][i] =
1564 adt7475_read(TEMP_MAX_REG(i)) << 2;
1565 data->temp[AUTOMIN][i] =
1566 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1567 data->temp[THERM][i] =
1568 adt7475_read(TEMP_THERM_REG(i)) << 2;
1569 data->temp[OFFSET][i] =
1570 adt7475_read(TEMP_OFFSET_REG(i));
1571 }
1572 adt7475_read_hystersis(client);
1573
Jean Delvare378933c2009-12-09 20:36:06 +01001574 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1575 if (i == 3 && !data->has_fan4)
1576 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001577 data->tach[MIN][i] =
1578 adt7475_read_word(client, TACH_MIN_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001579 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001580
1581 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
Jean Delvare378933c2009-12-09 20:36:06 +01001582 if (i == 1 && !data->has_pwm2)
1583 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001584 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1585 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1586 /* Set the channel and control information */
1587 adt7475_read_pwm(client, i);
1588 }
1589
1590 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1591 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1592 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1593
1594 data->limits_updated = jiffies;
1595 data->valid = 1;
1596 }
1597
1598 mutex_unlock(&data->lock);
1599
1600 return data;
1601}
1602
1603static int __init sensors_adt7475_init(void)
1604{
1605 return i2c_add_driver(&adt7475_driver);
1606}
1607
1608static void __exit sensors_adt7475_exit(void)
1609{
1610 i2c_del_driver(&adt7475_driver);
1611}
1612
1613MODULE_AUTHOR("Advanced Micro Devices, Inc");
1614MODULE_DESCRIPTION("adt7475 driver");
1615MODULE_LICENSE("GPL");
1616
1617module_init(sensors_adt7475_init);
1618module_exit(sensors_adt7475_exit);