blob: 9c262d95533150f6fd23fae6d9aeef9126ce607a [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 Delvare7c81c602014-01-29 20:40:08 +01006 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
Jean Delvare3d849982009-12-09 20:36:05 +01007 *
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
Dan Carpenter8ccd9e42018-08-14 13:07:47 +0300277static int adt7475_read_word(struct i2c_client *client, int reg)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100278{
Dan Carpenter8ccd9e42018-08-14 13:07:47 +0300279 int val1, val2;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100280
Dan Carpenter8ccd9e42018-08-14 13:07:47 +0300281 val1 = i2c_smbus_read_byte_data(client, reg);
282 if (val1 < 0)
283 return val1;
284 val2 = i2c_smbus_read_byte_data(client, reg + 1);
285 if (val2 < 0)
286 return val2;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100287
Dan Carpenter8ccd9e42018-08-14 13:07:47 +0300288 return val1 | (val2 << 8);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100289}
290
291static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
292{
293 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
294 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
295}
296
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800297/*
298 * Find the nearest value in a table - used for pwm frequency and
299 * auto temp range
300 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100301static int find_nearest(long val, const int *array, int size)
302{
303 int i;
304
305 if (val < array[0])
306 return 0;
307
308 if (val > array[size - 1])
309 return size - 1;
310
311 for (i = 0; i < size - 1; i++) {
312 int a, b;
313
314 if (val > array[i + 1])
315 continue;
316
317 a = val - array[i];
318 b = array[i + 1] - val;
319
320 return (a <= b) ? i : i + 1;
321 }
322
323 return 0;
324}
325
326static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
327 char *buf)
328{
329 struct adt7475_data *data = adt7475_update_device(dev);
330 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
331 unsigned short val;
332
333 switch (sattr->nr) {
334 case ALARM:
335 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100336 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100337 default:
338 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100339 return sprintf(buf, "%d\n",
340 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100341 }
342}
343
344static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
345 const char *buf, size_t count)
346{
347
348 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
349 struct i2c_client *client = to_i2c_client(dev);
350 struct adt7475_data *data = i2c_get_clientdata(client);
351 unsigned char reg;
352 long val;
353
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100354 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100355 return -EINVAL;
356
357 mutex_lock(&data->lock);
358
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100359 data->voltage[sattr->nr][sattr->index] =
360 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100361
Jean Delvare3d849982009-12-09 20:36:05 +0100362 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
363 if (sattr->nr == MIN)
364 reg = VOLTAGE_MIN_REG(sattr->index);
365 else
366 reg = VOLTAGE_MAX_REG(sattr->index);
367 } else {
368 if (sattr->nr == MIN)
369 reg = REG_VTT_MIN;
370 else
371 reg = REG_VTT_MAX;
372 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100373
374 i2c_smbus_write_byte_data(client, reg,
375 data->voltage[sattr->nr][sattr->index] >> 2);
376 mutex_unlock(&data->lock);
377
378 return count;
379}
380
381static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
382 char *buf)
383{
384 struct adt7475_data *data = adt7475_update_device(dev);
385 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
386 int out;
387
388 switch (sattr->nr) {
389 case HYSTERSIS:
390 mutex_lock(&data->lock);
391 out = data->temp[sattr->nr][sattr->index];
392 if (sattr->index != 1)
393 out = (out >> 4) & 0xF;
394 else
395 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800396 /*
397 * Show the value as an absolute number tied to
398 * THERM
399 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100400 out = reg2temp(data, data->temp[THERM][sattr->index]) -
401 out * 1000;
402 mutex_unlock(&data->lock);
403 break;
404
405 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800406 /*
407 * Offset is always 2's complement, regardless of the
408 * setting in CONFIG5
409 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100410 mutex_lock(&data->lock);
411 out = (s8)data->temp[sattr->nr][sattr->index];
412 if (data->config5 & CONFIG5_TEMPOFFSET)
413 out *= 1000;
414 else
415 out *= 500;
416 mutex_unlock(&data->lock);
417 break;
418
419 case ALARM:
420 out = (data->alarms >> (sattr->index + 4)) & 1;
421 break;
422
423 case FAULT:
424 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100425 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100426 break;
427
428 default:
429 /* All other temp values are in the configured format */
430 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
431 }
432
433 return sprintf(buf, "%d\n", out);
434}
435
436static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
437 const char *buf, size_t count)
438{
439 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
440 struct i2c_client *client = to_i2c_client(dev);
441 struct adt7475_data *data = i2c_get_clientdata(client);
442 unsigned char reg = 0;
443 u8 out;
444 int temp;
445 long val;
446
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100447 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100448 return -EINVAL;
449
450 mutex_lock(&data->lock);
451
452 /* We need the config register in all cases for temp <-> reg conv. */
453 data->config5 = adt7475_read(REG_CONFIG5);
454
455 switch (sattr->nr) {
456 case OFFSET:
457 if (data->config5 & CONFIG5_TEMPOFFSET) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800458 val = clamp_val(val, -63000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100459 out = data->temp[OFFSET][sattr->index] = val / 1000;
460 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800461 val = clamp_val(val, -63000, 64000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100462 out = data->temp[OFFSET][sattr->index] = val / 500;
463 }
464 break;
465
466 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800467 /*
468 * The value will be given as an absolute value, turn it
469 * into an offset based on THERM
470 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100471
472 /* Read fresh THERM and HYSTERSIS values from the chip */
473 data->temp[THERM][sattr->index] =
474 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
475 adt7475_read_hystersis(client);
476
477 temp = reg2temp(data, data->temp[THERM][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800478 val = clamp_val(val, temp - 15000, temp);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100479 val = (temp - val) / 1000;
480
481 if (sattr->index != 1) {
482 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
483 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
484 } else {
485 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
486 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
487 }
488
489 out = data->temp[HYSTERSIS][sattr->index];
490 break;
491
492 default:
493 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
494
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800495 /*
496 * We maintain an extra 2 digits of precision for simplicity
497 * - shift those back off before writing the value
498 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100499 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
500 }
501
502 switch (sattr->nr) {
503 case MIN:
504 reg = TEMP_MIN_REG(sattr->index);
505 break;
506 case MAX:
507 reg = TEMP_MAX_REG(sattr->index);
508 break;
509 case OFFSET:
510 reg = TEMP_OFFSET_REG(sattr->index);
511 break;
512 case AUTOMIN:
513 reg = TEMP_TMIN_REG(sattr->index);
514 break;
515 case THERM:
516 reg = TEMP_THERM_REG(sattr->index);
517 break;
518 case HYSTERSIS:
519 if (sattr->index != 2)
520 reg = REG_REMOTE1_HYSTERSIS;
521 else
522 reg = REG_REMOTE2_HYSTERSIS;
523
524 break;
525 }
526
527 i2c_smbus_write_byte_data(client, reg, out);
528
529 mutex_unlock(&data->lock);
530 return count;
531}
532
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800533/*
534 * Table of autorange values - the user will write the value in millidegrees,
535 * and we'll convert it
536 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100537static const int autorange_table[] = {
538 2000, 2500, 3330, 4000, 5000, 6670, 8000,
539 10000, 13330, 16000, 20000, 26670, 32000, 40000,
540 53330, 80000
541};
542
543static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
544 char *buf)
545{
546 struct adt7475_data *data = adt7475_update_device(dev);
547 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
548 int out, val;
549
550 mutex_lock(&data->lock);
551 out = (data->range[sattr->index] >> 4) & 0x0F;
552 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
553 mutex_unlock(&data->lock);
554
555 return sprintf(buf, "%d\n", val + autorange_table[out]);
556}
557
558static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
559 const char *buf, size_t count)
560{
561 struct i2c_client *client = to_i2c_client(dev);
562 struct adt7475_data *data = i2c_get_clientdata(client);
563 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
564 int temp;
565 long val;
566
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100567 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100568 return -EINVAL;
569
570 mutex_lock(&data->lock);
571
572 /* Get a fresh copy of the needed registers */
573 data->config5 = adt7475_read(REG_CONFIG5);
574 data->temp[AUTOMIN][sattr->index] =
575 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
576 data->range[sattr->index] =
577 adt7475_read(TEMP_TRANGE_REG(sattr->index));
578
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800579 /*
580 * The user will write an absolute value, so subtract the start point
581 * to figure the range
582 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100583 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800584 val = clamp_val(val, temp + autorange_table[0],
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100585 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
586 val -= temp;
587
588 /* Find the nearest table entry to what the user wrote */
589 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
590
591 data->range[sattr->index] &= ~0xF0;
592 data->range[sattr->index] |= val << 4;
593
594 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
595 data->range[sattr->index]);
596
597 mutex_unlock(&data->lock);
598 return count;
599}
600
601static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
602 char *buf)
603{
604 struct adt7475_data *data = adt7475_update_device(dev);
605 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
606 int out;
607
608 if (sattr->nr == ALARM)
609 out = (data->alarms >> (sattr->index + 10)) & 1;
610 else
611 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
612
613 return sprintf(buf, "%d\n", out);
614}
615
616static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
617 const char *buf, size_t count)
618{
619
620 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
621 struct i2c_client *client = to_i2c_client(dev);
622 struct adt7475_data *data = i2c_get_clientdata(client);
623 unsigned long val;
624
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100625 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100626 return -EINVAL;
627
628 mutex_lock(&data->lock);
629
630 data->tach[MIN][sattr->index] = rpm2tach(val);
631
632 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
633 data->tach[MIN][sattr->index]);
634
635 mutex_unlock(&data->lock);
636 return count;
637}
638
639static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
640 char *buf)
641{
642 struct adt7475_data *data = adt7475_update_device(dev);
643 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
644
645 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
646}
647
648static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
649 char *buf)
650{
651 struct adt7475_data *data = adt7475_update_device(dev);
652 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
653
654 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
655}
656
657static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
658 char *buf)
659{
660 struct adt7475_data *data = adt7475_update_device(dev);
661 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
662
663 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
664}
665
666static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
667 const char *buf, size_t count)
668{
669
670 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
671 struct i2c_client *client = to_i2c_client(dev);
672 struct adt7475_data *data = i2c_get_clientdata(client);
673 unsigned char reg = 0;
674 long val;
675
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100676 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100677 return -EINVAL;
678
679 mutex_lock(&data->lock);
680
681 switch (sattr->nr) {
682 case INPUT:
683 /* Get a fresh value for CONTROL */
684 data->pwm[CONTROL][sattr->index] =
685 adt7475_read(PWM_CONFIG_REG(sattr->index));
686
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800687 /*
688 * If we are not in manual mode, then we shouldn't allow
689 * the user to set the pwm speed
690 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100691 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
692 mutex_unlock(&data->lock);
693 return count;
694 }
695
696 reg = PWM_REG(sattr->index);
697 break;
698
699 case MIN:
700 reg = PWM_MIN_REG(sattr->index);
701 break;
702
703 case MAX:
704 reg = PWM_MAX_REG(sattr->index);
705 break;
706 }
707
Guenter Roeck2a844c12013-01-09 08:09:34 -0800708 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100709 i2c_smbus_write_byte_data(client, reg,
710 data->pwm[sattr->nr][sattr->index]);
711
712 mutex_unlock(&data->lock);
713
714 return count;
715}
716
717/* Called by set_pwmctrl and set_pwmchan */
718
719static int hw_set_pwm(struct i2c_client *client, int index,
720 unsigned int pwmctl, unsigned int pwmchan)
721{
722 struct adt7475_data *data = i2c_get_clientdata(client);
723 long val = 0;
724
725 switch (pwmctl) {
726 case 0:
727 val = 0x03; /* Run at full speed */
728 break;
729 case 1:
730 val = 0x07; /* Manual mode */
731 break;
732 case 2:
733 switch (pwmchan) {
734 case 1:
735 /* Remote1 controls PWM */
736 val = 0x00;
737 break;
738 case 2:
739 /* local controls PWM */
740 val = 0x01;
741 break;
742 case 4:
743 /* remote2 controls PWM */
744 val = 0x02;
745 break;
746 case 6:
747 /* local/remote2 control PWM */
748 val = 0x05;
749 break;
750 case 7:
751 /* All three control PWM */
752 val = 0x06;
753 break;
754 default:
755 return -EINVAL;
756 }
757 break;
758 default:
759 return -EINVAL;
760 }
761
762 data->pwmctl[index] = pwmctl;
763 data->pwmchan[index] = pwmchan;
764
765 data->pwm[CONTROL][index] &= ~0xE0;
766 data->pwm[CONTROL][index] |= (val & 7) << 5;
767
768 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
769 data->pwm[CONTROL][index]);
770
771 return 0;
772}
773
774static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
775 const char *buf, size_t count)
776{
777 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
778 struct i2c_client *client = to_i2c_client(dev);
779 struct adt7475_data *data = i2c_get_clientdata(client);
780 int r;
781 long val;
782
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100783 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100784 return -EINVAL;
785
786 mutex_lock(&data->lock);
787 /* Read Modify Write PWM values */
788 adt7475_read_pwm(client, sattr->index);
789 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
790 if (r)
791 count = r;
792 mutex_unlock(&data->lock);
793
794 return count;
795}
796
797static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
798 const char *buf, size_t count)
799{
800 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
801 struct i2c_client *client = to_i2c_client(dev);
802 struct adt7475_data *data = i2c_get_clientdata(client);
803 int r;
804 long val;
805
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100806 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100807 return -EINVAL;
808
809 mutex_lock(&data->lock);
810 /* Read Modify Write PWM values */
811 adt7475_read_pwm(client, sattr->index);
812 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
813 if (r)
814 count = r;
815 mutex_unlock(&data->lock);
816
817 return count;
818}
819
820/* List of frequencies for the PWM */
821static const int pwmfreq_table[] = {
822 11, 14, 22, 29, 35, 44, 58, 88
823};
824
825static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
826 char *buf)
827{
828 struct adt7475_data *data = adt7475_update_device(dev);
829 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
830
831 return sprintf(buf, "%d\n",
832 pwmfreq_table[data->range[sattr->index] & 7]);
833}
834
835static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
836 const char *buf, size_t count)
837{
838 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
839 struct i2c_client *client = to_i2c_client(dev);
840 struct adt7475_data *data = i2c_get_clientdata(client);
841 int out;
842 long val;
843
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100844 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100845 return -EINVAL;
846
847 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
848
849 mutex_lock(&data->lock);
850
851 data->range[sattr->index] =
852 adt7475_read(TEMP_TRANGE_REG(sattr->index));
853 data->range[sattr->index] &= ~7;
854 data->range[sattr->index] |= out;
855
856 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
857 data->range[sattr->index]);
858
859 mutex_unlock(&data->lock);
860 return count;
861}
862
Jean Delvaref99318b2009-12-09 20:36:03 +0100863static ssize_t show_pwm_at_crit(struct device *dev,
864 struct device_attribute *devattr, char *buf)
865{
866 struct adt7475_data *data = adt7475_update_device(dev);
867 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
868}
869
870static ssize_t set_pwm_at_crit(struct device *dev,
871 struct device_attribute *devattr,
872 const char *buf, size_t count)
873{
874 struct i2c_client *client = to_i2c_client(dev);
875 struct adt7475_data *data = i2c_get_clientdata(client);
876 long val;
877
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100878 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +0100879 return -EINVAL;
880 if (val != 0 && val != 1)
881 return -EINVAL;
882
883 mutex_lock(&data->lock);
884 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
885 if (val)
886 data->config4 |= CONFIG4_MAXDUTY;
887 else
888 data->config4 &= ~CONFIG4_MAXDUTY;
889 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
890 mutex_unlock(&data->lock);
891
892 return count;
893}
894
Jean Delvare54fe4672009-12-09 20:36:08 +0100895static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr,
896 char *buf)
897{
898 struct adt7475_data *data = dev_get_drvdata(dev);
899 return sprintf(buf, "%d\n", (int)data->vrm);
900}
901
902static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr,
903 const char *buf, size_t count)
904{
905 struct adt7475_data *data = dev_get_drvdata(dev);
906 long val;
907
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100908 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +0100909 return -EINVAL;
910 if (val < 0 || val > 255)
911 return -EINVAL;
912 data->vrm = val;
913
914 return count;
915}
916
917static ssize_t show_vid(struct device *dev, struct device_attribute *devattr,
918 char *buf)
919{
920 struct adt7475_data *data = adt7475_update_device(dev);
921 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
922}
923
Jean Delvare3d849982009-12-09 20:36:05 +0100924static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
925static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
926 set_voltage, MAX, 0);
927static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
928 set_voltage, MIN, 0);
929static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100930static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100931static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100932 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100933static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100934 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100935static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
936static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
937static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
938 set_voltage, MAX, 2);
939static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
940 set_voltage, MIN, 2);
941static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +0100942static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
943static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
944 set_voltage, MAX, 3);
945static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
946 set_voltage, MIN, 3);
947static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
948static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
949static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
950 set_voltage, MAX, 4);
951static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
952 set_voltage, MIN, 4);
953static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
954static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
955static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
956 set_voltage, MAX, 5);
957static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
958 set_voltage, MIN, 5);
959static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100960static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
961static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
962static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
963static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
964 MAX, 0);
965static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
966 MIN, 0);
967static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
968 set_temp, OFFSET, 0);
969static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
970 show_temp, set_temp, AUTOMIN, 0);
971static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
972 show_point2, set_point2, 0, 0);
973static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
974 THERM, 0);
975static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
976 set_temp, HYSTERSIS, 0);
977static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
978static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
979static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
980 MAX, 1);
981static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
982 MIN, 1);
983static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
984 set_temp, OFFSET, 1);
985static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
986 show_temp, set_temp, AUTOMIN, 1);
987static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
988 show_point2, set_point2, 0, 1);
989static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
990 THERM, 1);
991static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
992 set_temp, HYSTERSIS, 1);
993static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
994static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
995static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
996static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
997 MAX, 2);
998static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
999 MIN, 2);
1000static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1001 set_temp, OFFSET, 2);
1002static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
1003 show_temp, set_temp, AUTOMIN, 2);
1004static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
1005 show_point2, set_point2, 0, 2);
1006static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1007 THERM, 2);
1008static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1009 set_temp, HYSTERSIS, 2);
1010static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1011static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1012 MIN, 0);
1013static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1014static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1015static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1016 MIN, 1);
1017static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1018static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1019static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1020 MIN, 2);
1021static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1022static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1023static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1024 MIN, 3);
1025static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1026static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1027 0);
1028static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1029 set_pwmfreq, INPUT, 0);
1030static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1031 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +01001032static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001033 show_pwmchan, set_pwmchan, INPUT, 0);
1034static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1035 set_pwm, MIN, 0);
1036static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1037 set_pwm, MAX, 0);
1038static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1039 1);
1040static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1041 set_pwmfreq, INPUT, 1);
1042static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1043 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +01001044static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001045 show_pwmchan, set_pwmchan, INPUT, 1);
1046static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1047 set_pwm, MIN, 1);
1048static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1049 set_pwm, MAX, 1);
1050static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1051 2);
1052static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1053 set_pwmfreq, INPUT, 2);
1054static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1055 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +01001056static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001057 show_pwmchan, set_pwmchan, INPUT, 2);
1058static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1059 set_pwm, MIN, 2);
1060static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1061 set_pwm, MAX, 2);
1062
Jean Delvaref99318b2009-12-09 20:36:03 +01001063/* Non-standard name, might need revisiting */
1064static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
1065 show_pwm_at_crit, set_pwm_at_crit);
1066
Jean Delvare54fe4672009-12-09 20:36:08 +01001067static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm);
1068static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1069
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001070static struct attribute *adt7475_attrs[] = {
1071 &sensor_dev_attr_in1_input.dev_attr.attr,
1072 &sensor_dev_attr_in1_max.dev_attr.attr,
1073 &sensor_dev_attr_in1_min.dev_attr.attr,
1074 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1075 &sensor_dev_attr_in2_input.dev_attr.attr,
1076 &sensor_dev_attr_in2_max.dev_attr.attr,
1077 &sensor_dev_attr_in2_min.dev_attr.attr,
1078 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1079 &sensor_dev_attr_temp1_input.dev_attr.attr,
1080 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1081 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1082 &sensor_dev_attr_temp1_max.dev_attr.attr,
1083 &sensor_dev_attr_temp1_min.dev_attr.attr,
1084 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1085 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1086 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1087 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1088 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1089 &sensor_dev_attr_temp2_input.dev_attr.attr,
1090 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1091 &sensor_dev_attr_temp2_max.dev_attr.attr,
1092 &sensor_dev_attr_temp2_min.dev_attr.attr,
1093 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1094 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1095 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1096 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1097 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1098 &sensor_dev_attr_temp3_input.dev_attr.attr,
1099 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1100 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1101 &sensor_dev_attr_temp3_max.dev_attr.attr,
1102 &sensor_dev_attr_temp3_min.dev_attr.attr,
1103 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1104 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1105 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1106 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1107 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1108 &sensor_dev_attr_fan1_input.dev_attr.attr,
1109 &sensor_dev_attr_fan1_min.dev_attr.attr,
1110 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1111 &sensor_dev_attr_fan2_input.dev_attr.attr,
1112 &sensor_dev_attr_fan2_min.dev_attr.attr,
1113 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1114 &sensor_dev_attr_fan3_input.dev_attr.attr,
1115 &sensor_dev_attr_fan3_min.dev_attr.attr,
1116 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001117 &sensor_dev_attr_pwm1.dev_attr.attr,
1118 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1119 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001120 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001121 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1122 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001123 &sensor_dev_attr_pwm3.dev_attr.attr,
1124 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1125 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001126 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001127 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1128 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001129 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001130 NULL,
1131};
1132
Jean Delvare378933c2009-12-09 20:36:06 +01001133static struct attribute *fan4_attrs[] = {
1134 &sensor_dev_attr_fan4_input.dev_attr.attr,
1135 &sensor_dev_attr_fan4_min.dev_attr.attr,
1136 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1137 NULL
1138};
1139
1140static struct attribute *pwm2_attrs[] = {
1141 &sensor_dev_attr_pwm2.dev_attr.attr,
1142 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1143 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1144 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1145 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1146 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1147 NULL
1148};
1149
Jean Delvare378933c2009-12-09 20:36:06 +01001150static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001151 &sensor_dev_attr_in0_input.dev_attr.attr,
1152 &sensor_dev_attr_in0_max.dev_attr.attr,
1153 &sensor_dev_attr_in0_min.dev_attr.attr,
1154 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001155 NULL
1156};
1157
Jean Delvared8d2ee02009-12-09 20:36:08 +01001158static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001159 &sensor_dev_attr_in3_input.dev_attr.attr,
1160 &sensor_dev_attr_in3_max.dev_attr.attr,
1161 &sensor_dev_attr_in3_min.dev_attr.attr,
1162 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001163 NULL
1164};
1165
1166static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001167 &sensor_dev_attr_in4_input.dev_attr.attr,
1168 &sensor_dev_attr_in4_max.dev_attr.attr,
1169 &sensor_dev_attr_in4_min.dev_attr.attr,
1170 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001171 NULL
1172};
1173
1174static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001175 &sensor_dev_attr_in5_input.dev_attr.attr,
1176 &sensor_dev_attr_in5_max.dev_attr.attr,
1177 &sensor_dev_attr_in5_min.dev_attr.attr,
1178 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1179 NULL
1180};
1181
Jean Delvare54fe4672009-12-09 20:36:08 +01001182static struct attribute *vid_attrs[] = {
1183 &dev_attr_cpu0_vid.attr,
1184 &dev_attr_vrm.attr,
1185 NULL
1186};
1187
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001188static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jean Delvare378933c2009-12-09 20:36:06 +01001189static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1190static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1191static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
Jean Delvared8d2ee02009-12-09 20:36:08 +01001192static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1193static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1194static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
Jean Delvare54fe4672009-12-09 20:36:08 +01001195static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001196
Jean Delvare310ec792009-12-14 21:17:23 +01001197static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001198 struct i2c_board_info *info)
1199{
1200 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001201 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001202 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001203
1204 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1205 return -ENODEV;
1206
Jean Delvareb180d052009-12-09 20:36:02 +01001207 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001208 devid2 = adt7475_read(REG_DEVID2);
1209 if (vendid != 0x41 || /* Analog Devices */
1210 (devid2 & 0xf8) != 0x68)
1211 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001212
Jean Delvared656b6f2009-12-09 20:36:04 +01001213 devid = adt7475_read(REG_DEVID);
1214 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001215 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001216 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001217 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001218 else if (devid == 0x76)
1219 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001220 else if ((devid2 & 0xfc) == 0x6c)
1221 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001222 else {
1223 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001224 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001225 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001226 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001227 }
1228
Jean Delvareb180d052009-12-09 20:36:02 +01001229 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001230
1231 return 0;
1232}
1233
Jean Delvare0f144802009-12-09 20:36:06 +01001234static void adt7475_remove_files(struct i2c_client *client,
1235 struct adt7475_data *data)
1236{
1237 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001238 if (data->has_fan4)
1239 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1240 if (data->has_pwm2)
1241 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1242 if (data->has_voltage & (1 << 0))
1243 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001244 if (data->has_voltage & (1 << 3))
1245 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1246 if (data->has_voltage & (1 << 4))
1247 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1248 if (data->has_voltage & (1 << 5))
1249 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001250 if (data->has_vid)
1251 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001252}
1253
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001254static int adt7475_probe(struct i2c_client *client,
1255 const struct i2c_device_id *id)
1256{
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001257 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001258 [adt7473] = "ADT7473",
1259 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001260 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001261 [adt7490] = "ADT7490",
1262 };
1263
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001264 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001265 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001266 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001267
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001268 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001269 if (data == NULL)
1270 return -ENOMEM;
1271
1272 mutex_init(&data->lock);
1273 i2c_set_clientdata(client, data);
1274
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001275 /* Initialize device-specific values */
1276 switch (id->driver_data) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001277 case adt7476:
1278 data->has_voltage = 0x0e; /* in1 to in3 */
1279 revision = adt7475_read(REG_DEVID2) & 0x07;
1280 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001281 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001282 data->has_voltage = 0x3e; /* in1 to in5 */
1283 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001284 if (revision == 0x03)
1285 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001286 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001287 default:
1288 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001289 revision = adt7475_read(REG_DEVID2) & 0x07;
1290 }
1291
1292 config3 = adt7475_read(REG_CONFIG3);
1293 /* Pin PWM2 may alternatively be used for ALERT output */
1294 if (!(config3 & CONFIG3_SMBALERT))
1295 data->has_pwm2 = 1;
1296 /* Meaning of this bit is inverted for the ADT7473-1 */
1297 if (id->driver_data == adt7473 && revision >= 1)
1298 data->has_pwm2 = !data->has_pwm2;
1299
1300 data->config4 = adt7475_read(REG_CONFIG4);
1301 /* Pin TACH4 may alternatively be used for THERM */
1302 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1303 data->has_fan4 = 1;
1304
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001305 /*
1306 * THERM configuration is more complex on the ADT7476 and ADT7490,
1307 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1308 * this function
1309 */
Jean Delvare378933c2009-12-09 20:36:06 +01001310 if (id->driver_data == adt7490) {
1311 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1312 !(config3 & CONFIG3_THERM))
1313 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001314 }
1315 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001316 if (!(config3 & CONFIG3_THERM) ||
1317 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1318 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001319 }
1320
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001321 /*
1322 * On the ADT7476, the +12V input pin may instead be used as VID5,
1323 * and VID pins may alternatively be used as GPIO
1324 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001325 if (id->driver_data == adt7476) {
1326 u8 vid = adt7475_read(REG_VID);
1327 if (!(vid & VID_VIDSEL))
1328 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001329
1330 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001331 }
1332
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001333 /* Voltage attenuators can be bypassed, globally or individually */
1334 config2 = adt7475_read(REG_CONFIG2);
1335 if (config2 & CONFIG2_ATTN) {
1336 data->bypass_attn = (0x3 << 3) | 0x3;
1337 } else {
1338 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1339 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1340 }
1341 data->bypass_attn &= data->has_voltage;
1342
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001343 /*
1344 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1345 * pwm's which are disabled to manual mode with 0% duty cycle
1346 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001347 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1348 adt7475_read_pwm(client, i);
1349
1350 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1351 if (ret)
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001352 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001353
Jean Delvare378933c2009-12-09 20:36:06 +01001354 /* Features that can be disabled individually */
1355 if (data->has_fan4) {
1356 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1357 if (ret)
1358 goto eremove;
1359 }
1360 if (data->has_pwm2) {
1361 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1362 if (ret)
1363 goto eremove;
1364 }
1365 if (data->has_voltage & (1 << 0)) {
1366 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1367 if (ret)
1368 goto eremove;
1369 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001370 if (data->has_voltage & (1 << 3)) {
1371 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1372 if (ret)
1373 goto eremove;
1374 }
1375 if (data->has_voltage & (1 << 4)) {
1376 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1377 if (ret)
1378 goto eremove;
1379 }
1380 if (data->has_voltage & (1 << 5)) {
1381 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1382 if (ret)
1383 goto eremove;
1384 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001385 if (data->has_vid) {
1386 data->vrm = vid_which_vrm();
1387 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1388 if (ret)
1389 goto eremove;
1390 }
Jean Delvare378933c2009-12-09 20:36:06 +01001391
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001392 data->hwmon_dev = hwmon_device_register(&client->dev);
1393 if (IS_ERR(data->hwmon_dev)) {
1394 ret = PTR_ERR(data->hwmon_dev);
1395 goto eremove;
1396 }
1397
Jean Delvared07ca4a2009-12-09 20:36:07 +01001398 dev_info(&client->dev, "%s device, revision %d\n",
1399 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001400 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001401 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001402 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001403 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001404 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001405 data->has_pwm2 ? " pwm2" : "",
1406 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001407 if (data->bypass_attn)
1408 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1409 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1410 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1411 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1412 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001413
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001414 return 0;
1415
1416eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001417 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001418 return ret;
1419}
1420
1421static int adt7475_remove(struct i2c_client *client)
1422{
1423 struct adt7475_data *data = i2c_get_clientdata(client);
1424
1425 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001426 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001427
1428 return 0;
1429}
1430
1431static struct i2c_driver adt7475_driver = {
1432 .class = I2C_CLASS_HWMON,
1433 .driver = {
1434 .name = "adt7475",
1435 },
1436 .probe = adt7475_probe,
1437 .remove = adt7475_remove,
1438 .id_table = adt7475_id,
1439 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001440 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001441};
1442
1443static void adt7475_read_hystersis(struct i2c_client *client)
1444{
1445 struct adt7475_data *data = i2c_get_clientdata(client);
1446
1447 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1448 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1449 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1450}
1451
1452static void adt7475_read_pwm(struct i2c_client *client, int index)
1453{
1454 struct adt7475_data *data = i2c_get_clientdata(client);
1455 unsigned int v;
1456
1457 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1458
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001459 /*
1460 * Figure out the internal value for pwmctrl and pwmchan
1461 * based on the current settings
1462 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001463 v = (data->pwm[CONTROL][index] >> 5) & 7;
1464
1465 if (v == 3)
1466 data->pwmctl[index] = 0;
1467 else if (v == 7)
1468 data->pwmctl[index] = 1;
1469 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001470 /*
1471 * The fan is disabled - we don't want to
1472 * support that, so change to manual mode and
1473 * set the duty cycle to 0 instead
1474 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001475 data->pwm[INPUT][index] = 0;
1476 data->pwm[CONTROL][index] &= ~0xE0;
1477 data->pwm[CONTROL][index] |= (7 << 5);
1478
1479 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1480 data->pwm[INPUT][index]);
1481
1482 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1483 data->pwm[CONTROL][index]);
1484
1485 data->pwmctl[index] = 1;
1486 } else {
1487 data->pwmctl[index] = 2;
1488
1489 switch (v) {
1490 case 0:
1491 data->pwmchan[index] = 1;
1492 break;
1493 case 1:
1494 data->pwmchan[index] = 2;
1495 break;
1496 case 2:
1497 data->pwmchan[index] = 4;
1498 break;
1499 case 5:
1500 data->pwmchan[index] = 6;
1501 break;
1502 case 6:
1503 data->pwmchan[index] = 7;
1504 break;
1505 }
1506 }
1507}
1508
1509static struct adt7475_data *adt7475_update_device(struct device *dev)
1510{
1511 struct i2c_client *client = to_i2c_client(dev);
1512 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001513 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001514 int i;
1515
1516 mutex_lock(&data->lock);
1517
1518 /* Measurement values update every 2 seconds */
1519 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1520 !data->valid) {
1521 data->alarms = adt7475_read(REG_STATUS2) << 8;
1522 data->alarms |= adt7475_read(REG_STATUS1);
1523
Jean Delvare3d849982009-12-09 20:36:05 +01001524 ext = (adt7475_read(REG_EXTEND2) << 8) |
1525 adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001526 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1527 if (!(data->has_voltage & (1 << i)))
1528 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001529 data->voltage[INPUT][i] =
1530 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001531 ((ext >> (i * 2)) & 3);
1532 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001533
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001534 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1535 data->temp[INPUT][i] =
1536 (adt7475_read(TEMP_REG(i)) << 2) |
Jean Delvare3d849982009-12-09 20:36:05 +01001537 ((ext >> ((i + 5) * 2)) & 3);
1538
1539 if (data->has_voltage & (1 << 5)) {
1540 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1541 ext = adt7475_read(REG_EXTEND3);
1542 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1543 ((ext >> 4) & 3);
1544 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001545
Jean Delvare378933c2009-12-09 20:36:06 +01001546 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1547 if (i == 3 && !data->has_fan4)
1548 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001549 data->tach[INPUT][i] =
1550 adt7475_read_word(client, TACH_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001551 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001552
1553 /* Updated by hw when in auto mode */
Jean Delvare378933c2009-12-09 20:36:06 +01001554 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1555 if (i == 1 && !data->has_pwm2)
1556 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001557 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001558 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001559
Jean Delvare54fe4672009-12-09 20:36:08 +01001560 if (data->has_vid)
1561 data->vid = adt7475_read(REG_VID) & 0x3f;
1562
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001563 data->measure_updated = jiffies;
1564 }
1565
1566 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001567 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001568 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001569 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001570 data->config5 = adt7475_read(REG_CONFIG5);
1571
1572 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001573 if (!(data->has_voltage & (1 << i)))
1574 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001575 /* Adjust values so they match the input precision */
1576 data->voltage[MIN][i] =
1577 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1578 data->voltage[MAX][i] =
1579 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1580 }
1581
Jean Delvare3d849982009-12-09 20:36:05 +01001582 if (data->has_voltage & (1 << 5)) {
1583 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1584 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1585 }
1586
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001587 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1588 /* Adjust values so they match the input precision */
1589 data->temp[MIN][i] =
1590 adt7475_read(TEMP_MIN_REG(i)) << 2;
1591 data->temp[MAX][i] =
1592 adt7475_read(TEMP_MAX_REG(i)) << 2;
1593 data->temp[AUTOMIN][i] =
1594 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1595 data->temp[THERM][i] =
1596 adt7475_read(TEMP_THERM_REG(i)) << 2;
1597 data->temp[OFFSET][i] =
1598 adt7475_read(TEMP_OFFSET_REG(i));
1599 }
1600 adt7475_read_hystersis(client);
1601
Jean Delvare378933c2009-12-09 20:36:06 +01001602 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1603 if (i == 3 && !data->has_fan4)
1604 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001605 data->tach[MIN][i] =
1606 adt7475_read_word(client, TACH_MIN_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001607 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001608
1609 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
Jean Delvare378933c2009-12-09 20:36:06 +01001610 if (i == 1 && !data->has_pwm2)
1611 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001612 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1613 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1614 /* Set the channel and control information */
1615 adt7475_read_pwm(client, i);
1616 }
1617
1618 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1619 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1620 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1621
1622 data->limits_updated = jiffies;
1623 data->valid = 1;
1624 }
1625
1626 mutex_unlock(&data->lock);
1627
1628 return data;
1629}
1630
Axel Linf0967ee2012-01-20 15:38:18 +08001631module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001632
1633MODULE_AUTHOR("Advanced Micro Devices, Inc");
1634MODULE_DESCRIPTION("adt7475 driver");
1635MODULE_LICENSE("GPL");