blob: d2583caa8087c6932813617322ea321726cd2baf [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))
Luuk Paulussenc5e1aae2019-12-06 12:16:59 +1300271 reg = DIV_ROUND_CLOSEST(volt * 1024, 2250);
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100272 else
Luuk Paulussenc5e1aae2019-12-06 12:16:59 +1300273 reg = DIV_ROUND_CLOSEST(volt * r[1] * 1024,
274 (r[0] + r[1]) * 2250);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800275 return clamp_val(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100276}
277
Dan Carpenter8ccd9e42018-08-14 13:07:47 +0300278static int adt7475_read_word(struct i2c_client *client, int reg)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100279{
Dan Carpenter8ccd9e42018-08-14 13:07:47 +0300280 int val1, val2;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100281
Dan Carpenter8ccd9e42018-08-14 13:07:47 +0300282 val1 = i2c_smbus_read_byte_data(client, reg);
283 if (val1 < 0)
284 return val1;
285 val2 = i2c_smbus_read_byte_data(client, reg + 1);
286 if (val2 < 0)
287 return val2;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100288
Dan Carpenter8ccd9e42018-08-14 13:07:47 +0300289 return val1 | (val2 << 8);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100290}
291
292static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
293{
294 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
295 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
296}
297
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800298/*
299 * Find the nearest value in a table - used for pwm frequency and
300 * auto temp range
301 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100302static int find_nearest(long val, const int *array, int size)
303{
304 int i;
305
306 if (val < array[0])
307 return 0;
308
309 if (val > array[size - 1])
310 return size - 1;
311
312 for (i = 0; i < size - 1; i++) {
313 int a, b;
314
315 if (val > array[i + 1])
316 continue;
317
318 a = val - array[i];
319 b = array[i + 1] - val;
320
321 return (a <= b) ? i : i + 1;
322 }
323
324 return 0;
325}
326
327static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
328 char *buf)
329{
330 struct adt7475_data *data = adt7475_update_device(dev);
331 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
332 unsigned short val;
333
334 switch (sattr->nr) {
335 case ALARM:
336 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100337 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100338 default:
339 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100340 return sprintf(buf, "%d\n",
341 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100342 }
343}
344
345static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
346 const char *buf, size_t count)
347{
348
349 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
350 struct i2c_client *client = to_i2c_client(dev);
351 struct adt7475_data *data = i2c_get_clientdata(client);
352 unsigned char reg;
353 long val;
354
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100355 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100356 return -EINVAL;
357
358 mutex_lock(&data->lock);
359
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100360 data->voltage[sattr->nr][sattr->index] =
361 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100362
Jean Delvare3d849982009-12-09 20:36:05 +0100363 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
364 if (sattr->nr == MIN)
365 reg = VOLTAGE_MIN_REG(sattr->index);
366 else
367 reg = VOLTAGE_MAX_REG(sattr->index);
368 } else {
369 if (sattr->nr == MIN)
370 reg = REG_VTT_MIN;
371 else
372 reg = REG_VTT_MAX;
373 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100374
375 i2c_smbus_write_byte_data(client, reg,
376 data->voltage[sattr->nr][sattr->index] >> 2);
377 mutex_unlock(&data->lock);
378
379 return count;
380}
381
382static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
383 char *buf)
384{
385 struct adt7475_data *data = adt7475_update_device(dev);
386 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
387 int out;
388
389 switch (sattr->nr) {
390 case HYSTERSIS:
391 mutex_lock(&data->lock);
392 out = data->temp[sattr->nr][sattr->index];
393 if (sattr->index != 1)
394 out = (out >> 4) & 0xF;
395 else
396 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800397 /*
398 * Show the value as an absolute number tied to
399 * THERM
400 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100401 out = reg2temp(data, data->temp[THERM][sattr->index]) -
402 out * 1000;
403 mutex_unlock(&data->lock);
404 break;
405
406 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800407 /*
408 * Offset is always 2's complement, regardless of the
409 * setting in CONFIG5
410 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100411 mutex_lock(&data->lock);
412 out = (s8)data->temp[sattr->nr][sattr->index];
413 if (data->config5 & CONFIG5_TEMPOFFSET)
414 out *= 1000;
415 else
416 out *= 500;
417 mutex_unlock(&data->lock);
418 break;
419
420 case ALARM:
421 out = (data->alarms >> (sattr->index + 4)) & 1;
422 break;
423
424 case FAULT:
425 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100426 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100427 break;
428
429 default:
430 /* All other temp values are in the configured format */
431 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
432 }
433
434 return sprintf(buf, "%d\n", out);
435}
436
437static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
438 const char *buf, size_t count)
439{
440 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
441 struct i2c_client *client = to_i2c_client(dev);
442 struct adt7475_data *data = i2c_get_clientdata(client);
443 unsigned char reg = 0;
444 u8 out;
445 int temp;
446 long val;
447
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100448 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100449 return -EINVAL;
450
451 mutex_lock(&data->lock);
452
453 /* We need the config register in all cases for temp <-> reg conv. */
454 data->config5 = adt7475_read(REG_CONFIG5);
455
456 switch (sattr->nr) {
457 case OFFSET:
458 if (data->config5 & CONFIG5_TEMPOFFSET) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800459 val = clamp_val(val, -63000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100460 out = data->temp[OFFSET][sattr->index] = val / 1000;
461 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800462 val = clamp_val(val, -63000, 64000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100463 out = data->temp[OFFSET][sattr->index] = val / 500;
464 }
465 break;
466
467 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800468 /*
469 * The value will be given as an absolute value, turn it
470 * into an offset based on THERM
471 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100472
473 /* Read fresh THERM and HYSTERSIS values from the chip */
474 data->temp[THERM][sattr->index] =
475 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
476 adt7475_read_hystersis(client);
477
478 temp = reg2temp(data, data->temp[THERM][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800479 val = clamp_val(val, temp - 15000, temp);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100480 val = (temp - val) / 1000;
481
482 if (sattr->index != 1) {
483 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
484 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
485 } else {
486 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
487 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
488 }
489
490 out = data->temp[HYSTERSIS][sattr->index];
491 break;
492
493 default:
494 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
495
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800496 /*
497 * We maintain an extra 2 digits of precision for simplicity
498 * - shift those back off before writing the value
499 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100500 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
501 }
502
503 switch (sattr->nr) {
504 case MIN:
505 reg = TEMP_MIN_REG(sattr->index);
506 break;
507 case MAX:
508 reg = TEMP_MAX_REG(sattr->index);
509 break;
510 case OFFSET:
511 reg = TEMP_OFFSET_REG(sattr->index);
512 break;
513 case AUTOMIN:
514 reg = TEMP_TMIN_REG(sattr->index);
515 break;
516 case THERM:
517 reg = TEMP_THERM_REG(sattr->index);
518 break;
519 case HYSTERSIS:
520 if (sattr->index != 2)
521 reg = REG_REMOTE1_HYSTERSIS;
522 else
523 reg = REG_REMOTE2_HYSTERSIS;
524
525 break;
526 }
527
528 i2c_smbus_write_byte_data(client, reg, out);
529
530 mutex_unlock(&data->lock);
531 return count;
532}
533
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800534/*
535 * Table of autorange values - the user will write the value in millidegrees,
536 * and we'll convert it
537 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100538static const int autorange_table[] = {
539 2000, 2500, 3330, 4000, 5000, 6670, 8000,
540 10000, 13330, 16000, 20000, 26670, 32000, 40000,
541 53330, 80000
542};
543
544static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
545 char *buf)
546{
547 struct adt7475_data *data = adt7475_update_device(dev);
548 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
549 int out, val;
550
551 mutex_lock(&data->lock);
552 out = (data->range[sattr->index] >> 4) & 0x0F;
553 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
554 mutex_unlock(&data->lock);
555
556 return sprintf(buf, "%d\n", val + autorange_table[out]);
557}
558
559static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
560 const char *buf, size_t count)
561{
562 struct i2c_client *client = to_i2c_client(dev);
563 struct adt7475_data *data = i2c_get_clientdata(client);
564 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
565 int temp;
566 long val;
567
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100568 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100569 return -EINVAL;
570
571 mutex_lock(&data->lock);
572
573 /* Get a fresh copy of the needed registers */
574 data->config5 = adt7475_read(REG_CONFIG5);
575 data->temp[AUTOMIN][sattr->index] =
576 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
577 data->range[sattr->index] =
578 adt7475_read(TEMP_TRANGE_REG(sattr->index));
579
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800580 /*
581 * The user will write an absolute value, so subtract the start point
582 * to figure the range
583 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100584 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800585 val = clamp_val(val, temp + autorange_table[0],
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100586 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
587 val -= temp;
588
589 /* Find the nearest table entry to what the user wrote */
590 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
591
592 data->range[sattr->index] &= ~0xF0;
593 data->range[sattr->index] |= val << 4;
594
595 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
596 data->range[sattr->index]);
597
598 mutex_unlock(&data->lock);
599 return count;
600}
601
602static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
603 char *buf)
604{
605 struct adt7475_data *data = adt7475_update_device(dev);
606 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
607 int out;
608
609 if (sattr->nr == ALARM)
610 out = (data->alarms >> (sattr->index + 10)) & 1;
611 else
612 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
613
614 return sprintf(buf, "%d\n", out);
615}
616
617static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
618 const char *buf, size_t count)
619{
620
621 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
622 struct i2c_client *client = to_i2c_client(dev);
623 struct adt7475_data *data = i2c_get_clientdata(client);
624 unsigned long val;
625
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100626 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100627 return -EINVAL;
628
629 mutex_lock(&data->lock);
630
631 data->tach[MIN][sattr->index] = rpm2tach(val);
632
633 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
634 data->tach[MIN][sattr->index]);
635
636 mutex_unlock(&data->lock);
637 return count;
638}
639
640static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
641 char *buf)
642{
643 struct adt7475_data *data = adt7475_update_device(dev);
644 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
645
646 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
647}
648
649static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
650 char *buf)
651{
652 struct adt7475_data *data = adt7475_update_device(dev);
653 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
654
655 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
656}
657
658static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
659 char *buf)
660{
661 struct adt7475_data *data = adt7475_update_device(dev);
662 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
663
664 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
665}
666
667static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
668 const char *buf, size_t count)
669{
670
671 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
672 struct i2c_client *client = to_i2c_client(dev);
673 struct adt7475_data *data = i2c_get_clientdata(client);
674 unsigned char reg = 0;
675 long val;
676
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100677 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100678 return -EINVAL;
679
680 mutex_lock(&data->lock);
681
682 switch (sattr->nr) {
683 case INPUT:
684 /* Get a fresh value for CONTROL */
685 data->pwm[CONTROL][sattr->index] =
686 adt7475_read(PWM_CONFIG_REG(sattr->index));
687
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800688 /*
689 * If we are not in manual mode, then we shouldn't allow
690 * the user to set the pwm speed
691 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100692 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
693 mutex_unlock(&data->lock);
694 return count;
695 }
696
697 reg = PWM_REG(sattr->index);
698 break;
699
700 case MIN:
701 reg = PWM_MIN_REG(sattr->index);
702 break;
703
704 case MAX:
705 reg = PWM_MAX_REG(sattr->index);
706 break;
707 }
708
Guenter Roeck2a844c12013-01-09 08:09:34 -0800709 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100710 i2c_smbus_write_byte_data(client, reg,
711 data->pwm[sattr->nr][sattr->index]);
712
713 mutex_unlock(&data->lock);
714
715 return count;
716}
717
718/* Called by set_pwmctrl and set_pwmchan */
719
720static int hw_set_pwm(struct i2c_client *client, int index,
721 unsigned int pwmctl, unsigned int pwmchan)
722{
723 struct adt7475_data *data = i2c_get_clientdata(client);
724 long val = 0;
725
726 switch (pwmctl) {
727 case 0:
728 val = 0x03; /* Run at full speed */
729 break;
730 case 1:
731 val = 0x07; /* Manual mode */
732 break;
733 case 2:
734 switch (pwmchan) {
735 case 1:
736 /* Remote1 controls PWM */
737 val = 0x00;
738 break;
739 case 2:
740 /* local controls PWM */
741 val = 0x01;
742 break;
743 case 4:
744 /* remote2 controls PWM */
745 val = 0x02;
746 break;
747 case 6:
748 /* local/remote2 control PWM */
749 val = 0x05;
750 break;
751 case 7:
752 /* All three control PWM */
753 val = 0x06;
754 break;
755 default:
756 return -EINVAL;
757 }
758 break;
759 default:
760 return -EINVAL;
761 }
762
763 data->pwmctl[index] = pwmctl;
764 data->pwmchan[index] = pwmchan;
765
766 data->pwm[CONTROL][index] &= ~0xE0;
767 data->pwm[CONTROL][index] |= (val & 7) << 5;
768
769 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
770 data->pwm[CONTROL][index]);
771
772 return 0;
773}
774
775static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
776 const char *buf, size_t count)
777{
778 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
779 struct i2c_client *client = to_i2c_client(dev);
780 struct adt7475_data *data = i2c_get_clientdata(client);
781 int r;
782 long val;
783
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100784 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100785 return -EINVAL;
786
787 mutex_lock(&data->lock);
788 /* Read Modify Write PWM values */
789 adt7475_read_pwm(client, sattr->index);
790 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
791 if (r)
792 count = r;
793 mutex_unlock(&data->lock);
794
795 return count;
796}
797
798static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
799 const char *buf, size_t count)
800{
801 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
802 struct i2c_client *client = to_i2c_client(dev);
803 struct adt7475_data *data = i2c_get_clientdata(client);
804 int r;
805 long val;
806
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100807 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100808 return -EINVAL;
809
810 mutex_lock(&data->lock);
811 /* Read Modify Write PWM values */
812 adt7475_read_pwm(client, sattr->index);
813 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
814 if (r)
815 count = r;
816 mutex_unlock(&data->lock);
817
818 return count;
819}
820
821/* List of frequencies for the PWM */
822static const int pwmfreq_table[] = {
823 11, 14, 22, 29, 35, 44, 58, 88
824};
825
826static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
827 char *buf)
828{
829 struct adt7475_data *data = adt7475_update_device(dev);
830 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
831
832 return sprintf(buf, "%d\n",
833 pwmfreq_table[data->range[sattr->index] & 7]);
834}
835
836static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
837 const char *buf, size_t count)
838{
839 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
840 struct i2c_client *client = to_i2c_client(dev);
841 struct adt7475_data *data = i2c_get_clientdata(client);
842 int out;
843 long val;
844
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100845 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100846 return -EINVAL;
847
848 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
849
850 mutex_lock(&data->lock);
851
852 data->range[sattr->index] =
853 adt7475_read(TEMP_TRANGE_REG(sattr->index));
854 data->range[sattr->index] &= ~7;
855 data->range[sattr->index] |= out;
856
857 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
858 data->range[sattr->index]);
859
860 mutex_unlock(&data->lock);
861 return count;
862}
863
Jean Delvaref99318b2009-12-09 20:36:03 +0100864static ssize_t show_pwm_at_crit(struct device *dev,
865 struct device_attribute *devattr, char *buf)
866{
867 struct adt7475_data *data = adt7475_update_device(dev);
868 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
869}
870
871static ssize_t set_pwm_at_crit(struct device *dev,
872 struct device_attribute *devattr,
873 const char *buf, size_t count)
874{
875 struct i2c_client *client = to_i2c_client(dev);
876 struct adt7475_data *data = i2c_get_clientdata(client);
877 long val;
878
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100879 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +0100880 return -EINVAL;
881 if (val != 0 && val != 1)
882 return -EINVAL;
883
884 mutex_lock(&data->lock);
885 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
886 if (val)
887 data->config4 |= CONFIG4_MAXDUTY;
888 else
889 data->config4 &= ~CONFIG4_MAXDUTY;
890 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
891 mutex_unlock(&data->lock);
892
893 return count;
894}
895
Jean Delvare54fe4672009-12-09 20:36:08 +0100896static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr,
897 char *buf)
898{
899 struct adt7475_data *data = dev_get_drvdata(dev);
900 return sprintf(buf, "%d\n", (int)data->vrm);
901}
902
903static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr,
904 const char *buf, size_t count)
905{
906 struct adt7475_data *data = dev_get_drvdata(dev);
907 long val;
908
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100909 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +0100910 return -EINVAL;
911 if (val < 0 || val > 255)
912 return -EINVAL;
913 data->vrm = val;
914
915 return count;
916}
917
918static ssize_t show_vid(struct device *dev, struct device_attribute *devattr,
919 char *buf)
920{
921 struct adt7475_data *data = adt7475_update_device(dev);
922 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
923}
924
Jean Delvare3d849982009-12-09 20:36:05 +0100925static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
926static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
927 set_voltage, MAX, 0);
928static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
929 set_voltage, MIN, 0);
930static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100931static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100932static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100933 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100934static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100935 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100936static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
937static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
938static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
939 set_voltage, MAX, 2);
940static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
941 set_voltage, MIN, 2);
942static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +0100943static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
944static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
945 set_voltage, MAX, 3);
946static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
947 set_voltage, MIN, 3);
948static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
949static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
950static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
951 set_voltage, MAX, 4);
952static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
953 set_voltage, MIN, 4);
954static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
955static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
956static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
957 set_voltage, MAX, 5);
958static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
959 set_voltage, MIN, 5);
960static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100961static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
962static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
963static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
964static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
965 MAX, 0);
966static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
967 MIN, 0);
968static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
969 set_temp, OFFSET, 0);
970static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
971 show_temp, set_temp, AUTOMIN, 0);
972static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
973 show_point2, set_point2, 0, 0);
974static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
975 THERM, 0);
976static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
977 set_temp, HYSTERSIS, 0);
978static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
979static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
980static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
981 MAX, 1);
982static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
983 MIN, 1);
984static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
985 set_temp, OFFSET, 1);
986static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
987 show_temp, set_temp, AUTOMIN, 1);
988static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
989 show_point2, set_point2, 0, 1);
990static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
991 THERM, 1);
992static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
993 set_temp, HYSTERSIS, 1);
994static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
995static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
996static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
997static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
998 MAX, 2);
999static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1000 MIN, 2);
1001static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1002 set_temp, OFFSET, 2);
1003static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
1004 show_temp, set_temp, AUTOMIN, 2);
1005static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
1006 show_point2, set_point2, 0, 2);
1007static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1008 THERM, 2);
1009static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1010 set_temp, HYSTERSIS, 2);
1011static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1012static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1013 MIN, 0);
1014static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1015static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1016static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1017 MIN, 1);
1018static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1019static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1020static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1021 MIN, 2);
1022static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1023static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1024static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1025 MIN, 3);
1026static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1027static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1028 0);
1029static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1030 set_pwmfreq, INPUT, 0);
1031static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1032 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +01001033static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001034 show_pwmchan, set_pwmchan, INPUT, 0);
1035static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1036 set_pwm, MIN, 0);
1037static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1038 set_pwm, MAX, 0);
1039static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1040 1);
1041static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1042 set_pwmfreq, INPUT, 1);
1043static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1044 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +01001045static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001046 show_pwmchan, set_pwmchan, INPUT, 1);
1047static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1048 set_pwm, MIN, 1);
1049static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1050 set_pwm, MAX, 1);
1051static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1052 2);
1053static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1054 set_pwmfreq, INPUT, 2);
1055static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1056 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +01001057static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001058 show_pwmchan, set_pwmchan, INPUT, 2);
1059static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1060 set_pwm, MIN, 2);
1061static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1062 set_pwm, MAX, 2);
1063
Jean Delvaref99318b2009-12-09 20:36:03 +01001064/* Non-standard name, might need revisiting */
1065static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
1066 show_pwm_at_crit, set_pwm_at_crit);
1067
Jean Delvare54fe4672009-12-09 20:36:08 +01001068static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm);
1069static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1070
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001071static struct attribute *adt7475_attrs[] = {
1072 &sensor_dev_attr_in1_input.dev_attr.attr,
1073 &sensor_dev_attr_in1_max.dev_attr.attr,
1074 &sensor_dev_attr_in1_min.dev_attr.attr,
1075 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1076 &sensor_dev_attr_in2_input.dev_attr.attr,
1077 &sensor_dev_attr_in2_max.dev_attr.attr,
1078 &sensor_dev_attr_in2_min.dev_attr.attr,
1079 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1080 &sensor_dev_attr_temp1_input.dev_attr.attr,
1081 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1082 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1083 &sensor_dev_attr_temp1_max.dev_attr.attr,
1084 &sensor_dev_attr_temp1_min.dev_attr.attr,
1085 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1086 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1087 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1088 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1089 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1090 &sensor_dev_attr_temp2_input.dev_attr.attr,
1091 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1092 &sensor_dev_attr_temp2_max.dev_attr.attr,
1093 &sensor_dev_attr_temp2_min.dev_attr.attr,
1094 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1095 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1096 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1097 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1098 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1099 &sensor_dev_attr_temp3_input.dev_attr.attr,
1100 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1101 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1102 &sensor_dev_attr_temp3_max.dev_attr.attr,
1103 &sensor_dev_attr_temp3_min.dev_attr.attr,
1104 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1105 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1106 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1107 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1108 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1109 &sensor_dev_attr_fan1_input.dev_attr.attr,
1110 &sensor_dev_attr_fan1_min.dev_attr.attr,
1111 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1112 &sensor_dev_attr_fan2_input.dev_attr.attr,
1113 &sensor_dev_attr_fan2_min.dev_attr.attr,
1114 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1115 &sensor_dev_attr_fan3_input.dev_attr.attr,
1116 &sensor_dev_attr_fan3_min.dev_attr.attr,
1117 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001118 &sensor_dev_attr_pwm1.dev_attr.attr,
1119 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1120 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001121 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001122 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1123 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001124 &sensor_dev_attr_pwm3.dev_attr.attr,
1125 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1126 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001127 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001128 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1129 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001130 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001131 NULL,
1132};
1133
Jean Delvare378933c2009-12-09 20:36:06 +01001134static struct attribute *fan4_attrs[] = {
1135 &sensor_dev_attr_fan4_input.dev_attr.attr,
1136 &sensor_dev_attr_fan4_min.dev_attr.attr,
1137 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1138 NULL
1139};
1140
1141static struct attribute *pwm2_attrs[] = {
1142 &sensor_dev_attr_pwm2.dev_attr.attr,
1143 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1144 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1145 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1146 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1147 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1148 NULL
1149};
1150
Jean Delvare378933c2009-12-09 20:36:06 +01001151static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001152 &sensor_dev_attr_in0_input.dev_attr.attr,
1153 &sensor_dev_attr_in0_max.dev_attr.attr,
1154 &sensor_dev_attr_in0_min.dev_attr.attr,
1155 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001156 NULL
1157};
1158
Jean Delvared8d2ee02009-12-09 20:36:08 +01001159static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001160 &sensor_dev_attr_in3_input.dev_attr.attr,
1161 &sensor_dev_attr_in3_max.dev_attr.attr,
1162 &sensor_dev_attr_in3_min.dev_attr.attr,
1163 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001164 NULL
1165};
1166
1167static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001168 &sensor_dev_attr_in4_input.dev_attr.attr,
1169 &sensor_dev_attr_in4_max.dev_attr.attr,
1170 &sensor_dev_attr_in4_min.dev_attr.attr,
1171 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001172 NULL
1173};
1174
1175static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001176 &sensor_dev_attr_in5_input.dev_attr.attr,
1177 &sensor_dev_attr_in5_max.dev_attr.attr,
1178 &sensor_dev_attr_in5_min.dev_attr.attr,
1179 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1180 NULL
1181};
1182
Jean Delvare54fe4672009-12-09 20:36:08 +01001183static struct attribute *vid_attrs[] = {
1184 &dev_attr_cpu0_vid.attr,
1185 &dev_attr_vrm.attr,
1186 NULL
1187};
1188
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001189static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jean Delvare378933c2009-12-09 20:36:06 +01001190static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1191static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1192static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
Jean Delvared8d2ee02009-12-09 20:36:08 +01001193static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1194static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1195static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
Jean Delvare54fe4672009-12-09 20:36:08 +01001196static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001197
Jean Delvare310ec792009-12-14 21:17:23 +01001198static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001199 struct i2c_board_info *info)
1200{
1201 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001202 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001203 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001204
1205 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1206 return -ENODEV;
1207
Jean Delvareb180d052009-12-09 20:36:02 +01001208 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001209 devid2 = adt7475_read(REG_DEVID2);
1210 if (vendid != 0x41 || /* Analog Devices */
1211 (devid2 & 0xf8) != 0x68)
1212 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001213
Jean Delvared656b6f2009-12-09 20:36:04 +01001214 devid = adt7475_read(REG_DEVID);
1215 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001216 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001217 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001218 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001219 else if (devid == 0x76)
1220 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001221 else if ((devid2 & 0xfc) == 0x6c)
1222 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001223 else {
1224 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001225 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001226 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001227 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001228 }
1229
Jean Delvareb180d052009-12-09 20:36:02 +01001230 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001231
1232 return 0;
1233}
1234
Jean Delvare0f144802009-12-09 20:36:06 +01001235static void adt7475_remove_files(struct i2c_client *client,
1236 struct adt7475_data *data)
1237{
1238 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001239 if (data->has_fan4)
1240 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1241 if (data->has_pwm2)
1242 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1243 if (data->has_voltage & (1 << 0))
1244 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001245 if (data->has_voltage & (1 << 3))
1246 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1247 if (data->has_voltage & (1 << 4))
1248 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1249 if (data->has_voltage & (1 << 5))
1250 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001251 if (data->has_vid)
1252 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001253}
1254
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001255static int adt7475_probe(struct i2c_client *client,
1256 const struct i2c_device_id *id)
1257{
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001258 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001259 [adt7473] = "ADT7473",
1260 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001261 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001262 [adt7490] = "ADT7490",
1263 };
1264
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001265 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001266 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001267 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001268
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001269 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001270 if (data == NULL)
1271 return -ENOMEM;
1272
1273 mutex_init(&data->lock);
1274 i2c_set_clientdata(client, data);
1275
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001276 /* Initialize device-specific values */
1277 switch (id->driver_data) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001278 case adt7476:
1279 data->has_voltage = 0x0e; /* in1 to in3 */
1280 revision = adt7475_read(REG_DEVID2) & 0x07;
1281 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001282 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001283 data->has_voltage = 0x3e; /* in1 to in5 */
1284 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001285 if (revision == 0x03)
1286 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001287 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001288 default:
1289 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001290 revision = adt7475_read(REG_DEVID2) & 0x07;
1291 }
1292
1293 config3 = adt7475_read(REG_CONFIG3);
1294 /* Pin PWM2 may alternatively be used for ALERT output */
1295 if (!(config3 & CONFIG3_SMBALERT))
1296 data->has_pwm2 = 1;
1297 /* Meaning of this bit is inverted for the ADT7473-1 */
1298 if (id->driver_data == adt7473 && revision >= 1)
1299 data->has_pwm2 = !data->has_pwm2;
1300
1301 data->config4 = adt7475_read(REG_CONFIG4);
1302 /* Pin TACH4 may alternatively be used for THERM */
1303 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1304 data->has_fan4 = 1;
1305
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001306 /*
1307 * THERM configuration is more complex on the ADT7476 and ADT7490,
1308 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1309 * this function
1310 */
Jean Delvare378933c2009-12-09 20:36:06 +01001311 if (id->driver_data == adt7490) {
1312 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1313 !(config3 & CONFIG3_THERM))
1314 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001315 }
1316 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001317 if (!(config3 & CONFIG3_THERM) ||
1318 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1319 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001320 }
1321
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001322 /*
1323 * On the ADT7476, the +12V input pin may instead be used as VID5,
1324 * and VID pins may alternatively be used as GPIO
1325 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001326 if (id->driver_data == adt7476) {
1327 u8 vid = adt7475_read(REG_VID);
1328 if (!(vid & VID_VIDSEL))
1329 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001330
1331 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001332 }
1333
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001334 /* Voltage attenuators can be bypassed, globally or individually */
1335 config2 = adt7475_read(REG_CONFIG2);
1336 if (config2 & CONFIG2_ATTN) {
1337 data->bypass_attn = (0x3 << 3) | 0x3;
1338 } else {
1339 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1340 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1341 }
1342 data->bypass_attn &= data->has_voltage;
1343
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001344 /*
1345 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1346 * pwm's which are disabled to manual mode with 0% duty cycle
1347 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001348 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1349 adt7475_read_pwm(client, i);
1350
1351 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1352 if (ret)
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001353 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001354
Jean Delvare378933c2009-12-09 20:36:06 +01001355 /* Features that can be disabled individually */
1356 if (data->has_fan4) {
1357 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1358 if (ret)
1359 goto eremove;
1360 }
1361 if (data->has_pwm2) {
1362 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1363 if (ret)
1364 goto eremove;
1365 }
1366 if (data->has_voltage & (1 << 0)) {
1367 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1368 if (ret)
1369 goto eremove;
1370 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001371 if (data->has_voltage & (1 << 3)) {
1372 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1373 if (ret)
1374 goto eremove;
1375 }
1376 if (data->has_voltage & (1 << 4)) {
1377 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1378 if (ret)
1379 goto eremove;
1380 }
1381 if (data->has_voltage & (1 << 5)) {
1382 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1383 if (ret)
1384 goto eremove;
1385 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001386 if (data->has_vid) {
1387 data->vrm = vid_which_vrm();
1388 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1389 if (ret)
1390 goto eremove;
1391 }
Jean Delvare378933c2009-12-09 20:36:06 +01001392
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001393 data->hwmon_dev = hwmon_device_register(&client->dev);
1394 if (IS_ERR(data->hwmon_dev)) {
1395 ret = PTR_ERR(data->hwmon_dev);
1396 goto eremove;
1397 }
1398
Jean Delvared07ca4a2009-12-09 20:36:07 +01001399 dev_info(&client->dev, "%s device, revision %d\n",
1400 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001401 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001402 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001403 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001404 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001405 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001406 data->has_pwm2 ? " pwm2" : "",
1407 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001408 if (data->bypass_attn)
1409 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1410 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1411 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1412 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1413 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001414
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001415 return 0;
1416
1417eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001418 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001419 return ret;
1420}
1421
1422static int adt7475_remove(struct i2c_client *client)
1423{
1424 struct adt7475_data *data = i2c_get_clientdata(client);
1425
1426 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001427 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001428
1429 return 0;
1430}
1431
1432static struct i2c_driver adt7475_driver = {
1433 .class = I2C_CLASS_HWMON,
1434 .driver = {
1435 .name = "adt7475",
1436 },
1437 .probe = adt7475_probe,
1438 .remove = adt7475_remove,
1439 .id_table = adt7475_id,
1440 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001441 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001442};
1443
1444static void adt7475_read_hystersis(struct i2c_client *client)
1445{
1446 struct adt7475_data *data = i2c_get_clientdata(client);
1447
1448 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1449 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1450 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1451}
1452
1453static void adt7475_read_pwm(struct i2c_client *client, int index)
1454{
1455 struct adt7475_data *data = i2c_get_clientdata(client);
1456 unsigned int v;
1457
1458 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1459
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001460 /*
1461 * Figure out the internal value for pwmctrl and pwmchan
1462 * based on the current settings
1463 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001464 v = (data->pwm[CONTROL][index] >> 5) & 7;
1465
1466 if (v == 3)
1467 data->pwmctl[index] = 0;
1468 else if (v == 7)
1469 data->pwmctl[index] = 1;
1470 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001471 /*
1472 * The fan is disabled - we don't want to
1473 * support that, so change to manual mode and
1474 * set the duty cycle to 0 instead
1475 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001476 data->pwm[INPUT][index] = 0;
1477 data->pwm[CONTROL][index] &= ~0xE0;
1478 data->pwm[CONTROL][index] |= (7 << 5);
1479
1480 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1481 data->pwm[INPUT][index]);
1482
1483 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1484 data->pwm[CONTROL][index]);
1485
1486 data->pwmctl[index] = 1;
1487 } else {
1488 data->pwmctl[index] = 2;
1489
1490 switch (v) {
1491 case 0:
1492 data->pwmchan[index] = 1;
1493 break;
1494 case 1:
1495 data->pwmchan[index] = 2;
1496 break;
1497 case 2:
1498 data->pwmchan[index] = 4;
1499 break;
1500 case 5:
1501 data->pwmchan[index] = 6;
1502 break;
1503 case 6:
1504 data->pwmchan[index] = 7;
1505 break;
1506 }
1507 }
1508}
1509
1510static struct adt7475_data *adt7475_update_device(struct device *dev)
1511{
1512 struct i2c_client *client = to_i2c_client(dev);
1513 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001514 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001515 int i;
1516
1517 mutex_lock(&data->lock);
1518
1519 /* Measurement values update every 2 seconds */
1520 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1521 !data->valid) {
1522 data->alarms = adt7475_read(REG_STATUS2) << 8;
1523 data->alarms |= adt7475_read(REG_STATUS1);
1524
Jean Delvare3d849982009-12-09 20:36:05 +01001525 ext = (adt7475_read(REG_EXTEND2) << 8) |
1526 adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001527 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1528 if (!(data->has_voltage & (1 << i)))
1529 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001530 data->voltage[INPUT][i] =
1531 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001532 ((ext >> (i * 2)) & 3);
1533 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001534
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001535 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1536 data->temp[INPUT][i] =
1537 (adt7475_read(TEMP_REG(i)) << 2) |
Jean Delvare3d849982009-12-09 20:36:05 +01001538 ((ext >> ((i + 5) * 2)) & 3);
1539
1540 if (data->has_voltage & (1 << 5)) {
1541 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1542 ext = adt7475_read(REG_EXTEND3);
1543 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1544 ((ext >> 4) & 3);
1545 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001546
Jean Delvare378933c2009-12-09 20:36:06 +01001547 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1548 if (i == 3 && !data->has_fan4)
1549 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001550 data->tach[INPUT][i] =
1551 adt7475_read_word(client, TACH_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001552 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001553
1554 /* Updated by hw when in auto mode */
Jean Delvare378933c2009-12-09 20:36:06 +01001555 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1556 if (i == 1 && !data->has_pwm2)
1557 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001558 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001559 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001560
Jean Delvare54fe4672009-12-09 20:36:08 +01001561 if (data->has_vid)
1562 data->vid = adt7475_read(REG_VID) & 0x3f;
1563
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001564 data->measure_updated = jiffies;
1565 }
1566
1567 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001568 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001569 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001570 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001571 data->config5 = adt7475_read(REG_CONFIG5);
1572
1573 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001574 if (!(data->has_voltage & (1 << i)))
1575 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001576 /* Adjust values so they match the input precision */
1577 data->voltage[MIN][i] =
1578 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1579 data->voltage[MAX][i] =
1580 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1581 }
1582
Jean Delvare3d849982009-12-09 20:36:05 +01001583 if (data->has_voltage & (1 << 5)) {
1584 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1585 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1586 }
1587
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001588 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1589 /* Adjust values so they match the input precision */
1590 data->temp[MIN][i] =
1591 adt7475_read(TEMP_MIN_REG(i)) << 2;
1592 data->temp[MAX][i] =
1593 adt7475_read(TEMP_MAX_REG(i)) << 2;
1594 data->temp[AUTOMIN][i] =
1595 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1596 data->temp[THERM][i] =
1597 adt7475_read(TEMP_THERM_REG(i)) << 2;
1598 data->temp[OFFSET][i] =
1599 adt7475_read(TEMP_OFFSET_REG(i));
1600 }
1601 adt7475_read_hystersis(client);
1602
Jean Delvare378933c2009-12-09 20:36:06 +01001603 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1604 if (i == 3 && !data->has_fan4)
1605 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001606 data->tach[MIN][i] =
1607 adt7475_read_word(client, TACH_MIN_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001608 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001609
1610 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
Jean Delvare378933c2009-12-09 20:36:06 +01001611 if (i == 1 && !data->has_pwm2)
1612 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001613 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1614 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1615 /* Set the channel and control information */
1616 adt7475_read_pwm(client, i);
1617 }
1618
1619 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1620 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1621 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1622
1623 data->limits_updated = jiffies;
1624 data->valid = 1;
1625 }
1626
1627 mutex_unlock(&data->lock);
1628
1629 return data;
1630}
1631
Axel Linf0967ee2012-01-20 15:38:18 +08001632module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001633
1634MODULE_AUTHOR("Advanced Micro Devices, Inc");
1635MODULE_DESCRIPTION("adt7475 driver");
1636MODULE_LICENSE("GPL");