1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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/hwmon.h>
30#include <linux/hwmon-sysfs.h>
31#include <linux/err.h>
32#include <linux/mutex.h>
33
34
35#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
36#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
37#define ADM1031_REG_PWM (0x22)
38#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
39#define ADM1031_REG_FAN_FILTER (0x23)
40
41#define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
42#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
43#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
44#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
45
46#define ADM1031_REG_TEMP(nr) (0x0a + (nr))
47#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
48
49#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
50
51#define ADM1031_REG_CONF1 0x00
52#define ADM1031_REG_CONF2 0x01
53#define ADM1031_REG_EXT_TEMP 0x06
54
55#define ADM1031_CONF1_MONITOR_ENABLE 0x01
56#define ADM1031_CONF1_PWM_INVERT 0x08
57#define ADM1031_CONF1_AUTO_MODE 0x80
58
59#define ADM1031_CONF2_PWM1_ENABLE 0x01
60#define ADM1031_CONF2_PWM2_ENABLE 0x02
61#define ADM1031_CONF2_TACH1_ENABLE 0x04
62#define ADM1031_CONF2_TACH2_ENABLE 0x08
63#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
64
65#define ADM1031_UPDATE_RATE_MASK 0x1c
66#define ADM1031_UPDATE_RATE_SHIFT 2
67
68
69static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
70
71enum chips { adm1030, adm1031 };
72
73typedef u8 auto_chan_table_t[8][2];
74
75
76struct adm1031_data {
77 struct device *hwmon_dev;
78 struct mutex update_lock;
79 int chip_type;
80 char valid;
81 unsigned long last_updated;
82 unsigned int update_interval;
83
84
85
86
87 const auto_chan_table_t *chan_select_table;
88 u16 alarm;
89 u8 conf1;
90 u8 conf2;
91 u8 fan[2];
92 u8 fan_div[2];
93 u8 fan_min[2];
94 u8 pwm[2];
95 u8 old_pwm[2];
96 s8 temp[3];
97 u8 ext_temp[3];
98 u8 auto_temp[3];
99 u8 auto_temp_min[3];
100 u8 auto_temp_off[3];
101 u8 auto_temp_max[3];
102 s8 temp_offset[3];
103 s8 temp_min[3];
104 s8 temp_max[3];
105 s8 temp_crit[3];
106};
107
108static int adm1031_probe(struct i2c_client *client,
109 const struct i2c_device_id *id);
110static int adm1031_detect(struct i2c_client *client,
111 struct i2c_board_info *info);
112static void adm1031_init_client(struct i2c_client *client);
113static int adm1031_remove(struct i2c_client *client);
114static struct adm1031_data *adm1031_update_device(struct device *dev);
115
116static const struct i2c_device_id adm1031_id[] = {
117 { "adm1030", adm1030 },
118 { "adm1031", adm1031 },
119 { }
120};
121MODULE_DEVICE_TABLE(i2c, adm1031_id);
122
123
124static struct i2c_driver adm1031_driver = {
125 .class = I2C_CLASS_HWMON,
126 .driver = {
127 .name = "adm1031",
128 },
129 .probe = adm1031_probe,
130 .remove = adm1031_remove,
131 .id_table = adm1031_id,
132 .detect = adm1031_detect,
133 .address_list = normal_i2c,
134};
135
136static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
137{
138 return i2c_smbus_read_byte_data(client, reg);
139}
140
141static inline int
142adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
143{
144 return i2c_smbus_write_byte_data(client, reg, value);
145}
146
147
148#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
149 ((val + 500) / 1000)))
150
151#define TEMP_FROM_REG(val) ((val) * 1000)
152
153#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
154
155#define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
156#define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
157 (val) | 0x70 : (val))
158
159#define FAN_FROM_REG(reg, div) ((reg) ? \
160 (11250 * 60) / ((reg) * (div)) : 0)
161
162static int FAN_TO_REG(int reg, int div)
163{
164 int tmp;
165 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
166 return tmp > 255 ? 255 : tmp;
167}
168
169#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
170
171#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
172#define PWM_FROM_REG(val) ((val) << 4)
173
174#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
175#define FAN_CHAN_TO_REG(val, reg) \
176 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
177
178#define AUTO_TEMP_MIN_TO_REG(val, reg) \
179 ((((val) / 500) & 0xf8) | ((reg) & 0x7))
180#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7)))
181#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
182
183#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
184
185#define AUTO_TEMP_OFF_FROM_REG(reg) \
186 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
187
188#define AUTO_TEMP_MAX_FROM_REG(reg) \
189 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
190 AUTO_TEMP_MIN_FROM_REG(reg))
191
192static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
193{
194 int ret;
195 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
196
197 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
198 ret = ((reg & 0xf8) |
199 (range < 10000 ? 0 :
200 range < 20000 ? 1 :
201 range < 40000 ? 2 : range < 80000 ? 3 : 4));
202 return ret;
203}
204
205
206#define GET_FAN_AUTO_BITFIELD(data, idx) \
207 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
208
209
210
211
212
213
214
215static const auto_chan_table_t auto_channel_select_table_adm1031 = {
216 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
217 { 2 , 4 },
218 { 2 , 2 },
219 { 4 , 4 },
220 { 7 , 7 },
221};
222
223static const auto_chan_table_t auto_channel_select_table_adm1030 = {
224 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
225 { 2 , 0 },
226 { 0xff , 0 },
227 { 0xff , 0 },
228 { 3 , 0 },
229};
230
231
232
233
234
235static int
236get_fan_auto_nearest(struct adm1031_data *data, int chan, u8 val, u8 reg)
237{
238 int i;
239 int first_match = -1, exact_match = -1;
240 u8 other_reg_val =
241 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
242
243 if (val == 0)
244 return 0;
245
246 for (i = 0; i < 8; i++) {
247 if ((val == (*data->chan_select_table)[i][chan]) &&
248 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
249 other_reg_val)) {
250
251 exact_match = i;
252 break;
253 } else if (val == (*data->chan_select_table)[i][chan] &&
254 first_match == -1) {
255
256
257
258
259 first_match = i;
260 }
261 }
262
263 if (exact_match >= 0)
264 return exact_match;
265 else if (first_match >= 0)
266 return first_match;
267
268 return -EINVAL;
269}
270
271static ssize_t show_fan_auto_channel(struct device *dev,
272 struct device_attribute *attr, char *buf)
273{
274 int nr = to_sensor_dev_attr(attr)->index;
275 struct adm1031_data *data = adm1031_update_device(dev);
276 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
277}
278
279static ssize_t
280set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
281 const char *buf, size_t count)
282{
283 struct i2c_client *client = to_i2c_client(dev);
284 struct adm1031_data *data = i2c_get_clientdata(client);
285 int nr = to_sensor_dev_attr(attr)->index;
286 long val;
287 u8 reg;
288 int ret;
289 u8 old_fan_mode;
290
291 ret = kstrtol(buf, 10, &val);
292 if (ret)
293 return ret;
294
295 old_fan_mode = data->conf1;
296
297 mutex_lock(&data->update_lock);
298
299 ret = get_fan_auto_nearest(data, nr, val, data->conf1);
300 if (ret < 0) {
301 mutex_unlock(&data->update_lock);
302 return ret;
303 }
304 reg = ret;
305 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
306 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
307 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
308 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
309
310
311
312
313
314 data->old_pwm[0] = data->pwm[0];
315 data->old_pwm[1] = data->pwm[1];
316 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
317 } else {
318
319 data->pwm[0] = data->old_pwm[0];
320 data->pwm[1] = data->old_pwm[1];
321
322 adm1031_write_value(client, ADM1031_REG_PWM,
323 data->pwm[0] | (data->pwm[1] << 4));
324 }
325 }
326 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
327 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
328 mutex_unlock(&data->update_lock);
329 return count;
330}
331
332static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
333 show_fan_auto_channel, set_fan_auto_channel, 0);
334static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
335 show_fan_auto_channel, set_fan_auto_channel, 1);
336
337
338static ssize_t show_auto_temp_off(struct device *dev,
339 struct device_attribute *attr, char *buf)
340{
341 int nr = to_sensor_dev_attr(attr)->index;
342 struct adm1031_data *data = adm1031_update_device(dev);
343 return sprintf(buf, "%d\n",
344 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
345}
346static ssize_t show_auto_temp_min(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
349 int nr = to_sensor_dev_attr(attr)->index;
350 struct adm1031_data *data = adm1031_update_device(dev);
351 return sprintf(buf, "%d\n",
352 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
353}
354static ssize_t
355set_auto_temp_min(struct device *dev, struct device_attribute *attr,
356 const char *buf, size_t count)
357{
358 struct i2c_client *client = to_i2c_client(dev);
359 struct adm1031_data *data = i2c_get_clientdata(client);
360 int nr = to_sensor_dev_attr(attr)->index;
361 long val;
362 int ret;
363
364 ret = kstrtol(buf, 10, &val);
365 if (ret)
366 return ret;
367
368 mutex_lock(&data->update_lock);
369 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
370 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
371 data->auto_temp[nr]);
372 mutex_unlock(&data->update_lock);
373 return count;
374}
375static ssize_t show_auto_temp_max(struct device *dev,
376 struct device_attribute *attr, char *buf)
377{
378 int nr = to_sensor_dev_attr(attr)->index;
379 struct adm1031_data *data = adm1031_update_device(dev);
380 return sprintf(buf, "%d\n",
381 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
382}
383static ssize_t
384set_auto_temp_max(struct device *dev, struct device_attribute *attr,
385 const char *buf, size_t count)
386{
387 struct i2c_client *client = to_i2c_client(dev);
388 struct adm1031_data *data = i2c_get_clientdata(client);
389 int nr = to_sensor_dev_attr(attr)->index;
390 long val;
391 int ret;
392
393 ret = kstrtol(buf, 10, &val);
394 if (ret)
395 return ret;
396
397 mutex_lock(&data->update_lock);
398 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
399 data->pwm[nr]);
400 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
401 data->temp_max[nr]);
402 mutex_unlock(&data->update_lock);
403 return count;
404}
405
406#define auto_temp_reg(offset) \
407static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
408 show_auto_temp_off, NULL, offset - 1); \
409static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
410 show_auto_temp_min, set_auto_temp_min, offset - 1); \
411static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
412 show_auto_temp_max, set_auto_temp_max, offset - 1)
413
414auto_temp_reg(1);
415auto_temp_reg(2);
416auto_temp_reg(3);
417
418
419static ssize_t show_pwm(struct device *dev,
420 struct device_attribute *attr, char *buf)
421{
422 int nr = to_sensor_dev_attr(attr)->index;
423 struct adm1031_data *data = adm1031_update_device(dev);
424 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
425}
426static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
427 const char *buf, size_t count)
428{
429 struct i2c_client *client = to_i2c_client(dev);
430 struct adm1031_data *data = i2c_get_clientdata(client);
431 int nr = to_sensor_dev_attr(attr)->index;
432 long val;
433 int ret, reg;
434
435 ret = kstrtol(buf, 10, &val);
436 if (ret)
437 return ret;
438
439 mutex_lock(&data->update_lock);
440 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
441 (((val>>4) & 0xf) != 5)) {
442
443 mutex_unlock(&data->update_lock);
444 return -EINVAL;
445 }
446 data->pwm[nr] = PWM_TO_REG(val);
447 reg = adm1031_read_value(client, ADM1031_REG_PWM);
448 adm1031_write_value(client, ADM1031_REG_PWM,
449 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
450 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
451 mutex_unlock(&data->update_lock);
452 return count;
453}
454
455static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
456static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
457static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
458 show_pwm, set_pwm, 0);
459static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
460 show_pwm, set_pwm, 1);
461
462
463
464
465
466
467
468
469static int trust_fan_readings(struct adm1031_data *data, int chan)
470{
471 int res = 0;
472
473 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
474 switch (data->conf1 & 0x60) {
475 case 0x00:
476
477
478
479
480 res = data->temp[chan+1] >=
481 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
482 break;
483 case 0x20:
484 res =
485 data->temp[1] >=
486 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
487 break;
488 case 0x40:
489 res =
490 data->temp[2] >=
491 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
492 break;
493 case 0x60:
494 res =
495 data->temp[0] >=
496 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
497 || data->temp[1] >=
498 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
499 || (data->chip_type == adm1031
500 && data->temp[2] >=
501 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
502 break;
503 }
504 } else {
505 res = data->pwm[chan] > 0;
506 }
507 return res;
508}
509
510
511static ssize_t show_fan(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
514 int nr = to_sensor_dev_attr(attr)->index;
515 struct adm1031_data *data = adm1031_update_device(dev);
516 int value;
517
518 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
519 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
520 return sprintf(buf, "%d\n", value);
521}
522
523static ssize_t show_fan_div(struct device *dev,
524 struct device_attribute *attr, char *buf)
525{
526 int nr = to_sensor_dev_attr(attr)->index;
527 struct adm1031_data *data = adm1031_update_device(dev);
528 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
529}
530static ssize_t show_fan_min(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
533 int nr = to_sensor_dev_attr(attr)->index;
534 struct adm1031_data *data = adm1031_update_device(dev);
535 return sprintf(buf, "%d\n",
536 FAN_FROM_REG(data->fan_min[nr],
537 FAN_DIV_FROM_REG(data->fan_div[nr])));
538}
539static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
540 const char *buf, size_t count)
541{
542 struct i2c_client *client = to_i2c_client(dev);
543 struct adm1031_data *data = i2c_get_clientdata(client);
544 int nr = to_sensor_dev_attr(attr)->index;
545 long val;
546 int ret;
547
548 ret = kstrtol(buf, 10, &val);
549 if (ret)
550 return ret;
551
552 mutex_lock(&data->update_lock);
553 if (val) {
554 data->fan_min[nr] =
555 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
556 } else {
557 data->fan_min[nr] = 0xff;
558 }
559 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
560 mutex_unlock(&data->update_lock);
561 return count;
562}
563static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
565{
566 struct i2c_client *client = to_i2c_client(dev);
567 struct adm1031_data *data = i2c_get_clientdata(client);
568 int nr = to_sensor_dev_attr(attr)->index;
569 long val;
570 u8 tmp;
571 int old_div;
572 int new_min;
573 int ret;
574
575 ret = kstrtol(buf, 10, &val);
576 if (ret)
577 return ret;
578
579 tmp = val == 8 ? 0xc0 :
580 val == 4 ? 0x80 :
581 val == 2 ? 0x40 :
582 val == 1 ? 0x00 :
583 0xff;
584 if (tmp == 0xff)
585 return -EINVAL;
586
587 mutex_lock(&data->update_lock);
588
589 data->fan_div[nr] = adm1031_read_value(client,
590 ADM1031_REG_FAN_DIV(nr));
591 data->fan_min[nr] = adm1031_read_value(client,
592 ADM1031_REG_FAN_MIN(nr));
593
594
595 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
596 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
597 new_min = data->fan_min[nr] * old_div / val;
598 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
599
600 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
601 data->fan_div[nr]);
602 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
603 data->fan_min[nr]);
604
605
606 data->valid = 0;
607 mutex_unlock(&data->update_lock);
608 return count;
609}
610
611#define fan_offset(offset) \
612static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
613 show_fan, NULL, offset - 1); \
614static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
615 show_fan_min, set_fan_min, offset - 1); \
616static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
617 show_fan_div, set_fan_div, offset - 1)
618
619fan_offset(1);
620fan_offset(2);
621
622
623
624static ssize_t show_temp(struct device *dev,
625 struct device_attribute *attr, char *buf)
626{
627 int nr = to_sensor_dev_attr(attr)->index;
628 struct adm1031_data *data = adm1031_update_device(dev);
629 int ext;
630 ext = nr == 0 ?
631 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
632 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
633 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
634}
635static ssize_t show_temp_offset(struct device *dev,
636 struct device_attribute *attr, char *buf)
637{
638 int nr = to_sensor_dev_attr(attr)->index;
639 struct adm1031_data *data = adm1031_update_device(dev);
640 return sprintf(buf, "%d\n",
641 TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
642}
643static ssize_t show_temp_min(struct device *dev,
644 struct device_attribute *attr, char *buf)
645{
646 int nr = to_sensor_dev_attr(attr)->index;
647 struct adm1031_data *data = adm1031_update_device(dev);
648 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
649}
650static ssize_t show_temp_max(struct device *dev,
651 struct device_attribute *attr, char *buf)
652{
653 int nr = to_sensor_dev_attr(attr)->index;
654 struct adm1031_data *data = adm1031_update_device(dev);
655 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
656}
657static ssize_t show_temp_crit(struct device *dev,
658 struct device_attribute *attr, char *buf)
659{
660 int nr = to_sensor_dev_attr(attr)->index;
661 struct adm1031_data *data = adm1031_update_device(dev);
662 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
663}
664static ssize_t set_temp_offset(struct device *dev,
665 struct device_attribute *attr, const char *buf,
666 size_t count)
667{
668 struct i2c_client *client = to_i2c_client(dev);
669 struct adm1031_data *data = i2c_get_clientdata(client);
670 int nr = to_sensor_dev_attr(attr)->index;
671 long val;
672 int ret;
673
674 ret = kstrtol(buf, 10, &val);
675 if (ret)
676 return ret;
677
678 val = SENSORS_LIMIT(val, -15000, 15000);
679 mutex_lock(&data->update_lock);
680 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
681 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
682 data->temp_offset[nr]);
683 mutex_unlock(&data->update_lock);
684 return count;
685}
686static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
687 const char *buf, size_t count)
688{
689 struct i2c_client *client = to_i2c_client(dev);
690 struct adm1031_data *data = i2c_get_clientdata(client);
691 int nr = to_sensor_dev_attr(attr)->index;
692 long val;
693 int ret;
694
695 ret = kstrtol(buf, 10, &val);
696 if (ret)
697 return ret;
698
699 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
700 mutex_lock(&data->update_lock);
701 data->temp_min[nr] = TEMP_TO_REG(val);
702 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
703 data->temp_min[nr]);
704 mutex_unlock(&data->update_lock);
705 return count;
706}
707static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
709{
710 struct i2c_client *client = to_i2c_client(dev);
711 struct adm1031_data *data = i2c_get_clientdata(client);
712 int nr = to_sensor_dev_attr(attr)->index;
713 long val;
714 int ret;
715
716 ret = kstrtol(buf, 10, &val);
717 if (ret)
718 return ret;
719
720 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
721 mutex_lock(&data->update_lock);
722 data->temp_max[nr] = TEMP_TO_REG(val);
723 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
724 data->temp_max[nr]);
725 mutex_unlock(&data->update_lock);
726 return count;
727}
728static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
729 const char *buf, size_t count)
730{
731 struct i2c_client *client = to_i2c_client(dev);
732 struct adm1031_data *data = i2c_get_clientdata(client);
733 int nr = to_sensor_dev_attr(attr)->index;
734 long val;
735 int ret;
736
737 ret = kstrtol(buf, 10, &val);
738 if (ret)
739 return ret;
740
741 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
742 mutex_lock(&data->update_lock);
743 data->temp_crit[nr] = TEMP_TO_REG(val);
744 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
745 data->temp_crit[nr]);
746 mutex_unlock(&data->update_lock);
747 return count;
748}
749
750#define temp_reg(offset) \
751static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
752 show_temp, NULL, offset - 1); \
753static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
754 show_temp_offset, set_temp_offset, offset - 1); \
755static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
756 show_temp_min, set_temp_min, offset - 1); \
757static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
758 show_temp_max, set_temp_max, offset - 1); \
759static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
760 show_temp_crit, set_temp_crit, offset - 1)
761
762temp_reg(1);
763temp_reg(2);
764temp_reg(3);
765
766
767static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
768 char *buf)
769{
770 struct adm1031_data *data = adm1031_update_device(dev);
771 return sprintf(buf, "%d\n", data->alarm);
772}
773
774static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
775
776static ssize_t show_alarm(struct device *dev,
777 struct device_attribute *attr, char *buf)
778{
779 int bitnr = to_sensor_dev_attr(attr)->index;
780 struct adm1031_data *data = adm1031_update_device(dev);
781 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
782}
783
784static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
785static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
786static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
787static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
788static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
789static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
790static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
791static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
792static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
793static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
794static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
795static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
796static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
797static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
798static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
799
800
801static const unsigned int update_intervals[] = {
802 16000, 8000, 4000, 2000, 1000, 500, 250, 125,
803};
804
805static ssize_t show_update_interval(struct device *dev,
806 struct device_attribute *attr, char *buf)
807{
808 struct i2c_client *client = to_i2c_client(dev);
809 struct adm1031_data *data = i2c_get_clientdata(client);
810
811 return sprintf(buf, "%u\n", data->update_interval);
812}
813
814static ssize_t set_update_interval(struct device *dev,
815 struct device_attribute *attr,
816 const char *buf, size_t count)
817{
818 struct i2c_client *client = to_i2c_client(dev);
819 struct adm1031_data *data = i2c_get_clientdata(client);
820 unsigned long val;
821 int i, err;
822 u8 reg;
823
824 err = kstrtoul(buf, 10, &val);
825 if (err)
826 return err;
827
828
829
830
831
832 for (i = 0; i < ARRAY_SIZE(update_intervals) - 1; i++) {
833 if (val >= update_intervals[i])
834 break;
835 }
836
837
838
839 reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
840 reg &= ~ADM1031_UPDATE_RATE_MASK;
841 reg |= i << ADM1031_UPDATE_RATE_SHIFT;
842 adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg);
843
844 mutex_lock(&data->update_lock);
845 data->update_interval = update_intervals[i];
846 mutex_unlock(&data->update_lock);
847
848 return count;
849}
850
851static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
852 set_update_interval);
853
854static struct attribute *adm1031_attributes[] = {
855 &sensor_dev_attr_fan1_input.dev_attr.attr,
856 &sensor_dev_attr_fan1_div.dev_attr.attr,
857 &sensor_dev_attr_fan1_min.dev_attr.attr,
858 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
859 &sensor_dev_attr_fan1_fault.dev_attr.attr,
860 &sensor_dev_attr_pwm1.dev_attr.attr,
861 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
862 &sensor_dev_attr_temp1_input.dev_attr.attr,
863 &sensor_dev_attr_temp1_offset.dev_attr.attr,
864 &sensor_dev_attr_temp1_min.dev_attr.attr,
865 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
866 &sensor_dev_attr_temp1_max.dev_attr.attr,
867 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
868 &sensor_dev_attr_temp1_crit.dev_attr.attr,
869 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
870 &sensor_dev_attr_temp2_input.dev_attr.attr,
871 &sensor_dev_attr_temp2_offset.dev_attr.attr,
872 &sensor_dev_attr_temp2_min.dev_attr.attr,
873 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
874 &sensor_dev_attr_temp2_max.dev_attr.attr,
875 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
876 &sensor_dev_attr_temp2_crit.dev_attr.attr,
877 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
878 &sensor_dev_attr_temp2_fault.dev_attr.attr,
879
880 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
881 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
882 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
883
884 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
885 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
886 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
887
888 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
889
890 &dev_attr_update_interval.attr,
891 &dev_attr_alarms.attr,
892
893 NULL
894};
895
896static const struct attribute_group adm1031_group = {
897 .attrs = adm1031_attributes,
898};
899
900static struct attribute *adm1031_attributes_opt[] = {
901 &sensor_dev_attr_fan2_input.dev_attr.attr,
902 &sensor_dev_attr_fan2_div.dev_attr.attr,
903 &sensor_dev_attr_fan2_min.dev_attr.attr,
904 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
905 &sensor_dev_attr_fan2_fault.dev_attr.attr,
906 &sensor_dev_attr_pwm2.dev_attr.attr,
907 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
908 &sensor_dev_attr_temp3_input.dev_attr.attr,
909 &sensor_dev_attr_temp3_offset.dev_attr.attr,
910 &sensor_dev_attr_temp3_min.dev_attr.attr,
911 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
912 &sensor_dev_attr_temp3_max.dev_attr.attr,
913 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
914 &sensor_dev_attr_temp3_crit.dev_attr.attr,
915 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
916 &sensor_dev_attr_temp3_fault.dev_attr.attr,
917 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
918 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
919 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
920 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
921 NULL
922};
923
924static const struct attribute_group adm1031_group_opt = {
925 .attrs = adm1031_attributes_opt,
926};
927
928
929static int adm1031_detect(struct i2c_client *client,
930 struct i2c_board_info *info)
931{
932 struct i2c_adapter *adapter = client->adapter;
933 const char *name;
934 int id, co;
935
936 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
937 return -ENODEV;
938
939 id = i2c_smbus_read_byte_data(client, 0x3d);
940 co = i2c_smbus_read_byte_data(client, 0x3e);
941
942 if (!((id == 0x31 || id == 0x30) && co == 0x41))
943 return -ENODEV;
944 name = (id == 0x30) ? "adm1030" : "adm1031";
945
946 strlcpy(info->type, name, I2C_NAME_SIZE);
947
948 return 0;
949}
950
951static int adm1031_probe(struct i2c_client *client,
952 const struct i2c_device_id *id)
953{
954 struct adm1031_data *data;
955 int err;
956
957 data = devm_kzalloc(&client->dev, sizeof(struct adm1031_data),
958 GFP_KERNEL);
959 if (!data)
960 return -ENOMEM;
961
962 i2c_set_clientdata(client, data);
963 data->chip_type = id->driver_data;
964 mutex_init(&data->update_lock);
965
966 if (data->chip_type == adm1030)
967 data->chan_select_table = &auto_channel_select_table_adm1030;
968 else
969 data->chan_select_table = &auto_channel_select_table_adm1031;
970
971
972 adm1031_init_client(client);
973
974
975 err = sysfs_create_group(&client->dev.kobj, &adm1031_group);
976 if (err)
977 return err;
978
979 if (data->chip_type == adm1031) {
980 err = sysfs_create_group(&client->dev.kobj, &adm1031_group_opt);
981 if (err)
982 goto exit_remove;
983 }
984
985 data->hwmon_dev = hwmon_device_register(&client->dev);
986 if (IS_ERR(data->hwmon_dev)) {
987 err = PTR_ERR(data->hwmon_dev);
988 goto exit_remove;
989 }
990
991 return 0;
992
993exit_remove:
994 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
995 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
996 return err;
997}
998
999static int adm1031_remove(struct i2c_client *client)
1000{
1001 struct adm1031_data *data = i2c_get_clientdata(client);
1002
1003 hwmon_device_unregister(data->hwmon_dev);
1004 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
1005 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
1006 return 0;
1007}
1008
1009static void adm1031_init_client(struct i2c_client *client)
1010{
1011 unsigned int read_val;
1012 unsigned int mask;
1013 int i;
1014 struct adm1031_data *data = i2c_get_clientdata(client);
1015
1016 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
1017 if (data->chip_type == adm1031) {
1018 mask |= (ADM1031_CONF2_PWM2_ENABLE |
1019 ADM1031_CONF2_TACH2_ENABLE);
1020 }
1021
1022 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
1023 if ((read_val | mask) != read_val)
1024 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
1025
1026 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
1027 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
1028 adm1031_write_value(client, ADM1031_REG_CONF1,
1029 read_val | ADM1031_CONF1_MONITOR_ENABLE);
1030 }
1031
1032
1033 mask = ADM1031_UPDATE_RATE_MASK;
1034 read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
1035 i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT;
1036
1037 data->update_interval = update_intervals[i];
1038}
1039
1040static struct adm1031_data *adm1031_update_device(struct device *dev)
1041{
1042 struct i2c_client *client = to_i2c_client(dev);
1043 struct adm1031_data *data = i2c_get_clientdata(client);
1044 unsigned long next_update;
1045 int chan;
1046
1047 mutex_lock(&data->update_lock);
1048
1049 next_update = data->last_updated
1050 + msecs_to_jiffies(data->update_interval);
1051 if (time_after(jiffies, next_update) || !data->valid) {
1052
1053 dev_dbg(&client->dev, "Starting adm1031 update\n");
1054 for (chan = 0;
1055 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
1056 u8 oldh, newh;
1057
1058 oldh =
1059 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
1060 data->ext_temp[chan] =
1061 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
1062 newh =
1063 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
1064 if (newh != oldh) {
1065 data->ext_temp[chan] =
1066 adm1031_read_value(client,
1067 ADM1031_REG_EXT_TEMP);
1068#ifdef DEBUG
1069 oldh =
1070 adm1031_read_value(client,
1071 ADM1031_REG_TEMP(chan));
1072
1073
1074 if (newh != oldh)
1075 dev_warn(&client->dev,
1076 "Remote temperature may be wrong.\n");
1077#endif
1078 }
1079 data->temp[chan] = newh;
1080
1081 data->temp_offset[chan] =
1082 adm1031_read_value(client,
1083 ADM1031_REG_TEMP_OFFSET(chan));
1084 data->temp_min[chan] =
1085 adm1031_read_value(client,
1086 ADM1031_REG_TEMP_MIN(chan));
1087 data->temp_max[chan] =
1088 adm1031_read_value(client,
1089 ADM1031_REG_TEMP_MAX(chan));
1090 data->temp_crit[chan] =
1091 adm1031_read_value(client,
1092 ADM1031_REG_TEMP_CRIT(chan));
1093 data->auto_temp[chan] =
1094 adm1031_read_value(client,
1095 ADM1031_REG_AUTO_TEMP(chan));
1096
1097 }
1098
1099 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
1100 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
1101
1102 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
1103 | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) << 8);
1104 if (data->chip_type == adm1030)
1105 data->alarm &= 0xc0ff;
1106
1107 for (chan = 0; chan < (data->chip_type == adm1030 ? 1 : 2);
1108 chan++) {
1109 data->fan_div[chan] =
1110 adm1031_read_value(client,
1111 ADM1031_REG_FAN_DIV(chan));
1112 data->fan_min[chan] =
1113 adm1031_read_value(client,
1114 ADM1031_REG_FAN_MIN(chan));
1115 data->fan[chan] =
1116 adm1031_read_value(client,
1117 ADM1031_REG_FAN_SPEED(chan));
1118 data->pwm[chan] =
1119 (adm1031_read_value(client,
1120 ADM1031_REG_PWM) >> (4 * chan)) & 0x0f;
1121 }
1122 data->last_updated = jiffies;
1123 data->valid = 1;
1124 }
1125
1126 mutex_unlock(&data->update_lock);
1127
1128 return data;
1129}
1130
1131module_i2c_driver(adm1031_driver);
1132
1133MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1134MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1135MODULE_LICENSE("GPL");
1136