blob: 22d008bbdc1011d16919e70170f2f9d09b394235 [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>
Jean Delvaredcd8f392012-10-10 15:25:56 +020023#include <linux/jiffies.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010024
25/* Indexes for the sysfs hooks */
26
27#define INPUT 0
28#define MIN 1
29#define MAX 2
30#define CONTROL 3
31#define OFFSET 3
32#define AUTOMIN 4
33#define THERM 5
34#define HYSTERSIS 6
35
Guenter Roeck9ed5bc22012-01-19 11:02:15 -080036/*
37 * These are unique identifiers for the sysfs functions - unlike the
38 * numbers above, these are not also indexes into an array
39 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +010040
41#define ALARM 9
42#define FAULT 10
43
44/* 7475 Common Registers */
45
Jean Delvared07ca4a2009-12-09 20:36:07 +010046#define REG_DEVREV2 0x12 /* ADT7490 only */
47
Jean Delvare3d849982009-12-09 20:36:05 +010048#define REG_VTT 0x1E /* ADT7490 only */
49#define REG_EXTEND3 0x1F /* ADT7490 only */
50
Jean Delvarecffb9dd2009-12-09 20:36:03 +010051#define REG_VOLTAGE_BASE 0x20
Jordan Crouse1c301fc2009-01-15 22:27:47 +010052#define REG_TEMP_BASE 0x25
53#define REG_TACH_BASE 0x28
54#define REG_PWM_BASE 0x30
55#define REG_PWM_MAX_BASE 0x38
56
57#define REG_DEVID 0x3D
58#define REG_VENDID 0x3E
Jean Delvared656b6f2009-12-09 20:36:04 +010059#define REG_DEVID2 0x3F
Jordan Crouse1c301fc2009-01-15 22:27:47 +010060
61#define REG_STATUS1 0x41
62#define REG_STATUS2 0x42
63
Jean Delvared8d2ee02009-12-09 20:36:08 +010064#define REG_VID 0x43 /* ADT7476 only */
65
Jean Delvarecffb9dd2009-12-09 20:36:03 +010066#define REG_VOLTAGE_MIN_BASE 0x44
67#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010068
69#define REG_TEMP_MIN_BASE 0x4E
70#define REG_TEMP_MAX_BASE 0x4F
71
72#define REG_TACH_MIN_BASE 0x54
73
74#define REG_PWM_CONFIG_BASE 0x5C
75
76#define REG_TEMP_TRANGE_BASE 0x5F
77
78#define REG_PWM_MIN_BASE 0x64
79
80#define REG_TEMP_TMIN_BASE 0x67
81#define REG_TEMP_THERM_BASE 0x6A
82
83#define REG_REMOTE1_HYSTERSIS 0x6D
84#define REG_REMOTE2_HYSTERSIS 0x6E
85
86#define REG_TEMP_OFFSET_BASE 0x70
87
Jean Delvareebfaf1f2009-12-09 20:36:07 +010088#define REG_CONFIG2 0x73
89
Jordan Crouse1c301fc2009-01-15 22:27:47 +010090#define REG_EXTEND1 0x76
91#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010092
93#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +010094#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +010095#define REG_CONFIG4 0x7D
96
Jean Delvare3d849982009-12-09 20:36:05 +010097#define REG_STATUS4 0x81 /* ADT7490 only */
98
99#define REG_VTT_MIN 0x84 /* ADT7490 only */
100#define REG_VTT_MAX 0x86 /* ADT7490 only */
101
Jean Delvared8d2ee02009-12-09 20:36:08 +0100102#define VID_VIDSEL 0x80 /* ADT7476 only */
103
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100104#define CONFIG2_ATTN 0x20
105
Jean Delvare378933c2009-12-09 20:36:06 +0100106#define CONFIG3_SMBALERT 0x01
107#define CONFIG3_THERM 0x02
108
109#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100110#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100111#define CONFIG4_ATTN_IN10 0x30
112#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100113
114#define CONFIG5_TWOSCOMP 0x01
115#define CONFIG5_TEMPOFFSET 0x02
Jean Delvare54fe4672009-12-09 20:36:08 +0100116#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100117
118/* ADT7475 Settings */
119
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100120#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100121#define ADT7475_TEMP_COUNT 3
122#define ADT7475_TACH_COUNT 4
123#define ADT7475_PWM_COUNT 3
124
125/* Macro to read the registers */
126
127#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
128
129/* Macros to easily index the registers */
130
131#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
132#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
133
134#define PWM_REG(idx) (REG_PWM_BASE + (idx))
135#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
136#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
137#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
138
139#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
140#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
141#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
142
143#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
144#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
145#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
146#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
147#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
148#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
149#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
150
Jean Delvare918ee912010-10-28 20:31:50 +0200151static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100152
Jean Delvaree5e9f442009-12-14 21:17:27 +0100153enum chips { adt7473, adt7475, adt7476, adt7490 };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100154
155static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100156 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100157 { "adt7475", adt7475 },
Jean Delvared8d2ee02009-12-09 20:36:08 +0100158 { "adt7476", adt7476 },
Jean Delvare3d849982009-12-09 20:36:05 +0100159 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100160 { }
161};
162MODULE_DEVICE_TABLE(i2c, adt7475_id);
163
164struct adt7475_data {
165 struct device *hwmon_dev;
166 struct mutex lock;
167
168 unsigned long measure_updated;
169 unsigned long limits_updated;
170 char valid;
171
Jean Delvaref99318b2009-12-09 20:36:03 +0100172 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100173 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100174 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100175 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100176 u8 has_pwm2:1;
177 u8 has_fan4:1;
Jean Delvare54fe4672009-12-09 20:36:08 +0100178 u8 has_vid:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100179 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100180 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100181 u16 temp[7][3];
182 u16 tach[2][4];
183 u8 pwm[4][3];
184 u8 range[3];
185 u8 pwmctl[3];
186 u8 pwmchan[3];
Jean Delvare54fe4672009-12-09 20:36:08 +0100187
188 u8 vid;
189 u8 vrm;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100190};
191
192static struct i2c_driver adt7475_driver;
193static struct adt7475_data *adt7475_update_device(struct device *dev);
194static void adt7475_read_hystersis(struct i2c_client *client);
195static void adt7475_read_pwm(struct i2c_client *client, int index);
196
197/* Given a temp value, convert it to register value */
198
199static inline u16 temp2reg(struct adt7475_data *data, long val)
200{
201 u16 ret;
202
203 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800204 val = clamp_val(val, -64000, 191000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100205 ret = (val + 64500) / 1000;
206 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800207 val = clamp_val(val, -128000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100208 if (val < -500)
209 ret = (256500 + val) / 1000;
210 else
211 ret = (val + 500) / 1000;
212 }
213
214 return ret << 2;
215}
216
217/* Given a register value, convert it to a real temp value */
218
219static inline int reg2temp(struct adt7475_data *data, u16 reg)
220{
221 if (data->config5 & CONFIG5_TWOSCOMP) {
222 if (reg >= 512)
223 return (reg - 1024) * 250;
224 else
225 return reg * 250;
226 } else
227 return (reg - 256) * 250;
228}
229
230static inline int tach2rpm(u16 tach)
231{
232 if (tach == 0 || tach == 0xFFFF)
233 return 0;
234
235 return (90000 * 60) / tach;
236}
237
238static inline u16 rpm2tach(unsigned long rpm)
239{
240 if (rpm == 0)
241 return 0;
242
Guenter Roeck2a844c12013-01-09 08:09:34 -0800243 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100244}
245
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100246/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
247static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
248 { 45, 94 }, /* +2.5V */
249 { 175, 525 }, /* Vccp */
250 { 68, 71 }, /* Vcc */
251 { 93, 47 }, /* +5V */
252 { 120, 20 }, /* +12V */
253 { 45, 45 }, /* Vtt */
254};
255
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100256static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100257{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100258 const int *r = adt7473_in_scaling[channel];
259
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100260 if (bypass_attn & (1 << channel))
261 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100262 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100263}
264
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100265static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100266{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100267 const int *r = adt7473_in_scaling[channel];
268 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100269
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100270 if (bypass_attn & (1 << channel))
271 reg = (volt * 1024) / 2250;
272 else
273 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800274 return clamp_val(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100275}
276
277static u16 adt7475_read_word(struct i2c_client *client, int reg)
278{
279 u16 val;
280
281 val = i2c_smbus_read_byte_data(client, reg);
282 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
283
284 return val;
285}
286
287static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
288{
289 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
290 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
291}
292
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800293/*
294 * Find the nearest value in a table - used for pwm frequency and
295 * auto temp range
296 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100297static int find_nearest(long val, const int *array, int size)
298{
299 int i;
300
301 if (val < array[0])
302 return 0;
303
304 if (val > array[size - 1])
305 return size - 1;
306
307 for (i = 0; i < size - 1; i++) {
308 int a, b;
309
310 if (val > array[i + 1])
311 continue;
312
313 a = val - array[i];
314 b = array[i + 1] - val;
315
316 return (a <= b) ? i : i + 1;
317 }
318
319 return 0;
320}
321
322static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
323 char *buf)
324{
325 struct adt7475_data *data = adt7475_update_device(dev);
326 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
327 unsigned short val;
328
329 switch (sattr->nr) {
330 case ALARM:
331 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100332 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100333 default:
334 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100335 return sprintf(buf, "%d\n",
336 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100337 }
338}
339
340static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
341 const char *buf, size_t count)
342{
343
344 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
345 struct i2c_client *client = to_i2c_client(dev);
346 struct adt7475_data *data = i2c_get_clientdata(client);
347 unsigned char reg;
348 long val;
349
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100350 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100351 return -EINVAL;
352
353 mutex_lock(&data->lock);
354
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100355 data->voltage[sattr->nr][sattr->index] =
356 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100357
Jean Delvare3d849982009-12-09 20:36:05 +0100358 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
359 if (sattr->nr == MIN)
360 reg = VOLTAGE_MIN_REG(sattr->index);
361 else
362 reg = VOLTAGE_MAX_REG(sattr->index);
363 } else {
364 if (sattr->nr == MIN)
365 reg = REG_VTT_MIN;
366 else
367 reg = REG_VTT_MAX;
368 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100369
370 i2c_smbus_write_byte_data(client, reg,
371 data->voltage[sattr->nr][sattr->index] >> 2);
372 mutex_unlock(&data->lock);
373
374 return count;
375}
376
377static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
378 char *buf)
379{
380 struct adt7475_data *data = adt7475_update_device(dev);
381 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
382 int out;
383
384 switch (sattr->nr) {
385 case HYSTERSIS:
386 mutex_lock(&data->lock);
387 out = data->temp[sattr->nr][sattr->index];
388 if (sattr->index != 1)
389 out = (out >> 4) & 0xF;
390 else
391 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800392 /*
393 * Show the value as an absolute number tied to
394 * THERM
395 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100396 out = reg2temp(data, data->temp[THERM][sattr->index]) -
397 out * 1000;
398 mutex_unlock(&data->lock);
399 break;
400
401 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800402 /*
403 * Offset is always 2's complement, regardless of the
404 * setting in CONFIG5
405 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100406 mutex_lock(&data->lock);
407 out = (s8)data->temp[sattr->nr][sattr->index];
408 if (data->config5 & CONFIG5_TEMPOFFSET)
409 out *= 1000;
410 else
411 out *= 500;
412 mutex_unlock(&data->lock);
413 break;
414
415 case ALARM:
416 out = (data->alarms >> (sattr->index + 4)) & 1;
417 break;
418
419 case FAULT:
420 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100421 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100422 break;
423
424 default:
425 /* All other temp values are in the configured format */
426 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
427 }
428
429 return sprintf(buf, "%d\n", out);
430}
431
432static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
433 const char *buf, size_t count)
434{
435 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
436 struct i2c_client *client = to_i2c_client(dev);
437 struct adt7475_data *data = i2c_get_clientdata(client);
438 unsigned char reg = 0;
439 u8 out;
440 int temp;
441 long val;
442
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100443 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100444 return -EINVAL;
445
446 mutex_lock(&data->lock);
447
448 /* We need the config register in all cases for temp <-> reg conv. */
449 data->config5 = adt7475_read(REG_CONFIG5);
450
451 switch (sattr->nr) {
452 case OFFSET:
453 if (data->config5 & CONFIG5_TEMPOFFSET) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800454 val = clamp_val(val, -63000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100455 out = data->temp[OFFSET][sattr->index] = val / 1000;
456 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800457 val = clamp_val(val, -63000, 64000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100458 out = data->temp[OFFSET][sattr->index] = val / 500;
459 }
460 break;
461
462 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800463 /*
464 * The value will be given as an absolute value, turn it
465 * into an offset based on THERM
466 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100467
468 /* Read fresh THERM and HYSTERSIS values from the chip */
469 data->temp[THERM][sattr->index] =
470 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
471 adt7475_read_hystersis(client);
472
473 temp = reg2temp(data, data->temp[THERM][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800474 val = clamp_val(val, temp - 15000, temp);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100475 val = (temp - val) / 1000;
476
477 if (sattr->index != 1) {
478 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
479 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
480 } else {
481 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
482 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
483 }
484
485 out = data->temp[HYSTERSIS][sattr->index];
486 break;
487
488 default:
489 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
490
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800491 /*
492 * We maintain an extra 2 digits of precision for simplicity
493 * - shift those back off before writing the value
494 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100495 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
496 }
497
498 switch (sattr->nr) {
499 case MIN:
500 reg = TEMP_MIN_REG(sattr->index);
501 break;
502 case MAX:
503 reg = TEMP_MAX_REG(sattr->index);
504 break;
505 case OFFSET:
506 reg = TEMP_OFFSET_REG(sattr->index);
507 break;
508 case AUTOMIN:
509 reg = TEMP_TMIN_REG(sattr->index);
510 break;
511 case THERM:
512 reg = TEMP_THERM_REG(sattr->index);
513 break;
514 case HYSTERSIS:
515 if (sattr->index != 2)
516 reg = REG_REMOTE1_HYSTERSIS;
517 else
518 reg = REG_REMOTE2_HYSTERSIS;
519
520 break;
521 }
522
523 i2c_smbus_write_byte_data(client, reg, out);
524
525 mutex_unlock(&data->lock);
526 return count;
527}
528
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800529/*
530 * Table of autorange values - the user will write the value in millidegrees,
531 * and we'll convert it
532 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100533static const int autorange_table[] = {
534 2000, 2500, 3330, 4000, 5000, 6670, 8000,
535 10000, 13330, 16000, 20000, 26670, 32000, 40000,
536 53330, 80000
537};
538
539static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
540 char *buf)
541{
542 struct adt7475_data *data = adt7475_update_device(dev);
543 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
544 int out, val;
545
546 mutex_lock(&data->lock);
547 out = (data->range[sattr->index] >> 4) & 0x0F;
548 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
549 mutex_unlock(&data->lock);
550
551 return sprintf(buf, "%d\n", val + autorange_table[out]);
552}
553
554static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
555 const char *buf, size_t count)
556{
557 struct i2c_client *client = to_i2c_client(dev);
558 struct adt7475_data *data = i2c_get_clientdata(client);
559 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
560 int temp;
561 long val;
562
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100563 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100564 return -EINVAL;
565
566 mutex_lock(&data->lock);
567
568 /* Get a fresh copy of the needed registers */
569 data->config5 = adt7475_read(REG_CONFIG5);
570 data->temp[AUTOMIN][sattr->index] =
571 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
572 data->range[sattr->index] =
573 adt7475_read(TEMP_TRANGE_REG(sattr->index));
574
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800575 /*
576 * The user will write an absolute value, so subtract the start point
577 * to figure the range
578 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100579 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800580 val = clamp_val(val, temp + autorange_table[0],
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100581 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
582 val -= temp;
583
584 /* Find the nearest table entry to what the user wrote */
585 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
586
587 data->range[sattr->index] &= ~0xF0;
588 data->range[sattr->index] |= val << 4;
589
590 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
591 data->range[sattr->index]);
592
593 mutex_unlock(&data->lock);
594 return count;
595}
596
597static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
598 char *buf)
599{
600 struct adt7475_data *data = adt7475_update_device(dev);
601 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
602 int out;
603
604 if (sattr->nr == ALARM)
605 out = (data->alarms >> (sattr->index + 10)) & 1;
606 else
607 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
608
609 return sprintf(buf, "%d\n", out);
610}
611
612static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
613 const char *buf, size_t count)
614{
615
616 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
617 struct i2c_client *client = to_i2c_client(dev);
618 struct adt7475_data *data = i2c_get_clientdata(client);
619 unsigned long val;
620
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100621 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100622 return -EINVAL;
623
624 mutex_lock(&data->lock);
625
626 data->tach[MIN][sattr->index] = rpm2tach(val);
627
628 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
629 data->tach[MIN][sattr->index]);
630
631 mutex_unlock(&data->lock);
632 return count;
633}
634
635static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
636 char *buf)
637{
638 struct adt7475_data *data = adt7475_update_device(dev);
639 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
640
641 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
642}
643
644static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
645 char *buf)
646{
647 struct adt7475_data *data = adt7475_update_device(dev);
648 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
649
650 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
651}
652
653static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
654 char *buf)
655{
656 struct adt7475_data *data = adt7475_update_device(dev);
657 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
658
659 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
660}
661
662static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
663 const char *buf, size_t count)
664{
665
666 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
667 struct i2c_client *client = to_i2c_client(dev);
668 struct adt7475_data *data = i2c_get_clientdata(client);
669 unsigned char reg = 0;
670 long val;
671
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100672 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100673 return -EINVAL;
674
675 mutex_lock(&data->lock);
676
677 switch (sattr->nr) {
678 case INPUT:
679 /* Get a fresh value for CONTROL */
680 data->pwm[CONTROL][sattr->index] =
681 adt7475_read(PWM_CONFIG_REG(sattr->index));
682
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800683 /*
684 * If we are not in manual mode, then we shouldn't allow
685 * the user to set the pwm speed
686 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100687 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
688 mutex_unlock(&data->lock);
689 return count;
690 }
691
692 reg = PWM_REG(sattr->index);
693 break;
694
695 case MIN:
696 reg = PWM_MIN_REG(sattr->index);
697 break;
698
699 case MAX:
700 reg = PWM_MAX_REG(sattr->index);
701 break;
702 }
703
Guenter Roeck2a844c12013-01-09 08:09:34 -0800704 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100705 i2c_smbus_write_byte_data(client, reg,
706 data->pwm[sattr->nr][sattr->index]);
707
708 mutex_unlock(&data->lock);
709
710 return count;
711}
712
713/* Called by set_pwmctrl and set_pwmchan */
714
715static int hw_set_pwm(struct i2c_client *client, int index,
716 unsigned int pwmctl, unsigned int pwmchan)
717{
718 struct adt7475_data *data = i2c_get_clientdata(client);
719 long val = 0;
720
721 switch (pwmctl) {
722 case 0:
723 val = 0x03; /* Run at full speed */
724 break;
725 case 1:
726 val = 0x07; /* Manual mode */
727 break;
728 case 2:
729 switch (pwmchan) {
730 case 1:
731 /* Remote1 controls PWM */
732 val = 0x00;
733 break;
734 case 2:
735 /* local controls PWM */
736 val = 0x01;
737 break;
738 case 4:
739 /* remote2 controls PWM */
740 val = 0x02;
741 break;
742 case 6:
743 /* local/remote2 control PWM */
744 val = 0x05;
745 break;
746 case 7:
747 /* All three control PWM */
748 val = 0x06;
749 break;
750 default:
751 return -EINVAL;
752 }
753 break;
754 default:
755 return -EINVAL;
756 }
757
758 data->pwmctl[index] = pwmctl;
759 data->pwmchan[index] = pwmchan;
760
761 data->pwm[CONTROL][index] &= ~0xE0;
762 data->pwm[CONTROL][index] |= (val & 7) << 5;
763
764 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
765 data->pwm[CONTROL][index]);
766
767 return 0;
768}
769
770static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
771 const char *buf, size_t count)
772{
773 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
774 struct i2c_client *client = to_i2c_client(dev);
775 struct adt7475_data *data = i2c_get_clientdata(client);
776 int r;
777 long val;
778
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100779 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100780 return -EINVAL;
781
782 mutex_lock(&data->lock);
783 /* Read Modify Write PWM values */
784 adt7475_read_pwm(client, sattr->index);
785 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
786 if (r)
787 count = r;
788 mutex_unlock(&data->lock);
789
790 return count;
791}
792
793static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t count)
795{
796 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
797 struct i2c_client *client = to_i2c_client(dev);
798 struct adt7475_data *data = i2c_get_clientdata(client);
799 int r;
800 long val;
801
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100802 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100803 return -EINVAL;
804
805 mutex_lock(&data->lock);
806 /* Read Modify Write PWM values */
807 adt7475_read_pwm(client, sattr->index);
808 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
809 if (r)
810 count = r;
811 mutex_unlock(&data->lock);
812
813 return count;
814}
815
816/* List of frequencies for the PWM */
817static const int pwmfreq_table[] = {
818 11, 14, 22, 29, 35, 44, 58, 88
819};
820
821static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
822 char *buf)
823{
824 struct adt7475_data *data = adt7475_update_device(dev);
825 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
826
827 return sprintf(buf, "%d\n",
828 pwmfreq_table[data->range[sattr->index] & 7]);
829}
830
831static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
832 const char *buf, size_t count)
833{
834 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
835 struct i2c_client *client = to_i2c_client(dev);
836 struct adt7475_data *data = i2c_get_clientdata(client);
837 int out;
838 long val;
839
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100840 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100841 return -EINVAL;
842
843 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
844
845 mutex_lock(&data->lock);
846
847 data->range[sattr->index] =
848 adt7475_read(TEMP_TRANGE_REG(sattr->index));
849 data->range[sattr->index] &= ~7;
850 data->range[sattr->index] |= out;
851
852 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
853 data->range[sattr->index]);
854
855 mutex_unlock(&data->lock);
856 return count;
857}
858
Jean Delvaref99318b2009-12-09 20:36:03 +0100859static ssize_t show_pwm_at_crit(struct device *dev,
860 struct device_attribute *devattr, char *buf)
861{
862 struct adt7475_data *data = adt7475_update_device(dev);
863 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
864}
865
866static ssize_t set_pwm_at_crit(struct device *dev,
867 struct device_attribute *devattr,
868 const char *buf, size_t count)
869{
870 struct i2c_client *client = to_i2c_client(dev);
871 struct adt7475_data *data = i2c_get_clientdata(client);
872 long val;
873
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100874 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +0100875 return -EINVAL;
876 if (val != 0 && val != 1)
877 return -EINVAL;
878
879 mutex_lock(&data->lock);
880 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
881 if (val)
882 data->config4 |= CONFIG4_MAXDUTY;
883 else
884 data->config4 &= ~CONFIG4_MAXDUTY;
885 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
886 mutex_unlock(&data->lock);
887
888 return count;
889}
890
Jean Delvare54fe4672009-12-09 20:36:08 +0100891static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr,
892 char *buf)
893{
894 struct adt7475_data *data = dev_get_drvdata(dev);
895 return sprintf(buf, "%d\n", (int)data->vrm);
896}
897
898static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr,
899 const char *buf, size_t count)
900{
901 struct adt7475_data *data = dev_get_drvdata(dev);
902 long val;
903
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100904 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +0100905 return -EINVAL;
906 if (val < 0 || val > 255)
907 return -EINVAL;
908 data->vrm = val;
909
910 return count;
911}
912
913static ssize_t show_vid(struct device *dev, struct device_attribute *devattr,
914 char *buf)
915{
916 struct adt7475_data *data = adt7475_update_device(dev);
917 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
918}
919
Jean Delvare3d849982009-12-09 20:36:05 +0100920static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
921static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
922 set_voltage, MAX, 0);
923static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
924 set_voltage, MIN, 0);
925static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100926static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100927static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100928 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100929static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100930 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100931static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
932static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
933static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
934 set_voltage, MAX, 2);
935static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
936 set_voltage, MIN, 2);
937static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +0100938static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
939static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
940 set_voltage, MAX, 3);
941static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
942 set_voltage, MIN, 3);
943static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
944static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
945static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
946 set_voltage, MAX, 4);
947static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
948 set_voltage, MIN, 4);
949static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
950static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
951static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
952 set_voltage, MAX, 5);
953static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
954 set_voltage, MIN, 5);
955static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100956static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
957static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
958static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
959static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
960 MAX, 0);
961static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
962 MIN, 0);
963static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
964 set_temp, OFFSET, 0);
965static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
966 show_temp, set_temp, AUTOMIN, 0);
967static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
968 show_point2, set_point2, 0, 0);
969static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
970 THERM, 0);
971static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
972 set_temp, HYSTERSIS, 0);
973static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
974static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
975static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
976 MAX, 1);
977static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
978 MIN, 1);
979static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
980 set_temp, OFFSET, 1);
981static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
982 show_temp, set_temp, AUTOMIN, 1);
983static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
984 show_point2, set_point2, 0, 1);
985static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
986 THERM, 1);
987static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
988 set_temp, HYSTERSIS, 1);
989static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
990static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
991static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
992static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
993 MAX, 2);
994static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
995 MIN, 2);
996static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
997 set_temp, OFFSET, 2);
998static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
999 show_temp, set_temp, AUTOMIN, 2);
1000static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
1001 show_point2, set_point2, 0, 2);
1002static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1003 THERM, 2);
1004static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1005 set_temp, HYSTERSIS, 2);
1006static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1007static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1008 MIN, 0);
1009static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1010static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1011static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1012 MIN, 1);
1013static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1014static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1015static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1016 MIN, 2);
1017static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1018static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1019static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1020 MIN, 3);
1021static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1022static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1023 0);
1024static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1025 set_pwmfreq, INPUT, 0);
1026static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1027 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +01001028static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001029 show_pwmchan, set_pwmchan, INPUT, 0);
1030static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1031 set_pwm, MIN, 0);
1032static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1033 set_pwm, MAX, 0);
1034static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1035 1);
1036static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1037 set_pwmfreq, INPUT, 1);
1038static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1039 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +01001040static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001041 show_pwmchan, set_pwmchan, INPUT, 1);
1042static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1043 set_pwm, MIN, 1);
1044static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1045 set_pwm, MAX, 1);
1046static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1047 2);
1048static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1049 set_pwmfreq, INPUT, 2);
1050static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1051 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +01001052static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001053 show_pwmchan, set_pwmchan, INPUT, 2);
1054static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1055 set_pwm, MIN, 2);
1056static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1057 set_pwm, MAX, 2);
1058
Jean Delvaref99318b2009-12-09 20:36:03 +01001059/* Non-standard name, might need revisiting */
1060static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
1061 show_pwm_at_crit, set_pwm_at_crit);
1062
Jean Delvare54fe4672009-12-09 20:36:08 +01001063static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm);
1064static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1065
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001066static struct attribute *adt7475_attrs[] = {
1067 &sensor_dev_attr_in1_input.dev_attr.attr,
1068 &sensor_dev_attr_in1_max.dev_attr.attr,
1069 &sensor_dev_attr_in1_min.dev_attr.attr,
1070 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1071 &sensor_dev_attr_in2_input.dev_attr.attr,
1072 &sensor_dev_attr_in2_max.dev_attr.attr,
1073 &sensor_dev_attr_in2_min.dev_attr.attr,
1074 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1075 &sensor_dev_attr_temp1_input.dev_attr.attr,
1076 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1077 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1078 &sensor_dev_attr_temp1_max.dev_attr.attr,
1079 &sensor_dev_attr_temp1_min.dev_attr.attr,
1080 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1081 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1082 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1083 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1084 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1085 &sensor_dev_attr_temp2_input.dev_attr.attr,
1086 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1087 &sensor_dev_attr_temp2_max.dev_attr.attr,
1088 &sensor_dev_attr_temp2_min.dev_attr.attr,
1089 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1090 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1091 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1092 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1093 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1094 &sensor_dev_attr_temp3_input.dev_attr.attr,
1095 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1096 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1097 &sensor_dev_attr_temp3_max.dev_attr.attr,
1098 &sensor_dev_attr_temp3_min.dev_attr.attr,
1099 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1100 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1101 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1102 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1103 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1104 &sensor_dev_attr_fan1_input.dev_attr.attr,
1105 &sensor_dev_attr_fan1_min.dev_attr.attr,
1106 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1107 &sensor_dev_attr_fan2_input.dev_attr.attr,
1108 &sensor_dev_attr_fan2_min.dev_attr.attr,
1109 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1110 &sensor_dev_attr_fan3_input.dev_attr.attr,
1111 &sensor_dev_attr_fan3_min.dev_attr.attr,
1112 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001113 &sensor_dev_attr_pwm1.dev_attr.attr,
1114 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1115 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001116 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001117 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1118 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001119 &sensor_dev_attr_pwm3.dev_attr.attr,
1120 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1121 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001122 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001123 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1124 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001125 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001126 NULL,
1127};
1128
Jean Delvare378933c2009-12-09 20:36:06 +01001129static struct attribute *fan4_attrs[] = {
1130 &sensor_dev_attr_fan4_input.dev_attr.attr,
1131 &sensor_dev_attr_fan4_min.dev_attr.attr,
1132 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1133 NULL
1134};
1135
1136static struct attribute *pwm2_attrs[] = {
1137 &sensor_dev_attr_pwm2.dev_attr.attr,
1138 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1139 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1140 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1141 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1142 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1143 NULL
1144};
1145
Jean Delvare378933c2009-12-09 20:36:06 +01001146static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001147 &sensor_dev_attr_in0_input.dev_attr.attr,
1148 &sensor_dev_attr_in0_max.dev_attr.attr,
1149 &sensor_dev_attr_in0_min.dev_attr.attr,
1150 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001151 NULL
1152};
1153
Jean Delvared8d2ee02009-12-09 20:36:08 +01001154static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001155 &sensor_dev_attr_in3_input.dev_attr.attr,
1156 &sensor_dev_attr_in3_max.dev_attr.attr,
1157 &sensor_dev_attr_in3_min.dev_attr.attr,
1158 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001159 NULL
1160};
1161
1162static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001163 &sensor_dev_attr_in4_input.dev_attr.attr,
1164 &sensor_dev_attr_in4_max.dev_attr.attr,
1165 &sensor_dev_attr_in4_min.dev_attr.attr,
1166 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001167 NULL
1168};
1169
1170static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001171 &sensor_dev_attr_in5_input.dev_attr.attr,
1172 &sensor_dev_attr_in5_max.dev_attr.attr,
1173 &sensor_dev_attr_in5_min.dev_attr.attr,
1174 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1175 NULL
1176};
1177
Jean Delvare54fe4672009-12-09 20:36:08 +01001178static struct attribute *vid_attrs[] = {
1179 &dev_attr_cpu0_vid.attr,
1180 &dev_attr_vrm.attr,
1181 NULL
1182};
1183
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001184static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jean Delvare378933c2009-12-09 20:36:06 +01001185static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1186static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1187static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
Jean Delvared8d2ee02009-12-09 20:36:08 +01001188static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1189static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1190static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
Jean Delvare54fe4672009-12-09 20:36:08 +01001191static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001192
Jean Delvare310ec792009-12-14 21:17:23 +01001193static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001194 struct i2c_board_info *info)
1195{
1196 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001197 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001198 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001199
1200 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1201 return -ENODEV;
1202
Jean Delvareb180d052009-12-09 20:36:02 +01001203 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001204 devid2 = adt7475_read(REG_DEVID2);
1205 if (vendid != 0x41 || /* Analog Devices */
1206 (devid2 & 0xf8) != 0x68)
1207 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001208
Jean Delvared656b6f2009-12-09 20:36:04 +01001209 devid = adt7475_read(REG_DEVID);
1210 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001211 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001212 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001213 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001214 else if (devid == 0x76)
1215 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001216 else if ((devid2 & 0xfc) == 0x6c)
1217 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001218 else {
1219 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001220 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001221 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001222 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001223 }
1224
Jean Delvareb180d052009-12-09 20:36:02 +01001225 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001226
1227 return 0;
1228}
1229
Jean Delvare0f144802009-12-09 20:36:06 +01001230static void adt7475_remove_files(struct i2c_client *client,
1231 struct adt7475_data *data)
1232{
1233 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001234 if (data->has_fan4)
1235 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1236 if (data->has_pwm2)
1237 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1238 if (data->has_voltage & (1 << 0))
1239 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001240 if (data->has_voltage & (1 << 3))
1241 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1242 if (data->has_voltage & (1 << 4))
1243 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1244 if (data->has_voltage & (1 << 5))
1245 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001246 if (data->has_vid)
1247 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001248}
1249
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001250static int adt7475_probe(struct i2c_client *client,
1251 const struct i2c_device_id *id)
1252{
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001253 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001254 [adt7473] = "ADT7473",
1255 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001256 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001257 [adt7490] = "ADT7490",
1258 };
1259
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001260 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001261 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001262 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001263
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001264 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001265 if (data == NULL)
1266 return -ENOMEM;
1267
1268 mutex_init(&data->lock);
1269 i2c_set_clientdata(client, data);
1270
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001271 /* Initialize device-specific values */
1272 switch (id->driver_data) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001273 case adt7476:
1274 data->has_voltage = 0x0e; /* in1 to in3 */
1275 revision = adt7475_read(REG_DEVID2) & 0x07;
1276 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001277 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001278 data->has_voltage = 0x3e; /* in1 to in5 */
1279 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001280 if (revision == 0x03)
1281 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001282 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001283 default:
1284 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001285 revision = adt7475_read(REG_DEVID2) & 0x07;
1286 }
1287
1288 config3 = adt7475_read(REG_CONFIG3);
1289 /* Pin PWM2 may alternatively be used for ALERT output */
1290 if (!(config3 & CONFIG3_SMBALERT))
1291 data->has_pwm2 = 1;
1292 /* Meaning of this bit is inverted for the ADT7473-1 */
1293 if (id->driver_data == adt7473 && revision >= 1)
1294 data->has_pwm2 = !data->has_pwm2;
1295
1296 data->config4 = adt7475_read(REG_CONFIG4);
1297 /* Pin TACH4 may alternatively be used for THERM */
1298 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1299 data->has_fan4 = 1;
1300
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001301 /*
1302 * THERM configuration is more complex on the ADT7476 and ADT7490,
1303 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1304 * this function
1305 */
Jean Delvare378933c2009-12-09 20:36:06 +01001306 if (id->driver_data == adt7490) {
1307 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1308 !(config3 & CONFIG3_THERM))
1309 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001310 }
1311 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001312 if (!(config3 & CONFIG3_THERM) ||
1313 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1314 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001315 }
1316
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001317 /*
1318 * On the ADT7476, the +12V input pin may instead be used as VID5,
1319 * and VID pins may alternatively be used as GPIO
1320 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001321 if (id->driver_data == adt7476) {
1322 u8 vid = adt7475_read(REG_VID);
1323 if (!(vid & VID_VIDSEL))
1324 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001325
1326 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001327 }
1328
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001329 /* Voltage attenuators can be bypassed, globally or individually */
1330 config2 = adt7475_read(REG_CONFIG2);
1331 if (config2 & CONFIG2_ATTN) {
1332 data->bypass_attn = (0x3 << 3) | 0x3;
1333 } else {
1334 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1335 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1336 }
1337 data->bypass_attn &= data->has_voltage;
1338
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001339 /*
1340 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1341 * pwm's which are disabled to manual mode with 0% duty cycle
1342 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001343 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1344 adt7475_read_pwm(client, i);
1345
1346 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1347 if (ret)
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001348 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001349
Jean Delvare378933c2009-12-09 20:36:06 +01001350 /* Features that can be disabled individually */
1351 if (data->has_fan4) {
1352 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1353 if (ret)
1354 goto eremove;
1355 }
1356 if (data->has_pwm2) {
1357 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1358 if (ret)
1359 goto eremove;
1360 }
1361 if (data->has_voltage & (1 << 0)) {
1362 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1363 if (ret)
1364 goto eremove;
1365 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001366 if (data->has_voltage & (1 << 3)) {
1367 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1368 if (ret)
1369 goto eremove;
1370 }
1371 if (data->has_voltage & (1 << 4)) {
1372 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1373 if (ret)
1374 goto eremove;
1375 }
1376 if (data->has_voltage & (1 << 5)) {
1377 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1378 if (ret)
1379 goto eremove;
1380 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001381 if (data->has_vid) {
1382 data->vrm = vid_which_vrm();
1383 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1384 if (ret)
1385 goto eremove;
1386 }
Jean Delvare378933c2009-12-09 20:36:06 +01001387
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001388 data->hwmon_dev = hwmon_device_register(&client->dev);
1389 if (IS_ERR(data->hwmon_dev)) {
1390 ret = PTR_ERR(data->hwmon_dev);
1391 goto eremove;
1392 }
1393
Jean Delvared07ca4a2009-12-09 20:36:07 +01001394 dev_info(&client->dev, "%s device, revision %d\n",
1395 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001396 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001397 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001398 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001399 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001400 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001401 data->has_pwm2 ? " pwm2" : "",
1402 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001403 if (data->bypass_attn)
1404 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1405 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1406 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1407 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1408 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001409
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001410 return 0;
1411
1412eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001413 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001414 return ret;
1415}
1416
1417static int adt7475_remove(struct i2c_client *client)
1418{
1419 struct adt7475_data *data = i2c_get_clientdata(client);
1420
1421 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001422 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001423
1424 return 0;
1425}
1426
1427static struct i2c_driver adt7475_driver = {
1428 .class = I2C_CLASS_HWMON,
1429 .driver = {
1430 .name = "adt7475",
1431 },
1432 .probe = adt7475_probe,
1433 .remove = adt7475_remove,
1434 .id_table = adt7475_id,
1435 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001436 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001437};
1438
1439static void adt7475_read_hystersis(struct i2c_client *client)
1440{
1441 struct adt7475_data *data = i2c_get_clientdata(client);
1442
1443 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1444 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1445 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1446}
1447
1448static void adt7475_read_pwm(struct i2c_client *client, int index)
1449{
1450 struct adt7475_data *data = i2c_get_clientdata(client);
1451 unsigned int v;
1452
1453 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1454
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001455 /*
1456 * Figure out the internal value for pwmctrl and pwmchan
1457 * based on the current settings
1458 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001459 v = (data->pwm[CONTROL][index] >> 5) & 7;
1460
1461 if (v == 3)
1462 data->pwmctl[index] = 0;
1463 else if (v == 7)
1464 data->pwmctl[index] = 1;
1465 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001466 /*
1467 * The fan is disabled - we don't want to
1468 * support that, so change to manual mode and
1469 * set the duty cycle to 0 instead
1470 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001471 data->pwm[INPUT][index] = 0;
1472 data->pwm[CONTROL][index] &= ~0xE0;
1473 data->pwm[CONTROL][index] |= (7 << 5);
1474
1475 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1476 data->pwm[INPUT][index]);
1477
1478 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1479 data->pwm[CONTROL][index]);
1480
1481 data->pwmctl[index] = 1;
1482 } else {
1483 data->pwmctl[index] = 2;
1484
1485 switch (v) {
1486 case 0:
1487 data->pwmchan[index] = 1;
1488 break;
1489 case 1:
1490 data->pwmchan[index] = 2;
1491 break;
1492 case 2:
1493 data->pwmchan[index] = 4;
1494 break;
1495 case 5:
1496 data->pwmchan[index] = 6;
1497 break;
1498 case 6:
1499 data->pwmchan[index] = 7;
1500 break;
1501 }
1502 }
1503}
1504
1505static struct adt7475_data *adt7475_update_device(struct device *dev)
1506{
1507 struct i2c_client *client = to_i2c_client(dev);
1508 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001509 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001510 int i;
1511
1512 mutex_lock(&data->lock);
1513
1514 /* Measurement values update every 2 seconds */
1515 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1516 !data->valid) {
1517 data->alarms = adt7475_read(REG_STATUS2) << 8;
1518 data->alarms |= adt7475_read(REG_STATUS1);
1519
Jean Delvare3d849982009-12-09 20:36:05 +01001520 ext = (adt7475_read(REG_EXTEND2) << 8) |
1521 adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001522 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1523 if (!(data->has_voltage & (1 << i)))
1524 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001525 data->voltage[INPUT][i] =
1526 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001527 ((ext >> (i * 2)) & 3);
1528 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001529
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001530 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1531 data->temp[INPUT][i] =
1532 (adt7475_read(TEMP_REG(i)) << 2) |
Jean Delvare3d849982009-12-09 20:36:05 +01001533 ((ext >> ((i + 5) * 2)) & 3);
1534
1535 if (data->has_voltage & (1 << 5)) {
1536 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1537 ext = adt7475_read(REG_EXTEND3);
1538 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1539 ((ext >> 4) & 3);
1540 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001541
Jean Delvare378933c2009-12-09 20:36:06 +01001542 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1543 if (i == 3 && !data->has_fan4)
1544 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001545 data->tach[INPUT][i] =
1546 adt7475_read_word(client, TACH_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001547 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001548
1549 /* Updated by hw when in auto mode */
Jean Delvare378933c2009-12-09 20:36:06 +01001550 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1551 if (i == 1 && !data->has_pwm2)
1552 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001553 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001554 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001555
Jean Delvare54fe4672009-12-09 20:36:08 +01001556 if (data->has_vid)
1557 data->vid = adt7475_read(REG_VID) & 0x3f;
1558
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001559 data->measure_updated = jiffies;
1560 }
1561
1562 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001563 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001564 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001565 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001566 data->config5 = adt7475_read(REG_CONFIG5);
1567
1568 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001569 if (!(data->has_voltage & (1 << i)))
1570 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001571 /* Adjust values so they match the input precision */
1572 data->voltage[MIN][i] =
1573 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1574 data->voltage[MAX][i] =
1575 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1576 }
1577
Jean Delvare3d849982009-12-09 20:36:05 +01001578 if (data->has_voltage & (1 << 5)) {
1579 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1580 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1581 }
1582
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001583 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1584 /* Adjust values so they match the input precision */
1585 data->temp[MIN][i] =
1586 adt7475_read(TEMP_MIN_REG(i)) << 2;
1587 data->temp[MAX][i] =
1588 adt7475_read(TEMP_MAX_REG(i)) << 2;
1589 data->temp[AUTOMIN][i] =
1590 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1591 data->temp[THERM][i] =
1592 adt7475_read(TEMP_THERM_REG(i)) << 2;
1593 data->temp[OFFSET][i] =
1594 adt7475_read(TEMP_OFFSET_REG(i));
1595 }
1596 adt7475_read_hystersis(client);
1597
Jean Delvare378933c2009-12-09 20:36:06 +01001598 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1599 if (i == 3 && !data->has_fan4)
1600 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001601 data->tach[MIN][i] =
1602 adt7475_read_word(client, TACH_MIN_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001603 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001604
1605 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
Jean Delvare378933c2009-12-09 20:36:06 +01001606 if (i == 1 && !data->has_pwm2)
1607 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001608 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1609 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1610 /* Set the channel and control information */
1611 adt7475_read_pwm(client, i);
1612 }
1613
1614 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1615 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1616 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1617
1618 data->limits_updated = jiffies;
1619 data->valid = 1;
1620 }
1621
1622 mutex_unlock(&data->lock);
1623
1624 return data;
1625}
1626
Axel Linf0967ee2012-01-20 15:38:18 +08001627module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001628
1629MODULE_AUTHOR("Advanced Micro Devices, Inc");
1630MODULE_DESCRIPTION("adt7475 driver");
1631MODULE_LICENSE("GPL");