blob: ac3b1542556e4369d91b549bf05efce730b3af18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Based on lm75.c and lm85.c
5 Supports adm1030 / adm1031
6 Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
7 Reworked by Jean Delvare <khali@linux-fr.org>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
29#include <linux/i2c-sensor.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040030#include <linux/hwmon.h>
31#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* Following macros takes channel parameter starting from 0 to 2 */
34#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
35#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
36#define ADM1031_REG_PWM (0x22)
37#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
38
39#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4*(nr))
40#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4*(nr))
41#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4*(nr))
42
43#define ADM1031_REG_TEMP(nr) (0xa + (nr))
44#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
45
46#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
47
48#define ADM1031_REG_CONF1 0x0
49#define ADM1031_REG_CONF2 0x1
50#define ADM1031_REG_EXT_TEMP 0x6
51
52#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
53#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
54#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
55
56#define ADM1031_CONF2_PWM1_ENABLE 0x01
57#define ADM1031_CONF2_PWM2_ENABLE 0x02
58#define ADM1031_CONF2_TACH1_ENABLE 0x04
59#define ADM1031_CONF2_TACH2_ENABLE 0x08
60#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
61
62/* Addresses to scan */
63static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
64static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
65
66/* Insmod parameters */
67SENSORS_INSMOD_2(adm1030, adm1031);
68
69typedef u8 auto_chan_table_t[8][2];
70
71/* Each client has this additional data */
72struct adm1031_data {
73 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -040074 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct semaphore update_lock;
76 int chip_type;
77 char valid; /* !=0 if following fields are valid */
78 unsigned long last_updated; /* In jiffies */
79 /* The chan_select_table contains the possible configurations for
80 * auto fan control.
81 */
82 auto_chan_table_t *chan_select_table;
83 u16 alarm;
84 u8 conf1;
85 u8 conf2;
86 u8 fan[2];
87 u8 fan_div[2];
88 u8 fan_min[2];
89 u8 pwm[2];
90 u8 old_pwm[2];
91 s8 temp[3];
92 u8 ext_temp[3];
93 u8 auto_temp[3];
94 u8 auto_temp_min[3];
95 u8 auto_temp_off[3];
96 u8 auto_temp_max[3];
97 s8 temp_min[3];
98 s8 temp_max[3];
99 s8 temp_crit[3];
100};
101
102static int adm1031_attach_adapter(struct i2c_adapter *adapter);
103static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind);
104static void adm1031_init_client(struct i2c_client *client);
105static int adm1031_detach_client(struct i2c_client *client);
106static struct adm1031_data *adm1031_update_device(struct device *dev);
107
108/* This is the driver that will be inserted */
109static struct i2c_driver adm1031_driver = {
110 .owner = THIS_MODULE,
111 .name = "adm1031",
112 .flags = I2C_DF_NOTIFY,
113 .attach_adapter = adm1031_attach_adapter,
114 .detach_client = adm1031_detach_client,
115};
116
117static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
118{
119 return i2c_smbus_read_byte_data(client, reg);
120}
121
122static inline int
123adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
124{
125 return i2c_smbus_write_byte_data(client, reg, value);
126}
127
128
129#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
130 ((val + 500) / 1000)))
131
132#define TEMP_FROM_REG(val) ((val) * 1000)
133
134#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
135
136#define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
137
138static int FAN_TO_REG(int reg, int div)
139{
140 int tmp;
141 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
142 return tmp > 255 ? 255 : tmp;
143}
144
145#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
146
147#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
148#define PWM_FROM_REG(val) ((val) << 4)
149
150#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
151#define FAN_CHAN_TO_REG(val, reg) \
152 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
153
154#define AUTO_TEMP_MIN_TO_REG(val, reg) \
155 ((((val)/500) & 0xf8)|((reg) & 0x7))
156#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
157#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
158
159#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
160
161#define AUTO_TEMP_OFF_FROM_REG(reg) \
162 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
163
164#define AUTO_TEMP_MAX_FROM_REG(reg) \
165 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
166 AUTO_TEMP_MIN_FROM_REG(reg))
167
168static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
169{
170 int ret;
171 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
172
173 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
174 ret = ((reg & 0xf8) |
175 (range < 10000 ? 0 :
176 range < 20000 ? 1 :
177 range < 40000 ? 2 : range < 80000 ? 3 : 4));
178 return ret;
179}
180
181/* FAN auto control */
182#define GET_FAN_AUTO_BITFIELD(data, idx) \
183 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
184
185/* The tables below contains the possible values for the auto fan
186 * control bitfields. the index in the table is the register value.
187 * MSb is the auto fan control enable bit, so the four first entries
188 * in the table disables auto fan control when both bitfields are zero.
189 */
190static auto_chan_table_t auto_channel_select_table_adm1031 = {
191 {0, 0}, {0, 0}, {0, 0}, {0, 0},
192 {2 /*0b010 */ , 4 /*0b100 */ },
193 {2 /*0b010 */ , 2 /*0b010 */ },
194 {4 /*0b100 */ , 4 /*0b100 */ },
195 {7 /*0b111 */ , 7 /*0b111 */ },
196};
197
198static auto_chan_table_t auto_channel_select_table_adm1030 = {
199 {0, 0}, {0, 0}, {0, 0}, {0, 0},
200 {2 /*0b10 */ , 0},
201 {0xff /*invalid */ , 0},
202 {0xff /*invalid */ , 0},
203 {3 /*0b11 */ , 0},
204};
205
206/* That function checks if a bitfield is valid and returns the other bitfield
207 * nearest match if no exact match where found.
208 */
209static int
210get_fan_auto_nearest(struct adm1031_data *data,
211 int chan, u8 val, u8 reg, u8 * new_reg)
212{
213 int i;
214 int first_match = -1, exact_match = -1;
215 u8 other_reg_val =
216 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
217
218 if (val == 0) {
219 *new_reg = 0;
220 return 0;
221 }
222
223 for (i = 0; i < 8; i++) {
224 if ((val == (*data->chan_select_table)[i][chan]) &&
225 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
226 other_reg_val)) {
227 /* We found an exact match */
228 exact_match = i;
229 break;
230 } else if (val == (*data->chan_select_table)[i][chan] &&
231 first_match == -1) {
232 /* Save the first match in case of an exact match has not been
233 * found
234 */
235 first_match = i;
236 }
237 }
238
239 if (exact_match >= 0) {
240 *new_reg = exact_match;
241 } else if (first_match >= 0) {
242 *new_reg = first_match;
243 } else {
244 return -EINVAL;
245 }
246 return 0;
247}
248
249static ssize_t show_fan_auto_channel(struct device *dev, char *buf, int nr)
250{
251 struct adm1031_data *data = adm1031_update_device(dev);
252 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
253}
254
255static ssize_t
256set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
257{
258 struct i2c_client *client = to_i2c_client(dev);
259 struct adm1031_data *data = i2c_get_clientdata(client);
260 int val = simple_strtol(buf, NULL, 10);
261 u8 reg;
262 int ret;
263 u8 old_fan_mode;
264
265 old_fan_mode = data->conf1;
266
267 down(&data->update_lock);
268
269 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
270 up(&data->update_lock);
271 return ret;
272 }
273 if (((data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1)) & ADM1031_CONF1_AUTO_MODE) ^
274 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
275 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
276 /* Switch to Auto Fan Mode
277 * Save PWM registers
278 * Set PWM registers to 33% Both */
279 data->old_pwm[0] = data->pwm[0];
280 data->old_pwm[1] = data->pwm[1];
281 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
282 } else {
283 /* Switch to Manual Mode */
284 data->pwm[0] = data->old_pwm[0];
285 data->pwm[1] = data->old_pwm[1];
286 /* Restore PWM registers */
287 adm1031_write_value(client, ADM1031_REG_PWM,
288 data->pwm[0] | (data->pwm[1] << 4));
289 }
290 }
291 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
292 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
293 up(&data->update_lock);
294 return count;
295}
296
297#define fan_auto_channel_offset(offset) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400298static ssize_t show_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{ \
300 return show_fan_auto_channel(dev, buf, offset - 1); \
301} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400302static ssize_t set_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 const char *buf, size_t count) \
304{ \
305 return set_fan_auto_channel(dev, buf, count, offset - 1); \
306} \
307static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR, \
308 show_fan_auto_channel_##offset, \
309 set_fan_auto_channel_##offset)
310
311fan_auto_channel_offset(1);
312fan_auto_channel_offset(2);
313
314/* Auto Temps */
315static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr)
316{
317 struct adm1031_data *data = adm1031_update_device(dev);
318 return sprintf(buf, "%d\n",
319 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
320}
321static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr)
322{
323 struct adm1031_data *data = adm1031_update_device(dev);
324 return sprintf(buf, "%d\n",
325 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
326}
327static ssize_t
328set_auto_temp_min(struct device *dev, const char *buf, size_t count, int nr)
329{
330 struct i2c_client *client = to_i2c_client(dev);
331 struct adm1031_data *data = i2c_get_clientdata(client);
332 int val = simple_strtol(buf, NULL, 10);
333
334 down(&data->update_lock);
335 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
336 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
337 data->auto_temp[nr]);
338 up(&data->update_lock);
339 return count;
340}
341static ssize_t show_auto_temp_max(struct device *dev, char *buf, int nr)
342{
343 struct adm1031_data *data = adm1031_update_device(dev);
344 return sprintf(buf, "%d\n",
345 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
346}
347static ssize_t
348set_auto_temp_max(struct device *dev, const char *buf, size_t count, int nr)
349{
350 struct i2c_client *client = to_i2c_client(dev);
351 struct adm1031_data *data = i2c_get_clientdata(client);
352 int val = simple_strtol(buf, NULL, 10);
353
354 down(&data->update_lock);
355 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
356 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
357 data->temp_max[nr]);
358 up(&data->update_lock);
359 return count;
360}
361
362#define auto_temp_reg(offset) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400363static ssize_t show_auto_temp_##offset##_off (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{ \
365 return show_auto_temp_off(dev, buf, offset - 1); \
366} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400367static ssize_t show_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{ \
369 return show_auto_temp_min(dev, buf, offset - 1); \
370} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400371static ssize_t show_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{ \
373 return show_auto_temp_max(dev, buf, offset - 1); \
374} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400375static ssize_t set_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 const char *buf, size_t count) \
377{ \
378 return set_auto_temp_min(dev, buf, count, offset - 1); \
379} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400380static ssize_t set_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 const char *buf, size_t count) \
382{ \
383 return set_auto_temp_max(dev, buf, count, offset - 1); \
384} \
385static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
386 show_auto_temp_##offset##_off, NULL); \
387static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
388 show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\
389static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
390 show_auto_temp_##offset##_max, set_auto_temp_##offset##_max)
391
392auto_temp_reg(1);
393auto_temp_reg(2);
394auto_temp_reg(3);
395
396/* pwm */
397static ssize_t show_pwm(struct device *dev, char *buf, int nr)
398{
399 struct adm1031_data *data = adm1031_update_device(dev);
400 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
401}
402static ssize_t
403set_pwm(struct device *dev, const char *buf, size_t count, int nr)
404{
405 struct i2c_client *client = to_i2c_client(dev);
406 struct adm1031_data *data = i2c_get_clientdata(client);
407 int val = simple_strtol(buf, NULL, 10);
408 int reg;
409
410 down(&data->update_lock);
411 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
412 (((val>>4) & 0xf) != 5)) {
413 /* In automatic mode, the only PWM accepted is 33% */
414 up(&data->update_lock);
415 return -EINVAL;
416 }
417 data->pwm[nr] = PWM_TO_REG(val);
418 reg = adm1031_read_value(client, ADM1031_REG_PWM);
419 adm1031_write_value(client, ADM1031_REG_PWM,
420 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
421 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
422 up(&data->update_lock);
423 return count;
424}
425
426#define pwm_reg(offset) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400427static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{ \
429 return show_pwm(dev, buf, offset - 1); \
430} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400431static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 const char *buf, size_t count) \
433{ \
434 return set_pwm(dev, buf, count, offset - 1); \
435} \
436static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
437 show_pwm_##offset, set_pwm_##offset)
438
439pwm_reg(1);
440pwm_reg(2);
441
442/* Fans */
443
444/*
445 * That function checks the cases where the fan reading is not
Steven Cole44bbe872005-05-03 18:21:25 -0600446 * relevant. It is used to provide 0 as fan reading when the fan is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * not supposed to run
448 */
449static int trust_fan_readings(struct adm1031_data *data, int chan)
450{
451 int res = 0;
452
453 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
454 switch (data->conf1 & 0x60) {
455 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
456 res = data->temp[chan+1] >=
457 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
458 break;
459 case 0x20: /* remote temp1 controls both fans */
460 res =
461 data->temp[1] >=
462 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
463 break;
464 case 0x40: /* remote temp2 controls both fans */
465 res =
466 data->temp[2] >=
467 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
468 break;
469 case 0x60: /* max controls both fans */
470 res =
471 data->temp[0] >=
472 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
473 || data->temp[1] >=
474 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
475 || (data->chip_type == adm1031
476 && data->temp[2] >=
477 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
478 break;
479 }
480 } else {
481 res = data->pwm[chan] > 0;
482 }
483 return res;
484}
485
486
487static ssize_t show_fan(struct device *dev, char *buf, int nr)
488{
489 struct adm1031_data *data = adm1031_update_device(dev);
490 int value;
491
492 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
493 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
494 return sprintf(buf, "%d\n", value);
495}
496
497static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
498{
499 struct adm1031_data *data = adm1031_update_device(dev);
500 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
501}
502static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
503{
504 struct adm1031_data *data = adm1031_update_device(dev);
505 return sprintf(buf, "%d\n",
506 FAN_FROM_REG(data->fan_min[nr],
507 FAN_DIV_FROM_REG(data->fan_div[nr])));
508}
509static ssize_t
510set_fan_min(struct device *dev, const char *buf, size_t count, int nr)
511{
512 struct i2c_client *client = to_i2c_client(dev);
513 struct adm1031_data *data = i2c_get_clientdata(client);
514 int val = simple_strtol(buf, NULL, 10);
515
516 down(&data->update_lock);
517 if (val) {
518 data->fan_min[nr] =
519 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
520 } else {
521 data->fan_min[nr] = 0xff;
522 }
523 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
524 up(&data->update_lock);
525 return count;
526}
527static ssize_t
528set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
529{
530 struct i2c_client *client = to_i2c_client(dev);
531 struct adm1031_data *data = i2c_get_clientdata(client);
532 int val = simple_strtol(buf, NULL, 10);
533 u8 tmp;
534 int old_div;
535 int new_min;
536
537 tmp = val == 8 ? 0xc0 :
538 val == 4 ? 0x80 :
539 val == 2 ? 0x40 :
540 val == 1 ? 0x00 :
541 0xff;
542 if (tmp == 0xff)
543 return -EINVAL;
544
545 down(&data->update_lock);
546 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
547 data->fan_div[nr] = (tmp & 0xC0) | (0x3f & data->fan_div[nr]);
548 new_min = data->fan_min[nr] * old_div /
549 FAN_DIV_FROM_REG(data->fan_div[nr]);
550 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
551 data->fan[nr] = data->fan[nr] * old_div /
552 FAN_DIV_FROM_REG(data->fan_div[nr]);
553
554 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
555 data->fan_div[nr]);
556 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
557 data->fan_min[nr]);
558 up(&data->update_lock);
559 return count;
560}
561
562#define fan_offset(offset) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400563static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{ \
565 return show_fan(dev, buf, offset - 1); \
566} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400567static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{ \
569 return show_fan_min(dev, buf, offset - 1); \
570} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400571static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{ \
573 return show_fan_div(dev, buf, offset - 1); \
574} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400575static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 const char *buf, size_t count) \
577{ \
578 return set_fan_min(dev, buf, count, offset - 1); \
579} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400580static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 const char *buf, size_t count) \
582{ \
583 return set_fan_div(dev, buf, count, offset - 1); \
584} \
585static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
586 NULL); \
587static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
588 show_fan_##offset##_min, set_fan_##offset##_min); \
589static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
590 show_fan_##offset##_div, set_fan_##offset##_div); \
591static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR, \
592 show_pwm_##offset, set_pwm_##offset)
593
594fan_offset(1);
595fan_offset(2);
596
597
598/* Temps */
599static ssize_t show_temp(struct device *dev, char *buf, int nr)
600{
601 struct adm1031_data *data = adm1031_update_device(dev);
602 int ext;
603 ext = nr == 0 ?
604 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
605 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
606 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
607}
608static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
609{
610 struct adm1031_data *data = adm1031_update_device(dev);
611 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
612}
613static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
614{
615 struct adm1031_data *data = adm1031_update_device(dev);
616 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
617}
618static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
619{
620 struct adm1031_data *data = adm1031_update_device(dev);
621 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
622}
623static ssize_t
624set_temp_min(struct device *dev, const char *buf, size_t count, int nr)
625{
626 struct i2c_client *client = to_i2c_client(dev);
627 struct adm1031_data *data = i2c_get_clientdata(client);
628 int val;
629
630 val = simple_strtol(buf, NULL, 10);
631 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
632 down(&data->update_lock);
633 data->temp_min[nr] = TEMP_TO_REG(val);
634 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
635 data->temp_min[nr]);
636 up(&data->update_lock);
637 return count;
638}
639static ssize_t
640set_temp_max(struct device *dev, const char *buf, size_t count, int nr)
641{
642 struct i2c_client *client = to_i2c_client(dev);
643 struct adm1031_data *data = i2c_get_clientdata(client);
644 int val;
645
646 val = simple_strtol(buf, NULL, 10);
647 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
648 down(&data->update_lock);
649 data->temp_max[nr] = TEMP_TO_REG(val);
650 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
651 data->temp_max[nr]);
652 up(&data->update_lock);
653 return count;
654}
655static ssize_t
656set_temp_crit(struct device *dev, const char *buf, size_t count, int nr)
657{
658 struct i2c_client *client = to_i2c_client(dev);
659 struct adm1031_data *data = i2c_get_clientdata(client);
660 int val;
661
662 val = simple_strtol(buf, NULL, 10);
663 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
664 down(&data->update_lock);
665 data->temp_crit[nr] = TEMP_TO_REG(val);
666 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
667 data->temp_crit[nr]);
668 up(&data->update_lock);
669 return count;
670}
671
672#define temp_reg(offset) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400673static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{ \
675 return show_temp(dev, buf, offset - 1); \
676} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400677static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{ \
679 return show_temp_min(dev, buf, offset - 1); \
680} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400681static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{ \
683 return show_temp_max(dev, buf, offset - 1); \
684} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400685static ssize_t show_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{ \
687 return show_temp_crit(dev, buf, offset - 1); \
688} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400689static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 const char *buf, size_t count) \
691{ \
692 return set_temp_min(dev, buf, count, offset - 1); \
693} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400694static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 const char *buf, size_t count) \
696{ \
697 return set_temp_max(dev, buf, count, offset - 1); \
698} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400699static ssize_t set_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 const char *buf, size_t count) \
701{ \
702 return set_temp_crit(dev, buf, count, offset - 1); \
703} \
704static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
705 NULL); \
706static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
707 show_temp_##offset##_min, set_temp_##offset##_min); \
708static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
709 show_temp_##offset##_max, set_temp_##offset##_max); \
710static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
711 show_temp_##offset##_crit, set_temp_##offset##_crit)
712
713temp_reg(1);
714temp_reg(2);
715temp_reg(3);
716
717/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400718static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct adm1031_data *data = adm1031_update_device(dev);
721 return sprintf(buf, "%d\n", data->alarm);
722}
723
724static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
725
726
727static int adm1031_attach_adapter(struct i2c_adapter *adapter)
728{
729 if (!(adapter->class & I2C_CLASS_HWMON))
730 return 0;
731 return i2c_detect(adapter, &addr_data, adm1031_detect);
732}
733
734/* This function is called by i2c_detect */
735static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
736{
737 struct i2c_client *new_client;
738 struct adm1031_data *data;
739 int err = 0;
740 const char *name = "";
741
742 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
743 goto exit;
744
745 if (!(data = kmalloc(sizeof(struct adm1031_data), GFP_KERNEL))) {
746 err = -ENOMEM;
747 goto exit;
748 }
749 memset(data, 0, sizeof(struct adm1031_data));
750
751 new_client = &data->client;
752 i2c_set_clientdata(new_client, data);
753 new_client->addr = address;
754 new_client->adapter = adapter;
755 new_client->driver = &adm1031_driver;
756 new_client->flags = 0;
757
758 if (kind < 0) {
759 int id, co;
760 id = i2c_smbus_read_byte_data(new_client, 0x3d);
761 co = i2c_smbus_read_byte_data(new_client, 0x3e);
762
763 if (!((id == 0x31 || id == 0x30) && co == 0x41))
764 goto exit_free;
765 kind = (id == 0x30) ? adm1030 : adm1031;
766 }
767
768 if (kind <= 0)
769 kind = adm1031;
770
771 /* Given the detected chip type, set the chip name and the
772 * auto fan control helper table. */
773 if (kind == adm1030) {
774 name = "adm1030";
775 data->chan_select_table = &auto_channel_select_table_adm1030;
776 } else if (kind == adm1031) {
777 name = "adm1031";
778 data->chan_select_table = &auto_channel_select_table_adm1031;
779 }
780 data->chip_type = kind;
781
782 strlcpy(new_client->name, name, I2C_NAME_SIZE);
783 data->valid = 0;
784 init_MUTEX(&data->update_lock);
785
786 /* Tell the I2C layer a new client has arrived */
787 if ((err = i2c_attach_client(new_client)))
788 goto exit_free;
789
790 /* Initialize the ADM1031 chip */
791 adm1031_init_client(new_client);
792
793 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400794 data->class_dev = hwmon_device_register(&new_client->dev);
795 if (IS_ERR(data->class_dev)) {
796 err = PTR_ERR(data->class_dev);
797 goto exit_detach;
798 }
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 device_create_file(&new_client->dev, &dev_attr_fan1_input);
801 device_create_file(&new_client->dev, &dev_attr_fan1_div);
802 device_create_file(&new_client->dev, &dev_attr_fan1_min);
803 device_create_file(&new_client->dev, &dev_attr_pwm1);
804 device_create_file(&new_client->dev, &dev_attr_auto_fan1_channel);
805 device_create_file(&new_client->dev, &dev_attr_temp1_input);
806 device_create_file(&new_client->dev, &dev_attr_temp1_min);
807 device_create_file(&new_client->dev, &dev_attr_temp1_max);
808 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
809 device_create_file(&new_client->dev, &dev_attr_temp2_input);
810 device_create_file(&new_client->dev, &dev_attr_temp2_min);
811 device_create_file(&new_client->dev, &dev_attr_temp2_max);
812 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
813
814 device_create_file(&new_client->dev, &dev_attr_auto_temp1_off);
815 device_create_file(&new_client->dev, &dev_attr_auto_temp1_min);
816 device_create_file(&new_client->dev, &dev_attr_auto_temp1_max);
817
818 device_create_file(&new_client->dev, &dev_attr_auto_temp2_off);
819 device_create_file(&new_client->dev, &dev_attr_auto_temp2_min);
820 device_create_file(&new_client->dev, &dev_attr_auto_temp2_max);
821
822 device_create_file(&new_client->dev, &dev_attr_auto_fan1_min_pwm);
823
824 device_create_file(&new_client->dev, &dev_attr_alarms);
825
826 if (kind == adm1031) {
827 device_create_file(&new_client->dev, &dev_attr_fan2_input);
828 device_create_file(&new_client->dev, &dev_attr_fan2_div);
829 device_create_file(&new_client->dev, &dev_attr_fan2_min);
830 device_create_file(&new_client->dev, &dev_attr_pwm2);
831 device_create_file(&new_client->dev,
832 &dev_attr_auto_fan2_channel);
833 device_create_file(&new_client->dev, &dev_attr_temp3_input);
834 device_create_file(&new_client->dev, &dev_attr_temp3_min);
835 device_create_file(&new_client->dev, &dev_attr_temp3_max);
836 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
837 device_create_file(&new_client->dev, &dev_attr_auto_temp3_off);
838 device_create_file(&new_client->dev, &dev_attr_auto_temp3_min);
839 device_create_file(&new_client->dev, &dev_attr_auto_temp3_max);
840 device_create_file(&new_client->dev, &dev_attr_auto_fan2_min_pwm);
841 }
842
843 return 0;
844
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400845exit_detach:
846 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847exit_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400848 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849exit:
850 return err;
851}
852
853static int adm1031_detach_client(struct i2c_client *client)
854{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400855 struct adm1031_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 int ret;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400857
858 hwmon_device_unregister(data->class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if ((ret = i2c_detach_client(client)) != 0) {
860 return ret;
861 }
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400862 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 0;
864}
865
866static void adm1031_init_client(struct i2c_client *client)
867{
868 unsigned int read_val;
869 unsigned int mask;
870 struct adm1031_data *data = i2c_get_clientdata(client);
871
872 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
873 if (data->chip_type == adm1031) {
874 mask |= (ADM1031_CONF2_PWM2_ENABLE |
875 ADM1031_CONF2_TACH2_ENABLE);
876 }
877 /* Initialize the ADM1031 chip (enables fan speed reading ) */
878 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
879 if ((read_val | mask) != read_val) {
880 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
881 }
882
883 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
884 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
885 adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
886 ADM1031_CONF1_MONITOR_ENABLE);
887 }
888
889}
890
891static struct adm1031_data *adm1031_update_device(struct device *dev)
892{
893 struct i2c_client *client = to_i2c_client(dev);
894 struct adm1031_data *data = i2c_get_clientdata(client);
895 int chan;
896
897 down(&data->update_lock);
898
899 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
900 || !data->valid) {
901
902 dev_dbg(&client->dev, "Starting adm1031 update\n");
903 for (chan = 0;
904 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
905 u8 oldh, newh;
906
907 oldh =
908 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
909 data->ext_temp[chan] =
910 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
911 newh =
912 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
913 if (newh != oldh) {
914 data->ext_temp[chan] =
915 adm1031_read_value(client,
916 ADM1031_REG_EXT_TEMP);
917#ifdef DEBUG
918 oldh =
919 adm1031_read_value(client,
920 ADM1031_REG_TEMP(chan));
921
922 /* oldh is actually newer */
923 if (newh != oldh)
924 dev_warn(&client->dev,
925 "Remote temperature may be "
926 "wrong.\n");
927#endif
928 }
929 data->temp[chan] = newh;
930
931 data->temp_min[chan] =
932 adm1031_read_value(client,
933 ADM1031_REG_TEMP_MIN(chan));
934 data->temp_max[chan] =
935 adm1031_read_value(client,
936 ADM1031_REG_TEMP_MAX(chan));
937 data->temp_crit[chan] =
938 adm1031_read_value(client,
939 ADM1031_REG_TEMP_CRIT(chan));
940 data->auto_temp[chan] =
941 adm1031_read_value(client,
942 ADM1031_REG_AUTO_TEMP(chan));
943
944 }
945
946 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
947 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
948
949 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
950 | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
951 << 8);
952 if (data->chip_type == adm1030) {
953 data->alarm &= 0xc0ff;
954 }
955
956 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
957 data->fan_div[chan] =
958 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
959 data->fan_min[chan] =
960 adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
961 data->fan[chan] =
962 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
963 data->pwm[chan] =
964 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
965 (4*chan));
966 }
967 data->last_updated = jiffies;
968 data->valid = 1;
969 }
970
971 up(&data->update_lock);
972
973 return data;
974}
975
976static int __init sensors_adm1031_init(void)
977{
978 return i2c_add_driver(&adm1031_driver);
979}
980
981static void __exit sensors_adm1031_exit(void)
982{
983 i2c_del_driver(&adm1031_driver);
984}
985
986MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
987MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
988MODULE_LICENSE("GPL");
989
990module_init(sensors_adm1031_init);
991module_exit(sensors_adm1031_exit);